omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / tests / irqcmd_host.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | set -eu |
| 4 | R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) |
| 5 | OMC=${IRQ_OMC:-$R/build/irq-omc} |
| 6 | . "$R/tools/irq-omc.pin" |
| 7 | a=$(sha256sum "$OMC"); a=${a%% *} |
| 8 | [ "$a" = "$OMC_SHA256" ] || { echo 'irqcmd_host: INVALID compiler pin' >&2; exit 2; } |
| 9 | export R OMC |
| 10 | python3 - <<'PY' |
| 11 | import os, pathlib, struct, subprocess, tempfile |
| 12 | r=pathlib.Path(os.environ['R']); omc=os.environ['OMC'] |
| 13 | units=(r/'tools/ogd.units').read_text().splitlines() |
| 14 | parts={name:(r/name).read_text() for name in units if name!='ogd.om'} |
| 15 | parts['tests/irqcmd_trace.om']=(r/'tests/irqcmd_trace.om').read_text() |
| 16 | # WolframLanguageEvaluator:{8*8,8*8+40}->{64,104} bytes per status/capture record. |
| 17 | cases={ |
| 18 | 'pre-admission':[1,4,7], 'full':[0]+[1]*9, |
| 19 | 'ordered':[0,1,2,4,5,4,5,4], 'stale-ack':[0,1,4,6,5], |
| 20 | 'double-ack':[0,1,4,5,5], 'immutable':[0,1,4,9,5], |
| 21 | 'revoke-held':[0,1,4,7,5,1,4], 'no-implicit-reset':[0,0], |
| 22 | 'duplicate-sequence':[0,1,3,11], 'bad-op':[0,10], |
| 23 | 'non-model':[0,1,8,1,4,5,7], 'wrap':[0]+[1,4,5]*24, |
| 24 | } |
| 25 | def reference(commands): |
| 26 | mode=state=prod=cons=seq=held=0; slots=[bytes(40) for _ in range(8)]; item=bytes(40); out=b'' |
| 27 | for op in commands: |
| 28 | status=0 |
| 29 | if op==0: |
| 30 | if state: status=-4 |
| 31 | else: mode=state=1 |
| 32 | elif op==8: mode=0 |
| 33 | elif op==9: |
| 34 | value=bytearray(slots[cons%8]); value[8]=99; slots[cons%8]=bytes(value) |
| 35 | elif op in (1,2,3,10,11): |
| 36 | newseq=seq+1 if op in (1,2,10) else seq if op==3 else 0 |
| 37 | function=op if op in (1,2) else 3 if op==10 else 1 |
| 38 | if mode!=1: status=-95 |
| 39 | elif state!=1 or newseq==0 or newseq<=seq: status=-4 |
| 40 | elif function not in (1,2): status=-1 |
| 41 | elif prod-cons>=8: status=-2 |
| 42 | else: |
| 43 | slots[prod%8]=struct.pack('<5Q',function,newseq,16,32,4); prod+=1; seq=newseq |
| 44 | elif op==4: |
| 45 | if mode!=1: status=-95 |
| 46 | elif state!=1 or held: status=-4 |
| 47 | elif prod==cons: status=0 |
| 48 | else: item=slots[cons%8]; held=1; status=1 |
| 49 | elif op in (5,6): |
| 50 | token=struct.unpack('<5Q',item)[1]+int(op==6) |
| 51 | if mode!=1: status=-95 |
| 52 | elif state!=1 or held!=1 or token==0 or token!=struct.unpack('<5Q',item)[1]: status=-4 |
| 53 | else: held=0; cons+=1 |
| 54 | elif op==7: |
| 55 | if mode!=1: status=-95 |
| 56 | elif state!=1: status=-4 |
| 57 | else: state=2; held=0 |
| 58 | fields=struct.unpack('<5Q',item) |
| 59 | out+=struct.pack('<8Q',status+128,prod,cons,state,held,seq,fields[0],fields[1])+item |
| 60 | return out |
| 61 | |
| 62 | def build(t,sources,name): |
| 63 | z=subprocess.run([omc],input='\n'.join(sources.values()).encode(),capture_output=True) |
| 64 | if z.returncode: raise RuntimeError(f'INVALID compile {name}: {z.stderr.decode()}') |
| 65 | p=t/name; p.write_bytes(z.stdout); p.chmod(0o700); return p |
| 66 | |
| 67 | def run(p,commands): |
| 68 | z=subprocess.run([str(p)],input=bytes(commands),capture_output=True,timeout=5) |
| 69 | if z.returncode or len(z.stdout)!=104*len(commands): raise RuntimeError(f'INVALID run {p.name}: {z.returncode} {z.stderr!r}') |
| 70 | return z.stdout |
| 71 | |
| 72 | mutants=[ |
| 73 | ('early-consumer','gsp/irqcmd.om','put iqchold 0 1','put iqccons 0 add get iqccons 0 1\nput iqchold 0 1','ordered'), |
| 74 | ('wrong-sequence','gsp/irqcmd.om','if or eq seq 0 ne seq iqcget 1','if eq seq 0','stale-ack'), |
| 75 | ('double-ack','gsp/irqcmd.om','if or ne get iqcstate 0 1 ne get iqchold 0 1','if ne get iqcstate 0 1','double-ack'), |
| 76 | ('revoke-publishes','gsp/irqcmd.om','put iqcstate 0 2','put iqccons 0 get iqcprod 0\nput iqcstate 0 2','revoke-held'), |
| 77 | ('live-alias','gsp/irqcmd.om','v = ulong 0\nfor i ulong 8\nv = or v shl ulong get iqcitem add mul word 8 i mul i 8','o = add mul rem get iqccons 0 8 40 mul word 8\nv = ulong 0\nfor i ulong 8\nv = or v shl ulong get iqcmd add o i mul i 8','immutable'), |
| 78 | ] |
| 79 | with tempfile.TemporaryDirectory(prefix='irqcmd-') as td: |
| 80 | t=pathlib.Path(td); p=build(t,parts,'commands') |
| 81 | for name,commands in cases.items(): |
| 82 | if run(p,commands)!=reference(commands): raise RuntimeError(f'FAIL {name}') |
| 83 | print(f'irqcmd_host: PASS {len(cases)} independent five-word queue/ownership cases') |
| 84 | for name,path,old,new,case in mutants: |
| 85 | if old not in parts[path]: raise RuntimeError(f'INVALID mutation {name}') |
| 86 | changed=dict(parts); changed[path]=changed[path].replace(old,new,1) |
| 87 | p=build(t,changed,name) |
| 88 | if run(p,cases[case])==reference(cases[case]): raise RuntimeError(f'FAIL undetected {name}') |
| 89 | print(f'irqcmd_host: PASS {len(mutants)} compiling terminating behavior mutants; physical MMU/TLB completion NOT_RUN') |
| 90 | PY |