5 Guest-side hardware interface
6 =============================
11 The QEMU TPM emulation implements a TPM TIS hardware interface
12 following the Trusted Computing Group's specification "TCG PC Client
13 Specific TPM Interface Specification (TIS)", Specification Version
14 1.3, 21 March 2013. (see the `TIS specification`_, or a later version
17 The TIS interface makes a memory mapped IO region in the area
18 0xfed40000-0xfed44fff available to the guest operating system.
20 QEMU files related to TPM TIS interface:
21 - ``hw/tpm/tpm_tis_common.c``
22 - ``hw/tpm/tpm_tis_isa.c``
23 - ``hw/tpm/tpm_tis_sysbus.c``
24 - ``hw/tpm/tpm_tis_i2c.c``
25 - ``hw/tpm/tpm_tis.h``
27 Both an ISA device and a sysbus device are available. The former is
28 used with pc/q35 machine while the latter can be instantiated in the
31 An I2C device support is also provided which can be instantiated in the Arm
32 based emulation machines. This device only supports the TPM 2 protocol.
37 QEMU also implements a TPM CRB interface following the Trusted
38 Computing Group's specification "TCG PC Client Platform TPM Profile
39 (PTP) Specification", Family "2.0", Level 00 Revision 01.03 v22, May
40 22, 2017. (see the `CRB specification`_, or a later version of it)
42 The CRB interface makes a memory mapped IO region in the area
43 0xfed40000-0xfed40fff (1 locality) available to the guest
46 QEMU files related to TPM CRB interface:
47 - ``hw/tpm/tpm_crb.c``
52 pSeries (ppc64) machines offer a tpm-spapr device model.
54 QEMU files related to the SPAPR interface:
55 - ``hw/tpm/tpm_spapr.c``
60 The bios/firmware may read the ``"etc/tpm/config"`` fw_cfg entry for
61 configuring the guest appropriately.
63 The entry of 6 bytes has the following content, in little-endian:
67 #define TPM_VERSION_UNSPEC 0
68 #define TPM_VERSION_1_2 1
69 #define TPM_VERSION_2_0 2
71 #define TPM_PPI_VERSION_NONE 0
72 #define TPM_PPI_VERSION_1_30 1
74 struct FwCfgTPMConfig {
75 uint32_t tpmppi_address; /* PPI memory location */
76 uint8_t tpm_version; /* TPM version */
77 uint8_t tpmppi_version; /* PPI version */
83 The TPM device is defined with ACPI ID "PNP0C31". QEMU builds a SSDT
84 and passes it into the guest through the fw_cfg device. The device
85 description contains the base address of the TIS interface 0xfed40000
86 and the size of the MMIO area (0x5000). In case a TPM2 is used by
87 QEMU, a TPM2 ACPI table is also provided. The device is described to
88 be used in polling mode rather than interrupt mode primarily because
89 no unused IRQ could be found.
91 To support measurement logs to be written by the firmware,
92 e.g. SeaBIOS, a TCPA table is implemented. This table provides a 64kb
93 buffer where the firmware can write its log into. For TPM 2 only a
94 more recent version of the TPM2 table provides support for
95 measurements logs and a TCPA table does not need to be created.
97 The TCPA and TPM2 ACPI tables follow the Trusted Computing Group
98 specification "TCG ACPI Specification" Family "1.2" and "2.0", Level
99 00 Revision 00.37. (see the `ACPI specification`_, or a later version
105 QEMU supports the Physical Presence Interface (PPI) for TPM 1.2 and
106 TPM 2. This interface requires ACPI and firmware support. (see the
107 `PPI specification`_)
109 PPI enables a system administrator (root) to request a modification to
110 the TPM upon reboot. The PPI specification defines the operation
111 requests and the actions the firmware has to take. The system
112 administrator passes the operation request number to the firmware
113 through an ACPI interface which writes this number to a memory
114 location that the firmware knows. Upon reboot, the firmware finds the
115 number and sends commands to the TPM. The firmware writes the TPM
116 result code and the operation request number to a memory location that
117 ACPI can read from and pass the result on to the administrator.
119 The PPI specification defines a set of mandatory and optional
120 operations for the firmware to implement. The ACPI interface also
121 allows an administrator to list the supported operations. In QEMU the
122 ACPI code is generated by QEMU, yet the firmware needs to implement
123 support on a per-operations basis, and different firmwares may support
124 a different subset. Therefore, QEMU introduces the virtual memory
125 device for PPI where the firmware can indicate which operations it
126 supports and ACPI can enable the ones that are supported and disable
127 all others. This interface lies in main memory and has the following
130 +-------------+--------+--------+-------------------------------------------+
131 | Field | Length | Offset | Description |
132 +=============+========+========+===========================================+
133 | ``func`` | 0x100 | 0x000 | Firmware sets values for each supported |
134 | | | | operation. See defined values below. |
135 +-------------+--------+--------+-------------------------------------------+
136 | ``ppin`` | 0x1 | 0x100 | SMI interrupt to use. Set by firmware. |
137 | | | | Not supported. |
138 +-------------+--------+--------+-------------------------------------------+
139 | ``ppip`` | 0x4 | 0x101 | ACPI function index to pass to SMM code. |
140 | | | | Set by ACPI. Not supported. |
141 +-------------+--------+--------+-------------------------------------------+
142 | ``pprp`` | 0x4 | 0x105 | Result of last executed operation. Set by |
143 | | | | firmware. See function index 5 for values.|
144 +-------------+--------+--------+-------------------------------------------+
145 | ``pprq`` | 0x4 | 0x109 | Operation request number to execute. See |
146 | | | | 'Physical Presence Interface Operation |
147 | | | | Summary' tables in specs. Set by ACPI. |
148 +-------------+--------+--------+-------------------------------------------+
149 | ``pprm`` | 0x4 | 0x10d | Operation request optional parameter. |
150 | | | | Values depend on operation. Set by ACPI. |
151 +-------------+--------+--------+-------------------------------------------+
152 | ``lppr`` | 0x4 | 0x111 | Last executed operation request number. |
153 | | | | Copied from pprq field by firmware. |
154 +-------------+--------+--------+-------------------------------------------+
155 | ``fret`` | 0x4 | 0x115 | Result code from SMM function. |
156 | | | | Not supported. |
157 +-------------+--------+--------+-------------------------------------------+
158 | ``res1`` | 0x40 | 0x119 | Reserved for future use |
159 +-------------+--------+--------+-------------------------------------------+
160 |``next_step``| 0x1 | 0x159 | Operation to execute after reboot by |
161 | | | | firmware. Used by firmware. |
162 +-------------+--------+--------+-------------------------------------------+
163 | ``movv`` | 0x1 | 0x15a | Memory overwrite variable |
164 +-------------+--------+--------+-------------------------------------------+
166 The following values are supported for the ``func`` field. They
167 correspond to the values used by ACPI function index 8.
169 +----------+-------------------------------------------------------------+
170 | Value | Description |
171 +==========+=============================================================+
172 | 0 | Operation is not implemented. |
173 +----------+-------------------------------------------------------------+
174 | 1 | Operation is only accessible through firmware. |
175 +----------+-------------------------------------------------------------+
176 | 2 | Operation is blocked for OS by firmware configuration. |
177 +----------+-------------------------------------------------------------+
178 | 3 | Operation is allowed and physically present user required. |
179 +----------+-------------------------------------------------------------+
180 | 4 | Operation is allowed and physically present user is not |
182 +----------+-------------------------------------------------------------+
184 The location of the table is given by the fw_cfg ``tpmppi_address``
185 field. The PPI memory region size is 0x400 (``TPM_PPI_ADDR_SIZE``) to
186 leave enough room for future updates.
188 QEMU files related to TPM ACPI tables:
189 - ``hw/i386/acpi-build.c``
190 - ``include/hw/acpi/tpm.h``
195 The TPM implementation is split into two parts, frontend and
196 backend. The frontend part is the hardware interface, such as the TPM
197 TIS interface described earlier, and the other part is the TPM backend
198 interface. The backend interfaces implement the interaction with a TPM
199 device, which may be a physical or an emulated device. The split
200 between the front- and backend devices allows a frontend to be
201 connected with any available backend. This enables the TIS interface
202 to be used with the passthrough backend or the swtpm backend.
204 QEMU files related to TPM backends:
206 - ``include/sysemu/tpm.h``
207 - ``include/sysemu/tpm_backend.h``
209 The QEMU TPM passthrough device
210 -------------------------------
212 In case QEMU is run on Linux as the host operating system it is
213 possible to make the hardware TPM device available to a single QEMU
214 guest. In this case the user must make sure that no other program is
215 using the device, e.g., /dev/tpm0, before trying to start QEMU with
218 The passthrough driver uses the host's TPM device for sending TPM
219 commands and receiving responses from. Besides that it accesses the
220 TPM device's sysfs entry for support of command cancellation. Since
221 none of the state of a hardware TPM can be migrated between hosts,
222 virtual machine migration is disabled when the TPM passthrough driver
225 Since the host's TPM device will already be initialized by the host's
226 firmware, certain commands, e.g. ``TPM_Startup()``, sent by the
227 virtual firmware for device initialization, will fail. In this case
228 the firmware should not use the TPM.
230 Sharing the device with the host is generally not a recommended usage
231 scenario for a TPM device. The primary reason for this is that two
232 operating systems can then access the device's single set of
233 resources, such as platform configuration registers
234 (PCRs). Applications or kernel security subsystems, such as the Linux
235 Integrity Measurement Architecture (IMA), are not expecting to share
238 QEMU files related to the TPM passthrough device:
239 - ``backends/tpm/tpm_passthrough.c``
240 - ``backends/tpm/tpm_util.c``
241 - ``include/sysemu/tpm_util.h``
244 Command line to start QEMU with the TPM passthrough device using the host's
245 hardware TPM ``/dev/tpm0``:
247 .. code-block:: console
249 qemu-system-x86_64 -display sdl -accel kvm \
250 -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
251 -tpmdev passthrough,id=tpm0,path=/dev/tpm0 \
252 -device tpm-tis,tpmdev=tpm0 test.img
255 The following commands should result in similar output inside the VM
256 with a Linux kernel that either has the TPM TIS driver built-in or
257 available as a module (assuming a TPM 2 is passed through):
259 .. code-block:: console
261 # dmesg | grep -i tpm
262 [ 0.012560] ACPI: TPM2 0x000000000BFFD1900 00004C (v04 BOCHS \
263 BXPC 0000001 BXPC 00000001)
266 crw-rw----. 1 tss root 10, 224 Sep 6 12:36 /dev/tpm0
267 crw-rw----. 1 tss rss 253, 65536 Sep 6 12:36 /dev/tpmrm0
269 Starting with Linux 5.12 there are PCR entries for TPM 2 in sysfs:
270 # find /sys/devices/ -type f | grep pcr-sha
272 /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/1
274 /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/9
277 The QEMU TPM emulator device
278 ----------------------------
280 The TPM emulator device uses an external TPM emulator called 'swtpm'
281 for sending TPM commands to and receiving responses from. The swtpm
282 program must have been started before trying to access it through the
283 TPM emulator with QEMU.
285 The TPM emulator implements a command channel for transferring TPM
286 commands and responses as well as a control channel over which control
287 commands can be sent. (see the `SWTPM protocol`_ specification)
289 The control channel serves the purpose of resetting, initializing, and
290 migrating the TPM state, among other things.
292 The swtpm program behaves like a hardware TPM and therefore needs to
293 be initialized by the firmware running inside the QEMU virtual
294 machine. One necessary step for initializing the device is to send
295 the TPM_Startup command to it. SeaBIOS, for example, has been
296 instrumented to initialize a TPM 1.2 or TPM 2 device using this
299 QEMU files related to the TPM emulator device:
300 - ``backends/tpm/tpm_emulator.c``
301 - ``backends/tpm/tpm_util.c``
302 - ``include/sysemu/tpm_util.h``
304 The following commands start the swtpm with a UnixIO control channel over
305 a socket interface. They do not need to be run as root.
307 .. code-block:: console
310 swtpm socket --tpmstate dir=/tmp/mytpm1 \
311 --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
315 Command line to start QEMU with the TPM emulator device communicating
316 with the swtpm (x86):
318 .. code-block:: console
320 qemu-system-x86_64 -display sdl -accel kvm \
321 -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
322 -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
323 -tpmdev emulator,id=tpm0,chardev=chrtpm \
324 -device tpm-tis,tpmdev=tpm0 test.img
326 In case a pSeries machine is emulated, use the following command line:
328 .. code-block:: console
330 qemu-system-ppc64 -display sdl -machine pseries,accel=kvm \
331 -m 1024 -bios slof.bin -boot menu=on \
332 -nodefaults -device VGA -device pci-ohci -device usb-kbd \
333 -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
334 -tpmdev emulator,id=tpm0,chardev=chrtpm \
335 -device tpm-spapr,tpmdev=tpm0 \
336 -device spapr-vscsi,id=scsi0,reg=0x00002000 \
337 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x3,drive=drive-virtio-disk0,id=virtio-disk0 \
338 -drive file=test.img,format=raw,if=none,id=drive-virtio-disk0
340 In case an Arm virt machine is emulated, use the following command line:
342 .. code-block:: console
344 qemu-system-aarch64 -machine virt,gic-version=3,accel=kvm \
346 -nographic -no-acpi \
347 -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
348 -tpmdev emulator,id=tpm0,chardev=chrtpm \
349 -device tpm-tis-device,tpmdev=tpm0 \
350 -device virtio-blk-pci,drive=drv0 \
351 -drive format=qcow2,file=hda.qcow2,if=none,id=drv0 \
352 -drive if=pflash,format=raw,file=flash0.img,readonly=on \
353 -drive if=pflash,format=raw,file=flash1.img
355 In case a ast2600-evb bmc machine is emulated and you want to use a TPM device
356 attached to I2C bus, use the following command line:
358 .. code-block:: console
360 qemu-system-arm -M ast2600-evb -nographic \
361 -kernel arch/arm/boot/zImage \
362 -dtb arch/arm/boot/dts/aspeed-ast2600-evb.dtb \
363 -initrd rootfs.cpio \
364 -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
365 -tpmdev emulator,id=tpm0,chardev=chrtpm \
366 -device tpm-tis-i2c,tpmdev=tpm0,bus=aspeed.i2c.bus.12,address=0x2e
368 For testing, use this command to load the driver to the correct address
370 echo tpm_tis_i2c 0x2e > /sys/bus/i2c/devices/i2c-12/new_device
372 In case SeaBIOS is used as firmware, it should show the TPM menu item
373 after entering the menu with 'ESC'.
375 .. code-block:: console
378 1. DVD/CD [ata1-0: QEMU DVD-ROM ATAPI-4 DVD/CD]
384 The following commands should result in similar output inside the VM
385 with a Linux kernel that either has the TPM TIS driver built-in or
386 available as a module:
388 .. code-block:: console
390 # dmesg | grep -i tpm
391 [ 0.012560] ACPI: TPM2 0x000000000BFFD1900 00004C (v04 BOCHS \
392 BXPC 0000001 BXPC 00000001)
395 crw-rw----. 1 tss root 10, 224 Sep 6 12:36 /dev/tpm0
396 crw-rw----. 1 tss rss 253, 65536 Sep 6 12:36 /dev/tpmrm0
398 Starting with Linux 5.12 there are PCR entries for TPM 2 in sysfs:
399 # find /sys/devices/ -type f | grep pcr-sha
401 /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/1
403 /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/9
406 Migration with the TPM emulator
407 ===============================
409 The TPM emulator supports the following types of virtual machine
412 - VM save / restore (migration into a file)
414 - Snapshotting (migration into storage like QoW2 or QED)
416 The following command sequences can be used to test VM save / restore.
418 In a 1st terminal start an instance of a swtpm using the following command:
420 .. code-block:: console
423 swtpm socket --tpmstate dir=/tmp/mytpm1 \
424 --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
428 In a 2nd terminal start the VM:
430 .. code-block:: console
432 qemu-system-x86_64 -display sdl -accel kvm \
433 -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
434 -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
435 -tpmdev emulator,id=tpm0,chardev=chrtpm \
436 -device tpm-tis,tpmdev=tpm0 \
440 Verify that the attached TPM is working as expected using applications
443 To store the state of the VM use the following command in the QEMU
444 monitor in the 2nd terminal:
446 .. code-block:: console
448 (qemu) migrate "exec:cat > testvm.bin"
451 At this point a file called ``testvm.bin`` should exists and the swtpm
452 and QEMU processes should have ended.
454 To test 'VM restore' you have to start the swtpm with the same
455 parameters as before. If previously a TPM 2 [--tpm2] was saved, --tpm2
456 must now be passed again on the command line.
458 In the 1st terminal restart the swtpm with the same command line as
461 .. code-block:: console
463 swtpm socket --tpmstate dir=/tmp/mytpm1 \
464 --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
465 --log level=20 --tpm2
467 In the 2nd terminal restore the state of the VM using the additional
470 .. code-block:: console
472 qemu-system-x86_64 -display sdl -accel kvm \
473 -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
474 -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
475 -tpmdev emulator,id=tpm0,chardev=chrtpm \
476 -device tpm-tis,tpmdev=tpm0 \
477 -incoming "exec:cat < testvm.bin" \
480 Troubleshooting migration
481 -------------------------
483 There are several reasons why migration may fail. In case of problems,
484 please ensure that the command lines adhere to the following rules
485 and, if possible, that identical versions of QEMU and swtpm are used
490 - QEMU command line parameters should be identical apart from the
491 '-incoming' option on VM restore
493 - swtpm command line parameters should be identical
495 VM migration to 'localhost':
497 - QEMU command line parameters should be identical apart from the
498 '-incoming' option on the destination side
500 - swtpm command line parameters should point to two different
501 directories on the source and destination swtpm (--tpmstate dir=...)
502 (especially if different versions of libtpms were to be used on the
505 VM migration across the network:
507 - QEMU command line parameters should be identical apart from the
508 '-incoming' option on the destination side
510 - swtpm command line parameters should be identical
513 - QEMU command line parameters should be identical
515 - swtpm command line parameters should be identical
518 Besides that, migration failure reasons on the swtpm level may include
521 - the versions of the swtpm on the source and destination sides are
524 - downgrading of TPM state may not be supported
526 - the source and destination libtpms were compiled with different
527 compile-time options and the destination side refuses to accept the
530 - different migration keys are used on the source and destination side
531 and the destination side cannot decrypt the migrated state
532 (swtpm ... --migration-key ... )
535 .. _TIS specification:
536 https://trustedcomputinggroup.org/pc-client-work-group-pc-client-specific-tpm-interface-specification-tis/
538 .. _CRB specification:
539 https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
542 .. _ACPI specification:
543 https://trustedcomputinggroup.org/tcg-acpi-specification/
545 .. _PPI specification:
546 https://trustedcomputinggroup.org/resource/tcg-physical-presence-interface-specification/
549 https://github.com/stefanberger/swtpm/blob/master/man/man3/swtpm_ioctls.pod