interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / apps / omega-metabd / general_pe.om

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