Skip to contentSkip to conversation

tpm: Extend TPM with state migration support

RFC PATCH Superseded Stefan Berger 3 participants 7 messages Oct 11, 2017 – Oct 12, 2017

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#1 lore raw

RFC PATCH 1/4 tpm: print buffers received from TPM when debugging

In reply to Stefan Berger
---
 hw/tpm/tpm_tis.c | 2 ++
 1 file changed, 2 insertions(+)
hw/tpm/tpm_tis.c +2 -0
index d5118e7..9bf0bce 100644
@@ -373,6 +373,8 @@ static void tpm_tis_receive_bh(void *opaque)
     tis->loc[locty].r_offset = 0;
     tis->loc[locty].w_offset = 0;
 
+    tpm_tis_show_buffer(&tis->loc[locty].r_buffer, "tpm_tis: From TPM");
+
     if (TPM_TIS_IS_VALID_LOCTY(tis->next_locty)) {
         tpm_tis_abort(s, locty);
     }
Signature · 1 line
2.5.5
#3 lore raw

RFC PATCH 2/4 tpm: Introduce condition to notify waiters of completed command

In reply to Stefan Berger

Introduce a lock and a condition to notify anyone waiting for the completion of the execution of a TPM command by the backend (thread). The backend uses the condition to signal anyone waiting for command completion. We need to place the condition in two locations: one is invoked by the backend thread, the other by the bottom half thread.
We will use the signalling to wait for command completion before VM suspend.

---
 hw/tpm/tpm_int.h |  3 +++
 hw/tpm/tpm_tis.c | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
hw/tpm/tpm_int.h +3 -0
index f2f285b..b766b3d 100644
@@ -30,6 +30,9 @@ struct TPMState {
     char *backend;
     TPMBackend *be_driver;
     TPMVersion be_tpm_version;
+
+    QemuMutex state_lock;
+    QemuCond cmd_complete;
 };
 
 #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
hw/tpm/tpm_tis.c +14 -0
index 9bf0bce..b9b9a2f 100644
@@ -367,6 +367,8 @@ static void tpm_tis_receive_bh(void *opaque)
     TPMTISEmuState *tis = &s->s.tis;
     uint8_t locty = s->locty_number;
 
+    qemu_mutex_lock(&s->state_lock);
+
     tpm_tis_sts_set(&tis->loc[locty],
                     TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE);
     tis->loc[locty].state = TPM_TIS_STATE_COMPLETION;
@@ -385,6 +387,10 @@ static void tpm_tis_receive_bh(void *opaque)
     tpm_tis_raise_irq(s, locty,
                       TPM_TIS_INT_DATA_AVAILABLE | TPM_TIS_INT_STS_VALID);
 #endif
+
+    /* notify of completed command */
+    qemu_cond_signal(&s->cmd_complete);
+    qemu_mutex_unlock(&s->state_lock);
 }
 
 /*
@@ -404,6 +410,11 @@ static void tpm_tis_receive_cb(TPMState *s, uint8_t locty,
         }
     }
 
+    qemu_mutex_lock(&s->state_lock);
+    /* notify of completed command */
+    qemu_cond_signal(&s->cmd_complete);
+    qemu_mutex_unlock(&s->state_lock);
+
     qemu_bh_schedule(tis->bh);
 }
 
@@ -1083,6 +1094,9 @@ static void tpm_tis_initfn(Object *obj)
     memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
                           s, "tpm-tis-mmio",
                           TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+
+    qemu_mutex_init(&s->state_lock);
+    qemu_cond_init(&s->cmd_complete);
 }
 
 static void tpm_tis_class_init(ObjectClass *klass, void *data)
Signature · 1 line
2.5.5
#4 lore raw

RFC PATCH 3/4 tpm: Introduce condition in TPM backend for notification

In reply to Stefan Berger

TPM backends will suspend independently of the frontends. Also here we need to be able to wait for the TPM command to have been completely processed.

---
 backends/tpm.c               | 16 ++++++++++++++++
 hw/tpm/tpm_emulator.c        |  3 +++
 include/sysemu/tpm_backend.h | 14 ++++++++++++++
 3 files changed, 33 insertions(+)
