From 19d0857d84cbc0267a8b222368e143bdcdaaf9a7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 19 Jun 2024 17:56:55 +0200 Subject: ARM64: Got into the linking stage, writing missing drivers in HAL now. Signed-off-by: Amlal El Mahrouss --- Kernel/HALKit/AMD64/HalPageAlloc.cpp | 103 ---------------------------------- Kernel/HALKit/AMD64/HalPageAlloc.hpp | 2 +- Kernel/HALKit/AMD64/Processor.hpp | 12 ++-- Kernel/HALKit/ARM64/HalPageAlloc.hpp | 82 +++++++++++++++++++++++++++ Kernel/HALKit/ARM64/HalPageInternal.S | 5 ++ Kernel/HALKit/ARM64/Hart.hxx | 4 +- Kernel/HALKit/ARM64/Processor.hxx | 55 ++++++++++++++++++ Kernel/HALKit/ARM64/Storage/Flash.cxx | 27 +++++++++ Kernel/HALKit/POWER/Hart.hxx | 4 +- Kernel/HALKit/RISCV/Hart.hxx | 4 +- 10 files changed, 183 insertions(+), 115 deletions(-) delete mode 100644 Kernel/HALKit/AMD64/HalPageAlloc.cpp create mode 100644 Kernel/HALKit/ARM64/HalPageAlloc.hpp create mode 100644 Kernel/HALKit/ARM64/HalPageInternal.S create mode 100644 Kernel/HALKit/ARM64/Processor.hxx create mode 100644 Kernel/HALKit/ARM64/Storage/Flash.cxx (limited to 'Kernel/HALKit') diff --git a/Kernel/HALKit/AMD64/HalPageAlloc.cpp b/Kernel/HALKit/AMD64/HalPageAlloc.cpp deleted file mode 100644 index ac043cff..00000000 --- a/Kernel/HALKit/AMD64/HalPageAlloc.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* ------------------------------------------- - - Copyright Zeta Electronics Corporation - -------------------------------------------- */ - -#include -#include -#include -#include - -STATIC NewOS::Boolean kAllocationInProgress = false; - -namespace NewOS -{ - namespace HAL - { - namespace Detail - { - struct VirtualMemoryHeader - { - UInt32 Magic; - Boolean Present; - Boolean ReadWrite; - Boolean User; - SizeT PageSize; - }; - - struct VirtualMemoryHeaderTraits - { - /// @brief Get next header. - /// @param current - /// @return - VirtualMemoryHeader* Next(VirtualMemoryHeader* current) - { - return current + sizeof(PTE) + current->PageSize; - } - - /// @brief Get previous header. - /// @param current - /// @return - VirtualMemoryHeader* Prev(VirtualMemoryHeader* current) - { - return current - sizeof(PTE) - current->PageSize; - } - }; - } // namespace Detail - - /// @brief Allocates a new page of memory. - /// @param sz the size of it. - /// @param rw read/write flag. - /// @param user user flag. - /// @return the page table of it. - STATIC auto hal_try_alloc_new_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr - { - if (kAllocationInProgress) - return nullptr; - - kAllocationInProgress = true; - - constexpr auto cVMTMagic = 0xDEEFD00D; - - ///! fetch from the start. - Detail::VirtualMemoryHeader* vmHeader = reinterpret_cast(kKernelVirtualStart); - Detail::VirtualMemoryHeaderTraits traits; - - while (vmHeader->Present && - vmHeader->Magic != cVMTMagic) - { - vmHeader = traits.Next(vmHeader); - } - - vmHeader->Magic = cVMTMagic; - vmHeader->Present = true; - vmHeader->ReadWrite = rw; - vmHeader->User = user; - vmHeader->PageSize = size; - - kAllocationInProgress = false; - - return reinterpret_cast(vmHeader); - } - - /// @brief Allocate a new page to be used by the OS. - /// @param rw read/write bit. - /// @param user user bit. - /// @return - auto hal_alloc_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr - { - /// Wait for a ongoing allocation to complete. - while (kAllocationInProgress) - { - ; - } - - if (size == 0) - ++size; - - /// allocate new page. - return hal_try_alloc_new_page(rw, user, size); - } - } // namespace HAL -} // namespace NewOS diff --git a/Kernel/HALKit/AMD64/HalPageAlloc.hpp b/Kernel/HALKit/AMD64/HalPageAlloc.hpp index 29947f2f..332c8ed4 100644 --- a/Kernel/HALKit/AMD64/HalPageAlloc.hpp +++ b/Kernel/HALKit/AMD64/HalPageAlloc.hpp @@ -45,7 +45,7 @@ namespace NewOS::HAL bool Cache : 1; bool Accessed : 1; NewOS::Int32 Reserved : 6; - NewOS::UIntPtr PhysicalAddress : 36; + NewOS::UInt64 PhysicalAddress : 36; NewOS::Int32 Reserved1 : 15; bool ExecDisable : 1; }; diff --git a/Kernel/HALKit/AMD64/Processor.hpp b/Kernel/HALKit/AMD64/Processor.hpp index c52fecdf..7c0a4415 100644 --- a/Kernel/HALKit/AMD64/Processor.hpp +++ b/Kernel/HALKit/AMD64/Processor.hpp @@ -54,7 +54,7 @@ namespace NewOS namespace NewOS::HAL { - + /// @brief Virtual memory flags. enum { eFlagsUser, @@ -87,9 +87,11 @@ namespace NewOS::HAL { kcout << "PM is already present.\r"; - kcout << "PhysicalAddress: " << hex_number(pde->Pte[pml4_index].PhysicalAddress) << endl; - kcout << "User: " << (pde->Pte[pml4_index].User ? "yes" : "no") << "\r"; - kcout << "RW: " << (pde->Pte[pml4_index].Rw ? "yes" : "no") << "\r"; + kcout << "PhysicalAddress: " << hex_number(pde->Pte[pml4_index].PhysicalAddress); + kcout << "\r"; + + kcout << "User: " << (pde->Pte[pml4_index].User ? "YES" : "NO") << "\r"; + kcout << "RW: " << (pde->Pte[pml4_index].Rw ? "YES" : "NO") << "\r"; return 1; } @@ -146,7 +148,7 @@ namespace NewOS::HAL using RawRegister = UInt64; using InterruptId = UShort; /* For each element in the IVT */ - using interruptTrap = UIntPtr(UIntPtr sp); + using InterruptTrapKind = UIntPtr(UIntPtr sp); typedef UIntPtr Reg; diff --git a/Kernel/HALKit/ARM64/HalPageAlloc.hpp b/Kernel/HALKit/ARM64/HalPageAlloc.hpp new file mode 100644 index 00000000..57e1e193 --- /dev/null +++ b/Kernel/HALKit/ARM64/HalPageAlloc.hpp @@ -0,0 +1,82 @@ +/* ------------------------------------------- + + Copyright Zeta Electronics Corporation + +------------------------------------------- */ + +#pragma once + +/** --------------------------------------------------- + + * THIS FILE CONTAINS CODE FOR ARM64 PAGING. + +------------------------------------------------------- */ + +#include + +#ifndef kPTEMax +#define kPTEMax (0x200) +#endif //! kPTEMax + +#ifndef kPTEAlign +#define kPTEAlign (0x1000) +#endif //! kPTEAlign + +#ifndef kPTESize +#define kPTESize (0x1000) +#endif // !kPTESize + +EXTERN_C void hal_flush_tlb(); + +namespace NewOS::HAL +{ + struct PACKED PageTable64 final + { + bool Present : 1; + bool Rw : 1; + bool User : 1; + bool Wt : 1; + bool Cache : 1; + bool Accessed : 1; + NewOS::Int32 Reserved : 6; + NewOS::UInt64 PhysicalAddress : 36; + NewOS::Int32 Reserved1 : 15; + bool ExecDisable : 1; + }; + + namespace Detail + { + enum class ControlRegisterBits + { + ProtectedModeEnable = 0, + MonitorCoProcessor = 1, + Emulation = 2, + TaskSwitched = 3, + ExtensionType = 4, + NumericError = 5, + WriteProtect = 16, + AlignementMask = 18, + NotWriteThrough = 29, + CacheDisable = 30, + PageEnable = 31, + }; + + inline UInt8 control_register_cast(ControlRegisterBits reg) + { + return static_cast(reg); + } + } // namespace Detail + + struct PageDirectory64 final + { + PageTable64 ALIGN(kPTEAlign) Pte[kPTEMax]; + }; + + VoidPtr hal_alloc_page(Boolean rw, Boolean user, SizeT size); +} // namespace NewOS::HAL + +namespace NewOS +{ + typedef HAL::PageTable64 PTE; + typedef HAL::PageDirectory64 PDE; +} // namespace NewOS diff --git a/Kernel/HALKit/ARM64/HalPageInternal.S b/Kernel/HALKit/ARM64/HalPageInternal.S new file mode 100644 index 00000000..8fcf40ff --- /dev/null +++ b/Kernel/HALKit/ARM64/HalPageInternal.S @@ -0,0 +1,5 @@ +.text + +hal_flush_tlb: + tlbi + ret diff --git a/Kernel/HALKit/ARM64/Hart.hxx b/Kernel/HALKit/ARM64/Hart.hxx index 1b16a072..5769b01e 100644 --- a/Kernel/HALKit/ARM64/Hart.hxx +++ b/Kernel/HALKit/ARM64/Hart.hxx @@ -16,8 +16,8 @@ typedef NewOS::Int32 Arm64HartType; /// @brief Set PC to specific hart. /// @param hart the hart /// @param epc the pc. -/// @return -EXTERN_C NewOS::Void hal_switch_to_hart(Arm64HartType hart, NewOS::VoidPtr epc); +/// @return +EXTERN_C NewOS::Void hal_set_pc_to_hart(Arm64HartType hart, NewOS::VoidPtr epc); /// @brief Hart IPI enum enum { diff --git a/Kernel/HALKit/ARM64/Processor.hxx b/Kernel/HALKit/ARM64/Processor.hxx new file mode 100644 index 00000000..8b06794f --- /dev/null +++ b/Kernel/HALKit/ARM64/Processor.hxx @@ -0,0 +1,55 @@ +/* ------------------------------------------- + + Copyright Zeta Electronics Corporation + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include + +#ifdef kCPUBackendName +#undef kCPUBackendName +#endif // ifdef kCPUBackendName + +#define kPTESize 512 /* 64-bit PT */ + +#define kCPUBackendName "ARM64" + +#ifdef __ZETA_MACHINE__ +#define kVirtualAddressStartOffset (0x80000000) +#else +#error !!! please provide that macro. !!! +#endif + +namespace NewOS::HAL +{ + struct PACKED Register64 final + { + UShort Limit; + UIntPtr Base; + }; + + typedef UIntPtr Reg; + typedef Register64 Register; + + struct PACKED StackFrame final + { + Reg IntNum, Exception; + Reg Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax; + Reg R8, R9, R10, R11, R12, R13, R14, R15; + Reg Gs, Fs; + }; + + typedef StackFrame* StackFramePtr; +} + +inline NewOS::VoidPtr kKernelVirtualStart = (NewOS::VoidPtr)kVirtualAddressStartOffset; +inline NewOS::UIntPtr kKernelVirtualSize = 0UL; + +inline NewOS::VoidPtr kKernelPhysicalStart = nullptr; + +#include diff --git a/Kernel/HALKit/ARM64/Storage/Flash.cxx b/Kernel/HALKit/ARM64/Storage/Flash.cxx new file mode 100644 index 00000000..8afd0f58 --- /dev/null +++ b/Kernel/HALKit/ARM64/Storage/Flash.cxx @@ -0,0 +1,27 @@ +/* ------------------------------------------- + + Copyright Zeta Electronics Corporation + +------------------------------------------- */ + +#include +#include + +/// @brief MBCI flash drive. + +#ifdef __FLASH_MEM__ + +namespace NewOS +{ + SizeT drv_std_get_sector_count(void) + { + return 0; + } + + SizeT drv_std_get_drv_size(void) + { + return 0; + } +} // namespace NewOS + +#endif // if __FLASH_MEM__ (MBCI) diff --git a/Kernel/HALKit/POWER/Hart.hxx b/Kernel/HALKit/POWER/Hart.hxx index 10066048..ab74be4a 100644 --- a/Kernel/HALKit/POWER/Hart.hxx +++ b/Kernel/HALKit/POWER/Hart.hxx @@ -30,5 +30,5 @@ typedef struct HalHardwareThread /// @brief Set PC to specific hart. /// @param hart the hart /// @param epc the pc. -/// @return -EXTERN_C NewOS::Void hal_switch_to_hart(HalHardwareThread* hart, NewOS::VoidPtr epc); \ No newline at end of file +/// @return +EXTERN_C NewOS::Void hal_set_pc_to_hart(HalHardwareThread* hart, NewOS::VoidPtr epc); diff --git a/Kernel/HALKit/RISCV/Hart.hxx b/Kernel/HALKit/RISCV/Hart.hxx index ba56b7c9..38160322 100644 --- a/Kernel/HALKit/RISCV/Hart.hxx +++ b/Kernel/HALKit/RISCV/Hart.hxx @@ -20,5 +20,5 @@ typedef NewOS::Int32 Rv64HartType; /// @brief Set PC to specific hart. /// @param hart the hart /// @param epc the pc. -/// @return -EXTERN_C NewOS::Void hal_switch_to_hart(Rv64HartType hart, NewOS::VoidPtr epc); \ No newline at end of file +/// @return +EXTERN_C NewOS::Void hal_set_pc_to_hart(Rv64HartType hart, NewOS::VoidPtr epc); -- cgit v1.2.3