vfio.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. VFIO - "Virtual Function I/O"[1]
  2. -------------------------------------------------------------------------------
  3. Many modern system now provide DMA and interrupt remapping facilities
  4. to help ensure I/O devices behave within the boundaries they've been
  5. allotted. This includes x86 hardware with AMD-Vi and Intel VT-d,
  6. POWER systems with Partitionable Endpoints (PEs) and embedded PowerPC
  7. systems such as Freescale PAMU. The VFIO driver is an IOMMU/device
  8. agnostic framework for exposing direct device access to userspace, in
  9. a secure, IOMMU protected environment. In other words, this allows
  10. safe[2], non-privileged, userspace drivers.
  11. Why do we want that? Virtual machines often make use of direct device
  12. access ("device assignment") when configured for the highest possible
  13. I/O performance. From a device and host perspective, this simply
  14. turns the VM into a userspace driver, with the benefits of
  15. significantly reduced latency, higher bandwidth, and direct use of
  16. bare-metal device drivers[3].
  17. Some applications, particularly in the high performance computing
  18. field, also benefit from low-overhead, direct device access from
  19. userspace. Examples include network adapters (often non-TCP/IP based)
  20. and compute accelerators. Prior to VFIO, these drivers had to either
  21. go through the full development cycle to become proper upstream
  22. driver, be maintained out of tree, or make use of the UIO framework,
  23. which has no notion of IOMMU protection, limited interrupt support,
  24. and requires root privileges to access things like PCI configuration
  25. space.
  26. The VFIO driver framework intends to unify these, replacing both the
  27. KVM PCI specific device assignment code as well as provide a more
  28. secure, more featureful userspace driver environment than UIO.
  29. Groups, Devices, and IOMMUs
  30. -------------------------------------------------------------------------------
  31. Devices are the main target of any I/O driver. Devices typically
  32. create a programming interface made up of I/O access, interrupts,
  33. and DMA. Without going into the details of each of these, DMA is
  34. by far the most critical aspect for maintaining a secure environment
  35. as allowing a device read-write access to system memory imposes the
  36. greatest risk to the overall system integrity.
  37. To help mitigate this risk, many modern IOMMUs now incorporate
  38. isolation properties into what was, in many cases, an interface only
  39. meant for translation (ie. solving the addressing problems of devices
  40. with limited address spaces). With this, devices can now be isolated
  41. from each other and from arbitrary memory access, thus allowing
  42. things like secure direct assignment of devices into virtual machines.
  43. This isolation is not always at the granularity of a single device
  44. though. Even when an IOMMU is capable of this, properties of devices,
  45. interconnects, and IOMMU topologies can each reduce this isolation.
  46. For instance, an individual device may be part of a larger multi-
  47. function enclosure. While the IOMMU may be able to distinguish
  48. between devices within the enclosure, the enclosure may not require
  49. transactions between devices to reach the IOMMU. Examples of this
  50. could be anything from a multi-function PCI device with backdoors
  51. between functions to a non-PCI-ACS (Access Control Services) capable
  52. bridge allowing redirection without reaching the IOMMU. Topology
  53. can also play a factor in terms of hiding devices. A PCIe-to-PCI
  54. bridge masks the devices behind it, making transaction appear as if
  55. from the bridge itself. Obviously IOMMU design plays a major factor
  56. as well.
  57. Therefore, while for the most part an IOMMU may have device level
  58. granularity, any system is susceptible to reduced granularity. The
  59. IOMMU API therefore supports a notion of IOMMU groups. A group is
  60. a set of devices which is isolatable from all other devices in the
  61. system. Groups are therefore the unit of ownership used by VFIO.
  62. While the group is the minimum granularity that must be used to
  63. ensure secure user access, it's not necessarily the preferred
  64. granularity. In IOMMUs which make use of page tables, it may be
  65. possible to share a set of page tables between different groups,
  66. reducing the overhead both to the platform (reduced TLB thrashing,
  67. reduced duplicate page tables), and to the user (programming only
  68. a single set of translations). For this reason, VFIO makes use of
  69. a container class, which may hold one or more groups. A container
  70. is created by simply opening the /dev/vfio/vfio character device.
  71. On its own, the container provides little functionality, with all
  72. but a couple version and extension query interfaces locked away.
  73. The user needs to add a group into the container for the next level
  74. of functionality. To do this, the user first needs to identify the
  75. group associated with the desired device. This can be done using
  76. the sysfs links described in the example below. By unbinding the
  77. device from the host driver and binding it to a VFIO driver, a new
  78. VFIO group will appear for the group as /dev/vfio/$GROUP, where
  79. $GROUP is the IOMMU group number of which the device is a member.
  80. If the IOMMU group contains multiple devices, each will need to
  81. be bound to a VFIO driver before operations on the VFIO group
  82. are allowed (it's also sufficient to only unbind the device from
  83. host drivers if a VFIO driver is unavailable; this will make the
  84. group available, but not that particular device). TBD - interface
  85. for disabling driver probing/locking a device.
  86. Once the group is ready, it may be added to the container by opening
  87. the VFIO group character device (/dev/vfio/$GROUP) and using the
  88. VFIO_GROUP_SET_CONTAINER ioctl, passing the file descriptor of the
  89. previously opened container file. If desired and if the IOMMU driver
  90. supports sharing the IOMMU context between groups, multiple groups may
  91. be set to the same container. If a group fails to set to a container
  92. with existing groups, a new empty container will need to be used
  93. instead.
  94. With a group (or groups) attached to a container, the remaining
  95. ioctls become available, enabling access to the VFIO IOMMU interfaces.
  96. Additionally, it now becomes possible to get file descriptors for each
  97. device within a group using an ioctl on the VFIO group file descriptor.
  98. The VFIO device API includes ioctls for describing the device, the I/O
  99. regions and their read/write/mmap offsets on the device descriptor, as
  100. well as mechanisms for describing and registering interrupt
  101. notifications.
  102. VFIO Usage Example
  103. -------------------------------------------------------------------------------
  104. Assume user wants to access PCI device 0000:06:0d.0
  105. $ readlink /sys/bus/pci/devices/0000:06:0d.0/iommu_group
  106. ../../../../kernel/iommu_groups/26
  107. This device is therefore in IOMMU group 26. This device is on the
  108. pci bus, therefore the user will make use of vfio-pci to manage the
  109. group:
  110. # modprobe vfio-pci
  111. Binding this device to the vfio-pci driver creates the VFIO group
  112. character devices for this group:
  113. $ lspci -n -s 0000:06:0d.0
  114. 06:0d.0 0401: 1102:0002 (rev 08)
  115. # echo 0000:06:0d.0 > /sys/bus/pci/devices/0000:06:0d.0/driver/unbind
  116. # echo 1102 0002 > /sys/bus/pci/drivers/vfio-pci/new_id
  117. Now we need to look at what other devices are in the group to free
  118. it for use by VFIO:
  119. $ ls -l /sys/bus/pci/devices/0000:06:0d.0/iommu_group/devices
  120. total 0
  121. lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:00:1e.0 ->
  122. ../../../../devices/pci0000:00/0000:00:1e.0
  123. lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:06:0d.0 ->
  124. ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.0
  125. lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:06:0d.1 ->
  126. ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.1
  127. This device is behind a PCIe-to-PCI bridge[4], therefore we also
  128. need to add device 0000:06:0d.1 to the group following the same
  129. procedure as above. Device 0000:00:1e.0 is a bridge that does
  130. not currently have a host driver, therefore it's not required to
  131. bind this device to the vfio-pci driver (vfio-pci does not currently
  132. support PCI bridges).
  133. The final step is to provide the user with access to the group if
  134. unprivileged operation is desired (note that /dev/vfio/vfio provides
  135. no capabilities on its own and is therefore expected to be set to
  136. mode 0666 by the system).
  137. # chown user:user /dev/vfio/26
  138. The user now has full access to all the devices and the iommu for this
  139. group and can access them as follows:
  140. int container, group, device, i;
  141. struct vfio_group_status group_status =
  142. { .argsz = sizeof(group_status) };
  143. struct vfio_iommu_x86_info iommu_info = { .argsz = sizeof(iommu_info) };
  144. struct vfio_iommu_x86_dma_map dma_map = { .argsz = sizeof(dma_map) };
  145. struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
  146. /* Create a new container */
  147. container = open("/dev/vfio/vfio, O_RDWR);
  148. if (ioctl(container, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
  149. /* Unknown API version */
  150. if (!ioctl(container, VFIO_CHECK_EXTENSION, VFIO_X86_IOMMU))
  151. /* Doesn't support the IOMMU driver we want. */
  152. /* Open the group */
  153. group = open("/dev/vfio/26", O_RDWR);
  154. /* Test the group is viable and available */
  155. ioctl(group, VFIO_GROUP_GET_STATUS, &group_status);
  156. if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
  157. /* Group is not viable (ie, not all devices bound for vfio) */
  158. /* Add the group to the container */
  159. ioctl(group, VFIO_GROUP_SET_CONTAINER, &container);
  160. /* Enable the IOMMU model we want */
  161. ioctl(container, VFIO_SET_IOMMU, VFIO_X86_IOMMU)
  162. /* Get addition IOMMU info */
  163. ioctl(container, VFIO_IOMMU_GET_INFO, &iommu_info);
  164. /* Allocate some space and setup a DMA mapping */
  165. dma_map.vaddr = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE,
  166. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  167. dma_map.size = 1024 * 1024;
  168. dma_map.iova = 0; /* 1MB starting at 0x0 from device view */
  169. dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
  170. ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map);
  171. /* Get a file descriptor for the device */
  172. device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:06:0d.0");
  173. /* Test and setup the device */
  174. ioctl(device, VFIO_DEVICE_GET_INFO, &device_info);
  175. for (i = 0; i < device_info.num_regions; i++) {
  176. struct vfio_region_info reg = { .argsz = sizeof(reg) };
  177. reg.index = i;
  178. ioctl(device, VFIO_DEVICE_GET_REGION_INFO, &reg);
  179. /* Setup mappings... read/write offsets, mmaps
  180. * For PCI devices, config space is a region */
  181. }
  182. for (i = 0; i < device_info.num_irqs; i++) {
  183. struct vfio_irq_info irq = { .argsz = sizeof(irq) };
  184. irq.index = i;
  185. ioctl(device, VFIO_DEVICE_GET_IRQ_INFO, &reg);
  186. /* Setup IRQs... eventfds, VFIO_DEVICE_SET_IRQS */
  187. }
  188. /* Gratuitous device reset and go... */
  189. ioctl(device, VFIO_DEVICE_RESET);
  190. VFIO User API
  191. -------------------------------------------------------------------------------
  192. Please see include/linux/vfio.h for complete API documentation.
  193. VFIO bus driver API
  194. -------------------------------------------------------------------------------
  195. VFIO bus drivers, such as vfio-pci make use of only a few interfaces
  196. into VFIO core. When devices are bound and unbound to the driver,
  197. the driver should call vfio_add_group_dev() and vfio_del_group_dev()
  198. respectively:
  199. extern int vfio_add_group_dev(struct iommu_group *iommu_group,
  200. struct device *dev,
  201. const struct vfio_device_ops *ops,
  202. void *device_data);
  203. extern void *vfio_del_group_dev(struct device *dev);
  204. vfio_add_group_dev() indicates to the core to begin tracking the
  205. specified iommu_group and register the specified dev as owned by
  206. a VFIO bus driver. The driver provides an ops structure for callbacks
  207. similar to a file operations structure:
  208. struct vfio_device_ops {
  209. int (*open)(void *device_data);
  210. void (*release)(void *device_data);
  211. ssize_t (*read)(void *device_data, char __user *buf,
  212. size_t count, loff_t *ppos);
  213. ssize_t (*write)(void *device_data, const char __user *buf,
  214. size_t size, loff_t *ppos);
  215. long (*ioctl)(void *device_data, unsigned int cmd,
  216. unsigned long arg);
  217. int (*mmap)(void *device_data, struct vm_area_struct *vma);
  218. };
  219. Each function is passed the device_data that was originally registered
  220. in the vfio_add_group_dev() call above. This allows the bus driver
  221. an easy place to store its opaque, private data. The open/release
  222. callbacks are issued when a new file descriptor is created for a
  223. device (via VFIO_GROUP_GET_DEVICE_FD). The ioctl interface provides
  224. a direct pass through for VFIO_DEVICE_* ioctls. The read/write/mmap
  225. interfaces implement the device region access defined by the device's
  226. own VFIO_DEVICE_GET_REGION_INFO ioctl.
  227. -------------------------------------------------------------------------------
  228. [1] VFIO was originally an acronym for "Virtual Function I/O" in its
  229. initial implementation by Tom Lyon while as Cisco. We've since
  230. outgrown the acronym, but it's catchy.
  231. [2] "safe" also depends upon a device being "well behaved". It's
  232. possible for multi-function devices to have backdoors between
  233. functions and even for single function devices to have alternative
  234. access to things like PCI config space through MMIO registers. To
  235. guard against the former we can include additional precautions in the
  236. IOMMU driver to group multi-function PCI devices together
  237. (iommu=group_mf). The latter we can't prevent, but the IOMMU should
  238. still provide isolation. For PCI, SR-IOV Virtual Functions are the
  239. best indicator of "well behaved", as these are designed for
  240. virtualization usage models.
  241. [3] As always there are trade-offs to virtual machine device
  242. assignment that are beyond the scope of VFIO. It's expected that
  243. future IOMMU technologies will reduce some, but maybe not all, of
  244. these trade-offs.
  245. [4] In this case the device is below a PCI bridge, so transactions
  246. from either function of the device are indistinguishable to the iommu:
  247. -[0000:00]-+-1e.0-[06]--+-0d.0
  248. \-0d.1
  249. 00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 90)