4 * Copyright (c) 2017 Intel Corporation
5 * Author: Amarnath Valluri <amarnath.valluri@intel.com>
7 * Copyright (c) 2010 - 2013, 2018 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/module.h"
32 #include "qemu/sockets.h"
33 #include "io/channel-socket.h"
34 #include "sysemu/tpm_backend.h"
38 #include "tpm_ioctl.h"
39 #include "migration/blocker.h"
40 #include "qapi/error.h"
41 #include "qapi/clone-visitor.h"
42 #include "qapi/qapi-visit-tpm.h"
43 #include "chardev/char-fe.h"
46 #define TYPE_TPM_EMULATOR "tpm-emulator"
47 #define TPM_EMULATOR(obj) \
48 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
50 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
54 /* blobs from the TPM; part of VM state when migrating */
55 typedef struct TPMBlobBuffers
{
56 uint32_t permanent_flags
;
57 TPMSizedBuffer permanent
;
59 uint32_t volatil_flags
;
60 TPMSizedBuffer volatil
;
62 uint32_t savestate_flags
;
63 TPMSizedBuffer savestate
;
66 typedef struct TPMEmulator
{
69 TPMEmulatorOptions
*options
;
72 TPMVersion tpm_version
;
73 ptm_cap caps
; /* capabilities of the TPM */
74 uint8_t cur_locty_number
; /* last set locality */
75 Error
*migration_blocker
;
79 unsigned int established_flag
:1;
80 unsigned int established_flag_cached
:1;
82 TPMBlobBuffers state_blobs
;
86 static int tpm_emulator_ctrlcmd(TPMEmulator
*tpm
, unsigned long cmd
, void *msg
,
87 size_t msg_len_in
, size_t msg_len_out
)
89 CharBackend
*dev
= &tpm
->ctrl_chr
;
90 uint32_t cmd_no
= cpu_to_be32(cmd
);
91 ssize_t n
= sizeof(uint32_t) + msg_len_in
;
95 qemu_mutex_lock(&tpm
->mutex
);
98 memcpy(buf
, &cmd_no
, sizeof(cmd_no
));
99 memcpy(buf
+ sizeof(cmd_no
), msg
, msg_len_in
);
101 n
= qemu_chr_fe_write_all(dev
, buf
, n
);
106 if (msg_len_out
!= 0) {
107 n
= qemu_chr_fe_read_all(dev
, msg
, msg_len_out
);
116 qemu_mutex_unlock(&tpm
->mutex
);
120 static int tpm_emulator_unix_tx_bufs(TPMEmulator
*tpm_emu
,
121 const uint8_t *in
, uint32_t in_len
,
122 uint8_t *out
, uint32_t out_len
,
127 bool is_selftest
= false;
130 *selftest_done
= false;
131 is_selftest
= tpm_util_is_selftest(in
, in_len
);
134 ret
= qio_channel_write_all(tpm_emu
->data_ioc
, (char *)in
, in_len
, err
);
139 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
,
140 sizeof(struct tpm_resp_hdr
), err
);
145 ret
= qio_channel_read_all(tpm_emu
->data_ioc
,
146 (char *)out
+ sizeof(struct tpm_resp_hdr
),
147 tpm_cmd_get_size(out
) - sizeof(struct tpm_resp_hdr
), err
);
153 *selftest_done
= tpm_cmd_get_errcode(out
) == 0;
159 static int tpm_emulator_set_locality(TPMEmulator
*tpm_emu
, uint8_t locty_number
,
164 if (tpm_emu
->cur_locty_number
== locty_number
) {
168 trace_tpm_emulator_set_locality(locty_number
);
170 memset(&loc
, 0, sizeof(loc
));
171 loc
.u
.req
.loc
= locty_number
;
172 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_LOCALITY
, &loc
,
173 sizeof(loc
), sizeof(loc
)) < 0) {
174 error_setg(errp
, "tpm-emulator: could not set locality : %s",
179 loc
.u
.resp
.tpm_result
= be32_to_cpu(loc
.u
.resp
.tpm_result
);
180 if (loc
.u
.resp
.tpm_result
!= 0) {
181 error_setg(errp
, "tpm-emulator: TPM result for set locality : 0x%x",
182 loc
.u
.resp
.tpm_result
);
186 tpm_emu
->cur_locty_number
= locty_number
;
191 static void tpm_emulator_handle_request(TPMBackend
*tb
, TPMBackendCmd
*cmd
,
194 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
196 trace_tpm_emulator_handle_request();
198 if (tpm_emulator_set_locality(tpm_emu
, cmd
->locty
, errp
) < 0 ||
199 tpm_emulator_unix_tx_bufs(tpm_emu
, cmd
->in
, cmd
->in_len
,
200 cmd
->out
, cmd
->out_len
,
201 &cmd
->selftest_done
, errp
) < 0) {
202 tpm_util_write_fatal_error_response(cmd
->out
, cmd
->out_len
);
206 static int tpm_emulator_probe_caps(TPMEmulator
*tpm_emu
)
208 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_CAPABILITY
,
209 &tpm_emu
->caps
, 0, sizeof(tpm_emu
->caps
)) < 0) {
210 error_report("tpm-emulator: probing failed : %s", strerror(errno
));
214 tpm_emu
->caps
= be64_to_cpu(tpm_emu
->caps
);
216 trace_tpm_emulator_probe_caps(tpm_emu
->caps
);
221 static int tpm_emulator_check_caps(TPMEmulator
*tpm_emu
)
224 const char *tpm
= NULL
;
226 /* check for min. required capabilities */
227 switch (tpm_emu
->tpm_version
) {
228 case TPM_VERSION_1_2
:
229 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
230 PTM_CAP_SET_LOCALITY
| PTM_CAP_SET_DATAFD
| PTM_CAP_STOP
|
231 PTM_CAP_SET_BUFFERSIZE
;
234 case TPM_VERSION_2_0
:
235 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
236 PTM_CAP_SET_LOCALITY
| PTM_CAP_RESET_TPMESTABLISHED
|
237 PTM_CAP_SET_DATAFD
| PTM_CAP_STOP
| PTM_CAP_SET_BUFFERSIZE
;
240 case TPM_VERSION_UNSPEC
:
241 error_report("tpm-emulator: TPM version has not been set");
245 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, caps
)) {
246 error_report("tpm-emulator: TPM does not implement minimum set of "
247 "required capabilities for TPM %s (0x%x)", tpm
, (int)caps
);
254 static int tpm_emulator_stop_tpm(TPMBackend
*tb
)
256 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
259 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_STOP
, &res
, 0, sizeof(res
)) < 0) {
260 error_report("tpm-emulator: Could not stop TPM: %s",
265 res
= be32_to_cpu(res
);
267 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res
);
274 static int tpm_emulator_set_buffer_size(TPMBackend
*tb
,
278 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
279 ptm_setbuffersize psbs
;
281 if (tpm_emulator_stop_tpm(tb
) < 0) {
285 psbs
.u
.req
.buffersize
= cpu_to_be32(wanted_size
);
287 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_BUFFERSIZE
, &psbs
,
288 sizeof(psbs
.u
.req
), sizeof(psbs
.u
.resp
)) < 0) {
289 error_report("tpm-emulator: Could not set buffer size: %s",
294 psbs
.u
.resp
.tpm_result
= be32_to_cpu(psbs
.u
.resp
.tpm_result
);
295 if (psbs
.u
.resp
.tpm_result
!= 0) {
296 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
297 psbs
.u
.resp
.tpm_result
);
302 *actual_size
= be32_to_cpu(psbs
.u
.resp
.buffersize
);
305 trace_tpm_emulator_set_buffer_size(
306 be32_to_cpu(psbs
.u
.resp
.buffersize
),
307 be32_to_cpu(psbs
.u
.resp
.minsize
),
308 be32_to_cpu(psbs
.u
.resp
.maxsize
));
313 static int tpm_emulator_startup_tpm_resume(TPMBackend
*tb
, size_t buffersize
,
316 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
318 .u
.req
.init_flags
= 0,
322 trace_tpm_emulator_startup_tpm_resume(is_resume
, buffersize
);
324 if (buffersize
!= 0 &&
325 tpm_emulator_set_buffer_size(tb
, buffersize
, NULL
) < 0) {
330 init
.u
.req
.init_flags
|= cpu_to_be32(PTM_INIT_FLAG_DELETE_VOLATILE
);
333 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_INIT
, &init
, sizeof(init
),
335 error_report("tpm-emulator: could not send INIT: %s",
340 res
= be32_to_cpu(init
.u
.resp
.tpm_result
);
342 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res
);
351 static int tpm_emulator_startup_tpm(TPMBackend
*tb
, size_t buffersize
)
353 return tpm_emulator_startup_tpm_resume(tb
, buffersize
, false);
356 static bool tpm_emulator_get_tpm_established_flag(TPMBackend
*tb
)
358 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
361 if (tpm_emu
->established_flag_cached
) {
362 return tpm_emu
->established_flag
;
365 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_TPMESTABLISHED
, &est
,
366 0, sizeof(est
)) < 0) {
367 error_report("tpm-emulator: Could not get the TPM established flag: %s",
371 trace_tpm_emulator_get_tpm_established_flag(est
.u
.resp
.bit
);
373 tpm_emu
->established_flag_cached
= 1;
374 tpm_emu
->established_flag
= (est
.u
.resp
.bit
!= 0);
376 return tpm_emu
->established_flag
;
379 static int tpm_emulator_reset_tpm_established_flag(TPMBackend
*tb
,
382 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
383 ptm_reset_est reset_est
;
386 /* only a TPM 2.0 will support this */
387 if (tpm_emu
->tpm_version
!= TPM_VERSION_2_0
) {
391 reset_est
.u
.req
.loc
= tpm_emu
->cur_locty_number
;
392 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_RESET_TPMESTABLISHED
,
393 &reset_est
, sizeof(reset_est
),
394 sizeof(reset_est
)) < 0) {
395 error_report("tpm-emulator: Could not reset the establishment bit: %s",
400 res
= be32_to_cpu(reset_est
.u
.resp
.tpm_result
);
402 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
407 tpm_emu
->established_flag_cached
= 0;
412 static void tpm_emulator_cancel_cmd(TPMBackend
*tb
)
414 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
417 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, PTM_CAP_CANCEL_TPM_CMD
)) {
418 trace_tpm_emulator_cancel_cmd_not_supt();
422 /* FIXME: make the function non-blocking, or it may block a VCPU */
423 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_CANCEL_TPM_CMD
, &res
, 0,
425 error_report("tpm-emulator: Could not cancel command: %s",
427 } else if (res
!= 0) {
428 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
433 static TPMVersion
tpm_emulator_get_tpm_version(TPMBackend
*tb
)
435 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
437 return tpm_emu
->tpm_version
;
440 static size_t tpm_emulator_get_buffer_size(TPMBackend
*tb
)
444 if (tpm_emulator_set_buffer_size(tb
, 0, &actual_size
) < 0) {
451 static int tpm_emulator_block_migration(TPMEmulator
*tpm_emu
)
454 ptm_cap caps
= PTM_CAP_GET_STATEBLOB
| PTM_CAP_SET_STATEBLOB
|
457 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, caps
)) {
458 error_setg(&tpm_emu
->migration_blocker
,
459 "Migration disabled: TPM emulator does not support "
461 migrate_add_blocker(tpm_emu
->migration_blocker
, &err
);
463 error_report_err(err
);
464 error_free(tpm_emu
->migration_blocker
);
465 tpm_emu
->migration_blocker
= NULL
;
474 static int tpm_emulator_prepare_data_fd(TPMEmulator
*tpm_emu
)
478 int fds
[2] = { -1, -1 };
480 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
481 error_report("tpm-emulator: Failed to create socketpair");
485 qemu_chr_fe_set_msgfds(&tpm_emu
->ctrl_chr
, fds
+ 1, 1);
487 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_DATAFD
, &res
, 0,
488 sizeof(res
)) < 0 || res
!= 0) {
489 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
494 tpm_emu
->data_ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(fds
[0], &err
));
496 error_prepend(&err
, "tpm-emulator: Failed to create io channel: ");
497 error_report_err(err
);
511 static int tpm_emulator_handle_device_opts(TPMEmulator
*tpm_emu
, QemuOpts
*opts
)
515 value
= qemu_opt_get(opts
, "chardev");
518 Chardev
*dev
= qemu_chr_find(value
);
521 error_report("tpm-emulator: tpm chardev '%s' not found.", value
);
525 if (!qemu_chr_fe_init(&tpm_emu
->ctrl_chr
, dev
, &err
)) {
526 error_prepend(&err
, "tpm-emulator: No valid chardev found at '%s':",
528 error_report_err(err
);
532 tpm_emu
->options
->chardev
= g_strdup(value
);
535 if (tpm_emulator_prepare_data_fd(tpm_emu
) < 0) {
539 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
540 * by passthrough driver, which not yet using GIOChannel.
542 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu
->data_ioc
)->fd
,
543 &tpm_emu
->tpm_version
)) {
544 error_report("'%s' is not emulating TPM device. Error: %s",
545 tpm_emu
->options
->chardev
, strerror(errno
));
549 switch (tpm_emu
->tpm_version
) {
550 case TPM_VERSION_1_2
:
551 trace_tpm_emulator_handle_device_opts_tpm12();
553 case TPM_VERSION_2_0
:
554 trace_tpm_emulator_handle_device_opts_tpm2();
557 trace_tpm_emulator_handle_device_opts_unspec();
560 if (tpm_emulator_probe_caps(tpm_emu
) ||
561 tpm_emulator_check_caps(tpm_emu
)) {
565 return tpm_emulator_block_migration(tpm_emu
);
568 trace_tpm_emulator_handle_device_opts_startup_error();
573 static TPMBackend
*tpm_emulator_create(QemuOpts
*opts
)
575 TPMBackend
*tb
= TPM_BACKEND(object_new(TYPE_TPM_EMULATOR
));
577 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb
), opts
)) {
578 object_unref(OBJECT(tb
));
585 static TpmTypeOptions
*tpm_emulator_get_tpm_options(TPMBackend
*tb
)
587 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
588 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
590 options
->type
= TPM_TYPE_OPTIONS_KIND_EMULATOR
;
591 options
->u
.emulator
.data
= QAPI_CLONE(TPMEmulatorOptions
, tpm_emu
->options
);
596 static const QemuOptDesc tpm_emulator_cmdline_opts
[] = {
597 TPM_STANDARD_CMDLINE_OPTS
,
600 .type
= QEMU_OPT_STRING
,
601 .help
= "Character device to use for out-of-band control messages",
603 { /* end of list */ },
607 * Transfer a TPM state blob from the TPM into a provided buffer.
609 * @tpm_emu: TPMEmulator
610 * @type: the type of blob to transfer
611 * @tsb: the TPMSizeBuffer to fill with the blob
612 * @flags: the flags to return to the caller
614 static int tpm_emulator_get_state_blob(TPMEmulator
*tpm_emu
,
622 uint32_t totlength
, length
;
624 tpm_sized_buffer_reset(tsb
);
626 pgs
.u
.req
.state_flags
= cpu_to_be32(PTM_STATE_FLAG_DECRYPTED
);
627 pgs
.u
.req
.type
= cpu_to_be32(type
);
628 pgs
.u
.req
.offset
= 0;
630 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_STATEBLOB
,
631 &pgs
, sizeof(pgs
.u
.req
),
632 offsetof(ptm_getstate
, u
.resp
.data
)) < 0) {
633 error_report("tpm-emulator: could not get state blob type %d : %s",
634 type
, strerror(errno
));
638 res
= be32_to_cpu(pgs
.u
.resp
.tpm_result
);
639 if (res
!= 0 && (res
& 0x800) == 0) {
640 error_report("tpm-emulator: Getting the stateblob (type %d) failed "
641 "with a TPM error 0x%x", type
, res
);
645 totlength
= be32_to_cpu(pgs
.u
.resp
.totlength
);
646 length
= be32_to_cpu(pgs
.u
.resp
.length
);
647 if (totlength
!= length
) {
648 error_report("tpm-emulator: Expecting to read %u bytes "
649 "but would get %u", totlength
, length
);
653 *flags
= be32_to_cpu(pgs
.u
.resp
.state_flags
);
656 tsb
->buffer
= g_try_malloc(totlength
);
658 error_report("tpm-emulator: Out of memory allocating %u bytes",
663 n
= qemu_chr_fe_read_all(&tpm_emu
->ctrl_chr
, tsb
->buffer
, totlength
);
664 if (n
!= totlength
) {
665 error_report("tpm-emulator: Could not read stateblob (type %d); "
666 "expected %u bytes, got %zd",
671 tsb
->size
= totlength
;
673 trace_tpm_emulator_get_state_blob(type
, tsb
->size
, *flags
);
678 static int tpm_emulator_get_state_blobs(TPMEmulator
*tpm_emu
)
680 TPMBlobBuffers
*state_blobs
= &tpm_emu
->state_blobs
;
682 if (tpm_emulator_get_state_blob(tpm_emu
, PTM_BLOB_TYPE_PERMANENT
,
683 &state_blobs
->permanent
,
684 &state_blobs
->permanent_flags
) < 0 ||
685 tpm_emulator_get_state_blob(tpm_emu
, PTM_BLOB_TYPE_VOLATILE
,
686 &state_blobs
->volatil
,
687 &state_blobs
->volatil_flags
) < 0 ||
688 tpm_emulator_get_state_blob(tpm_emu
, PTM_BLOB_TYPE_SAVESTATE
,
689 &state_blobs
->savestate
,
690 &state_blobs
->savestate_flags
) < 0) {
697 tpm_sized_buffer_reset(&state_blobs
->volatil
);
698 tpm_sized_buffer_reset(&state_blobs
->permanent
);
699 tpm_sized_buffer_reset(&state_blobs
->savestate
);
705 * Transfer a TPM state blob to the TPM emulator.
707 * @tpm_emu: TPMEmulator
708 * @type: the type of TPM state blob to transfer
709 * @tsb: TPMSizedBuffer containing the TPM state blob
710 * @flags: Flags describing the (encryption) state of the TPM state blob
712 static int tpm_emulator_set_state_blob(TPMEmulator
*tpm_emu
,
721 if (tsb
->size
== 0) {
725 pss
= (ptm_setstate
) {
726 .u
.req
.state_flags
= cpu_to_be32(flags
),
727 .u
.req
.type
= cpu_to_be32(type
),
728 .u
.req
.length
= cpu_to_be32(tsb
->size
),
731 /* write the header only */
732 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_STATEBLOB
, &pss
,
733 offsetof(ptm_setstate
, u
.req
.data
), 0) < 0) {
734 error_report("tpm-emulator: could not set state blob type %d : %s",
735 type
, strerror(errno
));
740 n
= qemu_chr_fe_write_all(&tpm_emu
->ctrl_chr
, tsb
->buffer
, tsb
->size
);
741 if (n
!= tsb
->size
) {
742 error_report("tpm-emulator: Writing the stateblob (type %d) "
743 "failed; could not write %u bytes, but only %zd",
748 /* now get the result */
749 n
= qemu_chr_fe_read_all(&tpm_emu
->ctrl_chr
,
750 (uint8_t *)&pss
, sizeof(pss
.u
.resp
));
751 if (n
!= sizeof(pss
.u
.resp
)) {
752 error_report("tpm-emulator: Reading response from writing stateblob "
753 "(type %d) failed; expected %zu bytes, got %zd", type
,
754 sizeof(pss
.u
.resp
), n
);
758 tpm_result
= be32_to_cpu(pss
.u
.resp
.tpm_result
);
759 if (tpm_result
!= 0) {
760 error_report("tpm-emulator: Setting the stateblob (type %d) failed "
761 "with a TPM error 0x%x", type
, tpm_result
);
765 trace_tpm_emulator_set_state_blob(type
, tsb
->size
, flags
);
771 * Set all the TPM state blobs.
773 * Returns a negative errno code in case of error.
775 static int tpm_emulator_set_state_blobs(TPMBackend
*tb
)
777 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
778 TPMBlobBuffers
*state_blobs
= &tpm_emu
->state_blobs
;
780 trace_tpm_emulator_set_state_blobs();
782 if (tpm_emulator_stop_tpm(tb
) < 0) {
783 trace_tpm_emulator_set_state_blobs_error("Could not stop TPM");
787 if (tpm_emulator_set_state_blob(tpm_emu
, PTM_BLOB_TYPE_PERMANENT
,
788 &state_blobs
->permanent
,
789 state_blobs
->permanent_flags
) < 0 ||
790 tpm_emulator_set_state_blob(tpm_emu
, PTM_BLOB_TYPE_VOLATILE
,
791 &state_blobs
->volatil
,
792 state_blobs
->volatil_flags
) < 0 ||
793 tpm_emulator_set_state_blob(tpm_emu
, PTM_BLOB_TYPE_SAVESTATE
,
794 &state_blobs
->savestate
,
795 state_blobs
->savestate_flags
) < 0) {
799 trace_tpm_emulator_set_state_blobs_done();
804 static int tpm_emulator_pre_save(void *opaque
)
806 TPMBackend
*tb
= opaque
;
807 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
809 trace_tpm_emulator_pre_save();
811 tpm_backend_finish_sync(tb
);
813 /* get the state blobs from the TPM */
814 return tpm_emulator_get_state_blobs(tpm_emu
);
818 * Load the TPM state blobs into the TPM.
820 * Returns negative errno codes in case of error.
822 static int tpm_emulator_post_load(void *opaque
, int version_id
)
824 TPMBackend
*tb
= opaque
;
827 ret
= tpm_emulator_set_state_blobs(tb
);
832 if (tpm_emulator_startup_tpm_resume(tb
, 0, true) < 0) {
839 static const VMStateDescription vmstate_tpm_emulator
= {
840 .name
= "tpm-emulator",
842 .pre_save
= tpm_emulator_pre_save
,
843 .post_load
= tpm_emulator_post_load
,
844 .fields
= (VMStateField
[]) {
845 VMSTATE_UINT32(state_blobs
.permanent_flags
, TPMEmulator
),
846 VMSTATE_UINT32(state_blobs
.permanent
.size
, TPMEmulator
),
847 VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs
.permanent
.buffer
,
849 state_blobs
.permanent
.size
),
851 VMSTATE_UINT32(state_blobs
.volatil_flags
, TPMEmulator
),
852 VMSTATE_UINT32(state_blobs
.volatil
.size
, TPMEmulator
),
853 VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs
.volatil
.buffer
,
855 state_blobs
.volatil
.size
),
857 VMSTATE_UINT32(state_blobs
.savestate_flags
, TPMEmulator
),
858 VMSTATE_UINT32(state_blobs
.savestate
.size
, TPMEmulator
),
859 VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs
.savestate
.buffer
,
861 state_blobs
.savestate
.size
),
863 VMSTATE_END_OF_LIST()
867 static void tpm_emulator_inst_init(Object
*obj
)
869 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
871 trace_tpm_emulator_inst_init();
873 tpm_emu
->options
= g_new0(TPMEmulatorOptions
, 1);
874 tpm_emu
->cur_locty_number
= ~0;
875 qemu_mutex_init(&tpm_emu
->mutex
);
877 vmstate_register(NULL
, -1, &vmstate_tpm_emulator
, obj
);
881 * Gracefully shut down the external TPM
883 static void tpm_emulator_shutdown(TPMEmulator
*tpm_emu
)
887 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SHUTDOWN
, &res
, 0, sizeof(res
)) < 0) {
888 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
890 } else if (res
!= 0) {
891 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
896 static void tpm_emulator_inst_finalize(Object
*obj
)
898 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
899 TPMBlobBuffers
*state_blobs
= &tpm_emu
->state_blobs
;
901 tpm_emulator_shutdown(tpm_emu
);
903 object_unref(OBJECT(tpm_emu
->data_ioc
));
905 qemu_chr_fe_deinit(&tpm_emu
->ctrl_chr
, false);
907 qapi_free_TPMEmulatorOptions(tpm_emu
->options
);
909 if (tpm_emu
->migration_blocker
) {
910 migrate_del_blocker(tpm_emu
->migration_blocker
);
911 error_free(tpm_emu
->migration_blocker
);
914 tpm_sized_buffer_reset(&state_blobs
->volatil
);
915 tpm_sized_buffer_reset(&state_blobs
->permanent
);
916 tpm_sized_buffer_reset(&state_blobs
->savestate
);
918 qemu_mutex_destroy(&tpm_emu
->mutex
);
920 vmstate_unregister(NULL
, &vmstate_tpm_emulator
, obj
);
923 static void tpm_emulator_class_init(ObjectClass
*klass
, void *data
)
925 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
927 tbc
->type
= TPM_TYPE_EMULATOR
;
928 tbc
->opts
= tpm_emulator_cmdline_opts
;
929 tbc
->desc
= "TPM emulator backend driver";
930 tbc
->create
= tpm_emulator_create
;
931 tbc
->startup_tpm
= tpm_emulator_startup_tpm
;
932 tbc
->cancel_cmd
= tpm_emulator_cancel_cmd
;
933 tbc
->get_tpm_established_flag
= tpm_emulator_get_tpm_established_flag
;
934 tbc
->reset_tpm_established_flag
= tpm_emulator_reset_tpm_established_flag
;
935 tbc
->get_tpm_version
= tpm_emulator_get_tpm_version
;
936 tbc
->get_buffer_size
= tpm_emulator_get_buffer_size
;
937 tbc
->get_tpm_options
= tpm_emulator_get_tpm_options
;
939 tbc
->handle_request
= tpm_emulator_handle_request
;
942 static const TypeInfo tpm_emulator_info
= {
943 .name
= TYPE_TPM_EMULATOR
,
944 .parent
= TYPE_TPM_BACKEND
,
945 .instance_size
= sizeof(TPMEmulator
),
946 .class_init
= tpm_emulator_class_init
,
947 .instance_init
= tpm_emulator_inst_init
,
948 .instance_finalize
= tpm_emulator_inst_finalize
,
951 static void tpm_emulator_register(void)
953 type_register_static(&tpm_emulator_info
);
956 type_init(tpm_emulator_register
)