gunyah.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #ifndef _UAPI_LINUX_GUNYAH_H
  6. #define _UAPI_LINUX_GUNYAH_H
  7. /*
  8. * Userspace interface for /dev/gunyah - gunyah based virtual machine
  9. */
  10. #include <linux/types.h>
  11. #include <linux/ioctl.h>
  12. #define GH_IOCTL_TYPE 'G'
  13. /*
  14. * ioctls for /dev/gunyah fds:
  15. */
  16. #define GH_CREATE_VM _IO(GH_IOCTL_TYPE, 0x0) /* Returns a Gunyah VM fd */
  17. /*
  18. * ioctls for VM fds
  19. */
  20. /**
  21. * enum gh_mem_flags - Possible flags on &struct gh_userspace_memory_region
  22. * @GH_MEM_ALLOW_READ: Allow guest to read the memory
  23. * @GH_MEM_ALLOW_WRITE: Allow guest to write to the memory
  24. * @GH_MEM_ALLOW_EXEC: Allow guest to execute instructions in the memory
  25. */
  26. enum gh_mem_flags {
  27. GH_MEM_ALLOW_READ = 1UL << 0,
  28. GH_MEM_ALLOW_WRITE = 1UL << 1,
  29. GH_MEM_ALLOW_EXEC = 1UL << 2,
  30. };
  31. /**
  32. * struct gh_userspace_memory_region - Userspace memory descripion for GH_VM_SET_USER_MEM_REGION
  33. * @label: Identifer to the region which is unique to the VM.
  34. * @flags: Flags for memory parcel behavior. See &enum gh_mem_flags.
  35. * @guest_phys_addr: Location of the memory region in guest's memory space (page-aligned)
  36. * @memory_size: Size of the region (page-aligned)
  37. * @userspace_addr: Location of the memory region in caller (userspace)'s memory
  38. *
  39. * See Documentation/virt/gunyah/vm-manager.rst for further details.
  40. */
  41. struct gh_userspace_memory_region {
  42. __u32 label;
  43. __u32 flags;
  44. __u64 guest_phys_addr;
  45. __u64 memory_size;
  46. __u64 userspace_addr;
  47. };
  48. #define GH_VM_SET_USER_MEM_REGION _IOW(GH_IOCTL_TYPE, 0x1, \
  49. struct gh_userspace_memory_region)
  50. /**
  51. * struct gh_vm_dtb_config - Set the location of the VM's devicetree blob
  52. * @guest_phys_addr: Address of the VM's devicetree in guest memory.
  53. * @size: Maximum size of the devicetree including space for overlays.
  54. * Resource manager applies an overlay to the DTB and dtb_size should
  55. * include room for the overlay. A page of memory is typicaly plenty.
  56. */
  57. struct gh_vm_dtb_config {
  58. __u64 guest_phys_addr;
  59. __u64 size;
  60. };
  61. #define GH_VM_SET_DTB_CONFIG _IOW(GH_IOCTL_TYPE, 0x2, struct gh_vm_dtb_config)
  62. #define GH_VM_START _IO(GH_IOCTL_TYPE, 0x3)
  63. /**
  64. * enum gh_fn_type - Valid types of Gunyah VM functions
  65. * @GH_FN_VCPU: create a vCPU instance to control a vCPU
  66. * &struct gh_fn_desc.arg is a pointer to &struct gh_fn_vcpu_arg
  67. * Return: file descriptor to manipulate the vcpu.
  68. * @GH_FN_IRQFD: register eventfd to assert a Gunyah doorbell
  69. * &struct gh_fn_desc.arg is a pointer to &struct gh_fn_irqfd_arg
  70. * @GH_FN_IOEVENTFD: register ioeventfd to trigger when VM faults on parameter
  71. * &struct gh_fn_desc.arg is a pointer to &struct gh_fn_ioeventfd_arg
  72. */
  73. enum gh_fn_type {
  74. GH_FN_VCPU = 1,
  75. GH_FN_IRQFD,
  76. GH_FN_IOEVENTFD,
  77. };
  78. #define GH_FN_MAX_ARG_SIZE 256
  79. /**
  80. * struct gh_fn_vcpu_arg - Arguments to create a vCPU.
  81. * @id: vcpu id
  82. *
  83. * Create this function with &GH_VM_ADD_FUNCTION using type &GH_FN_VCPU.
  84. *
  85. * The vcpu type will register with the VM Manager to expect to control
  86. * vCPU number `vcpu_id`. It returns a file descriptor allowing interaction with
  87. * the vCPU. See the Gunyah vCPU API description sections for interacting with
  88. * the Gunyah vCPU file descriptors.
  89. */
  90. struct gh_fn_vcpu_arg {
  91. __u32 id;
  92. };
  93. /**
  94. * enum gh_irqfd_flags - flags for use in gh_fn_irqfd_arg
  95. * @GH_IRQFD_FLAGS_LEVEL: make the interrupt operate like a level triggered
  96. * interrupt on guest side. Triggering IRQFD before
  97. * guest handles the interrupt causes interrupt to
  98. * stay asserted.
  99. */
  100. enum gh_irqfd_flags {
  101. GH_IRQFD_FLAGS_LEVEL = 1UL << 0,
  102. };
  103. /**
  104. * struct gh_fn_irqfd_arg - Arguments to create an irqfd function.
  105. *
  106. * Create this function with &GH_VM_ADD_FUNCTION using type &GH_FN_IRQFD.
  107. *
  108. * Allows setting an eventfd to directly trigger a guest interrupt.
  109. * irqfd.fd specifies the file descriptor to use as the eventfd.
  110. * irqfd.label corresponds to the doorbell label used in the guest VM's devicetree.
  111. *
  112. * @fd: an eventfd which when written to will raise a doorbell
  113. * @label: Label of the doorbell created on the guest VM
  114. * @flags: see &enum gh_irqfd_flags
  115. * @padding: padding bytes
  116. */
  117. struct gh_fn_irqfd_arg {
  118. __u32 fd;
  119. __u32 label;
  120. __u32 flags;
  121. __u32 padding;
  122. };
  123. /**
  124. * enum gh_ioeventfd_flags - flags for use in gh_fn_ioeventfd_arg
  125. * @GH_IOEVENTFD_FLAGS_DATAMATCH: the event will be signaled only if the
  126. * written value to the registered address is
  127. * equal to &struct gh_fn_ioeventfd_arg.datamatch
  128. */
  129. enum gh_ioeventfd_flags {
  130. GH_IOEVENTFD_FLAGS_DATAMATCH = 1UL << 0,
  131. };
  132. /**
  133. * struct gh_fn_ioeventfd_arg - Arguments to create an ioeventfd function
  134. * @datamatch: data used when GH_IOEVENTFD_DATAMATCH is set
  135. * @addr: Address in guest memory
  136. * @len: Length of access
  137. * @fd: When ioeventfd is matched, this eventfd is written
  138. * @flags: See &enum gh_ioeventfd_flags
  139. * @padding: padding bytes
  140. *
  141. * Create this function with &GH_VM_ADD_FUNCTION using type &GH_FN_IOEVENTFD.
  142. *
  143. * Attaches an ioeventfd to a legal mmio address within the guest. A guest write
  144. * in the registered address will signal the provided event instead of triggering
  145. * an exit on the GH_VCPU_RUN ioctl.
  146. */
  147. struct gh_fn_ioeventfd_arg {
  148. __u64 datamatch;
  149. __u64 addr; /* legal mmio address */
  150. __u32 len; /* 1, 2, 4, or 8 bytes; or 0 to ignore length */
  151. __s32 fd;
  152. __u32 flags;
  153. __u32 padding;
  154. };
  155. /**
  156. * struct gh_fn_desc - Arguments to create a VM function
  157. * @type: Type of the function. See &enum gh_fn_type.
  158. * @arg_size: Size of argument to pass to the function. arg_size <= GH_FN_MAX_ARG_SIZE
  159. * @arg: Pointer to argument given to the function. See &enum gh_fn_type for expected
  160. * arguments for a function type.
  161. */
  162. struct gh_fn_desc {
  163. __u32 type;
  164. __u32 arg_size;
  165. __u64 arg;
  166. };
  167. #define GH_VM_ADD_FUNCTION _IOW(GH_IOCTL_TYPE, 0x4, struct gh_fn_desc)
  168. #define GH_VM_REMOVE_FUNCTION _IOW(GH_IOCTL_TYPE, 0x7, struct gh_fn_desc)
  169. /*
  170. * ioctls for vCPU fds
  171. */
  172. /**
  173. * enum gh_vm_status - Stores status reason why VM is not runnable (exited).
  174. * @GH_VM_STATUS_LOAD_FAILED: VM didn't start because it couldn't be loaded.
  175. * @GH_VM_STATUS_EXITED: VM requested shutdown/reboot.
  176. * Use &struct gh_vm_exit_info.reason for further details.
  177. * @GH_VM_STATUS_CRASHED: VM state is unknown and has crashed.
  178. */
  179. enum gh_vm_status {
  180. GH_VM_STATUS_LOAD_FAILED = 1,
  181. GH_VM_STATUS_EXITED = 2,
  182. GH_VM_STATUS_CRASHED = 3,
  183. };
  184. /*
  185. * Gunyah presently sends max 4 bytes of exit_reason.
  186. * If that changes, this macro can be safely increased without breaking
  187. * userspace so long as struct gh_vcpu_run < PAGE_SIZE.
  188. */
  189. #define GH_VM_MAX_EXIT_REASON_SIZE 8u
  190. /**
  191. * struct gh_vm_exit_info - Reason for VM exit as reported by Gunyah
  192. * See Gunyah documentation for values.
  193. * @type: Describes how VM exited
  194. * @padding: padding bytes
  195. * @reason_size: Number of bytes valid for `reason`
  196. * @reason: See Gunyah documentation for interpretation. Note: these values are
  197. * not interpreted by Linux and need to be converted from little-endian
  198. * as applicable.
  199. */
  200. struct gh_vm_exit_info {
  201. __u16 type;
  202. __u16 padding;
  203. __u32 reason_size;
  204. __u8 reason[GH_VM_MAX_EXIT_REASON_SIZE];
  205. };
  206. /**
  207. * enum gh_vcpu_exit - Stores reason why &GH_VCPU_RUN ioctl recently exited with status 0
  208. * @GH_VCPU_EXIT_UNKNOWN: Not used, status != 0
  209. * @GH_VCPU_EXIT_MMIO: vCPU performed a read or write that could not be handled
  210. * by hypervisor or Linux. Use @struct gh_vcpu_run.mmio for
  211. * details of the read/write.
  212. * @GH_VCPU_EXIT_STATUS: vCPU not able to run because the VM has exited.
  213. * Use @struct gh_vcpu_run.status for why VM has exited.
  214. */
  215. enum gh_vcpu_exit {
  216. GH_VCPU_EXIT_UNKNOWN,
  217. GH_VCPU_EXIT_MMIO,
  218. GH_VCPU_EXIT_STATUS,
  219. };
  220. /**
  221. * struct gh_vcpu_run - Application code obtains a pointer to the gh_vcpu_run
  222. * structure by mmap()ing a vcpu fd.
  223. * @immediate_exit: polled when scheduling the vcpu. If set, immediately returns -EINTR.
  224. * @padding: padding bytes
  225. * @exit_reason: Set when GH_VCPU_RUN returns successfully and gives reason why
  226. * GH_VCPU_RUN has stopped running the vCPU. See &enum gh_vcpu_exit.
  227. * @mmio: Used when exit_reason == GH_VCPU_EXIT_MMIO
  228. * The guest has faulted on an memory-mapped I/O instruction that
  229. * couldn't be satisfied by gunyah.
  230. * @mmio.phys_addr: Address guest tried to access
  231. * @mmio.data: the value that was written if `is_write == 1`. Filled by
  232. * user for reads (`is_write == 0`).
  233. * @mmio.len: Length of write. Only the first `len` bytes of `data`
  234. * are considered by Gunyah.
  235. * @mmio.is_write: 1 if VM tried to perform a write, 0 for a read
  236. * @status: Used when exit_reason == GH_VCPU_EXIT_STATUS.
  237. * The guest VM is no longer runnable. This struct informs why.
  238. * @status.status: See &enum gh_vm_status for possible values
  239. * @status.exit_info: Used when status == GH_VM_STATUS_EXITED
  240. */
  241. struct gh_vcpu_run {
  242. /* in */
  243. __u8 immediate_exit;
  244. __u8 padding[7];
  245. /* out */
  246. __u32 exit_reason;
  247. union {
  248. struct {
  249. __u64 phys_addr;
  250. __u8 data[8];
  251. __u32 len;
  252. __u8 is_write;
  253. } mmio;
  254. struct {
  255. enum gh_vm_status status;
  256. struct gh_vm_exit_info exit_info;
  257. } status;
  258. };
  259. };
  260. #define GH_VCPU_RUN _IO(GH_IOCTL_TYPE, 0x5)
  261. #define GH_VCPU_MMAP_SIZE _IO(GH_IOCTL_TYPE, 0x6)
  262. /**
  263. * ANDROID: android14-6.1 unfortunately contains UAPI that won't be carried
  264. * in kernel.org. Expose orthogonal ioctls that will never conflict with
  265. * kernel.org for these UAPIs. See b/268234781.
  266. */
  267. #define GH_ANDROID_IOCTL_TYPE 'A'
  268. #define GH_VM_ANDROID_LEND_USER_MEM _IOW(GH_ANDROID_IOCTL_TYPE, 0x11, \
  269. struct gh_userspace_memory_region)
  270. struct gh_vm_firmware_config {
  271. __u64 guest_phys_addr;
  272. __u64 size;
  273. };
  274. #define GH_VM_ANDROID_SET_FW_CONFIG _IOW(GH_ANDROID_IOCTL_TYPE, 0x12, \
  275. struct gh_vm_firmware_config)
  276. #endif