Files
nvdriver/tests/vm/gpu_registry.rs
LIghtJUNction 32c8728037 Migrate the workspace and kernel crates to Rust edition 2024
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>
2026-12-02 01:52:38 +08:00

71 lines
2.1 KiB
Rust

//! Actual C probe publication and Rust lease lookup in the Linux guest.
use crate::{
gpu::GpuLease,
gpu_registry::{self, Key},
};
pub unsafe fn probe(
lease: &GpuLease,
out: *mut Key,
domain: u32,
bus: u32,
slot: u32,
function: u32,
physical: u64,
bytes: u64,
) -> i32 {
unsafe {
let mut checks = 0;
macro_rules! check {
($p:expr) => {
if !$p {
return -5;
}
checks += 1;
};
}
let key = match gpu_registry::key_for(lease) {
Ok(k) => k,
Err(_) => return -5,
};
checks += 1;
check!(key.index() < 32 && key.generation() != 0);
check!(gpu_registry::keys().iter().flatten().count() == 1);
let acquired = match gpu_registry::acquire(key) {
Ok(g) => g,
Err(_) => return -5,
};
checks += 1;
check!(gpu_registry::key_for(&acquired) == Ok(key));
check!(acquired.identity() == lease.identity());
let facts = match acquired.pci_identity() {
Ok(facts) => facts,
Err(_) => return -5,
};
check!(facts.domain == domain && u32::from(facts.bus) == bus);
check!(u32::from(facts.slot) == slot && u32::from(facts.function) == function);
check!(!facts.is_virtfn);
check!(facts.gpu_id() == ((domain & 0xffff) << 16 | bus << 8 | slot));
let mapping = match acquired.bar0_mapping() {
Ok(mapping) => mapping,
Err(_) => return -5,
};
check!(mapping.physical == physical && mapping.bytes == bytes);
check!(mapping.kernel_base.get() != 0);
check!(!acquired.hardware_ready());
check!(gpu_registry::acquire(Key::invalid()).is_err());
core::ptr::write(out, key);
checks
}
}
pub unsafe fn removed(key: Key) -> i32 {
unsafe {
if gpu_registry::acquire(key).is_ok() {
return -5;
}
if !gpu_registry::keys().iter().all(Option::is_none) {
return -5;
}
2
}
}