xenplatform/
lib.rs

1pub mod boot;
2pub mod elfloader;
3pub mod error;
4pub mod mem;
5pub mod sys;
6
7use boot::{BootDomain, BootImageInfo, BootImageLoader, BootSetupPlatform};
8use domain::{PlatformKernelConfig, PlatformResourcesConfig};
9use elfloader::ElfImageLoader;
10use error::Result;
11use unsupported::UnsupportedPlatform;
12use xencall::{sys::CreateDomain, XenCall};
13
14use crate::error::Error;
15
16pub mod domain;
17pub mod unsupported;
18#[cfg(target_arch = "x86_64")]
19pub mod x86pv;
20
21#[derive(Clone)]
22pub enum ImageLoader {
23    Elf(ElfImageLoader),
24}
25
26impl ImageLoader {
27    async fn parse(&self, hvm: bool) -> Result<BootImageInfo> {
28        match self {
29            ImageLoader::Elf(elf) => elf.parse(hvm).await,
30        }
31    }
32
33    async fn load(&self, image_info: &BootImageInfo, dst: &mut [u8]) -> Result<()> {
34        match self {
35            ImageLoader::Elf(elf) => elf.load(image_info, dst).await,
36        }
37    }
38}
39
40#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
41pub enum RuntimePlatformType {
42    Unsupported,
43    #[cfg(target_arch = "x86_64")]
44    Pv,
45}
46
47impl RuntimePlatformType {
48    pub fn create(&self) -> RuntimePlatform {
49        match self {
50            RuntimePlatformType::Unsupported => {
51                RuntimePlatform::Unsupported(UnsupportedPlatform::new())
52            }
53            #[cfg(target_arch = "x86_64")]
54            RuntimePlatformType::Pv => RuntimePlatform::Pv(x86pv::X86PvPlatform::new()),
55        }
56    }
57
58    pub fn supported() -> RuntimePlatformType {
59        #[cfg(target_arch = "x86_64")]
60        return RuntimePlatformType::Pv;
61        #[cfg(not(target_arch = "x86_64"))]
62        return RuntimePlatformType::Unsupported;
63    }
64}
65
66#[allow(clippy::large_enum_variant)]
67pub enum RuntimePlatform {
68    Unsupported(UnsupportedPlatform),
69    #[cfg(target_arch = "x86_64")]
70    Pv(x86pv::X86PvPlatform),
71}
72
73impl RuntimePlatform {
74    #[allow(clippy::too_many_arguments)]
75    pub async fn initialize(
76        &mut self,
77        domid: u32,
78        call: XenCall,
79        image_loader: &ImageLoader,
80        kernel: &PlatformKernelConfig,
81        resources: &PlatformResourcesConfig,
82    ) -> Result<BootDomain> {
83        match self {
84            RuntimePlatform::Unsupported(unsupported) => {
85                unsupported
86                    .initialize(domid, call, image_loader, kernel, resources)
87                    .await
88            }
89            #[cfg(target_arch = "x86_64")]
90            RuntimePlatform::Pv(pv) => {
91                pv.initialize(domid, call, image_loader, kernel, resources)
92                    .await
93            }
94        }
95    }
96
97    pub async fn boot(&mut self, domid: u32, call: XenCall, domain: &mut BootDomain) -> Result<()> {
98        match self {
99            RuntimePlatform::Unsupported(unsupported) => {
100                unsupported.boot(domid, call, domain).await
101            }
102            #[cfg(target_arch = "x86_64")]
103            RuntimePlatform::Pv(pv) => pv.boot(domid, call, domain).await,
104        }
105    }
106
107    pub fn create_domain(&self, enable_iommu: bool) -> CreateDomain {
108        match self {
109            RuntimePlatform::Unsupported(unsupported) => unsupported.create_domain(enable_iommu),
110            #[cfg(target_arch = "x86_64")]
111            RuntimePlatform::Pv(pv) => pv.create_domain(enable_iommu),
112        }
113    }
114}