summaryrefslogtreecommitdiffhomepage
path: root/Private/HALKit
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-26 12:16:25 +0100
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-26 12:16:25 +0100
commitbdcc3d84e08a509a766a469a605a97419ec14c35 (patch)
tree1b3a365581b37a7f0246629be6256f521daeae4e /Private/HALKit
parent91c88797f7fa9dbb6cce12c14928a6fbd97d51b6 (diff)
Kernel/Bootloader: add CompilerKit/Version.hxx
- Rework BTextWriter class. - Add BVersionString class. - Worked on interrupts almost working! Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Private/HALKit')
-rw-r--r--Private/HALKit/AMD64/HalCoreInterruptHandler.cpp46
-rw-r--r--Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp9
-rw-r--r--Private/HALKit/AMD64/HalDebugOutput.cxx6
-rw-r--r--Private/HALKit/AMD64/HalInterruptRouting.asm28
-rw-r--r--Private/HALKit/AMD64/HalKernelMain.cxx19
-rw-r--r--Private/HALKit/AMD64/HalPlatformAMD64.cpp37
6 files changed, 90 insertions, 55 deletions
diff --git a/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp b/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp
index 1e46e5af..b161f1db 100644
--- a/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp
+++ b/Private/HALKit/AMD64/HalCoreInterruptHandler.cpp
@@ -11,7 +11,42 @@
#include <KernelKit/ProcessManager.hpp>
#include <NewKit/String.hpp>
-extern "C" HCore::UIntPtr rt_handle_interrupts(HCore::UIntPtr &rsp) {
+static const char* kExceptionMessage[32] = {
+ "Division by zero",
+ "Debug Breakpoint",
+ "Non-maskable interrupt",
+ "Breakpoint",
+ "Detected overflow",
+ "Out-of-bounds",
+ "Invalid opcode",
+ "No coprocessor",
+ "Double fault",
+ "Coprocessor segment overrun",
+ "Bad TSS",
+ "Segment not found",
+ "Stack error.",
+ "General Protection Fault",
+ "Page Fault",
+ "Invalid interrupt",
+ "Coprocessor fault",
+ "Alignment check",
+ "Machine check",
+ "Reserved",
+ "Reserved",
+ "System Process Switch Issued",
+ "System was interrupted by kernel",
+ "System hang by kernel",
+ "Reserved",
+ "Reserved",
+ "Reserved",
+ "Reserved",
+ "Reserved",
+ "Reserved",
+ "Reserved",
+ "Reserved",
+};
+
+extern "C" HCore::UIntPtr rt_handle_interrupts(HCore::UIntPtr& rsp) {
HCore::HAL::rt_cli();
HCore::HAL::StackFramePtr sf = (HCore::HAL::StackFramePtr)rsp;
@@ -20,8 +55,13 @@ extern "C" HCore::UIntPtr rt_handle_interrupts(HCore::UIntPtr &rsp) {
rt_syscall_handle(sf);
}
- HCore::HAL::Out8(0x20, 0x20);
- HCore::HAL::Out8(0xa0, 0x20);
+ if (sf->IntNum < 32) {
+ HCore::kcout << "Exception:" << kExceptionMessage[sf->IntNum] << "\n";
+ }
+
+ if (sf->IntNum >= 40) HCore::HAL::Out8(0x20, 0x20); // ACK MASTER
+
+ HCore::HAL::Out8(0xA0, 0x20); // ACK SLAVE
HCore::HAL::rt_sti();
diff --git a/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp b/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp
index 98d36f86..f1001410 100644
--- a/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp
+++ b/Private/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cpp
@@ -11,6 +11,8 @@
#include <KernelKit/ProcessManager.hpp>
#include <NewKit/String.hpp>
+#include "KernelKit/DebugOutput.hpp"
+
extern "C" void idt_handle_system_call(HCore::UIntPtr rsp) {
HCore::HAL::StackFrame *sf = reinterpret_cast<HCore::HAL::StackFrame *>(rsp);
rt_syscall_handle(sf);
@@ -35,12 +37,13 @@ extern "C" void idt_handle_scheduler(HCore::UIntPtr rsp) {
HCore::kcout << HCore::StringBuilder::FromInt("rsp{%}", rsp);
HCore::kcout
- << "Will be scheduled back later "
- << HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName();
+ << "HCoreKrnl: Will be scheduled back later "
+ << HCore::ProcessManager::Shared().Leak().GetCurrent().Leak().GetName()
+ << HCore::EndLine();
/// schedule another process.
if (!HCore::ProcessHelper::StartScheduling()) {
- HCore::kcout << "Let's continue schedule this process...\r\n";
+ HCore::kcout << "HCoreKrnl: Continue schedule this process...\r\n";
}
}
diff --git a/Private/HALKit/AMD64/HalDebugOutput.cxx b/Private/HALKit/AMD64/HalDebugOutput.cxx
index f4d81338..37beb912 100644
--- a/Private/HALKit/AMD64/HalDebugOutput.cxx
+++ b/Private/HALKit/AMD64/HalDebugOutput.cxx
@@ -53,7 +53,7 @@ bool serial_init() {
}
} // namespace Detail
-void ke_io_print(const char *bytes) {
+void ke_io_print(const char* bytes) {
Detail::serial_init();
if (!bytes || Detail::kState != kStateReady) return;
@@ -72,8 +72,8 @@ void ke_io_print(const char *bytes) {
Detail::kState = kStateReady;
}
-TerminalDevice TerminalDevice::Shared() noexcept {
- TerminalDevice out(HCore::ke_io_print, nullptr);
+TerminalDevice& TerminalDevice::Shared() noexcept {
+ static TerminalDevice out(HCore::ke_io_print, nullptr);
return out;
}
diff --git a/Private/HALKit/AMD64/HalInterruptRouting.asm b/Private/HALKit/AMD64/HalInterruptRouting.asm
index f50fb752..a1d532cb 100644
--- a/Private/HALKit/AMD64/HalInterruptRouting.asm
+++ b/Private/HALKit/AMD64/HalInterruptRouting.asm
@@ -17,17 +17,18 @@
%macro IntExp 1
HCoreInterrupt%1:
- cli
+ push 1
push %1
jmp ke_handle_irq
+ iretq
%endmacro
%macro IntNormal 1
HCoreInterrupt%1:
- cli
push 0
push %1
jmp ke_handle_irq
+ iretq
%endmacro
; This file handles the core interrupt table
@@ -56,7 +57,7 @@ ke_handle_irq:
push r14
push r15
- mov rdi, rsp
+ mov rcx, rsp
call rt_handle_interrupts
add rsp, 8
@@ -76,10 +77,10 @@ ke_handle_irq:
pop rbx
pop rax
- iretq
-
-section .data
+ sti
+ retf
+__IVT:
IntNormal 0
IntNormal 1
IntNormal 2
@@ -112,6 +113,13 @@ section .data
IntNormal 29
IntExp 30
IntNormal 31
+ IntNormal 32
+
+ %assign i 33
+ %rep 223
+ IntNormal i
+ %assign i i+1
+ %endrep
section .data
@@ -121,11 +129,3 @@ __EXEC_IVT:
IntDecl i
%assign i i+1
%endrep
-
- %assign i 32
- %rep 224
- IntNormal i
- %assign i i+1
- %endrep
-
- ret
diff --git a/Private/HALKit/AMD64/HalKernelMain.cxx b/Private/HALKit/AMD64/HalKernelMain.cxx
index b581f6de..9cced576 100644
--- a/Private/HALKit/AMD64/HalKernelMain.cxx
+++ b/Private/HALKit/AMD64/HalKernelMain.cxx
@@ -20,17 +20,21 @@
#include <NewKit/KernelHeap.hpp>
#include <NewKit/UserHeap.hpp>
-extern "C" HCore::UIntPtr __EXEC_IVT;
+extern "C" HCore::VoidPtr __EXEC_IVT;
namespace Detail {
using namespace HCore;
+/// @brief kernel POST.
Void PowerOnSelfTest() {
kcout << "POST: Starting PowerOn-Self Test...\r\n";
- asm("int $0x21");
- kcout << "POST: Done\r\n";
+ asm("int $0x21"); // dummy 21h interrupt.
+ kcout << "POST: Successfuly Done!\r\n";
}
+/**
+ @brief Global descriptor table entry, either null, code or data.
+*/
struct PACKED HC_GDT_ENTRY final {
UInt16 Limit0;
UInt16 Base0;
@@ -79,17 +83,15 @@ EXTERN_C void RuntimeMain(
HCore::HAL::GDTLoader gdt;
gdt.Load(gdtBase);
- HCore::VoidPtr IDT = new HCore::VoidPtr;
-
HCore::HAL::Register64 idtBase;
- idtBase.Base = (HCore::UIntPtr)IDT;
+ idtBase.Base = (HCore::UIntPtr)__EXEC_IVT;
idtBase.Limit = 0x0FFF;
HCore::HAL::IDTLoader idt;
idt.Load(idtBase);
if (HandoverHeader->f_Bootloader == 0xDD) {
- /// Mount a New partition.
+ /// Mounts a NewFS partition.
HCore::IFilesystemManager::Mount(new HCore::NewFilesystemManager());
// Open file from first hard-drive.
@@ -111,9 +113,6 @@ EXTERN_C void RuntimeMain(
ResourceClear();
- DrawResource(PoweredByAward, HandoverHeader, POWEREDBYAWARD_HEIGHT,
- POWEREDBYAWARD_WIDTH, POWEREDBYAWARD_WIDTH + 20, 10);
-
Detail::PowerOnSelfTest();
/**
diff --git a/Private/HALKit/AMD64/HalPlatformAMD64.cpp b/Private/HALKit/AMD64/HalPlatformAMD64.cpp
index b2e98004..80721aed 100644
--- a/Private/HALKit/AMD64/HalPlatformAMD64.cpp
+++ b/Private/HALKit/AMD64/HalPlatformAMD64.cpp
@@ -32,47 +32,40 @@ struct InterruptDescriptorAMD64 final {
};
} // namespace Detail::AMD64
-#define kInterruptGate 0xE
-#define kTrapGate 0xF
-#define kRing0 (0 << 5)
-#define kRing3 (3 << 5)
-#define kIdtPresent (1 << 7)
+#define kInterruptGate 0x8E
+#define kTrapGate 0x8F
+#define kTaskGate 0x85
#define kGdtSelector 0x08
extern "C" HCore::UIntPtr rt_handle_interrupts(HCore::UIntPtr &rsp);
static ALIGN(0x10)
Detail::AMD64::InterruptDescriptorAMD64 kIdtRegs[kKernelMaxSystemCalls];
+
static HAL::Register64 kRegIdt;
void IDTLoader::Load(Register64 &idt) {
+ VoidPtr *baseIdt = (VoidPtr *)idt.Base;
+
for (auto i = 0; i < 32; i++) {
kIdtRegs[i].Selector = kGdtSelector;
- kIdtRegs[i].Ist = 0x00;
- kIdtRegs[i].TypeAttributes =
- kInterruptGate | kTrapGate | kRing0 | kIdtPresent;
- kIdtRegs[i].OffsetLow = (((UIntPtr *)idt.Base)[i]) & 0xFFFF;
- kIdtRegs[i].OffsetMid = (((UIntPtr *)idt.Base)[i] >> 16) & 0xFFFF;
- kIdtRegs[i].OffsetHigh = (((UIntPtr *)idt.Base)[i] >> 32) & 0xFFFF;
+ kIdtRegs[i].Ist = 001;
+ kIdtRegs[i].TypeAttributes = kTrapGate;
+ kIdtRegs[i].OffsetLow = (UIntPtr)baseIdt & 0xFFFF;
+ kIdtRegs[i].OffsetMid = (UIntPtr)baseIdt >> 16 & 0xFFFF;
+ kIdtRegs[i].OffsetHigh = (UIntPtr)baseIdt >> 32 & 0xFFFFFFFF;
kIdtRegs[i].Zero = 0;
}
- kIdtRegs[0x21].Selector = kGdtSelector;
- kIdtRegs[0x21].Ist = 0x00;
- kIdtRegs[0x21].TypeAttributes = kInterruptGate | kRing3 | kIdtPresent;
- kIdtRegs[0x21].OffsetLow = (((UIntPtr *)idt.Base)[0x21]) & 0xFFFF;
- kIdtRegs[0x21].OffsetMid = (((UIntPtr *)idt.Base)[0x21] >> 16) & 0xFFFF;
- kIdtRegs[0x21].OffsetHigh = (((UIntPtr *)idt.Base)[0x21] >> 32) & 0xFFFF;
- kIdtRegs[0x21].Zero = 0;
-
- kRegIdt.Base = (UIntPtr)kIdtRegs;
- kRegIdt.Limit = sizeof(Detail::AMD64::InterruptDescriptorAMD64) * idt.Limit;
+ kRegIdt.Base = (UIntPtr)&kIdtRegs[0];
+ kRegIdt.Limit =
+ sizeof(Detail::AMD64::InterruptDescriptorAMD64) * idt.Limit - 1;
kcout << "HCoreKrnl: Installing Interrupt vector...\n";
rt_load_idt(kRegIdt);
- asm volatile("sti");
+ rt_sti();
kcout << "HCoreKrnl: Interrupt Vector installed.\n";
}