Skip to contentSkip to conversation

libx86: Work around GCC bug with ebx output constrants

PATCH Superseded Andrew Cooper 3 participants 16 messages Nov 19, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libx86: Work around GCC bug with ebx output constrants

Some versions of GCC can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/include/xen/lib/x86/cpuid.h | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
xen/include/xen/lib/x86/cpuid.h +31 -4
index 266c910..77f63d5 100644
@@ -20,21 +20,48 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
+/*
+ * Some versions of GCC are unable to cope with preserving the GOT (held in
+ * %ebx) around an asm() with an %ebx output constraint, and produce a rather
+ * cryptic error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * Experimentally, 64-bit builds work correctly, and 32-bit builds on GCC 6 or
+ * later work correctly.
+ *
+ * To work around the issue, use a separate register to hold the the ebx
+ * output, and xchg twice to leave %ebx preserved around the asm() statement.
+ */
+#if __PIC__ && __i386__ && __GNUC__ < 6 && !__clang__
+#define XCHG_BX "xchg %%ebx, %k[bx];"
+#define BX_CON [bx] "=&r"
+#else
+#define XCHG_BX ""
+#define BX_CON "=b"
+#endif
+
 static inline void cpuid_leaf(uint32_t leaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=d" (l->d)
           : "a" (leaf) );
 }
 
 static inline void cpuid_count_leaf(
     uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=d" (l->d)
           : "a" (leaf), "c" (subleaf) );
 }
 
+#undef BX_CON
+#undef XCHG
+
 #define CPUID_GUEST_NR_BASIC      (0xdu + 1)
 #define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_CACHE      (5u + 1)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 8 lines · Andrew Cooper

Some versions of GCC can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

Quoted 5 lines

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

Actually, having just got GCC 5 working, that is also fine.  I'll adjust the wording/check, but won't bother posting a v2 if that is the only change.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Andrew Cooper

Some versions of GCC can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

Quoted 8 lines
Quoted 5 lines

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

Actually, having just got GCC 5 working, that is also fine. I'll adjust the wording/check, but won't bother posting a v2 if that is the only change.

I wonder if it has anything to do with this:

"Reuse of the PIC hard register, instead of using a fixed register, was implemented on x86/x86-64 targets. This improves generated PIC code performance as more hard registers can be used. Shared libraries can significantly benefit from this optimization. Currently it is switched on only for x86/x86-64 targets. As RA infrastructure is already implemented for PIC register reuse, other targets might follow this in the future."

https://gcc.gnu.org/gcc-5/changes.html

Signature · 7 lines
Mihai Donțu


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw
Quoted 19 lines · Mihai Donțu
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Andrew Cooper

Some versions of GCC can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

Quoted 7 lines
Quoted 5 lines

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

Actually, having just got GCC 5 working, that is also fine. I'll adjust the wording/check, but won't bother posting a v2 if that is the only change.

I wonder if it has anything to do with this:

Quoted 7 lines

"Reuse of the PIC hard register, instead of using a fixed register, was implemented on x86/x86-64 targets. This improves generated PIC code performance as more hard registers can be used. Shared libraries can significantly benefit from this optimization. Currently it is switched on only for x86/x86-64 targets. As RA infrastructure is already implemented for PIC register reuse, other targets might follow this in the future."

That is almost certainly it.  Looks like it is down to the pre 5.0 code considering %rBX non-spillable

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54232

Thanks for digging this out.  I missed it while scanning the 5 changelog.  I think I'll tweak the text to reflect this.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v1 1/1
#4 lore raw
In reply to Andrew Cooper
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 14:11, <[email protected]> wrote:

Some versions of GCC can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

And indeed the version I've seen this with (4.3; I wrongly said "newer" on irc) does slightly better: "can't find a register in class 'BREG' while reloading 'asm'" (followed again by an "impossible constraint" one).

Quoted 5 lines

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

I don't think there's anything curious here: The GOT (or actually .rodata here) gets accessed by %rip-relative addressing in small model 64-bit code, iirc.

