#include #include #include #include #include #include #include #define PAGE_SIZE 4096 /* * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * ---------------------------------------------------------------------------- * Below is an updated version of this example, because the libnvmm API has * changed since I posted the blog entry. See: * https://mail-index.netbsd.org/tech-kern/2019/06/05/msg025101.html * The original version (using the previous API) is available here: * https://www.netbsd.org/~maxv/nvmm/calc-vm-old-api.c * ---------------------------------------------------------------------------- */ /* * A simple calculator. Creates a VM which performs the addition of the two * ints given as argument. * * The guest does EBX+=EAX, followed by HLT. We set EAX and EBX, and then * fetch the result in EBX. HLT is our shutdown point, we stop the VM there. * * We give one single page to the guest, and copy there the instructions it * must execute. The guest runs in 16bit real mode, and its initial state is * the x86 RESET state (default state). The instruction pointer uses CS.base * as base, and this base value is 0xFFFF0000. So we make it our GPA, and set * RIP=0, which means "RIP=0xFFFF0000+0". The guest therefore executes the * instructions at GPA 0xFFFF0000. * * gcc -o calc-vm calc-vm.c -lnvmm * ./calc-vm 3 5 * Result: 8 * * Don't forget to modload the NVMM kernel module beforehand! */ int main(int argc, char *argv[]) { const uint8_t instr[] = { 0x01, 0xc3, /* add %eax,%ebx */ 0xf4 /* hlt */ }; struct nvmm_machine mach; struct nvmm_vcpu vcpu; uintptr_t hva; gpaddr_t gpa = 0xFFFF0000; int num1, num2, ret; if (argc != 3) errx(EXIT_FAILURE, "wrong arguments"); num1 = atoi(argv[1]); num2 = atoi(argv[2]); /* Init NVMM. */ if (nvmm_init() == -1) err(EXIT_FAILURE, "unable to init NVMM"); /* Create the VM. */ if (nvmm_machine_create(&mach) == -1) err(EXIT_FAILURE, "unable to create the VM"); nvmm_vcpu_create(&mach, 0, &vcpu); /* Allocate a HVA. The HVA is writable. */ hva = (uintptr_t)mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); nvmm_hva_map(&mach, hva, PAGE_SIZE); /* Link the GPA towards the HVA. The GPA is executable. */ nvmm_gpa_map(&mach, hva, gpa, PAGE_SIZE, PROT_READ|PROT_EXEC); /* Install the guest instructions there. */ memcpy((void *)hva, instr, sizeof(instr)); /* Reset the instruction pointer, and set EAX/EBX. */ nvmm_vcpu_getstate(&mach, &vcpu, NVMM_X64_STATE_GPRS); vcpu.state->gprs[NVMM_X64_GPR_RIP] = 0; vcpu.state->gprs[NVMM_X64_GPR_RAX] = num1; vcpu.state->gprs[NVMM_X64_GPR_RBX] = num2; nvmm_vcpu_setstate(&mach, &vcpu, NVMM_X64_STATE_GPRS); while (1) { /* Run VCPU0. */ nvmm_vcpu_run(&mach, &vcpu); /* Process the exit reasons. */ switch (vcpu.exit->reason) { case NVMM_VCPU_EXIT_NONE: /* Nothing to do, keep rolling. */ break; case NVMM_VCPU_EXIT_HALTED: /* Our shutdown point. Fetch the result. */ nvmm_vcpu_getstate(&mach, &vcpu, NVMM_X64_STATE_GPRS); ret = vcpu.state->gprs[NVMM_X64_GPR_RBX]; printf("Result: %d\n", ret); return 0; /* THE PROCESS EXITS, THE VM GETS DESTROYED. */ default: errx(EXIT_FAILURE, "unknown exit reason"); } } return 0; }