"""Does the SMU validate the DRAM address it DMAs tables to? SetDriverDramAddr{High,Low} + TransferTableSmu2Dram makes the SMU (a trusted co-processor) DMA a table to a host-supplied MC address. Question: does it bounds-check that address, or blindly write there? Escalation (this pass, deliberately NOT touching TMR/PSP memory): 1. baseline -> DMA metrics to the legit driver_table buffer, confirm real data 2. redirect -> DMA to a SECOND buffer we allocated, confirm address is honored 3. out-of-range -> DMA to an MC address well above VRAM (unbacked), observe whether SMU errors (validates) or accepts (no validation) After every step: restore the legit driver addr and check the SMU still responds. No reset / feature / power messages. Records raw response codes. """ import time, ctypes from tinygrad import Device from tinygrad.runtime.autogen.am import smu_13_0_0 as smu from tinygrad.helpers import to_mv d = Device["AMD"]; adev = d.iface.dev_impl MSG, ARG, RESP = adev.mmMP1_SMN_C2PMSG_66, adev.mmMP1_SMN_C2PMSG_82, adev.mmMP1_SMN_C2PMSG_90 TABLE_SMU_METRICS = 5 def send(msg, param=0, timeout_ms=300): RESP.write(0); ARG.write(param & 0xffffffff); MSG.write(msg) end = time.monotonic() + timeout_ms/1000 while time.monotonic() < end: r = RESP.read() if r != 0: return r, ARG.read() time.sleep(0.001) return None, ARG.read() def hi32(x): return (x>>32)&0xffffffff def lo32(x): return x&0xffffffff def set_addr(mc): a=send(smu.PPSMC_MSG_SetDriverDramAddrHigh, hi32(mc)) b=send(smu.PPSMC_MSG_SetDriverDramAddrLow, lo32(mc)) return a[0], b[0] def transfer(idx=TABLE_SMU_METRICS, timeout_ms=300): return send(smu.PPSMC_MSG_TransferTableSmu2Dram, idx, timeout_ms=timeout_ms) def first_nonzero(paddr, n=256): b = bytes(adev.vram.view(paddr, n)[:]) return sum(1 for x in b if x!=0), b[:16].hex() legit = adev.smu.driver_table_paddr legit_mc = adev.paddr2mc(legit) vram_size = adev.vram_size print("driver_table_paddr=%#x mc=%#x vram_size=%#x mc_base=%#x"%(legit,legit_mc,vram_size,adev.gmc.mc_base)) print("smu alive:", adev.smu.is_smu_alive()) # ---- 1. baseline ---- # zero via aligned block slice assignment (USB path needs 4-byte alignment) adev.vram.view(legit, 0x400)[0:0x400] = bytes(0x400) r,a = transfer() nz,head = first_nonzero(legit) print("\n[1] baseline transfer resp=%s buffer nonzero=%d/256 head=%s"%(r,nz,head)) # ---- 2. redirect to a second buffer ---- buf2 = adev.mm.palloc(0x4000, zero=True, boot=False) buf2_mc = adev.paddr2mc(buf2) set_addr(buf2_mc) r,a = transfer() nz2,head2 = first_nonzero(buf2) nz1_after,_ = first_nonzero(legit) print("[2] redirect to buf2=%#x mc=%#x resp=%s buf2 nonzero=%d/256 head=%s"%(buf2,buf2_mc,r,nz2,head2)) print(" (old buffer now: nonzero=%d/256 -> should be unchanged from baseline)"%nz1_after) set_addr(legit_mc) # restore # ---- 3. out-of-range (unbacked, well above VRAM), NOT PSP/TMR ---- oor_paddr = vram_size + 0x2000000 # 32MB past end of VRAM oor_mc = adev.gmc.mc_base + oor_paddr print("\n[3] out-of-range test: paddr=%#x mc=%#x (vram ends at %#x)"%(oor_paddr,oor_mc,vram_size)) set_addr(oor_mc) r,a = transfer(timeout_ms=500) print(" transfer resp=%s arg=%#x -> %s"%(r,a, "SMU ACCEPTED (no range validation!)" if r==1 else ("SMU REJECTED (validates)" if r not in (None,) else "TIMEOUT/hang"))) set_addr(legit_mc) # restore # ---- health check ---- alive = adev.smu.is_smu_alive() v = send(smu.PPSMC_MSG_GetSmuVersion) print("\nrestored legit addr. SMU alive=%s GetSmuVersion resp=%s"%(alive, v[0])) print("DONE. legit driver addr restored:", hex(legit_mc))