Revert "gdbstub: Do not kill target in system emulation mode"
[qemu/qmp-unstable.git] / hw / tpm / tpm_passthrough.c
blob73ca90628274191802f95d970597f9bfb672ea29
1 /*
2 * passthrough TPM driver
4 * Copyright (c) 2010 - 2013 IBM Corporation
5 * Authors:
6 * Stefan Berger <stefanb@us.ibm.com>
8 * Copyright (C) 2011 IAIK, Graz University of Technology
9 * Author: Andreas Niederl
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, see <http://www.gnu.org/licenses/>
25 #include <dirent.h>
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/sockets.h"
30 #include "sysemu/tpm_backend.h"
31 #include "tpm_int.h"
32 #include "hw/hw.h"
33 #include "hw/i386/pc.h"
34 #include "sysemu/tpm_backend_int.h"
35 #include "tpm_tis.h"
37 #define DEBUG_TPM 0
39 #define DPRINTF(fmt, ...) do { \
40 if (DEBUG_TPM) { \
41 fprintf(stderr, fmt, ## __VA_ARGS__); \
42 } \
43 } while (0);
45 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
46 #define TPM_PASSTHROUGH(obj) \
47 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
49 static const TPMDriverOps tpm_passthrough_driver;
51 /* data structures */
52 typedef struct TPMPassthruThreadParams {
53 TPMState *tpm_state;
55 TPMRecvDataCB *recv_data_callback;
56 TPMBackend *tb;
57 } TPMPassthruThreadParams;
59 struct TPMPassthruState {
60 TPMBackend parent;
62 TPMBackendThread tbt;
64 TPMPassthruThreadParams tpm_thread_params;
66 char *tpm_dev;
67 int tpm_fd;
68 bool tpm_executing;
69 bool tpm_op_canceled;
70 int cancel_fd;
71 bool had_startup_error;
74 typedef struct TPMPassthruState TPMPassthruState;
76 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
78 /* functions */
80 static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
82 static int tpm_passthrough_unix_write(int fd, const uint8_t *buf, uint32_t len)
84 return send_all(fd, buf, len);
87 static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
89 return recv_all(fd, buf, len, true);
92 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf)
94 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)buf;
96 return be32_to_cpu(resp->len);
100 * Write an error message in the given output buffer.
102 static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
104 if (out_len >= sizeof(struct tpm_resp_hdr)) {
105 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
107 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
108 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
109 resp->errcode = cpu_to_be32(TPM_FAIL);
113 static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len)
115 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
117 if (in_len >= sizeof(*hdr)) {
118 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
121 return false;
124 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
125 const uint8_t *in, uint32_t in_len,
126 uint8_t *out, uint32_t out_len,
127 bool *selftest_done)
129 int ret;
130 bool is_selftest;
131 const struct tpm_resp_hdr *hdr;
133 tpm_pt->tpm_op_canceled = false;
134 tpm_pt->tpm_executing = true;
135 *selftest_done = false;
137 is_selftest = tpm_passthrough_is_selftest(in, in_len);
139 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
140 if (ret != in_len) {
141 if (!tpm_pt->tpm_op_canceled ||
142 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
143 error_report("tpm_passthrough: error while transmitting data "
144 "to TPM: %s (%i)",
145 strerror(errno), errno);
147 goto err_exit;
150 tpm_pt->tpm_executing = false;
152 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
153 if (ret < 0) {
154 if (!tpm_pt->tpm_op_canceled ||
155 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
156 error_report("tpm_passthrough: error while reading data from "
157 "TPM: %s (%i)",
158 strerror(errno), errno);
160 } else if (ret < sizeof(struct tpm_resp_hdr) ||
161 tpm_passthrough_get_size_from_buffer(out) != ret) {
162 ret = -1;
163 error_report("tpm_passthrough: received invalid response "
164 "packet from TPM");
167 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
168 hdr = (struct tpm_resp_hdr *)out;
169 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
172 err_exit:
173 if (ret < 0) {
174 tpm_write_fatal_error_response(out, out_len);
177 tpm_pt->tpm_executing = false;
179 return ret;
182 static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
183 const TPMLocality *locty_data,
184 bool *selftest_done)
186 return tpm_passthrough_unix_tx_bufs(tpm_pt,
187 locty_data->w_buffer.buffer,
188 locty_data->w_offset,
189 locty_data->r_buffer.buffer,
190 locty_data->r_buffer.size,
191 selftest_done);
194 static void tpm_passthrough_worker_thread(gpointer data,
195 gpointer user_data)
197 TPMPassthruThreadParams *thr_parms = user_data;
198 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
199 TPMBackendCmd cmd = (TPMBackendCmd)data;
200 bool selftest_done = false;
202 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
204 switch (cmd) {
205 case TPM_BACKEND_CMD_PROCESS_CMD:
206 tpm_passthrough_unix_transfer(tpm_pt,
207 thr_parms->tpm_state->locty_data,
208 &selftest_done);
210 thr_parms->recv_data_callback(thr_parms->tpm_state,
211 thr_parms->tpm_state->locty_number,
212 selftest_done);
213 break;
214 case TPM_BACKEND_CMD_INIT:
215 case TPM_BACKEND_CMD_END:
216 case TPM_BACKEND_CMD_TPM_RESET:
217 /* nothing to do */
218 break;
223 * Start the TPM (thread). If it had been started before, then terminate
224 * and start it again.
226 static int tpm_passthrough_startup_tpm(TPMBackend *tb)
228 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
230 /* terminate a running TPM */
231 tpm_backend_thread_end(&tpm_pt->tbt);
233 tpm_backend_thread_create(&tpm_pt->tbt,
234 tpm_passthrough_worker_thread,
235 &tpm_pt->tpm_thread_params);
237 return 0;
240 static void tpm_passthrough_reset(TPMBackend *tb)
242 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
244 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
246 tpm_passthrough_cancel_cmd(tb);
248 tpm_backend_thread_end(&tpm_pt->tbt);
250 tpm_pt->had_startup_error = false;
253 static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
254 TPMRecvDataCB *recv_data_cb)
256 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
258 tpm_pt->tpm_thread_params.tpm_state = s;
259 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
260 tpm_pt->tpm_thread_params.tb = tb;
262 return 0;
265 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
267 return false;
270 static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
272 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
274 return tpm_pt->had_startup_error;
277 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
279 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
281 if (sb->size != wanted_size) {
282 sb->buffer = g_realloc(sb->buffer, wanted_size);
283 sb->size = wanted_size;
285 return sb->size;
288 static void tpm_passthrough_deliver_request(TPMBackend *tb)
290 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
292 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
295 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
297 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
298 int n;
301 * As of Linux 3.7 the tpm_tis driver does not properly cancel
302 * commands on all TPM manufacturers' TPMs.
303 * Only cancel if we're busy so we don't cancel someone else's
304 * command, e.g., a command executed on the host.
306 if (tpm_pt->tpm_executing) {
307 if (tpm_pt->cancel_fd >= 0) {
308 n = write(tpm_pt->cancel_fd, "-", 1);
309 if (n != 1) {
310 error_report("Canceling TPM command failed: %s",
311 strerror(errno));
312 } else {
313 tpm_pt->tpm_op_canceled = true;
315 } else {
316 error_report("Cannot cancel TPM command due to missing "
317 "TPM sysfs cancel entry");
322 static const char *tpm_passthrough_create_desc(void)
324 return "Passthrough TPM backend driver";
328 * A basic test of a TPM device. We expect a well formatted response header
329 * (error response is fine) within one second.
331 static int tpm_passthrough_test_tpmdev(int fd)
333 struct tpm_req_hdr req = {
334 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
335 .len = cpu_to_be32(sizeof(req)),
336 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
338 struct tpm_resp_hdr *resp;
339 fd_set readfds;
340 int n;
341 struct timeval tv = {
342 .tv_sec = 1,
343 .tv_usec = 0,
345 unsigned char buf[1024];
347 n = write(fd, &req, sizeof(req));
348 if (n < 0) {
349 return errno;
351 if (n != sizeof(req)) {
352 return EFAULT;
355 FD_ZERO(&readfds);
356 FD_SET(fd, &readfds);
358 /* wait for a second */
359 n = select(fd + 1, &readfds, NULL, NULL, &tv);
360 if (n != 1) {
361 return errno;
364 n = read(fd, &buf, sizeof(buf));
365 if (n < sizeof(struct tpm_resp_hdr)) {
366 return EFAULT;
369 resp = (struct tpm_resp_hdr *)buf;
370 /* check the header */
371 if (be16_to_cpu(resp->tag) != TPM_TAG_RSP_COMMAND ||
372 be32_to_cpu(resp->len) != n) {
373 return EBADMSG;
376 return 0;
380 * Unless path or file descriptor set has been provided by user,
381 * determine the sysfs cancel file following kernel documentation
382 * in Documentation/ABI/stable/sysfs-class-tpm.
383 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
385 static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
387 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
388 int fd = -1;
389 char *dev;
390 char path[PATH_MAX];
392 if (tb->cancel_path) {
393 fd = qemu_open(tb->cancel_path, O_WRONLY);
394 if (fd < 0) {
395 error_report("Could not open TPM cancel path : %s",
396 strerror(errno));
398 return fd;
401 dev = strrchr(tpm_pt->tpm_dev, '/');
402 if (dev) {
403 dev++;
404 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
405 dev) < sizeof(path)) {
406 fd = qemu_open(path, O_WRONLY);
407 if (fd >= 0) {
408 tb->cancel_path = g_strdup(path);
409 } else {
410 error_report("tpm_passthrough: Could not open TPM cancel "
411 "path %s : %s", path, strerror(errno));
414 } else {
415 error_report("tpm_passthrough: Bad TPM device path %s",
416 tpm_pt->tpm_dev);
419 return fd;
422 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
424 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
425 const char *value;
427 value = qemu_opt_get(opts, "cancel-path");
428 tb->cancel_path = g_strdup(value);
430 value = qemu_opt_get(opts, "path");
431 if (!value) {
432 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
435 tpm_pt->tpm_dev = g_strdup(value);
437 tb->path = g_strdup(tpm_pt->tpm_dev);
439 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
440 if (tpm_pt->tpm_fd < 0) {
441 error_report("Cannot access TPM device using '%s': %s",
442 tpm_pt->tpm_dev, strerror(errno));
443 goto err_free_parameters;
446 if (tpm_passthrough_test_tpmdev(tpm_pt->tpm_fd)) {
447 error_report("'%s' is not a TPM device.",
448 tpm_pt->tpm_dev);
449 goto err_close_tpmdev;
452 return 0;
454 err_close_tpmdev:
455 qemu_close(tpm_pt->tpm_fd);
456 tpm_pt->tpm_fd = -1;
458 err_free_parameters:
459 g_free(tb->path);
460 tb->path = NULL;
462 g_free(tpm_pt->tpm_dev);
463 tpm_pt->tpm_dev = NULL;
465 return 1;
468 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
470 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
471 TPMBackend *tb = TPM_BACKEND(obj);
472 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
474 tb->id = g_strdup(id);
475 /* let frontend set the fe_model to proper value */
476 tb->fe_model = -1;
478 tb->ops = &tpm_passthrough_driver;
480 if (tpm_passthrough_handle_device_opts(opts, tb)) {
481 goto err_exit;
484 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
485 if (tpm_pt->cancel_fd < 0) {
486 goto err_exit;
489 return tb;
491 err_exit:
492 g_free(tb->id);
494 return NULL;
497 static void tpm_passthrough_destroy(TPMBackend *tb)
499 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
501 tpm_passthrough_cancel_cmd(tb);
503 tpm_backend_thread_end(&tpm_pt->tbt);
505 qemu_close(tpm_pt->tpm_fd);
506 qemu_close(tpm_pt->cancel_fd);
508 g_free(tb->id);
509 g_free(tb->path);
510 g_free(tb->cancel_path);
511 g_free(tpm_pt->tpm_dev);
514 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
515 TPM_STANDARD_CMDLINE_OPTS,
517 .name = "cancel-path",
518 .type = QEMU_OPT_STRING,
519 .help = "Sysfs file entry for canceling TPM commands",
522 .name = "path",
523 .type = QEMU_OPT_STRING,
524 .help = "Path to TPM device on the host",
526 { /* end of list */ },
529 static const TPMDriverOps tpm_passthrough_driver = {
530 .type = TPM_TYPE_PASSTHROUGH,
531 .opts = tpm_passthrough_cmdline_opts,
532 .desc = tpm_passthrough_create_desc,
533 .create = tpm_passthrough_create,
534 .destroy = tpm_passthrough_destroy,
535 .init = tpm_passthrough_init,
536 .startup_tpm = tpm_passthrough_startup_tpm,
537 .realloc_buffer = tpm_passthrough_realloc_buffer,
538 .reset = tpm_passthrough_reset,
539 .had_startup_error = tpm_passthrough_get_startup_error,
540 .deliver_request = tpm_passthrough_deliver_request,
541 .cancel_cmd = tpm_passthrough_cancel_cmd,
542 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
545 static void tpm_passthrough_inst_init(Object *obj)
549 static void tpm_passthrough_inst_finalize(Object *obj)
553 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
555 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
557 tbc->ops = &tpm_passthrough_driver;
560 static const TypeInfo tpm_passthrough_info = {
561 .name = TYPE_TPM_PASSTHROUGH,
562 .parent = TYPE_TPM_BACKEND,
563 .instance_size = sizeof(TPMPassthruState),
564 .class_init = tpm_passthrough_class_init,
565 .instance_init = tpm_passthrough_inst_init,
566 .instance_finalize = tpm_passthrough_inst_finalize,
569 static void tpm_passthrough_register(void)
571 type_register_static(&tpm_passthrough_info);
572 tpm_register_driver(&tpm_passthrough_driver);
575 type_init(tpm_passthrough_register)