int xh_vm_reinit(void) { int error; vcpu_freeze_all(true); error = vm_reinit(vm); vcpu_freeze_all(false); return (error); }
static struct vmctx * do_open(const char *vmname) { struct vmctx *ctx; int error; bool reinit, romboot; reinit = romboot = false; if (lpc_bootrom()) romboot = true; error = vm_create(vmname); if (error) { if (errno == EEXIST) { if (romboot) { reinit = true; } else { /* * The virtual machine has been setup by the * userspace bootloader. */ } } else { perror("vm_create"); exit(1); } } else { if (!romboot) { /* * If the virtual machine was just created then a * bootrom must be configured to boot it. */ fprintf(stderr, "virtual machine cannot be booted\n"); exit(1); } } ctx = vm_open(vmname); if (ctx == NULL) { perror("vm_open"); exit(1); } if (reinit) { error = vm_reinit(ctx); if (error) { perror("vm_reinit"); exit(1); } } return (ctx); }
int grub_emu_bhyve_init(const char *name, grub_uint64_t memsz) { int err; int val; grub_uint64_t lomemsz; #ifdef VMMAPI_VERSION int need_reinit = 0; #endif err = vm_create (name); if (err != 0) { if (errno != EEXIST) { fprintf (stderr, "Could not create VM %s\n", name); return GRUB_ERR_ACCESS_DENIED; } #ifdef VMMAPI_VERSION need_reinit = 1; #endif } bhyve_ctx = vm_open (name); if (bhyve_ctx == NULL) { fprintf (stderr, "Could not open VM %s\n", name); return GRUB_ERR_BUG; } #ifdef VMMAPI_VERSION if (need_reinit) { err = vm_reinit (bhyve_ctx); if (err != 0) { fprintf (stderr, "Could not reinit VM %s\n", name); return GRUB_ERR_BUG; } } #endif val = 0; err = vm_get_capability (bhyve_ctx, 0, VM_CAP_UNRESTRICTED_GUEST, &val); if (err != 0) { fprintf (stderr, "VM unrestricted guest capability required\n"); return GRUB_ERR_BAD_DEVICE; } err = vm_set_capability (bhyve_ctx, 0, VM_CAP_UNRESTRICTED_GUEST, 1); if (err != 0) { fprintf (stderr, "Could not enable unrestricted guest for VM\n"); return GRUB_ERR_BUG; } err = vm_setup_memory (bhyve_ctx, memsz, VM_MMAP_ALL); if (err) { fprintf (stderr, "Could not setup memory for VM\n"); return GRUB_ERR_OUT_OF_MEMORY; } lomemsz = vm_get_lowmem_limit(bhyve_ctx); /* * Extract the virtual address of the mapped guest memory. */ if (memsz >= lomemsz) { bhyve_g2h.lomem = lomemsz; bhyve_g2h.himem = memsz - lomemsz; bhyve_g2h.himem_ptr = vm_map_gpa(bhyve_ctx, 4*GB, bhyve_g2h.himem); } else { bhyve_g2h.lomem = memsz; bhyve_g2h.himem = 0; } bhyve_g2h.lomem_ptr = vm_map_gpa(bhyve_ctx, 0, bhyve_g2h.lomem); /* * bhyve is going to return the following memory segments * * 0 - 640K - usable * 640K- 1MB - vga hole, BIOS, not usable. * 1MB - lomem - usable * lomem - 4G - not usable * 4G - himem - usable [optional if himem != 0] */ bhyve_info.nsegs = 2; bhyve_info.segs = bhyve_mm; bhyve_mm[0].start = 0x0; bhyve_mm[0].end = 640*1024 - 1; /* 640K */ bhyve_mm[0].type = GRUB_MEMORY_AVAILABLE; bhyve_mm[1].start = 1024*1024; bhyve_mm[1].end = (memsz > lomemsz) ? lomemsz : memsz; bhyve_mm[1].type = GRUB_MEMORY_AVAILABLE; if (memsz > lomemsz) { bhyve_info.nsegs++; bhyve_mm[2].start = 4*GB; bhyve_mm[2].end = (memsz - lomemsz) + bhyve_mm[2].start; bhyve_mm[2].type = GRUB_MEMORY_AVAILABLE; } /* The boot-code size is just the GDT that needs to be copied */ bhyve_info.bootsz = sizeof(bhyve_gdt); return 0; }
static struct vmctx * do_open(const char *vmname) { struct vmctx *ctx; int error; bool reinit, romboot; #ifndef WITHOUT_CAPSICUM cap_rights_t rights; const cap_ioctl_t *cmds; size_t ncmds; #endif reinit = romboot = false; if (lpc_bootrom()) romboot = true; error = vm_create(vmname); if (error) { if (errno == EEXIST) { if (romboot) { reinit = true; } else { /* * The virtual machine has been setup by the * userspace bootloader. */ } } else { perror("vm_create"); exit(1); } } else { if (!romboot) { /* * If the virtual machine was just created then a * bootrom must be configured to boot it. */ fprintf(stderr, "virtual machine cannot be booted\n"); exit(1); } } ctx = vm_open(vmname); if (ctx == NULL) { perror("vm_open"); exit(1); } #ifndef WITHOUT_CAPSICUM cap_rights_init(&rights, CAP_IOCTL, CAP_MMAP_RW); if (cap_rights_limit(vm_get_device_fd(ctx), &rights) == -1 && errno != ENOSYS) errx(EX_OSERR, "Unable to apply rights for sandbox"); vm_get_ioctls(&ncmds); cmds = vm_get_ioctls(NULL); if (cmds == NULL) errx(EX_OSERR, "out of memory"); if (cap_ioctls_limit(vm_get_device_fd(ctx), cmds, ncmds) == -1 && errno != ENOSYS) errx(EX_OSERR, "Unable to apply rights for sandbox"); free((cap_ioctl_t *)cmds); #endif if (reinit) { error = vm_reinit(ctx); if (error) { perror("vm_reinit"); exit(1); } } error = vm_set_topology(ctx, sockets, cores, threads, maxcpus); if (error) errx(EX_OSERR, "vm_set_topology"); return (ctx); }