kernel
Loading...
Searching...
No Matches
exception.h
Go to the documentation of this file.
1#ifndef X86_MATANEL_EXCEPTION
2#define X86_MATANEL_EXCEPTION
3
4/*++
5
6Module Name:
7
8 exception.h
9
10Purpose:
11
12 This module contains the header files & prototypes required for runtime exception handling of the OS.
13
14Author:
15
16 slep (Matanel) 2025.
17
18Revision History:
19
20--*/
21
22// Base includes
23#include <stdint.h>
24#include <stddef.h>
25
26// Other file includes
27#include "me.h"
28#include "core.h"
29
30// ------------------ STRUCTURES ------------------
31
32//struct _CONTEXT;
33
34#define EXCEPTION_MAXIMUM_PARAMETERS 15
35typedef struct _EXCEPTION_RECORD {
38 struct _EXCEPTION_RECORD* ExceptionRecord; // For nested exceptions
39 void* ExceptionAddress; // RIP at time of fault
40 //struct _CONTEXT ExceptionContext;
42
49
50typedef struct _CONTEXT {
51 uint64_t RFlags;
52 uint64_t Dr0;
53 uint64_t Dr1;
54 uint64_t Dr2;
55 uint64_t Dr3;
56 uint64_t Dr6;
57 uint64_t Dr7;
58 uint64_t Rax;
59 uint64_t Rcx;
60 uint64_t Rdx;
61 uint64_t Rbx;
62 uint64_t Rsp;
63 uint64_t Rbp;
64 uint64_t Rsi;
65 uint64_t Rdi;
66 uint64_t R8;
67 uint64_t R9;
68 uint64_t R10;
69 uint64_t R11;
70 uint64_t R12;
71 uint64_t R13;
72 uint64_t R14;
73 uint64_t R15;
74 uint64_t Rip;
76
81
86
92
93// Symbols defined by the linker script
96
97// Helper to search the table
98uint64_t MiSearchExceptionTable(uint64_t rip);
99
100// ------------------ FUNCTIONS ------------------
101
102extern PETHREAD PsGetCurrentThread(void);
103extern bool ExpCaptureContext(IN PCONTEXT Context);
104
106 PEXCEPTION_RECORD ExceptionRecord,
107 void* EstablisherFrame,
108 PCONTEXT ContextRecord,
109 void* DispatcherContext
110);
111
112// macros
113// Try except blocks mean we will most likely touch user accessible memory, so stac and clac are always included no matter the previousmode.
114#ifndef _MSC_VER
115#define try do { \
116 __label__ _try_start, _try_end, _except_label, _try_break; \
117 __stac(); \
118 /* Emit the table entry linking this range to the handler */ \
119 __asm__ volatile ( \
120 ".section __ex_table,\"a\"\n\t" \
121 ".quad %P0, %P1, %P2\n\t" \
122 ".previous\n\t" \
123 : \
124 : "i" (&& _try_start), "i" (&& _try_end), "i" (&& _except_label)\
125 ); \
126 /* Start of protected region */ \
127 _try_start: \
128 __asm__ volatile("" ::: "memory"); /* Prevent hoisting */ \
129 {
130
131#define except \
132 } \
133 __asm__ volatile("" ::: "memory"); \
134 _try_end: \
135 { \
136 int _volatile_true = 1; \
137 __asm__ volatile("" : "+r"(_volatile_true)); \
138 if (_volatile_true) goto _try_break; \
139 } \
140 _except_label: \
141 { \
142 __clac(); \
143 /* The Page fault handler jumps here if we faulted */
144
145#define end_try \
146 } \
147 _try_break: \
148 __clac(); \
149} while (0)
150#define leave do { goto _try_break; } while (0)
151#else
152#define try
153#define except /* */
154#define end_try
155#define leave
156#endif
157bool
159 IN PETHREAD Thread
160);
161
162void
164 IN PTRAP_FRAME TrapFrame,
165 IN PCONTEXT ContextRecord,
166 IN PEXCEPTION_RECORD ExceptionRecord
167);
168
169uint64_t
171 uint64_t Rip
172);
173
174// instruction.c
175
176bool
177ExpIsPrivilegedInstruction(uint8_t* Ip /*, bool Wow64*/);
178
179// probe.c
180
183 IN const void* Address,
184 IN size_t Length,
185 IN uint32_t Alignment
186);
187
188// raise.c
189// unused func.
190void
192 IN MTSTATUS Status,
193 IN uint64_t Rip
194);
195
196#endif
#define IN
Definition annotations.h:8
TRAP_FRAME * PTRAP_FRAME
Definition core.h:56
ETHREAD * PETHREAD
Definition core.h:44
_EXCEPTION_DISPOSITION
Definition exception.h:43
@ ExceptionContinueSearch
Definition exception.h:45
@ ExceptionCollidedUnwind
Definition exception.h:47
@ ExceptionNestedException
Definition exception.h:46
@ ExceptionContinueExecution
Definition exception.h:44
EXCEPTION_RANGE __stop_ex_table[]
bool ExpIsExceptionHandlerPresent(IN PETHREAD Thread)
Definition exception.c:23
struct _EXCEPTION_RANGE EXCEPTION_RANGE
struct _CONTEXT CONTEXT
MTSTATUS ProbeForRead(IN const void *Address, IN size_t Length, IN uint32_t Alignment)
Definition probe.c:23
struct _EXCEPTION_RECORD EXCEPTION_RECORD
bool ExpIsPrivilegedInstruction(uint8_t *Ip)
Definition instruction.c:4
PETHREAD PsGetCurrentThread(void)
Definition thread.c:279
void ExpDispatchException(IN PTRAP_FRAME TrapFrame, IN PCONTEXT ContextRecord, IN PEXCEPTION_RECORD ExceptionRecord)
Definition exception.c:56
void ExpRaiseStatus(IN MTSTATUS Status, IN uint64_t Rip)
Definition raise.c:23
struct _EX_FRAME_REGISTRATION EX_FRAME_REGISTRATION
struct _CONTEXT * PCONTEXT
enum _EXCEPTION_DISPOSITION EXCEPTION_DISPOSITION
struct _EXCEPTION_REGISTRATION_RECORD EXCEPTION_REGISTRATION_RECORD
EXCEPTION_RANGE __start_ex_table[]
uint64_t MiSearchExceptionTable(uint64_t rip)
struct _EXCEPTION_RECORD * PEXCEPTION_RECORD
bool ExpCaptureContext(IN PCONTEXT Context)
EXCEPTION_DISPOSITION MeStandardHandler(PEXCEPTION_RECORD ExceptionRecord, void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext)
struct _EXCEPTION_RANGE * PEXCEPTION_RANGE
uint64_t ExpFindKernelModeExceptionHandler(uint64_t Rip)
Definition exception.c:114
uint64_t Address
Definition mh.h:4
uint32_t Length
Definition mh.h:6
int32_t MTSTATUS
Definition mtstatus.h:12
uint64_t R10
Definition exception.h:68
uint64_t R12
Definition exception.h:70
uint64_t Dr0
Definition exception.h:52
uint64_t Dr3
Definition exception.h:55
uint64_t Rdi
Definition exception.h:65
uint64_t Rsp
Definition exception.h:62
uint64_t RFlags
Definition exception.h:51
uint64_t Dr1
Definition exception.h:53
uint64_t Dr7
Definition exception.h:57
uint64_t R9
Definition exception.h:67
uint64_t Rbp
Definition exception.h:63
uint64_t Rax
Definition exception.h:58
uint64_t Rip
Definition exception.h:74
uint64_t R13
Definition exception.h:71
uint64_t Rdx
Definition exception.h:60
uint64_t R15
Definition exception.h:73
uint64_t R11
Definition exception.h:69
uint64_t Dr6
Definition exception.h:56
uint64_t R14
Definition exception.h:72
uint64_t Rsi
Definition exception.h:64
uint64_t Dr2
Definition exception.h:54
uint64_t Rbx
Definition exception.h:61
uint64_t R8
Definition exception.h:66
uint64_t Rcx
Definition exception.h:59
EXCEPTION_REGISTRATION_RECORD * RegistrationPointer
Definition exception.h:84
uint64_t end_addr
Definition exception.h:89
uint64_t start_addr
Definition exception.h:88
uint64_t handler_addr
Definition exception.h:90
uint32_t ExceptionFlags
Definition exception.h:37
void * ExceptionAddress
Definition exception.h:39
struct _EXCEPTION_RECORD * ExceptionRecord
Definition exception.h:38
MTSTATUS ExceptionCode
Definition exception.h:36
struct _EXCEPTION_REGISTRATION_RECORD * Next
Definition exception.h:78
enum _EXCEPTION_DISPOSITION(* Handler)(struct _EXCEPTION_RECORD *arg1, void *Frame, struct _CONTEXT *arg2, void *DispCtx)
Definition exception.h:79