159bd54a8SKuppuswamy Sathyanarayanan // SPDX-License-Identifier: GPL-2.0
259bd54a8SKuppuswamy Sathyanarayanan /* Copyright (C) 2021-2022 Intel Corporation */
359bd54a8SKuppuswamy Sathyanarayanan
459bd54a8SKuppuswamy Sathyanarayanan #undef pr_fmt
559bd54a8SKuppuswamy Sathyanarayanan #define pr_fmt(fmt) "tdx: " fmt
659bd54a8SKuppuswamy Sathyanarayanan
759bd54a8SKuppuswamy Sathyanarayanan #include <linux/cpufeature.h>
851acfe89SKuppuswamy Sathyanarayanan #include <linux/export.h>
951acfe89SKuppuswamy Sathyanarayanan #include <linux/io.h>
10859e63b7SKirill A. Shutemov #include <linux/kexec.h>
1141394e33SKirill A. Shutemov #include <asm/coco.h>
1259bd54a8SKuppuswamy Sathyanarayanan #include <asm/tdx.h>
13bfe6ed0cSKirill A. Shutemov #include <asm/vmx.h>
14b82a8dbdSKirill A. Shutemov #include <asm/ia32.h>
1531d58c4eSKirill A. Shutemov #include <asm/insn.h>
1631d58c4eSKirill A. Shutemov #include <asm/insn-eval.h>
179f98a4f4SVishal Annapurve #include <asm/paravirt_types.h>
187dbde763SKirill A. Shutemov #include <asm/pgtable.h>
19859e63b7SKirill A. Shutemov #include <asm/set_memory.h>
20d4fc4d01SAlexey Gladkov (Intel) #include <asm/traps.h>
2159bd54a8SKuppuswamy Sathyanarayanan
2231d58c4eSKirill A. Shutemov /* MMIO direction */
2331d58c4eSKirill A. Shutemov #define EPT_READ 0
2431d58c4eSKirill A. Shutemov #define EPT_WRITE 1
2531d58c4eSKirill A. Shutemov
2603149948SKuppuswamy Sathyanarayanan /* Port I/O direction */
2703149948SKuppuswamy Sathyanarayanan #define PORT_READ 0
2803149948SKuppuswamy Sathyanarayanan #define PORT_WRITE 1
2903149948SKuppuswamy Sathyanarayanan
3003149948SKuppuswamy Sathyanarayanan /* See Exit Qualification for I/O Instructions in VMX documentation */
3103149948SKuppuswamy Sathyanarayanan #define VE_IS_IO_IN(e) ((e) & BIT(3))
3203149948SKuppuswamy Sathyanarayanan #define VE_GET_IO_SIZE(e) (((e) & GENMASK(2, 0)) + 1)
3303149948SKuppuswamy Sathyanarayanan #define VE_GET_PORT_NUM(e) ((e) >> 16)
3403149948SKuppuswamy Sathyanarayanan #define VE_IS_IO_STRING(e) ((e) & BIT(4))
3503149948SKuppuswamy Sathyanarayanan
3651acfe89SKuppuswamy Sathyanarayanan /* TDX Module call error codes */
3751acfe89SKuppuswamy Sathyanarayanan #define TDCALL_RETURN_CODE(a) ((a) >> 32)
3851acfe89SKuppuswamy Sathyanarayanan #define TDCALL_INVALID_OPERAND 0xc0000100
393f88ca96SCedric Xing #define TDCALL_OPERAND_BUSY 0x80000200
4051acfe89SKuppuswamy Sathyanarayanan
4151acfe89SKuppuswamy Sathyanarayanan #define TDREPORT_SUBTYPE_0 0
4251acfe89SKuppuswamy Sathyanarayanan
43c3abbf13SKirill A. Shutemov static atomic_long_t nr_shared;
44c3abbf13SKirill A. Shutemov
45eb94f1b6SKuppuswamy Sathyanarayanan /* Called from __tdx_hypercall() for unrecoverable failure */
__tdx_hypercall_failed(void)46c641cfb5SKai Huang noinstr void __noreturn __tdx_hypercall_failed(void)
47eb94f1b6SKuppuswamy Sathyanarayanan {
48c3982c1aSPeter Zijlstra instrumentation_begin();
49eb94f1b6SKuppuswamy Sathyanarayanan panic("TDVMCALL failed. TDX module bug?");
50eb94f1b6SKuppuswamy Sathyanarayanan }
51eb94f1b6SKuppuswamy Sathyanarayanan
52cfb8ec7aSKuppuswamy Sathyanarayanan #ifdef CONFIG_KVM_GUEST
tdx_kvm_hypercall(unsigned int nr,unsigned long p1,unsigned long p2,unsigned long p3,unsigned long p4)53cfb8ec7aSKuppuswamy Sathyanarayanan long tdx_kvm_hypercall(unsigned int nr, unsigned long p1, unsigned long p2,
54cfb8ec7aSKuppuswamy Sathyanarayanan unsigned long p3, unsigned long p4)
55cfb8ec7aSKuppuswamy Sathyanarayanan {
568a8544bdSKai Huang struct tdx_module_args args = {
57cfb8ec7aSKuppuswamy Sathyanarayanan .r10 = nr,
58cfb8ec7aSKuppuswamy Sathyanarayanan .r11 = p1,
59cfb8ec7aSKuppuswamy Sathyanarayanan .r12 = p2,
60cfb8ec7aSKuppuswamy Sathyanarayanan .r13 = p3,
61cfb8ec7aSKuppuswamy Sathyanarayanan .r14 = p4,
62cfb8ec7aSKuppuswamy Sathyanarayanan };
63cfb8ec7aSKuppuswamy Sathyanarayanan
647a3a4018SKirill A. Shutemov return __tdx_hypercall(&args);
65cfb8ec7aSKuppuswamy Sathyanarayanan }
66cfb8ec7aSKuppuswamy Sathyanarayanan EXPORT_SYMBOL_GPL(tdx_kvm_hypercall);
67cfb8ec7aSKuppuswamy Sathyanarayanan #endif
68cfb8ec7aSKuppuswamy Sathyanarayanan
69bfe6ed0cSKirill A. Shutemov /*
7041394e33SKirill A. Shutemov * Used for TDX guests to make calls directly to the TD module. This
7141394e33SKirill A. Shutemov * should only be used for calls that have no legitimate reason to fail
7241394e33SKirill A. Shutemov * or where the kernel can not survive the call failing.
7341394e33SKirill A. Shutemov */
tdcall(u64 fn,struct tdx_module_args * args)7457a420bbSKai Huang static inline void tdcall(u64 fn, struct tdx_module_args *args)
7541394e33SKirill A. Shutemov {
7657a420bbSKai Huang if (__tdcall_ret(fn, args))
7741394e33SKirill A. Shutemov panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
7841394e33SKirill A. Shutemov }
7941394e33SKirill A. Shutemov
805081e8faSKirill A. Shutemov /* Read TD-scoped metadata */
tdg_vm_rd(u64 field,u64 * value)81f65aa0adSKirill A. Shutemov static inline u64 tdg_vm_rd(u64 field, u64 *value)
825081e8faSKirill A. Shutemov {
835081e8faSKirill A. Shutemov struct tdx_module_args args = {
845081e8faSKirill A. Shutemov .rdx = field,
855081e8faSKirill A. Shutemov };
865081e8faSKirill A. Shutemov u64 ret;
875081e8faSKirill A. Shutemov
885081e8faSKirill A. Shutemov ret = __tdcall_ret(TDG_VM_RD, &args);
895081e8faSKirill A. Shutemov *value = args.r8;
905081e8faSKirill A. Shutemov
915081e8faSKirill A. Shutemov return ret;
925081e8faSKirill A. Shutemov }
935081e8faSKirill A. Shutemov
945081e8faSKirill A. Shutemov /* Write TD-scoped metadata */
tdg_vm_wr(u64 field,u64 value,u64 mask)955081e8faSKirill A. Shutemov static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)
965081e8faSKirill A. Shutemov {
975081e8faSKirill A. Shutemov struct tdx_module_args args = {
985081e8faSKirill A. Shutemov .rdx = field,
995081e8faSKirill A. Shutemov .r8 = value,
1005081e8faSKirill A. Shutemov .r9 = mask,
1015081e8faSKirill A. Shutemov };
1025081e8faSKirill A. Shutemov
1035081e8faSKirill A. Shutemov return __tdcall(TDG_VM_WR, &args);
1045081e8faSKirill A. Shutemov }
1055081e8faSKirill A. Shutemov
10651acfe89SKuppuswamy Sathyanarayanan /**
10751acfe89SKuppuswamy Sathyanarayanan * tdx_mcall_get_report0() - Wrapper to get TDREPORT0 (a.k.a. TDREPORT
10851acfe89SKuppuswamy Sathyanarayanan * subtype 0) using TDG.MR.REPORT TDCALL.
10951acfe89SKuppuswamy Sathyanarayanan * @reportdata: Address of the input buffer which contains user-defined
11051acfe89SKuppuswamy Sathyanarayanan * REPORTDATA to be included into TDREPORT.
11151acfe89SKuppuswamy Sathyanarayanan * @tdreport: Address of the output buffer to store TDREPORT.
11251acfe89SKuppuswamy Sathyanarayanan *
113*2748566dSCedric Xing * Refer to section titled "TDG.MR.REPORT leaf" in the TDX Module v1.0
114*2748566dSCedric Xing * specification for more information on TDG.MR.REPORT TDCALL.
115*2748566dSCedric Xing *
11651acfe89SKuppuswamy Sathyanarayanan * It is used in the TDX guest driver module to get the TDREPORT0.
11751acfe89SKuppuswamy Sathyanarayanan *
118*2748566dSCedric Xing * Return 0 on success, -ENXIO for invalid operands, -EBUSY for busy operation,
119*2748566dSCedric Xing * or -EIO on other TDCALL failures.
12051acfe89SKuppuswamy Sathyanarayanan */
tdx_mcall_get_report0(u8 * reportdata,u8 * tdreport)12151acfe89SKuppuswamy Sathyanarayanan int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
12251acfe89SKuppuswamy Sathyanarayanan {
12357a420bbSKai Huang struct tdx_module_args args = {
12457a420bbSKai Huang .rcx = virt_to_phys(tdreport),
12557a420bbSKai Huang .rdx = virt_to_phys(reportdata),
12657a420bbSKai Huang .r8 = TDREPORT_SUBTYPE_0,
12757a420bbSKai Huang };
12851acfe89SKuppuswamy Sathyanarayanan u64 ret;
12951acfe89SKuppuswamy Sathyanarayanan
13057a420bbSKai Huang ret = __tdcall(TDG_MR_REPORT, &args);
13151acfe89SKuppuswamy Sathyanarayanan if (ret) {
13251acfe89SKuppuswamy Sathyanarayanan if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
133*2748566dSCedric Xing return -ENXIO;
134*2748566dSCedric Xing else if (TDCALL_RETURN_CODE(ret) == TDCALL_OPERAND_BUSY)
135*2748566dSCedric Xing return -EBUSY;
13651acfe89SKuppuswamy Sathyanarayanan return -EIO;
13751acfe89SKuppuswamy Sathyanarayanan }
13851acfe89SKuppuswamy Sathyanarayanan
13951acfe89SKuppuswamy Sathyanarayanan return 0;
14051acfe89SKuppuswamy Sathyanarayanan }
14151acfe89SKuppuswamy Sathyanarayanan EXPORT_SYMBOL_GPL(tdx_mcall_get_report0);
14251acfe89SKuppuswamy Sathyanarayanan
143f4738f56SKuppuswamy Sathyanarayanan /**
1443f88ca96SCedric Xing * tdx_mcall_extend_rtmr() - Wrapper to extend RTMR registers using
1453f88ca96SCedric Xing * TDG.MR.RTMR.EXTEND TDCALL.
1463f88ca96SCedric Xing * @index: Index of RTMR register to be extended.
1473f88ca96SCedric Xing * @data: Address of the input buffer with RTMR register extend data.
1483f88ca96SCedric Xing *
1493f88ca96SCedric Xing * Refer to section titled "TDG.MR.RTMR.EXTEND leaf" in the TDX Module v1.0
1503f88ca96SCedric Xing * specification for more information on TDG.MR.RTMR.EXTEND TDCALL.
1513f88ca96SCedric Xing *
1523f88ca96SCedric Xing * It is used in the TDX guest driver module to allow user to extend the RTMR
1533f88ca96SCedric Xing * registers.
1543f88ca96SCedric Xing *
1553f88ca96SCedric Xing * Return 0 on success, -ENXIO for invalid operands, -EBUSY for busy operation,
1563f88ca96SCedric Xing * or -EIO on other TDCALL failures.
1573f88ca96SCedric Xing */
tdx_mcall_extend_rtmr(u8 index,u8 * data)1583f88ca96SCedric Xing int tdx_mcall_extend_rtmr(u8 index, u8 *data)
1593f88ca96SCedric Xing {
1603f88ca96SCedric Xing struct tdx_module_args args = {
1613f88ca96SCedric Xing .rcx = virt_to_phys(data),
1623f88ca96SCedric Xing .rdx = index,
1633f88ca96SCedric Xing };
1643f88ca96SCedric Xing u64 ret;
1653f88ca96SCedric Xing
1663f88ca96SCedric Xing ret = __tdcall(TDG_MR_RTMR_EXTEND, &args);
1673f88ca96SCedric Xing if (ret) {
1683f88ca96SCedric Xing if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
1693f88ca96SCedric Xing return -ENXIO;
1703f88ca96SCedric Xing if (TDCALL_RETURN_CODE(ret) == TDCALL_OPERAND_BUSY)
1713f88ca96SCedric Xing return -EBUSY;
1723f88ca96SCedric Xing return -EIO;
1733f88ca96SCedric Xing }
1743f88ca96SCedric Xing
1753f88ca96SCedric Xing return 0;
1763f88ca96SCedric Xing }
1773f88ca96SCedric Xing EXPORT_SYMBOL_GPL(tdx_mcall_extend_rtmr);
1783f88ca96SCedric Xing
1793f88ca96SCedric Xing /**
180f4738f56SKuppuswamy Sathyanarayanan * tdx_hcall_get_quote() - Wrapper to request TD Quote using GetQuote
181f4738f56SKuppuswamy Sathyanarayanan * hypercall.
182f4738f56SKuppuswamy Sathyanarayanan * @buf: Address of the directly mapped shared kernel buffer which
183f4738f56SKuppuswamy Sathyanarayanan * contains TDREPORT. The same buffer will be used by VMM to
184f4738f56SKuppuswamy Sathyanarayanan * store the generated TD Quote output.
185f4738f56SKuppuswamy Sathyanarayanan * @size: size of the tdquote buffer (4KB-aligned).
186f4738f56SKuppuswamy Sathyanarayanan *
187f4738f56SKuppuswamy Sathyanarayanan * Refer to section titled "TDG.VP.VMCALL<GetQuote>" in the TDX GHCI
188f4738f56SKuppuswamy Sathyanarayanan * v1.0 specification for more information on GetQuote hypercall.
189f4738f56SKuppuswamy Sathyanarayanan * It is used in the TDX guest driver module to get the TD Quote.
190f4738f56SKuppuswamy Sathyanarayanan *
191f4738f56SKuppuswamy Sathyanarayanan * Return 0 on success or error code on failure.
192f4738f56SKuppuswamy Sathyanarayanan */
tdx_hcall_get_quote(u8 * buf,size_t size)193f4738f56SKuppuswamy Sathyanarayanan u64 tdx_hcall_get_quote(u8 *buf, size_t size)
194f4738f56SKuppuswamy Sathyanarayanan {
195f4738f56SKuppuswamy Sathyanarayanan /* Since buf is a shared memory, set the shared (decrypted) bits */
196f4738f56SKuppuswamy Sathyanarayanan return _tdx_hypercall(TDVMCALL_GET_QUOTE, cc_mkdec(virt_to_phys(buf)), size, 0, 0);
197f4738f56SKuppuswamy Sathyanarayanan }
198f4738f56SKuppuswamy Sathyanarayanan EXPORT_SYMBOL_GPL(tdx_hcall_get_quote);
199f4738f56SKuppuswamy Sathyanarayanan
tdx_panic(const char * msg)20071acdcd7SKirill A. Shutemov static void __noreturn tdx_panic(const char *msg)
20171acdcd7SKirill A. Shutemov {
2028a8544bdSKai Huang struct tdx_module_args args = {
20371acdcd7SKirill A. Shutemov .r10 = TDX_HYPERCALL_STANDARD,
20471acdcd7SKirill A. Shutemov .r11 = TDVMCALL_REPORT_FATAL_ERROR,
20571acdcd7SKirill A. Shutemov .r12 = 0, /* Error code: 0 is Panic */
20671acdcd7SKirill A. Shutemov };
20771acdcd7SKirill A. Shutemov union {
20871acdcd7SKirill A. Shutemov /* Define register order according to the GHCI */
20971acdcd7SKirill A. Shutemov struct { u64 r14, r15, rbx, rdi, rsi, r8, r9, rdx; };
21071acdcd7SKirill A. Shutemov
211c0e1d465SKees Cook char bytes[64] __nonstring;
21271acdcd7SKirill A. Shutemov } message;
21371acdcd7SKirill A. Shutemov
21471acdcd7SKirill A. Shutemov /* VMM assumes '\0' in byte 65, if the message took all 64 bytes */
215c0e1d465SKees Cook strtomem_pad(message.bytes, msg, '\0');
21671acdcd7SKirill A. Shutemov
21771acdcd7SKirill A. Shutemov args.r8 = message.r8;
21871acdcd7SKirill A. Shutemov args.r9 = message.r9;
21971acdcd7SKirill A. Shutemov args.r14 = message.r14;
22071acdcd7SKirill A. Shutemov args.r15 = message.r15;
22171acdcd7SKirill A. Shutemov args.rdi = message.rdi;
22271acdcd7SKirill A. Shutemov args.rsi = message.rsi;
22371acdcd7SKirill A. Shutemov args.rbx = message.rbx;
22471acdcd7SKirill A. Shutemov args.rdx = message.rdx;
22571acdcd7SKirill A. Shutemov
22671acdcd7SKirill A. Shutemov /*
22771acdcd7SKirill A. Shutemov * This hypercall should never return and it is not safe
22871acdcd7SKirill A. Shutemov * to keep the guest running. Call it forever if it
22971acdcd7SKirill A. Shutemov * happens to return.
23071acdcd7SKirill A. Shutemov */
23171acdcd7SKirill A. Shutemov while (1)
2327a3a4018SKirill A. Shutemov __tdx_hypercall(&args);
23371acdcd7SKirill A. Shutemov }
23471acdcd7SKirill A. Shutemov
235f65aa0adSKirill A. Shutemov /*
236f65aa0adSKirill A. Shutemov * The kernel cannot handle #VEs when accessing normal kernel memory. Ensure
237f65aa0adSKirill A. Shutemov * that no #VE will be delivered for accesses to TD-private memory.
238f65aa0adSKirill A. Shutemov *
239f65aa0adSKirill A. Shutemov * TDX 1.0 does not allow the guest to disable SEPT #VE on its own. The VMM
240f65aa0adSKirill A. Shutemov * controls if the guest will receive such #VE with TD attribute
241564ea84cSKirill A. Shutemov * TDX_ATTR_SEPT_VE_DISABLE.
242f65aa0adSKirill A. Shutemov *
243f65aa0adSKirill A. Shutemov * Newer TDX modules allow the guest to control if it wants to receive SEPT
244f65aa0adSKirill A. Shutemov * violation #VEs.
245f65aa0adSKirill A. Shutemov *
246f65aa0adSKirill A. Shutemov * Check if the feature is available and disable SEPT #VE if possible.
247f65aa0adSKirill A. Shutemov *
248564ea84cSKirill A. Shutemov * If the TD is allowed to disable/enable SEPT #VEs, the TDX_ATTR_SEPT_VE_DISABLE
249f65aa0adSKirill A. Shutemov * attribute is no longer reliable. It reflects the initial state of the
250f65aa0adSKirill A. Shutemov * control for the TD, but it will not be updated if someone (e.g. bootloader)
251f65aa0adSKirill A. Shutemov * changes it before the kernel starts. Kernel must check TDCS_TD_CTLS bit to
252f65aa0adSKirill A. Shutemov * determine if SEPT #VEs are enabled or disabled.
253f65aa0adSKirill A. Shutemov */
disable_sept_ve(u64 td_attr)254f65aa0adSKirill A. Shutemov static void disable_sept_ve(u64 td_attr)
255f65aa0adSKirill A. Shutemov {
256f65aa0adSKirill A. Shutemov const char *msg = "TD misconfiguration: SEPT #VE has to be disabled";
257564ea84cSKirill A. Shutemov bool debug = td_attr & TDX_ATTR_DEBUG;
258f65aa0adSKirill A. Shutemov u64 config, controls;
259f65aa0adSKirill A. Shutemov
260f65aa0adSKirill A. Shutemov /* Is this TD allowed to disable SEPT #VE */
261f65aa0adSKirill A. Shutemov tdg_vm_rd(TDCS_CONFIG_FLAGS, &config);
262f65aa0adSKirill A. Shutemov if (!(config & TDCS_CONFIG_FLEXIBLE_PENDING_VE)) {
263f65aa0adSKirill A. Shutemov /* No SEPT #VE controls for the guest: check the attribute */
264564ea84cSKirill A. Shutemov if (td_attr & TDX_ATTR_SEPT_VE_DISABLE)
265f65aa0adSKirill A. Shutemov return;
266f65aa0adSKirill A. Shutemov
267f65aa0adSKirill A. Shutemov /* Relax SEPT_VE_DISABLE check for debug TD for backtraces */
268f65aa0adSKirill A. Shutemov if (debug)
269f65aa0adSKirill A. Shutemov pr_warn("%s\n", msg);
270f65aa0adSKirill A. Shutemov else
271f65aa0adSKirill A. Shutemov tdx_panic(msg);
272f65aa0adSKirill A. Shutemov return;
273f65aa0adSKirill A. Shutemov }
274f65aa0adSKirill A. Shutemov
275f65aa0adSKirill A. Shutemov /* Check if SEPT #VE has been disabled before us */
276f65aa0adSKirill A. Shutemov tdg_vm_rd(TDCS_TD_CTLS, &controls);
277f65aa0adSKirill A. Shutemov if (controls & TD_CTLS_PENDING_VE_DISABLE)
278f65aa0adSKirill A. Shutemov return;
279f65aa0adSKirill A. Shutemov
280f65aa0adSKirill A. Shutemov /* Keep #VEs enabled for splats in debugging environments */
281f65aa0adSKirill A. Shutemov if (debug)
282f65aa0adSKirill A. Shutemov return;
283f65aa0adSKirill A. Shutemov
284f65aa0adSKirill A. Shutemov /* Disable SEPT #VEs */
285f65aa0adSKirill A. Shutemov tdg_vm_wr(TDCS_TD_CTLS, TD_CTLS_PENDING_VE_DISABLE,
286f65aa0adSKirill A. Shutemov TD_CTLS_PENDING_VE_DISABLE);
287f65aa0adSKirill A. Shutemov }
288f65aa0adSKirill A. Shutemov
2897ae15e2fSKirill A. Shutemov /*
2907ae15e2fSKirill A. Shutemov * TDX 1.0 generates a #VE when accessing topology-related CPUID leafs (0xB and
2917ae15e2fSKirill A. Shutemov * 0x1F) and the X2APIC_APICID MSR. The kernel returns all zeros on CPUID #VEs.
2927ae15e2fSKirill A. Shutemov * In practice, this means that the kernel can only boot with a plain topology.
2937ae15e2fSKirill A. Shutemov * Any complications will cause problems.
2947ae15e2fSKirill A. Shutemov *
2957ae15e2fSKirill A. Shutemov * The ENUM_TOPOLOGY feature allows the VMM to provide topology information.
2967ae15e2fSKirill A. Shutemov * Enabling the feature eliminates topology-related #VEs: the TDX module
2977ae15e2fSKirill A. Shutemov * virtualizes accesses to the CPUID leafs and the MSR.
2987ae15e2fSKirill A. Shutemov *
2997ae15e2fSKirill A. Shutemov * Enable ENUM_TOPOLOGY if it is available.
3007ae15e2fSKirill A. Shutemov */
enable_cpu_topology_enumeration(void)3017ae15e2fSKirill A. Shutemov static void enable_cpu_topology_enumeration(void)
3027ae15e2fSKirill A. Shutemov {
3037ae15e2fSKirill A. Shutemov u64 configured;
3047ae15e2fSKirill A. Shutemov
3057ae15e2fSKirill A. Shutemov /* Has the VMM provided a valid topology configuration? */
3067ae15e2fSKirill A. Shutemov tdg_vm_rd(TDCS_TOPOLOGY_ENUM_CONFIGURED, &configured);
3077ae15e2fSKirill A. Shutemov if (!configured) {
3087ae15e2fSKirill A. Shutemov pr_err("VMM did not configure X2APIC_IDs properly\n");
3097ae15e2fSKirill A. Shutemov return;
3107ae15e2fSKirill A. Shutemov }
3117ae15e2fSKirill A. Shutemov
3127ae15e2fSKirill A. Shutemov tdg_vm_wr(TDCS_TD_CTLS, TD_CTLS_ENUM_TOPOLOGY, TD_CTLS_ENUM_TOPOLOGY);
3137ae15e2fSKirill A. Shutemov }
3147ae15e2fSKirill A. Shutemov
reduce_unnecessary_ve(void)315cd9ce821SKirill A. Shutemov static void reduce_unnecessary_ve(void)
316cd9ce821SKirill A. Shutemov {
317cd9ce821SKirill A. Shutemov u64 err = tdg_vm_wr(TDCS_TD_CTLS, TD_CTLS_REDUCE_VE, TD_CTLS_REDUCE_VE);
318cd9ce821SKirill A. Shutemov
319cd9ce821SKirill A. Shutemov if (err == TDX_SUCCESS)
320cd9ce821SKirill A. Shutemov return;
321cd9ce821SKirill A. Shutemov
322cd9ce821SKirill A. Shutemov /*
323cd9ce821SKirill A. Shutemov * Enabling REDUCE_VE includes ENUM_TOPOLOGY. Only try to
324cd9ce821SKirill A. Shutemov * enable ENUM_TOPOLOGY if REDUCE_VE was not successful.
325cd9ce821SKirill A. Shutemov */
326cd9ce821SKirill A. Shutemov enable_cpu_topology_enumeration();
327cd9ce821SKirill A. Shutemov }
328cd9ce821SKirill A. Shutemov
tdx_setup(u64 * cc_mask)329b064043dSKirill A. Shutemov static void tdx_setup(u64 *cc_mask)
33041394e33SKirill A. Shutemov {
33157a420bbSKai Huang struct tdx_module_args args = {};
33241394e33SKirill A. Shutemov unsigned int gpa_width;
333373e715eSKirill A. Shutemov u64 td_attr;
33441394e33SKirill A. Shutemov
33541394e33SKirill A. Shutemov /*
33641394e33SKirill A. Shutemov * TDINFO TDX module call is used to get the TD execution environment
33741394e33SKirill A. Shutemov * information like GPA width, number of available vcpus, debug mode
33841394e33SKirill A. Shutemov * information, etc. More details about the ABI can be found in TDX
33941394e33SKirill A. Shutemov * Guest-Host-Communication Interface (GHCI), section 2.4.2 TDCALL
34041394e33SKirill A. Shutemov * [TDG.VP.INFO].
34141394e33SKirill A. Shutemov */
34257a420bbSKai Huang tdcall(TDG_VP_INFO, &args);
34341394e33SKirill A. Shutemov
34441394e33SKirill A. Shutemov /*
34541394e33SKirill A. Shutemov * The highest bit of a guest physical address is the "sharing" bit.
34641394e33SKirill A. Shutemov * Set it for shared pages and clear it for private pages.
347373e715eSKirill A. Shutemov *
348373e715eSKirill A. Shutemov * The GPA width that comes out of this call is critical. TDX guests
349373e715eSKirill A. Shutemov * can not meaningfully run without it.
35041394e33SKirill A. Shutemov */
35157a420bbSKai Huang gpa_width = args.rcx & GENMASK(5, 0);
352a6dd6f39SDave Hansen *cc_mask = BIT_ULL(gpa_width - 1);
353373e715eSKirill A. Shutemov
354f65aa0adSKirill A. Shutemov td_attr = args.rdx;
355f65aa0adSKirill A. Shutemov
356b064043dSKirill A. Shutemov /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
357b064043dSKirill A. Shutemov tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL);
358b064043dSKirill A. Shutemov
359f65aa0adSKirill A. Shutemov disable_sept_ve(td_attr);
360cd9ce821SKirill A. Shutemov
361cd9ce821SKirill A. Shutemov reduce_unnecessary_ve();
36241394e33SKirill A. Shutemov }
36341394e33SKirill A. Shutemov
364cdd85786SKirill A. Shutemov /*
365cdd85786SKirill A. Shutemov * The TDX module spec states that #VE may be injected for a limited set of
366cdd85786SKirill A. Shutemov * reasons:
367cdd85786SKirill A. Shutemov *
368cdd85786SKirill A. Shutemov * - Emulation of the architectural #VE injection on EPT violation;
369cdd85786SKirill A. Shutemov *
370cdd85786SKirill A. Shutemov * - As a result of guest TD execution of a disallowed instruction,
371cdd85786SKirill A. Shutemov * a disallowed MSR access, or CPUID virtualization;
372cdd85786SKirill A. Shutemov *
373cdd85786SKirill A. Shutemov * - A notification to the guest TD about anomalous behavior;
374cdd85786SKirill A. Shutemov *
375cdd85786SKirill A. Shutemov * The last one is opt-in and is not used by the kernel.
376cdd85786SKirill A. Shutemov *
377cdd85786SKirill A. Shutemov * The Intel Software Developer's Manual describes cases when instruction
378cdd85786SKirill A. Shutemov * length field can be used in section "Information for VM Exits Due to
379cdd85786SKirill A. Shutemov * Instruction Execution".
380cdd85786SKirill A. Shutemov *
381cdd85786SKirill A. Shutemov * For TDX, it ultimately means GET_VEINFO provides reliable instruction length
382cdd85786SKirill A. Shutemov * information if #VE occurred due to instruction execution, but not for EPT
383cdd85786SKirill A. Shutemov * violations.
384cdd85786SKirill A. Shutemov */
ve_instr_len(struct ve_info * ve)385cdd85786SKirill A. Shutemov static int ve_instr_len(struct ve_info *ve)
386cdd85786SKirill A. Shutemov {
387cdd85786SKirill A. Shutemov switch (ve->exit_reason) {
388cdd85786SKirill A. Shutemov case EXIT_REASON_HLT:
389cdd85786SKirill A. Shutemov case EXIT_REASON_MSR_READ:
390cdd85786SKirill A. Shutemov case EXIT_REASON_MSR_WRITE:
391cdd85786SKirill A. Shutemov case EXIT_REASON_CPUID:
392cdd85786SKirill A. Shutemov case EXIT_REASON_IO_INSTRUCTION:
393cdd85786SKirill A. Shutemov /* It is safe to use ve->instr_len for #VE due instructions */
394cdd85786SKirill A. Shutemov return ve->instr_len;
395cdd85786SKirill A. Shutemov case EXIT_REASON_EPT_VIOLATION:
396cdd85786SKirill A. Shutemov /*
397cdd85786SKirill A. Shutemov * For EPT violations, ve->insn_len is not defined. For those,
398cdd85786SKirill A. Shutemov * the kernel must decode instructions manually and should not
399cdd85786SKirill A. Shutemov * be using this function.
400cdd85786SKirill A. Shutemov */
401cdd85786SKirill A. Shutemov WARN_ONCE(1, "ve->instr_len is not defined for EPT violations");
402cdd85786SKirill A. Shutemov return 0;
403cdd85786SKirill A. Shutemov default:
404cdd85786SKirill A. Shutemov WARN_ONCE(1, "Unexpected #VE-type: %lld\n", ve->exit_reason);
405cdd85786SKirill A. Shutemov return ve->instr_len;
406cdd85786SKirill A. Shutemov }
407cdd85786SKirill A. Shutemov }
408cdd85786SKirill A. Shutemov
__halt(const bool irq_disabled)409e80a48baSPeter Zijlstra static u64 __cpuidle __halt(const bool irq_disabled)
410bfe6ed0cSKirill A. Shutemov {
4118a8544bdSKai Huang struct tdx_module_args args = {
412bfe6ed0cSKirill A. Shutemov .r10 = TDX_HYPERCALL_STANDARD,
413bfe6ed0cSKirill A. Shutemov .r11 = hcall_func(EXIT_REASON_HLT),
414bfe6ed0cSKirill A. Shutemov .r12 = irq_disabled,
415bfe6ed0cSKirill A. Shutemov };
416bfe6ed0cSKirill A. Shutemov
417bfe6ed0cSKirill A. Shutemov /*
418bfe6ed0cSKirill A. Shutemov * Emulate HLT operation via hypercall. More info about ABI
419bfe6ed0cSKirill A. Shutemov * can be found in TDX Guest-Host-Communication Interface
420bfe6ed0cSKirill A. Shutemov * (GHCI), section 3.8 TDG.VP.VMCALL<Instruction.HLT>.
421bfe6ed0cSKirill A. Shutemov *
422bfe6ed0cSKirill A. Shutemov * The VMM uses the "IRQ disabled" param to understand IRQ
423bfe6ed0cSKirill A. Shutemov * enabled status (RFLAGS.IF) of the TD guest and to determine
424bfe6ed0cSKirill A. Shutemov * whether or not it should schedule the halted vCPU if an
425bfe6ed0cSKirill A. Shutemov * IRQ becomes pending. E.g. if IRQs are disabled, the VMM
426bfe6ed0cSKirill A. Shutemov * can keep the vCPU in virtual HLT, even if an IRQ is
427bfe6ed0cSKirill A. Shutemov * pending, without hanging/breaking the guest.
428bfe6ed0cSKirill A. Shutemov */
4297a3a4018SKirill A. Shutemov return __tdx_hypercall(&args);
430bfe6ed0cSKirill A. Shutemov }
431bfe6ed0cSKirill A. Shutemov
handle_halt(struct ve_info * ve)432cdd85786SKirill A. Shutemov static int handle_halt(struct ve_info *ve)
433bfe6ed0cSKirill A. Shutemov {
434bfe6ed0cSKirill A. Shutemov const bool irq_disabled = irqs_disabled();
435bfe6ed0cSKirill A. Shutemov
436e8f45927SVishal Annapurve /*
437e8f45927SVishal Annapurve * HLT with IRQs enabled is unsafe, as an IRQ that is intended to be a
438e8f45927SVishal Annapurve * wake event may be consumed before requesting HLT emulation, leaving
439e8f45927SVishal Annapurve * the vCPU blocking indefinitely.
440e8f45927SVishal Annapurve */
441e8f45927SVishal Annapurve if (WARN_ONCE(!irq_disabled, "HLT emulation with IRQs enabled"))
442e8f45927SVishal Annapurve return -EIO;
443e8f45927SVishal Annapurve
444e80a48baSPeter Zijlstra if (__halt(irq_disabled))
445cdd85786SKirill A. Shutemov return -EIO;
446bfe6ed0cSKirill A. Shutemov
447cdd85786SKirill A. Shutemov return ve_instr_len(ve);
448bfe6ed0cSKirill A. Shutemov }
449bfe6ed0cSKirill A. Shutemov
tdx_halt(void)4509f98a4f4SVishal Annapurve void __cpuidle tdx_halt(void)
451bfe6ed0cSKirill A. Shutemov {
452bfe6ed0cSKirill A. Shutemov const bool irq_disabled = false;
453bfe6ed0cSKirill A. Shutemov
454bfe6ed0cSKirill A. Shutemov /*
455bfe6ed0cSKirill A. Shutemov * Use WARN_ONCE() to report the failure.
456bfe6ed0cSKirill A. Shutemov */
457e80a48baSPeter Zijlstra if (__halt(irq_disabled))
458bfe6ed0cSKirill A. Shutemov WARN_ONCE(1, "HLT instruction emulation failed\n");
459bfe6ed0cSKirill A. Shutemov }
460bfe6ed0cSKirill A. Shutemov
tdx_safe_halt(void)4619f98a4f4SVishal Annapurve static void __cpuidle tdx_safe_halt(void)
4629f98a4f4SVishal Annapurve {
4639f98a4f4SVishal Annapurve tdx_halt();
4649f98a4f4SVishal Annapurve /*
4659f98a4f4SVishal Annapurve * "__cpuidle" section doesn't support instrumentation, so stick
4669f98a4f4SVishal Annapurve * with raw_* variant that avoids tracing hooks.
4679f98a4f4SVishal Annapurve */
4689f98a4f4SVishal Annapurve raw_local_irq_enable();
4699f98a4f4SVishal Annapurve }
4709f98a4f4SVishal Annapurve
read_msr(struct pt_regs * regs,struct ve_info * ve)471cdd85786SKirill A. Shutemov static int read_msr(struct pt_regs *regs, struct ve_info *ve)
472ae87f609SKirill A. Shutemov {
4738a8544bdSKai Huang struct tdx_module_args args = {
474ae87f609SKirill A. Shutemov .r10 = TDX_HYPERCALL_STANDARD,
475ae87f609SKirill A. Shutemov .r11 = hcall_func(EXIT_REASON_MSR_READ),
476ae87f609SKirill A. Shutemov .r12 = regs->cx,
477ae87f609SKirill A. Shutemov };
478ae87f609SKirill A. Shutemov
479ae87f609SKirill A. Shutemov /*
480ae87f609SKirill A. Shutemov * Emulate the MSR read via hypercall. More info about ABI
481ae87f609SKirill A. Shutemov * can be found in TDX Guest-Host-Communication Interface
482ae87f609SKirill A. Shutemov * (GHCI), section titled "TDG.VP.VMCALL<Instruction.RDMSR>".
483ae87f609SKirill A. Shutemov */
484c641cfb5SKai Huang if (__tdx_hypercall(&args))
485cdd85786SKirill A. Shutemov return -EIO;
486ae87f609SKirill A. Shutemov
487ae87f609SKirill A. Shutemov regs->ax = lower_32_bits(args.r11);
488ae87f609SKirill A. Shutemov regs->dx = upper_32_bits(args.r11);
489cdd85786SKirill A. Shutemov return ve_instr_len(ve);
490ae87f609SKirill A. Shutemov }
491ae87f609SKirill A. Shutemov
write_msr(struct pt_regs * regs,struct ve_info * ve)492cdd85786SKirill A. Shutemov static int write_msr(struct pt_regs *regs, struct ve_info *ve)
493ae87f609SKirill A. Shutemov {
4948a8544bdSKai Huang struct tdx_module_args args = {
495ae87f609SKirill A. Shutemov .r10 = TDX_HYPERCALL_STANDARD,
496ae87f609SKirill A. Shutemov .r11 = hcall_func(EXIT_REASON_MSR_WRITE),
497ae87f609SKirill A. Shutemov .r12 = regs->cx,
498ae87f609SKirill A. Shutemov .r13 = (u64)regs->dx << 32 | regs->ax,
499ae87f609SKirill A. Shutemov };
500ae87f609SKirill A. Shutemov
501ae87f609SKirill A. Shutemov /*
502ae87f609SKirill A. Shutemov * Emulate the MSR write via hypercall. More info about ABI
503ae87f609SKirill A. Shutemov * can be found in TDX Guest-Host-Communication Interface
504ae87f609SKirill A. Shutemov * (GHCI) section titled "TDG.VP.VMCALL<Instruction.WRMSR>".
505ae87f609SKirill A. Shutemov */
5067a3a4018SKirill A. Shutemov if (__tdx_hypercall(&args))
507cdd85786SKirill A. Shutemov return -EIO;
508cdd85786SKirill A. Shutemov
509cdd85786SKirill A. Shutemov return ve_instr_len(ve);
510ae87f609SKirill A. Shutemov }
511ae87f609SKirill A. Shutemov
handle_cpuid(struct pt_regs * regs,struct ve_info * ve)512cdd85786SKirill A. Shutemov static int handle_cpuid(struct pt_regs *regs, struct ve_info *ve)
513c141fa2cSKirill A. Shutemov {
5148a8544bdSKai Huang struct tdx_module_args args = {
515c141fa2cSKirill A. Shutemov .r10 = TDX_HYPERCALL_STANDARD,
516c141fa2cSKirill A. Shutemov .r11 = hcall_func(EXIT_REASON_CPUID),
517c141fa2cSKirill A. Shutemov .r12 = regs->ax,
518c141fa2cSKirill A. Shutemov .r13 = regs->cx,
519c141fa2cSKirill A. Shutemov };
520c141fa2cSKirill A. Shutemov
521c141fa2cSKirill A. Shutemov /*
522c141fa2cSKirill A. Shutemov * Only allow VMM to control range reserved for hypervisor
523c141fa2cSKirill A. Shutemov * communication.
524c141fa2cSKirill A. Shutemov *
525c141fa2cSKirill A. Shutemov * Return all-zeros for any CPUID outside the range. It matches CPU
526c141fa2cSKirill A. Shutemov * behaviour for non-supported leaf.
527c141fa2cSKirill A. Shutemov */
528c141fa2cSKirill A. Shutemov if (regs->ax < 0x40000000 || regs->ax > 0x4FFFFFFF) {
529c141fa2cSKirill A. Shutemov regs->ax = regs->bx = regs->cx = regs->dx = 0;
530cdd85786SKirill A. Shutemov return ve_instr_len(ve);
531c141fa2cSKirill A. Shutemov }
532c141fa2cSKirill A. Shutemov
533c141fa2cSKirill A. Shutemov /*
534c141fa2cSKirill A. Shutemov * Emulate the CPUID instruction via a hypercall. More info about
535c141fa2cSKirill A. Shutemov * ABI can be found in TDX Guest-Host-Communication Interface
536c141fa2cSKirill A. Shutemov * (GHCI), section titled "VP.VMCALL<Instruction.CPUID>".
537c141fa2cSKirill A. Shutemov */
538c641cfb5SKai Huang if (__tdx_hypercall(&args))
539cdd85786SKirill A. Shutemov return -EIO;
540c141fa2cSKirill A. Shutemov
541c141fa2cSKirill A. Shutemov /*
542c141fa2cSKirill A. Shutemov * As per TDX GHCI CPUID ABI, r12-r15 registers contain contents of
543c141fa2cSKirill A. Shutemov * EAX, EBX, ECX, EDX registers after the CPUID instruction execution.
544c141fa2cSKirill A. Shutemov * So copy the register contents back to pt_regs.
545c141fa2cSKirill A. Shutemov */
546c141fa2cSKirill A. Shutemov regs->ax = args.r12;
547c141fa2cSKirill A. Shutemov regs->bx = args.r13;
548c141fa2cSKirill A. Shutemov regs->cx = args.r14;
549c141fa2cSKirill A. Shutemov regs->dx = args.r15;
550c141fa2cSKirill A. Shutemov
551cdd85786SKirill A. Shutemov return ve_instr_len(ve);
552c141fa2cSKirill A. Shutemov }
553c141fa2cSKirill A. Shutemov
mmio_read(int size,unsigned long addr,unsigned long * val)55431d58c4eSKirill A. Shutemov static bool mmio_read(int size, unsigned long addr, unsigned long *val)
55531d58c4eSKirill A. Shutemov {
5568a8544bdSKai Huang struct tdx_module_args args = {
55731d58c4eSKirill A. Shutemov .r10 = TDX_HYPERCALL_STANDARD,
55831d58c4eSKirill A. Shutemov .r11 = hcall_func(EXIT_REASON_EPT_VIOLATION),
55931d58c4eSKirill A. Shutemov .r12 = size,
56031d58c4eSKirill A. Shutemov .r13 = EPT_READ,
56131d58c4eSKirill A. Shutemov .r14 = addr,
56231d58c4eSKirill A. Shutemov };
56331d58c4eSKirill A. Shutemov
564c641cfb5SKai Huang if (__tdx_hypercall(&args))
56531d58c4eSKirill A. Shutemov return false;
566c641cfb5SKai Huang
56731d58c4eSKirill A. Shutemov *val = args.r11;
56831d58c4eSKirill A. Shutemov return true;
56931d58c4eSKirill A. Shutemov }
57031d58c4eSKirill A. Shutemov
mmio_write(int size,unsigned long addr,unsigned long val)57131d58c4eSKirill A. Shutemov static bool mmio_write(int size, unsigned long addr, unsigned long val)
57231d58c4eSKirill A. Shutemov {
57331d58c4eSKirill A. Shutemov return !_tdx_hypercall(hcall_func(EXIT_REASON_EPT_VIOLATION), size,
57431d58c4eSKirill A. Shutemov EPT_WRITE, addr, val);
57531d58c4eSKirill A. Shutemov }
57631d58c4eSKirill A. Shutemov
handle_mmio(struct pt_regs * regs,struct ve_info * ve)577cdd85786SKirill A. Shutemov static int handle_mmio(struct pt_regs *regs, struct ve_info *ve)
57831d58c4eSKirill A. Shutemov {
5791e776965SKirill A. Shutemov unsigned long *reg, val, vaddr;
58031d58c4eSKirill A. Shutemov char buffer[MAX_INSN_SIZE];
58172bb8f8cSJason A. Donenfeld enum insn_mmio_type mmio;
58231d58c4eSKirill A. Shutemov struct insn insn = {};
58331d58c4eSKirill A. Shutemov int size, extend_size;
58431d58c4eSKirill A. Shutemov u8 extend_val = 0;
58531d58c4eSKirill A. Shutemov
58631d58c4eSKirill A. Shutemov /* Only in-kernel MMIO is supported */
58731d58c4eSKirill A. Shutemov if (WARN_ON_ONCE(user_mode(regs)))
588cdd85786SKirill A. Shutemov return -EFAULT;
58931d58c4eSKirill A. Shutemov
59031d58c4eSKirill A. Shutemov if (copy_from_kernel_nofault(buffer, (void *)regs->ip, MAX_INSN_SIZE))
591cdd85786SKirill A. Shutemov return -EFAULT;
59231d58c4eSKirill A. Shutemov
59331d58c4eSKirill A. Shutemov if (insn_decode(&insn, buffer, MAX_INSN_SIZE, INSN_MODE_64))
594cdd85786SKirill A. Shutemov return -EINVAL;
59531d58c4eSKirill A. Shutemov
59631d58c4eSKirill A. Shutemov mmio = insn_decode_mmio(&insn, &size);
59772bb8f8cSJason A. Donenfeld if (WARN_ON_ONCE(mmio == INSN_MMIO_DECODE_FAILED))
598cdd85786SKirill A. Shutemov return -EINVAL;
59931d58c4eSKirill A. Shutemov
60072bb8f8cSJason A. Donenfeld if (mmio != INSN_MMIO_WRITE_IMM && mmio != INSN_MMIO_MOVS) {
60131d58c4eSKirill A. Shutemov reg = insn_get_modrm_reg_ptr(&insn, regs);
60231d58c4eSKirill A. Shutemov if (!reg)
603cdd85786SKirill A. Shutemov return -EINVAL;
60431d58c4eSKirill A. Shutemov }
60531d58c4eSKirill A. Shutemov
606d4fc4d01SAlexey Gladkov (Intel) if (!fault_in_kernel_space(ve->gla)) {
607d4fc4d01SAlexey Gladkov (Intel) WARN_ONCE(1, "Access to userspace address is not supported");
608d4fc4d01SAlexey Gladkov (Intel) return -EINVAL;
609d4fc4d01SAlexey Gladkov (Intel) }
610d4fc4d01SAlexey Gladkov (Intel)
6111e776965SKirill A. Shutemov /*
6121e776965SKirill A. Shutemov * Reject EPT violation #VEs that split pages.
6131e776965SKirill A. Shutemov *
6141e776965SKirill A. Shutemov * MMIO accesses are supposed to be naturally aligned and therefore
6151e776965SKirill A. Shutemov * never cross page boundaries. Seeing split page accesses indicates
6161e776965SKirill A. Shutemov * a bug or a load_unaligned_zeropad() that stepped into an MMIO page.
6171e776965SKirill A. Shutemov *
6181e776965SKirill A. Shutemov * load_unaligned_zeropad() will recover using exception fixups.
6191e776965SKirill A. Shutemov */
6201e776965SKirill A. Shutemov vaddr = (unsigned long)insn_get_addr_ref(&insn, regs);
6211e776965SKirill A. Shutemov if (vaddr / PAGE_SIZE != (vaddr + size - 1) / PAGE_SIZE)
6221e776965SKirill A. Shutemov return -EFAULT;
6231e776965SKirill A. Shutemov
62431d58c4eSKirill A. Shutemov /* Handle writes first */
62531d58c4eSKirill A. Shutemov switch (mmio) {
62672bb8f8cSJason A. Donenfeld case INSN_MMIO_WRITE:
62731d58c4eSKirill A. Shutemov memcpy(&val, reg, size);
628cdd85786SKirill A. Shutemov if (!mmio_write(size, ve->gpa, val))
629cdd85786SKirill A. Shutemov return -EIO;
630cdd85786SKirill A. Shutemov return insn.length;
63172bb8f8cSJason A. Donenfeld case INSN_MMIO_WRITE_IMM:
63231d58c4eSKirill A. Shutemov val = insn.immediate.value;
633cdd85786SKirill A. Shutemov if (!mmio_write(size, ve->gpa, val))
634cdd85786SKirill A. Shutemov return -EIO;
635cdd85786SKirill A. Shutemov return insn.length;
63672bb8f8cSJason A. Donenfeld case INSN_MMIO_READ:
63772bb8f8cSJason A. Donenfeld case INSN_MMIO_READ_ZERO_EXTEND:
63872bb8f8cSJason A. Donenfeld case INSN_MMIO_READ_SIGN_EXTEND:
63931d58c4eSKirill A. Shutemov /* Reads are handled below */
64031d58c4eSKirill A. Shutemov break;
64172bb8f8cSJason A. Donenfeld case INSN_MMIO_MOVS:
64272bb8f8cSJason A. Donenfeld case INSN_MMIO_DECODE_FAILED:
64331d58c4eSKirill A. Shutemov /*
64431d58c4eSKirill A. Shutemov * MMIO was accessed with an instruction that could not be
64531d58c4eSKirill A. Shutemov * decoded or handled properly. It was likely not using io.h
64631d58c4eSKirill A. Shutemov * helpers or accessed MMIO accidentally.
64731d58c4eSKirill A. Shutemov */
648cdd85786SKirill A. Shutemov return -EINVAL;
64931d58c4eSKirill A. Shutemov default:
65031d58c4eSKirill A. Shutemov WARN_ONCE(1, "Unknown insn_decode_mmio() decode value?");
651cdd85786SKirill A. Shutemov return -EINVAL;
65231d58c4eSKirill A. Shutemov }
65331d58c4eSKirill A. Shutemov
65431d58c4eSKirill A. Shutemov /* Handle reads */
65531d58c4eSKirill A. Shutemov if (!mmio_read(size, ve->gpa, &val))
656cdd85786SKirill A. Shutemov return -EIO;
65731d58c4eSKirill A. Shutemov
65831d58c4eSKirill A. Shutemov switch (mmio) {
65972bb8f8cSJason A. Donenfeld case INSN_MMIO_READ:
66031d58c4eSKirill A. Shutemov /* Zero-extend for 32-bit operation */
66131d58c4eSKirill A. Shutemov extend_size = size == 4 ? sizeof(*reg) : 0;
66231d58c4eSKirill A. Shutemov break;
66372bb8f8cSJason A. Donenfeld case INSN_MMIO_READ_ZERO_EXTEND:
66431d58c4eSKirill A. Shutemov /* Zero extend based on operand size */
66531d58c4eSKirill A. Shutemov extend_size = insn.opnd_bytes;
66631d58c4eSKirill A. Shutemov break;
66772bb8f8cSJason A. Donenfeld case INSN_MMIO_READ_SIGN_EXTEND:
66831d58c4eSKirill A. Shutemov /* Sign extend based on operand size */
66931d58c4eSKirill A. Shutemov extend_size = insn.opnd_bytes;
67031d58c4eSKirill A. Shutemov if (size == 1 && val & BIT(7))
67131d58c4eSKirill A. Shutemov extend_val = 0xFF;
67231d58c4eSKirill A. Shutemov else if (size > 1 && val & BIT(15))
67331d58c4eSKirill A. Shutemov extend_val = 0xFF;
67431d58c4eSKirill A. Shutemov break;
67531d58c4eSKirill A. Shutemov default:
67631d58c4eSKirill A. Shutemov /* All other cases has to be covered with the first switch() */
67731d58c4eSKirill A. Shutemov WARN_ON_ONCE(1);
678cdd85786SKirill A. Shutemov return -EINVAL;
67931d58c4eSKirill A. Shutemov }
68031d58c4eSKirill A. Shutemov
68131d58c4eSKirill A. Shutemov if (extend_size)
68231d58c4eSKirill A. Shutemov memset(reg, extend_val, extend_size);
68331d58c4eSKirill A. Shutemov memcpy(reg, &val, size);
684cdd85786SKirill A. Shutemov return insn.length;
68531d58c4eSKirill A. Shutemov }
68631d58c4eSKirill A. Shutemov
handle_in(struct pt_regs * regs,int size,int port)68703149948SKuppuswamy Sathyanarayanan static bool handle_in(struct pt_regs *regs, int size, int port)
68803149948SKuppuswamy Sathyanarayanan {
6898a8544bdSKai Huang struct tdx_module_args args = {
69003149948SKuppuswamy Sathyanarayanan .r10 = TDX_HYPERCALL_STANDARD,
69103149948SKuppuswamy Sathyanarayanan .r11 = hcall_func(EXIT_REASON_IO_INSTRUCTION),
69203149948SKuppuswamy Sathyanarayanan .r12 = size,
69303149948SKuppuswamy Sathyanarayanan .r13 = PORT_READ,
69403149948SKuppuswamy Sathyanarayanan .r14 = port,
69503149948SKuppuswamy Sathyanarayanan };
69603149948SKuppuswamy Sathyanarayanan u64 mask = GENMASK(BITS_PER_BYTE * size, 0);
69703149948SKuppuswamy Sathyanarayanan bool success;
69803149948SKuppuswamy Sathyanarayanan
69903149948SKuppuswamy Sathyanarayanan /*
70003149948SKuppuswamy Sathyanarayanan * Emulate the I/O read via hypercall. More info about ABI can be found
70103149948SKuppuswamy Sathyanarayanan * in TDX Guest-Host-Communication Interface (GHCI) section titled
70203149948SKuppuswamy Sathyanarayanan * "TDG.VP.VMCALL<Instruction.IO>".
70303149948SKuppuswamy Sathyanarayanan */
704c641cfb5SKai Huang success = !__tdx_hypercall(&args);
70503149948SKuppuswamy Sathyanarayanan
70603149948SKuppuswamy Sathyanarayanan /* Update part of the register affected by the emulated instruction */
70703149948SKuppuswamy Sathyanarayanan regs->ax &= ~mask;
70803149948SKuppuswamy Sathyanarayanan if (success)
70903149948SKuppuswamy Sathyanarayanan regs->ax |= args.r11 & mask;
71003149948SKuppuswamy Sathyanarayanan
71103149948SKuppuswamy Sathyanarayanan return success;
71203149948SKuppuswamy Sathyanarayanan }
71303149948SKuppuswamy Sathyanarayanan
handle_out(struct pt_regs * regs,int size,int port)71403149948SKuppuswamy Sathyanarayanan static bool handle_out(struct pt_regs *regs, int size, int port)
71503149948SKuppuswamy Sathyanarayanan {
71603149948SKuppuswamy Sathyanarayanan u64 mask = GENMASK(BITS_PER_BYTE * size, 0);
71703149948SKuppuswamy Sathyanarayanan
71803149948SKuppuswamy Sathyanarayanan /*
71903149948SKuppuswamy Sathyanarayanan * Emulate the I/O write via hypercall. More info about ABI can be found
72003149948SKuppuswamy Sathyanarayanan * in TDX Guest-Host-Communication Interface (GHCI) section titled
72103149948SKuppuswamy Sathyanarayanan * "TDG.VP.VMCALL<Instruction.IO>".
72203149948SKuppuswamy Sathyanarayanan */
72303149948SKuppuswamy Sathyanarayanan return !_tdx_hypercall(hcall_func(EXIT_REASON_IO_INSTRUCTION), size,
72403149948SKuppuswamy Sathyanarayanan PORT_WRITE, port, regs->ax & mask);
72503149948SKuppuswamy Sathyanarayanan }
72603149948SKuppuswamy Sathyanarayanan
72703149948SKuppuswamy Sathyanarayanan /*
72803149948SKuppuswamy Sathyanarayanan * Emulate I/O using hypercall.
72903149948SKuppuswamy Sathyanarayanan *
73003149948SKuppuswamy Sathyanarayanan * Assumes the IO instruction was using ax, which is enforced
73103149948SKuppuswamy Sathyanarayanan * by the standard io.h macros.
73203149948SKuppuswamy Sathyanarayanan *
73303149948SKuppuswamy Sathyanarayanan * Return True on success or False on failure.
73403149948SKuppuswamy Sathyanarayanan */
handle_io(struct pt_regs * regs,struct ve_info * ve)735cdd85786SKirill A. Shutemov static int handle_io(struct pt_regs *regs, struct ve_info *ve)
73603149948SKuppuswamy Sathyanarayanan {
737cdd85786SKirill A. Shutemov u32 exit_qual = ve->exit_qual;
73803149948SKuppuswamy Sathyanarayanan int size, port;
739cdd85786SKirill A. Shutemov bool in, ret;
74003149948SKuppuswamy Sathyanarayanan
74103149948SKuppuswamy Sathyanarayanan if (VE_IS_IO_STRING(exit_qual))
742cdd85786SKirill A. Shutemov return -EIO;
74303149948SKuppuswamy Sathyanarayanan
74403149948SKuppuswamy Sathyanarayanan in = VE_IS_IO_IN(exit_qual);
74503149948SKuppuswamy Sathyanarayanan size = VE_GET_IO_SIZE(exit_qual);
74603149948SKuppuswamy Sathyanarayanan port = VE_GET_PORT_NUM(exit_qual);
74703149948SKuppuswamy Sathyanarayanan
74803149948SKuppuswamy Sathyanarayanan
74903149948SKuppuswamy Sathyanarayanan if (in)
750cdd85786SKirill A. Shutemov ret = handle_in(regs, size, port);
75103149948SKuppuswamy Sathyanarayanan else
752cdd85786SKirill A. Shutemov ret = handle_out(regs, size, port);
753cdd85786SKirill A. Shutemov if (!ret)
754cdd85786SKirill A. Shutemov return -EIO;
755cdd85786SKirill A. Shutemov
756cdd85786SKirill A. Shutemov return ve_instr_len(ve);
75703149948SKuppuswamy Sathyanarayanan }
75803149948SKuppuswamy Sathyanarayanan
75932e72854SAndi Kleen /*
76032e72854SAndi Kleen * Early #VE exception handler. Only handles a subset of port I/O.
76132e72854SAndi Kleen * Intended only for earlyprintk. If failed, return false.
76232e72854SAndi Kleen */
tdx_early_handle_ve(struct pt_regs * regs)76332e72854SAndi Kleen __init bool tdx_early_handle_ve(struct pt_regs *regs)
76432e72854SAndi Kleen {
76532e72854SAndi Kleen struct ve_info ve;
766cdd85786SKirill A. Shutemov int insn_len;
76732e72854SAndi Kleen
76832e72854SAndi Kleen tdx_get_ve_info(&ve);
76932e72854SAndi Kleen
77032e72854SAndi Kleen if (ve.exit_reason != EXIT_REASON_IO_INSTRUCTION)
77132e72854SAndi Kleen return false;
77232e72854SAndi Kleen
773cdd85786SKirill A. Shutemov insn_len = handle_io(regs, &ve);
774cdd85786SKirill A. Shutemov if (insn_len < 0)
775cdd85786SKirill A. Shutemov return false;
776cdd85786SKirill A. Shutemov
777cdd85786SKirill A. Shutemov regs->ip += insn_len;
778cdd85786SKirill A. Shutemov return true;
77932e72854SAndi Kleen }
78032e72854SAndi Kleen
tdx_get_ve_info(struct ve_info * ve)7819a22bf6dSKirill A. Shutemov void tdx_get_ve_info(struct ve_info *ve)
7829a22bf6dSKirill A. Shutemov {
78357a420bbSKai Huang struct tdx_module_args args = {};
7849a22bf6dSKirill A. Shutemov
7859a22bf6dSKirill A. Shutemov /*
7869a22bf6dSKirill A. Shutemov * Called during #VE handling to retrieve the #VE info from the
7879a22bf6dSKirill A. Shutemov * TDX module.
7889a22bf6dSKirill A. Shutemov *
7899a22bf6dSKirill A. Shutemov * This has to be called early in #VE handling. A "nested" #VE which
7909a22bf6dSKirill A. Shutemov * occurs before this will raise a #DF and is not recoverable.
7919a22bf6dSKirill A. Shutemov *
7929a22bf6dSKirill A. Shutemov * The call retrieves the #VE info from the TDX module, which also
7939a22bf6dSKirill A. Shutemov * clears the "#VE valid" flag. This must be done before anything else
7949a22bf6dSKirill A. Shutemov * because any #VE that occurs while the valid flag is set will lead to
7959a22bf6dSKirill A. Shutemov * #DF.
7969a22bf6dSKirill A. Shutemov *
7979a22bf6dSKirill A. Shutemov * Note, the TDX module treats virtual NMIs as inhibited if the #VE
7989a22bf6dSKirill A. Shutemov * valid flag is set. It means that NMI=>#VE will not result in a #DF.
7999a22bf6dSKirill A. Shutemov */
80057a420bbSKai Huang tdcall(TDG_VP_VEINFO_GET, &args);
8019a22bf6dSKirill A. Shutemov
8029a22bf6dSKirill A. Shutemov /* Transfer the output parameters */
80357a420bbSKai Huang ve->exit_reason = args.rcx;
80457a420bbSKai Huang ve->exit_qual = args.rdx;
80557a420bbSKai Huang ve->gla = args.r8;
80657a420bbSKai Huang ve->gpa = args.r9;
80757a420bbSKai Huang ve->instr_len = lower_32_bits(args.r10);
80857a420bbSKai Huang ve->instr_info = upper_32_bits(args.r10);
8099a22bf6dSKirill A. Shutemov }
8109a22bf6dSKirill A. Shutemov
811cdd85786SKirill A. Shutemov /*
812cdd85786SKirill A. Shutemov * Handle the user initiated #VE.
813cdd85786SKirill A. Shutemov *
814cdd85786SKirill A. Shutemov * On success, returns the number of bytes RIP should be incremented (>=0)
815cdd85786SKirill A. Shutemov * or -errno on error.
816cdd85786SKirill A. Shutemov */
virt_exception_user(struct pt_regs * regs,struct ve_info * ve)817cdd85786SKirill A. Shutemov static int virt_exception_user(struct pt_regs *regs, struct ve_info *ve)
818c141fa2cSKirill A. Shutemov {
819c141fa2cSKirill A. Shutemov switch (ve->exit_reason) {
820c141fa2cSKirill A. Shutemov case EXIT_REASON_CPUID:
821cdd85786SKirill A. Shutemov return handle_cpuid(regs, ve);
822c141fa2cSKirill A. Shutemov default:
823c141fa2cSKirill A. Shutemov pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
824cdd85786SKirill A. Shutemov return -EIO;
825c141fa2cSKirill A. Shutemov }
826c141fa2cSKirill A. Shutemov }
827c141fa2cSKirill A. Shutemov
is_private_gpa(u64 gpa)82847e67cf3SKirill A. Shutemov static inline bool is_private_gpa(u64 gpa)
82947e67cf3SKirill A. Shutemov {
83047e67cf3SKirill A. Shutemov return gpa == cc_mkenc(gpa);
83147e67cf3SKirill A. Shutemov }
83247e67cf3SKirill A. Shutemov
833cdd85786SKirill A. Shutemov /*
834cdd85786SKirill A. Shutemov * Handle the kernel #VE.
835cdd85786SKirill A. Shutemov *
836cdd85786SKirill A. Shutemov * On success, returns the number of bytes RIP should be incremented (>=0)
837cdd85786SKirill A. Shutemov * or -errno on error.
838cdd85786SKirill A. Shutemov */
virt_exception_kernel(struct pt_regs * regs,struct ve_info * ve)839cdd85786SKirill A. Shutemov static int virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve)
840bfe6ed0cSKirill A. Shutemov {
841bfe6ed0cSKirill A. Shutemov switch (ve->exit_reason) {
842bfe6ed0cSKirill A. Shutemov case EXIT_REASON_HLT:
843cdd85786SKirill A. Shutemov return handle_halt(ve);
844ae87f609SKirill A. Shutemov case EXIT_REASON_MSR_READ:
845cdd85786SKirill A. Shutemov return read_msr(regs, ve);
846ae87f609SKirill A. Shutemov case EXIT_REASON_MSR_WRITE:
847cdd85786SKirill A. Shutemov return write_msr(regs, ve);
848c141fa2cSKirill A. Shutemov case EXIT_REASON_CPUID:
849cdd85786SKirill A. Shutemov return handle_cpuid(regs, ve);
85031d58c4eSKirill A. Shutemov case EXIT_REASON_EPT_VIOLATION:
85147e67cf3SKirill A. Shutemov if (is_private_gpa(ve->gpa))
85247e67cf3SKirill A. Shutemov panic("Unexpected EPT-violation on private memory.");
85331d58c4eSKirill A. Shutemov return handle_mmio(regs, ve);
85403149948SKuppuswamy Sathyanarayanan case EXIT_REASON_IO_INSTRUCTION:
855cdd85786SKirill A. Shutemov return handle_io(regs, ve);
856bfe6ed0cSKirill A. Shutemov default:
857bfe6ed0cSKirill A. Shutemov pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
858cdd85786SKirill A. Shutemov return -EIO;
859bfe6ed0cSKirill A. Shutemov }
860bfe6ed0cSKirill A. Shutemov }
861bfe6ed0cSKirill A. Shutemov
tdx_handle_virt_exception(struct pt_regs * regs,struct ve_info * ve)8629a22bf6dSKirill A. Shutemov bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve)
8639a22bf6dSKirill A. Shutemov {
864cdd85786SKirill A. Shutemov int insn_len;
8659a22bf6dSKirill A. Shutemov
866bfe6ed0cSKirill A. Shutemov if (user_mode(regs))
867cdd85786SKirill A. Shutemov insn_len = virt_exception_user(regs, ve);
868bfe6ed0cSKirill A. Shutemov else
869cdd85786SKirill A. Shutemov insn_len = virt_exception_kernel(regs, ve);
870cdd85786SKirill A. Shutemov if (insn_len < 0)
871cdd85786SKirill A. Shutemov return false;
872bfe6ed0cSKirill A. Shutemov
873bfe6ed0cSKirill A. Shutemov /* After successful #VE handling, move the IP */
874cdd85786SKirill A. Shutemov regs->ip += insn_len;
875bfe6ed0cSKirill A. Shutemov
876cdd85786SKirill A. Shutemov return true;
8779a22bf6dSKirill A. Shutemov }
8789a22bf6dSKirill A. Shutemov
tdx_tlb_flush_required(bool private)8797dbde763SKirill A. Shutemov static bool tdx_tlb_flush_required(bool private)
8807dbde763SKirill A. Shutemov {
8817dbde763SKirill A. Shutemov /*
8827dbde763SKirill A. Shutemov * TDX guest is responsible for flushing TLB on private->shared
8837dbde763SKirill A. Shutemov * transition. VMM is responsible for flushing on shared->private.
8847dbde763SKirill A. Shutemov *
8857dbde763SKirill A. Shutemov * The VMM _can't_ flush private addresses as it can't generate PAs
8867dbde763SKirill A. Shutemov * with the guest's HKID. Shared memory isn't subject to integrity
8877dbde763SKirill A. Shutemov * checking, i.e. the VMM doesn't need to flush for its own protection.
8887dbde763SKirill A. Shutemov *
8897dbde763SKirill A. Shutemov * There's no need to flush when converting from shared to private,
8907dbde763SKirill A. Shutemov * as flushing is the VMM's responsibility in this case, e.g. it must
8917dbde763SKirill A. Shutemov * flush to avoid integrity failures in the face of a buggy or
8927dbde763SKirill A. Shutemov * malicious guest.
8937dbde763SKirill A. Shutemov */
8947dbde763SKirill A. Shutemov return !private;
8957dbde763SKirill A. Shutemov }
8967dbde763SKirill A. Shutemov
tdx_cache_flush_required(void)8977dbde763SKirill A. Shutemov static bool tdx_cache_flush_required(void)
8987dbde763SKirill A. Shutemov {
8997dbde763SKirill A. Shutemov /*
9007dbde763SKirill A. Shutemov * AMD SME/SEV can avoid cache flushing if HW enforces cache coherence.
9017dbde763SKirill A. Shutemov * TDX doesn't have such capability.
9027dbde763SKirill A. Shutemov *
9037dbde763SKirill A. Shutemov * Flush cache unconditionally.
9047dbde763SKirill A. Shutemov */
9057dbde763SKirill A. Shutemov return true;
9067dbde763SKirill A. Shutemov }
9077dbde763SKirill A. Shutemov
9087dbde763SKirill A. Shutemov /*
909019b383dSDexuan Cui * Notify the VMM about page mapping conversion. More info about ABI
910019b383dSDexuan Cui * can be found in TDX Guest-Host-Communication Interface (GHCI),
911019b383dSDexuan Cui * section "TDG.VP.VMCALL<MapGPA>".
912019b383dSDexuan Cui */
tdx_map_gpa(phys_addr_t start,phys_addr_t end,bool enc)913019b383dSDexuan Cui static bool tdx_map_gpa(phys_addr_t start, phys_addr_t end, bool enc)
914019b383dSDexuan Cui {
915019b383dSDexuan Cui /* Retrying the hypercall a second time should succeed; use 3 just in case */
916019b383dSDexuan Cui const int max_retries_per_page = 3;
917019b383dSDexuan Cui int retry_count = 0;
918019b383dSDexuan Cui
919019b383dSDexuan Cui if (!enc) {
920019b383dSDexuan Cui /* Set the shared (decrypted) bits: */
921019b383dSDexuan Cui start |= cc_mkdec(0);
922019b383dSDexuan Cui end |= cc_mkdec(0);
923019b383dSDexuan Cui }
924019b383dSDexuan Cui
925019b383dSDexuan Cui while (retry_count < max_retries_per_page) {
9268a8544bdSKai Huang struct tdx_module_args args = {
927019b383dSDexuan Cui .r10 = TDX_HYPERCALL_STANDARD,
928019b383dSDexuan Cui .r11 = TDVMCALL_MAP_GPA,
929019b383dSDexuan Cui .r12 = start,
930019b383dSDexuan Cui .r13 = end - start };
931019b383dSDexuan Cui
932019b383dSDexuan Cui u64 map_fail_paddr;
933c641cfb5SKai Huang u64 ret = __tdx_hypercall(&args);
934019b383dSDexuan Cui
935019b383dSDexuan Cui if (ret != TDVMCALL_STATUS_RETRY)
936019b383dSDexuan Cui return !ret;
937019b383dSDexuan Cui /*
938019b383dSDexuan Cui * The guest must retry the operation for the pages in the
939019b383dSDexuan Cui * region starting at the GPA specified in R11. R11 comes
940019b383dSDexuan Cui * from the untrusted VMM. Sanity check it.
941019b383dSDexuan Cui */
942019b383dSDexuan Cui map_fail_paddr = args.r11;
943019b383dSDexuan Cui if (map_fail_paddr < start || map_fail_paddr >= end)
944019b383dSDexuan Cui return false;
945019b383dSDexuan Cui
946019b383dSDexuan Cui /* "Consume" a retry without forward progress */
947019b383dSDexuan Cui if (map_fail_paddr == start) {
948019b383dSDexuan Cui retry_count++;
949019b383dSDexuan Cui continue;
950019b383dSDexuan Cui }
951019b383dSDexuan Cui
952019b383dSDexuan Cui start = map_fail_paddr;
953019b383dSDexuan Cui retry_count = 0;
954019b383dSDexuan Cui }
955019b383dSDexuan Cui
956019b383dSDexuan Cui return false;
957019b383dSDexuan Cui }
958019b383dSDexuan Cui
959019b383dSDexuan Cui /*
9607dbde763SKirill A. Shutemov * Inform the VMM of the guest's intent for this physical page: shared with
9617dbde763SKirill A. Shutemov * the VMM or private to the guest. The VMM is expected to change its mapping
9627dbde763SKirill A. Shutemov * of the page in response.
9637dbde763SKirill A. Shutemov */
tdx_enc_status_changed(unsigned long vaddr,int numpages,bool enc)9647dbde763SKirill A. Shutemov static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
9657dbde763SKirill A. Shutemov {
9667dbde763SKirill A. Shutemov phys_addr_t start = __pa(vaddr);
9677dbde763SKirill A. Shutemov phys_addr_t end = __pa(vaddr + numpages * PAGE_SIZE);
9687dbde763SKirill A. Shutemov
969019b383dSDexuan Cui if (!tdx_map_gpa(start, end, enc))
9707dbde763SKirill A. Shutemov return false;
9717dbde763SKirill A. Shutemov
97275d090fdSKirill A. Shutemov /* shared->private conversion requires memory to be accepted before use */
97375d090fdSKirill A. Shutemov if (enc)
97475d090fdSKirill A. Shutemov return tdx_accept_memory(start, end);
9757dbde763SKirill A. Shutemov
9767dbde763SKirill A. Shutemov return true;
9777dbde763SKirill A. Shutemov }
9787dbde763SKirill A. Shutemov
tdx_enc_status_change_prepare(unsigned long vaddr,int numpages,bool enc)97999c5c4c6SKirill A. Shutemov static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
980195edce0SKirill A. Shutemov bool enc)
981195edce0SKirill A. Shutemov {
982195edce0SKirill A. Shutemov /*
983195edce0SKirill A. Shutemov * Only handle shared->private conversion here.
984195edce0SKirill A. Shutemov * See the comment in tdx_early_init().
985195edce0SKirill A. Shutemov */
98699c5c4c6SKirill A. Shutemov if (enc && !tdx_enc_status_changed(vaddr, numpages, enc))
98799c5c4c6SKirill A. Shutemov return -EIO;
98899c5c4c6SKirill A. Shutemov
98999c5c4c6SKirill A. Shutemov return 0;
990195edce0SKirill A. Shutemov }
991195edce0SKirill A. Shutemov
tdx_enc_status_change_finish(unsigned long vaddr,int numpages,bool enc)99299c5c4c6SKirill A. Shutemov static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
993195edce0SKirill A. Shutemov bool enc)
994195edce0SKirill A. Shutemov {
995195edce0SKirill A. Shutemov /*
996195edce0SKirill A. Shutemov * Only handle private->shared conversion here.
997195edce0SKirill A. Shutemov * See the comment in tdx_early_init().
998195edce0SKirill A. Shutemov */
99999c5c4c6SKirill A. Shutemov if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc))
100099c5c4c6SKirill A. Shutemov return -EIO;
100199c5c4c6SKirill A. Shutemov
1002c3abbf13SKirill A. Shutemov if (enc)
1003c3abbf13SKirill A. Shutemov atomic_long_sub(numpages, &nr_shared);
1004c3abbf13SKirill A. Shutemov else
1005c3abbf13SKirill A. Shutemov atomic_long_add(numpages, &nr_shared);
1006c3abbf13SKirill A. Shutemov
100799c5c4c6SKirill A. Shutemov return 0;
1008195edce0SKirill A. Shutemov }
1009195edce0SKirill A. Shutemov
1010859e63b7SKirill A. Shutemov /* Stop new private<->shared conversions */
tdx_kexec_begin(void)1011859e63b7SKirill A. Shutemov static void tdx_kexec_begin(void)
1012859e63b7SKirill A. Shutemov {
1013859e63b7SKirill A. Shutemov if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1014859e63b7SKirill A. Shutemov return;
1015859e63b7SKirill A. Shutemov
1016859e63b7SKirill A. Shutemov /*
1017859e63b7SKirill A. Shutemov * Crash kernel reaches here with interrupts disabled: can't wait for
1018859e63b7SKirill A. Shutemov * conversions to finish.
1019859e63b7SKirill A. Shutemov *
1020859e63b7SKirill A. Shutemov * If race happened, just report and proceed.
1021859e63b7SKirill A. Shutemov */
1022859e63b7SKirill A. Shutemov if (!set_memory_enc_stop_conversion())
1023859e63b7SKirill A. Shutemov pr_warn("Failed to stop shared<->private conversions\n");
1024859e63b7SKirill A. Shutemov }
1025859e63b7SKirill A. Shutemov
1026859e63b7SKirill A. Shutemov /* Walk direct mapping and convert all shared memory back to private */
tdx_kexec_finish(void)1027859e63b7SKirill A. Shutemov static void tdx_kexec_finish(void)
1028859e63b7SKirill A. Shutemov {
1029859e63b7SKirill A. Shutemov unsigned long addr, end;
1030859e63b7SKirill A. Shutemov long found = 0, shared;
1031859e63b7SKirill A. Shutemov
1032859e63b7SKirill A. Shutemov if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1033859e63b7SKirill A. Shutemov return;
1034859e63b7SKirill A. Shutemov
1035859e63b7SKirill A. Shutemov lockdep_assert_irqs_disabled();
1036859e63b7SKirill A. Shutemov
1037859e63b7SKirill A. Shutemov addr = PAGE_OFFSET;
1038859e63b7SKirill A. Shutemov end = PAGE_OFFSET + get_max_mapped();
1039859e63b7SKirill A. Shutemov
1040859e63b7SKirill A. Shutemov while (addr < end) {
1041859e63b7SKirill A. Shutemov unsigned long size;
1042859e63b7SKirill A. Shutemov unsigned int level;
1043859e63b7SKirill A. Shutemov pte_t *pte;
1044859e63b7SKirill A. Shutemov
1045859e63b7SKirill A. Shutemov pte = lookup_address(addr, &level);
1046859e63b7SKirill A. Shutemov size = page_level_size(level);
1047859e63b7SKirill A. Shutemov
1048859e63b7SKirill A. Shutemov if (pte && pte_decrypted(*pte)) {
1049859e63b7SKirill A. Shutemov int pages = size / PAGE_SIZE;
1050859e63b7SKirill A. Shutemov
1051859e63b7SKirill A. Shutemov /*
1052859e63b7SKirill A. Shutemov * Touching memory with shared bit set triggers implicit
1053859e63b7SKirill A. Shutemov * conversion to shared.
1054859e63b7SKirill A. Shutemov *
1055859e63b7SKirill A. Shutemov * Make sure nobody touches the shared range from
1056859e63b7SKirill A. Shutemov * now on.
1057859e63b7SKirill A. Shutemov */
1058859e63b7SKirill A. Shutemov set_pte(pte, __pte(0));
1059859e63b7SKirill A. Shutemov
1060859e63b7SKirill A. Shutemov /*
1061859e63b7SKirill A. Shutemov * Memory encryption state persists across kexec.
1062859e63b7SKirill A. Shutemov * If tdx_enc_status_changed() fails in the first
1063859e63b7SKirill A. Shutemov * kernel, it leaves memory in an unknown state.
1064859e63b7SKirill A. Shutemov *
1065859e63b7SKirill A. Shutemov * If that memory remains shared, accessing it in the
1066859e63b7SKirill A. Shutemov * *next* kernel through a private mapping will result
1067859e63b7SKirill A. Shutemov * in an unrecoverable guest shutdown.
1068859e63b7SKirill A. Shutemov *
1069859e63b7SKirill A. Shutemov * The kdump kernel boot is not impacted as it uses
1070859e63b7SKirill A. Shutemov * a pre-reserved memory range that is always private.
1071859e63b7SKirill A. Shutemov * However, gathering crash information could lead to
1072859e63b7SKirill A. Shutemov * a crash if it accesses unconverted memory through
1073859e63b7SKirill A. Shutemov * a private mapping which is possible when accessing
1074859e63b7SKirill A. Shutemov * that memory through /proc/vmcore, for example.
1075859e63b7SKirill A. Shutemov *
1076859e63b7SKirill A. Shutemov * In all cases, print error info in order to leave
1077859e63b7SKirill A. Shutemov * enough bread crumbs for debugging.
1078859e63b7SKirill A. Shutemov */
1079859e63b7SKirill A. Shutemov if (!tdx_enc_status_changed(addr, pages, true)) {
1080859e63b7SKirill A. Shutemov pr_err("Failed to unshare range %#lx-%#lx\n",
1081859e63b7SKirill A. Shutemov addr, addr + size);
1082859e63b7SKirill A. Shutemov }
1083859e63b7SKirill A. Shutemov
1084859e63b7SKirill A. Shutemov found += pages;
1085859e63b7SKirill A. Shutemov }
1086859e63b7SKirill A. Shutemov
1087859e63b7SKirill A. Shutemov addr += size;
1088859e63b7SKirill A. Shutemov }
1089859e63b7SKirill A. Shutemov
1090859e63b7SKirill A. Shutemov __flush_tlb_all();
1091859e63b7SKirill A. Shutemov
1092859e63b7SKirill A. Shutemov shared = atomic_long_read(&nr_shared);
1093859e63b7SKirill A. Shutemov if (shared != found) {
1094859e63b7SKirill A. Shutemov pr_err("shared page accounting is off\n");
1095859e63b7SKirill A. Shutemov pr_err("nr_shared = %ld, nr_found = %ld\n", shared, found);
1096859e63b7SKirill A. Shutemov }
1097859e63b7SKirill A. Shutemov }
1098859e63b7SKirill A. Shutemov
tdx_announce(void)1099564ea84cSKirill A. Shutemov static __init void tdx_announce(void)
1100564ea84cSKirill A. Shutemov {
1101564ea84cSKirill A. Shutemov struct tdx_module_args args = {};
1102564ea84cSKirill A. Shutemov u64 controls;
1103564ea84cSKirill A. Shutemov
1104564ea84cSKirill A. Shutemov pr_info("Guest detected\n");
1105564ea84cSKirill A. Shutemov
1106564ea84cSKirill A. Shutemov tdcall(TDG_VP_INFO, &args);
1107564ea84cSKirill A. Shutemov tdx_dump_attributes(args.rdx);
1108564ea84cSKirill A. Shutemov
1109564ea84cSKirill A. Shutemov tdg_vm_rd(TDCS_TD_CTLS, &controls);
1110564ea84cSKirill A. Shutemov tdx_dump_td_ctls(controls);
1111564ea84cSKirill A. Shutemov }
1112564ea84cSKirill A. Shutemov
tdx_early_init(void)111359bd54a8SKuppuswamy Sathyanarayanan void __init tdx_early_init(void)
111459bd54a8SKuppuswamy Sathyanarayanan {
111541394e33SKirill A. Shutemov u64 cc_mask;
111659bd54a8SKuppuswamy Sathyanarayanan u32 eax, sig[3];
111759bd54a8SKuppuswamy Sathyanarayanan
111859bd54a8SKuppuswamy Sathyanarayanan cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]);
111959bd54a8SKuppuswamy Sathyanarayanan
112059bd54a8SKuppuswamy Sathyanarayanan if (memcmp(TDX_IDENT, sig, sizeof(sig)))
112159bd54a8SKuppuswamy Sathyanarayanan return;
112259bd54a8SKuppuswamy Sathyanarayanan
112359bd54a8SKuppuswamy Sathyanarayanan setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);
112459bd54a8SKuppuswamy Sathyanarayanan
11259ee4318cSKirill A. Shutemov /* TSC is the only reliable clock in TDX guest */
11269ee4318cSKirill A. Shutemov setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
11279ee4318cSKirill A. Shutemov
1128da86eb96SBorislav Petkov (AMD) cc_vendor = CC_VENDOR_INTEL;
112941394e33SKirill A. Shutemov
1130b064043dSKirill A. Shutemov /* Configure the TD */
1131b064043dSKirill A. Shutemov tdx_setup(&cc_mask);
1132b064043dSKirill A. Shutemov
1133b064043dSKirill A. Shutemov cc_set_mask(cc_mask);
11348de62af0SKirill A. Shutemov
113565fab5bcSKirill A. Shutemov /*
113665fab5bcSKirill A. Shutemov * All bits above GPA width are reserved and kernel treats shared bit
113765fab5bcSKirill A. Shutemov * as flag, not as part of physical address.
113865fab5bcSKirill A. Shutemov *
113965fab5bcSKirill A. Shutemov * Adjust physical mask to only cover valid GPA bits.
114065fab5bcSKirill A. Shutemov */
114165fab5bcSKirill A. Shutemov physical_mask &= cc_mask - 1;
114265fab5bcSKirill A. Shutemov
1143195edce0SKirill A. Shutemov /*
1144195edce0SKirill A. Shutemov * The kernel mapping should match the TDX metadata for the page.
1145195edce0SKirill A. Shutemov * load_unaligned_zeropad() can touch memory *adjacent* to that which is
1146195edce0SKirill A. Shutemov * owned by the caller and can catch even _momentary_ mismatches. Bad
1147195edce0SKirill A. Shutemov * things happen on mismatch:
1148195edce0SKirill A. Shutemov *
1149195edce0SKirill A. Shutemov * - Private mapping => Shared Page == Guest shutdown
1150195edce0SKirill A. Shutemov * - Shared mapping => Private Page == Recoverable #VE
1151195edce0SKirill A. Shutemov *
1152195edce0SKirill A. Shutemov * guest.enc_status_change_prepare() converts the page from
1153195edce0SKirill A. Shutemov * shared=>private before the mapping becomes private.
1154195edce0SKirill A. Shutemov *
1155195edce0SKirill A. Shutemov * guest.enc_status_change_finish() converts the page from
1156195edce0SKirill A. Shutemov * private=>shared after the mapping becomes private.
1157195edce0SKirill A. Shutemov *
1158195edce0SKirill A. Shutemov * In both cases there is a temporary shared mapping to a private page,
1159195edce0SKirill A. Shutemov * which can result in a #VE. But, there is never a private mapping to
1160195edce0SKirill A. Shutemov * a shared page.
1161195edce0SKirill A. Shutemov */
1162195edce0SKirill A. Shutemov x86_platform.guest.enc_status_change_prepare = tdx_enc_status_change_prepare;
1163195edce0SKirill A. Shutemov x86_platform.guest.enc_status_change_finish = tdx_enc_status_change_finish;
1164195edce0SKirill A. Shutemov
11657dbde763SKirill A. Shutemov x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required;
11667dbde763SKirill A. Shutemov x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required;
11677dbde763SKirill A. Shutemov
1168859e63b7SKirill A. Shutemov x86_platform.guest.enc_kexec_begin = tdx_kexec_begin;
1169859e63b7SKirill A. Shutemov x86_platform.guest.enc_kexec_finish = tdx_kexec_finish;
1170859e63b7SKirill A. Shutemov
1171ff3cfcb0SThomas Gleixner /*
11729f98a4f4SVishal Annapurve * Avoid "sti;hlt" execution in TDX guests as HLT induces a #VE that
11739f98a4f4SVishal Annapurve * will enable interrupts before HLT TDCALL invocation if executed
11749f98a4f4SVishal Annapurve * in STI-shadow, possibly resulting in missed wakeup events.
11759f98a4f4SVishal Annapurve *
11769f98a4f4SVishal Annapurve * Modify all possible HLT execution paths to use TDX specific routines
11779f98a4f4SVishal Annapurve * that directly execute TDCALL and toggle the interrupt state as
11789f98a4f4SVishal Annapurve * needed after TDCALL completion. This also reduces HLT related #VEs
11799f98a4f4SVishal Annapurve * in addition to having a reliable halt logic execution.
11809f98a4f4SVishal Annapurve */
11819f98a4f4SVishal Annapurve pv_ops.irq.safe_halt = tdx_safe_halt;
11829f98a4f4SVishal Annapurve pv_ops.irq.halt = tdx_halt;
11839f98a4f4SVishal Annapurve
11849f98a4f4SVishal Annapurve /*
1185ff3cfcb0SThomas Gleixner * TDX intercepts the RDMSR to read the X2APIC ID in the parallel
1186ff3cfcb0SThomas Gleixner * bringup low level code. That raises #VE which cannot be handled
1187ff3cfcb0SThomas Gleixner * there.
1188ff3cfcb0SThomas Gleixner *
1189ff3cfcb0SThomas Gleixner * Intel-TDX has a secure RDMSR hypercall, but that needs to be
119054aa699eSBjorn Helgaas * implemented separately in the low level startup ASM code.
1191ff3cfcb0SThomas Gleixner * Until that is in place, disable parallel bringup for TDX.
1192ff3cfcb0SThomas Gleixner */
1193ff3cfcb0SThomas Gleixner x86_cpuinit.parallel_bringup = false;
1194ff3cfcb0SThomas Gleixner
1195564ea84cSKirill A. Shutemov tdx_announce();
119659bd54a8SKuppuswamy Sathyanarayanan }
1197