4 * Copyright IBM, Corp. 2013
7 * Stefan Berger <stefanb@us.ibm.com>
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.
12 * Based on backends/rng.c by Anthony Liguori
15 #include "qemu/osdep.h"
16 #include "sysemu/tpm_backend.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "sysemu/tpm.h"
20 #include "qemu/thread.h"
21 #include "qemu/main-loop.h"
23 static void tpm_backend_request_completed_bh(void *opaque
)
25 TPMBackend
*s
= TPM_BACKEND(opaque
);
26 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
28 tic
->request_completed(s
->tpmif
);
31 static void tpm_backend_worker_thread(gpointer data
, gpointer user_data
)
33 TPMBackend
*s
= TPM_BACKEND(user_data
);
34 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
36 assert(k
->handle_request
!= NULL
);
37 k
->handle_request(s
, (TPMBackendCmd
*)data
);
39 qemu_bh_schedule(s
->bh
);
42 static void tpm_backend_thread_end(TPMBackend
*s
)
45 g_thread_pool_free(s
->thread_pool
, FALSE
, TRUE
);
46 s
->thread_pool
= NULL
;
50 enum TpmType
tpm_backend_get_type(TPMBackend
*s
)
52 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
57 int tpm_backend_init(TPMBackend
*s
, TPMIf
*tpmif
, Error
**errp
)
60 error_setg(errp
, "TPM backend '%s' is already initialized", s
->id
);
65 object_ref(OBJECT(tpmif
));
67 s
->had_startup_error
= false;
72 int tpm_backend_startup_tpm(TPMBackend
*s
)
75 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
77 /* terminate a running TPM */
78 tpm_backend_thread_end(s
);
80 s
->thread_pool
= g_thread_pool_new(tpm_backend_worker_thread
, s
, 1, TRUE
,
83 res
= k
->startup_tpm
? k
->startup_tpm(s
) : 0;
85 s
->had_startup_error
= (res
!= 0);
90 bool tpm_backend_had_startup_error(TPMBackend
*s
)
92 return s
->had_startup_error
;
95 void tpm_backend_deliver_request(TPMBackend
*s
, TPMBackendCmd
*cmd
)
97 g_thread_pool_push(s
->thread_pool
, cmd
, NULL
);
100 void tpm_backend_reset(TPMBackend
*s
)
102 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
108 tpm_backend_thread_end(s
);
110 s
->had_startup_error
= false;
113 void tpm_backend_cancel_cmd(TPMBackend
*s
)
115 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
117 assert(k
->cancel_cmd
);
122 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
124 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
126 return k
->get_tpm_established_flag
?
127 k
->get_tpm_established_flag(s
) : false;
130 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
132 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
134 return k
->reset_tpm_established_flag
?
135 k
->reset_tpm_established_flag(s
, locty
) : 0;
138 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
140 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
142 assert(k
->get_tpm_version
);
144 return k
->get_tpm_version(s
);
147 TPMInfo
*tpm_backend_query_tpm(TPMBackend
*s
)
149 TPMInfo
*info
= g_new0(TPMInfo
, 1);
150 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
151 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
153 info
->id
= g_strdup(s
->id
);
154 info
->model
= tic
->model
;
155 if (k
->get_tpm_options
) {
156 info
->options
= k
->get_tpm_options(s
);
162 static bool tpm_backend_prop_get_opened(Object
*obj
, Error
**errp
)
164 TPMBackend
*s
= TPM_BACKEND(obj
);
169 void tpm_backend_open(TPMBackend
*s
, Error
**errp
)
171 object_property_set_bool(OBJECT(s
), true, "opened", errp
);
174 static void tpm_backend_prop_set_opened(Object
*obj
, bool value
, Error
**errp
)
176 TPMBackend
*s
= TPM_BACKEND(obj
);
177 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
178 Error
*local_err
= NULL
;
180 if (value
== s
->opened
) {
184 if (!value
&& s
->opened
) {
185 error_setg(errp
, QERR_PERMISSION_DENIED
);
190 k
->opened(s
, &local_err
);
192 error_propagate(errp
, local_err
);
200 static void tpm_backend_instance_init(Object
*obj
)
202 TPMBackend
*s
= TPM_BACKEND(obj
);
204 object_property_add_bool(obj
, "opened",
205 tpm_backend_prop_get_opened
,
206 tpm_backend_prop_set_opened
,
208 s
->bh
= qemu_bh_new(tpm_backend_request_completed_bh
, s
);
211 static void tpm_backend_instance_finalize(Object
*obj
)
213 TPMBackend
*s
= TPM_BACKEND(obj
);
215 object_unref(OBJECT(s
->tpmif
));
217 tpm_backend_thread_end(s
);
218 qemu_bh_delete(s
->bh
);
221 static const TypeInfo tpm_backend_info
= {
222 .name
= TYPE_TPM_BACKEND
,
223 .parent
= TYPE_OBJECT
,
224 .instance_size
= sizeof(TPMBackend
),
225 .instance_init
= tpm_backend_instance_init
,
226 .instance_finalize
= tpm_backend_instance_finalize
,
227 .class_size
= sizeof(TPMBackendClass
),
231 static const TypeInfo tpm_if_info
= {
233 .parent
= TYPE_INTERFACE
,
234 .class_size
= sizeof(TPMIfClass
),
237 static void register_types(void)
239 type_register_static(&tpm_backend_info
);
240 type_register_static(&tpm_if_info
);
243 type_init(register_types
);