-
Notifications
You must be signed in to change notification settings - Fork 7
/
func.s
254 lines (228 loc) · 4.11 KB
/
func.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
.code32
.globl io_hlt, io_cli, io_sti, io_stihlt
.globl io_out8, io_in8
.globl io_store_eflags, io_load_eflags
.globl load_gdtr, load_idtr
.globl load_cr0, store_cr0
.globl load_tr
.globl asm_inthandler20, asm_inthandler21, asm_inthandler2c, asm_inthandler27
.globl asm_inthandler0c, asm_inthandler0d
.globl asm_end_app
.globl farjmp, farcall
.globl asm_hrb_api
.globl start_app
.extern inthandler20, inthandler21, inthandler2c, inthandler27
.extern inthandler0c, inthandler0d
.extern hrb_api
.text
io_hlt:
hlt
ret
io_cli:
cli
ret
io_sti:
sti
ret
io_stihlt:
sti
hlt
ret
# void io_out8(int port, int data)
io_out8:
movl 4(%esp), %edx
movb 8(%esp), %al
outb %al, %dx
ret
# int io_in8(int port)
io_in8:
movl 4(%esp), %edx
movl $0, %eax
inb %dx, %al
ret
# void io_store_eflags(int eflags)
io_store_eflags:
movl 4(%esp), %eax
pushl %eax
popf
ret
# int io_load_eflags(void)
io_load_eflags:
pushf
popl %eax
ret
# void load_gdtr(int limit, int addr)
load_gdtr:
movw 4(%esp), %ax
movw %ax, 6(%esp)
lgdt 6(%esp)
ret
# void load_idtr(int limit, int addr)
load_idtr:
movw 4(%esp), %ax
movw %ax, 6(%esp)
lidt 6(%esp)
ret
# int load_cr0(void)
load_cr0:
movl %cr0, %eax
ret
# void store_cr0(int cr0);
store_cr0:
movl 4(%esp), %eax
movl %eax, %cr0
ret
# void load_tr(int tr);
load_tr:
ltr 4(%esp)
ret
asm_inthandler20:
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call inthandler20
pop %eax
popa
pop %ds
pop %es
iret
asm_inthandler21:
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call inthandler21
pop %eax
popa
pop %ds
pop %es
iret
asm_inthandler27:
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call inthandler27
pop %eax
popa
pop %dx
pop %es
iret
asm_inthandler2c:
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call inthandler2c
pop %eax
popa
pop %ds
pop %es
iret
asm_inthandler0c:
sti
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call inthandler0c
cmp $0, %eax
jne asm_end_app
pop %eax
popa
pop %ds
pop %es
add $4, %esp
iret
asm_inthandler0d:
sti
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call inthandler0d
cmp $0, %eax
jne asm_end_app
pop %eax
popa
pop %ds
pop %es
add $4, %esp
iret
# void farjmp(int eip, int cs);
farjmp:
ljmpl *4(%esp)
ret
# void farcall(int eip, int cs);
farcall:
lcall *4(%esp)
ret
asm_hrb_api:
sti
push %ds
push %es
pusha
pusha
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call hrb_api
cmp $0, %eax
jne asm_end_app
add $32, %esp
popa
pop %es
pop %ds
iret
asm_end_app:
mov (%eax), %esp
movw $0, 4(%eax)
popa
ret # return to cmp_app
# void start_app(int eip, int cs, int esp, int ds, int *tss_esp0);
start_app:
pusha # 32ビットレジスタを全部保存しておく
mov 36(%esp), %eax # アプリ用のEIP
mov 40(%esp), %ecx # アプリ用のCS
mov 44(%esp), %edx # アプリ用のESP
mov 48(%esp), %ebx # アプリ用のDS/SS
mov 52(%esp), %ebp # tss.esp0の番地
mov %esp, (%ebp) # OS用のESPを保存
mov %ss, 4(%ebp) # OS用のSSを保存
mov %bx, %es
mov %bx, %ds
mov %bx, %fs
mov %bx, %gs
# 以下はlretでアプリに行かせるためのスタック調整
or $3, %ecx # アプリ用のセグメント番号に3をorする
or $3, %ebx # アプリ用のセグメント番号に3をorする
push %ebx # アプリのss
push %edx # アプリのesp
push %ecx # アプリのcs
push %eax # アプリのeip
lret