Rewrite project target
[reactos.git] / msvc6 / ntoskrnl / ke_i386_bthread.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2000 David Welch <welch@cwcom.net>
4 *
5 * Moved to MSVC-compatible inline assembler by Mike Nordell, 2003-12-25
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 /* $Id: ke_i386_bthread.c,v 1.1 2004/02/06 08:21:56 fireball Exp $
22 *
23 * COPYRIGHT: See COPYING in the top level directory
24 * PROJECT: ReactOS kernel
25 * FILE: ntoskrnl/ke/i386/bthread.S
26 * PURPOSE: Trap handlers
27 * PROGRAMMER: David Welch (david.welch@seh.ox.ac.uk)
28 */
29
30 /* INCLUDES ******************************************************************/
31
32 #include <ddk/ntddk.h>
33 #include <ddk/status.h>
34 #include <internal/i386/segment.h>
35 #include <internal/i386/fpu.h>
36 #include <internal/ps.h>
37 #include <ddk/defines.h>
38
39 /* Values for contextflags */
40 #define CONTEXT_i386 0x10000
41 #ifndef CONTEXT_CONTROL
42 #define CONTEXT_CONTROL (CONTEXT_i386 | 1)
43 #endif
44 #ifndef CONTEXT_INTEGER
45 #define CONTEXT_INTEGER (CONTEXT_i386 | 2)
46 #endif
47 #ifndef CONTEXT_SEGMENTS
48 #define CONTEXT_SEGMENTS (CONTEXT_i386 | 4)
49 #endif
50 #ifndef CONTEXT_FLOATING_POINT
51 #define CONTEXT_FLOATING_POINT (CONTEXT_i386 | 8)
52 #endif
53 #ifndef CONTEXT_DEBUG_REGISTERS
54 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_i386 | 0x10)
55 #endif
56 #ifndef CONTEXT_FULL
57 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS)
58 #endif
59
60 /* FUNCTIONS *****************************************************************/
61
62 void KeReturnFromSystemCallWithHook();
63
64 VOID PiBeforeBeginThread(CONTEXT c);
65
66 /*
67 *
68 */
69
70 __declspec(naked)
71 VOID PsBeginThread(PKSTART_ROUTINE StartRoutine, PVOID StartContext)
72 {
73 /*
74 * This isn't really a function, we are called as the return address
75 * of the context switch function
76 */
77
78 /*
79 * Do the necessary prolog after a context switch
80 */
81 __asm
82 {
83 call PiBeforeBeginThread
84
85 /*
86 * Call the actual start of the thread
87 */
88 // We must NOT use the arguments by name. VC then uses EBP-relative
89 // addressing, and with an EBP of 0 you can imagine what happens.
90 mov ebx, 4[esp] // StartRoutine
91 mov eax, 8[esp] // StartContext
92 push eax
93 call ebx /* Call the start routine */
94 add esp, 4
95
96 /*
97 * Terminate the thread
98 */
99 push eax
100 call PsTerminateSystemThread
101 add esp, 4
102
103 }
104
105 /* If that fails then bug check */
106 KeBugCheck(0);
107
108 /* And if that fails then loop */
109 for (;;)
110 ; // forever
111 }
112
113
114 __declspec(naked)
115 VOID PsBeginThreadWithContextInternal(VOID)
116 {
117 /*
118 * This isn't really a function, we are called as the return
119 * address of a context switch
120 */
121
122 /*
123 * Do the necessary prolog before the context switch
124 */
125 __asm
126 {
127 call PiBeforeBeginThread
128
129 /*
130 * Load the context flags.
131 */
132 pop ebx
133
134 /*
135 * Load the debugging registers
136 */
137 test ebx, (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)
138 jz L1
139 pop eax __asm mov dr0, eax
140 pop eax __asm mov dr1, eax
141 pop eax __asm mov dr2, eax
142 pop eax __asm mov dr3, eax
143 pop eax __asm mov dr6, eax
144 pop eax __asm mov dr7, eax
145 jmp L3
146 L1:
147 add esp, 24
148 L3:
149
150 /*
151 * Load the floating point registers
152 */
153 mov eax, HardwareMathSupport
154 test eax,eax
155 jz L2
156 test ebx, (CONTEXT_FLOATING_POINT & ~CONTEXT_i386)
157 jz L2
158 frstor [esp]
159 L2:
160 add esp, 112
161
162 /* Load the rest of the thread's user mode context. */
163 mov eax, 0
164 jmp KeReturnFromSystemCallWithHook
165 }
166 }
167