summaryrefslogtreecommitdiffstats
path: root/syscall/invoke-untyped.c
blob: 6b9199fd0ddebc1ce554426f273bf7e46a96c30f (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
#include "handles.h"
#include "syscall.h"
#include "error.h"
#include "memory.h"
#include "print.h"

static int
s_handle_invoke_untyped_size(
        objdir_t *root_dir, 
        objdir_entry_t *target, 
        uint8_t *payload,
        size_t payload_at
        )
{
    size_t *dest;
    SYSCALL_PAYLOAD_TAKEP(payload, payload_at, dest, size_t);

    size_t *untyped = ko_entry_data(target);
#ifdef DBG_SYSCALL
    klogf("root_dir %p target %p data %p\n", root_dir, target, untyped);
#endif
    *dest = *untyped;
    return 0;
}

static int
s_handle_invoke_untyped_alignment(
        objdir_t *root_dir, 
        objdir_entry_t *target, 
        uint8_t *payload,
        size_t payload_at
        )
{
    size_t *dest;
    SYSCALL_PAYLOAD_TAKEP(payload, payload_at, dest, size_t);
#ifdef DBG_SYSCALL
    klogf("root_dir %p target %p data %p\n", root_dir, target, target->data);
#endif

    *dest = target->data & 0xFFF;
    return 0;
}

static int
s_handle_invoke_untyped_split(
        objdir_t *root_dir, 
        objdir_entry_t *target, 
        uint8_t *payload,
        size_t payload_at
        )
{
    size_t dest_pathw;
    size_t dest_bytes;
    objdir_entry_t *dest_entry;

    SYSCALL_PAYLOAD_TAKEOBJ(payload, payload_at, dest_pathw, dest_entry);
    if(dest_entry->type != KO_NONE) return -KE_BADOBJ;

    size_t *untyped = (size_t*)target->data;
    size_t untyped_size = *untyped;

    SYSCALL_PAYLOAD_TAKEL(payload, payload_at, dest_bytes, size_t);
    if(untyped_size - sizeof(size_t) <= dest_bytes) return -KE_TOOSMALL;

    size_t *split = (size_t*)(((uintptr_t)untyped) + untyped_size - dest_bytes);
    *untyped -= dest_bytes;
    *split = dest_bytes;

    *dest_entry = (objdir_entry_t) {
        .type = KO_MEMORY_UNTYPED,
        .data = (uintptr_t)split
    };
    return 0;
}

static int (*s_invoke_handles[])(objdir_t*, objdir_entry_t*, uint8_t*, size_t) = {
    [INVOKE_UNTYPED_SIZE] = s_handle_invoke_untyped_size,
    [INVOKE_UNTYPED_SPLIT] = s_handle_invoke_untyped_split,

    [INVOKE_UNTYPED_ALIGNMENT] = s_handle_invoke_untyped_alignment
};
static size_t s_invoke_handles_count = sizeof(s_invoke_handles) / sizeof(void*);

int
syscall_handle_invoke_untyped(
        objdir_t *root_dir, 
        objdir_entry_t *target, 
        uint8_t *payload, 
        size_t payload_at)
{
    uint8_t funcid;
    SYSCALL_PAYLOAD_TAKEL(payload, payload_at, funcid, uint8_t);

    if(funcid >= s_invoke_handles_count) return -KE_BADFUNC;
    return s_invoke_handles[funcid](root_dir, target, payload, payload_at);
}