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