vmmapi.c revision 268976
1169691Skan/*-
2169691Skan * Copyright (c) 2011 NetApp, Inc.
3169691Skan * All rights reserved.
4169691Skan *
5169691Skan * Redistribution and use in source and binary forms, with or without
6169691Skan * modification, are permitted provided that the following conditions
7169691Skan * are met:
8169691Skan * 1. Redistributions of source code must retain the above copyright
9169691Skan *    notice, this list of conditions and the following disclaimer.
10169691Skan * 2. Redistributions in binary form must reproduce the above copyright
11169691Skan *    notice, this list of conditions and the following disclaimer in the
12169691Skan *    documentation and/or other materials provided with the distribution.
13169691Skan *
14169691Skan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17169691Skan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24169691Skan * SUCH DAMAGE.
25169691Skan *
26169691Skan * $FreeBSD: stable/10/lib/libvmmapi/vmmapi.c 268976 2014-07-22 04:39:16Z jhb $
27169691Skan */
28169691Skan
29169691Skan#include <sys/cdefs.h>
30169691Skan__FBSDID("$FreeBSD: stable/10/lib/libvmmapi/vmmapi.c 268976 2014-07-22 04:39:16Z jhb $");
31169691Skan
32169691Skan#include <sys/types.h>
33169691Skan#include <sys/sysctl.h>
34169691Skan#include <sys/ioctl.h>
35169691Skan#include <sys/mman.h>
36169691Skan#include <sys/_iovec.h>
37169691Skan
38169691Skan#include <machine/specialreg.h>
39169691Skan#include <machine/param.h>
40169691Skan
41169691Skan#include <stdio.h>
42169691Skan#include <stdlib.h>
43169691Skan#include <assert.h>
44169691Skan#include <string.h>
45169691Skan#include <fcntl.h>
46169691Skan#include <unistd.h>
47169691Skan
48169691Skan#include <libutil.h>
49169691Skan
50169691Skan#include <machine/vmm.h>
51169691Skan#include <machine/vmm_dev.h>
52169691Skan
53169691Skan#include "vmmapi.h"
54169691Skan
55169691Skan#define	MB	(1024 * 1024UL)
56169691Skan#define	GB	(1024 * 1024 * 1024UL)
57169691Skan
58169691Skanstruct vmctx {
59169691Skan	int	fd;
60169691Skan	uint32_t lowmem_limit;
61169691Skan	enum vm_mmap_style vms;
62169691Skan	int	memflags;
63169691Skan	size_t	lowmem;
64169691Skan	char	*lowmem_addr;
65169691Skan	size_t	highmem;
66169691Skan	char	*highmem_addr;
67169691Skan	char	*name;
68169691Skan};
69169691Skan
70169691Skan#define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
71169691Skan#define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
72169691Skan
73169691Skanstatic int
74169691Skanvm_device_open(const char *name)
75169691Skan{
76169691Skan        int fd, len;
77169691Skan        char *vmfile;
78169691Skan
79169691Skan	len = strlen("/dev/vmm/") + strlen(name) + 1;
80169691Skan	vmfile = malloc(len);
81169691Skan	assert(vmfile != NULL);
82169691Skan	snprintf(vmfile, len, "/dev/vmm/%s", name);
83169691Skan
84169691Skan        /* Open the device file */
85169691Skan        fd = open(vmfile, O_RDWR, 0);
86169691Skan
87169691Skan	free(vmfile);
88169691Skan        return (fd);
89169691Skan}
90169691Skan
91169691Skanint
92169691Skanvm_create(const char *name)
93169691Skan{
94169691Skan
95169691Skan	return (CREATE((char *)name));
96169691Skan}
97169691Skan
98169691Skanstruct vmctx *
99169691Skanvm_open(const char *name)
100169691Skan{
101169691Skan	struct vmctx *vm;
102169691Skan
103169691Skan	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
104169691Skan	assert(vm != NULL);
105169691Skan
106169691Skan	vm->fd = -1;
107169691Skan	vm->memflags = 0;
108169691Skan	vm->lowmem_limit = 3 * GB;
109169691Skan	vm->name = (char *)(vm + 1);
110169691Skan	strcpy(vm->name, name);
111169691Skan
112169691Skan	if ((vm->fd = vm_device_open(vm->name)) < 0)
113169691Skan		goto err;
114169691Skan
115169691Skan	return (vm);
116169691Skanerr:
117169691Skan	vm_destroy(vm);
118169691Skan	return (NULL);
119169691Skan}
120169691Skan
121169691Skanvoid
122169691Skanvm_destroy(struct vmctx *vm)
123169691Skan{
124169691Skan	assert(vm != NULL);
125169691Skan
126169691Skan	if (vm->fd >= 0)
127169691Skan		close(vm->fd);
128169691Skan	DESTROY(vm->name);
129169691Skan
130169691Skan	free(vm);
131169691Skan}
132169691Skan
133169691Skanint
134169691Skanvm_parse_memsize(const char *optarg, size_t *ret_memsize)
135169691Skan{
136169691Skan	char *endptr;
137169691Skan	size_t optval;
138169691Skan	int error;
139169691Skan
140169691Skan	optval = strtoul(optarg, &endptr, 0);
141169691Skan	if (*optarg != '\0' && *endptr == '\0') {
142169691Skan		/*
143169691Skan		 * For the sake of backward compatibility if the memory size
144169691Skan		 * specified on the command line is less than a megabyte then
145169691Skan		 * it is interpreted as being in units of MB.
146169691Skan		 */
147169691Skan		if (optval < MB)
148169691Skan			optval *= MB;
149169691Skan		*ret_memsize = optval;
150169691Skan		error = 0;
151169691Skan	} else
152169691Skan		error = expand_number(optarg, ret_memsize);
153169691Skan
154169691Skan	return (error);
155169691Skan}
156169691Skan
157169691Skanint
158169691Skanvm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len,
159169691Skan		  int *wired)
160169691Skan{
161169691Skan	int error;
162169691Skan	struct vm_memory_segment seg;
163169691Skan
164169691Skan	bzero(&seg, sizeof(seg));
165169691Skan	seg.gpa = gpa;
166169691Skan	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
167169691Skan	*ret_len = seg.len;
168169691Skan	if (wired != NULL)
169169691Skan		*wired = seg.wired;
170169691Skan	return (error);
171169691Skan}
172169691Skan
173169691Skanuint32_t
174169691Skanvm_get_lowmem_limit(struct vmctx *ctx)
175169691Skan{
176169691Skan
177169691Skan	return (ctx->lowmem_limit);
178169691Skan}
179169691Skan
180169691Skanvoid
181169691Skanvm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
182169691Skan{
183169691Skan
184169691Skan	ctx->lowmem_limit = limit;
185169691Skan}
186169691Skan
187169691Skanvoid
188169691Skanvm_set_memflags(struct vmctx *ctx, int flags)
189169691Skan{
190169691Skan
191169691Skan	ctx->memflags = flags;
192169691Skan}
193169691Skan
194169691Skanstatic int
195169691Skansetup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
196169691Skan{
197169691Skan	int error, mmap_flags;
198169691Skan	struct vm_memory_segment seg;
199169691Skan
200169691Skan	/*
201169691Skan	 * Create and optionally map 'len' bytes of memory at guest
202169691Skan	 * physical address 'gpa'
203169691Skan	 */
204169691Skan	bzero(&seg, sizeof(seg));
205169691Skan	seg.gpa = gpa;
206169691Skan	seg.len = len;
207169691Skan	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
208169691Skan	if (error == 0 && addr != NULL) {
209169691Skan		mmap_flags = MAP_SHARED;
210169691Skan		if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
211169691Skan			mmap_flags |= MAP_NOCORE;
212169691Skan		*addr = mmap(NULL, len, PROT_READ | PROT_WRITE, mmap_flags,
213169691Skan		    ctx->fd, gpa);
214169691Skan	}
215169691Skan	return (error);
216169691Skan}
217169691Skan
218169691Skanint
219169691Skanvm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
220169691Skan{
221169691Skan	char **addr;
222169691Skan	int error;
223169691Skan
224169691Skan	/* XXX VM_MMAP_SPARSE not implemented yet */
225169691Skan	assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
226169691Skan	ctx->vms = vms;
227169691Skan
228169691Skan	/*
229169691Skan	 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
230169691Skan	 * create another 'highmem' segment above 4GB for the remainder.
231169691Skan	 */
232169691Skan	if (memsize > ctx->lowmem_limit) {
233169691Skan		ctx->lowmem = ctx->lowmem_limit;
234169691Skan		ctx->highmem = memsize - ctx->lowmem;
235169691Skan	} else {
236169691Skan		ctx->lowmem = memsize;
237169691Skan		ctx->highmem = 0;
238169691Skan	}
239169691Skan
240169691Skan	if (ctx->lowmem > 0) {
241169691Skan		addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
242169691Skan		error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
243169691Skan		if (error)
244169691Skan			return (error);
245169691Skan	}
246169691Skan
247169691Skan	if (ctx->highmem > 0) {
248169691Skan		addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
249169691Skan		error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
250169691Skan		if (error)
251169691Skan			return (error);
252169691Skan	}
253169691Skan
254169691Skan	return (0);
255169691Skan}
256169691Skan
257169691Skanvoid *
258169691Skanvm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
259169691Skan{
260169691Skan
261	/* XXX VM_MMAP_SPARSE not implemented yet */
262	assert(ctx->vms == VM_MMAP_ALL);
263
264	if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
265		return ((void *)(ctx->lowmem_addr + gaddr));
266
267	if (gaddr >= 4*GB) {
268		gaddr -= 4*GB;
269		if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
270			return ((void *)(ctx->highmem_addr + gaddr));
271	}
272
273	return (NULL);
274}
275
276int
277vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
278	    uint64_t base, uint32_t limit, uint32_t access)
279{
280	int error;
281	struct vm_seg_desc vmsegdesc;
282
283	bzero(&vmsegdesc, sizeof(vmsegdesc));
284	vmsegdesc.cpuid = vcpu;
285	vmsegdesc.regnum = reg;
286	vmsegdesc.desc.base = base;
287	vmsegdesc.desc.limit = limit;
288	vmsegdesc.desc.access = access;
289
290	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
291	return (error);
292}
293
294int
295vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
296	    uint64_t *base, uint32_t *limit, uint32_t *access)
297{
298	int error;
299	struct vm_seg_desc vmsegdesc;
300
301	bzero(&vmsegdesc, sizeof(vmsegdesc));
302	vmsegdesc.cpuid = vcpu;
303	vmsegdesc.regnum = reg;
304
305	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
306	if (error == 0) {
307		*base = vmsegdesc.desc.base;
308		*limit = vmsegdesc.desc.limit;
309		*access = vmsegdesc.desc.access;
310	}
311	return (error);
312}
313
314int
315vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
316{
317	int error;
318	struct vm_register vmreg;
319
320	bzero(&vmreg, sizeof(vmreg));
321	vmreg.cpuid = vcpu;
322	vmreg.regnum = reg;
323	vmreg.regval = val;
324
325	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
326	return (error);
327}
328
329int
330vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
331{
332	int error;
333	struct vm_register vmreg;
334
335	bzero(&vmreg, sizeof(vmreg));
336	vmreg.cpuid = vcpu;
337	vmreg.regnum = reg;
338
339	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
340	*ret_val = vmreg.regval;
341	return (error);
342}
343
344int
345vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
346{
347	int error;
348	struct vm_run vmrun;
349
350	bzero(&vmrun, sizeof(vmrun));
351	vmrun.cpuid = vcpu;
352	vmrun.rip = rip;
353
354	error = ioctl(ctx->fd, VM_RUN, &vmrun);
355	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
356	return (error);
357}
358
359int
360vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
361{
362	struct vm_suspend vmsuspend;
363
364	bzero(&vmsuspend, sizeof(vmsuspend));
365	vmsuspend.how = how;
366	return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
367}
368
369static int
370vm_inject_exception_real(struct vmctx *ctx, int vcpu, int vector,
371    int error_code, int error_code_valid)
372{
373	struct vm_exception exc;
374
375	bzero(&exc, sizeof(exc));
376	exc.cpuid = vcpu;
377	exc.vector = vector;
378	exc.error_code = error_code;
379	exc.error_code_valid = error_code_valid;
380
381	return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
382}
383
384int
385vm_inject_exception(struct vmctx *ctx, int vcpu, int vector)
386{
387
388	return (vm_inject_exception_real(ctx, vcpu, vector, 0, 0));
389}
390
391int
392vm_inject_exception2(struct vmctx *ctx, int vcpu, int vector, int errcode)
393{
394
395	return (vm_inject_exception_real(ctx, vcpu, vector, errcode, 1));
396}
397
398int
399vm_apicid2vcpu(struct vmctx *ctx, int apicid)
400{
401	/*
402	 * The apic id associated with the 'vcpu' has the same numerical value
403	 * as the 'vcpu' itself.
404	 */
405	return (apicid);
406}
407
408int
409vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
410{
411	struct vm_lapic_irq vmirq;
412
413	bzero(&vmirq, sizeof(vmirq));
414	vmirq.cpuid = vcpu;
415	vmirq.vector = vector;
416
417	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
418}
419
420int
421vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
422{
423	struct vm_lapic_irq vmirq;
424
425	bzero(&vmirq, sizeof(vmirq));
426	vmirq.cpuid = vcpu;
427	vmirq.vector = vector;
428
429	return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
430}
431
432int
433vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
434{
435	struct vm_lapic_msi vmmsi;
436
437	bzero(&vmmsi, sizeof(vmmsi));
438	vmmsi.addr = addr;
439	vmmsi.msg = msg;
440
441	return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
442}
443
444int
445vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
446{
447	struct vm_ioapic_irq ioapic_irq;
448
449	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
450	ioapic_irq.irq = irq;
451
452	return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
453}
454
455int
456vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
457{
458	struct vm_ioapic_irq ioapic_irq;
459
460	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
461	ioapic_irq.irq = irq;
462
463	return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
464}
465
466int
467vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
468{
469	struct vm_ioapic_irq ioapic_irq;
470
471	bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
472	ioapic_irq.irq = irq;
473
474	return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
475}
476
477int
478vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
479{
480
481	return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
482}
483
484int
485vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
486{
487	struct vm_isa_irq isa_irq;
488
489	bzero(&isa_irq, sizeof(struct vm_isa_irq));
490	isa_irq.atpic_irq = atpic_irq;
491	isa_irq.ioapic_irq = ioapic_irq;
492
493	return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
494}
495
496int
497vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
498{
499	struct vm_isa_irq isa_irq;
500
501	bzero(&isa_irq, sizeof(struct vm_isa_irq));
502	isa_irq.atpic_irq = atpic_irq;
503	isa_irq.ioapic_irq = ioapic_irq;
504
505	return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
506}
507
508int
509vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
510{
511	struct vm_isa_irq isa_irq;
512
513	bzero(&isa_irq, sizeof(struct vm_isa_irq));
514	isa_irq.atpic_irq = atpic_irq;
515	isa_irq.ioapic_irq = ioapic_irq;
516
517	return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
518}
519
520int
521vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
522    enum vm_intr_trigger trigger)
523{
524	struct vm_isa_irq_trigger isa_irq_trigger;
525
526	bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
527	isa_irq_trigger.atpic_irq = atpic_irq;
528	isa_irq_trigger.trigger = trigger;
529
530	return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
531}
532
533int
534vm_inject_nmi(struct vmctx *ctx, int vcpu)
535{
536	struct vm_nmi vmnmi;
537
538	bzero(&vmnmi, sizeof(vmnmi));
539	vmnmi.cpuid = vcpu;
540
541	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
542}
543
544static struct {
545	const char	*name;
546	int		type;
547} capstrmap[] = {
548	{ "hlt_exit",		VM_CAP_HALT_EXIT },
549	{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
550	{ "pause_exit",		VM_CAP_PAUSE_EXIT },
551	{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
552	{ "enable_invpcid",	VM_CAP_ENABLE_INVPCID },
553	{ 0 }
554};
555
556int
557vm_capability_name2type(const char *capname)
558{
559	int i;
560
561	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
562		if (strcmp(capstrmap[i].name, capname) == 0)
563			return (capstrmap[i].type);
564	}
565
566	return (-1);
567}
568
569const char *
570vm_capability_type2name(int type)
571{
572	int i;
573
574	for (i = 0; capstrmap[i].name != NULL; i++) {
575		if (capstrmap[i].type == type)
576			return (capstrmap[i].name);
577	}
578
579	return (NULL);
580}
581
582int
583vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
584		  int *retval)
585{
586	int error;
587	struct vm_capability vmcap;
588
589	bzero(&vmcap, sizeof(vmcap));
590	vmcap.cpuid = vcpu;
591	vmcap.captype = cap;
592
593	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
594	*retval = vmcap.capval;
595	return (error);
596}
597
598int
599vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
600{
601	struct vm_capability vmcap;
602
603	bzero(&vmcap, sizeof(vmcap));
604	vmcap.cpuid = vcpu;
605	vmcap.captype = cap;
606	vmcap.capval = val;
607
608	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
609}
610
611int
612vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
613{
614	struct vm_pptdev pptdev;
615
616	bzero(&pptdev, sizeof(pptdev));
617	pptdev.bus = bus;
618	pptdev.slot = slot;
619	pptdev.func = func;
620
621	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
622}
623
624int
625vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
626{
627	struct vm_pptdev pptdev;
628
629	bzero(&pptdev, sizeof(pptdev));
630	pptdev.bus = bus;
631	pptdev.slot = slot;
632	pptdev.func = func;
633
634	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
635}
636
637int
638vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
639		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
640{
641	struct vm_pptdev_mmio pptmmio;
642
643	bzero(&pptmmio, sizeof(pptmmio));
644	pptmmio.bus = bus;
645	pptmmio.slot = slot;
646	pptmmio.func = func;
647	pptmmio.gpa = gpa;
648	pptmmio.len = len;
649	pptmmio.hpa = hpa;
650
651	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
652}
653
654int
655vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
656    uint64_t addr, uint64_t msg, int numvec)
657{
658	struct vm_pptdev_msi pptmsi;
659
660	bzero(&pptmsi, sizeof(pptmsi));
661	pptmsi.vcpu = vcpu;
662	pptmsi.bus = bus;
663	pptmsi.slot = slot;
664	pptmsi.func = func;
665	pptmsi.msg = msg;
666	pptmsi.addr = addr;
667	pptmsi.numvec = numvec;
668
669	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
670}
671
672int
673vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
674    int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
675{
676	struct vm_pptdev_msix pptmsix;
677
678	bzero(&pptmsix, sizeof(pptmsix));
679	pptmsix.vcpu = vcpu;
680	pptmsix.bus = bus;
681	pptmsix.slot = slot;
682	pptmsix.func = func;
683	pptmsix.idx = idx;
684	pptmsix.msg = msg;
685	pptmsix.addr = addr;
686	pptmsix.vector_control = vector_control;
687
688	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
689}
690
691uint64_t *
692vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
693	     int *ret_entries)
694{
695	int error;
696
697	static struct vm_stats vmstats;
698
699	vmstats.cpuid = vcpu;
700
701	error = ioctl(ctx->fd, VM_STATS, &vmstats);
702	if (error == 0) {
703		if (ret_entries)
704			*ret_entries = vmstats.num_entries;
705		if (ret_tv)
706			*ret_tv = vmstats.tv;
707		return (vmstats.statbuf);
708	} else
709		return (NULL);
710}
711
712const char *
713vm_get_stat_desc(struct vmctx *ctx, int index)
714{
715	static struct vm_stat_desc statdesc;
716
717	statdesc.index = index;
718	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
719		return (statdesc.desc);
720	else
721		return (NULL);
722}
723
724int
725vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
726{
727	int error;
728	struct vm_x2apic x2apic;
729
730	bzero(&x2apic, sizeof(x2apic));
731	x2apic.cpuid = vcpu;
732
733	error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
734	*state = x2apic.state;
735	return (error);
736}
737
738int
739vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
740{
741	int error;
742	struct vm_x2apic x2apic;
743
744	bzero(&x2apic, sizeof(x2apic));
745	x2apic.cpuid = vcpu;
746	x2apic.state = state;
747
748	error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
749
750	return (error);
751}
752
753/*
754 * From Intel Vol 3a:
755 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
756 */
757int
758vcpu_reset(struct vmctx *vmctx, int vcpu)
759{
760	int error;
761	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
762	uint32_t desc_access, desc_limit;
763	uint16_t sel;
764
765	zero = 0;
766
767	rflags = 0x2;
768	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
769	if (error)
770		goto done;
771
772	rip = 0xfff0;
773	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
774		goto done;
775
776	cr0 = CR0_NE;
777	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
778		goto done;
779
780	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
781		goto done;
782
783	cr4 = 0;
784	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
785		goto done;
786
787	/*
788	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
789	 */
790	desc_base = 0xffff0000;
791	desc_limit = 0xffff;
792	desc_access = 0x0093;
793	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
794			    desc_base, desc_limit, desc_access);
795	if (error)
796		goto done;
797
798	sel = 0xf000;
799	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
800		goto done;
801
802	/*
803	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
804	 */
805	desc_base = 0;
806	desc_limit = 0xffff;
807	desc_access = 0x0093;
808	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
809			    desc_base, desc_limit, desc_access);
810	if (error)
811		goto done;
812
813	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
814			    desc_base, desc_limit, desc_access);
815	if (error)
816		goto done;
817
818	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
819			    desc_base, desc_limit, desc_access);
820	if (error)
821		goto done;
822
823	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
824			    desc_base, desc_limit, desc_access);
825	if (error)
826		goto done;
827
828	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
829			    desc_base, desc_limit, desc_access);
830	if (error)
831		goto done;
832
833	sel = 0;
834	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
835		goto done;
836	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
837		goto done;
838	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
839		goto done;
840	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
841		goto done;
842	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
843		goto done;
844
845	/* General purpose registers */
846	rdx = 0xf00;
847	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
848		goto done;
849	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
850		goto done;
851	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
852		goto done;
853	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
854		goto done;
855	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
856		goto done;
857	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
858		goto done;
859	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
860		goto done;
861	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
862		goto done;
863
864	/* GDTR, IDTR */
865	desc_base = 0;
866	desc_limit = 0xffff;
867	desc_access = 0;
868	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
869			    desc_base, desc_limit, desc_access);
870	if (error != 0)
871		goto done;
872
873	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
874			    desc_base, desc_limit, desc_access);
875	if (error != 0)
876		goto done;
877
878	/* TR */
879	desc_base = 0;
880	desc_limit = 0xffff;
881	desc_access = 0x0000008b;
882	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
883	if (error)
884		goto done;
885
886	sel = 0;
887	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
888		goto done;
889
890	/* LDTR */
891	desc_base = 0;
892	desc_limit = 0xffff;
893	desc_access = 0x00000082;
894	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
895			    desc_limit, desc_access);
896	if (error)
897		goto done;
898
899	sel = 0;
900	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
901		goto done;
902
903	/* XXX cr2, debug registers */
904
905	error = 0;
906done:
907	return (error);
908}
909
910int
911vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
912{
913	int error, i;
914	struct vm_gpa_pte gpapte;
915
916	bzero(&gpapte, sizeof(gpapte));
917	gpapte.gpa = gpa;
918
919	error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
920
921	if (error == 0) {
922		*num = gpapte.ptenum;
923		for (i = 0; i < gpapte.ptenum; i++)
924			pte[i] = gpapte.pte[i];
925	}
926
927	return (error);
928}
929
930int
931vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
932{
933	int error;
934	struct vm_hpet_cap cap;
935
936	bzero(&cap, sizeof(struct vm_hpet_cap));
937	error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
938	if (capabilities != NULL)
939		*capabilities = cap.capabilities;
940	return (error);
941}
942
943static int
944gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
945    uint64_t gla, int prot, int *fault, uint64_t *gpa)
946{
947	struct vm_gla2gpa gg;
948	int error;
949
950	bzero(&gg, sizeof(struct vm_gla2gpa));
951	gg.vcpuid = vcpu;
952	gg.prot = prot;
953	gg.gla = gla;
954	gg.paging = *paging;
955
956	error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
957	if (error == 0) {
958		*fault = gg.fault;
959		*gpa = gg.gpa;
960	}
961	return (error);
962}
963
964#ifndef min
965#define	min(a,b)	(((a) < (b)) ? (a) : (b))
966#endif
967
968int
969vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
970    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt)
971{
972	uint64_t gpa;
973	int error, fault, i, n, off;
974
975	for (i = 0; i < iovcnt; i++) {
976		iov[i].iov_base = 0;
977		iov[i].iov_len = 0;
978	}
979
980	while (len) {
981		assert(iovcnt > 0);
982		error = gla2gpa(ctx, vcpu, paging, gla, prot, &fault, &gpa);
983		if (error)
984			return (-1);
985		if (fault)
986			return (1);
987
988		off = gpa & PAGE_MASK;
989		n = min(len, PAGE_SIZE - off);
990
991		iov->iov_base = (void *)gpa;
992		iov->iov_len = n;
993		iov++;
994		iovcnt--;
995
996		gla += n;
997		len -= n;
998	}
999	return (0);
1000}
1001
1002void
1003vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1004{
1005	const char *src;
1006	char *dst;
1007	uint64_t gpa;
1008	size_t n;
1009
1010	dst = vp;
1011	while (len) {
1012		assert(iov->iov_len);
1013		gpa = (uint64_t)iov->iov_base;
1014		n = min(len, iov->iov_len);
1015		src = vm_map_gpa(ctx, gpa, n);
1016		bcopy(src, dst, n);
1017
1018		iov++;
1019		dst += n;
1020		len -= n;
1021	}
1022}
1023
1024void
1025vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1026    size_t len)
1027{
1028	const char *src;
1029	char *dst;
1030	uint64_t gpa;
1031	size_t n;
1032
1033	src = vp;
1034	while (len) {
1035		assert(iov->iov_len);
1036		gpa = (uint64_t)iov->iov_base;
1037		n = min(len, iov->iov_len);
1038		dst = vm_map_gpa(ctx, gpa, n);
1039		bcopy(src, dst, n);
1040
1041		iov++;
1042		src += n;
1043		len -= n;
1044	}
1045}
1046