From: "Jan Beulich" Subject: [PATCH 2/2] arm64: fix fls() Date: Thu, 22 Jan 2015 13:38:54 +0000 Message-ID: <54C10B7E0200007800058256@mail.emea.novell.com> References: <54C108910200007800058233@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part7F4AE67E.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YEHyJ-00038Q-V9 for xen-devel@lists.xenproject.org; Thu, 22 Jan 2015 13:38:56 +0000 In-Reply-To: <54C108910200007800058233@mail.emea.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: Ian Campbell , Tim Deegan , Stefano Stabellini 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. --=__Part7F4AE67E.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline It using CLZ on a 64-bit register while specifying the input operand as only 32 bits wide is wrong: An operand intentionally shrunk down to 32 bits at the source level doesn't imply respective zero extension also happens at the machine instruction level, and hence the wrong result could get returned. Add suitable inline assembly abstraction so that the function can remain shared between arm32 and arm64. The need to include asm_defns.h in bitops.h makes it necessary to adjust processor.h though - it is generally wrong to include public headers without making sure that integer types are properly defined. (I didn't innvestigate or try whether the possible alternative of moving the public/arch-arm.h inclusion down in the file would also work.) Signed-off-by: Jan Beulich --- Note: Only compile tested due to lack of suitable ARM64 test environment. --- a/xen/include/asm-arm/asm_defns.h +++ b/xen/include/asm-arm/asm_defns.h @@ -7,6 +7,15 @@ #endif #include =20 +/* For generic assembly code: use macros to define operand sizes. */ +#if defined(CONFIG_ARM_32) +# define __OP32 +#elif defined(CONFIG_ARM_64) +# define __OP32 "w" +#else +# error "unknown ARM variant" +#endif + #endif /* __ARM_ASM_DEFNS_H__ */ /* * Local variables: --- a/xen/include/asm-arm/bitops.h +++ b/xen/include/asm-arm/bitops.h @@ -9,6 +9,8 @@ #ifndef _ARM_BITOPS_H #define _ARM_BITOPS_H =20 +#include + /* * Non-atomic bit manipulation. * @@ -111,9 +113,8 @@ static inline int fls(unsigned int x) if (__builtin_constant_p(x)) return generic_fls(x); =20 - asm("clz\t%0, %1" : "=3Dr" (ret) : "r" (x)); - ret =3D BITS_PER_LONG - ret; - return ret; + asm("clz\t%"__OP32"0, %"__OP32"1" : "=3Dr" (ret) : "r" (x)); + return 32 - ret; } =20 =20 --- a/xen/include/asm-arm/processor.h +++ b/xen/include/asm-arm/processor.h @@ -3,6 +3,9 @@ =20 #include #include +#ifndef __ASSEMBLY__ +#include +#endif #include =20 /* MIDR Main ID Register */ @@ -220,8 +223,6 @@ =20 #ifndef __ASSEMBLY__ =20 -#include - struct cpuinfo_arm { union { uint32_t bits; --=__Part7F4AE67E.1__= Content-Type: text/plain; name="arm64-fls.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="arm64-fls.patch" arm64: fix fls()=0A=0AIt using CLZ on a 64-bit register while specifying = the input operand as=0Aonly 32 bits wide is wrong: An operand intentionally= shrunk down to 32=0Abits at the source level doesn't imply respective = zero extension also=0Ahappens at the machine instruction level, and hence = the wrong result=0Acould get returned.=0A=0AAdd suitable inline assembly = abstraction so that the function can=0Aremain shared between arm32 and = arm64. The need to include asm_defns.h=0Ain bitops.h makes it necessary to = adjust processor.h though - it is=0Agenerally wrong to include public = headers without making sure that=0Ainteger types are properly defined. (I = didn't innvestigate or try=0Awhether the possible alternative of moving = the public/arch-arm.h=0Ainclusion down in the file would also work.)=0A=0AS= igned-off-by: Jan Beulich =0A---=0ANote: Only compile = tested due to lack of suitable ARM64 test environment.=0A=0A--- a/xen/inclu= de/asm-arm/asm_defns.h=0A+++ b/xen/include/asm-arm/asm_defns.h=0A@@ -7,6 = +7,15 @@=0A #endif=0A #include =0A =0A+/* For generic = assembly code: use macros to define operand sizes. */=0A+#if defined(CONFIG= _ARM_32)=0A+# define __OP32=0A+#elif defined(CONFIG_ARM_64)=0A+# define = __OP32 "w"=0A+#else=0A+# error "unknown ARM variant"=0A+#endif=0A+=0A = #endif /* __ARM_ASM_DEFNS_H__ */=0A /*=0A * Local variables:=0A--- = a/xen/include/asm-arm/bitops.h=0A+++ b/xen/include/asm-arm/bitops.h=0A@@ = -9,6 +9,8 @@=0A #ifndef _ARM_BITOPS_H=0A #define _ARM_BITOPS_H=0A = =0A+#include =0A+=0A /*=0A * Non-atomic bit manipulation.= =0A *=0A@@ -111,9 +113,8 @@ static inline int fls(unsigned int x)=0A = if (__builtin_constant_p(x))=0A return generic_fls(x);=0A= =0A- asm("clz\t%0, %1" : "=3Dr" (ret) : "r" (x));=0A- ret = =3D BITS_PER_LONG - ret;=0A- return ret;=0A+ asm("clz\t%"__OP= 32"0, %"__OP32"1" : "=3Dr" (ret) : "r" (x));=0A+ return 32 - = ret;=0A }=0A =0A =0A--- a/xen/include/asm-arm/processor.h=0A+++ b/xen/inclu= de/asm-arm/processor.h=0A@@ -3,6 +3,9 @@=0A =0A #include =0A = #include =0A+#ifndef __ASSEMBLY__=0A+#include = =0A+#endif=0A #include =0A =0A /* MIDR Main ID Register = */=0A@@ -220,8 +223,6 @@=0A =0A #ifndef __ASSEMBLY__=0A =0A-#include = =0A-=0A struct cpuinfo_arm {=0A union {=0A = uint32_t bits;=0A --=__Part7F4AE67E.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 --=__Part7F4AE67E.1__=--