summaryrefslogtreecommitdiffstats
path: root/apps/init/arch/x86_64/paging.c
blob: 6c1c3866fe99ace30ff75e5aad61bcf66d2c531f (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "../../memory.h"
#include "jove/object-dir.h"
#include "jove/jove.h"
#include <jove/arch/x86_64/object-pagemap.h>
#include <stdbool.h>

KernelObjectPageMap init_pagemap;
unsigned d_cache[3] = { -1, -1, -1 };
uintptr_t vptr_cache = 0;

KernelObjectUntyped work_page;

#define PMLI_SHL(d) (((4 - d) * 9) + 3)
#define PMLI_DL(v, d) ((v >> PMLI_SHL(d)) % 512)

uint64_t
pager_write_path(uintptr_t vptr, uint8_t depth)
{
    uint64_t r = 0;
    if(depth >= 4) return -1;

    uint16_t *path = (uint16_t*)&r;
    for(uint8_t i = 0; i < depth; i++) path[i] = PMLI_DL(vptr, i);
    return r;
}

bool
pager_depth_exists(uintptr_t vptr, uint8_t depth)
{
    uint64_t path = pager_write_path(vptr, depth);
    if(path == -1) return false;

    return jove_pagemap_exists(&init_pagemap, depth, (uint16_t*)&path);
}
/* Places new page in work_page. */
JoveError
pager_alloc_page_untyped(void)
{
    uint8_t lastmemb = jove_objdir_lastmemb(&untypedDirectory);
    if(jove_errno) {
        JoveError err = jove_errno;
        jove_errno = EJOVE_OK;
        return err;
    }
    
    KernelObjectUntyped last_untyped;
    _jove_alloc_untyped_inplace(&last_untyped, &untypedDirectory, lastmemb);
    
    if(jove_untyped_size(&last_untyped) == 0x1000) {
        jove_objdir_move(&untypedDirectory, lastmemb, &__rootdir, work_page.typed.membi);
        return EJOVE_OK;
    }

    return jove_untyped_split_inplace(&last_untyped, 0x1000, &work_page);
}

JoveError
pager_ensure_at_depth(uint8_t depth, uint16_t *path)
{
    int exists = jove_pagemap_exists(&init_pagemap, depth, path);
    if(exists) {
        return EJOVE_OK;
    }

    JoveError pagealloc_err = pager_alloc_page_untyped();
    if(pagealloc_err) {
        jove_kprintf("Failed to allocate page with error %i\n", pagealloc_err);
        return pagealloc_err;
    }
    return jove_pagemap_map(&init_pagemap, depth, path, &work_page);
}

#define PAGER_CACHE_ENSURE_DEPTH(path_seg, d) \
    if(path_seg[d] != d_cache[d]) { \
        JoveError d_err = pager_ensure_at_depth(d, path_seg); \
        if(d_err) return d_err; \
        d_cache[d] = path_seg[d]; \
    }

JoveError
pager_ensure(uintptr_t vptr)
{
    uint64_t path = pager_write_path(vptr, 3);
    if(path == -1) return EJOVE_BADARG;

    vptr &= ~0xFFFULL;
    uint16_t *path_seg = (uint16_t*)&path;
    jove_kprintf("%x:%x:%x:%x\n", path_seg[0], path_seg[1], path_seg[2], path_seg[3]);

    PAGER_CACHE_ENSURE_DEPTH(path_seg, 0);
    PAGER_CACHE_ENSURE_DEPTH(path_seg, 1);
    PAGER_CACHE_ENSURE_DEPTH(path_seg, 2);
    if(vptr != vptr_cache) {
        return pager_ensure_at_depth(3, path_seg);
    }
    return EJOVE_OK;
}

void
pager_setup(void)
{
    _jove_alloc_objdir_inplace(&untypedDirectory, &__rootdir, INIT_OBJECT_UNTYPED_DIR);
    _jove_alloc_pagemap_inplace(&init_pagemap, &__rootdir, INIT_OBJECT_PAGEMAP);

    size_t lastfree = jove_objdir_lastmemb(&__rootdir) + 1;
    _jove_alloc_untyped_inplace(&work_page, &__rootdir, lastfree++);

    pager_ensure(0x00000000F0000000);
}