ReactOS 0.4.16-dev-1475-g410db43
stubs.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: stubs
5 * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
6 */
7
8/* INCLUDES ******************************************************************/
9
10#include <ntoskrnl.h>
11
12#define NDEBUG
13#include <debug.h>
14
15/* GLOBALS *******************************************************************/
16
19
22 LONG_PTR StackOffset,
23 PVOID OldStackBase);
24
25/*
26 * Kernel stack layout (example pointers):
27 * 0xFFFFFC0F'2D008000 KTHREAD::StackBase
28 * [XSAVE_AREA size == KeXStateLength = 0x440]
29 * 0xFFFFFC0F'2D007BC0 KTHREAD::StateSaveArea _XSAVE_FORMAT
30 * 0xFFFFFC0F'2D007B90 KTHREAD::InitialStack
31 * [0x190 bytes KTRAP_FRAME]
32 * 0xFFFFFC0F'2D007A00 KTHREAD::TrapFrame
33 * [KSTART_FRAME] or ...
34 * [KSWITCH_FRAME]
35 * 0xFFFFFC0F'2D007230 KTHREAD::KernelStack
36 */
37
40KiSwitchKernelStack(PVOID StackBase, PVOID StackLimit)
41{
42 PKTHREAD CurrentThread;
43 PVOID OldStackBase;
44 LONG_PTR StackOffset;
46 PKIPCR Pcr;
47 ULONG Eflags;
48
49 /* Get the current thread */
50 CurrentThread = KeGetCurrentThread();
51
52 /* Save the old stack base */
53 OldStackBase = CurrentThread->StackBase;
54
55 /* Get the size of the current stack */
56 StackSize = (ULONG_PTR)CurrentThread->StackBase - CurrentThread->StackLimit;
57 ASSERT(StackSize <= (ULONG_PTR)StackBase - (ULONG_PTR)StackLimit);
58
59 /* Copy the current stack contents to the new stack */
60 RtlCopyMemory((PUCHAR)StackBase - StackSize,
61 (PVOID)CurrentThread->StackLimit,
62 StackSize);
63
64 /* Calculate the offset between the old and the new stack */
65 StackOffset = (PUCHAR)StackBase - (PUCHAR)CurrentThread->StackBase;
66
67 /* Disable interrupts while messing with the stack */
68 Eflags = __readeflags();
69 _disable();
70
71 /* Set the new trap frame */
72 CurrentThread->TrapFrame = (PKTRAP_FRAME)Add2Ptr(CurrentThread->TrapFrame,
73 StackOffset);
74
75 /* Set the new initial stack */
76 CurrentThread->InitialStack = Add2Ptr(CurrentThread->InitialStack,
77 StackOffset);
78
79 /* Switch StateSaveArea */
80 CurrentThread->StateSaveArea = Add2Ptr(CurrentThread->StateSaveArea,
81 StackOffset);
82
83 /* Set the new stack limits */
84 CurrentThread->StackBase = StackBase;
85 CurrentThread->StackLimit = (ULONG_PTR)StackLimit;
86 CurrentThread->LargeStack = TRUE;
87
88 /* Adjust RspBase in the PCR */
89 Pcr = (PKIPCR)KeGetPcr();
90 Pcr->Prcb.RspBase += StackOffset;
91
92 /* Adjust Rsp0 in the TSS */
93 Pcr->TssBase->Rsp0 += StackOffset;
94
95 /* Restore interrupts */
96 __writeeflags(Eflags);
97
98 return OldStackBase;
99}
100
102VOID
104{
105 PKPRCB Prcb = KeGetCurrentPrcb();
106 PKTHREAD OldThread, NewThread;
107
108 /* Now loop forever */
109 while (TRUE)
110 {
111 /* Start of the idle loop: disable interrupts */
112 _enable();
115 _disable();
116
117 /* Check for pending timers, pending DPCs, or pending ready threads */
118 if ((Prcb->DpcData[0].DpcQueueDepth) ||
119 (Prcb->TimerRequest) ||
121 {
122 /* Quiesce the DPC software interrupt */
124
125 /* Handle it */
126 KiRetireDpcList(Prcb);
127 }
128
129 /* Check if a new thread is scheduled for execution */
130 if (Prcb->NextThread)
131 {
132 /* Enable interrupts */
133 _enable();
134
135 /* Capture current thread data */
136 OldThread = Prcb->CurrentThread;
137 NewThread = Prcb->NextThread;
138
139 /* Set new thread data */
140 Prcb->NextThread = NULL;
141 Prcb->CurrentThread = NewThread;
142
143 /* The thread is now running */
144 NewThread->State = Running;
145
146#ifdef CONFIG_SMP
147 /* Do the swap at SYNCH_LEVEL */
149#endif
150
151 /* Switch away from the idle thread */
152 KiSwapContext(APC_LEVEL, OldThread);
153
154#ifdef CONFIG_SMP
155 /* Go back to DISPATCH_LEVEL */
157#endif
158 }
159 else
160 {
161 /* Continue staying idle. Note the HAL returns with interrupts on */
162 Prcb->PowerState.IdleFunction(&Prcb->PowerState);
163 }
164 }
165}
166
167VOID
168NTAPI
170 IN PKPROCESS OldProcess)
171{
172 PKIPCR Pcr = (PKIPCR)KeGetPcr();
173
174#ifdef CONFIG_SMP
175 /* Update active processor mask */
176 InterlockedXor64((PLONG64)&NewProcess->ActiveProcessors, Pcr->Prcb.SetMember);
177 InterlockedXor64((PLONG64)&OldProcess->ActiveProcessors, Pcr->Prcb.SetMember);
178#endif
179
180 /* Update CR3 */
181 __writecr3(NewProcess->DirectoryTableBase[0]);
182
183 /* Update IOPM offset */
184 Pcr->TssBase->IoMapBase = NewProcess->IopmOffset;
185}
186
188NTAPI
189NtSetLdtEntries(ULONG Selector1, LDT_ENTRY LdtEntry1, ULONG Selector2, LDT_ENTRY LdtEntry2)
190{
192 __debugbreak();
193 return STATUS_UNSUCCESSFUL;
194}
195
197NTAPI
199 IN PVOID ControlData)
200{
201 /* Not supported */
203}
static ULONG StackSize
Definition: StackOverflow.c:19
LONG NTSTATUS
Definition: precomp.h:26
__int64 * PLONG64
Definition: basetsd.h:183
#define UNIMPLEMENTED
Definition: ntoskrnl.c:15
#define STATUS_NOT_IMPLEMENTED
Definition: d3dkmdt.h:42
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define ULONG_PTR
Definition: config.h:101
#define SYNCH_LEVEL
Definition: env_spec_w32.h:704
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
VOID FASTCALL HalClearSoftwareInterrupt(IN KIRQL Irql)
Definition: pic.c:282
#define KeGetCurrentThread
Definition: hal.h:55
#define InterlockedXor64
Definition: interlocked.h:306
void __cdecl _disable(void)
Definition: intrin_arm.h:365
void __cdecl _enable(void)
Definition: intrin_arm.h:373
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
__INTRIN_INLINE void __writeeflags(uintptr_t Value)
Definition: intrin_x86.h:1683
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1688
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1808
#define Add2Ptr(PTR, INC)
#define ASSERT(a)
Definition: mode.c:44
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1187
struct _KIPCR * PKIPCR
struct _KTRAP_FRAME * PKTRAP_FRAME
#define KeGetPcr()
Definition: ketypes.h:81
@ Running
Definition: ketypes.h:390
#define DECLSPEC_NORETURN
Definition: ntbasedef.h:179
BOOLEAN FASTCALL KiSwapContext(IN KIRQL WaitIrql, IN PKTHREAD CurrentThread)
VOID FASTCALL KiRetireDpcList(IN PKPRCB Prcb)
Definition: dpc.c:562
NTSTATUS NTAPI NtSetLdtEntries(ULONG Selector1, LDT_ENTRY LdtEntry1, ULONG Selector2, LDT_ENTRY LdtEntry2)
Definition: stubs.c:189
DECLSPEC_NORETURN VOID KiIdleLoop(VOID)
Definition: stubs.c:103
NTSTATUS NTAPI NtVdmControl(IN ULONG ControlCode, IN PVOID ControlData)
Definition: stubs.c:198
SIZE_T KeXStateLength
Definition: stubs.c:18
PVOID KiSwitchKernelStackHelper(LONG_PTR StackOffset, PVOID OldStackBase)
PVOID NTAPI KiSwitchKernelStack(PVOID StackBase, PVOID StackLimit)
Definition: stubs.c:40
ULONG ProcessCount
Definition: stubs.c:17
VOID NTAPI KiSwapProcess(IN PKPROCESS NewProcess, IN PKPROCESS OldProcess)
Definition: stubs.c:169
#define YieldProcessor
Definition: ke.h:48
volatile ULONG DpcQueueDepth
Definition: ketypes.h:983
KPRCB Prcb
Definition: ketypes.h:993
struct _KTSS64 * TssBase
Definition: ketypes.h:965
UINT64 TimerRequest
Definition: ketypes.h:789
PROCESSOR_POWER_STATE PowerState
Definition: ketypes.h:893
UINT64 RspBase
Definition: ketypes.h:674
KDPC_DATA DpcData[2]
Definition: ketypes.h:774
struct _KTHREAD * CurrentThread
Definition: ketypes.h:664
struct _KTHREAD * NextThread
Definition: ketypes.h:665
UINT64 SetMember
Definition: ketypes.h:676
SINGLE_LIST_ENTRY DeferredReadyListHead
Definition: ketypes.h:726
PKTRAP_FRAME TrapFrame
Definition: ketypes.h:1902
PVOID InitialStack
Definition: ketypes.h:1792
PVOID StackBase
Definition: ketypes.h:1794
XSAVE_FORMAT * StateSaveArea
Definition: ketypes.h:1804
volatile VOID * StackLimit
Definition: ketypes.h:1793
volatile UCHAR State
Definition: ketypes.h:1917
Definition: compat.h:777
PPROCESSOR_IDLE_FUNCTION IdleFunction
Definition: potypes.h:68
struct _SINGLE_LIST_ENTRY * Next
Definition: ntbasedef.h:641
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
XSAVE_FORMAT
Definition: ketypes.h:966