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_drive.h
| 1 | /* pipe_drive.h -- TEST driver for the pipe primitive (pipe.h): a producer and a consumer task, each a step function, |
| 2 | * interleaved by the run-queue scheduler (sched.h), exchanging a binary payload (every fifth byte NUL) through one pipe. |
| 3 | * Shared by the host test (tests/test_pipe.c) and the QEMU self-test (core/pipe_test.c, CK_PIPE_TEST) so both run |
| 4 | * the same scenario. No libc, no allocation: the caller owns the world, the pool, both tables and the sink buffer. |
| 5 | * |
| 6 | * The producer owns the write end; the read end is handed to the consumer's own table with STREAM_DUP (READ, plus |
| 7 | * REVOKE for the revocation scenario) and the producer's copy is closed, so each task holds one end. Non-blocking mode: |
| 8 | * a WOULD_BLOCK ends the step with no progress and the scheduler runs the other task. Blocking mode: the wait hook |
| 9 | * marks the caller blocked and keeps ticking the scheduler, running the other task's steps, until the wake hook fires |
| 10 | * for that end (OK, the call retries) or the spin bound runs out (INTERRUPTED: a stalled peer). A wake posted |
| 11 | * before wait runs is kept: woken[] is not cleared on entry. Static functions only. One kernel thread. */ |
| 12 | #ifndef AIENOS_CK_PIPE_DRIVE_H |
| 13 | #define AIENOS_CK_PIPE_DRIVE_H |
| 14 | |
| 15 | #include "pipe.h" |
| 16 | #include "sched.h" |
| 17 | |
| 18 | #define PD_CHUNK_MAX 8192u |
| 19 | #define PD_SPIN_MAX 64u |
| 20 | #define PD_TICK_MAX 2000000u |
| 21 | |
| 22 | struct pd_world; |
| 23 | struct pd_task { |
| 24 | uint32_t id; |
| 25 | int done, blocked; |
| 26 | void (*step)(struct pd_world *w, struct pd_task *t); |
| 27 | }; |
| 28 | |
| 29 | struct pd_world { |
| 30 | struct ck_pipe_pool *pool; |
| 31 | struct ck_cap_table *prod_t, *cons_t; |
| 32 | struct ck_pipe_hooks hooks; |
| 33 | struct ck_sched sched; |
| 34 | struct pd_task tasks[2]; /* 0 producer, 1 consumer */ |
| 35 | struct pd_task *current; |
| 36 | struct ck_handle w, r; |
| 37 | uint8_t woken[CK_PIPE_POOL_PIPES][2]; |
| 38 | unsigned flags; /* CK_PIPE_F_BLOCK or 0 */ |
| 39 | size_t total; /* payload bytes to send */ |
| 40 | size_t chunk_w, chunk_r; /* bytes per producer write / consumer read */ |
| 41 | unsigned prod_every, cons_every; /* a task moves bytes on one step in this many (a slow side), 0 or 1 = every step */ |
| 42 | size_t revoke_at; /* consumer revokes the pipe once it has this many bytes (0 = never) */ |
| 43 | uint8_t *sink; /* total bytes */ |
| 44 | uint8_t wbuf[PD_CHUNK_MAX], rbuf[PD_CHUNK_MAX]; |
| 45 | /* results */ |
| 46 | size_t sent, recvd, reads_after_revoke; |
| 47 | unsigned setup_err, prod_err, cons_err; /* OSH_E_OK, or the error the task ended on */ |
| 48 | int eof_seen, revoked; |
| 49 | uint64_t ticks, wait_spins, prod_steps, cons_steps; |
| 50 | }; |
| 51 | |
| 52 | static inline uint8_t pd_byte(size_t i) |
| 53 | { |
| 54 | return i % 5 == 0 ? 0 : (uint8_t)(((uint32_t)i * 2654435761u) >> 13); |
| 55 | } |
| 56 | |
| 57 | static inline struct pd_task *pd_find(struct pd_world *w, uint32_t id) |
| 58 | { |
| 59 | return w->tasks[0].id == id ? &w->tasks[0] : w->tasks[1].id == id ? &w->tasks[1] : 0; |
| 60 | } |
| 61 | |
| 62 | /* One scheduler tick: run the picked task's step unless it is blocked (parked on the stack) or done. */ |
| 63 | static inline int pd_tick(struct pd_world *w) |
| 64 | { |
| 65 | uint32_t id = 0; |
| 66 | if (ck_sched_tick(&w->sched, 0, &id) != 1) |
| 67 | return 0; |
| 68 | w->ticks++; |
| 69 | struct pd_task *t = pd_find(w, id); |
| 70 | if (!t || t->blocked || t->done) |
| 71 | return 1; |
| 72 | struct pd_task *prev = w->current; |
| 73 | w->current = t; |
| 74 | t->step(w, t); |
| 75 | w->current = prev; |
| 76 | if (t->done) |
| 77 | ck_sched_remove(&w->sched, t->id); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static inline unsigned pd_wait(void *ctx, unsigned pipe, unsigned end) |
| 82 | { |
| 83 | struct pd_world *w = ctx; |
| 84 | struct pd_task *me = w->current; |
| 85 | if (!me) |
| 86 | return OSH_E_INTERRUPTED; |
| 87 | me->blocked = 1; |
| 88 | /* Do not clear woken[] here. The pipe publishes the waiter, drops its lock, then calls wait. A peer can |
| 89 | * wake in that window. Clearing first would drop that wake and the task would sleep with data already queued. */ |
| 90 | for (unsigned s = 0; s < PD_SPIN_MAX; s++) { |
| 91 | if (__atomic_load_n(&w->woken[pipe][end], __ATOMIC_ACQUIRE)) |
| 92 | break; |
| 93 | w->wait_spins++; |
| 94 | if (!pd_tick(w)) |
| 95 | break; |
| 96 | } |
| 97 | me->blocked = 0; |
| 98 | unsigned got = __atomic_exchange_n(&w->woken[pipe][end], 0, __ATOMIC_ACQ_REL); |
| 99 | return got ? OSH_E_OK : OSH_E_INTERRUPTED; |
| 100 | } |
| 101 | |
| 102 | static inline void pd_wake(void *ctx, unsigned pipe, unsigned end) |
| 103 | { |
| 104 | struct pd_world *w = ctx; |
| 105 | __atomic_store_n(&w->woken[pipe][end], 1, __ATOMIC_RELEASE); |
| 106 | } |
| 107 | |
| 108 | static inline void pd_producer(struct pd_world *w, struct pd_task *t) |
| 109 | { |
| 110 | w->prod_steps++; |
| 111 | if (w->prod_every > 1 && w->prod_steps % w->prod_every) |
| 112 | return; /* thinking: no I/O this step */ |
| 113 | if (w->sent == w->total) { |
| 114 | ck_stream_close(w->pool, w->prod_t, w->w); /* last writer: the consumer sees EOF once drained */ |
| 115 | t->done = 1; |
| 116 | return; |
| 117 | } |
| 118 | size_t want = w->total - w->sent; |
| 119 | if (want > w->chunk_w) |
| 120 | want = w->chunk_w; |
| 121 | for (size_t i = 0; i < want; i++) |
| 122 | w->wbuf[i] = pd_byte(w->sent + i); |
| 123 | size_t n = 0; |
| 124 | unsigned e = ck_stream_write(w->pool, w->prod_t, w->w, w->wbuf, want, w->flags, &n); |
| 125 | if (e == OSH_E_OK) { |
| 126 | w->sent += n; |
| 127 | } else if (e != OSH_E_WOULD_BLOCK) { |
| 128 | w->prod_err = e; |
| 129 | ck_stream_close(w->pool, w->prod_t, w->w); |
| 130 | t->done = 1; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static inline void pd_consumer(struct pd_world *w, struct pd_task *t) |
| 135 | { |
| 136 | w->cons_steps++; |
| 137 | if (w->cons_every > 1 && w->cons_steps % w->cons_every) |
| 138 | return; |
| 139 | size_t n = 0; |
| 140 | unsigned e = ck_stream_read(w->pool, w->cons_t, w->r, w->rbuf, w->chunk_r, w->flags, &n); |
| 141 | if (e == OSH_E_OK && n > 0) { |
| 142 | if (w->revoked) |
| 143 | w->reads_after_revoke++; |
| 144 | for (size_t i = 0; i < n && w->recvd + i < w->total; i++) |
| 145 | w->sink[w->recvd + i] = w->rbuf[i]; |
| 146 | w->recvd += n; |
| 147 | if (w->revoke_at && !w->revoked && w->recvd >= w->revoke_at) { |
| 148 | w->revoked = ck_pipe_revoke(w->pool, w->cons_t, w->r) == OSH_E_OK; |
| 149 | if (!w->revoked) |
| 150 | w->cons_err = OSH_E_IO; |
| 151 | } |
| 152 | } else if (e == OSH_E_OK) { |
| 153 | w->eof_seen = 1; |
| 154 | ck_stream_close(w->pool, w->cons_t, w->r); |
| 155 | t->done = 1; |
| 156 | } else if (e != OSH_E_WOULD_BLOCK) { |
| 157 | w->cons_err = e; |
| 158 | ck_stream_close(w->pool, w->cons_t, w->r); |
| 159 | t->done = 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* Run one transfer. pool and both tables must be fresh (the pool initialised with &w->hooks by this call). */ |
| 164 | static inline void pd_run(struct pd_world *w, struct ck_pipe_pool *pool, struct ck_cap_table *prod_t, |
| 165 | struct ck_cap_table *cons_t, uint8_t *sink, size_t total, size_t chunk_w, size_t chunk_r, |
| 166 | unsigned flags, unsigned prod_every, unsigned cons_every, size_t revoke_at) |
| 167 | { |
| 168 | *w = (struct pd_world){ .pool = pool, .prod_t = prod_t, .cons_t = cons_t, .sink = sink, .total = total, |
| 169 | .chunk_w = chunk_w > PD_CHUNK_MAX ? PD_CHUNK_MAX : chunk_w, |
| 170 | .chunk_r = chunk_r > PD_CHUNK_MAX ? PD_CHUNK_MAX : chunk_r, .flags = flags, |
| 171 | .prod_every = prod_every, .cons_every = cons_every, .revoke_at = revoke_at }; |
| 172 | w->hooks = (struct ck_pipe_hooks){ w, pd_wait, pd_wake, 0 }; |
| 173 | ck_pipe_pool_init(pool, &w->hooks); |
| 174 | struct ck_handle r0; |
| 175 | w->setup_err = ck_pipe_create(pool, prod_t, &r0, &w->w); |
| 176 | if (w->setup_err != OSH_E_OK) |
| 177 | return; |
| 178 | w->setup_err = ck_stream_dup(pool, prod_t, r0, cons_t, CK_R_READ | (revoke_at ? CK_R_REVOKE : 0), &w->r); |
| 179 | unsigned c = ck_stream_close(pool, prod_t, r0); /* the producer keeps only the write end */ |
| 180 | if (w->setup_err == OSH_E_OK) |
| 181 | w->setup_err = c; |
| 182 | if (w->setup_err != OSH_E_OK) { |
| 183 | ck_pipe_release_table(pool, prod_t); |
| 184 | ck_pipe_release_table(pool, cons_t); |
| 185 | return; |
| 186 | } |
| 187 | static const uint8_t classes[1] = { 0 }; |
| 188 | ck_sched_init(&w->sched, 1, classes, CK_SCHED_MAX_TASKS, 1); |
| 189 | w->tasks[0] = (struct pd_task){ .id = 0x50524f44u, .step = pd_producer }; /* "PROD" */ |
| 190 | w->tasks[1] = (struct pd_task){ .id = 0x434f4e53u, .step = pd_consumer }; /* "CONS" */ |
| 191 | ck_sched_enqueue(&w->sched, w->tasks[0].id, CK_PRIO_NORMAL); |
| 192 | ck_sched_enqueue(&w->sched, w->tasks[1].id, CK_PRIO_NORMAL); |
| 193 | while ((!w->tasks[0].done || !w->tasks[1].done) && w->ticks < PD_TICK_MAX) |
| 194 | if (!pd_tick(w)) |
| 195 | break; |
| 196 | /* section 12: whatever happened, no end stays open */ |
| 197 | ck_pipe_release_table(pool, prod_t); |
| 198 | ck_pipe_release_table(pool, cons_t); |
| 199 | } |
| 200 | |
| 201 | /* Bytes of the sink that differ from the payload, over what was received. */ |
| 202 | static inline size_t pd_mismatches(const struct pd_world *w) |
| 203 | { |
| 204 | size_t bad = 0, n = w->recvd < w->total ? w->recvd : w->total; |
| 205 | for (size_t i = 0; i < n; i++) |
| 206 | bad += w->sink[i] != pd_byte(i); |
| 207 | return bad; |
| 208 | } |
| 209 | |
| 210 | static inline size_t pd_nul_count(const struct pd_world *w) |
| 211 | { |
| 212 | size_t z = 0, n = w->recvd < w->total ? w->recvd : w->total; |
| 213 | for (size_t i = 0; i < n; i++) |
| 214 | z += w->sink[i] == 0; |
| 215 | return z; |
| 216 | } |
| 217 | |
| 218 | /* 1 if the table holds no live slot at all (tombstones included). */ |
| 219 | static inline int pd_table_empty(const struct ck_cap_table *t) |
| 220 | { |
| 221 | for (unsigned i = 0; i < CK_CAP_SLOTS; i++) |
| 222 | if (t->slots[i].live) |
| 223 | return 0; |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | #endif |