1/*
2 * Copyright (C) 2015 Mihai Carabas <mihai.carabas@gmail.com>
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 AUTHOR AND CONTRIBUTORS ``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 AUTHOR 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
27#ifndef	_VMM_DEV_H_
28#define	_VMM_DEV_H_
29
30#ifdef _KERNEL
31void	vmmdev_init(void);
32int	vmmdev_cleanup(void);
33#endif
34
35struct vm_memmap {
36	vm_paddr_t	gpa;
37	int		segid;		/* memory segment */
38	vm_ooffset_t	segoff;		/* offset into memory segment */
39	size_t		len;		/* mmap length */
40	int		prot;		/* RWX */
41	int		flags;
42};
43#define	VM_MEMMAP_F_WIRED	0x01
44
45struct vm_munmap {
46	vm_paddr_t	gpa;
47	size_t		len;
48};
49
50#define	VM_MEMSEG_NAME(m)	((m)->name[0] != '\0' ? (m)->name : NULL)
51struct vm_memseg {
52	int		segid;
53	size_t		len;
54	char		name[VM_MAX_SUFFIXLEN + 1];
55};
56
57struct vm_register {
58	int		cpuid;
59	int		regnum;		/* enum vm_reg_name */
60	uint64_t	regval;
61};
62
63struct vm_register_set {
64	int		cpuid;
65	unsigned int	count;
66	const int	*regnums;	/* enum vm_reg_name */
67	uint64_t	*regvals;
68};
69
70struct vm_run {
71	int		cpuid;
72	cpuset_t	*cpuset;	/* CPU set storage */
73	size_t		cpusetsize;
74	struct vm_exit	*vm_exit;
75};
76
77struct vm_exception {
78	int		cpuid;
79	uint64_t	esr;
80	uint64_t	far;
81};
82
83struct vm_msi {
84	uint64_t	msg;
85	uint64_t	addr;
86	int		bus;
87	int		slot;
88	int		func;
89};
90
91struct vm_capability {
92	int		cpuid;
93	enum vm_cap_type captype;
94	int		capval;
95	int		allcpus;
96};
97
98#define	MAX_VM_STATS	64
99struct vm_stats {
100	int		cpuid;				/* in */
101	int		index;				/* in */
102	int		num_entries;			/* out */
103	struct timeval	tv;
104	uint64_t	statbuf[MAX_VM_STATS];
105};
106struct vm_stat_desc {
107	int		index;				/* in */
108	char		desc[128];			/* out */
109};
110
111struct vm_suspend {
112	enum vm_suspend_how how;
113};
114
115struct vm_gla2gpa {
116	int		vcpuid;		/* inputs */
117	int 		prot;		/* PROT_READ or PROT_WRITE */
118	uint64_t	gla;
119	struct vm_guest_paging paging;
120	int		fault;		/* outputs */
121	uint64_t	gpa;
122};
123
124struct vm_activate_cpu {
125	int		vcpuid;
126};
127
128struct vm_cpuset {
129	int		which;
130	int		cpusetsize;
131	cpuset_t	*cpus;
132};
133#define	VM_ACTIVE_CPUS		0
134#define	VM_SUSPENDED_CPUS	1
135#define	VM_DEBUG_CPUS		2
136
137struct vm_vgic_version {
138	u_int version;
139	u_int flags;
140};
141
142struct vm_vgic_descr {
143	struct vm_vgic_version ver;
144	union {
145		struct {
146			uint64_t dist_start;
147			uint64_t dist_size;
148			uint64_t redist_start;
149			uint64_t redist_size;
150		} v3_regs;
151	};
152};
153
154struct vm_irq {
155	uint32_t irq;
156};
157
158struct vm_cpu_topology {
159	uint16_t	sockets;
160	uint16_t	cores;
161	uint16_t	threads;
162	uint16_t	maxcpus;
163};
164
165enum {
166	/* general routines */
167	IOCNUM_ABIVERS = 0,
168	IOCNUM_RUN = 1,
169	IOCNUM_SET_CAPABILITY = 2,
170	IOCNUM_GET_CAPABILITY = 3,
171	IOCNUM_SUSPEND = 4,
172	IOCNUM_REINIT = 5,
173
174	/* memory apis */
175	IOCNUM_GET_GPA_PMAP = 12,
176	IOCNUM_GLA2GPA_NOFAULT = 13,
177	IOCNUM_ALLOC_MEMSEG = 14,
178	IOCNUM_GET_MEMSEG = 15,
179	IOCNUM_MMAP_MEMSEG = 16,
180	IOCNUM_MMAP_GETNEXT = 17,
181	IOCNUM_MUNMAP_MEMSEG = 18,
182
183	/* register/state accessors */
184	IOCNUM_SET_REGISTER = 20,
185	IOCNUM_GET_REGISTER = 21,
186	IOCNUM_SET_REGISTER_SET = 24,
187	IOCNUM_GET_REGISTER_SET = 25,
188
189	/* statistics */
190	IOCNUM_VM_STATS = 50,
191	IOCNUM_VM_STAT_DESC = 51,
192
193	/* CPU Topology */
194	IOCNUM_SET_TOPOLOGY = 63,
195	IOCNUM_GET_TOPOLOGY = 64,
196
197	/* interrupt injection */
198	IOCNUM_ASSERT_IRQ = 80,
199	IOCNUM_DEASSERT_IRQ = 81,
200	IOCNUM_RAISE_MSI = 82,
201	IOCNUM_INJECT_EXCEPTION = 83,
202
203	/* vm_cpuset */
204	IOCNUM_ACTIVATE_CPU = 90,
205	IOCNUM_GET_CPUSET = 91,
206	IOCNUM_SUSPEND_CPU = 92,
207	IOCNUM_RESUME_CPU = 93,
208
209	/* vm_attach_vgic */
210	IOCNUM_GET_VGIC_VERSION = 110,
211	IOCNUM_ATTACH_VGIC = 111,
212};
213
214#define	VM_RUN		\
215	_IOWR('v', IOCNUM_RUN, struct vm_run)
216#define	VM_SUSPEND	\
217	_IOW('v', IOCNUM_SUSPEND, struct vm_suspend)
218#define	VM_REINIT	\
219	_IO('v', IOCNUM_REINIT)
220#define	VM_ALLOC_MEMSEG	\
221	_IOW('v', IOCNUM_ALLOC_MEMSEG, struct vm_memseg)
222#define	VM_GET_MEMSEG	\
223	_IOWR('v', IOCNUM_GET_MEMSEG, struct vm_memseg)
224#define	VM_MMAP_MEMSEG	\
225	_IOW('v', IOCNUM_MMAP_MEMSEG, struct vm_memmap)
226#define	VM_MMAP_GETNEXT	\
227	_IOWR('v', IOCNUM_MMAP_GETNEXT, struct vm_memmap)
228#define	VM_MUNMAP_MEMSEG	\
229	_IOW('v', IOCNUM_MUNMAP_MEMSEG, struct vm_munmap)
230#define	VM_SET_REGISTER \
231	_IOW('v', IOCNUM_SET_REGISTER, struct vm_register)
232#define	VM_GET_REGISTER \
233	_IOWR('v', IOCNUM_GET_REGISTER, struct vm_register)
234#define	VM_SET_REGISTER_SET \
235	_IOW('v', IOCNUM_SET_REGISTER_SET, struct vm_register_set)
236#define	VM_GET_REGISTER_SET \
237	_IOWR('v', IOCNUM_GET_REGISTER_SET, struct vm_register_set)
238#define	VM_SET_CAPABILITY \
239	_IOW('v', IOCNUM_SET_CAPABILITY, struct vm_capability)
240#define	VM_GET_CAPABILITY \
241	_IOWR('v', IOCNUM_GET_CAPABILITY, struct vm_capability)
242#define	VM_STATS \
243	_IOWR('v', IOCNUM_VM_STATS, struct vm_stats)
244#define	VM_STAT_DESC \
245	_IOWR('v', IOCNUM_VM_STAT_DESC, struct vm_stat_desc)
246#define VM_ASSERT_IRQ \
247	_IOW('v', IOCNUM_ASSERT_IRQ, struct vm_irq)
248#define VM_DEASSERT_IRQ \
249	_IOW('v', IOCNUM_DEASSERT_IRQ, struct vm_irq)
250#define VM_RAISE_MSI \
251	_IOW('v', IOCNUM_RAISE_MSI, struct vm_msi)
252#define	VM_INJECT_EXCEPTION	\
253	_IOW('v', IOCNUM_INJECT_EXCEPTION, struct vm_exception)
254#define VM_SET_TOPOLOGY \
255	_IOW('v', IOCNUM_SET_TOPOLOGY, struct vm_cpu_topology)
256#define VM_GET_TOPOLOGY \
257	_IOR('v', IOCNUM_GET_TOPOLOGY, struct vm_cpu_topology)
258#define	VM_GLA2GPA_NOFAULT \
259	_IOWR('v', IOCNUM_GLA2GPA_NOFAULT, struct vm_gla2gpa)
260#define	VM_ACTIVATE_CPU	\
261	_IOW('v', IOCNUM_ACTIVATE_CPU, struct vm_activate_cpu)
262#define	VM_GET_CPUS	\
263	_IOW('v', IOCNUM_GET_CPUSET, struct vm_cpuset)
264#define	VM_SUSPEND_CPU \
265	_IOW('v', IOCNUM_SUSPEND_CPU, struct vm_activate_cpu)
266#define	VM_RESUME_CPU \
267	_IOW('v', IOCNUM_RESUME_CPU, struct vm_activate_cpu)
268#define	VM_GET_VGIC_VERSION	\
269	_IOR('v', IOCNUM_GET_VGIC_VERSION, struct vm_vgic_version)
270#define	VM_ATTACH_VGIC	\
271	_IOW('v', IOCNUM_ATTACH_VGIC, struct vm_vgic_descr)
272#endif
273