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-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"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-01-29 19:33:05 +01:00
|
|
|
#include "block/thread-pool.h"
|
|
|
|
#include "qemu/error-report.h"
|
2017-11-06 19:39:02 +01:00
|
|
|
|
2018-01-29 19:33:05 +01:00
|
|
|
static void tpm_backend_request_completed(void *opaque, int ret)
|
2017-11-06 19:39:02 +01:00
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(opaque);
|
|
|
|
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
|
|
|
|
|
2018-01-29 19:33:06 +01:00
|
|
|
tic->request_completed(s->tpmif, ret);
|
2018-01-29 19:33:05 +01:00
|
|
|
|
|
|
|
/* no need for atomic, as long the BQL is taken */
|
|
|
|
s->cmd = NULL;
|
|
|
|
object_unref(OBJECT(s));
|
2017-11-06 19:39:02 +01:00
|
|
|
}
|
2017-09-29 13:10:14 +02:00
|
|
|
|
2018-01-29 19:33:05 +01:00
|
|
|
static int tpm_backend_worker_thread(gpointer data)
|
2017-09-29 13:10:14 +02:00
|
|
|
{
|
2018-01-29 19:33:05 +01:00
|
|
|
TPMBackend *s = TPM_BACKEND(data);
|
2017-11-06 19:39:02 +01:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
2018-01-29 19:33:06 +01:00
|
|
|
Error *err = NULL;
|
2017-09-29 13:10:14 +02:00
|
|
|
|
2018-01-29 19:33:06 +01:00
|
|
|
k->handle_request(s, s->cmd, &err);
|
|
|
|
if (err) {
|
|
|
|
error_report_err(err);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-06 19:39:02 +01:00
|
|
|
|
2018-01-29 19:33:05 +01:00
|
|
|
return 0;
|
2017-09-29 13:10:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 19:33:05 +01:00
|
|
|
void tpm_backend_finish_sync(TPMBackend *s)
|
2017-09-29 13:10:14 +02:00
|
|
|
{
|
2018-01-29 19:33:05 +01:00
|
|
|
while (s->cmd) {
|
|
|
|
aio_poll(qemu_get_aio_context(), true);
|
2017-09-29 13:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-11-05 00:57:15 +01:00
|
|
|
int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize)
|
2013-03-28 12:26:21 +01:00
|
|
|
{
|
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 */
|
2018-01-29 19:33:05 +01:00
|
|
|
tpm_backend_finish_sync(s);
|
2017-09-29 13:10:14 +02:00
|
|
|
|
2017-11-05 00:57:15 +01:00
|
|
|
res = k->startup_tpm ? k->startup_tpm(s, buffersize) : 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
|
|
|
{
|
2018-01-29 19:33:05 +01:00
|
|
|
ThreadPool *pool = aio_get_thread_pool(qemu_get_aio_context());
|
|
|
|
|
|
|
|
if (s->cmd != NULL) {
|
|
|
|
error_report("There is a TPM request pending");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->cmd = cmd;
|
|
|
|
object_ref(OBJECT(s));
|
|
|
|
thread_pool_submit_aio(pool, tpm_backend_worker_thread, s,
|
|
|
|
tpm_backend_request_completed, s);
|
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
|
|
|
|
2018-01-29 19:33:05 +01:00
|
|
|
tpm_backend_finish_sync(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-11-03 23:10:01 +01:00
|
|
|
size_t tpm_backend_get_buffer_size(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
|
|
|
return k->get_buffer_size(s);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
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),
|
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);
|