pub struct VirtualMachine<'a> { /* private fields */ }
Expand description
An individual virtual machine created through a Kvm
object.
Implementations§
Source§impl<'a> VirtualMachine<'a>
impl<'a> VirtualMachine<'a>
Sourcepub fn check_extension(&self, ext: int) -> Result<int>
pub fn check_extension(&self, ext: int) -> Result<int>
Query whether the KVM subsystem in the current kernel supports a particular extension for a specific VM.
A result of zero indicates a lack of support while nonzero indicates support. The nonzero value may carry additional meanings for some extensions.
Sourcepub fn create_vcpu(&self, cpu_id: int) -> Result<VirtualCpu<'_>>
pub fn create_vcpu(&self, cpu_id: int) -> Result<VirtualCpu<'_>>
Create a new VCPU for this VM.
If creating multiple VCPUs in the same VM, start with cpu_id
zero
and then increment for each new VM. The kernel enforces a
platform-specific limit on VCPUs per VM, which you can determine by
querying extensions using Self::check_extension
.
Examples found in repository?
examples/kvm-vm-setup.rs (line 44)
7fn run() -> linux_io::result::Result<()> {
8 let kvm = Kvm::open()?;
9 println!("opened KVM subsystem: {:?}", &kvm);
10 let version = kvm.get_api_version()?;
11 if version != 12 {
12 eprintln!("unsupported KVM API version {}", version);
13 return Err(linux_io::result::Error::new(25));
14 }
15 let mut vm = kvm.create_vm()?;
16 println!("created a VM: {:?}", &vm);
17
18 let mut mem = MemoryRegion::new(0x1000)?;
19 println!("created a memory region: {:?}", &mem);
20
21 {
22 let mem_data = mem.as_mut_slice();
23
24 #[cfg(target_arch = "x86_64")]
25 {
26 // "UD2" explicitly-undefined instruction
27 mem_data[0] = 0x0f;
28 mem_data[1] = 0x0b;
29 }
30
31 #[cfg(target_arch = "aarch64")]
32 {
33 // "udf" explicitly-undefined instruction
34 mem_data[0] = 0x01;
35 mem_data[1] = 0x00;
36 mem_data[2] = 0x00;
37 mem_data[3] = 0x00;
38 }
39 }
40
41 vm.set_guest_memory_region(0, KVM_MEM_READONLY, 0x1000, &mut mem)?;
42 println!("registered the memory region with the VM");
43
44 let cpu = vm.create_vcpu(0)?;
45 println!("created a VCPU: {:?}", &cpu);
46 let mut runner = cpu.to_runner()?;
47 println!("created a VCPU runner: {:?}", &runner);
48 runner.with_raw_run_state(|state| println!("run state: {:?}", &state));
49
50 runner.modify_regs(|regs| {
51 #[cfg(target_arch = "x86_64")]
52 {
53 regs.rip = 0x1000;
54 }
55 #[cfg(target_arch = "aarch64")]
56 {
57 regs.regs.pc = 0x1000;
58 }
59 })?;
60 println!("set initial register values");
61
62 #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
63 {
64 runner.run_raw()?;
65 runner.with_raw_run_state(|state| println!("run state after running: {:?}", &state));
66 let regs = runner.get_regs()?;
67 println!("registers after running: {:?}", ®s)
68 }
69
70 Ok(())
71}
Sourcepub fn set_guest_memory_region<'r: 'a>(
&mut self,
slot: u32,
flags: u32,
guest_phys_addr: u64,
host_region: &'r mut MemoryRegion,
) -> Result<()>
pub fn set_guest_memory_region<'r: 'a>( &mut self, slot: u32, flags: u32, guest_phys_addr: u64, host_region: &'r mut MemoryRegion, ) -> Result<()>
Sets one of the VM’s memory region slots to refer to the given memory region, which must outlive this VCPU.
Examples found in repository?
examples/kvm-vm-setup.rs (line 41)
7fn run() -> linux_io::result::Result<()> {
8 let kvm = Kvm::open()?;
9 println!("opened KVM subsystem: {:?}", &kvm);
10 let version = kvm.get_api_version()?;
11 if version != 12 {
12 eprintln!("unsupported KVM API version {}", version);
13 return Err(linux_io::result::Error::new(25));
14 }
15 let mut vm = kvm.create_vm()?;
16 println!("created a VM: {:?}", &vm);
17
18 let mut mem = MemoryRegion::new(0x1000)?;
19 println!("created a memory region: {:?}", &mem);
20
21 {
22 let mem_data = mem.as_mut_slice();
23
24 #[cfg(target_arch = "x86_64")]
25 {
26 // "UD2" explicitly-undefined instruction
27 mem_data[0] = 0x0f;
28 mem_data[1] = 0x0b;
29 }
30
31 #[cfg(target_arch = "aarch64")]
32 {
33 // "udf" explicitly-undefined instruction
34 mem_data[0] = 0x01;
35 mem_data[1] = 0x00;
36 mem_data[2] = 0x00;
37 mem_data[3] = 0x00;
38 }
39 }
40
41 vm.set_guest_memory_region(0, KVM_MEM_READONLY, 0x1000, &mut mem)?;
42 println!("registered the memory region with the VM");
43
44 let cpu = vm.create_vcpu(0)?;
45 println!("created a VCPU: {:?}", &cpu);
46 let mut runner = cpu.to_runner()?;
47 println!("created a VCPU runner: {:?}", &runner);
48 runner.with_raw_run_state(|state| println!("run state: {:?}", &state));
49
50 runner.modify_regs(|regs| {
51 #[cfg(target_arch = "x86_64")]
52 {
53 regs.rip = 0x1000;
54 }
55 #[cfg(target_arch = "aarch64")]
56 {
57 regs.regs.pc = 0x1000;
58 }
59 })?;
60 println!("set initial register values");
61
62 #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
63 {
64 runner.run_raw()?;
65 runner.with_raw_run_state(|state| println!("run state after running: {:?}", &state));
66 let regs = runner.get_regs()?;
67 println!("registers after running: {:?}", ®s)
68 }
69
70 Ok(())
71}
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for VirtualMachine<'a>
impl<'a> RefUnwindSafe for VirtualMachine<'a>
impl<'a> Send for VirtualMachine<'a>
impl<'a> Sync for VirtualMachine<'a>
impl<'a> Unpin for VirtualMachine<'a>
impl<'a> UnwindSafe for VirtualMachine<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more