Skip to contentSkip to conversation

Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

PATCH In review Quan Xu 3 participants 21 messages Nov 2, 2014 – Nov 6, 2014

lore
Share & tools
lore

Quan Xu

author
#0 lore raw

PATCH 2/4 Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

---
 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +1 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o xen_devconfig.o
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_msi.o
hw/xen/xen_backend.c +180 -2

229 lines not on this page · raw message

hw/xen/xen_stubdom_vtpm.c new file +333 -0

333 lines not on this page · raw message

include/hw/xen/xen_backend.h +11 -0
index 3b4125e..45fd6d3 100644
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console quirk) */
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t count);
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t *count);
     void      (*backend_changed)(struct XenDevice *xendev, const char *node);
     void      (*frontend_changed)(struct XenDevice *xendev, const char *node);
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice *xendev);
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int remote_dom);
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c     */
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c */
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c        */
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c         */
+extern struct XenDevOps xen_vtpmdev_ops;      /* xen_stubdom_vtpm.c*/
 
 void xen_init_display(int domid);
 
include/hw/xen/xen_common.h +6 -0
index 95612a4..fb43084 100644
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +13 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
         fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif
+
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
Signature · 1 line
1.8.3.2
#1 lore raw
Quoted 5 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Where does the vTPM backend lives?

Quoted 184 lines
 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +1 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o xen_devconfig.o
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_msi.o
hw/xen/xen_backend.c +119 -2
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom, xendev->dev);
+        snprintf(xendev->be, sizeof(xendev->be), "%s/backend/%s/%d/%d",
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%" PRIxPTR,
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom, (void *)ops);
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]), ops);
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is NULL.\n");
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",

hw/xen/xen_backend.c err: · line 861

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch failed.\n");
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

Quoted 51 lines
hw/xen/xen_backend.c +39 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int remote_dom)
+{
+    xendev->local_port = xc_evtchn_bind_unbound_port(xendev->evtchndev,
+                                                     remote_dom);
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice *xendev)
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 332 lines
hw/xen/xen_backend.c +0 -0
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +323 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel complete */
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be zero */
+    uint32_t extra_pages[0]; /* grant IDs; length is actually nr_extra_pages */
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum xenbus_state xbus)
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t *count)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t count)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev, "frontend");
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs, vtpmdev->bedomid, 1,
+                                         &ring_ref, PROT_READ|PROT_WRITE);
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0, vtpmdev->bedomid)) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char *node)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev, XenbusStateClosing);
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler, vtpmdev);
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Quoted 76 lines
include/hw/xen/xen_backend.h +11 -0
index 3b4125e..45fd6d3 100644
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console quirk) */
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t count);
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t *count);
     void      (*backend_changed)(struct XenDevice *xendev, const char *node);
     void      (*frontend_changed)(struct XenDevice *xendev, const char *node);
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice *xendev);
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int remote_dom);
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c     */
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c */
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c        */
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c         */
+extern struct XenDevOps xen_vtpmdev_ops;      /* xen_stubdom_vtpm.c*/
 
 void xen_init_display(int domid);
 
include/hw/xen/xen_common.h +6 -0
index 95612a4..fb43084 100644
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +5 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 10 lines
xen-hvm.c +7 -0
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
         fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Quoted 6 lines
xen-hvm.c +0 -0
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
Signature · 1 line
1.8.3.2
#18 lore raw
In reply to Quan Xu
Quoted 339 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

---
 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +1 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o xen_devconfig.o
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_msi.o
hw/xen/xen_backend.c +180 -2
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom, xendev->dev);
+        snprintf(xendev->be, sizeof(xendev->be), "%s/backend/%s/%d/%d",
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%" PRIxPTR,
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom, (void *)ops);
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]), ops);
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is NULL.\n");
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch failed.\n");
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}
+
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int remote_dom)
+{
+    xendev->local_port = xc_evtchn_bind_unbound_port(xendev->evtchndev,
+                                                     remote_dom);
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice *xendev)
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}
+
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +67 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel complete */
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be zero */
+    uint32_t extra_pages[0]; /* grant IDs; length is actually nr_extra_pages */
+};

hw/xen/xen_stubdom_vtpm.c line 77

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};

This is a private structure, you cannot just copy the definition here.

You do not even need it in fact, since sr_bh->ctx is always vtpm_aio_ctx.

Paolo

Quoted 346 lines
hw/xen/xen_stubdom_vtpm.c new file +255 -0
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum xenbus_state xbus)
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t *count)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t count)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev, "frontend");
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs, vtpmdev->bedomid, 1,
+                                         &ring_ref, PROT_READ|PROT_WRITE);
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0, vtpmdev->bedomid)) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char *node)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev, XenbusStateClosing);
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler, vtpmdev);
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct XenVtpmDev,
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};
include/hw/xen/xen_backend.h +11 -0
index 3b4125e..45fd6d3 100644
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console quirk) */
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t count);
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t *count);
     void      (*backend_changed)(struct XenDevice *xendev, const char *node);
     void      (*frontend_changed)(struct XenDevice *xendev, const char *node);
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice *xendev);
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int remote_dom);
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c     */
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c */
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c        */
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c         */
+extern struct XenDevOps xen_vtpmdev_ops;      /* xen_stubdom_vtpm.c*/
 
 void xen_init_display(int domid);
 
include/hw/xen/xen_common.h +6 -0
index 95612a4..fb43084 100644
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +13 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
         fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif
+
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
author
#2 lore raw
In reply to Stefano Stabellini
Quoted 16 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system. Each virtual machine gets its own unique, emulated, software TPM. Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver.
This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root of trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory pages into vtpmmgr stubdom.

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Quoted 762 lines
Quoted 5 lines

 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +0 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines
hw/xen/xen_backend.c +0 -0
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 22 lines
hw/xen/xen_backend.c +19 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const

Quoted 1 line
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const

char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +1 -0
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 48 lines
hw/xen/xen_backend.c +41 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 759

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 761

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 19 lines
hw/xen/xen_backend.c +12 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 834

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 836

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

hw/xen/xen_backend.c err:

Quoted 6 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom)

hw/xen/xen_backend.c err: · line 869

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 870

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 9 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

Quoted 28 lines
hw/xen/xen_backend.c +25 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 14 lines
hw/xen/xen_backend.c +0 -0
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +5 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

hw/xen/xen_stubdom_vtpm.c

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 66

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 26 lines
hw/xen/xen_stubdom_vtpm.c new file +26 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 32 lines
hw/xen/xen_stubdom_vtpm.c new file +32 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 146

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 211

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char

*node)

hw/xen/xen_stubdom_vtpm.c line 265

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

include/hw/xen/xen_backend.h struct XenDevice; · line 16

Quoted 6 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines
include/hw/xen/xen_backend.h +3 -0
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 95

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 106

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 108

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 109

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 110

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+extern struct XenDevOps xen_vtpmdev_ops;      /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_init_display(int domid);
 
(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 17 lines
include/hw/xen/xen_common.h +6 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +0 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 12 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1174

Quoted 1 line
xen-hvm.c +0 -0
         fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +3 -0
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Also I will explain in my next email.

Quoted 8 lines
Quoted 8 lines
xen-hvm.c +0 -1
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
--

1.8.3.2

author
#19 lore raw
In reply to Paolo Bonzini
Quoted 379 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]] On Behalf Of Paolo Bonzini
Sent: Monday, November 03, 2014 8:13 PM
To: Xu, Quan; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 9 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

---
 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create mode

100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines

xen_pt_msi.o diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c index b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 31 lines
hw/xen/xen_backend.c +25 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int
Quoted 1 line

dev) @@ -222,6 +248,7 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 1 line

struct XenDevOps *ops)

{

hw/xen/xen_backend.c static struct XenDevice

Quoted 6 lines
     struct XenDevice *xendev;
hw/xen/xen_backend.c +1 -0
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 35 lines
hw/xen/xen_backend.c +32 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 10 lines
hw/xen/xen_backend.c +8 -0
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)  { @@

-681,6

Quoted 5 lines
hw/xen/xen_backend.c +1 -0
+756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }

+ if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 18 lines
hw/xen/xen_backend.c +11 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 833

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 835

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+            return -1;
+        }
+    }
+

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+    free(dev);
+    return 0;
+}
+
 int xen_be_register(const char *type, struct XenDevOps *ops)  {
     return xenstore_scan(type, xen_domid, ops);  }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
+remote_dom) {
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 868

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)  {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

hw/xen/xen_stubdom_vtpm.c

Quoted 41 lines
hw/xen/xen_backend.c +34 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}
+
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c +0 -0

new

Quoted 9 lines

file mode 100644 index 0000000..0d740c1

hw/xen/xen_stubdom_vtpm.c new file +5 -0
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

Quoted 18 lines
hw/xen/xen_stubdom_vtpm.c new file +18 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+<http://www.gnu.org/licenses/>  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 77

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually
+nr_extra_pages */ };
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};

This is a private structure, you cannot just copy the definition here.

Quoted 4 lines

You do not even need it in fact, since sr_bh->ctx is always vtpm_aio_ctx.

Paolo

Thanks Paolo, I will enhance it in v2.

Quan

Quoted 364 lines
Quoted 55 lines
hw/xen/xen_stubdom_vtpm.c new file +54 -0
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev) {
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum
+xenbus_state xbus) {
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+static int vtpm_aio_wait(QEMUBH *qbh) {
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque) { }
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t
+*count) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t
+count) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 205

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);
+
+    return 0;

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char
+*node) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 9 lines
hw/xen/xen_stubdom_vtpm.c new file +9 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    qemu_bh_delete(vtpmdev->sr_bh);
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c line 306

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0); }
+
+static void vtpm_event(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 30 lines
hw/xen/xen_stubdom_vtpm.c new file +16 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh); }
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */ #define DEVOPS_FLAG_IGNORE_STATE 2

include/hw/xen/xen_backend.h +2 -0
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 4 lines
include/hw/xen/xen_backend.h +1 -0
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

Quoted 1 line

void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h

Quoted 10 lines
     GCC_FMT_ATTR(3, 4);
include/hw/xen/xen_backend.h +6 -0
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops); int
+xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
+remote_dom); int xen_vtpm_send(unsigned char *buf, size_t count); int
+xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

Quoted 1 line

extern struct XenDevOps xen_kbdmouse_ops; /* xen_framebuffer.c

*/

Quoted 2 lines

extern struct XenDevOps xen_framebuffer_ops; /* xen_framebuffer.c */
extern struct XenDevOps xen_blkdev_ops; /* xen_disk.c

*/

Quoted 1 line

extern struct XenDevOps xen_netdev_ops; /* xen_nic.c

*/

Quoted 1 line

+extern struct XenDevOps xen_vtpmdev_ops; /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines

void xen_init_display(int domid);

(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger, · line 134

Quoted 4 lines
include/hw/xen/xen_common.h +2 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);  }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)

{

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 8 lines
include/hw/xen/xen_common.h +2 -0
+    return xc_gntshr_open(logger, open_flags); }
+
 /* FIXME There is now way to have the xen fd */  static inline int

xc_fd(xc_interface *xen_xc) { diff --git a/xen-hvm.c b/xen-hvm.c index 21f1cbb..c99ace8 100644

xen-hvm.c +0 -0
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 11 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState)); @@ -1169,6 +1174,14 @@
Quoted 1 line

int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t

*above_4g_mem_size,

Quoted 1 line

fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
         return -1;
     }
xen-hvm.c +3 -0
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 9 lines
xen-hvm.c +5 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif
+
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
author
#20 lore raw
In reply to Paolo Bonzini
Quoted 379 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]] On Behalf Of Paolo Bonzini
Sent: Monday, November 03, 2014 8:13 PM
To: Xu, Quan; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 9 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

---
 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create mode

100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines

xen_pt_msi.o diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c index b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 31 lines
hw/xen/xen_backend.c +25 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int
Quoted 1 line

dev) @@ -222,6 +248,7 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 1 line

struct XenDevOps *ops)

{

hw/xen/xen_backend.c static struct XenDevice

Quoted 6 lines
     struct XenDevice *xendev;
hw/xen/xen_backend.c +1 -0
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 35 lines
hw/xen/xen_backend.c +32 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 10 lines
hw/xen/xen_backend.c +8 -0
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)  { @@

-681,6

Quoted 5 lines
hw/xen/xen_backend.c +1 -0
+756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }

+ if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 18 lines
hw/xen/xen_backend.c +11 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 833

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 835

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+            return -1;
+        }
+    }
+

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+    free(dev);
+    return 0;
+}
+
 int xen_be_register(const char *type, struct XenDevOps *ops)  {
     return xenstore_scan(type, xen_domid, ops);  }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
+remote_dom) {
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 868

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)  {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

hw/xen/xen_stubdom_vtpm.c

Quoted 41 lines
hw/xen/xen_backend.c +34 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}
+
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c +0 -0

new

Quoted 9 lines

file mode 100644 index 0000000..0d740c1

hw/xen/xen_stubdom_vtpm.c new file +5 -0
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

Quoted 18 lines
hw/xen/xen_stubdom_vtpm.c new file +18 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+<http://www.gnu.org/licenses/>  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 77

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually
+nr_extra_pages */ };
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};

