migrate substitution keywords to SVN
[reactos.git] / reactos / ntoskrnl / ke / i386 / trap.s
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/i386/trap.s
23 * PURPOSE: Exception handlers
24 * PROGRAMMER: David Welch <welch@cwcom.net>
25 */
26
27 /* INCLUDES ******************************************************************/
28
29 #include <ddk/status.h>
30 #include <internal/i386/segment.h>
31 #include <internal/ps.h>
32 #include <ddk/defines.h>
33
34 /* FUNCTIONS *****************************************************************/
35
36 /*
37 * Epilog for exception handlers
38 */
39 _KiTrapEpilog:
40 cmpl $1, %eax /* Check for v86 recovery */
41 jne _KiTrapRet
42 jmp _KiV86Complete
43 _KiTrapRet:
44 /* Skip debug information and unsaved registers */
45 addl $0x18, %esp
46 popl %eax /* Dr0 */
47 movl %eax, %dr0
48 popl %eax /* Dr1 */
49 movl %eax, %dr1
50 popl %eax /* Dr2 */
51 movl %eax, %dr2
52 popl %eax /* Dr3 */
53 movl %eax, %dr3
54 popl %eax /* Dr6 */
55 movl %eax, %dr6
56 popl %eax /* Dr7 */
57 movl %eax, %dr7
58 popl %gs
59 popl %es
60 popl %ds
61 popl %edx
62 popl %ecx
63 popl %eax
64
65 /* Restore the old previous mode */
66 popl %ebx
67 movb %bl, %ss:KTHREAD_PREVIOUS_MODE(%esi)
68
69 /* Restore the old exception handler list */
70 popl %ebx
71 movl %ebx, %fs:KPCR_EXCEPTION_LIST
72
73 popl %fs
74 popl %edi
75 popl %esi
76 popl %ebx
77 popl %ebp
78 addl $0x4, %esp /* Ignore error code */
79
80 iret
81
82 .globl _KiTrapProlog
83 _KiTrapProlog:
84 pushl %edi
85 pushl %fs
86
87 /*
88 * Check that the PCR exists, very early in the boot process it may
89 * not
90 */
91 cmpl $0, %ss:_KiPcrInitDone
92 je .L5
93
94 /* Load the PCR selector into fs */
95 movl $PCR_SELECTOR, %ebx
96 movl %ebx, %fs
97
98 /* Save the old exception list */
99 movl %fs:KPCR_EXCEPTION_LIST, %ebx
100 pushl %ebx
101
102 /* Get a pointer to the current thread */
103 movl %fs:KPCR_CURRENT_THREAD, %edi
104
105 /* The current thread may be NULL early in the boot process */
106 cmpl $0, %edi
107 je .L4
108
109 /* Save the old previous mode */
110 movl $0, %ebx
111 movb %ss:KTHREAD_PREVIOUS_MODE(%edi), %bl
112 pushl %ebx
113
114 /* Set the new previous mode based on the saved CS selector */
115 movl 0x24(%esp), %ebx
116 andl $0x0000FFFF, %ebx
117 cmpl $KERNEL_CS, %ebx
118 jne .L1
119 movb $KernelMode, %ss:KTHREAD_PREVIOUS_MODE(%edi)
120 jmp .L3
121 .L1:
122 movb $UserMode, %ss:KTHREAD_PREVIOUS_MODE(%edi)
123 .L3:
124
125 /* Save other registers */
126 pushl %eax
127 pushl %ecx
128 pushl %edx
129 pushl %ds
130 pushl %es
131 pushl %gs
132 movl %dr7, %eax
133 pushl %eax /* Dr7 */
134 /* Clear all breakpoint enables in dr7. */
135 andl $0xFFFF0000, %eax
136 movl %eax, %dr7
137 movl %dr6, %eax
138 pushl %eax /* Dr6 */
139 movl %dr3, %eax
140 pushl %eax /* Dr3 */
141 movl %dr2, %eax
142 pushl %eax /* Dr2 */
143 movl %dr1, %eax
144 pushl %eax /* Dr1 */
145 movl %dr0, %eax
146 pushl %eax /* Dr0 */
147 pushl $0 /* XXX: TempESP */
148 pushl $0 /* XXX: TempCS */
149 pushl $0 /* XXX: DebugPointer */
150 pushl $0 /* XXX: DebugArgMark */
151 movl 0x60(%esp), %ebx
152 pushl %ebx /* XXX: DebugEIP */
153 pushl %ebp /* XXX: DebugEBP */
154
155 /* Load the segment registers */
156 movl $KERNEL_DS, %ebx
157 movl %ebx, %ds
158 movl %ebx, %es
159 movl %ebx, %gs
160
161 /* Set ES to kernel segment */
162 movw $KERNEL_DS,%bx
163 movw %bx,%es
164
165 movl %esp, %ebx
166 movl %esp, %ebp
167
168 /* Save the old trap frame. */
169 cmpl $0, %edi
170 je .L7
171 movl %ss:KTHREAD_TRAP_FRAME(%edi), %edx
172 pushl %edx
173 jmp .L8
174 .L7:
175 pushl $0
176 .L8:
177
178 /* Save a pointer to the trap frame in the current KTHREAD */
179 cmpl $0, %edi
180 je .L6
181 movl %ebx, %ss:KTHREAD_TRAP_FRAME(%edi)
182 .L6:
183
184 /* Call the C exception handler */
185 pushl %esi
186 pushl %ebx
187 call _KiTrapHandler
188 addl $4, %esp
189 addl $4, %esp
190
191 /* Get a pointer to the current thread */
192 movl %fs:KPCR_CURRENT_THREAD, %esi
193
194 /* Restore the old trap frame pointer */
195 popl %ebx
196 movl %ebx, KTHREAD_TRAP_FRAME(%esi)
197
198 /* Return to the caller */
199 jmp _KiTrapEpilog
200
201 /* Handle the no-pcr case out of line */
202 .L5:
203 pushl $0
204
205 /* Handle the no-thread case out of line */
206 .L4:
207 pushl $0
208 jmp .L3
209
210 .globl _KiTrap0
211 _KiTrap0:
212 /* No error code */
213 pushl $0
214 pushl %ebp
215 pushl %ebx
216 pushl %esi
217 movl $0, %esi
218 jmp _KiTrapProlog
219
220 .globl _KiTrap1
221 _KiTrap1:
222 /* No error code */
223 pushl $0
224 pushl %ebp
225 pushl %ebx
226 pushl %esi
227 movl $1, %esi
228 jmp _KiTrapProlog
229
230 .globl _KiTrap2
231 _KiTrap2:
232 pushl $0
233 pushl %ebp
234 pushl %ebx
235 pushl %esi
236 movl $2, %esi
237 jmp _KiTrapProlog
238
239 .globl _KiTrap3
240 _KiTrap3:
241 pushl $0
242 pushl %ebp
243 pushl %ebx
244 pushl %esi
245 movl $3, %esi
246 jmp _KiTrapProlog
247
248 .globl _KiTrap4
249 _KiTrap4:
250 pushl $0
251 pushl %ebp
252 pushl %ebx
253 pushl %esi
254 movl $4, %esi
255 jmp _KiTrapProlog
256
257 .globl _KiTrap5
258 _KiTrap5:
259 pushl $0
260 pushl %ebp
261 pushl %ebx
262 pushl %esi
263 movl $5, %esi
264 jmp _KiTrapProlog
265
266 .globl _KiTrap6
267 _KiTrap6:
268 pushl $0
269 pushl %ebp
270 pushl %ebx
271 pushl %esi
272 movl $6, %esi
273 jmp _KiTrapProlog
274
275 .globl _KiTrap7
276 _KiTrap7:
277 pushl $0
278 pushl %ebp
279 pushl %ebx
280 pushl %esi
281 movl $7, %esi
282 jmp _KiTrapProlog
283
284 .globl _KiTrap8
285 _KiTrap8:
286 call _KiDoubleFaultHandler
287 iret
288
289 .globl _KiTrap9
290 _KiTrap9:
291 pushl $0
292 pushl %ebp
293 pushl %ebx
294 pushl %esi
295 movl $9, %esi
296 jmp _KiTrapProlog
297
298 .globl _KiTrap10
299 _KiTrap10:
300 pushl %ebp
301 pushl %ebx
302 pushl %esi
303 movl $10, %esi
304 jmp _KiTrapProlog
305
306 .globl _KiTrap11
307 _KiTrap11:
308 pushl %ebp
309 pushl %ebx
310 pushl %esi
311 movl $11, %esi
312 jmp _KiTrapProlog
313
314 .globl _KiTrap12
315 _KiTrap12:
316 pushl %ebp
317 pushl %ebx
318 pushl %esi
319 movl $12, %esi
320 jmp _KiTrapProlog
321
322 .globl _KiTrap13
323 _KiTrap13:
324 pushl %ebp
325 pushl %ebx
326 pushl %esi
327 movl $13, %esi
328 jmp _KiTrapProlog
329
330 .globl _KiTrap14
331 _KiTrap14:
332 pushl %ebp
333 pushl %ebx
334 pushl %esi
335 movl $14, %esi
336 jmp _KiTrapProlog
337
338 .globl _KiTrap15
339 _KiTrap15:
340 pushl $0
341 pushl %ebp
342 pushl %ebx
343 pushl %esi
344 movl $15, %esi
345 jmp _KiTrapProlog
346
347 .globl _KiTrap16
348 _KiTrap16:
349 pushl $0
350 pushl %ebp
351 pushl %ebx
352 pushl %esi
353 movl $16, %esi
354 jmp _KiTrapProlog
355
356 .globl _KiTrap17
357 _KiTrap17:
358 pushl $0
359 pushl %ebp
360 pushl %ebx
361 pushl %esi
362 movl $17, %esi
363 jmp _KiTrapProlog
364
365 .globl _KiTrap18
366 _KiTrap18:
367 pushl $0
368 pushl %ebp
369 pushl %ebx
370 pushl %esi
371 movl $18, %esi
372 jmp _KiTrapProlog
373
374 .globl _KiTrap19
375 _KiTrap19:
376 pushl $0
377 pushl %ebp
378 pushl %ebx
379 pushl %esi
380 movl $19, %esi
381 jmp _KiTrapProlog
382
383 .globl _KiTrapUnknown
384 _KiTrapUnknown:
385 pushl $0
386 pushl %ebp
387 pushl %ebx
388 pushl %esi
389 movl $255, %esi
390 jmp _KiTrapProlog
391
392
393 /* EOF */