2024-06-21 10:38:19 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
boot::{BootSetup, BootSetupPlatform},
|
|
|
|
elfloader::ElfImageLoader,
|
|
|
|
};
|
|
|
|
use uuid::Uuid;
|
|
|
|
use xencall::XenCall;
|
|
|
|
|
|
|
|
use crate::error::Result;
|
|
|
|
|
2024-08-15 01:06:04 -07:00
|
|
|
pub const XEN_EXTRA_MEMORY_KB: u64 = 2048;
|
|
|
|
|
2024-06-21 10:38:19 -07:00
|
|
|
pub struct BaseDomainManager<P: BootSetupPlatform> {
|
|
|
|
call: XenCall,
|
|
|
|
pub platform: Arc<P>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: BootSetupPlatform> BaseDomainManager<P> {
|
|
|
|
pub async fn new(call: XenCall, platform: P) -> Result<BaseDomainManager<P>> {
|
|
|
|
Ok(BaseDomainManager {
|
|
|
|
call,
|
|
|
|
platform: Arc::new(platform),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create(&self, config: BaseDomainConfig) -> Result<CreatedDomain> {
|
|
|
|
let mut domain = self.platform.create_domain(config.enable_iommu);
|
|
|
|
domain.handle = config.uuid.into_bytes();
|
|
|
|
domain.max_vcpus = config.max_vcpus;
|
|
|
|
let domid = self.call.create_domain(domain).await?;
|
|
|
|
self.call.set_max_vcpus(domid, config.max_vcpus).await?;
|
|
|
|
self.call
|
2024-08-15 01:06:04 -07:00
|
|
|
.set_max_mem(domid, (config.max_mem_mb * 1024) + XEN_EXTRA_MEMORY_KB)
|
2024-06-21 10:38:19 -07:00
|
|
|
.await?;
|
|
|
|
let loader = ElfImageLoader::load_file_kernel(&config.kernel)?;
|
|
|
|
let platform = (*self.platform).clone();
|
|
|
|
let mut boot = BootSetup::new(self.call.clone(), domid, platform, loader, None);
|
|
|
|
let mut domain = boot
|
|
|
|
.initialize(
|
|
|
|
&config.initrd,
|
2024-08-14 01:14:49 -07:00
|
|
|
config.target_mem_mb,
|
|
|
|
config.max_mem_mb,
|
2024-06-21 10:38:19 -07:00
|
|
|
config.max_vcpus,
|
|
|
|
&config.cmdline,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
boot.boot(&mut domain).await?;
|
|
|
|
Ok(CreatedDomain {
|
|
|
|
domid,
|
|
|
|
store_evtchn: domain.store_evtchn,
|
|
|
|
store_mfn: domain.store_mfn,
|
|
|
|
console_evtchn: domain.console_evtchn,
|
|
|
|
console_mfn: domain.console_mfn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn destroy(&self, domid: u32) -> Result<()> {
|
|
|
|
self.call.destroy_domain(domid).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct BaseDomainConfig {
|
|
|
|
pub uuid: Uuid,
|
|
|
|
pub owner_domid: u32,
|
|
|
|
pub max_vcpus: u32,
|
2024-08-15 01:06:04 -07:00
|
|
|
pub target_vcpus: u32,
|
2024-08-14 01:14:49 -07:00
|
|
|
pub max_mem_mb: u64,
|
|
|
|
pub target_mem_mb: u64,
|
2024-06-21 10:38:19 -07:00
|
|
|
pub kernel: Vec<u8>,
|
|
|
|
pub initrd: Vec<u8>,
|
|
|
|
pub cmdline: String,
|
|
|
|
pub enable_iommu: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct CreatedDomain {
|
|
|
|
pub domid: u32,
|
|
|
|
pub store_evtchn: u32,
|
|
|
|
pub store_mfn: u64,
|
|
|
|
pub console_evtchn: u32,
|
|
|
|
pub console_mfn: u64,
|
|
|
|
}
|