vyre-driver-cuda 0.6.1

CUDA/PTX backend for vyre through the CUDA driver API.
Documentation
//! IoUring Fixed Mapped DMA Hardware Buffers (Cudagrep)
//!
//! Even with `SQPOLL` Kernel Bypassing, if Wireshift / Cudagrep reads into dynamic `Vec<u8>` layouts,
//! the Kernel's Network Stack inherently halts to `get_user_pages()` traversing the Virtual Memory Layouts natively
//! mapping physical RAM mappings natively pinning buffers continuously during IO limits.
//!
//! True native perfection invokes `IORING_REGISTER_BUFFERS`.
//! `Cudagrep` allocates a fixed array map once. The Linux Kernel mathematically caches 
//! the exact Physical Memory boundaries unconditionally. 
//! IO reads execute `IORING_OP_READ_FIXED`, forcing the NIC to DMA explicitly into pre-validated hardware boundaries taking exactly 0.0 nanoseconds of MMU setup overhead.

use libc::{c_void, io_uring_register, IORING_REGISTER_BUFFERS};
use std::os::unix::io::RawFd;
use std::alloc::{alloc, dealloc, Layout};

#[repr(C)] // Natively matching Linux `iovec` structs safely
struct Iovec {
    iov_base: *mut c_void,
    iov_len: usize,
}

pub struct FixedMmuBufferPool {
    ring_fd: RawFd,
    fixed_buffers: Vec<Iovec>,
}

#[derive(Debug)]
pub enum IoUringError {
    RegistrationFailed { os_error: i32, context: &'static str },
    AllocationFailed { count: usize, alignment: usize },
}
impl std::fmt::Display for IoUringError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::RegistrationFailed { os_error, context } => write!(f, "io_uring_register failed: {} (OS error: {})", context, os_error),
            Self::AllocationFailed { count, alignment } => write!(f, "failed to allocate io_uring pinned buffer pool: {} elements aligned to {}", count, alignment),
        }
    }
}
impl std::error::Error for IoUringError {}

impl FixedMmuBufferPool {
    /// Locks the architectural layout of the process exactly onto the Native Linux Memory Manager Limits
    pub fn register_physical_buffers(ring_fd: RawFd, capacity: usize, count: usize) -> Result<Self, IoUringError> {
        let mut buffers = Vec::with_capacity(count);
        let align = 4096; // Strictly align to page boundary for Physical DMA
        let layout = Layout::from_size_align(capacity, align).map_err(|_| {
            IoUringError::AllocationFailed { count: 1, alignment: align }
        })?;
        
        for _ in 0..count {
            let ptr = unsafe { alloc(layout) };
            if ptr.is_null() {
                // Instantly free previously successful blocks safely intercepting leak gaps
                for b in &buffers {
                    unsafe { dealloc(b.iov_base as *mut u8, layout) };
                }
                return Err(IoUringError::AllocationFailed { count, alignment: align });
            }
            buffers.push(Iovec {
                iov_base: ptr as *mut c_void,
                iov_len: capacity,
            });
        }

        let res = unsafe {
            // Evaluates pure Native OS Memory mapping mathematically
            io_uring_register(
                ring_fd, 
                IORING_REGISTER_BUFFERS, 
                buffers.as_ptr() as *const c_void, 
                count as u32
            )
        };

        if res < 0 {
            let err = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
            for b in &buffers {
                unsafe { dealloc(b.iov_base as *mut u8, layout) };
            }
            return Err(IoUringError::RegistrationFailed { os_error: err, context: "kernel rejected pinned buffer hardware restrictions natively" });
        }

        tracing::info!("Cudagrep flawlessly registered {} native Hardware Buffer mappings inside the Linux Kernel.", count);
        Ok(Self { ring_fd, fixed_buffers: buffers })
    }
}

impl Drop for FixedMmuBufferPool {
    fn drop(&mut self) {
        unsafe {
            // Force hardware limits back to kernel correctly bounds unmapped.
            // IORING_UNREGISTER_BUFFERS conceptually evaluates 1 natively
            let res = libc::io_uring_register(self.ring_fd, 1, std::ptr::null(), 0);
            if res < 0 {
                tracing::error!("failed to unregister fixed IORING mappings cleanly natively");
            }
            
            let align = 4096;
            for b in &self.fixed_buffers {
                let layout = Layout::from_size_align_unchecked(b.iov_len, align);
                dealloc(b.iov_base as *mut u8, layout);
            }
        }
    }
}