backends/tpm.c +16 -0
index 37c84b7..334836b 100644
@@ -52,10 +52,22 @@ int tpm_backend_init(TPMBackend *s, TPMState *state,
     s->tpm_state = state;
     s->recv_data_callback = datacb;
     s->had_startup_error = false;
+    s->tpm_busy = false;
+
+    qemu_mutex_init(&s->state_lock);
+    qemu_cond_init(&s->cmd_complete);
 
     return k->ops->init ? k->ops->init(s) : 0;
 }
 
+void tpm_backend_cmd_completed(TPMBackend *s)
+{
+    qemu_mutex_lock(&s->state_lock);
+    s->tpm_busy = false;
+    qemu_cond_signal(&s->cmd_complete);
+    qemu_mutex_unlock(&s->state_lock);
+}
+
 int tpm_backend_startup_tpm(TPMBackend *s)
 {
     int res = 0;
@@ -82,6 +94,10 @@ bool tpm_backend_had_startup_error(TPMBackend *s)
 
 void tpm_backend_deliver_request(TPMBackend *s)
 {
+    qemu_mutex_lock(&s->state_lock);
+    s->tpm_busy = true;
+    qemu_mutex_unlock(&s->state_lock);
+
     g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
                        NULL);
 }
hw/tpm/tpm_emulator.c +3 -0
index 95e1e04..556b8c8 100644
@@ -200,6 +200,9 @@ static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd cmd)
         tb->recv_data_callback(tb->tpm_state, tb->tpm_state->locty_number,
                                selftest_done);
 
+        /* result delivered */
+        tpm_backend_cmd_completed(tb);
+
         break;
     case TPM_BACKEND_CMD_INIT:
     case TPM_BACKEND_CMD_END:
include/sysemu/tpm_backend.h +14 -0
index 2c798a1..cdc4742 100644
@@ -18,6 +18,7 @@
 #include "qapi-types.h"
 #include "qemu/option.h"
 #include "sysemu/tpm.h"
+#include "qemu/thread.h"
 
 #define TYPE_TPM_BACKEND "tpm-backend"
 #define TPM_BACKEND(obj) \
@@ -54,6 +55,10 @@ struct TPMBackend {
     enum TpmModel fe_model;
 
     QLIST_ENTRY(TPMBackend) list;
+
+    QemuMutex state_lock;
+    QemuCond cmd_complete; /* signaled once tpm_busy is false */
+    bool tpm_busy;
 };
 
 struct TPMBackendClass {
@@ -213,6 +218,15 @@ TPMVersion tpm_backend_get_tpm_version(TPMBackend *s);
  */
 TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
 
+/**
+ * tpm_backend_cmd_completed:
+ * @s: the backend
+ *
+ * Mark the backend as not busy and notify anyone interested
+ * in the state changed
+ */
+void tpm_backend_cmd_completed(TPMBackend *s);
+
 TPMBackend *qemu_find_tpm(const char *id);
 
 const TPMDriverOps *tpm_get_backend_driver(const char *type);
Signature · 1 line
2.5.5
#5 lore raw

RFC PATCH 4/4 tpm: extend TPM emulator and TIS with state migration support

In reply to Stefan Berger

Extend the TPM emulator and TPM TIS interface with state migration support.

The external TPM emulator 'swtpm' provides a protocol over its control channel to retrieve its state blobs. We implement functions for getting and setting the different state blobs.

Since we have an external TPM emulator, we need to make sure that we do not migrate the state for as long as it is busy processing a request. We need to wait for notification that the request has completed processing.

---
 backends/tpm.c               |   9 ++
 hw/tpm/tpm_emulator.c        | 310 +++++++++++++++++++++++++++++++++++++++++--
 hw/tpm/tpm_tis.c             | 138 ++++++++++++++++++-
 hw/tpm/tpm_tis.h             |   2 +
 hw/tpm/tpm_util.c            |   7 +
 hw/tpm/tpm_util.h            |   2 +
 include/sysemu/tpm_backend.h |   8 ++
 7 files changed, 465 insertions(+), 11 deletions(-)
backends/tpm.c +9 -0
index 334836b..be35ee2 100644
@@ -68,6 +68,15 @@ void tpm_backend_cmd_completed(TPMBackend *s)
     qemu_mutex_unlock(&s->state_lock);
 }
 
