Files
nvdriver/tests/vm/nano_timer.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

154 lines
4.7 KiB
Rust

//! Real Linux hrtimer tests; no original RM callback or GPU programming.
use crate::{
clock::LinuxClock,
gpu::GpuLease,
nano_timer::{Callback, NanoTimer},
scheduling::LinuxScheduling,
thread,
};
use core::{
ffi::c_void,
sync::atomic::{AtomicU32, Ordering},
};
static DROPS: AtomicU32 = AtomicU32::new(0);
pub struct Counter {
fired: AtomicU32,
interrupt: AtomicU32,
}
impl Counter {
fn new() -> Self {
Self {
fired: AtomicU32::new(0),
interrupt: AtomicU32::new(0),
}
}
}
unsafe impl Callback for Counter {
fn fire(&self) {
if let Ok(context) = thread::current() {
self.interrupt
.store(context.in_interrupt, Ordering::Relaxed);
}
self.fired.fetch_add(1, Ordering::Release);
}
}
impl Drop for Counter {
fn drop(&mut self) {
DROPS.fetch_add(1, Ordering::SeqCst);
}
}
pub type Timer = NanoTimer<Counter>;
unsafe extern "C" {
fn vm_nano_call_irqoff(
timer: *mut c_void,
close: unsafe extern "C" fn(*mut c_void) -> i32,
) -> i32;
}
unsafe extern "C" fn close(raw: *mut c_void) -> i32 {
unsafe { (&mut *raw.cast::<Timer>()).close().err().unwrap_or(0) }
}
unsafe extern "C" fn start_irqoff(raw: *mut c_void) -> i32 {
unsafe {
(&*raw.cast::<Timer>())
.start_relative(60_000_000_000)
.err()
.unwrap_or(0)
}
}
pub unsafe fn run(lease: &GpuLease) -> i32 {
unsafe {
let mut checks = 0;
macro_rules! check {
($p:expr) => {
if !$p {
return -5;
}
checks += 1;
};
}
check!(DROPS.load(Ordering::SeqCst) == 0);
let gpu = match lease.try_clone() {
Ok(g) => g,
Err(_) => return -5,
};
checks += 1;
let mut timer = match Timer::create(gpu, Counter::new()) {
Ok(t) => t,
Err(_) => return -5,
};
checks += 1;
check!(timer.callback().is_ok());
check!(timer.callback().unwrap().fired.load(Ordering::Acquire) == 0);
check!(vm_nano_call_irqoff((&mut timer as *mut Timer).cast(), start_irqoff) == 0);
check!(timer.cancel() == Ok(true));
check!(timer.callback().unwrap().fired.load(Ordering::Acquire) == 0);
check!(timer.cancel() == Ok(false));
check!(timer.start_relative(1).is_ok());
let mut clock = LinuxClock;
let deadline = clock.monotonic_ns().saturating_add(2_000_000_000);
let mut schedule = LinuxScheduling;
while timer.callback().unwrap().fired.load(Ordering::Acquire) == 0 {
if clock.monotonic_ns() >= deadline {
return -110;
}
if schedule.schedule_cpu().is_err() {
return -5;
}
}
check!(timer.cancel() == Ok(false));
check!(timer.callback().unwrap().fired.load(Ordering::Acquire) == 1);
check!(timer.callback().unwrap().interrupt.load(Ordering::Relaxed) == 1);
check!(vm_nano_call_irqoff((&mut timer as *mut Timer).cast(), close) == -11);
check!(DROPS.load(Ordering::SeqCst) == 0);
check!(timer.callback().is_ok());
check!(timer.start_relative(60_000_000_000).is_ok());
check!(timer.close().is_ok());
check!(DROPS.load(Ordering::SeqCst) == 1);
check!(timer.start_relative(1) == Err(-19));
check!(timer.callback().is_err());
check!(timer.close() == Err(-19));
drop(timer);
check!(DROPS.load(Ordering::SeqCst) == 1);
checks
}
}
pub unsafe fn hold(lease: &GpuLease, storage: *mut Timer) -> i32 {
unsafe {
let gpu = match lease.try_clone() {
Ok(g) => g,
Err(_) => return -5,
};
let timer = match Timer::create(gpu, Counter::new()) {
Ok(t) => t,
Err(_) => return -5,
};
if timer.start_relative(60_000_000_000).is_err() {
return -5;
}
core::ptr::write(storage, timer);
0
}
}
pub unsafe fn removed(storage: *mut Timer) -> i32 {
unsafe {
let timer = &mut *storage;
let mut checks = 0;
macro_rules! check {
($p:expr) => {
if !$p {
return -5;
}
checks += 1;
};
}
check!(timer.start_relative(1) == Err(-19));
check!(timer.callback().unwrap().fired.load(Ordering::Acquire) == 0);
check!(timer.cancel() == Ok(true));
check!(timer.close().is_ok());
check!(DROPS.load(Ordering::SeqCst) == 2);
core::ptr::drop_in_place(storage);
check!(DROPS.load(Ordering::SeqCst) == 2);
checks
}
}