1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2025 Google
// SPDX-License-Identifier: MIT

use std::fmt;
use std::time::Duration;

use crate::error::MesaError;
use crate::error::MesaResult;
use crate::OwnedDescriptor;

/// Mapped memory caching flags (see virtio_gpu spec)
pub const MESA_MAP_CACHE_MASK: u32 = 0x0f;
pub const MESA_MAP_CACHE_CACHED: u32 = 0x01;
pub const MESA_MAP_CACHE_UNCACHED: u32 = 0x02;
pub const MESA_MAP_CACHE_WC: u32 = 0x03;
/// Access flags (not in virtio_gpu spec)
pub const MESA_MAP_ACCESS_MASK: u32 = 0xf0;
pub const MESA_MAP_ACCESS_READ: u32 = 0x10;
pub const MESA_MAP_ACCESS_WRITE: u32 = 0x20;
pub const MESA_MAP_ACCESS_RW: u32 = 0x30;

/// Mesa handle types (memory and sync in same namespace)
pub const MESA_HANDLE_TYPE_MEM_OPAQUE_FD: u32 = 0x0001;
pub const MESA_HANDLE_TYPE_MEM_DMABUF: u32 = 0x0002;
pub const MESA_HANDLE_TYPE_MEM_OPAQUE_WIN32: u32 = 0x0003;
pub const MESA_HANDLE_TYPE_MEM_SHM: u32 = 0x0004;
pub const MESA_HANDLE_TYPE_MEM_ZIRCON: u32 = 0x0005;

pub const MESA_HANDLE_TYPE_SIGNAL_OPAQUE_FD: u32 = 0x0010;
pub const MESA_HANDLE_TYPE_SIGNAL_SYNC_FD: u32 = 0x0020;
pub const MESA_HANDLE_TYPE_SIGNAL_OPAQUE_WIN32: u32 = 0x0030;
pub const MESA_HANDLE_TYPE_SIGNAL_ZIRCON: u32 = 0x0040;
pub const MESA_HANDLE_TYPE_SIGNAL_EVENT_FD: u32 = 0x0050;

/// Handle to OS-specific memory or synchronization objects.
pub struct MesaHandle {
    pub os_handle: OwnedDescriptor,
    pub handle_type: u32,
}

impl MesaHandle {
    /// Clones an existing Mesahandle, by using OS specific mechanisms.
    pub fn try_clone(&self) -> MesaResult<MesaHandle> {
        let clone = self
            .os_handle
            .try_clone()
            .map_err(|_| MesaError::InvalidMesaHandle)?;
        Ok(MesaHandle {
            os_handle: clone,
            handle_type: self.handle_type,
        })
    }
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct MesaMapping {
    pub ptr: u64,
    pub size: u64,
}

impl fmt::Debug for MesaHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Handle debug").finish()
    }
}

pub enum TubeType {
    Stream,
    Packet,
}

pub enum WaitTimeout {
    Finite(Duration),
    NoTimeout,
}

pub struct WaitEvent {
    pub connection_id: u64,
    pub hung_up: bool,
    pub readable: bool,
}

#[allow(dead_code)]
pub const WAIT_CONTEXT_MAX: usize = 16;

pub enum DescriptorType {
    Unknown,
    Memory(u32),
    WritePipe,
}

/// # Safety
///
/// Caller must ensure that MappedRegion's lifetime contains the lifetime of
/// pointer returned.
pub unsafe trait MappedRegion: Send + Sync {
    /// Returns a pointer to the beginning of the memory region. Should only be
    /// used for passing this region to ioctls for setting guest memory.
    fn as_ptr(&self) -> *mut u8;

    /// Returns the size of the memory region in bytes.
    fn size(&self) -> usize;

    /// Returns mesa mapping representation of the region
    fn as_mesa_mapping(&self) -> MesaMapping;
}

#[macro_export]
macro_rules! log_status {
    ($result:expr) => {
        match $result {
            Ok(_) => (),
            Err(e) => error!("Error recieved: {}", e),
        }
    };
}