Skip to main content

rvm_types/
memory.rs

1//! Memory region types.
2
3use crate::{GuestPhysAddr, PartitionId, PhysAddr};
4
5/// Unique identifier for an owned memory region.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[repr(transparent)]
8pub struct OwnedRegionId(u64);
9
10impl OwnedRegionId {
11    /// Create a new region identifier.
12    #[must_use]
13    pub const fn new(id: u64) -> Self {
14        Self(id)
15    }
16
17    /// Return the raw identifier value.
18    #[must_use]
19    pub const fn as_u64(self) -> u64 {
20        self.0
21    }
22}
23
24/// Memory tier classification (hot/warm/cold).
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[repr(u8)]
27pub enum MemoryTier {
28    /// Hot tier: SRAM or L1/L2 cache-resident.
29    Hot = 0,
30    /// Warm tier: DRAM.
31    Warm = 1,
32    /// Cold tier: persistent or swap-backed.
33    Cold = 2,
34}
35
36/// Access policy for a memory region.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct RegionPolicy {
39    /// Allow read access.
40    pub read: bool,
41    /// Allow write access.
42    pub write: bool,
43    /// Allow execute access.
44    pub execute: bool,
45}
46
47impl RegionPolicy {
48    /// Read-only policy.
49    pub const READ_ONLY: Self = Self {
50        read: true,
51        write: false,
52        execute: false,
53    };
54
55    /// Read-write policy.
56    pub const READ_WRITE: Self = Self {
57        read: true,
58        write: true,
59        execute: false,
60    };
61}
62
63/// Placement weights for region assignment during split.
64#[derive(Debug, Clone, Copy)]
65pub struct RegionPlacementWeights {
66    /// Weight toward left partition.
67    pub left: u16,
68    /// Weight toward right partition.
69    pub right: u16,
70}
71
72/// A typed, tiered, owned memory region.
73#[derive(Debug, Clone, Copy)]
74pub struct MemoryRegion {
75    /// Region identifier.
76    pub id: OwnedRegionId,
77    /// Owning partition.
78    pub owner: PartitionId,
79    /// Guest physical base address.
80    pub guest_base: GuestPhysAddr,
81    /// Host physical base address.
82    pub host_base: PhysAddr,
83    /// Number of pages.
84    pub page_count: u32,
85    /// Memory tier.
86    pub tier: MemoryTier,
87    /// Access policy.
88    pub policy: RegionPolicy,
89}