2ab3bdfd6ff377cf3e19bb6543bef5abcfd6c65e
[reactos.git] / reactos / ntoskrnl / include / internal / i386 / asmmacro.S
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/include/i386/asmmacro.S
5 * PURPOSE: Assembly Macros for Spinlocks and common Trap Code
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 * Timo Kreuzer (timo.kreuzer@reactos.org)
8 */
9
10 // Arguments for idt
11 #define INT_32_DPL0 HEX(08E00)
12 #define INT_32_DPL3 HEX(0EE00)
13
14 //
15 // These macros are inlined equivalents of KiAcquire/ReleaseSpinlock, that is,
16 // they will not be compiled into non-SMP builds. Usage is as follows:
17 //
18 // .BeginYourFunction
19 // mov reg, lockaddr
20 // ACQUIRE_SPINLOCK(reg, .spin)
21 // <thread-safe code here>
22 // RELEASE_SPINLOCK(reg)
23 // <misc code here>
24 // retn
25 // #IFDEF CONFIG_SMP
26 // .spin
27 // <any necessary steps to be able to jump back safely>
28 // SPIN_ON_LOCK(reg, .BeginYourFunction)
29 // #ENDIF
30 //
31 #ifdef CONFIG_SMP
32 #define LOCK lock
33 #define ACQUIRE_SPINLOCK(x, y) \
34 lock bts dword ptr [x], 0; \
35 jb y
36 #define RELEASE_SPINLOCK(x) mov byte ptr [x], 0
37 #define SPIN_ON_LOCK(x, y) \
38 1: \
39 test dword ptr [x], 1; \
40 jz y; \
41 pause; \
42 jmp 1b
43 #else
44 #define LOCK
45 #define ACQUIRE_SPINLOCK(x, y)
46 #define RELEASE_SPINLOCK(x)
47 #endif
48
49 //
50 // @name IDT
51 //
52 // This macro creates an IDT entry for the given handler
53 //
54 // @param Handler
55 // Pointer to the IDT handler
56 //
57 // @param Bits
58 // Descriptor Bits to associate
59 //
60 // @remark None.
61 //
62 MACRO(idt, Handler, Bits)
63 .long VAL(Handler)
64 .short VAL(Bits)
65 .short KGDT_R0_CODE
66 ENDM
67
68
69 KI_PUSH_FAKE_ERROR_CODE = HEX(0001)
70 KI_UNUSED = HEX(0002)
71 KI_NONVOLATILES_ONLY = HEX(0004)
72 KI_FAST_SYSTEM_CALL = HEX(0008)
73 KI_SOFTWARE_TRAP = HEX(0010)
74 KI_HARDWARE_INT = HEX(0020)
75 KI_DONT_SAVE_SEGS = HEX(0100)
76
77 MACRO(KiEnterTrap, Flags)
78 LOCAL kernel_trap
79 LOCAL not_v86_trap
80 LOCAL set_sane_segs
81
82 /* Check what kind of trap frame this trap requires */
83 if (Flags AND KI_FAST_SYSTEM_CALL)
84
85 /* SYSENTER requires us to build a complete ring transition trap frame */
86 FrameSize = KTRAP_FRAME_V86_ES
87
88 /* Fixup fs. cx is free to clobber */
89 mov cx, KGDT_R0_PCR
90 mov fs, cx
91
92 /* Get pointer to the TSS */
93 mov ecx, fs:[KPCR_TSS]
94
95 /* Get a stack pointer */
96 mov esp, [ecx + KTSS_ESP0]
97
98 elseif (Flags AND KI_SOFTWARE_TRAP)
99
100 /* Software traps need a complete non-ring transition trap frame */
101 FrameSize = KTRAP_FRAME_ESP
102
103 /* Software traps need to get their EIP from the caller's frame */
104 pop eax
105
106 elseif (Flags AND KI_PUSH_FAKE_ERROR_CODE)
107
108 /* If the trap doesn't have an error code, we'll make space for it */
109 FrameSize = KTRAP_FRAME_EIP
110
111 else
112
113 /* The trap already has an error code, so just make space for the rest */
114 FrameSize = KTRAP_FRAME_ERROR_CODE
115
116 endif
117
118 /* Make space for this frame */
119 sub esp, FrameSize
120
121 /* Save nonvolatile registers */
122 mov [esp + KTRAP_FRAME_EBP], ebp
123 mov [esp + KTRAP_FRAME_EBX], ebx
124 mov [esp + KTRAP_FRAME_ESI], esi
125 mov [esp + KTRAP_FRAME_EDI], edi
126
127 /* Save eax for system calls, for use by the C handler */
128 mov [esp + KTRAP_FRAME_EAX], eax
129
130 /* Does the caller want nonvolatiles only? */
131 if (NOT (Flags AND KI_NONVOLATILES_ONLY))
132 /* Otherwise, save the volatiles as well */
133 mov [esp + KTRAP_FRAME_ECX], ecx
134 mov [esp + KTRAP_FRAME_EDX], edx
135 endif
136
137 /* Save segment registers? */
138 if (Flags AND KI_DONT_SAVE_SEGS)
139
140 /* Initialize TrapFrame segment registers with sane values */
141 mov eax, KGDT_R3_DATA OR 3
142 mov ecx, fs
143 mov [esp + KTRAP_FRAME_DS], eax
144 mov [esp + KTRAP_FRAME_ES], eax
145 mov [esp + KTRAP_FRAME_FS], ecx
146 mov dword ptr [esp + KTRAP_FRAME_GS], 0
147
148 else
149
150 /* Check for V86 mode */
151 test byte ptr [esp + KTRAP_FRAME_EFLAGS + 2], (EFLAGS_V86_MASK / HEX(10000))
152 jz not_v86_trap
153
154 /* Restore V8086 segments into Protected Mode segments */
155 mov eax, [esp + KTRAP_FRAME_V86_DS]
156 mov ecx, [esp + KTRAP_FRAME_V86_ES]
157 mov [esp + KTRAP_FRAME_DS], eax
158 mov [esp + KTRAP_FRAME_ES], ecx
159 mov eax, [esp + KTRAP_FRAME_V86_FS]
160 mov ecx, [esp + KTRAP_FRAME_V86_GS]
161 mov [esp + KTRAP_FRAME_FS], eax
162 mov [esp + KTRAP_FRAME_GS], ecx
163 jmp set_sane_segs
164
165 not_v86_trap:
166
167 /* Save segment selectors */
168 mov eax, ds
169 mov ecx, es
170 mov [esp + KTRAP_FRAME_DS], eax
171 mov [esp + KTRAP_FRAME_ES], ecx
172 mov eax, fs
173 mov ecx, gs
174 mov [esp + KTRAP_FRAME_FS], eax
175 mov [esp + KTRAP_FRAME_GS], ecx
176
177 endif
178
179 set_sane_segs:
180 /* Load correct data segments */
181 mov ax, KGDT_R3_DATA OR RPL_MASK
182 mov ds, ax
183 mov es, ax
184
185 /* Fast system calls have fs already fixed */
186 if (NOT (Flags AND KI_FAST_SYSTEM_CALL))
187 /* Otherwise fix fs now */
188 mov ax, KGDT_R0_PCR
189 mov fs, ax
190 endif
191
192 #if DBG
193 /* Keep the frame chain intact */
194 mov eax, [esp + KTRAP_FRAME_EIP]
195 mov [esp + KTRAP_FRAME_DEBUGEIP], eax
196 mov [esp + KTRAP_FRAME_DEBUGEBP], ebp
197 mov ebp, esp
198 #endif
199
200 /* Set parameter 1 (ECX) to point to the frame */
201 mov ecx, esp
202
203 /* Clear direction flag */
204 cld
205
206 ENDM
207
208 MACRO(KiCallHandler, Handler)
209 #if DBG
210 /* Use a call to get the return address for back traces */
211 call Handler
212 #else
213 /* Use the faster jmp */
214 jmp Handler
215 #endif
216 nop
217 ENDM
218
219 MACRO(TRAP_ENTRY, Trap, Flags)
220 EXTERN @&Trap&Handler@4 :PROC
221 PUBLIC _&Trap
222 .PROC _&Trap
223 /* Generate proper debugging symbols */
224 FPO 0, 0, 0, 0, 1, FRAME_TRAP
225
226 /* Common code to create the trap frame */
227 KiEnterTrap Flags
228
229 /* Call the C handler */
230 KiCallHandler @&Trap&Handler@4
231 .ENDP
232 ENDM
233
234 #define KI_RESTORE_EAX HEX(001)
235 #define KI_RESTORE_ECX_EDX HEX(002)
236 #define KI_RESTORE_FS HEX(004)
237 #define KI_RESTORE_SEGMENTS HEX(008)
238 #define KI_RESTORE_EFLAGS HEX(010)
239 #define KI_EXIT_SYSCALL HEX(020)
240 #define KI_EXIT_JMP HEX(040)
241 #define KI_EXIT_RET HEX(080)
242 #define KI_EXIT_IRET HEX(100)
243 #define KI_EDITED_FRAME HEX(200)
244 #define KI_EXIT_RET8 HEX(400)
245 #define KI_RESTORE_VOLATILES (KI_RESTORE_EAX OR KI_RESTORE_ECX_EDX)
246
247 MACRO(KiTrapExitStub, Name, Flags)
248
249 PUBLIC @&Name&@4
250 @&Name&@4:
251
252 if (Flags AND KI_EXIT_RET8) OR (Flags AND KI_EXIT_IRET)
253
254 /* This is the IRET frame */
255 OffsetEsp = KTRAP_FRAME_EIP
256
257 elseif (Flags AND KI_RESTORE_EFLAGS)
258
259 /* We will pop EFlags off the stack */
260 OffsetEsp = KTRAP_FRAME_EFLAGS
261
262 else
263
264 OffsetEsp = 0
265
266 endif
267
268 if (Flags AND KI_EDITED_FRAME)
269
270 /* Load the requested ESP */
271 mov esp, [ecx + KTRAP_FRAME_TEMPESP]
272
273 /* Put return address on the new stack */
274 push [ecx + KTRAP_FRAME_EIP]
275
276 /* Put EFLAGS on the new stack */
277 push [ecx + KTRAP_FRAME_EFLAGS]
278
279 else
280
281 /* Point esp to an appropriate member of the frame */
282 lea esp, [ecx + OffsetEsp]
283
284 endif
285
286 /* Restore non volatiles */
287 mov ebx, [ecx + KTRAP_FRAME_EBX]
288 mov esi, [ecx + KTRAP_FRAME_ESI]
289 mov edi, [ecx + KTRAP_FRAME_EDI]
290 mov ebp, [ecx + KTRAP_FRAME_EBP]
291
292 if (Flags AND KI_RESTORE_EAX)
293
294 /* Restore eax */
295 mov eax, [ecx + KTRAP_FRAME_EAX]
296
297 endif
298
299 if (Flags AND KI_RESTORE_ECX_EDX)
300
301 /* Restore volatiles */
302 mov edx, [ecx + KTRAP_FRAME_EDX]
303 mov ecx, [ecx + KTRAP_FRAME_ECX]
304
305 elseif (Flags AND KI_EXIT_JMP)
306
307 /* Load return address into edx */
308 mov edx, [esp - OffsetEsp + KTRAP_FRAME_EIP]
309
310 elseif (Flags AND KI_EXIT_SYSCALL)
311
312 /* Set sysexit parameters */
313 mov edx, [esp - OffsetEsp + KTRAP_FRAME_EIP]
314 mov ecx, [esp - OffsetEsp + KTRAP_FRAME_ESP]
315
316 /* Keep interrupts disabled until the sti / sysexit */
317 and byte ptr [esp - OffsetEsp + KTRAP_FRAME_EFLAGS + 1], NOT (EFLAGS_INTERRUPT_MASK / HEX(100))
318
319 endif
320
321 if (Flags AND KI_RESTORE_SEGMENTS)
322
323 /* Restore segments for user mode */
324 mov ds, [esp - OffsetEsp + KTRAP_FRAME_DS]
325 mov es, [esp - OffsetEsp + KTRAP_FRAME_ES]
326 mov gs, [esp - OffsetEsp + KTRAP_FRAME_GS]
327
328 endif
329
330 if ((Flags AND KI_RESTORE_FS) OR (Flags AND KI_RESTORE_SEGMENTS))
331
332 /* Restore user mode FS */
333 mov fs, [esp - OffsetEsp + KTRAP_FRAME_FS]
334
335 endif
336
337 if (Flags AND KI_RESTORE_EFLAGS)
338
339 if (Flags AND KI_EXIT_RET8)
340
341 /* We are at the IRET frame, so push EFLAGS first */
342 push dword ptr [esp + 8]
343
344 endif
345
346 /* Restore EFLAGS */
347 popfd
348
349 endif
350
351 if (Flags AND KI_EXIT_SYSCALL)
352
353 /* Enable interrupts and return to user mode.
354 Both must follow directly after another to be "atomic". */
355 sti
356 sysexit
357
358 elseif (Flags AND KI_EXIT_IRET)
359
360 /* Return with iret */
361 iretd
362
363 elseif (Flags AND KI_EXIT_JMP)
364
365 /* Return to kernel mode with a jmp */
366 jmp edx
367
368 elseif (Flags AND KI_EXIT_RET8)
369
370 /* Return to kernel mode with a ret 8 */
371 ret 8
372
373 elseif (Flags AND KI_EXIT_RET)
374
375 /* Return to kernel mode with a ret */
376 ret
377
378 endif
379
380 ENDM
381