[NTOSKRNL]
[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 <asm.inc>
14 #include <ks386.inc>
15
16 EXTERN @KiSwapContextEntry@8:PROC
17 EXTERN @KiSwapContextExit@8:PROC
18 EXTERN @KiRetireDpcList@4:PROC
19 EXTERN @KiEnterV86Mode@4:PROC
20 EXTERN @KiExitV86Mode@4:PROC
21
22 /* FUNCTIONS ****************************************************************/
23 .code
24
25 PUBLIC @KiSwapContextInternal@0
26 @KiSwapContextInternal@0:
27 /* Build switch frame */
28 sub esp, 2 * 4
29 mov ecx, esp
30 jmp @KiSwapContextEntry@8
31
32
33 PUBLIC @KiSwapContext@8
34 @KiSwapContext@8:
35 /* Save 4 registers */
36 sub esp, 4 * 4
37
38 /* Save all the non-volatile ones */
39 mov [esp+12], ebx
40 mov [esp+8], esi
41 mov [esp+4], edi
42 mov [esp+0], ebp
43
44 /* Get the wait IRQL */
45 or dl, cl
46
47 /* Do the swap with the registers correctly setup */
48 call @KiSwapContextInternal@0
49
50 /* Return the registers */
51 mov ebp, [esp+0]
52 mov edi, [esp+4]
53 mov esi, [esp+8]
54 mov ebx, [esp+12]
55
56 /* Clean stack */
57 add esp, 4 * 4
58 ret
59
60
61 PUBLIC @KiSwitchThreads@8
62 @KiSwitchThreads@8:
63 /* Load the new kernel stack and switch OS to new thread */
64 mov esp, edx
65 call @KiSwapContextExit@8
66
67 /* Now we're on the new thread. Return to the caller to restore registers */
68 add esp, 2 * 4
69 ret
70
71
72 PUBLIC @KiRetireDpcListInDpcStack@8
73 @KiRetireDpcListInDpcStack@8:
74 /* Switch stacks and retire DPCs */
75 mov eax, esp
76 mov esp, edx
77 push eax
78 call @KiRetireDpcList@4
79
80 /* Return on original stack */
81 pop esp
82 ret
83
84
85 /* FIXFIX: Move to C code ****/
86 PUBLIC _Ki386SetupAndExitToV86Mode@4
87 _Ki386SetupAndExitToV86Mode@4:
88
89 /* Enter V8086 mode */
90 pushad
91 sub esp, (12 + KTRAP_FRAME_LENGTH + NPX_FRAME_LENGTH)
92 mov ecx, esp
93 call @KiEnterV86Mode@4
94 jmp $
95
96
97 PUBLIC @Ki386BiosCallReturnAddress@4
98 @Ki386BiosCallReturnAddress@4:
99
100 /* Exit V8086 mode */
101 call @KiExitV86Mode@4
102 mov esp, eax
103 add esp, (12 + KTRAP_FRAME_LENGTH + NPX_FRAME_LENGTH)
104 popad
105 ret 4
106
107 END