#!/usr/bin/env python3 """Probe SMU messages whose argument is index-shaped and whose reply is a value. These are read oracles by construction: the host puts an index in MP1_C2PMSG_82, the firmware answers with a 32-bit value in the same register. If an index is not range checked, the reply is a 32-bit read of whatever lies past the array -- an SMU memory disclosure primitive, 4 bytes per message, with no ring and no driver cooperation. Messages probed (smu_v13_0_0_ppsmc.h): 0x1F GetDpmFreqByIndex arg = (PPCLK_e << 16) | level PPCLK_COUNT = 13 0x25 GetVoltageByDpm arg = implementation defined 0x37 GetDebugData arg = index Decision rule: an out-of-range index is only interesting if it is ACCEPTED (resp OK) *and* the returned value varies with the index. A constant reply (0, -1, a clamped value) means the firmware clamped or ignored it -- that is not a primitive, and this script must say so rather than dress it up. Read-only: no Set* message is sent, so the device state is not modified. usage: PYTHONPATH=/root/psp/tinygrad smu_argprobe.py """ import time from tinygrad import Device MSGS = {0x1F: 'GetDpmFreqByIndex', 0x25: 'GetVoltageByDpm', 0x37: 'GetDebugData', 0x02: 'GetSmuVersion'} PPCLK = ['GFXCLK', 'SOCCLK', 'UCLK', 'FCLK', 'DCLK_0', 'VCLK_0', 'DCLK_1', 'VCLK_1', 'DISPCLK', 'DPPCLK', 'DPREFCLK', 'DCFCLK', 'DTBCLK'] PPCLK_COUNT = 13 RESP = {0: 'TIMEOUT', 1: 'OK', 0xFF: 'FAIL', 0xFE: 'UNKNOWN', 0xFD: 'BAD_PREREQ', 0xFC: 'BUSY'} class Probe: def __init__(self): self.adev = Device["AMD"].iface.dev_impl def msg(self, msg, param, timeout_ms=1500): self.adev.mmMP1_SMN_C2PMSG_90.write(0) self.adev.mmMP1_SMN_C2PMSG_82.write(param) self.adev.mmMP1_SMN_C2PMSG_66.write(msg) deadline = time.time() + timeout_ms / 1000.0 resp = 0 while time.time() < deadline: resp = self.adev.mmMP1_SMN_C2PMSG_90.read() if resp: break time.sleep(0.001) return resp, self.adev.mmMP1_SMN_C2PMSG_82.read() def alive(self): return self.msg(0x02, 0, 500)[0] == 1 def sweep(p, msg, args, label): print("\n=== 0x%02x %s -- %s" % (msg, MSGS[msg], label)) out = [] for a in args: resp, val = p.msg(msg, a) out.append((a, resp, val)) print(" arg=0x%08x resp=%-8s val=0x%08x (%d)" % ( a, RESP.get(resp, hex(resp)), val, val)) if resp == 0 and not p.alive(): print(" !! SMU unresponsive, aborting sweep") break return out def main(): p = Probe() print("smu alive: %s" % p.alive()) # level index sweep on a known-good clock (GFXCLK): in-range levels are the control gfx = sweep(p, 0x1F, [i for i in range(12)] + [0x20, 0x40, 0x7f, 0xfe, 0x100, 0xffff], "PPCLK_GFXCLK(0), level index sweep (0xff = count query)") # clock-id sweep at level 0: ids >= PPCLK_COUNT(13) are out of range clk = sweep(p, 0x1F, [(c << 16) for c in list(range(20)) + [0x40, 0xff, 0xfff]], "level 0, PPCLK id sweep (>=13 out of range)") volt = sweep(p, 0x25, list(range(8)) + [0x20, 0xff, 0xffff], "arg sweep") dbg = sweep(p, 0x37, list(range(8)) + [0x20, 0xff, 0xffff], "index sweep") print("\n--- analysis") for name, rows, inrange in (("GetDpmFreqByIndex level", gfx, lambda a: a < 0x20), ("GetDpmFreqByIndex clkid", clk, lambda a: (a >> 16) < PPCLK_COUNT), ("GetVoltageByDpm", volt, lambda a: a < 8), ("GetDebugData", dbg, lambda a: a < 8)): ok_in = [(a, v) for a, r, v in rows if r == 1 and inrange(a)] ok_out = [(a, v) for a, r, v in rows if r == 1 and not inrange(a)] vals_out = {v for _, v in ok_out} verdict = "no out-of-range arg accepted" if ok_out and len(vals_out) > 1: verdict = ("PRIMITIVE CANDIDATE: %d out-of-range args accepted with %d " "DISTINCT values" % (len(ok_out), len(vals_out))) elif ok_out: verdict = ("accepted %d out-of-range args but all returned the same value " "%s -> clamped/ignored, not a read oracle" % (len(ok_out), [hex(v) for v in vals_out])) print(" %-26s in-range OK=%-3d out-of-range OK=%-3d %s" % ( name, len(ok_in), len(ok_out), verdict)) print("\nsmu alive at end: %s" % p.alive()) if __name__ == '__main__': main()