sel4/arch/x86/
vspace.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: MIT
5//
6
7use crate::{
8    cap_type, const_helpers::u32_into_usize, sys, CapTypeForFrameObject,
9    CapTypeForFrameObjectOfFixedSize, CapTypeForTranslationTableObject, ObjectBlueprint,
10    ObjectBlueprintX64, ObjectBlueprintX86,
11};
12
13/// Frame object types for this kernel configuration.
14#[derive(Copy, Clone, Debug, PartialEq, Eq)]
15pub enum FrameObjectType {
16    _4k,
17    LargePage,
18    HugePage,
19}
20
21impl FrameObjectType {
22    pub const GRANULE: Self = Self::_4k;
23
24    pub const fn blueprint(self) -> ObjectBlueprint {
25        match self {
26            Self::_4k => ObjectBlueprint::Arch(ObjectBlueprintX86::_4k),
27            Self::LargePage => ObjectBlueprint::Arch(ObjectBlueprintX86::LargePage),
28            Self::HugePage => {
29                ObjectBlueprint::Arch(ObjectBlueprintX86::SeL4Arch(ObjectBlueprintX64::HugePage))
30            }
31        }
32    }
33
34    pub const fn from_bits(bits: usize) -> Option<Self> {
35        Some(match bits {
36            Self::_4K_BITS => Self::_4k,
37            Self::LARGE_PAGE_BITS => Self::LargePage,
38            Self::HUGE_PAGE_BITS => Self::HugePage,
39            _ => return None,
40        })
41    }
42
43    // For match arm LHS's, as we can't call const fn's
44    pub const _4K_BITS: usize = Self::_4k.bits();
45    pub const LARGE_PAGE_BITS: usize = Self::LargePage.bits();
46    pub const HUGE_PAGE_BITS: usize = Self::HugePage.bits();
47}
48
49impl CapTypeForFrameObject for cap_type::_4k {}
50
51impl CapTypeForFrameObjectOfFixedSize for cap_type::_4k {
52    const FRAME_OBJECT_TYPE: FrameObjectType = FrameObjectType::_4k;
53}
54
55impl CapTypeForFrameObject for cap_type::LargePage {}
56
57impl CapTypeForFrameObjectOfFixedSize for cap_type::LargePage {
58    const FRAME_OBJECT_TYPE: FrameObjectType = FrameObjectType::LargePage;
59}
60
61impl CapTypeForFrameObject for cap_type::HugePage {}
62
63impl CapTypeForFrameObjectOfFixedSize for cap_type::HugePage {
64    const FRAME_OBJECT_TYPE: FrameObjectType = FrameObjectType::HugePage;
65}
66
67// // //
68
69/// Translation table object types for this kernel configuration.
70#[derive(Copy, Clone, Debug, PartialEq, Eq)]
71pub enum TranslationTableObjectType {
72    PML4,
73    PDPT,
74    PageDirectory,
75    PageTable,
76}
77
78impl TranslationTableObjectType {
79    pub const fn blueprint(&self) -> ObjectBlueprint {
80        match self {
81            Self::PML4 => {
82                ObjectBlueprint::Arch(ObjectBlueprintX86::SeL4Arch(ObjectBlueprintX64::PML4))
83            }
84            Self::PDPT => {
85                ObjectBlueprint::Arch(ObjectBlueprintX86::SeL4Arch(ObjectBlueprintX64::PDPT))
86            }
87            Self::PageDirectory => ObjectBlueprint::Arch(ObjectBlueprintX86::PageDirectory),
88            Self::PageTable => ObjectBlueprint::Arch(ObjectBlueprintX86::PageTable),
89        }
90    }
91
92    pub const fn index_bits(&self) -> usize {
93        match self {
94            Self::PML4 => u32_into_usize(sys::seL4_PML4IndexBits),
95            Self::PDPT => u32_into_usize(sys::seL4_PDPTIndexBits),
96            Self::PageDirectory => u32_into_usize(sys::seL4_PageDirIndexBits),
97            Self::PageTable => u32_into_usize(sys::seL4_PageTableIndexBits),
98        }
99    }
100
101    pub const fn from_level(level: usize) -> Option<Self> {
102        Some(match level {
103            0 => Self::PML4,
104            1 => Self::PDPT,
105            2 => Self::PageDirectory,
106            3 => Self::PageTable,
107            _ => return None,
108        })
109    }
110}
111
112impl CapTypeForTranslationTableObject for cap_type::PML4 {
113    const TRANSLATION_TABLE_OBJECT_TYPE: TranslationTableObjectType =
114        TranslationTableObjectType::PML4;
115}
116
117impl CapTypeForTranslationTableObject for cap_type::PDPT {
118    const TRANSLATION_TABLE_OBJECT_TYPE: TranslationTableObjectType =
119        TranslationTableObjectType::PDPT;
120}
121
122impl CapTypeForTranslationTableObject for cap_type::PageDirectory {
123    const TRANSLATION_TABLE_OBJECT_TYPE: TranslationTableObjectType =
124        TranslationTableObjectType::PageDirectory;
125}
126
127impl CapTypeForTranslationTableObject for cap_type::PageTable {
128    const TRANSLATION_TABLE_OBJECT_TYPE: TranslationTableObjectType =
129        TranslationTableObjectType::PageTable;
130}
131
132pub mod vspace_levels {
133    pub const NUM_LEVELS: usize = 4;
134
135    pub const HIGHEST_LEVEL_WITH_PAGE_ENTRIES: usize = NUM_LEVELS - 3;
136}