Skip to contentSkip to conversation

x86: drop XSAVEOPT and CLWB build flags

PATCH In review Alexander M. Merritt 3 participants 7 messages Apr 3, 2025 – Apr 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: drop XSAVEOPT and CLWB build flags

The new toolchain baseline knows both the XSAVEOPT and CLWB instructions.

Resolves: https://gitlab.com/xen-project/xen/-/work_items/205

---
 xen/arch/x86/arch.mk              |  2 --
 xen/arch/x86/flushtlb.c           | 28 +---------------------------
 xen/arch/x86/include/asm/system.h |  7 -------
 3 files changed, 1 insertion(+), 36 deletions(-)
xen/arch/x86/arch.mk +0 -2
index 258e459bec..baa83418bc 100644
@@ -15,9 +15,7 @@ $(call as-option-add,CFLAGS,CC,"crc32 %eax$(comma)%eax",-DHAVE_AS_SSE4_2)
 $(call as-option-add,CFLAGS,CC,"invept (%rax)$(comma)%rax",-DHAVE_AS_EPT)
 $(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND)
 $(call as-option-add,CFLAGS,CC,"rdfsbase %rax",-DHAVE_AS_FSGSBASE)
-$(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT)
 $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
-$(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
 $(call as-option-add,CFLAGS,CC,".equ \"x\"$(comma)1",-DHAVE_AS_QUOTED_SYM)
 $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR)
xen/arch/x86/flushtlb.c +1 -27
index 65be0474a8..962bb87d69 100644
@@ -313,33 +313,7 @@ void cache_writeback(const void *addr, unsigned int size)
     clflush_size = current_cpu_data.x86_clflush_size ?: 16;
     addr -= (unsigned long)addr & (clflush_size - 1);
     for ( ; addr < end; addr += clflush_size )
-    {
-/*
- * The arguments to a macro must not include preprocessor directives. Doing so
- * results in undefined behavior, so we have to create some defines here in
- * order to avoid it.
- */
-#if defined(HAVE_AS_CLWB)
-# define CLWB_ENCODING "clwb %[p]"
-#elif defined(HAVE_AS_XSAVEOPT)
-# define CLWB_ENCODING "data16 xsaveopt %[p]" /* clwb */
-#else
-# define CLWB_ENCODING ".byte 0x66, 0x0f, 0xae, 0x30" /* clwb (%%rax) */
-#endif
-
-#define BASE_INPUT(addr) [p] "m" (*(const char *)(addr))
-#if defined(HAVE_AS_CLWB) || defined(HAVE_AS_XSAVEOPT)
-# define INPUT BASE_INPUT
-#else
-# define INPUT(addr) "a" (addr), BASE_INPUT(addr)
-#endif
-
-        asm volatile (CLWB_ENCODING :: INPUT(addr));
-
-#undef INPUT
-#undef BASE_INPUT
-#undef CLWB_ENCODING
-    }
+        asm volatile ("clwb %[p]" :: [p] "m" (*(const char *)(addr)));
 
     asm volatile ("sfence" ::: "memory");
 }
xen/arch/x86/include/asm/system.h +0 -7
index 8ceaaf45d1..c3529f99dd 100644
@@ -28,14 +28,7 @@ static inline void clflushopt(const void *p)
 
 static inline void clwb(const void *p)
 {
-#if defined(HAVE_AS_CLWB)
     asm volatile ( "clwb %0" :: "m" (*(const char *)p) );
-#elif defined(HAVE_AS_XSAVEOPT)
-    asm volatile ( "data16 xsaveopt %0" :: "m" (*(const char *)p) );
-#else
-    asm volatile ( ".byte 0x66, 0x0f, 0xae, 0x32"
-                   :: "d" (p), "m" (*(const char *)p) );
-#endif
 }
 
 #define xchg(ptr,v) \
Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Alexander M. Merritt

The new toolchain baseline knows both the XSAVEOPT and CLWB instructions.

I know that's what I wrote on the ticket, but what I'd forgotten was that we only use XSAVEOPT for it's operand.

Really what we're doing here is knowing CLWB, and also getting rid of the XSAVEOPT workaround for somewhat-more-old toolchains.

Quoted 49 lines

Resolves: https://gitlab.com/xen-project/xen/-/work_items/205

