feat: Various improvements

This commit is contained in:
Daniel Hammer
2025-04-16 10:32:11 +02:00
parent 35cf3f7c80
commit fcd81be1c5
14 changed files with 135 additions and 98 deletions
+9 -36
View File
@@ -1,12 +1,12 @@
/*
* gdt.hpp
* Intel Global Descriptor Table
* Copyright (c) 2025 Daniel Hammer
*/
#include "GDT.hpp"
#include "../Terminal/Terminal.hpp"
// Limine loads a GDT of course, (CS = 0x28) but we will need to make a TSS someday... therefore we load our own now
namespace Hal {
using namespace Kt;
@@ -14,39 +14,14 @@ namespace Hal {
BasicGDT kernelGDT{};
void PrepareGDT() {
kout << "HardwareAbstraction: GDT at " << base::hex << (uint64_t)&kernelGDT << "\n";
kernelGDT = {
// Code segment offset 0x08
// Data segment offset 0x10
// Not sure if having LimitLow set to 0xFFFF for the Null segment is kosher
{0xFFFF, 0, 0, 0x00, 0x00, 0},
// Kernel code/data
{0xFFFF, 0, 0, 0x00, 0x00, 0},
{0xFFFF, 0, 0, 0x9A, 0xA0, 0},
{0xFFFF, 0, 0, 0x92, 0xA0, 0},
{0xFFFF, 0, 0, 0x9A, 0xA0, 0},
{0xFFFF, 0, 0, 0x92, 0xA0, 0},
// User code/data
{0xFFFF, 0, 0, 0x9A, 0xA0, 0},
{0xFFFF, 0, 0, 0x92, 0xA0, 0},
// One day this will point to our actual TSS
{
// Limit = sizeof(TSS) - 1
0,
// Base = &TSS
0,
0,
// Access byte = 0xFA
0xFA,
// Granularity = 0x00
0x00,
0x0
}
{0, 0, 0, 0xFA, 0x00, 0x0},
};
gdtPointer = GDTPointer{
@@ -60,11 +35,9 @@ namespace Hal {
extern "C" void ReloadSegments();
void BridgeLoadGDT() {
// Puts the GDT pointer structure into the GDTR
kout << "HardwareAbstraction: Setting GDTR" << Kt::newline;
LoadGDT(&gdtPointer);
kout << "HardwareAbstraction: Reloading segments" << Kt::newline;
ReloadSegments();
KernelLogStream(DEBUG, "HardwareAbstraction") << "Set new GDT (0x" << base::hex << (uint64_t)&kernelGDT << ")";
}
};
+6 -27
View File
@@ -1,5 +1,6 @@
/*
* gdt.hpp
* Intel Global Descriptor Table
* Copyright (c) 2025 Daniel Hammer
*/
@@ -8,50 +9,28 @@
using namespace std;
// __attribute__((packed)) is the GCC extensions way of telling the compiler to ensure that it doesn't mess with these structures or add packing bytes
// for optimization because that would easily result in a triple fault.
namespace Hal {
class GDTEntry {
public:
// Base/Limit are obsolete in Long mode because segmentation is no longer used
// Sadly we must still build and load the GDT during kernel init
uint16_t LimitLow;
uint16_t BaseLow;
uint8_t BaseMiddle;
// Determine which processor rings this segment can be used in
uint8_t AccessByte;
// Lower 4 bits are the higher 4 bits of limit
uint8_t GranularityByte;
uint8_t BaseHigh;
// 16 + 16 + 8 + 8 = 48 bits
uint8_t BaseHigh;
}__attribute__((packed));
struct BasicGDT {
// Conventionally the first entry of the GDT has all values zeroed out.
GDTEntry Null;
// Kernel code segment descriptor
GDTEntry KernelCode;
// Kernel data segment descriptor
GDTEntry KernelData;
// UM code segment descriptor
GDTEntry UserCode;
// UM data segment descriptor
GDTEntry Null;
GDTEntry KernelCode;
GDTEntry KernelData;
GDTEntry UserCode;
GDTEntry UserData;
// Task State Segment
GDTEntry TSS;
}__attribute__((packed));
// Simple structure that tells the CPU the size of the GDT, and it's address
struct GDTPointer {
uint16_t Size;
uint64_t GDTAddress;
+2 -6
View File
@@ -56,11 +56,7 @@ namespace Hal {
template<size_t i>
__attribute__((interrupt)) void ExceptionHandler(System::PanicFrame* frame)
{
// kcp::cstringstream stream;
// stream << "Caught " << base::hex << "0x" << i << " " << ExceptionStrings[i] << " in kernel";
frame->InterruptVector = i;
Panic(ExceptionStrings[i], frame);
}
@@ -119,10 +115,10 @@ namespace Hal {
SetHandler<0, 31>::run();
kout << "HardwareAbstraction: Created exception interrupt vectors" << "\n";
Kt::KernelLogStream(Kt::OK, "HardwareAbstraction") << "Created exception interrupt vectors";
LoadIDT(IDTR);
kout << "HardwareAbstraction: Loaded new IDT" << "\n";
Kt::KernelLogStream(Kt::OK, "HardwareAbstraction") << "Loaded new IDT";
}
};