ReactOS 0.4.15-dev-7842-g558ab78
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 /* Set the new stack limits */
80 CurrentThread->StackBase = StackBase;
81 CurrentThread->StackLimit = (ULONG_PTR)StackLimit;
82 CurrentThread->LargeStack = TRUE;
83
84 /* Adjust RspBase in the PCR */
85 Pcr = (PKIPCR)KeGetPcr();
86 Pcr->Prcb.RspBase += StackOffset;
87
88 /* Adjust Rsp0 in the TSS */
89 Pcr->TssBase->Rsp0 += StackOffset;
90
91 /* Restore interrupts */
92 __writeeflags(Eflags);
93
94 return OldStackBase;
95}
96
98VOID
100{
101 PKPRCB Prcb = KeGetCurrentPrcb();
102 PKTHREAD OldThread, NewThread;
103
104 /* Now loop forever */
105 while (TRUE)
106 {
107 /* Start of the idle loop: disable interrupts */
108 _enable();
111 _disable();
112
113 /* Check for pending timers, pending DPCs, or pending ready threads */
114 if ((Prcb->DpcData[0].DpcQueueDepth) ||
115 (Prcb->TimerRequest) ||
117 {
118 /* Quiesce the DPC software interrupt */
120
121 /* Handle it */
122 KiRetireDpcList(Prcb);
123 }
124
125 /* Check if a new thread is scheduled for execution */
126 if (Prcb->NextThread)
127 {
128 /* Enable interrupts */
129 _enable();
130
131 /* Capture current thread data */
132 OldThread = Prcb->CurrentThread;
133 NewThread = Prcb->NextThread;
134
135 /* Set new thread data */
136 Prcb->NextThread = NULL;
137 Prcb->CurrentThread = NewThread;
138
139 /* The thread is now running */
140 NewThread->State = Running;
141
142#ifdef CONFIG_SMP
143 /* Do the swap at SYNCH_LEVEL */
145#endif
146
147 /* Switch away from the idle thread */
148 KiSwapContext(APC_LEVEL, OldThread);
149
150#ifdef CONFIG_SMP
151 /* Go back to DISPATCH_LEVEL */
153#endif
154 }
155 else
156 {
157 /* Continue staying idle. Note the HAL returns with interrupts on */
158 Prcb->PowerState.IdleFunction(&Prcb->PowerState);
159 }
160 }
161}
162
163VOID
164NTAPI
166 IN PKPROCESS OldProcess)
167{
168 PKIPCR Pcr = (PKIPCR)KeGetPcr();
169
170#ifdef CONFIG_SMP
171 /* Update active processor mask */
172 InterlockedXor64((PLONG64)&NewProcess->ActiveProcessors, Pcr->Prcb.SetMember);
173 InterlockedXor64((PLONG64)&OldProcess->ActiveProcessors, Pcr->Prcb.SetMember);
174#endif
175
176 /* Update CR3 */
177 __writecr3(NewProcess->DirectoryTableBase[0]);
178
179 /* Update IOPM offset */
180 Pcr->TssBase->IoMapBase = NewProcess->IopmOffset;
181}
182
184NTAPI
185NtSetLdtEntries(ULONG Selector1, LDT_ENTRY LdtEntry1, ULONG Selector2, LDT_ENTRY LdtEntry2)
186{
188 __debugbreak();
189 return STATUS_UNSUCCESSFUL;
190}
191
193NTAPI
195 IN PVOID ControlData)
196{
197 /* Not supported */
199}
static ULONG StackSize
Definition: StackOverflow.c:19
LONG NTSTATUS
Definition: precomp.h:26
__int64 * PLONG64
Definition: basetsd.h:183
#define UNIMPLEMENTED
Definition: debug.h:115
#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:291
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:1669
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1674
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1794
#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:1148
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:176
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:185
DECLSPEC_NORETURN VOID KiIdleLoop(VOID)
Definition: stubs.c:99
NTSTATUS NTAPI NtVdmControl(IN ULONG ControlCode, IN PVOID ControlData)
Definition: stubs.c:194
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:165
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:239
#define YieldProcessor
Definition: ke.h:48
volatile ULONG DpcQueueDepth
Definition: ketypes.h:858
KPRCB Prcb
Definition: ketypes.h:962
struct _KTSS64 * TssBase
Definition: ketypes.h:934
PROCESSOR_POWER_STATE PowerState
Definition: ketypes.h:865
UINT64 SetMember
Definition: ketypes.h:648
UINT64 RspBase
Definition: ketypes.h:646
KDPC_DATA DpcData[2]
Definition: ketypes.h:746
struct _KTHREAD * CurrentThread
Definition: ketypes.h:636
struct _KTHREAD * NextThread
Definition: ketypes.h:637
UINT64 TimerRequest
Definition: ketypes.h:761
SINGLE_LIST_ENTRY DeferredReadyListHead
Definition: ketypes.h:698
PKTRAP_FRAME TrapFrame
Definition: ketypes.h:1774
PVOID InitialStack
Definition: ketypes.h:1664
PVOID StackBase
Definition: ketypes.h:1666
volatile VOID * StackLimit
Definition: ketypes.h:1665
volatile UCHAR State
Definition: ketypes.h:1789
Definition: compat.h:777
PPROCESSOR_IDLE_FUNCTION IdleFunction
Definition: potypes.h:68
struct _SINGLE_LIST_ENTRY * Next
Definition: ntbasedef.h:629
#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:963