Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/docs/adr/0013-aienos-abi-v1.md # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / docs / adr / 0013-aienos-abi-v1.md` 197 lines ยท 9500 bytes Raw Blame History Rendered 1 # ADR 0013: AIENOS ABI v1 2 3 Status: Proposed 4 Date: 2026-09-24 5 Governing: [ADR 0012](0012-self-construction-capability-growth-and-generations.md) (rule 5: no native admission without enforcement), [issue #30](https://github.com/aien-dev/aienux/issues/30) (IPC and capability handles), [PR #106](https://github.com/aien-dev/aienux/pull/106) (typed IPC). 6 Code: [`crates/aienos-kernel/src/abi.rs`](../../crates/aienos-kernel/src/abi.rs). 7 8 ## Context 9 10 M3 puts a kernel-enforced boundary between tasks. Everything that crosses 11 that boundary (capability handles in registers, typed IPC messages, rights 12 masks, memory regions) is today defined only by Rust structs in `caps.rs`, 13 `user.rs` and the typed IPC work in PR #106. That makes the contract an 14 accident of the current code: 15 16 1. `caps::Handle { index: u32, generation: u32 }` is not `repr(C)`. Its 17 register form `(generation << 32) | index` lives in private helpers in 18 `user.rs` and is unpacked with no validity check. 19 2. `caps::Rights` is a `u8` whose reserved bits are rejected by the kernel, 20 but no document says which bits are reserved or how wide the field is on 21 the wire. 22 3. Resources are opaque `u32` values with no kind. 23 4. The IPC `Message` layout in PR #106 depends on `repr(C)` and the Rust 24 compiler's layout rules on AArch64. 25 26 Generated components (ADR 0012) and future non-Rust or non-AArch64 peers 27 need a contract they can implement from a document, and persisted 28 capability records need a format that a later build can still read or 29 reject safely. This ADR freezes that contract as ABI v1. 30 31 ## Decision 32 33 AIENOS ABI v1 is a byte-level contract. Every ABI type has an explicit 34 little-endian encoding defined here, independent of Rust in-memory layout, 35 architecture, and pointer width. The reference encoder and decoder is 36 `crates/aienos-kernel/src/abi.rs`; its golden-byte tests are the executable 37 form of this document. 38 39 ### General rules 40 41 - Byte order: little-endian for every multi-byte integer. 42 - Only fixed-width unsigned integers (`u16`, `u32`, `u64`) and fixed arrays 43 of them appear on the wire. No `usize`, `isize`, pointers, `Vec`, `String`, 44 `&str`, floating point, booleans, or compiler-chosen enum layout. 45 - Every type has one fixed size. There is no implicit padding: every byte is 46 a named field or a named reserved field. 47 - Reserved fields and reserved bits are written as zero and must be zero on 48 decode. 49 - Enum discriminants are explicit integers. 0 is never a valid discriminant. 50 - Decoding requires the exact size. Shorter input is `Truncated`; longer 51 input is `WrongLength`. 52 53 ### Types 54 55 | Type | Size | Layout (byte offset: field) | Validity | 56 |---|---|---|---| 57 | `Handle` | 8 | 0: index u32; 4: generation u32 | generation != 0 | 58 | `TaskId` | 4 | 0: u32 | any value | 59 | `ObjectId` | 8 | 0: u64 | any value | 60 | `ChannelId` | 4 | 0: u32 | any value | 61 | `DeviceId` | 4 | 0: u32 | any value | 62 | `ResourceId` | 8 | 0: kind u16; 2: reserved u16; 4: id u32 | kind in 1..=5; reserved == 0 | 63 | `Rights` | 4 | 0: u32 bit mask | bits 6 to 31 == 0 | 64 | `Capability` | 16 | 0: resource `ResourceId`; 8: rights `Rights`; 12: reserved u32 | nested rules; reserved == 0 | 65 | `MemoryRegion` | 16 | 0: base u64; 8: pages u32; 12: reserved u32 | reserved == 0 | 66 | `Message` | 56 | 0: kind u32; 4: reserved u32; 8: payload 3 x u64; 32: region `MemoryRegion`; 48: object `ObjectId` | reserved == 0; nested rules | 67 68 `ResourceKind` discriminants: 1 Console, 2 Channel, 3 Object, 4 Device, 69 5 MemoryRegion. `ResourceId.id` is the kernel's resource number within that 70 kind (the existing opaque `u32` resource). It is not an `ObjectId`; an 71 `ObjectId` is a separate 64-bit identity carried in messages. 72 73 `Rights` bits: 0 READ, 1 WRITE, 2 MAP, 3 GRANT, 4 DERIVE, 5 REVOKE. The wire 74 field is 32 bits so new rights can be added without changing the size; the 75 kernel's in-memory `caps::Rights` stays `u8` and converts at the boundary. 76 77 `Message` keeps the field order of the typed IPC message in PR #106. Its 78 `_pad` word is the reserved field at offset 4. `Message.kind` is opaque to 79 this ABI; the IPC layer assigns meaning to it. 80 81 ### Handles 82 83 A handle is 64 bits: a 32-bit slot index plus the 32-bit generation of that 84 slot when the handle was issued. The register form, used in `x0` by the 85 syscall path, is `(generation << 32) | index`. 86 87 Why 64-bit generational rather than a 32-bit handle: 88 89 - Stale-handle protection. When a slot is freed and reused, its generation 90 goes up, so an old handle to the same index no longer matches and is 91 refused. A task cannot reach a new resource through a handle it kept 92 from an old one. 93 - Generations never wrap. They start at 1 on first use and a slot whose 94 generation reaches `u32::MAX` is retired rather than reused, so the same 95 (index, generation) pair is never issued twice for one table. 96 - Generation 0 is never issued, so it is a reserved "no handle" value and 97 every decoder rejects it (`InvalidHandle`). Zeroed memory or a zeroed 98 register is never a valid handle. 99 - 32 bits of index and 32 bits of generation both fit one AArch64 register 100 and one 64-bit word on any other architecture. 101 102 ### Versioned envelope 103 104 Any ABI value that is persisted or transported outside a single syscall 105 (capability records, messages written to storage or sent to another 106 machine) is wrapped in an 8-byte envelope header followed by the payload: 107 108 | Offset | Field | Rule | 109 |---|---|---| 110 | 0 | tag u16 | known `EnvelopeTag`, and equal to the expected type | 111 | 2 | version u16 | must equal `ABI_VERSION` (1) | 112 | 4 | length u32 | payload length in bytes, must equal the type's size | 113 | 8 | payload | exactly `length` bytes | 114 115 `EnvelopeTag` discriminants: 1 Handle, 2 TaskId, 3 ObjectId, 4 ChannelId, 116 5 DeviceId, 6 ResourceId, 7 Rights, 8 Capability, 9 MemoryRegion, 117 10 Message. The header layout itself is fixed for every future version, so 118 any reader can always find the version and length. 119 120 Decode checks run in this order and stop at the first failure: header 121 present, version, tag known, tag matches, length field, total buffer size, 122 payload. 123 124 ### Errors 125 126 Decoding fails closed. Each failure has its own error: 127 128 | Error | Cause | 129 |---|---| 130 | `Truncated` | input shorter than required | 131 | `WrongLength` | input longer than the type, or envelope length field wrong | 132 | `UnknownVersion` | envelope version is not 1 | 133 | `UnknownTag` | envelope tag not defined | 134 | `TagMismatch` | envelope tag defined but for another type | 135 | `ReservedNonZero` | a reserved field is not zero | 136 | `ReservedBits` | a reserved rights bit is set | 137 | `UnknownDiscriminant` | a resource kind is not defined | 138 | `InvalidHandle` | handle generation is 0 | 139 | `BufferTooSmall` | output buffer too small when encoding | 140 141 ### Compatibility and extension 142 143 - ABI v1 is frozen. The sizes, offsets, byte order, discriminants and 144 validity rules above do not change for version 1. 145 - New fields are added only by giving meaning to a reserved field or reserved 146 bit, or by defining a new version. A v1 decoder keeps rejecting nonzero 147 reserved fields, so an old reader refuses a newer record instead of 148 misreading it. 149 - Discriminants (`ResourceKind`, `EnvelopeTag`) are append only and never 150 reused, even after a kind or tag is retired. 151 - A new version gets a new `ABI_VERSION` value. A decoder that does not know 152 a version rejects it with `UnknownVersion`; it never guesses. 153 - Register calling conventions for individual syscalls are outside this ADR; 154 they carry v1 values in the register form given here. 155 156 ### Relation to AEGIS 157 158 This ABI is kernel enforcement: it defines what a well-formed handle, 159 capability, or message is, and the kernel refuses anything else before any 160 policy runs. AEGIS is runtime policy: it decides whether a well-formed, 161 correctly authorized request should be allowed for this owner and moment 162 (ADR 0004, ADR 0012 rule 4). AEGIS never widens what the kernel accepts, and 163 a malformed value never reaches AEGIS. 164 165 ## Invariants 166 167 1. Every ABI v1 type has exactly one byte encoding, fixed in size, little 168 endian, with no implicit padding. 169 2. Encode then decode returns the same value, and decode then encode returns 170 the same bytes, for every valid value. Golden-byte tests pin this for every 171 type. 172 3. Decoding never accepts: short or long input, nonzero reserved fields or 173 bits, unknown discriminants, unknown versions, mismatched tags, or 174 generation-0 handles. 175 4. Handle generations start at 1 and never wrap; a retired slot is never 176 reissued. 177 5. No ABI type contains `usize`, `isize`, pointers, heap types, or 178 compiler-chosen enum layout. `const` assertions pin the size, alignment and 179 field offsets of every in-memory mirror. 180 6. Discriminants are never reused. 181 7. No em dashes or en dashes in this document or the ABI code comments. 182 183 ## Consequences 184 185 - `abi.rs` is the reference contract. `caps.rs`, `user.rs` and `ipc.rs` 186 adopt these types in a later change (after PR #106 lands); until then 187 `abi.rs` provides lossless conversions to and from `caps::Handle` and 188 `caps::Rights`. 189 - Non-Rust and non-AArch64 implementations (other languages, other 190 machines through Fabric, generated components under ADR 0012) can 191 implement the ABI from this document and check themselves against the 192 golden bytes in the tests. 193 - Persisted capability records and Generation manifests (ADR 0012) can use 194 the envelope and be rejected safely by a build that does not understand 195 them. 196 - Any change to a v1 layout is a breaking change and needs a new version and 197 a new ADR.