2013-03-28 15:26:21 +04: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 20:49:54 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-08 18:55:25 +04: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 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2013-04-02 20:28:41 +04:00
|
|
|
#include "sysemu/tpm.h"
|
|
|
|
#include "qemu/thread.h"
|
2017-11-06 21:39:02 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2018-01-29 21:33:05 +03:00
|
|
|
#include "block/thread-pool.h"
|
|
|
|
#include "qemu/error-report.h"
|
2017-11-06 21:39:02 +03:00
|
|
|
|
2018-01-29 21:33:05 +03:00
|
|
|
static void tpm_backend_request_completed(void *opaque, int ret)
|
2017-11-06 21:39:02 +03:00
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(opaque);
|
|
|
|
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
|
|
|
|
|
2018-01-29 21:33:06 +03:00
|
|
|
tic->request_completed(s->tpmif, ret);
|
2018-01-29 21:33:05 +03:00
|
|
|
|
|
|
|
/* no need for atomic, as long the BQL is taken */
|
|
|
|
s->cmd = NULL;
|
|
|
|
object_unref(OBJECT(s));
|
2017-11-06 21:39:02 +03:00
|
|
|
}
|
2017-09-29 14:10:14 +03:00
|
|
|
|
2018-01-29 21:33:05 +03:00
|
|
|
static int tpm_backend_worker_thread(gpointer data)
|
2017-09-29 14:10:14 +03:00
|
|
|
{
|
2018-01-29 21:33:05 +03:00
|
|
|
TPMBackend *s = TPM_BACKEND(data);
|
2017-11-06 21:39:02 +03:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
2018-01-29 21:33:06 +03:00
|
|
|
Error *err = NULL;
|
2017-09-29 14:10:14 +03:00
|
|
|
|
2018-01-29 21:33:06 +03:00
|
|
|
k->handle_request(s, s->cmd, &err);
|
|
|
|
if (err) {
|
|
|
|
error_report_err(err);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-06 21:39:02 +03:00
|
|
|
|
2018-01-29 21:33:05 +03:00
|
|
|
return 0;
|
2017-09-29 14:10:14 +03:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:33:05 +03:00
|
|
|
void tpm_backend_finish_sync(TPMBackend *s)
|
2017-09-29 14:10:14 +03:00
|
|
|
{
|
2018-01-29 21:33:05 +03:00
|
|
|
while (s->cmd) {
|
|
|
|
aio_poll(qemu_get_aio_context(), true);
|
2017-09-29 14:10:14 +03:00
|
|
|
}
|
|
|
|
}
|
2013-03-28 15:26:21 +04:00
|
|
|
|
|
|
|
enum TpmType tpm_backend_get_type(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->type;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2017-11-06 21:39:03 +03:00
|
|
|
int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp)
|
2013-03-28 15:26:21 +04:00
|
|
|
{
|
2017-11-06 21:39:00 +03:00
|
|
|
if (s->tpmif) {
|
2017-11-06 21:39:03 +03:00
|
|
|
error_setg(errp, "TPM backend '%s' is already initialized", s->id);
|
2017-11-06 21:39:00 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->tpmif = tpmif;
|
|
|
|
object_ref(OBJECT(tpmif));
|
|
|
|
|
2017-09-29 14:10:16 +03:00
|
|
|
s->had_startup_error = false;
|
2017-09-29 14:10:14 +03:00
|
|
|
|
2017-10-10 01:55:50 +03:00
|
|
|
return 0;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2017-11-05 02:57:15 +03:00
|
|
|
int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize)
|
2013-03-28 15:26:21 +04:00
|
|
|
{
|
2017-09-29 14:10:16 +03:00
|
|
|
int res = 0;
|
2013-03-28 15:26:21 +04:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-09-29 14:10:14 +03:00
|
|
|
/* terminate a running TPM */
|
2018-01-29 21:33:05 +03:00
|
|
|
tpm_backend_finish_sync(s);
|
2017-09-29 14:10:14 +03:00
|
|
|
|
2017-11-05 02:57:15 +03:00
|
|
|
res = k->startup_tpm ? k->startup_tpm(s, buffersize) : 0;
|
2017-09-29 14:10:16 +03:00
|
|
|
|
|
|
|
s->had_startup_error = (res != 0);
|
|
|
|
|
|
|
|
return res;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_had_startup_error(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 14:10:16 +03:00
|
|
|
return s->had_startup_error;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2017-10-10 01:55:55 +03:00
|
|
|
void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
|
2013-03-28 15:26:21 +04:00
|
|
|
{
|
2018-01-29 21:33:05 +03:00
|
|
|
if (s->cmd != NULL) {
|
|
|
|
error_report("There is a TPM request pending");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->cmd = cmd;
|
|
|
|
object_ref(OBJECT(s));
|
2023-02-03 16:17:31 +03:00
|
|
|
thread_pool_submit_aio(tpm_backend_worker_thread, s,
|
2018-01-29 21:33:05 +03:00
|
|
|
tpm_backend_request_completed, s);
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_reset(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
if (k->reset) {
|
|
|
|
k->reset(s);
|
2017-09-29 14:10:16 +03:00
|
|
|
}
|
2017-09-29 14:10:14 +03:00
|
|
|
|
2018-01-29 21:33:05 +03:00
|
|
|
tpm_backend_finish_sync(s);
|
2017-09-29 14:10:16 +03:00
|
|
|
|
|
|
|
s->had_startup_error = false;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_cancel_cmd(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
k->cancel_cmd(s);
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->get_tpm_established_flag ?
|
|
|
|
k->get_tpm_established_flag(s) : false;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2015-05-26 23:51:05 +03:00
|
|
|
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->reset_tpm_established_flag ?
|
|
|
|
k->reset_tpm_established_flag(s, locty) : 0;
|
2015-05-26 23:51:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->get_tpm_version(s);
|
2015-05-26 23:51:05 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 01:10:01 +03: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 14:10:17 +03:00
|
|
|
TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMInfo *info = g_new0(TPMInfo, 1);
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
2017-11-06 21:39:04 +03:00
|
|
|
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
|
2017-09-29 14:10:17 +03:00
|
|
|
|
|
|
|
info->id = g_strdup(s->id);
|
2017-11-06 21:39:04 +03:00
|
|
|
info->model = tic->model;
|
2017-11-06 21:39:07 +03:00
|
|
|
info->options = k->get_tpm_options(s);
|
2017-09-29 14:10:17 +03:00
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-09-29 14:10:14 +03:00
|
|
|
static void tpm_backend_instance_finalize(Object *obj)
|
2013-04-02 20:28:41 +04:00
|
|
|
{
|
2017-09-29 14:10:14 +03:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
2013-04-02 20:28:41 +04:00
|
|
|
|
2017-11-06 21:39:00 +03:00
|
|
|
object_unref(OBJECT(s->tpmif));
|
2017-09-29 14:10:15 +03:00
|
|
|
g_free(s->id);
|
2013-04-02 20:28:41 +04:00
|
|
|
}
|
|
|
|
|
2013-03-28 15:26:21 +04:00
|
|
|
static const TypeInfo tpm_backend_info = {
|
|
|
|
.name = TYPE_TPM_BACKEND,
|
|
|
|
.parent = TYPE_OBJECT,
|
|
|
|
.instance_size = sizeof(TPMBackend),
|
2017-09-29 14:10:14 +03:00
|
|
|
.instance_finalize = tpm_backend_instance_finalize,
|
2013-03-28 15:26:21 +04:00
|
|
|
.class_size = sizeof(TPMBackendClass),
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
2017-10-10 01:56:01 +03:00
|
|
|
static const TypeInfo tpm_if_info = {
|
|
|
|
.name = TYPE_TPM_IF,
|
|
|
|
.parent = TYPE_INTERFACE,
|
|
|
|
.class_size = sizeof(TPMIfClass),
|
|
|
|
};
|
|
|
|
|
2013-03-28 15:26:21 +04:00
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&tpm_backend_info);
|
2017-10-10 01:56:01 +03:00
|
|
|
type_register_static(&tpm_if_info);
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|