1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26221828Sgrehan * $FreeBSD$
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#include <sys/cdefs.h>
30221828Sgrehan__FBSDID("$FreeBSD$");
31221828Sgrehan
32221828Sgrehan#include <sys/types.h>
33221828Sgrehan#include <sys/sysctl.h>
34221828Sgrehan#include <sys/ioctl.h>
35221828Sgrehan#include <sys/mman.h>
36221828Sgrehan
37221828Sgrehan#include <machine/specialreg.h>
38221828Sgrehan
39221828Sgrehan#include <stdio.h>
40221828Sgrehan#include <stdlib.h>
41221828Sgrehan#include <assert.h>
42221828Sgrehan#include <string.h>
43221828Sgrehan#include <fcntl.h>
44221828Sgrehan#include <unistd.h>
45221828Sgrehan
46256176Sneel#include <libutil.h>
47256176Sneel
48221828Sgrehan#include <machine/vmm.h>
49221828Sgrehan#include <machine/vmm_dev.h>
50221828Sgrehan
51221828Sgrehan#include "vmmapi.h"
52221828Sgrehan
53256176Sneel#define	MB	(1024 * 1024UL)
54248477Sneel#define	GB	(1024 * 1024 * 1024UL)
55248477Sneel
56221828Sgrehanstruct vmctx {
57221828Sgrehan	int	fd;
58248477Sneel	uint32_t lowmem_limit;
59248477Sneel	enum vm_mmap_style vms;
60248477Sneel	size_t	lowmem;
61248477Sneel	char	*lowmem_addr;
62248477Sneel	size_t	highmem;
63248477Sneel	char	*highmem_addr;
64221828Sgrehan	char	*name;
65221828Sgrehan};
66221828Sgrehan
67221828Sgrehan#define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
68221828Sgrehan#define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
69221828Sgrehan
70221828Sgrehanstatic int
71221828Sgrehanvm_device_open(const char *name)
72221828Sgrehan{
73221828Sgrehan        int fd, len;
74221828Sgrehan        char *vmfile;
75221828Sgrehan
76221828Sgrehan	len = strlen("/dev/vmm/") + strlen(name) + 1;
77221828Sgrehan	vmfile = malloc(len);
78221828Sgrehan	assert(vmfile != NULL);
79221828Sgrehan	snprintf(vmfile, len, "/dev/vmm/%s", name);
80221828Sgrehan
81221828Sgrehan        /* Open the device file */
82221828Sgrehan        fd = open(vmfile, O_RDWR, 0);
83221828Sgrehan
84221828Sgrehan	free(vmfile);
85221828Sgrehan        return (fd);
86221828Sgrehan}
87221828Sgrehan
88221828Sgrehanint
89221828Sgrehanvm_create(const char *name)
90221828Sgrehan{
91221828Sgrehan
92221828Sgrehan	return (CREATE((char *)name));
93221828Sgrehan}
94221828Sgrehan
95221828Sgrehanstruct vmctx *
96221828Sgrehanvm_open(const char *name)
97221828Sgrehan{
98221828Sgrehan	struct vmctx *vm;
99221828Sgrehan
100221828Sgrehan	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
101221828Sgrehan	assert(vm != NULL);
102221828Sgrehan
103221828Sgrehan	vm->fd = -1;
104248477Sneel	vm->lowmem_limit = 3 * GB;
105221828Sgrehan	vm->name = (char *)(vm + 1);
106221828Sgrehan	strcpy(vm->name, name);
107221828Sgrehan
108221828Sgrehan	if ((vm->fd = vm_device_open(vm->name)) < 0)
109221828Sgrehan		goto err;
110221828Sgrehan
111221828Sgrehan	return (vm);
112221828Sgrehanerr:
113221828Sgrehan	vm_destroy(vm);
114221828Sgrehan	return (NULL);
115221828Sgrehan}
116221828Sgrehan
117221828Sgrehanvoid
118221828Sgrehanvm_destroy(struct vmctx *vm)
119221828Sgrehan{
120221828Sgrehan	assert(vm != NULL);
121221828Sgrehan
122221828Sgrehan	if (vm->fd >= 0)
123221828Sgrehan		close(vm->fd);
124241178Sneel	DESTROY(vm->name);
125241178Sneel
126221828Sgrehan	free(vm);
127221828Sgrehan}
128221828Sgrehan
129221828Sgrehanint
130256176Sneelvm_parse_memsize(const char *optarg, size_t *ret_memsize)
131256176Sneel{
132256176Sneel	char *endptr;
133256176Sneel	size_t optval;
134256176Sneel	int error;
135256176Sneel
136256176Sneel	optval = strtoul(optarg, &endptr, 0);
137256176Sneel	if (*optarg != '\0' && *endptr == '\0') {
138256176Sneel		/*
139256176Sneel		 * For the sake of backward compatibility if the memory size
140256176Sneel		 * specified on the command line is less than a megabyte then
141256176Sneel		 * it is interpreted as being in units of MB.
142256176Sneel		 */
143256176Sneel		if (optval < MB)
144256176Sneel			optval *= MB;
145256176Sneel		*ret_memsize = optval;
146256176Sneel		error = 0;
147256176Sneel	} else
148256176Sneel		error = expand_number(optarg, ret_memsize);
149256176Sneel
150256176Sneel	return (error);
151256176Sneel}
152256176Sneel
153256176Sneelint
154256072Sneelvm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len,
155256072Sneel		  int *wired)
156221828Sgrehan{
157221828Sgrehan	int error;
158221828Sgrehan	struct vm_memory_segment seg;
159221828Sgrehan
160221828Sgrehan	bzero(&seg, sizeof(seg));
161221828Sgrehan	seg.gpa = gpa;
162221828Sgrehan	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
163221828Sgrehan	*ret_len = seg.len;
164256072Sneel	if (wired != NULL)
165256072Sneel		*wired = seg.wired;
166221828Sgrehan	return (error);
167221828Sgrehan}
168221828Sgrehan
169248477Sneeluint32_t
170248477Sneelvm_get_lowmem_limit(struct vmctx *ctx)
171221828Sgrehan{
172248477Sneel
173248477Sneel	return (ctx->lowmem_limit);
174248477Sneel}
175248477Sneel
176248477Sneelvoid
177248477Sneelvm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
178248477Sneel{
179248477Sneel
180248477Sneel	ctx->lowmem_limit = limit;
181248477Sneel}
182248477Sneel
183248477Sneelstatic int
184248477Sneelsetup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
185248477Sneel{
186221828Sgrehan	int error;
187221828Sgrehan	struct vm_memory_segment seg;
188221828Sgrehan
189221828Sgrehan	/*
190221828Sgrehan	 * Create and optionally map 'len' bytes of memory at guest
191221828Sgrehan	 * physical address 'gpa'
192221828Sgrehan	 */
193221828Sgrehan	bzero(&seg, sizeof(seg));
194221828Sgrehan	seg.gpa = gpa;
195221828Sgrehan	seg.len = len;
196221828Sgrehan	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
197248477Sneel	if (error == 0 && addr != NULL) {
198248477Sneel		*addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
199221828Sgrehan				ctx->fd, gpa);
200221828Sgrehan	}
201221828Sgrehan	return (error);
202221828Sgrehan}
203221828Sgrehan
204248477Sneelint
205248477Sneelvm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
206221828Sgrehan{
207248477Sneel	char **addr;
208248477Sneel	int error;
209221828Sgrehan
210248477Sneel	/* XXX VM_MMAP_SPARSE not implemented yet */
211248477Sneel	assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
212248477Sneel	ctx->vms = vms;
213248477Sneel
214248477Sneel	/*
215248477Sneel	 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
216248477Sneel	 * create another 'highmem' segment above 4GB for the remainder.
217248477Sneel	 */
218248477Sneel	if (memsize > ctx->lowmem_limit) {
219248477Sneel		ctx->lowmem = ctx->lowmem_limit;
220248477Sneel		ctx->highmem = memsize - ctx->lowmem;
221248477Sneel	} else {
222248477Sneel		ctx->lowmem = memsize;
223248477Sneel		ctx->highmem = 0;
224248477Sneel	}
225248477Sneel
226248477Sneel	if (ctx->lowmem > 0) {
227248477Sneel		addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
228248477Sneel		error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
229248477Sneel		if (error)
230248477Sneel			return (error);
231248477Sneel	}
232248477Sneel
233248477Sneel	if (ctx->highmem > 0) {
234248477Sneel		addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
235248477Sneel		error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
236248477Sneel		if (error)
237248477Sneel			return (error);
238248477Sneel	}
239248477Sneel
240248477Sneel	return (0);
241221828Sgrehan}
242221828Sgrehan
243248477Sneelvoid *
244248477Sneelvm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
245248477Sneel{
246248477Sneel
247248477Sneel	/* XXX VM_MMAP_SPARSE not implemented yet */
248248477Sneel	assert(ctx->vms == VM_MMAP_ALL);
249248477Sneel
250248477Sneel	if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
251248477Sneel		return ((void *)(ctx->lowmem_addr + gaddr));
252248477Sneel
253248477Sneel	if (gaddr >= 4*GB) {
254248477Sneel		gaddr -= 4*GB;
255248477Sneel		if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
256248477Sneel			return ((void *)(ctx->highmem_addr + gaddr));
257248477Sneel	}
258248477Sneel
259248477Sneel	return (NULL);
260248477Sneel}
261248477Sneel
262221828Sgrehanint
263221828Sgrehanvm_set_desc(struct vmctx *ctx, int vcpu, int reg,
264221828Sgrehan	    uint64_t base, uint32_t limit, uint32_t access)
265221828Sgrehan{
266221828Sgrehan	int error;
267221828Sgrehan	struct vm_seg_desc vmsegdesc;
268221828Sgrehan
269221828Sgrehan	bzero(&vmsegdesc, sizeof(vmsegdesc));
270221828Sgrehan	vmsegdesc.cpuid = vcpu;
271221828Sgrehan	vmsegdesc.regnum = reg;
272221828Sgrehan	vmsegdesc.desc.base = base;
273221828Sgrehan	vmsegdesc.desc.limit = limit;
274221828Sgrehan	vmsegdesc.desc.access = access;
275221828Sgrehan
276221828Sgrehan	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
277221828Sgrehan	return (error);
278221828Sgrehan}
279221828Sgrehan
280221828Sgrehanint
281221828Sgrehanvm_get_desc(struct vmctx *ctx, int vcpu, int reg,
282221828Sgrehan	    uint64_t *base, uint32_t *limit, uint32_t *access)
283221828Sgrehan{
284221828Sgrehan	int error;
285221828Sgrehan	struct vm_seg_desc vmsegdesc;
286221828Sgrehan
287221828Sgrehan	bzero(&vmsegdesc, sizeof(vmsegdesc));
288221828Sgrehan	vmsegdesc.cpuid = vcpu;
289221828Sgrehan	vmsegdesc.regnum = reg;
290221828Sgrehan
291221828Sgrehan	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
292221828Sgrehan	if (error == 0) {
293221828Sgrehan		*base = vmsegdesc.desc.base;
294221828Sgrehan		*limit = vmsegdesc.desc.limit;
295221828Sgrehan		*access = vmsegdesc.desc.access;
296221828Sgrehan	}
297221828Sgrehan	return (error);
298221828Sgrehan}
299221828Sgrehan
300221828Sgrehanint
301221828Sgrehanvm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
302221828Sgrehan{
303221828Sgrehan	int error;
304221828Sgrehan	struct vm_register vmreg;
305221828Sgrehan
306221828Sgrehan	bzero(&vmreg, sizeof(vmreg));
307221828Sgrehan	vmreg.cpuid = vcpu;
308221828Sgrehan	vmreg.regnum = reg;
309221828Sgrehan	vmreg.regval = val;
310221828Sgrehan
311221828Sgrehan	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
312221828Sgrehan	return (error);
313221828Sgrehan}
314221828Sgrehan
315221828Sgrehanint
316221828Sgrehanvm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
317221828Sgrehan{
318221828Sgrehan	int error;
319221828Sgrehan	struct vm_register vmreg;
320221828Sgrehan
321221828Sgrehan	bzero(&vmreg, sizeof(vmreg));
322221828Sgrehan	vmreg.cpuid = vcpu;
323221828Sgrehan	vmreg.regnum = reg;
324221828Sgrehan
325221828Sgrehan	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
326221828Sgrehan	*ret_val = vmreg.regval;
327221828Sgrehan	return (error);
328221828Sgrehan}
329221828Sgrehan
330221828Sgrehanint
331221828Sgrehanvm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
332221828Sgrehan{
333221828Sgrehan	int error;
334221828Sgrehan	struct vm_run vmrun;
335221828Sgrehan
336221828Sgrehan	bzero(&vmrun, sizeof(vmrun));
337221828Sgrehan	vmrun.cpuid = vcpu;
338221828Sgrehan	vmrun.rip = rip;
339221828Sgrehan
340221828Sgrehan	error = ioctl(ctx->fd, VM_RUN, &vmrun);
341221828Sgrehan	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
342221828Sgrehan	return (error);
343221828Sgrehan}
344221828Sgrehan
345221828Sgrehanstatic int
346221828Sgrehanvm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
347221828Sgrehan		     int vector, int error_code, int error_code_valid)
348221828Sgrehan{
349221828Sgrehan	struct vm_event ev;
350221828Sgrehan
351221828Sgrehan	bzero(&ev, sizeof(ev));
352221828Sgrehan	ev.cpuid = vcpu;
353221828Sgrehan	ev.type = type;
354221828Sgrehan	ev.vector = vector;
355221828Sgrehan	ev.error_code = error_code;
356221828Sgrehan	ev.error_code_valid = error_code_valid;
357221828Sgrehan
358221828Sgrehan	return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
359221828Sgrehan}
360221828Sgrehan
361221828Sgrehanint
362221828Sgrehanvm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
363221828Sgrehan		int vector)
364221828Sgrehan{
365221828Sgrehan
366221828Sgrehan	return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
367221828Sgrehan}
368221828Sgrehan
369221828Sgrehanint
370221828Sgrehanvm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
371221828Sgrehan		 int vector, int error_code)
372221828Sgrehan{
373221828Sgrehan
374221828Sgrehan	return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
375221828Sgrehan}
376221828Sgrehan
377221828Sgrehanint
378239026Sneelvm_apicid2vcpu(struct vmctx *ctx, int apicid)
379239026Sneel{
380239026Sneel	/*
381239026Sneel	 * The apic id associated with the 'vcpu' has the same numerical value
382239026Sneel	 * as the 'vcpu' itself.
383239026Sneel	 */
384239026Sneel	return (apicid);
385239026Sneel}
386239026Sneel
387239026Sneelint
388221828Sgrehanvm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
389221828Sgrehan{
390221828Sgrehan	struct vm_lapic_irq vmirq;
391221828Sgrehan
392221828Sgrehan	bzero(&vmirq, sizeof(vmirq));
393221828Sgrehan	vmirq.cpuid = vcpu;
394221828Sgrehan	vmirq.vector = vector;
395221828Sgrehan
396221828Sgrehan	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
397221828Sgrehan}
398221828Sgrehan
399221828Sgrehanint
400221828Sgrehanvm_inject_nmi(struct vmctx *ctx, int vcpu)
401221828Sgrehan{
402221828Sgrehan	struct vm_nmi vmnmi;
403221828Sgrehan
404221828Sgrehan	bzero(&vmnmi, sizeof(vmnmi));
405221828Sgrehan	vmnmi.cpuid = vcpu;
406221828Sgrehan
407221828Sgrehan	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
408221828Sgrehan}
409221828Sgrehan
410241486Sneelstatic struct {
411241486Sneel	const char	*name;
412241486Sneel	int		type;
413241486Sneel} capstrmap[] = {
414241486Sneel	{ "hlt_exit",		VM_CAP_HALT_EXIT },
415241486Sneel	{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
416241486Sneel	{ "pause_exit",		VM_CAP_PAUSE_EXIT },
417241486Sneel	{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
418256869Sneel	{ "enable_invpcid",	VM_CAP_ENABLE_INVPCID },
419241486Sneel	{ 0 }
420241486Sneel};
421241486Sneel
422221828Sgrehanint
423221828Sgrehanvm_capability_name2type(const char *capname)
424221828Sgrehan{
425221828Sgrehan	int i;
426221828Sgrehan
427221828Sgrehan	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
428221828Sgrehan		if (strcmp(capstrmap[i].name, capname) == 0)
429221828Sgrehan			return (capstrmap[i].type);
430221828Sgrehan	}
431221828Sgrehan
432221828Sgrehan	return (-1);
433221828Sgrehan}
434221828Sgrehan
435241486Sneelconst char *
436241486Sneelvm_capability_type2name(int type)
437241486Sneel{
438241486Sneel	int i;
439241486Sneel
440241486Sneel	for (i = 0; capstrmap[i].name != NULL; i++) {
441241486Sneel		if (capstrmap[i].type == type)
442241486Sneel			return (capstrmap[i].name);
443241486Sneel	}
444241486Sneel
445241486Sneel	return (NULL);
446241486Sneel}
447241486Sneel
448221828Sgrehanint
449221828Sgrehanvm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
450221828Sgrehan		  int *retval)
451221828Sgrehan{
452221828Sgrehan	int error;
453221828Sgrehan	struct vm_capability vmcap;
454221828Sgrehan
455221828Sgrehan	bzero(&vmcap, sizeof(vmcap));
456221828Sgrehan	vmcap.cpuid = vcpu;
457221828Sgrehan	vmcap.captype = cap;
458221828Sgrehan
459221828Sgrehan	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
460221828Sgrehan	*retval = vmcap.capval;
461221828Sgrehan	return (error);
462221828Sgrehan}
463221828Sgrehan
464221828Sgrehanint
465221828Sgrehanvm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
466221828Sgrehan{
467221828Sgrehan	struct vm_capability vmcap;
468221828Sgrehan
469221828Sgrehan	bzero(&vmcap, sizeof(vmcap));
470221828Sgrehan	vmcap.cpuid = vcpu;
471221828Sgrehan	vmcap.captype = cap;
472221828Sgrehan	vmcap.capval = val;
473221828Sgrehan
474221828Sgrehan	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
475221828Sgrehan}
476221828Sgrehan
477221828Sgrehanint
478221828Sgrehanvm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
479221828Sgrehan{
480221828Sgrehan	struct vm_pptdev pptdev;
481221828Sgrehan
482221828Sgrehan	bzero(&pptdev, sizeof(pptdev));
483221828Sgrehan	pptdev.bus = bus;
484221828Sgrehan	pptdev.slot = slot;
485221828Sgrehan	pptdev.func = func;
486221828Sgrehan
487221828Sgrehan	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
488221828Sgrehan}
489221828Sgrehan
490221828Sgrehanint
491221828Sgrehanvm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
492221828Sgrehan{
493221828Sgrehan	struct vm_pptdev pptdev;
494221828Sgrehan
495221828Sgrehan	bzero(&pptdev, sizeof(pptdev));
496221828Sgrehan	pptdev.bus = bus;
497221828Sgrehan	pptdev.slot = slot;
498221828Sgrehan	pptdev.func = func;
499221828Sgrehan
500221828Sgrehan	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
501221828Sgrehan}
502221828Sgrehan
503221828Sgrehanint
504221828Sgrehanvm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
505221828Sgrehan		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
506221828Sgrehan{
507221828Sgrehan	struct vm_pptdev_mmio pptmmio;
508221828Sgrehan
509221828Sgrehan	bzero(&pptmmio, sizeof(pptmmio));
510221828Sgrehan	pptmmio.bus = bus;
511221828Sgrehan	pptmmio.slot = slot;
512221828Sgrehan	pptmmio.func = func;
513221828Sgrehan	pptmmio.gpa = gpa;
514221828Sgrehan	pptmmio.len = len;
515221828Sgrehan	pptmmio.hpa = hpa;
516221828Sgrehan
517221828Sgrehan	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
518221828Sgrehan}
519221828Sgrehan
520221828Sgrehanint
521221828Sgrehanvm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
522221828Sgrehan	     int destcpu, int vector, int numvec)
523221828Sgrehan{
524221828Sgrehan	struct vm_pptdev_msi pptmsi;
525221828Sgrehan
526221828Sgrehan	bzero(&pptmsi, sizeof(pptmsi));
527221828Sgrehan	pptmsi.vcpu = vcpu;
528221828Sgrehan	pptmsi.bus = bus;
529221828Sgrehan	pptmsi.slot = slot;
530221828Sgrehan	pptmsi.func = func;
531221828Sgrehan	pptmsi.destcpu = destcpu;
532221828Sgrehan	pptmsi.vector = vector;
533221828Sgrehan	pptmsi.numvec = numvec;
534221828Sgrehan
535221828Sgrehan	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
536221828Sgrehan}
537221828Sgrehan
538234761Sgrehanint
539234761Sgrehanvm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
540234761Sgrehan	      int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
541234761Sgrehan{
542234761Sgrehan	struct vm_pptdev_msix pptmsix;
543234761Sgrehan
544234761Sgrehan	bzero(&pptmsix, sizeof(pptmsix));
545234761Sgrehan	pptmsix.vcpu = vcpu;
546234761Sgrehan	pptmsix.bus = bus;
547234761Sgrehan	pptmsix.slot = slot;
548234761Sgrehan	pptmsix.func = func;
549234761Sgrehan	pptmsix.idx = idx;
550234761Sgrehan	pptmsix.msg = msg;
551234761Sgrehan	pptmsix.addr = addr;
552234761Sgrehan	pptmsix.vector_control = vector_control;
553234761Sgrehan
554234761Sgrehan	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
555234761Sgrehan}
556234761Sgrehan
557221828Sgrehanuint64_t *
558221828Sgrehanvm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
559221828Sgrehan	     int *ret_entries)
560221828Sgrehan{
561221828Sgrehan	int error;
562221828Sgrehan
563221828Sgrehan	static struct vm_stats vmstats;
564221828Sgrehan
565221828Sgrehan	vmstats.cpuid = vcpu;
566221828Sgrehan
567221828Sgrehan	error = ioctl(ctx->fd, VM_STATS, &vmstats);
568221828Sgrehan	if (error == 0) {
569221828Sgrehan		if (ret_entries)
570221828Sgrehan			*ret_entries = vmstats.num_entries;
571221828Sgrehan		if (ret_tv)
572221828Sgrehan			*ret_tv = vmstats.tv;
573221828Sgrehan		return (vmstats.statbuf);
574221828Sgrehan	} else
575221828Sgrehan		return (NULL);
576221828Sgrehan}
577221828Sgrehan
578221828Sgrehanconst char *
579221828Sgrehanvm_get_stat_desc(struct vmctx *ctx, int index)
580221828Sgrehan{
581221828Sgrehan	static struct vm_stat_desc statdesc;
582221828Sgrehan
583221828Sgrehan	statdesc.index = index;
584221828Sgrehan	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
585221828Sgrehan		return (statdesc.desc);
586221828Sgrehan	else
587221828Sgrehan		return (NULL);
588221828Sgrehan}
589221828Sgrehan
590240922Sneelint
591240922Sneelvm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
592240922Sneel{
593240922Sneel	int error;
594240922Sneel	struct vm_x2apic x2apic;
595240922Sneel
596240922Sneel	bzero(&x2apic, sizeof(x2apic));
597240922Sneel	x2apic.cpuid = vcpu;
598240922Sneel
599240922Sneel	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
600240922Sneel	*state = x2apic.state;
601240922Sneel	return (error);
602240922Sneel}
603240922Sneel
604240922Sneelint
605240922Sneelvm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
606240922Sneel{
607240922Sneel	int error;
608240922Sneel	struct vm_x2apic x2apic;
609240922Sneel
610240922Sneel	bzero(&x2apic, sizeof(x2apic));
611240922Sneel	x2apic.cpuid = vcpu;
612240922Sneel	x2apic.state = state;
613240922Sneel
614240922Sneel	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
615240922Sneel
616240922Sneel	return (error);
617240922Sneel}
618240922Sneel
619221828Sgrehan/*
620221828Sgrehan * From Intel Vol 3a:
621221828Sgrehan * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
622221828Sgrehan */
623221828Sgrehanint
624221828Sgrehanvcpu_reset(struct vmctx *vmctx, int vcpu)
625221828Sgrehan{
626221828Sgrehan	int error;
627221828Sgrehan	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
628221828Sgrehan	uint32_t desc_access, desc_limit;
629221828Sgrehan	uint16_t sel;
630221828Sgrehan
631221828Sgrehan	zero = 0;
632221828Sgrehan
633221828Sgrehan	rflags = 0x2;
634221828Sgrehan	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
635221828Sgrehan	if (error)
636221828Sgrehan		goto done;
637221828Sgrehan
638221828Sgrehan	rip = 0xfff0;
639221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
640221828Sgrehan		goto done;
641221828Sgrehan
642221828Sgrehan	cr0 = CR0_NE;
643221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
644221828Sgrehan		goto done;
645221828Sgrehan
646221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
647221828Sgrehan		goto done;
648221828Sgrehan
649239025Sneel	cr4 = 0;
650221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
651221828Sgrehan		goto done;
652221828Sgrehan
653221828Sgrehan	/*
654221828Sgrehan	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
655221828Sgrehan	 */
656221828Sgrehan	desc_base = 0xffff0000;
657221828Sgrehan	desc_limit = 0xffff;
658221828Sgrehan	desc_access = 0x0093;
659221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
660221828Sgrehan			    desc_base, desc_limit, desc_access);
661221828Sgrehan	if (error)
662221828Sgrehan		goto done;
663221828Sgrehan
664221828Sgrehan	sel = 0xf000;
665221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
666221828Sgrehan		goto done;
667221828Sgrehan
668221828Sgrehan	/*
669221828Sgrehan	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
670221828Sgrehan	 */
671221828Sgrehan	desc_base = 0;
672221828Sgrehan	desc_limit = 0xffff;
673221828Sgrehan	desc_access = 0x0093;
674221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
675221828Sgrehan			    desc_base, desc_limit, desc_access);
676221828Sgrehan	if (error)
677221828Sgrehan		goto done;
678221828Sgrehan
679221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
680221828Sgrehan			    desc_base, desc_limit, desc_access);
681221828Sgrehan	if (error)
682221828Sgrehan		goto done;
683221828Sgrehan
684221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
685221828Sgrehan			    desc_base, desc_limit, desc_access);
686221828Sgrehan	if (error)
687221828Sgrehan		goto done;
688221828Sgrehan
689221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
690221828Sgrehan			    desc_base, desc_limit, desc_access);
691221828Sgrehan	if (error)
692221828Sgrehan		goto done;
693221828Sgrehan
694221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
695221828Sgrehan			    desc_base, desc_limit, desc_access);
696221828Sgrehan	if (error)
697221828Sgrehan		goto done;
698221828Sgrehan
699221828Sgrehan	sel = 0;
700221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
701221828Sgrehan		goto done;
702221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
703221828Sgrehan		goto done;
704221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
705221828Sgrehan		goto done;
706221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
707221828Sgrehan		goto done;
708221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
709221828Sgrehan		goto done;
710221828Sgrehan
711221828Sgrehan	/* General purpose registers */
712221828Sgrehan	rdx = 0xf00;
713221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
714221828Sgrehan		goto done;
715221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
716221828Sgrehan		goto done;
717221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
718221828Sgrehan		goto done;
719221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
720221828Sgrehan		goto done;
721221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
722221828Sgrehan		goto done;
723221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
724221828Sgrehan		goto done;
725221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
726221828Sgrehan		goto done;
727221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
728221828Sgrehan		goto done;
729221828Sgrehan
730221828Sgrehan	/* GDTR, IDTR */
731221828Sgrehan	desc_base = 0;
732221828Sgrehan	desc_limit = 0xffff;
733221828Sgrehan	desc_access = 0;
734221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
735221828Sgrehan			    desc_base, desc_limit, desc_access);
736221828Sgrehan	if (error != 0)
737221828Sgrehan		goto done;
738221828Sgrehan
739221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
740221828Sgrehan			    desc_base, desc_limit, desc_access);
741221828Sgrehan	if (error != 0)
742221828Sgrehan		goto done;
743221828Sgrehan
744221828Sgrehan	/* TR */
745221828Sgrehan	desc_base = 0;
746221828Sgrehan	desc_limit = 0xffff;
747221828Sgrehan	desc_access = 0x0000008b;
748221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
749221828Sgrehan	if (error)
750221828Sgrehan		goto done;
751221828Sgrehan
752221828Sgrehan	sel = 0;
753221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
754221828Sgrehan		goto done;
755221828Sgrehan
756221828Sgrehan	/* LDTR */
757221828Sgrehan	desc_base = 0;
758221828Sgrehan	desc_limit = 0xffff;
759221828Sgrehan	desc_access = 0x00000082;
760221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
761221828Sgrehan			    desc_limit, desc_access);
762221828Sgrehan	if (error)
763221828Sgrehan		goto done;
764221828Sgrehan
765221828Sgrehan	sel = 0;
766221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
767221828Sgrehan		goto done;
768221828Sgrehan
769221828Sgrehan	/* XXX cr2, debug registers */
770221828Sgrehan
771221828Sgrehan	error = 0;
772221828Sgrehandone:
773221828Sgrehan	return (error);
774221828Sgrehan}
775256072Sneel
776256072Sneelint
777256072Sneelvm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
778256072Sneel{
779256072Sneel	int error, i;
780256072Sneel	struct vm_gpa_pte gpapte;
781256072Sneel
782256072Sneel	bzero(&gpapte, sizeof(gpapte));
783256072Sneel	gpapte.gpa = gpa;
784256072Sneel
785256072Sneel	error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
786256072Sneel
787256072Sneel	if (error == 0) {
788256072Sneel		*num = gpapte.ptenum;
789256072Sneel		for (i = 0; i < gpapte.ptenum; i++)
790256072Sneel			pte[i] = gpapte.pte[i];
791256072Sneel	}
792256072Sneel
793256072Sneel	return (error);
794256072Sneel}
795