Network commands port to userspace

This commit is contained in:
boreddevnl
2026-02-27 23:24:43 +01:00
parent 304c2e1383
commit 8a6928b5c9
22 changed files with 680 additions and 69 deletions

View File

@@ -51,8 +51,8 @@ void process_create(void* entry_point, bool is_user) {
if (!new_proc->pml4_phys) return;
// 2. Allocate aligned stack
void* stack = kmalloc_aligned(524288, 4096); // 512KB for kernel threads
void* kernel_stack = kmalloc_aligned(126976, 16384); // 128KB for ringswitch
void* stack = kmalloc_aligned(4096, 4096);
void* kernel_stack = kmalloc_aligned(16384, 16384); // Needed for when user interrupts to Ring 0
if (is_user) {
// Map user stack to 0x800000
@@ -66,7 +66,7 @@ void process_create(void* entry_point, bool is_user) {
// Build initial stack frame for iretq
// Stack grows down, start at top
uint64_t* stack_ptr = (uint64_t*)((uint64_t)kernel_stack + 126976);
uint64_t* stack_ptr = (uint64_t*)((uint64_t)kernel_stack + 16384);
*(--stack_ptr) = 0x1B; // SS (User Data)
*(--stack_ptr) = 0x800000 + 4096; // RSP
@@ -79,11 +79,11 @@ void process_create(void* entry_point, bool is_user) {
// Push 15 zeros for general purpose registers (r15 -> rax)
for (int i = 0; i < 15; i++) *(--stack_ptr) = 0;
new_proc->kernel_stack = (uint64_t)kernel_stack + 126976;
new_proc->kernel_stack = (uint64_t)kernel_stack + 16384;
new_proc->rsp = (uint64_t)stack_ptr;
} else {
// Kernel thread
uint64_t* stack_ptr = (uint64_t*)((uint64_t)stack + 524288);
uint64_t* stack_ptr = (uint64_t*)((uint64_t)stack + 4096);
*(--stack_ptr) = 0x10; // SS (Kernel Data)
stack_ptr--;
*stack_ptr = (uint64_t)stack_ptr; // RSP
@@ -134,18 +134,17 @@ process_t* process_create_elf(const char* filepath, const char* args_str) {
}
// 3. Allocate generic User stack and Kernel stack for interrupts
void* stack = kmalloc_aligned(2097152, 4096); // 2MB for user apps
void* kernel_stack = kmalloc_aligned(126976, 16384); // 128KB for interrupts
void* stack = kmalloc_aligned(65536, 4096);
void* kernel_stack = kmalloc_aligned(16384, 16384);
// Map User stack to 0x800000 (starting from 0x600000 for 2MB)
for (uint64_t i = 0; i < 512; i++) {
paging_map_page(new_proc->pml4_phys, 0x800000 - 2097152 + (i * 4096), v2p((uint64_t)stack + (i * 4096)), PT_PRESENT | PT_RW | PT_USER);
// Map User stack to 0x800000 (starting from 0x7F0000 for 64KB)
for (uint64_t i = 0; i < 16; i++) {
paging_map_page(new_proc->pml4_phys, 0x800000 - 65536 + (i * 4096), v2p((uint64_t)stack + (i * 4096)), PT_PRESENT | PT_RW | PT_USER);
}
// Parse arguments and push them to the user stack
// We'll place the strings at the very high end of the user stack
int argc = 1;
char *args_buf = (char *)stack + 2097152;
char *args_buf = (char *)stack + 65536;
uint64_t user_args_buf = 0x800000;
// Copy filepath as argv[0]
@@ -197,7 +196,7 @@ process_t* process_create_elf(const char* filepath, const char* args_str) {
// Align stack to 8 bytes before pushing argv array
uint64_t current_user_sp = user_args_buf;
current_user_sp &= ~7ULL;
args_buf = (char *)((uint64_t)stack + (current_user_sp - (0x800000 - 2097152)));
args_buf = (char *)((uint64_t)stack + (current_user_sp - (0x800000 - 65536)));
// Push argv array
int argv_size = (argc + 1) * sizeof(uint64_t);
@@ -215,7 +214,7 @@ process_t* process_create_elf(const char* filepath, const char* args_str) {
current_user_sp &= ~15ULL;
// 4. Build Stack Frame for context switch via IRETQ
uint64_t* stack_ptr = (uint64_t*)((uint64_t)kernel_stack + 126976);
uint64_t* stack_ptr = (uint64_t*)((uint64_t)kernel_stack + 16384);
*(--stack_ptr) = 0x1B; // SS (User Mode Data)
*(--stack_ptr) = current_user_sp; // RSP (Updated user stack pointer)
*(--stack_ptr) = 0x202; // RFLAGS (Interrupts Enabled)