This is a private structure, you cannot just copy the definition here.

Quoted 4 lines

You do not even need it in fact, since sr_bh->ctx is always vtpm_aio_ctx.

Paolo

Thanks Paolo, I will enhance it in v2.

Quan

Quoted 364 lines
Quoted 55 lines
hw/xen/xen_stubdom_vtpm.c new file +54 -0
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev) {
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum
+xenbus_state xbus) {
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+static int vtpm_aio_wait(QEMUBH *qbh) {
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque) { }
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t
+*count) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t
+count) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 205

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);
+
+    return 0;

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char
+*node) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 9 lines
hw/xen/xen_stubdom_vtpm.c new file +9 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    qemu_bh_delete(vtpmdev->sr_bh);
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c line 306

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0); }
+
+static void vtpm_event(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 30 lines
hw/xen/xen_stubdom_vtpm.c new file +16 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh); }
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */ #define DEVOPS_FLAG_IGNORE_STATE 2

include/hw/xen/xen_backend.h +2 -0
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 4 lines
include/hw/xen/xen_backend.h +1 -0
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

Quoted 1 line

void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h

Quoted 10 lines
     GCC_FMT_ATTR(3, 4);
include/hw/xen/xen_backend.h +6 -0
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops); int
+xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
+remote_dom); int xen_vtpm_send(unsigned char *buf, size_t count); int
+xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

Quoted 1 line

extern struct XenDevOps xen_kbdmouse_ops; /* xen_framebuffer.c

*/

Quoted 2 lines

extern struct XenDevOps xen_framebuffer_ops; /* xen_framebuffer.c */
extern struct XenDevOps xen_blkdev_ops; /* xen_disk.c

*/

Quoted 1 line

extern struct XenDevOps xen_netdev_ops; /* xen_nic.c

*/

Quoted 1 line

+extern struct XenDevOps xen_vtpmdev_ops; /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines

void xen_init_display(int domid);

(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger, · line 134

Quoted 4 lines
include/hw/xen/xen_common.h +2 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);  }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)

{

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 8 lines
include/hw/xen/xen_common.h +2 -0
+    return xc_gntshr_open(logger, open_flags); }
+
 /* FIXME There is now way to have the xen fd */  static inline int

xc_fd(xc_interface *xen_xc) { diff --git a/xen-hvm.c b/xen-hvm.c index 21f1cbb..c99ace8 100644

xen-hvm.c +0 -0
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 11 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState)); @@ -1169,6 +1174,14 @@
Quoted 1 line

int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t

*above_4g_mem_size,

Quoted 1 line

fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
         return -1;
     }
xen-hvm.c +3 -0
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 9 lines
xen-hvm.c +5 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif
+
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
#3 lore raw
In reply to Xu, Quan
Quoted 96 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system. Each virtual machine gets its own unique, emulated, software TPM. Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver.
This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+
Quoted 7 lines

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Quoted 5 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Quoted 5 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Quoted 25 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root of trust for the entire system.

Quoted 5 lines

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory pages into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 9 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Quoted 783 lines
Quoted 762 lines
Quoted 5 lines

 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +0 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines
hw/xen/xen_backend.c +0 -0
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 22 lines
hw/xen/xen_backend.c +19 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const

Quoted 1 line
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const

char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +1 -0
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 48 lines
hw/xen/xen_backend.c +41 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 759

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 761

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 19 lines
hw/xen/xen_backend.c +12 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 834

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 836

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

hw/xen/xen_backend.c err:

Quoted 6 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom)

hw/xen/xen_backend.c err: · line 869

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 870

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 9 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

Quoted 28 lines
hw/xen/xen_backend.c +25 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 14 lines
hw/xen/xen_backend.c +0 -0
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +5 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

hw/xen/xen_stubdom_vtpm.c

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 66

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 26 lines
hw/xen/xen_stubdom_vtpm.c new file +26 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 32 lines
hw/xen/xen_stubdom_vtpm.c new file +32 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 146

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 211

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char

*node)

hw/xen/xen_stubdom_vtpm.c line 265

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

include/hw/xen/xen_backend.h struct XenDevice; · line 16

Quoted 6 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines
include/hw/xen/xen_backend.h +3 -0
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 95

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 106

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 108

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 109

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 110

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+extern struct XenDevOps xen_vtpmdev_ops;      /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_init_display(int domid);
 
(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 17 lines
include/hw/xen/xen_common.h +6 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +0 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 12 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1174

Quoted 1 line
xen-hvm.c +0 -0
         fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +3 -0
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Also I will explain in my next email.

Quoted 8 lines
Quoted 8 lines
xen-hvm.c +0 -1
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
--

1.8.3.2

#4 lore raw
In reply to Xu, Quan
Quoted 96 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system. Each virtual machine gets its own unique, emulated, software TPM. Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver.
This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+
Quoted 7 lines

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Quoted 5 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Quoted 5 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Quoted 25 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root of trust for the entire system.

Quoted 5 lines

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory pages into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 9 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Quoted 783 lines
Quoted 762 lines
Quoted 5 lines

 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +0 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines
hw/xen/xen_backend.c +0 -0
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 22 lines
hw/xen/xen_backend.c +19 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const

Quoted 1 line
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const

char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +1 -0
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 48 lines
hw/xen/xen_backend.c +41 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 759

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 761

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 19 lines
hw/xen/xen_backend.c +12 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 834

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 836

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

hw/xen/xen_backend.c err:

Quoted 6 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom)

hw/xen/xen_backend.c err: · line 869

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 870

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 9 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

Quoted 28 lines
hw/xen/xen_backend.c +25 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 14 lines
hw/xen/xen_backend.c +0 -0
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +5 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

hw/xen/xen_stubdom_vtpm.c

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 66

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 26 lines
hw/xen/xen_stubdom_vtpm.c new file +26 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 32 lines
hw/xen/xen_stubdom_vtpm.c new file +32 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 146

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 211

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char

*node)

hw/xen/xen_stubdom_vtpm.c line 265

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

include/hw/xen/xen_backend.h struct XenDevice; · line 16

Quoted 6 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines
include/hw/xen/xen_backend.h +3 -0
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 95

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 106

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 108

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 109

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 110

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+extern struct XenDevOps xen_vtpmdev_ops;      /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_init_display(int domid);
 
(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 17 lines
include/hw/xen/xen_common.h +6 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +0 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 12 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1174

Quoted 1 line
xen-hvm.c +0 -0
         fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +3 -0
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Also I will explain in my next email.

Quoted 8 lines
Quoted 8 lines
xen-hvm.c +0 -1
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
--

1.8.3.2

author
#5 lore raw
Quoted 112 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Wednesday, November 05, 2014 7:01 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 31 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Quoted 8 lines

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system.

Each virtual machine gets its own unique, emulated, software TPM.

Quoted 61 lines

Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver. This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

Quoted 8 lines
Quoted 3 lines

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Not so.
Refer to below further explanation.

Quoted 11 lines
Quoted 7 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Yes, it is Patch #3. Agree with your description. :)

Quoted 8 lines
Quoted 6 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV

Yes, it patch #2.

Quoted 4 lines

frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

Yes, it's tpmfront implementation in Qemu. also it supports PV Linux since Xen 4.3.0. vTPM stubdom / vtpmmgr stubdom ware implemented by Matthew Fioravante (JHUAPL), Linux pv vTPM frontend was implemented by Daniel De Graaf (NSA)

Quoted 2 lines

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Not so.
Maybe I should explain a bit further. TPM is a character device. 'Qemu tpm1.2 Tis' Implement some TPM register hook of TPM, and enable TIS (TPM interface Specification). The ' Qemu tpm1.2 Tis ' should deal with TPM commands.

Such as TPM_ContinueSelfTest command,

    { 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x53};
If success, the TPM will respond,
    { 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00};

Qemu upstream has supported TPM passthrough. actually, 'Qemu TPM passthrough' send These TPM commands from "Qemu tpm1.2 Tis" to TPM hardware, and get the respond.

" [PATCH 0/4] Qemu-Xen-vTPM: enable Xen stubdom vTPM for HVM virtual machine " send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom( explain in previous email) And get the respond.

#(qemu-system-* --tpmdev help)
#(passthrough Passthrough TPM backend driver)
#( xenstubdoms Xenstubdoms TPM backend driver)

The following is Qemu part.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +------------------------------+
                     |  ^
                     v  |
            +------------------------------+
            |  xen_vtpmdev_ops  |  (new ..)
            +------------------------------+

"vTPM XenStubdoms driver" and "xen_vtpmdev_ops" are implemented to send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom.

Quoted 41 lines
Quoted 20 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

Quoted 5 lines

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root

of

Quoted 6 lines

trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory

pages

Quoted 1 line

into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 3 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

Quoted 9 lines
Quoted 6 lines

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Welcome, thanks for your careful review.

Quoted 787 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines


 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create mode

100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

Quoted 1 line
Quoted 1 line

hw/xen/Makefile.objs line 3

Quoted 1 line
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) +=

xen_stubdom_vtpm.o

Quoted 42 lines
Quoted 42 lines

hw/xen/Makefile.objs line 5

Quoted 3 lines
hw/xen/Makefile.objs +0 -0
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice · line 194

Quoted 6 lines
hw/xen/xen_backend.c index renamed from hw/xen/xen_backend.c +0 -0

b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice
 *xendev,

enum xenbus_state state)

Quoted 31 lines
hw/xen/xen_backend.c +25 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* -------------------------------------------------------------
 */
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

struct XenDevice *xen_be_find_xendev(const char *type, int dom,
int

dev)

hw/xen/xen_backend.c static struct XenDevice · line 248

Quoted 2 lines
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice
 *xen_be_get_xendev(const

char *type, int dom, int dev,

Quoted 1 line
hw/xen/xen_backend.c +0 -0
                                            struct XenDevOps

*ops)

Quoted 15 lines
Quoted 15 lines

hw/xen/xen_backend.c static struct XenDevice

Quoted 7 lines
hw/xen/xen_backend.c +1 -0
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
hw/xen/xen_backend.c +0 -0
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,
Quoted 7 lines
Quoted 7 lines

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 59 lines
Quoted 59 lines
Quoted 7 lines
hw/xen/xen_backend.c +2 -0
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type,

int

dom, struct XenDevOps *ops)

Quoted 40 lines
     return 0;

}

hw/xen/xen_backend.c +37 -0
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 10 lines
hw/xen/xen_backend.c +4 -1
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)  { @@
-681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

Quoted 2 lines
Quoted 2 lines

hw/xen/xen_backend.c

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type,

dom,

Quoted 68 lines
Quoted 68 lines

(void *)ops);

Quoted 18 lines
hw/xen/xen_backend.c +11 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 833

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 835

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)  {
     return xenstore_scan(type, xen_domid, ops);  }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
Quoted 8 lines
Quoted 8 lines

remote_dom)

hw/xen/xen_backend.c err: · line 866

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 867

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound

failed\n");

Quoted 54 lines
Quoted 54 lines

hw/xen/xen_backend.c err:

Quoted 3 lines
hw/xen/xen_backend.c +3 -0
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

Quoted 8 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)  {
     if (xendev->local_port != -1) { @@ -770,6 +912,42 @@ int

xen_be_send_notify(struct XenDevice

*xendev)

Quoted 26 lines
     return xc_evtchn_notify(xendev->evtchndev,

xendev->local_port); }

hw/xen/xen_backend.c +23 -0
+int xen_vtpm_send(unsigned char *buf, size_t count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines

/*
* msg_level:
* 0 == errors (stderr + logfile).

(quoted diff) +0 -0

b/hw/xen/xen_stubdom_vtpm.c

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

new file mode 100644 index 0000000..0d740c1

hw/xen/xen_stubdom_vtpm.c new file +7 -0
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:
+ *    Quan Xu <[email protected]>
+ *
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 8 lines
hw/xen/xen_stubdom_vtpm.c new file +8 -0
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be
+useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty

of

Quoted 1 line
Quoted 1 line

hw/xen/xen_stubdom_vtpm.c line 16

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See

the

Quoted 32 lines
Quoted 32 lines

GNU

hw/xen/xen_stubdom_vtpm.c line 21

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 3 lines
Quoted 3 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 3 lines
hw/xen/xen_stubdom_vtpm.c new file +3 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM

working */

Quoted 231 lines
Quoted 231 lines
Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,
+};

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets;
+ may be

zero */

hw/xen/xen_stubdom_vtpm.c line 69

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 25 lines
hw/xen/xen_stubdom_vtpm.c new file +25 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev) {
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 29 lines
hw/xen/xen_stubdom_vtpm.c new file +29 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    return -1;
+}
+
+static int vtpm_aio_wait(QEMUBH *qbh) {
+    return aio_poll(qbh->ctx, true); }
+
+static void sr_bh_handler(void *opaque) { }
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf,
+size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 145

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 11 lines
hw/xen/xen_stubdom_vtpm.c new file +11 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf,
+size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 210

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 16 lines
hw/xen/xen_stubdom_vtpm.c new file +16 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {
+        goto abort_transaction;

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe,
+ "feature-protocol-v2", 1))

