1 // SPDX-License-Identifier: GPL-2.0
3 * TDX guest user interface driver
5 * Copyright (C) 2022 Intel Corporation
8 #include <linux/kernel.h>
9 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/string.h>
14 #include <linux/uaccess.h>
15 #include <linux/set_memory.h>
17 #include <linux/delay.h>
18 #include <linux/tsm.h>
19 #include <linux/sizes.h>
21 #include <uapi/linux/tdx-guest.h>
23 #include <asm/cpu_device_id.h>
27 * Intel's SGX QE implementation generally uses Quote size less
28 * than 8K (2K Quote data + ~5K of certificate blob).
30 #define GET_QUOTE_BUF_SIZE SZ_8K
32 #define GET_QUOTE_CMD_VER 1
34 /* TDX GetQuote status codes */
35 #define GET_QUOTE_SUCCESS 0
36 #define GET_QUOTE_IN_FLIGHT 0xffffffffffffffff
38 /* struct tdx_quote_buf: Format of Quote request buffer.
39 * @version: Quote format version, filled by TD.
40 * @status: Status code of Quote request, filled by VMM.
41 * @in_len: Length of TDREPORT, filled by TD.
42 * @out_len: Length of Quote data, filled by VMM.
43 * @data: Quote data on output or TDREPORT on input.
45 * More details of Quote request buffer can be found in TDX
46 * Guest-Host Communication Interface (GHCI) for Intel TDX 1.0,
47 * section titled "TDG.VP.VMCALL<GetQuote>"
49 struct tdx_quote_buf {
57 /* Quote data buffer */
58 static void *quote_data;
60 /* Lock to streamline quote requests */
61 static DEFINE_MUTEX(quote_lock);
64 * GetQuote request timeout in seconds. Expect that 30 seconds
65 * is enough time for QE to respond to any Quote requests.
67 static u32 getquote_timeout = 30;
69 static long tdx_get_report0(struct tdx_report_req __user *req)
71 u8 *reportdata, *tdreport;
74 reportdata = kmalloc(TDX_REPORTDATA_LEN, GFP_KERNEL);
78 tdreport = kzalloc(TDX_REPORT_LEN, GFP_KERNEL);
84 if (copy_from_user(reportdata, req->reportdata, TDX_REPORTDATA_LEN)) {
89 /* Generate TDREPORT0 using "TDG.MR.REPORT" TDCALL */
90 ret = tdx_mcall_get_report0(reportdata, tdreport);
94 if (copy_to_user(req->tdreport, tdreport, TDX_REPORT_LEN))
104 static void free_quote_buf(void *buf)
106 size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
107 unsigned int count = len >> PAGE_SHIFT;
109 if (set_memory_encrypted((unsigned long)buf, count)) {
110 pr_err("Failed to restore encryption mask for Quote buffer, leak it\n");
114 free_pages_exact(buf, len);
117 static void *alloc_quote_buf(void)
119 size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
120 unsigned int count = len >> PAGE_SHIFT;
123 addr = alloc_pages_exact(len, GFP_KERNEL | __GFP_ZERO);
127 if (set_memory_decrypted((unsigned long)addr, count)) {
128 free_pages_exact(addr, len);
136 * wait_for_quote_completion() - Wait for Quote request completion
137 * @quote_buf: Address of Quote buffer.
138 * @timeout: Timeout in seconds to wait for the Quote generation.
140 * As per TDX GHCI v1.0 specification, sec titled "TDG.VP.VMCALL<GetQuote>",
141 * the status field in the Quote buffer will be set to GET_QUOTE_IN_FLIGHT
142 * while VMM processes the GetQuote request, and will change it to success
143 * or error code after processing is complete. So wait till the status
144 * changes from GET_QUOTE_IN_FLIGHT or the request being timed out.
146 static int wait_for_quote_completion(struct tdx_quote_buf *quote_buf, u32 timeout)
151 * Quote requests usually take a few seconds to complete, so waking up
152 * once per second to recheck the status is fine for this use case.
154 while (quote_buf->status == GET_QUOTE_IN_FLIGHT && i++ < timeout) {
155 if (msleep_interruptible(MSEC_PER_SEC))
159 return (i == timeout) ? -ETIMEDOUT : 0;
162 static int tdx_report_new(struct tsm_report *report, void *data)
164 u8 *buf, *reportdata = NULL, *tdreport = NULL;
165 struct tdx_quote_buf *quote_buf = quote_data;
166 struct tsm_desc *desc = &report->desc;
170 /* TODO: switch to guard(mutex_intr) */
171 if (mutex_lock_interruptible("e_lock))
175 * If the previous request is timedout or interrupted, and the
176 * Quote buf status is still in GET_QUOTE_IN_FLIGHT (owned by
177 * VMM), don't permit any new request.
179 if (quote_buf->status == GET_QUOTE_IN_FLIGHT) {
184 if (desc->inblob_len != TDX_REPORTDATA_LEN) {
189 reportdata = kmalloc(TDX_REPORTDATA_LEN, GFP_KERNEL);
195 tdreport = kzalloc(TDX_REPORT_LEN, GFP_KERNEL);
201 memcpy(reportdata, desc->inblob, desc->inblob_len);
203 /* Generate TDREPORT0 using "TDG.MR.REPORT" TDCALL */
204 ret = tdx_mcall_get_report0(reportdata, tdreport);
206 pr_err("GetReport call failed\n");
210 memset(quote_data, 0, GET_QUOTE_BUF_SIZE);
212 /* Update Quote buffer header */
213 quote_buf->version = GET_QUOTE_CMD_VER;
214 quote_buf->in_len = TDX_REPORT_LEN;
216 memcpy(quote_buf->data, tdreport, TDX_REPORT_LEN);
218 err = tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE);
220 pr_err("GetQuote hypercall failed, status:%llx\n", err);
225 ret = wait_for_quote_completion(quote_buf, getquote_timeout);
227 pr_err("GetQuote request timedout\n");
231 buf = kvmemdup(quote_buf->data, quote_buf->out_len, GFP_KERNEL);
237 report->outblob = buf;
238 report->outblob_len = quote_buf->out_len;
241 * TODO: parse the PEM-formatted cert chain out of the quote buffer when
245 mutex_unlock("e_lock);
252 static long tdx_guest_ioctl(struct file *file, unsigned int cmd,
256 case TDX_CMD_GET_REPORT0:
257 return tdx_get_report0((struct tdx_report_req __user *)arg);
263 static const struct file_operations tdx_guest_fops = {
264 .owner = THIS_MODULE,
265 .unlocked_ioctl = tdx_guest_ioctl,
269 static struct miscdevice tdx_misc_dev = {
270 .name = KBUILD_MODNAME,
271 .minor = MISC_DYNAMIC_MINOR,
272 .fops = &tdx_guest_fops,
275 static const struct x86_cpu_id tdx_guest_ids[] = {
276 X86_MATCH_FEATURE(X86_FEATURE_TDX_GUEST, NULL),
279 MODULE_DEVICE_TABLE(x86cpu, tdx_guest_ids);
281 static const struct tsm_ops tdx_tsm_ops = {
282 .name = KBUILD_MODNAME,
283 .report_new = tdx_report_new,
286 static int __init tdx_guest_init(void)
290 if (!x86_match_cpu(tdx_guest_ids))
293 ret = misc_register(&tdx_misc_dev);
297 quote_data = alloc_quote_buf();
299 pr_err("Failed to allocate Quote buffer\n");
304 ret = tsm_register(&tdx_tsm_ops, NULL, NULL);
311 free_quote_buf(quote_data);
313 misc_deregister(&tdx_misc_dev);
317 module_init(tdx_guest_init);
319 static void __exit tdx_guest_exit(void)
321 tsm_unregister(&tdx_tsm_ops);
322 free_quote_buf(quote_data);
323 misc_deregister(&tdx_misc_dev);
325 module_exit(tdx_guest_exit);
327 MODULE_AUTHOR("Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>");
328 MODULE_DESCRIPTION("TDX Guest Driver");
329 MODULE_LICENSE("GPL");