From patchwork Wed Dec 14 13:43:16 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 131380 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2895E1007D6 for ; Thu, 15 Dec 2011 00:44:33 +1100 (EST) Received: from localhost ([::1]:32999 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rap8D-0000Lg-R3 for incoming@patchwork.ozlabs.org; Wed, 14 Dec 2011 08:44:25 -0500 Received: from eggs.gnu.org ([140.186.70.92]:38260) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rap7v-00083A-Rw for qemu-devel@nongnu.org; Wed, 14 Dec 2011 08:44:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rap7t-0001Hl-Hl for qemu-devel@nongnu.org; Wed, 14 Dec 2011 08:44:07 -0500 Received: from e8.ny.us.ibm.com ([32.97.182.138]:48931) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rap7t-0001HZ-4G for qemu-devel@nongnu.org; Wed, 14 Dec 2011 08:44:05 -0500 Received: from /spool/local by e8.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 14 Dec 2011 08:44:03 -0500 Received: from d01relay04.pok.ibm.com (9.56.227.236) by e8.ny.us.ibm.com (192.168.1.108) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 14 Dec 2011 08:43:32 -0500 Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pBEDhVkM330052 for ; Wed, 14 Dec 2011 08:43:31 -0500 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pBEDhUQc032156 for ; Wed, 14 Dec 2011 06:43:31 -0700 Received: from d941e-5.watson.ibm.com (d941e-5.watson.ibm.com [9.59.241.149]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pBEDhUct032134 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 14 Dec 2011 06:43:30 -0700 Received: from d941e-5.watson.ibm.com (localhost [127.0.0.1]) by d941e-5.watson.ibm.com (8.14.5/8.14.3) with ESMTP id pBEDhSTV025787; Wed, 14 Dec 2011 08:43:28 -0500 Received: (from root@localhost) by d941e-5.watson.ibm.com (8.14.5/8.14.5/Submit) id pBEDhRAP025786; Wed, 14 Dec 2011 08:43:27 -0500 From: Stefan Berger To: stefanb@linux.vnet.ibm.com, qemu-devel@nongnu.org, anthony@codemonkey.ws Date: Wed, 14 Dec 2011 08:43:16 -0500 Message-Id: <1323870202-25742-2-git-send-email-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.6.4 In-Reply-To: <1323870202-25742-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1323870202-25742-1-git-send-email-stefanb@linux.vnet.ibm.com> x-cbid: 11121413-9360-0000-0000-000001818F38 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.182.138 Cc: andreas.niederl@iaik.tugraz.at, mst@redhat.com Subject: [Qemu-devel] [PATCH V14 1/7] Support for TPM command line options X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds support for TPM command line options. The command line options supported here are ./qemu-... -tpmdev passthrough,path=,id= -device tpm-tis,tpmdev= and ./qemu-... -tpmdev ? where the latter works similar to -soundhw ? and shows a list of available TPM backends (for example 'passthrough'). Using the type parameter, the backend is chosen, i.e., 'passthrough' for the passthrough driver. The interpretation of the other parameters along with determining whether enough parameters were provided is pushed into the backend driver, which needs to implement the interface function 'create' and return a TPMDriver structure if the VM can be started or 'NULL' if not enough or bad parameters were provided. Monitor support for 'info tpm' has been added. It for example prints the following: (qemu) info tpm TPM devices: tpm0: model=tpm-tis \ tpm0: type=passthrough,path=/dev/tpm0 Signed-off-by: Stefan Berger --- hmp-commands.hx | 2 + hmp.c | 28 +++++++ hmp.h | 1 + hw/tpm_tis.h | 80 ++++++++++++++++++++ monitor.c | 8 ++ qapi-schema.json | 29 +++++++ qemu-config.c | 20 +++++ qemu-options.hx | 30 ++++++++ tpm.c | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tpm.h | 86 ++++++++++++++++++++++ vl.c | 20 +++++ 11 files changed, 518 insertions(+), 0 deletions(-) create mode 100644 hw/tpm_tis.h create mode 100644 tpm.c create mode 100644 tpm.h diff --git a/hmp-commands.hx b/hmp-commands.hx index fdbed15..4506f56 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1352,6 +1352,8 @@ show device tree show qdev device model list @item info roms show roms +@item info tpm +show the TPM device @end table ETEXI diff --git a/hmp.c b/hmp.c index e7659d5..8865e41 100644 --- a/hmp.c +++ b/hmp.c @@ -507,6 +507,34 @@ void hmp_info_pci(Monitor *mon) qapi_free_PciInfoList(info); } +void hmp_info_tpm(Monitor *mon) +{ + TPMInfoList *info_list, *info; + Error *err = NULL; + unsigned int c = 0; + + info_list = qmp_query_tpm(&err); + if (err) { + monitor_printf(mon, "TPM device not supported\n"); + error_free(err); + return; + } + + monitor_printf(mon, "TPM device:\n"); + + for (info = info_list; info; info = info->next) { + TPMInfo *ti = info->value; + monitor_printf(mon, " tpm%d: model=%s\n", + c, ti->model); + monitor_printf(mon, " \\ %s: type=%s%s%s\n", + ti->id, ti->type, + ti->parameters ? "," : "", + ti->parameters ? ti->parameters : ""); + c++; + } + qapi_free_TPMInfoList(info_list); +} + void hmp_quit(Monitor *mon, const QDict *qdict) { monitor_suspend(mon); diff --git a/hmp.h b/hmp.h index 093242d..73be269 100644 --- a/hmp.h +++ b/hmp.h @@ -32,6 +32,7 @@ void hmp_info_vnc(Monitor *mon); void hmp_info_spice(Monitor *mon); void hmp_info_balloon(Monitor *mon); void hmp_info_pci(Monitor *mon); +void hmp_info_tpm(Monitor *mon); void hmp_quit(Monitor *mon, const QDict *qdict); void hmp_stop(Monitor *mon, const QDict *qdict); void hmp_system_reset(Monitor *mon, const QDict *qdict); diff --git a/hw/tpm_tis.h b/hw/tpm_tis.h new file mode 100644 index 0000000..1270242 --- /dev/null +++ b/hw/tpm_tis.h @@ -0,0 +1,80 @@ +/* + * tpm_tis.c - QEMU's TPM TIS interface emulator + * + * Copyright (C) 2006,2010,2011 IBM Corporation + * + * Authors: + * Stefan Berger + * David Safford + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2 of the + * License. + * + * + * Implementation of the TIS interface according to specs found at + * http://www.trustedcomputiggroup.org + * + */ +#ifndef HW_TPM_TIS_H +#define HW_TPM_TIS_H + +#include "isa.h" +#include "qemu-thread.h" +#include "qemu-common.h" + +#define TPM_TIS_ADDR_BASE 0xFED40000 + +#define TPM_TIS_NUM_LOCALITIES 5 /* per spec */ +#define TPM_TIS_NO_LOCALITY 0xff + +#define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) + +#define TPM_TIS_IRQ 5 + +#define TPM_TIS_BUFFER_MAX 4096 + + +typedef struct TPMSizedBuffer { + uint32_t size; + uint8_t *buffer; +} TPMSizedBuffer; + +typedef enum { + TPM_TIS_STATUS_IDLE = 0, + TPM_TIS_STATUS_READY, + TPM_TIS_STATUS_COMPLETION, + TPM_TIS_STATUS_EXECUTION, + TPM_TIS_STATUS_RECEPTION, +} TPMTISStatus; + +/* locality data -- all fields are persisted */ +typedef struct TPMLocality { + TPMTISStatus status; + uint8_t access; + uint8_t sts; + uint32_t inte; + uint32_t ints; + + uint16_t w_offset; + uint16_t r_offset; + TPMSizedBuffer w_buffer; + TPMSizedBuffer r_buffer; +} TPMLocality; + +typedef struct TPMTISState { + uint32_t offset; + uint8_t buf[TPM_TIS_BUFFER_MAX]; + + uint8_t active_locty; + uint8_t aborting_locty; + uint8_t next_locty; + + TPMLocality loc[TPM_TIS_NUM_LOCALITIES]; + + qemu_irq irq; + uint32_t irq_num; +} TPMTISState; + +#endif /* HW_TPM_TIS_H */ diff --git a/monitor.c b/monitor.c index 7334401..50693a3 100644 --- a/monitor.c +++ b/monitor.c @@ -47,6 +47,7 @@ #include "migration.h" #include "kvm.h" #include "acl.h" +#include "tpm.h" #include "qint.h" #include "qfloat.h" #include "qlist.h" @@ -2736,6 +2737,13 @@ static mon_cmd_t info_cmds[] = { .mhandler.info = do_trace_print_events, }, { + .name = "tpm", + .args_type = "", + .params = "", + .help = "show the TPM device", + .mhandler.info = hmp_info_tpm, + }, + { .name = NULL, }, }; diff --git a/qapi-schema.json b/qapi-schema.json index f358b49..0bfc678 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -1168,3 +1168,32 @@ # Since: 0.14.0 ## { 'command': 'migrate_set_speed', 'data': {'value': 'int'} } + +## +# @TPMInfo: +# +# Information about the TPM +# +# @model: The TPM frontend model, i.e., tpm-tis +# +# @id: The ID of the TPM +# +# @type: The type of TPM backend, i.e., passthrough +# +# @parameters: Additional parameters of the TPM backend device +# +# Since: 1.1 +## +{ 'type': 'TPMInfo', + 'data': {'model': 'str', 'id': 'str', 'type': 'str', 'parameters': 'str' } } + +## +# @query-tpm +# +# Return information about the TPM device. +# +# Returns: @TPMInfo on success +# +# Since: 1.1 +## +{ 'command': 'query-tpm', 'returns': ['TPMInfo'] } diff --git a/qemu-config.c b/qemu-config.c index 18f3020..cc4c31d 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -549,6 +549,25 @@ QemuOptsList qemu_boot_opts = { }, }; +static QemuOptsList qemu_tpmdev_opts = { + .name = "tpmdev", + .implied_opt_name = "type", + .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head), + .desc = { + { + .name = "type", + .type = QEMU_OPT_STRING, + .help = "Type of TPM backend", + }, + { + .name = "path", + .type = QEMU_OPT_STRING, + .help = "Persistent storage for TPM state", + }, + { /* end of list */ } + }, +}; + static QemuOptsList *vm_config_groups[32] = { &qemu_drive_opts, &qemu_chardev_opts, @@ -563,6 +582,7 @@ static QemuOptsList *vm_config_groups[32] = { &qemu_option_rom_opts, &qemu_machine_opts, &qemu_boot_opts, + &qemu_tpmdev_opts, NULL, }; diff --git a/qemu-options.hx b/qemu-options.hx index b3db10c..40a63b1 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -1903,6 +1903,36 @@ ETEXI DEFHEADING() +DEFHEADING(TPM device options:) + +DEF("tpmdev", HAS_ARG, QEMU_OPTION_tpmdev, \ + "-tpmdev [],id=str[,option][,option][,...]\n", + QEMU_ARCH_ALL) +STEXI + +The general form of a TPM device option is: +@table @option + +@item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] +@findex -tpmdev +Backend type must be: + +The specific backend type will determine the applicable options. +The @code{-tpmdev} options requires a @code{-device} option. + +Options to each backend are described below. + +Use ? to print all available TPM backend types. +@example +qemu -tpmdev ? +@end example + +@end table + +ETEXI + +DEFHEADING() + DEFHEADING(Linux/Multiboot boot specific:) STEXI diff --git a/tpm.c b/tpm.c new file mode 100644 index 0000000..408d319 --- /dev/null +++ b/tpm.c @@ -0,0 +1,214 @@ +/* + * TPM configuration + * + * Copyright (C) 2011 IBM Corporation + * + * Authors: + * Stefan Berger + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + * Based on net.c + */ +#include "config.h" + +#include "monitor.h" +#include "qerror.h" +#include "tpm.h" +#include "qmp-commands.h" + +static QLIST_HEAD(, TPMBackend) tpm_backends = + QLIST_HEAD_INITIALIZER(tpm_backends); + +#ifdef CONFIG_TPM + +static const TPMDriverOps *bes[] = { + NULL, +}; + +const TPMDriverOps *tpm_get_backend_driver(const char *id) +{ + int i; + + for (i = 0; bes[i] != NULL; i++) { + if (!strcmp(bes[i]->id, id)) { + break; + } + } + + return bes[i]; +} + +/* Walk the list of available TPM backend drivers and display them on the + * screen. + */ +void tpm_display_backend_drivers(void) +{ + int i; + + fprintf(stderr, "Supported TPM types (choose only one):\n"); + + for (i = 0; bes[i] != NULL; i++) { + fprintf(stderr, "%12s %s\n", bes[i]->id, bes[i]->desc()); + } + fprintf(stderr, "\n"); +} + +/* Find the TPM with the given Id + */ +TPMBackend *qemu_find_tpm(const char *id) +{ + TPMBackend *drv; + + QLIST_FOREACH(drv, &tpm_backends, list) { + if (!strcmp(drv->id, id)) { + return drv; + } + } + + return NULL; +} + +static int configure_tpm(QemuOpts *opts) +{ + const char *value; + const char *id; + const TPMDriverOps *be; + TPMBackend *drv; + + if (!QLIST_EMPTY(&tpm_backends)) { + error_report("Only one TPM is allowed.\n"); + return 1; + } + + id = qemu_opts_id(opts); + if (id == NULL) { + qerror_report(QERR_MISSING_PARAMETER, "id"); + return 1; + } + + value = qemu_opt_get(opts, "type"); + if (!value) { + qerror_report(QERR_MISSING_PARAMETER, "type"); + tpm_display_backend_drivers(); + return 1; + } + + be = tpm_get_backend_driver(value); + if (be == NULL) { + qerror_report(QERR_INVALID_PARAMETER_VALUE, "type", + "a tpm backend type"); + tpm_display_backend_drivers(); + return 1; + } + + drv = be->create(opts, id); + if (!drv) { + return 1; + } + + QLIST_INSERT_HEAD(&tpm_backends, drv, list); + + return 0; +} + +static int tpm_init_tpmdev(QemuOpts *opts, void *dummy) +{ + return configure_tpm(opts); +} + +/* Walk the list of TPM backend drivers that are in use and call their + * destroy function to have them cleaned up. + */ +void tpm_cleanup(void) +{ + TPMBackend *drv, *next; + + QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { + QLIST_REMOVE(drv, list); + drv->ops->destroy(drv); + } +} + +/* Initialize the TPM. Process the tpmdev command line options describing the + * TPM backend. + */ +int tpm_init(void) +{ + if (qemu_opts_foreach(qemu_find_opts("tpmdev"), + tpm_init_tpmdev, NULL, 1) != 0) { + return -1; + } + + atexit(tpm_cleanup); + + return 0; +} + +/* Parse the TPM configuration options. + * It is possible to pass an option '-tpmdev none' to not activate any TPM. + * To display all available TPM backends the user may use '-tpmdev ?' + */ +int tpm_config_parse(QemuOptsList *opts_list, const char *optarg) +{ + QemuOpts *opts; + + if (strcmp("none", optarg) != 0) { + if (*optarg == '?') { + tpm_display_backend_drivers(); + return -1; + } + opts = qemu_opts_parse(opts_list, optarg, 1); + if (!opts) { + return -1; + } + } + return 0; +} + +#endif /* CONFIG_TPM */ + +static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv) +{ + TPMInfo *res = g_malloc0(sizeof(*res)); + + if (res) { + res->model = g_strdup(drv->fe_model); + res->id = g_strdup(drv->id); + if (drv->parameters) { + res->parameters = g_strdup(drv->parameters); + } + res->type = g_strdup(drv->ops->id); + } + + return res; +} + +/* Walk the list of active TPM backends and collect information about them + * following the schema description in qapi-schema.json. + */ +TPMInfoList *qmp_query_tpm(Error **errp) +{ + TPMBackend *drv; + TPMInfoList *info, *head = NULL, *cur_item = NULL; + + QLIST_FOREACH(drv, &tpm_backends, list) { + info = g_malloc0(sizeof(*info)); + + if (!info) { + break; + } + + info->value = qmp_query_tpm_inst(drv); + + if (!cur_item) { + head = cur_item = info; + } else { + cur_item->next = info; + cur_item = info; + } + } + + return head; +} diff --git a/tpm.h b/tpm.h new file mode 100644 index 0000000..075de3f --- /dev/null +++ b/tpm.h @@ -0,0 +1,86 @@ +/* + * TPM configuration + * + * Copyright (C) 2011 IBM Corporation + * + * Authors: + * Stefan Berger + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#ifndef QEMU_TPM_H +#define QEMU_TPM_H + +#include "memory.h" +#include "hw/tpm_tis.h" + +struct TPMDriverOps; +typedef struct TPMDriverOps TPMDriverOps; + +typedef struct TPMBackend { + char *id; + const char *fe_model; + char *parameters; + const TPMDriverOps *ops; + + QLIST_ENTRY(TPMBackend) list; +} TPMBackend; + +/* overall state of the TPM interface */ +typedef struct TPMState { + ISADevice busdev; + MemoryRegion mmio; + + union { + TPMTISState tis; + } s; + + uint8_t command_locty; + TPMLocality *cmd_locty; + + QemuMutex state_lock; + QemuCond from_tpm_cond; + QemuCond to_tpm_cond; + bool to_tpm_execute; + + bool tpm_initialized; + + char *backend; + TPMBackend *be_driver; +} TPMState; + +typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty); + +struct TPMDriverOps { + const char *id; + /* get a descriptive text of the backend to display to the user */ + const char *(*desc)(void); + + TPMBackend *(*create)(QemuOpts *opts, const char *id); + void (*destroy)(TPMBackend *t); + + /* initialize the backend */ + int (*init)(TPMBackend *t, TPMState *s, TPMRecvDataCB *datacb); + /* start up the TPM on the backend */ + int (*startup_tpm)(TPMBackend *t); + /* returns true if nothing will ever answer TPM requests */ + bool (*had_startup_error)(TPMBackend *t); + + size_t (*realloc_buffer)(TPMSizedBuffer *sb); + + void (*reset)(TPMBackend *t); + + bool (*get_tpm_established_flag)(TPMBackend *t); +}; + +#define TPM_DEFAULT_DEVICE_MODEL "tpm-tis" + +int tpm_config_parse(QemuOptsList *opts_list, const char *optarg); +int tpm_init(void); +void tpm_cleanup(void); +TPMBackend *qemu_find_tpm(const char *id); +void tpm_display_backend_drivers(void); +const TPMDriverOps *tpm_get_backend_driver(const char *id); + +#endif /* QEMU_TPM_H */ diff --git a/vl.c b/vl.c index 5372a96..2c8ca80 100644 --- a/vl.c +++ b/vl.c @@ -139,6 +139,7 @@ int main(int argc, char **argv) #include "block.h" #include "blockdev.h" #include "block-migration.h" +#include "tpm.h" #include "dma.h" #include "audio/audio.h" #include "migration.h" @@ -2550,6 +2551,16 @@ int main(int argc, char **argv, char **envp) ram_size = value; break; } + case QEMU_OPTION_tpmdev: +#ifdef CONFIG_TPM + if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) { + exit(1); + } +#else + fprintf(stderr, "TPM support is disabled\n"); + exit(1); +#endif + break; case QEMU_OPTION_mempath: mem_path = optarg; break; @@ -3243,6 +3254,12 @@ int main(int argc, char **argv, char **envp) exit(1); } +#ifdef CONFIG_TPM + if (tpm_init() < 0) { + exit(1); + } +#endif + /* init the bluetooth world */ if (foreach_device_config(DEV_BT, bt_parse)) exit(1); @@ -3487,6 +3504,9 @@ int main(int argc, char **argv, char **envp) pause_all_vcpus(); net_cleanup(); res_free(); +#ifdef CONFIG_TPM + tpm_cleanup(); +#endif return 0; }