]> git.proxmox.com Git - mirror_qemu.git/blame - backends/tpm.c
tpm-backend: Move thread handling inside TPMBackend
[mirror_qemu.git] / backends / tpm.c
CommitLineData
8f0605cc
SB
1/*
2 * QEMU TPM Backend
3 *
4 * Copyright IBM, Corp. 2013
5 *
6 * Authors:
7 * Stefan Berger <stefanb@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 * Based on backends/rng.c by Anthony Liguori
13 */
14
9c058332 15#include "qemu/osdep.h"
dccfcd0e 16#include "sysemu/tpm_backend.h"
da34e65c 17#include "qapi/error.h"
8f0605cc 18#include "qapi/qmp/qerror.h"
bdee56f5
PB
19#include "sysemu/tpm.h"
20#include "qemu/thread.h"
b19a5eea
AV
21
22static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
23{
24 TPMBackend *s = TPM_BACKEND(user_data);
25 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
26
27 assert(k->handle_request != NULL);
28 k->handle_request(s, (TPMBackendCmd)data);
29}
30
31static void tpm_backend_thread_end(TPMBackend *s)
32{
33 if (s->thread_pool) {
34 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
35 g_thread_pool_free(s->thread_pool, FALSE, TRUE);
36 s->thread_pool = NULL;
37 }
38}
8f0605cc
SB
39
40enum TpmType tpm_backend_get_type(TPMBackend *s)
41{
42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
43
44 return k->ops->type;
45}
46
47const char *tpm_backend_get_desc(TPMBackend *s)
48{
49 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
50
51 return k->ops->desc();
52}
53
54void tpm_backend_destroy(TPMBackend *s)
55{
56 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
57
9c80d953 58 k->ops->destroy(s);
b19a5eea
AV
59
60 tpm_backend_thread_end(s);
8f0605cc
SB
61}
62
63int tpm_backend_init(TPMBackend *s, TPMState *state,
64 TPMRecvDataCB *datacb)
65{
66 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
67
b19a5eea
AV
68 s->tpm_state = state;
69 s->recv_data_callback = datacb;
70
71 return k->ops->init(s);
8f0605cc
SB
72}
73
74int tpm_backend_startup_tpm(TPMBackend *s)
75{
76 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
77
b19a5eea
AV
78 /* terminate a running TPM */
79 tpm_backend_thread_end(s);
80
81 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
82 NULL);
83 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
84
8f0605cc
SB
85 return k->ops->startup_tpm(s);
86}
87
88bool tpm_backend_had_startup_error(TPMBackend *s)
89{
90 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
91
92 return k->ops->had_startup_error(s);
93}
94
95size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
96{
97 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
98
99 return k->ops->realloc_buffer(sb);
100}
101
102void tpm_backend_deliver_request(TPMBackend *s)
103{
b19a5eea
AV
104 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
105 NULL);
8f0605cc
SB
106}
107
108void tpm_backend_reset(TPMBackend *s)
109{
110 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
111
112 k->ops->reset(s);
b19a5eea
AV
113
114 tpm_backend_thread_end(s);
8f0605cc
SB
115}
116
117void tpm_backend_cancel_cmd(TPMBackend *s)
118{
119 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
120
121 k->ops->cancel_cmd(s);
122}
123
124bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
125{
126 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
127
128 return k->ops->get_tpm_established_flag(s);
129}
130
116694c3
SB
131int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
132{
133 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
134
135 return k->ops->reset_tpm_established_flag(s, locty);
136}
137
138TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
139{
140 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
141
142 return k->ops->get_tpm_version(s);
143}
144
8f0605cc
SB
145static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
146{
147 TPMBackend *s = TPM_BACKEND(obj);
148
149 return s->opened;
150}
151
152void tpm_backend_open(TPMBackend *s, Error **errp)
153{
154 object_property_set_bool(OBJECT(s), true, "opened", errp);
155}
156
157static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
158{
159 TPMBackend *s = TPM_BACKEND(obj);
160 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
65cd9064 161 Error *local_err = NULL;
8f0605cc
SB
162
163 if (value == s->opened) {
164 return;
165 }
166
167 if (!value && s->opened) {
c6bd8c70 168 error_setg(errp, QERR_PERMISSION_DENIED);
8f0605cc
SB
169 return;
170 }
171
172 if (k->opened) {
65cd9064
MA
173 k->opened(s, &local_err);
174 if (local_err) {
175 error_propagate(errp, local_err);
176 return;
177 }
8f0605cc
SB
178 }
179
65cd9064 180 s->opened = true;
8f0605cc
SB
181}
182
183static void tpm_backend_instance_init(Object *obj)
184{
185 object_property_add_bool(obj, "opened",
186 tpm_backend_prop_get_opened,
187 tpm_backend_prop_set_opened,
188 NULL);
8f0605cc 189
bdee56f5
PB
190}
191
b19a5eea 192static void tpm_backend_instance_finalize(Object *obj)
bdee56f5 193{
b19a5eea 194 TPMBackend *s = TPM_BACKEND(obj);
bdee56f5 195
b19a5eea 196 tpm_backend_thread_end(s);
bdee56f5
PB
197}
198
8f0605cc
SB
199static const TypeInfo tpm_backend_info = {
200 .name = TYPE_TPM_BACKEND,
201 .parent = TYPE_OBJECT,
202 .instance_size = sizeof(TPMBackend),
203 .instance_init = tpm_backend_instance_init,
b19a5eea 204 .instance_finalize = tpm_backend_instance_finalize,
8f0605cc
SB
205 .class_size = sizeof(TPMBackendClass),
206 .abstract = true,
207};
208
209static void register_types(void)
210{
211 type_register_static(&tpm_backend_info);
212}
213
214type_init(register_types);