---
 xen/arch/x86/arch.mk              |  2 --
 xen/arch/x86/flushtlb.c           | 28 +---------------------------
 xen/arch/x86/include/asm/system.h |  7 -------
 3 files changed, 1 insertion(+), 36 deletions(-)
xen/arch/x86/arch.mk +0 -2
index 258e459bec..baa83418bc 100644
@@ -15,9 +15,7 @@ $(call as-option-add,CFLAGS,CC,"crc32 %eax$(comma)%eax",-DHAVE_AS_SSE4_2)
 $(call as-option-add,CFLAGS,CC,"invept (%rax)$(comma)%rax",-DHAVE_AS_EPT)
 $(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND)
 $(call as-option-add,CFLAGS,CC,"rdfsbase %rax",-DHAVE_AS_FSGSBASE)
-$(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT)
 $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
-$(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
 $(call as-option-add,CFLAGS,CC,".equ \"x\"$(comma)1",-DHAVE_AS_QUOTED_SYM)
 $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR)
xen/arch/x86/flushtlb.c +0 -18
index 65be0474a8..962bb87d69 100644
@@ -313,33 +313,7 @@ void cache_writeback(const void *addr, unsigned int size)
     clflush_size = current_cpu_data.x86_clflush_size ?: 16;
     addr -= (unsigned long)addr & (clflush_size - 1);
     for ( ; addr < end; addr += clflush_size )
-    {
-/*
- * The arguments to a macro must not include preprocessor directives. Doing so
- * results in undefined behavior, so we have to create some defines here in
- * order to avoid it.
- */
-#if defined(HAVE_AS_CLWB)
-# define CLWB_ENCODING "clwb %[p]"
-#elif defined(HAVE_AS_XSAVEOPT)
-# define CLWB_ENCODING "data16 xsaveopt %[p]" /* clwb */
-#else
-# define CLWB_ENCODING ".byte 0x66, 0x0f, 0xae, 0x30" /* clwb (%%rax) */
-#endif
-
-#define BASE_INPUT(addr) [p] "m" (*(const char *)(addr))
-#if defined(HAVE_AS_CLWB) || defined(HAVE_AS_XSAVEOPT)
-# define INPUT BASE_INPUT
-#else

xen/arch/x86/flushtlb.c void cache_writeback(const void *addr, unsigned int size) · line 316

Quoted 10 lines
xen/arch/x86/flushtlb.c +1 -9
-# define INPUT(addr) "a" (addr), BASE_INPUT(addr)
-#endif
-
-        asm volatile (CLWB_ENCODING :: INPUT(addr));
-
-#undef INPUT
-#undef BASE_INPUT
-#undef CLWB_ENCODING
-    }
+        asm volatile ("clwb %[p]" :: [p] "m" (*(const char *)(addr)));

One minor note about whitespace.  We typically have spaces inside the outermost brackets on asm statements, as per the clwb() example below.

Also, given the expression is so simple, I'd just use %0 and drop the [p].  It's just line-verbosity here.

Can fix both on commit if you're happy.

Quoted 22 lines
xen/arch/x86/flushtlb.c +0 -0
     asm volatile ("sfence" ::: "memory");
 }
xen/arch/x86/include/asm/system.h +0 -7
index 8ceaaf45d1..c3529f99dd 100644
@@ -28,14 +28,7 @@ static inline void clflushopt(const void *p)
 
 static inline void clwb(const void *p)
 {
-#if defined(HAVE_AS_CLWB)
     asm volatile ( "clwb %0" :: "m" (*(const char *)p) );
-#elif defined(HAVE_AS_XSAVEOPT)
-    asm volatile ( "data16 xsaveopt %0" :: "m" (*(const char *)p) );
-#else
-    asm volatile ( ".byte 0x66, 0x0f, 0xae, 0x32"
-                   :: "d" (p), "m" (*(const char *)p) );
-#endif
 }
 
 #define xchg(ptr,v) \
#2 lore raw
Quoted 69 lines · Andrew Cooper
Quoted 1 line · Alexander M. Merritt

The new toolchain baseline knows both the XSAVEOPT and CLWB instructions.

I know that's what I wrote on the ticket, but what I'd forgotten was that we only use XSAVEOPT for it's operand.

Really what we're doing here is knowing CLWB, and also getting rid of the XSAVEOPT workaround for somewhat-more-old toolchains.

