虚拟化 MacOS, PCI 直通 QEMU

2023-07-16

  • mesa3d 一个 OpenGL, OpenGL ES, Vulkan, OpenCL 开源实现库
  • 启用 vga qxl 以后没有明显的显示卡顿
  • virgl 也许能更快
  • Look into SPICE, it provides a usable enough graphics acceleration. With virt-manager is relatively easy to configure and it supports both Linux and Windows guests.
  • 听闻 vmware 在设置 gpu 加速上可足够简单. 但 You can not legally install OS X on VMware Workstation or Virtualbox and VMware has checks to prevent it.

参考

认识 USB 在哪个 PCI

/sys/bus/usb/devices/

位置上有 usb 的设备信息。

lsusb -t -v

按照树的数据结构可以得到

/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/6p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 5: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 12M
        ID 1462:7c37 Micro Star International
    |__ Port 6: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M
        ID 05e3:0608 Genesys Logic, Inc. Hub
        |__ Port 3: Dev 4, If 0, Class=Imaging, Driver=usbfs, 480M
            ID 05ac:12a8 Apple, Inc. iPhone 5/5C/5S/6/SE
        |__ Port 3: Dev 4, If 1, Class=Vendor Specific Class, Driver=usbfs, 480M
            ID 05ac:12a8 Apple, Inc. iPhone 5/5C/5S/6/SE
        |__ Port 3: Dev 4, If 2, Class=Vendor Specific Class, Driver=ipheth, 480M
            ID 05ac:12a8 Apple, Inc. iPhone 5/5C/5S/6/SE

可以认识到

BUS 03 Port 6 . Port 3 : Dev 4, If 0

简写后的结果就是 3-6.3:4.0

为了获得精准 pci id; 执行

readlink /sys/bus/usb/devices/3-6.3:4.2

output:

../../../devices/pci0000:00/0000:00:01.2/0000:20:00.0/0000:21:08.0/0000:2a:00.3/usb3/3-6/3-6.3/3-6.3:4.2

PCI 的罗列:


lspci | grep USB

output:

2a:00.1 USB controller: Advanced Micro Devices, Inc. [AMD] Matisse USB 3.0 Host Controller
2a:00.3 USB controller: Advanced Micro Devices, Inc. [AMD] Matisse USB 3.0 Host Controller
2d:00.2 USB controller: NVIDIA Corporation TU106 USB 3.1 Host Controller (rev a1)
2d:00.3 Serial bus controller: NVIDIA Corporation TU106 USB Type-C UCSI Controller (rev a1)
2f:00.3 USB controller: Advanced Micro Devices, Inc. [AMD] Matisse USB 3.0 Host Controller
lspci -v

detail:

2a:00.3 USB controller: Advanced Micro Devices, Inc. [AMD] Matisse USB 3.0 Host Controller (prog-if 30 [XHCI])
	Subsystem: Advanced Micro Devices, Inc. [AMD] Matisse USB 3.0 Host Controller
	Flags: bus master, fast devsel, latency 0, IRQ 39, IOMMU group 17
	Memory at fc200000 (64-bit, non-prefetchable) [size=1M]
	Capabilities: <access denied>
	Kernel driver in use: xhci_hcd
	Kernel modules: xhci_pci

对 readlink 的结果和 PCI 的结果匹配一下,可以得到: 3-6.3:4.0 是在 2a:00.3 PCI 上的。

对 2a:00.3 设备解绑

echo 0000:2a:00.3 > /sys/bus/pci/drivers/xhci_hcd/unbind

这里注意要新建一个 shell 用 sudo 执行,原因见:https://stackoverflow.com/questions/67282119/echo-command-on-unbind-wont-work-with-sudo-permission-denied

了解 pci 以 <vendor_code>:<device_code> 的格式显示

Issue lspci -n to display this information for your PCIe devices in the format <vendor_code>:<device_code>.

lspci -n

...
2a:00.3 0c03: 1022:149c
...

# echo 1022 149c > /sys/bus/pci/drivers/vfio-pci/new_id

了解 pci 目前的绑定情况

lspci -k

给 qemu 加上 vfio-pci

  -device vfio-pci,host=0000:2a:00.3

出现报错


(qemu) qemu-system-x86_64: -device vfio-pci,host=0000:2a:00.3: VFIO_MAP_DMA failed: Cannot allocate memory
qemu-system-x86_64: -device vfio-pci,host=0000:2a:00.3: VFIO_MAP_DMA failed: Cannot allocate memory
qemu-system-x86_64: -device vfio-pci,host=0000:2a:00.3: vfio 0000:2a:00.3: failed to setup container for group 17: memory listener initialization failed: Region pc.ram: vfio_dma_map(0x5576b8de4a90, 0x0, 0x80000000, 0x7f3922600000) = -12 (Cannot allocate memory)

vfio_get_group vfio_connect_container vfio_dma_map

在 hw/vfio/common.c

三个命令,无法分配内存,原因,udev 没有分配到指定组。解决办法:将 qemu 组跟 device 组保持一致,则可正常运行。

编译 qemu 8.0 命令

cd build/

../configure --enable-slirp --target-list=x86_64-softmmu,xtensa-softmmu xtensaeb-softmmu,x86_64-linux-user,xtensa-linux-user  --enable-debug --enable-vfio-user-server --enable-sdl --enable-opengl --enable-virglrenderer --enable-system --enable-modules --audio-drv-list=pa
copyright ©2019-2024 shenzhen
粤ICP备20041170号-1