1 #pragma once
2 
3 #include <section_attribs.h>
4 
5 
6 
7 #ifdef _NTOSKRNL_
8 
9 #ifndef _ARM_
10 #define KeGetCurrentThread  _KeGetCurrentThread
11 #define KeGetPreviousMode   _KeGetPreviousMode
12 #endif
13 #undef  PsGetCurrentProcess
14 #define PsGetCurrentProcess _PsGetCurrentProcess
15 
16 #define RVA(m, b) ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
17 
18 //
19 // We are very lazy on ARM -- we just import intrinsics
20 // Question: Why wasn't this done for x86 too? (see fastintrlck.asm)
21 //
22 #define InterlockedDecrement         _InterlockedDecrement
23 #define InterlockedDecrement16       _InterlockedDecrement16
24 #define InterlockedIncrement         _InterlockedIncrement
25 #define InterlockedIncrement16       _InterlockedIncrement16
26 #define InterlockedCompareExchange   _InterlockedCompareExchange
27 #define InterlockedCompareExchange16 _InterlockedCompareExchange16
28 #define InterlockedCompareExchange64 _InterlockedCompareExchange64
29 #define InterlockedExchange          _InterlockedExchange
30 #define InterlockedExchangeAdd       _InterlockedExchangeAdd
31 #define InterlockedOr                _InterlockedOr
32 #define InterlockedAnd               _InterlockedAnd
33 
34 //
35 // Use inlined versions of fast/guarded mutex routines
36 //
37 #define ExEnterCriticalRegionAndAcquireFastMutexUnsafe _ExEnterCriticalRegionAndAcquireFastMutexUnsafe
38 #define ExReleaseFastMutexUnsafeAndLeaveCriticalRegion _ExReleaseFastMutexUnsafeAndLeaveCriticalRegion
39 #define ExAcquireFastMutex _ExAcquireFastMutex
40 #define ExReleaseFastMutex _ExReleaseFastMutex
41 #define ExAcquireFastMutexUnsafe _ExAcquireFastMutexUnsafe
42 #define ExReleaseFastMutexUnsafe _ExReleaseFastMutexUnsafe
43 #define ExTryToAcquireFastMutex _ExTryToAcquireFastMutex
44 
45 #define KeInitializeGuardedMutex _KeInitializeGuardedMutex
46 #define KeAcquireGuardedMutex _KeAcquireGuardedMutex
47 #define KeReleaseGuardedMutex _KeReleaseGuardedMutex
48 #define KeAcquireGuardedMutexUnsafe _KeAcquireGuardedMutexUnsafe
49 #define KeReleaseGuardedMutexUnsafe _KeReleaseGuardedMutexUnsafe
50 #define KeTryToAcquireGuardedMutex _KeTryToAcquireGuardedMutex
51 
52 #include "tag.h"
53 #include "ke.h"
54 #include "ob.h"
55 #include "mm.h"
56 #include "ex.h"
57 #include "cm.h"
58 #include "ps.h"
59 #include "cc.h"
60 #include "io.h"
61 #include "po.h"
62 #include "se.h"
63 #include "ldr.h"
64 #ifndef _WINKD_
65 #include "kd.h"
66 #endif
67 #include "kd64.h"
68 #include "fsrtl.h"
69 #include "lpc.h"
70 #include "rtl.h"
71 #ifdef KDBG
72 #include <kdbg/kdb.h>
73 #endif
74 #include "dbgk.h"
75 #include "spinlock.h"
76 #include "test.h"
77 #include "inbv.h"
78 #include "vdm.h"
79 #include "hal.h"
80 #include "hdl.h"
81 #include "icif.h"
82 #include "arch/intrin_i.h"
83 #include <arbiter.h>
84 
85 /*
86  * Use IsPointerOffset to test whether a pointer should be interpreted as an offset
87  * or as a pointer
88  */
89 #if defined(_X86_) || defined(_M_AMD64) || defined(_MIPS_) || defined(_PPC_) || defined(_ARM_)
90 
91 /* for x86 and x86-64 the MSB is 1 so we can simply test on that */
92 #define IsPointerOffset(Ptr) ((LONG_PTR)(Ptr) >= 0)
93 
94 #elif defined(_IA64_)
95 
96 /* on Itanium if the 24 most significant bits are set, we're not dealing with
97    offsets anymore. */
98 #define IsPointerOffset(Ptr)  (((ULONG_PTR)(Ptr) & 0xFFFFFF0000000000ULL) == 0)
99 
100 #else
101 #error IsPointerOffset() needs to be defined for this architecture
102 #endif
103 
104 #endif
105 
106 #ifndef _WIN64
107 C_ASSERT(FIELD_OFFSET(KUSER_SHARED_DATA, SystemCall) == 0x300);
108 
109 C_ASSERT(FIELD_OFFSET(KTHREAD, InitialStack) == KTHREAD_INITIAL_STACK);
110 C_ASSERT(FIELD_OFFSET(KTHREAD, KernelStack) == KTHREAD_KERNEL_STACK);
111 C_ASSERT(FIELD_OFFSET(KTHREAD, SystemAffinityActive) == FIELD_OFFSET(KTHREAD, WaitBlock) + FIELD_OFFSET(KWAIT_BLOCK, SpareByte));
112 C_ASSERT(FIELD_OFFSET(KTHREAD, ApcState.Process) == KTHREAD_APCSTATE_PROCESS);
113 C_ASSERT(FIELD_OFFSET(KTHREAD, ApcQueueable) == FIELD_OFFSET(KTHREAD, ApcState.UserApcPending) + 1);
114 C_ASSERT(FIELD_OFFSET(KTHREAD, ApcQueueable) == 0x3F);
115 C_ASSERT(FIELD_OFFSET(KTHREAD, NextProcessor) == 0x40);
116 C_ASSERT(FIELD_OFFSET(KTHREAD, DeferredProcessor) == 0x41);
117 C_ASSERT(FIELD_OFFSET(KTHREAD, AdjustReason) == 0x42);
118 C_ASSERT(FIELD_OFFSET(KTHREAD, NpxState) == KTHREAD_NPX_STATE);
119 C_ASSERT(FIELD_OFFSET(KTHREAD, Alertable) == 0x58);
120 C_ASSERT(FIELD_OFFSET(KTHREAD, SwapBusy) == 0x05D);
121 C_ASSERT(FIELD_OFFSET(KTHREAD, Teb) == KTHREAD_TEB);
122 C_ASSERT(FIELD_OFFSET(KTHREAD, Timer) == 0x078);
123 C_ASSERT(FIELD_OFFSET(KTHREAD, ThreadFlags) == 0x0A0);
124 C_ASSERT(FIELD_OFFSET(KTHREAD, WaitBlock) == 0x0A8);
125 C_ASSERT(FIELD_OFFSET(KTHREAD, WaitBlockFill0) == 0x0A8);
126 C_ASSERT(FIELD_OFFSET(KTHREAD, QueueListEntry) == 0x108);
127 C_ASSERT(FIELD_OFFSET(KTHREAD, PreviousMode) == KTHREAD_PREVIOUS_MODE);
128 C_ASSERT(FIELD_OFFSET(KTHREAD, PreviousMode) == FIELD_OFFSET(KTHREAD, WaitBlock) + sizeof(KWAIT_BLOCK) + FIELD_OFFSET(KWAIT_BLOCK, SpareByte));
129 C_ASSERT(FIELD_OFFSET(KTHREAD, ResourceIndex) == FIELD_OFFSET(KTHREAD, WaitBlock) + 2*sizeof(KWAIT_BLOCK) + FIELD_OFFSET(KWAIT_BLOCK, SpareByte));
130 C_ASSERT(FIELD_OFFSET(KTHREAD, LargeStack) == FIELD_OFFSET(KTHREAD, WaitBlock) + 3*sizeof(KWAIT_BLOCK) + FIELD_OFFSET(KWAIT_BLOCK, SpareByte));
131 C_ASSERT(FIELD_OFFSET(KTHREAD, TrapFrame) == KTHREAD_TRAP_FRAME);
132 C_ASSERT(FIELD_OFFSET(KTHREAD, CallbackStack) == KTHREAD_CALLBACK_STACK);
133 C_ASSERT(FIELD_OFFSET(KTHREAD, ServiceTable) == KTHREAD_SERVICE_TABLE);
134 C_ASSERT(FIELD_OFFSET(KTHREAD, FreezeCount) == FIELD_OFFSET(KTHREAD, SavedApcState.UserApcPending) + 1);
135 C_ASSERT(FIELD_OFFSET(KTHREAD, Quantum) == FIELD_OFFSET(KTHREAD, SuspendApc.SpareByte0));
136 C_ASSERT(FIELD_OFFSET(KTHREAD, QuantumReset) == FIELD_OFFSET(KTHREAD, SuspendApc.SpareByte1));
137 C_ASSERT(FIELD_OFFSET(KTHREAD, KernelTime) == FIELD_OFFSET(KTHREAD, SuspendApc.SpareLong0));
138 C_ASSERT(FIELD_OFFSET(KTHREAD, TlsArray) == FIELD_OFFSET(KTHREAD, SuspendApc.SystemArgument1));
139 C_ASSERT(FIELD_OFFSET(KTHREAD, LegoData) == FIELD_OFFSET(KTHREAD, SuspendApc.SystemArgument2));
140 C_ASSERT(FIELD_OFFSET(KTHREAD, PowerState) == FIELD_OFFSET(KTHREAD, SuspendApc.Inserted) + 1);
141 C_ASSERT(sizeof(KTHREAD) == 0x1B8);
142 
143 C_ASSERT(FIELD_OFFSET(KPROCESS, DirectoryTableBase) == KPROCESS_DIRECTORY_TABLE_BASE);
144 
145 C_ASSERT(FIELD_OFFSET(KPCR, NtTib.ExceptionList) == KPCR_EXCEPTION_LIST);
146 C_ASSERT(FIELD_OFFSET(KPCR, SelfPcr) == KPCR_SELF);
147 C_ASSERT(FIELD_OFFSET(KPCR, IRR) == KPCR_IRR);
148 C_ASSERT(FIELD_OFFSET(KPCR, IDR) == KPCR_IDR);
149 C_ASSERT(FIELD_OFFSET(KPCR, Irql) == KPCR_IRQL);
150 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, CurrentThread) == KPCR_CURRENT_THREAD);
151 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, NextThread) == KPCR_PRCB_NEXT_THREAD);
152 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, NpxThread) == KPCR_NPX_THREAD);
153 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) == KPCR_PRCB_DATA);
154 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, KeSystemCalls) == KPCR_SYSTEM_CALLS);
155 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcData) + FIELD_OFFSET(KDPC_DATA, DpcQueueDepth) == KPCR_PRCB_DPC_QUEUE_DEPTH);
156 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcData) + 16 == KPCR_PRCB_DPC_COUNT);
157 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcStack) == KPCR_PRCB_DPC_STACK);
158 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, TimerRequest) == KPCR_PRCB_TIMER_REQUEST);
159 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, MaximumDpcQueueDepth) == KPCR_PRCB_MAXIMUM_DPC_QUEUE_DEPTH);
160 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcRequestRate) == KPCR_PRCB_DPC_REQUEST_RATE);
161 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcInterruptRequested) == KPCR_PRCB_DPC_INTERRUPT_REQUESTED);
162 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcRoutineActive) == KPCR_PRCB_DPC_ROUTINE_ACTIVE);
163 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcLastCount) == KPCR_PRCB_DPC_LAST_COUNT);
164 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, TimerRequest) == KPCR_PRCB_TIMER_REQUEST);
165 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, QuantumEnd) == KPCR_PRCB_QUANTUM_END);
166 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DeferredReadyListHead) == KPCR_PRCB_DEFERRED_READY_LIST_HEAD);
167 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, PowerState) == KPCR_PRCB_POWER_STATE_IDLE_FUNCTION);
168 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, PrcbLock) == KPCR_PRCB_PRCB_LOCK);
169 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, DpcStack) == KPCR_PRCB_DPC_STACK);
170 C_ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, IdleSchedule) == KPCR_PRCB_IDLE_SCHEDULE);
171 C_ASSERT(sizeof(FX_SAVE_AREA) == SIZEOF_FX_SAVE_AREA);
172 
173 /* Platform specific checks */
174 C_ASSERT(FIELD_OFFSET(KPROCESS, IopmOffset) == KPROCESS_IOPM_OFFSET);
175 C_ASSERT(FIELD_OFFSET(KPROCESS, LdtDescriptor) == KPROCESS_LDT_DESCRIPTOR0);
176 C_ASSERT(FIELD_OFFSET(KTSS, Esp0) == KTSS_ESP0);
177 C_ASSERT(FIELD_OFFSET(KTSS, IoMapBase) == KTSS_IOMAPBASE);
178 #endif
179