Skip to content

Commit aa5a461

Browse files
tlendackysuryasaimadhu
authored andcommitted
x86/sev: Add an x86 version of cc_platform_has()
Introduce an x86 version of the cc_platform_has() function. This will be used to replace vendor specific calls like sme_active(), sev_active(), etc. Signed-off-by: Tom Lendacky <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 46b49b1 commit aa5a461

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ config AMD_MEM_ENCRYPT
15181518
select ARCH_HAS_FORCE_DMA_UNENCRYPTED
15191519
select INSTRUCTION_DECODER
15201520
select ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS
1521+
select ARCH_HAS_CC_PLATFORM
15211522
help
15221523
Say yes to enable support for the encryption of system memory.
15231524
This requires an AMD processor that supports Secure Memory

arch/x86/include/asm/mem_encrypt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef __ASSEMBLY__
1414

1515
#include <linux/init.h>
16+
#include <linux/cc_platform.h>
1617

1718
#include <asm/bootparam.h>
1819

arch/x86/kernel/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ CFLAGS_REMOVE_ftrace.o = -pg
2121
CFLAGS_REMOVE_early_printk.o = -pg
2222
CFLAGS_REMOVE_head64.o = -pg
2323
CFLAGS_REMOVE_sev.o = -pg
24+
CFLAGS_REMOVE_cc_platform.o = -pg
2425
endif
2526

2627
KASAN_SANITIZE_head$(BITS).o := n
@@ -29,6 +30,7 @@ KASAN_SANITIZE_dumpstack_$(BITS).o := n
2930
KASAN_SANITIZE_stacktrace.o := n
3031
KASAN_SANITIZE_paravirt.o := n
3132
KASAN_SANITIZE_sev.o := n
33+
KASAN_SANITIZE_cc_platform.o := n
3234

3335
# With some compiler versions the generated code results in boot hangs, caused
3436
# by several compilation units. To be safe, disable all instrumentation.
@@ -47,6 +49,7 @@ endif
4749
KCOV_INSTRUMENT := n
4850

4951
CFLAGS_head$(BITS).o += -fno-stack-protector
52+
CFLAGS_cc_platform.o += -fno-stack-protector
5053

5154
CFLAGS_irq.o := -I $(srctree)/$(src)/../include/asm/trace
5255

@@ -147,6 +150,9 @@ obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o
147150
obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o
148151

149152
obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev.o
153+
154+
obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += cc_platform.o
155+
150156
###
151157
# 64 bit specific files
152158
ifeq ($(CONFIG_X86_64),y)

arch/x86/kernel/cc_platform.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Confidential Computing Platform Capability checks
4+
*
5+
* Copyright (C) 2021 Advanced Micro Devices, Inc.
6+
*
7+
* Author: Tom Lendacky <[email protected]>
8+
*/
9+
10+
#include <linux/export.h>
11+
#include <linux/cc_platform.h>
12+
#include <linux/mem_encrypt.h>
13+
14+
#include <asm/processor.h>
15+
16+
static bool __maybe_unused intel_cc_platform_has(enum cc_attr attr)
17+
{
18+
#ifdef CONFIG_INTEL_TDX_GUEST
19+
return false;
20+
#else
21+
return false;
22+
#endif
23+
}
24+
25+
/*
26+
* SME and SEV are very similar but they are not the same, so there are
27+
* times that the kernel will need to distinguish between SME and SEV. The
28+
* cc_platform_has() function is used for this. When a distinction isn't
29+
* needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
30+
*
31+
* The trampoline code is a good example for this requirement. Before
32+
* paging is activated, SME will access all memory as decrypted, but SEV
33+
* will access all memory as encrypted. So, when APs are being brought
34+
* up under SME the trampoline area cannot be encrypted, whereas under SEV
35+
* the trampoline area must be encrypted.
36+
*/
37+
static bool amd_cc_platform_has(enum cc_attr attr)
38+
{
39+
#ifdef CONFIG_AMD_MEM_ENCRYPT
40+
switch (attr) {
41+
case CC_ATTR_MEM_ENCRYPT:
42+
return sme_me_mask;
43+
44+
case CC_ATTR_HOST_MEM_ENCRYPT:
45+
return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
46+
47+
case CC_ATTR_GUEST_MEM_ENCRYPT:
48+
return sev_status & MSR_AMD64_SEV_ENABLED;
49+
50+
case CC_ATTR_GUEST_STATE_ENCRYPT:
51+
return sev_status & MSR_AMD64_SEV_ES_ENABLED;
52+
53+
default:
54+
return false;
55+
}
56+
#else
57+
return false;
58+
#endif
59+
}
60+
61+
62+
bool cc_platform_has(enum cc_attr attr)
63+
{
64+
if (sme_me_mask)
65+
return amd_cc_platform_has(attr);
66+
67+
return false;
68+
}
69+
EXPORT_SYMBOL_GPL(cc_platform_has);

arch/x86/mm/mem_encrypt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/bitops.h>
2121
#include <linux/dma-mapping.h>
2222
#include <linux/virtio_config.h>
23+
#include <linux/cc_platform.h>
2324

2425
#include <asm/tlbflush.h>
2526
#include <asm/fixmap.h>

0 commit comments

Comments
 (0)