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/NewBoot/Source/HEL/AMD64/Entrypoint.cxx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Private/NewBoot/Source') 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; -- 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 (limited to 'Private/NewBoot/Source') 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 (limited to 'Private/NewBoot/Source') 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