{

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev,
+ XenbusStateInitialised);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const
+char

*node)

Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c line 267

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node,

&be_state);

Quoted 111 lines
Quoted 111 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 9 lines
hw/xen/xen_stubdom_vtpm.c new file +9 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    qemu_bh_delete(vtpmdev->sr_bh);
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c line 314

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0); }
+
+static void vtpm_event(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 6 lines
hw/xen/xen_stubdom_vtpm.c new file +6 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh); }
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

Quoted 7 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka

console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines

#define DEVOPS_FLAG_IGNORE_STATE 2

include/hw/xen/xen_backend.h +3 -0
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const

char

*fmt, ...)

include/hw/xen/xen_backend.h

Quoted 5 lines
     GCC_FMT_ATTR(3, 4);
include/hw/xen/xen_backend.h +3 -0
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops); int
+xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

Quoted 7 lines
Quoted 7 lines

include/hw/xen/xen_backend.h

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count); int
+xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

Quoted 1 line

extern struct XenDevOps xen_kbdmouse_ops; /*

xen_framebuffer.c

Quoted 2 lines
Quoted 2 lines

*/

Quoted 1 line

extern struct XenDevOps xen_framebuffer_ops; /*

xen_framebuffer.c */

Quoted 73 lines
Quoted 52 lines
Quoted 1 line

extern struct XenDevOps xen_blkdev_ops; /* xen_disk.c

*/

Quoted 1 line

extern struct XenDevOps xen_netdev_ops; /* xen_nic.c

*/

Quoted 1 line

+extern struct XenDevOps xen_vtpmdev_ops; /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines

void xen_init_display(int domid);

(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

Quoted 5 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC

xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 14 lines

return xc_interface_open(logger, dombuild_logger,
open_flags); }

include/hw/xen/xen_common.h +5 -0
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int
+open_flags) {
+    return xc_gntshr_open(logger, open_flags); }
+
 /* FIXME There is now way to have the xen fd */  static inline

int xc_fd(xc_interface *xen_xc) { diff --git a/xen-hvm.c b/xen-hvm.c index 21f1cbb..c99ace8 100644

xen-hvm.c +0 -0
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 11 lines
xen-hvm.c +4 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0; #endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState)); @@ -1169,6 +1174,14

@@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 1 line

fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
         return -1;
     }
xen-hvm.c +3 -0
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Also I will explain in my next email.

Quoted 8 lines
Quoted 8 lines

     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);

--
1.8.3.2

author
#6 lore raw
In reply to Stefano Stabellini
Quoted 112 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Wednesday, November 05, 2014 7:01 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 31 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Quoted 8 lines

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system.

Each virtual machine gets its own unique, emulated, software TPM.

Quoted 61 lines

Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver. This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

Quoted 8 lines
Quoted 3 lines

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Not so.
Refer to below further explanation.

Quoted 11 lines
Quoted 7 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Yes, it is Patch #3. Agree with your description. :)

Quoted 8 lines
Quoted 6 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV

Yes, it patch #2.

Quoted 4 lines

frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

Yes, it's tpmfront implementation in Qemu. also it supports PV Linux since Xen 4.3.0. vTPM stubdom / vtpmmgr stubdom ware implemented by Matthew Fioravante (JHUAPL), Linux pv vTPM frontend was implemented by Daniel De Graaf (NSA)

Quoted 2 lines

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Not so.
Maybe I should explain a bit further. TPM is a character device. 'Qemu tpm1.2 Tis' Implement some TPM register hook of TPM, and enable TIS (TPM interface Specification). The ' Qemu tpm1.2 Tis ' should deal with TPM commands.

Such as TPM_ContinueSelfTest command,

    { 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x53};
If success, the TPM will respond,
    { 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00};

Qemu upstream has supported TPM passthrough. actually, 'Qemu TPM passthrough' send These TPM commands from "Qemu tpm1.2 Tis" to TPM hardware, and get the respond.

" [PATCH 0/4] Qemu-Xen-vTPM: enable Xen stubdom vTPM for HVM virtual machine " send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom( explain in previous email) And get the respond.

#(qemu-system-* --tpmdev help)
#(passthrough Passthrough TPM backend driver)
#( xenstubdoms Xenstubdoms TPM backend driver)

The following is Qemu part.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +------------------------------+
                     |  ^
                     v  |
            +------------------------------+
            |  xen_vtpmdev_ops  |  (new ..)
            +------------------------------+

"vTPM XenStubdoms driver" and "xen_vtpmdev_ops" are implemented to send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom.

Quoted 41 lines
Quoted 20 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

Quoted 5 lines

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root

of

Quoted 6 lines

trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory

pages

Quoted 1 line

into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 3 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

Quoted 9 lines
Quoted 6 lines

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Welcome, thanks for your careful review.

Quoted 787 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines


 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create mode

100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

Quoted 1 line
Quoted 1 line

hw/xen/Makefile.objs line 3

Quoted 1 line
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) +=

xen_stubdom_vtpm.o

Quoted 42 lines
Quoted 42 lines

hw/xen/Makefile.objs line 5

Quoted 3 lines
hw/xen/Makefile.objs +0 -0
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice · line 194

Quoted 6 lines
hw/xen/xen_backend.c index renamed from hw/xen/xen_backend.c +0 -0

b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice
 *xendev,

enum xenbus_state state)

Quoted 31 lines
hw/xen/xen_backend.c +25 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* -------------------------------------------------------------
 */
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

struct XenDevice *xen_be_find_xendev(const char *type, int dom,
int

dev)

hw/xen/xen_backend.c static struct XenDevice · line 248

Quoted 2 lines
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice
 *xen_be_get_xendev(const

char *type, int dom, int dev,

Quoted 1 line
hw/xen/xen_backend.c +0 -0
                                            struct XenDevOps

*ops)

Quoted 15 lines
Quoted 15 lines

hw/xen/xen_backend.c static struct XenDevice

Quoted 7 lines
hw/xen/xen_backend.c +1 -0
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
hw/xen/xen_backend.c +0 -0
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,
Quoted 7 lines
Quoted 7 lines

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 59 lines
Quoted 59 lines
Quoted 7 lines
hw/xen/xen_backend.c +2 -0
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type,

int

dom, struct XenDevOps *ops)

Quoted 40 lines
     return 0;

}

hw/xen/xen_backend.c +37 -0
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 10 lines
hw/xen/xen_backend.c +4 -1
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)  { @@
-681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

Quoted 2 lines
Quoted 2 lines

hw/xen/xen_backend.c

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type,

dom,

Quoted 68 lines
Quoted 68 lines

(void *)ops);

Quoted 18 lines
hw/xen/xen_backend.c +11 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 833

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 835

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)  {
     return xenstore_scan(type, xen_domid, ops);  }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
Quoted 8 lines
Quoted 8 lines

remote_dom)

hw/xen/xen_backend.c err: · line 866

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 867

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound

failed\n");

Quoted 54 lines
Quoted 54 lines

hw/xen/xen_backend.c err:

Quoted 3 lines
hw/xen/xen_backend.c +3 -0
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

Quoted 8 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)  {
     if (xendev->local_port != -1) { @@ -770,6 +912,42 @@ int

xen_be_send_notify(struct XenDevice

*xendev)

Quoted 26 lines
     return xc_evtchn_notify(xendev->evtchndev,

xendev->local_port); }

hw/xen/xen_backend.c +23 -0
+int xen_vtpm_send(unsigned char *buf, size_t count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines

/*
* msg_level:
* 0 == errors (stderr + logfile).

(quoted diff) +0 -0

b/hw/xen/xen_stubdom_vtpm.c

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

new file mode 100644 index 0000000..0d740c1

hw/xen/xen_stubdom_vtpm.c new file +7 -0
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:
+ *    Quan Xu <[email protected]>
+ *
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 8 lines
hw/xen/xen_stubdom_vtpm.c new file +8 -0
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be
+useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty

of

Quoted 1 line
Quoted 1 line

hw/xen/xen_stubdom_vtpm.c line 16

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See

the

Quoted 32 lines
Quoted 32 lines

GNU

hw/xen/xen_stubdom_vtpm.c line 21

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 3 lines
Quoted 3 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 3 lines
hw/xen/xen_stubdom_vtpm.c new file +3 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM

working */

Quoted 231 lines
Quoted 231 lines
Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,
+};

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets;
+ may be

zero */

hw/xen/xen_stubdom_vtpm.c line 69

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 25 lines
hw/xen/xen_stubdom_vtpm.c new file +25 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev) {
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 29 lines
hw/xen/xen_stubdom_vtpm.c new file +29 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    return -1;
+}
+
+static int vtpm_aio_wait(QEMUBH *qbh) {
+    return aio_poll(qbh->ctx, true); }
+
+static void sr_bh_handler(void *opaque) { }
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf,
+size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 145

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 11 lines
hw/xen/xen_stubdom_vtpm.c new file +11 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf,
+size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 210

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 16 lines
hw/xen/xen_stubdom_vtpm.c new file +16 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {
+        goto abort_transaction;

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe,
+ "feature-protocol-v2", 1))

{

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev,
+ XenbusStateInitialised);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const
+char

*node)

Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c line 267

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node,

&be_state);

Quoted 111 lines
Quoted 111 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 9 lines
hw/xen/xen_stubdom_vtpm.c new file +9 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    qemu_bh_delete(vtpmdev->sr_bh);
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c line 314

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0); }
+
+static void vtpm_event(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 6 lines
hw/xen/xen_stubdom_vtpm.c new file +6 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh); }
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

Quoted 7 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka

console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines

#define DEVOPS_FLAG_IGNORE_STATE 2

include/hw/xen/xen_backend.h +3 -0
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const

char

*fmt, ...)

include/hw/xen/xen_backend.h

Quoted 5 lines
     GCC_FMT_ATTR(3, 4);
include/hw/xen/xen_backend.h +3 -0
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops); int
+xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

Quoted 7 lines
Quoted 7 lines

include/hw/xen/xen_backend.h

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count); int
+xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

Quoted 1 line

extern struct XenDevOps xen_kbdmouse_ops; /*

xen_framebuffer.c

Quoted 2 lines
Quoted 2 lines

*/

Quoted 1 line

extern struct XenDevOps xen_framebuffer_ops; /*

xen_framebuffer.c */

Quoted 73 lines
Quoted 52 lines
Quoted 1 line

extern struct XenDevOps xen_blkdev_ops; /* xen_disk.c

*/

Quoted 1 line

extern struct XenDevOps xen_netdev_ops; /* xen_nic.c

*/

Quoted 1 line

+extern struct XenDevOps xen_vtpmdev_ops; /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines

void xen_init_display(int domid);

(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

Quoted 5 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC

xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 14 lines

return xc_interface_open(logger, dombuild_logger,
open_flags); }

include/hw/xen/xen_common.h +5 -0
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int
+open_flags) {
+    return xc_gntshr_open(logger, open_flags); }
+
 /* FIXME There is now way to have the xen fd */  static inline

int xc_fd(xc_interface *xen_xc) { diff --git a/xen-hvm.c b/xen-hvm.c index 21f1cbb..c99ace8 100644

xen-hvm.c +0 -0
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 11 lines
xen-hvm.c +4 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0; #endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState)); @@ -1169,6 +1174,14

@@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 1 line

fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
         return -1;
     }
xen-hvm.c +3 -0
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Also I will explain in my next email.

Quoted 8 lines
Quoted 8 lines

     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);

--
1.8.3.2

#7 lore raw
Quoted 196 lines · Quan
Quoted 110 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Wednesday, November 05, 2014 7:01 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 31 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Quoted 8 lines

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system.

Each virtual machine gets its own unique, emulated, software TPM.

Quoted 61 lines

Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver. This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

Quoted 8 lines
Quoted 3 lines

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Not so.
Refer to below further explanation.

Quoted 11 lines
Quoted 7 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Yes, it is Patch #3. Agree with your description. :)

Quoted 8 lines
Quoted 6 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV

Yes, it patch #2.

Quoted 4 lines

frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

Yes, it's tpmfront implementation in Qemu. also it supports PV Linux since Xen 4.3.0. vTPM stubdom / vtpmmgr stubdom ware implemented by Matthew Fioravante (JHUAPL), Linux pv vTPM frontend was implemented by Daniel De Graaf (NSA)

Quoted 2 lines

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Not so.
Maybe I should explain a bit further. TPM is a character device. 'Qemu tpm1.2 Tis' Implement some TPM register hook of TPM, and enable TIS (TPM interface Specification). The ' Qemu tpm1.2 Tis ' should deal with TPM commands.

Such as TPM_ContinueSelfTest command,

    { 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x53};
If success, the TPM will respond,
    { 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00};

Qemu upstream has supported TPM passthrough. actually, 'Qemu TPM passthrough' send These TPM commands from "Qemu tpm1.2 Tis" to TPM hardware, and get the respond.

" [PATCH 0/4] Qemu-Xen-vTPM: enable Xen stubdom vTPM for HVM virtual machine " send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom( explain in previous email) And get the respond.

