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
//
// Copyright 2023, Colias Group, LLC
// Copyright (c) 2020 Arm Limited
//
// SPDX-License-Identifier: MIT
//

use core::ffi::c_uint;

use sel4_config::{sel4_cfg_enum, sel4_cfg_wrap_match};

use crate::{
    const_helpers::u32_into_usize, sys, ObjectBlueprint, ObjectBlueprintSeL4Arch, ObjectType,
    ObjectTypeSeL4Arch,
};

/// Alias for [`ObjectTypeArm`].
pub type ObjectTypeArch = ObjectTypeArm;

/// Alias for [`ObjectBlueprintArm`].
pub type ObjectBlueprintArch = ObjectBlueprintArm;

/// Corresponds to `seL4_ArchObjectType`.
#[sel4_cfg_enum]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ObjectTypeArm {
    SmallPage,
    LargePage,
    PT,
    #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
    VCpu,
    SeL4Arch(ObjectTypeSeL4Arch),
}

impl ObjectTypeArm {
    pub(crate) const fn into_sys(self) -> c_uint {
        sel4_cfg_wrap_match! {
            match self {
                Self::SmallPage => sys::_object::seL4_ARM_SmallPageObject,
                Self::LargePage => sys::_object::seL4_ARM_LargePageObject,
                Self::PT => sys::_object::seL4_ARM_PageTableObject,
                #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
                Self::VCpu => sys::_object::seL4_ARM_VCPUObject,
                Self::SeL4Arch(sel4_arch) => sel4_arch.into_sys(),
            }
        }
    }
}

impl From<ObjectTypeSeL4Arch> for ObjectTypeArch {
    fn from(ty: ObjectTypeSeL4Arch) -> Self {
        Self::SeL4Arch(ty)
    }
}

impl From<ObjectTypeSeL4Arch> for ObjectType {
    fn from(ty: ObjectTypeSeL4Arch) -> Self {
        Self::from(ObjectTypeArch::from(ty))
    }
}

/// Arm-specific variants of [`ObjectBlueprint`].
#[sel4_cfg_enum]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ObjectBlueprintArm {
    SmallPage,
    LargePage,
    PT,
    #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
    VCpu,
    SeL4Arch(ObjectBlueprintSeL4Arch),
}

impl ObjectBlueprintArm {
    pub(crate) const fn ty(self) -> ObjectTypeArch {
        sel4_cfg_wrap_match! {
            match self {
                Self::SmallPage => ObjectTypeArm::SmallPage,
                Self::LargePage => ObjectTypeArm::LargePage,
                Self::PT => ObjectTypeArm::PT,
                #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
                Self::VCpu => ObjectTypeArm::VCpu,
                Self::SeL4Arch(sel4_arch) => ObjectTypeArch::SeL4Arch(sel4_arch.ty()),
            }
        }
    }

    pub(crate) const fn physical_size_bits(self) -> usize {
        sel4_cfg_wrap_match! {
            match self {
                Self::SmallPage => u32_into_usize(sys::seL4_PageBits),
                Self::LargePage => u32_into_usize(sys::seL4_LargePageBits),
                Self::PT => u32_into_usize(sys::seL4_PageTableBits),
                #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
                Self::VCpu => u32_into_usize(sys::seL4_VCPUBits),
                Self::SeL4Arch(sel4_arch) => sel4_arch.physical_size_bits(),
            }
        }
    }
}

impl From<ObjectBlueprintSeL4Arch> for ObjectBlueprintArch {
    fn from(blueprint: ObjectBlueprintSeL4Arch) -> Self {
        Self::SeL4Arch(blueprint)
    }
}

impl From<ObjectBlueprintSeL4Arch> for ObjectBlueprint {
    fn from(ty: ObjectBlueprintSeL4Arch) -> Self {
        Self::from(ObjectBlueprintArch::from(ty))
    }
}