gzvm.h 12 KB

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