/
fibers.asm
73 lines (61 loc) · 1.66 KB
/
fibers.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
; Copyright 2021 Google LLC
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
[BITS 64]
[GLOBAL fiber_single_parameter_entrypoint]
[GLOBAL switch_with_fiber]
[GLOBAL jump_to_fiber]
; The fiber entrypoint that jumps to a function that takes a single parameter.
; We pass the parameters on the stack when we create a fiber, so we need to convert
; it to the System V calling convention.
fiber_single_parameter_entrypoint:
; Get the function parameter
mov rdi, [rsp + 8]
; Jump to the function
ret
; Switches context with a different fiber.
; rdi = new context
; rsi = old context
switch_with_fiber:
; Remember stack
mov rax, rsp
; Point stack to top of context.
add rsi, 8 * 7
mov rsp, rsi
; Push old stack
push rax
; Callee preserved registers
push rbp
push rbx
push r12
push r13
push r14
push r15
; Fall through to jump_to_fiber
; Jumps to a different fiber's context without saving the
; existing fider.
; rdi - new context
jump_to_fiber:
; Point to the bottom of the new context.
mov rsp, rdi
; Load callee-preserved registers
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
; Pop old stack
pop rsp
; Jump into the call location at the top of the stack.
ret