feat: almost posix-like entry point

This commit is contained in:
Jordan ⌨️ 2023-12-22 10:43:20 +01:00 committed by Keyboard Slayer
parent 6d143cccf1
commit 013e95f766
12 changed files with 155 additions and 16 deletions

View file

@ -39,8 +39,8 @@ Res hal_context_start(HalContext *self, uintptr_t ip, uintptr_t sp, SysArgs args
self->regs.cs = (GDT_USER_CODE * 8) | 3;
self->regs.ss = (GDT_USER_DATA * 8) | 3;
self->regs.rsp = sp + STACK_SIZE;
self->regs.rbp = sp;
self->regs.rsp = sp;
self->regs.rbp = 0;
PmmObj kStackObj = pmm_alloc(align_up$(STACK_SIZE, PMM_PAGE_SIZE) / PMM_PAGE_SIZE);
if (kStackObj.base == 0)

View file

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

View file

@ -2,11 +2,13 @@
#include <hal.h>
#include <handover.h>
#include <kmalloc/kmalloc.h>
#include <math.h>
#include <mem/stack.h>
#include <specs/elf.h>
#include <string.h>
#include <tinyvmem/tinyvmem.h>
#include "x86_64/ctx.h"
#include "hal.h"
#include "pmm.h"
#include "pre-sched.h"
@ -24,6 +26,7 @@
static HalPage *sched_vspace;
static HalContext *sched_ctx;
static Vmem sched_vmem;
static Stack sched_stack;
static HalContext *kernel_ctx;
static bool need_switch = false;
@ -77,7 +80,7 @@ Res load_scheduler(void)
sched_ctx = (HalContext *)try$(hal_context_create());
kernel_ctx = (HalContext *)try$(hal_context_create());
PmmObj sched_stack_obj = pmm_alloc(align_up$(STACK_SIZE, PMM_PAGE_SIZE) / PMM_PAGE_SIZE);
PmmObj sched_stack_obj = pmm_alloc(align_up$(USER_STACK_SIZE, PMM_PAGE_SIZE) / PMM_PAGE_SIZE);
if (sched_stack_obj.len == 0)
{
return err$(RES_NOMEM);
@ -97,10 +100,27 @@ Res load_scheduler(void)
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});
Alloc kmalloc = kmalloc_acquire();
sched_stack = stack_init(hal_mmap_l2h(sched_stack_obj.base), USER_STACK_SIZE);
uintptr_t *argc = (uintptr_t *)try$(kmalloc.calloc(1, sizeof(uintptr_t)));
*argc = 2;
uintptr_t *argv = (uintptr_t *)try$(kmalloc.calloc(2, sizeof(uintptr_t)));
argv[0] = USER_STACK_TOP - stack_push(&sched_stack, "boot:///bin/procman\0", 20);
argv[1] = handover_addr;
stack_push(&sched_stack, argv, 2 * sizeof(uintptr_t));
stack_push(&sched_stack, (void *)argc, sizeof(uintptr_t));
kmalloc.free(argc);
kmalloc.free(argv);
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, USER_STACK_SIZE, HAL_MEM_READ | HAL_MEM_WRITE | HAL_MEM_USER));
hal_context_start(sched_ctx, hdr->e_entry, USER_STACK_TOP - sched_stack.off, (SysArgs){0, 0, 0, 0, 0, 0});
need_switch = true;
return ok$();

View file

@ -0,0 +1,30 @@
#include <dbg/log.h>
#include <res.h>
#include <stddef.h>
#include <stdint.h>
[[gnu::weak]] Res main([[gnu::unused]] int argc, [[gnu::unused]] char **argv)
{
return ok$();
}
static void parse_stack(uintptr_t *stack, int *argc, char ***argv)
{
uintptr_t *sp = stack;
*argc = *sp++;
*argv = (char **)(sp);
sp += *argc;
sp++;
}
int _entry(char *stack)
{
int argc = 0;
char **argv = NULL;
parse_stack((uintptr_t *)stack, &argc, &argv);
main(argc, argv);
for (;;)
;
}

View file

@ -0,0 +1,9 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"type": "lib",
"id": "entry",
"requires": [
"entry-arch",
"stdc-shim"
]
}

View file

@ -0,0 +1,8 @@
.text
.globl _start
_start:
xor %rbp, %rbp
mov %rsp, %rdi
call _entry

View file

@ -0,0 +1,13 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"type": "lib",
"id": "entry-x86_64",
"enableIf": {
"arch": [
"x86_64"
]
},
"provides": [
"entry-arch"
]
}

View file

@ -0,0 +1,5 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"type": "lib",
"id": "mem"
}

24
src/libs/mem/stack.c Normal file
View file

@ -0,0 +1,24 @@
#include <dbg/log.h>
#include <string.h>
#include "stack.h"
Stack stack_init(uintptr_t base, size_t len)
{
Stack stack = {
.base = base + len,
.len = len,
.off = 0,
};
return stack;
}
size_t stack_push(Stack *self, void *data, size_t size)
{
self->base -= size;
self->off += size;
memcpy((void *)self->base, data, size);
return self->off;
}

23
src/libs/mem/stack.h Normal file
View file

@ -0,0 +1,23 @@
#pragma once
#include <hal.h>
#include <stddef.h>
#include <stdint.h>
#include <tinyvmem/tinyvmem.h>
typedef struct
{
uintptr_t base;
size_t len;
size_t off;
} Stack;
struct stack_push_param
{
HalPage *vspace;
Vmem *vmem;
};
Stack stack_init(uintptr_t base, size_t len);
size_t stack_push(Stack *self, void *data, size_t size);

View file

@ -1,18 +1,23 @@
#include <dbg/log.h>
#include <handover/handover.h>
#include <stdint.h>
_Noreturn int _start(uintptr_t handover_addr)
Res main(int argc, char const *argv[])
{
HandoverPayload *handover = (HandoverPayload *)handover_addr;
if (argc == 1)
{
error$("no handover payload was provided");
return err$(RES_INVAL);
}
HandoverPayload *handover = (HandoverPayload *)argv[1];
if (handover->magic != HANDOVER_MAGIC)
{
error$("Invalid handover magic");
for (;;);
return err$(RES_INVAL);
}
log$("Hello, world!");
for (;;)
;
log$("Hello from procman!");
return ok$();
}

View file

@ -3,6 +3,7 @@
"type": "exe",
"id": "procman",
"requires": [
"entry",
"dbg"
],
"enableIf": {