mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-02 21:00:55 +00:00
add support for hypercalls
This commit is contained in:
parent
47b924d618
commit
faf8027590
@ -1,6 +1,7 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"xenstore",
|
"xenstore",
|
||||||
"xenevtchn"
|
"xenevtchn",
|
||||||
|
"xencall"
|
||||||
]
|
]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
16
xencall/Cargo.toml
Normal file
16
xencall/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "xencall"
|
||||||
|
version = "0.0.1"
|
||||||
|
edition = "2021"
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies.nix]
|
||||||
|
version = "0.27.1"
|
||||||
|
features = ["ioctl"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "xencall-simple"
|
||||||
|
path = "examples/simple.rs"
|
11
xencall/examples/simple.rs
Normal file
11
xencall/examples/simple.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use std::ffi::c_ulong;
|
||||||
|
use std::ptr::addr_of;
|
||||||
|
use xencall::{XenCall, XenCallError};
|
||||||
|
|
||||||
|
fn main() -> Result<(), XenCallError> {
|
||||||
|
let mut call = XenCall::open()?;
|
||||||
|
let message = "Hello World";
|
||||||
|
let bytes = message.as_bytes();
|
||||||
|
call.hypercall3(18, 0, bytes.len() as c_ulong, addr_of!(bytes) as c_ulong)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
118
xencall/src/lib.rs
Normal file
118
xencall/src/lib.rs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
mod sys;
|
||||||
|
|
||||||
|
use crate::sys::Hypercall;
|
||||||
|
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;
|
||||||
|
|
||||||
|
pub struct XenCall {
|
||||||
|
pub handle: File,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct XenCallError {
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl XenCallError {
|
||||||
|
pub fn new(msg: &str) -> XenCallError {
|
||||||
|
XenCallError {
|
||||||
|
message: msg.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for XenCallError {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for XenCallError {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
&self.message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for XenCallError {
|
||||||
|
fn from(value: std::io::Error) -> Self {
|
||||||
|
XenCallError::new(value.to_string().as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Errno> for XenCallError {
|
||||||
|
fn from(value: Errno) -> Self {
|
||||||
|
XenCallError::new(value.to_string().as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl XenCall {
|
||||||
|
pub fn open() -> Result<XenCall, XenCallError> {
|
||||||
|
let file = OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.open("/dev/xen/privcmd")?;
|
||||||
|
Ok(XenCall { handle: file })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall(&mut self, op: c_ulong, arg: [c_ulong; 5]) -> Result<c_long, XenCallError> {
|
||||||
|
unsafe {
|
||||||
|
let mut call = Hypercall { op, arg, retval: 0 };
|
||||||
|
sys::hypercall(self.handle.as_raw_fd(), &mut call)?;
|
||||||
|
Ok(call.retval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall0(&mut self, op: c_ulong) -> Result<c_long, XenCallError> {
|
||||||
|
self.hypercall(op, [0, 0, 0, 0, 0])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall1(&mut self, op: c_ulong, arg1: c_ulong) -> Result<c_long, XenCallError> {
|
||||||
|
self.hypercall(op, [arg1, 0, 0, 0, 0])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall2(
|
||||||
|
&mut self,
|
||||||
|
op: c_ulong,
|
||||||
|
arg1: c_ulong,
|
||||||
|
arg2: c_ulong,
|
||||||
|
) -> Result<c_long, XenCallError> {
|
||||||
|
self.hypercall(op, [arg1, arg2, 0, 0, 0])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall3(
|
||||||
|
&mut self,
|
||||||
|
op: c_ulong,
|
||||||
|
arg1: c_ulong,
|
||||||
|
arg2: c_ulong,
|
||||||
|
arg3: c_ulong,
|
||||||
|
) -> Result<c_long, XenCallError> {
|
||||||
|
self.hypercall(op, [arg1, arg2, arg3, 0, 0])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall4(
|
||||||
|
&mut self,
|
||||||
|
op: c_ulong,
|
||||||
|
arg1: c_ulong,
|
||||||
|
arg2: c_ulong,
|
||||||
|
arg3: c_ulong,
|
||||||
|
arg4: c_ulong,
|
||||||
|
) -> Result<c_long, XenCallError> {
|
||||||
|
self.hypercall(op, [arg1, arg2, arg3, arg4, 0])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hypercall5(
|
||||||
|
&mut self,
|
||||||
|
op: c_ulong,
|
||||||
|
arg1: c_ulong,
|
||||||
|
arg2: c_ulong,
|
||||||
|
arg3: c_ulong,
|
||||||
|
arg4: c_ulong,
|
||||||
|
arg5: c_ulong,
|
||||||
|
) -> Result<c_long, XenCallError> {
|
||||||
|
self.hypercall(op, [arg1, arg2, arg3, arg4, arg5])
|
||||||
|
}
|
||||||
|
}
|
11
xencall/src/sys.rs
Normal file
11
xencall/src/sys.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use nix::{ioctl_readwrite_bad, request_code_none};
|
||||||
|
use std::ffi::{c_long, c_ulong};
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct Hypercall {
|
||||||
|
pub op: c_ulong,
|
||||||
|
pub arg: [c_ulong; 5],
|
||||||
|
pub retval: c_long,
|
||||||
|
}
|
||||||
|
|
||||||
|
ioctl_readwrite_bad!(hypercall, request_code_none!(b'E', 0), Hypercall);
|
Loading…
Reference in New Issue
Block a user