#(qemu-system-* --tpmdev help)
#(passthrough Passthrough TPM backend driver)
#( xenstubdoms Xenstubdoms TPM backend driver)

The following is Qemu part.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +------------------------------+
                     |  ^
                     v  |
            +------------------------------+
            |  xen_vtpmdev_ops  |  (new ..)
            +------------------------------+
Quoted 2 lines

"vTPM XenStubdoms driver" and "xen_vtpmdev_ops" are implemented to send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom.

OK, so if I understand correctly in your architecture QEMU is running in Dom0 as usual, right?

Quoted 843 lines
Quoted 40 lines
Quoted 19 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

Quoted 5 lines

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root

of

Quoted 6 lines

trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory

pages

Quoted 1 line

into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 3 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

Quoted 9 lines
Quoted 6 lines

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Welcome, thanks for your careful review.

Quoted 787 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines


 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create mode

100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

Quoted 1 line
Quoted 1 line

hw/xen/Makefile.objs line 3

Quoted 1 line
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) +=

xen_stubdom_vtpm.o

Quoted 42 lines
Quoted 42 lines

hw/xen/Makefile.objs line 5

Quoted 3 lines
hw/xen/Makefile.objs +0 -0
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice · line 194

Quoted 6 lines
hw/xen/xen_backend.c index renamed from hw/xen/xen_backend.c +0 -0

b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice
 *xendev,

enum xenbus_state state)

Quoted 31 lines
hw/xen/xen_backend.c +25 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* -------------------------------------------------------------
 */
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

struct XenDevice *xen_be_find_xendev(const char *type, int dom,
int

dev)

hw/xen/xen_backend.c static struct XenDevice · line 248

Quoted 2 lines
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice
 *xen_be_get_xendev(const

char *type, int dom, int dev,

Quoted 1 line
hw/xen/xen_backend.c +0 -0
                                            struct XenDevOps

*ops)

Quoted 15 lines
Quoted 15 lines

hw/xen/xen_backend.c static struct XenDevice

Quoted 7 lines
hw/xen/xen_backend.c +1 -0
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
hw/xen/xen_backend.c +0 -0
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,
Quoted 7 lines
Quoted 7 lines

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 59 lines
Quoted 59 lines
Quoted 7 lines
hw/xen/xen_backend.c +2 -0
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type,

int

dom, struct XenDevOps *ops)

Quoted 40 lines
     return 0;

}

hw/xen/xen_backend.c +37 -0
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 10 lines
hw/xen/xen_backend.c +4 -1
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)  { @@
-681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

Quoted 2 lines
Quoted 2 lines

hw/xen/xen_backend.c

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type,

dom,

Quoted 68 lines
Quoted 68 lines

(void *)ops);

Quoted 18 lines
hw/xen/xen_backend.c +11 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 833

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 835

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)  {
     return xenstore_scan(type, xen_domid, ops);  }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
Quoted 8 lines
Quoted 8 lines

remote_dom)

hw/xen/xen_backend.c err: · line 866

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 867

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound

failed\n");

Quoted 54 lines
Quoted 54 lines

hw/xen/xen_backend.c err:

Quoted 3 lines
hw/xen/xen_backend.c +3 -0
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

Quoted 8 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)  {
     if (xendev->local_port != -1) { @@ -770,6 +912,42 @@ int

xen_be_send_notify(struct XenDevice

*xendev)

Quoted 26 lines
     return xc_evtchn_notify(xendev->evtchndev,

xendev->local_port); }

hw/xen/xen_backend.c +23 -0
+int xen_vtpm_send(unsigned char *buf, size_t count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines

/*
* msg_level:
* 0 == errors (stderr + logfile).

(quoted diff) +0 -0

b/hw/xen/xen_stubdom_vtpm.c

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

new file mode 100644 index 0000000..0d740c1

hw/xen/xen_stubdom_vtpm.c new file +7 -0
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:
+ *    Quan Xu <[email protected]>
+ *
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 8 lines
hw/xen/xen_stubdom_vtpm.c new file +8 -0
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be
+useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty

of

Quoted 1 line
Quoted 1 line

hw/xen/xen_stubdom_vtpm.c line 16

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See

the

Quoted 32 lines
Quoted 32 lines

GNU

hw/xen/xen_stubdom_vtpm.c line 21

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 3 lines
Quoted 3 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 3 lines
hw/xen/xen_stubdom_vtpm.c new file +3 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM

working */

Quoted 231 lines
Quoted 231 lines
Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,
+};

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets;
+ may be

zero */

hw/xen/xen_stubdom_vtpm.c line 69

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 25 lines
hw/xen/xen_stubdom_vtpm.c new file +25 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev) {
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 29 lines
hw/xen/xen_stubdom_vtpm.c new file +29 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    return -1;
+}
+
+static int vtpm_aio_wait(QEMUBH *qbh) {
+    return aio_poll(qbh->ctx, true); }
+
+static void sr_bh_handler(void *opaque) { }
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf,
+size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 145

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 11 lines
hw/xen/xen_stubdom_vtpm.c new file +11 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf,
+size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 210

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 16 lines
hw/xen/xen_stubdom_vtpm.c new file +16 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {
+        goto abort_transaction;

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe,
+ "feature-protocol-v2", 1))

{

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev,
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

hw/xen/xen_stubdom_vtpm.c line 262

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ XenbusStateInitialised);
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
[… more lines in the raw message]

*node)

Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

&be_state);

Quoted 111 lines
Quoted 111 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 9 lines
hw/xen/xen_stubdom_vtpm.c new file +9 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenVtpmDev,

75 lines not shown · raw message

Quoted 7 lines

7 lines not shown · raw message

77 lines not shown · raw message

Parts of this message are not shown here. View the raw message.

#8 lore raw
In reply to Xu, Quan
Quoted 196 lines · Quan
Quoted 110 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Wednesday, November 05, 2014 7:01 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 31 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

Quoted 8 lines

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical system.

Each virtual machine gets its own unique, emulated, software TPM.

Quoted 61 lines

Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor.
The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM backend driver. This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

Quoted 8 lines
Quoted 3 lines

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Not so.
Refer to below further explanation.

Quoted 11 lines
Quoted 7 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Yes, it is Patch #3. Agree with your description. :)

Quoted 8 lines
Quoted 6 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM stubdom. Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV

Yes, it patch #2.

Quoted 4 lines

frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

Yes, it's tpmfront implementation in Qemu. also it supports PV Linux since Xen 4.3.0. vTPM stubdom / vtpmmgr stubdom ware implemented by Matthew Fioravante (JHUAPL), Linux pv vTPM frontend was implemented by Daniel De Graaf (NSA)

Quoted 2 lines

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Not so.
Maybe I should explain a bit further. TPM is a character device. 'Qemu tpm1.2 Tis' Implement some TPM register hook of TPM, and enable TIS (TPM interface Specification). The ' Qemu tpm1.2 Tis ' should deal with TPM commands.

Such as TPM_ContinueSelfTest command,

    { 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x53};
If success, the TPM will respond,
    { 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00};

Qemu upstream has supported TPM passthrough. actually, 'Qemu TPM passthrough' send These TPM commands from "Qemu tpm1.2 Tis" to TPM hardware, and get the respond.

" [PATCH 0/4] Qemu-Xen-vTPM: enable Xen stubdom vTPM for HVM virtual machine " send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom( explain in previous email) And get the respond.

#(qemu-system-* --tpmdev help)
#(passthrough Passthrough TPM backend driver)
#( xenstubdoms Xenstubdoms TPM backend driver)

The following is Qemu part.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +------------------------------+
                     |  ^
                     v  |
            +------------------------------+
            |  xen_vtpmdev_ops  |  (new ..)
            +------------------------------+
Quoted 2 lines

"vTPM XenStubdoms driver" and "xen_vtpmdev_ops" are implemented to send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom.

OK, so if I understand correctly in your architecture QEMU is running in Dom0 as usual, right?

Quoted 843 lines
Quoted 40 lines
Quoted 19 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances and logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with vtpmmgr-stubdom. This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

Quoted 5 lines

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is only one vTPM manager and it should be running during the entire lifetime of the machine. vtpmmgr domain securely stores encryption keys for each of the vTPMs and accesses to the hardware TPM to get the root

of

Quoted 6 lines

trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the hardware TPM. Communication is facilitated by mapping hardware memory

pages

Quoted 1 line

into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 3 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

Quoted 9 lines
Quoted 6 lines

===========

Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Welcome, thanks for your careful review.

Quoted 787 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines


 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create mode

100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

Quoted 1 line
Quoted 1 line

hw/xen/Makefile.objs line 3

Quoted 1 line
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) +=

xen_stubdom_vtpm.o

Quoted 42 lines
Quoted 42 lines

hw/xen/Makefile.objs line 5

Quoted 3 lines
hw/xen/Makefile.objs +0 -0
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice · line 194

Quoted 6 lines
hw/xen/xen_backend.c index renamed from hw/xen/xen_backend.c +0 -0

b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice
 *xendev,

enum xenbus_state state)

Quoted 31 lines
hw/xen/xen_backend.c +25 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* -------------------------------------------------------------
 */
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

struct XenDevice *xen_be_find_xendev(const char *type, int dom,
int

dev)

hw/xen/xen_backend.c static struct XenDevice · line 248

Quoted 2 lines
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice
 *xen_be_get_xendev(const

char *type, int dom, int dev,

Quoted 1 line
hw/xen/xen_backend.c +0 -0
                                            struct XenDevOps

*ops)

Quoted 15 lines
Quoted 15 lines

hw/xen/xen_backend.c static struct XenDevice

Quoted 7 lines
hw/xen/xen_backend.c +1 -0
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
hw/xen/xen_backend.c +0 -0
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,
Quoted 7 lines
Quoted 7 lines

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 59 lines
Quoted 59 lines
Quoted 7 lines
hw/xen/xen_backend.c +2 -0
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type,

int

dom, struct XenDevOps *ops)

Quoted 40 lines
     return 0;

}

hw/xen/xen_backend.c +37 -0
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 10 lines
hw/xen/xen_backend.c +4 -1
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)  { @@
-681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

Quoted 2 lines
Quoted 2 lines

hw/xen/xen_backend.c

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type,

dom,

Quoted 68 lines
Quoted 68 lines

(void *)ops);

Quoted 18 lines
hw/xen/xen_backend.c +11 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 833

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 835

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)  {
     return xenstore_scan(type, xen_domid, ops);  }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int
Quoted 8 lines
Quoted 8 lines

remote_dom)

hw/xen/xen_backend.c err: · line 866

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 867

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound

failed\n");

Quoted 54 lines
Quoted 54 lines

hw/xen/xen_backend.c err:

Quoted 3 lines
hw/xen/xen_backend.c +3 -0
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

Quoted 8 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)  {
     if (xendev->local_port != -1) { @@ -770,6 +912,42 @@ int

xen_be_send_notify(struct XenDevice

*xendev)

Quoted 26 lines
     return xc_evtchn_notify(xendev->evtchndev,

xendev->local_port); }

hw/xen/xen_backend.c +23 -0
+int xen_vtpm_send(unsigned char *buf, size_t count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count) {
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines

/*
* msg_level:
* 0 == errors (stderr + logfile).

(quoted diff) +0 -0

b/hw/xen/xen_stubdom_vtpm.c

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

new file mode 100644 index 0000000..0d740c1

hw/xen/xen_stubdom_vtpm.c new file +7 -0
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:
+ *    Quan Xu <[email protected]>
+ *
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 8 lines
hw/xen/xen_stubdom_vtpm.c new file +8 -0
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be
+useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty

of

Quoted 1 line
Quoted 1 line

hw/xen/xen_stubdom_vtpm.c line 16

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See

the

Quoted 32 lines
Quoted 32 lines

GNU

hw/xen/xen_stubdom_vtpm.c line 21

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 3 lines
Quoted 3 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 3 lines
hw/xen/xen_stubdom_vtpm.c new file +3 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM

working */

Quoted 231 lines
Quoted 231 lines
Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,
+};

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets;
+ may be

zero */

hw/xen/xen_stubdom_vtpm.c line 69

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 25 lines
hw/xen/xen_stubdom_vtpm.c new file +25 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev) {
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 29 lines
hw/xen/xen_stubdom_vtpm.c new file +29 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    return -1;
+}
+
+static int vtpm_aio_wait(QEMUBH *qbh) {
+    return aio_poll(qbh->ctx, true); }
+
+static void sr_bh_handler(void *opaque) { }
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf,
+size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 145

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 11 lines
hw/xen/xen_stubdom_vtpm.c new file +11 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf,
+size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev) {
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 210

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 16 lines
hw/xen/xen_stubdom_vtpm.c new file +16 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {
+        goto abort_transaction;

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe,
+ "feature-protocol-v2", 1))

{

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev,
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

hw/xen/xen_stubdom_vtpm.c line 262

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ XenbusStateInitialised);
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
[… more lines in the raw message]

*node)

Quoted 8 lines
Quoted 8 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

&be_state);

Quoted 111 lines
Quoted 111 lines

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 9 lines
hw/xen/xen_stubdom_vtpm.c new file +9 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

XenVtpmDev,

