Rust API to the OS X Hypervisor framework for hardware-accelerated virtualization

Overview

hypervisor-rs

hypervisor is a Rust library that taps into functionality that enables hardware-accelerated execution of virtual machines on OS X.

It binds to the Hypervisor framework on OS X, and exposes a safe Rust interface through the hypervisor module, and an unsafe foreign function interface through the hypervisor::ffi module.

Documentation

Prerequisites

To use this library, you need

  • OS X Yosemite (10.10), or newer

  • an Intel processor with the VT-x feature set that includes Extended Page Tables (EPT) and the Unrestricted Mode. To verify this, run and expect the following in your Terminal:

    $ sysctl kern.hv_support
    kern.hv_support: 1

Status

  • Accessing x86 registers
  • Accessing model-specific registers (MSRs)
  • Mapping guest physical memory segments into guest physical address space
  • Virtual CPUs
    • Executing and interrupting
    • Force flushing cached state
    • Invalidating translation lookaside buffer (TLB)
    • Accessing floating point (FP) and SIMD state
    • Obtaining cumulative execution time
    • Synchronizing guest timestamp-counters (TSC)
  • Accessing fields of Virtual Machine Control Structures (VMCS)
You might also like...
BM - a basic virtual machine written in rust

A basic virtual machine implementation with it's own binary format and assembly, as a learning experience.

LC3 Virtual Machine written in Rust 🦀

LC3 - Emulator LC3-rust is a virtual machine for the Little Computer 3 architecture, written using the Rust programming language. The VM has been writ

little computer 3 (lc3) virtual machine written in Rust

Little Computer 3 (LC3) Virtual Machine (by @lowlevelers) What is Little Computer 3? Little Computer 3, or LC-3, is a type of computer educational pro

virtualization-rs provides the API of the Apple Virtualization.framework in Rust language.
virtualization-rs provides the API of the Apple Virtualization.framework in Rust language.

virtualization-rs Rust bindings for Virtualization.framework virtualization-rs provides the API of the Apple Virtualization.framework in Rust language

Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.
Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.

Speedy2D Hardware-accelerated drawing of shapes, images, and text, with an easy to use API. Speedy2D aims to be: The simplest Rust API for creating a

Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

A hardware-accelerated GPU terminal emulator powered by WebGPU, focusing to run in desktops, browsers, tvs and everywhere.
A hardware-accelerated GPU terminal emulator powered by WebGPU, focusing to run in desktops, browsers, tvs and everywhere.

Rio term tl;dr: Rio is a terminal built to run everywhere, as a native desktop applications by Rust/WebGPU or even in the browser powered by WebAssemb

V86 - x86 virtualization in your browser, recompiling x86 to wasm on the fly

v86 emulates an x86-compatible CPU and hardware. Machine code is translated to WebAssembly modules at runtime in order to achieve decent performance.

An Intel HAXM powered, protected mode, 32 bit, hypervisor addition calculator, written in Rust.

HyperCalc An Intel HAXM powered, protected mode, 32 bit, hypervisor addition calculator, written in Rust. Purpose None 😏 . Mostly just to learn Rust

crosvm is a virtual machine monitor (VMM) based on Linux’s KVM hypervisor

crosvm - The Chrome OS Virtual Machine Monitor crosvm is a virtual machine monitor (VMM) based on Linux’s KVM hypervisor, with a focus on simplicity,

hy-rs, pronounced high rise, provides a unified and portable to the hypervisor APIs provided by various platforms.

Introduction The hy-rs crate, pronounced as high rise, provides a unified and portable interface to the hypervisor APIs provided by various platforms.

A thin-hypervisor that runs on aarch64 CPUs.

How to build the hypervisor By Rust toolchain (TBD) By docker Requirements Docker (Tested by Docker version 20.10.8, build 3967b7d28e) I tested by non

A simple hypervisor demonstrating the use of the Intel VT-rp (redirect protection) technology.

Hello-VT-rp A simple hypervisor demonstrating the use of the Intel VT-rp (redirect protection) technology. This repository is a complement of the blob

Risc-V hypervisor for TEE development

A micro hypervisor for RISC-V systems. Quick Start Building (using Bazel) git submodule update --init bazel build //:salus-all Running Prerequisites S

A single-binary, GPU-accelerated LLM server (HTTP and WebSocket API) written in Rust
A single-binary, GPU-accelerated LLM server (HTTP and WebSocket API) written in Rust

Poly Poly is a versatile LLM serving back-end. What it offers: High-performance, efficient and reliable serving of multiple local LLM models Optional

Retina is a network analysis framework that supports 100+ Gbps traffic analysis on a single server with no specialized hardware.

Retina Retina is a network analysis framework that enables operators and researchers to ask complex questions about high-speed (100gbE) network links

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust

Wez's Terminal A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust User facing docs and guide a

Rust edit distance routines accelerated using SIMD. Supports fast Hamming, Levenshtein, restricted Damerau-Levenshtein, etc. distance calculations and string search.

triple_accel Rust edit distance routines accelerated using SIMD. Supports fast Hamming, Levenshtein, restricted Damerau-Levenshtein, etc. distance cal

