ReactOS  0.4.15-dev-3297-g037c744
stubs.c File Reference
#include <ntoskrnl.h>
#include <fltkernel.h>
#include <debug.h>
Include dependency graph for stubs.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define MAX_SYSCALL_PARAMS   16
 

Functions

VOID KiRetireDpcListInDpcStack (PKPRCB Prcb, PVOID DpcStack)
 
NTSTATUS KiConvertToGuiThread (VOID)
 
 _Requires_lock_not_held_ (Prcb->PrcbLock) VOID NTAPI KiDpcInterruptHandler(VOID)
 
PVOID KiSwitchKernelStackHelper (LONG_PTR StackOffset, PVOID OldStackBase)
 
PVOID NTAPI KiSwitchKernelStack (PVOID StackBase, PVOID StackLimit)
 
DECLSPEC_NORETURN VOID KiIdleLoop (VOID)
 
VOID NTAPI KiSwapProcess (IN PKPROCESS NewProcess, IN PKPROCESS OldProcess)
 
NTSTATUS NtSyscallFailure (void)
 
PVOID KiSystemCallHandler (VOID)
 
VOID KiSystemService (IN PKTHREAD Thread, IN PKTRAP_FRAME TrapFrame, IN ULONG Instruction)
 
NTSTATUS NTAPI NtSetLdtEntries (ULONG Selector1, LDT_ENTRY LdtEntry1, ULONG Selector2, LDT_ENTRY LdtEntry2)
 
NTSTATUS NTAPI NtVdmControl (IN ULONG ControlCode, IN PVOID ControlData)
 

Variables

ULONG ProcessCount
 
SIZE_T KeXStateLength = sizeof(XSAVE_FORMAT)
 

Macro Definition Documentation

◆ MAX_SYSCALL_PARAMS

#define MAX_SYSCALL_PARAMS   16

Definition at line 245 of file stubs.c.

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file stubs.c.

Function Documentation

◆ _Requires_lock_not_held_()

_Requires_lock_not_held_ ( Prcb->  PrcbLock)

Definition at line 28 of file stubs.c.

