ReactOS 0.4.15-dev-7918-g2a2556c
thrdini.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for thrdini.c:

Go to the source code of this file.

Classes

struct  _KUINIT_FRAME
 
struct  _KKINIT_FRAME
 

Macros

#define NDEBUG
 

Typedefs

typedef struct _KUINIT_FRAME KUINIT_FRAME
 
typedef struct _KUINIT_FRAMEPKUINIT_FRAME
 
typedef struct _KKINIT_FRAME KKINIT_FRAME
 
typedef struct _KKINIT_FRAMEPKKINIT_FRAME
 

Functions

void KiInvalidSystemThreadStartupExit (void)
 
void KiUserThreadStartupExit (void)
 
void KiServiceExit3 (void)
 
VOID NTAPI KiInitializeContextThread (IN PKTHREAD Thread, IN PKSYSTEM_ROUTINE SystemRoutine, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext, IN PCONTEXT Context)
 
BOOLEAN KiSwapContextResume (_In_ BOOLEAN ApcBypass, _In_ PKTHREAD OldThread, _In_ PKTHREAD NewThread)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file thrdini.c.

Typedef Documentation

◆ KKINIT_FRAME

◆ KUINIT_FRAME

◆ PKKINIT_FRAME

◆ PKUINIT_FRAME

Function Documentation

◆ KiInitializeContextThread()

VOID NTAPI KiInitializeContextThread ( IN PKTHREAD  Thread,
IN PKSYSTEM_ROUTINE  SystemRoutine,
IN PKSTART_ROUTINE  StartRoutine,
IN PVOID  StartContext,
IN PCONTEXT  Context 
)

Definition at line 40 of file thrdini.c.

