vmmapi.c revision 239025
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
46221828Sgrehan#include <machine/vmm.h>
47221828Sgrehan#include <machine/vmm_dev.h>
48221828Sgrehan
49221828Sgrehan#include "vmmapi.h"
50221828Sgrehan#include "mptable.h"
51221828Sgrehan
52221828Sgrehan#define BIOS_ROM_BASE		(0xf0000)
53221828Sgrehan#define BIOS_ROM_SIZE		(0x10000)
54221828Sgrehan
55221828Sgrehanstruct vmctx {
56221828Sgrehan	int	fd;
57221828Sgrehan	char	*name;
58221828Sgrehan};
59221828Sgrehan
60221828Sgrehan#define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
61221828Sgrehan#define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
62221828Sgrehan
63221828Sgrehanstatic int
64221828Sgrehanvm_device_open(const char *name)
65221828Sgrehan{
66221828Sgrehan        int fd, len;
67221828Sgrehan        char *vmfile;
68221828Sgrehan
69221828Sgrehan	len = strlen("/dev/vmm/") + strlen(name) + 1;
70221828Sgrehan	vmfile = malloc(len);
71221828Sgrehan	assert(vmfile != NULL);
72221828Sgrehan	snprintf(vmfile, len, "/dev/vmm/%s", name);
73221828Sgrehan
74221828Sgrehan        /* Open the device file */
75221828Sgrehan        fd = open(vmfile, O_RDWR, 0);
76221828Sgrehan
77221828Sgrehan	free(vmfile);
78221828Sgrehan        return (fd);
79221828Sgrehan}
80221828Sgrehan
81221828Sgrehanint
82221828Sgrehanvm_create(const char *name)
83221828Sgrehan{
84221828Sgrehan
85221828Sgrehan	return (CREATE((char *)name));
86221828Sgrehan}
87221828Sgrehan
88221828Sgrehanstruct vmctx *
89221828Sgrehanvm_open(const char *name)
90221828Sgrehan{
91221828Sgrehan	struct vmctx *vm;
92221828Sgrehan
93221828Sgrehan	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
94221828Sgrehan	assert(vm != NULL);
95221828Sgrehan
96221828Sgrehan	vm->fd = -1;
97221828Sgrehan	vm->name = (char *)(vm + 1);
98221828Sgrehan	strcpy(vm->name, name);
99221828Sgrehan
100221828Sgrehan	if ((vm->fd = vm_device_open(vm->name)) < 0)
101221828Sgrehan		goto err;
102221828Sgrehan
103221828Sgrehan	return (vm);
104221828Sgrehanerr:
105221828Sgrehan	vm_destroy(vm);
106221828Sgrehan	return (NULL);
107221828Sgrehan}
108221828Sgrehan
109221828Sgrehanvoid
110221828Sgrehanvm_destroy(struct vmctx *vm)
111221828Sgrehan{
112221828Sgrehan	assert(vm != NULL);
113221828Sgrehan
114221828Sgrehan	DESTROY(vm->name);
115221828Sgrehan	if (vm->fd >= 0)
116221828Sgrehan		close(vm->fd);
117221828Sgrehan	free(vm);
118221828Sgrehan}
119221828Sgrehan
120221828Sgrehanint
121221828Sgrehanvm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa,
122221828Sgrehan		  vm_paddr_t *ret_hpa, size_t *ret_len)
123221828Sgrehan{
124221828Sgrehan	int error;
125221828Sgrehan	struct vm_memory_segment seg;
126221828Sgrehan
127221828Sgrehan	bzero(&seg, sizeof(seg));
128221828Sgrehan	seg.gpa = gpa;
129221828Sgrehan	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
130221828Sgrehan	*ret_hpa = seg.hpa;
131221828Sgrehan	*ret_len = seg.len;
132221828Sgrehan	return (error);
133221828Sgrehan}
134221828Sgrehan
135221828Sgrehanint
136221828Sgrehanvm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **mapaddr)
137221828Sgrehan{
138221828Sgrehan	int error;
139221828Sgrehan	struct vm_memory_segment seg;
140221828Sgrehan
141221828Sgrehan	/*
142221828Sgrehan	 * Create and optionally map 'len' bytes of memory at guest
143221828Sgrehan	 * physical address 'gpa'
144221828Sgrehan	 */
145221828Sgrehan	bzero(&seg, sizeof(seg));
146221828Sgrehan	seg.gpa = gpa;
147221828Sgrehan	seg.len = len;
148221828Sgrehan	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
149221828Sgrehan	if (error == 0 && mapaddr != NULL) {
150221828Sgrehan		*mapaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
151221828Sgrehan				ctx->fd, gpa);
152221828Sgrehan	}
153221828Sgrehan	return (error);
154221828Sgrehan}
155221828Sgrehan
156221828Sgrehanchar *
157221828Sgrehanvm_map_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len)
158221828Sgrehan{
159221828Sgrehan
160221828Sgrehan	/* Map 'len' bytes of memory at guest physical address 'gpa' */
161221828Sgrehan	return ((char *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
162221828Sgrehan		     ctx->fd, gpa));
163221828Sgrehan}
164221828Sgrehan
165221828Sgrehanint
166221828Sgrehanvm_set_desc(struct vmctx *ctx, int vcpu, int reg,
167221828Sgrehan	    uint64_t base, uint32_t limit, uint32_t access)
168221828Sgrehan{
169221828Sgrehan	int error;
170221828Sgrehan	struct vm_seg_desc vmsegdesc;
171221828Sgrehan
172221828Sgrehan	bzero(&vmsegdesc, sizeof(vmsegdesc));
173221828Sgrehan	vmsegdesc.cpuid = vcpu;
174221828Sgrehan	vmsegdesc.regnum = reg;
175221828Sgrehan	vmsegdesc.desc.base = base;
176221828Sgrehan	vmsegdesc.desc.limit = limit;
177221828Sgrehan	vmsegdesc.desc.access = access;
178221828Sgrehan
179221828Sgrehan	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
180221828Sgrehan	return (error);
181221828Sgrehan}
182221828Sgrehan
183221828Sgrehanint
184221828Sgrehanvm_get_desc(struct vmctx *ctx, int vcpu, int reg,
185221828Sgrehan	    uint64_t *base, uint32_t *limit, uint32_t *access)
186221828Sgrehan{
187221828Sgrehan	int error;
188221828Sgrehan	struct vm_seg_desc vmsegdesc;
189221828Sgrehan
190221828Sgrehan	bzero(&vmsegdesc, sizeof(vmsegdesc));
191221828Sgrehan	vmsegdesc.cpuid = vcpu;
192221828Sgrehan	vmsegdesc.regnum = reg;
193221828Sgrehan
194221828Sgrehan	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
195221828Sgrehan	if (error == 0) {
196221828Sgrehan		*base = vmsegdesc.desc.base;
197221828Sgrehan		*limit = vmsegdesc.desc.limit;
198221828Sgrehan		*access = vmsegdesc.desc.access;
199221828Sgrehan	}
200221828Sgrehan	return (error);
201221828Sgrehan}
202221828Sgrehan
203221828Sgrehanint
204221828Sgrehanvm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
205221828Sgrehan{
206221828Sgrehan	int error;
207221828Sgrehan	struct vm_register vmreg;
208221828Sgrehan
209221828Sgrehan	bzero(&vmreg, sizeof(vmreg));
210221828Sgrehan	vmreg.cpuid = vcpu;
211221828Sgrehan	vmreg.regnum = reg;
212221828Sgrehan	vmreg.regval = val;
213221828Sgrehan
214221828Sgrehan	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
215221828Sgrehan	return (error);
216221828Sgrehan}
217221828Sgrehan
218221828Sgrehanint
219221828Sgrehanvm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
220221828Sgrehan{
221221828Sgrehan	int error;
222221828Sgrehan	struct vm_register vmreg;
223221828Sgrehan
224221828Sgrehan	bzero(&vmreg, sizeof(vmreg));
225221828Sgrehan	vmreg.cpuid = vcpu;
226221828Sgrehan	vmreg.regnum = reg;
227221828Sgrehan
228221828Sgrehan	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
229221828Sgrehan	*ret_val = vmreg.regval;
230221828Sgrehan	return (error);
231221828Sgrehan}
232221828Sgrehan
233221828Sgrehanint
234221828Sgrehanvm_get_pinning(struct vmctx *ctx, int vcpu, int *host_cpuid)
235221828Sgrehan{
236221828Sgrehan	int error;
237221828Sgrehan	struct vm_pin vmpin;
238221828Sgrehan
239221828Sgrehan	bzero(&vmpin, sizeof(vmpin));
240221828Sgrehan	vmpin.vm_cpuid = vcpu;
241221828Sgrehan
242221828Sgrehan	error = ioctl(ctx->fd, VM_GET_PINNING, &vmpin);
243221828Sgrehan	*host_cpuid = vmpin.host_cpuid;
244221828Sgrehan	return (error);
245221828Sgrehan}
246221828Sgrehan
247221828Sgrehanint
248221828Sgrehanvm_set_pinning(struct vmctx *ctx, int vcpu, int host_cpuid)
249221828Sgrehan{
250221828Sgrehan	int error;
251221828Sgrehan	struct vm_pin vmpin;
252221828Sgrehan
253221828Sgrehan	bzero(&vmpin, sizeof(vmpin));
254221828Sgrehan	vmpin.vm_cpuid = vcpu;
255221828Sgrehan	vmpin.host_cpuid = host_cpuid;
256221828Sgrehan
257221828Sgrehan	error = ioctl(ctx->fd, VM_SET_PINNING, &vmpin);
258221828Sgrehan	return (error);
259221828Sgrehan}
260221828Sgrehan
261221828Sgrehanint
262221828Sgrehanvm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
263221828Sgrehan{
264221828Sgrehan	int error;
265221828Sgrehan	struct vm_run vmrun;
266221828Sgrehan
267221828Sgrehan	bzero(&vmrun, sizeof(vmrun));
268221828Sgrehan	vmrun.cpuid = vcpu;
269221828Sgrehan	vmrun.rip = rip;
270221828Sgrehan
271221828Sgrehan	error = ioctl(ctx->fd, VM_RUN, &vmrun);
272221828Sgrehan	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
273221828Sgrehan	return (error);
274221828Sgrehan}
275221828Sgrehan
276221828Sgrehanstatic int
277221828Sgrehanvm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
278221828Sgrehan		     int vector, int error_code, int error_code_valid)
279221828Sgrehan{
280221828Sgrehan	struct vm_event ev;
281221828Sgrehan
282221828Sgrehan	bzero(&ev, sizeof(ev));
283221828Sgrehan	ev.cpuid = vcpu;
284221828Sgrehan	ev.type = type;
285221828Sgrehan	ev.vector = vector;
286221828Sgrehan	ev.error_code = error_code;
287221828Sgrehan	ev.error_code_valid = error_code_valid;
288221828Sgrehan
289221828Sgrehan	return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
290221828Sgrehan}
291221828Sgrehan
292221828Sgrehanint
293221828Sgrehanvm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
294221828Sgrehan		int vector)
295221828Sgrehan{
296221828Sgrehan
297221828Sgrehan	return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
298221828Sgrehan}
299221828Sgrehan
300221828Sgrehanint
301221828Sgrehanvm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
302221828Sgrehan		 int vector, int error_code)
303221828Sgrehan{
304221828Sgrehan
305221828Sgrehan	return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
306221828Sgrehan}
307221828Sgrehan
308221828Sgrehanint
309221828Sgrehanvm_build_tables(struct vmctx *ctxt, int ncpu, void *oemtbl, int oemtblsz)
310221828Sgrehan{
311221828Sgrehan
312221828Sgrehan	return (vm_build_mptable(ctxt, BIOS_ROM_BASE, BIOS_ROM_SIZE, ncpu,
313221828Sgrehan				 oemtbl, oemtblsz));
314221828Sgrehan}
315221828Sgrehan
316221828Sgrehanint
317221828Sgrehanvm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
318221828Sgrehan{
319221828Sgrehan	struct vm_lapic_irq vmirq;
320221828Sgrehan
321221828Sgrehan	bzero(&vmirq, sizeof(vmirq));
322221828Sgrehan	vmirq.cpuid = vcpu;
323221828Sgrehan	vmirq.vector = vector;
324221828Sgrehan
325221828Sgrehan	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
326221828Sgrehan}
327221828Sgrehan
328221828Sgrehanint
329221828Sgrehanvm_inject_nmi(struct vmctx *ctx, int vcpu)
330221828Sgrehan{
331221828Sgrehan	struct vm_nmi vmnmi;
332221828Sgrehan
333221828Sgrehan	bzero(&vmnmi, sizeof(vmnmi));
334221828Sgrehan	vmnmi.cpuid = vcpu;
335221828Sgrehan
336221828Sgrehan	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
337221828Sgrehan}
338221828Sgrehan
339221828Sgrehanint
340221828Sgrehanvm_capability_name2type(const char *capname)
341221828Sgrehan{
342221828Sgrehan	int i;
343221828Sgrehan
344221828Sgrehan	static struct {
345221828Sgrehan		const char	*name;
346221828Sgrehan		int		type;
347221828Sgrehan	} capstrmap[] = {
348221828Sgrehan		{ "hlt_exit",		VM_CAP_HALT_EXIT },
349221828Sgrehan		{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
350221828Sgrehan		{ "pause_exit",		VM_CAP_PAUSE_EXIT },
351221828Sgrehan		{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
352221828Sgrehan		{ 0 }
353221828Sgrehan	};
354221828Sgrehan
355221828Sgrehan	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
356221828Sgrehan		if (strcmp(capstrmap[i].name, capname) == 0)
357221828Sgrehan			return (capstrmap[i].type);
358221828Sgrehan	}
359221828Sgrehan
360221828Sgrehan	return (-1);
361221828Sgrehan}
362221828Sgrehan
363221828Sgrehanint
364221828Sgrehanvm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
365221828Sgrehan		  int *retval)
366221828Sgrehan{
367221828Sgrehan	int error;
368221828Sgrehan	struct vm_capability vmcap;
369221828Sgrehan
370221828Sgrehan	bzero(&vmcap, sizeof(vmcap));
371221828Sgrehan	vmcap.cpuid = vcpu;
372221828Sgrehan	vmcap.captype = cap;
373221828Sgrehan
374221828Sgrehan	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
375221828Sgrehan	*retval = vmcap.capval;
376221828Sgrehan	return (error);
377221828Sgrehan}
378221828Sgrehan
379221828Sgrehanint
380221828Sgrehanvm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
381221828Sgrehan{
382221828Sgrehan	struct vm_capability vmcap;
383221828Sgrehan
384221828Sgrehan	bzero(&vmcap, sizeof(vmcap));
385221828Sgrehan	vmcap.cpuid = vcpu;
386221828Sgrehan	vmcap.captype = cap;
387221828Sgrehan	vmcap.capval = val;
388221828Sgrehan
389221828Sgrehan	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
390221828Sgrehan}
391221828Sgrehan
392221828Sgrehanint
393221828Sgrehanvm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
394221828Sgrehan{
395221828Sgrehan	struct vm_pptdev pptdev;
396221828Sgrehan
397221828Sgrehan	bzero(&pptdev, sizeof(pptdev));
398221828Sgrehan	pptdev.bus = bus;
399221828Sgrehan	pptdev.slot = slot;
400221828Sgrehan	pptdev.func = func;
401221828Sgrehan
402221828Sgrehan	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
403221828Sgrehan}
404221828Sgrehan
405221828Sgrehanint
406221828Sgrehanvm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
407221828Sgrehan{
408221828Sgrehan	struct vm_pptdev pptdev;
409221828Sgrehan
410221828Sgrehan	bzero(&pptdev, sizeof(pptdev));
411221828Sgrehan	pptdev.bus = bus;
412221828Sgrehan	pptdev.slot = slot;
413221828Sgrehan	pptdev.func = func;
414221828Sgrehan
415221828Sgrehan	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
416221828Sgrehan}
417221828Sgrehan
418221828Sgrehanint
419221828Sgrehanvm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
420221828Sgrehan		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
421221828Sgrehan{
422221828Sgrehan	struct vm_pptdev_mmio pptmmio;
423221828Sgrehan
424221828Sgrehan	bzero(&pptmmio, sizeof(pptmmio));
425221828Sgrehan	pptmmio.bus = bus;
426221828Sgrehan	pptmmio.slot = slot;
427221828Sgrehan	pptmmio.func = func;
428221828Sgrehan	pptmmio.gpa = gpa;
429221828Sgrehan	pptmmio.len = len;
430221828Sgrehan	pptmmio.hpa = hpa;
431221828Sgrehan
432221828Sgrehan	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
433221828Sgrehan}
434221828Sgrehan
435221828Sgrehanint
436221828Sgrehanvm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
437221828Sgrehan	     int destcpu, int vector, int numvec)
438221828Sgrehan{
439221828Sgrehan	struct vm_pptdev_msi pptmsi;
440221828Sgrehan
441221828Sgrehan	bzero(&pptmsi, sizeof(pptmsi));
442221828Sgrehan	pptmsi.vcpu = vcpu;
443221828Sgrehan	pptmsi.bus = bus;
444221828Sgrehan	pptmsi.slot = slot;
445221828Sgrehan	pptmsi.func = func;
446221828Sgrehan	pptmsi.destcpu = destcpu;
447221828Sgrehan	pptmsi.vector = vector;
448221828Sgrehan	pptmsi.numvec = numvec;
449221828Sgrehan
450221828Sgrehan	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
451221828Sgrehan}
452221828Sgrehan
453234761Sgrehanint
454234761Sgrehanvm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
455234761Sgrehan	      int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
456234761Sgrehan{
457234761Sgrehan	struct vm_pptdev_msix pptmsix;
458234761Sgrehan
459234761Sgrehan	bzero(&pptmsix, sizeof(pptmsix));
460234761Sgrehan	pptmsix.vcpu = vcpu;
461234761Sgrehan	pptmsix.bus = bus;
462234761Sgrehan	pptmsix.slot = slot;
463234761Sgrehan	pptmsix.func = func;
464234761Sgrehan	pptmsix.idx = idx;
465234761Sgrehan	pptmsix.msg = msg;
466234761Sgrehan	pptmsix.addr = addr;
467234761Sgrehan	pptmsix.vector_control = vector_control;
468234761Sgrehan
469234761Sgrehan	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
470234761Sgrehan}
471234761Sgrehan
472221828Sgrehanuint64_t *
473221828Sgrehanvm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
474221828Sgrehan	     int *ret_entries)
475221828Sgrehan{
476221828Sgrehan	int error;
477221828Sgrehan
478221828Sgrehan	static struct vm_stats vmstats;
479221828Sgrehan
480221828Sgrehan	vmstats.cpuid = vcpu;
481221828Sgrehan
482221828Sgrehan	error = ioctl(ctx->fd, VM_STATS, &vmstats);
483221828Sgrehan	if (error == 0) {
484221828Sgrehan		if (ret_entries)
485221828Sgrehan			*ret_entries = vmstats.num_entries;
486221828Sgrehan		if (ret_tv)
487221828Sgrehan			*ret_tv = vmstats.tv;
488221828Sgrehan		return (vmstats.statbuf);
489221828Sgrehan	} else
490221828Sgrehan		return (NULL);
491221828Sgrehan}
492221828Sgrehan
493221828Sgrehanconst char *
494221828Sgrehanvm_get_stat_desc(struct vmctx *ctx, int index)
495221828Sgrehan{
496221828Sgrehan	static struct vm_stat_desc statdesc;
497221828Sgrehan
498221828Sgrehan	statdesc.index = index;
499221828Sgrehan	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
500221828Sgrehan		return (statdesc.desc);
501221828Sgrehan	else
502221828Sgrehan		return (NULL);
503221828Sgrehan}
504221828Sgrehan
505221828Sgrehan/*
506221828Sgrehan * From Intel Vol 3a:
507221828Sgrehan * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
508221828Sgrehan */
509221828Sgrehanint
510221828Sgrehanvcpu_reset(struct vmctx *vmctx, int vcpu)
511221828Sgrehan{
512221828Sgrehan	int error;
513221828Sgrehan	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
514221828Sgrehan	uint32_t desc_access, desc_limit;
515221828Sgrehan	uint16_t sel;
516221828Sgrehan
517221828Sgrehan	zero = 0;
518221828Sgrehan
519221828Sgrehan	rflags = 0x2;
520221828Sgrehan	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
521221828Sgrehan	if (error)
522221828Sgrehan		goto done;
523221828Sgrehan
524221828Sgrehan	rip = 0xfff0;
525221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
526221828Sgrehan		goto done;
527221828Sgrehan
528221828Sgrehan	cr0 = CR0_NE;
529221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
530221828Sgrehan		goto done;
531221828Sgrehan
532221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
533221828Sgrehan		goto done;
534221828Sgrehan
535239025Sneel	cr4 = 0;
536221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
537221828Sgrehan		goto done;
538221828Sgrehan
539221828Sgrehan	/*
540221828Sgrehan	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
541221828Sgrehan	 */
542221828Sgrehan	desc_base = 0xffff0000;
543221828Sgrehan	desc_limit = 0xffff;
544221828Sgrehan	desc_access = 0x0093;
545221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
546221828Sgrehan			    desc_base, desc_limit, desc_access);
547221828Sgrehan	if (error)
548221828Sgrehan		goto done;
549221828Sgrehan
550221828Sgrehan	sel = 0xf000;
551221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
552221828Sgrehan		goto done;
553221828Sgrehan
554221828Sgrehan	/*
555221828Sgrehan	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
556221828Sgrehan	 */
557221828Sgrehan	desc_base = 0;
558221828Sgrehan	desc_limit = 0xffff;
559221828Sgrehan	desc_access = 0x0093;
560221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
561221828Sgrehan			    desc_base, desc_limit, desc_access);
562221828Sgrehan	if (error)
563221828Sgrehan		goto done;
564221828Sgrehan
565221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
566221828Sgrehan			    desc_base, desc_limit, desc_access);
567221828Sgrehan	if (error)
568221828Sgrehan		goto done;
569221828Sgrehan
570221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
571221828Sgrehan			    desc_base, desc_limit, desc_access);
572221828Sgrehan	if (error)
573221828Sgrehan		goto done;
574221828Sgrehan
575221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
576221828Sgrehan			    desc_base, desc_limit, desc_access);
577221828Sgrehan	if (error)
578221828Sgrehan		goto done;
579221828Sgrehan
580221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
581221828Sgrehan			    desc_base, desc_limit, desc_access);
582221828Sgrehan	if (error)
583221828Sgrehan		goto done;
584221828Sgrehan
585221828Sgrehan	sel = 0;
586221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
587221828Sgrehan		goto done;
588221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
589221828Sgrehan		goto done;
590221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
591221828Sgrehan		goto done;
592221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
593221828Sgrehan		goto done;
594221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
595221828Sgrehan		goto done;
596221828Sgrehan
597221828Sgrehan	/* General purpose registers */
598221828Sgrehan	rdx = 0xf00;
599221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
600221828Sgrehan		goto done;
601221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
602221828Sgrehan		goto done;
603221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
604221828Sgrehan		goto done;
605221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
606221828Sgrehan		goto done;
607221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
608221828Sgrehan		goto done;
609221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
610221828Sgrehan		goto done;
611221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
612221828Sgrehan		goto done;
613221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
614221828Sgrehan		goto done;
615221828Sgrehan
616221828Sgrehan	/* GDTR, IDTR */
617221828Sgrehan	desc_base = 0;
618221828Sgrehan	desc_limit = 0xffff;
619221828Sgrehan	desc_access = 0;
620221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
621221828Sgrehan			    desc_base, desc_limit, desc_access);
622221828Sgrehan	if (error != 0)
623221828Sgrehan		goto done;
624221828Sgrehan
625221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
626221828Sgrehan			    desc_base, desc_limit, desc_access);
627221828Sgrehan	if (error != 0)
628221828Sgrehan		goto done;
629221828Sgrehan
630221828Sgrehan	/* TR */
631221828Sgrehan	desc_base = 0;
632221828Sgrehan	desc_limit = 0xffff;
633221828Sgrehan	desc_access = 0x0000008b;
634221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
635221828Sgrehan	if (error)
636221828Sgrehan		goto done;
637221828Sgrehan
638221828Sgrehan	sel = 0;
639221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
640221828Sgrehan		goto done;
641221828Sgrehan
642221828Sgrehan	/* LDTR */
643221828Sgrehan	desc_base = 0;
644221828Sgrehan	desc_limit = 0xffff;
645221828Sgrehan	desc_access = 0x00000082;
646221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
647221828Sgrehan			    desc_limit, desc_access);
648221828Sgrehan	if (error)
649221828Sgrehan		goto done;
650221828Sgrehan
651221828Sgrehan	sel = 0;
652221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
653221828Sgrehan		goto done;
654221828Sgrehan
655221828Sgrehan	/* XXX cr2, debug registers */
656221828Sgrehan
657221828Sgrehan	error = 0;
658221828Sgrehandone:
659221828Sgrehan	return (error);
660221828Sgrehan}
661