From 1b072a1dc14d67c9a64028d515f60c715544fcd4 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 21 Feb 2024 19:48:01 +0100 Subject: Kernel: Got it up and running. Signed-off-by: Amlal El Mahrouss --- Private/ArchKit/ArchKit.hpp | 9 ++--- Private/HALKit/AMD64/HalDebugOutput.cxx | 12 +++++- Private/HALKit/AMD64/HalHardwareInit.cpp | 13 ++---- Private/KernelKit/DebugOutput.hpp | 10 ++++- .../NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx | 4 ++ Private/NewBoot/Source/RuntimeMain.cxx | 17 +++----- Private/Source/KernelHeap.cxx | 13 +++--- Private/Source/KernelMain.cxx | 47 ++++++++++++++++++++++ Private/Source/PageManager.cxx | 11 +++++ Private/Source/Pmm.cxx | 18 +++++---- Private/Source/RuntimeMain.cxx | 45 --------------------- Private/makefile | 2 +- Public/Kits/SystemKit/XIFF.hxx | 5 ++- 13 files changed, 116 insertions(+), 90 deletions(-) create mode 100644 Private/Source/KernelMain.cxx delete mode 100644 Private/Source/RuntimeMain.cxx diff --git a/Private/ArchKit/ArchKit.hpp b/Private/ArchKit/ArchKit.hpp index ae63c2a1..20d83ecc 100644 --- a/Private/ArchKit/ArchKit.hpp +++ b/Private/ArchKit/ArchKit.hpp @@ -62,11 +62,10 @@ extern HCore::Array kSyscalls; -extern "C" HCore::Void rt_wait_for_io(); -extern "C" HCore::Void rt_syscall_handle(HCore::HAL::StackFramePtr stackFrame); -extern "C" HCore::HAL::StackFramePtr rt_get_current_context(); -extern "C" HCore::Void rt_do_context_switch( - HCore::HAL::StackFramePtr stackFrame); +EXTERN_C HCore::Void rt_wait_for_io(); +EXTERN_C HCore::Void rt_syscall_handle(HCore::HAL::StackFramePtr stackFrame); +EXTERN_C HCore::HAL::StackFramePtr rt_get_current_context(); +EXTERN_C HCore::Void rt_do_context_switch(HCore::HAL::StackFramePtr stackFrame); inline HCore::VoidPtr kKernelVirtualStart; inline HCore::UIntPtr kKernelVirtualSize; diff --git a/Private/HALKit/AMD64/HalDebugOutput.cxx b/Private/HALKit/AMD64/HalDebugOutput.cxx index ba8770fe..635537ae 100644 --- a/Private/HALKit/AMD64/HalDebugOutput.cxx +++ b/Private/HALKit/AMD64/HalDebugOutput.cxx @@ -42,7 +42,7 @@ bool serial_init() { ke_stop(RUNTIME_CHECK_HANDSHAKE); } - kReady = kStateReady; + kState = kStateReady; // If serial is not faulty set it in normal operation mode // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) @@ -54,6 +54,8 @@ bool serial_init() { } // namespace Detail void ke_io_print(const char *bytes) { + Detail::serial_init(); + if (!bytes || Detail::kState != kStateReady) return; if (*bytes == 0) return; @@ -67,8 +69,14 @@ void ke_io_print(const char *bytes) { ++index; } + HAL::Out8(Detail::PORT, '\r'); + HAL::Out8(Detail::PORT, '\n'); Detail::kState = kStateReady; } -TerminalDevice kcout(HCore::ke_io_print, nullptr); +TerminalDevice TerminalDevice::Shared() noexcept { + TerminalDevice out(HCore::ke_io_print, nullptr); + return out; +} + } // namespace HCore diff --git a/Private/HALKit/AMD64/HalHardwareInit.cpp b/Private/HALKit/AMD64/HalHardwareInit.cpp index adcacce4..2a44f696 100644 --- a/Private/HALKit/AMD64/HalHardwareInit.cpp +++ b/Private/HALKit/AMD64/HalHardwareInit.cpp @@ -13,20 +13,15 @@ extern "C" HCore::VoidPtr __EXEC_IVT; -static HCore::HAL::Register64* kIdtRegister; -static HCore::HAL::Register64* kGdtRegister; - namespace HCore { bool ke_init_hal() { - kIdtRegister = nullptr; - kGdtRegister = nullptr; + HCore::HAL::Register64 kIdtRegister; - kIdtRegister = new HCore::HAL::Register64(); - kIdtRegister->Base = (UIntPtr)__EXEC_IVT; - kIdtRegister->Limit = sizeof(HAL::Register64) * 256; + kIdtRegister.Base = (UIntPtr)__EXEC_IVT; + kIdtRegister.Limit = sizeof(HAL::Register64) * 256; HAL::IDTLoader idt; - idt.Load(*kIdtRegister); + idt.Load(kIdtRegister); return true; } diff --git a/Private/KernelKit/DebugOutput.hpp b/Private/KernelKit/DebugOutput.hpp index 6e651924..235163ac 100644 --- a/Private/KernelKit/DebugOutput.hpp +++ b/Private/KernelKit/DebugOutput.hpp @@ -28,11 +28,17 @@ class TerminalDevice final : public DeviceInterface { TerminalDevice &operator=(const TerminalDevice &) = default; TerminalDevice(const TerminalDevice &) = default; + + static TerminalDevice Shared() noexcept; }; namespace Detail { bool serial_init(); } - -extern TerminalDevice kcout; } // namespace HCore + +#ifdef kcout +#undef kcout +#endif // ifdef kcout + +#define kcout TerminalDevice::Shared() diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx index 5a4a2445..e6c962b1 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx @@ -18,6 +18,8 @@ #include #include +#ifndef __BUNDLE_KERNEL__ + extern "C" void rt_halt() { asm volatile("hlt"); } extern "C" void rt_cli() { asm volatile("cli"); } @@ -32,6 +34,8 @@ extern "C" void rt_std() { asm volatile("std"); } extern "C" void ___chkstk_ms(void) {} +#endif + namespace EFI { Void Stop() noexcept { while (true) { diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx index 38eb0ea8..4dd0c437 100644 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ b/Private/NewBoot/Source/RuntimeMain.cxx @@ -18,7 +18,7 @@ #ifdef __x86_64__ #include #else -#error Unknown CPU. +#error This CPU is unknown. #endif // ifdef __x86_64__ #define kHeadersSz \ @@ -43,7 +43,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, const char strDate[] = __DATE__; - writer.WriteString(L"HCoreLdr: Build date: "); + writer.WriteString(L"HCoreLdr: Build: "); for (auto& ch : strDate) writer.WriteCharacter(ch); @@ -64,7 +64,8 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, if (ptrHdr && ptrHdr->mMachine == EFI::Platform() && ptrHdr->mMagic == kPeMagic) { - if (ptrHdr->mNumberOfSections > 1) { + /// sections must be at least 3. + if (ptrHdr->mNumberOfSections >= 3) { ExecOptionalHeaderPtr optHdr = reinterpret_cast( ptrHdr + sizeof(ExecHeader)); @@ -122,15 +123,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, SystemTable->FirmwareVendor, handoverHdrPtr->f_FirmwareVendorLen); - writer.WriteString(L"HCoreLdr: Leaving it to kernel...\r\n"); - - EFI::ExitBootServices(MapKey, ImageHandle); - - HCore::HEL::HandoverProc proc = - reinterpret_cast( - optHdr->mAddressOfEntryPoint); - - proc(handoverHdrPtr); + writer.WriteString(L"HCoreLdr: Booting HCore...\r\n"); EFI::Stop(); diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 72aa2d67..38b279a7 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -9,6 +9,8 @@ #include +#include "KernelKit/DebugOutput.hpp" + //! @file KernelHeap.cpp //! @brief Kernel allocator. @@ -18,7 +20,7 @@ namespace HCore { static Ref kWrapperList[kMaxWrappers]; static SizeT kWrapperCount = 0UL; static Ref kLastWrapper; -static Pmm kPmm; +static PageManager kPageManager; namespace Detail { STATIC voidPtr ke_find_heap(const SizeT &sz, const bool rw, const bool user) { @@ -45,7 +47,7 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { if (auto ptr = Detail::ke_find_heap(sz, rw, user); ptr) return ptr; - Ref wrapper = kPmm.RequestPage(user, rw); + Ref wrapper = kPageManager.Request(user, rw, false); if (wrapper) { kLastWrapper = wrapper; @@ -67,7 +69,7 @@ Int32 ke_delete_ke_heap(voidPtr ptr) { const UIntPtr virtualAddress = reinterpret_cast(ptr); if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { - if (kPmm.FreePage(kLastWrapper)) { + if (kPageManager.Free(kLastWrapper)) { kLastWrapper->NoExecute(false); return true; } @@ -82,7 +84,7 @@ Int32 ke_delete_ke_heap(voidPtr ptr) { wrapper = kWrapperList[indexWrapper]; // if page is no more, then mark it also as non executable. - if (kPmm.FreePage(wrapper)) { + if (kPageManager.Free(wrapper)) { wrapper->NoExecute(false); return true; } @@ -124,6 +126,7 @@ Boolean kernel_valid_ptr(voidPtr ptr) { Void ke_init_ke_heap() noexcept { kWrapperCount = 0UL; Ref kLastWrapper = Ref(nullptr); - Pmm kPmm = Pmm(); + + kcout << "KernelHeap: Init [OK]\r\n"; } } // namespace HCore diff --git a/Private/Source/KernelMain.cxx b/Private/Source/KernelMain.cxx new file mode 100644 index 00000000..1d52967d --- /dev/null +++ b/Private/Source/KernelMain.cxx @@ -0,0 +1,47 @@ +/* + * ======================================================== + * + * HCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "NewKit/Utils.hpp" + +EXTERN_C void RuntimeMain( + HCore::HEL::HandoverInformationHeader* HandoverHeader) { + /// Setup kernel globals. + kKernelVirtualSize = HandoverHeader->f_VirtualSize; + kKernelVirtualStart = HandoverHeader->f_VirtualStart; + + kKernelPhysicalSize = HandoverHeader->f_VirtualSize; + kKernelPhysicalStart = HandoverHeader->f_VirtualStart; + + /// Setup base page. + HCore::HAL::hal_page_base((HCore::UIntPtr)kKernelVirtualStart); + + /// Init memory managers. + HCore::ke_init_ke_heap(); + HCore::ke_init_heap(); + + /// Init the HAL. + MUST_PASS(HCore::ke_init_hal()); + + /// Mount a New partition. + HCore::IFilesystemManager::Mount(new HCore::NewFilesystemManager()); + HCore::PEFLoader img("/System/HCoreShell.exe"); + + /// Run the shell. + if (!HCore::Utils::execute_from_image(img)) { + HCore::ke_stop(RUNTIME_CHECK_BOOTSTRAP); + } +} diff --git a/Private/Source/PageManager.cxx b/Private/Source/PageManager.cxx index b4d9ed8e..b3a168d3 100644 --- a/Private/Source/PageManager.cxx +++ b/Private/Source/PageManager.cxx @@ -10,6 +10,8 @@ #include #include +#include "NewKit/String.hpp" + #ifdef __x86_64__ #include #endif // ifdef __x86_64__ @@ -47,6 +49,13 @@ PTEWrapper::~PTEWrapper() { } void PTEWrapper::FlushTLB(Ref &pm) { + volatile PTE *virtAddr = static_cast(virtAddr); + + virtAddr->Present = this->m_Present; + virtAddr->ExecDisable = this->m_ExecDisable; + virtAddr->Rw = this->m_Rw; + virtAddr->User = this->m_User; + pm.Leak().FlushTLB(this->m_VirtAddr); } @@ -75,6 +84,8 @@ PTEWrapper *PageManager::Request(Boolean Rw, Boolean User, return nullptr; } + PageTableEntry->NoExecute(ExecDisable); + *PageTableEntry = PTEWrapper{Rw, User, ExecDisable, Detail::create_page_wrapper(Rw, User)}; return PageTableEntry; diff --git a/Private/Source/Pmm.cxx b/Private/Source/Pmm.cxx index 7eb6875e..58f3e4a2 100644 --- a/Private/Source/Pmm.cxx +++ b/Private/Source/Pmm.cxx @@ -11,22 +11,24 @@ #include namespace HCore { -Pmm::Pmm() : m_PageManager() { - kcout << "[PMM] Allocate PageMemoryManager\r\n"; -} +Pmm::Pmm() : m_PageManager() { kcout << "[PMM] Allocate PageMemoryManager"; } Pmm::~Pmm() = default; /* If this returns Null pointer, enter emergency mode */ Ref Pmm::RequestPage(Boolean user, Boolean readWrite) { - if (m_PageManager) { - PTEWrapper *pt = m_PageManager.Leak().Request(user, readWrite, true); + PTEWrapper *pt = m_PageManager.Leak().Request(user, readWrite, false); + + if (pt) { + pt->m_Present = true; + pt->FlushTLB(m_PageManager); + + kcout << "[PMM]: Allocation was successful."; - if (pt) return Ref(pt); + return Ref(pt); } - kcout << "[Pmm::RequestPage] Ref could not be created! " - "m_PageManager is nullptr!\r\n"; + kcout << "[PMM]: Allocation failure."; return {}; } diff --git a/Private/Source/RuntimeMain.cxx b/Private/Source/RuntimeMain.cxx deleted file mode 100644 index abbfb749..00000000 --- a/Private/Source/RuntimeMain.cxx +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ======================================================== - * - * HCore - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include -#include -#include -#include -#include -#include -#include - -extern "C" void RuntimeMain( - HCore::HEL::HandoverInformationHeader* HandoverHeader) { - /// Setup kernel globals. - kKernelVirtualSize = HandoverHeader->f_VirtualSize; - kKernelVirtualStart = HandoverHeader->f_VirtualStart; - - kKernelPhysicalSize = HandoverHeader->f_VirtualSize; - kKernelPhysicalStart = HandoverHeader->f_VirtualStart; - - /// Setup base page. - HCore::HAL::hal_page_base((HCore::UIntPtr)kKernelVirtualStart); - - /// Init memory managers. - HCore::ke_init_ke_heap(); - HCore::ke_init_heap(); - - /// Init the HAL. - MUST_PASS(HCore::ke_init_hal()); - - /// Mount a New partition. - HCore::IFilesystemManager::Mount(new HCore::NewFilesystemManager()); - HCore::PEFLoader img("/System/HCoreShell.exe"); - - /// Run the shell. - if (!HCore::Utils::execute_from_image(img)) { - HCore::ke_stop(RUNTIME_CHECK_BOOTSTRAP); - } -} diff --git a/Private/makefile b/Private/makefile index 1296117b..9aab13b8 100644 --- a/Private/makefile +++ b/Private/makefile @@ -5,7 +5,7 @@ CC = x86_64-w64-mingw32-g++ LD = x86_64-w64-mingw32-ld -CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__USE_NEWFS__ -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ +CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__DEBUG__ -D__USE_NEWFS__ -D__HAVE_HCORE_APIS__ -D__HCORE__ -I../ -I./ -I$(HOME)/ ASM = nasm ASMFLAGS = -f win64 LDFLAGS = -e Main -shared --subsystem=17 diff --git a/Public/Kits/SystemKit/XIFF.hxx b/Public/Kits/SystemKit/XIFF.hxx index fb8b441f..665a8b67 100644 --- a/Public/Kits/SystemKit/XIFF.hxx +++ b/Public/Kits/SystemKit/XIFF.hxx @@ -2,7 +2,8 @@ (C) Mahrouss Logic ===========================================*/ -#pragma once +#ifndef __XIFF__ +#define __XIFF__ /** --------------------------------------------------- @@ -31,3 +32,5 @@ struct PACKED XiffHeader { #define kXIFFAudio "XAFF" #define kXIFFInstaller "XnFF" #define kXIFFGeneric "XIFF" + +#endif // ifndef __XIFF__ -- cgit v1.2.3