diff options
| author | Amlal El Mahrouss <amlal@el-mahrouss-logic.com> | 2024-03-20 18:24:58 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@el-mahrouss-logic.com> | 2024-03-20 18:25:02 +0100 |
| commit | f48c5b2cda43241919d3ea1b263bef01e014c537 (patch) | |
| tree | 5bf74621fcff0a98163b3908d35cef2a6339bfb7 /Private | |
| parent | 4ba02280f19b8a2beb1ad8445be7df6b7f9e1805 (diff) | |
Kernel: See below.
- Fix: Kernel page alloc. Inside HalPageAlloc.cpp.
- Made NewFSJournalRunner fields private.
- Rework StorageKit for current ticket 14.
- :boom: Breaking changes to virtual memory api.
Signed-off-by: Amlal El Mahrouss <amlal@el-mahrouss-logic.com>
Diffstat (limited to 'Private')
| -rw-r--r-- | Private/HALKit/AMD64/HalKernelMain.cxx | 2 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalPageAlloc.cpp | 56 | ||||
| -rw-r--r-- | Private/HALKit/AMD64/HalPageAlloc.hpp | 3 | ||||
| -rw-r--r-- | Private/HALKit/PowerPC/HalHardware.cxx | 3 | ||||
| -rw-r--r-- | Private/NewKit/PageAllocator.hpp | 2 | ||||
| -rw-r--r-- | Private/NewKit/PageManager.hpp | 1 | ||||
| -rw-r--r-- | Private/Source/KernelCheck.cxx | 2 | ||||
| -rw-r--r-- | Private/Source/KernelHeap.cxx | 32 | ||||
| -rw-r--r-- | Private/Source/NewFS+Journal.cxx | 6 | ||||
| -rw-r--r-- | Private/Source/PageAllocator.cxx | 6 | ||||
| -rw-r--r-- | Private/Source/PageManager.cxx | 46 | ||||
| -rw-r--r-- | Private/Source/Pmm.cxx | 1 | ||||
| -rw-r--r-- | Private/StorageKit/StorageCore.inl | 20 |
13 files changed, 66 insertions, 114 deletions
diff --git a/Private/HALKit/AMD64/HalKernelMain.cxx b/Private/HALKit/AMD64/HalKernelMain.cxx index 2b420bb9..6c2ad263 100644 --- a/Private/HALKit/AMD64/HalKernelMain.cxx +++ b/Private/HALKit/AMD64/HalKernelMain.cxx @@ -32,8 +32,6 @@ EXTERN_C void RuntimeMain( kKernelPhysicalSize = HandoverHeader->f_PhysicalSize; kKernelPhysicalStart = HandoverHeader->f_PhysicalStart; - hal_write_cr3((HCore::UIntPtr)kKernelVirtualStart); - STATIC HCore::HAL::Detail::HCoreGDT GDT = { {0, 0, 0, 0x00, 0x00, 0}, // null entry {0, 0, 0, 0x9a, 0xaf, 0}, // kernel code diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp index 91bde617..5f6564c0 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.cpp +++ b/Private/HALKit/AMD64/HalPageAlloc.cpp @@ -9,13 +9,7 @@ #include <NewKit/Defines.hpp> #include <NewKit/KernelCheck.hpp> -// this files handles paging. - -STATIC HCore::SizeT kPageCnt = 0UL; STATIC HCore::Boolean kAllocationInProgress = false; - -#define kKernelPagingPadding kPTEAlign - namespace HCore { namespace HAL { /// @brief Allocates a new page of memory. @@ -23,47 +17,33 @@ namespace HAL { /// @param rw read/write flag. /// @param user user flag. /// @return the page table of it. -STATIC auto hal_try_alloc_new_page(SizeT sz, Boolean rw, Boolean user) - -> VoidPtr { - MUST_PASS(sz > 0); - +STATIC auto hal_try_alloc_new_page(Boolean rw, Boolean user) -> VoidPtr { kAllocationInProgress = true; - PDE* cr3 = (PDE*)hal_read_cr3(); + PTE* newAddress = (PTE*)kKernelVirtualStart; - kcout << "HCoreKrnl.exe: CR3: " << hex_number((UIntPtr)cr3) << endl; - - for (size_t i = 0; i < kPTESize; ++i) - { - if (cr3->Pte[i].Present) continue; - kcout << "HCoreKrnl.exe: Page index: " << hex_number(i) << endl; - - cr3->Pte[i].Rw = rw; - cr3->Pte[i].User = user; - cr3->Pte[i].Present = true; + while (newAddress->Present) { + newAddress = newAddress + sizeof(PTE); + } - ++kPageCnt; + newAddress->Present = true; + newAddress->Rw = rw; + newAddress->User = user; - kAllocationInProgress = false; - kcout << "HCoreKrnl.exe: Allocation done for: " << hex_number(i) << endl; - return (VoidPtr)cr3->Pte[i].PhysicalAddress; - } - kAllocationInProgress = false; - return nullptr; -} -auto hal_alloc_page(SizeT sz, Boolean rw, Boolean user) -> VoidPtr { - while (kAllocationInProgress) {} + return reinterpret_cast<VoidPtr>(newAddress); +} - if (sz == 0) - ++sz; +/// @brief Allocate a new page to be used by the OS. +/// @param rw +/// @param user +/// @return +auto hal_alloc_page(Boolean rw, Boolean user) -> VoidPtr { + while (kAllocationInProgress) { + } /// allocate new page. - return hal_try_alloc_new_page(sz, rw, user); -} - -auto hal_create_page(Boolean rw, Boolean user) -> UIntPtr { - return reinterpret_cast<UIntPtr>(hal_alloc_page(sizeof(PageTable64), rw, user)); + return hal_try_alloc_new_page(rw, user); } } // namespace HAL } // namespace HCore diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp index 2ce56b5a..5fe55992 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.hpp +++ b/Private/HALKit/AMD64/HalPageAlloc.hpp @@ -72,8 +72,7 @@ struct PageDirectory64 final { PageTable64 ALIGN(kPTEAlign) Pte[kPTEMax]; }; -VoidPtr hal_alloc_page(SizeT sz, Boolean rw, Boolean user); -UIntPtr hal_create_page(Boolean rw, Boolean user); +VoidPtr hal_alloc_page(Boolean rw, Boolean user); } // namespace HCore::HAL namespace HCore { diff --git a/Private/HALKit/PowerPC/HalHardware.cxx b/Private/HALKit/PowerPC/HalHardware.cxx index 340f515e..49e85138 100644 --- a/Private/HALKit/PowerPC/HalHardware.cxx +++ b/Private/HALKit/PowerPC/HalHardware.cxx @@ -9,8 +9,7 @@ namespace HCore { namespace HAL { -UIntPtr hal_create_page(bool rw, bool user) { return 0; } -UIntPtr hal_alloc_page(UIntPtr offset, bool rw, bool user) { return 0; } +UIntPtr hal_alloc_page(bool rw, bool user) { return 0; } } // namespace HAL // @brief wakes up thread. diff --git a/Private/NewKit/PageAllocator.hpp b/Private/NewKit/PageAllocator.hpp index 70bbbad9..2b761844 100644 --- a/Private/NewKit/PageAllocator.hpp +++ b/Private/NewKit/PageAllocator.hpp @@ -12,7 +12,7 @@ namespace HCore { namespace Detail { -UIntPtr create_page_wrapper(Boolean rw, Boolean user); +VoidPtr create_page_wrapper(Boolean rw, Boolean user); void exec_disable(UIntPtr addr); bool page_disable(UIntPtr addr); } // namespace Detail diff --git a/Private/NewKit/PageManager.hpp b/Private/NewKit/PageManager.hpp index dab9ac73..a167c0f9 100644 --- a/Private/NewKit/PageManager.hpp +++ b/Private/NewKit/PageManager.hpp @@ -31,7 +31,6 @@ class PTEWrapper final { PTEWrapper(const PTEWrapper &) = default; public: - void Flush(); const UIntPtr VirtualAddress(); void NoExecute(const bool enable = false); diff --git a/Private/Source/KernelCheck.cxx b/Private/Source/KernelCheck.cxx index 593190e1..30c76050 100644 --- a/Private/Source/KernelCheck.cxx +++ b/Private/Source/KernelCheck.cxx @@ -47,7 +47,7 @@ void ke_stop(const HCore::Int &id) { } case RUNTIME_CHECK_BOOTSTRAP: { kcout << "*** CAUSE: RUNTIME_CHECK_BOOTSTRAP *** \r\n"; - kcout << "*** WHAT: BAD BOOT. *** \r\n"; + kcout << "*** WHAT: INVALID BOOT SEQUENCE. *** \r\n"; break; } case RUNTIME_CHECK_HANDSHAKE: { diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 359e3cbd..438df005 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -4,8 +4,9 @@ ------------------------------------------- */ -#include <NewKit/Crc32.hpp> +#include <KernelKit/DebugOutput.hpp> #include <KernelKit/KernelHeap.hpp> +#include <NewKit/Crc32.hpp> #include <NewKit/PageManager.hpp> //! @file KernelHeap.cxx @@ -14,19 +15,19 @@ #define kHeapMagic 0xD4D7 namespace HCore { -STATIC SizeT kHeapCount = 0UL; -STATIC Ref<PTEWrapper> kHeapLastWrapper; -STATIC PageManager kHeapPageManager; +STATIC SizeT kHeapCount = 0UL; +STATIC Ref<PTEWrapper> kHeapLastWrapper; +STATIC PageManager kHeapPageManager; namespace Detail { /// @brief Kernel heap information block. /// Located before the address bytes. /// | HIB | ADDRESS | struct HeapInformationBlock final { - UInt16 hMagic; + UInt16 hMagic; Boolean hPresent; - Int32 hCRC32; - Int64 hSizeAddress; + Int32 hCRC32; + Int64 hSizeAddress; UIntPtr hAddress; }; @@ -44,17 +45,21 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { auto wrapper = kHeapPageManager.Request(rw, user, false); kHeapLastWrapper = wrapper; + kcout << "HCoreKrnl.exe: Populating HIB...\r\n"; + Detail::HeapInformationBlockPtr heapInfo = reinterpret_cast<Detail::HeapInformationBlockPtr>( wrapper.VirtualAddress()); heapInfo->hSizeAddress = sz; heapInfo->hMagic = kHeapMagic; - heapInfo->hCRC32 = 0; // dont fill it for now. + heapInfo->hCRC32 = 0; // dont fill it for now. heapInfo->hAddress = wrapper.VirtualAddress(); ++kHeapCount; + kcout << "HCoreKrnl.exe: Return address...\r\n"; + return reinterpret_cast<VoidPtr>(wrapper.VirtualAddress() + sizeof(Detail::HeapInformationBlock)); } @@ -70,8 +75,10 @@ Int32 ke_delete_ke_heap(VoidPtr ptr) { if (kHeapLastWrapper && virtualAddress->hMagic == kHeapMagic && virtualAddress->hAddress == kHeapLastWrapper.Leak().VirtualAddress()) { - virtualAddress->hSizeAddress = 0UL; - virtualAddress->hPresent = false; + virtualAddress->hSizeAddress = 0UL; + virtualAddress->hPresent = false; + + --kHeapCount; return true; } @@ -84,13 +91,14 @@ Int32 ke_delete_ke_heap(VoidPtr ptr) { /// @param ptr the pointer /// @return if it exists. Boolean ke_is_valid_ptr(VoidPtr ptr) { + if (kHeapCount < 1) return false; + if (ptr) { Detail::HeapInformationBlockPtr virtualAddress = reinterpret_cast<Detail::HeapInformationBlockPtr>(ptr) - sizeof(Detail::HeapInformationBlock); - if (virtualAddress->hPresent && - virtualAddress->hMagic == kHeapMagic) { + if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) { return true; } } diff --git a/Private/Source/NewFS+Journal.cxx b/Private/Source/NewFS+Journal.cxx index 472db1fe..ec3c5eb7 100644 --- a/Private/Source/NewFS+Journal.cxx +++ b/Private/Source/NewFS+Journal.cxx @@ -19,11 +19,12 @@ typedef Boolean (*NewFSRunnerType)(VoidPtr delegate); /// @brief Journal thread class. class NewFSJournalRunner final { - public: + private: NewFSRunnerType fLoadRoutine{nullptr}; NewFSRunnerType fCacheRoutine{nullptr}; NewFSRunnerType fUnloadRoutine{nullptr}; + public: explicit NewFSJournalRunner(NewFSRunnerType load_runner) : fLoadRoutine(load_runner) { MUST_PASS(fLoadRoutine); @@ -44,7 +45,8 @@ class NewFSJournalRunner final { switch (operation) { case kNewFSOpLog: { if (!classPtr) { - kcout << "HCoreKrnl.exe: Miss for classPtr at NewFSJournalManager::Run(classPtr) " + kcout << "HCoreKrnl.exe: Miss for classPtr at " + "NewFSJournalManager::Run(classPtr) " << __FILE__ << "\n"; return false; } diff --git a/Private/Source/PageAllocator.cxx b/Private/Source/PageAllocator.cxx index eb22dd35..209bb9ac 100644 --- a/Private/Source/PageAllocator.cxx +++ b/Private/Source/PageAllocator.cxx @@ -8,10 +8,10 @@ #include <KernelKit/DebugOutput.hpp> #include <NewKit/PageAllocator.hpp> -// empty for now. +/// @brief Internal namespace, used internally by kernel. namespace HCore::Detail { -UIntPtr create_page_wrapper(Boolean rw, Boolean user) { - auto addr = HAL::hal_create_page(rw, user); +VoidPtr create_page_wrapper(Boolean rw, Boolean user) { + auto addr = HAL::hal_alloc_page(rw, user); if (addr == kBadAddress) { kcout << "[create_page_wrapper] kBadAddress returned\n"; diff --git a/Private/Source/PageManager.cxx b/Private/Source/PageManager.cxx index 6cedd5b6..67aa9b7f 100644 --- a/Private/Source/PageManager.cxx +++ b/Private/Source/PageManager.cxx @@ -25,40 +25,9 @@ PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, m_Shareable(false), m_Wt(false), m_Present(true), - m_Accessed(false) { - // special case for the null region. - if (VirtAddr <= kProtectedRegionEnd) { - m_Wt = false; - m_Rw = false; - m_Cache = false; - m_Shareable = false; - m_ExecDisable = true; - } -} - -PTEWrapper::~PTEWrapper() { - PDE* cr3 = (PDE*)hal_read_cr3(); - - PTE* raw = (PTE*)&cr3->Pte[(this->m_VirtAddr % kPTESize)]; - - raw->Present = false; - raw->Rw = false; -} + m_Accessed(false) {} -void PTEWrapper::Flush() { - PDE* cr3 = (PDE*)hal_read_cr3(); - - kcout << "CR3: " << hex_number((UIntPtr)cr3) << endl; - kcout << "Index: " << hex_number((this->m_VirtAddr % kPTESize)) << endl; - - cr3->Pte[(this->m_VirtAddr % kPTESize)].Wt = m_Wt; - cr3->Pte[(this->m_VirtAddr % kPTESize)].Rw = m_Rw; - cr3->Pte[(this->m_VirtAddr % kPTESize)].Cache = m_Cache; - cr3->Pte[(this->m_VirtAddr % kPTESize)].Present = m_Present; - cr3->Pte[(this->m_VirtAddr % kPTESize)].ExecDisable = m_ExecDisable; - - kcout << "Wrote PTE to PDE: " << hex_number((UIntPtr)cr3) << endl; -} +PTEWrapper::~PTEWrapper() {} void PageManager::FlushTLB(UIntPtr VirtAddr) { if (VirtAddr == kBadAddress) return; @@ -75,12 +44,9 @@ bool PTEWrapper::Reclaim() { return false; } -PTEWrapper PageManager::Request(Boolean Rw, Boolean User, - Boolean ExecDisable) { +PTEWrapper PageManager::Request(Boolean Rw, Boolean User, Boolean ExecDisable) { // Store PTE wrapper right after PTE. - VoidPtr ptr = reinterpret_cast<PTEWrapper *>( - HCore::HAL::hal_alloc_page(sizeof(PTEWrapper), Rw, User)); - + VoidPtr ptr = HCore::HAL::hal_alloc_page(Rw, User); return PTEWrapper{Rw, User, ExecDisable, (UIntPtr)ptr}; } @@ -94,7 +60,9 @@ bool PageManager::Free(Ref<PTEWrapper *> &wrapper) { return false; } -const UIntPtr PTEWrapper::VirtualAddress() { return (m_VirtAddr + sizeof(PTE) + sizeof(PTEWrapper)); } +const UIntPtr PTEWrapper::VirtualAddress() { + return (m_VirtAddr + sizeof(PTE) + sizeof(PTEWrapper)); +} bool PTEWrapper::Shareable() { return m_Shareable; } diff --git a/Private/Source/Pmm.cxx b/Private/Source/Pmm.cxx index 9112b3d9..97614cfa 100644 --- a/Private/Source/Pmm.cxx +++ b/Private/Source/Pmm.cxx @@ -30,7 +30,6 @@ Boolean Pmm::FreePage(Ref<PTEWrapper> PageRef) { if (!PageRef) return false; PageRef.Leak().m_Present = false; - PageRef.Leak().Flush(); return true; } diff --git a/Private/StorageKit/StorageCore.inl b/Private/StorageKit/StorageCore.inl index 2a621ac5..3288b4a2 100644 --- a/Private/StorageKit/StorageCore.inl +++ b/Private/StorageKit/StorageCore.inl @@ -16,25 +16,25 @@ namespace HCore { typedef Char* SKStr; ///! @brief Storage context, reads and write file according to the descriptor -///! layout. -class StorageContext { +///layout. +class StorageInterface { public: - explicit StorageContext() = default; - ~StorageContext() = default; + explicit StorageInterface() = default; + virtual ~StorageInterface() = default; - StorageContext& operator=(const StorageContext&) = default; - StorageContext(const StorageContext&) = default; + StorageInterface& operator=(const StorageInterface&) = default; + StorageInterface(const StorageInterface&) = default; public: - bool Write(VoidPtr fileDescriptor, SizeT sizeFileDescriptor); - struct PacketDescriptor final { VoidPtr fFilePtr; SizeT fFilePtrSz; + Lba fBase; + UInt32 fDriveId; }; - PacketDescriptor* Read(const SKStr name); - Int32 Write(PacketDescriptor* packet, const SKStr name); + virtual PacketDescriptor* Read(const SKStr name) = 0; + virtual Int32 Write(PacketDescriptor* packet, const SKStr name) = 0; }; } // namespace HCore |
