user program (still many #PF)

This commit is contained in:
2026-04-02 17:05:51 +02:00
parent 11a9dd4adb
commit aa30d9c6b5
11 changed files with 107 additions and 16 deletions
+58
View File
@@ -4,6 +4,8 @@
* @license GPL-3.0-only
*/
#include "mem/paging.h"
#include "mem/vmm.h"
#include <stddef.h>
#include <sched/process.h>
#include <mem/kheap.h>
@@ -13,6 +15,7 @@
#include <config.h>
#include <io/serial/serial.h>
#include <io/term/flanterm.h>
#include <mem/utils.h>
extern struct flanterm_context* ft_ctx;
@@ -196,4 +199,59 @@ void process_exit()
for (;;) {
asm("hlt");
}
}
/*
* process_jump_to_user - Jump to userland
* @stack_top: Address of the top of the user stack
* @user_code: Address of the first instruction of user code
*/
void process_jump_to_user(uintptr_t stack_top, uintptr_t user_code)
{
// 0x20 | 3 = 0x23 (user data segment | 3)
// 0x18 | 3 = 0x1B (user code segment | 3)
asm volatile(" \
push $0x23 \n\
push %0 \n\
push $0x202 \n\
push $0x1B \n\
push %1 \n\
iretq \n\
" :: "r"(stack_top), "r"(user_code));
}
// Kernel stack used for interrupts from userland process.
// Should be set in TSS.RSP0 when switching to userland process.
uint8_t interrupt_stack[0x8000];
extern struct tss tss;
/*
* process_create_user - Create a new user process
* @file: pointer to Limine file structure
*
* This function takes a loaded Limine executable
* module, and maps its code, a user stack, sets the
* TSS RSP0 for interrupts, and finally jumps to the
* user code.
*/
void process_create_user(struct limine_file* file)
{
void* exec_addr = file->address;
uint64_t exec_size = file->size;
uint64_t* user_pml4 = vmm_create_address_space();
uintptr_t stack_top = vmm_alloc_user_stack(user_pml4);
uint64_t code = vmm_alloc_user_code(user_pml4, exec_addr, exec_size);
// Could be kalloc_stack()ed PER PROCESS when we grow that
tss.rsp0 = (uint64_t)(interrupt_stack + sizeof(interrupt_stack));
// Load user_pml4 into cr3 along here??
load_cr3(VIRT_TO_PHYS((uint64_t)user_pml4));
// Copy code into user pages
memcpy((uint64_t*)code, exec_addr, exec_size);
process_jump_to_user(stack_top, code);
}