2
0

kconfig.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. .. _kconfig:
  2. ================
  3. QEMU and Kconfig
  4. ================
  5. QEMU is a very versatile emulator; it can be built for a variety of
  6. targets, where each target can emulate various boards and at the same
  7. time different targets can share large amounts of code. For example,
  8. a POWER and an x86 board can run the same code to emulate a PCI network
  9. card, even though the boards use different PCI host bridges, and they
  10. can run the same code to emulate a SCSI disk while using different
  11. SCSI adapters. Arm, s390 and x86 boards can all present a virtio-blk
  12. disk to their guests, but with three different virtio guest interfaces.
  13. Each QEMU target enables a subset of the boards, devices and buses that
  14. are included in QEMU's source code. As a result, each QEMU executable
  15. only links a small subset of the files that form QEMU's source code;
  16. anything that is not needed to support a particular target is culled.
  17. QEMU uses a simple domain-specific language to describe the dependencies
  18. between components. This is useful for two reasons:
  19. * new targets and boards can be added without knowing in detail the
  20. architecture of the hardware emulation subsystems. Boards only have
  21. to list the components they need, and the compiled executable will
  22. include all the required dependencies and all the devices that the
  23. user can add to that board;
  24. * users can easily build reduced versions of QEMU that support only a subset
  25. of boards or devices. For example, by default most targets will include
  26. all emulated PCI devices that QEMU supports, but the build process is
  27. configurable and it is easy to drop unnecessary (or otherwise unwanted)
  28. code to make a leaner binary.
  29. This domain-specific language is based on the Kconfig language that
  30. originated in the Linux kernel, though it was heavily simplified and
  31. the handling of dependencies is stricter in QEMU.
  32. Unlike Linux, there is no user interface to edit the configuration, which
  33. is instead specified in per-target files under the ``configs/``
  34. directory of the QEMU source tree. This is because, unlike Linux,
  35. configuration and dependencies can be treated as a black box when building
  36. QEMU; the default configuration that QEMU ships with should be okay in
  37. almost all cases.
  38. The Kconfig language
  39. --------------------
  40. Kconfig defines configurable components in files named ``hw/*/Kconfig``.
  41. Note that configurable components are _not_ visible in C code as preprocessor
  42. symbols; they are only visible in the Makefile. Each configurable component
  43. defines a Makefile variable whose name starts with ``CONFIG_``.
  44. All elements have boolean (true/false) type; truth is written as ``y``, while
  45. falsehood is written ``n``. They are defined in a Kconfig
  46. stanza like the following::
  47. config ARM_VIRT
  48. bool
  49. imply PCI_DEVICES
  50. imply VFIO_AMD_XGBE
  51. imply VFIO_XGMAC
  52. select A15MPCORE
  53. select ACPI
  54. select ARM_SMMUV3
  55. The ``config`` keyword introduces a new configuration element. In the example
  56. above, Makefiles will have access to a variable named ``CONFIG_ARM_VIRT``,
  57. with value ``y`` or ``n`` (respectively for boolean true and false).
  58. Boolean expressions can be used within the language, whenever ``<expr>``
  59. is written in the remainder of this section. The ``&&``, ``||`` and
  60. ``!`` operators respectively denote conjunction (AND), disjunction (OR)
  61. and negation (NOT).
  62. The ``bool`` data type declaration is optional, but it is suggested to
  63. include it for clarity and future-proofing. After ``bool`` the following
  64. directives can be included:
  65. **dependencies**: ``depends on <expr>``
  66. This defines a dependency for this configurable element. Dependencies
  67. evaluate an expression and force the value of the variable to false
  68. if the expression is false.
  69. **reverse dependencies**: ``select <symbol> [if <expr>]``
  70. While ``depends on`` can force a symbol to false, reverse dependencies can
  71. be used to force another symbol to true. In the following example,
  72. ``CONFIG_BAZ`` will be true whenever ``CONFIG_FOO`` is true::
  73. config FOO
  74. select BAZ
  75. The optional expression will prevent ``select`` from having any effect
  76. unless it is true.
  77. Note that unlike Linux's Kconfig implementation, QEMU will detect
  78. contradictions between ``depends on`` and ``select`` statements and prevent
  79. you from building such a configuration.
  80. **default value**: ``default <value> [if <expr>]``
  81. Default values are assigned to the config symbol if no other value was
  82. set by the user via ``configs/*.mak`` files, and only if
  83. ``select`` or ``depends on`` directives do not force the value to true
  84. or false respectively. ``<value>`` can be ``y`` or ``n``; it cannot
  85. be an arbitrary Boolean expression. However, a condition for applying
  86. the default value can be added with ``if``.
  87. A configuration element can have any number of default values (usually,
  88. if more than one default is present, they will have different
  89. conditions). If multiple default values satisfy their condition,
  90. only the first defined one is active.
  91. **reverse default** (weak reverse dependency): ``imply <symbol> [if <expr>]``
  92. This is similar to ``select`` as it applies a lower limit of ``y``
  93. to another symbol. However, the lower limit is only a default
  94. and the "implied" symbol's value may still be set to ``n`` from a
  95. ``configs/*.mak`` files. The following two examples are
  96. equivalent::
  97. config FOO
  98. bool
  99. imply BAZ
  100. config BAZ
  101. bool
  102. default y if FOO
  103. The next section explains where to use ``imply`` or ``default y``.
  104. Guidelines for writing Kconfig files
  105. ------------------------------------
  106. Configurable elements in QEMU fall under five broad groups. Each group
  107. declares its dependencies in different ways:
  108. **subsystems**, of which **buses** are a special case
  109. Example::
  110. config SCSI
  111. bool
  112. Subsystems always default to false (they have no ``default`` directive)
  113. and are never visible in ``configs/*.mak`` files. It's
  114. up to other symbols to ``select`` whatever subsystems they require.
  115. They sometimes have ``select`` directives to bring in other required
  116. subsystems or buses. For example, ``AUX`` (the DisplayPort auxiliary
  117. channel "bus") selects ``I2C`` because it can act as an I2C master too.
  118. **devices**
  119. Example::
  120. config MEGASAS_SCSI_PCI
  121. bool
  122. default y if PCI_DEVICES
  123. depends on PCI
  124. select SCSI
  125. Devices are the most complex of the five. They can have a variety
  126. of directives that cooperate so that a default configuration includes
  127. all the devices that can be accessed from QEMU.
  128. Devices *depend on* the bus that they lie on, for example a PCI
  129. device would specify ``depends on PCI``. An MMIO device will likely
  130. have no ``depends on`` directive. Devices also *select* the buses
  131. that the device provides, for example a SCSI adapter would specify
  132. ``select SCSI``. Finally, devices are usually ``default y`` if and
  133. only if they have at least one ``depends on``; the default could be
  134. conditional on a device group.
  135. Devices also select any optional subsystem that they use; for example
  136. a video card might specify ``select EDID`` if it needs to build EDID
  137. information and publish it to the guest.
  138. **device groups**
  139. Example::
  140. config PCI_DEVICES
  141. bool
  142. Device groups provide a convenient mechanism to enable/disable many
  143. devices in one go. This is useful when a set of devices is likely to
  144. be enabled/disabled by several targets. Device groups usually need
  145. no directive and are not used in the Makefile either; they only appear
  146. as conditions for ``default y`` directives.
  147. QEMU currently has three device groups, ``PCI_DEVICES``, ``I2C_DEVICES``,
  148. and ``TEST_DEVICES``. PCI devices usually have a ``default y if
  149. PCI_DEVICES`` directive rather than just ``default y``. This lets
  150. some boards (notably s390) easily support a subset of PCI devices,
  151. for example only VFIO (passthrough) and virtio-pci devices.
  152. ``I2C_DEVICES`` is similar to ``PCI_DEVICES``. It contains i2c devices
  153. that users might reasonably want to plug in to an i2c bus on any
  154. board (and not ones which are very board-specific or that need
  155. to be wired up in a way that can't be done on the command line).
  156. ``TEST_DEVICES`` instead is used for devices that are rarely used on
  157. production virtual machines, but provide useful hooks to test QEMU
  158. or KVM.
  159. **boards**
  160. Example::
  161. config SUN4M
  162. bool
  163. default y
  164. depends on SPARC && !SPARC64
  165. imply TCX
  166. imply CG3
  167. select CS4231
  168. select ECCMEMCTL
  169. select EMPTY_SLOT
  170. select ESCC
  171. select ESP
  172. select FDC
  173. select SLAVIO
  174. select LANCE
  175. select M48T59
  176. select STP2000
  177. Boards specify their constituent devices using ``imply`` and ``select``
  178. directives. A device should be listed under ``select`` if the board
  179. cannot be started at all without it. It should be listed under
  180. ``imply`` if (depending on the QEMU command line) the board may or
  181. may not be started without it. Boards default to true, but also
  182. have a ``depends on`` clause to limit them to the appropriate targets.
  183. For some targets, not all boards may be supported by hardware
  184. virtualization, in which case they also depend on the ``TCG`` symbol,
  185. Other symbols that are commonly used as dependencies for boards
  186. include libraries (such as ``FDT``) or ``TARGET_BIG_ENDIAN``
  187. (possibly negated).
  188. Boards are listed for convenience in the ``configs/*.mak``
  189. for the target they apply to.
  190. **internal elements**
  191. Example::
  192. config ECCMEMCTL
  193. bool
  194. select ECC
  195. Internal elements group code that is useful in several boards or
  196. devices. They are usually enabled with ``select`` and in turn select
  197. other elements; they are never visible in ``configs/*.mak``
  198. files, and often not even in the Makefile.
  199. Writing and modifying default configurations
  200. --------------------------------------------
  201. In addition to the Kconfig files under hw/, each target also includes
  202. a file called ``configs/TARGETNAME-softmmu.mak``. These files
  203. initialize some Kconfig variables to non-default values and provide the
  204. starting point to turn on devices and subsystems.
  205. A file in ``configs/`` looks like the following example::
  206. # Default configuration for alpha-softmmu
  207. # Uncomment the following lines to disable these optional devices:
  208. #
  209. #CONFIG_PCI_DEVICES=n
  210. #CONFIG_TEST_DEVICES=n
  211. # Boards:
  212. #
  213. CONFIG_DP264=y
  214. The first part, consisting of commented-out ``=n`` assignments, tells
  215. the user which devices or device groups are implied by the boards.
  216. The second part, consisting of ``=y`` assignments, tells the user which
  217. boards are supported by the target. The user will typically modify
  218. the default configuration by uncommenting lines in the first group,
  219. or commenting out lines in the second group.
  220. It is also possible to run QEMU's configure script with the
  221. ``--without-default-devices`` option. When this is done, everything defaults
  222. to ``n`` unless it is ``select``\ ed or explicitly switched on in the
  223. ``.mak`` files. In other words, ``default`` and ``imply`` directives
  224. are disabled. When QEMU is built with this option, the user will probably
  225. want to change some lines in the first group, for example like this::
  226. CONFIG_PCI_DEVICES=y
  227. #CONFIG_TEST_DEVICES=n
  228. and/or pick a subset of the devices in those device groups. Without
  229. further modifications to ``configs/devices/``, a system emulator built
  230. without default devices might not do much more than start an empty
  231. machine, and even then only if ``--nodefaults`` is specified on the
  232. command line. Starting a VM *without* ``--nodefaults`` is allowed to
  233. fail, but should never abort. Failures in ``make check`` with
  234. ``--without-default-devices`` are considered bugs in the test code:
  235. the tests should either use ``--nodefaults``, and should be skipped
  236. if a necessary device is not present in the build. Such failures
  237. should not be worked around with ``select`` directives.
  238. Right now there is no single place that lists all the optional devices
  239. for ``CONFIG_PCI_DEVICES`` and ``CONFIG_TEST_DEVICES``. In the future,
  240. we expect that ``.mak`` files will be automatically generated, so that
  241. they will include all these symbols and some help text on what they do.
  242. ``Kconfig.host``
  243. ----------------
  244. In some special cases, a configurable element depends on host features
  245. that are detected by QEMU's configure or ``meson.build`` scripts; for
  246. example some devices depend on the availability of KVM or on the presence
  247. of a library on the host.
  248. These symbols should be listed in ``Kconfig.host`` like this::
  249. config TPM
  250. bool
  251. and also listed as follows in the top-level meson.build's host_kconfig
  252. variable::
  253. host_kconfig = \
  254. (have_tpm ? ['CONFIG_TPM=y'] : []) + \
  255. (host_os == 'linux' ? ['CONFIG_LINUX=y'] : []) + \
  256. (have_ivshmem ? ['CONFIG_IVSHMEM=y'] : []) + \
  257. ...