Files
RedBear-OS/recipes/drivers/virtio-core/src/utils.rs
T
vasilito b9874d0941 feat: USB storage read/write proof + full Red Bear OS tree sync
Add redbear-usb-storage-check in-guest binary that validates USB mass
storage read and write I/O: discovers /scheme/disk/ devices, writes a
test pattern to sector 2048, reads it back, verifies match, restores
original content. Updates test-usb-storage-qemu.sh with write-proof
verification step.

Includes all accumulated Red Bear OS work: kernel patches, relibc
patches, driver infrastructure, DRM/GPU, KDE recipes, firmware,
validation tooling, build system hardening, and documentation.
2026-05-03 23:03:24 +01:00

81 lines
1.8 KiB
Rust

use core::cell::UnsafeCell;
use core::fmt::Debug;
use core::marker::PhantomData;
#[repr(C)]
pub struct VolatileCell<T> {
value: UnsafeCell<T>,
}
impl<T: Copy> VolatileCell<T> {
#[inline]
pub const fn new(value: T) -> Self {
Self {
value: UnsafeCell::new(value),
}
}
/// Returns a copy of the contained value.
#[inline]
pub fn get(&self) -> T {
unsafe { core::ptr::read_volatile(self.value.get()) }
}
/// Sets the contained value.
#[inline]
pub fn set(&mut self, value: T) {
unsafe { core::ptr::write_volatile(self.value.get(), value) }
}
}
impl<T> Debug for VolatileCell<T>
where
T: Debug + Copy,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VolatileCell")
.field("value", &self.get())
.finish()
}
}
unsafe impl<T> Sync for VolatileCell<T> {}
#[repr(C)]
pub struct IncompleteArrayField<T>(PhantomData<T>, [T; 0]);
impl<T> IncompleteArrayField<T> {
#[inline]
pub const fn new() -> Self {
IncompleteArrayField(PhantomData, [])
}
#[inline]
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
core::slice::from_raw_parts(self.as_ptr(), len)
}
#[inline]
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
core::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
self as *const _ as *const T
}
#[inline]
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
self as *mut _ as *mut T
}
}
pub const fn align(val: usize, align: usize) -> usize {
(val + align) & !align
}
pub const fn align_down(addr: usize) -> usize {
addr & !(syscall::PAGE_SIZE - 1)
}