1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Encrypted Register State Support
7 * This file is not compiled stand-alone. It contains code shared
8 * between the pre-decompression boot code and the running Linux kernel
9 * and is included directly into both code-bases.
12 #include <asm/setup_data.h>
14 #ifndef __BOOT_COMPRESSED
15 #define error(v) pr_err(v)
16 #define has_cpuflag(f) boot_cpu_has(f)
17 #define sev_printk(fmt, ...) printk(fmt, ##__VA_ARGS__)
18 #define sev_printk_rtl(fmt, ...) printk_ratelimited(fmt, ##__VA_ARGS__)
21 #define WARN(condition, format...) (!!(condition))
22 #define sev_printk(fmt, ...)
23 #define sev_printk_rtl(fmt, ...)
24 #undef vc_forward_exception
25 #define vc_forward_exception(c) panic("SNP: Hypervisor requested exception\n")
29 * SVSM related information:
30 * When running under an SVSM, the VMPL that Linux is executing at must be
31 * non-zero. The VMPL is therefore used to indicate the presence of an SVSM.
33 * During boot, the page tables are set up as identity mapped and later
34 * changed to use kernel virtual addresses. Maintain separate virtual and
35 * physical addresses for the CAA to allow SVSM functions to be used during
36 * early boot, both with identity mapped virtual addresses and proper kernel
39 u8 snp_vmpl __ro_after_init;
40 EXPORT_SYMBOL_GPL(snp_vmpl);
41 static struct svsm_ca *boot_svsm_caa __ro_after_init;
42 static u64 boot_svsm_caa_pa __ro_after_init;
44 static struct svsm_ca *svsm_get_caa(void);
45 static u64 svsm_get_caa_pa(void);
46 static int svsm_perform_call_protocol(struct svsm_call *call);
48 /* I/O parameters for CPUID-related helpers */
59 * Individual entries of the SNP CPUID table, as defined by the SNP
60 * Firmware ABI, Revision 0.9, Section 7.1, Table 14.
75 * SNP CPUID table, as defined by the SNP Firmware ABI, Revision 0.9,
76 * Section 8.14.2.6. Also noted there is the SNP firmware-enforced limit
77 * of 64 entries per CPUID table.
79 #define SNP_CPUID_COUNT_MAX 64
81 struct snp_cpuid_table {
85 struct snp_cpuid_fn fn[SNP_CPUID_COUNT_MAX];
89 * Since feature negotiation related variables are set early in the boot
90 * process they must reside in the .data section so as not to be zeroed
91 * out when the .bss section is later cleared.
93 * GHCB protocol version negotiated with the hypervisor.
95 static u16 ghcb_version __ro_after_init;
97 /* Copy of the SNP firmware's CPUID page. */
98 static struct snp_cpuid_table cpuid_table_copy __ro_after_init;
101 * These will be initialized based on CPUID table so that non-present
102 * all-zero leaves (for sparse tables) can be differentiated from
103 * invalid/out-of-range leaves. This is needed since all-zero leaves
104 * still need to be post-processed.
106 static u32 cpuid_std_range_max __ro_after_init;
107 static u32 cpuid_hyp_range_max __ro_after_init;
108 static u32 cpuid_ext_range_max __ro_after_init;
110 static bool __init sev_es_check_cpu_features(void)
112 if (!has_cpuflag(X86_FEATURE_RDRAND)) {
113 error("RDRAND instruction not supported - no trusted source of randomness available\n");
120 static void __head __noreturn
121 sev_es_terminate(unsigned int set, unsigned int reason)
123 u64 val = GHCB_MSR_TERM_REQ;
125 /* Tell the hypervisor what went wrong. */
126 val |= GHCB_SEV_TERM_REASON(set, reason);
128 /* Request Guest Termination from Hypervisor */
129 sev_es_wr_ghcb_msr(val);
133 asm volatile("hlt\n" : : : "memory");
137 * The hypervisor features are available from GHCB version 2 onward.
139 static u64 get_hv_features(void)
143 if (ghcb_version < 2)
146 sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ);
149 val = sev_es_rd_ghcb_msr();
150 if (GHCB_RESP_CODE(val) != GHCB_MSR_HV_FT_RESP)
153 return GHCB_MSR_HV_FT_RESP_VAL(val);
156 static void snp_register_ghcb_early(unsigned long paddr)
158 unsigned long pfn = paddr >> PAGE_SHIFT;
161 sev_es_wr_ghcb_msr(GHCB_MSR_REG_GPA_REQ_VAL(pfn));
164 val = sev_es_rd_ghcb_msr();
166 /* If the response GPA is not ours then abort the guest */
167 if ((GHCB_RESP_CODE(val) != GHCB_MSR_REG_GPA_RESP) ||
168 (GHCB_MSR_REG_GPA_RESP_VAL(val) != pfn))
169 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_REGISTER);
172 static bool sev_es_negotiate_protocol(void)
176 /* Do the GHCB protocol version negotiation */
177 sev_es_wr_ghcb_msr(GHCB_MSR_SEV_INFO_REQ);
179 val = sev_es_rd_ghcb_msr();
181 if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
184 if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
185 GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
188 ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX);
193 static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb)
195 ghcb->save.sw_exit_code = 0;
196 __builtin_memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
199 static bool vc_decoding_needed(unsigned long exit_code)
201 /* Exceptions don't require to decode the instruction */
202 return !(exit_code >= SVM_EXIT_EXCP_BASE &&
203 exit_code <= SVM_EXIT_LAST_EXCP);
206 static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt,
207 struct pt_regs *regs,
208 unsigned long exit_code)
210 enum es_result ret = ES_OK;
212 memset(ctxt, 0, sizeof(*ctxt));
215 if (vc_decoding_needed(exit_code))
216 ret = vc_decode_insn(ctxt);
221 static void vc_finish_insn(struct es_em_ctxt *ctxt)
223 ctxt->regs->ip += ctxt->insn.length;
226 static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
230 ret = ghcb->save.sw_exit_info_1 & GENMASK_ULL(31, 0);
235 u64 info = ghcb->save.sw_exit_info_2;
236 unsigned long v = info & SVM_EVTINJ_VEC_MASK;
238 /* Check if exception information from hypervisor is sane. */
239 if ((info & SVM_EVTINJ_VALID) &&
240 ((v == X86_TRAP_GP) || (v == X86_TRAP_UD)) &&
241 ((info & SVM_EVTINJ_TYPE_MASK) == SVM_EVTINJ_TYPE_EXEPT)) {
244 if (info & SVM_EVTINJ_VALID_ERR)
245 ctxt->fi.error_code = info >> 32;
254 static inline int svsm_process_result_codes(struct svsm_call *call)
256 switch (call->rax_out) {
259 case SVSM_ERR_INCOMPLETE:
268 * Issue a VMGEXIT to call the SVSM:
269 * - Load the SVSM register state (RAX, RCX, RDX, R8 and R9)
270 * - Set the CA call pending field to 1
272 * - Save the SVSM return register state (RAX, RCX, RDX, R8 and R9)
273 * - Perform atomic exchange of the CA call pending field
275 * - See the "Secure VM Service Module for SEV-SNP Guests" specification for
276 * details on the calling convention.
277 * - The calling convention loosely follows the Microsoft X64 calling
278 * convention by putting arguments in RCX, RDX, R8 and R9.
279 * - RAX specifies the SVSM protocol/callid as input and the return code
282 static __always_inline void svsm_issue_call(struct svsm_call *call, u8 *pending)
284 register unsigned long rax asm("rax") = call->rax;
285 register unsigned long rcx asm("rcx") = call->rcx;
286 register unsigned long rdx asm("rdx") = call->rdx;
287 register unsigned long r8 asm("r8") = call->r8;
288 register unsigned long r9 asm("r9") = call->r9;
290 call->caa->call_pending = 1;
292 asm volatile("rep; vmmcall\n\t"
293 : "+r" (rax), "+r" (rcx), "+r" (rdx), "+r" (r8), "+r" (r9)
296 *pending = xchg(&call->caa->call_pending, *pending);
305 static int svsm_perform_msr_protocol(struct svsm_call *call)
311 * When using the MSR protocol, be sure to save and restore
312 * the current MSR value.
314 val = sev_es_rd_ghcb_msr();
316 sev_es_wr_ghcb_msr(GHCB_MSR_VMPL_REQ_LEVEL(0));
318 svsm_issue_call(call, &pending);
320 resp = sev_es_rd_ghcb_msr();
322 sev_es_wr_ghcb_msr(val);
327 if (GHCB_RESP_CODE(resp) != GHCB_MSR_VMPL_RESP)
330 if (GHCB_MSR_VMPL_RESP_VAL(resp))
333 return svsm_process_result_codes(call);
336 static int svsm_perform_ghcb_protocol(struct ghcb *ghcb, struct svsm_call *call)
338 struct es_em_ctxt ctxt;
341 vc_ghcb_invalidate(ghcb);
344 * Fill in protocol and format specifiers. This can be called very early
345 * in the boot, so use rip-relative references as needed.
347 ghcb->protocol_version = RIP_REL_REF(ghcb_version);
348 ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
350 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_SNP_RUN_VMPL);
351 ghcb_set_sw_exit_info_1(ghcb, 0);
352 ghcb_set_sw_exit_info_2(ghcb, 0);
354 sev_es_wr_ghcb_msr(__pa(ghcb));
356 svsm_issue_call(call, &pending);
361 switch (verify_exception_info(ghcb, &ctxt)) {
365 vc_forward_exception(&ctxt);
371 return svsm_process_result_codes(call);
374 static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
375 struct es_em_ctxt *ctxt,
376 u64 exit_code, u64 exit_info_1,
379 /* Fill in protocol and format specifiers */
380 ghcb->protocol_version = ghcb_version;
381 ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
383 ghcb_set_sw_exit_code(ghcb, exit_code);
384 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
385 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
387 sev_es_wr_ghcb_msr(__pa(ghcb));
390 return verify_exception_info(ghcb, ctxt);
393 static int __sev_cpuid_hv(u32 fn, int reg_idx, u32 *reg)
397 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, reg_idx));
399 val = sev_es_rd_ghcb_msr();
400 if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP)
408 static int __sev_cpuid_hv_msr(struct cpuid_leaf *leaf)
413 * MSR protocol does not support fetching non-zero subfunctions, but is
414 * sufficient to handle current early-boot cases. Should that change,
415 * make sure to report an error rather than ignoring the index and
416 * grabbing random values. If this issue arises in the future, handling
417 * can be added here to use GHCB-page protocol for cases that occur late
418 * enough in boot that GHCB page is available.
420 if (cpuid_function_is_indexed(leaf->fn) && leaf->subfn)
423 ret = __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EAX, &leaf->eax);
424 ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EBX, &leaf->ebx);
425 ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_ECX, &leaf->ecx);
426 ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EDX, &leaf->edx);
431 static int __sev_cpuid_hv_ghcb(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
433 u32 cr4 = native_read_cr4();
436 ghcb_set_rax(ghcb, leaf->fn);
437 ghcb_set_rcx(ghcb, leaf->subfn);
439 if (cr4 & X86_CR4_OSXSAVE)
440 /* Safe to read xcr0 */
441 ghcb_set_xcr0(ghcb, xgetbv(XCR_XFEATURE_ENABLED_MASK));
443 /* xgetbv will cause #UD - use reset value for xcr0 */
444 ghcb_set_xcr0(ghcb, 1);
446 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
450 if (!(ghcb_rax_is_valid(ghcb) &&
451 ghcb_rbx_is_valid(ghcb) &&
452 ghcb_rcx_is_valid(ghcb) &&
453 ghcb_rdx_is_valid(ghcb)))
456 leaf->eax = ghcb->save.rax;
457 leaf->ebx = ghcb->save.rbx;
458 leaf->ecx = ghcb->save.rcx;
459 leaf->edx = ghcb->save.rdx;
464 static int sev_cpuid_hv(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
466 return ghcb ? __sev_cpuid_hv_ghcb(ghcb, ctxt, leaf)
467 : __sev_cpuid_hv_msr(leaf);
471 * This may be called early while still running on the initial identity
472 * mapping. Use RIP-relative addressing to obtain the correct address
473 * while running with the initial identity mapping as well as the
474 * switch-over to kernel virtual addresses later.
476 static const struct snp_cpuid_table *snp_cpuid_get_table(void)
478 return &RIP_REL_REF(cpuid_table_copy);
482 * The SNP Firmware ABI, Revision 0.9, Section 7.1, details the use of
483 * XCR0_IN and XSS_IN to encode multiple versions of 0xD subfunctions 0
484 * and 1 based on the corresponding features enabled by a particular
485 * combination of XCR0 and XSS registers so that a guest can look up the
486 * version corresponding to the features currently enabled in its XCR0/XSS
487 * registers. The only values that differ between these versions/table
488 * entries is the enabled XSAVE area size advertised via EBX.
490 * While hypervisors may choose to make use of this support, it is more
491 * robust/secure for a guest to simply find the entry corresponding to the
492 * base/legacy XSAVE area size (XCR0=1 or XCR0=3), and then calculate the
493 * XSAVE area size using subfunctions 2 through 64, as documented in APM
494 * Volume 3, Rev 3.31, Appendix E.3.8, which is what is done here.
496 * Since base/legacy XSAVE area size is documented as 0x240, use that value
497 * directly rather than relying on the base size in the CPUID table.
499 * Return: XSAVE area size on success, 0 otherwise.
501 static u32 snp_cpuid_calc_xsave_size(u64 xfeatures_en, bool compacted)
503 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
504 u64 xfeatures_found = 0;
505 u32 xsave_size = 0x240;
508 for (i = 0; i < cpuid_table->count; i++) {
509 const struct snp_cpuid_fn *e = &cpuid_table->fn[i];
511 if (!(e->eax_in == 0xD && e->ecx_in > 1 && e->ecx_in < 64))
513 if (!(xfeatures_en & (BIT_ULL(e->ecx_in))))
515 if (xfeatures_found & (BIT_ULL(e->ecx_in)))
518 xfeatures_found |= (BIT_ULL(e->ecx_in));
521 xsave_size += e->eax;
523 xsave_size = max(xsave_size, e->eax + e->ebx);
527 * Either the guest set unsupported XCR0/XSS bits, or the corresponding
528 * entries in the CPUID table were not present. This is not a valid
531 if (xfeatures_found != (xfeatures_en & GENMASK_ULL(63, 2)))
538 snp_cpuid_get_validated_func(struct cpuid_leaf *leaf)
540 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
543 for (i = 0; i < cpuid_table->count; i++) {
544 const struct snp_cpuid_fn *e = &cpuid_table->fn[i];
546 if (e->eax_in != leaf->fn)
549 if (cpuid_function_is_indexed(leaf->fn) && e->ecx_in != leaf->subfn)
553 * For 0xD subfunctions 0 and 1, only use the entry corresponding
554 * to the base/legacy XSAVE area size (XCR0=1 or XCR0=3, XSS=0).
555 * See the comments above snp_cpuid_calc_xsave_size() for more
558 if (e->eax_in == 0xD && (e->ecx_in == 0 || e->ecx_in == 1))
559 if (!(e->xcr0_in == 1 || e->xcr0_in == 3) || e->xss_in)
573 static void snp_cpuid_hv(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
575 if (sev_cpuid_hv(ghcb, ctxt, leaf))
576 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID_HV);
579 static int snp_cpuid_postprocess(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
580 struct cpuid_leaf *leaf)
582 struct cpuid_leaf leaf_hv = *leaf;
586 snp_cpuid_hv(ghcb, ctxt, &leaf_hv);
588 /* initial APIC ID */
589 leaf->ebx = (leaf_hv.ebx & GENMASK(31, 24)) | (leaf->ebx & GENMASK(23, 0));
590 /* APIC enabled bit */
591 leaf->edx = (leaf_hv.edx & BIT(9)) | (leaf->edx & ~BIT(9));
593 /* OSXSAVE enabled bit */
594 if (native_read_cr4() & X86_CR4_OSXSAVE)
595 leaf->ecx |= BIT(27);
598 /* OSPKE enabled bit */
599 leaf->ecx &= ~BIT(4);
600 if (native_read_cr4() & X86_CR4_PKE)
605 snp_cpuid_hv(ghcb, ctxt, &leaf_hv);
607 /* extended APIC ID */
608 leaf->edx = leaf_hv.edx;
611 bool compacted = false;
612 u64 xcr0 = 1, xss = 0;
615 if (leaf->subfn != 0 && leaf->subfn != 1)
618 if (native_read_cr4() & X86_CR4_OSXSAVE)
619 xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
620 if (leaf->subfn == 1) {
621 /* Get XSS value if XSAVES is enabled. */
622 if (leaf->eax & BIT(3)) {
623 unsigned long lo, hi;
625 asm volatile("rdmsr" : "=a" (lo), "=d" (hi)
626 : "c" (MSR_IA32_XSS));
627 xss = (hi << 32) | lo;
631 * The PPR and APM aren't clear on what size should be
632 * encoded in 0xD:0x1:EBX when compaction is not enabled
633 * by either XSAVEC (feature bit 1) or XSAVES (feature
634 * bit 3) since SNP-capable hardware has these feature
635 * bits fixed as 1. KVM sets it to 0 in this case, but
636 * to avoid this becoming an issue it's safer to simply
637 * treat this as unsupported for SNP guests.
639 if (!(leaf->eax & (BIT(1) | BIT(3))))
645 xsave_size = snp_cpuid_calc_xsave_size(xcr0 | xss, compacted);
649 leaf->ebx = xsave_size;
653 snp_cpuid_hv(ghcb, ctxt, &leaf_hv);
655 /* extended APIC ID */
656 leaf->eax = leaf_hv.eax;
658 leaf->ebx = (leaf->ebx & GENMASK(31, 8)) | (leaf_hv.ebx & GENMASK(7, 0));
660 leaf->ecx = (leaf->ecx & GENMASK(31, 8)) | (leaf_hv.ecx & GENMASK(7, 0));
663 /* No fix-ups needed, use values as-is. */
671 * Returns -EOPNOTSUPP if feature not enabled. Any other non-zero return value
672 * should be treated as fatal by caller.
675 snp_cpuid(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
677 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
679 if (!cpuid_table->count)
682 if (!snp_cpuid_get_validated_func(leaf)) {
684 * Some hypervisors will avoid keeping track of CPUID entries
685 * where all values are zero, since they can be handled the
686 * same as out-of-range values (all-zero). This is useful here
687 * as well as it allows virtually all guest configurations to
688 * work using a single SNP CPUID table.
690 * To allow for this, there is a need to distinguish between
691 * out-of-range entries and in-range zero entries, since the
692 * CPUID table entries are only a template that may need to be
693 * augmented with additional values for things like
694 * CPU-specific information during post-processing. So if it's
695 * not in the table, set the values to zero. Then, if they are
696 * within a valid CPUID range, proceed with post-processing
697 * using zeros as the initial values. Otherwise, skip
698 * post-processing and just return zeros immediately.
700 leaf->eax = leaf->ebx = leaf->ecx = leaf->edx = 0;
702 /* Skip post-processing for out-of-range zero leafs. */
703 if (!(leaf->fn <= RIP_REL_REF(cpuid_std_range_max) ||
704 (leaf->fn >= 0x40000000 && leaf->fn <= RIP_REL_REF(cpuid_hyp_range_max)) ||
705 (leaf->fn >= 0x80000000 && leaf->fn <= RIP_REL_REF(cpuid_ext_range_max))))
709 return snp_cpuid_postprocess(ghcb, ctxt, leaf);
713 * Boot VC Handler - This is the first VC handler during boot, there is no GHCB
714 * page yet, so it only supports the MSR based communication with the
715 * hypervisor and only the CPUID exit-code.
717 void __head do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
719 unsigned int subfn = lower_bits(regs->cx, 32);
720 unsigned int fn = lower_bits(regs->ax, 32);
721 u16 opcode = *(unsigned short *)regs->ip;
722 struct cpuid_leaf leaf;
725 /* Only CPUID is supported via MSR protocol */
726 if (exit_code != SVM_EXIT_CPUID)
729 /* Is it really a CPUID insn? */
730 if (opcode != 0xa20f)
736 ret = snp_cpuid(NULL, NULL, &leaf);
740 if (ret != -EOPNOTSUPP)
743 if (__sev_cpuid_hv_msr(&leaf))
753 * This is a VC handler and the #VC is only raised when SEV-ES is
754 * active, which means SEV must be active too. Do sanity checks on the
755 * CPUID results to make sure the hypervisor does not trick the kernel
756 * into the no-sev path. This could map sensitive data unencrypted and
757 * make it accessible to the hypervisor.
759 * In particular, check for:
760 * - Availability of CPUID leaf 0x8000001f
763 * The hypervisor might still report the wrong C-bit position, but this
764 * can't be checked here.
767 if (fn == 0x80000000 && (regs->ax < 0x8000001f))
770 else if ((fn == 0x8000001f && !(regs->ax & BIT(1))))
774 /* Skip over the CPUID two-byte opcode */
780 /* Terminate the guest */
781 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
784 static enum es_result vc_insn_string_check(struct es_em_ctxt *ctxt,
785 unsigned long address,
788 if (user_mode(ctxt->regs) && fault_in_kernel_space(address)) {
789 ctxt->fi.vector = X86_TRAP_PF;
790 ctxt->fi.error_code = X86_PF_USER;
791 ctxt->fi.cr2 = address;
793 ctxt->fi.error_code |= X86_PF_WRITE;
801 static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt,
802 void *src, char *buf,
803 unsigned int data_size,
807 int i, b = backwards ? -1 : 1;
808 unsigned long address = (unsigned long)src;
811 ret = vc_insn_string_check(ctxt, address, false);
815 for (i = 0; i < count; i++) {
816 void *s = src + (i * data_size * b);
817 char *d = buf + (i * data_size);
819 ret = vc_read_mem(ctxt, s, d, data_size);
827 static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt,
828 void *dst, char *buf,
829 unsigned int data_size,
833 int i, s = backwards ? -1 : 1;
834 unsigned long address = (unsigned long)dst;
837 ret = vc_insn_string_check(ctxt, address, true);
841 for (i = 0; i < count; i++) {
842 void *d = dst + (i * data_size * s);
843 char *b = buf + (i * data_size);
845 ret = vc_write_mem(ctxt, d, b, data_size);
853 #define IOIO_TYPE_STR BIT(2)
854 #define IOIO_TYPE_IN 1
855 #define IOIO_TYPE_INS (IOIO_TYPE_IN | IOIO_TYPE_STR)
856 #define IOIO_TYPE_OUT 0
857 #define IOIO_TYPE_OUTS (IOIO_TYPE_OUT | IOIO_TYPE_STR)
859 #define IOIO_REP BIT(3)
861 #define IOIO_ADDR_64 BIT(9)
862 #define IOIO_ADDR_32 BIT(8)
863 #define IOIO_ADDR_16 BIT(7)
865 #define IOIO_DATA_32 BIT(6)
866 #define IOIO_DATA_16 BIT(5)
867 #define IOIO_DATA_8 BIT(4)
869 #define IOIO_SEG_ES (0 << 10)
870 #define IOIO_SEG_DS (3 << 10)
872 static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)
874 struct insn *insn = &ctxt->insn;
880 switch (insn->opcode.bytes[0]) {
884 *exitinfo |= IOIO_TYPE_INS;
885 *exitinfo |= IOIO_SEG_ES;
886 port = ctxt->regs->dx & 0xffff;
892 *exitinfo |= IOIO_TYPE_OUTS;
893 *exitinfo |= IOIO_SEG_DS;
894 port = ctxt->regs->dx & 0xffff;
897 /* IN immediate opcodes */
900 *exitinfo |= IOIO_TYPE_IN;
901 port = (u8)insn->immediate.value & 0xffff;
904 /* OUT immediate opcodes */
907 *exitinfo |= IOIO_TYPE_OUT;
908 port = (u8)insn->immediate.value & 0xffff;
911 /* IN register opcodes */
914 *exitinfo |= IOIO_TYPE_IN;
915 port = ctxt->regs->dx & 0xffff;
918 /* OUT register opcodes */
921 *exitinfo |= IOIO_TYPE_OUT;
922 port = ctxt->regs->dx & 0xffff;
926 return ES_DECODE_FAILED;
929 *exitinfo |= port << 16;
931 switch (insn->opcode.bytes[0]) {
938 /* Single byte opcodes */
939 *exitinfo |= IOIO_DATA_8;
943 /* Length determined by instruction parsing */
944 *exitinfo |= (insn->opnd_bytes == 2) ? IOIO_DATA_16
946 size = (insn->opnd_bytes == 2) ? 2 : 4;
949 switch (insn->addr_bytes) {
951 *exitinfo |= IOIO_ADDR_16;
954 *exitinfo |= IOIO_ADDR_32;
957 *exitinfo |= IOIO_ADDR_64;
961 if (insn_has_rep_prefix(insn))
962 *exitinfo |= IOIO_REP;
964 return vc_ioio_check(ctxt, (u16)port, size);
967 static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
969 struct pt_regs *regs = ctxt->regs;
970 u64 exit_info_1, exit_info_2;
973 ret = vc_ioio_exitinfo(ctxt, &exit_info_1);
977 if (exit_info_1 & IOIO_TYPE_STR) {
981 bool df = ((regs->flags & X86_EFLAGS_DF) == X86_EFLAGS_DF);
982 unsigned int io_bytes, exit_bytes;
983 unsigned int ghcb_count, op_count;
984 unsigned long es_base;
988 * For the string variants with rep prefix the amount of in/out
989 * operations per #VC exception is limited so that the kernel
990 * has a chance to take interrupts and re-schedule while the
991 * instruction is emulated.
993 io_bytes = (exit_info_1 >> 4) & 0x7;
994 ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;
996 op_count = (exit_info_1 & IOIO_REP) ? regs->cx : 1;
997 exit_info_2 = min(op_count, ghcb_count);
998 exit_bytes = exit_info_2 * io_bytes;
1000 es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
1002 /* Read bytes of OUTS into the shared buffer */
1003 if (!(exit_info_1 & IOIO_TYPE_IN)) {
1004 ret = vc_insn_string_read(ctxt,
1005 (void *)(es_base + regs->si),
1006 ghcb->shared_buffer, io_bytes,
1013 * Issue an VMGEXIT to the HV to consume the bytes from the
1014 * shared buffer or to have it write them into the shared buffer
1015 * depending on the instruction: OUTS or INS.
1017 sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer);
1018 ghcb_set_sw_scratch(ghcb, sw_scratch);
1019 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO,
1020 exit_info_1, exit_info_2);
1024 /* Read bytes from shared buffer into the guest's destination. */
1025 if (exit_info_1 & IOIO_TYPE_IN) {
1026 ret = vc_insn_string_write(ctxt,
1027 (void *)(es_base + regs->di),
1028 ghcb->shared_buffer, io_bytes,
1034 regs->di -= exit_bytes;
1036 regs->di += exit_bytes;
1039 regs->si -= exit_bytes;
1041 regs->si += exit_bytes;
1044 if (exit_info_1 & IOIO_REP)
1045 regs->cx -= exit_info_2;
1047 ret = regs->cx ? ES_RETRY : ES_OK;
1051 /* IN/OUT into/from rAX */
1053 int bits = (exit_info_1 & 0x70) >> 1;
1056 if (!(exit_info_1 & IOIO_TYPE_IN))
1057 rax = lower_bits(regs->ax, bits);
1059 ghcb_set_rax(ghcb, rax);
1061 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0);
1065 if (exit_info_1 & IOIO_TYPE_IN) {
1066 if (!ghcb_rax_is_valid(ghcb))
1067 return ES_VMM_ERROR;
1068 regs->ax = lower_bits(ghcb->save.rax, bits);
1075 static int vc_handle_cpuid_snp(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1077 struct pt_regs *regs = ctxt->regs;
1078 struct cpuid_leaf leaf;
1082 leaf.subfn = regs->cx;
1083 ret = snp_cpuid(ghcb, ctxt, &leaf);
1085 regs->ax = leaf.eax;
1086 regs->bx = leaf.ebx;
1087 regs->cx = leaf.ecx;
1088 regs->dx = leaf.edx;
1094 static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
1095 struct es_em_ctxt *ctxt)
1097 struct pt_regs *regs = ctxt->regs;
1098 u32 cr4 = native_read_cr4();
1102 snp_cpuid_ret = vc_handle_cpuid_snp(ghcb, ctxt);
1105 if (snp_cpuid_ret != -EOPNOTSUPP)
1106 return ES_VMM_ERROR;
1108 ghcb_set_rax(ghcb, regs->ax);
1109 ghcb_set_rcx(ghcb, regs->cx);
1111 if (cr4 & X86_CR4_OSXSAVE)
1112 /* Safe to read xcr0 */
1113 ghcb_set_xcr0(ghcb, xgetbv(XCR_XFEATURE_ENABLED_MASK));
1115 /* xgetbv will cause #GP - use reset value for xcr0 */
1116 ghcb_set_xcr0(ghcb, 1);
1118 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
1122 if (!(ghcb_rax_is_valid(ghcb) &&
1123 ghcb_rbx_is_valid(ghcb) &&
1124 ghcb_rcx_is_valid(ghcb) &&
1125 ghcb_rdx_is_valid(ghcb)))
1126 return ES_VMM_ERROR;
1128 regs->ax = ghcb->save.rax;
1129 regs->bx = ghcb->save.rbx;
1130 regs->cx = ghcb->save.rcx;
1131 regs->dx = ghcb->save.rdx;
1136 static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
1137 struct es_em_ctxt *ctxt,
1138 unsigned long exit_code)
1140 bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
1143 ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
1147 if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb) &&
1148 (!rdtscp || ghcb_rcx_is_valid(ghcb))))
1149 return ES_VMM_ERROR;
1151 ctxt->regs->ax = ghcb->save.rax;
1152 ctxt->regs->dx = ghcb->save.rdx;
1154 ctxt->regs->cx = ghcb->save.rcx;
1159 struct cc_setup_data {
1160 struct setup_data header;
1161 u32 cc_blob_address;
1165 * Search for a Confidential Computing blob passed in as a setup_data entry
1166 * via the Linux Boot Protocol.
1169 struct cc_blob_sev_info *find_cc_blob_setup_data(struct boot_params *bp)
1171 struct cc_setup_data *sd = NULL;
1172 struct setup_data *hdr;
1174 hdr = (struct setup_data *)bp->hdr.setup_data;
1177 if (hdr->type == SETUP_CC_BLOB) {
1178 sd = (struct cc_setup_data *)hdr;
1179 return (struct cc_blob_sev_info *)(unsigned long)sd->cc_blob_address;
1181 hdr = (struct setup_data *)hdr->next;
1188 * Initialize the kernel's copy of the SNP CPUID table, and set up the
1189 * pointer that will be used to access it.
1191 * Maintaining a direct mapping of the SNP CPUID table used by firmware would
1192 * be possible as an alternative, but the approach is brittle since the
1193 * mapping needs to be updated in sync with all the changes to virtual memory
1194 * layout and related mapping facilities throughout the boot process.
1196 static void __head setup_cpuid_table(const struct cc_blob_sev_info *cc_info)
1198 const struct snp_cpuid_table *cpuid_table_fw, *cpuid_table;
1201 if (!cc_info || !cc_info->cpuid_phys || cc_info->cpuid_len < PAGE_SIZE)
1202 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
1204 cpuid_table_fw = (const struct snp_cpuid_table *)cc_info->cpuid_phys;
1205 if (!cpuid_table_fw->count || cpuid_table_fw->count > SNP_CPUID_COUNT_MAX)
1206 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
1208 cpuid_table = snp_cpuid_get_table();
1209 memcpy((void *)cpuid_table, cpuid_table_fw, sizeof(*cpuid_table));
1211 /* Initialize CPUID ranges for range-checking. */
1212 for (i = 0; i < cpuid_table->count; i++) {
1213 const struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
1215 if (fn->eax_in == 0x0)
1216 RIP_REL_REF(cpuid_std_range_max) = fn->eax;
1217 else if (fn->eax_in == 0x40000000)
1218 RIP_REL_REF(cpuid_hyp_range_max) = fn->eax;
1219 else if (fn->eax_in == 0x80000000)
1220 RIP_REL_REF(cpuid_ext_range_max) = fn->eax;
1224 static inline void __pval_terminate(u64 pfn, bool action, unsigned int page_size,
1225 int ret, u64 svsm_ret)
1227 WARN(1, "PVALIDATE failure: pfn: 0x%llx, action: %u, size: %u, ret: %d, svsm_ret: 0x%llx\n",
1228 pfn, action, page_size, ret, svsm_ret);
1230 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
1233 static void svsm_pval_terminate(struct svsm_pvalidate_call *pc, int ret, u64 svsm_ret)
1235 unsigned int page_size;
1239 pfn = pc->entry[pc->cur_index].pfn;
1240 action = pc->entry[pc->cur_index].action;
1241 page_size = pc->entry[pc->cur_index].page_size;
1243 __pval_terminate(pfn, action, page_size, ret, svsm_ret);
1246 static void svsm_pval_4k_page(unsigned long paddr, bool validate)
1248 struct svsm_pvalidate_call *pc;
1249 struct svsm_call call = {};
1250 unsigned long flags;
1255 * This can be called very early in the boot, use native functions in
1256 * order to avoid paravirt issues.
1258 flags = native_local_irq_save();
1260 call.caa = svsm_get_caa();
1262 pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer;
1263 pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
1265 pc->num_entries = 1;
1267 pc->entry[0].page_size = RMP_PG_SIZE_4K;
1268 pc->entry[0].action = validate;
1269 pc->entry[0].ignore_cf = 0;
1270 pc->entry[0].pfn = paddr >> PAGE_SHIFT;
1272 /* Protocol 0, Call ID 1 */
1273 call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE);
1276 ret = svsm_perform_call_protocol(&call);
1278 svsm_pval_terminate(pc, ret, call.rax_out);
1280 native_local_irq_restore(flags);
1283 static void pvalidate_4k_page(unsigned long vaddr, unsigned long paddr, bool validate)
1288 * This can be called very early during boot, so use rIP-relative
1289 * references as needed.
1291 if (RIP_REL_REF(snp_vmpl)) {
1292 svsm_pval_4k_page(paddr, validate);
1294 ret = pvalidate(vaddr, RMP_PG_SIZE_4K, validate);
1296 __pval_terminate(PHYS_PFN(paddr), validate, RMP_PG_SIZE_4K, ret, 0);
1300 static void pval_pages(struct snp_psc_desc *desc)
1302 struct psc_entry *e;
1303 unsigned long vaddr;
1310 for (i = 0; i <= desc->hdr.end_entry; i++) {
1311 e = &desc->entries[i];
1314 vaddr = (unsigned long)pfn_to_kaddr(pfn);
1315 size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K;
1316 validate = e->operation == SNP_PAGE_STATE_PRIVATE;
1318 rc = pvalidate(vaddr, size, validate);
1322 if (rc == PVALIDATE_FAIL_SIZEMISMATCH && size == RMP_PG_SIZE_2M) {
1323 unsigned long vaddr_end = vaddr + PMD_SIZE;
1325 for (; vaddr < vaddr_end; vaddr += PAGE_SIZE, pfn++) {
1326 rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate);
1328 __pval_terminate(pfn, validate, RMP_PG_SIZE_4K, rc, 0);
1331 __pval_terminate(pfn, validate, size, rc, 0);
1336 static u64 svsm_build_ca_from_pfn_range(u64 pfn, u64 pfn_end, bool action,
1337 struct svsm_pvalidate_call *pc)
1339 struct svsm_pvalidate_entry *pe;
1341 /* Nothing in the CA yet */
1342 pc->num_entries = 0;
1347 while (pfn < pfn_end) {
1348 pe->page_size = RMP_PG_SIZE_4K;
1349 pe->action = action;
1357 if (pc->num_entries == SVSM_PVALIDATE_MAX_COUNT)
1364 static int svsm_build_ca_from_psc_desc(struct snp_psc_desc *desc, unsigned int desc_entry,
1365 struct svsm_pvalidate_call *pc)
1367 struct svsm_pvalidate_entry *pe;
1368 struct psc_entry *e;
1370 /* Nothing in the CA yet */
1371 pc->num_entries = 0;
1375 e = &desc->entries[desc_entry];
1377 while (desc_entry <= desc->hdr.end_entry) {
1378 pe->page_size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K;
1379 pe->action = e->operation == SNP_PAGE_STATE_PRIVATE;
1388 if (pc->num_entries == SVSM_PVALIDATE_MAX_COUNT)
1395 static void svsm_pval_pages(struct snp_psc_desc *desc)
1397 struct svsm_pvalidate_entry pv_4k[VMGEXIT_PSC_MAX_ENTRY];
1398 unsigned int i, pv_4k_count = 0;
1399 struct svsm_pvalidate_call *pc;
1400 struct svsm_call call = {};
1401 unsigned long flags;
1407 * This can be called very early in the boot, use native functions in
1408 * order to avoid paravirt issues.
1410 flags = native_local_irq_save();
1413 * The SVSM calling area (CA) can support processing 510 entries at a
1414 * time. Loop through the Page State Change descriptor until the CA is
1415 * full or the last entry in the descriptor is reached, at which time
1416 * the SVSM is invoked. This repeats until all entries in the descriptor
1419 call.caa = svsm_get_caa();
1421 pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer;
1422 pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
1424 /* Protocol 0, Call ID 1 */
1425 call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE);
1428 for (i = 0; i <= desc->hdr.end_entry;) {
1429 i = svsm_build_ca_from_psc_desc(desc, i, pc);
1432 ret = svsm_perform_call_protocol(&call);
1437 * Check if the entry failed because of an RMP mismatch (a
1438 * PVALIDATE at 2M was requested, but the page is mapped in
1442 if (call.rax_out == SVSM_PVALIDATE_FAIL_SIZEMISMATCH &&
1443 pc->entry[pc->cur_index].page_size == RMP_PG_SIZE_2M) {
1444 /* Save this entry for post-processing at 4K */
1445 pv_4k[pv_4k_count++] = pc->entry[pc->cur_index];
1447 /* Skip to the next one unless at the end of the list */
1449 if (pc->cur_index < pc->num_entries)
1454 } while (ret == -EAGAIN);
1457 svsm_pval_terminate(pc, ret, call.rax_out);
1460 /* Process any entries that failed to be validated at 2M and validate them at 4K */
1461 for (i = 0; i < pv_4k_count; i++) {
1464 action = pv_4k[i].action;
1466 pfn_end = pfn + 512;
1468 while (pfn < pfn_end) {
1469 pfn = svsm_build_ca_from_pfn_range(pfn, pfn_end, action, pc);
1471 ret = svsm_perform_call_protocol(&call);
1473 svsm_pval_terminate(pc, ret, call.rax_out);
1477 native_local_irq_restore(flags);
1480 static void pvalidate_pages(struct snp_psc_desc *desc)
1483 svsm_pval_pages(desc);
1488 static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc)
1490 int cur_entry, end_entry, ret = 0;
1491 struct snp_psc_desc *data;
1492 struct es_em_ctxt ctxt;
1494 vc_ghcb_invalidate(ghcb);
1496 /* Copy the input desc into GHCB shared buffer */
1497 data = (struct snp_psc_desc *)ghcb->shared_buffer;
1498 memcpy(ghcb->shared_buffer, desc, min_t(int, GHCB_SHARED_BUF_SIZE, sizeof(*desc)));
1501 * As per the GHCB specification, the hypervisor can resume the guest
1502 * before processing all the entries. Check whether all the entries
1503 * are processed. If not, then keep retrying. Note, the hypervisor
1504 * will update the data memory directly to indicate the status, so
1505 * reference the data->hdr everywhere.
1507 * The strategy here is to wait for the hypervisor to change the page
1508 * state in the RMP table before guest accesses the memory pages. If the
1509 * page state change was not successful, then later memory access will
1510 * result in a crash.
1512 cur_entry = data->hdr.cur_entry;
1513 end_entry = data->hdr.end_entry;
1515 while (data->hdr.cur_entry <= data->hdr.end_entry) {
1516 ghcb_set_sw_scratch(ghcb, (u64)__pa(data));
1518 /* This will advance the shared buffer data points to. */
1519 ret = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_PSC, 0, 0);
1522 * Page State Change VMGEXIT can pass error code through
1525 if (WARN(ret || ghcb->save.sw_exit_info_2,
1526 "SNP: PSC failed ret=%d exit_info_2=%llx\n",
1527 ret, ghcb->save.sw_exit_info_2)) {
1532 /* Verify that reserved bit is not set */
1533 if (WARN(data->hdr.reserved, "Reserved bit is set in the PSC header\n")) {
1539 * Sanity check that entry processing is not going backwards.
1540 * This will happen only if hypervisor is tricking us.
1542 if (WARN(data->hdr.end_entry > end_entry || cur_entry > data->hdr.cur_entry,
1543 "SNP: PSC processing going backward, end_entry %d (got %d) cur_entry %d (got %d)\n",
1544 end_entry, data->hdr.end_entry, cur_entry, data->hdr.cur_entry)) {
1554 static enum es_result vc_check_opcode_bytes(struct es_em_ctxt *ctxt,
1555 unsigned long exit_code)
1557 unsigned int opcode = (unsigned int)ctxt->insn.opcode.value;
1558 u8 modrm = ctxt->insn.modrm.value;
1560 switch (exit_code) {
1564 /* handled separately */
1567 case SVM_EXIT_CPUID:
1568 if (opcode == 0xa20f)
1573 if (opcode == 0x080f)
1577 case SVM_EXIT_MONITOR:
1578 /* MONITOR and MONITORX instructions generate the same error code */
1579 if (opcode == 0x010f && (modrm == 0xc8 || modrm == 0xfa))
1583 case SVM_EXIT_MWAIT:
1584 /* MWAIT and MWAITX instructions generate the same error code */
1585 if (opcode == 0x010f && (modrm == 0xc9 || modrm == 0xfb))
1591 if (opcode == 0x320f ||
1597 case SVM_EXIT_RDPMC:
1598 if (opcode == 0x330f)
1602 case SVM_EXIT_RDTSC:
1603 if (opcode == 0x310f)
1607 case SVM_EXIT_RDTSCP:
1608 if (opcode == 0x010f && modrm == 0xf9)
1612 case SVM_EXIT_READ_DR7:
1613 if (opcode == 0x210f &&
1614 X86_MODRM_REG(ctxt->insn.modrm.value) == 7)
1618 case SVM_EXIT_VMMCALL:
1619 if (opcode == 0x010f && modrm == 0xd9)
1624 case SVM_EXIT_WRITE_DR7:
1625 if (opcode == 0x230f &&
1626 X86_MODRM_REG(ctxt->insn.modrm.value) == 7)
1630 case SVM_EXIT_WBINVD:
1631 if (opcode == 0x90f)
1639 sev_printk(KERN_ERR "Wrong/unhandled opcode bytes: 0x%x, exit_code: 0x%lx, rIP: 0x%lx\n",
1640 opcode, exit_code, ctxt->regs->ip);
1642 return ES_UNSUPPORTED;
1646 * Maintain the GPA of the SVSM Calling Area (CA) in order to utilize the SVSM
1647 * services needed when not running in VMPL0.
1649 static bool __head svsm_setup_ca(const struct cc_blob_sev_info *cc_info)
1651 struct snp_secrets_page *secrets_page;
1652 struct snp_cpuid_table *cpuid_table;
1656 BUILD_BUG_ON(sizeof(*secrets_page) != PAGE_SIZE);
1659 * Check if running at VMPL0.
1661 * Use RMPADJUST (see the rmpadjust() function for a description of what
1662 * the instruction does) to update the VMPL1 permissions of a page. If
1663 * the guest is running at VMPL0, this will succeed and implies there is
1664 * no SVSM. If the guest is running at any other VMPL, this will fail.
1665 * Linux SNP guests only ever run at a single VMPL level so permission mask
1666 * changes of a lesser-privileged VMPL are a don't-care.
1668 * Use a rip-relative reference to obtain the proper address, since this
1669 * routine is running identity mapped when called, both by the decompressor
1670 * code and the early kernel code.
1672 if (!rmpadjust((unsigned long)&RIP_REL_REF(boot_ghcb_page), RMP_PG_SIZE_4K, 1))
1676 * Not running at VMPL0, ensure everything has been properly supplied
1677 * for running under an SVSM.
1679 if (!cc_info || !cc_info->secrets_phys || cc_info->secrets_len != PAGE_SIZE)
1680 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECRETS_PAGE);
1682 secrets_page = (struct snp_secrets_page *)cc_info->secrets_phys;
1683 if (!secrets_page->svsm_size)
1684 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NO_SVSM);
1686 if (!secrets_page->svsm_guest_vmpl)
1687 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_VMPL0);
1689 RIP_REL_REF(snp_vmpl) = secrets_page->svsm_guest_vmpl;
1691 caa = secrets_page->svsm_caa;
1694 * An open-coded PAGE_ALIGNED() in order to avoid including
1695 * kernel-proper headers into the decompressor.
1697 if (caa & (PAGE_SIZE - 1))
1698 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_CAA);
1701 * The CA is identity mapped when this routine is called, both by the
1702 * decompressor code and the early kernel code.
1704 RIP_REL_REF(boot_svsm_caa) = (struct svsm_ca *)caa;
1705 RIP_REL_REF(boot_svsm_caa_pa) = caa;
1707 /* Advertise the SVSM presence via CPUID. */
1708 cpuid_table = (struct snp_cpuid_table *)snp_cpuid_get_table();
1709 for (i = 0; i < cpuid_table->count; i++) {
1710 struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
1712 if (fn->eax_in == 0x8000001f)