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 / capability / aienos_capability.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 | /* |
| 18 | * aienos_capability.h -- native AIENOS capability authority. |
| 19 | * |
| 20 | * The view can check a reference. The admin can change the table. The |
| 21 | * reaction world is given the view. Cognition has no path to the admin. |
| 22 | * |
| 23 | * Slot rules: a reference is an index plus a generation, a reused slot does |
| 24 | * not honor the old reference, generation is 64 bits and never wraps, a |
| 25 | * restarted table starts above every generation the old table used, |
| 26 | * delegation can only narrow rights, and revoking an ancestor revokes the |
| 27 | * descendants. Each entry carries its subject, epoch, lease, full 64-bit |
| 28 | * resource, and office rights that cannot be delegated. |
| 29 | * |
| 30 | * The declarations below match omega/src/runtime/aienos_cap.h in name and |
| 31 | * layout, so the reaction world links this file without change. |
| 32 | */ |
| 33 | #ifndef AIENOS_CAPABILITY_H |
| 34 | #define AIENOS_CAPABILITY_H |
| 35 | |
| 36 | #include <stdint.h> |
| 37 | |
| 38 | #define AIENOS_CAP_MAX 256u |
| 39 | #define AIENOS_CAP_MAX_DEPTH 8u |
| 40 | #define AIENOS_CAP_TOKEN_LEN 32u |
| 41 | #define AIENOS_CAP_RES_AUTHORITY 0ull |
| 42 | |
| 43 | #define AIENOS_CAP_OK 0 |
| 44 | #define AIENOS_CAP_ERR_BOUNDS (-1) |
| 45 | #define AIENOS_CAP_ERR_STALE_GEN (-2) |
| 46 | #define AIENOS_CAP_ERR_REVOKED (-3) |
| 47 | #define AIENOS_CAP_ERR_EPOCH (-4) |
| 48 | #define AIENOS_CAP_ERR_SUBJECT (-5) |
| 49 | #define AIENOS_CAP_ERR_RESOURCE (-6) |
| 50 | #define AIENOS_CAP_ERR_RIGHTS (-7) |
| 51 | #define AIENOS_CAP_ERR_EXPIRED (-8) |
| 52 | #define AIENOS_CAP_ERR_CHAIN (-9) |
| 53 | #define AIENOS_CAP_ERR_AMPLIFY (-10) |
| 54 | #define AIENOS_CAP_ERR_NOT_DELEGABLE (-11) |
| 55 | #define AIENOS_CAP_ERR_FULL (-12) |
| 56 | #define AIENOS_CAP_ERR_IO (-13) |
| 57 | #define AIENOS_CAP_ERR_STATE (-14) |
| 58 | #define AIENOS_CAP_ERR_UNAUTHORIZED (-15) |
| 59 | #define AIENOS_CAP_ERR_OVERFLOW (-16) |
| 60 | #define AIENOS_CAP_ERR_EXHAUSTED (-17) |
| 61 | |
| 62 | #define AIENOS_CAP_RIGHT_READ 0x1u |
| 63 | #define AIENOS_CAP_RIGHT_WRITE 0x2u |
| 64 | #define AIENOS_CAP_RIGHT_EFFECT 0x4u |
| 65 | #define AIENOS_CAP_RIGHT_DELEGATE 0x8u |
| 66 | #define AIENOS_CAP_RIGHT_MINT 0x10u |
| 67 | #define AIENOS_CAP_RIGHT_REVOKE 0x20u |
| 68 | #define AIENOS_CAP_RIGHT_RECLAIM 0x40u |
| 69 | #define AIENOS_CAP_RIGHT_EPOCH 0x80u |
| 70 | #define AIENOS_CAP_RIGHT_CLOCK 0x100u |
| 71 | /* Authorizes a lineage transition. Intelligence may propose the next |
| 72 | * generation. Only a holder of this right may promote it, and the right |
| 73 | * cannot be handed on. */ |
| 74 | #define AIENOS_CAP_RIGHT_PROMOTE 0x200u |
| 75 | #define AIENOS_CAP_RIGHT_PRIVILEGED \ |
| 76 | (AIENOS_CAP_RIGHT_MINT | AIENOS_CAP_RIGHT_REVOKE | AIENOS_CAP_RIGHT_RECLAIM | \ |
| 77 | AIENOS_CAP_RIGHT_EPOCH | AIENOS_CAP_RIGHT_CLOCK | AIENOS_CAP_RIGHT_PROMOTE) |
| 78 | #define AIENOS_CAP_RIGHT_KNOWN \ |
| 79 | (AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE | AIENOS_CAP_RIGHT_EFFECT | \ |
| 80 | AIENOS_CAP_RIGHT_DELEGATE | AIENOS_CAP_RIGHT_PRIVILEGED) |
| 81 | |
| 82 | #define AIENOS_CAP_STATE_FREE 0u |
| 83 | #define AIENOS_CAP_STATE_LIVE 1u |
| 84 | #define AIENOS_CAP_STATE_REVOKED 2u |
| 85 | |
| 86 | #define AIENOS_CAP_PARENT_NONE UINT32_MAX |
| 87 | |
| 88 | /* Width of every generation field in this ABI (AienosCapRef.generation, |
| 89 | * AienosCapEntry.generation/parent_generation/minted_by_generation). This |
| 90 | * library is the production capability ABI; aienos_capability.c pins the |
| 91 | * struct layouts below with _Static_assert. The Rust crates |
| 92 | * aienos-capability and aienos-capability-ffi are a separate 32-bit |
| 93 | * implementation that exports aienos_cap32_* symbols and is not |
| 94 | * interchangeable with this one. */ |
| 95 | #define AIENOS_CAP_GENERATION_BITS 64 |
| 96 | |
| 97 | typedef struct AienosCapAdmin AienosCapAdmin; |
| 98 | typedef struct AienosCapView AienosCapView; |
| 99 | |
| 100 | typedef struct { |
| 101 | uint32_t cap_id; |
| 102 | uint64_t generation; |
| 103 | } AienosCapRef; |
| 104 | |
| 105 | typedef struct { |
| 106 | uint32_t cap_id; |
| 107 | uint64_t generation; |
| 108 | uint32_t state; |
| 109 | uint32_t issuer; |
| 110 | uint32_t subject; |
| 111 | uint32_t rights; |
| 112 | uint64_t resource; |
| 113 | uint64_t epoch; |
| 114 | uint64_t lease_expiry; |
| 115 | uint32_t parent_id; |
| 116 | uint64_t parent_generation; |
| 117 | uint32_t minted_by_id; |
| 118 | uint64_t minted_by_generation; |
| 119 | } AienosCapEntry; |
| 120 | |
| 121 | typedef struct { |
| 122 | uint32_t issuer; |
| 123 | uint32_t subject; |
| 124 | uint64_t resource; |
| 125 | uint32_t rights; |
| 126 | uint64_t lease_ticks; |
| 127 | AienosCapRef parent; |
| 128 | AienosCapRef authority; |
| 129 | } AienosCapMint; |
| 130 | |
| 131 | int aienos_cap_start(AienosCapAdmin **admin, AienosCapView **view); |
| 132 | void aienos_cap_stop(AienosCapAdmin *admin, AienosCapView *view); |
| 133 | int aienos_cap_office(const AienosCapAdmin *admin, AienosCapRef *out); |
| 134 | int aienos_cap_mint(AienosCapAdmin *admin, const AienosCapMint *request, AienosCapRef *out); |
| 135 | int aienos_cap_revoke(AienosCapAdmin *admin, AienosCapRef authority, AienosCapRef target); |
| 136 | int aienos_cap_reclaim(AienosCapAdmin *admin, AienosCapRef authority, uint32_t cap_id); |
| 137 | int aienos_cap_advance_clock(AienosCapAdmin *admin, AienosCapRef authority, uint64_t ticks); |
| 138 | int aienos_cap_bump_epoch(AienosCapAdmin *admin, AienosCapRef authority); |
| 139 | int aienos_cap_kill(AienosCapAdmin *admin); |
| 140 | int aienos_cap_restart(AienosCapAdmin *admin); |
| 141 | int aienos_cap_validate(const AienosCapView *view, AienosCapRef cap, uint32_t subject, |
| 142 | uint64_t resource, uint32_t rights, AienosCapEntry *out); |
| 143 | int aienos_cap_inspect(const AienosCapView *view, AienosCapRef cap, AienosCapEntry *out); |
| 144 | uint64_t aienos_cap_clock(const AienosCapView *view); |
| 145 | int aienos_cap_cognition_mint(const AienosCapView *view, const AienosCapMint *request, |
| 146 | const uint8_t *token); |
| 147 | int aienos_cap_cognition_admin(const AienosCapView *view, uint32_t op, AienosCapRef authority, |
| 148 | AienosCapRef target); |
| 149 | int aienos_cap_force_generation(AienosCapAdmin *admin, uint32_t cap_id, uint64_t generation); |
| 150 | int aienos_cap_generation_advance(uint64_t generation, uint64_t *out); |
| 151 | |
| 152 | /* Authority observer. The authority announces its own transitions, so a |
| 153 | * watcher sees a mint or revoke no matter which caller asked for it. |
| 154 | * |
| 155 | * The observer is set on the admin handle. The view has no path to it, and |
| 156 | * aienos_cap_validate never calls it. |
| 157 | * |
| 158 | * Calls, one per state change, with the entry as it is after the change: |
| 159 | * MINT new entry; a refused mint calls with entry NULL and its code |
| 160 | * REVOKE the target, then one call per descendant revoked with it, |
| 161 | * each ancestor before its descendants |
| 162 | * RECLAIM the freed slot (state FREE, next generation) |
| 163 | * EPOCH the entry of the authority that bumped it (the new epoch is |
| 164 | * not carried) |
| 165 | * CLOCK the entry of the authority that moved it (the new clock is |
| 166 | * not carried; aienos_cap_clock reads it) |
| 167 | * KILL entry NULL |
| 168 | * RESTART the new office entry (slot 0, new boot generation) |
| 169 | * A refused admin operation calls with entry NULL and the refusal code, |
| 170 | * except a call refused for a NULL argument, and a restart that fails |
| 171 | * before it takes the lock (out of memory, or /dev/urandom unreadable): |
| 172 | * those are not announced. |
| 173 | * aienos_cap_force_generation (the test seam) is not announced. |
| 174 | * AIENOS_CAP_OBS_VALIDATE_DENIED is reserved and never sent: validate runs |
| 175 | * on the view, which holds no observer, and it has no slow path to put |
| 176 | * one on. |
| 177 | * |
| 178 | * The entry is a copy taken under the table lock. It never holds the office |
| 179 | * token: the token is kept outside the table and outside AienosCapEntry. |
| 180 | * The call is made after the table lock is released, so a slow observer |
| 181 | * does not hold up validate or other admin operations. The callback gets |
| 182 | * a const entry and no admin handle, so its arguments give it no way to |
| 183 | * change the table. |
| 184 | * |
| 185 | * Ordering: calls from one thread arrive in the order that thread's |
| 186 | * operations changed the table. When admin operations run on several |
| 187 | * threads at once, calls may arrive in a different order than the changes; |
| 188 | * a caller that needs strict order serializes its admin calls. |
| 189 | * After aienos_cap_set_observer(admin, NULL, NULL) returns, no new call |
| 190 | * starts; a call already under way on another thread may still finish. |
| 191 | * |
| 192 | * With no observer set, each admin operation pays one NULL check. */ |
| 193 | #define AIENOS_CAP_OBS_MINT 1u |
| 194 | #define AIENOS_CAP_OBS_REVOKE 2u |
| 195 | #define AIENOS_CAP_OBS_RECLAIM 3u |
| 196 | #define AIENOS_CAP_OBS_EPOCH 4u |
| 197 | #define AIENOS_CAP_OBS_CLOCK 5u |
| 198 | #define AIENOS_CAP_OBS_KILL 6u |
| 199 | #define AIENOS_CAP_OBS_RESTART 7u |
| 200 | #define AIENOS_CAP_OBS_VALIDATE_DENIED 8u |
| 201 | |
| 202 | typedef void (*AienosCapObserver)(void *ctx, uint32_t op, const AienosCapEntry *entry, |
| 203 | int result); |
| 204 | |
| 205 | int aienos_cap_set_observer(AienosCapAdmin *admin, AienosCapObserver fn, void *ctx); |
| 206 | |
| 207 | /* Office token check, constant time. The token never enters the table. |
| 208 | * Nothing in this library calls it: admin operations are gated by holding |
| 209 | * the admin handle plus a live reference with the matching office right. |
| 210 | * The token is kept for a caller that wants a second factor. */ |
| 211 | int aienos_cap_authorize(const AienosCapAdmin *admin, const uint8_t *presented); |
| 212 | |
| 213 | #endif |