summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/HALKit/AMD64
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-08-31 10:47:14 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-08-31 10:47:14 +0200
commit2c4b02249ec4355a73b826909ab1889e45871faf (patch)
treea904b374c7a738dd3dcb0a580e5667714b72682a /dev/ZKA/HALKit/AMD64
parentd65e85588388bf8c2e0dc8f6b106947110c0e815 (diff)
Saving progress on User scheduler implementation.
+ Working on RISC-V, ARM64, POWER64 HALs, to be able to distribute mulitple versions of the product. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/ZKA/HALKit/AMD64')
-rw-r--r--dev/ZKA/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cxx16
-rw-r--r--dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx (renamed from dev/ZKA/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx)50
-rw-r--r--dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm4
-rw-r--r--dev/ZKA/HALKit/AMD64/HalKernelMain.cxx18
-rw-r--r--dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm46
-rw-r--r--dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx26
-rw-r--r--dev/ZKA/HALKit/AMD64/HalSchedulerCore.cxx11
-rw-r--r--dev/ZKA/HALKit/AMD64/HalTimer.cxx3
-rw-r--r--dev/ZKA/HALKit/AMD64/HalUtils.asm2
-rw-r--r--dev/ZKA/HALKit/AMD64/Processor.hxx6
10 files changed, 68 insertions, 114 deletions
diff --git a/dev/ZKA/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cxx b/dev/ZKA/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cxx
index df2df3b0..99b784be 100644
--- a/dev/ZKA/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cxx
@@ -5,42 +5,42 @@
------------------------------------------- */
#include <ArchKit/ArchKit.hxx>
-#include <KernelKit/ProcessScheduler.hxx>
+#include <KernelKit/UserProcessScheduler.hxx>
#include <NewKit/String.hxx>
/// @brief Handle GPF fault.
/// @param rsp
EXTERN_C void idt_handle_gpf(Kernel::UIntPtr rsp)
{
- Kernel::ke_stop(RUNTIME_CHECK_BAD_BEHAVIOR);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Crash();
}
/// @brief Handle page fault.
/// @param rsp
EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp)
{
- Kernel::ke_stop(RUNTIME_CHECK_BAD_BEHAVIOR);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Crash();
}
/// @brief Handle math fault.
/// @param rsp
EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp)
{
- Kernel::ke_stop(RUNTIME_CHECK_BAD_BEHAVIOR);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Crash();
}
/// @brief Handle any generic fault.
/// @param rsp
EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp)
{
- Kernel::ke_stop(RUNTIME_CHECK_BAD_BEHAVIOR);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Crash();
}
/// @brief Handle #UD fault.
/// @param rsp
EXTERN_C void idt_handle_ud(Kernel::UIntPtr rsp)
{
- Kernel::ke_stop(RUNTIME_CHECK_BAD_BEHAVIOR);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Crash();
}
/// @brief Enter syscall from assembly.
@@ -59,10 +59,10 @@ EXTERN_C Kernel::Void hal_system_call_enter(Kernel::UIntPtr rcx, Kernel::UIntPtr
}
}
-/// @brief Enter kernel call from assembly (DDK only).
+/// @brief Enter Kernel call from assembly (DDK only).
/// @param stack the stack pushed from assembly routine.
/// @return nothing.
-EXTERN_C Kernel::Void hal_kernel_call_enter(Kernel::UIntPtr rcx, Kernel::UIntPtr rdx, Kernel::UIntPtr r8, Kernel::UIntPtr r9)
+EXTERN_C Kernel::Void hal_Kernel_call_enter(Kernel::UIntPtr rcx, Kernel::UIntPtr rdx, Kernel::UIntPtr r8, Kernel::UIntPtr r9)
{
if (rcx <= (kSyscalls.Count() - 1))
{
diff --git a/dev/ZKA/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx b/dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx
index 88f77ba9..1f233e08 100644
--- a/dev/ZKA/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx
@@ -9,7 +9,7 @@
#include <NewKit/KernelCheck.hxx>
#include <ArchKit/ArchKit.hxx>
#include <KernelKit/Semaphore.hxx>
-#include <KernelKit/ProcessScheduler.hxx>
+#include <KernelKit/UserProcessScheduler.hxx>
#include <KernelKit/Timer.hxx>
#include <Modules/CoreCG/TextRenderer.hxx>
@@ -45,11 +45,11 @@ namespace Kernel::HAL
EXTERN_C Void _hal_spin_core(Void);
STATIC struct MADT_TABLE* kMADTBlock = nullptr;
- STATIC Bool kSMPAware = false;
- STATIC Int64 kSMPCount = 0;
+ STATIC Bool kSMPAware = false;
+ STATIC Int64 kSMPCount = 0;
STATIC Int32 cSMPInterrupt = 0;
- STATIC UInt64 cSMPCores[cSMPMax] = {0};
+ STATIC UInt64 cSMPCores[cSMPMax] = {0};
STATIC VoidPtr kRawMADT = nullptr;
/// @brief Multiple APIC Descriptor Table.
@@ -143,51 +143,43 @@ namespace Kernel::HAL
ke_stop(RUNTIME_CHECK_FAILED);
}
- constexpr auto cMaxPCBBlocks = cMaxHWThreads;
-
struct PROCESS_CONTROL_BLOCK final
{
- PROCESS_HEADER_BLOCK* f_PHB;
- HAL::StackFramePtr f_Frame;
- } fBlocks[cMaxPCBBlocks] = {0};
+ UserProcessPtr f_Process;
+ HAL::StackFramePtr f_Frame;
+ } fBlocks[cMaxHWThreads] = {0};
EXTERN_C HAL::StackFramePtr _hal_leak_current_context(Void)
{
- return fBlocks[ProcessScheduler::The().CurrentProcess().Leak().ProcessId % cMaxPCBBlocks].f_Frame;
+ return fBlocks[UserProcessScheduler::The().CurrentProcess().Leak().ProcessId % cMaxHWThreads].f_Frame;
}
EXTERN_C Void hal_switch_context(HAL::StackFramePtr stack_frame)
{
- Semaphore semaphore_process;
-
- const auto cDurationSeconds = Seconds(5);
-
- HardwareTimer timer(cDurationSeconds);
- semaphore_process.LockOrWait(&ProcessScheduler::The().CurrentProcess().Leak(), &timer);
-
- fBlocks[ProcessScheduler::The().CurrentProcess().Leak().ProcessId % cMaxPCBBlocks].f_PHB = &ProcessScheduler::The().CurrentProcess().Leak();
- fBlocks[ProcessScheduler::The().CurrentProcess().Leak().ProcessId % cMaxPCBBlocks].f_Frame = stack_frame;
-
- semaphore_process.Unlock();
- ke_stop(RUNTIME_CHECK_FAILED);
+ if (kSMPAware)
+ {
+ fBlocks[UserProcessScheduler::The().CurrentProcess().Leak().ProcessId % cMaxHWThreads].f_Process = &UserProcessScheduler::The().CurrentProcess().Leak();
+ fBlocks[UserProcessScheduler::The().CurrentProcess().Leak().ProcessId % cMaxHWThreads].f_Frame = stack_frame;
+ }
}
/***********************************************************************************/
/// @brief Fetch and enable cores inside main CPU.
/// @param vendor_ptr RSD PTR structure.
/***********************************************************************************/
- Void hal_system_get_cores(voidPtr vendor_ptr)
+ Void mp_get_cores(VoidPtr vendor_ptr) noexcept
{
+ kSMPAware = false;
+
+ if (!vendor_ptr)
+ return;
+
auto hw_and_pow_int = PowerFactoryInterface(vendor_ptr);
kRawMADT = hw_and_pow_int.Find(kApicSignature).Leak().Leak();
kMADTBlock = reinterpret_cast<MADT_TABLE*>(kRawMADT);
- if (!kMADTBlock)
- {
- kSMPAware = false;
- }
- else
+ if (kMADTBlock)
{
SizeT index = 0;
@@ -227,7 +219,7 @@ namespace Kernel::HAL
kcout << "newoskrnl: # of cores: " << number(kSMPCount) << endl;
// Kernel is now SMP aware.
- // That means that the scheduler is now available (on MP kernels)
+ // That means that the scheduler is now available (on MP Kernels)
kSMPAware = true;
diff --git a/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm b/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm
index 5ce9cabc..fb5e9b19 100644
--- a/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm
+++ b/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm
@@ -142,7 +142,7 @@ IntNormal 48
IntNormal 49
[extern hal_system_call_enter]
-[extern hal_kernel_call_enter]
+[extern hal_Kernel_call_enter]
__NEW_INT_50:
cli
@@ -169,7 +169,7 @@ __NEW_INT_51:
push r9
push rax
- call hal_kernel_call_enter
+ call hal_Kernel_call_enter
pop rax
pop r9
diff --git a/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx b/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx
index 4d0cfdb6..e2331ba6 100644
--- a/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx
@@ -11,7 +11,7 @@
#include <KernelKit/Framebuffer.hxx>
#include <KernelKit/Heap.hxx>
#include <KernelKit/PEFCodeManager.hxx>
-#include <KernelKit/ProcessScheduler.hxx>
+#include <KernelKit/UserProcessScheduler.hxx>
#include <NewKit/Json.hxx>
#include <Modules/CoreCG/Accessibility.hxx>
#include <KernelKit/CodeManager.hxx>
@@ -52,14 +52,14 @@ namespace Kernel::HAL
{
/// @brief Gets the system cores using the MADT.
/// @param rsdPtr The 'RSD PTR' data structure.
- EXTERN void hal_system_get_cores(Kernel::voidPtr rsdPtr);
+ EXTERN void mp_get_cores(Kernel::voidPtr rsdPtr);
} // namespace Kernel::HAL
/* GDT. */
STATIC Kernel::HAL::Detail::NewOSGDT cGdt = {
{0, 0, 0, 0x00, 0x00, 0}, // null entry
- {0, 0, 0, 0x9a, 0xaf, 0}, // kernel code
- {0, 0, 0, 0x92, 0xaf, 0}, // kernel data
+ {0, 0, 0, 0x9a, 0xaf, 0}, // Kernel code
+ {0, 0, 0, 0x92, 0xaf, 0}, // Kernel data
{0, 0, 0, 0x00, 0x00, 0}, // null entry
{0, 0, 0, 0x9a, 0xaf, 0}, // user code
{0, 0, 0, 0x92, 0xaf, 0}, // user data
@@ -143,7 +143,7 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
kSyscalls[cTlsInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
if (tls_check_syscall_impl(rdx) == false)
{
- Kernel::ProcessScheduler::The().CurrentProcess().Leak().Crash();
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Crash();
}
};
@@ -155,7 +155,7 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
return;
// assign the fThe field with the pointer.
- rdxInf->fThe = Kernel::ProcessScheduler::The().CurrentProcess().Leak().New(rdxInf->fTheSz);
+ rdxInf->fThe = Kernel::UserProcessScheduler::The().CurrentProcess().Leak().New(rdxInf->fTheSz);
};
kSyscalls[cDeleteInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
@@ -166,7 +166,7 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
return;
// delete ptr with sz in mind.
- Kernel::ProcessScheduler::The().CurrentProcess().Leak().Delete(rdxInf->fThe, rdxInf->fTheSz);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Delete(rdxInf->fThe, rdxInf->fTheSz);
};
kSyscalls[cTlsInstallInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
@@ -186,7 +186,7 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
return;
Kernel::kcout << "newoskrnl: " << rdxEi->fReason << "\r";
- Kernel::ProcessScheduler::The().CurrentProcess().Leak().Exit(rdxEi->fCode);
+ Kernel::UserProcessScheduler::The().CurrentProcess().Leak().Exit(rdxEi->fCode);
};
kSyscalls[cLastExitInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
@@ -218,7 +218,7 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
kSyscalls[cRebootInterrupt].fHooked = true;
if (kHandoverHeader->f_MultiProcessingEnabled)
- Kernel::HAL::hal_system_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
+ Kernel::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
Kernel::kcout << "newoskrnl: Creating filesystem and such.\r";
diff --git a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm
index 10114289..6b3ed33a 100644
--- a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm
+++ b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm
@@ -9,7 +9,7 @@
[bits 64]
-[global rt_get_current_context]
+[global mp_get_current_context]
[global mp_do_context_switch]
[global _hal_spin_core]
[extern _hal_switch_context]
@@ -21,49 +21,13 @@ section .text
;; rcx: Stack Pointer
;; rdx: SMP core address.
mp_do_context_switch:
- ;; Take care of context switching within AP.
-
- mov r9, rcx
-
- mov rbp, [r9 + (8 * 5)]
- mov rsp, [r9 + (8 * 6)]
-
- mov gs, [r9 + (8 * 19)]
- mov fs, [r9 + (8 * 20)]
-
- mov rcx, [r9 + (8 * 3)]
- mov rdx, [r9 + (8 * 4)]
- mov rbx, [r9 + (8 * 7)]
- mov rax, [r9 + (8 * 8)]
- movq xmm0, [r9 + (8 * 9)]
- movq xmm1, [r9 + (8 * 10)]
-
- mov r8, [r9 + (8 * 11)]
- mov r10, [r9 + (8 * 13)]
- mov r11, [r9 + (8 * 14)]
- mov r12, [r9 + (8 * 15)]
- mov r13, [r9 + (8 * 16)]
- mov r14, [r9 + (8 * 17)]
- mov r15, [r9 + (8 * 18)]
-
- fldcw word [r9 + (8 * 21)]
-
- mov r9, [r9 + (8 * 12)]
-
+ jmp $
ret
-;; gets the current stack frame.
-rt_get_current_context:
- push rax
-
+;; @brief Gets the current stack frame.
+mp_get_current_context:
call _hal_leak_current_context
-
- mov rax, r9
- pop rax
-
- mov r9, rax
-
- retfq
+ ret
_hal_spin_core:
jmp $
diff --git a/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx b/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx
index f389dd80..cad215b2 100644
--- a/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalPageAlloc.cxx
@@ -6,8 +6,8 @@
#include <ArchKit/ArchKit.hxx>
-#define cVMHMagic (0xDEEFD00D)
-#define cPaddingVMH (512)
+#define cVMHMagic (0xDEEFD00D)
+#define cPaddingVMH (16)
#ifdef __ZKA_AMD64__
#include <HALKit/AMD64/HalPageAlloc.hxx>
@@ -43,7 +43,7 @@ namespace Kernel
VIRTUAL_MEMORY_HEADER* Next(VIRTUAL_MEMORY_HEADER* current)
{
if (current->Magic != cVMHMagic)
- current->Size = cPaddingVMH;
+ return current;
return current + sizeof(VIRTUAL_MEMORY_HEADER) + current->Size;
}
@@ -54,7 +54,7 @@ namespace Kernel
VIRTUAL_MEMORY_HEADER* Prev(VIRTUAL_MEMORY_HEADER* current)
{
if (current->Magic != cVMHMagic)
- current->Size = cPaddingVMH;
+ return current;
return current - sizeof(VIRTUAL_MEMORY_HEADER) - current->Size;
}
@@ -84,16 +84,16 @@ namespace Kernel
if (vmh_header == reinterpret_cast<VoidPtr>(kBadPtr))
{
- ke_stop(RUNTIME_CHECK_POINTER);
- return nullptr;
+ ke_stop(RUNTIME_CHECK_POINTER);
+ return nullptr;
}
}
- vmh_header->Magic = cVMHMagic;
- vmh_header->Present = true;
+ vmh_header->Magic = cVMHMagic;
+ vmh_header->Present = true;
vmh_header->ReadWrite = rw;
- vmh_header->User = user;
- vmh_header->Size = size;
+ vmh_header->User = user;
+ vmh_header->Size = size;
kAllocationInProgress = false;
@@ -106,12 +106,12 @@ namespace Kernel
/// @return
auto hal_alloc_page(Boolean rw, Boolean user, SizeT size) -> VoidPtr
{
- kcout << "Waiting now...";
+ kcout << "PageAlloc: Waiting now...";
// Wait for a ongoing allocation to complete.
while (kAllocationInProgress)
{
- (void)0;
+ (Void)0;
}
kcout << ", done waiting, allocating...\r";
@@ -119,7 +119,7 @@ namespace Kernel
if (size == 0)
++size;
- // allocate new page.
+ // Now allocate the page.
return hal_try_alloc_new_page(rw, user, size);
}
} // namespace HAL
diff --git a/dev/ZKA/HALKit/AMD64/HalSchedulerCore.cxx b/dev/ZKA/HALKit/AMD64/HalSchedulerCore.cxx
index 072c09be..cb849150 100644
--- a/dev/ZKA/HALKit/AMD64/HalSchedulerCore.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalSchedulerCore.cxx
@@ -4,11 +4,11 @@
------------------------------------------- */
-#include <KernelKit/ProcessScheduler.hxx>
+#include <KernelKit/UserProcessScheduler.hxx>
using namespace Kernel;
-Void PROCESS_HEADER_BLOCK::SetEntrypoint(UIntPtr& imageStart) noexcept
+Void UserProcess::SetEntrypoint(UIntPtr& imageStart) noexcept
{
if (imageStart == 0)
this->Crash();
@@ -19,11 +19,12 @@ Void PROCESS_HEADER_BLOCK::SetEntrypoint(UIntPtr& imageStart) noexcept
namespace Kernel
{
- bool hal_check_stack(HAL::StackFramePtr stackPtr)
+ bool hal_check_stack(HAL::StackFramePtr stack_ptr)
{
- if (!stackPtr)
+ if (!stack_ptr)
return false;
- if (stackPtr->BP == 0 || stackPtr->SP == 0)
+
+ if (stack_ptr->BP == 0 || stack_ptr->SP == 0)
return false;
return true;
diff --git a/dev/ZKA/HALKit/AMD64/HalTimer.cxx b/dev/ZKA/HALKit/AMD64/HalTimer.cxx
index 4bc19801..48e62ee9 100644
--- a/dev/ZKA/HALKit/AMD64/HalTimer.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalTimer.cxx
@@ -75,8 +75,7 @@ Int32 HardwareTimer::Wait() noexcept
}
UInt64 ticks = fWaitFor / ((*(fDigitalTimer) >> 32) & __UINT32_MAX__);
-
- auto prev = *(fDigitalTimer + cHPETCounterRegValue);
+ UInt64 prev = *(fDigitalTimer + cHPETCounterRegValue);
prev += ticks;
diff --git a/dev/ZKA/HALKit/AMD64/HalUtils.asm b/dev/ZKA/HALKit/AMD64/HalUtils.asm
index 0e4caf2b..91bf216e 100644
--- a/dev/ZKA/HALKit/AMD64/HalUtils.asm
+++ b/dev/ZKA/HALKit/AMD64/HalUtils.asm
@@ -18,7 +18,7 @@ section .text
rt_install_tib:
mov rcx, gs ;; TIB -> Thread Information Block
- mov rdx, fs ;; PIB -> Process Information Block
+ mov rdx, fs ;; PIB -> UserProcess Information Block
ret
;; //////////////////////////////////////////////////// ;;
diff --git a/dev/ZKA/HALKit/AMD64/Processor.hxx b/dev/ZKA/HALKit/AMD64/Processor.hxx
index 2a42a105..b307e0f2 100644
--- a/dev/ZKA/HALKit/AMD64/Processor.hxx
+++ b/dev/ZKA/HALKit/AMD64/Processor.hxx
@@ -129,8 +129,7 @@ namespace Kernel::HAL
using RawRegister = UInt64;
- using InterruptId = UShort; /* For each element in the IVT */
- using InterruptTrapKind = UIntPtr(UIntPtr sp);
+ using InterruptId = UInt16; /* For each element in the IVT */
typedef UIntPtr Reg;
@@ -141,7 +140,6 @@ namespace Kernel::HAL
Reg A0, A2, BP, SP, A3, A4, A5, A6;
Reg R8, R9, R10, R11, R12, R13, R14, R15;
Reg Gs, Fs;
- Reg ControlWord;
};
typedef StackFrame* StackFramePtr;
@@ -211,7 +209,7 @@ namespace Kernel::HAL
static Void Load(Ref<Register64>& idt);
};
- Void hal_system_get_cores(VoidPtr rsdPtr);
+ Void mp_get_cores(VoidPtr rsdPtr) noexcept;
Void hal_send_start_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress);
Void hal_send_end_ipi(UInt32 apicId, UInt8 vector, UInt32 targetAddress);