From: "Jan Beulich" Subject: [PATCH 3/4] x86: use {rd, wr}{fs, gs}base when available Date: Thu, 10 Oct 2013 14:54:51 +0100 Message-ID: <5256CDCB02000078000FA3EA@nat28.tlf.novell.com> References: <5256CC6F02000078000FA3D0@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part66556FBB.0__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VUGhb-000325-JV for xen-devel@lists.xenproject.org; Thu, 10 Oct 2013 13:54:55 +0000 In-Reply-To: <5256CC6F02000078000FA3D0@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part66556FBB.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... as being intended to be faster than MSR reads/writes. In the case of emulate_privileged_op() also use these in favor of the cached (but possibly stale) addresses from arch.pv_vcpu. This allows entirely removing the code that was the subject of XSA-67. Signed-off-by: Jan Beulich --- a/xen/arch/x86/Rules.mk +++ b/xen/arch/x86/Rules.mk @@ -31,6 +31,7 @@ $(call cc-options-add,CFLAGS,CC,$(EMBEDD $(call cc-option-add,CFLAGS,CC,-Wnested-externs) $(call as-insn-check,CFLAGS,CC,"vmcall",-DHAVE_GAS_VMX) $(call as-insn-check,CFLAGS,CC,"invept (%rax)$$(comma)%rax",-DHAVE_GAS_EPT= ) +$(call as-insn-check,CFLAGS,CC,"rdfsbase %rax",-DHAVE_GAS_FSGSBASE) =20 ifeq ($(supervisor_mode_kernel),y) CFLAGS +=3D -DCONFIG_X86_SUPERVISOR_MODE_KERNEL=3D1 --- a/xen/arch/x86/acpi/suspend.c +++ b/xen/arch/x86/acpi/suspend.c @@ -27,8 +27,8 @@ void save_rest_processor_state(void) asm volatile ( "movw %%ds,(%0); movw %%es,2(%0); movw %%fs,4(%0); movw %%gs,6(%0)= " : : "r" (saved_segs) : "memory" ); - rdmsrl(MSR_FS_BASE, saved_fs_base); - rdmsrl(MSR_GS_BASE, saved_gs_base); + saved_fs_base =3D rdfsbase(); + saved_gs_base =3D rdgsbase(); rdmsrl(MSR_SHADOW_GS_BASE, saved_kernel_gs_base); rdmsrl(MSR_CSTAR, saved_cstar); rdmsrl(MSR_LSTAR, saved_lstar); @@ -56,8 +56,8 @@ void restore_rest_processor_state(void) X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_TF, 0U); =20 - wrmsrl(MSR_FS_BASE, saved_fs_base); - wrmsrl(MSR_GS_BASE, saved_gs_base); + wrfsbase(saved_fs_base); + wrgsbase(saved_gs_base); wrmsrl(MSR_SHADOW_GS_BASE, saved_kernel_gs_base); =20 if ( boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_INTEL || --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -1092,7 +1092,7 @@ static void load_segments(struct vcpu *n { /* This can only be non-zero if selector is NULL. */ if ( n->arch.pv_vcpu.fs_base ) - wrmsrl(MSR_FS_BASE, n->arch.pv_vcpu.fs_base); + wrfsbase(n->arch.pv_vcpu.fs_base); =20 /* Most kernels have non-zero GS base, so don't bother testing. = */ /* (This is also a serialising instruction, avoiding AMD erratum = #88.) */ @@ -1100,7 +1100,7 @@ static void load_segments(struct vcpu *n =20 /* This can only be non-zero if selector is NULL. */ if ( n->arch.pv_vcpu.gs_base_user ) - wrmsrl(MSR_GS_BASE, n->arch.pv_vcpu.gs_base_user); + wrgsbase(n->arch.pv_vcpu.gs_base_user); =20 /* If in kernel mode then switch the GS bases around. */ if ( (n->arch.flags & TF_kernel_mode) ) --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -1264,6 +1264,9 @@ void __init __start_xen(unsigned long mb if ( cpu_has_smep ) set_in_cr4(X86_CR4_SMEP); =20 + if ( cpu_has_fsgsbase ) + set_in_cr4(X86_CR4_FSGSBASE); + local_irq_enable(); =20 pt_pci_init(); --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -1985,28 +1985,18 @@ static int emulate_privileged_op(struct=20 } else { - if ( lm_ovr =3D=3D lm_seg_none || data_sel < 4 ) + switch ( lm_ovr ) { - switch ( lm_ovr ) - { - case lm_seg_none: - data_base =3D 0UL; - break; - case lm_seg_fs: - data_base =3D v->arch.pv_vcpu.fs_base; - break; - case lm_seg_gs: - if ( guest_kernel_mode(v, regs) ) - data_base =3D v->arch.pv_vcpu.gs_base_kernel; - else - data_base =3D v->arch.pv_vcpu.gs_base_user; - break; - } + default: + data_base =3D 0UL; + break; + case lm_seg_fs: + data_base =3D rdfsbase(); + break; + case lm_seg_gs: + data_base =3D rdgsbase(); + break; } - else if ( !read_descriptor(data_sel, v, regs, - &data_base, &data_limit, &ar, 0) = || - !(ar & _SEGMENT_S) || !(ar & _SEGMENT_P) ) - goto fail; data_limit =3D ~0UL; ar =3D _SEGMENT_WR|_SEGMENT_S|_SEGMENT_DPL|_SEGMENT_P; } --- a/xen/include/asm-x86/domain.h +++ b/xen/include/asm-x86/domain.h @@ -457,12 +457,12 @@ unsigned long pv_guest_cr4_fixup(const s (((v)->arch.pv_vcpu.ctrlreg[4] \ | (mmu_cr4_features \ & (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_SMEP | \ - X86_CR4_OSXSAVE)) \ + X86_CR4_OSXSAVE | X86_CR4_FSGSBASE)) \ | ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0)) \ & ~X86_CR4_DE) #define real_cr4_to_pv_guest_cr4(c) \ - ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD \ - | X86_CR4_OSXSAVE | X86_CR4_SMEP)) + ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD | \ + X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE)) =20 void domain_cpuid(struct domain *d, unsigned int input, --- a/xen/include/asm-x86/msr.h +++ b/xen/include/asm-x86/msr.h @@ -9,6 +9,7 @@ #include #include #include +#include =20 #define rdmsr(msr,val1,val2) \ __asm__ __volatile__("rdmsr" \ @@ -97,6 +98,61 @@ static inline int wrmsr_safe(unsigned in : "=3Da" (low), "=3Dd" (high) \ : "c" (counter)) =20 +static inline unsigned long rdfsbase(void) +{ + unsigned long base; + + if ( cpu_has_fsgsbase ) +#ifdef HAVE_GAS_FSGSBASE + asm volatile ( "rdfsbase %0" : "=3Dr" (base) ); +#else + asm volatile ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xc0" : "=3Da" = (base) ); +#endif + else + rdmsrl(MSR_FS_BASE, base); + + return base; +} + +static inline unsigned long rdgsbase(void) +{ + unsigned long base; + + if ( cpu_has_fsgsbase ) +#ifdef HAVE_GAS_FSGSBASE + asm volatile ( "rdgsbase %0" : "=3Dr" (base) ); +#else + asm volatile ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xc8" : "=3Da" = (base) ); +#endif + else + rdmsrl(MSR_GS_BASE, base); + + return base; +} + +static inline void wrfsbase(unsigned long base) +{ + if ( cpu_has_fsgsbase ) +#ifdef HAVE_GAS_FSGSBASE + asm volatile ( "wrfsbase %0" :: "r" (base) ); +#else + asm volatile ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd0" :: "a" (base) = ); +#endif + else + wrmsrl(MSR_FS_BASE, base); +} + +static inline void wrgsbase(unsigned long base) +{ + if ( cpu_has_fsgsbase ) +#ifdef HAVE_GAS_FSGSBASE + asm volatile ( "wrgsbase %0" :: "r" (base) ); +#else + asm volatile ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd8" :: "a" (base) = ); +#endif + else + wrmsrl(MSR_GS_BASE, base); +} =20 DECLARE_PER_CPU(u64, efer); u64 read_efer(void); --=__Part66556FBB.0__= Content-Type: text/plain; name="x86-fsgsbase.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-fsgsbase.patch" x86: use {rd,wr}{fs,gs}base when available=0A=0A... as being intended to = be faster than MSR reads/writes.=0A=0AIn the case of emulate_privileged_op(= ) also use these in favor of the=0Acached (but possibly stale) addresses = from arch.pv_vcpu. This allows=0Aentirely removing the code that was the = subject of XSA-67.=0A=0ASigned-off-by: Jan Beulich =0A= =0A--- a/xen/arch/x86/Rules.mk=0A+++ b/xen/arch/x86/Rules.mk=0A@@ -31,6 = +31,7 @@ $(call cc-options-add,CFLAGS,CC,$(EMBEDD=0A $(call cc-option-add,C= FLAGS,CC,-Wnested-externs)=0A $(call as-insn-check,CFLAGS,CC,"vmcall",-DHAV= E_GAS_VMX)=0A $(call as-insn-check,CFLAGS,CC,"invept (%rax)$$(comma)%rax",-= DHAVE_GAS_EPT)=0A+$(call as-insn-check,CFLAGS,CC,"rdfsbase %rax",-DHAVE_GAS= _FSGSBASE)=0A =0A ifeq ($(supervisor_mode_kernel),y)=0A CFLAGS +=3D = -DCONFIG_X86_SUPERVISOR_MODE_KERNEL=3D1=0A--- a/xen/arch/x86/acpi/suspend.c= =0A+++ b/xen/arch/x86/acpi/suspend.c=0A@@ -27,8 +27,8 @@ void save_rest_pro= cessor_state(void)=0A asm volatile (=0A "movw %%ds,(%0); movw = %%es,2(%0); movw %%fs,4(%0); movw %%gs,6(%0)"=0A : : "r" (saved_seg= s) : "memory" );=0A- rdmsrl(MSR_FS_BASE, saved_fs_base);=0A- = rdmsrl(MSR_GS_BASE, saved_gs_base);=0A+ saved_fs_base =3D rdfsbase();=0A= + saved_gs_base =3D rdgsbase();=0A rdmsrl(MSR_SHADOW_GS_BASE, = saved_kernel_gs_base);=0A rdmsrl(MSR_CSTAR, saved_cstar);=0A = rdmsrl(MSR_LSTAR, saved_lstar);=0A@@ -56,8 +56,8 @@ void restore_rest_proce= ssor_state(void)=0A X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_TF,=0A= 0U);=0A =0A- wrmsrl(MSR_FS_BASE, saved_fs_base);=0A- = wrmsrl(MSR_GS_BASE, saved_gs_base);=0A+ wrfsbase(saved_fs_base);=0A+ = wrgsbase(saved_gs_base);=0A wrmsrl(MSR_SHADOW_GS_BASE, saved_kernel_gs_= base);=0A =0A if ( boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_INTEL = ||=0A--- a/xen/arch/x86/domain.c=0A+++ b/xen/arch/x86/domain.c=0A@@ = -1092,7 +1092,7 @@ static void load_segments(struct vcpu *n=0A {=0A = /* This can only be non-zero if selector is NULL. */=0A if ( = n->arch.pv_vcpu.fs_base )=0A- wrmsrl(MSR_FS_BASE, n->arch.pv_vcp= u.fs_base);=0A+ wrfsbase(n->arch.pv_vcpu.fs_base);=0A =0A = /* Most kernels have non-zero GS base, so don't bother testing. */=0A = /* (This is also a serialising instruction, avoiding AMD erratum = #88.) */=0A@@ -1100,7 +1100,7 @@ static void load_segments(struct vcpu = *n=0A =0A /* This can only be non-zero if selector is NULL. */=0A = if ( n->arch.pv_vcpu.gs_base_user )=0A- wrmsrl(MSR_GS_BAS= E, n->arch.pv_vcpu.gs_base_user);=0A+ wrgsbase(n->arch.pv_vcpu.g= s_base_user);=0A =0A /* If in kernel mode then switch the GS bases = around. */=0A if ( (n->arch.flags & TF_kernel_mode) )=0A--- = a/xen/arch/x86/setup.c=0A+++ b/xen/arch/x86/setup.c=0A@@ -1264,6 +1264,9 = @@ void __init __start_xen(unsigned long mb=0A if ( cpu_has_smep )=0A = set_in_cr4(X86_CR4_SMEP);=0A =0A+ if ( cpu_has_fsgsbase )=0A+ = set_in_cr4(X86_CR4_FSGSBASE);=0A+=0A local_irq_enable();=0A =0A = pt_pci_init();=0A--- a/xen/arch/x86/traps.c=0A+++ b/xen/arch/x86/traps.c= =0A@@ -1985,28 +1985,18 @@ static int emulate_privileged_op(struct =0A = }=0A else=0A {=0A- if ( lm_ovr =3D=3D = lm_seg_none || data_sel < 4 )=0A+ switch ( lm_ovr )=0A = {=0A- switch ( lm_ovr )=0A- {=0A- = case lm_seg_none:=0A- data_base =3D 0UL;=0A- = break;=0A- case lm_seg_fs:=0A- = data_base =3D v->arch.pv_vcpu.fs_base;=0A- = break;=0A- case lm_seg_gs:=0A- if ( = guest_kernel_mode(v, regs) )=0A- data_base =3D = v->arch.pv_vcpu.gs_base_kernel;=0A- else=0A- = data_base =3D v->arch.pv_vcpu.gs_base_user;=0A- = break;=0A- }=0A+ default:=0A+ = data_base =3D 0UL;=0A+ break;=0A+ case lm_seg_fs:= =0A+ data_base =3D rdfsbase();=0A+ break;=0A+= case lm_seg_gs:=0A+ data_base =3D rdgsbase();=0A= + break;=0A }=0A- else if ( = !read_descriptor(data_sel, v, regs,=0A- = &data_base, &data_limit, &ar, 0) ||=0A- !(ar & = _SEGMENT_S) || !(ar & _SEGMENT_P) )=0A- goto fail;=0A = data_limit =3D ~0UL;=0A ar =3D _SEGMENT_WR|_SEGMENT_S|_SE= GMENT_DPL|_SEGMENT_P;=0A }=0A--- a/xen/include/asm-x86/domain.h=0A+= ++ b/xen/include/asm-x86/domain.h=0A@@ -457,12 +457,12 @@ unsigned long = pv_guest_cr4_fixup(const s=0A (((v)->arch.pv_vcpu.ctrlreg[4] = \=0A | (mmu_cr4_features = \=0A & (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_SMEP | = \=0A- X86_CR4_OSXSAVE)) \=0A+ = X86_CR4_OSXSAVE | X86_CR4_FSGSBASE)) \=0A | = ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0)) \=0A & ~X86_CR4_DE= )=0A #define real_cr4_to_pv_guest_cr4(c) \=0A- = ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD \=0A- = | X86_CR4_OSXSAVE | X86_CR4_SMEP))=0A+ ((c) & ~(X86_CR4_PGE | X86_CR4_PS= E | X86_CR4_TSD | \=0A+ X86_CR4_OSXSAVE | X86_CR4_SMEP | = X86_CR4_FSGSBASE))=0A =0A void domain_cpuid(struct domain *d,=0A = unsigned int input,=0A--- a/xen/include/asm-x86/msr.h=0A+++ = b/xen/include/asm-x86/msr.h=0A@@ -9,6 +9,7 @@=0A #include =0A= #include =0A #include =0A+#include = =0A =0A #define rdmsr(msr,val1,val2) \=0A __asm__ = __volatile__("rdmsr" \=0A@@ -97,6 +98,61 @@ static inline int wrmsr_safe(un= signed in=0A : "=3Da" (low), "=3Dd" (high) \=0A = : "c" (counter))=0A =0A+static inline unsigned long rdfsbase(void= )=0A+{=0A+ unsigned long base;=0A+=0A+ if ( cpu_has_fsgsbase = )=0A+#ifdef HAVE_GAS_FSGSBASE=0A+ asm volatile ( "rdfsbase %0" : = "=3Dr" (base) );=0A+#else=0A+ asm volatile ( ".byte 0xf3, 0x48, = 0x0f, 0xae, 0xc0" : "=3Da" (base) );=0A+#endif=0A+ else=0A+ = rdmsrl(MSR_FS_BASE, base);=0A+=0A+ return base;=0A+}=0A+=0A+static = inline unsigned long rdgsbase(void)=0A+{=0A+ unsigned long base;=0A+=0A+= if ( cpu_has_fsgsbase )=0A+#ifdef HAVE_GAS_FSGSBASE=0A+ asm = volatile ( "rdgsbase %0" : "=3Dr" (base) );=0A+#else=0A+ asm = volatile ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xc8" : "=3Da" (base) );=0A+#endi= f=0A+ else=0A+ rdmsrl(MSR_GS_BASE, base);=0A+=0A+ return = base;=0A+}=0A+=0A+static inline void wrfsbase(unsigned long base)=0A+{=0A+ = if ( cpu_has_fsgsbase )=0A+#ifdef HAVE_GAS_FSGSBASE=0A+ asm = volatile ( "wrfsbase %0" :: "r" (base) );=0A+#else=0A+ asm volatile = ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd0" :: "a" (base) );=0A+#endif=0A+ = else=0A+ wrmsrl(MSR_FS_BASE, base);=0A+}=0A+=0A+static inline void = wrgsbase(unsigned long base)=0A+{=0A+ if ( cpu_has_fsgsbase )=0A+#ifdef = HAVE_GAS_FSGSBASE=0A+ asm volatile ( "wrgsbase %0" :: "r" (base) = );=0A+#else=0A+ asm volatile ( ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd8" = :: "a" (base) );=0A+#endif=0A+ else=0A+ wrmsrl(MSR_GS_BASE, = base);=0A+}=0A =0A DECLARE_PER_CPU(u64, efer);=0A u64 read_efer(void);=0A --=__Part66556FBB.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part66556FBB.0__=--