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