12c46bc24SAlex BennéeIntroduction 22c46bc24SAlex Bennée============ 32c46bc24SAlex Bennée 4b1769345SClaudio Fontana.. _Accelerators: 5b1769345SClaudio Fontana 62c46bc24SAlex BennéeVirtualisation Accelerators 72c46bc24SAlex Bennée--------------------------- 82c46bc24SAlex Bennée 92c46bc24SAlex BennéeQEMU's system emulation provides a virtual model of a machine (CPU, 102c46bc24SAlex Bennéememory and emulated devices) to run a guest OS. It supports a number 112c46bc24SAlex Bennéeof hypervisors (known as accelerators) as well as a JIT known as the 122c46bc24SAlex BennéeTiny Code Generator (TCG) capable of emulating many CPUs. 132c46bc24SAlex Bennée 142c46bc24SAlex Bennée.. list-table:: Supported Accelerators 152c46bc24SAlex Bennée :header-rows: 1 162c46bc24SAlex Bennée 172c46bc24SAlex Bennée * - Accelerator 182c46bc24SAlex Bennée - Host OS 192c46bc24SAlex Bennée - Host Architectures 202c46bc24SAlex Bennée * - KVM 212c46bc24SAlex Bennée - Linux 222c46bc24SAlex Bennée - Arm (64 bit only), MIPS, PPC, RISC-V, s390x, x86 232c46bc24SAlex Bennée * - Xen 242c46bc24SAlex Bennée - Linux (as dom0) 252c46bc24SAlex Bennée - Arm, x86 262c46bc24SAlex Bennée * - Hypervisor Framework (hvf) 272c46bc24SAlex Bennée - MacOS 282c46bc24SAlex Bennée - x86 (64 bit only), Arm (64 bit only) 29ca45a640SStefan Weil * - Windows Hypervisor Platform (whpx) 302c46bc24SAlex Bennée - Windows 312c46bc24SAlex Bennée - x86 322c46bc24SAlex Bennée * - NetBSD Virtual Machine Monitor (nvmm) 332c46bc24SAlex Bennée - NetBSD 342c46bc24SAlex Bennée - x86 352c46bc24SAlex Bennée * - Tiny Code Generator (tcg) 362c46bc24SAlex Bennée - Linux, other POSIX, Windows, MacOS 372c46bc24SAlex Bennée - Arm, x86, Loongarch64, MIPS, PPC, s390x, Sparc64 382c46bc24SAlex Bennée 392c46bc24SAlex BennéeFeature Overview 402c46bc24SAlex Bennée---------------- 412c46bc24SAlex Bennée 422c46bc24SAlex BennéeSystem emulation provides a wide range of device models to emulate 432c46bc24SAlex Bennéevarious hardware components you may want to add to your machine. This 442c46bc24SAlex Bennéeincludes a wide number of VirtIO devices which are specifically tuned 452c46bc24SAlex Bennéefor efficient operation under virtualisation. Some of the device 462c46bc24SAlex Bennéeemulation can be offloaded from the main QEMU process using either 472c46bc24SAlex Bennéevhost-user (for VirtIO) or :ref:`Multi-process QEMU`. If the platform 482c46bc24SAlex Bennéesupports it QEMU also supports directly passing devices through to 492c46bc24SAlex Bennéeguest VMs to eliminate the device emulation overhead. See 502c46bc24SAlex Bennée:ref:`device-emulation` for more details. 512c46bc24SAlex Bennée 522c46bc24SAlex BennéeThere is a full :ref:`featured block layer<Live Block Operations>` 532c46bc24SAlex Bennéewhich allows for construction of complex storage topology which can be 542c46bc24SAlex Bennéestacked across multiple layers supporting redirection, networking, 552c46bc24SAlex Bennéesnapshots and migration support. 562c46bc24SAlex Bennée 572c46bc24SAlex BennéeThe flexible ``chardev`` system allows for handling IO from character 582c46bc24SAlex Bennéelike devices using stdio, files, unix sockets and TCP networking. 592c46bc24SAlex Bennée 602c46bc24SAlex BennéeQEMU provides a number of management interfaces including a line based 612c46bc24SAlex Bennée:ref:`Human Monitor Protocol (HMP)<QEMU monitor>` that allows you to 622c46bc24SAlex Bennéedynamically add and remove devices as well as introspect the system 632c46bc24SAlex Bennéestate. The :ref:`QEMU Monitor Protocol<QMP Ref>` (QMP) is a well 642c46bc24SAlex Bennéedefined, versioned, machine usable API that presents a rich interface 652c46bc24SAlex Bennéeto other tools to create, control and manage Virtual Machines. This is 662c46bc24SAlex Bennéethe interface used by higher level tools interfaces such as `Virt 672c46bc24SAlex BennéeManager <https://virt-manager.org/>`_ using the `libvirt framework 682c46bc24SAlex Bennée<https://libvirt.org>`_. 692c46bc24SAlex Bennée 702c46bc24SAlex BennéeFor the common accelerators QEMU, supported debugging with its 712c46bc24SAlex Bennée:ref:`gdbstub<GDB usage>` which allows users to connect GDB and debug 722c46bc24SAlex Bennéesystem software images. 732c46bc24SAlex Bennée 742c46bc24SAlex BennéeRunning 752c46bc24SAlex Bennée------- 762c46bc24SAlex Bennée 772c46bc24SAlex BennéeQEMU provides a rich and complex API which can be overwhelming to 782c46bc24SAlex Bennéeunderstand. While some architectures can boot something with just a 792c46bc24SAlex Bennéedisk image, those examples elide a lot of details with defaults that 802c46bc24SAlex Bennéemay not be optimal for modern systems. 812c46bc24SAlex Bennée 822c46bc24SAlex BennéeFor a non-x86 system where we emulate a broad range of machine types, 832c46bc24SAlex Bennéethe command lines are generally more explicit in defining the machine 842c46bc24SAlex Bennéeand boot behaviour. You will find often find example command lines in 852c46bc24SAlex Bennéethe :ref:`system-targets-ref` section of the manual. 862c46bc24SAlex Bennée 872c46bc24SAlex BennéeWhile the project doesn't want to discourage users from using the 882c46bc24SAlex Bennéecommand line to launch VMs, we do want to highlight that there are a 892c46bc24SAlex Bennéenumber of projects dedicated to providing a more user friendly 902c46bc24SAlex Bennéeexperience. Those built around the ``libvirt`` framework can make use 912c46bc24SAlex Bennéeof feature probing to build modern VM images tailored to run on the 922c46bc24SAlex Bennéehardware you have. 932c46bc24SAlex Bennée 942c46bc24SAlex BennéeThat said, the general form of a QEMU command line can be expressed 952c46bc24SAlex Bennéeas: 962c46bc24SAlex Bennée 972c46bc24SAlex Bennée.. parsed-literal:: 982c46bc24SAlex Bennée 992c46bc24SAlex Bennée $ |qemu_system| [machine opts] \\ 1002c46bc24SAlex Bennée [cpu opts] \\ 1012c46bc24SAlex Bennée [accelerator opts] \\ 1022c46bc24SAlex Bennée [device opts] \\ 1032c46bc24SAlex Bennée [backend opts] \\ 1042c46bc24SAlex Bennée [interface opts] \\ 1052c46bc24SAlex Bennée [boot opts] 1062c46bc24SAlex Bennée 1072c46bc24SAlex BennéeMost options will generate some help information. So for example: 1082c46bc24SAlex Bennée 1092c46bc24SAlex Bennée.. parsed-literal:: 1102c46bc24SAlex Bennée 1112c46bc24SAlex Bennée $ |qemu_system| -M help 1122c46bc24SAlex Bennée 1132c46bc24SAlex Bennéewill list the machine types supported by that QEMU binary. ``help`` 1142c46bc24SAlex Bennéecan also be passed as an argument to another option. For example: 1152c46bc24SAlex Bennée 1162c46bc24SAlex Bennée.. parsed-literal:: 1172c46bc24SAlex Bennée 1182c46bc24SAlex Bennée $ |qemu_system| -device scsi-hd,help 1192c46bc24SAlex Bennée 1202c46bc24SAlex Bennéewill list the arguments and their default values of additional options 1212c46bc24SAlex Bennéethat can control the behaviour of the ``scsi-hd`` device. 1222c46bc24SAlex Bennée 1232c46bc24SAlex Bennée.. list-table:: Options Overview 1242c46bc24SAlex Bennée :header-rows: 1 1252c46bc24SAlex Bennée :widths: 10, 90 1262c46bc24SAlex Bennée 1272c46bc24SAlex Bennée * - Options 1282c46bc24SAlex Bennée - 1292c46bc24SAlex Bennée * - Machine 1302c46bc24SAlex Bennée - Define the machine type, amount of memory etc 1312c46bc24SAlex Bennée * - CPU 1322c46bc24SAlex Bennée - Type and number/topology of vCPUs. Most accelerators offer 1332c46bc24SAlex Bennée a ``host`` cpu option which simply passes through your host CPU 1342c46bc24SAlex Bennée configuration without filtering out any features. 1352c46bc24SAlex Bennée * - Accelerator 1362c46bc24SAlex Bennée - This will depend on the hypervisor you run. Note that the 1372c46bc24SAlex Bennée default is TCG, which is purely emulated, so you must specify an 1382c46bc24SAlex Bennée accelerator type to take advantage of hardware virtualization. 1392c46bc24SAlex Bennée * - Devices 1402c46bc24SAlex Bennée - Additional devices that are not defined by default with the 1412c46bc24SAlex Bennée machine type. 1422c46bc24SAlex Bennée * - Backends 1432c46bc24SAlex Bennée - Backends are how QEMU deals with the guest's data, for example 1442c46bc24SAlex Bennée how a block device is stored, how network devices see the 1452c46bc24SAlex Bennée network or how a serial device is directed to the outside world. 1462c46bc24SAlex Bennée * - Interfaces 1472c46bc24SAlex Bennée - How the system is displayed, how it is managed and controlled or 1482c46bc24SAlex Bennée debugged. 1492c46bc24SAlex Bennée * - Boot 1502c46bc24SAlex Bennée - How the system boots, via firmware or direct kernel boot. 1512c46bc24SAlex Bennée 1522c46bc24SAlex BennéeIn the following example we first define a ``virt`` machine which is a 1532c46bc24SAlex Bennéegeneral purpose platform for running Aarch64 guests. We enable 1542c46bc24SAlex Bennéevirtualisation so we can use KVM inside the emulated guest. As the 1552c46bc24SAlex Bennée``virt`` machine comes with some built in pflash devices we give them 1562c46bc24SAlex Bennéenames so we can override the defaults later. 1572c46bc24SAlex Bennée 1582c46bc24SAlex Bennée.. code:: 1592c46bc24SAlex Bennée 1602c46bc24SAlex Bennée $ qemu-system-aarch64 \ 1612c46bc24SAlex Bennée -machine type=virt,virtualization=on,pflash0=rom,pflash1=efivars \ 1622c46bc24SAlex Bennée -m 4096 \ 1632c46bc24SAlex Bennée 1642c46bc24SAlex BennéeWe then define the 4 vCPUs using the ``max`` option which gives us all 1652c46bc24SAlex Bennéethe Arm features QEMU is capable of emulating. We enable a more 1662c46bc24SAlex Bennéeemulation friendly implementation of Arm's pointer authentication 1672c46bc24SAlex Bennéealgorithm. We explicitly specify TCG acceleration even though QEMU 1682c46bc24SAlex Bennéewould default to it anyway. 1692c46bc24SAlex Bennée 1702c46bc24SAlex Bennée.. code:: 1712c46bc24SAlex Bennée 172*132f8ec7SPierrick Bouvier -cpu max \ 1732c46bc24SAlex Bennée -smp 4 \ 1742c46bc24SAlex Bennée -accel tcg \ 1752c46bc24SAlex Bennée 1762c46bc24SAlex BennéeAs the ``virt`` platform doesn't have any default network or storage 1772c46bc24SAlex Bennéedevices we need to define them. We give them ids so we can link them 1782c46bc24SAlex Bennéewith the backend later on. 1792c46bc24SAlex Bennée 1802c46bc24SAlex Bennée.. code:: 1812c46bc24SAlex Bennée 1822c46bc24SAlex Bennée -device virtio-net-pci,netdev=unet \ 1832c46bc24SAlex Bennée -device virtio-scsi-pci \ 1842c46bc24SAlex Bennée -device scsi-hd,drive=hd \ 1852c46bc24SAlex Bennée 1862c46bc24SAlex BennéeWe connect the user-mode networking to our network device. As 1872c46bc24SAlex Bennéeuser-mode networking isn't directly accessible from the outside world 1882c46bc24SAlex Bennéewe forward localhost port 2222 to the ssh port on the guest. 1892c46bc24SAlex Bennée 1902c46bc24SAlex Bennée.. code:: 1912c46bc24SAlex Bennée 1922c46bc24SAlex Bennée -netdev user,id=unet,hostfwd=tcp::2222-:22 \ 1932c46bc24SAlex Bennée 1942c46bc24SAlex BennéeWe connect the guest visible block device to an LVM partition we have 1952c46bc24SAlex Bennéeset aside for our guest. 1962c46bc24SAlex Bennée 1972c46bc24SAlex Bennée.. code:: 1982c46bc24SAlex Bennée 1992c46bc24SAlex Bennée -blockdev driver=raw,node-name=hd,file.driver=host_device,file.filename=/dev/lvm-disk/debian-bullseye-arm64 \ 2002c46bc24SAlex Bennée 2012c46bc24SAlex BennéeWe then tell QEMU to multiplex the :ref:`QEMU monitor` with the serial 2022c46bc24SAlex Bennéeport output (we can switch between the two using :ref:`keys in the 2032c46bc24SAlex Bennéecharacter backend multiplexer`). As there is no default graphical 2042c46bc24SAlex Bennéedevice we disable the display as we can work entirely in the terminal. 2052c46bc24SAlex Bennée 2062c46bc24SAlex Bennée.. code:: 2072c46bc24SAlex Bennée 2082c46bc24SAlex Bennée -serial mon:stdio \ 2092c46bc24SAlex Bennée -display none \ 2102c46bc24SAlex Bennée 2112c46bc24SAlex BennéeFinally we override the default firmware to ensure we have some 2122c46bc24SAlex Bennéestorage for EFI to persist its configuration. That firmware is 2132c46bc24SAlex Bennéeresponsible for finding the disk, booting grub and eventually running 2142c46bc24SAlex Bennéeour system. 2152c46bc24SAlex Bennée 2162c46bc24SAlex Bennée.. code:: 2172c46bc24SAlex Bennée 2182c46bc24SAlex Bennée -blockdev node-name=rom,driver=file,filename=(pwd)/pc-bios/edk2-aarch64-code.fd,read-only=true \ 2192c46bc24SAlex Bennée -blockdev node-name=efivars,driver=file,filename=$HOME/images/qemu-arm64-efivars 220