From ff2bc0c1159d7c1a32495cb0dbad83720554fb3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:42 +0200 Subject: [PATCH 01/21] tpm-tis: remove unused hw_access argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This argument is always false, simplify the code. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_tis.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index d5118e7f6062..a9e9cbdeb275 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -615,9 +615,8 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, * Write a value to a register of the TIS interface * See specs pages 33-63 for description of the registers */ -static void tpm_tis_mmio_write_intern(void *opaque, hwaddr addr, - uint64_t val, unsigned size, - bool hw_access) +static void tpm_tis_mmio_write(void *opaque, hwaddr addr, + uint64_t val, unsigned size) { TPMState *s = opaque; TPMTISEmuState *tis = &s->s.tis; @@ -631,7 +630,7 @@ static void tpm_tis_mmio_write_intern(void *opaque, hwaddr addr, DPRINTF("tpm_tis: write.%u(%08x) = %08x\n", size, (int)addr, (int)val); - if (locty == 4 && !hw_access) { + if (locty == 4) { DPRINTF("tpm_tis: Access to locality 4 only allowed from hardware\n"); return; } @@ -942,12 +941,6 @@ static void tpm_tis_mmio_write_intern(void *opaque, hwaddr addr, } } -static void tpm_tis_mmio_write(void *opaque, hwaddr addr, - uint64_t val, unsigned size) -{ - tpm_tis_mmio_write_intern(opaque, addr, val, size, false); -} - static const MemoryRegionOps tpm_tis_memory_ops = { .read = tpm_tis_mmio_read, .write = tpm_tis_mmio_write, From d8383d61fa6e11692fb8abfe0c673ece5e1ba9bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:43 +0200 Subject: [PATCH 02/21] tpm-tis: remove RAISE_STS_IRQ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This look like temporary hacking code. It shouldn't be necessary in release code, or there should be a runtime option for it. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_tis.c | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index a9e9cbdeb275..c54dac32de3f 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -43,9 +43,6 @@ } \ } while (0); -/* whether the STS interrupt is supported */ -#define RAISE_STS_IRQ - /* tis registers */ #define TPM_TIS_REG_ACCESS 0x00 #define TPM_TIS_REG_INT_ENABLE 0x08 @@ -98,21 +95,11 @@ #define TPM_TIS_INT_POLARITY_MASK (3 << 3) #define TPM_TIS_INT_POLARITY_LOW_LEVEL (1 << 3) -#ifndef RAISE_STS_IRQ - -#define TPM_TIS_INTERRUPTS_SUPPORTED (TPM_TIS_INT_LOCALITY_CHANGED | \ - TPM_TIS_INT_DATA_AVAILABLE | \ - TPM_TIS_INT_COMMAND_READY) - -#else - #define TPM_TIS_INTERRUPTS_SUPPORTED (TPM_TIS_INT_LOCALITY_CHANGED | \ TPM_TIS_INT_DATA_AVAILABLE | \ TPM_TIS_INT_STS_VALID | \ TPM_TIS_INT_COMMAND_READY) -#endif - #define TPM_TIS_CAP_INTERFACE_VERSION1_3 (2 << 28) #define TPM_TIS_CAP_INTERFACE_VERSION1_3_FOR_TPM2_0 (3 << 28) #define TPM_TIS_CAP_DATA_TRANSFER_64B (3 << 9) @@ -377,12 +364,8 @@ static void tpm_tis_receive_bh(void *opaque) tpm_tis_abort(s, locty); } -#ifndef RAISE_STS_IRQ - tpm_tis_raise_irq(s, locty, TPM_TIS_INT_DATA_AVAILABLE); -#else tpm_tis_raise_irq(s, locty, TPM_TIS_INT_DATA_AVAILABLE | TPM_TIS_INT_STS_VALID); -#endif } /* @@ -421,9 +404,7 @@ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty) if (tis->loc[locty].r_offset >= len) { /* got last byte */ tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_VALID); -#ifdef RAISE_STS_IRQ tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID); -#endif } DPRINTF("tpm_tis: tpm_tis_data_read byte 0x%02x [%d]\n", ret, tis->loc[locty].r_offset-1); @@ -912,9 +893,8 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, if (tis->loc[locty].w_offset > 5 && (tis->loc[locty].sts & TPM_TIS_STS_EXPECT)) { /* we have a packet length - see if we have all of it */ -#ifdef RAISE_STS_IRQ bool need_irq = !(tis->loc[locty].sts & TPM_TIS_STS_VALID); -#endif + len = tpm_tis_get_size_from_buffer(&tis->loc[locty].w_buffer); if (len > tis->loc[locty].w_offset) { tpm_tis_sts_set(&tis->loc[locty], @@ -923,11 +903,9 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, /* packet complete */ tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_VALID); } -#ifdef RAISE_STS_IRQ if (need_irq) { tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID); } -#endif } } break; From 349ccdc89c1b76f6cdd9f8cdc412f0d806046d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:44 +0200 Subject: [PATCH 03/21] tpm: make tpm_get_backend_driver() static MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No need to export the function. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- include/sysemu/tpm_backend.h | 1 - tpm.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 2c798a1eb477..c6e5637f6820 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -215,7 +215,6 @@ TPMInfo *tpm_backend_query_tpm(TPMBackend *s); TPMBackend *qemu_find_tpm(const char *id); -const TPMDriverOps *tpm_get_backend_driver(const char *type); void tpm_register_model(enum TpmModel model); void tpm_register_driver(const TPMDriverOps *tdo); diff --git a/tpm.c b/tpm.c index 31222271566e..bc7d7470a827 100644 --- a/tpm.c +++ b/tpm.c @@ -31,7 +31,7 @@ void tpm_register_model(enum TpmModel model) tpm_models[model] = true; } -const TPMDriverOps *tpm_get_backend_driver(const char *type) +static const TPMDriverOps *tpm_get_backend_driver(const char *type) { int i = qapi_enum_parse(&TpmType_lookup, type, -1, NULL); From f3faa1dff4dc4f3fea28ca847d57caa150c15683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:45 +0200 Subject: [PATCH 04/21] tpm: lookup tpm backend class in tpm_driver_find_by_type() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One step towards removing TPMDriverOps and driver registration. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- tpm.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tpm.c b/tpm.c index bc7d7470a827..04de8da5fdc1 100644 --- a/tpm.c +++ b/tpm.c @@ -47,6 +47,24 @@ void tpm_register_driver(const TPMDriverOps *tdo) be_drivers[tdo->type] = tdo; } +static const TPMDriverOps * +tpm_driver_find_by_type(enum TpmType type) +{ + ObjectClass *oc; + TPMBackendClass *bc; + char *typename = g_strdup_printf("tpm-%s", TpmType_str(type)); + + oc = object_class_by_name(typename); + g_free(typename); + + if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) { + return NULL; + } + + bc = TPM_BACKEND_CLASS(oc); + return bc->ops; +} + /* * Walk the list of available TPM backend drivers and display them on the * screen. @@ -58,11 +76,11 @@ static void tpm_display_backend_drivers(void) fprintf(stderr, "Supported TPM types (choose only one):\n"); for (i = 0; i < TPM_TYPE__MAX; i++) { - if (be_drivers[i] == NULL) { + const TPMDriverOps *ops = tpm_driver_find_by_type(i); + if (!ops) { continue; } - fprintf(stderr, "%12s %s\n", - TpmType_str(i), be_drivers[i]->desc); + fprintf(stderr, "%12s %s\n", TpmType_str(i), ops->desc); } fprintf(stderr, "\n"); } @@ -196,11 +214,6 @@ int tpm_config_parse(QemuOptsList *opts_list, const char *optarg) #endif /* CONFIG_TPM */ -static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type) -{ - return be_drivers[type]; -} - /* * Walk the list of active TPM backends and collect information about them * following the schema description in qapi-schema.json. From d91a7a575e9f4e65447c6c3c094932cd13ba1167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:46 +0200 Subject: [PATCH 05/21] tpm: replace tpm_get_backend_driver() to drop be_drivers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use tpm_driver_find_by_type() instead. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- tpm.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tpm.c b/tpm.c index 04de8da5fdc1..ad2ed6a0da74 100644 --- a/tpm.c +++ b/tpm.c @@ -31,13 +31,6 @@ void tpm_register_model(enum TpmModel model) tpm_models[model] = true; } -static const TPMDriverOps *tpm_get_backend_driver(const char *type) -{ - int i = qapi_enum_parse(&TpmType_lookup, type, -1, NULL); - - return i >= 0 ? be_drivers[i] : NULL; -} - #ifdef CONFIG_TPM void tpm_register_driver(const TPMDriverOps *tdo) @@ -110,6 +103,7 @@ static int configure_tpm(QemuOpts *opts) const TPMDriverOps *be; TPMBackend *drv; Error *local_err = NULL; + int i; if (!QLIST_EMPTY(&tpm_backends)) { error_report("Only one TPM is allowed."); @@ -129,7 +123,8 @@ static int configure_tpm(QemuOpts *opts) return 1; } - be = tpm_get_backend_driver(value); + i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL); + be = i >= 0 ? tpm_driver_find_by_type(i) : NULL; if (be == NULL) { error_report(QERR_INVALID_PARAMETER_VALUE, "type", "a TPM backend type"); From ed836d9d6bf0c89bf2e43e300fd2254237a39ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:47 +0200 Subject: [PATCH 06/21] tpm: remove tpm_register_driver() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No more users of be_drivers[], drop that too. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_emulator.c | 1 - hw/tpm/tpm_passthrough.c | 1 - include/sysemu/tpm_backend.h | 1 - tpm.c | 8 -------- 4 files changed, 11 deletions(-) diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 95e1e041cff8..187a69e442d1 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -581,7 +581,6 @@ static const TypeInfo tpm_emulator_info = { static void tpm_emulator_register(void) { type_register_static(&tpm_emulator_info); - tpm_register_driver(&tpm_emulator_driver); } type_init(tpm_emulator_register) diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index e6ace28b04a4..f04eab3e630b 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -418,7 +418,6 @@ static const TypeInfo tpm_passthrough_info = { static void tpm_passthrough_register(void) { type_register_static(&tpm_passthrough_info); - tpm_register_driver(&tpm_passthrough_driver); } type_init(tpm_passthrough_register) diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index c6e5637f6820..adb6a68076fe 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -216,6 +216,5 @@ TPMInfo *tpm_backend_query_tpm(TPMBackend *s); TPMBackend *qemu_find_tpm(const char *id); void tpm_register_model(enum TpmModel model); -void tpm_register_driver(const TPMDriverOps *tdo); #endif diff --git a/tpm.c b/tpm.c index ad2ed6a0da74..75a589b837a1 100644 --- a/tpm.c +++ b/tpm.c @@ -23,7 +23,6 @@ static QLIST_HEAD(, TPMBackend) tpm_backends = QLIST_HEAD_INITIALIZER(tpm_backends); -static TPMDriverOps const *be_drivers[TPM_TYPE__MAX]; static bool tpm_models[TPM_MODEL__MAX]; void tpm_register_model(enum TpmModel model) @@ -33,13 +32,6 @@ void tpm_register_model(enum TpmModel model) #ifdef CONFIG_TPM -void tpm_register_driver(const TPMDriverOps *tdo) -{ - assert(!be_drivers[tdo->type]); - - be_drivers[tdo->type] = tdo; -} - static const TPMDriverOps * tpm_driver_find_by_type(enum TpmType type) { From cfab6da8a7d3e9d825ea284a0ebbf6579e204f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:48 +0200 Subject: [PATCH 07/21] tpm: move TPMSizedBuffer to tpm_tis.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close to where it's being used. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_tis.h | 5 +++++ include/sysemu/tpm_backend.h | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hw/tpm/tpm_tis.h b/hw/tpm/tpm_tis.h index a1df41fa21a4..31e612a7f4dd 100644 --- a/hw/tpm/tpm_tis.h +++ b/hw/tpm/tpm_tis.h @@ -37,6 +37,11 @@ typedef enum { TPM_TIS_STATE_RECEPTION, } TPMTISState; +typedef struct TPMSizedBuffer { + uint32_t size; + uint8_t *buffer; +} TPMSizedBuffer; + /* locality data -- all fields are persisted */ typedef struct TPMLocality { TPMTISState state; diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index adb6a68076fe..63093551a169 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -66,11 +66,6 @@ struct TPMBackendClass { void (*handle_request)(TPMBackend *s, TPMBackendCmd cmd); }; -typedef struct TPMSizedBuffer { - uint32_t size; - uint8_t *buffer; -} TPMSizedBuffer; - struct TPMDriverOps { enum TpmType type; const QemuOptDesc *opts; From d31076ba75d4239928282143efd30d08730c8845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:49 +0200 Subject: [PATCH 08/21] tpm: remove TPMDriverOps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use TPMBackendClass to hold class methods/fields. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 31 ++++++++++++++++--------------- hw/tpm/tpm_emulator.c | 29 ++++++++++++----------------- hw/tpm/tpm_passthrough.c | 25 +++++++++++-------------- include/sysemu/tpm_backend.h | 15 +++++---------- scripts/checkpatch.pl | 1 - tpm.c | 20 +++++++++----------- 6 files changed, 53 insertions(+), 68 deletions(-) diff --git a/backends/tpm.c b/backends/tpm.c index 37c84b7c66b5..ca3a78eea8e1 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -41,7 +41,7 @@ enum TpmType tpm_backend_get_type(TPMBackend *s) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - return k->ops->type; + return k->type; } int tpm_backend_init(TPMBackend *s, TPMState *state, @@ -53,7 +53,7 @@ int tpm_backend_init(TPMBackend *s, TPMState *state, s->recv_data_callback = datacb; s->had_startup_error = false; - return k->ops->init ? k->ops->init(s) : 0; + return k->init ? k->init(s) : 0; } int tpm_backend_startup_tpm(TPMBackend *s) @@ -68,7 +68,7 @@ int tpm_backend_startup_tpm(TPMBackend *s) NULL); g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL); - res = k->ops->startup_tpm ? k->ops->startup_tpm(s) : 0; + res = k->startup_tpm ? k->startup_tpm(s) : 0; s->had_startup_error = (res != 0); @@ -90,8 +90,8 @@ void tpm_backend_reset(TPMBackend *s) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - if (k->ops->reset) { - k->ops->reset(s); + if (k->reset) { + k->reset(s); } tpm_backend_thread_end(s); @@ -103,34 +103,34 @@ void tpm_backend_cancel_cmd(TPMBackend *s) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - assert(k->ops->cancel_cmd); + assert(k->cancel_cmd); - k->ops->cancel_cmd(s); + k->cancel_cmd(s); } bool tpm_backend_get_tpm_established_flag(TPMBackend *s) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - return k->ops->get_tpm_established_flag ? - k->ops->get_tpm_established_flag(s) : false; + return k->get_tpm_established_flag ? + k->get_tpm_established_flag(s) : false; } int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - return k->ops->reset_tpm_established_flag ? - k->ops->reset_tpm_established_flag(s, locty) : 0; + return k->reset_tpm_established_flag ? + k->reset_tpm_established_flag(s, locty) : 0; } TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - assert(k->ops->get_tpm_version); + assert(k->get_tpm_version); - return k->ops->get_tpm_version(s); + return k->get_tpm_version(s); } TPMInfo *tpm_backend_query_tpm(TPMBackend *s) @@ -140,8 +140,9 @@ TPMInfo *tpm_backend_query_tpm(TPMBackend *s) info->id = g_strdup(s->id); info->model = s->fe_model; - info->options = k->ops->get_tpm_options ? - k->ops->get_tpm_options(s) : NULL; + if (k->get_tpm_options) { + info->options = k->get_tpm_options(s); + } return info; } diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 187a69e442d1..03ea5845dbc4 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -60,8 +60,6 @@ #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap)) -static const TPMDriverOps tpm_emulator_driver; - /* data structures */ typedef struct TPMEmulator { TPMBackend parent; @@ -504,20 +502,6 @@ static const QemuOptDesc tpm_emulator_cmdline_opts[] = { { /* end of list */ }, }; -static const TPMDriverOps tpm_emulator_driver = { - .type = TPM_TYPE_EMULATOR, - .opts = tpm_emulator_cmdline_opts, - .desc = "TPM emulator backend driver", - - .create = tpm_emulator_create, - .startup_tpm = tpm_emulator_startup_tpm, - .cancel_cmd = tpm_emulator_cancel_cmd, - .get_tpm_established_flag = tpm_emulator_get_tpm_established_flag, - .reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag, - .get_tpm_version = tpm_emulator_get_tpm_version, - .get_tpm_options = tpm_emulator_get_tpm_options, -}; - static void tpm_emulator_inst_init(Object *obj) { TPMEmulator *tpm_emu = TPM_EMULATOR(obj); @@ -565,7 +549,18 @@ static void tpm_emulator_inst_finalize(Object *obj) static void tpm_emulator_class_init(ObjectClass *klass, void *data) { TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass); - tbc->ops = &tpm_emulator_driver; + + tbc->type = TPM_TYPE_EMULATOR; + tbc->opts = tpm_emulator_cmdline_opts; + tbc->desc = "TPM emulator backend driver"; + tbc->create = tpm_emulator_create; + tbc->startup_tpm = tpm_emulator_startup_tpm; + tbc->cancel_cmd = tpm_emulator_cancel_cmd; + tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag; + tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag; + tbc->get_tpm_version = tpm_emulator_get_tpm_version; + tbc->get_tpm_options = tpm_emulator_get_tpm_options; + tbc->handle_request = tpm_emulator_handle_request; } diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index f04eab3e630b..d9da99bc8ed2 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -365,19 +365,6 @@ static const QemuOptDesc tpm_passthrough_cmdline_opts[] = { { /* end of list */ }, }; -static const TPMDriverOps tpm_passthrough_driver = { - .type = TPM_TYPE_PASSTHROUGH, - .opts = tpm_passthrough_cmdline_opts, - .desc = "Passthrough TPM backend driver", - .create = tpm_passthrough_create, - .reset = tpm_passthrough_reset, - .cancel_cmd = tpm_passthrough_cancel_cmd, - .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag, - .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag, - .get_tpm_version = tpm_passthrough_get_tpm_version, - .get_tpm_options = tpm_passthrough_get_tpm_options, -}; - static void tpm_passthrough_inst_init(Object *obj) { TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj); @@ -402,7 +389,17 @@ static void tpm_passthrough_class_init(ObjectClass *klass, void *data) { TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass); - tbc->ops = &tpm_passthrough_driver; + tbc->type = TPM_TYPE_PASSTHROUGH; + tbc->opts = tpm_passthrough_cmdline_opts; + tbc->desc = "Passthrough TPM backend driver"; + tbc->create = tpm_passthrough_create; + tbc->reset = tpm_passthrough_reset; + tbc->cancel_cmd = tpm_passthrough_cancel_cmd; + tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag; + tbc->reset_tpm_established_flag = + tpm_passthrough_reset_tpm_established_flag; + tbc->get_tpm_version = tpm_passthrough_get_tpm_version; + tbc->get_tpm_options = tpm_passthrough_get_tpm_options; tbc->handle_request = tpm_passthrough_handle_request; } diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 63093551a169..a4288df03846 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -29,7 +29,7 @@ typedef struct TPMBackendClass TPMBackendClass; typedef struct TPMBackend TPMBackend; -typedef struct TPMDriverOps TPMDriverOps; + typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done); typedef enum TPMBackendCmd { @@ -59,14 +59,6 @@ struct TPMBackend { struct TPMBackendClass { ObjectClass parent_class; - const TPMDriverOps *ops; - - void (*opened)(TPMBackend *s, Error **errp); - - void (*handle_request)(TPMBackend *s, TPMBackendCmd cmd); -}; - -struct TPMDriverOps { enum TpmType type; const QemuOptDesc *opts; /* get a descriptive text of the backend to display to the user */ @@ -90,8 +82,11 @@ struct TPMDriverOps { TPMVersion (*get_tpm_version)(TPMBackend *t); TpmTypeOptions *(*get_tpm_options)(TPMBackend *t); -}; + void (*opened)(TPMBackend *s, Error **errp); + + void (*handle_request)(TPMBackend *s, TPMBackendCmd cmd); +}; /** * tpm_backend_get_type: diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 0c41f1212f2c..c9529df67fdd 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -2602,7 +2602,6 @@ sub process { SCSIBusInfo| SCSIReqOps| Spice[A-Z][a-zA-Z0-9]*Interface| - TPMDriverOps| USBDesc[A-Z][a-zA-Z0-9]*| VhostOps| VMStateDescription| diff --git a/tpm.c b/tpm.c index 75a589b837a1..4882501bd7fa 100644 --- a/tpm.c +++ b/tpm.c @@ -32,11 +32,10 @@ void tpm_register_model(enum TpmModel model) #ifdef CONFIG_TPM -static const TPMDriverOps * -tpm_driver_find_by_type(enum TpmType type) +static const TPMBackendClass * +tpm_be_find_by_type(enum TpmType type) { ObjectClass *oc; - TPMBackendClass *bc; char *typename = g_strdup_printf("tpm-%s", TpmType_str(type)); oc = object_class_by_name(typename); @@ -46,8 +45,7 @@ tpm_driver_find_by_type(enum TpmType type) return NULL; } - bc = TPM_BACKEND_CLASS(oc); - return bc->ops; + return TPM_BACKEND_CLASS(oc); } /* @@ -61,11 +59,11 @@ static void tpm_display_backend_drivers(void) fprintf(stderr, "Supported TPM types (choose only one):\n"); for (i = 0; i < TPM_TYPE__MAX; i++) { - const TPMDriverOps *ops = tpm_driver_find_by_type(i); - if (!ops) { + const TPMBackendClass *bc = tpm_be_find_by_type(i); + if (!bc) { continue; } - fprintf(stderr, "%12s %s\n", TpmType_str(i), ops->desc); + fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc); } fprintf(stderr, "\n"); } @@ -92,7 +90,7 @@ static int configure_tpm(QemuOpts *opts) { const char *value; const char *id; - const TPMDriverOps *be; + const TPMBackendClass *be; TPMBackend *drv; Error *local_err = NULL; int i; @@ -116,7 +114,7 @@ static int configure_tpm(QemuOpts *opts) } i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL); - be = i >= 0 ? tpm_driver_find_by_type(i) : NULL; + be = i >= 0 ? tpm_be_find_by_type(i) : NULL; if (be == NULL) { error_report(QERR_INVALID_PARAMETER_VALUE, "type", "a TPM backend type"); @@ -234,7 +232,7 @@ TpmTypeList *qmp_query_tpm_types(Error **errp) TpmTypeList *head = NULL, *prev = NULL, *cur_item; for (i = 0; i < TPM_TYPE__MAX; i++) { - if (!tpm_driver_find_by_type(i)) { + if (!tpm_be_find_by_type(i)) { continue; } cur_item = g_new0(TpmTypeList, 1); From 27a79d96b42a819122c83745a5d837c9664ba915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:50 +0200 Subject: [PATCH 09/21] tpm: remove init() class method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No backend use it. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 4 +--- include/sysemu/tpm_backend.h | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/backends/tpm.c b/backends/tpm.c index ca3a78eea8e1..dc750d48c9a4 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -47,13 +47,11 @@ enum TpmType tpm_backend_get_type(TPMBackend *s) int tpm_backend_init(TPMBackend *s, TPMState *state, TPMRecvDataCB *datacb) { - TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - s->tpm_state = state; s->recv_data_callback = datacb; s->had_startup_error = false; - return k->init ? k->init(s) : 0; + return 0; } int tpm_backend_startup_tpm(TPMBackend *s) diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index a4288df03846..b08f98550093 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -66,8 +66,6 @@ struct TPMBackendClass { TPMBackend *(*create)(QemuOpts *opts, const char *id); - /* initialize the backend */ - int (*init)(TPMBackend *t); /* start up the TPM on the backend */ int (*startup_tpm)(TPMBackend *t); From 76ae76bfea67ca972d1158dfdea07e27e4c0852e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:51 +0200 Subject: [PATCH 10/21] tpm: remove configure_tpm() hop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- tpm.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tpm.c b/tpm.c index 4882501bd7fa..45520f555d16 100644 --- a/tpm.c +++ b/tpm.c @@ -86,7 +86,7 @@ TPMBackend *qemu_find_tpm(const char *id) return NULL; } -static int configure_tpm(QemuOpts *opts) +static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp) { const char *value; const char *id; @@ -145,11 +145,6 @@ static int configure_tpm(QemuOpts *opts) return 0; } -static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp) -{ - return configure_tpm(opts); -} - /* * Walk the list of TPM backend drivers that are in use and call their * destroy function to have them cleaned up. From 905e78ba25c613a9daa7adb4e8fb3aec0fa4a2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:52 +0200 Subject: [PATCH 11/21] tpm: remove unused TPMBackendCmd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is only handling of request so far in both backends. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 7 ++---- hw/tpm/tpm_emulator.c | 42 ++++++++++++++---------------------- hw/tpm/tpm_passthrough.c | 29 +++++++++---------------- include/sysemu/tpm_backend.h | 9 +------- 4 files changed, 29 insertions(+), 58 deletions(-) diff --git a/backends/tpm.c b/backends/tpm.c index dc750d48c9a4..34e82085ec06 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -25,13 +25,12 @@ static void tpm_backend_worker_thread(gpointer data, gpointer user_data) TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); assert(k->handle_request != NULL); - k->handle_request(s, (TPMBackendCmd)data); + k->handle_request(s); } static void tpm_backend_thread_end(TPMBackend *s) { if (s->thread_pool) { - g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL); g_thread_pool_free(s->thread_pool, FALSE, TRUE); s->thread_pool = NULL; } @@ -64,7 +63,6 @@ int tpm_backend_startup_tpm(TPMBackend *s) s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, NULL); - g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL); res = k->startup_tpm ? k->startup_tpm(s) : 0; @@ -80,8 +78,7 @@ bool tpm_backend_had_startup_error(TPMBackend *s) void tpm_backend_deliver_request(TPMBackend *s) { - g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, - NULL); + g_thread_pool_push(s->thread_pool, NULL, NULL); } void tpm_backend_reset(TPMBackend *s) diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 03ea5845dbc4..a6763e8ea23d 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -172,39 +172,29 @@ static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number) return 0; } -static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd cmd) +static void tpm_emulator_handle_request(TPMBackend *tb) { TPMEmulator *tpm_emu = TPM_EMULATOR(tb); TPMLocality *locty = NULL; bool selftest_done = false; Error *err = NULL; - DPRINTF("processing command type %d", cmd); - - switch (cmd) { - case TPM_BACKEND_CMD_PROCESS_CMD: - locty = tb->tpm_state->locty_data; - if (tpm_emulator_set_locality(tpm_emu, - tb->tpm_state->locty_number) < 0 || - tpm_emulator_unix_tx_bufs(tpm_emu, locty->w_buffer.buffer, - locty->w_offset, locty->r_buffer.buffer, - locty->r_buffer.size, &selftest_done, - &err) < 0) { - tpm_util_write_fatal_error_response(locty->r_buffer.buffer, - locty->r_buffer.size); - error_report_err(err); - } - - tb->recv_data_callback(tb->tpm_state, tb->tpm_state->locty_number, - selftest_done); - - break; - case TPM_BACKEND_CMD_INIT: - case TPM_BACKEND_CMD_END: - case TPM_BACKEND_CMD_TPM_RESET: - /* nothing to do */ - break; + DPRINTF("processing TPM command"); + + locty = tb->tpm_state->locty_data; + if (tpm_emulator_set_locality(tpm_emu, + tb->tpm_state->locty_number) < 0 || + tpm_emulator_unix_tx_bufs(tpm_emu, locty->w_buffer.buffer, + locty->w_offset, locty->r_buffer.buffer, + locty->r_buffer.size, &selftest_done, + &err) < 0) { + tpm_util_write_fatal_error_response(locty->r_buffer.buffer, + locty->r_buffer.size); + error_report_err(err); } + + tb->recv_data_callback(tb->tpm_state, tb->tpm_state->locty_number, + selftest_done); } static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu) diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index d9da99bc8ed2..5cd988e8a4da 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -149,29 +149,20 @@ static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt, selftest_done); } -static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd cmd) +static void tpm_passthrough_handle_request(TPMBackend *tb) { TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); bool selftest_done = false; - DPRINTF("tpm_passthrough: processing command type %d\n", cmd); - - switch (cmd) { - case TPM_BACKEND_CMD_PROCESS_CMD: - tpm_passthrough_unix_transfer(tpm_pt, - tb->tpm_state->locty_data, - &selftest_done); - - tb->recv_data_callback(tb->tpm_state, - tb->tpm_state->locty_number, - selftest_done); - break; - case TPM_BACKEND_CMD_INIT: - case TPM_BACKEND_CMD_END: - case TPM_BACKEND_CMD_TPM_RESET: - /* nothing to do */ - break; - } + DPRINTF("tpm_passthrough: processing command\n"); + + tpm_passthrough_unix_transfer(tpm_pt, + tb->tpm_state->locty_data, + &selftest_done); + + tb->recv_data_callback(tb->tpm_state, + tb->tpm_state->locty_number, + selftest_done); } static void tpm_passthrough_reset(TPMBackend *tb) diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index b08f98550093..7d7ebfc21ddc 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -32,13 +32,6 @@ typedef struct TPMBackend TPMBackend; typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done); -typedef enum TPMBackendCmd { - TPM_BACKEND_CMD_INIT = 1, - TPM_BACKEND_CMD_PROCESS_CMD, - TPM_BACKEND_CMD_END, - TPM_BACKEND_CMD_TPM_RESET, -} TPMBackendCmd; - struct TPMBackend { Object parent; @@ -83,7 +76,7 @@ struct TPMBackendClass { void (*opened)(TPMBackend *s, Error **errp); - void (*handle_request)(TPMBackend *s, TPMBackendCmd cmd); + void (*handle_request)(TPMBackend *s); }; /** From 54aa36d5c8ac70db25923c7a8aa11c8f7aadd225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:53 +0200 Subject: [PATCH 12/21] tpm: remove needless cast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_passthrough.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index 5cd988e8a4da..fed3d697992a 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -96,7 +96,7 @@ static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, is_selftest = tpm_util_is_selftest(in, in_len); - ret = qemu_write_full(tpm_pt->tpm_fd, (const void *)in, (size_t)in_len); + ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len); if (ret != in_len) { if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) { error_report("tpm_passthrough: error while transmitting data " From d1fd6b563d44a1132f8a5758f8f7bafba548502c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:54 +0200 Subject: [PATCH 13/21] tpm: remove locty argument from receive_cb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tpm_state is passed as argument, the assert() is pointless since we give it the value of tpm_state->locty_number already. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_emulator.c | 3 +-- hw/tpm/tpm_passthrough.c | 4 +--- hw/tpm/tpm_tis.c | 5 ++--- include/sysemu/tpm_backend.h | 2 +- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index a6763e8ea23d..8fbe9ad74a2d 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -193,8 +193,7 @@ static void tpm_emulator_handle_request(TPMBackend *tb) error_report_err(err); } - tb->recv_data_callback(tb->tpm_state, tb->tpm_state->locty_number, - selftest_done); + tb->recv_data_callback(tb->tpm_state, selftest_done); } static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu) diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index fed3d697992a..0ae45969324d 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -160,9 +160,7 @@ static void tpm_passthrough_handle_request(TPMBackend *tb) tb->tpm_state->locty_data, &selftest_done); - tb->recv_data_callback(tb->tpm_state, - tb->tpm_state->locty_number, - selftest_done); + tb->recv_data_callback(tb->tpm_state, selftest_done); } static void tpm_passthrough_reset(TPMBackend *tb) diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index c54dac32de3f..345a4fbee543 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -371,14 +371,13 @@ static void tpm_tis_receive_bh(void *opaque) /* * Callback from the TPM to indicate that the response was received. */ -static void tpm_tis_receive_cb(TPMState *s, uint8_t locty, +static void tpm_tis_receive_cb(TPMState *s, bool is_selftest_done) { TPMTISEmuState *tis = &s->s.tis; + uint8_t locty = s->locty_number; uint8_t l; - assert(s->locty_number == locty); - if (is_selftest_done) { for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) { tis->loc[locty].sts |= TPM_TIS_STS_SELFTEST_DONE; diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 7d7ebfc21ddc..9c83a512e1b1 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -30,7 +30,7 @@ typedef struct TPMBackendClass TPMBackendClass; typedef struct TPMBackend TPMBackend; -typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done); +typedef void (TPMRecvDataCB)(TPMState *, bool selftest_done); struct TPMBackend { Object parent; From 0e43b7e61ce677e154584523943c1651779baccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:55 +0200 Subject: [PATCH 14/21] tpm: add TPMBackendCmd to hold the request state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This simplifies a bit locality handling, and argument passing, and could pave the way to queuing requests (if that makes sense). Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 6 +++--- hw/tpm/tpm_emulator.c | 29 +++++++++++++++-------------- hw/tpm/tpm_int.h | 1 + hw/tpm/tpm_passthrough.c | 24 +++++------------------- hw/tpm/tpm_tis.c | 18 +++++++++++++----- include/sysemu/tpm_backend.h | 16 +++++++++++++--- 6 files changed, 50 insertions(+), 44 deletions(-) diff --git a/backends/tpm.c b/backends/tpm.c index 34e82085ec06..dc7c831ff8a6 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -25,7 +25,7 @@ static void tpm_backend_worker_thread(gpointer data, gpointer user_data) TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); assert(k->handle_request != NULL); - k->handle_request(s); + k->handle_request(s, (TPMBackendCmd *)data); } static void tpm_backend_thread_end(TPMBackend *s) @@ -76,9 +76,9 @@ bool tpm_backend_had_startup_error(TPMBackend *s) return s->had_startup_error; } -void tpm_backend_deliver_request(TPMBackend *s) +void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd) { - g_thread_pool_push(s->thread_pool, NULL, NULL); + g_thread_pool_push(s->thread_pool, cmd, NULL); } void tpm_backend_reset(TPMBackend *s) diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 8fbe9ad74a2d..0b1a99fcc598 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -172,28 +172,29 @@ static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number) return 0; } -static void tpm_emulator_handle_request(TPMBackend *tb) +static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) { TPMEmulator *tpm_emu = TPM_EMULATOR(tb); - TPMLocality *locty = NULL; - bool selftest_done = false; Error *err = NULL; DPRINTF("processing TPM command"); - locty = tb->tpm_state->locty_data; - if (tpm_emulator_set_locality(tpm_emu, - tb->tpm_state->locty_number) < 0 || - tpm_emulator_unix_tx_bufs(tpm_emu, locty->w_buffer.buffer, - locty->w_offset, locty->r_buffer.buffer, - locty->r_buffer.size, &selftest_done, - &err) < 0) { - tpm_util_write_fatal_error_response(locty->r_buffer.buffer, - locty->r_buffer.size); - error_report_err(err); + if (tpm_emulator_set_locality(tpm_emu, tb->tpm_state->locty_number) < 0) { + goto error; + } + + if (tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len, + cmd->out, cmd->out_len, + &cmd->selftest_done, &err) < 0) { + goto error; } - tb->recv_data_callback(tb->tpm_state, selftest_done); + tb->recv_data_callback(tb->tpm_state); + return; + +error: + tpm_util_write_fatal_error_response(cmd->out, cmd->out_len); + error_report_err(err); } static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu) diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index f2f285b3cc1d..6d7b3dc850f6 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -26,6 +26,7 @@ struct TPMState { uint8_t locty_number; TPMLocality *locty_data; + TPMBackendCmd cmd; char *backend; TPMBackend *be_driver; diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index 0ae45969324d..93d72b8e9e54 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -137,30 +137,16 @@ static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, return ret; } -static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt, - const TPMLocality *locty_data, - bool *selftest_done) -{ - return tpm_passthrough_unix_tx_bufs(tpm_pt, - locty_data->w_buffer.buffer, - locty_data->w_offset, - locty_data->r_buffer.buffer, - locty_data->r_buffer.size, - selftest_done); -} - -static void tpm_passthrough_handle_request(TPMBackend *tb) +static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) { TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); - bool selftest_done = false; - DPRINTF("tpm_passthrough: processing command\n"); + DPRINTF("tpm_passthrough: processing command %p\n", cmd); - tpm_passthrough_unix_transfer(tpm_pt, - tb->tpm_state->locty_data, - &selftest_done); + tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len, + cmd->out, cmd->out_len, &cmd->selftest_done); - tb->recv_data_callback(tb->tpm_state, selftest_done); + tb->recv_data_callback(tb->tpm_state); } static void tpm_passthrough_reset(TPMBackend *tb) diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index 345a4fbee543..ffed7bfaf928 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -215,7 +215,15 @@ static void tpm_tis_tpm_send(TPMState *s, uint8_t locty) */ tis->loc[locty].state = TPM_TIS_STATE_EXECUTION; - tpm_backend_deliver_request(s->be_driver); + s->cmd = (TPMBackendCmd) { + .locty = locty, + .in = s->locty_data->w_buffer.buffer, + .in_len = s->locty_data->w_offset, + .out = s->locty_data->r_buffer.buffer, + .out_len = s->locty_data->r_buffer.size + }; + + tpm_backend_deliver_request(s->be_driver, &s->cmd); } /* raise an interrupt if allowed */ @@ -352,7 +360,7 @@ static void tpm_tis_receive_bh(void *opaque) { TPMState *s = opaque; TPMTISEmuState *tis = &s->s.tis; - uint8_t locty = s->locty_number; + uint8_t locty = s->cmd.locty; tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE); @@ -371,11 +379,11 @@ static void tpm_tis_receive_bh(void *opaque) /* * Callback from the TPM to indicate that the response was received. */ -static void tpm_tis_receive_cb(TPMState *s, - bool is_selftest_done) +static void tpm_tis_receive_cb(TPMState *s) { TPMTISEmuState *tis = &s->s.tis; - uint8_t locty = s->locty_number; + bool is_selftest_done = s->cmd.selftest_done; + uint8_t locty = s->cmd.locty; uint8_t l; if (is_selftest_done) { diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 9c83a512e1b1..3bb90be3def0 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -30,7 +30,16 @@ typedef struct TPMBackendClass TPMBackendClass; typedef struct TPMBackend TPMBackend; -typedef void (TPMRecvDataCB)(TPMState *, bool selftest_done); +typedef void (TPMRecvDataCB)(TPMState *); + +typedef struct TPMBackendCmd { + uint8_t locty; + const uint8_t *in; + uint32_t in_len; + uint8_t *out; + uint32_t out_len; + bool selftest_done; +} TPMBackendCmd; struct TPMBackend { Object parent; @@ -76,7 +85,7 @@ struct TPMBackendClass { void (*opened)(TPMBackend *s, Error **errp); - void (*handle_request)(TPMBackend *s); + void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd); }; /** @@ -121,11 +130,12 @@ bool tpm_backend_had_startup_error(TPMBackend *s); /** * tpm_backend_deliver_request: * @s: the backend to send the request to + * @cmd: the command to deliver * * Send a request to the backend. The backend will then send the request * to the TPM implementation. */ -void tpm_backend_deliver_request(TPMBackend *s); +void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd); /** * tpm_backend_reset: From c106ede9c85bf4b38c52dbee96b00c2aa0a97dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:56 +0200 Subject: [PATCH 15/21] tpm-emulator: fix error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous patch cleaned up a bit error handling, and exposed an existing bug: error_report_err() could be called with a NULL error. Instead, make tpm_emulator_set_locality() set the error. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_emulator.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 0b1a99fcc598..a613cec4568b 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -141,7 +141,8 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu, return 0; } -static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number) +static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number, + Error **errp) { ptm_loc loc; @@ -155,15 +156,15 @@ static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number) loc.u.req.loc = locty_number; if (tpm_emulator_ctrlcmd(&tpm_emu->ctrl_chr, CMD_SET_LOCALITY, &loc, sizeof(loc), sizeof(loc)) < 0) { - error_report("tpm-emulator: could not set locality : %s", - strerror(errno)); + error_setg(errp, "tpm-emulator: could not set locality : %s", + strerror(errno)); return -1; } loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result); if (loc.u.resp.tpm_result != 0) { - error_report("tpm-emulator: TPM result for set locality : 0x%x", - loc.u.resp.tpm_result); + error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x", + loc.u.resp.tpm_result); return -1; } @@ -179,7 +180,8 @@ static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) DPRINTF("processing TPM command"); - if (tpm_emulator_set_locality(tpm_emu, tb->tpm_state->locty_number) < 0) { + if (tpm_emulator_set_locality(tpm_emu, + tb->tpm_state->locty_number, &err) < 0) { goto error; } From d2809766250d0e6b358588819c6c2e422685edfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:57 +0200 Subject: [PATCH 16/21] tpm: remove locty_data from TPMState MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep it internal to tpm-tis instead. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_emulator.c | 3 +-- hw/tpm/tpm_int.h | 2 -- hw/tpm/tpm_tis.c | 12 +++++------- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index a613cec4568b..6500b86b41e7 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -180,8 +180,7 @@ static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) DPRINTF("processing TPM command"); - if (tpm_emulator_set_locality(tpm_emu, - tb->tpm_state->locty_number, &err) < 0) { + if (tpm_emulator_set_locality(tpm_emu, cmd->locty, &err) < 0) { goto error; } diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index 6d7b3dc850f6..252c4082efa7 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -24,8 +24,6 @@ struct TPMState { TPMTISEmuState tis; } s; - uint8_t locty_number; - TPMLocality *locty_data; TPMBackendCmd cmd; char *backend; diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index ffed7bfaf928..ad2845622ec6 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -203,12 +203,10 @@ static void tpm_tis_sts_set(TPMLocality *l, uint32_t flags) static void tpm_tis_tpm_send(TPMState *s, uint8_t locty) { TPMTISEmuState *tis = &s->s.tis; + TPMLocality *locty_data = &tis->loc[locty]; tpm_tis_show_buffer(&tis->loc[locty].w_buffer, "tpm_tis: To TPM"); - s->locty_number = locty; - s->locty_data = &tis->loc[locty]; - /* * w_offset serves as length indicator for length of data; * it's reset when the response comes back @@ -217,10 +215,10 @@ static void tpm_tis_tpm_send(TPMState *s, uint8_t locty) s->cmd = (TPMBackendCmd) { .locty = locty, - .in = s->locty_data->w_buffer.buffer, - .in_len = s->locty_data->w_offset, - .out = s->locty_data->r_buffer.buffer, - .out_len = s->locty_data->r_buffer.size + .in = locty_data->w_buffer.buffer, + .in_len = locty_data->w_offset, + .out = locty_data->r_buffer.buffer, + .out_len = locty_data->r_buffer.size }; tpm_backend_deliver_request(s->be_driver, &s->cmd); From 5086bf978445406312a4ff4127a7e2e8ac0fc655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:58 +0200 Subject: [PATCH 17/21] tpm-tis: move TPMState to TIS header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_int.h | 23 ++--------------------- hw/tpm/tpm_tis.h | 19 +++++++++++++++++++ hw/tpm/tpm_util.c | 1 + 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index 252c4082efa7..e231d0eb4f0b 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -12,28 +12,9 @@ #ifndef TPM_TPM_INT_H #define TPM_TPM_INT_H -#include "exec/memory.h" -#include "tpm_tis.h" +#include "qemu/osdep.h" -/* overall state of the TPM interface */ -struct TPMState { - ISADevice busdev; - MemoryRegion mmio; - - union { - TPMTISEmuState tis; - } s; - - TPMBackendCmd cmd; - - char *backend; - TPMBackend *be_driver; - TPMVersion be_tpm_version; -}; - -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS) - -#define TPM_STANDARD_CMDLINE_OPTS \ +#define TPM_STANDARD_CMDLINE_OPTS \ { \ .name = "type", \ .type = QEMU_OPT_STRING, \ diff --git a/hw/tpm/tpm_tis.h b/hw/tpm/tpm_tis.h index 31e612a7f4dd..30e35eef64c6 100644 --- a/hw/tpm/tpm_tis.h +++ b/hw/tpm/tpm_tis.h @@ -72,4 +72,23 @@ typedef struct TPMTISEmuState { uint32_t irq_num; } TPMTISEmuState; +/* overall state of the TPM interface */ +struct TPMState { + ISADevice busdev; + MemoryRegion mmio; + + union { + TPMTISEmuState tis; + } s; + + uint8_t locty_number; + TPMBackendCmd cmd; + + char *backend; + TPMBackend *be_driver; + TPMVersion be_tpm_version; +}; + +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS) + #endif /* TPM_TPM_TIS_H */ diff --git a/hw/tpm/tpm_util.c b/hw/tpm/tpm_util.c index 73d77965fdda..daf1faa63d47 100644 --- a/hw/tpm/tpm_util.c +++ b/hw/tpm/tpm_util.c @@ -22,6 +22,7 @@ #include "qemu/osdep.h" #include "tpm_util.h" #include "tpm_int.h" +#include "exec/memory.h" /* * Write an error message in the given output buffer. From 732cd5877ef07595d1d42745ef81135a27cdf3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:55:59 +0200 Subject: [PATCH 18/21] tpm-tis: remove tpm_tis.h header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The definitions are now private to TIS implementation. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_passthrough.c | 1 - hw/tpm/tpm_tis.c | 72 +++++++++++++++++++++++++++++- hw/tpm/tpm_tis.h | 94 ---------------------------------------- 3 files changed, 71 insertions(+), 96 deletions(-) delete mode 100644 hw/tpm/tpm_tis.h diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index 93d72b8e9e54..4274164a61df 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -31,7 +31,6 @@ #include "hw/hw.h" #include "hw/i386/pc.h" #include "qapi/clone-visitor.h" -#include "tpm_tis.h" #include "tpm_util.h" #define DEBUG_TPM 0 diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index ad2845622ec6..73cda41dc670 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -23,6 +23,7 @@ */ #include "qemu/osdep.h" +#include "hw/isa/isa.h" #include "sysemu/tpm_backend.h" #include "tpm_int.h" #include "sysemu/block-backend.h" @@ -30,10 +31,79 @@ #include "hw/hw.h" #include "hw/i386/pc.h" #include "hw/pci/pci_ids.h" -#include "tpm_tis.h" #include "qapi/error.h" #include "qemu-common.h" #include "qemu/main-loop.h" +#include "hw/acpi/tpm.h" + +#define TPM_TIS_NUM_LOCALITIES 5 /* per spec */ +#define TPM_TIS_LOCALITY_SHIFT 12 +#define TPM_TIS_NO_LOCALITY 0xff + +#define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) + +#define TPM_TIS_BUFFER_MAX 4096 + +typedef enum { + TPM_TIS_STATE_IDLE = 0, + TPM_TIS_STATE_READY, + TPM_TIS_STATE_COMPLETION, + TPM_TIS_STATE_EXECUTION, + TPM_TIS_STATE_RECEPTION, +} TPMTISState; + +typedef struct TPMSizedBuffer { + uint32_t size; + uint8_t *buffer; +} TPMSizedBuffer; + +/* locality data -- all fields are persisted */ +typedef struct TPMLocality { + TPMTISState state; + uint8_t access; + uint32_t sts; + uint32_t iface_id; + uint32_t inte; + uint32_t ints; + + uint16_t w_offset; + uint16_t r_offset; + TPMSizedBuffer w_buffer; + TPMSizedBuffer r_buffer; +} TPMLocality; + +typedef struct TPMTISEmuState { + QEMUBH *bh; + uint32_t offset; + uint8_t buf[TPM_TIS_BUFFER_MAX]; + + uint8_t active_locty; + uint8_t aborting_locty; + uint8_t next_locty; + + TPMLocality loc[TPM_TIS_NUM_LOCALITIES]; + + qemu_irq irq; + uint32_t irq_num; +} TPMTISEmuState; + +struct TPMState { + ISADevice busdev; + MemoryRegion mmio; + + union { + TPMTISEmuState tis; + } s; + + uint8_t locty_number; + TPMBackendCmd cmd; + + char *backend; + TPMBackend *be_driver; + TPMVersion be_tpm_version; +}; + +#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS) #define DEBUG_TIS 0 diff --git a/hw/tpm/tpm_tis.h b/hw/tpm/tpm_tis.h deleted file mode 100644 index 30e35eef64c6..000000000000 --- a/hw/tpm/tpm_tis.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * tpm_tis.h - QEMU's TPM TIS interface emulator - * - * Copyright (C) 2006, 2010-2013 IBM Corporation - * - * Authors: - * Stefan Berger - * David Safford - * - * This work is licensed under the terms of the GNU GPL, version 2 or later. - * See the COPYING file in the top-level directory. - * - * Implementation of the TIS interface according to specs found at - * http://www.trustedcomputinggroup.org - * - */ -#ifndef TPM_TPM_TIS_H -#define TPM_TPM_TIS_H - -#include "hw/isa/isa.h" -#include "hw/acpi/tpm.h" -#include "qemu-common.h" - -#define TPM_TIS_NUM_LOCALITIES 5 /* per spec */ -#define TPM_TIS_LOCALITY_SHIFT 12 -#define TPM_TIS_NO_LOCALITY 0xff - -#define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) - -#define TPM_TIS_BUFFER_MAX 4096 - -typedef enum { - TPM_TIS_STATE_IDLE = 0, - TPM_TIS_STATE_READY, - TPM_TIS_STATE_COMPLETION, - TPM_TIS_STATE_EXECUTION, - TPM_TIS_STATE_RECEPTION, -} TPMTISState; - -typedef struct TPMSizedBuffer { - uint32_t size; - uint8_t *buffer; -} TPMSizedBuffer; - -/* locality data -- all fields are persisted */ -typedef struct TPMLocality { - TPMTISState state; - uint8_t access; - uint32_t sts; - uint32_t iface_id; - uint32_t inte; - uint32_t ints; - - uint16_t w_offset; - uint16_t r_offset; - TPMSizedBuffer w_buffer; - TPMSizedBuffer r_buffer; -} TPMLocality; - -typedef struct TPMTISEmuState { - QEMUBH *bh; - uint32_t offset; - uint8_t buf[TPM_TIS_BUFFER_MAX]; - - uint8_t active_locty; - uint8_t aborting_locty; - uint8_t next_locty; - - TPMLocality loc[TPM_TIS_NUM_LOCALITIES]; - - qemu_irq irq; - uint32_t irq_num; -} TPMTISEmuState; - -/* overall state of the TPM interface */ -struct TPMState { - ISADevice busdev; - MemoryRegion mmio; - - union { - TPMTISEmuState tis; - } s; - - uint8_t locty_number; - TPMBackendCmd cmd; - - char *backend; - TPMBackend *be_driver; - TPMVersion be_tpm_version; -}; - -#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS) - -#endif /* TPM_TPM_TIS_H */ From 3d4960c7addeb08513f04abaa3c52bb3c69f80f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:56:00 +0200 Subject: [PATCH 19/21] tpm-tis: fold TPMTISEmuState in TPMState MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- hw/tpm/tpm_tis.c | 336 ++++++++++++++++++++++------------------------- 1 file changed, 157 insertions(+), 179 deletions(-) diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index 73cda41dc670..d84eec48b4af 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -72,7 +72,10 @@ typedef struct TPMLocality { TPMSizedBuffer r_buffer; } TPMLocality; -typedef struct TPMTISEmuState { +struct TPMState { + ISADevice busdev; + MemoryRegion mmio; + QEMUBH *bh; uint32_t offset; uint8_t buf[TPM_TIS_BUFFER_MAX]; @@ -85,15 +88,6 @@ typedef struct TPMTISEmuState { qemu_irq irq; uint32_t irq_num; -} TPMTISEmuState; - -struct TPMState { - ISADevice busdev; - MemoryRegion mmio; - - union { - TPMTISEmuState tis; - } s; uint8_t locty_number; TPMBackendCmd cmd; @@ -272,16 +266,15 @@ static void tpm_tis_sts_set(TPMLocality *l, uint32_t flags) */ static void tpm_tis_tpm_send(TPMState *s, uint8_t locty) { - TPMTISEmuState *tis = &s->s.tis; - TPMLocality *locty_data = &tis->loc[locty]; + TPMLocality *locty_data = &s->loc[locty]; - tpm_tis_show_buffer(&tis->loc[locty].w_buffer, "tpm_tis: To TPM"); + tpm_tis_show_buffer(&s->loc[locty].w_buffer, "tpm_tis: To TPM"); /* * w_offset serves as length indicator for length of data; * it's reset when the response comes back */ - tis->loc[locty].state = TPM_TIS_STATE_EXECUTION; + s->loc[locty].state = TPM_TIS_STATE_EXECUTION; s->cmd = (TPMBackendCmd) { .locty = locty, @@ -297,17 +290,15 @@ static void tpm_tis_tpm_send(TPMState *s, uint8_t locty) /* raise an interrupt if allowed */ static void tpm_tis_raise_irq(TPMState *s, uint8_t locty, uint32_t irqmask) { - TPMTISEmuState *tis = &s->s.tis; - if (!TPM_TIS_IS_VALID_LOCTY(locty)) { return; } - if ((tis->loc[locty].inte & TPM_TIS_INT_ENABLED) && - (tis->loc[locty].inte & irqmask)) { + if ((s->loc[locty].inte & TPM_TIS_INT_ENABLED) && + (s->loc[locty].inte & irqmask)) { DPRINTF("tpm_tis: Raising IRQ for flag %08x\n", irqmask); - qemu_irq_raise(s->s.tis.irq); - tis->loc[locty].ints |= irqmask; + qemu_irq_raise(s->irq); + s->loc[locty].ints |= irqmask; } } @@ -319,7 +310,7 @@ static uint32_t tpm_tis_check_request_use_except(TPMState *s, uint8_t locty) if (l == locty) { continue; } - if ((s->s.tis.loc[l].access & TPM_TIS_ACCESS_REQUEST_USE)) { + if ((s->loc[l].access & TPM_TIS_ACCESS_REQUEST_USE)) { return 1; } } @@ -329,14 +320,13 @@ static uint32_t tpm_tis_check_request_use_except(TPMState *s, uint8_t locty) static void tpm_tis_new_active_locality(TPMState *s, uint8_t new_active_locty) { - TPMTISEmuState *tis = &s->s.tis; - bool change = (s->s.tis.active_locty != new_active_locty); + bool change = (s->active_locty != new_active_locty); bool is_seize; uint8_t mask; - if (change && TPM_TIS_IS_VALID_LOCTY(s->s.tis.active_locty)) { + if (change && TPM_TIS_IS_VALID_LOCTY(s->active_locty)) { is_seize = TPM_TIS_IS_VALID_LOCTY(new_active_locty) && - tis->loc[new_active_locty].access & TPM_TIS_ACCESS_SEIZE; + s->loc[new_active_locty].access & TPM_TIS_ACCESS_SEIZE; if (is_seize) { mask = ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY); @@ -345,73 +335,70 @@ static void tpm_tis_new_active_locality(TPMState *s, uint8_t new_active_locty) TPM_TIS_ACCESS_REQUEST_USE); } /* reset flags on the old active locality */ - tis->loc[s->s.tis.active_locty].access &= mask; + s->loc[s->active_locty].access &= mask; if (is_seize) { - tis->loc[tis->active_locty].access |= TPM_TIS_ACCESS_BEEN_SEIZED; + s->loc[s->active_locty].access |= TPM_TIS_ACCESS_BEEN_SEIZED; } } - tis->active_locty = new_active_locty; + s->active_locty = new_active_locty; - DPRINTF("tpm_tis: Active locality is now %d\n", s->s.tis.active_locty); + DPRINTF("tpm_tis: Active locality is now %d\n", s->active_locty); if (TPM_TIS_IS_VALID_LOCTY(new_active_locty)) { /* set flags on the new active locality */ - tis->loc[new_active_locty].access |= TPM_TIS_ACCESS_ACTIVE_LOCALITY; - tis->loc[new_active_locty].access &= ~(TPM_TIS_ACCESS_REQUEST_USE | + s->loc[new_active_locty].access |= TPM_TIS_ACCESS_ACTIVE_LOCALITY; + s->loc[new_active_locty].access &= ~(TPM_TIS_ACCESS_REQUEST_USE | TPM_TIS_ACCESS_SEIZE); } if (change) { - tpm_tis_raise_irq(s, tis->active_locty, TPM_TIS_INT_LOCALITY_CHANGED); + tpm_tis_raise_irq(s, s->active_locty, TPM_TIS_INT_LOCALITY_CHANGED); } } /* abort -- this function switches the locality */ static void tpm_tis_abort(TPMState *s, uint8_t locty) { - TPMTISEmuState *tis = &s->s.tis; - - tis->loc[locty].r_offset = 0; - tis->loc[locty].w_offset = 0; + s->loc[locty].r_offset = 0; + s->loc[locty].w_offset = 0; - DPRINTF("tpm_tis: tis_abort: new active locality is %d\n", tis->next_locty); + DPRINTF("tpm_tis: tis_abort: new active locality is %d\n", s->next_locty); /* * Need to react differently depending on who's aborting now and * which locality will become active afterwards. */ - if (tis->aborting_locty == tis->next_locty) { - tis->loc[tis->aborting_locty].state = TPM_TIS_STATE_READY; - tpm_tis_sts_set(&tis->loc[tis->aborting_locty], + if (s->aborting_locty == s->next_locty) { + s->loc[s->aborting_locty].state = TPM_TIS_STATE_READY; + tpm_tis_sts_set(&s->loc[s->aborting_locty], TPM_TIS_STS_COMMAND_READY); - tpm_tis_raise_irq(s, tis->aborting_locty, TPM_TIS_INT_COMMAND_READY); + tpm_tis_raise_irq(s, s->aborting_locty, TPM_TIS_INT_COMMAND_READY); } /* locality after abort is another one than the current one */ - tpm_tis_new_active_locality(s, tis->next_locty); + tpm_tis_new_active_locality(s, s->next_locty); - tis->next_locty = TPM_TIS_NO_LOCALITY; + s->next_locty = TPM_TIS_NO_LOCALITY; /* nobody's aborting a command anymore */ - tis->aborting_locty = TPM_TIS_NO_LOCALITY; + s->aborting_locty = TPM_TIS_NO_LOCALITY; } /* prepare aborting current command */ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty) { - TPMTISEmuState *tis = &s->s.tis; uint8_t busy_locty; - tis->aborting_locty = locty; - tis->next_locty = newlocty; /* locality after successful abort */ + s->aborting_locty = locty; + s->next_locty = newlocty; /* locality after successful abort */ /* * only abort a command using an interrupt if currently executing * a command AND if there's a valid connection to the vTPM. */ for (busy_locty = 0; busy_locty < TPM_TIS_NUM_LOCALITIES; busy_locty++) { - if (tis->loc[busy_locty].state == TPM_TIS_STATE_EXECUTION) { + if (s->loc[busy_locty].state == TPM_TIS_STATE_EXECUTION) { /* * request the backend to cancel. Some backends may not * support it @@ -427,16 +414,15 @@ static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty) static void tpm_tis_receive_bh(void *opaque) { TPMState *s = opaque; - TPMTISEmuState *tis = &s->s.tis; uint8_t locty = s->cmd.locty; - tpm_tis_sts_set(&tis->loc[locty], + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE); - tis->loc[locty].state = TPM_TIS_STATE_COMPLETION; - tis->loc[locty].r_offset = 0; - tis->loc[locty].w_offset = 0; + s->loc[locty].state = TPM_TIS_STATE_COMPLETION; + s->loc[locty].r_offset = 0; + s->loc[locty].w_offset = 0; - if (TPM_TIS_IS_VALID_LOCTY(tis->next_locty)) { + if (TPM_TIS_IS_VALID_LOCTY(s->next_locty)) { tpm_tis_abort(s, locty); } @@ -449,18 +435,17 @@ static void tpm_tis_receive_bh(void *opaque) */ static void tpm_tis_receive_cb(TPMState *s) { - TPMTISEmuState *tis = &s->s.tis; bool is_selftest_done = s->cmd.selftest_done; uint8_t locty = s->cmd.locty; uint8_t l; if (is_selftest_done) { for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) { - tis->loc[locty].sts |= TPM_TIS_STS_SELFTEST_DONE; + s->loc[locty].sts |= TPM_TIS_STS_SELFTEST_DONE; } } - qemu_bh_schedule(tis->bh); + qemu_bh_schedule(s->bh); } /* @@ -468,21 +453,20 @@ static void tpm_tis_receive_cb(TPMState *s) */ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty) { - TPMTISEmuState *tis = &s->s.tis; uint32_t ret = TPM_TIS_NO_DATA_BYTE; uint16_t len; - if ((tis->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) { - len = tpm_tis_get_size_from_buffer(&tis->loc[locty].r_buffer); + if ((s->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) { + len = tpm_tis_get_size_from_buffer(&s->loc[locty].r_buffer); - ret = tis->loc[locty].r_buffer.buffer[tis->loc[locty].r_offset++]; - if (tis->loc[locty].r_offset >= len) { + ret = s->loc[locty].r_buffer.buffer[s->loc[locty].r_offset++]; + if (s->loc[locty].r_offset >= len) { /* got last byte */ - tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_VALID); + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID); tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID); } DPRINTF("tpm_tis: tpm_tis_data_read byte 0x%02x [%d]\n", - ret, tis->loc[locty].r_offset-1); + ret, s->loc[locty].r_offset - 1); } return ret; @@ -505,13 +489,12 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr) uint8_t locty = tpm_tis_locality_from_addr(addr); hwaddr base = addr & ~0xfff; TPMState *s = opaque; - TPMTISEmuState *tis = &s->s.tis; DPRINTF("tpm_tis: active locality : %d\n" "tpm_tis: state of locality %d : %d\n" "tpm_tis: register dump:\n", - tis->active_locty, - locty, tis->loc[locty].state); + s->active_locty, + locty, s->loc[locty].state); for (idx = 0; regs[idx] != 0xfff; idx++) { DPRINTF("tpm_tis: 0x%04x : 0x%08x\n", regs[idx], @@ -520,25 +503,25 @@ static void tpm_tis_dump_state(void *opaque, hwaddr addr) DPRINTF("tpm_tis: read offset : %d\n" "tpm_tis: result buffer : ", - tis->loc[locty].r_offset); + s->loc[locty].r_offset); for (idx = 0; - idx < tpm_tis_get_size_from_buffer(&tis->loc[locty].r_buffer); + idx < tpm_tis_get_size_from_buffer(&s->loc[locty].r_buffer); idx++) { DPRINTF("%c%02x%s", - tis->loc[locty].r_offset == idx ? '>' : ' ', - tis->loc[locty].r_buffer.buffer[idx], + s->loc[locty].r_offset == idx ? '>' : ' ', + s->loc[locty].r_buffer.buffer[idx], ((idx & 0xf) == 0xf) ? "\ntpm_tis: " : ""); } DPRINTF("\n" "tpm_tis: write offset : %d\n" "tpm_tis: request buffer: ", - tis->loc[locty].w_offset); + s->loc[locty].w_offset); for (idx = 0; - idx < tpm_tis_get_size_from_buffer(&tis->loc[locty].w_buffer); + idx < tpm_tis_get_size_from_buffer(&s->loc[locty].w_buffer); idx++) { DPRINTF("%c%02x%s", - tis->loc[locty].w_offset == idx ? '>' : ' ', - tis->loc[locty].w_buffer.buffer[idx], + s->loc[locty].w_offset == idx ? '>' : ' ', + s->loc[locty].w_buffer.buffer[idx], ((idx & 0xf) == 0xf) ? "\ntpm_tis: " : ""); } DPRINTF("\n"); @@ -553,7 +536,6 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, unsigned size) { TPMState *s = opaque; - TPMTISEmuState *tis = &s->s.tis; uint16_t offset = addr & 0xffc; uint8_t shift = (addr & 0x3) * 8; uint32_t val = 0xffffffff; @@ -568,7 +550,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, switch (offset) { case TPM_TIS_REG_ACCESS: /* never show the SEIZE flag even though we use it internally */ - val = tis->loc[locty].access & ~TPM_TIS_ACCESS_SEIZE; + val = s->loc[locty].access & ~TPM_TIS_ACCESS_SEIZE; /* the pending flag is always calculated */ if (tpm_tis_check_request_use_except(s, locty)) { val |= TPM_TIS_ACCESS_PENDING_REQUEST; @@ -576,13 +558,13 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, val |= !tpm_backend_get_tpm_established_flag(s->be_driver); break; case TPM_TIS_REG_INT_ENABLE: - val = tis->loc[locty].inte; + val = s->loc[locty].inte; break; case TPM_TIS_REG_INT_VECTOR: - val = tis->irq_num; + val = s->irq_num; break; case TPM_TIS_REG_INT_STATUS: - val = tis->loc[locty].ints; + val = s->loc[locty].ints; break; case TPM_TIS_REG_INTF_CAPABILITY: switch (s->be_tpm_version) { @@ -598,14 +580,14 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, } break; case TPM_TIS_REG_STS: - if (tis->active_locty == locty) { - if ((tis->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) { + if (s->active_locty == locty) { + if ((s->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) { val = TPM_TIS_BURST_COUNT( - tpm_tis_get_size_from_buffer(&tis->loc[locty].r_buffer) - - tis->loc[locty].r_offset) | tis->loc[locty].sts; + tpm_tis_get_size_from_buffer(&s->loc[locty].r_buffer) + - s->loc[locty].r_offset) | s->loc[locty].sts; } else { - avail = tis->loc[locty].w_buffer.size - - tis->loc[locty].w_offset; + avail = s->loc[locty].w_buffer.size + - s->loc[locty].w_offset; /* * byte-sized reads should not return 0x00 for 0x100 * available bytes. @@ -613,13 +595,13 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, if (size == 1 && avail > 0xff) { avail = 0xff; } - val = TPM_TIS_BURST_COUNT(avail) | tis->loc[locty].sts; + val = TPM_TIS_BURST_COUNT(avail) | s->loc[locty].sts; } } break; case TPM_TIS_REG_DATA_FIFO: case TPM_TIS_REG_DATA_XFIFO ... TPM_TIS_REG_DATA_XFIFO_END: - if (tis->active_locty == locty) { + if (s->active_locty == locty) { if (size > 4 - (addr & 0x3)) { /* prevent access beyond FIFO */ size = 4 - (addr & 0x3); @@ -627,7 +609,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, val = 0; shift = 0; while (size > 0) { - switch (tis->loc[locty].state) { + switch (s->loc[locty].state) { case TPM_TIS_STATE_COMPLETION: v = tpm_tis_data_read(s, locty); break; @@ -643,7 +625,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, } break; case TPM_TIS_REG_INTERFACE_ID: - val = tis->loc[locty].iface_id; + val = s->loc[locty].iface_id; break; case TPM_TIS_REG_DID_VID: val = (TPM_TIS_TPM_DID << 16) | TPM_TIS_TPM_VID; @@ -675,7 +657,6 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, uint64_t val, unsigned size) { TPMState *s = opaque; - TPMTISEmuState *tis = &s->s.tis; uint16_t off = addr & 0xffc; uint8_t shift = (addr & 0x3) * 8; uint8_t locty = tpm_tis_locality_from_addr(addr); @@ -712,17 +693,17 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, TPM_TIS_ACCESS_ACTIVE_LOCALITY); } - active_locty = tis->active_locty; + active_locty = s->active_locty; if ((val & TPM_TIS_ACCESS_ACTIVE_LOCALITY)) { /* give up locality if currently owned */ - if (tis->active_locty == locty) { + if (s->active_locty == locty) { DPRINTF("tpm_tis: Releasing locality %d\n", locty); uint8_t newlocty = TPM_TIS_NO_LOCALITY; /* anybody wants the locality ? */ for (c = TPM_TIS_NUM_LOCALITIES - 1; c >= 0; c--) { - if ((tis->loc[c].access & TPM_TIS_ACCESS_REQUEST_USE)) { + if ((s->loc[c].access & TPM_TIS_ACCESS_REQUEST_USE)) { DPRINTF("tpm_tis: Locality %d requests use.\n", c); newlocty = c; break; @@ -740,12 +721,12 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, } } else { /* not currently the owner; clear a pending request */ - tis->loc[locty].access &= ~TPM_TIS_ACCESS_REQUEST_USE; + s->loc[locty].access &= ~TPM_TIS_ACCESS_REQUEST_USE; } } if ((val & TPM_TIS_ACCESS_BEEN_SEIZED)) { - tis->loc[locty].access &= ~TPM_TIS_ACCESS_BEEN_SEIZED; + s->loc[locty].access &= ~TPM_TIS_ACCESS_BEEN_SEIZED; } if ((val & TPM_TIS_ACCESS_SEIZE)) { @@ -756,19 +737,19 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, * allow seize for requesting locality if no locality is * active */ - while ((TPM_TIS_IS_VALID_LOCTY(tis->active_locty) && - locty > tis->active_locty) || - !TPM_TIS_IS_VALID_LOCTY(tis->active_locty)) { + while ((TPM_TIS_IS_VALID_LOCTY(s->active_locty) && + locty > s->active_locty) || + !TPM_TIS_IS_VALID_LOCTY(s->active_locty)) { bool higher_seize = FALSE; /* already a pending SEIZE ? */ - if ((tis->loc[locty].access & TPM_TIS_ACCESS_SEIZE)) { + if ((s->loc[locty].access & TPM_TIS_ACCESS_SEIZE)) { break; } /* check for ongoing seize by a higher locality */ for (l = locty + 1; l < TPM_TIS_NUM_LOCALITIES; l++) { - if ((tis->loc[l].access & TPM_TIS_ACCESS_SEIZE)) { + if ((s->loc[l].access & TPM_TIS_ACCESS_SEIZE)) { higher_seize = TRUE; break; } @@ -780,24 +761,24 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, /* cancel any seize by a lower locality */ for (l = 0; l < locty - 1; l++) { - tis->loc[l].access &= ~TPM_TIS_ACCESS_SEIZE; + s->loc[l].access &= ~TPM_TIS_ACCESS_SEIZE; } - tis->loc[locty].access |= TPM_TIS_ACCESS_SEIZE; + s->loc[locty].access |= TPM_TIS_ACCESS_SEIZE; DPRINTF("tpm_tis: TPM_TIS_ACCESS_SEIZE: " "Locality %d seized from locality %d\n", - locty, tis->active_locty); + locty, s->active_locty); DPRINTF("tpm_tis: TPM_TIS_ACCESS_SEIZE: Initiating abort.\n"); set_new_locty = 0; - tpm_tis_prep_abort(s, tis->active_locty, locty); + tpm_tis_prep_abort(s, s->active_locty, locty); break; } } if ((val & TPM_TIS_ACCESS_REQUEST_USE)) { - if (tis->active_locty != locty) { - if (TPM_TIS_IS_VALID_LOCTY(tis->active_locty)) { - tis->loc[locty].access |= TPM_TIS_ACCESS_REQUEST_USE; + if (s->active_locty != locty) { + if (TPM_TIS_IS_VALID_LOCTY(s->active_locty)) { + s->loc[locty].access |= TPM_TIS_ACCESS_REQUEST_USE; } else { /* no locality active -> make this one active now */ active_locty = locty; @@ -811,12 +792,12 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, break; case TPM_TIS_REG_INT_ENABLE: - if (tis->active_locty != locty) { + if (s->active_locty != locty) { break; } - tis->loc[locty].inte &= mask; - tis->loc[locty].inte |= (val & (TPM_TIS_INT_ENABLED | + s->loc[locty].inte &= mask; + s->loc[locty].inte |= (val & (TPM_TIS_INT_ENABLED | TPM_TIS_INT_POLARITY_MASK | TPM_TIS_INTERRUPTS_SUPPORTED)); break; @@ -824,30 +805,30 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, /* hard wired -- ignore */ break; case TPM_TIS_REG_INT_STATUS: - if (tis->active_locty != locty) { + if (s->active_locty != locty) { break; } /* clearing of interrupt flags */ if (((val & TPM_TIS_INTERRUPTS_SUPPORTED)) && - (tis->loc[locty].ints & TPM_TIS_INTERRUPTS_SUPPORTED)) { - tis->loc[locty].ints &= ~val; - if (tis->loc[locty].ints == 0) { - qemu_irq_lower(tis->irq); + (s->loc[locty].ints & TPM_TIS_INTERRUPTS_SUPPORTED)) { + s->loc[locty].ints &= ~val; + if (s->loc[locty].ints == 0) { + qemu_irq_lower(s->irq); DPRINTF("tpm_tis: Lowering IRQ\n"); } } - tis->loc[locty].ints &= ~(val & TPM_TIS_INTERRUPTS_SUPPORTED); + s->loc[locty].ints &= ~(val & TPM_TIS_INTERRUPTS_SUPPORTED); break; case TPM_TIS_REG_STS: - if (tis->active_locty != locty) { + if (s->active_locty != locty) { break; } if (s->be_tpm_version == TPM_VERSION_2_0) { /* some flags that are only supported for TPM 2 */ if (val & TPM_TIS_STS_COMMAND_CANCEL) { - if (tis->loc[locty].state == TPM_TIS_STATE_EXECUTION) { + if (s->loc[locty].state == TPM_TIS_STATE_EXECUTION) { /* * request the backend to cancel. Some backends may not * support it @@ -867,16 +848,16 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, TPM_TIS_STS_RESPONSE_RETRY); if (val == TPM_TIS_STS_COMMAND_READY) { - switch (tis->loc[locty].state) { + switch (s->loc[locty].state) { case TPM_TIS_STATE_READY: - tis->loc[locty].w_offset = 0; - tis->loc[locty].r_offset = 0; + s->loc[locty].w_offset = 0; + s->loc[locty].r_offset = 0; break; case TPM_TIS_STATE_IDLE: - tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_COMMAND_READY); - tis->loc[locty].state = TPM_TIS_STATE_READY; + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_COMMAND_READY); + s->loc[locty].state = TPM_TIS_STATE_READY; tpm_tis_raise_irq(s, locty, TPM_TIS_INT_COMMAND_READY); break; @@ -889,23 +870,23 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, break; case TPM_TIS_STATE_COMPLETION: - tis->loc[locty].w_offset = 0; - tis->loc[locty].r_offset = 0; + s->loc[locty].w_offset = 0; + s->loc[locty].r_offset = 0; /* shortcut to ready state with C/R set */ - tis->loc[locty].state = TPM_TIS_STATE_READY; - if (!(tis->loc[locty].sts & TPM_TIS_STS_COMMAND_READY)) { - tpm_tis_sts_set(&tis->loc[locty], + s->loc[locty].state = TPM_TIS_STATE_READY; + if (!(s->loc[locty].sts & TPM_TIS_STS_COMMAND_READY)) { + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_COMMAND_READY); tpm_tis_raise_irq(s, locty, TPM_TIS_INT_COMMAND_READY); } - tis->loc[locty].sts &= ~(TPM_TIS_STS_DATA_AVAILABLE); + s->loc[locty].sts &= ~(TPM_TIS_STS_DATA_AVAILABLE); break; } } else if (val == TPM_TIS_STS_TPM_GO) { - switch (tis->loc[locty].state) { + switch (s->loc[locty].state) { case TPM_TIS_STATE_RECEPTION: - if ((tis->loc[locty].sts & TPM_TIS_STS_EXPECT) == 0) { + if ((s->loc[locty].sts & TPM_TIS_STS_EXPECT) == 0) { tpm_tis_tpm_send(s, locty); } break; @@ -914,10 +895,10 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, break; } } else if (val == TPM_TIS_STS_RESPONSE_RETRY) { - switch (tis->loc[locty].state) { + switch (s->loc[locty].state) { case TPM_TIS_STATE_COMPLETION: - tis->loc[locty].r_offset = 0; - tpm_tis_sts_set(&tis->loc[locty], + s->loc[locty].r_offset = 0; + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID| TPM_TIS_STS_DATA_AVAILABLE); break; @@ -930,20 +911,20 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, case TPM_TIS_REG_DATA_FIFO: case TPM_TIS_REG_DATA_XFIFO ... TPM_TIS_REG_DATA_XFIFO_END: /* data fifo */ - if (tis->active_locty != locty) { + if (s->active_locty != locty) { break; } - if (tis->loc[locty].state == TPM_TIS_STATE_IDLE || - tis->loc[locty].state == TPM_TIS_STATE_EXECUTION || - tis->loc[locty].state == TPM_TIS_STATE_COMPLETION) { + if (s->loc[locty].state == TPM_TIS_STATE_IDLE || + s->loc[locty].state == TPM_TIS_STATE_EXECUTION || + s->loc[locty].state == TPM_TIS_STATE_COMPLETION) { /* drop the byte */ } else { DPRINTF("tpm_tis: Data to send to TPM: %08x (size=%d)\n", (int)val, size); - if (tis->loc[locty].state == TPM_TIS_STATE_READY) { - tis->loc[locty].state = TPM_TIS_STATE_RECEPTION; - tpm_tis_sts_set(&tis->loc[locty], + if (s->loc[locty].state == TPM_TIS_STATE_READY) { + s->loc[locty].state = TPM_TIS_STATE_RECEPTION; + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID); } @@ -953,30 +934,30 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, size = 4 - (addr & 0x3); } - while ((tis->loc[locty].sts & TPM_TIS_STS_EXPECT) && size > 0) { - if (tis->loc[locty].w_offset < tis->loc[locty].w_buffer.size) { - tis->loc[locty].w_buffer. - buffer[tis->loc[locty].w_offset++] = (uint8_t)val; + while ((s->loc[locty].sts & TPM_TIS_STS_EXPECT) && size > 0) { + if (s->loc[locty].w_offset < s->loc[locty].w_buffer.size) { + s->loc[locty].w_buffer. + buffer[s->loc[locty].w_offset++] = (uint8_t)val; val >>= 8; size--; } else { - tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_VALID); + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID); } } /* check for complete packet */ - if (tis->loc[locty].w_offset > 5 && - (tis->loc[locty].sts & TPM_TIS_STS_EXPECT)) { + if (s->loc[locty].w_offset > 5 && + (s->loc[locty].sts & TPM_TIS_STS_EXPECT)) { /* we have a packet length - see if we have all of it */ - bool need_irq = !(tis->loc[locty].sts & TPM_TIS_STS_VALID); + bool need_irq = !(s->loc[locty].sts & TPM_TIS_STS_VALID); - len = tpm_tis_get_size_from_buffer(&tis->loc[locty].w_buffer); - if (len > tis->loc[locty].w_offset) { - tpm_tis_sts_set(&tis->loc[locty], + len = tpm_tis_get_size_from_buffer(&s->loc[locty].w_buffer); + if (len > s->loc[locty].w_offset) { + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID); } else { /* packet complete */ - tpm_tis_sts_set(&tis->loc[locty], TPM_TIS_STS_VALID); + tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID); } if (need_irq) { tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID); @@ -987,7 +968,7 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr, case TPM_TIS_REG_INTERFACE_ID: if (val & TPM_TIS_IFACE_ID_INT_SEL_LOCK) { for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) { - tis->loc[l].iface_id |= TPM_TIS_IFACE_ID_INT_SEL_LOCK; + s->loc[l].iface_id |= TPM_TIS_IFACE_ID_INT_SEL_LOCK; } } break; @@ -1036,39 +1017,38 @@ TPMVersion tpm_tis_get_tpm_version(Object *obj) static void tpm_tis_reset(DeviceState *dev) { TPMState *s = TPM(dev); - TPMTISEmuState *tis = &s->s.tis; int c; s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver); tpm_backend_reset(s->be_driver); - tis->active_locty = TPM_TIS_NO_LOCALITY; - tis->next_locty = TPM_TIS_NO_LOCALITY; - tis->aborting_locty = TPM_TIS_NO_LOCALITY; + s->active_locty = TPM_TIS_NO_LOCALITY; + s->next_locty = TPM_TIS_NO_LOCALITY; + s->aborting_locty = TPM_TIS_NO_LOCALITY; for (c = 0; c < TPM_TIS_NUM_LOCALITIES; c++) { - tis->loc[c].access = TPM_TIS_ACCESS_TPM_REG_VALID_STS; + s->loc[c].access = TPM_TIS_ACCESS_TPM_REG_VALID_STS; switch (s->be_tpm_version) { case TPM_VERSION_UNSPEC: break; case TPM_VERSION_1_2: - tis->loc[c].sts = TPM_TIS_STS_TPM_FAMILY1_2; - tis->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3; + s->loc[c].sts = TPM_TIS_STS_TPM_FAMILY1_2; + s->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3; break; case TPM_VERSION_2_0: - tis->loc[c].sts = TPM_TIS_STS_TPM_FAMILY2_0; - tis->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0; + s->loc[c].sts = TPM_TIS_STS_TPM_FAMILY2_0; + s->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0; break; } - tis->loc[c].inte = TPM_TIS_INT_POLARITY_LOW_LEVEL; - tis->loc[c].ints = 0; - tis->loc[c].state = TPM_TIS_STATE_IDLE; - - tis->loc[c].w_offset = 0; - tpm_tis_realloc_buffer(&tis->loc[c].w_buffer); - tis->loc[c].r_offset = 0; - tpm_tis_realloc_buffer(&tis->loc[c].r_buffer); + s->loc[c].inte = TPM_TIS_INT_POLARITY_LOW_LEVEL; + s->loc[c].ints = 0; + s->loc[c].state = TPM_TIS_STATE_IDLE; + + s->loc[c].w_offset = 0; + tpm_tis_realloc_buffer(&s->loc[c].w_buffer); + s->loc[c].r_offset = 0; + tpm_tis_realloc_buffer(&s->loc[c].r_buffer); } tpm_tis_do_startup_tpm(s); @@ -1080,8 +1060,7 @@ static const VMStateDescription vmstate_tpm_tis = { }; static Property tpm_tis_properties[] = { - DEFINE_PROP_UINT32("irq", TPMState, - s.tis.irq_num, TPM_TIS_IRQ), + DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ), DEFINE_PROP_STRING("tpmdev", TPMState, backend), DEFINE_PROP_END_OF_LIST(), }; @@ -1089,7 +1068,6 @@ static Property tpm_tis_properties[] = { static void tpm_tis_realizefn(DeviceState *dev, Error **errp) { TPMState *s = TPM(dev); - TPMTISEmuState *tis = &s->s.tis; s->be_driver = qemu_find_tpm(s->backend); if (!s->be_driver) { @@ -1106,15 +1084,15 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp) return; } - if (tis->irq_num > 15) { + if (s->irq_num > 15) { error_setg(errp, "tpm_tis: IRQ %d for TPM TIS is outside valid range " - "of 0 to 15", tis->irq_num); + "of 0 to 15", s->irq_num); return; } - tis->bh = qemu_bh_new(tpm_tis_receive_bh, s); + s->bh = qemu_bh_new(tpm_tis_receive_bh, s); - isa_init_irq(&s->busdev, &tis->irq, tis->irq_num); + isa_init_irq(&s->busdev, &s->irq, s->irq_num); memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)), TPM_TIS_ADDR_BASE, &s->mmio); From 698f5daa4a81984490612a6143b46f5da7392510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:56:01 +0200 Subject: [PATCH 20/21] tpm: add a QOM TPM interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will simplify backend / interface objects relationship, so the frontend interface will simply have to implement the TPM QOM interface. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 8 ++++++++ hw/tpm/tpm_int.h | 17 +++++++++++++++++ hw/tpm/tpm_tis.c | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/backends/tpm.c b/backends/tpm.c index dc7c831ff8a6..87c5c0917974 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -17,6 +17,7 @@ #include "qapi/error.h" #include "qapi/qmp/qerror.h" #include "sysemu/tpm.h" +#include "hw/tpm/tpm_int.h" #include "qemu/thread.h" static void tpm_backend_worker_thread(gpointer data, gpointer user_data) @@ -209,9 +210,16 @@ static const TypeInfo tpm_backend_info = { .abstract = true, }; +static const TypeInfo tpm_if_info = { + .name = TYPE_TPM_IF, + .parent = TYPE_INTERFACE, + .class_size = sizeof(TPMIfClass), +}; + static void register_types(void) { type_register_static(&tpm_backend_info); + type_register_static(&tpm_if_info); } type_init(register_types); diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index e231d0eb4f0b..eb02e7760c3d 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -13,6 +13,23 @@ #define TPM_TPM_INT_H #include "qemu/osdep.h" +#include "qom/object.h" + +#define TYPE_TPM_IF "tpm-if" +#define TPM_IF_CLASS(klass) \ + OBJECT_CLASS_CHECK(TPMIfClass, (klass), TYPE_TPM_IF) +#define TPM_IF_GET_CLASS(obj) \ + OBJECT_GET_CLASS(TPMIfClass, (obj), TYPE_TPM_IF) +#define TPM_IF(obj) \ + INTERFACE_CHECK(TPMIf, (obj), TYPE_TPM_IF) + +typedef struct TPMIf { + Object parent_obj; +} TPMIf; + +typedef struct TPMIfClass { + InterfaceClass parent_class; +} TPMIfClass; #define TPM_STANDARD_CMDLINE_OPTS \ { \ diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index d84eec48b4af..dbb50043accc 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -1123,6 +1123,10 @@ static const TypeInfo tpm_tis_info = { .instance_size = sizeof(TPMState), .instance_init = tpm_tis_initfn, .class_init = tpm_tis_class_init, + .interfaces = (InterfaceInfo[]) { + { TYPE_TPM_IF }, + { } + } }; static void tpm_tis_register(void) From 05a699985c001927a50f8f276251f2af1e0c5b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 10 Oct 2017 00:56:02 +0200 Subject: [PATCH 21/21] tpm: move recv_data_callback to TPM interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify the TPM backend setup, move callback to TPM interface. Signed-off-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 4 +--- hw/tpm/tpm_emulator.c | 3 ++- hw/tpm/tpm_int.h | 3 +++ hw/tpm/tpm_passthrough.c | 3 ++- hw/tpm/tpm_tis.c | 11 ++++++----- include/sysemu/tpm_backend.h | 6 +----- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/backends/tpm.c b/backends/tpm.c index 87c5c0917974..5763f6f369fb 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -44,11 +44,9 @@ enum TpmType tpm_backend_get_type(TPMBackend *s) return k->type; } -int tpm_backend_init(TPMBackend *s, TPMState *state, - TPMRecvDataCB *datacb) +int tpm_backend_init(TPMBackend *s, TPMState *state) { s->tpm_state = state; - s->recv_data_callback = datacb; s->had_startup_error = false; return 0; diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 6500b86b41e7..9aaec8e3efb7 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -176,6 +176,7 @@ static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number, static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) { TPMEmulator *tpm_emu = TPM_EMULATOR(tb); + TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpm_state); Error *err = NULL; DPRINTF("processing TPM command"); @@ -190,7 +191,7 @@ static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) goto error; } - tb->recv_data_callback(tb->tpm_state); + tic->request_completed(TPM_IF(tb->tpm_state)); return; error: diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index eb02e7760c3d..9c045b6691a4 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -29,6 +29,9 @@ typedef struct TPMIf { typedef struct TPMIfClass { InterfaceClass parent_class; + + /* run in thread pool by backend */ + void (*request_completed)(TPMIf *obj); } TPMIfClass; #define TPM_STANDARD_CMDLINE_OPTS \ diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index 4274164a61df..c440aff4b205 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -139,13 +139,14 @@ static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd) { TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); + TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpm_state); DPRINTF("tpm_passthrough: processing command %p\n", cmd); tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len, cmd->out, cmd->out_len, &cmd->selftest_done); - tb->recv_data_callback(tb->tpm_state); + tic->request_completed(TPM_IF(tb->tpm_state)); } static void tpm_passthrough_reset(TPMBackend *tb) diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c index dbb50043accc..8c5cac5fa5ec 100644 --- a/hw/tpm/tpm_tis.c +++ b/hw/tpm/tpm_tis.c @@ -430,11 +430,10 @@ static void tpm_tis_receive_bh(void *opaque) TPM_TIS_INT_DATA_AVAILABLE | TPM_TIS_INT_STS_VALID); } -/* - * Callback from the TPM to indicate that the response was received. - */ -static void tpm_tis_receive_cb(TPMState *s) +static void tpm_tis_request_completed(TPMIf *ti) { + TPMState *s = TPM(ti); + bool is_selftest_done = s->cmd.selftest_done; uint8_t locty = s->cmd.locty; uint8_t l; @@ -1078,7 +1077,7 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp) s->be_driver->fe_model = TPM_MODEL_TPM_TIS; - if (tpm_backend_init(s->be_driver, s, tpm_tis_receive_cb)) { + if (tpm_backend_init(s->be_driver, s)) { error_setg(errp, "tpm_tis: backend driver with id %s could not be " "initialized", s->backend); return; @@ -1110,11 +1109,13 @@ static void tpm_tis_initfn(Object *obj) static void tpm_tis_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); + TPMIfClass *tc = TPM_IF_CLASS(klass); dc->realize = tpm_tis_realizefn; dc->props = tpm_tis_properties; dc->reset = tpm_tis_reset; dc->vmsd = &vmstate_tpm_tis; + tc->request_completed = tpm_tis_request_completed; } static const TypeInfo tpm_tis_info = { diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 3bb90be3def0..03ea5a3400aa 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -30,8 +30,6 @@ typedef struct TPMBackendClass TPMBackendClass; typedef struct TPMBackend TPMBackend; -typedef void (TPMRecvDataCB)(TPMState *); - typedef struct TPMBackendCmd { uint8_t locty; const uint8_t *in; @@ -48,7 +46,6 @@ struct TPMBackend { bool opened; TPMState *tpm_state; GThreadPool *thread_pool; - TPMRecvDataCB *recv_data_callback; bool had_startup_error; /* */ @@ -106,8 +103,7 @@ enum TpmType tpm_backend_get_type(TPMBackend *s); * * Returns 0 on success. */ -int tpm_backend_init(TPMBackend *s, TPMState *state, - TPMRecvDataCB *datacb); +int tpm_backend_init(TPMBackend *s, TPMState *state); /** * tpm_backend_startup_tpm: