GDT finally works

This commit is contained in:
2025-08-10 21:29:16 +02:00
parent f8f00cc608
commit 8ee1ea1292
36 changed files with 868 additions and 206 deletions

View File

@ -0,0 +1,82 @@
#include <stdint.h>
#include <stddef.h>
#include <limine.h>
#include "bootinfo.h"
#include "hal/hal.h"
BootInfo BOOT_INFO;
static volatile struct limine_paging_mode_request PAGING_REQ = {
.id = LIMINE_PAGING_MODE_REQUEST,
.revision = 0,
#if defined(__x86_64__)
.mode = LIMINE_PAGING_MODE_X86_64_4LVL,
#else
# error "Paging mode is unknown for this architecture"
#endif
};
static volatile struct limine_kernel_address_request KERN_ADDR_REQ = {
.id = LIMINE_KERNEL_ADDRESS_REQUEST, .revision = 0,
};
static volatile struct limine_hhdm_request HHDM_REQ = {
.id = LIMINE_HHDM_REQUEST, .revision = 0,
};
static volatile struct limine_memmap_request MEMMAP_REQ = {
.id = LIMINE_MEMMAP_REQUEST, .revision = 0,
};
static volatile struct limine_smp_request SMP_REQ = {
.id = LIMINE_SMP_REQUEST, .revision = 0,
};
static volatile struct limine_rsdp_request RSDP_REQ = {
.id = LIMINE_RSDP_REQUEST, .revision = 0,
};
void bootinfo_init(void) {
struct limine_paging_mode_response *pagingres = PAGING_REQ.response;
#if defined(__x86_64__)
if (pagingres->mode != LIMINE_PAGING_MODE_X86_64_4LVL) {
#endif
hal_hang();
}
struct limine_hhdm_response *hhdmres = HHDM_REQ.response;
BOOT_INFO.hhdm_off = hhdmres->offset;
struct limine_kernel_address_response *kernaddrres = KERN_ADDR_REQ.response;
BOOT_INFO.kern_virtbase = kernaddrres->virtual_base;
BOOT_INFO.kern_physbase = kernaddrres->physical_base;
struct limine_memmap_response *memmapres = MEMMAP_REQ.response;
BOOT_INFO.memmap_entries = memmapres->entries;
BOOT_INFO.memmap_entrycount = memmapres->entry_count;
BOOT_INFO.memmap_total = 0;
for (size_t i = 0; i < memmapres->entry_count; i++) {
struct limine_memmap_entry *entry = memmapres->entries[i];
if (entry->type != LIMINE_MEMMAP_RESERVED) {
BOOT_INFO.memmap_total += entry->length;
}
}
struct limine_smp_response *smpres = SMP_REQ.response;
BOOT_INFO.smp = smpres;
BOOT_INFO.smp_bspindex = (uint64_t)(-1);
for (size_t i = 0; i < smpres->cpu_count; i++) {
struct limine_smp_info *entry = smpres->cpus[i];
if (entry->lapic_id == smpres->bsp_lapic_id) {
BOOT_INFO.smp_bspindex = i;
}
}
if (BOOT_INFO.smp_bspindex == (uint64_t)-1) {
hal_hang();
}
struct limine_rsdp_response *rsdpres = RSDP_REQ.response;
BOOT_INFO.rsdp = (size_t)rsdpres->address - BOOT_INFO.hhdm_off;
}

View File

@ -0,0 +1,33 @@
#ifndef BOOTINFO_BOOTINFO_H_
#define BOOTINFO_BOOTINFO_H_
#include <stdint.h>
#include <stddef.h>
#include <limine.h>
extern uint64_t kernel_text_start, kernel_text_end,
kernel_rodata_start, kernel_rodata_end,
kernel_data_start, kernel_data_end,
kernel_start, kernel_end;
typedef struct {
// Higher Half Direct Map: https://github.com/dreamportdev/Osdev-Notes/blob/master/01_Build_Process/02_Boot_Protocols.md#limine-protocol
size_t hhdm_off;
size_t kern_virtbase;
size_t kern_physbase;
size_t rsdp;
size_t memmap_total;
uint64_t memmap_entrycount;
LIMINE_PTR(struct limine_memmap_entry **) memmap_entries;
LIMINE_PTR(struct limine_smp_response *) smp;
uint64_t smp_bspindex;
} BootInfo;
extern BootInfo BOOT_INFO;
#define IS_IN_HHDM(a) ((size_t)a >= BOOT_INFO.hhdm_off \
&& (size_t)a <= BOOT_INFO.hhdm_off + BOOT_INFO.memmap_total)
void bootinfo_init(void);
#endif // BOOTINFO_BOOTINFO_H_