sel4/
vspace.rs

1//
2// Copyright 2023, Colias Group, LLC
3// Copyright (c) 2020 Arm Limited
4//
5// SPDX-License-Identifier: MIT
6//
7
8use crate::{
9    cap_type, CapType, CapTypeForObjectOfFixedSize, FrameObjectType, TranslationTableObjectType,
10};
11
12impl FrameObjectType {
13    pub const fn bits(self) -> usize {
14        self.blueprint().physical_size_bits()
15    }
16
17    pub const fn bytes(self) -> usize {
18        1 << self.bits()
19    }
20}
21
22/// Trait for [`CapType`]s which correspond to frame objects.
23pub trait CapTypeForFrameObject: CapType {}
24
25impl CapTypeForFrameObject for cap_type::UnspecifiedPage {}
26
27/// Trait for [`CapType`]s which correspond to frame objects of fixed size.
28pub trait CapTypeForFrameObjectOfFixedSize:
29    CapTypeForObjectOfFixedSize + CapTypeForFrameObject
30{
31    const FRAME_OBJECT_TYPE: FrameObjectType;
32}
33
34/// Trait for [`CapType`]s which correspond to translation table objects.
35pub trait CapTypeForTranslationTableObject: CapTypeForObjectOfFixedSize {
36    const TRANSLATION_TABLE_OBJECT_TYPE: TranslationTableObjectType;
37}
38
39/// Items describing the layout of address translation structures for this kernel configuration.
40pub mod vspace_levels {
41    use crate::{FrameObjectType, TranslationTableObjectType};
42
43    /// The maximum number of levels of translation tables for this kernel configuration.
44    pub use crate::arch::vspace_levels::NUM_LEVELS;
45
46    /// Highest level of translation table whose entries can be pages rather than lower-level translation tables.
47    pub use crate::arch::vspace_levels::HIGHEST_LEVEL_WITH_PAGE_ENTRIES;
48
49    /// The number of address bits spanned by the given translation table level.
50    pub fn span_bits(level: usize) -> usize {
51        assert!(level < NUM_LEVELS);
52        (level..NUM_LEVELS)
53            .map(|level| {
54                TranslationTableObjectType::from_level(level)
55                    .unwrap()
56                    .index_bits()
57            })
58            .sum::<usize>()
59            + FrameObjectType::GRANULE.bits()
60    }
61
62    /// The number of address bits spanned by entries of the given translation table level.
63    pub fn step_bits(level: usize) -> usize {
64        span_bits(level)
65            - TranslationTableObjectType::from_level(level)
66                .unwrap()
67                .index_bits()
68    }
69}