feat: handing over the handover

This commit is contained in:
Jordan ⌨️ 2023-12-21 15:14:26 +01:00 committed by Keyboard Slayer
parent e45cc2aff3
commit 6d143cccf1
9 changed files with 62 additions and 11 deletions

View file

@ -26,7 +26,8 @@
"-mno-mmx",
"-mno-3dnow",
"-mno-sse",
"-mno-sse2"
"-mno-sse2",
"-g"
]
},
"cxx": {

View file

@ -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
{

View file

@ -15,6 +15,8 @@ _Noreturn int _start()
hal_panic();
}
vmem_bootstrap();
Res hal = hal_setup();
if (hal.type != RES_OK)
{

View file

@ -7,7 +7,8 @@
"dbg",
"stdc-shim",
"loader",
"kmalloc"
"kmalloc",
"tinyvmem"
],
"tools": {
"cc": {

View file

@ -1,7 +1,11 @@
#include <dbg/log.h>
#include <hal.h>
#include <handover.h>
#include <kmalloc/kmalloc.h>
#include <math.h>
#include <specs/elf.h>
#include <string.h>
#include <tinyvmem/tinyvmem.h>
#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$();

View file

@ -215,7 +215,6 @@ HandoverPayload *handover(void)
handover_parse_mmap(&builder);
is_handover_init = true;
}
return builder.payload;

View file

@ -1,7 +1,7 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"type": "lib",
"id": "mem",
"id": "tinyvmem",
"requires": [
"dbg",
"stdc-shim"

View file

@ -7,6 +7,7 @@
*/
#include <string.h>
#include <sync/spinlock.h>
#include <sys/queue.h>
#include "tinyvmem.h"
@ -21,6 +22,7 @@
#else
# include <dbg/log.h>
# include <hal.h>
# 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()

View file

@ -1,7 +1,17 @@
#include <dbg/log.h>
#include <handover/handover.h>
#include <stdint.h>
_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 (;;)
;