actually allocate max memory

This commit is contained in:
Alex Zenla
2024-01-10 19:18:48 -08:00
parent 153619a02c
commit 629c3d81b0
7 changed files with 80 additions and 64 deletions

View File

@ -6,7 +6,7 @@ use crate::sys::{
};
use crate::{XenCall, XenCallError};
use std::ffi::c_ulong;
use std::ptr::addr_of;
use std::ptr::addr_of_mut;
pub struct DomainControl<'a> {
call: &'a XenCall,
@ -18,7 +18,7 @@ impl DomainControl<'_> {
}
pub fn get_domain_info(&self, domid: u32) -> Result<GetDomainInfo, XenCallError> {
let domctl = DomCtl {
let mut domctl = DomCtl {
cmd: XEN_DOMCTL_GETDOMAININFO,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
@ -48,24 +48,24 @@ impl DomainControl<'_> {
},
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
.hypercall1(HYPERVISOR_DOMCTL, addr_of_mut!(domctl) as c_ulong)?;
Ok(unsafe { domctl.value.get_domain_info })
}
pub fn create_domain(&self, create_domain: CreateDomain) -> Result<u32, XenCallError> {
let domctl = DomCtl {
let mut domctl = DomCtl {
cmd: XEN_DOMCTL_CREATEDOMAIN,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid: 0,
value: DomCtlValue { create_domain },
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
.hypercall1(HYPERVISOR_DOMCTL, addr_of_mut!(domctl) as c_ulong)?;
Ok(domctl.domid)
}
pub fn set_max_mem(&mut self, domid: u32, memkb: u64) -> Result<(), XenCallError> {
let domctl = DomCtl {
pub fn set_max_mem(&self, domid: u32, memkb: u64) -> Result<(), XenCallError> {
let mut domctl = DomCtl {
cmd: XEN_DOMCTL_MAX_MEM,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
@ -74,12 +74,12 @@ impl DomainControl<'_> {
},
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
.hypercall1(HYPERVISOR_DOMCTL, addr_of_mut!(domctl) as c_ulong)?;
Ok(())
}
pub fn set_max_vcpus(&mut self, domid: u32, max_vcpus: u32) -> Result<(), XenCallError> {
let domctl = DomCtl {
pub fn set_max_vcpus(&self, domid: u32, max_vcpus: u32) -> Result<(), XenCallError> {
let mut domctl = DomCtl {
cmd: XEN_DOMCTL_MAX_VCPUS,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
@ -88,12 +88,12 @@ impl DomainControl<'_> {
},
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
.hypercall1(HYPERVISOR_DOMCTL, addr_of_mut!(domctl) as c_ulong)?;
Ok(())
}
pub fn hypercall_init(&self, domid: u32, gmfn: u64) -> Result<(), XenCallError> {
let domctl = DomCtl {
let mut domctl = DomCtl {
cmd: XEN_DOMCTL_HYPERCALL_INIT,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
@ -102,19 +102,19 @@ impl DomainControl<'_> {
},
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
.hypercall1(HYPERVISOR_DOMCTL, addr_of_mut!(domctl) as c_ulong)?;
Ok(())
}
pub fn destroy_domain(&self, domid: u32) -> Result<(), XenCallError> {
let domctl = DomCtl {
let mut domctl = DomCtl {
cmd: XEN_DOMCTL_DESTROYDOMAIN,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
value: DomCtlValue { pad: [0; 128] },
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
.hypercall1(HYPERVISOR_DOMCTL, addr_of_mut!(domctl) as c_ulong)?;
Ok(())
}
}

View File

@ -12,7 +12,7 @@ use std::ffi::{c_long, c_ulong, c_void};
use std::fmt::{Display, Formatter};
use std::fs::{File, OpenOptions};
use std::os::fd::AsRawFd;
use std::ptr::addr_of;
use std::ptr::addr_of_mut;
pub struct XenCall {
pub handle: File,
@ -143,7 +143,7 @@ impl XenCall {
pub fn mmap_batch(
&self,
domid: u32,
count: u64,
num: u64,
addr: u64,
mfns: Vec<u64>,
) -> Result<c_long, XenCallError> {
@ -151,7 +151,7 @@ impl XenCall {
let mut mfns = mfns.clone();
let mut errors = vec![0i32; mfns.len()];
let mut batch = MmapBatch {
num: count as u32,
num: num as u32,
domid: domid as u16,
addr,
mfns: mfns.as_mut_ptr(),
@ -163,13 +163,13 @@ impl XenCall {
}
pub fn get_version_capabilities(&self) -> Result<XenCapabilitiesInfo, XenCallError> {
let info = XenCapabilitiesInfo {
let mut info = XenCapabilitiesInfo {
capabilities: [0; 1024],
};
self.hypercall2(
HYPERVISOR_XEN_VERSION,
XENVER_CAPABILITIES,
addr_of!(info) as c_ulong,
addr_of_mut!(info) as c_ulong,
)?;
Ok(info)
}

View File

@ -3,7 +3,7 @@ use crate::{XenCall, XenCallError};
use std::ffi::c_ulong;
use std::ptr::addr_of;
use std::ptr::addr_of_mut;
pub struct MemoryControl<'a> {
call: &'a XenCall,
@ -22,19 +22,28 @@ impl MemoryControl<'_> {
mem_flags: u32,
extent_starts: &[u64],
) -> Result<Vec<u64>, XenCallError> {
let extent_starts = extent_starts.to_vec();
let reservation = MemoryReservation {
extent_start: addr_of!(extent_starts) as c_ulong,
let mut extent_starts = extent_starts.to_vec();
let mut reservation = MemoryReservation {
extent_start: extent_starts.as_mut_ptr() as c_ulong,
nr_extents,
extent_order,
mem_flags,
domid: domid as u16,
};
self.call.hypercall2(
let code = self.call.hypercall2(
HYPERVISOR_MEMORY_OP,
XEN_MEM_POPULATE_PHYSMAP as c_ulong,
addr_of!(reservation) as c_ulong,
addr_of_mut!(reservation) as c_ulong,
)?;
Ok(extent_starts)
if code < 0 {
return Err(XenCallError::new("failed to populate physmap"));
}
if code as usize > extent_starts.len() {
return Err(XenCallError::new("failed to populate physmap"));
}
Ok(extent_starts[0..code as usize].to_vec())
}
}