diff --git a/meta/targets/navy-x86_64.json b/meta/targets/navy-x86_64.json index 27ab785..2173fc7 100644 --- a/meta/targets/navy-x86_64.json +++ b/meta/targets/navy-x86_64.json @@ -26,7 +26,8 @@ "-mno-mmx", "-mno-3dnow", "-mno-sse", - "-mno-sse2" + "-mno-sse2", + "-g" ] }, "cxx": { diff --git a/src/kernel/archs/x86_64/ctx.h b/src/kernel/archs/x86_64/ctx.h index b50957f..d02b3c5 100644 --- a/src/kernel/archs/x86_64/ctx.h +++ b/src/kernel/archs/x86_64/ctx.h @@ -6,7 +6,12 @@ #define STACK_SIZE (kib$(4)) #define KERNEL_STACK_SIZE (0x1000) -#define USER_STACK_BASE (0xff0000000) +#define USER_STACK_TOP (0x7fffffffe000) +#define USER_STACK_SIZE (mib$(2)) +#define USER_STACK_BASE (USER_STACK_TOP - USER_STACK_SIZE) +#define USER_HEAP_BASE (0x80000000000) +#define USER_HEAP_SIZE (gib$(4)) +#define USER_HEAP_TOP (USER_HEAP_BASE + USER_HEAP_SIZE) struct _HalContext { diff --git a/src/kernel/core/main.c b/src/kernel/core/main.c index 691e053..1bc0ac1 100644 --- a/src/kernel/core/main.c +++ b/src/kernel/core/main.c @@ -15,6 +15,8 @@ _Noreturn int _start() hal_panic(); } + vmem_bootstrap(); + Res hal = hal_setup(); if (hal.type != RES_OK) { diff --git a/src/kernel/core/manifest.json b/src/kernel/core/manifest.json index f924bf2..c2e0abd 100644 --- a/src/kernel/core/manifest.json +++ b/src/kernel/core/manifest.json @@ -7,7 +7,8 @@ "dbg", "stdc-shim", "loader", - "kmalloc" + "kmalloc", + "tinyvmem" ], "tools": { "cc": { diff --git a/src/kernel/core/pre-sched.c b/src/kernel/core/pre-sched.c index 7616b39..083c0b1 100644 --- a/src/kernel/core/pre-sched.c +++ b/src/kernel/core/pre-sched.c @@ -1,7 +1,11 @@ #include +#include #include +#include +#include #include #include +#include #include "hal.h" #include "pmm.h" @@ -19,6 +23,7 @@ static HalPage *sched_vspace; static HalContext *sched_ctx; +static Vmem sched_vmem; static HalContext *kernel_ctx; static bool need_switch = false; @@ -33,6 +38,8 @@ Res load_scheduler(void) try$(hal_space_create(&sched_vspace)); + vmem_init(&sched_vmem, "procman", (void *)USER_HEAP_BASE, USER_HEAP_SIZE, PMM_PAGE_SIZE, 0, 0, 0, 0, 0); + Elf_Ehdr *hdr = (Elf_Ehdr *)sched.start; if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) { @@ -76,9 +83,24 @@ Res load_scheduler(void) return err$(RES_NOMEM); } - hal_space_map(sched_vspace, USER_STACK_BASE, sched_stack_obj.base, STACK_SIZE, HAL_MEM_READ | HAL_MEM_WRITE | HAL_MEM_USER); + HandoverBuilder builder; + PmmObj handover_obj = pmm_alloc(align_up$(kib$(16), PMM_PAGE_SIZE) / PMM_PAGE_SIZE); + if (handover_obj.len == 0) + { + return err$(RES_NOMEM); + } - hal_context_start(sched_ctx, hdr->e_entry, USER_STACK_BASE, (SysArgs){0, 0, 0, 0, 0, 0}); + handover_builder_init(&builder, (void *)hal_mmap_l2h(handover_obj.base), kib$(16)); + handover_parse_module(&builder); + + builder.payload->magic = HANDOVER_MAGIC; + + uintptr_t handover_addr = (uintptr_t)vmem_alloc(&sched_vmem, kib$(16), VM_INSTANTFIT); + + try$(hal_space_map(sched_vspace, align_down$(handover_addr, PMM_PAGE_SIZE), align_down$((uintptr_t)handover_obj.base, PMM_PAGE_SIZE), + align_up$(builder.size, PMM_PAGE_SIZE), HAL_MEM_READ | HAL_MEM_USER)); + try$(hal_space_map(sched_vspace, USER_STACK_BASE, sched_stack_obj.base, STACK_SIZE, HAL_MEM_READ | HAL_MEM_WRITE | HAL_MEM_USER)); + hal_context_start(sched_ctx, hdr->e_entry, USER_STACK_BASE, (SysArgs){handover_addr, 0, 0, 0, 0, 0}); need_switch = true; return ok$(); diff --git a/src/kernel/klibs/limine/handover.c b/src/kernel/klibs/limine/handover.c index 0c8d09f..d0286eb 100644 --- a/src/kernel/klibs/limine/handover.c +++ b/src/kernel/klibs/limine/handover.c @@ -55,7 +55,7 @@ void handover_parse_mmap(HandoverBuilder *self) error$("Couldn't retrieve memory map from Limine"); hal_panic(); } - + if (kernel_addr_req.response == NULL) { error$("Couldn't retrieve kernel address from Limine"); @@ -215,7 +215,6 @@ HandoverPayload *handover(void) handover_parse_mmap(&builder); is_handover_init = true; - } return builder.payload; diff --git a/src/kernel/klibs/tinyvmem/manifest.json b/src/kernel/klibs/tinyvmem/manifest.json index 4a493b1..8a44a22 100644 --- a/src/kernel/klibs/tinyvmem/manifest.json +++ b/src/kernel/klibs/tinyvmem/manifest.json @@ -1,7 +1,7 @@ { "$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1", "type": "lib", - "id": "mem", + "id": "tinyvmem", "requires": [ "dbg", "stdc-shim" diff --git a/src/kernel/klibs/tinyvmem/tinyvmem.c b/src/kernel/klibs/tinyvmem/tinyvmem.c index fa93b09..074354b 100644 --- a/src/kernel/klibs/tinyvmem/tinyvmem.c +++ b/src/kernel/klibs/tinyvmem/tinyvmem.c @@ -7,6 +7,7 @@ */ #include +#include #include #include "tinyvmem.h" @@ -21,6 +22,7 @@ #else # include # include + # include "../../core/pmm.h" # define vmem_printf(...) ((void)0) # define ASSERT(x) (x ? (void)0 : _ASSERT(__FILE__, __LINE__, #x)) @@ -74,8 +76,17 @@ static int nfreesegs = 0; #ifdef __ck_sys_kernel__ -void vmem_lock(void); -void vmem_unlock(void); +static Spinlock lock = SPINLOCK_INIT; + +void vmem_lock(void) +{ + spinlock_acquire(&lock); +} + +void vmem_unlock(void) +{ + spinlock_release(&lock); +} #else # define vmem_lock() diff --git a/src/srvs/procman/main.c b/src/srvs/procman/main.c index 10f157b..533a3f5 100644 --- a/src/srvs/procman/main.c +++ b/src/srvs/procman/main.c @@ -1,7 +1,17 @@ #include +#include +#include -_Noreturn int _start() +_Noreturn int _start(uintptr_t handover_addr) { + HandoverPayload *handover = (HandoverPayload *)handover_addr; + + if (handover->magic != HANDOVER_MAGIC) + { + error$("Invalid handover magic"); + for (;;); + } + log$("Hello, world!"); for (;;) ;