Fixed ELF loading again, memory management needs to be redone properly

This commit is contained in:
Jozef Nagy 2025-04-16 23:11:21 +02:00
parent 67f719c73f
commit 819a24ab8d
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
9 changed files with 68 additions and 28 deletions

View file

@ -65,21 +65,21 @@ uintptr_t elf64_load(char *data, pagetable *pagemap)
flags |= VMM_WRITABLE;
if (!(ph[i].p_flags & PF_X))
flags |= VMM_NX;
debug("elf64_load(): phys=0x%llx, virt=0x%llx, size=%lu\n", ph[i].p_paddr, ph[i].p_vaddr, ph[i].p_filesz);
uint64_t phys = (uint64_t)mem_alloc(ph[i].p_memsz);
if (!phys) {
debug("elf64_load(): Out of memory\n");
return 0;
}
debug("elf64_load(): phys=0x%llx, virt=0x%llx, size=%lu\n", phys, ph[i].p_vaddr, ph[i].p_filesz);
map_page(pagemap, ph[i].p_vaddr, phys, flags);
memcpy((void*)ph[i].p_vaddr - lowest, data + ph[i].p_offset, ph[i].p_filesz);
}
debug("elf64_load(): ELF loaded successfully, entry: 0x%llx\n", header->e_entry);
return (uintptr_t)((uint8_t *)data + (header->e_entry - lowest));
return (uintptr_t)((uint8_t *)data + header->e_entry);
}
uintptr_t elf_load(char *data, pagetable *pagemap)

View file

@ -23,6 +23,9 @@
#include <mm/vmm.h>
#include <vfs/vfs.h>
#include <print.h>
#include <axboot.h>
#include <efi.h>
#include <efilib.h>
extern __attribute__((noreturn)) void aurix_handoff(void *pagemap, void *stack, uint64_t entry, void *params);
extern char _aurix_handoff_start[], _aurix_handoff_end[];
@ -35,20 +38,24 @@ void aurix_load(char *kernel)
// TODO: Do something with the kernel :p
pagetable *pm = create_pagemap();
// __asm__ volatile("mov %%cr3, %0" : "=r"(pm));
if (!pm) {
debug("aurix_load(): Failed to create kernel pagemap! Halting...\n");
// TODO: Halt
while (1);
}
map_pages(pm, (uintptr_t)_aurix_handoff_start, (uintptr_t)_aurix_handoff_start, (uint64_t)_aurix_handoff_end - (uint64_t)_aurix_handoff_start, VMM_PRESENT | VMM_USER | VMM_WRITABLE);
map_pages(pm, (uintptr_t)pm, (uintptr_t)pm, PAGE_SIZE, VMM_WRITABLE);
map_pages(pm, (uintptr_t)_aurix_handoff_start, (uintptr_t)_aurix_handoff_start, (uint64_t)_aurix_handoff_end - (uint64_t)_aurix_handoff_start, 0);
void *stack = mem_alloc(16*1024); // 16 KiB stack should be well more than enough
if (!stack) {
debug("aurix_load(): Failed to allocate stack! Halting...\n");
while (1);
}
map_pages(pm, (uintptr_t)stack, (uintptr_t)stack, 16*1024, VMM_WRITABLE | VMM_NX);
void *kernel_entry = (void *)elf_load(kbuf, pm);
if (!kernel_entry) {
debug("aurix_load(): Failed to load '%s'! Halting...\n", kernel);
@ -60,10 +67,14 @@ void aurix_load(char *kernel)
debug("aurix_load(): Handoff state: pm=0x%llx, stack=0x%llx, kernel_entry=0x%llx\n", pm, stack, kernel_entry);
aurix_handoff(pm, (void *)((uint8_t)stack + 16*1024), (uint64_t)kernel_entry, (void *)parameters);
// this triggers a #GP ????
// aurix_handoff(pm, stack, (uint64_t)kernel_entry, (void *)parameters);
// __builtin_unreachable();
// __asm__ volatile("movq %[pml4], %%cr3\n" :: [pml4]"r"(pm) : "memory");
__asm__ volatile("movq %[pml4], %%cr3\n"
"movq %[stack], %%rsp\n"
"callq *%[entry]\n"
:: [pml4]"r"(pm), [stack]"r"(stack), [entry]"r"(kernel_entry) : "memory");
// __asm__ volatile("callq *%[entry]\n"
// :: [entry]"r"(kernel_entry));
}
// :: [entry]"r"(kernel_entry));
}