1
0
Fork 0

feat/kernel: Started on LAPIC

This commit is contained in:
Kevin Alavik 2025-05-30 11:05:07 +02:00
parent 242ab03274
commit 4534f1da14
Signed by: cmpsb
GPG key ID: 10D1CC0526FDC6D7
5 changed files with 63 additions and 1 deletions

View file

@ -24,7 +24,8 @@
"lapic.h": "c", "lapic.h": "c",
"spinlock.h": "c", "spinlock.h": "c",
"fb.h": "c", "fb.h": "c",
"acpi.h": "c" "acpi.h": "c",
"madt.h": "c"
}, },
"editor.formatOnPaste": true, "editor.formatOnPaste": true,
"editor.formatOnSave": true, "editor.formatOnSave": true,

View file

@ -3,6 +3,7 @@
#define CPU_H #define CPU_H
#include <stdnoreturn.h> #include <stdnoreturn.h>
#include <stdint.h>
static inline noreturn void hlt() static inline noreturn void hlt()
{ {

View file

@ -20,6 +20,7 @@
#include <arch/smp.h> #include <arch/smp.h>
#include <sys/acpi.h> #include <sys/acpi.h>
#include <sys/acpi/madt.h> #include <sys/acpi/madt.h>
#include <sys/apic/lapic.h>
__attribute__((used, section(".limine_requests"))) static volatile LIMINE_BASE_REVISION(3); __attribute__((used, section(".limine_requests"))) static volatile LIMINE_BASE_REVISION(3);
__attribute__((used, section(".limine_requests"))) static volatile struct limine_memmap_request memmap_request = { __attribute__((used, section(".limine_requests"))) static volatile struct limine_memmap_request memmap_request = {
@ -182,6 +183,7 @@ void emk_entry(void)
/* Setup APIC */ /* Setup APIC */
madt_init(); madt_init();
lapic_init();
/* Finished */ /* Finished */
log_early("%s", LOG_SEPARATOR); log_early("%s", LOG_SEPARATOR);

View file

@ -0,0 +1,42 @@
/* EMK 1.0 Copyright (c) 2025 Piraterna */
#include <sys/apic/lapic.h>
#include <arch/cpu.h>
#include <sys/acpi/madt.h>
#include <util/log.h>
#include <mm/vmm.h>
#include <boot/emk.h>
uint64_t lapic_msr = 0;
volatile uint64_t *lapic_base = 0;
void lapic_write(uint32_t offset, uint32_t value)
{
if (!lapic_base)
{
log_early("warning: LAPIC not initialized!");
return;
}
volatile uint32_t *reg = (volatile uint32_t *)((uint8_t *)lapic_base + offset);
*reg = value;
}
uint32_t lapic_read(uint32_t offset)
{
if (!lapic_base)
{
log_early("warning: LAPIC not initialized!");
return 0;
}
volatile uint32_t *reg = (volatile uint32_t *)((uint8_t *)lapic_base + offset);
uint32_t value = *reg;
return value;
}
void lapic_init()
{
lapic_msr = rdmsr(LAPIC_BASE);
lapic_base = (volatile uint64_t *)(lapic_msr & ~(0xffff));
/* Change the lapic address that we got from MADT earlier */
lapic_addr = (uint64_t)lapic_base;
log_early("New LAPIC base: 0x%lx", lapic_base);
}

View file

@ -0,0 +1,16 @@
/* EMK 1.0 Copyright (c) 2025 Piraterna */
#ifndef LAPIC_H
#define LAPIC_H
#include <stdint.h>
#define LAPIC_BASE 0x1b
#define LAPIC_REGOFF_LAPICID 0x20
#define LAPIC_REGOFF_EOI 0xB0
#define LAPIC_REGOFF_SPIV 0xF0
uint32_t lapic_read(uint32_t offset);
void lapic_write(uint32_t offset, uint32_t value);
void lapic_init();
#endif // LAPIC_H