aboutsummaryrefslogtreecommitdiffstats
path: root/hw/fp_field_sweep.py
blob: 4bb23cde1b131407c31bfddf306d237a3002f0a7 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""Which host field drives FP cmd3's output count, and can it be made large?

The six-TA differential showed FP cmd3 returns dword[+4] = 8 with a zeroed buffer and 4 with
the 'big' profile, so some host input reaches a count-like value. This isolates which input
field does it by varying one dword at a time, and then tries to push that value up.

Relevance: the static reading says the length FP passes to the SYS_DRV 0x33 descriptor is the
constant 0x10. If a host field can drive a length-like quantity, that reading needs revisiting.

Safety: ordinary INVOKEs to FP only. FP does not emit SYS_DRV opcode 0xd.
"""
import argparse, glob, os, struct, sys

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--dir', default='/root/psp/contall')
    a = ap.parse_args()
    from tinygrad import Device
    import tinygrad.runtime.autogen.am.am as am
    adev = Device["AMD"].iface.dev_impl; psp = adev.psp
    def alloc(n):
        pa = adev.mm.palloc(n, align=0x1000, zero=True, boot=False)
        return pa, adev.paddr2mc(pa)
    def load(blob):
        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=0x8000
        try:
            r=psp._ring_submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, cp
        except Exception as e:
            try: return int(str(e).strip().split()[-1])&0xFFFFFFFF, None, cp
            except Exception: return -1, None, cp
    def invoke(sid, shm, cid, mutate):
        buf = bytearray(0x2000); struct.pack_into('<I', buf, 0, cid)
        mutate(buf)
        adev.vram.view(shm, len(buf))[:] = 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, cid
        try:
            r=psp._ring_submit(c); st=r.resp.status&0xFFFFFFFF
        except Exception as e:
            try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
            except Exception: st=-1
        adev.gmc.flush_hdp()
        return st, bytes(adev.vram.view(shm,32)[:])
    cands = glob.glob(os.path.join(a.dir,'ta_19-0-0-8_7e8be282*'))
    blob = open(cands[0],'rb').read()
    st, sid, shm = load(blob)
    print('FP load: %#x sid=%s' % (st, sid))
    if st != 0: return 2
    base_st, base_out = invoke(sid, shm, 3, lambda b: None)
    print('baseline cmd3: st=%#x out=%s' % (base_st, base_out[:16].hex()))
    print('\nvarying one dword at a time (value 0x800), reporting changed outputs:')
    changed = []
    for off in range(4, 0x60, 4):
        def mut(b, o=off): struct.pack_into('<I', b, o, 0x800)
        s2, out = invoke(sid, shm, 3, mut)
        if out[:16] != base_out[:16]:
            changed.append((off, out[:16].hex()))
            print('   +%#-4x -> st=%#x out=%s' % (off, s2, out[:16].hex()))
    if not changed:
        print('   (none)')
    # push the responsible field up
    if changed:
        off = changed[0][0]
        print('\nscaling field +%#x:' % off)
        for v in (0x10, 0x80, 0x100, 0x800, 0x10000, 0xFFFFFFFF):
            def mut(b, o=off, val=v): struct.pack_into('<I', b, o, val)
            s2, out = invoke(sid, shm, 3, mut)
            print('   value %#-12x -> st=%#-12x out=%s' % (v, s2, out[:16].hex()))
    c = am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_UNLOAD_TA)
    c.cmd.cmd_unload_ta.session_id = sid
    try: psp._ring_submit(c)
    except Exception: pass
    return 0

if __name__ == '__main__':
    sys.exit(main())