1 /*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/types.h> 33 #include <sys/sysctl.h> 34 #include <sys/ioctl.h> 35 #include <sys/mman.h> 36 37 #include <machine/specialreg.h> 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <assert.h> 42 #include <string.h> 43 #include <fcntl.h> 44 #include <unistd.h> 45 46 #include <libutil.h> 47 48 #include <machine/vmm.h> 49 #include <machine/vmm_dev.h> 50 51 #include "vmmapi.h" 52 53 #define MB (1024 * 1024UL) 54 #define GB (1024 * 1024 * 1024UL) 55 56 struct vmctx { 57 int fd; 58 uint32_t lowmem_limit; 59 enum vm_mmap_style vms; 60 size_t lowmem; 61 char *lowmem_addr; 62 size_t highmem; 63 char *highmem_addr; 64 char *name; 65 }; 66 67 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x))) 68 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x))) 69 70 static int 71 vm_device_open(const char *name) 72 { 73 int fd, len; 74 char *vmfile; 75 76 len = strlen("/dev/vmm/") + strlen(name) + 1; 77 vmfile = malloc(len); 78 assert(vmfile != NULL); 79 snprintf(vmfile, len, "/dev/vmm/%s", name); 80 81 /* Open the device file */ 82 fd = open(vmfile, O_RDWR, 0); 83 84 free(vmfile); 85 return (fd); 86 } 87 88 int 89 vm_create(const char *name) 90 { 91 92 return (CREATE((char *)name)); 93 } 94 95 struct vmctx * 96 vm_open(const char *name) 97 { 98 struct vmctx *vm; 99 100 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1); 101 assert(vm != NULL); 102 103 vm->fd = -1; 104 vm->lowmem_limit = 3 * GB; 105 vm->name = (char *)(vm + 1); 106 strcpy(vm->name, name); 107 108 if ((vm->fd = vm_device_open(vm->name)) < 0) 109 goto err; 110 111 return (vm); 112 err: 113 vm_destroy(vm); 114 return (NULL); 115 } 116 117 void 118 vm_destroy(struct vmctx *vm) 119 { 120 assert(vm != NULL); 121 122 if (vm->fd >= 0) 123 close(vm->fd); 124 DESTROY(vm->name); 125 126 free(vm); 127 } 128 129 int 130 vm_parse_memsize(const char *optarg, size_t *ret_memsize) 131 { 132 char *endptr; 133 size_t optval; 134 int error; 135 136 optval = strtoul(optarg, &endptr, 0); 137 if (*optarg != '\0' && *endptr == '\0') { 138 /* 139 * For the sake of backward compatibility if the memory size 140 * specified on the command line is less than a megabyte then 141 * it is interpreted as being in units of MB. 142 */ 143 if (optval < MB) 144 optval *= MB; 145 *ret_memsize = optval; 146 error = 0; 147 } else 148 error = expand_number(optarg, ret_memsize); 149 150 return (error); 151 } 152 153 int 154 vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len, 155 int *wired) 156 { 157 int error; 158 struct vm_memory_segment seg; 159 160 bzero(&seg, sizeof(seg)); 161 seg.gpa = gpa; 162 error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg); 163 *ret_len = seg.len; 164 if (wired != NULL) 165 *wired = seg.wired; 166 return (error); 167 } 168 169 uint32_t 170 vm_get_lowmem_limit(struct vmctx *ctx) 171 { 172 173 return (ctx->lowmem_limit); 174 } 175 176 void 177 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit) 178 { 179 180 ctx->lowmem_limit = limit; 181 } 182 183 static int 184 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr) 185 { 186 int error; 187 struct vm_memory_segment seg; 188 189 /* 190 * Create and optionally map 'len' bytes of memory at guest 191 * physical address 'gpa' 192 */ 193 bzero(&seg, sizeof(seg)); 194 seg.gpa = gpa; 195 seg.len = len; 196 error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg); 197 if (error == 0 && addr != NULL) { 198 *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, 199 ctx->fd, gpa); 200 } 201 return (error); 202 } 203 204 int 205 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) 206 { 207 char **addr; 208 int error; 209 210 /* XXX VM_MMAP_SPARSE not implemented yet */ 211 assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL); 212 ctx->vms = vms; 213 214 /* 215 * If 'memsize' cannot fit entirely in the 'lowmem' segment then 216 * create another 'highmem' segment above 4GB for the remainder. 217 */ 218 if (memsize > ctx->lowmem_limit) { 219 ctx->lowmem = ctx->lowmem_limit; 220 ctx->highmem = memsize - ctx->lowmem; 221 } else { 222 ctx->lowmem = memsize; 223 ctx->highmem = 0; 224 } 225 226 if (ctx->lowmem > 0) { 227 addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL; 228 error = setup_memory_segment(ctx, 0, ctx->lowmem, addr); 229 if (error) 230 return (error); 231 } 232 233 if (ctx->highmem > 0) { 234 addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL; 235 error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr); 236 if (error) 237 return (error); 238 } 239 240 return (0); 241 } 242 243 void * 244 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len) 245 { 246 247 /* XXX VM_MMAP_SPARSE not implemented yet */ 248 assert(ctx->vms == VM_MMAP_ALL); 249 250 if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem) 251 return ((void *)(ctx->lowmem_addr + gaddr)); 252 253 if (gaddr >= 4*GB) { 254 gaddr -= 4*GB; 255 if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem) 256 return ((void *)(ctx->highmem_addr + gaddr)); 257 } 258 259 return (NULL); 260 } 261 262 int 263 vm_set_desc(struct vmctx *ctx, int vcpu, int reg, 264 uint64_t base, uint32_t limit, uint32_t access) 265 { 266 int error; 267 struct vm_seg_desc vmsegdesc; 268 269 bzero(&vmsegdesc, sizeof(vmsegdesc)); 270 vmsegdesc.cpuid = vcpu; 271 vmsegdesc.regnum = reg; 272 vmsegdesc.desc.base = base; 273 vmsegdesc.desc.limit = limit; 274 vmsegdesc.desc.access = access; 275 276 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc); 277 return (error); 278 } 279 280 int 281 vm_get_desc(struct vmctx *ctx, int vcpu, int reg, 282 uint64_t *base, uint32_t *limit, uint32_t *access) 283 { 284 int error; 285 struct vm_seg_desc vmsegdesc; 286 287 bzero(&vmsegdesc, sizeof(vmsegdesc)); 288 vmsegdesc.cpuid = vcpu; 289 vmsegdesc.regnum = reg; 290 291 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc); 292 if (error == 0) { 293 *base = vmsegdesc.desc.base; 294 *limit = vmsegdesc.desc.limit; 295 *access = vmsegdesc.desc.access; 296 } 297 return (error); 298 } 299 300 int 301 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val) 302 { 303 int error; 304 struct vm_register vmreg; 305 306 bzero(&vmreg, sizeof(vmreg)); 307 vmreg.cpuid = vcpu; 308 vmreg.regnum = reg; 309 vmreg.regval = val; 310 311 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg); 312 return (error); 313 } 314 315 int 316 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val) 317 { 318 int error; 319 struct vm_register vmreg; 320 321 bzero(&vmreg, sizeof(vmreg)); 322 vmreg.cpuid = vcpu; 323 vmreg.regnum = reg; 324 325 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg); 326 *ret_val = vmreg.regval; 327 return (error); 328 } 329 330 int 331 vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit) 332 { 333 int error; 334 struct vm_run vmrun; 335 336 bzero(&vmrun, sizeof(vmrun)); 337 vmrun.cpuid = vcpu; 338 vmrun.rip = rip; 339 340 error = ioctl(ctx->fd, VM_RUN, &vmrun); 341 bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit)); 342 return (error); 343 } 344 345 int 346 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how) 347 { 348 struct vm_suspend vmsuspend; 349 350 bzero(&vmsuspend, sizeof(vmsuspend)); 351 vmsuspend.how = how; 352 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend)); 353 } 354 355 static int 356 vm_inject_exception_real(struct vmctx *ctx, int vcpu, int vector, 357 int error_code, int error_code_valid) 358 { 359 struct vm_exception exc; 360 361 bzero(&exc, sizeof(exc)); 362 exc.cpuid = vcpu; 363 exc.vector = vector; 364 exc.error_code = error_code; 365 exc.error_code_valid = error_code_valid; 366 367 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc)); 368 } 369 370 int 371 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector) 372 { 373 374 return (vm_inject_exception_real(ctx, vcpu, vector, 0, 0)); 375 } 376 377 int 378 vm_inject_exception2(struct vmctx *ctx, int vcpu, int vector, int errcode) 379 { 380 381 return (vm_inject_exception_real(ctx, vcpu, vector, errcode, 1)); 382 } 383 384 int 385 vm_apicid2vcpu(struct vmctx *ctx, int apicid) 386 { 387 /* 388 * The apic id associated with the 'vcpu' has the same numerical value 389 * as the 'vcpu' itself. 390 */ 391 return (apicid); 392 } 393 394 int 395 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector) 396 { 397 struct vm_lapic_irq vmirq; 398 399 bzero(&vmirq, sizeof(vmirq)); 400 vmirq.cpuid = vcpu; 401 vmirq.vector = vector; 402 403 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq)); 404 } 405 406 int 407 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector) 408 { 409 struct vm_lapic_irq vmirq; 410 411 bzero(&vmirq, sizeof(vmirq)); 412 vmirq.cpuid = vcpu; 413 vmirq.vector = vector; 414 415 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq)); 416 } 417 418 int 419 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg) 420 { 421 struct vm_lapic_msi vmmsi; 422 423 bzero(&vmmsi, sizeof(vmmsi)); 424 vmmsi.addr = addr; 425 vmmsi.msg = msg; 426 427 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi)); 428 } 429 430 int 431 vm_ioapic_assert_irq(struct vmctx *ctx, int irq) 432 { 433 struct vm_ioapic_irq ioapic_irq; 434 435 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 436 ioapic_irq.irq = irq; 437 438 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq)); 439 } 440 441 int 442 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq) 443 { 444 struct vm_ioapic_irq ioapic_irq; 445 446 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 447 ioapic_irq.irq = irq; 448 449 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq)); 450 } 451 452 int 453 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq) 454 { 455 struct vm_ioapic_irq ioapic_irq; 456 457 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 458 ioapic_irq.irq = irq; 459 460 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq)); 461 } 462 463 int 464 vm_ioapic_pincount(struct vmctx *ctx, int *pincount) 465 { 466 467 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount)); 468 } 469 470 int 471 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) 472 { 473 struct vm_isa_irq isa_irq; 474 475 bzero(&isa_irq, sizeof(struct vm_isa_irq)); 476 isa_irq.atpic_irq = atpic_irq; 477 isa_irq.ioapic_irq = ioapic_irq; 478 479 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq)); 480 } 481 482 int 483 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) 484 { 485 struct vm_isa_irq isa_irq; 486 487 bzero(&isa_irq, sizeof(struct vm_isa_irq)); 488 isa_irq.atpic_irq = atpic_irq; 489 isa_irq.ioapic_irq = ioapic_irq; 490 491 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq)); 492 } 493 494 int 495 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) 496 { 497 struct vm_isa_irq isa_irq; 498 bzero(&isa_irq, sizeof(struct vm_isa_irq)); 499 isa_irq.atpic_irq = atpic_irq; 500 isa_irq.ioapic_irq = ioapic_irq; 501 502 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq)); 503 } 504 505 int 506 vm_inject_nmi(struct vmctx *ctx, int vcpu) 507 { 508 struct vm_nmi vmnmi; 509 510 bzero(&vmnmi, sizeof(vmnmi)); 511 vmnmi.cpuid = vcpu; 512 513 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi)); 514 } 515 516 static struct { 517 const char *name; 518 int type; 519 } capstrmap[] = { 520 { "hlt_exit", VM_CAP_HALT_EXIT }, 521 { "mtrap_exit", VM_CAP_MTRAP_EXIT }, 522 { "pause_exit", VM_CAP_PAUSE_EXIT }, 523 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST }, 524 { "enable_invpcid", VM_CAP_ENABLE_INVPCID }, 525 { 0 } 526 }; 527 528 int 529 vm_capability_name2type(const char *capname) 530 { 531 int i; 532 533 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) { 534 if (strcmp(capstrmap[i].name, capname) == 0) 535 return (capstrmap[i].type); 536 } 537 538 return (-1); 539 } 540 541 const char * 542 vm_capability_type2name(int type) 543 { 544 int i; 545 546 for (i = 0; capstrmap[i].name != NULL; i++) { 547 if (capstrmap[i].type == type) 548 return (capstrmap[i].name); 549 } 550 551 return (NULL); 552 } 553 554 int 555 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, 556 int *retval) 557 { 558 int error; 559 struct vm_capability vmcap; 560 561 bzero(&vmcap, sizeof(vmcap)); 562 vmcap.cpuid = vcpu; 563 vmcap.captype = cap; 564 565 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap); 566 *retval = vmcap.capval; 567 return (error); 568 } 569 570 int 571 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val) 572 { 573 struct vm_capability vmcap; 574 575 bzero(&vmcap, sizeof(vmcap)); 576 vmcap.cpuid = vcpu; 577 vmcap.captype = cap; 578 vmcap.capval = val; 579 580 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap)); 581 } 582 583 int 584 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 585 { 586 struct vm_pptdev pptdev; 587 588 bzero(&pptdev, sizeof(pptdev)); 589 pptdev.bus = bus; 590 pptdev.slot = slot; 591 pptdev.func = func; 592 593 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev)); 594 } 595 596 int 597 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 598 { 599 struct vm_pptdev pptdev; 600 601 bzero(&pptdev, sizeof(pptdev)); 602 pptdev.bus = bus; 603 pptdev.slot = slot; 604 pptdev.func = func; 605 606 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev)); 607 } 608 609 int 610 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, 611 vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 612 { 613 struct vm_pptdev_mmio pptmmio; 614 615 bzero(&pptmmio, sizeof(pptmmio)); 616 pptmmio.bus = bus; 617 pptmmio.slot = slot; 618 pptmmio.func = func; 619 pptmmio.gpa = gpa; 620 pptmmio.len = len; 621 pptmmio.hpa = hpa; 622 623 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio)); 624 } 625 626 int 627 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 628 uint64_t addr, uint64_t msg, int numvec) 629 { 630 struct vm_pptdev_msi pptmsi; 631 632 bzero(&pptmsi, sizeof(pptmsi)); 633 pptmsi.vcpu = vcpu; 634 pptmsi.bus = bus; 635 pptmsi.slot = slot; 636 pptmsi.func = func; 637 pptmsi.msg = msg; 638 pptmsi.addr = addr; 639 pptmsi.numvec = numvec; 640 641 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi)); 642 } 643 644 int 645 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 646 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control) 647 { 648 struct vm_pptdev_msix pptmsix; 649 650 bzero(&pptmsix, sizeof(pptmsix)); 651 pptmsix.vcpu = vcpu; 652 pptmsix.bus = bus; 653 pptmsix.slot = slot; 654 pptmsix.func = func; 655 pptmsix.idx = idx; 656 pptmsix.msg = msg; 657 pptmsix.addr = addr; 658 pptmsix.vector_control = vector_control; 659 660 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix); 661 } 662 663 uint64_t * 664 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv, 665 int *ret_entries) 666 { 667 int error; 668 669 static struct vm_stats vmstats; 670 671 vmstats.cpuid = vcpu; 672 673 error = ioctl(ctx->fd, VM_STATS, &vmstats); 674 if (error == 0) { 675 if (ret_entries) 676 *ret_entries = vmstats.num_entries; 677 if (ret_tv) 678 *ret_tv = vmstats.tv; 679 return (vmstats.statbuf); 680 } else 681 return (NULL); 682 } 683 684 const char * 685 vm_get_stat_desc(struct vmctx *ctx, int index) 686 { 687 static struct vm_stat_desc statdesc; 688 689 statdesc.index = index; 690 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0) 691 return (statdesc.desc); 692 else 693 return (NULL); 694 } 695 696 int 697 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state) 698 { 699 int error; 700 struct vm_x2apic x2apic; 701 702 bzero(&x2apic, sizeof(x2apic)); 703 x2apic.cpuid = vcpu; 704 705 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic); 706 *state = x2apic.state; 707 return (error); 708 } 709 710 int 711 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state) 712 { 713 int error; 714 struct vm_x2apic x2apic; 715 716 bzero(&x2apic, sizeof(x2apic)); 717 x2apic.cpuid = vcpu; 718 x2apic.state = state; 719 720 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic); 721 722 return (error); 723 } 724 725 /* 726 * From Intel Vol 3a: 727 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT 728 */ 729 int 730 vcpu_reset(struct vmctx *vmctx, int vcpu) 731 { 732 int error; 733 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx; 734 uint32_t desc_access, desc_limit; 735 uint16_t sel; 736 737 zero = 0; 738 739 rflags = 0x2; 740 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); 741 if (error) 742 goto done; 743 744 rip = 0xfff0; 745 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0) 746 goto done; 747 748 cr0 = CR0_NE; 749 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) 750 goto done; 751 752 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0) 753 goto done; 754 755 cr4 = 0; 756 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0) 757 goto done; 758 759 /* 760 * CS: present, r/w, accessed, 16-bit, byte granularity, usable 761 */ 762 desc_base = 0xffff0000; 763 desc_limit = 0xffff; 764 desc_access = 0x0093; 765 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, 766 desc_base, desc_limit, desc_access); 767 if (error) 768 goto done; 769 770 sel = 0xf000; 771 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0) 772 goto done; 773 774 /* 775 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity 776 */ 777 desc_base = 0; 778 desc_limit = 0xffff; 779 desc_access = 0x0093; 780 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, 781 desc_base, desc_limit, desc_access); 782 if (error) 783 goto done; 784 785 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, 786 desc_base, desc_limit, desc_access); 787 if (error) 788 goto done; 789 790 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, 791 desc_base, desc_limit, desc_access); 792 if (error) 793 goto done; 794 795 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, 796 desc_base, desc_limit, desc_access); 797 if (error) 798 goto done; 799 800 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, 801 desc_base, desc_limit, desc_access); 802 if (error) 803 goto done; 804 805 sel = 0; 806 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0) 807 goto done; 808 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0) 809 goto done; 810 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0) 811 goto done; 812 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0) 813 goto done; 814 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0) 815 goto done; 816 817 /* General purpose registers */ 818 rdx = 0xf00; 819 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0) 820 goto done; 821 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0) 822 goto done; 823 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0) 824 goto done; 825 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0) 826 goto done; 827 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0) 828 goto done; 829 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0) 830 goto done; 831 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0) 832 goto done; 833 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0) 834 goto done; 835 836 /* GDTR, IDTR */ 837 desc_base = 0; 838 desc_limit = 0xffff; 839 desc_access = 0; 840 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, 841 desc_base, desc_limit, desc_access); 842 if (error != 0) 843 goto done; 844 845 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR, 846 desc_base, desc_limit, desc_access); 847 if (error != 0) 848 goto done; 849 850 /* TR */ 851 desc_base = 0; 852 desc_limit = 0xffff; 853 desc_access = 0x0000008b; 854 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access); 855 if (error) 856 goto done; 857 858 sel = 0; 859 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0) 860 goto done; 861 862 /* LDTR */ 863 desc_base = 0; 864 desc_limit = 0xffff; 865 desc_access = 0x00000082; 866 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base, 867 desc_limit, desc_access); 868 if (error) 869 goto done; 870 871 sel = 0; 872 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) 873 goto done; 874 875 /* XXX cr2, debug registers */ 876 877 error = 0; 878 done: 879 return (error); 880 } 881 882 int 883 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num) 884 { 885 int error, i; 886 struct vm_gpa_pte gpapte; 887 888 bzero(&gpapte, sizeof(gpapte)); 889 gpapte.gpa = gpa; 890 891 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte); 892 893 if (error == 0) { 894 *num = gpapte.ptenum; 895 for (i = 0; i < gpapte.ptenum; i++) 896 pte[i] = gpapte.pte[i]; 897 } 898 899 return (error); 900 } 901 902 int 903 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities) 904 { 905 int error; 906 struct vm_hpet_cap cap; 907 908 bzero(&cap, sizeof(struct vm_hpet_cap)); 909 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap); 910 if (capabilities != NULL) 911 *capabilities = cap.capabilities; 912 return (error); 913 } 914