/
asm_util.s
342 lines (314 loc) · 7.82 KB
/
asm_util.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
##############################################################################
#
# @file : asm_util.s
# @brief : Utility functions which are described from assembler.
# @author : nutti
# @date : 2011.9.15 (Thu) 17:45
#
##############################################################################
.code32
.global asm_halt
.global asm_write_mem
.global asm_enable_intr
.global asm_disable_intr
.global asm_get_eflags
.global asm_set_eflags
.global asm_out8
.global asm_lgdt
.global asm_seg_jmp
.global asm_memcpy
.global LoadIDT
.global IntHook21
.extern IntHandler21
.global task_switch
.global load_tr
.global switch_task
.global switch_task_2
########################################################################
#
# @name : asm_halt
# @brief : Stop CPU.
# @usage : C language can call this function as follows.
# void asm_halt();
#
########################################################################
asm_halt:
hlt
ret
########################################################################
#
# @name : asm_write_mem
# @brief : Write memory directly.
# @usage : C language can call this function as follows.
# void asm_write_mem( int addr, int val );
# addr is the memory address on which will be written.
# val is the value which will be written.
#
########################################################################
asm_write_mem:
movl 4(%esp), %ecx
movb 8(%esp), %al
movb %al, (%ecx)
ret
########################################################################
#
# @name : asm_enable_intr
# @brief : Enable interrupt.
# @usage : C language can call this function as follows.
# void asm_enable_intr();
#
########################################################################
asm_enable_intr:
sti
ret
########################################################################
#
# @name : asm_disable_intr
# @brief : Disable interrupt.
# @usage : C language can call this function as follows.
# void asm_disable_intr();
#
########################################################################
asm_disable_intr:
cli
ret
# Enable intel syntax.
.intel_syntax noprefix
########################################################################
#
# @name : asm_get_eflags
# @brief : Get eflags.
# @usage : C language can call this function as follows.
# int get_eflags();
# eflags will be returned.
#
########################################################################
asm_get_eflags:
pushfd
pop eax
ret
########################################################################
#
# @name : asm_set_eflags
# @brief : Set eflags.
# @usage : C language can call this function as follows.
# void set_eflags( int eflags );
# eflags will be set.
#
########################################################################
asm_set_eflags:
mov eax, [esp+4]
push eax
popfd
ret
########################################################################
#
# @name : asm_out8
# @brief : Output. (8bit)
# @usage : C language can call this function as follows.
# void asm_out8( int port, int data );
# port is port number.
# data is data which will be sent.
#
########################################################################
asm_out8:
mov edx, [esp+4]
mov al, [esp+8]
out dx, al
ret
########################################################################
#
# @name : asm_lgdt
# @brief : Load segment descriptor table.
# @usage : C language can call this function as follows.
# void asm_lgdt( int limit, int addr );
# limit is the size of gdt.
# addr is the address of first gdt.
#
########################################################################
asm_lgdt:
mov ax, [esp+4]
mov [esp+6], ax
lgdt [esp+6]
ret
########################################################################
#
# @name : LoadIDT
# @brief : Load interrupt descriptor table.
# @usage : C language can call this function as follows.
# void LoadIDT( int limit, int addr );
# limit is the size of idt.
# addr is the address of first idt.
#
########################################################################
LoadIDT:
mov ax, [esp+4]
mov [esp+6], ax
lidt [esp+6]
ret
########################################################################
#
# @name : asm_seg_jmp
# @brief : Jump to another segment.
# @usage : C language can call this function as follows.
# void asm_seg_jmp( int seg, int addr );
# seg is the segment to be jampped.
# addr is the address.
#
########################################################################
asm_seg_jmp:
mov eax, [esp+4]
mov edx, [esp+8]
mov esp, 0x310000
jmp 16, 0x0000001b
########################################################################
#
# @name : asm_memcpy
# @brief : Copy memory.
# @usage : C language can call this function as follows.
# void asm_memcpy( char* pSrc, char* pDest, int count );
# pSrc is the source address of copy.
# pDest is the destination address to be copied.
#
########################################################################
asm_memcpy:
push eax
push ebx
push ecx
push edx
mov eax, [esp+20] # pushした分、スタックポインタを移動させる。
mov edx, [esp+24]
mov ecx, [esp+28]
memcopy:
mov ebx, [eax]
mov [edx], ebx
add eax, 4
add edx, 4
sub ecx, 1
jnz memcopy
pop edx
pop ecx
pop ebx
pop eax
ret
########################################################################
#
# @name : IntHook21
# @brief : Hook IRQ21.
#
########################################################################
IntHook21:
push es
push ds
pushad # レジスタの全保存
mov eax, esp
push eax
mov ax, ss
mov ds, ax
mov es, ax # C言語の関数を呼ぶときは、SS=DS=ESでなくてはならない
call IntHandler21
pop eax
popad
pop ds
pop es # レジスタの復帰
iretd # 割り込みルーチンからの復帰
########################################################################
#
# @name : load_tr
# @brief : Load TR register.
# @usage : void load_tr( int tr );
#
########################################################################
load_tr:
LTR [esp+4]
RET
########################################################################
#
# @name : task_switch
# @brief : Task switch function.
# @usage : void task_switch();
#
########################################################################
task_switch:
ljmp 5*8:0x0
#hlt
#ljmp $0x20,$0x0
RET
# switch_task( src, dst )
switch_task:
pushad
mov ebx, [esp+36] # src
# Copy to previous task.
mov eax, [esp+32]
mov [ebx+40], eax # eax
mov eax, [esp+28]
mov [ebx+44], eax # ecx
mov eax, [esp+24]
mov [ebx+48], eax # edx
mov eax, [esp+20]
mov [ebx+52], eax # ebx
mov eax, [esp+16]
mov [ebx+56], eax # esp
mov eax, [esp+12]
mov [ebx+60], eax # ebp
mov eax, [esp+8]
mov [ebx+64], eax # esi
mov eax, [esp+4]
mov [ebx+68], eax # edi
mov [ebx+72], es # es
mov [ebx+76], cs # cs
mov [ebx+80], ss # ss
mov [ebx+84], ds # ds
mov [ebx+88], fs # fs
mov [ebx+92], gs # gs
popad
# Move to destination task.
mov ebx, [esp+8]
mov ecx, [ebx+44] # ecx
mov edx, [ebx+48] # edx
mov esp, [ebx+56] # esp
mov ebp, [ebx+60] # ebp
mov esi, [ebx+64] # esi
mov edi, [ebx+68] # edi
mov fs, [ebx+88] # fs
mov gs, [ebx+92] # gs
mov eax, [ebx+40] # eax
push [ebx+80] # push ss
push [ebx+56] # push esp
push [ebx+36] # push eflags
push [ebx+76] # push cs
push [ebx+32] # push eip
push [ebx+52] # push ebx
mov es, [ebx+72] # es
mov ds, [ebx+84] # ds
pop ebx
iretd
back:
ret
switch_task_2:
# Save
pushad
pushfd
mov eax, cs
push eax
mov eax, ss
push eax
mov ebx, [esp+48]
mov [ebx+56], esp # ESP
.att_syntax noprefix
movl $toswitch, %eax# 0x435
.intel_syntax noprefix
sub eax, 0x280000
# hlt
mov [ebx+32], eax # EIP
# Restore
mov ebx, [esp+52]
mov esp, [ebx+56]
jmp [ebx+32]
toswitch:
pop eax
mov ss, eax
pop eax
popfd
popad
ret