272886d489130f5cf8f260b1ee45d14047a42cfe
[reactos.git] / reactos / ntoskrnl / ke / i386 / trap.s
1 /*
2 * FILE: ntoskrnl/ke/i386/trap.S
3 * COPYRIGHT: See COPYING in the top level directory
4 * PURPOSE: System Traps, Entrypoints and Exitpoints
5 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
6 * Timo Kreuzer (timo.kreuzer@reactos.org)
7 * NOTE: See asmmacro.S for the shared entry/exit code.
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <asm.inc>
13 #include <ks386.inc>
14 #include <internal/i386/asmmacro.S>
15
16 MACRO(GENERATE_IDT_STUB, Vector)
17 idt _KiUnexpectedInterrupt&Vector, INT_32_DPL0
18 ENDM
19
20 MACRO(GENERATE_INT_HANDLER, Vector)
21 //.func KiUnexpectedInterrupt&Number
22 _KiUnexpectedInterrupt&Vector:
23 /* This is a push instruction with 8bit operand. Since the instruction
24 sign extends the value to 32 bits, we need to offset it */
25 push (Vector - 128)
26 jmp _KiEndUnexpectedRange@0
27 //.endfunc
28 ENDM
29
30 EXTERN _KiTrap02:PROC
31
32 /* GLOBALS *******************************************************************/
33
34 .data
35 ASSUME nothing
36
37 .align 16
38
39 PUBLIC _KiIdt
40 _KiIdt:
41 /* This is the Software Interrupt Table that we handle in this file: */
42 idt _KiTrap00, INT_32_DPL0 /* INT 00: Divide Error (#DE) */
43 idt _KiTrap01, INT_32_DPL0 /* INT 01: Debug Exception (#DB) */
44 idt _KiTrap02, INT_32_DPL0 /* INT 02: NMI Interrupt */
45 idt _KiTrap03, INT_32_DPL3 /* INT 03: Breakpoint Exception (#BP) */
46 idt _KiTrap04, INT_32_DPL3 /* INT 04: Overflow Exception (#OF) */
47 idt _KiTrap05, INT_32_DPL0 /* INT 05: BOUND Range Exceeded (#BR) */
48 idt _KiTrap06, INT_32_DPL0 /* INT 06: Invalid Opcode Code (#UD) */
49 idt _KiTrap07, INT_32_DPL0 /* INT 07: Device Not Available (#NM) */
50 idt _KiTrap08, INT_32_DPL0 /* INT 08: Double Fault Exception (#DF) */
51 idt _KiTrap09, INT_32_DPL0 /* INT 09: RESERVED */
52 idt _KiTrap0A, INT_32_DPL0 /* INT 0A: Invalid TSS Exception (#TS) */
53 idt _KiTrap0B, INT_32_DPL0 /* INT 0B: Segment Not Present (#NP) */
54 idt _KiTrap0C, INT_32_DPL0 /* INT 0C: Stack Fault Exception (#SS) */
55 idt _KiTrap0D, INT_32_DPL0 /* INT 0D: General Protection (#GP) */
56 idt _KiTrap0E, INT_32_DPL0 /* INT 0E: Page-Fault Exception (#PF) */
57 idt _KiTrap0F, INT_32_DPL0 /* INT 0F: RESERVED */
58 idt _KiTrap10, INT_32_DPL0 /* INT 10: x87 FPU Error (#MF) */
59 idt _KiTrap11, INT_32_DPL0 /* INT 11: Align Check Exception (#AC) */
60 idt _KiTrap0F, INT_32_DPL0 /* INT 12: Machine Check Exception (#MC)*/
61 idt _KiTrap0F, INT_32_DPL0 /* INT 13: SIMD FPU Exception (#XF) */
62 REPEAT 22
63 idt _KiTrap0F, INT_32_DPL0 /* INT 14-29: UNDEFINED INTERRUPTS */
64 ENDR
65 idt _KiGetTickCount, INT_32_DPL3 /* INT 2A: Get Tick Count Handler */
66 idt _KiCallbackReturn, INT_32_DPL3 /* INT 2B: User-Mode Callback Return */
67 idt _KiRaiseAssertion, INT_32_DPL3 /* INT 2C: Debug Assertion Handler */
68 idt _KiDebugService, INT_32_DPL3 /* INT 2D: Debug Service Handler */
69 idt _KiSystemService, INT_32_DPL3 /* INT 2E: System Call Service Handler */
70 idt _KiTrap0F, INT_32_DPL0 /* INT 2F: RESERVED */
71 i = HEX(30)
72 REPEAT 208
73 GENERATE_IDT_STUB %i
74 i = i + 1
75 ENDR
76
77 PUBLIC _KiIdtDescriptor
78 _KiIdtDescriptor:
79 .short 0
80 .short HEX(7FF)
81 .long _KiIdt
82
83 PUBLIC _KiUnexpectedEntrySize
84 _KiUnexpectedEntrySize:
85 .long _KiUnexpectedInterrupt49 - _KiUnexpectedInterrupt48
86
87 /******************************************************************************/
88 .code
89
90 PUBLIC _KiStartUnexpectedRange@0
91 _KiStartUnexpectedRange@0:
92 i = HEX(30)
93 REPEAT 208
94 GENERATE_INT_HANDLER %i
95 i = i + 1
96 ENDR
97
98 TRAP_ENTRY KiTrap00, KI_PUSH_FAKE_ERROR_CODE
99 TRAP_ENTRY KiTrap01, KI_PUSH_FAKE_ERROR_CODE
100 TRAP_ENTRY KiTrap03, KI_PUSH_FAKE_ERROR_CODE
101 TRAP_ENTRY KiTrap04, KI_PUSH_FAKE_ERROR_CODE
102 TRAP_ENTRY KiTrap05, KI_PUSH_FAKE_ERROR_CODE
103 TRAP_ENTRY KiTrap06, KI_PUSH_FAKE_ERROR_CODE
104 TRAP_ENTRY KiTrap07, KI_PUSH_FAKE_ERROR_CODE
105 TRAP_ENTRY KiTrap08, 0
106 TRAP_ENTRY KiTrap09, KI_PUSH_FAKE_ERROR_CODE
107 TRAP_ENTRY KiTrap0A, 0
108 TRAP_ENTRY KiTrap0B, 0
109 TRAP_ENTRY KiTrap0C, 0
110 TRAP_ENTRY KiTrap0D, 0
111 TRAP_ENTRY KiTrap0E, 0
112 TRAP_ENTRY KiTrap0F, KI_PUSH_FAKE_ERROR_CODE
113 TRAP_ENTRY KiTrap10, KI_PUSH_FAKE_ERROR_CODE
114 TRAP_ENTRY KiTrap11, KI_PUSH_FAKE_ERROR_CODE
115 TRAP_ENTRY KiTrap13, KI_PUSH_FAKE_ERROR_CODE
116 TRAP_ENTRY KiGetTickCount, KI_PUSH_FAKE_ERROR_CODE
117 TRAP_ENTRY KiCallbackReturn, KI_PUSH_FAKE_ERROR_CODE
118 TRAP_ENTRY KiRaiseAssertion, KI_PUSH_FAKE_ERROR_CODE
119 TRAP_ENTRY KiDebugService, KI_PUSH_FAKE_ERROR_CODE
120 TRAP_ENTRY KiUnexpectedInterruptTail, 0
121
122 ALIGN 4
123 EXTERN @KiInterruptTemplateHandler@8:PROC
124 PUBLIC _KiInterruptTemplate
125 _KiInterruptTemplate:
126 KiEnterTrap KI_PUSH_FAKE_ERROR_CODE
127 PUBLIC _KiInterruptTemplate2ndDispatch
128 _KiInterruptTemplate2ndDispatch:
129 mov edx, 0
130 PUBLIC _KiInterruptTemplateObject
131 _KiInterruptTemplateObject:
132 mov eax, offset @KiInterruptTemplateHandler@8
133 jmp eax
134 PUBLIC _KiInterruptTemplateDispatch
135 _KiInterruptTemplateDispatch:
136
137 EXTERN @KiSystemServiceHandler@8:PROC
138 PUBLIC _KiSystemService
139 .PROC _KiSystemService
140 FPO 0, 0, 0, 0, 1, FRAME_TRAP
141 KiEnterTrap (KI_PUSH_FAKE_ERROR_CODE OR KI_NONVOLATILES_ONLY OR KI_DONT_SAVE_SEGS)
142 KiCallHandler @KiSystemServiceHandler@8
143 .ENDP
144
145 EXTERN @KiFastCallEntryHandler@8:PROC
146 PUBLIC _KiFastCallEntry
147 .PROC _KiFastCallEntry
148 FPO 0, 0, 0, 0, 1, FRAME_TRAP
149 KiEnterTrap (KI_FAST_SYSTEM_CALL OR KI_NONVOLATILES_ONLY OR KI_DONT_SAVE_SEGS)
150 KiCallHandler @KiFastCallEntryHandler@8
151 .ENDP
152
153
154 PUBLIC _KiEndUnexpectedRange@0
155 _KiEndUnexpectedRange@0:
156 add dword ptr[esp], 128
157 jmp _KiUnexpectedInterruptTail
158
159
160 /* EXIT CODE *****************************************************************/
161
162 KiTrapExitStub KiSystemCallReturn, (KI_RESTORE_EAX OR KI_RESTORE_EFLAGS OR KI_EXIT_JMP)
163 KiTrapExitStub KiSystemCallSysExitReturn, (KI_RESTORE_EAX OR KI_RESTORE_FS OR KI_RESTORE_EFLAGS OR KI_EXIT_SYSCALL)
164 KiTrapExitStub KiSystemCallTrapReturn, (KI_RESTORE_EAX OR KI_RESTORE_FS OR KI_EXIT_IRET)
165
166 KiTrapExitStub KiEditedTrapReturn, (KI_RESTORE_VOLATILES OR KI_RESTORE_EFLAGS OR KI_EDITED_FRAME OR KI_EXIT_RET)
167 KiTrapExitStub KiTrapReturn, (KI_RESTORE_VOLATILES OR KI_RESTORE_SEGMENTS OR KI_EXIT_IRET)
168 KiTrapExitStub KiTrapReturnNoSegments, (KI_RESTORE_VOLATILES OR KI_EXIT_IRET)
169 KiTrapExitStub KiTrapReturnNoSegmentsRet8,(KI_RESTORE_VOLATILES OR KI_RESTORE_EFLAGS OR KI_EXIT_RET8)
170
171 #ifdef _MSC_VER
172 EXTERN _PsConvertToGuiThread@0:PROC
173
174 PUBLIC _KiConvertToGuiThread@0
175 _KiConvertToGuiThread@0:
176 /* Safe ebx */
177 push ebx
178
179 /* Calculate the stack frame offset in ebx */
180 mov ebx, ebp
181 sub ebx, esp
182
183 /* Call the worker function */
184 call _PsConvertToGuiThread@0
185
186 /* Adjust ebp to the new stack */
187 mov ebp, esp
188 add ebp, ebx
189
190 /* Restore ebx */
191 pop ebx
192
193 /* return to the caller */
194 ret
195 #endif
196
197 END