From 11a9dd4adbc690416ca72e38c37e3d3379316be9 Mon Sep 17 00:00:00 2001 From: xamidev Date: Wed, 1 Apr 2026 15:51:04 +0200 Subject: [PATCH] Load limine module + alloc user stack --- Makefile | 2 ++ include/config.h | 4 ++++ include/kernel.h | 1 + include/sched/process.h | 1 + limine.conf | 1 + src/boot/boot.c | 11 +++++++++-- src/debug/panic.c | 2 +- src/kmain.c | 12 ++++++++++++ src/mem/vmm.c | 25 +++++++++++++++++++++++++ user/hello.S | 5 +++++ 10 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 user/hello.S diff --git a/Makefile b/Makefile index 07bf3e1..1542d6d 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ CC_FLAGS=-Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fstack-protector -fn LD := x86_64-elf-ld $(ELFFILE): $(BUILDDIR) $(OBJFILES) + nasm -f bin user/hello.S -o $(BUILDDIR)/hello nasm -f elf64 src/arch/x86/idt.S -o $(BUILDDIR)/idt_stub.o $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o # Get the symbols for debugging @@ -47,6 +48,7 @@ build-iso: limine/limine $(ELFFILE) cp -v $(ELFFILE) iso_root/boot mkdir -p iso_root/boot/limine cp -v limine.conf iso_root/boot/limine + cp $(BUILDDIR)/hello iso_root/boot/ mkdir -p iso_root/EFI/BOOT cp -v limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/ cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/ diff --git a/include/config.h b/include/config.h index 2ec0d50..811ad4d 100644 --- a/include/config.h +++ b/include/config.h @@ -40,6 +40,10 @@ #define KERNEL_STACK_SIZE 65536 #define KERNEL_IDT_ENTRIES 33 +/* user */ +#define USER_STACK_TOP 0x80000000 +#define USER_STACK_PAGES 16 // 16*4096 = 64kb + /* paging */ #define PAGING_MAX_PHYS 0x200000000 diff --git a/include/kernel.h b/include/kernel.h index d77ca98..7a9712b 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -54,6 +54,7 @@ struct boot_context { struct limine_hhdm_response* hhdm; struct limine_kernel_address_response* kaddr; struct limine_boot_time_response* bootdate; + struct limine_module_response* module; }; // Are these modules initialized yet? diff --git a/include/sched/process.h b/include/sched/process.h index 22548f6..c42bf0d 100644 --- a/include/sched/process.h +++ b/include/sched/process.h @@ -24,6 +24,7 @@ struct process { status_t status; struct cpu_status* context; void* root_page_table; // Process PML4 (should contain kernel PML4 in higher half [256-511] + void* kernel_stack; // Used for interrupts (syscall: int 0x80), defines the TSS RSP0 struct process* next; }; diff --git a/limine.conf b/limine.conf index 71825ed..980ace0 100644 --- a/limine.conf +++ b/limine.conf @@ -6,3 +6,4 @@ interface_branding: Welcome to the PepperOS disk! comment: Default configuration (warning: spicy) path: boot():/boot/pepperk + module_path: boot():/boot/hello diff --git a/src/boot/boot.c b/src/boot/boot.c index 6fa8b9f..06a7eb4 100644 --- a/src/boot/boot.c +++ b/src/boot/boot.c @@ -13,6 +13,9 @@ #include #include +__attribute__((used, section(".limine_requests_start"))) +volatile LIMINE_REQUESTS_START_MARKER; + __attribute__((used, section(".limine_requests"))) volatile struct limine_framebuffer_request framebuffer_request = { .id = LIMINE_FRAMEBUFFER_REQUEST, @@ -43,8 +46,11 @@ volatile struct limine_boot_time_request date_request = { .revision = 0 }; -__attribute__((used, section(".limine_requests_start"))) -volatile LIMINE_REQUESTS_START_MARKER; +__attribute__((used, section(".limine_requests"))) +volatile struct limine_module_request module_request = { + .id = LIMINE_MODULE_REQUEST, + .revision = 0 +}; __attribute__((used, section(".limine_requests_end"))) volatile LIMINE_REQUESTS_END_MARKER; @@ -58,4 +64,5 @@ void populate_boot_context(struct boot_context* ctx) ctx->hhdm = hhdm_request.response ? hhdm_request.response : NULL; ctx->kaddr = kerneladdr_request.response ? kerneladdr_request.response : NULL; ctx->bootdate = date_request.response ? date_request.response : NULL; + ctx->module = module_request.response ? module_request.response : NULL; } \ No newline at end of file diff --git a/src/debug/panic.c b/src/debug/panic.c index bbeb884..71310b2 100644 --- a/src/debug/panic.c +++ b/src/debug/panic.c @@ -18,7 +18,7 @@ extern int panic_count; */ void read_rflags(uint64_t rflags) { - DEBUG("\x1b[38;5;226m%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\x1b[38;5;231m", + printf("\x1b[38;5;226m%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\x1b[38;5;231m", CHECK_BIT(rflags, 0) ? "CF " : "", /*carry flag*/ CHECK_BIT(rflags, 2) ? "PF " : "", /*parity flag*/ CHECK_BIT(rflags, 4) ? "AF " : "", /*auxiliary carry flag*/ diff --git a/src/kmain.c b/src/kmain.c index 470bc73..6d7cd21 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -62,6 +62,9 @@ extern volatile struct limine_memmap_request memmap_request; extern volatile struct limine_hhdm_request hhdm_request; extern volatile struct limine_kernel_address_request kerneladdr_request; extern volatile struct limine_boot_time_request date_request; +extern volatile struct limine_module_request module_request; + +struct limine_file* file; extern struct process* processes_list; extern struct process* current_process; @@ -119,6 +122,15 @@ void kmain() scheduler_init(); + if (!boot_ctx.module) { + panic(NULL, "could not load 'hello' executable :("); + } + + if (boot_ctx.module->module_count == 1) { + file = boot_ctx.module->modules[0]; + DEBUG("file: addr=%p size=%u", file->address, file->size); + } + printf(PEPPEROS_SPLASH); init.all = true; diff --git a/src/mem/vmm.c b/src/mem/vmm.c index 46de4fc..ec20a39 100644 --- a/src/mem/vmm.c +++ b/src/mem/vmm.c @@ -13,6 +13,7 @@ in a specified virtual space compared to the PMM which allocs/frees 4kb frames ("physical pages"). */ +#include "config.h" #include #include #include @@ -225,6 +226,30 @@ void* vmm_alloc_region(uint64_t* pml4, size_t pages, uint64_t flags) return NULL; } +/* + * vmm_map_user_stack - Map a user stack + * @pml4: the user process's PML4 + * + * This function maps and allocates a userspace + * stack in the user @pml4 provided, according + * to constants USER_STACK_TOP and USER_STACK_PAGES. + * + * Return: + * - User stack top address + */ + +uintptr_t vmm_alloc_user_stack(uint64_t* pml4) +{ + uintptr_t stack_top = USER_STACK_TOP; + size_t stack_size = USER_STACK_PAGES*PAGE_SIZE; + + for (size_t i=stack_top; i>stack_top-stack_size; i-=PAGE_SIZE) { + vmm_map(pml4, i, PTE_PRESENT | PTE_WRITABLE | PTE_USER); + } + + return stack_top; +} + void vmm_init() { // NO U diff --git a/user/hello.S b/user/hello.S new file mode 100644 index 0000000..be06990 --- /dev/null +++ b/user/hello.S @@ -0,0 +1,5 @@ +bits 64 + +hello: + mov qword rax, 0xcafebabe + jmp hello \ No newline at end of file