* [PATCH v5 00/17] x86/msr: Inline rdmsr/wrmsr instructions
@ 2026-09-11 8:41 Juergen Gross
2026-09-11 8:42 ` [PATCH v5 08/17] x86/msr: Make wrmsrns() a first class citizen Juergen Gross
2026-09-11 8:42 ` [PATCH v5 16/17] x86/paravirt: Use alternatives for MSR access with paravirt Juergen Gross
0 siblings, 2 replies; 3+ messages in thread
From: Juergen Gross @ 2026-09-11 8:41 UTC (permalink / raw)
To: linux-kernel, x86, linux-coco, kvm, linux-hyperv, virtualization,
llvm
Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Sean Christopherson,
Boris Ostrovsky, xen-devel, Ajay Kaher, Alexey Makhalov,
Broadcom internal kernel review list, Andy Lutomirski,
Peter Zijlstra, Xin Li, Nathan Chancellor, Nick Desaulniers,
Bill Wendling, Justin Stitt, Josh Poimboeuf
When building a kernel with CONFIG_PARAVIRT_XXL the paravirt
infrastructure will always use functions for reading or writing MSRs,
even when running on bare metal.
Switch to inline RDMSR/WRMSR instructions in this case, reducing the
paravirt overhead.
The first patch is a prerequisite fix for alternative patching. Its
is needed due to the initial indirect call needs to be padded with
NOPs in some cases with the following patches.
In order to make this less intrusive, some further reorganization of
the MSR access helpers is done in the patches 2-5.
The next 5 patches are converting the non-paravirt case to use direct
inlining of the MSR access instructions, including the WRMSRNS
instruction and the immediate variants of RDMSR and WRMSR if possible.
Patches 11-13 are some further preparations for making the real switch
to directly patch in the native MSR instructions easier.
Patch 14 is switching the paravirt MSR function interface from normal
call ABI to one more similar to the native MSR instructions.
Patch 15 is a little cleanup patch.
Patch 16 is the final step for patching in the native MSR instructions
when not running as a Xen PV guest.
Patch 17 converts the rest of the MSR helpers to __always_inline.
This series has been tested to work with Xen PV and on bare metal.
Based on [1] and [2].
Changes since V4:
- Rebase
- dropped patch 3 of V4, as already covered by [1]
Changes since V3:
- Rebase
- wrmsrns() related changes (patches 9+10)
Changes since V2:
- switch back to the paravirt approach
Changes since V1:
- Use Xin Li's approach for inlining
- Several new patches
[1]: https://lore.kernel.org/lkml/20260911074530.3140830-1-jgross@suse.com/T/#t
[2]: https://lore.kernel.org/lkml/20260911075216.3142309-1-jgross@suse.com/T/#t
Juergen Gross (17):
x86/alternative: Support alt_replace_call() with instructions after
call
coco/tdx: Rename MSR access helpers
x86/msr: Minimize usage of native_*() msr access functions
x86/msr: Move MSR trace calls one function level up
x86/hyperv: Switch from __rdmsr() to native_rdmsrq()
x86/opcode: Add immediate form MSR instructions
x86/extable: Add support for immediate form MSR instructions
x86/msr: Make wrmsrns() a first class citizen
x86/msr: Introduce sync_cpu_after_wrmsrns()
x86/msr: Use the alternatives mechanism for RDMSR
x86/alternatives: Add ALTERNATIVE_4()
x86/paravirt: Split off MSR related hooks into new header
x86/paravirt: Prepare support of MSR instruction interfaces
x86/paravirt: Switch MSR access pv_ops functions to instruction
interfaces
x86/msr: Reduce number of low level MSR access helpers
x86/paravirt: Use alternatives for MSR access with paravirt
x86/msr: Make all MSR access functions __always_inline
arch/x86/coco/tdx/tdx.c | 8 +-
arch/x86/hyperv/hv_crash.c | 6 +-
arch/x86/hyperv/ivm.c | 2 +-
arch/x86/include/asm/alternative.h | 6 +
arch/x86/include/asm/fred.h | 2 +-
arch/x86/include/asm/msr.h | 340 +++++++++++++++++-----
arch/x86/include/asm/paravirt-msr.h | 180 ++++++++++++
arch/x86/include/asm/paravirt.h | 45 ---
arch/x86/include/asm/paravirt_types.h | 57 ++--
arch/x86/include/asm/qspinlock_paravirt.h | 4 +-
arch/x86/kernel/alternative.c | 5 +-
arch/x86/kernel/cpu/mshyperv.c | 4 +-
arch/x86/kernel/kvmclock.c | 2 +-
arch/x86/kernel/paravirt.c | 42 ++-
arch/x86/kvm/svm/svm.c | 16 +-
arch/x86/lib/x86-opcode-map.txt | 5 +-
arch/x86/mm/extable.c | 41 ++-
arch/x86/xen/enlighten_pv.c | 52 +++-
arch/x86/xen/pmu.c | 4 +-
tools/arch/x86/lib/x86-opcode-map.txt | 5 +-
tools/objtool/check.c | 1 +
21 files changed, 632 insertions(+), 195 deletions(-)
create mode 100644 arch/x86/include/asm/paravirt-msr.h
--
2.55.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v5 08/17] x86/msr: Make wrmsrns() a first class citizen
2026-09-11 8:41 [PATCH v5 00/17] x86/msr: Inline rdmsr/wrmsr instructions Juergen Gross
@ 2026-09-11 8:42 ` Juergen Gross
2026-09-11 8:42 ` [PATCH v5 16/17] x86/paravirt: Use alternatives for MSR access with paravirt Juergen Gross
1 sibling, 0 replies; 3+ messages in thread
From: Juergen Gross @ 2026-09-11 8:42 UTC (permalink / raw)
To: linux-kernel, x86, virtualization, llvm
Cc: Juergen Gross, Xin Li, H. Peter Anvin, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, Ajay Kaher,
Alexey Makhalov, Broadcom internal kernel review list,
Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt
Today wrmsrns() is - apart from the potential use of the wrmsrns
instruction - equivalent to __wrmsrq(). Change that by supporting
MSR write trace entries and a safe variant.
wrmsrns() and wrmsrns_safe() will be the "normal" interfaces like
wrmsrq() and wrmsrq_safe(). They will call write_msrns[_safe]() and
conditionally create trace entries via do_trace_write_msr().
write_msrns[_safe]() are different between paravirt and non-paravirt
cases. For the paravirt case they will (for now) only use the wrmsr
paravirt functions, while for non-paravirt they call native_wrmsrns()
and native_wrmsrns_safe().
native_wrmsrns() is like wrmsrns() today, native_wrmsrns_safe() is just
the safe variant of it. The both rely on __wrmsrns(), which will use
the ALTERNATIVE*() macros for selecting WRMSR or WRMSRNS (with or
without an immediate operand specifying the MSR register) depending on
availability.
Switch the wrmsrns() call in fred_update_rsp0() to native_wrmsrns() in
order to avoid a change of functionality. The wrmsrns() call in
vmx_write_guest_host_msr() can be kept, as it has replaced a wrmsrq()
call, so eventually creating a trace entry is obviously fine here.
Originally-by: Xin Li (Intel) <xin@zytor.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch, partially taken from "[RFC PATCH v2 21/34] x86/msr: Utilize
the alternatives mechanism to write MSR" by Xin Li.
V4:
- don't modify __wrmsrq(), but create __wrmsrns().
---
arch/x86/include/asm/fred.h | 2 +-
arch/x86/include/asm/msr.h | 150 +++++++++++++++++++++++++++++---
arch/x86/include/asm/paravirt.h | 10 +++
3 files changed, 148 insertions(+), 14 deletions(-)
diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h
index 18a2f811c358..0a6773b76968 100644
--- a/arch/x86/include/asm/fred.h
+++ b/arch/x86/include/asm/fred.h
@@ -101,7 +101,7 @@ static __always_inline void fred_update_rsp0(void)
unsigned long rsp0 = (unsigned long) task_stack_page(current) + THREAD_SIZE;
if (cpu_feature_enabled(X86_FEATURE_FRED) && (__this_cpu_read(fred_rsp0) != rsp0)) {
- wrmsrns(MSR_IA32_FRED_RSP0, rsp0);
+ native_wrmsrns(MSR_IA32_FRED_RSP0, rsp0);
__this_cpu_write(fred_rsp0, rsp0);
}
}
diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h
index 266298b3d201..91d6f481732b 100644
--- a/arch/x86/include/asm/msr.h
+++ b/arch/x86/include/asm/msr.h
@@ -7,11 +7,11 @@
#ifndef __ASSEMBLER__
#include <asm/asm.h>
-#include <asm/errno.h>
#include <asm/cpumask.h>
#include <uapi/asm/msr.h>
#include <asm/shared/msr.h>
+#include <linux/errno.h>
#include <linux/types.h>
#include <linux/percpu.h>
@@ -56,6 +56,36 @@ static inline void do_trace_read_msr(u32 msr, u64 val, int failed) {}
static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {}
#endif
+/* The GNU Assembler (Gas) with Binutils 2.40 adds WRMSRNS support */
+#if defined(CONFIG_AS_IS_GNU) && CONFIG_AS_VERSION >= 24000
+#define ASM_WRMSRNS "wrmsrns\n\t"
+#else
+#define ASM_WRMSRNS _ASM_BYTES(0x0f,0x01,0xc6)
+#endif
+
+/* The GNU Assembler (Gas) with Binutils 2.41 adds the .insn directive support */
+#if defined(CONFIG_AS_IS_GNU) && CONFIG_AS_VERSION >= 24100
+#define ASM_WRMSRNS_IMM \
+ " .insn VEX.128.F3.M7.W0 0xf6 /0, %[val], %[msr]%{:u32}\n\t"
+#else
+/*
+ * Note, clang also doesn't support the .insn directive.
+ *
+ * The register operand is encoded as %rax because all uses of the immediate
+ * form MSR access instructions reference %rax as the register operand.
+ */
+#define ASM_WRMSRNS_IMM \
+ " .byte 0xc4,0xe7,0x7a,0xf6,0xc0; .long %c[msr]"
+#endif
+
+#define PREPARE_RDX_FOR_WRMSR \
+ "mov %%rax, %%rdx\n\t" \
+ "shr $0x20, %%rdx\n\t"
+
+#define PREPARE_RCX_RDX_FOR_WRMSR \
+ "mov %[msr], %%ecx\n\t" \
+ PREPARE_RDX_FOR_WRMSR
+
/*
* __rdmsr() and __wrmsr() are the two primitives which are the bare minimum MSR
* accessors and should not have any tracing or other functionality piggybacking
@@ -83,6 +113,78 @@ static __always_inline void __wrmsrq(u32 msr, u64 val)
: : "c" (msr), "a" ((u32)val), "d" ((u32)(val >> 32)) : "memory");
}
+static __always_inline bool __wrmsrns_variable(u32 msr, u64 val, int type)
+{
+#ifdef CONFIG_X86_64
+ BUILD_BUG_ON(__builtin_constant_p(msr));
+#endif
+
+ /*
+ * WRMSR is 2 bytes. WRMSRNS is 3 bytes. Pad WRMSR with a redundant
+ * DS prefix to avoid a trailing NOP.
+ */
+ asm_inline volatile goto(
+ "1:\n"
+ ALTERNATIVE("ds wrmsr",
+ ASM_WRMSRNS,
+ X86_FEATURE_WRMSRNS)
+ _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type])
+
+ :
+ : "c" (msr), "a" ((u32)val), "d" ((u32)(val >> 32)), [type] "i" (type)
+ : "memory"
+ : badmsr);
+
+ return false;
+
+badmsr:
+ return true;
+}
+
+#ifdef CONFIG_X86_64
+/*
+ * Non-serializing WRMSR or its immediate form, when available.
+ *
+ * Otherwise, it falls back to a serializing WRMSR.
+ */
+static __always_inline bool __wrmsrns_constant(u32 msr, u64 val, int type)
+{
+ BUILD_BUG_ON(!__builtin_constant_p(msr));
+
+ asm_inline volatile goto(
+ "1:\n"
+ ALTERNATIVE_2(PREPARE_RCX_RDX_FOR_WRMSR
+ "2: ds wrmsr",
+ PREPARE_RCX_RDX_FOR_WRMSR
+ ASM_WRMSRNS,
+ X86_FEATURE_WRMSRNS,
+ ASM_WRMSRNS_IMM,
+ X86_FEATURE_MSR_IMM)
+ _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For WRMSRNS immediate */
+ _ASM_EXTABLE_TYPE(2b, %l[badmsr], %c[type]) /* For WRMSR(NS) */
+
+ :
+ : [val] "a" (val), [msr] "i" (msr), [type] "i" (type)
+ : "memory", "ecx", "rdx"
+ : badmsr);
+
+ return false;
+
+badmsr:
+ return true;
+}
+#endif
+
+static __always_inline bool __wrmsrns(u32 msr, u64 val, int type)
+{
+#ifdef CONFIG_X86_64
+ if (__builtin_constant_p(msr))
+ return __wrmsrns_constant(msr, val, type);
+#endif
+
+ return __wrmsrns_variable(msr, val, type);
+}
+
static __always_inline u64 native_rdmsrq(u32 msr)
{
return __rdmsr(msr);
@@ -134,6 +236,16 @@ static inline int notrace native_write_msr_safe(u32 msr, u64 val)
return err;
}
+static __always_inline void native_wrmsrns(u32 msr, u64 val)
+{
+ __wrmsrns(msr, val, EX_TYPE_WRMSR);
+}
+
+static __always_inline int native_wrmsrns_safe(u32 msr, u64 val)
+{
+ return __wrmsrns(msr, val, EX_TYPE_WRMSR_SAFE) ? -EIO : 0;
+}
+
extern int rdmsr_safe_regs(u32 regs[8]);
extern int wrmsr_safe_regs(u32 regs[8]);
@@ -150,7 +262,6 @@ static inline u64 native_read_pmc(int counter)
#ifdef CONFIG_PARAVIRT_XXL
#include <asm/paravirt.h>
#else
-#include <linux/errno.h>
static __always_inline u64 read_msr(u32 msr)
{
return native_read_msr(msr);
@@ -171,6 +282,16 @@ static __always_inline int write_msr_safe(u32 msr, u64 val)
return native_write_msr_safe(msr, val);
}
+static __always_inline void write_msrns(u32 msr, u64 val)
+{
+ native_wrmsrns(msr, val);
+}
+
+static __always_inline int write_msrns_safe(u32 msr, u64 val)
+{
+ return native_wrmsrns_safe(msr, val);
+}
+
static __always_inline u64 rdpmc(int counter)
{
return native_read_pmc(counter);
@@ -223,19 +344,22 @@ static inline int wrmsrq_safe(u32 msr, u64 val)
return err;
}
-/* Instruction opcode for WRMSRNS supported in binutils >= 2.40 */
-#define ASM_WRMSRNS _ASM_BYTES(0x0f,0x01,0xc6)
-
-/* Non-serializing WRMSR, when available. Falls back to a serializing WRMSR. */
static __always_inline void wrmsrns(u32 msr, u64 val)
{
- /*
- * WRMSR is 2 bytes. WRMSRNS is 3 bytes. Pad WRMSR with a redundant
- * DS prefix to avoid a trailing NOP.
- */
- asm volatile("1: " ALTERNATIVE("ds wrmsr", ASM_WRMSRNS, X86_FEATURE_WRMSRNS)
- "2: " _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_WRMSR)
- : : "c" (msr), "a" ((u32)val), "d" ((u32)(val >> 32)));
+ write_msrns(msr, val);
+
+ if (tracepoint_enabled(write_msr))
+ do_trace_write_msr(msr, val, 0);
+}
+
+static __always_inline int wrmsrns_safe(u32 msr, u64 val)
+{
+ int err = write_msrns_safe(msr, val);
+
+ if (tracepoint_enabled(write_msr))
+ do_trace_write_msr(msr, val, err);
+
+ return err;
}
struct msr __percpu *msrs_alloc(void);
diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h
index a5a1fc4c88d1..b0c740316cf7 100644
--- a/arch/x86/include/asm/paravirt.h
+++ b/arch/x86/include/asm/paravirt.h
@@ -160,11 +160,21 @@ static inline void write_msr(u32 msr, u64 val)
paravirt_write_msr(msr, val);
}
+static __always_inline void write_msrns(u32 msr, u64 val)
+{
+ paravirt_write_msr(msr, val);
+}
+
static inline int write_msr_safe(u32 msr, u64 val)
{
return paravirt_write_msr_safe(msr, val);
}
+static __always_inline int write_msrns_safe(u32 msr, u64 val)
+{
+ return paravirt_write_msr_safe(msr, val);
+}
+
static __always_inline int read_msr_safe(u32 msr, u64 *p)
{
return paravirt_read_msr_safe(msr, p);
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v5 16/17] x86/paravirt: Use alternatives for MSR access with paravirt
2026-09-11 8:41 [PATCH v5 00/17] x86/msr: Inline rdmsr/wrmsr instructions Juergen Gross
2026-09-11 8:42 ` [PATCH v5 08/17] x86/msr: Make wrmsrns() a first class citizen Juergen Gross
@ 2026-09-11 8:42 ` Juergen Gross
1 sibling, 0 replies; 3+ messages in thread
From: Juergen Gross @ 2026-09-11 8:42 UTC (permalink / raw)
To: linux-kernel, x86, virtualization, llvm
Cc: Juergen Gross, Ajay Kaher, Alexey Makhalov,
Broadcom internal kernel review list, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin,
Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt
When not running as Xen PV guest, patch in the optimal MSR instructions
via alternative and use direct calls otherwise.
This will especially have positive effects for performance when not
running as a Xen PV guest with paravirtualization enabled, as there
will be no call overhead for MSR access functions any longer.
Signed-off-by: Juergen Gross <jgross@suse.com>
---
V3:
- new patch
V4:
- fix build error with clang (kernel test robot)
---
arch/x86/include/asm/paravirt-msr.h | 136 ++++++++++++++++++++------
arch/x86/include/asm/paravirt_types.h | 1 +
2 files changed, 109 insertions(+), 28 deletions(-)
diff --git a/arch/x86/include/asm/paravirt-msr.h b/arch/x86/include/asm/paravirt-msr.h
index 4b71a1cd780c..ba3ee64446db 100644
--- a/arch/x86/include/asm/paravirt-msr.h
+++ b/arch/x86/include/asm/paravirt-msr.h
@@ -27,67 +27,147 @@ extern struct pv_msr_ops pv_ops_msr;
#define PV_CALLEE_SAVE_REGS_MSR_THUNK(func) \
__PV_CALLEE_SAVE_REGS_THUNK(func, ".text", MSR)
+#define ASM_CLRERR "xor %[err],%[err]\n"
+
+#define PV_RDMSR_VAR(__msr, __val, __type, __func, __err) \
+ asm volatile( \
+ "1:\n" \
+ ALTERNATIVE_2(PARAVIRT_CALL, \
+ RDMSR_AND_SAVE_RESULT ASM_CLRERR, X86_FEATURE_ALWAYS, \
+ ALT_CALL_INSTR, ALT_XEN_CALL) \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, __type, %[err]) \
+ : [err] "=d" (__err), [val] "=a" (__val), \
+ ASM_CALL_CONSTRAINT \
+ : paravirt_ptr(pv_ops_msr, __func), "c" (__msr) \
+ : "cc")
+
+#define PV_RDMSR_CONST(__msr, __val, __type, __func, __err) \
+ asm volatile( \
+ "1:\n" \
+ ALTERNATIVE_3(PARAVIRT_CALL, \
+ RDMSR_AND_SAVE_RESULT ASM_CLRERR, X86_FEATURE_ALWAYS, \
+ ASM_RDMSR_IMM ASM_CLRERR, X86_FEATURE_MSR_IMM, \
+ ALT_CALL_INSTR, ALT_XEN_CALL) \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, __type, %[err]) \
+ : [err] "=d" (__err), [val] "=a" (__val), \
+ ASM_CALL_CONSTRAINT \
+ : paravirt_ptr(pv_ops_msr, __func), \
+ "c" (__msr), [msr] "i" (__msr) \
+ : "cc")
+
+#define PV_WRMSR(__msr, __val, __type, __func, __err) \
+({ \
+ unsigned long rdx = rdx; \
+ asm volatile( \
+ "1:\n" \
+ ALTERNATIVE_2(PARAVIRT_CALL, \
+ "wrmsr;" ASM_CLRERR, X86_FEATURE_ALWAYS, \
+ ALT_CALL_INSTR, ALT_XEN_CALL) \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, __type, %[err]) \
+ : [err] "=a" (__err), "=d" (rdx), ASM_CALL_CONSTRAINT \
+ : paravirt_ptr(pv_ops_msr, __func), \
+ "0" (__val), "1" ((__val) >> 32), "c" (__msr) \
+ : "memory", "cc"); \
+})
+
+#define PV_WRMSRNS_VAR(__msr, __val, __type, __func, __err) \
+({ \
+ unsigned long rdx = rdx; \
+ asm volatile( \
+ "1:\n" \
+ ALTERNATIVE_3(PARAVIRT_CALL, \
+ "wrmsr;" ASM_CLRERR, X86_FEATURE_ALWAYS, \
+ ASM_WRMSRNS ASM_CLRERR, X86_FEATURE_WRMSRNS, \
+ ALT_CALL_INSTR, ALT_XEN_CALL) \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, __type, %[err]) \
+ : [err] "=a" (__err), "=d" (rdx), ASM_CALL_CONSTRAINT \
+ : paravirt_ptr(pv_ops_msr, __func), \
+ "0" (__val), "1" ((__val) >> 32), "c" (__msr) \
+ : "memory", "cc"); \
+})
+
+#define PV_WRMSRNS_CONST(__msr, __val, __type, __func, __err) \
+({ \
+ unsigned long rdx = rdx; \
+ asm volatile( \
+ "1:\n" \
+ ALTERNATIVE_4(PARAVIRT_CALL, \
+ "wrmsr;" ASM_CLRERR, X86_FEATURE_ALWAYS, \
+ ASM_WRMSRNS ASM_CLRERR, X86_FEATURE_WRMSRNS, \
+ ASM_WRMSRNS_IMM ASM_CLRERR, X86_FEATURE_MSR_IMM,\
+ ALT_CALL_INSTR, ALT_XEN_CALL) \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, __type, %[err]) \
+ : [err] "=a" (__err), "=d" (rdx), ASM_CALL_CONSTRAINT \
+ : paravirt_ptr(pv_ops_msr, __func), \
+ [val] "0" (__val), "1" ((__val) >> 32), \
+ "c" (__msr), [msr] "i" (__msr) \
+ : "memory", "cc"); \
+})
+
static __always_inline u64 read_msr(u32 msr)
{
u64 val;
+ u64 err;
- asm volatile(PARAVIRT_CALL
- : "=a" (val), ASM_CALL_CONSTRAINT
- : paravirt_ptr(pv_ops_msr, read_msr), "c" (msr)
- : "rdx");
+ if (__builtin_constant_p(msr))
+ PV_RDMSR_CONST(msr, val, EX_TYPE_RDMSR, read_msr, err);
+ else
+ PV_RDMSR_VAR(msr, val, EX_TYPE_RDMSR, read_msr, err);
return val;
}
static __always_inline void write_msr(u32 msr, u64 val)
{
- asm volatile(PARAVIRT_CALL
- : ASM_CALL_CONSTRAINT
- : paravirt_ptr(pv_ops_msr, write_msr), "c" (msr), "a" (val)
- : "memory", "rdx");
+ u64 err;
+
+ PV_WRMSR(msr, val, EX_TYPE_WRMSR, write_msr, err);
}
static __always_inline void write_msrns(u32 msr, u64 val)
{
- asm volatile(PARAVIRT_CALL
- : ASM_CALL_CONSTRAINT
- : paravirt_ptr(pv_ops_msr, write_msr), "c" (msr), "a" (val)
- : "memory", "rdx");
+ u64 err;
+
+ if (__builtin_constant_p(msr))
+ PV_WRMSRNS_CONST(msr, val, EX_TYPE_WRMSR, write_msr, err);
+ else
+ PV_WRMSRNS_VAR(msr, val, EX_TYPE_WRMSR, write_msr, err);
}
static __always_inline int read_msr_safe(u32 msr, u64 *val)
{
- int err;
+ u64 err;
- asm volatile(PARAVIRT_CALL
- : [err] "=d" (err), "=a" (*val), ASM_CALL_CONSTRAINT
- : paravirt_ptr(pv_ops_msr, read_msr_safe), "c" (msr));
+ if (__builtin_constant_p(msr))
+ PV_RDMSR_CONST(msr, *val, EX_TYPE_RDMSR_SAFE, read_msr_safe, err);
+ else
+ PV_RDMSR_VAR(msr, *val, EX_TYPE_RDMSR_SAFE, read_msr_safe, err);
return err ? -EIO : 0;
}
static __always_inline int write_msr_safe(u32 msr, u64 val)
{
- int err;
+ u64 err;
- asm volatile(PARAVIRT_CALL
- : [err] "=a" (err), ASM_CALL_CONSTRAINT
- : paravirt_ptr(pv_ops_msr, write_msr_safe),
- "c" (msr), "a" (val)
- : "memory", "rdx");
+ PV_WRMSR(msr, val, EX_TYPE_WRMSR_SAFE, write_msr_safe, err);
return err ? -EIO : 0;
}
static __always_inline int write_msrns_safe(u32 msr, u64 val)
{
- int err;
+ u64 err;
- asm volatile(PARAVIRT_CALL
- : [err] "=a" (err), ASM_CALL_CONSTRAINT
- : paravirt_ptr(pv_ops_msr, write_msr_safe),
- "c" (msr), "a" (val)
- : "memory", "rdx");
+ if (__builtin_constant_p(msr))
+ PV_WRMSRNS_CONST(msr, val, EX_TYPE_WRMSR_SAFE, write_msr_safe, err);
+ else
+ PV_WRMSRNS_VAR(msr, val, EX_TYPE_WRMSR_SAFE, write_msr_safe, err);
return err ? -EIO : 0;
}
diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h
index 740ea819bbab..54f7c3d8fadf 100644
--- a/arch/x86/include/asm/paravirt_types.h
+++ b/arch/x86/include/asm/paravirt_types.h
@@ -442,6 +442,7 @@ extern struct paravirt_patch_template pv_ops;
#endif /* __ASSEMBLER__ */
#define ALT_NOT_XEN ALT_NOT(X86_FEATURE_XENPV)
+#define ALT_XEN_CALL ALT_DIRECT_CALL(X86_FEATURE_XENPV)
#ifdef CONFIG_X86_32
/* save and restore all caller-save registers, except return value */
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-11 8:43 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-11 8:41 [PATCH v5 00/17] x86/msr: Inline rdmsr/wrmsr instructions Juergen Gross
2026-09-11 8:42 ` [PATCH v5 08/17] x86/msr: Make wrmsrns() a first class citizen Juergen Gross
2026-09-11 8:42 ` [PATCH v5 16/17] x86/paravirt: Use alternatives for MSR access with paravirt Juergen Gross
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox