interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / core / ipc.c

352 lines · 12374 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* ipc.c -- see ipc.h. caps.rs and ipc.rs logic, step for step. */
18#include "ipc.h"
19
20void ck_cap_init(struct ck_cap_table *t, uint32_t id, unsigned n)
21{
22 t->id = id;
23 t->n = n == 0 ? 1 : (n > CK_CAP_SLOTS ? CK_CAP_SLOTS : n);
24 for (unsigned i = 0; i < CK_CAP_SLOTS; i++) {
25 t->slots[i] = (struct ck_cap_slot){ 0 };
26 t->generations[i] = 0;
27 }
28}
29
30static int allocate(struct ck_cap_table *t, uint32_t resource, unsigned rights, int has_parent,
31 uint32_t parent_table, struct ck_handle parent, struct ck_handle *out)
32{
33 for (unsigned i = 0; i < t->n; i++) {
34 /* A slot whose generation reached the maximum is retired for good, so
35 * no old handle can ever match again. */
36 if (t->slots[i].live || t->generations[i] == 0xffffffffu)
37 continue;
38 uint32_t gen = t->generations[i] + 1;
39 t->generations[i] = gen;
40 t->slots[i] = (struct ck_cap_slot){ .live = 1, .rights = (uint8_t)rights,
41 .has_parent = (uint8_t)has_parent,
42 .generation = gen, .resource = resource,
43 .parent_table = parent_table, .parent = parent };
44 out->index = i;
45 out->generation = gen;
46 return CK_CAP_OK;
47 }
48 return CK_CAP_FULL;
49}
50
51int ck_cap_insert(struct ck_cap_table *t, uint32_t resource, unsigned rights, struct ck_handle *out)
52{
53 if (rights & ~CK_R_ALL)
54 return CK_CAP_ESCALATION;
55 return allocate(t, resource, rights, 0, 0, (struct ck_handle){ 0, 0 }, out);
56}
57
58/* Live slot for the handle, tombstones included (revocation walks them). */
59static const struct ck_cap_slot *slot_any(const struct ck_cap_table *t, struct ck_handle h)
60{
61 if (h.index >= t->n)
62 return 0;
63 const struct ck_cap_slot *s = &t->slots[h.index];
64 return s->live && s->generation == h.generation ? s : 0;
65}
66
67static const struct ck_cap_slot *slot_ok(const struct ck_cap_table *t, struct ck_handle h)
68{
69 const struct ck_cap_slot *s = slot_any(t, h);
70 return s && !s->tombstone ? s : 0;
71}
72
73int ck_cap_lookup(const struct ck_cap_table *t, struct ck_handle h, unsigned required,
74 uint32_t *resource)
75{
76 const struct ck_cap_slot *s = slot_ok(t, h);
77 if (!s)
78 return CK_CAP_INVALID;
79 if ((s->rights & required) != required)
80 return CK_CAP_MISSING_RIGHTS;
81 if (resource)
82 *resource = s->resource;
83 return CK_CAP_OK;
84}
85
86int ck_cap_rights(const struct ck_cap_table *t, struct ck_handle h, unsigned *rights)
87{
88 const struct ck_cap_slot *s = slot_ok(t, h);
89 if (!s)
90 return CK_CAP_INVALID;
91 *rights = s->rights;
92 return CK_CAP_OK;
93}
94
95int ck_cap_remove(struct ck_cap_table *t, struct ck_handle h)
96{
97 const struct ck_cap_slot *s = slot_ok(t, h);
98 if (!s)
99 return CK_CAP_INVALID;
100 if (s->derived)
101 t->slots[h.index].tombstone = 1;
102 else
103 t->slots[h.index] = (struct ck_cap_slot){ 0 };
104 return CK_CAP_OK;
105}
106
107int ck_cap_derive_into(struct ck_cap_table *from, struct ck_handle h, struct ck_cap_table *to,
108 unsigned rights, struct ck_handle *out)
109{
110 const struct ck_cap_slot *s = slot_ok(from, h);
111 if (!s)
112 return CK_CAP_INVALID;
113 if (!(s->rights & CK_R_DERIVE))
114 return CK_CAP_MISSING_RIGHTS;
115 if (rights & ~(unsigned)s->rights)
116 return CK_CAP_ESCALATION;
117 int r = allocate(to, s->resource, rights, 1, from->id, h, out);
118 if (r)
119 return r;
120 from->slots[h.index].derived = 1;
121 return CK_CAP_OK;
122}
123
124int ck_cap_derive(struct ck_cap_table *t, struct ck_handle h, unsigned rights, struct ck_handle *out)
125{
126 return ck_cap_derive_into(t, h, t, rights, out);
127}
128
129int ck_cap_transfer(struct ck_cap_table *from, struct ck_handle h, struct ck_cap_table *to,
130 struct ck_handle *out)
131{
132 const struct ck_cap_slot *s = slot_ok(from, h);
133 if (!s)
134 return CK_CAP_INVALID;
135 if (!(s->rights & CK_R_GRANT))
136 return CK_CAP_MISSING_RIGHTS;
137 return allocate(to, s->resource, s->rights, 0, 0, (struct ck_handle){ 0, 0 }, out);
138}
139
140static struct ck_cap_table *table_by_id(struct ck_cap_table *const *tables, unsigned n, uint32_t id)
141{
142 for (unsigned i = 0; i < n; i++)
143 if (tables[i]->id == id)
144 return tables[i];
145 return 0;
146}
147
148/* Depth of (table, h) below the root along parent links: >= 0, -1 if not a
149 * descendant, DEPTH_CYCLE if the chain is longer than every slot (a cycle). */
150#define DEPTH_CYCLE (-1000)
151static int revocation_depth(uint32_t table, struct ck_handle h, uint32_t root_table,
152 struct ck_handle root, struct ck_cap_table *const *tables, unsigned n,
153 unsigned capacity)
154{
155 for (unsigned depth = 0; depth <= capacity; depth++) {
156 if (table == root_table && h.index == root.index && h.generation == root.generation)
157 return (int)depth;
158 const struct ck_cap_table *t = table_by_id(tables, n, table);
159 if (!t)
160 return -1;
161 const struct ck_cap_slot *s = slot_any(t, h);
162 if (!s || !s->has_parent)
163 return -1;
164 table = s->parent_table;
165 h = s->parent;
166 }
167 return DEPTH_CYCLE;
168}
169
170int ck_cap_revoke(uint32_t table_id, struct ck_handle h, struct ck_cap_table *const *tables,
171 unsigned ntables)
172{
173 unsigned matches = 0;
174 for (unsigned i = 0; i < ntables; i++)
175 matches += tables[i]->id == table_id;
176 if (matches != 1)
177 return CK_CAP_DUPLICATE_TABLE;
178 const struct ck_cap_slot *rs = slot_ok(table_by_id(tables, ntables, table_id), h);
179 if (!rs)
180 return CK_CAP_INVALID;
181 if (!(rs->rights & CK_R_REVOKE))
182 return CK_CAP_MISSING_RIGHTS;
183 unsigned capacity = 0;
184 for (unsigned i = 0; i < ntables; i++)
185 capacity += tables[i]->n;
186 /* Validate every chain first: a cycle fails before anything is removed. */
187 for (unsigned i = 0; i < ntables; i++)
188 for (unsigned s = 0; s < tables[i]->n; s++)
189 if (tables[i]->slots[s].live &&
190 revocation_depth(tables[i]->id,
191 (struct ck_handle){ s, tables[i]->slots[s].generation }, table_id,
192 h, tables, ntables, capacity) == DEPTH_CYCLE)
193 return CK_CAP_FULL;
194 for (unsigned round = 0; round < capacity; round++) {
195 int best = -1;
196 struct ck_cap_table *bt = 0;
197 unsigned bs = 0;
198 for (unsigned i = 0; i < ntables; i++)
199 for (unsigned s = 0; s < tables[i]->n; s++) {
200 if (!tables[i]->slots[s].live)
201 continue;
202 int d = revocation_depth(tables[i]->id,
203 (struct ck_handle){ s, tables[i]->slots[s].generation },
204 table_id, h, tables, ntables, capacity);
205 if (d > best) {
206 best = d;
207 bt = tables[i];
208 bs = s;
209 }
210 }
211 if (best < 0)
212 break;
213 bt->slots[bs] = (struct ck_cap_slot){ 0 };
214 }
215 return CK_CAP_OK;
216}
217
218/* ---- channel ---- */
219void ck_chan_init(struct ck_channel *c, unsigned cap)
220{
221 c->head = c->len = 0;
222 c->cap = cap == 0 ? 1 : (cap > CK_CHAN_CAP ? CK_CHAN_CAP : cap);
223}
224
225int ck_chan_send(struct ck_channel *c, const struct ck_msg *m)
226{
227 if (c->len == c->cap)
228 return -1;
229 c->ring[(c->head + c->len) % c->cap] = *m;
230 c->len++;
231 return 0;
232}
233
234int ck_chan_recv(struct ck_channel *c, struct ck_msg *m)
235{
236 if (c->len == 0)
237 return -1;
238 *m = c->ring[c->head];
239 c->head = (c->head + 1) % c->cap;
240 c->len--;
241 return 0;
242}
243
244/* ---- ipc ---- */
245void ck_ipc_reset(struct ck_ipc *ipc, struct ck_handle *a_send, struct ck_handle *a_object,
246 struct ck_handle *b_recv)
247{
248 ck_chan_init(&ipc->chan, CK_CHAN_CAP);
249 ipc->object[0] = 0xc0ffee01;
250 ipc->object[1] = 0x0badc0de;
251 ipc->object[2] = 0x12345678;
252 ipc->object[3] = 0x9abcdef0;
253 ck_cap_init(&ipc->tables[0], CK_IPC_PRINCIPAL_A, CK_CAP_SLOTS);
254 ck_cap_init(&ipc->tables[1], CK_IPC_PRINCIPAL_B, CK_CAP_SLOTS);
255 ipc->active = 0;
256 ck_cap_insert(&ipc->tables[0], CK_IPC_CHANNEL_RESOURCE, CK_R_WRITE, a_send);
257 ck_cap_insert(&ipc->tables[0], CK_IPC_OBJECT_RESOURCE,
258 CK_R_READ | CK_R_WRITE | CK_R_DERIVE | CK_R_REVOKE, a_object);
259 ck_cap_insert(&ipc->tables[1], CK_IPC_CHANNEL_RESOURCE, CK_R_READ, b_recv);
260}
261
262static int gated(const struct ck_cap_table *t, struct ck_handle h, unsigned right, uint32_t resource)
263{
264 uint32_t found;
265 int r = ck_cap_lookup(t, h, right, &found);
266 if (r == CK_CAP_OK)
267 return found == resource ? CK_IPC_OK : CK_IPC_WRONG_RESOURCE;
268 return r == CK_CAP_MISSING_RIGHTS ? CK_IPC_MISSING_RIGHTS : CK_IPC_INVALID_HANDLE;
269}
270
271int ck_ipc_send(struct ck_ipc *ipc, unsigned p, struct ck_handle h, const struct ck_msg *m)
272{
273 if (p > 1)
274 return CK_IPC_INVALID_HANDLE;
275 int r = gated(&ipc->tables[p], h, CK_R_WRITE, CK_IPC_CHANNEL_RESOURCE);
276 if (r)
277 return r;
278 return ck_chan_send(&ipc->chan, m) ? CK_IPC_CHANNEL_FULL : CK_IPC_OK;
279}
280
281int ck_ipc_recv(struct ck_ipc *ipc, unsigned p, struct ck_handle h, struct ck_msg *m)
282{
283 if (p > 1)
284 return CK_IPC_INVALID_HANDLE;
285 int r = gated(&ipc->tables[p], h, CK_R_READ, CK_IPC_CHANNEL_RESOURCE);
286 if (r)
287 return r;
288 return ck_chan_recv(&ipc->chan, m) ? CK_IPC_CHANNEL_EMPTY : CK_IPC_OK;
289}
290
291int ck_ipc_delegate(struct ck_ipc *ipc, unsigned from, struct ck_handle h, unsigned to,
292 unsigned rights, struct ck_handle *child)
293{
294 if (from > 1 || to > 1 || from == to)
295 return CK_IPC_INVALID_HANDLE;
296 int r = gated(&ipc->tables[from], h, CK_R_READ, CK_IPC_OBJECT_RESOURCE);
297 if (r)
298 return r;
299 if (rights & ~(CK_R_READ | CK_R_WRITE))
300 return CK_IPC_MISSING_RIGHTS;
301 r = ck_cap_derive_into(&ipc->tables[from], h, &ipc->tables[to], rights, child);
302 switch (r) {
303 case CK_CAP_OK: return CK_IPC_OK;
304 case CK_CAP_FULL: return CK_IPC_DELEGATION_FAILED;
305 case CK_CAP_MISSING_RIGHTS:
306 case CK_CAP_ESCALATION: return CK_IPC_MISSING_RIGHTS;
307 default: return CK_IPC_INVALID_HANDLE;
308 }
309}
310
311int ck_ipc_object_read(struct ck_ipc *ipc, unsigned p, struct ck_handle h, uint64_t offset,
312 uint64_t *value)
313{
314 if (p > 1)
315 return CK_IPC_INVALID_HANDLE;
316 int r = gated(&ipc->tables[p], h, CK_R_READ, CK_IPC_OBJECT_RESOURCE);
317 if (r)
318 return r;
319 if (offset >= CK_IPC_OBJECT_SLOTS)
320 return CK_IPC_WRONG_RESOURCE;
321 *value = ipc->object[offset];
322 return CK_IPC_OK;
323}
324
325int ck_ipc_object_write(struct ck_ipc *ipc, unsigned p, struct ck_handle h, uint64_t offset,
326 uint64_t value)
327{
328 if (p > 1)
329 return CK_IPC_INVALID_HANDLE;
330 int r = gated(&ipc->tables[p], h, CK_R_WRITE, CK_IPC_OBJECT_RESOURCE);
331 if (r)
332 return r;
333 if (offset >= CK_IPC_OBJECT_SLOTS)
334 return CK_IPC_WRONG_RESOURCE;
335 ipc->object[offset] = value;
336 return CK_IPC_OK;
337}
338
339int ck_ipc_revoke(struct ck_ipc *ipc, unsigned owner, struct ck_handle h)
340{
341 if (owner > 1)
342 return CK_IPC_INVALID_HANDLE;
343 struct ck_cap_table *const tables[2] = { &ipc->tables[0], &ipc->tables[1] };
344 int r = ck_cap_revoke(ipc->tables[owner].id, h, tables, 2);
345 switch (r) {
346 case CK_CAP_OK: return CK_IPC_OK;
347 case CK_CAP_FULL: return CK_IPC_DELEGATION_FAILED;
348 case CK_CAP_MISSING_RIGHTS:
349 case CK_CAP_ESCALATION: return CK_IPC_MISSING_RIGHTS;
350 default: return CK_IPC_INVALID_HANDLE;
351 }
352}