1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
"""Fuzz RAS cmd 2 (TRIGGER ERROR) -> ras_parse_trigger_request -> ras_dram_range_config
-> ras_block_config_405030* . Host supplies a 64-bit address + 4 dwords.
This is the last significant unexplored host-reachable PSP surface. It drives
real error injection, so: health check EVERY round, abort on first anomaly.
Request layout (from ras_parse_trigger_request):
+0 type (must be 0, else 40980)
+4 u64 target address
+16,+20,+24,+28 dwords
"""
import ctypes,struct,random,sys,collections,time
from tinygrad import Device
from tinygrad.runtime.autogen.am import am
from tinygrad.helpers import lo32,hi32
d=Device["AMD"]; adev=d.iface.dev_impl; psp=adev.psp
SZ=ctypes.sizeof(am.struct_psp_gfx_cmd_resp); P='/Users/user/dev/amd-psp-re/psp_out/'; S=0x8000; MARK=0x5A
def stat(): return int.from_bytes(bytes(adev.vram.view(psp.cmd_paddr,SZ)[:])[864:868],"little")
def alloc(n):
pa=adev.mm.palloc(n,align=0x1000,zero=True); return pa,adev.paddr2mc(pa)
def load(cmd,blob):
ap,amc=alloc(0x40000); cp,cmc=alloc(S)
adev.vram.view(ap,len(blob))[:]=blob; adev.gmc.flush_hdp()
adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
c=am.struct_psp_gfx_cmd_resp(cmd_id=cmd); t=c.cmd.cmd_load_ta
t.app_phy_addr_hi,t.app_phy_addr_lo=hi32(amc),lo32(amc); t.app_len=len(blob)
t.cmd_buf_phy_addr_hi,t.cmd_buf_phy_addr_lo=hi32(cmc),lo32(cmc); t.cmd_buf_len=S
return psp._ring_submit(c).resp.session_id, cp
def inv(sid,shm,buf,tc):
adev.vram.view(shm,S)[:]=bytes(buf); adev.gmc.flush_hdp()
adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
ic=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD); iv=ic.cmd.cmd_invoke_cmd
iv.session_id,iv.ta_cmd_id=sid,tc
try: psp._ring_submit(ic); ps=0
except RuntimeError: ps=stat()
except TimeoutError: return "TIMEOUT",None
return ps, bytes(adev.vram.view(shm,S)[:])
ras,rsh=load(am.GFX_CMD_ID_LOAD_TA, open(P+'ras_full.bin','rb').read())
print("RAS sid=%s MEMSIZE=%s"%(ras,hex(adev.rreg(0xde3))))
rng=random.Random(int(sys.argv[1]) if len(sys.argv)>1 else 23)
ROUNDS=int(sys.argv[2]) if len(sys.argv)>2 else 2000
BLK=[0,1,2,3,4,5,6,7,8,0xf,0x10,0xff,0xffffffff,0x7fffffff,0x80000000]
ADDRLO=[0,0x1000,0x9a000,0x100000,0x1600000,0x40000000,0xffffffff,0x7fffffff,0x80000000,0xfffff000]
ADDRHI=[0,1,0xff,0xffff,0xffffffff,0x80,0x8000]
rets=collections.Counter(); anom=[]; t0=time.time()
for r in range(ROUNDS):
b=bytearray(S)
for i in range(0x800,S): b[i]=MARK
struct.pack_into('<I',b,0,2) # cmd 2 TRIGGER ERROR
# the trigger request struct is read from the shared buffer; type must be 0
base=0x10
struct.pack_into('<I',b,base+0,0) # request type
# v4 (block type, 64-bit at +4) must be 2 or 4 with high dword 0 to pass
# ras_dram_range_config's gate; bias heavily to those so we reach the handlers.
struct.pack_into('<I',b,base+4, rng.choice([2,4,2,4,2,4,0,1,3,5,6]))
struct.pack_into('<I',b,base+8, rng.choice([0,0,0,0,1,0xffffffff]))
# v5 (64-bit at +24/+28) selects the sub-handler: 1,2 -> block_config_v2; 4 / 0 -> others
struct.pack_into('<I',b,base+24, rng.choice([0,1,2,3,4,5,0xffffffff,0x80000000]))
struct.pack_into('<I',b,base+28, rng.choice([0,0,0,1,0xffffffff]))
for o in (16,20): struct.pack_into('<I',b,base+o,rng.choice(BLK))
# also scatter into the low request area in case the struct sits elsewhere
for off in range(4,0x10,4): struct.pack_into('<I',b,off,rng.choice(BLK))
sent=bytes(b)
ps,o=inv(ras,rsh,b,2)
if o is None:
anom.append(r); print(" [r=%d] HANG alive=%s"%(r,psp.is_sos_alive()))
open('rastrig_hang_r%d.bin'%r,'wb').write(sent); break
rets[struct.unpack_from('<I',o,8)[0]]+=1
if r%200==0:
alive=psp.is_sos_alive(); mem=adev.rreg(0xde3)
if not alive or mem!=0x5ff0:
print(" [r=%d] !! HEALTH FAIL alive=%s MEMSIZE=%s -- aborting"%(r,alive,hex(mem))); break
if r: print(" ..r=%d ok alive=%s MEMSIZE=%s"%(r,alive,hex(mem)))
print("\nstatus histogram: %s"%dict(rets))
print("anomalies: %d elapsed=%.0fs"%(len(anom),time.time()-t0))
print("FINAL alive=%s MEMSIZE=%s"%(psp.is_sos_alive(),hex(adev.rreg(0xde3))))
|