pub const VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM: u32 = 1; pub const BYTES_PER_PIXEL_RGBA: u32 = 4; pub const PAGE_SIZE_BYTES: u32 = 4096; pub const MAX_RESOURCE_DIMENSION: u32 = 3840; pub const MAX_RESOURCE_BACKING_BYTES: u32 = 16 * 1024 * 1024; pub const MAX_BACKING_PAGES: u32 = 4096; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ResourcePhase { Unallocated = 0, Refused = 1, ScanoutAttached = 2, } #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ResourceError { ZeroResourceId = 1, UnsupportedPixelFormat = 2, ZeroDimensions = 3, DimensionExceedsLimit = 4, StrideOverflowOrTooSmall = 5, UnalignedRowStride = 6, BackingSizeOverflow = 7, BackingExceedsRamBudget = 8, MismatchedBackingBytes = 9, MismatchedBackingPages = 10, InvalidScanoutId = 11, UnboundResourceDigest = 12, AlreadyAllocated = 13, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct VirtioGpuResourceRequest { pub resource_id: u32, pub scanout_id: u32, pub format: u32, pub width: u32, pub height: u32, pub stride: u32, pub backing_bytes: u32, pub backing_pages: u32, pub resource_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct VirtioGpuResourceReceipt { pub resource_id: u32, pub scanout_id: u32, pub format: u32, pub width: u32, pub height: u32, pub stride: u32, pub backing_bytes: u32, pub backing_pages: u32, pub resource_binding_digest: u64, pub scanout_receipt_digest: u64, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ResourceStatus { pub phase: ResourcePhase, pub allocated: bool, pub resource_id: u32, pub width: u32, pub height: u32, pub backing_bytes: u32, pub backing_pages: u32, pub resource_binding_digest: u64, pub scanout_receipt_digest: u64, pub overflow_refusals: u64, pub backing_mismatch_refusals: u64, pub format_or_id_refusals: u64, pub rejected_total: u64, pub allocated_total: u64, } pub const fn compute_resource_binding( resource_id: u32, scanout_id: u32, format: u32, width: u32, height: u32, stride: u32, backing_bytes: u32, ) -> u64 { let mut h: u64 = 0xCBF2_9CE4_8422_2325; h = (h ^ (resource_id as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (scanout_id as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (format as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (width as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (height as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (stride as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (backing_bytes as u64)).wrapping_mul(0x0000_0100_0000_01B3); h } pub const fn compute_scanout_receipt_digest( resource_binding_digest: u64, backing_pages: u32, scanout_id: u32, ) -> u64 { let mut h = resource_binding_digest ^ 0x517C_C1B7_2722_0A95; h = (h ^ (backing_pages as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (scanout_id as u64)).wrapping_mul(0x0000_0100_0000_01B3); h } pub fn create_2d_resource_and_attach_backing( req: &VirtioGpuResourceRequest, ) -> Result { if req.resource_id == 0 { return Err(ResourceError::ZeroResourceId); } if req.scanout_id > 15 { return Err(ResourceError::InvalidScanoutId); } if req.format != VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM { return Err(ResourceError::UnsupportedPixelFormat); } if req.width == 0 || req.height == 0 { return Err(ResourceError::ZeroDimensions); } if req.width > MAX_RESOURCE_DIMENSION || req.height > MAX_RESOURCE_DIMENSION { return Err(ResourceError::DimensionExceedsLimit); } let min_stride = match req.width.checked_mul(BYTES_PER_PIXEL_RGBA) { Some(v) => v, None => return Err(ResourceError::StrideOverflowOrTooSmall), }; if req.stride < min_stride { return Err(ResourceError::StrideOverflowOrTooSmall); } if (req.stride & 3) != 0 { return Err(ResourceError::UnalignedRowStride); } let expected_bytes = match req.stride.checked_mul(req.height) { Some(v) => v, None => return Err(ResourceError::BackingSizeOverflow), }; if expected_bytes > MAX_RESOURCE_BACKING_BYTES { return Err(ResourceError::BackingExceedsRamBudget); } if req.backing_bytes != expected_bytes { return Err(ResourceError::MismatchedBackingBytes); } let expected_pages = expected_bytes.div_ceil(PAGE_SIZE_BYTES); if req.backing_pages != expected_pages || req.backing_pages > MAX_BACKING_PAGES { return Err(ResourceError::MismatchedBackingPages); } let expected_binding = compute_resource_binding( req.resource_id, req.scanout_id, req.format, req.width, req.height, req.stride, req.backing_bytes, ); if req.resource_binding_digest != expected_binding { return Err(ResourceError::UnboundResourceDigest); } let scanout_receipt_digest = compute_scanout_receipt_digest(expected_binding, req.backing_pages, req.scanout_id); Ok(VirtioGpuResourceReceipt { resource_id: req.resource_id, scanout_id: req.scanout_id, format: req.format, width: req.width, height: req.height, stride: req.stride, backing_bytes: req.backing_bytes, backing_pages: req.backing_pages, resource_binding_digest: expected_binding, scanout_receipt_digest, }) } pub struct VirtioGpuResourceCoordinator { phase: ResourcePhase, allocated: bool, committed: Option, overflow_refusals: u64, backing_mismatch_refusals: u64, format_or_id_refusals: u64, rejected_total: u64, allocated_total: u64, } impl VirtioGpuResourceCoordinator { pub const fn new() -> Self { Self { phase: ResourcePhase::Unallocated, allocated: false, committed: None, overflow_refusals: 0, backing_mismatch_refusals: 0, format_or_id_refusals: 0, rejected_total: 0, allocated_total: 0, } } pub fn allocate_and_attach( &mut self, req: &VirtioGpuResourceRequest, ) -> Result { if self.allocated { self.rejected_total += 1; return Err(ResourceError::AlreadyAllocated); } match create_2d_resource_and_attach_backing(req) { Ok(receipt) => { self.phase = ResourcePhase::ScanoutAttached; self.allocated = true; self.allocated_total += 1; self.committed = Some(receipt); Ok(receipt) } Err(e) => { self.phase = ResourcePhase::Refused; self.rejected_total += 1; match e { ResourceError::DimensionExceedsLimit | ResourceError::StrideOverflowOrTooSmall | ResourceError::BackingSizeOverflow | ResourceError::BackingExceedsRamBudget => { self.overflow_refusals += 1; } ResourceError::MismatchedBackingBytes | ResourceError::MismatchedBackingPages | ResourceError::UnalignedRowStride => { self.backing_mismatch_refusals += 1; } _ => { self.format_or_id_refusals += 1; } } Err(e) } } } pub fn committed_receipt(&self) -> Option { self.committed } pub fn status(&self) -> ResourceStatus { let ( resource_id, width, height, backing_bytes, backing_pages, resource_binding_digest, scanout_receipt_digest, ) = match self.committed { Some(r) => ( r.resource_id, r.width, r.height, r.backing_bytes, r.backing_pages, r.resource_binding_digest, r.scanout_receipt_digest, ), None => (0, 0, 0, 0, 0, 0, 0), }; ResourceStatus { phase: self.phase, allocated: self.allocated, resource_id, width, height, backing_bytes, backing_pages, resource_binding_digest, scanout_receipt_digest, overflow_refusals: self.overflow_refusals, backing_mismatch_refusals: self.backing_mismatch_refusals, format_or_id_refusals: self.format_or_id_refusals, rejected_total: self.rejected_total, allocated_total: self.allocated_total, } } }