• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Copyright (c) 2023 MediaTek Inc.
4  */
5 
6 /**
7  * DOC: UAPI of GenieZone Hypervisor
8  *
9  * This file declares common data structure shared among user space,
10  * kernel space, and GenieZone hypervisor.
11  */
12 #ifndef __GZVM_H__
13 #define __GZVM_H__
14 
15 #include <linux/const.h>
16 #include <linux/types.h>
17 #include <linux/ioctl.h>
18 
19 #define GZVM_CAP_VM_GPA_SIZE	0xa5
20 #define GZVM_CAP_PROTECTED_VM	0xffbadab1
21 /* query hypervisor supported block-based demand page */
22 #define GZVM_CAP_BLOCK_BASED_DEMAND_PAGING	0x9201
23 #define GZVM_CAP_ENABLE_DEMAND_PAGING	0x9202
24 #define GZVM_CAP_ENABLE_IDLE		0x9203
25 
26 /* sub-commands put in args[0] for GZVM_CAP_PROTECTED_VM */
27 #define GZVM_CAP_PVM_SET_PVMFW_GPA		0
28 #define GZVM_CAP_PVM_GET_PVMFW_SIZE		1
29 /* GZVM_CAP_PVM_SET_PROTECTED_VM only sets protected but not load pvmfw */
30 #define GZVM_CAP_PVM_SET_PROTECTED_VM		2
31 
32 /*
33  * Architecture specific registers are to be defined and ORed with
34  * the arch identifier.
35  */
36 #define GZVM_REG_ARCH_ARM64	FIELD_PREP(GENMASK_ULL(63, 56), 0x60)
37 #define GZVM_REG_ARCH_MASK	FIELD_PREP(GENMASK_ULL(63, 56), 0xff)
38 /*
39  * Reg size = BIT((reg.id & GZVM_REG_SIZE_MASK) >> GZVM_REG_SIZE_SHIFT) bytes
40  */
41 #define GZVM_REG_SIZE_SHIFT	52
42 #define GZVM_REG_SIZE_MASK	FIELD_PREP(GENMASK_ULL(63, 48), 0x00f0)
43 
44 #define GZVM_REG_SIZE_U8	FIELD_PREP(GENMASK_ULL(63, 48), 0x0000)
45 #define GZVM_REG_SIZE_U16	FIELD_PREP(GENMASK_ULL(63, 48), 0x0010)
46 #define GZVM_REG_SIZE_U32	FIELD_PREP(GENMASK_ULL(63, 48), 0x0020)
47 #define GZVM_REG_SIZE_U64	FIELD_PREP(GENMASK_ULL(63, 48), 0x0030)
48 #define GZVM_REG_SIZE_U128	FIELD_PREP(GENMASK_ULL(63, 48), 0x0040)
49 #define GZVM_REG_SIZE_U256	FIELD_PREP(GENMASK_ULL(63, 48), 0x0050)
50 #define GZVM_REG_SIZE_U512	FIELD_PREP(GENMASK_ULL(63, 48), 0x0060)
51 #define GZVM_REG_SIZE_U1024	FIELD_PREP(GENMASK_ULL(63, 48), 0x0070)
52 #define GZVM_REG_SIZE_U2048	FIELD_PREP(GENMASK_ULL(63, 48), 0x0080)
53 
54 #define GZVM_REG_TYPE_GENERAL2	FIELD_PREP(GENMASK(23, 16), 0x10)
55 
56 /* GZVM ioctls */
57 #define GZVM_IOC_MAGIC			0x92	/* gz */
58 
59 /* ioctls for /dev/gzvm fds */
60 #define GZVM_CREATE_VM             _IO(GZVM_IOC_MAGIC,   0x01) /* Returns a Geniezone VM fd */
61 
62 /*
63  * Check if the given capability is supported or not.
64  * The argument is capability. Ex. GZVM_CAP_PROTECTED_VM or GZVM_CAP_VM_GPA_SIZE
65  * return is 0 (supported, no error)
66  * return is -EOPNOTSUPP (unsupported)
67  * return is -EFAULT (failed to get the argument from userspace)
68  */
69 #define GZVM_CHECK_EXTENSION       _IO(GZVM_IOC_MAGIC,   0x03)
70 
71 /* ioctls for VM fds */
72 /* for GZVM_SET_MEMORY_REGION */
73 struct gzvm_memory_region {
74 	__u32 slot;
75 	__u32 flags;
76 	__u64 guest_phys_addr;
77 	__u64 memory_size; /* bytes */
78 };
79 
80 #define GZVM_SET_MEMORY_REGION     _IOW(GZVM_IOC_MAGIC,  0x40, \
81 					struct gzvm_memory_region)
82 /*
83  * GZVM_CREATE_VCPU receives as a parameter the vcpu slot,
84  * and returns a vcpu fd.
85  */
86 #define GZVM_CREATE_VCPU           _IO(GZVM_IOC_MAGIC,   0x41)
87 
88 /**
89  * struct gzvm_userspace_memory_region: gzvm userspace memory region descriptor
90  * @slot: memory slot
91  * @flags: describe the usage of userspace memory region
92  * @guest_phys_addr: guest vm's physical address
93  * @memory_size: memory size in bytes
94  * @userspace_addr: start of the userspace allocated memory
95  */
96 struct gzvm_userspace_memory_region {
97 	__u32 slot;
98 	__u32 flags;
99 	__u64 guest_phys_addr;
100 	__u64 memory_size;
101 	__u64 userspace_addr;
102 };
103 
104 #define GZVM_SET_USER_MEMORY_REGION _IOW(GZVM_IOC_MAGIC, 0x46, \
105 					 struct gzvm_userspace_memory_region)
106 
107 /* for GZVM_IRQ_LINE, irq field index values */
108 #define GZVM_IRQ_VCPU_MASK		0xff
109 #define GZVM_IRQ_LINE_TYPE		GENMASK(27, 24)
110 #define GZVM_IRQ_LINE_VCPU		GENMASK(23, 16)
111 #define GZVM_IRQ_LINE_VCPU2		GENMASK(31, 28)
112 #define GZVM_IRQ_LINE_NUM		GENMASK(15, 0)
113 
114 /* irq_type field */
115 #define GZVM_IRQ_TYPE_CPU		0
116 #define GZVM_IRQ_TYPE_SPI		1
117 #define GZVM_IRQ_TYPE_PPI		2
118 
119 /* out-of-kernel GIC cpu interrupt injection irq_number field */
120 #define GZVM_IRQ_CPU_IRQ		0
121 #define GZVM_IRQ_CPU_FIQ		1
122 
123 struct gzvm_irq_level {
124 	union {
125 		__u32 irq;
126 		__s32 status;
127 	};
128 	__u32 level;
129 };
130 
131 #define GZVM_IRQ_LINE              _IOW(GZVM_IOC_MAGIC,  0x61, \
132 					struct gzvm_irq_level)
133 
134 enum gzvm_device_type {
135 	GZVM_DEV_TYPE_ARM_VGIC_V3_DIST = 0,
136 	GZVM_DEV_TYPE_ARM_VGIC_V3_REDIST = 1,
137 	GZVM_DEV_TYPE_MAX,
138 };
139 
140 /**
141  * struct gzvm_create_device: For GZVM_CREATE_DEVICE.
142  * @dev_type: Device type.
143  * @id: Device id.
144  * @flags: Bypass to hypervisor to handle them and these are flags of virtual
145  *         devices.
146  * @dev_addr: Device ipa address in VM's view.
147  * @dev_reg_size: Device register range size.
148  * @attr_addr: If user -> kernel, this is user virtual address of device
149  *             specific attributes (if needed). If kernel->hypervisor,
150  *             this is ipa.
151  * @attr_size: This attr_size is the buffer size in bytes of each attribute
152  *             needed from various devices. The attribute here refers to the
153  *             additional data passed from VMM(e.g. Crosvm) to GenieZone
154  *             hypervisor when virtual devices were to be created. Thus,
155  *             we need attr_addr and attr_size in the gzvm_create_device
156  *             structure to keep track of the attribute mentioned.
157  *
158  * Store information needed to create device.
159  */
160 struct gzvm_create_device {
161 	__u32 dev_type;
162 	__u32 id;
163 	__u64 flags;
164 	__u64 dev_addr;
165 	__u64 dev_reg_size;
166 	__u64 attr_addr;
167 	__u64 attr_size;
168 };
169 
170 #define GZVM_CREATE_DEVICE	   _IOWR(GZVM_IOC_MAGIC,  0xe0, \
171 					struct gzvm_create_device)
172 
173 /*
174  * ioctls for vcpu fds
175  */
176 #define GZVM_RUN                   _IO(GZVM_IOC_MAGIC,   0x80)
177 
178 /* VM exit reason */
179 enum {
180 	GZVM_EXIT_UNKNOWN = 0x92920000,
181 	GZVM_EXIT_MMIO = 0x92920001,
182 	GZVM_EXIT_HYPERCALL = 0x92920002,
183 	GZVM_EXIT_IRQ = 0x92920003,
184 	GZVM_EXIT_EXCEPTION = 0x92920004,
185 	GZVM_EXIT_DEBUG = 0x92920005,
186 	GZVM_EXIT_FAIL_ENTRY = 0x92920006,
187 	GZVM_EXIT_INTERNAL_ERROR = 0x92920007,
188 	GZVM_EXIT_SYSTEM_EVENT = 0x92920008,
189 	GZVM_EXIT_SHUTDOWN = 0x92920009,
190 	GZVM_EXIT_GZ = 0x9292000a,
191 	GZVM_EXIT_IDLE = 0x9292000b,
192 	GZVM_EXIT_IPI = 0x9292000d,
193 };
194 
195 /* exception definitions of GZVM_EXIT_EXCEPTION */
196 enum {
197 	GZVM_EXCEPTION_UNKNOWN = 0x0,
198 	GZVM_EXCEPTION_PAGE_FAULT = 0x1,
199 };
200 
201 /* hypercall definitions of GZVM_EXIT_HYPERCALL */
202 enum {
203 	GZVM_HVC_PTP = 0x86000001,
204 	GZVM_HVC_MEM_RELINQUISH = 0xc6000009,
205 };
206 
207 /**
208  * struct gzvm_vcpu_run: Same purpose as kvm_run, this struct is
209  *			 shared between userspace, kernel and
210  *			 GenieZone hypervisor
211  * @exit_reason: The reason why gzvm_vcpu_run has stopped running the vCPU
212  * @immediate_exit: Polled when the vcpu is scheduled.
213  *                  If set, immediately returns -EINTR
214  * @padding1: Reserved for future-proof and must be zero filled
215  * @mmio: The nested struct in anonymous union. Handle mmio in host side
216  * @fail_entry: The nested struct in anonymous union.
217  *              Handle invalid entry address at the first run
218  * @exception: The nested struct in anonymous union.
219  *             Handle exception occurred in VM
220  * @hypercall: The nested struct in anonymous union.
221  *             Some hypercalls issued from VM must be handled
222  * @internal: The nested struct in anonymous union. The errors from hypervisor
223  * @system_event: The nested struct in anonymous union.
224  *                VM's PSCI must be handled by host
225  * @padding: Fix it to a reasonable size future-proof for keeping the same
226  *           struct size when adding new variables in the union is needed
227  *
228  * Keep identical layout between the 3 modules
229  */
230 struct gzvm_vcpu_run {
231 	/* to userspace */
232 	__u32 exit_reason;
233 	__u8 immediate_exit;
234 	__u8 padding1[3];
235 	/* union structure of collection of guest exit reason */
236 	union {
237 		/* GZVM_EXIT_MMIO */
238 		struct {
239 			/* From FAR_EL2 */
240 			/* The address guest tries to access */
241 			__u64 phys_addr;
242 			/* The value to be written (is_write is 1) or
243 			 * be filled by user for reads (is_write is 0)
244 			 */
245 			__u8 data[8];
246 			/* From ESR_EL2 as */
247 			/* The size of written data.
248 			 * Only the first `size` bytes of `data` are handled
249 			 */
250 			__u64 size;
251 			/* From ESR_EL2 */
252 			/* The register number where the data is stored */
253 			__u32 reg_nr;
254 			/* From ESR_EL2 */
255 			/* 1 for VM to perform a write or 0 for VM to perform a read */
256 			__u8 is_write;
257 		} mmio;
258 		/* GZVM_EXIT_FAIL_ENTRY */
259 		struct {
260 			/* The reason codes about hardware entry failure */
261 			__u64 hardware_entry_failure_reason;
262 			/* The current processor number via smp_processor_id() */
263 			__u32 cpu;
264 		} fail_entry;
265 		/* GZVM_EXIT_EXCEPTION */
266 		struct {
267 			/* Which exception vector */
268 			__u32 exception;
269 			/* Exception error codes */
270 			__u32 error_code;
271 			/* Fault GPA (guest physical address or IPA in ARM) */
272 			__u64 fault_gpa;
273 			/* Future-proof reservation and reset to zero in hypervisor.
274 			 * Fill up to the union size, 256 bytes.
275 			 */
276 			__u64 reserved[30];
277 		} exception;
278 		/* GZVM_EXIT_HYPERCALL */
279 		struct {
280 			/* The hypercall's arguments */
281 			__u64 args[8];	/* in-out */
282 		} hypercall;
283 		/* GZVM_EXIT_INTERNAL_ERROR */
284 		struct {
285 			/* The errors codes about GZVM_EXIT_INTERNAL_ERROR */
286 			__u32 suberror;
287 			/* The number of elements used in data[] */
288 			__u32 ndata;
289 			/* Keep the detailed information about GZVM_EXIT_SYSTEM_EVENT */
290 			__u64 data[16];
291 		} internal;
292 		/* GZVM_EXIT_SYSTEM_EVENT */
293 		struct {
294 #define GZVM_SYSTEM_EVENT_SHUTDOWN       1
295 #define GZVM_SYSTEM_EVENT_RESET          2
296 #define GZVM_SYSTEM_EVENT_CRASH          3
297 #define GZVM_SYSTEM_EVENT_WAKEUP         4
298 #define GZVM_SYSTEM_EVENT_SUSPEND        5
299 #define GZVM_SYSTEM_EVENT_SEV_TERM       6
300 #define GZVM_SYSTEM_EVENT_S2IDLE         7
301 			/* System event type.
302 			 * Ex. GZVM_SYSTEM_EVENT_SHUTDOWN or GZVM_SYSTEM_EVENT_RESET...etc.
303 			 */
304 			__u32 type;
305 			/* The number of elements used in data[] */
306 			__u32 ndata;
307 			/* Keep the detailed information about GZVM_EXIT_SYSTEM_EVENT */
308 			__u64 data[16];
309 		} system_event;
310 		char padding[256];
311 	};
312 };
313 
314 /**
315  * struct gzvm_enable_cap: The `capability support` on GenieZone hypervisor
316  * @cap: `GZVM_CAP_ARM_PROTECTED_VM` or `GZVM_CAP_ARM_VM_IPA_SIZE`
317  * @args: x3-x7 registers can be used for additional args
318  */
319 struct gzvm_enable_cap {
320 	__u64 cap;
321 	__u64 args[5];
322 };
323 
324 #define GZVM_ENABLE_CAP            _IOW(GZVM_IOC_MAGIC,  0xa3, \
325 					struct gzvm_enable_cap)
326 
327 /* for GZVM_GET/SET_ONE_REG */
328 struct gzvm_one_reg {
329 	__u64 id;
330 	__u64 addr;
331 };
332 
333 #define GZVM_GET_ONE_REG	   _IOW(GZVM_IOC_MAGIC,  0xab, \
334 					struct gzvm_one_reg)
335 #define GZVM_SET_ONE_REG	   _IOW(GZVM_IOC_MAGIC,  0xac, \
336 					struct gzvm_one_reg)
337 
338 #define GZVM_REG_GENERIC	   0x0000000000000000ULL
339 
340 #define GZVM_IRQFD_FLAG_DEASSIGN	BIT(0)
341 /*
342  * GZVM_IRQFD_FLAG_RESAMPLE indicates resamplefd is valid and specifies
343  * the irqfd to operate in resampling mode for level triggered interrupt
344  * emulation.
345  */
346 #define GZVM_IRQFD_FLAG_RESAMPLE	BIT(1)
347 
348 /**
349  * struct gzvm_irqfd: gzvm irqfd descriptor
350  * @fd: File descriptor.
351  * @gsi: Used for level IRQ fast-path.
352  * @flags: FLAG_DEASSIGN or FLAG_RESAMPLE.
353  * @resamplefd: The file descriptor of the resampler.
354  * @pad: Reserved for future-proof.
355  */
356 struct gzvm_irqfd {
357 	__u32 fd;
358 	__u32 gsi;
359 	__u32 flags;
360 	__u32 resamplefd;
361 	__u8  pad[16];
362 };
363 
364 #define GZVM_IRQFD	_IOW(GZVM_IOC_MAGIC, 0x76, struct gzvm_irqfd)
365 
366 enum {
367 	gzvm_ioeventfd_flag_nr_datamatch = 0,
368 	gzvm_ioeventfd_flag_nr_pio = 1,
369 	gzvm_ioeventfd_flag_nr_deassign = 2,
370 	gzvm_ioeventfd_flag_nr_max,
371 };
372 
373 #define GZVM_IOEVENTFD_FLAG_DATAMATCH	(1 << gzvm_ioeventfd_flag_nr_datamatch)
374 #define GZVM_IOEVENTFD_FLAG_PIO		(1 << gzvm_ioeventfd_flag_nr_pio)
375 #define GZVM_IOEVENTFD_FLAG_DEASSIGN	(1 << gzvm_ioeventfd_flag_nr_deassign)
376 #define GZVM_IOEVENTFD_VALID_FLAG_MASK	((1 << gzvm_ioeventfd_flag_nr_max) - 1)
377 
378 struct gzvm_ioeventfd {
379 	__u64 datamatch;
380 	/* private: legal pio/mmio address */
381 	__u64 addr;
382 	/* private: 1, 2, 4, or 8 bytes; or 0 to ignore length */
383 	__u32 len;
384 	__s32 fd;
385 	__u32 flags;
386 	__u8  pad[36];
387 };
388 
389 #define GZVM_IOEVENTFD	_IOW(GZVM_IOC_MAGIC, 0x79, struct gzvm_ioeventfd)
390 
391 /**
392  * struct gzvm_dtb_config: store address and size of dtb passed from userspace
393  *
394  * @dtb_addr: dtb address set by VMM (guset memory)
395  * @dtb_size: dtb size
396  */
397 struct gzvm_dtb_config {
398 	__u64 dtb_addr;
399 	__u64 dtb_size;
400 };
401 
402 #define GZVM_SET_DTB_CONFIG       _IOW(GZVM_IOC_MAGIC, 0xff, \
403 				       struct gzvm_dtb_config)
404 
405 #endif /* __GZVM_H__ */
406