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 / apps / omega-metabd / general_pe.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note apps/omega-metabd/general_pe.om: Stage 4 (Pillar 1) General Multi-Block ΩIR Partial Evaluator, |
| 3 | note EGP3 Proof Pipeline, Native AArch64 Machine-Code Emitter, Strict W^X (RW->SHA256->RX) Sealer, |
| 4 | note 3-Bit Proof Gate (Contract|EGP3|Monk+Immune == 7), Cooperative fabWtbl Hot-Swap & OOD Rollback. |
| 5 | note |
| 6 | note Pure seeded OM-2 library unit (no om2 header, no fn main). |
| 7 | note Prefixes: gpe_/GPE_ (partial evaluator + ΩIR eval + AArch64 emit), wx_/WX_ (strict W^X), mb_/MB_ (daemon core). |
| 8 | note Links unchanged after: ir.om, sha256.om, target_desc.om, isel.om, sm121_pe.om, fabric.om, |
| 9 | note crystallize.om, ir_egraph.om, egp3_checker.om, arboris_spec.om, profile_ring.om. |
| 10 | |
| 11 | note ==== Part 1: Constants & State ==== |
| 12 | |
| 13 | const GPE_TRAP_DEOPT ulong 14 |
| 14 | const GPE_MAX_UNROLL ulong 32 |
| 15 | const GPE_MAX_VALS ulong 2048 |
| 16 | const GPE_MAX_BLKS ulong 64 |
| 17 | const GPE_MAX_FIX ulong 128 |
| 18 | |
| 19 | const GPE_WK_AFFINE ulong 1 |
| 20 | const GPE_WK_BRANCH ulong 2 |
| 21 | const GPE_WK_SPARSE ulong 3 |
| 22 | |
| 23 | note Strict W^X protection flags (Linux PROT_* bitmask): |
| 24 | const WX_PROT_NONE ulong 0 |
| 25 | const WX_PROT_READ ulong 1 |
| 26 | const WX_PROT_WRITE ulong 2 |
| 27 | const WX_PROT_EXEC ulong 4 |
| 28 | const WX_PROT_RW ulong 3 |
| 29 | const WX_PROT_RX ulong 5 |
| 30 | const WX_PROT_RWX ulong 7 |
| 31 | |
| 32 | const WX_OK ulong 0 |
| 33 | const WX_ERR_RWX_FORBIDDEN ulong 1 |
| 34 | const WX_ERR_ELF_BAD ulong 2 |
| 35 | const WX_ERR_IO ulong 3 |
| 36 | const WX_ERR_SHA_MISMATCH ulong 4 |
| 37 | const WX_ERR_MODE_WRITABLE ulong 5 |
| 38 | const WX_ERR_EXEC ulong 6 |
| 39 | const WX_ERR_NOT_RX ulong 7 |
| 40 | |
| 41 | note 3-bit proof gate masks & refusal codes: |
| 42 | const MB_BIT_CONTRACT ulong 1 |
| 43 | const MB_BIT_EGP3 ulong 2 |
| 44 | const MB_BIT_MONK_IMMUNE ulong 4 |
| 45 | const MB_PROOF_ALL ulong 7 |
| 46 | |
| 47 | const MB_OK ulong 0 |
| 48 | const MB_ERR_SLOT ulong 1 |
| 49 | const MB_ERR_PROOF_MASK ulong 2 |
| 50 | const MB_ERR_CONTRACT ulong 3 |
| 51 | const MB_ERR_EGP3 ulong 4 |
| 52 | const MB_ERR_MONK_IMMUNE ulong 5 |
| 53 | const MB_ERR_WX_RWX ulong 6 |
| 54 | const MB_ERR_PARITY ulong 7 |
| 55 | const MB_ERR_SWAP ulong 8 |
| 56 | const MB_ERR_NOT_A725 ulong 9 |
| 57 | const MB_ERR_PE ulong 10 |
| 58 | |
| 59 | const MB_DISP_BASELINE ulong 0 |
| 60 | const MB_DISP_SPEC ulong 1 |
| 61 | const MB_DISP_ROLLBACK ulong 2 |
| 62 | |
| 63 | const MB_ALARM_OOD ulong 1 |
| 64 | const MB_ALARM_ORACLE ulong 2 |
| 65 | const MB_ALARM_RWX ulong 3 |
| 66 | |
| 67 | const MB_MAX_SLOTS ulong 8 |
| 68 | |
| 69 | note Multi-block ΩIR evaluator state: |
| 70 | global gpe_ev_val ulong 2048 |
| 71 | global gpe_ev_tmp ulong 16 |
| 72 | global gpe_last_ops ulong 1 |
| 73 | global gpe_last_blks ulong 1 |
| 74 | global gpe_last_trap ulong 1 |
| 75 | |
| 76 | note General PE configuration & abstract environment: |
| 77 | global gpe_pin_mask ulong 1 |
| 78 | global gpe_pin_val ulong 4 |
| 79 | global gpe_cfg_bias ulong 1 |
| 80 | global gpe_cfg_sparse ulong 1 |
| 81 | global gpe_cfg_tid ulong 1 |
| 82 | |
| 83 | global gpe_v_kind ulong 2048 |
| 84 | global gpe_v_cval ulong 2048 |
| 85 | global gpe_v_sym uint 2048 |
| 86 | global gpe_b_vis ulong 64 |
| 87 | global gpe_pe_stat ulong 16 |
| 88 | note gpe_pe_stat: [0]=blks_src [1]=blks_spec [2]=br_folded [3]=loops_unrolled [4]=ops_folded |
| 89 | note [5]=eg_cost_bef [6]=eg_cost_aft [7]=egp3_rc [8]=egp3_certs [9]=egp3_steps |
| 90 | note [10]=egp3_evals [11]=hot_blk_handle [12]=hot_root_val [13]=opt_fn [14]=egp3_bytes |
| 91 | |
| 92 | note Saved live EGP3 bundle from general PE -> egp3_checker verification & negative controls: |
| 93 | global gpe_egp3_buf uchar 131072 |
| 94 | global gpe_egp3_len ulong 1 |
| 95 | |
| 96 | note E-graph SSA-id to ΩIR value handle mapping (for materializing extracted optimal DAG): |
| 97 | global gpe_eg_mat uint 512 |
| 98 | global gpe_eg_mat_ok uchar 512 |
| 99 | |
| 100 | note Native AArch64 multi-block compiler state (emits directly into isBuf[0..1024) + elfBuf): |
| 101 | global gpe_a6_vbase ulong 1 |
| 102 | global gpe_a6_p_h uint 4 |
| 103 | global gpe_a6_vslot ulong 2048 |
| 104 | global gpe_a6_vnext ulong 1 |
| 105 | global gpe_a6_blks uint 64 |
| 106 | global gpe_a6_boff ulong 64 |
| 107 | global gpe_a6_nblk ulong 1 |
| 108 | global gpe_a6_fpc ulong 128 |
| 109 | global gpe_a6_ftgt uint 128 |
| 110 | global gpe_a6_fkind ulong 128 |
| 111 | global gpe_a6_freg ulong 128 |
| 112 | global gpe_a6_nfix ulong 1 |
| 113 | global gpe_a6_epi_pc ulong 1 |
| 114 | global gpe_a6_top_pc ulong 1 |
| 115 | global gpe_a6_insns ulong 1 |
| 116 | global gpe_a6_ovf ulong 1 |
| 117 | |
| 118 | note Strict W^X staging/sealing state: |
| 119 | global wx_dir uchar 512 |
| 120 | global wx_dir_len ulong 1 |
| 121 | global wx_p_stage uchar 1024 |
| 122 | global wx_p_stagel ulong 1 |
| 123 | global wx_p_rx uchar 1024 |
| 124 | global wx_p_rxl ulong 1 |
| 125 | global wx_ren_pkt uchar 2200 |
| 126 | global wx_stat_buf uchar 128 |
| 127 | global wx_rb_buf uchar 4096 |
| 128 | global wx_sha_exp uchar 32 |
| 129 | global wx_sha_got uchar 32 |
| 130 | global wx_av_pkt uchar 1200 |
| 131 | global wx_p_in uchar 8 |
| 132 | global wx_p_out uchar 8 |
| 133 | global wx_io_in uchar 40 |
| 134 | global wx_io_out uchar 8 |
| 135 | global wx_wait_st uchar 4 |
| 136 | global wx_args ulong 5 |
| 137 | |
| 138 | global wx_rwx_refusals ulong 1 |
| 139 | global wx_sha_refusals ulong 1 |
| 140 | global wx_mode_refusals ulong 1 |
| 141 | global wx_seals_ok ulong 1 |
| 142 | global wx_last_exit ulong 1 |
| 143 | global wx_last_ns ulong 1 |
| 144 | global wx_last_out ulong 1 |
| 145 | |
| 146 | note Cooperative slot table & daemon telemetry: |
| 147 | global mb_slot_valid ulong 8 |
| 148 | global mb_slot_sem ulong 8 |
| 149 | global mb_slot_fbase uint 8 |
| 150 | global mb_slot_fspec uint 8 |
| 151 | global mb_slot_base_insns ulong 8 |
| 152 | global mb_slot_spec_insns ulong 8 |
| 153 | global mb_slot_base_ops ulong 8 |
| 154 | global mb_slot_spec_ops ulong 8 |
| 155 | global mb_slot_base_ptr ulong 8 |
| 156 | global mb_slot_spec_ptr ulong 8 |
| 157 | global mb_slot_base_tid ulong 8 |
| 158 | global mb_slot_spec_tid ulong 8 |
| 159 | global mb_slot_base_slab ulong 8 |
| 160 | global mb_slot_alarm_slab ulong 8 |
| 161 | global mb_slot_base_path uchar 4096 |
| 162 | global mb_slot_base_plen ulong 8 |
| 163 | global mb_slot_spec_path uchar 4096 |
| 164 | global mb_slot_spec_plen ulong 8 |
| 165 | global mb_slot_pin_mask ulong 8 |
| 166 | global mb_slot_pin_v0 ulong 8 |
| 167 | global mb_slot_pin_v1 ulong 8 |
| 168 | global mb_slot_pin_v2 ulong 8 |
| 169 | global mb_slot_pin_v3 ulong 8 |
| 170 | global mb_slot_wk ulong 8 |
| 171 | |
| 172 | global mb_worker_cpu ulong 1 |
| 173 | global mb_swaps ulong 1 |
| 174 | global mb_refusals ulong 1 |
| 175 | global mb_rollbacks ulong 1 |
| 176 | global mb_spec_hits ulong 1 |
| 177 | global mb_base_hits ulong 1 |
| 178 | global mb_alarms ulong 1 |
| 179 | global mb_last_disp ulong 1 |
| 180 | global mb_last_err ulong 1 |
| 181 | global mb_alarm_cnt ulong 1 |
| 182 | global mb_alarm_slot ulong 32 |
| 183 | global mb_alarm_code ulong 32 |
| 184 | global mb_alarm_x0 ulong 32 |
| 185 | global mb_alarm_x1 ulong 32 |
| 186 | global mb_cpu_mask uchar 128 |
| 187 | global mb_mut_flag ulong 1 |
| 188 | |
| 189 | text wx_t_def_dir "/tmp/omega-metabd-wx" |
| 190 | text wx_t_sl "/slot_" |
| 191 | text wx_t_v "_v" |
| 192 | text wx_t_stg ".stage" |
| 193 | text wx_t_rx ".rx" |
| 194 | |
| 195 | note ==== Part 2: Multi-Block ΩIR Benchmark Builders (real ir.om functions, @pure @exact v2) ==== |
| 196 | |
| 197 | fn gpe_mk_bin b uint op uint a uint bv uint to uint |
| 198 | o = ir_op b op 5 0 2 |
| 199 | ir_set o 0 a |
| 200 | ir_set o 1 bv |
| 201 | ret ir_res o |
| 202 | end |
| 203 | |
| 204 | fn gpe_mk_cmp b uint op uint a uint bv uint to uint |
| 205 | o = ir_op b op 1 0 2 |
| 206 | ir_set o 0 a |
| 207 | ir_set o 1 bv |
| 208 | ret ir_res o |
| 209 | end |
| 210 | |
| 211 | fn gpe_mk_cbr b uint tk uint cond uint bt uint bf uint to void |
| 212 | cb = ir_op b 61 0 0 4 |
| 213 | ir_set cb 0 tk |
| 214 | ir_set cb 1 cond |
| 215 | ir_set cb 2 bt |
| 216 | ir_set cb 3 bf |
| 217 | end |
| 218 | |
| 219 | fn gpe_mk_br1 b uint tgt uint tk uint v0 uint to void |
| 220 | br = ir_op b 60 0 0 3 |
| 221 | ir_set br 0 tgt |
| 222 | ir_set br 1 tk |
| 223 | ir_set br 2 v0 |
| 224 | end |
| 225 | |
| 226 | fn gpe_mk_br2 b uint tgt uint tk uint v0 uint v1 uint to void |
| 227 | br = ir_op b 60 0 0 4 |
| 228 | ir_set br 0 tgt |
| 229 | ir_set br 1 tk |
| 230 | ir_set br 2 v0 |
| 231 | ir_set br 3 v1 |
| 232 | end |
| 233 | |
| 234 | fn gpe_mk_retv b uint tk uint v uint to void |
| 235 | rv = ir_op b 63 0 0 2 |
| 236 | ir_set rv 0 tk |
| 237 | ir_set rv 1 v |
| 238 | end |
| 239 | |
| 240 | note Helper globals for builder params/blocks (keeps every builder well below omc's 16-slot limit) |
| 241 | global gpe_bp uint 8 |
| 242 | global gpe_bb uint 8 |
| 243 | |
| 244 | fn gpe_new_fn4 to uint |
| 245 | f = ir_fnew 0 0 5 |
| 246 | ir_fset_mode f 1 1 0 |
| 247 | ir_fset_phys f 0 5000 15000 |
| 248 | put gpe_bp 0 ir_fpar f 5 |
| 249 | put gpe_bp 1 ir_fpar f 5 |
| 250 | put gpe_bp 2 ir_fpar f 5 |
| 251 | put gpe_bp 3 ir_fpar f 5 |
| 252 | ret f |
| 253 | end |
| 254 | |
| 255 | note Workload 1 (4 blocks): affine_route(x, scale, bias, mode) -> u64 |
| 256 | note Hot path (mode==0, scale=8, bias=0): ((x*scale + bias)*3 + bias)*1 + (x - x) == 24*x |
| 257 | note Cold path (mode!=0): ((x*7 + scale)*11 + bias)*1 + (x - x) |
| 258 | fn gpe_build_affine_arms to void |
| 259 | bf = get gpe_bb 1 |
| 260 | bc = get gpe_bb 2 |
| 261 | bm = get gpe_bb 3 |
| 262 | p0 = get gpe_bp 0 |
| 263 | p1 = get gpe_bp 1 |
| 264 | p2 = get gpe_bp 2 |
| 265 | c3 = ir_res ir_op bf 1 5 3 0 |
| 266 | t = gpe_mk_bin bf 12 p0 p1 |
| 267 | t = gpe_mk_bin bf 10 t p2 |
| 268 | t = gpe_mk_bin bf 12 t c3 |
| 269 | t = gpe_mk_bin bf 10 t p2 |
| 270 | gpe_mk_br1 bf bm ir_btok bf t |
| 271 | c7 = ir_res ir_op bc 1 5 7 0 |
| 272 | c11 = ir_res ir_op bc 1 5 11 0 |
| 273 | k = gpe_mk_bin bc 12 p0 c7 |
| 274 | k = gpe_mk_bin bc 10 k p1 |
| 275 | k = gpe_mk_bin bc 12 k c11 |
| 276 | k = gpe_mk_bin bc 10 k p2 |
| 277 | gpe_mk_br1 bc bm ir_btok bc k |
| 278 | end |
| 279 | |
| 280 | fn gpe_build_affine_route to uint |
| 281 | f = gpe_new_fn4 |
| 282 | b0 = ir_blk f |
| 283 | put gpe_bb 0 b0 |
| 284 | put gpe_bb 1 ir_blk f |
| 285 | put gpe_bb 2 ir_blk f |
| 286 | bm = ir_blk f |
| 287 | put gpe_bb 3 bm |
| 288 | mv = ir_bpar bm 5 |
| 289 | c0 = ir_res ir_op b0 1 5 0 0 |
| 290 | cond = gpe_mk_cmp b0 30 get gpe_bp 3 c0 |
| 291 | gpe_mk_cbr b0 ir_ftok f cond get gpe_bb 1 get gpe_bb 2 |
| 292 | gpe_build_affine_arms |
| 293 | c1 = ir_res ir_op bm 1 5 1 0 |
| 294 | m1 = gpe_mk_bin bm 12 mv c1 |
| 295 | z0 = gpe_mk_bin bm 11 get gpe_bp 0 get gpe_bp 0 |
| 296 | gpe_mk_retv bm ir_btok bm gpe_mk_bin bm 10 m1 z0 |
| 297 | ret f |
| 298 | end |
| 299 | |
| 300 | note Workload 2 (4 blocks): branch_diamond(x, y, flag, pad) -> u64 |
| 301 | note Hot arm (flag==1): (x*4 + y) + 0; Cold arm (flag!=1): x*5 + y*9 |
| 302 | fn gpe_build_branch_arms to void |
| 303 | bh = get gpe_bb 1 |
| 304 | bc = get gpe_bb 2 |
| 305 | bj = get gpe_bb 3 |
| 306 | c4 = ir_res ir_op bh 1 5 4 0 |
| 307 | h = gpe_mk_bin bh 12 get gpe_bp 0 c4 |
| 308 | h = gpe_mk_bin bh 10 h get gpe_bp 1 |
| 309 | gpe_mk_br1 bh bj ir_btok bh h |
| 310 | q1 = gpe_mk_bin bc 12 get gpe_bp 0 ir_res ir_op bc 1 5 5 0 |
| 311 | q2 = gpe_mk_bin bc 12 get gpe_bp 1 ir_res ir_op bc 1 5 9 0 |
| 312 | gpe_mk_br1 bc bj ir_btok bc gpe_mk_bin bc 10 q1 q2 |
| 313 | end |
| 314 | |
| 315 | fn gpe_build_branch_diamond to uint |
| 316 | f = gpe_new_fn4 |
| 317 | b0 = ir_blk f |
| 318 | bh = ir_blk f |
| 319 | bc = ir_blk f |
| 320 | bj = ir_blk f |
| 321 | jv = ir_bpar bj 5 |
| 322 | put gpe_bb 0 b0 |
| 323 | put gpe_bb 1 bh |
| 324 | put gpe_bb 2 bc |
| 325 | put gpe_bb 3 bj |
| 326 | c1 = ir_res ir_op b0 1 5 1 0 |
| 327 | cond = gpe_mk_cmp b0 30 get gpe_bp 2 c1 |
| 328 | gpe_mk_cbr b0 ir_ftok f cond bh bc |
| 329 | gpe_build_branch_arms |
| 330 | c0 = ir_res ir_op bj 1 5 0 0 |
| 331 | r = gpe_mk_bin bj 10 jv c0 |
| 332 | gpe_mk_retv bj ir_btok bj r |
| 333 | ret f |
| 334 | end |
| 335 | |
| 336 | note Workload 3 (4-block loop CFG with back-edge): sparse_dot_loop(x, n_iters, mask, stride) -> u64 |
| 337 | note Sums (x + i) * stride for i in 0..n_iters-1 where ((mask >> i) & 1) != 0. |
| 338 | fn gpe_build_sparse_body iv uint ac uint to void |
| 339 | bb = get gpe_bb 2 |
| 340 | c0 = ir_res ir_op bb 1 5 0 0 |
| 341 | c1 = ir_res ir_op bb 1 5 1 0 |
| 342 | t = gpe_mk_bin bb 21 get gpe_bp 2 iv |
| 343 | t = gpe_mk_bin bb 16 t c1 |
| 344 | act = gpe_mk_cmp bb 31 t c0 |
| 345 | t = gpe_mk_bin bb 10 get gpe_bp 0 iv |
| 346 | t = gpe_mk_bin bb 12 t get gpe_bp 3 |
| 347 | so = ir_op bb 2 5 0 3 |
| 348 | ir_set so 0 act |
| 349 | ir_set so 1 t |
| 350 | ir_set so 2 c0 |
| 351 | ac2 = gpe_mk_bin bb 10 ac ir_res so |
| 352 | iv2 = gpe_mk_bin bb 10 iv c1 |
| 353 | gpe_mk_br2 bb get gpe_bb 1 ir_btok bb iv2 ac2 |
| 354 | end |
| 355 | |
| 356 | fn gpe_build_sparse_loop to uint |
| 357 | f = gpe_new_fn4 |
| 358 | b0 = ir_blk f |
| 359 | bh = ir_blk f |
| 360 | iv = ir_bpar bh 5 |
| 361 | ac = ir_bpar bh 5 |
| 362 | bb = ir_blk f |
| 363 | bx = ir_blk f |
| 364 | put gpe_bb 0 b0 |
| 365 | put gpe_bb 1 bh |
| 366 | put gpe_bb 2 bb |
| 367 | put gpe_bb 3 bx |
| 368 | z0 = ir_res ir_op b0 1 5 0 0 |
| 369 | gpe_mk_br2 b0 bh ir_ftok f z0 z0 |
| 370 | cond = gpe_mk_cmp bh 32 iv get gpe_bp 1 |
| 371 | gpe_mk_cbr bh ir_btok bh cond bb bx |
| 372 | gpe_build_sparse_body iv ac |
| 373 | gpe_mk_retv bx ir_btok bx ac |
| 374 | ret f |
| 375 | end |
| 376 | |
| 377 | note ==== Part 3: Exact Multi-Block ΩIR Interpreter (Semantic Oracle) ==== |
| 378 | |
| 379 | fn gpe_eval_pure op ulong a ulong b ulong c ulong to ulong |
| 380 | if eq op 10 |
| 381 | ret add a b |
| 382 | end |
| 383 | if eq op 11 |
| 384 | ret sub a b |
| 385 | end |
| 386 | if eq op 12 |
| 387 | ret mul a b |
| 388 | end |
| 389 | if eq op 13 |
| 390 | if eq b 0 |
| 391 | ret 0 |
| 392 | end |
| 393 | ret div a b |
| 394 | end |
| 395 | if eq op 14 |
| 396 | if eq b 0 |
| 397 | ret 0 |
| 398 | end |
| 399 | ret rem a b |
| 400 | end |
| 401 | if eq op 16 |
| 402 | ret and a b |
| 403 | end |
| 404 | if eq op 17 |
| 405 | ret or a b |
| 406 | end |
| 407 | if eq op 18 |
| 408 | ret xor a b |
| 409 | end |
| 410 | if eq op 20 |
| 411 | ret shl a and b 63 |
| 412 | end |
| 413 | if eq op 21 |
| 414 | ret shr a and b 63 |
| 415 | end |
| 416 | if eq op 46 |
| 417 | ret add mul a b c |
| 418 | end |
| 419 | if eq op 2 |
| 420 | if ne a 0 |
| 421 | ret b |
| 422 | end |
| 423 | ret c |
| 424 | end |
| 425 | if eq op 30 |
| 426 | if eq a b |
| 427 | ret 1 |
| 428 | end |
| 429 | ret 0 |
| 430 | end |
| 431 | if eq op 31 |
| 432 | if ne a b |
| 433 | ret 1 |
| 434 | end |
| 435 | ret 0 |
| 436 | end |
| 437 | if eq op 32 |
| 438 | if lt a b |
| 439 | ret 1 |
| 440 | end |
| 441 | ret 0 |
| 442 | end |
| 443 | if eq op 33 |
| 444 | if le a b |
| 445 | ret 1 |
| 446 | end |
| 447 | ret 0 |
| 448 | end |
| 449 | if eq op 34 |
| 450 | if gt a b |
| 451 | ret 1 |
| 452 | end |
| 453 | ret 0 |
| 454 | end |
| 455 | if eq op 35 |
| 456 | if ge a b |
| 457 | ret 1 |
| 458 | end |
| 459 | ret 0 |
| 460 | end |
| 461 | ret 0 |
| 462 | end |
| 463 | |
| 464 | fn gpe_ev_get vbase ulong h uint to ulong |
| 465 | idx = sub ulong h vbase |
| 466 | if ge idx GPE_MAX_VALS |
| 467 | ret 0 |
| 468 | end |
| 469 | ret get gpe_ev_val idx |
| 470 | end |
| 471 | |
| 472 | fn gpe_ev_set vbase ulong h uint val ulong to void |
| 473 | idx = sub ulong h vbase |
| 474 | if lt idx GPE_MAX_VALS |
| 475 | put gpe_ev_val idx val |
| 476 | end |
| 477 | end |
| 478 | |
| 479 | global gpe_ev_ret ulong 1 |
| 480 | global gpe_ev_done ulong 1 |
| 481 | |
| 482 | fn gpe_eval_br vbase ulong o uint to uint |
| 483 | tgt = ir_arg o 0 |
| 484 | na = ulong get i_ona o |
| 485 | if gt na 2 |
| 486 | nk = sub na 2 |
| 487 | for j ulong nk bound 16 |
| 488 | put gpe_ev_tmp j gpe_ev_get vbase ir_arg o uint add j 2 |
| 489 | end |
| 490 | bp = get i_vnx get i_bp1 tgt |
| 491 | for j ulong nk bound 16 |
| 492 | if eq bp 0 |
| 493 | break |
| 494 | end |
| 495 | gpe_ev_set vbase bp get gpe_ev_tmp j |
| 496 | bp = get i_vnx bp |
| 497 | end |
| 498 | end |
| 499 | ret tgt |
| 500 | end |
| 501 | |
| 502 | fn gpe_eval_blk vbase ulong blk uint to uint |
| 503 | o = get i_bo1 blk |
| 504 | while ne o 0 bound 512 |
| 505 | op = ulong get i_oop o |
| 506 | put gpe_last_ops 0 add get gpe_last_ops 0 1 |
| 507 | if eq op 1 |
| 508 | gpe_ev_set vbase get i_ors o get i_oim o |
| 509 | o = get i_onx o |
| 510 | continue |
| 511 | end |
| 512 | if eq op 63 |
| 513 | put gpe_ev_ret 0 gpe_ev_get vbase ir_arg o 1 |
| 514 | put gpe_ev_done 0 1 |
| 515 | ret 0 |
| 516 | end |
| 517 | if eq op 65 |
| 518 | put gpe_last_trap 0 get i_oim o |
| 519 | put gpe_ev_done 0 1 |
| 520 | ret 0 |
| 521 | end |
| 522 | if eq op 61 |
| 523 | if ne gpe_ev_get vbase ir_arg o 1 0 |
| 524 | ret ir_arg o 2 |
| 525 | end |
| 526 | ret ir_arg o 3 |
| 527 | end |
| 528 | if eq op 60 |
| 529 | ret gpe_eval_br vbase o |
| 530 | end |
| 531 | va = gpe_ev_get vbase ir_arg o 0 |
| 532 | vb = ulong 0 |
| 533 | vc = ulong 0 |
| 534 | if ge get i_ona o 2 |
| 535 | vb = gpe_ev_get vbase ir_arg o 1 |
| 536 | end |
| 537 | if ge get i_ona o 3 |
| 538 | vc = gpe_ev_get vbase ir_arg o 2 |
| 539 | end |
| 540 | gpe_ev_set vbase get i_ors o gpe_eval_pure op va vb vc |
| 541 | o = get i_onx o |
| 542 | end |
| 543 | ret 0 |
| 544 | end |
| 545 | |
| 546 | fn gpe_eval_fn f uint a0 ulong a1 ulong a2 ulong a3 ulong to ulong |
| 547 | vbase = ulong get i_ftk f |
| 548 | put gpe_last_ops 0 0 |
| 549 | put gpe_last_blks 0 0 |
| 550 | put gpe_last_trap 0 0 |
| 551 | put gpe_ev_ret 0 0 |
| 552 | put gpe_ev_done 0 0 |
| 553 | p = get i_fp1 f |
| 554 | k = ulong 0 |
| 555 | while ne p 0 bound 8 |
| 556 | if eq k 0 |
| 557 | gpe_ev_set vbase p a0 |
| 558 | end |
| 559 | if eq k 1 |
| 560 | gpe_ev_set vbase p a1 |
| 561 | end |
| 562 | if eq k 2 |
| 563 | gpe_ev_set vbase p a2 |
| 564 | end |
| 565 | if eq k 3 |
| 566 | gpe_ev_set vbase p a3 |
| 567 | end |
| 568 | p = get i_vnx p |
| 569 | k = add k 1 |
| 570 | end |
| 571 | blk = get i_fb1 f |
| 572 | for step ulong 256 |
| 573 | if eq blk 0 |
| 574 | put gpe_last_trap 0 13 |
| 575 | ret 0 |
| 576 | end |
| 577 | put gpe_last_blks 0 add get gpe_last_blks 0 1 |
| 578 | blk = gpe_eval_blk vbase blk |
| 579 | if ne get gpe_ev_done 0 0 |
| 580 | ret get gpe_ev_ret 0 |
| 581 | end |
| 582 | end |
| 583 | put gpe_last_trap 0 4 |
| 584 | ret 0 |
| 585 | end |
| 586 | |
| 587 | note ==== Part 4: General Multi-Block ΩIR Partial Evaluator + Live ir_egraph -> EGP3 -> egp3_checker ==== |
| 588 | |
| 589 | fn gpe_cfg_reset to void |
| 590 | put gpe_pin_mask 0 0 |
| 591 | for i ulong 4 |
| 592 | put gpe_pin_val i 0 |
| 593 | end |
| 594 | put gpe_cfg_bias 0 5000 |
| 595 | put gpe_cfg_sparse 0 0 |
| 596 | put gpe_cfg_tid 0 TID_X925 |
| 597 | for j ulong 16 |
| 598 | put gpe_pe_stat j 0 |
| 599 | end |
| 600 | end |
| 601 | |
| 602 | fn gpe_cfg_pin idx ulong val ulong to void |
| 603 | if lt idx 4 |
| 604 | put gpe_pin_mask 0 or get gpe_pin_mask 0 shl ulong 1 idx |
| 605 | put gpe_pin_val idx val |
| 606 | end |
| 607 | end |
| 608 | |
| 609 | fn gpe_cfg_set_profile bias ulong sparse ulong tid ulong to void |
| 610 | put gpe_cfg_bias 0 bias |
| 611 | put gpe_cfg_sparse 0 sparse |
| 612 | put gpe_cfg_tid 0 tid |
| 613 | end |
| 614 | |
| 615 | fn gpe_pe_set_k vbase ulong h uint val ulong to void |
| 616 | idx = sub ulong h vbase |
| 617 | if lt idx GPE_MAX_VALS |
| 618 | put gpe_v_kind idx 1 |
| 619 | put gpe_v_cval idx val |
| 620 | put gpe_v_sym idx 0 |
| 621 | end |
| 622 | end |
| 623 | |
| 624 | fn gpe_pe_set_s vbase ulong h uint sym uint to void |
| 625 | idx = sub ulong h vbase |
| 626 | if lt idx GPE_MAX_VALS |
| 627 | put gpe_v_kind idx 2 |
| 628 | put gpe_v_cval idx 0 |
| 629 | put gpe_v_sym idx sym |
| 630 | end |
| 631 | end |
| 632 | |
| 633 | fn gpe_pe_materialize b_hot uint vbase ulong h uint to uint |
| 634 | idx = sub ulong h vbase |
| 635 | if ge idx GPE_MAX_VALS |
| 636 | ret 0 |
| 637 | end |
| 638 | if eq get gpe_v_kind idx 2 |
| 639 | ret get gpe_v_sym idx |
| 640 | end |
| 641 | ret ir_res ir_op b_hot 1 5 get gpe_v_cval idx 0 |
| 642 | end |
| 643 | |
| 644 | fn gpe_pe_blk_idx f uint tgt uint to ulong |
| 645 | b = get i_fb1 f |
| 646 | k = ulong 0 |
| 647 | while ne b 0 bound 64 |
| 648 | if eq b tgt |
| 649 | ret k |
| 650 | end |
| 651 | b = get i_bnx b |
| 652 | k = add k 1 |
| 653 | end |
| 654 | ret 0 |
| 655 | end |
| 656 | |
| 657 | global gpe_ca_k ulong 16 |
| 658 | global gpe_ca_c ulong 16 |
| 659 | global gpe_ca_s uint 16 |
| 660 | |
| 661 | note Copy abstract value state from src block-arg list to dst block parameters (eliminates br PHI args!). |
| 662 | fn gpe_pe_copy_bargs vbase ulong br_op uint dst_blk uint to void |
| 663 | na = ulong get i_ona br_op |
| 664 | if le na 2 |
| 665 | ret |
| 666 | end |
| 667 | nk = sub na 2 |
| 668 | for j ulong nk bound 16 |
| 669 | ai = sub ulong ir_arg br_op uint add j 2 vbase |
| 670 | put gpe_ca_k j get gpe_v_kind ai |
| 671 | put gpe_ca_c j get gpe_v_cval ai |
| 672 | put gpe_ca_s j get gpe_v_sym ai |
| 673 | end |
| 674 | bp = get i_vnx get i_bp1 dst_blk |
| 675 | for j ulong nk bound 16 |
| 676 | if eq bp 0 |
| 677 | break |
| 678 | end |
| 679 | bi = sub ulong bp vbase |
| 680 | put gpe_v_kind bi get gpe_ca_k j |
| 681 | put gpe_v_cval bi get gpe_ca_c j |
| 682 | put gpe_v_sym bi get gpe_ca_s j |
| 683 | bp = get i_vnx bp |
| 684 | end |
| 685 | end |
| 686 | |
| 687 | note Partially evaluate one select op (fold immediately when condition is known constant -> zero sparse ops!). |
| 688 | fn gpe_pe_step_select b_hot uint vbase ulong o uint to void |
| 689 | rs = get i_ors o |
| 690 | a0 = ir_arg o 0 |
| 691 | a1 = ir_arg o 1 |
| 692 | a2 = ir_arg o 2 |
| 693 | i0 = sub ulong a0 vbase |
| 694 | if eq get gpe_v_kind i0 1 |
| 695 | put gpe_pe_stat 4 add get gpe_pe_stat 4 1 |
| 696 | src_i = sub ulong a2 vbase |
| 697 | if ne get gpe_v_cval i0 0 |
| 698 | src_i = sub ulong a1 vbase |
| 699 | end |
| 700 | dst_i = sub ulong rs vbase |
| 701 | put gpe_v_kind dst_i get gpe_v_kind src_i |
| 702 | put gpe_v_cval dst_i get gpe_v_cval src_i |
| 703 | put gpe_v_sym dst_i get gpe_v_sym src_i |
| 704 | ret |
| 705 | end |
| 706 | no = ir_op b_hot 2 5 0 3 |
| 707 | ir_set no 0 gpe_pe_materialize b_hot vbase a0 |
| 708 | ir_set no 1 gpe_pe_materialize b_hot vbase a1 |
| 709 | ir_set no 2 gpe_pe_materialize b_hot vbase a2 |
| 710 | gpe_pe_set_s vbase rs ir_res no |
| 711 | end |
| 712 | |
| 713 | fn gpe_pe_emit_res b_hot uint vbase ulong o uint to void |
| 714 | op = uint get i_oop o |
| 715 | s0 = gpe_pe_materialize b_hot vbase ir_arg o 0 |
| 716 | s1 = gpe_pe_materialize b_hot vbase ir_arg o 1 |
| 717 | if eq op 46 |
| 718 | s2 = gpe_pe_materialize b_hot vbase ir_arg o 2 |
| 719 | no = ir_op b_hot 46 get i_oty o 0 3 |
| 720 | ir_set no 0 s0 |
| 721 | ir_set no 1 s1 |
| 722 | ir_set no 2 s2 |
| 723 | gpe_pe_set_s vbase get i_ors o ir_res no |
| 724 | ret |
| 725 | end |
| 726 | no = ir_op b_hot op get i_oty o 0 2 |
| 727 | ir_set no 0 s0 |
| 728 | ir_set no 1 s1 |
| 729 | gpe_pe_set_s vbase get i_ors o ir_res no |
| 730 | end |
| 731 | |
| 732 | note Partially evaluate one pure op o from f_src into b_hot. |
| 733 | fn gpe_pe_step_pure b_hot uint vbase ulong o uint to void |
| 734 | op = ulong get i_oop o |
| 735 | rs = get i_ors o |
| 736 | if eq op 1 |
| 737 | gpe_pe_set_k vbase rs get i_oim o |
| 738 | ret |
| 739 | end |
| 740 | if eq op 2 |
| 741 | gpe_pe_step_select b_hot vbase o |
| 742 | ret |
| 743 | end |
| 744 | i0 = sub ulong ir_arg o 0 vbase |
| 745 | i1 = sub ulong ir_arg o 1 vbase |
| 746 | if and eq get gpe_v_kind i0 1 eq get gpe_v_kind i1 1 |
| 747 | if eq op 46 |
| 748 | i2 = sub ulong ir_arg o 2 vbase |
| 749 | if eq get gpe_v_kind i2 1 |
| 750 | put gpe_pe_stat 4 add get gpe_pe_stat 4 1 |
| 751 | gpe_pe_set_k vbase rs gpe_eval_pure op get gpe_v_cval i0 get gpe_v_cval i1 get gpe_v_cval i2 |
| 752 | ret |
| 753 | end |
| 754 | else |
| 755 | put gpe_pe_stat 4 add get gpe_pe_stat 4 1 |
| 756 | gpe_pe_set_k vbase rs gpe_eval_pure op get gpe_v_cval i0 get gpe_v_cval i1 0 |
| 757 | ret |
| 758 | end |
| 759 | end |
| 760 | gpe_pe_emit_res b_hot vbase o |
| 761 | end |
| 762 | |
| 763 | note Helper: build f_spec with entry guard blocks FIRST, b_hot next, and b_deopt(trap 14) LAST (valid ΩIR entry!). |
| 764 | fn gpe_build_spec_shell f_src uint to uint |
| 765 | f_spec = ir_fnew 0 0 get i_frs f_src |
| 766 | ir_fset_mode f_spec ulong get i_fpur f_src ulong get i_fprc f_src ulong get i_fhd f_src |
| 767 | ir_fset_phys f_spec get i_ferr f_src get i_fdl f_src get i_fpwr f_src |
| 768 | vbase = ulong get i_ftk f_src |
| 769 | p_src = get i_fp1 f_src |
| 770 | k = ulong 0 |
| 771 | pm = get gpe_pin_mask 0 |
| 772 | while ne p_src 0 bound 8 |
| 773 | p_new = ir_fpar f_spec get i_vty p_src |
| 774 | if ne and pm shl ulong 1 k 0 |
| 775 | gpe_pe_set_k vbase p_src get gpe_pin_val k |
| 776 | else |
| 777 | gpe_pe_set_s vbase p_src p_new |
| 778 | end |
| 779 | p_src = get i_vnx p_src |
| 780 | k = add k 1 |
| 781 | end |
| 782 | ret f_spec |
| 783 | end |
| 784 | |
| 785 | fn gpe_emit_guard_diff b uint p uint v ulong acc uint to uint |
| 786 | d = p |
| 787 | if ne v 0 |
| 788 | ck = ir_res ir_op b 1 5 v 0 |
| 789 | d = gpe_mk_bin b 18 p ck |
| 790 | end |
| 791 | if eq acc 0 |
| 792 | ret d |
| 793 | end |
| 794 | ret gpe_mk_bin b 17 acc d |
| 795 | end |
| 796 | |
| 797 | fn gpe_emit_guards_ordered f_spec uint to uint |
| 798 | pm = get gpe_pin_mask 0 |
| 799 | b_ent = ir_blk f_spec |
| 800 | if eq pm 0 |
| 801 | ret b_ent |
| 802 | end |
| 803 | b_deopt = ir_blk f_spec |
| 804 | td = ir_op b_deopt 65 0 GPE_TRAP_DEOPT 1 |
| 805 | ir_set td 0 ir_btok b_deopt |
| 806 | b_hot = ir_blk f_spec |
| 807 | acc = uint 0 |
| 808 | ps = get i_fp1 f_spec |
| 809 | for k ulong 4 |
| 810 | if eq ps 0 |
| 811 | break |
| 812 | end |
| 813 | if ne and pm shl ulong 1 k 0 |
| 814 | acc = gpe_emit_guard_diff b_ent ps get gpe_pin_val k acc |
| 815 | end |
| 816 | ps = get i_vnx ps |
| 817 | end |
| 818 | c0 = ir_res ir_op b_ent 1 5 0 0 |
| 819 | cond = gpe_mk_cmp b_ent 30 acc c0 |
| 820 | gpe_mk_cbr b_ent ir_ftok f_spec cond b_hot b_deopt |
| 821 | ret b_hot |
| 822 | end |
| 823 | |
| 824 | global gpe_pe_bhot uint 1 |
| 825 | global gpe_pe_tkhot uint 1 |
| 826 | global gpe_pe_done ulong 1 |
| 827 | global gpe_pe_fail ulong 1 |
| 828 | |
| 829 | fn gpe_pe_handle_cbr f_spec uint vbase ulong o uint to uint |
| 830 | ch = ir_arg o 1 |
| 831 | ci = sub ulong ch vbase |
| 832 | if eq get gpe_v_kind ci 1 |
| 833 | put gpe_pe_stat 2 add get gpe_pe_stat 2 1 |
| 834 | if ne get gpe_v_cval ci 0 |
| 835 | ret ir_arg o 2 |
| 836 | end |
| 837 | ret ir_arg o 3 |
| 838 | end |
| 839 | bias = get gpe_cfg_bias 0 |
| 840 | if or ge bias 9000 le bias 1000 |
| 841 | b_deopt = ir_blk f_spec |
| 842 | td = ir_op b_deopt 65 0 GPE_TRAP_DEOPT 1 |
| 843 | ir_set td 0 ir_btok b_deopt |
| 844 | b_cont = ir_blk f_spec |
| 845 | bh = get gpe_pe_bhot 0 |
| 846 | sc = gpe_pe_materialize bh vbase ch |
| 847 | if ge bias 9000 |
| 848 | gpe_mk_cbr bh get gpe_pe_tkhot 0 sc b_cont b_deopt |
| 849 | put gpe_pe_bhot 0 b_cont |
| 850 | put gpe_pe_tkhot 0 ir_btok b_cont |
| 851 | put gpe_pe_stat 2 add get gpe_pe_stat 2 1 |
| 852 | ret ir_arg o 2 |
| 853 | end |
| 854 | gpe_mk_cbr bh get gpe_pe_tkhot 0 sc b_deopt b_cont |
| 855 | put gpe_pe_bhot 0 b_cont |
| 856 | put gpe_pe_tkhot 0 ir_btok b_cont |
| 857 | put gpe_pe_stat 2 add get gpe_pe_stat 2 1 |
| 858 | ret ir_arg o 3 |
| 859 | end |
| 860 | put gpe_pe_fail 0 1 |
| 861 | ret 0 |
| 862 | end |
| 863 | |
| 864 | fn gpe_pe_trace_blk f_spec uint vbase ulong blk uint to uint |
| 865 | o = get i_bo1 blk |
| 866 | while ne o 0 bound 512 |
| 867 | op = ulong get i_oop o |
| 868 | if eq op 63 |
| 869 | bh = get gpe_pe_bhot 0 |
| 870 | rv_h = gpe_pe_materialize bh vbase ir_arg o 1 |
| 871 | gpe_mk_retv bh get gpe_pe_tkhot 0 rv_h |
| 872 | put gpe_pe_stat 11 ulong bh |
| 873 | put gpe_pe_stat 12 ulong rv_h |
| 874 | put gpe_pe_done 0 1 |
| 875 | ret 0 |
| 876 | end |
| 877 | if eq op 65 |
| 878 | tp = ir_op get gpe_pe_bhot 0 65 0 get i_oim o 1 |
| 879 | ir_set tp 0 get gpe_pe_tkhot 0 |
| 880 | put gpe_pe_done 0 1 |
| 881 | ret 0 |
| 882 | end |
| 883 | if eq op 60 |
| 884 | tgt = ir_arg o 0 |
| 885 | gpe_pe_copy_bargs vbase o tgt |
| 886 | ret tgt |
| 887 | end |
| 888 | if eq op 61 |
| 889 | ret gpe_pe_handle_cbr f_spec vbase o |
| 890 | end |
| 891 | gpe_pe_step_pure get gpe_pe_bhot 0 vbase o |
| 892 | o = get i_onx o |
| 893 | end |
| 894 | ret 0 |
| 895 | end |
| 896 | |
| 897 | fn gpe_specialize_fn f_src uint to uint |
| 898 | vbase = ulong get i_ftk f_src |
| 899 | for i ulong GPE_MAX_VALS |
| 900 | put gpe_v_kind i 0 |
| 901 | put gpe_v_cval i 0 |
| 902 | put gpe_v_sym i 0 |
| 903 | end |
| 904 | for b ulong GPE_MAX_BLKS |
| 905 | put gpe_b_vis b 0 |
| 906 | end |
| 907 | for s ulong 16 |
| 908 | put gpe_pe_stat s 0 |
| 909 | end |
| 910 | put gpe_pe_stat 0 ulong get i_fnb f_src |
| 911 | put gpe_pe_done 0 0 |
| 912 | put gpe_pe_fail 0 0 |
| 913 | |
| 914 | f_spec = gpe_build_spec_shell f_src |
| 915 | bh = gpe_emit_guards_ordered f_spec |
| 916 | tk = ir_btok bh |
| 917 | if eq tk 0 |
| 918 | tk = ir_ftok f_spec |
| 919 | end |
| 920 | put gpe_pe_bhot 0 bh |
| 921 | put gpe_pe_tkhot 0 tk |
| 922 | |
| 923 | blk = get i_fb1 f_src |
| 924 | for step ulong 128 |
| 925 | if or ne get gpe_pe_done 0 0 or ne get gpe_pe_fail 0 0 eq blk 0 |
| 926 | break |
| 927 | end |
| 928 | bidx = gpe_pe_blk_idx f_src blk |
| 929 | vis = add get gpe_b_vis bidx 1 |
| 930 | if gt vis GPE_MAX_UNROLL |
| 931 | ret 0 |
| 932 | end |
| 933 | put gpe_b_vis bidx vis |
| 934 | if gt vis 1 |
| 935 | put gpe_pe_stat 3 add get gpe_pe_stat 3 1 |
| 936 | end |
| 937 | blk = gpe_pe_trace_blk f_spec vbase blk |
| 938 | end |
| 939 | |
| 940 | if or eq get gpe_pe_done 0 0 ne get gpe_pe_fail 0 0 |
| 941 | ret 0 |
| 942 | end |
| 943 | put gpe_pe_stat 1 ulong get i_fnb f_spec |
| 944 | ret f_spec |
| 945 | end |
| 946 | |
| 947 | note Materialize winning e-graph node `nid` recursively into target ΩIR block `b_dst` (depth-bounded). |
| 948 | fn gpe_eg_find_ssa_val sid ulong to uint |
| 949 | n = get eg3_ir_vn 0 |
| 950 | for i ulong n bound 256 |
| 951 | nod = get eg3_ir_nod i |
| 952 | if eq get eg3_op nod EG3_OP_SSA |
| 953 | if eq get eg3_vlo nod sid |
| 954 | ret get eg3_ir_val i |
| 955 | end |
| 956 | end |
| 957 | end |
| 958 | ret 0 |
| 959 | end |
| 960 | |
| 961 | fn gpe_eg_map_ssa sid ulong f_spec uint f_opt uint to uint |
| 962 | orig_v = gpe_eg_find_ssa_val sid |
| 963 | p_s = get i_fp1 f_spec |
| 964 | p_o = get i_fp1 f_opt |
| 965 | while ne p_s 0 bound 8 |
| 966 | if eq p_s orig_v |
| 967 | ret p_o |
| 968 | end |
| 969 | p_s = get i_vnx p_s |
| 970 | p_o = get i_vnx p_o |
| 971 | end |
| 972 | ret 0 |
| 973 | end |
| 974 | |
| 975 | fn gpe_eg_op_to_ir op ulong to uint |
| 976 | if eq op EG3_OP_SUB |
| 977 | ret 11 |
| 978 | end |
| 979 | if eq op EG3_OP_MUL |
| 980 | ret 12 |
| 981 | end |
| 982 | if eq op EG3_OP_SHL |
| 983 | ret 20 |
| 984 | end |
| 985 | if eq op EG3_OP_SHR |
| 986 | ret 21 |
| 987 | end |
| 988 | if eq op EG3_OP_AND |
| 989 | ret 16 |
| 990 | end |
| 991 | if eq op EG3_OP_OR |
| 992 | ret 17 |
| 993 | end |
| 994 | if eq op EG3_OP_XOR |
| 995 | ret 18 |
| 996 | end |
| 997 | ret 10 |
| 998 | end |
| 999 | |
| 1000 | fn gpe_eg_materialize b_dst uint nid ulong f_spec uint f_opt uint depth ulong to uint |
| 1001 | if gt depth 32 |
| 1002 | ret 0 |
| 1003 | end |
| 1004 | b = eg3_get_best nid |
| 1005 | if eq get gpe_eg_mat_ok b 1 |
| 1006 | ret get gpe_eg_mat b |
| 1007 | end |
| 1008 | op = get eg3_op b |
| 1009 | if eq op EG3_OP_CONST |
| 1010 | vh = ir_res ir_op b_dst 1 5 eg3_node_val b 0 |
| 1011 | put gpe_eg_mat b vh |
| 1012 | put gpe_eg_mat_ok b 1 |
| 1013 | ret vh |
| 1014 | end |
| 1015 | if eq op EG3_OP_SSA |
| 1016 | vh = gpe_eg_map_ssa get eg3_vlo b f_spec f_opt |
| 1017 | if ne vh 0 |
| 1018 | put gpe_eg_mat b vh |
| 1019 | put gpe_eg_mat_ok b 1 |
| 1020 | end |
| 1021 | ret vh |
| 1022 | end |
| 1023 | va = gpe_eg_materialize b_dst get eg3_a b f_spec f_opt add depth 1 |
| 1024 | vb = gpe_eg_materialize b_dst get eg3_b b f_spec f_opt add depth 1 |
| 1025 | if or eq va 0 eq vb 0 |
| 1026 | ret 0 |
| 1027 | end |
| 1028 | if eq op EG3_OP_FMA |
| 1029 | vc = gpe_eg_materialize b_dst get eg3_c b f_spec f_opt add depth 1 |
| 1030 | if eq vc 0 |
| 1031 | ret 0 |
| 1032 | end |
| 1033 | nf = ir_op b_dst 46 5 0 3 |
| 1034 | ir_set nf 0 va |
| 1035 | ir_set nf 1 vb |
| 1036 | ir_set nf 2 vc |
| 1037 | vh = ir_res nf |
| 1038 | put gpe_eg_mat b vh |
| 1039 | put gpe_eg_mat_ok b 1 |
| 1040 | ret vh |
| 1041 | end |
| 1042 | vh = gpe_mk_bin b_dst gpe_eg_op_to_ir op va vb |
| 1043 | put gpe_eg_mat b vh |
| 1044 | put gpe_eg_mat_ok b 1 |
| 1045 | ret vh |
| 1046 | end |
| 1047 | |
| 1048 | fn gpe_eg_copy_and_verify to ulong |
| 1049 | nb = get eg3_bpos 0 |
| 1050 | put gpe_pe_stat 14 nb |
| 1051 | put gpe_egp3_len 0 nb |
| 1052 | for i ulong nb bound 131072 |
| 1053 | b = get eg3_buf i |
| 1054 | put gpe_egp3_buf i b |
| 1055 | put vc3_buf i b |
| 1056 | end |
| 1057 | vrc = vc3_verify nb |
| 1058 | put gpe_pe_stat 7 vrc |
| 1059 | put gpe_pe_stat 8 get vc3_stat 0 |
| 1060 | put gpe_pe_stat 9 get vc3_stat 1 |
| 1061 | put gpe_pe_stat 10 get vc3_stat 2 |
| 1062 | ret vrc |
| 1063 | end |
| 1064 | |
| 1065 | note Feed specialized ΩIR block through live ir_egraph.om -> emit live EGP3 cert -> verify via zero-shared-code egp3_checker.om! |
| 1066 | note If verified and strictly lower cost, materializes optimal post-egraph ΩIR function f_opt! |
| 1067 | fn gpe_prove_and_optimize f_spec uint target_tid ulong to uint |
| 1068 | b_hot = uint get gpe_pe_stat 11 |
| 1069 | if eq b_hot 0 |
| 1070 | ret 0 |
| 1071 | end |
| 1072 | eg3_cert_init |
| 1073 | eg3_set_target_costs tdGet target_tid TDF_EXEC tdGet target_tid TDF_MULLAT tdGet target_tid TDF_DIVLAT tdGet target_tid TDF_FEAT |
| 1074 | delta = eg3_opt_ir_block 0 0 b_hot 64 true |
| 1075 | put gpe_pe_stat 5 get eg3_ir_stat 5 |
| 1076 | put gpe_pe_stat 6 get eg3_ir_stat 6 |
| 1077 | if or ne gpe_eg_copy_and_verify 0 eq get vc3_stat 0 0 |
| 1078 | ret 0 |
| 1079 | end |
| 1080 | |
| 1081 | f_opt = ir_fnew 0 0 get i_frs f_spec |
| 1082 | ir_fset_mode f_opt ulong get i_fpur f_spec ulong get i_fprc f_spec ulong get i_fhd f_spec |
| 1083 | ir_fset_phys f_opt get i_ferr f_spec get i_fdl f_spec get i_fpwr f_spec |
| 1084 | p_s = get i_fp1 f_spec |
| 1085 | while ne p_s 0 bound 8 |
| 1086 | ign = ir_fpar f_opt get i_vty p_s |
| 1087 | p_s = get i_vnx p_s |
| 1088 | end |
| 1089 | b_opt = gpe_emit_guards_ordered f_opt |
| 1090 | tk_opt = ir_btok b_opt |
| 1091 | if eq tk_opt 0 |
| 1092 | tk_opt = ir_ftok f_opt |
| 1093 | end |
| 1094 | for j ulong 512 |
| 1095 | put gpe_eg_mat j 0 |
| 1096 | put gpe_eg_mat_ok j 0 |
| 1097 | end |
| 1098 | res_v = gpe_eg_materialize b_opt get eg3_ir_stat 8 f_spec f_opt 0 |
| 1099 | if eq res_v 0 |
| 1100 | ret 0 |
| 1101 | end |
| 1102 | gpe_mk_retv b_opt tk_opt res_v |
| 1103 | put gpe_pe_stat 13 ulong f_opt |
| 1104 | ret f_opt |
| 1105 | end |
| 1106 | |
| 1107 | note ==== Part 5: Real Native AArch64 Machine-Code Compiler from Multi-Block ΩIR ==== |
| 1108 | note Emits real Arm ARM DDI 0487 AArch64 instructions into isBuf[0..1024) using isel.om (isEmitOp TID_X925), |
| 1109 | note wrapped into a strict W^X static Linux AArch64 ELF64 (PT_LOAD PF_R|PF_X=5) via elfEmitStatic! |
| 1110 | note Runtime ABI of emitted ELF: |
| 1111 | note stdin: 40 bytes = 5 x u64 LE (a0, a1, a2, a3, repeat_count) |
| 1112 | note stdout: 8 bytes = 1 x u64 LE return value |
| 1113 | note exit: 0 on normal return, 100+code (114 for OOD trap 14) on trap! |
| 1114 | |
| 1115 | fn gpe_a6_emitw w ulong to void |
| 1116 | if gt add get isLen 0 4 1020 |
| 1117 | put gpe_a6_ovf 0 1 |
| 1118 | ret |
| 1119 | end |
| 1120 | isEmitU32 w |
| 1121 | put gpe_a6_insns 0 add get gpe_a6_insns 0 1 |
| 1122 | end |
| 1123 | |
| 1124 | fn gpe_a6_patch_u32 off ulong w ulong to void |
| 1125 | put isBuf off uchar and w 255 |
| 1126 | put isBuf add off 1 uchar and shr w 8 255 |
| 1127 | put isBuf add off 2 uchar and shr w 16 255 |
| 1128 | put isBuf add off 3 uchar and shr w 24 255 |
| 1129 | end |
| 1130 | |
| 1131 | fn gpe_a6_movz rd ulong imm16 ulong hw ulong to void |
| 1132 | w = or 0xd2800000 or shl hw 21 or shl and imm16 65535 5 rd |
| 1133 | gpe_a6_emitw w |
| 1134 | end |
| 1135 | |
| 1136 | fn gpe_a6_movk rd ulong imm16 ulong hw ulong to void |
| 1137 | w = or 0xf2800000 or shl hw 21 or shl and imm16 65535 5 rd |
| 1138 | gpe_a6_emitw w |
| 1139 | end |
| 1140 | |
| 1141 | fn gpe_a6_mov_imm rd ulong val ulong to void |
| 1142 | gpe_a6_movz rd and val 65535 0 |
| 1143 | v1 = and shr val 16 65535 |
| 1144 | if ne v1 0 |
| 1145 | gpe_a6_movk rd v1 1 |
| 1146 | end |
| 1147 | v2 = and shr val 32 65535 |
| 1148 | if ne v2 0 |
| 1149 | gpe_a6_movk rd v2 2 |
| 1150 | end |
| 1151 | v3 = and shr val 48 65535 |
| 1152 | if ne v3 0 |
| 1153 | gpe_a6_movk rd v3 3 |
| 1154 | end |
| 1155 | end |
| 1156 | |
| 1157 | fn gpe_a6_mov_reg rd ulong rm ulong to void |
| 1158 | if eq rd rm |
| 1159 | ret |
| 1160 | end |
| 1161 | gpe_a6_emitw or 0xaa0003e0 or shl rm 16 rd |
| 1162 | end |
| 1163 | |
| 1164 | fn gpe_a6_ldr_sp rt ulong idx8 ulong to void |
| 1165 | gpe_a6_emitw or 0xf94003e0 or shl idx8 10 rt |
| 1166 | end |
| 1167 | |
| 1168 | fn gpe_a6_str_sp rt ulong idx8 ulong to void |
| 1169 | gpe_a6_emitw or 0xf90003e0 or shl idx8 10 rt |
| 1170 | end |
| 1171 | |
| 1172 | fn gpe_a6_vreg h uint to ulong |
| 1173 | for k ulong 4 |
| 1174 | if eq h get gpe_a6_p_h k |
| 1175 | ret add 19 k |
| 1176 | end |
| 1177 | end |
| 1178 | idx = sub ulong h get gpe_a6_vbase 0 |
| 1179 | sl = get gpe_a6_vslot idx |
| 1180 | if eq sl 0 |
| 1181 | sl = get gpe_a6_vnext 0 |
| 1182 | put gpe_a6_vslot idx sl |
| 1183 | put gpe_a6_vnext 0 add sl 1 |
| 1184 | end |
| 1185 | if le sl 10 |
| 1186 | ret add 8 sl |
| 1187 | end |
| 1188 | ret 99 |
| 1189 | end |
| 1190 | |
| 1191 | fn gpe_a6_vstack_idx h uint to ulong |
| 1192 | idx = sub ulong h get gpe_a6_vbase 0 |
| 1193 | sl = get gpe_a6_vslot idx |
| 1194 | ret add 12 sub sl 11 |
| 1195 | end |
| 1196 | |
| 1197 | fn gpe_a6_load_to dst_r ulong h uint to ulong |
| 1198 | r = gpe_a6_vreg h |
| 1199 | if lt r 32 |
| 1200 | ret r |
| 1201 | end |
| 1202 | gpe_a6_ldr_sp dst_r gpe_a6_vstack_idx h |
| 1203 | ret dst_r |
| 1204 | end |
| 1205 | |
| 1206 | fn gpe_a6_store_from h uint src_r ulong to void |
| 1207 | r = gpe_a6_vreg h |
| 1208 | if lt r 32 |
| 1209 | gpe_a6_mov_reg r src_r |
| 1210 | ret |
| 1211 | end |
| 1212 | gpe_a6_str_sp src_r gpe_a6_vstack_idx h |
| 1213 | end |
| 1214 | |
| 1215 | fn gpe_a6_add_fix tgt uint kind ulong reg ulong to void |
| 1216 | n = get gpe_a6_nfix 0 |
| 1217 | if lt n GPE_MAX_FIX |
| 1218 | put gpe_a6_fpc n get isLen 0 |
| 1219 | put gpe_a6_ftgt n tgt |
| 1220 | put gpe_a6_fkind n kind |
| 1221 | put gpe_a6_freg n reg |
| 1222 | put gpe_a6_nfix 0 add n 1 |
| 1223 | end |
| 1224 | gpe_a6_emitw 0xd503201f |
| 1225 | end |
| 1226 | |
| 1227 | fn gpe_a6_cmp_cond op ulong to ulong |
| 1228 | if eq op 30 |
| 1229 | ret 0 |
| 1230 | end |
| 1231 | if eq op 31 |
| 1232 | ret 1 |
| 1233 | end |
| 1234 | if eq op 32 |
| 1235 | ret 3 |
| 1236 | end |
| 1237 | if eq op 33 |
| 1238 | ret 9 |
| 1239 | end |
| 1240 | if eq op 34 |
| 1241 | ret 8 |
| 1242 | end |
| 1243 | ret 2 |
| 1244 | end |
| 1245 | |
| 1246 | fn gpe_a6_emit_br o uint to void |
| 1247 | tgt = ir_arg o 0 |
| 1248 | na = ulong get i_ona o |
| 1249 | if gt na 2 |
| 1250 | nk = sub na 2 |
| 1251 | for j ulong nk bound 8 |
| 1252 | rv = gpe_a6_load_to 0 ir_arg o uint add j 2 |
| 1253 | gpe_a6_str_sp rv add 6 j |
| 1254 | end |
| 1255 | bp = get i_vnx get i_bp1 tgt |
| 1256 | for j ulong nk bound 8 |
| 1257 | if eq bp 0 |
| 1258 | break |
| 1259 | end |
| 1260 | gpe_a6_ldr_sp 0 add 6 j |
| 1261 | gpe_a6_store_from bp 0 |
| 1262 | bp = get i_vnx bp |
| 1263 | end |
| 1264 | end |
| 1265 | gpe_a6_add_fix tgt 0 0 |
| 1266 | end |
| 1267 | |
| 1268 | fn gpe_a6_emit_pure o uint op ulong to void |
| 1269 | rs = get i_ors o |
| 1270 | ra = gpe_a6_load_to 0 ir_arg o 0 |
| 1271 | rb = gpe_a6_load_to 1 ir_arg o 1 |
| 1272 | rd = gpe_a6_vreg rs |
| 1273 | tr = rd |
| 1274 | if ge tr 32 |
| 1275 | tr = ulong 0 |
| 1276 | end |
| 1277 | if or eq op 10 or eq op 11 eq op 12 |
| 1278 | oir = OIR_ADD |
| 1279 | if eq op 11 |
| 1280 | oir = OIR_SUB |
| 1281 | end |
| 1282 | if eq op 12 |
| 1283 | oir = OIR_MUL |
| 1284 | end |
| 1285 | ok = isEmitOp TID_X925 oir tr ra rb |
| 1286 | put gpe_a6_insns 0 add get gpe_a6_insns 0 1 |
| 1287 | if ge rd 32 |
| 1288 | gpe_a6_store_from rs tr |
| 1289 | end |
| 1290 | ret |
| 1291 | end |
| 1292 | if eq op 16 |
| 1293 | gpe_a6_emitw or 0x8a000000 or shl rb 16 or shl ra 5 tr |
| 1294 | end |
| 1295 | if eq op 17 |
| 1296 | gpe_a6_emitw or 0xaa000000 or shl rb 16 or shl ra 5 tr |
| 1297 | end |
| 1298 | if eq op 18 |
| 1299 | gpe_a6_emitw or 0xca000000 or shl rb 16 or shl ra 5 tr |
| 1300 | end |
| 1301 | if eq op 20 |
| 1302 | gpe_a6_emitw or 0x9ac02000 or shl rb 16 or shl ra 5 tr |
| 1303 | end |
| 1304 | if eq op 21 |
| 1305 | gpe_a6_emitw or 0x9ac02400 or shl rb 16 or shl ra 5 tr |
| 1306 | end |
| 1307 | if eq op 46 |
| 1308 | rc = gpe_a6_load_to 2 ir_arg o 2 |
| 1309 | gpe_a6_emitw or 0x9b000000 or shl rb 16 or shl rc 10 or shl ra 5 tr |
| 1310 | end |
| 1311 | if eq op 2 |
| 1312 | rc = gpe_a6_load_to 2 ir_arg o 2 |
| 1313 | gpe_a6_emitw or 0xf100001f shl ra 5 |
| 1314 | gpe_a6_emitw or 0x9a801000 or shl rc 16 or shl rb 5 tr |
| 1315 | end |
| 1316 | if and ge op 30 le op 35 |
| 1317 | gpe_a6_emitw or 0xeb00001f or shl rb 16 shl ra 5 |
| 1318 | gpe_a6_emitw or 0x9a9f07e0 or shl xor gpe_a6_cmp_cond op 1 12 tr |
| 1319 | end |
| 1320 | if ge rd 32 |
| 1321 | gpe_a6_store_from rs tr |
| 1322 | end |
| 1323 | end |
| 1324 | |
| 1325 | fn gpe_a6_emit_op o uint to void |
| 1326 | op = ulong get i_oop o |
| 1327 | if eq op 1 |
| 1328 | rs = get i_ors o |
| 1329 | rd = gpe_a6_vreg rs |
| 1330 | if lt rd 32 |
| 1331 | gpe_a6_mov_imm rd get i_oim o |
| 1332 | else |
| 1333 | gpe_a6_mov_imm 0 get i_oim o |
| 1334 | gpe_a6_store_from rs 0 |
| 1335 | end |
| 1336 | ret |
| 1337 | end |
| 1338 | if eq op 63 |
| 1339 | rv = gpe_a6_load_to 0 ir_arg o 1 |
| 1340 | gpe_a6_mov_reg 25 rv |
| 1341 | gpe_a6_add_fix 0 2 0 |
| 1342 | ret |
| 1343 | end |
| 1344 | if eq op 65 |
| 1345 | gpe_a6_movz 0 add 100 get i_oim o 0 |
| 1346 | gpe_a6_movz 8 93 0 |
| 1347 | gpe_a6_emitw 0xd4000001 |
| 1348 | ret |
| 1349 | end |
| 1350 | if eq op 61 |
| 1351 | rc = gpe_a6_load_to 0 ir_arg o 1 |
| 1352 | gpe_a6_add_fix ir_arg o 2 1 rc |
| 1353 | gpe_a6_add_fix ir_arg o 3 0 0 |
| 1354 | ret |
| 1355 | end |
| 1356 | if eq op 60 |
| 1357 | gpe_a6_emit_br o |
| 1358 | ret |
| 1359 | end |
| 1360 | gpe_a6_emit_pure o op |
| 1361 | end |
| 1362 | |
| 1363 | fn gpe_a6_find_boff tgt uint to ulong |
| 1364 | n = get gpe_a6_nblk 0 |
| 1365 | for i ulong n bound 64 |
| 1366 | if eq get gpe_a6_blks i tgt |
| 1367 | ret get gpe_a6_boff i |
| 1368 | end |
| 1369 | end |
| 1370 | ret 0 |
| 1371 | end |
| 1372 | |
| 1373 | fn gpe_a6_emit_prologue to void |
| 1374 | gpe_a6_emitw 0xd11003ff |
| 1375 | gpe_a6_movz 0 0 0 |
| 1376 | gpe_a6_emitw 0x910003e1 |
| 1377 | gpe_a6_movz 2 40 0 |
| 1378 | gpe_a6_movz 8 63 0 |
| 1379 | gpe_a6_emitw 0xd4000001 |
| 1380 | gpe_a6_ldr_sp 19 0 |
| 1381 | gpe_a6_ldr_sp 20 1 |
| 1382 | gpe_a6_ldr_sp 21 2 |
| 1383 | gpe_a6_ldr_sp 22 3 |
| 1384 | gpe_a6_ldr_sp 23 4 |
| 1385 | put gpe_a6_top_pc 0 get isLen 0 |
| 1386 | end |
| 1387 | |
| 1388 | fn gpe_a6_sbits d long bits ulong to ulong |
| 1389 | mod = shl ulong 1 bits |
| 1390 | mask = sub mod 1 |
| 1391 | if ge d 0 |
| 1392 | ret and ulong d mask |
| 1393 | end |
| 1394 | mag = and ulong neg d mask |
| 1395 | ret and sub mod mag mask |
| 1396 | end |
| 1397 | |
| 1398 | fn gpe_a6_emit_epilogue to ulong |
| 1399 | epi_pc = get isLen 0 |
| 1400 | put gpe_a6_epi_pc 0 epi_pc |
| 1401 | gpe_a6_emitw 0xf10006f7 |
| 1402 | back_d = div sub long get gpe_a6_top_pc 0 long get isLen 0 4 |
| 1403 | gpe_a6_emitw or 0x54000008 shl gpe_a6_sbits back_d 19 5 |
| 1404 | gpe_a6_str_sp 25 0 |
| 1405 | gpe_a6_movz 0 1 0 |
| 1406 | gpe_a6_emitw 0x910003e1 |
| 1407 | gpe_a6_movz 2 8 0 |
| 1408 | gpe_a6_movz 8 64 0 |
| 1409 | gpe_a6_emitw 0xd4000001 |
| 1410 | gpe_a6_movz 0 0 0 |
| 1411 | gpe_a6_movz 8 93 0 |
| 1412 | gpe_a6_emitw 0xd4000001 |
| 1413 | ret epi_pc |
| 1414 | end |
| 1415 | |
| 1416 | fn gpe_a6_patch_one i ulong epi_pc ulong to void |
| 1417 | fpc = get gpe_a6_fpc i |
| 1418 | fk = get gpe_a6_fkind i |
| 1419 | tpc = epi_pc |
| 1420 | if ne fk 2 |
| 1421 | tpc = gpe_a6_find_boff get gpe_a6_ftgt i |
| 1422 | end |
| 1423 | disp = div sub long tpc long fpc 4 |
| 1424 | if eq fk 1 |
| 1425 | im = shl gpe_a6_sbits disp 19 5 |
| 1426 | w = or 0xb5000000 or im get gpe_a6_freg i |
| 1427 | gpe_a6_patch_u32 fpc w |
| 1428 | else |
| 1429 | w = or 0x14000000 gpe_a6_sbits disp 26 |
| 1430 | gpe_a6_patch_u32 fpc w |
| 1431 | end |
| 1432 | end |
| 1433 | |
| 1434 | fn gpe_a6_patch_fixups epi_pc ulong to void |
| 1435 | nf = get gpe_a6_nfix 0 |
| 1436 | for i ulong nf bound 128 |
| 1437 | gpe_a6_patch_one i epi_pc |
| 1438 | end |
| 1439 | end |
| 1440 | |
| 1441 | note Compile multi-block ΩIR function f directly to a static AArch64 ELF64 executable in elfBuf[0..elfLen). |
| 1442 | note Returns total ELF byte length (0 on overflow/error); sets gpe_a6_insns to exact emitted AArch64 instruction count. |
| 1443 | fn gpe_compile_a64_elf f uint to ulong |
| 1444 | isBufReset |
| 1445 | put gpe_a6_vbase 0 ulong get i_ftk f |
| 1446 | put gpe_a6_vnext 0 1 |
| 1447 | put gpe_a6_nblk 0 0 |
| 1448 | put gpe_a6_nfix 0 0 |
| 1449 | put gpe_a6_insns 0 0 |
| 1450 | put gpe_a6_ovf 0 0 |
| 1451 | for i ulong GPE_MAX_VALS |
| 1452 | put gpe_a6_vslot i 0 |
| 1453 | end |
| 1454 | for p ulong 4 |
| 1455 | put gpe_a6_p_h p 0 |
| 1456 | end |
| 1457 | ph = get i_fp1 f |
| 1458 | pk = ulong 0 |
| 1459 | while ne ph 0 bound 4 |
| 1460 | put gpe_a6_p_h pk ph |
| 1461 | ph = get i_vnx ph |
| 1462 | pk = add pk 1 |
| 1463 | end |
| 1464 | gpe_a6_emit_prologue |
| 1465 | b = get i_fb1 f |
| 1466 | while ne b 0 bound 64 |
| 1467 | nb = get gpe_a6_nblk 0 |
| 1468 | put gpe_a6_blks nb b |
| 1469 | put gpe_a6_boff nb get isLen 0 |
| 1470 | put gpe_a6_nblk 0 add nb 1 |
| 1471 | o = get i_bo1 b |
| 1472 | while ne o 0 bound 256 |
| 1473 | gpe_a6_emit_op o |
| 1474 | o = get i_onx o |
| 1475 | end |
| 1476 | b = get i_bnx b |
| 1477 | end |
| 1478 | epi_pc = gpe_a6_emit_epilogue |
| 1479 | gpe_a6_patch_fixups epi_pc |
| 1480 | if ne get gpe_a6_ovf 0 0 |
| 1481 | ret 0 |
| 1482 | end |
| 1483 | ret elfEmitStatic TID_X925 |
| 1484 | end |
| 1485 | |
| 1486 | note ==== Part 6: Strict W^X (RW -> SHA-256 -> RX) Staging, Sealing & Native Execution ==== |
| 1487 | |
| 1488 | fn wx_init to void |
| 1489 | put wx_rwx_refusals 0 0 |
| 1490 | put wx_sha_refusals 0 0 |
| 1491 | put wx_mode_refusals 0 0 |
| 1492 | put wx_seals_ok 0 0 |
| 1493 | put wx_last_exit 0 0 |
| 1494 | put wx_last_ns 0 0 |
| 1495 | put wx_last_out 0 0 |
| 1496 | n = len wx_t_def_dir |
| 1497 | for i ulong n bound 512 |
| 1498 | put wx_dir i get wx_t_def_dir i |
| 1499 | end |
| 1500 | put wx_dir_len 0 n |
| 1501 | end |
| 1502 | |
| 1503 | fn wx_set_dir d view uchar * to void |
| 1504 | n = len d |
| 1505 | if gt n 440 |
| 1506 | n = ulong 440 |
| 1507 | end |
| 1508 | for i ulong n bound 440 |
| 1509 | put wx_dir i get d i |
| 1510 | end |
| 1511 | put wx_dir_len 0 n |
| 1512 | r = makedir wx_dir 0 n 448 |
| 1513 | end |
| 1514 | |
| 1515 | fn wx_set_dir_buf n ulong to void |
| 1516 | if gt n 440 |
| 1517 | n = ulong 440 |
| 1518 | end |
| 1519 | put wx_dir_len 0 n |
| 1520 | r = makedir wx_dir 0 n 448 |
| 1521 | end |
| 1522 | |
| 1523 | note Strict W^X policy gate: REFUSE ANY protection mask with BOTH PROT_WRITE(2) and PROT_EXEC(4) set! |
| 1524 | fn wx_validate_prot prot ulong to ulong |
| 1525 | if eq and prot 6 6 |
| 1526 | put wx_rwx_refusals 0 add get wx_rwx_refusals 0 1 |
| 1527 | ret WX_ERR_RWX_FORBIDDEN |
| 1528 | end |
| 1529 | ret WX_OK |
| 1530 | end |
| 1531 | |
| 1532 | note Inspect ELF64 header & PT_LOAD program header: MUST be EM_AARCH64=183 and PT_LOAD PF_R|PF_X (5), NEVER PF_W|PF_X! |
| 1533 | fn wx_verify_elf_wx n ulong to ulong |
| 1534 | if lt n 136 |
| 1535 | ret WX_ERR_ELF_BAD |
| 1536 | end |
| 1537 | if or ne get elfBuf 0 127 or ne get elfBuf 1 69 or ne get elfBuf 2 76 ne get elfBuf 3 70 |
| 1538 | ret WX_ERR_ELF_BAD |
| 1539 | end |
| 1540 | if ne get elfBuf 18 183 |
| 1541 | ret WX_ERR_ELF_BAD |
| 1542 | end |
| 1543 | if ne get elfBuf 64 1 |
| 1544 | ret WX_ERR_ELF_BAD |
| 1545 | end |
| 1546 | pf = ulong get elfBuf 68 |
| 1547 | note ELF p_flags: PF_X=1, PF_W=2, PF_R=4 -> PF_W|PF_X (3) is RWX and strictly FORBIDDEN! |
| 1548 | if eq and pf 3 3 |
| 1549 | put wx_rwx_refusals 0 add get wx_rwx_refusals 0 1 |
| 1550 | ret WX_ERR_RWX_FORBIDDEN |
| 1551 | end |
| 1552 | if ne pf 5 |
| 1553 | ret WX_ERR_NOT_RX |
| 1554 | end |
| 1555 | ret WX_OK |
| 1556 | end |
| 1557 | |
| 1558 | fn wx_p_append dst ulong s view uchar * to void |
| 1559 | n = len s |
| 1560 | if eq dst 0 |
| 1561 | p = get wx_p_stagel 0 |
| 1562 | for i ulong n bound 64 |
| 1563 | put wx_p_stage add p i get s i |
| 1564 | end |
| 1565 | put wx_p_stagel 0 add p n |
| 1566 | else |
| 1567 | p = get wx_p_rxl 0 |
| 1568 | for i ulong n bound 64 |
| 1569 | put wx_p_rx add p i get s i |
| 1570 | end |
| 1571 | put wx_p_rxl 0 add p n |
| 1572 | end |
| 1573 | end |
| 1574 | |
| 1575 | fn wx_p_append_u64 dst ulong v ulong to void |
| 1576 | tmp = new uchar 24 0 |
| 1577 | idx = ulong 24 |
| 1578 | x = v |
| 1579 | for i ulong 21 |
| 1580 | idx = sub idx 1 |
| 1581 | put tmp idx uchar add 48 rem x 10 |
| 1582 | x = div x 10 |
| 1583 | if eq x 0 |
| 1584 | break |
| 1585 | end |
| 1586 | end |
| 1587 | if eq dst 0 |
| 1588 | p = get wx_p_stagel 0 |
| 1589 | while lt idx 24 bound 24 |
| 1590 | put wx_p_stage p get tmp idx |
| 1591 | p = add p 1 |
| 1592 | idx = add idx 1 |
| 1593 | end |
| 1594 | put wx_p_stagel 0 p |
| 1595 | else |
| 1596 | p = get wx_p_rxl 0 |
| 1597 | while lt idx 24 bound 24 |
| 1598 | put wx_p_rx p get tmp idx |
| 1599 | p = add p 1 |
| 1600 | idx = add idx 1 |
| 1601 | end |
| 1602 | put wx_p_rxl 0 p |
| 1603 | end |
| 1604 | end |
| 1605 | |
| 1606 | fn wx_build_paths slot ulong ver ulong to void |
| 1607 | dn = get wx_dir_len 0 |
| 1608 | for i ulong dn bound 512 |
| 1609 | b = get wx_dir i |
| 1610 | put wx_p_stage i b |
| 1611 | put wx_p_rx i b |
| 1612 | end |
| 1613 | put wx_p_stagel 0 dn |
| 1614 | put wx_p_rxl 0 dn |
| 1615 | wx_p_append 0 wx_t_sl |
| 1616 | wx_p_append_u64 0 slot |
| 1617 | wx_p_append 0 wx_t_v |
| 1618 | wx_p_append_u64 0 ver |
| 1619 | wx_p_append 0 wx_t_stg |
| 1620 | wx_p_append 1 wx_t_sl |
| 1621 | wx_p_append_u64 1 slot |
| 1622 | wx_p_append 1 wx_t_v |
| 1623 | wx_p_append_u64 1 ver |
| 1624 | wx_p_append 1 wx_t_rx |
| 1625 | end |
| 1626 | |
| 1627 | note Check Linux AArch64 stat st_mode (offset 16) on global wx_p_rx[0..plen): MUST have X (0100=64) and ZERO W bits (0222=146)! |
| 1628 | fn wx_check_stat_rx_buf plen ulong to ulong |
| 1629 | r = stat wx_p_rx 0 plen wx_stat_buf 0 128 |
| 1630 | if ne r 0 |
| 1631 | ret WX_ERR_IO |
| 1632 | end |
| 1633 | m = ulong get wx_stat_buf 16 |
| 1634 | m = or m shl ulong get wx_stat_buf 17 8 |
| 1635 | if ne and m 146 0 |
| 1636 | put wx_mode_refusals 0 add get wx_mode_refusals 0 1 |
| 1637 | ret WX_ERR_MODE_WRITABLE |
| 1638 | end |
| 1639 | if eq and m 64 0 |
| 1640 | ret WX_ERR_NOT_RX |
| 1641 | end |
| 1642 | ret WX_OK |
| 1643 | end |
| 1644 | |
| 1645 | fn wx_write_and_verify_stage elen ulong to ulong |
| 1646 | for i ulong elen bound 4096 |
| 1647 | put sh_dat i get elfBuf i |
| 1648 | end |
| 1649 | sh_all elen |
| 1650 | for j ulong 32 |
| 1651 | put wx_sha_exp j get sh_dig j |
| 1652 | end |
| 1653 | z = unlink wx_p_stage 0 get wx_p_stagel 0 |
| 1654 | fd = fopen wx_p_stage 0 get wx_p_stagel 0 578 384 |
| 1655 | if lt fd 0 |
| 1656 | ret WX_ERR_IO |
| 1657 | end |
| 1658 | if ne write int fd elfBuf 0 elen long elen |
| 1659 | z = fclose int fd |
| 1660 | z = unlink wx_p_stage 0 get wx_p_stagel 0 |
| 1661 | ret WX_ERR_IO |
| 1662 | end |
| 1663 | if lt fseek int fd 0 0 0 |
| 1664 | z = fclose int fd |
| 1665 | ret WX_ERR_IO |
| 1666 | end |
| 1667 | if ne read int fd wx_rb_buf 0 elen long elen |
| 1668 | z = fclose int fd |
| 1669 | ret WX_ERR_IO |
| 1670 | end |
| 1671 | for i ulong elen bound 4096 |
| 1672 | put sh_dat i get wx_rb_buf i |
| 1673 | end |
| 1674 | sh_all elen |
| 1675 | for j ulong 32 |
| 1676 | if ne get sh_dig j get wx_sha_exp j |
| 1677 | put wx_sha_refusals 0 add get wx_sha_refusals 0 1 |
| 1678 | z = fclose int fd |
| 1679 | z = unlink wx_p_stage 0 get wx_p_stagel 0 |
| 1680 | ret WX_ERR_SHA_MISMATCH |
| 1681 | end |
| 1682 | end |
| 1683 | cm = fchmod int fd 320 |
| 1684 | z = fsync int fd |
| 1685 | z = fclose int fd |
| 1686 | if ne cm 0 |
| 1687 | z = unlink wx_p_stage 0 get wx_p_stagel 0 |
| 1688 | ret WX_ERR_IO |
| 1689 | end |
| 1690 | ret WX_OK |
| 1691 | end |
| 1692 | |
| 1693 | fn wx_seal_rename to ulong |
| 1694 | sl = get wx_p_stagel 0 |
| 1695 | rl = get wx_p_rxl 0 |
| 1696 | z = unlink wx_p_rx 0 rl |
| 1697 | put wx_ren_pkt 0 2 |
| 1698 | put wx_ren_pkt 1 0 |
| 1699 | put wx_ren_pkt 2 uchar and sl 255 |
| 1700 | put wx_ren_pkt 3 uchar shr sl 8 |
| 1701 | for i ulong sl bound 1024 |
| 1702 | put wx_ren_pkt add 4 i get wx_p_stage i |
| 1703 | end |
| 1704 | off = add 4 sl |
| 1705 | put wx_ren_pkt off uchar and rl 255 |
| 1706 | put wx_ren_pkt add off 1 uchar shr rl 8 |
| 1707 | off = add off 2 |
| 1708 | for i ulong rl bound 1024 |
| 1709 | put wx_ren_pkt add off i get wx_p_rx i |
| 1710 | end |
| 1711 | if ne renames wx_ren_pkt 0 add off rl 0 0 |
| 1712 | z = unlink wx_p_stage 0 sl |
| 1713 | ret WX_ERR_IO |
| 1714 | end |
| 1715 | rc = wx_check_stat_rx_buf rl |
| 1716 | if ne rc WX_OK |
| 1717 | z = unlink wx_p_rx 0 rl |
| 1718 | ret rc |
| 1719 | end |
| 1720 | ret WX_OK |
| 1721 | end |
| 1722 | |
| 1723 | note 3-Phase Strict W^X Staging & Seal: |
| 1724 | note 1. Validate req_prot (refuse RWX) + ELF PT_LOAD PF_R|PF_X |
| 1725 | note 2. Write RW-only staging file (mode 0600), read back, verify FIPS 180-4 SHA-256 |
| 1726 | note 3. fchmod(0500 = r-x, zero write bits!), fsync, atomic renames -> .rx, stat-verify W^X! |
| 1727 | fn wx_stage_and_seal slot ulong ver ulong req_prot ulong to ulong |
| 1728 | rc = wx_validate_prot req_prot |
| 1729 | if ne rc WX_OK |
| 1730 | ret rc |
| 1731 | end |
| 1732 | if ne req_prot WX_PROT_RX |
| 1733 | ret WX_ERR_NOT_RX |
| 1734 | end |
| 1735 | elen = get elfLen 0 |
| 1736 | rc = wx_verify_elf_wx elen |
| 1737 | if ne rc WX_OK |
| 1738 | ret rc |
| 1739 | end |
| 1740 | z = makedir wx_dir 0 get wx_dir_len 0 448 |
| 1741 | wx_build_paths slot ver |
| 1742 | rc = wx_write_and_verify_stage elen |
| 1743 | if ne rc WX_OK |
| 1744 | ret rc |
| 1745 | end |
| 1746 | rc = wx_seal_rename |
| 1747 | if ne rc WX_OK |
| 1748 | ret rc |
| 1749 | end |
| 1750 | put wx_seals_ok 0 add get wx_seals_ok 0 1 |
| 1751 | ret WX_OK |
| 1752 | end |
| 1753 | |
| 1754 | fn wx_put_u64_in off ulong v ulong to void |
| 1755 | for j ulong 8 |
| 1756 | put wx_io_in add off j uchar and shr v mul j 8 255 |
| 1757 | end |
| 1758 | end |
| 1759 | |
| 1760 | fn wx_get_u64_out to ulong |
| 1761 | v = ulong 0 |
| 1762 | for j ulong 8 |
| 1763 | v = or v shl ulong get wx_io_out j mul j 8 |
| 1764 | end |
| 1765 | ret v |
| 1766 | end |
| 1767 | |
| 1768 | fn wx_rd_fd32 which ulong off ulong to int |
| 1769 | b0 = ulong 0 |
| 1770 | b1 = ulong 0 |
| 1771 | b2 = ulong 0 |
| 1772 | b3 = ulong 0 |
| 1773 | if eq which 0 |
| 1774 | b0 = ulong get wx_p_in off |
| 1775 | b1 = shl ulong get wx_p_in add off 1 8 |
| 1776 | b2 = shl ulong get wx_p_in add off 2 16 |
| 1777 | b3 = shl ulong get wx_p_in add off 3 24 |
| 1778 | else |
| 1779 | b0 = ulong get wx_p_out off |
| 1780 | b1 = shl ulong get wx_p_out add off 1 8 |
| 1781 | b2 = shl ulong get wx_p_out add off 2 16 |
| 1782 | b3 = shl ulong get wx_p_out add off 3 24 |
| 1783 | end |
| 1784 | ret int or or b0 b1 or b2 b3 |
| 1785 | end |
| 1786 | |
| 1787 | fn wx_set_args a0 ulong a1 ulong a2 ulong a3 ulong reps ulong to void |
| 1788 | put wx_args 0 a0 |
| 1789 | put wx_args 1 a1 |
| 1790 | put wx_args 2 a2 |
| 1791 | put wx_args 3 a3 |
| 1792 | put wx_args 4 reps |
| 1793 | end |
| 1794 | |
| 1795 | fn wx_pack_io_in to void |
| 1796 | wx_put_u64_in 0 get wx_args 0 |
| 1797 | wx_put_u64_in 8 get wx_args 1 |
| 1798 | wx_put_u64_in 16 get wx_args 2 |
| 1799 | wx_put_u64_in 24 get wx_args 3 |
| 1800 | wx_put_u64_in 32 get wx_args 4 |
| 1801 | end |
| 1802 | |
| 1803 | fn wx_exec_collect pid long out_rd int t0 long to ulong |
| 1804 | for j ulong 8 |
| 1805 | put wx_io_out j 0 |
| 1806 | end |
| 1807 | tot = ulong 0 |
| 1808 | while lt tot 8 bound 16 |
| 1809 | rn = read out_rd wx_io_out tot sub 8 tot |
| 1810 | if le rn 0 |
| 1811 | break |
| 1812 | end |
| 1813 | tot = add tot ulong rn |
| 1814 | end |
| 1815 | z = fclose out_rd |
| 1816 | z = waitpid int pid wx_wait_st 0 4 0 |
| 1817 | t1 = monons |
| 1818 | if ge t1 t0 |
| 1819 | put wx_last_ns 0 ulong sub t1 t0 |
| 1820 | end |
| 1821 | if ne get wx_wait_st 0 0 |
| 1822 | put wx_last_exit 0 add 128 ulong get wx_wait_st 0 |
| 1823 | ret 0 |
| 1824 | end |
| 1825 | put wx_last_exit 0 ulong get wx_wait_st 1 |
| 1826 | if eq tot 8 |
| 1827 | res = wx_get_u64_out |
| 1828 | put wx_last_out 0 res |
| 1829 | ret res |
| 1830 | end |
| 1831 | ret 0 |
| 1832 | end |
| 1833 | |
| 1834 | note Execute sealed RX AArch64 ELF binary at global wx_p_rx[0..plen) with staged wx_args[0..4]. |
| 1835 | note Enforces stat W^X re-check before exec; measures native duration via monons. |
| 1836 | fn wx_exec_sealed plen ulong to ulong |
| 1837 | put wx_last_exit 0 255 |
| 1838 | put wx_last_ns 0 0 |
| 1839 | put wx_last_out 0 0 |
| 1840 | if ne wx_check_stat_rx_buf plen WX_OK |
| 1841 | ret 0 |
| 1842 | end |
| 1843 | if ne pipefds wx_p_in 0 8 0 0 |
| 1844 | ret 0 |
| 1845 | end |
| 1846 | if ne pipefds wx_p_out 0 8 0 0 |
| 1847 | z = fclose wx_rd_fd32 0 0 |
| 1848 | z = fclose wx_rd_fd32 0 4 |
| 1849 | ret 0 |
| 1850 | end |
| 1851 | in_rd = wx_rd_fd32 0 0 |
| 1852 | in_wr = wx_rd_fd32 0 4 |
| 1853 | out_rd = wx_rd_fd32 1 0 |
| 1854 | out_wr = wx_rd_fd32 1 4 |
| 1855 | wx_pack_io_in |
| 1856 | z = write in_wr wx_io_in 0 40 |
| 1857 | z = fclose in_wr |
| 1858 | put wx_av_pkt 0 1 |
| 1859 | put wx_av_pkt 1 0 |
| 1860 | put wx_av_pkt 2 uchar and plen 255 |
| 1861 | put wx_av_pkt 3 uchar shr plen 8 |
| 1862 | for i ulong plen bound 1024 |
| 1863 | put wx_av_pkt add 4 i get wx_p_rx i |
| 1864 | end |
| 1865 | t0 = monons |
| 1866 | pid = fork |
| 1867 | if lt pid 0 |
| 1868 | z = fclose in_rd |
| 1869 | z = fclose out_rd |
| 1870 | z = fclose out_wr |
| 1871 | ret 0 |
| 1872 | end |
| 1873 | if eq pid 0 |
| 1874 | z = dupfd in_rd 0 0 |
| 1875 | z = dupfd out_wr 1 0 |
| 1876 | z = fclose in_rd |
| 1877 | z = fclose out_rd |
| 1878 | z = fclose out_wr |
| 1879 | z = execprog wx_av_pkt 0 add 4 plen |
| 1880 | exit 127 |
| 1881 | end |
| 1882 | z = fclose in_rd |
| 1883 | z = fclose out_wr |
| 1884 | ret wx_exec_collect pid out_rd t0 |
| 1885 | end |
| 1886 | |
| 1887 | note ==== Part 7: 3-Bit Proof Gate + Monk/Immune + Cooperative fabWtbl Hot-Swap + OOD Rollback ==== |
| 1888 | |
| 1889 | fn mb_init to void |
| 1890 | tdInitAll |
| 1891 | isInitRules |
| 1892 | tdCacheInit |
| 1893 | fabInit |
| 1894 | crInit |
| 1895 | as_init |
| 1896 | pr_init |
| 1897 | wx_init |
| 1898 | put mb_worker_cpu 0 0 |
| 1899 | put mb_swaps 0 0 |
| 1900 | put mb_refusals 0 0 |
| 1901 | put mb_rollbacks 0 0 |
| 1902 | put mb_spec_hits 0 0 |
| 1903 | put mb_base_hits 0 0 |
| 1904 | put mb_alarms 0 0 |
| 1905 | put mb_last_disp 0 MB_DISP_BASELINE |
| 1906 | put mb_last_err 0 MB_OK |
| 1907 | put mb_alarm_cnt 0 0 |
| 1908 | put mb_mut_flag 0 0 |
| 1909 | for s ulong MB_MAX_SLOTS |
| 1910 | put mb_slot_valid s 0 |
| 1911 | put mb_slot_sem s 0 |
| 1912 | put mb_slot_fbase s 0 |
| 1913 | put mb_slot_fspec s 0 |
| 1914 | put mb_slot_base_insns s 0 |
| 1915 | put mb_slot_spec_insns s 0 |
| 1916 | put mb_slot_base_ops s 0 |
| 1917 | put mb_slot_spec_ops s 0 |
| 1918 | put mb_slot_base_ptr s 0 |
| 1919 | put mb_slot_spec_ptr s 0 |
| 1920 | put mb_slot_base_tid s 0 |
| 1921 | put mb_slot_spec_tid s 0 |
| 1922 | put mb_slot_base_slab s 0 |
| 1923 | put mb_slot_alarm_slab s 0 |
| 1924 | put mb_slot_base_plen s 0 |
| 1925 | put mb_slot_spec_plen s 0 |
| 1926 | put mb_slot_pin_mask s 0 |
| 1927 | put mb_slot_pin_v0 s 0 |
| 1928 | put mb_slot_pin_v1 s 0 |
| 1929 | put mb_slot_pin_v2 s 0 |
| 1930 | put mb_slot_pin_v3 s 0 |
| 1931 | put mb_slot_wk s 0 |
| 1932 | end |
| 1933 | end |
| 1934 | |
| 1935 | fn mb_log_alarm slot ulong code ulong x0 ulong x1 ulong to void |
| 1936 | put mb_alarms 0 add get mb_alarms 0 1 |
| 1937 | idx = get mb_alarm_cnt 0 |
| 1938 | if lt idx 32 |
| 1939 | put mb_alarm_slot idx slot |
| 1940 | put mb_alarm_code idx code |
| 1941 | put mb_alarm_x0 idx x0 |
| 1942 | put mb_alarm_x1 idx x1 |
| 1943 | put mb_alarm_cnt 0 add idx 1 |
| 1944 | end |
| 1945 | end |
| 1946 | |
| 1947 | fn mb_save_path is_spec bool slot ulong to void |
| 1948 | base = mul slot 512 |
| 1949 | rl = get wx_p_rxl 0 |
| 1950 | if gt rl 511 |
| 1951 | rl = ulong 511 |
| 1952 | end |
| 1953 | for i ulong rl bound 512 |
| 1954 | if is_spec |
| 1955 | put mb_slot_spec_path add base i get wx_p_rx i |
| 1956 | else |
| 1957 | put mb_slot_base_path add base i get wx_p_rx i |
| 1958 | end |
| 1959 | end |
| 1960 | if is_spec |
| 1961 | put mb_slot_spec_plen slot rl |
| 1962 | else |
| 1963 | put mb_slot_base_plen slot rl |
| 1964 | end |
| 1965 | end |
| 1966 | |
| 1967 | fn mb_exec_slot_bin is_spec bool slot ulong to ulong |
| 1968 | base = mul slot 512 |
| 1969 | pl = get mb_slot_base_plen slot |
| 1970 | if is_spec |
| 1971 | pl = get mb_slot_spec_plen slot |
| 1972 | end |
| 1973 | for i ulong pl bound 512 |
| 1974 | if is_spec |
| 1975 | put wx_p_rx i get mb_slot_spec_path add base i |
| 1976 | else |
| 1977 | put wx_p_rx i get mb_slot_base_path add base i |
| 1978 | end |
| 1979 | end |
| 1980 | put wx_p_rxl 0 pl |
| 1981 | ret wx_exec_sealed pl |
| 1982 | end |
| 1983 | |
| 1984 | note Register an immutable verified baseline ΩIR function into fabWtbl[slot] + SLAB_IMMUTABLE + SLAB_MUTABLE_ALARMED! |
| 1985 | fn mb_register_slot slot ulong sem_id ulong f_base uint wk ulong to ulong |
| 1986 | if ge slot MB_MAX_SLOTS |
| 1987 | ret MB_ERR_SLOT |
| 1988 | end |
| 1989 | elen = gpe_compile_a64_elf f_base |
| 1990 | if eq elen 0 |
| 1991 | ret MB_ERR_PE |
| 1992 | end |
| 1993 | insns = get gpe_a6_insns 0 |
| 1994 | rc = wx_stage_and_seal slot 1 WX_PROT_RX |
| 1995 | if ne rc WX_OK |
| 1996 | ret MB_ERR_WX_RWX |
| 1997 | end |
| 1998 | mb_save_path false slot |
| 1999 | b_ptr = add 0x100000 mul slot 0x10000 |
| 2000 | if not fabHotSwap slot sem_id b_ptr TID_X925 FAB_MODE_CPU_NATIVE MB_PROOF_ALL |
| 2001 | ret MB_ERR_SWAP |
| 2002 | end |
| 2003 | cpufence |
| 2004 | s_imm = mul slot 2 |
| 2005 | s_alm = add s_imm 1 |
| 2006 | crRegSlab s_imm SLAB_IMMUTABLE 0 TID_X925 |
| 2007 | crRegSlab s_alm SLAB_MUTABLE_ALARMED 100 TID_X925 |
| 2008 | put mb_slot_valid slot 1 |
| 2009 | put mb_slot_sem slot sem_id |
| 2010 | put mb_slot_fbase slot f_base |
| 2011 | put mb_slot_fspec slot 0 |
| 2012 | put mb_slot_base_insns slot insns |
| 2013 | put mb_slot_spec_insns slot 0 |
| 2014 | put mb_slot_base_ptr slot b_ptr |
| 2015 | put mb_slot_spec_ptr slot 0 |
| 2016 | put mb_slot_base_tid slot TID_X925 |
| 2017 | put mb_slot_spec_tid slot TID_X925 |
| 2018 | put mb_slot_base_slab slot s_imm |
| 2019 | put mb_slot_alarm_slab slot s_alm |
| 2020 | put mb_slot_wk slot wk |
| 2021 | ret MB_OK |
| 2022 | end |
| 2023 | |
| 2024 | fn mb_check_a64_words to bool |
| 2025 | if ne wx_verify_elf_wx get elfLen 0 WX_OK |
| 2026 | ret false |
| 2027 | end |
| 2028 | clen = get isLen 0 |
| 2029 | if or lt clen 16 ne rem clen 4 0 |
| 2030 | ret false |
| 2031 | end |
| 2032 | has_svc = false |
| 2033 | nins = div clen 4 |
| 2034 | for i ulong nins bound 256 |
| 2035 | off = mul i 4 |
| 2036 | w = ulong get isBuf off |
| 2037 | w = or w shl ulong get isBuf add off 1 8 |
| 2038 | w = or w shl ulong get isBuf add off 2 16 |
| 2039 | w = or w shl ulong get isBuf add off 3 24 |
| 2040 | if eq w 0 |
| 2041 | ret false |
| 2042 | end |
| 2043 | if eq w 0xd4000001 |
| 2044 | has_svc = true |
| 2045 | end |
| 2046 | end |
| 2047 | ret has_svc |
| 2048 | end |
| 2049 | |
| 2050 | fn mb_verify_monk_immune_cpu f_base uint f_opt uint slot ulong to bool |
| 2051 | if not mb_check_a64_words |
| 2052 | ret false |
| 2053 | end |
| 2054 | pm = get gpe_pin_mask 0 |
| 2055 | for t ulong 3 |
| 2056 | x0 = add 5 mul t 17 |
| 2057 | x1 = add 3 mul t 11 |
| 2058 | x2 = add 7 mul t 13 |
| 2059 | x3 = add 2 mul t 5 |
| 2060 | if ne and pm 1 0 |
| 2061 | x0 = get gpe_pin_val 0 |
| 2062 | end |
| 2063 | if ne and pm 2 0 |
| 2064 | x1 = get gpe_pin_val 1 |
| 2065 | end |
| 2066 | if ne and pm 4 0 |
| 2067 | x2 = get gpe_pin_val 2 |
| 2068 | end |
| 2069 | if ne and pm 8 0 |
| 2070 | x3 = get gpe_pin_val 3 |
| 2071 | end |
| 2072 | yb = gpe_eval_fn f_base x0 x1 x2 x3 |
| 2073 | ys = gpe_eval_fn f_opt x0 x1 x2 x3 |
| 2074 | if or ne get gpe_last_trap 0 0 ne yb ys |
| 2075 | ret false |
| 2076 | end |
| 2077 | end |
| 2078 | ret true |
| 2079 | end |
| 2080 | |
| 2081 | fn mb_verify_monk_immune_sm121 to bool |
| 2082 | ret as_verify_antibodies |
| 2083 | end |
| 2084 | |
| 2085 | fn mb_refuse code ulong to ulong |
| 2086 | put mb_refusals 0 add get mb_refusals 0 1 |
| 2087 | put mb_last_err 0 code |
| 2088 | ret code |
| 2089 | end |
| 2090 | |
| 2091 | fn mb_spec_check_contract f_base uint target_tid ulong to bool |
| 2092 | tdSetReq EXEC_SCALAR_SIMD 0 12 4 0 FEAT_FMA |
| 2093 | if ne tdCheckKernelContract target_tid TDK_OK |
| 2094 | ret false |
| 2095 | end |
| 2096 | pur = ulong get i_fpur f_base |
| 2097 | prc = ulong get i_fprc f_base |
| 2098 | hd = ulong get i_fhd f_base |
| 2099 | eb = get i_ferr f_base |
| 2100 | dl = get i_fdl f_base |
| 2101 | ret ir_fc_ok pur prc hd 0 eb dl |
| 2102 | end |
| 2103 | |
| 2104 | fn mb_spec_commit slot ulong target_tid ulong f_opt uint insns ulong eff ulong to ulong |
| 2105 | nxt_ver = add get fabVer slot 1 |
| 2106 | s_ptr = add 0x200000 add mul slot 0x10000 mul nxt_ver 4096 |
| 2107 | if not fabHotSwap slot get mb_slot_sem slot s_ptr target_tid FAB_MODE_CRYSTALLIZED eff |
| 2108 | ret mb_refuse MB_ERR_SWAP |
| 2109 | end |
| 2110 | cpufence |
| 2111 | put mb_slot_fspec slot f_opt |
| 2112 | put mb_slot_spec_insns slot insns |
| 2113 | put mb_slot_spec_ptr slot s_ptr |
| 2114 | put mb_slot_spec_tid slot target_tid |
| 2115 | put mb_slot_pin_mask slot get gpe_pin_mask 0 |
| 2116 | put mb_slot_pin_v0 slot get gpe_pin_val 0 |
| 2117 | put mb_slot_pin_v1 slot get gpe_pin_val 1 |
| 2118 | put mb_slot_pin_v2 slot get gpe_pin_val 2 |
| 2119 | put mb_slot_pin_v3 slot get gpe_pin_val 3 |
| 2120 | put mb_swaps 0 add get mb_swaps 0 1 |
| 2121 | ret MB_OK |
| 2122 | end |
| 2123 | |
| 2124 | note Efficiency-core (TID_A725) background specialization with strict 3-bit proof gate & W^X seal! |
| 2125 | fn mb_specialize_slot slot ulong target_tid ulong req_prot ulong caller_proof_mask ulong to ulong |
| 2126 | put mb_last_err 0 MB_OK |
| 2127 | if or ge slot MB_MAX_SLOTS eq get mb_slot_valid slot 0 |
| 2128 | ret mb_refuse MB_ERR_SLOT |
| 2129 | end |
| 2130 | if ne fabScheduleApu TASK_BG_METABOLIC TID_A725 |
| 2131 | ret mb_refuse MB_ERR_NOT_A725 |
| 2132 | end |
| 2133 | z = cpuget 0 mb_cpu_mask 0 128 |
| 2134 | z = cpunow |
| 2135 | if ge z 0 |
| 2136 | put mb_worker_cpu 0 ulong z |
| 2137 | end |
| 2138 | if ne wx_validate_prot req_prot WX_OK |
| 2139 | mb_log_alarm slot MB_ALARM_RWX req_prot 0 |
| 2140 | ret mb_refuse MB_ERR_WX_RWX |
| 2141 | end |
| 2142 | f_base = get mb_slot_fbase slot |
| 2143 | computed = ulong 0 |
| 2144 | if mb_spec_check_contract f_base target_tid |
| 2145 | computed = or computed MB_BIT_CONTRACT |
| 2146 | end |
| 2147 | if eq and computed MB_BIT_CONTRACT 0 |
| 2148 | ret mb_refuse MB_ERR_CONTRACT |
| 2149 | end |
| 2150 | f_spec = gpe_specialize_fn f_base |
| 2151 | if eq f_spec 0 |
| 2152 | ret mb_refuse MB_ERR_PE |
| 2153 | end |
| 2154 | f_opt = gpe_prove_and_optimize f_spec target_tid |
| 2155 | if ne f_opt 0 |
| 2156 | if and eq get gpe_pe_stat 7 0 ge get gpe_pe_stat 8 1 |
| 2157 | if ge get gpe_pe_stat 10 4 |
| 2158 | computed = or computed MB_BIT_EGP3 |
| 2159 | end |
| 2160 | end |
| 2161 | end |
| 2162 | if eq and computed MB_BIT_EGP3 0 |
| 2163 | ret mb_refuse MB_ERR_EGP3 |
| 2164 | end |
| 2165 | if eq gpe_compile_a64_elf f_opt 0 |
| 2166 | ret mb_refuse MB_ERR_PE |
| 2167 | end |
| 2168 | if mb_verify_monk_immune_cpu f_base f_opt slot |
| 2169 | computed = or computed MB_BIT_MONK_IMMUNE |
| 2170 | end |
| 2171 | if eq and computed MB_BIT_MONK_IMMUNE 0 |
| 2172 | ret mb_refuse MB_ERR_MONK_IMMUNE |
| 2173 | end |
| 2174 | eff = and computed caller_proof_mask |
| 2175 | if ne eff MB_PROOF_ALL |
| 2176 | ret mb_refuse MB_ERR_PROOF_MASK |
| 2177 | end |
| 2178 | if ne wx_stage_and_seal slot add get fabVer slot 1 req_prot WX_OK |
| 2179 | ret mb_refuse MB_ERR_WX_RWX |
| 2180 | end |
| 2181 | mb_save_path true slot |
| 2182 | ret mb_spec_commit slot target_tid f_opt get gpe_a6_insns 0 eff |
| 2183 | end |
| 2184 | |
| 2185 | note SM121 GPU specialization check via TargetDesc contract + Antibody #13-#15 (for negative control & GPU lane) |
| 2186 | fn mb_check_gpu_candidate target_tid ulong M ulong K ulong N ulong proof_mask ulong to ulong |
| 2187 | pePlanRegime M K N |
| 2188 | crc = tdCheckKernelContract target_tid |
| 2189 | if ne crc TDK_OK |
| 2190 | put mb_refusals 0 add get mb_refusals 0 1 |
| 2191 | put mb_last_err 0 MB_ERR_CONTRACT |
| 2192 | ret MB_ERR_CONTRACT |
| 2193 | end |
| 2194 | if not mb_verify_monk_immune_sm121 |
| 2195 | put mb_refusals 0 add get mb_refusals 0 1 |
| 2196 | put mb_last_err 0 MB_ERR_MONK_IMMUNE |
| 2197 | ret MB_ERR_MONK_IMMUNE |
| 2198 | end |
| 2199 | if ne and proof_mask MB_PROOF_ALL MB_PROOF_ALL |
| 2200 | put mb_refusals 0 add get mb_refusals 0 1 |
| 2201 | put mb_last_err 0 MB_ERR_PROOF_MASK |
| 2202 | ret MB_ERR_PROOF_MASK |
| 2203 | end |
| 2204 | ret MB_OK |
| 2205 | end |
| 2206 | |
| 2207 | note Atomic rollback of fabWtbl[slot] back to immutable baseline slot, tripping SLAB_MUTABLE_ALARMED! |
| 2208 | fn mb_rollback_slot slot ulong reason ulong x0 ulong x1 ulong to bool |
| 2209 | if or ge slot MB_MAX_SLOTS eq get mb_slot_valid slot 0 |
| 2210 | ret false |
| 2211 | end |
| 2212 | note Trip SLAB_MUTABLE_ALARMED norm alarm (delta 200 > limit 100 -> CR_ERR_ALARM, slab norm untouched!) |
| 2213 | a_rc = crUpdateSlab get mb_slot_alarm_slab slot 200 |
| 2214 | mb_log_alarm slot reason x0 x1 |
| 2215 | ok = fabHotSwap slot get mb_slot_sem slot get mb_slot_base_ptr slot get mb_slot_base_tid slot FAB_MODE_CPU_NATIVE MB_PROOF_ALL |
| 2216 | cpufence |
| 2217 | if ok |
| 2218 | put mb_rollbacks 0 add get mb_rollbacks 0 1 |
| 2219 | end |
| 2220 | ret ok |
| 2221 | end |
| 2222 | |
| 2223 | note Cooperative guarded dispatch on fabWtbl[slot] using staged wx_args[0..4]: |
| 2224 | note - If specialized (FAB_MODE_CRYSTALLIZED): runs sealed RX specialized AArch64 ELF! |
| 2225 | note - If specialized binary trips OOD trap 14 (exit 114) or spot-check mismatches baseline oracle: |
| 2226 | note instantly trips SLAB_MUTABLE_ALARMED, atomically rolls back fabWtbl[slot] to immutable baseline, |
| 2227 | note and executes the immutable baseline RX ELF returning the exact baseline result! |
| 2228 | fn mb_dispatch_slot slot ulong spot_check bool to ulong |
| 2229 | if or ge slot MB_MAX_SLOTS eq get mb_slot_valid slot 0 |
| 2230 | put mb_last_disp 0 MB_DISP_BASELINE |
| 2231 | ret 0 |
| 2232 | end |
| 2233 | a0 = get wx_args 0 |
| 2234 | a1 = get wx_args 1 |
| 2235 | a2 = get wx_args 2 |
| 2236 | a3 = get wx_args 3 |
| 2237 | mode = get fabMode slot |
| 2238 | if eq mode FAB_MODE_CRYSTALLIZED |
| 2239 | y_spec = mb_exec_slot_bin true slot |
| 2240 | ex = get wx_last_exit 0 |
| 2241 | if ne ex 0 |
| 2242 | rb = mb_rollback_slot slot MB_ALARM_OOD a0 a1 |
| 2243 | put mb_last_disp 0 MB_DISP_ROLLBACK |
| 2244 | ret mb_exec_slot_bin false slot |
| 2245 | end |
| 2246 | if spot_check |
| 2247 | y_ref = gpe_eval_fn get mb_slot_fbase slot a0 a1 a2 a3 |
| 2248 | if ne y_spec y_ref |
| 2249 | rb = mb_rollback_slot slot MB_ALARM_ORACLE a0 a1 |
| 2250 | put mb_last_disp 0 MB_DISP_ROLLBACK |
| 2251 | ret mb_exec_slot_bin false slot |
| 2252 | end |
| 2253 | end |
| 2254 | put mb_spec_hits 0 add get mb_spec_hits 0 1 |
| 2255 | put mb_last_disp 0 MB_DISP_SPEC |
| 2256 | ret y_spec |
| 2257 | end |
| 2258 | put mb_base_hits 0 add get mb_base_hits 0 1 |
| 2259 | put mb_last_disp 0 MB_DISP_BASELINE |
| 2260 | ret mb_exec_slot_bin false slot |
| 2261 | end |