omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / net / wire / wire_loop.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/net/wire/wire_loop.om: Zero-userspace in-kernel polled TCP <-> GPU wire loop (Piece 6). |
| 3 | note Pure seeded Omega (OM-2). Zero Linux syscalls, zero heap, fixed globals only. |
| 4 | note Composes after: aienux/net/tcp/tcp.om, aienux/net/wire/omtk.om, and a platform/NIC/GPU seam unit |
| 5 | note defining: |
| 6 | note wl_cntvct -> ulong Piece 1 CNTVCT_EL0 counter |
| 7 | note wl_cntfrq -> ulong Piece 1 CNTFRQ_EL0 frequency (Hz) |
| 8 | note wl_dsb_ish -> void Piece 1 ARM64 dsb ish barrier |
| 9 | note wl_nic_rx -> ulong Piece 4 poll 1 Ethernet frame into Trx[0..n), 0 if none |
| 10 | note wl_nic_tx n ulong -> long Piece 4 transmit Ttx[0..n), 0 ok, <0 err |
| 11 | note wl_nic_reclaim -> void Piece 4 reclaim completed TX descriptors |
| 12 | note wl_q38_reset_kv keep_pos ulong -> long Piece 5 q38_reset_kv (pb_words > 0, or -2 E_UNSUP) |
| 13 | note wl_q38_stage_step tok uint kv uint mode uchar -> long Piece 5 q38_stage_step (pb_words > 0) |
| 14 | note wl_q38_verify_step -> long Piece 5 q38_verify_step (0 ok, <0 Trap #13 guard) |
| 15 | note wl_q38_read_tok -> uint Piece 5 read verified output tok_id from Normal-NC |
| 16 | note wl_gpu_kick pb_words uint -> long Piece 3 cbgpent + cbkickbar0 (DSB SY chain) + cmsub |
| 17 | note wl_gpu_poll -> long Piece 3 non-blocking cmpoll (>0 done, 0 wait, <0 err) |
| 18 | note wl_gpu_irq_check -> long Piece 3 iqservice (>=0 ok, -13 HALT, -14 lockdown) |
| 19 | note wl_gpu_quarantine -> void Piece 3 cmquar quarantine latch |
| 20 | note wl_should_stop -> ulong 0 on bare-metal forever; nonzero exits host test |
| 21 | |
| 22 | note Loop states |
| 23 | const WL_IDLE ulong 0 |
| 24 | const WL_SUBMIT ulong 1 |
| 25 | const WL_WAIT ulong 2 |
| 26 | const WL_FAULT ulong 3 |
| 27 | |
| 28 | note In-flight GPU operation kinds |
| 29 | const WL_OPNONE ulong 0 |
| 30 | const WL_OPRESET ulong 1 |
| 31 | const WL_OPPREF ulong 2 |
| 32 | const WL_OPDEC ulong 3 |
| 33 | |
| 34 | note State vector indices (wl_st[0..31]) |
| 35 | const WL_S_STATE ulong 0 |
| 36 | const WL_S_OP ulong 1 |
| 37 | const WL_S_PORT ulong 2 |
| 38 | const WL_S_MAXCTX ulong 3 |
| 39 | const WL_S_VOCAB ulong 4 |
| 40 | const WL_S_KVPOS ulong 5 |
| 41 | const WL_S_REQID ulong 6 |
| 42 | const WL_S_CANCEL ulong 7 |
| 43 | const WL_S_SUBMS ulong 8 |
| 44 | const WL_S_ACTMS ulong 9 |
| 45 | const WL_S_CONN ulong 10 |
| 46 | const WL_S_PFIDX ulong 11 |
| 47 | const WL_S_PFCNT ulong 12 |
| 48 | const WL_S_DCSEQ ulong 13 |
| 49 | const WL_S_DCMAX ulong 14 |
| 50 | const WL_S_DCEOS ulong 15 |
| 51 | const WL_S_CURTOK ulong 16 |
| 52 | const WL_S_GERR ulong 17 |
| 53 | const WL_S_RXN ulong 18 |
| 54 | const WL_S_RXDROP ulong 19 |
| 55 | const WL_S_LASTTOK ulong 20 |
| 56 | const WL_S_IDLETMO ulong 21 |
| 57 | const WL_S_STEPTMO ulong 22 |
| 58 | const WL_S_TXH ulong 23 |
| 59 | const WL_S_TXN ulong 24 |
| 60 | const WL_S_DROPS ulong 25 |
| 61 | const WL_S_HASFIRST ulong 26 |
| 62 | const WL_S_CURMODE ulong 27 |
| 63 | const WL_S_REARM ulong 28 |
| 64 | |
| 65 | const WL_DEFPORT ulong 18100 |
| 66 | const WL_DEFCTX ulong 262144 |
| 67 | const WL_DEFVOC ulong 248320 |
| 68 | const WL_DEFIDLE ulong 30000 |
| 69 | const WL_DEFSTEP ulong 5000 |
| 70 | const WL_DECMAX ulong 16384 |
| 71 | const WL_TXCAP ulong 4096 |
| 72 | const WL_U32MAX ulong 4294967295 |
| 73 | const WL_CBLENMAX long 2097151 |
| 74 | |
| 75 | global wl_st ulong 32 |
| 76 | global wl_rx uchar 16400 |
| 77 | global wl_ptoks uint 4096 |
| 78 | global wl_txq uchar 4096 |
| 79 | |
| 80 | note Required by aienux/net/tcp/tcp.om: transmit Ttx[0..n) through Piece 4 NIC. |
| 81 | fn tlinkout n ulong to void |
| 82 | rc = wl_nic_tx n |
| 83 | if lt rc 0 |
| 84 | tstat 30 |
| 85 | end |
| 86 | end |
| 87 | |
| 88 | note Overflow-safe monotonic millisecond clock from Piece 1 CNTVCT_EL0 / CNTFRQ_EL0. |
| 89 | note Returns 0 if frq < 1000 so (t / frq) * 1000 cannot overflow u64 for any t. |
| 90 | fn wl_now_ms to ulong |
| 91 | t = wl_cntvct |
| 92 | frq = wl_cntfrq |
| 93 | if lt frq 1000 |
| 94 | ret 0 |
| 95 | end |
| 96 | sec_ms = mul div t frq 1000 |
| 97 | rem_ms = div mul rem t frq 1000 frq |
| 98 | ret add sec_ms rem_ms |
| 99 | end |
| 100 | |
| 101 | fn wl_err_u32 rc long to ulong |
| 102 | if eq rc -9223372036854775808 |
| 103 | ret 9223372036854775808 |
| 104 | end |
| 105 | if lt rc 0 |
| 106 | ret ulong sub 0 rc |
| 107 | end |
| 108 | ret ulong rc |
| 109 | end |
| 110 | |
| 111 | note Initialize wire loop state and arm tcp.om passive listener on port. |
| 112 | fn wl_init port ulong max_ctx ulong vocab_sz ulong to void |
| 113 | for i ulong 32 |
| 114 | put wl_st i 0 |
| 115 | end |
| 116 | if eq port 0 |
| 117 | port = WL_DEFPORT |
| 118 | end |
| 119 | if eq max_ctx 0 |
| 120 | max_ctx = WL_DEFCTX |
| 121 | end |
| 122 | if gt max_ctx WL_U32MAX |
| 123 | max_ctx = WL_U32MAX |
| 124 | end |
| 125 | if eq vocab_sz 0 |
| 126 | vocab_sz = WL_DEFVOC |
| 127 | end |
| 128 | if gt vocab_sz WL_U32MAX |
| 129 | vocab_sz = WL_U32MAX |
| 130 | end |
| 131 | put wl_st WL_S_PORT port |
| 132 | put wl_st WL_S_MAXCTX max_ctx |
| 133 | put wl_st WL_S_VOCAB vocab_sz |
| 134 | put wl_st WL_S_IDLETMO WL_DEFIDLE |
| 135 | put wl_st WL_S_STEPTMO WL_DEFSTEP |
| 136 | now = wl_now_ms |
| 137 | put Tcf 0 now |
| 138 | put wl_st WL_S_ACTMS now |
| 139 | tinit |
| 140 | tlisten port |
| 141 | end |
| 142 | |
| 143 | note Queue omktx[0..omktxn) into bounded wl_txq ring (4096 B). |
| 144 | note Suppresses emission if peer is disconnected (WL_S_CONN == 0) to prevent cross-connection leaks. |
| 145 | fn wl_qtx to void |
| 146 | if eq get wl_st WL_S_CONN 0 |
| 147 | ret |
| 148 | end |
| 149 | n = get omktxn 0 |
| 150 | if or eq n 0 gt n 64 |
| 151 | ret |
| 152 | end |
| 153 | cur = get wl_st WL_S_TXN |
| 154 | if gt n sub WL_TXCAP cur |
| 155 | ret |
| 156 | end |
| 157 | h = get wl_st WL_S_TXH |
| 158 | tail = rem add h cur WL_TXCAP |
| 159 | i = ulong 0 |
| 160 | while lt i n bound 64 |
| 161 | put wl_txq rem add tail i WL_TXCAP get omktx i |
| 162 | i = add i 1 |
| 163 | end |
| 164 | put wl_st WL_S_TXN add cur n |
| 165 | end |
| 166 | |
| 167 | note Drain queued reply bytes from wl_txq into tcp.om send ring via twrite. |
| 168 | fn wl_flush_tx to void |
| 169 | n = get wl_st WL_S_TXN |
| 170 | if eq n 0 |
| 171 | ret |
| 172 | end |
| 173 | h = get wl_st WL_S_TXH |
| 174 | i = ulong 0 |
| 175 | while lt i n bound 4096 |
| 176 | put Tapp i get wl_txq rem add h i WL_TXCAP |
| 177 | i = add i 1 |
| 178 | end |
| 179 | w = twrite n |
| 180 | if gt w 0 |
| 181 | put wl_st WL_S_TXH rem add h w WL_TXCAP |
| 182 | put wl_st WL_S_TXN sub n w |
| 183 | end |
| 184 | end |
| 185 | |
| 186 | fn wl_send_err req_id ulong ecode ulong detail ulong to void |
| 187 | rc = omk_enc_err req_id ecode detail |
| 188 | if eq rc OMK_OK |
| 189 | wl_qtx |
| 190 | end |
| 191 | end |
| 192 | |
| 193 | fn wl_send_done req_id ulong flags ulong kv ulong reason ulong to void |
| 194 | rc = omk_enc_done req_id flags kv reason |
| 195 | if eq rc OMK_OK |
| 196 | wl_qtx |
| 197 | end |
| 198 | end |
| 199 | |
| 200 | fn wl_send_tok req_id ulong flags ulong seq ulong tok ulong to void |
| 201 | rc = omk_enc_token req_id flags seq tok |
| 202 | if eq rc OMK_OK |
| 203 | wl_qtx |
| 204 | end |
| 205 | end |
| 206 | |
| 207 | fn wl_send_pong req_id ulong to void |
| 208 | st = get wl_st WL_S_STATE |
| 209 | kv = get wl_st WL_S_KVPOS |
| 210 | mc = get wl_st WL_S_MAXCTX |
| 211 | ge_ = get wl_st WL_S_GERR |
| 212 | rc = omk_enc_pong req_id st kv mc ge_ |
| 213 | if eq rc OMK_OK |
| 214 | wl_qtx |
| 215 | end |
| 216 | end |
| 217 | |
| 218 | note Enter quarantined FAULT state: latch cmquar, emit ERR, refuse future compute. |
| 219 | fn wl_fault req_id ulong ecode ulong detail ulong to void |
| 220 | wl_gpu_quarantine |
| 221 | put wl_st WL_S_STATE WL_FAULT |
| 222 | put wl_st WL_S_OP WL_OPNONE |
| 223 | put wl_st WL_S_CANCEL 0 |
| 224 | put wl_st WL_S_GERR ecode |
| 225 | wl_send_err req_id ecode detail |
| 226 | end |
| 227 | |
| 228 | note Little-endian u32/u16 readers from wl_rx. |
| 229 | fn wl_rx_r16 off ulong to ulong |
| 230 | a = ulong get wl_rx off |
| 231 | b = ulong get wl_rx add off 1 |
| 232 | ret or a shl b 8 |
| 233 | end |
| 234 | |
| 235 | fn wl_rx_r32 off ulong to ulong |
| 236 | a = ulong get wl_rx off |
| 237 | b = ulong get wl_rx add off 1 |
| 238 | c = ulong get wl_rx add off 2 |
| 239 | d = ulong get wl_rx add off 3 |
| 240 | ret or or or a shl b 8 shl c 16 shl d 24 |
| 241 | end |
| 242 | |
| 243 | note Unpack wl_rx[0..16) into omkhdr[0..5] and validate. |
| 244 | fn wl_parse_rx_hdr to ulong |
| 245 | put omkhdr 0 wl_rx_r32 0 |
| 246 | put omkhdr 1 ulong get wl_rx 4 |
| 247 | put omkhdr 2 ulong get wl_rx 5 |
| 248 | put omkhdr 3 wl_rx_r16 6 |
| 249 | put omkhdr 4 wl_rx_r32 8 |
| 250 | put omkhdr 5 wl_rx_r32 12 |
| 251 | ret omk_chk_hdr |
| 252 | end |
| 253 | |
| 254 | note Consume n bytes from the front of wl_rx. |
| 255 | fn wl_rx_consume n ulong to void |
| 256 | cur = get wl_st WL_S_RXN |
| 257 | if ge n cur |
| 258 | put wl_st WL_S_RXN 0 |
| 259 | ret |
| 260 | end |
| 261 | remn = sub cur n |
| 262 | i = ulong 0 |
| 263 | while lt i remn bound 16400 |
| 264 | put wl_rx i get wl_rx add i n |
| 265 | i = add i 1 |
| 266 | end |
| 267 | put wl_st WL_S_RXN remn |
| 268 | end |
| 269 | |
| 270 | note Reset per-connection framing buffers; defer tlisten until any in-flight GPU op drains. |
| 271 | fn wl_rearm now ulong to void |
| 272 | put wl_st WL_S_RXN 0 |
| 273 | put wl_st WL_S_RXDROP 0 |
| 274 | put wl_st WL_S_TXH 0 |
| 275 | put wl_st WL_S_TXN 0 |
| 276 | put wl_st WL_S_CONN 0 |
| 277 | put wl_st WL_S_ACTMS now |
| 278 | st = get wl_st WL_S_STATE |
| 279 | if or eq st WL_SUBMIT eq st WL_WAIT |
| 280 | put wl_st WL_S_CANCEL 1 |
| 281 | put wl_st WL_S_REARM 1 |
| 282 | ret |
| 283 | end |
| 284 | put wl_st WL_S_REARM 0 |
| 285 | tlisten get wl_st WL_S_PORT |
| 286 | end |
| 287 | |
| 288 | note Handle RESET_KV (op=3): validate keep <= kv_pos; keep_pos == 0 routes through GPU_SUBMIT -> GPU_WAIT; |
| 289 | note keep_pos > 0 calls q38_reset_kv and propagates ERR(E_UNSUP) on -2 (never falls through to full wipe). |
| 290 | fn wl_do_reset req_id ulong to void |
| 291 | st = get wl_st WL_S_STATE |
| 292 | if eq st WL_FAULT |
| 293 | wl_send_err req_id OMK_EFAULT get wl_st WL_S_GERR |
| 294 | ret |
| 295 | end |
| 296 | if ne st WL_IDLE |
| 297 | wl_send_err req_id OMK_EBUSY 0 |
| 298 | ret |
| 299 | end |
| 300 | keep = wl_rx_r32 16 |
| 301 | if gt keep get wl_st WL_S_KVPOS |
| 302 | wl_send_err req_id OMK_ERANGE keep |
| 303 | ret |
| 304 | end |
| 305 | if gt keep 0 |
| 306 | rc = wl_q38_reset_kv keep |
| 307 | if eq rc -2 |
| 308 | wl_send_err req_id OMK_EUNSUP keep |
| 309 | ret |
| 310 | end |
| 311 | if lt rc 0 |
| 312 | wl_send_err req_id OMK_EGSTAGE wl_err_u32 rc |
| 313 | ret |
| 314 | end |
| 315 | if eq rc 0 |
| 316 | put wl_st WL_S_KVPOS keep |
| 317 | put wl_st WL_S_HASFIRST 0 |
| 318 | wl_send_done req_id 0 keep OMK_RRESET |
| 319 | ret |
| 320 | end |
| 321 | wl_send_err req_id OMK_EUNSUP keep |
| 322 | ret |
| 323 | end |
| 324 | put wl_st WL_S_REQID req_id |
| 325 | put wl_st WL_S_OP WL_OPRESET |
| 326 | put wl_st WL_S_CANCEL 0 |
| 327 | put wl_st WL_S_STATE WL_SUBMIT |
| 328 | end |
| 329 | |
| 330 | note Handle PREFILL (op=4): overflow-safe MAX_CTX check + vocab check before GPU submit. |
| 331 | fn wl_do_prefill req_id ulong plen ulong to void |
| 332 | st = get wl_st WL_S_STATE |
| 333 | if eq st WL_FAULT |
| 334 | wl_send_err req_id OMK_EFAULT get wl_st WL_S_GERR |
| 335 | ret |
| 336 | end |
| 337 | if ne st WL_IDLE |
| 338 | wl_send_err req_id OMK_EBUSY 0 |
| 339 | ret |
| 340 | end |
| 341 | ntok = div plen 4 |
| 342 | mctx = get wl_st WL_S_MAXCTX |
| 343 | kv = get wl_st WL_S_KVPOS |
| 344 | if gt ntok mctx |
| 345 | wl_send_err req_id OMK_ERANGE ntok |
| 346 | ret |
| 347 | end |
| 348 | if gt kv sub mctx ntok |
| 349 | wl_send_err req_id OMK_ERANGE kv |
| 350 | ret |
| 351 | end |
| 352 | voc = get wl_st WL_S_VOCAB |
| 353 | i = ulong 0 |
| 354 | while lt i ntok bound 4096 |
| 355 | tok = wl_rx_r32 add OMK_HDR mul i 4 |
| 356 | if ge tok voc |
| 357 | wl_send_err req_id OMK_EBADTOK tok |
| 358 | ret |
| 359 | end |
| 360 | put wl_ptoks i uint tok |
| 361 | i = add i 1 |
| 362 | end |
| 363 | put wl_st WL_S_REQID req_id |
| 364 | put wl_st WL_S_PFIDX 0 |
| 365 | put wl_st WL_S_PFCNT ntok |
| 366 | put wl_st WL_S_HASFIRST 0 |
| 367 | put wl_st WL_S_OP WL_OPPREF |
| 368 | put wl_st WL_S_CANCEL 0 |
| 369 | put wl_st WL_S_STATE WL_SUBMIT |
| 370 | end |
| 371 | |
| 372 | note Handle DECODE (op=5): bounds, temp==0 check, overflow-safe MAX_CTX check. |
| 373 | note If final PREFILL already computed the first predicted token (WL_S_HASFIRST == 1), |
| 374 | note emit it immediately as TOKEN(seq=0) before stepping CURTOK at kv_pos. |
| 375 | fn wl_do_decode req_id ulong to void |
| 376 | st = get wl_st WL_S_STATE |
| 377 | if eq st WL_FAULT |
| 378 | wl_send_err req_id OMK_EFAULT get wl_st WL_S_GERR |
| 379 | ret |
| 380 | end |
| 381 | if ne st WL_IDLE |
| 382 | wl_send_err req_id OMK_EBUSY 0 |
| 383 | ret |
| 384 | end |
| 385 | mtok = wl_rx_r32 16 |
| 386 | eos = wl_rx_r32 20 |
| 387 | tmp = wl_rx_r32 24 |
| 388 | if or eq mtok 0 gt mtok WL_DECMAX |
| 389 | wl_send_err req_id OMK_ERANGE mtok |
| 390 | ret |
| 391 | end |
| 392 | if ne tmp 0 |
| 393 | wl_send_err req_id OMK_EUNSUP tmp |
| 394 | ret |
| 395 | end |
| 396 | if ge eos get wl_st WL_S_VOCAB |
| 397 | wl_send_err req_id OMK_EBADTOK eos |
| 398 | ret |
| 399 | end |
| 400 | kv = get wl_st WL_S_KVPOS |
| 401 | hasf = get wl_st WL_S_HASFIRST |
| 402 | if eq hasf 0 |
| 403 | if or eq kv 0 ge kv get wl_st WL_S_MAXCTX |
| 404 | wl_send_err req_id OMK_ERANGE kv |
| 405 | ret |
| 406 | end |
| 407 | end |
| 408 | put wl_st WL_S_REQID req_id |
| 409 | put wl_st WL_S_DCMAX mtok |
| 410 | put wl_st WL_S_DCEOS eos |
| 411 | put wl_st WL_S_CANCEL 0 |
| 412 | if ne hasf 0 |
| 413 | otok = get wl_st WL_S_LASTTOK |
| 414 | put wl_st WL_S_HASFIRST 0 |
| 415 | put wl_st WL_S_CURTOK otok |
| 416 | tflg = ulong 0 |
| 417 | if eq otok eos |
| 418 | tflg = OMK_FEOS |
| 419 | end |
| 420 | wl_send_tok req_id tflg 0 otok |
| 421 | if eq otok eos |
| 422 | wl_send_done req_id OMK_FEOS kv OMK_REOS |
| 423 | ret |
| 424 | end |
| 425 | if or le mtok 1 ge kv get wl_st WL_S_MAXCTX |
| 426 | wl_send_done req_id 0 kv OMK_RMAX |
| 427 | ret |
| 428 | end |
| 429 | put wl_st WL_S_DCSEQ 1 |
| 430 | put wl_st WL_S_OP WL_OPDEC |
| 431 | put wl_st WL_S_STATE WL_SUBMIT |
| 432 | ret |
| 433 | end |
| 434 | put wl_st WL_S_DCSEQ 0 |
| 435 | put wl_st WL_S_CURTOK get wl_st WL_S_LASTTOK |
| 436 | put wl_st WL_S_OP WL_OPDEC |
| 437 | put wl_st WL_S_STATE WL_SUBMIT |
| 438 | end |
| 439 | |
| 440 | note Handle CANCEL (op=8). |
| 441 | fn wl_do_cancel req_id ulong to void |
| 442 | st = get wl_st WL_S_STATE |
| 443 | if or eq st WL_SUBMIT eq st WL_WAIT |
| 444 | put wl_st WL_S_CANCEL 1 |
| 445 | ret |
| 446 | end |
| 447 | wl_send_done req_id 0 get wl_st WL_S_KVPOS OMK_RCANCEL |
| 448 | end |
| 449 | |
| 450 | note Parse and dispatch at most one complete OMTK v1 frame from wl_rx. |
| 451 | note Returns 1 if a frame was consumed, 0 if incomplete or dropped. |
| 452 | fn wl_dispatch_one now ulong to ulong |
| 453 | if ne get wl_st WL_S_RXDROP 0 |
| 454 | ret 0 |
| 455 | end |
| 456 | rxn = get wl_st WL_S_RXN |
| 457 | if lt rxn OMK_HDR |
| 458 | ret 0 |
| 459 | end |
| 460 | rc = wl_parse_rx_hdr |
| 461 | req = get omkhdr 4 |
| 462 | if ne rc OMK_OK |
| 463 | wl_send_err req rc 0 |
| 464 | wl_flush_tx |
| 465 | tstep now |
| 466 | tclose |
| 467 | put wl_st WL_S_RXDROP 1 |
| 468 | put wl_st WL_S_RXN 0 |
| 469 | ret 0 |
| 470 | end |
| 471 | plen = get omkhdr 5 |
| 472 | need = add OMK_HDR plen |
| 473 | if lt rxn need |
| 474 | ret 0 |
| 475 | end |
| 476 | op = get omkhdr 2 |
| 477 | if eq op OMK_PING |
| 478 | wl_send_pong req |
| 479 | wl_rx_consume need |
| 480 | ret 1 |
| 481 | end |
| 482 | if eq op OMK_RESET |
| 483 | wl_do_reset req |
| 484 | wl_rx_consume need |
| 485 | ret 1 |
| 486 | end |
| 487 | if eq op OMK_PREFILL |
| 488 | wl_do_prefill req plen |
| 489 | wl_rx_consume need |
| 490 | ret 1 |
| 491 | end |
| 492 | if eq op OMK_DECODE |
| 493 | wl_do_decode req |
| 494 | wl_rx_consume need |
| 495 | ret 1 |
| 496 | end |
| 497 | if eq op OMK_CANCEL |
| 498 | wl_do_cancel req |
| 499 | wl_rx_consume need |
| 500 | ret 1 |
| 501 | end |
| 502 | wl_send_err req OMK_EBADOP op |
| 503 | wl_flush_tx |
| 504 | tstep now |
| 505 | tclose |
| 506 | put wl_st WL_S_RXDROP 1 |
| 507 | put wl_st WL_S_RXN 0 |
| 508 | ret 0 |
| 509 | end |
| 510 | |
| 511 | note Pump incoming TCP bytes from tcp.om into wl_rx and dispatch complete frames. |
| 512 | fn wl_pump_tcp now ulong to void |
| 513 | if ne get wl_st WL_S_RXDROP 0 |
| 514 | ret |
| 515 | end |
| 516 | rxn = get wl_st WL_S_RXN |
| 517 | room = sub OMK_MAXFRM rxn |
| 518 | if gt room 0 |
| 519 | n = tread room |
| 520 | if gt n 0 |
| 521 | put wl_st WL_S_ACTMS now |
| 522 | i = ulong 0 |
| 523 | while lt i n bound 16400 |
| 524 | put wl_rx add rxn i get Tapp i |
| 525 | i = add i 1 |
| 526 | end |
| 527 | put wl_st WL_S_RXN add rxn n |
| 528 | end |
| 529 | end |
| 530 | k = ulong 0 |
| 531 | while lt k 8 bound 8 |
| 532 | got = wl_dispatch_one now |
| 533 | if eq got 0 |
| 534 | break |
| 535 | end |
| 536 | k = add k 1 |
| 537 | end |
| 538 | end |
| 539 | |
| 540 | note Stage and kick one GPU op (WL_SUBMIT -> WL_WAIT). |
| 541 | note Applies TX backpressure (>= 64 B free in wl_txq) so TOKEN/DONE/ERR frames are never dropped. |
| 542 | fn wl_gpu_submit now ulong to void |
| 543 | if ne get wl_st WL_S_CONN 0 |
| 544 | if lt sub WL_TXCAP get wl_st WL_S_TXN 64 |
| 545 | ret |
| 546 | end |
| 547 | end |
| 548 | req = get wl_st WL_S_REQID |
| 549 | op = get wl_st WL_S_OP |
| 550 | if ne get wl_st WL_S_CANCEL 0 |
| 551 | if ne op WL_OPRESET |
| 552 | wl_send_done req 0 get wl_st WL_S_KVPOS OMK_RCANCEL |
| 553 | put wl_st WL_S_STATE WL_IDLE |
| 554 | put wl_st WL_S_OP WL_OPNONE |
| 555 | put wl_st WL_S_CANCEL 0 |
| 556 | ret |
| 557 | end |
| 558 | end |
| 559 | pbw = long 0 |
| 560 | if eq op WL_OPRESET |
| 561 | put wl_st WL_S_CURMODE 0 |
| 562 | pbw = wl_q38_reset_kv 0 |
| 563 | else |
| 564 | if eq op WL_OPPREF |
| 565 | idx = get wl_st WL_S_PFIDX |
| 566 | cnt = get wl_st WL_S_PFCNT |
| 567 | tok = get wl_ptoks idx |
| 568 | kv = get wl_st WL_S_KVPOS |
| 569 | md = uchar 0 |
| 570 | if eq add idx 1 cnt |
| 571 | md = uchar 1 |
| 572 | end |
| 573 | put wl_st WL_S_CURMODE ulong md |
| 574 | pbw = wl_q38_stage_step tok uint kv md |
| 575 | else |
| 576 | if eq op WL_OPDEC |
| 577 | kv = get wl_st WL_S_KVPOS |
| 578 | if ge kv get wl_st WL_S_MAXCTX |
| 579 | wl_send_done req 0 kv OMK_RMAX |
| 580 | put wl_st WL_S_STATE WL_IDLE |
| 581 | put wl_st WL_S_OP WL_OPNONE |
| 582 | ret |
| 583 | end |
| 584 | tok = uint get wl_st WL_S_CURTOK |
| 585 | put wl_st WL_S_CURMODE 1 |
| 586 | pbw = wl_q38_stage_step tok uint kv 1 |
| 587 | end |
| 588 | end |
| 589 | end |
| 590 | if or le pbw 0 gt pbw WL_CBLENMAX |
| 591 | wl_send_err req OMK_EGSTAGE wl_err_u32 pbw |
| 592 | put wl_st WL_S_STATE WL_IDLE |
| 593 | put wl_st WL_S_OP WL_OPNONE |
| 594 | ret |
| 595 | end |
| 596 | krc = wl_gpu_kick uint pbw |
| 597 | if lt krc 0 |
| 598 | wl_fault req OMK_EGKICK wl_err_u32 krc |
| 599 | ret |
| 600 | end |
| 601 | put wl_st WL_S_SUBMS now |
| 602 | put wl_st WL_S_STATE WL_WAIT |
| 603 | end |
| 604 | |
| 605 | note Handle completed prefill or decode step after semaphore retirement. |
| 606 | note For mode=0 (non-final PREFILL), Piece 5 poisons out_tok_pa=0xFFFFFFFF and skips lm_head, |
| 607 | note so run dsb ish + q38_verify_step but skip wl_q38_read_tok. |
| 608 | fn wl_finish_step req ulong op ulong to void |
| 609 | wl_dsb_ish |
| 610 | vrc = wl_q38_verify_step |
| 611 | if ne vrc 0 |
| 612 | wl_fault req OMK_EGVER wl_err_u32 vrc |
| 613 | ret |
| 614 | end |
| 615 | kv = add get wl_st WL_S_KVPOS 1 |
| 616 | put wl_st WL_S_KVPOS kv |
| 617 | md = get wl_st WL_S_CURMODE |
| 618 | if eq op WL_OPPREF |
| 619 | if ne md 0 |
| 620 | otok = ulong wl_q38_read_tok |
| 621 | if ge otok get wl_st WL_S_VOCAB |
| 622 | wl_fault req OMK_EGVER otok |
| 623 | ret |
| 624 | end |
| 625 | put wl_st WL_S_LASTTOK otok |
| 626 | put wl_st WL_S_HASFIRST 1 |
| 627 | end |
| 628 | idx = add get wl_st WL_S_PFIDX 1 |
| 629 | put wl_st WL_S_PFIDX idx |
| 630 | if ne get wl_st WL_S_CANCEL 0 |
| 631 | put wl_st WL_S_HASFIRST 0 |
| 632 | wl_send_done req 0 kv OMK_RCANCEL |
| 633 | put wl_st WL_S_STATE WL_IDLE |
| 634 | put wl_st WL_S_OP WL_OPNONE |
| 635 | put wl_st WL_S_CANCEL 0 |
| 636 | ret |
| 637 | end |
| 638 | if ge idx get wl_st WL_S_PFCNT |
| 639 | wl_send_done req 0 kv OMK_RPREF |
| 640 | put wl_st WL_S_STATE WL_IDLE |
| 641 | put wl_st WL_S_OP WL_OPNONE |
| 642 | ret |
| 643 | end |
| 644 | put wl_st WL_S_STATE WL_SUBMIT |
| 645 | ret |
| 646 | end |
| 647 | otok = ulong wl_q38_read_tok |
| 648 | if ge otok get wl_st WL_S_VOCAB |
| 649 | wl_fault req OMK_EGVER otok |
| 650 | ret |
| 651 | end |
| 652 | put wl_st WL_S_LASTTOK otok |
| 653 | seq = get wl_st WL_S_DCSEQ |
| 654 | eos = get wl_st WL_S_DCEOS |
| 655 | tflg = ulong 0 |
| 656 | if eq otok eos |
| 657 | tflg = OMK_FEOS |
| 658 | end |
| 659 | wl_send_tok req tflg seq otok |
| 660 | seq = add seq 1 |
| 661 | put wl_st WL_S_DCSEQ seq |
| 662 | put wl_st WL_S_CURTOK otok |
| 663 | if eq otok eos |
| 664 | wl_send_done req OMK_FEOS kv OMK_REOS |
| 665 | put wl_st WL_S_STATE WL_IDLE |
| 666 | put wl_st WL_S_OP WL_OPNONE |
| 667 | ret |
| 668 | end |
| 669 | if ne get wl_st WL_S_CANCEL 0 |
| 670 | wl_send_done req 0 kv OMK_RCANCEL |
| 671 | put wl_st WL_S_STATE WL_IDLE |
| 672 | put wl_st WL_S_OP WL_OPNONE |
| 673 | put wl_st WL_S_CANCEL 0 |
| 674 | ret |
| 675 | end |
| 676 | if or ge seq get wl_st WL_S_DCMAX ge kv get wl_st WL_S_MAXCTX |
| 677 | wl_send_done req 0 kv OMK_RMAX |
| 678 | put wl_st WL_S_STATE WL_IDLE |
| 679 | put wl_st WL_S_OP WL_OPNONE |
| 680 | ret |
| 681 | end |
| 682 | put wl_st WL_S_STATE WL_SUBMIT |
| 683 | end |
| 684 | |
| 685 | note Non-blocking GPU completion poll + GSP HALT/lockdown check + step timeout. |
| 686 | fn wl_gpu_wait now ulong to void |
| 687 | req = get wl_st WL_S_REQID |
| 688 | irq = wl_gpu_irq_check |
| 689 | if lt irq 0 |
| 690 | wl_fault req OMK_EGHALT wl_err_u32 irq |
| 691 | ret |
| 692 | end |
| 693 | pr = wl_gpu_poll |
| 694 | if lt pr 0 |
| 695 | wl_fault req OMK_ESEM wl_err_u32 pr |
| 696 | ret |
| 697 | end |
| 698 | if eq pr 0 |
| 699 | subms = get wl_st WL_S_SUBMS |
| 700 | tmo = get wl_st WL_S_STEPTMO |
| 701 | if ge now subms |
| 702 | if ge sub now subms tmo |
| 703 | wl_fault req OMK_EGTMO tmo |
| 704 | end |
| 705 | end |
| 706 | ret |
| 707 | end |
| 708 | put wl_st WL_S_ACTMS now |
| 709 | op = get wl_st WL_S_OP |
| 710 | if eq op WL_OPRESET |
| 711 | wl_dsb_ish |
| 712 | put wl_st WL_S_KVPOS 0 |
| 713 | put wl_st WL_S_LASTTOK 0 |
| 714 | put wl_st WL_S_HASFIRST 0 |
| 715 | put wl_st WL_S_CANCEL 0 |
| 716 | wl_send_done req 0 0 OMK_RRESET |
| 717 | put wl_st WL_S_STATE WL_IDLE |
| 718 | put wl_st WL_S_OP WL_OPNONE |
| 719 | ret |
| 720 | end |
| 721 | wl_finish_step req op |
| 722 | end |
| 723 | |
| 724 | note Execute one complete poll iteration: NIC RX -> TCP -> OMTK -> GPU -> idle check -> NIC reclaim. |
| 725 | fn wl_tick to void |
| 726 | b = ulong 0 |
| 727 | while lt b 16 bound 16 |
| 728 | n = wl_nic_rx |
| 729 | if eq n 0 |
| 730 | break |
| 731 | end |
| 732 | if le n 2048 |
| 733 | tinput n |
| 734 | end |
| 735 | b = add b 1 |
| 736 | end |
| 737 | now = wl_now_ms |
| 738 | tstep now |
| 739 | tst = get Tcb kST |
| 740 | if eq tst 0 |
| 741 | if eq get wl_st WL_S_REARM 0 |
| 742 | wl_rearm now |
| 743 | else |
| 744 | st0 = get wl_st WL_S_STATE |
| 745 | if or eq st0 WL_IDLE eq st0 WL_FAULT |
| 746 | put wl_st WL_S_REARM 0 |
| 747 | tlisten get wl_st WL_S_PORT |
| 748 | end |
| 749 | end |
| 750 | else |
| 751 | if eq tst 4 |
| 752 | if eq get wl_st WL_S_CONN 0 |
| 753 | put wl_st WL_S_CONN 1 |
| 754 | put wl_st WL_S_ACTMS now |
| 755 | end |
| 756 | wl_pump_tcp now |
| 757 | else |
| 758 | if eq tst 7 |
| 759 | wl_pump_tcp now |
| 760 | if eq get wl_st WL_S_TXN 0 |
| 761 | tclose |
| 762 | end |
| 763 | end |
| 764 | end |
| 765 | end |
| 766 | st = get wl_st WL_S_STATE |
| 767 | if eq st WL_SUBMIT |
| 768 | wl_gpu_submit now |
| 769 | else |
| 770 | if eq st WL_WAIT |
| 771 | wl_gpu_wait now |
| 772 | end |
| 773 | end |
| 774 | wl_flush_tx |
| 775 | tstep now |
| 776 | tst2 = get Tcb kST |
| 777 | if ge tst2 4 |
| 778 | if ne get wl_st WL_S_STATE WL_WAIT |
| 779 | act = get wl_st WL_S_ACTMS |
| 780 | itmo = get wl_st WL_S_IDLETMO |
| 781 | if ge now act |
| 782 | if ge sub now act itmo |
| 783 | tdrop 0 |
| 784 | put wl_st WL_S_DROPS add get wl_st WL_S_DROPS 1 |
| 785 | wl_rearm now |
| 786 | end |
| 787 | end |
| 788 | end |
| 789 | end |
| 790 | wl_nic_reclaim |
| 791 | end |
| 792 | |
| 793 | note Bare-metal EL1 kernel main loop: nested bound 9223372036854775807 (no 1M-pass Trap 4). |
| 794 | fn wl_run to void |
| 795 | while eq wl_should_stop 0 bound 9223372036854775807 |
| 796 | while eq wl_should_stop 0 bound 9223372036854775807 |
| 797 | wl_tick |
| 798 | end |
| 799 | end |
| 800 | end |