Fixed some bugs.
[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: trap.s,v 1.13 2002/07/17 21:04:55 dwelch Exp $
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 /* Get a pointer to the current thread */
45 movl %fs:0x124, %esi
46
47 /* Restore the old trap frame pointer */
48 movl 0x3c(%esp), %ebx
49 movl %ebx, KTHREAD_TRAP_FRAME(%esi)
50
51 /* Skip debug information and unsaved registers */
52 addl $0x30, %esp
53 popl %gs
54 popl %es
55 popl %ds
56 popl %edx
57 popl %ecx
58 popl %eax
59
60 /* Restore the old previous mode */
61 popl %ebx
62 movb %bl, %ss:KTHREAD_PREVIOUS_MODE(%esi)
63
64 /* Restore the old exception handler list */
65 popl %ebx
66 movl %ebx, %fs:KPCR_EXCEPTION_LIST
67
68 popl %fs
69 popl %edi
70 popl %esi
71 popl %ebx
72 popl %ebp
73 addl $0x4, %esp /* Ignore error code */
74
75 iret
76
77 .globl _KiTrapProlog
78 _KiTrapProlog:
79 pushl %edi
80 pushl %fs
81
82 /*
83 * Check that the PCR exists, very early in the boot process it may
84 * not
85 */
86 cmpl $0, %ss:_KiPcrInitDone
87 je .L5
88
89 /* Load the PCR selector into fs */
90 movl $PCR_SELECTOR, %ebx
91 movl %ebx, %fs
92
93 /* Save the old exception list */
94 movl %fs:KPCR_EXCEPTION_LIST, %ebx
95 pushl %ebx
96
97 /* Put the exception handler chain terminator */
98 movl $0xffffffff, %fs:KPCR_EXCEPTION_LIST
99
100 /* Get a pointer to the current thread */
101 movl %fs:KPCR_CURRENT_THREAD, %edi
102
103 /* The current thread may be NULL early in the boot process */
104 cmpl $0, %edi
105 je .L4
106
107 /* Save the old previous mode */
108 movl $0, %ebx
109 movb %ss:KTHREAD_PREVIOUS_MODE(%edi), %bl
110 pushl %ebx
111
112 /* Set the new previous mode based on the saved CS selector */
113 movl 0x24(%esp), %ebx
114 cmpl $KERNEL_CS, %ebx
115 jne .L1
116 movb $KernelMode, %ss:KTHREAD_PREVIOUS_MODE(%edi)
117 jmp .L3
118 .L1:
119 movb $UserMode, %ss:KTHREAD_PREVIOUS_MODE(%edi)
120 .L3:
121
122 /* Save other registers */
123 pushl %eax
124 pushl %ecx
125 pushl %edx
126 pushl %ds
127 pushl %es
128 pushl %gs
129 pushl $0 /* DR7 */
130 pushl $0 /* DR6 */
131 pushl $0 /* DR3 */
132 pushl $0 /* DR2 */
133 pushl $0 /* DR1 */
134 pushl $0 /* DR0 */
135 pushl $0 /* XXX: TempESP */
136 pushl $0 /* XXX: TempCS */
137 pushl $0 /* XXX: DebugPointer */
138 pushl $0 /* XXX: DebugArgMark */
139 movl 0x60(%esp), %ebx
140 pushl %ebx /* XXX: DebugEIP */
141 pushl %ebp /* XXX: DebugEBP */
142
143 /* Load the segment registers */
144 movl $KERNEL_DS, %ebx
145 movl %ebx, %ds
146 movl %ebx, %es
147 movl %ebx, %gs
148
149 /* Set ES to kernel segment */
150 movw $KERNEL_DS,%bx
151 movw %bx,%es
152
153 movl %esp, %ebx
154 movl %esp, %ebp
155
156 /* Save a pointer to the trap frame in the current KTHREAD */
157 cmpl $0, %edi
158 je .L6
159 movl %ebx, %ss:KTHREAD_TRAP_FRAME(%edi)
160 .L6:
161
162 /* Call the C exception handler */
163 pushl %esi
164 pushl %ebx
165 call _KiTrapHandler
166 addl $4, %esp
167 addl $4, %esp
168
169 /* Return to the caller */
170 jmp _KiTrapEpilog
171
172 /* Handle the no-pcr case out of line */
173 .L5:
174 pushl $0
175
176 /* Handle the no-thread case out of line */
177 .L4:
178 pushl $0
179 jmp .L3
180
181 .globl _KiTrap0
182 _KiTrap0:
183 /* No error code */
184 pushl $0
185 pushl %ebp
186 pushl %ebx
187 pushl %esi
188 movl $0, %esi
189 jmp _KiTrapProlog
190
191 .globl _KiTrap1
192 _KiTrap1:
193 /* No error code */
194 pushl $0
195 pushl %ebp
196 pushl %ebx
197 pushl %esi
198 movl $1, %esi
199 jmp _KiTrapProlog
200
201 .globl _KiTrap2
202 _KiTrap2:
203 pushl $0
204 pushl %ebp
205 pushl %ebx
206 pushl %esi
207 movl $2, %esi
208 jmp _KiTrapProlog
209
210 .globl _KiTrap3
211 _KiTrap3:
212 pushl $0
213 pushl %ebp
214 pushl %ebx
215 pushl %esi
216 movl $3, %esi
217 jmp _KiTrapProlog
218
219 .globl _KiTrap4
220 _KiTrap4:
221 pushl $0
222 pushl %ebp
223 pushl %ebx
224 pushl %esi
225 movl $4, %esi
226 jmp _KiTrapProlog
227
228 .globl _KiTrap5
229 _KiTrap5:
230 pushl $0
231 pushl %ebp
232 pushl %ebx
233 pushl %esi
234 movl $5, %esi
235 jmp _KiTrapProlog
236
237 .globl _KiTrap6
238 _KiTrap6:
239 pushl $0
240 pushl %ebp
241 pushl %ebx
242 pushl %esi
243 movl $6, %esi
244 jmp _KiTrapProlog
245
246 .globl _KiTrap7
247 _KiTrap7:
248 pushl $0
249 pushl %ebp
250 pushl %ebx
251 pushl %esi
252 movl $7, %esi
253 jmp _KiTrapProlog
254
255 .globl _KiTrap8
256 _KiTrap8:
257 call _KiDoubleFaultHandler
258 iret
259
260 .globl _KiTrap9
261 _KiTrap9:
262 pushl $0
263 pushl %ebp
264 pushl %ebx
265 pushl %esi
266 movl $9, %esi
267 jmp _KiTrapProlog
268
269 .globl _KiTrap10
270 _KiTrap10:
271 pushl %ebp
272 pushl %ebx
273 pushl %esi
274 movl $10, %esi
275 jmp _KiTrapProlog
276
277 .globl _KiTrap11
278 _KiTrap11:
279 pushl %ebp
280 pushl %ebx
281 pushl %esi
282 movl $11, %esi
283 jmp _KiTrapProlog
284
285 .globl _KiTrap12
286 _KiTrap12:
287 pushl %ebp
288 pushl %ebx
289 pushl %esi
290 movl $12, %esi
291 jmp _KiTrapProlog
292
293 .globl _KiTrap13
294 _KiTrap13:
295 pushl %ebp
296 pushl %ebx
297 pushl %esi
298 movl $13, %esi
299 jmp _KiTrapProlog
300
301 .globl _KiTrap14
302 _KiTrap14:
303 pushl %ebp
304 pushl %ebx
305 pushl %esi
306 movl $14, %esi
307 jmp _KiTrapProlog
308
309 .globl _KiTrap15
310 _KiTrap15:
311 pushl %ebp
312 pushl %ebx
313 pushl %esi
314 movl $15, %esi
315 jmp _KiTrapProlog
316
317 .globl _KiTrap16
318 _KiTrap16:
319 pushl %ebp
320 pushl %ebx
321 pushl %esi
322 movl $16, %esi
323 jmp _KiTrapProlog
324
325 .globl _KiTrapUnknown
326 _KiTrapUnknown:
327 pushl $0
328 pushl %ebp
329 pushl %ebx
330 pushl %esi
331 movl $255, %esi
332 jmp _KiTrapProlog
333
334
335 /* EOF */