12eba427eSPaolo Bonzini.. _kconfig: 22eba427eSPaolo Bonzini 3576c3f2fSPaolo Bonzini================ 4576c3f2fSPaolo BonziniQEMU and Kconfig 5576c3f2fSPaolo Bonzini================ 6576c3f2fSPaolo Bonzini 7576c3f2fSPaolo BonziniQEMU is a very versatile emulator; it can be built for a variety of 8576c3f2fSPaolo Bonzinitargets, where each target can emulate various boards and at the same 9576c3f2fSPaolo Bonzinitime different targets can share large amounts of code. For example, 10576c3f2fSPaolo Bonzinia POWER and an x86 board can run the same code to emulate a PCI network 11576c3f2fSPaolo Bonzinicard, even though the boards use different PCI host bridges, and they 12576c3f2fSPaolo Bonzinican run the same code to emulate a SCSI disk while using different 136fe6d6c9SPeter MaydellSCSI adapters. Arm, s390 and x86 boards can all present a virtio-blk 14576c3f2fSPaolo Bonzinidisk to their guests, but with three different virtio guest interfaces. 15576c3f2fSPaolo Bonzini 16576c3f2fSPaolo BonziniEach QEMU target enables a subset of the boards, devices and buses that 17576c3f2fSPaolo Bonziniare included in QEMU's source code. As a result, each QEMU executable 18576c3f2fSPaolo Bonzinionly links a small subset of the files that form QEMU's source code; 19576c3f2fSPaolo Bonzinianything that is not needed to support a particular target is culled. 20576c3f2fSPaolo Bonzini 21576c3f2fSPaolo BonziniQEMU uses a simple domain-specific language to describe the dependencies 22576c3f2fSPaolo Bonzinibetween components. This is useful for two reasons: 23576c3f2fSPaolo Bonzini 24576c3f2fSPaolo Bonzini* new targets and boards can be added without knowing in detail the 25576c3f2fSPaolo Bonzini architecture of the hardware emulation subsystems. Boards only have 26576c3f2fSPaolo Bonzini to list the components they need, and the compiled executable will 27576c3f2fSPaolo Bonzini include all the required dependencies and all the devices that the 28576c3f2fSPaolo Bonzini user can add to that board; 29576c3f2fSPaolo Bonzini 30576c3f2fSPaolo Bonzini* users can easily build reduced versions of QEMU that support only a subset 31576c3f2fSPaolo Bonzini of boards or devices. For example, by default most targets will include 32576c3f2fSPaolo Bonzini all emulated PCI devices that QEMU supports, but the build process is 33576c3f2fSPaolo Bonzini configurable and it is easy to drop unnecessary (or otherwise unwanted) 34576c3f2fSPaolo Bonzini code to make a leaner binary. 35576c3f2fSPaolo Bonzini 36576c3f2fSPaolo BonziniThis domain-specific language is based on the Kconfig language that 37576c3f2fSPaolo Bonzinioriginated in the Linux kernel, though it was heavily simplified and 38576c3f2fSPaolo Bonzinithe handling of dependencies is stricter in QEMU. 39576c3f2fSPaolo Bonzini 40576c3f2fSPaolo BonziniUnlike Linux, there is no user interface to edit the configuration, which 41576c3f2fSPaolo Bonziniis instead specified in per-target files under the ``default-configs/`` 42576c3f2fSPaolo Bonzinidirectory of the QEMU source tree. This is because, unlike Linux, 43576c3f2fSPaolo Bonziniconfiguration and dependencies can be treated as a black box when building 44576c3f2fSPaolo BonziniQEMU; the default configuration that QEMU ships with should be okay in 45576c3f2fSPaolo Bonzinialmost all cases. 46576c3f2fSPaolo Bonzini 47576c3f2fSPaolo BonziniThe Kconfig language 48576c3f2fSPaolo Bonzini-------------------- 49576c3f2fSPaolo Bonzini 50576c3f2fSPaolo BonziniKconfig defines configurable components in files named ``hw/*/Kconfig``. 51576c3f2fSPaolo BonziniNote that configurable components are _not_ visible in C code as preprocessor 52576c3f2fSPaolo Bonzinisymbols; they are only visible in the Makefile. Each configurable component 53576c3f2fSPaolo Bonzinidefines a Makefile variable whose name starts with ``CONFIG_``. 54576c3f2fSPaolo Bonzini 55576c3f2fSPaolo BonziniAll elements have boolean (true/false) type; truth is written as ``y``, while 56576c3f2fSPaolo Bonzinifalsehood is written ``n``. They are defined in a Kconfig 57576c3f2fSPaolo Bonzinistanza like the following:: 58576c3f2fSPaolo Bonzini 59576c3f2fSPaolo Bonzini config ARM_VIRT 60576c3f2fSPaolo Bonzini bool 61576c3f2fSPaolo Bonzini imply PCI_DEVICES 62576c3f2fSPaolo Bonzini imply VFIO_AMD_XGBE 63576c3f2fSPaolo Bonzini imply VFIO_XGMAC 64576c3f2fSPaolo Bonzini select A15MPCORE 65576c3f2fSPaolo Bonzini select ACPI 66576c3f2fSPaolo Bonzini select ARM_SMMUV3 67576c3f2fSPaolo Bonzini 68576c3f2fSPaolo BonziniThe ``config`` keyword introduces a new configuration element. In the example 69576c3f2fSPaolo Bonziniabove, Makefiles will have access to a variable named ``CONFIG_ARM_VIRT``, 70576c3f2fSPaolo Bonziniwith value ``y`` or ``n`` (respectively for boolean true and false). 71576c3f2fSPaolo Bonzini 72576c3f2fSPaolo BonziniBoolean expressions can be used within the language, whenever ``<expr>`` 73576c3f2fSPaolo Bonziniis written in the remainder of this section. The ``&&``, ``||`` and 74576c3f2fSPaolo Bonzini``!`` operators respectively denote conjunction (AND), disjunction (OR) 75576c3f2fSPaolo Bonziniand negation (NOT). 76576c3f2fSPaolo Bonzini 77576c3f2fSPaolo BonziniThe ``bool`` data type declaration is optional, but it is suggested to 78576c3f2fSPaolo Bonziniinclude it for clarity and future-proofing. After ``bool`` the following 79576c3f2fSPaolo Bonzinidirectives can be included: 80576c3f2fSPaolo Bonzini 81576c3f2fSPaolo Bonzini**dependencies**: ``depends on <expr>`` 82576c3f2fSPaolo Bonzini 83576c3f2fSPaolo Bonzini This defines a dependency for this configurable element. Dependencies 84576c3f2fSPaolo Bonzini evaluate an expression and force the value of the variable to false 85576c3f2fSPaolo Bonzini if the expression is false. 86576c3f2fSPaolo Bonzini 87576c3f2fSPaolo Bonzini**reverse dependencies**: ``select <symbol> [if <expr>]`` 88576c3f2fSPaolo Bonzini 89576c3f2fSPaolo Bonzini While ``depends on`` can force a symbol to false, reverse dependencies can 90576c3f2fSPaolo Bonzini be used to force another symbol to true. In the following example, 91576c3f2fSPaolo Bonzini ``CONFIG_BAZ`` will be true whenever ``CONFIG_FOO`` is true:: 92576c3f2fSPaolo Bonzini 93576c3f2fSPaolo Bonzini config FOO 94576c3f2fSPaolo Bonzini select BAZ 95576c3f2fSPaolo Bonzini 96576c3f2fSPaolo Bonzini The optional expression will prevent ``select`` from having any effect 97576c3f2fSPaolo Bonzini unless it is true. 98576c3f2fSPaolo Bonzini 99576c3f2fSPaolo Bonzini Note that unlike Linux's Kconfig implementation, QEMU will detect 100576c3f2fSPaolo Bonzini contradictions between ``depends on`` and ``select`` statements and prevent 101576c3f2fSPaolo Bonzini you from building such a configuration. 102576c3f2fSPaolo Bonzini 103576c3f2fSPaolo Bonzini**default value**: ``default <value> [if <expr>]`` 104576c3f2fSPaolo Bonzini 105576c3f2fSPaolo Bonzini Default values are assigned to the config symbol if no other value was 106576c3f2fSPaolo Bonzini set by the user via ``default-configs/*.mak`` files, and only if 107576c3f2fSPaolo Bonzini ``select`` or ``depends on`` directives do not force the value to true 108576c3f2fSPaolo Bonzini or false respectively. ``<value>`` can be ``y`` or ``n``; it cannot 109576c3f2fSPaolo Bonzini be an arbitrary Boolean expression. However, a condition for applying 110576c3f2fSPaolo Bonzini the default value can be added with ``if``. 111576c3f2fSPaolo Bonzini 112576c3f2fSPaolo Bonzini A configuration element can have any number of default values (usually, 113576c3f2fSPaolo Bonzini if more than one default is present, they will have different 114576c3f2fSPaolo Bonzini conditions). If multiple default values satisfy their condition, 115576c3f2fSPaolo Bonzini only the first defined one is active. 116576c3f2fSPaolo Bonzini 117576c3f2fSPaolo Bonzini**reverse default** (weak reverse dependency): ``imply <symbol> [if <expr>]`` 118576c3f2fSPaolo Bonzini 119576c3f2fSPaolo Bonzini This is similar to ``select`` as it applies a lower limit of ``y`` 120576c3f2fSPaolo Bonzini to another symbol. However, the lower limit is only a default 121576c3f2fSPaolo Bonzini and the "implied" symbol's value may still be set to ``n`` from a 122576c3f2fSPaolo Bonzini ``default-configs/*.mak`` files. The following two examples are 123576c3f2fSPaolo Bonzini equivalent:: 124576c3f2fSPaolo Bonzini 125576c3f2fSPaolo Bonzini config FOO 126576c3f2fSPaolo Bonzini bool 127576c3f2fSPaolo Bonzini imply BAZ 128576c3f2fSPaolo Bonzini 129576c3f2fSPaolo Bonzini config BAZ 130576c3f2fSPaolo Bonzini bool 131576c3f2fSPaolo Bonzini default y if FOO 132576c3f2fSPaolo Bonzini 133576c3f2fSPaolo Bonzini The next section explains where to use ``imply`` or ``default y``. 134576c3f2fSPaolo Bonzini 135576c3f2fSPaolo BonziniGuidelines for writing Kconfig files 136576c3f2fSPaolo Bonzini------------------------------------ 137576c3f2fSPaolo Bonzini 138576c3f2fSPaolo BonziniConfigurable elements in QEMU fall under five broad groups. Each group 139576c3f2fSPaolo Bonzinideclares its dependencies in different ways: 140576c3f2fSPaolo Bonzini 141576c3f2fSPaolo Bonzini**subsystems**, of which **buses** are a special case 142576c3f2fSPaolo Bonzini 143576c3f2fSPaolo Bonzini Example:: 144576c3f2fSPaolo Bonzini 145576c3f2fSPaolo Bonzini config SCSI 146576c3f2fSPaolo Bonzini bool 147576c3f2fSPaolo Bonzini 148576c3f2fSPaolo Bonzini Subsystems always default to false (they have no ``default`` directive) 149576c3f2fSPaolo Bonzini and are never visible in ``default-configs/*.mak`` files. It's 150576c3f2fSPaolo Bonzini up to other symbols to ``select`` whatever subsystems they require. 151576c3f2fSPaolo Bonzini 152576c3f2fSPaolo Bonzini They sometimes have ``select`` directives to bring in other required 153576c3f2fSPaolo Bonzini subsystems or buses. For example, ``AUX`` (the DisplayPort auxiliary 154576c3f2fSPaolo Bonzini channel "bus") selects ``I2C`` because it can act as an I2C master too. 155576c3f2fSPaolo Bonzini 156576c3f2fSPaolo Bonzini**devices** 157576c3f2fSPaolo Bonzini 158576c3f2fSPaolo Bonzini Example:: 159576c3f2fSPaolo Bonzini 160576c3f2fSPaolo Bonzini config MEGASAS_SCSI_PCI 161576c3f2fSPaolo Bonzini bool 162576c3f2fSPaolo Bonzini default y if PCI_DEVICES 163576c3f2fSPaolo Bonzini depends on PCI 164576c3f2fSPaolo Bonzini select SCSI 165576c3f2fSPaolo Bonzini 166576c3f2fSPaolo Bonzini Devices are the most complex of the five. They can have a variety 167576c3f2fSPaolo Bonzini of directives that cooperate so that a default configuration includes 168576c3f2fSPaolo Bonzini all the devices that can be accessed from QEMU. 169576c3f2fSPaolo Bonzini 170576c3f2fSPaolo Bonzini Devices *depend on* the bus that they lie on, for example a PCI 171576c3f2fSPaolo Bonzini device would specify ``depends on PCI``. An MMIO device will likely 172576c3f2fSPaolo Bonzini have no ``depends on`` directive. Devices also *select* the buses 173576c3f2fSPaolo Bonzini that the device provides, for example a SCSI adapter would specify 174576c3f2fSPaolo Bonzini ``select SCSI``. Finally, devices are usually ``default y`` if and 175576c3f2fSPaolo Bonzini only if they have at least one ``depends on``; the default could be 176576c3f2fSPaolo Bonzini conditional on a device group. 177576c3f2fSPaolo Bonzini 178576c3f2fSPaolo Bonzini Devices also select any optional subsystem that they use; for example 179576c3f2fSPaolo Bonzini a video card might specify ``select EDID`` if it needs to build EDID 180576c3f2fSPaolo Bonzini information and publish it to the guest. 181576c3f2fSPaolo Bonzini 182576c3f2fSPaolo Bonzini**device groups** 183576c3f2fSPaolo Bonzini 184576c3f2fSPaolo Bonzini Example:: 185576c3f2fSPaolo Bonzini 186576c3f2fSPaolo Bonzini config PCI_DEVICES 187576c3f2fSPaolo Bonzini bool 188576c3f2fSPaolo Bonzini 189576c3f2fSPaolo Bonzini Device groups provide a convenient mechanism to enable/disable many 190576c3f2fSPaolo Bonzini devices in one go. This is useful when a set of devices is likely to 191576c3f2fSPaolo Bonzini be enabled/disabled by several targets. Device groups usually need 192576c3f2fSPaolo Bonzini no directive and are not used in the Makefile either; they only appear 193576c3f2fSPaolo Bonzini as conditions for ``default y`` directives. 194576c3f2fSPaolo Bonzini 195b5bf5a53SPeter Maydell QEMU currently has three device groups, ``PCI_DEVICES``, ``I2C_DEVICES``, 196b5bf5a53SPeter Maydell and ``TEST_DEVICES``. PCI devices usually have a ``default y if 197576c3f2fSPaolo Bonzini PCI_DEVICES`` directive rather than just ``default y``. This lets 198576c3f2fSPaolo Bonzini some boards (notably s390) easily support a subset of PCI devices, 199576c3f2fSPaolo Bonzini for example only VFIO (passthrough) and virtio-pci devices. 200b5bf5a53SPeter Maydell ``I2C_DEVICES`` is similar to ``PCI_DEVICES``. It contains i2c devices 201b5bf5a53SPeter Maydell that users might reasonably want to plug in to an i2c bus on any 202b5bf5a53SPeter Maydell board (and not ones which are very board-specific or that need 203b5bf5a53SPeter Maydell to be wired up in a way that can't be done on the command line). 204576c3f2fSPaolo Bonzini ``TEST_DEVICES`` instead is used for devices that are rarely used on 205576c3f2fSPaolo Bonzini production virtual machines, but provide useful hooks to test QEMU 206576c3f2fSPaolo Bonzini or KVM. 207576c3f2fSPaolo Bonzini 208576c3f2fSPaolo Bonzini**boards** 209576c3f2fSPaolo Bonzini 210576c3f2fSPaolo Bonzini Example:: 211576c3f2fSPaolo Bonzini 212576c3f2fSPaolo Bonzini config SUN4M 213576c3f2fSPaolo Bonzini bool 214*7cdfcea6SPaolo Bonzini default y 215*7cdfcea6SPaolo Bonzini depends on SPARC && !SPARC64 216576c3f2fSPaolo Bonzini imply TCX 217576c3f2fSPaolo Bonzini imply CG3 218576c3f2fSPaolo Bonzini select CS4231 219576c3f2fSPaolo Bonzini select ECCMEMCTL 220576c3f2fSPaolo Bonzini select EMPTY_SLOT 221576c3f2fSPaolo Bonzini select ESCC 222576c3f2fSPaolo Bonzini select ESP 223576c3f2fSPaolo Bonzini select FDC 224576c3f2fSPaolo Bonzini select SLAVIO 225576c3f2fSPaolo Bonzini select LANCE 226576c3f2fSPaolo Bonzini select M48T59 227576c3f2fSPaolo Bonzini select STP2000 228576c3f2fSPaolo Bonzini 229576c3f2fSPaolo Bonzini Boards specify their constituent devices using ``imply`` and ``select`` 230576c3f2fSPaolo Bonzini directives. A device should be listed under ``select`` if the board 231576c3f2fSPaolo Bonzini cannot be started at all without it. It should be listed under 232576c3f2fSPaolo Bonzini ``imply`` if (depending on the QEMU command line) the board may or 233*7cdfcea6SPaolo Bonzini may not be started without it. Boards default to true, but also 234*7cdfcea6SPaolo Bonzini have a ``depends on`` clause to limit them to the appropriate targets. 235*7cdfcea6SPaolo Bonzini For some targets, not all boards may be supported by hardware 236*7cdfcea6SPaolo Bonzini virtualization, in which case they also depend on the ``TCG`` symbol, 237*7cdfcea6SPaolo Bonzini Other symbols that are commonly used as dependencies for boards 238*7cdfcea6SPaolo Bonzini include libraries (such as ``FDT``) or ``TARGET_BIG_ENDIAN`` 239*7cdfcea6SPaolo Bonzini (possibly negated). 240*7cdfcea6SPaolo Bonzini 241*7cdfcea6SPaolo Bonzini Boards are listed for convenience in the ``default-configs/*.mak`` 242*7cdfcea6SPaolo Bonzini for the target they apply to. 243576c3f2fSPaolo Bonzini 244576c3f2fSPaolo Bonzini**internal elements** 245576c3f2fSPaolo Bonzini 246576c3f2fSPaolo Bonzini Example:: 247576c3f2fSPaolo Bonzini 248576c3f2fSPaolo Bonzini config ECCMEMCTL 249576c3f2fSPaolo Bonzini bool 250576c3f2fSPaolo Bonzini select ECC 251576c3f2fSPaolo Bonzini 252576c3f2fSPaolo Bonzini Internal elements group code that is useful in several boards or 253576c3f2fSPaolo Bonzini devices. They are usually enabled with ``select`` and in turn select 254576c3f2fSPaolo Bonzini other elements; they are never visible in ``default-configs/*.mak`` 255576c3f2fSPaolo Bonzini files, and often not even in the Makefile. 256576c3f2fSPaolo Bonzini 257576c3f2fSPaolo BonziniWriting and modifying default configurations 258576c3f2fSPaolo Bonzini-------------------------------------------- 259576c3f2fSPaolo Bonzini 260576c3f2fSPaolo BonziniIn addition to the Kconfig files under hw/, each target also includes 261576c3f2fSPaolo Bonzinia file called ``default-configs/TARGETNAME-softmmu.mak``. These files 262576c3f2fSPaolo Bonziniinitialize some Kconfig variables to non-default values and provide the 263576c3f2fSPaolo Bonzinistarting point to turn on devices and subsystems. 264576c3f2fSPaolo Bonzini 265576c3f2fSPaolo BonziniA file in ``default-configs/`` looks like the following example:: 266576c3f2fSPaolo Bonzini 267576c3f2fSPaolo Bonzini # Default configuration for alpha-softmmu 268576c3f2fSPaolo Bonzini 269576c3f2fSPaolo Bonzini # Uncomment the following lines to disable these optional devices: 270576c3f2fSPaolo Bonzini # 271576c3f2fSPaolo Bonzini #CONFIG_PCI_DEVICES=n 272576c3f2fSPaolo Bonzini #CONFIG_TEST_DEVICES=n 273576c3f2fSPaolo Bonzini 274576c3f2fSPaolo Bonzini # Boards: 275576c3f2fSPaolo Bonzini # 276576c3f2fSPaolo Bonzini CONFIG_DP264=y 277576c3f2fSPaolo Bonzini 278576c3f2fSPaolo BonziniThe first part, consisting of commented-out ``=n`` assignments, tells 279576c3f2fSPaolo Bonzinithe user which devices or device groups are implied by the boards. 280576c3f2fSPaolo BonziniThe second part, consisting of ``=y`` assignments, tells the user which 281576c3f2fSPaolo Bonziniboards are supported by the target. The user will typically modify 282576c3f2fSPaolo Bonzinithe default configuration by uncommenting lines in the first group, 283576c3f2fSPaolo Bonzinior commenting out lines in the second group. 284576c3f2fSPaolo Bonzini 285576c3f2fSPaolo BonziniIt is also possible to run QEMU's configure script with the 2866baabe5cSMarc-André Lureau``--without-default-devices`` option. When this is done, everything defaults 2870f40f7caSPeter Maydellto ``n`` unless it is ``select``\ ed or explicitly switched on in the 288576c3f2fSPaolo Bonzini``.mak`` files. In other words, ``default`` and ``imply`` directives 289576c3f2fSPaolo Bonziniare disabled. When QEMU is built with this option, the user will probably 290576c3f2fSPaolo Bonziniwant to change some lines in the first group, for example like this:: 291576c3f2fSPaolo Bonzini 292576c3f2fSPaolo Bonzini CONFIG_PCI_DEVICES=y 293576c3f2fSPaolo Bonzini #CONFIG_TEST_DEVICES=n 294576c3f2fSPaolo Bonzini 29523b2a3beSPaolo Bonziniand/or pick a subset of the devices in those device groups. Without 29623b2a3beSPaolo Bonzinifurther modifications to ``configs/devices/``, a system emulator built 29723b2a3beSPaolo Bonziniwithout default devices might not do much more than start an empty 29823b2a3beSPaolo Bonzinimachine, and even then only if ``--nodefaults`` is specified on the 29923b2a3beSPaolo Bonzinicommand line. Starting a VM *without* ``--nodefaults`` is allowed to 30023b2a3beSPaolo Bonzinifail, but should never abort. Failures in ``make check`` with 30123b2a3beSPaolo Bonzini``--without-default-devices`` are considered bugs in the test code: 30223b2a3beSPaolo Bonzinithe tests should either use ``--nodefaults``, and should be skipped 30323b2a3beSPaolo Bonziniif a necessary device is not present in the build. Such failures 30423b2a3beSPaolo Bonzinishould not be worked around with ``select`` directives. 30523b2a3beSPaolo Bonzini 30623b2a3beSPaolo BonziniRight now there is no single place that lists all the optional devices 30723b2a3beSPaolo Bonzinifor ``CONFIG_PCI_DEVICES`` and ``CONFIG_TEST_DEVICES``. In the future, 308576c3f2fSPaolo Bonziniwe expect that ``.mak`` files will be automatically generated, so that 309576c3f2fSPaolo Bonzinithey will include all these symbols and some help text on what they do. 310576c3f2fSPaolo Bonzini 311576c3f2fSPaolo Bonzini``Kconfig.host`` 312576c3f2fSPaolo Bonzini---------------- 313576c3f2fSPaolo Bonzini 314576c3f2fSPaolo BonziniIn some special cases, a configurable element depends on host features 3150a189110SPaolo Bonzinithat are detected by QEMU's configure or ``meson.build`` scripts; for 3160a189110SPaolo Bonziniexample some devices depend on the availability of KVM or on the presence 3170a189110SPaolo Bonziniof a library on the host. 318576c3f2fSPaolo Bonzini 319576c3f2fSPaolo BonziniThese symbols should be listed in ``Kconfig.host`` like this:: 320576c3f2fSPaolo Bonzini 3210a189110SPaolo Bonzini config TPM 322576c3f2fSPaolo Bonzini bool 323576c3f2fSPaolo Bonzini 3240a189110SPaolo Bonziniand also listed as follows in the top-level meson.build's host_kconfig 325576c3f2fSPaolo Bonzinivariable:: 326576c3f2fSPaolo Bonzini 3270a189110SPaolo Bonzini host_kconfig = \ 3280d04c4c9SPaolo Bonzini (have_tpm ? ['CONFIG_TPM=y'] : []) + \ 329d0cda6f4SPaolo Bonzini (host_os == 'linux' ? ['CONFIG_LINUX=y'] : []) + \ 330ccd250aaSPaolo Bonzini (have_ivshmem ? ['CONFIG_IVSHMEM=y'] : []) + \ 331576c3f2fSPaolo Bonzini ... 332