#!/usr/bin/env python3 """Load/unload churn at scale: does the session slot table leak or issue out-of-range ids? SYS_DRV indexes a 32-entry slot table and `sub_E0B148` bounds the index to < 0x20. Two things worth knowing, neither testable before the ring ceiling was removed: * does repeated load/unload leak slots (loads eventually fail)? * is any session id >= 32 ever issued (which would index past the table)? 500 cycles, recording every session id and any failure. """ import glob, struct, sys, time sys.path.insert(0,'/root/psp/hw') from tinygrad import Device import tinygrad.runtime.autogen.am.am as am from ring_wrap import install adev=Device["AMD"].iface.dev_impl; psp=adev.psp submit=install(psp,adev) def alloc(n): pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa) blob=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read() ap_,amc=alloc(0x80000); cp,cmc=alloc(0x8000) n=(len(blob)+3)&~3 adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp() def load(): c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA); t=c.cmd.cmd_load_ta t.app_phy_addr_lo,t.app_phy_addr_hi=amc&0xFFFFFFFF,amc>>32; t.app_len=len(blob) t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=0x4000 t0=time.time() try: r=submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, time.time()-t0 except Exception as e: try: return int(str(e).strip().split()[-1])&0xFFFFFFFF, None, time.time()-t0 except Exception: return -1, None, time.time()-t0 def unload(sid): c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_UNLOAD_TA); c.cmd.cmd_unload_ta.session_id=sid try: return submit(c).resp.status&0xFFFFFFFF except Exception as e: try: return int(str(e).strip().split()[-1])&0xFFFFFFFF except Exception: return -1 ids=set(); fails=[] for i in range(500): st,sid,dt = load() if dt>5: print('LOAD STALL at cycle %d'%i, flush=True); break if st!=0 or sid is None: fails.append((i,hex(st))); if len(fails)>3: break continue ids.add(sid) u=unload(sid) if u!=0: fails.append((i,'unload %#x'%u)); break if i%100==0: print(' cycle %d ok (sid=%d)'%(i,sid), flush=True) print('cycles completed, distinct session ids seen: %s'%sorted(ids), flush=True) print('max sid: %s (table has 32 entries, accessor bounds to <0x20)'%(max(ids) if ids else None), flush=True) print('failures: %s'%(fails[:5] or 'none'), flush=True)