Commit e67277f8 authored by Peter Maydell's avatar Peter Maydell
Browse files

Merge remote-tracking branch 'remotes/stefanberger/tags/pull-tpm-2017-10-19-1' into staging



Merge tpm 2017/10/19 v1

# gpg: Signature made Thu 19 Oct 2017 16:42:39 BST
# gpg:                using RSA key 0x75AD65802A0B4211
# gpg: Good signature from "Stefan Berger <stefanb@linux.vnet.ibm.com>"
# gpg: WARNING: This key is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: B818 B9CA DF90 89C2 D5CE  C66B 75AD 6580 2A0B 4211

* remotes/stefanberger/tags/pull-tpm-2017-10-19-1: (21 commits)
  tpm: move recv_data_callback to TPM interface
  tpm: add a QOM TPM interface
  tpm-tis: fold TPMTISEmuState in TPMState
  tpm-tis: remove tpm_tis.h header
  tpm-tis: move TPMState to TIS header
  tpm: remove locty_data from TPMState
  tpm-emulator: fix error handling
  tpm: add TPMBackendCmd to hold the request state
  tpm: remove locty argument from receive_cb
  tpm: remove needless cast
  tpm: remove unused TPMBackendCmd
  tpm: remove configure_tpm() hop
  tpm: remove init() class method
  tpm: remove TPMDriverOps
  tpm: move TPMSizedBuffer to tpm_tis.h
  tpm: remove tpm_register_driver()
  tpm: replace tpm_get_backend_driver() to drop be_drivers
  tpm: lookup tpm backend class in tpm_driver_find_by_type()
  tpm: make tpm_get_backend_driver() static
  tpm-tis: remove RAISE_STS_IRQ
  ...

Signed-off-by: default avatarPeter Maydell <peter.maydell@linaro.org>
parents 840e0691 05a69998
Loading
Loading
Loading
Loading
+28 −26
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include "qapi/error.h"
#include "qapi/qmp/qerror.h"
#include "sysemu/tpm.h"
#include "hw/tpm/tpm_int.h"
#include "qemu/thread.h"

static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
@@ -25,13 +26,12 @@ static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
    TPMBackendClass *k  = TPM_BACKEND_GET_CLASS(s);

    assert(k->handle_request != NULL);
    k->handle_request(s, (TPMBackendCmd)data);
    k->handle_request(s, (TPMBackendCmd *)data);
}

static void tpm_backend_thread_end(TPMBackend *s)
{
    if (s->thread_pool) {
        g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
        g_thread_pool_free(s->thread_pool, FALSE, TRUE);
        s->thread_pool = NULL;
    }
@@ -41,19 +41,15 @@ enum TpmType tpm_backend_get_type(TPMBackend *s)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    return k->ops->type;
    return k->type;
}

int tpm_backend_init(TPMBackend *s, TPMState *state,
                     TPMRecvDataCB *datacb)
int tpm_backend_init(TPMBackend *s, TPMState *state)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    s->tpm_state = state;
    s->recv_data_callback = datacb;
    s->had_startup_error = false;

    return k->ops->init ? k->ops->init(s) : 0;
    return 0;
}

int tpm_backend_startup_tpm(TPMBackend *s)
@@ -66,9 +62,8 @@ int tpm_backend_startup_tpm(TPMBackend *s)

    s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
                                       NULL);
    g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);

    res = k->ops->startup_tpm ? k->ops->startup_tpm(s) : 0;
    res = k->startup_tpm ? k->startup_tpm(s) : 0;

    s->had_startup_error = (res != 0);

@@ -80,18 +75,17 @@ bool tpm_backend_had_startup_error(TPMBackend *s)
    return s->had_startup_error;
}

void tpm_backend_deliver_request(TPMBackend *s)
void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
{
    g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
                       NULL);
    g_thread_pool_push(s->thread_pool, cmd, NULL);
}