32 {
33  PKPRCB Prcb = KeGetCurrentPrcb();
34  PKTHREAD NewThread, OldThread;
35  KIRQL OldIrql;
36 
37  /* Raise to DISPATCH_LEVEL */
39 
40  /* Send an EOI */
41  KiSendEOI();
42 
43  /* Check for pending timers, pending DPCs, or pending ready threads */
44  if ((Prcb->DpcData[0].DpcQueueDepth) ||
45  (Prcb->TimerRequest) ||
47  {
48  /* Retire DPCs while under the DPC stack */
50  }
51 
52  /* Enable interrupts */
53  _enable();
54 
55  /* Check for quantum end */
56  if (Prcb->QuantumEnd)
57  {
58  /* Handle quantum end */
59  Prcb->QuantumEnd = FALSE;
60  KiQuantumEnd();
61  }
62  else if (Prcb->NextThread)
63  {
64  /* Acquire the PRCB lock */
65  KiAcquirePrcbLock(Prcb);
66 
67  /* Capture current thread data */
68  OldThread = Prcb->CurrentThread;
69  NewThread = Prcb->NextThread;
70 
71  /* Set new thread data */
72  Prcb->NextThread = NULL;
73  Prcb->CurrentThread = NewThread;
74 
75  /* The thread is now running */
76  NewThread->State = Running;
77  OldThread->WaitReason = WrDispatchInt;
78 
79  /* Make the old thread ready */
80  KxQueueReadyThread(OldThread, Prcb);
81 
82  /* Swap to the new thread */
83  KiSwapContext(APC_LEVEL, OldThread);
84  }
85 
86  /* Disable interrupts and go back to old irql */
87  _disable();
89 }
KDPC_DATA DpcData[2]
Definition: ketypes.h:680
void __cdecl _enable(void)
Definition: intrin_arm.h:373
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1079
VOID KiRetireDpcListInDpcStack(PKPRCB Prcb, PVOID DpcStack)
VOID NTAPI KiQuantumEnd(VOID)
UCHAR QuantumEnd
Definition: ketypes.h:700
struct _KTHREAD * NextThread
Definition: ketypes.h:571
BOOLEAN FASTCALL KiSwapContext(IN KIRQL WaitIrql, IN PKTHREAD CurrentThread)
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define FALSE
Definition: types.h:117
struct _KTHREAD * CurrentThread
Definition: ketypes.h:570
FORCEINLINE VOID KiAcquirePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:220
UCHAR WaitReason
Definition: ketypes.h:1896
KIRQL OldIrql
Definition: mm.h:1502
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
UINT64 TimerRequest
Definition: ketypes.h:695
struct _SINGLE_LIST_ENTRY * Next
Definition: ntbasedef.h:629
FORCEINLINE VOID KiSendEOI(VOID)
Definition: ke.h:342
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
SINGLE_LIST_ENTRY DeferredReadyListHead
Definition: ketypes.h:632
volatile ULONG DpcQueueDepth
Definition: ketypes.h:788
PVOID DpcStack
Definition: ketypes.h:681
#define NULL
Definition: types.h:112
volatile UCHAR State
Definition: ketypes.h:1721
void __cdecl _disable(void)
Definition: intrin_arm.h:365
VOID NTAPI KeLowerIrql(KIRQL NewIrql)
Definition: spinlock.c:39
#define APC_LEVEL
Definition: env_spec_w32.h:695

◆ KiConvertToGuiThread()

NTSTATUS KiConvertToGuiThread ( VOID  )

Referenced by KiSystemCallHandler().

◆ KiIdleLoop()

DECLSPEC_NORETURN VOID KiIdleLoop ( VOID  )

Definition at line 165 of file stubs.c.

166 {
167  PKPRCB Prcb = KeGetCurrentPrcb();
168  PKTHREAD OldThread, NewThread;
169 
170  /* Now loop forever */
171  while (TRUE)
172  {
173  /* Start of the idle loop: disable interrupts */
174  _enable();
175  YieldProcessor();
176  YieldProcessor();
177  _disable();
178 
179  /* Check for pending timers, pending DPCs, or pending ready threads */
180  if ((Prcb->DpcData[0].DpcQueueDepth) ||
181  (Prcb->TimerRequest) ||
182  (Prcb->DeferredReadyListHead.Next))
183  {
184  /* Quiesce the DPC software interrupt */
186 
187  /* Handle it */
188  KiRetireDpcList(Prcb);
189  }
190 
191  /* Check if a new thread is scheduled for execution */
192  if (Prcb->NextThread)
193  {
194  /* Enable interrupts */
195  _enable();
196 
197  /* Capture current thread data */
198  OldThread = Prcb->CurrentThread;
199  NewThread = Prcb->NextThread;
200 
201  /* Set new thread data */
202  Prcb->NextThread = NULL;
203  Prcb->CurrentThread = NewThread;
204 
205  /* The thread is now running */
206  NewThread->State = Running;
207 
208  /* Do the swap at SYNCH_LEVEL */
210 
211  /* Switch away from the idle thread */
212  KiSwapContext(APC_LEVEL, OldThread);
213 
214  /* Go back to DISPATCH_LEVEL */
216  }
217  else
218  {
219  /* Continue staying idle. Note the HAL returns with interrupts on */
220  Prcb->PowerState.IdleFunction(&Prcb->PowerState);
221  }
222  }
223 }
KDPC_DATA DpcData[2]
Definition: ketypes.h:680
#define TRUE
Definition: types.h:120
void __cdecl _enable(void)
Definition: intrin_arm.h:373
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1079
PROCESSOR_POWER_STATE PowerState
Definition: ketypes.h:799
struct _KTHREAD * NextThread
Definition: ketypes.h:571
BOOLEAN FASTCALL KiSwapContext(IN KIRQL WaitIrql, IN PKTHREAD CurrentThread)
FORCEINLINE VOID YieldProcessor(VOID)
Definition: ke.h:32
struct _KTHREAD * CurrentThread
Definition: ketypes.h:570
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
UINT64 TimerRequest
Definition: ketypes.h:695
struct _SINGLE_LIST_ENTRY * Next
Definition: ntbasedef.h:629
#define SYNCH_LEVEL
Definition: env_spec_w32.h:704
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
SINGLE_LIST_ENTRY DeferredReadyListHead
Definition: ketypes.h:632
volatile ULONG DpcQueueDepth
Definition: ketypes.h:788
#define NULL
Definition: types.h:112
PPROCESSOR_IDLE_FUNCTION IdleFunction
Definition: potypes.h:68
VOID FASTCALL HalClearSoftwareInterrupt(IN KIRQL Irql)
Definition: pic.c:282
volatile UCHAR State
Definition: ketypes.h:1721
void __cdecl _disable(void)
Definition: intrin_arm.h:365
VOID NTAPI KeLowerIrql(KIRQL NewIrql)
Definition: spinlock.c:39
#define APC_LEVEL
Definition: env_spec_w32.h:695
VOID FASTCALL KiRetireDpcList(IN PKPRCB Prcb)
Definition: dpc.c:562

◆ KiRetireDpcListInDpcStack()

VOID KiRetireDpcListInDpcStack ( PKPRCB  Prcb,
PVOID  DpcStack 
)

◆ KiSwapProcess()

VOID NTAPI KiSwapProcess ( IN PKPROCESS  NewProcess,
IN PKPROCESS  OldProcess 
)

Definition at line 227 of file stubs.c.

229 {
230  PKIPCR Pcr = (PKIPCR)KeGetPcr();
231 
232 #ifdef CONFIG_SMP
233  /* Update active processor mask */
234  InterlockedXor64((PLONG64)&NewProcess->ActiveProcessors, Pcr->Prcb.SetMember);
235  InterlockedXor64((PLONG64)&OldProcess->ActiveProcessors, Pcr->Prcb.SetMember);
236 #endif
237 
238  /* Update CR3 */
239  __writecr3(NewProcess->DirectoryTableBase[0]);
240 
241  /* Update IOPM offset */
242  Pcr->TssBase->IoMapBase = NewProcess->IopmOffset;
243 }
struct _KIPCR * PKIPCR
#define KeGetPcr()
Definition: ke.h:26
struct _KTSS64 * TssBase
Definition: ketypes.h:865
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1795
#define InterlockedXor64
Definition: interlocked.h:291
KPRCB Prcb
Definition: ketypes.h:893
UINT64 SetMember
Definition: ketypes.h:582
__int64 * PLONG64
Definition: basetsd.h:185

◆ KiSwitchKernelStack()

PVOID NTAPI KiSwitchKernelStack ( PVOID  StackBase,
PVOID  StackLimit 
)

Definition at line 111 of file stubs.c.

112 {
113  PKTHREAD CurrentThread;
114  PVOID OldStackBase;
115  LONG_PTR StackOffset;
117  PKIPCR Pcr;
118 
119  /* Get the current thread */
120  CurrentThread = KeGetCurrentThread();
121 
122  /* Save the old stack base */
123  OldStackBase = CurrentThread->StackBase;
124 
125  /* Get the size of the current stack */
126  StackSize = (ULONG_PTR)CurrentThread->StackBase - CurrentThread->StackLimit;
127  ASSERT(StackSize <= (ULONG_PTR)StackBase - (ULONG_PTR)StackLimit);
128 
129  /* Copy the current stack contents to the new stack */
130  RtlCopyMemory((PUCHAR)StackBase - StackSize,
131  (PVOID)CurrentThread->StackLimit,
132  StackSize);
133 
134  /* Calculate the offset between the old and the new stack */
135  StackOffset = (PUCHAR)StackBase - (PUCHAR)CurrentThread->StackBase;
136 
137  /* Disable interrupts while messing with the stack */
138  _disable();
139 
140  /* Set the new trap frame */
141  CurrentThread->TrapFrame = (PKTRAP_FRAME)Add2Ptr(CurrentThread->TrapFrame,
142  StackOffset);
143 
144  /* Set the new initial stack */
145  CurrentThread->InitialStack = Add2Ptr(CurrentThread->InitialStack,
146  StackOffset);
147 
148  /* Set the new stack limits */
149  CurrentThread->StackBase = StackBase;
150  CurrentThread->StackLimit = (ULONG_PTR)StackLimit;
151  CurrentThread->LargeStack = TRUE;
152 
153  /* Adjust RspBase in the PCR */
154  Pcr = (PKIPCR)KeGetPcr();
155  Pcr->Prcb.RspBase += StackOffset;
156 
157  /* Adjust Rsp0 in the TSS */
158  Pcr->TssBase->Rsp0 += StackOffset;
159 
160  return OldStackBase;
161 }
#define Add2Ptr(PTR, INC)
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
struct _KIPCR * PKIPCR
PKTRAP_FRAME TrapFrame
Definition: ketypes.h:1706
#define KeGetPcr()
Definition: ke.h:26
struct _KTSS64 * TssBase
Definition: ketypes.h:865
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define ASSERT(a)
Definition: mode.c:44
volatile VOID * StackLimit
Definition: ketypes.h:1597
KPRCB Prcb
Definition: ketypes.h:893
_In_ USHORT _In_ CCHAR StackSize
Definition: iofuncs.h:1058
PVOID StackBase
Definition: ketypes.h:1598
ULONG_PTR SIZE_T
Definition: typedefs.h:80
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
void __cdecl _disable(void)
Definition: intrin_arm.h:365
#define ULONG_PTR
Definition: config.h:101
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
struct _KTRAP_FRAME * PKTRAP_FRAME
#define KeGetCurrentThread
Definition: hal.h:55
PVOID InitialStack
Definition: ketypes.h:1596
UINT64 RspBase
Definition: ketypes.h:580

◆ KiSwitchKernelStackHelper()

PVOID KiSwitchKernelStackHelper ( LONG_PTR  StackOffset,
PVOID  OldStackBase 
)

◆ KiSystemCallHandler()

PVOID KiSystemCallHandler ( VOID  )

Definition at line 255 of file stubs.c.

257 {
258  PKTRAP_FRAME TrapFrame;
259  PKSERVICE_TABLE_DESCRIPTOR DescriptorTable;
261  PULONG64 KernelParams, UserParams;
262  ULONG ServiceNumber, Offset, Count;
263  ULONG64 UserRsp;
264 
265  /* Get a pointer to the trap frame */
267 
268  /* Increase system call count */
269  __addgsdword(FIELD_OFFSET(KIPCR, Prcb.KeSystemCalls), 1);
270 
271  /* Get the current thread */
273 
274  /* Set previous mode */
275  Thread->PreviousMode = TrapFrame->PreviousMode = UserMode;
276 
277  /* Save the old trap frame and set the new */
278  TrapFrame->TrapFrame = (ULONG64)Thread->TrapFrame;
279  Thread->TrapFrame = TrapFrame;
280 
281  /* We don't have an exception frame yet */
282  TrapFrame->ExceptionFrame = 0;
283 
284  /* Before enabling interrupts get the user rsp from the KPCR */
285  UserRsp = __readgsqword(FIELD_OFFSET(KIPCR, UserRsp));
286  TrapFrame->Rsp = UserRsp;
287 
288  /* Enable interrupts */
289  _enable();
290 
291  /* If the usermode rsp was not a usermode address, prepare an exception */
292  if (UserRsp > MmUserProbeAddress) UserRsp = MmUserProbeAddress;
293 
294  /* Get the address of the usermode and kernelmode parameters */
295  UserParams = (PULONG64)UserRsp + 1;
296  KernelParams = (PULONG64)TrapFrame - MAX_SYSCALL_PARAMS;
297 
298  /* Get the system call number from the trap frame and decode it */
299  ServiceNumber = (ULONG)TrapFrame->Rax;
300  Offset = (ServiceNumber >> SERVICE_TABLE_SHIFT) & SERVICE_TABLE_MASK;
301  ServiceNumber &= SERVICE_NUMBER_MASK;
302 
303  /* Check for win32k system calls */
305  {
306  ULONG GdiBatchCount;
307 
308  /* Read the GDI batch count from the TEB */
309  _SEH2_TRY
310  {
311  GdiBatchCount = NtCurrentTeb()->GdiBatchCount;
312  }
314  {
315  GdiBatchCount = 0;
316  }
317  _SEH2_END;
318 
319  /* Flush batch, if there are entries */
320  if (GdiBatchCount != 0)
321  {
323  }
324  }
325 
326  /* Get descriptor table */
327  DescriptorTable = (PVOID)((ULONG_PTR)Thread->ServiceTable + Offset);
328 
329  /* Validate the system call number */
330  if (ServiceNumber >= DescriptorTable->Limit)
331  {
332  /* Check if this is a GUI call */
333  if (!(Offset & SERVICE_TABLE_TEST))
334  {
335  /* Fail the call */
336  TrapFrame->Rax = STATUS_INVALID_SYSTEM_SERVICE;
337  return (PVOID)NtSyscallFailure;
338  }
339 
340  /* Convert us to a GUI thread
341  To be entirely correct. we return KiConvertToGuiThread,
342  which allocates a new stack, switches to it, calls
343  PsConvertToGuiThread and resumes in the middle of
344  KiSystemCallEntry64 to restart the system call handling. */
345  return (PVOID)KiConvertToGuiThread;
346  }
347 
348  /* Get stack bytes and calculate argument count */
349  Count = DescriptorTable->Number[ServiceNumber] / 8;
350 
351  _SEH2_TRY
352  {
353  switch (Count)
354  {
355  case 16: KernelParams[15] = UserParams[15];
356  case 15: KernelParams[14] = UserParams[14];
357  case 14: KernelParams[13] = UserParams[13];
358  case 13: KernelParams[12] = UserParams[12];
359  case 12: KernelParams[11] = UserParams[11];
360  case 11: KernelParams[10] = UserParams[10];
361  case 10: KernelParams[9] = UserParams[9];
362  case 9: KernelParams[8] = UserParams[8];
363  case 8: KernelParams[7] = UserParams[7];
364  case 7: KernelParams[6] = UserParams[6];
365  case 6: KernelParams[5] = UserParams[5];
366  case 5: KernelParams[4] = UserParams[4];
367  case 4:
368  case 3:
369  case 2:
370  case 1:
371  case 0:
372  break;
373 
374  default:
375  ASSERT(FALSE);
376  break;
377  }
378  }
380  {
381  TrapFrame->Rax = _SEH2_GetExceptionCode();
382  return (PVOID)NtSyscallFailure;
383  }
384  _SEH2_END;
385 
386  return (PVOID)DescriptorTable->Base[ServiceNumber];
387 }
ULONG MmUserProbeAddress
Definition: init.c:50
#define SERVICE_TABLE_SHIFT
Definition: ketypes.h:71
#define SERVICE_TABLE_MASK
Definition: ketypes.h:78
void __cdecl _enable(void)
Definition: intrin_arm.h:373
void * _AddressOfReturnAddress(void)
NTSTATUS KiConvertToGuiThread(VOID)
#define SERVICE_NUMBER_MASK
Definition: ketypes.h:83
_SEH2_TRY
Definition: create.c:4226
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define FALSE
Definition: types.h:117
UINT64 Rax
Definition: ketypes.h:322
#define SERVICE_TABLE_TEST
Definition: ketypes.h:90
void * PVOID
Definition: retypes.h:9
NTSTATUS NtSyscallFailure(void)
Definition: stubs.c:248
int Count
Definition: noreturn.cpp:7
#define ASSERT(a)
Definition: mode.c:44
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define MAX_SYSCALL_PARAMS
Definition: stubs.c:245
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2652
unsigned __int64 ULONG64
Definition: imports.h:198
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_SEH2_END
Definition: create.c:4400
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
PGDI_BATCHFLUSH_ROUTINE KeGdiFlushUserBatch
Definition: win32.c:20
UINT64 Rsp
Definition: ketypes.h:390
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
UINT64 ExceptionFrame
Definition: ketypes.h:380
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_INVALID_SYSTEM_SERVICE
Definition: ntstatus.h:265
unsigned __int64 * PULONG64
Definition: imports.h:198
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
struct _KTRAP_FRAME * PKTRAP_FRAME
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define KeGetCurrentThread
Definition: hal.h:55
UINT64 TrapFrame
Definition: ketypes.h:372
CHAR PreviousMode
Definition: ketypes.h:317

◆ KiSystemService()

VOID KiSystemService ( IN PKTHREAD  Thread,
IN PKTRAP_FRAME  TrapFrame,
IN ULONG  Instruction 
)

Definition at line 392 of file stubs.c.

395 {
397  __debugbreak();
398 }
#define UNIMPLEMENTED
Definition: stubs.c:20
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698

◆ NtSetLdtEntries()

NTSTATUS NTAPI NtSetLdtEntries ( ULONG  Selector1,
LDT_ENTRY  LdtEntry1,
ULONG  Selector2,
LDT_ENTRY  LdtEntry2 
)

Definition at line 403 of file stubs.c.

404 {
406  __debugbreak();
407  return STATUS_UNSUCCESSFUL;
408 }
#define UNIMPLEMENTED
Definition: stubs.c:20
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132

Referenced by init_funcs().

◆ NtSyscallFailure()

NTSTATUS NtSyscallFailure ( void  )

Definition at line 248 of file stubs.c.

249 {
250  /* This is the failure function */
251  return (NTSTATUS)KeGetCurrentThread()->TrapFrame->Rax;
252 }
LONG NTSTATUS
Definition: precomp.h:26
#define KeGetCurrentThread
Definition: hal.h:55

Referenced by KiSystemCallHandler().

◆ NtVdmControl()

NTSTATUS NTAPI NtVdmControl ( IN ULONG  ControlCode,
IN PVOID  ControlData 
)

Definition at line 412 of file stubs.c.

414 {
415  /* Not supported */
416  return STATUS_NOT_IMPLEMENTED;
417 }
return STATUS_NOT_IMPLEMENTED

Referenced by KiEnterV86Mode().

Variable Documentation

◆ KeXStateLength

SIZE_T KeXStateLength = sizeof(XSAVE_FORMAT)

Definition at line 17 of file stubs.c.

◆ ProcessCount

ULONG ProcessCount

Definition at line 16 of file stubs.c.