-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_context.s
70 lines (51 loc) · 1.19 KB
/
prepare_context.s
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
.global PrepareContextInBuffer
.global SwitchBufferContext
PrepareContextInBuffer:
# rdi, rsi, rdx, rcx, r8, r9
# 1. %rdi - buffer
# 2. %rsi - CallRunner
# 3. %rdx - runner
# Switch to stack from buffer
movq %rsp, %r11 # save rsp to stack
movq %rdi, %rsp # load buffer to rsp
subq $64, %rsp # Just to be safe
# buffer stack must be 16-byte aligned
andq $-16, %rsp
addq $8, %rsp
# 3-thd arg (runner)
# mov to 7-th argument in buffer
movq %rdx, 8(%rsp)
# Prepare for context switching
pushq %rsi # push CallRunner
# Callee-saved registers
pushq $0 # r15
pushq $0 # r14
pushq $0 # r13
pushq $0 # r12
pushq $0 # rbp
pushq $0 # rbx
# Return value for PrepareContextInBuffer
# rsp to buffer
movq %rsp, %rax
# Restore callstack
movq %r11, %rsp # rsp := tmp
retq
SwitchBufferContext:
# rdi, rsi, rdx, rcx, r8, r9
# 1. %rdi - source
# 2. %rsi - destination
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rbp
movq %rsp, (%rdi)
movq (%rsi), %rsp
pop %rbp
pop %rbx
pop %r12
pop %r13
pop %r14
pop %r15
retq