From 6dcf5b87da65de2254d6102f567183eaeca03088 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 26 Oct 2024 19:49:02 +0200 Subject: IMP: This commit contains fixes and improvements regarding the kernel, a next one will be done soon. Signed-off-by: Amlal --- dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc | 2 +- dev/zka/HALKit/AMD64/HalCPU.cc | 101 --------------- dev/zka/HALKit/AMD64/HalCPUAMD64.cc | 101 +++++++++++++++ dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm | 42 ++++++ dev/zka/HALKit/AMD64/HalDescriptorLoader.cc | 2 +- dev/zka/HALKit/AMD64/HalKernelMain.cc | 15 ++- dev/zka/HALKit/AMD64/HalMPContextSwitch.asm | 42 ------ dev/zka/HALKit/AMD64/HalPagingMgr.cc | 162 ------------------------ dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc | 159 +++++++++++++++++++++++ dev/zka/HALKit/AMD64/HalSchedulerCore.cc | 56 -------- dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc | 46 +++++++ dev/zka/HALKit/AMD64/HalTimer.cc | 86 ------------- dev/zka/HALKit/AMD64/HalTimerAMD64.cc | 86 +++++++++++++ dev/zka/HALKit/AMD64/Processor.h | 11 +- dev/zka/HALKit/ARM64/HalSchedulerCore.cc | 50 -------- dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc | 40 ++++++ dev/zka/HALKit/ARM64/Processor.h | 10 +- dev/zka/HALKit/POWER/HalVirtualMemory.cc | 24 ++-- dev/zka/HALKit/POWER/Hart.h | 2 +- dev/zka/HALKit/POWER/Processor.h | 2 + 20 files changed, 512 insertions(+), 527 deletions(-) delete mode 100644 dev/zka/HALKit/AMD64/HalCPU.cc create mode 100644 dev/zka/HALKit/AMD64/HalCPUAMD64.cc create mode 100644 dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm delete mode 100644 dev/zka/HALKit/AMD64/HalMPContextSwitch.asm delete mode 100644 dev/zka/HALKit/AMD64/HalPagingMgr.cc create mode 100644 dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc delete mode 100644 dev/zka/HALKit/AMD64/HalSchedulerCore.cc create mode 100644 dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc delete mode 100644 dev/zka/HALKit/AMD64/HalTimer.cc create mode 100644 dev/zka/HALKit/AMD64/HalTimerAMD64.cc delete mode 100644 dev/zka/HALKit/ARM64/HalSchedulerCore.cc create mode 100644 dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc (limited to 'dev/zka/HALKit') diff --git a/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc b/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc index 2ed013ef..ccb5983f 100644 --- a/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc +++ b/dev/zka/HALKit/AMD64/HalACPIFactoryInterface.cc @@ -56,7 +56,7 @@ namespace Kernel UInt8 Century; // reserved in ACPI 1.0; used since ACPI 2.0+ - UInt16 BootArchitectureFlags; + UInt16 BootArchitecturkMMFlags; UInt8 Reserved2; UInt32 Flags; diff --git a/dev/zka/HALKit/AMD64/HalCPU.cc b/dev/zka/HALKit/AMD64/HalCPU.cc deleted file mode 100644 index d24bfc0a..00000000 --- a/dev/zka/HALKit/AMD64/HalCPU.cc +++ /dev/null @@ -1,101 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: HalCPU.cc - Purpose: Platform processor routines. - -------------------------------------------- */ - -#include -#include - -/** - * @file HalCPU.cc - * @brief Common CPU API. - */ - -namespace Kernel::HAL -{ - Void Out8(UInt16 port, UInt8 value) - { - asm volatile("outb %%al, %1" - : - : "a"(value), "Nd"(port) - : "memory"); - } - - Void Out16(UInt16 port, UInt16 value) - { - asm volatile("outw %%ax, %1" - : - : "a"(value), "Nd"(port) - : "memory"); - } - - Void Out32(UInt16 port, UInt32 value) - { - asm volatile("outl %%eax, %1" - : - : "a"(value), "Nd"(port) - : "memory"); - } - - UInt8 In8(UInt16 port) - { - UInt8 value = 0UL; - asm volatile("inb %1, %%al" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; - } - - UInt16 In16(UInt16 port) - { - UInt16 value = 0UL; - asm volatile("inw %1, %%ax" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; - } - - UInt32 In32(UInt16 port) - { - UInt32 value = 0UL; - asm volatile("inl %1, %%eax" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; - } - - Void rt_halt() - { - asm volatile("hlt"); - } - - Void rt_cli() - { - asm volatile("cli"); - } - - Void rt_sti() - { - asm volatile("sti"); - } - - Void rt_cld() - { - asm volatile("cld"); - } - - Void rt_std() - { - asm volatile("std"); - } -} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalCPUAMD64.cc b/dev/zka/HALKit/AMD64/HalCPUAMD64.cc new file mode 100644 index 00000000..d24bfc0a --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalCPUAMD64.cc @@ -0,0 +1,101 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: HalCPU.cc + Purpose: Platform processor routines. + +------------------------------------------- */ + +#include +#include + +/** + * @file HalCPU.cc + * @brief Common CPU API. + */ + +namespace Kernel::HAL +{ + Void Out8(UInt16 port, UInt8 value) + { + asm volatile("outb %%al, %1" + : + : "a"(value), "Nd"(port) + : "memory"); + } + + Void Out16(UInt16 port, UInt16 value) + { + asm volatile("outw %%ax, %1" + : + : "a"(value), "Nd"(port) + : "memory"); + } + + Void Out32(UInt16 port, UInt32 value) + { + asm volatile("outl %%eax, %1" + : + : "a"(value), "Nd"(port) + : "memory"); + } + + UInt8 In8(UInt16 port) + { + UInt8 value = 0UL; + asm volatile("inb %1, %%al" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; + } + + UInt16 In16(UInt16 port) + { + UInt16 value = 0UL; + asm volatile("inw %1, %%ax" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; + } + + UInt32 In32(UInt16 port) + { + UInt32 value = 0UL; + asm volatile("inl %1, %%eax" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; + } + + Void rt_halt() + { + asm volatile("hlt"); + } + + Void rt_cli() + { + asm volatile("cli"); + } + + Void rt_sti() + { + asm volatile("sti"); + } + + Void rt_cld() + { + asm volatile("cld"); + } + + Void rt_std() + { + asm volatile("std"); + } +} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm b/dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm new file mode 100644 index 00000000..cb054a0d --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalContextSwitchAMD64.asm @@ -0,0 +1,42 @@ +;; /* +;; * ======================================================== +;; * +;; * ZKA +;; * Copyright ZKA Web Services Co., all rights reserved. +;; * +;; * ======================================================== +;; */ + +[bits 64] + +[global mp_do_task_switch] +[global mp_do_context_switch_pre] +[global mp_user_switch_proc] +[global mp_user_switch_proc_stack_begin] + +section .text + +;; @brief Switch to user mode. +mp_do_task_switch: + mov rbp, rdx + mov rsp, rdx + + mov ax, 0x18 | 3 + mov ds, ax + mov es, ax + mov gs, ax + mov fs, ax + + push 0x18 | 3 + + mov rax, rdx + push rax + + o64 pushf + + push 0x20 | 3 + + mov rax, rcx + push rax + + o64 iret diff --git a/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc b/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc index f0eff4e1..5f7bde36 100644 --- a/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc +++ b/dev/zka/HALKit/AMD64/HalDescriptorLoader.cc @@ -82,7 +82,7 @@ namespace Kernel::HAL Void IDTLoader::Load(Register64& idt) { - const auto kPITTickForScheduler = 100; + const Int16 kPITTickForScheduler = 100; volatile ::Kernel::UIntPtr** ptr_ivt = (volatile ::Kernel::UIntPtr**)idt.Base; diff --git a/dev/zka/HALKit/AMD64/HalKernelMain.cc b/dev/zka/HALKit/AMD64/HalKernelMain.cc index eb195bcc..cd700eef 100644 --- a/dev/zka/HALKit/AMD64/HalKernelMain.cc +++ b/dev/zka/HALKit/AMD64/HalKernelMain.cc @@ -81,21 +81,28 @@ EXTERN_C void hal_init_platform( Kernel::ke_stop(RUNTIME_CHECK_BOOTSTRAP); } +EXTERN_C Kernel::Void hal_kernel_server(Kernel::Void) noexcept +{ + while (Yes) + ; +} + EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { /* Initialize filesystem. */ - Kernel::NeFileSystemMgr* mgr = Kernel::mm_new_class(); - Kernel::NeFileSystemMgr::Mount(mgr); + Kernel::NeFileSystemMgr::Mount(new Kernel::NeFileSystemMgr()); /* Initialize scheduler. */ Kernel::UserProcessHelper::InitializeScheduler(); + const Kernel::Char kKernelServerName[255] = "KernelServer"; + + Kernel::rtl_create_process(&hal_kernel_server, kKernelServerName); + /* Start any cores. */ if (kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled) Kernel::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); - /* Load OSLdr.exe here (TODO) */ - Kernel::HAL::Register64 idt_reg; idt_reg.Base = (Kernel::UIntPtr)kInterruptVectorTable; diff --git a/dev/zka/HALKit/AMD64/HalMPContextSwitch.asm b/dev/zka/HALKit/AMD64/HalMPContextSwitch.asm deleted file mode 100644 index cb054a0d..00000000 --- a/dev/zka/HALKit/AMD64/HalMPContextSwitch.asm +++ /dev/null @@ -1,42 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * ZKA -;; * Copyright ZKA Web Services Co., all rights reserved. -;; * -;; * ======================================================== -;; */ - -[bits 64] - -[global mp_do_task_switch] -[global mp_do_context_switch_pre] -[global mp_user_switch_proc] -[global mp_user_switch_proc_stack_begin] - -section .text - -;; @brief Switch to user mode. -mp_do_task_switch: - mov rbp, rdx - mov rsp, rdx - - mov ax, 0x18 | 3 - mov ds, ax - mov es, ax - mov gs, ax - mov fs, ax - - push 0x18 | 3 - - mov rax, rdx - push rax - - o64 pushf - - push 0x20 | 3 - - mov rax, rcx - push rax - - o64 iret diff --git a/dev/zka/HALKit/AMD64/HalPagingMgr.cc b/dev/zka/HALKit/AMD64/HalPagingMgr.cc deleted file mode 100644 index 1a7202d7..00000000 --- a/dev/zka/HALKit/AMD64/HalPagingMgr.cc +++ /dev/null @@ -1,162 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: HalPagingMgr.cc - Purpose: Platform Paging Manager.. - -------------------------------------------- */ - -#include -#include - -namespace Kernel::HAL -{ - typedef UInt32 PageTableIndex; - - /// \brief Page store type. - struct ZKA_PAGE_STORE final - { - struct - { - PDE* fPde{nullptr}; - PTE* fPte{nullptr}; - VoidPtr fVAddr{nullptr}; - } fInternalStore; - - Bool fStoreOp{No}; // Store operation in progress. - - bool IsValidPage(PTE* pte) - { - return pte && pte->Present; - } - - bool IsWRPage(PTE* pte) - { - return pte && pte->Wr; - } - - bool IsUserPage(PTE* pte) - { - return pte && pte->User; - } - - static ZKA_PAGE_STORE& The() - { - static ZKA_PAGE_STORE the; - return the; - } - }; - - /// \brief Retrieve the page status of a PTE. - STATIC Void mmi_page_status(PTE* pte) - { - kcout << (pte->Present ? "Present" : "Not Present") << endl; - kcout << (pte->Wr ? "W/R" : "Not W/R") << endl; - kcout << (pte->ExecDisable ? "NX" : "Not NX") << endl; - kcout << (pte->User ? "User" : "Not User") << endl; - } - - STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry); - - /// @brief Maps or allocates a page from virtual_address. - /// @param virtual_address a valid virtual address. - /// @param phys_addr point to physical address. - /// @param flags the flags to put on the page. - /// @return Status code of page manipulation process. - EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, UInt32 flags) - { - if (!virtual_address || - !flags) - return 0; - - // Constants for table index bits - const UInt64 cPmlIndexMask = 0x1FFULL; // Mask for PML4, PDPT, PD, PT index (9 bits) - const UInt64 cPtIndexMask = 0xFFFULL; // Mask for page table index (12 bits) - - UInt64 cr3 = (UInt64)hal_read_cr3(); - - ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); - - // Extract the indices from the virtual address - UInt64 pml4_index = ((UIntPtr)virtual_address >> 39) & cPmlIndexMask; - UInt64 pdpt_index = ((UIntPtr)virtual_address >> 30) & cPmlIndexMask; - UInt64 pd_index = ((UIntPtr)virtual_address >> 21) & cPmlIndexMask; - UInt64 pt_index = ((UIntPtr)virtual_address >> 12) & cPmlIndexMask; - - while (page_store.fStoreOp) - ; - - page_store.fStoreOp = Yes; - - if (page_store.fInternalStore.fVAddr == virtual_address) - { - page_store.fStoreOp = No; - return mmi_map_page_table_entry(page_store.fInternalStore.fVAddr, flags, page_store.fInternalStore.fPte); - } - - const auto cPmlEntrySize = 8; - - // Read the PML4 entry from memory - UInt64 pml4_base = cr3 & ~cPtIndexMask; // CR3 points to the PML4 table base, mask off lower bits - UInt64 pml4_entry = (pml4_base + pml4_index * cPmlEntrySize); // Each entry is 8 bytes - - // Read the PDPT entry - UInt64 pdpt_base = pml4_entry & ~cPtIndexMask; // Get the PDPT base physical address - UInt64 pdpt_entry = (pdpt_base + pdpt_index * cPmlEntrySize); - - // Read the PD entry - UInt64 pd_base = pdpt_entry & ~cPtIndexMask; // Get the Page Directory base physical address - UInt64 pd_entry = (pd_base + pd_index * cPmlEntrySize); - - // Read the PT entry - UInt64 pt_base = pd_entry & ~cPtIndexMask; // Get the Page Table base physical address - UInt64 pt_entry = (pt_base + pt_index * cPmlEntrySize); - - // Lastly, grab the pte entry. - ZKA_PDE* pte_struct = reinterpret_cast(pt_base); - - return mmi_map_page_table_entry(virtual_address, flags, pte_struct->fEntries[pt_entry]); - } - - /// @brief Maps flags for a specific pte. - /// @internal Internal function. - STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry) - { - if (flags & ~eFlagsPresent) - pt_entry->Present = false; - else if (flags & eFlagsPresent) - pt_entry->Present = true; - - if (flags & eFlagsWr) - pt_entry->Wr = true; - else if (flags & ~eFlagsWr) - pt_entry->Wr = false; - - if (flags & eFlagsNX) - pt_entry->ExecDisable = true; - else if (flags & ~eFlagsNX) - pt_entry->ExecDisable = false; - - if (flags & eFlagsUser) - pt_entry->User = true; - else if (flags & ~eFlagsUser) - pt_entry->User = false; - - hal_invl_tlb(reinterpret_cast(pt_entry)); - - mmi_page_status(pt_entry); - - ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); - - // Update Internal store. - - page_store.fInternalStore.fPde = nullptr; - page_store.fInternalStore.fPte = pt_entry; - page_store.fInternalStore.fVAddr = virtual_address; - - page_store.fStoreOp = No; - - return 0; - } -} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc b/dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc new file mode 100644 index 00000000..39f53fe4 --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalPagingMgrAMD64.cc @@ -0,0 +1,159 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: HalPagingMgr.cc + Purpose: Platform Paging Manager.. + +------------------------------------------- */ + +#include +#include + +namespace Kernel::HAL +{ + typedef UInt32 PageTableIndex; + + /// \brief Page store type. + struct ZKA_PAGE_STORE final + { + struct + { + PDE* fPde{nullptr}; + PTE* fPte{nullptr}; + VoidPtr fVAddr{nullptr}; + } fInternalStore; + + Bool fStoreOp{No}; // Store operation in progress. + + bool IsValidPage(PTE* pte) + { + return pte && pte->Present; + } + + bool IsWRPage(PTE* pte) + { + return pte && pte->Wr; + } + + bool IsUserPage(PTE* pte) + { + return pte && pte->User; + } + + static ZKA_PAGE_STORE& The() + { + static ZKA_PAGE_STORE the; + return the; + } + }; + + /// \brief Retrieve the page status of a PTE. + STATIC Void mmi_page_status(PTE* pte) + { + kcout << (pte->Present ? "Present" : "Not Present") << endl; + kcout << (pte->Wr ? "W/R" : "Not W/R") << endl; + kcout << (pte->ExecDisable ? "NX" : "Not NX") << endl; + kcout << (pte->User ? "User" : "Not User") << endl; + } + + STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry); + + /// @brief Maps or allocates a page from virtual_address. + /// @param virtual_address a valid virtual address. + /// @param phys_addr point to physical address. + /// @param flags the flags to put on the page. + /// @return Status code of page manipulation process. + EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, UInt32 flags) + { + if (!virtual_address || + !flags) + return 0; + + // Constants for table index bits + const UInt64 cPmlIndexMask = 0x1FFULL; // Mask for PML4, PDPT, PD, PT index (9 bits) + const UInt64 cPtIndexMask = 0xFFFULL; // Mask for page table index (12 bits) + + UInt64 cr3 = (UInt64)hal_read_cr3(); + + ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); + + // Extract the indices from the virtual address + UInt64 pml4_index = ((UIntPtr)virtual_address >> 39) & cPmlIndexMask; + UInt64 pdpt_index = ((UIntPtr)virtual_address >> 30) & cPmlIndexMask; + UInt64 pd_index = ((UIntPtr)virtual_address >> 21) & cPmlIndexMask; + UInt64 pt_index = ((UIntPtr)virtual_address >> 12) & cPmlIndexMask; + + while (page_store.fStoreOp) + ; + + page_store.fStoreOp = Yes; + + if (page_store.fInternalStore.fVAddr == virtual_address) + { + page_store.fStoreOp = No; + return mmi_map_page_table_entry(page_store.fInternalStore.fVAddr, flags, page_store.fInternalStore.fPte); + } + + const auto cPmlEntrySize = 8; + + // Read the PML4 entry from memory + UInt64 pml4_base = cr3 & ~cPtIndexMask; // CR3 points to the PML4 table base, mask off lower bits + UInt64 pml4_entry = (pml4_base + pml4_index * cPmlEntrySize); // Each entry is 8 bytes + + // Read the PDPT entry + UInt64 pdpt_base = pml4_entry & ~cPtIndexMask; // Get the PDPT base physical address + UInt64 pdpt_entry = (pdpt_base + pdpt_index * cPmlEntrySize); + + // Read the PD entry + UInt64 pd_base = pdpt_entry & ~cPtIndexMask; // Get the Page Directory base physical address + UInt64 pd_entry = (pd_base + pd_index * cPmlEntrySize); + + // Read the PT entry + UInt64 pt_base = pd_entry & ~cPtIndexMask; // Get the Page Table base physical address + UInt64 pt_entry = (pt_base + pt_index * cPmlEntrySize); + + // Lastly, grab the pte entry. + ZKA_PDE* pte_struct = reinterpret_cast(pt_base); + + return mmi_map_page_table_entry(virtual_address, flags, pte_struct->fEntries[pt_entry]); + } + + /// @brief Maps flags for a specific pte. + /// @internal Internal function. + STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry) + { + pt_entry->Present = true; + + if (flags & kMMFlagsWr) + pt_entry->Wr = true; + else if (flags & ~kMMFlagsWr) + pt_entry->Wr = false; + + if (flags & kMMFlagsNX) + pt_entry->ExecDisable = true; + else if (flags & ~kMMFlagsNX) + pt_entry->ExecDisable = false; + + if (flags & kMMFlagsUser) + pt_entry->User = true; + else if (flags & ~kMMFlagsUser) + pt_entry->User = false; + + hal_invl_tlb(reinterpret_cast(pt_entry)); + + mmi_page_status(pt_entry); + + ZKA_PAGE_STORE& page_store = ZKA_PAGE_STORE::The(); + + // Update Internal store. + + page_store.fInternalStore.fPde = nullptr; + page_store.fInternalStore.fPte = pt_entry; + page_store.fInternalStore.fVAddr = virtual_address; + + page_store.fStoreOp = No; + + return 0; + } +} // namespace Kernel::HAL diff --git a/dev/zka/HALKit/AMD64/HalSchedulerCore.cc b/dev/zka/HALKit/AMD64/HalSchedulerCore.cc deleted file mode 100644 index 214b889b..00000000 --- a/dev/zka/HALKit/AMD64/HalSchedulerCore.cc +++ /dev/null @@ -1,56 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include - -using namespace Kernel; - -Void UserProcess::SetImageStart(VoidPtr imageStart) noexcept -{ - if (imageStart == nullptr) - this->Crash(); - - this->Image = imageStart; -} - -namespace Kernel -{ - /***********************************************************************************/ - /// @brief Unimplemented function (crashes by default) - /// @param - /***********************************************************************************/ - - EXTERN_C Void __zka_pure_call(void) - { - UserProcessScheduler::The().CurrentProcess().Leak().Crash(); - } - - Bool hal_check_stack(HAL::StackFramePtr stack_ptr) - { - if (!stack_ptr) - return false; - - return true; - } - - /// @brief Wakes up thread. - /// Wakes up thread from the hang state. - Void mp_wakeup_thread(HAL::StackFrame* stack) - { - Kernel::UserProcessHelper::StartScheduling(); - } - - /// @brief makes the thread sleep on a loop. - /// hooks and hangs thread to prevent code from executing. - Void mp_hang_thread(HAL::StackFrame* stack) - { - while (Yes) - { - /* Nothing to do, code is spinning */ - } - } -} // namespace Kernel diff --git a/dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc b/dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc new file mode 100644 index 00000000..d4ca494c --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalSchedulerCoreAMD64.cc @@ -0,0 +1,46 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include + +namespace Kernel +{ + /***********************************************************************************/ + /// @brief Unimplemented function (crashes by default) + /// @param + /***********************************************************************************/ + + EXTERN_C Void __zka_pure_call(void) + { + UserProcessScheduler::The().CurrentProcess().Leak().Crash(); + } + + Bool hal_check_stack(HAL::StackFramePtr stack_ptr) + { + if (!stack_ptr) + return false; + + return true; + } + + /// @brief Wakes up thread. + /// Wakes up thread from the hang state. + Void mp_wakeup_thread(HAL::StackFrame* stack) + { + Kernel::UserProcessHelper::StartScheduling(); + } + + /// @brief makes the thread sleep on a loop. + /// hooks and hangs thread to prevent code from executing. + Void mp_hang_thread(HAL::StackFrame* stack) + { + while (Yes) + { + /* Nothing to do, code is spinning */ + } + } +} // namespace Kernel diff --git a/dev/zka/HALKit/AMD64/HalTimer.cc b/dev/zka/HALKit/AMD64/HalTimer.cc deleted file mode 100644 index c7f6bea3..00000000 --- a/dev/zka/HALKit/AMD64/HalTimer.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: HalTimer.cc - Purpose: HAL timer - - Revision History: - - 07/07/24: Added file (amlel) - -------------------------------------------- */ - -#include -#include -#include - -// timer slot 0 - -#define cHPETCounterRegValue (0x00) -#define cHPETConfigRegValue (0x20) -#define cHPETCompRegValue (0x24) -#define cHPETInterruptRegValue (0x2C) - -///! BUGS: 0 -///! @file HalTimer.cc -///! @brief Hardware Timer (HPET) - -namespace Kernel::Detail -{ - struct HPET_BLOCK : public Kernel::SDT - { - Kernel::UInt8 hardware_rev_id; - Kernel::UInt8 comparator_count : 5; - Kernel::UInt8 counter_size : 1; - Kernel::UInt8 reserved : 1; - Kernel::UInt8 legacy_replacement : 1; - Kernel::UInt16 pci_vendor_id; - ACPI_ADDRESS address; - Kernel::UInt8 hpet_number; - Kernel::UInt16 minimum_tick; - Kernel::UInt8 page_protection; - } PACKED; -} // namespace Kernel::Detail - -using namespace Kernel; - -HardwareTimer::HardwareTimer(Int64 ms) - : fWaitFor(ms) -{ - auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr); - - auto hpet = (Detail::HPET_BLOCK*)power.Find("HPET").Leak().Leak(); - MUST_PASS(hpet); - - fDigitalTimer = (IntPtr*)hpet->address.Address; -} - -HardwareTimer::~HardwareTimer() -{ - fDigitalTimer = nullptr; - fWaitFor = 0; -} - -Int32 HardwareTimer::Wait() noexcept -{ - if (fWaitFor < 1) - return -1; - - // if not enabled yet. - if (!(*(fDigitalTimer + cHPETConfigRegValue) & (1 << 0))) - { - *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 0); // enable it - *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 3); // one shot conf - } - - UInt64 ticks = fWaitFor / ((*(fDigitalTimer) >> 32) & __UINT32_MAX__); - UInt64 prev = *(fDigitalTimer + cHPETCounterRegValue); - - prev += ticks; - - while (*(fDigitalTimer + cHPETCounterRegValue) < (ticks)) - ; - - return 0; -} diff --git a/dev/zka/HALKit/AMD64/HalTimerAMD64.cc b/dev/zka/HALKit/AMD64/HalTimerAMD64.cc new file mode 100644 index 00000000..c7f6bea3 --- /dev/null +++ b/dev/zka/HALKit/AMD64/HalTimerAMD64.cc @@ -0,0 +1,86 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: HalTimer.cc + Purpose: HAL timer + + Revision History: + + 07/07/24: Added file (amlel) + +------------------------------------------- */ + +#include +#include +#include + +// timer slot 0 + +#define cHPETCounterRegValue (0x00) +#define cHPETConfigRegValue (0x20) +#define cHPETCompRegValue (0x24) +#define cHPETInterruptRegValue (0x2C) + +///! BUGS: 0 +///! @file HalTimer.cc +///! @brief Hardware Timer (HPET) + +namespace Kernel::Detail +{ + struct HPET_BLOCK : public Kernel::SDT + { + Kernel::UInt8 hardware_rev_id; + Kernel::UInt8 comparator_count : 5; + Kernel::UInt8 counter_size : 1; + Kernel::UInt8 reserved : 1; + Kernel::UInt8 legacy_replacement : 1; + Kernel::UInt16 pci_vendor_id; + ACPI_ADDRESS address; + Kernel::UInt8 hpet_number; + Kernel::UInt16 minimum_tick; + Kernel::UInt8 page_protection; + } PACKED; +} // namespace Kernel::Detail + +using namespace Kernel; + +HardwareTimer::HardwareTimer(Int64 ms) + : fWaitFor(ms) +{ + auto power = PowerFactoryInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr); + + auto hpet = (Detail::HPET_BLOCK*)power.Find("HPET").Leak().Leak(); + MUST_PASS(hpet); + + fDigitalTimer = (IntPtr*)hpet->address.Address; +} + +HardwareTimer::~HardwareTimer() +{ + fDigitalTimer = nullptr; + fWaitFor = 0; +} + +Int32 HardwareTimer::Wait() noexcept +{ + if (fWaitFor < 1) + return -1; + + // if not enabled yet. + if (!(*(fDigitalTimer + cHPETConfigRegValue) & (1 << 0))) + { + *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 0); // enable it + *(fDigitalTimer + cHPETConfigRegValue) |= (1 << 3); // one shot conf + } + + UInt64 ticks = fWaitFor / ((*(fDigitalTimer) >> 32) & __UINT32_MAX__); + UInt64 prev = *(fDigitalTimer + cHPETCounterRegValue); + + prev += ticks; + + while (*(fDigitalTimer + cHPETCounterRegValue) < (ticks)) + ; + + return 0; +} diff --git a/dev/zka/HALKit/AMD64/Processor.h b/dev/zka/HALKit/AMD64/Processor.h index 8dbac715..968cf5e9 100644 --- a/dev/zka/HALKit/AMD64/Processor.h +++ b/dev/zka/HALKit/AMD64/Processor.h @@ -71,11 +71,12 @@ namespace Kernel::HAL /// @brief Memory Manager mapping flags. enum { - eFlagsPresent = 1 << 0, - eFlagsWr = 1 << 1, - eFlagsUser = 1 << 2, - eFlagsNX = 1 << 3, - eFlagsCount = 3, + kMMFlagsInvalid = 0 << 0, + kMMFlagsPresent = 1 << 0, + kMMFlagsWr = 1 << 1, + kMMFlagsUser = 1 << 2, + kMMFlagsNX = 1 << 3, + kMMFlagsCount = 4, }; struct PACKED Register64 final diff --git a/dev/zka/HALKit/ARM64/HalSchedulerCore.cc b/dev/zka/HALKit/ARM64/HalSchedulerCore.cc deleted file mode 100644 index a384db22..00000000 --- a/dev/zka/HALKit/ARM64/HalSchedulerCore.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include - -namespace Kernel -{ - EXTERN_C Void __zka_pure_call(void) - { - UserProcessScheduler::The().CurrentProcess().Leak().Crash(); - } - - Void UserProcess::SetImageStart(VoidPtr image_start) noexcept - { - if (image_start == 0) - this->Crash(); - - this->Image = image_start; - } - - bool hal_check_stack(HAL::StackFramePtr stackPtr) - { - if (!stackPtr) - return false; - if (stackPtr->BP == 0 || stackPtr->SP == 0) - return false; - - return true; - } - - /// @brief Wakes up thread. - /// Wakes up thread from the hang state. - Void mp_wakeup_thread(HAL::StackFrame* stack) - { - ZKA_UNUSED(stack); - } - - /// @brief makes the thread sleep on a loop. - /// hooks and hangs thread to prevent code from executing. - Void mp_hang_thread(HAL::StackFrame* stack) - { - while (Yes) - { - /* Nothing to do, code is spinning */ - } - } -} // namespace Kernel diff --git a/dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc b/dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc new file mode 100644 index 00000000..49927c84 --- /dev/null +++ b/dev/zka/HALKit/ARM64/HalSchedulerCoreARM64.cc @@ -0,0 +1,40 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include + +namespace Kernel +{ + EXTERN_C Void __zka_pure_call(void) + { + UserProcessScheduler::The().CurrentProcess().Leak().Crash(); + } + + bool hal_check_stack(HAL::StackFramePtr stackPtr) + { + if (!stackPtr) + return No; + + if (stackPtr->BP == 0 || stackPtr->SP == 0) + return No; + + return Yes; + } + + /// @brief Wakes up thread. + /// Wakes up thread from the hang state. + Void mp_wakeup_thread(HAL::StackFrame* stack) + { + ZKA_UNUSED(stack); + } + + /// @brief makes the thread sleep on a loop. + /// hooks and hangs thread to prevent code from executing. + Void mp_hang_thread(HAL::StackFrame* stack) + { + ZKA_UNUSUED(stack); + } +} // namespace Kernel diff --git a/dev/zka/HALKit/ARM64/Processor.h b/dev/zka/HALKit/ARM64/Processor.h index 5f00e345..793761e2 100644 --- a/dev/zka/HALKit/ARM64/Processor.h +++ b/dev/zka/HALKit/ARM64/Processor.h @@ -24,11 +24,11 @@ namespace Kernel::HAL /// @brief Memory Manager mapping flags. enum { - eFlagsPresent = 1 << 0, - eFlagsWr = 1 << 1, - eFlagsUser = 1 << 2, - eFlagsNX = 1 << 3, - eFlagsCount = 3, + kMMFlagsPresent = 1 << 0, + kMMFlagsWr = 1 << 1, + kMMFlagsUser = 1 << 2, + kMMFlagsNX = 1 << 3, + kMMFlagsCount = 3, }; /// @brief Set a PTE from pd_base. diff --git a/dev/zka/HALKit/POWER/HalVirtualMemory.cc b/dev/zka/HALKit/POWER/HalVirtualMemory.cc index 8d8db849..e21d7074 100644 --- a/dev/zka/HALKit/POWER/HalVirtualMemory.cc +++ b/dev/zka/HALKit/POWER/HalVirtualMemory.cc @@ -4,32 +4,30 @@ ------------------------------------------- */ -#include -#include - #include #include +#include -/// @note refer to the SoC documentation. +/// @note Refer to SoC documentation. using namespace Kernel; -Void hal_write_tlb(UInt32 mas0, UInt32 mas1, UInt32 mas2, UInt32 mas3, UInt32 mas7) +EXTERN_C Void hal_write_tlb(UInt32 mas0, UInt32 mas1, UInt32 mas2, UInt32 mas3, UInt32 mas7) { - mtspr(MAS0, mas0); - mtspr(MAS1, mas1); - mtspr(MAS2, mas2); - mtspr(MAS3, mas3); - mtspr(MAS7, mas7); + hal_mtspr(MAS0, mas0); + hal_mtspr(MAS1, mas1); + hal_mtspr(MAS2, mas2); + hal_mtspr(MAS3, mas3); + hal_mtspr(MAS7, mas7); hal_flush_tlb(); } -Bool hal_set_tlb(UInt8 tlb, UInt32 epn, UInt64 rpn, UInt8 perms, UInt8 wimge, UInt8 ts, UInt8 esel, UInt8 tsize, UInt8 iprot) +EXTERN_C Bool hal_set_tlb(UInt8 tlb, UInt32 epn, UInt64 rpn, UInt8 perms, UInt8 wimge, UInt8 ts, UInt8 esel, UInt8 tsize, UInt8 iprot) { - if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 && (tsize & 1)) + if ((hal_mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 && (tsize & 1)) { - // this mmu-version does not allow odd tsize values + // this MMU does not allow odd tsize values return false; } diff --git a/dev/zka/HALKit/POWER/Hart.h b/dev/zka/HALKit/POWER/Hart.h index eaf00493..2e40784e 100644 --- a/dev/zka/HALKit/POWER/Hart.h +++ b/dev/zka/HALKit/POWER/Hart.h @@ -25,7 +25,7 @@ typedef struct HAL_HARDWARE_THREAD { Kernel::UIntPtr fStartAddress; Kernel::UInt8 fPrivleged : 1; - Kernel::UInt32 fPageFlags; + Kernel::UInt32 fPagkMMFlags; PPCHartType fIdentNumber; } HAL_HARDWARE_THREAD; diff --git a/dev/zka/HALKit/POWER/Processor.h b/dev/zka/HALKit/POWER/Processor.h index 95c1f1c6..4df55fc4 100644 --- a/dev/zka/HALKit/POWER/Processor.h +++ b/dev/zka/HALKit/POWER/Processor.h @@ -29,6 +29,8 @@ namespace Kernel::HAL Reg R13{0}; Reg R14{0}; Reg R15{0}; + Reg SP{0}; + Reg BP{0}; }; typedef StackFrame* StackFramePtr; -- cgit v1.2.3