aboutsummaryrefslogtreecommitdiffstats
path: root/hw/len0_test.py
blob: 386d5fd9fae15bfa882d1ef693a9010c81e8d597 (plain) (blame)
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
#!/usr/bin/env python3
"""Is the declared cmd_buf_len enforced at all?

LOAD_TA accepts cmd_buf_len = 0. If a TA then still writes into the buffer, the declared
length is not a bound the PSP enforces -- meaning the earlier "no writes past the declared
window" result reflected TAs writing little, not the platform enforcing anything.

AUC cmd 5 is the probe: it writes ~158 dwords with a normal window.
"""
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)
PAT=0xA5A5A5A5
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_16-0-0-9_9e9044cf*')[0],'rb').read()
def run(decl):
    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()
    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=decl
    try: r=submit(c); st=r.resp.status&0xFFFFFFFF; sid=r.resp.session_id
    except Exception as e: print('  load failed %s'%str(e)[:50]); return
    buf=bytearray(struct.pack('<I',PAT)*(0x8000//4)); struct.pack_into('<I',buf,0,5)
    adev.vram.view(cp,0x8000)[:]=bytes(buf); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,5
    t0=time.time()
    try: r=submit(c); s=r.resp.status&0xFFFFFFFF
    except Exception as e:
        try: s=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: s=-1
    dt=time.time()-t0
    adev.gmc.flush_hdp()
    out=bytes(adev.vram.view(cp,0x8000)[:])
    changed=[o for o in range(4,0x8000,4) if struct.unpack_from('<I',out,o)[0]!=PAT]
    hi=max(changed) if changed else 0
    print('  decl=%#-9x load=%#-6x invoke=%#-10x %5.1fs dwords changed=%-4d highest=%#x%s'
          %(decl,st,s,dt,len(changed),hi,'   <<< HANG' if dt>5 else ''), flush=True)
for d in (0x1000, 0x0):
    run(d)