Skip to content

Commit dd3922c

Browse files
committed
Merge tag 'x86_sev_for_v6.16_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull AMD SEV update from Borislav Petkov: "Add a virtual TPM driver glue which allows a guest kernel to talk to a TPM device emulated by a Secure VM Service Module (SVSM) - a helper module of sorts which runs at a different privilege level in the SEV-SNP VM stack. The intent being that a TPM device is emulated by a trusted entity and not by the untrusted host which is the default assumption in the confidential computing scenarios" * tag 'x86_sev_for_v6.16_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/sev: Register tpm-svsm platform device tpm: Add SNP SVSM vTPM driver svsm: Add header with SVSM_VTPM_CMD helpers x86/sev: Add SVSM vTPM probe/send_command functions
2 parents 350a604 + e396dd8 commit dd3922c

File tree

6 files changed

+360
-1
lines changed

6 files changed

+360
-1
lines changed

arch/x86/coco/sev/core.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,11 +1462,74 @@ static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_dat
14621462
return ret;
14631463
}
14641464

1465+
/**
1466+
* snp_svsm_vtpm_probe() - Probe if SVSM provides a vTPM device
1467+
*
1468+
* Check that there is SVSM and that it supports at least TPM_SEND_COMMAND
1469+
* which is the only request used so far.
1470+
*
1471+
* Return: true if the platform provides a vTPM SVSM device, false otherwise.
1472+
*/
1473+
static bool snp_svsm_vtpm_probe(void)
1474+
{
1475+
struct svsm_call call = {};
1476+
1477+
/* The vTPM device is available only if a SVSM is present */
1478+
if (!snp_vmpl)
1479+
return false;
1480+
1481+
call.caa = svsm_get_caa();
1482+
call.rax = SVSM_VTPM_CALL(SVSM_VTPM_QUERY);
1483+
1484+
if (svsm_perform_call_protocol(&call))
1485+
return false;
1486+
1487+
/* Check platform commands contains TPM_SEND_COMMAND - platform command 8 */
1488+
return call.rcx_out & BIT_ULL(8);
1489+
}
1490+
1491+
/**
1492+
* snp_svsm_vtpm_send_command() - Execute a vTPM operation on SVSM
1493+
* @buffer: A buffer used to both send the command and receive the response.
1494+
*
1495+
* Execute a SVSM_VTPM_CMD call as defined by
1496+
* "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00
1497+
*
1498+
* All command request/response buffers have a common structure as specified by
1499+
* the following table:
1500+
* Byte Size     In/Out    Description
1501+
* Offset    (Bytes)
1502+
* 0x000     4          In        Platform command
1503+
 *                         Out       Platform command response size
1504+
*
1505+
* Each command can build upon this common request/response structure to create
1506+
* a structure specific to the command. See include/linux/tpm_svsm.h for more
1507+
* details.
1508+
*
1509+
* Return: 0 on success, -errno on failure
1510+
*/
1511+
int snp_svsm_vtpm_send_command(u8 *buffer)
1512+
{
1513+
struct svsm_call call = {};
1514+
1515+
call.caa = svsm_get_caa();
1516+
call.rax = SVSM_VTPM_CALL(SVSM_VTPM_CMD);
1517+
call.rcx = __pa(buffer);
1518+
1519+
return svsm_perform_call_protocol(&call);
1520+
}
1521+
EXPORT_SYMBOL_GPL(snp_svsm_vtpm_send_command);
1522+
14651523
static struct platform_device sev_guest_device = {
14661524
.name = "sev-guest",
14671525
.id = -1,
14681526
};
14691527

1528+
static struct platform_device tpm_svsm_device = {
1529+
.name = "tpm-svsm",
1530+
.id = -1,
1531+
};
1532+
14701533
static int __init snp_init_platform_device(void)
14711534
{
14721535
if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
@@ -1475,7 +1538,11 @@ static int __init snp_init_platform_device(void)
14751538
if (platform_device_register(&sev_guest_device))
14761539
return -ENODEV;
14771540

1478-
pr_info("SNP guest platform device initialized.\n");
1541+
if (snp_svsm_vtpm_probe() &&
1542+
platform_device_register(&tpm_svsm_device))
1543+
return -ENODEV;
1544+
1545+
pr_info("SNP guest platform devices initialized.\n");
14791546
return 0;
14801547
}
14811548
device_initcall(snp_init_platform_device);

arch/x86/include/asm/sev.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ struct svsm_call {
415415
#define SVSM_ATTEST_SERVICES 0
416416
#define SVSM_ATTEST_SINGLE_SERVICE 1
417417

418+
#define SVSM_VTPM_CALL(x) ((2ULL << 32) | (x))
419+
#define SVSM_VTPM_QUERY 0
420+
#define SVSM_VTPM_CMD 1
421+
418422
#ifdef CONFIG_AMD_MEM_ENCRYPT
419423

420424
extern u8 snp_vmpl;
@@ -512,6 +516,8 @@ void snp_msg_free(struct snp_msg_desc *mdesc);
512516
int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
513517
struct snp_guest_request_ioctl *rio);
514518

519+
int snp_svsm_vtpm_send_command(u8 *buffer);
520+
515521
void __init snp_secure_tsc_prepare(void);
516522
void __init snp_secure_tsc_init(void);
517523

@@ -583,6 +589,7 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
583589
static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
584590
static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
585591
struct snp_guest_request_ioctl *rio) { return -ENODEV; }
592+
static inline int snp_svsm_vtpm_send_command(u8 *buffer) { return -ENODEV; }
586593
static inline void __init snp_secure_tsc_prepare(void) { }
587594
static inline void __init snp_secure_tsc_init(void) { }
588595

