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/native/kernel/core/pipe.c # 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 / native / kernel / core / pipe.c` 667 lines ยท 23191 bytes Raw Blame History 1 /* pipe.c -- see pipe.h. Bounded byte pipes over ipc.h capability tables; no allocation, no libc. 2 * 3 * The pool lock is the kernel's shared spinlock (core/spinlock.h; its ticket lock started here as pool_lock / 4 * pool_unlock): fair, and it masks interrupts on this CPU while held. Every pipe field, refcount, stat and the 5 * live count is touched only while the lock is held. wait and wake run after it is dropped (interrupts restored), 6 * so a hook can re-enter. 7 */ 8 #include "pipe.h" 9 10 static void bytes_zero(uint8_t *d, size_t n) 11 { 12 for (size_t i = 0; i < n; i++) 13 d[i] = 0; 14 } 15 16 /* The snapshot sits on the stack and is dead after the call. A plain store can be deleted; a volatile store cannot. */ 17 static void snap_zero(uint8_t *d, size_t n) 18 { 19 volatile uint8_t *p = d; 20 for (size_t i = 0; i < n; i++) 21 p[i] = 0; 22 } 23 24 static void stats_zero(struct ck_pipe_stats *s) 25 { 26 *s = (struct ck_pipe_stats){ 0 }; 27 } 28 29 static void pool_lock(struct ck_pipe_pool *p) 30 { 31 ck_spin_lock(&p->lock); 32 } 33 34 static void pool_unlock(struct ck_pipe_pool *p) 35 { 36 ck_spin_unlock(&p->lock); 37 } 38 39 void ck_pipe_pool_init(struct ck_pipe_pool *p, const struct ck_pipe_hooks *hooks) 40 { 41 for (unsigned i = 0; i < CK_PIPE_POOL_PIPES; i++) { 42 struct ck_pipe *q = &p->pipes[i]; 43 bytes_zero(q->buf, CK_PIPE_CAPACITY); 44 q->head = q->len = q->epoch = 0; 45 q->reader_refs = q->writer_refs = 0; 46 q->flags = 0; 47 q->waiting[0] = q->waiting[1] = 0; 48 stats_zero(&q->stats); 49 } 50 p->hooks = hooks; 51 stats_zero(&p->total); 52 p->live = 0; 53 /* Unlocked. Init is single-threaded, before the pool is shared. */ 54 ck_spin_init(&p->lock); 55 } 56 57 int ck_pipe_pool_empty(struct ck_pipe_pool *p) 58 { 59 pool_lock(p); 60 int empty = p->live == 0; 61 for (unsigned i = 0; i < CK_PIPE_POOL_PIPES; i++) 62 if (p->pipes[i].flags & CK_PIPE_LIVE) 63 empty = 0; 64 pool_unlock(p); 65 return empty; 66 } 67 68 /* Every counter is kept twice: this incarnation and the pool total. */ 69 #define BUMP(p, q, field, by) \ 70 do { \ 71 (q)->stats.field += (by); \ 72 (p)->total.field += (by); \ 73 } while (0) 74 75 /* Wakes recorded under the lock and fired after it is dropped. One close wakes one end; a table has CK_CAP_SLOTS. */ 76 #define CK_PIPE_WAKE_NOTE CK_CAP_SLOTS 77 struct ck_wake_note { 78 unsigned n; 79 unsigned idx[CK_PIPE_WAKE_NOTE]; 80 unsigned end[CK_PIPE_WAKE_NOTE]; 81 }; 82 83 static void note_wake(struct ck_wake_note *note, struct ck_pipe_pool *p, unsigned idx, unsigned end) 84 { 85 struct ck_pipe *q = &p->pipes[idx]; 86 if (!q->waiting[end] || !p->hooks || !p->hooks->wake) 87 return; 88 BUMP(p, q, wakeups, 1); 89 if (note->n < CK_PIPE_WAKE_NOTE) { 90 note->idx[note->n] = idx; 91 note->end[note->n] = end; 92 note->n++; 93 } 94 } 95 96 static void fire_wakes(struct ck_pipe_pool *p, const struct ck_wake_note *note) 97 { 98 if (!note->n || !p->hooks || !p->hooks->wake) 99 return; 100 for (unsigned i = 0; i < note->n; i++) 101 p->hooks->wake(p->hooks->ctx, note->idx[i], note->end[i]); 102 } 103 104 static unsigned data_right(unsigned end) 105 { 106 return end == CK_PIPE_END_READ ? CK_R_READ : CK_R_WRITE; 107 } 108 109 /* The effect-boundary check, run inside every operation at the moment of its effect. Lock is held. 110 * need: rights the capability must hold. want_end: the end the operation needs, or -1 for either. 111 * OK with *idx and *end set; otherwise the refusal. A REVOKED pipe is reported only when live_ok is 0. */ 112 static unsigned resolve(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, unsigned need, 113 int want_end, int live_ok, unsigned *idx, unsigned *end) 114 { 115 if (!t || h.generation == 0 || h.index >= t->n) 116 return OSH_E_INVALID_ARG; 117 uint32_t res = 0; 118 int r = ck_cap_lookup(t, h, need, &res); 119 if (r == CK_CAP_INVALID) 120 return OSH_E_STALE; /* slot closed, or reused under a newer generation */ 121 if (r != CK_CAP_OK) 122 return OSH_E_DENIED; /* the capability lacks a right this effect needs */ 123 if ((res >> 24) != CK_PIPE_RES_TAG) 124 return OSH_E_DENIED; /* not a stream capability */ 125 unsigned i = (res >> 20) & 7u, e = (res >> 23) & 1u; 126 uint32_t epoch = res & CK_PIPE_EPOCH_MAX; 127 if (i >= CK_PIPE_POOL_PIPES) 128 return OSH_E_DENIED; 129 if (want_end >= 0 && e != (unsigned)want_end) 130 return OSH_E_DENIED; /* a read end cannot write, a write end cannot read */ 131 const struct ck_pipe *q = &p->pipes[i]; 132 if (!(q->flags & CK_PIPE_LIVE) || q->epoch != epoch) 133 return OSH_E_STALE; /* section 4.4: an id from an earlier incarnation */ 134 if (!live_ok && (q->flags & CK_PIPE_REVOKED)) 135 return OSH_E_REVOKED; 136 *idx = i; 137 *end = e; 138 return OSH_E_OK; 139 } 140 141 static unsigned refuse(struct ck_pipe_pool *p, unsigned e) 142 { 143 if (e == OSH_E_DENIED || e == OSH_E_STALE || e == OSH_E_REVOKED) 144 p->total.refused++; 145 return e; 146 } 147 148 unsigned ck_pipe_create(struct ck_pipe_pool *p, struct ck_cap_table *t, struct ck_handle *rd, struct ck_handle *wr) 149 { 150 if (!t || !rd || !wr) 151 return OSH_E_INVALID_ARG; 152 pool_lock(p); 153 unsigned idx = CK_PIPE_POOL_PIPES; 154 for (unsigned i = 0; i < CK_PIPE_POOL_PIPES; i++) 155 if (!(p->pipes[i].flags & CK_PIPE_LIVE) && p->pipes[i].epoch < CK_PIPE_EPOCH_MAX) { 156 idx = i; 157 break; 158 } 159 if (idx == CK_PIPE_POOL_PIPES) { 160 pool_unlock(p); 161 return OSH_E_LIMIT; 162 } 163 struct ck_pipe *q = &p->pipes[idx]; 164 uint32_t epoch = q->epoch + 1; 165 struct ck_handle r, w; 166 if (ck_cap_insert(t, ck_pipe_resource(idx, CK_PIPE_END_READ, epoch), CK_R_READ | CK_PIPE_CTL_RIGHTS, &r) != 167 CK_CAP_OK) { 168 pool_unlock(p); 169 return OSH_E_LIMIT; 170 } 171 if (ck_cap_insert(t, ck_pipe_resource(idx, CK_PIPE_END_WRITE, epoch), CK_R_WRITE | CK_PIPE_CTL_RIGHTS, &w) != 172 CK_CAP_OK) { 173 ck_cap_remove(t, r); /* nothing stays allocated on failure */ 174 pool_unlock(p); 175 return OSH_E_LIMIT; 176 } 177 q->epoch = epoch; 178 q->head = q->len = 0; 179 q->reader_refs = q->writer_refs = 1; 180 q->flags = CK_PIPE_LIVE; 181 q->waiting[0] = q->waiting[1] = 0; 182 stats_zero(&q->stats); 183 p->live++; 184 *rd = r; 185 *wr = w; 186 pool_unlock(p); 187 return OSH_E_OK; 188 } 189 190 static unsigned read_once(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, uint8_t *buf, 191 size_t max, size_t *n, unsigned *idx_out, struct ck_wake_note *note) 192 { 193 unsigned idx, end; 194 unsigned e = resolve(p, t, h, CK_R_READ, CK_PIPE_END_READ, 0, &idx, &end); 195 if (e != OSH_E_OK) 196 return refuse(p, e); 197 struct ck_pipe *q = &p->pipes[idx]; 198 *idx_out = idx; 199 if (q->flags & CK_PIPE_CANCEL_READ) { 200 q->flags &= ~CK_PIPE_CANCEL_READ; 201 BUMP(p, q, cancellations, 1); 202 return OSH_E_INTERRUPTED; 203 } 204 if (max == 0) 205 return OSH_E_OK; 206 if (q->len == 0) { 207 if (q->writer_refs == 0) { 208 BUMP(p, q, eof, 1); 209 return OSH_E_OK; /* every writer closed and the buffer is drained: EOF */ 210 } 211 BUMP(p, q, would_block, 1); 212 return OSH_E_WOULD_BLOCK; 213 } 214 size_t take = max < q->len ? max : q->len; 215 for (size_t i = 0; i < take; i++) { 216 buf[i] = q->buf[q->head]; 217 q->buf[q->head] = 0; /* consumed bytes do not stay resident */ 218 q->head = (q->head + 1) % CK_PIPE_CAPACITY; 219 } 220 q->len -= (uint32_t)take; 221 if (q->len == 0) 222 q->head = 0; 223 *n = take; 224 BUMP(p, q, bytes_read, take); 225 BUMP(p, q, reads, 1); 226 note_wake(note, p, idx, CK_PIPE_END_WRITE); 227 return OSH_E_OK; 228 } 229 230 static unsigned write_once(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, 231 const uint8_t *buf, size_t len, size_t *n, unsigned *idx_out, struct ck_wake_note *note) 232 { 233 unsigned idx, end; 234 #ifdef CK_PIPE_MUTANT_SKIP_RIGHTS 235 /* TEST-ONLY mutant (make pipe-mutant): the write right and the end are not checked. test_pipe must fail. */ 236 unsigned e = resolve(p, t, h, 0, -1, 0, &idx, &end); 237 #else 238 unsigned e = resolve(p, t, h, CK_R_WRITE, CK_PIPE_END_WRITE, 0, &idx, &end); 239 #endif 240 if (e != OSH_E_OK) 241 return refuse(p, e); 242 struct ck_pipe *q = &p->pipes[idx]; 243 *idx_out = idx; 244 if (q->flags & CK_PIPE_CANCEL_WRITE) { 245 q->flags &= ~CK_PIPE_CANCEL_WRITE; 246 BUMP(p, q, cancellations, 1); 247 return OSH_E_INTERRUPTED; 248 } 249 if (q->reader_refs == 0) { 250 BUMP(p, q, broken_pipe, 1); 251 return OSH_E_BROKEN_PIPE; 252 } 253 if (len == 0) 254 return OSH_E_OK; 255 size_t room = CK_PIPE_CAPACITY - q->len; 256 if (room == 0) { 257 BUMP(p, q, would_block, 1); 258 return OSH_E_WOULD_BLOCK; 259 } 260 size_t put = len < room ? len : room; 261 uint32_t at = (q->head + q->len) % CK_PIPE_CAPACITY; 262 for (size_t i = 0; i < put; i++) { 263 q->buf[at] = buf[i]; 264 at = (at + 1) % CK_PIPE_CAPACITY; 265 } 266 q->len += (uint32_t)put; 267 *n = put; 268 BUMP(p, q, bytes_written, put); 269 BUMP(p, q, writes, 1); 270 if (q->len > q->stats.max_resident) 271 q->stats.max_resident = q->len; 272 if (q->len > p->total.max_resident) 273 p->total.max_resident = q->len; 274 note_wake(note, p, idx, CK_PIPE_END_READ); 275 return OSH_E_OK; 276 } 277 278 /* Park on (idx, end). Lock is held on entry and on return. The hook runs without the lock. */ 279 static unsigned park(struct ck_pipe_pool *p, unsigned idx, unsigned end) 280 { 281 if (!p->hooks || !p->hooks->wait) 282 return OSH_E_WOULD_BLOCK; 283 struct ck_pipe *q = &p->pipes[idx]; 284 uint32_t epoch = q->epoch; 285 q->waiting[end]++; 286 BUMP(p, q, waits, 1); 287 pool_unlock(p); 288 if (p->hooks->before_wait) 289 p->hooks->before_wait(p->hooks->ctx); 290 unsigned w = p->hooks->wait(p->hooks->ctx, idx, end); 291 pool_lock(p); 292 q = &p->pipes[idx]; 293 /* Freed and recreated while we were out: that incarnation's waiting[] is not ours to decrement. */ 294 if (q->epoch == epoch && q->waiting[end]) 295 q->waiting[end]--; 296 if (w != OSH_E_OK) { 297 if (w == OSH_E_INTERRUPTED && q->epoch == epoch) 298 BUMP(p, q, cancellations, 1); 299 return w >= OSH_E_COUNT ? OSH_E_IO : w; /* section 8.6: an unknown code is IO, never OK */ 300 } 301 return OSH_E_OK; 302 } 303 304 unsigned ck_stream_read(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, uint8_t *buf, 305 size_t max, unsigned flags, size_t *n) 306 { 307 if (!n || (max && !buf) || (flags & ~CK_PIPE_F_BLOCK)) 308 return OSH_E_INVALID_ARG; 309 pool_lock(p); 310 for (;;) { 311 struct ck_wake_note note = { 0 }; 312 unsigned idx = 0; 313 *n = 0; 314 unsigned e = read_once(p, t, h, buf, max, n, &idx, ¬e); 315 if (e != OSH_E_WOULD_BLOCK || !(flags & CK_PIPE_F_BLOCK)) { 316 pool_unlock(p); 317 fire_wakes(p, ¬e); 318 return e; 319 } 320 e = park(p, idx, CK_PIPE_END_READ); 321 if (e != OSH_E_OK) { 322 pool_unlock(p); 323 return e; 324 } 325 } 326 } 327 328 unsigned ck_stream_write(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h, const uint8_t *buf, 329 size_t len, unsigned flags, size_t *n) 330 { 331 if (!n || (len && !buf) || (flags & ~CK_PIPE_F_BLOCK)) 332 return OSH_E_INVALID_ARG; 333 /* Snapshot only on the path that parks. The copy happens before park, while the lock is still held, 334 * so the retry cannot observe a caller buffer that changed during the wait. */ 335 uint8_t snap[CK_PIPE_WRITE_SNAP]; 336 const uint8_t *src = buf; 337 size_t src_len = len; 338 int snapped = 0; 339 pool_lock(p); 340 for (;;) { 341 struct ck_wake_note note = { 0 }; 342 unsigned idx = 0; 343 *n = 0; 344 unsigned e = write_once(p, t, h, src, src_len, n, &idx, ¬e); 345 if (e != OSH_E_WOULD_BLOCK || !(flags & CK_PIPE_F_BLOCK)) { 346 pool_unlock(p); 347 fire_wakes(p, ¬e); 348 if (snapped) 349 snap_zero(snap, CK_PIPE_WRITE_SNAP); 350 return e; 351 } 352 if (!snapped && len > 0) { 353 size_t take = len < CK_PIPE_WRITE_SNAP ? len : CK_PIPE_WRITE_SNAP; 354 for (size_t i = 0; i < take; i++) 355 snap[i] = buf[i]; 356 src = snap; 357 src_len = take; 358 snapped = 1; 359 } 360 e = park(p, idx, CK_PIPE_END_WRITE); 361 if (e != OSH_E_OK) { 362 pool_unlock(p); 363 if (snapped) 364 snap_zero(snap, CK_PIPE_WRITE_SNAP); 365 return e; 366 } 367 } 368 } 369 370 static void pipe_free(struct ck_pipe_pool *p, struct ck_pipe *q) 371 { 372 bytes_zero(q->buf, CK_PIPE_CAPACITY); 373 q->head = q->len = 0; 374 q->flags = 0; 375 q->waiting[0] = q->waiting[1] = 0; 376 p->live--; 377 } 378 379 static unsigned close_locked(struct ck_pipe_pool *p, struct ck_cap_table *t, struct ck_handle h, 380 struct ck_wake_note *note) 381 { 382 if (!t || h.generation == 0 || h.index >= t->n) 383 return OSH_E_INVALID_ARG; 384 uint32_t res = 0; 385 if (ck_cap_lookup(t, h, 0, &res) != CK_CAP_OK) 386 return refuse(p, OSH_E_STALE); /* already closed, or the slot belongs to someone newer */ 387 if ((res >> 24) != CK_PIPE_RES_TAG || ((res >> 20) & 7u) >= CK_PIPE_POOL_PIPES) 388 return refuse(p, OSH_E_DENIED); /* not ours to close here */ 389 unsigned idx, end; 390 unsigned e = resolve(p, t, h, 0, -1, 1, &idx, &end); 391 ck_cap_remove(t, h); /* the handle is released whatever the pipe's state */ 392 if (e != OSH_E_OK) 393 return refuse(p, e); 394 struct ck_pipe *q = &p->pipes[idx]; 395 if (end == CK_PIPE_END_READ) { 396 if (q->reader_refs) 397 q->reader_refs--; 398 if (q->reader_refs == 0) 399 note_wake(note, p, idx, CK_PIPE_END_WRITE); /* a blocked writer now sees BROKEN_PIPE */ 400 } else { 401 if (q->writer_refs) 402 q->writer_refs--; 403 if (q->writer_refs == 0) 404 note_wake(note, p, idx, CK_PIPE_END_READ); /* a blocked reader now sees EOF once drained */ 405 } 406 if (q->reader_refs == 0 && q->writer_refs == 0) 407 pipe_free(p, q); 408 return OSH_E_OK; 409 } 410 411 unsigned ck_stream_close(struct ck_pipe_pool *p, struct ck_cap_table *t, struct ck_handle h) 412 { 413 if (!t || h.generation == 0 || h.index >= t->n) 414 return OSH_E_INVALID_ARG; 415 struct ck_wake_note note = { 0 }; 416 pool_lock(p); 417 unsigned e = close_locked(p, t, h, ¬e); 418 pool_unlock(p); 419 fire_wakes(p, ¬e); 420 return e; 421 } 422 423 unsigned ck_stream_dup(struct ck_pipe_pool *p, struct ck_cap_table *from, struct ck_handle h, struct ck_cap_table *to, 424 unsigned rights, struct ck_handle *out) 425 { 426 if (!to || !out) 427 return OSH_E_INVALID_ARG; 428 pool_lock(p); 429 unsigned need = to == from ? CK_R_DERIVE : CK_R_GRANT; 430 unsigned idx, end; 431 unsigned e = resolve(p, from, h, need, -1, 0, &idx, &end); 432 if (e != OSH_E_OK) { 433 e = refuse(p, e); 434 pool_unlock(p); 435 return e; 436 } 437 unsigned have = 0; 438 ck_cap_rights(from, h, &have); 439 unsigned dr = data_right(end); 440 if (!(rights & dr) || (rights & ~(dr | CK_PIPE_CTL_RIGHTS))) { 441 pool_unlock(p); 442 return OSH_E_INVALID_ARG; /* a dup carries its end's data right and nothing of the other end */ 443 } 444 if (rights & ~have) { 445 e = refuse(p, OSH_E_DENIED); /* attenuation only: never more than the source holds */ 446 pool_unlock(p); 447 return e; 448 } 449 struct ck_pipe *q = &p->pipes[idx]; 450 struct ck_handle c; 451 if (ck_cap_insert(to, ck_pipe_resource(idx, end, q->epoch), rights, &c) != CK_CAP_OK) { 452 pool_unlock(p); 453 return OSH_E_LIMIT; 454 } 455 if (end == CK_PIPE_END_READ) 456 q->reader_refs++; 457 else 458 q->writer_refs++; 459 *out = c; 460 pool_unlock(p); 461 return OSH_E_OK; 462 } 463 464 unsigned ck_pipe_revoke(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h) 465 { 466 struct ck_wake_note note = { 0 }; 467 pool_lock(p); 468 unsigned idx, end; 469 unsigned e = resolve(p, t, h, CK_R_REVOKE, -1, 0, &idx, &end); 470 if (e != OSH_E_OK) { 471 e = refuse(p, e); 472 pool_unlock(p); 473 return e; 474 } 475 struct ck_pipe *q = &p->pipes[idx]; 476 q->flags |= CK_PIPE_REVOKED; 477 q->flags &= ~(CK_PIPE_CANCEL_READ | CK_PIPE_CANCEL_WRITE); 478 bytes_zero(q->buf, CK_PIPE_CAPACITY); /* buffered bytes are never delivered after a revoke */ 479 q->head = q->len = 0; 480 BUMP(p, q, revocations, 1); 481 note_wake(¬e, p, idx, CK_PIPE_END_READ); 482 note_wake(¬e, p, idx, CK_PIPE_END_WRITE); 483 pool_unlock(p); 484 fire_wakes(p, ¬e); 485 return OSH_E_OK; 486 } 487 488 unsigned ck_pipe_cancel(struct ck_pipe_pool *p, const struct ck_cap_table *t, struct ck_handle h) 489 { 490 struct ck_wake_note note = { 0 }; 491 pool_lock(p); 492 unsigned idx, end, rights = 0; 493 unsigned e = resolve(p, t, h, 0, -1, 0, &idx, &end); 494 if (e != OSH_E_OK) { 495 e = refuse(p, e); 496 pool_unlock(p); 497 return e; 498 } 499 ck_cap_rights(t, h, &rights); 500 if (!(rights & data_right(end))) { /* the end's own data right: READ to interrupt reads, WRITE to interrupt writes */ 501 e = refuse(p, OSH_E_DENIED); 502 pool_unlock(p); 503 return e; 504 } 505 p->pipes[idx].flags |= end == CK_PIPE_END_READ ? CK_PIPE_CANCEL_READ : CK_PIPE_CANCEL_WRITE; 506 note_wake(¬e, p, idx, end); 507 pool_unlock(p); 508 fire_wakes(p, ¬e); 509 return OSH_E_OK; 510 } 511 512 unsigned ck_pipe_release_table(struct ck_pipe_pool *p, struct ck_cap_table *t) 513 { 514 struct ck_wake_note note = { 0 }; 515 unsigned closed = 0; 516 pool_lock(p); 517 for (unsigned i = 0; i < t->n; i++) { 518 const struct ck_cap_slot *s = &t->slots[i]; 519 if (!s->live || s->tombstone || (s->resource >> 24) != CK_PIPE_RES_TAG) 520 continue; 521 struct ck_handle h = { i, s->generation }; 522 if (close_locked(p, t, h, ¬e) == OSH_E_OK) 523 closed++; 524 } 525 pool_unlock(p); 526 fire_wakes(p, ¬e); 527 return closed; 528 } 529 530 /* ---- versioned op table ---- */ 531 static const uint8_t op_args[] = { [CK_PIPE_OP_CREATE] = 0, [CK_PIPE_OP_READ] = 2, [CK_PIPE_OP_WRITE] = 2, 532 [CK_PIPE_OP_CLOSE] = 1, [CK_PIPE_OP_DUP] = 3 }; 533 #define OP_MAX CK_PIPE_OP_DUP 534 535 #ifndef CK_PIPE_MUTANT_SKIP_SLICE 536 /* 1 when [s, s+len) lies in [lo, hi) and hi is at most the user-VA top. len 0 passes. Otherwise no s+len add: a 537 * wrapping length fails. */ 538 static int slice_ok(const void *s, size_t len, uintptr_t lo, uintptr_t hi) 539 { 540 uintptr_t b = (uintptr_t)s; 541 if (len == 0) 542 return 1; 543 #ifndef CK_PIPE_MUTANT_NO_USER_TOP 544 if (hi > CK_PIPE_USER_TOP) 545 return 0; 546 #endif 547 if (lo >= hi || b < lo || b >= hi) 548 return 0; 549 return len <= hi - b; 550 } 551 #endif 552 553 #ifndef CK_PIPE_MUTANT_NO_BOUNCE 554 /* Copy between the caller's slice and the kernel bounce buffer. Never called with the pool lock held. */ 555 static void slice_copy(uint8_t *d, const uint8_t *s, size_t n) 556 { 557 for (size_t i = 0; i < n; i++) 558 d[i] = s[i]; 559 } 560 #endif 561 562 /* READ / WRITE through the bounce buffer: the slice is only touched here, before ck_stream_write takes the lock or 563 * after ck_stream_read dropped it. At most CK_PIPE_BOUNCE bytes per call. */ 564 static unsigned slice_rw(struct ck_pipe_pool *p, struct ck_cap_table *caller, struct ck_handle h, unsigned op, 565 uint8_t *slice, size_t slice_len, unsigned flags, size_t *n) 566 { 567 #ifdef CK_PIPE_MUTANT_NO_BOUNCE 568 /* TEST-ONLY (make pipe-bounce-mutant): the ring copy reads and writes the slice under the lock. test_pipe must 569 * fail. */ 570 return op == CK_PIPE_OP_READ ? ck_stream_read(p, caller, h, slice, slice_len, flags, n) 571 : ck_stream_write(p, caller, h, slice, slice_len, flags, n); 572 #else 573 uint8_t bounce[CK_PIPE_BOUNCE]; 574 size_t len = slice_len < CK_PIPE_BOUNCE ? slice_len : CK_PIPE_BOUNCE; 575 unsigned e; 576 if (op == CK_PIPE_OP_WRITE) { 577 slice_copy(bounce, slice, len); 578 e = ck_stream_write(p, caller, h, len ? bounce : 0, len, flags, n); 579 } else { 580 e = ck_stream_read(p, caller, h, len ? bounce : 0, len, flags, n); 581 if (e == OSH_E_OK && *n) 582 slice_copy(slice, bounce, *n); 583 } 584 snap_zero(bounce, len); /* pipe bytes do not stay on the kernel stack */ 585 return e; 586 #endif 587 } 588 589 unsigned ck_pipe_dispatch(struct ck_pipe_pool *p, struct ck_cap_table *caller, struct ck_cap_table *peer, 590 const uint64_t *frame, size_t ncells, uint8_t *slice, size_t slice_len, 591 uintptr_t user_lo, uintptr_t user_hi, struct ck_pipe_ret *ret) 592 { 593 struct ck_pipe_ret r = { 0 }; 594 unsigned e; 595 struct ck_handle h = { 0, 0 }; 596 if (!frame || ncells < 4 || frame[0] != CK_PIPE_FRAME_MAGIC) { 597 e = OSH_E_INVALID_ARG; 598 goto done; 599 } 600 if (frame[1] != CK_PIPE_ABI_VERSION) { 601 e = OSH_E_NOT_SUPPORTED; /* section 3: never guess at another version */ 602 goto done; 603 } 604 if (frame[3] != 0) { 605 e = OSH_E_INVALID_ARG; 606 goto done; 607 } 608 if (frame[2] == 0 || frame[2] > OP_MAX) { 609 e = OSH_E_NOT_SUPPORTED; /* unknown op: refused before any effect */ 610 goto done; 611 } 612 unsigned op = (unsigned)frame[2]; 613 if (ncells != 4u + op_args[op] || (op != CK_PIPE_OP_CREATE && ck_handle_from_raw(frame[4], &h) != 0)) { 614 e = OSH_E_INVALID_ARG; 615 goto done; 616 } 617 size_t n = 0; 618 switch (op) { 619 case CK_PIPE_OP_CREATE: { 620 struct ck_handle rd, wr; 621 e = ck_pipe_create(p, caller, &rd, &wr); 622 if (e == OSH_E_OK) { 623 r.value = ck_handle_raw(rd); 624 r.value2 = ck_handle_raw(wr); 625 } 626 break; 627 } 628 case CK_PIPE_OP_READ: 629 case CK_PIPE_OP_WRITE: 630 if (frame[5] & ~(uint64_t)CK_PIPE_F_BLOCK) { 631 e = OSH_E_INVALID_ARG; 632 break; 633 } 634 #ifdef CK_PIPE_MUTANT_SKIP_SLICE 635 /* TEST-ONLY (make pipe-slice-mutant): the user window is not checked. test_pipe must fail. */ 636 (void)user_lo; 637 (void)user_hi; 638 #else 639 if (!slice_ok(slice, slice_len, user_lo, user_hi)) { 640 e = OSH_E_INVALID_ARG; 641 break; 642 } 643 #endif 644 e = slice_rw(p, caller, h, op, slice, slice_len, (unsigned)frame[5], &n); 645 r.value = n; 646 break; 647 case CK_PIPE_OP_CLOSE: 648 e = ck_stream_close(p, caller, h); 649 break; 650 default: { /* CK_PIPE_OP_DUP */ 651 struct ck_handle c; 652 if (frame[5] & ~(uint64_t)CK_R_ALL || frame[6] > 1 || (frame[6] == 1 && !peer)) { 653 e = OSH_E_INVALID_ARG; 654 break; 655 } 656 e = ck_stream_dup(p, caller, h, frame[6] ? peer : caller, (unsigned)frame[5], &c); 657 if (e == OSH_E_OK) 658 r.value = ck_handle_raw(c); 659 break; 660 } 661 } 662 done: 663 r.error = e; 664 if (ret) 665 *ret = r; 666 return e; 667 }