• Stars
    star
    284
  • Rank 140,230 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Virtual machine's guest memory crate

vm-memory

crates.io docs.rs

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

Xen support

Supporting Xen requires special handling while mapping the guest memory and hence a separate feature is provided in the crate: xen. Mapping the guest memory for Xen requires an ioctl() to be issued along with mmap() for the memory area. The arguments for the ioctl() are received via the vhost-user protocol's memory region area.

Xen allows two different mapping models: Foreign and Grant.

In Foreign mapping model, the entire guest address space is mapped at once, in advance. In Grant mapping model, the memory for few regions, like those representing the virtqueues, is mapped in advance. The rest of the memory regions are mapped (partially) only while accessing the buffers and the same is immediately deallocated after the buffer is accessed. Hence, special handling for the same in VolatileMemory.rs.

In order to still support standard Unix memory regions, for special regions and testing, the Xen specific implementation here allows a third mapping type: MmapXenFlags::UNIX. This performs standard Unix memory mapping and the same is used for all tests in this crate.

It was decided by the rust-vmm maintainers to keep the interface simple and build the crate for either standard Unix memory mapping or Xen, and not both.

Xen is only supported for Unix platforms.

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

More Repositories

1

community

rust-vmm community content
459
star
2

vm-virtio

virtio implementation
Rust
334
star
3

kvm-ioctls

Rust
248
star
4

linux-loader

Linux kernel loader
Rust
174
star
5

vmm-reference

A VMM implementation based of rust-vmm components
Rust
143
star
6

vhost

Rust
121
star
7

kvm-bindings

Rust
113
star
8

vmm-sys-util

Helpers and utilities used by multiple rust-vmm components and VMMs
Rust
78
star
9

rust-vmm-container

Container with all dependencies required for running rust-vmm crates integration tests.
Shell
60
star
10

seccompiler

Provides easy-to-use Linux seccomp-bpf jailing.
Rust
58
star
11

vhost-device

'vhost-user' device backends workspace
Rust
54
star
12

event-manager

Rust
43
star
13

vm-device

Rust
37
star
14

vm-superio

Emulation for legacy devices
Rust
30
star
15

vm-allocator

Provides allocations and release strategies for resources used during the lifetime of a VM.
Rust
29
star
16

mshv

Crates for Microsoft Hypervisor ioctls and bindings
Rust
28
star
17

rust-vmm-ci

Python
18
star
18

vhost-user-backend

Deprecated repository, code now lives in: https://github.com/rust-vmm/vhost/
Rust
18
star
19

virtio-bindings

This crate is now part of the vm-virtio workspace: https://github.com/rust-vmm/vm-virtio
Rust
15
star
20

vfio-ioctls

Safe wrappers for VFIO
Rust
15
star
21

vfio

Rust
13
star
22

crate-template

This is the template used by all repositories which represent a crate. The purpose is to speed up the creation of new crates and keep the same standard in rust-vmm.
Rust
12
star
23

vfio-bindings

Rust
11
star
24

vm-fdt

Rust
11
star
25

acpi_tables

Rust
9
star
26

xen-sys

Rust
9
star
27

kvm

Rust
8
star
28

vmm-vcpu

7
star
29

kvm_wrapper

Deprecated. Use https://github.com/rust-vmm/kvm-bindings instead.
Rust
6
star
30

vfio-user

Rust
5
star
31

vm-pci

Rust
2
star
32

io-rate-limiter

Rust
1
star
33

.github

1
star