From 71630b9fb04196db183de9a75a5656aa7c7607c5 Mon Sep 17 00:00:00 2001 From: Kevin Alavik Date: Fri, 16 May 2025 19:17:10 +0200 Subject: [PATCH] feat/kernel: Added MADT header for kernel --- kernel/src/arch/cpu.h | 14 +++++ kernel/src/sys/acpi.c | 2 - kernel/src/sys/acpi/madt.h | 103 +++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 kernel/src/sys/acpi/madt.h diff --git a/kernel/src/arch/cpu.h b/kernel/src/arch/cpu.h index b8f2e68..fb9378f 100644 --- a/kernel/src/arch/cpu.h +++ b/kernel/src/arch/cpu.h @@ -17,4 +17,18 @@ static inline noreturn void hcf() __asm__ volatile("hlt"); } +static inline void wrmsr(uint64_t msr, uint64_t value) +{ + uint32_t low = value & 0xFFFFFFFF; + uint32_t high = value >> 32; + asm volatile("wrmsr" : : "c"(msr), "a"(low), "d"(high)); +} + +static inline uint64_t rdmsr(uint64_t msr) +{ + uint32_t low, high; + asm volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr)); + return ((uint64_t)high << 32) | low; +} + #endif // CPU_H \ No newline at end of file diff --git a/kernel/src/sys/acpi.c b/kernel/src/sys/acpi.c index 8bf7d7d..1f36ac0 100644 --- a/kernel/src/sys/acpi.c +++ b/kernel/src/sys/acpi.c @@ -21,8 +21,6 @@ void acpi_init(void) if (memcmp(rsdp->signature, "RSD PTR", 7) != 0) kpanic(NULL, "Invalid RSDP signature!"); - log_early("RSDP Signature: %.*s", 7, rsdp->signature); - if (rsdp->revision != 0) { acpi_uses_xsdt = 1; diff --git a/kernel/src/sys/acpi/madt.h b/kernel/src/sys/acpi/madt.h new file mode 100644 index 0000000..f540868 --- /dev/null +++ b/kernel/src/sys/acpi/madt.h @@ -0,0 +1,103 @@ +/* EMK 1.0 Copyright (c) 2025 Piraterna */ +#ifndef MADT_H +#define MADT_H + +#include +#include + +#define MADT_ENTRY_LAPIC 0 /* Local APIC */ +#define MADT_ENTRY_IOAPIC 1 /* I/O APIC */ +#define MADT_ENTRY_IOAPIC_SRC_OVR 2 /* I/O APIC Interrupt Source Override */ +#define MADT_ENTRY_IOAPIC_NMI_SRC 3 /* I/O APIC Non-Maskable Interrupt Source */ +#define MADT_ENTRY_LAPIC_NMI 4 /* Local APIC Non-Maskable Interrupt */ +#define MADT_ENTRY_LAPIC_ADDR_OVR 5 /* Local APIC Address Override */ +#define MADT_ENTRY_LX2APIC 9 /* Local x2APIC */ + +typedef struct acpi_madt +{ + acpi_sdt_header_t sdt; + uint32_t lapic_base; + uint32_t flags; + char table[]; +} __attribute__((packed)) acpi_madt_t; + +typedef struct acpi_madt_entry +{ + uint8_t type; + uint8_t length; +} __attribute__((packed)) acpi_madt_entry_t; + +/* Entry Type 0: Processor Local APIC */ +typedef struct acpi_madt_lapic +{ + uint8_t type; /* 0 */ + uint8_t length; + uint8_t acpi_proc_id; /* ACPI Processor ID */ + uint8_t apic_id; /* APIC ID */ + uint32_t flags; /* Bit 0: Processor Enabled, Bit 1: Online Capable */ +} __attribute__((packed)) acpi_madt_lapic_t; + +/* Entry Type 1: I/O APIC */ +typedef struct acpi_madt_ioapic +{ + uint8_t type; + uint8_t length; + uint8_t ioapic_id; /* I/O APIC's ID */ + uint8_t reserved; /* Reserved (0) */ + uint32_t ioapic_addr; /* I/O APIC Address */ + uint32_t gsi_base; /* Global System Interrupt Base */ +} __attribute__((packed)) acpi_madt_ioapic_t; + +/* Entry Type 2: I/O APIC Interrupt Source Override */ +typedef struct acpi_madt_ioapic_src_ovr +{ + uint8_t type; + uint8_t length; + uint8_t bus_source; /* Bus Source */ + uint8_t irq_source; /* IRQ Source */ + uint32_t gsi; /* Global System Interrupt */ + uint16_t flags; /* Flags */ +} __attribute__((packed)) acpi_madt_ioapic_src_ovr_t; + +/* Entry Type 3: I/O APIC Non-Maskable Interrupt Source */ +typedef struct acpi_madt_ioapic_nmi_src +{ + uint8_t type; + uint8_t length; + uint8_t nmi_source; /* NMI Source */ + uint8_t reserved; /* Reserved */ + uint16_t flags; /* Flags */ + uint32_t gsi; /* Global System Interrupt */ +} __attribute__((packed)) acpi_madt_ioapic_nmi_src_t; + +/* Entry Type 4: Local APIC Non-Maskable Interrupt */ +typedef struct acpi_madt_lapic_nmi +{ + uint8_t type; + uint8_t length; + uint8_t acpi_proc_id; /* ACPI Processor ID (0xFF for all processors) */ + uint16_t flags; /* Flags */ + uint8_t lint; /* LINT# (0 or 1) */ +} __attribute__((packed)) acpi_madt_lapic_nmi_t; + +/* Entry Type 5: Local APIC Address Override */ +typedef struct acpi_madt_lapic_addr_ovr +{ + uint8_t type; + uint8_t length; + uint16_t reserved; /* Reserved */ + uint64_t lapic_addr; /* 64-bit Physical Address of Local APIC */ +} __attribute__((packed)) acpi_madt_lapic_addr_ovr_t; + +/* Entry Type 9: Processor Local x2APIC */ +typedef struct acpi_madt_lx2apic +{ + uint8_t type; + uint8_t length; + uint16_t reserved; + uint32_t x2apic_id; /* Processor's Local x2APIC ID */ + uint32_t flags; /* Flags (same as Local APIC) */ + uint32_t acpi_id; /* ACPI ID */ +} __attribute__((packed)) acpi_madt_lx2apic_t; + +#endif // MADT_H \ No newline at end of file