9a9c95f4db1b353b2c3d2e31ef6ce248f83338e5
[reactos.git] / ntoskrnl / ke / i386 / ctxswitch.S
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/i386/ctxswitch.S
5 * PURPOSE: Thread Context Switching
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 * Gregor Anich (FPU Code)
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ndk/asm.h>
14 .intel_syntax noprefix
15
16 /* FUNCTIONS ****************************************************************/
17
18 .globl @KiSwapContextInternal@0
19 .func @KiSwapContextInternal@0, @KiSwapContextInternal@0
20 @KiSwapContextInternal@0:
21 /* Build switch frame */
22 sub esp, 2 * 4
23 mov ecx, esp
24 jmp @KiSwapContextEntry@8
25 .endfunc
26
27 .globl @KiSwapContext@8
28 .func @KiSwapContext@8, @KiSwapContext@8
29 @KiSwapContext@8:
30 /* Save 4 registers */
31 sub esp, 4 * 4
32
33 /* Save all the non-volatile ones */
34 mov [esp+12], ebx
35 mov [esp+8], esi
36 mov [esp+4], edi
37 mov [esp+0], ebp
38
39 /* Get the wait IRQL */
40 or dl, cl
41
42 /* Do the swap with the registers correctly setup */
43 call @KiSwapContextInternal@0
44
45 /* Return the registers */
46 mov ebp, [esp+0]
47 mov edi, [esp+4]
48 mov esi, [esp+8]
49 mov ebx, [esp+12]
50
51 /* Clean stack */
52 add esp, 4 * 4
53 ret
54 .endfunc
55
56 .globl @KiSwitchThreads@8
57 .func @KiSwitchThreads@8, @KiSwitchThreads@8
58 @KiSwitchThreads@8:
59 /* Load the new kernel stack and switch OS to new thread */
60 mov esp, edx
61 call @KiSwapContextExit@8
62
63 /* Now we're on the new thread. Return to the caller to restore registers */
64 add esp, 2 * 4
65 ret
66 .endfunc
67
68 .globl @KiRetireDpcListInDpcStack@8
69 .func @KiRetireDpcListInDpcStack@8, @KiRetireDpcListInDpcStack@8
70 @KiRetireDpcListInDpcStack@8:
71 /* Switch stacks and retire DPCs */
72 mov eax, esp
73 mov esp, edx
74 push eax
75 call @KiRetireDpcList@4
76
77 /* Return on original stack */
78 pop esp
79 ret
80 .endfunc
81
82 /* FIXFIX: Move to C code ****/
83 .globl _Ki386SetupAndExitToV86Mode@4
84 .func Ki386SetupAndExitToV86Mode@4
85 _Ki386SetupAndExitToV86Mode@4:
86
87 /* Enter V8086 mode */
88 pushad
89 sub esp, (12 + KTRAP_FRAME_LENGTH + NPX_FRAME_LENGTH)
90 mov ecx, esp
91 call @KiEnterV86Mode@4
92 jmp $
93 .endfunc
94
95 .globl @Ki386BiosCallReturnAddress@4
96 @Ki386BiosCallReturnAddress@4:
97
98 /* Exit V8086 mode */
99 call @KiExitV86Mode@4
100 mov esp, eax
101 add esp, (12 + KTRAP_FRAME_LENGTH + NPX_FRAME_LENGTH)
102 popad
103 ret 4
104