From: "Jan Beulich" Subject: [PATCH 3/4] VMX: use proper instruction mnemonics if assembler supports them Date: Fri, 23 Aug 2013 15:03:18 +0100 Message-ID: <521787C602000078000EE07F@nat28.tlf.novell.com> References: <521786A002000078000EE064@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartF0C3B8B6.1__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VCrxS-0000IR-MO for xen-devel@lists.xenproject.org; Fri, 23 Aug 2013 14:03:23 +0000 In-Reply-To: <521786A002000078000EE064@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 , Eddie Dong , Jun Nakajima 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. --=__PartF0C3B8B6.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline With the hex byte emission we were taking away a good part of flexibility from the compiler, as for simplicity reasons these were built using fixed operands. All half way modern build environments would allow using the mnemonics (but we can't disable the hex variants yet, since the binutils around at the time gcc 4.1 got released didn't support these yet). I didn't convert __vmread() yet because that would, just like for __vmread_safe(), imply converting to a macro so that the output operand can be the caller supplied variable rather than an intermediate one. As that would require touching all invocation points of __vmread() (of which there are quite a few), I'd first like to be certain the approach is acceptable; the main question being whether the now conditional code might be considered to cause future maintenance issues, and the second being that of parameter/argument ordering (here I made __vmread_safe() match __vmwrite(), but one could also take the position that read and write should use the inverse order of one another, in line with the actual instruction operands). Additionally I was quite puzzled to find that all the asm()-s involved here have memory clobbers - what are they needed for? Or can they be dropped at least in some cases? Signed-off-by: Jan Beulich --- a/Config.mk +++ b/Config.mk @@ -4,6 +4,12 @@ ifeq ($(filter /%,$(XEN_ROOT)),) $(error XEN_ROOT must be absolute) endif =20 +# Convenient variables +comma :=3D , +squote :=3D ' +empty :=3D +space :=3D $(empty) $(empty) + # fallback for older make realpath =3D $(wildcard $(foreach file,$(1),$(shell cd -P $(dir $(file)) = && echo "$$PWD/$(notdir $(file))"))) =20 @@ -128,6 +134,21 @@ endef check-$(gcc) =3D $(call cc-ver-check,CC,0x040100,"Xen requires at least = gcc-4.1") $(eval $(check-y)) =20 +# as-insn: Check whether assembler supports an instruction. +# Usage: cflags-y +=3D $(call as-insn "insn",option-yes,option-no) +as-insn =3D $(if $(shell echo 'void _(void) { asm volatile ( $(2) ); }' \ + | $(1) -c -x c -o /dev/null - 2>&1),$(4),$(3)) + +# as-insn-check: Add an option to compilation flags, but only if insn is +# supported by assembler. +# Usage: $(call as-insn-check CFLAGS,CC,"nop",-DHAVE_GAS_NOP) +as-insn-check =3D $(eval $(call as-insn-check-closure,$(1),$(2),$(3),$(4))= ) +define as-insn-check-closure + ifeq ($$(call as-insn,$$($(2)),$(3),y,n),y) + $(1) +=3D $(4) + endif +endef + define buildmakevars2shellvars export PREFIX=3D"$(PREFIX)"; = \ export XEN_SCRIPT_DIR=3D"$(XEN_SCRIPT_DIR)"; = \ --- a/xen/arch/x86/Rules.mk +++ b/xen/arch/x86/Rules.mk @@ -28,6 +28,8 @@ CFLAGS +=3D -msoft-float =20 $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS)) $(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= ) =20 ifeq ($(supervisor_mode_kernel),y) CFLAGS +=3D -DCONFIG_X86_SUPERVISOR_MODE_KERNEL=3D1 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1292,12 +1292,11 @@ void vmx_do_resume(struct vcpu *v) reset_stack_and_jump(vmx_asm_do_vmentry); } =20 -static unsigned long vmr(unsigned long field) +static inline unsigned long vmr(unsigned long field) { - int rc; unsigned long val; - val =3D __vmread_safe(field, &rc); - return rc ? 0 : val; + + return __vmread_safe(field, val) ? val : 0; } =20 static void vmx_dump_sel(char *name, uint32_t selector) --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -951,13 +951,11 @@ fallback: vvmcs_to_shadow(vvmcs, field[i]); } =20 -static void shadow_to_vvmcs(void *vvmcs, unsigned int field) +static inline void shadow_to_vvmcs(void *vvmcs, unsigned int field) { u64 value; - int rc; =20 - value =3D __vmread_safe(field, &rc); - if ( !rc ) + if ( __vmread_safe(field, value) ) __set_vvmcs(vvmcs, field, value); } =20 --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -282,27 +282,44 @@ extern uint8_t posted_intr_vector; =20 static inline void __vmptrld(u64 addr) { - asm volatile ( VMPTRLD_OPCODE - MODRM_EAX_06 + asm volatile ( +#ifdef HAVE_GAS_VMX + "vmptrld %0\n" +#else + VMPTRLD_OPCODE MODRM_EAX_06 +#endif /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */ UNLIKELY_START(be, vmptrld) "\tud2\n" UNLIKELY_END_SECTION : +#ifdef HAVE_GAS_VMX + : "m" (addr) +#else : "a" (&addr) +#endif : "memory"); } =20 static inline void __vmpclear(u64 addr) { - asm volatile ( VMCLEAR_OPCODE - MODRM_EAX_06 + asm volatile ( +#ifdef HAVE_GAS_VMX + "vmclear %0\n" +#else + VMCLEAR_OPCODE MODRM_EAX_06 +#endif /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */ UNLIKELY_START(be, vmclear) "\tud2\n" UNLIKELY_END_SECTION +#ifdef HAVE_GAS_VMX + : "+m" (addr) + : +#else : : "a" (&addr) +#endif : "memory"); } =20 @@ -325,31 +342,58 @@ static inline unsigned long __vmread(uns =20 static inline void __vmwrite(unsigned long field, unsigned long value) { - asm volatile ( VMWRITE_OPCODE - MODRM_EAX_ECX + asm volatile ( +#ifdef HAVE_GAS_VMX + "vmwrite %1, %0\n" +#else + VMWRITE_OPCODE MODRM_EAX_ECX +#endif /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */ UNLIKELY_START(be, vmwrite) "\tud2\n" UNLIKELY_END_SECTION :=20 +#ifdef HAVE_GAS_VMX + : "r" (field) , "rm" (value) +#else : "a" (field) , "c" (value) +#endif : "memory"); } =20 -static inline unsigned long __vmread_safe(unsigned long field, int = *error) -{ - unsigned long ecx; - - asm volatile ( VMREAD_OPCODE - MODRM_EAX_ECX - /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D -1 */ - "setna %b0 ; neg %0" - : "=3Dq" (*error), "=3Dc" (ecx) - : "0" (0), "a" (field) - : "memory"); - - return ecx; -} +#ifdef HAVE_GAS_VMX +#define __vmread_safe(field, value) ({ \ + bool_t okay_; \ + if ( sizeof(value) =3D=3D sizeof(long) ) \ + asm volatile ( "vmread %q2, %1\n\t" \ + /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D 0 */ \ + "setnbe %0" \ + : "=3Dqm" (okay_), "=3Drm" (value) \ + : "r" (field) \ + : "memory"); \ + else \ + { \ + asm volatile ( "vmread %q2, %q1\n\t" \ + /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D 0 */ \ + "setnbe %0" \ + : "=3Dqm" (okay_), "=3Dr" (value) \ + : "r" (field) \ + : "memory"); \ + } \ + okay_; \ +}) +#else +#define __vmread_safe(field, value) ({ \ + bool_t okay_; \ + asm volatile ( VMREAD_OPCODE MODRM_EAX_ECX \ + /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D 0 */ \ + "setnbe %0" \ + : "=3Dqm" (okay_), "=3Dc" (value) \ + : "a" (field) \ + : "memory"); \ + okay_; \ +}) +#endif =20 static inline void __invept(int type, u64 eptp, u64 gpa) { @@ -365,14 +409,22 @@ static inline void __invept(int type, u6 !cpu_has_vmx_ept_invept_single_context ) type =3D INVEPT_ALL_CONTEXT; =20 - asm volatile ( INVEPT_OPCODE - MODRM_EAX_08 + asm volatile ( +#ifdef HAVE_GAS_EPT + "invept %0, %q1\n" +#else + INVEPT_OPCODE MODRM_EAX_08 +#endif /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */ UNLIKELY_START(be, invept) "\tud2\n" UNLIKELY_END_SECTION : +#ifdef HAVE_GAS_EPT + : "m" (operand), "r" (type) +#else : "a" (&operand), "c" (type) +#endif : "memory" ); } =20 @@ -385,14 +437,23 @@ static inline void __invvpid(int type, u } __attribute__ ((packed)) operand =3D {vpid, 0, gva}; =20 /* Fix up #UD exceptions which occur when TLBs are flushed before = VMXON. */ - asm volatile ( "1: " INVVPID_OPCODE MODRM_EAX_08 + asm volatile ( "1: " +#ifdef HAVE_GAS_EPT + "invvpid %0, %q1\n" +#else + INVVPID_OPCODE MODRM_EAX_08 +#endif /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */ "2: " UNLIKELY_START(be, invvpid) "\tud2\n" UNLIKELY_END_SECTION "\n" _ASM_EXTABLE(1b, 2b) : +#ifdef HAVE_GAS_EPT + : "m" (operand), "r" (type) +#else : "a" (&operand), "c" (type) +#endif : "memory" ); } =20 --=__PartF0C3B8B6.1__= Content-Type: text/plain; name="VMX-use-mnemonics.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="VMX-use-mnemonics.patch" VMX: use proper instruction mnemonics if assembler supports them=0A=0AWith = the hex byte emission we were taking away a good part of=0Aflexibility = from the compiler, as for simplicity reasons these were=0Abuilt using = fixed operands. All half way modern build environments=0Awould allow using = the mnemonics (but we can't disable the hex variants=0Ayet, since the = binutils around at the time gcc 4.1 got released didn't=0Asupport these = yet).=0A=0AI didn't convert __vmread() yet because that would, just like = for=0A__vmread_safe(), imply converting to a macro so that the output = operand=0Acan be the caller supplied variable rather than an intermediate = one. As=0Athat would require touching all invocation points of __vmread() = (of=0Awhich there are quite a few), I'd first like to be certain the = approach=0Ais acceptable; the main question being whether the now = conditional code=0Amight be considered to cause future maintenance issues, = and the second=0Abeing that of parameter/argument ordering (here I made = __vmread_safe()=0Amatch __vmwrite(), but one could also take the position = that read and=0Awrite should use the inverse order of one another, in line = with the=0Aactual instruction operands).=0A=0AAdditionally I was quite = puzzled to find that all the asm()-s involved=0Ahere have memory clobbers = - what are they needed for? Or can they be=0Adropped at least in some = cases?=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/Config.mk=0A+++ b/Config.mk=0A@@ -4,6 +4,12 @@ ifeq ($(filter /%,$(XEN_RO= OT)),)=0A $(error XEN_ROOT must be absolute)=0A endif=0A =0A+# Convenient = variables=0A+comma :=3D ,=0A+squote :=3D '=0A+empty :=3D=0A+space = :=3D $(empty) $(empty)=0A+=0A # fallback for older make=0A realpath =3D = $(wildcard $(foreach file,$(1),$(shell cd -P $(dir $(file)) && echo = "$$PWD/$(notdir $(file))")))=0A =0A@@ -128,6 +134,21 @@ endef=0A check-$(gc= c) =3D $(call cc-ver-check,CC,0x040100,"Xen requires at least gcc-4.1")=0A = $(eval $(check-y))=0A =0A+# as-insn: Check whether assembler supports an = instruction.=0A+# Usage: cflags-y +=3D $(call as-insn "insn",option-yes,opt= ion-no)=0A+as-insn =3D $(if $(shell echo 'void _(void) { asm volatile ( = $(2) ); }' \=0A+ | $(1) -c -x c -o /dev/null - = 2>&1),$(4),$(3))=0A+=0A+# as-insn-check: Add an option to compilation = flags, but only if insn is=0A+# supported by assembler.=0A+#= Usage: $(call as-insn-check CFLAGS,CC,"nop",-DHAVE_GAS_NOP)=0A+as-insn-che= ck =3D $(eval $(call as-insn-check-closure,$(1),$(2),$(3),$(4)))=0A+define = as-insn-check-closure=0A+ ifeq ($$(call as-insn,$$($(2)),$(3),y,n),y)=0A= + $(1) +=3D $(4)=0A+ endif=0A+endef=0A+=0A define buildmakevars2s= hellvars=0A export PREFIX=3D"$(PREFIX)"; = \=0A export XEN_SCRIPT_DIR=3D"$(XEN_SCRIPT_DIR)"; = \=0A--- a/xen/arch/x86/Rules.mk=0A+++ b/xen/arch/x86/Rul= es.mk=0A@@ -28,6 +28,8 @@ CFLAGS +=3D -msoft-float=0A =0A $(call cc-options= -add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS))=0A $(call cc-option-add,CFLAGS,CC,= -Wnested-externs)=0A+$(call as-insn-check,CFLAGS,CC,"vmcall",-DHAVE_GAS_VMX= )=0A+$(call as-insn-check,CFLAGS,CC,"invept (%rax)$$(comma)%rax",-DHAVE_GAS= _EPT)=0A =0A ifeq ($(supervisor_mode_kernel),y)=0A CFLAGS +=3D -DCONFIG_X86= _SUPERVISOR_MODE_KERNEL=3D1=0A--- a/xen/arch/x86/hvm/vmx/vmcs.c=0A+++ = b/xen/arch/x86/hvm/vmx/vmcs.c=0A@@ -1292,12 +1292,11 @@ void vmx_do_resume(= struct vcpu *v)=0A reset_stack_and_jump(vmx_asm_do_vmentry);=0A }=0A = =0A-static unsigned long vmr(unsigned long field)=0A+static inline = unsigned long vmr(unsigned long field)=0A {=0A- int rc;=0A unsigned = long val;=0A- val =3D __vmread_safe(field, &rc);=0A- return rc ? 0 : = val;=0A+=0A+ return __vmread_safe(field, val) ? val : 0;=0A }=0A =0A = static void vmx_dump_sel(char *name, uint32_t selector)=0A--- a/xen/arch/x8= 6/hvm/vmx/vvmx.c=0A+++ b/xen/arch/x86/hvm/vmx/vvmx.c=0A@@ -951,13 +951,11 = @@ fallback:=0A vvmcs_to_shadow(vvmcs, field[i]);=0A }=0A = =0A-static void shadow_to_vvmcs(void *vvmcs, unsigned int field)=0A+static = inline void shadow_to_vvmcs(void *vvmcs, unsigned int field)=0A {=0A = u64 value;=0A- int rc;=0A =0A- value =3D __vmread_safe(field, = &rc);=0A- if ( !rc )=0A+ if ( __vmread_safe(field, value) )=0A = __set_vvmcs(vvmcs, field, value);=0A }=0A =0A--- a/xen/include/asm-x86/hv= m/vmx/vmx.h=0A+++ b/xen/include/asm-x86/hvm/vmx/vmx.h=0A@@ -282,27 +282,44 = @@ extern uint8_t posted_intr_vector;=0A =0A static inline void __vmptrld(u= 64 addr)=0A {=0A- asm volatile ( VMPTRLD_OPCODE=0A- = MODRM_EAX_06=0A+ asm volatile (=0A+#ifdef HAVE_GAS_VMX=0A+ = "vmptrld %0\n"=0A+#else=0A+ VMPTRLD_OPCODE = MODRM_EAX_06=0A+#endif=0A /* CF=3D=3D1 or ZF=3D=3D1 --> = crash (ud2) */=0A UNLIKELY_START(be, vmptrld)=0A = "\tud2\n"=0A UNLIKELY_END_SECTION=0A = :=0A+#ifdef HAVE_GAS_VMX=0A+ : "m" (addr)=0A+= #else=0A : "a" (&addr)=0A+#endif=0A = : "memory");=0A }=0A =0A static inline void __vmpclear(u64 addr)=0A {=0A- = asm volatile ( VMCLEAR_OPCODE=0A- MODRM_EAX_06=0A+ = asm volatile (=0A+#ifdef HAVE_GAS_VMX=0A+ "vmclear = %0\n"=0A+#else=0A+ VMCLEAR_OPCODE MODRM_EAX_06=0A+#endif= =0A /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */=0A = UNLIKELY_START(be, vmclear)=0A = "\tud2\n"=0A UNLIKELY_END_SECTION=0A+#ifdef HAVE_GAS_VMX= =0A+ : "+m" (addr)=0A+ :=0A+#else=0A = :=0A : "a" (&addr)=0A+#endif=0A = : "memory");=0A }=0A =0A@@ -325,31 +342,58 @@ static inline = unsigned long __vmread(uns=0A =0A static inline void __vmwrite(unsigned = long field, unsigned long value)=0A {=0A- asm volatile ( VMWRITE_OPCODE= =0A- MODRM_EAX_ECX=0A+ asm volatile (=0A+#ifdef = HAVE_GAS_VMX=0A+ "vmwrite %1, %0\n"=0A+#else=0A+ = VMWRITE_OPCODE MODRM_EAX_ECX=0A+#endif=0A /* = CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */=0A UNLIKELY_ST= ART(be, vmwrite)=0A "\tud2\n"=0A = UNLIKELY_END_SECTION=0A : =0A+#ifdef HAVE_GAS_VMX=0A+ = : "r" (field) , "rm" (value)=0A+#else=0A = : "a" (field) , "c" (value)=0A+#endif=0A : "memory");= =0A }=0A =0A-static inline unsigned long __vmread_safe(unsigned long = field, int *error)=0A-{=0A- unsigned long ecx;=0A-=0A- asm volatile = ( VMREAD_OPCODE=0A- MODRM_EAX_ECX=0A- = /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D -1 */=0A- "setna = %b0 ; neg %0"=0A- : "=3Dq" (*error), "=3Dc" (ecx)=0A- = : "0" (0), "a" (field)=0A- : "memory");=0A= -=0A- return ecx;=0A-}=0A+#ifdef HAVE_GAS_VMX=0A+#define __vmread_safe(f= ield, value) ({ \=0A+ bool_t okay_; \=0A+ if ( sizeof(value) =3D=3D = sizeof(long) ) \=0A+ asm volatile ( "vmread %q2, %1\n\t" \=0A+ = /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D 0 */ \=0A+ = "setnbe %0" \=0A+ : "=3Dqm" (okay_), = "=3Drm" (value) \=0A+ : "r" (field) \=0A+ = : "memory"); \=0A+ else \=0A+ { \=0A+ asm volatile = ( "vmread %q2, %q1\n\t" \=0A+ /* CF=3D=3D1 or = ZF=3D=3D1 --> rc =3D 0 */ \=0A+ "setnbe %0" \=0A+ = : "=3Dqm" (okay_), "=3Dr" (value) \=0A+ = : "r" (field) \=0A+ : "memory"); \=0A+ } = \=0A+ okay_; \=0A+})=0A+#else=0A+#define __vmread_safe(field, value) ({ = \=0A+ bool_t okay_; \=0A+ asm volatile ( VMREAD_OPCODE MODRM_EAX_ECX = \=0A+ /* CF=3D=3D1 or ZF=3D=3D1 --> rc =3D 0 */ \=0A+ = "setnbe %0" \=0A+ : "=3Dqm" (okay_), = "=3Dc" (value) \=0A+ : "a" (field) \=0A+ = : "memory"); \=0A+ okay_; \=0A+})=0A+#endif=0A =0A static inline void = __invept(int type, u64 eptp, u64 gpa)=0A {=0A@@ -365,14 +409,22 @@ static = inline void __invept(int type, u6=0A !cpu_has_vmx_ept_invept_singl= e_context )=0A type =3D INVEPT_ALL_CONTEXT;=0A =0A- asm = volatile ( INVEPT_OPCODE=0A- MODRM_EAX_08=0A+ asm = volatile (=0A+#ifdef HAVE_GAS_EPT=0A+ "invept %0, = %q1\n"=0A+#else=0A+ INVEPT_OPCODE MODRM_EAX_08=0A+#endif= =0A /* CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */=0A = UNLIKELY_START(be, invept)=0A = "\tud2\n"=0A UNLIKELY_END_SECTION=0A = :=0A+#ifdef HAVE_GAS_EPT=0A+ : "m" (operand), "r" = (type)=0A+#else=0A : "a" (&operand), "c" (type)=0A+#endi= f=0A : "memory" );=0A }=0A =0A@@ -385,14 +437,23 @@ = static inline void __invvpid(int type, u=0A } __attribute__ ((packed)) = operand =3D {vpid, 0, gva};=0A =0A /* Fix up #UD exceptions which = occur when TLBs are flushed before VMXON. */=0A- asm volatile ( "1: " = INVVPID_OPCODE MODRM_EAX_08=0A+ asm volatile ( "1: "=0A+#ifdef = HAVE_GAS_EPT=0A+ "invvpid %0, %q1\n"=0A+#else=0A+ = INVVPID_OPCODE MODRM_EAX_08=0A+#endif=0A /* = CF=3D=3D1 or ZF=3D=3D1 --> crash (ud2) */=0A "2: " = UNLIKELY_START(be, invvpid)=0A "\tud2\n"=0A = UNLIKELY_END_SECTION "\n"=0A _ASM_EXTABLE(1b, = 2b)=0A :=0A+#ifdef HAVE_GAS_EPT=0A+ : = "m" (operand), "r" (type)=0A+#else=0A : "a" (&operand), = "c" (type)=0A+#endif=0A : "memory" );=0A }=0A =0A --=__PartF0C3B8B6.1__= 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 --=__PartF0C3B8B6.1__=--