kernel
Loading...
Searching...
No Matches
exception.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 exception.c
6
7Purpose:
8
9 This translation unit contains the implementation of exception checking & handling in MatanelOS (_try _except macros)
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
20#include "../../includes/ps.h"
21
22bool
24 IN PETHREAD Thread
25)
26
27/*++
28
29 Routine description:
30
31 Checks if an exception handler is present in the current thread context.
32
33 Arguments:
34
35 [IN] PETHREAD Thread - The thread to check on.
36
37 Return Values:
38
39 True if present, false otherwise.
40
41--*/
42
43{
44 if (Thread) {
45 if (Thread->ExceptionRegistration.Handler != NULL) {
46 return true;
47 }
48 else {
49 return false;
50 }
51 }
52 return false;
53}
54
55void
57 IN PTRAP_FRAME TrapFrame,
58 IN PCONTEXT ContextRecord,
59 IN PEXCEPTION_RECORD ExceptionRecord
60)
61
62/*++
63
64 Routine description:
65 (UNUSED)
66 Changes the trap frame to point to the _except handler of the thread.
67
68 Arguments:
69
70 [IN] PTRAP_FRAME trap - Pointer to Trap frame of the thread.
71 [IN] PCONTEXT ContextRecord - Pointer to Context record of the thread (saved in _try by thread)
72 [IN] PEXCEPTION_RECORD ExceptionRecord - Pointer to Exception record of the thread
73
74 Return Values:
75
76 None.
77
78--*/
79
80{
81 UNREFERENCED_PARAMETER(ExceptionRecord);
83 // Change trap frame to context record set by thread. (except RIP)
84 TrapFrame->rsp = ContextRecord->Rsp;
85 TrapFrame->rflags = ContextRecord->RFlags;
86
87 // General-purpose registers from the context frame
88 TrapFrame->r15 = ContextRecord->R15;
89 TrapFrame->r14 = ContextRecord->R14;
90 TrapFrame->r13 = ContextRecord->R13;
91 TrapFrame->r12 = ContextRecord->R12;
92
93 TrapFrame->r11 = ContextRecord->R11;
94 TrapFrame->r10 = ContextRecord->R10;
95 TrapFrame->r9 = ContextRecord->R9;
96 TrapFrame->r8 = ContextRecord->R8;
97
98 TrapFrame->rbp = ContextRecord->Rbp;
99 TrapFrame->rdi = ContextRecord->Rdi;
100 TrapFrame->rsi = ContextRecord->Rsi;
101
102 TrapFrame->rcx = ContextRecord->Rcx;
103 TrapFrame->rbx = ContextRecord->Rbx;
104 TrapFrame->rdx = ContextRecord->Rdx;
105 TrapFrame->rax = ContextRecord->Rax;
106
107 // Enumerate all handlers, if one returned FIXME TODO (Decide between return value approach or completely different approach, i scrapped the exception handling idea for now, too complicated and messy, id rather work on memory
108 }
109
110
111}
112
113uint64_t
115 uint64_t Rip
116)
117
118/*++
119
120 Routine description:
121
122 Enumerates the section provided by the linker script to find a suitable exception handler for the kernel RIP given.
123
124 Arguments:
125
126 [IN] uint64_t Rip - Address that caused the page fault in kernel mode.
127
128 Return Values:
129
130 Address of exception handler if found, else 0.
131
132--*/
133
134{
136
137 // We could do a binary search since this is address given, but WHO cares.
138 while (Entry < __stop_ex_table) {
139 // Check if the faulting RIP is within the range.
140 if (Rip >= Entry->start_addr && Rip < Entry->end_addr) {
141 // Found a handler!
142 return Entry->handler_addr;
143 }
144
145 // Increment entry.
146 Entry++;
147 }
148
149 // No exception handler found..
150 return 0;
151}
#define IN
Definition annotations.h:8
TRAP_FRAME * PTRAP_FRAME
Definition core.h:56
ETHREAD * PETHREAD
Definition core.h:44
bool ExpIsExceptionHandlerPresent(IN PETHREAD Thread)
Definition exception.c:23
void ExpDispatchException(IN PTRAP_FRAME TrapFrame, IN PCONTEXT ContextRecord, IN PEXCEPTION_RECORD ExceptionRecord)
Definition exception.c:56
uint64_t ExpFindKernelModeExceptionHandler(uint64_t Rip)
Definition exception.c:114
EXCEPTION_RANGE __stop_ex_table[]
struct _CONTEXT * PCONTEXT
EXCEPTION_RANGE __start_ex_table[]
struct _EXCEPTION_RECORD * PEXCEPTION_RECORD
struct _EXCEPTION_RANGE * PEXCEPTION_RANGE
#define UNREFERENCED_PARAMETER(x)
Definition intrin.h:29
uint64_t start_addr
Definition exception.h:88
uint64_t handler_addr
Definition exception.h:90
PETHREAD PsGetCurrentThread(void)
Definition thread.c:279