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 <[email protected]>
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
28 /*
29 * This code runs before CPU feature bits are set. By default, the
30 * pgtable_l5_enabled() function uses bit X86_FEATURE_LA57 to determine if
31 * 5-level paging is active, so that won't work here. USE_EARLY_PGTABLE_L5
32 * is provided to handle this situation and, instead, use a variable that
33 * has been set by the early boot code.
34 */
35 #define USE_EARLY_PGTABLE_L5
36
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/mem_encrypt.h>
40 #include <linux/cc_platform.h>
41
42 #include <asm/init.h>
43 #include <asm/setup.h>
44 #include <asm/sections.h>
45 #include <asm/coco.h>
46 #include <asm/sev.h>
47
48 #include "mm_internal.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
sme_clear_pgd(struct sme_populate_pgd_data * ppd)96 static void __head 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
sme_prepare_pgd(struct sme_populate_pgd_data * ppd)111 static pud_t __head *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
sme_populate_pgd_large(struct sme_populate_pgd_data * ppd)148 static void __head 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
sme_populate_pgd(struct sme_populate_pgd_data * ppd)164 static void __head 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
__sme_map_range_pmd(struct sme_populate_pgd_data * ppd)190 static void __head __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
__sme_map_range_pte(struct sme_populate_pgd_data * ppd)200 static void __head __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
__sme_map_range(struct sme_populate_pgd_data * ppd,pmdval_t pmd_flags,pteval_t pte_flags)210 static void __head __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
sme_map_range_encrypted(struct sme_populate_pgd_data * ppd)234 static void __head sme_map_range_encrypted(struct sme_populate_pgd_data *ppd)
235 {
236 __sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC);
237 }
238
sme_map_range_decrypted(struct sme_populate_pgd_data * ppd)239 static void __head sme_map_range_decrypted(struct sme_populate_pgd_data *ppd)
240 {
241 __sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC);
242 }
243
sme_map_range_decrypted_wp(struct sme_populate_pgd_data * ppd)244 static void __head 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
sme_pgtable_calc(unsigned long len)249 static unsigned long __head 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
sme_encrypt_kernel(struct boot_params * bp)286 void __head 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() ||
303 RIP_REL_REF(sev_status) & MSR_AMD64_SEV_ENABLED)
304 return;
305
306 /*
307 * Prepare for encrypting the kernel and initrd by building new
308 * pagetables with the necessary attributes needed to encrypt the
309 * kernel in place.
310 *
311 * One range of virtual addresses will map the memory occupied
312 * by the kernel and initrd as encrypted.
313 *
314 * Another range of virtual addresses will map the memory occupied
315 * by the kernel and initrd as decrypted and write-protected.
316 *
317 * The use of write-protect attribute will prevent any of the
318 * memory from being cached.
319 */
320
321 kernel_start = (unsigned long)RIP_REL_REF(_text);
322 kernel_end = ALIGN((unsigned long)RIP_REL_REF(_end), PMD_SIZE);
323 kernel_len = kernel_end - kernel_start;
324
325 initrd_start = 0;
326 initrd_end = 0;
327 initrd_len = 0;
328 #ifdef CONFIG_BLK_DEV_INITRD
329 initrd_len = (unsigned long)bp->hdr.ramdisk_size |
330 ((unsigned long)bp->ext_ramdisk_size << 32);
331 if (initrd_len) {
332 initrd_start = (unsigned long)bp->hdr.ramdisk_image |
333 ((unsigned long)bp->ext_ramdisk_image << 32);
334 initrd_end = PAGE_ALIGN(initrd_start + initrd_len);
335 initrd_len = initrd_end - initrd_start;
336 }
337 #endif
338
339 /*
340 * Calculate required number of workarea bytes needed:
341 * executable encryption area size:
342 * stack page (PAGE_SIZE)
343 * encryption routine page (PAGE_SIZE)
344 * intermediate copy buffer (PMD_SIZE)
345 * pagetable structures for the encryption of the kernel
346 * pagetable structures for workarea (in case not currently mapped)
347 */
348 execute_start = workarea_start = (unsigned long)RIP_REL_REF(sme_workarea);
349 execute_end = execute_start + (PAGE_SIZE * 2) + PMD_SIZE;
350 execute_len = execute_end - execute_start;
351
352 /*
353 * One PGD for both encrypted and decrypted mappings and a set of
354 * PUDs and PMDs for each of the encrypted and decrypted mappings.
355 */
356 pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD;
357 pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2;
358 if (initrd_len)
359 pgtable_area_len += sme_pgtable_calc(initrd_len) * 2;
360
361 /* PUDs and PMDs needed in the current pagetables for the workarea */
362 pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len);
363
364 /*
365 * The total workarea includes the executable encryption area and
366 * the pagetable area. The start of the workarea is already 2MB
367 * aligned, align the end of the workarea on a 2MB boundary so that
368 * we don't try to create/allocate PTE entries from the workarea
369 * before it is mapped.
370 */
371 workarea_len = execute_len + pgtable_area_len;
372 workarea_end = ALIGN(workarea_start + workarea_len, PMD_SIZE);
373
374 /*
375 * Set the address to the start of where newly created pagetable
376 * structures (PGDs, PUDs and PMDs) will be allocated. New pagetable
377 * structures are created when the workarea is added to the current
378 * pagetables and when the new encrypted and decrypted kernel
379 * mappings are populated.
380 */
381 ppd.pgtable_area = (void *)execute_end;
382
383 /*
384 * Make sure the current pagetable structure has entries for
385 * addressing the workarea.
386 */
387 ppd.pgd = (pgd_t *)native_read_cr3_pa();
388 ppd.paddr = workarea_start;
389 ppd.vaddr = workarea_start;
390 ppd.vaddr_end = workarea_end;
391 sme_map_range_decrypted(&ppd);
392
393 /* Flush the TLB - no globals so cr3 is enough */
394 native_write_cr3(__native_read_cr3());
395
396 /*
397 * A new pagetable structure is being built to allow for the kernel
398 * and initrd to be encrypted. It starts with an empty PGD that will
399 * then be populated with new PUDs and PMDs as the encrypted and
400 * decrypted kernel mappings are created.
401 */
402 ppd.pgd = ppd.pgtable_area;
403 memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD);
404 ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD;
405
406 /*
407 * A different PGD index/entry must be used to get different
408 * pagetable entries for the decrypted mapping. Choose the next
409 * PGD index and convert it to a virtual address to be used as
410 * the base of the mapping.
411 */
412 decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1);
413 if (initrd_len) {
414 unsigned long check_base;
415
416 check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1);
417 decrypted_base = max(decrypted_base, check_base);
418 }
419 decrypted_base <<= PGDIR_SHIFT;
420
421 /* Add encrypted kernel (identity) mappings */
422 ppd.paddr = kernel_start;
423 ppd.vaddr = kernel_start;
424 ppd.vaddr_end = kernel_end;
425 sme_map_range_encrypted(&ppd);
426
427 /* Add decrypted, write-protected kernel (non-identity) mappings */
428 ppd.paddr = kernel_start;
429 ppd.vaddr = kernel_start + decrypted_base;
430 ppd.vaddr_end = kernel_end + decrypted_base;
431 sme_map_range_decrypted_wp(&ppd);
432
433 if (initrd_len) {
434 /* Add encrypted initrd (identity) mappings */
435 ppd.paddr = initrd_start;
436 ppd.vaddr = initrd_start;
437 ppd.vaddr_end = initrd_end;
438 sme_map_range_encrypted(&ppd);
439 /*
440 * Add decrypted, write-protected initrd (non-identity) mappings
441 */
442 ppd.paddr = initrd_start;
443 ppd.vaddr = initrd_start + decrypted_base;
444 ppd.vaddr_end = initrd_end + decrypted_base;
445 sme_map_range_decrypted_wp(&ppd);
446 }
447
448 /* Add decrypted workarea mappings to both kernel mappings */
449 ppd.paddr = workarea_start;
450 ppd.vaddr = workarea_start;
451 ppd.vaddr_end = workarea_end;
452 sme_map_range_decrypted(&ppd);
453
454 ppd.paddr = workarea_start;
455 ppd.vaddr = workarea_start + decrypted_base;
456 ppd.vaddr_end = workarea_end + decrypted_base;
457 sme_map_range_decrypted(&ppd);
458
459 /* Perform the encryption */
460 sme_encrypt_execute(kernel_start, kernel_start + decrypted_base,
461 kernel_len, workarea_start, (unsigned long)ppd.pgd);
462
463 if (initrd_len)
464 sme_encrypt_execute(initrd_start, initrd_start + decrypted_base,
465 initrd_len, workarea_start,
466 (unsigned long)ppd.pgd);
467
468 /*
469 * At this point we are running encrypted. Remove the mappings for
470 * the decrypted areas - all that is needed for this is to remove
471 * the PGD entry/entries.
472 */
473 ppd.vaddr = kernel_start + decrypted_base;
474 ppd.vaddr_end = kernel_end + decrypted_base;
475 sme_clear_pgd(&ppd);
476
477 if (initrd_len) {
478 ppd.vaddr = initrd_start + decrypted_base;
479 ppd.vaddr_end = initrd_end + decrypted_base;
480 sme_clear_pgd(&ppd);
481 }
482
483 ppd.vaddr = workarea_start + decrypted_base;
484 ppd.vaddr_end = workarea_end + decrypted_base;
485 sme_clear_pgd(&ppd);
486
487 /* Flush the TLB - no globals so cr3 is enough */
488 native_write_cr3(__native_read_cr3());
489 }
490
sme_enable(struct boot_params * bp)491 void __head sme_enable(struct boot_params *bp)
492 {
493 unsigned int eax, ebx, ecx, edx;
494 unsigned long feature_mask;
495 unsigned long me_mask;
496 bool snp_en;
497 u64 msr;
498
499 snp_en = snp_init(bp);
500
501 /* Check for the SME/SEV support leaf */
502 eax = 0x80000000;
503 ecx = 0;
504 native_cpuid(&eax, &ebx, &ecx, &edx);
505 if (eax < 0x8000001f)
506 return;
507
508 #define AMD_SME_BIT BIT(0)
509 #define AMD_SEV_BIT BIT(1)
510
511 /*
512 * Check for the SME/SEV feature:
513 * CPUID Fn8000_001F[EAX]
514 * - Bit 0 - Secure Memory Encryption support
515 * - Bit 1 - Secure Encrypted Virtualization support
516 * CPUID Fn8000_001F[EBX]
517 * - Bits 5:0 - Pagetable bit position used to indicate encryption
518 */
519 eax = 0x8000001f;
520 ecx = 0;
521 native_cpuid(&eax, &ebx, &ecx, &edx);
522 /* Check whether SEV or SME is supported */
523 if (!(eax & (AMD_SEV_BIT | AMD_SME_BIT)))
524 return;
525
526 me_mask = 1UL << (ebx & 0x3f);
527
528 /* Check the SEV MSR whether SEV or SME is enabled */
529 RIP_REL_REF(sev_status) = msr = __rdmsr(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 snp_abort();
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 = __rdmsr(MSR_AMD64_SYSCFG);
561 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
562 return;
563 }
564
565 RIP_REL_REF(sme_me_mask) = me_mask;
566 RIP_REL_REF(physical_mask) &= ~me_mask;
567 RIP_REL_REF(cc_vendor) = CC_VENDOR_AMD;
568 cc_set_mask(me_mask);
569 }
570