mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
1021 lines
31 KiB
Rust
1021 lines
31 KiB
Rust
//! Guest-only actual Linux DMA + Rust MMU construction test. No GPU root install.
|
|
#![no_std]
|
|
#![allow(dead_code)]
|
|
#[path = "../../src/nvidia/kernel/acr_bringup.rs"]
|
|
mod acr_bringup;
|
|
#[path = "../../src/nvidia/kernel/adapter_start.rs"]
|
|
mod adapter_start;
|
|
#[path = "../../src/nvidia/kernel/bar1_bringup.rs"]
|
|
mod bar1_bringup;
|
|
#[path = "../../src/nvidia/kernel/clock.rs"]
|
|
mod clock;
|
|
#[path = "../../src/nvidia/kernel/device_software.rs"]
|
|
mod device_software;
|
|
#[path = "../../src/nvidia/kernel/dma.rs"]
|
|
mod dma;
|
|
#[path = "../../src/nvidia/kernel/fifo_bringup.rs"]
|
|
mod fifo_bringup;
|
|
#[path = "../../src/nvidia/kernel/firmware_pack.rs"]
|
|
mod firmware_pack;
|
|
#[path = "../../src/nvidia/kernel/gpu.rs"]
|
|
mod gpu;
|
|
#[path = "../../src/nvidia/kernel/gpu_registry.rs"]
|
|
mod gpu_registry;
|
|
#[path = "gpu_registry.rs"]
|
|
mod gpu_registry_test;
|
|
#[path = "../../src/nvidia/kernel/gr_bringup.rs"]
|
|
mod gr_bringup;
|
|
#[path = "../../src/nvidia/kernel/gsp_bringup.rs"]
|
|
mod gsp_bringup;
|
|
#[path = "../../src/nvidia/kernel/hypervisor.rs"]
|
|
mod hypervisor;
|
|
#[path = "../../src/nvidia/kernel/instance.rs"]
|
|
mod instance;
|
|
#[path = "../../src/nvidia/kernel/irq.rs"]
|
|
mod irq;
|
|
#[path = "../../src/nvidia/kernel/kernel_vec.rs"]
|
|
mod kernel_vec;
|
|
#[path = "../../src/nvidia/kernel/nano_timer.rs"]
|
|
mod nano_timer;
|
|
#[path = "nano_timer.rs"]
|
|
mod nano_timer_test;
|
|
#[path = "../../src/nvidia/kernel/native_program.rs"]
|
|
mod native_program;
|
|
#[path = "../../src/nvidia/kernel/nvdec_bringup.rs"]
|
|
mod nvdec_bringup;
|
|
#[path = "../../src/nvidia/kernel/page_tables.rs"]
|
|
mod page_tables;
|
|
#[path = "../../src/nvidia/kernel/pci.rs"]
|
|
mod pci;
|
|
#[path = "../../src/nvidia/kernel/pf_software.rs"]
|
|
mod pf_software;
|
|
#[path = "../../src/nvidia/kernel/pmu_bringup.rs"]
|
|
mod pmu_bringup;
|
|
#[path = "../../src/nvidia/kernel/protocol.rs"]
|
|
mod protocol;
|
|
#[path = "../../src/nvidia/kernel/registry.rs"]
|
|
mod registry;
|
|
#[path = "../../src/nvidia/kernel/rmapi_node.rs"]
|
|
mod rmapi_node;
|
|
#[path = "../../src/nvidia/kernel/runtime_threads.rs"]
|
|
mod runtime_threads;
|
|
#[path = "../../src/nvidia/kernel/scheduling.rs"]
|
|
mod scheduling;
|
|
#[path = "../../src/nvidia/kernel/small_vm.rs"]
|
|
mod small_vm;
|
|
#[path = "small_vm.rs"]
|
|
mod small_vm_test;
|
|
#[path = "../../src/nvidia/kernel/software_manager.rs"]
|
|
mod software_manager;
|
|
#[path = "../../src/nvidia/kernel/system_memory.rs"]
|
|
mod system_memory;
|
|
#[path = "../../src/nvidia/kernel/system_memory_ffi.rs"]
|
|
mod system_memory_ffi;
|
|
#[path = "../../src/nvidia/kernel/system_os.rs"]
|
|
mod system_os;
|
|
#[path = "../../src/nvidia/kernel/system_platform.rs"]
|
|
mod system_platform;
|
|
#[path = "../../src/nvidia/kernel/thread.rs"]
|
|
mod thread;
|
|
#[path = "../../src/nvidia/kernel/thread_state.rs"]
|
|
mod thread_state;
|
|
#[path = "../../src/nvidia/kernel/timeout_runtime.rs"]
|
|
mod timeout_runtime;
|
|
#[path = "../../src/nvidia/kernel/timer_event.rs"]
|
|
mod timer_event;
|
|
#[path = "timer_event.rs"]
|
|
mod timer_event_test;
|
|
use core::{
|
|
ffi::c_void,
|
|
mem::{self, MaybeUninit},
|
|
ptr,
|
|
};
|
|
use dma::LinuxDma;
|
|
use nv_rs_core::{
|
|
dma::{Constraints, Manager},
|
|
mmu::{Aperture, Architecture, Protection, Pte},
|
|
};
|
|
use page_tables::UnpublishedRoot;
|
|
type Root = UnpublishedRoot<8>;
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_native_memory(lease: *const gpu::GpuLease, pci: *mut c_void) -> i32 {
|
|
unsafe { small_vm_test::run(&*lease, pci) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_registry_probe(
|
|
lease: *const gpu::GpuLease,
|
|
out: *mut gpu_registry::Key,
|
|
domain: u32,
|
|
bus: u32,
|
|
slot: u32,
|
|
function: u32,
|
|
physical: u64,
|
|
bytes: u64,
|
|
) -> i32 {
|
|
unsafe { gpu_registry_test::probe(&*lease, out, domain, bus, slot, function, physical, bytes) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_registry_removed(key: *const gpu_registry::Key) -> i32 {
|
|
unsafe { gpu_registry_test::removed(ptr::read(key)) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_os_event(lease: *const gpu::GpuLease) -> i32 {
|
|
unsafe { timer_event_test::run(&*lease) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_nano_timer(lease: *const gpu::GpuLease) -> i32 {
|
|
unsafe { nano_timer_test::run(&*lease) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_nano_size() -> usize {
|
|
mem::size_of::<nano_timer_test::Timer>()
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_nano_hold(
|
|
lease: *const gpu::GpuLease,
|
|
storage: *mut nano_timer_test::Timer,
|
|
) -> i32 {
|
|
unsafe { nano_timer_test::hold(&*lease, storage) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_nano_removed(storage: *mut nano_timer_test::Timer) -> i32 {
|
|
unsafe { nano_timer_test::removed(storage) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_gpu_lease_size() -> usize {
|
|
mem::size_of::<gpu::GpuLease>()
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_lease_acquire(
|
|
state: *mut gpu::KernelGpu,
|
|
storage: *mut gpu::GpuLease,
|
|
) -> i32 {
|
|
unsafe {
|
|
if storage.is_null() || !(storage as usize).is_multiple_of(mem::align_of::<gpu::GpuLease>())
|
|
{
|
|
return -22;
|
|
}
|
|
match gpu::GpuLease::acquire_pinned(state) {
|
|
Ok(lease) => {
|
|
ptr::write(storage, lease);
|
|
0
|
|
}
|
|
Err(error) => error,
|
|
}
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_lease_ready(storage: *const gpu::GpuLease) -> bool {
|
|
unsafe { (&*storage).hardware_ready() }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_lease_drop(storage: *mut gpu::GpuLease) {
|
|
unsafe {
|
|
ptr::drop_in_place(storage);
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_gpu_registers_size() -> usize {
|
|
mem::size_of::<gpu::GpuRegisters>()
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_registers_acquire(
|
|
lease: *const gpu::GpuLease,
|
|
storage: *mut gpu::GpuRegisters,
|
|
) -> i32 {
|
|
unsafe {
|
|
if storage.is_null()
|
|
|| !(storage as usize).is_multiple_of(mem::align_of::<gpu::GpuRegisters>())
|
|
{
|
|
return -22;
|
|
}
|
|
match (&*lease).register_io() {
|
|
Ok(registers) => {
|
|
ptr::write(storage, registers);
|
|
0
|
|
}
|
|
Err(error) => error,
|
|
}
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_registers_read(
|
|
storage: *mut gpu::GpuRegisters,
|
|
offset: u32,
|
|
output: *mut u32,
|
|
) -> i32 {
|
|
unsafe {
|
|
ptr::write(output, 0);
|
|
match (&mut *storage).read(offset) {
|
|
Ok(value) => {
|
|
ptr::write(output, value);
|
|
0
|
|
}
|
|
Err(error) => error,
|
|
}
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_registers_write(
|
|
storage: *mut gpu::GpuRegisters,
|
|
offset: u32,
|
|
value: u32,
|
|
) -> i32 {
|
|
unsafe { (&mut *storage).write(offset, value).err().unwrap_or(0) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_registers_drop(storage: *mut gpu::GpuRegisters) {
|
|
unsafe {
|
|
ptr::drop_in_place(storage);
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_gpu_locked_size() -> usize {
|
|
mem::size_of::<gpu::LockedGpuRegisters>()
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_locked_acquire(
|
|
lease: *const gpu::GpuLease,
|
|
storage: *mut gpu::LockedGpuRegisters,
|
|
) -> i32 {
|
|
unsafe {
|
|
match (&*lease).lock_register_io() {
|
|
Ok(value) => {
|
|
ptr::write(storage, value);
|
|
0
|
|
}
|
|
Err(error) => error,
|
|
}
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_cold_msi_config(
|
|
storage: *mut gpu::LockedGpuRegisters,
|
|
expected: u32,
|
|
) -> i32 {
|
|
unsafe {
|
|
(&mut *storage)
|
|
.with_power(|guard| {
|
|
let policy = irq::ColdPciPolicy::resolve(guard)?;
|
|
if policy.msi_config() != expected {
|
|
return Err(-22);
|
|
}
|
|
Ok(())
|
|
})
|
|
.and_then(|result| result)
|
|
.err()
|
|
.unwrap_or(0)
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_locked_read(
|
|
storage: *mut gpu::LockedGpuRegisters,
|
|
offset: u32,
|
|
output: *mut u32,
|
|
) -> i32 {
|
|
unsafe {
|
|
ptr::write(output, 0);
|
|
match (&mut *storage).read(offset) {
|
|
Ok(value) => {
|
|
ptr::write(output, value);
|
|
0
|
|
}
|
|
Err(error) => error,
|
|
}
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_locked_write(
|
|
storage: *mut gpu::LockedGpuRegisters,
|
|
offset: u32,
|
|
value: u32,
|
|
) -> i32 {
|
|
unsafe { (&mut *storage).write(offset, value).err().unwrap_or(0) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_locked_drop(storage: *mut gpu::LockedGpuRegisters) {
|
|
unsafe {
|
|
ptr::drop_in_place(storage);
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_gpu_waited_acquire(state: *mut gpu::KernelGpu) -> i32 {
|
|
unsafe {
|
|
// Fixture holds the unpublished driver base until acquisition has reached
|
|
// its operation-lock callback. This lease is created/dropped on this task.
|
|
let lease = match gpu::GpuLease::acquire_pinned(state) {
|
|
Ok(value) => value,
|
|
Err(error) => return error,
|
|
};
|
|
match lease.lock_register_io() {
|
|
Ok(guard) => {
|
|
drop(guard);
|
|
0
|
|
}
|
|
Err(error) => error,
|
|
}
|
|
}
|
|
}
|
|
struct MissingDiagnostics;
|
|
static THREAD_CALLBACK_ORDER: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_thread_state() -> i32 {
|
|
use core::{num::NonZeroU32, sync::atomic::Ordering};
|
|
use nv_rs_abi::NvStatus;
|
|
use nv_rs_core::gpu::{thread_timeout as t, timeout};
|
|
let registry = match thread_state::Registry::new(
|
|
t::Database {
|
|
setup_flags: 3,
|
|
timeout_flags: timeout::OS_TIMER,
|
|
compute_ms: 1000,
|
|
non_compute_ms: 1000,
|
|
compute_gpu_mask: 0,
|
|
},
|
|
NonZeroU32::new(4).unwrap(),
|
|
) {
|
|
Ok(value) => value,
|
|
Err(_) => return -5,
|
|
};
|
|
if registry.current() != Err(NvStatus(0x57)) {
|
|
return -5;
|
|
}
|
|
let mut services = scheduling::ThreadServices::new(MissingDiagnostics);
|
|
let mut entry =
|
|
match unsafe { registry.enter(0, vm_thread_state as *const () as u64, &mut services) } {
|
|
Ok(Some(entry)) => entry,
|
|
_ => return -5,
|
|
};
|
|
let current = match thread::current() {
|
|
Ok(value) => value,
|
|
Err(_) => return -5,
|
|
};
|
|
let snapshot = match entry.snapshot() {
|
|
Ok(value) => value,
|
|
Err(_) => return -5,
|
|
};
|
|
if snapshot.thread_id != current.thread_id || snapshot.thread_id == 0 {
|
|
return -5;
|
|
}
|
|
if snapshot.cpu >= 4 || snapshot.flags & 0x120 != 0x120 {
|
|
return -5;
|
|
}
|
|
if !matches!(
|
|
unsafe { registry.enter(0, 0, &mut services) },
|
|
Err(NvStatus(0xffff))
|
|
) || entry.snapshot() != Ok(snapshot)
|
|
{
|
|
return -5;
|
|
}
|
|
THREAD_CALLBACK_ORDER.store(0, Ordering::SeqCst);
|
|
if entry
|
|
.on_free(|| THREAD_CALLBACK_ORDER.store(1, Ordering::SeqCst))
|
|
.is_err()
|
|
{
|
|
return -5;
|
|
}
|
|
let cancelled = match entry.on_free(|| THREAD_CALLBACK_ORDER.store(99, Ordering::SeqCst)) {
|
|
Ok(id) => id,
|
|
Err(_) => return -5,
|
|
};
|
|
if entry.cancel_callback(cancelled) != Ok(true) {
|
|
return -5;
|
|
}
|
|
if entry
|
|
.on_free(|| {
|
|
let next = if THREAD_CALLBACK_ORDER.load(Ordering::SeqCst) == 1 {
|
|
3
|
|
} else {
|
|
999
|
|
};
|
|
THREAD_CALLBACK_ORDER.store(next, Ordering::SeqCst);
|
|
})
|
|
.is_err()
|
|
{
|
|
return -5;
|
|
}
|
|
if entry.reset_timeout(&mut services) != Ok(NvStatus::OK) {
|
|
return -5;
|
|
}
|
|
if entry.check_timeout(&mut services, None, None) != Ok(NvStatus(0x40)) {
|
|
return -5;
|
|
}
|
|
if entry.yield_cpu(&mut services, true) != Ok(()) {
|
|
return -5;
|
|
}
|
|
drop(entry);
|
|
if THREAD_CALLBACK_ORDER.load(Ordering::SeqCst) != 3 {
|
|
return -5;
|
|
}
|
|
if registry.current() != Err(NvStatus(0x57)) {
|
|
return -5;
|
|
}
|
|
12
|
|
}
|
|
impl scheduling::Diagnostics for MissingDiagnostics {
|
|
fn print_node(
|
|
&mut self,
|
|
_: &nv_rs_core::gpu::thread_timeout::Node,
|
|
) -> Result<(), nv_rs_abi::NvStatus> {
|
|
Err(nv_rs_abi::NvStatus::NOT_SUPPORTED)
|
|
}
|
|
fn configured_timeout(&mut self, _: u64) -> Result<(), nv_rs_abi::NvStatus> {
|
|
Err(nv_rs_abi::NvStatus::NOT_SUPPORTED)
|
|
}
|
|
fn assert_timeout(&mut self) -> Result<(), nv_rs_abi::NvStatus> {
|
|
Err(nv_rs_abi::NvStatus::NOT_SUPPORTED)
|
|
}
|
|
fn log_yield(&mut self) -> Result<(), nv_rs_abi::NvStatus> {
|
|
Err(nv_rs_abi::NvStatus::NOT_SUPPORTED)
|
|
}
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_host_services() -> i32 {
|
|
use nv_rs_abi::NvStatus;
|
|
use nv_rs_core::gpu::{thread_timeout as t, timeout};
|
|
let mut os = scheduling::LinuxScheduling;
|
|
if os.delay_us(0) != Ok(NvStatus::OK) {
|
|
return -5;
|
|
}
|
|
if os.delay_ns_wrapper(0) != Ok(NvStatus::OK) {
|
|
return -5;
|
|
}
|
|
if os.delay_us(1001) != Ok(NvStatus::OK) {
|
|
return -5;
|
|
}
|
|
if os.schedule_cpu() != Ok(NvStatus::OK) {
|
|
return -5;
|
|
}
|
|
let wall_clock = clock::LinuxClock.system_time();
|
|
if wall_clock.seconds == 0 || wall_clock.microseconds >= 1_000_000 {
|
|
return -5;
|
|
}
|
|
let mut services = scheduling::ThreadServices::new(MissingDiagnostics);
|
|
let db = t::Database {
|
|
setup_flags: t::ENABLED | t::TIMEOUT_ENABLED,
|
|
timeout_flags: timeout::OS_TIMER,
|
|
compute_ms: 1000,
|
|
non_compute_ms: 1000,
|
|
compute_gpu_mask: 0,
|
|
};
|
|
let mut node = t::Node::default();
|
|
// This is an owned fixture node, not an invented RM registry lookup.
|
|
if t::reset_current(&mut services, &db, || Ok(t::Lookup::Found(&mut node))) != Ok(NvStatus::OK)
|
|
|| node.flags & t::NODE_TIMEOUT_INITIALIZED == 0
|
|
{
|
|
return -5;
|
|
}
|
|
node.next_yield_ns = 0;
|
|
if t::yield_current(&mut services, &db, true, || Ok(t::Lookup::Found(&mut node))) != Ok(()) {
|
|
return -5;
|
|
}
|
|
if node.next_yield_ns == 0 {
|
|
return -5;
|
|
}
|
|
if t::check_current(&mut services, &db, None, None, || {
|
|
Ok(t::Lookup::Found(&mut node))
|
|
}) != Ok(timeout::INVALID_STATE)
|
|
{
|
|
return -5;
|
|
}
|
|
9
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_host_irq_services() -> i32 {
|
|
use nv_rs_core::gpu::host_time;
|
|
let mut os = scheduling::LinuxScheduling;
|
|
if host_time::Io::context(&mut os)
|
|
.map(|flags| flags & (host_time::HARD_IRQ | host_time::INTERRUPT))
|
|
!= Ok(host_time::HARD_IRQ | host_time::INTERRUPT)
|
|
{
|
|
return -5;
|
|
}
|
|
if os.delay_us(20001) != Ok(host_time::GENERIC) {
|
|
return -5;
|
|
}
|
|
if os.schedule_cpu() != Ok(host_time::ILLEGAL_ACTION) {
|
|
return -5;
|
|
}
|
|
if os.delay_us(1) != Ok(nv_rs_abi::NvStatus::OK) {
|
|
return -5;
|
|
}
|
|
4
|
|
}
|
|
struct Workspace {
|
|
manager: Option<Manager<LinuxDma, 8>>,
|
|
root: MaybeUninit<Root>,
|
|
instance_manager: Option<Manager<LinuxDma, 1>>,
|
|
instance: MaybeUninit<instance::LinuxInstance>,
|
|
}
|
|
#[inline(never)]
|
|
unsafe fn initialize_manager(pci: *mut c_void, w: *mut Workspace) -> i32 {
|
|
unsafe {
|
|
ptr::addr_of_mut!((*w).manager).write(None);
|
|
let backend = match LinuxDma::from_owned_pci(pci, 32) {
|
|
Ok(b) => b,
|
|
Err(e) => return e.0,
|
|
};
|
|
(*w).manager = Some(
|
|
match Manager::<_, 8>::new_retaining(
|
|
backend,
|
|
Constraints {
|
|
dma_bits: 32,
|
|
max_segment_bytes: 4096,
|
|
segment_boundary_mask: u64::MAX,
|
|
},
|
|
) {
|
|
Ok(manager) => manager,
|
|
Err((_, mut backend)) => {
|
|
let _ = backend.close();
|
|
return -22;
|
|
}
|
|
},
|
|
);
|
|
0
|
|
}
|
|
}
|
|
#[inline(never)]
|
|
unsafe fn initialize_root(w: *mut Workspace) -> i32 {
|
|
unsafe {
|
|
let manager = match (*w).manager.take() {
|
|
Some(m) => m,
|
|
None => return -22,
|
|
};
|
|
ptr::write(
|
|
(*w).root.as_mut_ptr(),
|
|
Root::uninitialized(manager, Architecture::Volta, true),
|
|
);
|
|
match (&mut *(*w).root.as_mut_ptr()).initialize() {
|
|
Ok(()) => 0,
|
|
Err(_) => -5,
|
|
}
|
|
}
|
|
}
|
|
#[inline(never)]
|
|
unsafe fn initialize_instance_manager(pci: *mut c_void, w: *mut Workspace) -> i32 {
|
|
unsafe {
|
|
ptr::addr_of_mut!((*w).instance_manager).write(None);
|
|
let backend = match LinuxDma::from_owned_pci(pci, 32) {
|
|
Ok(b) => b,
|
|
Err(e) => return e.0,
|
|
};
|
|
(*w).instance_manager = Some(
|
|
match Manager::<_, 1>::new_retaining(
|
|
backend,
|
|
Constraints {
|
|
dma_bits: 32,
|
|
max_segment_bytes: 4096,
|
|
segment_boundary_mask: u64::MAX,
|
|
},
|
|
) {
|
|
Ok(manager) => manager,
|
|
Err((_, mut backend)) => {
|
|
let _ = backend.close();
|
|
return -22;
|
|
}
|
|
},
|
|
);
|
|
0
|
|
}
|
|
}
|
|
#[inline(never)]
|
|
unsafe fn validate_instance(pci: *mut c_void, w: *mut Workspace, root_address: u64) -> i32 {
|
|
unsafe {
|
|
use nv_rs_core::mmu::{
|
|
instance::Update,
|
|
ramin::{self, BaseFeatures, RootDescriptor, Target},
|
|
};
|
|
let status = initialize_instance_manager(pci, w);
|
|
if status != 0 {
|
|
return status;
|
|
}
|
|
let manager = match (*w).instance_manager.take() {
|
|
Some(m) => m,
|
|
None => return -22,
|
|
};
|
|
ptr::write(
|
|
(*w).instance.as_mut_ptr(),
|
|
instance::LinuxInstance::new(manager),
|
|
);
|
|
let block = &mut *(*w).instance.as_mut_ptr();
|
|
if block.initialize().is_err() {
|
|
return -5;
|
|
}
|
|
let mut checks = 1;
|
|
let mut actual = [0x99; 8];
|
|
if block.read_unpublished(512, &mut actual).is_err() || actual != [0; 8] {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let pdb = match ramin::encode_pdb(
|
|
Some(RootDescriptor {
|
|
dma_address: root_address,
|
|
aperture: ramin::Aperture::SystemCoherent,
|
|
volatile: false,
|
|
}),
|
|
Target::Base,
|
|
true,
|
|
BaseFeatures {
|
|
version2: true,
|
|
big_page_64k: true,
|
|
},
|
|
) {
|
|
Ok(p) => p,
|
|
Err(_) => return -5,
|
|
};
|
|
if block
|
|
.commit(Update {
|
|
pdb,
|
|
va_limit: None,
|
|
ats: None,
|
|
magic: None,
|
|
})
|
|
.is_err()
|
|
{
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if block.read_unpublished(512, &mut actual).is_err()
|
|
|| u32::from_le_bytes(actual[..4].try_into().unwrap())
|
|
!= fixture_ramin_base_low(root_address) as u32
|
|
|| u32::from_le_bytes(actual[4..].try_into().unwrap())
|
|
!= fixture_ramin_base_high(root_address) as u32
|
|
{
|
|
return -5;
|
|
}
|
|
checks += 2;
|
|
if block.freeze().is_ok() || block.dma_address().is_ok() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if block.flush_system_memory().is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let address = match block.dma_address() {
|
|
Ok(a) => a,
|
|
Err(_) => return -5,
|
|
};
|
|
if address == root_address || address & 4095 != 0 || address > u32::MAX as u64 {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let pin = match block.freeze() {
|
|
Ok(p) => p,
|
|
Err(_) => return -5,
|
|
};
|
|
checks += 1;
|
|
if block.close().is_ok() || block.read_unpublished(512, &mut actual).is_ok() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
// No root/channel was installed, so GPU quiescence holds by construction.
|
|
if block.thaw_after_gpu_quiescence(pin).is_err() || block.close().is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
checks
|
|
}
|
|
}
|
|
unsafe extern "C" {
|
|
fn fixture_volta_sys_pde(address: u64, depth: u32, ats: u32) -> u64;
|
|
fn fixture_volta_small_sys_pde(address: u64) -> u64;
|
|
fn fixture_volta_video_pte(address: u64) -> u64;
|
|
fn fixture_ramin_base_low(address: u64) -> u64;
|
|
fn fixture_ramin_base_high(address: u64) -> u64;
|
|
}
|
|
#[panic_handler]
|
|
fn panic(_: &core::panic::PanicInfo<'_>) -> ! {
|
|
unsafe { core::arch::asm!("ud2", options(noreturn, nostack)) }
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_rust_backing_size() -> usize {
|
|
mem::size_of::<Workspace>()
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_rust_backing_align() -> usize {
|
|
mem::align_of::<Workspace>()
|
|
}
|
|
/// Storage is caller-owned aligned kernel memory; PCI pointer is an owned lease.
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_rust_backing(pci: *mut c_void, storage: *mut u8, bytes: usize) -> i32 {
|
|
unsafe {
|
|
let mut clock = clock::LinuxClock;
|
|
if clock.tick_resolution_ns() == 0 || clock.monotonic_ns() > clock.monotonic_ns() {
|
|
return -5;
|
|
}
|
|
let context = match thread::current() {
|
|
Ok(c) => c,
|
|
Err(e) => return e,
|
|
};
|
|
if context.thread_id == 0 || context.in_interrupt != 0 {
|
|
return -5;
|
|
}
|
|
if pci.is_null()
|
|
|| storage.is_null()
|
|
|| bytes < mem::size_of::<Workspace>()
|
|
|| !(storage as usize).is_multiple_of(mem::align_of::<Workspace>())
|
|
{
|
|
return -22;
|
|
}
|
|
let w = storage.cast::<Workspace>();
|
|
let status = initialize_manager(pci, w);
|
|
if status != 0 {
|
|
return status;
|
|
}
|
|
let status = initialize_root(w);
|
|
if status != 0 {
|
|
return status;
|
|
}
|
|
let root = &mut *(*w).root.as_mut_ptr();
|
|
let mut checks = 4; // Clock properties and Rust process-context snapshot.
|
|
let pte = Pte {
|
|
address: 0x800000,
|
|
aperture: Aperture::Video,
|
|
protection: Protection::ReadWriteAtomic,
|
|
cached: true,
|
|
access_counters_disabled: false,
|
|
};
|
|
let va = 0x12345000;
|
|
if root.map_4k(va, pte).is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if root.table_count() != 5 {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let words = match root.inspect_4k(va) {
|
|
Ok(w) => w,
|
|
Err(_) => return -5,
|
|
};
|
|
let addresses = match root.inspect_addresses_4k(va) {
|
|
Ok(a) => a,
|
|
Err(_) => return -5,
|
|
};
|
|
for (i, &address) in addresses.iter().enumerate() {
|
|
if address & 4095 != 0 || address > u32::MAX as u64 || addresses[..i].contains(&address)
|
|
{
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
}
|
|
for level in 0..3 {
|
|
if words[level] != fixture_volta_sys_pde(addresses[level + 1], level as u32, 1) {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
}
|
|
if words[3] != fixture_volta_small_sys_pde(addresses[4]) {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if words[4] != fixture_volta_video_pte(pte.address) {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let root_address = match root.dma_address() {
|
|
Ok(a) => a,
|
|
Err(_) => return -5,
|
|
};
|
|
if root_address != addresses[0]
|
|
|| root_address & 4095 != 0
|
|
|| root_address > u32::MAX as u64
|
|
{
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let pins = match root.freeze() {
|
|
Ok(p) => p,
|
|
Err(_) => return -5,
|
|
};
|
|
checks += 1;
|
|
if root.map_4k(va + 4096, pte).is_ok() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
let instance_checks = validate_instance(pci, w, root_address);
|
|
if instance_checks < 11 {
|
|
return -5;
|
|
}
|
|
checks += instance_checks;
|
|
// No GPU root has been installed, so quiescence is established by construction.
|
|
if root.thaw_after_gpu_quiescence(pins).is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if root.unmap_4k(va).is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if root.inspect_4k(va).map(|v| v[4]) != Ok(0) {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if root.recover().is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
if root.drain_and_close().is_err() {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
checks
|
|
}
|
|
}
|
|
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_software_manager_init() -> i32 {
|
|
hypervisor::BuildProduct::current()
|
|
.map_err(|_| -95)
|
|
.and_then(|product| {
|
|
let hypervisor_type =
|
|
nv_rs_core::hypervisor::detect_current_x86(product.is_vgx_hyper());
|
|
software_manager::initialize(hypervisor_type)
|
|
})
|
|
.and_then(|()| runtime_threads::initialize())
|
|
.map_or_else(|e| e, |()| 0)
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn vm_software_manager_shutdown() -> i32 {
|
|
runtime_threads::shutdown()
|
|
.and_then(|()| software_manager::shutdown())
|
|
.map_or_else(|e| e, |()| 0)
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_device_software(raw: *const gpu::GpuLease, pci: *mut c_void) -> i32 {
|
|
unsafe {
|
|
unsafe extern "C" {
|
|
fn vm_pci_power_usage(pci: *mut c_void) -> i32;
|
|
}
|
|
let lease = &*raw;
|
|
if !lease.matches_pci(pci) {
|
|
return -5;
|
|
}
|
|
let mut guard = match lease.lock_register_io() {
|
|
Ok(guard) => guard,
|
|
Err(_) => return -5,
|
|
};
|
|
let before = vm_pci_power_usage(pci);
|
|
let failure = guard.with_power(|_| {
|
|
if vm_pci_power_usage(pci) != before + 1 {
|
|
return -22;
|
|
}
|
|
-5
|
|
});
|
|
if failure != Ok(-5) || vm_pci_power_usage(pci) != before {
|
|
return -5;
|
|
}
|
|
let checks = guard
|
|
.with_power(|guard| {
|
|
if vm_pci_power_usage(pci) != before + 1 {
|
|
return -5;
|
|
}
|
|
device_software_check(lease, guard)
|
|
})
|
|
.unwrap_or(-5);
|
|
if vm_pci_power_usage(pci) != before {
|
|
return -5;
|
|
}
|
|
checks
|
|
}
|
|
}
|
|
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn vm_first_open_cpu_state(raw: *const gpu::GpuLease) -> i32 {
|
|
unsafe {
|
|
let lease = &*raw;
|
|
let mut guard = match lease.lock_register_io() {
|
|
Ok(guard) => guard,
|
|
Err(error) => return error,
|
|
};
|
|
match device_software::with(lease, &mut guard, |view| view.record.phase()) {
|
|
Ok(timeout_runtime::Phase::Configured) => 1,
|
|
Err(error) if error.kernel == -2 => 0,
|
|
_ => -5,
|
|
}
|
|
}
|
|
}
|
|
|
|
unsafe fn device_software_check(lease: &gpu::GpuLease, guard: &mut gpu::LockedGpuRegisters) -> i32 {
|
|
unsafe {
|
|
let checks = runtime_threads::with(|registry| device_software_run(lease, guard, registry))
|
|
.unwrap_or(-5);
|
|
if checks < 0 {
|
|
return checks;
|
|
}
|
|
// A new borrow must see the configuration from the previous RM entry,
|
|
// and the entry itself must have been released on that earlier return.
|
|
match runtime_threads::with(|registry| {
|
|
registry.timeout_database().timeout_flags == 8
|
|
&& registry.current() == Err(nv_rs_abi::NvStatus(0x57))
|
|
}) {
|
|
Ok(true) => checks,
|
|
_ => -5,
|
|
}
|
|
}
|
|
}
|
|
|
|
unsafe fn device_software_run(
|
|
lease: &gpu::GpuLease,
|
|
guard: &mut gpu::LockedGpuRegisters,
|
|
registry: &thread_state::Registry,
|
|
) -> i32 {
|
|
unsafe {
|
|
use nv_rs_core::gpu::thread_timeout as t;
|
|
let mut checks = 0;
|
|
macro_rules! check {
|
|
($v:expr) => {
|
|
if !$v {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
};
|
|
}
|
|
check!(device_software::prepare_edu(lease, guard).is_ok()); //1
|
|
let before = match device_software::with(lease, guard, |mut v| {
|
|
let fields = v.parent.timeout_fields().unwrap();
|
|
let links = v
|
|
.parent
|
|
.with_image(|i| {
|
|
(
|
|
u64::from_le_bytes(i[0x9d8..0x9e0].try_into().unwrap()),
|
|
u64::from_le_bytes(i[0x1e60..0x1e68].try_into().unwrap()),
|
|
)
|
|
})
|
|
.unwrap();
|
|
(
|
|
v.timeout_key(),
|
|
fields.instance,
|
|
links.0 == v.context_logical.get(),
|
|
links.0 != links.1,
|
|
v.with_timeout_data(|_| ()).is_ok(),
|
|
v.record.phase(),
|
|
)
|
|
}) {
|
|
Ok(v) => v,
|
|
Err(_) => return -5,
|
|
};
|
|
check!(before.0 .0 != 0); //2
|
|
check!(before.1 < 32); //3
|
|
check!(before.2 && before.3); //4
|
|
check!(before.4); //5
|
|
check!(before.5 == timeout_runtime::Phase::Configured); //6
|
|
check!(device_software::prepare_edu(lease, guard).is_ok()); //7
|
|
check!(software_manager::with(|m| Ok(m.instances.live_count())) == Ok(1)); //8
|
|
if runtime_threads::shutdown() != Err(-16) {
|
|
return -5;
|
|
}
|
|
let caller = vm_device_software as *const () as u64;
|
|
check!(adapter_start::configure_timeout(lease, guard, caller).is_ok()); //9
|
|
check!(adapter_start::configure_timeout(lease, guard, caller).is_ok()); //10
|
|
let product = match hypervisor::BuildProduct::current() {
|
|
Ok(p) => p,
|
|
Err(_) => return -5,
|
|
};
|
|
let expected = if product.is_vgx_hyper() {
|
|
1_800_000
|
|
} else {
|
|
4_000_000
|
|
};
|
|
let after = match device_software::with(lease, guard, |mut v| {
|
|
(
|
|
v.timeout_key(),
|
|
v.record.phase(),
|
|
v.with_timeout_data(|d| (d.default_us, d.default_flags, d.gpu)),
|
|
v.parent.with_image(|i| (i[0x915] != 0, i[0x918])).unwrap(),
|
|
)
|
|
}) {
|
|
Ok(v) => v,
|
|
Err(_) => return -5,
|
|
};
|
|
check!(after.0 == before.0); //11
|
|
check!(after.1 == timeout_runtime::Phase::Configured); //12
|
|
check!(after.2 == Ok((expected, 8, Some(before.0)))); //13
|
|
let db = registry.timeout_database();
|
|
check!(
|
|
db.compute_ms == u64::from(expected / 1000)
|
|
&& db.non_compute_ms == u64::from(expected / 1000)
|
|
&& db.timeout_flags == 8
|
|
); //14
|
|
let mut services = scheduling::ThreadServices::new(MissingDiagnostics);
|
|
let mut current = match registry.enter(0, caller, &mut services) {
|
|
Ok(Some(entry)) => entry,
|
|
_ => return -5,
|
|
};
|
|
check!(
|
|
current.check_timeout(
|
|
&mut services,
|
|
Some(t::Gpu {
|
|
id: before.0,
|
|
instance: before.1,
|
|
attached_915: after.3 .0,
|
|
reset_918: after.3 .1
|
|
}),
|
|
None
|
|
) == Ok(nv_rs_abi::NvStatus::OK)
|
|
); //15
|
|
check!(!lease.hardware_ready()); //16
|
|
checks
|
|
}
|
|
}
|