-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub_handler.S
285 lines (247 loc) · 5.45 KB
/
stub_handler.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
.text
.global irq0
.global irq1
.global irq8
.global system_call
#--------------------------------------------------------------------------
#=====stack=====#
# ebx #
# ecx #
# edx #
# esi #
# edi #
# ebp #
# esp #
# eax #
# ds #
# es #
# fs #
# gs #
# int number #
# err code #
# ret addr (p)#
# cs (p)#
# eflags (p)#
# p_esp (p)#
# ss (p)#
#===============#
# -----------------------------------------------------irq-------------------------------------------------------------
# void irq0:(void);
# Description: do irq0 function call
# Interface : Register-based arguments (not C-style)
# Inputs : push 0, push 0x20
# Outputs : do irq0 function call
# Registers:
.align 4
irq0:
pushl $0 #dummy error code
pushl $0x20 #irq_num
jmp common_handler
# void irq1:(void);
# Description: do irq1 function call
# Interface : Register-based arguments (not C-style)
# Inputs : push 0, push 0x21
# Outputs : do irq1 function call
# Registers:
.align 4
irq1:
pushl $0 #dummy error code
pushl $0x21 #irq_num
jmp common_handler
# void irq8:(void);
# Description: do irq8 function call
# Interface : Register-based arguments (not C-style)
# Inputs : push 0, push 0x28
# Outputs : do irq8 function call
# Registers:
.align 4
irq8:
pushl $0 #dummy error code
pushl $0x28 #irq_num
jmp common_handler
# -----Context Save (stack bottom is the processor saved context)
.align 4
common_handler:
pushl %gs
pushl %fs
pushl %es
pushl %ds
pushl %eax
pushl %esp
pushl %ebp
pushl %edi
pushl %esi
pushl %edx
pushl %ecx
pushl %ebx
# -----Spare the context info
pushfl
pushl %edx #spare the regs to be used by the system call
pushl %ecx
pushl %ebx
pushl %eax
leal 20(%esp),%eax
pushl %eax #push argument, which is a ptr to the context regs
call save_context_for_terminal_switch #save context
addl $4,%esp #pop argument
popl %eax
popl %ebx
popl %ecx
popl %edx
popfl
# -----Data segment switch to kernel mode
pushl %eax #cs set by idt entry,ss and esp set by tss
movw $0x18,%ax
movw %ax,%ds
movw %ax,%es
movw %ax,%gs
movw %ax,%fs
popl %eax
# -----Do irq call functions
movl 48(%esp),%eax
call *irq_except_table(,%eax,4) #look up jump table to find desired function
# -----Context Restore
popl %ebx #pop regs
popl %ecx
popl %edx
popl %esi
popl %edi
popl %ebp
addl $4,%esp
addl $4,%esp
popl %ds
popl %es
popl %fs
popl %gs
addl $8,%esp #pop arguments
iret
.align 4
irq_except_table:
#---exceptions---
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.rept 16
.long 0
.endr
#---irqs---
.long do_irq0
.long do_irq1
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long do_irq8
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
# -----------------------------------------------------system call-------------------------------------------------------------
# void system_call:(void);
# Description: perform system call otherwise returns
# Interface : Register-based arguments (not C-style)
# Inputs : 0x80, init sys call.
# Outputs : jump to the desired function call according
# to jump table, perform kernel/user stack switch
# Registers: gs,fs,es,ds,eax,esp,ebp,edi,esi,edx,ecx,ebx
.align 4
system_call:
# -----Context Save (stack bottom is the processor saved context)
cli
cmpl $10,%eax #system service number validity check
jae badsys
pushl $0 #dummy error code
pushl $0x80
pushl %gs
pushl %fs
pushl %es
pushl %ds
pushl %eax
pushl %esp
pushl %ebp
pushl %edi
pushl %esi
pushl %edx
pushl %ecx
pushl %ebx
# -----Spare the context info
pushfl
pushl %edx #spare the regs to be used by the system call
pushl %ecx
pushl %ebx
pushl %eax
leal 20(%esp),%eax
pushl %eax #push argument, which is a ptr to the context regs
call save_context #save context
addl $4,%esp #pop argument
popl %eax
popl %ebx
popl %ecx
popl %edx
popfl
# -----Data segment switch to kernel mode
pushl %eax #cs set by idt entry,ss and esp set by tss
movw $0x18,%ax #store 0x18 onto the user stack
movw %ax,%ds
movw %ax,%es
movw %ax,%gs
movw %ax,%fs
popl %eax
# -----Do system call functions
pushl %edx #push arguments
pushl %ecx
pushl %ebx
call *sys_call_table(,%eax,4) #look up jump table to find desired function
popl %ebx
popl %ecx
popl %edx
# -----Context Restore
popl %ebx #pop regs
popl %ecx
popl %edx
popl %esi
popl %edi
popl %ebp
addl $4,%esp
addl $4,%esp #update esp
popl %ds
popl %es
popl %fs
popl %gs
addl $8,%esp #pop arguments
iret
.align 4
badsys: #bad system call
movl $-1,%eax
iret
.align 4
sys_call_table: #jump table
.long do_test
.long do_halt
.long do_execute
.long do_read
.long do_write
.long do_open
.long do_close
.long do_getargs
.long do_vidmap
.long do_set_handler
.long do_sigreturn