45{
46 //PFX_SAVE_AREA FxSaveArea;
47 //PFXSAVE_FORMAT FxSaveFormat;
48 PKSTART_FRAME StartFrame;
49 PKSWITCH_FRAME CtxSwitchFrame;
50 PKTRAP_FRAME TrapFrame;
51 ULONG ContextFlags;
52
53 /* Check if this is a With-Context Thread */
54 if (Context)
55 {
56 PKUINIT_FRAME InitFrame;
57
58 /* Set up the Initial Frame */
59 InitFrame = ((PKUINIT_FRAME)Thread->InitialStack) - 1;
60 StartFrame = &InitFrame->StartFrame;
61 CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
62
63 /* Save back the new value of the kernel stack. */
64 Thread->KernelStack = (PVOID)InitFrame;
65
66 /* Tell the thread it will run in User Mode */
67 Thread->PreviousMode = UserMode;
68
69 // FIXME Setup the Fx Area
70
71 /* Set the Thread's NPX State */
72 Thread->NpxState = 0xA;
73 Thread->Header.NpxIrql = PASSIVE_LEVEL;
74
75 /* Make sure, we have control registers, disable debug registers */
76 ASSERT((Context->ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL);
77 ContextFlags = Context->ContextFlags & ~CONTEXT_DEBUG_REGISTERS;
78
79 /* Setup the Trap Frame */
80 TrapFrame = &InitFrame->TrapFrame;
81
82 /* Zero out the trap frame */
83 RtlZeroMemory(TrapFrame, sizeof(KTRAP_FRAME));
84 RtlZeroMemory(&InitFrame->ExceptionFrame, sizeof(KEXCEPTION_FRAME));
85
86 /* Set up a trap frame from the context. */
88 &InitFrame->ExceptionFrame,
89 TrapFrame,
90 CONTEXT_AMD64 | ContextFlags,
91 UserMode);
92
93 /* Set SS, DS, ES's RPL Mask properly */
94 TrapFrame->SegSs |= RPL_MASK;
95 TrapFrame->SegDs |= RPL_MASK;
96 TrapFrame->SegEs |= RPL_MASK;
97 TrapFrame->Dr7 = 0;
98
99 /* Set the previous mode as user */
100 TrapFrame->PreviousMode = UserMode;
101
102 /* Terminate the Exception Handler List */
103 TrapFrame->ExceptionFrame = 0;
104
105 /* KiThreadStartup returns to KiUserThreadStartupExit */
107
108 /* KiUserThreadStartupExit returns to KiServiceExit3 */
110 }
111 else
112 {
113 PKKINIT_FRAME InitFrame;
114
115 /* Set up the Initial Frame for the system thread */
116 InitFrame = ((PKKINIT_FRAME)Thread->InitialStack) - 1;
117 StartFrame = &InitFrame->StartFrame;
118 CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
119
120 /* Save back the new value of the kernel stack. */
121 Thread->KernelStack = (PVOID)InitFrame;
122
123 /* Tell the thread it will run in Kernel Mode */
124 Thread->PreviousMode = KernelMode;
125
126 // FIXME Setup the Fx Area
127
128 /* No NPX State */
129 Thread->NpxState = 0xA;
130
131 /* This must never return! */
133 }
134
135 /* Set up the Context Switch Frame */
136 CtxSwitchFrame->Return = (ULONG64)KiThreadStartup;
137 CtxSwitchFrame->ApcBypass = TRUE;
138
139 StartFrame->P1Home = (ULONG64)StartRoutine;
140 StartFrame->P2Home = (ULONG64)StartContext;
141 StartFrame->P3Home = 0;
142 StartFrame->P4Home = (ULONG64)SystemRoutine;
143 StartFrame->Reserved = 0;
144}
void KiInvalidSystemThreadStartupExit(void)
void KiUserThreadStartupExit(void)
void KiServiceExit3(void)
struct _KUINIT_FRAME * PKUINIT_FRAME
struct _KKINIT_FRAME * PKKINIT_FRAME
#define TRUE
Definition: types.h:120
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define ASSERT(a)
Definition: mode.c:44
unsigned __int64 ULONG64
Definition: imports.h:198
#define KernelMode
Definition: asm.h:34
#define UserMode
Definition: asm.h:35
#define RPL_MASK
Definition: ketypes.h:130
#define CONTEXT_CONTROL
Definition: nt_native.h:1369
VOID NTAPI KiThreadStartup(VOID)
Definition: thrdini.c:63
VOID NTAPI KeContextToTrapFrame(PCONTEXT Context, PKEXCEPTION_FRAME ExeptionFrame, PKTRAP_FRAME TrapFrame, ULONG ContextFlags, KPROCESSOR_MODE PreviousMode)
#define CONTEXT_AMD64
ULONG64 Return
Definition: ketypes.h:1037
KSWITCH_FRAME CtxSwitchFrame
Definition: thrdini.c:31
KSTART_FRAME StartFrame
Definition: thrdini.c:32
ULONG64 P2Home
Definition: ketypes.h:1102
ULONG64 Reserved
Definition: ketypes.h:1105
ULONG64 P1Home
Definition: ketypes.h:1101
ULONG64 Return
Definition: ketypes.h:1106
ULONG64 P4Home
Definition: ketypes.h:1104
ULONG64 P3Home
Definition: ketypes.h:1103
ULONG64 Return
Definition: ketypes.h:1119
KIRQL ApcBypass
Definition: ketypes.h:1116
CHAR PreviousMode
Definition: ketypes.h:383
UINT64 Dr7
Definition: ketypes.h:417
USHORT SegSs
Definition: ketypes.h:457
UINT64 ExceptionFrame
Definition: ketypes.h:446
USHORT SegEs
Definition: ketypes.h:435
USHORT SegDs
Definition: ketypes.h:434
KTRAP_FRAME TrapFrame
Definition: thrdini.c:25
KSTART_FRAME StartFrame
Definition: thrdini.c:23
KEXCEPTION_FRAME ExceptionFrame
Definition: thrdini.c:24
KSWITCH_FRAME CtxSwitchFrame
Definition: thrdini.c:22
void * PVOID
Definition: typedefs.h:50
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG
Definition: typedefs.h:59
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
Definition: psfuncs.h:91

◆ KiInvalidSystemThreadStartupExit()

