1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Memory Encryption Support 4 * 5 * Copyright (C) 2016 Advanced Micro Devices, Inc. 6 * 7 * Author: Tom Lendacky <thomas.lendacky@amd.com> 8 */ 9 10 /* 11 * Since we're dealing with identity mappings, physical and virtual 12 * addresses are the same, so override these defines which are ultimately 13 * used by the headers in misc.h. 14 */ 15 #define __pa(x) ((unsigned long)(x)) 16 #define __va(x) ((void *)((unsigned long)(x))) 17 18 /* 19 * Special hack: we have to be careful, because no indirections are 20 * allowed here, and paravirt_ops is a kind of one. As it will only run in 21 * baremetal anyway, we just keep it from happening. (This list needs to 22 * be extended when new paravirt and debugging variants are added.) 23 */ 24 #undef CONFIG_PARAVIRT 25 #undef CONFIG_PARAVIRT_XXL 26 #undef CONFIG_PARAVIRT_SPINLOCKS 27 #undef CONFIG_ARCH_HAS_LAZY_MMU_MODE 28 29 /* 30 * This code runs before CPU feature bits are set. By default, the 31 * pgtable_l5_enabled() function uses bit X86_FEATURE_LA57 to determine if 32 * 5-level paging is active, so that won't work here. USE_EARLY_PGTABLE_L5 33 * is provided to handle this situation and, instead, use a variable that 34 * has been set by the early boot code. 35 */ 36 #define USE_EARLY_PGTABLE_L5 37 38 #include <linux/kernel.h> 39 #include <linux/mm.h> 40 #include <linux/mem_encrypt.h> 41 #include <linux/cc_platform.h> 42 43 #include <asm/init.h> 44 #include <asm/setup.h> 45 #include <asm/sections.h> 46 #include <asm/coco.h> 47 #include <asm/sev.h> 48 49 #define PGD_FLAGS _KERNPG_TABLE_NOENC 50 #define P4D_FLAGS _KERNPG_TABLE_NOENC 51 #define PUD_FLAGS _KERNPG_TABLE_NOENC 52 #define PMD_FLAGS _KERNPG_TABLE_NOENC 53 54 #define PMD_FLAGS_LARGE (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL) 55 56 #define PMD_FLAGS_DEC PMD_FLAGS_LARGE 57 #define PMD_FLAGS_DEC_WP ((PMD_FLAGS_DEC & ~_PAGE_LARGE_CACHE_MASK) | \ 58 (_PAGE_PAT_LARGE | _PAGE_PWT)) 59 60 #define PMD_FLAGS_ENC (PMD_FLAGS_LARGE | _PAGE_ENC) 61 62 #define PTE_FLAGS (__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL) 63 64 #define PTE_FLAGS_DEC PTE_FLAGS 65 #define PTE_FLAGS_DEC_WP ((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \ 66 (_PAGE_PAT | _PAGE_PWT)) 67 68 #define PTE_FLAGS_ENC (PTE_FLAGS | _PAGE_ENC) 69 70 struct sme_populate_pgd_data { 71 void *pgtable_area; 72 pgd_t *pgd; 73 74 pmdval_t pmd_flags; 75 pteval_t pte_flags; 76 unsigned long paddr; 77 78 unsigned long vaddr; 79 unsigned long vaddr_end; 80 }; 81 82 /* 83 * This work area lives in the .init.scratch section, which lives outside of 84 * the kernel proper. It is sized to hold the intermediate copy buffer and 85 * more than enough pagetable pages. 86 * 87 * By using this section, the kernel can be encrypted in place and it 88 * avoids any possibility of boot parameters or initramfs images being 89 * placed such that the in-place encryption logic overwrites them. This 90 * section is 2MB aligned to allow for simple pagetable setup using only 91 * PMD entries (see vmlinux.lds.S). 92 */ 93 static char sme_workarea[2 * PMD_SIZE] __section(".init.scratch"); 94 95 static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd) 96 { 97 unsigned long pgd_start, pgd_end, pgd_size; 98 pgd_t *pgd_p; 99 100 pgd_start = ppd->vaddr & PGDIR_MASK; 101 pgd_end = ppd->vaddr_end & PGDIR_MASK; 102 103 pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t); 104 105 pgd_p = ppd->pgd + pgd_index(ppd->vaddr); 106 107 memset(pgd_p, 0, pgd_size); 108 } 109 110 static pud_t __init *sme_prepare_pgd(struct sme_populate_pgd_data *ppd) 111 { 112 pgd_t *pgd; 113 p4d_t *p4d; 114 pud_t *pud; 115 pmd_t *pmd; 116 117 pgd = ppd->pgd + pgd_index(ppd->vaddr); 118 if (pgd_none(*pgd)) { 119 p4d = ppd->pgtable_area; 120 memset(p4d, 0, sizeof(*p4d) * PTRS_PER_P4D); 121 ppd->pgtable_area += sizeof(*p4d) * PTRS_PER_P4D; 122 set_pgd(pgd, __pgd(PGD_FLAGS | __pa(p4d))); 123 } 124 125 p4d = p4d_offset(pgd, ppd->vaddr); 126 if (p4d_none(*p4d)) { 127 pud = ppd->pgtable_area; 128 memset(pud, 0, sizeof(*pud) * PTRS_PER_PUD); 129 ppd->pgtable_area += sizeof(*pud) * PTRS_PER_PUD; 130 set_p4d(p4d, __p4d(P4D_FLAGS | __pa(pud))); 131 } 132 133 pud = pud_offset(p4d, ppd->vaddr); 134 if (pud_none(*pud)) { 135 pmd = ppd->pgtable_area; 136 memset(pmd, 0, sizeof(*pmd) * PTRS_PER_PMD); 137 ppd->pgtable_area += sizeof(*pmd) * PTRS_PER_PMD; 138 set_pud(pud, __pud(PUD_FLAGS | __pa(pmd))); 139 } 140 141 if (pud_leaf(*pud)) 142 return NULL; 143 144 return pud; 145 } 146 147 static void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd) 148 { 149 pud_t *pud; 150 pmd_t *pmd; 151 152 pud = sme_prepare_pgd(ppd); 153 if (!pud) 154 return; 155 156 pmd = pmd_offset(pud, ppd->vaddr); 157 if (pmd_leaf(*pmd)) 158 return; 159 160 set_pmd(pmd, __pmd(ppd->paddr | ppd->pmd_flags)); 161 } 162 163 static void __init sme_populate_pgd(struct sme_populate_pgd_data *ppd) 164 { 165 pud_t *pud; 166 pmd_t *pmd; 167 pte_t *pte; 168 169 pud = sme_prepare_pgd(ppd); 170 if (!pud) 171 return; 172 173 pmd = pmd_offset(pud, ppd->vaddr); 174 if (pmd_none(*pmd)) { 175 pte = ppd->pgtable_area; 176 memset(pte, 0, sizeof(*pte) * PTRS_PER_PTE); 177 ppd->pgtable_area += sizeof(*pte) * PTRS_PER_PTE; 178 set_pmd(pmd, __pmd(PMD_FLAGS | __pa(pte))); 179 } 180 181 if (pmd_leaf(*pmd)) 182 return; 183 184 pte = pte_offset_kernel(pmd, ppd->vaddr); 185 if (pte_none(*pte)) 186 set_pte(pte, __pte(ppd->paddr | ppd->pte_flags)); 187 } 188 189 static void __init __sme_map_range_pmd(struct sme_populate_pgd_data *ppd) 190 { 191 while (ppd->vaddr < ppd->vaddr_end) { 192 sme_populate_pgd_large(ppd); 193 194 ppd->vaddr += PMD_SIZE; 195 ppd->paddr += PMD_SIZE; 196 } 197 } 198 199 static void __init __sme_map_range_pte(struct sme_populate_pgd_data *ppd) 200 { 201 while (ppd->vaddr < ppd->vaddr_end) { 202 sme_populate_pgd(ppd); 203 204 ppd->vaddr += PAGE_SIZE; 205 ppd->paddr += PAGE_SIZE; 206 } 207 } 208 209 static void __init __sme_map_range(struct sme_populate_pgd_data *ppd, 210 pmdval_t pmd_flags, pteval_t pte_flags) 211 { 212 unsigned long vaddr_end; 213 214 ppd->pmd_flags = pmd_flags; 215 ppd->pte_flags = pte_flags; 216 217 /* Save original end value since we modify the struct value */ 218 vaddr_end = ppd->vaddr_end; 219 220 /* If start is not 2MB aligned, create PTE entries */ 221 ppd->vaddr_end = ALIGN(ppd->vaddr, PMD_SIZE); 222 __sme_map_range_pte(ppd); 223 224 /* Create PMD entries */ 225 ppd->vaddr_end = vaddr_end & PMD_MASK; 226 __sme_map_range_pmd(ppd); 227 228 /* If end is not 2MB aligned, create PTE entries */ 229 ppd->vaddr_end = vaddr_end; 230 __sme_map_range_pte(ppd); 231 } 232 233 static void __init sme_map_range_encrypted(struct sme_populate_pgd_data *ppd) 234 { 235 __sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC); 236 } 237 238 static void __init sme_map_range_decrypted(struct sme_populate_pgd_data *ppd) 239 { 240 __sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC); 241 } 242 243 static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd) 244 { 245 __sme_map_range(ppd, PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP); 246 } 247 248 static unsigned long __init sme_pgtable_calc(unsigned long len) 249 { 250 unsigned long entries = 0, tables = 0; 251 252 /* 253 * Perform a relatively simplistic calculation of the pagetable 254 * entries that are needed. Those mappings will be covered mostly 255 * by 2MB PMD entries so we can conservatively calculate the required 256 * number of P4D, PUD and PMD structures needed to perform the 257 * mappings. For mappings that are not 2MB aligned, PTE mappings 258 * would be needed for the start and end portion of the address range 259 * that fall outside of the 2MB alignment. This results in, at most, 260 * two extra pages to hold PTE entries for each range that is mapped. 261 * Incrementing the count for each covers the case where the addresses 262 * cross entries. 263 */ 264 265 /* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */ 266 if (PTRS_PER_P4D > 1) 267 entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D; 268 entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD; 269 entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD; 270 entries += 2 * sizeof(pte_t) * PTRS_PER_PTE; 271 272 /* 273 * Now calculate the added pagetable structures needed to populate 274 * the new pagetables. 275 */ 276 277 if (PTRS_PER_P4D > 1) 278 tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D; 279 tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD; 280 tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD; 281 282 return entries + tables; 283 } 284 285 void __init sme_encrypt_kernel(struct boot_params *bp) 286 { 287 unsigned long workarea_start, workarea_end, workarea_len; 288 unsigned long execute_start, execute_end, execute_len; 289 unsigned long kernel_start, kernel_end, kernel_len; 290 unsigned long initrd_start, initrd_end, initrd_len; 291 struct sme_populate_pgd_data ppd; 292 unsigned long pgtable_area_len; 293 unsigned long decrypted_base; 294 295 /* 296 * This is early code, use an open coded check for SME instead of 297 * using cc_platform_has(). This eliminates worries about removing 298 * instrumentation or checking boot_cpu_data in the cc_platform_has() 299 * function. 300 */ 301 if (!sme_get_me_mask() || sev_status & MSR_AMD64_SEV_ENABLED) 302 return; 303 304 /* 305 * Prepare for encrypting the kernel and initrd by building new 306 * pagetables with the necessary attributes needed to encrypt the 307 * kernel in place. 308 * 309 * One range of virtual addresses will map the memory occupied 310 * by the kernel and initrd as encrypted. 311 * 312 * Another range of virtual addresses will map the memory occupied 313 * by the kernel and initrd as decrypted and write-protected. 314 * 315 * The use of write-protect attribute will prevent any of the 316 * memory from being cached. 317 */ 318 319 kernel_start = (unsigned long)rip_rel_ptr(_text); 320 kernel_end = ALIGN((unsigned long)rip_rel_ptr(_end), PMD_SIZE); 321 kernel_len = kernel_end - kernel_start; 322 323 initrd_start = 0; 324 initrd_end = 0; 325 initrd_len = 0; 326 #ifdef CONFIG_BLK_DEV_INITRD 327 initrd_len = (unsigned long)bp->hdr.ramdisk_size | 328 ((unsigned long)bp->ext_ramdisk_size << 32); 329 if (initrd_len) { 330 initrd_start = (unsigned long)bp->hdr.ramdisk_image | 331 ((unsigned long)bp->ext_ramdisk_image << 32); 332 initrd_end = PAGE_ALIGN(initrd_start + initrd_len); 333 initrd_len = initrd_end - initrd_start; 334 } 335 #endif 336 337 /* 338 * Calculate required number of workarea bytes needed: 339 * executable encryption area size: 340 * stack page (PAGE_SIZE) 341 * encryption routine page (PAGE_SIZE) 342 * intermediate copy buffer (PMD_SIZE) 343 * pagetable structures for the encryption of the kernel 344 * pagetable structures for workarea (in case not currently mapped) 345 */ 346 execute_start = workarea_start = (unsigned long)rip_rel_ptr(sme_workarea); 347 execute_end = execute_start + (PAGE_SIZE * 2) + PMD_SIZE; 348 execute_len = execute_end - execute_start; 349 350 /* 351 * One PGD for both encrypted and decrypted mappings and a set of 352 * PUDs and PMDs for each of the encrypted and decrypted mappings. 353 */ 354 pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD; 355 pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2; 356 if (initrd_len) 357 pgtable_area_len += sme_pgtable_calc(initrd_len) * 2; 358 359 /* PUDs and PMDs needed in the current pagetables for the workarea */ 360 pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len); 361 362 /* 363 * The total workarea includes the executable encryption area and 364 * the pagetable area. The start of the workarea is already 2MB 365 * aligned, align the end of the workarea on a 2MB boundary so that 366 * we don't try to create/allocate PTE entries from the workarea 367 * before it is mapped. 368 */ 369 workarea_len = execute_len + pgtable_area_len; 370 workarea_end = ALIGN(workarea_start + workarea_len, PMD_SIZE); 371 372 /* 373 * Set the address to the start of where newly created pagetable 374 * structures (PGDs, PUDs and PMDs) will be allocated. New pagetable 375 * structures are created when the workarea is added to the current 376 * pagetables and when the new encrypted and decrypted kernel 377 * mappings are populated. 378 */ 379 ppd.pgtable_area = (void *)execute_end; 380 381 /* 382 * Make sure the current pagetable structure has entries for 383 * addressing the workarea. 384 */ 385 ppd.pgd = (pgd_t *)native_read_cr3_pa(); 386 ppd.paddr = workarea_start; 387 ppd.vaddr = workarea_start; 388 ppd.vaddr_end = workarea_end; 389 sme_map_range_decrypted(&ppd); 390 391 /* Flush the TLB - no globals so cr3 is enough */ 392 native_write_cr3(__native_read_cr3()); 393 394 /* 395 * A new pagetable structure is being built to allow for the kernel 396 * and initrd to be encrypted. It starts with an empty PGD that will 397 * then be populated with new PUDs and PMDs as the encrypted and 398 * decrypted kernel mappings are created. 399 */ 400 ppd.pgd = ppd.pgtable_area; 401 memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD); 402 ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD; 403 404 /* 405 * A different PGD index/entry must be used to get different 406 * pagetable entries for the decrypted mapping. Choose the next 407 * PGD index and convert it to a virtual address to be used as 408 * the base of the mapping. 409 */ 410 decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1); 411 if (initrd_len) { 412 unsigned long check_base; 413 414 check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1); 415 decrypted_base = max(decrypted_base, check_base); 416 } 417 decrypted_base <<= PGDIR_SHIFT; 418 419 /* Add encrypted kernel (identity) mappings */ 420 ppd.paddr = kernel_start; 421 ppd.vaddr = kernel_start; 422 ppd.vaddr_end = kernel_end; 423 sme_map_range_encrypted(&ppd); 424 425 /* Add decrypted, write-protected kernel (non-identity) mappings */ 426 ppd.paddr = kernel_start; 427 ppd.vaddr = kernel_start + decrypted_base; 428 ppd.vaddr_end = kernel_end + decrypted_base; 429 sme_map_range_decrypted_wp(&ppd); 430 431 if (initrd_len) { 432 /* Add encrypted initrd (identity) mappings */ 433 ppd.paddr = initrd_start; 434 ppd.vaddr = initrd_start; 435 ppd.vaddr_end = initrd_end; 436 sme_map_range_encrypted(&ppd); 437 /* 438 * Add decrypted, write-protected initrd (non-identity) mappings 439 */ 440 ppd.paddr = initrd_start; 441 ppd.vaddr = initrd_start + decrypted_base; 442 ppd.vaddr_end = initrd_end + decrypted_base; 443 sme_map_range_decrypted_wp(&ppd); 444 } 445 446 /* Add decrypted workarea mappings to both kernel mappings */ 447 ppd.paddr = workarea_start; 448 ppd.vaddr = workarea_start; 449 ppd.vaddr_end = workarea_end; 450 sme_map_range_decrypted(&ppd); 451 452 ppd.paddr = workarea_start; 453 ppd.vaddr = workarea_start + decrypted_base; 454 ppd.vaddr_end = workarea_end + decrypted_base; 455 sme_map_range_decrypted(&ppd); 456 457 /* Perform the encryption */ 458 sme_encrypt_execute(kernel_start, kernel_start + decrypted_base, 459 kernel_len, workarea_start, (unsigned long)ppd.pgd); 460 461 if (initrd_len) 462 sme_encrypt_execute(initrd_start, initrd_start + decrypted_base, 463 initrd_len, workarea_start, 464 (unsigned long)ppd.pgd); 465 466 /* 467 * At this point we are running encrypted. Remove the mappings for 468 * the decrypted areas - all that is needed for this is to remove 469 * the PGD entry/entries. 470 */ 471 ppd.vaddr = kernel_start + decrypted_base; 472 ppd.vaddr_end = kernel_end + decrypted_base; 473 sme_clear_pgd(&ppd); 474 475 if (initrd_len) { 476 ppd.vaddr = initrd_start + decrypted_base; 477 ppd.vaddr_end = initrd_end + decrypted_base; 478 sme_clear_pgd(&ppd); 479 } 480 481 ppd.vaddr = workarea_start + decrypted_base; 482 ppd.vaddr_end = workarea_end + decrypted_base; 483 sme_clear_pgd(&ppd); 484 485 /* Flush the TLB - no globals so cr3 is enough */ 486 native_write_cr3(__native_read_cr3()); 487 } 488 489 void __init sme_enable(struct boot_params *bp) 490 { 491 unsigned int eax, ebx, ecx, edx; 492 unsigned long feature_mask; 493 unsigned long me_mask; 494 bool snp_en; 495 u64 msr; 496 497 snp_en = snp_init(bp); 498 499 /* Check for the SME/SEV support leaf */ 500 eax = 0x80000000; 501 ecx = 0; 502 native_cpuid(&eax, &ebx, &ecx, &edx); 503 if (eax < 0x8000001f) 504 return; 505 506 #define AMD_SME_BIT BIT(0) 507 #define AMD_SEV_BIT BIT(1) 508 509 /* 510 * Check for the SME/SEV feature: 511 * CPUID Fn8000_001F[EAX] 512 * - Bit 0 - Secure Memory Encryption support 513 * - Bit 1 - Secure Encrypted Virtualization support 514 * CPUID Fn8000_001F[EBX] 515 * - Bits 5:0 - Pagetable bit position used to indicate encryption 516 */ 517 eax = 0x8000001f; 518 ecx = 0; 519 native_cpuid(&eax, &ebx, &ecx, &edx); 520 /* Check whether SEV or SME is supported */ 521 if (!(eax & (AMD_SEV_BIT | AMD_SME_BIT))) 522 return; 523 524 me_mask = 1UL << (ebx & 0x3f); 525 sev_snp_needs_sfw = !(ebx & BIT(31)); 526 527 /* Check the SEV MSR whether SEV or SME is enabled */ 528 sev_status = msr = native_rdmsrq(MSR_AMD64_SEV); 529 feature_mask = (msr & MSR_AMD64_SEV_ENABLED) ? AMD_SEV_BIT : AMD_SME_BIT; 530 531 /* 532 * Any discrepancies between the presence of a CC blob and SNP 533 * enablement abort the guest. 534 */ 535 if (snp_en ^ !!(msr & MSR_AMD64_SEV_SNP_ENABLED)) 536 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); 537 538 /* Check if memory encryption is enabled */ 539 if (feature_mask == AMD_SME_BIT) { 540 if (!(bp->hdr.xloadflags & XLF_MEM_ENCRYPTION)) 541 return; 542 543 /* 544 * No SME if Hypervisor bit is set. This check is here to 545 * prevent a guest from trying to enable SME. For running as a 546 * KVM guest the MSR_AMD64_SYSCFG will be sufficient, but there 547 * might be other hypervisors which emulate that MSR as non-zero 548 * or even pass it through to the guest. 549 * A malicious hypervisor can still trick a guest into this 550 * path, but there is no way to protect against that. 551 */ 552 eax = 1; 553 ecx = 0; 554 native_cpuid(&eax, &ebx, &ecx, &edx); 555 if (ecx & BIT(31)) 556 return; 557 558 /* For SME, check the SYSCFG MSR */ 559 msr = native_rdmsrq(MSR_AMD64_SYSCFG); 560 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT)) 561 return; 562 } 563 564 sme_me_mask = me_mask; 565 physical_mask &= ~me_mask; 566 cc_vendor = CC_VENDOR_AMD; 567 cc_set_mask(me_mask); 568 } 569 570 #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION 571 /* Local version for startup code, which never operates on user page tables */ 572 pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) 573 { 574 return pgd; 575 } 576 #endif 577