Merge remote-tracking branch 'remotes/vivier2/tags/linux-user-for-2.12-pull-request...
[qemu/ar7.git] / hw / tpm / tpm_emulator.c
blob6418ef08310f401de5fea9c429a4c97bfd21e41f
1 /*
2 * Emulator TPM driver
4 * Copyright (c) 2017 Intel Corporation
5 * Author: Amarnath Valluri <amarnath.valluri@intel.com>
7 * Copyright (c) 2010 - 2013 IBM Corporation
8 * Authors:
9 * Stefan Berger <stefanb@us.ibm.com>
11 * Copyright (C) 2011 IAIK, Graz University of Technology
12 * Author: Andreas Niederl
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, see <http://www.gnu.org/licenses/>
29 #include "qemu/osdep.h"
30 #include "qemu/error-report.h"
31 #include "qemu/sockets.h"
32 #include "io/channel-socket.h"
33 #include "sysemu/tpm_backend.h"
34 #include "tpm_int.h"
35 #include "hw/hw.h"
36 #include "tpm_util.h"
37 #include "tpm_ioctl.h"
38 #include "migration/blocker.h"
39 #include "qapi/error.h"
40 #include "qapi/clone-visitor.h"
41 #include "qapi/qapi-visit-tpm.h"
42 #include "chardev/char-fe.h"
43 #include "trace.h"
45 #define TYPE_TPM_EMULATOR "tpm-emulator"
46 #define TPM_EMULATOR(obj) \
47 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
49 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
51 /* data structures */
52 typedef struct TPMEmulator {
53 TPMBackend parent;
55 TPMEmulatorOptions *options;
56 CharBackend ctrl_chr;
57 QIOChannel *data_ioc;
58 TPMVersion tpm_version;
59 ptm_cap caps; /* capabilities of the TPM */
60 uint8_t cur_locty_number; /* last set locality */
61 Error *migration_blocker;
63 QemuMutex mutex;
65 unsigned int established_flag:1;
66 unsigned int established_flag_cached:1;
67 } TPMEmulator;
70 static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
71 size_t msg_len_in, size_t msg_len_out)
73 CharBackend *dev = &tpm->ctrl_chr;
74 uint32_t cmd_no = cpu_to_be32(cmd);
75 ssize_t n = sizeof(uint32_t) + msg_len_in;
76 uint8_t *buf = NULL;
77 int ret = -1;
79 qemu_mutex_lock(&tpm->mutex);
81 buf = g_alloca(n);
82 memcpy(buf, &cmd_no, sizeof(cmd_no));
83 memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
85 n = qemu_chr_fe_write_all(dev, buf, n);
86 if (n <= 0) {
87 goto end;
90 if (msg_len_out != 0) {
91 n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
92 if (n <= 0) {
93 goto end;
97 ret = 0;
99 end:
100 qemu_mutex_unlock(&tpm->mutex);
101 return ret;
104 static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
105 const uint8_t *in, uint32_t in_len,
106 uint8_t *out, uint32_t out_len,
107 bool *selftest_done,
108 Error **err)
110 ssize_t ret;
111 bool is_selftest = false;
113 if (selftest_done) {
114 *selftest_done = false;
115 is_selftest = tpm_util_is_selftest(in, in_len);
118 ret = qio_channel_write_all(tpm_emu->data_ioc, (char *)in, in_len, err);
119 if (ret != 0) {
120 return -1;
123 ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out,
124 sizeof(struct tpm_resp_hdr), err);
125 if (ret != 0) {
126 return -1;
129 ret = qio_channel_read_all(tpm_emu->data_ioc,
130 (char *)out + sizeof(struct tpm_resp_hdr),
131 tpm_cmd_get_size(out) - sizeof(struct tpm_resp_hdr), err);
132 if (ret != 0) {
133 return -1;
136 if (is_selftest) {
137 *selftest_done = tpm_cmd_get_errcode(out) == 0;
140 return 0;
143 static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
144 Error **errp)
146 ptm_loc loc;
148 if (tpm_emu->cur_locty_number == locty_number) {
149 return 0;
152 trace_tpm_emulator_set_locality(locty_number);
154 loc.u.req.loc = locty_number;
155 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_LOCALITY, &loc,
156 sizeof(loc), sizeof(loc)) < 0) {
157 error_setg(errp, "tpm-emulator: could not set locality : %s",
158 strerror(errno));
159 return -1;
162 loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result);
163 if (loc.u.resp.tpm_result != 0) {
164 error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x",
165 loc.u.resp.tpm_result);
166 return -1;
169 tpm_emu->cur_locty_number = locty_number;
171 return 0;
174 static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
175 Error **errp)
177 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
179 trace_tpm_emulator_handle_request();
181 if (tpm_emulator_set_locality(tpm_emu, cmd->locty, errp) < 0 ||
182 tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
183 cmd->out, cmd->out_len,
184 &cmd->selftest_done, errp) < 0) {
185 tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
189 static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu)
191 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_CAPABILITY,
192 &tpm_emu->caps, 0, sizeof(tpm_emu->caps)) < 0) {
193 error_report("tpm-emulator: probing failed : %s", strerror(errno));
194 return -1;
197 tpm_emu->caps = be64_to_cpu(tpm_emu->caps);
199 trace_tpm_emulator_probe_caps(tpm_emu->caps);
201 return 0;
204 static int tpm_emulator_check_caps(TPMEmulator *tpm_emu)
206 ptm_cap caps = 0;
207 const char *tpm = NULL;
209 /* check for min. required capabilities */
210 switch (tpm_emu->tpm_version) {
211 case TPM_VERSION_1_2:
212 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
213 PTM_CAP_SET_LOCALITY | PTM_CAP_SET_DATAFD | PTM_CAP_STOP |
214 PTM_CAP_SET_BUFFERSIZE;
215 tpm = "1.2";
216 break;
217 case TPM_VERSION_2_0:
218 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
219 PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED |
220 PTM_CAP_SET_DATAFD | PTM_CAP_STOP | PTM_CAP_SET_BUFFERSIZE;
221 tpm = "2";
222 break;
223 case TPM_VERSION_UNSPEC:
224 error_report("tpm-emulator: TPM version has not been set");
225 return -1;
228 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
229 error_report("tpm-emulator: TPM does not implement minimum set of "
230 "required capabilities for TPM %s (0x%x)", tpm, (int)caps);
231 return -1;
234 return 0;
237 static int tpm_emulator_stop_tpm(TPMBackend *tb)
239 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
240 ptm_res res;
242 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_STOP, &res, 0, sizeof(res)) < 0) {
243 error_report("tpm-emulator: Could not stop TPM: %s",
244 strerror(errno));
245 return -1;
248 res = be32_to_cpu(res);
249 if (res) {
250 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res);
251 return -1;
254 return 0;
257 static int tpm_emulator_set_buffer_size(TPMBackend *tb,
258 size_t wanted_size,
259 size_t *actual_size)
261 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
262 ptm_setbuffersize psbs;
264 if (tpm_emulator_stop_tpm(tb) < 0) {
265 return -1;
268 psbs.u.req.buffersize = cpu_to_be32(wanted_size);
270 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_BUFFERSIZE, &psbs,
271 sizeof(psbs.u.req), sizeof(psbs.u.resp)) < 0) {
272 error_report("tpm-emulator: Could not set buffer size: %s",
273 strerror(errno));
274 return -1;
277 psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result);
278 if (psbs.u.resp.tpm_result != 0) {
279 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
280 psbs.u.resp.tpm_result);
281 return -1;
284 if (actual_size) {
285 *actual_size = be32_to_cpu(psbs.u.resp.buffersize);
288 trace_tpm_emulator_set_buffer_size(
289 be32_to_cpu(psbs.u.resp.buffersize),
290 be32_to_cpu(psbs.u.resp.minsize),
291 be32_to_cpu(psbs.u.resp.maxsize));
293 return 0;
296 static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
298 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
299 ptm_init init = {
300 .u.req.init_flags = 0,
302 ptm_res res;
304 if (buffersize != 0 &&
305 tpm_emulator_set_buffer_size(tb, buffersize, NULL) < 0) {
306 goto err_exit;
309 trace_tpm_emulator_startup_tpm();
310 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_INIT, &init, sizeof(init),
311 sizeof(init)) < 0) {
312 error_report("tpm-emulator: could not send INIT: %s",
313 strerror(errno));
314 goto err_exit;
317 res = be32_to_cpu(init.u.resp.tpm_result);
318 if (res) {
319 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res);
320 goto err_exit;
322 return 0;
324 err_exit:
325 return -1;
328 static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
330 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
331 ptm_est est;
333 if (tpm_emu->established_flag_cached) {
334 return tpm_emu->established_flag;
337 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
338 0, sizeof(est)) < 0) {
339 error_report("tpm-emulator: Could not get the TPM established flag: %s",
340 strerror(errno));
341 return false;
343 trace_tpm_emulator_get_tpm_established_flag(est.u.resp.bit);
345 tpm_emu->established_flag_cached = 1;
346 tpm_emu->established_flag = (est.u.resp.bit != 0);
348 return tpm_emu->established_flag;
351 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
352 uint8_t locty)
354 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
355 ptm_reset_est reset_est;
356 ptm_res res;
358 /* only a TPM 2.0 will support this */
359 if (tpm_emu->tpm_version != TPM_VERSION_2_0) {
360 return 0;
363 reset_est.u.req.loc = tpm_emu->cur_locty_number;
364 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_RESET_TPMESTABLISHED,
365 &reset_est, sizeof(reset_est),
366 sizeof(reset_est)) < 0) {
367 error_report("tpm-emulator: Could not reset the establishment bit: %s",
368 strerror(errno));
369 return -1;
372 res = be32_to_cpu(reset_est.u.resp.tpm_result);
373 if (res) {
374 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
375 res);
376 return -1;
379 tpm_emu->established_flag_cached = 0;
381 return 0;
384 static void tpm_emulator_cancel_cmd(TPMBackend *tb)
386 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
387 ptm_res res;
389 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, PTM_CAP_CANCEL_TPM_CMD)) {
390 trace_tpm_emulator_cancel_cmd_not_supt();
391 return;
394 /* FIXME: make the function non-blocking, or it may block a VCPU */
395 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_CANCEL_TPM_CMD, &res, 0,
396 sizeof(res)) < 0) {
397 error_report("tpm-emulator: Could not cancel command: %s",
398 strerror(errno));
399 } else if (res != 0) {
400 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
401 be32_to_cpu(res));
405 static TPMVersion tpm_emulator_get_tpm_version(TPMBackend *tb)
407 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
409 return tpm_emu->tpm_version;
412 static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)
414 size_t actual_size;
416 if (tpm_emulator_set_buffer_size(tb, 0, &actual_size) < 0) {
417 return 4096;
420 return actual_size;
423 static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
425 Error *err = NULL;
427 error_setg(&tpm_emu->migration_blocker,
428 "Migration disabled: TPM emulator not yet migratable");
429 migrate_add_blocker(tpm_emu->migration_blocker, &err);
430 if (err) {
431 error_report_err(err);
432 error_free(tpm_emu->migration_blocker);
433 tpm_emu->migration_blocker = NULL;
435 return -1;
438 return 0;
441 static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
443 ptm_res res;
444 Error *err = NULL;
445 int fds[2] = { -1, -1 };
447 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
448 error_report("tpm-emulator: Failed to create socketpair");
449 return -1;
452 qemu_chr_fe_set_msgfds(&tpm_emu->ctrl_chr, fds + 1, 1);
454 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_DATAFD, &res, 0,
455 sizeof(res)) < 0 || res != 0) {
456 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
457 strerror(errno));
458 goto err_exit;
461 tpm_emu->data_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(fds[0], &err));
462 if (err) {
463 error_prepend(&err, "tpm-emulator: Failed to create io channel: ");
464 error_report_err(err);
465 goto err_exit;
468 closesocket(fds[1]);
470 return 0;
472 err_exit:
473 closesocket(fds[0]);
474 closesocket(fds[1]);
475 return -1;
478 static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
480 const char *value;
482 value = qemu_opt_get(opts, "chardev");
483 if (value) {
484 Error *err = NULL;
485 Chardev *dev = qemu_chr_find(value);
487 if (!dev) {
488 error_report("tpm-emulator: tpm chardev '%s' not found.", value);
489 goto err;
492 if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
493 error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
494 value);
495 error_report_err(err);
496 goto err;
499 tpm_emu->options->chardev = g_strdup(value);
502 if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
503 goto err;
506 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
507 * by passthrough driver, which not yet using GIOChannel.
509 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu->data_ioc)->fd,
510 &tpm_emu->tpm_version)) {
511 error_report("'%s' is not emulating TPM device. Error: %s",
512 tpm_emu->options->chardev, strerror(errno));
513 goto err;
516 switch (tpm_emu->tpm_version) {
517 case TPM_VERSION_1_2:
518 trace_tpm_emulator_handle_device_opts_tpm12();
519 break;
520 case TPM_VERSION_2_0:
521 trace_tpm_emulator_handle_device_opts_tpm2();
522 break;
523 default:
524 trace_tpm_emulator_handle_device_opts_unspec();
527 if (tpm_emulator_probe_caps(tpm_emu) ||
528 tpm_emulator_check_caps(tpm_emu)) {
529 goto err;
532 return tpm_emulator_block_migration(tpm_emu);
534 err:
535 trace_tpm_emulator_handle_device_opts_startup_error();
537 return -1;
540 static TPMBackend *tpm_emulator_create(QemuOpts *opts)
542 TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
544 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb), opts)) {
545 object_unref(OBJECT(tb));
546 return NULL;
549 return tb;
552 static TpmTypeOptions *tpm_emulator_get_tpm_options(TPMBackend *tb)
554 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
555 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
557 options->type = TPM_TYPE_OPTIONS_KIND_EMULATOR;
558 options->u.emulator.data = QAPI_CLONE(TPMEmulatorOptions, tpm_emu->options);
560 return options;
563 static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
564 TPM_STANDARD_CMDLINE_OPTS,
566 .name = "chardev",
567 .type = QEMU_OPT_STRING,
568 .help = "Character device to use for out-of-band control messages",
570 { /* end of list */ },
573 static void tpm_emulator_inst_init(Object *obj)
575 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
577 trace_tpm_emulator_inst_init();
579 tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
580 tpm_emu->cur_locty_number = ~0;
581 qemu_mutex_init(&tpm_emu->mutex);
585 * Gracefully shut down the external TPM
587 static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
589 ptm_res res;
591 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
592 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
593 strerror(errno));
594 } else if (res != 0) {
595 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
596 be32_to_cpu(res));
600 static void tpm_emulator_inst_finalize(Object *obj)
602 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
604 tpm_emulator_shutdown(tpm_emu);
606 object_unref(OBJECT(tpm_emu->data_ioc));
608 qemu_chr_fe_deinit(&tpm_emu->ctrl_chr, false);
610 qapi_free_TPMEmulatorOptions(tpm_emu->options);
612 if (tpm_emu->migration_blocker) {
613 migrate_del_blocker(tpm_emu->migration_blocker);
614 error_free(tpm_emu->migration_blocker);
617 qemu_mutex_destroy(&tpm_emu->mutex);
620 static void tpm_emulator_class_init(ObjectClass *klass, void *data)
622 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
624 tbc->type = TPM_TYPE_EMULATOR;
625 tbc->opts = tpm_emulator_cmdline_opts;
626 tbc->desc = "TPM emulator backend driver";
627 tbc->create = tpm_emulator_create;
628 tbc->startup_tpm = tpm_emulator_startup_tpm;
629 tbc->cancel_cmd = tpm_emulator_cancel_cmd;
630 tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
631 tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
632 tbc->get_tpm_version = tpm_emulator_get_tpm_version;
633 tbc->get_buffer_size = tpm_emulator_get_buffer_size;
634 tbc->get_tpm_options = tpm_emulator_get_tpm_options;
636 tbc->handle_request = tpm_emulator_handle_request;
639 static const TypeInfo tpm_emulator_info = {
640 .name = TYPE_TPM_EMULATOR,
641 .parent = TYPE_TPM_BACKEND,
642 .instance_size = sizeof(TPMEmulator),
643 .class_init = tpm_emulator_class_init,
644 .instance_init = tpm_emulator_inst_init,
645 .instance_finalize = tpm_emulator_inst_finalize,
648 static void tpm_emulator_register(void)
650 type_register_static(&tpm_emulator_info);
653 type_init(tpm_emulator_register)