summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/HALKit/AMD64
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-12-31 16:50:34 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-12-31 16:50:34 +0100
commitad677bd1ed2c6d1a052d426117a4d20a0b59ac52 (patch)
tree67898ad7cb935389619d8e9272cac9a732e24c92 /dev/Kernel/HALKit/AMD64
parent510c659355d9227d1b75edfe50c1b8691ea2f982 (diff)
FIX: CUSA and improved algorithm.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/HALKit/AMD64')
-rw-r--r--dev/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc71
-rw-r--r--dev/Kernel/HALKit/AMD64/HalDescriptorLoader.cc6
-rw-r--r--dev/Kernel/HALKit/AMD64/HalInterruptAPI.asm95
-rw-r--r--dev/Kernel/HALKit/AMD64/HalKernelMain.cc13
-rw-r--r--dev/Kernel/HALKit/AMD64/HalUtils.asm1
5 files changed, 129 insertions, 57 deletions
diff --git a/dev/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc b/dev/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc
index 771a1e33..32be8dee 100644
--- a/dev/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc
+++ b/dev/Kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc
@@ -9,24 +9,31 @@
#include <NewKit/KString.h>
#include <POSIXKit/signal.h>
+STATIC BOOL kIsScheduling = NO;
+
/// @brief Handle GPF fault.
/// @param rsp
EXTERN_C void idt_handle_gpf(Kernel::UIntPtr rsp)
{
- kcout << "Kernel: GPF.\r";
-
auto process = Kernel::UserProcessScheduler::The().GetCurrentProcess();
if (!process)
Kernel::ke_panic(RUNTIME_CHECK_PAGE);
+ if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning)
+ return;
+
+ kIsScheduling = NO;
+
+ kcout << "Kernel: GPF.\r";
+
process.Leak().ProcessSignal.SignalIP = 0UL;
process.Leak().ProcessSignal.SignalID = SIGKILL;
process.Leak().ProcessSignal.PreviousStatus = process.Leak().Status;
kcout << "Kernel: PRCFROZE status set..\r";
- process.Leak().Status = Kernel::ProcessStatusKind::kFrozen;
+ process.Leak().Status = Kernel::ProcessStatusKind::kKilled;
process.Leak().Crash();
}
@@ -35,21 +42,26 @@ EXTERN_C void idt_handle_gpf(Kernel::UIntPtr rsp)
/// @param rsp
EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp)
{
- kcout << "Kernel: Page Fault.\r";
- kcout << "Kernel: SIGKILL set.\r";
-
auto process = Kernel::UserProcessScheduler::The().GetCurrentProcess();
if (!process)
Kernel::ke_panic(RUNTIME_CHECK_PAGE);
+ if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning)
+ return;
+
+ kIsScheduling = NO;
+
+ kcout << "Kernel: Page Fault.\r";
+ kcout << "Kernel: SIGKILL set.\r";
+
process.Leak().ProcessSignal.SignalIP = 0UL;
process.Leak().ProcessSignal.SignalID = SIGKILL;
process.Leak().ProcessSignal.PreviousStatus = process.Leak().Status;
kcout << "Kernel: PRCFROZE status set..\r";
- process.Leak().Status = Kernel::ProcessStatusKind::kFrozen;
+ process.Leak().Status = Kernel::ProcessStatusKind::kKilled;
process.Leak().Crash();
@@ -59,10 +71,9 @@ EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp)
/// @brief Handle scheduler interrupt.
EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp)
{
- static BOOL is_scheduling = NO;
static Kernel::Int64 try_count_before_brute = 100000UL;
- while (is_scheduling)
+ while (kIsScheduling)
{
--try_count_before_brute;
@@ -71,32 +82,37 @@ EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp)
}
try_count_before_brute = 100000UL;
- is_scheduling = YES;
+ kIsScheduling = YES;
kcout << "Kernel: Timer IRQ (Scheduler Notification).\r";
Kernel::UserProcessHelper::StartScheduling();
- is_scheduling = NO;
+ kIsScheduling = NO;
}
/// @brief Handle math fault.
/// @param rsp
EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp)
{
- kcout << "Kernel: Math error (division by zero?).\r";
-
auto process = Kernel::UserProcessScheduler::The().GetCurrentProcess();
if (!process)
Kernel::ke_panic(RUNTIME_CHECK_PAGE);
+ if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning)
+ return;
+
+ kIsScheduling = NO;
+
+ kcout << "Kernel: Math error (division by zero?).\r";
+
process.Leak().ProcessSignal.SignalIP = 0UL;
process.Leak().ProcessSignal.SignalID = SIGKILL;
process.Leak().ProcessSignal.PreviousStatus = process.Leak().Status;
kcout << "Kernel: PRCFROZE status set..\r";
- process.Leak().Status = Kernel::ProcessStatusKind::kFrozen;
+ process.Leak().Status = Kernel::ProcessStatusKind::kKilled;
process.Leak().Crash();
@@ -107,20 +123,25 @@ EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp)
/// @param rsp
EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp)
{
- kcout << "Kernel: Generic Process Fault.\r";
-
auto process = Kernel::UserProcessScheduler::The().GetCurrentProcess();
if (!process)
Kernel::ke_panic(RUNTIME_CHECK_PAGE);
+ if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning)
+ return;
+
+ kIsScheduling = NO;
+
+ kcout << "Kernel: Generic Process Fault.\r";
+
process.Leak().ProcessSignal.SignalIP = 0UL;
process.Leak().ProcessSignal.SignalID = SIGKILL;
process.Leak().ProcessSignal.PreviousStatus = process.Leak().Status;
kcout << "Kernel: PRCFROZE status set..\r";
- process.Leak().Status = Kernel::ProcessStatusKind::kFrozen;
+ process.Leak().Status = Kernel::ProcessStatusKind::kKilled;
process.Leak().Crash();
@@ -134,6 +155,11 @@ EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip)
if (!process)
Kernel::ke_panic(RUNTIME_CHECK_PAGE);
+ if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning)
+ return;
+
+ kIsScheduling = NO;
+
kcout << "Kernel: Process RIP: " << Kernel::hex_number(rip) << endl;
kcout << "Kernel: SIGTRAP set.\r";
@@ -151,20 +177,25 @@ EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip)
/// @param rsp
EXTERN_C void idt_handle_ud(Kernel::UIntPtr rsp)
{
- kcout << "Kernel: Undefined Opcode.\r";
-
auto process = Kernel::UserProcessScheduler::The().GetCurrentProcess();
if (!process)
Kernel::ke_panic(RUNTIME_CHECK_PAGE);
+ if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning)
+ return;
+
+ kIsScheduling = NO;
+
+ kcout << "Kernel: Undefined Opcode.\r";
+
process.Leak().ProcessSignal.SignalIP = 0UL;
process.Leak().ProcessSignal.SignalID = SIGKILL;
process.Leak().ProcessSignal.PreviousStatus = process.Leak().Status;
kcout << "Kernel: PRCFROZE status set..\r";
- process.Leak().Status = Kernel::ProcessStatusKind::kFrozen;
+ process.Leak().Status = Kernel::ProcessStatusKind::kKilled;
process.Leak().Crash();
diff --git a/dev/Kernel/HALKit/AMD64/HalDescriptorLoader.cc b/dev/Kernel/HALKit/AMD64/HalDescriptorLoader.cc
index 8f20e8ae..ed9bf15b 100644
--- a/dev/Kernel/HALKit/AMD64/HalDescriptorLoader.cc
+++ b/dev/Kernel/HALKit/AMD64/HalDescriptorLoader.cc
@@ -26,11 +26,11 @@ namespace Kernel::HAL
// Configure PIT to receieve scheduler interrupts.
- UInt16 cCommonDivisor = kPITFrequency / ticks; // 100 Hz.
+ UInt16 cCommDivisor = kPITFrequency / ticks; // 100 Hz.
HAL::rt_out8(kPITControlPort, 0x36); // Command to PIT
- HAL::rt_out8(kPITChannel0Port, cCommonDivisor & 0xFF); // Send low byte
- HAL::rt_out8(kPITChannel0Port, (cCommonDivisor >> 8) & 0xFF); // Send high byte
+ HAL::rt_out8(kPITChannel0Port, cCommDivisor & 0xFF); // Send low byte
+ HAL::rt_out8(kPITChannel0Port, (cCommDivisor >> 8) & 0xFF); // Send high byte
hal_clear_irq_mask(32);
}
diff --git a/dev/Kernel/HALKit/AMD64/HalInterruptAPI.asm b/dev/Kernel/HALKit/AMD64/HalInterruptAPI.asm
index 11deca52..fbc7cda8 100644
--- a/dev/Kernel/HALKit/AMD64/HalInterruptAPI.asm
+++ b/dev/Kernel/HALKit/AMD64/HalInterruptAPI.asm
@@ -16,26 +16,32 @@
%macro IntExp 1
global __ZKA_INT_%1
__ZKA_INT_%1:
- cli
+ cld
mov al, 0x20
out 0xA0, al
out 0x20, al
- sti
+ push rcx
+ call idt_handle_generic
+ pop rcx
+
+
+
+ std
o64 iret
%endmacro
%macro IntNormal 1
global __ZKA_INT_%1
__ZKA_INT_%1:
- cli
+ cld
mov al, 0x20
out 0xA0, al
out 0x20, al
- sti
+ std
o64 iret
%endmacro
@@ -55,25 +61,31 @@ extern idt_handle_breakpoint
section .text
__ZKA_INT_0:
- cli
+ cld
mov al, 0x20
out 0x20, al
- sti
+ std
o64 iret
__ZKA_INT_1:
- cli
+ cld
mov al, 0x20
out 0x20, al
- sti
+ push rcx
+ call idt_handle_generic
+ pop rcx
+
+
+
+ std
o64 iret
__ZKA_INT_2:
- cli
+ cld
mov al, 0x20
out 0x20, al
@@ -82,12 +94,14 @@ __ZKA_INT_2:
call idt_handle_generic
pop rcx
- sti
+
+
+ std
o64 iret
;; @brief Triggers a breakpoint and freeze the process. RIP is also fetched.
__ZKA_INT_3:
- cli
+ cld
mov al, 0x20
out 0x20, al
@@ -95,11 +109,14 @@ __ZKA_INT_3:
push rcx
call idt_handle_generic
pop rcx
- sti
+
+
+
+ std
o64 iret
__ZKA_INT_4:
- cli
+ cld
mov al, 0x20
out 0x20, al
@@ -108,21 +125,27 @@ __ZKA_INT_4:
push rcx
call idt_handle_generic
pop rcx
- sti
+
+
+
+
+ std
o64 iret
__ZKA_INT_5:
- cli
+ cld
mov al, 0x20
out 0x20, al
- sti
+
+
+ std
o64 iret
;; Invalid opcode interrupt
__ZKA_INT_6:
- cli
+ cld
mov al, 0x20
out 0x20, al
@@ -131,11 +154,13 @@ __ZKA_INT_6:
call idt_handle_generic
pop rcx
- sti
+
+
+ std
o64 iret
__ZKA_INT_7:
- cli
+ cld
mov al, 0x20
out 0x20, al
@@ -144,12 +169,14 @@ __ZKA_INT_7:
call idt_handle_generic
pop rcx
- sti
+
+
+ std
o64 iret
;; Invalid opcode interrupt
__ZKA_INT_8:
- cli
+ cld
mov al, 0x20
out 0xA0, al
@@ -159,7 +186,7 @@ __ZKA_INT_8:
call idt_handle_generic
pop rcx
- sti
+ std
o64 iret
IntNormal 9
@@ -169,7 +196,7 @@ IntExp 11
IntExp 12
__ZKA_INT_13:
- cli
+ cld
mov al, 0x20
out 0xA0, al
@@ -179,11 +206,13 @@ __ZKA_INT_13:
call idt_handle_gpf
pop rcx
- sti
+ add qword [rsp + 4], 2
+
+ std
o64 iret
__ZKA_INT_14:
- cli
+ cld
mov al, 0x20
out 0xA0, al
@@ -193,7 +222,9 @@ __ZKA_INT_14:
call idt_handle_pf
pop rcx
- sti
+ add qword [rsp + 4], 2
+
+ std
o64 iret
IntNormal 15
@@ -219,7 +250,7 @@ IntNormal 31
[extern idt_handle_scheduler]
__ZKA_INT_32:
- cli
+ cld
mov al, 0x20
out 0xA0, al
@@ -230,7 +261,7 @@ __ZKA_INT_32:
call idt_handle_scheduler
pop rax
- sti
+ std
o64 iret
IntNormal 33
@@ -258,7 +289,7 @@ IntNormal 49
[extern hal_kernel_call_enter]
__ZKA_INT_50:
- cli
+ cld
mov al, 0x20
out 0xA0, al
@@ -273,12 +304,12 @@ __ZKA_INT_50:
call rax
pop rax
- sti
+ std
o64 iret
__ZKA_INT_51:
- cli
+ cld
mov al, 0x20
out 0xA0, al
@@ -293,7 +324,7 @@ __ZKA_INT_51:
call rax
pop rax
- sti
+ std
o64 iret
diff --git a/dev/Kernel/HALKit/AMD64/HalKernelMain.cc b/dev/Kernel/HALKit/AMD64/HalKernelMain.cc
index 5de6ddd9..25bb1de3 100644
--- a/dev/Kernel/HALKit/AMD64/HalKernelMain.cc
+++ b/dev/Kernel/HALKit/AMD64/HalKernelMain.cc
@@ -24,6 +24,14 @@ EXTERN_C Kernel::Void rtl_kernel_main(Kernel::SizeT argc, char** argv, char** en
STATIC Kernel::Void hal_init_cxx_ctors()
{
+ for (Kernel::SizeT i = 0U; i < Kernel::UserProcessScheduler::The().CurrentTeam().AsArray().Count(); ++i)
+ {
+ Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[i] = Kernel::UserThread();
+ Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[i].Status = Kernel::ProcessStatusKind::kKilled;
+ }
+
+ Kernel::UserProcessScheduler::The().CurrentTeam().mProcessCount = 0UL;
+
for (Kernel::SizeT index = 0UL; __CTOR_LIST__[index] != __DTOR_LIST__; ++index)
{
Kernel::rtl_ctor_kind constructor_cxx = (Kernel::rtl_ctor_kind)__CTOR_LIST__[index];
@@ -85,7 +93,10 @@ EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept
{
auto str_proc = Kernel::rt_alloc_string("System");
- Kernel::rtl_create_process(rtl_kernel_main, str_proc);
+ auto pid = Kernel::rtl_create_process(rtl_kernel_main, str_proc);
+
+ Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[pid].PTime = 0;
+ Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[pid].Status = Kernel::ProcessStatusKind::kRunning;
delete str_proc;
str_proc = nullptr;
diff --git a/dev/Kernel/HALKit/AMD64/HalUtils.asm b/dev/Kernel/HALKit/AMD64/HalUtils.asm
index e48f7311..bd515e22 100644
--- a/dev/Kernel/HALKit/AMD64/HalUtils.asm
+++ b/dev/Kernel/HALKit/AMD64/HalUtils.asm
@@ -23,5 +23,4 @@ rt_install_tib:
;; //////////////////////////////////////////////////// ;;
-[extern cBspDone]
[extern kApicMadtAddressesCount]