summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/AMD64
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-21 03:45:08 +0200
committerAmlal El Mahrouss <amlal@nekernel.org>2025-05-21 03:45:08 +0200
commitc85a99c2afdd4c9dfa9d8f0f212e4625b6adade7 (patch)
tree3bb631cf7c49b74fbf6d02104059e0540f26ba4b /dev/kernel/HALKit/AMD64
parentbec0e457ad346783be7f84be71bb0eddc881847c (diff)
feat(kernel): source code improvements.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/HALKit/AMD64')
-rw-r--r--dev/kernel/HALKit/AMD64/HalAPStartup.s12
-rw-r--r--dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc67
-rw-r--r--dev/kernel/HALKit/AMD64/HalApplicationProcessorGNU.s8
-rw-r--r--dev/kernel/HALKit/AMD64/HalApplicationProcessorStartup.asm75
-rw-r--r--dev/kernel/HALKit/AMD64/Paging.h4
-rw-r--r--dev/kernel/HALKit/AMD64/Processor.h8
-rwxr-xr-xdev/kernel/HALKit/AMD64/make_ap_blob.sh3
7 files changed, 58 insertions, 119 deletions
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 <ArchKit/ArchKit.h>
-#include <HALKit/AMD64/Processor.h>
-#include <KernelKit/BinaryMutex.h>
-#include <KernelKit/HardwareThreadScheduler.h>
-#include <KernelKit/ProcessScheduler.h>
-#include <KernelKit/Timer.h>
-#include <NewKit/KernelPanic.h>
-#include <modules/ACPI/ACPIFactoryInterface.h>
-#include <modules/CoreGfx/TextGfx.h>
-#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 <ArchKit/ArchKit.h>
+#include <HALKit/AMD64/Processor.h>
+#include <KernelKit/BinaryMutex.h>
+#include <KernelKit/HardwareThreadScheduler.h>
+#include <KernelKit/ProcessScheduler.h>
+#include <KernelKit/Timer.h>
+#include <NewKit/KernelPanic.h>
+#include <modules/ACPI/ACPIFactoryInterface.h>
+#include <modules/CoreGfx/TextGfx.h>
+
/// @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;
}
@@ -131,19 +145,10 @@ Bool mp_is_smp(Void) noexcept {
}
/***********************************************************************************/
-/// @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 <FirmwareKit/Handover.h>
#include <HALKit/AMD64/Paging.h>
#include <NewKit/Array.h>
@@ -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