* Sync to recent trunk (r52563).
[reactos.git] / boot / freeldr / freeldr / arch / i386 / i386trap.S
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <asm.inc>
21 #include <arch/pc/x86common.h>
22 #include <ver.h>
23
24 #define SCREEN_ATTR 0x1f /* Bright white on blue background */
25
26 EXTERN _i386PrintExceptionText@12:PROC
27
28 .code32
29
30 MACRO(SAVE_CPU_REGS)
31 /* push the rest of the KTRAP_FRAME */
32 push ebp
33 push ebx
34 push esi
35 push edi
36 push fs
37 push 0 // ExceptionList
38 push 0 // PreviousPreviousMode
39 push eax
40 push ecx
41 push edx
42 push ds
43 push es
44 push gs
45 mov eax, dr7
46 push eax
47 mov eax, dr6
48 push eax
49 mov eax, dr3
50 push eax
51 mov eax, dr2
52 push eax
53 mov eax, dr1
54 push eax
55 mov eax, dr0
56 push eax
57 sub esp, 6 * 4
58
59 /* push KSPECIAL_REGISTERS */
60 /* Gdtr, Idtr, Tr, Ldtr, Reserved */
61 sub esp, 44
62 sgdt [esp]
63 sidt [esp + 8]
64 str word ptr [esp + 16]
65 sldt word ptr [esp + 18]
66 mov eax, dr7;
67 push eax
68 mov eax, dr6;
69 push eax
70 mov eax, dr3;
71 push eax
72 mov eax, dr2;
73 push eax
74 mov eax, dr1;
75 push eax
76 mov eax, dr0;
77 push eax
78 mov eax, cr4;
79 push eax
80 mov eax, cr3;
81 push eax
82 mov eax, cr2;
83 push eax
84 mov eax, cr0;
85 push eax
86 ENDM
87
88 /* Set by each exception handler to the address of the description text */
89 i386ExceptionIndex:
90 .long 0
91
92 /************************************************************************/
93 i386CommonExceptionHandler:
94
95 SAVE_CPU_REGS
96
97 lea eax, [esp + (21 * 4)] // KTRAP_FRAME
98 push esp // KSPECIAL_REGISTERS
99 push eax
100 push i386ExceptionIndex
101 call _i386PrintExceptionText@12
102
103 cli
104 i386ExceptionHandlerHang:
105 hlt
106 jmp i386ExceptionHandlerHang
107
108 iret
109
110 MACRO(TRAP_STUB, function, index)
111 PUBLIC VAL(function)
112 #ifdef _USE_ML
113 function:
114 #else
115 \function:
116 #endif
117 push 0 // Fake error code
118 mov dword ptr i386ExceptionIndex, VAL(index)
119 jmp i386CommonExceptionHandler
120 ENDM
121
122 MACRO(TRAP_STUB2, function, index)
123 PUBLIC VAL(function)
124 #ifdef _USE_ML
125 function:
126 #else
127 \function:
128 #endif
129 mov dword ptr i386ExceptionIndex, VAL(index)
130 jmp i386CommonExceptionHandler
131 ENDM
132
133 /************************************************************************/
134 TRAP_STUB _i386DivideByZero, 0
135
136 TRAP_STUB _i386DebugException, 1
137 TRAP_STUB _i386NMIException, 2
138 TRAP_STUB _i386Breakpoint, 3
139 TRAP_STUB _i386Overflow, 4
140 TRAP_STUB _i386BoundException, 5
141 TRAP_STUB _i386InvalidOpcode, 6
142 TRAP_STUB _i386FPUNotAvailable, 7
143 TRAP_STUB2 _i386DoubleFault, 8
144 TRAP_STUB _i386CoprocessorSegment, 9
145 TRAP_STUB2 _i386InvalidTSS, 10
146 TRAP_STUB2 _i386SegmentNotPresent, 11
147 TRAP_STUB2 _i386StackException, 12
148 TRAP_STUB2 _i386GeneralProtectionFault, 13
149 TRAP_STUB2 _i386PageFault, 14
150 // 15 is reserved
151 TRAP_STUB _i386CoprocessorError, 16
152 TRAP_STUB _i386AlignmentCheck, 17
153 TRAP_STUB _i386MachineCheck, 18
154 TRAP_STUB _i386SimdFloatError, 19
155
156 /************************************************************************
157 * DEBUGGING SUPPORT FUNCTIONS
158 ************************************************************************/
159
160 MACRO(BREAKPOINT_TEPLATE, functionname, mask1, mask2)
161 PUBLIC VAL(functionname)
162 #ifdef _USE_ML
163 functionname:
164 #else
165 \functionname:
166 #endif
167 push eax
168 mov eax, [esp + 8]
169 mov dr3, eax
170 mov eax, dr7
171 and eax, VAL(mask1)
172 or eax, VAL(mask2)
173 mov dr7, eax
174 pop eax
175 ret
176 ENDM
177
178 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT1, HEX(0fff0ffff), HEX(000000303)
179 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT1, HEX(0fff0ffff), HEX(000030303)
180 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT1, HEX(0fff0ffff), HEX(000010303)
181 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT2, HEX(0ff0fffff), HEX(00000030c)
182 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT2, HEX(0ff0fffff), HEX(00030030c)
183 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT2, HEX(0ff0fffff), HEX(00010030c)
184 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT3, HEX(0f0ffffff), HEX(000000330)
185 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT3, HEX(0f0ffffff), HEX(003000330)
186 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT3, HEX(0f0ffffff), HEX(001000330)
187 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT4, HEX(00fffffff), HEX(0000003c0)
188 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT4, HEX(00fffffff), HEX(0300003c0)
189 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT4, HEX(00fffffff), HEX(0100003c0)
190
191
192 END