Commit 4d96f910 authored by Tom Lendacky's avatar Tom Lendacky Committed by Borislav Petkov

x86/sev: Replace occurrences of sev_active() with cc_platform_has()

Replace uses of sev_active() with the more generic cc_platform_has()
using CC_ATTR_GUEST_MEM_ENCRYPT. If future support is added for other
memory encryption technologies, the use of CC_ATTR_GUEST_MEM_ENCRYPT
can be updated, as required.
Signed-off-by: default avatarTom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20210928191009.32551-7-bp@alien8.de
parent 32cb4d02
...@@ -51,7 +51,6 @@ void __init mem_encrypt_free_decrypted_mem(void); ...@@ -51,7 +51,6 @@ void __init mem_encrypt_free_decrypted_mem(void);
void __init mem_encrypt_init(void); void __init mem_encrypt_init(void);
void __init sev_es_init_vc_handling(void); void __init sev_es_init_vc_handling(void);
bool sev_active(void);
bool sev_es_active(void); bool sev_es_active(void);
#define __bss_decrypted __section(".bss..decrypted") #define __bss_decrypted __section(".bss..decrypted")
...@@ -75,7 +74,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { } ...@@ -75,7 +74,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
static inline void __init sme_enable(struct boot_params *bp) { } static inline void __init sme_enable(struct boot_params *bp) { }
static inline void sev_es_init_vc_handling(void) { } static inline void sev_es_init_vc_handling(void) { }
static inline bool sev_active(void) { return false; }
static inline bool sev_es_active(void) { return false; } static inline bool sev_es_active(void) { return false; }
static inline int __init static inline int __init
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <linux/crash_dump.h> #include <linux/crash_dump.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/cc_platform.h>
static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
unsigned long offset, int userbuf, unsigned long offset, int userbuf,
...@@ -73,5 +74,6 @@ ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize, ...@@ -73,5 +74,6 @@ ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos) ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
{ {
return read_from_oldmem(buf, count, ppos, 0, sev_active()); return read_from_oldmem(buf, count, ppos, 0,
cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT));
} }
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/nmi.h> #include <linux/nmi.h>
#include <linux/swait.h> #include <linux/swait.h>
#include <linux/syscore_ops.h> #include <linux/syscore_ops.h>
#include <linux/cc_platform.h>
#include <asm/timer.h> #include <asm/timer.h>
#include <asm/cpu.h> #include <asm/cpu.h>
#include <asm/traps.h> #include <asm/traps.h>
...@@ -418,7 +419,7 @@ static void __init sev_map_percpu_data(void) ...@@ -418,7 +419,7 @@ static void __init sev_map_percpu_data(void)
{ {
int cpu; int cpu;
if (!sev_active()) if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
return; return;
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
......
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/set_memory.h> #include <linux/set_memory.h>
#include <linux/cc_platform.h>
#include <asm/hypervisor.h> #include <asm/hypervisor.h>
#include <asm/mem_encrypt.h>
#include <asm/x86_init.h> #include <asm/x86_init.h>
#include <asm/kvmclock.h> #include <asm/kvmclock.h>
...@@ -223,7 +223,7 @@ static void __init kvmclock_init_mem(void) ...@@ -223,7 +223,7 @@ static void __init kvmclock_init_mem(void)
* hvclock is shared between the guest and the hypervisor, must * hvclock is shared between the guest and the hypervisor, must
* be mapped decrypted. * be mapped decrypted.
*/ */
if (sev_active()) { if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
r = set_memory_decrypted((unsigned long) hvclock_mem, r = set_memory_decrypted((unsigned long) hvclock_mem,
1UL << order); 1UL << order);
if (r) { if (r) {
......
...@@ -167,7 +167,7 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd) ...@@ -167,7 +167,7 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
} }
pte = pte_offset_kernel(pmd, vaddr); pte = pte_offset_kernel(pmd, vaddr);
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
prot = PAGE_KERNEL_EXEC; prot = PAGE_KERNEL_EXEC;
set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot)); set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
...@@ -207,7 +207,7 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable) ...@@ -207,7 +207,7 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
level4p = (pgd_t *)__va(start_pgtable); level4p = (pgd_t *)__va(start_pgtable);
clear_page(level4p); clear_page(level4p);
if (sev_active()) { if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
info.page_flag |= _PAGE_ENC; info.page_flag |= _PAGE_ENC;
info.kernpg_flag |= _PAGE_ENC; info.kernpg_flag |= _PAGE_ENC;
} }
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/swap.h> #include <linux/swap.h>
#include <linux/rwsem.h> #include <linux/rwsem.h>
#include <linux/cc_platform.h>
#include <asm/apic.h> #include <asm/apic.h>
#include <asm/perf_event.h> #include <asm/perf_event.h>
...@@ -455,7 +456,7 @@ static int has_svm(void) ...@@ -455,7 +456,7 @@ static int has_svm(void)
return 0; return 0;
} }
if (sev_active()) { if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
pr_info("KVM is unsupported when running as an SEV guest\n"); pr_info("KVM is unsupported when running as an SEV guest\n");
return 0; return 0;
} }
......
...@@ -92,7 +92,7 @@ static unsigned int __ioremap_check_ram(struct resource *res) ...@@ -92,7 +92,7 @@ static unsigned int __ioremap_check_ram(struct resource *res)
*/ */
static unsigned int __ioremap_check_encrypted(struct resource *res) static unsigned int __ioremap_check_encrypted(struct resource *res)
{ {
if (!sev_active()) if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
return 0; return 0;
switch (res->desc) { switch (res->desc) {
...@@ -112,7 +112,7 @@ static unsigned int __ioremap_check_encrypted(struct resource *res) ...@@ -112,7 +112,7 @@ static unsigned int __ioremap_check_encrypted(struct resource *res)
*/ */
static void __ioremap_check_other(resource_size_t addr, struct ioremap_desc *desc) static void __ioremap_check_other(resource_size_t addr, struct ioremap_desc *desc)
{ {
if (!sev_active()) if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
return; return;
if (!IS_ENABLED(CONFIG_EFI)) if (!IS_ENABLED(CONFIG_EFI))
...@@ -556,7 +556,7 @@ static bool memremap_should_map_decrypted(resource_size_t phys_addr, ...@@ -556,7 +556,7 @@ static bool memremap_should_map_decrypted(resource_size_t phys_addr,
case E820_TYPE_NVS: case E820_TYPE_NVS:
case E820_TYPE_UNUSABLE: case E820_TYPE_UNUSABLE:
/* For SEV, these areas are encrypted */ /* For SEV, these areas are encrypted */
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
break; break;
fallthrough; fallthrough;
......
...@@ -194,7 +194,7 @@ void __init sme_early_init(void) ...@@ -194,7 +194,7 @@ void __init sme_early_init(void)
for (i = 0; i < ARRAY_SIZE(protection_map); i++) for (i = 0; i < ARRAY_SIZE(protection_map); i++)
protection_map[i] = pgprot_encrypted(protection_map[i]); protection_map[i] = pgprot_encrypted(protection_map[i]);
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
swiotlb_force = SWIOTLB_FORCE; swiotlb_force = SWIOTLB_FORCE;
} }
...@@ -203,7 +203,7 @@ void __init sev_setup_arch(void) ...@@ -203,7 +203,7 @@ void __init sev_setup_arch(void)
phys_addr_t total_mem = memblock_phys_mem_size(); phys_addr_t total_mem = memblock_phys_mem_size();
unsigned long size; unsigned long size;
if (!sev_active()) if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
return; return;
/* /*
...@@ -364,8 +364,8 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) ...@@ -364,8 +364,8 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
/* /*
* SME and SEV are very similar but they are not the same, so there are * SME and SEV are very similar but they are not the same, so there are
* times that the kernel will need to distinguish between SME and SEV. The * times that the kernel will need to distinguish between SME and SEV. The
* sme_active() and sev_active() functions are used for this. When a * cc_platform_has() function is used for this. When a distinction isn't
* distinction isn't needed, the mem_encrypt_active() function can be used. * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
* *
* The trampoline code is a good example for this requirement. Before * The trampoline code is a good example for this requirement. Before
* paging is activated, SME will access all memory as decrypted, but SEV * paging is activated, SME will access all memory as decrypted, but SEV
...@@ -373,11 +373,6 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) ...@@ -373,11 +373,6 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
* up under SME the trampoline area cannot be encrypted, whereas under SEV * up under SME the trampoline area cannot be encrypted, whereas under SEV
* the trampoline area must be encrypted. * the trampoline area must be encrypted.
*/ */
bool sev_active(void)
{
return sev_status & MSR_AMD64_SEV_ENABLED;
}
EXPORT_SYMBOL_GPL(sev_active);
/* Needs to be called from non-instrumentable code */ /* Needs to be called from non-instrumentable code */
bool noinstr sev_es_active(void) bool noinstr sev_es_active(void)
...@@ -391,7 +386,7 @@ bool force_dma_unencrypted(struct device *dev) ...@@ -391,7 +386,7 @@ bool force_dma_unencrypted(struct device *dev)
/* /*
* For SEV, all DMA must be to unencrypted addresses. * For SEV, all DMA must be to unencrypted addresses.
*/ */
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
return true; return true;
/* /*
...@@ -450,7 +445,7 @@ static void print_mem_encrypt_feature_info(void) ...@@ -450,7 +445,7 @@ static void print_mem_encrypt_feature_info(void)
} }
/* Secure Encrypted Virtualization */ /* Secure Encrypted Virtualization */
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
pr_cont(" SEV"); pr_cont(" SEV");
/* Encrypted Register State */ /* Encrypted Register State */
...@@ -473,7 +468,7 @@ void __init mem_encrypt_init(void) ...@@ -473,7 +468,7 @@ void __init mem_encrypt_init(void)
* With SEV, we need to unroll the rep string I/O instructions, * With SEV, we need to unroll the rep string I/O instructions,
* but SEV-ES supports them through the #VC handler. * but SEV-ES supports them through the #VC handler.
*/ */
if (sev_active() && !sev_es_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && !sev_es_active())
static_branch_enable(&sev_enable_key); static_branch_enable(&sev_enable_key);
print_mem_encrypt_feature_info(); print_mem_encrypt_feature_info();
...@@ -481,6 +476,6 @@ void __init mem_encrypt_init(void) ...@@ -481,6 +476,6 @@ void __init mem_encrypt_init(void)
int arch_has_restricted_virtio_memory_access(void) int arch_has_restricted_virtio_memory_access(void)
{ {
return sev_active(); return cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT);
} }
EXPORT_SYMBOL_GPL(arch_has_restricted_virtio_memory_access); EXPORT_SYMBOL_GPL(arch_has_restricted_virtio_memory_access);
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#include <linux/reboot.h> #include <linux/reboot.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/ucs2_string.h> #include <linux/ucs2_string.h>
#include <linux/mem_encrypt.h> #include <linux/cc_platform.h>
#include <linux/sched/task.h> #include <linux/sched/task.h>
#include <asm/setup.h> #include <asm/setup.h>
...@@ -284,7 +284,8 @@ static void __init __map_region(efi_memory_desc_t *md, u64 va) ...@@ -284,7 +284,8 @@ static void __init __map_region(efi_memory_desc_t *md, u64 va)
if (!(md->attribute & EFI_MEMORY_WB)) if (!(md->attribute & EFI_MEMORY_WB))
flags |= _PAGE_PCD; flags |= _PAGE_PCD;
if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) &&
md->type != EFI_MEMORY_MAPPED_IO)
flags |= _PAGE_ENC; flags |= _PAGE_ENC;
pfn = md->phys_addr >> PAGE_SHIFT; pfn = md->phys_addr >> PAGE_SHIFT;
...@@ -390,7 +391,7 @@ static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *m ...@@ -390,7 +391,7 @@ static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *m
if (!(md->attribute & EFI_MEMORY_RO)) if (!(md->attribute & EFI_MEMORY_RO))
pf |= _PAGE_RW; pf |= _PAGE_RW;
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
pf |= _PAGE_ENC; pf |= _PAGE_ENC;
return efi_update_mappings(md, pf); return efi_update_mappings(md, pf);
...@@ -438,7 +439,7 @@ void __init efi_runtime_update_mappings(void) ...@@ -438,7 +439,7 @@ void __init efi_runtime_update_mappings(void)
(md->type != EFI_RUNTIME_SERVICES_CODE)) (md->type != EFI_RUNTIME_SERVICES_CODE))
pf |= _PAGE_RW; pf |= _PAGE_RW;
if (sev_active()) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
pf |= _PAGE_ENC; pf |= _PAGE_ENC;
efi_update_mappings(md, pf); efi_update_mappings(md, pf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment