[PATCH] x86/xstate: Rework xsetbv() to use asm goto()

Andrew Cooper posted 1 patch 5 months, 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20250521143622.311566-1-andrew.cooper3@citrix.com
xen/arch/x86/xstate.c | 29 +++++++++++++++--------------
1 file changed, 15 insertions(+), 14 deletions(-)
[PATCH] x86/xstate: Rework xsetbv() to use asm goto()
Posted by Andrew Cooper 5 months, 1 week ago
Switch to using a mnemonic, and asm_inline as there's EXTABLE metadata.

Update the types, and adjust parameter names.  The name xfeatures is specific
to xcr0, so rename to val to be more generic, even if there isn't another
writeable xcr yet.

No functional change.

Resolves: https://gitlab.com/xen-project/xen/-/work_items/212
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
---
 xen/arch/x86/xstate.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c
index 3d249518a1b7..e8e218caed36 100644
--- a/xen/arch/x86/xstate.c
+++ b/xen/arch/x86/xstate.c
@@ -40,21 +40,22 @@ static DEFINE_PER_CPU(uint64_t, xcr0);
 /* Because XCR0 is cached for each CPU, xsetbv() is not exposed. Users should 
  * use set_xcr0() instead.
  */
-static inline bool xsetbv(u32 index, u64 xfeatures)
+static inline bool xsetbv(uint32_t xcr, uint64_t val)
 {
-    u32 hi = xfeatures >> 32;
-    u32 lo = (u32)xfeatures;
-
-    asm volatile ( "1: .byte 0x0f,0x01,0xd1\n"
-                   "3:                     \n"
-                   ".section .fixup,\"ax\" \n"
-                   "2: xor %0,%0           \n"
-                   "   jmp 3b              \n"
-                   ".previous              \n"
-                   _ASM_EXTABLE(1b, 2b)
-                   : "+a" (lo)
-                   : "c" (index), "d" (hi));
-    return lo != 0;
+    uint32_t hi = val >> 32, lo = val;
+
+    asm_inline goto (
+        "1: xsetbv\n\t"
+        _ASM_EXTABLE(1b, %l[fault])
+        :
+        : "a" (lo), "c" (xcr), "d" (hi)
+        :
+        : fault );
+
+    return true;
+
+ fault:
+    return false;
 }
 
 bool set_xcr0(u64 xfeatures)
-- 
2.39.5


Re: [PATCH] x86/xstate: Rework xsetbv() to use asm goto()
Posted by Jan Beulich 5 months, 1 week ago
On 21.05.2025 16:36, Andrew Cooper wrote:
> Switch to using a mnemonic, and asm_inline as there's EXTABLE metadata.
> 
> Update the types, and adjust parameter names.  The name xfeatures is specific
> to xcr0, so rename to val to be more generic, even if there isn't another
> writeable xcr yet.
> 
> No functional change.
> 
> Resolves: https://gitlab.com/xen-project/xen/-/work_items/212
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>