x86/msr: Inline rdmsr/wrmsr instructions
Subsystems (13)
Juergen Gross
maintainerv6 cover x86/msr: Inline rdmsr/wrmsr instructions
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 V5:
- Rebase
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/[email protected]/T/#md59eab1d0c8b23a3184cbab408629c86f95e8419
[2]: https://lore.kernel.org/lkml/[email protected]/T/#mf22bd746d396918f2d8163541405b2222770600a
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 | 356 +++++++++++++++++----- 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, 645 insertions(+), 198 deletions(-) create mode 100644 arch/x86/include/asm/paravirt-msr.h
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 1/17 x86/alternative: Support alt_replace_call() with instructions after call
Today alt_replace_call() requires the initial indirect call not to be followed by any further instructions, including padding NOPs. In case any replacement is longer than 6 bytes, a subsequent replacement of the indirect call with a direct one will result in a crash.
Fix that by crashing only if the original instruction is less than 6 bytes long or not a known indirect call.
--- arch/x86/kernel/alternative.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/alternative.c +3 -2
@@ -528,6 +528,7 @@ noinstr void BUG_func(void) }EXPORT_SYMBOL (BUG_func); +#define CALL_RIP_INSTRLEN 6#define CALL_RIP_REL_OPCODE 0xff#define CALL_RIP_REL_MODRM 0x15
@@ -545,7 +546,7 @@ static unsigned int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instrBUG (); } -if (a->instrlen !=6 || +if (a->instrlen <CALL_RIP_INSTRLEN || instr[0 ] !=CALL_RIP_REL_OPCODE || instr[1 ] !=CALL_RIP_REL_MODRM ) {pr_err ("ALT_FLAG_DIRECT_CALL set for unrecognized indirect call\n" );
@@ -557,7 +558,7 @@ static unsigned int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr#ifdef CONFIG_X86_64 /* ff 15 00 00 00 00 call *0x0(%rip) */ /* target address is stored at "next instruction + disp". */ - target = *(void **)(instr + a->instrlen + disp); + target = *(void **)(instr +CALL_RIP_INSTRLEN + disp);#else /* ff 15 00 00 00 00 call *0x0 */ /* target address is stored at disp. */
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 2/17 coco/tdx: Rename MSR access helpers
In reply to Juergen GrossIn order to avoid a name clash with some general MSR access helpers after a future MSR infrastructure rework, rename the TDX specific helpers.
--- arch/x86/coco/tdx/tdx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/coco/tdx/tdx.c +4 -4
@@ -451,7 +451,7 @@ static void __cpuidle tdx_safe_halt(void)raw_local_irq_enable (); } -static int read_msr (struct pt_regs *regs,struct ve_info *ve) +static int tdx_read_msr (struct pt_regs *regs,struct ve_info *ve) {struct tdx_module_args args = { .r10 =TDX_HYPERCALL_STANDARD ,
@@ -472,7 +472,7 @@ static int read_msr(struct pt_regs *regs, struct ve_info *ve)return ve_instr_len (ve); } -static int write_msr (struct pt_regs *regs,struct ve_info *ve) +static int tdx_write_msr (struct pt_regs *regs,struct ve_info *ve) {struct tdx_module_args args = { .r10 =TDX_HYPERCALL_STANDARD ,
@@ -823,9 +823,9 @@ static int virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve)case EXIT_REASON_HLT :return handle_halt (ve);case EXIT_REASON_MSR_READ : -return read_msr (regs, ve); +return tdx_read_msr (regs, ve);case EXIT_REASON_MSR_WRITE : -return write_msr (regs, ve); +return tdx_write_msr (regs, ve);case EXIT_REASON_CPUID :return handle_cpuid (regs, ve);case EXIT_REASON_EPT_VIOLATION :
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 3/17 x86/msr: Minimize usage of native_*() msr access functions
In reply to Juergen GrossIn order to prepare for some MSR access function reorg work, switch most users of native_{read|write}_msr[_safe]() to the more generic rdmsr*()/wrmsr*() variants.
For now this will have some intermediate performance impact with paravirtualization configured when running on bare metal, but this is a prereq change for the planned direct inlining of the rdmsr/wrmsr instructions with this configuration.
The main reason for this switch is the planned move of the MSR trace function invocation from the native_*() functions to the generic rdmsr*()/wrmsr*() variants. Without this switch the users of the native_*() functions would lose the related tracing entries.
Note that the Xen related MSR access functions will not be switched, as these will be handled after the move of the trace hooks.
--- arch/x86/hyperv/ivm.c | 2 +- arch/x86/kernel/cpu/mshyperv.c | 4 ++-- arch/x86/kernel/kvmclock.c | 2 +- arch/x86/kvm/svm/svm.c | 16 ++++++++-------- arch/x86/xen/pmu.c | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/hyperv/ivm.c +1 -1
@@ -328,7 +328,7 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)savesegment (ds, vmsa->ds.selector);hv_populate_vmcb_seg (vmsa->ds, vmsa->gdtr.base); - vmsa->efer =native_read_msr (MSR_EFER ); + vmsa->efer =rdmsrq (MSR_EFER ); vmsa->cr4 =native_read_cr4 (); vmsa->cr3 =__native_read_cr3 ();
arch/x86/kernel/cpu/mshyperv.c +2 -2
@@ -114,7 +114,7 @@ u64 hv_para_get_synic_register(unsigned int reg) {if (WARN_ON (!ms_hyperv.paravisor_present || !hv_is_synic_msr (reg)))return ~0ULL ; -return native_read_msr (reg); +return rdmsrq (reg); }/*
@@ -124,7 +124,7 @@ void hv_para_set_synic_register(unsigned int reg, u64 val) {if (WARN_ON (!ms_hyperv.paravisor_present || !hv_is_synic_msr (reg)))return ; -native_write_msr (reg, val); +wrmsrq (reg, val); }u64 hv_get_msr (unsigned int reg)
arch/x86/kernel/kvmclock.c +1 -1
@@ -219,7 +219,7 @@ static void kvm_setup_secondary_clock(void)void kvmclock_disable (void ) {if (msr_kvm_system_time) -native_write_msr (msr_kvm_system_time,0 ); +wrmsrq (msr_kvm_system_time,0 ); }static void __init kvmclock_init_mem (void )
arch/x86/kvm/svm/svm.c +8 -8
@@ -415,12 +415,12 @@ static void svm_init_erratum_383(void)return ;/* Use _safe variants to not break nested virtualization */ -if (native_read_msr_safe (MSR_AMD64_DC_CFG , &val)) +if (rdmsrq_safe (MSR_AMD64_DC_CFG , &val))return ; val |= (1ULL <<47 ); -native_write_msr_safe (MSR_AMD64_DC_CFG , val); +wrmsrq_safe (MSR_AMD64_DC_CFG , val); erratum_383_found = true; }
@@ -473,8 +473,8 @@ static void svm_init_os_visible_workarounds(void)return ;if (!this_cpu_has (X86_FEATURE_OSVW ) || -native_read_msr_safe (MSR_AMD64_OSVW_ID_LENGTH , &len) || -native_read_msr_safe (MSR_AMD64_OSVW_STATUS , &status)) +rdmsrq_safe (MSR_AMD64_OSVW_ID_LENGTH , &len) || +rdmsrq_safe (MSR_AMD64_OSVW_STATUS , &status)) len = status =0 ;if (status ==READ_ONCE (osvw_status) && len >=READ_ONCE (osvw_len))
@@ -2156,7 +2156,7 @@ static bool is_erratum_383(void)if (!erratum_383_found)return false; -if (native_read_msr_safe (MSR_IA32_MC0_STATUS , &value)) +if (rdmsrq_safe (MSR_IA32_MC0_STATUS , &value))return false;/* Bit 62 may or may not be set for this mce */
@@ -2167,11 +2167,11 @@ static bool is_erratum_383(void)/* Clear MCi_STATUS registers */ for (i =0 ; i <6 ; ++i) -native_write_msr_safe (MSR_IA32_MCx_STATUS (i),0 ); +wrmsrq_safe (MSR_IA32_MCx_STATUS (i),0 ); -if (!native_read_msr_safe (MSR_IA32_MCG_STATUS , &value)) { +if (!rdmsrq_safe (MSR_IA32_MCG_STATUS , &value)) { value &= ~(1ULL <<2 ); -native_write_msr_safe (MSR_IA32_MCG_STATUS , value); +wrmsrq_safe (MSR_IA32_MCG_STATUS , value); }/* Flush tlb to evict multi-match entries */
arch/x86/xen/pmu.c +2 -2
@@ -324,7 +324,7 @@ static u64 xen_amd_read_pmc(int counter)u64 val; msr = amd_counters_base + (counter * amd_msr_step); -native_read_msr_safe (msr, &val); +rdmsrq_safe (msr, &val);return val; }
@@ -350,7 +350,7 @@ static u64 xen_intel_read_pmc(int counter)else msr =MSR_IA32_PERFCTR0 + counter; -native_read_msr_safe (msr, &val); +rdmsrq_safe (msr, &val);return val; }
Signature · 1 line
2.56.0
RE: [PATCH v6 03/17] x86/msr: Minimize usage of native_*() msr access functions
From: Juergen Gross <[email protected]> Sent: Monday, November 16, 2026 10:39 AM
Quoted 18 lines
In order to prepare for some MSR access function reorg work, switch most users of native_{read|write}_msr[_safe]() to the more generic rdmsr*()/wrmsr*() variants.
For now this will have some intermediate performance impact with paravirtualization configured when running on bare metal, but this is a prereq change for the planned direct inlining of the rdmsr/wrmsr instructions with this configuration.
The main reason for this switch is the planned move of the MSR trace function invocation from the native_*() functions to the generic rdmsr*()/wrmsr*() variants. Without this switch the users of the native_*() functions would lose the related tracing entries.
Note that the Xen related MSR access functions will not be switched, as these will be handled after the move of the trace hooks.
Quoted 7 lines
Signed-off-by Juergen Gross <[email protected]>Acked-by Wei Liu <[email protected]>Reviewed-by H. Peter Anvin (Intel) <[email protected]>--- arch/x86/hyperv/ivm.c | 2 +- arch/x86/kernel/cpu/mshyperv.c | 4 ++--
For these two Hyper-V related files,
Quoted 133 lines
arch/x86/kernel/kvmclock.c | 2 +- arch/x86/kvm/svm/svm.c | 16 ++++++++-------- arch/x86/xen/pmu.c | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-)arch/x86/hyperv/ivm.c +1
-1@@ -328,7 +328,7 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu) savesegment(ds, vmsa->ds.selector);hv_populate_vmcb_seg (vmsa->ds, vmsa->gdtr.base); - vmsa->efer =native_read_msr (MSR_EFER ); + vmsa->efer =rdmsrq (MSR_EFER ); vmsa->cr4 =native_read_cr4 ();arch/x86/hyperv/ivm.c +0
-0vmsa->cr3 =__native_read_cr3 ();arch/x86/kernel/cpu/mshyperv.c +2
-2@@ -114,7 +114,7 @@ u64 hv_para_get_synic_register(unsigned int reg) {if (WARN_ON (!ms_hyperv.paravisor_present || !hv_is_synic_msr (reg)))return ~0ULL ; -return native_read_msr (reg); +return rdmsrq (reg); }/* @@ -124,7 +124,7 @@ void hv_para_set_synic_register(unsigned int reg, u64 val) {if (WARN_ON (!ms_hyperv.paravisor_present || !hv_is_synic_msr (reg)))return ; -native_write_msr (reg, val); +wrmsrq (reg, val); }u64 hv_get_msr (unsigned int reg)arch/x86/kernel/kvmclock.c +1
-1@@ -219,7 +219,7 @@ static void kvm_setup_secondary_clock(void)void kvmclock_disable (void ) {if (msr_kvm_system_time) -native_write_msr (msr_kvm_system_time,0 ); +wrmsrq (msr_kvm_system_time,0 ); }static void __init kvmclock_init_mem (void )arch/x86/kvm/svm/svm.c +8
-8@@ -415,12 +415,12 @@ static void svm_init_erratum_383(void)return ;/* Use _safe variants to not break nested virtualization */ -if (native_read_msr_safe (MSR_AMD64_DC_CFG , &val)) +if (rdmsrq_safe (MSR_AMD64_DC_CFG , &val))return ; val |= (1ULL <<47 ); -native_write_msr_safe (MSR_AMD64_DC_CFG , val); +wrmsrq_safe (MSR_AMD64_DC_CFG , val); erratum_383_found = true; }@@ -473,8 +473,8 @@ static void svm_init_os_visible_workarounds(void)return ;if (!this_cpu_has (X86_FEATURE_OSVW ) || -native_read_msr_safe (MSR_AMD64_OSVW_ID_LENGTH , &len) || -native_read_msr_safe (MSR_AMD64_OSVW_STATUS , &status)) +rdmsrq_safe (MSR_AMD64_OSVW_ID_LENGTH , &len) || +rdmsrq_safe (MSR_AMD64_OSVW_STATUS , &status)) len = status =0 ;if (status ==READ_ONCE (osvw_status) && len >=READ_ONCE (osvw_len))@@ -2156,7 +2156,7 @@ static bool is_erratum_383(void)if (!erratum_383_found)return false; -if (native_read_msr_safe (MSR_IA32_MC0_STATUS , &value)) +if (rdmsrq_safe (MSR_IA32_MC0_STATUS , &value))return false;/* Bit 62 may or may not be set for this mce */ @@ -2167,11 +2167,11 @@ static bool is_erratum_383(void)/* Clear MCi_STATUS registers */ for (i =0 ; i <6 ; ++i) -native_write_msr_safe (MSR_IA32_MCx_STATUS (i),0 ); +wrmsrq_safe (MSR_IA32_MCx_STATUS (i),0 ); -if (!native_read_msr_safe (MSR_IA32_MCG_STATUS , &value)) { +if (!rdmsrq_safe (MSR_IA32_MCG_STATUS , &value)) { value &= ~(1ULL <<2 ); -native_write_msr_safe (MSR_IA32_MCG_STATUS , value); +wrmsrq_safe (MSR_IA32_MCG_STATUS , value); }/* Flush tlb to evict multi-match entries */ arch/x86/xen/pmu.c +2
-3@@ -324,7 +324,7 @@ static u64 xen_amd_read_pmc(int counter)u64 val; msr = amd_counters_base + (counter * amd_msr_step); -native_read_msr_safe (msr, &val); +rdmsrq_safe (msr, &val);return val; }@@ -350,7 +350,7 @@ static u64 xen_intel_read_pmc(int counter)else msr =MSR_IA32_PERFCTR0 + counter; -native_read_msr_safe (msr, &val); +rdmsrq_safe (msr, &val);return val; } --2.56.0
Juergen Gross
maintainerPATCH v6 4/17 x86/msr: Move MSR trace calls one function level up
In reply to Juergen GrossIn order to prepare paravirt inlining of the MSR access instructions move the calls of MSR trace functions one function level up.
Introduce {read|write}_msr[_safe]() helpers allowing to have common definitions in msr.h doing the trace calls.
Do the same for rdpmc(), which up to now didn't support tracing under Xen PV.
---
V4:
- some modifications removed due to rebase
V6:
- cover rdpmc() as well (Shreshth Srivastava)
--- arch/x86/include/asm/msr.h | 91 +++++++++++++++++++++++---------- arch/x86/include/asm/paravirt.h | 10 ++-- 2 files changed, 69 insertions(+), 32 deletions(-)
arch/x86/include/asm/msr.h +64 -27
@@ -95,14 +95,7 @@ static __always_inline void native_wrmsrq(u32 msr, u64 val)static inline u64 native_read_msr (u32 msr) { -u64 val; - - val =__rdmsr (msr); - -if (tracepoint_enabled (read_msr)) -do_trace_read_msr (msr, val,0 ); - -return val; +return __rdmsr (msr); }static inline int native_read_msr_safe (u32 msr,u64 *p)
@@ -115,8 +108,6 @@ static inline int native_read_msr_safe(u32 msr, u64 *p) _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_RDMSR_SAFE, %[err]) : [err] "=r" (err), EAX_EDX_RET(val, low, high) : "c" (msr)); - if (tracepoint_enabled(read_msr)) -do_trace_read_msr (msr,EAX_EDX_VAL (val, low, high), err); *p =EAX_EDX_VAL (val, low, high);
@@ -127,9 +118,6 @@ static inline int native_read_msr_safe(u32 msr, u64 *p) static inline void notrace native_write_msr(u32 msr, u64 val) {native_wrmsrq (msr, val); - -if (tracepoint_enabled (write_msr)) -do_trace_write_msr (msr, val,0 ); }/* Can be uninlined because referenced by paravirt */
@@ -143,8 +131,6 @@ static inline int notrace native_write_msr_safe(u32 msr, u64 val) : [err] "=a" (err) : "c" (msr), "0" ((u32)val), "d" ((u32)(val >> 32)) : "memory"); -if (tracepoint_enabled (write_msr)) -do_trace_write_msr (msr, val, err); return err; }
@@ -156,8 +142,7 @@ static inline u64 native_read_pmc(int counter)EAX_EDX_DECLARE_ARGS (val, low, high); asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter)); -if (tracepoint_enabled (rdpmc)) -do_trace_rdpmc (counter,EAX_EDX_VAL (val, low, high),0 ); +return EAX_EDX_VAL (val, low, high); }
@@ -165,36 +150,88 @@ static inline u64 native_read_pmc(int counter)#include <asm/paravirt.h> #else #include <linux/errno.h> +static __always_inline u64 read_msr(u32 msr) +{ +return native_read_msr (msr); +} + +static __always_inline int read_msr_safe(u32 msr, u64 *p) +{ +return native_read_msr_safe (msr, p); +} + +static __always_inline void write_msr(u32 msr, u64 val) +{ +native_write_msr (msr, val); +} + +static __always_inline int write_msr_safe(u32 msr, u64 val) +{ +return native_write_msr_safe (msr, val); +} + +static __always_inline u64 do_rdpmc(int counter) +{ +return native_read_pmc (counter); +} +#endif /* !CONFIG_PARAVIRT_XXL */ /* Access to machine-specific registers (available on 586 and better only) */ static __always_inline u64 rdmsrq(u32 msr) { -return native_read_msr (msr); +u64 val =read_msr (msr); + +if (tracepoint_enabled (read_msr)) +do_trace_read_msr (msr, val,0 ); + +return val; +} + +/* rdmsr with exception handling */ +static inline int rdmsrq_safe (u32 msr,u64 *p) +{ +int err; + + err =read_msr_safe (msr, p); + +if (tracepoint_enabled (read_msr)) +do_trace_read_msr (msr, *p, err); + +return err; }static inline void wrmsrq (u32 msr,u64 val) { -native_write_msr (msr, val); +write_msr (msr, val); + +if (tracepoint_enabled (write_msr)) +do_trace_write_msr (msr, val,0 ); }/* wrmsr with exception handling */ static inline int wrmsrq_safe (u32 msr,u64 val) { -return native_write_msr_safe (msr, val); -} +int err; -static inline int rdmsrq_safe (u32 msr,u64 *p) -{ -return native_read_msr_safe (msr, p); + err =write_msr_safe (msr, val); + +if (tracepoint_enabled (write_msr)) +do_trace_write_msr (msr, val, err); + +return err; } static __always_inline u64 rdpmc(int counter) { -return native_read_pmc (counter); -} +u64 val; -#endif /* !CONFIG_PARAVIRT_XXL */ + val =do_rdpmc (counter); +if (tracepoint_enabled (rdpmc)) +do_trace_rdpmc (counter, val,0 ); + +return val; +}/* Instruction opcode for WRMSRNS supported in binutils >= 2.40 */ #define ASM_WRMSRNS _ASM_BYTES(0x0f,0x01,0xc6)
arch/x86/include/asm/paravirt.h +5 -5
@@ -150,27 +150,27 @@ static inline int paravirt_write_msr_safe(u32 msr, u64 val)return PVOP_CALL2 (int, pv_ops, cpu.write_msr_safe, msr, val); } -static __always_inline u64 rdmsrq(u32 msr) +static __always_inline u64 read_msr(u32 msr) {return paravirt_read_msr (msr); } -static inline void wrmsrq (u32 msr,u64 val) +static inline void write_msr (u32 msr,u64 val) {paravirt_write_msr (msr, val); } -static inline int wrmsrq_safe (u32 msr,u64 val) +static inline int write_msr_safe (u32 msr,u64 val) {return paravirt_write_msr_safe (msr, val); } -static __always_inline int rdmsrq_safe(u32 msr, u64 *p) +static __always_inline int read_msr_safe(u32 msr, u64 *p) {return paravirt_read_msr_safe (msr, p); } -static __always_inline u64 rdpmc(int counter) +static __always_inline u64 do_rdpmc(int counter) {return PVOP_CALL1 (u64, pv_ops, cpu.read_pmc, counter); }
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 5/17 x86/hyperv: Switch from __rdmsr() to native_rdmsrq()
In reply to Juergen GrossThe __rdmsr() helper will be changed soon, so don't use it directly outside of msr.h. Switch to native_rdmsrq() in HyperV related code.
---
V4:
- new patch (kernel test robot)
--- arch/x86/hyperv/hv_crash.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/hyperv/hv_crash.c +3 -3
@@ -217,9 +217,9 @@ static void hv_hvcrash_ctxt_save(void)native_store_gdt (&ctxt->gdtr);store_idt (&ctxt->idtr); - ctxt->gsbase =__rdmsr (MSR_GS_BASE ); - ctxt->efer =__rdmsr (MSR_EFER ); - ctxt->pat =__rdmsr (MSR_IA32_CR_PAT ); + ctxt->gsbase =native_rdmsrq (MSR_GS_BASE ); + ctxt->efer =native_rdmsrq (MSR_EFER ); + ctxt->pat =native_rdmsrq (MSR_IA32_CR_PAT ); }/* Add trampoline page to the kernel pagetable for transition to kernel PT */
Signature · 1 line
2.56.0
RE: [PATCH v6 05/17] x86/hyperv: Switch from __rdmsr() to native_rdmsrq()
From: Juergen Gross <[email protected]> Sent: Monday, November 16, 2026 10:39 AM
Quoted 33 lines
The
__rdmsr()helper will be changed soon, so don't use it directly outside ofmsr.h. Switch tonative_rdmsrq()in HyperV related code.Reported-by kernel test robot <[email protected]>Signed-off-by Juergen Gross <[email protected]>---
V4:
- new patch (kernel test robot)--- arch/x86/hyperv/hv_crash.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/x86/hyperv/hv_crash.c +3
-4@@ -217,9 +217,9 @@ static void hv_hvcrash_ctxt_save(void)native_store_gdt (&ctxt->gdtr);store_idt (&ctxt->idtr); - ctxt->gsbase =__rdmsr (MSR_GS_BASE ); - ctxt->efer =__rdmsr (MSR_EFER ); - ctxt->pat =__rdmsr (MSR_IA32_CR_PAT ); + ctxt->gsbase =native_rdmsrq (MSR_GS_BASE ); + ctxt->efer =native_rdmsrq (MSR_EFER ); + ctxt->pat =native_rdmsrq (MSR_IA32_CR_PAT ); }/* Add trampoline page to the kernel pagetable for transition to kernel PT */ --
Quoted 2 lines
2.56.0
Juergen Gross
maintainerPATCH v6 6/17 x86/opcode: Add immediate form MSR instructions
In reply to Juergen GrossAdd the instruction opcodes used by the immediate form WRMSRNS/RDMSR to x86-opcode-map.
---
V2:
- new patch, taken from the RFC v2 MSR refactor series by Xin Li
--- arch/x86/lib/x86-opcode-map.txt | 5 +++-- tools/arch/x86/lib/x86-opcode-map.txt | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/lib/x86-opcode-map.txt +3 -2
@@ -844,7 +844,7 @@ f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSSD/Q My,Gy (66) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My,Gy +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My,Gy | RDMSR Rq,Gq (F2),(11B) | WRMSRNS Gq,Rq (F3),(11B) f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) | URDMSR Rq,Gq (F2),(11B) | UWRMSR Gq,Rq (F3),(11B) f9: MOVDIRI My,Gy
@@ -1019,7 +1019,7 @@ f1: CRC32 Gy,Ey (es) | CRC32 Gy,Ey (66),(es) | INVVPID Gy,Mdq (F3),(ev) f2: INVPCID Gy,Mdq (F3),(ev) f4: TZCNT Gv,Ev (es) | TZCNT Gv,Ev (66),(es) f5: LZCNT Gv,Ev (es) | LZCNT Gv,Ev (66),(es) -f6: Grp3_1 Eb (1A),(ev) +f6: Grp3_1 Eb (1A),(ev) | RDMSR Rq,Gq (F2),(11B),(ev) | WRMSRNS Gq,Rq (F3),(11B),(ev) f7: Grp3_2 Ev (1A),(es) f8: MOVDIR64B Gv,Mdqq (66),(ev) | ENQCMD Gv,Mdqq (F2),(ev) | ENQCMDS Gv,Mdqq (F3),(ev) | URDMSR Rq,Gq (F2),(11B),(ev) | UWRMSR Gq,Rq (F3),(11B),(ev) f9: MOVDIRI My,Gy (ev)
@@ -1108,6 +1108,7 @@ EndTable Table: VEX map 7 Referrer: AVXcode: 7 +f6: RDMSR Rq,Id (F2),(v1),(11B) | WRMSRNS Id,Rq (F3),(v1),(11B) f8: URDMSR Rq,Id (F2),(v1),(11B) | UWRMSR Id,Rq (F3),(v1),(11B) EndTable
tools/arch/x86/lib/x86-opcode-map.txt +3 -2
@@ -844,7 +844,7 @@ f1: MOVBE My,Gy | MOVBE Mw,Gw (66) | CRC32 Gd,Ey (F2) | CRC32 Gd,Ew (66&F2) f2: ANDN Gy,By,Ey (v) f3: Grp17 (1A) f5: BZHI Gy,Ey,By (v) | PEXT Gy,By,Ey (F3),(v) | PDEP Gy,By,Ey (F2),(v) | WRUSSD/Q My,Gy (66) -f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My,Gy +f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSSD/Q My,Gy | RDMSR Rq,Gq (F2),(11B) | WRMSRNS Gq,Rq (F3),(11B) f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v) f8: MOVDIR64B Gv,Mdqq (66) | ENQCMD Gv,Mdqq (F2) | ENQCMDS Gv,Mdqq (F3) | URDMSR Rq,Gq (F2),(11B) | UWRMSR Gq,Rq (F3),(11B) f9: MOVDIRI My,Gy
@@ -1019,7 +1019,7 @@ f1: CRC32 Gy,Ey (es) | CRC32 Gy,Ey (66),(es) | INVVPID Gy,Mdq (F3),(ev) f2: INVPCID Gy,Mdq (F3),(ev) f4: TZCNT Gv,Ev (es) | TZCNT Gv,Ev (66),(es) f5: LZCNT Gv,Ev (es) | LZCNT Gv,Ev (66),(es) -f6: Grp3_1 Eb (1A),(ev) +f6: Grp3_1 Eb (1A),(ev) | RDMSR Rq,Gq (F2),(11B),(ev) | WRMSRNS Gq,Rq (F3),(11B),(ev) f7: Grp3_2 Ev (1A),(es) f8: MOVDIR64B Gv,Mdqq (66),(ev) | ENQCMD Gv,Mdqq (F2),(ev) | ENQCMDS Gv,Mdqq (F3),(ev) | URDMSR Rq,Gq (F2),(11B),(ev) | UWRMSR Gq,Rq (F3),(11B),(ev) f9: MOVDIRI My,Gy (ev)
@@ -1108,6 +1108,7 @@ EndTable Table: VEX map 7 Referrer: AVXcode: 7 +f6: RDMSR Rq,Id (F2),(v1),(11B) | WRMSRNS Id,Rq (F3),(v1),(11B) f8: URDMSR Rq,Id (F2),(v1),(11B) | UWRMSR Id,Rq (F3),(v1),(11B) EndTable
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 7/17 x86/extable: Add support for immediate form MSR instructions
In reply to Juergen Gross---
V2:
- new patch, taken from the RFC v2 MSR refactor series by Xin Li V3:
- use instruction decoder (Peter Zijlstra)
V4:
- don't assume %rax for immediate form (Andrew Cooper) V5:
- drop stale comment (H. Peter Anvin)
--- arch/x86/mm/extable.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-)
arch/x86/mm/extable.c +35 -6
@@ -166,25 +166,54 @@ static bool ex_handler_uaccess(const struct exception_table_entry *fixup,static bool ex_handler_msr (const struct exception_table_entry *fixup,struct pt_regs *regs,bool wrmsr,bool safe,int reg) { +unsigned long *regptr; +struct insn insn; +bool imm_insn; +u32 msr; + + imm_insn =insn_decode_kernel (&insn, (void *)regs->ip) && + insn.vex_prefix.nbytes; + msr = imm_insn ? insn.immediate.value : (u32 )regs->cx; + regptr = imm_insn ?insn_get_modrm_reg_ptr (&insn, regs) : ®s->ax; +if (unlikely (!regptr)) { +pr_err ("Inconsistent %sMSR access instruction data at rIP: 0x%lx (%pS)!\n" , + wrmsr ?"WR" :"RD" , regs->ip, (void *)regs->ip); +show_stack_regs (regs); + goto out; + } +if (__ONCE_LITE_IF (!safe && wrmsr)) { -pr_warn ("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n" , - (unsigned int )regs->cx, (unsigned int )regs->dx, - (unsigned int )regs->ax, regs->ip, (void *)regs->ip); +u64 msr_val = *regptr; + +if (!imm_insn) { +/* +* On processors that support the Intel 64 architecture, the +* high-order 32 bits of each of RAX and RDX are ignored. +*/ + msr_val &=0xffffffff ; + msr_val |= (u64 )regs->dx <<32 ; + } + +pr_warn ("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%016llx) at rIP: 0x%lx (%pS)\n" , + msr, msr_val, regs->ip, (void *)regs->ip);show_stack_regs (regs); }if (__ONCE_LITE_IF (!safe && !wrmsr)) {pr_warn ("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n" , - (unsigned int )regs->cx, regs->ip, (void *)regs->ip); + msr, regs->ip, (void *)regs->ip);show_stack_regs (regs); }if (!wrmsr) {/* Pretend that the read succeeded and returned 0. */ - regs->ax =0 ; - regs->dx =0 ; + *regptr =0 ; + +if (!imm_insn) + regs->dx =0 ; } + out:if (safe) *pt_regs_nr (regs, reg) = -EIO ;
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 8/17 x86/msr: Make wrmsrns() a first class citizen
In reply to Juergen GrossToday 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.
---
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().
V6:
- add instruction delimiter in macro (Shreshth Srivastava)
--- arch/x86/include/asm/fred.h | 2 +- arch/x86/include/asm/msr.h | 158 ++++++++++++++++++++++++++++---- arch/x86/include/asm/paravirt.h | 10 ++ 3 files changed, 152 insertions(+), 18 deletions(-)
arch/x86/include/asm/fred.h +1 -1
@@ -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); } }
arch/x86/include/asm/msr.h +141 -17
210 lines not on this page · raw message
arch/x86/include/asm/paravirt.h +10 -0
@@ -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);
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 9/17 x86/msr: Introduce sync_cpu_after_wrmsrns()
In reply to Juergen GrossIn order to allow using wrmsrns() for multiple MSR register writes introduce sync_cpu_after_wrmsrns() which will then do a CPU serialize operation, if the hardware does support the WRMSRNS instruction. In case the hardware doesn't support WRMSRNS, sync_cpu_after_wrmsrns() will be a NOP.
---
V4:
- new patch
--- arch/x86/include/asm/msr.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/msr.h +7 -0
@@ -14,6 +14,7 @@#include <linux/errno.h> #include <linux/types.h> #include <linux/percpu.h> +#include <asm/special_insns.h> struct msr_info {u32 msr_no;
@@ -361,6 +362,12 @@ static __always_inline int wrmsrns_safe(u32 msr, u64 val)return err; } +static __always_inline void sync_cpu_after_wrmsrns(void) +{ +if (cpu_feature_enabled (X86_FEATURE_WRMSRNS )) +serialize (); +} + static __always_inline u64 rdpmc(int counter) {u64 val;
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 10/17 x86/msr: Use the alternatives mechanism for RDMSR
In reply to Juergen GrossWhen available use the immediate variant of RDMSR in __rdmsr().
For the safe/unsafe variants make __rdmsr() to be a common base function instead of duplicating the ALTERNATIVE*() macros.
Modify native_rdmsr() and native_read_msr() to use native_rdmsrq().
The paravirt case will be handled later.
---
V2:
- new patch, partially taken from "[RFC PATCH v2 22/34] x86/msr: Utilize the alternatives mechanism to read MSR" by Xin Li
V6:
- add instruction delimiter in macro (Shreshth Srivastava)
--- arch/x86/include/asm/msr.h | 106 +++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 22 deletions(-)
arch/x86/include/asm/msr.h +84 -22
@@ -66,6 +66,8 @@ static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {}/* 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_RDMSR_IMM \ + " .insn VEX.128.F2.M7.W0 0xf6 /0, %[msr]%{:u32}, %[val]\n\t"#define ASM_WRMSRNS_IMM \ " .insn VEX.128.F3.M7.W0 0xf6 /0, %[val], %[msr]%{:u32}\n\t"#else
@@ -75,10 +77,17 @@ static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {}* 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_RDMSR_IMM \ + " .byte 0xc4,0xe7,0x7b,0xf6,0xc0; .long %c[msr]\n\t"#define ASM_WRMSRNS_IMM \ " .byte 0xc4,0xe7,0x7a,0xf6,0xc0; .long %c[msr]\n\t"#endif +#define RDMSR_AND_SAVE_RESULT \ + "rdmsr\n\t" \ + "shl $0x20, %%rdx\n\t" \ + "or %%rdx, %%rax\n\t" +#define PREPARE_RDX_FOR_WRMSR \ "mov %%rax, %%rdx\n\t" \ "shr $0x20, %%rdx\n\t"
@@ -94,16 +103,76 @@ static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {}* think of extending them - you will be slapped with a stinking trout or a frozen * shark will reach you, wherever you are! You've been warned. */ -static __always_inline u64 __rdmsr(u32 msr) +static __always_inline bool __rdmsrq_variable(u32 msr, u64 *val, int type) + { +#ifdef CONFIG_X86_64 +BUILD_BUG_ON (__builtin_constant_p (msr)); + + asm_inline volatile goto( +"1:\n" +RDMSR_AND_SAVE_RESULT + _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For RDMSR */ + + : [val] "=a" (*val) + : "c" (msr), [type] "i" (type) + : "rdx" + : badmsr); +#else + asm_inline volatile goto( + "1: rdmsr\n\t" + _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For RDMSR */ + + : "=A" (*val) + : "c" (msr), [type] "i" (type) + : + : badmsr); +#endif + +return false; + +badmsr: + *val =0 ; + +return true; +} + +#ifdef CONFIG_X86_64 +static __always_inline bool __rdmsrq_constant(u32 msr, u64 *val, int type) { -EAX_EDX_DECLARE_ARGS (val, low, high); +BUILD_BUG_ON (!__builtin_constant_p (msr)); - asm volatile("1: rdmsr\n" - "2:\n" - _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_RDMSR) - : EAX_EDX_RET(val, low, high) : "c" (msr)); + asm_inline volatile goto( +"1:\n" +ALTERNATIVE ("mov %[msr], %%ecx\n\t" +"2:\n" +RDMSR_AND_SAVE_RESULT , +ASM_RDMSR_IMM , + X86_FEATURE_MSR_IMM) + _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For RDMSR immediate */ + _ASM_EXTABLE_TYPE(2b, %l[badmsr], %c[type]) /* For RDMSR */ + + : [val] "=a" (*val) + : [msr] "i" (msr), [type] "i" (type) + : "ecx", "rdx" + : badmsr); -return EAX_EDX_VAL (val, low, high); +return false; + +badmsr: + *val =0 ; + +return true; +} +#endif + +static __always_inline bool __rdmsr(u32 msr, u64 *val, int type) +{ +#ifdef CONFIG_X86_64 +if (__builtin_constant_p (msr)) +return __rdmsrq_constant (msr, val, type); +#endif + +return __rdmsrq_variable (msr, val, type); } static __always_inline void __wrmsrq(u32 msr, u64 val)
@@ -188,7 +257,11 @@ static __always_inline bool __wrmsrns(u32 msr, u64 val, int type) static __always_inline u64 native_rdmsrq(u32 msr) { -return __rdmsr (msr); +u64 val; + +__rdmsr (msr, &val,EX_TYPE_RDMSR ); + +return val; } static __always_inline void native_wrmsrq(u32 msr, u64 val)
@@ -198,23 +271,12 @@ static __always_inline void native_wrmsrq(u32 msr, u64 val)static inline u64 native_read_msr (u32 msr) { -return __rdmsr (msr); +return native_rdmsrq (msr); } -static inline int native_read_msr_safe (u32 msr,u64 *p) +static inline int native_read_msr_safe (u32 msr,u64 *val) { -int err; -EAX_EDX_DECLARE_ARGS (val, low, high); - - asmvolatile ("1: rdmsr ; xor %[err],%[err]\n" -"2:\n\t" - _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_RDMSR_SAFE, %[err]) - : [err] "=r" (err), EAX_EDX_RET(val, low, high) - : "c" (msr)); - - *p =EAX_EDX_VAL (val, low, high); - -return err; +return __rdmsr (msr, val,EX_TYPE_RDMSR_SAFE ) ? -EIO :0 ; }/* Can be uninlined because referenced by paravirt */
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 11/17 x86/alternatives: Add ALTERNATIVE_4()
In reply to Juergen GrossFor supporting WRMSR with CONFIG_PARAVIRT_XXL using direct instruction replacement, ALTERNATIVE_4() is needed.
---
V3:
- new patch
--- arch/x86/include/asm/alternative.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/alternative.h +6 -0
@@ -180,6 +180,12 @@ static __always_inline bool cpu_wants_rethunk_at(void *addr)ALTERNATIVE (ALTERNATIVE_2 (oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2), \ newinstr3, ft_flags3) +#define ALTERNATIVE_4 (oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, \ + newinstr3, ft_flags3, newinstr4, ft_flags4) \ + ALTERNATIVE(ALTERNATIVE_3(oldinstr, newinstr1, ft_flags1, \ + newinstr2, ft_flags2, newinstr3, ft_flags3),\ + newinstr4, ft_flags4) +/* * Alternative instructions for different CPU types or capabilities. *
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 12/17 x86/paravirt: Split off MSR related hooks into new header
In reply to Juergen GrossMove the WRMSR, RDMSR and RDPMC related parts of paravirt.h and paravirt_types.h into a new header file paravirt-msr.h.
Switch all moved helper functions to __always_inline.
---
V3:
- new patch
V4:
- always use __always_inline
--- arch/x86/include/asm/msr.h | 2 +- arch/x86/include/asm/paravirt-msr.h | 56 +++++++++++++++++++++++++++ arch/x86/include/asm/paravirt.h | 55 -------------------------- arch/x86/include/asm/paravirt_types.h | 13 ------- arch/x86/kernel/paravirt.c | 14 ++++--- arch/x86/xen/enlighten_pv.c | 11 +++--- tools/objtool/check.c | 1 + 7 files changed, 73 insertions(+), 79 deletions(-) create mode 100644 arch/x86/include/asm/paravirt-msr.h
arch/x86/include/asm/msr.h +1 -1
@@ -322,7 +322,7 @@ static inline u64 native_read_pmc(int counter) }#ifdef CONFIG_PARAVIRT_XXL -#include <asm/paravirt.h> +#include <asm/paravirt-msr.h> #else static __always_inline u64 read_msr(u32 msr) {
arch/x86/include/asm/paravirt-msr.h new file +56 -0
56 lines not on this page · raw message
arch/x86/include/asm/paravirt.h +0 -55
61 lines not on this page · raw message
arch/x86/include/asm/paravirt_types.h +0 -13
@@ -58,19 +58,6 @@ struct pv_cpu_ops {void (*cpuid)(unsigned int *eax,unsigned int *ebx,unsigned int *ecx,unsigned int *edx); -/* Unsafe MSR operations. These will warn or panic on failure. */ -u64 (*read_msr)(u32 msr); -void (*write_msr)(u32 msr,u64 val); - -/* -* Safe MSR operations. -* Returns 0 or -EIO. -*/ -int (*read_msr_safe)(u32 msr,u64 *val); -int (*write_msr_safe)(u32 msr,u64 val); - -u64 (*read_pmc)(int counter); -void (*start_context_switch)(struct task_struct *prev);void (*end_context_switch)(struct task_struct *next);#endif
arch/x86/kernel/paravirt.c +9 -5
@@ -110,11 +110,6 @@ struct paravirt_patch_template pv_ops = { .cpu.read_cr0 = native_read_cr0, .cpu.write_cr0 = native_write_cr0, .cpu.write_cr4 = native_write_cr4, - .cpu.read_msr = native_read_msr, - .cpu.write_msr = native_write_msr, - .cpu.read_msr_safe = native_read_msr_safe, - .cpu.write_msr_safe = native_write_msr_safe, - .cpu.read_pmc = native_read_pmc, .cpu.load_tr_desc = native_load_tr_desc, .cpu.set_ldt = native_set_ldt, .cpu.load_gdt = native_load_gdt,
@@ -212,6 +207,15 @@ struct paravirt_patch_template pv_ops = { };#ifdef CONFIG_PARAVIRT_XXL +struct pv_msr_ops pv_ops_msr = { + .read_msr = native_read_msr, + .write_msr = native_write_msr, + .read_msr_safe = native_read_msr_safe, + .write_msr_safe = native_write_msr_safe, + .read_pmc = native_read_pmc, +}; +EXPORT_SYMBOL (pv_ops_msr); +NOKPROBE_SYMBOL (native_load_idt);#endif
arch/x86/xen/enlighten_pv.c +6 -5
@@ -1360,11 +1360,6 @@ asmlinkage __visible void __init xen_start_kernel(struct start_info *si) pv_ops.cpu.read_cr0 = xen_read_cr0; pv_ops.cpu.write_cr0 = xen_write_cr0; pv_ops.cpu.write_cr4 = xen_write_cr4; - pv_ops.cpu.read_msr = xen_read_msr; - pv_ops.cpu.write_msr = xen_write_msr; - pv_ops.cpu.read_msr_safe = xen_read_msr_safe; - pv_ops.cpu.write_msr_safe = xen_write_msr_safe; - pv_ops.cpu.read_pmc = xen_read_pmc; pv_ops.cpu.load_tr_desc = paravirt_nop; pv_ops.cpu.set_ldt = xen_set_ldt; pv_ops.cpu.load_gdt = xen_load_gdt;
@@ -1385,6 +1380,12 @@ asmlinkage __visible void __init xen_start_kernel(struct start_info *si) pv_ops.cpu.start_context_switch = xen_start_context_switch; pv_ops.cpu.end_context_switch = xen_end_context_switch; + pv_ops_msr.read_msr = xen_read_msr; + pv_ops_msr.write_msr = xen_write_msr; + pv_ops_msr.read_msr_safe = xen_read_msr_safe; + pv_ops_msr.write_msr_safe = xen_write_msr_safe; + pv_ops_msr.read_pmc = xen_read_pmc; +xen_init_irq_ops ();/*
tools/objtool/check.c +1 -0
@@ -530,6 +530,7 @@ static struct { } pv_ops_tables[] = { { .name = "pv_ops", }, { .name = "pv_ops_lock", }, + { .name = "pv_ops_msr", }, { .name = NULL, .idx_off = -1 } };
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 13/17 x86/paravirt: Prepare support of MSR instruction interfaces
In reply to Juergen GrossMake the paravirt callee-save infrastructure more generic by allowing arbitrary register interfaces via prologue and epilogue helper macros.
---
V3:
- carved out from patch 5 of V1
--- arch/x86/include/asm/paravirt_types.h | 43 ++++++++++++++--------- arch/x86/include/asm/qspinlock_paravirt.h | 4 +-- 2 files changed, 29 insertions(+), 18 deletions(-)
arch/x86/include/asm/paravirt_types.h +27 -16
@@ -448,27 +448,38 @@ extern struct paravirt_patch_template pv_ops;#define PV_SAVE_ALL_CALLER_REGS "pushl %ecx;"#define PV_RESTORE_ALL_CALLER_REGS "popl %ecx;"#else +/* Save and restore caller-save registers, except %rax, %rcx and %rdx. */ +#define PV_SAVE_COMMON_CALLER_REGS \ + "push %rsi;" \ + "push %rdi;" \ + "push %r8;" \ + "push %r9;" \ + "push %r10;" \ + "push %r11;" + +#define PV_RESTORE_COMMON_CALLER_REGS \ + "pop %r11;" \ + "pop %r10;" \ + "pop %r9;" \ + "pop %r8;" \ + "pop %rdi;" \ + "pop %rsi;" +/* save and restore all caller-save registers, except return value */ #define PV_SAVE_ALL_CALLER_REGS \ "push %rcx;" \ "push %rdx;" \ - "push %rsi;" \ - "push %rdi;" \ - "push %r8;" \ - "push %r9;" \ - "push %r10;" \ - "push %r11;" + PV_SAVE_COMMON_CALLER_REGS +#define PV_RESTORE_ALL_CALLER_REGS \ - "pop %r11;" \ - "pop %r10;" \ - "pop %r9;" \ - "pop %r8;" \ - "pop %rdi;" \ - "pop %rsi;" \ + PV_RESTORE_COMMON_CALLER_REGS \ "pop %rdx;" \ "pop %rcx;"#endif +#define PV_PROLOGUE_ALL (func) PV_SAVE_ALL_CALLER_REGS +#define PV_EPILOGUE_ALL (func) PV_RESTORE_ALL_CALLER_REGS +/* * Generate a thunk around a function which saves all caller-save * registers except for the return value. This allows C functions to
@@ -482,7 +493,7 @@ extern struct paravirt_patch_template pv_ops;* functions. */ #define PV_THUNK_NAME (func) "__raw_callee_save_" #func -#define __PV_CALLEE_SAVE_REGS_THUNK (func, section) \ +#define __PV_CALLEE_SAVE_REGS_THUNK (func, section, helper) \ extern typeof(func) __raw_callee_save_##func; \ \ asm(".pushsection " section ", \"ax\";" \
@@ -492,16 +503,16 @@ extern struct paravirt_patch_template pv_ops; PV_THUNK_NAME(func) ":" \ ASM_ENDBR \ FRAME_BEGIN \ -PV_SAVE_ALL_CALLER_REGS \ + PV_PROLOGUE_##helper(func) \ "call " #func ";" \ - PV_RESTORE_ALL_CALLER_REGS \ + PV_EPILOGUE_##helper(func) \ FRAME_END \ ASM_RET \ ".size " PV_THUNK_NAME(func) ", .-" PV_THUNK_NAME(func) ";" \ ".popsection")#define PV_CALLEE_SAVE_REGS_THUNK (func) \ - __PV_CALLEE_SAVE_REGS_THUNK(func, ".text") + __PV_CALLEE_SAVE_REGS_THUNK(func, ".text", ALL)/* Get a reference to a callee-save function */ #define PV_CALLEE_SAVE (func) \
arch/x86/include/asm/qspinlock_paravirt.h +2 -2
@@ -14,7 +14,7 @@ void __lockfunc __pv_queued_spin_unlock_slowpath(struct qspinlock *lock, u8 lock*/ #ifdef CONFIG_64BIT -__PV_CALLEE_SAVE_REGS_THUNK (__pv_queued_spin_unlock_slowpath,".spinlock.text" ); +__PV_CALLEE_SAVE_REGS_THUNK (__pv_queued_spin_unlock_slowpath,".spinlock.text" ,ALL );#define __pv_queued_spin_unlock __pv_queued_spin_unlock/*
@@ -61,7 +61,7 @@ DEFINE_ASM_FUNC(__raw_callee_save___pv_queued_spin_unlock,#else /* CONFIG_64BIT */ extern void __lockfunc __pv_queued_spin_unlock(struct qspinlock *lock); -__PV_CALLEE_SAVE_REGS_THUNK (__pv_queued_spin_unlock,".spinlock.text" ); +__PV_CALLEE_SAVE_REGS_THUNK (__pv_queued_spin_unlock,".spinlock.text" ,ALL );#endif /* CONFIG_64BIT */ #endif
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 14/17 x86/paravirt: Switch MSR access pv_ops functions to instruction interfaces
In reply to Juergen GrossIn order to prepare for inlining RDMSR/WRMSR instructions via alternatives directly when running not in a Xen PV guest, switch the interfaces of the MSR related pvops callbacks to ones similar of the related instructions.
In order to prepare for supporting the immediate variants of RDMSR/WRMSR use a 64-bit interface instead of the 32-bit one of RDMSR/WRMSR.
---
V3:
- former patch 5 of V1 has been split
- use 64-bit interface (Xin Li)
--- arch/x86/include/asm/paravirt-msr.h | 64 ++++++++++++++++++++++++----- arch/x86/kernel/paravirt.c | 36 ++++++++++++++-- arch/x86/xen/enlighten_pv.c | 45 +++++++++++++++----- 3 files changed, 120 insertions(+), 25 deletions(-)
arch/x86/include/asm/paravirt-msr.h +54 -10
@@ -6,46 +6,90 @@struct pv_msr_ops {/* Unsafe MSR operations. These will warn or panic on failure. */ -u64 (*read_msr)(u32 msr); -void (*write_msr)(u32 msr,u64 val); +struct paravirt_callee_save read_msr; +struct paravirt_callee_save write_msr;/* Safe MSR operations. Returns 0 or -EIO. */ -int (*read_msr_safe)(u32 msr,u64 *val); -int (*write_msr_safe)(u32 msr,u64 val); +struct paravirt_callee_save read_msr_safe; +struct paravirt_callee_save write_msr_safe;u64 (*read_pmc)(int counter); } __no_randomize_layout;extern struct pv_msr_ops pv_ops_msr; +#define PV_PROLOGUE_MSR (func) \ + PV_SAVE_COMMON_CALLER_REGS \ + PV_PROLOGUE_MSR_##func + +#define PV_EPILOGUE_MSR (func) PV_RESTORE_COMMON_CALLER_REGS + +#define PV_CALLEE_SAVE_REGS_MSR_THUNK (func) \ + __PV_CALLEE_SAVE_REGS_THUNK(func, ".text", MSR) + static __always_inline u64 read_msr(u32 msr) { -return PVOP_CALL1 (u64, pv_ops_msr, read_msr, msr); +u64 val; + + asm volatile(PARAVIRT_CALL + : "=a" (val), ASM_CALL_CONSTRAINT + : paravirt_ptr(pv_ops_msr, read_msr), "c" (msr) + : "rdx"); + +return val; } static __always_inline void write_msr(u32 msr, u64 val) { -PVOP_VCALL2 (pv_ops_msr, write_msr, msr, val); + asm volatile(PARAVIRT_CALL + : ASM_CALL_CONSTRAINT + : paravirt_ptr(pv_ops_msr, write_msr), "c" (msr), "a" (val) + : "memory", "rdx"); } static __always_inline void write_msrns(u32 msr, u64 val) { -PVOP_VCALL2 (pv_ops_msr, write_msr, msr, val); + asm volatile(PARAVIRT_CALL + : ASM_CALL_CONSTRAINT + : paravirt_ptr(pv_ops_msr, write_msr), "c" (msr), "a" (val) + : "memory", "rdx"); } static __always_inline int read_msr_safe(u32 msr, u64 *val) { -return PVOP_CALL2 (int, pv_ops_msr, read_msr_safe, msr, val); + int err; + + asm volatile(PARAVIRT_CALL + : [err] "=d" (err), "=a" (*val), ASM_CALL_CONSTRAINT + : paravirt_ptr(pv_ops_msr, read_msr_safe), "c" (msr)); + + return err ? -EIO : 0; } static __always_inline int write_msr_safe(u32 msr, u64 val) { -return PVOP_CALL2 (int, pv_ops_msr, write_msr_safe, msr, val); + int 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"); + + return err ? -EIO : 0; } static __always_inline int write_msrns_safe(u32 msr, u64 val) { -return PVOP_CALL2 (int, pv_ops_msr, write_msr_safe, msr, val); + int 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"); + + return err ? -EIO : 0; } static __always_inline u64 do_rdpmc(int counter)
arch/x86/kernel/paravirt.c +32 -4
@@ -50,12 +50,40 @@ unsigned long pv_native_save_fl(void);void pv_native_irq_disable (void );void pv_native_irq_enable (void );unsigned long pv_native_read_cr2 (void ); +void pv_native_rdmsr (void ); +void pv_native_wrmsr (void ); +void pv_native_rdmsr_safe (void ); +void pv_native_wrmsr_safe (void ); DEFINE_ASM_FUNC(_paravirt_ident_64, "mov %rdi, %rax", .text); DEFINE_ASM_FUNC(pv_native_save_fl, "pushf; pop %rax", .noinstr.text); DEFINE_ASM_FUNC(pv_native_irq_disable, "cli", .noinstr.text); DEFINE_ASM_FUNC(pv_native_irq_enable, "sti", .noinstr.text); DEFINE_ASM_FUNC(pv_native_read_cr2, "mov %cr2, %rax", .noinstr.text); +DEFINE_ASM_FUNC (pv_native_rdmsr, +"1: rdmsr\n" +"shl $32, %rdx; or %rdx, %rax\n" +"2:\n" + _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_RDMSR), .noinstr.text); +DEFINE_ASM_FUNC (pv_native_wrmsr, +"mov %rax, %rdx; shr $32, %rdx\n" +"1: wrmsr\n" +"2:\n" + _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_WRMSR), .noinstr.text); +DEFINE_ASM_FUNC (pv_native_rdmsr_safe, +"1: rdmsr\n" +"shl $32, %rdx; or %rdx, %rax\n" +"xor %edx, %edx\n" +"2:\n" + _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_RDMSR_SAFE, %%edx), + .noinstr.text); +DEFINE_ASM_FUNC (pv_native_wrmsr_safe, +"mov %rax, %rdx; shr $32, %rdx\n" +"1: wrmsr\n" +"xor %eax, %eax\n" +"2:\n" + _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_WRMSR_SAFE, %%eax), + .noinstr.text);#endif static noinstr void pv_native_safe_halt(void)
@@ -208,10 +236,10 @@ struct paravirt_patch_template pv_ops = {#ifdef CONFIG_PARAVIRT_XXL struct pv_msr_ops pv_ops_msr = { - .read_msr = native_read_msr, - .write_msr = native_write_msr, - .read_msr_safe = native_read_msr_safe, - .write_msr_safe = native_write_msr_safe, + .read_msr =__PV_IS_CALLEE_SAVE (pv_native_rdmsr), + .write_msr =__PV_IS_CALLEE_SAVE (pv_native_wrmsr), + .read_msr_safe =__PV_IS_CALLEE_SAVE (pv_native_rdmsr_safe), + .write_msr_safe =__PV_IS_CALLEE_SAVE (pv_native_wrmsr_safe), .read_pmc = native_read_pmc, };EXPORT_SYMBOL (pv_ops_msr);
arch/x86/xen/enlighten_pv.c +34 -11
79 lines not on this page · raw message
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 15/17 x86/msr: Reduce number of low level MSR access helpers
In reply to Juergen GrossSome MSR access helpers are redundant now, so remove the no longer needed ones.
--- arch/x86/include/asm/msr.h | 15 ++------------- arch/x86/xen/enlighten_pv.c | 4 ++-- 2 files changed, 4 insertions(+), 15 deletions(-)
arch/x86/include/asm/msr.h +2 -13
@@ -269,22 +269,11 @@ static __always_inline void native_wrmsrq(u32 msr, u64 val)__wrmsrq (msr, val); } -static inline u64 native_read_msr (u32 msr) -{ -return native_rdmsrq (msr); -} -static inline int native_read_msr_safe (u32 msr,u64 *val) {return __rdmsr (msr, val,EX_TYPE_RDMSR_SAFE ) ? -EIO :0 ; } -/* Can be uninlined because referenced by paravirt */ -static inline void notrace native_write_msr(u32 msr, u64 val) -{ -native_wrmsrq (msr, val); -} -/* Can be uninlined because referenced by paravirt */ static inline int notrace native_write_msr_safe(u32 msr, u64 val) {
@@ -326,7 +315,7 @@ static inline u64 native_read_pmc(int counter)#else static __always_inline u64 read_msr(u32 msr) { -return native_read_msr (msr); +return native_rdmsrq (msr); } static __always_inline int read_msr_safe(u32 msr, u64 *p)
@@ -336,7 +325,7 @@ static __always_inline int read_msr_safe(u32 msr, u64 *p) static __always_inline void write_msr(u32 msr, u64 val) { -native_write_msr (msr, val); +native_wrmsrq (msr, val); } static __always_inline int write_msr_safe(u32 msr, u64 val)
arch/x86/xen/enlighten_pv.c +2 -2
@@ -1085,7 +1085,7 @@ static u64 xen_do_read_msr(u32 msr, int *err)if (err) *err =native_read_msr_safe (msr, &val);else - val =native_read_msr (msr); + val =native_rdmsrq (msr);switch (msr) {case MSR_IA32_APICBASE :
@@ -1144,7 +1144,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)if (err) *err =native_write_msr_safe (msr, val);else -native_write_msr (msr, val); +native_wrmsrq (msr, val); } }
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 16/17 x86/paravirt: Use alternatives for MSR access with paravirt
In reply to Juergen GrossWhen 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.
---
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(-)
arch/x86/include/asm/paravirt-msr.h +108 -28
@@ -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 ; }
arch/x86/include/asm/paravirt_types.h +1 -0
@@ -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 */
Signature · 1 line
2.56.0
Juergen Gross
maintainerPATCH v6 17/17 x86/msr: Make all MSR access functions __always_inline
In reply to Juergen GrossThere are a few MSR access functions left which are not yet marked as __always_inline. Do the conversion.
Remove a leftover comment no longer being true related to this.
---
V4:
- new patch
--- arch/x86/include/asm/msr.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/include/asm/msr.h +6 -7
@@ -269,13 +269,12 @@ static __always_inline void native_wrmsrq(u32 msr, u64 val)__wrmsrq (msr, val); } -static inline int native_read_msr_safe (u32 msr,u64 *val) +static __always_inline int native_read_msr_safe(u32 msr, u64 *val) {return __rdmsr (msr, val,EX_TYPE_RDMSR_SAFE ) ? -EIO :0 ; } -/* Can be uninlined because referenced by paravirt */ -static inline int notrace native_write_msr_safe(u32 msr, u64 val) +static __always_inline int notrace native_write_msr_safe(u32 msr, u64 val) {int err;
@@ -301,7 +300,7 @@ static __always_inline int native_wrmsrns_safe(u32 msr, u64 val)extern int rdmsr_safe_regs (u32 regs[8 ]);extern int wrmsr_safe_regs (u32 regs[8 ]); -static inline u64 native_read_pmc (int counter) +static __always_inline u64 native_read_pmc(int counter) {EAX_EDX_DECLARE_ARGS (val, low, high);
@@ -362,7 +361,7 @@ static __always_inline u64 rdmsrq(u32 msr) }/* rdmsr with exception handling */ -static inline int rdmsrq_safe (u32 msr,u64 *p) +static __always_inline int rdmsrq_safe(u32 msr, u64 *p) {int err;
@@ -374,7 +373,7 @@ static inline int rdmsrq_safe(u32 msr, u64 *p)return err; } -static inline void wrmsrq (u32 msr,u64 val) +static __always_inline void wrmsrq(u32 msr, u64 val) {write_msr (msr, val);
@@ -383,7 +382,7 @@ static inline void wrmsrq(u32 msr, u64 val) }/* wrmsr with exception handling */ -static inline int wrmsrq_safe (u32 msr,u64 val) +static __always_inline int wrmsrq_safe(u32 msr, u64 val) {int err;
Signature · 1 line
2.56.0