void KiInvalidSystemThreadStartupExit ( void  )

◆ KiServiceExit3()

void KiServiceExit3 ( void  )

◆ KiSwapContextResume()

BOOLEAN KiSwapContextResume ( _In_ BOOLEAN  ApcBypass,
_In_ PKTHREAD  OldThread,
_In_ PKTHREAD  NewThread 
)

Definition at line 147 of file thrdini.c.

151{
152 PKIPCR Pcr = (PKIPCR)KeGetPcr();
153 PKPROCESS OldProcess, NewProcess;
154
155 /* Setup ring 0 stack pointer */
156 Pcr->TssBase->Rsp0 = (ULONG64)NewThread->InitialStack; // FIXME: NPX save area?
157 Pcr->Prcb.RspBase = Pcr->TssBase->Rsp0;
158
159 /* Now we are the new thread. Check if it's in a new process */
160 OldProcess = OldThread->ApcState.Process;
161 NewProcess = NewThread->ApcState.Process;
162 if (OldProcess != NewProcess)
163 {
164 /* Switch address space and flush TLB */
165 __writecr3(NewProcess->DirectoryTableBase[0]);
166
167 /* Set new TSS fields */
168 //Pcr->TssBase->IoMapBase = NewProcess->IopmOffset;
169 }
170
171 /* Set TEB pointer and GS base */
172 Pcr->NtTib.Self = (PVOID)NewThread->Teb;
173 if (NewThread->Teb)
174 {
175 /* This will switch the usermode gs */
176 __writemsr(MSR_GS_SWAP, (ULONG64)NewThread->Teb);
177 }
178
179 /* Increase context switch count */
180 Pcr->ContextSwitches++;
181 NewThread->ContextSwitches++;
182
183 /* DPCs shouldn't be active */
184 if (Pcr->Prcb.DpcRoutineActive)
185 {
186 /* Crash the machine */
187 KeBugCheckEx(ATTEMPTED_SWITCH_FROM_DPC,
188 (ULONG_PTR)OldThread,
189 (ULONG_PTR)NewThread,
190 (ULONG_PTR)OldThread->InitialStack,
191 0);
192 }
193
194 /* Old thread os no longer busy */
195 OldThread->SwapBusy = FALSE;
196
197 /* Kernel APCs may be pending */
198 if (NewThread->ApcState.KernelApcPending)
199 {
200 /* Are APCs enabled? */
201 if ((NewThread->SpecialApcDisable == 0) &&
202 (ApcBypass == 0))
203 {
204 /* Return TRUE to indicate that we want APCs to be delivered */
205 return TRUE;
206 }
207
208 /* Request an APC interrupt to be delivered later */
210 }
211
212 /* Return stating that no kernel APCs are pending*/
213 return FALSE;
214}
#define FALSE
Definition: types.h:117
#define APC_LEVEL
Definition: env_spec_w32.h:695
VOID FASTCALL HalRequestSoftwareInterrupt(IN KIRQL Irql)
Definition: pic.c:271
PPC_QUAL void __writemsr(const unsigned long Value)
Definition: intrin_ppc.h:748
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1794
if(dx< 0)
Definition: linetemp.h:194
#define MSR_GS_SWAP
Definition: ketypes.h:254
struct _KIPCR * PKIPCR
#define KeGetPcr()
Definition: ketypes.h:81
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:108
ULONG ContextSwitches
Definition: ketypes.h:964
KPRCB Prcb
Definition: ketypes.h:961
struct _KTSS64 * TssBase
Definition: ketypes.h:933
NT_TIB NtTib
Definition: ketypes.h:929
UCHAR DpcRoutineActive
Definition: ketypes.h:757
UINT64 RspBase
Definition: ketypes.h:645
ULONG_PTR DirectoryTableBase
Definition: ketypes.h:2087
struct _NT_TIB * Self
Definition: compat.h:720
uint32_t ULONG_PTR
Definition: typedefs.h:65

◆ KiUserThreadStartupExit()

void KiUserThreadStartupExit ( void  )