xref: /openbmc/qemu/docs/specs/tpm.rst (revision 88cd34ee)
1===============
2QEMU TPM Device
3===============
4
5Guest-side hardware interface
6=============================
7
8TIS interface
9-------------
10
11The QEMU TPM emulation implements a TPM TIS hardware interface
12following the Trusted Computing Group's specification "TCG PC Client
13Specific TPM Interface Specification (TIS)", Specification Version
141.3, 21 March 2013. (see the `TIS specification`_, or a later version
15of it).
16
17The TIS interface makes a memory mapped IO region in the area
180xfed40000-0xfed44fff available to the guest operating system.
19
20QEMU files related to TPM TIS interface:
21 - ``hw/tpm/tpm_tis.c``
22 - ``hw/tpm/tpm_tis.h``
23
24CRB interface
25-------------
26
27QEMU also implements a TPM CRB interface following the Trusted
28Computing Group's specification "TCG PC Client Platform TPM Profile
29(PTP) Specification", Family "2.0", Level 00 Revision 01.03 v22, May
3022, 2017. (see the `CRB specification`_, or a later version of it)
31
32The CRB interface makes a memory mapped IO region in the area
330xfed40000-0xfed40fff (1 locality) available to the guest
34operating system.
35
36QEMU files related to TPM CRB interface:
37 - ``hw/tpm/tpm_crb.c``
38
39SPAPR interface
40---------------
41
42pSeries (ppc64) machines offer a tpm-spapr device model.
43
44QEMU files related to the SPAPR interface:
45 - ``hw/tpm/tpm_spapr.c``
46
47fw_cfg interface
48================
49
50The bios/firmware may read the ``"etc/tpm/config"`` fw_cfg entry for
51configuring the guest appropriately.
52
53The entry of 6 bytes has the following content, in little-endian:
54
55.. code-block:: c
56
57    #define TPM_VERSION_UNSPEC          0
58    #define TPM_VERSION_1_2             1
59    #define TPM_VERSION_2_0             2
60
61    #define TPM_PPI_VERSION_NONE        0
62    #define TPM_PPI_VERSION_1_30        1
63
64    struct FwCfgTPMConfig {
65        uint32_t tpmppi_address;         /* PPI memory location */
66        uint8_t tpm_version;             /* TPM version */
67        uint8_t tpmppi_version;          /* PPI version */
68    };
69
70ACPI interface
71==============
72
73The TPM device is defined with ACPI ID "PNP0C31". QEMU builds a SSDT
74and passes it into the guest through the fw_cfg device. The device
75description contains the base address of the TIS interface 0xfed40000
76and the size of the MMIO area (0x5000). In case a TPM2 is used by
77QEMU, a TPM2 ACPI table is also provided.  The device is described to
78be used in polling mode rather than interrupt mode primarily because
79no unused IRQ could be found.
80
81To support measurement logs to be written by the firmware,
82e.g. SeaBIOS, a TCPA table is implemented. This table provides a 64kb
83buffer where the firmware can write its log into. For TPM 2 only a
84more recent version of the TPM2 table provides support for
85measurements logs and a TCPA table does not need to be created.
86
87The TCPA and TPM2 ACPI tables follow the Trusted Computing Group
88specification "TCG ACPI Specification" Family "1.2" and "2.0", Level
8900 Revision 00.37. (see the `ACPI specification`_, or a later version
90of it)
91
92ACPI PPI Interface
93------------------
94
95QEMU supports the Physical Presence Interface (PPI) for TPM 1.2 and
96TPM 2. This interface requires ACPI and firmware support. (see the
97`PPI specification`_)
98
99PPI enables a system administrator (root) to request a modification to
100the TPM upon reboot. The PPI specification defines the operation
101requests and the actions the firmware has to take. The system
102administrator passes the operation request number to the firmware
103through an ACPI interface which writes this number to a memory
104location that the firmware knows. Upon reboot, the firmware finds the
105number and sends commands to the TPM. The firmware writes the TPM
106result code and the operation request number to a memory location that
107ACPI can read from and pass the result on to the administrator.
108
109The PPI specification defines a set of mandatory and optional
110operations for the firmware to implement. The ACPI interface also
111allows an administrator to list the supported operations. In QEMU the
112ACPI code is generated by QEMU, yet the firmware needs to implement
113support on a per-operations basis, and different firmwares may support
114a different subset. Therefore, QEMU introduces the virtual memory
115device for PPI where the firmware can indicate which operations it
116supports and ACPI can enable the ones that are supported and disable
117all others. This interface lies in main memory and has the following
118layout:
119
120 +-------------+--------+--------+-------------------------------------------+
121 |  Field      | Length | Offset | Description                               |
122 +=============+========+========+===========================================+
123 | ``func``    |  0x100 |  0x000 | Firmware sets values for each supported   |
124 |             |        |        | operation. See defined values below.      |
125 +-------------+--------+--------+-------------------------------------------+
126 | ``ppin``    |   0x1  |  0x100 | SMI interrupt to use. Set by firmware.    |
127 |             |        |        | Not supported.                            |
128 +-------------+--------+--------+-------------------------------------------+
129 | ``ppip``    |   0x4  |  0x101 | ACPI function index to pass to SMM code.  |
130 |             |        |        | Set by ACPI. Not supported.               |
131 +-------------+--------+--------+-------------------------------------------+
132 | ``pprp``    |   0x4  |  0x105 | Result of last executed operation. Set by |
133 |             |        |        | firmware. See function index 5 for values.|
134 +-------------+--------+--------+-------------------------------------------+
135 | ``pprq``    |   0x4  |  0x109 | Operation request number to execute. See  |
136 |             |        |        | 'Physical Presence Interface Operation    |
137 |             |        |        | Summary' tables in specs. Set by ACPI.    |
138 +-------------+--------+--------+-------------------------------------------+
139 | ``pprm``    |   0x4  |  0x10d | Operation request optional parameter.     |
140 |             |        |        | Values depend on operation. Set by ACPI.  |
141 +-------------+--------+--------+-------------------------------------------+
142 | ``lppr``    |   0x4  |  0x111 | Last executed operation request number.   |
143 |             |        |        | Copied from pprq field by firmware.       |
144 +-------------+--------+--------+-------------------------------------------+
145 | ``fret``    |   0x4  |  0x115 | Result code from SMM function.            |
146 |             |        |        | Not supported.                            |
147 +-------------+--------+--------+-------------------------------------------+
148 | ``res1``    |  0x40  |  0x119 | Reserved for future use                   |
149 +-------------+--------+--------+-------------------------------------------+
150 |``next_step``|   0x1  |  0x159 | Operation to execute after reboot by      |
151 |             |        |        | firmware. Used by firmware.               |
152 +-------------+--------+--------+-------------------------------------------+
153 | ``movv``    |   0x1  |  0x15a | Memory overwrite variable                 |
154 +-------------+--------+--------+-------------------------------------------+
155
156The following values are supported for the ``func`` field. They
157correspond to the values used by ACPI function index 8.
158
159 +----------+-------------------------------------------------------------+
160 | Value    | Description                                                 |
161 +==========+=============================================================+
162 | 0        | Operation is not implemented.                               |
163 +----------+-------------------------------------------------------------+
164 | 1        | Operation is only accessible through firmware.              |
165 +----------+-------------------------------------------------------------+
166 | 2        | Operation is blocked for OS by firmware configuration.      |
167 +----------+-------------------------------------------------------------+
168 | 3        | Operation is allowed and physically present user required.  |
169 +----------+-------------------------------------------------------------+
170 | 4        | Operation is allowed and physically present user is not     |
171 |          | required.                                                   |
172 +----------+-------------------------------------------------------------+
173
174The location of the table is given by the fw_cfg ``tpmppi_address``
175field.  The PPI memory region size is 0x400 (``TPM_PPI_ADDR_SIZE``) to
176leave enough room for future updates.
177
178QEMU files related to TPM ACPI tables:
179 - ``hw/i386/acpi-build.c``
180 - ``include/hw/acpi/tpm.h``
181
182TPM backend devices
183===================
184
185The TPM implementation is split into two parts, frontend and
186backend. The frontend part is the hardware interface, such as the TPM
187TIS interface described earlier, and the other part is the TPM backend
188interface. The backend interfaces implement the interaction with a TPM
189device, which may be a physical or an emulated device. The split
190between the front- and backend devices allows a frontend to be
191connected with any available backend. This enables the TIS interface
192to be used with the passthrough backend or the swtpm backend.
193
194QEMU files related to TPM backends:
195 - ``backends/tpm.c``
196 - ``include/sysemu/tpm_backend.h``
197 - ``include/sysemu/tpm_backend_int.h``
198
199The QEMU TPM passthrough device
200-------------------------------
201
202In case QEMU is run on Linux as the host operating system it is
203possible to make the hardware TPM device available to a single QEMU
204guest. In this case the user must make sure that no other program is
205using the device, e.g., /dev/tpm0, before trying to start QEMU with
206it.
207
208The passthrough driver uses the host's TPM device for sending TPM
209commands and receiving responses from. Besides that it accesses the
210TPM device's sysfs entry for support of command cancellation. Since
211none of the state of a hardware TPM can be migrated between hosts,
212virtual machine migration is disabled when the TPM passthrough driver
213is used.
214
215Since the host's TPM device will already be initialized by the host's
216firmware, certain commands, e.g. ``TPM_Startup()``, sent by the
217virtual firmware for device initialization, will fail. In this case
218the firmware should not use the TPM.
219
220Sharing the device with the host is generally not a recommended usage
221scenario for a TPM device. The primary reason for this is that two
222operating systems can then access the device's single set of
223resources, such as platform configuration registers
224(PCRs). Applications or kernel security subsystems, such as the Linux
225Integrity Measurement Architecture (IMA), are not expecting to share
226PCRs.
227
228QEMU files related to the TPM passthrough device:
229 - ``hw/tpm/tpm_passthrough.c``
230 - ``hw/tpm/tpm_util.c``
231 - ``hw/tpm/tpm_util.h``
232
233
234Command line to start QEMU with the TPM passthrough device using the host's
235hardware TPM ``/dev/tpm0``:
236
237.. code-block:: console
238
239  qemu-system-x86_64 -display sdl -accel kvm \
240  -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
241  -tpmdev passthrough,id=tpm0,path=/dev/tpm0 \
242  -device tpm-tis,tpmdev=tpm0 test.img
243
244
245The following commands should result in similar output inside the VM
246with a Linux kernel that either has the TPM TIS driver built-in or
247available as a module:
248
249.. code-block:: console
250
251  # dmesg | grep -i tpm
252  [    0.711310] tpm_tis 00:06: 1.2 TPM (device=id 0x1, rev-id 1)
253
254  # dmesg | grep TCPA
255  [    0.000000] ACPI: TCPA 0x0000000003FFD191C 000032 (v02 BOCHS  \
256      BXPCTCPA 0000001 BXPC 00000001)
257
258  # ls -l /dev/tpm*
259  crw-------. 1 root root 10, 224 Jul 11 10:11 /dev/tpm0
260
261  # find /sys/devices/ | grep pcrs$ | xargs cat
262  PCR-00: 35 4E 3B CE 23 9F 38 59 ...
263  ...
264  PCR-23: 00 00 00 00 00 00 00 00 ...
265
266The QEMU TPM emulator device
267----------------------------
268
269The TPM emulator device uses an external TPM emulator called 'swtpm'
270for sending TPM commands to and receiving responses from. The swtpm
271program must have been started before trying to access it through the
272TPM emulator with QEMU.
273
274The TPM emulator implements a command channel for transferring TPM
275commands and responses as well as a control channel over which control
276commands can be sent. (see the `SWTPM protocol`_ specification)
277
278The control channel serves the purpose of resetting, initializing, and
279migrating the TPM state, among other things.
280
281The swtpm program behaves like a hardware TPM and therefore needs to
282be initialized by the firmware running inside the QEMU virtual
283machine.  One necessary step for initializing the device is to send
284the TPM_Startup command to it. SeaBIOS, for example, has been
285instrumented to initialize a TPM 1.2 or TPM 2 device using this
286command.
287
288QEMU files related to the TPM emulator device:
289 - ``hw/tpm/tpm_emulator.c``
290 - ``hw/tpm/tpm_util.c``
291 - ``hw/tpm/tpm_util.h``
292
293The following commands start the swtpm with a UnixIO control channel over
294a socket interface. They do not need to be run as root.
295
296.. code-block:: console
297
298  mkdir /tmp/mytpm1
299  swtpm socket --tpmstate dir=/tmp/mytpm1 \
300    --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
301    --log level=20
302
303Command line to start QEMU with the TPM emulator device communicating
304with the swtpm (x86):
305
306.. code-block:: console
307
308  qemu-system-x86_64 -display sdl -accel kvm \
309    -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
310    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
311    -tpmdev emulator,id=tpm0,chardev=chrtpm \
312    -device tpm-tis,tpmdev=tpm0 test.img
313
314In case a pSeries machine is emulated, use the following command line:
315
316.. code-block:: console
317
318  qemu-system-ppc64 -display sdl -machine pseries,accel=kvm \
319    -m 1024 -bios slof.bin -boot menu=on \
320    -nodefaults -device VGA -device pci-ohci -device usb-kbd \
321    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
322    -tpmdev emulator,id=tpm0,chardev=chrtpm \
323    -device tpm-spapr,tpmdev=tpm0 \
324    -device spapr-vscsi,id=scsi0,reg=0x00002000 \
325    -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x3,drive=drive-virtio-disk0,id=virtio-disk0 \
326    -drive file=test.img,format=raw,if=none,id=drive-virtio-disk0
327
328In case SeaBIOS is used as firmware, it should show the TPM menu item
329after entering the menu with 'ESC'.
330
331.. code-block:: console
332
333  Select boot device:
334  1. DVD/CD [ata1-0: QEMU DVD-ROM ATAPI-4 DVD/CD]
335  [...]
336  5. Legacy option rom
337
338  t. TPM Configuration
339
340The following commands should result in similar output inside the VM
341with a Linux kernel that either has the TPM TIS driver built-in or
342available as a module:
343
344.. code-block:: console
345
346  # dmesg | grep -i tpm
347  [    0.711310] tpm_tis 00:06: 1.2 TPM (device=id 0x1, rev-id 1)
348
349  # dmesg | grep TCPA
350  [    0.000000] ACPI: TCPA 0x0000000003FFD191C 000032 (v02 BOCHS  \
351      BXPCTCPA 0000001 BXPC 00000001)
352
353  # ls -l /dev/tpm*
354  crw-------. 1 root root 10, 224 Jul 11 10:11 /dev/tpm0
355
356  # find /sys/devices/ | grep pcrs$ | xargs cat
357  PCR-00: 35 4E 3B CE 23 9F 38 59 ...
358  ...
359  PCR-23: 00 00 00 00 00 00 00 00 ...
360
361Migration with the TPM emulator
362===============================
363
364The TPM emulator supports the following types of virtual machine
365migration:
366
367- VM save / restore (migration into a file)
368- Network migration
369- Snapshotting (migration into storage like QoW2 or QED)
370
371The following command sequences can be used to test VM save / restore.
372
373In a 1st terminal start an instance of a swtpm using the following command:
374
375.. code-block:: console
376
377  mkdir /tmp/mytpm1
378  swtpm socket --tpmstate dir=/tmp/mytpm1 \
379    --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
380    --log level=20 --tpm2
381
382In a 2nd terminal start the VM:
383
384.. code-block:: console
385
386  qemu-system-x86_64 -display sdl -accel kvm \
387    -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
388    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
389    -tpmdev emulator,id=tpm0,chardev=chrtpm \
390    -device tpm-tis,tpmdev=tpm0 \
391    -monitor stdio \
392    test.img
393
394Verify that the attached TPM is working as expected using applications
395inside the VM.
396
397To store the state of the VM use the following command in the QEMU
398monitor in the 2nd terminal:
399
400.. code-block:: console
401
402  (qemu) migrate "exec:cat > testvm.bin"
403  (qemu) quit
404
405At this point a file called ``testvm.bin`` should exists and the swtpm
406and QEMU processes should have ended.
407
408To test 'VM restore' you have to start the swtpm with the same
409parameters as before. If previously a TPM 2 [--tpm2] was saved, --tpm2
410must now be passed again on the command line.
411
412In the 1st terminal restart the swtpm with the same command line as
413before:
414
415.. code-block:: console
416
417  swtpm socket --tpmstate dir=/tmp/mytpm1 \
418    --ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
419    --log level=20 --tpm2
420
421In the 2nd terminal restore the state of the VM using the additional
422'-incoming' option.
423
424.. code-block:: console
425
426  qemu-system-x86_64 -display sdl -accel kvm \
427    -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
428    -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \
429    -tpmdev emulator,id=tpm0,chardev=chrtpm \
430    -device tpm-tis,tpmdev=tpm0 \
431    -incoming "exec:cat < testvm.bin" \
432    test.img
433
434Troubleshooting migration
435-------------------------
436
437There are several reasons why migration may fail. In case of problems,
438please ensure that the command lines adhere to the following rules
439and, if possible, that identical versions of QEMU and swtpm are used
440at all times.
441
442VM save and restore:
443
444 - QEMU command line parameters should be identical apart from the
445   '-incoming' option on VM restore
446
447 - swtpm command line parameters should be identical
448
449VM migration to 'localhost':
450
451 - QEMU command line parameters should be identical apart from the
452   '-incoming' option on the destination side
453
454 - swtpm command line parameters should point to two different
455   directories on the source and destination swtpm (--tpmstate dir=...)
456   (especially if different versions of libtpms were to be used on the
457   same machine).
458
459VM migration across the network:
460
461 - QEMU command line parameters should be identical apart from the
462   '-incoming' option on the destination side
463
464 - swtpm command line parameters should be identical
465
466VM Snapshotting:
467 - QEMU command line parameters should be identical
468
469 - swtpm command line parameters should be identical
470
471
472Besides that, migration failure reasons on the swtpm level may include
473the following:
474
475 - the versions of the swtpm on the source and destination sides are
476   incompatible
477
478   - downgrading of TPM state may not be supported
479
480   - the source and destination libtpms were compiled with different
481     compile-time options and the destination side refuses to accept the
482     state
483
484 - different migration keys are used on the source and destination side
485   and the destination side cannot decrypt the migrated state
486   (swtpm ... --migration-key ... )
487
488
489.. _TIS specification:
490   https://trustedcomputinggroup.org/pc-client-work-group-pc-client-specific-tpm-interface-specification-tis/
491
492.. _CRB specification:
493   https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
494
495
496.. _ACPI specification:
497   https://trustedcomputinggroup.org/tcg-acpi-specification/
498
499.. _PPI specification:
500   https://trustedcomputinggroup.org/resource/tcg-physical-presence-interface-specification/
501
502.. _SWTPM protocol:
503   https://github.com/stefanberger/swtpm/blob/master/man/man3/swtpm_ioctls.pod
504