diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-01 17:24:09 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-01 17:24:09 +0100 |
| commit | 6ce7dffe92775f262384a028af233999a7d18048 (patch) | |
| tree | 985e7e39fd9657ceb45ab292f6a934e0e6d4571b | |
| parent | 26ceef5cccbb40b00a302979ed297243b356feff (diff) | |
Kernel: Really big improvements on Paged Memory Model.
Bootloader: Design shift in BFileReader class.
Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
| -rw-r--r-- | Private/EFIKit/EFILib.hxx | 6 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalPageAlloc.hpp | 2 | ||||
| -rw-r--r-- | Private/NewBoot/BootKit/BootKit.hxx | 12 | ||||
| -rw-r--r-- | Private/NewBoot/Source/EFILib.cxx | 14 | ||||
| -rw-r--r-- | Private/NewBoot/Source/compile_flags.txt | 3 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 2 | ||||
| -rw-r--r-- | Private/NewKit/PageManager.hpp | 103 | ||||
| -rw-r--r-- | Private/NewKit/Pmm.hpp | 58 | ||||
| -rw-r--r-- | Private/NewKit/Ref.hpp | 124 | ||||
| -rw-r--r-- | Private/Source/KernelHeap.cxx | 29 | ||||
| -rw-r--r-- | Private/Source/PageManager.cxx | 21 | ||||
| -rw-r--r-- | Private/Source/Pmm.cxx | 9 |
12 files changed, 194 insertions, 189 deletions
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 <BootKit/Platform.hxx> #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 <EFIKit/EFILib.hxx> 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<PageManager> &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<PageManager> &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<PTEWrapper*> &wrapper); + public: + PTEWrapper *Request(Boolean Rw, Boolean User, Boolean ExecDisable); + bool Free(Ref<PTEWrapper *> &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 <NewKit/PageManager.hpp> #include <NewKit/Ref.hpp> -namespace HCore -{ - class Pmm; - class PTEWrapper; - - class Pmm final - { - public: - Pmm(); - ~Pmm(); - - public: - explicit Pmm(Ref<PageManager*> &pm); - - Pmm &operator=(const Pmm &) = delete; - Pmm(const Pmm &) = delete; - - Ref<PTEWrapper*> RequestPage(Boolean user = false, Boolean readWrite = false); - Boolean FreePage(Ref<PTEWrapper*> refPage); - - Boolean ToggleRw(Ref<PTEWrapper*> refPage, Boolean enable = true); - Boolean TogglePresent(Ref<PTEWrapper*> refPage, Boolean enable = true); - Boolean ToggleUser(Ref<PTEWrapper*> refPage, Boolean enable = true); - Boolean ToggleShare(Ref<PTEWrapper*> refPage, Boolean enable = true); - - private: - Ref<PageManager*> 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<PTEWrapper *> RequestPage(Boolean user = false, + Boolean readWrite = false); + Boolean FreePage(Ref<PTEWrapper *> refPage); + + Boolean ToggleRw(Ref<PTEWrapper *> refPage, Boolean enable = true); + Boolean TogglePresent(Ref<PTEWrapper *> refPage, Boolean enable = true); + Boolean ToggleUser(Ref<PTEWrapper *> refPage, Boolean enable = true); + Boolean ToggleShare(Ref<PTEWrapper *> refPage, Boolean enable = true); + + /// @brief Get the page manager of this. + Ref<PageManager> &Leak() { return m_PageManager; } + + private: + Ref<PageManager> 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 <NewKit/Defines.hpp> #include <NewKit/RuntimeCheck.hpp> -namespace HCore -{ - template <typename T> - 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 <typename T> - class NonNullRef final - { - public: - NonNullRef() = delete; - NonNullRef(nullPtr) = delete; - - NonNullRef(T *ref) : m_Ref(ref, true) {} - - Ref<T> &operator->() - { - MUST_PASS(m_Ref); - return m_Ref; - } - - NonNullRef &operator=(const NonNullRef<T> &ref) = delete; - NonNullRef(const NonNullRef<T> &ref) = default; - - private: - Ref<T> m_Ref{nullptr}; - - }; -} // namespace HCore +namespace HCore { +template <typename T> +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 <typename T> +class NonNullRef final { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T *ref) : m_Ref(ref, true) {} + + Ref<T> &operator->() { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef &operator=(const NonNullRef<T> &ref) = delete; + NonNullRef(const NonNullRef<T> &ref) = default; + + private: + Ref<T> 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 <NewKit/KernelHeap.hpp> +#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<PTEWrapper *> 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<UIntPtr>(ptr); if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) { - return kPmm.FreePage(kLastWrapper); + if (kPmm.FreePage(kLastWrapper)) { + kLastWrapper->NoExecute(false); + return true; + } + + return false; } - Ref<PTEWrapper *> wrapper; + Ref<PTEWrapper *> 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<PTEWrapper *> kLastWrapper = Ref<PTEWrapper*>(nullptr); + Ref<PTEWrapper *> kLastWrapper = Ref<PTEWrapper *>(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<PTEWrapper *> &wrapper) { return false; } +//////////////////////////// + +// VIRTUAL ADDRESS + +//////////////////////////// + const UIntPtr &PTEWrapper::VirtualAddress() { return m_VirtAddr; } +//////////////////////////// + +// PAGE GETTERS + +//////////////////////////// + bool PTEWrapper::Shareable() { auto raw = reinterpret_cast<PTE *>(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 <NewKit/Pmm.hpp> namespace HCore { -Pmm::Pmm() = default; - -Pmm::Pmm(Ref<PageManager *> &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<PTEWrapper *> 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<PTEWrapper *>(pt); |
