implement elf loader

This commit is contained in:
Alex Zenla
2024-01-09 22:39:32 -08:00
parent 35f3346858
commit 19b797f1a2
11 changed files with 383 additions and 25 deletions

View File

@ -22,3 +22,7 @@ path = "examples/domain_info.rs"
[[example]]
name = "xencall-domain-create"
path = "examples/domain_create.rs"
[[example]]
name = "xencall-version-capabilities"
path = "examples/version_capabilities.rs"

View File

@ -0,0 +1,8 @@
use xencall::{XenCall, XenCallError};
fn main() -> Result<(), XenCallError> {
let mut call = XenCall::open()?;
let info = call.get_version_capabilities()?;
println!("{:?}", info);
Ok(())
}

View File

@ -1,6 +1,7 @@
use crate::sys::{
ArchDomainConfig, CreateDomain, DomCtl, DomCtlValue, GetDomainInfo, HYPERVISOR_DOMCTL,
XEN_DOMCTL_CREATEDOMAIN, XEN_DOMCTL_GETDOMAININFO, XEN_DOMCTL_INTERFACE_VERSION,
ArchDomainConfig, CreateDomain, DomCtl, DomCtlValue, GetDomainInfo, MaxMem, MaxVcpus,
HYPERVISOR_DOMCTL, XEN_DOMCTL_CREATEDOMAIN, XEN_DOMCTL_GETDOMAININFO,
XEN_DOMCTL_INTERFACE_VERSION, XEN_DOMCTL_MAX_MEM, XEN_DOMCTL_MAX_VCPUS,
};
use crate::{XenCall, XenCallError};
use std::ffi::c_ulong;
@ -70,4 +71,32 @@ impl DomainControl<'_> {
domid: domctl.domid,
})
}
pub fn set_max_mem(&mut self, domid: u32, memkb: u64) -> Result<(), XenCallError> {
let domctl = DomCtl {
cmd: XEN_DOMCTL_MAX_MEM,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
value: DomCtlValue {
max_mem: MaxMem { max_memkb: memkb },
},
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
Ok(())
}
pub fn set_max_vcpus(&mut self, domid: u32, max_vcpus: u32) -> Result<(), XenCallError> {
let domctl = DomCtl {
cmd: XEN_DOMCTL_MAX_VCPUS,
interface_version: XEN_DOMCTL_INTERFACE_VERSION,
domid,
value: DomCtlValue {
max_cpus: MaxVcpus { max_vcpus },
},
};
self.call
.hypercall1(HYPERVISOR_DOMCTL, addr_of!(domctl) as c_ulong)?;
Ok(())
}
}

View File

@ -1,13 +1,16 @@
pub mod domctl;
pub mod sys;
use crate::sys::Hypercall;
use crate::sys::{
Hypercall, Mmap, XenCapabilitiesInfo, HYPERVISOR_XEN_VERSION, XENVER_CAPABILITIES,
};
use nix::errno::Errno;
use std::error::Error;
use std::ffi::{c_long, c_ulong};
use std::fmt::{Display, Formatter};
use std::fs::{File, OpenOptions};
use std::os::fd::AsRawFd;
use std::ptr::addr_of;
pub struct XenCall {
pub handle: File,
@ -116,4 +119,24 @@ impl XenCall {
) -> Result<c_long, XenCallError> {
self.hypercall(op, [arg1, arg2, arg3, arg4, arg5])
}
pub fn mmap(&mut self, mmap: Mmap) -> Result<c_long, XenCallError> {
unsafe {
let mut mmap = mmap.clone();
let result = sys::mmap(self.handle.as_raw_fd(), &mut mmap)?;
Ok(result as c_long)
}
}
pub fn get_version_capabilities(&mut self) -> Result<XenCapabilitiesInfo, XenCallError> {
let info = XenCapabilitiesInfo {
capabilities: [0; 1024],
};
self.hypercall2(
HYPERVISOR_XEN_VERSION,
XENVER_CAPABILITIES,
addr_of!(info) as c_ulong,
)?;
Ok(info)
}
}

View File

@ -1,6 +1,6 @@
/// Handwritten hypercall bindings.
use nix::ioctl_readwrite_bad;
use std::ffi::c_ulong;
use std::ffi::{c_char, c_int, c_ulong};
use uuid::Uuid;
#[repr(C)]
@ -10,9 +10,27 @@ pub struct Hypercall {
pub arg: [c_ulong; 5],
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct MmapEntry {
pub va: u64,
pub mfn: u64,
pub npages: u64,
}
#[repr(C)]
#[derive(Clone, Debug)]
pub struct Mmap {
pub num: c_int,
pub dom: u16,
pub entry: *mut MmapEntry,
}
const IOCTL_PRIVCMD_HYPERCALL: u64 = 0x305000;
const IOCTL_PRIVCMD_MMAP: u64 = 0x105002;
ioctl_readwrite_bad!(hypercall, IOCTL_PRIVCMD_HYPERCALL, Hypercall);
ioctl_readwrite_bad!(mmap, IOCTL_PRIVCMD_MMAP, Mmap);
pub const HYPERVISOR_SET_TRAP_TABLE: c_ulong = 0;
pub const HYPERVISOR_MMU_UPDATE: c_ulong = 1;
@ -175,6 +193,8 @@ pub struct DomCtl {
pub union DomCtlValue {
pub create_domain: CreateDomain,
pub get_domain_info: GetDomainInfo,
pub max_mem: MaxMem,
pub max_cpus: MaxVcpus,
}
#[repr(C)]
@ -194,6 +214,28 @@ pub struct CreateDomain {
pub arch_domain_config: ArchDomainConfig,
}
impl Default for CreateDomain {
fn default() -> Self {
CreateDomain {
ssidref: SECINITSID_DOMU,
handle: Uuid::new_v4().into_bytes(),
flags: 0,
iommu_opts: 0,
max_vcpus: 1,
max_evtchn_port: 1023,
max_grant_frames: -1,
max_maptrack_frames: -1,
grant_opts: 2,
vmtrace_size: 0,
cpupool_id: 0,
arch_domain_config: ArchDomainConfig {
emulation_flags: 0,
misc_flags: 0,
},
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct GetDomainInfo {
@ -223,27 +265,25 @@ pub struct ArchDomainConfig {
pub misc_flags: u32,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct MaxMem {
pub max_memkb: u64,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct MaxVcpus {
pub max_vcpus: u32,
}
pub const XEN_DOMCTL_INTERFACE_VERSION: u32 = 0x00000015;
pub const SECINITSID_DOMU: u32 = 13;
impl Default for CreateDomain {
fn default() -> Self {
CreateDomain {
ssidref: SECINITSID_DOMU,
handle: Uuid::new_v4().into_bytes(),
flags: 0,
iommu_opts: 0,
max_vcpus: 1,
max_evtchn_port: 1023,
max_grant_frames: -1,
max_maptrack_frames: -1,
grant_opts: 2,
vmtrace_size: 0,
cpupool_id: 0,
arch_domain_config: ArchDomainConfig {
emulation_flags: 0,
misc_flags: 0,
},
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct XenCapabilitiesInfo {
pub capabilities: [c_char; 1024],
}
pub const XENVER_CAPABILITIES: u64 = 3;