-
Notifications
You must be signed in to change notification settings - Fork 11
/
lowlevel-32bit.S
226 lines (180 loc) · 6.03 KB
/
lowlevel-32bit.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
/*
* PTLsim: Cycle Accurate x86-64 Simulator
* 32-bit low level functions
*
* Copyright 2003-2008 Matt T. Yourst <[email protected]>
*/
.text
.intel_syntax
.global inside_sim_escape_code_template_32bit
.global inside_sim_escape_code_template_32bit_end
inside_sim_escape_code_template_32bit:
# Pass args in registers: rdi rsi rdx rcx r8 r9
push %edi
push %esi
push %edx
push %ecx
mov %edi,[%esp+(4+1)*4 + 0*8]
mov %esi,[%esp+(4+1)*4 + 1*8]
mov %edx,[%esp+(4+1)*4 + 2*8]
mov %ecx,[%esp+(4+1)*4 + 3*8]
# Undocumented x86 escape opcode to do PTL calls
.byte 0x0f
.byte 0x37
pop %ecx
pop %edx
pop %esi
pop %edi
ret
inside_sim_escape_code_template_32bit_end:
#ifndef __x86_64__
/*
* PTLsim32 32-bit entry code
*/
#define __ASM_ONLY__
#include <ptlhwdef.h>
.extern ptlsim_init
.extern stack_min_addr
.extern inside_ptlsim
.global ptlsim_preinit_entry
ptlsim_preinit_entry:
fxsave x87state
mov word ptr [saved_cs],%cs
mov word ptr [saved_ss],%ss
mov word ptr [saved_ds],%ds
mov word ptr [saved_es],%es
mov word ptr [saved_fs],%fs
mov word ptr [saved_gs],%gs
mov byte ptr [running_in_sim_mode],1
mov %eax,%esp # origrsp
push offset main # next_init_func
push %eax
call ptlsim_preinit
add %esp,8
test byte ptr [inside_ptlsim],1
jnz 1f
# All args (argc, argv) already on stack on x86-32:
mov %eax,[%esp] # argc
lea %ebx,[%esp+4] # argv
push %ebx
push %eax
call main #
int3 # (main never returns)
1:
mov %ebp,%eax # save new PTL private stack pointer
/*
* Give user thread a really big stack by accessing memory
* below the grows-down stack object. We have to do this
* now since PTLsim has no concept of grow down auto allocate
* stacks and will just throw erroneous page faults unless
* the stack pages are already visible to mqueryall().
*/
xor %eax,%eax
mov %esi,[stack_min_addr]
1:
sub %esp,4096
mov [%esp],%eax
cmp %esp,%esi
jae 1b
mov %esp,%ebp # update to new stack pointer
mov %eax,[%esp] # argc
lea %ebx,[%esp+4] # argv
push %ebx
push %eax
call main
/*
* struct ThreadState {
* ThreadState* self;
* void* stack;
* ...
* };
*/
#define ThreadState_self 4*0
#define ThreadState_rsp 4*1
#define ThreadState_simcall 4*2
.extern ctx
.extern save_context_switch_to_sim
.extern x87state
.extern basetls
.extern running_in_sim_mode
.data
.global saved_fsreg
saved_fsreg:
.word 0
.global saved_gsreg
saved_gsreg:
.word 0
.previous
.global save_context_switch_to_sim_lowlevel
save_context_switch_to_sim_lowlevel:
mov byte ptr [running_in_sim_mode],1 # store must be atomic
mov [ctx + 8*REG_rax + 8*0],%eax
mov [ctx + 8*REG_rax + 8*1],%ecx
mov [ctx + 8*REG_rax + 8*2],%edx
mov [ctx + 8*REG_rax + 8*3],%ebx
mov [ctx + 8*REG_rax + 8*4],%esp
mov [ctx + 8*REG_rax + 8*5],%ebp
mov [ctx + 8*REG_rax + 8*6],%esi
mov [ctx + 8*REG_rax + 8*7],%edi
mov dword ptr [ctx + 8*REG_rax + 8*0 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*1 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*2 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*3 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*4 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*5 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*6 + 4],0
mov dword ptr [ctx + 8*REG_rax + 8*7 + 4],0
mov %eax,[%esp] # Get return %rip (if we got here through a CALL insn)
mov [ctx + 8*REG_rip],%eax # Save %rip
mov dword ptr [ctx + 8*REG_rip + 4],0
mov %esp,[basetls + ThreadState_rsp] # Switch to private thread stack
pushfd # Save rflags
pop dword ptr [ctx + 8*REG_flags + 8] # Put flags into structure
# (skip tr0/tr1/tr2)
mov dword ptr [ctx + 8*REG_zero],0 # Save %zero
and %esp,-16
fxsave x87state
mov word ptr [saved_cs],%cs
mov word ptr [saved_ss],%ss
mov word ptr [saved_ds],%ds
mov word ptr [saved_es],%es
mov word ptr [saved_fs],%fs
mov word ptr [saved_gs],%gs
sub %esp,4 # Realign stack
jmp save_context_switch_to_sim
.data
switch_to_native_restore_context_temp_32_to_32:
.long 0
.previous
# extern "C" void switch_to_native_restore_context_lowlevel(const UserContext& ctx, int switch_64_to_32);
# %rdi = ctx
# %rsi = switch_64_to_32
.global switch_to_native_restore_context_lowlevel
switch_to_native_restore_context_lowlevel:
# Calling convention:
# %rdi = pointer to state to restore
mov %edi,[%esp + 4 + 0*4] # get arg 0 (pointer to state to restore)
mov %eax,[%edi + 8*REG_rip] # Load %rip
mov [switch_to_native_restore_context_temp_32_to_32],%eax # Save %rip for final jump
lea %esp,[%edi + 8*REG_flags] # Load address of flags
popfd # Restore flags
mov %esp,[%edi + 8*REG_rsp] # Restore user %esp; now on user stack
mov %eax,[%edi + 8*REG_rax + 8*0]
mov %ecx,[%edi + 8*REG_rax + 8*1]
mov %edx,[%edi + 8*REG_rax + 8*2]
mov %ebx,[%edi + 8*REG_rax + 8*3]
# mov %esp,[%edi + 8*REG_rax + 8*4] # (already done)
mov %ebp,[%edi + 8*REG_rax + 8*5]
mov %esi,[%edi + 8*REG_rax + 8*6]
# mov %edi,[%edi + 8*REG_rax + 8*7] # (done at very end)
# mov %rip,[%edi + 8*REG_rip] # (do later)
# mov %rflags,[%edi + 8*REG_flags] # (already done)
# (No need to restore %tr0, %tr1, %tr2, %zero
fxrstor x87state
mov %fs,word ptr [saved_fs]
mov %gs,word ptr [saved_gs]
mov %edi,[%edi + 8*REG_rdi] # Restore %rdi
mov byte ptr [running_in_sim_mode],0 # store must be atomic
jmp dword ptr [switch_to_native_restore_context_temp_32_to_32]
#endif
.end