From nobody Thu Apr 10 21:20:23 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 435BC24889E for ; Thu, 3 Apr 2025 10:09:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743674998; cv=none; b=ZoeFq6rXnk9H1QPCoWeaDXW5lIsDHdR5lflqliYyBdwA6PvORyPnEhJZj+lupB55W7y8wwSpHkphRXmGOSarhcaDM76ex5ppmqD1les2PTGAu6TFaDwwHvUdIQIlsJvikg76PwYiFZHgolD33ZpimKsQXvgsNYl/fP3FoZtYqyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743674998; c=relaxed/simple; bh=MzD9Ad6mK9r4fxgZHo9dfRHlp4OQUEWf4LOVIPBSNt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VqEh0I5smzKL7v5Er3JiIZTxJxb96XrXvmA3Z/npFIZOvvuYKROAKzBzHN1aeFLxw6MHss1jUYHf44FDq+4WjmHFNAA8ysB9AcvviYeFmZa3eCjp8Qf5x6+A8r3e7aOJFriVApyT8guSXg24IVX6UjW3B4cP3gBoLSCer2JN1pE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YVHW88wS; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YVHW88wS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743674994; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lpoVdmq8NVnUnZOD3EnO3m6uLwgKHTodPmRunL4I+rs=; b=YVHW88wStBo8sRtmmnVERR2Xyuhl0lviTRrw6c0mMrcyOnKubf5HSeBu5uw+NTmcPkOzH5 LV5Tsde0WPblEdMAchQqzBzYWJVMC8YLPSl/RQI7NumKzWq66Kj3KK7zURVLBvuXisd6eU RzPiHnilV59NPVBUCgD4jGEhLTBdHZE= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-iDmnS0w0OMGODNVKgA3IUA-1; Thu, 03 Apr 2025 06:09:53 -0400 X-MC-Unique: iDmnS0w0OMGODNVKgA3IUA-1 X-Mimecast-MFC-AGG-ID: iDmnS0w0OMGODNVKgA3IUA_1743674992 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43d22c304adso8628015e9.0 for ; Thu, 03 Apr 2025 03:09:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743674992; x=1744279792; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lpoVdmq8NVnUnZOD3EnO3m6uLwgKHTodPmRunL4I+rs=; b=WWOB+5o73dnbB6tMImaQk/dNVrnUN08JTRSBi8Mm4W7dmOOB+VtZBVDFtFW2oE9Vca B/FCL/EIgZjWwvBjQ18CLUDZX91TRHfcFphLZ6Rc/u+YGFWl4zxfxwkiusGEB/Jn5r+L SB1X3UwAENC5ah0MA+3f8Px3oFtARF+MjKhU+zemqF62fP7GHr2ecUeLUJQeIEmaAAn2 YW35So/Qlh+hb9I1NOpuNxBq8Q74yomx+IhrjrUVnhKB6W/rNOL4XpbQETGCaO+m7WJu NF1EHW39LOQnNkMeQz6azUdrX7cwFr+xlKLvVuk+dgDGmi14UCEEvMDV3P0v13vLYemw RmoA== X-Forwarded-Encrypted: i=1; AJvYcCWPdN+SxlPaeNWtWd5A89MF8d17CnhYyzrP7wcfB1pDmW4R/xwq6HvhLpXaQ2UPmhhqr2saItLW6LX5IRs=@vger.kernel.org X-Gm-Message-State: AOJu0YxtvCD7AT73hKLCKPCl9Ev8GqIJE6TawKaepStlFefXITVSxq05 pSktd29vwiqp2Whf27J53kWnsRisJ9GrET5/hUpCl3Q/KHTlmpNAR+QAs5jGpfc6K3Tu7xHrGgZ oNpWX+i4z3LM4RDt/5AuN9zFMJKx0qBnQsfAavsHzHMajXJLkvfe0/M9TVfp/sw== X-Gm-Gg: ASbGnct5VxJk9CU52Plllkz1ps0BJp06kWWtAJ0C03m3F+zekU1lCFy7FKwP3gBiuUk pQRlLWWldzkv/srC1TshOIfWLMlWDCm8Jh7vZ0/q933S+jd86iOH06ZK4txs1I0tlbzVO2a5CgK 7EEEu8CXhkmO1/TAF/k49+MRE62aKZiu005wo5BWV//iuVgNv9/Uz3L/n92qhd6b/ylmn6RO0Zp TnWTHhqfHqaEahgtmjUUAqwahejaLTg0prJ6480PD5i5xEgUepFecG+boSUbYits1Gl9TDCQnQY YVPTnq5dMRIlFPVG9enXioCXs1CeKLmGVKKHtAcMe0z0ZF9RwUHk2uILy77H X-Received: by 2002:a05:600c:1ca3:b0:439:9737:675b with SMTP id 5b1f17b1804b1-43ebeed6dd4mr24861295e9.7.1743674991880; Thu, 03 Apr 2025 03:09:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEhK91zBiijUbCC0j/FPAiwjeqLDEV12HIxEw5TJqrQUqDMe/B3PRBRIv3dfMrFpNdP4Zc8Jg== X-Received: by 2002:a05:600c:1ca3:b0:439:9737:675b with SMTP id 5b1f17b1804b1-43ebeed6dd4mr24860915e9.7.1743674991373; Thu, 03 Apr 2025 03:09:51 -0700 (PDT) Received: from stex1.redhat.com (host-87-11-6-59.retail.telecomitalia.it. [87.11.6.59]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c30226f2bsm1368623f8f.96.2025.04.03.03.09.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 03:09:50 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Dave Hansen , Peter Huewe , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, linux-integrity@vger.kernel.org, Borislav Petkov , Ingo Molnar , x86@kernel.org, Tom Lendacky , Joerg Roedel , Dionna Glaze , Claudio Carvalho , James Bottomley , linux-kernel@vger.kernel.org, Dov Murik , Thomas Gleixner , Stefano Garzarella Subject: [PATCH v6 1/4] x86/sev: add SVSM vTPM probe/send_command functions Date: Thu, 3 Apr 2025 12:09:39 +0200 Message-ID: <20250403100943.120738-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250403100943.120738-1-sgarzare@redhat.com> References: <20250403100943.120738-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Stefano Garzarella Add two new functions to probe and send commands to the SVSM vTPM. They leverage the two calls defined by the AMD SVSM specification [1] for the vTPM protocol: SVSM_VTPM_QUERY and SVSM_VTPM_CMD. Expose these functions to be used by other modules such as a tpm driver. [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Co-developed-by: James Bottomley Signed-off-by: James Bottomley Co-developed-by: Claudio Carvalho Signed-off-by: Claudio Carvalho Reviewed-by: Tom Lendacky Reviewed-by: Jarkko Sakkinen Signed-off-by: Stefano Garzarella --- v5: - added stubs when !CONFIG_AMD_MEM_ENCRYPT [Dionna] - added Jarkko's R-b v4: - added Tom's R-b - added functions documentation [Jarkko] - simplified TPM_SEND_COMMAND check [Tom/Jarkko] v3: - removed link to the spec because those URLs are unstable [Borislav] - squashed "x86/sev: add SVSM call macros for the vTPM protocol" patch in this one [Borislav] - slimmed down snp_svsm_vtpm_probe() [Borislav] - removed features check and any print related [Tom] --- arch/x86/include/asm/sev.h | 9 ++++++ arch/x86/coco/sev/core.c | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ba7999f66abe..ba7a0a327afb 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -384,6 +384,10 @@ struct svsm_call { #define SVSM_ATTEST_SERVICES 0 #define SVSM_ATTEST_SINGLE_SERVICE 1 =20 +#define SVSM_VTPM_CALL(x) ((2ULL << 32) | (x)) +#define SVSM_VTPM_QUERY 0 +#define SVSM_VTPM_CMD 1 + #ifdef CONFIG_AMD_MEM_ENCRYPT =20 extern u8 snp_vmpl; @@ -481,6 +485,9 @@ void snp_msg_free(struct snp_msg_desc *mdesc); int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_re= q *req, struct snp_guest_request_ioctl *rio); =20 +bool snp_svsm_vtpm_probe(void); +int snp_svsm_vtpm_send_command(u8 *buffer); + void __init snp_secure_tsc_prepare(void); void __init snp_secure_tsc_init(void); =20 @@ -524,6 +531,8 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) = { return NULL; } static inline void snp_msg_free(struct snp_msg_desc *mdesc) { } static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struc= t snp_guest_req *req, struct snp_guest_request_ioctl *rio) { return -ENODEV; } +static inline bool snp_svsm_vtpm_probe(void) { return false; } +static inline int snp_svsm_vtpm_send_command(u8 *buffer) { return -ENODEV;= } static inline void __init snp_secure_tsc_prepare(void) { } static inline void __init snp_secure_tsc_init(void) { } =20 diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index b0c1a7a57497..efb43c9d3d30 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2625,6 +2625,65 @@ static int snp_issue_guest_request(struct snp_guest_= req *req, struct snp_req_dat return ret; } =20 +/** + * snp_svsm_vtpm_probe() - Probe if SVSM provides a vTPM device + * + * This function checks that there is SVSM and that it supports at least + * TPM_SEND_COMMAND which is the only request we use so far. + * + * Return: true if the platform provides a vTPM SVSM device, false otherwi= se. + */ +bool snp_svsm_vtpm_probe(void) +{ + struct svsm_call call =3D {}; + + /* The vTPM device is available only if a SVSM is present */ + if (!snp_vmpl) + return false; + + call.caa =3D svsm_get_caa(); + call.rax =3D SVSM_VTPM_CALL(SVSM_VTPM_QUERY); + + if (svsm_perform_call_protocol(&call)) + return false; + + /* Check platform commands contains TPM_SEND_COMMAND - platform command 8= */ + return call.rcx_out & BIT_ULL(8); +} +EXPORT_SYMBOL_GPL(snp_svsm_vtpm_probe); + +/** + * snp_svsm_vtpm_send_command() - execute a vTPM operation on SVSM + * @buffer: A buffer used to both send the command and receive the respons= e. + * + * This function executes a SVSM_VTPM_CMD call as defined by + * "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revis= ion: 1.00 + * + * All command request/response buffers have a common structure as specifi= ed by + * the following table: + * Byte Size =C2=A0=C2=A0 =C2=A0In/Out=C2=A0=C2=A0=C2=A0=C2=A0D= escription + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0In=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Platform command +=C2=A0*=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Out=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command respons= e size + * + * Each command can build upon this common request/response structure to c= reate + * a structure specific to the command. + * See include/linux/tpm_svsm.h for more details. + * + * Return: 0 on success, -errno on failure + */ +int snp_svsm_vtpm_send_command(u8 *buffer) +{ + struct svsm_call call =3D {}; + + call.caa =3D svsm_get_caa(); + call.rax =3D SVSM_VTPM_CALL(SVSM_VTPM_CMD); + call.rcx =3D __pa(buffer); + + return svsm_perform_call_protocol(&call); +} +EXPORT_SYMBOL_GPL(snp_svsm_vtpm_send_command); + static struct platform_device sev_guest_device =3D { .name =3D "sev-guest", .id =3D -1, --=20 2.49.0 From nobody Thu Apr 10 21:20:23 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35AB224A074 for ; Thu, 3 Apr 2025 10:09:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743675002; cv=none; b=T9WMN6jCJu08KSwbtnBCyfK7kVJN6DqYQ2413TUJ2KxLrlihs3nQ2lQKi6KTb7P3+YFXjLLw5oPWnNvXL8vUp2satNW55sBAHYa5DJhn31vppoX2xrolJxoflC8W7MecYRuHB/qiR/SLAdz6/4c/0C/m4BgmEU6qZiyJBlD4yyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743675002; c=relaxed/simple; bh=JanhuPR/iml68kEFk6VsdfPYwqYcdQYvtjajdTMi5bI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IZ1Oiak/DmMQ/oSockhq/n8lcJFPc/p8Ei0MlEKyaQczCgp6O+Uz0zH4UsLVMao7KDtb2codM0srzrh1FMgXgxVQd2YQB15EsjTg7X3znYLop2rumRnkvJWPF9YmUv/JPXRTfMJf2y58r13WH3bL3jSkKCzbYN/JGtJNnDbI8eM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eCcbtwv4; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eCcbtwv4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743674999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9cwx8PB++1jPTpqj+0lA5MhEHp2F2B5hQ/5H2FoJu9E=; b=eCcbtwv4X3tYU5SReXklqybEmQMrj14X+WVqa+d0YJh5e5aS+EVG423N0HXhwIEmigiW6a CFgfkl0SeBqdTKQRzO1vs5ChQUo1zBzGS9I/xrVgD1d/v04DBBIIriYoRtfySvE5Fs+yQ+ hK1Z4l0e/GhBAMYjVmZwV0smRRk7qyM= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-AA9G1v0qMpirkmeVzHjF5w-1; Thu, 03 Apr 2025 06:09:57 -0400 X-MC-Unique: AA9G1v0qMpirkmeVzHjF5w-1 X-Mimecast-MFC-AGG-ID: AA9G1v0qMpirkmeVzHjF5w_1743674997 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-399744f742bso226417f8f.1 for ; Thu, 03 Apr 2025 03:09:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743674997; x=1744279797; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9cwx8PB++1jPTpqj+0lA5MhEHp2F2B5hQ/5H2FoJu9E=; b=fcVnUhjMLjJ72JY3AF0uPdnwHGEoZghIR/ygwMTlJjgpptAEhm9OjOQ+ZSEiqUrSQ0 blFM5hj/yjdOOZXqqn73jn8mB9oXOLEvRR0AowZsrHFPQ9XDoyzggzcUicB08W6E1PST o/MlpUFoK80CTLk+pDx9PwLJPX+Y+o4cyeIpg5i5lt8C5eecxZM5pSsxduxZzc6TiHrh eYT85G/DNq1nOpNDAN1npYjhLXRZGuZggDq6sShzv/rp7LumiE8QFWUhjsHR7XkN+Ha6 FKzwpURGgB74CTVbHfeV2E9N/NCxFjOBWns6ndNmFUmxAkn1NdMrPRdbcANtsUVSNYVO v1zA== X-Forwarded-Encrypted: i=1; AJvYcCVLVkwgy8zj+YcZlwNlWq7BteK9usRDxoh0pKkYvR8Hvy1cwvrSwwPkumCUi0VMuEZfP4vjdg00NBEaQe0=@vger.kernel.org X-Gm-Message-State: AOJu0YzXFq6pLNhOpgynzdfA6qsqM/plkZdnPJYDJoXwtdyOL32sZvoT J/7Ccd3hDbkqfrREaErHsxZwOaDT47il+cukYBXuipB5JBNtUU7AkIJyis7Tyk0Z2SqPP2dgHqF 0t3+LayDI4KmOuynVw8HCmPVnIV9lMettkkEPdDoaZI7+pETa44lEYuwiU8rCBw== X-Gm-Gg: ASbGncv+cLSFgvZ+aAr1zLpTfKvxQ1ieCCIDCpSAE7IxYd4gOtHkMO0S07bl0oFMVUv y3J3AbMZc+mNZOQQjQIQ0RmxJC1ArORiONTEEt7VJS5RZAEg83XR1F/rG+nxeMp7j97ENR0PuRi BlExAOAfvwGecNwtFVn6jdvMOpF5F9cNwCuBKau7Y2bCNBLB7yaS1xf5MEa7r2edq9U6o3Rw2Vk NIzeOZ91CJx/FzWm4LG5F5DB99o60c9ye8z9ubd1F9u3UlXD1XL2gh/Txm17W4z4F1xetrT8t01 V0SmWZ7gSzSg/pyE2pBqHm5PXWwh5KadMzI/xpffjKObTE6td7de6+kOoV3+ X-Received: by 2002:a05:6000:4023:b0:39c:119f:27c4 with SMTP id ffacd0b85a97d-39c29767c83mr5000304f8f.30.1743674996676; Thu, 03 Apr 2025 03:09:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFF/dLWCdMcq0LYWuX6GJvujzOPjM/4jK9yFrsoYlFUFwEyPuB2KMh6zCBm2HL0y2v/2J1nkA== X-Received: by 2002:a05:6000:4023:b0:39c:119f:27c4 with SMTP id ffacd0b85a97d-39c29767c83mr5000270f8f.30.1743674996120; Thu, 03 Apr 2025 03:09:56 -0700 (PDT) Received: from stex1.redhat.com (host-87-11-6-59.retail.telecomitalia.it. [87.11.6.59]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ec1795630sm17531555e9.29.2025.04.03.03.09.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 03:09:55 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Dave Hansen , Peter Huewe , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, linux-integrity@vger.kernel.org, Borislav Petkov , Ingo Molnar , x86@kernel.org, Tom Lendacky , Joerg Roedel , Dionna Glaze , Claudio Carvalho , James Bottomley , linux-kernel@vger.kernel.org, Dov Murik , Thomas Gleixner , Stefano Garzarella Subject: [PATCH v6 2/4] svsm: add header with SVSM_VTPM_CMD helpers Date: Thu, 3 Apr 2025 12:09:40 +0200 Message-ID: <20250403100943.120738-3-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250403100943.120738-1-sgarzare@redhat.com> References: <20250403100943.120738-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Stefano Garzarella Helpers for the SVSM_VTPM_CMD calls used by the vTPM protocol defined by the AMD SVSM spec [1]. The vTPM protocol follows the Official TPM 2.0 Reference Implementation (originally by Microsoft, now part of the TCG) simulator protocol. [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Co-developed-by: James Bottomley Signed-off-by: James Bottomley Co-developed-by: Claudio Carvalho Signed-off-by: Claudio Carvalho Reviewed-by: Jarkko Sakkinen Signed-off-by: Stefano Garzarella --- v5: - added Jarkko's R-b v4: - used svsm_vtpm_ prefix consistently [Jarkko] - removed __packed where not needed [Jarkko] - expanded headers to avoid obfuscation [Jarkko] - used `buf` instead of `inbuf`/`outbuf` [Jarkko] - added more documentation quoting the specification - removed TPM_* macros since we only use TPM_SEND_COMMAND in one place and don't want dependencies on external headers, but put the value directly as specified in the AMD SVSM specification - header renamed in tpm_svsm.h so it will fall under TPM DEVICE DRIVER section [Borislav, Jarkko] v3: - renamed header and prefix to make clear it's related to the SVSM vTPM protocol - renamed fill/parse functions [Tom] - removed link to the spec because those URLs are unstable [Borislav] --- include/linux/tpm_svsm.h | 149 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 include/linux/tpm_svsm.h diff --git a/include/linux/tpm_svsm.h b/include/linux/tpm_svsm.h new file mode 100644 index 000000000000..38e341f9761a --- /dev/null +++ b/include/linux/tpm_svsm.h @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2023 James.Bottomley@HansenPartnership.com + * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved. + * + * Helpers for the SVSM_VTPM_CMD calls used by the vTPM protocol defined b= y the + * AMD SVSM spec [1]. + * + * The vTPM protocol follows the Official TPM 2.0 Reference Implementation + * (originally by Microsoft, now part of the TCG) simulator protocol. + * + * [1] "Secure VM Service Module for SEV-SNP Guests" + * Publication # 58019 Revision: 1.00 + */ +#ifndef _TPM_SVSM_H_ +#define _TPM_SVSM_H_ + +#include +#include +#include + +#define SVSM_VTPM_MAX_BUFFER 4096 /* max req/resp buffer size */ + +/** + * struct svsm_vtpm_request - Generic request for single word command + * @cmd: The command to send + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 15: vTPM Common Request/Response Structure + * Byte Size =C2=A0=C2=A0 =C2=A0In/Out=C2=A0=C2=A0=C2=A0=C2=A0D= escription + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0In=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Platform command +=C2=A0*=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Out=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command respons= e size + */ +struct svsm_vtpm_request { + u32 cmd; +}; + +/** + * struct svsm_vtpm_response - Generic response + * @size: The response size (zero if nothing follows) + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 15: vTPM Common Request/Response Structure + * Byte Size =C2=A0=C2=A0 =C2=A0In/Out=C2=A0=C2=A0=C2=A0=C2=A0D= escription + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0In=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Platform command +=C2=A0*=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0Out=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command respons= e size + * + * Note: most TCG Simulator commands simply return zero here with no indic= ation + * of success or failure. + */ +struct svsm_vtpm_response { + u32 size; +}; + +/** + * struct svsm_vtpm_cmd_request - Structure for a TPM_SEND_COMMAND request + * @cmd: The command to send (must be TPM_SEND_COMMAND) + * @locality: The locality + * @buf_size: The size of the input buffer following + * @buf: A buffer of size buf_size + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 16: TPM_SEND_COMMAND Request Structure + * Byte Size Meaning + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Platform command (8) + * 0x004=C2=A0=C2=A0=C2=A0=C2=A0=C2=A01=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Locality (must-be-0) + * 0x005=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0TPM Command size (in bytes) + * 0x009=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Variable=C2=A0=C2=A0=C2=A0TPM Co= mmand + * + * Note: the TCG Simulator expects @buf_size to be equal to the size of the + * specific TPM command, otherwise an TPM_RC_COMMAND_SIZE error is returne= d. + */ +struct svsm_vtpm_cmd_request { + u32 cmd; + u8 locality; + u32 buf_size; + u8 buf[]; +} __packed; + +/** + * struct svsm_vtpm_cmd_response - Structure for a TPM_SEND_COMMAND respon= se + * @buf_size: The size of the output buffer following + * @buf: A buffer of size buf_size + * + * Defined by AMD SVSM spec [1] in section "8.2 SVSM_VTPM_CMD Call" - + * Table 17: TPM_SEND_COMMAND Response Structure + * Byte Size Meaning + * Offset=C2=A0=C2=A0=C2=A0=C2=A0(Bytes) + * 0x000=C2=A0=C2=A0=C2=A0=C2=A0=C2=A04=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Response size (in bytes) + * 0x004=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Variable=C2=A0=C2=A0=C2=A0Respon= se + */ +struct svsm_vtpm_cmd_response { + u32 buf_size; + u8 buf[]; +}; + +/** + * svsm_vtpm_cmd_request_fill() - Fill a TPM_SEND_COMMAND request to be se= nt to SVSM + * @req: The struct svsm_vtpm_cmd_request to fill + * @locality: The locality + * @buf: The buffer from where to copy the payload of the command + * @len: The size of the buffer + * + * Return: 0 on success, negative error code on failure. + */ +static inline int +svsm_vtpm_cmd_request_fill(struct svsm_vtpm_cmd_request *req, u8 locality, + const u8 *buf, size_t len) +{ + if (len > SVSM_VTPM_MAX_BUFFER - sizeof(*req)) + return -EINVAL; + + req->cmd =3D 8; /* TPM_SEND_COMMAND */ + req->locality =3D locality; + req->buf_size =3D len; + + memcpy(req->buf, buf, len); + + return 0; +} + +/** + * svsm_vtpm_cmd_response_parse() - Parse a TPM_SEND_COMMAND response rece= ived from SVSM + * @resp: The struct svsm_vtpm_cmd_response to parse + * @buf: The buffer where to copy the response + * @len: The size of the buffer + * + * Return: buffer size filled with the response on success, negative error + * code on failure. + */ +static inline int +svsm_vtpm_cmd_response_parse(const struct svsm_vtpm_cmd_response *resp, u8= *buf, + size_t len) +{ + if (len < resp->buf_size) + return -E2BIG; + + if (resp->buf_size > SVSM_VTPM_MAX_BUFFER - sizeof(*resp)) + return -EINVAL; // Invalid response from the platform TPM + + memcpy(buf, resp->buf, resp->buf_size); + + return resp->buf_size; +} + +#endif /* _TPM_SVSM_H_ */ --=20 2.49.0 From nobody Thu Apr 10 21:20:23 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E762524BBF6 for ; Thu, 3 Apr 2025 10:10:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743675006; cv=none; b=iaZ1la3o6+3q0Vq4Ok1Fu39yjZ24I51zjQ8ffmfW3CgM9bD7CPjhjZAXzitFP+2F0RM8TO4u5cQe1WUc7aR3Iq+5bpYtQnIrNhJP2EixGC3vmQ9N/556xWNroHUad5vJB8+o+C3g/WuW2ubZvd2iRKHOYMqzbn2wXsxIpAdiCgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743675006; c=relaxed/simple; bh=nqTYW20eQeRWC6Ahiua5kvtbrZfZBXVwk+9t2ZFiCdk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ApuEAOyGJybatTMKFF4xO2Gqo3AY3KMlIqBMBWVzwZGoZKbXmeQoqKYYIxaRrzSroJ0xB2WA8qY/UI5vvMpfTrm/bIBsFWTaauChIV/22aoAPhk4wb4SnSAH9xAUMxm9681rzvB1ca4ao1ORJBdSDkEqNbXElNCGNgZZraRRqxo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=E3Huxy78; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E3Huxy78" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743675004; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HHN/GOOnq6Txv1XxeXH0YVjxE8uHCc3AcxqH5SqtB70=; b=E3Huxy78ihC7NvSqmO28BEoPlLbpvLY71C6tiCupcS1NZkM8OA9v06ITc3V2cFkUK6os6T /rG/1b6x5Bj6eT1+AT5Cyg9nvyQKm2Z6LkCNTDK07VuG6dNXJWrdtpledFTqskx3lMEET2 vAHUpPLzmVeY5mRlp5rzE5yeBJwrCxA= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-653-HAjnm6OGM5uSc0Zny8IIYw-1; Thu, 03 Apr 2025 06:10:03 -0400 X-MC-Unique: HAjnm6OGM5uSc0Zny8IIYw-1 X-Mimecast-MFC-AGG-ID: HAjnm6OGM5uSc0Zny8IIYw_1743675002 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43cf172ff63so4066595e9.3 for ; Thu, 03 Apr 2025 03:10:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743675001; x=1744279801; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HHN/GOOnq6Txv1XxeXH0YVjxE8uHCc3AcxqH5SqtB70=; b=Wg2bqQv/V6qBk3kMHEVPb3Aa84rZ9tHaNkizG7OcawJAmRTQC4JcsrnXmklzMjbOgb 8f9MS5HEemVwog8YWe8hProhLNNymEtHVZfEEoBgTbIKeKDjnS4O1aK9yJ1ySRvo+HBk rJrGa0fhRLEXDUgsoNcsRTomJ5wVhXBr9ztf95Y9rST644FjSZSMmMVAquI2wcpYe3aG 3VvQfyx1/pLO/dvb0gohghblSIggavXY8zfLe9Mv2co824xVBfpf9A0Mu1uXWyEIwwuK EW/i+BhHdHK5t7SXaR84YhazdxhHpKwB4fckR4RPkxKfqM1Lkhxvl9m91+88MJE9l1Zz oQ6Q== X-Forwarded-Encrypted: i=1; AJvYcCVycEX2fUKwRpJmf1aYUWixZVxB9nU2EdYcQw44LLglGTU/xHWaT82hSgTnMk31hixj5ZBQGXJDNCykglY=@vger.kernel.org X-Gm-Message-State: AOJu0YxULQ0KMn0LtaxS/Ur1pvNpZIufIaCKnrUyNwPf/qZ9fkWAZuic gqFxt8w2Len248ZK6X4KvB2wS3xaPk+mO627IeLGSwQT30xuM7Fr3BUPROp09v0cm9kmQbU+k0r FsOt6ZQdD/Gl1LmbsYvUBHSfgyXNI8UVxoKbIGIP/9dOf82y01Of9fP4VbsskrQ== X-Gm-Gg: ASbGnctCxaJ/s5CVBwO5oj/CbccxCKwHo7op1jzAC1ciBv8oglbzPdj3BxQ1Gz+t9M5 dsJu1bsRAjWZINixwrm02LYiteh4InUJAsf4roi19IBsB7nMpN5njvR01C+NkSB3yWpezXNRZYT dF6ppYyBKtbItNpw6I6TM+b1CRo/u+RUQcsERmTG6rB85RiooveGGi5Gw8ZFVm8C37zzj4wILOv yZeqHlqLevGv3cXO7NwiKNrnkTZMKOo3haKt3PDOSnWeHYv/FxkgY4fSm2axsBfmSMW+wM+iN2q i1CybP1pFhlGEgHJtbBbtFUNVrSFUMChkPedkQAD9DnJ/IyiUkTHwgRKHR2k X-Received: by 2002:a05:6000:1785:b0:39c:2661:4ce0 with SMTP id ffacd0b85a97d-39c3032df6fmr1771664f8f.13.1743675001472; Thu, 03 Apr 2025 03:10:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwZgz6CFxdlnhGWRxVWtOMzdrp4cTJAX2/XgXKjJ5i6StpXDDY/u+vUjfxZIDvSXh82PBRMA== X-Received: by 2002:a05:6000:1785:b0:39c:2661:4ce0 with SMTP id ffacd0b85a97d-39c3032df6fmr1771614f8f.13.1743675000890; Thu, 03 Apr 2025 03:10:00 -0700 (PDT) Received: from stex1.redhat.com (host-87-11-6-59.retail.telecomitalia.it. [87.11.6.59]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c301a67b7sm1345908f8f.25.2025.04.03.03.09.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 03:10:00 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Dave Hansen , Peter Huewe , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, linux-integrity@vger.kernel.org, Borislav Petkov , Ingo Molnar , x86@kernel.org, Tom Lendacky , Joerg Roedel , Dionna Glaze , Claudio Carvalho , James Bottomley , linux-kernel@vger.kernel.org, Dov Murik , Thomas Gleixner , Stefano Garzarella Subject: [PATCH v6 3/4] tpm: add SNP SVSM vTPM driver Date: Thu, 3 Apr 2025 12:09:41 +0200 Message-ID: <20250403100943.120738-4-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250403100943.120738-1-sgarzare@redhat.com> References: <20250403100943.120738-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Stefano Garzarella Add driver for the vTPM defined by the AMD SVSM spec [1]. The specification defines a protocol that a SEV-SNP guest OS can use to discover and talk to a vTPM emulated by the Secure VM Service Module (SVSM) in the guest context, but at a more privileged level (VMPL0). The new tpm-svsm platform driver uses two functions exposed by x86/sev to verify that the device is actually emulated by the platform and to send commands and receive responses. The device cannot be hot-plugged/unplugged as it is emulated by the platform, so we can use module_platform_driver_probe(). The probe function will only check whether in the current runtime configuration, SVSM is present and provides a vTPM. This device does not support interrupts and sends responses to commands synchronously. In order to have .recv() called just after .send() in tpm_try_transmit(), the .status() callback returns 0, and both .req_complete_mask and .req_complete_val are set to 0. [1] "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 Signed-off-by: Stefano Garzarella Reviewed-by: Jarkko Sakkinen --- v6: - removed the `locality` field (set to 0) and the FIXME comment [Jarkko] v5: - removed cancel/status/req_* ops after rebase on master that cotains commit 980a573621ea ("tpm: Make chip->{status,cancel,req_canceled} opt") v4: - moved "asm" includes after the "linux" includes [Tom] - allocated buffer separately [Tom/Jarkko/Jason] v3: - removed send_recv() ops and followed the ftpm driver implementing .status, .req_complete_mask, .req_complete_val, etc. [Jarkko] - removed link to the spec because those URLs are unstable [Borislav] --- drivers/char/tpm/tpm_svsm.c | 128 ++++++++++++++++++++++++++++++++++++ drivers/char/tpm/Kconfig | 10 +++ drivers/char/tpm/Makefile | 1 + 3 files changed, 139 insertions(+) create mode 100644 drivers/char/tpm/tpm_svsm.c diff --git a/drivers/char/tpm/tpm_svsm.c b/drivers/char/tpm/tpm_svsm.c new file mode 100644 index 000000000000..b9242c9eab87 --- /dev/null +++ b/drivers/char/tpm/tpm_svsm.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved. + * + * Driver for the vTPM defined by the AMD SVSM spec [1]. + * + * The specification defines a protocol that a SEV-SNP guest OS can use to + * discover and talk to a vTPM emulated by the Secure VM Service Module (S= VSM) + * in the guest context, but at a more privileged level (usually VMPL0). + * + * [1] "Secure VM Service Module for SEV-SNP Guests" + * Publication # 58019 Revision: 1.00 + */ + +#include +#include +#include +#include + +#include + +#include "tpm.h" + +struct tpm_svsm_priv { + void *buffer; +}; + +static int tpm_svsm_send(struct tpm_chip *chip, u8 *buf, size_t len) +{ + struct tpm_svsm_priv *priv =3D dev_get_drvdata(&chip->dev); + int ret; + + ret =3D svsm_vtpm_cmd_request_fill(priv->buffer, 0, buf, len); + if (ret) + return ret; + + /* + * The SVSM call uses the same buffer for the command and for the + * response, so after this call, the buffer will contain the response + * that can be used by .recv() op. + */ + return snp_svsm_vtpm_send_command(priv->buffer); +} + +static int tpm_svsm_recv(struct tpm_chip *chip, u8 *buf, size_t len) +{ + struct tpm_svsm_priv *priv =3D dev_get_drvdata(&chip->dev); + + /* + * The internal buffer contains the response after we send the command + * to SVSM. + */ + return svsm_vtpm_cmd_response_parse(priv->buffer, buf, len); +} + +static struct tpm_class_ops tpm_chip_ops =3D { + .flags =3D TPM_OPS_AUTO_STARTUP, + .recv =3D tpm_svsm_recv, + .send =3D tpm_svsm_send, +}; + +static int __init tpm_svsm_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct tpm_svsm_priv *priv; + struct tpm_chip *chip; + int err; + + if (!snp_svsm_vtpm_probe()) + return -ENODEV; + + priv =3D devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + /* + * The maximum buffer supported is one page (see SVSM_VTPM_MAX_BUFFER + * in tpm_svsm.h). + */ + priv->buffer =3D (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + if (!priv->buffer) + return -ENOMEM; + + chip =3D tpmm_chip_alloc(dev, &tpm_chip_ops); + if (IS_ERR(chip)) + return PTR_ERR(chip); + + dev_set_drvdata(&chip->dev, priv); + + err =3D tpm2_probe(chip); + if (err) + return err; + + err =3D tpm_chip_register(chip); + if (err) + return err; + + dev_info(dev, "SNP SVSM vTPM %s device\n", + (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2"); + + return 0; +} + +static void __exit tpm_svsm_remove(struct platform_device *pdev) +{ + struct tpm_chip *chip =3D platform_get_drvdata(pdev); + + tpm_chip_unregister(chip); +} + +/* + * tpm_svsm_remove() lives in .exit.text. For drivers registered via + * module_platform_driver_probe() this is ok because they cannot get unbou= nd + * at runtime. So mark the driver struct with __refdata to prevent modpost + * triggering a section mismatch warning. + */ +static struct platform_driver tpm_svsm_driver __refdata =3D { + .remove =3D __exit_p(tpm_svsm_remove), + .driver =3D { + .name =3D "tpm-svsm", + }, +}; + +module_platform_driver_probe(tpm_svsm_driver, tpm_svsm_probe); + +MODULE_DESCRIPTION("SNP SVSM vTPM Driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:tpm-svsm"); diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig index fe4f3a609934..dddd702b2454 100644 --- a/drivers/char/tpm/Kconfig +++ b/drivers/char/tpm/Kconfig @@ -234,5 +234,15 @@ config TCG_FTPM_TEE help This driver proxies for firmware TPM running in TEE. =20 +config TCG_SVSM + tristate "SNP SVSM vTPM interface" + depends on AMD_MEM_ENCRYPT + help + This is a driver for the AMD SVSM vTPM protocol that a SEV-SNP guest + OS can use to discover and talk to a vTPM emulated by the Secure VM + Service Module (SVSM) in the guest context, but at a more privileged + level (usually VMPL0). To compile this driver as a module, choose M + here; the module will be called tpm_svsm. + source "drivers/char/tpm/st33zp24/Kconfig" endif # TCG_TPM diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile index 2b004df8c04b..9de1b3ea34a9 100644 --- a/drivers/char/tpm/Makefile +++ b/drivers/char/tpm/Makefile @@ -45,3 +45,4 @@ obj-$(CONFIG_TCG_CRB) +=3D tpm_crb.o obj-$(CONFIG_TCG_ARM_CRB_FFA) +=3D tpm_crb_ffa.o obj-$(CONFIG_TCG_VTPM_PROXY) +=3D tpm_vtpm_proxy.o obj-$(CONFIG_TCG_FTPM_TEE) +=3D tpm_ftpm_tee.o +obj-$(CONFIG_TCG_SVSM) +=3D tpm_svsm.o --=20 2.49.0 From nobody Thu Apr 10 21:20:23 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E777E242936 for ; Thu, 3 Apr 2025 10:10:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743675011; cv=none; b=sOXuvGgQFnSQFbjRowobAK29fL+LnCnulGgYtQ7PgFXWQ9KgvVNeWUts8LwsabSniRlwAph674r2fXmELhW8ebg0if9JHhYAVvJ9u/jKlbaSWJTFrwlJgi0P7YP52tiqb0d7w/jXEVmZrvxEhfFHPQ2TxeSBzOOLuhwVyqK4LPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743675011; c=relaxed/simple; bh=95AbKM5/KwL2zVTjvGMvU2UbnZhLAbo6VoxnM/Vc07I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g+uI18ZJ9cMbsyCsCqiXO30ScgmWbI7Sf1etW8fUiOwZZ73wKuZHl2eCdjrORVGtJQ4PCvA+AoEqLGGg2yAWTjdx51IdcoPrmQrOsG0Zdz51x0KieUFVmBPHP7qClOBvxBqWrmrhGfqHZPyW9wmtLHdTsubKSfTi+3bg4c3ZcPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dGWnilcY; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dGWnilcY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743675008; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qB/CtSEwcy7NwseWKn2u4Fr2QuPsDAEv+lGIosiLsww=; b=dGWnilcY6e6Q4wLc/9BfKyPmm6wst1sylrT4GvYEA6MyXmAnJ35j8a2WNY37Knh+ek+Z+E FWTiIg9iMsZ1sDi5pEAqtFepOt/iZuVmVR0CANiLVHzSZ3GJK4fpKQLsJGETNGsP6ol9gr Yii1gaGBApxkI6yQMc00gEBiuJAbL/Q= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-316-27zLWChLPsKUttsxsdtkzQ-1; Thu, 03 Apr 2025 06:10:07 -0400 X-MC-Unique: 27zLWChLPsKUttsxsdtkzQ-1 X-Mimecast-MFC-AGG-ID: 27zLWChLPsKUttsxsdtkzQ_1743675006 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-39ac9b0cb6aso567549f8f.2 for ; Thu, 03 Apr 2025 03:10:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743675006; x=1744279806; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qB/CtSEwcy7NwseWKn2u4Fr2QuPsDAEv+lGIosiLsww=; b=PCxcm0xjzzbgZoBm2QE1B9R2i/E1ABhFir37WmxwAwE0zUdHhxkiYY1SFXt7sl59CK 75rQYa3bMRB6oWPqaVQTsI+XG/3XacycwZM5SHfW3nKV+o5myXA23tNb24vtAP31mjau R26yJOtrhiOqyjHhxgxNhuLG+X2tcQXVFCfkxYX7q9W4e2YopzrhJmKi7la9gH9qp6da eWZGM1KeYSirCS6+8LBSneeoMyyPciRLOCrFBk8A+Kckv2g1usaTP8NIfjRff+ZSrNUg Z+NTV1IKDvXEBbJCjhqq9KIJidPifxG/M2TKCmgtvoaNDiMxVCwcuoErw4gwcqtrtY15 AC3Q== X-Forwarded-Encrypted: i=1; AJvYcCWLNo/noDlnzsetvGeE5bWWrImYb1GdVbCOXSs6gsL7sWrcOrT/0YXzrzgzQrJAEygVOvZ9GccQ2WSUFBk=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5pWO8cWWMCjjAVsg5PI/1aAfurq0OLJj3SE5BCGCkyIar2556 O+MmiuF8FAtmX2x6iGp+mRzXKSv7J7Xgd+hMxr8cyks0RRhezah1GG2S6Sbhf3Y+XHcH8Quqbqe q2DKsg8YYqLYLqZHYwrUJZf67rXPWvc2JDDPY8bcc0hDDsoybVrBVwjOofRKZGw== X-Gm-Gg: ASbGnctnR3x/tLXxs97gM7vnskJXGhssbVPhplY7Bd219ODzJ3/Tmbz9olj6MVBssFI 1VU4UAA3THG+ERk39tlg/6JHC09EA7mTFhf2MDb/BaZdaBGZ708/5OssgfdyIAWBb2FEEiHK4+m iPiiE85Q1MGtvyEdOaRBSvoxkWwH4cIrTXtVdeXqQN645VD8ZjID0YGmjqHn/VFJHSfCgfiy8wN ibABsn8PxzPzeY4gTFBtgqQlyFAaEt4+Pu8DfF0wA//3QnfNubLghLdVMSqsLSDRowmMeCTm16A H4jn0w0pn3h/DegGotExvyW2+DU0H81w0gjZhHb8eZN56wTA1cDDS9twRTtp X-Received: by 2002:a05:6000:144d:b0:39c:268b:8b66 with SMTP id ffacd0b85a97d-39c297e3f7amr4552172f8f.43.1743675006270; Thu, 03 Apr 2025 03:10:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IECepLnUWcbOzJjNcNGIqxQwtp6LRHNvjNkwMgWLkILIn8YR8+ktWlYOnrFo0WoqQvUci03UA== X-Received: by 2002:a05:6000:144d:b0:39c:268b:8b66 with SMTP id ffacd0b85a97d-39c297e3f7amr4552121f8f.43.1743675005738; Thu, 03 Apr 2025 03:10:05 -0700 (PDT) Received: from stex1.redhat.com (host-87-11-6-59.retail.telecomitalia.it. [87.11.6.59]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ec36691b3sm13702755e9.34.2025.04.03.03.10.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 03:10:04 -0700 (PDT) From: Stefano Garzarella To: Jarkko Sakkinen Cc: Dave Hansen , Peter Huewe , Jason Gunthorpe , "H. Peter Anvin" , linux-coco@lists.linux.dev, linux-integrity@vger.kernel.org, Borislav Petkov , Ingo Molnar , x86@kernel.org, Tom Lendacky , Joerg Roedel , Dionna Glaze , Claudio Carvalho , James Bottomley , linux-kernel@vger.kernel.org, Dov Murik , Thomas Gleixner , Stefano Garzarella Subject: [PATCH v6 4/4] x86/sev: register tpm-svsm platform device Date: Thu, 3 Apr 2025 12:09:42 +0200 Message-ID: <20250403100943.120738-5-sgarzare@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250403100943.120738-1-sgarzare@redhat.com> References: <20250403100943.120738-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Stefano Garzarella SNP platform can provide a vTPM device emulated by SVSM. The "tpm-svsm" device can be handled by the platform driver added by the previous commit in drivers/char/tpm/tpm_svsm.c Register the device unconditionally. The support check (e.g. SVSM, cmd) is in snp_svsm_vtpm_probe(), keeping all logic in one place. This function is called during the driver's probe along with other setup tasks like memory allocation. Reviewed-by: Tom Lendacky Signed-off-by: Stefano Garzarella Reviewed-by: Jarkko Sakkinen --- v6: - added Tom's R-b v4: - explained better why we register it anyway in the commit message --- arch/x86/coco/sev/core.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index efb43c9d3d30..acbd9bc526b1 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2689,6 +2689,11 @@ static struct platform_device sev_guest_device =3D { .id =3D -1, }; =20 +static struct platform_device tpm_svsm_device =3D { + .name =3D "tpm-svsm", + .id =3D -1, +}; + static int __init snp_init_platform_device(void) { if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) @@ -2697,6 +2702,9 @@ static int __init snp_init_platform_device(void) if (platform_device_register(&sev_guest_device)) return -ENODEV; =20 + if (platform_device_register(&tpm_svsm_device)) + return -ENODEV; + pr_info("SNP guest platform device initialized.\n"); return 0; } --=20 2.49.0