Skip to main content

vm/
page_state.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use kernel::relaxed_atomic::RelaxedAtomicI64;
6
7/// Defines the state of a VM page (`vm_page_t`).
8///
9/// Be sure to keep this enum in sync with the definition of `vm_page_t`.
10#[repr(u8)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub enum VmPageState {
13    Free = 0,
14    Alloc,
15    Object,
16    Wired,
17    Heap,
18    /// Allocated to serve arch-specific mmu purposes.
19    Mmu,
20    /// Allocated for platform-specific iommu structures.
21    Iommu,
22    Ipc,
23    Cache,
24    Slab,
25    Zram,
26    FreeLoaned,
27
28    Count,
29}
30
31impl VmPageState {
32    /// Returns the index of `self` as a `usize`.
33    #[inline]
34    pub const fn index(self) -> usize {
35        self as usize
36    }
37}
38
39/// Counts of VM pages by state.
40#[repr(C)]
41#[derive(Debug, Default)]
42pub struct VmPageCounts {
43    /// See comment in `percpu::vm_page_counts` for why we used a `RelaxedAtomic`.
44    pub by_state: [RelaxedAtomicI64; (VmPageState::Count).index()],
45}
46
47/// Returns a string description of `state`.
48#[inline]
49pub const fn page_state_to_string(state: VmPageState) -> &'static str {
50    match state {
51        VmPageState::Free => "free",
52        VmPageState::Alloc => "alloc",
53        VmPageState::Object => "object",
54        VmPageState::Wired => "wired",
55        VmPageState::Heap => "heap",
56        VmPageState::Mmu => "mmu",
57        VmPageState::Ipc => "ipc",
58        VmPageState::Cache => "cache",
59        VmPageState::Slab => "slab",
60        VmPageState::Zram => "zram",
61        _ => "unknown",
62    }
63}
64
65impl core::fmt::Display for VmPageState {
66    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67        f.write_str(page_state_to_string(*self))
68    }
69}
70
71const _: () = assert!(core::mem::size_of::<VmPageState>() == 1);
72const _: () = assert!(core::mem::align_of::<VmPageState>() == 1);
73const _: () = assert!(core::mem::size_of::<VmPageCounts>() == 12 * 8);
74const _: () = assert!(core::mem::align_of::<VmPageCounts>() == 8);
75const _: () = assert!(core::mem::offset_of!(VmPageCounts, by_state) == 0);