My Project
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
87// ------------------ FUNCTIONS ------------------
88
89extern PETHREAD PsGetCurrentThread(void);
90extern bool ExpCaptureContext(IN PCONTEXT Context);
91
93 PEXCEPTION_RECORD ExceptionRecord,
94 void* EstablisherFrame,
95 PCONTEXT ContextRecord,
96 void* DispatcherContext
97);
98
99// macros
100#define _try \
101 { \
102 PETHREAD _CurrentThread = PsGetCurrentThread(); \
103 ME_EXCEPTION_FRAME _MyFrame; \
104 _MyFrame.Next = (PME_EXCEPTION_FRAME)_CurrentThread->ExceptionList; \
105 _MyFrame.Handler = MeStandardHandler; \
106 _CurrentThread->ExceptionList = &_MyFrame; \
107 /* Save Context. Returns 0 initially. Returns 1 if we crashed. */ \
108 if (ExpCaptureContext(&_MyFrame) == 0) { \
109
110#define _except(FilterExpression) \
111 /* Success path: Unlink frame */ \
112 _CurrentThread->ExceptionList = _MyFrame.Next; \
113 } else { \
114 /* Crash path: We just "landed" here from the handler! */ \
115 /* Unlink frame (safe to do again) */ \
116 _CurrentThread->ExceptionList = _MyFrame.Next; \
117 /* You can access _MyFrame.ExceptionCode here if needed */ \
118 { \
119 /* User Code inside except block */
120
121#define _end_except \
122 } \
123 } \
124 }
125
126bool
128 IN PETHREAD Thread
129);
130
131void
133 IN PTRAP_FRAME TrapFrame,
134 IN PCONTEXT ContextRecord,
135 IN PEXCEPTION_RECORD ExceptionRecord
136);
137
138#endif
#define IN
Definition annotations.h:7
TRAP_FRAME * PTRAP_FRAME
Definition core.h:54
ETHREAD * PETHREAD
Definition core.h:42
_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
bool ExpIsExceptionHandlerPresent(IN PETHREAD Thread)
Definition exception.c:23
struct _CONTEXT CONTEXT
struct _EXCEPTION_RECORD EXCEPTION_RECORD
PETHREAD PsGetCurrentThread(void)
Definition thread.c:191
void ExpDispatchException(IN PTRAP_FRAME TrapFrame, IN PCONTEXT ContextRecord, IN PEXCEPTION_RECORD ExceptionRecord)
Definition exception.c:56
struct _EX_FRAME_REGISTRATION EX_FRAME_REGISTRATION
struct _CONTEXT * PCONTEXT
enum _EXCEPTION_DISPOSITION EXCEPTION_DISPOSITION
struct _EXCEPTION_REGISTRATION_RECORD EXCEPTION_REGISTRATION_RECORD
struct _EXCEPTION_RECORD * PEXCEPTION_RECORD
bool ExpCaptureContext(IN PCONTEXT Context)
EXCEPTION_DISPOSITION MeStandardHandler(PEXCEPTION_RECORD ExceptionRecord, void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext)
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
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