xref: /openbmc/qemu/docs/specs/tpm.rst (revision f1572ab94738bd5787b7badcd4bd93a3657f0680)
106ecb6acSAlex Bennée.. _tpm-device:
206ecb6acSAlex Bennée
36e8a3ff6SMarc-André Lureau===============
46e8a3ff6SMarc-André LureauQEMU TPM Device
56e8a3ff6SMarc-André Lureau===============
66e8a3ff6SMarc-André Lureau
76e8a3ff6SMarc-André LureauGuest-side hardware interface
86e8a3ff6SMarc-André Lureau=============================
96e8a3ff6SMarc-André Lureau
106e8a3ff6SMarc-André LureauTIS interface
116e8a3ff6SMarc-André Lureau-------------
126e8a3ff6SMarc-André Lureau
136e8a3ff6SMarc-André LureauThe QEMU TPM emulation implements a TPM TIS hardware interface
146e8a3ff6SMarc-André Lureaufollowing the Trusted Computing Group's specification "TCG PC Client
156e8a3ff6SMarc-André LureauSpecific TPM Interface Specification (TIS)", Specification Version
166e8a3ff6SMarc-André Lureau1.3, 21 March 2013. (see the `TIS specification`_, or a later version
176e8a3ff6SMarc-André Lureauof it).
186e8a3ff6SMarc-André Lureau
196e8a3ff6SMarc-André LureauThe TIS interface makes a memory mapped IO region in the area
206e8a3ff6SMarc-André Lureau0xfed40000-0xfed44fff available to the guest operating system.
216e8a3ff6SMarc-André Lureau
226e8a3ff6SMarc-André LureauQEMU files related to TPM TIS interface:
23fcaa2041SEric Auger - ``hw/tpm/tpm_tis_common.c``
24fcaa2041SEric Auger - ``hw/tpm/tpm_tis_isa.c``
25fcaa2041SEric Auger - ``hw/tpm/tpm_tis_sysbus.c``
26fbd945e7SNinad Palsule - ``hw/tpm/tpm_tis_i2c.c``
276e8a3ff6SMarc-André Lureau - ``hw/tpm/tpm_tis.h``
286e8a3ff6SMarc-André Lureau
29fcaa2041SEric AugerBoth an ISA device and a sysbus device are available. The former is
30fcaa2041SEric Augerused with pc/q35 machine while the latter can be instantiated in the
316fe6d6c9SPeter MaydellArm virt machine.
32fcaa2041SEric Auger
33fbd945e7SNinad PalsuleAn I2C device support is also provided which can be instantiated in the Arm
34fbd945e7SNinad Palsulebased emulation machines. This device only supports the TPM 2 protocol.
35fbd945e7SNinad Palsule
366e8a3ff6SMarc-André LureauCRB interface
376e8a3ff6SMarc-André Lureau-------------
386e8a3ff6SMarc-André Lureau
396e8a3ff6SMarc-André LureauQEMU also implements a TPM CRB interface following the Trusted
406e8a3ff6SMarc-André LureauComputing Group's specification "TCG PC Client Platform TPM Profile
416e8a3ff6SMarc-André Lureau(PTP) Specification", Family "2.0", Level 00 Revision 01.03 v22, May
426e8a3ff6SMarc-André Lureau22, 2017. (see the `CRB specification`_, or a later version of it)
436e8a3ff6SMarc-André Lureau
446e8a3ff6SMarc-André LureauThe CRB interface makes a memory mapped IO region in the area
456e8a3ff6SMarc-André Lureau0xfed40000-0xfed40fff (1 locality) available to the guest
466e8a3ff6SMarc-André Lureauoperating system.
476e8a3ff6SMarc-André Lureau
486e8a3ff6SMarc-André LureauQEMU files related to TPM CRB interface:
496e8a3ff6SMarc-André Lureau - ``hw/tpm/tpm_crb.c``
506e8a3ff6SMarc-André Lureau
516e8a3ff6SMarc-André LureauSPAPR interface
526e8a3ff6SMarc-André Lureau---------------
536e8a3ff6SMarc-André Lureau
546e8a3ff6SMarc-André LureaupSeries (ppc64) machines offer a tpm-spapr device model.
556e8a3ff6SMarc-André Lureau
566e8a3ff6SMarc-André LureauQEMU files related to the SPAPR interface:
576e8a3ff6SMarc-André Lureau - ``hw/tpm/tpm_spapr.c``
586e8a3ff6SMarc-André Lureau
596e8a3ff6SMarc-André Lureaufw_cfg interface
606e8a3ff6SMarc-André Lureau================
616e8a3ff6SMarc-André Lureau
626e8a3ff6SMarc-André LureauThe bios/firmware may read the ``"etc/tpm/config"`` fw_cfg entry for
636e8a3ff6SMarc-André Lureauconfiguring the guest appropriately.
646e8a3ff6SMarc-André Lureau
656e8a3ff6SMarc-André LureauThe entry of 6 bytes has the following content, in little-endian:
666e8a3ff6SMarc-André Lureau
676e8a3ff6SMarc-André Lureau.. code-block:: c
686e8a3ff6SMarc-André Lureau
696e8a3ff6SMarc-André Lureau    #define TPM_VERSION_UNSPEC          0
706e8a3ff6SMarc-André Lureau    #define TPM_VERSION_1_2             1
716e8a3ff6SMarc-André Lureau    #define TPM_VERSION_2_0             2
726e8a3ff6SMarc-André Lureau
736e8a3ff6SMarc-André Lureau    #define TPM_PPI_VERSION_NONE        0
746e8a3ff6SMarc-André Lureau    #define TPM_PPI_VERSION_1_30        1
756e8a3ff6SMarc-André Lureau
766e8a3ff6SMarc-André Lureau    struct FwCfgTPMConfig {
776e8a3ff6SMarc-André Lureau        uint32_t tpmppi_address;         /* PPI memory location */
786e8a3ff6SMarc-André Lureau        uint8_t tpm_version;             /* TPM version */
796e8a3ff6SMarc-André Lureau        uint8_t tpmppi_version;          /* PPI version */
806e8a3ff6SMarc-André Lureau    };
816e8a3ff6SMarc-André Lureau
826e8a3ff6SMarc-André LureauACPI interface
836e8a3ff6SMarc-André Lureau==============
846e8a3ff6SMarc-André Lureau
856e8a3ff6SMarc-André LureauThe TPM device is defined with ACPI ID "PNP0C31". QEMU builds a SSDT
866e8a3ff6SMarc-André Lureauand passes it into the guest through the fw_cfg device. The device
876e8a3ff6SMarc-André Lureaudescription contains the base address of the TIS interface 0xfed40000
886e8a3ff6SMarc-André Lureauand the size of the MMIO area (0x5000). In case a TPM2 is used by
896e8a3ff6SMarc-André LureauQEMU, a TPM2 ACPI table is also provided.  The device is described to
906e8a3ff6SMarc-André Lureaube used in polling mode rather than interrupt mode primarily because
916e8a3ff6SMarc-André Lureauno unused IRQ could be found.
926e8a3ff6SMarc-André Lureau
936e8a3ff6SMarc-André LureauTo support measurement logs to be written by the firmware,
946e8a3ff6SMarc-André Lureaue.g. SeaBIOS, a TCPA table is implemented. This table provides a 64kb
956e8a3ff6SMarc-André Lureaubuffer where the firmware can write its log into. For TPM 2 only a
966e8a3ff6SMarc-André Lureaumore recent version of the TPM2 table provides support for
976e8a3ff6SMarc-André Lureaumeasurements logs and a TCPA table does not need to be created.
986e8a3ff6SMarc-André Lureau
996e8a3ff6SMarc-André LureauThe TCPA and TPM2 ACPI tables follow the Trusted Computing Group
1006e8a3ff6SMarc-André Lureauspecification "TCG ACPI Specification" Family "1.2" and "2.0", Level
1016e8a3ff6SMarc-André Lureau00 Revision 00.37. (see the `ACPI specification`_, or a later version
1026e8a3ff6SMarc-André Lureauof it)
1036e8a3ff6SMarc-André Lureau
1046e8a3ff6SMarc-André LureauACPI PPI Interface
1056e8a3ff6SMarc-André Lureau------------------
1066e8a3ff6SMarc-André Lureau
1076e8a3ff6SMarc-André LureauQEMU supports the Physical Presence Interface (PPI) for TPM 1.2 and
1086e8a3ff6SMarc-André LureauTPM 2. This interface requires ACPI and firmware support. (see the
1096e8a3ff6SMarc-André Lureau`PPI specification`_)
1106e8a3ff6SMarc-André Lureau
1116e8a3ff6SMarc-André LureauPPI enables a system administrator (root) to request a modification to
1126e8a3ff6SMarc-André Lureauthe TPM upon reboot. The PPI specification defines the operation
1136e8a3ff6SMarc-André Lureaurequests and the actions the firmware has to take. The system
1146e8a3ff6SMarc-André Lureauadministrator passes the operation request number to the firmware
1156e8a3ff6SMarc-André Lureauthrough an ACPI interface which writes this number to a memory
1166e8a3ff6SMarc-André Lureaulocation that the firmware knows. Upon reboot, the firmware finds the
1176e8a3ff6SMarc-André Lureaunumber and sends commands to the TPM. The firmware writes the TPM
1186e8a3ff6SMarc-André Lureauresult code and the operation request number to a memory location that
1196e8a3ff6SMarc-André LureauACPI can read from and pass the result on to the administrator.
1206e8a3ff6SMarc-André Lureau
1216e8a3ff6SMarc-André LureauThe PPI specification defines a set of mandatory and optional
1226e8a3ff6SMarc-André Lureauoperations for the firmware to implement. The ACPI interface also
1236e8a3ff6SMarc-André Lureauallows an administrator to list the supported operations. In QEMU the
1246e8a3ff6SMarc-André LureauACPI code is generated by QEMU, yet the firmware needs to implement
1256e8a3ff6SMarc-André Lureausupport on a per-operations basis, and different firmwares may support
1266e8a3ff6SMarc-André Lureaua different subset. Therefore, QEMU introduces the virtual memory
1276e8a3ff6SMarc-André Lureaudevice for PPI where the firmware can indicate which operations it
1286e8a3ff6SMarc-André Lureausupports and ACPI can enable the ones that are supported and disable
1296e8a3ff6SMarc-André Lureauall others. This interface lies in main memory and has the following
1306e8a3ff6SMarc-André Lureaulayout:
1316e8a3ff6SMarc-André Lureau
1326e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1336e8a3ff6SMarc-André Lureau |  Field      | Length | Offset | Description                               |
1346e8a3ff6SMarc-André Lureau +=============+========+========+===========================================+
1356e8a3ff6SMarc-André Lureau | ``func``    |  0x100 |  0x000 | Firmware sets values for each supported   |
1366e8a3ff6SMarc-André Lureau |             |        |        | operation. See defined values below.      |
1376e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1386e8a3ff6SMarc-André Lureau | ``ppin``    |   0x1  |  0x100 | SMI interrupt to use. Set by firmware.    |
1396e8a3ff6SMarc-André Lureau |             |        |        | Not supported.                            |
1406e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1416e8a3ff6SMarc-André Lureau | ``ppip``    |   0x4  |  0x101 | ACPI function index to pass to SMM code.  |
1426e8a3ff6SMarc-André Lureau |             |        |        | Set by ACPI. Not supported.               |
1436e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1446e8a3ff6SMarc-André Lureau | ``pprp``    |   0x4  |  0x105 | Result of last executed operation. Set by |
1456e8a3ff6SMarc-André Lureau |             |        |        | firmware. See function index 5 for values.|
1466e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1476e8a3ff6SMarc-André Lureau | ``pprq``    |   0x4  |  0x109 | Operation request number to execute. See  |
1486e8a3ff6SMarc-André Lureau |             |        |        | 'Physical Presence Interface Operation    |
1496e8a3ff6SMarc-André Lureau |             |        |        | Summary' tables in specs. Set by ACPI.    |
1506e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1516e8a3ff6SMarc-André Lureau | ``pprm``    |   0x4  |  0x10d | Operation request optional parameter.     |
1526e8a3ff6SMarc-André Lureau |             |        |        | Values depend on operation. Set by ACPI.  |
1536e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1546e8a3ff6SMarc-André Lureau | ``lppr``    |   0x4  |  0x111 | Last executed operation request number.   |
1556e8a3ff6SMarc-André Lureau |             |        |        | Copied from pprq field by firmware.       |
1566e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1576e8a3ff6SMarc-André Lureau | ``fret``    |   0x4  |  0x115 | Result code from SMM function.            |
1586e8a3ff6SMarc-André Lureau |             |        |        | Not supported.                            |
1596e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1606e8a3ff6SMarc-André Lureau | ``res1``    |  0x40  |  0x119 | Reserved for future use                   |
1616e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1626e8a3ff6SMarc-André Lureau |``next_step``|   0x1  |  0x159 | Operation to execute after reboot by      |
1636e8a3ff6SMarc-André Lureau |             |        |        | firmware. Used by firmware.               |
1646e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1656e8a3ff6SMarc-André Lureau | ``movv``    |   0x1  |  0x15a | Memory overwrite variable                 |
1666e8a3ff6SMarc-André Lureau +-------------+--------+--------+-------------------------------------------+
1676e8a3ff6SMarc-André Lureau
1686e8a3ff6SMarc-André LureauThe following values are supported for the ``func`` field. They
1696e8a3ff6SMarc-André Lureaucorrespond to the values used by ACPI function index 8.
1706e8a3ff6SMarc-André Lureau
1716e8a3ff6SMarc-André Lureau +----------+-------------------------------------------------------------+
1726e8a3ff6SMarc-André Lureau | Value    | Description                                                 |
1736e8a3ff6SMarc-André Lureau +==========+=============================================================+
1746e8a3ff6SMarc-André Lureau | 0        | Operation is not implemented.                               |
1756e8a3ff6SMarc-André Lureau +----------+-------------------------------------------------------------+
1766e8a3ff6SMarc-André Lureau | 1        | Operation is only accessible through firmware.              |
1776e8a3ff6SMarc-André Lureau +----------+-------------------------------------------------------------+
1786e8a3ff6SMarc-André Lureau | 2        | Operation is blocked for OS by firmware configuration.      |
1796e8a3ff6SMarc-André Lureau +----------+-------------------------------------------------------------+
1806e8a3ff6SMarc-André Lureau | 3        | Operation is allowed and physically present user required.  |
1816e8a3ff6SMarc-André Lureau +----------+-------------------------------------------------------------+
1826e8a3ff6SMarc-André Lureau | 4        | Operation is allowed and physically present user is not     |
1836e8a3ff6SMarc-André Lureau |          | required.                                                   |
1846e8a3ff6SMarc-André Lureau +----------+-------------------------------------------------------------+
1856e8a3ff6SMarc-André Lureau
1866e8a3ff6SMarc-André LureauThe location of the table is given by the fw_cfg ``tpmppi_address``
1876e8a3ff6SMarc-André Lureaufield.  The PPI memory region size is 0x400 (``TPM_PPI_ADDR_SIZE``) to
1886e8a3ff6SMarc-André Lureauleave enough room for future updates.
1896e8a3ff6SMarc-André Lureau
1906e8a3ff6SMarc-André LureauQEMU files related to TPM ACPI tables:
1916e8a3ff6SMarc-André Lureau - ``hw/i386/acpi-build.c``
1926e8a3ff6SMarc-André Lureau - ``include/hw/acpi/tpm.h``
1936e8a3ff6SMarc-André Lureau
1946e8a3ff6SMarc-André LureauTPM backend devices
1956e8a3ff6SMarc-André Lureau===================
1966e8a3ff6SMarc-André Lureau
1976e8a3ff6SMarc-André LureauThe TPM implementation is split into two parts, frontend and
1986e8a3ff6SMarc-André Lureaubackend. The frontend part is the hardware interface, such as the TPM
1996e8a3ff6SMarc-André LureauTIS interface described earlier, and the other part is the TPM backend
2006e8a3ff6SMarc-André Lureauinterface. The backend interfaces implement the interaction with a TPM
2016e8a3ff6SMarc-André Lureaudevice, which may be a physical or an emulated device. The split
2026e8a3ff6SMarc-André Lureaubetween the front- and backend devices allows a frontend to be
2036e8a3ff6SMarc-André Lureauconnected with any available backend. This enables the TIS interface
2046e8a3ff6SMarc-André Lureauto be used with the passthrough backend or the swtpm backend.
2056e8a3ff6SMarc-André Lureau
2066e8a3ff6SMarc-André LureauQEMU files related to TPM backends:
2076e8a3ff6SMarc-André Lureau - ``backends/tpm.c``
2088a5fee03SPhilippe Mathieu-Daudé - ``include/sysemu/tpm.h``
2096e8a3ff6SMarc-André Lureau - ``include/sysemu/tpm_backend.h``
2106e8a3ff6SMarc-André Lureau
2116e8a3ff6SMarc-André LureauThe QEMU TPM passthrough device
2126e8a3ff6SMarc-André Lureau-------------------------------
2136e8a3ff6SMarc-André Lureau
2146e8a3ff6SMarc-André LureauIn case QEMU is run on Linux as the host operating system it is
2156e8a3ff6SMarc-André Lureaupossible to make the hardware TPM device available to a single QEMU
2166e8a3ff6SMarc-André Lureauguest. In this case the user must make sure that no other program is
2176e8a3ff6SMarc-André Lureauusing the device, e.g., /dev/tpm0, before trying to start QEMU with
2186e8a3ff6SMarc-André Lureauit.
2196e8a3ff6SMarc-André Lureau
2206e8a3ff6SMarc-André LureauThe passthrough driver uses the host's TPM device for sending TPM
2216e8a3ff6SMarc-André Lureaucommands and receiving responses from. Besides that it accesses the
2226e8a3ff6SMarc-André LureauTPM device's sysfs entry for support of command cancellation. Since
2236e8a3ff6SMarc-André Lureaunone of the state of a hardware TPM can be migrated between hosts,
2246e8a3ff6SMarc-André Lureauvirtual machine migration is disabled when the TPM passthrough driver
2256e8a3ff6SMarc-André Lureauis used.
2266e8a3ff6SMarc-André Lureau
2276e8a3ff6SMarc-André LureauSince the host's TPM device will already be initialized by the host's
2286e8a3ff6SMarc-André Lureaufirmware, certain commands, e.g. ``TPM_Startup()``, sent by the
2296e8a3ff6SMarc-André Lureauvirtual firmware for device initialization, will fail. In this case
2306e8a3ff6SMarc-André Lureauthe firmware should not use the TPM.
2316e8a3ff6SMarc-André Lureau
2326e8a3ff6SMarc-André LureauSharing the device with the host is generally not a recommended usage
2336e8a3ff6SMarc-André Lureauscenario for a TPM device. The primary reason for this is that two
2346e8a3ff6SMarc-André Lureauoperating systems can then access the device's single set of
2356e8a3ff6SMarc-André Lureauresources, such as platform configuration registers
2366e8a3ff6SMarc-André Lureau(PCRs). Applications or kernel security subsystems, such as the Linux
2376e8a3ff6SMarc-André LureauIntegrity Measurement Architecture (IMA), are not expecting to share
2386e8a3ff6SMarc-André LureauPCRs.
2396e8a3ff6SMarc-André Lureau
2406e8a3ff6SMarc-André LureauQEMU files related to the TPM passthrough device:
241ca64b086SPhilippe Mathieu-Daudé - ``backends/tpm/tpm_passthrough.c``
242ca64b086SPhilippe Mathieu-Daudé - ``backends/tpm/tpm_util.c``
2430f7d2148SPhilippe Mathieu-Daudé - ``include/sysemu/tpm_util.h``
2446e8a3ff6SMarc-André Lureau
2456e8a3ff6SMarc-André Lureau
2466e8a3ff6SMarc-André LureauCommand line to start QEMU with the TPM passthrough device using the host's
2476e8a3ff6SMarc-André Lureauhardware TPM ``/dev/tpm0``:
2486e8a3ff6SMarc-André Lureau
2496e8a3ff6SMarc-André Lureau.. code-block:: console
2506e8a3ff6SMarc-André Lureau
2516e8a3ff6SMarc-André Lureau  qemu-system-x86_64 -display sdl -accel kvm \
2526e8a3ff6SMarc-André Lureau  -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
2536e8a3ff6SMarc-André Lureau  -tpmdev passthrough,id=tpm0,path=/dev/tpm0 \
2546e8a3ff6SMarc-André Lureau  -device tpm-tis,tpmdev=tpm0 test.img
2556e8a3ff6SMarc-André Lureau
2566e8a3ff6SMarc-André Lureau
2576e8a3ff6SMarc-André LureauThe following commands should result in similar output inside the VM
2586e8a3ff6SMarc-André Lureauwith a Linux kernel that either has the TPM TIS driver built-in or
2594a4a74bfSStefan Bergeravailable as a module (assuming a TPM 2 is passed through):
2606e8a3ff6SMarc-André Lureau
2616e8a3ff6SMarc-André Lureau.. code-block:: console
2626e8a3ff6SMarc-André Lureau
2636e8a3ff6SMarc-André Lureau  # dmesg | grep -i tpm
2644a4a74bfSStefan Berger  [    0.012560] ACPI: TPM2 0x000000000BFFD1900 00004C (v04 BOCHS  \
2654a4a74bfSStefan Berger      BXPC     0000001 BXPC 00000001)
2666e8a3ff6SMarc-André Lureau
2676e8a3ff6SMarc-André Lureau  # ls -l /dev/tpm*
2684a4a74bfSStefan Berger  crw-rw----. 1 tss root  10,   224 Sep  6 12:36 /dev/tpm0
2694a4a74bfSStefan Berger  crw-rw----. 1 tss rss  253, 65536 Sep  6 12:36 /dev/tpmrm0
2706e8a3ff6SMarc-André Lureau
2714a4a74bfSStefan Berger  Starting with Linux 5.12 there are PCR entries for TPM 2 in sysfs:
2724a4a74bfSStefan Berger  # find /sys/devices/ -type f | grep pcr-sha
2736e8a3ff6SMarc-André Lureau  ...
2744a4a74bfSStefan Berger  /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/1
2754a4a74bfSStefan Berger  ...
2764a4a74bfSStefan Berger  /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/9
2774a4a74bfSStefan Berger  ...
2786e8a3ff6SMarc-André Lureau
2796e8a3ff6SMarc-André LureauThe QEMU TPM emulator device
2806e8a3ff6SMarc-André Lureau----------------------------
2816e8a3ff6SMarc-André Lureau
2826e8a3ff6SMarc-André LureauThe TPM emulator device uses an external TPM emulator called 'swtpm'
2836e8a3ff6SMarc-André Lureaufor sending TPM commands to and receiving responses from. The swtpm
2846e8a3ff6SMarc-André Lureauprogram must have been started before trying to access it through the
2856e8a3ff6SMarc-André LureauTPM emulator with QEMU.
2866e8a3ff6SMarc-André Lureau
2876e8a3ff6SMarc-André LureauThe TPM emulator implements a command channel for transferring TPM
2886e8a3ff6SMarc-André Lureaucommands and responses as well as a control channel over which control
2896e8a3ff6SMarc-André Lureaucommands can be sent. (see the `SWTPM protocol`_ specification)
2906e8a3ff6SMarc-André Lureau
2916e8a3ff6SMarc-André LureauThe control channel serves the purpose of resetting, initializing, and
2926e8a3ff6SMarc-André Lureaumigrating the TPM state, among other things.
2936e8a3ff6SMarc-André Lureau
2946e8a3ff6SMarc-André LureauThe swtpm program behaves like a hardware TPM and therefore needs to
2956e8a3ff6SMarc-André Lureaube initialized by the firmware running inside the QEMU virtual
2966e8a3ff6SMarc-André Lureaumachine.  One necessary step for initializing the device is to send
2976e8a3ff6SMarc-André Lureauthe TPM_Startup command to it. SeaBIOS, for example, has been
2986e8a3ff6SMarc-André Lureauinstrumented to initialize a TPM 1.2 or TPM 2 device using this
2996e8a3ff6SMarc-André Lureaucommand.
3006e8a3ff6SMarc-André Lureau
3016e8a3ff6SMarc-André LureauQEMU files related to the TPM emulator device:
302ca64b086SPhilippe Mathieu-Daudé - ``backends/tpm/tpm_emulator.c``
303ca64b086SPhilippe Mathieu-Daudé - ``backends/tpm/tpm_util.c``
3040f7d2148SPhilippe Mathieu-Daudé - ``include/sysemu/tpm_util.h``
3056e8a3ff6SMarc-André Lureau
3066e8a3ff6SMarc-André LureauThe following commands start the swtpm with a UnixIO control channel over
3076e8a3ff6SMarc-André Lureaua socket interface. They do not need to be run as root.
3086e8a3ff6SMarc-André Lureau
3096e8a3ff6SMarc-André Lureau.. code-block:: console
3106e8a3ff6SMarc-André Lureau
3116e8a3ff6SMarc-André Lureau  mkdir /tmp/mytpm1
3126e8a3ff6SMarc-André Lureau  swtpm socket --tpmstate dir=/tmp/mytpm1 \
3136e8a3ff6SMarc-André Lureau    --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
3144a4a74bfSStefan Berger    --tpm2 \
3156e8a3ff6SMarc-André Lureau    --log level=20
3166e8a3ff6SMarc-André Lureau
3176e8a3ff6SMarc-André LureauCommand line to start QEMU with the TPM emulator device communicating
3186e8a3ff6SMarc-André Lureauwith the swtpm (x86):
3196e8a3ff6SMarc-André Lureau
3206e8a3ff6SMarc-André Lureau.. code-block:: console
3216e8a3ff6SMarc-André Lureau
3226e8a3ff6SMarc-André Lureau  qemu-system-x86_64 -display sdl -accel kvm \
3236e8a3ff6SMarc-André Lureau    -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
3246e8a3ff6SMarc-André Lureau    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
3256e8a3ff6SMarc-André Lureau    -tpmdev emulator,id=tpm0,chardev=chrtpm \
3266e8a3ff6SMarc-André Lureau    -device tpm-tis,tpmdev=tpm0 test.img
3276e8a3ff6SMarc-André Lureau
3286e8a3ff6SMarc-André LureauIn case a pSeries machine is emulated, use the following command line:
3296e8a3ff6SMarc-André Lureau
3306e8a3ff6SMarc-André Lureau.. code-block:: console
3316e8a3ff6SMarc-André Lureau
3326e8a3ff6SMarc-André Lureau  qemu-system-ppc64 -display sdl -machine pseries,accel=kvm \
3336e8a3ff6SMarc-André Lureau    -m 1024 -bios slof.bin -boot menu=on \
3346e8a3ff6SMarc-André Lureau    -nodefaults -device VGA -device pci-ohci -device usb-kbd \
3356e8a3ff6SMarc-André Lureau    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
3366e8a3ff6SMarc-André Lureau    -tpmdev emulator,id=tpm0,chardev=chrtpm \
3376e8a3ff6SMarc-André Lureau    -device tpm-spapr,tpmdev=tpm0 \
3386e8a3ff6SMarc-André Lureau    -device spapr-vscsi,id=scsi0,reg=0x00002000 \
339*993e3802SPaolo Bonzini    -device virtio-blk-pci,bus=pci.0,addr=0x3,drive=drive-virtio-disk0,id=virtio-disk0 \
3406e8a3ff6SMarc-André Lureau    -drive file=test.img,format=raw,if=none,id=drive-virtio-disk0
3416e8a3ff6SMarc-André Lureau
3426fe6d6c9SPeter MaydellIn case an Arm virt machine is emulated, use the following command line:
343fcaa2041SEric Auger
344fcaa2041SEric Auger.. code-block:: console
345fcaa2041SEric Auger
3464a641010SThomas Huth  qemu-system-aarch64 -machine virt,gic-version=3,acpi=off \
347fcaa2041SEric Auger    -cpu host -m 4G \
3484a641010SThomas Huth    -nographic -accel kvm \
349fcaa2041SEric Auger    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
350fcaa2041SEric Auger    -tpmdev emulator,id=tpm0,chardev=chrtpm \
351fcaa2041SEric Auger    -device tpm-tis-device,tpmdev=tpm0 \
352fcaa2041SEric Auger    -device virtio-blk-pci,drive=drv0 \
353fcaa2041SEric Auger    -drive format=qcow2,file=hda.qcow2,if=none,id=drv0 \
354991c180dSPaolo Bonzini    -drive if=pflash,format=raw,file=flash0.img,readonly=on \
355fcaa2041SEric Auger    -drive if=pflash,format=raw,file=flash1.img
356fcaa2041SEric Auger
357fbd945e7SNinad PalsuleIn case a ast2600-evb bmc machine is emulated and you want to use a TPM device
358fbd945e7SNinad Palsuleattached to I2C bus, use the following command line:
359fbd945e7SNinad Palsule
360fbd945e7SNinad Palsule.. code-block:: console
361fbd945e7SNinad Palsule
362fbd945e7SNinad Palsule  qemu-system-arm -M ast2600-evb -nographic \
363fbd945e7SNinad Palsule    -kernel arch/arm/boot/zImage \
364fbd945e7SNinad Palsule    -dtb arch/arm/boot/dts/aspeed-ast2600-evb.dtb \
365fbd945e7SNinad Palsule    -initrd rootfs.cpio \
366fbd945e7SNinad Palsule    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
367fbd945e7SNinad Palsule    -tpmdev emulator,id=tpm0,chardev=chrtpm \
368fbd945e7SNinad Palsule    -device tpm-tis-i2c,tpmdev=tpm0,bus=aspeed.i2c.bus.12,address=0x2e
369fbd945e7SNinad Palsule
370fbd945e7SNinad Palsule  For testing, use this command to load the driver to the correct address
371fbd945e7SNinad Palsule
372fbd945e7SNinad Palsule  echo tpm_tis_i2c 0x2e > /sys/bus/i2c/devices/i2c-12/new_device
373fbd945e7SNinad Palsule
3746e8a3ff6SMarc-André LureauIn case SeaBIOS is used as firmware, it should show the TPM menu item
3756e8a3ff6SMarc-André Lureauafter entering the menu with 'ESC'.
3766e8a3ff6SMarc-André Lureau
3776e8a3ff6SMarc-André Lureau.. code-block:: console
3786e8a3ff6SMarc-André Lureau
3796e8a3ff6SMarc-André Lureau  Select boot device:
3806e8a3ff6SMarc-André Lureau  1. DVD/CD [ata1-0: QEMU DVD-ROM ATAPI-4 DVD/CD]
3816e8a3ff6SMarc-André Lureau  [...]
3826e8a3ff6SMarc-André Lureau  5. Legacy option rom
3836e8a3ff6SMarc-André Lureau
3846e8a3ff6SMarc-André Lureau  t. TPM Configuration
3856e8a3ff6SMarc-André Lureau
3866e8a3ff6SMarc-André LureauThe following commands should result in similar output inside the VM
3876e8a3ff6SMarc-André Lureauwith a Linux kernel that either has the TPM TIS driver built-in or
3886e8a3ff6SMarc-André Lureauavailable as a module:
3896e8a3ff6SMarc-André Lureau
3906e8a3ff6SMarc-André Lureau.. code-block:: console
3916e8a3ff6SMarc-André Lureau
3926e8a3ff6SMarc-André Lureau  # dmesg | grep -i tpm
3934a4a74bfSStefan Berger  [    0.012560] ACPI: TPM2 0x000000000BFFD1900 00004C (v04 BOCHS  \
3944a4a74bfSStefan Berger      BXPC     0000001 BXPC 00000001)
3956e8a3ff6SMarc-André Lureau
3966e8a3ff6SMarc-André Lureau  # ls -l /dev/tpm*
3974a4a74bfSStefan Berger  crw-rw----. 1 tss root  10,   224 Sep  6 12:36 /dev/tpm0
3984a4a74bfSStefan Berger  crw-rw----. 1 tss rss  253, 65536 Sep  6 12:36 /dev/tpmrm0
3996e8a3ff6SMarc-André Lureau
4004a4a74bfSStefan Berger  Starting with Linux 5.12 there are PCR entries for TPM 2 in sysfs:
4014a4a74bfSStefan Berger  # find /sys/devices/ -type f | grep pcr-sha
4026e8a3ff6SMarc-André Lureau  ...
4034a4a74bfSStefan Berger  /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/1
4044a4a74bfSStefan Berger  ...
4054a4a74bfSStefan Berger  /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/9
4064a4a74bfSStefan Berger  ...
4076e8a3ff6SMarc-André Lureau
4086e8a3ff6SMarc-André LureauMigration with the TPM emulator
4096e8a3ff6SMarc-André Lureau===============================
4106e8a3ff6SMarc-André Lureau
4116e8a3ff6SMarc-André LureauThe TPM emulator supports the following types of virtual machine
4126e8a3ff6SMarc-André Lureaumigration:
4136e8a3ff6SMarc-André Lureau
4146e8a3ff6SMarc-André Lureau- VM save / restore (migration into a file)
4156e8a3ff6SMarc-André Lureau- Network migration
4166e8a3ff6SMarc-André Lureau- Snapshotting (migration into storage like QoW2 or QED)
4176e8a3ff6SMarc-André Lureau
4186e8a3ff6SMarc-André LureauThe following command sequences can be used to test VM save / restore.
4196e8a3ff6SMarc-André Lureau
4206e8a3ff6SMarc-André LureauIn a 1st terminal start an instance of a swtpm using the following command:
4216e8a3ff6SMarc-André Lureau
4226e8a3ff6SMarc-André Lureau.. code-block:: console
4236e8a3ff6SMarc-André Lureau
4246e8a3ff6SMarc-André Lureau  mkdir /tmp/mytpm1
4256e8a3ff6SMarc-André Lureau  swtpm socket --tpmstate dir=/tmp/mytpm1 \
4266e8a3ff6SMarc-André Lureau    --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
4274a4a74bfSStefan Berger    --tpm2 \
4284a4a74bfSStefan Berger    --log level=20
4296e8a3ff6SMarc-André Lureau
4306e8a3ff6SMarc-André LureauIn a 2nd terminal start the VM:
4316e8a3ff6SMarc-André Lureau
4326e8a3ff6SMarc-André Lureau.. code-block:: console
4336e8a3ff6SMarc-André Lureau
4346e8a3ff6SMarc-André Lureau  qemu-system-x86_64 -display sdl -accel kvm \
4356e8a3ff6SMarc-André Lureau    -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
4366e8a3ff6SMarc-André Lureau    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
4376e8a3ff6SMarc-André Lureau    -tpmdev emulator,id=tpm0,chardev=chrtpm \
4386e8a3ff6SMarc-André Lureau    -device tpm-tis,tpmdev=tpm0 \
4396e8a3ff6SMarc-André Lureau    -monitor stdio \
4406e8a3ff6SMarc-André Lureau    test.img
4416e8a3ff6SMarc-André Lureau
4426e8a3ff6SMarc-André LureauVerify that the attached TPM is working as expected using applications
4436e8a3ff6SMarc-André Lureauinside the VM.
4446e8a3ff6SMarc-André Lureau
4456e8a3ff6SMarc-André LureauTo store the state of the VM use the following command in the QEMU
4466e8a3ff6SMarc-André Lureaumonitor in the 2nd terminal:
4476e8a3ff6SMarc-André Lureau
4486e8a3ff6SMarc-André Lureau.. code-block:: console
4496e8a3ff6SMarc-André Lureau
4506e8a3ff6SMarc-André Lureau  (qemu) migrate "exec:cat > testvm.bin"
4516e8a3ff6SMarc-André Lureau  (qemu) quit
4526e8a3ff6SMarc-André Lureau
4536e8a3ff6SMarc-André LureauAt this point a file called ``testvm.bin`` should exists and the swtpm
4546e8a3ff6SMarc-André Lureauand QEMU processes should have ended.
4556e8a3ff6SMarc-André Lureau
4566e8a3ff6SMarc-André LureauTo test 'VM restore' you have to start the swtpm with the same
4576e8a3ff6SMarc-André Lureauparameters as before. If previously a TPM 2 [--tpm2] was saved, --tpm2
4586e8a3ff6SMarc-André Lureaumust now be passed again on the command line.
4596e8a3ff6SMarc-André Lureau
4606e8a3ff6SMarc-André LureauIn the 1st terminal restart the swtpm with the same command line as
4616e8a3ff6SMarc-André Lureaubefore:
4626e8a3ff6SMarc-André Lureau
4636e8a3ff6SMarc-André Lureau.. code-block:: console
4646e8a3ff6SMarc-André Lureau
4656e8a3ff6SMarc-André Lureau  swtpm socket --tpmstate dir=/tmp/mytpm1 \
4666e8a3ff6SMarc-André Lureau    --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
4676e8a3ff6SMarc-André Lureau    --log level=20 --tpm2
4686e8a3ff6SMarc-André Lureau
4696e8a3ff6SMarc-André LureauIn the 2nd terminal restore the state of the VM using the additional
4706e8a3ff6SMarc-André Lureau'-incoming' option.
4716e8a3ff6SMarc-André Lureau
4726e8a3ff6SMarc-André Lureau.. code-block:: console
4736e8a3ff6SMarc-André Lureau
4746e8a3ff6SMarc-André Lureau  qemu-system-x86_64 -display sdl -accel kvm \
4756e8a3ff6SMarc-André Lureau    -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
4766e8a3ff6SMarc-André Lureau    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
4776e8a3ff6SMarc-André Lureau    -tpmdev emulator,id=tpm0,chardev=chrtpm \
4786e8a3ff6SMarc-André Lureau    -device tpm-tis,tpmdev=tpm0 \
4796e8a3ff6SMarc-André Lureau    -incoming "exec:cat < testvm.bin" \
4806e8a3ff6SMarc-André Lureau    test.img
4816e8a3ff6SMarc-André Lureau
4826e8a3ff6SMarc-André LureauTroubleshooting migration
4836e8a3ff6SMarc-André Lureau-------------------------
4846e8a3ff6SMarc-André Lureau
4856e8a3ff6SMarc-André LureauThere are several reasons why migration may fail. In case of problems,
4866e8a3ff6SMarc-André Lureauplease ensure that the command lines adhere to the following rules
4876e8a3ff6SMarc-André Lureauand, if possible, that identical versions of QEMU and swtpm are used
4886e8a3ff6SMarc-André Lureauat all times.
4896e8a3ff6SMarc-André Lureau
4906e8a3ff6SMarc-André LureauVM save and restore:
4916e8a3ff6SMarc-André Lureau
4926e8a3ff6SMarc-André Lureau - QEMU command line parameters should be identical apart from the
4936e8a3ff6SMarc-André Lureau   '-incoming' option on VM restore
4946e8a3ff6SMarc-André Lureau
4956e8a3ff6SMarc-André Lureau - swtpm command line parameters should be identical
4966e8a3ff6SMarc-André Lureau
4976e8a3ff6SMarc-André LureauVM migration to 'localhost':
4986e8a3ff6SMarc-André Lureau
4996e8a3ff6SMarc-André Lureau - QEMU command line parameters should be identical apart from the
5006e8a3ff6SMarc-André Lureau   '-incoming' option on the destination side
5016e8a3ff6SMarc-André Lureau
5026e8a3ff6SMarc-André Lureau - swtpm command line parameters should point to two different
5036e8a3ff6SMarc-André Lureau   directories on the source and destination swtpm (--tpmstate dir=...)
5046e8a3ff6SMarc-André Lureau   (especially if different versions of libtpms were to be used on the
5056e8a3ff6SMarc-André Lureau   same machine).
5066e8a3ff6SMarc-André Lureau
5076e8a3ff6SMarc-André LureauVM migration across the network:
5086e8a3ff6SMarc-André Lureau
5096e8a3ff6SMarc-André Lureau - QEMU command line parameters should be identical apart from the
5106e8a3ff6SMarc-André Lureau   '-incoming' option on the destination side
5116e8a3ff6SMarc-André Lureau
5126e8a3ff6SMarc-André Lureau - swtpm command line parameters should be identical
5136e8a3ff6SMarc-André Lureau
5146e8a3ff6SMarc-André LureauVM Snapshotting:
5156e8a3ff6SMarc-André Lureau - QEMU command line parameters should be identical
5166e8a3ff6SMarc-André Lureau
5176e8a3ff6SMarc-André Lureau - swtpm command line parameters should be identical
5186e8a3ff6SMarc-André Lureau
5196e8a3ff6SMarc-André Lureau
5206e8a3ff6SMarc-André LureauBesides that, migration failure reasons on the swtpm level may include
5216e8a3ff6SMarc-André Lureauthe following:
5226e8a3ff6SMarc-André Lureau
5236e8a3ff6SMarc-André Lureau - the versions of the swtpm on the source and destination sides are
5246e8a3ff6SMarc-André Lureau   incompatible
5256e8a3ff6SMarc-André Lureau
5266e8a3ff6SMarc-André Lureau   - downgrading of TPM state may not be supported
5276e8a3ff6SMarc-André Lureau
5286e8a3ff6SMarc-André Lureau   - the source and destination libtpms were compiled with different
5296e8a3ff6SMarc-André Lureau     compile-time options and the destination side refuses to accept the
5306e8a3ff6SMarc-André Lureau     state
5316e8a3ff6SMarc-André Lureau
5326e8a3ff6SMarc-André Lureau - different migration keys are used on the source and destination side
5336e8a3ff6SMarc-André Lureau   and the destination side cannot decrypt the migrated state
5346e8a3ff6SMarc-André Lureau   (swtpm ... --migration-key ... )
5356e8a3ff6SMarc-André Lureau
5366e8a3ff6SMarc-André Lureau
5376e8a3ff6SMarc-André Lureau.. _TIS specification:
5386e8a3ff6SMarc-André Lureau   https://trustedcomputinggroup.org/pc-client-work-group-pc-client-specific-tpm-interface-specification-tis/
5396e8a3ff6SMarc-André Lureau
5406e8a3ff6SMarc-André Lureau.. _CRB specification:
5416e8a3ff6SMarc-André Lureau   https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
5426e8a3ff6SMarc-André Lureau
5436e8a3ff6SMarc-André Lureau
5446e8a3ff6SMarc-André Lureau.. _ACPI specification:
5456e8a3ff6SMarc-André Lureau   https://trustedcomputinggroup.org/tcg-acpi-specification/
5466e8a3ff6SMarc-André Lureau
5476e8a3ff6SMarc-André Lureau.. _PPI specification:
5486e8a3ff6SMarc-André Lureau   https://trustedcomputinggroup.org/resource/tcg-physical-presence-interface-specification/
5496e8a3ff6SMarc-André Lureau
5506e8a3ff6SMarc-André Lureau.. _SWTPM protocol:
5516e8a3ff6SMarc-André Lureau   https://github.com/stefanberger/swtpm/blob/master/man/man3/swtpm_ioctls.pod
552