vmmapi.c revision 239026
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/types.h>
33#include <sys/sysctl.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36
37#include <machine/specialreg.h>
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <assert.h>
42#include <string.h>
43#include <fcntl.h>
44#include <unistd.h>
45
46#include <machine/vmm.h>
47#include <machine/vmm_dev.h>
48
49#include "vmmapi.h"
50#include "mptable.h"
51
52#define BIOS_ROM_BASE		(0xf0000)
53#define BIOS_ROM_SIZE		(0x10000)
54
55struct vmctx {
56	int	fd;
57	char	*name;
58};
59
60#define	CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
61#define	DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
62
63static int
64vm_device_open(const char *name)
65{
66        int fd, len;
67        char *vmfile;
68
69	len = strlen("/dev/vmm/") + strlen(name) + 1;
70	vmfile = malloc(len);
71	assert(vmfile != NULL);
72	snprintf(vmfile, len, "/dev/vmm/%s", name);
73
74        /* Open the device file */
75        fd = open(vmfile, O_RDWR, 0);
76
77	free(vmfile);
78        return (fd);
79}
80
81int
82vm_create(const char *name)
83{
84
85	return (CREATE((char *)name));
86}
87
88struct vmctx *
89vm_open(const char *name)
90{
91	struct vmctx *vm;
92
93	vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
94	assert(vm != NULL);
95
96	vm->fd = -1;
97	vm->name = (char *)(vm + 1);
98	strcpy(vm->name, name);
99
100	if ((vm->fd = vm_device_open(vm->name)) < 0)
101		goto err;
102
103	return (vm);
104err:
105	vm_destroy(vm);
106	return (NULL);
107}
108
109void
110vm_destroy(struct vmctx *vm)
111{
112	assert(vm != NULL);
113
114	DESTROY(vm->name);
115	if (vm->fd >= 0)
116		close(vm->fd);
117	free(vm);
118}
119
120int
121vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa,
122		  vm_paddr_t *ret_hpa, size_t *ret_len)
123{
124	int error;
125	struct vm_memory_segment seg;
126
127	bzero(&seg, sizeof(seg));
128	seg.gpa = gpa;
129	error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
130	*ret_hpa = seg.hpa;
131	*ret_len = seg.len;
132	return (error);
133}
134
135int
136vm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **mapaddr)
137{
138	int error;
139	struct vm_memory_segment seg;
140
141	/*
142	 * Create and optionally map 'len' bytes of memory at guest
143	 * physical address 'gpa'
144	 */
145	bzero(&seg, sizeof(seg));
146	seg.gpa = gpa;
147	seg.len = len;
148	error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
149	if (error == 0 && mapaddr != NULL) {
150		*mapaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
151				ctx->fd, gpa);
152	}
153	return (error);
154}
155
156char *
157vm_map_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len)
158{
159
160	/* Map 'len' bytes of memory at guest physical address 'gpa' */
161	return ((char *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
162		     ctx->fd, gpa));
163}
164
165int
166vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
167	    uint64_t base, uint32_t limit, uint32_t access)
168{
169	int error;
170	struct vm_seg_desc vmsegdesc;
171
172	bzero(&vmsegdesc, sizeof(vmsegdesc));
173	vmsegdesc.cpuid = vcpu;
174	vmsegdesc.regnum = reg;
175	vmsegdesc.desc.base = base;
176	vmsegdesc.desc.limit = limit;
177	vmsegdesc.desc.access = access;
178
179	error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
180	return (error);
181}
182
183int
184vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
185	    uint64_t *base, uint32_t *limit, uint32_t *access)
186{
187	int error;
188	struct vm_seg_desc vmsegdesc;
189
190	bzero(&vmsegdesc, sizeof(vmsegdesc));
191	vmsegdesc.cpuid = vcpu;
192	vmsegdesc.regnum = reg;
193
194	error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
195	if (error == 0) {
196		*base = vmsegdesc.desc.base;
197		*limit = vmsegdesc.desc.limit;
198		*access = vmsegdesc.desc.access;
199	}
200	return (error);
201}
202
203int
204vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
205{
206	int error;
207	struct vm_register vmreg;
208
209	bzero(&vmreg, sizeof(vmreg));
210	vmreg.cpuid = vcpu;
211	vmreg.regnum = reg;
212	vmreg.regval = val;
213
214	error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
215	return (error);
216}
217
218int
219vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
220{
221	int error;
222	struct vm_register vmreg;
223
224	bzero(&vmreg, sizeof(vmreg));
225	vmreg.cpuid = vcpu;
226	vmreg.regnum = reg;
227
228	error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
229	*ret_val = vmreg.regval;
230	return (error);
231}
232
233int
234vm_get_pinning(struct vmctx *ctx, int vcpu, int *host_cpuid)
235{
236	int error;
237	struct vm_pin vmpin;
238
239	bzero(&vmpin, sizeof(vmpin));
240	vmpin.vm_cpuid = vcpu;
241
242	error = ioctl(ctx->fd, VM_GET_PINNING, &vmpin);
243	*host_cpuid = vmpin.host_cpuid;
244	return (error);
245}
246
247int
248vm_set_pinning(struct vmctx *ctx, int vcpu, int host_cpuid)
249{
250	int error;
251	struct vm_pin vmpin;
252
253	bzero(&vmpin, sizeof(vmpin));
254	vmpin.vm_cpuid = vcpu;
255	vmpin.host_cpuid = host_cpuid;
256
257	error = ioctl(ctx->fd, VM_SET_PINNING, &vmpin);
258	return (error);
259}
260
261int
262vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vm_exit *vmexit)
263{
264	int error;
265	struct vm_run vmrun;
266
267	bzero(&vmrun, sizeof(vmrun));
268	vmrun.cpuid = vcpu;
269	vmrun.rip = rip;
270
271	error = ioctl(ctx->fd, VM_RUN, &vmrun);
272	bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
273	return (error);
274}
275
276static int
277vm_inject_event_real(struct vmctx *ctx, int vcpu, enum vm_event_type type,
278		     int vector, int error_code, int error_code_valid)
279{
280	struct vm_event ev;
281
282	bzero(&ev, sizeof(ev));
283	ev.cpuid = vcpu;
284	ev.type = type;
285	ev.vector = vector;
286	ev.error_code = error_code;
287	ev.error_code_valid = error_code_valid;
288
289	return (ioctl(ctx->fd, VM_INJECT_EVENT, &ev));
290}
291
292int
293vm_inject_event(struct vmctx *ctx, int vcpu, enum vm_event_type type,
294		int vector)
295{
296
297	return (vm_inject_event_real(ctx, vcpu, type, vector, 0, 0));
298}
299
300int
301vm_inject_event2(struct vmctx *ctx, int vcpu, enum vm_event_type type,
302		 int vector, int error_code)
303{
304
305	return (vm_inject_event_real(ctx, vcpu, type, vector, error_code, 1));
306}
307
308int
309vm_build_tables(struct vmctx *ctxt, int ncpu, void *oemtbl, int oemtblsz)
310{
311
312	return (vm_build_mptable(ctxt, BIOS_ROM_BASE, BIOS_ROM_SIZE, ncpu,
313				 oemtbl, oemtblsz));
314}
315
316int
317vm_apicid2vcpu(struct vmctx *ctx, int apicid)
318{
319	/*
320	 * The apic id associated with the 'vcpu' has the same numerical value
321	 * as the 'vcpu' itself.
322	 */
323	return (apicid);
324}
325
326int
327vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
328{
329	struct vm_lapic_irq vmirq;
330
331	bzero(&vmirq, sizeof(vmirq));
332	vmirq.cpuid = vcpu;
333	vmirq.vector = vector;
334
335	return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
336}
337
338int
339vm_inject_nmi(struct vmctx *ctx, int vcpu)
340{
341	struct vm_nmi vmnmi;
342
343	bzero(&vmnmi, sizeof(vmnmi));
344	vmnmi.cpuid = vcpu;
345
346	return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
347}
348
349int
350vm_capability_name2type(const char *capname)
351{
352	int i;
353
354	static struct {
355		const char	*name;
356		int		type;
357	} capstrmap[] = {
358		{ "hlt_exit",		VM_CAP_HALT_EXIT },
359		{ "mtrap_exit",		VM_CAP_MTRAP_EXIT },
360		{ "pause_exit",		VM_CAP_PAUSE_EXIT },
361		{ "unrestricted_guest",	VM_CAP_UNRESTRICTED_GUEST },
362		{ 0 }
363	};
364
365	for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
366		if (strcmp(capstrmap[i].name, capname) == 0)
367			return (capstrmap[i].type);
368	}
369
370	return (-1);
371}
372
373int
374vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
375		  int *retval)
376{
377	int error;
378	struct vm_capability vmcap;
379
380	bzero(&vmcap, sizeof(vmcap));
381	vmcap.cpuid = vcpu;
382	vmcap.captype = cap;
383
384	error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
385	*retval = vmcap.capval;
386	return (error);
387}
388
389int
390vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
391{
392	struct vm_capability vmcap;
393
394	bzero(&vmcap, sizeof(vmcap));
395	vmcap.cpuid = vcpu;
396	vmcap.captype = cap;
397	vmcap.capval = val;
398
399	return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
400}
401
402int
403vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
404{
405	struct vm_pptdev pptdev;
406
407	bzero(&pptdev, sizeof(pptdev));
408	pptdev.bus = bus;
409	pptdev.slot = slot;
410	pptdev.func = func;
411
412	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
413}
414
415int
416vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
417{
418	struct vm_pptdev pptdev;
419
420	bzero(&pptdev, sizeof(pptdev));
421	pptdev.bus = bus;
422	pptdev.slot = slot;
423	pptdev.func = func;
424
425	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
426}
427
428int
429vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
430		   vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
431{
432	struct vm_pptdev_mmio pptmmio;
433
434	bzero(&pptmmio, sizeof(pptmmio));
435	pptmmio.bus = bus;
436	pptmmio.slot = slot;
437	pptmmio.func = func;
438	pptmmio.gpa = gpa;
439	pptmmio.len = len;
440	pptmmio.hpa = hpa;
441
442	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
443}
444
445int
446vm_setup_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
447	     int destcpu, int vector, int numvec)
448{
449	struct vm_pptdev_msi pptmsi;
450
451	bzero(&pptmsi, sizeof(pptmsi));
452	pptmsi.vcpu = vcpu;
453	pptmsi.bus = bus;
454	pptmsi.slot = slot;
455	pptmsi.func = func;
456	pptmsi.destcpu = destcpu;
457	pptmsi.vector = vector;
458	pptmsi.numvec = numvec;
459
460	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
461}
462
463int
464vm_setup_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
465	      int idx, uint32_t msg, uint32_t vector_control, uint64_t addr)
466{
467	struct vm_pptdev_msix pptmsix;
468
469	bzero(&pptmsix, sizeof(pptmsix));
470	pptmsix.vcpu = vcpu;
471	pptmsix.bus = bus;
472	pptmsix.slot = slot;
473	pptmsix.func = func;
474	pptmsix.idx = idx;
475	pptmsix.msg = msg;
476	pptmsix.addr = addr;
477	pptmsix.vector_control = vector_control;
478
479	return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
480}
481
482uint64_t *
483vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
484	     int *ret_entries)
485{
486	int error;
487
488	static struct vm_stats vmstats;
489
490	vmstats.cpuid = vcpu;
491
492	error = ioctl(ctx->fd, VM_STATS, &vmstats);
493	if (error == 0) {
494		if (ret_entries)
495			*ret_entries = vmstats.num_entries;
496		if (ret_tv)
497			*ret_tv = vmstats.tv;
498		return (vmstats.statbuf);
499	} else
500		return (NULL);
501}
502
503const char *
504vm_get_stat_desc(struct vmctx *ctx, int index)
505{
506	static struct vm_stat_desc statdesc;
507
508	statdesc.index = index;
509	if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
510		return (statdesc.desc);
511	else
512		return (NULL);
513}
514
515/*
516 * From Intel Vol 3a:
517 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
518 */
519int
520vcpu_reset(struct vmctx *vmctx, int vcpu)
521{
522	int error;
523	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
524	uint32_t desc_access, desc_limit;
525	uint16_t sel;
526
527	zero = 0;
528
529	rflags = 0x2;
530	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
531	if (error)
532		goto done;
533
534	rip = 0xfff0;
535	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
536		goto done;
537
538	cr0 = CR0_NE;
539	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
540		goto done;
541
542	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
543		goto done;
544
545	cr4 = 0;
546	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
547		goto done;
548
549	/*
550	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
551	 */
552	desc_base = 0xffff0000;
553	desc_limit = 0xffff;
554	desc_access = 0x0093;
555	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
556			    desc_base, desc_limit, desc_access);
557	if (error)
558		goto done;
559
560	sel = 0xf000;
561	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
562		goto done;
563
564	/*
565	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
566	 */
567	desc_base = 0;
568	desc_limit = 0xffff;
569	desc_access = 0x0093;
570	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
571			    desc_base, desc_limit, desc_access);
572	if (error)
573		goto done;
574
575	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
576			    desc_base, desc_limit, desc_access);
577	if (error)
578		goto done;
579
580	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
581			    desc_base, desc_limit, desc_access);
582	if (error)
583		goto done;
584
585	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
586			    desc_base, desc_limit, desc_access);
587	if (error)
588		goto done;
589
590	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
591			    desc_base, desc_limit, desc_access);
592	if (error)
593		goto done;
594
595	sel = 0;
596	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
597		goto done;
598	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
599		goto done;
600	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
601		goto done;
602	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
603		goto done;
604	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
605		goto done;
606
607	/* General purpose registers */
608	rdx = 0xf00;
609	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
610		goto done;
611	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
612		goto done;
613	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
614		goto done;
615	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
616		goto done;
617	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
618		goto done;
619	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
620		goto done;
621	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
622		goto done;
623	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
624		goto done;
625
626	/* GDTR, IDTR */
627	desc_base = 0;
628	desc_limit = 0xffff;
629	desc_access = 0;
630	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
631			    desc_base, desc_limit, desc_access);
632	if (error != 0)
633		goto done;
634
635	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
636			    desc_base, desc_limit, desc_access);
637	if (error != 0)
638		goto done;
639
640	/* TR */
641	desc_base = 0;
642	desc_limit = 0xffff;
643	desc_access = 0x0000008b;
644	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
645	if (error)
646		goto done;
647
648	sel = 0;
649	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
650		goto done;
651
652	/* LDTR */
653	desc_base = 0;
654	desc_limit = 0xffff;
655	desc_access = 0x00000082;
656	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
657			    desc_limit, desc_access);
658	if (error)
659		goto done;
660
661	sel = 0;
662	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
663		goto done;
664
665	/* XXX cr2, debug registers */
666
667	error = 0;
668done:
669	return (error);
670}
671