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 / pipe.h
| 1 | /* pipe.h -- bounded byte-stream pipes, a native AIENOS execution primitive (aienos-mv#428, AIENOS-NATIVE-PIPE-1). |
| 2 | * |
| 3 | * Contract: aien-protocols-mv specs/osh-platform/OSH_PLATFORM_ABI.md read at commit fb5fca98e667 (v1 DRAFT, unfrozen): |
| 4 | * section 8.1 (PIPE_CREATE, STREAM_READ / WRITE / CLOSE / DUP; bounded pipes with backpressure, WOULD_BLOCK or block, |
| 5 | * BROKEN_PIPE when the reader closed), 8.5 (64 KiB per pipe minimum, LIMIT instead of truncation), 8.6 (error codes, |
| 6 | * shared with osh_op.h through osh_err.h), 4.4 (epoch: an id from another epoch fails STALE) and 9.2 (authority is |
| 7 | * checked at the moment of the effect). Anything below marked UNVERIFIED is this kernel's own choice where the draft |
| 8 | * names a thing but does not pin it. |
| 9 | * |
| 10 | * This is a byte pipe, separate from the 56-byte typed message ring in ipc.h. It reuses only the capability tables of |
| 11 | * ipc.h: every stream end is a capability in the caller's ck_cap_table, and every operation looks the handle up with |
| 12 | * ck_cap_lookup (live slot, matching generation, required right) when the effect happens, not before. |
| 13 | * |
| 14 | * read end capability with CK_R_READ (never CK_R_WRITE) |
| 15 | * write end capability with CK_R_WRITE (never CK_R_READ) |
| 16 | * optional CK_R_DERIVE (dup into the same table), CK_R_GRANT (dup into another table), CK_R_REVOKE (revoke the pipe) |
| 17 | * |
| 18 | * Lifetimes. A pipe lives while at least one end capability is open. reader_refs / writer_refs count the open end |
| 19 | * capabilities (create gives one of each, every dup adds one, every close removes one). Last writer closed: once the |
| 20 | * buffer is drained a read returns OK with 0 bytes (EOF). No reader: a write returns BROKEN_PIPE. Both counts zero: the |
| 21 | * pipe slot is scrubbed and freed. Capabilities are independent slots (no derive parent link), so a closed end frees |
| 22 | * its slot at once and a table can always be emptied; ipc.h cascading revoke is therefore not used for pipes. |
| 23 | * Every pipe capability must be released through ck_stream_close or ck_pipe_release_table: removing one with |
| 24 | * ck_cap_remove directly would leave its count held. |
| 25 | * |
| 26 | * Epoch. Each pool slot has an epoch, incremented every time the slot is (re)created. The capability resource carries |
| 27 | * it, so a capability that outlived its pipe never reaches the next pipe in that slot: STALE. A handle whose capability |
| 28 | * slot was closed (and maybe reused with a new generation) also fails STALE, by the ck_cap generation check. |
| 29 | * |
| 30 | * Revocation and cancellation. ck_pipe_revoke (CK_R_REVOKE on either end) kills the whole pipe: the buffered bytes are |
| 31 | * scrubbed, both ends are woken, and every later read, write, dup or cancel on any end returns REVOKED; no byte is |
| 32 | * delivered after a revoke. Only close still works, to release the handles. ck_pipe_cancel (the data right of that |
| 33 | * end) interrupts the end: the next or currently blocked read (or write) on that end returns INTERRUPTED with no |
| 34 | * bytes moved, once; a later call runs normally (section 10: an interrupted wait is retried without re-running an |
| 35 | * effect, and an interrupted call here never moved a byte). |
| 36 | * |
| 37 | * Blocking. With CK_PIPE_F_BLOCK a read on an empty pipe or a write on a full one parks the caller through the |
| 38 | * pool's wait hook instead of returning WOULD_BLOCK, and retries (re-checking authority) after the hook returns OK. |
| 39 | * A blocking call returns as soon as it moved at least one byte (partial I/O; the caller loops). The kernel's hook |
| 40 | * yields to the scheduler; the host test simulates it. Without a wait hook a blocking call returns WOULD_BLOCK. |
| 41 | * A blocking write copies the bytes it may commit into a kernel buffer before it parks (at most CK_PIPE_WRITE_SNAP |
| 42 | * of them) and the retry reads that buffer, not the caller memory. A direct ck_stream_write that does not park |
| 43 | * copies once, under the pool lock, straight into the ring; ck_pipe_dispatch never hands it caller memory (see the |
| 44 | * user-window note below). |
| 45 | * |
| 46 | * Locking. One lock covers the pool (every pipe, the ref counts, the ring, the stats, the live count). It is the |
| 47 | * kernel's shared spinlock (core/spinlock.h): a fair ticket lock that masks interrupts on the holding CPU, so a |
| 48 | * pipe op reached from an interrupt handler waits for the unlock instead of deadlocking that CPU against itself. |
| 49 | * Only the non-blocking forms may be called from an interrupt handler: a blocking read or write calls the wait |
| 50 | * hook, which parks the caller, and an interrupt handler must never park. |
| 51 | * create, read, write, close, dup, revoke, cancel, release and ck_pipe_pool_empty all take it. It is |
| 52 | * never held across wait or wake, so a hook may call back into the pipe. The waiter increments waiting[end] under |
| 53 | * the lock before dropping it; a waker calls the wake hook after dropping the lock, and only when that count was |
| 54 | * non-zero. The wake hook must be sticky (woken[] stays set until wait has observed it). before_wait, if set, |
| 55 | * runs in that window; production leaves it NULL. The capability table is covered only by this pool lock. Another |
| 56 | * core using the same ck_cap_table for a non-pipe operation would race. |
| 57 | * ck_pipe_pool_init is single-threaded, before the pool is shared. |
| 58 | * |
| 59 | * User window. ck_pipe_dispatch rejects a read or a write whose slice lies outside [user_lo, user_hi) with |
| 60 | * INVALID_ARG, before anything reads or writes the slice. A length of 0 passes. Otherwise user_hi must be at most |
| 61 | * CK_PIPE_USER_TOP (the window ends at or below the top of user VA: TTBR1 space starts at 0xFFFF000000000000 on |
| 62 | * ARM64; the x86 canonical lower half ends at 0x0000800000000000 with 4-level paging), the base must sit in |
| 63 | * [user_lo, user_hi) and the length must be <= user_hi - base. The check never adds base and length, so a length |
| 64 | * that would wrap the address space fails. ck_stream_read and ck_stream_write do not repeat it; a direct caller |
| 65 | * passes a kernel buffer it already owns. |
| 66 | * Bounce. ck_pipe_dispatch never touches the caller's slice while the pool lock is held. A write copies at most |
| 67 | * CK_PIPE_BOUNCE bytes of the slice into a kernel stack buffer before it calls ck_stream_write; a read reads at |
| 68 | * most CK_PIPE_BOUNCE bytes into that buffer through ck_stream_read and copies them out to the slice after the |
| 69 | * lock is dropped. A longer slice moves a partial count (section 8.1 allows partial I/O; the caller loops). A |
| 70 | * fault on the slice therefore never runs inside the ticket lock. The copy is still a plain load/store loop: the |
| 71 | * syscall layer, once there is one, supplies fault recovery around it. A read whose copy-out faults has already |
| 72 | * consumed those bytes from the ring. |
| 73 | * |
| 74 | * Memory: a fixed pool of CK_PIPE_POOL_PIPES pipes with CK_PIPE_CAPACITY bytes each, all static; nothing grows. |
| 75 | * Pure logic, no hardware: host-tested in tests/test_pipe.c (fixtures in tests/fixtures/pipe/, plus host threads |
| 76 | * for concurrent read, write and close), driven in QEMU by core/pipe_test.c (CK_PIPE_TEST). The QEMU driver is |
| 77 | * one kernel thread. Host and QEMU evidence qualify nothing physical. */ |
| 78 | #ifndef AIENOS_CK_PIPE_H |
| 79 | #define AIENOS_CK_PIPE_H |
| 80 | |
| 81 | #include <stddef.h> |
| 82 | #include <stdint.h> |
| 83 | #include "ipc.h" |
| 84 | #include "osh_err.h" |
| 85 | #include "spinlock.h" |
| 86 | |
| 87 | #define CK_PIPE_ABI_VERSION 1u /* section 3: abi_version of the platform frame */ |
| 88 | #define CK_PIPE_FRAME_MAGIC 0x4F534846u /* section 3: "OSHF", platform frame magic */ |
| 89 | #define CK_PIPE_CAPACITY 65536u /* bytes per pipe; section 8.5 minimum 64 KiB (LIMITS_GET max_pipe_bytes) */ |
| 90 | #define CK_PIPE_POOL_PIPES 4u /* pipes per pool; UNVERIFIED: the draft sets no pipe count */ |
| 91 | _Static_assert(CK_PIPE_CAPACITY >= 65536u, "section 8.5: at least 64 KiB per pipe"); |
| 92 | _Static_assert(CK_PIPE_POOL_PIPES >= 1u && CK_PIPE_POOL_PIPES <= 8u, "the pipe index has three bits in the resource"); |
| 93 | |
| 94 | /* Capability resource of a stream end: tag 0x50 ("P") in bits 31..24, end in bit 23, pipe index in bits 22..20, |
| 95 | * epoch in bits 19..0. A slot whose epoch reached CK_PIPE_EPOCH_MAX is retired for good. Layout UNVERIFIED (own). */ |
| 96 | #define CK_PIPE_RES_TAG 0x50u |
| 97 | #define CK_PIPE_EPOCH_MAX 0xFFFFFu |
| 98 | static inline uint32_t ck_pipe_resource(unsigned idx, unsigned end, uint32_t epoch) |
| 99 | { |
| 100 | return (CK_PIPE_RES_TAG << 24) | ((uint32_t)(end & 1u) << 23) | ((uint32_t)(idx & 7u) << 20) | |
| 101 | (epoch & CK_PIPE_EPOCH_MAX); |
| 102 | } |
| 103 | |
| 104 | enum ck_pipe_end { CK_PIPE_END_READ = 0, CK_PIPE_END_WRITE = 1 }; |
| 105 | |
| 106 | /* Platform op ids of the versioned op table. The draft names the operations; the numbers are UNVERIFIED (own). */ |
| 107 | enum ck_pipe_op { |
| 108 | CK_PIPE_OP_CREATE = 1, /* PIPE_CREATE() -> (read_stream, write_stream) */ |
| 109 | CK_PIPE_OP_READ = 2, /* STREAM_READ(stream, out_slice) -> n, 0 = EOF */ |
| 110 | CK_PIPE_OP_WRITE = 3, /* STREAM_WRITE(stream, in_slice) -> n, partial writes allowed */ |
| 111 | CK_PIPE_OP_CLOSE = 4, /* STREAM_CLOSE(stream) */ |
| 112 | CK_PIPE_OP_DUP = 5, /* STREAM_DUP(src_stream, rights, target table) -> stream; capability form, UNVERIFIED */ |
| 113 | }; |
| 114 | |
| 115 | #define CK_PIPE_F_BLOCK 0x1u /* read / write flag: park through the wait hook instead of WOULD_BLOCK */ |
| 116 | /* Bytes a blocking write snapshots before it parks. Kept under 4 KiB so a nested park still fits a 16 KiB |
| 117 | * secondary-core stack (core/smp.c). A longer blocking write returns a partial count and the caller loops. */ |
| 118 | #define CK_PIPE_WRITE_SNAP 1024u |
| 119 | _Static_assert(CK_PIPE_WRITE_SNAP >= 1u && CK_PIPE_WRITE_SNAP <= 4096u, "snapshot must fit a secondary stack"); |
| 120 | /* Bytes ck_pipe_dispatch moves per READ or WRITE call through its kernel bounce buffer. It sits on the stack next to |
| 121 | * a blocking write's snapshot, so the two together stay under 4 KiB (same secondary-stack budget as above). */ |
| 122 | #define CK_PIPE_BOUNCE 1024u |
| 123 | _Static_assert(CK_PIPE_BOUNCE >= 1u && CK_PIPE_BOUNCE + CK_PIPE_WRITE_SNAP <= 4096u, "bounce + snapshot must fit"); |
| 124 | |
| 125 | /* Top of user VA (exclusive): a dispatch window must end at or below it. The x86 value assumes 4-level paging; |
| 126 | * x86/kmain_x86.c panics if firmware runs 5-level paging (CR4.LA57). */ |
| 127 | #if defined(__aarch64__) |
| 128 | #define CK_PIPE_USER_TOP ((uintptr_t)0xFFFF000000000000ull) /* TTBR1 (kernel) space starts here */ |
| 129 | #elif defined(__x86_64__) |
| 130 | #define CK_PIPE_USER_TOP ((uintptr_t)0x0000800000000000ull) /* end of the canonical lower half, 4-level paging */ |
| 131 | #else |
| 132 | #error "pipe.h: no user-VA top for this architecture" |
| 133 | #endif |
| 134 | |
| 135 | /* Rights a create gives each end beyond its data right, and the only rights a dup may carry. */ |
| 136 | #define CK_PIPE_CTL_RIGHTS (CK_R_DERIVE | CK_R_GRANT | CK_R_REVOKE) |
| 137 | |
| 138 | struct ck_pipe_stats { |
| 139 | uint64_t bytes_written, bytes_read; /* bytes that moved */ |
| 140 | uint64_t writes, reads; /* calls that moved at least one byte */ |
| 141 | uint64_t max_resident; /* most bytes ever buffered at once (never above CK_PIPE_CAPACITY) */ |
| 142 | uint64_t would_block; /* WOULD_BLOCK results seen, returned or waited on (backpressure) */ |
| 143 | uint64_t waits; /* calls into the wait hook */ |
| 144 | uint64_t wakeups; /* calls into the wake hook (an end had a waiter) */ |
| 145 | uint64_t cancellations; /* INTERRUPTED results */ |
| 146 | uint64_t revocations; /* pipes revoked */ |
| 147 | uint64_t broken_pipe; /* BROKEN_PIPE results */ |
| 148 | uint64_t eof; /* reads that returned EOF */ |
| 149 | uint64_t refused; /* DENIED, STALE and REVOKED results at the effect boundary (pool total only) */ |
| 150 | }; |
| 151 | |
| 152 | #define CK_PIPE_LIVE 0x1u |
| 153 | #define CK_PIPE_REVOKED 0x2u |
| 154 | #define CK_PIPE_CANCEL_READ 0x4u |
| 155 | #define CK_PIPE_CANCEL_WRITE 0x8u |
| 156 | |
| 157 | struct ck_pipe { |
| 158 | uint8_t buf[CK_PIPE_CAPACITY]; /* ring: bytes head..head+len-1 (mod capacity) are buffered */ |
| 159 | uint32_t head, len; |
| 160 | uint32_t epoch; /* 0 = never created */ |
| 161 | uint32_t reader_refs, writer_refs; |
| 162 | uint32_t flags; |
| 163 | uint32_t waiting[2]; /* callers parked in the wait hook, per end; only touched under the pool lock */ |
| 164 | struct ck_pipe_stats stats; /* this incarnation; reset at create */ |
| 165 | }; |
| 166 | |
| 167 | /* Wait / wake hook. wait: the caller would block on (pipe, end); return OSH_E_OK to retry the operation (it re-checks |
| 168 | * authority) or an error (normally OSH_E_INTERRUPTED) to give up; the operation then returns it with no byte moved. |
| 169 | * Called without the pool lock. The wake flag must be sticky. A wake may already have run after the waiter was |
| 170 | * published and before wait is called; wait must observe it and must not clear it before that observation |
| 171 | * (pd_wait leaves woken[] set until it has seen it). A hook that clears the flag on entry loses that wake. |
| 172 | * wake: (pipe, end) may proceed (bytes, space, close, revoke or cancel); called only when that end has a waiter, |
| 173 | * and only after the pool lock is dropped. before_wait: optional, same window, NULL in production. */ |
| 174 | struct ck_pipe_hooks { |
| 175 | void *ctx; |
| 176 | unsigned (*wait)(void *ctx, unsigned pipe, unsigned end); |
| 177 | void (*wake)(void *ctx, unsigned pipe, unsigned end); |
| 178 | void (*before_wait)(void *ctx); |
| 179 | }; |
| 180 | |
| 181 | struct ck_pipe_pool { |
| 182 | struct ck_pipe pipes[CK_PIPE_POOL_PIPES]; |
| 183 | const struct ck_pipe_hooks *hooks; /* may be 0 */ |
| 184 | struct ck_pipe_stats total; /* every incarnation since init */ |
| 185 | unsigned live; /* pipes currently live */ |
| 186 | struct ck_spinlock lock; /* core/spinlock.h: fair, masks interrupts while held */ |
| 187 | }; |
| 188 | |
| 189 | void ck_pipe_pool_init(struct ck_pipe_pool *p, const struct ck_pipe_hooks *hooks); |
| 190 | /* 1 if no pipe is live (every end of every pipe was closed). Takes the pool lock. */ |
| 191 | int ck_pipe_pool_empty(struct ck_pipe_pool *p); |
| 192 | |
| 193 | /* PIPE_CREATE: a new pipe, both ends inserted into t. LIMIT if the pool or the table is full (nothing is left |
| 194 | * allocated). The draft's PIPE_CREATE takes no binding, so no prior capability is required (UNVERIFIED). */ |
| 195 | unsigned ck_pipe_create(struct ck_pipe_pool *p, struct ck_cap_table *t, struct ck_handle *rd, struct ck_handle *wr); |
| 196 | /* STREAM_READ / STREAM_WRITE: *n = bytes moved (0 with OK on a read = EOF, or max == 0). */ |
| 197 | unsigned ck_stream_read(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, uint8_t *buf, |
| 198 | size_t max, unsigned flags, size_t *n); |
| 199 | unsigned ck_stream_write(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, const uint8_t *buf, |
| 200 | size_t len, unsigned flags, size_t *n); |
| 201 | /* STREAM_CLOSE: releases the capability. Works on a revoked pipe. STALE (after releasing the capability) if it named |
| 202 | * an earlier incarnation of the slot. */ |
| 203 | unsigned ck_stream_close(struct ck_pipe_pool *p, struct ck_cap_table *t, struct ck_handle h); |
| 204 | /* STREAM_DUP: a new capability on the same end in `to` with `rights`, which must hold the end's data right, be a |
| 205 | * subset of the source rights and of data right | CK_PIPE_CTL_RIGHTS. Needs CK_R_DERIVE (to == from) or |
| 206 | * CK_R_GRANT (another table) on the source. */ |
| 207 | unsigned ck_stream_dup(struct ck_pipe_pool *p, struct ck_cap_table *from, struct ck_handle h, struct ck_cap_table *to, |
| 208 | unsigned rights, struct ck_handle *out); |
| 209 | /* Revoke the whole pipe (CK_R_REVOKE on either end). */ |
| 210 | unsigned ck_pipe_revoke(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h); |
| 211 | /* Interrupt the end h names (its data right needed). */ |
| 212 | unsigned ck_pipe_cancel(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h); |
| 213 | /* Section 12 cleanup: closes every pipe capability in t. Returns how many STREAM_CLOSE calls returned OK. |
| 214 | * A failure is not counted. STALE (the capability was released, but it named a dead incarnation) is a failure. */ |
| 215 | unsigned ck_pipe_release_table(struct ck_pipe_pool *p, struct ck_cap_table *t); |
| 216 | |
| 217 | /* ---- versioned op table ---- |
| 218 | * A platform call frame is cells (u64): 0 magic CK_PIPE_FRAME_MAGIC, 1 abi_version, 2 op, 3 reserved (0), then the |
| 219 | * op's arguments; the frame must be exactly 4 + nargs cells. Arguments: READ / WRITE: handle (ck_handle_raw form), |
| 220 | * flags; CLOSE: handle; DUP: handle, rights, target (0 caller table, 1 peer table); CREATE: none. The byte slice is |
| 221 | * the read destination or the write source. Check order, first failure wins, all before any effect: frame shorter |
| 222 | * than 4 cells, bad magic or nonzero reserved -> INVALID_ARG; abi_version not CK_PIPE_ABI_VERSION or an unknown op |
| 223 | * -> NOT_SUPPORTED; wrong cell count, a generation-0 handle or unknown flag bits -> INVALID_ARG. |
| 224 | * Result: {error, value} as in section 8; PIPE_CREATE also fills value2 (the write end). value2 is UNVERIFIED. |
| 225 | * READ and WRITE then require slice[0, slice_len) inside [user_lo, user_hi), user_hi exclusive and at most |
| 226 | * CK_PIPE_USER_TOP. A length of 0 passes. Any other slice is INVALID_ARG before the buffer is touched (see the |
| 227 | * user-window note above). At most CK_PIPE_BOUNCE bytes move per call, through a kernel bounce buffer. |
| 228 | * user_lo and user_hi are the caller's window; kernel self-tests pass the bounds of their own buffer. */ |
| 229 | struct ck_pipe_ret { |
| 230 | uint32_t error; |
| 231 | uint64_t value, value2; |
| 232 | }; |
| 233 | unsigned ck_pipe_dispatch(struct ck_pipe_pool *p, struct ck_cap_table *caller, struct ck_cap_table *peer, |
| 234 | const uint64_t *frame, size_t ncells, uint8_t *slice, size_t slice_len, |
| 235 | uintptr_t user_lo, uintptr_t user_hi, struct ck_pipe_ret *ret); |
| 236 | |
| 237 | #endif |