vmmapi.c revision 261088
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: stable/10/lib/libvmmapi/vmmapi.c 261088 2014-01-23 20:21:39Z jhb $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/lib/libvmmapi/vmmapi.c 261088 2014-01-23 20:21:39Z jhb $"); 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 56struct 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 70static int 71vm_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 88int 89vm_create(const char *name) 90{ 91 92 return (CREATE((char *)name)); 93} 94 95struct vmctx * 96vm_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); 112err: 113 vm_destroy(vm); 114 return (NULL); 115} 116 117void 118vm_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 129int 130vm_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 153int 154vm_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 169uint32_t 170vm_get_lowmem_limit(struct vmctx *ctx) 171{ 172 173 return (ctx->lowmem_limit); 174} 175 176void 177vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit) 178{ 179 180 ctx->lowmem_limit = limit; 181} 182 183static int 184setup_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 204int 205vm_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 243void * 244vm_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 262int 263vm_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 280int 281vm_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 300int 301vm_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 315int 316vm_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 330int 331vm_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 345static int 346vm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type, 347 int vector, int error_code, int error_code_valid) 348{ 349 struct vm_event ev; 350 351 bzero(&ev, sizeof(ev)); 352 ev.cpuid = vcpu; 353 ev.type = type; 354 ev.vector = vector; 355 ev.error_code = error_code; 356 ev.error_code_valid = error_code_valid; 357 358 return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev)); 359} 360 361int 362vm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type, 363 int vector) 364{ 365 366 return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0)); 367} 368 369int 370vm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type, 371 int vector, int error_code) 372{ 373 374 return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1)); 375} 376 377int 378vm_apicid2vcpu(struct vmctx *ctx, int apicid) 379{ 380 /* 381 * The apic id associated with the 'vcpu' has the same numerical value 382 * as the 'vcpu' itself. 383 */ 384 return (apicid); 385} 386 387int 388vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector) 389{ 390 struct vm_lapic_irq vmirq; 391 392 bzero(&vmirq, sizeof(vmirq)); 393 vmirq.cpuid = vcpu; 394 vmirq.vector = vector; 395 396 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq)); 397} 398 399int 400vm_ioapic_assert_irq(struct vmctx *ctx, int irq) 401{ 402 struct vm_ioapic_irq ioapic_irq; 403 404 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 405 ioapic_irq.irq = irq; 406 407 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq)); 408} 409 410int 411vm_ioapic_deassert_irq(struct vmctx *ctx, int irq) 412{ 413 struct vm_ioapic_irq ioapic_irq; 414 415 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 416 ioapic_irq.irq = irq; 417 418 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq)); 419} 420 421int 422vm_ioapic_pulse_irq(struct vmctx *ctx, int irq) 423{ 424 struct vm_ioapic_irq ioapic_irq; 425 426 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); 427 ioapic_irq.irq = irq; 428 429 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq)); 430} 431 432int 433vm_inject_nmi(struct vmctx *ctx, int vcpu) 434{ 435 struct vm_nmi vmnmi; 436 437 bzero(&vmnmi, sizeof(vmnmi)); 438 vmnmi.cpuid = vcpu; 439 440 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi)); 441} 442 443static struct { 444 const char *name; 445 int type; 446} capstrmap[] = { 447 { "hlt_exit", VM_CAP_HALT_EXIT }, 448 { "mtrap_exit", VM_CAP_MTRAP_EXIT }, 449 { "pause_exit", VM_CAP_PAUSE_EXIT }, 450 { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST }, 451 { "enable_invpcid", VM_CAP_ENABLE_INVPCID }, 452 { 0 } 453}; 454 455int 456vm_capability_name2type(const char *capname) 457{ 458 int i; 459 460 for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) { 461 if (strcmp(capstrmap[i].name, capname) == 0) 462 return (capstrmap[i].type); 463 } 464 465 return (-1); 466} 467 468const char * 469vm_capability_type2name(int type) 470{ 471 int i; 472 473 for (i = 0; capstrmap[i].name != NULL; i++) { 474 if (capstrmap[i].type == type) 475 return (capstrmap[i].name); 476 } 477 478 return (NULL); 479} 480 481int 482vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, 483 int *retval) 484{ 485 int error; 486 struct vm_capability vmcap; 487 488 bzero(&vmcap, sizeof(vmcap)); 489 vmcap.cpuid = vcpu; 490 vmcap.captype = cap; 491 492 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap); 493 *retval = vmcap.capval; 494 return (error); 495} 496 497int 498vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val) 499{ 500 struct vm_capability vmcap; 501 502 bzero(&vmcap, sizeof(vmcap)); 503 vmcap.cpuid = vcpu; 504 vmcap.captype = cap; 505 vmcap.capval = val; 506 507 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap)); 508} 509 510int 511vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 512{ 513 struct vm_pptdev pptdev; 514 515 bzero(&pptdev, sizeof(pptdev)); 516 pptdev.bus = bus; 517 pptdev.slot = slot; 518 pptdev.func = func; 519 520 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev)); 521} 522 523int 524vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func) 525{ 526 struct vm_pptdev pptdev; 527 528 bzero(&pptdev, sizeof(pptdev)); 529 pptdev.bus = bus; 530 pptdev.slot = slot; 531 pptdev.func = func; 532 533 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev)); 534} 535 536int 537vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, 538 vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 539{ 540 struct vm_pptdev_mmio pptmmio; 541 542 bzero(&pptmmio, sizeof(pptmmio)); 543 pptmmio.bus = bus; 544 pptmmio.slot = slot; 545 pptmmio.func = func; 546 pptmmio.gpa = gpa; 547 pptmmio.len = len; 548 pptmmio.hpa = hpa; 549 550 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio)); 551} 552 553int 554vm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 555 int destcpu, int vector, int numvec) 556{ 557 struct vm_pptdev_msi pptmsi; 558 559 bzero(&pptmsi, sizeof(pptmsi)); 560 pptmsi.vcpu = vcpu; 561 pptmsi.bus = bus; 562 pptmsi.slot = slot; 563 pptmsi.func = func; 564 pptmsi.destcpu = destcpu; 565 pptmsi.vector = vector; 566 pptmsi.numvec = numvec; 567 568 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi)); 569} 570 571int 572vm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func, 573 int idx, uint32_t msg, uint32_t vector_control, uint64_t addr) 574{ 575 struct vm_pptdev_msix pptmsix; 576 577 bzero(&pptmsix, sizeof(pptmsix)); 578 pptmsix.vcpu = vcpu; 579 pptmsix.bus = bus; 580 pptmsix.slot = slot; 581 pptmsix.func = func; 582 pptmsix.idx = idx; 583 pptmsix.msg = msg; 584 pptmsix.addr = addr; 585 pptmsix.vector_control = vector_control; 586 587 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix); 588} 589 590uint64_t * 591vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv, 592 int *ret_entries) 593{ 594 int error; 595 596 static struct vm_stats vmstats; 597 598 vmstats.cpuid = vcpu; 599 600 error = ioctl(ctx->fd, VM_STATS, &vmstats); 601 if (error == 0) { 602 if (ret_entries) 603 *ret_entries = vmstats.num_entries; 604 if (ret_tv) 605 *ret_tv = vmstats.tv; 606 return (vmstats.statbuf); 607 } else 608 return (NULL); 609} 610 611const char * 612vm_get_stat_desc(struct vmctx *ctx, int index) 613{ 614 static struct vm_stat_desc statdesc; 615 616 statdesc.index = index; 617 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0) 618 return (statdesc.desc); 619 else 620 return (NULL); 621} 622 623int 624vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state) 625{ 626 int error; 627 struct vm_x2apic x2apic; 628 629 bzero(&x2apic, sizeof(x2apic)); 630 x2apic.cpuid = vcpu; 631 632 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic); 633 *state = x2apic.state; 634 return (error); 635} 636 637int 638vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state) 639{ 640 int error; 641 struct vm_x2apic x2apic; 642 643 bzero(&x2apic, sizeof(x2apic)); 644 x2apic.cpuid = vcpu; 645 x2apic.state = state; 646 647 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic); 648 649 return (error); 650} 651 652/* 653 * From Intel Vol 3a: 654 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT 655 */ 656int 657vcpu_reset(struct vmctx *vmctx, int vcpu) 658{ 659 int error; 660 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx; 661 uint32_t desc_access, desc_limit; 662 uint16_t sel; 663 664 zero = 0; 665 666 rflags = 0x2; 667 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); 668 if (error) 669 goto done; 670 671 rip = 0xfff0; 672 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0) 673 goto done; 674 675 cr0 = CR0_NE; 676 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) 677 goto done; 678 679 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0) 680 goto done; 681 682 cr4 = 0; 683 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0) 684 goto done; 685 686 /* 687 * CS: present, r/w, accessed, 16-bit, byte granularity, usable 688 */ 689 desc_base = 0xffff0000; 690 desc_limit = 0xffff; 691 desc_access = 0x0093; 692 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, 693 desc_base, desc_limit, desc_access); 694 if (error) 695 goto done; 696 697 sel = 0xf000; 698 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0) 699 goto done; 700 701 /* 702 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity 703 */ 704 desc_base = 0; 705 desc_limit = 0xffff; 706 desc_access = 0x0093; 707 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, 708 desc_base, desc_limit, desc_access); 709 if (error) 710 goto done; 711 712 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, 713 desc_base, desc_limit, desc_access); 714 if (error) 715 goto done; 716 717 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, 718 desc_base, desc_limit, desc_access); 719 if (error) 720 goto done; 721 722 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, 723 desc_base, desc_limit, desc_access); 724 if (error) 725 goto done; 726 727 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, 728 desc_base, desc_limit, desc_access); 729 if (error) 730 goto done; 731 732 sel = 0; 733 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0) 734 goto done; 735 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0) 736 goto done; 737 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0) 738 goto done; 739 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0) 740 goto done; 741 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0) 742 goto done; 743 744 /* General purpose registers */ 745 rdx = 0xf00; 746 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0) 747 goto done; 748 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0) 749 goto done; 750 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0) 751 goto done; 752 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0) 753 goto done; 754 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0) 755 goto done; 756 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0) 757 goto done; 758 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0) 759 goto done; 760 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0) 761 goto done; 762 763 /* GDTR, IDTR */ 764 desc_base = 0; 765 desc_limit = 0xffff; 766 desc_access = 0; 767 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, 768 desc_base, desc_limit, desc_access); 769 if (error != 0) 770 goto done; 771 772 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR, 773 desc_base, desc_limit, desc_access); 774 if (error != 0) 775 goto done; 776 777 /* TR */ 778 desc_base = 0; 779 desc_limit = 0xffff; 780 desc_access = 0x0000008b; 781 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access); 782 if (error) 783 goto done; 784 785 sel = 0; 786 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0) 787 goto done; 788 789 /* LDTR */ 790 desc_base = 0; 791 desc_limit = 0xffff; 792 desc_access = 0x00000082; 793 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base, 794 desc_limit, desc_access); 795 if (error) 796 goto done; 797 798 sel = 0; 799 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) 800 goto done; 801 802 /* XXX cr2, debug registers */ 803 804 error = 0; 805done: 806 return (error); 807} 808 809int 810vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num) 811{ 812 int error, i; 813 struct vm_gpa_pte gpapte; 814 815 bzero(&gpapte, sizeof(gpapte)); 816 gpapte.gpa = gpa; 817 818 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte); 819 820 if (error == 0) { 821 *num = gpapte.ptenum; 822 for (i = 0; i < gpapte.ptenum; i++) 823 pte[i] = gpapte.pte[i]; 824 } 825 826 return (error); 827} 828 829int 830vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities) 831{ 832 int error; 833 struct vm_hpet_cap cap; 834 835 bzero(&cap, sizeof(struct vm_hpet_cap)); 836 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap); 837 if (capabilities != NULL) 838 *capabilities = cap.capabilities; 839 return (error); 840} 841