void tpm_backend_reset(TPMBackend *s)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    if (k->ops->reset) {
        k->ops->reset(s);
    if (k->reset) {
        k->reset(s);
    }

    tpm_backend_thread_end(s);
@@ -103,34 +97,34 @@ void tpm_backend_cancel_cmd(TPMBackend *s)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    assert(k->ops->cancel_cmd);
    assert(k->cancel_cmd);

    k->ops->cancel_cmd(s);
    k->cancel_cmd(s);
}

bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    return k->ops->get_tpm_established_flag ?
           k->ops->get_tpm_established_flag(s) : false;
    return k->get_tpm_established_flag ?
           k->get_tpm_established_flag(s) : false;
}

int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    return k->ops->reset_tpm_established_flag ?
           k->ops->reset_tpm_established_flag(s, locty) : 0;
    return k->reset_tpm_established_flag ?
           k->reset_tpm_established_flag(s, locty) : 0;
}

TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
{
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);

    assert(k->ops->get_tpm_version);
    assert(k->get_tpm_version);

    return k->ops->get_tpm_version(s);
    return k->get_tpm_version(s);
}

TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
@@ -140,8 +134,9 @@ TPMInfo *tpm_backend_query_tpm(TPMBackend *s)

    info->id = g_strdup(s->id);
    info->model = s->fe_model;
    info->options = k->ops->get_tpm_options ?
                    k->ops->get_tpm_options(s) : NULL;
    if (k->get_tpm_options) {
        info->options = k->get_tpm_options(s);
    }

    return info;
}
@@ -213,9 +208,16 @@ static const TypeInfo tpm_backend_info = {
    .abstract = true,
};

static const TypeInfo tpm_if_info = {
    .name = TYPE_TPM_IF,
    .parent = TYPE_INTERFACE,
    .class_size = sizeof(TPMIfClass),
};

static void register_types(void)
{
    type_register_static(&tpm_backend_info);
    type_register_static(&tpm_if_info);
}

type_init(register_types);
+35 −49
Original line number Diff line number Diff line
@@ -60,8 +60,6 @@

#define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))

static const TPMDriverOps tpm_emulator_driver;

/* data structures */
typedef struct TPMEmulator {
    TPMBackend parent;
@@ -143,7 +141,8 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
    return 0;
}

static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number)
static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
                                     Error **errp)
{
    ptm_loc loc;

@@ -157,14 +156,14 @@ static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number)
    loc.u.req.loc = locty_number;
    if (tpm_emulator_ctrlcmd(&tpm_emu->ctrl_chr, CMD_SET_LOCALITY, &loc,
                             sizeof(loc), sizeof(loc)) < 0) {
        error_report("tpm-emulator: could not set locality : %s",
        error_setg(errp, "tpm-emulator: could not set locality : %s",
                   strerror(errno));
        return -1;
    }

    loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result);
    if (loc.u.resp.tpm_result != 0) {
        error_report("tpm-emulator: TPM result for set locality : 0x%x",
        error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x",
                   loc.u.resp.tpm_result);
        return -1;
    }
@@ -174,39 +173,30 @@ static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number)
    return 0;
}

static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd cmd)
static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
{
    TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
    TPMLocality *locty = NULL;
    bool selftest_done = false;
    TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpm_state);
    Error *err = NULL;

    DPRINTF("processing command type %d", cmd);

    switch (cmd) {
    case TPM_BACKEND_CMD_PROCESS_CMD:
        locty = tb->tpm_state->locty_data;
        if (tpm_emulator_set_locality(tpm_emu,
                                      tb->tpm_state->locty_number) < 0 ||
            tpm_emulator_unix_tx_bufs(tpm_emu, locty->w_buffer.buffer,
                                      locty->w_offset, locty->r_buffer.buffer,
                                      locty->r_buffer.size, &selftest_done,
                                      &err) < 0) {
            tpm_util_write_fatal_error_response(locty->r_buffer.buffer,
                                                locty->r_buffer.size);
            error_report_err(err);
        }
    DPRINTF("processing TPM command");

        tb->recv_data_callback(tb->tpm_state, tb->tpm_state->locty_number,
                               selftest_done);
    if (tpm_emulator_set_locality(tpm_emu, cmd->locty, &err) < 0) {
        goto error;
    }

        break;
    case TPM_BACKEND_CMD_INIT:
    case TPM_BACKEND_CMD_END:
    case TPM_BACKEND_CMD_TPM_RESET:
        /* nothing to do */
        break;
    if (tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
                                  cmd->out, cmd->out_len,
                                  &cmd->selftest_done, &err) < 0) {
        goto error;
    }

    tic->request_completed(TPM_IF(tb->tpm_state));
    return;

