Run virtual environment using VMX in C++

Viewed 43

I was curious if it's possible to start a virtual environment that can do something like this:

int main() {
  std::cout << "This part is being ran on the host";

  StartVM();

  std::cout << "This part is being ran on a VM";

  EndVM();

  return 0;
}

I've read on some documentation on Intel-VT's VMX operations, but it's a level too far above my understanding of the intricacies of x86 hardware and x86 assembly.

I've tried to run the code in the OSDev wiki but i'm having trouble trying to manually enable the corresponding bits in the article using inline assembly to make the vmxon instruction work.

If there's a library or an API in C++ (or even in C) that does this, please let me know. Also, if I'm deeply misunderstanding something about VMX or virtualisation in general, then please point me to the right direction.

1 Answers

A research paper called dune implements what you describe.

It achieves so with a kernel module that handles VM entries/exits and syscall forwarding. It also applies a bunch of (clever!) tricks that make the virtual memory space identical inside and outside the VM so the program can continue to execute after entering the VM.

Google's gVisor(https://gvisor.dev/) project may also help, and developers of gVisor are seeking to add the ability to transparently forward syscalls from VM in the kernel (https://lwn.net/Articles/902585/).

Related