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 / seed / ng / physics / phys_solver.om

660 lines · 17591 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/physics/phys_solver.om
3note Sovereign Intelligence 2.0 Stage 3 (Pillar 3: Physics-Typed Computing)
4note Real-Time Physics Dispatch Solver + ΩIR v2 Contract Reader + Error-Composition Tracker
5note + Direct fabHotSwap (seed/ng/target/fabric.om) Wiring.
6note
7note Exact 1:1 fixed-point mirror of seed/helix/proofs/PhysicsDispatch.lean.
8note Queries apps/omega-wattd/watt_cbf.om UNCHANGED (strictly DRY_RUN_READONLY, hw_writes=0).
9note
10note LOCKED strict priority hierarchy:
11note 1. Hardware WattCBF thermal/power safety ceiling is INVIOLABLE
12note (P_gpu <= 80,000 mW, P_total <= min(P_budget, 140,000 mW), CBF h >= 0, fail_closed == 0,
13note plus @power contract cap when > 0).
14note 2. Declared @err bound is NEVER silently broken (proven_err_bound <= @err).
15note 3. Minimize latency / honor @deadline inside (1) & (2):
16note - If no safe realization meets @deadline:
17note * @hard_deadline == 1 -> fail closed with PHYS_ERR_HARD_DEADLINE (never dispatches!)
18note * @hard_deadline == 0 -> select fastest safe realization inside (1) & (2) AND emit
19note explicit structured ALARM_DEADLINE_SLIP (never silent!).
20
21note Dispatch & refusal status codes
22const PHYS_OK ulong 0
23const PHYS_OK_DEADLINE_SLIP ulong 1
24const PHYS_ERR_BAD_SLOT ulong 10
25const PHYS_ERR_BAD_CONTRACT ulong 11
26const PHYS_ERR_BAD_BASELINE ulong 12
27const PHYS_ERR_WATTCBF_FAIL_CLOSED ulong 13
28const PHYS_ERR_NO_REALIZATION ulong 14
29const PHYS_ERR_WATTCBF_UNSAFE ulong 15
30const PHYS_ERR_ERR_BOUND ulong 16
31const PHYS_ERR_HARD_DEADLINE ulong 17
32const PHYS_ERR_FAB_SWAP ulong 18
33const PHYS_ERR_HW_WRITE_GUARD ulong 19
34const PHYS_ERR_CHAIN_ERR_OVERFLOW ulong 20
35const PHYS_ERR_CHAIN_BAD_STAGE ulong 21
36
37note Structured alarm codes (0 = none)
38const PHYS_ALARM_NONE ulong 0
39const PHYS_ALARM_DEADLINE_SLIP ulong 1
40
41note Actuator mode (strictly READ-ONLY / DRY-RUN)
42const PHYS_ACTUATOR_DRY_RUN_READONLY ulong 0
43
44note ΩIR v2 precision tiers (exact mirror of seed/ng/IR.md §16.1)
45const PHYS_TIER_UNSPEC ulong 0
46const PHYS_TIER_EXACT ulong 1
47const PHYS_TIER_TOL ulong 2
48
49note Contract staging array ps_ctr[0..6]:
50note [0]=purity_flag [1]=precision_tier [2]=hard_deadline [3]=err_bound
51note [4]=deadline_us [5]=power_mw [6]=reserved_byte
52global ps_ctr long 8
53
54note Baseline non-kernel rail loads ps_base[0..2]:
55note [0]=base_cpu_mw [1]=base_unc_mw [2]=fan_req_pct
56global ps_base long 4
57
58note Dispatch telemetry array ps_tel[0..19]:
59const PS_TEL_STATUS ulong 0
60const PS_TEL_BANK_IDX ulong 1
61const PS_TEL_TID ulong 2
62const PS_TEL_PREC ulong 3
63const PS_TEL_POWER_MW ulong 4
64const PS_TEL_LAT_US ulong 5
65const PS_TEL_JOULES_UJ ulong 6
66const PS_TEL_ERR_BOUND ulong 7
67const PS_TEL_DL_MARGIN_US ulong 8
68const PS_TEL_ALARM_CODE ulong 9
69const PS_TEL_ALARM_COUNT ulong 10
70const PS_TEL_TURING_YIELD ulong 11
71const PS_TEL_FAN_PCT ulong 12
72const PS_TEL_BARRIER_H ulong 13
73const PS_TEL_P_SAFE_GPU ulong 14
74const PS_TEL_P_SAFE_CPU ulong 15
75const PS_TEL_HW_WRITES ulong 16
76const PS_TEL_ACTUATOR_MODE ulong 17
77const PS_TEL_REJ_WATTCBF ulong 18
78const PS_TEL_REJ_ERR ulong 19
79global ps_tel long 20
80
81note Cumulative structured alarm log (up to 64 deadline-slip events; never silent)
82const PS_ALARM_CAP ulong 64
83global ps_alm_count ulong 1
84global ps_alm_code ulong 64
85global ps_alm_sem ulong 64
86global ps_alm_dl long 64
87global ps_alm_lat long 64
88global ps_alm_slip long 64
89global ps_alm_prec ulong 64
90global ps_alm_tid ulong 64
91
92note Cumulative dispatch statistics: [0]=dispatches [1]=refusals [2]=swaps [3]=hard_dl_refusals
93global ps_stats ulong 8
94
95note Sequential call-chain error-composition tracker (up to 64 stages)
96const PS_CHAIN_CAP ulong 64
97global ps_ch_active ulong 1
98global ps_ch_refused ulong 1
99global ps_ch_outer_err long 1
100global ps_ch_outer_tier long 1
101global ps_ch_total_err long 1
102global ps_ch_len ulong 1
103global ps_ch_sem ulong 64
104global ps_ch_err long 64
105
106fn ps_init to void
107for i ulong 8
108put ps_ctr i 0
109put ps_stats i 0
110end
111put ps_base 0 10000
112put ps_base 1 5000
113put ps_base 2 40
114for j ulong 20
115put ps_tel j 0
116end
117put ps_tel PS_TEL_BANK_IDX long PB_NONE
118put ps_tel PS_TEL_HW_WRITES 0
119put ps_tel PS_TEL_ACTUATOR_MODE long PHYS_ACTUATOR_DRY_RUN_READONLY
120put ps_alm_count 0 0
121put ps_ch_active 0 0
122put ps_ch_refused 0 0
123put ps_ch_outer_err 0 0
124put ps_ch_outer_tier 0 0
125put ps_ch_total_err 0 0
126put ps_ch_len 0 0
127note Ensure watt_sense hw_writes counter starts at 0 (strictly read-only)
128put g_wmeta 12 0
129end
130
131fn ps_set_baseline base_cpu_mw long base_unc_mw long fan_req long to void
132put ps_base 0 base_cpu_mw
133put ps_base 1 base_unc_mw
134put ps_base 2 fan_req
135end
136
137fn ps_set_contract purity long tier long hard_dl long err_b long dl_us long pwr_mw long to void
138put ps_ctr 0 purity
139put ps_ctr 1 tier
140put ps_ctr 2 hard_dl
141put ps_ctr 3 err_b
142put ps_ctr 4 dl_us
143put ps_ctr 5 pwr_mw
144put ps_ctr 6 0
145end
146
147note Decode little-endian u64 from byte slice at offset off (checked bounds)
148fn ps_le_u64 buf view uchar * off ulong to ulong
149if gt add off 8 len buf
150ret 0
151end
152v = ulong 0
153for k ulong 8
154b = ulong get buf add off k
155v = or v shl b mul k 8
156end
157ret v
158end
159
160note Canonical ΩIR v2 contract validation (exact mirror of seed/ng/IR.md §16.1 & PhysicsDispatch.lean)
161fn ps_contract_valid to bool
162if ne get ps_ctr 6 0
163ret false
164end
165pur = get ps_ctr 0
166if or lt pur 0 gt pur 1
167ret false
168end
169tier = get ps_ctr 1
170if or lt tier 0 gt tier 2
171ret false
172end
173hdl = get ps_ctr 2
174if or lt hdl 0 gt hdl 1
175ret false
176end
177eb = get ps_ctr 3
178if lt eb 0
179ret false
180end
181dl = get ps_ctr 4
182if lt dl 0
183ret false
184end
185if lt get ps_ctr 5 0
186ret false
187end
188if ne tier long PHYS_TIER_TOL
189if ne eb 0
190ret false
191end
192end
193if eq hdl 1
194if le dl 0
195ret false
196end
197end
198ret true
199end
200
201note Parse a live 28-byte ΩIR v2 wire contract record (IR.md §16.1) into ps_ctr and validate
202note Wire layout: [0]=purity u8, [1]=precision_tier u8, [2]=hard_deadline u8, [3]=reserved u8,
203note [4..11]=err_bound u64 LE, [12..19]=deadline_us u64 LE, [20..27]=power_mw u64 LE
204fn ps_parse_oir_v2_contract rec view uchar * to ulong
205if lt len rec 28
206ret PHYS_ERR_BAD_CONTRACT
207end
208put ps_ctr 0 long get rec 0
209put ps_ctr 1 long get rec 1
210put ps_ctr 2 long get rec 2
211put ps_ctr 6 long get rec 3
212v = ps_le_u64 rec 4
213if gt v 9000000000000000000
214ret PHYS_ERR_BAD_CONTRACT
215end
216put ps_ctr 3 long v
217v = ps_le_u64 rec 12
218if gt v 9000000000000000000
219ret PHYS_ERR_BAD_CONTRACT
220end
221put ps_ctr 4 long v
222v = ps_le_u64 rec 20
223if gt v 9000000000000000000
224ret PHYS_ERR_BAD_CONTRACT
225end
226put ps_ctr 5 long v
227if not ps_contract_valid
228ret PHYS_ERR_BAD_CONTRACT
229end
230ret PHYS_OK
231end
232
233note Build WattProp for bank realization idx using baseline CPU/Uncore loads
234fn ps_make_prop idx ulong prop edit WattProp to void
235tid = get pb_tid idx
236pw = get pb_power_mw idx
237bcpu = get ps_base 0
238bunc = get ps_base 1
239fan = get ps_base 2
240if pb_is_gpu_target tid
241put prop p_gpu pw
242put prop p_cpu bcpu
243put prop p_unc bunc
244put prop fan_pct fan
245else
246put prop p_gpu 0
247put prop p_cpu add bcpu pw
248put prop p_unc bunc
249put prop fan_pct fan
250end
251end
252
253note Query watt_cbf.om instantaneous safe power headroom (P_safe_gpu, P_safe_cpu_extra)
254fn ps_query_envelope st view WattThermal pol view WattPolicy to void
255bcpu = get ps_base 0
256bunc = get ps_base 1
257fan = get ps_base 2
258pg_probe = new WattProp WATT_P_GPU_HARD_MAX bcpu bunc fan
259cg = new WattCmd 0 0 0 0 0 0
260wc_cbf_filter st pol pg_probe cg
261if eq get cg fail_closed 1
262put ps_tel PS_TEL_P_SAFE_GPU 0
263put ps_tel PS_TEL_P_SAFE_CPU 0
264ret
265end
266put ps_tel PS_TEL_P_SAFE_GPU get cg p_gpu
267pc_probe = new WattProp 0 WATT_P_TOTAL_HARD_MAX bunc fan
268cc = new WattCmd 0 0 0 0 0 0
269wc_cbf_filter st pol pc_probe cc
270cpu_tot = get cc p_cpu
271cpu_extra = sub cpu_tot bcpu
272if lt cpu_extra 0
273cpu_extra = long 0
274end
275put ps_tel PS_TEL_P_SAFE_CPU cpu_extra
276end
277
278note Priority 1 (INVIOLABLE): verify proof_mask == 7, WattCBF un-clamped admission, and @power cap
279fn ps_check_wattcbf idx ulong st view WattThermal pol view WattPolicy to bool
280if ne get pb_proof idx PB_PROOF_REQUIRED
281ret false
282end
283pw = get pb_power_mw idx
284if lt pw 0
285ret false
286end
287if gt pw WATT_P_TOTAL_HARD_MAX
288ret false
289end
290if le get pb_lat_us idx 0
291ret false
292end
293if lt get pb_joules_uj idx 0
294ret false
295end
296if lt get pb_err_bound idx 0
297ret false
298end
299cpw = get ps_ctr 5
300if gt cpw 0
301if gt pw cpw
302ret false
303end
304end
305pr = new WattProp 0 0 0 0
306cmd = new WattCmd 0 0 0 0 0 0
307ps_make_prop idx pr
308wc_cbf_filter st pol pr cmd
309if ne get cmd fail_closed 0
310ret false
311end
312if ne get cmd p_gpu get pr p_gpu
313ret false
314end
315if ne get cmd p_cpu get pr p_cpu
316ret false
317end
318if ne get cmd p_unc get pr p_unc
319ret false
320end
321ret true
322end
323
324note Priority 2 (INVIOLABLE): declared @err bound is NEVER silently broken
325fn ps_check_err idx ulong to bool
326ret le get pb_err_bound idx get ps_ctr 3
327end
328
329note Exact deterministic preference order inside (1) & (2):
330note minimize est_latency_us, tie-break by lower err_bound, lower est_joules_uj, lower est_power_mw
331fn ps_better a ulong b ulong to bool
332la = get pb_lat_us a
333lb = get pb_lat_us b
334if lt la lb
335ret true
336end
337if gt la lb
338ret false
339end
340ea = get pb_err_bound a
341eb = get pb_err_bound b
342if lt ea eb
343ret true
344end
345if gt ea eb
346ret false
347end
348ua = get pb_joules_uj a
349ub = get pb_joules_uj b
350if lt ua ub
351ret true
352end
353if gt ua ub
354ret false
355end
356ret lt get pb_power_mw a get pb_power_mw b
357end
358
359note Compute exact TuringYield_J (bits/Joule) via watt_cbf.om's overflow-free wc_turing_yield_bpj
360fn ps_turing_yield_uj work_bits long joules_uj long to long
361if or le work_bits 0 le joules_uj 0
362ret 0
363end
364if le work_bits 9000000000000000
365ret wc_turing_yield_bpj mul work_bits 1000 joules_uj
366end
367mj = wc_ceil_div joules_uj 1000
368ret wc_turing_yield_bpj work_bits mj
369end
370
371note Emit structured ALARM_DEADLINE_SLIP event for bank entry idx (never silent!)
372fn ps_emit_alarm idx ulong dl long to void
373cnt = get ps_alm_count 0
374lat = get pb_lat_us idx
375if lt cnt PS_ALARM_CAP
376put ps_alm_code cnt PHYS_ALARM_DEADLINE_SLIP
377put ps_alm_sem cnt get pb_sem idx
378put ps_alm_dl cnt dl
379put ps_alm_lat cnt lat
380put ps_alm_slip cnt sub lat dl
381put ps_alm_prec cnt get pb_prec idx
382put ps_alm_tid cnt get pb_tid idx
383end
384ncnt = add cnt 1
385put ps_alm_count 0 ncnt
386put ps_tel PS_TEL_ALARM_CODE long PHYS_ALARM_DEADLINE_SLIP
387put ps_tel PS_TEL_ALARM_COUNT long ncnt
388end
389
390note Record structured refusal telemetry (fabWtbl untouched!)
391fn ps_record_refusal code ulong to ulong
392put ps_tel PS_TEL_STATUS long code
393put ps_tel PS_TEL_BANK_IDX long PB_NONE
394put ps_tel PS_TEL_TID 0
395put ps_tel PS_TEL_PREC 0
396put ps_tel PS_TEL_POWER_MW 0
397put ps_tel PS_TEL_LAT_US 0
398put ps_tel PS_TEL_JOULES_UJ 0
399put ps_tel PS_TEL_ERR_BOUND 0
400put ps_tel PS_TEL_DL_MARGIN_US 0
401put ps_tel PS_TEL_ALARM_CODE long PHYS_ALARM_NONE
402put ps_tel PS_TEL_ALARM_COUNT long get ps_alm_count 0
403put ps_tel PS_TEL_TURING_YIELD 0
404put ps_tel PS_TEL_HW_WRITES get g_wmeta 12
405put ps_tel PS_TEL_ACTUATOR_MODE long PHYS_ACTUATOR_DRY_RUN_READONLY
406put ps_stats 1 add get ps_stats 1 1
407if eq code PHYS_ERR_HARD_DEADLINE
408put ps_stats 3 add get ps_stats 3 1
409end
410ret code
411end
412
413note Record WattCBF fan and barrier-h telemetry for admitted bank entry idx
414fn ps_record_cbf_tel idx ulong st view WattThermal pol view WattPolicy to void
415pr = new WattProp 0 0 0 0
416cmd = new WattCmd 0 0 0 0 0 0
417ps_make_prop idx pr
418wc_cbf_filter st pol pr cmd
419dt = wc_next_die_delta st get cmd p_gpu get cmd p_cpu get cmd p_unc
420bh = wc_barrier_h get pol t_ceil get st t_die get pol tau dt
421put ps_tel PS_TEL_FAN_PCT get cmd fan_pct
422put ps_tel PS_TEL_BARRIER_H bh
423end
424
425note Record telemetry for an admitted, hot-swapped realization
426fn ps_record_win idx ulong st view WattThermal pol view WattPolicy slipped bool to ulong
427ps_record_cbf_tel idx st pol
428dl = get ps_ctr 4
429lat = get pb_lat_us idx
430margin = long 0
431if gt dl 0
432margin = sub dl lat
433end
434put ps_tel PS_TEL_DL_MARGIN_US margin
435put ps_tel PS_TEL_LAT_US lat
436put ps_tel PS_TEL_BANK_IDX long idx
437put ps_tel PS_TEL_TID long get pb_tid idx
438put ps_tel PS_TEL_PREC long get pb_prec idx
439put ps_tel PS_TEL_POWER_MW get pb_power_mw idx
440put ps_tel PS_TEL_JOULES_UJ get pb_joules_uj idx
441put ps_tel PS_TEL_ERR_BOUND get pb_err_bound idx
442put ps_tel PS_TEL_TURING_YIELD ps_turing_yield_uj get pb_work_bits idx get pb_joules_uj idx
443put ps_tel PS_TEL_HW_WRITES get g_wmeta 12
444put ps_tel PS_TEL_ACTUATOR_MODE long PHYS_ACTUATOR_DRY_RUN_READONLY
445put ps_stats 0 add get ps_stats 0 1
446put ps_stats 2 add get ps_stats 2 1
447if slipped
448ps_emit_alarm idx dl
449put ps_tel PS_TEL_STATUS long PHYS_OK_DEADLINE_SLIP
450ret PHYS_OK_DEADLINE_SLIP
451end
452put ps_tel PS_TEL_ALARM_CODE long PHYS_ALARM_NONE
453put ps_tel PS_TEL_ALARM_COUNT long get ps_alm_count 0
454put ps_tel PS_TEL_STATUS long PHYS_OK
455ret PHYS_OK
456end
457
458note Scan bank for minimum-latency safe candidate inside Priorities (1) & (2).
459note Sets ps_tel[PS_TEL_REJ_WATTCBF] and ps_tel[PS_TEL_REJ_ERR]; returns bank idx or PB_NONE.
460note Uses ps_stats[4] temporarily for found count.
461fn ps_select_best sem_id ulong st view WattThermal pol view WattPolicy to ulong
462best = PB_NONE
463found = ulong 0
464rej_wc = long 0
465rej_err = long 0
466for i ulong PB_CAP
467if eq get pb_used i 1
468if eq get pb_sem i sem_id
469found = add found 1
470if not ps_check_wattcbf i st pol
471rej_wc = add rej_wc 1
472else
473if not ps_check_err i
474rej_err = add rej_err 1
475else
476if eq best PB_NONE
477best = i
478else
479if ps_better i best
480best = i
481end
482end
483end
484end
485end
486end
487end
488put ps_stats 4 found
489put ps_tel PS_TEL_REJ_WATTCBF rej_wc
490put ps_tel PS_TEL_REJ_ERR rej_err
491ret best
492end
493
494note Primary Real-Time Physics Dispatch Solver:
495note Queries watt_cbf.om + ΩIR v2 contract, enforces WattCBF > @err > @deadline,
496note wires winner into fabHotSwap (seed/ng/target/fabric.om), records telemetry.
497fn ps_dispatch sem_id ulong wtbl_slot ulong st view WattThermal pol view WattPolicy to ulong
498if ne get g_wmeta 12 0
499ret ps_record_refusal PHYS_ERR_HW_WRITE_GUARD
500end
501if ge wtbl_slot FAB_SLOTS
502ret ps_record_refusal PHYS_ERR_BAD_SLOT
503end
504if not ps_contract_valid
505ret ps_record_refusal PHYS_ERR_BAD_CONTRACT
506end
507if lt get ps_base 0 0
508ret ps_record_refusal PHYS_ERR_BAD_BASELINE
509end
510if lt get ps_base 1 0
511ret ps_record_refusal PHYS_ERR_BAD_BASELINE
512end
513ps_query_envelope st pol
514if wc_must_fail_closed st pol
515put ps_tel PS_TEL_FAN_PCT WATT_FAN_MAX
516put ps_tel PS_TEL_BARRIER_H 0
517ret ps_record_refusal PHYS_ERR_WATTCBF_FAIL_CLOSED
518end
519best = ps_select_best sem_id st pol
520if eq best PB_NONE
521if eq get ps_stats 4 0
522ret ps_record_refusal PHYS_ERR_NO_REALIZATION
523end
524if gt get ps_tel PS_TEL_REJ_ERR 0
525ret ps_record_refusal PHYS_ERR_ERR_BOUND
526end
527ret ps_record_refusal PHYS_ERR_WATTCBF_UNSAFE
528end
529dl = get ps_ctr 4
530slipped = false
531if gt dl 0
532if gt get pb_lat_us best dl
533slipped = true
534end
535end
536if slipped
537if eq get ps_ctr 2 1
538ret ps_record_refusal PHYS_ERR_HARD_DEADLINE
539end
540end
541tid = get pb_tid best
542mode = pb_mode_for_realization tid get pb_prec best
543if not fabHotSwap wtbl_slot sem_id get pb_code_ptr best tid mode get pb_proof best
544ret ps_record_refusal PHYS_ERR_FAB_SWAP
545end
546ret ps_record_win best st pol slipped
547end
548
549note ---- Sequential Call-Chain Error-Composition Tracker ----
550
551note Begin a sequential call chain bounded by outer_err (and outer precision tier)
552fn ps_chain_begin outer_err long outer_tier long to ulong
553put ps_ch_active 0 0
554put ps_ch_refused 0 0
555put ps_ch_total_err 0 0
556put ps_ch_len 0 0
557if or lt outer_err 0 or lt outer_tier 0 gt outer_tier 2
558ret PHYS_ERR_BAD_CONTRACT
559end
560if ne outer_tier long PHYS_TIER_TOL
561if ne outer_err 0
562ret PHYS_ERR_BAD_CONTRACT
563end
564end
565put ps_ch_outer_err 0 outer_err
566put ps_ch_outer_tier 0 outer_tier
567put ps_ch_active 0 1
568ret PHYS_OK
569end
570
571note Compose one additive stage error bound into the active call chain (overflow-safe)
572fn ps_chain_add_stage sem_id ulong stage_err long to ulong
573if eq get ps_ch_active 0 0
574ret PHYS_ERR_CHAIN_BAD_STAGE
575end
576if eq get ps_ch_refused 0 1
577ret PHYS_ERR_CHAIN_ERR_OVERFLOW
578end
579if lt stage_err 0
580put ps_ch_refused 0 1
581ret PHYS_ERR_CHAIN_BAD_STAGE
582end
583n = get ps_ch_len 0
584if ge n PS_CHAIN_CAP
585put ps_ch_refused 0 1
586ret PHYS_ERR_CHAIN_BAD_STAGE
587end
588tot = get ps_ch_total_err 0
589outer = get ps_ch_outer_err 0
590rem_bud = sub outer tot
591if gt stage_err rem_bud
592put ps_ch_refused 0 1
593ret PHYS_ERR_CHAIN_ERR_OVERFLOW
594end
595put ps_ch_sem n sem_id
596put ps_ch_err n stage_err
597put ps_ch_total_err 0 add tot stage_err
598put ps_ch_len 0 add n 1
599ret PHYS_OK
600end
601
602note Compose one interval/Lipschitz stage: outward-rounded ceil(total_err * amp_num / amp_den) + stage_err
603fn ps_chain_add_stage_amp sem_id ulong stage_err long amp_num long amp_den long to ulong
604if eq get ps_ch_active 0 0
605ret PHYS_ERR_CHAIN_BAD_STAGE
606end
607if eq get ps_ch_refused 0 1
608ret PHYS_ERR_CHAIN_ERR_OVERFLOW
609end
610if or lt stage_err 0 or le amp_den 0 lt amp_num amp_den
611put ps_ch_refused 0 1
612ret PHYS_ERR_CHAIN_BAD_STAGE
613end
614n = get ps_ch_len 0
615if ge n PS_CHAIN_CAP
616put ps_ch_refused 0 1
617ret PHYS_ERR_CHAIN_BAD_STAGE
618end
619tot = get ps_ch_total_err 0
620outer = get ps_ch_outer_err 0
621if gt tot 1000000000
622put ps_ch_refused 0 1
623ret PHYS_ERR_CHAIN_ERR_OVERFLOW
624end
625if gt amp_num 1000000000
626put ps_ch_refused 0 1
627ret PHYS_ERR_CHAIN_ERR_OVERFLOW
628end
629scaled = wc_ceil_div mul tot amp_num amp_den
630if gt scaled outer
631put ps_ch_refused 0 1
632ret PHYS_ERR_CHAIN_ERR_OVERFLOW
633end
634rem_bud = sub outer scaled
635if gt stage_err rem_bud
636put ps_ch_refused 0 1
637ret PHYS_ERR_CHAIN_ERR_OVERFLOW
638end
639put ps_ch_sem n sem_id
640put ps_ch_err n stage_err
641put ps_ch_total_err 0 add scaled stage_err
642put ps_ch_len 0 add n 1
643ret PHYS_OK
644end
645
646note Commit the active call chain; refuses if any stage overflowed outer @err
647fn ps_chain_commit to ulong
648if eq get ps_ch_active 0 0
649ret PHYS_ERR_CHAIN_BAD_STAGE
650end
651if ne get ps_ch_refused 0 0
652ret PHYS_ERR_CHAIN_ERR_OVERFLOW
653end
654if gt get ps_ch_total_err 0 get ps_ch_outer_err 0
655put ps_ch_refused 0 1
656ret PHYS_ERR_CHAIN_ERR_OVERFLOW
657end
658put ps_ch_active 0 0
659ret PHYS_OK
660end