1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Memory Encryption Support 4 * 5 * Copyright (C) 2019 SUSE 6 * 7 * Author: Joerg Roedel <jroedel@suse.de> 8 */ 9 10 #define pr_fmt(fmt) "SEV: " fmt 11 12 #include <linux/sched/debug.h> /* For show_regs() */ 13 #include <linux/percpu-defs.h> 14 #include <linux/cc_platform.h> 15 #include <linux/printk.h> 16 #include <linux/mm_types.h> 17 #include <linux/set_memory.h> 18 #include <linux/memblock.h> 19 #include <linux/kernel.h> 20 #include <linux/mm.h> 21 #include <linux/cpumask.h> 22 #include <linux/efi.h> 23 #include <linux/platform_device.h> 24 #include <linux/io.h> 25 #include <linux/psp-sev.h> 26 #include <linux/dmi.h> 27 #include <uapi/linux/sev-guest.h> 28 #include <crypto/gcm.h> 29 30 #include <asm/init.h> 31 #include <asm/cpu_entry_area.h> 32 #include <asm/stacktrace.h> 33 #include <asm/sev.h> 34 #include <asm/sev-internal.h> 35 #include <asm/insn-eval.h> 36 #include <asm/fpu/xcr.h> 37 #include <asm/processor.h> 38 #include <asm/realmode.h> 39 #include <asm/setup.h> 40 #include <asm/traps.h> 41 #include <asm/svm.h> 42 #include <asm/smp.h> 43 #include <asm/cpu.h> 44 #include <asm/apic.h> 45 #include <asm/cpuid/api.h> 46 #include <asm/cmdline.h> 47 #include <asm/msr.h> 48 49 /* AP INIT values as documented in the APM2 section "Processor Initialization State" */ 50 #define AP_INIT_CS_LIMIT 0xffff 51 #define AP_INIT_DS_LIMIT 0xffff 52 #define AP_INIT_LDTR_LIMIT 0xffff 53 #define AP_INIT_GDTR_LIMIT 0xffff 54 #define AP_INIT_IDTR_LIMIT 0xffff 55 #define AP_INIT_TR_LIMIT 0xffff 56 #define AP_INIT_RFLAGS_DEFAULT 0x2 57 #define AP_INIT_DR6_DEFAULT 0xffff0ff0 58 #define AP_INIT_GPAT_DEFAULT 0x0007040600070406ULL 59 #define AP_INIT_XCR0_DEFAULT 0x1 60 #define AP_INIT_X87_FTW_DEFAULT 0x5555 61 #define AP_INIT_X87_FCW_DEFAULT 0x0040 62 #define AP_INIT_CR0_DEFAULT 0x60000010 63 #define AP_INIT_MXCSR_DEFAULT 0x1f80 64 65 static const char * const sev_status_feat_names[] = { 66 [MSR_AMD64_SEV_ENABLED_BIT] = "SEV", 67 [MSR_AMD64_SEV_ES_ENABLED_BIT] = "SEV-ES", 68 [MSR_AMD64_SEV_SNP_ENABLED_BIT] = "SEV-SNP", 69 [MSR_AMD64_SNP_VTOM_BIT] = "vTom", 70 [MSR_AMD64_SNP_REFLECT_VC_BIT] = "ReflectVC", 71 [MSR_AMD64_SNP_RESTRICTED_INJ_BIT] = "RI", 72 [MSR_AMD64_SNP_ALT_INJ_BIT] = "AI", 73 [MSR_AMD64_SNP_DEBUG_SWAP_BIT] = "DebugSwap", 74 [MSR_AMD64_SNP_PREVENT_HOST_IBS_BIT] = "NoHostIBS", 75 [MSR_AMD64_SNP_BTB_ISOLATION_BIT] = "BTBIsol", 76 [MSR_AMD64_SNP_VMPL_SSS_BIT] = "VmplSSS", 77 [MSR_AMD64_SNP_SECURE_TSC_BIT] = "SecureTSC", 78 [MSR_AMD64_SNP_VMGEXIT_PARAM_BIT] = "VMGExitParam", 79 [MSR_AMD64_SNP_IBS_VIRT_BIT] = "IBSVirt", 80 [MSR_AMD64_SNP_VMSA_REG_PROT_BIT] = "VMSARegProt", 81 [MSR_AMD64_SNP_SMT_PROT_BIT] = "SMTProt", 82 }; 83 84 /* 85 * For Secure TSC guests, the BSP fetches TSC_INFO using SNP guest messaging and 86 * initializes snp_tsc_scale and snp_tsc_offset. These values are replicated 87 * across the APs VMSA fields (TSC_SCALE and TSC_OFFSET). 88 */ 89 static u64 snp_tsc_scale __ro_after_init; 90 static u64 snp_tsc_offset __ro_after_init; 91 static u64 snp_tsc_freq_khz __ro_after_init; 92 93 DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data); 94 DEFINE_PER_CPU(struct sev_es_save_area *, sev_vmsa); 95 96 /* 97 * SVSM related information: 98 * When running under an SVSM, the VMPL that Linux is executing at must be 99 * non-zero. The VMPL is therefore used to indicate the presence of an SVSM. 100 */ 101 u8 snp_vmpl __ro_after_init; 102 EXPORT_SYMBOL_GPL(snp_vmpl); 103 104 static u64 __init get_snp_jump_table_addr(void) 105 { 106 struct snp_secrets_page *secrets; 107 void __iomem *mem; 108 u64 addr; 109 110 mem = ioremap_encrypted(sev_secrets_pa, PAGE_SIZE); 111 if (!mem) { 112 pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n"); 113 return 0; 114 } 115 116 secrets = (__force struct snp_secrets_page *)mem; 117 118 addr = secrets->os_area.ap_jump_table_pa; 119 iounmap(mem); 120 121 return addr; 122 } 123 124 static u64 __init get_jump_table_addr(void) 125 { 126 struct ghcb_state state; 127 unsigned long flags; 128 struct ghcb *ghcb; 129 u64 ret = 0; 130 131 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 132 return get_snp_jump_table_addr(); 133 134 local_irq_save(flags); 135 136 ghcb = __sev_get_ghcb(&state); 137 138 vc_ghcb_invalidate(ghcb); 139 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_JUMP_TABLE); 140 ghcb_set_sw_exit_info_1(ghcb, SVM_VMGEXIT_GET_AP_JUMP_TABLE); 141 ghcb_set_sw_exit_info_2(ghcb, 0); 142 143 sev_es_wr_ghcb_msr(__pa(ghcb)); 144 VMGEXIT(); 145 146 if (ghcb_sw_exit_info_1_is_valid(ghcb) && 147 ghcb_sw_exit_info_2_is_valid(ghcb)) 148 ret = ghcb->save.sw_exit_info_2; 149 150 __sev_put_ghcb(&state); 151 152 local_irq_restore(flags); 153 154 return ret; 155 } 156 157 static inline void __pval_terminate(u64 pfn, bool action, unsigned int page_size, 158 int ret, u64 svsm_ret) 159 { 160 WARN(1, "PVALIDATE failure: pfn: 0x%llx, action: %u, size: %u, ret: %d, svsm_ret: 0x%llx\n", 161 pfn, action, page_size, ret, svsm_ret); 162 163 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); 164 } 165 166 static void svsm_pval_terminate(struct svsm_pvalidate_call *pc, int ret, u64 svsm_ret) 167 { 168 unsigned int page_size; 169 bool action; 170 u64 pfn; 171 172 pfn = pc->entry[pc->cur_index].pfn; 173 action = pc->entry[pc->cur_index].action; 174 page_size = pc->entry[pc->cur_index].page_size; 175 176 __pval_terminate(pfn, action, page_size, ret, svsm_ret); 177 } 178 179 static void pval_pages(struct snp_psc_desc *desc) 180 { 181 struct psc_entry *e; 182 unsigned long vaddr; 183 unsigned int size; 184 unsigned int i; 185 bool validate; 186 u64 pfn; 187 int rc; 188 189 for (i = 0; i <= desc->hdr.end_entry; i++) { 190 e = &desc->entries[i]; 191 192 pfn = e->gfn; 193 vaddr = (unsigned long)pfn_to_kaddr(pfn); 194 size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K; 195 validate = e->operation == SNP_PAGE_STATE_PRIVATE; 196 197 rc = pvalidate(vaddr, size, validate); 198 if (!rc) 199 continue; 200 201 if (rc == PVALIDATE_FAIL_SIZEMISMATCH && size == RMP_PG_SIZE_2M) { 202 unsigned long vaddr_end = vaddr + PMD_SIZE; 203 204 for (; vaddr < vaddr_end; vaddr += PAGE_SIZE, pfn++) { 205 rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); 206 if (rc) 207 __pval_terminate(pfn, validate, RMP_PG_SIZE_4K, rc, 0); 208 } 209 } else { 210 __pval_terminate(pfn, validate, size, rc, 0); 211 } 212 } 213 } 214 215 static u64 svsm_build_ca_from_pfn_range(u64 pfn, u64 pfn_end, bool action, 216 struct svsm_pvalidate_call *pc) 217 { 218 struct svsm_pvalidate_entry *pe; 219 220 /* Nothing in the CA yet */ 221 pc->num_entries = 0; 222 pc->cur_index = 0; 223 224 pe = &pc->entry[0]; 225 226 while (pfn < pfn_end) { 227 pe->page_size = RMP_PG_SIZE_4K; 228 pe->action = action; 229 pe->ignore_cf = 0; 230 pe->pfn = pfn; 231 232 pe++; 233 pfn++; 234 235 pc->num_entries++; 236 if (pc->num_entries == SVSM_PVALIDATE_MAX_COUNT) 237 break; 238 } 239 240 return pfn; 241 } 242 243 static int svsm_build_ca_from_psc_desc(struct snp_psc_desc *desc, unsigned int desc_entry, 244 struct svsm_pvalidate_call *pc) 245 { 246 struct svsm_pvalidate_entry *pe; 247 struct psc_entry *e; 248 249 /* Nothing in the CA yet */ 250 pc->num_entries = 0; 251 pc->cur_index = 0; 252 253 pe = &pc->entry[0]; 254 e = &desc->entries[desc_entry]; 255 256 while (desc_entry <= desc->hdr.end_entry) { 257 pe->page_size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K; 258 pe->action = e->operation == SNP_PAGE_STATE_PRIVATE; 259 pe->ignore_cf = 0; 260 pe->pfn = e->gfn; 261 262 pe++; 263 e++; 264 265 desc_entry++; 266 pc->num_entries++; 267 if (pc->num_entries == SVSM_PVALIDATE_MAX_COUNT) 268 break; 269 } 270 271 return desc_entry; 272 } 273 274 static void svsm_pval_pages(struct snp_psc_desc *desc) 275 { 276 struct svsm_pvalidate_entry pv_4k[VMGEXIT_PSC_MAX_ENTRY]; 277 unsigned int i, pv_4k_count = 0; 278 struct svsm_pvalidate_call *pc; 279 struct svsm_call call = {}; 280 unsigned long flags; 281 bool action; 282 u64 pc_pa; 283 int ret; 284 285 /* 286 * This can be called very early in the boot, use native functions in 287 * order to avoid paravirt issues. 288 */ 289 flags = native_local_irq_save(); 290 291 /* 292 * The SVSM calling area (CA) can support processing 510 entries at a 293 * time. Loop through the Page State Change descriptor until the CA is 294 * full or the last entry in the descriptor is reached, at which time 295 * the SVSM is invoked. This repeats until all entries in the descriptor 296 * are processed. 297 */ 298 call.caa = svsm_get_caa(); 299 300 pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer; 301 pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer); 302 303 /* Protocol 0, Call ID 1 */ 304 call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE); 305 call.rcx = pc_pa; 306 307 for (i = 0; i <= desc->hdr.end_entry;) { 308 i = svsm_build_ca_from_psc_desc(desc, i, pc); 309 310 do { 311 ret = svsm_perform_call_protocol(&call); 312 if (!ret) 313 continue; 314 315 /* 316 * Check if the entry failed because of an RMP mismatch (a 317 * PVALIDATE at 2M was requested, but the page is mapped in 318 * the RMP as 4K). 319 */ 320 321 if (call.rax_out == SVSM_PVALIDATE_FAIL_SIZEMISMATCH && 322 pc->entry[pc->cur_index].page_size == RMP_PG_SIZE_2M) { 323 /* Save this entry for post-processing at 4K */ 324 pv_4k[pv_4k_count++] = pc->entry[pc->cur_index]; 325 326 /* Skip to the next one unless at the end of the list */ 327 pc->cur_index++; 328 if (pc->cur_index < pc->num_entries) 329 ret = -EAGAIN; 330 else 331 ret = 0; 332 } 333 } while (ret == -EAGAIN); 334 335 if (ret) 336 svsm_pval_terminate(pc, ret, call.rax_out); 337 } 338 339 /* Process any entries that failed to be validated at 2M and validate them at 4K */ 340 for (i = 0; i < pv_4k_count; i++) { 341 u64 pfn, pfn_end; 342 343 action = pv_4k[i].action; 344 pfn = pv_4k[i].pfn; 345 pfn_end = pfn + 512; 346 347 while (pfn < pfn_end) { 348 pfn = svsm_build_ca_from_pfn_range(pfn, pfn_end, action, pc); 349 350 ret = svsm_perform_call_protocol(&call); 351 if (ret) 352 svsm_pval_terminate(pc, ret, call.rax_out); 353 } 354 } 355 356 native_local_irq_restore(flags); 357 } 358 359 static void pvalidate_pages(struct snp_psc_desc *desc) 360 { 361 if (snp_vmpl) 362 svsm_pval_pages(desc); 363 else 364 pval_pages(desc); 365 } 366 367 static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc) 368 { 369 int cur_entry, end_entry, ret = 0; 370 struct snp_psc_desc *data; 371 struct es_em_ctxt ctxt; 372 373 vc_ghcb_invalidate(ghcb); 374 375 /* Copy the input desc into GHCB shared buffer */ 376 data = (struct snp_psc_desc *)ghcb->shared_buffer; 377 memcpy(ghcb->shared_buffer, desc, min_t(int, GHCB_SHARED_BUF_SIZE, sizeof(*desc))); 378 379 /* 380 * As per the GHCB specification, the hypervisor can resume the guest 381 * before processing all the entries. Check whether all the entries 382 * are processed. If not, then keep retrying. Note, the hypervisor 383 * will update the data memory directly to indicate the status, so 384 * reference the data->hdr everywhere. 385 * 386 * The strategy here is to wait for the hypervisor to change the page 387 * state in the RMP table before guest accesses the memory pages. If the 388 * page state change was not successful, then later memory access will 389 * result in a crash. 390 */ 391 cur_entry = data->hdr.cur_entry; 392 end_entry = data->hdr.end_entry; 393 394 while (data->hdr.cur_entry <= data->hdr.end_entry) { 395 ghcb_set_sw_scratch(ghcb, (u64)__pa(data)); 396 397 /* This will advance the shared buffer data points to. */ 398 ret = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_PSC, 0, 0); 399 400 /* 401 * Page State Change VMGEXIT can pass error code through 402 * exit_info_2. 403 */ 404 if (WARN(ret || ghcb->save.sw_exit_info_2, 405 "SNP: PSC failed ret=%d exit_info_2=%llx\n", 406 ret, ghcb->save.sw_exit_info_2)) { 407 ret = 1; 408 goto out; 409 } 410 411 /* Verify that reserved bit is not set */ 412 if (WARN(data->hdr.reserved, "Reserved bit is set in the PSC header\n")) { 413 ret = 1; 414 goto out; 415 } 416 417 /* 418 * Sanity check that entry processing is not going backwards. 419 * This will happen only if hypervisor is tricking us. 420 */ 421 if (WARN(data->hdr.end_entry > end_entry || cur_entry > data->hdr.cur_entry, 422 "SNP: PSC processing going backward, end_entry %d (got %d) cur_entry %d (got %d)\n", 423 end_entry, data->hdr.end_entry, cur_entry, data->hdr.cur_entry)) { 424 ret = 1; 425 goto out; 426 } 427 } 428 429 out: 430 return ret; 431 } 432 433 static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, 434 unsigned long vaddr_end, int op) 435 { 436 struct ghcb_state state; 437 bool use_large_entry; 438 struct psc_hdr *hdr; 439 struct psc_entry *e; 440 unsigned long flags; 441 unsigned long pfn; 442 struct ghcb *ghcb; 443 int i; 444 445 hdr = &data->hdr; 446 e = data->entries; 447 448 memset(data, 0, sizeof(*data)); 449 i = 0; 450 451 while (vaddr < vaddr_end && i < ARRAY_SIZE(data->entries)) { 452 hdr->end_entry = i; 453 454 if (is_vmalloc_addr((void *)vaddr)) { 455 pfn = vmalloc_to_pfn((void *)vaddr); 456 use_large_entry = false; 457 } else { 458 pfn = __pa(vaddr) >> PAGE_SHIFT; 459 use_large_entry = true; 460 } 461 462 e->gfn = pfn; 463 e->operation = op; 464 465 if (use_large_entry && IS_ALIGNED(vaddr, PMD_SIZE) && 466 (vaddr_end - vaddr) >= PMD_SIZE) { 467 e->pagesize = RMP_PG_SIZE_2M; 468 vaddr += PMD_SIZE; 469 } else { 470 e->pagesize = RMP_PG_SIZE_4K; 471 vaddr += PAGE_SIZE; 472 } 473 474 e++; 475 i++; 476 } 477 478 /* Page validation must be rescinded before changing to shared */ 479 if (op == SNP_PAGE_STATE_SHARED) 480 pvalidate_pages(data); 481 482 local_irq_save(flags); 483 484 if (sev_cfg.ghcbs_initialized) 485 ghcb = __sev_get_ghcb(&state); 486 else 487 ghcb = boot_ghcb; 488 489 /* Invoke the hypervisor to perform the page state changes */ 490 if (!ghcb || vmgexit_psc(ghcb, data)) 491 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); 492 493 if (sev_cfg.ghcbs_initialized) 494 __sev_put_ghcb(&state); 495 496 local_irq_restore(flags); 497 498 /* Page validation must be performed after changing to private */ 499 if (op == SNP_PAGE_STATE_PRIVATE) 500 pvalidate_pages(data); 501 502 return vaddr; 503 } 504 505 static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) 506 { 507 struct snp_psc_desc desc; 508 unsigned long vaddr_end; 509 510 /* Use the MSR protocol when a GHCB is not available. */ 511 if (!boot_ghcb) 512 return early_set_pages_state(vaddr, __pa(vaddr), npages, op); 513 514 vaddr = vaddr & PAGE_MASK; 515 vaddr_end = vaddr + (npages << PAGE_SHIFT); 516 517 while (vaddr < vaddr_end) 518 vaddr = __set_pages_state(&desc, vaddr, vaddr_end, op); 519 } 520 521 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) 522 { 523 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 524 return; 525 526 set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED); 527 } 528 529 void snp_set_memory_private(unsigned long vaddr, unsigned long npages) 530 { 531 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 532 return; 533 534 set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); 535 } 536 537 void snp_accept_memory(phys_addr_t start, phys_addr_t end) 538 { 539 unsigned long vaddr, npages; 540 541 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 542 return; 543 544 vaddr = (unsigned long)__va(start); 545 npages = (end - start) >> PAGE_SHIFT; 546 547 set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); 548 } 549 550 static int vmgexit_ap_control(u64 event, struct sev_es_save_area *vmsa, u32 apic_id) 551 { 552 bool create = event != SVM_VMGEXIT_AP_DESTROY; 553 struct ghcb_state state; 554 unsigned long flags; 555 struct ghcb *ghcb; 556 int ret = 0; 557 558 local_irq_save(flags); 559 560 ghcb = __sev_get_ghcb(&state); 561 562 vc_ghcb_invalidate(ghcb); 563 564 if (create) 565 ghcb_set_rax(ghcb, vmsa->sev_features); 566 567 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_CREATION); 568 ghcb_set_sw_exit_info_1(ghcb, 569 ((u64)apic_id << 32) | 570 ((u64)snp_vmpl << 16) | 571 event); 572 ghcb_set_sw_exit_info_2(ghcb, __pa(vmsa)); 573 574 sev_es_wr_ghcb_msr(__pa(ghcb)); 575 VMGEXIT(); 576 577 if (!ghcb_sw_exit_info_1_is_valid(ghcb) || 578 lower_32_bits(ghcb->save.sw_exit_info_1)) { 579 pr_err("SNP AP %s error\n", (create ? "CREATE" : "DESTROY")); 580 ret = -EINVAL; 581 } 582 583 __sev_put_ghcb(&state); 584 585 local_irq_restore(flags); 586 587 return ret; 588 } 589 590 static int snp_set_vmsa(void *va, void *caa, int apic_id, bool make_vmsa) 591 { 592 int ret; 593 594 if (snp_vmpl) { 595 struct svsm_call call = {}; 596 unsigned long flags; 597 598 local_irq_save(flags); 599 600 call.caa = this_cpu_read(svsm_caa); 601 call.rcx = __pa(va); 602 603 if (make_vmsa) { 604 /* Protocol 0, Call ID 2 */ 605 call.rax = SVSM_CORE_CALL(SVSM_CORE_CREATE_VCPU); 606 call.rdx = __pa(caa); 607 call.r8 = apic_id; 608 } else { 609 /* Protocol 0, Call ID 3 */ 610 call.rax = SVSM_CORE_CALL(SVSM_CORE_DELETE_VCPU); 611 } 612 613 ret = svsm_perform_call_protocol(&call); 614 615 local_irq_restore(flags); 616 } else { 617 /* 618 * If the kernel runs at VMPL0, it can change the VMSA 619 * bit for a page using the RMPADJUST instruction. 620 * However, for the instruction to succeed it must 621 * target the permissions of a lesser privileged (higher 622 * numbered) VMPL level, so use VMPL1. 623 */ 624 u64 attrs = 1; 625 626 if (make_vmsa) 627 attrs |= RMPADJUST_VMSA_PAGE_BIT; 628 629 ret = rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs); 630 } 631 632 return ret; 633 } 634 635 static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa, int apic_id) 636 { 637 int err; 638 639 err = snp_set_vmsa(vmsa, NULL, apic_id, false); 640 if (err) 641 pr_err("clear VMSA page failed (%u), leaking page\n", err); 642 else 643 free_page((unsigned long)vmsa); 644 } 645 646 static void set_pte_enc(pte_t *kpte, int level, void *va) 647 { 648 struct pte_enc_desc d = { 649 .kpte = kpte, 650 .pte_level = level, 651 .va = va, 652 .encrypt = true 653 }; 654 655 prepare_pte_enc(&d); 656 set_pte_enc_mask(kpte, d.pfn, d.new_pgprot); 657 } 658 659 static void unshare_all_memory(void) 660 { 661 unsigned long addr, end, size, ghcb; 662 struct sev_es_runtime_data *data; 663 unsigned int npages, level; 664 bool skipped_addr; 665 pte_t *pte; 666 int cpu; 667 668 /* Unshare the direct mapping. */ 669 addr = PAGE_OFFSET; 670 end = PAGE_OFFSET + get_max_mapped(); 671 672 while (addr < end) { 673 pte = lookup_address(addr, &level); 674 size = page_level_size(level); 675 npages = size / PAGE_SIZE; 676 skipped_addr = false; 677 678 if (!pte || !pte_decrypted(*pte) || pte_none(*pte)) { 679 addr += size; 680 continue; 681 } 682 683 /* 684 * Ensure that all the per-CPU GHCBs are made private at the 685 * end of the unsharing loop so that the switch to the slower 686 * MSR protocol happens last. 687 */ 688 for_each_possible_cpu(cpu) { 689 data = per_cpu(runtime_data, cpu); 690 ghcb = (unsigned long)&data->ghcb_page; 691 692 /* Handle the case of a huge page containing the GHCB page */ 693 if (addr <= ghcb && ghcb < addr + size) { 694 skipped_addr = true; 695 break; 696 } 697 } 698 699 if (!skipped_addr) { 700 set_pte_enc(pte, level, (void *)addr); 701 snp_set_memory_private(addr, npages); 702 } 703 addr += size; 704 } 705 706 /* Unshare all bss decrypted memory. */ 707 addr = (unsigned long)__start_bss_decrypted; 708 end = (unsigned long)__start_bss_decrypted_unused; 709 npages = (end - addr) >> PAGE_SHIFT; 710 711 for (; addr < end; addr += PAGE_SIZE) { 712 pte = lookup_address(addr, &level); 713 if (!pte || !pte_decrypted(*pte) || pte_none(*pte)) 714 continue; 715 716 set_pte_enc(pte, level, (void *)addr); 717 } 718 addr = (unsigned long)__start_bss_decrypted; 719 snp_set_memory_private(addr, npages); 720 721 __flush_tlb_all(); 722 } 723 724 /* Stop new private<->shared conversions */ 725 void snp_kexec_begin(void) 726 { 727 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 728 return; 729 730 if (!IS_ENABLED(CONFIG_KEXEC_CORE)) 731 return; 732 733 /* 734 * Crash kernel ends up here with interrupts disabled: can't wait for 735 * conversions to finish. 736 * 737 * If race happened, just report and proceed. 738 */ 739 if (!set_memory_enc_stop_conversion()) 740 pr_warn("Failed to stop shared<->private conversions\n"); 741 } 742 743 /* 744 * Shutdown all APs except the one handling kexec/kdump and clearing 745 * the VMSA tag on AP's VMSA pages as they are not being used as 746 * VMSA page anymore. 747 */ 748 static void shutdown_all_aps(void) 749 { 750 struct sev_es_save_area *vmsa; 751 int apic_id, this_cpu, cpu; 752 753 this_cpu = get_cpu(); 754 755 /* 756 * APs are already in HLT loop when enc_kexec_finish() callback 757 * is invoked. 758 */ 759 for_each_present_cpu(cpu) { 760 vmsa = per_cpu(sev_vmsa, cpu); 761 762 /* 763 * The BSP or offlined APs do not have guest allocated VMSA 764 * and there is no need to clear the VMSA tag for this page. 765 */ 766 if (!vmsa) 767 continue; 768 769 /* 770 * Cannot clear the VMSA tag for the currently running vCPU. 771 */ 772 if (this_cpu == cpu) { 773 unsigned long pa; 774 struct page *p; 775 776 pa = __pa(vmsa); 777 /* 778 * Mark the VMSA page of the running vCPU as offline 779 * so that is excluded and not touched by makedumpfile 780 * while generating vmcore during kdump. 781 */ 782 p = pfn_to_online_page(pa >> PAGE_SHIFT); 783 if (p) 784 __SetPageOffline(p); 785 continue; 786 } 787 788 apic_id = cpuid_to_apicid[cpu]; 789 790 /* 791 * Issue AP destroy to ensure AP gets kicked out of guest mode 792 * to allow using RMPADJUST to remove the VMSA tag on it's 793 * VMSA page. 794 */ 795 vmgexit_ap_control(SVM_VMGEXIT_AP_DESTROY, vmsa, apic_id); 796 snp_cleanup_vmsa(vmsa, apic_id); 797 } 798 799 put_cpu(); 800 } 801 802 void snp_kexec_finish(void) 803 { 804 struct sev_es_runtime_data *data; 805 unsigned long size, addr; 806 unsigned int level, cpu; 807 struct ghcb *ghcb; 808 pte_t *pte; 809 810 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 811 return; 812 813 if (!IS_ENABLED(CONFIG_KEXEC_CORE)) 814 return; 815 816 shutdown_all_aps(); 817 818 unshare_all_memory(); 819 820 /* 821 * Switch to using the MSR protocol to change per-CPU GHCBs to 822 * private. All the per-CPU GHCBs have been switched back to private, 823 * so can't do any more GHCB calls to the hypervisor beyond this point 824 * until the kexec'ed kernel starts running. 825 */ 826 boot_ghcb = NULL; 827 sev_cfg.ghcbs_initialized = false; 828 829 for_each_possible_cpu(cpu) { 830 data = per_cpu(runtime_data, cpu); 831 ghcb = &data->ghcb_page; 832 pte = lookup_address((unsigned long)ghcb, &level); 833 size = page_level_size(level); 834 /* Handle the case of a huge page containing the GHCB page */ 835 addr = (unsigned long)ghcb & page_level_mask(level); 836 set_pte_enc(pte, level, (void *)addr); 837 snp_set_memory_private(addr, (size / PAGE_SIZE)); 838 } 839 } 840 841 #define __ATTR_BASE (SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK) 842 #define INIT_CS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK) 843 #define INIT_DS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_WRITE_MASK) 844 845 #define INIT_LDTR_ATTRIBS (SVM_SELECTOR_P_MASK | 2) 846 #define INIT_TR_ATTRIBS (SVM_SELECTOR_P_MASK | 3) 847 848 static void *snp_alloc_vmsa_page(int cpu) 849 { 850 struct page *p; 851 852 /* 853 * Allocate VMSA page to work around the SNP erratum where the CPU will 854 * incorrectly signal an RMP violation #PF if a large page (2MB or 1GB) 855 * collides with the RMP entry of VMSA page. The recommended workaround 856 * is to not use a large page. 857 * 858 * Allocate an 8k page which is also 8k-aligned. 859 */ 860 p = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1); 861 if (!p) 862 return NULL; 863 864 split_page(p, 1); 865 866 /* Free the first 4k. This page may be 2M/1G aligned and cannot be used. */ 867 __free_page(p); 868 869 return page_address(p + 1); 870 } 871 872 static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip) 873 { 874 struct sev_es_save_area *cur_vmsa, *vmsa; 875 struct svsm_ca *caa; 876 u8 sipi_vector; 877 int cpu, ret; 878 u64 cr4; 879 880 /* 881 * The hypervisor SNP feature support check has happened earlier, just check 882 * the AP_CREATION one here. 883 */ 884 if (!(sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION)) 885 return -EOPNOTSUPP; 886 887 /* 888 * Verify the desired start IP against the known trampoline start IP 889 * to catch any future new trampolines that may be introduced that 890 * would require a new protected guest entry point. 891 */ 892 if (WARN_ONCE(start_ip != real_mode_header->trampoline_start, 893 "Unsupported SNP start_ip: %lx\n", start_ip)) 894 return -EINVAL; 895 896 /* Override start_ip with known protected guest start IP */ 897 start_ip = real_mode_header->sev_es_trampoline_start; 898 899 /* Find the logical CPU for the APIC ID */ 900 for_each_present_cpu(cpu) { 901 if (arch_match_cpu_phys_id(cpu, apic_id)) 902 break; 903 } 904 if (cpu >= nr_cpu_ids) 905 return -EINVAL; 906 907 cur_vmsa = per_cpu(sev_vmsa, cpu); 908 909 /* 910 * A new VMSA is created each time because there is no guarantee that 911 * the current VMSA is the kernels or that the vCPU is not running. If 912 * an attempt was done to use the current VMSA with a running vCPU, a 913 * #VMEXIT of that vCPU would wipe out all of the settings being done 914 * here. 915 */ 916 vmsa = (struct sev_es_save_area *)snp_alloc_vmsa_page(cpu); 917 if (!vmsa) 918 return -ENOMEM; 919 920 /* If an SVSM is present, the SVSM per-CPU CAA will be !NULL */ 921 caa = per_cpu(svsm_caa, cpu); 922 923 /* CR4 should maintain the MCE value */ 924 cr4 = native_read_cr4() & X86_CR4_MCE; 925 926 /* Set the CS value based on the start_ip converted to a SIPI vector */ 927 sipi_vector = (start_ip >> 12); 928 vmsa->cs.base = sipi_vector << 12; 929 vmsa->cs.limit = AP_INIT_CS_LIMIT; 930 vmsa->cs.attrib = INIT_CS_ATTRIBS; 931 vmsa->cs.selector = sipi_vector << 8; 932 933 /* Set the RIP value based on start_ip */ 934 vmsa->rip = start_ip & 0xfff; 935 936 /* Set AP INIT defaults as documented in the APM */ 937 vmsa->ds.limit = AP_INIT_DS_LIMIT; 938 vmsa->ds.attrib = INIT_DS_ATTRIBS; 939 vmsa->es = vmsa->ds; 940 vmsa->fs = vmsa->ds; 941 vmsa->gs = vmsa->ds; 942 vmsa->ss = vmsa->ds; 943 944 vmsa->gdtr.limit = AP_INIT_GDTR_LIMIT; 945 vmsa->ldtr.limit = AP_INIT_LDTR_LIMIT; 946 vmsa->ldtr.attrib = INIT_LDTR_ATTRIBS; 947 vmsa->idtr.limit = AP_INIT_IDTR_LIMIT; 948 vmsa->tr.limit = AP_INIT_TR_LIMIT; 949 vmsa->tr.attrib = INIT_TR_ATTRIBS; 950 951 vmsa->cr4 = cr4; 952 vmsa->cr0 = AP_INIT_CR0_DEFAULT; 953 vmsa->dr7 = DR7_RESET_VALUE; 954 vmsa->dr6 = AP_INIT_DR6_DEFAULT; 955 vmsa->rflags = AP_INIT_RFLAGS_DEFAULT; 956 vmsa->g_pat = AP_INIT_GPAT_DEFAULT; 957 vmsa->xcr0 = AP_INIT_XCR0_DEFAULT; 958 vmsa->mxcsr = AP_INIT_MXCSR_DEFAULT; 959 vmsa->x87_ftw = AP_INIT_X87_FTW_DEFAULT; 960 vmsa->x87_fcw = AP_INIT_X87_FCW_DEFAULT; 961 962 /* SVME must be set. */ 963 vmsa->efer = EFER_SVME; 964 965 /* 966 * Set the SNP-specific fields for this VMSA: 967 * VMPL level 968 * SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits) 969 */ 970 vmsa->vmpl = snp_vmpl; 971 vmsa->sev_features = sev_status >> 2; 972 973 /* Populate AP's TSC scale/offset to get accurate TSC values. */ 974 if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) { 975 vmsa->tsc_scale = snp_tsc_scale; 976 vmsa->tsc_offset = snp_tsc_offset; 977 } 978 979 /* Switch the page over to a VMSA page now that it is initialized */ 980 ret = snp_set_vmsa(vmsa, caa, apic_id, true); 981 if (ret) { 982 pr_err("set VMSA page failed (%u)\n", ret); 983 free_page((unsigned long)vmsa); 984 985 return -EINVAL; 986 } 987 988 /* Issue VMGEXIT AP Creation NAE event */ 989 ret = vmgexit_ap_control(SVM_VMGEXIT_AP_CREATE, vmsa, apic_id); 990 if (ret) { 991 snp_cleanup_vmsa(vmsa, apic_id); 992 vmsa = NULL; 993 } 994 995 /* Free up any previous VMSA page */ 996 if (cur_vmsa) 997 snp_cleanup_vmsa(cur_vmsa, apic_id); 998 999 /* Record the current VMSA page */ 1000 per_cpu(sev_vmsa, cpu) = vmsa; 1001 1002 return ret; 1003 } 1004 1005 void __init snp_set_wakeup_secondary_cpu(void) 1006 { 1007 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 1008 return; 1009 1010 /* 1011 * Always set this override if SNP is enabled. This makes it the 1012 * required method to start APs under SNP. If the hypervisor does 1013 * not support AP creation, then no APs will be started. 1014 */ 1015 apic_update_callback(wakeup_secondary_cpu, wakeup_cpu_via_vmgexit); 1016 } 1017 1018 int __init sev_es_setup_ap_jump_table(struct real_mode_header *rmh) 1019 { 1020 u16 startup_cs, startup_ip; 1021 phys_addr_t jump_table_pa; 1022 u64 jump_table_addr; 1023 u16 __iomem *jump_table; 1024 1025 jump_table_addr = get_jump_table_addr(); 1026 1027 /* On UP guests there is no jump table so this is not a failure */ 1028 if (!jump_table_addr) 1029 return 0; 1030 1031 /* Check if AP Jump Table is page-aligned */ 1032 if (jump_table_addr & ~PAGE_MASK) 1033 return -EINVAL; 1034 1035 jump_table_pa = jump_table_addr & PAGE_MASK; 1036 1037 startup_cs = (u16)(rmh->trampoline_start >> 4); 1038 startup_ip = (u16)(rmh->sev_es_trampoline_start - 1039 rmh->trampoline_start); 1040 1041 jump_table = ioremap_encrypted(jump_table_pa, PAGE_SIZE); 1042 if (!jump_table) 1043 return -EIO; 1044 1045 writew(startup_ip, &jump_table[0]); 1046 writew(startup_cs, &jump_table[1]); 1047 1048 iounmap(jump_table); 1049 1050 return 0; 1051 } 1052 1053 /* 1054 * This is needed by the OVMF UEFI firmware which will use whatever it finds in 1055 * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu 1056 * runtime GHCBs used by the kernel are also mapped in the EFI page-table. 1057 */ 1058 int __init sev_es_efi_map_ghcbs(pgd_t *pgd) 1059 { 1060 struct sev_es_runtime_data *data; 1061 unsigned long address, pflags; 1062 int cpu; 1063 u64 pfn; 1064 1065 if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) 1066 return 0; 1067 1068 pflags = _PAGE_NX | _PAGE_RW; 1069 1070 for_each_possible_cpu(cpu) { 1071 data = per_cpu(runtime_data, cpu); 1072 1073 address = __pa(&data->ghcb_page); 1074 pfn = address >> PAGE_SHIFT; 1075 1076 if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags)) 1077 return 1; 1078 } 1079 1080 return 0; 1081 } 1082 1083 static void snp_register_per_cpu_ghcb(void) 1084 { 1085 struct sev_es_runtime_data *data; 1086 struct ghcb *ghcb; 1087 1088 data = this_cpu_read(runtime_data); 1089 ghcb = &data->ghcb_page; 1090 1091 snp_register_ghcb_early(__pa(ghcb)); 1092 } 1093 1094 void setup_ghcb(void) 1095 { 1096 if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) 1097 return; 1098 1099 /* 1100 * Check whether the runtime #VC exception handler is active. It uses 1101 * the per-CPU GHCB page which is set up by sev_es_init_vc_handling(). 1102 * 1103 * If SNP is active, register the per-CPU GHCB page so that the runtime 1104 * exception handler can use it. 1105 */ 1106 if (initial_vc_handler == (unsigned long)kernel_exc_vmm_communication) { 1107 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 1108 snp_register_per_cpu_ghcb(); 1109 1110 sev_cfg.ghcbs_initialized = true; 1111 1112 return; 1113 } 1114 1115 /* 1116 * Make sure the hypervisor talks a supported protocol. 1117 * This gets called only in the BSP boot phase. 1118 */ 1119 if (!sev_es_negotiate_protocol()) 1120 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); 1121 1122 /* 1123 * Clear the boot_ghcb. The first exception comes in before the bss 1124 * section is cleared. 1125 */ 1126 memset(&boot_ghcb_page, 0, PAGE_SIZE); 1127 1128 /* Alright - Make the boot-ghcb public */ 1129 boot_ghcb = &boot_ghcb_page; 1130 1131 /* SNP guest requires that GHCB GPA must be registered. */ 1132 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 1133 snp_register_ghcb_early(__pa(&boot_ghcb_page)); 1134 } 1135 1136 #ifdef CONFIG_HOTPLUG_CPU 1137 static void sev_es_ap_hlt_loop(void) 1138 { 1139 struct ghcb_state state; 1140 struct ghcb *ghcb; 1141 1142 ghcb = __sev_get_ghcb(&state); 1143 1144 while (true) { 1145 vc_ghcb_invalidate(ghcb); 1146 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_HLT_LOOP); 1147 ghcb_set_sw_exit_info_1(ghcb, 0); 1148 ghcb_set_sw_exit_info_2(ghcb, 0); 1149 1150 sev_es_wr_ghcb_msr(__pa(ghcb)); 1151 VMGEXIT(); 1152 1153 /* Wakeup signal? */ 1154 if (ghcb_sw_exit_info_2_is_valid(ghcb) && 1155 ghcb->save.sw_exit_info_2) 1156 break; 1157 } 1158 1159 __sev_put_ghcb(&state); 1160 } 1161 1162 /* 1163 * Play_dead handler when running under SEV-ES. This is needed because 1164 * the hypervisor can't deliver an SIPI request to restart the AP. 1165 * Instead the kernel has to issue a VMGEXIT to halt the VCPU until the 1166 * hypervisor wakes it up again. 1167 */ 1168 static void sev_es_play_dead(void) 1169 { 1170 play_dead_common(); 1171 1172 /* IRQs now disabled */ 1173 1174 sev_es_ap_hlt_loop(); 1175 1176 /* 1177 * If we get here, the VCPU was woken up again. Jump to CPU 1178 * startup code to get it back online. 1179 */ 1180 soft_restart_cpu(); 1181 } 1182 #else /* CONFIG_HOTPLUG_CPU */ 1183 #define sev_es_play_dead native_play_dead 1184 #endif /* CONFIG_HOTPLUG_CPU */ 1185 1186 #ifdef CONFIG_SMP 1187 static void __init sev_es_setup_play_dead(void) 1188 { 1189 smp_ops.play_dead = sev_es_play_dead; 1190 } 1191 #else 1192 static inline void sev_es_setup_play_dead(void) { } 1193 #endif 1194 1195 static void __init alloc_runtime_data(int cpu) 1196 { 1197 struct sev_es_runtime_data *data; 1198 1199 data = memblock_alloc_node(sizeof(*data), PAGE_SIZE, cpu_to_node(cpu)); 1200 if (!data) 1201 panic("Can't allocate SEV-ES runtime data"); 1202 1203 per_cpu(runtime_data, cpu) = data; 1204 1205 if (snp_vmpl) { 1206 struct svsm_ca *caa; 1207 1208 /* Allocate the SVSM CA page if an SVSM is present */ 1209 caa = memblock_alloc_or_panic(sizeof(*caa), PAGE_SIZE); 1210 1211 per_cpu(svsm_caa, cpu) = caa; 1212 per_cpu(svsm_caa_pa, cpu) = __pa(caa); 1213 } 1214 } 1215 1216 static void __init init_ghcb(int cpu) 1217 { 1218 struct sev_es_runtime_data *data; 1219 int err; 1220 1221 data = per_cpu(runtime_data, cpu); 1222 1223 err = early_set_memory_decrypted((unsigned long)&data->ghcb_page, 1224 sizeof(data->ghcb_page)); 1225 if (err) 1226 panic("Can't map GHCBs unencrypted"); 1227 1228 memset(&data->ghcb_page, 0, sizeof(data->ghcb_page)); 1229 1230 data->ghcb_active = false; 1231 data->backup_ghcb_active = false; 1232 } 1233 1234 void __init sev_es_init_vc_handling(void) 1235 { 1236 int cpu; 1237 1238 BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE); 1239 1240 if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) 1241 return; 1242 1243 if (!sev_es_check_cpu_features()) 1244 panic("SEV-ES CPU Features missing"); 1245 1246 /* 1247 * SNP is supported in v2 of the GHCB spec which mandates support for HV 1248 * features. 1249 */ 1250 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { 1251 sev_hv_features = get_hv_features(); 1252 1253 if (!(sev_hv_features & GHCB_HV_FT_SNP)) 1254 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); 1255 } 1256 1257 /* Initialize per-cpu GHCB pages */ 1258 for_each_possible_cpu(cpu) { 1259 alloc_runtime_data(cpu); 1260 init_ghcb(cpu); 1261 } 1262 1263 /* If running under an SVSM, switch to the per-cpu CA */ 1264 if (snp_vmpl) { 1265 struct svsm_call call = {}; 1266 unsigned long flags; 1267 int ret; 1268 1269 local_irq_save(flags); 1270 1271 /* 1272 * SVSM_CORE_REMAP_CA call: 1273 * RAX = 0 (Protocol=0, CallID=0) 1274 * RCX = New CA GPA 1275 */ 1276 call.caa = svsm_get_caa(); 1277 call.rax = SVSM_CORE_CALL(SVSM_CORE_REMAP_CA); 1278 call.rcx = this_cpu_read(svsm_caa_pa); 1279 ret = svsm_perform_call_protocol(&call); 1280 if (ret) 1281 panic("Can't remap the SVSM CA, ret=%d, rax_out=0x%llx\n", 1282 ret, call.rax_out); 1283 1284 sev_cfg.use_cas = true; 1285 1286 local_irq_restore(flags); 1287 } 1288 1289 sev_es_setup_play_dead(); 1290 1291 /* Secondary CPUs use the runtime #VC handler */ 1292 initial_vc_handler = (unsigned long)kernel_exc_vmm_communication; 1293 } 1294 1295 /* 1296 * SEV-SNP guests should only execute dmi_setup() if EFI_CONFIG_TABLES are 1297 * enabled, as the alternative (fallback) logic for DMI probing in the legacy 1298 * ROM region can cause a crash since this region is not pre-validated. 1299 */ 1300 void __init snp_dmi_setup(void) 1301 { 1302 if (efi_enabled(EFI_CONFIG_TABLES)) 1303 dmi_setup(); 1304 } 1305 1306 static void dump_cpuid_table(void) 1307 { 1308 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table(); 1309 int i = 0; 1310 1311 pr_info("count=%d reserved=0x%x reserved2=0x%llx\n", 1312 cpuid_table->count, cpuid_table->__reserved1, cpuid_table->__reserved2); 1313 1314 for (i = 0; i < SNP_CPUID_COUNT_MAX; i++) { 1315 const struct snp_cpuid_fn *fn = &cpuid_table->fn[i]; 1316 1317 pr_info("index=%3d fn=0x%08x subfn=0x%08x: eax=0x%08x ebx=0x%08x ecx=0x%08x edx=0x%08x xcr0_in=0x%016llx xss_in=0x%016llx reserved=0x%016llx\n", 1318 i, fn->eax_in, fn->ecx_in, fn->eax, fn->ebx, fn->ecx, 1319 fn->edx, fn->xcr0_in, fn->xss_in, fn->__reserved); 1320 } 1321 } 1322 1323 /* 1324 * It is useful from an auditing/testing perspective to provide an easy way 1325 * for the guest owner to know that the CPUID table has been initialized as 1326 * expected, but that initialization happens too early in boot to print any 1327 * sort of indicator, and there's not really any other good place to do it, 1328 * so do it here. 1329 * 1330 * If running as an SNP guest, report the current VM privilege level (VMPL). 1331 */ 1332 static int __init report_snp_info(void) 1333 { 1334 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table(); 1335 1336 if (cpuid_table->count) { 1337 pr_info("Using SNP CPUID table, %d entries present.\n", 1338 cpuid_table->count); 1339 1340 if (sev_cfg.debug) 1341 dump_cpuid_table(); 1342 } 1343 1344 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 1345 pr_info("SNP running at VMPL%u.\n", snp_vmpl); 1346 1347 return 0; 1348 } 1349 arch_initcall(report_snp_info); 1350 1351 static void update_attest_input(struct svsm_call *call, struct svsm_attest_call *input) 1352 { 1353 /* If (new) lengths have been returned, propagate them up */ 1354 if (call->rcx_out != call->rcx) 1355 input->manifest_buf.len = call->rcx_out; 1356 1357 if (call->rdx_out != call->rdx) 1358 input->certificates_buf.len = call->rdx_out; 1359 1360 if (call->r8_out != call->r8) 1361 input->report_buf.len = call->r8_out; 1362 } 1363 1364 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, 1365 struct svsm_attest_call *input) 1366 { 1367 struct svsm_attest_call *ac; 1368 unsigned long flags; 1369 u64 attest_call_pa; 1370 int ret; 1371 1372 if (!snp_vmpl) 1373 return -EINVAL; 1374 1375 local_irq_save(flags); 1376 1377 call->caa = svsm_get_caa(); 1378 1379 ac = (struct svsm_attest_call *)call->caa->svsm_buffer; 1380 attest_call_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer); 1381 1382 *ac = *input; 1383 1384 /* 1385 * Set input registers for the request and set RDX and R8 to known 1386 * values in order to detect length values being returned in them. 1387 */ 1388 call->rax = call_id; 1389 call->rcx = attest_call_pa; 1390 call->rdx = -1; 1391 call->r8 = -1; 1392 ret = svsm_perform_call_protocol(call); 1393 update_attest_input(call, input); 1394 1395 local_irq_restore(flags); 1396 1397 return ret; 1398 } 1399 EXPORT_SYMBOL_GPL(snp_issue_svsm_attest_req); 1400 1401 static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, 1402 struct snp_guest_request_ioctl *rio) 1403 { 1404 struct ghcb_state state; 1405 struct es_em_ctxt ctxt; 1406 unsigned long flags; 1407 struct ghcb *ghcb; 1408 int ret; 1409 1410 rio->exitinfo2 = SEV_RET_NO_FW_CALL; 1411 1412 /* 1413 * __sev_get_ghcb() needs to run with IRQs disabled because it is using 1414 * a per-CPU GHCB. 1415 */ 1416 local_irq_save(flags); 1417 1418 ghcb = __sev_get_ghcb(&state); 1419 if (!ghcb) { 1420 ret = -EIO; 1421 goto e_restore_irq; 1422 } 1423 1424 vc_ghcb_invalidate(ghcb); 1425 1426 if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) { 1427 ghcb_set_rax(ghcb, input->data_gpa); 1428 ghcb_set_rbx(ghcb, input->data_npages); 1429 } 1430 1431 ret = sev_es_ghcb_hv_call(ghcb, &ctxt, req->exit_code, input->req_gpa, input->resp_gpa); 1432 if (ret) 1433 goto e_put; 1434 1435 rio->exitinfo2 = ghcb->save.sw_exit_info_2; 1436 switch (rio->exitinfo2) { 1437 case 0: 1438 break; 1439 1440 case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_BUSY): 1441 ret = -EAGAIN; 1442 break; 1443 1444 case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN): 1445 /* Number of expected pages are returned in RBX */ 1446 if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) { 1447 input->data_npages = ghcb_get_rbx(ghcb); 1448 ret = -ENOSPC; 1449 break; 1450 } 1451 fallthrough; 1452 default: 1453 ret = -EIO; 1454 break; 1455 } 1456 1457 e_put: 1458 __sev_put_ghcb(&state); 1459 e_restore_irq: 1460 local_irq_restore(flags); 1461 1462 return ret; 1463 } 1464 1465 /** 1466 * snp_svsm_vtpm_probe() - Probe if SVSM provides a vTPM device 1467 * 1468 * Check that there is SVSM and that it supports at least TPM_SEND_COMMAND 1469 * which is the only request used so far. 1470 * 1471 * Return: true if the platform provides a vTPM SVSM device, false otherwise. 1472 */ 1473 static bool snp_svsm_vtpm_probe(void) 1474 { 1475 struct svsm_call call = {}; 1476 1477 /* The vTPM device is available only if a SVSM is present */ 1478 if (!snp_vmpl) 1479 return false; 1480 1481 call.caa = svsm_get_caa(); 1482 call.rax = SVSM_VTPM_CALL(SVSM_VTPM_QUERY); 1483 1484 if (svsm_perform_call_protocol(&call)) 1485 return false; 1486 1487 /* Check platform commands contains TPM_SEND_COMMAND - platform command 8 */ 1488 return call.rcx_out & BIT_ULL(8); 1489 } 1490 1491 /** 1492 * snp_svsm_vtpm_send_command() - Execute a vTPM operation on SVSM 1493 * @buffer: A buffer used to both send the command and receive the response. 1494 * 1495 * Execute a SVSM_VTPM_CMD call as defined by 1496 * "Secure VM Service Module for SEV-SNP Guests" Publication # 58019 Revision: 1.00 1497 * 1498 * All command request/response buffers have a common structure as specified by 1499 * the following table: 1500 * Byte Size In/Out Description 1501 * Offset (Bytes) 1502 * 0x000 4 In Platform command 1503 * Out Platform command response size 1504 * 1505 * Each command can build upon this common request/response structure to create 1506 * a structure specific to the command. See include/linux/tpm_svsm.h for more 1507 * details. 1508 * 1509 * Return: 0 on success, -errno on failure 1510 */ 1511 int snp_svsm_vtpm_send_command(u8 *buffer) 1512 { 1513 struct svsm_call call = {}; 1514 1515 call.caa = svsm_get_caa(); 1516 call.rax = SVSM_VTPM_CALL(SVSM_VTPM_CMD); 1517 call.rcx = __pa(buffer); 1518 1519 return svsm_perform_call_protocol(&call); 1520 } 1521 EXPORT_SYMBOL_GPL(snp_svsm_vtpm_send_command); 1522 1523 static struct platform_device sev_guest_device = { 1524 .name = "sev-guest", 1525 .id = -1, 1526 }; 1527 1528 static struct platform_device tpm_svsm_device = { 1529 .name = "tpm-svsm", 1530 .id = -1, 1531 }; 1532 1533 static int __init snp_init_platform_device(void) 1534 { 1535 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 1536 return -ENODEV; 1537 1538 if (platform_device_register(&sev_guest_device)) 1539 return -ENODEV; 1540 1541 if (snp_svsm_vtpm_probe() && 1542 platform_device_register(&tpm_svsm_device)) 1543 return -ENODEV; 1544 1545 pr_info("SNP guest platform devices initialized.\n"); 1546 return 0; 1547 } 1548 device_initcall(snp_init_platform_device); 1549 1550 void sev_show_status(void) 1551 { 1552 int i; 1553 1554 pr_info("Status: "); 1555 for (i = 0; i < MSR_AMD64_SNP_RESV_BIT; i++) { 1556 if (sev_status & BIT_ULL(i)) { 1557 if (!sev_status_feat_names[i]) 1558 continue; 1559 1560 pr_cont("%s ", sev_status_feat_names[i]); 1561 } 1562 } 1563 pr_cont("\n"); 1564 } 1565 1566 void __init snp_update_svsm_ca(void) 1567 { 1568 if (!snp_vmpl) 1569 return; 1570 1571 /* Update the CAA to a proper kernel address */ 1572 boot_svsm_caa = &boot_svsm_ca_page; 1573 } 1574 1575 #ifdef CONFIG_SYSFS 1576 static ssize_t vmpl_show(struct kobject *kobj, 1577 struct kobj_attribute *attr, char *buf) 1578 { 1579 return sysfs_emit(buf, "%d\n", snp_vmpl); 1580 } 1581 1582 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl); 1583 1584 static struct attribute *vmpl_attrs[] = { 1585 &vmpl_attr.attr, 1586 NULL 1587 }; 1588 1589 static struct attribute_group sev_attr_group = { 1590 .attrs = vmpl_attrs, 1591 }; 1592 1593 static int __init sev_sysfs_init(void) 1594 { 1595 struct kobject *sev_kobj; 1596 struct device *dev_root; 1597 int ret; 1598 1599 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) 1600 return -ENODEV; 1601 1602 dev_root = bus_get_dev_root(&cpu_subsys); 1603 if (!dev_root) 1604 return -ENODEV; 1605 1606 sev_kobj = kobject_create_and_add("sev", &dev_root->kobj); 1607 put_device(dev_root); 1608 1609 if (!sev_kobj) 1610 return -ENOMEM; 1611 1612 ret = sysfs_create_group(sev_kobj, &sev_attr_group); 1613 if (ret) 1614 kobject_put(sev_kobj); 1615 1616 return ret; 1617 } 1618 arch_initcall(sev_sysfs_init); 1619 #endif // CONFIG_SYSFS 1620 1621 static void free_shared_pages(void *buf, size_t sz) 1622 { 1623 unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; 1624 int ret; 1625 1626 if (!buf) 1627 return; 1628 1629 ret = set_memory_encrypted((unsigned long)buf, npages); 1630 if (ret) { 1631 WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n"); 1632 return; 1633 } 1634 1635 __free_pages(virt_to_page(buf), get_order(sz)); 1636 } 1637 1638 static void *alloc_shared_pages(size_t sz) 1639 { 1640 unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; 1641 struct page *page; 1642 int ret; 1643 1644 page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz)); 1645 if (!page) 1646 return NULL; 1647 1648 ret = set_memory_decrypted((unsigned long)page_address(page), npages); 1649 if (ret) { 1650 pr_err("failed to mark page shared, ret=%d\n", ret); 1651 __free_pages(page, get_order(sz)); 1652 return NULL; 1653 } 1654 1655 return page_address(page); 1656 } 1657 1658 static u8 *get_vmpck(int id, struct snp_secrets_page *secrets, u32 **seqno) 1659 { 1660 u8 *key = NULL; 1661 1662 switch (id) { 1663 case 0: 1664 *seqno = &secrets->os_area.msg_seqno_0; 1665 key = secrets->vmpck0; 1666 break; 1667 case 1: 1668 *seqno = &secrets->os_area.msg_seqno_1; 1669 key = secrets->vmpck1; 1670 break; 1671 case 2: 1672 *seqno = &secrets->os_area.msg_seqno_2; 1673 key = secrets->vmpck2; 1674 break; 1675 case 3: 1676 *seqno = &secrets->os_area.msg_seqno_3; 1677 key = secrets->vmpck3; 1678 break; 1679 default: 1680 break; 1681 } 1682 1683 return key; 1684 } 1685 1686 static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) 1687 { 1688 struct aesgcm_ctx *ctx; 1689 1690 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1691 if (!ctx) 1692 return NULL; 1693 1694 if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { 1695 pr_err("Crypto context initialization failed\n"); 1696 kfree(ctx); 1697 return NULL; 1698 } 1699 1700 return ctx; 1701 } 1702 1703 int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) 1704 { 1705 /* Adjust the default VMPCK key based on the executing VMPL level */ 1706 if (vmpck_id == -1) 1707 vmpck_id = snp_vmpl; 1708 1709 mdesc->vmpck = get_vmpck(vmpck_id, mdesc->secrets, &mdesc->os_area_msg_seqno); 1710 if (!mdesc->vmpck) { 1711 pr_err("Invalid VMPCK%d communication key\n", vmpck_id); 1712 return -EINVAL; 1713 } 1714 1715 /* Verify that VMPCK is not zero. */ 1716 if (!memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) { 1717 pr_err("Empty VMPCK%d communication key\n", vmpck_id); 1718 return -EINVAL; 1719 } 1720 1721 mdesc->vmpck_id = vmpck_id; 1722 1723 mdesc->ctx = snp_init_crypto(mdesc->vmpck, VMPCK_KEY_LEN); 1724 if (!mdesc->ctx) 1725 return -ENOMEM; 1726 1727 return 0; 1728 } 1729 EXPORT_SYMBOL_GPL(snp_msg_init); 1730 1731 struct snp_msg_desc *snp_msg_alloc(void) 1732 { 1733 struct snp_msg_desc *mdesc; 1734 void __iomem *mem; 1735 1736 BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE); 1737 1738 mdesc = kzalloc(sizeof(struct snp_msg_desc), GFP_KERNEL); 1739 if (!mdesc) 1740 return ERR_PTR(-ENOMEM); 1741 1742 mem = ioremap_encrypted(sev_secrets_pa, PAGE_SIZE); 1743 if (!mem) 1744 goto e_free_mdesc; 1745 1746 mdesc->secrets = (__force struct snp_secrets_page *)mem; 1747 1748 /* Allocate the shared page used for the request and response message. */ 1749 mdesc->request = alloc_shared_pages(sizeof(struct snp_guest_msg)); 1750 if (!mdesc->request) 1751 goto e_unmap; 1752 1753 mdesc->response = alloc_shared_pages(sizeof(struct snp_guest_msg)); 1754 if (!mdesc->response) 1755 goto e_free_request; 1756 1757 return mdesc; 1758 1759 e_free_request: 1760 free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg)); 1761 e_unmap: 1762 iounmap(mem); 1763 e_free_mdesc: 1764 kfree(mdesc); 1765 1766 return ERR_PTR(-ENOMEM); 1767 } 1768 EXPORT_SYMBOL_GPL(snp_msg_alloc); 1769 1770 void snp_msg_free(struct snp_msg_desc *mdesc) 1771 { 1772 if (!mdesc) 1773 return; 1774 1775 kfree(mdesc->ctx); 1776 free_shared_pages(mdesc->response, sizeof(struct snp_guest_msg)); 1777 free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg)); 1778 iounmap((__force void __iomem *)mdesc->secrets); 1779 1780 memset(mdesc, 0, sizeof(*mdesc)); 1781 kfree(mdesc); 1782 } 1783 EXPORT_SYMBOL_GPL(snp_msg_free); 1784 1785 /* Mutex to serialize the shared buffer access and command handling. */ 1786 static DEFINE_MUTEX(snp_cmd_mutex); 1787 1788 /* 1789 * If an error is received from the host or AMD Secure Processor (ASP) there 1790 * are two options. Either retry the exact same encrypted request or discontinue 1791 * using the VMPCK. 1792 * 1793 * This is because in the current encryption scheme GHCB v2 uses AES-GCM to 1794 * encrypt the requests. The IV for this scheme is the sequence number. GCM 1795 * cannot tolerate IV reuse. 1796 * 1797 * The ASP FW v1.51 only increments the sequence numbers on a successful 1798 * guest<->ASP back and forth and only accepts messages at its exact sequence 1799 * number. 1800 * 1801 * So if the sequence number were to be reused the encryption scheme is 1802 * vulnerable. If the sequence number were incremented for a fresh IV the ASP 1803 * will reject the request. 1804 */ 1805 static void snp_disable_vmpck(struct snp_msg_desc *mdesc) 1806 { 1807 pr_alert("Disabling VMPCK%d communication key to prevent IV reuse.\n", 1808 mdesc->vmpck_id); 1809 memzero_explicit(mdesc->vmpck, VMPCK_KEY_LEN); 1810 mdesc->vmpck = NULL; 1811 } 1812 1813 static inline u64 __snp_get_msg_seqno(struct snp_msg_desc *mdesc) 1814 { 1815 u64 count; 1816 1817 lockdep_assert_held(&snp_cmd_mutex); 1818 1819 /* Read the current message sequence counter from secrets pages */ 1820 count = *mdesc->os_area_msg_seqno; 1821 1822 return count + 1; 1823 } 1824 1825 /* Return a non-zero on success */ 1826 static u64 snp_get_msg_seqno(struct snp_msg_desc *mdesc) 1827 { 1828 u64 count = __snp_get_msg_seqno(mdesc); 1829 1830 /* 1831 * The message sequence counter for the SNP guest request is a 64-bit 1832 * value but the version 2 of GHCB specification defines a 32-bit storage 1833 * for it. If the counter exceeds the 32-bit value then return zero. 1834 * The caller should check the return value, but if the caller happens to 1835 * not check the value and use it, then the firmware treats zero as an 1836 * invalid number and will fail the message request. 1837 */ 1838 if (count >= UINT_MAX) { 1839 pr_err("request message sequence counter overflow\n"); 1840 return 0; 1841 } 1842 1843 return count; 1844 } 1845 1846 static void snp_inc_msg_seqno(struct snp_msg_desc *mdesc) 1847 { 1848 /* 1849 * The counter is also incremented by the PSP, so increment it by 2 1850 * and save in secrets page. 1851 */ 1852 *mdesc->os_area_msg_seqno += 2; 1853 } 1854 1855 static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_req *req) 1856 { 1857 struct snp_guest_msg *resp_msg = &mdesc->secret_response; 1858 struct snp_guest_msg *req_msg = &mdesc->secret_request; 1859 struct snp_guest_msg_hdr *req_msg_hdr = &req_msg->hdr; 1860 struct snp_guest_msg_hdr *resp_msg_hdr = &resp_msg->hdr; 1861 struct aesgcm_ctx *ctx = mdesc->ctx; 1862 u8 iv[GCM_AES_IV_SIZE] = {}; 1863 1864 pr_debug("response [seqno %lld type %d version %d sz %d]\n", 1865 resp_msg_hdr->msg_seqno, resp_msg_hdr->msg_type, resp_msg_hdr->msg_version, 1866 resp_msg_hdr->msg_sz); 1867 1868 /* Copy response from shared memory to encrypted memory. */ 1869 memcpy(resp_msg, mdesc->response, sizeof(*resp_msg)); 1870 1871 /* Verify that the sequence counter is incremented by 1 */ 1872 if (unlikely(resp_msg_hdr->msg_seqno != (req_msg_hdr->msg_seqno + 1))) 1873 return -EBADMSG; 1874 1875 /* Verify response message type and version number. */ 1876 if (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) || 1877 resp_msg_hdr->msg_version != req_msg_hdr->msg_version) 1878 return -EBADMSG; 1879 1880 /* 1881 * If the message size is greater than our buffer length then return 1882 * an error. 1883 */ 1884 if (unlikely((resp_msg_hdr->msg_sz + ctx->authsize) > req->resp_sz)) 1885 return -EBADMSG; 1886 1887 /* Decrypt the payload */ 1888 memcpy(iv, &resp_msg_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_msg_hdr->msg_seqno))); 1889 if (!aesgcm_decrypt(ctx, req->resp_buf, resp_msg->payload, resp_msg_hdr->msg_sz, 1890 &resp_msg_hdr->algo, AAD_LEN, iv, resp_msg_hdr->authtag)) 1891 return -EBADMSG; 1892 1893 return 0; 1894 } 1895 1896 static int enc_payload(struct snp_msg_desc *mdesc, u64 seqno, struct snp_guest_req *req) 1897 { 1898 struct snp_guest_msg *msg = &mdesc->secret_request; 1899 struct snp_guest_msg_hdr *hdr = &msg->hdr; 1900 struct aesgcm_ctx *ctx = mdesc->ctx; 1901 u8 iv[GCM_AES_IV_SIZE] = {}; 1902 1903 memset(msg, 0, sizeof(*msg)); 1904 1905 hdr->algo = SNP_AEAD_AES_256_GCM; 1906 hdr->hdr_version = MSG_HDR_VER; 1907 hdr->hdr_sz = sizeof(*hdr); 1908 hdr->msg_type = req->msg_type; 1909 hdr->msg_version = req->msg_version; 1910 hdr->msg_seqno = seqno; 1911 hdr->msg_vmpck = req->vmpck_id; 1912 hdr->msg_sz = req->req_sz; 1913 1914 /* Verify the sequence number is non-zero */ 1915 if (!hdr->msg_seqno) 1916 return -ENOSR; 1917 1918 pr_debug("request [seqno %lld type %d version %d sz %d]\n", 1919 hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); 1920 1921 if (WARN_ON((req->req_sz + ctx->authsize) > sizeof(msg->payload))) 1922 return -EBADMSG; 1923 1924 memcpy(iv, &hdr->msg_seqno, min(sizeof(iv), sizeof(hdr->msg_seqno))); 1925 aesgcm_encrypt(ctx, msg->payload, req->req_buf, req->req_sz, &hdr->algo, 1926 AAD_LEN, iv, hdr->authtag); 1927 1928 return 0; 1929 } 1930 1931 static int __handle_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, 1932 struct snp_guest_request_ioctl *rio) 1933 { 1934 unsigned long req_start = jiffies; 1935 unsigned int override_npages = 0; 1936 u64 override_err = 0; 1937 int rc; 1938 1939 retry_request: 1940 /* 1941 * Call firmware to process the request. In this function the encrypted 1942 * message enters shared memory with the host. So after this call the 1943 * sequence number must be incremented or the VMPCK must be deleted to 1944 * prevent reuse of the IV. 1945 */ 1946 rc = snp_issue_guest_request(req, &req->input, rio); 1947 switch (rc) { 1948 case -ENOSPC: 1949 /* 1950 * If the extended guest request fails due to having too 1951 * small of a certificate data buffer, retry the same 1952 * guest request without the extended data request in 1953 * order to increment the sequence number and thus avoid 1954 * IV reuse. 1955 */ 1956 override_npages = req->input.data_npages; 1957 req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; 1958 1959 /* 1960 * Override the error to inform callers the given extended 1961 * request buffer size was too small and give the caller the 1962 * required buffer size. 1963 */ 1964 override_err = SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN); 1965 1966 /* 1967 * If this call to the firmware succeeds, the sequence number can 1968 * be incremented allowing for continued use of the VMPCK. If 1969 * there is an error reflected in the return value, this value 1970 * is checked further down and the result will be the deletion 1971 * of the VMPCK and the error code being propagated back to the 1972 * user as an ioctl() return code. 1973 */ 1974 goto retry_request; 1975 1976 /* 1977 * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been 1978 * throttled. Retry in the driver to avoid returning and reusing the 1979 * message sequence number on a different message. 1980 */ 1981 case -EAGAIN: 1982 if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { 1983 rc = -ETIMEDOUT; 1984 break; 1985 } 1986 schedule_timeout_killable(SNP_REQ_RETRY_DELAY); 1987 goto retry_request; 1988 } 1989 1990 /* 1991 * Increment the message sequence number. There is no harm in doing 1992 * this now because decryption uses the value stored in the response 1993 * structure and any failure will wipe the VMPCK, preventing further 1994 * use anyway. 1995 */ 1996 snp_inc_msg_seqno(mdesc); 1997 1998 if (override_err) { 1999 rio->exitinfo2 = override_err; 2000 2001 /* 2002 * If an extended guest request was issued and the supplied certificate 2003 * buffer was not large enough, a standard guest request was issued to 2004 * prevent IV reuse. If the standard request was successful, return -EIO 2005 * back to the caller as would have originally been returned. 2006 */ 2007 if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) 2008 rc = -EIO; 2009 } 2010 2011 if (override_npages) 2012 req->input.data_npages = override_npages; 2013 2014 return rc; 2015 } 2016 2017 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, 2018 struct snp_guest_request_ioctl *rio) 2019 { 2020 u64 seqno; 2021 int rc; 2022 2023 guard(mutex)(&snp_cmd_mutex); 2024 2025 /* Check if the VMPCK is not empty */ 2026 if (!mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) { 2027 pr_err_ratelimited("VMPCK is disabled\n"); 2028 return -ENOTTY; 2029 } 2030 2031 /* Get message sequence and verify that its a non-zero */ 2032 seqno = snp_get_msg_seqno(mdesc); 2033 if (!seqno) 2034 return -EIO; 2035 2036 /* Clear shared memory's response for the host to populate. */ 2037 memset(mdesc->response, 0, sizeof(struct snp_guest_msg)); 2038 2039 /* Encrypt the userspace provided payload in mdesc->secret_request. */ 2040 rc = enc_payload(mdesc, seqno, req); 2041 if (rc) 2042 return rc; 2043 2044 /* 2045 * Write the fully encrypted request to the shared unencrypted 2046 * request page. 2047 */ 2048 memcpy(mdesc->request, &mdesc->secret_request, sizeof(mdesc->secret_request)); 2049 2050 /* Initialize the input address for guest request */ 2051 req->input.req_gpa = __pa(mdesc->request); 2052 req->input.resp_gpa = __pa(mdesc->response); 2053 req->input.data_gpa = req->certs_data ? __pa(req->certs_data) : 0; 2054 2055 rc = __handle_guest_request(mdesc, req, rio); 2056 if (rc) { 2057 if (rc == -EIO && 2058 rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) 2059 return rc; 2060 2061 pr_alert("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", 2062 rc, rio->exitinfo2); 2063 2064 snp_disable_vmpck(mdesc); 2065 return rc; 2066 } 2067 2068 rc = verify_and_dec_payload(mdesc, req); 2069 if (rc) { 2070 pr_alert("Detected unexpected decode failure from ASP. rc: %d\n", rc); 2071 snp_disable_vmpck(mdesc); 2072 return rc; 2073 } 2074 2075 return 0; 2076 } 2077 EXPORT_SYMBOL_GPL(snp_send_guest_request); 2078 2079 static int __init snp_get_tsc_info(void) 2080 { 2081 struct snp_guest_request_ioctl *rio; 2082 struct snp_tsc_info_resp *tsc_resp; 2083 struct snp_tsc_info_req *tsc_req; 2084 struct snp_msg_desc *mdesc; 2085 struct snp_guest_req *req; 2086 int rc = -ENOMEM; 2087 2088 tsc_req = kzalloc(sizeof(*tsc_req), GFP_KERNEL); 2089 if (!tsc_req) 2090 return rc; 2091 2092 /* 2093 * The intermediate response buffer is used while decrypting the 2094 * response payload. Make sure that it has enough space to cover 2095 * the authtag. 2096 */ 2097 tsc_resp = kzalloc(sizeof(*tsc_resp) + AUTHTAG_LEN, GFP_KERNEL); 2098 if (!tsc_resp) 2099 goto e_free_tsc_req; 2100 2101 req = kzalloc(sizeof(*req), GFP_KERNEL); 2102 if (!req) 2103 goto e_free_tsc_resp; 2104 2105 rio = kzalloc(sizeof(*rio), GFP_KERNEL); 2106 if (!rio) 2107 goto e_free_req; 2108 2109 mdesc = snp_msg_alloc(); 2110 if (IS_ERR_OR_NULL(mdesc)) 2111 goto e_free_rio; 2112 2113 rc = snp_msg_init(mdesc, snp_vmpl); 2114 if (rc) 2115 goto e_free_mdesc; 2116 2117 req->msg_version = MSG_HDR_VER; 2118 req->msg_type = SNP_MSG_TSC_INFO_REQ; 2119 req->vmpck_id = snp_vmpl; 2120 req->req_buf = tsc_req; 2121 req->req_sz = sizeof(*tsc_req); 2122 req->resp_buf = (void *)tsc_resp; 2123 req->resp_sz = sizeof(*tsc_resp) + AUTHTAG_LEN; 2124 req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; 2125 2126 rc = snp_send_guest_request(mdesc, req, rio); 2127 if (rc) 2128 goto e_request; 2129 2130 pr_debug("%s: response status 0x%x scale 0x%llx offset 0x%llx factor 0x%x\n", 2131 __func__, tsc_resp->status, tsc_resp->tsc_scale, tsc_resp->tsc_offset, 2132 tsc_resp->tsc_factor); 2133 2134 if (!tsc_resp->status) { 2135 snp_tsc_scale = tsc_resp->tsc_scale; 2136 snp_tsc_offset = tsc_resp->tsc_offset; 2137 } else { 2138 pr_err("Failed to get TSC info, response status 0x%x\n", tsc_resp->status); 2139 rc = -EIO; 2140 } 2141 2142 e_request: 2143 /* The response buffer contains sensitive data, explicitly clear it. */ 2144 memzero_explicit(tsc_resp, sizeof(*tsc_resp) + AUTHTAG_LEN); 2145 e_free_mdesc: 2146 snp_msg_free(mdesc); 2147 e_free_rio: 2148 kfree(rio); 2149 e_free_req: 2150 kfree(req); 2151 e_free_tsc_resp: 2152 kfree(tsc_resp); 2153 e_free_tsc_req: 2154 kfree(tsc_req); 2155 2156 return rc; 2157 } 2158 2159 void __init snp_secure_tsc_prepare(void) 2160 { 2161 if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) 2162 return; 2163 2164 if (snp_get_tsc_info()) { 2165 pr_alert("Unable to retrieve Secure TSC info from ASP\n"); 2166 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECURE_TSC); 2167 } 2168 2169 pr_debug("SecureTSC enabled"); 2170 } 2171 2172 static unsigned long securetsc_get_tsc_khz(void) 2173 { 2174 return snp_tsc_freq_khz; 2175 } 2176 2177 void __init snp_secure_tsc_init(void) 2178 { 2179 unsigned long long tsc_freq_mhz; 2180 2181 if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) 2182 return; 2183 2184 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); 2185 rdmsrq(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz); 2186 snp_tsc_freq_khz = (unsigned long)(tsc_freq_mhz * 1000); 2187 2188 x86_platform.calibrate_cpu = securetsc_get_tsc_khz; 2189 x86_platform.calibrate_tsc = securetsc_get_tsc_khz; 2190 } 2191