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