From 341759a325b41a494ff0e6ced0ccdd06c333d93b Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Fri, 24 Apr 2026 13:58:33 +0200 Subject: [PATCH] Implement Kernel Shared Data management and initialization --- sdk/xtdk/ketypes.h | 21 ++++++ sdk/xtdk/xtstruct.h | 2 + xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/includes/ke.hh | 1 + xtoskrnl/includes/ke/shdata.hh | 31 ++++++++ xtoskrnl/ke/amd64/krnlinit.cc | 4 +- xtoskrnl/ke/data.cc | 3 + xtoskrnl/ke/i686/krnlinit.cc | 4 +- xtoskrnl/ke/shdata.cc | 129 +++++++++++++++++++++++++++++++++ 9 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 xtoskrnl/includes/ke/shdata.hh create mode 100644 xtoskrnl/ke/shdata.cc diff --git a/sdk/xtdk/ketypes.h b/sdk/xtdk/ketypes.h index d9e36e6..3837d6b 100644 --- a/sdk/xtdk/ketypes.h +++ b/sdk/xtdk/ketypes.h @@ -449,6 +449,27 @@ typedef struct _KPROCESS UCHAR Spare; } KPROCESS, *PKPROCESS; +/* System Time structure definition */ +typedef struct _KSYSTEM_TIME +{ + ULONG LowPart; + LONG High1Part; + LONG High2Part; +} KSYSTEM_TIME, *PKSYSTEM_TIME; + +/* Kernel Shared Data (KSD) structure definition */ +typedef struct _KSHARED_DATA +{ + VOLATILE KSYSTEM_TIME SystemTime; + ULONG XtMajorVersion; + ULONG XtMinorVersion; + WCHAR XtBuild[8]; + WCHAR XtBuildHash[11]; + WCHAR XtArchitecture[8]; + WCHAR XtDate[9]; + WCHAR XtFullDate[25]; +} KSHARED_DATA, *PKSHARED_DATA; + /* Thread control block structure definition */ typedef struct _KTHREAD { diff --git a/sdk/xtdk/xtstruct.h b/sdk/xtdk/xtstruct.h index b9e1ce5..30f9bfd 100644 --- a/sdk/xtdk/xtstruct.h +++ b/sdk/xtdk/xtstruct.h @@ -263,7 +263,9 @@ typedef struct _KPROCESS KPROCESS, *PKPROCESS; typedef struct _KQUEUE KQUEUE, *PKQUEUE; typedef struct _KSEMAPHORE KSEMAPHORE, *PKSEMAPHORE; typedef struct _KSERVICE_DESCRIPTOR_TABLE KSERVICE_DESCRIPTOR_TABLE, *PKSERVICE_DESCRIPTOR_TABLE; +typedef struct _KSHARED_DATA KSHARED_DATA, *PKSHARED_DATA; typedef struct _KSPIN_LOCK_QUEUE KSPIN_LOCK_QUEUE, *PKSPIN_LOCK_QUEUE; +typedef struct _KSYSTEM_TIME KSYSTEM_TIME, *PKSYSTEM_TIME; typedef struct _KTHREAD KTHREAD, *PKTHREAD; typedef struct _KTIMER KTIMER, *PKTIMER; typedef struct _KUBSAN_FLOAT_CAST_OVERFLOW_DATA KUBSAN_FLOAT_CAST_OVERFLOW_DATA, *PKUBSAN_FLOAT_CAST_OVERFLOW_DATA; diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index a0c48bd..afa9e8d 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -50,6 +50,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/ke/kubsan.cc ${XTOSKRNL_SOURCE_DIR}/ke/runlevel.cc ${XTOSKRNL_SOURCE_DIR}/ke/semphore.cc + ${XTOSKRNL_SOURCE_DIR}/ke/shdata.cc ${XTOSKRNL_SOURCE_DIR}/ke/spinlock.cc ${XTOSKRNL_SOURCE_DIR}/ke/sysres.cc ${XTOSKRNL_SOURCE_DIR}/ke/timer.cc diff --git a/xtoskrnl/includes/ke.hh b/xtoskrnl/includes/ke.hh index fa995d6..6448dd3 100644 --- a/xtoskrnl/includes/ke.hh +++ b/xtoskrnl/includes/ke.hh @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/xtoskrnl/includes/ke/shdata.hh b/xtoskrnl/includes/ke/shdata.hh new file mode 100644 index 0000000..cc00b4a --- /dev/null +++ b/xtoskrnl/includes/ke/shdata.hh @@ -0,0 +1,31 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/ke/shdata.hh + * DESCRIPTION: Kernel Shared Data + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_KE_SHDATA_HH +#define __XTOSKRNL_KE_SHDATA_HH + +#include + + +/* Kernel Library */ +namespace KE +{ + class SharedData + { + private: + STATIC PKSHARED_DATA KernelSharedData; + + public: + STATIC XTAPI PKSHARED_DATA GetKernelSharedData(VOID); + STATIC XTAPI LARGE_INTEGER GetSystemTime(VOID); + STATIC XTAPI VOID InitializeKernelSharedData(VOID); + STATIC XTAPI VOID SetSystemTime(IN LARGE_INTEGER Time); + }; +} + +#endif /* __XTOSKRNL_KE_SHDATA_HH */ diff --git a/xtoskrnl/ke/amd64/krnlinit.cc b/xtoskrnl/ke/amd64/krnlinit.cc index e0ad0ff..9148e49 100644 --- a/xtoskrnl/ke/amd64/krnlinit.cc +++ b/xtoskrnl/ke/amd64/krnlinit.cc @@ -53,8 +53,8 @@ KE::KernelInit::InitializeMachine(VOID) /* Initialize page map support */ MM::Paging::InitializePageMapSupport(); - /* Map Kernel Shared Data (KSD) */ - MM::Manager::MapKernelSharedData(); + /* Initialize Kernel Shared Data (KSD) */ + KE::SharedData::InitializeKernelSharedData(); /* Initialize processor */ HL::Cpu::InitializeProcessor(); diff --git a/xtoskrnl/ke/data.cc b/xtoskrnl/ke/data.cc index 5f09268..916991a 100644 --- a/xtoskrnl/ke/data.cc +++ b/xtoskrnl/ke/data.cc @@ -21,6 +21,9 @@ ETHREAD KE::KThread::InitialThread = {}; /* Kernel UBSAN active frame flag */ BOOLEAN KE::KUbsan::ActiveFrame = FALSE; +/* Kernel shared data (KSD) */ +PKSHARED_DATA KE::SharedData::KernelSharedData; + /* Kernel dispatcher lock queue */ KSPIN_LOCK KE::SpinLock::DispatcherLockQueue; diff --git a/xtoskrnl/ke/i686/krnlinit.cc b/xtoskrnl/ke/i686/krnlinit.cc index ccf7bdd..aed82f0 100644 --- a/xtoskrnl/ke/i686/krnlinit.cc +++ b/xtoskrnl/ke/i686/krnlinit.cc @@ -53,8 +53,8 @@ KE::KernelInit::InitializeMachine(VOID) /* Initialize page map support */ MM::Paging::InitializePageMapSupport(); - /* Map Kernel Shared Data (KSD) */ - MM::Manager::MapKernelSharedData(); + /* Initialize Kernel Shared Data (KSD) */ + KE::SharedData::InitializeKernelSharedData(); /* Initialize processor */ HL::Cpu::InitializeProcessor(); diff --git a/xtoskrnl/ke/shdata.cc b/xtoskrnl/ke/shdata.cc new file mode 100644 index 0000000..2d08dc6 --- /dev/null +++ b/xtoskrnl/ke/shdata.cc @@ -0,0 +1,129 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/ke/shdata.cc + * DESCRIPTION: Kernel Shared Data + * DEVELOPERS: Aiken Harris + */ + +#include + + +/** + * Retrieves a pointer to the memory-mapped Kernel Shared Data. + * + * @return This routine returns a pointer to the KSHARED_DATA structure. + * + * @since XT 1.0 + */ +XTAPI +PKSHARED_DATA +KE::SharedData::GetKernelSharedData(VOID) +{ + /* Return the internally managed pointer */ + return KernelSharedData; +} + +/** + * Retrieves the current system time using a lock-free read mechanism. + * + * @return This routine returns a LARGE_INTEGER containing the system time. + * + * @since XT 1.0 + */ +XTAPI +LARGE_INTEGER +KE::SharedData::GetSystemTime(VOID) +{ + LARGE_INTEGER CurrentTime; + + /* Initialize to zero */ + CurrentTime.QuadPart = 0; + + /* Perform a lock-free read sequence */ + do + { + /* Read the primary high part and low part */ + CurrentTime.HighPart = KernelSharedData->SystemTime.High1Part; + CurrentTime.LowPart = KernelSharedData->SystemTime.LowPart; + } + while(CurrentTime.HighPart != KernelSharedData->SystemTime.High2Part); + + /* Return the 64-bit time */ + return CurrentTime; +} + +/** + * Maps and initializes the Kernel Shared Data (KSD) structure. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KE::SharedData::InitializeKernelSharedData(VOID) +{ + PCSTR SourceString; + XTSTATUS Status; + + /* Map Kernel Shared Data (KSD) */ + Status = MM::Manager::MapKernelSharedData(); + if(Status != STATUS_SUCCESS) + { + /* Failed to map KSD, raise kernel panic */ + KE::Crash::Panic(0); + } + + /* Bind the pointer to the architectural virtual address */ + KernelSharedData = (PKSHARED_DATA)MM_KERNEL_SHARED_DATA_ADDRESS; + + /* Populate numeric version identifiers */ + KernelSharedData->XtMajorVersion = XTOS_VERSION_MAJOR; + KernelSharedData->XtMinorVersion = XTOS_VERSION_MINOR; + + /* Convert and copy system build string */ + SourceString = XTOS_VERSION_BUILD; + RTL::String::StringToWideString(KernelSharedData->XtBuild, (PCSTR*)&SourceString, + (sizeof(KernelSharedData->XtBuild) / sizeof(WCHAR)) - 1); + + /* Convert and copy system build hash string */ + SourceString = XTOS_VERSION_HASH; + RTL::String::StringToWideString(KernelSharedData->XtBuildHash, (PCSTR*)&SourceString, + (sizeof(KernelSharedData->XtBuildHash) / sizeof(WCHAR)) - 1); + + /* Convert and copy system architecture string */ + SourceString = XTOS_VERSION_ARCH; + RTL::String::StringToWideString(KernelSharedData->XtArchitecture, (PCSTR*)&SourceString, + (sizeof(KernelSharedData->XtArchitecture) / sizeof(WCHAR)) - 1); + + /* Convert and copy system build date string */ + SourceString = XTOS_VERSION_DATE; + RTL::String::StringToWideString(KernelSharedData->XtDate, (PCSTR*)&SourceString, + (sizeof(KernelSharedData->XtDate) / sizeof(WCHAR)) - 1); + + /* Convert and copy system build full date string */ + SourceString = XTOS_VERSION_FULLDATE; + RTL::String::StringToWideString(KernelSharedData->XtFullDate, (PCSTR*)&SourceString, + (sizeof(KernelSharedData->XtFullDate) / sizeof(WCHAR)) - 1); +} + +/** + * Updates the global system time using a strict lock-free write mechanism. + * + * @param Time + * Supplies the new system time as a 64-bit LARGE_INTEGER value. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KE::SharedData::SetSystemTime(IN LARGE_INTEGER Time) +{ + /* Set the new system time */ + KernelSharedData->SystemTime.High2Part = Time.HighPart; + KernelSharedData->SystemTime.LowPart = Time.LowPart; + KernelSharedData->SystemTime.High1Part = Time.HighPart; +}