#memory

vm-memory

Safe abstractions for accessing the VM physical memory

6 releases

new 0.2.2 Aug 5, 2020
0.2.1 May 28, 2020
0.2.0 Mar 19, 2020
0.1.2 Aug 5, 2020
0.1.0 Mar 3, 2020

#3 in Emulators

Download history 760/week @ 2020-04-21 1217/week @ 2020-04-28 1145/week @ 2020-05-05 1306/week @ 2020-05-12 1362/week @ 2020-05-19 1268/week @ 2020-05-26 1066/week @ 2020-06-02 1297/week @ 2020-06-09 551/week @ 2020-06-16 400/week @ 2020-06-23 545/week @ 2020-06-30 353/week @ 2020-07-07 566/week @ 2020-07-14 655/week @ 2020-07-21 411/week @ 2020-07-28 460/week @ 2020-08-04

3,488 downloads per month
Used in versionize

Apache-2.0 OR BSD-3-Clause

215KB
3.5K SLoC

vm-memory

Design

In a typical Virtual Machine Monitor (VMM) there are several components, such as boot loader, virtual device drivers, virtio backend drivers and vhost drivers, that need to access the VM physical memory. The vm-memory crate provides a set of traits to decouple VM memory consumers from VM memory providers. Based on these traits, VM memory consumers can access the physical memory of the VM without knowing the implementation details of the VM memory provider. Thus VMM components based on these traits can be shared and reused by multiple virtualization solutions.

The detailed design of the vm-memory crate can be found here.

Platform Support

  • Arch: x86, AMD64, ARM64
  • OS: Linux/Unix/Windows

Usage

Add vm-memory as a dependency in Cargo.toml

[dependencies]
vm-memory = "*"

Then add extern crate vm-memory; to your crate root.

Examples

  • Creating a VM physical memory object in hypervisor specific ways using the GuestMemoryMmap implementation of the GuestMemory trait:
fn provide_mem_to_virt_dev() {
    let gm = GuestMemoryMmap::from_ranges(&[
        (GuestAddress(0), 0x1000),
        (GuestAddress(0x1000), 0x1000)
    ]).unwrap();
    virt_device_io(&gm);
}
  • Consumers accessing the VM's physical memory:
fn virt_device_io<T: GuestMemory>(mem: &T) {
    let sample_buf = &[1, 2, 3, 4, 5];
    assert_eq!(mem.write(sample_buf, GuestAddress(0xffc)).unwrap(), 5);
    let buf = &mut [0u8; 5];
    assert_eq!(mem.read(buf, GuestAddress(0xffc)).unwrap(), 5);
    assert_eq!(buf, sample_buf);
}

License

This project is licensed under either of

Dependencies

~85KB