code / tomo-coroutines

Lines715 C545 Assembly92 Tomo47 Markdown31
(208 lines)
1 .text
2 .globl aco_yield_asm
3 #if defined(__APPLE__)
4 #else
5 .type aco_yield_asm, @function
6 #endif
7 .intel_syntax noprefix
8 aco_yield_asm:
9 /*
10 extern void aco_yield_asm(aco_t* from_co, aco_t* to_co);
12 struct aco_t {
13 void* reg[X];
14 // ...
17 reference:
18 https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI
20 pitfall:
21 http://man7.org/linux/man-pages/man7/signal.7.html
22 http://man7.org/linux/man-pages/man2/sigaltstack.2.html
24 > $ man 7 signal
25 > ...
26 > By default, the signal handler is invoked on the normal process
27 > stack. It is possible to arrange that the signal handler
28 > uses an alternate stack; see sigaltstack(2) for a discussion of
29 > how to do this and when it might be useful.
30 > ...
32 This is a BUG example:
33 https://github.com/Tencent/libco/blob/v1.0/coctx_swap.S#L27
35 proof of correctness:
36 https://github.com/hnes/libaco
38 mxcsr & fpu:
39 fnstcw * m2byte
40 Store FPU control word to m2byte without checking for
41 pending unmasked floating-point exceptions.
43 fldcw m2byte
44 Load FPU control word from m2byte.
46 stmxcsr m32
47 Store contents of MXCSR register to m32
49 ldmxcsr m32
50 Load MXCSR register from m32.
51 */
52 /*
53 0x00 --> 0xff
54 eip esp ebp edi esi ebx fpucw16 mxcsr32
55 0 4 8 c 10 14 18 1c
56 */
57 #ifdef __i386__
58 mov eax,DWORD PTR [esp+0x4] // from_co
59 mov edx,DWORD PTR [esp] // retaddr
60 lea ecx,[esp+0x4] // esp
61 mov DWORD PTR [eax+0x8],ebp //<ebp
62 mov DWORD PTR [eax+0x4],ecx //<esp
63 mov DWORD PTR [eax+0x0],edx //<retaddr
64 mov DWORD PTR [eax+0xc],edi //<edi
65 mov ecx,DWORD PTR [esp+0x8] // to_co
66 mov DWORD PTR [eax+0x10],esi //<esi
67 mov DWORD PTR [eax+0x14],ebx //<ebx
68 #ifndef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
69 fnstcw WORD PTR [eax+0x18] //<fpucw
70 stmxcsr DWORD PTR [eax+0x1c] //<mxcsr
71 #endif
72 mov edx,DWORD PTR [ecx+0x4] //>esp
73 mov ebp,DWORD PTR [ecx+0x8] //>ebp
74 mov eax,DWORD PTR [ecx+0x0] //>retaddr
75 mov edi,DWORD PTR [ecx+0xc] //>edi
76 mov esi,DWORD PTR [ecx+0x10] //>esi
77 mov ebx,DWORD PTR [ecx+0x14] //>ebx
78 #ifndef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
79 fldcw WORD PTR [ecx+0x18] //>fpucw
80 ldmxcsr DWORD PTR [ecx+0x1c] //>mxcsr
81 #endif
82 xor ecx,ecx
83 mov esp,edx
84 mov edx,eax
86 // Pass the user-provided argument as first argument:
87 #ifdef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
88 mov eax,DWORD PTR [ecx+0x24]
89 #else
90 mov eax,DWORD PTR [ecx+0x28]
91 #endif
93 jmp edx
94 #elif __x86_64__
95 /*
96 0x00 --> 0xff
97 r12 r13 r14 r15 rip rsp rbx rbp fpucw16 mxcsr32
98 0 8 10 18 20 28 30 38 40 44
99 */
100 // rdi - from_co | rsi - to_co
101 mov rdx,QWORD PTR [rsp] // retaddr
102 lea rcx,[rsp+0x8] // rsp
103 mov QWORD PTR [rdi+0x0], r12
104 mov QWORD PTR [rdi+0x8], r13
105 mov QWORD PTR [rdi+0x10],r14
106 mov QWORD PTR [rdi+0x18],r15
107 mov QWORD PTR [rdi+0x20],rdx // retaddr
108 mov QWORD PTR [rdi+0x28],rcx // rsp
109 mov QWORD PTR [rdi+0x30],rbx
110 mov QWORD PTR [rdi+0x38],rbp
111 #ifndef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
112 fnstcw WORD PTR [rdi+0x40]
113 stmxcsr DWORD PTR [rdi+0x44]
114 #endif
115 mov r12,QWORD PTR [rsi+0x0]
116 mov r13,QWORD PTR [rsi+0x8]
117 mov r14,QWORD PTR [rsi+0x10]
118 mov r15,QWORD PTR [rsi+0x18]
119 mov rax,QWORD PTR [rsi+0x20] // retaddr
120 mov rcx,QWORD PTR [rsi+0x28] // rsp
121 mov rbx,QWORD PTR [rsi+0x30]
122 mov rbp,QWORD PTR [rsi+0x38]
123 #ifndef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
124 fldcw WORD PTR [rsi+0x40]
125 ldmxcsr DWORD PTR [rsi+0x44]
126 #endif
128 // Pass the user-provided argument as first argument:
129 #ifdef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
130 mov rdi,QWORD PTR [rsi+0x48]
131 #else
132 mov rdi,QWORD PTR [rsi+0x50]
133 #endif
135 mov rsp,rcx
136 jmp rax
137 #else
138 #error "platform not supported"
139 #endif
141 .globl aco_save_fpucw_mxcsr
142 #if defined(__APPLE__)
143 #else
144 .type aco_save_fpucw_mxcsr, @function
145 #endif
146 .intel_syntax noprefix
147 aco_save_fpucw_mxcsr:
148 #ifdef __i386__
149 mov eax,DWORD PTR [esp+0x4] // ptr
150 fnstcw WORD PTR [eax]
151 stmxcsr DWORD PTR [eax+0x4]
152 ret
153 #elif __x86_64__
154 fnstcw WORD PTR [rdi]
155 stmxcsr DWORD PTR [rdi+0x4]
156 ret
157 #else
158 #error "platform not supported"
159 #endif
161 #if defined(__APPLE__)
162 .globl _abort
163 .globl _aco_funcp_protector
164 #else
165 .globl abort
166 .globl aco_funcp_protector
167 #endif
169 .globl aco_funcp_protector_asm
170 #if defined(__APPLE__)
171 #else
172 .type aco_funcp_protector_asm, @function
173 #endif
174 .intel_syntax noprefix
175 aco_funcp_protector_asm:
176 #ifdef __i386__
177 and esp,0xfffffff0
178 #if defined(__APPLE__)
179 call _aco_funcp_protector
180 call _abort
181 #else
182 #if defined(__pic__) || defined(__PIC__)
183 call aco_funcp_protector@PLT
184 call abort@PLT
185 #else
186 call aco_funcp_protector
187 call abort
188 #endif
189 #endif
190 ret
191 #elif __x86_64__
192 and rsp,0xfffffffffffffff0
193 #if defined(__APPLE__)
194 call _aco_funcp_protector
195 call _abort
196 #else
197 #if defined(__pic__) || defined(__PIC__)
198 call aco_funcp_protector@PLT
199 call abort@PLT
200 #else
201 call aco_funcp_protector
202 call abort
203 #endif
204 #endif
205 ret
206 #else
207 #error "platform not supported"
208 #endif