SIMD-accelerated UTF-8 validation for Rust.
SIMD-accelerated UTF-8 validation for Rust.

simdutf8 – High-speed UTF-8 validation for Rust Blazingly fast API-compatible UTF-8 validation for Rust using SIMD extensions, based on the implementa

Comments
  • Need a simple example

    Need a simple example

    I used this crate, with the code below, and it seems there must be something wrong in using this crate, can you help?

    extern crate hypervisor;
    
    use hypervisor::vCPU;
    use hypervisor::VMXCap;
    use hypervisor::read_vmx_cap;
    use hypervisor::consts::vmcs;
    use hypervisor::consts::vmx_exit;
    use hypervisor::map_mem;
    use hypervisor::MemPerm;
    use hypervisor::x86Reg;
    use hypervisor::Error;
    
    fn cap2ctrl(cap: u64, ctrl: u64) -> u64 {
        return ((ctrl) | ((cap) & 0xffffffff)) & ((cap) >> 32);
    }
    
    fn req(e: Error) {
        match e {
            Error::Success => println!("Exited OK"),
            _ => println!("Exited Error")
        }
    }
    
    const VMCS_PRI_PROC_BASED_CTLS_HLT: u64 = (1 << 7);
    const VMCS_PRI_PROC_BASED_CTLS_CR8_LOAD: u64 = (1 << 19);
    const VMCS_PRI_PROC_BASED_CTLS_CR8_STORE: u64 = (1 << 20);
    
    fn main() {
        hypervisor::create_vm();
        let cpu = vCPU::new().expect("Failed create vCPU");
        let cap_pinbased_type = VMXCap::PINBASED;
        let cap_procbased_type = VMXCap::PROCBASED;
        let cap_procbased2_type = VMXCap::PROCBASED2;
        let cap_entry_type = VMXCap::ENTRY;
        let cap_pinbased: u64;
        let cap_procbased: u64;
        let cap_procbased2: u64;
        let cap_entry: u64;
        cap_pinbased = read_vmx_cap(&cap_pinbased_type).expect("Reading cap error");
        cap_procbased = read_vmx_cap(&cap_procbased_type).expect("Reading cap error");
        cap_procbased2 = read_vmx_cap(&cap_procbased2_type).expect("Reading cap error");
        cap_entry = read_vmx_cap(&cap_entry_type).expect("Reading cap error");
        
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_PIN_BASED, cap2ctrl(cap_pinbased, 0)));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_CPU_BASED, cap2ctrl(cap_procbased,
            VMCS_PRI_PROC_BASED_CTLS_HLT |
            VMCS_PRI_PROC_BASED_CTLS_CR8_LOAD |
            VMCS_PRI_PROC_BASED_CTLS_CR8_STORE)));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_CPU_BASED2, cap2ctrl(cap_procbased2, 0) | (1 << 7)));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_VMENTRY_CONTROLS, cap2ctrl(cap_entry, 0)));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_EXC_BITMAP, 0xffffffff));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_CR0_MASK, 0xffffffff));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_CR0_SHADOW, 0xffffffff));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_CR4_MASK, 0xffffffff));
        req(cpu.write_vmcs(vmcs::VMCS_CTRL_CR4_SHADOW, 0xffffffff));
    
        req(cpu.write_vmcs(vmcs::VMCS_GUEST_CS, 1 << 3));
        req(cpu.write_vmcs(vmcs::VMCS_GUEST_CS_AR, 0xc093));
        req(cpu.write_vmcs(vmcs::VMCS_GUEST_CS_LIMIT, 0xffffffff));
        cpu.write_vmcs(vmcs::VMCS_GUEST_CS_BASE, 0x0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_DS, 2 << 3);
        cpu.write_vmcs(vmcs::VMCS_GUEST_DS_AR, 0xc093);
        cpu.write_vmcs(vmcs::VMCS_GUEST_DS_LIMIT, 0xffffffff);
        cpu.write_vmcs(vmcs::VMCS_GUEST_DS_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_ES, 2 << 3);
        cpu.write_vmcs(vmcs::VMCS_GUEST_ES_AR, 0xc093);
        cpu.write_vmcs(vmcs::VMCS_GUEST_ES_LIMIT, 0xffffffff);
        cpu.write_vmcs(vmcs::VMCS_GUEST_ES_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_FS, 2 << 3);
        cpu.write_vmcs(vmcs::VMCS_GUEST_FS_AR, 0xc093);
        cpu.write_vmcs(vmcs::VMCS_GUEST_FS_LIMIT, 0xffffffff);
        cpu.write_vmcs(vmcs::VMCS_GUEST_FS_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_GS, 2 << 3);
        cpu.write_vmcs(vmcs::VMCS_GUEST_GS_AR, 0xc093);
        cpu.write_vmcs(vmcs::VMCS_GUEST_GS_LIMIT, 0xffffffff);
        cpu.write_vmcs(vmcs::VMCS_GUEST_GS_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_SS, 2 << 3);
        cpu.write_vmcs(vmcs::VMCS_GUEST_SS_AR, 0xc093);
        cpu.write_vmcs(vmcs::VMCS_GUEST_SS_LIMIT, 0xffffffff);
        cpu.write_vmcs(vmcs::VMCS_GUEST_SS_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_LDTR, 0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_LDTR_LIMIT, 0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_LDTR_AR, 0x10000);
        cpu.write_vmcs(vmcs::VMCS_GUEST_LDTR_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_TR, 0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_TR_LIMIT, 0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_TR_AR, 0x83);
        cpu.write_vmcs(vmcs::VMCS_GUEST_LDTR_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_GDTR_LIMIT, 0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_GDTR_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_IDTR_LIMIT, 0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_IDTR_BASE, 0);
    
        cpu.write_vmcs(vmcs::VMCS_GUEST_CR0, 0x20);
        cpu.write_vmcs(vmcs::VMCS_GUEST_CR3, 0x0);
        cpu.write_vmcs(vmcs::VMCS_GUEST_CR4, 0x2000);
    
        //     mov ax, 0x1234
        //     nop
        //     nop
        //     hlt
    
        let vm_mem: Vec<u8> = vec![0xB8, 0x34, 0x12, 0x90, 0x90, 0xF4]; 
        // ### I think i am doing wrong here?
        match map_mem(&vm_mem as &[u8], 0, &MemPerm::ExecAndWrite) {
            Error::Success => println!("map_mem() OK"),
            _ => println!("map_mem() error")
        }
        
        req(cpu.write_register(&x86Reg::RIP, 0));
        req(cpu.write_register(&x86Reg::RFLAGS, 0x2));
        req(cpu.write_register(&x86Reg::RAX, 0));
    
        loop {
            match cpu.run() {
                Error::Success => println!("Cpu running..."),
                _ => println!("Running cpu error")
            }
            let exit_reason = cpu.read_vmcs(vmcs::VMCS_RO_EXIT_REASON).expect("read vmcs error");
            println!("Exit reason: {:x}, {}", exit_reason, exit_reason);
            if exit_reason == vmx_exit::VMX_REASON_EPT_VIOLATION ||
                 exit_reason == vmx_exit::VMX_REASON_IRQ {
                    continue;
            }
            break;
        }
    
        let v = cpu.read_register(&x86Reg::RAX).expect("Read register error");
        println!("RAX = {:x}", v);
        let ip = cpu.read_register(&x86Reg::RIP).expect("Read register error");
        println!("RIP = {:x}", ip);
        println!("Done.");
    }
    

    RAX should be 0x90901234 RIP should be 0x5

    opened by orklann 2
