summaryrefslogtreecommitdiffstats
path: root/usr/umode.c
blob: 1105d9e1fe10abbec5cdf3aa5722c7606bb16a92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "umode.h"
#include "elf.h"
#include "boot/cmdline.h"
#include "lib/jove.h"
#include "ird/initrd.h"
#include "mem/memory.h"

void
umode_setup(void)
{
    extern void syscall_setup_syscall(void);
    syscall_setup_syscall();

    const char *init_path = cmdline_get("init");
    if(init_path == NULL)
        kpanic("Missing path to init ELF file / binary\n");

    struct InitrdFile *init_file = ird_getfile(init_path);
    if(init_file == NULL)
        kpanic("Missing init file %s in initrd\n", init_path);

    void (*entry_point)(void) = elf_load(init_file->data, init_file->size);
    if(entry_point == NULL)
        kpanic("Init file %s is incorrectly formatted (want ELF64)\n", init_path);

    void *user_stack = (void*)(0x00007FFFFFFFFFFF);
    mem_ensure_range(
            (uintptr_t)user_stack & ~0xFFF,
            (uintptr_t)user_stack,
            (page_flags_t) {
                .present = true,
                .writeable = true,
                .useraccess = true,
                .executable = false
            });

    klogf("User entry point %#016X\n", entry_point);
    umode_enter(entry_point, user_stack);
}