sel4/arch/riscv/
vspace.rs

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
118
119
120
121
122
123
124
125
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT
//

#![allow(clippy::eq_op)]

use sel4_config::sel4_cfg_wrap_match;

#[allow(unused_imports)]
use crate::{
    cap_type, const_helpers::u32_into_usize, sys, CapTypeForFrameObject,
    CapTypeForFrameObjectOfFixedSize, CapTypeForTranslationTableObject, ObjectBlueprint,
    ObjectBlueprintRiscV,
};

/// Frame object types for this kernel configuration.
#[sel4_config::sel4_cfg_enum]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum FrameObjectType {
    _4kPage,
    MegaPage,
    #[sel4_cfg(any(PT_LEVELS = "3", PT_LEVELS = "4"))]
    GigaPage,
}

impl FrameObjectType {
    pub const GRANULE: Self = Self::_4kPage;

    pub const fn blueprint(self) -> ObjectBlueprint {
        sel4_cfg_wrap_match! {
            match self {
                FrameObjectType::_4kPage => ObjectBlueprint::Arch(ObjectBlueprintRiscV::_4kPage),
                FrameObjectType::MegaPage => ObjectBlueprint::Arch(ObjectBlueprintRiscV::MegaPage),
                #[sel4_cfg(any(PT_LEVELS = "3", PT_LEVELS = "4"))]
                FrameObjectType::GigaPage => ObjectBlueprint::Arch(ObjectBlueprintRiscV::GigaPage),
            }
        }
    }

    pub const fn from_bits(bits: usize) -> Option<Self> {
        Some(sel4_cfg_wrap_match! {
            match bits {
                Self::_4K_PAGE_BITS => Self::_4kPage,
                Self::MEGA_PAGE_BITS => Self::MegaPage,
                #[sel4_cfg(any(PT_LEVELS = "3", PT_LEVELS = "4"))]
                Self::GIGA_PAGE_BITS => Self::GigaPage,
                _ => return None,
            }
        })
    }

    // For match arm LHS's, as we can't call const fn's

    pub const _4K_PAGE_BITS: usize = Self::_4kPage.bits();
    pub const MEGA_PAGE_BITS: usize = Self::MegaPage.bits();

    #[sel4_config::sel4_cfg(any(PT_LEVELS = "3", PT_LEVELS = "4"))]
    pub const GIGA_PAGE_BITS: usize = Self::GigaPage.bits();
}

impl CapTypeForFrameObject for cap_type::_4kPage {}

impl CapTypeForFrameObjectOfFixedSize for cap_type::_4kPage {
    const FRAME_OBJECT_TYPE: FrameObjectType = FrameObjectType::_4kPage;
}

impl CapTypeForFrameObject for cap_type::MegaPage {}

impl CapTypeForFrameObjectOfFixedSize for cap_type::MegaPage {
    const FRAME_OBJECT_TYPE: FrameObjectType = FrameObjectType::MegaPage;
}

#[sel4_config::sel4_cfg(any(PT_LEVELS = "3", PT_LEVELS = "4"))]
impl CapTypeForFrameObject for cap_type::GigaPage {}

#[sel4_config::sel4_cfg(any(PT_LEVELS = "3", PT_LEVELS = "4"))]
impl CapTypeForFrameObjectOfFixedSize for cap_type::GigaPage {
    const FRAME_OBJECT_TYPE: FrameObjectType = FrameObjectType::GigaPage;
}

// // //

/// Translation table object types for this kernel configuration.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TranslationTableObjectType {
    PageTable,
}

impl TranslationTableObjectType {
    pub const fn blueprint(&self) -> ObjectBlueprint {
        ObjectBlueprint::Arch(ObjectBlueprintRiscV::PageTable)
    }

    pub const fn index_bits(&self) -> usize {
        u32_into_usize(sys::seL4_PageTableIndexBits)
    }

    pub const fn from_level(level: usize) -> Option<Self> {
        if level < vspace_levels::NUM_LEVELS {
            Some(Self::PageTable)
        } else {
            None
        }
    }
}

impl CapTypeForTranslationTableObject for cap_type::PageTable {
    const TRANSLATION_TABLE_OBJECT_TYPE: TranslationTableObjectType =
        TranslationTableObjectType::PageTable;
}

pub mod vspace_levels {
    use sel4_config::sel4_cfg_usize;

    pub const NUM_LEVELS: usize = sel4_cfg_usize!(PT_LEVELS);

    pub const HIGHEST_LEVEL_WITH_PAGE_ENTRIES: usize = NUM_LEVELS
        - if sel4_cfg_usize!(PT_LEVELS) == 3 || sel4_cfg_usize!(PT_LEVELS) == 4 {
            3
        } else {
            2
        };
}