From c85a99c2afdd4c9dfa9d8f0f212e4625b6adade7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 21 May 2025 03:45:08 +0200 Subject: feat(kernel): source code improvements. Signed-off-by: Amlal El Mahrouss --- dev/kernel/HALKit/AMD64/HalAPStartup.s | 12 +++ dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc | 67 ++++++++------- .../HALKit/AMD64/HalApplicationProcessorGNU.s | 8 -- .../AMD64/HalApplicationProcessorStartup.asm | 75 ----------------- dev/kernel/HALKit/AMD64/Paging.h | 4 + dev/kernel/HALKit/AMD64/Processor.h | 8 +- dev/kernel/HALKit/AMD64/make_ap_blob.sh | 3 - dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc | 97 ++++++++++------------ dev/kernel/HALKit/ARM64/HalCommonAPI.s | 9 ++ dev/kernel/HALKit/ARM64/HalFlushTLB.S | 9 -- dev/kernel/HALKit/ARM64/HalInterruptAPI.s | 3 + dev/kernel/HALKit/ARM64/HalTimerARM64.cc | 1 + dev/kernel/HALKit/ARM64/Paging.h | 4 + dev/kernel/HALKit/ARM64/Processor.h | 28 +++---- 14 files changed, 131 insertions(+), 197 deletions(-) create mode 100644 dev/kernel/HALKit/AMD64/HalAPStartup.s delete mode 100644 dev/kernel/HALKit/AMD64/HalApplicationProcessorGNU.s delete mode 100644 dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm delete mode 100755 dev/kernel/HALKit/AMD64/make_ap_blob.sh create mode 100644 dev/kernel/HALKit/ARM64/HalCommonAPI.s delete mode 100644 dev/kernel/HALKit/ARM64/HalFlushTLB.S create mode 100644 dev/kernel/HALKit/ARM64/HalInterruptAPI.s (limited to 'dev/kernel/HALKit') diff --git a/dev/kernel/HALKit/AMD64/HalAPStartup.s b/dev/kernel/HALKit/AMD64/HalAPStartup.s new file mode 100644 index 00000000..ec17bf7b --- /dev/null +++ b/dev/kernel/HALKit/AMD64/HalAPStartup.s @@ -0,0 +1,12 @@ +.data + +.global hal_ap_blob_start +.global hal_ap_blob_length + +hal_ap_blob_start: + cli + hlt + jmp hal_ap_blob_start + +hal_ap_blob_length: + .long 4 diff --git a/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc b/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc index 46ad8fd6..e4ad1024 100644 --- a/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc @@ -4,21 +4,8 @@ ------------------------------------------- */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "NewKit/Defines.h" - #define APIC_MAG "APIC" -#define AP_BLOB_SIZE 126 - #define APIC_ICR_LOW 0x300 #define APIC_ICR_HIGH 0x310 #define APIC_SIPI_VEC 0x00500 @@ -35,6 +22,16 @@ #define APIC_BASE_MSR_BSP 0x100 #define APIC_BASE_MSR_ENABLE 0x800 +#include +#include +#include +#include +#include +#include +#include +#include +#include + /// @note: _hal_switch_context is internal /////////////////////////////////////////////////////////////////////////////////////// @@ -44,8 +41,6 @@ /////////////////////////////////////////////////////////////////////////////////////// namespace Kernel::HAL { -EXTERN_C Void sched_jump_to_task(HAL::StackFramePtr stack_frame); - struct HAL_APIC_MADT; struct HAL_HARDWARE_THREAD; @@ -54,6 +49,8 @@ struct HAL_HARDWARE_THREAD final { ProcessID mThreadID{0}; }; +EXTERN_C Void sched_jump_to_task(HAL::StackFramePtr stack_frame); + STATIC HAL_APIC_MADT* kMADTBlock = nullptr; STATIC Bool kSMPAware = false; STATIC Int64 kSMPCount = 0; @@ -64,6 +61,8 @@ STATIC Int32 kSMPInterrupt = 0; STATIC UInt64 kAPICLocales[kMaxAPInsideSched] = {0}; STATIC VoidPtr kRawMADT = nullptr; +STATIC HAL_HARDWARE_THREAD kHWThread[kSchedProcessLimitPerTeam] = {{}}; + /// @brief Multiple APIC Descriptor Table. struct HAL_APIC_MADT final SDT_OBJECT { UInt32 Address; // Madt address @@ -97,7 +96,10 @@ Void hal_send_ipi_msg(UInt32 target, UInt32 apic_id, UInt8 vector) { } } -STATIC HAL_HARDWARE_THREAD kHWThread[kSchedProcessLimitPerTeam] = {{}}; +/***********************************************************************************/ +/// @brief Get current stack frame for a thread. +/// @param thrdid The thread ID. +/***********************************************************************************/ EXTERN_C HAL::StackFramePtr mp_get_current_context(Int64 thrdid) { const auto process_index = thrdid % kSchedProcessLimitPerTeam; @@ -105,16 +107,28 @@ EXTERN_C HAL::StackFramePtr mp_get_current_context(Int64 thrdid) { return kHWThread[process_index].mFramePtr; } +/***********************************************************************************/ +/// @brief Register current stack frame for a thread. +/// @param stack_frame The current stack frame. +/// @param thrdid The thread ID. +/***********************************************************************************/ + EXTERN_C BOOL mp_register_process(HAL::StackFramePtr stack_frame, ProcessID thrdid) { if (thrdid > kSMPCount) return NO; - if (mp_is_smp()) { - kHWThread[thrdid].mFramePtr = stack_frame; - kHWThread[thrdid].mThreadID = thrdid; + if (!mp_is_smp()) { + if (stack_frame) { + kHWThread[thrdid].mFramePtr = stack_frame; + kHWThread[thrdid].mThreadID = thrdid; + HardwareThreadScheduler::The()[thrdid].Leak()->Busy(NO); - HardwareThreadScheduler::The()[thrdid].Leak()->Busy(NO); + sched_jump_to_task(stack_frame); - sched_jump_to_task(stack_frame); + return YES; + } + } else { + kHWThread[thrdid].mFramePtr = stack_frame; + kHWThread[thrdid].mThreadID = thrdid; return YES; } @@ -130,20 +144,11 @@ Bool mp_is_smp(Void) noexcept { return kSMPAware; } -/***********************************************************************************/ -/// @brief Assembly symbol to bootstrap AP. -/***********************************************************************************/ -EXTERN_C Char* hal_ap_blob_start; - -/***********************************************************************************/ -/// @brief Assembly symbol to bootstrap AP. -/***********************************************************************************/ -EXTERN_C Char* hal_ap_blob_end; - /***********************************************************************************/ /// @brief Fetch and enable SMP scheduler. /// @param vendor_ptr SMP containing structure. /***********************************************************************************/ + Void mp_init_cores(VoidPtr vendor_ptr) noexcept { if (!vendor_ptr) return; diff --git a/dev/kernel/HALKit/AMD64/HalApplicationProcessorGNU.s b/dev/kernel/HALKit/AMD64/HalApplicationProcessorGNU.s deleted file mode 100644 index a8ad3b76..00000000 --- a/dev/kernel/HALKit/AMD64/HalApplicationProcessorGNU.s +++ /dev/null @@ -1,8 +0,0 @@ -.data - -.global hal_ap_blob_start /* Export the start symbol */ -.global hal_ap_blob_end /* Export the end symbol */ - -hal_ap_blob_start: - .incbin "HALKit/AMD64/HalApplicationProcessorStartup.bin" -hal_ap_blob_end: diff --git a/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm b/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm deleted file mode 100644 index 2adc8fed..00000000 --- a/dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm +++ /dev/null @@ -1,75 +0,0 @@ -;; /* -;; * ======================================================== -;; * -;; * NeKernel -;; * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. -;; * -;; * 25/03/25: FIX: Fix warning regarding resb being used inside a non-bss area (using no-op instead). -;; * -;; * ======================================================== -;; */ - -[bits 16] -[org 0x7c00] - -hal_ap_start: - mov ax, 0x0 - mov ss, ax - mov esp, 0x7000 - - cli - mov eax, cr0 - or eax, 1 - mov cr0, eax - jmp .hal_ap_start_flush -.hal_ap_start_flush: - mov ax, 0x10 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - - mov eax, cr4 - or eax, 1 << 5 - mov cr4, eax - - mov eax, cr3 - mov cr3, eax - - mov ecx, 0xC0000080 - rdmsr - or eax, 1 - wrmsr - - mov eax, cr0 - or eax, (1 << 31) - mov cr0, eax - - jmp 0x08:hal_ap_64bit_entry -hal_ap_end: - -hal_ap_length: - dq hal_ap_end - hal_ap_start - -[bits 64] - -hal_ap_64bit_entry: - mov ax, 0x23 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - - mov rsp, rbx - - push 0x33 - lea rax, [hal_ap_64bit_entry_loop] - push rax - o64 pushf - - o64 iret - -hal_ap_64bit_entry_loop: - jmp $ diff --git a/dev/kernel/HALKit/AMD64/Paging.h b/dev/kernel/HALKit/AMD64/Paging.h index 061bae45..b73b8604 100644 --- a/dev/kernel/HALKit/AMD64/Paging.h +++ b/dev/kernel/HALKit/AMD64/Paging.h @@ -6,6 +6,8 @@ #pragma once +#ifdef __NE_AMD64__ + /** --------------------------------------------------- * THIS FILE CONTAINS CODE FOR X86_64 PAGING. @@ -85,3 +87,5 @@ struct PDE { ATTRIBUTE(aligned(kib_cast(4))) PTE fPTE[512]; }; } // namespace Kernel + +#endif // __NE_AMD64__ \ No newline at end of file diff --git a/dev/kernel/HALKit/AMD64/Processor.h b/dev/kernel/HALKit/AMD64/Processor.h index 8fb69c0c..c574f8d5 100644 --- a/dev/kernel/HALKit/AMD64/Processor.h +++ b/dev/kernel/HALKit/AMD64/Processor.h @@ -13,6 +13,8 @@ #pragma once +#ifdef __NE_AMD64__ + #include #include #include @@ -73,7 +75,7 @@ enum { kMMFlagsNX = 1 << 4, kMMFlagsPCD = 1 << 5, kMMFlagsPwt = 1 << 6, - kMMFlagsCount = 4, + kMMFlagsCount = 6, }; struct PACKED Register64 final { @@ -283,4 +285,6 @@ EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_gdt(Kernel::HAL::Register64 ptr) inline Kernel::VoidPtr kKernelBitMpStart = nullptr; inline Kernel::UIntPtr kKernelBitMpSize = 0UL; -inline Kernel::VoidPtr kKernelCR3 = nullptr; \ No newline at end of file +inline Kernel::VoidPtr kKernelCR3 = nullptr; + +#endif // __NE_AMD64__ */ \ No newline at end of file diff --git a/dev/kernel/HALKit/AMD64/make_ap_blob.sh b/dev/kernel/HALKit/AMD64/make_ap_blob.sh deleted file mode 100755 index 3f079187..00000000 --- a/dev/kernel/HALKit/AMD64/make_ap_blob.sh +++ /dev/null @@ -1,3 +0,0 @@ -# !/bin/sh - -nasm -f bin HalApplicationProcessorStartup.asm -o HalApplicationProcessorStartup.bin \ No newline at end of file diff --git a/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc b/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc index d37a3e54..5be41e4e 100644 --- a/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc @@ -4,6 +4,23 @@ ------------------------------------------- */ +#define GICD_BASE 0x08000000 +#define GICC_BASE 0x08010000 + +#define GICD_CTLR 0x000 +#define GICD_ISENABLER 0x100 +#define GICD_ICENABLER 0x180 +#define GICD_ISPENDR 0x200 +#define GICD_ICPENDR 0x280 +#define GICD_IPRIORITYR 0x400 +#define GICD_ITARGETSR 0x800 +#define GICD_ICFGR 0xC00 + +#define GICC_CTLR 0x000 +#define GICC_PMR 0x004 +#define GICC_IAR 0x00C +#define GICC_EOIR 0x010 + #include #include #include @@ -11,28 +28,11 @@ #include #include -#define GICD_BASE 0x08000000 // Distributor base address -#define GICC_BASE 0x08010000 // CPU interface base address - -#define GICD_CTLR 0x000 // Distributor Control Register -#define GICD_ISENABLER 0x100 // Interrupt Set-Enable Registers -#define GICD_ICENABLER 0x180 // Interrupt Clear-Enable Registers -#define GICD_ISPENDR 0x200 // Interrupt Set-Pending Registers -#define GICD_ICPENDR 0x280 // Interrupt Clear-Pending Registers -#define GICD_IPRIORITYR 0x400 // Interrupt Priority Registers -#define GICD_ITARGETSR 0x800 // Interrupt Processor Targets Registers -#define GICD_ICFGR 0xC00 // Interrupt Configuration Registers - -#define GICC_CTLR 0x000 // CPU Interface Control Register -#define GICC_PMR 0x004 // Interrupt Priority Mask Register -#define GICC_IAR 0x00C // Interrupt Acknowledge Register -#define GICC_EOIR 0x010 // End of Interrupt Register - // ================================================================= // namespace Kernel { struct HAL_HARDWARE_THREAD final { - HAL::StackFramePtr mFrame; + HAL::StackFramePtr mFramePtr; ProcessID mThreadID{0}; }; @@ -41,49 +41,37 @@ STATIC HAL_HARDWARE_THREAD kHWThread[kMaxAPInsideSched] = {{nullptr}}; namespace Detail { STATIC BOOL kGICEnabled = NO; - STATIC void mp_hang_fn(void) { - while (YES) - ; - - dbg_break_point(); - } - - Void mp_setup_gic_el0(Void) { - // enable distributor. + /***********************************************************************************/ + /// @brief Enables the GIC with EL0 configuration. + /// @internal + /***********************************************************************************/ + STATIC Void mp_setup_gic_el0(Void) { ke_dma_write(GICD_BASE, GICD_CTLR, YES); UInt32 gicc_ctlr = ke_dma_read(GICC_BASE, GICC_CTLR); - const auto kEnableSignalInt = YES; + const UInt8 kEnableSignalInt = 0x1; - gicc_ctlr |= kEnableSignalInt; // Enable signaling of interrupts - gicc_ctlr |= (kEnableSignalInt << 1); // Allow Group 1 interrupts in EL0 + gicc_ctlr |= kEnableSignalInt; + gicc_ctlr |= (kEnableSignalInt << 0x1); ke_dma_write(GICC_BASE, GICC_CTLR, gicc_ctlr); - // Set priority mask (accept all priorities) ke_dma_write(GICC_BASE, GICC_PMR, 0xFF); UInt32 icfgr = ke_dma_read(GICD_BASE, GICD_ICFGR + (0x20 / 0x10) * 4); - icfgr |= (0x2 << ((32 % 16) * 2)); // Edge-triggered - ke_dma_write(GICD_BASE, GICD_ICFGR + (0x20 / 0x10) * 4, icfgr); + icfgr |= (0x2 << ((32 % 16) * 2)); - // Target interrupt 32 to CPU 1 + ke_dma_write(GICD_BASE, GICD_ICFGR + (0x20 / 0x10) * 4, icfgr); ke_dma_write(GICD_BASE, GICD_ITARGETSR + (0x20 / 0x04) * 4, 0x2 << ((32 % 4) * 8)); - - // Set interrupt 32 priority to lowest (0xFF) ke_dma_write(GICD_BASE, GICD_IPRIORITYR + (0x20 / 0x04) * 4, 0xFF << ((32 % 4) * 8)); - - // Enable interrupt 32 for AP. ke_dma_write(GICD_BASE, GICD_ISENABLER + 4, 0x01); } - BOOL mp_handle_gic_interrupt_el0(Void) { - // Read the interrupt ID + EXTERN_C BOOL mp_handle_gic_interrupt_el0(Void) { UInt32 interrupt_id = ke_dma_read(GICC_BASE, GICC_IAR); - // Check if it's a valid interrupt (not spurious) if ((interrupt_id & 0x3FF) < 1020) { auto interrupt = interrupt_id & 0x3FF; @@ -106,15 +94,25 @@ namespace Detail { return YES; } - // spurious interrupt return NO; } } // namespace Detail +/***********************************************************************************/ +/// @brief Get current stack frame for a thread. +/// @param thrdid The thread ID. +/***********************************************************************************/ + EXTERN_C HAL::StackFramePtr mp_get_current_context(ProcessID thrdid) { - return kHWThread[thrdid].mFrame; + return kHWThread[thrdid].mFramePtr; } +/***********************************************************************************/ +/// @brief Register current stack frame for a thread. +/// @param stack_frame The current stack frame. +/// @param thrdid The thread ID. +/***********************************************************************************/ + EXTERN_C Bool mp_register_process(HAL::StackFramePtr stack_frame, ProcessID thrdid) { MUST_PASS(Detail::kGICEnabled); @@ -123,25 +121,20 @@ EXTERN_C Bool mp_register_process(HAL::StackFramePtr stack_frame, ProcessID thrd const auto process_index = thrdid; - kHWThread[process_index].mFrame = stack_frame; + kHWThread[process_index].mFramePtr = stack_frame; kHWThread[process_index].mThreadID = thrdid; - STATIC HardwareTimer timer{rtl_milliseconds(1000)}; - timer.Wait(); - - HardwareThreadScheduler::The()[thrdid].Leak()->Busy(NO); - return YES; } -/// @internal +/***********************************************************************************/ /// @brief Initialize the Global Interrupt Controller. +/// @internal +/***********************************************************************************/ Void mp_init_cores(Void) noexcept { if (!Detail::kGICEnabled) { Detail::kGICEnabled = YES; Detail::mp_setup_gic_el0(); } - - return Detail::kGICEnabled; } } // namespace Kernel \ No newline at end of file diff --git a/dev/kernel/HALKit/ARM64/HalCommonAPI.s b/dev/kernel/HALKit/ARM64/HalCommonAPI.s new file mode 100644 index 00000000..e76b6e3f --- /dev/null +++ b/dev/kernel/HALKit/ARM64/HalCommonAPI.s @@ -0,0 +1,9 @@ +/* (c) 2024-2025 Amlal El Mahrouss */ + +.text + +.global hal_flush_tlb + +hal_flush_tlb: + tlbi + ret diff --git a/dev/kernel/HALKit/ARM64/HalFlushTLB.S b/dev/kernel/HALKit/ARM64/HalFlushTLB.S deleted file mode 100644 index e76b6e3f..00000000 --- a/dev/kernel/HALKit/ARM64/HalFlushTLB.S +++ /dev/null @@ -1,9 +0,0 @@ -/* (c) 2024-2025 Amlal El Mahrouss */ - -.text - -.global hal_flush_tlb - -hal_flush_tlb: - tlbi - ret diff --git a/dev/kernel/HALKit/ARM64/HalInterruptAPI.s b/dev/kernel/HALKit/ARM64/HalInterruptAPI.s new file mode 100644 index 00000000..cafebb7d --- /dev/null +++ b/dev/kernel/HALKit/ARM64/HalInterruptAPI.s @@ -0,0 +1,3 @@ +/* (c) 2024-2025 Amlal El Mahrouss */ + +.text diff --git a/dev/kernel/HALKit/ARM64/HalTimerARM64.cc b/dev/kernel/HALKit/ARM64/HalTimerARM64.cc index 32f64aec..2a595f11 100644 --- a/dev/kernel/HALKit/ARM64/HalTimerARM64.cc +++ b/dev/kernel/HALKit/ARM64/HalTimerARM64.cc @@ -12,3 +12,4 @@ ------------------------------------------- */ #include +#include \ No newline at end of file diff --git a/dev/kernel/HALKit/ARM64/Paging.h b/dev/kernel/HALKit/ARM64/Paging.h index 2eb02bc1..88eedcd8 100644 --- a/dev/kernel/HALKit/ARM64/Paging.h +++ b/dev/kernel/HALKit/ARM64/Paging.h @@ -12,6 +12,8 @@ ------------------------------------------------------- */ +#ifdef __NE_ARM64__ + #include #ifndef kPageMax @@ -101,3 +103,5 @@ typedef HAL::PDE_4KB PDE; } // namespace Kernel EXTERN_C void hal_flush_tlb(); + +#endif // __NE_ARM64__ \ No newline at end of file diff --git a/dev/kernel/HALKit/ARM64/Processor.h b/dev/kernel/HALKit/ARM64/Processor.h index 9f16d8f5..f52b854f 100644 --- a/dev/kernel/HALKit/ARM64/Processor.h +++ b/dev/kernel/HALKit/ARM64/Processor.h @@ -6,12 +6,14 @@ #pragma once +#ifdef __NE_ARM64__ + #include #include #include #include -#define kCPUBackendName "ARMv8" +#define kCPUBackendName "aarch64" namespace Kernel::HAL { struct PACKED Register64 final { @@ -21,11 +23,11 @@ struct PACKED Register64 final { /// @brief Memory Manager mapping flags. enum { - kMMFlagsPresent = 1 << 0, - kMMFlagsWr = 1 << 1, - kMMFlagsUser = 1 << 2, - kMMFlagsNX = 1 << 3, - kMMFlagsPCD = 1 << 4, + kMMFlagsInvalid = 1 << 0, + kMMFlagsPresent = 1 << 1, + kMMFlagsWr = 1 << 2, + kMMFlagsUser = 1 << 3, + kMMFlagsNX = 1 << 4, kMMFlagsCount = 4, }; @@ -62,16 +64,6 @@ inline Void rt_halt() noexcept { } } -template -inline void hal_dma_write(UIntPtr address, DataKind value) { - *reinterpret_cast(address) = value; -} - -template -inline DataKind hal_dma_read(UIntPtr address) { - return *reinterpret_cast(address); -} - inline Void hal_wfi(Void) { asm volatile("wfi"); } @@ -80,6 +72,8 @@ inline Void hal_wfi(Void) { inline Kernel::VoidPtr kKernelBitMpStart = nullptr; inline Kernel::UIntPtr kKernelBitMpSize = 0UL; -inline Kernel::VoidPtr kKernelPhysicalStart = nullptr; +inline Kernel::VoidPtr kKernelPDE = nullptr; #include + +#endif // __NE_ARM64__ \ No newline at end of file -- cgit v1.2.3