75 lines not shown · raw message

Quoted 7 lines

7 lines not shown · raw message

77 lines not shown · raw message

Parts of this message are not shown here. View the raw message.

author
#9 lore raw
Quoted 223 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Thursday, November 06, 2014 2:24 AM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 33 lines · Quan
Quoted 33 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Wednesday, November 05, 2014 7:01 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 23 lines · Quan
Quoted 22 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 6 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 2 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each

others.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical

system.

Quoted 6 lines
Quoted 6 lines

Each virtual machine gets its own unique, emulated, software TPM.

Quoted 5 lines

Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor. The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM

backend driver.

Quoted 76 lines
Quoted 56 lines
Quoted 56 lines

This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

Quoted 8 lines
Quoted 3 lines

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Not so.
Refer to below further explanation.

Quoted 8 lines
Quoted 7 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

Quoted 10 lines
Quoted 3 lines

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Yes, it is Patch #3. Agree with your description. :)

Quoted 4 lines
Quoted 4 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM

stubdom.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV

Yes, it patch #2.

Quoted 7 lines
Quoted 4 lines

frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

Yes, it's tpmfront implementation in Qemu. also it supports PV Linux since

Xen 4.3.0.

Quoted 1 line

vTPM stubdom / vtpmmgr stubdom ware implemented by Matthew

Fioravante

Quoted 10 lines

(JHUAPL), Linux pv vTPM frontend was implemented by Daniel De Graaf (NSA)

Quoted 2 lines

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Not so.
Maybe I should explain a bit further. TPM is a character device. 'Qemu

tpm1.2 Tis'

Quoted 1 line

Implement some TPM register hook of TPM, and enable TIS (TPM interface

Specification).

Quoted 9 lines

The ' Qemu tpm1.2 Tis ' should deal with TPM commands.

Such as TPM_ContinueSelfTest command,

    { 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x53}; If
success, the TPM will respond,
    { 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00};

Qemu upstream has supported TPM passthrough. actually, 'Qemu TPM passthrough' send These TPM commands from "Qemu tpm1.2 Tis" to TPM

hardware, and get the respond.

Quoted 2 lines

" [PATCH 0/4] Qemu-Xen-vTPM: enable Xen stubdom vTPM for HVM

virtual

Quoted 19 lines

machine " send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom( explain in previous email) And get the respond.

#(qemu-system-* --tpmdev help)
#(passthrough Passthrough TPM backend driver)
#( xenstubdoms Xenstubdoms TPM backend driver)

The following is Qemu part.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +------------------------------+
                     |  ^
                     v  |
            +------------------------------+
            |  xen_vtpmdev_ops  |  (new ..)
            +------------------------------+ "vTPM XenStubdoms driver"
Quoted 1 line

and "xen_vtpmdev_ops" are implemented to send These TPM commands

Quoted 5 lines

from

Quoted 1 line

"Qemu tpm1.2 Tis" to Xen stubdom.

OK, so if I understand correctly in your architecture QEMU is running in Dom0 as usual, right?

Yes,

Quoted 884 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances

and

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with

vtpmmgr-stubdom.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is

only

Quoted 1 line
Quoted 1 line
Quoted 1 line

one vTPM manager and it should be running during the entire

lifetime

Quoted 1 line
Quoted 1 line
Quoted 1 line

of the machine. vtpmmgr domain securely stores encryption keys

for

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

each of the vTPMs and accesses to the hardware TPM to get the
root

of

Quoted 5 lines

trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the

hardware

Quoted 14 lines
Quoted 14 lines
Quoted 1 line

TPM. Communication is facilitated by mapping hardware memory

pages

Quoted 1 line

into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 7 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

===========

Quoted 16 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Welcome, thanks for your careful review.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines


 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182

++++++++++++++++++++++-

Quoted 62 lines
Quoted 62 lines
Quoted 17 lines
Quoted 17 lines
Quoted 1 line
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

Quoted 12 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create

mode
100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs line 2

Quoted 2 lines
hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

Quoted 1 line
Quoted 1 line

hw/xen/Makefile.objs line 3

Quoted 1 line
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) +=

xen_stubdom_vtpm.o

Quoted 43 lines
Quoted 43 lines

hw/xen/Makefile.objs line 5

Quoted 3 lines
hw/xen/Makefile.objs +0 -0
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice · line 194

Quoted 6 lines
hw/xen/xen_backend.c index renamed from hw/xen/xen_backend.c +0 -0

b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice
 *xendev,

enum xenbus_state state)

Quoted 32 lines
hw/xen/xen_backend.c +25 -1
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /*
-------------------------------------------------------------

*/

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

struct XenDevice *xen_be_find_xendev(const char *type, int
dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice · line 248

Quoted 2 lines
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice
 *xen_be_get_xendev(const

char *type, int dom, int dev,

Quoted 1 line
hw/xen/xen_backend.c +0 -0
                                            struct

XenDevOps

Quoted 5 lines
Quoted 5 lines

*ops)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
hw/xen/xen_backend.c +1 -0
 {
     struct XenDevice *xendev;
+    char *stub;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_backend.c static struct XenDevice

Quoted 3 lines
hw/xen/xen_backend.c +0 -0
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
hw/xen/xen_backend.c +0 -0
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 4 lines
hw/xen/xen_backend.c +0 -1
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 3 lines
hw/xen/xen_backend.c +2 -1
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+                 stub, xendev->type, xendev->dom,

xendev->dev);

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 3 lines
hw/xen/xen_backend.c +3 -0
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 11 lines
Quoted 11 lines

hw/xen/xen_backend.c static int xenstore_scan(const char · line 645

Quoted 7 lines
hw/xen/xen_backend.c +2 -0
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char
 *type, int

dom, struct XenDevOps *ops)

Quoted 3 lines
hw/xen/xen_backend.c +0 -0
     return 0;
 }
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type,

dom);

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
hw/xen/xen_backend.c +22 -0
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops) · line 690

Quoted 8 lines
hw/xen/xen_backend.c +6 -0
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)

{ @@

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

-681,6 +756,10 @@ static void xenstore_update(void *unused)

     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1)

{

659 lines not shown · raw message

Part of this message is not shown here. View the raw message.

author
#10 lore raw
In reply to Stefano Stabellini
Quoted 223 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Thursday, November 06, 2014 2:24 AM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 33 lines · Quan
Quoted 33 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Wednesday, November 05, 2014 7:01 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 23 lines · Quan
Quoted 22 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 6 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Thanks Stefano.
one more day, I will explain in detail what changes did make to xen_backend.c and why.
The following 2 sections are introduction and architecture.

Quoted 2 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each

others.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

Add 2 parts for detailed descriptions, introduction and architecture.

*INTRODUCTION*

The goal of virtual Trusted Platform Module (vTPM) is to provide a TPM functionality to virtual machines (Fedora, Ubuntu, Redhat, Windows .etc). This allows programs to interact with a TPM in a virtual machine the same way they interact with a TPM on the physical

system.

Quoted 6 lines
Quoted 6 lines

Each virtual machine gets its own unique, emulated, software TPM.

Quoted 5 lines

Each major component of vTPM is implemented as a stubdom, providing secure separation guaranteed by the hypervisor. The vTPM stubdom is a Xen mini-OS domain that emulates a TPM for the virtual machine to use. It is a small wrapper around the Berlios TPM emulator. TPM commands are passed from mini-os TPM

backend driver.

Quoted 76 lines
Quoted 56 lines
Quoted 56 lines

This patch series are only the Qemu part to enable Xen stubdom vTPM for HVM virtual machine.
===========

*ARCHITECTURE*

The architecture of stubdom vTPM for HVM virtual machine:

            +--------------------+
            | Windows/Linux DomU | ...
            |        |  ^        |
            |        v  |        |
            |  Qemu tpm1.2 Tis   |
            |        |  ^        |
            |        v  |        |
            |        vTPM        |
            | XenStubdoms driver |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  xen_vtpmdev_ops   |  (new ..)
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |   vTPM stubdom     | ...
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpmfront  |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |  mini-os/tpmback   |
            |        |  ^        |
            |        v  |        |
            |  vtpmmgr stubdom   |
            |        |  ^        |
            |        v  |        |
            |  mini-os/tpm_tis   |
            +--------------------+
                     |  ^
                     v  |
            +--------------------+
            |    Hardware TPM    |
            +--------------------+

* Windows/Linux DomU:
The HVM based guest that wants to use a vTPM. There may be more than one of these.

Quoted 8 lines
Quoted 3 lines

* Qemu tpm1.2 Tis:
Implementation of the tpm1.2 Tis interface for HVM virtual machines. It is Qemu emulation device.

It looks like you are running upstream QEMU within a Mini-OS stubdom? If so, where are the patches to do that? As far as I know upstream QEMU doesn't run on Mini-OS. Or maybe it is a Linux stubdom? Either way, it is not clear.

Not so.
Refer to below further explanation.

Quoted 8 lines
Quoted 7 lines

* vTPM xenstubdoms driver:
Similar to a TPM passthrough backend driver, it is a new TPM backend for emulated TPM TIS interface. This driver provides vTPM initialization and sending data and commends to a Xen vTPM stubdom.

Quoted 10 lines
Quoted 3 lines

This is patch #3, right? It is basically the internal QEMU "glue" to connect the tpm emulator to xen_vtpmdev_ops, correct?

Yes, it is Patch #3. Agree with your description. :)

Quoted 4 lines
Quoted 4 lines

* xen_vtpmdev_ops:
Register Xen stubdom vTPM backend, and transfer any request/ repond between TPM xenstubdoms driver and Xen vTPM

stubdom.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Facilitate communications between Xen vTPM stubdom and vTPM xenstubdoms driver.

It looks like this correspond to patch #2? If so, this is a regular Xen PV

Yes, it patch #2.

Quoted 7 lines
Quoted 4 lines

frontend, living in QEMU? It requires the gntalloc device so I guess it is supposed to run on Linux. In that case is it just a tpmfront implementation in QEMU?

Yes, it's tpmfront implementation in Qemu. also it supports PV Linux since

Xen 4.3.0.

Quoted 1 line

vTPM stubdom / vtpmmgr stubdom ware implemented by Matthew

Fioravante

Quoted 10 lines

(JHUAPL), Linux pv vTPM frontend was implemented by Daniel De Graaf (NSA)

Quoted 2 lines

If you are running it on Linux, we might need to introduce Linux based stundoms to the Xen build system.

Not so.
Maybe I should explain a bit further. TPM is a character device. 'Qemu

tpm1.2 Tis'

Quoted 1 line

Implement some TPM register hook of TPM, and enable TIS (TPM interface

Specification).

Quoted 9 lines

The ' Qemu tpm1.2 Tis ' should deal with TPM commands.

Such as TPM_ContinueSelfTest command,

    { 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x53}; If
success, the TPM will respond,
    { 0x00, 0xc4, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00};

Qemu upstream has supported TPM passthrough. actually, 'Qemu TPM passthrough' send These TPM commands from "Qemu tpm1.2 Tis" to TPM

hardware, and get the respond.

Quoted 2 lines

" [PATCH 0/4] Qemu-Xen-vTPM: enable Xen stubdom vTPM for HVM

virtual

Quoted 19 lines

machine " send These TPM commands from "Qemu tpm1.2 Tis" to Xen stubdom( explain in previous email) And get the respond.

#(qemu-system-* --tpmdev help)
#(passthrough Passthrough TPM backend driver)
#( xenstubdoms Xenstubdoms TPM backend driver)

The following is Qemu part.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +------------------------------+
                     |  ^
                     v  |
            +------------------------------+
            |  xen_vtpmdev_ops  |  (new ..)
            +------------------------------+ "vTPM XenStubdoms driver"
Quoted 1 line

and "xen_vtpmdev_ops" are implemented to send These TPM commands

Quoted 5 lines

from

Quoted 1 line

"Qemu tpm1.2 Tis" to Xen stubdom.

OK, so if I understand correctly in your architecture QEMU is running in Dom0 as usual, right?

Yes,

Quoted 884 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

* mini-os/tpmback:
Mini-os TPM backend driver. The Linux frontend driver connects to this backend driver to facilitate communications between the Linux DomU and its vTPM. This driver is also used by vtpmmgr stubdom to communicate with vTPM stubdom.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

* vTPM stubdom:
A mini-os stub domain that implements a vTPM. There is a one to one mapping between running vTPM stubdom instances

and

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

logical vTPMs on the system. The vTPM Platform Configuration Registers (PCRs) are all initialized to zero.

* mini-os/tpmfront:
Mini-os TPM frontend driver. The vTPM mini-os domain vTPM stubdom uses this driver to communicate with

vtpmmgr-stubdom.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

This driver could also be used separately to implement a mini-os domain that wishes to use a vTPM of its own.

* vtpmmgr stubdom:
A mini-os domain that implements the vTPM manager. There is

only

Quoted 1 line
Quoted 1 line
Quoted 1 line

one vTPM manager and it should be running during the entire

lifetime

Quoted 1 line
Quoted 1 line
Quoted 1 line

of the machine. vtpmmgr domain securely stores encryption keys

for

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

each of the vTPMs and accesses to the hardware TPM to get the
root

of

Quoted 5 lines

trust for the entire system.

* mini-os/tpm_tis:
Mini-os TPM version 1.2 TPM Interface Specification (TIS) driver. This driver used by vtpmmgr-stubdom to talk directly to the

hardware

Quoted 14 lines
Quoted 14 lines
Quoted 1 line

TPM. Communication is facilitated by mapping hardware memory

pages

Quoted 1 line

into vtpmmgr stubdom.

Are all the Mini-OS component upstream, or do we need patches?

Quoted 7 lines

* Hardware TPM:
The physical TPM 1.2 that is soldered onto the motherboard.

===========

Quoted 16 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

Where does the vTPM backend lives?

The vTPM backend lives in Xen vTPM stubdom (Xen Mini-os)

Thanks for the explanation, it is a bit clearer now.

Welcome, thanks for your careful review.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines


 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182

++++++++++++++++++++++-

Quoted 62 lines
Quoted 62 lines
Quoted 17 lines
Quoted 17 lines
Quoted 1 line
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

Quoted 12 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)  create

