aboutsummaryrefslogtreecommitdiffstats
path: root/hw/alias_test.py
blob: 181f4d35329569015b83eca2954349cf9ef00c62 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""Image source and command buffer aliasing the same memory.

Never tested: every LOAD_TA so far used two disjoint allocations. If the loader copies the
image out and then treats an overlapping region as the command buffer -- or writes command
results over the image while authenticating -- aliasing is where that shows.

Cases (health control after each):
  A disjoint (control)
  B cmd_buf == app_phy_addr exactly
  C cmd_buf inside the image (app + 0x1000)
  D app inside the cmd_buf region
"""
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)
blob=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read()
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
def attempt(tag, same=False, cmd_delta=None, app_in_cmd=False):
    ap_,amc=alloc(0x100000); cp,cmc=alloc(0x20000)
    n=(len(blob)+3)&~3
    adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
    a=amc; b=cmc
    if same: b=amc
    elif cmd_delta is not None: b=amc+cmd_delta
    elif app_in_cmd:
        adev.vram.view(cp,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
        a=cmc; b=cmc
    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=a&0xFFFFFFFF,a>>32; t.app_len=len(blob)
    t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=b&0xFFFFFFFF,b>>32; t.cmd_buf_len=0x4000
    t0=time.time(); sid=None
    try:
        r=submit(c); st=r.resp.status&0xFFFFFFFF; sid=r.resp.session_id
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    extra=''
    if st==0 and sid is not None and dt<5:
        bb=bytearray(0x4000); struct.pack_into('<I',bb,0,1)
        adev.vram.view(b if b!=a else a,0x4000)[:]=bytes(bb); adev.gmc.flush_hdp()
        c2=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
        iv=c2.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,1
        t1=time.time()
        try: s=submit(c2).resp.status&0xFFFFFFFF
        except Exception as e:
            try: s=int(str(e).strip().split()[-1])&0xFFFFFFFF
            except Exception: s=-1
        extra='invoke=%#-10x %.1fs'%(s,time.time()-t1)
    print('  %-32s app=%#-13x cmd=%#-13x -> %#-10x %.1fs %s%s'%(tag,a,b,st,dt,extra,
          '  <<< STALL' if dt>5 else ''), flush=True)
    return dt>5
if attempt('A control: disjoint'): sys.exit(3)
if attempt('B cmd_buf == app', same=True): sys.exit(3)
if attempt('C cmd_buf inside image', cmd_delta=0x1000): sys.exit(3)
if attempt('D app inside cmd region', app_in_cmd=True): sys.exit(3)
attempt('HEALTH control: disjoint')