xref: /openbmc/linux/arch/x86/boot/compressed/sev.c (revision 5f211f4fc49622473667e6983bb57beab755f6f6)
1e759959fSBrijesh Singh // SPDX-License-Identifier: GPL-2.0
2e759959fSBrijesh Singh /*
3e759959fSBrijesh Singh  * AMD Encrypted Register State Support
4e759959fSBrijesh Singh  *
5e759959fSBrijesh Singh  * Author: Joerg Roedel <jroedel@suse.de>
6e759959fSBrijesh Singh  */
7e759959fSBrijesh Singh 
8e759959fSBrijesh Singh /*
9e759959fSBrijesh Singh  * misc.h needs to be first because it knows how to include the other kernel
10e759959fSBrijesh Singh  * headers in the pre-decompression code in a way that does not break
11e759959fSBrijesh Singh  * compilation.
12e759959fSBrijesh Singh  */
13e759959fSBrijesh Singh #include "misc.h"
14e759959fSBrijesh Singh 
15e759959fSBrijesh Singh #include <asm/pgtable_types.h>
16e759959fSBrijesh Singh #include <asm/sev.h>
17e759959fSBrijesh Singh #include <asm/trapnr.h>
18e759959fSBrijesh Singh #include <asm/trap_pf.h>
19e759959fSBrijesh Singh #include <asm/msr-index.h>
20e759959fSBrijesh Singh #include <asm/fpu/xcr.h>
21e759959fSBrijesh Singh #include <asm/ptrace.h>
22e759959fSBrijesh Singh #include <asm/svm.h>
23801baa69SMichael Roth #include <asm/cpuid.h>
24e759959fSBrijesh Singh 
25e759959fSBrijesh Singh #include "error.h"
26950d0055SMichael Roth #include "../msr.h"
27e759959fSBrijesh Singh 
28e759959fSBrijesh Singh struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
29e759959fSBrijesh Singh struct ghcb *boot_ghcb;
30e759959fSBrijesh Singh 
31e759959fSBrijesh Singh /*
32e759959fSBrijesh Singh  * Copy a version of this function here - insn-eval.c can't be used in
33e759959fSBrijesh Singh  * pre-decompression code.
34e759959fSBrijesh Singh  */
35e759959fSBrijesh Singh static bool insn_has_rep_prefix(struct insn *insn)
36e759959fSBrijesh Singh {
37e759959fSBrijesh Singh 	insn_byte_t p;
38e759959fSBrijesh Singh 	int i;
39e759959fSBrijesh Singh 
40e759959fSBrijesh Singh 	insn_get_prefixes(insn);
41e759959fSBrijesh Singh 
42e759959fSBrijesh Singh 	for_each_insn_prefix(insn, i, p) {
43e759959fSBrijesh Singh 		if (p == 0xf2 || p == 0xf3)
44e759959fSBrijesh Singh 			return true;
45e759959fSBrijesh Singh 	}
46e759959fSBrijesh Singh 
47e759959fSBrijesh Singh 	return false;
48e759959fSBrijesh Singh }
49e759959fSBrijesh Singh 
50e759959fSBrijesh Singh /*
51e759959fSBrijesh Singh  * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
52e759959fSBrijesh Singh  * doesn't use segments.
53e759959fSBrijesh Singh  */
54e759959fSBrijesh Singh static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
55e759959fSBrijesh Singh {
56e759959fSBrijesh Singh 	return 0UL;
57e759959fSBrijesh Singh }
58e759959fSBrijesh Singh 
59e759959fSBrijesh Singh static inline u64 sev_es_rd_ghcb_msr(void)
60e759959fSBrijesh Singh {
61950d0055SMichael Roth 	struct msr m;
62e759959fSBrijesh Singh 
63950d0055SMichael Roth 	boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
64e759959fSBrijesh Singh 
65950d0055SMichael Roth 	return m.q;
66e759959fSBrijesh Singh }
67e759959fSBrijesh Singh 
68e759959fSBrijesh Singh static inline void sev_es_wr_ghcb_msr(u64 val)
69e759959fSBrijesh Singh {
70950d0055SMichael Roth 	struct msr m;
71e759959fSBrijesh Singh 
72950d0055SMichael Roth 	m.q = val;
73950d0055SMichael Roth 	boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
74e759959fSBrijesh Singh }
75e759959fSBrijesh Singh 
76e759959fSBrijesh Singh static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
77e759959fSBrijesh Singh {
78e759959fSBrijesh Singh 	char buffer[MAX_INSN_SIZE];
79e759959fSBrijesh Singh 	int ret;
80e759959fSBrijesh Singh 
81e759959fSBrijesh Singh 	memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
82e759959fSBrijesh Singh 
83e759959fSBrijesh Singh 	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
84e759959fSBrijesh Singh 	if (ret < 0)
85e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
86e759959fSBrijesh Singh 
87e759959fSBrijesh Singh 	return ES_OK;
88e759959fSBrijesh Singh }
89e759959fSBrijesh Singh 
90e759959fSBrijesh Singh static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
91e759959fSBrijesh Singh 				   void *dst, char *buf, size_t size)
92e759959fSBrijesh Singh {
93e759959fSBrijesh Singh 	memcpy(dst, buf, size);
94e759959fSBrijesh Singh 
95e759959fSBrijesh Singh 	return ES_OK;
96e759959fSBrijesh Singh }
97e759959fSBrijesh Singh 
98e759959fSBrijesh Singh static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
99e759959fSBrijesh Singh 				  void *src, char *buf, size_t size)
100e759959fSBrijesh Singh {
101e759959fSBrijesh Singh 	memcpy(buf, src, size);
102e759959fSBrijesh Singh 
103e759959fSBrijesh Singh 	return ES_OK;
104e759959fSBrijesh Singh }
105e759959fSBrijesh Singh 
106e759959fSBrijesh Singh #undef __init
107e759959fSBrijesh Singh #undef __pa
108e759959fSBrijesh Singh #define __init
109e759959fSBrijesh Singh #define __pa(x)	((unsigned long)(x))
110e759959fSBrijesh Singh 
111e759959fSBrijesh Singh #define __BOOT_COMPRESSED
112e759959fSBrijesh Singh 
113e759959fSBrijesh Singh /* Basic instruction decoding support needed */
114e759959fSBrijesh Singh #include "../../lib/inat.c"
115e759959fSBrijesh Singh #include "../../lib/insn.c"
116e759959fSBrijesh Singh 
117e759959fSBrijesh Singh /* Include code for early handlers */
118e759959fSBrijesh Singh #include "../../kernel/sev-shared.c"
119e759959fSBrijesh Singh 
1204f9c403eSBrijesh Singh static inline bool sev_snp_enabled(void)
1214f9c403eSBrijesh Singh {
1224f9c403eSBrijesh Singh 	return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
1234f9c403eSBrijesh Singh }
1244f9c403eSBrijesh Singh 
1254f9c403eSBrijesh Singh static void __page_state_change(unsigned long paddr, enum psc_op op)
1264f9c403eSBrijesh Singh {
1274f9c403eSBrijesh Singh 	u64 val;
1284f9c403eSBrijesh Singh 
1294f9c403eSBrijesh Singh 	if (!sev_snp_enabled())
1304f9c403eSBrijesh Singh 		return;
1314f9c403eSBrijesh Singh 
1324f9c403eSBrijesh Singh 	/*
1334f9c403eSBrijesh Singh 	 * If private -> shared then invalidate the page before requesting the
1344f9c403eSBrijesh Singh 	 * state change in the RMP table.
1354f9c403eSBrijesh Singh 	 */
1364f9c403eSBrijesh Singh 	if (op == SNP_PAGE_STATE_SHARED && pvalidate(paddr, RMP_PG_SIZE_4K, 0))
1374f9c403eSBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
1384f9c403eSBrijesh Singh 
1394f9c403eSBrijesh Singh 	/* Issue VMGEXIT to change the page state in RMP table. */
1404f9c403eSBrijesh Singh 	sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
1414f9c403eSBrijesh Singh 	VMGEXIT();
1424f9c403eSBrijesh Singh 
1434f9c403eSBrijesh Singh 	/* Read the response of the VMGEXIT. */
1444f9c403eSBrijesh Singh 	val = sev_es_rd_ghcb_msr();
1454f9c403eSBrijesh Singh 	if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
1464f9c403eSBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
1474f9c403eSBrijesh Singh 
1484f9c403eSBrijesh Singh 	/*
1494f9c403eSBrijesh Singh 	 * Now that page state is changed in the RMP table, validate it so that it is
1504f9c403eSBrijesh Singh 	 * consistent with the RMP entry.
1514f9c403eSBrijesh Singh 	 */
1524f9c403eSBrijesh Singh 	if (op == SNP_PAGE_STATE_PRIVATE && pvalidate(paddr, RMP_PG_SIZE_4K, 1))
1534f9c403eSBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
1544f9c403eSBrijesh Singh }
1554f9c403eSBrijesh Singh 
1564f9c403eSBrijesh Singh void snp_set_page_private(unsigned long paddr)
1574f9c403eSBrijesh Singh {
1584f9c403eSBrijesh Singh 	__page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
1594f9c403eSBrijesh Singh }
1604f9c403eSBrijesh Singh 
1614f9c403eSBrijesh Singh void snp_set_page_shared(unsigned long paddr)
1624f9c403eSBrijesh Singh {
1634f9c403eSBrijesh Singh 	__page_state_change(paddr, SNP_PAGE_STATE_SHARED);
1644f9c403eSBrijesh Singh }
1654f9c403eSBrijesh Singh 
166cbd3d4f7SBrijesh Singh static bool early_setup_ghcb(void)
167e759959fSBrijesh Singh {
168e759959fSBrijesh Singh 	if (set_page_decrypted((unsigned long)&boot_ghcb_page))
169e759959fSBrijesh Singh 		return false;
170e759959fSBrijesh Singh 
171e759959fSBrijesh Singh 	/* Page is now mapped decrypted, clear it */
172e759959fSBrijesh Singh 	memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
173e759959fSBrijesh Singh 
174e759959fSBrijesh Singh 	boot_ghcb = &boot_ghcb_page;
175e759959fSBrijesh Singh 
176e759959fSBrijesh Singh 	/* Initialize lookup tables for the instruction decoder */
177e759959fSBrijesh Singh 	inat_init_tables();
178e759959fSBrijesh Singh 
17987294bdbSBrijesh Singh 	/* SNP guest requires the GHCB GPA must be registered */
18087294bdbSBrijesh Singh 	if (sev_snp_enabled())
18187294bdbSBrijesh Singh 		snp_register_ghcb_early(__pa(&boot_ghcb_page));
18287294bdbSBrijesh Singh 
183e759959fSBrijesh Singh 	return true;
184e759959fSBrijesh Singh }
185e759959fSBrijesh Singh 
186e759959fSBrijesh Singh void sev_es_shutdown_ghcb(void)
187e759959fSBrijesh Singh {
188e759959fSBrijesh Singh 	if (!boot_ghcb)
189e759959fSBrijesh Singh 		return;
190e759959fSBrijesh Singh 
191e759959fSBrijesh Singh 	if (!sev_es_check_cpu_features())
192e759959fSBrijesh Singh 		error("SEV-ES CPU Features missing.");
193e759959fSBrijesh Singh 
194e759959fSBrijesh Singh 	/*
195e759959fSBrijesh Singh 	 * GHCB Page must be flushed from the cache and mapped encrypted again.
196e759959fSBrijesh Singh 	 * Otherwise the running kernel will see strange cache effects when
197e759959fSBrijesh Singh 	 * trying to use that page.
198e759959fSBrijesh Singh 	 */
199e759959fSBrijesh Singh 	if (set_page_encrypted((unsigned long)&boot_ghcb_page))
200e759959fSBrijesh Singh 		error("Can't map GHCB page encrypted");
201e759959fSBrijesh Singh 
202e759959fSBrijesh Singh 	/*
203e759959fSBrijesh Singh 	 * GHCB page is mapped encrypted again and flushed from the cache.
204e759959fSBrijesh Singh 	 * Mark it non-present now to catch bugs when #VC exceptions trigger
205e759959fSBrijesh Singh 	 * after this point.
206e759959fSBrijesh Singh 	 */
207e759959fSBrijesh Singh 	if (set_page_non_present((unsigned long)&boot_ghcb_page))
208e759959fSBrijesh Singh 		error("Can't unmap GHCB page");
209e759959fSBrijesh Singh }
210e759959fSBrijesh Singh 
211e759959fSBrijesh Singh bool sev_es_check_ghcb_fault(unsigned long address)
212e759959fSBrijesh Singh {
213e759959fSBrijesh Singh 	/* Check whether the fault was on the GHCB page */
214e759959fSBrijesh Singh 	return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
215e759959fSBrijesh Singh }
216e759959fSBrijesh Singh 
217e759959fSBrijesh Singh void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
218e759959fSBrijesh Singh {
219e759959fSBrijesh Singh 	struct es_em_ctxt ctxt;
220e759959fSBrijesh Singh 	enum es_result result;
221e759959fSBrijesh Singh 
222cbd3d4f7SBrijesh Singh 	if (!boot_ghcb && !early_setup_ghcb())
2236c0f74d6SBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
224e759959fSBrijesh Singh 
225e759959fSBrijesh Singh 	vc_ghcb_invalidate(boot_ghcb);
226e759959fSBrijesh Singh 	result = vc_init_em_ctxt(&ctxt, regs, exit_code);
227e759959fSBrijesh Singh 	if (result != ES_OK)
228e759959fSBrijesh Singh 		goto finish;
229e759959fSBrijesh Singh 
230e759959fSBrijesh Singh 	switch (exit_code) {
231e759959fSBrijesh Singh 	case SVM_EXIT_RDTSC:
232e759959fSBrijesh Singh 	case SVM_EXIT_RDTSCP:
233e759959fSBrijesh Singh 		result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
234e759959fSBrijesh Singh 		break;
235e759959fSBrijesh Singh 	case SVM_EXIT_IOIO:
236e759959fSBrijesh Singh 		result = vc_handle_ioio(boot_ghcb, &ctxt);
237e759959fSBrijesh Singh 		break;
238e759959fSBrijesh Singh 	case SVM_EXIT_CPUID:
239e759959fSBrijesh Singh 		result = vc_handle_cpuid(boot_ghcb, &ctxt);
240e759959fSBrijesh Singh 		break;
241e759959fSBrijesh Singh 	default:
242e759959fSBrijesh Singh 		result = ES_UNSUPPORTED;
243e759959fSBrijesh Singh 		break;
244e759959fSBrijesh Singh 	}
245e759959fSBrijesh Singh 
246e759959fSBrijesh Singh finish:
247e759959fSBrijesh Singh 	if (result == ES_OK)
248e759959fSBrijesh Singh 		vc_finish_insn(&ctxt);
249e759959fSBrijesh Singh 	else if (result != ES_RETRY)
2506c0f74d6SBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
251e759959fSBrijesh Singh }
252ec1c66afSMichael Roth 
25381cc3df9SBrijesh Singh static void enforce_vmpl0(void)
25481cc3df9SBrijesh Singh {
25581cc3df9SBrijesh Singh 	u64 attrs;
25681cc3df9SBrijesh Singh 	int err;
25781cc3df9SBrijesh Singh 
25881cc3df9SBrijesh Singh 	/*
25981cc3df9SBrijesh Singh 	 * RMPADJUST modifies RMP permissions of a lesser-privileged (numerically
26081cc3df9SBrijesh Singh 	 * higher) privilege level. Here, clear the VMPL1 permission mask of the
26181cc3df9SBrijesh Singh 	 * GHCB page. If the guest is not running at VMPL0, this will fail.
26281cc3df9SBrijesh Singh 	 *
26381cc3df9SBrijesh Singh 	 * If the guest is running at VMPL0, it will succeed. Even if that operation
26481cc3df9SBrijesh Singh 	 * modifies permission bits, it is still ok to do so currently because Linux
26581cc3df9SBrijesh Singh 	 * SNP guests are supported only on VMPL0 so VMPL1 or higher permission masks
26681cc3df9SBrijesh Singh 	 * changing is a don't-care.
26781cc3df9SBrijesh Singh 	 */
26881cc3df9SBrijesh Singh 	attrs = 1;
26981cc3df9SBrijesh Singh 	if (rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, attrs))
27081cc3df9SBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
27181cc3df9SBrijesh Singh }
27281cc3df9SBrijesh Singh 
273ec1c66afSMichael Roth void sev_enable(struct boot_params *bp)
274ec1c66afSMichael Roth {
275ec1c66afSMichael Roth 	unsigned int eax, ebx, ecx, edx;
276ec1c66afSMichael Roth 	struct msr m;
277c01fce9cSMichael Roth 	bool snp;
278c01fce9cSMichael Roth 
279c01fce9cSMichael Roth 	/*
280c01fce9cSMichael Roth 	 * Setup/preliminary detection of SNP. This will be sanity-checked
281c01fce9cSMichael Roth 	 * against CPUID/MSR values later.
282c01fce9cSMichael Roth 	 */
283c01fce9cSMichael Roth 	snp = snp_init(bp);
284ec1c66afSMichael Roth 
285ec1c66afSMichael Roth 	/* Check for the SME/SEV support leaf */
286ec1c66afSMichael Roth 	eax = 0x80000000;
287ec1c66afSMichael Roth 	ecx = 0;
288ec1c66afSMichael Roth 	native_cpuid(&eax, &ebx, &ecx, &edx);
289ec1c66afSMichael Roth 	if (eax < 0x8000001f)
290ec1c66afSMichael Roth 		return;
291ec1c66afSMichael Roth 
292ec1c66afSMichael Roth 	/*
293ec1c66afSMichael Roth 	 * Check for the SME/SEV feature:
294ec1c66afSMichael Roth 	 *   CPUID Fn8000_001F[EAX]
295ec1c66afSMichael Roth 	 *   - Bit 0 - Secure Memory Encryption support
296ec1c66afSMichael Roth 	 *   - Bit 1 - Secure Encrypted Virtualization support
297ec1c66afSMichael Roth 	 *   CPUID Fn8000_001F[EBX]
298ec1c66afSMichael Roth 	 *   - Bits 5:0 - Pagetable bit position used to indicate encryption
299ec1c66afSMichael Roth 	 */
300ec1c66afSMichael Roth 	eax = 0x8000001f;
301ec1c66afSMichael Roth 	ecx = 0;
302ec1c66afSMichael Roth 	native_cpuid(&eax, &ebx, &ecx, &edx);
303ec1c66afSMichael Roth 	/* Check whether SEV is supported */
304c01fce9cSMichael Roth 	if (!(eax & BIT(1))) {
305c01fce9cSMichael Roth 		if (snp)
306c01fce9cSMichael Roth 			error("SEV-SNP support indicated by CC blob, but not CPUID.");
307ec1c66afSMichael Roth 		return;
308c01fce9cSMichael Roth 	}
309ec1c66afSMichael Roth 
310ec1c66afSMichael Roth 	/* Set the SME mask if this is an SEV guest. */
311ec1c66afSMichael Roth 	boot_rdmsr(MSR_AMD64_SEV, &m);
312ec1c66afSMichael Roth 	sev_status = m.q;
313ec1c66afSMichael Roth 	if (!(sev_status & MSR_AMD64_SEV_ENABLED))
314ec1c66afSMichael Roth 		return;
315ec1c66afSMichael Roth 
316cbd3d4f7SBrijesh Singh 	/* Negotiate the GHCB protocol version. */
317cbd3d4f7SBrijesh Singh 	if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
318cbd3d4f7SBrijesh Singh 		if (!sev_es_negotiate_protocol())
319cbd3d4f7SBrijesh Singh 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
320cbd3d4f7SBrijesh Singh 	}
321cbd3d4f7SBrijesh Singh 
322cbd3d4f7SBrijesh Singh 	/*
323cbd3d4f7SBrijesh Singh 	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
324cbd3d4f7SBrijesh Singh 	 * features.
325cbd3d4f7SBrijesh Singh 	 */
32681cc3df9SBrijesh Singh 	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
32781cc3df9SBrijesh Singh 		if (!(get_hv_features() & GHCB_HV_FT_SNP))
328cbd3d4f7SBrijesh Singh 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
329cbd3d4f7SBrijesh Singh 
33081cc3df9SBrijesh Singh 		enforce_vmpl0();
33181cc3df9SBrijesh Singh 	}
33281cc3df9SBrijesh Singh 
333c01fce9cSMichael Roth 	if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
334c01fce9cSMichael Roth 		error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
335c01fce9cSMichael Roth 
336ec1c66afSMichael Roth 	sme_me_mask = BIT_ULL(ebx & 0x3f);
337ec1c66afSMichael Roth }
338c01fce9cSMichael Roth 
339c01fce9cSMichael Roth /* Search for Confidential Computing blob in the EFI config table. */
340c01fce9cSMichael Roth static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
341c01fce9cSMichael Roth {
342c01fce9cSMichael Roth 	unsigned long cfg_table_pa;
343c01fce9cSMichael Roth 	unsigned int cfg_table_len;
344c01fce9cSMichael Roth 	int ret;
345c01fce9cSMichael Roth 
346c01fce9cSMichael Roth 	ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
347c01fce9cSMichael Roth 	if (ret)
348c01fce9cSMichael Roth 		return NULL;
349c01fce9cSMichael Roth 
350c01fce9cSMichael Roth 	return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
351c01fce9cSMichael Roth 								cfg_table_len,
352c01fce9cSMichael Roth 								EFI_CC_BLOB_GUID);
353c01fce9cSMichael Roth }
354c01fce9cSMichael Roth 
355c01fce9cSMichael Roth struct cc_setup_data {
356c01fce9cSMichael Roth 	struct setup_data header;
357c01fce9cSMichael Roth 	u32 cc_blob_address;
358c01fce9cSMichael Roth };
359c01fce9cSMichael Roth 
360c01fce9cSMichael Roth /*
361c01fce9cSMichael Roth  * Search for a Confidential Computing blob passed in as a setup_data entry
362c01fce9cSMichael Roth  * via the Linux Boot Protocol.
363c01fce9cSMichael Roth  */
364c01fce9cSMichael Roth static struct cc_blob_sev_info *find_cc_blob_setup_data(struct boot_params *bp)
365c01fce9cSMichael Roth {
366c01fce9cSMichael Roth 	struct cc_setup_data *sd = NULL;
367c01fce9cSMichael Roth 	struct setup_data *hdr;
368c01fce9cSMichael Roth 
369c01fce9cSMichael Roth 	hdr = (struct setup_data *)bp->hdr.setup_data;
370c01fce9cSMichael Roth 
371c01fce9cSMichael Roth 	while (hdr) {
372c01fce9cSMichael Roth 		if (hdr->type == SETUP_CC_BLOB) {
373c01fce9cSMichael Roth 			sd = (struct cc_setup_data *)hdr;
374c01fce9cSMichael Roth 			return (struct cc_blob_sev_info *)(unsigned long)sd->cc_blob_address;
375c01fce9cSMichael Roth 		}
376c01fce9cSMichael Roth 		hdr = (struct setup_data *)hdr->next;
377c01fce9cSMichael Roth 	}
378c01fce9cSMichael Roth 
379c01fce9cSMichael Roth 	return NULL;
380c01fce9cSMichael Roth }
381c01fce9cSMichael Roth 
382c01fce9cSMichael Roth /*
383c01fce9cSMichael Roth  * Initial set up of SNP relies on information provided by the
384c01fce9cSMichael Roth  * Confidential Computing blob, which can be passed to the boot kernel
385c01fce9cSMichael Roth  * by firmware/bootloader in the following ways:
386c01fce9cSMichael Roth  *
387c01fce9cSMichael Roth  * - via an entry in the EFI config table
388c01fce9cSMichael Roth  * - via a setup_data structure, as defined by the Linux Boot Protocol
389c01fce9cSMichael Roth  *
390c01fce9cSMichael Roth  * Scan for the blob in that order.
391c01fce9cSMichael Roth  */
392c01fce9cSMichael Roth static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
393c01fce9cSMichael Roth {
394c01fce9cSMichael Roth 	struct cc_blob_sev_info *cc_info;
395c01fce9cSMichael Roth 
396c01fce9cSMichael Roth 	cc_info = find_cc_blob_efi(bp);
397c01fce9cSMichael Roth 	if (cc_info)
398c01fce9cSMichael Roth 		goto found_cc_info;
399c01fce9cSMichael Roth 
400c01fce9cSMichael Roth 	cc_info = find_cc_blob_setup_data(bp);
401c01fce9cSMichael Roth 	if (!cc_info)
402c01fce9cSMichael Roth 		return NULL;
403c01fce9cSMichael Roth 
404c01fce9cSMichael Roth found_cc_info:
405c01fce9cSMichael Roth 	if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
406c01fce9cSMichael Roth 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
407c01fce9cSMichael Roth 
408c01fce9cSMichael Roth 	return cc_info;
409c01fce9cSMichael Roth }
410c01fce9cSMichael Roth 
411c01fce9cSMichael Roth /*
412*5f211f4fSMichael Roth  * Initialize the kernel's copy of the SNP CPUID table, and set up the
413*5f211f4fSMichael Roth  * pointer that will be used to access it.
414*5f211f4fSMichael Roth  *
415*5f211f4fSMichael Roth  * Maintaining a direct mapping of the SNP CPUID table used by firmware would
416*5f211f4fSMichael Roth  * be possible as an alternative, but the approach is brittle since the
417*5f211f4fSMichael Roth  * mapping needs to be updated in sync with all the changes to virtual memory
418*5f211f4fSMichael Roth  * layout and related mapping facilities throughout the boot process.
419*5f211f4fSMichael Roth  */
420*5f211f4fSMichael Roth static void setup_cpuid_table(const struct cc_blob_sev_info *cc_info)
421*5f211f4fSMichael Roth {
422*5f211f4fSMichael Roth 	const struct snp_cpuid_table *cpuid_table_fw, *cpuid_table;
423*5f211f4fSMichael Roth 	int i;
424*5f211f4fSMichael Roth 
425*5f211f4fSMichael Roth 	if (!cc_info || !cc_info->cpuid_phys || cc_info->cpuid_len < PAGE_SIZE)
426*5f211f4fSMichael Roth 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
427*5f211f4fSMichael Roth 
428*5f211f4fSMichael Roth 	cpuid_table_fw = (const struct snp_cpuid_table *)cc_info->cpuid_phys;
429*5f211f4fSMichael Roth 	if (!cpuid_table_fw->count || cpuid_table_fw->count > SNP_CPUID_COUNT_MAX)
430*5f211f4fSMichael Roth 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
431*5f211f4fSMichael Roth 
432*5f211f4fSMichael Roth 	cpuid_table = snp_cpuid_get_table();
433*5f211f4fSMichael Roth 	memcpy((void *)cpuid_table, cpuid_table_fw, sizeof(*cpuid_table));
434*5f211f4fSMichael Roth 
435*5f211f4fSMichael Roth 	/* Initialize CPUID ranges for range-checking. */
436*5f211f4fSMichael Roth 	for (i = 0; i < cpuid_table->count; i++) {
437*5f211f4fSMichael Roth 		const struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
438*5f211f4fSMichael Roth 
439*5f211f4fSMichael Roth 		if (fn->eax_in == 0x0)
440*5f211f4fSMichael Roth 			cpuid_std_range_max = fn->eax;
441*5f211f4fSMichael Roth 		else if (fn->eax_in == 0x40000000)
442*5f211f4fSMichael Roth 			cpuid_hyp_range_max = fn->eax;
443*5f211f4fSMichael Roth 		else if (fn->eax_in == 0x80000000)
444*5f211f4fSMichael Roth 			cpuid_ext_range_max = fn->eax;
445*5f211f4fSMichael Roth 	}
446*5f211f4fSMichael Roth }
447*5f211f4fSMichael Roth 
448*5f211f4fSMichael Roth /*
449c01fce9cSMichael Roth  * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
450c01fce9cSMichael Roth  * will verify the SNP CPUID/MSR bits.
451c01fce9cSMichael Roth  */
452c01fce9cSMichael Roth bool snp_init(struct boot_params *bp)
453c01fce9cSMichael Roth {
454c01fce9cSMichael Roth 	struct cc_blob_sev_info *cc_info;
455c01fce9cSMichael Roth 
456c01fce9cSMichael Roth 	if (!bp)
457c01fce9cSMichael Roth 		return false;
458c01fce9cSMichael Roth 
459c01fce9cSMichael Roth 	cc_info = find_cc_blob(bp);
460c01fce9cSMichael Roth 	if (!cc_info)
461c01fce9cSMichael Roth 		return false;
462c01fce9cSMichael Roth 
463c01fce9cSMichael Roth 	/*
464*5f211f4fSMichael Roth 	 * If a SNP-specific Confidential Computing blob is present, then
465*5f211f4fSMichael Roth 	 * firmware/bootloader have indicated SNP support. Verifying this
466*5f211f4fSMichael Roth 	 * involves CPUID checks which will be more reliable if the SNP
467*5f211f4fSMichael Roth 	 * CPUID table is used. See comments over snp_setup_cpuid_table() for
468*5f211f4fSMichael Roth 	 * more details.
469*5f211f4fSMichael Roth 	 */
470*5f211f4fSMichael Roth 	setup_cpuid_table(cc_info);
471*5f211f4fSMichael Roth 
472*5f211f4fSMichael Roth 	/*
473c01fce9cSMichael Roth 	 * Pass run-time kernel a pointer to CC info via boot_params so EFI
474c01fce9cSMichael Roth 	 * config table doesn't need to be searched again during early startup
475c01fce9cSMichael Roth 	 * phase.
476c01fce9cSMichael Roth 	 */
477c01fce9cSMichael Roth 	bp->cc_blob_address = (u32)(unsigned long)cc_info;
478c01fce9cSMichael Roth 
479c01fce9cSMichael Roth 	return true;
480c01fce9cSMichael Roth }
481