From 3e93ed8492aa56f0f4bcfedbbcb07aa8fbdf2c7c Mon Sep 17 00:00:00 2001 From: AutoJanitor Date: Fri, 4 Dec 2026 18:16:53 -0600 Subject: [PATCH] gv100: port the OBJTMR constructor _nv059547rm (#480) engine_construct.rs stated OBJTMR's fields as constants because its constructor had no body. Port _nv059547rm and the pool initialiser _nv049710rm it calls (timer/construct.rs) from the disassembly in reports/objtmr-construct-rea/: byte +251c, the spinlock at +2500 (stored before its null test; a null lock returns 0x1a), the null list heads +250/+258, the 96-entry alarm pool from +268 and the 36-entry pool from +2078 with their free-list heads, the +24b-gated +24f8 event, the retry count +2510 = 3 and +2520 = 0. gv100() runs it between Intr and OBJBUS. The engine live oracle now records these fields (`construct` bytes and new `construct-pointer` lines) and a focused oracle test compares every store with the object the original constructs in emulation. Co-authored-by: Claude Opus 5.5 --- .../_nv049710rm.objdump.txt | 40 ++++ .../_nv052691rm.objdump.txt | 5 + .../_nv052744rm.objdump.txt | 32 +++ .../_nv059547rm.objdump.txt | 44 +++++ reports/objtmr-construct-rea/derivation.json | 187 ++++++++++++++++++ reports/objtmr-construct-rea/index.json | 38 ++++ src/nvidia/src/gpu/engine_construct.rs | 37 +++- src/nvidia/src/gpu/engine_construct_tests.rs | 125 +++++++++++- src/nvidia/src/gpu/engine_live_support.rs | 8 + src/nvidia/src/gpu/timer.rs | 1 + src/nvidia/src/gpu/timer/construct.rs | 168 ++++++++++++++++ src/nvidia/src/gpu/timer/construct_tests.rs | 170 ++++++++++++++++ tools/check_gpu_engine_live_binary.py | 24 ++- 13 files changed, 869 insertions(+), 10 deletions(-) create mode 100644 reports/objtmr-construct-rea/_nv049710rm.objdump.txt create mode 100644 reports/objtmr-construct-rea/_nv052691rm.objdump.txt create mode 100644 reports/objtmr-construct-rea/_nv052744rm.objdump.txt create mode 100644 reports/objtmr-construct-rea/_nv059547rm.objdump.txt create mode 100644 reports/objtmr-construct-rea/derivation.json create mode 100644 reports/objtmr-construct-rea/index.json create mode 100644 src/nvidia/src/gpu/timer/construct.rs create mode 100644 src/nvidia/src/gpu/timer/construct_tests.rs diff --git a/reports/objtmr-construct-rea/_nv049710rm.objdump.txt b/reports/objtmr-construct-rea/_nv049710rm.objdump.txt new file mode 100644 index 00000000..f6ee1521 --- /dev/null +++ b/reports/objtmr-construct-rea/_nv049710rm.objdump.txt @@ -0,0 +1,40 @@ +0000000000074840 <_nv049710rm>: + 74840: endbr64 + 74844: push %r12 + 74846: lea 0x2078(%rdi),%rax + 7484d: lea 0x2098(%rdi),%rsi + 74854: movq $0x0,0x2068(%rdi) + 7485f: mov %rax,0x2070(%rdi) + 74866: lea 0x24f8(%rdi),%rax + 7486d: nopl (%rax) + 74870: mov %rsi,-0x8(%rsi) + 74874: add $0x20,%rsi + 74878: cmp %rax,%rsi + 7487b: jne 74870 <_nv049710rm+0x30> + 7487d: movq $0x0,0x24f0(%rdi) + 74888: xor %r12d,%r12d + 7488b: cmpb $0x0,0x24b(%rdi) + 74892: jne 7489a <_nv049710rm+0x5a> + 74894: mov %r12d,%eax + 74897: pop %r12 + 74899: ret + 7489a: mov $0x1,%r8d + 748a0: xor %ecx,%ecx + 748a2: mov $0x0,%rdx + 748a5: R_X86_64_32S .text+0x74bc0 + 748a9: call 748ae <_nv049710rm+0x6e> + 748aa: R_X86_64_PC32 _nv059559rm-0x4 + 748ae: mov %eax,%r12d + 748b1: test %eax,%eax + 748b3: je 74894 <_nv049710rm+0x54> + 748b5: mov %eax,%edx + 748b7: mov $0x411000,%esi + 748bc: mov $0xe6e1714,%edi + 748c1: xor %eax,%eax + 748c3: call 748c8 <_nv049710rm+0x88> + 748c4: R_X86_64_PC32 _nv047869rm-0x4 + 748c8: mov $0x41,%esi + 748cd: mov %r12d,%edi + 748d0: call 748d5 <_nv049710rm+0x95> + 748d1: R_X86_64_PC32 _nv046747rm-0x4 + 748d5: jmp 74894 <_nv049710rm+0x54> diff --git a/reports/objtmr-construct-rea/_nv052691rm.objdump.txt b/reports/objtmr-construct-rea/_nv052691rm.objdump.txt new file mode 100644 index 00000000..3e3b8ce2 --- /dev/null +++ b/reports/objtmr-construct-rea/_nv052691rm.objdump.txt @@ -0,0 +1,5 @@ +0000000000af7f80 <_nv052691rm>: + af7f80: endbr64 + af7f84: mov $0x0,%rax + af7f87: R_X86_64_32S .bss+0x39e50 + af7f8b: ret diff --git a/reports/objtmr-construct-rea/_nv052744rm.objdump.txt b/reports/objtmr-construct-rea/_nv052744rm.objdump.txt new file mode 100644 index 00000000..aa84e5f5 --- /dev/null +++ b/reports/objtmr-construct-rea/_nv052744rm.objdump.txt @@ -0,0 +1,32 @@ +0000000000af8ea0 <_nv052744rm>: + af8ea0: endbr64 + af8ea4: push %r13 + af8ea6: mov %rdi,%r13 + af8ea9: push %r12 + af8eab: sub $0x8,%rsp + af8eaf: mov 0x0(%rip),%rsi # af8eb6 <_nv052744rm+0x16> + af8eb2: R_X86_64_PC32 _nv052748rm-0x4 + af8eb6: call af8ebb <_nv052744rm+0x1b> + af8eb7: R_X86_64_PC32 _nv015312rm-0x4 + af8ebb: mov %rax,%r12 + af8ebe: test %rax,%rax + af8ec1: je af8edc <_nv052744rm+0x3c> + af8ec3: movq $0x0,0x10(%rax) + af8ecb: mov %rax,%rdi + af8ece: call af8ed3 <_nv052744rm+0x33> + af8ecf: R_X86_64_PC32 os_alloc_spinlock-0x4 + af8ed3: test %eax,%eax + af8ed5: jne af8ef0 <_nv052744rm+0x50> + af8ed7: mov %r13,0x10(%r12) + af8edc: add $0x8,%rsp + af8ee0: mov %r12,%rax + af8ee3: pop %r12 + af8ee5: pop %r13 + af8ee7: ret + af8ee8: nopl 0x0(%rax,%rax,1) + af8ef0: mov %r12,%rsi + af8ef3: mov %r13,%rdi + af8ef6: xor %r12d,%r12d + af8ef9: call af8efe <_nv052744rm+0x5e> + af8efa: R_X86_64_PC32 _nv015317rm-0x4 + af8efe: jmp af8edc <_nv052744rm+0x3c> diff --git a/reports/objtmr-construct-rea/_nv059547rm.objdump.txt b/reports/objtmr-construct-rea/_nv059547rm.objdump.txt new file mode 100644 index 00000000..88566478 --- /dev/null +++ b/reports/objtmr-construct-rea/_nv059547rm.objdump.txt @@ -0,0 +1,44 @@ +0000000000a923d0 <_nv059547rm>: + a923d0: endbr64 + a923d4: push %rbx + a923d5: mov %rsi,%rbx + a923d8: movb $0x0,0x251c(%rsi) + a923df: call a923e4 <_nv059547rm+0x14> + a923e0: R_X86_64_PC32 _nv052691rm-0x4 + a923e4: mov %rax,%rdi + a923e7: call a923ec <_nv059547rm+0x1c> + a923e8: R_X86_64_PC32 _nv052744rm-0x4 + a923ec: mov %rax,0x2500(%rbx) + a923f3: test %rax,%rax + a923f6: je a92480 <_nv059547rm+0xb0> + a923fc: lea 0x268(%rbx),%rax + a92403: lea 0x2068(%rbx),%rdx + a9240a: movq $0x0,0x250(%rbx) + a92415: movq $0x0,0x258(%rbx) + a92420: mov %rax,0x260(%rbx) + a92427: lea 0x2b8(%rbx),%rax + a9242e: xchg %ax,%ax + a92430: mov %rax,-0x8(%rax) + a92434: add $0x50,%rax + a92438: movb $0x0,-0x70(%rax) + a9243c: movb $0x1,-0x80(%rax) + a92440: cmp %rdx,%rax + a92443: jne a92430 <_nv059547rm+0x60> + a92445: movq $0x0,0x2060(%rbx) + a92450: mov %rbx,%rdi + a92453: movb $0x0,0x2048(%rbx) + a9245a: movb $0x1,0x2038(%rbx) + a92461: call a92466 <_nv059547rm+0x96> + a92462: R_X86_64_PC32 _nv049710rm-0x4 + a92466: xor %eax,%eax + a92468: movq $0x3,0x2510(%rbx) + a92473: movq $0x0,0x2520(%rbx) + a9247e: pop %rbx + a9247f: ret + a92480: mov $0x410000,%esi + a92485: mov $0xe75cd9b,%edi + a9248a: call a9248f <_nv059547rm+0xbf> + a9248b: R_X86_64_PC32 _nv047869rm-0x4 + a9248f: mov $0x1a,%eax + a92494: pop %rbx + a92495: ret diff --git a/reports/objtmr-construct-rea/derivation.json b/reports/objtmr-construct-rea/derivation.json new file mode 100644 index 00000000..daf8217d --- /dev/null +++ b/reports/objtmr-construct-rea/derivation.json @@ -0,0 +1,187 @@ +{ + "subject": { + "name": "nv-kernel.o_binary", + "version": "580.178.04", + "sha256": "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" + }, + "evidence_categories": { + "[D]": "static disassembly of the stock object (files in this directory)", + "[E]": "instruction emulation of the original constructor inside tools/check_gpu_engine_live_binary.py (D.construct(), the original gpumgrAttachGpu over Unicorn); fields read from the constructed OBJTMR", + "[T]/[O]": "none: no hardware and no trace of the original driver was used for this report" + }, + "call": { + "function": "_nv059547rm", + "arguments": "rdi = GPU (unused except as caller context), rsi = OBJTMR (kept in rbx)", + "category": "[D]" + }, + "stores_in_order": [ + { + "offset": "+251c", + "width": 1, + "value": "0", + "category": "[D]", + "insn": "a923d8" + }, + { + "offset": "+2500", + "width": 8, + "value": "_nv052744rm(_nv052691rm()) \u2014 stored before the null test", + "category": "[D]", + "insn": "a923ec" + }, + { + "branch": "+2500 == null", + "effect": "_nv047869rm(0xe75cd9b, 0x410000); return 0x1a (NV_ERR_INSUFFICIENT_RESOURCES); no further store", + "category": "[D]", + "insn": "a92480..a92495" + }, + { + "offset": "+250", + "width": 8, + "value": "0", + "category": "[D]", + "insn": "a9240a" + }, + { + "offset": "+258", + "width": 8, + "value": "0", + "category": "[D]", + "insn": "a92415" + }, + { + "offset": "+260", + "width": 8, + "value": "&OBJTMR+268", + "category": "[D]", + "insn": "a92420" + }, + { + "loop": "rax = OBJTMR+2b8 step 0x50 until OBJTMR+2068 (95 iterations)", + "per_entry_i_0_to_94": { + "base": "+268 + 0x50*i", + "+48": "&entry(i+1)", + "+30": "0 (byte)", + "+20": "1 (byte)" + }, + "store_order": "+48, +30, +20", + "category": "[D]", + "insn": "a92430..a92443" + }, + { + "offset": "+2060", + "width": 8, + "value": "0 (entry 95 +48)", + "category": "[D]", + "insn": "a92445" + }, + { + "offset": "+2048", + "width": 1, + "value": "0 (entry 95 +30)", + "category": "[D]", + "insn": "a92453" + }, + { + "offset": "+2038", + "width": 1, + "value": "1 (entry 95 +20)", + "category": "[D]", + "insn": "a9245a" + }, + { + "call": "_nv049710rm(OBJTMR); return value ignored", + "category": "[D]", + "insn": "a92461" + }, + { + "in": "_nv049710rm", + "offset": "+2068", + "width": 8, + "value": "0", + "category": "[D]", + "insn": "74854" + }, + { + "in": "_nv049710rm", + "offset": "+2070", + "width": 8, + "value": "&OBJTMR+2078", + "category": "[D]", + "insn": "7485f" + }, + { + "in": "_nv049710rm", + "loop": "rsi = OBJTMR+2098 step 0x20 until OBJTMR+24f8 (35 iterations)", + "per_entry_i_0_to_34": { + "base": "+2078 + 0x20*i", + "+18": "&entry(i+1)" + }, + "category": "[D]", + "insn": "74870..7487b" + }, + { + "in": "_nv049710rm", + "offset": "+24f0", + "width": 8, + "value": "0 (entry 35 +18)", + "category": "[D]", + "insn": "7487d" + }, + { + "in": "_nv049710rm", + "branch": "byte +24b != 0", + "effect": "_nv059559rm(OBJTMR, OBJTMR+24f8, .text+0x74bc0, 0, 1); on non-zero status _nv047869rm(0xe6e1714, 0x411000, status) and _nv046747rm(status, 0x41); _nv049710rm returns the status, which the constructor discards", + "category": "[D]", + "insn": "7488b..748d5" + }, + { + "offset": "+2510", + "width": 8, + "value": "3 (u32 +2510 = 3, u32 +2514 = 0)", + "category": "[D]", + "insn": "a92468" + }, + { + "offset": "+2520", + "width": 8, + "value": "0", + "category": "[D]", + "insn": "a92473" + }, + { + "return": "0", + "category": "[D]", + "insn": "a92466" + } + ], + "emulated_gv100_object": { + "category": "[E]", + "object_size": "0x2530", + "fields": { + "+24b": "00", + "+24e": "00", + "+248": "01000000 (not written by this constructor)", + "+250": "null", + "+258": "null", + "+2068": "0", + "+2510": "0300000000000000", + "+2518": "0", + "+2520": "0", + "+251c": "0", + "+2500": "outside the object (the spinlock)", + "+260": "+268", + "+2070": "+2078", + "+24f8": "null", + "+24f0": "null", + "+2060": "null" + }, + "pools": "all 96 alarm entries (+20 = 1, +30 = 0, +48 = next/null) and all 36 callback entries (+18 = next/null) as derived above: 0 mismatches", + "how": "tools/check_gpu_engine_live_binary.py `construct` / `construct-pointer` lines (target/gpu-stage/original-engine-live.txt)" + }, + "not_derived": [ + "_nv059559rm (event create) and its callback .text+0x74bc0: not reached on GV100 (+24b = 0 [E]); the Rust treats it as a service", + "_nv047869rm / _nv046747rm: diagnostics, treated as services", + "the spinlock object's layout (_nv015312rm allocation size _nv052748rm): outside OBJTMR, not modelled" + ] +} \ No newline at end of file diff --git a/reports/objtmr-construct-rea/index.json b/reports/objtmr-construct-rea/index.json new file mode 100644 index 00000000..d63753ce --- /dev/null +++ b/reports/objtmr-construct-rea/index.json @@ -0,0 +1,38 @@ +{ + "subject": { + "name": "nv-kernel.o_binary", + "version": "580.178.04", + "sha256": "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b" + }, + "tool": "GNU objdump 2.45 -dr --no-show-raw-insn (the REA MCP server was not available to this session)", + "functions": [ + { + "name": "_nv059547rm", + "address": "0xa923d0", + "size": 198, + "file": "reports/objtmr-construct-rea/_nv059547rm.objdump.txt", + "role": "OBJTMR ConstructEngine" + }, + { + "name": "_nv049710rm", + "address": "0x74840", + "size": null, + "file": "reports/objtmr-construct-rea/_nv049710rm.objdump.txt", + "role": "callback-pool initialiser called by the constructor" + }, + { + "name": "_nv052691rm", + "address": "0xaf7f80", + "size": null, + "file": "reports/objtmr-construct-rea/_nv052691rm.objdump.txt", + "role": "returns the global allocator (.bss+0x39e50)" + }, + { + "name": "_nv052744rm", + "address": "0xaf8ea0", + "size": null, + "file": "reports/objtmr-construct-rea/_nv052744rm.objdump.txt", + "role": "spinlock create: allocation via _nv015312rm, os_alloc_spinlock, allocator stored at +10; null on failure" + } + ] +} \ No newline at end of file diff --git a/src/nvidia/src/gpu/engine_construct.rs b/src/nvidia/src/gpu/engine_construct.rs index a26588ed..2b11cc33 100644 --- a/src/nvidia/src/gpu/engine_construct.rs +++ b/src/nvidia/src/gpu/engine_construct.rs @@ -12,7 +12,7 @@ //! | OBJMC | `_nv003374rm`: no field of `mc_init::State` | `_nv043560rm` = `mc_init::construct_engine`, stores nothing | //! | KernelMc | none | none; both fields are stored by InitLocked | //! | Intr | `_nv005787rm`: Intr+3f7 = 1 | `_nv037522rm` = `intr_init::construct_engine` | -//! | OBJTMR | none of the fields below | `_nv059547rm` has no body here; the fields below are zero after it | +//! | OBJTMR | none of the fields below (+24b is zero) | `_nv059547rm` = `timer::construct::construct_engine` | //! | OBJBUS | `_nv005812rm`: OBJBUS+460, +461, +467, +469 = 0 | `_nv018445rm` = `bus_stage::construct_engine` | //! //! Binder values are those of reports/gv100-engine-hal-bindings.json @@ -28,8 +28,7 @@ //! //! Not derived here, and passed in as `Foreign`: fields of engine objects //! whose constructors have no body in this driver (Perf, MemorySystem, -//! KernelGmmu, Lpwr). OBJTMR's own constructor is in the same position; its -//! fields are stated as constants and are checked by the same oracle. +//! KernelGmmu, Lpwr). //! //! These are the values after the attach. The pre-stage steps of //! `_nv000790rm` that run before the PreInit pass change some of them on a @@ -77,6 +76,9 @@ pub const GV100_GPU_4DD8: u32 = 1; pub const GV100_INTR_3F7: bool = true; /// OBJTMR+24e after construction. pub const GV100_TIMER_24E: bool = false; +/// OBJTMR+24b, the byte `_nv049710rm` tests before creating the +24f8 +/// event: zero in the object the original constructs for a GV100. +pub const GV100_TIMER_24B: bool = false; /// Fields of other engines' objects that a body of the first engines reads /// or writes. Their constructors have no body in this driver, so the caller @@ -103,7 +105,8 @@ pub struct TimerLists { pub os_258: bool, } -/// Both lists are empty after construction. +/// Both lists are empty after construction: the constructor stores both +/// heads as null. pub const CONSTRUCTED_TIMER_LISTS: TimerLists = TimerLists { alarm_250: false, os_258: false, @@ -116,11 +119,22 @@ pub struct Constructed { pub intr_3f7: bool, pub timer_24e: bool, pub timer_lists: TimerLists, + /// What the OBJTMR constructor stored. + pub timer_construct: timer::construct::Constructed, } -/// What the two constructors with registry reads and allocations call. -pub trait Services: intr_init::ConstructServices + bus_stage::ConstructServices {} -impl Services for T {} +/// What the constructors with registry reads and allocations call. +pub trait Services: + intr_init::ConstructServices + timer::construct::ConstructServices + bus_stage::ConstructServices +{ +} +impl< + T: intr_init::ConstructServices + + timer::construct::ConstructServices + + bus_stage::ConstructServices, + > Services for T +{ +} /// Which constructor returned a status other than success. The original /// deletes such an object, so the engine is then not constructed. @@ -191,6 +205,14 @@ pub fn gv100( status, })); } + // OBJTMR: zeroed object, then the constructor. + let (timer_construct, status) = timer::construct::construct_engine(services, GV100_TIMER_24B)?; + if status != NvStatus::OK { + return Ok(Err(Declined { + class_id: super::engine_bodies::OBJTMR, + status, + })); + } // OBJBUS: the binder's zeros, GPU+4dd8 from the GPU's own initializer, // then the constructor. let bus = bus_stage::State { @@ -292,6 +314,7 @@ pub fn gv100( intr_3f7: GV100_INTR_3F7, timer_24e: GV100_TIMER_24E, timer_lists: CONSTRUCTED_TIMER_LISTS, + timer_construct, })) } diff --git a/src/nvidia/src/gpu/engine_construct_tests.rs b/src/nvidia/src/gpu/engine_construct_tests.rs index 1e8b6c13..84d2a566 100644 --- a/src/nvidia/src/gpu/engine_construct_tests.rs +++ b/src/nvidia/src/gpu/engine_construct_tests.rs @@ -10,11 +10,13 @@ use std::{collections::BTreeMap, string::String, vec::Vec}; type Service = Result; /// The constructors' callees as the oracle's emulation answers them: the -/// registry has no key, the vector and list initialisers succeed. +/// registry has no key, the vector and list initialisers succeed, the OBJTMR +/// spinlock is created. #[derive(Default)] struct Fixture { calls: Vec, fail_vector: Option, + no_timer_lock: bool, } impl intr_init::Diagnostics for Fixture { @@ -45,6 +47,18 @@ impl intr_init::ConstructServices for Fixture { Ok(()) } } +impl timer::construct::ConstructServices for Fixture { + fn timer_lock_create(&mut self) -> Service { + self.calls.push("timer lock".into()); + Ok(!self.no_timer_lock) + } + fn timer_lock_unavailable(&mut self) {} + fn timer_event_create_24f8(&mut self) -> Service { + self.calls.push("timer event".into()); + Ok(NvStatus::OK) + } + fn timer_event_create_failed(&mut self, _status: NvStatus) {} +} impl bus_stage::ConstructServices for Fixture { fn disable_fast_flush(&mut self) -> Service> { Ok(Err(NvStatus(0x57))) @@ -94,6 +108,85 @@ pub(in crate::gpu) fn constructed(live: &Live) -> Constructed { .unwrap() } +/// Compares every pointer the OBJTMR constructor stores with the oracle's +/// `construct-pointer` lines, and returns its other stores (and the byte +/// +24b it tests) as bytes of the object. The list heads +250 and +258 are +/// left out: they are compared as null or not through `timer_lists`. +fn timer_construct_bytes(live: &Live, built: &Constructed) -> BTreeMap<(&'static str, u32), u8> { + use timer::construct::Value; + let pointer_lines: BTreeMap = live + .construct_pointers + .iter() + .filter(|(object, _, _)| object == "OBJTMR") + .map(|(_, offset, target)| (*offset, target.as_str())) + .collect(); + let mut bytes = BTreeMap::new(); + bytes.insert(("OBJTMR", 0x24b), u8::from(built.timer_construct.tmr_24b)); + let mut pointers = 0; + built.timer_construct.for_each_store(|offset, value| { + if let Some(target) = pointer_lines.get(&offset) { + let expected = match value { + Value::Quad(0) => String::from("null"), + Value::Object(at) => std::format!("+{at:x}"), + // `outside` shows only a non-null pointer outside OBJTMR, + // not that it is the lock _nv052744rm returned. + Value::Lock => String::from("outside"), + other => panic!("OBJTMR+{offset:x} {other:?} is not a pointer"), + }; + assert_eq!(*target, expected, "OBJTMR+{offset:x}"); + pointers += 1; + return; + } + let stored: Vec = match value { + Value::Byte(byte) => std::vec![byte], + Value::Quad(quad) => quad.to_le_bytes().to_vec(), + other => panic!("OBJTMR+{offset:x} {other:?} has no pointer line"), + }; + if matches!(offset, 0x250 | 0x258) { + assert_eq!(value, Value::Quad(0)); + return; + } + for (index, byte) in stored.into_iter().enumerate() { + assert!(bytes + .insert(("OBJTMR", offset + index as u32), byte) + .is_none()); + } + }); + // The +24f8 event is created only when +24b is set; on a GV100 it is + // not, and the pointer stays null. + assert_eq!(built.timer_construct.event_24f8, None); + assert_eq!(pointer_lines.get(&0x24f8), Some(&"null")); + assert_eq!(pointers + 1, pointer_lines.len()); + bytes +} + +#[test] +#[ignore = "requires the engine live oracle"] +fn the_timer_constructor_equals_the_object_the_original_constructs() { + let live = load(); + let built = constructed(&live); + let bytes = timer_construct_bytes(&live, &built); + // +248 (4 bytes) and +24b overlap, so count bytes, not lines. + let mut compared = std::collections::BTreeSet::new(); + for (object, offset, expected) in &live.construct { + if object != "OBJTMR" { + continue; + } + for (index, byte) in expected.iter().enumerate() { + let at = offset + index as u32; + if let Some(stated) = bytes.get(&("OBJTMR", at)) { + assert_eq!(stated, byte, "OBJTMR+{at:x}"); + compared.insert(at); + } + } + } + // +24b, +251c, 8 bytes each of +2068, +2510 and +2520, and two bytes + // of each of the 96 alarm entries: every one is in the oracle. + assert_eq!(compared.len(), bytes.len()); + assert_eq!(compared.len(), 1 + 1 + 3 * 8 + 2 * 96); + assert_eq!(live.construct_pointers.len(), 4 + 96 + 36); +} + #[test] #[ignore = "requires the engine live oracle"] fn every_field_equals_the_object_the_original_constructs() { @@ -118,6 +211,10 @@ fn every_field_equals_the_object_the_original_constructs() { } bytes.insert(("Intr", 0x3f7), u8::from(built.intr_3f7)); bytes.insert(("OBJTMR", 0x24e), u8::from(built.timer_24e)); + // The OBJTMR constructor: the byte it tests, then every store it makes. + for (key, byte) in timer_construct_bytes(&live, &built) { + assert!(bytes.insert(key, byte).is_none(), "{}+{:x}", key.0, key.1); + } let mut compared = 0; let mut seen = Vec::new(); for (object, offset, expected) in &live.construct { @@ -197,8 +294,15 @@ fn the_constructors_run_in_creation_order_with_their_callees() { let built = gv100(&mut fixture, NO_FOREIGN).unwrap().unwrap(); assert_eq!( fixture.calls, - ["vector 430 0 10", "list 15c0 28", "vector 15e8 0 8"] + [ + "vector 430 0 10", + "list 15c0 28", + "vector 15e8 0 8", + "timer lock" + ] ); + assert_eq!(built.timer_construct.retry_times_2510(), 3); + assert_eq!(built.timer_construct.event_24f8, None); assert_eq!(built.states.intr.intr_3f5, 1); assert_eq!( built.states.bus.bus_668, @@ -227,6 +331,23 @@ fn a_declining_constructor_is_reported_and_is_not_a_state() { ); } +#[test] +fn a_timer_without_a_lock_declines_before_objbus() { + let mut fixture = Fixture { + no_timer_lock: true, + ..Fixture::default() + }; + let declined = gv100(&mut fixture, NO_FOREIGN).unwrap().err().unwrap(); + assert_eq!( + declined, + Declined { + class_id: crate::gpu::engine_bodies::OBJTMR, + status: NvStatus::INSUFFICIENT_RESOURCES, + } + ); + assert_eq!(fixture.calls.last().map(String::as_str), Some("timer lock")); +} + #[test] fn child_presence_follows_the_set_and_the_children_table() { let set = EngineSet::gv100(|descriptor| { diff --git a/src/nvidia/src/gpu/engine_live_support.rs b/src/nvidia/src/gpu/engine_live_support.rs index 29faac8d..210e3cbf 100644 --- a/src/nvidia/src/gpu/engine_live_support.rs +++ b/src/nvidia/src/gpu/engine_live_support.rs @@ -72,6 +72,9 @@ pub struct Block { pub struct Live { /// (object, offset) -> bytes at construction. pub construct: Vec<(String, u32, Vec)>, + /// (object, offset) -> pointer at construction: `null`, `+` + /// into the same object, or `outside`. + pub construct_pointers: Vec<(String, u32, String)>, /// Fields of other engines' objects; `None` when the object is absent. pub foreign: BTreeMap>>, /// GPU child offset -> pointer set. @@ -137,6 +140,11 @@ pub fn load() -> Live { let (object, offset) = place(rest[0]); live.construct.push((object, offset, bytes(rest[1]))); } + ("construct-pointer", _) => { + let (object, offset) = place(rest[0]); + live.construct_pointers + .push((object, offset, rest[1].to_string())); + } ("absent", _) => { live.absent = rest .iter() diff --git a/src/nvidia/src/gpu/timer.rs b/src/nvidia/src/gpu/timer.rs index af504775..613c6dae 100644 --- a/src/nvidia/src/gpu/timer.rs +++ b/src/nvidia/src/gpu/timer.rs @@ -10,6 +10,7 @@ pub const TIME_HI: u32 = 0x9410; pub const USER_TIME_LO: u32 = 0x810080; pub const USER_TIME_HI: u32 = 0x810084; pub mod alarm; +pub mod construct; pub mod delay; pub mod event; pub mod init; diff --git a/src/nvidia/src/gpu/timer/construct.rs b/src/nvidia/src/gpu/timer/construct.rs new file mode 100644 index 00000000..0900458f --- /dev/null +++ b/src/nvidia/src/gpu/timer/construct.rs @@ -0,0 +1,168 @@ +//! OBJTMR ConstructEngine `_nv059547rm` and the callback-pool initialiser +//! `_nv049710rm` it calls, as stores into the zeroed OBJTMR object. +//! +//! Evidence: reports/objtmr-construct-rea/ (disassembly of both functions +//! and their callees on nv-kernel.o_binary 580.178.04, and the fields the +//! original leaves in the object it constructs in emulation). +//! +//! The constructor, in instruction order: +//! +//! 1. byte +251c = 0; +//! 2. +2500 = the spinlock `_nv052744rm(_nv052691rm())` returns (the global +//! allocator, then a lock allocation and `os_alloc_spinlock`). It is +//! stored before the test, so a failed creation leaves +2500 null; the +//! constructor then prints `_nv047869rm(0xe75cd9b, 0x41 << 16)` and +//! returns 0x1a with nothing else stored; +//! 3. the alarm list heads +250 and +258 = null, the free-list head +260 = +//! &OBJTMR+268, and 96 entries of 0x50 bytes from +268: per entry +48 = +//! the next entry (null for the last), +30 = 0, +20 = 1; +//! 4. `_nv049710rm`: +2068 = 0, the free-list head +2070 = &OBJTMR+2078, +//! 36 entries of 0x20 bytes from +2078, per entry +18 = the next entry +//! (null for the last). When byte +24b is set it creates the event +//! stored at +24f8 (`_nv059559rm(OBJTMR, OBJTMR+24f8, callback, 0, 1)`), +//! printing and asserting on failure. `_nv049710rm` returns that status +//! (zero when +24b is clear); the constructor discards it; +//! 5. eight bytes +2510 = 3 (the retry count +2510 = 3 and the error count +//! +2514 = 0 of `timer::State`), eight bytes +2520 = 0, status 0. +use nv_rs_abi::NvStatus; + +/// First entry of the alarm-event pool and its free-list head. +pub const ALARM_POOL: u32 = 0x268; +pub const ALARM_FREE_HEAD: u32 = 0x260; +pub const ALARM_ENTRY_BYTES: u32 = 0x50; +pub const ALARM_ENTRIES: u32 = 96; +/// First entry of the callback pool `_nv049710rm` links, and its head. +pub const CALLBACK_POOL: u32 = 0x2078; +pub const CALLBACK_FREE_HEAD: u32 = 0x2070; +pub const CALLBACK_ENTRY_BYTES: u32 = 0x20; +pub const CALLBACK_ENTRIES: u32 = 36; +/// OBJTMR+2510 after construction (`timer::State::retry_times_2510`). +pub const RETRY_TIMES: u32 = 3; +/// Line of both diagnostics (`0x41 << 16` in the original's second +/// argument). +pub const DIAGNOSTIC_LINE: u32 = 0x41; + +pub trait ConstructServices { + /// `_nv052744rm(_nv052691rm())`: whether a spinlock was created. + fn timer_lock_create(&mut self) -> Result; + /// `_nv047869rm(0xe75cd9b, 0x41 << 16)`: the lock was not created. + fn timer_lock_unavailable(&mut self); + /// `_nv059559rm(OBJTMR, OBJTMR+24f8, .text+0x74bc0, 0, 1)`, reached only + /// when OBJTMR+24b is set. + fn timer_event_create_24f8(&mut self) -> Result; + /// `_nv047869rm(0xe6e1714, 0x41 << 16 | 0x1000, status)` then + /// `_nv046747rm(status, 0x41)`: the event creation failed. + fn timer_event_create_failed(&mut self, status: NvStatus); +} + +/// A field the constructor stores, as the bytes of the original object +/// would show it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Value { + Byte(u8), + /// Eight bytes; a null pointer is `Quad(0)`. + Quad(u64), + /// A pointer to OBJTMR+offset. + Object(u32), + /// The spinlock `_nv052744rm` returned, outside the object. + Lock, +} + +/// What the constructor did. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Constructed { + /// A spinlock was created. When not, only +251c and +2500 are stored. + pub lock: bool, + /// OBJTMR+24b as the property binder left it; zero on GV100 [E]. + pub tmr_24b: bool, + /// Executed status of the +24f8 event creation when `tmr_24b`. + pub event_24f8: Option, +} + +/// `_nv059547rm` on an object whose byte +24b is `tmr_24b`. The outer `Err` +/// is an unavailable service. +pub fn construct_engine( + services: &mut S, + tmr_24b: bool, +) -> Result<(Constructed, NvStatus), NvStatus> { + let lock = services.timer_lock_create()?; + let mut constructed = Constructed { + lock, + tmr_24b, + event_24f8: None, + }; + if !lock { + services.timer_lock_unavailable(); + return Ok((constructed, NvStatus::INSUFFICIENT_RESOURCES)); + } + // _nv049710rm; its status is discarded by the constructor. + if tmr_24b { + // An unavailable service here returns with the lock created and + // not released: this host model has no destructor, and the caller + // drops the whole construction on the outer `Err`. + let status = services.timer_event_create_24f8()?; + if status != NvStatus::OK { + services.timer_event_create_failed(status); + } + constructed.event_24f8 = Some(status); + } + Ok((constructed, NvStatus::OK)) +} + +impl Constructed { + /// Every store of the constructor and of `_nv049710rm`, in instruction + /// order, as (OBJTMR offset, value). The +24f8 event is written by + /// `_nv059559rm`, not here, and is not listed. + pub fn for_each_store(&self, mut store: impl FnMut(u32, Value)) { + store(0x251c, Value::Byte(0)); + if !self.lock { + store(0x2500, Value::Quad(0)); + return; + } + store(0x2500, Value::Lock); + store(0x250, Value::Quad(0)); + store(0x258, Value::Quad(0)); + store(ALARM_FREE_HEAD, Value::Object(ALARM_POOL)); + for index in 0..ALARM_ENTRIES { + let entry = ALARM_POOL + index * ALARM_ENTRY_BYTES; + store( + entry + 0x48, + if index + 1 < ALARM_ENTRIES { + Value::Object(entry + ALARM_ENTRY_BYTES) + } else { + Value::Quad(0) + }, + ); + store(entry + 0x30, Value::Byte(0)); + store(entry + 0x20, Value::Byte(1)); + } + store(0x2068, Value::Quad(0)); + store(CALLBACK_FREE_HEAD, Value::Object(CALLBACK_POOL)); + for index in 0..CALLBACK_ENTRIES { + let entry = CALLBACK_POOL + index * CALLBACK_ENTRY_BYTES; + store( + entry + 0x18, + if index + 1 < CALLBACK_ENTRIES { + Value::Object(entry + CALLBACK_ENTRY_BYTES) + } else { + Value::Quad(0) + }, + ); + } + store(0x2510, Value::Quad(u64::from(RETRY_TIMES))); + store(0x2520, Value::Quad(0)); + } + + /// OBJTMR+2510, the retry count of the time read, after construction. + pub const fn retry_times_2510(&self) -> u32 { + if self.lock { + RETRY_TIMES + } else { + 0 + } + } +} + +#[cfg(test)] +#[path = "construct_tests.rs"] +mod tests; diff --git a/src/nvidia/src/gpu/timer/construct_tests.rs b/src/nvidia/src/gpu/timer/construct_tests.rs new file mode 100644 index 00000000..2da8046a --- /dev/null +++ b/src/nvidia/src/gpu/timer/construct_tests.rs @@ -0,0 +1,170 @@ +extern crate std; +use super::*; +use std::{collections::BTreeMap, string::String, vec, vec::Vec}; + +struct Model { + lock: bool, + event: NvStatus, + unavailable: bool, + calls: Vec, +} + +impl Default for Model { + fn default() -> Self { + Self { + lock: false, + event: NvStatus::OK, + unavailable: false, + calls: Vec::new(), + } + } +} + +impl ConstructServices for Model { + fn timer_lock_create(&mut self) -> Result { + if self.unavailable { + return Err(NvStatus::NOT_SUPPORTED); + } + self.calls.push("lock".into()); + Ok(self.lock) + } + fn timer_lock_unavailable(&mut self) { + self.calls.push("print 41".into()); + } + fn timer_event_create_24f8(&mut self) -> Result { + self.calls.push("event 24f8".into()); + Ok(self.event) + } + fn timer_event_create_failed(&mut self, status: NvStatus) { + self.calls + .push(std::format!("print+assert 41 {:x}", status.0)); + } +} + +fn stores(built: &Constructed) -> Vec<(u32, Value)> { + let mut all = Vec::new(); + built.for_each_store(|offset, value| all.push((offset, value))); + all +} + +/// The GV100 path (OBJTMR+24b clear): every store of _nv059547rm and +/// _nv049710rm, from the disassembly [D]. +#[test] +fn the_gv100_constructor_stores_the_pools_and_the_retry_count() { + let mut model = Model { + lock: true, + ..Model::default() + }; + let (built, status) = construct_engine(&mut model, false).unwrap(); + assert_eq!(status, NvStatus::OK); + assert_eq!(model.calls, ["lock"]); + assert_eq!(built.retry_times_2510(), 3); + let all = stores(&built); + // 5 + 3 * 96 + 2 + 36 + 2 stores, no offset twice. + assert_eq!(all.len(), 333); + let by_offset: BTreeMap = all.iter().copied().collect(); + assert_eq!(by_offset.len(), all.len()); + assert_eq!( + &all[..5], + [ + (0x251c, Value::Byte(0)), + (0x2500, Value::Lock), + (0x250, Value::Quad(0)), + (0x258, Value::Quad(0)), + (0x260, Value::Object(0x268)), + ] + ); + // Loop of _nv059547rm: first entry, the one the loop ends on, and the + // three stores after the loop (+2060, +2048, +2038). + // In the loop each entry stores +48, then +30, then +20; the last entry + // is peeled after the loop in the same order. + assert_eq!( + &all[5..8], + [ + (0x2b0, Value::Object(0x2b8)), + (0x298, Value::Byte(0)), + (0x288, Value::Byte(1)), + ] + ); + for index in 0..ALARM_ENTRIES { + let entry = ALARM_POOL + index * ALARM_ENTRY_BYTES; + let at = 5 + 3 * index as usize; + let offsets: Vec = all[at..at + 3].iter().map(|(offset, _)| *offset).collect(); + assert_eq!(offsets, [entry + 0x48, entry + 0x30, entry + 0x20]); + } + assert_eq!( + &all[290..293], + [ + (0x2060, Value::Quad(0)), + (0x2048, Value::Byte(0)), + (0x2038, Value::Byte(1)), + ] + ); + assert_eq!(by_offset[&0x2b0], Value::Object(0x2b8)); + assert_eq!(by_offset[&0x298], Value::Byte(0)); + assert_eq!(by_offset[&0x288], Value::Byte(1)); + assert_eq!(by_offset[&0x2010], Value::Object(0x2018)); + assert_eq!(by_offset[&0x2060], Value::Quad(0)); + assert_eq!(by_offset[&0x2048], Value::Byte(0)); + assert_eq!(by_offset[&0x2038], Value::Byte(1)); + // _nv049710rm: +2068, head +2070, links to +24d8, null at +24f0. + assert_eq!(by_offset[&0x2068], Value::Quad(0)); + assert_eq!(by_offset[&0x2070], Value::Object(0x2078)); + assert_eq!(by_offset[&0x2090], Value::Object(0x2098)); + assert_eq!(by_offset[&0x24d0], Value::Object(0x24d8)); + assert_eq!(by_offset[&0x24f0], Value::Quad(0)); + assert!(!by_offset.contains_key(&0x24f8)); + assert_eq!( + &all[all.len() - 2..], + [(0x2510, Value::Quad(3)), (0x2520, Value::Quad(0))] + ); +} + +#[test] +fn a_missing_lock_stores_null_and_returns_insufficient_resources() { + let mut model = Model::default(); + let (built, status) = construct_engine(&mut model, true).unwrap(); + assert_eq!(status, NvStatus::INSUFFICIENT_RESOURCES); + // The event is never reached: the early return is before _nv049710rm. + assert_eq!(model.calls, ["lock", "print 41"]); + assert_eq!(built.event_24f8, None); + assert_eq!( + stores(&built), + vec![(0x251c, Value::Byte(0)), (0x2500, Value::Quad(0))] + ); + assert_eq!(built.retry_times_2510(), 0); +} + +#[test] +fn the_24f8_event_status_is_reported_and_ignored() { + for (event, calls) in [ + (NvStatus::OK, &["lock", "event 24f8"][..]), + ( + NvStatus(0x51), + &["lock", "event 24f8", "print+assert 41 51"][..], + ), + ] { + let mut model = Model { + lock: true, + event, + ..Model::default() + }; + let (built, status) = construct_engine(&mut model, true).unwrap(); + assert_eq!(status, NvStatus::OK); + assert_eq!(model.calls, calls); + assert_eq!(built.event_24f8, Some(event)); + assert_eq!(stores(&built).len(), 333); + } +} + +#[test] +fn an_unavailable_service_is_the_outer_error() { + let mut model = Model { + unavailable: true, + ..Model::default() + }; + assert_eq!( + construct_engine(&mut model, false), + Err(NvStatus::NOT_SUPPORTED) + ); +} diff --git a/tools/check_gpu_engine_live_binary.py b/tools/check_gpu_engine_live_binary.py index fab65f78..677d0231 100644 --- a/tools/check_gpu_engine_live_binary.py +++ b/tools/check_gpu_engine_live_binary.py @@ -17,7 +17,9 @@ Two things are written per case: 1. `construct` lines: the construction-time value of every field the Rust engine states (`gpu/engine_bodies.rs::States`, `gpu/engine_bodies_more.rs`) and their arguments hold, - read from the constructed objects before any pass runs. + read from the constructed objects before any pass runs. `construct-pointer` lines give a + pointer field the Rust constructor states (`gpu/timer/construct.rs`) as `null`, an offset + into its own object, or `outside`. 2. Per live callback, a `live` block: live @@ -503,6 +505,20 @@ FIELDS = ( ("Lpwr", 0x1208, 8), ("OBJINFOROM", 0x1f80, 1), ) +# OBJTMR ConstructEngine _nv059547rm with _nv049710rm (gpu/timer/construct.rs): the +# property byte it tests, the words it stores, and the two bytes of every alarm-pool entry. +FIELDS += ( + ("OBJTMR", 0x24b, 1), ("OBJTMR", 0x251c, 1), ("OBJTMR", 0x2510, 8), + ("OBJTMR", 0x2520, 8), ("OBJTMR", 0x2068, 8), +) + tuple(("OBJTMR", 0x268 + 0x50 * entry + field, 1) + for entry in range(96) for field in (0x20, 0x30)) +# Pointer fields it stores, written as `construct-pointer