mode
100644 hw/xen/xen_stubdom_vtpm.c

hw/xen/Makefile.objs index renamed from hw/xen/Makefile.objs +0 -0

a0ca0aa..724df8d 100644

hw/xen/Makefile.objs line 2

Quoted 2 lines
hw/xen/Makefile.objs +0 -0
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

Quoted 1 line
Quoted 1 line

hw/xen/Makefile.objs line 3

Quoted 1 line
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) +=

xen_stubdom_vtpm.o

Quoted 43 lines
Quoted 43 lines

hw/xen/Makefile.objs line 5

Quoted 3 lines
hw/xen/Makefile.objs +0 -0
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice · line 194

Quoted 6 lines
hw/xen/xen_backend.c index renamed from hw/xen/xen_backend.c +0 -0

b2cb22b..45a5778 100644

hw/xen/xen_backend.c +0 -0
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice
 *xendev,

enum xenbus_state state)

Quoted 32 lines
hw/xen/xen_backend.c +25 -1
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev) {
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /*
-------------------------------------------------------------

*/

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

struct XenDevice *xen_be_find_xendev(const char *type, int
dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice · line 248

Quoted 2 lines
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice
 *xen_be_get_xendev(const

char *type, int dom, int dev,

Quoted 1 line
hw/xen/xen_backend.c +0 -0
                                            struct

XenDevOps

Quoted 5 lines
Quoted 5 lines

*ops)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
hw/xen/xen_backend.c +1 -0
 {
     struct XenDevice *xendev;
+    char *stub;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_backend.c static struct XenDevice

Quoted 3 lines
hw/xen/xen_backend.c +0 -0
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
hw/xen/xen_backend.c +0 -0
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 4 lines
hw/xen/xen_backend.c +0 -1
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 3 lines
hw/xen/xen_backend.c +2 -1
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+                 stub, xendev->type, xendev->dom,

xendev->dev);

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 3 lines
hw/xen/xen_backend.c +3 -0
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be),

"backend/%s/%d/%d",

Quoted 11 lines
Quoted 11 lines

hw/xen/xen_backend.c static int xenstore_scan(const char · line 645

Quoted 7 lines
hw/xen/xen_backend.c +2 -0
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char
 *type, int

dom, struct XenDevOps *ops)

Quoted 3 lines
hw/xen/xen_backend.c +0 -0
     return 0;
 }
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
hw/xen/xen_backend.c +4 -0
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops) {
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type,

dom);

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
hw/xen/xen_backend.c +22 -0
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops) · line 690

Quoted 8 lines
hw/xen/xen_backend.c +6 -0
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)

{ @@

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

-681,6 +756,10 @@ static void xenstore_update(void *unused)

     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1)

{

659 lines not shown · raw message

Part of this message is not shown here. View the raw message.

author
#11 lore raw
In reply to Stefano Stabellini
Quoted 16 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops);
+ xen_be_register("qdisk", &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag in include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

In previous email, I have explained what is "Xen vTPM stubdom", what is the " vTPM xenstubdoms driver ".

Let me describe how the communicate with each others.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +-----------------------------+
                     |  ^
                     v  |
            +-----------------------------+
            |  xen_vtpmdev_ops |  (new ..)
            +-----------------------------+

xen_vtpmdev_ops is initialized with the following process:
xen_hvm_init()

    [...]
    -->xen_vtpm_register()
      [...]
        --> vtpm_alloc()
        --> vtpm_initialise()

##
vTPM XenStubdoms driver is initialized by Qemu command line options,
"-tpmdev xenstubdoms,id=xenvtpm0 -device tpm-tis,tpmdev=xenvtpm0"

(the communicate with each others in following function.
.. hw/tpm/tpm_xenstubdoms.c
static int tpm_xenstubdoms_unix_transfer(const TPMLocality *locty_data) {
[...]

    xen_vtpm_send(locty_data->w_buffer.buffer, locty_data->w_offset);
    xen_vtpm_recv(locty_data->r_buffer.buffer, &rlen);

[...]
}

Quoted 1 line

Where does the vTPM backend lives?

Xen stubDom.

Quoted 204 lines
Quoted 5 lines

 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +0 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines
hw/xen/xen_backend.c +0 -0
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 22 lines
hw/xen/xen_backend.c +19 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const

Quoted 1 line
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const

char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +1 -0
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 48 lines
hw/xen/xen_backend.c +41 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 759

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 761

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 19 lines
hw/xen/xen_backend.c +12 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 834

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 836

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

Quoted 11 lines

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

1. setup watch
2. look for backends
3. initialize the frontend

Quoted 62 lines

hw/xen/xen_backend.c err:

Quoted 7 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom)

hw/xen/xen_backend.c err: · line 869

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 870

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 9 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

Quoted 33 lines
hw/xen/xen_backend.c +30 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
Quoted 9 lines

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

OK, I will move it to hw/tpm.

Quoted 476 lines
Quoted 15 lines
hw/xen/xen_backend.c +0 -0
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +5 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

hw/xen/xen_stubdom_vtpm.c

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 66

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 26 lines
hw/xen/xen_stubdom_vtpm.c new file +26 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 32 lines
hw/xen/xen_stubdom_vtpm.c new file +32 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 146

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 211

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char

*node)

hw/xen/xen_stubdom_vtpm.c line 265

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

include/hw/xen/xen_backend.h struct XenDevice; · line 16

Quoted 6 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines
include/hw/xen/xen_backend.h +3 -0
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 95

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 106

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 108

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 109

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 110

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+extern struct XenDevOps xen_vtpmdev_ops;      /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_init_display(int domid);
 
(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 17 lines
include/hw/xen/xen_common.h +6 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +0 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 12 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1174

Quoted 1 line
xen-hvm.c +0 -0
         fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +3 -0
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Refer to above explanation.

Thanks
Quan

Quoted 8 lines
Quoted 8 lines
xen-hvm.c +0 -1
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
--

1.8.3.2

author
#12 lore raw
In reply to Stefano Stabellini
Quoted 16 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops);
+ xen_be_register("qdisk", &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag in include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Quoted 3 lines

Please also describe what is the "Xen vTPM stubdom", what is the "vTPM xenstubdoms driver" and how the communicate with each others.

In previous email, I have explained what is "Xen vTPM stubdom", what is the " vTPM xenstubdoms driver ".

Let me describe how the communicate with each others.

            |        |  ^       |
            |        v  |       |
            |        vTPM      |
            | XenStubdoms driver |  (new ..)
            +-----------------------------+
                     |  ^
                     v  |
            +-----------------------------+
            |  xen_vtpmdev_ops |  (new ..)
            +-----------------------------+

xen_vtpmdev_ops is initialized with the following process:
xen_hvm_init()

    [...]
    -->xen_vtpm_register()
      [...]
        --> vtpm_alloc()
        --> vtpm_initialise()

##
vTPM XenStubdoms driver is initialized by Qemu command line options,
"-tpmdev xenstubdoms,id=xenvtpm0 -device tpm-tis,tpmdev=xenvtpm0"

(the communicate with each others in following function.
.. hw/tpm/tpm_xenstubdoms.c
static int tpm_xenstubdoms_unix_transfer(const TPMLocality *locty_data) {
[...]

    xen_vtpm_send(locty_data->w_buffer.buffer, locty_data->w_offset);
    xen_vtpm_recv(locty_data->r_buffer.buffer, &rlen);

[...]
}

Quoted 1 line

Where does the vTPM backend lives?

Xen stubDom.

Quoted 204 lines
Quoted 5 lines

 hw/xen/Makefile.objs         |   1 +
 hw/xen/xen_backend.c         | 182 ++++++++++++++++++++++-
 hw/xen/xen_stubdom_vtpm.c    | 333

+++++++++++++++++++++++++++++++++++++++++++

hw/xen/Makefile.objs line 2

Quoted 13 lines
 include/hw/xen/xen_backend.h |  11 ++
 include/hw/xen/xen_common.h  |   6 +
 xen-hvm.c                    |  13 ++
 6 files changed, 544 insertions(+), 2 deletions(-)
 create mode 100644 hw/xen/xen_stubdom_vtpm.c
hw/xen/Makefile.objs +0 -0
index a0ca0aa..724df8d 100644
@@ -1,5 +1,6 @@
 # xen backend driver support
 common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o

xen_devconfig.o

hw/xen/Makefile.objs

Quoted 4 lines
hw/xen/Makefile.objs +1 -0
+common-obj-$(CONFIG_TPM_XENSTUBDOMS) += xen_stubdom_vtpm.o
 
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o

xen_pt_config_init.o xen_pt_msi.o

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev,

Quoted 5 lines
hw/xen/xen_backend.c +0 -0
index b2cb22b..45a5778 100644
@@ -194,6 +194,32 @@ int xen_be_set_state(struct XenDevice *xendev,

enum xenbus_state state)

Quoted 22 lines
hw/xen/xen_backend.c +19 -0
     return 0;
 }
 
+/*get stubdom backend*/
+static char *xen_stubdom_be(const char *type, int dom, int dev)
+{
+    char *val, *domu;
+    char path[XEN_BUFSIZE];
+    unsigned int len, ival;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, dom);
+    snprintf(path, sizeof(path), "%s/device/%s/%d/backend-id",
+             domu, type, dev);
+    g_free(domu);
+
+    val = xs_read(xenstore, 0, path, &len);
+    if (!val || 1 != sscanf(val, "%d", &ival)) {
+        g_free(val);
+        return NULL;
+    }
+    g_free(val);

hw/xen/xen_backend.c int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)

Quoted 10 lines
hw/xen/xen_backend.c +7 -0
+
+    /*backend domu*/
+    domu = xs_get_domain_path(xenstore, ival);
+
+    return domu;
+}
+
 /* ------------------------------------------------------------- */
 
 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int

dev)

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const

Quoted 1 line
hw/xen/xen_backend.c +0 -0
@@ -222,6 +248,7 @@ static struct XenDevice *xen_be_get_xendev(const

char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice

Quoted 8 lines
hw/xen/xen_backend.c +1 -0
                                            struct XenDevOps *ops)
 {
     struct XenDevice *xendev;
+    char *stub;
 
     xendev = xen_be_find_xendev(type, dom, dev);
     if (xendev) {
@@ -235,8 +262,15 @@ static struct XenDevice

*xen_be_get_xendev(const char *type, int dom, int dev,

hw/xen/xen_backend.c static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,

Quoted 7 lines
hw/xen/xen_backend.c +2 -2
     xendev->dev   = dev;
     xendev->ops   = ops;
 
-    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
-             xendev->type, xendev->dom, xendev->dev);
+    if (ops->flags & DEVOPS_FLAG_STUBDOM_BE) {
+        stub = xen_stubdom_be(xendev->type, xendev->dom,

xendev->dev);

hw/xen/xen_backend.c static struct XenDevice · line 267

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+        snprintf(xendev->be, sizeof(xendev->be),

"%s/backend/%s/%d/%d",

hw/xen/xen_backend.c static int xenstore_scan(const char *type, int

Quoted 10 lines
hw/xen/xen_backend.c +6 -0
+                 stub, xendev->type, xendev->dom, xendev->dev);
+        g_free(stub);
+    } else {
+        snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
+                 xendev->type, xendev->dom, xendev->dev);
+    }
     snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
              xendev->type, xendev->dev);
 
@@ -611,6 +645,47 @@ static int xenstore_scan(const char *type, int

dom, struct XenDevOps *ops)

Quoted 48 lines
hw/xen/xen_backend.c +41 -0
     return 0;
 }
 
