summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/hal/obj.c
blob: 103b53d06e872fde4a72d66c341b121b3f533f1c (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "obj.h"
#include "arch/x86_64/lock.h"
#include "lib/printf.h"
#include "paging.h"
#include "hal/obj.h"
#include "jove.h"
#include "sys/link.h"

jove_owned_obj_t*
obj_take(jove_obj_t *obj)
{
    if((uintptr_t)obj < IDENT_VIRT_BASE) {
        reterr(EBADOBJ);
    }
    take_mutex(&obj->lock);
    return (jove_owned_obj_t*)obj;
}

jove_owned_obj_t*
obj_trytake(jove_obj_t *obj)
{
    if((uintptr_t)obj < IDENT_VIRT_BASE) {
        reterr(EBADOBJ);
    }
    if(try_take_mutex(&obj->lock)) {
        reterr(EBUSY);
    }
    return (jove_owned_obj_t*)obj;
}

jove_obj_t*
obj_release(jove_owned_obj_t *obj)
{
    if((uintptr_t)obj < IDENT_VIRT_BASE) {
        reterr(EBADOBJ);
    }
    release_mutex((atomic_flag*)&obj->locked.lock);
    return (jove_obj_t*)obj;
}

void*
obj_deref(jove_owned_obj_t *obj)
{
    return (void*)(((uintptr_t)obj_getvalue(obj)) | 0xFFFF800000000000ULL);
}

uint8_t
obj_gettype(jove_owned_obj_t *obj)
{
    return obj->locked.type;
}

uint8_t
obj_peektype(jove_obj_t obj)
{
    return obj.type;
}

void
obj_settype(jove_owned_obj_t *obj, uint8_t type)
{
    obj->locked.type = type;
}

uintmax_t
obj_getvalue(jove_owned_obj_t *obj)
{
    return obj->locked.value;
}

void
obj_setvalue(jove_owned_obj_t *obj, uintmax_t value)
{
    obj->locked.value = value & 0x00007FFFFFFFFFFFULL;
}

jove_obj_t
objdir_peeklocal(
        jove_owned_obj_t *objdir_obj, 
        uint16_t entry)
{
    if(obj_gettype(objdir_obj) != JOVE_OBJ_OBJDIR) {
        errno = EBADOBJ;
        return (jove_obj_t) { 0 };
    }
    if(entry >= JOVE_OBJDIR_ENTRIES) {
        errno = EBADPATH;
        return (jove_obj_t) { 0 };
    }

    jove_objdir_t *objtab = obj_deref(objdir_obj);
    return objtab->entries[entry];
}

jove_owned_obj_t*
objdir_getlocal(
        jove_owned_obj_t *objdir_obj, 
        uint16_t entry)
{
    if(obj_gettype(objdir_obj) != JOVE_OBJ_OBJDIR) {
        reterr(EBADOBJ);
    }
    if(entry >= JOVE_OBJDIR_ENTRIES) {
        reterr(EBADPATH);
    }

    jove_objdir_t *objtab = obj_deref(objdir_obj);
    return obj_take(&objtab->entries[entry]);
}

jove_owned_obj_t*
objdir_trygetlocal(
        jove_owned_obj_t *objdir_obj, 
        uint16_t entry)
{
    if(obj_gettype(objdir_obj) != JOVE_OBJ_OBJDIR) {
        reterr(EBADOBJ);
    }
    if(entry >= JOVE_OBJDIR_ENTRIES) {
        reterr(EBADPATH);
    }

    jove_objdir_t *objtab = obj_deref(objdir_obj);
    return obj_trytake(&objtab->entries[entry]);
}

jove_owned_obj_t*
objdir_getobj(
        jove_owned_obj_t *objdir_obj, 
        const uint16_t *path, 
        size_t path_len)
{
    errno = 0;
    if(path_len == 0) {
        return objdir_obj;
    }
    {
        jove_owned_obj_t *child_objdir = objdir_getlocal(objdir_obj, path[0]);
        if(child_objdir == 0) {
            reterr(EBADPATH);
        }

        jove_owned_obj_t *target_obj = objdir_getobj(child_objdir, path + 1, path_len - 1);
        obj_release(child_objdir);
        return target_obj;
    }
}

jove_owned_obj_t*
objdir_trygetobj(
        jove_owned_obj_t *objdir_obj, 
        const uint16_t *path, 
        size_t path_len)
{
    errno = 0;
    if(path_len == 0) {
        return objdir_obj;
    }
    {
        jove_owned_obj_t *child_objdir = objdir_trygetlocal(objdir_obj, path[0]);
        if(child_objdir == 0) {
            return 0;
        }

        jove_owned_obj_t *target_obj = objdir_trygetobj(child_objdir, path + 1, path_len - 1);
        obj_release(child_objdir);
        return target_obj;
    }
}