2013-03-28 12:26:21 +01:00
|
|
|
/*
|
|
|
|
* QEMU TPM Backend
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2013
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Berger <stefanb@us.ibm.com>
|
|
|
|
*
|
|
|
|
* 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 backends/rng.c by Anthony Liguori
|
|
|
|
*/
|
|
|
|
|
2016-01-29 18:49:54 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-08 16:55:25 +02:00
|
|
|
#include "sysemu/tpm_backend.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2013-03-28 12:26:21 +01:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2013-04-02 18:28:41 +02:00
|
|
|
#include "sysemu/tpm.h"
|
|
|
|
#include "qemu/thread.h"
|
2017-11-06 19:39:02 +01:00
|
|
|
#include "qemu/main-loop.h"
|
|
|
|
|
|
|
|
static void tpm_backend_request_completed_bh(void *opaque)
|
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(opaque);
|
|
|
|
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
|
|
|
|
|
|
|
|
tic->request_completed(s->tpmif);
|
|
|
|
}
|
2017-09-29 13:10:14 +02:00
|
|
|
|
|
|
|
static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(user_data);
|
2017-11-06 19:39:02 +01:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
2017-09-29 13:10:14 +02:00
|
|
|
|
2017-10-10 00:55:55 +02:00
|
|
|
k->handle_request(s, (TPMBackendCmd *)data);
|
2017-11-06 19:39:02 +01:00
|
|
|
|
|
|
|
qemu_bh_schedule(s->bh);
|
2017-09-29 13:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_backend_thread_end(TPMBackend *s)
|
|
|
|
{
|
|
|
|
if (s->thread_pool) {
|
|
|
|
g_thread_pool_free(s->thread_pool, FALSE, TRUE);
|
|
|
|
s->thread_pool = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 12:26:21 +01:00
|
|
|
|
|
|
|
enum TpmType tpm_backend_get_type(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
return k->type;
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
2017-11-06 19:39:03 +01:00
|
|
|
int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp)
|
2013-03-28 12:26:21 +01:00
|
|
|
{
|
2017-11-06 19:39:00 +01:00
|
|
|
if (s->tpmif) {
|
2017-11-06 19:39:03 +01:00
|
|
|
error_setg(errp, "TPM backend '%s' is already initialized", s->id);
|
2017-11-06 19:39:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->tpmif = tpmif;
|
|
|
|
object_ref(OBJECT(tpmif));
|
|
|
|
|
2017-09-29 13:10:16 +02:00
|
|
|
s->had_startup_error = false;
|
2017-09-29 13:10:14 +02:00
|
|
|
|
2017-10-10 00:55:50 +02:00
|
|
|
return 0;
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int tpm_backend_startup_tpm(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 13:10:16 +02:00
|
|
|
int res = 0;
|
2013-03-28 12:26:21 +01:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-09-29 13:10:14 +02:00
|
|
|
/* terminate a running TPM */
|
|
|
|
tpm_backend_thread_end(s);
|
|
|
|
|
|
|
|
s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
|
|
|
|
NULL);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
res = k->startup_tpm ? k->startup_tpm(s) : 0;
|
2017-09-29 13:10:16 +02:00
|
|
|
|
|
|
|
s->had_startup_error = (res != 0);
|
|
|
|
|
|
|
|
return res;
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_had_startup_error(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 13:10:16 +02:00
|
|
|
return s->had_startup_error;
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-10 00:55:55 +02:00
|
|
|
void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
|
2013-03-28 12:26:21 +01:00
|
|
|
{
|
2017-10-10 00:55:55 +02:00
|
|
|
g_thread_pool_push(s->thread_pool, cmd, NULL);
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_reset(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
if (k->reset) {
|
|
|
|
k->reset(s);
|
2017-09-29 13:10:16 +02:00
|
|
|
}
|
2017-09-29 13:10:14 +02:00
|
|
|
|
|
|
|
tpm_backend_thread_end(s);
|
2017-09-29 13:10:16 +02:00
|
|
|
|
|
|
|
s->had_startup_error = false;
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_cancel_cmd(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
k->cancel_cmd(s);
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
return k->get_tpm_established_flag ?
|
|
|
|
k->get_tpm_established_flag(s) : false;
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
2015-05-26 22:51:05 +02:00
|
|
|
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
return k->reset_tpm_established_flag ?
|
|
|
|
k->reset_tpm_established_flag(s, locty) : 0;
|
2015-05-26 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 00:55:49 +02:00
|
|
|
return k->get_tpm_version(s);
|
2015-05-26 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-09-29 13:10:17 +02:00
|
|
|
TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMInfo *info = g_new0(TPMInfo, 1);
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
2017-11-06 19:39:04 +01:00
|
|
|
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
|
2017-09-29 13:10:17 +02:00
|
|
|
|
|
|
|
info->id = g_strdup(s->id);
|
2017-11-06 19:39:04 +01:00
|
|
|
info->model = tic->model;
|
2017-11-06 19:39:07 +01:00
|
|
|
info->options = k->get_tpm_options(s);
|
2017-09-29 13:10:17 +02:00
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-03-28 12:26:21 +01:00
|
|
|
static void tpm_backend_instance_init(Object *obj)
|
|
|
|
{
|
2017-09-29 13:10:15 +02:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
|
|
|
|
2017-11-06 19:39:02 +01:00
|
|
|
s->bh = qemu_bh_new(tpm_backend_request_completed_bh, s);
|
2013-04-02 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
2017-09-29 13:10:14 +02:00
|
|
|
static void tpm_backend_instance_finalize(Object *obj)
|
2013-04-02 18:28:41 +02:00
|
|
|
{
|
2017-09-29 13:10:14 +02:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
2013-04-02 18:28:41 +02:00
|
|
|
|
2017-11-06 19:39:00 +01:00
|
|
|
object_unref(OBJECT(s->tpmif));
|
2017-09-29 13:10:15 +02:00
|
|
|
g_free(s->id);
|
2017-09-29 13:10:14 +02:00
|
|
|
tpm_backend_thread_end(s);
|
2017-11-06 19:39:02 +01:00
|
|
|
qemu_bh_delete(s->bh);
|
2013-04-02 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
2013-03-28 12:26:21 +01:00
|
|
|
static const TypeInfo tpm_backend_info = {
|
|
|
|
.name = TYPE_TPM_BACKEND,
|
|
|
|
.parent = TYPE_OBJECT,
|
|
|
|
.instance_size = sizeof(TPMBackend),
|
|
|
|
.instance_init = tpm_backend_instance_init,
|
2017-09-29 13:10:14 +02:00
|
|
|
.instance_finalize = tpm_backend_instance_finalize,
|
2013-03-28 12:26:21 +01:00
|
|
|
.class_size = sizeof(TPMBackendClass),
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
2017-10-10 00:56:01 +02:00
|
|
|
static const TypeInfo tpm_if_info = {
|
|
|
|
.name = TYPE_TPM_IF,
|
|
|
|
.parent = TYPE_INTERFACE,
|
|
|
|
.class_size = sizeof(TPMIfClass),
|
|
|
|
};
|
|
|
|
|
2013-03-28 12:26:21 +01:00
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&tpm_backend_info);
|
2017-10-10 00:56:01 +02:00
|
|
|
type_register_static(&tpm_if_info);
|
2013-03-28 12:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|