aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/vm.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/vm.rs
downloadmini-kvm-rs-786a195f8e81d4f7c0af2a82b9d458361d424a71.tar.gz
mini-kvm-rs-786a195f8e81d4f7c0af2a82b9d458361d424a71.zip
minimal KVM abstraction + real mode guest example
Diffstat (limited to 'src/vm.rs')
-rw-r--r--src/vm.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/vm.rs b/src/vm.rs
new file mode 100644
index 0000000..6f8a355
--- /dev/null
+++ b/src/vm.rs
@@ -0,0 +1,45 @@
+use std::fs;
+use std::io;
+use std::os::unix::io::FromRawFd;
+
+use crate::vcpu::Vcpu;
+use crate::{ioctl, kvm_sys, KvmRun, PhysAddr, UserMem};
+
+pub struct Vm {
+ vm: fs::File,
+ vcpu_mmap_size: usize,
+}
+
+impl Vm {
+ pub(crate) fn new(vm: fs::File, vcpu_mmap_size: usize) -> Vm {
+ Vm { vm, vcpu_mmap_size }
+ }
+
+ pub unsafe fn set_user_memory_region(
+ &self,
+ phys_addr: PhysAddr,
+ mem: &UserMem,
+ ) -> io::Result<()> {
+ // Create guest physical memory mapping for `slot : 0` at guest `phys addr : 0`.
+ let mut kvm_mem = kvm_sys::kvm_userspace_memory_region::default();
+ kvm_mem.userspace_addr = mem.ptr as u64;
+ kvm_mem.memory_size = mem.len as u64;
+ kvm_mem.guest_phys_addr = phys_addr.0;
+
+ ioctl(
+ &self.vm,
+ kvm_sys::KVM_SET_USER_MEMORY_REGION,
+ &kvm_mem as *const _ as u64,
+ )
+ .map(|_| ())
+ }
+
+ pub fn create_vpcu(&self, id: u64) -> io::Result<Vcpu> {
+ let vcpu = ioctl(&self.vm, kvm_sys::KVM_CREATE_VCPU, id)
+ .map(|fd| unsafe { fs::File::from_raw_fd(fd) })?;
+
+ let kvm_run = KvmRun::new(&vcpu, self.vcpu_mmap_size)?;
+
+ Ok(Vcpu::new(vcpu, kvm_run))
+ }
+}