error:
    tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
    error_report_err(err);
}

static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu)
@@ -504,20 +494,6 @@ static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
    { /* end of list */ },
};

static const TPMDriverOps tpm_emulator_driver = {
    .type                     = TPM_TYPE_EMULATOR,
    .opts                     = tpm_emulator_cmdline_opts,
    .desc                     = "TPM emulator backend driver",

    .create                   = tpm_emulator_create,
    .startup_tpm              = tpm_emulator_startup_tpm,
    .cancel_cmd               = tpm_emulator_cancel_cmd,
    .get_tpm_established_flag = tpm_emulator_get_tpm_established_flag,
    .reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag,
    .get_tpm_version          = tpm_emulator_get_tpm_version,
    .get_tpm_options          = tpm_emulator_get_tpm_options,
};

static void tpm_emulator_inst_init(Object *obj)
{
    TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
@@ -565,7 +541,18 @@ static void tpm_emulator_inst_finalize(Object *obj)
static void tpm_emulator_class_init(ObjectClass *klass, void *data)
{
    TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
    tbc->ops = &tpm_emulator_driver;

    tbc->type = TPM_TYPE_EMULATOR;
    tbc->opts = tpm_emulator_cmdline_opts;
    tbc->desc = "TPM emulator backend driver";
    tbc->create = tpm_emulator_create;
    tbc->startup_tpm = tpm_emulator_startup_tpm;
    tbc->cancel_cmd = tpm_emulator_cancel_cmd;
    tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
    tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
    tbc->get_tpm_version = tpm_emulator_get_tpm_version;
    tbc->get_tpm_options = tpm_emulator_get_tpm_options;

    tbc->handle_request = tpm_emulator_handle_request;
}

@@ -581,7 +568,6 @@ static const TypeInfo tpm_emulator_info = {
static void tpm_emulator_register(void)
{
    type_register_static(&tpm_emulator_info);
    tpm_register_driver(&tpm_emulator_driver);
}

type_init(tpm_emulator_register)
+18 −18
Original line number Diff line number Diff line
@@ -12,27 +12,27 @@
#ifndef TPM_TPM_INT_H
#define TPM_TPM_INT_H

#include "exec/memory.h"
#include "tpm_tis.h"

/* overall state of the TPM interface */
struct TPMState {
    ISADevice busdev;
    MemoryRegion mmio;

    union {
        TPMTISEmuState tis;
    } s;

    uint8_t     locty_number;
    TPMLocality *locty_data;

    char *backend;
    TPMBackend *be_driver;
    TPMVersion be_tpm_version;
};

#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
#include "qemu/osdep.h"
#include "qom/object.h"

#define TYPE_TPM_IF "tpm-if"
#define TPM_IF_CLASS(klass) \
    OBJECT_CLASS_CHECK(TPMIfClass, (klass), TYPE_TPM_IF)
#define TPM_IF_GET_CLASS(obj) \
    OBJECT_GET_CLASS(TPMIfClass, (obj), TYPE_TPM_IF)
#define TPM_IF(obj) \
    INTERFACE_CHECK(TPMIf, (obj), TYPE_TPM_IF)

typedef struct TPMIf {
    Object parent_obj;
} TPMIf;

typedef struct TPMIfClass {
    InterfaceClass parent_class;

    /* run in thread pool by backend */
    void (*request_completed)(TPMIf *obj);
} TPMIfClass;

#define TPM_STANDARD_CMDLINE_OPTS               \
    { \
+21 −50
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@
#include "hw/hw.h"
#include "hw/i386/pc.h"
#include "qapi/clone-visitor.h"
#include "tpm_tis.h"
#include "tpm_util.h"

#define DEBUG_TPM 0
@@ -96,7 +95,7 @@ static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,

    is_selftest = tpm_util_is_selftest(in, in_len);

    ret = qemu_write_full(tpm_pt->tpm_fd, (const void *)in, (size_t)in_len);
    ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len);
    if (ret != in_len) {
        if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
            error_report("tpm_passthrough: error while transmitting data "
@@ -137,41 +136,17 @@ err_exit:
    return ret;
}

static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
                                         const TPMLocality *locty_data,
                                         bool *selftest_done)
{
    return tpm_passthrough_unix_tx_bufs(tpm_pt,
                                        locty_data->w_buffer.buffer,
                                        locty_data->w_offset,
                                        locty_data->r_buffer.buffer,
                                        locty_data->r_buffer.size,
                                        selftest_done);
}

static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd cmd)
static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
{
    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
    bool selftest_done = false;
    TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpm_state);

    DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
    DPRINTF("tpm_passthrough: processing command %p\n", cmd);

    switch (cmd) {
    case TPM_BACKEND_CMD_PROCESS_CMD:
        tpm_passthrough_unix_transfer(tpm_pt,
                                      tb->tpm_state->locty_data,
                                      &selftest_done);
    tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
                                 cmd->out, cmd->out_len, &cmd->selftest_done);

        tb->recv_data_callback(tb->tpm_state,
                               tb->tpm_state->locty_number,
                               selftest_done);
        break;
    case TPM_BACKEND_CMD_INIT:
    case TPM_BACKEND_CMD_END:
    case TPM_BACKEND_CMD_TPM_RESET:
        /* nothing to do */
        break;
    }
    tic->request_completed(TPM_IF(tb->tpm_state));
}

