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