xref: /freebsd/sys/arm64/vmm/vmm_dev_machdep.c (revision 0225c6d85d0d4250ed304b31e6ce855fc5d93f15)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * Copyright (C) 2015 Mihai Carabas <mihai.carabas@gmail.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/conf.h>
33 #include <sys/libkern.h>
34 #include <sys/ioccom.h>
35 #include <sys/mman.h>
36 #include <sys/uio.h>
37 #include <sys/proc.h>
38 
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <vm/vm_map.h>
42 
43 #include <machine/machdep.h>
44 #include <machine/vmparam.h>
45 #include <machine/vmm.h>
46 
47 #include <dev/vmm/vmm_dev.h>
48 #include <dev/vmm/vmm_mem.h>
49 
50 #include "io/vgic.h"
51 
52 const struct vmmdev_ioctl vmmdev_machdep_ioctls[] = {
53 	VMMDEV_IOCTL(VM_RUN, VMMDEV_IOCTL_LOCK_ONE_VCPU),
54 	VMMDEV_IOCTL(VM_INJECT_EXCEPTION, VMMDEV_IOCTL_LOCK_ONE_VCPU),
55 	VMMDEV_IOCTL(VM_GLA2GPA_NOFAULT, VMMDEV_IOCTL_LOCK_ONE_VCPU),
56 
57 	VMMDEV_IOCTL(VM_ATTACH_VGIC,
58 	    VMMDEV_IOCTL_XLOCK_MEMSEGS | VMMDEV_IOCTL_LOCK_ALL_VCPUS),
59 
60 	VMMDEV_IOCTL(VM_GET_VGIC_VERSION, 0),
61 	VMMDEV_IOCTL(VM_RAISE_MSI, 0),
62 	VMMDEV_IOCTL(VM_ASSERT_IRQ, 0),
63 	VMMDEV_IOCTL(VM_DEASSERT_IRQ, 0),
64 };
65 const size_t vmmdev_machdep_ioctl_count = nitems(vmmdev_machdep_ioctls);
66 
67 int
vmmdev_machdep_ioctl(struct vm * vm,struct vcpu * vcpu,u_long cmd,caddr_t data,int fflag,struct thread * td)68 vmmdev_machdep_ioctl(struct vm *vm, struct vcpu *vcpu, u_long cmd, caddr_t data,
69     int fflag, struct thread *td)
70 {
71 	int error;
72 
73 	error = 0;
74 	switch (cmd) {
75 	case VM_RUN: {
76 		struct vm_exit *vme;
77 		struct vm_run *vmrun;
78 
79 		vmrun = (struct vm_run *)data;
80 		vme = vm_exitinfo(vcpu);
81 
82 		error = vm_run(vcpu);
83 		if (error != 0)
84 			break;
85 
86 		error = copyout(vme, vmrun->vm_exit, sizeof(*vme));
87 		if (error != 0)
88 			break;
89 		break;
90 	}
91 	case VM_INJECT_EXCEPTION: {
92 		struct vm_exception *vmexc;
93 
94 		vmexc = (struct vm_exception *)data;
95 		error = vm_inject_exception(vcpu, vmexc->esr, vmexc->far);
96 		break;
97 	}
98 	case VM_GLA2GPA_NOFAULT: {
99 		struct vm_gla2gpa *gg;
100 
101 		gg = (struct vm_gla2gpa *)data;
102 		error = vm_gla2gpa_nofault(vcpu, &gg->paging, gg->gla,
103 		    gg->prot, &gg->gpa, &gg->fault);
104 		KASSERT(error == 0 || error == EFAULT,
105 		    ("%s: vm_gla2gpa unknown error %d", __func__, error));
106 		break;
107 	}
108 	case VM_GET_VGIC_VERSION: {
109 		struct vm_vgic_version *vgv;
110 
111 		vgv = (struct vm_vgic_version *)data;
112 		/* TODO: Query the vgic driver for this */
113 		vgv->version = 3;
114 		vgv->flags = 0;
115 		error = 0;
116 		break;
117 	}
118 	case VM_ATTACH_VGIC: {
119 		struct vm_vgic_descr *vgic;
120 
121 		vgic = (struct vm_vgic_descr *)data;
122 		error = vm_attach_vgic(vm, vgic);
123 		break;
124 	}
125 	case VM_RAISE_MSI: {
126 		struct vm_msi *vmsi;
127 
128 		vmsi = (struct vm_msi *)data;
129 		error = vm_raise_msi(vm, vmsi->msg, vmsi->addr, vmsi->bus,
130 		    vmsi->slot, vmsi->func);
131 		break;
132 	}
133 	case VM_ASSERT_IRQ: {
134 		struct vm_irq *vi;
135 
136 		vi = (struct vm_irq *)data;
137 		error = vm_assert_irq(vm, vi->irq);
138 		break;
139 	}
140 	case VM_DEASSERT_IRQ: {
141 		struct vm_irq *vi;
142 
143 		vi = (struct vm_irq *)data;
144 		error = vm_deassert_irq(vm, vi->irq);
145 		break;
146 	}
147 	default:
148 		error = ENOTTY;
149 		break;
150 	}
151 
152 	return (error);
153 }
154