-
Notifications
You must be signed in to change notification settings - Fork 2
/
shift0.S
283 lines (273 loc) · 6.68 KB
/
shift0.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Code for the shift0/reset0 operators and the runcont function.
* shift0.S
*
* Copyright (c) 2011, Marek Materzok <tilk@tilk.eu>
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The software is provided 'as is', without any warranty.
*/
#define STACKSIZE 8192
#define OFFSET_ESP 8188
#define OFFSET_EIP 8184
#define OFFSET_EBP 8180
.text
.cfi_sections .debug_frame, .eh_frame
.comm marker,4
.comm stacks,64
.p2align 4,,15
.globl reset_arg
.type reset_arg, @function
reset_arg:
.cfi_startproc
.cfi_personality 0x0,__reset_personality
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
# protect the callee-saved registers
pushl %ebx
pushl %esi
pushl %edi
.cfi_offset 3, -12
.cfi_offset 6, -16
.cfi_offset 7, -20
# allocate the new stack
pushl $STACKSIZE
call malloc
addl $4, %esp
# push the stack on the stack of stacks
movl marker, %ecx
movl $stacks, %edx
movl %eax, (%edx, %ecx, 4)
incl marker
# load the address of the function to call and of the argument
movl 20(%esp), %ecx
movl 24(%esp), %ebx
# remember the stack pointer
movl %esp, %edx
# switch the stack and push the old stack pointer
movl %eax, %esp
addl $STACKSIZE, %esp
pushl %edx
# do the call
pushl $reset_after
jmp reset_in
# shift0_arg was not called, do the cleanup
# go back to the old stack
reset_after:
popl %esp
# pop the uncaptured stack from the stack of stacks
decl marker
movl marker, %ecx
movl $stacks, %edx
movl (%edx, %ecx, 4), %edx
# free it
movl %eax, %ebx
push %edx
call free
addl $4, %esp
movl %ebx, %eax
# restore the callee-saved registers
popl %edi
popl %esi
popl %ebx
leave
ret
.cfi_endproc
.size reset_arg, .-reset_arg
.p2align 4,,15
.globl reset_in
.type reset_in, @function
reset_in:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
pushl %ebx
call *%ecx
popl %ebx
popl %ebp
ret
.cfi_endproc
.size reset_in, .-reset_in
.p2align 4,,15
.globl shift0_arg
.type shift0_arg, @function
shift0_arg:
.cfi_startproc
# pop the current stack from the stack of stacks
decl marker
movl marker, %ecx
movl $stacks, %edx
movl (%edx, %ecx, 4), %eax
# store the initial runcont
movl $runcont_clean, (%eax)
# store the callee-saved registers and the stack pointer
movl %esp, 4(%eax)
movl %ebp, 8(%eax)
movl %ebx, 12(%eax)
movl %esi, 16(%eax)
movl %edi, 20(%eax)
# calculate and store the stack size
movl %eax, %ecx
addl $STACKSIZE, %ecx
subl %esp, %ecx
movl %ecx, 24(%eax)
# remember the parameters
movl 4(%esp), %edx
movl 8(%esp), %ecx
# switch the stack
movl OFFSET_ESP(%eax), %esp
.cfi_def_cfa_offset 8
movl OFFSET_EBP(%eax), %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
.cfi_offset 3, -12
.cfi_offset 6, -16
.cfi_offset 7, -20
# call the parameter
pushl %ecx
pushl %eax
call *%edx
addl $8, %esp
# restore the callee-saved registers
popl %edi
popl %esi
popl %ebx
leave
ret
.cfi_endproc
.size shift0_arg, .-shift0_arg
.p2align 4,,15
.globl runcont_clean
.type runcont_clean, @function
runcont_clean:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
# protect the callee-saved registers
pushl %ebx
pushl %esi
pushl %edi
.cfi_offset 3, -12
.cfi_offset 6, -16
.cfi_offset 7, -20
# backup the stack
movl 20(%esp), %ebx
movl $runcont_reentry, (%ebx)
pushl 24(%ebx)
call malloc
pushl 4(%ebx)
pushl %eax
call memcpy
movl %eax, 28(%ebx)
addl $12, %esp
# do the actual calling
jmp runcont_common
.cfi_endproc
.size runcont_clean, .-runcont_clean
.p2align 4,,15
.globl runcont_common
.type runcont_common, @function
runcont_common:
.cfi_startproc
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
.cfi_def_cfa_register 5
.cfi_offset 3, -12
.cfi_offset 6, -16
.cfi_offset 7, -20
# set-up the stack for running
movl %esp, OFFSET_ESP(%ebx)
movl %ebp, OFFSET_EBP(%ebx)
movl $runcont_after, OFFSET_EIP(%ebx)
# remember the parameter
movl 24(%esp), %eax
# restore the callee-saved registers and the stack, then return
movl %ebx, %ecx
movl 4(%ecx), %esp
movl 8(%ecx), %ebp
movl 12(%ecx), %ebx
movl 16(%ecx), %esi
movl 20(%ecx), %edi
ret
.cfi_personality 0x0,__runcont_personality
runcont_after:
# go back to the old stack
popl %esp
# store the runcont for dirtied continuations
movl 20(%esp), %ebx
movl $runcont_dirty, (%ebx)
# restore the callee-saved registers and return
popl %edi
popl %esi
popl %ebx
leave
ret
.cfi_endproc
.size runcont_common, .-runcont_common
.p2align 4,,15
.globl runcont_dirty
.type runcont_dirty, @function
runcont_dirty:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
# protect the callee-saved registers
pushl %ebx
pushl %esi
pushl %edi
.cfi_offset 3, -12
.cfi_offset 6, -16
.cfi_offset 7, -20
# restore the original stack
movl 20(%esp), %ebx
movl $runcont_reentry, (%ebx)
pushl 24(%ebx)
pushl 28(%ebx)
pushl 4(%ebx)
call memcpy
addl $12, %esp
# do the actual calling
jmp runcont_common
.cfi_endproc
.size runcont_dirty, .-runcont_dirty
.p2align 4,,15
.globl runcont_reentry
.type runcont_reentry, @function
runcont_reentry:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
call abort
leave
ret
.cfi_endproc
.size runcont_reentry, .-runcont_reentry
.p2align 4,,15
.globl runcont
.type runcont, @function
runcont:
.cfi_startproc
# call the right runcont impl
movl 4(%esp), %eax
jmp *(%eax)
.cfi_endproc
.size runcont, .-runcont