Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-gpu-driver/blob/main/tests/uvm_check.py # omega-gpu-driver aien-dev/omega-gpu-driver Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-gpu-driver / tests / uvm_check.py` 534 lines ยท 17273 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 # Independent checker for tests/uvm_trace.om. It does not call the Omega 4 # binary. It recomputes every status from the printed request, rebuilds every 5 # fault packet, and walks the dumped page-table bytes itself. 6 # 7 # Constants, WolframLanguageEvaluator: 8 # 2^12 -> 4096, 2^16 -> 65536, 2^21 -> `2097152` 9 # 2^31 -> `2147483648`, 16*4096 -> 65536 10 # 2^32 -> `4294967296`, 2^28 -> `268435456` 11 # 2^57 -> `144115188075855872`, 2^52 -> `4503599627370496` 12 # BitAnd[2^52-1, BitNot[2^12-1]] -> `4503599627366400` 13 # BitAnd[2^52-1, BitNot[2^8-1]] -> `4503599627370240` 14 # 2^20-1 -> `1048575` 15 # {2^1-1, 2^9-1, 2^8-1, 2^5-1} -> {1, 511, 255, 31} 16 17 import sys 18 19 PG4 = 4096 20 PG64 = 65536 21 PG2 = `2097152` 22 PAGES = (PG4, PG64, PG2) 23 POOL = `2147483648` 24 POOL_BYTES = 65536 25 VA_BASE = `4294967296` 26 VA_WIN = `268435456` 27 VA_MAX = `144115188075855872` 28 PA_MAX = `4503599627370496` 29 PDE_MASK = `4503599627366400` 30 BIG_MASK = `4503599627370240` 31 PN_LO = `1048575` 32 # level lo/hi. Index 5 is PT_BIG, index 6 is PT_SMALL. Both hang off PD0. 33 LO = (56, 47, 38, 29, 21, 16, 12) 34 HI = (56, 55, 46, 37, 28, 20, 20) 35 # width masks for those levels: 1, 9, 9, 9, 8, 5, 9 bits. 36 MASK = (1, 511, 511, 511, 255, 31, 511) 37 38 UVOK = 0 39 UVEINVAL = 1 40 UVEALIGN = 2 41 UVEBUSY = 3 42 UVENOM = 4 43 UVENOENT = 5 44 UVERANGE = 6 45 UVIGN = 8 46 UVREPLAY = 9 47 UVCANCEL = 10 48 UVFATAL = 11 49 UVFREE = 21 50 UVMAPPED = 22 51 UVCONF = 23 52 UVMISS = 24 53 54 55 class Fail(Exception): 56 pass 57 58 59 def die(msg): 60 raise Fail(msg) 61 62 63 def fields(line): 64 out = {} 65 for part in line.split(): 66 if "=" in part: 67 k, v = part.split("=", 1) 68 out[k] = int(v) 69 return out 70 71 72 def load64(mem, off): 73 if off < 0 or off + 8 > POOL_BYTES: 74 die("walk offset %d outside the pool" % off) 75 return int.from_bytes(mem[off:off + 8], "little") 76 77 78 def in_pool(pa): 79 return pa >= POOL and pa + PG4 <= POOL + POOL_BYTES and (pa & (PG4 - 1)) == 0 80 81 82 def index(va, lv): 83 return (va >> LO[lv]) & MASK[lv] 84 85 86 def decode_pte(v): 87 return {"pa": v & PDE_MASK, "ap": (v >> 1) & 3, "pcf": (v >> 3) & 31, "v": v & 1} 88 89 90 def follow_pde(v, mask): 91 if v & 1: 92 return None 93 if ((v >> 1) & 3) == 0: 94 return None 95 if ((v >> 1) & 3) != 2 or ((v >> 3) & 7) != 2: 96 die("PDE aperture/PCF is not SYS/2: %x" % v) 97 pa = v & mask 98 if not in_pool(pa): 99 die("PDE address %d is outside the pool" % pa) 100 return pa - POOL 101 102 103 def walk_one(mem, va, page): 104 tbl = 0 105 for lv in range(4): 106 v = load64(mem, tbl + index(va, lv) * 8) 107 if v & 1: 108 return UVCONF, None 109 if ((v >> 1) & 3) == 0: 110 return UVMISS, None 111 child = follow_pde(v, PDE_MASK) 112 if child is None: 113 return UVCONF, None 114 tbl = child 115 ent = tbl + index(va, 4) * 16 116 w0 = load64(mem, ent) 117 w1 = load64(mem, ent + 8) 118 if page == PG2: 119 if w0 & 1: 120 return UVMAPPED, decode_pte(w0) 121 if ((w0 >> 1) & 3) or ((w1 >> 1) & 3): 122 return UVCONF, None 123 return UVFREE, None 124 if page == PG64: 125 if (w0 & 1) or ((w1 >> 1) & 3): 126 return UVCONF, None 127 if ((w0 >> 1) & 3) == 0: 128 return UVMISS, None 129 child = follow_pde(w0, BIG_MASK) 130 if child is None: 131 return UVCONF, None 132 v = load64(mem, child + index(va, 5) * 8) 133 if v & 1: 134 return UVMAPPED, decode_pte(v) 135 return UVFREE, None 136 if (w1 & 1) or ((w0 >> 1) & 3): 137 return UVCONF, None 138 if ((w1 >> 1) & 3) == 0: 139 return UVMISS, None 140 child = follow_pde(w1, PDE_MASK) 141 if child is None: 142 return UVCONF, None 143 v = load64(mem, child + index(va, 6) * 8) 144 if v & 1: 145 return UVMAPPED, decode_pte(v) 146 return UVFREE, None 147 148 149 def collect(mem): 150 found = {} 151 152 def upper(tbl, lv, base): 153 n = MASK[lv] + 1 154 for i in range(n): 155 v = load64(mem, tbl + i * 8) 156 if v == 0: 157 continue 158 child = follow_pde(v, PDE_MASK) 159 if child is None: 160 die("upper level %d entry %d is not a PDE" % (lv, i)) 161 va = base | (i << LO[lv]) 162 if lv < 3: 163 upper(child, lv + 1, va) 164 else: 165 pd0(child, va) 166 167 def pd0(tbl, base): 168 for i in range(256): 169 w0 = load64(mem, tbl + i * 16) 170 w1 = load64(mem, tbl + i * 16 + 8) 171 va = base | (i << 21) 172 if w0 & 1: 173 found[va] = (PG2, decode_pte(w0)) 174 elif (w0 >> 1) & 3: 175 child = follow_pde(w0, BIG_MASK) 176 if child is None: 177 die("PD0 big half %d is not a PDE" % i) 178 leaf(child, va, PG64, 5, 32) 179 if w1 & 1: 180 die("PD0 small half %d has the PTE valid bit" % i) 181 if (w1 >> 1) & 3: 182 child = follow_pde(w1, PDE_MASK) 183 if child is None: 184 die("PD0 small half %d is not a PDE" % i) 185 leaf(child, va, PG4, 6, 512) 186 187 def leaf(tbl, base, page, lv, n): 188 for j in range(n): 189 v = load64(mem, tbl + j * 8) 190 if v == 0: 191 continue 192 if (v & 1) == 0: 193 die("leaf page has a non-PTE") 194 found[base | (j << LO[lv])] = (page, decode_pte(v)) 195 196 upper(0, 0, 0) 197 return found 198 def overlap(a, asz, b, bsz): 199 return a < b + bsz and b < a + asz 200 201 202 def pool_hit(pa, sz): 203 return overlap(pa, sz, POOL, POOL_BYTES) 204 205 206 def find_res(reservations, va): 207 for r in reservations: 208 if r["va"] <= va < r["va"] + r["sz"]: 209 return r 210 return None 211 212 213 def res_overlap(reservations, va, sz): 214 for r in reservations: 215 if overlap(va, sz, r["va"], r["sz"]): 216 return True 217 return False 218 219 220 def expect_alloc(reservations, va, sz, pg): 221 if pg not in PAGES or sz == 0 or sz % pg: 222 return UVEINVAL 223 if va % pg: 224 return UVEALIGN 225 if va < VA_BASE or sz > (VA_BASE + VA_WIN) - va: 226 return UVERANGE 227 if res_overlap(reservations, va, sz): 228 return UVEBUSY 229 return UVOK 230 231 232 def expect_fit(reservations, sz, pg): 233 if pg not in PAGES or sz == 0 or sz % pg: 234 return UVEINVAL, 0 235 if sz > VA_WIN: 236 return UVERANGE, 0 237 off = VA_BASE 238 while off < VA_BASE + VA_WIN: 239 if off % pg == 0 and not res_overlap(reservations, off, sz): 240 if sz <= (VA_BASE + VA_WIN) - off: 241 return UVOK, off 242 off += pg 243 return UVENOM, 0 244 245 246 def expect_bind(reservations, va, pa): 247 r = find_res(reservations, va) 248 if r is None: 249 return UVENOENT 250 if pa % r["pg"]: 251 return UVEALIGN 252 if pa >= PA_MAX or pool_hit(pa, r["pg"]): 253 return UVERANGE 254 return UVOK 255 256 257 def expect_map(mem, va, pa, sz, pg, ap, pcf): 258 if pg not in PAGES or sz == 0 or sz > pg * 8 or sz % pg: 259 return UVEINVAL 260 if va % pg or pa % pg: 261 return UVEALIGN 262 if va >= VA_MAX or sz > VA_MAX - va: 263 return UVERANGE 264 if pa >= PA_MAX or sz > PA_MAX - pa or pool_hit(pa, sz): 265 return UVERANGE 266 if ap > 3 or pcf > 31: 267 return UVEINVAL 268 off = 0 269 while off < sz: 270 rc, _ = walk_one(mem, va + off, pg) 271 if rc == UVMAPPED or rc == UVCONF: 272 return UVEBUSY 273 off += pg 274 return UVOK 275 276 277 def expect_unmap(mem, va, sz, pg): 278 if pg not in PAGES or sz == 0 or sz > pg * 8 or sz % pg: 279 return UVEINVAL 280 if va % pg: 281 return UVEALIGN 282 if va >= VA_MAX or sz > VA_MAX - va: 283 return UVERANGE 284 off = 0 285 while off < sz: 286 rc, _ = walk_one(mem, va + off, pg) 287 if rc != UVMAPPED: 288 return UVENOENT 289 off += pg 290 return UVOK 291 292 293 def encode_pkt(va, ts, typ, rep, acc, en, val, cli): 294 raw = bytearray(32) 295 296 def put(n, v): 297 raw[n * 4:n * 4 + 4] = (v & 0xFFFFFFFF).to_bytes(4, "little") 298 299 put(2, ((va >> 12) & PN_LO) << 12) 300 put(3, va >> 32) 301 put(4, ts & 0xFFFFFFFF) 302 put(5, ts >> 32) 303 w = typ & 31 304 w |= (rep & 1) << 7 305 w |= (cli & 127) << 8 306 w |= (acc & 15) << 16 307 w |= (en & 1) << 30 308 w |= (val & 1) << 31 309 put(7, w) 310 return raw.hex() 311 312 313 def decode_pkt(hexbytes): 314 raw = bytes.fromhex(hexbytes) 315 if len(raw) != 32: 316 die("packet is %d bytes" % len(raw)) 317 318 def get(n): 319 return int.from_bytes(raw[n * 4:n * 4 + 4], "little") 320 321 w2, w3, w7 = get(2), get(3), get(7) 322 pn = (w2 >> 12) | (w3 << 20) 323 return { 324 "va": pn << 12, 325 "ts": get(4) | (get(5) << 32), 326 "typ": w7 & 31, 327 "rep": (w7 >> 7) & 1, 328 "cli": (w7 >> 8) & 127, 329 "acc": (w7 >> 16) & 15, 330 "en": (w7 >> 30) & 1, 331 "val": (w7 >> 31) & 1, 332 } 333 334 335 def policy(reservations, pkt): 336 if pkt["val"] == 0: 337 return UVIGN 338 if pkt["en"] == 0 or pkt["rep"] == 0 or pkt["typ"] >= 3: 339 return UVFATAL 340 r = find_res(reservations, pkt["va"]) 341 if r is None or r["pa"] == 0: 342 return UVCANCEL 343 if r["ro"] and pkt["acc"] in (1, 2): 344 return UVFATAL 345 return UVREPLAY 346 347 348 def replay_leaf(reservations, va): 349 r = find_res(reservations, va) 350 off = (va - r["va"]) & ~(r["pg"] - 1) 351 pcf = 4 if r["ro"] else 0 352 return r["va"] + off, r["pa"] + off, r["pg"], pcf 353 354 355 def check(text): 356 lines = [ln.rstrip("\n") for ln in text.splitlines() if ln.strip()] 357 if not lines or lines[-1] != "DONE rc=0": 358 die("trace does not end with DONE rc=0") 359 mem = bytearray(POOL_BYTES) 360 reservations = [] 361 leaves = {} 362 prev_sum = None 363 saw = {"overlap": 0, "misalign": 0, "range": 0, "replay": 0, "fatal": 0, "ignore": 0, "cancel": 0, "ro": 0} 364 i = 0 365 while i < len(lines): 366 line = lines[i] 367 kind = line.split()[0] 368 f = fields(line) 369 if kind == "INIT": 370 if f.get("rc") != UVOK: 371 die("INIT rc %s" % f.get("rc")) 372 elif kind == "ALLOC": 373 exp = expect_alloc(reservations, f["va"], f["sz"], f["pg"]) 374 if f["rc"] != exp: 375 die("ALLOC rc %d, checker wants %d (%s)" % (f["rc"], exp, line)) 376 if exp == UVOK: 377 reservations.append({"va": f["va"], "sz": f["sz"], "pg": f["pg"], "pa": 0, "ro": 0}) 378 if exp == UVEBUSY: 379 saw["overlap"] += 1 380 if exp == UVEALIGN: 381 saw["misalign"] += 1 382 if exp == UVERANGE: 383 saw["range"] += 1 384 elif kind == "FIT": 385 exp, va = expect_fit(reservations, f["sz"], f["pg"]) 386 if f["rc"] != exp or (exp == UVOK and f["va"] != va): 387 die("FIT rc %d va %s, checker wants %d va %d" % (f["rc"], f.get("va"), exp, va)) 388 if exp == UVOK: 389 reservations.append({"va": va, "sz": f["sz"], "pg": f["pg"], "pa": 0, "ro": 0}) 390 elif kind == "BIND": 391 exp = expect_bind(reservations, f["va"], f["pa"]) 392 if f["rc"] != exp: 393 die("BIND rc %d, checker wants %d (%s)" % (f["rc"], exp, line)) 394 if exp == UVOK: 395 find_res(reservations, f["va"])["pa"] = f["pa"] 396 if exp == UVEALIGN: 397 saw["misalign"] += 1 398 elif kind == "PROT": 399 r = find_res(reservations, f["va"]) 400 exp = UVOK if r is not None else UVENOENT 401 if f["rc"] != exp: 402 die("PROT rc %d, checker wants %d" % (f["rc"], exp)) 403 if exp == UVOK: 404 r["ro"] = f["ro"] 405 elif kind == "MAP": 406 exp = expect_map(mem, f["va"], f["pa"], f["sz"], f["pg"], f["ap"], f["pcf"]) 407 if f["rc"] != exp: 408 die("MAP rc %d, checker wants %d (%s)" % (f["rc"], exp, line)) 409 if exp == UVOK: 410 off = 0 411 while off < f["sz"]: 412 leaves[f["va"] + off] = (f["pa"] + off, f["pg"], f["ap"], f["pcf"]) 413 off += f["pg"] 414 if exp == UVEBUSY: 415 saw["overlap"] += 1 416 if exp == UVEALIGN: 417 saw["misalign"] += 1 418 if exp == UVERANGE: 419 saw["range"] += 1 420 elif kind == "UNMAP": 421 exp = expect_unmap(mem, f["va"], f["sz"], f["pg"]) 422 if f["rc"] != exp: 423 die("UNMAP rc %d, checker wants %d (%s)" % (f["rc"], exp, line)) 424 if exp == UVOK: 425 off = 0 426 while off < f["sz"]: 427 del leaves[f["va"] + off] 428 off += f["pg"] 429 if exp == UVEALIGN: 430 saw["misalign"] += 1 431 elif kind == "ENC": 432 if i + 3 >= len(lines): 433 die("truncated fault block") 434 pkt = lines[i + 1] 435 dec = fields(lines[i + 2]) 436 serve = fields(lines[i + 3]) 437 if not pkt.startswith("PKT") or not lines[i + 2].startswith("DEC") or not lines[i + 3].startswith("SERVE"): 438 die("fault block is not ENC/PKT/DEC/SERVE") 439 expect_hex = encode_pkt(f["va"], f["ts"], f["typ"], f["rep"], f["acc"], f["en"], f["val"], f["cli"]) 440 if pkt[3:] != expect_hex: 441 die("packet bytes differ from the independent encoder") 442 got = decode_pkt(pkt[3:]) 443 for k in ("va", "ts", "typ", "rep", "acc", "en", "val", "cli"): 444 if dec.get(k) != got[k]: 445 die("DEC %s is %s, packet says %s" % (k, dec.get(k), got[k])) 446 act = policy(reservations, got) 447 if serve.get("act") != act or serve.get("rc") != act: 448 die("SERVE act %s rc %s, checker wants %d" % (serve.get("act"), serve.get("rc"), act)) 449 if act == UVREPLAY: 450 saw["replay"] += 1 451 leaf_va, leaf_pa, leaf_pg, leaf_pcf = replay_leaf(reservations, got["va"]) 452 rc, _ = walk_one(mem, leaf_va, leaf_pg) 453 if rc != UVMAPPED: 454 leaves[leaf_va] = (leaf_pa, leaf_pg, 2, leaf_pcf) 455 elif act == UVFATAL: 456 saw["fatal"] += 1 457 elif act == UVIGN: 458 saw["ignore"] += 1 459 elif act == UVCANCEL: 460 saw["cancel"] += 1 461 i += 3 462 elif kind == "SUM": 463 prev_sum = f.get("SUM") 464 if prev_sum is None: 465 # "SUM 1294" has no '='; fields() missed it. 466 prev_sum = int(line.split()[1]) 467 elif kind == "DUMP": 468 words = {} 469 i += 1 470 while i < len(lines) and lines[i] != "ENDDUMP": 471 if not lines[i].startswith("W "): 472 die("dump has a non-word line: %s" % lines[i]) 473 parts = lines[i].split() 474 words[int(parts[1])] = parts[2] 475 i += 1 476 mem = bytearray(POOL_BYTES) 477 total = 0 478 for idx, hx in words.items(): 479 if len(hx) != 16: 480 die("word %d is not 16 hex digits" % idx) 481 v = int(hx, 16) 482 raw = v.to_bytes(8, "little") 483 off = idx * 8 484 mem[off:off + 8] = raw 485 total += sum(raw) 486 if prev_sum is None or total != prev_sum: 487 die("SUM %s does not match the dumped bytes %d" % (prev_sum, total)) 488 found = collect(mem) 489 if set(found) != set(leaves): 490 die("walk found %s, requests require %s" % (sorted(found), sorted(leaves))) 491 for va, (pa, pg, ap, pcf) in leaves.items(): 492 got_pg, got = found[va] 493 if got_pg != pg or got["pa"] != pa or got["ap"] != ap or got["pcf"] != pcf or got["v"] != 1: 494 die("VA %d walks to %s, request was pa %d pg %d ap %d pcf %d" % (va, got, pa, pg, ap, pcf)) 495 if pcf == 4: 496 saw["ro"] += 1 497 elif kind == "WALK": 498 rc, got = walk_one(mem, f["va"], f["pg"]) 499 if f["rc"] != rc: 500 die("WALK rc %d, independent walk wants %d (%s)" % (f["rc"], rc, line)) 501 if rc == UVMAPPED: 502 if got["pa"] != f["pa"] or got["ap"] != f["ap"] or got["pcf"] != f["pcf"] or got["v"] != f["v"]: 503 die("WALK fields differ from the independent decode (%s)" % line) 504 elif f["pa"] != 0 or f["v"] != 0: 505 die("unmapped WALK carries a physical address (%s)" % line) 506 elif kind == "DONE": 507 if f.get("rc") != 0: 508 die("DONE rc") 509 elif kind in ("PKT", "DEC", "SERVE", "ENDDUMP", "W"): 510 die("unexpected %s outside its block" % kind) 511 else: 512 die("unknown line %s" % line) 513 i += 1 514 for name in ("overlap", "misalign", "range", "replay", "fatal", "ignore", "cancel", "ro"): 515 if saw[name] < 1: 516 die("negative control missing: %s" % name) 517 return len(lines) 518 519 520 def main(): 521 if len(sys.argv) != 2: 522 sys.stderr.write("usage: uvm_check.py TRACE\n") 523 return 2 524 try: 525 n = check(open(sys.argv[1], "r", encoding="utf-8").read()) 526 except Fail as e: 527 sys.stderr.write("uvm_check: FAIL %s\n" % e) 528 return 1 529 sys.stdout.write("uvm_check: PASS (%d lines, independent walk)\n" % n) 530 return 0 531 532 533 if __name__ == "__main__": 534 sys.exit(main())