Add vTPM emulator supportfor ppc64 platform
v4 cover Add vTPM emulator supportfor ppc64 platform
The following series of patches adds vTPM emulator support for the ppc64 platform (pSeries).
It can be tested as follows with swtpm/libtpms:
mkdir /tmp/mytpm1
swtpm socket --tpmstate dir=/tmp/mytpm1 \
--ctrl type=unixio,path=/tmp/mytpm1/swtpm-sock \
--log level=20
If TPM 2 is desired, add --tpm2 as parameter to the above.
In another terminal start QEMU:
sudo ./ppc64-softmmu/qemu-system-ppc64 -display sdl \ -machine pseries,accel=kvm \ -m 1024 -bios slof.bin -boot menu=on \ -nodefaults -device VGA -device pci-ohci -device usb-kbd \ -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \ -tpmdev emulator,id=tpm0,chardev=chrtpm \ -device tpm-spapr,tpmdev=tpm0 \ -device spapr-vscsi,id=scsi0,reg=0x00002000 \ -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x3,drive=drive-virtio-disk0,id=virtio-disk0 \ -drive file=test.img,format=raw,if=none,id=drive-virtio-disk0
Links:
- libtpms: https://github.com/stefanberger/libtpms/wiki
- swtpm: https://github.com/stefanberger/swtpm/wiki
Changes:
v3->v4:
- addressed comments to v3
- reworked suspend/resume support that requires extensions to backends
v2->v3:
- patch 1: a TPM 2 is identified by IBM,vtpm20 in the compatible node
- patch 1: convert to tracing to display Tx and Rx buffers
- added documentation patch
- added patch to enable TPM device as part of pSeries
v1->v2:
- followed Cedric Le Goater's suggestions to patch 1
- send appropriate CRQ error responses if DMA read or write fails
- renamed tpm_spapr_got_payload to tpm_spapr_process_cmd and pass endianess-adjusted data pointer from CRQ to it
Regards,
Stefan
Stefan Berger (8):
tpm_spapr: Support TPM for ppc64 using CRQ based interface
tpm_backend: Implement check whether tpm backend is suspended
tpm_emulator: Implement callback for whether we are suspended
tpm_passthrough: Implement callback for whether we are suspended
tpm: Return bool from tpm_backend_finish_sync
tpm_spapr: Support suspend and resume
hw/ppc/Kconfig: Enable TPM_SPAPR as part of PSERIES config
docs: tpm: Add example command line for ppc64 and tpm-spapr
backends/tpm.c | 13 +- docs/specs/tpm.txt | 20 +- hw/ppc/Kconfig | 1 + hw/tpm/Kconfig | 6 + hw/tpm/Makefile.objs | 1 + hw/tpm/tpm_emulator.c | 12 + hw/tpm/tpm_passthrough.c | 6 + hw/tpm/tpm_spapr.c | 460 +++++++++++++++++++++++++++++++++++ hw/tpm/trace-events | 14 ++ include/sysemu/tpm.h | 3 + include/sysemu/tpm_backend.h | 16 +- qapi/tpm.json | 6 +- 12 files changed, 552 insertions(+), 6 deletions(-) create mode 100644 hw/tpm/tpm_spapr.c
Signature · 1 line
2.21.0
PATCH v4 1/8 tpm_spapr: Support TPM for ppc64 using CRQ based interface
Implement support for TPM on ppc64 by implementing the vTPM CRQ interface as a frontend. It can use the tpm_emulator driver backend with the external swtpm.
The Linux vTPM driver for ppc64 works with this emulation.
This TPM emulator also handles the TPM 2 case.
hw/tpm/Kconfig +6 -0
@@ -22,3 +22,9 @@ config TPM_EMULATOR bool default y depends on TPMDEV + +config TPM_SPAPR + bool + default n + select TPMDEV + depends on PSERIES
hw/tpm/Makefile.objs +1 -0
@@ -4,3 +4,4 @@ common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o common-obj-$(CONFIG_TPM_CRB) += tpm_crb.o common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o +obj-$(CONFIG_TPM_SPAPR) += tpm_spapr.o
hw/tpm/tpm_spapr.c new file +405 -0
405 lines not on this page · raw message
hw/tpm/trace-events +12 -0
@@ -55,3 +55,15 @@ tpm_tis_pre_save(uint8_t locty, uint32_t rw_offset) "locty: %d, rw_offset = %u" # tpm_ppi.c tpm_ppi_memset(uint8_t *ptr, size_t size) "memset: %p %zu" + +# hw/tpm/tpm_spapr.c +tpm_spapr_show_buffer(const char *direction, size_t len, const char *buf) "direction: %s len: %zu\n%s" +tpm_spapr_do_crq(uint8_t raw1, uint8_t raw2) "1st 2 bytes in CRQ: 0x%02x 0x%02x" +tpm_spapr_do_crq_crq_result(void) "SPAPR_VTPM_INIT_CRQ_RESULT" +tpm_spapr_do_crq_crq_complete_result(void) "SPAPR_VTPM_INIT_CRQ_COMP_RESULT" +tpm_spapr_do_crq_tpm_command(void) "got TPM command payload" +tpm_spapr_do_crq_tpm_get_rtce_buffer_size(size_t buffersize) "response: buffer size is %zu" +tpm_spapr_do_crq_get_version(uint32_t version) "response: version %u" +tpm_spapr_do_crq_prepare_to_suspend(void) "response: preparing to suspend" +tpm_spapr_do_crq_unknown_msg_type(uint8_t type) "Unknown message type 0x%02x" +tpm_spapr_do_crq_unknown_crq(uint8_t raw1, uint8_t raw2) "unknown CRQ 0x%02x 0x%02x ..."
include/sysemu/tpm.h +3 -0
@@ -45,11 +45,14 @@ typedef struct TPMIfClass {#define TYPE_TPM_TIS "tpm-tis"#define TYPE_TPM_CRB "tpm-crb" +#define TYPE_TPM_SPAPR "tpm-spapr"#define TPM_IS_TIS (chr) \ object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS)#define TPM_IS_CRB (chr) \ object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB) +#define TPM_IS_SPAPR (chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_TPM_SPAPR)/* returns NULL unless there is exactly one TPM device */ static inline TPMIf *tpm_find (void )
qapi/tpm.json +3 -3
@@ -12,11 +12,11 @@ # # @tpm-tis: TPM TIS model # @tpm-crb: TPM CRB model (since 2.12) +# @tpm-spapr: TPM SPAPR model (since 5.0) # # Since: 1.5 ## -{ 'enum': 'TpmModel', 'data': [ 'tpm-tis', 'tpm-crb' ] } - +{ 'enum': 'TpmModel', 'data': [ 'tpm-tis', 'tpm-crb', 'tpm-spapr' ] } ## # @query-tpm-models: #
@@ -29,7 +29,7 @@ # Example: # # -> { "execute": "query-tpm-models" } -# <- { "return": [ "tpm-tis", "tpm-crb" ] } +# <- { "return": [ "tpm-tis", "tpm-crb", "tpm-spapr" ] } # ## { 'command': 'query-tpm-models', 'returns': ['TpmModel'] }
Signature · 1 line
2.21.0
PATCH v4 2/8 tpm_backend: Implement check whether tpm backend is suspended
In reply to Stefan BergerSome frontends need to know whether the backend is suspended. Implement tpm_backend_is_suspended().
backends/tpm.c +7 -0
@@ -163,6 +163,13 @@ size_t tpm_backend_get_buffer_size(TPMBackend *s)return k->get_buffer_size (s); } +bool tpm_backend_is_suspended (TPMBackend *s) +{ +TPMBackendClass *k =TPM_BACKEND_GET_CLASS (s); + +return k->is_suspended (s); +} +TPMInfo *tpm_backend_query_tpm (TPMBackend *s) {TPMInfo *info =g_new0 (TPMInfo,1 );
include/sysemu/tpm_backend.h +12 -0
@@ -84,6 +84,8 @@ struct TPMBackendClass { TpmTypeOptions *(*get_tpm_options)(TPMBackend *t);void (*handle_request)(TPMBackend *s,TPMBackendCmd *cmd,Error **errp); + +bool (*is_suspended)(TPMBackend *t); };/**
@@ -213,6 +215,16 @@ void tpm_backend_finish_sync(TPMBackend *s);*/ TPMInfo *tpm_backend_query_tpm (TPMBackend *s); +/** +* tpm_backend_is_suspended: +* @s: the backend to call into +* +* Whether the backend is suspended +* +* Returns true in case the backend is suspended +*/ +bool tpm_backend_is_suspended (TPMBackend *s); +TPMBackend *qemu_find_tpm_be (const char *id);#endif
Signature · 1 line
2.21.0
PATCH v4 3/8 tpm_emulator: Implement callback for whether we are suspended
In reply to Stefan BergerImplement the check whether the emulator backend is suspended.
hw/tpm/tpm_emulator.c +12 -0
@@ -80,6 +80,8 @@ typedef struct TPMEmulator { unsigned int established_flag_cached:1;TPMBlobBuffers state_blobs; + +bool is_suspended; } TPMEmulator;struct tpm_error {
@@ -486,6 +488,13 @@ static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)return actual_size; } +static bool tpm_emulator_is_suspended (TPMBackend *tb) +{ +TPMEmulator *tpm_emu =TPM_EMULATOR (tb); + +return tpm_emu->is_suspended; +} +static int tpm_emulator_block_migration (TPMEmulator *tpm_emu) {Error *err =NULL ;
@@ -846,6 +855,8 @@ static int tpm_emulator_pre_save(void *opaque)TPMBackend *tb = opaque;TPMEmulator *tpm_emu =TPM_EMULATOR (tb); + tpm_emu->is_suspended = true; +trace_tpm_emulator_pre_save ();tpm_backend_finish_sync (tb);
@@ -975,6 +986,7 @@ static void tpm_emulator_class_init(ObjectClass *klass, void *data) tbc->get_tpm_version = tpm_emulator_get_tpm_version; tbc->get_buffer_size = tpm_emulator_get_buffer_size; tbc->get_tpm_options = tpm_emulator_get_tpm_options; + tbc->is_suspended = tpm_emulator_is_suspended; tbc->handle_request = tpm_emulator_handle_request; }
Signature · 1 line
2.21.0
Re: [PATCH v4 3/8] tpm_emulator: Implement callback for whether we are suspended
Quoted 32 lines · Stefan Berger
Implement the check whether the emulator backend is suspended.
Signed-off-by Stefan Berger <[email protected]>hw/tpm/tpm_emulator.c +9
-0@@ -80,6 +80,8 @@ typedef struct TPMEmulator { unsigned int established_flag_cached:1;TPMBlobBuffers state_blobs; + +bool is_suspended; } TPMEmulator;struct tpm_error {@@ -486,6 +488,13 @@ static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)return actual_size; } +static bool tpm_emulator_is_suspended (TPMBackend *tb) +{ +TPMEmulator *tpm_emu =TPM_EMULATOR (tb); + +return tpm_emu->is_suspended; +} +static int tpm_emulator_block_migration (TPMEmulator *tpm_emu) {Error *err =NULL ;
hw/tpm/tpm_emulator.c static int tpm_emulator_pre_save(void *opaque) · line 858
Quoted 4 lines
hw/tpm/tpm_emulator.c +1
-0@@ -846,6 +855,8 @@ static int tpm_emulator_pre_save(void *opaque)TPMBackend *tb = opaque;TPMEmulator *tpm_emu =TPM_EMULATOR (tb); + tpm_emu->is_suspended = true;
This is the most critical part here. It must be true when we receive a response in the tpm_spapr_request_completed(). The problem is that what tpm_backend_finish_sync() does is not specific to this backend but more a global operation that another device could run as well -- none seem to do this today. So the point is that there could be a race here. This flag should really be set in '.pre_pre_save,' so before any other device could poll. Better would be calling a global function that indicates whether device suspension has started. In this case we could do away with this and just call that function from the spapr device.
Re: [PATCH v4 3/8] tpm_emulator: Implement callback for whether we are suspended
Quoted 28 lines · Stefan Berger
Quoted 27 lines · Stefan Berger
Implement the check whether the emulator backend is suspended.
Signed-off-by Stefan Berger <[email protected]>hw/tpm/tpm_emulator.c +8
-0@@ -80,6 +80,8 @@ typedef struct TPMEmulator { unsigned int established_flag_cached:1; TPMBlobBuffers state_blobs; + + bool is_suspended; } TPMEmulator; struct tpm_error {@@ -486,6 +488,13 @@ static size_t tpm_emulator_get_buffer_size(TPMBackend *tb) return actual_size; } +static bool tpm_emulator_is_suspended(TPMBackend *tb) +{ + TPMEmulator *tpm_emu = TPM_EMULATOR(tb); + + return tpm_emu->is_suspended; +} + static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
Quoted 8 lines
hw/tpm/tpm_emulator.c static int tpm_emulator_pre_save(void *opaque) · line 857
Quoted 6 lines
hw/tpm/tpm_emulator.c +0
-0{Error *err = NULL;
hw/tpm/tpm_emulator.c +0
-0@@ -846,6 +855,8 @@ static int tpm_emulator_pre_save(void *opaque) TPMBackend *tb = opaque; TPMEmulator *tpm_emu = TPM_EMULATOR(tb); + tpm_emu->is_suspended = true;This is the most critical part here. It must be true when we receive a response in the
tpm_spapr_request_completed(). The problem is that whattpm_backend_finish_sync()does is not specific to this backend but more a global operation that another device could run as well -- none seem to do this today. So the point is that there could be a race here. This flag should really be set in '.pre_pre_save,' so before any other device could poll. Better would be calling a global function that indicates whether device suspension has started. In this case we could do away with this and just call that function from the spapr device.
runstate_check(RUN_STATE_FINISH_MIGRATE) seems to be what we need here...
Quoted 3 lines
PATCH v4 4/8 tpm_passthrough: Implement callback for whether we are suspended
In reply to Stefan BergerImplement the callback for whether the passthrough backend is suspended. We always respond with false.
hw/tpm/tpm_passthrough.c +6 -0
@@ -203,6 +203,11 @@ static size_t tpm_passthrough_get_buffer_size(TPMBackend *tb)return tpm_pt->tpm_buffersize; } +static bool tpm_passthrough_is_suspended (TPMBackend *tb) +{ +return false; +} +/* * Unless path or file descriptor set has been provided by user, * determine the sysfs cancel file following kernel documentation
@@ -386,6 +391,7 @@ static void tpm_passthrough_class_init(ObjectClass *klass, void *data) tbc->get_buffer_size = tpm_passthrough_get_buffer_size; tbc->get_tpm_options = tpm_passthrough_get_tpm_options; tbc->handle_request = tpm_passthrough_handle_request; + tbc->is_suspended = tpm_passthrough_is_suspended; }static const TypeInfo tpm_passthrough_info = {
Signature · 1 line
2.21.0
PATCH v4 5/8 tpm: Return bool from tpm_backend_finish_sync
In reply to Stefan BergerReturn true in case we had to wait for an outstanding response to come back, false otherwise.
backends/tpm.c +5 -1
@@ -49,11 +49,15 @@ static int tpm_backend_worker_thread(gpointer data)return 0 ; } -void tpm_backend_finish_sync (TPMBackend *s) +bool tpm_backend_finish_sync (TPMBackend *s) { +bool ret = s->cmd !=NULL ; +while (s->cmd) {aio_poll (qemu_get_aio_context (), true); } + +return ret; }enum TpmType tpm_backend_get_type (TPMBackend *s)
include/sysemu/tpm_backend.h +3 -1
@@ -202,8 +202,10 @@ size_t tpm_backend_get_buffer_size(TPMBackend *s);* * Finish the pending command synchronously (this will call aio_poll() * on qemu main AIOContext until it ends) +* +* Returns true in case there was a pending command, false otherwise. */ -void tpm_backend_finish_sync (TPMBackend *s); +bool tpm_backend_finish_sync (TPMBackend *s);/** * tpm_backend_query_tpm:
Signature · 1 line
2.21.0
PATCH v4 6/8 tpm_spapr: Support suspend and resume
In reply to Stefan BergerExtend the tpm_spapr frontend with VM suspend and resume support.
hw/tpm/tpm_spapr.c +56 -1
@@ -87,6 +87,8 @@ typedef struct {TPMVersion be_tpm_version;size_t be_buffer_size; + +bool deliver_response;/* whether to deliver response after VM resume */ } SPAPRvTPMState; static void tpm_spapr_show_buffer(const unsigned char *buffer,
@@ -255,6 +257,14 @@ static void tpm_spapr_request_completed(TPMIf *ti, int ret)TPMSpaprCRQ *crq = &s->crq;uint32_t len;int rc; +bool be_suspended; + + be_suspended =tpm_backend_is_suspended (s->be_driver); +if (be_suspended) { +/* defer delivery of response until .post_load */ + s->deliver_response |= true; +return ; + } s->state =SPAPR_VTPM_STATE_COMPLETION ;
@@ -316,6 +326,7 @@ static void tpm_spapr_reset(SpaprVioDevice *dev)SPAPRvTPMState *s =VIO_SPAPR_VTPM (dev); s->state =SPAPR_VTPM_STATE_NONE ; + s->deliver_response = false; s->be_tpm_version =tpm_backend_get_tpm_version (s->be_driver);tpm_spapr_update_deviceclass (dev);
@@ -339,9 +350,53 @@ static enum TPMVersion tpm_spapr_get_version(TPMIf *ti)return tpm_backend_get_tpm_version (s->be_driver); } +/* persistent state handling */ + +static int tpm_spapr_pre_save (void *opaque) +{ +SPAPRvTPMState *s = opaque; + + s->deliver_response |=tpm_backend_finish_sync (s->be_driver); + +trace_tpm_spapr_pre_save (s->deliver_response); +/* +* we cannot deliver the results to the VM since DMA would touch VM memory +*/ + +return 0 ; +} + +static int tpm_spapr_post_load (void *opaque,int version_id) +{ +SPAPRvTPMState *s = opaque; + +if (s->deliver_response) { +trace_tpm_spapr_post_load (); +/* deliver the results to the VM via DMA */ +tpm_spapr_request_completed (TPM_IF (s),0 ); + s->deliver_response = false; + } + +return 0 ; +} +static const VMStateDescription vmstate_spapr_vtpm = { .name ="tpm-spapr" , - .unmigratable =1 , + .version_id =1 , + .minimum_version_id =0 , + .minimum_version_id_old =0 , + .pre_save = tpm_spapr_pre_save, + .post_load = tpm_spapr_post_load, + .fields = (VMStateField []) { +VMSTATE_SPAPR_VIO (vdev, SPAPRvTPMState), + +VMSTATE_UINT8 (state, SPAPRvTPMState), +VMSTATE_BUFFER (buffer, SPAPRvTPMState), +/* remember DMA address */ +VMSTATE_UINT32 (crq.s.data, SPAPRvTPMState), +VMSTATE_BOOL (deliver_response, SPAPRvTPMState), +VMSTATE_END_OF_LIST (), + } };static Property tpm_spapr_properties[] = {
hw/tpm/trace-events +2 -0
@@ -67,3 +67,5 @@ tpm_spapr_do_crq_get_version(uint32_t version) "response: version %u" tpm_spapr_do_crq_prepare_to_suspend(void) "response: preparing to suspend" tpm_spapr_do_crq_unknown_msg_type(uint8_t type) "Unknown message type 0x%02x" tpm_spapr_do_crq_unknown_crq(uint8_t raw1, uint8_t raw2) "unknown CRQ 0x%02x 0x%02x ..." +tpm_spapr_pre_save(bool v) "TPM response to deliver after resume: %d" +tpm_spapr_post_load(void) "Delivering TPM response after resume"
Signature · 1 line
2.21.0
PATCH v4 7/8 hw/ppc/Kconfig: Enable TPM_SPAPR as part of PSERIES config
In reply to Stefan Bergerhw/ppc/Kconfig +1 -0
@@ -10,6 +10,7 @@ config PSERIES select XICS_SPAPR select XIVE_SPAPR select MSI_NONBROKEN + select TPM_SPAPR config SPAPR_RNG bool
Signature · 1 line
2.21.0
PATCH v4 8/8 docs: tpm: Add example command line for ppc64 and tpm-spapr
In reply to Stefan BergerAdd an example to the TPM docs for how to add a TPM SPAPR device model to a QEMU VM emulating a pSeries machine.
docs/specs/tpm.txt +19 -1
@@ -34,6 +34,12 @@ The CRB interface makes a memory mapped IO region in the area 0xfed40000 - QEMU files related to TPM CRB interface: - hw/tpm/tpm_crb.c + +pSeries (ppc64) machines offer a tpm-spapr device model. + +QEMU files related to the SPAPR interface: + - hw/tpm/tpm_spapr.c + = fw_cfg interface = The bios/firmware may read the "etc/tpm/config" fw_cfg entry for
@@ -281,7 +287,7 @@ swtpm socket --tpmstate dir=/tmp/mytpm1 \ --log level=20 Command line to start QEMU with the TPM emulator device communicating with -the swtpm: +the swtpm (x86): qemu-system-x86_64 -display sdl -accel kvm \ -m 1024 -boot d -bios bios-256k.bin -boot menu=on \
@@ -289,6 +295,18 @@ qemu-system-x86_64 -display sdl -accel kvm \ -tpmdev emulator,id=tpm0,chardev=chrtpm \ -device tpm-tis,tpmdev=tpm0 test.img +In case a pSeries machine is emulated, use the following command line: + +qemu-system-ppc64 -display sdl -machine pseries,accel=kvm \ + -m 1024 -bios slof.bin -boot menu=on \ + -nodefaults -device VGA -device pci-ohci -device usb-kbd \ + -chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock \ + -tpmdev emulator,id=tpm0,chardev=chrtpm \ + -device tpm-spapr,tpmdev=tpm0 \ + -device spapr-vscsi,id=scsi0,reg=0x00002000 \ + -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x3,drive=drive-virtio-disk0,id=virtio-disk0 \ + -drive file=test.img,format=raw,if=none,id=drive-virtio-disk0 + In case SeaBIOS is used as firmware, it should show the TPM menu item after entering the menu with 'ESC'.
Signature · 1 line
2.21.0