• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AMD Encrypted Register State Support
4  *
5  * Author: Joerg Roedel <jroedel@suse.de>
6  */
7 
8 /*
9  * misc.h needs to be first because it knows how to include the other kernel
10  * headers in the pre-decompression code in a way that does not break
11  * compilation.
12  */
13 #include "misc.h"
14 
15 #include <asm/bootparam.h>
16 #include <asm/pgtable_types.h>
17 #include <asm/sev.h>
18 #include <asm/trapnr.h>
19 #include <asm/trap_pf.h>
20 #include <asm/msr-index.h>
21 #include <asm/fpu/xcr.h>
22 #include <asm/ptrace.h>
23 #include <asm/svm.h>
24 #include <asm/cpuid.h>
25 
26 #include "error.h"
27 #include "../msr.h"
28 
29 static struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
30 struct ghcb *boot_ghcb;
31 
32 /*
33  * Copy a version of this function here - insn-eval.c can't be used in
34  * pre-decompression code.
35  */
insn_has_rep_prefix(struct insn * insn)36 static bool insn_has_rep_prefix(struct insn *insn)
37 {
38 	insn_byte_t p;
39 	int i;
40 
41 	insn_get_prefixes(insn);
42 
43 	for_each_insn_prefix(insn, i, p) {
44 		if (p == 0xf2 || p == 0xf3)
45 			return true;
46 	}
47 
48 	return false;
49 }
50 
51 /*
52  * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
53  * doesn't use segments.
54  */
insn_get_seg_base(struct pt_regs * regs,int seg_reg_idx)55 static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
56 {
57 	return 0UL;
58 }
59 
sev_es_rd_ghcb_msr(void)60 static inline u64 sev_es_rd_ghcb_msr(void)
61 {
62 	struct msr m;
63 
64 	boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
65 
66 	return m.q;
67 }
68 
sev_es_wr_ghcb_msr(u64 val)69 static inline void sev_es_wr_ghcb_msr(u64 val)
70 {
71 	struct msr m;
72 
73 	m.q = val;
74 	boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
75 }
76 
vc_decode_insn(struct es_em_ctxt * ctxt)77 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
78 {
79 	char buffer[MAX_INSN_SIZE];
80 	int ret;
81 
82 	memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
83 
84 	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
85 	if (ret < 0)
86 		return ES_DECODE_FAILED;
87 
88 	return ES_OK;
89 }
90 
vc_write_mem(struct es_em_ctxt * ctxt,void * dst,char * buf,size_t size)91 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
92 				   void *dst, char *buf, size_t size)
93 {
94 	memcpy(dst, buf, size);
95 
96 	return ES_OK;
97 }
98 
vc_read_mem(struct es_em_ctxt * ctxt,void * src,char * buf,size_t size)99 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
100 				  void *src, char *buf, size_t size)
101 {
102 	memcpy(buf, src, size);
103 
104 	return ES_OK;
105 }
106 
vc_ioio_check(struct es_em_ctxt * ctxt,u16 port,size_t size)107 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
108 {
109 	return ES_OK;
110 }
111 
fault_in_kernel_space(unsigned long address)112 static bool fault_in_kernel_space(unsigned long address)
113 {
114 	return false;
115 }
116 
117 #undef __init
118 #define __init
119 
120 #undef __head
121 #define __head
122 
123 #define __BOOT_COMPRESSED
124 
125 /* Basic instruction decoding support needed */
126 #include "../../lib/inat.c"
127 #include "../../lib/insn.c"
128 
129 /* Include code for early handlers */
130 #include "../../coco/sev/shared.c"
131 
svsm_get_caa(void)132 static struct svsm_ca *svsm_get_caa(void)
133 {
134 	return boot_svsm_caa;
135 }
136 
svsm_get_caa_pa(void)137 static u64 svsm_get_caa_pa(void)
138 {
139 	return boot_svsm_caa_pa;
140 }
141 
svsm_perform_call_protocol(struct svsm_call * call)142 static int svsm_perform_call_protocol(struct svsm_call *call)
143 {
144 	struct ghcb *ghcb;
145 	int ret;
146 
147 	if (boot_ghcb)
148 		ghcb = boot_ghcb;
149 	else
150 		ghcb = NULL;
151 
152 	do {
153 		ret = ghcb ? svsm_perform_ghcb_protocol(ghcb, call)
154 			   : svsm_perform_msr_protocol(call);
155 	} while (ret == -EAGAIN);
156 
157 	return ret;
158 }
159 
sev_snp_enabled(void)160 bool sev_snp_enabled(void)
161 {
162 	return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
163 }
164 
__page_state_change(unsigned long paddr,enum psc_op op)165 static void __page_state_change(unsigned long paddr, enum psc_op op)
166 {
167 	u64 val, msr;
168 
169 	/*
170 	 * If private -> shared then invalidate the page before requesting the
171 	 * state change in the RMP table.
172 	 */
173 	if (op == SNP_PAGE_STATE_SHARED)
174 		pvalidate_4k_page(paddr, paddr, false);
175 
176 	/* Save the current GHCB MSR value */
177 	msr = sev_es_rd_ghcb_msr();
178 
179 	/* Issue VMGEXIT to change the page state in RMP table. */
180 	sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
181 	VMGEXIT();
182 
183 	/* Read the response of the VMGEXIT. */
184 	val = sev_es_rd_ghcb_msr();
185 	if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
186 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
187 
188 	/* Restore the GHCB MSR value */
189 	sev_es_wr_ghcb_msr(msr);
190 
191 	/*
192 	 * Now that page state is changed in the RMP table, validate it so that it is
193 	 * consistent with the RMP entry.
194 	 */
195 	if (op == SNP_PAGE_STATE_PRIVATE)
196 		pvalidate_4k_page(paddr, paddr, true);
197 }
198 
snp_set_page_private(unsigned long paddr)199 void snp_set_page_private(unsigned long paddr)
200 {
201 	if (!sev_snp_enabled())
202 		return;
203 
204 	__page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
205 }
206 
snp_set_page_shared(unsigned long paddr)207 void snp_set_page_shared(unsigned long paddr)
208 {
209 	if (!sev_snp_enabled())
210 		return;
211 
212 	__page_state_change(paddr, SNP_PAGE_STATE_SHARED);
213 }
214 
early_setup_ghcb(void)215 static bool early_setup_ghcb(void)
216 {
217 	if (set_page_decrypted((unsigned long)&boot_ghcb_page))
218 		return false;
219 
220 	/* Page is now mapped decrypted, clear it */
221 	memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
222 
223 	boot_ghcb = &boot_ghcb_page;
224 
225 	/* Initialize lookup tables for the instruction decoder */
226 	inat_init_tables();
227 
228 	/* SNP guest requires the GHCB GPA must be registered */
229 	if (sev_snp_enabled())
230 		snp_register_ghcb_early(__pa(&boot_ghcb_page));
231 
232 	return true;
233 }
234 
snp_accept_memory(phys_addr_t start,phys_addr_t end)235 void snp_accept_memory(phys_addr_t start, phys_addr_t end)
236 {
237 	for (phys_addr_t pa = start; pa < end; pa += PAGE_SIZE)
238 		__page_state_change(pa, SNP_PAGE_STATE_PRIVATE);
239 }
240 
sev_es_shutdown_ghcb(void)241 void sev_es_shutdown_ghcb(void)
242 {
243 	if (!boot_ghcb)
244 		return;
245 
246 	if (!sev_es_check_cpu_features())
247 		error("SEV-ES CPU Features missing.");
248 
249 	/*
250 	 * This denotes whether to use the GHCB MSR protocol or the GHCB
251 	 * shared page to perform a GHCB request. Since the GHCB page is
252 	 * being changed to encrypted, it can't be used to perform GHCB
253 	 * requests. Clear the boot_ghcb variable so that the GHCB MSR
254 	 * protocol is used to change the GHCB page over to an encrypted
255 	 * page.
256 	 */
257 	boot_ghcb = NULL;
258 
259 	/*
260 	 * GHCB Page must be flushed from the cache and mapped encrypted again.
261 	 * Otherwise the running kernel will see strange cache effects when
262 	 * trying to use that page.
263 	 */
264 	if (set_page_encrypted((unsigned long)&boot_ghcb_page))
265 		error("Can't map GHCB page encrypted");
266 
267 	/*
268 	 * GHCB page is mapped encrypted again and flushed from the cache.
269 	 * Mark it non-present now to catch bugs when #VC exceptions trigger
270 	 * after this point.
271 	 */
272 	if (set_page_non_present((unsigned long)&boot_ghcb_page))
273 		error("Can't unmap GHCB page");
274 }
275 
sev_es_ghcb_terminate(struct ghcb * ghcb,unsigned int set,unsigned int reason,u64 exit_info_2)276 static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,
277 					     unsigned int reason, u64 exit_info_2)
278 {
279 	u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);
280 
281 	vc_ghcb_invalidate(ghcb);
282 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);
283 	ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
284 	ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
285 
286 	sev_es_wr_ghcb_msr(__pa(ghcb));
287 	VMGEXIT();
288 
289 	while (true)
290 		asm volatile("hlt\n" : : : "memory");
291 }
292 
sev_es_check_ghcb_fault(unsigned long address)293 bool sev_es_check_ghcb_fault(unsigned long address)
294 {
295 	/* Check whether the fault was on the GHCB page */
296 	return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
297 }
298 
do_boot_stage2_vc(struct pt_regs * regs,unsigned long exit_code)299 void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
300 {
301 	struct es_em_ctxt ctxt;
302 	enum es_result result;
303 
304 	if (!boot_ghcb && !early_setup_ghcb())
305 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
306 
307 	vc_ghcb_invalidate(boot_ghcb);
308 	result = vc_init_em_ctxt(&ctxt, regs, exit_code);
309 	if (result != ES_OK)
310 		goto finish;
311 
312 	result = vc_check_opcode_bytes(&ctxt, exit_code);
313 	if (result != ES_OK)
314 		goto finish;
315 
316 	switch (exit_code) {
317 	case SVM_EXIT_RDTSC:
318 	case SVM_EXIT_RDTSCP:
319 		result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
320 		break;
321 	case SVM_EXIT_IOIO:
322 		result = vc_handle_ioio(boot_ghcb, &ctxt);
323 		break;
324 	case SVM_EXIT_CPUID:
325 		result = vc_handle_cpuid(boot_ghcb, &ctxt);
326 		break;
327 	default:
328 		result = ES_UNSUPPORTED;
329 		break;
330 	}
331 
332 finish:
333 	if (result == ES_OK)
334 		vc_finish_insn(&ctxt);
335 	else if (result != ES_RETRY)
336 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
337 }
338 
339 /*
340  * SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need
341  * guest side implementation for proper functioning of the guest. If any
342  * of these features are enabled in the hypervisor but are lacking guest
343  * side implementation, the behavior of the guest will be undefined. The
344  * guest could fail in non-obvious way making it difficult to debug.
345  *
346  * As the behavior of reserved feature bits is unknown to be on the
347  * safe side add them to the required features mask.
348  */
349 #define SNP_FEATURES_IMPL_REQ	(MSR_AMD64_SNP_VTOM |			\
350 				 MSR_AMD64_SNP_REFLECT_VC |		\
351 				 MSR_AMD64_SNP_RESTRICTED_INJ |		\
352 				 MSR_AMD64_SNP_ALT_INJ |		\
353 				 MSR_AMD64_SNP_DEBUG_SWAP |		\
354 				 MSR_AMD64_SNP_VMPL_SSS |		\
355 				 MSR_AMD64_SNP_SECURE_TSC |		\
356 				 MSR_AMD64_SNP_VMGEXIT_PARAM |		\
357 				 MSR_AMD64_SNP_VMSA_REG_PROT |		\
358 				 MSR_AMD64_SNP_RESERVED_BIT13 |		\
359 				 MSR_AMD64_SNP_RESERVED_BIT15 |		\
360 				 MSR_AMD64_SNP_RESERVED_MASK)
361 
362 /*
363  * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
364  * by the guest kernel. As and when a new feature is implemented in the
365  * guest kernel, a corresponding bit should be added to the mask.
366  */
367 #define SNP_FEATURES_PRESENT	MSR_AMD64_SNP_DEBUG_SWAP
368 
snp_get_unsupported_features(u64 status)369 u64 snp_get_unsupported_features(u64 status)
370 {
371 	if (!(status & MSR_AMD64_SEV_SNP_ENABLED))
372 		return 0;
373 
374 	return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;
375 }
376 
snp_check_features(void)377 void snp_check_features(void)
378 {
379 	u64 unsupported;
380 
381 	/*
382 	 * Terminate the boot if hypervisor has enabled any feature lacking
383 	 * guest side implementation. Pass on the unsupported features mask through
384 	 * EXIT_INFO_2 of the GHCB protocol so that those features can be reported
385 	 * as part of the guest boot failure.
386 	 */
387 	unsupported = snp_get_unsupported_features(sev_status);
388 	if (unsupported) {
389 		if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))
390 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
391 
392 		sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,
393 				      GHCB_SNP_UNSUPPORTED, unsupported);
394 	}
395 }
396 
397 /* Search for Confidential Computing blob in the EFI config table. */
find_cc_blob_efi(struct boot_params * bp)398 static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
399 {
400 	unsigned long cfg_table_pa;
401 	unsigned int cfg_table_len;
402 	int ret;
403 
404 	ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
405 	if (ret)
406 		return NULL;
407 
408 	return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
409 								cfg_table_len,
410 								EFI_CC_BLOB_GUID);
411 }
412 
413 /*
414  * Initial set up of SNP relies on information provided by the
415  * Confidential Computing blob, which can be passed to the boot kernel
416  * by firmware/bootloader in the following ways:
417  *
418  * - via an entry in the EFI config table
419  * - via a setup_data structure, as defined by the Linux Boot Protocol
420  *
421  * Scan for the blob in that order.
422  */
find_cc_blob(struct boot_params * bp)423 static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
424 {
425 	struct cc_blob_sev_info *cc_info;
426 
427 	cc_info = find_cc_blob_efi(bp);
428 	if (cc_info)
429 		goto found_cc_info;
430 
431 	cc_info = find_cc_blob_setup_data(bp);
432 	if (!cc_info)
433 		return NULL;
434 
435 found_cc_info:
436 	if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
437 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
438 
439 	return cc_info;
440 }
441 
442 /*
443  * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
444  * will verify the SNP CPUID/MSR bits.
445  */
early_snp_init(struct boot_params * bp)446 static bool early_snp_init(struct boot_params *bp)
447 {
448 	struct cc_blob_sev_info *cc_info;
449 
450 	if (!bp)
451 		return false;
452 
453 	cc_info = find_cc_blob(bp);
454 	if (!cc_info)
455 		return false;
456 
457 	/*
458 	 * If a SNP-specific Confidential Computing blob is present, then
459 	 * firmware/bootloader have indicated SNP support. Verifying this
460 	 * involves CPUID checks which will be more reliable if the SNP
461 	 * CPUID table is used. See comments over snp_setup_cpuid_table() for
462 	 * more details.
463 	 */
464 	setup_cpuid_table(cc_info);
465 
466 	/*
467 	 * Record the SVSM Calling Area (CA) address if the guest is not
468 	 * running at VMPL0. The CA will be used to communicate with the
469 	 * SVSM and request its services.
470 	 */
471 	svsm_setup_ca(cc_info);
472 
473 	/*
474 	 * Pass run-time kernel a pointer to CC info via boot_params so EFI
475 	 * config table doesn't need to be searched again during early startup
476 	 * phase.
477 	 */
478 	bp->cc_blob_address = (u32)(unsigned long)cc_info;
479 
480 	return true;
481 }
482 
483 /*
484  * sev_check_cpu_support - Check for SEV support in the CPU capabilities
485  *
486  * Returns < 0 if SEV is not supported, otherwise the position of the
487  * encryption bit in the page table descriptors.
488  */
sev_check_cpu_support(void)489 static int sev_check_cpu_support(void)
490 {
491 	unsigned int eax, ebx, ecx, edx;
492 
493 	/* Check for the SME/SEV support leaf */
494 	eax = 0x80000000;
495 	ecx = 0;
496 	native_cpuid(&eax, &ebx, &ecx, &edx);
497 	if (eax < 0x8000001f)
498 		return -ENODEV;
499 
500 	/*
501 	 * Check for the SME/SEV feature:
502 	 *   CPUID Fn8000_001F[EAX]
503 	 *   - Bit 0 - Secure Memory Encryption support
504 	 *   - Bit 1 - Secure Encrypted Virtualization support
505 	 *   CPUID Fn8000_001F[EBX]
506 	 *   - Bits 5:0 - Pagetable bit position used to indicate encryption
507 	 */
508 	eax = 0x8000001f;
509 	ecx = 0;
510 	native_cpuid(&eax, &ebx, &ecx, &edx);
511 	/* Check whether SEV is supported */
512 	if (!(eax & BIT(1)))
513 		return -ENODEV;
514 
515 	return ebx & 0x3f;
516 }
517 
sev_enable(struct boot_params * bp)518 void sev_enable(struct boot_params *bp)
519 {
520 	struct msr m;
521 	int bitpos;
522 	bool snp;
523 
524 	/*
525 	 * bp->cc_blob_address should only be set by boot/compressed kernel.
526 	 * Initialize it to 0 to ensure that uninitialized values from
527 	 * buggy bootloaders aren't propagated.
528 	 */
529 	if (bp)
530 		bp->cc_blob_address = 0;
531 
532 	/*
533 	 * Do an initial SEV capability check before early_snp_init() which
534 	 * loads the CPUID page and the same checks afterwards are done
535 	 * without the hypervisor and are trustworthy.
536 	 *
537 	 * If the HV fakes SEV support, the guest will crash'n'burn
538 	 * which is good enough.
539 	 */
540 
541 	if (sev_check_cpu_support() < 0)
542 		return;
543 
544 	/*
545 	 * Setup/preliminary detection of SNP. This will be sanity-checked
546 	 * against CPUID/MSR values later.
547 	 */
548 	snp = early_snp_init(bp);
549 
550 	/* Now repeat the checks with the SNP CPUID table. */
551 
552 	bitpos = sev_check_cpu_support();
553 	if (bitpos < 0) {
554 		if (snp)
555 			error("SEV-SNP support indicated by CC blob, but not CPUID.");
556 		return;
557 	}
558 
559 	/* Set the SME mask if this is an SEV guest. */
560 	boot_rdmsr(MSR_AMD64_SEV, &m);
561 	sev_status = m.q;
562 	if (!(sev_status & MSR_AMD64_SEV_ENABLED))
563 		return;
564 
565 	/* Negotiate the GHCB protocol version. */
566 	if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
567 		if (!sev_es_negotiate_protocol())
568 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
569 	}
570 
571 	/*
572 	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
573 	 * features.
574 	 */
575 	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
576 		u64 hv_features;
577 		int ret;
578 
579 		hv_features = get_hv_features();
580 		if (!(hv_features & GHCB_HV_FT_SNP))
581 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
582 
583 		/*
584 		 * Enforce running at VMPL0 or with an SVSM.
585 		 *
586 		 * Use RMPADJUST (see the rmpadjust() function for a description of
587 		 * what the instruction does) to update the VMPL1 permissions of a
588 		 * page. If the guest is running at VMPL0, this will succeed. If the
589 		 * guest is running at any other VMPL, this will fail. Linux SNP guests
590 		 * only ever run at a single VMPL level so permission mask changes of a
591 		 * lesser-privileged VMPL are a don't-care.
592 		 */
593 		ret = rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, 1);
594 
595 		/*
596 		 * Running at VMPL0 is not required if an SVSM is present and the hypervisor
597 		 * supports the required SVSM GHCB events.
598 		 */
599 		if (ret &&
600 		    !(snp_vmpl && (hv_features & GHCB_HV_FT_SNP_MULTI_VMPL)))
601 			sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
602 	}
603 
604 	if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
605 		error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
606 
607 	sme_me_mask = BIT_ULL(bitpos);
608 }
609 
610 /*
611  * sev_get_status - Retrieve the SEV status mask
612  *
613  * Returns 0 if the CPU is not SEV capable, otherwise the value of the
614  * AMD64_SEV MSR.
615  */
sev_get_status(void)616 u64 sev_get_status(void)
617 {
618 	struct msr m;
619 
620 	if (sev_check_cpu_support() < 0)
621 		return 0;
622 
623 	boot_rdmsr(MSR_AMD64_SEV, &m);
624 	return m.q;
625 }
626 
sev_prep_identity_maps(unsigned long top_level_pgt)627 void sev_prep_identity_maps(unsigned long top_level_pgt)
628 {
629 	/*
630 	 * The Confidential Computing blob is used very early in uncompressed
631 	 * kernel to find the in-memory CPUID table to handle CPUID
632 	 * instructions. Make sure an identity-mapping exists so it can be
633 	 * accessed after switchover.
634 	 */
635 	if (sev_snp_enabled()) {
636 		unsigned long cc_info_pa = boot_params_ptr->cc_blob_address;
637 		struct cc_blob_sev_info *cc_info;
638 
639 		kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
640 
641 		cc_info = (struct cc_blob_sev_info *)cc_info_pa;
642 		kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
643 	}
644 
645 	sev_verify_cbit(top_level_pgt);
646 }
647 
early_is_sevsnp_guest(void)648 bool early_is_sevsnp_guest(void)
649 {
650 	static bool sevsnp;
651 
652 	if (sevsnp)
653 		return true;
654 
655 	if (!(sev_get_status() & MSR_AMD64_SEV_SNP_ENABLED))
656 		return false;
657 
658 	sevsnp = true;
659 
660 	if (!snp_vmpl) {
661 		unsigned int eax, ebx, ecx, edx;
662 
663 		/*
664 		 * CPUID Fn8000_001F_EAX[28] - SVSM support
665 		 */
666 		eax = 0x8000001f;
667 		ecx = 0;
668 		native_cpuid(&eax, &ebx, &ecx, &edx);
669 		if (eax & BIT(28)) {
670 			struct msr m;
671 
672 			/* Obtain the address of the calling area to use */
673 			boot_rdmsr(MSR_SVSM_CAA, &m);
674 			boot_svsm_caa = (void *)m.q;
675 			boot_svsm_caa_pa = m.q;
676 
677 			/*
678 			 * The real VMPL level cannot be discovered, but the
679 			 * memory acceptance routines make no use of that so
680 			 * any non-zero value suffices here.
681 			 */
682 			snp_vmpl = U8_MAX;
683 		}
684 	}
685 	return true;
686 }
687