ReactOS 0.4.17-dev-1343-g8fe3cee
ke.h
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: ARM64 kernel definitions
5 * COPYRIGHT: Copyright 2026 Justin Miller <justin.miller@reactos.org>
6 */
7
8#pragma once
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14#ifndef __ASM__
15
16#include "intrin_i.h"
17
18//
19// brk #0xF000
20//
21#define KD_BREAKPOINT_TYPE ULONG
22#define KD_BREAKPOINT_SIZE sizeof(ULONG)
23#define KD_BREAKPOINT_VALUE 0xD43E0000
24
25/* Exception classes from ESR_EL1 */
26#define ESR_EC_SHIFT 26
27#define ESR_EC_UNKNOWN 0x00
28#define ESR_EC_WFX 0x01
29#define ESR_EC_FP_ACCESS 0x07
30#define ESR_EC_ILLEGAL_STATE 0x0E
31#define ESR_EC_SVC64 0x15
32#define ESR_EC_SYS64 0x18
33#define ESR_EC_SVE 0x19
34#define ESR_EC_IABT_LOWER 0x20
35#define ESR_EC_IABT_CURRENT 0x21
36#define ESR_EC_PC_ALIGN 0x22
37#define ESR_EC_DABT_LOWER 0x24
38#define ESR_EC_DABT_CURRENT 0x25
39#define ESR_EC_SP_ALIGN 0x26
40#define ESR_EC_FP_EXCEPTION 0x2C
41#define ESR_EC_SERROR 0x2F
42#define ESR_EC_BREAKPOINT_LOWER 0x30
43#define ESR_EC_BREAKPOINT_CURRENT 0x31
44#define ESR_EC_STEP_LOWER 0x32
45#define ESR_EC_STEP_CURRENT 0x33
46#define ESR_EC_WATCHPOINT_LOWER 0x34
47#define ESR_EC_WATCHPOINT_CURRENT 0x35
48#define ESR_EC_BRK64 0x3C
49
50/* Data and instruction abort syndrome */
51#define ESR_ISS_WNR 0x40
52#define ESR_ISS_FSC_MASK 0x3F
53
54/* Trap types passed from the vectors to KiArm64TrapDispatch */
55#define KI_TRAP_SYNCHRONOUS 0
56#define KI_TRAP_IRQ 1
57#define KI_TRAP_FIQ 2
58#define KI_TRAP_SERROR 3
59
60//
61// One-liners for getting and setting special purpose registers in portable code
62//
67{
68 return Context->Pc;
69}
70
72VOID
75 _In_ ULONG_PTR ProgramCounter)
76{
77 Context->Pc = ProgramCounter;
78}
79
84{
85 return Context->X0;
86}
87
89VOID
93{
94 Context->X0 = ReturnValue;
95}
96
101{
102 return Context->Sp;
103}
104
109{
110 return Context->Fp;
111}
112
114VOID
117 _In_ ULONG_PTR Frame)
118{
119 Context->Fp = Frame;
120}
121
125 _In_ PKTRAP_FRAME TrapFrame)
126{
127 return TrapFrame->Pc;
128}
129
133 _In_ PKTRAP_FRAME TrapFrame)
134{
135 return (PKTRAP_FRAME)TrapFrame->TrapFrame;
136}
137
141 _In_ PKTRAP_FRAME TrapFrame)
142{
143 return TrapFrame->Sp;
144}
145
149 _In_ PKTRAP_FRAME TrapFrame)
150{
151 return TrapFrame->Fp;
152}
153
157{
158 return (ULONG64)_ReadStatusReg(ARM64_CNTVCT);
159}
160
161//
162// Macro to get trap and exception frame from a thread stack
163//
164#define KeGetTrapFrame(Thread) \
165 ((PKTRAP_FRAME)((ULONG_PTR)((Thread)->InitialStack) - \
166 sizeof(KTRAP_FRAME)))
167
168#define KeGetExceptionFrame(Thread) \
169 (PKEXCEPTION_FRAME)((ULONG_PTR)KeGetTrapFrame(Thread) - \
170 sizeof(KEXCEPTION_FRAME))
171
172//
173// Macro to get context switches from the PRCB
174// All architectures but x86 have it in the PRCB's KeContextSwitches
175//
176#define KeGetContextSwitches(Prcb) \
177 ((Prcb)->KeContextSwitches)
178
179//
180// Macro to get the second level cache size field name which differs between
181// CISC and RISC architectures, as the former has unified I/D cache
182//
183#define KiGetSecondLevelDCacheSize() ((PKIPCR)KeGetPcr())->SecondLevelCacheSize
184
185//
186// Returns the Interrupt State from a Trap Frame.
187// ON = TRUE, OFF = FALSE
188//
189#define KeGetTrapFrameInterruptState(TrapFrame) \
190 (((TrapFrame)->Spsr & ARM64_PSR_I) == 0)
191
192/* Disable interrupts and return whether they were enabled before */
196{
197 ULONG64 Daif;
198
200 _disable();
201 return (Daif & ARM64_PSR_I) ? FALSE : TRUE;
202}
203
204/* Restore previous interrupt state */
206VOID
208 _In_ BOOLEAN WereEnabled)
209{
210 if (WereEnabled)
211 _enable();
212}
213
215VOID
218{
220}
221
223VOID
225{
227}
228
230VOID
233 _In_ SIZE_T FlushSize)
234{
236}
237
239VOID
242{
244}
245
249 _In_ PKTRAP_FRAME TrapFrame)
250{
251 return ((TrapFrame->Spsr & ARM64_PSR_MODE_MASK) == ARM64_PSR_MODE_EL0T);
252}
253
254#define KiGetPreviousMode(TrapFrame) \
255 (KiUserTrap(TrapFrame) ? UserMode : KernelMode)
256
258VOID
261 _In_ PKTRAP_FRAME TrapFrame)
262{
264 UNREFERENCED_PARAMETER(TrapFrame);
265}
266
268VOID
270{
271}
272
276{
277 return &PsGetCurrentThread()->Tcb.TrapFrame->Sp;
278}
279
281VOID
283 _In_ ULONG_PTR InitialStack);
284
285VOID
287
288VOID
290
293
294VOID
297 _In_ PKSYSTEM_ROUTINE SystemRoutine,
299 _In_ PVOID StartContext,
301#define KeArchInitThreadWithContext(Thread,SystemRoutine,StartRoutine,StartContext,Context) \
302 KeArm64InitThreadWithContext(Thread,SystemRoutine,StartRoutine,StartContext,Context)
303
304#ifdef _NTOSKRNL_
305VOID
307#endif
308
309VOID
311 _In_ PKTRAP_FRAME TrapFrame,
313
314VOID
316 _Out_ PKTRAP_FRAME TrapFrame,
318 _In_ KPROCESSOR_MODE RequestorMode);
319
321VOID
323 _In_ PKTRAP_FRAME TrapFrame);
324
326VOID
328 _In_ PKTRAP_FRAME TrapFrame,
329 _In_ PKEXCEPTION_FRAME ExceptionFrame);
330
331#endif /* __ASM__ */
332
333// HACK
334extern NTKERNELAPI volatile KSYSTEM_TIME KeTickCount;
335
336// win64 uses DMA macros, this one is not defined
339NTAPI
341 _In_ PADAPTER_OBJECT AdapterObject,
345
346#ifdef __cplusplus
347} // extern "C"
348#endif
UINT32 void void ** ReturnValue
Definition: acevents.h:216
unsigned char BOOLEAN
Definition: actypes.h:127
VOID NTAPI KeArm64FlushTbEntry(_In_ PVOID Address)
VOID NTAPI KeArm64SweepIcacheRange(_In_ PVOID BaseAddress, _In_ SIZE_T Length)
#define ARM64_DAIF
Definition: intrin_i.h:22
VOID NTAPI KeArm64FlushTbAll(VOID)
#define KeArm64ReadRegister(Register)
Definition: intrin_i.h:46
#define ARM64_PSR_I
Definition: intrin_i.h:39
#define ARM64_PSR_MODE_EL0T
Definition: intrin_i.h:43
#define ARM64_PSR_MODE_MASK
Definition: intrin_i.h:42
LONG NTSTATUS
Definition: precomp.h:26
_Out_ PKIRQL Irql
Definition: csq.h:179
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define DECLSPEC_NORETURN
Definition: corecrt.h:131
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_In_ PIRP _In_ PIO_COMPLETION_ROUTINE _In_ PVOID Context
Definition: kse.h:81
unsigned __int64 ULONG64
Definition: imports.h:198
VOID(NTAPI * PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine, PVOID StartContext)
Definition: ketypes.h:881
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID * BaseAddress
Definition: mmfuncs.h:408
#define _Inout_
Definition: no_sal2.h:162
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:329
VOID KiSetTrapContext(_Out_ PKTRAP_FRAME TrapFrame, _In_ PCONTEXT Context, _In_ KPROCESSOR_MODE RequestorMode)
DECLSPEC_NORETURN VOID KiUserCallbackExit(_In_ PKTRAP_FRAME TrapFrame)
FORCEINLINE ULONG_PTR KeGetTrapFrameFrameRegister(PKTRAP_FRAME TrapFrame)
Definition: ke.h:202
DECLSPEC_NORETURN VOID KiSwitchToBootStack(IN ULONG_PTR InitialStack)
Definition: ke.h:925
FORCEINLINE ULONG_PTR KeGetContextFrameRegister(PCONTEXT Context)
Definition: ke.h:167
FORCEINLINE VOID KeInvalidateTlbEntry(IN PVOID Address)
Definition: ke.h:274
FORCEINLINE VOID KeFlushProcessTb(VOID)
Definition: ke.h:282
FORCEINLINE VOID KeSetContextFrameRegister(PCONTEXT Context, ULONG_PTR Frame)
Definition: ke.h:174
DECLSPEC_NORETURN VOID KiExceptionExit(_In_ PKTRAP_FRAME TrapFrame, _In_ PKEXCEPTION_FRAME ExceptionFrame)
Definition: ke.h:720
FORCEINLINE ULONG64 KxQueryProcessorCycleTime(VOID)
Definition: ke.h:209
FORCEINLINE ULONG_PTR KeGetTrapFrameStackRegister(PKTRAP_FRAME TrapFrame)
Definition: ke.h:195
FORCEINLINE BOOLEAN KeDisableInterrupts(VOID)
Definition: ke.h:249
ULONG64 KiGetFeatureBits(VOID)
Evaluates the KeFeatureFlag bits for the current CPU.
Definition: cpu.c:73
NTHALAPI NTSTATUS NTAPI HalAllocateAdapterChannel(IN PADAPTER_OBJECT AdapterObject, IN PWAIT_CONTEXT_BLOCK Wcb, IN ULONG NumberOfMapRegisters, IN PDRIVER_CONTROL ExecutionRoutine)
Definition: dma.c:88
VOID KiGetTrapContext(_In_ PKTRAP_FRAME TrapFrame, _Out_ PCONTEXT Context)
FORCEINLINE VOID KeRestoreInterrupts(BOOLEAN WereEnabled)
Definition: ke.h:264
VOID KiSetProcessorType(VOID)
Definition: cpu.c:40
FORCEINLINE VOID KiRundownThread(IN PKTHREAD Thread)
Definition: ke.h:303
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
FORCEINLINE BOOLEAN KiUserTrap(IN PKTRAP_FRAME TrapFrame)
Definition: ke.h:377
FORCEINLINE VOID KeSweepICache(IN PVOID BaseAddress, IN SIZE_T FlushSize)
Definition: ke.h:290
FORCEINLINE PULONG_PTR KiGetUserModeStackAddress(void)
Definition: ke.h:479
FORCEINLINE ULONG_PTR KeGetContextStackRegister(PCONTEXT Context)
Definition: ke.h:160
VOID KiArm64InitializeTraps(VOID)
VOID KeArm64InitThreadWithContext(_In_ PKTHREAD Thread, _In_ PKSYSTEM_ROUTINE SystemRoutine, _In_ PKSTART_ROUTINE StartRoutine, _In_ PVOID StartContext, _In_opt_ PCONTEXT Context)
#define KeSetContextReturnRegister(Context, ReturnValue)
Definition: ke.h:43
#define Ki386PerfEnd()
Definition: ke.h:174
#define KiGetLinkedTrapFrame(x)
Definition: ke.h:177
#define KeSetContextPc(Context, ProgramCounter)
Definition: ke.h:34
#define KeGetContextPc(Context)
Definition: ke.h:31
#define KeGetContextReturnRegister(Context)
Definition: ke.h:40
#define KeGetTrapFramePc(TrapFrame)
Definition: ke.h:37
#define KiEndInterrupt(x, y)
Definition: ke.h:175
VOID NTAPI KiThreadStartup(VOID)
Definition: thrdini.c:63
#define NTHALAPI
Definition: ntoskrnl.h:40
static WCHAR Address[46]
Definition: ping.c:68
void __cdecl _disable(void)
Definition: intrin_arm.h:365
void __cdecl _enable(void)
Definition: intrin_arm.h:373
uint32_t * PULONG_PTR
Definition: typedefs.h:65
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define FORCEINLINE
Definition: wdftypes.h:67
#define NTKERNELAPI
_Out_ PULONG NumberOfMapRegisters
Definition: halfuncs.h:209
_In_ PDEVICE_OBJECT _In_ ULONG _In_ PDRIVER_CONTROL ExecutionRoutine
Definition: iofuncs.h:1717
DRIVER_CONTROL * PDRIVER_CONTROL
Definition: iotypes.h:215
KSTART_ROUTINE * PKSTART_ROUTINE
Definition: ketypes.h:567
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
Definition: psfuncs.h:92