Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * AMD Memory Encryption Support
0004  *
0005  * Copyright (C) 2016 Advanced Micro Devices, Inc.
0006  *
0007  * Author: Tom Lendacky <thomas.lendacky@amd.com>
0008  */
0009 
0010 #define DISABLE_BRANCH_PROFILING
0011 
0012 /*
0013  * Since we're dealing with identity mappings, physical and virtual
0014  * addresses are the same, so override these defines which are ultimately
0015  * used by the headers in misc.h.
0016  */
0017 #define __pa(x)  ((unsigned long)(x))
0018 #define __va(x)  ((void *)((unsigned long)(x)))
0019 
0020 /*
0021  * Special hack: we have to be careful, because no indirections are
0022  * allowed here, and paravirt_ops is a kind of one. As it will only run in
0023  * baremetal anyway, we just keep it from happening. (This list needs to
0024  * be extended when new paravirt and debugging variants are added.)
0025  */
0026 #undef CONFIG_PARAVIRT
0027 #undef CONFIG_PARAVIRT_XXL
0028 #undef CONFIG_PARAVIRT_SPINLOCKS
0029 
0030 /*
0031  * This code runs before CPU feature bits are set. By default, the
0032  * pgtable_l5_enabled() function uses bit X86_FEATURE_LA57 to determine if
0033  * 5-level paging is active, so that won't work here. USE_EARLY_PGTABLE_L5
0034  * is provided to handle this situation and, instead, use a variable that
0035  * has been set by the early boot code.
0036  */
0037 #define USE_EARLY_PGTABLE_L5
0038 
0039 #include <linux/kernel.h>
0040 #include <linux/mm.h>
0041 #include <linux/mem_encrypt.h>
0042 #include <linux/cc_platform.h>
0043 
0044 #include <asm/setup.h>
0045 #include <asm/sections.h>
0046 #include <asm/cmdline.h>
0047 #include <asm/coco.h>
0048 #include <asm/sev.h>
0049 
0050 #include "mm_internal.h"
0051 
0052 #define PGD_FLAGS       _KERNPG_TABLE_NOENC
0053 #define P4D_FLAGS       _KERNPG_TABLE_NOENC
0054 #define PUD_FLAGS       _KERNPG_TABLE_NOENC
0055 #define PMD_FLAGS       _KERNPG_TABLE_NOENC
0056 
0057 #define PMD_FLAGS_LARGE     (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL)
0058 
0059 #define PMD_FLAGS_DEC       PMD_FLAGS_LARGE
0060 #define PMD_FLAGS_DEC_WP    ((PMD_FLAGS_DEC & ~_PAGE_LARGE_CACHE_MASK) | \
0061                  (_PAGE_PAT_LARGE | _PAGE_PWT))
0062 
0063 #define PMD_FLAGS_ENC       (PMD_FLAGS_LARGE | _PAGE_ENC)
0064 
0065 #define PTE_FLAGS       (__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL)
0066 
0067 #define PTE_FLAGS_DEC       PTE_FLAGS
0068 #define PTE_FLAGS_DEC_WP    ((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \
0069                  (_PAGE_PAT | _PAGE_PWT))
0070 
0071 #define PTE_FLAGS_ENC       (PTE_FLAGS | _PAGE_ENC)
0072 
0073 struct sme_populate_pgd_data {
0074     void    *pgtable_area;
0075     pgd_t   *pgd;
0076 
0077     pmdval_t pmd_flags;
0078     pteval_t pte_flags;
0079     unsigned long paddr;
0080 
0081     unsigned long vaddr;
0082     unsigned long vaddr_end;
0083 };
0084 
0085 /*
0086  * This work area lives in the .init.scratch section, which lives outside of
0087  * the kernel proper. It is sized to hold the intermediate copy buffer and
0088  * more than enough pagetable pages.
0089  *
0090  * By using this section, the kernel can be encrypted in place and it
0091  * avoids any possibility of boot parameters or initramfs images being
0092  * placed such that the in-place encryption logic overwrites them.  This
0093  * section is 2MB aligned to allow for simple pagetable setup using only
0094  * PMD entries (see vmlinux.lds.S).
0095  */
0096 static char sme_workarea[2 * PMD_PAGE_SIZE] __section(".init.scratch");
0097 
0098 static char sme_cmdline_arg[] __initdata = "mem_encrypt";
0099 static char sme_cmdline_on[]  __initdata = "on";
0100 static char sme_cmdline_off[] __initdata = "off";
0101 
0102 static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
0103 {
0104     unsigned long pgd_start, pgd_end, pgd_size;
0105     pgd_t *pgd_p;
0106 
0107     pgd_start = ppd->vaddr & PGDIR_MASK;
0108     pgd_end = ppd->vaddr_end & PGDIR_MASK;
0109 
0110     pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
0111 
0112     pgd_p = ppd->pgd + pgd_index(ppd->vaddr);
0113 
0114     memset(pgd_p, 0, pgd_size);
0115 }
0116 
0117 static pud_t __init *sme_prepare_pgd(struct sme_populate_pgd_data *ppd)
0118 {
0119     pgd_t *pgd;
0120     p4d_t *p4d;
0121     pud_t *pud;
0122     pmd_t *pmd;
0123 
0124     pgd = ppd->pgd + pgd_index(ppd->vaddr);
0125     if (pgd_none(*pgd)) {
0126         p4d = ppd->pgtable_area;
0127         memset(p4d, 0, sizeof(*p4d) * PTRS_PER_P4D);
0128         ppd->pgtable_area += sizeof(*p4d) * PTRS_PER_P4D;
0129         set_pgd(pgd, __pgd(PGD_FLAGS | __pa(p4d)));
0130     }
0131 
0132     p4d = p4d_offset(pgd, ppd->vaddr);
0133     if (p4d_none(*p4d)) {
0134         pud = ppd->pgtable_area;
0135         memset(pud, 0, sizeof(*pud) * PTRS_PER_PUD);
0136         ppd->pgtable_area += sizeof(*pud) * PTRS_PER_PUD;
0137         set_p4d(p4d, __p4d(P4D_FLAGS | __pa(pud)));
0138     }
0139 
0140     pud = pud_offset(p4d, ppd->vaddr);
0141     if (pud_none(*pud)) {
0142         pmd = ppd->pgtable_area;
0143         memset(pmd, 0, sizeof(*pmd) * PTRS_PER_PMD);
0144         ppd->pgtable_area += sizeof(*pmd) * PTRS_PER_PMD;
0145         set_pud(pud, __pud(PUD_FLAGS | __pa(pmd)));
0146     }
0147 
0148     if (pud_large(*pud))
0149         return NULL;
0150 
0151     return pud;
0152 }
0153 
0154 static void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd)
0155 {
0156     pud_t *pud;
0157     pmd_t *pmd;
0158 
0159     pud = sme_prepare_pgd(ppd);
0160     if (!pud)
0161         return;
0162 
0163     pmd = pmd_offset(pud, ppd->vaddr);
0164     if (pmd_large(*pmd))
0165         return;
0166 
0167     set_pmd(pmd, __pmd(ppd->paddr | ppd->pmd_flags));
0168 }
0169 
0170 static void __init sme_populate_pgd(struct sme_populate_pgd_data *ppd)
0171 {
0172     pud_t *pud;
0173     pmd_t *pmd;
0174     pte_t *pte;
0175 
0176     pud = sme_prepare_pgd(ppd);
0177     if (!pud)
0178         return;
0179 
0180     pmd = pmd_offset(pud, ppd->vaddr);
0181     if (pmd_none(*pmd)) {
0182         pte = ppd->pgtable_area;
0183         memset(pte, 0, sizeof(*pte) * PTRS_PER_PTE);
0184         ppd->pgtable_area += sizeof(*pte) * PTRS_PER_PTE;
0185         set_pmd(pmd, __pmd(PMD_FLAGS | __pa(pte)));
0186     }
0187 
0188     if (pmd_large(*pmd))
0189         return;
0190 
0191     pte = pte_offset_map(pmd, ppd->vaddr);
0192     if (pte_none(*pte))
0193         set_pte(pte, __pte(ppd->paddr | ppd->pte_flags));
0194 }
0195 
0196 static void __init __sme_map_range_pmd(struct sme_populate_pgd_data *ppd)
0197 {
0198     while (ppd->vaddr < ppd->vaddr_end) {
0199         sme_populate_pgd_large(ppd);
0200 
0201         ppd->vaddr += PMD_PAGE_SIZE;
0202         ppd->paddr += PMD_PAGE_SIZE;
0203     }
0204 }
0205 
0206 static void __init __sme_map_range_pte(struct sme_populate_pgd_data *ppd)
0207 {
0208     while (ppd->vaddr < ppd->vaddr_end) {
0209         sme_populate_pgd(ppd);
0210 
0211         ppd->vaddr += PAGE_SIZE;
0212         ppd->paddr += PAGE_SIZE;
0213     }
0214 }
0215 
0216 static void __init __sme_map_range(struct sme_populate_pgd_data *ppd,
0217                    pmdval_t pmd_flags, pteval_t pte_flags)
0218 {
0219     unsigned long vaddr_end;
0220 
0221     ppd->pmd_flags = pmd_flags;
0222     ppd->pte_flags = pte_flags;
0223 
0224     /* Save original end value since we modify the struct value */
0225     vaddr_end = ppd->vaddr_end;
0226 
0227     /* If start is not 2MB aligned, create PTE entries */
0228     ppd->vaddr_end = ALIGN(ppd->vaddr, PMD_PAGE_SIZE);
0229     __sme_map_range_pte(ppd);
0230 
0231     /* Create PMD entries */
0232     ppd->vaddr_end = vaddr_end & PMD_PAGE_MASK;
0233     __sme_map_range_pmd(ppd);
0234 
0235     /* If end is not 2MB aligned, create PTE entries */
0236     ppd->vaddr_end = vaddr_end;
0237     __sme_map_range_pte(ppd);
0238 }
0239 
0240 static void __init sme_map_range_encrypted(struct sme_populate_pgd_data *ppd)
0241 {
0242     __sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC);
0243 }
0244 
0245 static void __init sme_map_range_decrypted(struct sme_populate_pgd_data *ppd)
0246 {
0247     __sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC);
0248 }
0249 
0250 static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
0251 {
0252     __sme_map_range(ppd, PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP);
0253 }
0254 
0255 static unsigned long __init sme_pgtable_calc(unsigned long len)
0256 {
0257     unsigned long entries = 0, tables = 0;
0258 
0259     /*
0260      * Perform a relatively simplistic calculation of the pagetable
0261      * entries that are needed. Those mappings will be covered mostly
0262      * by 2MB PMD entries so we can conservatively calculate the required
0263      * number of P4D, PUD and PMD structures needed to perform the
0264      * mappings.  For mappings that are not 2MB aligned, PTE mappings
0265      * would be needed for the start and end portion of the address range
0266      * that fall outside of the 2MB alignment.  This results in, at most,
0267      * two extra pages to hold PTE entries for each range that is mapped.
0268      * Incrementing the count for each covers the case where the addresses
0269      * cross entries.
0270      */
0271 
0272     /* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */
0273     if (PTRS_PER_P4D > 1)
0274         entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D;
0275     entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD;
0276     entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD;
0277     entries += 2 * sizeof(pte_t) * PTRS_PER_PTE;
0278 
0279     /*
0280      * Now calculate the added pagetable structures needed to populate
0281      * the new pagetables.
0282      */
0283 
0284     if (PTRS_PER_P4D > 1)
0285         tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D;
0286     tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD;
0287     tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD;
0288 
0289     return entries + tables;
0290 }
0291 
0292 void __init sme_encrypt_kernel(struct boot_params *bp)
0293 {
0294     unsigned long workarea_start, workarea_end, workarea_len;
0295     unsigned long execute_start, execute_end, execute_len;
0296     unsigned long kernel_start, kernel_end, kernel_len;
0297     unsigned long initrd_start, initrd_end, initrd_len;
0298     struct sme_populate_pgd_data ppd;
0299     unsigned long pgtable_area_len;
0300     unsigned long decrypted_base;
0301 
0302     /*
0303      * This is early code, use an open coded check for SME instead of
0304      * using cc_platform_has(). This eliminates worries about removing
0305      * instrumentation or checking boot_cpu_data in the cc_platform_has()
0306      * function.
0307      */
0308     if (!sme_get_me_mask() || sev_status & MSR_AMD64_SEV_ENABLED)
0309         return;
0310 
0311     /*
0312      * Prepare for encrypting the kernel and initrd by building new
0313      * pagetables with the necessary attributes needed to encrypt the
0314      * kernel in place.
0315      *
0316      *   One range of virtual addresses will map the memory occupied
0317      *   by the kernel and initrd as encrypted.
0318      *
0319      *   Another range of virtual addresses will map the memory occupied
0320      *   by the kernel and initrd as decrypted and write-protected.
0321      *
0322      *     The use of write-protect attribute will prevent any of the
0323      *     memory from being cached.
0324      */
0325 
0326     /* Physical addresses gives us the identity mapped virtual addresses */
0327     kernel_start = __pa_symbol(_text);
0328     kernel_end = ALIGN(__pa_symbol(_end), PMD_PAGE_SIZE);
0329     kernel_len = kernel_end - kernel_start;
0330 
0331     initrd_start = 0;
0332     initrd_end = 0;
0333     initrd_len = 0;
0334 #ifdef CONFIG_BLK_DEV_INITRD
0335     initrd_len = (unsigned long)bp->hdr.ramdisk_size |
0336              ((unsigned long)bp->ext_ramdisk_size << 32);
0337     if (initrd_len) {
0338         initrd_start = (unsigned long)bp->hdr.ramdisk_image |
0339                    ((unsigned long)bp->ext_ramdisk_image << 32);
0340         initrd_end = PAGE_ALIGN(initrd_start + initrd_len);
0341         initrd_len = initrd_end - initrd_start;
0342     }
0343 #endif
0344 
0345     /*
0346      * We're running identity mapped, so we must obtain the address to the
0347      * SME encryption workarea using rip-relative addressing.
0348      */
0349     asm ("lea sme_workarea(%%rip), %0"
0350          : "=r" (workarea_start)
0351          : "p" (sme_workarea));
0352 
0353     /*
0354      * Calculate required number of workarea bytes needed:
0355      *   executable encryption area size:
0356      *     stack page (PAGE_SIZE)
0357      *     encryption routine page (PAGE_SIZE)
0358      *     intermediate copy buffer (PMD_PAGE_SIZE)
0359      *   pagetable structures for the encryption of the kernel
0360      *   pagetable structures for workarea (in case not currently mapped)
0361      */
0362     execute_start = workarea_start;
0363     execute_end = execute_start + (PAGE_SIZE * 2) + PMD_PAGE_SIZE;
0364     execute_len = execute_end - execute_start;
0365 
0366     /*
0367      * One PGD for both encrypted and decrypted mappings and a set of
0368      * PUDs and PMDs for each of the encrypted and decrypted mappings.
0369      */
0370     pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD;
0371     pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2;
0372     if (initrd_len)
0373         pgtable_area_len += sme_pgtable_calc(initrd_len) * 2;
0374 
0375     /* PUDs and PMDs needed in the current pagetables for the workarea */
0376     pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len);
0377 
0378     /*
0379      * The total workarea includes the executable encryption area and
0380      * the pagetable area. The start of the workarea is already 2MB
0381      * aligned, align the end of the workarea on a 2MB boundary so that
0382      * we don't try to create/allocate PTE entries from the workarea
0383      * before it is mapped.
0384      */
0385     workarea_len = execute_len + pgtable_area_len;
0386     workarea_end = ALIGN(workarea_start + workarea_len, PMD_PAGE_SIZE);
0387 
0388     /*
0389      * Set the address to the start of where newly created pagetable
0390      * structures (PGDs, PUDs and PMDs) will be allocated. New pagetable
0391      * structures are created when the workarea is added to the current
0392      * pagetables and when the new encrypted and decrypted kernel
0393      * mappings are populated.
0394      */
0395     ppd.pgtable_area = (void *)execute_end;
0396 
0397     /*
0398      * Make sure the current pagetable structure has entries for
0399      * addressing the workarea.
0400      */
0401     ppd.pgd = (pgd_t *)native_read_cr3_pa();
0402     ppd.paddr = workarea_start;
0403     ppd.vaddr = workarea_start;
0404     ppd.vaddr_end = workarea_end;
0405     sme_map_range_decrypted(&ppd);
0406 
0407     /* Flush the TLB - no globals so cr3 is enough */
0408     native_write_cr3(__native_read_cr3());
0409 
0410     /*
0411      * A new pagetable structure is being built to allow for the kernel
0412      * and initrd to be encrypted. It starts with an empty PGD that will
0413      * then be populated with new PUDs and PMDs as the encrypted and
0414      * decrypted kernel mappings are created.
0415      */
0416     ppd.pgd = ppd.pgtable_area;
0417     memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD);
0418     ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD;
0419 
0420     /*
0421      * A different PGD index/entry must be used to get different
0422      * pagetable entries for the decrypted mapping. Choose the next
0423      * PGD index and convert it to a virtual address to be used as
0424      * the base of the mapping.
0425      */
0426     decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1);
0427     if (initrd_len) {
0428         unsigned long check_base;
0429 
0430         check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1);
0431         decrypted_base = max(decrypted_base, check_base);
0432     }
0433     decrypted_base <<= PGDIR_SHIFT;
0434 
0435     /* Add encrypted kernel (identity) mappings */
0436     ppd.paddr = kernel_start;
0437     ppd.vaddr = kernel_start;
0438     ppd.vaddr_end = kernel_end;
0439     sme_map_range_encrypted(&ppd);
0440 
0441     /* Add decrypted, write-protected kernel (non-identity) mappings */
0442     ppd.paddr = kernel_start;
0443     ppd.vaddr = kernel_start + decrypted_base;
0444     ppd.vaddr_end = kernel_end + decrypted_base;
0445     sme_map_range_decrypted_wp(&ppd);
0446 
0447     if (initrd_len) {
0448         /* Add encrypted initrd (identity) mappings */
0449         ppd.paddr = initrd_start;
0450         ppd.vaddr = initrd_start;
0451         ppd.vaddr_end = initrd_end;
0452         sme_map_range_encrypted(&ppd);
0453         /*
0454          * Add decrypted, write-protected initrd (non-identity) mappings
0455          */
0456         ppd.paddr = initrd_start;
0457         ppd.vaddr = initrd_start + decrypted_base;
0458         ppd.vaddr_end = initrd_end + decrypted_base;
0459         sme_map_range_decrypted_wp(&ppd);
0460     }
0461 
0462     /* Add decrypted workarea mappings to both kernel mappings */
0463     ppd.paddr = workarea_start;
0464     ppd.vaddr = workarea_start;
0465     ppd.vaddr_end = workarea_end;
0466     sme_map_range_decrypted(&ppd);
0467 
0468     ppd.paddr = workarea_start;
0469     ppd.vaddr = workarea_start + decrypted_base;
0470     ppd.vaddr_end = workarea_end + decrypted_base;
0471     sme_map_range_decrypted(&ppd);
0472 
0473     /* Perform the encryption */
0474     sme_encrypt_execute(kernel_start, kernel_start + decrypted_base,
0475                 kernel_len, workarea_start, (unsigned long)ppd.pgd);
0476 
0477     if (initrd_len)
0478         sme_encrypt_execute(initrd_start, initrd_start + decrypted_base,
0479                     initrd_len, workarea_start,
0480                     (unsigned long)ppd.pgd);
0481 
0482     /*
0483      * At this point we are running encrypted.  Remove the mappings for
0484      * the decrypted areas - all that is needed for this is to remove
0485      * the PGD entry/entries.
0486      */
0487     ppd.vaddr = kernel_start + decrypted_base;
0488     ppd.vaddr_end = kernel_end + decrypted_base;
0489     sme_clear_pgd(&ppd);
0490 
0491     if (initrd_len) {
0492         ppd.vaddr = initrd_start + decrypted_base;
0493         ppd.vaddr_end = initrd_end + decrypted_base;
0494         sme_clear_pgd(&ppd);
0495     }
0496 
0497     ppd.vaddr = workarea_start + decrypted_base;
0498     ppd.vaddr_end = workarea_end + decrypted_base;
0499     sme_clear_pgd(&ppd);
0500 
0501     /* Flush the TLB - no globals so cr3 is enough */
0502     native_write_cr3(__native_read_cr3());
0503 }
0504 
0505 void __init sme_enable(struct boot_params *bp)
0506 {
0507     const char *cmdline_ptr, *cmdline_arg, *cmdline_on, *cmdline_off;
0508     unsigned int eax, ebx, ecx, edx;
0509     unsigned long feature_mask;
0510     bool active_by_default;
0511     unsigned long me_mask;
0512     char buffer[16];
0513     bool snp;
0514     u64 msr;
0515 
0516     snp = snp_init(bp);
0517 
0518     /* Check for the SME/SEV support leaf */
0519     eax = 0x80000000;
0520     ecx = 0;
0521     native_cpuid(&eax, &ebx, &ecx, &edx);
0522     if (eax < 0x8000001f)
0523         return;
0524 
0525 #define AMD_SME_BIT BIT(0)
0526 #define AMD_SEV_BIT BIT(1)
0527 
0528     /*
0529      * Check for the SME/SEV feature:
0530      *   CPUID Fn8000_001F[EAX]
0531      *   - Bit 0 - Secure Memory Encryption support
0532      *   - Bit 1 - Secure Encrypted Virtualization support
0533      *   CPUID Fn8000_001F[EBX]
0534      *   - Bits 5:0 - Pagetable bit position used to indicate encryption
0535      */
0536     eax = 0x8000001f;
0537     ecx = 0;
0538     native_cpuid(&eax, &ebx, &ecx, &edx);
0539     /* Check whether SEV or SME is supported */
0540     if (!(eax & (AMD_SEV_BIT | AMD_SME_BIT)))
0541         return;
0542 
0543     me_mask = 1UL << (ebx & 0x3f);
0544 
0545     /* Check the SEV MSR whether SEV or SME is enabled */
0546     sev_status   = __rdmsr(MSR_AMD64_SEV);
0547     feature_mask = (sev_status & MSR_AMD64_SEV_ENABLED) ? AMD_SEV_BIT : AMD_SME_BIT;
0548 
0549     /* The SEV-SNP CC blob should never be present unless SEV-SNP is enabled. */
0550     if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
0551         snp_abort();
0552 
0553     /* Check if memory encryption is enabled */
0554     if (feature_mask == AMD_SME_BIT) {
0555         /*
0556          * No SME if Hypervisor bit is set. This check is here to
0557          * prevent a guest from trying to enable SME. For running as a
0558          * KVM guest the MSR_AMD64_SYSCFG will be sufficient, but there
0559          * might be other hypervisors which emulate that MSR as non-zero
0560          * or even pass it through to the guest.
0561          * A malicious hypervisor can still trick a guest into this
0562          * path, but there is no way to protect against that.
0563          */
0564         eax = 1;
0565         ecx = 0;
0566         native_cpuid(&eax, &ebx, &ecx, &edx);
0567         if (ecx & BIT(31))
0568             return;
0569 
0570         /* For SME, check the SYSCFG MSR */
0571         msr = __rdmsr(MSR_AMD64_SYSCFG);
0572         if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
0573             return;
0574     } else {
0575         /* SEV state cannot be controlled by a command line option */
0576         sme_me_mask = me_mask;
0577         goto out;
0578     }
0579 
0580     /*
0581      * Fixups have not been applied to phys_base yet and we're running
0582      * identity mapped, so we must obtain the address to the SME command
0583      * line argument data using rip-relative addressing.
0584      */
0585     asm ("lea sme_cmdline_arg(%%rip), %0"
0586          : "=r" (cmdline_arg)
0587          : "p" (sme_cmdline_arg));
0588     asm ("lea sme_cmdline_on(%%rip), %0"
0589          : "=r" (cmdline_on)
0590          : "p" (sme_cmdline_on));
0591     asm ("lea sme_cmdline_off(%%rip), %0"
0592          : "=r" (cmdline_off)
0593          : "p" (sme_cmdline_off));
0594 
0595     if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT))
0596         active_by_default = true;
0597     else
0598         active_by_default = false;
0599 
0600     cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr |
0601                      ((u64)bp->ext_cmd_line_ptr << 32));
0602 
0603     cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer));
0604 
0605     if (!strncmp(buffer, cmdline_on, sizeof(buffer)))
0606         sme_me_mask = me_mask;
0607     else if (!strncmp(buffer, cmdline_off, sizeof(buffer)))
0608         sme_me_mask = 0;
0609     else
0610         sme_me_mask = active_by_default ? me_mask : 0;
0611 out:
0612     if (sme_me_mask) {
0613         physical_mask &= ~sme_me_mask;
0614         cc_set_vendor(CC_VENDOR_AMD);
0615         cc_set_mask(sme_me_mask);
0616     }
0617 }