Skip to content

Commit

Permalink
Kernel: Clean up IDT code, replace System.h
Browse files Browse the repository at this point in the history
Put error code in RegisterContext and replaced idt_flush with inline assembly.
System.h was removed and its contents were put in CPU.h (RegisterContext) and IOPorts.h (outportb, etc.)
  • Loading branch information
fido2020 committed Jan 12, 2022
1 parent be9166a commit 90ef4b3
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 218 deletions.
2 changes: 1 addition & 1 deletion Kernel/Modules/Intel8254x/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include <APIC.h>
#include <IDT.h>
#include <IOPorts.h>
#include <Logging.h>
#include <Net/Net.h>
#include <PCI.h>
#include <Paging.h>
#include <PhysicalAllocator.h>
#include <System.h>
#include <Timer.h>
#include <Vector.h>

Expand Down
53 changes: 52 additions & 1 deletion Kernel/include/Arch/x86_64/CPU.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <Compiler.h>
#include <System.h>
#include <TSS.h>
#include <stdint.h>

Expand All @@ -14,11 +13,17 @@ typedef struct {
uint64_t base;
} __attribute__((packed)) gdt_ptr_t;

typedef struct {
uint16_t limit;
uint64_t base;
} __attribute__((packed)) idt_ptr_t;

struct CPU {
CPU* self;
uint64_t id; // APIC/CPU id
void* gdt; // GDT
gdt_ptr_t gdtPtr;
idt_ptr_t idtPtr;
Thread* currentThread = nullptr;
Thread* idleThread = nullptr;
Process* idleProcess;
Expand Down Expand Up @@ -95,6 +100,52 @@ typedef struct {
uint32_t features_edx; // CPU features (edx)
} __attribute__((packed)) cpuid_info_t;

struct RegisterContext {
uint64_t r15;
uint64_t r14;
uint64_t r13;
uint64_t r12;
uint64_t r11;
uint64_t r10;
uint64_t r9;
uint64_t r8;
uint64_t rbp;
uint64_t rdi;
uint64_t rsi;
uint64_t rdx;
uint64_t rcx;
uint64_t rbx;
uint64_t rax;
uint64_t err;
uint64_t rip;
uint64_t cs;
uint64_t rflags;
uint64_t rsp;
uint64_t ss;
};

typedef struct {
uint16_t fcw; // FPU Control Word
uint16_t fsw; // FPU Status Word
uint8_t ftw; // FPU Tag Words
uint8_t zero; // Literally just contains a zero
uint16_t fop; // FPU Opcode
uint64_t rip;
uint64_t rdp;
uint32_t mxcsr; // SSE Control Register
uint32_t mxcsrMask; // SSE Control Register Mask
uint8_t st[8][16]; // FPU Registers, Last 6 bytes reserved
uint8_t xmm[16][16]; // XMM Registers
} __attribute__((packed)) fx_state_t;

static inline int CheckInterrupts() {
unsigned long flags;
asm volatile("pushf;"
"pop %%rax;"
: "=a"(flags)::"cc");
return (flags & 0x200);
}

cpuid_info_t CPUID();

ALWAYS_INLINE uintptr_t GetRBP() {
Expand Down
37 changes: 15 additions & 22 deletions Kernel/include/Arch/x86_64/IDT.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
#pragma once

#include <CPU.h>
#include <stdint.h>
#include <System.h>

#define IRQ0 32

#define IPI_HALT 0xFE
#define IPI_SCHEDULE 0xFD

typedef struct {
uint16_t base_low;
uint16_t sel;
uint8_t ist;
uint8_t flags;
uint16_t base_med;
uint32_t base_high;
uint32_t null;
uint16_t base_low;
uint16_t sel;
uint8_t ist;
uint8_t flags;
uint16_t base_med;
uint32_t base_high;
uint32_t null;
} __attribute__((packed)) idt_entry_t;

typedef struct {
uint16_t limit;
uint64_t base;
} __attribute__((packed)) idt_ptr_t;

typedef void(*isr_t)(void*, RegisterContext*);
typedef void (*isr_t)(void*, RegisterContext*);

extern "C" void idt_flush();

namespace IDT{
void Initialize();
void RegisterInterruptHandler(uint8_t interrupt, isr_t handler, void* data = nullptr);

void DisablePIC();
namespace IDT {
void Initialize();
void RegisterInterruptHandler(uint8_t interrupt, isr_t handler, void* data = nullptr);

uint8_t ReserveUnusedInterrupt();
void DisablePIC();

int GetErrCode();
}
uint8_t ReserveUnusedInterrupt();
} // namespace IDT
13 changes: 13 additions & 0 deletions Kernel/include/Arch/x86_64/IOPorts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <stdint.h>

void outportb(uint16_t port, uint8_t value);
void outportw(uint16_t port, uint16_t value);
void outportd(uint16_t port, uint32_t value);
void outportl(uint16_t port, uint32_t value);

uint8_t inportb(uint16_t port);
uint16_t inportw(uint16_t port);
uint32_t inportd(uint16_t port);
uint32_t inportl(uint16_t port);
4 changes: 2 additions & 2 deletions Kernel/include/Arch/x86_64/Paging.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <System.h>
#include <CPU.h>
#include <stdint.h>

#define KERNEL_VIRTUAL_BASE 0xFFFFFFFF80000000ULL
Expand Down Expand Up @@ -82,7 +82,7 @@ typedef struct PageMap { // Each process will have a maximum of 96GB of virtual
// Allows handling of page faults without kernel panic
struct PageFaultTrap {
uintptr_t instructionPointer;
void(*handler)();
void (*handler)();
};

class AddressSpace;
Expand Down
7 changes: 3 additions & 4 deletions Kernel/include/Arch/x86_64/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
#include <Lock.h>
#include <MM/AddressSpace.h>
#include <Memory.h>
#include <Objects/Handle.h>
#include <Objects/Process.h>
#include <Paging.h>
#include <System.h>
#include <Thread.h>
#include <Timer.h>
#include <Vector.h>
#include <stdint.h>
#include <Objects/Process.h>
#include <Objects/Handle.h>
#include <Thread.h>

#define KERNEL_CS 0x08
#define KERNEL_SS 0x10
Expand Down
62 changes: 0 additions & 62 deletions Kernel/include/Arch/x86_64/System.h

This file was deleted.

Loading

0 comments on commit 90ef4b3

Please sign in to comment.