mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
Mechanical, behavior-neutral: edition/resolver keys, unsafe extern blocks, unsafe(no_mangle)/unsafe(export_name), unsafe blocks inside unsafe fn, r#gen renames, --edition flags in Makefiles/tools, rustfmt style_edition pinned to 2021 so formatting stays edition-independent, and clippy collapsible_if allowed workspace-wide (let-chain churn). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
391 lines
13 KiB
Rust
391 lines
13 KiB
Rust
#![allow(dead_code)]
|
|
#[path = "../../src/nvidia/kernel/thread.rs"]
|
|
mod thread;
|
|
#[path = "../../src/nvidia/kernel/thread_state.rs"]
|
|
mod thread_state;
|
|
use nv_rs_abi::NvStatus;
|
|
use nv_rs_core::gpu::{
|
|
thread_timeout::{Database, Node, Services},
|
|
timeout::OS_TIMER,
|
|
};
|
|
use std::{
|
|
num::NonZeroU32,
|
|
sync::{Arc, Barrier, Mutex},
|
|
};
|
|
use thread_state::Registry;
|
|
unsafe extern "C" {
|
|
fn fixture_context(pid: u64, cpu: u32, context: u32, flags: u32);
|
|
fn fixture_fail(allocation: bool, module: bool);
|
|
fn fixture_outstanding() -> u64;
|
|
}
|
|
fn context(pid: u64, cpu: u32, flags: u32, process: u32) {
|
|
unsafe {
|
|
fixture_context(pid, cpu, flags, process);
|
|
fixture_fail(false, false);
|
|
}
|
|
}
|
|
struct Clock(u64);
|
|
impl Services for Clock {
|
|
fn monotonic_ns(&mut self) -> Result<u64, NvStatus> {
|
|
let n = self.0;
|
|
self.0 += 1;
|
|
Ok(n)
|
|
}
|
|
fn delay_us(&mut self, _: u32) -> Result<(), NvStatus> {
|
|
panic!("unexpected delay")
|
|
}
|
|
fn print_node(&mut self, _: &Node) -> Result<(), NvStatus> {
|
|
panic!("unexpected print")
|
|
}
|
|
fn log_configured_timeout(&mut self, _: u64) -> Result<(), NvStatus> {
|
|
panic!("unexpected timeout")
|
|
}
|
|
fn assert_timeout(&mut self) -> Result<(), NvStatus> {
|
|
panic!("unexpected assert")
|
|
}
|
|
fn schedule_cpu(&mut self) -> Result<NvStatus, NvStatus> {
|
|
panic!("unexpected schedule")
|
|
}
|
|
fn log_yield(&mut self) -> Result<(), NvStatus> {
|
|
panic!("unexpected yield log")
|
|
}
|
|
}
|
|
struct MissingClock;
|
|
impl Services for MissingClock {
|
|
fn monotonic_ns(&mut self) -> Result<u64, NvStatus> {
|
|
Err(NvStatus::NOT_SUPPORTED)
|
|
}
|
|
fn delay_us(&mut self, _: u32) -> Result<(), NvStatus> {
|
|
panic!("unexpected delay")
|
|
}
|
|
fn print_node(&mut self, _: &Node) -> Result<(), NvStatus> {
|
|
panic!("unexpected print")
|
|
}
|
|
fn log_configured_timeout(&mut self, _: u64) -> Result<(), NvStatus> {
|
|
panic!("unexpected timeout")
|
|
}
|
|
fn assert_timeout(&mut self) -> Result<(), NvStatus> {
|
|
panic!("unexpected assert")
|
|
}
|
|
fn schedule_cpu(&mut self) -> Result<NvStatus, NvStatus> {
|
|
panic!("unexpected schedule")
|
|
}
|
|
fn log_yield(&mut self) -> Result<(), NvStatus> {
|
|
panic!("unexpected log")
|
|
}
|
|
}
|
|
fn database(mode: u32) -> Database {
|
|
Database {
|
|
setup_flags: 3,
|
|
timeout_flags: mode,
|
|
compute_ms: 17,
|
|
non_compute_ms: 13,
|
|
compute_gpu_mask: 0,
|
|
}
|
|
}
|
|
struct Case {
|
|
flags: u32,
|
|
mode: u32,
|
|
failure: u8,
|
|
sequence: u32,
|
|
trace: usize,
|
|
status: u32,
|
|
node_flags: u32,
|
|
timeout: [u64; 5],
|
|
}
|
|
include!("original_vectors.rs");
|
|
|
|
#[test]
|
|
fn original_owned_registration_fields_and_partial_failure_order() {
|
|
assert_eq!(ORIGINAL.len(), 108);
|
|
for case in ORIGINAL {
|
|
context(0x9876, 2, 0, 0);
|
|
let registry = Registry::new(database(case.mode), NonZeroU32::new(4).unwrap()).unwrap();
|
|
let mut existing = if case.failure == 2 {
|
|
Some(unsafe { registry.enter(0, 0, &mut Clock(0)).unwrap().unwrap() })
|
|
} else {
|
|
None
|
|
};
|
|
registry.oracle_seed(case.sequence, case.trace);
|
|
context(0x9876, if case.failure == 1 { 4 } else { 2 }, 0, 0);
|
|
{
|
|
let result = unsafe { registry.enter(case.flags, 0x60000000, &mut Clock(100)) };
|
|
let (trace, index) = registry.trace();
|
|
match result {
|
|
Ok(Some(entry)) => {
|
|
assert_eq!(case.status, 0);
|
|
let snapshot = entry.snapshot().unwrap();
|
|
assert_eq!(
|
|
(
|
|
snapshot.thread_id,
|
|
snapshot.sequence,
|
|
snapshot.cpu,
|
|
snapshot.flags
|
|
),
|
|
(0x9876, case.sequence.wrapping_add(1), 2, case.node_flags)
|
|
);
|
|
let t = entry.timeout_snapshot();
|
|
assert_eq!(
|
|
[
|
|
t.enter_ns,
|
|
t.non_compute,
|
|
t.compute,
|
|
t.next_yield_ns,
|
|
t.override_ms
|
|
],
|
|
case.timeout
|
|
);
|
|
assert_eq!(t.flags, case.node_flags);
|
|
assert_eq!(index, (case.trace + 1) & 7);
|
|
assert_eq!(
|
|
(trace[case.trace].caller, trace[case.trace].flags),
|
|
(0x60000000, case.node_flags)
|
|
);
|
|
drop(entry);
|
|
assert_eq!(registry.current(), Err(NvStatus(0x57)));
|
|
}
|
|
Err(status) => {
|
|
assert_eq!(status.0, case.status);
|
|
assert_eq!(index, case.trace);
|
|
}
|
|
Ok(None) => panic!("tracking enabled"),
|
|
}
|
|
}
|
|
context(0x9876, 2, 0, 0);
|
|
if let Some(entry) = existing.take() {
|
|
assert!(entry.snapshot().is_ok());
|
|
drop(entry);
|
|
}
|
|
drop(existing);
|
|
drop(registry);
|
|
assert_eq!(unsafe { fixture_outstanding() }, 0);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn dynamic_callbacks_failures_task_identity_and_actual_lookup() {
|
|
context(0x9876, 2, 0, 3);
|
|
let registry = Registry::new(database(OS_TIMER), NonZeroU32::new(4).unwrap()).unwrap();
|
|
assert_eq!(registry.current(), Err(NvStatus(0x57)));
|
|
unsafe { fixture_fail(true, false) };
|
|
assert!(matches!(
|
|
unsafe { registry.enter(0, 1, &mut Clock(10)) },
|
|
Err(NvStatus(0x51))
|
|
));
|
|
unsafe { fixture_fail(false, true) };
|
|
assert!(matches!(
|
|
unsafe { registry.enter(0, 1, &mut Clock(10)) },
|
|
Err(NvStatus(0x40))
|
|
));
|
|
unsafe { fixture_fail(false, false) };
|
|
assert!(matches!(
|
|
unsafe { registry.enter(0, 1, &mut MissingClock) },
|
|
Err(NvStatus::NOT_SUPPORTED)
|
|
));
|
|
assert_eq!(registry.current(), Err(NvStatus(0x57)));
|
|
let mut entry = unsafe { registry.enter(0, 1, &mut Clock(10)).unwrap().unwrap() };
|
|
assert_eq!(entry.snapshot().unwrap().flags, 0x720);
|
|
assert_eq!(
|
|
entry.check_timeout(&mut Clock(20), None, None),
|
|
Ok(NvStatus(0x40))
|
|
);
|
|
assert_eq!(entry.reset_timeout(&mut Clock(30)), Ok(NvStatus::OK));
|
|
assert_eq!(entry.yield_cpu(&mut Clock(31), true), Ok(()));
|
|
context(99, 2, 0, 0);
|
|
assert!(matches!(entry.snapshot(), Err(NvStatus(0x40))));
|
|
for flags in [2, 4, 8] {
|
|
context(0x9876, 2, flags, 0);
|
|
assert!(matches!(entry.snapshot(), Err(NvStatus(0x40))));
|
|
assert!(matches!(
|
|
unsafe { registry.enter(0, 1, &mut Clock(0)) },
|
|
Err(NvStatus(0x40))
|
|
));
|
|
}
|
|
context(0x9876, 2, 0, 0);
|
|
let events = Arc::new(Mutex::new(Vec::new()));
|
|
let first = events.clone();
|
|
let registry_address = ®istry as *const Registry as usize;
|
|
// The callback owns its resources; registry outlives this Entry. Raw address
|
|
// is used only to assert original callback-before-unlink ordering here.
|
|
let old_id = entry
|
|
.on_free(move || {
|
|
let registry = unsafe { &*(registry_address as *const Registry) };
|
|
assert!(registry.current().is_ok());
|
|
first.lock().unwrap().push(1);
|
|
})
|
|
.ok()
|
|
.unwrap();
|
|
let cancelled = events.clone();
|
|
let id = entry
|
|
.on_free(move || cancelled.lock().unwrap().push(99))
|
|
.ok()
|
|
.unwrap();
|
|
let third = events.clone();
|
|
entry
|
|
.on_free(move || third.lock().unwrap().push(3))
|
|
.ok()
|
|
.unwrap();
|
|
assert_eq!(entry.cancel_callback(id), Ok(true));
|
|
assert_eq!(entry.cancel_callback(id), Ok(false));
|
|
unsafe { fixture_fail(true, false) };
|
|
let payload = events.clone();
|
|
let rejected = entry.on_free(move || payload.lock().unwrap().push(100));
|
|
assert!(matches!(rejected, Err((NvStatus(0x51), _))));
|
|
drop(rejected);
|
|
drop(entry);
|
|
assert_eq!(*events.lock().unwrap(), vec![1, 3]);
|
|
assert_eq!(registry.current(), Err(NvStatus(0x57)));
|
|
let mut next = unsafe { registry.enter(0, 2, &mut Clock(10)).unwrap().unwrap() };
|
|
let head = next
|
|
.on_free(|| -> () { panic!("cancelled head") })
|
|
.ok()
|
|
.unwrap();
|
|
let tail = next
|
|
.on_free(|| -> () { panic!("cancelled tail") })
|
|
.ok()
|
|
.unwrap();
|
|
assert_eq!(next.cancel_callback(old_id), Ok(false));
|
|
assert_eq!(next.cancel_callback(head), Ok(true));
|
|
assert_eq!(next.cancel_callback(tail), Ok(true));
|
|
drop(next);
|
|
drop(registry);
|
|
assert_eq!(unsafe { fixture_outstanding() }, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn multiple_actual_host_threads_do_not_alias_nodes_and_unlink_safely() {
|
|
context(0x9876, 2, 0, 0);
|
|
let registry =
|
|
Arc::new(Registry::new(database(OS_TIMER), NonZeroU32::new(4).unwrap()).unwrap());
|
|
let barrier = Arc::new(Barrier::new(8));
|
|
let threads: Vec<_> = (0..8)
|
|
.map(|i| {
|
|
let registry = registry.clone();
|
|
let barrier = barrier.clone();
|
|
std::thread::spawn(move || {
|
|
context(100 + i, 2, 0, 0);
|
|
let mut entry = unsafe { registry.enter(0, i, &mut Clock(0)).unwrap().unwrap() };
|
|
barrier.wait();
|
|
for _ in 0..300 {
|
|
assert_eq!(entry.snapshot().unwrap().thread_id, 100 + i);
|
|
assert_eq!(entry.reset_timeout(&mut Clock(1)), Ok(NvStatus::OK));
|
|
}
|
|
drop(entry);
|
|
})
|
|
})
|
|
.collect();
|
|
for t in threads {
|
|
t.join().unwrap();
|
|
}
|
|
assert_eq!(registry.current(), Err(NvStatus(0x57)));
|
|
drop(registry);
|
|
assert_eq!(unsafe { fixture_outstanding() }, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn disabled_and_unimplemented_modes_do_not_manufacture_nodes() {
|
|
context(0x9876, 2, 0, 0);
|
|
let mut db = database(OS_TIMER);
|
|
db.setup_flags = 0;
|
|
let registry = Registry::new(db, NonZeroU32::new(4).unwrap()).unwrap();
|
|
assert!(unsafe { registry.enter(0, 0, &mut Clock(0)).unwrap().is_none() });
|
|
assert!(matches!(
|
|
unsafe { registry.enter(1, 0, &mut Clock(0)) },
|
|
Err(NvStatus::NOT_SUPPORTED)
|
|
));
|
|
assert_eq!(registry.current(), Err(NvStatus(0x40)));
|
|
drop(registry);
|
|
for flags in [8, 32, 1 << 31] {
|
|
let mut db = database(OS_TIMER);
|
|
db.setup_flags |= flags;
|
|
assert!(matches!(
|
|
Registry::new(db, NonZeroU32::new(4).unwrap()),
|
|
Err(NvStatus::NOT_SUPPORTED)
|
|
));
|
|
}
|
|
assert_eq!(unsafe { fixture_outstanding() }, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn owned_current_entry_uses_published_compute_configuration_after_explicit_reset() {
|
|
use nv_rs_core::gpu::timeout::{config, GpuId};
|
|
struct Policy(i32);
|
|
impl config::Hypervisor for Policy {
|
|
fn is_vgx(&mut self) -> Result<bool, NvStatus> {
|
|
Ok(false)
|
|
} // explicit native-host test input
|
|
}
|
|
impl config::OsParams for Policy {
|
|
fn params(&mut self, _: GpuId) -> Result<config::Params, NvStatus> {
|
|
config::linux_params(
|
|
config::LinuxPolicy {
|
|
compute_refs: self.0,
|
|
gsp_client: false,
|
|
extended_gsp_init_timeout: false,
|
|
gsp_initialized: false,
|
|
emulation: false,
|
|
simulation: false,
|
|
},
|
|
self,
|
|
)
|
|
}
|
|
}
|
|
context(0x8888, 0, 0, 0);
|
|
let mut db = database(OS_TIMER);
|
|
db.non_compute_ms = 4000;
|
|
db.compute_ms = 4000;
|
|
let registry = Registry::new(db, NonZeroU32::new(4).unwrap()).unwrap();
|
|
let mut entry = unsafe { registry.enter(0, 0, &mut Clock(100)).unwrap().unwrap() };
|
|
let original = entry.timeout_snapshot();
|
|
let mut changed = registry.timeout_database();
|
|
config::configure_thread(
|
|
&mut changed,
|
|
&mut Policy(1),
|
|
GpuId(57),
|
|
config::ThreadPolicy {
|
|
instance: 0,
|
|
compute_refs: 1,
|
|
scale: 1,
|
|
},
|
|
30_000_000,
|
|
OS_TIMER,
|
|
)
|
|
.unwrap();
|
|
registry.replace_timeout_database(changed).unwrap();
|
|
assert_eq!(entry.timeout_snapshot(), original); // publication is not an implicit reset
|
|
assert_eq!(entry.reset_timeout(&mut Clock(1000)), Ok(NvStatus::OK));
|
|
assert_eq!(entry.timeout_snapshot().compute, 30_000_001_000);
|
|
assert_eq!(entry.timeout_snapshot().non_compute, 4_000_001_000);
|
|
assert_eq!(entry.timeout_snapshot().enter_ns, original.enter_ns);
|
|
let mut changed = registry.timeout_database();
|
|
config::configure_thread(
|
|
&mut changed,
|
|
&mut Policy(0),
|
|
GpuId(57),
|
|
config::ThreadPolicy {
|
|
instance: 0,
|
|
compute_refs: 0,
|
|
scale: 1,
|
|
},
|
|
4_000_000,
|
|
OS_TIMER,
|
|
)
|
|
.unwrap();
|
|
registry.replace_timeout_database(changed).unwrap();
|
|
assert_eq!(entry.reset_timeout(&mut Clock(2000)), Ok(NvStatus::OK));
|
|
assert_eq!(entry.timeout_snapshot().compute, 4_000_002_000);
|
|
assert_eq!(registry.timeout_database().compute_gpu_mask & 1, 0);
|
|
let current = registry.timeout_database();
|
|
let mut unsupported = current;
|
|
unsupported.setup_flags |= 8;
|
|
assert_eq!(
|
|
registry.replace_timeout_database(unsupported),
|
|
Err(NvStatus::NOT_SUPPORTED)
|
|
);
|
|
assert_eq!(registry.timeout_database(), current);
|
|
assert!(entry.snapshot().is_ok());
|
|
drop(entry);
|
|
drop(registry);
|
|
assert_eq!(unsafe { fixture_outstanding() }, 0);
|
|
}
|