summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/proc/process.c
blob: 616558595d5f4f6a0fc15d9ddb5fb62edd71e209 (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
#include "process.h"
#include "proc/process.h"
#include "arch/x86_64/hal/obj.h"
#include "arch/x86_64/hal/smp.h"
#include "arch/x86_64/hal/paging.h"
#include "hal/obj.h"
#include "hal/smp.h"
#include "lib/string.h"
#include "init.h"
#include "jove.h"

PAGEALIGN uint8_t s_init_iopb[0x2000];
PAGEALIGN char _init_pcb_bytes[0x1000] = { 0 };
pcb_t *pcb_tree;

void
processing_setup(void (*process_main))
{
    pcb_t *init_pcb = (pcb_t*)&_init_pcb_bytes;
    pcb_tree = init_pcb;

    memset(s_init_iopb, 0xFF, 0x2000);
    uintptr_t ksp = ((uintptr_t)&_init_pcb_bytes) + 0xFF8;

    init_pcb->header.tree_node.get_key = (void*)pcb_getpid;
    init_pcb->header.tree_node.color = 1;
    init_pcb->header.rootdir = _rootdir_obj;
    init_pcb->header.pagedir = obj_release(objdir_getlocal(_rootdir_obj, ROOTDIR_PAGEDIR_I));

    init_pcb->header.pid = 1;
    init_pcb->ksp = (void*)ksp;

    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);
    init_pcb->iopb = obj_release(iopb_obj);

    processor_t *proc = processor_current();
    proc->tss.rsp[0] = (uintptr_t)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_PCB);
        obj_setvalue(process_obj, (uintptr_t)init_pcb);

        proc->pcb_obj = process_obj;
        proc->pcb = init_pcb;
    }

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