Skip to content

Commit 8260b98

Browse files
Kuppuswamy Sathyanarayanansuryasaimadhu
authored andcommitted
x86/sev: Use CC_ATTR attribute to generalize string I/O unroll
INS/OUTS are not supported in TDX guests and cause #UD. Kernel has to avoid them when running in TDX guest. To support existing usage, string I/O operations are unrolled using IN/OUT instructions. AMD SEV platform implements this support by adding unroll logic in ins#bwl()/outs#bwl() macros with SEV-specific checks. Since TDX VM guests will also need similar support, use CC_ATTR_GUEST_UNROLL_STRING_IO and generic cc_platform_has() API to implement it. String I/O helpers were the last users of sev_key_active() interface and sev_enable_key static key. Remove them. [ bp: Move comment too and do not delete it. ] Suggested-by: Tom Lendacky <[email protected]> Signed-off-by: Kuppuswamy Sathyanarayanan <[email protected]> Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Tony Luck <[email protected]> Reviewed-by: Tom Lendacky <[email protected]> Tested-by: Tom Lendacky <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 75cc9a8 commit 8260b98

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

arch/x86/include/asm/io.h

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#include <linux/string.h>
4242
#include <linux/compiler.h>
43+
#include <linux/cc_platform.h>
4344
#include <asm/page.h>
4445
#include <asm/early_ioremap.h>
4546
#include <asm/pgtable_types.h>
@@ -256,21 +257,6 @@ static inline void slow_down_io(void)
256257

257258
#endif
258259

259-
#ifdef CONFIG_AMD_MEM_ENCRYPT
260-
#include <linux/jump_label.h>
261-
262-
extern struct static_key_false sev_enable_key;
263-
static inline bool sev_key_active(void)
264-
{
265-
return static_branch_unlikely(&sev_enable_key);
266-
}
267-
268-
#else /* !CONFIG_AMD_MEM_ENCRYPT */
269-
270-
static inline bool sev_key_active(void) { return false; }
271-
272-
#endif /* CONFIG_AMD_MEM_ENCRYPT */
273-
274260
#define BUILDIO(bwl, bw, type) \
275261
static inline void out##bwl(unsigned type value, int port) \
276262
{ \
@@ -301,7 +287,7 @@ static inline unsigned type in##bwl##_p(int port) \
301287
\
302288
static inline void outs##bwl(int port, const void *addr, unsigned long count) \
303289
{ \
304-
if (sev_key_active()) { \
290+
if (cc_platform_has(CC_ATTR_GUEST_UNROLL_STRING_IO)) { \
305291
unsigned type *value = (unsigned type *)addr; \
306292
while (count) { \
307293
out##bwl(*value, port); \
@@ -317,7 +303,7 @@ static inline void outs##bwl(int port, const void *addr, unsigned long count) \
317303
\
318304
static inline void ins##bwl(int port, void *addr, unsigned long count) \
319305
{ \
320-
if (sev_key_active()) { \
306+
if (cc_platform_has(CC_ATTR_GUEST_UNROLL_STRING_IO)) { \
321307
unsigned type *value = (unsigned type *)addr; \
322308
while (count) { \
323309
*value = in##bwl(port); \

arch/x86/kernel/cc_platform.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ static bool amd_cc_platform_has(enum cc_attr attr)
5050
case CC_ATTR_GUEST_STATE_ENCRYPT:
5151
return sev_status & MSR_AMD64_SEV_ES_ENABLED;
5252

53+
/*
54+
* With SEV, the rep string I/O instructions need to be unrolled
55+
* but SEV-ES supports them through the #VC handler.
56+
*/
57+
case CC_ATTR_GUEST_UNROLL_STRING_IO:
58+
return (sev_status & MSR_AMD64_SEV_ENABLED) &&
59+
!(sev_status & MSR_AMD64_SEV_ES_ENABLED);
60+
5361
default:
5462
return false;
5563
}

arch/x86/mm/mem_encrypt.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ u64 sme_me_mask __section(".data") = 0;
4343
u64 sev_status __section(".data") = 0;
4444
u64 sev_check_data __section(".data") = 0;
4545
EXPORT_SYMBOL(sme_me_mask);
46-
DEFINE_STATIC_KEY_FALSE(sev_enable_key);
47-
EXPORT_SYMBOL_GPL(sev_enable_key);
4846

4947
/* Buffer used for early in-place encryption by BSP, no locking needed */
5048
static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
@@ -499,14 +497,6 @@ void __init mem_encrypt_init(void)
499497
/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
500498
swiotlb_update_mem_attributes();
501499

502-
/*
503-
* With SEV, we need to unroll the rep string I/O instructions,
504-
* but SEV-ES supports them through the #VC handler.
505-
*/
506-
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) &&
507-
!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
508-
static_branch_enable(&sev_enable_key);
509-
510500
print_mem_encrypt_feature_info();
511501
}
512502

include/linux/cc_platform.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ enum cc_attr {
6161
* Examples include SEV-ES.
6262
*/
6363
CC_ATTR_GUEST_STATE_ENCRYPT,
64+
65+
/**
66+
* @CC_ATTR_GUEST_UNROLL_STRING_IO: String I/O is implemented with
67+
* IN/OUT instructions
68+
*
69+
* The platform/OS is running as a guest/virtual machine and uses
70+
* IN/OUT instructions in place of string I/O.
71+
*
72+
* Examples include TDX guest & SEV.
73+
*/
74+
CC_ATTR_GUEST_UNROLL_STRING_IO,
6475
};
6576

6677
#ifdef CONFIG_ARCH_HAS_CC_PLATFORM

0 commit comments

Comments
 (0)