xref: /freebsd/lib/libvmmapi/vmmapi.c (revision 55888cfaa2584256e00c6252483c743da83ea9f9)
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 static int
346 vm_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 
361 int
362 vm_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 
369 int
370 vm_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 
377 int
378 vm_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 
387 int
388 vm_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 
399 int
400 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
401 {
402 	struct vm_lapic_msi vmmsi;
403 
404 	bzero(&vmmsi, sizeof(vmmsi));
405 	vmmsi.addr = addr;
406 	vmmsi.msg = msg;
407 
408 	return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
409 }
410 
411 int
412 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
413 {
414 	struct vm_ioapic_irq ioapic_irq;
415 
416 	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
417 	ioapic_irq.irq = irq;
418 
419 	return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
420 }
421 
422 int
423 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
424 {
425 	struct vm_ioapic_irq ioapic_irq;
426 
427 	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
428 	ioapic_irq.irq = irq;
429 
430 	return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
431 }
432 
433 int
434 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
435 {
436 	struct vm_ioapic_irq ioapic_irq;
437 
438 	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
439 	ioapic_irq.irq = irq;
440 
441 	return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
442 }
443 
444 int
445 vm_inject_nmi(struct vmctx *ctx, int vcpu)
446 {
447 	struct vm_nmi vmnmi;
448 
449 	bzero(&vmnmi, sizeof(vmnmi));
450 	vmnmi.cpuid = vcpu;
451 
452 	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
453 }
454 
455 static struct {
456 	const char	*name;
457 	int		type;
458 } capstrmap[] = {
459 	{ "hlt_exit",		VM_CAP_HALT_EXIT },
460 	{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
461 	{ "pause_exit",		VM_CAP_PAUSE_EXIT },
462 	{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
463 	{ "enable_invpcid",	VM_CAP_ENABLE_INVPCID },
464 	{ 0 }
465 };
466 
467 int
468 vm_capability_name2type(const char *capname)
469 {
470 	int i;
471 
472 	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
473 		if (strcmp(capstrmap[i].name, capname) == 0)
474 			return (capstrmap[i].type);
475 	}
476 
477 	return (-1);
478 }
479 
480 const char *
481 vm_capability_type2name(int type)
482 {
483 	int i;
484 
485 	for (i = 0; capstrmap[i].name != NULL; i++) {
486 		if (capstrmap[i].type == type)
487 			return (capstrmap[i].name);
488 	}
489 
490 	return (NULL);
491 }
492 
493 int
494 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
495 		  int *retval)
496 {
497 	int error;
498 	struct vm_capability vmcap;
499 
500 	bzero(&vmcap, sizeof(vmcap));
501 	vmcap.cpuid = vcpu;
502 	vmcap.captype = cap;
503 
504 	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
505 	*retval = vmcap.capval;
506 	return (error);
507 }
508 
509 int
510 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
511 {
512 	struct vm_capability vmcap;
513 
514 	bzero(&vmcap, sizeof(vmcap));
515 	vmcap.cpuid = vcpu;
516 	vmcap.captype = cap;
517 	vmcap.capval = val;
518 
519 	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
520 }
521 
522 int
523 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
524 {
525 	struct vm_pptdev pptdev;
526 
527 	bzero(&pptdev, sizeof(pptdev));
528 	pptdev.bus = bus;
529 	pptdev.slot = slot;
530 	pptdev.func = func;
531 
532 	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
533 }
534 
535 int
536 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
537 {
538 	struct vm_pptdev pptdev;
539 
540 	bzero(&pptdev, sizeof(pptdev));
541 	pptdev.bus = bus;
542 	pptdev.slot = slot;
543 	pptdev.func = func;
544 
545 	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
546 }
547 
548 int
549 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
550 		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
551 {
552 	struct vm_pptdev_mmio pptmmio;
553 
554 	bzero(&pptmmio, sizeof(pptmmio));
555 	pptmmio.bus = bus;
556 	pptmmio.slot = slot;
557 	pptmmio.func = func;
558 	pptmmio.gpa = gpa;
559 	pptmmio.len = len;
560 	pptmmio.hpa = hpa;
561 
562 	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
563 }
564 
565 int
566 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
567     uint64_t addr, uint64_t msg, int numvec)
568 {
569 	struct vm_pptdev_msi pptmsi;
570 
571 	bzero(&pptmsi, sizeof(pptmsi));
572 	pptmsi.vcpu = vcpu;
573 	pptmsi.bus = bus;
574 	pptmsi.slot = slot;
575 	pptmsi.func = func;
576 	pptmsi.msg = msg;
577 	pptmsi.addr = addr;
578 	pptmsi.numvec = numvec;
579 
580 	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
581 }
582 
583 int
584 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
585     int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
586 {
587 	struct vm_pptdev_msix pptmsix;
588 
589 	bzero(&pptmsix, sizeof(pptmsix));
590 	pptmsix.vcpu = vcpu;
591 	pptmsix.bus = bus;
592 	pptmsix.slot = slot;
593 	pptmsix.func = func;
594 	pptmsix.idx = idx;
595 	pptmsix.msg = msg;
596 	pptmsix.addr = addr;
597 	pptmsix.vector_control = vector_control;
598 
599 	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
600 }
601 
602 uint64_t *
603 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
604 	     int *ret_entries)
605 {
606 	int error;
607 
608 	static struct vm_stats vmstats;
609 
610 	vmstats.cpuid = vcpu;
611 
612 	error = ioctl(ctx->fd, VM_STATS, &vmstats);
613 	if (error == 0) {
614 		if (ret_entries)
615 			*ret_entries = vmstats.num_entries;
616 		if (ret_tv)
617 			*ret_tv = vmstats.tv;
618 		return (vmstats.statbuf);
619 	} else
620 		return (NULL);
621 }
622 
623 const char *
624 vm_get_stat_desc(struct vmctx *ctx, int index)
625 {
626 	static struct vm_stat_desc statdesc;
627 
628 	statdesc.index = index;
629 	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
630 		return (statdesc.desc);
631 	else
632 		return (NULL);
633 }
634 
635 int
636 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
637 {
638 	int error;
639 	struct vm_x2apic x2apic;
640 
641 	bzero(&x2apic, sizeof(x2apic));
642 	x2apic.cpuid = vcpu;
643 
644 	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
645 	*state = x2apic.state;
646 	return (error);
647 }
648 
649 int
650 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
651 {
652 	int error;
653 	struct vm_x2apic x2apic;
654 
655 	bzero(&x2apic, sizeof(x2apic));
656 	x2apic.cpuid = vcpu;
657 	x2apic.state = state;
658 
659 	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
660 
661 	return (error);
662 }
663 
664 /*
665  * From Intel Vol 3a:
666  * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
667  */
668 int
669 vcpu_reset(struct vmctx *vmctx, int vcpu)
670 {
671 	int error;
672 	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
673 	uint32_t desc_access, desc_limit;
674 	uint16_t sel;
675 
676 	zero = 0;
677 
678 	rflags = 0x2;
679 	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
680 	if (error)
681 		goto done;
682 
683 	rip = 0xfff0;
684 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
685 		goto done;
686 
687 	cr0 = CR0_NE;
688 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
689 		goto done;
690 
691 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
692 		goto done;
693 
694 	cr4 = 0;
695 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
696 		goto done;
697 
698 	/*
699 	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
700 	 */
701 	desc_base = 0xffff0000;
702 	desc_limit = 0xffff;
703 	desc_access = 0x0093;
704 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
705 			    desc_base, desc_limit, desc_access);
706 	if (error)
707 		goto done;
708 
709 	sel = 0xf000;
710 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
711 		goto done;
712 
713 	/*
714 	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
715 	 */
716 	desc_base = 0;
717 	desc_limit = 0xffff;
718 	desc_access = 0x0093;
719 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
720 			    desc_base, desc_limit, desc_access);
721 	if (error)
722 		goto done;
723 
724 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
725 			    desc_base, desc_limit, desc_access);
726 	if (error)
727 		goto done;
728 
729 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
730 			    desc_base, desc_limit, desc_access);
731 	if (error)
732 		goto done;
733 
734 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
735 			    desc_base, desc_limit, desc_access);
736 	if (error)
737 		goto done;
738 
739 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
740 			    desc_base, desc_limit, desc_access);
741 	if (error)
742 		goto done;
743 
744 	sel = 0;
745 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
746 		goto done;
747 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
748 		goto done;
749 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
750 		goto done;
751 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
752 		goto done;
753 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
754 		goto done;
755 
756 	/* General purpose registers */
757 	rdx = 0xf00;
758 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
759 		goto done;
760 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
761 		goto done;
762 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
763 		goto done;
764 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
765 		goto done;
766 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
767 		goto done;
768 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
769 		goto done;
770 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
771 		goto done;
772 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
773 		goto done;
774 
775 	/* GDTR, IDTR */
776 	desc_base = 0;
777 	desc_limit = 0xffff;
778 	desc_access = 0;
779 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
780 			    desc_base, desc_limit, desc_access);
781 	if (error != 0)
782 		goto done;
783 
784 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
785 			    desc_base, desc_limit, desc_access);
786 	if (error != 0)
787 		goto done;
788 
789 	/* TR */
790 	desc_base = 0;
791 	desc_limit = 0xffff;
792 	desc_access = 0x0000008b;
793 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
794 	if (error)
795 		goto done;
796 
797 	sel = 0;
798 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
799 		goto done;
800 
801 	/* LDTR */
802 	desc_base = 0;
803 	desc_limit = 0xffff;
804 	desc_access = 0x00000082;
805 	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
806 			    desc_limit, desc_access);
807 	if (error)
808 		goto done;
809 
810 	sel = 0;
811 	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
812 		goto done;
813 
814 	/* XXX cr2, debug registers */
815 
816 	error = 0;
817 done:
818 	return (error);
819 }
820 
821 int
822 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
823 {
824 	int error, i;
825 	struct vm_gpa_pte gpapte;
826 
827 	bzero(&gpapte, sizeof(gpapte));
828 	gpapte.gpa = gpa;
829 
830 	error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
831 
832 	if (error == 0) {
833 		*num = gpapte.ptenum;
834 		for (i = 0; i < gpapte.ptenum; i++)
835 			pte[i] = gpapte.pte[i];
836 	}
837 
838 	return (error);
839 }
840 
841 int
842 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
843 {
844 	int error;
845 	struct vm_hpet_cap cap;
846 
847 	bzero(&cap, sizeof(struct vm_hpet_cap));
848 	error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
849 	if (capabilities != NULL)
850 		*capabilities = cap.capabilities;
851 	return (error);
852 }
853