From ef4f9ba74d6207395093655d2d5012d7d7a07234 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Tue, 30 Jun 2026 22:21:30 +0200 Subject: [PATCH] Introduce very basic exception handling support --- sdk/xtdk/rtltypes.h | 10 ++++ xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/includes/rtl.hh | 1 + xtoskrnl/includes/rtl/exsup.hh | 27 +++++++++++ xtoskrnl/rtl/exsup.cc | 89 ++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 xtoskrnl/includes/rtl/exsup.hh create mode 100644 xtoskrnl/rtl/exsup.cc diff --git a/sdk/xtdk/rtltypes.h b/sdk/xtdk/rtltypes.h index 7f1f96a..7fdd284 100644 --- a/sdk/xtdk/rtltypes.h +++ b/sdk/xtdk/rtltypes.h @@ -32,6 +32,16 @@ #define DOUBLE_SCIENTIFIC_PRECISION -4 #define DOUBLE_SIGN_BIT 0x8000000000000000ULL +/* Exception Record flags */ +#define EXCEPTION_CONTINUE_SEARCH 0x00 +#define EXCEPTION_NONCONTINUABLE 0x01 +#define EXCEPTION_UNWINDING 0x02 +#define EXCEPTION_EXIT_UNWIND 0x04 +#define EXCEPTION_STACK_INVALID 0x08 +#define EXCEPTION_NESTED_CALL 0x10 +#define EXCEPTION_TARGET_UNWIND 0x20 +#define EXCEPTION_COLLIDED_UNWIND 0x40 + /* Maximum number of lead bytes for NLS */ #define NLS_MAXIMUM_LEADBYTES 12 diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 3a1c242..db6f401 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -100,6 +100,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/rtl/data.cc ${XTOSKRNL_SOURCE_DIR}/rtl/endian.cc ${XTOSKRNL_SOURCE_DIR}/rtl/exports.cc + ${XTOSKRNL_SOURCE_DIR}/rtl/exsup.cc ${XTOSKRNL_SOURCE_DIR}/rtl/guid.cc ${XTOSKRNL_SOURCE_DIR}/rtl/lifo.cc ${XTOSKRNL_SOURCE_DIR}/rtl/llist.cc diff --git a/xtoskrnl/includes/rtl.hh b/xtoskrnl/includes/rtl.hh index 610ca79..b27b08d 100644 --- a/xtoskrnl/includes/rtl.hh +++ b/xtoskrnl/includes/rtl.hh @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include diff --git a/xtoskrnl/includes/rtl/exsup.hh b/xtoskrnl/includes/rtl/exsup.hh new file mode 100644 index 0000000..d221c98 --- /dev/null +++ b/xtoskrnl/includes/rtl/exsup.hh @@ -0,0 +1,27 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/rtl/exsup.hh + * DESCRIPTION: Exception handling + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_RTL_EXSUP_HH +#define __XTOSKRNL_RTL_EXSUP_HH + +#include + + +/* Runtime Library */ +namespace RTL +{ + class Exception + { + public: + STATIC XTAPI VOID RaiseException(IN PEXCEPTION_RECORD ExceptionRecord); + STATIC XTAPI VOID RaiseStatus(IN XTSTATUS Status); + STATIC XTAPI LONG SystemFilter(VOID); + }; +} + +#endif /* __XTOSKRNL_RTL_EXSUP_HH */ diff --git a/xtoskrnl/rtl/exsup.cc b/xtoskrnl/rtl/exsup.cc new file mode 100644 index 0000000..5486c77 --- /dev/null +++ b/xtoskrnl/rtl/exsup.cc @@ -0,0 +1,89 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/rtl/exsup.cc + * DESCRIPTION: Exception handling + * DEVELOPERS: Aiken Harris + */ + +#include + + +/** + * Raises a structured exception using an exception record. + * + * @param ExceptionRecord + * Supplies a pointer to the exception record containing details. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +RTL::Exception::RaiseException(IN PEXCEPTION_RECORD ExceptionRecord) +{ + /* Not implemented */ + UNIMPLEMENTED; + + /* Disable interrupts */ + AR::CpuFunctions::ClearInterruptFlag(); + + /* Halt the system */ + KE::Crash::Panic(ExceptionRecord->ExceptionCode); +} + +/** + * Raises a structured exception based on the provided status code. + * + * @param Status + * Supplies the status code to be raised as an exception. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +RTL::Exception::RaiseStatus(IN XTSTATUS Status) +{ + EXCEPTION_RECORD ExceptionRecord; + + /* Not implemented */ + UNIMPLEMENTED; + + /* Create a basic exception record */ + ExceptionRecord.ExceptionAddress = _ReturnAddress(); + ExceptionRecord.ExceptionCode = Status; + ExceptionRecord.ExceptionRecord = NULLPTR; + ExceptionRecord.NumberParameters = 0; + ExceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE; + + /* Dispatch the exception */ + RaiseException(&ExceptionRecord); +} + +/** + * Determines whether a SEH exception should be handled or lead to a kernel panic based on the processor mode. + * + * @return This routine returns EXCEPTION_EXECUTE_HANDLER if the CPU was in UserMode, + * or EXCEPTION_CONTINUE_SEARCH if the CPU was in KernelMode. + * + * @since XT 1.0 + */ +XTAPI +LONG +RTL::Exception::SystemFilter(VOID) +{ + /* Check if the exception originated from kernel-mode execution */ + if(KE::Processor::GetCurrentThread()->PreviousMode == KernelMode) + { + /* Kernel-mode fault, force a kernel panic */ + return EXCEPTION_CONTINUE_SEARCH; + } + else + { + /* User-mode fault, catch it and return the error status gracefully */ + return EXCEPTION_EXECUTE_HANDLER; + } +}