aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/kvm.rs
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2021-05-26 00:21:06 +0200
committerjohannst <johannes.stoelp@gmail.com>2021-05-26 00:21:06 +0200
commit786a195f8e81d4f7c0af2a82b9d458361d424a71 (patch)
treec0df3de9a71e3ae1db1cdf4a59cde6d1accadf33 /src/kvm.rs
downloadmini-kvm-rs-786a195f8e81d4f7c0af2a82b9d458361d424a71.tar.gz
mini-kvm-rs-786a195f8e81d4f7c0af2a82b9d458361d424a71.zip
minimal KVM abstraction + real mode guest example
Diffstat (limited to 'src/kvm.rs')
-rw-r--r--src/kvm.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/kvm.rs b/src/kvm.rs
new file mode 100644
index 0000000..3522adc
--- /dev/null
+++ b/src/kvm.rs
@@ -0,0 +1,39 @@
+use std::fs;
+use std::io;
+use std::os::unix::io::FromRawFd;
+
+use crate::{libcret, ioctl, kvm_sys};
+use crate::vm::Vm;
+
+pub struct Kvm {
+ kvm: fs::File,
+}
+
+impl Kvm {
+ pub fn new() -> io::Result<Kvm> {
+ let kvm = libcret(unsafe {
+ libc::open("/dev/kvm\0".as_ptr().cast(), libc::O_RDWR | libc::O_CLOEXEC)
+ })
+ .map(|fd| unsafe { fs::File::from_raw_fd(fd) })?;
+
+ assert_eq!(
+ kvm_sys::KVM_API_VERSION,
+ ioctl(&kvm, kvm_sys::KVM_GET_API_VERSION, 0)?
+ );
+
+ Ok(Kvm { kvm })
+ }
+
+ fn get_vpcu_mmap_size(&self) -> io::Result<usize> {
+ ioctl(&self.kvm, kvm_sys::KVM_GET_VCPU_MMAP_SIZE, 0).map(|size| size as usize)
+ }
+
+ pub fn create_vm(&self) -> io::Result<Vm> {
+ let vm = ioctl(&self.kvm, kvm_sys::KVM_CREATE_VM, 0 /* machine id */)
+ .map(|fd| unsafe { fs::File::from_raw_fd(fd) })?;
+
+ let vcpu_mmap_size = self.get_vpcu_mmap_size()?;
+
+ Ok(Vm::new(vm, vcpu_mmap_size))
+ }
+}