xref: /freebsd/lib/libvmmapi/vmmapi.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
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 <machine/vmm.h>
47 #include <machine/vmm_dev.h>
48 
49 #include "vmmapi.h"
50 
51 #define	GB	(1024 * 1024 * 1024UL)
52 
53 struct vmctx {
54 	int	fd;
55 	uint32_t lowmem_limit;
56 	enum vm_mmap_style vms;
57 	size_t	lowmem;
58 	char	*lowmem_addr;
59 	size_t	highmem;
60 	char	*highmem_addr;
61 	char	*name;
62 };
63 
64 #define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
65 #define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
66 
67 static int
68 vm_device_open(const char *name)
69 {
70         int fd, len;
71         char *vmfile;
72 
73 	len = strlen("/dev/vmm/") + strlen(name) + 1;
74 	vmfile = malloc(len);
75 	assert(vmfile != NULL);
76 	snprintf(vmfile, len, "/dev/vmm/%s", name);
77 
78         /* Open the device file */
79         fd = open(vmfile, O_RDWR, 0);
80 
81 	free(vmfile);
82         return (fd);
83 }
84 
85 int
86 vm_create(const char *name)
87 {
88 
89 	return (CREATE((char *)name));
90 }
91 
92 struct vmctx *
93 vm_open(const char *name)
94 {
95 	struct vmctx *vm;
96 
97 	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
98 	assert(vm != NULL);
99 
100 	vm->fd = -1;
101 	vm->lowmem_limit = 3 * GB;
102 	vm->name = (char *)(vm + 1);
103 	strcpy(vm->name, name);
104 
105 	if ((vm->fd = vm_device_open(vm->name)) < 0)
106 		goto err;
107 
108 	return (vm);
109 err:
110 	vm_destroy(vm);
111 	return (NULL);
112 }
113 
114 void
115 vm_destroy(struct vmctx *vm)
116 {
117 	assert(vm != NULL);
118 
119 	if (vm->fd >= 0)
120 		close(vm->fd);
121 	DESTROY(vm->name);
122 
123 	free(vm);
124 }
125 
126 size_t
127 vmm_get_mem_total(void)
128 {
129 	size_t mem_total = 0;
130 	size_t oldlen = sizeof(mem_total);
131 	int error;
132 	error = sysctlbyname("hw.vmm.mem_total", &mem_total, &oldlen, NULL, 0);
133 	if (error)
134 		return -1;
135 	return mem_total;
136 }
137 
138 size_t
139 vmm_get_mem_free(void)
140 {
141 	size_t mem_free = 0;
142 	size_t oldlen = sizeof(mem_free);
143 	int error;
144 	error = sysctlbyname("hw.vmm.mem_free", &mem_free, &oldlen, NULL, 0);
145 	if (error)
146 		return -1;
147 	return mem_free;
148 }
149 
150 int
151 vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len)
152 {
153 	int error;
154 	struct vm_memory_segment seg;
155 
156 	bzero(&seg, sizeof(seg));
157 	seg.gpa = gpa;
158 	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
159 	*ret_len = seg.len;
160 	return (error);
161 }
162 
163 uint32_t
164 vm_get_lowmem_limit(struct vmctx *ctx)
165 {
166 
167 	return (ctx->lowmem_limit);
168 }
169 
170 void
171 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
172 {
173 
174 	ctx->lowmem_limit = limit;
175 }
176 
177 static int
178 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
179 {
180 	int error;
181 	struct vm_memory_segment seg;
182 
183 	/*
184 	 * Create and optionally map 'len' bytes of memory at guest
185 	 * physical address 'gpa'
186 	 */
187 	bzero(&seg, sizeof(seg));
188 	seg.gpa = gpa;
189 	seg.len = len;
190 	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
191 	if (error == 0 && addr != NULL) {
192 		*addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
193 				ctx->fd, gpa);
194 	}
195 	return (error);
196 }
197 
198 int
199 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
200 {
201 	char **addr;
202 	int error;
203 
204 	/* XXX VM_MMAP_SPARSE not implemented yet */
205 	assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
206 	ctx->vms = vms;
207 
208 	/*
209 	 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
210 	 * create another 'highmem' segment above 4GB for the remainder.
211 	 */
212 	if (memsize > ctx->lowmem_limit) {
213 		ctx->lowmem = ctx->lowmem_limit;
214 		ctx->highmem = memsize - ctx->lowmem;
215 	} else {
216 		ctx->lowmem = memsize;
217 		ctx->highmem = 0;
218 	}
219 
220 	if (ctx->lowmem > 0) {
221 		addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
222 		error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
223 		if (error)
224 			return (error);
225 	}
226 
227 	if (ctx->highmem > 0) {
228 		addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
229 		error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
230 		if (error)
231 			return (error);
232 	}
233 
234 	return (0);
235 }
236 
237 void *
238 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
239 {
240 
241 	/* XXX VM_MMAP_SPARSE not implemented yet */
242 	assert(ctx->vms == VM_MMAP_ALL);
243 
244 	if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
245 		return ((void *)(ctx->lowmem_addr + gaddr));
246 
247 	if (gaddr >= 4*GB) {
248 		gaddr -= 4*GB;
249 		if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
250 			return ((void *)(ctx->highmem_addr + gaddr));
251 	}
252 
253 	return (NULL);
254 }
255 
256 int
257 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
258 	    uint64_t base, uint32_t limit, uint32_t access)
259 {
260 	int error;
261 	struct vm_seg_desc vmsegdesc;
262 
263 	bzero(&vmsegdesc, sizeof(vmsegdesc));
264 	vmsegdesc.cpuid = vcpu;
265 	vmsegdesc.regnum = reg;
266 	vmsegdesc.desc.base = base;
267 	vmsegdesc.desc.limit = limit;
268 	vmsegdesc.desc.access = access;
269 
270 	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
271 	return (error);
272 }
273 
274 int
275 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
276 	    uint64_t *base, uint32_t *limit, uint32_t *access)
277 {
278 	int error;
279 	struct vm_seg_desc vmsegdesc;
280 
281 	bzero(&vmsegdesc, sizeof(vmsegdesc));
282 	vmsegdesc.cpuid = vcpu;
283 	vmsegdesc.regnum = reg;
284 
285 	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
286 	if (error == 0) {
287 		*base = vmsegdesc.desc.base;
288 		*limit = vmsegdesc.desc.limit;
289 		*access = vmsegdesc.desc.access;
290 	}
291 	return (error);
292 }
293 
294 int
295 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
296 {
297 	int error;
298 	struct vm_register vmreg;
299 
300 	bzero(&vmreg, sizeof(vmreg));
301 	vmreg.cpuid = vcpu;
302 	vmreg.regnum = reg;
303 	vmreg.regval = val;
304 
305 	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
306 	return (error);
307 }
308 
309 int
310 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
311 {
312 	int error;
313 	struct vm_register vmreg;
314 
315 	bzero(&vmreg, sizeof(vmreg));
316 	vmreg.cpuid = vcpu;
317 	vmreg.regnum = reg;
318 
319 	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
320 	*ret_val = vmreg.regval;
321 	return (error);
322 }
323 
324 int
325 vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
326 {
327 	int error;
328 	struct vm_run vmrun;
329 
330 	bzero(&vmrun, sizeof(vmrun));
331 	vmrun.cpuid = vcpu;
332 	vmrun.rip = rip;
333 
334 	error = ioctl(ctx->fd, VM_RUN, &vmrun);
335 	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
336 	return (error);
337 }
338 
339 static int
340 vm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
341 		     int vector, int error_code, int error_code_valid)
342 {
343 	struct vm_event ev;
344 
345 	bzero(&ev, sizeof(ev));
346 	ev.cpuid = vcpu;
347 	ev.type = type;
348 	ev.vector = vector;
349 	ev.error_code = error_code;
350 	ev.error_code_valid = error_code_valid;
351 
352 	return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
353 }
354 
355 int
356 vm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
357 		int vector)
358 {
359 
360 	return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
361 }
362 
363 int
364 vm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
365 		 int vector, int error_code)
366 {
367 
368 	return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
369 }
370 
371 int
372 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
373 {
374 	/*
375 	 * The apic id associated with the 'vcpu' has the same numerical value
376 	 * as the 'vcpu' itself.
377 	 */
378 	return (apicid);
379 }
380 
381 int
382 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
383 {
384 	struct vm_lapic_irq vmirq;
385 
386 	bzero(&vmirq, sizeof(vmirq));
387 	vmirq.cpuid = vcpu;
388 	vmirq.vector = vector;
389 
390 	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
391 }
392 
393 int
394 vm_inject_nmi(struct vmctx *ctx, int vcpu)
395 {
396 	struct vm_nmi vmnmi;
397 
398 	bzero(&vmnmi, sizeof(vmnmi));
399 	vmnmi.cpuid = vcpu;
400 
401 	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
402 }
403 
404 static struct {
405 	const char	*name;
406 	int		type;
407 } capstrmap[] = {
408 	{ "hlt_exit",		VM_CAP_HALT_EXIT },
409 	{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
410 	{ "pause_exit",		VM_CAP_PAUSE_EXIT },
411 	{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
412 	{ 0 }
413 };
414 
415 int
416 vm_capability_name2type(const char *capname)
417 {
418 	int i;
419 
420 	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
421 		if (strcmp(capstrmap[i].name, capname) == 0)
422 			return (capstrmap[i].type);
423 	}
424 
425 	return (-1);
426 }
427 
428 const char *
429 vm_capability_type2name(int type)
430 {
431 	int i;
432 
433 	for (i = 0; capstrmap[i].name != NULL; i++) {
434 		if (capstrmap[i].type == type)
435 			return (capstrmap[i].name);
436 	}
437 
438 	return (NULL);
439 }
440 
441 int
442 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
443 		  int *retval)
444 {
445 	int error;
446 	struct vm_capability vmcap;
447 
448 	bzero(&vmcap, sizeof(vmcap));
449 	vmcap.cpuid = vcpu;
450 	vmcap.captype = cap;
451 
452 	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
453 	*retval = vmcap.capval;
454 	return (error);
455 }
456 
457 int
458 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
459 {
460 	struct vm_capability vmcap;
461 
462 	bzero(&vmcap, sizeof(vmcap));
463 	vmcap.cpuid = vcpu;
464 	vmcap.captype = cap;
465 	vmcap.capval = val;
466 
467 	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
468 }
469 
470 int
471 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
472 {
473 	struct vm_pptdev pptdev;
474 
475 	bzero(&pptdev, sizeof(pptdev));
476 	pptdev.bus = bus;
477 	pptdev.slot = slot;
478 	pptdev.func = func;
479 
480 	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
481 }
482 
483 int
484 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
485 {
486 	struct vm_pptdev pptdev;
487 
488 	bzero(&pptdev, sizeof(pptdev));
489 	pptdev.bus = bus;
490 	pptdev.slot = slot;
491 	pptdev.func = func;
492 
493 	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
494 }
495 
496 int
497 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
498 		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
499 {
500 	struct vm_pptdev_mmio pptmmio;
501 
502 	bzero(&pptmmio, sizeof(pptmmio));
503 	pptmmio.bus = bus;
504 	pptmmio.slot = slot;
505 	pptmmio.func = func;
506 	pptmmio.gpa = gpa;
507 	pptmmio.len = len;
508 	pptmmio.hpa = hpa;
509 
510 	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
511 }
512 
513 int
514 vm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
515 	     int destcpu, int vector, int numvec)
516 {
517 	struct vm_pptdev_msi pptmsi;
518 
519 	bzero(&pptmsi, sizeof(pptmsi));
520 	pptmsi.vcpu = vcpu;
521 	pptmsi.bus = bus;
522 	pptmsi.slot = slot;
523 	pptmsi.func = func;
524 	pptmsi.destcpu = destcpu;
525 	pptmsi.vector = vector;
526 	pptmsi.numvec = numvec;
527 
528 	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
529 }
530 
531 int
532 vm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
533 	      int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
534 {
535 	struct vm_pptdev_msix pptmsix;
536 
537 	bzero(&pptmsix, sizeof(pptmsix));
538 	pptmsix.vcpu = vcpu;
539 	pptmsix.bus = bus;
540 	pptmsix.slot = slot;
541 	pptmsix.func = func;
542 	pptmsix.idx = idx;
543 	pptmsix.msg = msg;
544 	pptmsix.addr = addr;
545 	pptmsix.vector_control = vector_control;
546 
547 	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
548 }
549 
550 uint64_t *
551 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
552 	     int *ret_entries)
553 {
554 	int error;
555 
556 	static struct vm_stats vmstats;
557 
558 	vmstats.cpuid = vcpu;
559 
560 	error = ioctl(ctx->fd, VM_STATS, &vmstats);
561 	if (error == 0) {
562 		if (ret_entries)
563 			*ret_entries = vmstats.num_entries;
564 		if (ret_tv)
565 			*ret_tv = vmstats.tv;
566 		return (vmstats.statbuf);
567 	} else
568 		return (NULL);
569 }
570 
571 const char *
572 vm_get_stat_desc(struct vmctx *ctx, int index)
573 {
574 	static struct vm_stat_desc statdesc;
575 
576 	statdesc.index = index;
577 	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
578 		return (statdesc.desc);
579 	else
580 		return (NULL);
581 }
582 
583 int
584 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
585 {
586 	int error;
587 	struct vm_x2apic x2apic;
588 
589 	bzero(&x2apic, sizeof(x2apic));
590 	x2apic.cpuid = vcpu;
591 
592 	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
593 	*state = x2apic.state;
594 	return (error);
595 }
596 
597 int
598 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
599 {
600 	int error;
601 	struct vm_x2apic x2apic;
602 
603 	bzero(&x2apic, sizeof(x2apic));
604 	x2apic.cpuid = vcpu;
605 	x2apic.state = state;
606 
607 	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
608 
609 	return (error);
610 }
611 
612 /*
613  * From Intel Vol 3a:
614  * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
615  */
616 int
617 vcpu_reset(struct vmctx *vmctx, int vcpu)
618 {
619 	int error;
620 	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
621 	uint32_t desc_access, desc_limit;
622 	uint16_t sel;
623 
624 	zero = 0;
625 
626 	rflags = 0x2;
627 	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
628 	if (error)
629 		goto done;
630 
631 	rip = 0xfff0;
632 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
633 		goto done;
634 
635 	cr0 = CR0_NE;
636 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
637 		goto done;
638 
639 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
640 		goto done;
641 
642 	cr4 = 0;
643 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
644 		goto done;
645 
646 	/*
647 	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
648 	 */
649 	desc_base = 0xffff0000;
650 	desc_limit = 0xffff;
651 	desc_access = 0x0093;
652 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
653 			    desc_base, desc_limit, desc_access);
654 	if (error)
655 		goto done;
656 
657 	sel = 0xf000;
658 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
659 		goto done;
660 
661 	/*
662 	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
663 	 */
664 	desc_base = 0;
665 	desc_limit = 0xffff;
666 	desc_access = 0x0093;
667 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
668 			    desc_base, desc_limit, desc_access);
669 	if (error)
670 		goto done;
671 
672 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
673 			    desc_base, desc_limit, desc_access);
674 	if (error)
675 		goto done;
676 
677 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
678 			    desc_base, desc_limit, desc_access);
679 	if (error)
680 		goto done;
681 
682 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
683 			    desc_base, desc_limit, desc_access);
684 	if (error)
685 		goto done;
686 
687 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
688 			    desc_base, desc_limit, desc_access);
689 	if (error)
690 		goto done;
691 
692 	sel = 0;
693 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
694 		goto done;
695 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
696 		goto done;
697 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
698 		goto done;
699 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
700 		goto done;
701 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
702 		goto done;
703 
704 	/* General purpose registers */
705 	rdx = 0xf00;
706 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
707 		goto done;
708 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
709 		goto done;
710 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
711 		goto done;
712 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
713 		goto done;
714 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
715 		goto done;
716 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
717 		goto done;
718 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
719 		goto done;
720 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
721 		goto done;
722 
723 	/* GDTR, IDTR */
724 	desc_base = 0;
725 	desc_limit = 0xffff;
726 	desc_access = 0;
727 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
728 			    desc_base, desc_limit, desc_access);
729 	if (error != 0)
730 		goto done;
731 
732 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
733 			    desc_base, desc_limit, desc_access);
734 	if (error != 0)
735 		goto done;
736 
737 	/* TR */
738 	desc_base = 0;
739 	desc_limit = 0xffff;
740 	desc_access = 0x0000008b;
741 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
742 	if (error)
743 		goto done;
744 
745 	sel = 0;
746 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
747 		goto done;
748 
749 	/* LDTR */
750 	desc_base = 0;
751 	desc_limit = 0xffff;
752 	desc_access = 0x00000082;
753 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
754 			    desc_limit, desc_access);
755 	if (error)
756 		goto done;
757 
758 	sel = 0;
759 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
760 		goto done;
761 
762 	/* XXX cr2, debug registers */
763 
764 	error = 0;
765 done:
766 	return (error);
767 }
768