2 * AMD Memory Encryption Support
4 * Copyright (C) 2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #define DISABLE_BRANCH_PROFILING
15 #include <linux/linkage.h>
16 #include <linux/init.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/swiotlb.h>
20 #include <linux/mem_encrypt.h>
22 #include <asm/tlbflush.h>
23 #include <asm/fixmap.h>
24 #include <asm/setup.h>
25 #include <asm/bootparam.h>
26 #include <asm/set_memory.h>
27 #include <asm/cacheflush.h>
28 #include <asm/sections.h>
29 #include <asm/processor-flags.h>
31 #include <asm/cmdline.h>
33 static char sme_cmdline_arg[] __initdata = "mem_encrypt";
34 static char sme_cmdline_on[] __initdata = "on";
35 static char sme_cmdline_off[] __initdata = "off";
38 * Since SME related variables are set early in the boot process they must
39 * reside in the .data section so as not to be zeroed out when the .bss
40 * section is later cleared.
42 u64 sme_me_mask __section(.data) = 0;
43 EXPORT_SYMBOL(sme_me_mask);
45 /* Buffer used for early in-place encryption by BSP, no locking needed */
46 static char sme_early_buffer[PAGE_SIZE] __aligned(PAGE_SIZE);
49 * This routine does not change the underlying encryption setting of the
50 * page(s) that map this memory. It assumes that eventually the memory is
51 * meant to be accessed as either encrypted or decrypted but the contents
52 * are currently not in the desired state.
54 * This routine follows the steps outlined in the AMD64 Architecture
55 * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
57 static void __init __sme_early_enc_dec(resource_size_t paddr,
58 unsigned long size, bool enc)
70 * There are limited number of early mapping slots, so map (at most)
74 len = min_t(size_t, sizeof(sme_early_buffer), size);
77 * Create mappings for the current and desired format of
78 * the memory. Use a write-protected mapping for the source.
80 src = enc ? early_memremap_decrypted_wp(paddr, len) :
81 early_memremap_encrypted_wp(paddr, len);
83 dst = enc ? early_memremap_encrypted(paddr, len) :
84 early_memremap_decrypted(paddr, len);
87 * If a mapping can't be obtained to perform the operation,
88 * then eventual access of that area in the desired mode
94 * Use a temporary buffer, of cache-line multiple size, to
95 * avoid data corruption as documented in the APM.
97 memcpy(sme_early_buffer, src, len);
98 memcpy(dst, sme_early_buffer, len);
100 early_memunmap(dst, len);
101 early_memunmap(src, len);
108 void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
110 __sme_early_enc_dec(paddr, size, true);
113 void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
115 __sme_early_enc_dec(paddr, size, false);
118 static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
121 unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
122 pmdval_t pmd_flags, pmd;
124 /* Use early_pmd_flags but remove the encryption mask */
125 pmd_flags = __sme_clr(early_pmd_flags);
128 pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
129 __early_make_pgtable((unsigned long)vaddr, pmd);
133 size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
136 __native_flush_tlb();
139 void __init sme_unmap_bootdata(char *real_mode_data)
141 struct boot_params *boot_data;
142 unsigned long cmdline_paddr;
147 /* Get the command line address before unmapping the real_mode_data */
148 boot_data = (struct boot_params *)real_mode_data;
149 cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
151 __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
156 __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
159 void __init sme_map_bootdata(char *real_mode_data)
161 struct boot_params *boot_data;
162 unsigned long cmdline_paddr;
167 __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
169 /* Get the command line address after mapping the real_mode_data */
170 boot_data = (struct boot_params *)real_mode_data;
171 cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
176 __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
179 void __init sme_early_init(void)
186 early_pmd_flags = __sme_set(early_pmd_flags);
188 __supported_pte_mask = __sme_set(__supported_pte_mask);
190 /* Update the protection map with memory encryption mask */
191 for (i = 0; i < ARRAY_SIZE(protection_map); i++)
192 protection_map[i] = pgprot_encrypted(protection_map[i]);
195 /* Architecture __weak replacement functions */
196 void __init mem_encrypt_init(void)
201 /* Call into SWIOTLB to update the SWIOTLB DMA buffers */
202 swiotlb_update_mem_attributes();
204 pr_info("AMD Secure Memory Encryption (SME) active\n");
207 void swiotlb_set_mem_attributes(void *vaddr, unsigned long size)
209 WARN(PAGE_ALIGN(size) != size,
210 "size is not page-aligned (%#lx)\n", size);
212 /* Make the SWIOTLB buffer area decrypted */
213 set_memory_decrypted((unsigned long)vaddr, size >> PAGE_SHIFT);
216 struct sme_populate_pgd_data {
225 unsigned long vaddr_end;
228 static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
230 unsigned long pgd_start, pgd_end, pgd_size;
233 pgd_start = ppd->vaddr & PGDIR_MASK;
234 pgd_end = ppd->vaddr_end & PGDIR_MASK;
236 pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
238 pgd_p = ppd->pgd + pgd_index(ppd->vaddr);
240 memset(pgd_p, 0, pgd_size);
243 #define PGD_FLAGS _KERNPG_TABLE_NOENC
244 #define P4D_FLAGS _KERNPG_TABLE_NOENC
245 #define PUD_FLAGS _KERNPG_TABLE_NOENC
246 #define PMD_FLAGS _KERNPG_TABLE_NOENC
248 #define PMD_FLAGS_LARGE (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL)
250 #define PMD_FLAGS_DEC PMD_FLAGS_LARGE
251 #define PMD_FLAGS_DEC_WP ((PMD_FLAGS_DEC & ~_PAGE_LARGE_CACHE_MASK) | \
252 (_PAGE_PAT_LARGE | _PAGE_PWT))
254 #define PMD_FLAGS_ENC (PMD_FLAGS_LARGE | _PAGE_ENC)
256 #define PTE_FLAGS (__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL)
258 #define PTE_FLAGS_DEC PTE_FLAGS
259 #define PTE_FLAGS_DEC_WP ((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \
260 (_PAGE_PAT | _PAGE_PWT))
262 #define PTE_FLAGS_ENC (PTE_FLAGS | _PAGE_ENC)
264 static pmd_t __init *sme_prepare_pgd(struct sme_populate_pgd_data *ppd)
271 pgd_p = ppd->pgd + pgd_index(ppd->vaddr);
272 if (native_pgd_val(*pgd_p)) {
273 if (IS_ENABLED(CONFIG_X86_5LEVEL))
274 p4d_p = (p4d_t *)(native_pgd_val(*pgd_p) & ~PTE_FLAGS_MASK);
276 pud_p = (pud_t *)(native_pgd_val(*pgd_p) & ~PTE_FLAGS_MASK);
280 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
281 p4d_p = ppd->pgtable_area;
282 memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
283 ppd->pgtable_area += sizeof(*p4d_p) * PTRS_PER_P4D;
285 pgd = native_make_pgd((pgdval_t)p4d_p + PGD_FLAGS);
287 pud_p = ppd->pgtable_area;
288 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
289 ppd->pgtable_area += sizeof(*pud_p) * PTRS_PER_PUD;
291 pgd = native_make_pgd((pgdval_t)pud_p + PGD_FLAGS);
293 native_set_pgd(pgd_p, pgd);
296 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
297 p4d_p += p4d_index(ppd->vaddr);
298 if (native_p4d_val(*p4d_p)) {
299 pud_p = (pud_t *)(native_p4d_val(*p4d_p) & ~PTE_FLAGS_MASK);
303 pud_p = ppd->pgtable_area;
304 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
305 ppd->pgtable_area += sizeof(*pud_p) * PTRS_PER_PUD;
307 p4d = native_make_p4d((pudval_t)pud_p + P4D_FLAGS);
308 native_set_p4d(p4d_p, p4d);
312 pud_p += pud_index(ppd->vaddr);
313 if (native_pud_val(*pud_p)) {
314 if (native_pud_val(*pud_p) & _PAGE_PSE)
317 pmd_p = (pmd_t *)(native_pud_val(*pud_p) & ~PTE_FLAGS_MASK);
321 pmd_p = ppd->pgtable_area;
322 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
323 ppd->pgtable_area += sizeof(*pmd_p) * PTRS_PER_PMD;
325 pud = native_make_pud((pmdval_t)pmd_p + PUD_FLAGS);
326 native_set_pud(pud_p, pud);
332 static void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd)
336 pmd_p = sme_prepare_pgd(ppd);
340 pmd_p += pmd_index(ppd->vaddr);
341 if (!native_pmd_val(*pmd_p) || !(native_pmd_val(*pmd_p) & _PAGE_PSE))
342 native_set_pmd(pmd_p, native_make_pmd(ppd->paddr | ppd->pmd_flags));
345 static void __init sme_populate_pgd(struct sme_populate_pgd_data *ppd)
350 pmd_p = sme_prepare_pgd(ppd);
354 pmd_p += pmd_index(ppd->vaddr);
355 if (native_pmd_val(*pmd_p)) {
356 if (native_pmd_val(*pmd_p) & _PAGE_PSE)
359 pte_p = (pte_t *)(native_pmd_val(*pmd_p) & ~PTE_FLAGS_MASK);
363 pte_p = ppd->pgtable_area;
364 memset(pte_p, 0, sizeof(*pte_p) * PTRS_PER_PTE);
365 ppd->pgtable_area += sizeof(*pte_p) * PTRS_PER_PTE;
367 pmd = native_make_pmd((pteval_t)pte_p + PMD_FLAGS);
368 native_set_pmd(pmd_p, pmd);
371 pte_p += pte_index(ppd->vaddr);
372 if (!native_pte_val(*pte_p))
373 native_set_pte(pte_p, native_make_pte(ppd->paddr | ppd->pte_flags));
376 static void __init __sme_map_range_pmd(struct sme_populate_pgd_data *ppd)
378 while (ppd->vaddr < ppd->vaddr_end) {
379 sme_populate_pgd_large(ppd);
381 ppd->vaddr += PMD_PAGE_SIZE;
382 ppd->paddr += PMD_PAGE_SIZE;
386 static void __init __sme_map_range_pte(struct sme_populate_pgd_data *ppd)
388 while (ppd->vaddr < ppd->vaddr_end) {
389 sme_populate_pgd(ppd);
391 ppd->vaddr += PAGE_SIZE;
392 ppd->paddr += PAGE_SIZE;
396 static void __init __sme_map_range(struct sme_populate_pgd_data *ppd,
397 pmdval_t pmd_flags, pteval_t pte_flags)
399 unsigned long vaddr_end;
401 ppd->pmd_flags = pmd_flags;
402 ppd->pte_flags = pte_flags;
404 /* Save original end value since we modify the struct value */
405 vaddr_end = ppd->vaddr_end;
407 /* If start is not 2MB aligned, create PTE entries */
408 ppd->vaddr_end = ALIGN(ppd->vaddr, PMD_PAGE_SIZE);
409 __sme_map_range_pte(ppd);
411 /* Create PMD entries */
412 ppd->vaddr_end = vaddr_end & PMD_PAGE_MASK;
413 __sme_map_range_pmd(ppd);
415 /* If end is not 2MB aligned, create PTE entries */
416 ppd->vaddr_end = vaddr_end;
417 __sme_map_range_pte(ppd);
420 static void __init sme_map_range_encrypted(struct sme_populate_pgd_data *ppd)
422 __sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC);
425 static void __init sme_map_range_decrypted(struct sme_populate_pgd_data *ppd)
427 __sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC);
430 static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
432 __sme_map_range(ppd, PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP);
435 static unsigned long __init sme_pgtable_calc(unsigned long len)
437 unsigned long p4d_size, pud_size, pmd_size, pte_size;
441 * Perform a relatively simplistic calculation of the pagetable
442 * entries that are needed. Those mappings will be covered mostly
443 * by 2MB PMD entries so we can conservatively calculate the required
444 * number of P4D, PUD and PMD structures needed to perform the
445 * mappings. For mappings that are not 2MB aligned, PTE mappings
446 * would be needed for the start and end portion of the address range
447 * that fall outside of the 2MB alignment. This results in, at most,
448 * two extra pages to hold PTE entries for each range that is mapped.
449 * Incrementing the count for each covers the case where the addresses
452 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
453 p4d_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
454 p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
455 pud_size = (ALIGN(len, P4D_SIZE) / P4D_SIZE) + 1;
456 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
459 pud_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
460 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
462 pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1;
463 pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
464 pte_size = 2 * sizeof(pte_t) * PTRS_PER_PTE;
466 total = p4d_size + pud_size + pmd_size + pte_size;
469 * Now calculate the added pagetable structures needed to populate
470 * the new pagetables.
472 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
473 p4d_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
474 p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
475 pud_size = ALIGN(total, P4D_SIZE) / P4D_SIZE;
476 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
479 pud_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
480 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
482 pmd_size = ALIGN(total, PUD_SIZE) / PUD_SIZE;
483 pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
485 total += p4d_size + pud_size + pmd_size;
490 void __init __nostackprotector sme_encrypt_kernel(struct boot_params *bp)
492 unsigned long workarea_start, workarea_end, workarea_len;
493 unsigned long execute_start, execute_end, execute_len;
494 unsigned long kernel_start, kernel_end, kernel_len;
495 unsigned long initrd_start, initrd_end, initrd_len;
496 struct sme_populate_pgd_data ppd;
497 unsigned long pgtable_area_len;
498 unsigned long decrypted_base;
504 * Prepare for encrypting the kernel and initrd by building new
505 * pagetables with the necessary attributes needed to encrypt the
508 * One range of virtual addresses will map the memory occupied
509 * by the kernel and initrd as encrypted.
511 * Another range of virtual addresses will map the memory occupied
512 * by the kernel and initrd as decrypted and write-protected.
514 * The use of write-protect attribute will prevent any of the
515 * memory from being cached.
518 /* Physical addresses gives us the identity mapped virtual addresses */
519 kernel_start = __pa_symbol(_text);
520 kernel_end = ALIGN(__pa_symbol(_end), PMD_PAGE_SIZE);
521 kernel_len = kernel_end - kernel_start;
526 #ifdef CONFIG_BLK_DEV_INITRD
527 initrd_len = (unsigned long)bp->hdr.ramdisk_size |
528 ((unsigned long)bp->ext_ramdisk_size << 32);
530 initrd_start = (unsigned long)bp->hdr.ramdisk_image |
531 ((unsigned long)bp->ext_ramdisk_image << 32);
532 initrd_end = PAGE_ALIGN(initrd_start + initrd_len);
533 initrd_len = initrd_end - initrd_start;
537 /* Set the encryption workarea to be immediately after the kernel */
538 workarea_start = kernel_end;
541 * Calculate required number of workarea bytes needed:
542 * executable encryption area size:
543 * stack page (PAGE_SIZE)
544 * encryption routine page (PAGE_SIZE)
545 * intermediate copy buffer (PMD_PAGE_SIZE)
546 * pagetable structures for the encryption of the kernel
547 * pagetable structures for workarea (in case not currently mapped)
549 execute_start = workarea_start;
550 execute_end = execute_start + (PAGE_SIZE * 2) + PMD_PAGE_SIZE;
551 execute_len = execute_end - execute_start;
554 * One PGD for both encrypted and decrypted mappings and a set of
555 * PUDs and PMDs for each of the encrypted and decrypted mappings.
557 pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD;
558 pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2;
560 pgtable_area_len += sme_pgtable_calc(initrd_len) * 2;
562 /* PUDs and PMDs needed in the current pagetables for the workarea */
563 pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len);
566 * The total workarea includes the executable encryption area and
567 * the pagetable area. The start of the workarea is already 2MB
568 * aligned, align the end of the workarea on a 2MB boundary so that
569 * we don't try to create/allocate PTE entries from the workarea
570 * before it is mapped.
572 workarea_len = execute_len + pgtable_area_len;
573 workarea_end = ALIGN(workarea_start + workarea_len, PMD_PAGE_SIZE);
576 * Set the address to the start of where newly created pagetable
577 * structures (PGDs, PUDs and PMDs) will be allocated. New pagetable
578 * structures are created when the workarea is added to the current
579 * pagetables and when the new encrypted and decrypted kernel
580 * mappings are populated.
582 ppd.pgtable_area = (void *)execute_end;
585 * Make sure the current pagetable structure has entries for
586 * addressing the workarea.
588 ppd.pgd = (pgd_t *)native_read_cr3_pa();
589 ppd.paddr = workarea_start;
590 ppd.vaddr = workarea_start;
591 ppd.vaddr_end = workarea_end;
592 sme_map_range_decrypted(&ppd);
594 /* Flush the TLB - no globals so cr3 is enough */
595 native_write_cr3(__native_read_cr3());
598 * A new pagetable structure is being built to allow for the kernel
599 * and initrd to be encrypted. It starts with an empty PGD that will
600 * then be populated with new PUDs and PMDs as the encrypted and
601 * decrypted kernel mappings are created.
603 ppd.pgd = ppd.pgtable_area;
604 memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD);
605 ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD;
608 * A different PGD index/entry must be used to get different
609 * pagetable entries for the decrypted mapping. Choose the next
610 * PGD index and convert it to a virtual address to be used as
611 * the base of the mapping.
613 decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1);
615 unsigned long check_base;
617 check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1);
618 decrypted_base = max(decrypted_base, check_base);
620 decrypted_base <<= PGDIR_SHIFT;
622 /* Add encrypted kernel (identity) mappings */
623 ppd.paddr = kernel_start;
624 ppd.vaddr = kernel_start;
625 ppd.vaddr_end = kernel_end;
626 sme_map_range_encrypted(&ppd);
628 /* Add decrypted, write-protected kernel (non-identity) mappings */
629 ppd.paddr = kernel_start;
630 ppd.vaddr = kernel_start + decrypted_base;
631 ppd.vaddr_end = kernel_end + decrypted_base;
632 sme_map_range_decrypted_wp(&ppd);
635 /* Add encrypted initrd (identity) mappings */
636 ppd.paddr = initrd_start;
637 ppd.vaddr = initrd_start;
638 ppd.vaddr_end = initrd_end;
639 sme_map_range_encrypted(&ppd);
641 * Add decrypted, write-protected initrd (non-identity) mappings
643 ppd.paddr = initrd_start;
644 ppd.vaddr = initrd_start + decrypted_base;
645 ppd.vaddr_end = initrd_end + decrypted_base;
646 sme_map_range_decrypted_wp(&ppd);
649 /* Add decrypted workarea mappings to both kernel mappings */
650 ppd.paddr = workarea_start;
651 ppd.vaddr = workarea_start;
652 ppd.vaddr_end = workarea_end;
653 sme_map_range_decrypted(&ppd);
655 ppd.paddr = workarea_start;
656 ppd.vaddr = workarea_start + decrypted_base;
657 ppd.vaddr_end = workarea_end + decrypted_base;
658 sme_map_range_decrypted(&ppd);
660 /* Perform the encryption */
661 sme_encrypt_execute(kernel_start, kernel_start + decrypted_base,
662 kernel_len, workarea_start, (unsigned long)ppd.pgd);
665 sme_encrypt_execute(initrd_start, initrd_start + decrypted_base,
666 initrd_len, workarea_start,
667 (unsigned long)ppd.pgd);
670 * At this point we are running encrypted. Remove the mappings for
671 * the decrypted areas - all that is needed for this is to remove
672 * the PGD entry/entries.
674 ppd.vaddr = kernel_start + decrypted_base;
675 ppd.vaddr_end = kernel_end + decrypted_base;
679 ppd.vaddr = initrd_start + decrypted_base;
680 ppd.vaddr_end = initrd_end + decrypted_base;
684 ppd.vaddr = workarea_start + decrypted_base;
685 ppd.vaddr_end = workarea_end + decrypted_base;
688 /* Flush the TLB - no globals so cr3 is enough */
689 native_write_cr3(__native_read_cr3());
692 void __init __nostackprotector sme_enable(struct boot_params *bp)
694 const char *cmdline_ptr, *cmdline_arg, *cmdline_on, *cmdline_off;
695 unsigned int eax, ebx, ecx, edx;
696 bool active_by_default;
697 unsigned long me_mask;
701 /* Check for the SME support leaf */
704 native_cpuid(&eax, &ebx, &ecx, &edx);
705 if (eax < 0x8000001f)
709 * Check for the SME feature:
710 * CPUID Fn8000_001F[EAX] - Bit 0
711 * Secure Memory Encryption support
712 * CPUID Fn8000_001F[EBX] - Bits 5:0
713 * Pagetable bit position used to indicate encryption
717 native_cpuid(&eax, &ebx, &ecx, &edx);
721 me_mask = 1UL << (ebx & 0x3f);
723 /* Check if SME is enabled */
724 msr = __rdmsr(MSR_K8_SYSCFG);
725 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
729 * Fixups have not been applied to phys_base yet and we're running
730 * identity mapped, so we must obtain the address to the SME command
731 * line argument data using rip-relative addressing.
733 asm ("lea sme_cmdline_arg(%%rip), %0"
735 : "p" (sme_cmdline_arg));
736 asm ("lea sme_cmdline_on(%%rip), %0"
738 : "p" (sme_cmdline_on));
739 asm ("lea sme_cmdline_off(%%rip), %0"
741 : "p" (sme_cmdline_off));
743 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT))
744 active_by_default = true;
746 active_by_default = false;
748 cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr |
749 ((u64)bp->ext_cmd_line_ptr << 32));
751 cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer));
753 if (!strncmp(buffer, cmdline_on, sizeof(buffer)))
754 sme_me_mask = me_mask;
755 else if (!strncmp(buffer, cmdline_off, sizeof(buffer)))
758 sme_me_mask = active_by_default ? me_mask : 0;