+void tpm_backend_wait_cmd_completed(TPMBackend *s)
+{
+    qemu_mutex_lock(&s->state_lock);
+    if (s->tpm_busy) {
+        qemu_cond_wait(&s->cmd_complete, &s->state_lock);
+    }
+    qemu_mutex_unlock(&s->state_lock);
+}
+
 int tpm_backend_startup_tpm(TPMBackend *s)
 {
     int res = 0;
hw/tpm/tpm_emulator.c +300 -10

366 lines not on this page · raw message

hw/tpm/tpm_tis.c +137 -1

158 lines not on this page · raw message

hw/tpm/tpm_tis.h +2 -0
index a1df41f..b7fc0ea 100644
@@ -54,6 +54,8 @@ typedef struct TPMLocality {
 
 typedef struct TPMTISEmuState {
     QEMUBH *bh;
+    bool bh_scheduled; /* bh scheduled but did not run yet */
+
     uint32_t offset;
     uint8_t buf[TPM_TIS_BUFFER_MAX];
 
hw/tpm/tpm_util.c +7 -0
index 1129155..f496e39 100644
@@ -150,3 +150,10 @@ int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
 
     return 1;
 }
+
+void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)
+{
+    g_free(tsb->buffer);
+    tsb->buffer = NULL;
+    tsb->size = 0;
+}
hw/tpm/tpm_util.h +2 -0
index 2f7c961..d394cb6 100644
@@ -30,4 +30,6 @@ bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len);
 
 int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version);
 
+void tpm_sized_buffer_reset(TPMSizedBuffer *tsb);
+
 #endif /* TPM_TPM_UTIL_H */
include/sysemu/tpm_backend.h +8 -0
index cdc4742..250d763 100644
@@ -227,6 +227,14 @@ TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
  */
 void tpm_backend_cmd_completed(TPMBackend *s);
 
+/**
+ * tpm_backend_wait_cmd_completed:
+ * @s: the backend
+ *
+ * Wait the backend to not be busy anymore
+ */
+void tpm_backend_wait_cmd_completed(TPMBackend *s);
+
 TPMBackend *qemu_find_tpm(const char *id);
 
 const TPMDriverOps *tpm_get_backend_driver(const char *type);
Signature · 1 line
2.5.5
#0 lore raw

cover tpm: Extend TPM with state migration support

This set of patches implements support for migrating the state of the external 'swtpm' TPM emulator. I have primarily tested this with TPM 1.2 so far.

This series of patches builds on top of the recent pull request

Stefan

Stefan Berger (4):
tpm: print buffers received from TPM when debugging
tpm: Introduce condition to notify waiters of completed command
tpm: Introduce condition in TPM backend for notification
tpm: extend TPM emulator and TIS with state migration support

 backends/tpm.c               |  25 ++++
 hw/tpm/tpm_emulator.c        | 313 +++++++++++++++++++++++++++++++++++++++++--
 hw/tpm/tpm_int.h             |   3 +
 hw/tpm/tpm_tis.c             | 154 ++++++++++++++++++++-
 hw/tpm/tpm_tis.h             |   2 +
 hw/tpm/tpm_util.c            |   7 +
 hw/tpm/tpm_util.h            |   2 +
 include/sysemu/tpm_backend.h |  22 +++
 8 files changed, 517 insertions(+), 11 deletions(-)
Signature · 1 line
2.5.5
#6 lore raw
Quoted 5 lines · Stefan Berger

This set of patches implements support for migrating the state of the external 'swtpm' TPM emulator. I have primarily tested this with TPM 1.2 so far.

This series of patches builds on top of the recent pull request

Just to make sure patchew knows what you mean:

Based-on:
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg02050.html
[PULL v2 00/10] Merge tpm 2017/10/04

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-devel] [RFC PATCH 1/4] tpm: print buffers received from TPM when debugging

In reply to Stefan Berger
Quoted 1 line · Stefan Berger
Quoted 20 lines
---
 hw/tpm/tpm_tis.c | 2 ++
 1 file changed, 2 insertions(+)
hw/tpm/tpm_tis.c +2 -1
index d5118e7..9bf0bce 100644
@@ -373,6 +373,8 @@ static void tpm_tis_receive_bh(void *opaque)
     tis->loc[locty].r_offset = 0;
     tis->loc[locty].w_offset = 0;
 
+    tpm_tis_show_buffer(&tis->loc[locty].r_buffer, "tpm_tis: From TPM");
+
     if (TPM_TIS_IS_VALID_LOCTY(tis->next_locty)) {
         tpm_tis_abort(s, locty);
     }
--

2.5.5

Signature · 1 line
Marc-André Lureau