feat: almost posix-like entry point
This commit is contained in:
parent
6d143cccf1
commit
013e95f766
|
@ -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.cs = (GDT_USER_CODE * 8) | 3;
|
||||||
self->regs.ss = (GDT_USER_DATA * 8) | 3;
|
self->regs.ss = (GDT_USER_DATA * 8) | 3;
|
||||||
self->regs.rsp = sp + STACK_SIZE;
|
self->regs.rsp = sp;
|
||||||
self->regs.rbp = sp;
|
self->regs.rbp = 0;
|
||||||
|
|
||||||
PmmObj kStackObj = pmm_alloc(align_up$(STACK_SIZE, PMM_PAGE_SIZE) / PMM_PAGE_SIZE);
|
PmmObj kStackObj = pmm_alloc(align_up$(STACK_SIZE, PMM_PAGE_SIZE) / PMM_PAGE_SIZE);
|
||||||
if (kStackObj.base == 0)
|
if (kStackObj.base == 0)
|
||||||
|
|
|
@ -8,7 +8,8 @@
|
||||||
"stdc-shim",
|
"stdc-shim",
|
||||||
"loader",
|
"loader",
|
||||||
"kmalloc",
|
"kmalloc",
|
||||||
"tinyvmem"
|
"tinyvmem",
|
||||||
|
"mem"
|
||||||
],
|
],
|
||||||
"tools": {
|
"tools": {
|
||||||
"cc": {
|
"cc": {
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
#include <hal.h>
|
#include <hal.h>
|
||||||
#include <handover.h>
|
#include <handover.h>
|
||||||
#include <kmalloc/kmalloc.h>
|
#include <kmalloc/kmalloc.h>
|
||||||
#include <math.h>
|
#include <mem/stack.h>
|
||||||
#include <specs/elf.h>
|
#include <specs/elf.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <tinyvmem/tinyvmem.h>
|
#include <tinyvmem/tinyvmem.h>
|
||||||
|
|
||||||
|
#include "x86_64/ctx.h"
|
||||||
|
|
||||||
#include "hal.h"
|
#include "hal.h"
|
||||||
#include "pmm.h"
|
#include "pmm.h"
|
||||||
#include "pre-sched.h"
|
#include "pre-sched.h"
|
||||||
|
@ -24,6 +26,7 @@
|
||||||
static HalPage *sched_vspace;
|
static HalPage *sched_vspace;
|
||||||
static HalContext *sched_ctx;
|
static HalContext *sched_ctx;
|
||||||
static Vmem sched_vmem;
|
static Vmem sched_vmem;
|
||||||
|
static Stack sched_stack;
|
||||||
|
|
||||||
static HalContext *kernel_ctx;
|
static HalContext *kernel_ctx;
|
||||||
static bool need_switch = false;
|
static bool need_switch = false;
|
||||||
|
@ -77,7 +80,7 @@ Res load_scheduler(void)
|
||||||
sched_ctx = (HalContext *)try$(hal_context_create());
|
sched_ctx = (HalContext *)try$(hal_context_create());
|
||||||
kernel_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)
|
if (sched_stack_obj.len == 0)
|
||||||
{
|
{
|
||||||
return err$(RES_NOMEM);
|
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);
|
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),
|
Alloc kmalloc = kmalloc_acquire();
|
||||||
align_up$(builder.size, PMM_PAGE_SIZE), HAL_MEM_READ | HAL_MEM_USER));
|
sched_stack = stack_init(hal_mmap_l2h(sched_stack_obj.base), USER_STACK_SIZE);
|
||||||
try$(hal_space_map(sched_vspace, USER_STACK_BASE, sched_stack_obj.base, STACK_SIZE, HAL_MEM_READ | HAL_MEM_WRITE | HAL_MEM_USER));
|
uintptr_t *argc = (uintptr_t *)try$(kmalloc.calloc(1, sizeof(uintptr_t)));
|
||||||
hal_context_start(sched_ctx, hdr->e_entry, USER_STACK_BASE, (SysArgs){handover_addr, 0, 0, 0, 0, 0});
|
*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;
|
need_switch = true;
|
||||||
|
|
||||||
return ok$();
|
return ok$();
|
||||||
|
|
30
src/libs/entry/common/entry.c
Normal file
30
src/libs/entry/common/entry.c
Normal 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 (;;)
|
||||||
|
;
|
||||||
|
}
|
9
src/libs/entry/common/manifest.json
Normal file
9
src/libs/entry/common/manifest.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
|
||||||
|
"type": "lib",
|
||||||
|
"id": "entry",
|
||||||
|
"requires": [
|
||||||
|
"entry-arch",
|
||||||
|
"stdc-shim"
|
||||||
|
]
|
||||||
|
}
|
8
src/libs/entry/x86_64/entry.s
Normal file
8
src/libs/entry/x86_64/entry.s
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
.text
|
||||||
|
|
||||||
|
.globl _start
|
||||||
|
|
||||||
|
_start:
|
||||||
|
xor %rbp, %rbp
|
||||||
|
mov %rsp, %rdi
|
||||||
|
call _entry
|
13
src/libs/entry/x86_64/manifest.json
Normal file
13
src/libs/entry/x86_64/manifest.json
Normal 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"
|
||||||
|
]
|
||||||
|
}
|
5
src/libs/mem/manifest.json
Normal file
5
src/libs/mem/manifest.json
Normal 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
24
src/libs/mem/stack.c
Normal 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
23
src/libs/mem/stack.h
Normal 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);
|
|
@ -1,18 +1,23 @@
|
||||||
#include <dbg/log.h>
|
#include <dbg/log.h>
|
||||||
#include <handover/handover.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)
|
if (handover->magic != HANDOVER_MAGIC)
|
||||||
{
|
{
|
||||||
error$("Invalid handover magic");
|
error$("Invalid handover magic");
|
||||||
for (;;);
|
return err$(RES_INVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
log$("Hello, world!");
|
log$("Hello from procman!");
|
||||||
for (;;)
|
|
||||||
;
|
return ok$();
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
"type": "exe",
|
"type": "exe",
|
||||||
"id": "procman",
|
"id": "procman",
|
||||||
"requires": [
|
"requires": [
|
||||||
|
"entry",
|
||||||
"dbg"
|
"dbg"
|
||||||
],
|
],
|
||||||
"enableIf": {
|
"enableIf": {
|
||||||
|
|
Loading…
Reference in a new issue