summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/sys/init.c
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2026-09-05 20:24:13 -0400
committerJon Santmyer <jon@jonsantmyer.com>2026-09-05 20:24:13 -0400
commit779b7fc1fe91e5a355b6e15de882d46b26ab2270 (patch)
treec62da25a0a0b02488550e856921350fd94668c2b /arch/x86_64/sys/init.c
parent73cbb23130f634bf52ac92da063efd3e6016f8ee (diff)
downloadjove-kernel-779b7fc1fe91e5a355b6e15de882d46b26ab2270.tar.gz
jove-kernel-779b7fc1fe91e5a355b6e15de882d46b26ab2270.tar.bz2
jove-kernel-779b7fc1fe91e5a355b6e15de882d46b26ab2270.zip
working syscalls.
Diffstat (limited to 'arch/x86_64/sys/init.c')
-rw-r--r--arch/x86_64/sys/init.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/arch/x86_64/sys/init.c b/arch/x86_64/sys/init.c
index 26eefde..cecca32 100644
--- a/arch/x86_64/sys/init.c
+++ b/arch/x86_64/sys/init.c
@@ -1,6 +1,7 @@
#include "init.h"
#include "arch/x86_64/hal/msr.h"
#include "config.h"
+#include "hal/process.h"
#include "jove.h"
#include "hal/obj.h"
#include "lib/string.h"
@@ -98,15 +99,16 @@ init_setup(void)
jove_owned_obj_t *init_pagedir = objdir_getlocal(_rootdir_obj, ROOTDIR_PAGEDIR_I);
page_t *pml4 = obj_deref(init_pagedir);
+ size_t last_base = 0;
//Load the init ELF
- for(size_t i = 0; i < init_ehdr->e_shnum; i++) {
- struct elf_shdr *shdr = init_data + init_ehdr->e_shoff + i * init_ehdr->e_shentsize;
- if(shdr->sh_type != SHT_PROGBITS && shdr->sh_type != SHT_NOBITS) continue;
- if((shdr->sh_flags & SHF_ALLOC) == 0) continue;
+ for(size_t i = 0; i < init_ehdr->e_phnum; i++) {
+ struct elf_phdr *phdr = init_data + init_ehdr->e_phoff + i * init_ehdr->e_phentsize;
+ if(phdr->p_type != PT_LOAD) continue;
+ kprintf("init: phdr %i flags %b\n", i, phdr->p_flags);
- uintptr_t base = shdr->sh_addr;
- size_t limit = shdr->sh_size;
+ uintptr_t base = phdr->p_vaddr;
+ size_t limit = phdr->p_memsz;
uintptr_t page_base = base & ~0xFFFULL;
size_t pml3i = (page_base >> PMLI_SHL(3)) & 0x1FF;
@@ -125,15 +127,15 @@ init_setup(void)
if(pml0i > INIT_PROGRAM_PAGES_MAX) {
kpanic("init: Program exceeds maximum program pages in the same hugepage frame!\n");
}
+ if(last_base < base + limit) last_base = base + limit;
uintptr_t pml0_pptr = init_program_pml0_pptr + (pml0i << 12);
s_init_program_pml1[pml0i].value = pml0_pptr | PAGE_PRESENT | PAGE_USER | PAGE_WRITEABLE;
}
//Copy data from section if SHT_PROGBITS
- if(shdr->sh_type == SHT_PROGBITS) {
- memcpy((void*)shdr->sh_addr, init_data + shdr->sh_offset, shdr->sh_size);
- }
+ kprintf("init: load %p : %p\n", base, limit);
+ memcpy((void*)phdr->p_vaddr, init_data + phdr->p_offset, phdr->p_memsz);
}
//Map the stack
@@ -147,19 +149,37 @@ init_setup(void)
s_init_stack_pml1[pml0i].value = page_pptr | PAGE_PRESENT | PAGE_WRITEABLE | PAGE_USER | PAGE_XD;
}
+ //Map the syscall message.
+ last_base = (last_base + 0xFFF) & ~0xFFF;
+ size_t last_page = (last_base >> 12) & 0x1FF;
+ uintptr_t message_pptr = init_program_pml0_pptr + (last_page << 12);
+ s_init_program_pml1[last_page].value = message_pptr | PAGE_PRESENT | PAGE_WRITEABLE | PAGE_USER | PAGE_XD;
+
+ jove_owned_obj_t *message_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_MESSAGE_I);
+ obj_settype(message_obj, JOVE_OBJ_MESSAGE);
+ obj_setvalue(message_obj, (message_pptr & ~0xFFF) | 1);
+ obj_release(message_obj);
+
obj_release(init_pagedir);
//Enable syscalls
extern void _syscall_entry(void);
msr_set_lstar(_syscall_entry);
+ //Setup KSP for syscalls
+ jove_owned_obj_t *pcb_obj = process_current();
+ pcb_t *pcb = obj_deref(pcb_obj);
+
+ obj_release(pcb_obj);
+
uintptr_t usp = 0x00007FFFFFFFFFF8;
uintptr_t uip = init_ehdr->e_entry;
__asm__ volatile("movq %0, %%rsp; \
movq %1, %%rcx; \
+ movq %2, %%rdi; \
movq $0x202, %%r11; \
swapgs; \
sysretq"::
- "r"(usp), "r"(uip): "memory");
+ "r"(usp), "r"(uip), "m"(last_base): "memory");
}