vfio内核实现分析-(6)将io暴露到userspace

来源:互联网 发布:java中socket编程实例 编辑:程序博客网 时间:2024/06/05 15:52

六、将io暴露到userspace

Io暴露到userspace比较简单,只是把io物理地址remapuserspace,对于pci设备包括pci config spacebar等。

userspace可按照如下方式访问io区域:

 /* Get a file descriptor for the device */device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:06:0d.0");/* Test and setup the device */ioctl(device, VFIO_DEVICE_GET_INFO, &device_info);for (i = 0; i < device_info.num_regions; i++) {struct vfio_region_info reg = { .argsz = sizeof(reg) };reg.index = i;ioctl(device, VFIO_DEVICE_GET_REGION_INFO, &reg);/* Setup mappings... read/write offsets, mmaps * For PCI devices, config space is a region */}

首先分析ioctl(device, VFIO_DEVICE_GET_REGION_INFO, ®),经device fd对应的struct file_operations vfio_device_fops{.ioctl} ==> struct vfio_device{vfio_device_ops{.ioctl}} :


==>function vfio_pci_ioctl 


Pci config spacepci barindex定义:


这里分析pci bar,其他的io region重映射方式类似;


==>VFIO_PCI_INDEX_TO_OFFSET


Pci bar对应的offset只是index <<40,而当userspace通过mmapread/write等访问对应区域时,对于传入的参数pposppos40位存储了实际的偏移量,ppos >> 40即可得到pci bar对应的index,有了这个index,再通过pci_resource_start、pci_resoucre_endpci_resource_len等就可得到pci bar io region对应的开始地址、结束地址、长度等信息,查看mmap实现:

==>vfio_pci_mmap


Line 846之所以是(VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)而不是(VFIO_PCI_OFFSET_SHIFT)是因为vma->vm_pgoff表示的是页粒度的偏移量offset of the area in the file, in pages)。


Line 897得到实际要访问的io区域,最后remap_pfn_range负责建立该物理区域的页表。




0 0
原创粉丝点击