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