1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * AMD Encrypted Register State Support 4 * 5 * Author: Joerg Roedel <jroedel@suse.de> 6 * 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. 10 */ 11 12 #include <asm/setup_data.h> 13 14 #ifndef __BOOT_COMPRESSED 15 #define error(v) pr_err(v) 16 #define has_cpuflag(f) boot_cpu_has(f) 17 #else 18 #undef WARN 19 #define WARN(condition, format...) (!!(condition)) 20 #endif 21 22 /* 23 * SVSM related information: 24 * During boot, the page tables are set up as identity mapped and later 25 * changed to use kernel virtual addresses. Maintain separate virtual and 26 * physical addresses for the CAA to allow SVSM functions to be used during 27 * early boot, both with identity mapped virtual addresses and proper kernel 28 * virtual addresses. 29 */ 30 u64 boot_svsm_caa_pa __ro_after_init; 31 32 /* 33 * Since feature negotiation related variables are set early in the boot 34 * process they must reside in the .data section so as not to be zeroed 35 * out when the .bss section is later cleared. 36 * 37 * GHCB protocol version negotiated with the hypervisor. 38 */ 39 u16 ghcb_version __ro_after_init; 40 41 /* Copy of the SNP firmware's CPUID page. */ 42 static struct snp_cpuid_table cpuid_table_copy __ro_after_init; 43 44 /* 45 * These will be initialized based on CPUID table so that non-present 46 * all-zero leaves (for sparse tables) can be differentiated from 47 * invalid/out-of-range leaves. This is needed since all-zero leaves 48 * still need to be post-processed. 49 */ 50 static u32 cpuid_std_range_max __ro_after_init; 51 static u32 cpuid_hyp_range_max __ro_after_init; 52 static u32 cpuid_ext_range_max __ro_after_init; 53 54 bool sev_snp_needs_sfw; 55 56 void __head __noreturn 57 sev_es_terminate(unsigned int set, unsigned int reason) 58 { 59 u64 val = GHCB_MSR_TERM_REQ; 60 61 /* Tell the hypervisor what went wrong. */ 62 val |= GHCB_SEV_TERM_REASON(set, reason); 63 64 /* Request Guest Termination from Hypervisor */ 65 sev_es_wr_ghcb_msr(val); 66 VMGEXIT(); 67 68 while (true) 69 asm volatile("hlt\n" : : : "memory"); 70 } 71 72 /* 73 * The hypervisor features are available from GHCB version 2 onward. 74 */ 75 u64 get_hv_features(void) 76 { 77 u64 val; 78 79 if (ghcb_version < 2) 80 return 0; 81 82 sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ); 83 VMGEXIT(); 84 85 val = sev_es_rd_ghcb_msr(); 86 if (GHCB_RESP_CODE(val) != GHCB_MSR_HV_FT_RESP) 87 return 0; 88 89 return GHCB_MSR_HV_FT_RESP_VAL(val); 90 } 91 92 int svsm_process_result_codes(struct svsm_call *call) 93 { 94 switch (call->rax_out) { 95 case SVSM_SUCCESS: 96 return 0; 97 case SVSM_ERR_INCOMPLETE: 98 case SVSM_ERR_BUSY: 99 return -EAGAIN; 100 default: 101 return -EINVAL; 102 } 103 } 104 105 /* 106 * Issue a VMGEXIT to call the SVSM: 107 * - Load the SVSM register state (RAX, RCX, RDX, R8 and R9) 108 * - Set the CA call pending field to 1 109 * - Issue VMGEXIT 110 * - Save the SVSM return register state (RAX, RCX, RDX, R8 and R9) 111 * - Perform atomic exchange of the CA call pending field 112 * 113 * - See the "Secure VM Service Module for SEV-SNP Guests" specification for 114 * details on the calling convention. 115 * - The calling convention loosely follows the Microsoft X64 calling 116 * convention by putting arguments in RCX, RDX, R8 and R9. 117 * - RAX specifies the SVSM protocol/callid as input and the return code 118 * as output. 119 */ 120 void svsm_issue_call(struct svsm_call *call, u8 *pending) 121 { 122 register unsigned long rax asm("rax") = call->rax; 123 register unsigned long rcx asm("rcx") = call->rcx; 124 register unsigned long rdx asm("rdx") = call->rdx; 125 register unsigned long r8 asm("r8") = call->r8; 126 register unsigned long r9 asm("r9") = call->r9; 127 128 call->caa->call_pending = 1; 129 130 asm volatile("rep; vmmcall\n\t" 131 : "+r" (rax), "+r" (rcx), "+r" (rdx), "+r" (r8), "+r" (r9) 132 : : "memory"); 133 134 *pending = xchg(&call->caa->call_pending, *pending); 135 136 call->rax_out = rax; 137 call->rcx_out = rcx; 138 call->rdx_out = rdx; 139 call->r8_out = r8; 140 call->r9_out = r9; 141 } 142 143 int svsm_perform_msr_protocol(struct svsm_call *call) 144 { 145 u8 pending = 0; 146 u64 val, resp; 147 148 /* 149 * When using the MSR protocol, be sure to save and restore 150 * the current MSR value. 151 */ 152 val = sev_es_rd_ghcb_msr(); 153 154 sev_es_wr_ghcb_msr(GHCB_MSR_VMPL_REQ_LEVEL(0)); 155 156 svsm_issue_call(call, &pending); 157 158 resp = sev_es_rd_ghcb_msr(); 159 160 sev_es_wr_ghcb_msr(val); 161 162 if (pending) 163 return -EINVAL; 164 165 if (GHCB_RESP_CODE(resp) != GHCB_MSR_VMPL_RESP) 166 return -EINVAL; 167 168 if (GHCB_MSR_VMPL_RESP_VAL(resp)) 169 return -EINVAL; 170 171 return svsm_process_result_codes(call); 172 } 173 174 static int __sev_cpuid_hv(u32 fn, int reg_idx, u32 *reg) 175 { 176 u64 val; 177 178 sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, reg_idx)); 179 VMGEXIT(); 180 val = sev_es_rd_ghcb_msr(); 181 if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) 182 return -EIO; 183 184 *reg = (val >> 32); 185 186 return 0; 187 } 188 189 static int __sev_cpuid_hv_msr(struct cpuid_leaf *leaf) 190 { 191 int ret; 192 193 /* 194 * MSR protocol does not support fetching non-zero subfunctions, but is 195 * sufficient to handle current early-boot cases. Should that change, 196 * make sure to report an error rather than ignoring the index and 197 * grabbing random values. If this issue arises in the future, handling 198 * can be added here to use GHCB-page protocol for cases that occur late 199 * enough in boot that GHCB page is available. 200 */ 201 if (cpuid_function_is_indexed(leaf->fn) && leaf->subfn) 202 return -EINVAL; 203 204 ret = __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EAX, &leaf->eax); 205 ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EBX, &leaf->ebx); 206 ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_ECX, &leaf->ecx); 207 ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EDX, &leaf->edx); 208 209 return ret; 210 } 211 212 213 214 /* 215 * This may be called early while still running on the initial identity 216 * mapping. Use RIP-relative addressing to obtain the correct address 217 * while running with the initial identity mapping as well as the 218 * switch-over to kernel virtual addresses later. 219 */ 220 const struct snp_cpuid_table *snp_cpuid_get_table(void) 221 { 222 return rip_rel_ptr(&cpuid_table_copy); 223 } 224 225 /* 226 * The SNP Firmware ABI, Revision 0.9, Section 7.1, details the use of 227 * XCR0_IN and XSS_IN to encode multiple versions of 0xD subfunctions 0 228 * and 1 based on the corresponding features enabled by a particular 229 * combination of XCR0 and XSS registers so that a guest can look up the 230 * version corresponding to the features currently enabled in its XCR0/XSS 231 * registers. The only values that differ between these versions/table 232 * entries is the enabled XSAVE area size advertised via EBX. 233 * 234 * While hypervisors may choose to make use of this support, it is more 235 * robust/secure for a guest to simply find the entry corresponding to the 236 * base/legacy XSAVE area size (XCR0=1 or XCR0=3), and then calculate the 237 * XSAVE area size using subfunctions 2 through 64, as documented in APM 238 * Volume 3, Rev 3.31, Appendix E.3.8, which is what is done here. 239 * 240 * Since base/legacy XSAVE area size is documented as 0x240, use that value 241 * directly rather than relying on the base size in the CPUID table. 242 * 243 * Return: XSAVE area size on success, 0 otherwise. 244 */ 245 static u32 __head snp_cpuid_calc_xsave_size(u64 xfeatures_en, bool compacted) 246 { 247 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table(); 248 u64 xfeatures_found = 0; 249 u32 xsave_size = 0x240; 250 int i; 251 252 for (i = 0; i < cpuid_table->count; i++) { 253 const struct snp_cpuid_fn *e = &cpuid_table->fn[i]; 254 255 if (!(e->eax_in == 0xD && e->ecx_in > 1 && e->ecx_in < 64)) 256 continue; 257 if (!(xfeatures_en & (BIT_ULL(e->ecx_in)))) 258 continue; 259 if (xfeatures_found & (BIT_ULL(e->ecx_in))) 260 continue; 261 262 xfeatures_found |= (BIT_ULL(e->ecx_in)); 263 264 if (compacted) 265 xsave_size += e->eax; 266 else 267 xsave_size = max(xsave_size, e->eax + e->ebx); 268 } 269 270 /* 271 * Either the guest set unsupported XCR0/XSS bits, or the corresponding 272 * entries in the CPUID table were not present. This is not a valid 273 * state to be in. 274 */ 275 if (xfeatures_found != (xfeatures_en & GENMASK_ULL(63, 2))) 276 return 0; 277 278 return xsave_size; 279 } 280 281 static bool __head 282 snp_cpuid_get_validated_func(struct cpuid_leaf *leaf) 283 { 284 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table(); 285 int i; 286 287 for (i = 0; i < cpuid_table->count; i++) { 288 const struct snp_cpuid_fn *e = &cpuid_table->fn[i]; 289 290 if (e->eax_in != leaf->fn) 291 continue; 292 293 if (cpuid_function_is_indexed(leaf->fn) && e->ecx_in != leaf->subfn) 294 continue; 295 296 /* 297 * For 0xD subfunctions 0 and 1, only use the entry corresponding 298 * to the base/legacy XSAVE area size (XCR0=1 or XCR0=3, XSS=0). 299 * See the comments above snp_cpuid_calc_xsave_size() for more 300 * details. 301 */ 302 if (e->eax_in == 0xD && (e->ecx_in == 0 || e->ecx_in == 1)) 303 if (!(e->xcr0_in == 1 || e->xcr0_in == 3) || e->xss_in) 304 continue; 305 306 leaf->eax = e->eax; 307 leaf->ebx = e->ebx; 308 leaf->ecx = e->ecx; 309 leaf->edx = e->edx; 310 311 return true; 312 } 313 314 return false; 315 } 316 317 static void snp_cpuid_hv_msr(void *ctx, struct cpuid_leaf *leaf) 318 { 319 if (__sev_cpuid_hv_msr(leaf)) 320 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID_HV); 321 } 322 323 static int __head 324 snp_cpuid_postprocess(void (*cpuid_fn)(void *ctx, struct cpuid_leaf *leaf), 325 void *ctx, struct cpuid_leaf *leaf) 326 { 327 struct cpuid_leaf leaf_hv = *leaf; 328 329 switch (leaf->fn) { 330 case 0x1: 331 cpuid_fn(ctx, &leaf_hv); 332 333 /* initial APIC ID */ 334 leaf->ebx = (leaf_hv.ebx & GENMASK(31, 24)) | (leaf->ebx & GENMASK(23, 0)); 335 /* APIC enabled bit */ 336 leaf->edx = (leaf_hv.edx & BIT(9)) | (leaf->edx & ~BIT(9)); 337 338 /* OSXSAVE enabled bit */ 339 if (native_read_cr4() & X86_CR4_OSXSAVE) 340 leaf->ecx |= BIT(27); 341 break; 342 case 0x7: 343 /* OSPKE enabled bit */ 344 leaf->ecx &= ~BIT(4); 345 if (native_read_cr4() & X86_CR4_PKE) 346 leaf->ecx |= BIT(4); 347 break; 348 case 0xB: 349 leaf_hv.subfn = 0; 350 cpuid_fn(ctx, &leaf_hv); 351 352 /* extended APIC ID */ 353 leaf->edx = leaf_hv.edx; 354 break; 355 case 0xD: { 356 bool compacted = false; 357 u64 xcr0 = 1, xss = 0; 358 u32 xsave_size; 359 360 if (leaf->subfn != 0 && leaf->subfn != 1) 361 return 0; 362 363 if (native_read_cr4() & X86_CR4_OSXSAVE) 364 xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); 365 if (leaf->subfn == 1) { 366 /* Get XSS value if XSAVES is enabled. */ 367 if (leaf->eax & BIT(3)) { 368 unsigned long lo, hi; 369 370 asm volatile("rdmsr" : "=a" (lo), "=d" (hi) 371 : "c" (MSR_IA32_XSS)); 372 xss = (hi << 32) | lo; 373 } 374 375 /* 376 * The PPR and APM aren't clear on what size should be 377 * encoded in 0xD:0x1:EBX when compaction is not enabled 378 * by either XSAVEC (feature bit 1) or XSAVES (feature 379 * bit 3) since SNP-capable hardware has these feature 380 * bits fixed as 1. KVM sets it to 0 in this case, but 381 * to avoid this becoming an issue it's safer to simply 382 * treat this as unsupported for SNP guests. 383 */ 384 if (!(leaf->eax & (BIT(1) | BIT(3)))) 385 return -EINVAL; 386 387 compacted = true; 388 } 389 390 xsave_size = snp_cpuid_calc_xsave_size(xcr0 | xss, compacted); 391 if (!xsave_size) 392 return -EINVAL; 393 394 leaf->ebx = xsave_size; 395 } 396 break; 397 case 0x8000001E: 398 cpuid_fn(ctx, &leaf_hv); 399 400 /* extended APIC ID */ 401 leaf->eax = leaf_hv.eax; 402 /* compute ID */ 403 leaf->ebx = (leaf->ebx & GENMASK(31, 8)) | (leaf_hv.ebx & GENMASK(7, 0)); 404 /* node ID */ 405 leaf->ecx = (leaf->ecx & GENMASK(31, 8)) | (leaf_hv.ecx & GENMASK(7, 0)); 406 break; 407 default: 408 /* No fix-ups needed, use values as-is. */ 409 break; 410 } 411 412 return 0; 413 } 414 415 /* 416 * Returns -EOPNOTSUPP if feature not enabled. Any other non-zero return value 417 * should be treated as fatal by caller. 418 */ 419 int __head snp_cpuid(void (*cpuid_fn)(void *ctx, struct cpuid_leaf *leaf), 420 void *ctx, struct cpuid_leaf *leaf) 421 { 422 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table(); 423 424 if (!cpuid_table->count) 425 return -EOPNOTSUPP; 426 427 if (!snp_cpuid_get_validated_func(leaf)) { 428 /* 429 * Some hypervisors will avoid keeping track of CPUID entries 430 * where all values are zero, since they can be handled the 431 * same as out-of-range values (all-zero). This is useful here 432 * as well as it allows virtually all guest configurations to 433 * work using a single SNP CPUID table. 434 * 435 * To allow for this, there is a need to distinguish between 436 * out-of-range entries and in-range zero entries, since the 437 * CPUID table entries are only a template that may need to be 438 * augmented with additional values for things like 439 * CPU-specific information during post-processing. So if it's 440 * not in the table, set the values to zero. Then, if they are 441 * within a valid CPUID range, proceed with post-processing 442 * using zeros as the initial values. Otherwise, skip 443 * post-processing and just return zeros immediately. 444 */ 445 leaf->eax = leaf->ebx = leaf->ecx = leaf->edx = 0; 446 447 /* Skip post-processing for out-of-range zero leafs. */ 448 if (!(leaf->fn <= cpuid_std_range_max || 449 (leaf->fn >= 0x40000000 && leaf->fn <= cpuid_hyp_range_max) || 450 (leaf->fn >= 0x80000000 && leaf->fn <= cpuid_ext_range_max))) 451 return 0; 452 } 453 454 return snp_cpuid_postprocess(cpuid_fn, ctx, leaf); 455 } 456 457 /* 458 * Boot VC Handler - This is the first VC handler during boot, there is no GHCB 459 * page yet, so it only supports the MSR based communication with the 460 * hypervisor and only the CPUID exit-code. 461 */ 462 void __head do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) 463 { 464 unsigned int subfn = lower_bits(regs->cx, 32); 465 unsigned int fn = lower_bits(regs->ax, 32); 466 u16 opcode = *(unsigned short *)regs->ip; 467 struct cpuid_leaf leaf; 468 int ret; 469 470 /* Only CPUID is supported via MSR protocol */ 471 if (exit_code != SVM_EXIT_CPUID) 472 goto fail; 473 474 /* Is it really a CPUID insn? */ 475 if (opcode != 0xa20f) 476 goto fail; 477 478 leaf.fn = fn; 479 leaf.subfn = subfn; 480 481 ret = snp_cpuid(snp_cpuid_hv_msr, NULL, &leaf); 482 if (!ret) 483 goto cpuid_done; 484 485 if (ret != -EOPNOTSUPP) 486 goto fail; 487 488 if (__sev_cpuid_hv_msr(&leaf)) 489 goto fail; 490 491 cpuid_done: 492 regs->ax = leaf.eax; 493 regs->bx = leaf.ebx; 494 regs->cx = leaf.ecx; 495 regs->dx = leaf.edx; 496 497 /* 498 * This is a VC handler and the #VC is only raised when SEV-ES is 499 * active, which means SEV must be active too. Do sanity checks on the 500 * CPUID results to make sure the hypervisor does not trick the kernel 501 * into the no-sev path. This could map sensitive data unencrypted and 502 * make it accessible to the hypervisor. 503 * 504 * In particular, check for: 505 * - Availability of CPUID leaf 0x8000001f 506 * - SEV CPUID bit. 507 * 508 * The hypervisor might still report the wrong C-bit position, but this 509 * can't be checked here. 510 */ 511 512 if (fn == 0x80000000 && (regs->ax < 0x8000001f)) 513 /* SEV leaf check */ 514 goto fail; 515 else if ((fn == 0x8000001f && !(regs->ax & BIT(1)))) 516 /* SEV bit */ 517 goto fail; 518 519 /* Skip over the CPUID two-byte opcode */ 520 regs->ip += 2; 521 522 return; 523 524 fail: 525 /* Terminate the guest */ 526 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); 527 } 528 529 struct cc_setup_data { 530 struct setup_data header; 531 u32 cc_blob_address; 532 }; 533 534 /* 535 * Search for a Confidential Computing blob passed in as a setup_data entry 536 * via the Linux Boot Protocol. 537 */ 538 static __head 539 struct cc_blob_sev_info *find_cc_blob_setup_data(struct boot_params *bp) 540 { 541 struct cc_setup_data *sd = NULL; 542 struct setup_data *hdr; 543 544 hdr = (struct setup_data *)bp->hdr.setup_data; 545 546 while (hdr) { 547 if (hdr->type == SETUP_CC_BLOB) { 548 sd = (struct cc_setup_data *)hdr; 549 return (struct cc_blob_sev_info *)(unsigned long)sd->cc_blob_address; 550 } 551 hdr = (struct setup_data *)hdr->next; 552 } 553 554 return NULL; 555 } 556 557 /* 558 * Initialize the kernel's copy of the SNP CPUID table, and set up the 559 * pointer that will be used to access it. 560 * 561 * Maintaining a direct mapping of the SNP CPUID table used by firmware would 562 * be possible as an alternative, but the approach is brittle since the 563 * mapping needs to be updated in sync with all the changes to virtual memory 564 * layout and related mapping facilities throughout the boot process. 565 */ 566 static void __head setup_cpuid_table(const struct cc_blob_sev_info *cc_info) 567 { 568 const struct snp_cpuid_table *cpuid_table_fw, *cpuid_table; 569 int i; 570 571 if (!cc_info || !cc_info->cpuid_phys || cc_info->cpuid_len < PAGE_SIZE) 572 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID); 573 574 cpuid_table_fw = (const struct snp_cpuid_table *)cc_info->cpuid_phys; 575 if (!cpuid_table_fw->count || cpuid_table_fw->count > SNP_CPUID_COUNT_MAX) 576 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID); 577 578 cpuid_table = snp_cpuid_get_table(); 579 memcpy((void *)cpuid_table, cpuid_table_fw, sizeof(*cpuid_table)); 580 581 /* Initialize CPUID ranges for range-checking. */ 582 for (i = 0; i < cpuid_table->count; i++) { 583 const struct snp_cpuid_fn *fn = &cpuid_table->fn[i]; 584 585 if (fn->eax_in == 0x0) 586 cpuid_std_range_max = fn->eax; 587 else if (fn->eax_in == 0x40000000) 588 cpuid_hyp_range_max = fn->eax; 589 else if (fn->eax_in == 0x80000000) 590 cpuid_ext_range_max = fn->eax; 591 } 592 } 593 594 static int __head svsm_call_msr_protocol(struct svsm_call *call) 595 { 596 int ret; 597 598 do { 599 ret = svsm_perform_msr_protocol(call); 600 } while (ret == -EAGAIN); 601 602 return ret; 603 } 604 605 static void __head svsm_pval_4k_page(unsigned long paddr, bool validate) 606 { 607 struct svsm_pvalidate_call *pc; 608 struct svsm_call call = {}; 609 unsigned long flags; 610 u64 pc_pa; 611 612 /* 613 * This can be called very early in the boot, use native functions in 614 * order to avoid paravirt issues. 615 */ 616 flags = native_local_irq_save(); 617 618 call.caa = svsm_get_caa(); 619 620 pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer; 621 pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer); 622 623 pc->num_entries = 1; 624 pc->cur_index = 0; 625 pc->entry[0].page_size = RMP_PG_SIZE_4K; 626 pc->entry[0].action = validate; 627 pc->entry[0].ignore_cf = 0; 628 pc->entry[0].rsvd = 0; 629 pc->entry[0].pfn = paddr >> PAGE_SHIFT; 630 631 /* Protocol 0, Call ID 1 */ 632 call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE); 633 call.rcx = pc_pa; 634 635 /* 636 * Use the MSR protocol exclusively, so that this code is usable in 637 * startup code where VA/PA translations of the GHCB page's address may 638 * be problematic. 639 */ 640 if (svsm_call_msr_protocol(&call)) 641 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); 642 643 native_local_irq_restore(flags); 644 } 645 646 static void __head pvalidate_4k_page(unsigned long vaddr, unsigned long paddr, 647 bool validate) 648 { 649 int ret; 650 651 if (snp_vmpl) { 652 svsm_pval_4k_page(paddr, validate); 653 } else { 654 ret = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); 655 if (ret) 656 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); 657 } 658 659 /* 660 * If validating memory (making it private) and affected by the 661 * cache-coherency vulnerability, perform the cache eviction mitigation. 662 */ 663 if (validate && sev_snp_needs_sfw) 664 sev_evict_cache((void *)vaddr, 1); 665 } 666 667 /* 668 * Maintain the GPA of the SVSM Calling Area (CA) in order to utilize the SVSM 669 * services needed when not running in VMPL0. 670 */ 671 static bool __head svsm_setup_ca(const struct cc_blob_sev_info *cc_info, 672 void *page) 673 { 674 struct snp_secrets_page *secrets_page; 675 struct snp_cpuid_table *cpuid_table; 676 unsigned int i; 677 u64 caa; 678 679 BUILD_BUG_ON(sizeof(*secrets_page) != PAGE_SIZE); 680 681 /* 682 * Check if running at VMPL0. 683 * 684 * Use RMPADJUST (see the rmpadjust() function for a description of what 685 * the instruction does) to update the VMPL1 permissions of a page. If 686 * the guest is running at VMPL0, this will succeed and implies there is 687 * no SVSM. If the guest is running at any other VMPL, this will fail. 688 * Linux SNP guests only ever run at a single VMPL level so permission mask 689 * changes of a lesser-privileged VMPL are a don't-care. 690 * 691 * Use a rip-relative reference to obtain the proper address, since this 692 * routine is running identity mapped when called, both by the decompressor 693 * code and the early kernel code. 694 */ 695 if (!rmpadjust((unsigned long)page, RMP_PG_SIZE_4K, 1)) 696 return false; 697 698 /* 699 * Not running at VMPL0, ensure everything has been properly supplied 700 * for running under an SVSM. 701 */ 702 if (!cc_info || !cc_info->secrets_phys || cc_info->secrets_len != PAGE_SIZE) 703 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECRETS_PAGE); 704 705 secrets_page = (struct snp_secrets_page *)cc_info->secrets_phys; 706 if (!secrets_page->svsm_size) 707 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NO_SVSM); 708 709 if (!secrets_page->svsm_guest_vmpl) 710 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_VMPL0); 711 712 snp_vmpl = secrets_page->svsm_guest_vmpl; 713 714 caa = secrets_page->svsm_caa; 715 716 /* 717 * An open-coded PAGE_ALIGNED() in order to avoid including 718 * kernel-proper headers into the decompressor. 719 */ 720 if (caa & (PAGE_SIZE - 1)) 721 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_CAA); 722 723 boot_svsm_caa_pa = caa; 724 725 /* Advertise the SVSM presence via CPUID. */ 726 cpuid_table = (struct snp_cpuid_table *)snp_cpuid_get_table(); 727 for (i = 0; i < cpuid_table->count; i++) { 728 struct snp_cpuid_fn *fn = &cpuid_table->fn[i]; 729 730 if (fn->eax_in == 0x8000001f) 731 fn->eax |= BIT(28); 732 } 733 734 return true; 735 } 736