249463ca10521208f38e81ee682a37f0a82d22f0
[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.12 2002/01/27 01:11:23 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 pushl $0 /* XXX: DebugEIP */
140 pushl $0 /* XXX: DebugEBP */
141
142 /* Load the segment registers */
143 movl $KERNEL_DS, %ebx
144 movl %ebx, %ds
145 movl %ebx, %es
146 movl %ebx, %gs
147
148 /* Set ES to kernel segment */
149 movw $KERNEL_DS,%bx
150 movw %bx,%es
151
152 movl %esp, %ebx
153
154 /* Save a pointer to the trap frame in the current KTHREAD */
155 movl %ebx, %ss:KTHREAD_TRAP_FRAME(%edi)
156
157 /* Call the C exception handler */
158 pushl %esi
159 pushl %ebx
160 call _KiTrapHandler
161 addl $4, %esp
162 addl $4, %esp
163
164 /* Return to the caller */
165 jmp _KiTrapEpilog
166
167 /* Handle the no-pcr case out of line */
168 .L5:
169 pushl $0
170
171 /* Handle the no-thread case out of line */
172 .L4:
173 pushl $0
174 jmp .L3
175
176 .globl _KiTrap0
177 _KiTrap0:
178 /* No error code */
179 pushl $0
180 pushl %ebp
181 pushl %ebx
182 pushl %esi
183 movl $0, %esi
184 jmp _KiTrapProlog
185
186 .globl _KiTrap1
187 _KiTrap1:
188 /* No error code */
189 pushl $0
190 pushl %ebp
191 pushl %ebx
192 pushl %esi
193 movl $1, %esi
194 jmp _KiTrapProlog
195
196 .globl _KiTrap2
197 _KiTrap2:
198 pushl $0
199 pushl %ebp
200 pushl %ebx
201 pushl %esi
202 movl $2, %esi
203 jmp _KiTrapProlog
204
205 .globl _KiTrap3
206 _KiTrap3:
207 pushl $0
208 pushl %ebp
209 pushl %ebx
210 pushl %esi
211 movl $3, %esi
212 jmp _KiTrapProlog
213
214 .globl _KiTrap4
215 _KiTrap4:
216 pushl $0
217 pushl %ebp
218 pushl %ebx
219 pushl %esi
220 movl $4, %esi
221 jmp _KiTrapProlog
222
223 .globl _KiTrap5
224 _KiTrap5:
225 pushl $0
226 pushl %ebp
227 pushl %ebx
228 pushl %esi
229 movl $5, %esi
230 jmp _KiTrapProlog
231
232 .globl _KiTrap6
233 _KiTrap6:
234 pushl $0
235 pushl %ebp
236 pushl %ebx
237 pushl %esi
238 movl $6, %esi
239 jmp _KiTrapProlog
240
241 .globl _KiTrap7
242 _KiTrap7:
243 pushl $0
244 pushl %ebp
245 pushl %ebx
246 pushl %esi
247 movl $7, %esi
248 jmp _KiTrapProlog
249
250 .globl _KiTrap8
251 _KiTrap8:
252 call _KiDoubleFaultHandler
253 iret
254
255 .globl _KiTrap9
256 _KiTrap9:
257 pushl $0
258 pushl %ebp
259 pushl %ebx
260 pushl %esi
261 movl $9, %esi
262 jmp _KiTrapProlog
263
264 .globl _KiTrap10
265 _KiTrap10:
266 pushl %ebp
267 pushl %ebx
268 pushl %esi
269 movl $10, %esi
270 jmp _KiTrapProlog
271
272 .globl _KiTrap11
273 _KiTrap11:
274 pushl %ebp
275 pushl %ebx
276 pushl %esi
277 movl $11, %esi
278 jmp _KiTrapProlog
279
280 .globl _KiTrap12
281 _KiTrap12:
282 pushl %ebp
283 pushl %ebx
284 pushl %esi
285 movl $12, %esi
286 jmp _KiTrapProlog
287
288 .globl _KiTrap13
289 _KiTrap13:
290 pushl %ebp
291 pushl %ebx
292 pushl %esi
293 movl $13, %esi
294 jmp _KiTrapProlog
295
296 .globl _KiTrap14
297 _KiTrap14:
298 pushl %ebp
299 pushl %ebx
300 pushl %esi
301 movl $14, %esi
302 jmp _KiTrapProlog
303
304 .globl _KiTrap15
305 _KiTrap15:
306 pushl %ebp
307 pushl %ebx
308 pushl %esi
309 movl $15, %esi
310 jmp _KiTrapProlog
311
312 .globl _KiTrap16
313 _KiTrap16:
314 pushl %ebp
315 pushl %ebx
316 pushl %esi
317 movl $16, %esi
318 jmp _KiTrapProlog
319
320 .globl _KiTrapUnknown
321 _KiTrapUnknown:
322 pushl $0
323 pushl %ebp
324 pushl %ebx
325 pushl %esi
326 movl $255, %esi
327 jmp _KiTrapProlog
328
329
330 /* EOF */