static void tpm_passthrough_reset(TPMBackend *tb)
@@ -365,19 +340,6 @@ static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
    { /* end of list */ },
};

static const TPMDriverOps tpm_passthrough_driver = {
    .type                     = TPM_TYPE_PASSTHROUGH,
    .opts                     = tpm_passthrough_cmdline_opts,
    .desc                     = "Passthrough TPM backend driver",
    .create                   = tpm_passthrough_create,
    .reset                    = tpm_passthrough_reset,
    .cancel_cmd               = tpm_passthrough_cancel_cmd,
    .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
    .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,
    .get_tpm_version          = tpm_passthrough_get_tpm_version,
    .get_tpm_options          = tpm_passthrough_get_tpm_options,
};

static void tpm_passthrough_inst_init(Object *obj)
{
    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
@@ -402,7 +364,17 @@ static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
{
    TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);

    tbc->ops = &tpm_passthrough_driver;
    tbc->type = TPM_TYPE_PASSTHROUGH;
    tbc->opts = tpm_passthrough_cmdline_opts;
    tbc->desc = "Passthrough TPM backend driver";
    tbc->create = tpm_passthrough_create;
    tbc->reset = tpm_passthrough_reset;
    tbc->cancel_cmd = tpm_passthrough_cancel_cmd;
    tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag;
    tbc->reset_tpm_established_flag =
        tpm_passthrough_reset_tpm_established_flag;
    tbc->get_tpm_version = tpm_passthrough_get_tpm_version;
    tbc->get_tpm_options = tpm_passthrough_get_tpm_options;
    tbc->handle_request = tpm_passthrough_handle_request;
}

@@ -418,7 +390,6 @@ static const TypeInfo tpm_passthrough_info = {
static void tpm_passthrough_register(void)
{
    type_register_static(&tpm_passthrough_info);
    tpm_register_driver(&tpm_passthrough_driver);
}

type_init(tpm_passthrough_register)
+244 −215

File changed.

Preview size limit exceeded, changes collapsed.

Loading