summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/hal/process.c
blob: c766a28dcfdd1138599cea9ddab8bc80756e18d3 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "process.h"
#include "arch/x86_64/hal/obj.h"
#include "hal/obj.h"
#include "hal/process.h"
#include "hal/smp.h"
#include "lib/string.h"
#include "init.h"
#include "smp.h"
#include "paging.h"
#include "jove.h"

PAGEALIGN uint8_t s_init_iopb[0x2000];
PAGEALIGN char s_init_pcb_bytes[0x1000] = { 0 };
static pcb_t *s_init_pcb = (pcb_t*)&s_init_pcb_bytes;

jove_owned_obj_t*
process_current(void)
{
    return obj_take(((processor_t*)processor_current())->pcb_obj);
}

pcb_t*
pcb_from_obj(jove_owned_obj_t *obj)
{
    if(obj_gettype(obj) != JOVE_OBJ_PROCESS) {
        reterr(EBADOBJ);
    }
    return obj_deref(obj);
}

pid_t
pcb_pid(pcb_t *pcb)
{
    return pcb->pid;
}

jove_owned_obj_t*
pcb_rootdir(pcb_t *pcb)
{
    return pcb->rootdir;
}

void
processing_setup(void (*process_main))
{
    memset(s_init_iopb, 0xFF, 0x2000);
    uintptr_t ksp = ((uintptr_t)&s_init_pcb_bytes) + 0xFF8;

    s_init_pcb->pid = 1;
    s_init_pcb->ksp = (void*)ksp;
    s_init_pcb->pml4 = paging_get_pml4();
    s_init_pcb->rootdir = obj_take(_rootdir_obj);

    jove_owned_obj_t *iopb_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_IOPB_I);
    obj_settype(iopb_obj, JOVE_OBJ_IOPB);
    obj_setvalue(iopb_obj, (uintptr_t)s_init_iopb);

    processor_t *proc = processor_current();
    proc->tss.rsp[0] = (uintptr_t)s_init_pcb->ksp;

    memcpy(proc->iopb, s_init_iopb, 0x2000);

    {
        jove_owned_obj_t *process_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_PCB_I);
        obj_settype(process_obj, JOVE_OBJ_PROCESS);
        obj_setvalue(process_obj, (uintptr_t)s_init_pcb);

        proc->pcb_obj = obj_release(process_obj);
        proc->pcb = s_init_pcb;
    }

    __asm__ volatile("\
            movq %0, %%rsp; \
            movq $0, %%rbp; \
            jmpq %1"::
            "r"(s_init_pcb->ksp), "r"(process_main));
}