be5c7de3ce9bb67e5bd62d0e425a9dc5307d4061
[reactos.git] / reactos / 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 .intel_syntax noprefix
21 .text
22 .code16
23
24 #define ASM
25 #include <arch.h>
26 #include <ver.h>
27
28 #define SCREEN_ATTR 0x1f /* Bright white on blue background */
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 [esp + 16]
65 sldt [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 /************************************************************************/
94 i386CommonExceptionHandler:
95 .code32
96
97 SAVE_CPU_REGS
98
99 lea eax, [esp + (21 * 4)] // KTRAP_FRAME
100 push esp // KSPECIAL_REGISTERS
101 push eax
102 push i386ExceptionIndex
103 call _i386PrintExceptionText@12
104
105 cli
106 i386ExceptionHandlerHang:
107 hlt
108 jmp i386ExceptionHandlerHang
109
110 iret
111
112 .macro TRAP_STUB function, index
113 EXTERN(\function)
114 push 0 // Fake error code
115 mov dword ptr i386ExceptionIndex, \index
116 jmp i386CommonExceptionHandler
117 .endm
118
119 .macro TRAP_STUB2 function, index
120 EXTERN(\function)
121 mov dword ptr i386ExceptionIndex, \index
122 jmp i386CommonExceptionHandler
123 .endm
124
125 /************************************************************************/
126 TRAP_STUB i386DivideByZero, 0
127 TRAP_STUB i386DebugException, 1
128 TRAP_STUB i386NMIException, 2
129 TRAP_STUB i386Breakpoint, 3
130 TRAP_STUB i386Overflow, 4
131 TRAP_STUB i386BoundException, 5
132 TRAP_STUB i386InvalidOpcode, 6
133 TRAP_STUB i386FPUNotAvailable, 7
134 TRAP_STUB2 i386DoubleFault, 8
135 TRAP_STUB i386CoprocessorSegment, 9
136 TRAP_STUB2 i386InvalidTSS, 10
137 TRAP_STUB2 i386SegmentNotPresent, 11
138 TRAP_STUB2 i386StackException, 12
139 TRAP_STUB2 i386GeneralProtectionFault, 13
140 TRAP_STUB2 i386PageFault, 14
141 // 15 is reserved
142 TRAP_STUB i386CoprocessorError, 16
143 TRAP_STUB i386AlignmentCheck, 17
144 TRAP_STUB i386MachineCheck, 18
145 TRAP_STUB i386SimdFloatError, 19
146
147 /************************************************************************
148 * DEBUGGING SUPPORT FUNCTIONS
149 ************************************************************************/
150
151 .macro BREAKPOINT_TEPLATE functionname, mask1, mask2
152 EXTERN(\functionname)
153 push eax
154 mov eax, [esp + 8]
155 mov dr3, eax
156 mov eax, dr7
157 and eax, \mask1
158 or eax, \mask2
159 mov dr7, eax
160 pop eax
161 ret
162 .endm
163
164 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT1, 0xfff0ffff, 0x00000303
165 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT1, 0xfff0ffff, 0x00030303
166 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT1, 0xfff0ffff, 0x00010303
167 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT2, 0xff0fffff, 0x0000030c
168 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT2, 0xff0fffff, 0x0030030c
169 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT2, 0xff0fffff, 0x0010030c
170 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT3, 0xf0ffffff, 0x00000330
171 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT3, 0xf0ffffff, 0x03000330
172 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT3, 0xf0ffffff, 0x01000330
173 BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT4, 0x0fffffff, 0x000003c0
174 BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT4, 0x0fffffff, 0x300003c0
175 BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT4, 0x0fffffff, 0x100003c0
176
177