Quoted 9 lines
xen/include/xen/lib/x86/cpuid.h +3 -0
@@ -20,21 +20,48 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
+/*
+ * Some versions of GCC are unable to cope with preserving the GOT (held in
+ * %ebx) around an asm() with an %ebx output constraint, and produce a rather

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 35

Quoted 10 lines
xen/include/xen/lib/x86/cpuid.h +10 -0
+ * cryptic error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * Experimentally, 64-bit builds work correctly, and 32-bit builds on GCC 6 or
+ * later work correctly.
+ *
+ * To work around the issue, use a separate register to hold the the ebx
+ * output, and xchg twice to leave %ebx preserved around the asm() statement.
+ */
+#if __PIC__ && __i386__ && __GNUC__ < 6 && !__clang__

I think you're missing a bunch of defined() here.

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 36

Quoted 1 line
xen/include/xen/lib/x86/cpuid.h +1 -0
+#define XCHG_BX "xchg %%ebx, %k[bx];"

I don't think the k modifier is actually needed?

Quoted 4 lines
xen/include/xen/lib/x86/cpuid.h +4 -0
+#define BX_CON [bx] "=&r"
+#else
+#define XCHG_BX ""
+#define BX_CON "=b"

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 48

Quoted 10 lines
xen/include/xen/lib/x86/cpuid.h +6 -2
+#endif
+
 static inline void cpuid_leaf(uint32_t leaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=d" (l->d)

Strictly speaking all other outputs also need to use =& in the 32-bit case. But I wouldn't insist on such an adjustment. With the others done

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 12 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 14:11, <[email protected]> wrote:

Some versions of GCC can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

And indeed the version I've seen this with (4.3; I wrongly said "newer" on irc) does slightly better: "can't find a register in class 'BREG' while reloading 'asm'" (followed again by an "impossible constraint" one).

Quoted 8 lines
Quoted 5 lines

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

I don't think there's anything curious here: The GOT (or actually .rodata here) gets accessed by %rip-relative addressing in small model 64-bit code, iirc.

Aah - good point, and also explains the other conditionals in GCC's intrinsic header.

I'll update the logic to cope with the larger 64bit models.

Quoted 7 lines
Quoted 7 lines
xen/include/xen/lib/x86/cpuid.h +0 -0
@@ -20,21 +20,48 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
Quoted 14 lines

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 35

Quoted 13 lines
xen/include/xen/lib/x86/cpuid.h +13 -0
+/*
+ * Some versions of GCC are unable to cope with preserving the GOT (held in
+ * %ebx) around an asm() with an %ebx output constraint, and produce a rather
+ * cryptic error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * Experimentally, 64-bit builds work correctly, and 32-bit builds on GCC 6 or
+ * later work correctly.
+ *
+ * To work around the issue, use a separate register to hold the the ebx
+ * output, and xchg twice to leave %ebx preserved around the asm() statement.
+ */
+#if __PIC__ && __i386__ && __GNUC__ < 6 && !__clang__

I think you're missing a bunch of defined() here.

Am I?  Undefined symbols behave as if they had the value 0 in an #if

Quoted 3 lines

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 36

Quoted 2 lines
xen/include/xen/lib/x86/cpuid.h +1 -0
+#define XCHG_BX "xchg %%ebx, %k[bx];"

I don't think the k modifier is actually needed?

Perhaps not, but the 64bit version will need a q.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:08, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 14:11, <[email protected]> wrote:
xen/include/xen/lib/x86/cpuid.h +0 -0
@@ -20,21 +20,48 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
Quoted 14 lines

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 35

Quoted 13 lines
xen/include/xen/lib/x86/cpuid.h +13 -0
+/*
+ * Some versions of GCC are unable to cope with preserving the GOT (held in
+ * %ebx) around an asm() with an %ebx output constraint, and produce a rather
+ * cryptic error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * Experimentally, 64-bit builds work correctly, and 32-bit builds on GCC 6 or
+ * later work correctly.
+ *
+ * To work around the issue, use a separate register to hold the the ebx
+ * output, and xchg twice to leave %ebx preserved around the asm() statement.
+ */
+#if __PIC__ && __i386__ && __GNUC__ < 6 && !__clang__

I think you're missing a bunch of defined() here.

Quoted 1 line

Am I? Undefined symbols behave as if they had the value 0 in an #if

But compilers may warn in such cases. Or else there would almost never be a need to use defined().

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
In reply to Andrew Cooper
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:08, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 14:11, <[email protected]> wrote:

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

Quoted 8 lines
Quoted 3 lines

I don't think there's anything curious here: The GOT (or actually .rodata here) gets accessed by %rip-relative addressing in small model 64-bit code, iirc.

Aah - good point, and also explains the other conditionals in GCC's intrinsic header.

I'll update the logic to cope with the larger 64bit models.

Do you need to? The ABI specifies that the GOT pointer lives in %r15 in that case.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:08, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 14:11, <[email protected]> wrote:

In practice, this is a collision between the output constraint and the GOT which is held in %ebx when compiling with -fPIC for libraries.

This affects at least GCC 4.9 as shipped in Debian Jessie, but experimentally is fixed in GCC 6 and later. Curiously, it only affects 32-bit builds.

I don't think there's anything curious here: The GOT (or actually .rodata here) gets accessed by %rip-relative addressing in small model 64-bit code, iirc.

Quoted 6 lines
Quoted 4 lines

Aah - good point, and also explains the other conditionals in GCC's intrinsic header.

I'll update the logic to cope with the larger 64bit models.

Do you need to? The ABI specifies that the GOT pointer lives in %r15 in that case.

I've checked, and gcc 4.9 complains equally when using -mcmodel=large

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 48

Quoted 8 lines

static inline void cpuid_leaf(uint32_t leaf, struct cpuid_leaf *l) {

xen/include/xen/lib/x86/cpuid.h +4 -2
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=d" (l->d)
Quoted 4 lines

Strictly speaking all other outputs also need to use =& in the 32-bit case. But I wouldn't insist on such an adjustment. With the others done

Using =& for the constraints which also have inputs results "operand has impossible constraints"

It should only matter for the output-only operands, to prevent GCC allocating %[er]dx for the bx constraint, but that shouldn't matter anyway because =&r can't be allocated to conflict with =d

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:33, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 8 lines

static inline void cpuid_leaf(uint32_t leaf, struct cpuid_leaf *l) {

xen/include/xen/lib/x86/cpuid.h +4 -2
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=d" (l->d)
Quoted 7 lines
Quoted 4 lines

Strictly speaking all other outputs also need to use =& in the 32-bit case. But I wouldn't insist on such an adjustment. With the others done

Using =& for the constraints which also have inputs results "operand has impossible constraints"

Yes, I had (silently) assumed this would be the case.

Quoted 3 lines

It should only matter for the output-only operands, to prevent GCC allocating %[er]dx for the bx constraint, but that shouldn't matter anyway because =&r can't be allocated to conflict with =d

Hence me having said "strictly speaking".

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

PATCH v2 1/1 libx86: Work around GCC bug with ebx output constrants

In reply to Andrew Cooper

Versions of GCC before 5 can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* GCC 5 is fine. Its cpuid instrinct has none of the PIC workarounds thant 4.9 have.
* Fix 64bit builds with larger models.

---
 xen/include/xen/lib/x86/cpuid.h | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
xen/include/xen/lib/x86/cpuid.h +33 -4
index 266c910..cc30654 100644
@@ -20,21 +20,50 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
+/*
+ * Versions of GCC before 5 are unable to cope with %rBX output constraints
+ * when compiling Position Independent Code, and produce a rather cryptic
+ * error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * To work around the issue, use a separate register to hold the the %rBX
+ * output, and xchg twice to leave %rBX preserved around the asm() statement.
+ */
+#if defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && defined(__i386__)
+# define XCHG_BX "xchg %%ebx, %[bx];"
+# define BX_CON [bx] "=&r"
+#elif defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && \
+    defined(__x86_64__) && (defined(__code_model_medium__) || \
+                            defined(__code_model_large__))
+# define XCHG_BX "xchg %%rbx, %q[bx];"
+# define BX_CON [bx] "=&r"
+#else
+# define XCHG_BX ""
+# define BX_CON "=&b"
+#endif
+
 static inline void cpuid_leaf(uint32_t leaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=&c" (l->c), "=&d" (l->d)
           : "a" (leaf) );
 }
 
 static inline void cpuid_count_leaf(
     uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=&d" (l->d)
           : "a" (leaf), "c" (subleaf) );
 }
 
+#undef BX_CON
+#undef XCHG
+
 #define CPUID_GUEST_NR_BASIC      (0xdu + 1)
 #define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_CACHE      (5u + 1)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/1
#12 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:45, <[email protected]> wrote:

Versions of GCC before 5 can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* GCC 5 is fine. Its cpuid instrinct has none of the PIC workarounds thant 4.9 have.
* Fix 64bit builds with larger models.

It is rather odd that 64-bit is also affected - the error gets raised even when there's no use of %rbx for GOT accesses. By when they need a callee-saved register, they indeed use %rbx instead to the ABI-suggested %r15.

Quoted 16 lines
xen/include/xen/lib/x86/cpuid.h +10 -0
@@ -20,21 +20,50 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
+/*
+ * Versions of GCC before 5 are unable to cope with %rBX output constraints
+ * when compiling Position Independent Code, and produce a rather cryptic
+ * error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * To work around the issue, use a separate register to hold the the %rBX
+ * output, and xchg twice to leave %rBX preserved around the asm() statement.
+ */
+#if defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && defined(__i386__)

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 42

Quoted 10 lines
xen/include/xen/lib/x86/cpuid.h +10 -0
+# define XCHG_BX "xchg %%ebx, %[bx];"
+# define BX_CON [bx] "=&r"
+#elif defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && \
+    defined(__x86_64__) && (defined(__code_model_medium__) || \
+                            defined(__code_model_large__))
+# define XCHG_BX "xchg %%rbx, %q[bx];"
+# define BX_CON [bx] "=&r"
+#else
+# define XCHG_BX ""
+# define BX_CON "=&b"

The & is unnecessary here I think. Preferably with it dropped

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw
Quoted 51 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:45, <[email protected]> wrote:

Versions of GCC before 5 can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* GCC 5 is fine. Its cpuid instrinct has none of the PIC workarounds thant 4.9 have.
* Fix 64bit builds with larger models.

It is rather odd that 64-bit is also affected - the error gets raised even when there's no use of %rbx for GOT accesses. By when they need a callee-saved register, they indeed use %rbx instead to the ABI-suggested %r15.

Quoted 16 lines
xen/include/xen/lib/x86/cpuid.h +10 -0
@@ -20,21 +20,50 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
+/*
+ * Versions of GCC before 5 are unable to cope with %rBX output constraints
+ * when compiling Position Independent Code, and produce a rather cryptic
+ * error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * To work around the issue, use a separate register to hold the the %rBX
+ * output, and xchg twice to leave %rBX preserved around the asm() statement.
+ */
+#if defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && defined(__i386__)

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 42

Quoted 10 lines
xen/include/xen/lib/x86/cpuid.h +10 -0
+# define XCHG_BX "xchg %%ebx, %[bx];"
+# define BX_CON [bx] "=&r"
+#elif defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && \
+    defined(__x86_64__) && (defined(__code_model_medium__) || \
+                            defined(__code_model_large__))
+# define XCHG_BX "xchg %%rbx, %q[bx];"
+# define BX_CON [bx] "=&r"
+#else
+# define XCHG_BX ""
+# define BX_CON "=&b"

The & is unnecessary here I think. Preferably with it dropped

Quoted 1 line

That was to match the "=&d" and friends.  I'd prefer to be consistent (one way or the other).

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw
Quoted 58 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 16:19, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.11.18 at 15:45, <[email protected]> wrote:

Versions of GCC before 5 can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints

in an ‘asm’

Quoted 11 lines
Quoted 11 lines

asm ( "cpuid"
^

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* GCC 5 is fine. Its cpuid instrinct has none of the PIC workarounds thant

4.9 have.

Quoted 12 lines
Quoted 1 line

* Fix 64bit builds with larger models.

It is rather odd that 64-bit is also affected - the error gets raised even when there's no use of %rbx for GOT accesses. By when they need a callee-saved register, they indeed use %rbx instead to the ABI-suggested %r15.

Quoted 6 lines
xen/include/xen/lib/x86/cpuid.h +0 -0
@@ -20,21 +20,50 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
Quoted 8 lines

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf

Quoted 8 lines
xen/include/xen/lib/x86/cpuid.h +8 -0
+/*
+ * Versions of GCC before 5 are unable to cope with %rBX output constraints
+ * when compiling Position Independent Code, and produce a rather cryptic
+ * error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * To work around the issue, use a separate register to hold the the %rBX
+ * output, and xchg twice to leave %rBX preserved around the asm() 

statement.

Quoted 13 lines

xen/include/xen/lib/x86/cpuid.h struct cpuid_leaf · line 42

Quoted 12 lines
xen/include/xen/lib/x86/cpuid.h +12 -0
+ */
+#if defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && defined(__i386__)
+# define XCHG_BX "xchg %%ebx, %[bx];"
+# define BX_CON [bx] "=&r"
+#elif defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && \
+    defined(__x86_64__) && (defined(__code_model_medium__) || \
+                            defined(__code_model_large__))
+# define XCHG_BX "xchg %%rbx, %q[bx];"
+# define BX_CON [bx] "=&r"
+#else
+# define XCHG_BX ""
+# define BX_CON "=&b"

The & is unnecessary here I think. Preferably with it dropped

Quoted 1 line
Quoted 2 lines

That was to match the "=&d" and friends. I'd prefer to be consistent (one way or the other).

Ah, well, yes - that's fine then.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

PATCH v3 1/1 libx86: Work around GCC being unable to spill the PIC hard register

In reply to Andrew Cooper

Versions of GCC before 5 can't compile cpuid.c, and fail with the rather cryptic:

In file included from lib/x86/cpuid.c:3:0:
lib/x86/cpuid.c: In function ‘x86_cpuid_policy_fill_native’:
include/xen/lib/x86/cpuid.h:25:5: error: inconsistent operand constraints in an ‘asm’
asm ( "cpuid"
^

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54232 for more details.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* GCC 5 is fine. Its cpuid instrinct has none of the PIC workarounds thant 4.9 have.
* Fix 64bit builds with larger models.
v3:
* Reference the bugzilla entry which fixed this.

---
 xen/include/xen/lib/x86/cpuid.h | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)
xen/include/xen/lib/x86/cpuid.h +34 -4
index 266c910..22d43ef 100644
@@ -20,21 +20,51 @@ struct cpuid_leaf
     uint32_t a, b, c, d;
 };
 
+/*
+ * Versions of GCC before 5 unconditionally reserve %rBX as the PIC hard
+ * register, and are unable to cope with spilling it.  This results in a
+ * rather cryptic error:
+ *    error: inconsistent operand constraints in an ‘asm’
+ *
+ * In affected situations, work around the issue by using a separate register
+ * to hold the the %rBX output, and xchg twice to leave %rBX preserved around
+ * the asm() statement.
+ */
+#if defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && defined(__i386__)
+# define XCHG_BX "xchg %%ebx, %[bx];"
+# define BX_CON [bx] "=&r"
+#elif defined(__PIC__) && __GNUC__ < 5 && !defined(__clang__) && \
+    defined(__x86_64__) && (defined(__code_model_medium__) || \
+                            defined(__code_model_large__))
+# define XCHG_BX "xchg %%rbx, %q[bx];"
+# define BX_CON [bx] "=&r"
+#else
+# define XCHG_BX ""
+# define BX_CON "=&b"
+#endif
+
 static inline void cpuid_leaf(uint32_t leaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=&c" (l->c), "=&d" (l->d)
           : "a" (leaf) );
 }
 
 static inline void cpuid_count_leaf(
     uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *l)
 {
-    asm ( "cpuid"
-          : "=a" (l->a), "=b" (l->b), "=c" (l->c), "=d" (l->d)
+    asm ( XCHG_BX
+          "cpuid;"
+          XCHG_BX
+          : "=a" (l->a), BX_CON (l->b), "=c" (l->c), "=&d" (l->d)
           : "a" (leaf), "c" (subleaf) );
 }
 
+#undef BX_CON
+#undef XCHG
+
 #define CPUID_GUEST_NR_BASIC      (0xdu + 1)
 #define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_CACHE      (5u + 1)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel