From 596268586bb4c8248a8ec106b8cdea12b9ab926a Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Tue, 18 Jun 2024 10:39:00 +0200 Subject: IMP: TLS syscall, serial write syscall. FIX: SMP manager writes to stack frame directly, check if we also want to free the stack. Signed-off-by: Amlal EL Mahrouss --- Kernel/KernelKit/ThreadLocalStorage.hxx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'Kernel/KernelKit/ThreadLocalStorage.hxx') diff --git a/Kernel/KernelKit/ThreadLocalStorage.hxx b/Kernel/KernelKit/ThreadLocalStorage.hxx index eaae7991..432cc9ac 100644 --- a/Kernel/KernelKit/ThreadLocalStorage.hxx +++ b/Kernel/KernelKit/ThreadLocalStorage.hxx @@ -37,14 +37,16 @@ struct PACKED ThreadInformationBlock final NewOS::Int32 ThreadID; // Thread execution ID. }; +typedef struct ThreadInformationBlock ProcessInformationBlock; + /// @brief TLS install TIB and PIB. -EXTERN_C void rt_install_tib(ThreadInformationBlock* TIB, NewOS::VoidPtr PIB); +EXTERN_C void rt_install_tib(ThreadInformationBlock* TIB, ThreadInformationBlock* PIB); ///! @brief Cookie Sanity check. NewOS::Boolean tls_check_tib(ThreadInformationBlock* Ptr); /// @brief TLS check system call -EXTERN_C NewOS::Void tls_check_syscall_impl(NewOS::HAL::StackFramePtr StackPtr) noexcept; +EXTERN_C NewOS::Void tls_check_syscall_impl(NewOS::VoidPtr TIB) noexcept; #include -- cgit v1.2.3 From 1a3b924f62346cc76bd183883825c5541834c90c Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Tue, 18 Jun 2024 12:40:05 +0200 Subject: MHR-31: TLS.hxx: FIX: Dont specify a virtual address for **every** architecture. Signed-off-by: Amlal EL Mahrouss --- Kernel/KernelKit/ThreadLocalStorage.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Kernel/KernelKit/ThreadLocalStorage.hxx') diff --git a/Kernel/KernelKit/ThreadLocalStorage.hxx b/Kernel/KernelKit/ThreadLocalStorage.hxx index 432cc9ac..9d5473a2 100644 --- a/Kernel/KernelKit/ThreadLocalStorage.hxx +++ b/Kernel/KernelKit/ThreadLocalStorage.hxx @@ -27,7 +27,7 @@ T* tls_new_class(Args&&... args); #define kTLSCookieLen 3 /// @brief Thread Information Block for Local Storage. -/// Located in GS on AMD64, Virtual Address 0x10000 (64x0, 32x0, ARM64) +/// Located in GS on AMD64, other architectures have their own stuff. (64x0, 32x0, ARM64) struct PACKED ThreadInformationBlock final { NewOS::Char Cookie[kTLSCookieLen]; -- cgit v1.2.3 From 90faf32f29b5482b52ab90f416528ed8f4e8f130 Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Tue, 18 Jun 2024 13:04:01 +0200 Subject: MHR-31 IMP: Add TLS, a new/delete syscall. TODO: File I/O and network syscalls as well. Signed-off-by: Amlal EL Mahrouss --- Kernel/HALKit/AMD64/HalInstallTIB.asm | 2 ++ Kernel/HALKit/AMD64/HalKernelMain.cxx | 44 ++++++++++++++++++++++++++++++++- Kernel/KernelKit/ThreadLocalStorage.hxx | 30 +++++++++++----------- 3 files changed, 61 insertions(+), 15 deletions(-) (limited to 'Kernel/KernelKit/ThreadLocalStorage.hxx') diff --git a/Kernel/HALKit/AMD64/HalInstallTIB.asm b/Kernel/HALKit/AMD64/HalInstallTIB.asm index f53fb14e..9111c835 100644 --- a/Kernel/HALKit/AMD64/HalInstallTIB.asm +++ b/Kernel/HALKit/AMD64/HalInstallTIB.asm @@ -11,6 +11,8 @@ [global rt_install_tib] +section .text + ;; changed: rs, fs ;; expected: rcx, rdx diff --git a/Kernel/HALKit/AMD64/HalKernelMain.cxx b/Kernel/HALKit/AMD64/HalKernelMain.cxx index 3fac40ca..701439f1 100644 --- a/Kernel/HALKit/AMD64/HalKernelMain.cxx +++ b/Kernel/HALKit/AMD64/HalKernelMain.cxx @@ -74,10 +74,13 @@ EXTERN_C void hal_init_platform( NewOS::HAL::IDTLoader idt; idt.Load(idtBase); - /* install basic hooks. */ + /* install basic syscalls. */ constexpr auto cSerialWriteInterrupt = 0x10; // 16 constexpr auto cTlsInterrupt = 0x11; // 17 + constexpr auto cTlsInstallInterrupt = 0x12; // 18 + constexpr auto cNewInterrupt = 0x13; // 19 + constexpr auto cDeleteInterrupt = 0x14; // 20 kSyscalls[cSerialWriteInterrupt].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx) -> void { const char* msg = (const char*)rdx; @@ -88,8 +91,47 @@ EXTERN_C void hal_init_platform( tls_check_syscall_impl(rdx); }; + struct PACKED HeapAllocInfo final + { + NewOS::VoidPtr fThe; + NewOS::Size fTheSz; + }; + + struct PACKED ProcessBlockInfo final + { + ThreadInformationBlock* fTIB; + ThreadInformationBlock* fPIB; + }; + + kSyscalls[cNewInterrupt].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx)->void { + + /// get HAC struct. + HeapAllocInfo* rdxInf = (HeapAllocInfo*)rdx; + + /// assign the fThe field with the pointer. + rdxInf->fThe = NewOS::ProcessScheduler::The().Leak().TheCurrent().Leak().New(rdxInf->fTheSz); + }; + + kSyscalls[cDeleteInterrupt].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx)->void { + /// get HAC struct. + HeapAllocInfo* rdxInf = (HeapAllocInfo*)rdx; + + /// delete ptr with sz in mind. + NewOS::ProcessScheduler::The().Leak().TheCurrent().Leak().Delete(rdxInf->fThe, rdxInf->fTheSz); + }; + + kSyscalls[cTlsInstallInterrupt].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx)->void { + ProcessBlockInfo* rdxPb = (ProcessBlockInfo*)rdx; + + /// install the process's fTIB and fPIB. + rt_install_tib(rdxPb->fTIB, rdxPb->fPIB); + }; + kSyscalls[cSerialWriteInterrupt].Leak().Leak()->fHooked = true; kSyscalls[cTlsInterrupt].Leak().Leak()->fHooked = true; + kSyscalls[cTlsInstallInterrupt].Leak().Leak()->fHooked = true; + kSyscalls[cDeleteInterrupt].Leak().Leak()->fHooked = true; + kSyscalls[cNewInterrupt].Leak().Leak()->fHooked = true; NewOS::HAL::Detail::_ke_power_on_self_test(); diff --git a/Kernel/KernelKit/ThreadLocalStorage.hxx b/Kernel/KernelKit/ThreadLocalStorage.hxx index 9d5473a2..75d29ced 100644 --- a/Kernel/KernelKit/ThreadLocalStorage.hxx +++ b/Kernel/KernelKit/ThreadLocalStorage.hxx @@ -15,16 +15,7 @@ #define kCookieMag1 'C' #define kCookieMag2 'R' -template -T* tls_new_ptr(void); - -template -bool tls_delete_ptr(T* ptr); - -template -T* tls_new_class(Args&&... args); - -#define kTLSCookieLen 3 +#define kTLSCookieLen (3U) /// @brief Thread Information Block for Local Storage. /// Located in GS on AMD64, other architectures have their own stuff. (64x0, 32x0, ARM64) @@ -39,13 +30,24 @@ struct PACKED ThreadInformationBlock final typedef struct ThreadInformationBlock ProcessInformationBlock; -/// @brief TLS install TIB and PIB. -EXTERN_C void rt_install_tib(ThreadInformationBlock* TIB, ThreadInformationBlock* PIB); - ///! @brief Cookie Sanity check. NewOS::Boolean tls_check_tib(ThreadInformationBlock* Ptr); -/// @brief TLS check system call +///! @brief new ptr syscall. +template +T* tls_new_ptr(void); + +///! @brief delete ptr syscall. +template +bool tls_delete_ptr(T* ptr); + +template +T* tls_new_class(Args&&... args); + +/// @brief TLS install TIB and PIB. (syscall) +EXTERN_C void rt_install_tib(ThreadInformationBlock* TIB, ThreadInformationBlock* PIB); + +/// @brief TLS check (syscall) EXTERN_C NewOS::Void tls_check_syscall_impl(NewOS::VoidPtr TIB) noexcept; #include -- cgit v1.2.3