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