interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / tests / irq_host.sh

115 lines · 5862 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# HOST ONLY: independent C oracle, explicit in-image device peer, compiling mutants.
4set -eu
5R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
6OMC=${OMC:-$R/build/omc}
7. "$R/tools/omc.pin"
8a=$(sha256sum "$OMC"); a=${a%% *}
9[ "$a" = "$OMC_SHA256" ] || { echo 'irq_host: INVALID compiler pin' >&2; exit 2; }
10export R OMC
11python3 - <<'PY'
12import hashlib, os, pathlib, struct, subprocess, tempfile
13r = pathlib.Path(os.environ['R'])
14omc = os.environ['OMC']
15policy = (r/'gsp/irq.om').read_text()
16peer = (r/'gsp/irqsim.om').read_text()
17trace = (r/'tests/irq_trace.om').read_text()
18
19def case(**kw):
20 v = dict(raw=64, mask=255, dest=255, queued=3, budget=8,
21 edge=0, fail=0, lock=0, tail=0, inactive=0, mode=0, stale=0, again=0)
22 v.update(kw)
23 return bytes(v.values()) + bytes(3)
24
25# Named boundaries also have explicit expectations independent of both implementations.
26# Output fields are fixed little-endian u64; status is biased by 32, not truncated.
27named = [
28 ('idle',case(raw=0),(0,1,0,0,0,0,0,1,0)),
29 ('drain',case(),(3,1,0,3,3,0,1,2,0)),
30 ('empty-edge',case(queued=0),(0,1,0,0,0,0,1,2,0)),
31 ('masked',case(mask=0),(0,1,0,0,0,64,0,1,0)),
32 ('not-host',case(dest=0),(0,1,0,0,0,64,0,1,0)),
33 ('halt',case(raw=80),(-13,2,80,0,0,80,0,1,1)),
34 ('unknown',case(raw=65),(-13,2,65,0,0,65,0,1,1)),
35 ('masked-halt',case(raw=80,mask=64),(3,1,0,3,3,16,1,2,0)),
36 ('edge-in-drain',case(edge=1),(4,1,64,4,4,64,1,2,0)),
37 ('edge-after-empty',case(tail=1),(3,1,64,3,3,64,1,2,0)),
38 ('budget',case(budget=2),(-11,2,64,2,2,0,1,1,1)),
39 ('exact-budget',case(budget=3),(3,1,0,3,3,0,1,2,0)),
40 ('max-budget',case(queued=64,budget=64),(64,1,0,64,64,0,1,2,0)),
41 ('over-max',case(queued=65,budget=64),(-11,2,64,64,64,0,1,1,1)),
42 ('peer-error',case(fail=2),(-15,2,64,1,1,0,1,1,1)),
43 ('lockdown',case(lock=2),(2,3,64,2,2,0,1,1,0)),
44 ('inactive',case(inactive=1),(-9,1,0,0,0,64,0,0,0)),
45 ('hardware-refused',case(mode=1),(-9,1,0,0,0,64,0,0,0)),
46 ('stale-generation',case(stale=1),(-10,1,0,0,0,64,0,0,0)),
47 ('zero-budget',case(budget=0),(-1,1,0,0,0,64,0,0,0)),
48 ('large-budget',case(budget=65),(-1,1,0,0,0,64,0,0,0)),
49]
50more = [case(**x) for x in [dict(edge=1,again=1),dict(tail=1,again=1),
51 dict(lock=1,again=1),dict(fail=1,again=1),dict(budget=1,again=1),
52 dict(raw=80,again=1),dict(stale=1,again=1),dict(queued=0,again=1)]]
53# All byte-sized raw statuses against independent mask/host routing combinations.
54matrix = [case(raw=s,mask=m,dest=d,queued=1,budget=1)
55 for s in range(256) for m in (0,16,64,80,255) for d in (0,16,64,80,255)]
56
57def run(p, data):
58 z = subprocess.run([str(p)],input=data,capture_output=True,timeout=10)
59 if z.returncode or len(z.stdout) != 128:
60 raise RuntimeError(f'INVALID {p.name}: rc={z.returncode} bytes={len(z.stdout)} {z.stderr!r}')
61 return z.stdout
62
63def compile_om(t, source, name):
64 z = subprocess.run([omc],input=(source+'\n'+peer+'\n'+trace).encode(),capture_output=True)
65 if z.returncode:
66 raise RuntimeError(f'INVALID compile {name}: {z.stderr.decode()}')
67 p=t/name; p.write_bytes(z.stdout); p.chmod(0o700)
68 return p
69
70with tempfile.TemporaryDirectory(prefix='gsp-irq-') as td:
71 t=pathlib.Path(td)
72 ref=t/'ref'
73 subprocess.run(['cc','-std=c11','-O2','-Wall','-Wextra','-Werror',str(r/'tests/irq_ref.c'),'-o',str(ref)],check=True)
74 p=compile_om(t,policy,'irq')
75 cases=[x[1] for x in named]+more+matrix
76 want=[run(ref,x) for x in cases]
77 for i,(name,_,expected) in enumerate(named):
78 v=list(struct.unpack('<16Q',want[i])); v[0]-=32
79 if tuple(v[:9])!=expected or v[15]!=0:
80 raise RuntimeError(f'INVALID referee {name}: {v}, expected {expected}')
81 for i,data in enumerate(cases):
82 if run(p,data)!=want[i]:
83 raise RuntimeError(f'FAIL differential case {i}: {list(data)}')
84 print(f'irq_host: PASS {len(cases)} exact C/model differential cases, including {len(named)} independently asserted boundaries')
85 controls=[
86 ('ack-before-drain','iqclear IQSW\nr = iqpump budget','r = iqpump budget\niqclear IQSW'),
87 ('missing-ack','iqclear IQSW','note omitted acknowledgement'),
88 ('wrong-ack-mask','iqclear IQSW','iqclear 16'),
89 ('late-clear','put iqpend 0 iqread','iqclear IQSW\nput iqpend 0 iqread'),
90 ('stale-generation','if ne gen get iqgen 0','if false'),
91 ('hardware-admission','if ne get iqmode 0 0','if false'),
92 ('inactive-access','if not iqpm','if false'),
93 ('quarantine-latch','put iqstate 0 2','put iqstate 0 1'),
94 ('missing-mask','iqmask\nput iqstate','note omitted mask\nput iqstate'),
95 ('budget','if ge get iqdone 0 budget','if false'),
96 ('unsupported-source','if ne and p not IQSW 0','if false'),
97 ('lockdown-access','if eq r 2','if false'),
98 ('peer-error','if lt r 0\nret iqstop -15','if lt r 0\nret 0'),
99 ('reinitialization','if ne get iqstate 0 0','if false'),
100 ]
101 # Each mutant must compile, run with rc=0 and well-formed output, then differ in behavior.
102 # A crash, trap, setup error or compiler refusal is INVALID, never a detected control.
103 for name,old,new in controls:
104 if policy.count(old)!=1: raise RuntimeError('INVALID mutant anchor '+name)
105 mutant=compile_om(t,policy.replace(old,new),name)
106 killed=False
107 for data,expected in zip(cases[:len(named)+len(more)],want):
108 if run(mutant,data)!=expected:
109 killed=True; break
110 if not killed: raise RuntimeError('SURVIVED '+name)
111 print('DETECTED',name)
112 print('irq source sha256',hashlib.sha256((policy+'\n'+peer+'\n'+trace).encode()).hexdigest())
113 print('irq binary sha256',hashlib.sha256(p.read_bytes()).hexdigest())
114 print(f'irq_host: PASS {len(controls)} compiling behavior mutants; HOST_MODEL only; physical IRQ/firmware/GB10 NOT_RUN')
115PY