4 * Copyright (c) 2017 Intel Corporation
5 * Author: Amarnath Valluri <amarnath.valluri@intel.com>
7 * Copyright (c) 2010 - 2013 IBM Corporation
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"
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"
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))
52 typedef struct TPMEmulator
{
55 TPMEmulatorOptions
*options
;
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
;
65 unsigned int established_flag
:1;
66 unsigned int established_flag_cached
:1;
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
;
79 qemu_mutex_lock(&tpm
->mutex
);
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
);
90 if (msg_len_out
!= 0) {
91 n
= qemu_chr_fe_read_all(dev
, msg
, msg_len_out
);
100 qemu_mutex_unlock(&tpm
->mutex
);
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
,
111 bool is_selftest
= false;
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
);
123 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
,
124 sizeof(struct tpm_resp_hdr
), err
);
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
);
137 *selftest_done
= tpm_cmd_get_errcode(out
) == 0;
143 static int tpm_emulator_set_locality(TPMEmulator
*tpm_emu
, uint8_t locty_number
,
148 if (tpm_emu
->cur_locty_number
== locty_number
) {
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",
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
);
169 tpm_emu
->cur_locty_number
= locty_number
;
174 static void tpm_emulator_handle_request(TPMBackend
*tb
, TPMBackendCmd
*cmd
,
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
));
197 tpm_emu
->caps
= be64_to_cpu(tpm_emu
->caps
);
199 trace_tpm_emulator_probe_caps(tpm_emu
->caps
);
204 static int tpm_emulator_check_caps(TPMEmulator
*tpm_emu
)
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
;
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
;
223 case TPM_VERSION_UNSPEC
:
224 error_report("tpm-emulator: TPM version has not been set");
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
);
237 static int tpm_emulator_stop_tpm(TPMBackend
*tb
)
239 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
242 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_STOP
, &res
, 0, sizeof(res
)) < 0) {
243 error_report("tpm-emulator: Could not stop TPM: %s",
248 res
= be32_to_cpu(res
);
250 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res
);
257 static int tpm_emulator_set_buffer_size(TPMBackend
*tb
,
261 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
262 ptm_setbuffersize psbs
;
264 if (tpm_emulator_stop_tpm(tb
) < 0) {
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",
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
);
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
));
296 static int tpm_emulator_startup_tpm(TPMBackend
*tb
, size_t buffersize
)
298 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
300 .u
.req
.init_flags
= 0,
304 if (buffersize
!= 0 &&
305 tpm_emulator_set_buffer_size(tb
, buffersize
, NULL
) < 0) {
309 trace_tpm_emulator_startup_tpm();
310 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_INIT
, &init
, sizeof(init
),
312 error_report("tpm-emulator: could not send INIT: %s",
317 res
= be32_to_cpu(init
.u
.resp
.tpm_result
);
319 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res
);
328 static bool tpm_emulator_get_tpm_established_flag(TPMBackend
*tb
)
330 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
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",
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
,
354 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
355 ptm_reset_est reset_est
;
358 /* only a TPM 2.0 will support this */
359 if (tpm_emu
->tpm_version
!= TPM_VERSION_2_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",
372 res
= be32_to_cpu(reset_est
.u
.resp
.tpm_result
);
374 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
379 tpm_emu
->established_flag_cached
= 0;
384 static void tpm_emulator_cancel_cmd(TPMBackend
*tb
)
386 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
389 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, PTM_CAP_CANCEL_TPM_CMD
)) {
390 trace_tpm_emulator_cancel_cmd_not_supt();
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,
397 error_report("tpm-emulator: Could not cancel command: %s",
399 } else if (res
!= 0) {
400 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
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
)
416 if (tpm_emulator_set_buffer_size(tb
, 0, &actual_size
) < 0) {
423 static int tpm_emulator_block_migration(TPMEmulator
*tpm_emu
)
427 error_setg(&tpm_emu
->migration_blocker
,
428 "Migration disabled: TPM emulator not yet migratable");
429 migrate_add_blocker(tpm_emu
->migration_blocker
, &err
);
431 error_report_err(err
);
432 error_free(tpm_emu
->migration_blocker
);
433 tpm_emu
->migration_blocker
= NULL
;
441 static int tpm_emulator_prepare_data_fd(TPMEmulator
*tpm_emu
)
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");
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",
461 tpm_emu
->data_ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(fds
[0], &err
));
463 error_prepend(&err
, "tpm-emulator: Failed to create io channel: ");
464 error_report_err(err
);
478 static int tpm_emulator_handle_device_opts(TPMEmulator
*tpm_emu
, QemuOpts
*opts
)
482 value
= qemu_opt_get(opts
, "chardev");
485 Chardev
*dev
= qemu_chr_find(value
);
488 error_report("tpm-emulator: tpm chardev '%s' not found.", value
);
492 if (!qemu_chr_fe_init(&tpm_emu
->ctrl_chr
, dev
, &err
)) {
493 error_prepend(&err
, "tpm-emulator: No valid chardev found at '%s':",
495 error_report_err(err
);
499 tpm_emu
->options
->chardev
= g_strdup(value
);
502 if (tpm_emulator_prepare_data_fd(tpm_emu
) < 0) {
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
));
516 switch (tpm_emu
->tpm_version
) {
517 case TPM_VERSION_1_2
:
518 trace_tpm_emulator_handle_device_opts_tpm12();
520 case TPM_VERSION_2_0
:
521 trace_tpm_emulator_handle_device_opts_tpm2();
524 trace_tpm_emulator_handle_device_opts_unspec();
527 if (tpm_emulator_probe_caps(tpm_emu
) ||
528 tpm_emulator_check_caps(tpm_emu
)) {
532 return tpm_emulator_block_migration(tpm_emu
);
535 trace_tpm_emulator_handle_device_opts_startup_error();
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
));
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
);
563 static const QemuOptDesc tpm_emulator_cmdline_opts
[] = {
564 TPM_STANDARD_CMDLINE_OPTS
,
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
)
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",
594 } else if (res
!= 0) {
595 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
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
)