drivers/char/tpm/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,15 @@ config TCG_FTPM_TEE
234234
help
235235
This driver proxies for firmware TPM running in TEE.
236236

237+
config TCG_SVSM
238+
tristate "SNP SVSM vTPM interface"
239+
depends on AMD_MEM_ENCRYPT
240+
help
241+
This is a driver for the AMD SVSM vTPM protocol that a SEV-SNP guest
242+
OS can use to discover and talk to a vTPM emulated by the Secure VM
243+
Service Module (SVSM) in the guest context, but at a more privileged
244+
level (usually VMPL0). To compile this driver as a module, choose M
245+
here; the module will be called tpm_svsm.
246+
237247
source "drivers/char/tpm/st33zp24/Kconfig"
238248
endif # TCG_TPM

drivers/char/tpm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ obj-$(CONFIG_TCG_CRB) += tpm_crb.o
4545
obj-$(CONFIG_TCG_ARM_CRB_FFA) += tpm_crb_ffa.o
4646
obj-$(CONFIG_TCG_VTPM_PROXY) += tpm_vtpm_proxy.o
4747
obj-$(CONFIG_TCG_FTPM_TEE) += tpm_ftpm_tee.o
48+
obj-$(CONFIG_TCG_SVSM) += tpm_svsm.o

drivers/char/tpm/tpm_svsm.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
4+
*
5+
* Driver for the vTPM defined by the AMD SVSM spec [1].
6+
*
7+
* The specification defines a protocol that a SEV-SNP guest OS can use to
8+
* discover and talk to a vTPM emulated by the Secure VM Service Module (SVSM)
9+
* in the guest context, but at a more privileged level (usually VMPL0).
10+
*
11+
* [1] "Secure VM Service Module for SEV-SNP Guests"
12+
* Publication # 58019 Revision: 1.00
13+
*/
14+
15+
#include <linux/module.h>
16+
#include <linux/kernel.h>
17+
#include <linux/platform_device.h>
18+
#include <linux/tpm_svsm.h>
19+
20+
#include <asm/sev.h>
21+
22+
#include "tpm.h"
23+
24+
struct tpm_svsm_priv {
25+
void *buffer;
26+
};
27+
28+
static int tpm_svsm_send(struct tpm_chip *chip, u8 *buf, size_t len)
29+
{
30+
struct tpm_svsm_priv *priv = dev_get_drvdata(&chip->dev);
31+
int ret;
32+
33+
ret = svsm_vtpm_cmd_request_fill(priv->buffer, 0, buf, len);
34+
if (ret)
35+
return ret;
36+
37+
/*
38+
* The SVSM call uses the same buffer for the command and for the
39+
* response, so after this call, the buffer will contain the response
40+
* that can be used by .recv() op.
41+
*/
42+
return snp_svsm_vtpm_send_command(priv->buffer);
43+
}
44+
45+
static int tpm_svsm_recv(struct tpm_chip *chip, u8 *buf, size_t len)
46+
{
47+
struct tpm_svsm_priv *priv = dev_get_drvdata(&chip->dev);
48+
49+
/*
50+
* The internal buffer contains the response after we send the command
51+
* to SVSM.
52+
*/
53+
return svsm_vtpm_cmd_response_parse(priv->buffer, buf, len);
54+
}
55+
56+
static struct tpm_class_ops tpm_chip_ops = {
57+
.flags = TPM_OPS_AUTO_STARTUP,
58+
.recv = tpm_svsm_recv,
59+
.send = tpm_svsm_send,
60+
};
61+
62+
static int __init tpm_svsm_probe(struct platform_device *pdev)
63+
{
64+
struct device *dev = &pdev->dev;
65+
struct tpm_svsm_priv *priv;
66+
struct tpm_chip *chip;
67+
int err;
68+
69+
priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
70+
if (!priv)
71+
return -ENOMEM;
72+
73+
/*
74+
* The maximum buffer supported is one page (see SVSM_VTPM_MAX_BUFFER
75+
* in tpm_svsm.h).
76+
*/
77+
priv->buffer = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
78+
if (!priv->buffer)
79+
return -ENOMEM;
80+
81+
chip = tpmm_chip_alloc(dev, &tpm_chip_ops);
82+
if (IS_ERR(chip))
83+
return PTR_ERR(chip);
84+
85+
dev_set_drvdata(&chip->dev, priv);
86+
87+
err = tpm2_probe(chip);
88+
if (err)
89+
return err;
90+
91+
err = tpm_chip_register(chip);
92+
if (err)
93+
return err;
94+
95+
dev_info(dev, "SNP SVSM vTPM %s device\n",
96+
(chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2");
97+
98+
return 0;
99+
}
100+
101+
static void __exit tpm_svsm_remove(struct platform_device *pdev)
102+
{
103+
struct tpm_chip *chip = platform_get_drvdata(pdev);
104+
105+
tpm_chip_unregister(chip);
106+
}
107+
108+
/*
109+
* tpm_svsm_remove() lives in .exit.text. For drivers registered via
110+
* module_platform_driver_probe() this is ok because they cannot get unbound
111+
* at runtime. So mark the driver struct with __refdata to prevent modpost
112+
* triggering a section mismatch warning.
113+
*/
114+
static struct platform_driver tpm_svsm_driver __refdata = {
115+
.remove = __exit_p(tpm_svsm_remove),
116+
.driver = {
117+
.name = "tpm-svsm",
118+
},
119+
};
120+
121+
module_platform_driver_probe(tpm_svsm_driver, tpm_svsm_probe);
122+
123+
MODULE_DESCRIPTION("SNP SVSM vTPM Driver");
124+
MODULE_LICENSE("GPL");
125+
MODULE_ALIAS("platform:tpm-svsm");

0 commit comments

Comments
 (0)