aboutsummaryrefslogtreecommitdiffstats
path: root/hw/align_test.py
blob: 551233a2a2ad6aa45a826717a910267731ce53f8 (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
"""Misaligned descriptor addresses: does the PSP assume 4K (or any) alignment?

Every buffer passed in this audit was page-aligned. Firmware that assumes alignment and then
masks or rounds an address can read/write outside the caller's region. Untested until now.

Cases, each a fresh LOAD_TA (control first):
  aligned                      -> expect 0x0
  cmd_buf +1 / +2 / +4 / +0x40 / +0x800   (byte, half, word, cacheline, sub-page)
  app_phy_addr +1 / +4 / +0x40
A clean rejection or clean success is benign; a stall or a write outside the region is not.
"""
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=0x33333333
base=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 run(tag, cmd_off=0, app_off=0):
    ap_,amc=alloc(0x80000); cp,cmc=alloc(0x20000)
    n=(len(base)+3)&~3
    adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base))
    adev.vram.view(cp,0x20000)[:]=bytes(struct.pack('<I',PAT)*(0x20000//4))
    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
    a=amc+app_off; b=cmc+cmd_off
    t.app_phy_addr_lo,t.app_phy_addr_hi=a&0xFFFFFFFF,a>>32; t.app_len=len(base)
    t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=b&0xFFFFFFFF,b>>32; t.cmd_buf_len=0x4000
    t0=time.time()
    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
        sid=None
    dt=time.time()-t0
    extra=''
    if st==0 and sid is not None and dt<5:
        buf=bytearray(0x4000); struct.pack_into('<I',buf,0,1)
        adev.vram.view(cp+cmd_off,0x4000)[:]=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,1
        try: s=submit(c).resp.status&0xFFFFFFFF
        except Exception as e:
            try: s=int(str(e).strip().split()[-1])&0xFFFFFFFF
            except Exception: s=-1
        adev.gmc.flush_hdp()
        out=bytes(adev.vram.view(cp,0x20000)[:])
        # anything written outside [cmd_off, cmd_off+0x4000) is out of the declared region
        stray=[o for o in range(0,0x20000,4)
               if not (cmd_off<=o<cmd_off+0x4000) and struct.unpack_from('<I',out,o)[0]!=PAT]
        extra='invoke=%#-10x stray-writes=%d'%(s,len(stray))
    print('  %-34s load %#-11x %.1fs  %s'%(tag,st,dt,extra), flush=True)
    return dt>5
run('CONTROL aligned')
for off in (1,2,4,0x40,0x800):
    if run('cmd_buf +%#x'%off, cmd_off=off): sys.exit(3)
for off in (1,4,0x40):
    if run('app_addr +%#x'%off, app_off=off): sys.exit(3)