From nobody Thu Aug 10 07:23:56 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RLz1n0Sgyz4qFlT; Thu, 10 Aug 2023 07:23:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RLz1m6lw5z4NkV; Thu, 10 Aug 2023 07:23:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1691652237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K+ycz7oubavgZCDCBewcIHB4FJ9ZWJhSyfeRPK1leUU=; b=d0gews4j4S2N6MmB0NBAcWXODsLyapMkPJ7FkEAB34I+d4hvTLKAnNpIYYAamELT2vx9mw RCnDVS8HpMmf9X5qawCYqv1T+TkxOzVgY0rh5ajaABKv7X3O5VrcBWTQfTEiGhsnyECZH3 D9BlNwCi2gB8cT3yaMundDzPK02RbdroF6DfsZBprG2Ms6zdiMH4zr0Z2IVR2YOYDIW80/ u26qhkWsd+CvCoXMVbBhrLng7CzAxgiTAbHc/G9SAKBWf0Ob9JDtuHLY9KTHCV+j88LKwy aahIuTXti5Y06fGXIC7mPBuIG35vbIN27pJhafrVKSYDKPqM4DeJ3wga1gagJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1691652237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K+ycz7oubavgZCDCBewcIHB4FJ9ZWJhSyfeRPK1leUU=; b=Br0BDZ7OFczKCXim+ee6c1zpFVzstYz1nG/pFNPWS9asPWFzlygJvHPlRLyf4YZC582gEV gIUk1n31flE8btO2TD38hwWAwsjRHTnpQBIFnYDMF748fyZvsZwV50ATqF/w3/ranGNAt2 OoVRKURy3ySPUCWxuZ5DPMYWawc8vZWG+8DLWgsdklmx/dkmthQ1PUUtAd23GjXO5rDXH/ /ArjG127mrDKi3u5cnv0iW5UWONrJL88imuLhYR5HzBC7x+EKCBIZrRuhG9FDFrQ88VVT5 lhUIN6Ycj6LQiXFa4X7e3Y4m7t5k+WyAqEx6rEp87Seow7XLHmztCGY2P+BCjA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1691652237; a=rsa-sha256; cv=none; b=OX7rHiFhgv8WPLVcydDxYYvUiAmmrJITwJktTb23wiudsGmm+naHMZKhBqJMikCmwUUTXm HVcE5+7dZp8L9BD1SFnrXqodkMR5sXxeDrn5q/QRM3dcvwKIkr3PSg7H0K8ZO3VEiznDbT 3KZks0F2TuqiEUfFGUXNzQVedj1w6PqM54Ys92Igj81aLiwLm+4TkvZzD71K6WOJ4tdaTF 8bSKIpDxm17cg1V/7NNHYLlVFRzaCAGLOVrTDLjqsQT9dVho1G/zSTe015hVrk6FJfnWQt csjYPTFhWLph5SZtStjyYeRtjihUiLgTRgLnlLQ7rV8nGFds6Nw7V2D5aQ0kzQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RLz1m5rNmzpCs; Thu, 10 Aug 2023 07:23:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37A7NuKT025594; Thu, 10 Aug 2023 07:23:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37A7NuTx025591; Thu, 10 Aug 2023 07:23:56 GMT (envelope-from git) Date: Thu, 10 Aug 2023 07:23:56 GMT Message-Id: <202308100723.37A7NuTx025591@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Corvin =?utf-8?Q?K=C3=B6hne?= Subject: git: 29200a8133c0 - main - bhyve: emulate TPM passthru by accessing /dev/tpmX List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: corvink X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 29200a8133c0963c17508035b63f5ca43db174c8 Auto-Submitted: auto-generated The branch main has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=29200a8133c0963c17508035b63f5ca43db174c8 commit 29200a8133c0963c17508035b63f5ca43db174c8 Author: Corvin Köhne AuthorDate: 2022-08-29 10:10:45 +0000 Commit: Corvin Köhne CommitDate: 2023-08-10 07:23:42 +0000 bhyve: emulate TPM passthru by accessing /dev/tpmX To send commands to the TPM device, bhyve can use the host TPM driver by reading and writing from /dev/tpmX. Using this approach, only the host TPM driver has to detect and interact with the physical TPM interface. This simplifies bhyve's code much. As the host TPM driver has to interact with the TPM regardless of bhyve making use of it or not, makes it a good approach. Reviewed by: markj MFC after: 1 week Sponsored by: Beckhoff Automation GmbH & Co. KG Differential Revision: https://reviews.freebsd.org/D40460 --- usr.sbin/bhyve/tpm_emul_passthru.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/usr.sbin/bhyve/tpm_emul_passthru.c b/usr.sbin/bhyve/tpm_emul_passthru.c index 9d706dee7120..084d31d5fa9f 100644 --- a/usr.sbin/bhyve/tpm_emul_passthru.c +++ b/usr.sbin/bhyve/tpm_emul_passthru.c @@ -22,6 +22,12 @@ struct tpm_passthru { int fd; }; +struct tpm_resp_hdr { + uint16_t tag; + uint32_t len; + uint32_t errcode; +} __packed; + static int tpm_passthru_init(void **sc, nvlist_t *nvl) { @@ -46,6 +52,37 @@ tpm_passthru_init(void **sc, nvlist_t *nvl) return (0); } +static int +tpm_passthru_execute_cmd(void *sc, void *cmd, uint32_t cmd_size, void *rsp, + uint32_t rsp_size) +{ + struct tpm_passthru *tpm; + ssize_t len; + + if (rsp_size < (ssize_t)sizeof(struct tpm_resp_hdr)) { + warn("%s: rsp_size of %u is too small", __func__, rsp_size); + return (EINVAL); + } + + tpm = sc; + + len = write(tpm->fd, cmd, cmd_size); + if (len != cmd_size) { + warn("%s: cmd write failed (bytes written: %zd / %d)", __func__, + len, cmd_size); + return (EFAULT); + } + + len = read(tpm->fd, rsp, rsp_size); + if (len < (ssize_t)sizeof(struct tpm_resp_hdr)) { + warn("%s: rsp read failed (bytes read: %zd / %d)", __func__, + len, rsp_size); + return (EFAULT); + } + + return (0); +} + static void tpm_passthru_deinit(void *sc) { @@ -65,5 +102,6 @@ static const struct tpm_emul tpm_emul_passthru = { .name = "passthru", .init = tpm_passthru_init, .deinit = tpm_passthru_deinit, + .execute_cmd = tpm_passthru_execute_cmd, }; TPM_EMUL_SET(tpm_emul_passthru);