diff --git a/devicemodel/Makefile b/devicemodel/Makefile index ca0949691..ccaaf14c1 100644 --- a/devicemodel/Makefile +++ b/devicemodel/Makefile @@ -84,6 +84,7 @@ SRCS += hw/platform/rpmb/rpmb_sim.c SRCS += hw/platform/rpmb/rpmb_backend.c SRCS += hw/platform/tpm/tpm_emulator.c SRCS += hw/platform/tpm/tpm_crb.c +SRCS += hw/platform/tpm/tpm.c SRCS += hw/platform/debugexit.c SRCS += hw/pci/wdt_i6300esb.c SRCS += hw/pci/lpc.c diff --git a/devicemodel/core/main.c b/devicemodel/core/main.c index 6d6700d31..a0ed4cc49 100644 --- a/devicemodel/core/main.c +++ b/devicemodel/core/main.c @@ -64,6 +64,7 @@ #include "atomic.h" #include "vmcfg_config.h" #include "vmcfg.h" +#include "tpm.h" #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */ @@ -157,6 +158,7 @@ usage(int code) " --ptdev_no_reset: disable reset check for ptdev\n" " --debugexit: enable debug exit function\n" " --intr_monitor: enable interrupt storm monitor\n" + " --vtpm2: Virtual TPM2 args: sock_path=$PATH_OF_SWTPM_SOCKET\n" "............its params: threshold/s,probe-period(s),delay_time(ms),delay_duration(ms)\n", progname, (int)strlen(progname), "", (int)strlen(progname), "", (int)strlen(progname), ""); @@ -446,6 +448,8 @@ vm_init_vdevs(struct vmctx *ctx) if (ret < 0) goto pci_fail; + init_vtpm2(ctx); + return 0; pci_fail: @@ -480,6 +484,7 @@ vm_deinit_vdevs(struct vmctx *ctx) atkbdc_deinit(ctx); pci_irq_deinit(ctx); ioapic_deinit(); + deinit_vtpm2(ctx); } static void @@ -703,6 +708,7 @@ enum { CMD_OPT_VMCFG, CMD_OPT_DUMP, CMD_OPT_INTR_MONITOR, + CMD_OPT_VTPM2, }; static struct option long_options[] = { @@ -737,6 +743,7 @@ static struct option long_options[] = { CMD_OPT_PTDEV_NO_RESET}, {"debugexit", no_argument, 0, CMD_OPT_DEBUGEXIT}, {"intr_monitor", required_argument, 0, CMD_OPT_INTR_MONITOR}, + {"vtpm2", required_argument, 0, CMD_OPT_VTPM2}, {0, 0, 0, 0 }, }; @@ -860,6 +867,11 @@ dm_run(int argc, char *argv[]) break; case CMD_OPT_DEBUGEXIT: debugexit_enabled = true; + case CMD_OPT_VTPM2: + if (acrn_parse_vtpm2(optarg) != 0) { + errx(EX_USAGE, "invalid vtpm2 param %s", optarg); + exit(1); + } break; case CMD_OPT_INTR_MONITOR: if (acrn_parse_intr_monitor(optarg) != 0) { diff --git a/devicemodel/hw/platform/tpm/tpm.c b/devicemodel/hw/platform/tpm/tpm.c new file mode 100644 index 000000000..b0baa0414 --- /dev/null +++ b/devicemodel/hw/platform/tpm/tpm.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2018 Intel Corporation + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include + +#include "vmmapi.h" +#include "tpm.h" +#include "tpm_internal.h" + +static int tpm_debug; +#define LOG_TAG "tpm: " +#define DPRINTF(fmt, args...) \ + do { if (tpm_debug) printf(LOG_TAG "%s:" fmt, __func__, ##args); } while (0) +#define WPRINTF(fmt, args...) \ + do { printf(LOG_TAG "%s:" fmt, __func__, ##args); } while (0) + +#define STR_MAX_LEN 1024U +static char *sock_path = NULL; + +enum { + SOCK_PATH_OPT = 0 +}; + +char *const token[] = { + [SOCK_PATH_OPT] = "sock_path", + NULL +}; + +int acrn_parse_vtpm2(char *arg) +{ + char *value; + size_t len = strlen(arg); + + if (len > STR_MAX_LEN) + return -1; + + if (SOCK_PATH_OPT == getsubopt(&arg, token, &value)) { + if (value == NULL) { + DPRINTF("Invalid vtpm socket path\n"); + return -1; + } + sock_path = calloc(len + 1, 1); + if (!sock_path) + return -1; + strcpy(sock_path, value); + } + + return 0; +} + +void init_vtpm2(struct vmctx *ctx) +{ + if (!sock_path) { + WPRINTF("Invalid socket path!\n"); + return; + } + + if (init_tpm_emulator(sock_path) < 0) { + WPRINTF("Failed init tpm emulator!\n"); + return; + } + + if (init_tpm_crb(ctx) < 0) { + WPRINTF("Failed init tpm emulator!\n"); + } +} + +void deinit_vtpm2(struct vmctx *ctx) +{ + if (ctx->tpm_dev) { + deinit_tpm_crb(ctx); + + deinit_tpm_emulator(); + } +} diff --git a/devicemodel/include/tpm.h b/devicemodel/include/tpm.h index 95fb9a88f..2ae8af7b8 100644 --- a/devicemodel/include/tpm.h +++ b/devicemodel/include/tpm.h @@ -39,4 +39,14 @@ enum { #define TPM_CRB_REG_SIZE ((CRB_DATA_BUFFER) - (TPM_CRB_MMIO_ADDR)) #define TPM_CRB_DATA_BUFFER_SIZE ((TPM_CRB_MMIO_SIZE) - (TPM_CRB_REG_SIZE)) +/* APIs by tpm.c */ +/* Initialize Virtual TPM2 */ +void init_vtpm2(struct vmctx *ctx); + +/* Deinitialize Virtual TPM2 */ +void deinit_vtpm2(struct vmctx *ctx); + +/* Parse Virtual TPM option from command line */ +int acrn_parse_vtpm2(char *arg); + #endif