aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / ipc.h
| 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.h -- capability tables and typed bounded IPC for the C kernel core. |
| 18 | * Port of crates/aienos-kernel/src/caps.rs (CapTable: generation-checked |
| 19 | * handles, attenuating derive, cross-table derive, tombstones, cascading |
| 20 | * revoke) and crates/aienos-kernel/src/ipc.rs (bounded FIFO channel of fixed |
| 21 | * 56-byte messages, two principals, a four-word object reachable only through |
| 22 | * capabilities). Pure logic, no hardware: host-tested in tests/test_ipc.c; |
| 23 | * the EL0 IPC demo (core/m3.c) drives it from SVC calls. */ |
| 24 | #ifndef AIENOS_CK_IPC_H |
| 25 | #define AIENOS_CK_IPC_H |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | /* Rights (caps.rs Rights; the same bits are the syscall wire form). */ |
| 30 | #define CK_R_READ 0x01u |
| 31 | #define CK_R_WRITE 0x02u |
| 32 | #define CK_R_MAP 0x04u |
| 33 | #define CK_R_GRANT 0x08u |
| 34 | #define CK_R_DERIVE 0x10u |
| 35 | #define CK_R_REVOKE 0x20u |
| 36 | #define CK_R_ALL 0x3fu |
| 37 | |
| 38 | enum { |
| 39 | CK_CAP_OK = 0, |
| 40 | CK_CAP_FULL = -1, |
| 41 | CK_CAP_INVALID = -2, |
| 42 | CK_CAP_MISSING_RIGHTS = -3, |
| 43 | CK_CAP_ESCALATION = -4, |
| 44 | CK_CAP_DUPLICATE_TABLE = -5, |
| 45 | }; |
| 46 | |
| 47 | struct ck_handle { |
| 48 | uint32_t index, generation; |
| 49 | }; |
| 50 | /* Register form: generation << 32 | index (abi.rs Handle::to_raw). */ |
| 51 | static inline uint64_t ck_handle_raw(struct ck_handle h) |
| 52 | { |
| 53 | return ((uint64_t)h.generation << 32) | h.index; |
| 54 | } |
| 55 | /* Fails closed (-1) on generation 0. */ |
| 56 | static inline int ck_handle_from_raw(uint64_t raw, struct ck_handle *h) |
| 57 | { |
| 58 | if ((raw >> 32) == 0) |
| 59 | return -1; |
| 60 | h->index = (uint32_t)raw; |
| 61 | h->generation = (uint32_t)(raw >> 32); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | #define CK_CAP_SLOTS 8 |
| 66 | struct ck_cap_slot { |
| 67 | uint8_t live, rights, derived, tombstone, has_parent; |
| 68 | uint32_t generation, resource; |
| 69 | uint32_t parent_table; |
| 70 | struct ck_handle parent; |
| 71 | }; |
| 72 | struct ck_cap_table { |
| 73 | uint32_t id; |
| 74 | unsigned n; /* usable slots, 1..CK_CAP_SLOTS */ |
| 75 | struct ck_cap_slot slots[CK_CAP_SLOTS]; |
| 76 | uint32_t generations[CK_CAP_SLOTS]; |
| 77 | }; |
| 78 | |
| 79 | void ck_cap_init(struct ck_cap_table *t, uint32_t id, unsigned n); |
| 80 | int ck_cap_insert(struct ck_cap_table *t, uint32_t resource, unsigned rights, struct ck_handle *out); |
| 81 | /* 0 and *resource if the handle is live and holds every required right. */ |
| 82 | int ck_cap_lookup(const struct ck_cap_table *t, struct ck_handle h, unsigned required, |
| 83 | uint32_t *resource); |
| 84 | int ck_cap_rights(const struct ck_cap_table *t, struct ck_handle h, unsigned *rights); |
| 85 | /* A derived-from capability becomes a tombstone so revoke still reaches its |
| 86 | * children; others free their slot. */ |
| 87 | int ck_cap_remove(struct ck_cap_table *t, struct ck_handle h); |
| 88 | int ck_cap_derive(struct ck_cap_table *t, struct ck_handle h, unsigned rights, struct ck_handle *out); |
| 89 | int ck_cap_derive_into(struct ck_cap_table *from, struct ck_handle h, struct ck_cap_table *to, |
| 90 | unsigned rights, struct ck_handle *out); |
| 91 | int ck_cap_transfer(struct ck_cap_table *from, struct ck_handle h, struct ck_cap_table *to, |
| 92 | struct ck_handle *out); |
| 93 | /* Needs REVOKE on (table_id, h); removes it and every descendant in tables, |
| 94 | * deepest first. Table ids must be unique among tables. */ |
| 95 | int ck_cap_revoke(uint32_t table_id, struct ck_handle h, struct ck_cap_table *const *tables, |
| 96 | unsigned ntables); |
| 97 | |
| 98 | /* ---- typed messages and the bounded channel (ipc.rs) ---- */ |
| 99 | struct ck_msg { |
| 100 | uint32_t kind; |
| 101 | uint32_t reserved; |
| 102 | uint64_t payload[3]; |
| 103 | uint64_t region_base; |
| 104 | uint32_t region_pages; |
| 105 | uint32_t reserved2; |
| 106 | uint64_t object; |
| 107 | }; |
| 108 | _Static_assert(sizeof(struct ck_msg) == 56, "message is 56 bytes like abi.rs Message"); |
| 109 | |
| 110 | #define CK_CHAN_CAP 8 |
| 111 | struct ck_channel { |
| 112 | struct ck_msg ring[CK_CHAN_CAP]; |
| 113 | unsigned head, len, cap; /* cap: 1..CK_CHAN_CAP */ |
| 114 | }; |
| 115 | void ck_chan_init(struct ck_channel *c, unsigned cap); |
| 116 | int ck_chan_send(struct ck_channel *c, const struct ck_msg *m); /* -1 full */ |
| 117 | int ck_chan_recv(struct ck_channel *c, struct ck_msg *m); /* -1 empty */ |
| 118 | |
| 119 | enum { |
| 120 | CK_IPC_OK = 0, |
| 121 | CK_IPC_CHANNEL_FULL = -1, |
| 122 | CK_IPC_CHANNEL_EMPTY = -2, |
| 123 | CK_IPC_MISSING_RIGHTS = -3, |
| 124 | CK_IPC_INVALID_HANDLE = -4, |
| 125 | CK_IPC_WRONG_RESOURCE = -5, |
| 126 | CK_IPC_DELEGATION_FAILED = -6, |
| 127 | }; |
| 128 | |
| 129 | #define CK_IPC_CHANNEL_RESOURCE 0x49504348u /* "IPCH" */ |
| 130 | #define CK_IPC_OBJECT_RESOURCE 0x49504f42u /* "IPOB" */ |
| 131 | #define CK_IPC_PRINCIPAL_A 0x45000041u |
| 132 | #define CK_IPC_PRINCIPAL_B 0x45000042u |
| 133 | #define CK_IPC_OBJECT_SLOTS 4 |
| 134 | |
| 135 | struct ck_ipc { |
| 136 | struct ck_channel chan; |
| 137 | uint64_t object[CK_IPC_OBJECT_SLOTS]; |
| 138 | struct ck_cap_table tables[2]; /* principal A, principal B */ |
| 139 | unsigned active; /* index of the running principal */ |
| 140 | }; |
| 141 | |
| 142 | /* Fresh state: A holds send (WRITE on the channel) and the object (READ, |
| 143 | * WRITE, DERIVE, REVOKE); B holds receive (READ on the channel). A active. */ |
| 144 | void ck_ipc_reset(struct ck_ipc *ipc, struct ck_handle *a_send, struct ck_handle *a_object, |
| 145 | struct ck_handle *b_recv); |
| 146 | int ck_ipc_send(struct ck_ipc *ipc, unsigned principal, struct ck_handle h, const struct ck_msg *m); |
| 147 | int ck_ipc_recv(struct ck_ipc *ipc, unsigned principal, struct ck_handle h, struct ck_msg *m); |
| 148 | /* Delegates the object from principal `from` to `to` with rights that must |
| 149 | * be a subset of READ|WRITE (and of the source rights). */ |
| 150 | int ck_ipc_delegate(struct ck_ipc *ipc, unsigned from, struct ck_handle h, unsigned to, |
| 151 | unsigned rights, struct ck_handle *child); |
| 152 | int ck_ipc_object_read(struct ck_ipc *ipc, unsigned principal, struct ck_handle h, |
| 153 | uint64_t offset, uint64_t *value); |
| 154 | int ck_ipc_object_write(struct ck_ipc *ipc, unsigned principal, struct ck_handle h, |
| 155 | uint64_t offset, uint64_t value); |
| 156 | /* Revokes (owner principal index, h) across both tables. */ |
| 157 | int ck_ipc_revoke(struct ck_ipc *ipc, unsigned owner, struct ck_handle h); |
| 158 | |
| 159 | #endif |