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