From 1be90ecf5c097c143ec90f463dbd73e7fa1ad3f9 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 3 Mar 2024 20:04:14 +0000 Subject: HCoreKrnl:HAL: Improve interrupt handling, found a solution to interrupt problem in C++; don't modify the stack on the C++ side. --- Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx | 19 +++++++++++++++++ Private/HALKit/AMD64/Processor.hpp | 2 +- Private/NewBoot/Source/BootMain.cxx | 26 ++++++++--------------- Private/NewKit/Macros.hpp | 1 + 4 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx diff --git a/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx b/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx new file mode 100644 index 00000000..b43ad8c0 --- /dev/null +++ b/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +//! @file InterruptHandler.cxx +//! @brief x64 interrupt handler. + +#include + +/// @brief AMD64 Interrupt handler redirects interrupts to their handlers. +EXTERN_C ATTRIBUTE(naked) HCore::UIntPtr rt_handle_interrupts(HCore::UIntPtr rsp) +{ + HCore::HAL::StackFramePtr stackPtr = reinterpret_cast(rsp); + HCORE_UNUSED(stackPtr); + + return rsp; +} \ No newline at end of file diff --git a/Private/HALKit/AMD64/Processor.hpp b/Private/HALKit/AMD64/Processor.hpp index 93a9775a..e5e35f20 100644 --- a/Private/HALKit/AMD64/Processor.hpp +++ b/Private/HALKit/AMD64/Processor.hpp @@ -79,7 +79,7 @@ using interruptTrap = UIntPtr(UIntPtr sp); typedef UIntPtr Reg; -struct PACKED StackFrame { +struct PACKED StackFrame final { Reg IntNum, ErrCode; Reg Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax; Reg R8, R9, R10, R11, R12, R13, R14, R15; diff --git a/Private/NewBoot/Source/BootMain.cxx b/Private/NewBoot/Source/BootMain.cxx index 53a217b6..67c6929a 100644 --- a/Private/NewBoot/Source/BootMain.cxx +++ b/Private/NewBoot/Source/BootMain.cxx @@ -7,8 +7,6 @@ * ======================================================== */ -#define __BOOTLOADER__ 1 - #include #include #include @@ -21,7 +19,7 @@ #endif // ifdef __x86_64__ #ifndef kBootKrnlSections -#error Please provide the amount of sections the kernel has. +#error [NewBoot/HCoreLdr] Please provide the amount of sections the kernel has. #endif // !kBootKrnlSections #define kBootReadSize \ @@ -33,11 +31,13 @@ typedef void (*bt_main_type)(HEL::HandoverInformationHeader* HIH); EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, EfiSystemTable* SystemTable) { - InitEFI(SystemTable); - InitQT(); + InitEFI(SystemTable); // Init the efi library. + InitQT(); // Quick Toolkit for UI BTextWriter writer; + /// Splash screen stuff + writer.Write(L"MahroussLogic (R) HCoreLdr: "); writer.Write(BVersionString::Shared()).Write(L"\r\n"); @@ -52,6 +52,8 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, .Write(SystemTable->FirmwareVendor) .Write(L"\r\n"); + /// Read Kernel blob. + BFileReader kernelImg(L"HCOREKRNL.EXE", ImageHandle); kernelImg.Size(kBootReadSize); @@ -70,6 +72,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, ExecOptionalHeaderPtr optHdr = reinterpret_cast( (UIntPtr)&ptrHdr->mCharacteristics + 1); + // first check for kernel.cfg inside ESP/EPM. BFileReader systemIni(L"KERNEL.CFG", ImageHandle); systemIni.Size(1); @@ -83,18 +86,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, UInt64 posSeek = 0; - /**** - * - * LOAD KERNEL CODE - * - * - * - * - * - * - * - * LOAD KERNEL CODE - */ + // load sections here kernelImg.File()->SetPosition(kernelImg.File(), &posSeek); kernelImg.Size(kBootReadSize + diff --git a/Private/NewKit/Macros.hpp b/Private/NewKit/Macros.hpp index 5b8fd0ce..141ccc53 100644 --- a/Private/NewKit/Macros.hpp +++ b/Private/NewKit/Macros.hpp @@ -89,3 +89,4 @@ #endif #define STRINGIFY(X) #X +#define HCORE_UNUSED(X) ((void)X) \ No newline at end of file -- cgit v1.2.3 From 2d2106367fc943bdd53d9c1e3570707cd5347d4a Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 4 Mar 2024 07:17:44 +0000 Subject: Update EPM.hxx --- Private/NewBoot/BootKit/EPM.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Private/NewBoot/BootKit/EPM.hxx b/Private/NewBoot/BootKit/EPM.hxx index 328906f2..2d20237d 100644 --- a/Private/NewBoot/BootKit/EPM.hxx +++ b/Private/NewBoot/BootKit/EPM.hxx @@ -9,4 +9,4 @@ #pragma once -#include +#include -- cgit v1.2.3 From 9a1a686f33c7fd77db0a336ad8e41044b58f022c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 4 Mar 2024 07:18:24 +0000 Subject: Update ReadMe.md --- ReadMe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReadMe.md b/ReadMe.md index 201e6406..1136c28b 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -20,6 +20,6 @@ And execute: make h-core-- ``` -You'd also need The SDK and MinGW, to build and link the components. For Tools look at `Public/Tools` +You'd also need The SDK and MinGW, to build and link the components. For Tools look at `Private/Tools` ##### Copyright, Mahrouss Logic, all rights reserved. -- cgit v1.2.3 From 64c8674bc09985fcf7119cfb2b88cad3081b1603 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 4 Mar 2024 15:06:34 +0000 Subject: Kernel: HAL: Document code and wrote interrupt handler for AMD64. --- Private/HALKit/AMD64/HalACPIManager.cpp | 5 ++++- Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp | 16 ++++++++-------- Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 2 ++ Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx | 13 ++++++++++++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Private/HALKit/AMD64/HalACPIManager.cpp b/Private/HALKit/AMD64/HalACPIManager.cpp index 20cbd327..945ae6d8 100644 --- a/Private/HALKit/AMD64/HalACPIManager.cpp +++ b/Private/HALKit/AMD64/HalACPIManager.cpp @@ -10,7 +10,7 @@ namespace HCore { ACPIManager::ACPIManager(voidPtr rsdPtr) : m_Rsdp(rsdPtr), m_Entries(0) { - RSDP *_rsdPtr = reinterpret_cast(this->m_Rsdp); + volatile RSDP *_rsdPtr = reinterpret_cast(this->m_Rsdp); MUST_PASS(_rsdPtr); MUST_PASS(_rsdPtr->Revision >= 2); @@ -19,6 +19,7 @@ ACPIManager::ACPIManager(voidPtr rsdPtr) : m_Rsdp(rsdPtr), m_Entries(0) { void ACPIManager::Shutdown() {} void ACPIManager::Reset() {} +/// @brief Finds a descriptor table inside ACPI XSDT. ErrorOr ACPIManager::Find(const char *signature) { MUST_PASS(m_Rsdp); @@ -61,6 +62,8 @@ bool ACPIManager::Checksum(const char *checksum, SSizeT len) { return chr == 0; } +/// Custom to the virtual machine, you'll need to parse the MADT instead. + void rt_shutdown_acpi_qemu_20(void) { HAL::Out16(0xb004, 0x2000); } void rt_shutdown_acpi_qemu_30_plus(void) { HAL::Out16(0x604, 0x2000); } diff --git a/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp b/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp index 90c29ea3..5fe39225 100644 --- a/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp +++ b/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp @@ -14,13 +14,13 @@ EXTERN_C void idt_handle_gpf(HCore::UIntPtr rsp) { HCore::kcout << HCore::StringBuilder::FromInt("rsp{%}", rsp); HCore::kcout - << "General Protection Fault, caused by " + << "HCoreKrnl: General Protection Fault, caused by " << HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } -extern "C" void idt_handle_scheduler(HCore::UIntPtr rsp) { +EXTERN_C void idt_handle_scheduler(HCore::UIntPtr rsp) { HCore::kcout << HCore::StringBuilder::FromInt("rsp{%}", rsp); HCore::kcout @@ -34,37 +34,37 @@ extern "C" void idt_handle_scheduler(HCore::UIntPtr rsp) { } } -extern "C" void idt_handle_pf(HCore::UIntPtr rsp) { +EXTERN_C void idt_handle_pf(HCore::UIntPtr rsp) { HCore::kcout << HCore::StringBuilder::FromInt("rsp{%}", rsp); MUST_PASS(HCore::ProcessManager::Shared().Leak().GetCurrent()); HCore::kcout - << "Segmentation Fault, caused by " + << "HCoreKrnl: Segmentation Fault, caused by " << HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } -extern "C" void idt_handle_math(HCore::UIntPtr rsp) { +EXTERN_C void idt_handle_math(HCore::UIntPtr rsp) { HCore::kcout << HCore::StringBuilder::FromInt("rsp{%}", rsp); MUST_PASS(HCore::ProcessManager::Shared().Leak().GetCurrent()); HCore::kcout - << "Math error, caused by " + << "HCoreKrnl: Math error, caused by " << HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } -extern "C" void idt_handle_generic(HCore::UIntPtr rsp) { +EXTERN_C void idt_handle_generic(HCore::UIntPtr rsp) { HCore::kcout << HCore::StringBuilder::FromInt("sp{%}", rsp); MUST_PASS(HCore::ProcessManager::Shared().Leak().GetCurrent()); HCore::kcout - << "Execution error, caused by " + << "HCoreKrnl: Execution error, caused by " << HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName(); HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); diff --git a/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index bf122415..fa68ef4a 100644 --- a/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Private/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -11,6 +11,8 @@ //! NOTE: fGSI stands 'Field Global System Interrupt' +/////////////////////////////////////////////////////////////////////////////////////// + namespace HCore::HAL { constexpr Int32 kThreadAPIC = 0; constexpr Int32 kThreadLAPIC = 1; diff --git a/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx b/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx index b43ad8c0..0f89d208 100644 --- a/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx +++ b/Private/HALKit/AMD64/HalInterruptHandlerAMD64.cxx @@ -9,11 +9,22 @@ #include -/// @brief AMD64 Interrupt handler redirects interrupts to their handlers. +/// @brief Interrupt handler 21h, 10h +/// @param rsp stack pointer. + +#define kInterruptIdAlt 0x10 +#define kInterruptId 0x21 + EXTERN_C ATTRIBUTE(naked) HCore::UIntPtr rt_handle_interrupts(HCore::UIntPtr rsp) { HCore::HAL::StackFramePtr stackPtr = reinterpret_cast(rsp); HCORE_UNUSED(stackPtr); + if (stackPtr->IntNum == kInterruptId || + stackPtr->IntNum == kInterruptIdAlt) { + /// Do system call TODO + + } + return rsp; } \ No newline at end of file -- cgit v1.2.3