vmmapi.c revision 268889
1133808Spjd/*-
2156878Spjd * Copyright (c) 2011 NetApp, Inc.
3133808Spjd * All rights reserved.
4133808Spjd *
5133808Spjd * Redistribution and use in source and binary forms, with or without
6133808Spjd * modification, are permitted provided that the following conditions
7133808Spjd * are met:
8133808Spjd * 1. Redistributions of source code must retain the above copyright
9133808Spjd *    notice, this list of conditions and the following disclaimer.
10133808Spjd * 2. Redistributions in binary form must reproduce the above copyright
11133808Spjd *    notice, this list of conditions and the following disclaimer in the
12133808Spjd *    documentation and/or other materials provided with the distribution.
13155174Spjd *
14133808Spjd * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15133808Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16133808Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17133808Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18133808Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19133808Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20133808Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21133808Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22133808Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23133808Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24133808Spjd * SUCH DAMAGE.
25133808Spjd *
26133808Spjd * $FreeBSD: head/lib/libvmmapi/vmmapi.c 268889 2014-07-19 20:59:08Z neel $
27133808Spjd */
28133808Spjd
29133808Spjd#include <sys/cdefs.h>
30133808Spjd__FBSDID("$FreeBSD: head/lib/libvmmapi/vmmapi.c 268889 2014-07-19 20:59:08Z neel $");
31133808Spjd
32133808Spjd#include <sys/param.h>
33133808Spjd#include <sys/sysctl.h>
34133808Spjd#include <sys/ioctl.h>
35133808Spjd#include <sys/mman.h>
36133808Spjd#include <sys/_iovec.h>
37133808Spjd#include <sys/cpuset.h>
38133808Spjd
39133808Spjd#include <machine/specialreg.h>
40133808Spjd#include <machine/param.h>
41133808Spjd
42133808Spjd#include <stdio.h>
43133808Spjd#include <stdlib.h>
44133808Spjd#include <assert.h>
45133808Spjd#include <string.h>
46133808Spjd#include <fcntl.h>
47133808Spjd#include <unistd.h>
48133808Spjd
49133808Spjd#include <libutil.h>
50133808Spjd
51133808Spjd#include <machine/vmm.h>
52133808Spjd#include <machine/vmm_dev.h>
53133808Spjd
54156612Spjd#include "vmmapi.h"
55133808Spjd
56133808Spjd#define	MB	(1024 * 1024UL)
57133808Spjd#define	GB	(1024 * 1024 * 1024UL)
58133808Spjd
59133808Spjdstruct vmctx {
60133808Spjd	int	fd;
61133808Spjd	uint32_t lowmem_limit;
62133808Spjd	enum vm_mmap_style vms;
63156612Spjd	int	memflags;
64156612Spjd	size_t	lowmem;
65133808Spjd	char	*lowmem_addr;
66133808Spjd	size_t	highmem;
67133808Spjd	char	*highmem_addr;
68156612Spjd	char	*name;
69133808Spjd};
70133808Spjd
71133808Spjd#define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
72133808Spjd#define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
73133808Spjd
74133808Spjdstatic int
75133808Spjdvm_device_open(const char *name)
76133808Spjd{
77133808Spjd        int fd, len;
78156612Spjd        char *vmfile;
79133808Spjd
80133808Spjd	len = strlen("/dev/vmm/") + strlen(name) + 1;
81133808Spjd	vmfile = malloc(len);
82133808Spjd	assert(vmfile != NULL);
83133808Spjd	snprintf(vmfile, len, "/dev/vmm/%s", name);
84133808Spjd
85133808Spjd        /* Open the device file */
86133808Spjd        fd = open(vmfile, O_RDWR, 0);
87133808Spjd
88133808Spjd	free(vmfile);
89133808Spjd        return (fd);
90133808Spjd}
91133808Spjd
92133808Spjdint
93133808Spjdvm_create(const char *name)
94133808Spjd{
95133808Spjd
96133808Spjd	return (CREATE((char *)name));
97133808Spjd}
98133808Spjd
99134124Spjdstruct vmctx *
100134168Spjdvm_open(const char *name)
101134168Spjd{
102134168Spjd	struct vmctx *vm;
103133808Spjd
104133808Spjd	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
105133808Spjd	assert(vm != NULL);
106133808Spjd
107144142Spjd	vm->fd = -1;
108144142Spjd	vm->memflags = 0;
109144142Spjd	vm->lowmem_limit = 3 * GB;
110144142Spjd	vm->name = (char *)(vm + 1);
111133808Spjd	strcpy(vm->name, name);
112133808Spjd
113133808Spjd	if ((vm->fd = vm_device_open(vm->name)) < 0)
114133808Spjd		goto err;
115133808Spjd
116133808Spjd	return (vm);
117133808Spjderr:
118133808Spjd	vm_destroy(vm);
119133808Spjd	return (NULL);
120133808Spjd}
121133808Spjd
122133808Spjdvoid
123133808Spjdvm_destroy(struct vmctx *vm)
124133808Spjd{
125133808Spjd	assert(vm != NULL);
126133808Spjd
127133808Spjd	if (vm->fd >= 0)
128133808Spjd		close(vm->fd);
129133808Spjd	DESTROY(vm->name);
130134124Spjd
131134124Spjd	free(vm);
132134124Spjd}
133134124Spjd
134134124Spjdint
135134124Spjdvm_parse_memsize(const char *optarg, size_t *ret_memsize)
136134124Spjd{
137134124Spjd	char *endptr;
138134124Spjd	size_t optval;
139134124Spjd	int error;
140134124Spjd
141134124Spjd	optval = strtoul(optarg, &endptr, 0);
142134124Spjd	if (*optarg != '\0' && *endptr == '\0') {
143134124Spjd		/*
144134124Spjd		 * For the sake of backward compatibility if the memory size
145134124Spjd		 * specified on the command line is less than a megabyte then
146134168Spjd		 * it is interpreted as being in units of MB.
147134168Spjd		 */
148134168Spjd		if (optval < MB)
149134168Spjd			optval *= MB;
150134168Spjd		*ret_memsize = optval;
151134168Spjd		error = 0;
152134168Spjd	} else
153134168Spjd		error = expand_number(optarg, ret_memsize);
154134168Spjd
155134168Spjd	return (error);
156134168Spjd}
157134168Spjd
158134168Spjdint
159134168Spjdvm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len,
160134168Spjd		  int *wired)
161134168Spjd{
162134168Spjd	int error;
163134124Spjd	struct vm_memory_segment seg;
164134124Spjd
165134124Spjd	bzero(&seg, sizeof(seg));
166156612Spjd	seg.gpa = gpa;
167156612Spjd	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
168156612Spjd	*ret_len = seg.len;
169156612Spjd	if (wired != NULL)
170156612Spjd		*wired = seg.wired;
171156612Spjd	return (error);
172156612Spjd}
173156612Spjd
174156612Spjduint32_t
175156612Spjdvm_get_lowmem_limit(struct vmctx *ctx)
176156612Spjd{
177156612Spjd
178156612Spjd	return (ctx->lowmem_limit);
179156612Spjd}
180156612Spjd
181133808Spjdvoid
182133808Spjdvm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
183133808Spjd{
184133808Spjd
185133808Spjd	ctx->lowmem_limit = limit;
186133808Spjd}
187133808Spjd
188133808Spjdvoid
189133808Spjdvm_set_memflags(struct vmctx *ctx, int flags)
190134168Spjd{
191134168Spjd
192134168Spjd	ctx->memflags = flags;
193134168Spjd}
194134168Spjd
195134168Spjdstatic int
196134168Spjdsetup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
197134124Spjd{
198134124Spjd	int error, mmap_flags;
199134124Spjd	struct vm_memory_segment seg;
200134124Spjd
201134124Spjd	/*
202134124Spjd	 * Create and optionally map 'len' bytes of memory at guest
203134124Spjd	 * physical address 'gpa'
204134168Spjd	 */
205134168Spjd	bzero(&seg, sizeof(seg));
206134168Spjd	seg.gpa = gpa;
207134168Spjd	seg.len = len;
208134168Spjd	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
209134168Spjd	if (error == 0 && addr != NULL) {
210134168Spjd		mmap_flags = MAP_SHARED;
211133808Spjd		if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
212133808Spjd			mmap_flags |= MAP_NOCORE;
213133808Spjd		*addr = mmap(NULL, len, PROT_READ | PROT_WRITE, mmap_flags,
214133808Spjd		    ctx->fd, gpa);
215133808Spjd	}
216133808Spjd	return (error);
217133808Spjd}
218133808Spjd
219133808Spjdint
220133808Spjdvm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
221133808Spjd{
222133808Spjd	char **addr;
223133808Spjd	int error;
224133808Spjd
225133808Spjd	/* XXX VM_MMAP_SPARSE not implemented yet */
226133808Spjd	assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
227133808Spjd	ctx->vms = vms;
228133808Spjd
229133808Spjd	/*
230156612Spjd	 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
231133808Spjd	 * create another 'highmem' segment above 4GB for the remainder.
232133808Spjd	 */
233133808Spjd	if (memsize > ctx->lowmem_limit) {
234133808Spjd		ctx->lowmem = ctx->lowmem_limit;
235133808Spjd		ctx->highmem = memsize - ctx->lowmem;
236139671Spjd	} else {
237133808Spjd		ctx->lowmem = memsize;
238133808Spjd		ctx->highmem = 0;
239139671Spjd	}
240133808Spjd
241139671Spjd	if (ctx->lowmem > 0) {
242133808Spjd		addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
243133808Spjd		error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
244133808Spjd		if (error)
245133808Spjd			return (error);
246133808Spjd	}
247133808Spjd
248133808Spjd	if (ctx->highmem > 0) {
249133808Spjd		addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
250133808Spjd		error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
251133808Spjd		if (error)
252133808Spjd			return (error);
253133808Spjd	}
254133808Spjd
255133808Spjd	return (0);
256133808Spjd}
257133808Spjd
258133808Spjdvoid *
259133808Spjdvm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
260133808Spjd{
261133808Spjd
262133808Spjd	/* XXX VM_MMAP_SPARSE not implemented yet */
263133808Spjd	assert(ctx->vms == VM_MMAP_ALL);
264133808Spjd
265156612Spjd	if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
266133808Spjd		return ((void *)(ctx->lowmem_addr + gaddr));
267133808Spjd
268133808Spjd	if (gaddr >= 4*GB) {
269133808Spjd		gaddr -= 4*GB;
270133808Spjd		if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
271156612Spjd			return ((void *)(ctx->highmem_addr + gaddr));
272133808Spjd	}
273133808Spjd
274133808Spjd	return (NULL);
275133808Spjd}
276133808Spjd
277156612Spjdsize_t
278133808Spjdvm_get_lowmem_size(struct vmctx *ctx)
279133808Spjd{
280133808Spjd
281133808Spjd	return (ctx->lowmem);
282133808Spjd}
283133808Spjd
284133808Spjdsize_t
285133808Spjdvm_get_highmem_size(struct vmctx *ctx)
286133808Spjd{
287133808Spjd
288139671Spjd	return (ctx->highmem);
289156612Spjd}
290139671Spjd
291156612Spjdint
292133808Spjdvm_set_desc(struct vmctx *ctx, int vcpu, int reg,
293133808Spjd	    uint64_t base, uint32_t limit, uint32_t access)
294139671Spjd{
295139671Spjd	int error;
296156612Spjd	struct vm_seg_desc vmsegdesc;
297139671Spjd
298139671Spjd	bzero(&vmsegdesc, sizeof(vmsegdesc));
299139671Spjd	vmsegdesc.cpuid = vcpu;
300156612Spjd	vmsegdesc.regnum = reg;
301139671Spjd	vmsegdesc.desc.base = base;
302156612Spjd	vmsegdesc.desc.limit = limit;
303133808Spjd	vmsegdesc.desc.access = access;
304133808Spjd
305133808Spjd	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
306133808Spjd	return (error);
307133808Spjd}
308133808Spjd
309133808Spjdint
310133808Spjdvm_get_desc(struct vmctx *ctx, int vcpu, int reg,
311133808Spjd	    uint64_t *base, uint32_t *limit, uint32_t *access)
312133808Spjd{
313133808Spjd	int error;
314133808Spjd	struct vm_seg_desc vmsegdesc;
315133808Spjd
316133808Spjd	bzero(&vmsegdesc, sizeof(vmsegdesc));
317133808Spjd	vmsegdesc.cpuid = vcpu;
318133808Spjd	vmsegdesc.regnum = reg;
319133808Spjd
320133808Spjd	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
321133808Spjd	if (error == 0) {
322133808Spjd		*base = vmsegdesc.desc.base;
323133808Spjd		*limit = vmsegdesc.desc.limit;
324133808Spjd		*access = vmsegdesc.desc.access;
325133808Spjd	}
326133808Spjd	return (error);
327133808Spjd}
328133808Spjd
329133808Spjdint
330133808Spjdvm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
331133808Spjd{
332133808Spjd	int error;
333133808Spjd	struct vm_register vmreg;
334133808Spjd
335133808Spjd	bzero(&vmreg, sizeof(vmreg));
336133808Spjd	vmreg.cpuid = vcpu;
337133808Spjd	vmreg.regnum = reg;
338133808Spjd	vmreg.regval = val;
339133808Spjd
340133808Spjd	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
341133808Spjd	return (error);
342133808Spjd}
343133808Spjd
344133808Spjdint
345156612Spjdvm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
346133808Spjd{
347133808Spjd	int error;
348156612Spjd	struct vm_register vmreg;
349133808Spjd
350133808Spjd	bzero(&vmreg, sizeof(vmreg));
351133808Spjd	vmreg.cpuid = vcpu;
352133808Spjd	vmreg.regnum = reg;
353133808Spjd
354133808Spjd	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
355133808Spjd	*ret_val = vmreg.regval;
356133808Spjd	return (error);
357133808Spjd}
358133808Spjd
359133808Spjdint
360133808Spjdvm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
361133808Spjd{
362133808Spjd	int error;
363133808Spjd	struct vm_run vmrun;
364133808Spjd
365133808Spjd	bzero(&vmrun, sizeof(vmrun));
366133808Spjd	vmrun.cpuid = vcpu;
367133808Spjd	vmrun.rip = rip;
368133808Spjd
369133808Spjd	error = ioctl(ctx->fd, VM_RUN, &vmrun);
370133808Spjd	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
371134420Spjd	return (error);
372133808Spjd}
373133808Spjd
374133808Spjdint
375133808Spjdvm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
376133808Spjd{
377133808Spjd	struct vm_suspend vmsuspend;
378133808Spjd
379133808Spjd	bzero(&vmsuspend, sizeof(vmsuspend));
380133808Spjd	vmsuspend.how = how;
381133808Spjd	return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
382133808Spjd}
383133808Spjd
384156612Spjdint
385156612Spjdvm_reinit(struct vmctx *ctx)
386156612Spjd{
387133808Spjd
388133808Spjd	return (ioctl(ctx->fd, VM_REINIT, 0));
389156612Spjd}
390156612Spjd
391156612Spjdstatic int
392133808Spjdvm_inject_exception_real(struct vmctx *ctx, int vcpu, int vector,
393133808Spjd    int error_code, int error_code_valid)
394133808Spjd{
395133808Spjd	struct vm_exception exc;
396133808Spjd
397133808Spjd	bzero(&exc, sizeof(exc));
398133808Spjd	exc.cpuid = vcpu;
399156612Spjd	exc.vector = vector;
400156612Spjd	exc.error_code = error_code;
401156612Spjd	exc.error_code_valid = error_code_valid;
402156612Spjd
403156612Spjd	return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
404156612Spjd}
405156612Spjd
406156612Spjdint
407156612Spjdvm_inject_exception(struct vmctx *ctx, int vcpu, int vector)
408156612Spjd{
409156612Spjd
410156612Spjd	return (vm_inject_exception_real(ctx, vcpu, vector, 0, 0));
411156612Spjd}
412156612Spjd
413156612Spjdint
414156612Spjdvm_inject_exception2(struct vmctx *ctx, int vcpu, int vector, int errcode)
415156612Spjd{
416156612Spjd
417156612Spjd	return (vm_inject_exception_real(ctx, vcpu, vector, errcode, 1));
418156612Spjd}
419156612Spjd
420156612Spjdint
421156612Spjdvm_apicid2vcpu(struct vmctx *ctx, int apicid)
422156612Spjd{
423156612Spjd	/*
424156612Spjd	 * The apic id associated with the 'vcpu' has the same numerical value
425156612Spjd	 * as the 'vcpu' itself.
426156612Spjd	 */
427156612Spjd	return (apicid);
428156612Spjd}
429156612Spjd
430156612Spjdint
431156612Spjdvm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
432133808Spjd{
433156612Spjd	struct vm_lapic_irq vmirq;
434133808Spjd
435156612Spjd	bzero(&vmirq, sizeof(vmirq));
436133808Spjd	vmirq.cpuid = vcpu;
437133808Spjd	vmirq.vector = vector;
438133808Spjd
439156612Spjd	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
440133808Spjd}
441156612Spjd
442133808Spjdint
443133808Spjdvm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
444156612Spjd{
445133808Spjd	struct vm_lapic_irq vmirq;
446133808Spjd
447133808Spjd	bzero(&vmirq, sizeof(vmirq));
448156612Spjd	vmirq.cpuid = vcpu;
449133808Spjd	vmirq.vector = vector;
450134420Spjd
451134420Spjd	return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
452156612Spjd}
453134420Spjd
454156612Spjdint
455134420Spjdvm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
456134420Spjd{
457134420Spjd	struct vm_lapic_msi vmmsi;
458134420Spjd
459134420Spjd	bzero(&vmmsi, sizeof(vmmsi));
460134420Spjd	vmmsi.addr = addr;
461134420Spjd	vmmsi.msg = msg;
462133808Spjd
463156612Spjd	return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
464133808Spjd}
465133808Spjd
466133808Spjdint
467133808Spjdvm_ioapic_assert_irq(struct vmctx *ctx, int irq)
468133808Spjd{
469133808Spjd	struct vm_ioapic_irq ioapic_irq;
470156527Spjd
471133808Spjd	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
472133808Spjd	ioapic_irq.irq = irq;
473133808Spjd
474133808Spjd	return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
475133808Spjd}
476133808Spjd
477133808Spjdint
478133808Spjdvm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
479156612Spjd{
480146118Spjd	struct vm_ioapic_irq ioapic_irq;
481146118Spjd
482146118Spjd	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
483146118Spjd	ioapic_irq.irq = irq;
484146118Spjd
485146117Spjd	return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
486156612Spjd}
487133808Spjd
488133808Spjdint
489133808Spjdvm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
490133808Spjd{
491133808Spjd	struct vm_ioapic_irq ioapic_irq;
492133808Spjd
493133808Spjd	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
494133808Spjd	ioapic_irq.irq = irq;
495133808Spjd
496133808Spjd	return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
497133808Spjd}
498133808Spjd
499133808Spjdint
500133808Spjdvm_ioapic_pincount(struct vmctx *ctx, int *pincount)
501133808Spjd{
502133808Spjd
503133808Spjd	return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
504133808Spjd}
505133808Spjd
506133808Spjdint
507156612Spjdvm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
508156612Spjd{
509156612Spjd	struct vm_isa_irq isa_irq;
510156612Spjd
511156612Spjd	bzero(&isa_irq, sizeof(struct vm_isa_irq));
512133808Spjd	isa_irq.atpic_irq = atpic_irq;
513133808Spjd	isa_irq.ioapic_irq = ioapic_irq;
514133808Spjd
515133808Spjd	return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
516133808Spjd}
517133808Spjd
518133808Spjdint
519133808Spjdvm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
520133808Spjd{
521133808Spjd	struct vm_isa_irq isa_irq;
522133808Spjd
523156612Spjd	bzero(&isa_irq, sizeof(struct vm_isa_irq));
524133808Spjd	isa_irq.atpic_irq = atpic_irq;
525133808Spjd	isa_irq.ioapic_irq = ioapic_irq;
526133808Spjd
527133808Spjd	return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
528133808Spjd}
529133808Spjd
530133808Spjdint
531133808Spjdvm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
532133808Spjd{
533133808Spjd	struct vm_isa_irq isa_irq;
534133808Spjd
535133808Spjd	bzero(&isa_irq, sizeof(struct vm_isa_irq));
536133808Spjd	isa_irq.atpic_irq = atpic_irq;
537133808Spjd	isa_irq.ioapic_irq = ioapic_irq;
538156612Spjd
539133808Spjd	return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
540133808Spjd}
541133808Spjd
542133808Spjdint
543133808Spjdvm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
544133808Spjd    enum vm_intr_trigger trigger)
545133808Spjd{
546139295Spjd	struct vm_isa_irq_trigger isa_irq_trigger;
547139295Spjd
548139295Spjd	bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
549156612Spjd	isa_irq_trigger.atpic_irq = atpic_irq;
550133808Spjd	isa_irq_trigger.trigger = trigger;
551133808Spjd
552133808Spjd	return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
553133808Spjd}
554133808Spjd
555133808Spjdint
556156612Spjdvm_inject_nmi(struct vmctx *ctx, int vcpu)
557133808Spjd{
558156612Spjd	struct vm_nmi vmnmi;
559133808Spjd
560133808Spjd	bzero(&vmnmi, sizeof(vmnmi));
561133808Spjd	vmnmi.cpuid = vcpu;
562133808Spjd
563133808Spjd	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
564133808Spjd}
565133808Spjd
566133808Spjdstatic struct {
567133808Spjd	const char	*name;
568133808Spjd	int		type;
569133808Spjd} capstrmap[] = {
570133808Spjd	{ "hlt_exit",		VM_CAP_HALT_EXIT },
571133808Spjd	{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
572133808Spjd	{ "pause_exit",		VM_CAP_PAUSE_EXIT },
573133808Spjd	{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
574133808Spjd	{ "enable_invpcid",	VM_CAP_ENABLE_INVPCID },
575133808Spjd	{ 0 }
576133808Spjd};
577133808Spjd
578156612Spjdint
579133808Spjdvm_capability_name2type(const char *capname)
580133808Spjd{
581133808Spjd	int i;
582133808Spjd
583133808Spjd	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
584133808Spjd		if (strcmp(capstrmap[i].name, capname) == 0)
585133808Spjd			return (capstrmap[i].type);
586133808Spjd	}
587133808Spjd
588133808Spjd	return (-1);
589133808Spjd}
590133808Spjd
591156612Spjdconst char *
592133808Spjdvm_capability_type2name(int type)
593{
594	int i;
595
596	for (i = 0; capstrmap[i].name != NULL; i++) {
597		if (capstrmap[i].type == type)
598			return (capstrmap[i].name);
599	}
600
601	return (NULL);
602}
603
604int
605vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
606		  int *retval)
607{
608	int error;
609	struct vm_capability vmcap;
610
611	bzero(&vmcap, sizeof(vmcap));
612	vmcap.cpuid = vcpu;
613	vmcap.captype = cap;
614
615	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
616	*retval = vmcap.capval;
617	return (error);
618}
619
620int
621vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
622{
623	struct vm_capability vmcap;
624
625	bzero(&vmcap, sizeof(vmcap));
626	vmcap.cpuid = vcpu;
627	vmcap.captype = cap;
628	vmcap.capval = val;
629
630	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
631}
632
633int
634vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
635{
636	struct vm_pptdev pptdev;
637
638	bzero(&pptdev, sizeof(pptdev));
639	pptdev.bus = bus;
640	pptdev.slot = slot;
641	pptdev.func = func;
642
643	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
644}
645
646int
647vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
648{
649	struct vm_pptdev pptdev;
650
651	bzero(&pptdev, sizeof(pptdev));
652	pptdev.bus = bus;
653	pptdev.slot = slot;
654	pptdev.func = func;
655
656	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
657}
658
659int
660vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
661		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
662{
663	struct vm_pptdev_mmio pptmmio;
664
665	bzero(&pptmmio, sizeof(pptmmio));
666	pptmmio.bus = bus;
667	pptmmio.slot = slot;
668	pptmmio.func = func;
669	pptmmio.gpa = gpa;
670	pptmmio.len = len;
671	pptmmio.hpa = hpa;
672
673	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
674}
675
676int
677vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
678    uint64_t addr, uint64_t msg, int numvec)
679{
680	struct vm_pptdev_msi pptmsi;
681
682	bzero(&pptmsi, sizeof(pptmsi));
683	pptmsi.vcpu = vcpu;
684	pptmsi.bus = bus;
685	pptmsi.slot = slot;
686	pptmsi.func = func;
687	pptmsi.msg = msg;
688	pptmsi.addr = addr;
689	pptmsi.numvec = numvec;
690
691	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
692}
693
694int
695vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
696    int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
697{
698	struct vm_pptdev_msix pptmsix;
699
700	bzero(&pptmsix, sizeof(pptmsix));
701	pptmsix.vcpu = vcpu;
702	pptmsix.bus = bus;
703	pptmsix.slot = slot;
704	pptmsix.func = func;
705	pptmsix.idx = idx;
706	pptmsix.msg = msg;
707	pptmsix.addr = addr;
708	pptmsix.vector_control = vector_control;
709
710	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
711}
712
713uint64_t *
714vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
715	     int *ret_entries)
716{
717	int error;
718
719	static struct vm_stats vmstats;
720
721	vmstats.cpuid = vcpu;
722
723	error = ioctl(ctx->fd, VM_STATS, &vmstats);
724	if (error == 0) {
725		if (ret_entries)
726			*ret_entries = vmstats.num_entries;
727		if (ret_tv)
728			*ret_tv = vmstats.tv;
729		return (vmstats.statbuf);
730	} else
731		return (NULL);
732}
733
734const char *
735vm_get_stat_desc(struct vmctx *ctx, int index)
736{
737	static struct vm_stat_desc statdesc;
738
739	statdesc.index = index;
740	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
741		return (statdesc.desc);
742	else
743		return (NULL);
744}
745
746int
747vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
748{
749	int error;
750	struct vm_x2apic x2apic;
751
752	bzero(&x2apic, sizeof(x2apic));
753	x2apic.cpuid = vcpu;
754
755	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
756	*state = x2apic.state;
757	return (error);
758}
759
760int
761vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
762{
763	int error;
764	struct vm_x2apic x2apic;
765
766	bzero(&x2apic, sizeof(x2apic));
767	x2apic.cpuid = vcpu;
768	x2apic.state = state;
769
770	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
771
772	return (error);
773}
774
775/*
776 * From Intel Vol 3a:
777 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
778 */
779int
780vcpu_reset(struct vmctx *vmctx, int vcpu)
781{
782	int error;
783	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
784	uint32_t desc_access, desc_limit;
785	uint16_t sel;
786
787	zero = 0;
788
789	rflags = 0x2;
790	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
791	if (error)
792		goto done;
793
794	rip = 0xfff0;
795	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
796		goto done;
797
798	cr0 = CR0_NE;
799	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
800		goto done;
801
802	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
803		goto done;
804
805	cr4 = 0;
806	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
807		goto done;
808
809	/*
810	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
811	 */
812	desc_base = 0xffff0000;
813	desc_limit = 0xffff;
814	desc_access = 0x0093;
815	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
816			    desc_base, desc_limit, desc_access);
817	if (error)
818		goto done;
819
820	sel = 0xf000;
821	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
822		goto done;
823
824	/*
825	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
826	 */
827	desc_base = 0;
828	desc_limit = 0xffff;
829	desc_access = 0x0093;
830	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
831			    desc_base, desc_limit, desc_access);
832	if (error)
833		goto done;
834
835	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
836			    desc_base, desc_limit, desc_access);
837	if (error)
838		goto done;
839
840	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
841			    desc_base, desc_limit, desc_access);
842	if (error)
843		goto done;
844
845	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
846			    desc_base, desc_limit, desc_access);
847	if (error)
848		goto done;
849
850	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
851			    desc_base, desc_limit, desc_access);
852	if (error)
853		goto done;
854
855	sel = 0;
856	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
857		goto done;
858	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
859		goto done;
860	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
861		goto done;
862	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
863		goto done;
864	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
865		goto done;
866
867	/* General purpose registers */
868	rdx = 0xf00;
869	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
870		goto done;
871	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
872		goto done;
873	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
874		goto done;
875	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
876		goto done;
877	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
878		goto done;
879	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
880		goto done;
881	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
882		goto done;
883	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
884		goto done;
885
886	/* GDTR, IDTR */
887	desc_base = 0;
888	desc_limit = 0xffff;
889	desc_access = 0;
890	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
891			    desc_base, desc_limit, desc_access);
892	if (error != 0)
893		goto done;
894
895	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
896			    desc_base, desc_limit, desc_access);
897	if (error != 0)
898		goto done;
899
900	/* TR */
901	desc_base = 0;
902	desc_limit = 0xffff;
903	desc_access = 0x0000008b;
904	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
905	if (error)
906		goto done;
907
908	sel = 0;
909	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
910		goto done;
911
912	/* LDTR */
913	desc_base = 0;
914	desc_limit = 0xffff;
915	desc_access = 0x00000082;
916	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
917			    desc_limit, desc_access);
918	if (error)
919		goto done;
920
921	sel = 0;
922	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
923		goto done;
924
925	/* XXX cr2, debug registers */
926
927	error = 0;
928done:
929	return (error);
930}
931
932int
933vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
934{
935	int error, i;
936	struct vm_gpa_pte gpapte;
937
938	bzero(&gpapte, sizeof(gpapte));
939	gpapte.gpa = gpa;
940
941	error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
942
943	if (error == 0) {
944		*num = gpapte.ptenum;
945		for (i = 0; i < gpapte.ptenum; i++)
946			pte[i] = gpapte.pte[i];
947	}
948
949	return (error);
950}
951
952int
953vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
954{
955	int error;
956	struct vm_hpet_cap cap;
957
958	bzero(&cap, sizeof(struct vm_hpet_cap));
959	error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
960	if (capabilities != NULL)
961		*capabilities = cap.capabilities;
962	return (error);
963}
964
965static int
966gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
967    uint64_t gla, int prot, int *fault, uint64_t *gpa)
968{
969	struct vm_gla2gpa gg;
970	int error;
971
972	bzero(&gg, sizeof(struct vm_gla2gpa));
973	gg.vcpuid = vcpu;
974	gg.prot = prot;
975	gg.gla = gla;
976	gg.paging = *paging;
977
978	error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
979	if (error == 0) {
980		*fault = gg.fault;
981		*gpa = gg.gpa;
982	}
983	return (error);
984}
985
986#ifndef min
987#define	min(a,b)	(((a) < (b)) ? (a) : (b))
988#endif
989
990int
991vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
992    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt)
993{
994	uint64_t gpa;
995	int error, fault, i, n, off;
996
997	for (i = 0; i < iovcnt; i++) {
998		iov[i].iov_base = 0;
999		iov[i].iov_len = 0;
1000	}
1001
1002	while (len) {
1003		assert(iovcnt > 0);
1004		error = gla2gpa(ctx, vcpu, paging, gla, prot, &fault, &gpa);
1005		if (error)
1006			return (-1);
1007		if (fault)
1008			return (1);
1009
1010		off = gpa & PAGE_MASK;
1011		n = min(len, PAGE_SIZE - off);
1012
1013		iov->iov_base = (void *)gpa;
1014		iov->iov_len = n;
1015		iov++;
1016		iovcnt--;
1017
1018		gla += n;
1019		len -= n;
1020	}
1021	return (0);
1022}
1023
1024void
1025vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1026{
1027	const char *src;
1028	char *dst;
1029	uint64_t gpa;
1030	size_t n;
1031
1032	dst = vp;
1033	while (len) {
1034		assert(iov->iov_len);
1035		gpa = (uint64_t)iov->iov_base;
1036		n = min(len, iov->iov_len);
1037		src = vm_map_gpa(ctx, gpa, n);
1038		bcopy(src, dst, n);
1039
1040		iov++;
1041		dst += n;
1042		len -= n;
1043	}
1044}
1045
1046void
1047vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1048    size_t len)
1049{
1050	const char *src;
1051	char *dst;
1052	uint64_t gpa;
1053	size_t n;
1054
1055	src = vp;
1056	while (len) {
1057		assert(iov->iov_len);
1058		gpa = (uint64_t)iov->iov_base;
1059		n = min(len, iov->iov_len);
1060		dst = vm_map_gpa(ctx, gpa, n);
1061		bcopy(src, dst, n);
1062
1063		iov++;
1064		src += n;
1065		len -= n;
1066	}
1067}
1068
1069static int
1070vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1071{
1072	struct vm_cpuset vm_cpuset;
1073	int error;
1074
1075	bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1076	vm_cpuset.which = which;
1077	vm_cpuset.cpusetsize = sizeof(cpuset_t);
1078	vm_cpuset.cpus = cpus;
1079
1080	error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1081	return (error);
1082}
1083
1084int
1085vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1086{
1087
1088	return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1089}
1090
1091int
1092vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1093{
1094
1095	return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1096}
1097
1098int
1099vm_activate_cpu(struct vmctx *ctx, int vcpu)
1100{
1101	struct vm_activate_cpu ac;
1102	int error;
1103
1104	bzero(&ac, sizeof(struct vm_activate_cpu));
1105	ac.vcpuid = vcpu;
1106	error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1107	return (error);
1108}
1109
1110int
1111vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1112{
1113	struct vm_intinfo vmii;
1114	int error;
1115
1116	bzero(&vmii, sizeof(struct vm_intinfo));
1117	vmii.vcpuid = vcpu;
1118	error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1119	if (error == 0) {
1120		*info1 = vmii.info1;
1121		*info2 = vmii.info2;
1122	}
1123	return (error);
1124}
1125
1126int
1127vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1128{
1129	struct vm_intinfo vmii;
1130	int error;
1131
1132	bzero(&vmii, sizeof(struct vm_intinfo));
1133	vmii.vcpuid = vcpu;
1134	vmii.info1 = info1;
1135	error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1136	return (error);
1137}
1138