Quoted 48 lines

Resolves: https://gitlab.com/xen-project/xen/-/work_items/205

---
 xen/arch/x86/arch.mk              |  2 --
 xen/arch/x86/flushtlb.c           | 28 +---------------------------
 xen/arch/x86/include/asm/system.h |  7 -------
 3 files changed, 1 insertion(+), 36 deletions(-)
xen/arch/x86/arch.mk +0 -2
index 258e459bec..baa83418bc 100644
@@ -15,9 +15,7 @@ $(call as-option-add,CFLAGS,CC,"crc32 %eax$(comma)%eax",-DHAVE_AS_SSE4_2)
 $(call as-option-add,CFLAGS,CC,"invept (%rax)$(comma)%rax",-DHAVE_AS_EPT)
 $(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND)
 $(call as-option-add,CFLAGS,CC,"rdfsbase %rax",-DHAVE_AS_FSGSBASE)
-$(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT)
 $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
-$(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
 $(call as-option-add,CFLAGS,CC,".equ \"x\"$(comma)1",-DHAVE_AS_QUOTED_SYM)
 $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR)
xen/arch/x86/flushtlb.c +0 -18
index 65be0474a8..962bb87d69 100644
@@ -313,33 +313,7 @@ void cache_writeback(const void *addr, unsigned int size)
     clflush_size = current_cpu_data.x86_clflush_size ?: 16;
     addr -= (unsigned long)addr & (clflush_size - 1);
     for ( ; addr < end; addr += clflush_size )
-    {
-/*
- * The arguments to a macro must not include preprocessor directives. Doing so
- * results in undefined behavior, so we have to create some defines here in
- * order to avoid it.
- */
-#if defined(HAVE_AS_CLWB)
-# define CLWB_ENCODING "clwb %[p]"
-#elif defined(HAVE_AS_XSAVEOPT)
-# define CLWB_ENCODING "data16 xsaveopt %[p]" /* clwb */
-#else
-# define CLWB_ENCODING ".byte 0x66, 0x0f, 0xae, 0x30" /* clwb (%%rax) */
-#endif
-
-#define BASE_INPUT(addr) [p] "m" (*(const char *)(addr))
-#if defined(HAVE_AS_CLWB) || defined(HAVE_AS_XSAVEOPT)
-# define INPUT BASE_INPUT
-#else

xen/arch/x86/flushtlb.c void cache_writeback(const void *addr, unsigned int size) · line 316

Quoted 10 lines
xen/arch/x86/flushtlb.c +1 -9
-# define INPUT(addr) "a" (addr), BASE_INPUT(addr)
-#endif
-
-        asm volatile (CLWB_ENCODING :: INPUT(addr));
-
-#undef INPUT
-#undef BASE_INPUT
-#undef CLWB_ENCODING
-    }
+        asm volatile ("clwb %[p]" :: [p] "m" (*(const char *)(addr)));

One minor note about whitespace.  We typically have spaces inside the outermost brackets on asm statements, as per the clwb() example below.

Quoted 6 lines

Also, given the expression is so simple, I'd just use %0 and drop the [p].  It's just line-verbosity here.

Can fix both on commit if you're happy.

Also, I forgot to write in the ticket, clflushopt wants similar treatment, even if there isn't an outward define for it.  I think the following two hunks should do:

~Andrew

xen/arch/x86/flushtlb.c +0 -0
index 18748b2bc805..ef30ef546336 100644
@@ -287,7 +287,7 @@ void cache_flush(const void *addr, unsigned int size)

          * of letting the alternative framework fill the gap by appending nops.
          */
         alternative_io("ds; clflush %[p]",
-                       "data16 clflush %[p]", /* clflushopt */ +                       "clflushopt %[p]", X86_FEATURE_CLFLUSHOPT,
                        /* no outputs */,
                        [p] "m" (*(const char *)(addr)));

(quoted diff) +0 -0

b/xen/arch/x86/include/asm/system.h
index 73cb16ca68d6..6f5b6d502911 100644

xen/arch/x86/include/asm/system.h +0 -0
@@ -23,7 +23,7 @@ static inline void clflush(const void *p)


 static inline void clflushopt(const void *p)
 {
-    asm volatile ( "data16 clflush %0" :: "m" (*(const char *)p) ); +    asm volatile ( "clflushopt %0" :: "m" (*(const char *)p) ); }

 static inline void clwb(const void *p)

