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 EBX = 0x00
44 ECX = 0x04
45 EDX = 0x08
46 ESI = 0x0C
47 EDI = 0x10
48 EBP = 0x14
49 EAX = 0x18
50 DS = 0x1C
51 ES = 0x20
52 FS = 0x24
53 GS = 0x28
54 ORIG_EAX = 0x2C
55 EIP = 0x30
56 CS = 0x34
57 EFLAGS = 0x38
58 OLDESP = 0x3C
59 OLDSS = 0x40
60
61 IF_MASK = 0x00000200
62 NT_MASK = 0x00004000
63 VM_MASK = 0x00020000
64
65
66
67
68 state = 0
69 counter = 4
70 priority = 8
71 signal = 12
72 sigaction = 16 # MUST be 16 (=len of sigaction)
73 blocked = (33*16)
74 saved_kernel_stack = ((33*16)+4)
75 kernel_stack_page = ((33*16)+8)
76 flags = ((33*16)+12)
77
78
79
80
81 sa_handler = 0
82 sa_mask = 4
83 sa_flags = 8
84 sa_restorer = 12
85
86 ENOSYS = 38
87
88 .globl _system_call,_sys_execve
89 .globl _device_not_available, _coprocessor_error
90 .globl _divide_error,_debug,_nmi,_int3,_overflow,_bounds,_invalid_op
91 .globl _double_fault,_coprocessor_segment_overrun
92 .globl _invalid_TSS,_segment_not_present,_stack_segment
93 .globl _general_protection,_reserved
94 .globl _alignment_check,_page_fault
95 .globl ret_from_sys_call
96
97 #define SAVE_ALL \
98 cld; \
99 push %gs; \
100 push %fs; \
101 push %es; \
102 push %ds; \
103 pushl %eax; \
104 pushl %ebp; \
105 pushl %edi; \
106 pushl %esi; \
107 pushl %edx; \
108 pushl %ecx; \
109 pushl %ebx; \
110 movl $0x10,%edx; \
111 mov %dx,%ds; \
112 mov %dx,%es; \
113 movl $0x17,%edx; \
114 mov %dx,%fs
115
116 .align 4
117 reschedule:
118 pushl $ret_from_sys_call
119 jmp _schedule
120 .align 4
121 _system_call:
122 pushl %eax # save orig_eax
123 SAVE_ALL
124 movl $-ENOSYS,EAX(%esp)
125 cmpl _NR_syscalls,%eax
126 jae ret_from_sys_call
127 movl _current,%ebx
128 testl $0x20,flags(%ebx) # PF_TRACESYS
129 je 1f
130 pushl $0
131 pushl %ebx
132 pushl $5 # SIGTRAP
133 call _send_sig
134 addl $12,%esp
135 call _schedule
136 movl ORIG_EAX(%esp),%eax
137 1: call _sys_call_table(,%eax,4)
138 movl %eax,EAX(%esp) # save the return value
139 movl _current,%eax
140 testl $0x20,flags(%eax) # PF_TRACESYS
141 je ret_from_sys_call
142 cmpl $0,signal(%eax)
143 jne ret_from_sys_call # ptrace would clear signal
144 pushl $0
145 pushl %eax
146 pushl $5 # SIGTRAP
147 call _send_sig
148 addl $12,%esp
149 call _schedule
150 .align 4,0x90
151 ret_from_sys_call:
152 movl EFLAGS(%esp),%eax # check VM86 flag: CS/SS are
153 testl $VM_MASK,%eax # different then
154 jne 4f
155 cmpw $0x0f,CS(%esp) # was old code segment supervisor ?
156 jne 2f
157 cmpw $0x17,OLDSS(%esp) # was stack segment = 0x17 ?
158 jne 2f
159 4: orl $IF_MASK,%eax # these just try to make sure
160 andl $~NT_MASK,%eax # the program doesn't do anything
161 movl %eax,EFLAGS(%esp) # stupid
162 1: cmpl $0,_need_resched
163 jne reschedule
164 movl _current,%eax
165 cmpl _task,%eax # task[0] cannot have signals
166 je 2f
167 cmpl $0,state(%eax) # state
168 jne reschedule
169 cmpl $0,counter(%eax) # counter
170 je reschedule
171 movl signal(%eax),%ebx
172 movl blocked(%eax),%ecx
173 notl %ecx
174 andl %ebx,%ecx
175 bsfl %ecx,%ecx
176 je 2f
177 btrl %ecx,%ebx
178 incl %ecx
179 movl %ebx,signal(%eax)
180 movl %esp,%ebx
181 testl $VM_MASK,EFLAGS(%esp)
182 je 3f
183 pushl %ebx
184 pushl %ecx
185 call _save_v86_state
186 popl %ecx
187 movl %eax,%ebx
188 movl %eax,%esp
189 3: pushl %ebx
190 pushl %ecx
191 call _do_signal
192 popl %ecx
193 popl %ebx
194 testl %eax, %eax
195 jne 1b # see if we need to switch tasks, or do more signals
196 2: popl %ebx
197 popl %ecx
198 popl %edx
199 popl %esi
200 popl %edi
201 popl %ebp
202 popl %eax
203 pop %ds
204 pop %es
205 pop %fs
206 pop %gs
207 addl $4,%esp # skip the orig_eax
208 iret
209
210 .align 4
211 _sys_execve:
212 lea (EIP+4)(%esp),%eax # don't forget about the return address.
213 pushl %eax
214 call _do_execve
215 addl $4,%esp
216 ret
217
218 .align 4
219 _divide_error:
220 pushl $0 # no error code
221 pushl $_do_divide_error
222 .align 4,0x90
223 error_code:
224 push %fs
225 push %es
226 push %ds
227 pushl %eax
228 pushl %ebp
229 pushl %edi
230 pushl %esi
231 pushl %edx
232 pushl %ecx
233 pushl %ebx
234 cld
235 movl $-1, %eax
236 xchgl %eax, ORIG_EAX(%esp) # orig_eax (get the error code. )
237 xorl %ebx,%ebx # zero ebx
238 mov %gs,%bx # get the lower order bits of gs
239 xchgl %ebx, GS(%esp) # get the address and save gs.
240 pushl %eax # push the error code
241 lea 52(%esp),%edx
242 pushl %edx
243 movl $0x10,%edx
244 mov %dx,%ds
245 mov %dx,%es
246 movl $0x17,%edx
247 mov %dx,%fs
248 call *%ebx
249 addl $8,%esp
250 jmp ret_from_sys_call
251
252 .align 4
253 _coprocessor_error:
254 pushl $0
255 pushl $_do_coprocessor_error
256 jmp error_code
257
258 .align 4
259 _device_not_available:
260 pushl $-1 # mark this as an int
261 SAVE_ALL
262 pushl $ret_from_sys_call
263 clts # clear TS so that we can use math
264 movl %cr0,%eax
265 testl $0x4,%eax # EM (math emulation bit)
266 je _math_state_restore
267 pushl $0 # temporary storage for ORIG_EIP
268 call _math_emulate
269 addl $4,%esp
270 ret
271
272 .align 4
273 _debug:
274 pushl $0
275 pushl $_do_debug
276 jmp error_code
277
278 .align 4
279 _nmi:
280 pushl $0
281 pushl $_do_nmi
282 jmp error_code
283
284 .align 4
285 _int3:
286 pushl $0
287 pushl $_do_int3
288 jmp error_code
289
290 .align 4
291 _overflow:
292 pushl $0
293 pushl $_do_overflow
294 jmp error_code
295
296 .align 4
297 _bounds:
298 pushl $0
299 pushl $_do_bounds
300 jmp error_code
301
302 .align 4
303 _invalid_op:
304 pushl $0
305 pushl $_do_invalid_op
306 jmp error_code
307
308 .align 4
309 _coprocessor_segment_overrun:
310 pushl $0
311 pushl $_do_coprocessor_segment_overrun
312 jmp error_code
313
314 .align 4
315 _reserved:
316 pushl $0
317 pushl $_do_reserved
318 jmp error_code
319
320 .align 4
321 _double_fault:
322 pushl $_do_double_fault
323 jmp error_code
324
325 .align 4
326 _invalid_TSS:
327 pushl $_do_invalid_TSS
328 jmp error_code
329
330 .align 4
331 _segment_not_present:
332 pushl $_do_segment_not_present
333 jmp error_code
334
335 .align 4
336 _stack_segment:
337 pushl $_do_stack_segment
338 jmp error_code
339
340 .align 4
341 _general_protection:
342 pushl $_do_general_protection
343 jmp error_code
344
345 .align 4
346 _alignment_check:
347 pushl $_do_alignment_check
348 jmp error_code
349
350 .align 4
351 _page_fault:
352 pushl $_do_page_fault
353 jmp error_code