+static void stubdom_update_be(char *watch, char *type, int dom,
+                              struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE];
+    char *ptr, *bepath;
+    unsigned int len, dev;
+
+    if (!(ops->flags & DEVOPS_FLAG_STUBDOM_BE)) {
+        return;
+    }
+
+    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
+    ptr = strstr(watch, path);
+    if (ptr == NULL) {
+        return;
+    }
+
+    if (sscanf(ptr+len, "/%u/%255s", &dev, path) != 2) {
+        strcpy(path, "");
+        if (sscanf(ptr+len, "/%u", &dev) != 1) {
+            dev = -1;
+        }
+    }
+
+    if (dev == -1) {
+        return;
+    }
+
+    xendev = xen_be_get_xendev(type, dom, dev, ops);
+    if (xendev != NULL) {
+        bepath = xs_read(xenstore, 0, xendev->be, &len);
+        if (bepath == NULL) {
+            xen_be_del_xendev(dom, dev);
+        } else {
+            free(bepath);
+            xen_be_backend_changed(xendev, path);
+        }
+    }
+}
+
 static void xenstore_update_be(char *watch, char *type, int dom,
                                struct XenDevOps *ops)
 {

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 759

Quoted 4 lines
hw/xen/xen_backend.c +1 -0
@@ -681,6 +756,10 @@ static void xenstore_update(void *unused)
     if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
         xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
     }
+    if (sscanf(vec[XS_WATCH_TOKEN], "stub:%" PRIxPTR ":%d:%"

PRIxPTR,

hw/xen/xen_backend.c static void xenstore_update(void *unused) · line 761

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+               &type, &dom, &ops) == 3) {
+        stubdom_update_be(vec[XS_WATCH_PATH], (void *)type, dom,

(void *)ops);

Quoted 19 lines
hw/xen/xen_backend.c +12 -0
+    }
 
 cleanup:
     free(vec);
@@ -732,11 +811,74 @@ err:
     return -1;
 }
 
+int xen_vtpm_register(struct XenDevOps *ops)
+{
+    struct XenDevice *xendev;
+    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
+    char *domu;
+    unsigned int cdev, j, rc;
+    const char *type = "vtpm";
+    char **dev = NULL;
+
+    /*front domu*/
+    domu = xs_get_domain_path(xenstore, xen_domid);

hw/xen/xen_backend.c err: · line 834

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+    snprintf(path, sizeof(path), "%s/device/%s",
+             domu, type);
+    free(domu);
+    dev = xs_directory(xenstore, 0, path, &cdev);
+    if (dev == NULL) {
+        return 0;
+    }
+
+    for (j = 0; j < cdev; j++) {
+        xendev = xen_be_get_xendev(type, xen_domid, atoi(dev[j]),

ops);

hw/xen/xen_backend.c err: · line 836

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+        if (xendev == NULL) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xendev is

NULL.\n");

Quoted 8 lines
hw/xen/xen_backend.c +8 -0
+            continue;
+        }
+
+        if (xendev->ops->initialise) {
+            rc = xendev->ops->initialise(xendev);
+
+            /*if initialise failed, delete it*/
+            if (rc != 0) {

hw/xen/xen_backend.c err:

Quoted 10 lines
hw/xen/xen_backend.c +10 -0
+                xen_be_del_xendev(xen_domid, atoi(dev[j]));
+                continue;
+            }
+        }
+
+        /*setup watch*/
+        snprintf(token, sizeof(token), "stub:%p:%d:%p",
+                 type, xen_domid, xendev->ops);
+        if (!xs_watch(xenstore, xendev->be, token)) {
+            xen_be_printf(xendev, 0, "xen_vtpm_register xs_watch

failed.\n");

Quoted 11 lines

hw/xen/xen_backend.c err: · line 861

Quoted 7 lines
hw/xen/xen_backend.c +7 -0
+            return -1;
+        }
+    }
+
+    free(dev);
+    return 0;
+}

What does this function do? I sholdn't need to guess from the code, I should be able to tell from the patch description.

1. setup watch
2. look for backends
3. initialize the frontend

Quoted 62 lines

hw/xen/xen_backend.c err:

Quoted 7 lines
hw/xen/xen_backend.c +1 -0
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
     return xenstore_scan(type, xen_domid, ops);
 }
 
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom)

hw/xen/xen_backend.c err: · line 869

Quoted 2 lines
hw/xen/xen_backend.c +2 -0
+{
+    xendev->local_port =

xc_evtchn_bind_unbound_port(xendev->evtchndev,

hw/xen/xen_backend.c err: · line 870

Quoted 1 line
hw/xen/xen_backend.c +1 -0
+

remote_dom);

hw/xen/xen_backend.c err:

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+    if (xendev->local_port == -1) {
+        xen_be_printf(xendev, 0, "xc_evtchn_alloc_unbound failed\n");
+        return -1;
+    }
+    xen_be_printf(xendev, 2, "bind evtchn port %d\n",

xendev->local_port);

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice

Quoted 9 lines
hw/xen/xen_backend.c +5 -0
+    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+                        xen_be_evtchn_event, NULL, xendev);
+    return 0;
+}
+
 int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1) {
@@ -770,6 +912,42 @@ int xen_be_send_notify(struct XenDevice

*xendev)

Quoted 33 lines
hw/xen/xen_backend.c +30 -0
     return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
+int xen_vtpm_send(unsigned char *buf, size_t count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->send) {
+        rc = xendev->ops->send(xendev, buf, count);
+    }
+
+    return rc;
+}
+
+int xen_vtpm_recv(unsigned char *buf, size_t *count)
+{
+    struct XenDevice *xendev;
+    int rc = -1;
+
+    xendev = xen_be_find_xendev("vtpm", xen_domid, 0);
+    if (xendev == NULL) {
+        xen_be_printf(xendev, 0, "Con not find vtpm device\n");
+        return -1;
+    }
+
+    if (xendev->ops->recv) {
Quoted 9 lines

hw/xen/xen_backend.c int xen_be_send_notify(struct XenDevice *xendev) · line 949

Quoted 5 lines
hw/xen/xen_backend.c +5 -0
+        xendev->ops->recv(xendev, buf, count);
+    }
+
+    return rc;
+}

xen_backend.c is supposed to be generic, so stubdom functions might be OK but vtpm specific functions should not be here.

OK, I will move it to hw/tpm.

Quoted 476 lines
Quoted 15 lines
hw/xen/xen_backend.c +0 -0
 /*
  * msg_level:
  *  0 == errors (stderr + logfile).
hw/xen/xen_stubdom_vtpm.c new file +5 -0
new file mode 100644
index 0000000..0d740c1
@@ -0,0 +1,333 @@
+/*
+ * Connect to Xen vTPM stubdom domain
+ *
+ *  Copyright (c) 2014 Intel Corporation
+ *  Authors:

hw/xen/xen_stubdom_vtpm.c line 15

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+ *    Quan Xu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU

hw/xen/xen_stubdom_vtpm.c

Quoted 4 lines
hw/xen/xen_stubdom_vtpm.c new file +4 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see

<http://www.gnu.org/licenses/>

Quoted 14 lines
hw/xen/xen_stubdom_vtpm.c new file +14 -0
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "hw/hw.h"
+#include "block/aio.h"
+#include "hw/xen/xen_backend.h"
+
+enum tpmif_state {
+    TPMIF_STATE_IDLE,        /* no contents / vTPM idle / cancel

complete */

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    TPMIF_STATE_SUBMIT,      /* request ready / vTPM working */
+    TPMIF_STATE_FINISH,      /* response ready / vTPM idle */
+    TPMIF_STATE_CANCEL,      /* cancel requested / vTPM working */
+};
+
+static AioContext *vtpm_aio_ctx;
+
+enum status_bits {
+    VTPM_STATUS_RUNNING  = 0x1,
+    VTPM_STATUS_IDLE     = 0x2,
+    VTPM_STATUS_RESULT   = 0x4,
+    VTPM_STATUS_CANCELED = 0x8,

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+};
+
+struct tpmif_shared_page {
+    uint32_t length;         /* request/response length in bytes */
+
+    uint8_t  state;           /* enum tpmif_state */
+    uint8_t  locality;        /* for the current request */
+    uint8_t  pad;             /* should be zero */
+
+    uint8_t  nr_extra_pages;  /* extra pages for long packets; may be

zero */

hw/xen/xen_stubdom_vtpm.c line 66

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+    uint32_t extra_pages[0]; /* grant IDs; length is actually

nr_extra_pages */

Quoted 26 lines
hw/xen/xen_stubdom_vtpm.c new file +26 -0
+};
+
+struct QEMUBH {
+    AioContext *ctx;
+    QEMUBHFunc *cb;
+    void *opaque;
+    QEMUBH *next;
+    bool scheduled;
+    bool idle;
+    bool deleted;
+};
+
+struct XenVtpmDev {
+    struct XenDevice xendev;  /* must be first */
+    struct           tpmif_shared_page *shr;
+    xc_gntshr        *xen_xcs;
+    int              ring_ref;
+    int              bedomid;
+    QEMUBH           *sr_bh;
+};
+
+static uint8_t vtpm_status(struct XenVtpmDev *vtpmdev)
+{
+    switch (vtpmdev->shr->state) {
+    case TPMIF_STATE_IDLE:
+    case TPMIF_STATE_FINISH:

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        return VTPM_STATUS_IDLE;
+    case TPMIF_STATE_SUBMIT:
+    case TPMIF_STATE_CANCEL:
+        return VTPM_STATUS_RUNNING;
+    default:
+        return 0;
+    }
+}
+
+static int xenbus_switch_state(struct XenDevice *xendev, enum

xenbus_state xbus)

Quoted 32 lines
hw/xen/xen_stubdom_vtpm.c new file +32 -0
+{
+    xs_transaction_t xbt = XBT_NULL;
+
+    if (xendev->fe_state == xbus) {
+        return 0;
+    }
+
+    xendev->fe_state = xbus;
+
+retry_transaction:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(xendev->fe, "state", xbus)) {
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto retry_transaction;
+        }
+    }
+
+    return 0;
+
+abort_transaction:
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+static int vtpm_aio_wait(QEMUBH *qbh)
+{
+    return aio_poll(qbh->ctx, true);
+}
+
+static void sr_bh_handler(void *opaque)
+{
+}
+
+static int vtpm_recv(struct XenDevice *xendev, uint8_t* buf, size_t

*count)

hw/xen/xen_stubdom_vtpm.c line 146

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset;
+
+    if (shr->state == TPMIF_STATE_IDLE) {
+        return -ECANCELED;
+    }
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    }
+
+    offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+    memcpy(buf, offset + (uint8_t *)shr, shr->length);
+    *count = shr->length;
+
+    return 0;
+}
+
+static int vtpm_send(struct XenDevice *xendev, uint8_t* buf, size_t

count)

hw/xen/xen_stubdom_vtpm.c line 168

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+                                              xendev);
+    struct tpmif_shared_page *shr = vtpmdev->shr;
+    unsigned int offset = sizeof(*shr) + 4*shr->nr_extra_pages;
+
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    memcpy(offset + (uint8_t *)shr, buf, count);
+    shr->length = count;
+    barrier();
+    shr->state = TPMIF_STATE_SUBMIT;
+    xen_wmb();
+    xen_be_send_notify(&vtpmdev->xendev);
+

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    while (vtpm_status(vtpmdev) != VTPM_STATUS_IDLE) {
+        vtpm_aio_wait(vtpmdev->sr_bh);
+    }
+
+    return count;
+}
+
+static int vtpm_initialise(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    xs_transaction_t xbt = XBT_NULL;
+    unsigned int ring_ref;
+
+    vtpmdev->xendev.fe = xenstore_read_be_str(&vtpmdev->xendev,

"frontend");

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+    if (vtpmdev->xendev.fe == NULL) {
+        return -1;
+    }
+
+    /* Get backend domid */
+    if (xenstore_read_fe_int(&vtpmdev->xendev, "backend-id",
+                             &vtpmdev->bedomid)) {
+        return -1;
+    }
+
+    /*alloc share page*/
+    vtpmdev->shr = xc_gntshr_share_pages(vtpmdev->xen_xcs,

vtpmdev->bedomid, 1,

hw/xen/xen_stubdom_vtpm.c line 211

Quoted 1 line
hw/xen/xen_stubdom_vtpm.c new file +1 -0
+                                         &ring_ref,

PROT_READ|PROT_WRITE);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    vtpmdev->ring_ref = ring_ref;
+    if (vtpmdev->shr == NULL) {
+        return -1;
+    }
+
+    /*Create event channel */
+    if (xen_be_alloc_unbound(&vtpmdev->xendev, 0,

vtpmdev->bedomid)) {

Quoted 15 lines
hw/xen/xen_stubdom_vtpm.c new file +15 -0
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+        return -1;
+    }
+
+    xc_evtchn_unmask(vtpmdev->xendev.evtchndev,
+                     vtpmdev->xendev.local_port);
+
+again:
+    xbt = xs_transaction_start(xenstore);
+    if (xbt == XBT_NULL) {
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "ring-ref",
+                           vtpmdev->ring_ref)) {

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (xenstore_write_int(vtpmdev->xendev.fe, "event-channel",
+                           vtpmdev->xendev.local_port)) {
+        goto abort_transaction;
+    }
+
+    /* Publish protocol v2 feature */
+    if (xenstore_write_int(vtpmdev->xendev.fe, "feature-protocol-v2", 1))

{

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+        goto abort_transaction;
+    }
+
+    if (!xs_transaction_end(xenstore, xbt, 0)) {
+        if (errno == EAGAIN) {
+            goto again;
+        }
+    }
+    /* Tell vtpm backend that we are ready */
+    xenbus_switch_state(&vtpmdev->xendev, XenbusStateInitialised);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+
+    return 0;
+
+abort_transaction:
+    xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    xs_transaction_end(xenstore, xbt, 1);
+    return -1;
+}
+
+static void vtpm_backend_changed(struct XenDevice *xendev, const char

*node)