#3 lore raw
Quoted 14 lines · Andrew Cooper

On 04/04/2025 12:28 am, Andrew Cooper wrote:
Also, I forgot to write in the ticket, clflushopt wants similar treatment, even if there isn't an outward define for it.  I think the following two hunks should do:

~Andrew

xen/arch/x86/flushtlb.c +0 -0
index 18748b2bc805..ef30ef546336 100644
@@ -287,7 +287,7 @@ void cache_flush(const void *addr, unsigned int 
 size)
           * of letting the alternative framework fill the gap by
Quoted 5 lines

appending nops.
          */
         alternative_io("ds; clflush %[p]",
-                       "data16 clflush %[p]", /* clflushopt */ +                       "clflushopt %[p]",

Agree on these changes. However, I see branch staging uses alternative_input and the below /* no outputs */ does not exist.

Quoted 17 lines

                        X86_FEATURE_CLFLUSHOPT, /* no outputs */,
                        [p] "m" (*(const char *)(addr)));

xen/arch/x86/include/asm/system.h +0 -0
index 73cb16ca68d6..6f5b6d502911 100644
@@ -23,7 +23,7 @@ static inline void clflush(const void *p)


 static inline void clflushopt(const void *p)
 {
-    asm volatile ( "data16 clflush %0" :: "m" (*(const char *)p) ); +    asm volatile ( "clflushopt %0" :: "m" (*(const char *)p) ); }

 static inline void clwb(const void *p)

#4 lore raw
In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper
Quoted 1 line · Alexander M. Merritt

The new toolchain baseline knows both the XSAVEOPT and CLWB instructions.

I know that's what I wrote on the ticket, but what I'd forgotten was that we only use XSAVEOPT for it's operand.

Really what we're doing here is knowing CLWB, and also getting rid of the XSAVEOPT workaround for somewhat-more-old toolchains.

Will try to be more detailed in the commit message next time, thanks for pointing out.

Quoted 5 lines
Quoted 2 lines

+ asm volatile ("clwb %[p]" :: [p] "m" (*(const char
*)(addr)));

One minor note about whitespace.  We typically have spaces inside the outermost brackets on asm statements, as per the clwb() example below.

Makes sense. I had searched for existing examples of this and saw a mix with and without spaces.

Quoted 2 lines

Also, given the expression is so simple, I'd just use %0 and drop the [p].  It's just line-verbosity here.

Yes, agreed.

Quoted 3 lines

Can fix both on commit if you're happy.

Yes please do. Thanks for the review!

#5 lore raw
In reply to Alexander M. Merritt
Quoted 9 lines · Alexander M. Merritt

The new toolchain baseline knows both the XSAVEOPT and CLWB instructions.

Resolves: https://gitlab.com/xen-project/xen/-/work_items/205

---
 xen/arch/x86/arch.mk              |  2 --
 xen/arch/x86/flushtlb.c           | 28 +---------------------------
 xen/arch/x86/include/asm/system.h |  7 -------
 3 files changed, 1 insertion(+), 36 deletions(-)

For XSAVEOPT there's more work to do, even if not connected via HAVE_AS_XSAVEOPT. Look for "xsaveopt" (case-insensitively) in xstate.c. Imo (just like was asked for for the RDRAND counterpart patch) this wants doing all in one go.

Jan

#6 lore raw
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Alexander M. Merritt

The new toolchain baseline knows both the XSAVEOPT and CLWB instructions.

Resolves: https://gitlab.com/xen-project/xen/-/work_items/205

---
 xen/arch/x86/arch.mk              |  2 --
 xen/arch/x86/flushtlb.c           | 28 +---------------------------
 xen/arch/x86/include/asm/system.h |  7 -------
 3 files changed, 1 insertion(+), 36 deletions(-)
Quoted 3 lines

For XSAVEOPT there's more work to do, even if not connected via HAVE_AS_XSAVEOPT. Look for "xsaveopt" (case-insensitively) in xstate.c. Imo (just like was asked for for the RDRAND counterpart patch) this wants doing all in one go.

I've got a different task pending for xsave.  It's a bit more involved than simply dropping the -D's.

We'll get to it.

~Andrew