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 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" 22 23 static void tpm_backend_request_completed_bh(void *opaque) 24 { 25 TPMBackend *s = TPM_BACKEND(opaque); 26 TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif); 27 28 tic->request_completed(s->tpmif); 29 } 30 31 static void tpm_backend_worker_thread(gpointer data, gpointer user_data) 32 { 33 TPMBackend *s = TPM_BACKEND(user_data); 34 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 35 36 assert(k->handle_request != NULL); 37 k->handle_request(s, (TPMBackendCmd *)data); 38 39 qemu_bh_schedule(s->bh); 40 } 41 42 static void tpm_backend_thread_end(TPMBackend *s) 43 { 44 if (s->thread_pool) { 45 g_thread_pool_free(s->thread_pool, FALSE, TRUE); 46 s->thread_pool = NULL; 47 } 48 } 49 50 enum TpmType tpm_backend_get_type(TPMBackend *s) 51 { 52 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 53 54 return k->type; 55 } 56 57 int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp) 58 { 59 if (s->tpmif) { 60 error_setg(errp, "TPM backend '%s' is already initialized", s->id); 61 return -1; 62 } 63 64 s->tpmif = tpmif; 65 object_ref(OBJECT(tpmif)); 66 67 s->had_startup_error = false; 68 69 return 0; 70 } 71 72 int tpm_backend_startup_tpm(TPMBackend *s) 73 { 74 int res = 0; 75 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 76 77 /* terminate a running TPM */ 78 tpm_backend_thread_end(s); 79 80 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, 81 NULL); 82 83 res = k->startup_tpm ? k->startup_tpm(s) : 0; 84 85 s->had_startup_error = (res != 0); 86 87 return res; 88 } 89 90 bool tpm_backend_had_startup_error(TPMBackend *s) 91 { 92 return s->had_startup_error; 93 } 94 95 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd) 96 { 97 g_thread_pool_push(s->thread_pool, cmd, NULL); 98 } 99 100 void tpm_backend_reset(TPMBackend *s) 101 { 102 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 103 104 if (k->reset) { 105 k->reset(s); 106 } 107 108 tpm_backend_thread_end(s); 109 110 s->had_startup_error = false; 111 } 112 113 void tpm_backend_cancel_cmd(TPMBackend *s) 114 { 115 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 116 117 assert(k->cancel_cmd); 118 119 k->cancel_cmd(s); 120 } 121 122 bool tpm_backend_get_tpm_established_flag(TPMBackend *s) 123 { 124 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 125 126 return k->get_tpm_established_flag ? 127 k->get_tpm_established_flag(s) : false; 128 } 129 130 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) 131 { 132 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 133 134 return k->reset_tpm_established_flag ? 135 k->reset_tpm_established_flag(s, locty) : 0; 136 } 137 138 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) 139 { 140 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 141 142 assert(k->get_tpm_version); 143 144 return k->get_tpm_version(s); 145 } 146 147 TPMInfo *tpm_backend_query_tpm(TPMBackend *s) 148 { 149 TPMInfo *info = g_new0(TPMInfo, 1); 150 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 151 152 info->id = g_strdup(s->id); 153 info->model = s->fe_model; 154 if (k->get_tpm_options) { 155 info->options = k->get_tpm_options(s); 156 } 157 158 return info; 159 } 160 161 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp) 162 { 163 TPMBackend *s = TPM_BACKEND(obj); 164 165 return s->opened; 166 } 167 168 void tpm_backend_open(TPMBackend *s, Error **errp) 169 { 170 object_property_set_bool(OBJECT(s), true, "opened", errp); 171 } 172 173 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp) 174 { 175 TPMBackend *s = TPM_BACKEND(obj); 176 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 177 Error *local_err = NULL; 178 179 if (value == s->opened) { 180 return; 181 } 182 183 if (!value && s->opened) { 184 error_setg(errp, QERR_PERMISSION_DENIED); 185 return; 186 } 187 188 if (k->opened) { 189 k->opened(s, &local_err); 190 if (local_err) { 191 error_propagate(errp, local_err); 192 return; 193 } 194 } 195 196 s->opened = true; 197 } 198 199 static void tpm_backend_instance_init(Object *obj) 200 { 201 TPMBackend *s = TPM_BACKEND(obj); 202 203 object_property_add_bool(obj, "opened", 204 tpm_backend_prop_get_opened, 205 tpm_backend_prop_set_opened, 206 NULL); 207 s->fe_model = -1; 208 s->bh = qemu_bh_new(tpm_backend_request_completed_bh, s); 209 } 210 211 static void tpm_backend_instance_finalize(Object *obj) 212 { 213 TPMBackend *s = TPM_BACKEND(obj); 214 215 object_unref(OBJECT(s->tpmif)); 216 g_free(s->id); 217 tpm_backend_thread_end(s); 218 qemu_bh_delete(s->bh); 219 } 220 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), 228 .abstract = true, 229 }; 230 231 static const TypeInfo tpm_if_info = { 232 .name = TYPE_TPM_IF, 233 .parent = TYPE_INTERFACE, 234 .class_size = sizeof(TPMIfClass), 235 }; 236 237 static void register_types(void) 238 { 239 type_register_static(&tpm_backend_info); 240 type_register_static(&tpm_if_info); 241 } 242 243 type_init(register_types); 244