From 1069f411d413e2185f6536b01b8993187056fcd8 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 4 Sep 2024 22:20:17 +0200 Subject: [ IMP ] BMP allocator needs more tweaking and fixes, to be usable. Signed-off-by: Amlal El Mahrouss --- dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx | 99 +++++++++++++++++++++++++++++ dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx | 99 ----------------------------- dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm | 34 ++++------ dev/ZKA/HALKit/POWER/HalHardware.cxx | 19 ------ 4 files changed, 111 insertions(+), 140 deletions(-) create mode 100644 dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx delete mode 100644 dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx delete mode 100644 dev/ZKA/HALKit/POWER/HalHardware.cxx (limited to 'dev/ZKA/HALKit') diff --git a/dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx b/dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx new file mode 100644 index 00000000..7bafd075 --- /dev/null +++ b/dev/ZKA/HALKit/AMD64/HalBMPMgr.cxx @@ -0,0 +1,99 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include + +#define cVMHMagic ((Kernel::UIntPtr)0x10210) + +#ifdef __ZKA_AMD64__ +#include +#elif defined(__ZKA_ARM64__) +#include +#endif + +#include +#include + +namespace Kernel +{ + namespace HAL + { + namespace Detail + { + struct AllocatorTraits final + { + /// @brief Iterate over availables pages for a free one. + /// @return The new address which was found. + VoidPtr FindBitMap(VoidPtr base_ptr, SizeT size, Bool rw, Bool user) noexcept + { + while (base_ptr && size) + { + UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); + + if (ptr_bit_set[0] != cVMHMagic) + { + ptr_bit_set[0] = cVMHMagic; + ptr_bit_set[1] = size; + + kcout << "BBP: STATUS\r"; + kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; + kcout << "BBP: ADDRESS: " << hex_number((UIntPtr)ptr_bit_set) << endl; + kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; + + if (rw) + mm_update_pte(base_ptr, eFlagsRw); + + if (user) + mm_update_pte(base_ptr, eFlagsUser); + + return (VoidPtr)(&ptr_bit_set[2]); + } + + base_ptr = reinterpret_cast(reinterpret_cast(base_ptr) + 1 + ptr_bit_set[1]); + } + + return nullptr; + } + }; + } // namespace Detail + + /// @brief Allocate a new page to be used by the OS. + /// @param rw read/write bit. + /// @param user user bit. + /// @return + auto mm_alloc_bitmap(Boolean rw, Boolean user, SizeT size) -> VoidPtr + { + VoidPtr ptr_new = nullptr; + Detail::AllocatorTraits traits; + + ptr_new = traits.FindBitMap(kKernelVirtualStart, size, rw, user); + + return &((UIntPtr*)ptr_new)[1]; + } + + auto mm_free_bitmap(VoidPtr page_ptr) -> Bool + { + if (!page_ptr) + return false; + + UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr) - 3; + + if (!ptr_bit_set[0] || + ptr_bit_set[0] != cVMHMagic) + return false; + + kcout << "BBP: FREE STATUS\r"; + kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; + kcout << "BBP: ADDRESSS: " << hex_number((UIntPtr)ptr_bit_set) << endl; + kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; + + ptr_bit_set[0] = 0UL; + ptr_bit_set[1] = 0UL; + + return true; + } + } // namespace HAL +} // namespace Kernel diff --git a/dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx b/dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx deleted file mode 100644 index 7bafd075..00000000 --- a/dev/ZKA/HALKit/AMD64/HalBitMapMgr.cxx +++ /dev/null @@ -1,99 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include - -#define cVMHMagic ((Kernel::UIntPtr)0x10210) - -#ifdef __ZKA_AMD64__ -#include -#elif defined(__ZKA_ARM64__) -#include -#endif - -#include -#include - -namespace Kernel -{ - namespace HAL - { - namespace Detail - { - struct AllocatorTraits final - { - /// @brief Iterate over availables pages for a free one. - /// @return The new address which was found. - VoidPtr FindBitMap(VoidPtr base_ptr, SizeT size, Bool rw, Bool user) noexcept - { - while (base_ptr && size) - { - UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); - - if (ptr_bit_set[0] != cVMHMagic) - { - ptr_bit_set[0] = cVMHMagic; - ptr_bit_set[1] = size; - - kcout << "BBP: STATUS\r"; - kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; - kcout << "BBP: ADDRESS: " << hex_number((UIntPtr)ptr_bit_set) << endl; - kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; - - if (rw) - mm_update_pte(base_ptr, eFlagsRw); - - if (user) - mm_update_pte(base_ptr, eFlagsUser); - - return (VoidPtr)(&ptr_bit_set[2]); - } - - base_ptr = reinterpret_cast(reinterpret_cast(base_ptr) + 1 + ptr_bit_set[1]); - } - - return nullptr; - } - }; - } // namespace Detail - - /// @brief Allocate a new page to be used by the OS. - /// @param rw read/write bit. - /// @param user user bit. - /// @return - auto mm_alloc_bitmap(Boolean rw, Boolean user, SizeT size) -> VoidPtr - { - VoidPtr ptr_new = nullptr; - Detail::AllocatorTraits traits; - - ptr_new = traits.FindBitMap(kKernelVirtualStart, size, rw, user); - - return &((UIntPtr*)ptr_new)[1]; - } - - auto mm_free_bitmap(VoidPtr page_ptr) -> Bool - { - if (!page_ptr) - return false; - - UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr) - 3; - - if (!ptr_bit_set[0] || - ptr_bit_set[0] != cVMHMagic) - return false; - - kcout << "BBP: FREE STATUS\r"; - kcout << "BBP: MAG: " << hex_number(ptr_bit_set[0]) << endl; - kcout << "BBP: ADDRESSS: " << hex_number((UIntPtr)ptr_bit_set) << endl; - kcout << "BBP: SIZE: " << hex_number(ptr_bit_set[1]) << endl; - - ptr_bit_set[0] = 0UL; - ptr_bit_set[1] = 0UL; - - return true; - } - } // namespace HAL -} // namespace Kernel diff --git a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm index 1f31c880..c26a346f 100644 --- a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm +++ b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm @@ -21,8 +21,6 @@ section .text ;; rcx: code ptr. ;; rdx: stack ptr. mp_do_context_switch: - swapgs - mov fs, rcx mov gs, rdx @@ -36,27 +34,12 @@ mp_do_context_switch: mov r11, gs mov r12, fs - ; Enable SCE that enables sysret and syscall - mov rcx, 0xc0000082 - wrmsr - mov rcx, 0xc0000080 - rdmsr - or eax, 1 - wrmsr - mov rcx, 0xc0000081 - rdmsr - mov edx, 0x00180008 - wrmsr - mov r11, 0x202 mov fs, [r8 + (8 * 4)] mov gs, [r8 + (8 * 9)] mov r8, [r8] - swapgs - sti - o64 sysret ;; @brief Gets the current stack frame. @@ -68,8 +51,6 @@ extern hal_system_call_enter global mp_system_call_handler mp_system_call_handler: - swapgs - push r8 push r9 push r10 @@ -80,9 +61,6 @@ mp_system_call_handler: pop r9 pop r8 - swapgs - sti - o64 sysret mp_do_context_switch_pre: @@ -96,4 +74,16 @@ mp_do_context_switch_pre: mov rcx, 0xc0000082 wrmsr + ; Enable SCE that enables sysret and syscall + mov rcx, 0xc0000082 + wrmsr + mov rcx, 0xc0000080 + rdmsr + or eax, 1 + wrmsr + mov rcx, 0xc0000081 + rdmsr + mov edx, 0x00180008 + wrmsr + ret diff --git a/dev/ZKA/HALKit/POWER/HalHardware.cxx b/dev/ZKA/HALKit/POWER/HalHardware.cxx deleted file mode 100644 index eb335d70..00000000 --- a/dev/ZKA/HALKit/POWER/HalHardware.cxx +++ /dev/null @@ -1,19 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include - -namespace Kernel -{ - namespace HAL - { - UIntPtr mm_alloc_bitmap(bool rw, bool user) - { - return 0; - } - } // namespace HAL -} // namespace Kernel -- cgit v1.2.3