x86/msr: Cleanup and FRED prep
v2 cover x86/msr: Cleanup and FRED prep
This started as the final couple of patches, and escalated...
Some has been posted before, two even back in 2018 (work literally done in Nanjing airport leaving XenSummit, and there was an 8h delay). The rest is FRED prep work, targetted for 4.21.
https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/1986711791
Andrew Cooper (16):
x86/msr: Implement rdmsr_safe() in C
x86/msr: Rework rdmsr_safe() using asm goto()
x86: Sort headers
x86/msr: Split out guest-msr.h
x86/msr: Split out fsgsbase.h
x86/msr: Split out tsc.h
x86/msr: Move msr_{split,fold}() into asm/cpu-user-regs.h
x86: Clean up asm/time.h
x86: Clean up asm/msr.h
x86/svm: Rename variable in svm_msr_write_intercept() x86/msr: Change rdmsr() to have normal API x86/msr: Change wrmsr() to take a single parameter x86/msr: Use MSR_IMM when available x86/fsgsbase: Split out __{rd,wr}gskern() helpers x86/fsgsbase: Update fs/gs helpers to use wrmsrns() x86/fsgsbase: Improve code generation in read_registers()
xen/arch/x86/acpi/cpufreq/acpi.c | 7 +- xen/arch/x86/acpi/cpufreq/cpufreq.c | 3 + xen/arch/x86/acpi/cpufreq/hwp.c | 18 +- xen/arch/x86/acpi/cpufreq/powernow.c | 12 +- xen/arch/x86/apic.c | 2 +- xen/arch/x86/cpu/amd.c | 33 +- xen/arch/x86/cpu/common.c | 27 +- xen/arch/x86/cpu/intel.c | 38 +- xen/arch/x86/cpu/mcheck/mce.h | 3 +- xen/arch/x86/cpu/mcheck/mce_amd.c | 2 +- xen/arch/x86/cpu/mcheck/mce_intel.c | 6 +- xen/arch/x86/cpu/microcode/intel.c | 4 + xen/arch/x86/cpuid.c | 1 + xen/arch/x86/debug.c | 2 + xen/arch/x86/domain.c | 16 +- xen/arch/x86/domctl.c | 45 +- xen/arch/x86/genapic/x2apic.c | 5 +- xen/arch/x86/guest/hyperv/hyperv.c | 1 + xen/arch/x86/hvm/hvm.c | 77 +-- xen/arch/x86/hvm/ioreq.c | 3 +- xen/arch/x86/hvm/svm/svm.c | 31 +- xen/arch/x86/hvm/svm/vmcb.c | 5 +- xen/arch/x86/hvm/vmx/vmcs.c | 32 +- xen/arch/x86/hvm/vmx/vmx.c | 64 +-- xen/arch/x86/hvm/vmx/vvmx.c | 8 +- xen/arch/x86/include/asm/alternative.h | 7 + xen/arch/x86/include/asm/cpu-user-regs.h | 11 + xen/arch/x86/include/asm/fsgsbase.h | 112 +++++ xen/arch/x86/include/asm/guest-msr.h | 155 ++++++ xen/arch/x86/include/asm/hvm/hvm.h | 3 +- xen/arch/x86/include/asm/msr.h | 502 +++++++------------- xen/arch/x86/include/asm/prot-key.h | 6 +- xen/arch/x86/include/asm/time.h | 13 +- xen/arch/x86/include/asm/tsc.h | 46 ++ xen/arch/x86/machine_kexec.c | 1 + xen/arch/x86/msr.c | 9 +- xen/arch/x86/nmi.c | 18 +- xen/arch/x86/oprofile/op_model_athlon.c | 2 +- xen/arch/x86/platform_hypercall.c | 2 +- xen/arch/x86/psr.c | 2 + xen/arch/x86/pv/domain.c | 1 + xen/arch/x86/pv/emul-priv-op.c | 22 +- xen/arch/x86/pv/emulate.h | 1 + xen/arch/x86/pv/misc-hypercalls.c | 1 + xen/arch/x86/spec_ctrl.c | 2 +- xen/arch/x86/tboot.c | 16 +- xen/arch/x86/traps.c | 16 +- xen/arch/x86/tsx.c | 29 +- xen/arch/x86/x86_64/asm-offsets.c | 1 + xen/arch/x86/x86_64/mmconfig-shared.c | 2 +- xen/arch/x86/xstate.c | 5 +- xen/common/efi/runtime.c | 1 + xen/common/ubsan/ubsan.c | 2 + xen/drivers/acpi/apei/apei-base.c | 15 +- xen/drivers/acpi/tables/tbfadt.c | 2 + xen/drivers/acpi/utilities/utmisc.c | 3 + xen/drivers/passthrough/vtd/iommu.c | 2 +- xen/include/public/arch-x86/cpufeatureset.h | 1 + xen/lib/x86/private.h | 2 +- 59 files changed, 846 insertions(+), 612 deletions(-) create mode 100644 xen/arch/x86/include/asm/fsgsbase.h create mode 100644 xen/arch/x86/include/asm/guest-msr.h create mode 100644 xen/arch/x86/include/asm/tsc.h
Signature · 1 line
2.39.5
PATCH v2 1/16 x86/msr: Implement rdmsr_safe() in C
... in preparation to be able to use asm goto.
Notably this mean that the value parameter must be taken by pointer rather than by value.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/acpi/cpufreq/hwp.c | 14 +++++------ xen/arch/x86/apic.c | 2 +- xen/arch/x86/cpu/amd.c | 25 +++++++++---------- xen/arch/x86/cpu/common.c | 6 ++--- xen/arch/x86/cpu/intel.c | 8 +++--- xen/arch/x86/cpu/mcheck/mce_amd.c | 2 +- xen/arch/x86/cpu/mcheck/mce_intel.c | 6 ++--- xen/arch/x86/hvm/ioreq.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 16 ++++++------ xen/arch/x86/hvm/vmx/vmx.c | 4 +-- xen/arch/x86/include/asm/msr.h | 35 ++++++++++++++++----------- xen/arch/x86/msr.c | 8 +++--- xen/arch/x86/platform_hypercall.c | 2 +- xen/arch/x86/pv/emul-priv-op.c | 18 +++++++------- xen/arch/x86/spec_ctrl.c | 2 +- xen/arch/x86/x86_64/mmconfig-shared.c | 2 +- xen/drivers/passthrough/vtd/iommu.c | 2 +- 17 files changed, 80 insertions(+), 74 deletions(-)
xen/arch/x86/acpi/cpufreq/hwp.c +7 -7
@@ -245,7 +245,7 @@ static void cf_check hwp_write_request(void *info) {hwp_verbose ("CPU%u: error wrmsr_safe(MSR_HWP_REQUEST, %lx)\n" , policy->cpu, hwp_req.raw); -rdmsr_safe (MSR_HWP_REQUEST , data->curr_req.raw); +rdmsr_safe (MSR_HWP_REQUEST , &data->curr_req.raw); data->ret = -EINVAL ; } }
@@ -281,7 +281,7 @@ static bool hdc_set_pkg_hdc_ctl(unsigned int cpu, bool val) {uint64_t msr; -if (rdmsr_safe (MSR_PKG_HDC_CTL , msr) ) +if (rdmsr_safe (MSR_PKG_HDC_CTL , &msr) ) {hwp_err (cpu,"rdmsr_safe(MSR_PKG_HDC_CTL)\n" );return false;
@@ -305,7 +305,7 @@ static bool hdc_set_pm_ctl1(unsigned int cpu, bool val) {uint64_t msr; -if (rdmsr_safe (MSR_PM_CTL1 , msr) ) +if (rdmsr_safe (MSR_PM_CTL1 , &msr) ) {hwp_err (cpu,"rdmsr_safe(MSR_PM_CTL1)\n" );return false;
@@ -353,7 +353,7 @@ static void cf_check hwp_init_msrs(void *info)* Package level MSR, but we don't have a good idea of packages here, so * just do it everytime. */ -if (rdmsr_safe (MSR_PM_ENABLE , val) ) +if (rdmsr_safe (MSR_PM_ENABLE , &val) ) {hwp_err (policy->cpu,"rdmsr_safe(MSR_PM_ENABLE)\n" ); data->curr_req.raw =-1 ;
@@ -375,13 +375,13 @@ static void cf_check hwp_init_msrs(void *info) } } -if (rdmsr_safe (MSR_HWP_CAPABILITIES , data->hwp_caps) ) +if (rdmsr_safe (MSR_HWP_CAPABILITIES , &data->hwp_caps) ) {hwp_err (policy->cpu,"rdmsr_safe(MSR_HWP_CAPABILITIES)\n" ); goto error; } -if (rdmsr_safe (MSR_HWP_REQUEST , data->curr_req.raw) ) +if (rdmsr_safe (MSR_HWP_REQUEST , &data->curr_req.raw) ) {hwp_err (policy->cpu,"rdmsr_safe(MSR_HWP_REQUEST)\n" ); goto error;
@@ -481,7 +481,7 @@ static void cf_check hwp_set_misc_turbo(void *info) data->ret =0 ; -if (rdmsr_safe (MSR_IA32_MISC_ENABLE , msr) ) +if (rdmsr_safe (MSR_IA32_MISC_ENABLE , &msr) ) {hwp_verbose ("CPU%u: error rdmsr_safe(MSR_IA32_MISC_ENABLE)\n" , policy->cpu);
xen/arch/x86/apic.c +1 -1
@@ -764,7 +764,7 @@ static int __init detect_init_APIC (void)if (enable_local_apic <0 )return -1 ; -if (rdmsr_safe (MSR_APIC_BASE , msr_content) ) +if (rdmsr_safe (MSR_APIC_BASE , &msr_content) ) {printk ("No local APIC present\n" );return -1 ;
xen/arch/x86/cpu/amd.c +12 -13
@@ -496,7 +496,7 @@ static void cf_check disable_c1e(void *unused)* The MSR does not exist in all FamilyF CPUs (only Rev F and above), * but we safely catch the #GP in that case. */ -if ((rdmsr_safe (MSR_K8_ENABLE_C1E , msr_content) ==0 ) && +if ((rdmsr_safe (MSR_K8_ENABLE_C1E , &msr_content) ==0 ) && (msr_content & (3ULL <<27 )) && (wrmsr_safe (MSR_K8_ENABLE_C1E , msr_content & ~(3ULL <<27 )) !=0 )) printk(KERN_ERR "Failed to disable C1E on CPU#%u (%16"PRIx64")\n",
@@ -695,21 +695,21 @@ static void amd_process_freq(const struct cpuinfo_x86 *c, lo =0 ;/* gcc may not recognize the loop having at least 5 iterations */ for (h = c->x86 ==0x10 ?5 :8 ; h--; ) -if (!rdmsr_safe (0xC0010064 + h, lo) && (lo >>63 )) +if (!rdmsr_safe (0xC0010064 + h, &lo) && (lo >>63 ))break ;if (!(lo >>63 ))return ;if (idx && idx < h && - !rdmsr_safe (0xC0010064 + idx, val) && (val >>63 ) && - !rdmsr_safe (0xC0010064 , hi) && (hi >>63 )) { + !rdmsr_safe (0xC0010064 + idx, &val) && (val >>63 ) && + !rdmsr_safe (0xC0010064 , &hi) && (hi >>63 )) {if (nom_mhz) *nom_mhz =amd_parse_freq (c->x86, val);if (low_mhz) *low_mhz =amd_parse_freq (c->x86, lo);if (hi_mhz) *hi_mhz =amd_parse_freq (c->x86, hi); - }else if (h && !rdmsr_safe (0xC0010064 , hi) && (hi >>63 )) { + }else if (h && !rdmsr_safe (0xC0010064 , &hi) && (hi >>63 )) {if (low_mhz) *low_mhz =amd_parse_freq (c->x86, lo);if (hi_mhz)
@@ -765,7 +765,7 @@ void amd_init_lfence(struct cpuinfo_x86 *c)* rather than per-thread, so do a full safe read/write/readback cycle * in the worst case. */ -if (rdmsr_safe (MSR_AMD64_DE_CFG , value)) +if (rdmsr_safe (MSR_AMD64_DE_CFG , &value))/* Unable to read. Assume the safer default. */ __clear_bit (X86_FEATURE_LFENCE_DISPATCH , c->x86_capability);
@@ -775,7 +775,7 @@ void amd_init_lfence(struct cpuinfo_x86 *c) c->x86_capability); else if (wrmsr_safe(MSR_AMD64_DE_CFG, value | AMD64_DE_CFG_LFENCE_SERIALISE) || - rdmsr_safe(MSR_AMD64_DE_CFG, value) || + rdmsr_safe(MSR_AMD64_DE_CFG, &value) || !(value & AMD64_DE_CFG_LFENCE_SERIALISE))/* Attempt to set failed. Assume the safer default. */ __clear_bit(X86_FEATURE_LFENCE_DISPATCH,
@@ -804,7 +804,7 @@ static bool set_legacy_ssbd(const struct cpuinfo_x86 *c, bool enable)if (bit >=0 ) {uint64_t val, mask =1ull << bit; - if (rdmsr_safe(MSR_AMD64_LS_CFG, val) || + if (rdmsr_safe(MSR_AMD64_LS_CFG, &val) || ({ val &= ~mask; if (enable)
@@ -962,7 +962,7 @@ void amd_init_spectral_chicken(void)if (cpu_has_hypervisor || !is_zen2_uarch ())return ; -if (rdmsr_safe (MSR_AMD64_DE_CFG2 , val) ==0 && !(val & chickenbit)) +if (rdmsr_safe (MSR_AMD64_DE_CFG2 , &val) ==0 && !(val & chickenbit))wrmsr_safe (MSR_AMD64_DE_CFG2 , val | chickenbit); }
@@ -1116,8 +1116,7 @@ static void amd_check_bp_cfg(void) static void cf_check init_amd(struct cpuinfo_x86 *c) {u32 l, h; - -unsigned long long value; +uint64_t value;/* Disable TLB flush filter by setting HWCR.FFDIS on K8 * bit 6 of msr C001_0015
@@ -1251,7 +1250,7 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)if ((c->x86 ==0x15 ) && (c->x86_model >=0x10 ) && (c->x86_model <=0x1f ) && !cpu_has (c,X86_FEATURE_TOPOEXT ) && - !rdmsr_safe (MSR_K8_EXT_FEATURE_MASK , value)) { + !rdmsr_safe (MSR_K8_EXT_FEATURE_MASK , &value)) { value |=1ULL <<54 ;wrmsr_safe (MSR_K8_EXT_FEATURE_MASK , value);rdmsrl (MSR_K8_EXT_FEATURE_MASK , value);
@@ -1267,7 +1266,7 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)* Disable it on the affected CPUs. */ if (c->x86 ==0x15 && c->x86_model >=0x02 && c->x86_model <0x20 && - !rdmsr_safe (MSR_AMD64_IC_CFG , value) && (value &0x1e ) !=0x1e ) + !rdmsr_safe (MSR_AMD64_IC_CFG , &value) && (value &0x1e ) !=0x1e )wrmsr_safe (MSR_AMD64_IC_CFG , value |0x1e );amd_get_topology (c);
xen/arch/x86/cpu/common.c +3 -3
@@ -130,14 +130,14 @@ bool __init probe_cpuid_faulting(void)uint64_t val;int rc; -if ((rc =rdmsr_safe (MSR_INTEL_PLATFORM_INFO , val)) ==0 ) +if ((rc =rdmsr_safe (MSR_INTEL_PLATFORM_INFO , &val)) ==0 ) raw_cpu_policy.platform_info.cpuid_faulting = val &MSR_PLATFORM_INFO_CPUID_FAULTING ;if (rc || !(val &MSR_PLATFORM_INFO_CPUID_FAULTING ) ||rdmsr_safe (MSR_INTEL_MISC_FEATURES_ENABLES , -this_cpu (msr_misc_features))) + &this_cpu (msr_misc_features))) {setup_clear_cpu_cap (X86_FEATURE_CPUID_FAULTING );return false;
@@ -851,7 +851,7 @@ static void skinit_enable_intr(void)* If the platform is performing a Secure Launch via SKINIT * INIT_REDIRECTION flag will be active. */ -if ( !cpu_has_skinit ||rdmsr_safe (MSR_K8_VM_CR , val) || +if ( !cpu_has_skinit ||rdmsr_safe (MSR_K8_VM_CR , &val) || !(val &VM_CR_INIT_REDIRECTION ) )return ;
xen/arch/x86/cpu/intel.c +4 -4
@@ -115,7 +115,7 @@ static uint64_t __init _probe_mask_msr(unsigned int *msr, uint64_t caps) expected_levelling_cap |= caps; -if (rdmsr_safe (*msr, val) ||wrmsr_safe (*msr, val)) +if (rdmsr_safe (*msr, &val) ||wrmsr_safe (*msr, val)) *msr =0 ;else levelling_caps |= caps;
@@ -546,7 +546,7 @@ static void intel_log_freq(const struct cpuinfo_x86 *c) { 26667, 13333, 20000, 16667, 33333, 10000, 40000 };case 6 : -if (rdmsr_safe (MSR_INTEL_PLATFORM_INFO , msrval) ) +if (rdmsr_safe (MSR_INTEL_PLATFORM_INFO , &msrval) )return ; max_ratio = msrval >>8 ; min_ratio = msrval >>40 ;
@@ -566,7 +566,7 @@ static void intel_log_freq(const struct cpuinfo_x86 *c)*/ if ( min_ratio > max_ratio )SWAP (min_ratio, max_ratio); -if (rdmsr_safe (MSR_FSB_FREQ , msrval) || +if (rdmsr_safe (MSR_FSB_FREQ , &msrval) || (msrval &=7 ) >=ARRAY_SIZE (core_factors) )return ; factor = core_factors[msrval];
@@ -584,7 +584,7 @@ static void intel_log_freq(const struct cpuinfo_x86 *c)break ;case 0xf : -if (rdmsr_safe (MSR_IA32_EBC_FREQUENCY_ID , msrval) ) +if (rdmsr_safe (MSR_IA32_EBC_FREQUENCY_ID , &msrval) )return ; max_ratio = msrval >>24 ; min_ratio =0 ;
xen/arch/x86/cpu/mcheck/mce_amd.c +1 -1
@@ -195,7 +195,7 @@ static void mcequirk_amd_apply(enum mcequirk_amd_flags flags)break ;case MCEQUIRK_F10_GART : -if (rdmsr_safe (MSR_AMD64_MCx_MASK (4 ), val) ==0 ) +if (rdmsr_safe (MSR_AMD64_MCx_MASK (4 ), &val) ==0 )wrmsr_safe (MSR_AMD64_MCx_MASK (4 ), val | (1 <<10 ));break ;
xen/arch/x86/cpu/mcheck/mce_intel.c +3 -3
@@ -726,7 +726,7 @@ static bool intel_enable_lmce(void)* MSR_IA32_MCG_EXT_CTL.LMCE_EN. */ -if (rdmsr_safe (MSR_IA32_FEATURE_CONTROL , msr_content) ) +if (rdmsr_safe (MSR_IA32_FEATURE_CONTROL , &msr_content) )return false; if ( (msr_content & IA32_FEATURE_CONTROL_LOCK) &&
@@ -879,14 +879,14 @@ static void intel_init_ppin(const struct cpuinfo_x86 *c)case 0x8f :/* Sapphire Rapids X */ if ( (c != &boot_cpu_data && !ppin_msr) || -rdmsr_safe (MSR_PPIN_CTL , val) ) +rdmsr_safe (MSR_PPIN_CTL , &val) )return ;/* If PPIN is disabled, but not locked, try to enable. */ if ( !(val & (PPIN_ENABLE |PPIN_LOCKOUT )) ) {wrmsr_safe (MSR_PPIN_CTL , val |PPIN_ENABLE ); -rdmsr_safe (MSR_PPIN_CTL , val); +rdmsr_safe (MSR_PPIN_CTL , &val); }if ( !(val &PPIN_ENABLE ) )
xen/arch/x86/hvm/ioreq.c +1 -1
@@ -292,7 +292,7 @@ bool arch_ioreq_server_get_type_addr(const struct domain *d, {uint64_t msr_val; -if ( !rdmsr_safe (MSR_AMD64_NB_CFG , msr_val) && +if ( !rdmsr_safe (MSR_AMD64_NB_CFG , &msr_val) && (msr_val & (1ULL <<AMD64_NB_CFG_CF8_EXT_ENABLE_BIT )) ) *addr |=CF8_ADDR_HI (cf8); }
xen/arch/x86/hvm/svm/svm.c +8 -8
@@ -1082,8 +1082,8 @@ static void svm_host_osvw_init(void) {uint64_t len, status; -if (rdmsr_safe (MSR_AMD_OSVW_ID_LENGTH , len) || -rdmsr_safe (MSR_AMD_OSVW_STATUS , status) ) +if (rdmsr_safe (MSR_AMD_OSVW_ID_LENGTH , &len) || +rdmsr_safe (MSR_AMD_OSVW_STATUS , &status) ) len = status =0 ;if ( len < osvw_length )
@@ -1481,7 +1481,7 @@ static void svm_init_erratum_383(const struct cpuinfo_x86 *c)return ;/* use safe methods to be compatible with nested virtualization */ -if (rdmsr_safe (MSR_AMD64_DC_CFG , msr_content) ==0 && +if (rdmsr_safe (MSR_AMD64_DC_CFG , &msr_content) ==0 &&wrmsr_safe (MSR_AMD64_DC_CFG , msr_content | (1ULL <<47 )) ==0 ) amd_erratum383_found =1 ;else
@@ -1785,7 +1785,7 @@ static int cf_check svm_msr_read_intercept(break ;case MSR_F10_BU_CFG : -if ( !rdmsr_safe (msr, *msr_content) ) +if ( !rdmsr_safe (msr, msr_content) )break ;if ( boot_cpu_data.x86 ==0xf )
@@ -1804,7 +1804,7 @@ static int cf_check svm_msr_read_intercept( goto gpf;case MSR_F10_BU_CFG2 : -if (rdmsr_safe (msr, *msr_content) ) +if (rdmsr_safe (msr, msr_content) ) goto gpf;break ;
@@ -1881,7 +1881,7 @@ static int cf_check svm_msr_read_intercept(break ;default : -if ( d->arch.msr_relaxed && !rdmsr_safe (msr, tmp) ) +if ( d->arch.msr_relaxed && !rdmsr_safe (msr, &tmp) ) { *msr_content =0 ;break ;
@@ -2047,7 +2047,7 @@ static int cf_check svm_msr_write_intercept(case MSR_F10_BU_CFG :case MSR_F10_BU_CFG2 : -if (rdmsr_safe (msr, msr_content) ) +if (rdmsr_safe (msr, &msr_content) ) goto gpf;break ;
@@ -2068,7 +2068,7 @@ static int cf_check svm_msr_write_intercept(break ;default : -if ( d->arch.msr_relaxed && !rdmsr_safe (msr, msr_content) ) +if ( d->arch.msr_relaxed && !rdmsr_safe (msr, &msr_content) )break ; gdprintk(XENLOG_WARNING,
xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -3499,7 +3499,7 @@ static int cf_check vmx_msr_read_intercept(break ; } -if ( curr->domain->arch.msr_relaxed && !rdmsr_safe (msr, tmp) ) +if ( curr->domain->arch.msr_relaxed && !rdmsr_safe (msr, &tmp) ) { *msr_content =0 ;break ;
@@ -3809,7 +3809,7 @@ static int cf_check vmx_msr_write_intercept( is_last_branch_msr(msr) )break ; -if ( v->domain->arch.msr_relaxed && !rdmsr_safe (msr, msr_content) ) +if ( v->domain->arch.msr_relaxed && !rdmsr_safe (msr, &msr_content) )break ; gdprintk(XENLOG_WARNING,
xen/arch/x86/include/asm/msr.h +21 -14
@@ -53,20 +53,27 @@ static inline void wrmsrns(uint32_t msr, uint64_t val) }/* rdmsr with exception handling */ -#define rdmsr_safe (msr,val) ({\ - int rc_; \ - uint64_t lo_, hi_; \ - __asm__ __volatile__( \ - "1: rdmsr\n2:\n" \ - ".section .fixup,\"ax\"\n" \ - "3: xorl %k0,%k0\n; xorl %k1,%k1\n" \ - " movl %5,%2\n; jmp 2b\n" \ - ".previous\n" \ - _ASM_EXTABLE(1b, 3b) \ - : "=a" (lo_), "=d" (hi_), "=&r" (rc_) \ - : "c" (msr), "2" (0), "i" (-EFAULT)); \ - val = lo_ | (hi_ << 32); \ - rc_; }) +static inline int rdmsr_safe (unsigned int msr,uint64_t *val) +{ +int rc; +uint64_t lo, hi; + + asm_inline volatile ( + "1: rdmsr\n2:\n" + ".section .fixup,\"ax\"\n" + "3: xorl %k0,%k0\n\t" + " xorl %k1,%k1\n\t" + " movl %5,%2\n\t" + " jmp 2b\n\t" + ".previous" + _ASM_EXTABLE(1b, 3b) + : "=a" (lo), "=d" (hi), "=&r" (rc) + : "c" (msr), "2" (0), "i" (-EFAULT) ); + + *val = lo | (hi <<32 ); + +return rc; +}/* wrmsr with exception handling */ static inline int wrmsr_safe (unsigned int msr,uint64_t val)
xen/arch/x86/msr.c +4 -4
@@ -191,7 +191,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)if ( !(cp->x86_vendor & (X86_VENDOR_INTEL |X86_VENDOR_AMD )) || !(boot_cpu_data.x86_vendor & (X86_VENDOR_INTEL |X86_VENDOR_AMD )) || -rdmsr_safe (MSR_AMD_PATCHLEVEL , *val) ) +rdmsr_safe (MSR_AMD_PATCHLEVEL , val) ) goto gp_fault;break ;
@@ -239,7 +239,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) goto gp_fault; *val =0 ; -if (likely (!is_cpufreq_controller (d)) ||rdmsr_safe (msr, *val) ==0 ) +if (likely (!is_cpufreq_controller (d)) ||rdmsr_safe (msr, val) ==0 )break ; goto gp_fault;
@@ -305,7 +305,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) goto gp_fault;if ( !is_hardware_domain (d) )return X86EMUL_UNHANDLEABLE ; -if (rdmsr_safe (msr, *val) ) +if (rdmsr_safe (msr, val) ) goto gp_fault;if ( msr ==MSR_K8_SYSCFG ) *val &= (SYSCFG_TOM2_FORCE_WB | SYSCFG_MTRR_TOM2_EN |
@@ -321,7 +321,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)case MSR_FAM10H_MMIO_CONF_BASE :if ( !is_hardware_domain (d) || !(cp->x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) || -rdmsr_safe (msr, *val) ) +rdmsr_safe (msr, val) ) goto gp_fault;break ;
xen/arch/x86/platform_hypercall.c +1 -1
@@ -186,7 +186,7 @@ void cf_check resource_access(void *info)if (unlikely (read_tsc) )local_irq_save (flags); - ret =rdmsr_safe (entry->idx, entry->val); + ret =rdmsr_safe (entry->idx, &entry->val);if (unlikely (read_tsc) ) {
xen/arch/x86/pv/emul-priv-op.c +9 -9
@@ -248,7 +248,7 @@ static bool pci_cfg_ok(struct domain *currd, unsigned int start, {uint64_t msr_val; -if (rdmsr_safe (MSR_AMD64_NB_CFG , msr_val) ) +if (rdmsr_safe (MSR_AMD64_NB_CFG , &msr_val) )return false;if ( msr_val & (1ULL <<AMD64_NB_CFG_CF8_EXT_ENABLE_BIT ) ) start |=CF8_ADDR_HI (currd->arch.pci_cf8);
@@ -961,7 +961,7 @@ static int cf_check read_msr(return X86EMUL_OKAY ;case MSR_IA32_MISC_ENABLE : -if (rdmsr_safe (reg, *val) ) +if (rdmsr_safe (reg, val) )break ; *val =guest_misc_enable (*val);return X86EMUL_OKAY ;
@@ -991,7 +991,7 @@ static int cf_check read_msr( }/* fall through */ default : -if ( currd->arch.msr_relaxed && !rdmsr_safe (reg, tmp) ) +if ( currd->arch.msr_relaxed && !rdmsr_safe (reg, &tmp) ) { *val =0 ;return X86EMUL_OKAY ;
@@ -1001,14 +1001,14 @@ static int cf_check read_msr(break ; normal: -if (rdmsr_safe (reg, *val) ) +if (rdmsr_safe (reg, val) )break ;return X86EMUL_OKAY ; } done:if ( ret !=X86EMUL_OKAY && !curr->arch.pv.trap_ctxt[X86_EXC_GP ].address && - (reg >>16 ) !=0x4000 && !rdmsr_safe (reg, tmp) ) + (reg >>16 ) !=0x4000 && !rdmsr_safe (reg, &tmp) ) {gprintk (XENLOG_WARNING ,"faking RDMSR 0x%08x\n" , reg); *val =0 ;
@@ -1095,7 +1095,7 @@ static int cf_check write_msr(case MSR_AMD64_NB_CFG :if ( !is_hwdom_pinned_vcpu (curr) )return X86EMUL_OKAY ; -if ( (rdmsr_safe (MSR_AMD64_NB_CFG , temp) !=0 ) || +if ( (rdmsr_safe (MSR_AMD64_NB_CFG , &temp) !=0 ) || ((val ^ temp) & ~(1ULL <<AMD64_NB_CFG_CF8_EXT_ENABLE_BIT )) ) goto invalid;if (wrmsr_safe (MSR_AMD64_NB_CFG , val) ==0 )
@@ -1108,7 +1108,7 @@ static int cf_check write_msr(break ;if ( !is_hwdom_pinned_vcpu (curr) )return X86EMUL_OKAY ; -if (rdmsr_safe (MSR_FAM10H_MMIO_CONF_BASE , temp) !=0 ) +if (rdmsr_safe (MSR_FAM10H_MMIO_CONF_BASE , &temp) !=0 )break ; if ( (pci_probe & PCI_PROBE_MASK) == PCI_PROBE_MMCONF ? temp != val :
@@ -1124,7 +1124,7 @@ static int cf_check write_msr(break ;case MSR_IA32_MISC_ENABLE : -if (rdmsr_safe (reg, temp) ) +if (rdmsr_safe (reg, &temp) )break ;if ( val !=guest_misc_enable (temp) ) goto invalid;
@@ -1171,7 +1171,7 @@ static int cf_check write_msr( }/* fall through */ default : -if ( currd->arch.msr_relaxed && !rdmsr_safe (reg, val) ) +if ( currd->arch.msr_relaxed && !rdmsr_safe (reg, &val) )return X86EMUL_OKAY ; gdprintk(XENLOG_WARNING,
xen/arch/x86/spec_ctrl.c +1 -1
@@ -714,7 +714,7 @@ static bool __init check_smt_enabled(void)*/ if ( boot_cpu_data.vendor ==X86_VENDOR_INTEL && boot_cpu_data.family !=0xf && !cpu_has_hypervisor && - !rdmsr_safe (MSR_INTEL_CORE_THREAD_COUNT , val) ) + !rdmsr_safe (MSR_INTEL_CORE_THREAD_COUNT , &val) )return (MASK_EXTR (val,MSR_CTC_CORE_MASK ) !=MASK_EXTR (val,MSR_CTC_THREAD_MASK ));
xen/arch/x86/x86_64/mmconfig-shared.c +1 -1
@@ -149,7 +149,7 @@ static const char *__init cf_check pci_mmcfg_amd_fam10h(void)return NULL ; address =MSR_FAM10H_MMIO_CONF_BASE ; -if (rdmsr_safe (address, msr_content)) +if (rdmsr_safe (address, &msr_content) )return NULL ;/* mmconfig is not enable */
xen/drivers/passthrough/vtd/iommu.c +1 -1
@@ -2286,7 +2286,7 @@ static bool __init vtd_ept_page_compatible(const struct vtd_iommu *iommu)/* EPT is not initialised yet, so we must check the capability in * the MSR explicitly rather than use cpu_has_vmx_ept_*() */ -if (rdmsr_safe (MSR_IA32_VMX_EPT_VPID_CAP , ept_cap) !=0 ) +if (rdmsr_safe (MSR_IA32_VMX_EPT_VPID_CAP , &ept_cap) !=0 )return false; return (ept_has_2mb(ept_cap) && opt_hap_2mb) <=
Signature · 1 line
2.39.5
Re: [PATCH v2 01/16] x86/msr: Implement rdmsr_safe() in C
Quoted 8 lines · Andrew Cooper
... in preparation to be able to use asm goto.
Notably this mean that the value parameter must be taken by pointer rather than by value.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
In principle
However, having looked at patch 2 first, ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 883
Quoted 7 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1@@ -879,14 +879,14 @@ static void intel_init_ppin(const struct cpuinfo_x86 *c)case 0x8f :/* Sapphire Rapids X */ if ( (c != &boot_cpu_data && !ppin_msr) || -rdmsr_safe (MSR_PPIN_CTL , val) ) +rdmsr_safe (MSR_PPIN_CTL , &val) )return ;
... with this, wouldn't we be better off using ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 889
Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1/* If PPIN is disabled, but not locked, try to enable. */ if ( !(val & (PPIN_ENABLE |PPIN_LOCKOUT )) ) {wrmsr_safe (MSR_PPIN_CTL , val |PPIN_ENABLE ); -rdmsr_safe (MSR_PPIN_CTL , val); +rdmsr_safe (MSR_PPIN_CTL , &val);
... plain rdmsr() here, thus not leaving it open to the behavioral change patch 2 comes with?
Jan
Re: [PATCH v2 01/16] x86/msr: Implement rdmsr_safe() in C
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
... in preparation to be able to use asm goto.
Notably this mean that the value parameter must be taken by pointer rather than by value.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>In principle
Reviewed-by Jan Beulich <[email protected]>
Thanks.
Quoted 11 lines
However, having looked at patch 2 first, ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 883
Quoted 7 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1@@ -879,14 +879,14 @@ static void intel_init_ppin(const struct cpuinfo_x86 *c)case 0x8f :/* Sapphire Rapids X */ if ( (c != &boot_cpu_data && !ppin_msr) || -rdmsr_safe (MSR_PPIN_CTL , val) ) +rdmsr_safe (MSR_PPIN_CTL , &val) )return ;... with this, wouldn't we be better off using ...
Quoted 8 lines
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 889
Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1/* If PPIN is disabled, but not locked, try to enable. */ if ( !(val & (PPIN_ENABLE |PPIN_LOCKOUT )) ) {wrmsr_safe (MSR_PPIN_CTL , val |PPIN_ENABLE ); -rdmsr_safe (MSR_PPIN_CTL , val); +rdmsr_safe (MSR_PPIN_CTL , &val);... plain
rdmsr()here, thus not leaving it open to the behavioral change patch 2 comes with?
Yeah, probably. At the point we've read it once, and written to it, a subsequent read is not going fail.
I'll adjust, although it will have to be a rdmsrl().
~Andrew
Re: [PATCH v2 01/16] x86/msr: Implement rdmsr_safe() in C
Quoted 31 lines · Andrew Cooper
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
... in preparation to be able to use asm goto.
Notably this mean that the value parameter must be taken by pointer rather than by value.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>In principle
Reviewed-by Jan Beulich <[email protected]>Thanks.
Quoted 17 lines
However, having looked at patch 2 first, ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 883
Quoted 7 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1@@ -879,14 +879,14 @@ static void intel_init_ppin(const struct cpuinfo_x86 *c)case 0x8f :/* Sapphire Rapids X */ if ( (c != &boot_cpu_data && !ppin_msr) || -rdmsr_safe (MSR_PPIN_CTL , val) ) +rdmsr_safe (MSR_PPIN_CTL , &val) )return ;... with this, wouldn't we be better off using ...
Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1/* If PPIN is disabled, but not locked, try to enable. */ if ( !(val & (PPIN_ENABLE |PPIN_LOCKOUT )) ) {wrmsr_safe (MSR_PPIN_CTL , val |PPIN_ENABLE ); -rdmsr_safe (MSR_PPIN_CTL , val); +rdmsr_safe (MSR_PPIN_CTL , &val);
Quoted 6 lines
Quoted 2 lines
... plain
rdmsr()here, thus not leaving it open to the behavioral change patch 2 comes with?Yeah, probably. At the point we've read it once, and written to it, a subsequent read is not going fail.
I'll adjust, although it will have to be a
rdmsrl().
No. That would introduce a bug, because this path ignores a write error too.
There isn't actually a problem even with patch 2, because of the how the logic is currently laid out.
~Andrew
Re: [PATCH v2 01/16] x86/msr: Implement rdmsr_safe() in C
Quoted 34 lines · Andrew Cooper
Quoted 33 lines · Andrew Cooper
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
... in preparation to be able to use asm goto.
Notably this mean that the value parameter must be taken by pointer rather than by value.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>In principle
Reviewed-by Jan Beulich <[email protected]>Thanks.
Quoted 19 lines
However, having looked at patch 2 first, ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 883
Quoted 7 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1@@ -879,14 +879,14 @@ static void intel_init_ppin(const struct cpuinfo_x86 *c)case 0x8f :/* Sapphire Rapids X */ if ( (c != &boot_cpu_data && !ppin_msr) || -rdmsr_safe (MSR_PPIN_CTL , val) ) +rdmsr_safe (MSR_PPIN_CTL , &val) )return ;... with this, wouldn't we be better off using ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_ppin(const struct cpuinfo_x86 *c) · line 889
Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1/* If PPIN is disabled, but not locked, try to enable. */ if ( !(val & (PPIN_ENABLE |PPIN_LOCKOUT )) ) {wrmsr_safe (MSR_PPIN_CTL , val |PPIN_ENABLE ); -rdmsr_safe (MSR_PPIN_CTL , val); +rdmsr_safe (MSR_PPIN_CTL , &val);... plain
rdmsr()here, thus not leaving it open to the behavioral change patch 2 comes with?
Quoted 7 lines
Quoted 4 lines
Yeah, probably. At the point we've read it once, and written to it, a subsequent read is not going fail.
I'll adjust, although it will have to be a
rdmsrl().No. That would introduce a bug, because this path ignores a write error too.
Why would there be a bug? Irrespective of the WRMSR, we know the earlier RDMSR worked.
Quoted 2 lines
There isn't actually a problem even with patch 2, because of the how the logic is currently laid out.
Yes, the logic would still be correct, just less obviously so.
Jan
PATCH v2 2/16 x86/msr: Rework rdmsr_safe() using asm goto()
In reply to Andrew Cooper... on capable toolchains.
This avoids needing to hold rc in a register across the RDMSR, and in most cases removes direct testing and branching based on rc, as the fault label can be rearranged to directly land on the out-of-line block.
There is a subtle difference in behaviour. The old behaviour would, on fault, still produce 0's and write to val.
The new behaviour only writes val on success, and write_msr() is the only place where this matters. Move temp out of switch() scope and initialise it to 0.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/217
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
Doing this safely does depend on getting GCC-14 into CI somewhere. Debian 13/Trixie satisfies this, as does archlinux I expect.
--- xen/arch/x86/include/asm/msr.h | 19 +++++++++++++++++++ xen/arch/x86/pv/emul-priv-op.c | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/msr.h +19 -0
@@ -55,6 +55,24 @@ static inline void wrmsrns(uint32_t msr, uint64_t val)/* rdmsr with exception handling */ static inline int rdmsr_safe (unsigned int msr,uint64_t *val) { +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +uint64_t lo, hi; + + asm_inline goto ( + "1: rdmsr\n\t" + _ASM_EXTABLE(1b, %l[fault]) + : "=a" (lo), "=d" (hi) + : "c" (msr) + : + : fault ); + + *val = lo | (hi <<32 ); + +return 0 ; + + fault: +return -EFAULT ; +#else int rc;uint64_t lo, hi;
@@ -73,6 +91,7 @@ static inline int rdmsr_safe(unsigned int msr, uint64_t *val) *val = lo | (hi <<32 );return rc; +#endif }/* wrmsr with exception handling */
xen/arch/x86/pv/emul-priv-op.c +1 -2
@@ -1027,6 +1027,7 @@ static int cf_check write_msr(struct vcpu *curr = current;const struct domain *currd = curr->domain;const struct cpu_policy *cp = currd->arch.cpu_policy; +uint64_t temp =0 ;bool vpmu_msr = false;int ret;
@@ -1040,8 +1041,6 @@ static int cf_check write_msr(switch ( reg ) { -uint64_t temp; -case MSR_FS_BASE :case MSR_GS_BASE :case MSR_SHADOW_GS_BASE :
Signature · 1 line
2.39.5
Re: [PATCH v2 02/16] x86/msr: Rework rdmsr_safe() using asm goto()
Quoted 6 lines · Andrew Cooper
... on capable toolchains.
This avoids needing to hold rc in a register across the RDMSR, and in most cases removes direct testing and branching based on rc, as the fault label can be rearranged to directly land on the out-of-line block.
Quoted 6 lines
There is a subtle difference in behaviour. The old behaviour would, on fault, still produce 0's and write to val.
The new behaviour only writes val on success, and
write_msr()is the only place where this matters. Move temp out ofswitch()scope and initialise it to 0.
But what's the motivation behind making this behavioral change? At least in the cases where the return value isn't checked, it would feel safer if we continued clearing the value. Even if in all cases where this could matter (besides the one you cover here) one can prove correctness by looking at surrounding code.
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 59
Quoted 8 lines
xen/arch/x86/include/asm/msr.h +2
-0@@ -55,6 +55,24 @@ static inline void wrmsrns(uint32_t msr, uint64_t val)/* rdmsr with exception handling */ static inline int rdmsr_safe (unsigned int msr,uint64_t *val) { +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +uint64_t lo, hi;
Could at least this line move ahead of the #ifdef, to also cover ...
Quoted 7 lines
xen/arch/x86/include/asm/msr.h +7
-0+ asm_inline goto ( + "1: rdmsr\n\t" + _ASM_EXTABLE(1b, %l[fault]) + : "=a" (lo), "=d" (hi) + : "c" (msr) + : + : fault );
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 77
Quoted 10 lines
xen/arch/x86/include/asm/msr.h +8
-0+ + *val = lo | (hi <<32 ); + +return 0 ; + + fault: +return -EFAULT ; +#else int rc;uint64_t lo, hi;
... the same being needed here?
Jan
Re: [PATCH v2 02/16] x86/msr: Rework rdmsr_safe() using asm goto()
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
... on capable toolchains.
This avoids needing to hold rc in a register across the RDMSR, and in most cases removes direct testing and branching based on rc, as the fault label can be rearranged to directly land on the out-of-line block.
There is a subtle difference in behaviour. The old behaviour would, on fault, still produce 0's and write to val.
Quoted 8 lines
Quoted 3 lines
The new behaviour only writes val on success, and
write_msr()is the only place where this matters. Move temp out ofswitch()scope and initialise it to 0.But what's the motivation behind making this behavioral change? At least in the cases where the return value isn't checked, it would feel safer if we continued clearing the value. Even if in all cases where this could matter (besides the one you cover here) one can prove correctness by looking at surrounding code.
I didn't realise I'd made a change at first, but it's a consequence of the compiler's ability to rearrange basic blocks.
It can be fixed with ...
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/include/asm/msr.h +7
-0@@ -55,6 +55,24 @@ static inline void wrmsrns(uint32_t msr, uint64_t val)/* rdmsr with exception handling */ static inline int rdmsr_safe (unsigned int msr,uint64_t *val) { +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +uint64_t lo, hi; + asm_inline goto ( + "1: rdmsr\n\t" + _ASM_EXTABLE(1b, %l[fault]) + : "=a" (lo), "=d" (hi) + : "c" (msr)
Quoted 8 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 73
Quoted 8 lines
xen/arch/x86/include/asm/msr.h +8
-0+ : + : fault ); + + *val = lo | (hi <<32 ); + +return 0 ; + + fault:
*val = 0;
here, but I don't want to do this. Because val is by pointer and generally spilled to the stack, the compiler can't optimise away the store.
I'd far rather get a real compiler error, than to have logic relying on the result of a faulting MSR read.
Quoted 5 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 77
Quoted 4 lines
xen/arch/x86/include/asm/msr.h +2
-0+return -EFAULT ; +#else int rc;uint64_t lo, hi;... the same being needed here?
Fixed.
~Andrew
Re: [PATCH v2 02/16] x86/msr: Rework rdmsr_safe() using asm goto()
Quoted 50 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
... on capable toolchains.
This avoids needing to hold rc in a register across the RDMSR, and in most cases removes direct testing and branching based on rc, as the fault label can be rearranged to directly land on the out-of-line block.
There is a subtle difference in behaviour. The old behaviour would, on fault, still produce 0's and write to val.
Quoted 8 lines
Quoted 3 lines
The new behaviour only writes val on success, and
write_msr()is the only place where this matters. Move temp out ofswitch()scope and initialise it to 0.But what's the motivation behind making this behavioral change? At least in the cases where the return value isn't checked, it would feel safer if we continued clearing the value. Even if in all cases where this could matter (besides the one you cover here) one can prove correctness by looking at surrounding code.
I didn't realise I'd made a change at first, but it's a consequence of the compiler's ability to rearrange basic blocks.
It can be fixed with ...
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/include/asm/msr.h +7
-0@@ -55,6 +55,24 @@ static inline void wrmsrns(uint32_t msr, uint64_t val)/* rdmsr with exception handling */ static inline int rdmsr_safe (unsigned int msr,uint64_t *val) { +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +uint64_t lo, hi; + asm_inline goto ( + "1: rdmsr\n\t" + _ASM_EXTABLE(1b, %l[fault]) + : "=a" (lo), "=d" (hi) + : "c" (msr)Quoted 8 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 73
Quoted 8 lines
xen/arch/x86/include/asm/msr.h +8
-0+ : + : fault ); + + *val = lo | (hi <<32 ); + +return 0 ; + + fault:*val = 0;
Quoted 2 lines
here, but I don't want to do this. Because val is by pointer and generally spilled to the stack, the compiler can't optimise away the store.
But the compiler is dealing with such indirection in inline functions just fine. I don't expect it would typically spill val to the stack. Is there anything specific here that you think would make this more likely?
Quoted 2 lines
I'd far rather get a real compiler error, than to have logic relying on the result of a faulting MSR read.
A compiler error? (Hmm, perhaps you think of uninitialized variable diagnostics. That may or may not trigger, depending on how else the caller's variable is used.)
Jan
Re: [PATCH v2 02/16] x86/msr: Rework rdmsr_safe() using asm goto()
Quoted 48 lines · Jan Beulich
Quoted 47 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
... on capable toolchains.
This avoids needing to hold rc in a register across the RDMSR, and in most cases removes direct testing and branching based on rc, as the fault label can be rearranged to directly land on the out-of-line block.
There is a subtle difference in behaviour. The old behaviour would, on fault, still produce 0's and write to val.
Quoted 8 lines
Quoted 3 lines
The new behaviour only writes val on success, and
write_msr()is the only place where this matters. Move temp out ofswitch()scope and initialise it to 0.But what's the motivation behind making this behavioral change? At least in the cases where the return value isn't checked, it would feel safer if we continued clearing the value. Even if in all cases where this could matter (besides the one you cover here) one can prove correctness by looking at surrounding code.
I didn't realise I'd made a change at first, but it's a consequence of the compiler's ability to rearrange basic blocks.
It can be fixed with ...
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/include/asm/msr.h +7
-0@@ -55,6 +55,24 @@ static inline void wrmsrns(uint32_t msr, uint64_t val)/* rdmsr with exception handling */ static inline int rdmsr_safe (unsigned int msr,uint64_t *val) { +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +uint64_t lo, hi; + asm_inline goto ( + "1: rdmsr\n\t" + _ASM_EXTABLE(1b, %l[fault]) + : "=a" (lo), "=d" (hi) + : "c" (msr)Quoted 8 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 73
Quoted 8 lines
xen/arch/x86/include/asm/msr.h +8
-0+ : + : fault ); + + *val = lo | (hi <<32 ); + +return 0 ; + + fault:*val = 0;
Quoted 5 lines
Quoted 2 lines
here, but I don't want to do this. Because val is by pointer and generally spilled to the stack, the compiler can't optimise away the store.
But the compiler is dealing with such indirection in inline functions just fine. I don't expect it would typically spill val to the stack. Is there anything specific here that you think would make this more likely?
Yes. The design of the functions they're used in. Adding this line results in:
add/remove: 0/0 grow/shrink: 7/2 up/down: 109/-36 (73) Function old new delta read_msr 1243 1307 +64 resource_access 326 341 +15 hwp_init_msrs.cold 297 308 +11 probe_cpuid_faulting 168 175 +7 svm_msr_read_intercept 1034 1039 +5 hwp_write_request 113 117 +4 hwp_init_msrs 371 374 +3 amd_log_freq 844 828 -16 guest_rdmsr 2168 2148 -20
Taking read_msr() as a concrete example, this is because it's a store into a parent functions variable, not into a local variable, and cannot be elided.
Quoted 6 lines
Quoted 3 lines
I'd far rather get a real compiler error, than to have logic relying on the result of a faulting MSR read.
A compiler error? (Hmm, perhaps you think of uninitialized variable diagnostics. That may or may not trigger, depending on how else the caller's variable is used.)
Yes I was referring to the uninitialised variable diagnostic. *_safe() are fairly rare, and we've got plenty of coverage in CI.
~Andrew
Jan Beulich
Reviewed-byRe: [PATCH v2 02/16] x86/msr: Rework rdmsr_safe() using asm goto()
Quoted 76 lines · Andrew Cooper
Quoted 48 lines · Jan Beulich
Quoted 47 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
... on capable toolchains.
This avoids needing to hold rc in a register across the RDMSR, and in most cases removes direct testing and branching based on rc, as the fault label can be rearranged to directly land on the out-of-line block.
There is a subtle difference in behaviour. The old behaviour would, on fault, still produce 0's and write to val.
Quoted 8 lines
Quoted 3 lines
The new behaviour only writes val on success, and
write_msr()is the only place where this matters. Move temp out ofswitch()scope and initialise it to 0.But what's the motivation behind making this behavioral change? At least in the cases where the return value isn't checked, it would feel safer if we continued clearing the value. Even if in all cases where this could matter (besides the one you cover here) one can prove correctness by looking at surrounding code.
I didn't realise I'd made a change at first, but it's a consequence of the compiler's ability to rearrange basic blocks.
It can be fixed with ...
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/include/asm/msr.h +7
-0@@ -55,6 +55,24 @@ static inline void wrmsrns(uint32_t msr, uint64_t val)/* rdmsr with exception handling */ static inline int rdmsr_safe (unsigned int msr,uint64_t *val) { +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +uint64_t lo, hi; + asm_inline goto ( + "1: rdmsr\n\t" + _ASM_EXTABLE(1b, %l[fault]) + : "=a" (lo), "=d" (hi) + : "c" (msr)Quoted 8 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsrns(uint32_t msr, uint64_t val) · line 73
Quoted 8 lines
xen/arch/x86/include/asm/msr.h +8
-0+ : + : fault ); + + *val = lo | (hi <<32 ); + +return 0 ; + + fault:*val = 0;
Quoted 5 lines
Quoted 2 lines
here, but I don't want to do this. Because val is by pointer and generally spilled to the stack, the compiler can't optimise away the store.
But the compiler is dealing with such indirection in inline functions just fine. I don't expect it would typically spill val to the stack. Is there anything specific here that you think would make this more likely?
Yes. The design of the functions they're used in. Adding this line results in:
add/remove: 0/0 grow/shrink: 7/2 up/down: 109/-36 (73) Function old new delta read_msr 1243 1307 +64 resource_access 326 341 +15 hwp_init_msrs.cold 297 308 +11 probe_cpuid_faulting 168 175 +7 svm_msr_read_intercept 1034 1039 +5 hwp_write_request 113 117 +4 hwp_init_msrs 371 374 +3 amd_log_freq 844 828 -16 guest_rdmsr 2168 2148 -20
Taking
read_msr()as a concrete example, this is because it's a store into a parent functions variable, not into a local variable, and cannot be elided.Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I'd far rather get a real compiler error, than to have logic relying on the result of a faulting MSR read.
A compiler error? (Hmm, perhaps you think of uninitialized variable diagnostics. That may or may not trigger, depending on how else the caller's variable is used.)
Yes I was referring to the uninitialised variable diagnostic.
*_safe()are fairly rare, and we've got plenty of coverage in CI.
Well, okay, slightly hesitantly
preferably with the paragraph in the description that I commented on slightly expanded to cover the "why" aspect.
Jan
PATCH v2 3/16 x86: Sort headers
In reply to Andrew CooperNo functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
I'm disappointed to notice that this is the second time I've done this to arch/x86/domain.c recently, and screwed it up the first time.
I think we need some tooling to help keep this in order, not that I remotely have time to look into this.
--- xen/arch/x86/acpi/cpufreq/acpi.c | 2 +- xen/arch/x86/cpu/mcheck/mce.h | 2 +- xen/arch/x86/domain.c | 4 +- xen/arch/x86/domctl.c | 44 ++++++++--------- xen/arch/x86/hvm/hvm.c | 76 +++++++++++++++--------------- xen/arch/x86/hvm/svm/svm.c | 6 +-- xen/arch/x86/hvm/svm/vmcb.c | 4 +- xen/arch/x86/hvm/vmx/vmx.c | 54 +++++++++++---------- xen/arch/x86/hvm/vmx/vvmx.c | 6 +-- xen/arch/x86/include/asm/hvm/hvm.h | 2 +- xen/arch/x86/tboot.c | 15 +++--- xen/arch/x86/xstate.c | 4 +- xen/drivers/acpi/apei/apei-base.c | 14 +++--- 13 files changed, 121 insertions(+), 112 deletions(-)
xen/arch/x86/acpi/cpufreq/acpi.c +1 -1
@@ -11,8 +11,8 @@* porting acpi-cpufreq.c from Linux 2.6.23 to Xen hypervisor */ -#include <xen/errno.h> #include <xen/delay.h> +#include <xen/errno.h> #include <xen/param.h> #include <xen/types.h>
xen/arch/x86/cpu/mcheck/mce.h +1 -1
@@ -7,8 +7,8 @@#include <xen/sched.h> #include <xen/smp.h> -#include <asm/traps.h> #include <asm/atomic.h> +#include <asm/traps.h> #include "x86_mca.h" #include "mctelem.h"
xen/arch/x86/domain.c +2 -2
@@ -24,11 +24,13 @@#include <xen/hypercall.h> #include <xen/init.h> #include <xen/iocap.h> +#include <xen/iommu.h> #include <xen/irq.h> #include <xen/kernel.h> #include <xen/lib.h> #include <xen/livepatch.h> #include <xen/multicall.h> +#include <xen/numa.h> #include <xen/paging.h> #include <xen/pci.h> #include <xen/percpu.h>
@@ -64,8 +66,6 @@#include <asm/system.h> #include <asm/traps.h> #include <asm/xstate.h> -#include <xen/iommu.h> -#include <xen/numa.h> #include <public/arch-x86/cpuid.h> #include <public/sysctl.h>
xen/arch/x86/domctl.c +23 -21
62 lines not on this page · raw message
xen/arch/x86/hvm/hvm.c +39 -37
103 lines not on this page · raw message
xen/arch/x86/hvm/svm/svm.c +3 -3
@@ -18,7 +18,6 @@#include <asm/cpufeature.h> #include <asm/current.h> #include <asm/debugreg.h> -#include <asm/idt.h> #include <asm/gdbsx.h> #include <asm/hvm/emulate.h> #include <asm/hvm/hvm.h>
@@ -29,13 +28,14 @@#include <asm/hvm/svm/svm.h> #include <asm/hvm/svm/svmdebug.h> #include <asm/hvm/svm/vmcb.h> -#include <asm/iocap.h> #include <asm/i387.h> +#include <asm/idt.h> +#include <asm/iocap.h> #include <asm/monitor.h> #include <asm/msr.h> +#include <asm/p2m.h> #include <asm/paging.h> #include <asm/processor.h> -#include <asm/p2m.h> #include <asm/x86_emulate.h> #include <asm/xenoprof.h>
xen/arch/x86/hvm/svm/vmcb.c +2 -2
@@ -14,11 +14,11 @@#include <xen/sched.h> #include <xen/softirq.h> +#include <asm/hvm/svm/svm.h> +#include <asm/hvm/svm/svmdebug.h> #include <asm/hvm/svm/vmcb.h> #include <asm/msr-index.h> #include <asm/p2m.h> -#include <asm/hvm/svm/svm.h> -#include <asm/hvm/svm/svmdebug.h> #include <asm/spec_ctrl.h> struct vmcb_struct *alloc_vmcb (void )
xen/arch/x86/hvm/vmx/vmx.c +28 -26
79 lines not on this page · raw message
xen/arch/x86/hvm/vmx/vvmx.c +3 -3
@@ -10,12 +10,12 @@#include <xen/ioreq.h> -#include <asm/mtrr.h> -#include <asm/p2m.h> +#include <asm/hvm/nestedhvm.h> #include <asm/hvm/support.h> #include <asm/hvm/vmx/vmx.h> #include <asm/hvm/vmx/vvmx.h> -#include <asm/hvm/nestedhvm.h> +#include <asm/mtrr.h> +#include <asm/p2m.h> static DEFINE_PER_CPU(u64 *, vvmcs_buf);
xen/arch/x86/include/asm/hvm/hvm.h +1 -1
@@ -14,8 +14,8 @@#include <asm/asm_defns.h> #include <asm/current.h> -#include <asm/x86_emulate.h> #include <asm/hvm/asid.h> +#include <asm/x86_emulate.h> struct pirq ;/* needed by pi_update_irte */
xen/arch/x86/tboot.c +8 -7
@@ -1,19 +1,20 @@ +#include <xen/acpi.h> +#include <xen/domain_page.h> #include <xen/efi.h> #include <xen/init.h> -#include <xen/types.h> +#include <xen/iommu.h> #include <xen/lib.h> #include <xen/param.h> -#include <xen/sched.h> -#include <xen/domain_page.h> -#include <xen/iommu.h> -#include <xen/acpi.h> #include <xen/pfn.h> +#include <xen/sched.h> +#include <xen/types.h> + +#include <asm/e820.h> #include <asm/fixmap.h> #include <asm/page.h> #include <asm/processor.h> -#include <asm/e820.h> -#include <asm/tboot.h> #include <asm/setup.h> +#include <asm/tboot.h> #include <asm/trampoline.h> #include <crypto/vmac.h>
xen/arch/x86/xstate.c +2 -2
@@ -10,12 +10,12 @@#include <xen/sched.h> #include <xen/xvmalloc.h> +#include <asm/asm_defns.h> #include <asm/cpu-policy.h> #include <asm/current.h> -#include <asm/processor.h> #include <asm/i387.h> +#include <asm/processor.h> #include <asm/xstate.h> -#include <asm/asm_defns.h> /* * Maximum size (in byte) of the XSAVE/XRSTOR save area required by all
xen/drivers/acpi/apei/apei-base.c +8 -6
@@ -30,15 +30,17 @@* You should have received a copy of the GNU General Public License * along with this program; If not, see <http://www.gnu.org/licenses/>. */ -#include <xen/kernel.h> -#include <xen/errno.h> + +#include <xen/cper.h> #include <xen/delay.h> -#include <xen/string.h> -#include <xen/types.h> -#include <xen/spinlock.h> +#include <xen/errno.h> #include <xen/init.h> -#include <xen/cper.h> +#include <xen/kernel.h> +#include <xen/spinlock.h> +#include <xen/string.h> +#include <asm/io.h> +#include <acpi/acpi.h> #include <acpi/apei.h>
Signature · 1 line
2.39.5
Re: [PATCH v2 03/16] x86: Sort headers
Quoted 3 lines · Andrew Cooper
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 4/16 x86/msr: Split out guest-msr.h
In reply to Andrew Cooperstruct vcpu_msrs has a very different purpose to everything else in msr.h
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/cpu/common.c | 1 + xen/arch/x86/cpuid.c | 1 + xen/arch/x86/debug.c | 1 + xen/arch/x86/domain.c | 1 + xen/arch/x86/domctl.c | 1 + xen/arch/x86/hvm/hvm.c | 1 + xen/arch/x86/hvm/svm/svm.c | 1 + xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmx.c | 1 + xen/arch/x86/hvm/vmx/vvmx.c | 1 + xen/arch/x86/include/asm/guest-msr.h | 155 +++++++++++++++++++++++++++ xen/arch/x86/include/asm/msr.h | 145 ------------------------- xen/arch/x86/msr.c | 1 + xen/arch/x86/pv/emulate.h | 1 + xen/arch/x86/x86_64/asm-offsets.c | 1 + 15 files changed, 168 insertions(+), 145 deletions(-) create mode 100644 xen/arch/x86/include/asm/guest-msr.h
xen/arch/x86/cpu/common.c +1 -0
@@ -9,6 +9,7 @@#include <asm/cpu-policy.h> #include <asm/current.h> #include <asm/debugreg.h> +#include <asm/guest-msr.h> #include <asm/idt.h> #include <asm/io.h> #include <asm/match-cpu.h>
xen/arch/x86/cpuid.c +1 -0
@@ -7,6 +7,7 @@#include <asm/cpu-policy.h> #include <asm/cpuid.h> +#include <asm/guest-msr.h> #include <asm/hvm/viridian.h> #include <asm/xstate.h>
xen/arch/x86/debug.c +1 -0
@@ -9,6 +9,7 @@#include <asm/current.h> #include <asm/debugreg.h> +#include <asm/guest-msr.h> /* * Merge new bits into dr6. 'new' is always given in positive polarity,
xen/arch/x86/domain.c +1 -0
@@ -44,6 +44,7 @@#include <asm/cpuidle.h> #include <asm/debugreg.h> #include <asm/desc.h> +#include <asm/guest-msr.h> #include <asm/hvm/hvm.h> #include <asm/hvm/nestedhvm.h> #include <asm/hvm/svm/svm.h>
xen/arch/x86/domctl.c +1 -0
@@ -25,6 +25,7 @@#include <asm/acpi.h> #include <asm/cpu-policy.h> #include <asm/gdbsx.h> +#include <asm/guest-msr.h> #include <asm/hvm/emulate.h> #include <asm/hvm/hvm.h> #include <asm/io_apic.h>
xen/arch/x86/hvm/hvm.c +1 -0
@@ -38,6 +38,7 @@#include <asm/current.h> #include <asm/debugreg.h> #include <asm/e820.h> +#include <asm/guest-msr.h> #include <asm/hap.h> #include <asm/hvm/cacheattr.h> #include <asm/hvm/emulate.h>
xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -19,6 +19,7 @@#include <asm/current.h> #include <asm/debugreg.h> #include <asm/gdbsx.h> +#include <asm/guest-msr.h> #include <asm/hvm/emulate.h> #include <asm/hvm/hvm.h> #include <asm/hvm/io.h>
xen/arch/x86/hvm/svm/vmcb.c +1 -0
@@ -14,6 +14,7 @@#include <xen/sched.h> #include <xen/softirq.h> +#include <asm/guest-msr.h> #include <asm/hvm/svm/svm.h> #include <asm/hvm/svm/svmdebug.h> #include <asm/hvm/svm/vmcb.h>
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -23,6 +23,7 @@#include <asm/debugreg.h> #include <asm/event.h> #include <asm/gdbsx.h> +#include <asm/guest-msr.h> #include <asm/hvm/emulate.h> #include <asm/hvm/hvm.h> #include <asm/hvm/monitor.h>
xen/arch/x86/hvm/vmx/vvmx.c +1 -0
@@ -10,6 +10,7 @@#include <xen/ioreq.h> +#include <asm/guest-msr.h> #include <asm/hvm/nestedhvm.h> #include <asm/hvm/support.h> #include <asm/hvm/vmx/vmx.h>
xen/arch/x86/include/asm/guest-msr.h new file +155 -0
155 lines not on this page · raw message
xen/arch/x86/include/asm/msr.h +0 -145
149 lines not on this page · raw message
xen/arch/x86/msr.c +1 -0
@@ -15,6 +15,7 @@#include <asm/amd.h> #include <asm/cpu-policy.h> #include <asm/debugreg.h> +#include <asm/guest-msr.h> #include <asm/hvm/nestedhvm.h> #include <asm/hvm/viridian.h> #include <asm/msr.h>
xen/arch/x86/pv/emulate.h +1 -0
@@ -3,6 +3,7 @@#include <xen/sched.h> +#include <asm/guest-msr.h> #include <asm/processor.h> #include <asm/x86_emulate.h>
xen/arch/x86/x86_64/asm-offsets.c +1 -0
@@ -15,6 +15,7 @@#include <asm/hardirq.h> #include <xen/multiboot.h> #include <xen/multiboot2.h> +#include <asm/guest-msr.h> #ifdef CONFIG_VIDEO # include "../boot/video.h"
Signature · 1 line
2.39.5
Re: [PATCH v2 04/16] x86/msr: Split out guest-msr.h
Quoted 5 lines · Andrew Cooper
struct
vcpu_msrshas a very different purpose to everything else inmsr.hNo functional change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 5/16 x86/msr: Split out fsgsbase.h
In reply to Andrew CooperIt is a large and complex API, but only a handful of translation units need access.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/hvm/vmx/vmx.c | 1 + xen/arch/x86/include/asm/fsgsbase.h | 114 ++++++++++++++++++++++++++++ xen/arch/x86/include/asm/msr.h | 105 ------------------------- xen/arch/x86/pv/domain.c | 1 + xen/arch/x86/pv/emul-priv-op.c | 1 + xen/arch/x86/pv/misc-hypercalls.c | 1 + xen/arch/x86/traps.c | 1 + 8 files changed, 120 insertions(+), 105 deletions(-) create mode 100644 xen/arch/x86/include/asm/fsgsbase.h
xen/arch/x86/domain.c +1 -0
@@ -44,6 +44,7 @@#include <asm/cpuidle.h> #include <asm/debugreg.h> #include <asm/desc.h> +#include <asm/fsgsbase.h> #include <asm/guest-msr.h> #include <asm/hvm/hvm.h> #include <asm/hvm/nestedhvm.h>
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -22,6 +22,7 @@#include <asm/current.h> #include <asm/debugreg.h> #include <asm/event.h> +#include <asm/fsgsbase.h> #include <asm/gdbsx.h> #include <asm/guest-msr.h> #include <asm/hvm/emulate.h>
xen/arch/x86/include/asm/fsgsbase.h new file +114 -0
114 lines not on this page · raw message
xen/arch/x86/include/asm/msr.h +0 -105
111 lines not on this page · raw message
xen/arch/x86/pv/domain.c +1 -0
@@ -12,6 +12,7 @@#include <asm/cpu-policy.h> #include <asm/cpufeature.h> +#include <asm/fsgsbase.h> #include <asm/invpcid.h> #include <asm/spec_ctrl.h> #include <asm/pv/domain.h>
xen/arch/x86/pv/emul-priv-op.c +1 -0
@@ -16,6 +16,7 @@#include <asm/amd.h> #include <asm/debugreg.h> #include <asm/endbr.h> +#include <asm/fsgsbase.h> #include <asm/hpet.h> #include <asm/mc146818rtc.h> #include <asm/pv/domain.h>
xen/arch/x86/pv/misc-hypercalls.c +1 -0
@@ -10,6 +10,7 @@#include <xen/hypercall.h> #include <asm/debugreg.h> +#include <asm/fsgsbase.h> long do_set_debugreg (int reg,unsigned long value) {
xen/arch/x86/traps.c +1 -0
@@ -33,6 +33,7 @@#include <asm/debugreg.h> #include <asm/desc.h> #include <asm/flushtlb.h> +#include <asm/fsgsbase.h> #include <asm/gdbsx.h> #include <asm/i387.h> #include <asm/io.h>
Signature · 1 line
2.39.5
Re: [PATCH v2 05/16] x86/msr: Split out fsgsbase.h
Quoted 6 lines · Andrew Cooper
It is a large and complex API, but only a handful of translation units need access.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 6/16 x86/msr: Split out tsc.h
In reply to Andrew CooperThe TSC functionality is only related to MSRs by write_tsc(), and this really does not want to be available as widely as is currently is.
asm/time.h shouldn't be including asm/msr.h, but this turns out to be sufficiently tangled that I've chosen to break it out into it's own patch.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/include/asm/msr.h | 39 ---------------------------- xen/arch/x86/include/asm/time.h | 1 + xen/arch/x86/include/asm/tsc.h | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 xen/arch/x86/include/asm/tsc.h
xen/arch/x86/include/asm/msr.h +0 -39
@@ -124,45 +124,6 @@ static inline void msr_split(struct cpu_user_regs *regs, uint64_t val) regs->rax = (uint32_t )val; } -static inline uint64_t rdtsc (void ) -{ -uint64_t low, high; - - __asm__ __volatile__("rdtsc" :"=a" (low),"=d" (high)); - -return (high <<32 ) | low; -} - -static inline uint64_t rdtsc_ordered (void ) -{ -uint64_t low, high, aux; - -/* -* The RDTSC instruction is not serializing. Make it dispatch serializing -* for the purposes here by issuing LFENCE (or MFENCE if necessary) ahead -* of it. -* -* RDTSCP, otoh, "does wait until all previous instructions have executed -* and all previous loads are globally visible" (SDM) / "forces all older -* instructions to retire before reading the timestamp counter" (APM). -*/ -alternative_io_2 ("lfence; rdtsc" , -"mfence; rdtsc" ,X86_FEATURE_MFENCE_RDTSC , -"rdtscp" ,X86_FEATURE_RDTSCP , -ASM_OUTPUT2 ("=a" (low),"=d" (high),"=c" (aux)), - /* no inputs */); - -return (high <<32 ) | low; -} - -#define __write_tsc (val) wrmsrl(MSR_IA32_TSC, val) -#define write_tsc (val) ({ \ -/* Reliable TSCs are in lockstep across all CPUs. We should \ -* never write to them. */ \ -ASSERT (!boot_cpu_has (X86_FEATURE_TSC_RELIABLE )); \ -__write_tsc (val); \ -}) -#define rdpmc (counter,low,high) \ __asm__ __volatile__("rdpmc" \ : "=a" (low), "=d" (high) \
xen/arch/x86/include/asm/time.h +1 -0
@@ -3,6 +3,7 @@#define __X86_TIME_H__#include <asm/msr.h> +#include <asm/tsc.h> typedef u64 cycles_t ;
xen/arch/x86/include/asm/tsc.h new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH v2 06/16] x86/msr: Split out tsc.h
Quoted 9 lines · Andrew Cooper
The TSC functionality is only related to MSRs by
write_tsc(), and this really does not want to be available as widely as is currently is.
asm/time.hshouldn't be includingasm/msr.h, but this turns out to be sufficiently tangled that I've chosen to break it out into it's own patch.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
with one nit:
Quoted 34 lines
xen/arch/x86/include/asm/tsc.h new file +31
-0@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_TSC_H +#define X86_TSC_H + +#include <asm/alternative.h> + +static inline uint64_t rdtsc (void ) +{ +uint64_t low, high; + + asmvolatile ("rdtsc" :"=a" (low),"=d" (high) ); + +return (high <<32 ) | low; +} + +static inline uint64_t rdtsc_ordered (void ) +{ +uint64_t low, high, aux; + +/* +* The RDTSC instruction is not serializing. Make it dispatch serializing +* for the purposes here by issuing LFENCE (or MFENCE if necessary) ahead +* of it. +* +* RDTSCP, otoh, "does wait until all previous instructions have executed +* and all previous loads are globally visible" (SDM) / "forces all older +* instructions to retire before reading the timestamp counter" (APM). +*/ + alternative_io_2("lfence; rdtsc", + "mfence; rdtsc", X86_FEATURE_MFENCE_RDTSC, + "rdtscp", X86_FEATURE_RDTSCP,
xen/arch/x86/include/asm/tsc.h line 41
Quoted 10 lines
xen/arch/x86/include/asm/tsc.h new file +10
-0+ ASM_OUTPUT2("=a" (low), "=d" (high), "=c" (aux)), + /* no inputs */); + +return (high <<32 ) | low; +} + +#define __write_tsc (val) wrmsrl(MSR_IA32_TSC, val) +#define write_tsc (val) ({ \ +/* Reliable TSCs are in lockstep across all CPUs. We should \ +* never write to them. */ \
This comment may want to become a proper Xen-style one while being moved.
Jan
Re: [PATCH v2 06/16] x86/msr: Split out tsc.h
Quoted 62 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
The TSC functionality is only related to MSRs by
write_tsc(), and this really does not want to be available as widely as is currently is.
asm/time.hshouldn't be includingasm/msr.h, but this turns out to be sufficiently tangled that I've chosen to break it out into it's own patch.No functional change.
Signed-off-by Andrew Cooper <[email protected]>Acked-by Jan Beulich <[email protected]>with one nit:
Quoted 34 lines
xen/arch/x86/include/asm/tsc.h new file +31
-0@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_TSC_H +#define X86_TSC_H + +#include <asm/alternative.h> + +static inline uint64_t rdtsc (void ) +{ +uint64_t low, high; + + asmvolatile ("rdtsc" :"=a" (low),"=d" (high) ); + +return (high <<32 ) | low; +} + +static inline uint64_t rdtsc_ordered (void ) +{ +uint64_t low, high, aux; + +/* +* The RDTSC instruction is not serializing. Make it dispatch serializing +* for the purposes here by issuing LFENCE (or MFENCE if necessary) ahead +* of it. +* +* RDTSCP, otoh, "does wait until all previous instructions have executed +* and all previous loads are globally visible" (SDM) / "forces all older +* instructions to retire before reading the timestamp counter" (APM). +*/ + alternative_io_2("lfence; rdtsc", + "mfence; rdtsc", X86_FEATURE_MFENCE_RDTSC, + "rdtscp", X86_FEATURE_RDTSCP,xen/arch/x86/include/asm/tsc.h line 41
Quoted 10 lines
xen/arch/x86/include/asm/tsc.h new file +10
-0+ ASM_OUTPUT2("=a" (low), "=d" (high), "=c" (aux)), + /* no inputs */); + +return (high <<32 ) | low; +} + +#define __write_tsc (val) wrmsrl(MSR_IA32_TSC, val) +#define write_tsc (val) ({ \ +/* Reliable TSCs are in lockstep across all CPUs. We should \ +* never write to them. */ \This comment may want to become a proper Xen-style one while being moved.
Quoted 2 lines
Jan
Does it make sense to move the comment out of the macro?
Frediano
PATCH v2 7/16 x86/msr: Move msr_{split,fold}() into asm/cpu-user-regs.h
In reply to Andrew CooperNow there is a local header, asm/cpu-user-regs.h is a far better place for these to live.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/include/asm/cpu-user-regs.h | 11 +++++++++++ xen/arch/x86/include/asm/msr.h | 11 ----------- 2 files changed, 11 insertions(+), 11 deletions(-)
xen/arch/x86/include/asm/cpu-user-regs.h +11 -0
@@ -55,4 +55,15 @@ struct cpu_user_regs */ }; +static inline uint64_t msr_fold (const struct cpu_user_regs *regs) +{ +return (regs->rdx <<32 ) | regs->eax; +} + +static inline void msr_split (struct cpu_user_regs *regs,uint64_t val) +{ + regs->rdx = val >>32 ; + regs->rax = (uint32_t )val; +} +#endif /* X86_CPU_USER_REGS_H */
xen/arch/x86/include/asm/msr.h +0 -11
@@ -113,17 +113,6 @@ static inline int wrmsr_safe(unsigned int msr, uint64_t val)return -EFAULT ; } -static inline uint64_t msr_fold (const struct cpu_user_regs *regs) -{ -return (regs->rdx <<32 ) | regs->eax; -} - -static inline void msr_split (struct cpu_user_regs *regs,uint64_t val) -{ - regs->rdx = val >>32 ; - regs->rax = (uint32_t )val; -} -#define rdpmc (counter,low,high) \ __asm__ __volatile__("rdpmc" \ : "=a" (low), "=d" (high) \
Signature · 1 line
2.39.5
Re: [PATCH v2 07/16] x86/msr: Move msr_{split,fold}() into asm/cpu-user-regs.h
Quoted 6 lines · Andrew Cooper
Now there is a local header,
asm/cpu-user-regs.his a far better place for these to live.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 8/16 x86: Clean up asm/time.h
In reply to Andrew CooperWith asm/tsc.h split out, asm/time.h shouldn't be including asm/msr.h, but it turns out that an outrageous number of files (even some headers) are pulling their dependentices transitively through asm/time.h -> asm/msr.h
Most are asm/msr{,-index}.h, but in some cases it's printk(), va_args, and even PRIxxx macros.
Give asm/time.h an SPDX tag, and strip trailing whitespace.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
Randconfig in CI has stopped finding new issues, but there's a reasonable risk that more is still lurking.
ubsan.c needing asm/processor.h isn't good but it does work with all 4 architectures right now. We really ought to have dump_execution_state() somewhere more common.
--- xen/arch/x86/acpi/cpufreq/acpi.c | 3 ++- xen/arch/x86/acpi/cpufreq/cpufreq.c | 3 +++ xen/arch/x86/cpu/mcheck/mce.h | 1 + xen/arch/x86/debug.c | 1 + xen/arch/x86/guest/hyperv/hyperv.c | 1 + xen/arch/x86/hvm/ioreq.c | 1 + xen/arch/x86/hvm/vmx/vvmx.c | 1 + xen/arch/x86/include/asm/hvm/hvm.h | 1 + xen/arch/x86/include/asm/time.h | 12 ++++++------ xen/arch/x86/machine_kexec.c | 1 + xen/arch/x86/psr.c | 2 ++ xen/arch/x86/tboot.c | 1 + xen/arch/x86/xstate.c | 1 + xen/common/efi/runtime.c | 1 + xen/common/ubsan/ubsan.c | 2 ++ xen/drivers/acpi/apei/apei-base.c | 1 + xen/drivers/acpi/tables/tbfadt.c | 2 ++ xen/drivers/acpi/utilities/utmisc.c | 3 +++ xen/lib/x86/private.h | 2 +- 19 files changed, 32 insertions(+), 8 deletions(-)
xen/arch/x86/acpi/cpufreq/acpi.c +2 -1
@@ -14,7 +14,8 @@#include <xen/delay.h> #include <xen/errno.h> #include <xen/param.h> -#include <xen/types.h> + +#include <asm/msr.h> #include <acpi/acpi.h> #include <acpi/cpufreq/cpufreq.h>
xen/arch/x86/acpi/cpufreq/cpufreq.c +3 -0
@@ -30,6 +30,9 @@#include <xen/errno.h> #include <xen/param.h> #include <xen/sched.h> + +#include <asm/msr.h> +#include <acpi/cpufreq/cpufreq.h> struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ];
xen/arch/x86/cpu/mcheck/mce.h +1 -0
@@ -8,6 +8,7 @@#include <xen/smp.h> #include <asm/atomic.h> +#include <asm/msr.h> #include <asm/traps.h> #include "x86_mca.h"
xen/arch/x86/debug.c +1 -0
@@ -10,6 +10,7 @@#include <asm/current.h> #include <asm/debugreg.h> #include <asm/guest-msr.h> +#include <asm/msr.h> /* * Merge new bits into dr6. 'new' is always given in positive polarity,
xen/arch/x86/guest/hyperv/hyperv.c +1 -0
@@ -12,6 +12,7 @@#include <asm/fixmap.h> #include <asm/guest.h> #include <asm/guest/hyperv-tlfs.h> +#include <asm/msr.h> #include <asm/processor.h> #include "private.h"
xen/arch/x86/hvm/ioreq.c +1 -0
@@ -20,6 +20,7 @@#include <asm/hvm/emulate.h> #include <asm/hvm/hvm.h> #include <asm/hvm/vmx/vmx.h> +#include <asm/msr.h> #include <public/hvm/ioreq.h> #include <public/hvm/params.h>
xen/arch/x86/hvm/vmx/vvmx.c +1 -0
@@ -15,6 +15,7 @@#include <asm/hvm/support.h> #include <asm/hvm/vmx/vmx.h> #include <asm/hvm/vmx/vvmx.h> +#include <asm/msr.h> #include <asm/mtrr.h> #include <asm/p2m.h>
xen/arch/x86/include/asm/hvm/hvm.h +1 -0
@@ -15,6 +15,7 @@#include <asm/asm_defns.h> #include <asm/current.h> #include <asm/hvm/asid.h> +#include <asm/msr-index.h> #include <asm/x86_emulate.h> struct pirq ;/* needed by pi_update_irte */
xen/arch/x86/include/asm/time.h +6 -6
@@ -1,8 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_TIME_H +#define X86_TIME_H -#ifndef __X86_TIME_H__ -#define __X86_TIME_H__ - -#include <asm/msr.h> #include <asm/tsc.h> typedef u64 cycles_t ;
@@ -38,6 +37,7 @@ uint64_t cf_check acpi_pm_tick_to_ns(uint64_t ticks);uint64_t tsc_ticks2ns (uint64_t ticks); +struct cpu_user_regs ;uint64_t pv_soft_rdtsc (const struct vcpu *v,const struct cpu_user_regs *regs);uint64_t gtime_to_gtsc (const struct domain *d,uint64_t time);uint64_t gtsc_to_gtime (const struct domain *d,uint64_t tsc);
@@ -47,7 +47,7 @@ int tsc_set_info(struct domain *d, uint32_t tsc_mode, uint64_t elapsed_nsec,void tsc_get_info (struct domain *d,uint32_t *tsc_mode,uint64_t *elapsed_nsec,uint32_t *gtsc_khz,uint32_t *incarnation); - +void force_update_vcpu_system_time (struct vcpu *v);
@@ -93,4 +93,4 @@ u64 scale_delta(u64 delta, const struct time_scale *scale);#define PIT_STATUS_NULL_COUNT (1 << 6)/* Lower bits match Timer Control Word. */ -#endif /* __X86_TIME_H__ */ +#endif /* X86_TIME_H */
xen/arch/x86/machine_kexec.c +1 -0
@@ -24,6 +24,7 @@#include <asm/hpet.h> #include <asm/idt.h> #include <asm/machine_kexec.h> +#include <asm/msr.h> #include <asm/page.h> /*
xen/arch/x86/psr.c +2 -0
@@ -18,6 +18,8 @@#include <xen/init.h> #include <xen/param.h> #include <xen/sched.h> + +#include <asm/msr.h> #include <asm/psr.h> /*
xen/arch/x86/tboot.c +1 -0
@@ -11,6 +11,7 @@#include <asm/e820.h> #include <asm/fixmap.h> +#include <asm/msr.h> #include <asm/page.h> #include <asm/processor.h> #include <asm/setup.h>
xen/arch/x86/xstate.c +1 -0
@@ -14,6 +14,7 @@#include <asm/cpu-policy.h> #include <asm/current.h> #include <asm/i387.h> +#include <asm/msr.h> #include <asm/processor.h> #include <asm/xstate.h>
xen/common/efi/runtime.c +1 -0
@@ -33,6 +33,7 @@ void efi_rs_leave(struct efi_rs_state *state);#ifndef CONFIG_ARM # include <asm/i387.h> +# include <asm/msr.h> # include <asm/xstate.h> # include <public/platform.h> #endif
xen/common/ubsan/ubsan.c +2 -0
@@ -16,6 +16,8 @@#include <xen/percpu.h> #include <xen/spinlock.h> +#include <asm/processor.h> +#define __noreturn noreturn#define pr_err (...) printk(XENLOG_ERR __VA_ARGS__)struct xen_ubsan {int in_ubsan; };
xen/drivers/acpi/apei/apei-base.c +1 -0
@@ -36,6 +36,7 @@#include <xen/errno.h> #include <xen/init.h> #include <xen/kernel.h> +#include <xen/lib.h> #include <xen/spinlock.h> #include <xen/string.h>
xen/drivers/acpi/tables/tbfadt.c +2 -0
@@ -42,6 +42,8 @@*/ #include <xen/init.h> +#include <xen/inttypes.h> +#include <acpi/acpi.h> #include <acpi/actables.h>
xen/drivers/acpi/utilities/utmisc.c +3 -0
@@ -42,6 +42,9 @@*/ #include <xen/init.h> +#include <xen/lib.h> +#include <xen/stdarg.h> +#include <acpi/acpi.h> #define _COMPONENT ACPI_UTILITIES
xen/lib/x86/private.h +1 -1
@@ -10,7 +10,7 @@#include <xen/nospec.h> #include <xen/types.h> -#include <asm/msr-index.h> +#include <asm/msr.h> #define copy_to_buffer_offset copy_to_guest_offset#define copy_from_buffer_offset copy_from_guest_offset
Signature · 1 line
2.39.5
Re: [PATCH v2 08/16] x86: Clean up asm/time.h
Quoted 4 lines · Andrew Cooper
With
asm/tsc.hsplit out,asm/time.hshouldn't be includingasm/msr.h, but it turns out that an outrageous number of files (even some headers) are pulling their dependentices transitively throughasm/time.h->asm/msr.h
Quoted 8 lines
Most are
asm/msr{,-index}.h, but in some cases it'sprintk(),va_args, and even PRIxxx macros.Give
asm/time.han SPDX tag, and strip trailing whitespace.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
preferably ...
Quoted 23 lines
xen/arch/x86/include/asm/time.h +4
-4@@ -1,8 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_TIME_H +#define X86_TIME_H -#ifndef __X86_TIME_H__ -#define __X86_TIME_H__ - -#include <asm/msr.h> #include <asm/tsc.h> typedef u64 cycles_t ;@@ -38,6 +37,7 @@ uint64_t cf_check acpi_pm_tick_to_ns(uint64_t ticks);uint64_t tsc_ticks2ns (uint64_t ticks); +struct cpu_user_regs ;uint64_t pv_soft_rdtsc (const struct vcpu *v,const struct cpu_user_regs *regs);uint64_t gtime_to_gtsc (const struct domain *d,uint64_t time);uint64_t gtsc_to_gtime (const struct domain *d,uint64_t tsc);
xen/arch/x86/include/asm/time.h int tsc_set_info(struct domain *d, uint32_t tsc_mode, uint64_t elapsed_nsec, · line 52
Quoted 7 lines
xen/arch/x86/include/asm/time.h +1
-1@@ -47,7 +47,7 @@ int tsc_set_info(struct domain *d, uint32_t tsc_mode, uint64_t elapsed_nsec,void tsc_get_info (struct domain *d,uint32_t *tsc_mode,uint64_t *elapsed_nsec,uint32_t *gtsc_khz,uint32_t *incarnation); - +void force_update_vcpu_system_time (struct vcpu *v);
... with that one line dropped rather than just pruned of trailing blanks, so we no longer have a double blank line here.
Jan
PATCH v2 9/16 x86: Clean up asm/msr.h
In reply to Andrew CooperNow that content has been split out, minimise the header files as msr.h is included by many translation units. A few more TUs were pulling dependencies in transitively, so fix them up.
Give asm/time.h an SPDX tag, and strip trailing whitespace.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/acpi/cpufreq/acpi.c | 2 ++ xen/arch/x86/acpi/cpufreq/hwp.c | 4 ++++ xen/arch/x86/cpu/microcode/intel.c | 4 ++++ xen/arch/x86/include/asm/msr.h | 22 ++++++---------------- xen/arch/x86/tsx.c | 2 ++ 5 files changed, 18 insertions(+), 16 deletions(-)
xen/arch/x86/acpi/cpufreq/acpi.c +2 -0
@@ -14,8 +14,10 @@#include <xen/delay.h> #include <xen/errno.h> #include <xen/param.h> +#include <xen/smp.h> #include <asm/msr.h> +#include <asm/processor.h> #include <acpi/acpi.h> #include <acpi/cpufreq/cpufreq.h>
xen/arch/x86/acpi/cpufreq/hwp.c +4 -0
@@ -8,8 +8,12 @@#include <xen/cpumask.h> #include <xen/init.h> #include <xen/param.h> +#include <xen/smp.h> #include <xen/xmalloc.h> +#include <asm/msr.h> +#include <asm/processor.h> +#include <acpi/cpufreq/cpufreq.h> static bool __ro_after_init hwp_in_use;
xen/arch/x86/cpu/microcode/intel.c +4 -0
@@ -23,8 +23,12 @@#include <xen/err.h> #include <xen/init.h> +#include <xen/lib.h> +#include <xen/string.h> +#include <xen/xmalloc.h> #include <asm/msr.h> +#include <asm/processor.h> #include <asm/system.h> #include "private.h"
xen/arch/x86/include/asm/msr.h +6 -16
@@ -1,18 +1,13 @@ -#ifndef __ASM_MSR_H -#define __ASM_MSR_H +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_MSR_H +#define X86_MSR_H -#include "msr-index.h" - -#include <xen/types.h> #include <xen/percpu.h> #include <xen/errno.h> -#include <xen/kernel.h> - -#include <xen/lib/x86/cpu-policy.h> +#include <asm/alternative.h> #include <asm/asm_defns.h> -#include <asm/cpufeature.h> -#include <asm/processor.h> +#include <asm/msr-index.h> #define rdmsr (msr,val1,val2) \ __asm__ __volatile__("rdmsr" \
@@ -113,11 +108,6 @@ static inline int wrmsr_safe(unsigned int msr, uint64_t val)return -EFAULT ; } -#define rdpmc (counter,low,high) \ - __asm__ __volatile__("rdpmc" \ - : "=a" (low), "=d" (high) \ - : "c" (counter)) -DECLARE_PER_CPU (uint64_t, efer);static inline uint64_t read_efer (void ) {
@@ -144,4 +134,4 @@ static inline void wrmsr_tsc_aux(uint32_t val) } } -#endif /* __ASM_MSR_H */ +#endif /* X86_MSR_H */
xen/arch/x86/tsx.c +2 -0
@@ -1,7 +1,9 @@#include <xen/init.h> #include <xen/param.h> +#include <asm/microcode.h> #include <asm/msr.h> +#include <asm/processor.h> /* * Valid values:
Signature · 1 line
2.39.5
Re: [PATCH v2 09/16] x86: Clean up asm/msr.h
Quoted 9 lines · Andrew Cooper
Now that content has been split out, minimise the header files as
msr.his included by many translation units. A few more TUs were pulling dependencies in transitively, so fix them up.Give
asm/time.han SPDX tag, and strip trailing whitespace.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
I think though that ...
Quoted 27 lines
xen/arch/x86/include/asm/msr.h +5
-10@@ -1,18 +1,13 @@ -#ifndef __ASM_MSR_H -#define __ASM_MSR_H +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_MSR_H +#define X86_MSR_H -#include "msr-index.h" - -#include <xen/types.h> #include <xen/percpu.h> #include <xen/errno.h> -#include <xen/kernel.h> - -#include <xen/lib/x86/cpu-policy.h> +#include <asm/alternative.h> #include <asm/asm_defns.h> -#include <asm/cpufeature.h> -#include <asm/processor.h> +#include <asm/msr-index.h> #define rdmsr (msr,val1,val2) \ __asm__ __volatile__("rdmsr" \
xen/arch/x86/include/asm/msr.h static inline int wrmsr_safe(unsigned int msr, uint64_t val) · line 120
Quoted 8 lines
xen/arch/x86/include/asm/msr.h +0
-5@@ -113,11 +108,6 @@ static inline int wrmsr_safe(unsigned int msr, uint64_t val)return -EFAULT ; } -#define rdpmc (counter,low,high) \ - __asm__ __volatile__("rdpmc" \ - : "=a" (low), "=d" (high) \ - : "c" (counter)) -
... this removal wants mentioning in the description. I'm actually surprised this is unused - how does vPMU get away?
Jan
Re: [PATCH v2 09/16] x86: Clean up asm/msr.h
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
Now that content has been split out, minimise the header files as
msr.his included by many translation units. A few more TUs were pulling dependencies in transitively, so fix them up.Give
asm/time.han SPDX tag, and strip trailing whitespace.No functional change.
Signed-off-by Andrew Cooper <[email protected]>Acked-by Jan Beulich <[email protected]>
Thanks.
Quoted 33 lines
I think though that ...
Quoted 30 lines
xen/arch/x86/include/asm/msr.h +5
-10@@ -1,18 +1,13 @@ -#ifndef __ASM_MSR_H -#define __ASM_MSR_H +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_MSR_H +#define X86_MSR_H -#include "msr-index.h" - -#include <xen/types.h> #include <xen/percpu.h> #include <xen/errno.h> -#include <xen/kernel.h> - -#include <xen/lib/x86/cpu-policy.h> +#include <asm/alternative.h> #include <asm/asm_defns.h> -#include <asm/cpufeature.h> -#include <asm/processor.h> +#include <asm/msr-index.h> #define rdmsr (msr,val1,val2) \ __asm__ __volatile__("rdmsr" \@@ -113,11 +108,6 @@ static inline int wrmsr_safe(unsigned int msr, uint64_t val)return -EFAULT ; }
Quoted 7 lines
xen/arch/x86/include/asm/msr.h static inline int wrmsr_safe(unsigned int msr, uint64_t val) · line 120
Quoted 5 lines
xen/arch/x86/include/asm/msr.h +0
-5-#define rdpmc (counter,low,high) \ - __asm__ __volatile__("rdpmc" \ - : "=a" (low), "=d" (high) \ - : "c" (counter)) -... this removal wants mentioning in the description. I'm actually surprised this is unused - how does vPMU get away?
Kernel software can't really use RDPMC. Everything else about perf counters needs model specific knowledge, and given that model specific knowledge, transforming the indices into another address space is just a waste.
~Andrew
PATCH v2 10/16 x86/svm: Rename variable in svm_msr_write_intercept()
In reply to Andrew CooperWe're about to turn the rdmsr() macro into a real function, at which point Eclair complains that we're now shadowing an identifer.
Rename rdmsr to is_rdmsr.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/hvm/svm/svm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/svm/svm.c +4 -4
@@ -2087,14 +2087,14 @@ static int cf_check svm_msr_write_intercept(static void svm_do_msr_access (struct cpu_user_regs *regs) {struct vcpu *curr = current; -bool rdmsr = curr->arch.hvm.svm.vmcb->exitinfo1 ==0 ; -int rc, inst_len =svm_get_insn_len (curr, rdmsr ?INSTR_RDMSR - :INSTR_WRMSR ); +bool is_rdmsr = curr->arch.hvm.svm.vmcb->exitinfo1 ==0 ; +int rc, inst_len =svm_get_insn_len (curr, is_rdmsr ?INSTR_RDMSR + :INSTR_WRMSR );if ( inst_len ==0 )return ; -if ( rdmsr ) +if ( is_rdmsr ) {uint64_t msr_content =0 ;
Signature · 1 line
2.39.5
Re: [PATCH v2 10/16] x86/svm: Rename variable in svm_msr_write_intercept()
Quoted 8 lines · Andrew Cooper
We're about to turn the
rdmsr()macro into a real function, at which point Eclair complains that we're now shadowing an identifer.Rename rdmsr to
is_rdmsr.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
xen/arch/x86/hvm/svm/svm.c static int cf_check svm_msr_write_intercept( · line 2090
Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +1
-3@@ -2087,14 +2087,14 @@ static int cf_check svm_msr_write_intercept(static void svm_do_msr_access (struct cpu_user_regs *regs) {struct vcpu *curr = current; -bool rdmsr = curr->arch.hvm.svm.vmcb->exitinfo1 ==0 ; -int rc, inst_len =svm_get_insn_len (curr, rdmsr ?INSTR_RDMSR - :INSTR_WRMSR ); +bool is_rdmsr = curr->arch.hvm.svm.vmcb->exitinfo1 ==0 ;
Could also have used the shorter "read" or "rd" instead.
Jan
Quoted 11 lines
xen/arch/x86/hvm/svm/svm.c +3
-1+int rc, inst_len =svm_get_insn_len (curr, is_rdmsr ?INSTR_RDMSR + :INSTR_WRMSR );if ( inst_len ==0 )return ; -if ( rdmsr ) +if ( is_rdmsr ) {uint64_t msr_content =0 ;
PATCH v2 11/16 x86/msr: Change rdmsr() to have normal API
In reply to Andrew CooperWe want a consistent MSR API, and these want to be named rdmsr() and wrmsr(), but not with their current APIs. The current rdmsr() flavours writing to their parameters by name makes code that reads like invalid C, and is unergonomic to use in lots of cases.
Change the API, and update the callers all in one go. Where appropriate, update the write side to wrmsrns() as per the recommendation.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
I do have a more creative solution if this patch is considered to be too
large. https://gitlab.com/xen-project/hardware/xen-staging/-/commit/e13cf25d06d08481e2c138daa1fd902cf36d757b
--- xen/arch/x86/acpi/cpufreq/powernow.c | 12 ++++++----- xen/arch/x86/cpu/amd.c | 6 +++--- xen/arch/x86/cpu/common.c | 20 +++++++++++-------- xen/arch/x86/cpu/intel.c | 30 ++++++++++++++-------------- xen/arch/x86/genapic/x2apic.c | 5 +---- xen/arch/x86/hvm/vmx/vmcs.c | 30 +++++++++++++++++++++------- xen/arch/x86/include/asm/msr.h | 30 ++++++++++++++++++++++++---- xen/arch/x86/include/asm/prot-key.h | 6 +----- xen/arch/x86/tsx.c | 27 ++++++++++--------------- 9 files changed, 99 insertions(+), 67 deletions(-)
xen/arch/x86/acpi/cpufreq/powernow.c +7 -5
@@ -140,23 +140,25 @@ static int cf_check powernow_cpufreq_target(static void amd_fixup_frequency (struct xen_processor_px *px) { -u32 hi, lo, fid, did; +uint64_t val; +uint32_t fid, did;int index = px->control &0x00000007 ;const struct cpuinfo_x86 *c = ¤t_cpu_data;if ((c->x86 !=0x10 || c->x86_model >=10 ) && c->x86 !=0x11 )return ; -rdmsr (MSR_PSTATE_DEF_BASE + index, lo, hi); + val =rdmsr (MSR_PSTATE_DEF_BASE + index); +/* * MSR C001_0064+: * Bit 63: PstateEn. Read-write. If set, the P-state is valid. */ -if (!(hi & (1U <<31 ))) +if (!(val & (1UL <<63 )))return ; - fid = lo &0x3f ; - did = (lo >>6 ) &7 ; + fid = val &0x3f ; + did = (val >>6 ) &7 ;if (c->x86 ==0x10 ) px->core_frequency = (100 * (fid +16 )) >> did;else
xen/arch/x86/cpu/amd.c +3 -3
@@ -1369,9 +1369,9 @@ static void cf_check init_amd(struct cpuinfo_x86 *c) }if (cpu_has (c,X86_FEATURE_EFRO )) { -rdmsr (MSR_K8_HWCR , l, h); - l |= (1 <<27 );/* Enable read-only APERF/MPERF bit */ -wrmsr (MSR_K8_HWCR , l, h); +/* Enable read-only APERF/MPERF bit */ +wrmsrns (MSR_K8_HWCR , +rdmsr (MSR_K8_HWCR ) | (1 <<27 )); }/* Prevent TSC drift in non single-processor, single-core platforms. */
xen/arch/x86/cpu/common.c +12 -8
@@ -329,6 +329,7 @@ static inline u32 phys_pkg_id(u32 cpuid_apic, int index_msb)void __init early_cpu_init (bool verbose) {struct cpuinfo_x86 *c = &boot_cpu_data; +uint64_t val;u32 eax, ebx, ecx, edx; c->x86_cache_alignment =32 ;
@@ -412,10 +413,11 @@ void __init early_cpu_init(bool verbose) &c->x86_capability[FEATURESET_7c0], &c->x86_capability[FEATURESET_7d0]); -if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) -rdmsr (MSR_ARCH_CAPABILITIES , - c->x86_capability[FEATURESET_m10Al], - c->x86_capability[FEATURESET_m10Ah]); +if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) { + val =rdmsr (MSR_ARCH_CAPABILITIES ); + c->x86_capability[FEATURESET_m10Al] = val; + c->x86_capability[FEATURESET_m10Al] = val >>32 ; + }if (max_subleaf >=1 ) cpuid_count(7, 1, &eax, &ebx, &ecx,
@@ -467,6 +469,7 @@ void reset_cpuinfo(struct cpuinfo_x86 *c, bool keep_basic)static void generic_identify (struct cpuinfo_x86 *c) { +uint64_t val;u32 eax, ebx, ecx, edx, tmp;/* Get vendor name */
@@ -559,10 +562,11 @@ static void generic_identify(struct cpuinfo_x86 *c) &c->x86_capability[FEATURESET_Da1], &tmp, &tmp, &tmp); -if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) -rdmsr (MSR_ARCH_CAPABILITIES , - c->x86_capability[FEATURESET_m10Al], - c->x86_capability[FEATURESET_m10Ah]); +if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) { + val =rdmsr (MSR_ARCH_CAPABILITIES ); + c->x86_capability[FEATURESET_m10Al] = val; + c->x86_capability[FEATURESET_m10Al] = val >>32 ; + } }/*
xen/arch/x86/cpu/intel.c +15 -15
@@ -23,17 +23,17 @@ static uint32_t __ro_after_init mcu_opt_ctrl_val;void update_mcu_opt_ctrl (void ) { -uint32_t mask = mcu_opt_ctrl_mask, lo, hi; +uint32_t mask = mcu_opt_ctrl_mask, val;if ( !mask )return ; -rdmsr (MSR_MCU_OPT_CTRL , lo, hi); + val =rdmsr (MSR_MCU_OPT_CTRL ); - lo &= ~mask; - lo |= mcu_opt_ctrl_val; + val &= ~mask; + val |= mcu_opt_ctrl_val; -wrmsr (MSR_MCU_OPT_CTRL , lo, hi); +wrmsrns (MSR_MCU_OPT_CTRL , val); }void __init set_in_mcu_opt_ctrl (uint32_t mask,uint32_t val)
@@ -51,17 +51,17 @@ static uint32_t __ro_after_init pb_opt_ctrl_val;void update_pb_opt_ctrl (void ) { -uint32_t mask = pb_opt_ctrl_mask, lo, hi; +uint32_t mask = pb_opt_ctrl_mask, val;if ( !mask )return ; -rdmsr (MSR_PB_OPT_CTRL , lo, hi); + val =rdmsr (MSR_PB_OPT_CTRL ); - lo &= ~mask; - lo |= pb_opt_ctrl_val; + val &= ~mask; + val |= pb_opt_ctrl_val; -wrmsr (MSR_PB_OPT_CTRL , lo, hi); +wrmsrns (MSR_PB_OPT_CTRL , val); }void __init set_in_pb_opt_ctrl (uint32_t mask,uint32_t val)
@@ -456,15 +456,15 @@ static void __init probe_mwait_errata(void)*/ static void Intel_errata_workarounds (struct cpuinfo_x86 *c) { -unsigned long lo, hi; +uint64_t val;if ((c->x86 ==15 ) && (c->x86_model ==1 ) && (c->x86_mask ==1 )) { -rdmsr (MSR_IA32_MISC_ENABLE , lo, hi); -if ((lo & (1 <<9 )) ==0 ) { + val =rdmsr (MSR_IA32_MISC_ENABLE ); +if ((val & (1 <<9 )) ==0 ) {printk (KERN_INFO "CPU: C0 stepping P4 Xeon detected.\n" );printk (KERN_INFO "CPU: Disabling hardware prefetching (Errata 037)\n" ); - lo |= (1 <<9 );/* Disable hw prefetching */ -wrmsr (MSR_IA32_MISC_ENABLE , lo, hi); + val |= (1 <<9 );/* Disable hw prefetching */ +wrmsrns (MSR_IA32_MISC_ENABLE , val); } }
xen/arch/x86/genapic/x2apic.c +1 -4
@@ -268,14 +268,11 @@ const struct genapic *__init apic_x2apic_probe(void)void __init check_x2apic_preenabled (void ) { -u32 lo, hi; -if ( !cpu_has_x2apic )return ;/* Check whether x2apic mode was already enabled by the BIOS. */ -rdmsr (MSR_APIC_BASE , lo, hi); -if ( lo &APIC_BASE_EXTD ) +if (rdmsr (MSR_APIC_BASE ) &APIC_BASE_EXTD ) {printk ("x2APIC mode is already enabled by BIOS.\n" ); x2apic_enabled =1 ;
xen/arch/x86/hvm/vmx/vmcs.c +23 -7
@@ -207,9 +207,13 @@ static void __init vmx_display_features(void)static u32 adjust_vmx_controls (const char *name,u32 ctl_min,u32 ctl_opt,u32 msr,bool *mismatch) { +uint64_t val;u32 vmx_msr_low, vmx_msr_high, ctl = ctl_min | ctl_opt; -rdmsr (msr, vmx_msr_low, vmx_msr_high); + val =rdmsr (msr); + + vmx_msr_low = val; + vmx_msr_high = val >>32 ; ctl &= vmx_msr_high;/* bit == 0 in high word ==> must be zero */ ctl |= vmx_msr_low;/* bit == 1 in low word ==> must be one */
@@ -258,10 +262,13 @@ static int vmx_init_vmcs_config(bool bsp) {u32 vmx_basic_msr_low, vmx_basic_msr_high, min, opt;struct vmx_caps caps = {}; -u64 _vmx_misc_cap =0 ; +uint64_t _vmx_misc_cap =0 , val;bool mismatch = false; -rdmsr (MSR_IA32_VMX_BASIC , vmx_basic_msr_low, vmx_basic_msr_high); + val =rdmsr (MSR_IA32_VMX_BASIC ); + + vmx_basic_msr_low = val; + vmx_basic_msr_high = val >>32 ; min = (PIN_BASED_EXT_INTR_MASK |PIN_BASED_NMI_EXITING );
@@ -366,7 +373,10 @@ static int vmx_init_vmcs_config(bool bsp)if ( caps.secondary_exec_control & (SECONDARY_EXEC_ENABLE_EPT |SECONDARY_EXEC_ENABLE_VPID ) ) { -rdmsr (MSR_IA32_VMX_EPT_VPID_CAP , caps.ept, caps.vpid); + val =rdmsr (MSR_IA32_VMX_EPT_VPID_CAP ); + + caps.ept = val; + caps.vpid = val >>32 ;if ( !opt_ept_ad ) caps.ept &= ~VMX_EPT_AD_BIT ;
@@ -408,9 +418,15 @@ static int vmx_init_vmcs_config(bool bsp)* We check VMX_BASIC_MSR[55] to correctly handle default controls. */ uint32_t must_be_one, must_be_zero, msr =MSR_IA32_VMX_PROCBASED_CTLS ; +if ( vmx_basic_msr_high & (VMX_BASIC_DEFAULT1_ZERO >>32 ) ) msr =MSR_IA32_VMX_TRUE_PROCBASED_CTLS ; -rdmsr (msr, must_be_one, must_be_zero); + + val =rdmsr (msr); + + must_be_one = val; + must_be_zero = val >>32 ; +if ( must_be_one & (CPU_BASED_INVLPG_EXITING |CPU_BASED_CR3_LOAD_EXITING |CPU_BASED_CR3_STORE_EXITING ) )
@@ -699,7 +715,7 @@ void cf_check vmx_cpu_dead(unsigned int cpu)static int _vmx_cpu_up (bool bsp) { -u32 eax, edx; +u32 eax;int rc, bios_locked, cpu =smp_processor_id ();u64 cr0, vmx_cr0_fixed0, vmx_cr0_fixed1;
@@ -719,7 +735,7 @@ static int _vmx_cpu_up(bool bsp)return -EINVAL ; } -rdmsr (MSR_IA32_FEATURE_CONTROL , eax, edx); + eax =rdmsr (MSR_IA32_FEATURE_CONTROL ); bios_locked = !!(eax &IA32_FEATURE_CONTROL_LOCK );if ( bios_locked )
xen/arch/x86/include/asm/msr.h +26 -4
@@ -9,10 +9,32 @@#include <asm/asm_defns.h> #include <asm/msr-index.h> -#define rdmsr (msr,val1,val2) \ - __asm__ __volatile__("rdmsr" \ - : "=a" (val1), "=d" (val2) \ - : "c" (msr)) +/* +* MSR APIs. Most logic is expected to use: +* +* uint64_t foo = rdmsr(MSR_BAR); +* wrmsrns(MSR_BAR, foo); +* +* In addition, *_safe() wrappers exist to cope gracefully with a #GP. +* +* +* All legacy forms are to be phased out: +* +* rdmsrl(MSR_FOO, val); +* wrmsr(MSR_FOO, lo, hi); +* wrmsrl(MSR_FOO, val); +*/ + +static inline uint64_t rdmsr (unsigned int msr) +{ +unsigned long lo, hi; + + asmvolatile ("rdmsr" + :"=a" (lo),"=d" (hi) + :"c" (msr) ); + +return (hi <<32 ) | lo; +}#define rdmsrl (msr,val) do { unsigned long a__,b__; \ __asm__ __volatile__("rdmsr" \
xen/arch/x86/include/asm/prot-key.h +1 -5
@@ -52,11 +52,7 @@ DECLARE_PER_CPU(uint32_t, pkrs);static inline uint32_t rdpkrs (void ) { -uint32_t pkrs, tmp; - -rdmsr (MSR_PKRS , pkrs, tmp); - -return pkrs; +return rdmsr (MSR_PKRS ); }static inline uint32_t rdpkrs_and_cache (void )
xen/arch/x86/tsx.c +11 -16
@@ -42,6 +42,8 @@ void tsx_init(void) {static bool __read_mostly once; +uint64_t val; +/* * This function is first called between microcode being loaded, and * CPUID being scanned generally. early_cpu_init() has already prepared
@@ -62,8 +64,6 @@ void tsx_init(void)* On a TAA-vulnerable or later part with at least the May 2020 * microcode mitigating SRBDS. */ -uint64_t val; -rdmsrl (MSR_MCU_OPT_CTRL , val);/*
@@ -118,8 +118,6 @@ void tsx_init(void)if ( cpu_has_tsx_force_abort ) { -uint64_t val; -/* * On an early TSX-enabled Skylake part subject to the memory * ordering erratum, with at least the March 2019 microcode.
@@ -250,18 +248,17 @@ void tsx_init(void)* controlled, we have or will set MSR_MCU_OPT_CTRL.RTM_ALLOW to * let TSX_CTRL.RTM_DISABLE be usable. */ -uint32_t hi, lo; -rdmsr (MSR_TSX_CTRL , lo, hi); + val =rdmsr (MSR_TSX_CTRL );/* Check bottom bit only. Higher bits are various sentinels. */ rtm_disabled = !(opt_tsx &1 ); - lo &= ~(TSX_CTRL_RTM_DISABLE |TSX_CTRL_CPUID_CLEAR ); + val &= ~(TSX_CTRL_RTM_DISABLE |TSX_CTRL_CPUID_CLEAR );if ( rtm_disabled ) - lo |=TSX_CTRL_RTM_DISABLE |TSX_CTRL_CPUID_CLEAR ; + val |=TSX_CTRL_RTM_DISABLE |TSX_CTRL_CPUID_CLEAR ; -wrmsr (MSR_TSX_CTRL , lo, hi); +wrmsrns (MSR_TSX_CTRL , val); }else if ( cpu_has_tsx_force_abort ) {
@@ -269,14 +266,12 @@ void tsx_init(void)* On an early TSX-enable Skylake part subject to the memory ordering * erratum, with at least the March 2019 microcode. */ -uint32_t hi, lo; - -rdmsr (MSR_TSX_FORCE_ABORT , lo, hi); + val =rdmsr (MSR_TSX_FORCE_ABORT );/* Check bottom bit only. Higher bits are various sentinels. */ rtm_disabled = !(opt_tsx &1 ); - lo &= ~(TSX_FORCE_ABORT_RTM |TSX_CPUID_CLEAR |TSX_ENABLE_RTM ); + val &= ~(TSX_FORCE_ABORT_RTM |TSX_CPUID_CLEAR |TSX_ENABLE_RTM );if ( cpu_has_rtm_always_abort ) {
@@ -291,7 +286,7 @@ void tsx_init(void)* - TSX_FORCE_ABORT.ENABLE_RTM may be used to opt in to * re-enabling RTM, at the users own risk. */ - lo |= rtm_disabled ?TSX_CPUID_CLEAR :TSX_ENABLE_RTM ; + val |= rtm_disabled ?TSX_CPUID_CLEAR :TSX_ENABLE_RTM ; }else {
@@ -304,10 +299,10 @@ void tsx_init(void)* setting TSX_FORCE_ABORT.FORCE_ABORT_RTM. */ if ( rtm_disabled ) - lo |=TSX_FORCE_ABORT_RTM ; + val |=TSX_FORCE_ABORT_RTM ; } -wrmsr (MSR_TSX_FORCE_ABORT , lo, hi); +wrmsrns (MSR_TSX_FORCE_ABORT , val); }else if ( opt_tsx >=0 ) printk_once(XENLOG_WARNING
Signature · 1 line
2.39.5
Re: [PATCH v2 11/16] x86/msr: Change rdmsr() to have normal API
Quoted 14 lines · Andrew Cooper
xen/arch/x86/cpu/common.c +1
-0@@ -329,6 +329,7 @@ static inline u32 phys_pkg_id(u32 cpuid_apic, int index_msb)void __init early_cpu_init (bool verbose) {struct cpuinfo_x86 *c = &boot_cpu_data; +uint64_t val;u32 eax, ebx, ecx, edx; c->x86_cache_alignment =32 ;@@ -412,10 +413,11 @@ void __init early_cpu_init(bool verbose) &c->x86_capability[FEATURESET_7c0],
xen/arch/x86/cpu/common.c void __init early_cpu_init(bool verbose) · line 419
Quoted 10 lines
xen/arch/x86/cpu/common.c +4
-4&c->x86_capability[FEATURESET_7d0]); -if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) -rdmsr (MSR_ARCH_CAPABILITIES , - c->x86_capability[FEATURESET_m10Al], - c->x86_capability[FEATURESET_m10Ah]); +if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) { + val =rdmsr (MSR_ARCH_CAPABILITIES ); + c->x86_capability[FEATURESET_m10Al] = val; + c->x86_capability[FEATURESET_m10Al] = val >>32 ;
This is a typo. Fixed locally.
~Andrew
Re: [PATCH v2 11/16] x86/msr: Change rdmsr() to have normal API
Quoted 17 lines · Andrew Cooper
Quoted 16 lines · Andrew Cooper
xen/arch/x86/cpu/common.c +1
-0@@ -329,6 +329,7 @@ static inline u32 phys_pkg_id(u32 cpuid_apic, int index_msb)void __init early_cpu_init (bool verbose) {struct cpuinfo_x86 *c = &boot_cpu_data; +uint64_t val;u32 eax, ebx, ecx, edx; c->x86_cache_alignment =32 ;@@ -412,10 +413,11 @@ void __init early_cpu_init(bool verbose) &c->x86_capability[FEATURESET_7c0], &c->x86_capability[FEATURESET_7d0]);
Quoted 10 lines
xen/arch/x86/cpu/common.c void __init early_cpu_init(bool verbose) · line 419
Quoted 8 lines
xen/arch/x86/cpu/common.c +4
-4-if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) -rdmsr (MSR_ARCH_CAPABILITIES , - c->x86_capability[FEATURESET_m10Al], - c->x86_capability[FEATURESET_m10Ah]); +if (test_bit (X86_FEATURE_ARCH_CAPS , c->x86_capability)) { + val =rdmsr (MSR_ARCH_CAPABILITIES ); + c->x86_capability[FEATURESET_m10Al] = val; + c->x86_capability[FEATURESET_m10Al] = val >>32 ;This is a typo. Fixed locally.
Both of them, I expect.
Jan
Re: [PATCH v2 11/16] x86/msr: Change rdmsr() to have normal API
In reply to Andrew CooperQuoted 10 lines · Andrew Cooper
We want a consistent MSR API, and these want to be named
rdmsr()andwrmsr(), but not with their current APIs. The currentrdmsr()flavours writing to their parameters by name makes code that reads like invalid C, and is unergonomic to use in lots of cases.Change the API, and update the callers all in one go. Where appropriate, update the write side to
wrmsrns()as per the recommendation.No functional change.
Quoted 7 lines
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>I do have a more creative solution if this patch is considered to be too
large. https://gitlab.com/xen-project/hardware/xen-staging/-/commit/e13cf25d06d08481e2c138daa1fd902cf36d757b
I'm not concerned by the size of this patch.
Quoted 13 lines
xen/arch/x86/cpu/intel.c +1
-2@@ -23,17 +23,17 @@ static uint32_t __ro_after_init mcu_opt_ctrl_val;void update_mcu_opt_ctrl (void ) { -uint32_t mask = mcu_opt_ctrl_mask, lo, hi; +uint32_t mask = mcu_opt_ctrl_mask, val;if ( !mask )return ; -rdmsr (MSR_MCU_OPT_CTRL , lo, hi);
xen/arch/x86/cpu/intel.c static uint32_t __ro_after_init mcu_opt_ctrl_val; · line 37
Quoted 10 lines
xen/arch/x86/cpu/intel.c +4
-3+ val =rdmsr (MSR_MCU_OPT_CTRL ); - lo &= ~mask; - lo |= mcu_opt_ctrl_val; + val &= ~mask; + val |= mcu_opt_ctrl_val; -wrmsr (MSR_MCU_OPT_CTRL , lo, hi); +wrmsrns (MSR_MCU_OPT_CTRL , val); }
I don't consider it a good idea to suddenly clear the upper half of this MSR, and ...
Quoted 11 lines
xen/arch/x86/cpu/intel.c +1
-2@@ -51,17 +51,17 @@ static uint32_t __ro_after_init pb_opt_ctrl_val;void update_pb_opt_ctrl (void ) { -uint32_t mask = pb_opt_ctrl_mask, lo, hi; +uint32_t mask = pb_opt_ctrl_mask, val;if ( !mask )return ; -rdmsr (MSR_PB_OPT_CTRL , lo, hi);
xen/arch/x86/cpu/intel.c static uint32_t __ro_after_init pb_opt_ctrl_val; · line 65
Quoted 10 lines
xen/arch/x86/cpu/intel.c +4
-3+ val =rdmsr (MSR_PB_OPT_CTRL ); - lo &= ~mask; - lo |= pb_opt_ctrl_val; + val &= ~mask; + val |= pb_opt_ctrl_val; -wrmsr (MSR_PB_OPT_CTRL , lo, hi); +wrmsrns (MSR_PB_OPT_CTRL , val); }
... this one.
Quoted 10 lines
xen/arch/x86/cpu/intel.c +1
-3@@ -456,15 +456,15 @@ static void __init probe_mwait_errata(void)*/ static void Intel_errata_workarounds (struct cpuinfo_x86 *c) { -unsigned long lo, hi; +uint64_t val;if ((c->x86 ==15 ) && (c->x86_model ==1 ) && (c->x86_mask ==1 )) { -rdmsr (MSR_IA32_MISC_ENABLE , lo, hi); -if ((lo & (1 <<9 )) ==0 ) {
xen/arch/x86/cpu/intel.c static void __init probe_mwait_errata(void) · line 469
Quoted 10 lines
xen/arch/x86/cpu/intel.c +4
-2+ val =rdmsr (MSR_IA32_MISC_ENABLE ); +if ((val & (1 <<9 )) ==0 ) {printk (KERN_INFO "CPU: C0 stepping P4 Xeon detected.\n" );printk (KERN_INFO "CPU: Disabling hardware prefetching (Errata 037)\n" ); - lo |= (1 <<9 );/* Disable hw prefetching */ -wrmsr (MSR_IA32_MISC_ENABLE , lo, hi); + val |= (1 <<9 );/* Disable hw prefetching */ +wrmsrns (MSR_IA32_MISC_ENABLE , val); } }
Move val into the more narrow scope at the same time?
xen/arch/x86/cpu/intel.c void cf_check vmx_cpu_dead(unsigned int cpu) · line 718
Quoted 6 lines
xen/arch/x86/cpu/intel.c +1
-1@@ -699,7 +715,7 @@ void cf_check vmx_cpu_dead(unsigned int cpu)static int _vmx_cpu_up (bool bsp) { -u32 eax, edx; +u32 eax;
Like you do elsewhere, switch to uint32_t at the same time?
xen/arch/x86/tsx.c void tsx_init(void) · line 47
Quoted 9 lines
xen/arch/x86/tsx.c +2
-0@@ -42,6 +42,8 @@ void tsx_init(void) {static bool __read_mostly once; +uint64_t val; +/*
No real need for yet another newline, I would say.
Jan
Re: [PATCH v2 11/16] x86/msr: Change rdmsr() to have normal API
Quoted 64 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
We want a consistent MSR API, and these want to be named
rdmsr()andwrmsr(), but not with their current APIs. The currentrdmsr()flavours writing to their parameters by name makes code that reads like invalid C, and is unergonomic to use in lots of cases.Change the API, and update the callers all in one go. Where appropriate, update the write side to
wrmsrns()as per the recommendation.No functional change.
Quoted 7 lines
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>I do have a more creative solution if this patch is considered to be too
large. https://gitlab.com/xen-project/hardware/xen-staging/-/commit/e13cf25d06d08481e2c138daa1fd902cf36d757bI'm not concerned by the size of this patch.
Quoted 13 lines
xen/arch/x86/cpu/intel.c +1
-2@@ -23,17 +23,17 @@ static uint32_t __ro_after_init mcu_opt_ctrl_val;void update_mcu_opt_ctrl (void ) { -uint32_t mask = mcu_opt_ctrl_mask, lo, hi; +uint32_t mask = mcu_opt_ctrl_mask, val;if ( !mask )return ; -rdmsr (MSR_MCU_OPT_CTRL , lo, hi);xen/arch/x86/cpu/intel.c static uint32_t __ro_after_init mcu_opt_ctrl_val; · line 37
Quoted 10 lines
xen/arch/x86/cpu/intel.c +4
-3+ val =rdmsr (MSR_MCU_OPT_CTRL ); - lo &= ~mask; - lo |= mcu_opt_ctrl_val; + val &= ~mask; + val |= mcu_opt_ctrl_val; -wrmsr (MSR_MCU_OPT_CTRL , lo, hi); +wrmsrns (MSR_MCU_OPT_CTRL , val); }I don't consider it a good idea to suddenly clear the upper half of this MSR, and ...
Quoted 18 lines
xen/arch/x86/cpu/intel.c +4
-4@@ -51,17 +51,17 @@ static uint32_t __ro_after_init pb_opt_ctrl_val;void update_pb_opt_ctrl (void ) { -uint32_t mask = pb_opt_ctrl_mask, lo, hi; +uint32_t mask = pb_opt_ctrl_mask, val;if ( !mask )return ; -rdmsr (MSR_PB_OPT_CTRL , lo, hi); + val =rdmsr (MSR_PB_OPT_CTRL ); - lo &= ~mask; - lo |= pb_opt_ctrl_val; + val &= ~mask; + val |= pb_opt_ctrl_val;
Quoted 4 lines
xen/arch/x86/cpu/intel.c static uint32_t __ro_after_init pb_opt_ctrl_val; · line 65
Quoted 3 lines
xen/arch/x86/cpu/intel.c +1
-1-wrmsr (MSR_PB_OPT_CTRL , lo, hi); +wrmsrns (MSR_PB_OPT_CTRL , val); }... this one.
Yeah, both the local variables need turning into uint64_t here.
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/cpu/intel.c +1
-1@@ -456,15 +456,15 @@ static void __init probe_mwait_errata(void)*/ static void Intel_errata_workarounds (struct cpuinfo_x86 *c) { -unsigned long lo, hi; +uint64_t val;
Quoted 14 lines
xen/arch/x86/cpu/intel.c static void __init probe_mwait_errata(void) · line 469
Quoted 13 lines
xen/arch/x86/cpu/intel.c +4
-4if ((c->x86 ==15 ) && (c->x86_model ==1 ) && (c->x86_mask ==1 )) { -rdmsr (MSR_IA32_MISC_ENABLE , lo, hi); -if ((lo & (1 <<9 )) ==0 ) { + val =rdmsr (MSR_IA32_MISC_ENABLE ); +if ((val & (1 <<9 )) ==0 ) {printk (KERN_INFO "CPU: C0 stepping P4 Xeon detected.\n" );printk (KERN_INFO "CPU: Disabling hardware prefetching (Errata 037)\n" ); - lo |= (1 <<9 );/* Disable hw prefetching */ -wrmsr (MSR_IA32_MISC_ENABLE , lo, hi); + val |= (1 <<9 );/* Disable hw prefetching */ +wrmsrns (MSR_IA32_MISC_ENABLE , val); } }Move val into the more narrow scope at the same time?
No, not based on the history of this function.
Quoted 8 lines
xen/arch/x86/cpu/intel.c void cf_check vmx_cpu_dead(unsigned int cpu) · line 718
Quoted 7 lines
xen/arch/x86/cpu/intel.c +1
-1@@ -699,7 +715,7 @@ void cf_check vmx_cpu_dead(unsigned int cpu)static int _vmx_cpu_up (bool bsp) { -u32 eax, edx; +u32 eax;Like you do elsewhere, switch to
uint32_tat the same time?
Will do.
Quoted 11 lines
xen/arch/x86/tsx.c void tsx_init(void) · line 47
Quoted 10 lines
xen/arch/x86/tsx.c +2
-0@@ -42,6 +42,8 @@ void tsx_init(void) {static bool __read_mostly once; +uint64_t val; +/* No real need for yet another newline, I would say.
Where? Before? that's separation of static and not. After? that's separation of variables and code. All as we do elsewhere.
~Andrew
Re: [PATCH v2 11/16] x86/msr: Change rdmsr() to have normal API
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/tsx.c +0
-0@@ -42,6 +42,8 @@ void tsx_init(void) {static bool __read_mostly once;
Quoted 7 lines
Quoted 4 lines
xen/arch/x86/tsx.c void tsx_init(void) · line 47
Quoted 3 lines
xen/arch/x86/tsx.c +2
-0+uint64_t val; +/* No real need for yet another newline, I would say.
Where? Before? that's separation of static and not. After? that's separation of variables and code. All as we do elsewhere.
The latter we do elsewhere too, sure. But separating statics from auto variables we don't normally do, unless the set of declarations is rather large.
Jan
PATCH v2 12/16 x86/msr: Change wrmsr() to take a single parameter
In reply to Andrew CooperMirroring the cleanup to rdmsr(), do the same to wrmsr(). It now has the same API as wrmsrl(), but we'll want to drop that wrapper in due course.
It's telling that almost all remaining users pass in 0. Most are converted directly to WRMSRNS, but a few are not.
MSR_VIRT_SPEC_CTRL is unconditionally intercepted is orders of magnitude more expensive than just serialising. In disable_lapic_nmi_watchdog(), the P4 case won't run on hardware which has anything more than plain WRMSR.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/cpu/amd.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/include/asm/msr.h | 20 ++++++++++---------- xen/arch/x86/nmi.c | 18 +++++++++--------- xen/arch/x86/oprofile/op_model_athlon.c | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-)
xen/arch/x86/cpu/amd.c +1 -1
@@ -934,7 +934,7 @@ void amd_set_legacy_ssbd(bool enable)return ;if (cpu_has_virt_ssbd) -wrmsr (MSR_VIRT_SPEC_CTRL , enable ?SPEC_CTRL_SSBD :0 ,0 ); +wrmsr (MSR_VIRT_SPEC_CTRL , enable ?SPEC_CTRL_SSBD :0 );else if (amd_legacy_ssbd)core_set_legacy_ssbd (enable);else
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
@@ -754,7 +754,7 @@ static int _vmx_cpu_up(bool bsp) eax |=IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX ;if (test_bit (X86_FEATURE_SMX , &boot_cpu_data.x86_capability) ) eax |=IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX ; -wrmsr (MSR_IA32_FEATURE_CONTROL , eax,0 ); +wrmsrns (MSR_IA32_FEATURE_CONTROL , eax); }if ( (rc =vmx_init_vmcs_config (bsp)) !=0 )
xen/arch/x86/include/asm/msr.h +10 -10
@@ -15,13 +15,17 @@* uint64_t foo = rdmsr(MSR_BAR); * wrmsrns(MSR_BAR, foo); * +* and, if architectural serialisaition is necessary, or there are other +* reasons that WRMSRNS is inapplicable, then: +* +* wrmsr(MSR_BAR, foo); +* * In addition, *_safe() wrappers exist to cope gracefully with a #GP. * * * All legacy forms are to be phased out: * * rdmsrl(MSR_FOO, val); -* wrmsr(MSR_FOO, lo, hi); * wrmsrl(MSR_FOO, val); */
@@ -43,17 +47,13 @@ static inline uint64_t rdmsr(unsigned int msr) val = a__ | ((u64 )b__<<32 ); \ }while (0 ) -#define wrmsr(msr,val1,val2) \ - __asm__ __volatile__("wrmsr" \ - : /* no outputs */ \ - : "c" (msr), "a" (val1), "d" (val2)) - -static inline void wrmsrl (unsigned int msr,uint64_t val) +static inline void wrmsr(unsigned int msr, uint64_t val) { -uint32_t lo = val, hi = val >>32 ; +uint32_t lo = val, hi = val >>32 ; -wrmsr (msr, lo, hi); + asmvolatile ("wrmsr" ::"a" (lo),"d" (hi),"c" (msr) ); } +#define wrmsrl (msr, val) wrmsr(msr, val)/* Non-serialising WRMSR, when available. Falls back to a serialising WRMSR. */ static inline void wrmsrns (uint32_t msr,uint64_t val)
@@ -151,7 +151,7 @@ static inline void wrmsr_tsc_aux(uint32_t val)if ( *this_tsc_aux != val ) { -wrmsr (MSR_TSC_AUX , val,0 ); +wrmsrns (MSR_TSC_AUX , val); *this_tsc_aux = val; } }
xen/arch/x86/nmi.c +9 -9
@@ -218,16 +218,16 @@ void disable_lapic_nmi_watchdog(void)return ;switch (boot_cpu_data.x86_vendor) {case X86_VENDOR_AMD : -wrmsr (MSR_K7_EVNTSEL0 ,0 ,0 ); +wrmsrns (MSR_K7_EVNTSEL0 ,0 );break ;case X86_VENDOR_INTEL :switch (boot_cpu_data.x86) {case 6 : -wrmsr (MSR_P6_EVNTSEL (0 ),0 ,0 ); +wrmsrns (MSR_P6_EVNTSEL (0 ),0 );break ;case 15 : -wrmsr (MSR_P4_IQ_CCCR0 ,0 ,0 ); -wrmsr (MSR_P4_CRU_ESCR0 ,0 ,0 ); +wrmsr (MSR_P4_IQ_CCCR0 ,0 ); +wrmsr (MSR_P4_CRU_ESCR0 ,0 );break ; }break ;
@@ -282,7 +282,7 @@ static void clear_msr_range(unsigned int base, unsigned int n)unsigned int i;for (i =0 ; i < n; i++) -wrmsr (base+i,0 ,0 ); +wrmsrns (base + i,0 ); }static inline void write_watchdog_counter (const char *descr)
@@ -308,11 +308,11 @@ static void setup_k7_watchdog(void) | K7_EVNTSEL_USR |K7_NMI_EVENT ; -wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel);write_watchdog_counter ("K7_PERFCTR0" );apic_write (APIC_LVTPC ,APIC_DM_NMI ); evntsel |=K7_EVNTSEL_ENABLE ; -wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel); }static void setup_p6_watchdog (unsigned counter)
@@ -338,11 +338,11 @@ static void setup_p6_watchdog(unsigned counter) | P6_EVNTSEL_USR | counter; -wrmsr (MSR_P6_EVNTSEL (0 ), evntsel,0 ); +wrmsrns (MSR_P6_EVNTSEL (0 ), evntsel);write_watchdog_counter ("P6_PERFCTR0" );apic_write (APIC_LVTPC ,APIC_DM_NMI ); evntsel |=P6_EVNTSEL0_ENABLE ; -wrmsr (MSR_P6_EVNTSEL (0 ), evntsel,0 ); +wrmsrns (MSR_P6_EVNTSEL (0 ), evntsel); }static void setup_p4_watchdog (void )
xen/arch/x86/oprofile/op_model_athlon.c +1 -1
@@ -34,7 +34,7 @@#define MAX_COUNTERS FAM15H_NUM_COUNTERS#define CTR_READ (msr_content,msrs,c) do {rdmsrl(msrs->counters[(c)].addr, (msr_content));} while (0) -#define CTR_WRITE (l,msrs,c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1);} while (0) +#define CTR_WRITE (l,msrs,c) do { wrmsr(msrs->counters[(c)].addr, -l); } while (0)#define CTR_OVERFLOWED (n) (!((n) & (1ULL<<31)))#define CTRL_READ (msr_content,msrs,c) do {rdmsrl(msrs->controls[(c)].addr, (msr_content));} while (0)
Signature · 1 line
2.39.5
Re: [PATCH v2 12/16] x86/msr: Change wrmsr() to take a single parameter
xen/arch/x86/nmi.c void disable_lapic_nmi_watchdog(void) · line 221
Quoted 8 lines · Andrew Cooper
xen/arch/x86/nmi.c +1
-1@@ -218,16 +218,16 @@ void disable_lapic_nmi_watchdog(void)return ;switch (boot_cpu_data.x86_vendor) {case X86_VENDOR_AMD : -wrmsr (MSR_K7_EVNTSEL0 ,0 ,0 ); +wrmsrns (MSR_K7_EVNTSEL0 ,0 );
Since you switch to non-serializing here, ...
xen/arch/x86/nmi.c static void setup_k7_watchdog(void) · line 316
Quoted 12 lines
xen/arch/x86/nmi.c +2
-2@@ -308,11 +308,11 @@ static void setup_k7_watchdog(void) | K7_EVNTSEL_USR |K7_NMI_EVENT ; -wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel);write_watchdog_counter ("K7_PERFCTR0" );apic_write (APIC_LVTPC ,APIC_DM_NMI ); evntsel |=K7_EVNTSEL_ENABLE ; -wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel); }
... why not also here?
xen/arch/x86/oprofile/op_model_athlon.c line 37
Quoted 8 lines
xen/arch/x86/oprofile/op_model_athlon.c +1
-1@@ -34,7 +34,7 @@#define MAX_COUNTERS FAM15H_NUM_COUNTERS#define CTR_READ (msr_content,msrs,c) do {rdmsrl(msrs->counters[(c)].addr, (msr_content));} while (0) -#define CTR_WRITE (l,msrs,c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1);} while (0) +#define CTR_WRITE (l,msrs,c) do { wrmsr(msrs->counters[(c)].addr, -l); } while (0)
This isn't obviously correct (as in: no functional change): The macro is, for example, passed reset_value[] contents, which is of type unsigned long. Quite possible that the original code was wrong, though.
In any event l wants parenthesizing.
Jan
Re: [PATCH v2 12/16] x86/msr: Change wrmsr() to take a single parameter
Quoted 15 lines · Jan Beulich
xen/arch/x86/nmi.c void disable_lapic_nmi_watchdog(void) · line 221
Quoted 8 lines · Andrew Cooper
xen/arch/x86/nmi.c +1
-1@@ -218,16 +218,16 @@ void disable_lapic_nmi_watchdog(void)return ;switch (boot_cpu_data.x86_vendor) {case X86_VENDOR_AMD : -wrmsr (MSR_K7_EVNTSEL0 ,0 ,0 ); +wrmsrns (MSR_K7_EVNTSEL0 ,0 );Since you switch to non-serializing here, ...
Quoted 4 lines
xen/arch/x86/nmi.c +0
-0@@ -308,11 +308,11 @@ static void setup_k7_watchdog(void) | K7_EVNTSEL_USR |K7_NMI_EVENT ;
Quoted 9 lines
xen/arch/x86/nmi.c static void setup_k7_watchdog(void) · line 316
Quoted 8 lines
xen/arch/x86/nmi.c +2
-2-wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel);write_watchdog_counter ("K7_PERFCTR0" );apic_write (APIC_LVTPC ,APIC_DM_NMI ); evntsel |=K7_EVNTSEL_ENABLE ; -wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel); }... why not also here?
An oversight. Fixed.
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/oprofile/op_model_athlon.c +0
-0@@ -34,7 +34,7 @@#define MAX_COUNTERS FAM15H_NUM_COUNTERS
Quoted 6 lines
xen/arch/x86/oprofile/op_model_athlon.c line 37
Quoted 3 lines
xen/arch/x86/oprofile/op_model_athlon.c +1
-1#define CTR_READ (msr_content,msrs,c) do {rdmsrl(msrs->counters[(c)].addr, (msr_content));} while (0) -#define CTR_WRITE (l,msrs,c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1);} while (0) +#define CTR_WRITE (l,msrs,c) do { wrmsr(msrs->counters[(c)].addr, -l); } while (0)This isn't obviously correct (as in: no functional change): The macro is, for example, passed reset_value[] contents, which is of type unsigned long. Quite possible that the original code was wrong, though.
I'm pretty sure the change is correct.
Perf counters get programmed to -(count), as they generate an interrupt when they overflow. The K8 is the oldest BKDG I can easily access, and the counters are 48 bits wide. The same is true of Intel systems of of the same age.
Interestingly, it is the singular omission from b5103d692aa7 which converts everything including the Intel version of CTR_WRITE() of this to use wrmsrl().
While looking at the mail list archives for b5103d692aa7, I found
https://lists.xenproject.org/archives/html/xen-devel/2010-06/msg01660.html
which shows that it was Christoph's attempt to turn rdmsr() and wrmsr() into a real C functions, so I'm pretty certain that CTR_WRITE() was an omission in b5103d692aa7.
Only 15 years late on that todo...
Quoted 1 line
In any event l wants parenthesizing.
Oh, so it does. Fixed.
~Andrew
Jan Beulich
Acked-byRe: [PATCH v2 12/16] x86/msr: Change wrmsr() to take a single parameter
Quoted 52 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich
xen/arch/x86/nmi.c void disable_lapic_nmi_watchdog(void) · line 221
Quoted 8 lines · Andrew Cooper
xen/arch/x86/nmi.c +1
-1@@ -218,16 +218,16 @@ void disable_lapic_nmi_watchdog(void)return ;switch (boot_cpu_data.x86_vendor) {case X86_VENDOR_AMD : -wrmsr (MSR_K7_EVNTSEL0 ,0 ,0 ); +wrmsrns (MSR_K7_EVNTSEL0 ,0 );Since you switch to non-serializing here, ...
Quoted 4 lines
xen/arch/x86/nmi.c +0
-0@@ -308,11 +308,11 @@ static void setup_k7_watchdog(void) | K7_EVNTSEL_USR |K7_NMI_EVENT ;Quoted 9 lines
xen/arch/x86/nmi.c static void setup_k7_watchdog(void) · line 316
Quoted 8 lines
xen/arch/x86/nmi.c +2
-2-wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel);write_watchdog_counter ("K7_PERFCTR0" );apic_write (APIC_LVTPC ,APIC_DM_NMI ); evntsel |=K7_EVNTSEL_ENABLE ; -wrmsr (MSR_K7_EVNTSEL0 , evntsel,0 ); +wrmsr (MSR_K7_EVNTSEL0 , evntsel); }... why not also here?
An oversight. Fixed.
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/oprofile/op_model_athlon.c +0
-0@@ -34,7 +34,7 @@#define MAX_COUNTERS FAM15H_NUM_COUNTERSQuoted 6 lines
xen/arch/x86/oprofile/op_model_athlon.c line 37
Quoted 3 lines
xen/arch/x86/oprofile/op_model_athlon.c +1
-1#define CTR_READ (msr_content,msrs,c) do {rdmsrl(msrs->counters[(c)].addr, (msr_content));} while (0) -#define CTR_WRITE (l,msrs,c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1);} while (0) +#define CTR_WRITE (l,msrs,c) do { wrmsr(msrs->counters[(c)].addr, -l); } while (0)This isn't obviously correct (as in: no functional change): The macro is, for example, passed reset_value[] contents, which is of type unsigned long. Quite possible that the original code was wrong, though.
I'm pretty sure the change is correct.
Perf counters get programmed to -(count), as they generate an interrupt when they overflow. The K8 is the oldest BKDG I can easily access, and the counters are 48 bits wide. The same is true of Intel systems of of the same age.
Interestingly, it is the singular omission from
b5103d692aa7which converts everything including the Intel version ofCTR_WRITE()of this to usewrmsrl().
Quoted 5 lines
While looking at the mail list archives for
b5103d692aa7, I found
https://lists.xenproject.org/archives/html/xen-devel/2010-06/msg01660.html
which shows that it was Christoph's attempt to turnrdmsr()andwrmsr()into a real C functions, so I'm pretty certain thatCTR_WRITE()was an omission inb5103d692aa7.
Okay, so mainly in need of having the description point out there is actually a correction of something in here. Then:
Jan
PATCH v2 13/16 x86/msr: Use MSR_IMM when available
In reply to Andrew CooperMost MSR accesses have compile time constant indexes. By using the immediate form when available, the decoder can start issuing uops directly for the relevant MSR, rather than having to issue uops to implement "switch (%ecx)". Modern CPUs have tens of thousands of MSRs, so that's quite an if/else chain.
Create __{rdmsr,wrmsrns}_imm() helpers and use them from {rdmsr,wrmsrns}() when the compiler can determine that the msr index is known at compile time.
At the instruction level, the combined ABI is awkward. Explain our choices in detail.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
The expression wrmsrns(MSR_STAR, rdmsr(MSR_STAR)) now yields:
<test_star>:
b9 81 00 00 c0 mov $0xc0000081,%ecx
0f 32 rdmsr
48 c1 e2 20 shl $0x20,%rdx
48 09 d0 or %rdx,%rax
48 89 c2 mov %rax,%rdx
48 c1 ea 20 shr $0x20,%rdx
2e 0f 30 cs wrmsr
e9 a3 84 e8 ff jmp ffff82d040204260 <__x86_return_thunk>which is as good as we can manage. The alternative form of this looks like:
<test_star>:
b9 81 00 00 c0 mov $0xc0000081,%ecx
c4 e7 7b f6 c0 81 00 rdmsr $0xc0000081,%rax
00 c0
2e c4 e7 7a f6 c0 81 cs wrmsrns %rax,$0xc0000081
00 00 c0
e9 xx xx xx xx jmp ffff82d040204260 <__x86_return_thunk>Still TBD. We ought to update the *_safe() forms too. rdmsr_safe() is easier because the potential #GP locations line up, but there need to be two variants because of
v2:
* Let the compiler do %ecx setup
* Add RDMSR $imm too
--- xen/arch/x86/include/asm/alternative.h | 7 ++ xen/arch/x86/include/asm/msr.h | 86 ++++++++++++++++++++- xen/include/public/arch-x86/cpufeatureset.h | 1 + 3 files changed, 92 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/alternative.h +7 -0
@@ -151,6 +151,13 @@ extern void alternative_instructions(void); ALTERNATIVE(oldinstr, newinstr, feature) \ :: input ) +#define alternative_input_2 (oldinstr, newinstr1, feature1, \ + newinstr2, feature2, input...) \ + asm_inline volatile ( \ + ALTERNATIVE_2(oldinstr, newinstr1, feature1, \ + newinstr2, feature2) \ + :: input ) +/* Like alternative_input, but with a single output argument */ #define alternative_io(oldinstr, newinstr, feature, output, input...) \ asm_inline volatile ( \
xen/arch/x86/include/asm/msr.h +84 -2
105 lines not on this page · raw message
xen/include/public/arch-x86/cpufeatureset.h +1 -0
@@ -349,6 +349,7 @@ XEN_CPUFEATURE(MCDT_NO, 13*32+ 5) /*A MCDT_NO */XEN_CPUFEATURE (UC_LOCK_DIS ,13 *32 +6 )/* UC-lock disable */ /* Intel-defined CPU features, CPUID level 0x00000007:1.ecx, word 14 */ +XEN_CPUFEATURE (MSR_IMM ,14 *32 +5 )/* {RD,WR}MSR $imm32 */ /* Intel-defined CPU features, CPUID level 0x00000007:1.edx, word 15 */XEN_CPUFEATURE (AVX_VNNI_INT8 ,15 *32 +4 )/*A AVX-VNNI-INT8 Instructions */
Signature · 1 line
2.39.5
Re: [PATCH v2 13/16] x86/msr: Use MSR_IMM when available
Quoted 38 lines · Andrew Cooper
Most MSR accesses have compile time constant indexes. By using the immediate form when available, the decoder can start issuing uops directly for the relevant MSR, rather than having to issue uops to implement "switch (%ecx)". Modern CPUs have tens of thousands of MSRs, so that's quite an if/else chain.
Create __{rdmsr,wrmsrns}_imm() helpers and use them from {rdmsr,wrmsrns}() when the compiler can determine that the msr index is known at compile time.
At the instruction level, the combined ABI is awkward. Explain our choices in detail.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>The expression
wrmsrns(MSR_STAR, rdmsr(MSR_STAR))now yields:<
test_star>:b9 81 00 00 c0 mov $0xc0000081,%ecx 0f 32 rdmsr 48 c1 e2 20 shl $0x20,%rdx 48 09 d0 or %rdx,%rax 48 89 c2 mov %rax,%rdx 48 c1 ea 20 shr $0x20,%rdx 2e 0f 30 cs wrmsr e9 a3 84 e8 ff jmp ffff82d040204260 <__x86_return_thunk>which is as good as we can manage. The alternative form of this looks like:
<
test_star>:b9 81 00 00 c0 mov $0xc0000081,%ecx c4 e7 7b f6 c0 81 00 rdmsr $0xc0000081,%rax 00 c0 2e c4 e7 7a f6 c0 81 cs wrmsrns %rax,$0xc0000081 00 00 c0 e9 xx xx xx xx jmp ffff82d040204260 <__x86_return_thunk>
Quoted 3 lines
Still TBD. We ought to update the
*_safe()forms too.rdmsr_safe()is easier because the potential #GP locations line up, but there need to be two variants because of
Because of ...?
Quoted 48 lines
xen/arch/x86/include/asm/alternative.h +7
-0@@ -151,6 +151,13 @@ extern void alternative_instructions(void); ALTERNATIVE(oldinstr, newinstr, feature) \ :: input ) +#define alternative_input_2 (oldinstr, newinstr1, feature1, \ + newinstr2, feature2, input...) \ + asm_inline volatile ( \ + ALTERNATIVE_2(oldinstr, newinstr1, feature1, \ + newinstr2, feature2) \ + :: input ) +/* Like alternative_input, but with a single output argument */ #define alternative_io(oldinstr, newinstr, feature, output, input...) \ asm_inline volatile ( \xen/arch/x86/include/asm/msr.h +23
-1@@ -29,10 +29,52 @@* wrmsrl(MSR_FOO, val); */ -static inline uint64_t rdmsr (unsigned int msr) +/* +* RDMSR with a compile-time constant index, when available. Falls back to +* plain RDMSR. +*/ +static always_inline uint64_t __rdmsr_imm(uint32_t msr) +{ +uint64_t val; + +/* +* For best performance, RDMSR $msr, %r64 is recommended. For +* compatibility, we need to fall back to plain RDMSR. +* +* The combined ABI is awkward, because RDMSR $imm produces an r64, +* whereas WRMSR{,NS} produces a split edx:eax pair. +* +* Always use RDMSR $imm, %rax, because it has the most in common with the +* legacy form. When MSR_IMM isn't available, emit logic to fold %edx +* back into %rax. +* +* Let the compiler do %ecx setup. This does mean there's a useless `mov +* $imm, %ecx` in the instruction stream in the MSR_IMM case, but it means +* the compiler can de-duplicate the setup in the common case of reading +* and writing the same MSR.
xen/arch/x86/include/asm/msr.h line 64
Quoted 10 lines
xen/arch/x86/include/asm/msr.h +10
-0+*/ + alternative_io( + "rdmsr\n\t" + "shl $32, %%rdx\n\t" + "or %%rdx, %%rax\n\t", + + /* RDMSR $msr, %rax */ + ".byte 0xc4,0xe7,0x7b,0xf6,0xc0; .long %c[msr]", X86_FEATURE_MSR_IMM, + + "=a" (val),
Strictly speaking "=&a". Not that it matters much here; just to not set a bad precedent.
Quoted 27 lines
xen/arch/x86/include/asm/msr.h +23
-0@@ -55,11 +97,51 @@ static inline void wrmsr(unsigned int msr, uint64_t val) }#define wrmsrl (msr, val) wrmsr(msr, val) +/* +* Non-serialising WRMSR with a compile-time constant index, when available. +* Falls back to plain WRMSRNS, or to a serialising WRMSR. +*/ +static always_inline void __wrmsrns_imm(uint32_t msr, uint64_t val) +{ +/* +* For best performance, WRMSRNS %r64, $msr is recommended. For +* compatibility, we need to fall back to plain WRMSRNS, or to WRMSR. +* +* The combined ABI is awkward, because WRMSRNS $imm takes a single r64, +* whereas WRMSR{,NS} takes a split edx:eax pair. +* +* Always use WRMSRNS %rax, $imm, because it has the most in common with +* the legacy forms. When MSR_IMM isn't available, emit setup logic for +* %edx. +* +* Let the compiler do %ecx setup. This does mean there's a useless `mov +* $imm, %ecx` in the instruction stream in the MSR_IMM case, but it means +* the compiler can de-duplicate the setup in the common case of reading +* and writing the same MSR. +*/ + alternative_input_2(
xen/arch/x86/include/asm/msr.h static inline void wrmsr(unsigned int msr, uint64_t val) · line 132
Quoted 10 lines
xen/arch/x86/include/asm/msr.h +10
-0+"mov %%rax, %%rdx\n\t" +"shr $32, %%rdx\n\t" +".byte 0x2e; wrmsr" , + +/* CS WRMSRNS %rax, $msr */ +".byte 0x2e,0xc4,0xe7,0x7a,0xf6,0xc0; .long %c[msr]" ,X86_FEATURE_MSR_IMM , + +"mov %%rax, %%rdx\n\t" +"shr $32, %%rdx\n\t" + ".byte 0x0f,0x01,0xc6", X86_FEATURE_WRMSRNS,
Isn't this the wrong way round for hardware which has both features? The last active alternative wins, iirc.
Jan
Re: [PATCH v2 13/16] x86/msr: Use MSR_IMM when available
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Andrew Cooper
Most MSR accesses have compile time constant indexes. By using the immediate form when available, the decoder can start issuing uops directly for the relevant MSR, rather than having to issue uops to implement "switch (%ecx)". Modern CPUs have tens of thousands of MSRs, so that's quite an if/else chain.
Create __{rdmsr,wrmsrns}_imm() helpers and use them from {rdmsr,wrmsrns}() when the compiler can determine that the msr index is known at compile time.
At the instruction level, the combined ABI is awkward. Explain our choices in detail.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>The expression
wrmsrns(MSR_STAR, rdmsr(MSR_STAR))now yields:<
test_star>:b9 81 00 00 c0 mov $0xc0000081,%ecx 0f 32 rdmsr 48 c1 e2 20 shl $0x20,%rdx 48 09 d0 or %rdx,%rax 48 89 c2 mov %rax,%rdx 48 c1 ea 20 shr $0x20,%rdx 2e 0f 30 cs wrmsr e9 a3 84 e8 ff jmp ffff82d040204260 <__x86_return_thunk>which is as good as we can manage. The alternative form of this looks like:
<
test_star>:b9 81 00 00 c0 mov $0xc0000081,%ecx c4 e7 7b f6 c0 81 00 rdmsr $0xc0000081,%rax 00 c0 2e c4 e7 7a f6 c0 81 cs wrmsrns %rax,$0xc0000081 00 00 c0 e9 xx xx xx xx jmp ffff82d040204260 <__x86_return_thunk>
Quoted 4 lines
Quoted 3 lines
Still TBD. We ought to update the
*_safe()forms too.rdmsr_safe()is easier because the potential #GP locations line up, but there need to be two variants because ofBecause of ...?
Oh. I guess I didn't finish that. Because of asm goto with outputs.
The WRMSR side is harder because there are two different fault locations.
Quoted 53 lines
Quoted 53 lines
xen/arch/x86/include/asm/alternative.h +7
-0@@ -151,6 +151,13 @@ extern void alternative_instructions(void); ALTERNATIVE(oldinstr, newinstr, feature) \ :: input ) +#define alternative_input_2 (oldinstr, newinstr1, feature1, \ + newinstr2, feature2, input...) \ + asm_inline volatile ( \ + ALTERNATIVE_2(oldinstr, newinstr1, feature1, \ + newinstr2, feature2) \ + :: input ) +/* Like alternative_input, but with a single output argument */ #define alternative_io(oldinstr, newinstr, feature, output, input...) \ asm_inline volatile ( \xen/arch/x86/include/asm/msr.h +27
-1@@ -29,10 +29,52 @@* wrmsrl(MSR_FOO, val); */ -static inline uint64_t rdmsr (unsigned int msr) +/* +* RDMSR with a compile-time constant index, when available. Falls back to +* plain RDMSR. +*/ +static always_inline uint64_t __rdmsr_imm(uint32_t msr) +{ +uint64_t val; + +/* +* For best performance, RDMSR $msr, %r64 is recommended. For +* compatibility, we need to fall back to plain RDMSR. +* +* The combined ABI is awkward, because RDMSR $imm produces an r64, +* whereas WRMSR{,NS} produces a split edx:eax pair. +* +* Always use RDMSR $imm, %rax, because it has the most in common with the +* legacy form. When MSR_IMM isn't available, emit logic to fold %edx +* back into %rax. +* +* Let the compiler do %ecx setup. This does mean there's a useless `mov +* $imm, %ecx` in the instruction stream in the MSR_IMM case, but it means +* the compiler can de-duplicate the setup in the common case of reading +* and writing the same MSR. +*/ +alternative_io ( +"rdmsr\n\t" + "shl $32, %%rdx\n\t"
Quoted 8 lines
xen/arch/x86/include/asm/msr.h line 64
Quoted 6 lines
xen/arch/x86/include/asm/msr.h +6
-0+"or %%rdx, %%rax\n\t" , + +/* RDMSR $msr, %rax */ +".byte 0xc4,0xe7,0x7b,0xf6,0xc0; .long %c[msr]" ,X86_FEATURE_MSR_IMM , + + "=a" (val),Strictly speaking "=&a". Not that it matters much here; just to not set a bad precedent.
Why? A is not written to until after all inputs are consumed.
I don't see how it can qualify for being early-clobber.
Quoted 32 lines
Quoted 32 lines
xen/arch/x86/include/asm/msr.h +27
-0@@ -55,11 +97,51 @@ static inline void wrmsr(unsigned int msr, uint64_t val) }#define wrmsrl (msr, val) wrmsr(msr, val) +/* +* Non-serialising WRMSR with a compile-time constant index, when available. +* Falls back to plain WRMSRNS, or to a serialising WRMSR. +*/ +static always_inline void __wrmsrns_imm(uint32_t msr, uint64_t val) +{ +/* +* For best performance, WRMSRNS %r64, $msr is recommended. For +* compatibility, we need to fall back to plain WRMSRNS, or to WRMSR. +* +* The combined ABI is awkward, because WRMSRNS $imm takes a single r64, +* whereas WRMSR{,NS} takes a split edx:eax pair. +* +* Always use WRMSRNS %rax, $imm, because it has the most in common with +* the legacy forms. When MSR_IMM isn't available, emit setup logic for +* %edx. +* +* Let the compiler do %ecx setup. This does mean there's a useless `mov +* $imm, %ecx` in the instruction stream in the MSR_IMM case, but it means +* the compiler can de-duplicate the setup in the common case of reading +* and writing the same MSR. +*/ + alternative_input_2( + "mov %%rax, %%rdx\n\t" + "shr $32, %%rdx\n\t" + ".byte 0x2e; wrmsr", +
Quoted 8 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsr(unsigned int msr, uint64_t val) · line 132
Quoted 6 lines
xen/arch/x86/include/asm/msr.h +6
-0+/* CS WRMSRNS %rax, $msr */ +".byte 0x2e,0xc4,0xe7,0x7a,0xf6,0xc0; .long %c[msr]" ,X86_FEATURE_MSR_IMM , + +"mov %%rax, %%rdx\n\t" +"shr $32, %%rdx\n\t" + ".byte 0x0f,0x01,0xc6", X86_FEATURE_WRMSRNS,Isn't this the wrong way round for hardware which has both features? The last active alternative wins, iirc.
Bah - fooled once again by the nop optimisation. I'll reorder.
But, we really should swap the order. Especially now that you've inserted serialisation, it's an expensive waste of time patching the same site multiple times.
~Andrew
Re: [PATCH v2 13/16] x86/msr: Use MSR_IMM when available
Quoted 45 lines · Andrew Cooper
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Andrew Cooper
xen/arch/x86/include/asm/msr.h +27
-1@@ -29,10 +29,52 @@* wrmsrl(MSR_FOO, val); */ -static inline uint64_t rdmsr (unsigned int msr) +/* +* RDMSR with a compile-time constant index, when available. Falls back to +* plain RDMSR. +*/ +static always_inline uint64_t __rdmsr_imm(uint32_t msr) +{ +uint64_t val; + +/* +* For best performance, RDMSR $msr, %r64 is recommended. For +* compatibility, we need to fall back to plain RDMSR. +* +* The combined ABI is awkward, because RDMSR $imm produces an r64, +* whereas WRMSR{,NS} produces a split edx:eax pair. +* +* Always use RDMSR $imm, %rax, because it has the most in common with the +* legacy form. When MSR_IMM isn't available, emit logic to fold %edx +* back into %rax. +* +* Let the compiler do %ecx setup. This does mean there's a useless `mov +* $imm, %ecx` in the instruction stream in the MSR_IMM case, but it means +* the compiler can de-duplicate the setup in the common case of reading +* and writing the same MSR. +*/ +alternative_io ( +"rdmsr\n\t" + "shl $32, %%rdx\n\t"Quoted 8 lines
xen/arch/x86/include/asm/msr.h line 64
Quoted 6 lines
xen/arch/x86/include/asm/msr.h +6
-0+"or %%rdx, %%rax\n\t" , + +/* RDMSR $msr, %rax */ +".byte 0xc4,0xe7,0x7b,0xf6,0xc0; .long %c[msr]" ,X86_FEATURE_MSR_IMM , + + "=a" (val),Strictly speaking "=&a". Not that it matters much here; just to not set a bad precedent.
Quoted 3 lines
Why? A is not written to until after all inputs are consumed.
I don't see how it can qualify for being early-clobber.
Because the very first insn already writes to the register.
Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
xen/arch/x86/include/asm/msr.h +27
-0@@ -55,11 +97,51 @@ static inline void wrmsr(unsigned int msr, uint64_t val) }#define wrmsrl (msr, val) wrmsr(msr, val) +/* +* Non-serialising WRMSR with a compile-time constant index, when available. +* Falls back to plain WRMSRNS, or to a serialising WRMSR. +*/ +static always_inline void __wrmsrns_imm(uint32_t msr, uint64_t val) +{ +/* +* For best performance, WRMSRNS %r64, $msr is recommended. For +* compatibility, we need to fall back to plain WRMSRNS, or to WRMSR. +* +* The combined ABI is awkward, because WRMSRNS $imm takes a single r64, +* whereas WRMSR{,NS} takes a split edx:eax pair. +* +* Always use WRMSRNS %rax, $imm, because it has the most in common with +* the legacy forms. When MSR_IMM isn't available, emit setup logic for +* %edx. +* +* Let the compiler do %ecx setup. This does mean there's a useless `mov +* $imm, %ecx` in the instruction stream in the MSR_IMM case, but it means +* the compiler can de-duplicate the setup in the common case of reading +* and writing the same MSR. +*/ + alternative_input_2( + "mov %%rax, %%rdx\n\t" + "shr $32, %%rdx\n\t" + ".byte 0x2e; wrmsr", +Quoted 8 lines
xen/arch/x86/include/asm/msr.h static inline void wrmsr(unsigned int msr, uint64_t val) · line 132
Quoted 6 lines
xen/arch/x86/include/asm/msr.h +6
-0+/* CS WRMSRNS %rax, $msr */ +".byte 0x2e,0xc4,0xe7,0x7a,0xf6,0xc0; .long %c[msr]" ,X86_FEATURE_MSR_IMM , + +"mov %%rax, %%rdx\n\t" +"shr $32, %%rdx\n\t" + ".byte 0x0f,0x01,0xc6", X86_FEATURE_WRMSRNS,Isn't this the wrong way round for hardware which has both features? The last active alternative wins, iirc.
Quoted 5 lines
Bah - fooled once again by the nop optimisation. I'll reorder.
But, we really should swap the order. Especially now that you've inserted serialisation, it's an expensive waste of time patching the same site multiple times.
I'm not convinced swapping the order would be a good thing. Nor do I see how it would (easily) help here: When both features are present, we always patch twice. To avoid that, while patching one location we would need to peek ahead into the next table entry to see whether that's going to patch the same location, same (or bigger) size.
Jan
PATCH v2 14/16 x86/fsgsbase: Split out __{rd,wr}gskern() helpers
In reply to Andrew CooperRight now they're inline in {read,write}_gs_shadow(), but we're going to need to use these elsewhere to support FRED.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/include/asm/fsgsbase.h | 36 ++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-)
xen/arch/x86/include/asm/fsgsbase.h +23 -13
@@ -32,6 +32,17 @@ static inline unsigned long __rdgsbase(void)return base; } +static inline unsigned long __rdgskern (void ) +{ +unsigned long base; + + asm_inline volatile ( "swapgs\n\t" + "rdgsbase %0\n\t" + "swapgs" : "=r" (base) ); + +return base; +} +static inline void __wrfsbase (unsigned long base) { asmvolatile ("wrfsbase %0" ::"r" (base) );
@@ -42,6 +53,14 @@ static inline void __wrgsbase(unsigned long base) asmvolatile ("wrgsbase %0" ::"r" (base) ); } +static inline void __wrgskern (unsigned long base) +{ + asm_inline volatile ( "swapgs\n\t" + "wrgsbase %0\n\t" + "swapgs" + :: "r" (base) ); +} +static inline unsigned long read_fs_base (void ) {unsigned long base;
@@ -71,13 +90,9 @@ static inline unsigned long read_gs_shadow(void)unsigned long base;if (read_cr4 () &X86_CR4_FSGSBASE ) - { - asmvolatile ("swapgs" ); - base =__rdgsbase (); - asmvolatile ("swapgs" ); - } -else -rdmsrl (MSR_SHADOW_GS_BASE , base); +return __rdgskern (); + +rdmsrl (MSR_SHADOW_GS_BASE , base);return base; }
@@ -101,12 +116,7 @@ static inline void write_gs_base(unsigned long base)static inline void write_gs_shadow (unsigned long base) {if (read_cr4 () &X86_CR4_FSGSBASE ) - { - asmvolatile ("swapgs\n\t" -"wrgsbase %0\n\t" -"swapgs" - ::"r" (base) ); - } +__wrgskern (base);else wrmsrl (MSR_SHADOW_GS_BASE , base); }
Signature · 1 line
2.39.5
Re: [PATCH v2 14/16] x86/fsgsbase: Split out __{rd,wr}gskern() helpers
Quoted 2 lines · Andrew Cooper
Right now they're inline in {read,write}_gs_shadow(), but we're going to need to use these elsewhere to support FRED.
But why "kern"? We're not dealing with GS in kernel / user terms, but in real / shadow ones. I'm also not quite happy with the double leading underscores, fwiw.
As to using them elsewhere to support FRED: How can we when they use SWAPGS, which isn't available under FRED?
xen/arch/x86/include/asm/fsgsbase.h static inline unsigned long __rdgsbase(void) · line 41
Quoted 13 lines
xen/arch/x86/include/asm/fsgsbase.h +7
-0@@ -32,6 +32,17 @@ static inline unsigned long __rdgsbase(void)return base; } +static inline unsigned long __rdgskern (void ) +{ +unsigned long base; + + asm_inline volatile ( "swapgs\n\t" + "rdgsbase %0\n\t" + "swapgs" : "=r" (base) );
Again strictly speaking "=&r", if already you open-code rdgsbase() now.
Jan
Re: [PATCH v2 14/16] x86/fsgsbase: Split out __{rd,wr}gskern() helpers
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper
Right now they're inline in {read,write}_gs_shadow(), but we're going to need to use these elsewhere to support FRED.
But why "kern"? We're not dealing with GS in kernel / user terms, but in real / shadow ones.
Because it's a common name that also has the property of aligning nicely when used beside GS_BASE.
But fine, I'll rename it.
Quoted 2 lines
I'm also not quite happy with the double leading
underscores, fwiw.
Consistency with the similar logic.
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/include/asm/fsgsbase.h +0
-0@@ -32,6 +32,17 @@ static inline unsigned long __rdgsbase(void)return base; }
Quoted 8 lines
xen/arch/x86/include/asm/fsgsbase.h static inline unsigned long __rdgsbase(void) · line 41
Quoted 7 lines
xen/arch/x86/include/asm/fsgsbase.h +7
-0+static inline unsigned long __rdgskern (void ) +{ +unsigned long base; + + asm_inline volatile ( "swapgs\n\t" + "rdgsbase %0\n\t" + "swapgs" : "=r" (base) );Again strictly speaking "=&r", if already you open-code
rdgsbase()now.
As before, why? There are no inputs to be clobbered, early or otherwise.
~Andrew
Re: [PATCH v2 14/16] x86/fsgsbase: Split out __{rd,wr}gskern() helpers
Quoted 10 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper
Right now they're inline in {read,write}_gs_shadow(), but we're going to need to use these elsewhere to support FRED.
But why "kern"? We're not dealing with GS in kernel / user terms, but in real / shadow ones.
Because it's a common name that also has the property of aligning nicely when used beside
GS_BASE.
Quoted 6 lines
But fine, I'll rename it.
Quoted 2 lines
I'm also not quite happy with the double leading
underscores, fwiw.Consistency with the similar logic.
Yet making for more changes later, once we mean to strictly follow the respective Misra rule.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/include/asm/fsgsbase.h +0
-0@@ -32,6 +32,17 @@ static inline unsigned long __rdgsbase(void)return base; }Quoted 8 lines
xen/arch/x86/include/asm/fsgsbase.h static inline unsigned long __rdgsbase(void) · line 41
Quoted 7 lines
xen/arch/x86/include/asm/fsgsbase.h +7
-0+static inline unsigned long __rdgskern (void ) +{ +unsigned long base; + + asm_inline volatile ( "swapgs\n\t" + "rdgsbase %0\n\t" + "swapgs" : "=r" (base) );Again strictly speaking "=&r", if already you open-code
rdgsbase()now.
Quoted 1 line
As before, why? There are no inputs to be clobbered, early or otherwise.
Hence why I said "strictly speaking". Inputs or not imo doesn't matter in such a decision; that merely makes using the early-clobber form benign. The sole criteria by which I think one ought to go is whether a register is altered solely by the last (often: only) insn. IOW it's again more to prevent setting a bad precedent. Here things are easy to see, so a hypothetical future change isn't at much of a risk of breaking things. The more complex asm()-s get, the easier it is to overlook a missing early- clobber when making some change.
Jan
PATCH v2 15/16 x86/fsgsbase: Update fs/gs helpers to use wrmsrns()
In reply to Andrew Cooper... and rdmsr() while here.
Most of these accesses are in fastpaths and do not need serialising behaviour, but the write side is serialising on all Intel hardware as well as older AMD hardware.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/domain.c | 10 +++++----- xen/arch/x86/hvm/vmx/vmx.c | 4 ++-- xen/arch/x86/include/asm/fsgsbase.h | 30 +++++++++-------------------- 3 files changed, 16 insertions(+), 28 deletions(-)
xen/arch/x86/domain.c +5 -5
@@ -1824,9 +1824,9 @@ static void load_segments(struct vcpu *n) }else { -wrmsrl (MSR_FS_BASE , n->arch.pv.fs_base); -wrmsrl (MSR_GS_BASE , gsb); -wrmsrl (MSR_SHADOW_GS_BASE , gss); +wrmsrns (MSR_FS_BASE , n->arch.pv.fs_base); +wrmsrns (MSR_GS_BASE , gsb); +wrmsrns (MSR_SHADOW_GS_BASE , gss); } }
@@ -1951,8 +1951,8 @@ static void save_segments(struct vcpu *v) }else { -rdmsrl (MSR_FS_BASE , fs_base); -rdmsrl (MSR_GS_BASE , gs_base); + fs_base =rdmsr (MSR_FS_BASE ); + gs_base =rdmsr (MSR_GS_BASE ); } v->arch.pv.fs_base = fs_base;
xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -2733,8 +2733,8 @@ static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg)case MSR_SHADOW_GS_BASE :if ( v != curr )return v->arch.hvm.vmx.shadow_gs; -rdmsrl (MSR_SHADOW_GS_BASE , val); -return val; +else +return rdmsr (MSR_SHADOW_GS_BASE ); }/* Logic which maybe requires remote VMCS acquisition. */
xen/arch/x86/include/asm/fsgsbase.h +9 -21
@@ -63,38 +63,26 @@ static inline void __wrgskern(unsigned long base)static inline unsigned long read_fs_base (void ) { -unsigned long base; -if (read_cr4 () &X86_CR4_FSGSBASE )return __rdfsbase (); - -rdmsrl (MSR_FS_BASE , base); - -return base; +else +return rdmsr (MSR_FS_BASE ); }static inline unsigned long read_gs_base (void ) { -unsigned long base; -if (read_cr4 () &X86_CR4_FSGSBASE )return __rdgsbase (); - -rdmsrl (MSR_GS_BASE , base); - -return base; +else +return rdmsr (MSR_GS_BASE ); }static inline unsigned long read_gs_shadow (void ) { -unsigned long base; -if (read_cr4 () &X86_CR4_FSGSBASE )return __rdgskern (); - -rdmsrl (MSR_SHADOW_GS_BASE , base); - -return base; +else +return rdmsr (MSR_SHADOW_GS_BASE ); }static inline void write_fs_base (unsigned long base)
@@ -102,7 +90,7 @@ static inline void write_fs_base(unsigned long base)if (read_cr4 () &X86_CR4_FSGSBASE )__wrfsbase (base);else -wrmsrl (MSR_FS_BASE , base); +wrmsrns (MSR_FS_BASE , base); }static inline void write_gs_base (unsigned long base)
@@ -110,7 +98,7 @@ static inline void write_gs_base(unsigned long base)if (read_cr4 () &X86_CR4_FSGSBASE )__wrgsbase (base);else -wrmsrl (MSR_GS_BASE , base); +wrmsrns (MSR_GS_BASE , base); }static inline void write_gs_shadow (unsigned long base)
@@ -118,7 +106,7 @@ static inline void write_gs_shadow(unsigned long base)if (read_cr4 () &X86_CR4_FSGSBASE )__wrgskern (base);else - wrmsrl(MSR_SHADOW_GS_BASE, base); + wrmsrns(MSR_SHADOW_GS_BASE, base); }#endif /* X86_FSGSBASE_H */
Signature · 1 line
2.39.5
Re: [PATCH v2 15/16] x86/fsgsbase: Update fs/gs helpers to use wrmsrns()
xen/arch/x86/hvm/vmx/vmx.c static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg) · line 2738
Quoted 11 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/vmx.c +2
-2@@ -2733,8 +2733,8 @@ static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg)case MSR_SHADOW_GS_BASE :if ( v != curr )return v->arch.hvm.vmx.shadow_gs; -rdmsrl (MSR_SHADOW_GS_BASE , val); -return val; +else +return rdmsr (MSR_SHADOW_GS_BASE ); }
Here and below, can we please do without the pointless "else"? Strictly speaking in Misra's terms that's "dead code" (things working identically without), and I'm quite happy that I can now use this argument to support my personal antipathy to this style of coding. Or else use the conditional operator in such cases (where applicable).
Jan
Re: [PATCH v2 15/16] x86/fsgsbase: Update fs/gs helpers to use wrmsrns()
Quoted 17 lines · Jan Beulich
xen/arch/x86/hvm/vmx/vmx.c static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg) · line 2738
Quoted 11 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/vmx.c +2
-2@@ -2733,8 +2733,8 @@ static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg)case MSR_SHADOW_GS_BASE :if ( v != curr )return v->arch.hvm.vmx.shadow_gs; -rdmsrl (MSR_SHADOW_GS_BASE , val); -return val; +else +return rdmsr (MSR_SHADOW_GS_BASE ); }Here and below, can we please do without the pointless "else"? Strictly speaking in Misra's terms that's "dead code" (things working identically without), and I'm quite happy that I can now use this argument to support my personal antipathy to this style of coding. Or else use the conditional operator in such cases (where applicable).
No. I have always, and will always prioritise readability first and foremost.
I do not agree with your interpretation of MISRA in this case.
~Andrew
Re: [PATCH v2 15/16] x86/fsgsbase: Update fs/gs helpers to use wrmsrns()
Quoted 19 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
xen/arch/x86/hvm/vmx/vmx.c static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg) · line 2738
Quoted 11 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/vmx.c +2
-2@@ -2733,8 +2733,8 @@ static uint64_t cf_check vmx_get_reg(struct vcpu *v, unsigned int reg)case MSR_SHADOW_GS_BASE :if ( v != curr )return v->arch.hvm.vmx.shadow_gs; -rdmsrl (MSR_SHADOW_GS_BASE , val); -return val; +else +return rdmsr (MSR_SHADOW_GS_BASE ); }Here and below, can we please do without the pointless "else"? Strictly speaking in Misra's terms that's "dead code" (things working identically without), and I'm quite happy that I can now use this argument to support my personal antipathy to this style of coding. Or else use the conditional operator in such cases (where applicable).
Quoted 2 lines
No. I have always, and will always prioritise readability first and foremost.
But my preference is precisely because of readability. The excess "else" gives a wrong impression to the reader, when not looking closely enough. (And I [now] expect you might say the opposite.)
Quoted 1 line
I do not agree with your interpretation of MISRA in this case.
Something to discuss in a broader group then, I suppose.
Jan
PATCH v2 16/16 x86/fsgsbase: Improve code generation in read_registers()
In reply to Andrew CooperIt turns out that using the higher level helpers adjacent like this leads to terrible code generation. Due to -fno-strict-alising, the store into state-> invalidates the read_cr4() address calculation (which is really cpu_info->cr4 under the hood), meaning that it can't be hoisted.
As a result we get "locate the top of stack block, get cr4, and see if FSGSBASE is set" repeated 3 times, and an unreasoanble number of basic blocks.
Hoist the calculation manually, which results in two basic blocks.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
Side-by-side disassembly: https://termbin.com/9xfq
--- xen/arch/x86/traps.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
xen/arch/x86/traps.c +12 -3
@@ -118,9 +118,18 @@ static void read_registers(struct extra_state *state) state->cr3 =read_cr3 (); state->cr4 =read_cr4 (); - state->fsb =read_fs_base (); - state->gsb =read_gs_base (); - state->gss =read_gs_shadow (); +if ( state->cr4 &X86_CR4_FSGSBASE ) + { + state->fsb =__rdfsbase (); + state->gsb =__rdgsbase (); + state->gss =__rdgskern (); + } +else + { + state->fsb =rdmsr (MSR_FS_BASE ); + state->gsb =rdmsr (MSR_GS_BASE ); + state->gss =rdmsr (MSR_SHADOW_GS_BASE ); + } asm ("mov %%ds, %0" :"=m" (state->ds) ); asm ("mov %%es, %0" :"=m" (state->es) );
Signature · 1 line
2.39.5
Re: [PATCH v2 16/16] x86/fsgsbase: Improve code generation in read_registers()
Quoted 11 lines · Andrew Cooper
It turns out that using the higher level helpers adjacent like this leads to terrible code generation. Due to -fno-strict-alising, the store into state-> invalidates the
read_cr4()address calculation (which is reallycpu_info->cr4under the hood), meaning that it can't be hoisted.As a result we get "locate the top of stack block, get cr4, and see if FSGSBASE is set" repeated 3 times, and an unreasoanble number of basic blocks.
Hoist the calculation manually, which results in two basic blocks.
Signed-off-by Andrew Cooper <[email protected]>
Otoh the function here isn't really performance or size critical. I'm undecided whether the undesirable open-coding or the bad code gen are the lesser evil.
Quoted 4 lines
xen/arch/x86/traps.c +0
-0@@ -118,9 +118,18 @@ static void read_registers(struct extra_state *state) state->cr3 =read_cr3 ();
xen/arch/x86/traps.c static void read_registers(struct extra_state *state) · line 125
Quoted 10 lines
xen/arch/x86/traps.c +5
-3state->cr4 =read_cr4 (); - state->fsb =read_fs_base (); - state->gsb =read_gs_base (); - state->gss =read_gs_shadow (); +if ( state->cr4 &X86_CR4_FSGSBASE ) + { + state->fsb =__rdfsbase (); + state->gsb =__rdgsbase (); + state->gss =__rdgskern ();
This, btw, supports my desire to not use "kern" but "shadow" in the new helper's name.
Jan
Re: [PATCH v2 16/16] x86/fsgsbase: Improve code generation in read_registers()
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
It turns out that using the higher level helpers adjacent like this leads to terrible code generation. Due to -fno-strict-alising, the store into state-> invalidates the
read_cr4()address calculation (which is reallycpu_info->cr4under the hood), meaning that it can't be hoisted.
Quoted 8 lines
Quoted 6 lines
As a result we get "locate the top of stack block, get cr4, and see if FSGSBASE is set" repeated 3 times, and an unreasoanble number of basic blocks.
Hoist the calculation manually, which results in two basic blocks.
Signed-off-by Andrew Cooper <[email protected]>Otoh the function here isn't really performance or size critical. I'm undecided whether the undesirable open-coding or the bad code gen are the lesser evil.
This function no, but every other place touching FS and GS is performance critical. They're all messy to start with, and get worse under FRED.
~Andrew
Re: [PATCH v2 16/16] x86/fsgsbase: Improve code generation in read_registers()
Quoted 12 lines · Andrew Cooper
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
It turns out that using the higher level helpers adjacent like this leads to terrible code generation. Due to -fno-strict-alising, the store into state-> invalidates the
read_cr4()address calculation (which is reallycpu_info->cr4under the hood), meaning that it can't be hoisted.As a result we get "locate the top of stack block, get cr4, and see if FSGSBASE is set" repeated 3 times, and an unreasoanble number of basic blocks.
Hoist the calculation manually, which results in two basic blocks.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Andrew Cooper <[email protected]>Otoh the function here isn't really performance or size critical. I'm undecided whether the undesirable open-coding or the bad code gen are the lesser evil.
This function no, but every other place touching FS and GS is performance critical. They're all messy to start with, and get worse under FRED.
Is there any (further) bad effect to the function here by the time all of the FRED bits are in?
Jan