hw/xen/xen_stubdom_vtpm.c line 265

Quoted 2 lines
hw/xen/xen_stubdom_vtpm.c new file +2 -0
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 12 lines
hw/xen/xen_stubdom_vtpm.c new file +12 -0
+                                              xendev);
+    int be_state;
+
+    if (strcmp(node, "state") == 0) {
+        xenstore_read_be_int(&vtpmdev->xendev, node, &be_state);
+        switch (be_state) {
+        case XenbusStateConnected:
+            /*TODO*/
+            break;
+        case XenbusStateClosing:
+        case XenbusStateClosed:
+            xenbus_switch_state(&vtpmdev->xendev,

XenbusStateClosing);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static int vtpm_free(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 5 lines
hw/xen/xen_stubdom_vtpm.c new file +5 -0
+                                              xendev);
+    QEMUBH *qbh = vtpmdev->sr_bh;
+
+    aio_poll(qbh->ctx, false);
+    qemu_bh_delete(vtpmdev->sr_bh);

hw/xen/xen_stubdom_vtpm.c

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    if (vtpmdev->shr) {
+        xc_gntshr_munmap(vtpmdev->xen_xcs, vtpmdev->shr, 1);
+    }
+    xc_interface_close(vtpmdev->xen_xcs);
+    return 0;
+}
+
+static void vtpm_alloc(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    vtpm_aio_ctx = aio_context_new(NULL);
+    if (vtpm_aio_ctx == NULL) {
+        return;
+    }
+    vtpmdev->sr_bh = aio_bh_new(vtpm_aio_ctx, sr_bh_handler,

vtpmdev);

hw/xen/xen_stubdom_vtpm.c

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+    qemu_bh_schedule(vtpmdev->sr_bh);
+    vtpmdev->xen_xcs = xen_xc_gntshr_open(0, 0);
+}
+
+static void vtpm_event(struct XenDevice *xendev)
+{
+    struct XenVtpmDev *vtpmdev = container_of(xendev, struct

XenVtpmDev,

Quoted 7 lines
hw/xen/xen_stubdom_vtpm.c new file +7 -0
+                                              xendev);
+
+    qemu_bh_schedule(vtpmdev->sr_bh);
+}
+
+struct XenDevOps xen_vtpmdev_ops = {
+    .size             = sizeof(struct XenVtpmDev),

hw/xen/xen_stubdom_vtpm.c line 333

Quoted 10 lines
hw/xen/xen_stubdom_vtpm.c new file +10 -0
+    .flags            = DEVOPS_FLAG_IGNORE_STATE |
+                        DEVOPS_FLAG_STUBDOM_BE,
+    .event            = vtpm_event,
+    .free             = vtpm_free,
+    .alloc            = vtpm_alloc,
+    .initialise       = vtpm_initialise,
+    .backend_changed  = vtpm_backend_changed,
+    .recv             = vtpm_recv,
+    .send             = vtpm_send,
+};

Is this the frontend, like the subject line would seem to imply? If so, XenDevOps are made for backends, while this is a frontend. In fact this is the first PV frontend in QEMU. We need to introduce something generic and similar to struct XenDevOps and xen_backend.c but for frontends.

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/hw/xen/xen_backend.h

include/hw/xen/xen_backend.h struct XenDevice; · line 16

Quoted 6 lines

index 3b4125e..45fd6d3 100644

include/hw/xen/xen_backend.h +0 -0
@@ -15,6 +15,8 @@ struct XenDevice;
 #define DEVOPS_FLAG_NEED_GNTDEV   1
 /* don't expect frontend doing correct state transitions (aka console

quirk) */

include/hw/xen/xen_backend.h struct XenDevOps { · line 31

Quoted 11 lines
include/hw/xen/xen_backend.h +3 -0
 #define DEVOPS_FLAG_IGNORE_STATE  2
+/*dev backend is stubdom*/
+#define DEVOPS_FLAG_STUBDOM_BE    4
 
 struct XenDevOps {
     size_t    size;
@@ -26,6 +28,8 @@ struct XenDevOps {
     void      (*event)(struct XenDevice *xendev);
     void      (*disconnect)(struct XenDevice *xendev);
     int       (*free)(struct XenDevice *xendev);
+    int       (*send)(struct XenDevice *xendev, uint8_t* buf, size_t

count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 32

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+    int       (*recv)(struct XenDevice *xendev, uint8_t* buf, size_t

*count);

include/hw/xen/xen_backend.h struct XenDevOps { · line 33

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*backend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h struct XenDevOps { · line 34

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
     void      (*frontend_changed)(struct XenDevice *xendev, const

char *node);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 };
@@ -91,12 +95,19 @@ int xen_be_send_notify(struct XenDevice

*xendev);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 95

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char

*fmt, ...)

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
     GCC_FMT_ATTR(3, 4);
 
+/*Xen stubdom vtpm*/
+int xen_vtpm_register(struct XenDevOps *ops);
+int xen_be_alloc_unbound(struct XenDevice *xendev, int dom, int

remote_dom);

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice *xendev);

Quoted 5 lines
include/hw/xen/xen_backend.h +3 -0
+int xen_vtpm_send(unsigned char *buf, size_t count);
+int xen_vtpm_recv(unsigned char *buf, size_t *count);
+
 /* actual backend drivers */
 extern struct XenDevOps xen_console_ops;      /* xen_console.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 106

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 108

Quoted 2 lines
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 109

Quoted 1 line
include/hw/xen/xen_backend.h +0 -0
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c

*/

include/hw/xen/xen_backend.h int xen_be_send_notify(struct XenDevice · line 110

Quoted 1 line
include/hw/xen/xen_backend.h +1 -0
+extern struct XenDevOps xen_vtpmdev_ops;      /*

xen_stubdom_vtpm.c*/

Review

Quoted 4 lines
include/hw/xen/xen_backend.h +0 -0
 void xen_init_display(int domid);
 
(quoted diff) +0 -0

b/include/hw/xen/xen_common.h

include/hw/xen/xen_common.h static inline XenXC xen_xc_interface_open(void

Quoted 4 lines

index 95612a4..fb43084 100644

include/hw/xen/xen_common.h +0 -0
@@ -130,6 +130,12 @@ static inline XenXC xen_xc_interface_open(void

*logger, void *dombuild_logger,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 17 lines
include/hw/xen/xen_common.h +6 -0
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+static inline xc_gntshr *xen_xc_gntshr_open(void *logger,
+                                           unsigned int open_flags)
+{
+    return xc_gntshr_open(logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
xen-hvm.c +0 -0
index 21f1cbb..c99ace8 100644
@@ -1067,6 +1067,11 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t

Quoted 12 lines
xen-hvm.c +5 -0
     int i, rc;
     unsigned long ioreq_pfn;
     unsigned long bufioreq_evtchn;
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    unsigned long stubdom_vtpm = 0;
+#endif
+
     XenIOState *state;
 
     state = g_malloc0(sizeof (XenIOState));
@@ -1169,6 +1174,14 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1174

Quoted 1 line
xen-hvm.c +0 -0
         fprintf(stderr, "%s: xen backend core setup failed\n",

__FUNCTION__);

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +3 -0
         return -1;
     }
+
+#ifdef CONFIG_TPM_XENSTUBDOMS
+    xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_STUBDOM_VTPM, &stubdom_vtpm);

Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1183

Quoted 4 lines
xen-hvm.c +4 -0
+    if (stubdom_vtpm) {
+        xen_vtpm_register(&xen_vtpmdev_ops);
+    }
+#endif

Given that vtpm is just a PV frontend, can't you just detect whether is present on xenstore and initialize it based on that? Like all the backend below?

Refer to above explanation.

Thanks
Quan

Quoted 8 lines
Quoted 8 lines
xen-hvm.c +0 -1
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
--

1.8.3.2

#13 lore raw
Quoted 49 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops);
+ xen_be_register("qdisk", &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

Quoted 6 lines

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag in include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Given that xen_vtpm_register is actually registering a frontend, not a backend, you cannot use xen_be_register for it.

However instead of introducing xen_vtpm_register, I think you should be adding a generic xen_fe_register function that handle any Xen PV frontend registration. It should also be able to handle backends not in Dom0. Then you can call:

xen_fe_register("console", &xen_vtpm_ops);

#14 lore raw
In reply to Xu, Quan
Quoted 49 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops);
+ xen_be_register("qdisk", &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

Quoted 6 lines

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag in include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Given that xen_vtpm_register is actually registering a frontend, not a backend, you cannot use xen_be_register for it.

However instead of introducing xen_vtpm_register, I think you should be adding a generic xen_fe_register function that handle any Xen PV frontend registration. It should also be able to handle backends not in Dom0. Then you can call:

xen_fe_register("console", &xen_vtpm_ops);

author
#15 lore raw
Quoted 71 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Thursday, November 06, 2014 11:42 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 49 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops); xen_be_register("qdisk",
+ &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

Quoted 4 lines

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag

in

Quoted 2 lines

include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Given that xen_vtpm_register is actually registering a frontend, not a backend, you cannot use xen_be_register for it.

Quoted 6 lines

However instead of introducing xen_vtpm_register, I think you should be adding a generic xen_fe_register function that handle any Xen PV frontend registration. It should also be able to handle backends not in Dom0. Then you can call:

xen_fe_register("console", &xen_vtpm_ops);

xen_fe_register("vtpm", &xen_vtpm_ops); ?

A good solution, I will try to add a generic xen_fe_register function that handle any Xen PV frontend in v2.

author
#16 lore raw
In reply to Stefano Stabellini
Quoted 71 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Thursday, November 06, 2014 11:42 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 49 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops); xen_be_register("qdisk",
+ &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

Quoted 4 lines

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag

in

Quoted 2 lines

include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Given that xen_vtpm_register is actually registering a frontend, not a backend, you cannot use xen_be_register for it.

Quoted 6 lines

However instead of introducing xen_vtpm_register, I think you should be adding a generic xen_fe_register function that handle any Xen PV frontend registration. It should also be able to handle backends not in Dom0. Then you can call:

xen_fe_register("console", &xen_vtpm_ops);

xen_fe_register("vtpm", &xen_vtpm_ops); ?

A good solution, I will try to add a generic xen_fe_register function that handle any Xen PV frontend in v2.

#17 lore raw
Quoted 69 lines · Quan
Quoted 69 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Thursday, November 06, 2014 11:42 PM
To: Xu, Quan
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 49 lines · Quan
Quoted 14 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Monday, November 03, 2014 7:54 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 2/4] Qemu-Xen-vTPM: Register Xen stubdom vTPM frontend driver

Quoted 4 lines · Quan Xu

This drvier transfers any request/repond between TPM xenstubdoms driver and Xen vTPM stubdom, and facilitates communications between Xen vTPM stubdom domain and vTPM xenstubdoms driver

Quoted 6 lines
Quoted 1 line

Please describe what changes did make to xen_backend.c and why. The commit message should contains info on all the changes made by the patch below.

The following is code process when Qemu is running with Xen. ##code process
[...]
xen_hvm_init()

    -->xen_be_register()
        -->xenstore_scan()
            -->xen_be_check_state()

    -->xen_vtpm_register()

ideally, I can register 'vtpm' via xen_vtpm_register() as

+ xen_be_register("console", &xen_console_ops);
+ xen_be_register("vkbd", &xen_kbdmouse_ops); xen_be_register("qdisk",
+ &xen_blkdev_ops);

but there are 2 reasons why I add xen_vtpm_register(), instead of xen_be_register().

1. The backend of TPM is runing in a Xen stubDom, not Domain 0. some functions are not working, for example 'setup watch' and 'look for backend' in xenstore_scan()

2. there is a thread runing in Xen stubDom [event_thread()], it will handle backend status when the frontend is initialized. It is not compatible with xen_be_check_state(). xen_be_check_state() always tries to modify the status of backend.

Quoted 4 lines

as there is always a tradeoff, if I force to integrate this case into xen_be_register(), there are maybe a lot of 'if ... else.. '. It will break the code architecture. Also I should leverage existing source code with minimum modifcation. i add 'DEVOPS_FLAG_STUBDOM_BE' flag

in

Quoted 2 lines

include/hw/xen/xen_backend.h to indicate that device backend is Xen stubDom.

Given that xen_vtpm_register is actually registering a frontend, not a backend, you cannot use xen_be_register for it.

Quoted 8 lines
Quoted 6 lines

However instead of introducing xen_vtpm_register, I think you should be adding a generic xen_fe_register function that handle any Xen PV frontend registration. It should also be able to handle backends not in Dom0. Then you can call:

xen_fe_register("console", &xen_vtpm_ops);

xen_fe_register("vtpm", &xen_vtpm_ops); ?

Yes, that is what I meant, sorry.

Quoted 1 line

A good solution, I will try to add a generic xen_fe_register function that handle any Xen PV frontend in v2.

Cool, thanks.