vmmapi.c revision 241178
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	if (vm->fd >= 0)
115221828Sgrehan		close(vm->fd);
116241178Sneel	DESTROY(vm->name);
117241178Sneel
118221828Sgrehan	free(vm);
119221828Sgrehan}
120221828Sgrehan
121239700Sgrehansize_t
122239700Sgrehanvmm_get_mem_total(void)
123239700Sgrehan{
124239700Sgrehan	size_t mem_total = 0;
125239700Sgrehan	size_t oldlen = sizeof(mem_total);
126239700Sgrehan	int error;
127239700Sgrehan	error = sysctlbyname("hw.vmm.mem_total", &mem_total, &oldlen, NULL, 0);
128239700Sgrehan	if (error)
129239700Sgrehan		return -1;
130239700Sgrehan	return mem_total;
131239700Sgrehan}
132239700Sgrehan
133239700Sgrehansize_t
134239700Sgrehanvmm_get_mem_free(void)
135239700Sgrehan{
136239700Sgrehan	size_t mem_free = 0;
137239700Sgrehan	size_t oldlen = sizeof(mem_free);
138239700Sgrehan	int error;
139239700Sgrehan	error = sysctlbyname("hw.vmm.mem_free", &mem_free, &oldlen, NULL, 0);
140239700Sgrehan	if (error)
141239700Sgrehan		return -1;
142239700Sgrehan	return mem_free;
143239700Sgrehan}
144239700Sgrehan
145221828Sgrehanint
146221828Sgrehanvm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa,
147221828Sgrehan		  vm_paddr_t *ret_hpa, size_t *ret_len)
148221828Sgrehan{
149221828Sgrehan	int error;
150221828Sgrehan	struct vm_memory_segment seg;
151221828Sgrehan
152221828Sgrehan	bzero(&seg, sizeof(seg));
153221828Sgrehan	seg.gpa = gpa;
154221828Sgrehan	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
155221828Sgrehan	*ret_len = seg.len;
156221828Sgrehan	return (error);
157221828Sgrehan}
158221828Sgrehan
159221828Sgrehanint
160221828Sgrehanvm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **mapaddr)
161221828Sgrehan{
162221828Sgrehan	int error;
163221828Sgrehan	struct vm_memory_segment seg;
164221828Sgrehan
165221828Sgrehan	/*
166221828Sgrehan	 * Create and optionally map 'len' bytes of memory at guest
167221828Sgrehan	 * physical address 'gpa'
168221828Sgrehan	 */
169221828Sgrehan	bzero(&seg, sizeof(seg));
170221828Sgrehan	seg.gpa = gpa;
171221828Sgrehan	seg.len = len;
172221828Sgrehan	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
173221828Sgrehan	if (error == 0 && mapaddr != NULL) {
174221828Sgrehan		*mapaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
175221828Sgrehan				ctx->fd, gpa);
176221828Sgrehan	}
177221828Sgrehan	return (error);
178221828Sgrehan}
179221828Sgrehan
180221828Sgrehanchar *
181221828Sgrehanvm_map_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len)
182221828Sgrehan{
183221828Sgrehan
184221828Sgrehan	/* Map 'len' bytes of memory at guest physical address 'gpa' */
185221828Sgrehan	return ((char *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
186221828Sgrehan		     ctx->fd, gpa));
187221828Sgrehan}
188221828Sgrehan
189221828Sgrehanint
190221828Sgrehanvm_set_desc(struct vmctx *ctx, int vcpu, int reg,
191221828Sgrehan	    uint64_t base, uint32_t limit, uint32_t access)
192221828Sgrehan{
193221828Sgrehan	int error;
194221828Sgrehan	struct vm_seg_desc vmsegdesc;
195221828Sgrehan
196221828Sgrehan	bzero(&vmsegdesc, sizeof(vmsegdesc));
197221828Sgrehan	vmsegdesc.cpuid = vcpu;
198221828Sgrehan	vmsegdesc.regnum = reg;
199221828Sgrehan	vmsegdesc.desc.base = base;
200221828Sgrehan	vmsegdesc.desc.limit = limit;
201221828Sgrehan	vmsegdesc.desc.access = access;
202221828Sgrehan
203221828Sgrehan	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
204221828Sgrehan	return (error);
205221828Sgrehan}
206221828Sgrehan
207221828Sgrehanint
208221828Sgrehanvm_get_desc(struct vmctx *ctx, int vcpu, int reg,
209221828Sgrehan	    uint64_t *base, uint32_t *limit, uint32_t *access)
210221828Sgrehan{
211221828Sgrehan	int error;
212221828Sgrehan	struct vm_seg_desc vmsegdesc;
213221828Sgrehan
214221828Sgrehan	bzero(&vmsegdesc, sizeof(vmsegdesc));
215221828Sgrehan	vmsegdesc.cpuid = vcpu;
216221828Sgrehan	vmsegdesc.regnum = reg;
217221828Sgrehan
218221828Sgrehan	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
219221828Sgrehan	if (error == 0) {
220221828Sgrehan		*base = vmsegdesc.desc.base;
221221828Sgrehan		*limit = vmsegdesc.desc.limit;
222221828Sgrehan		*access = vmsegdesc.desc.access;
223221828Sgrehan	}
224221828Sgrehan	return (error);
225221828Sgrehan}
226221828Sgrehan
227221828Sgrehanint
228221828Sgrehanvm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
229221828Sgrehan{
230221828Sgrehan	int error;
231221828Sgrehan	struct vm_register vmreg;
232221828Sgrehan
233221828Sgrehan	bzero(&vmreg, sizeof(vmreg));
234221828Sgrehan	vmreg.cpuid = vcpu;
235221828Sgrehan	vmreg.regnum = reg;
236221828Sgrehan	vmreg.regval = val;
237221828Sgrehan
238221828Sgrehan	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
239221828Sgrehan	return (error);
240221828Sgrehan}
241221828Sgrehan
242221828Sgrehanint
243221828Sgrehanvm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
244221828Sgrehan{
245221828Sgrehan	int error;
246221828Sgrehan	struct vm_register vmreg;
247221828Sgrehan
248221828Sgrehan	bzero(&vmreg, sizeof(vmreg));
249221828Sgrehan	vmreg.cpuid = vcpu;
250221828Sgrehan	vmreg.regnum = reg;
251221828Sgrehan
252221828Sgrehan	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
253221828Sgrehan	*ret_val = vmreg.regval;
254221828Sgrehan	return (error);
255221828Sgrehan}
256221828Sgrehan
257221828Sgrehanint
258221828Sgrehanvm_get_pinning(struct vmctx *ctx, int vcpu, int *host_cpuid)
259221828Sgrehan{
260221828Sgrehan	int error;
261221828Sgrehan	struct vm_pin vmpin;
262221828Sgrehan
263221828Sgrehan	bzero(&vmpin, sizeof(vmpin));
264221828Sgrehan	vmpin.vm_cpuid = vcpu;
265221828Sgrehan
266221828Sgrehan	error = ioctl(ctx->fd, VM_GET_PINNING, &vmpin);
267221828Sgrehan	*host_cpuid = vmpin.host_cpuid;
268221828Sgrehan	return (error);
269221828Sgrehan}
270221828Sgrehan
271221828Sgrehanint
272221828Sgrehanvm_set_pinning(struct vmctx *ctx, int vcpu, int host_cpuid)
273221828Sgrehan{
274221828Sgrehan	int error;
275221828Sgrehan	struct vm_pin vmpin;
276221828Sgrehan
277221828Sgrehan	bzero(&vmpin, sizeof(vmpin));
278221828Sgrehan	vmpin.vm_cpuid = vcpu;
279221828Sgrehan	vmpin.host_cpuid = host_cpuid;
280221828Sgrehan
281221828Sgrehan	error = ioctl(ctx->fd, VM_SET_PINNING, &vmpin);
282221828Sgrehan	return (error);
283221828Sgrehan}
284221828Sgrehan
285221828Sgrehanint
286221828Sgrehanvm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
287221828Sgrehan{
288221828Sgrehan	int error;
289221828Sgrehan	struct vm_run vmrun;
290221828Sgrehan
291221828Sgrehan	bzero(&vmrun, sizeof(vmrun));
292221828Sgrehan	vmrun.cpuid = vcpu;
293221828Sgrehan	vmrun.rip = rip;
294221828Sgrehan
295221828Sgrehan	error = ioctl(ctx->fd, VM_RUN, &vmrun);
296221828Sgrehan	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
297221828Sgrehan	return (error);
298221828Sgrehan}
299221828Sgrehan
300221828Sgrehanstatic int
301221828Sgrehanvm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
302221828Sgrehan		     int vector, int error_code, int error_code_valid)
303221828Sgrehan{
304221828Sgrehan	struct vm_event ev;
305221828Sgrehan
306221828Sgrehan	bzero(&ev, sizeof(ev));
307221828Sgrehan	ev.cpuid = vcpu;
308221828Sgrehan	ev.type = type;
309221828Sgrehan	ev.vector = vector;
310221828Sgrehan	ev.error_code = error_code;
311221828Sgrehan	ev.error_code_valid = error_code_valid;
312221828Sgrehan
313221828Sgrehan	return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
314221828Sgrehan}
315221828Sgrehan
316221828Sgrehanint
317221828Sgrehanvm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
318221828Sgrehan		int vector)
319221828Sgrehan{
320221828Sgrehan
321221828Sgrehan	return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
322221828Sgrehan}
323221828Sgrehan
324221828Sgrehanint
325221828Sgrehanvm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
326221828Sgrehan		 int vector, int error_code)
327221828Sgrehan{
328221828Sgrehan
329221828Sgrehan	return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
330221828Sgrehan}
331221828Sgrehan
332221828Sgrehanint
333239042Sneelvm_build_tables(struct vmctx *ctxt, int ncpu, int ioapic,
334239042Sneel		void *oemtbl, int oemtblsz)
335221828Sgrehan{
336221828Sgrehan
337221828Sgrehan	return (vm_build_mptable(ctxt, BIOS_ROM_BASE, BIOS_ROM_SIZE, ncpu,
338239042Sneel				 ioapic, oemtbl, oemtblsz));
339221828Sgrehan}
340221828Sgrehan
341221828Sgrehanint
342239026Sneelvm_apicid2vcpu(struct vmctx *ctx, int apicid)
343239026Sneel{
344239026Sneel	/*
345239026Sneel	 * The apic id associated with the 'vcpu' has the same numerical value
346239026Sneel	 * as the 'vcpu' itself.
347239026Sneel	 */
348239026Sneel	return (apicid);
349239026Sneel}
350239026Sneel
351239026Sneelint
352221828Sgrehanvm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
353221828Sgrehan{
354221828Sgrehan	struct vm_lapic_irq vmirq;
355221828Sgrehan
356221828Sgrehan	bzero(&vmirq, sizeof(vmirq));
357221828Sgrehan	vmirq.cpuid = vcpu;
358221828Sgrehan	vmirq.vector = vector;
359221828Sgrehan
360221828Sgrehan	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
361221828Sgrehan}
362221828Sgrehan
363221828Sgrehanint
364221828Sgrehanvm_inject_nmi(struct vmctx *ctx, int vcpu)
365221828Sgrehan{
366221828Sgrehan	struct vm_nmi vmnmi;
367221828Sgrehan
368221828Sgrehan	bzero(&vmnmi, sizeof(vmnmi));
369221828Sgrehan	vmnmi.cpuid = vcpu;
370221828Sgrehan
371221828Sgrehan	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
372221828Sgrehan}
373221828Sgrehan
374221828Sgrehanint
375221828Sgrehanvm_capability_name2type(const char *capname)
376221828Sgrehan{
377221828Sgrehan	int i;
378221828Sgrehan
379221828Sgrehan	static struct {
380221828Sgrehan		const char	*name;
381221828Sgrehan		int		type;
382221828Sgrehan	} capstrmap[] = {
383221828Sgrehan		{ "hlt_exit",		VM_CAP_HALT_EXIT },
384221828Sgrehan		{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
385221828Sgrehan		{ "pause_exit",		VM_CAP_PAUSE_EXIT },
386221828Sgrehan		{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
387221828Sgrehan		{ 0 }
388221828Sgrehan	};
389221828Sgrehan
390221828Sgrehan	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
391221828Sgrehan		if (strcmp(capstrmap[i].name, capname) == 0)
392221828Sgrehan			return (capstrmap[i].type);
393221828Sgrehan	}
394221828Sgrehan
395221828Sgrehan	return (-1);
396221828Sgrehan}
397221828Sgrehan
398221828Sgrehanint
399221828Sgrehanvm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
400221828Sgrehan		  int *retval)
401221828Sgrehan{
402221828Sgrehan	int error;
403221828Sgrehan	struct vm_capability vmcap;
404221828Sgrehan
405221828Sgrehan	bzero(&vmcap, sizeof(vmcap));
406221828Sgrehan	vmcap.cpuid = vcpu;
407221828Sgrehan	vmcap.captype = cap;
408221828Sgrehan
409221828Sgrehan	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
410221828Sgrehan	*retval = vmcap.capval;
411221828Sgrehan	return (error);
412221828Sgrehan}
413221828Sgrehan
414221828Sgrehanint
415221828Sgrehanvm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
416221828Sgrehan{
417221828Sgrehan	struct vm_capability vmcap;
418221828Sgrehan
419221828Sgrehan	bzero(&vmcap, sizeof(vmcap));
420221828Sgrehan	vmcap.cpuid = vcpu;
421221828Sgrehan	vmcap.captype = cap;
422221828Sgrehan	vmcap.capval = val;
423221828Sgrehan
424221828Sgrehan	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
425221828Sgrehan}
426221828Sgrehan
427221828Sgrehanint
428221828Sgrehanvm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
429221828Sgrehan{
430221828Sgrehan	struct vm_pptdev pptdev;
431221828Sgrehan
432221828Sgrehan	bzero(&pptdev, sizeof(pptdev));
433221828Sgrehan	pptdev.bus = bus;
434221828Sgrehan	pptdev.slot = slot;
435221828Sgrehan	pptdev.func = func;
436221828Sgrehan
437221828Sgrehan	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
438221828Sgrehan}
439221828Sgrehan
440221828Sgrehanint
441221828Sgrehanvm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
442221828Sgrehan{
443221828Sgrehan	struct vm_pptdev pptdev;
444221828Sgrehan
445221828Sgrehan	bzero(&pptdev, sizeof(pptdev));
446221828Sgrehan	pptdev.bus = bus;
447221828Sgrehan	pptdev.slot = slot;
448221828Sgrehan	pptdev.func = func;
449221828Sgrehan
450221828Sgrehan	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
451221828Sgrehan}
452221828Sgrehan
453221828Sgrehanint
454221828Sgrehanvm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
455221828Sgrehan		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
456221828Sgrehan{
457221828Sgrehan	struct vm_pptdev_mmio pptmmio;
458221828Sgrehan
459221828Sgrehan	bzero(&pptmmio, sizeof(pptmmio));
460221828Sgrehan	pptmmio.bus = bus;
461221828Sgrehan	pptmmio.slot = slot;
462221828Sgrehan	pptmmio.func = func;
463221828Sgrehan	pptmmio.gpa = gpa;
464221828Sgrehan	pptmmio.len = len;
465221828Sgrehan	pptmmio.hpa = hpa;
466221828Sgrehan
467221828Sgrehan	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
468221828Sgrehan}
469221828Sgrehan
470221828Sgrehanint
471221828Sgrehanvm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
472221828Sgrehan	     int destcpu, int vector, int numvec)
473221828Sgrehan{
474221828Sgrehan	struct vm_pptdev_msi pptmsi;
475221828Sgrehan
476221828Sgrehan	bzero(&pptmsi, sizeof(pptmsi));
477221828Sgrehan	pptmsi.vcpu = vcpu;
478221828Sgrehan	pptmsi.bus = bus;
479221828Sgrehan	pptmsi.slot = slot;
480221828Sgrehan	pptmsi.func = func;
481221828Sgrehan	pptmsi.destcpu = destcpu;
482221828Sgrehan	pptmsi.vector = vector;
483221828Sgrehan	pptmsi.numvec = numvec;
484221828Sgrehan
485221828Sgrehan	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
486221828Sgrehan}
487221828Sgrehan
488234761Sgrehanint
489234761Sgrehanvm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
490234761Sgrehan	      int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
491234761Sgrehan{
492234761Sgrehan	struct vm_pptdev_msix pptmsix;
493234761Sgrehan
494234761Sgrehan	bzero(&pptmsix, sizeof(pptmsix));
495234761Sgrehan	pptmsix.vcpu = vcpu;
496234761Sgrehan	pptmsix.bus = bus;
497234761Sgrehan	pptmsix.slot = slot;
498234761Sgrehan	pptmsix.func = func;
499234761Sgrehan	pptmsix.idx = idx;
500234761Sgrehan	pptmsix.msg = msg;
501234761Sgrehan	pptmsix.addr = addr;
502234761Sgrehan	pptmsix.vector_control = vector_control;
503234761Sgrehan
504234761Sgrehan	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
505234761Sgrehan}
506234761Sgrehan
507221828Sgrehanuint64_t *
508221828Sgrehanvm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
509221828Sgrehan	     int *ret_entries)
510221828Sgrehan{
511221828Sgrehan	int error;
512221828Sgrehan
513221828Sgrehan	static struct vm_stats vmstats;
514221828Sgrehan
515221828Sgrehan	vmstats.cpuid = vcpu;
516221828Sgrehan
517221828Sgrehan	error = ioctl(ctx->fd, VM_STATS, &vmstats);
518221828Sgrehan	if (error == 0) {
519221828Sgrehan		if (ret_entries)
520221828Sgrehan			*ret_entries = vmstats.num_entries;
521221828Sgrehan		if (ret_tv)
522221828Sgrehan			*ret_tv = vmstats.tv;
523221828Sgrehan		return (vmstats.statbuf);
524221828Sgrehan	} else
525221828Sgrehan		return (NULL);
526221828Sgrehan}
527221828Sgrehan
528221828Sgrehanconst char *
529221828Sgrehanvm_get_stat_desc(struct vmctx *ctx, int index)
530221828Sgrehan{
531221828Sgrehan	static struct vm_stat_desc statdesc;
532221828Sgrehan
533221828Sgrehan	statdesc.index = index;
534221828Sgrehan	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
535221828Sgrehan		return (statdesc.desc);
536221828Sgrehan	else
537221828Sgrehan		return (NULL);
538221828Sgrehan}
539221828Sgrehan
540240922Sneelint
541240922Sneelvm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
542240922Sneel{
543240922Sneel	int error;
544240922Sneel	struct vm_x2apic x2apic;
545240922Sneel
546240922Sneel	bzero(&x2apic, sizeof(x2apic));
547240922Sneel	x2apic.cpuid = vcpu;
548240922Sneel
549240922Sneel	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
550240922Sneel	*state = x2apic.state;
551240922Sneel	return (error);
552240922Sneel}
553240922Sneel
554240922Sneelint
555240922Sneelvm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
556240922Sneel{
557240922Sneel	int error;
558240922Sneel	struct vm_x2apic x2apic;
559240922Sneel
560240922Sneel	bzero(&x2apic, sizeof(x2apic));
561240922Sneel	x2apic.cpuid = vcpu;
562240922Sneel	x2apic.state = state;
563240922Sneel
564240922Sneel	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
565240922Sneel
566240922Sneel	return (error);
567240922Sneel}
568240922Sneel
569221828Sgrehan/*
570221828Sgrehan * From Intel Vol 3a:
571221828Sgrehan * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
572221828Sgrehan */
573221828Sgrehanint
574221828Sgrehanvcpu_reset(struct vmctx *vmctx, int vcpu)
575221828Sgrehan{
576221828Sgrehan	int error;
577221828Sgrehan	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
578221828Sgrehan	uint32_t desc_access, desc_limit;
579221828Sgrehan	uint16_t sel;
580221828Sgrehan
581221828Sgrehan	zero = 0;
582221828Sgrehan
583221828Sgrehan	rflags = 0x2;
584221828Sgrehan	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
585221828Sgrehan	if (error)
586221828Sgrehan		goto done;
587221828Sgrehan
588221828Sgrehan	rip = 0xfff0;
589221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
590221828Sgrehan		goto done;
591221828Sgrehan
592221828Sgrehan	cr0 = CR0_NE;
593221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
594221828Sgrehan		goto done;
595221828Sgrehan
596221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
597221828Sgrehan		goto done;
598221828Sgrehan
599239025Sneel	cr4 = 0;
600221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
601221828Sgrehan		goto done;
602221828Sgrehan
603221828Sgrehan	/*
604221828Sgrehan	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
605221828Sgrehan	 */
606221828Sgrehan	desc_base = 0xffff0000;
607221828Sgrehan	desc_limit = 0xffff;
608221828Sgrehan	desc_access = 0x0093;
609221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
610221828Sgrehan			    desc_base, desc_limit, desc_access);
611221828Sgrehan	if (error)
612221828Sgrehan		goto done;
613221828Sgrehan
614221828Sgrehan	sel = 0xf000;
615221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
616221828Sgrehan		goto done;
617221828Sgrehan
618221828Sgrehan	/*
619221828Sgrehan	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
620221828Sgrehan	 */
621221828Sgrehan	desc_base = 0;
622221828Sgrehan	desc_limit = 0xffff;
623221828Sgrehan	desc_access = 0x0093;
624221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
625221828Sgrehan			    desc_base, desc_limit, desc_access);
626221828Sgrehan	if (error)
627221828Sgrehan		goto done;
628221828Sgrehan
629221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
630221828Sgrehan			    desc_base, desc_limit, desc_access);
631221828Sgrehan	if (error)
632221828Sgrehan		goto done;
633221828Sgrehan
634221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
635221828Sgrehan			    desc_base, desc_limit, desc_access);
636221828Sgrehan	if (error)
637221828Sgrehan		goto done;
638221828Sgrehan
639221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
640221828Sgrehan			    desc_base, desc_limit, desc_access);
641221828Sgrehan	if (error)
642221828Sgrehan		goto done;
643221828Sgrehan
644221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
645221828Sgrehan			    desc_base, desc_limit, desc_access);
646221828Sgrehan	if (error)
647221828Sgrehan		goto done;
648221828Sgrehan
649221828Sgrehan	sel = 0;
650221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
651221828Sgrehan		goto done;
652221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
653221828Sgrehan		goto done;
654221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
655221828Sgrehan		goto done;
656221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
657221828Sgrehan		goto done;
658221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
659221828Sgrehan		goto done;
660221828Sgrehan
661221828Sgrehan	/* General purpose registers */
662221828Sgrehan	rdx = 0xf00;
663221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
664221828Sgrehan		goto done;
665221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
666221828Sgrehan		goto done;
667221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
668221828Sgrehan		goto done;
669221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
670221828Sgrehan		goto done;
671221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
672221828Sgrehan		goto done;
673221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
674221828Sgrehan		goto done;
675221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
676221828Sgrehan		goto done;
677221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
678221828Sgrehan		goto done;
679221828Sgrehan
680221828Sgrehan	/* GDTR, IDTR */
681221828Sgrehan	desc_base = 0;
682221828Sgrehan	desc_limit = 0xffff;
683221828Sgrehan	desc_access = 0;
684221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
685221828Sgrehan			    desc_base, desc_limit, desc_access);
686221828Sgrehan	if (error != 0)
687221828Sgrehan		goto done;
688221828Sgrehan
689221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
690221828Sgrehan			    desc_base, desc_limit, desc_access);
691221828Sgrehan	if (error != 0)
692221828Sgrehan		goto done;
693221828Sgrehan
694221828Sgrehan	/* TR */
695221828Sgrehan	desc_base = 0;
696221828Sgrehan	desc_limit = 0xffff;
697221828Sgrehan	desc_access = 0x0000008b;
698221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
699221828Sgrehan	if (error)
700221828Sgrehan		goto done;
701221828Sgrehan
702221828Sgrehan	sel = 0;
703221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
704221828Sgrehan		goto done;
705221828Sgrehan
706221828Sgrehan	/* LDTR */
707221828Sgrehan	desc_base = 0;
708221828Sgrehan	desc_limit = 0xffff;
709221828Sgrehan	desc_access = 0x00000082;
710221828Sgrehan	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
711221828Sgrehan			    desc_limit, desc_access);
712221828Sgrehan	if (error)
713221828Sgrehan		goto done;
714221828Sgrehan
715221828Sgrehan	sel = 0;
716221828Sgrehan	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
717221828Sgrehan		goto done;
718221828Sgrehan
719221828Sgrehan	/* XXX cr2, debug registers */
720221828Sgrehan
721221828Sgrehan	error = 0;
722221828Sgrehandone:
723221828Sgrehan	return (error);
724221828Sgrehan}
725