From 6d7e78543509af471568cf698c58a9f526dba129 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 10:25:09 +0100 Subject: See below. Stage 1: Create API for Partition read 'EFIPartitionManager' is the main class behind it, there is still some design to do. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/EFILib.hxx | 9 +++++++++ Private/EFIKit/EFiLib.cxx | 14 ++++++++++++++ Private/KernelKit/DriveManager.hpp | 1 + Private/KernelKit/FileManager.hpp | 1 + Private/KernelKit/PEF.hpp | 1 + 5 files changed, 26 insertions(+) create mode 100644 Private/EFIKit/EFiLib.cxx diff --git a/Private/EFIKit/EFILib.hxx b/Private/EFIKit/EFILib.hxx index e7b135ef..3f041e37 100644 --- a/Private/EFIKit/EFILib.hxx +++ b/Private/EFIKit/EFILib.hxx @@ -61,6 +61,15 @@ inline void KeRuntimeStop(const EfiCharType* ErrorCode, EFI::Stop(); } +enum { + kPartEPM, + kPartGPT, + kPartMBR, + kPartCnt, +}; + +class EFIPartitionManager final {}; + #ifdef __BOOTLOADER__ #include #endif // IF TARGET=BOOTLOADER diff --git a/Private/EFIKit/EFiLib.cxx b/Private/EFIKit/EFiLib.cxx new file mode 100644 index 00000000..b90cfa3a --- /dev/null +++ b/Private/EFIKit/EFiLib.cxx @@ -0,0 +1,14 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + + File: EFILib.hxx + Purpose: EFI C++ library + + Revision History: + + 01/02/24: Added file (amlel) + +------------------------------------------- */ + +#include diff --git a/Private/KernelKit/DriveManager.hpp b/Private/KernelKit/DriveManager.hpp index da478a45..223414d2 100644 --- a/Private/KernelKit/DriveManager.hpp +++ b/Private/KernelKit/DriveManager.hpp @@ -29,6 +29,7 @@ enum { kEPMDrive = 0x11, // Explicit Partition Map. kEPTDrive = 0x12, // ESP w/ EPM partition. kMBRDrive = 0x13, // IBM PC classic partition scheme + kDriveCnt = 9, }; typedef Int64 DriveID; diff --git a/Private/KernelKit/FileManager.hpp b/Private/KernelKit/FileManager.hpp index 6ed713c4..b65bdfe1 100644 --- a/Private/KernelKit/FileManager.hpp +++ b/Private/KernelKit/FileManager.hpp @@ -39,6 +39,7 @@ enum { kFileReadAll = 101, kFileReadChunk = 102, kFileWriteChunk = 103, + kFileIOCnt = (kFileWriteChunk - kFileWriteAll) + 1, }; typedef VoidPtr NodePtr; diff --git a/Private/KernelKit/PEF.hpp b/Private/KernelKit/PEF.hpp index afa43968..8c67c6ae 100644 --- a/Private/KernelKit/PEF.hpp +++ b/Private/KernelKit/PEF.hpp @@ -31,6 +31,7 @@ enum { kPefArchRISCV, kPefArch64x0, /* 64x000. */ kPefArch32x0, + kPefArchCount = (kPefArch32x0 - kPefArchIntel86S) + 1, kPefArchInvalid = 0xFF, }; -- cgit v1.2.3 From 54a426e7d11eb12a8c3710f3632b7084edf423fd Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 12:46:54 +0100 Subject: See below. - Implement Framebuffer object. - Print Firmware name in NewBoot. Signed-off-by: Amlal El Mahrouss --- Private/KernelKit/Framebuffer.hpp | 82 ++++++++++++------------- Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx | 4 +- Private/Source/Framebuffer.cxx | 48 +++++++-------- 3 files changed, 66 insertions(+), 68 deletions(-) diff --git a/Private/KernelKit/Framebuffer.hpp b/Private/KernelKit/Framebuffer.hpp index 6dc94a3b..8ad23571 100644 --- a/Private/KernelKit/Framebuffer.hpp +++ b/Private/KernelKit/Framebuffer.hpp @@ -13,47 +13,45 @@ #include #include -namespace HCore -{ - enum class FramebufferColorKind : UChar - { - RGB32, - RGB16, - RGB8, - INVALID, - }; - - class FramebufferContext final - { - public: - UIntPtr m_Base; - UIntPtr m_Bpp; - UInt m_Width; - UInt m_Height; - - }; - - class Framebuffer final - { - public: - Framebuffer(Ref &addr); - ~Framebuffer(); - - Framebuffer &operator=(const Framebuffer &) = delete; - Framebuffer(const Framebuffer &) = default; - - volatile UIntPtr* operator[](const UIntPtr &width_and_height); - operator bool(); - - const FramebufferColorKind& Color(const FramebufferColorKind &colour = FramebufferColorKind::INVALID); - - Ref& Leak(); - - private: - Ref m_FrameBufferAddr; - FramebufferColorKind m_Colour; - - }; -} // namespace HCore +namespace HCore { +enum class FramebufferColorKind : UChar { + RGB32, + RGB16, + RGB8, + INVALID, +}; + +class FramebufferContext final { + public: + UIntPtr m_Base; + UIntPtr m_Bpp; + UInt m_Width; + UInt m_Height; +}; + +class Framebuffer final { + public: + Framebuffer(Ref &addr) : m_FrameBufferAddr(addr) {} + ~Framebuffer() {} + + Framebuffer &operator=(const Framebuffer &) = delete; + Framebuffer(const Framebuffer &) = default; + + volatile UIntPtr *operator[](const UIntPtr &width_and_height); + + operator bool() { + return m_FrameBufferAddr && m_Colour != FramebufferColorKind::INVALID; + } + + const FramebufferColorKind &Color( + const FramebufferColorKind &colour = FramebufferColorKind::INVALID); + + Ref &Leak(); + + private: + Ref m_FrameBufferAddr; + FramebufferColorKind m_Colour; +}; +} // namespace HCore #endif /* ifndef __INC_FB_HPP__ */ diff --git a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx index d1d230b6..d06338b5 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx @@ -19,7 +19,9 @@ EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, KeInitEFI(SystemTable); BTextWriter writer; - writer.WriteString(L"HCoreLdr: Booting from \\Volume0\\...") + writer.WriteString(L"HCoreLdr: Firmware: ") + .WriteString(SystemTable->FirmwareVendor) + .WriteString(L"\r\nHCoreLdr: Booting on \\Volume0\\ (FAT32)") .WriteString(L"\r\n"); UInt64 mapKey = 0; diff --git a/Private/Source/Framebuffer.cxx b/Private/Source/Framebuffer.cxx index 251cdb93..3b8fa229 100644 --- a/Private/Source/Framebuffer.cxx +++ b/Private/Source/Framebuffer.cxx @@ -1,36 +1,34 @@ -/* - * ======================================================== - * - * HCore - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ +/* ------------------------------------------- -#include + Copyright Mahrouss Logic -namespace HCore { -Framebuffer::Framebuffer(HCore::Ref& addr) - : m_FrameBufferAddr(addr), m_Colour(FramebufferColorKind::RGB32) {} + File: Framebuffer.cxx + Purpose: EFI C++ library -Framebuffer::~Framebuffer() = default; + Revision History: -volatile UIntPtr* Framebuffer::operator[](const UIntPtr& width_and_height) { - if (m_FrameBufferAddr) - return reinterpret_cast( - m_FrameBufferAddr->m_Base + width_and_height); + 01/02/24: Added file (amlel) - return nullptr; -} +------------------------------------------- */ -Ref& Framebuffer::Leak() { return m_FrameBufferAddr; } +#include -Framebuffer::operator bool() { return m_FrameBufferAddr; } +using namespace HCore; -const FramebufferColorKind& Framebuffer::Color( - const FramebufferColorKind& colour) { - if (colour != FramebufferColorKind::INVALID) m_Colour = colour; +volatile UIntPtr *Framebuffer::operator[](const UIntPtr &width_and_height) { + return (UIntPtr *)(m_FrameBufferAddr->m_Base * width_and_height); +} + +const FramebufferColorKind &Framebuffer::Color( + const FramebufferColorKind &colour) { + if (m_Colour != FramebufferColorKind::INVALID && + colour != FramebufferColorKind::INVALID) { + m_Colour = colour; + } return m_Colour; } -} // namespace HCore + +Ref &Framebuffer::Leak() { + return this->m_FrameBufferAddr; +} -- cgit v1.2.3 From 1bb395c7ceae7d06448e2ac6f60e91ffffe6b091 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 12:48:39 +0100 Subject: HCR-9 Initial commit of branch for Jira. --- BUG_LIST.TXT | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BUG_LIST.TXT b/BUG_LIST.TXT index 229cc5dd..1fa4e00a 100644 --- a/BUG_LIST.TXT +++ b/BUG_LIST.TXT @@ -1 +1,3 @@ ADD THE BUGS HERE: + +None -- cgit v1.2.3 From 4efd7b8a6608a9299ef8cc750c264a3be0cb12e7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 14:41:18 +0100 Subject: HCR-9 : Update EFIKit, working on Volume API. HCR-9 Related: - New EfiMountVolume, EfiUnmountVolume. Kernel Related: - Update Shared Object API, it was lacking a cleanup routine. - Add __mh_purecall as a C linked symbol for unknown symbols. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/EFILib.hxx | 6 +- Private/HALKit/AMD64/DebugManager.asm | 2 - Private/KernelKit/PEFSharedObject.hxx | 39 ++++++------ Private/NewKit/Macros.hpp | 12 ++-- Private/Source/PEFCodeManager.cxx | 2 +- Private/Source/PEFSharedObjectMain.cxx | 58 ------------------ Private/Source/PEFSharedObjectRT.cxx | 107 +++++++++++++++++++++++++++++++++ 7 files changed, 140 insertions(+), 86 deletions(-) delete mode 100644 Private/Source/PEFSharedObjectMain.cxx create mode 100644 Private/Source/PEFSharedObjectRT.cxx diff --git a/Private/EFIKit/EFILib.hxx b/Private/EFIKit/EFILib.hxx index 3f041e37..fcd5df7d 100644 --- a/Private/EFIKit/EFILib.hxx +++ b/Private/EFIKit/EFILib.hxx @@ -68,7 +68,11 @@ enum { kPartCnt, }; -class EFIPartitionManager final {}; +typedef EfiHandlePtr* EfiFilesystemHandlePtr; + +EfiFilesystemHandlePtr EfiMountVolume(const EfiCharType* MountPath, + Int32 PartitionType); +Boolean EfiUnmountVolume(EfiFilesystemHandlePtr Handle); #ifdef __BOOTLOADER__ #include diff --git a/Private/HALKit/AMD64/DebugManager.asm b/Private/HALKit/AMD64/DebugManager.asm index cf3a4539..5cc0cd10 100644 --- a/Private/HALKit/AMD64/DebugManager.asm +++ b/Private/HALKit/AMD64/DebugManager.asm @@ -14,8 +14,6 @@ __rt_debug_record_table: db "DebugMgr/HCore", 0xa, 0xd, 0 - ;; User Data goes there - resb 64 __rt_debug_int_3: push 0x6677 ;; Debug check error ;; then halt and goes back to L0, thens halts... diff --git a/Private/KernelKit/PEFSharedObject.hxx b/Private/KernelKit/PEFSharedObject.hxx index af90858e..263f08f4 100644 --- a/Private/KernelKit/PEFSharedObject.hxx +++ b/Private/KernelKit/PEFSharedObject.hxx @@ -16,10 +16,13 @@ #include namespace HCore { - /** - * @brief Shared Library class - * Load library from this class - */ +/// @brief Pure implementation, missing method/function handler. +extern "C" void __mh_purecall(void); + +/** + * @brief Shared Library class + * Load library from this class + */ class SharedObject final { public: struct SharedObjectTraits final { @@ -44,9 +47,7 @@ class SharedObject final { public: void Mount(SharedObjectTraits *to_mount) { - if (!to_mount || - !to_mount->fImageObject) - return; + if (!to_mount || !to_mount->fImageObject) return; fMounted = to_mount; @@ -65,17 +66,18 @@ class SharedObject final { }; template - SymbolType Load(const char *symbol_name) { - auto ret = reinterpret_cast( - fLoader->FindSymbol(symbol_name, kPefCode)); + SymbolType Load(const char *symbol_name, SizeT len, Int32 kind) { + if (symbol_name == nullptr || *symbol_name == 0) return nullptr; + if (len > kPathLen || len < 1) return nullptr; - if (!ret) - ret = reinterpret_cast( - fLoader->FindSymbol(symbol_name, kPefData)); + auto ret = + reinterpret_cast(fLoader->FindSymbol(symbol_name, kind)); - if (!ret) - ret = reinterpret_cast( - fLoader->FindSymbol(symbol_name, kPefZero)); + if (!ret) { + if (kind == kPefCode) return (VoidPtr)__mh_purecall; + + return nullptr; + } return ret; } @@ -84,10 +86,7 @@ class SharedObject final { PEFLoader *fLoader{nullptr}; }; -inline void hcore_pure_call(void) { - // virtual placeholder. - return; -} +typedef SharedObject *SharedObjectPtr; } // namespace HCore #endif /* ifndef __KERNELKIT_SHARED_OBJECT_HXX__ */ diff --git a/Private/NewKit/Macros.hpp b/Private/NewKit/Macros.hpp index 02be7180..94fd8fe8 100644 --- a/Private/NewKit/Macros.hpp +++ b/Private/NewKit/Macros.hpp @@ -65,7 +65,7 @@ #endif #ifndef ENUM_STRING -#define ENUM_STRING(NAME, VAL) constexpr const char *NAME = VAL +#define ENUM_STRING(NAME, VAL) inline constexpr const char *NAME = VAL #endif #ifndef END_STRING_ENUM @@ -76,9 +76,13 @@ #define Alloca(Sz) __builtin_alloca(Sz) #endif // #ifndef Alloca -#ifndef CantReach -#define CantReach() __builtin_unreachable() +#ifndef CANT_REACH +#define CANT_REACH() __builtin_unreachable() #endif #define kBadPtr 0xFBFBFBFBFBFBFBFB -#define kmaxAddr 0xFFFFFFFFFFFFFFFF +#define kMaxAddr 0xFFFFFFFFFFFFFFFF +#define kPathLen 255 + +#define PACKED ATTRIBUTE(packed) +#define NO_EXEC ATTRIBUTE(noexec) diff --git a/Private/Source/PEFCodeManager.cxx b/Private/Source/PEFCodeManager.cxx index 8804d1ca..63e8659f 100644 --- a/Private/Source/PEFCodeManager.cxx +++ b/Private/Source/PEFCodeManager.cxx @@ -7,9 +7,9 @@ * ======================================================== */ -#include #include #include +#include #include #include #include diff --git a/Private/Source/PEFSharedObjectMain.cxx b/Private/Source/PEFSharedObjectMain.cxx deleted file mode 100644 index c1803312..00000000 --- a/Private/Source/PEFSharedObjectMain.cxx +++ /dev/null @@ -1,58 +0,0 @@ -/* - * ======================================================== - * - * HCore - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include -#include -#include -#include -#include -#include - -using namespace HCore; - -/***********************************************************************************/ -/// @file SharedObjectEntry.cxx -/// @brief Shared Object Init code. -/***********************************************************************************/ - -/***********************************************************************************/ -/* @brief Allocate new library to be added to the lookup table. - */ -/***********************************************************************************/ - -extern "C" SharedObject *__LibMain(VoidPtr image) { - SharedObject *library = hcore_tls_new_class(); - - if (!library) { - kcout << "__LibMain: Out of Memory!\n"; - ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); - - return nullptr; - } - - library->Mount(hcore_tls_new_class()); - - if (!library->Get()) { - kcout << "__LibMain: Out of Memory!\n"; - ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); - - return nullptr; - } - - library->Get()->fImageObject = - ProcessManager::Shared().Leak().GetCurrent().Leak().Image; - - library->Get()->fImageEntrypointOffset = library->Load(kPefStart); - - kcout << "__LibMain: Done jumping to library...\n"; - - return library; -} - -/***********************************************************************************/ diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx new file mode 100644 index 00000000..0b98834c --- /dev/null +++ b/Private/Source/PEFSharedObjectRT.cxx @@ -0,0 +1,107 @@ +/* + * ======================================================== + * + * HCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include +#include +#include +#include +#include +#include + +#include "NewKit/RuntimeCheck.hpp" + +/* ------------------------------------------- + + Revision History: + + 01/02/24: Rework shared library ABI, except a __LibInit and __LibFini + (amlel) + + ------------------------------------------- */ + +using namespace HCore; + +/***********************************************************************************/ +/// @file SharedObjectRT.cxx +/// @brief Shared Object runtime. +/***********************************************************************************/ + +/***********************************************************************************/ +/* @brief Allocates a new library. */ +/***********************************************************************************/ + +extern "C" SharedObject *__LibInit() { + SharedObject *library = hcore_tls_new_class(); + + if (!library) { + kcout << "__LibInit: Out of Memory!\n"; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + + return nullptr; + } + + library->Mount(hcore_tls_new_class()); + + if (!library->Get()) { + kcout << "__LibInit: Out of Memory!\n"; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + + return nullptr; + } + + library->Get()->fImageObject = + ProcessManager::Shared().Leak().GetCurrent().Leak().Image; + + if (!library->Get()->fImageObject) { + kcout << "__LibInit: Invalid image!\n"; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + + return nullptr; + } + + library->Get()->fImageEntrypointOffset = + library->Load(kPefStart, string_length(kPefStart, 0), kPefCode); + + kcout << "__LibMain: Task was successful... Returning library...\n"; + + return library; +} + +/***********************************************************************************/ + +/***********************************************************************************/ +/* @brief Frees the library. */ +/* @note Please check if the lib got freed! */ +/* @param SharedObjectPtr the library to free. */ +/***********************************************************************************/ + +extern "C" Void __LibFini(SharedObjectPtr lib, bool *successful) { + MUST_PASS(successful); + + // sanity check (will also trigger a bug check) + if (lib == nullptr) { + kcout << "__LibFini: Invalid image!\n"; + *successful = false; + ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); + } + + delete lib->Get(); + delete lib; + + lib = nullptr; + + *successful = true; +} + +/***********************************************************************************/ + +extern "C" void __mh_purecall(void) { + // virtual placeholder. + return; +} -- cgit v1.2.3 From 69d07f34fce76f89fe11beb7ced130e9aa72078b Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 15:14:39 +0100 Subject: Meta: update string. Signed-off-by: Amlal El Mahrouss --- Private/Source/PEFSharedObjectRT.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx index 0b98834c..018b618e 100644 --- a/Private/Source/PEFSharedObjectRT.cxx +++ b/Private/Source/PEFSharedObjectRT.cxx @@ -68,7 +68,7 @@ extern "C" SharedObject *__LibInit() { library->Get()->fImageEntrypointOffset = library->Load(kPefStart, string_length(kPefStart, 0), kPefCode); - kcout << "__LibMain: Task was successful... Returning library...\n"; + kcout << "__LibInit: Task was successful... Returning library...\n"; return library; } -- cgit v1.2.3 From 26ceef5cccbb40b00a302979ed297243b356feff Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 17:00:17 +0100 Subject: Kernel: Some groundwork done, this is a bumping commit. Signed-off-by: Amlal El Mahrouss --- Internal/.gitkeep | 0 Private/EFIKit/EFiLib.cxx | 14 ----------- Private/KernelKit/DebugOutput.hpp | 48 ++++++++++++++++++-------------------- Private/KernelKit/DriveManager.hpp | 2 +- Private/KernelKit/OSErr.hpp | 31 ++++++++++++------------ Private/NewBoot/Source/EFILib.cxx | 14 +++++++++++ 6 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 Internal/.gitkeep delete mode 100644 Private/EFIKit/EFiLib.cxx create mode 100644 Private/NewBoot/Source/EFILib.cxx diff --git a/Internal/.gitkeep b/Internal/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Private/EFIKit/EFiLib.cxx b/Private/EFIKit/EFiLib.cxx deleted file mode 100644 index b90cfa3a..00000000 --- a/Private/EFIKit/EFiLib.cxx +++ /dev/null @@ -1,14 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - - File: EFILib.hxx - Purpose: EFI C++ library - - Revision History: - - 01/02/24: Added file (amlel) - -------------------------------------------- */ - -#include diff --git a/Private/KernelKit/DebugOutput.hpp b/Private/KernelKit/DebugOutput.hpp index a651cb8b..875884ba 100644 --- a/Private/KernelKit/DebugOutput.hpp +++ b/Private/KernelKit/DebugOutput.hpp @@ -13,28 +13,26 @@ #include #include -namespace HCore -{ - // @brief Emulates a VT100 terminal. - class TerminalDevice final : public DeviceInterface - { - public: - TerminalDevice(void (*print)(const char *), void (*get)(const char *)) : DeviceInterface(print, get) {} - virtual ~TerminalDevice() {} - - /// @brief returns device name (terminal name) - /// @return string type (const char*) - virtual const char* Name() const override { return ("TerminalDevice"); } - - TerminalDevice &operator=(const TerminalDevice &) = default; - TerminalDevice(const TerminalDevice &) = default; - - }; - - namespace Detail - { - bool serial_init(); - } - - extern TerminalDevice kcout; -} // namespace HCore +namespace HCore { +// @brief Emulates a VT100 terminal. +class TerminalDevice final : public DeviceInterface { + public: + TerminalDevice(void (*print)(const char *), void (*get)(const char *)) + : DeviceInterface(print, get) {} + + virtual ~TerminalDevice() {} + + /// @brief returns device name (terminal name) + /// @return string type (const char*) + virtual const char *Name() const override { return ("TerminalDevice"); } + + TerminalDevice &operator=(const TerminalDevice &) = default; + TerminalDevice(const TerminalDevice &) = default; +}; + +namespace Detail { +bool serial_init(); +} + +extern TerminalDevice kcout; +} // namespace HCore diff --git a/Private/KernelKit/DriveManager.hpp b/Private/KernelKit/DriveManager.hpp index 223414d2..ac39c177 100644 --- a/Private/KernelKit/DriveManager.hpp +++ b/Private/KernelKit/DriveManager.hpp @@ -34,7 +34,7 @@ enum { typedef Int64 DriveID; -// Mounted drive. +/// @brief Mounted drive traits. struct DriveTraits final { char fName[kDriveNameLen]; // /System, /Boot, /USBDevice... Int32 fKind; // fMassStorage, fFloppy, fOpticalDisc. diff --git a/Private/KernelKit/OSErr.hpp b/Private/KernelKit/OSErr.hpp index 975de898..62916959 100644 --- a/Private/KernelKit/OSErr.hpp +++ b/Private/KernelKit/OSErr.hpp @@ -11,20 +11,19 @@ #include -namespace HCore -{ - typedef Int32 OSErr; +namespace HCore { +typedef Int32 OSErr; - inline constexpr OSErr kErrorExecutable = 33; - inline constexpr OSErr kErrorExecutableLib = 34; - inline constexpr OSErr kErrorFileNotFound = 35; - inline constexpr OSErr kErrorDirectoryNotFound = 36; - inline constexpr OSErr kErrorDiskReadOnly = 37; - inline constexpr OSErr kErrorDiskIsFull = 38; - inline constexpr OSErr kErrorProcessFault = 39; - inline constexpr OSErr kErrorSocketHangUp = 40; - inline constexpr OSErr kErrorThreadLocalStorage = 41; - inline constexpr OSErr kErrorMath = 42; - inline constexpr OSErr kErrorNoNetwork = 43; - inline constexpr OSErr kErrorHeapOutOfMemory = 44; -} \ No newline at end of file +inline constexpr OSErr kErrorExecutable = 33; +inline constexpr OSErr kErrorExecutableLib = 34; +inline constexpr OSErr kErrorFileNotFound = 35; +inline constexpr OSErr kErrorDirectoryNotFound = 36; +inline constexpr OSErr kErrorDiskReadOnly = 37; +inline constexpr OSErr kErrorDiskIsFull = 38; +inline constexpr OSErr kErrorProcessFault = 39; +inline constexpr OSErr kErrorSocketHangUp = 40; +inline constexpr OSErr kErrorThreadLocalStorage = 41; +inline constexpr OSErr kErrorMath = 42; +inline constexpr OSErr kErrorNoNetwork = 43; +inline constexpr OSErr kErrorHeapOutOfMemory = 44; +} // namespace HCore diff --git a/Private/NewBoot/Source/EFILib.cxx b/Private/NewBoot/Source/EFILib.cxx new file mode 100644 index 00000000..3e30e0d3 --- /dev/null +++ b/Private/NewBoot/Source/EFILib.cxx @@ -0,0 +1,14 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + + File: EFI.cxx + Purpose: EFI Library for NewBoot. + + Revision History: + + 01/02/24: Added file (amlel) + +------------------------------------------- */ + +#include -- cgit v1.2.3 From 6ce7dffe92775f262384a028af233999a7d18048 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Feb 2024 17:24:09 +0100 Subject: Kernel: Really big improvements on Paged Memory Model. Bootloader: Design shift in BFileReader class. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/EFILib.hxx | 6 -- Private/HALKit/AMD64/HalPageAlloc.hpp | 2 +- Private/NewBoot/BootKit/BootKit.hxx | 12 +++ Private/NewBoot/Source/EFILib.cxx | 14 ---- Private/NewBoot/Source/compile_flags.txt | 3 + Private/NewBoot/Source/makefile | 2 +- Private/NewKit/PageManager.hpp | 103 ++++++++++++------------- Private/NewKit/Pmm.hpp | 58 +++++++-------- Private/NewKit/Ref.hpp | 124 +++++++++++++------------------ Private/Source/KernelHeap.cxx | 29 ++++++-- Private/Source/PageManager.cxx | 21 ++++++ Private/Source/Pmm.cxx | 9 +-- 12 files changed, 194 insertions(+), 189 deletions(-) delete mode 100644 Private/NewBoot/Source/EFILib.cxx create mode 100644 Private/NewBoot/Source/compile_flags.txt diff --git a/Private/EFIKit/EFILib.hxx b/Private/EFIKit/EFILib.hxx index fcd5df7d..b08c2866 100644 --- a/Private/EFIKit/EFILib.hxx +++ b/Private/EFIKit/EFILib.hxx @@ -68,12 +68,6 @@ enum { kPartCnt, }; -typedef EfiHandlePtr* EfiFilesystemHandlePtr; - -EfiFilesystemHandlePtr EfiMountVolume(const EfiCharType* MountPath, - Int32 PartitionType); -Boolean EfiUnmountVolume(EfiFilesystemHandlePtr Handle); - #ifdef __BOOTLOADER__ #include #endif // IF TARGET=BOOTLOADER diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp index 809b6a3d..3f9428b9 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.hpp +++ b/Private/HALKit/AMD64/HalPageAlloc.hpp @@ -19,7 +19,7 @@ #define PTE_ALIGN (4096) #endif //! PTE_ALIGN -#define kPagePtrAddress 0x0900000 +#define kPagePtrAddress 0x9000000 extern "C" void flush_tlb(HCore::UIntPtr VirtualAddr); extern "C" void write_cr3(HCore::UIntPtr pde); diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 54f1d5d9..cec3bdc2 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -62,11 +62,23 @@ class BFileReader final { HCore::VoidPtr ReadAll(); + enum { + kOperationOkay, + kNotSupported, + kEmptyDirectory, + kNoSuchEntry, + kIsDirectory, + kCount, + }; + + Int32 &Error() { return mErrorCode; } + public: BFileReader &operator=(const BFileReader &) = default; BFileReader(const BFileReader &) = default; private: + Int32 mErrorCode{kOperationOkay}; CharacterType mPath[255]; }; diff --git a/Private/NewBoot/Source/EFILib.cxx b/Private/NewBoot/Source/EFILib.cxx deleted file mode 100644 index 3e30e0d3..00000000 --- a/Private/NewBoot/Source/EFILib.cxx +++ /dev/null @@ -1,14 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - - File: EFI.cxx - Purpose: EFI Library for NewBoot. - - Revision History: - - 01/02/24: Added file (amlel) - -------------------------------------------- */ - -#include diff --git a/Private/NewBoot/Source/compile_flags.txt b/Private/NewBoot/Source/compile_flags.txt new file mode 100644 index 00000000..e58d7ab9 --- /dev/null +++ b/Private/NewBoot/Source/compile_flags.txt @@ -0,0 +1,3 @@ +-std=c++20 +-I../ +-I../../ diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 4193f12d..f10a4194 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -14,7 +14,7 @@ invalid-recipe: .PHONY: bootloader-amd64 bootloader-amd64: - $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx + $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx $(LD_GNU) *.o -e efi_main -filealign:16 -shared --subsystem=10 -ffreestanding -o HCoreLdr.exe cp HCoreLdr.exe CDROM/EFI/BOOT/BOOTX64.EFI cp ../../HCoreKrnl.exe CDROM/EFI/BOOT/HCoreKrnl.exe diff --git a/Private/NewKit/PageManager.hpp b/Private/NewKit/PageManager.hpp index b5337268..fdde95f8 100644 --- a/Private/NewKit/PageManager.hpp +++ b/Private/NewKit/PageManager.hpp @@ -18,64 +18,65 @@ #ifndef kBadAddress #define kBadAddress (0) -#endif // #ifndef kBadAddress +#endif // #ifndef kBadAddress -namespace HCore -{ +namespace HCore { class PageManager; -class PTEWrapper final -{ - public: - explicit PTEWrapper(Boolean Rw = false, Boolean User = false, Boolean ExecDisable = false, UIntPtr Address = 0); - ~PTEWrapper(); - - PTEWrapper &operator=(const PTEWrapper &) = default; - PTEWrapper(const PTEWrapper &) = default; - - public: - void FlushTLB(Ref &pm); - const UIntPtr &VirtualAddress(); - - bool Reclaim(); - bool Shareable(); - bool Present(); - bool Access(); - - private: - Boolean m_Rw; - Boolean m_User; - Boolean m_ExecDisable; - UIntPtr m_VirtAddr; - Boolean m_Cache; - Boolean m_Shareable; - Boolean m_Wt; - Boolean m_Present; - Boolean m_Accessed; - - private: - friend class PageManager; - friend class Pmm; +class PTEWrapper final { + public: + explicit PTEWrapper(Boolean Rw = false, Boolean User = false, + Boolean ExecDisable = false, UIntPtr Address = 0); + ~PTEWrapper(); + + PTEWrapper &operator=(const PTEWrapper &) = default; + PTEWrapper(const PTEWrapper &) = default; + + public: + void FlushTLB(Ref &pm); + const UIntPtr &VirtualAddress(); + + void NoExecute(const bool enable = false); + const bool &NoExecute(); + + bool Reclaim(); + bool Shareable(); + bool Present(); + bool Access(); + + private: + Boolean m_Rw; + Boolean m_User; + Boolean m_ExecDisable; + UIntPtr m_VirtAddr; + Boolean m_Cache; + Boolean m_Shareable; + Boolean m_Wt; + Boolean m_Present; + Boolean m_Accessed; + + private: + friend class PageManager; + friend class Pmm; }; -struct PageManager final -{ - public: - PageManager() = default; - ~PageManager() = default; +struct PageManager final { + public: + PageManager() = default; + ~PageManager() = default; - PageManager &operator=(const PageManager &) = default; - PageManager(const PageManager &) = default; + PageManager &operator=(const PageManager &) = default; + PageManager(const PageManager &) = default; - public: - PTEWrapper *Request(Boolean Rw, Boolean User, Boolean ExecDisable); - bool Free(Ref &wrapper); + public: + PTEWrapper *Request(Boolean Rw, Boolean User, Boolean ExecDisable); + bool Free(Ref &wrapper); - private: - void FlushTLB(UIntPtr VirtAddr); + private: + void FlushTLB(UIntPtr VirtAddr); - private: - friend PTEWrapper; - friend class Pmm; + private: + friend PTEWrapper; + friend class Pmm; }; -} // namespace HCore +} // namespace HCore diff --git a/Private/NewKit/Pmm.hpp b/Private/NewKit/Pmm.hpp index 58bf7609..eb7f75ab 100644 --- a/Private/NewKit/Pmm.hpp +++ b/Private/NewKit/Pmm.hpp @@ -13,33 +13,31 @@ #include #include -namespace HCore -{ - class Pmm; - class PTEWrapper; - - class Pmm final - { - public: - Pmm(); - ~Pmm(); - - public: - explicit Pmm(Ref &pm); - - Pmm &operator=(const Pmm &) = delete; - Pmm(const Pmm &) = delete; - - Ref RequestPage(Boolean user = false, Boolean readWrite = false); - Boolean FreePage(Ref refPage); - - Boolean ToggleRw(Ref refPage, Boolean enable = true); - Boolean TogglePresent(Ref refPage, Boolean enable = true); - Boolean ToggleUser(Ref refPage, Boolean enable = true); - Boolean ToggleShare(Ref refPage, Boolean enable = true); - - private: - Ref m_PageManager; - - }; -} // namespace HCore +namespace HCore { +class Pmm; +class PTEWrapper; + +class Pmm final { + public: + explicit Pmm(); + ~Pmm(); + + Pmm &operator=(const Pmm &) = delete; + Pmm(const Pmm &) = default; + + Ref RequestPage(Boolean user = false, + Boolean readWrite = false); + Boolean FreePage(Ref refPage); + + Boolean ToggleRw(Ref refPage, Boolean enable = true); + Boolean TogglePresent(Ref refPage, Boolean enable = true); + Boolean ToggleUser(Ref refPage, Boolean enable = true); + Boolean ToggleShare(Ref refPage, Boolean enable = true); + + /// @brief Get the page manager of this. + Ref &Leak() { return m_PageManager; } + + private: + Ref m_PageManager; +}; +} // namespace HCore diff --git a/Private/NewKit/Ref.hpp b/Private/NewKit/Ref.hpp index d11f3f75..9988c213 100644 --- a/Private/NewKit/Ref.hpp +++ b/Private/NewKit/Ref.hpp @@ -13,76 +13,54 @@ #include #include -namespace HCore -{ - template - class Ref final - { - public: - Ref() = default; - ~Ref() = default; - - public: - Ref(T cls, const bool &strong = false) : m_Class(cls), m_Strong(strong) {} - - Ref &operator=(T ref) - { - m_Class = ref; - return *this; - } - - public: - T operator->() const - { - return m_Class; - } - - T &Leak() - { - return m_Class; - } - - T operator*() - { - return m_Class; - } - - bool IsStrong() const - { - return m_Strong; - } - - operator bool() - { - return m_Class; - } - - private: - T m_Class; - bool m_Strong{ false }; - - }; - - template - class NonNullRef final - { - public: - NonNullRef() = delete; - NonNullRef(nullPtr) = delete; - - NonNullRef(T *ref) : m_Ref(ref, true) {} - - Ref &operator->() - { - MUST_PASS(m_Ref); - return m_Ref; - } - - NonNullRef &operator=(const NonNullRef &ref) = delete; - NonNullRef(const NonNullRef &ref) = default; - - private: - Ref m_Ref{nullptr}; - - }; -} // namespace HCore +namespace HCore { +template +class Ref final { + public: + Ref() = default; + ~Ref() = default; + + public: + Ref(T cls, const bool &strong = false) : m_Class(cls), m_Strong(strong) {} + + Ref &operator=(T ref) { + m_Class = ref; + return *this; + } + + public: + T operator->() const { return m_Class; } + + T &Leak() { return m_Class; } + + T operator*() { return m_Class; } + + bool IsStrong() const { return m_Strong; } + + operator bool() { return m_Strong; } + + private: + T m_Class; + bool m_Strong{false}; +}; + +template +class NonNullRef final { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T *ref) : m_Ref(ref, true) {} + + Ref &operator->() { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef &operator=(const NonNullRef &ref) = delete; + NonNullRef(const NonNullRef &ref) = default; + + private: + Ref m_Ref{nullptr}; +}; +} // namespace HCore diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 42ea0187..603b91b3 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -9,6 +9,8 @@ #include +#include "NewKit/PageManager.hpp" + //! @file KernelHeap.cpp //! @brief Kernel allocator. @@ -48,6 +50,8 @@ VoidPtr ke_new_ke_heap(const SizeT &sz, const bool rw, const bool user) { Ref wrapper = kPmm.RequestPage(user, rw); if (wrapper) { + wrapper->NoExecute(true); + kLastWrapper = wrapper; kWrapperList[kWrapperCount] = wrapper; @@ -67,15 +71,27 @@ Int32 ke_delete_ke_heap(voidPtr ptr) { const UIntPtr virtualAddress = reinterpret_cast(ptr); if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { - return kPmm.FreePage(kLastWrapper); + if (kPmm.FreePage(kLastWrapper)) { + kLastWrapper->NoExecute(false); + return true; + } + + return false; } - Ref wrapper; + Ref wrapper{nullptr}; for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) { if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) { wrapper = kWrapperList[indexWrapper]; - return kPmm.FreePage(wrapper); + + // if page is no more, then mark it also as non executable. + if (kPmm.FreePage(wrapper)) { + wrapper->NoExecute(false); + return true; + } + + return false; } } } @@ -108,11 +124,10 @@ Boolean kernel_valid_ptr(voidPtr ptr) { } /// @brief The Kernel heap initializer function. -/// @return -Void ke_init_ke_heap() noexcept -{ +/// @return +Void ke_init_ke_heap() noexcept { kWrapperCount = 0UL; - Ref kLastWrapper = Ref(nullptr); + Ref kLastWrapper = Ref(nullptr); Pmm kPmm = Pmm(); } } // namespace HCore diff --git a/Private/Source/PageManager.cxx b/Private/Source/PageManager.cxx index 95097f99..d6455a18 100644 --- a/Private/Source/PageManager.cxx +++ b/Private/Source/PageManager.cxx @@ -85,8 +85,20 @@ bool PageManager::Free(Ref &wrapper) { return false; } +//////////////////////////// + +// VIRTUAL ADDRESS + +//////////////////////////// + const UIntPtr &PTEWrapper::VirtualAddress() { return m_VirtAddr; } +//////////////////////////// + +// PAGE GETTERS + +//////////////////////////// + bool PTEWrapper::Shareable() { auto raw = reinterpret_cast(m_VirtAddr); @@ -120,4 +132,13 @@ bool PTEWrapper::Access() { return m_Accessed; } + +//////////////////////////// + +// NO EXECUTE PROTECTION + +//////////////////////////// + +void PTEWrapper::NoExecute(const bool enable) { this->m_ExecDisable = enable; } +const bool &PTEWrapper::NoExecute() { return this->m_ExecDisable; } } // namespace HCore diff --git a/Private/Source/Pmm.cxx b/Private/Source/Pmm.cxx index aa5a3c89..76191fdd 100644 --- a/Private/Source/Pmm.cxx +++ b/Private/Source/Pmm.cxx @@ -11,11 +11,8 @@ #include namespace HCore { -Pmm::Pmm() = default; - -Pmm::Pmm(Ref &pm) : m_PageManager(pm) { - MUST_PASS(pm.Leak()); - kcout << "[PMM] New PhysicalMemoryManager\r\n"; +Pmm::Pmm() : m_PageManager() { + kcout << "[PMM] Allocate PageMemoryManager\r\n"; } Pmm::~Pmm() = default; @@ -23,7 +20,7 @@ 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, true); if (pt) return Ref(pt); -- cgit v1.2.3