Owner
Saurav Sachidanand
Interned at HackerRank '19, previously at GSoC '18 with CERN and '17 with Performance Co-Pilot
Saurav Sachidanand
Aftermath - a high-performance JVM for the TapVM framework written in the Rust

Aftermath Welcome to the source, my friend! Note -> This README is targetted towards users, not contributors. Please read the docs present at docs/for

null 6 Aug 1, 2022
RailCar: Rust implementation of the Open Containers Initiative oci-runtime

railcar - rust implementation of the oci-runtime spec What is railcar? railcar is a rust implementation of the opencontainers initiative's runtime spe

Oracle 1.1k Dec 21, 2022
Advanced Rust quantum computer simulator

quantum Advanced Rust quantum computer simulator. Motivation Quantum is a quantum computer simulator written with the following design goals in mind:

Ben Eills 215 Jan 1, 2023
Rust bindings for the unicorn CPU emulator

unicorn-rs THIS PACKAGE IS DEPRECATED AND NO LONGER MAINTAINED. Rust bindings are now included with unicorn and will be maintained there from now on.

null 129 Oct 10, 2022
StarWM is an extensible window manager written in Rust.

StarWM is an extensible, floating and tiling, X window manager for Linux-based operating systems written in Rust.

StarWM 30 Dec 9, 2022
An LC-3 virtual machine written in Rust for learning purposes.

LC-3 written in Rust An LC-3 virtual machine written in Rust for learning purposes.

Rodrigo Araújo 30 Dec 11, 2022
Revolutionary Machine (revm) is a fast Ethereum virtual machine written in rust.

revm - Revolutionary Machine Is Rust Ethereum Virtual Machine with great name that is focused on speed and simplicity.

null 513 Dec 31, 2022
Dragonball-sandbox is a collection of Rust crates to help build custom Virtual Machine Monitors and hypervisors

Dragonball-sandbox is a collection of Rust crates to help build custom Virtual Machine Monitors and hypervisors. The crates here are considered to be the downstream of rust-vmm.

OpenAnolis Community 62 Dec 30, 2022
A toy Cairo VM implementation in Rust

oriac A toy Cairo VM implementation in Rust This is a toy implementation of the Cairo VM I'm building for learning purposes. There's no guarantee on w

Jonathan LEI 57 Nov 23, 2022
A Rust implementation of the Lox programming language. Based on clox, the bytecode virtual machine.

A Rust implementation of the Lox programming language. Based on clox, the bytecode virtual machine.

Diego Freijo 23 Dec 26, 2022