Skip to main content

rvm_types/
addr.rs

1//! Address types for the RVM microhypervisor.
2//!
3//! Provides strongly-typed wrappers around raw addresses to prevent
4//! accidental mixing of physical, virtual, and guest-physical address spaces.
5
6/// A host physical address.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[repr(transparent)]
9pub struct PhysAddr(u64);
10
11impl PhysAddr {
12    /// Create a new physical address.
13    #[must_use]
14    pub const fn new(addr: u64) -> Self {
15        Self(addr)
16    }
17
18    /// Return the raw address value.
19    #[must_use]
20    pub const fn as_u64(self) -> u64 {
21        self.0
22    }
23
24    /// Check if the address is page-aligned (4 KiB).
25    #[must_use]
26    pub const fn is_page_aligned(self) -> bool {
27        self.0.trailing_zeros() >= 12
28    }
29
30    /// Align the address down to the nearest page boundary.
31    #[must_use]
32    pub const fn page_align_down(self) -> Self {
33        Self(self.0 & !0xFFF)
34    }
35}
36
37/// A host virtual address.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
39#[repr(transparent)]
40pub struct VirtAddr(u64);
41
42impl VirtAddr {
43    /// Create a new virtual address.
44    #[must_use]
45    pub const fn new(addr: u64) -> Self {
46        Self(addr)
47    }
48
49    /// Return the raw address value.
50    #[must_use]
51    pub const fn as_u64(self) -> u64 {
52        self.0
53    }
54}
55
56/// A guest physical address, scoped to a partition.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
58#[repr(transparent)]
59pub struct GuestPhysAddr(u64);
60
61impl GuestPhysAddr {
62    /// Create a new guest physical address.
63    #[must_use]
64    pub const fn new(addr: u64) -> Self {
65        Self(addr)
66    }
67
68    /// Return the raw address value.
69    #[must_use]
70    pub const fn as_u64(self) -> u64 {
71        self.0
72    }
73
74    /// Check if the address is page-aligned (4 KiB).
75    #[must_use]
76    pub const fn is_page_aligned(self) -> bool {
77        self.0.trailing_zeros() >= 12
78    }
79}