Skip to contentSkip to conversation

Two miscellenous xen-detect patches.

PATCH In review John Haxby 2 participants 9 messages Dec 1, 2014 – Dec 3, 2014

lore
Share & tools
lore

John Haxby

author
#0 lore raw

I needed xen-detect this morning so as usual I pulled it from git and it
didn't work and it threw a compilation warning. Thinking the problem
was the strict-aliasing warning (patch 1) I fixed that and ... it still didn't work. Well, that's not entirely true, it was just over-enthusiastically claiming everything except dom0 was hvm. Patch 2 reverses the order of the pv and hvm checks which works on everything I've tried it with.

jch

author
#1 lore raw

PATCH 1/2 xen-detect: fix strict-aliasing compilation warning.

With gcc 4.8.3, compiling xen-detect gives a compilation warning if you're optimising:

$ cc -Wall -Os xen-detect.c
xen-detect.c: In function ‘check_for_xen’:
xen-detect.c:65:9: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
         *(uint32_t *)(signature + 0) = regs[1];
         ^
---
 tools/misc/xen-detect.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
tools/misc/xen-detect.c +10 -11
index 787b5da..19c66d1 100644
@@ -54,28 +54,27 @@ static void cpuid(uint32_t idx, uint32_t *regs, int pv_context)
 
 static int check_for_xen(int pv_context)
 {
-    uint32_t regs[4];
-    char signature[13];
+    union
+    {
+        uint32_t regs[4];
+        char signature[17];
+    } u;
     uint32_t base;
 
     for ( base = 0x40000000; base < 0x40010000; base += 0x100 )
     {
-        cpuid(base, regs, pv_context);
-
-        *(uint32_t *)(signature + 0) = regs[1];
-        *(uint32_t *)(signature + 4) = regs[2];
-        *(uint32_t *)(signature + 8) = regs[3];
-        signature[12] = '\0';
+        cpuid(base, u.regs, pv_context);
+        u.signature[16] = '\0';
 
-        if ( !strcmp("XenVMMXenVMM", signature) && (regs[0] >= (base + 2)) )
+        if ( !strcmp("XenVMMXenVMM", u.signature+4) && (u.regs[0] >= (base + 2)) )
             goto found;
     }
 
     return 0;
 
  found:
-    cpuid(base + 1, regs, pv_context);
-    return regs[0];
+    cpuid(base + 1, u.regs, pv_context);
+    return u.regs[0];
 }
 
 static jmp_buf sigill_jmp;
Signature · 7 lines
1.9.3


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH 1/2] xen-detect: fix strict-aliasing compilation warning.

Quoted 11 lines · John Haxby

With gcc 4.8.3, compiling xen-detect gives a compilation warning if you're optimising:

$ cc -Wall -Os xen-detect.c
xen-detect.c: In function ‘check_for_xen’:
xen-detect.c:65:9: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
         *(uint32_t *)(signature + 0) = regs[1];
         ^

Why are you compiling without the CFLAGS from the Xen build system?

We explicitly disable strict alias optimisations, because optimisations based upon the aliasing rules in C is mad. Even when you eliminate all the warnings, there are still subtle bugs because the compiler is free to assume a lot more than a programmer would typically deem reasonable.

~Andrew

Quoted 37 lines
---
 tools/misc/xen-detect.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
tools/misc/xen-detect.c +8 -9
index 787b5da..19c66d1 100644
@@ -54,28 +54,27 @@ static void cpuid(uint32_t idx, uint32_t *regs, int pv_context)
 
 static int check_for_xen(int pv_context)
 {
-    uint32_t regs[4];
-    char signature[13];
+    union
+    {
+        uint32_t regs[4];
+        char signature[17];
+    } u;
     uint32_t base;
 
     for ( base = 0x40000000; base < 0x40010000; base += 0x100 )
     {
-        cpuid(base, regs, pv_context);
-
-        *(uint32_t *)(signature + 0) = regs[1];
-        *(uint32_t *)(signature + 4) = regs[2];
-        *(uint32_t *)(signature + 8) = regs[3];
-        signature[12] = '\0';
+        cpuid(base, u.regs, pv_context);
+        u.signature[16] = '\0';
 
-        if ( !strcmp("XenVMMXenVMM", signature) && (regs[0] >= (base + 2)) )
+        if ( !strcmp("XenVMMXenVMM", u.signature+4) && (u.regs[0] >= (base + 2)) )
             goto found;
     }
 

tools/misc/xen-detect.c static void cpuid(uint32_t idx, uint32_t *regs, int pv_context) · line 80

Quoted 10 lines
tools/misc/xen-detect.c +2 -2
     return 0;
 
  found:
-    cpuid(base + 1, regs, pv_context);
-    return regs[0];
+    cpuid(base + 1, u.regs, pv_context);
+    return u.regs[0];
 }
 
 static jmp_buf sigill_jmp;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#3 lore raw

Re: [PATCH 1/2] xen-detect: fix strict-aliasing compilation warning.

Quoted 14 lines · Andrew Cooper
Quoted 10 lines · John Haxby

With gcc 4.8.3, compiling xen-detect gives a compilation warning if you're optimising:

$ cc -Wall -Os xen-detect.c
xen-detect.c: In function ‘check_for_xen’:
xen-detect.c:65:9: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
        *(uint32_t *)(signature + 0) = regs[1];
        ^
Quoted 8 lines
Quoted 1 line
Signed-off-by John Haxby <[email protected] <mailto:[email protected]>>

Why are you compiling without the CFLAGS from the Xen build system?

We explicitly disable strict alias optimisations, because optimisations based upon the aliasing rules in C is mad. Even when you eliminate all the warnings, there are still subtle bugs because the compiler is free to assume a lot more than a programmer would typically deem reasonable.

I wasn’t building the whole system, I just wanted xen-detect so I pulled it out and compiled it; I usually use "-Wall -Os” because the combination finds problems I might otherwise overlook. The patch also removes three lines of code :) but you can take it or leave it as you choose. The other patch — reversing the code of pv and hvm checking — was the real problem.

jch

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH 1/2] xen-detect: fix strict-aliasing compilation warning.

Quoted 26 lines · John Haxby
Quoted 18 lines

On 1 Dec 2014, at 17:15, Andrew Cooper <[email protected] <mailto:[email protected]>> wrote:

Quoted 10 lines · John Haxby

With gcc 4.8.3, compiling xen-detect gives a compilation warning if you're optimising:

$ cc -Wall -Os xen-detect.c
xen-detect.c: In function ‘check_for_xen’:
xen-detect.c:65:9: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
        *(uint32_t *)(signature + 0) = regs[1];
        ^
Quoted 2 lines

<mailto:[email protected]>>

Quoted 6 lines

Why are you compiling without the CFLAGS from the Xen build system?

We explicitly disable strict alias optimisations, because optimisations based upon the aliasing rules in C is mad. Even when you eliminate all the warnings, there are still subtle bugs because the compiler is free to assume a lot more than a programmer would typically deem reasonable.

Quoted 8 lines

I wasn’t building the whole system, I just wanted xen-detect so I pulled it out and compiled it; I usually use "-Wall -Os” because the
combination finds problems I might otherwise overlook. The patch
also removes three lines of code :) but you can take it or leave it as
you choose. The other patch — reversing the code of pv and hvm
checking — was the real problem.

jch

I feel it would be neater to fix this by using the XEN_CPUID_SIGNATURE_E{B,C,D}X constants from the API. This fixes the strict aliasing, and does away with the string handling completely.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#5 lore raw

Re: [PATCH 1/2] xen-detect: fix strict-aliasing compilation warning.

In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper
Quoted 10 lines · John Haxby

With gcc 4.8.3, compiling xen-detect gives a compilation warning if you're optimising:

$ cc -Wall -Os xen-detect.c
xen-detect.c: In function ‘check_for_xen’:
xen-detect.c:65:9: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
         *(uint32_t *)(signature + 0) = regs[1];
         ^
Quoted 8 lines
Quoted 1 line

Why are you compiling without the CFLAGS from the Xen build system?

We explicitly disable strict alias optimisations, because optimisations based upon the aliasing rules in C is mad. Even when you eliminate all the warnings, there are still subtle bugs because the compiler is free to assume a lot more than a programmer would typically deem reasonable.

Do you want me to repost the second patch (the actual bug fix one) so that it doesn't assume the line number changes and whatnot for this one?

jch

Quoted 49 lines

~Andrew

Quoted 46 lines
---
 tools/misc/xen-detect.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
tools/misc/xen-detect.c +10 -11
index 787b5da..19c66d1 100644
@@ -54,28 +54,27 @@ static void cpuid(uint32_t idx, uint32_t *regs, int pv_context)
 
 static int check_for_xen(int pv_context)
 {
-    uint32_t regs[4];
-    char signature[13];
+    union
+    {
+        uint32_t regs[4];
+        char signature[17];
+    } u;
     uint32_t base;
 
     for ( base = 0x40000000; base < 0x40010000; base += 0x100 )
     {
-        cpuid(base, regs, pv_context);
-
-        *(uint32_t *)(signature + 0) = regs[1];
-        *(uint32_t *)(signature + 4) = regs[2];
-        *(uint32_t *)(signature + 8) = regs[3];
-        signature[12] = '\0';
+        cpuid(base, u.regs, pv_context);
+        u.signature[16] = '\0';
 
-        if ( !strcmp("XenVMMXenVMM", signature) && (regs[0] >= (base + 2)) )
+        if ( !strcmp("XenVMMXenVMM", u.signature+4) && (u.regs[0] >= (base + 2)) )
             goto found;
     }
 
     return 0;
 
  found:
-    cpuid(base + 1, regs, pv_context);
-    return regs[0];
+    cpuid(base + 1, u.regs, pv_context);
+    return u.regs[0];
 }
 
Quoted 9 lines

tools/misc/xen-detect.c static void cpuid(uint32_t idx, uint32_t *regs, int pv_context) · line 80

Quoted 1 line
tools/misc/xen-detect.c +0 -0
 static jmp_buf sigill_jmp;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw

Re: [PATCH 1/2] xen-detect: fix strict-aliasing compilation warning.

Quoted 15 lines · John Haxby
Quoted 14 lines · Andrew Cooper
Quoted 11 lines · John Haxby

With gcc 4.8.3, compiling xen-detect gives a compilation warning if you're optimising:

$ cc -Wall -Os xen-detect.c
xen-detect.c: In function ‘check_for_xen’:
xen-detect.c:65:9: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
         *(uint32_t *)(signature + 0) = regs[1];
         ^

Why are you compiling without the CFLAGS from the Xen build system?

Quoted 8 lines
Quoted 4 lines

We explicitly disable strict alias optimisations, because optimisations based upon the aliasing rules in C is mad. Even when you eliminate all the warnings, there are still subtle bugs because the compiler is free to assume a lot more than a programmer would typically deem reasonable.

Do you want me to repost the second patch (the actual bug fix one) so that it doesn't assume the line number changes and whatnot for this one?

jch

With a pragmatic hat on, making more stuff "-Wall" safe is probably better, although production code should use the surrounding infrastructure.

With all of these patches, you must CC the toolstack maintainers. If you believe it should make the cut for 4.5, you must also CC Konrad and argue for a release ack.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#7 lore raw

PATCH 2/2 xen-detect: check for XEN_PV before XEN_HVM

In reply to John Haxby

At some stage, the cpuid instruction used to detect a xen hvm domain also started working in a pv domain so pv domains were being identified as hvm (dom0 excepted). Change the order so that pv is tested for first.

---
 tools/misc/xen-detect.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/misc/xen-detect.c +6 -6
index 19c66d1..c8fccf9 100644
@@ -132,15 +132,10 @@ int main(int argc, char **argv)
         }
     }
 
-    /* Check for execution in HVM context. */
-    detected = XEN_HVM;
-    if ( (version = check_for_xen(0)) != 0 )
-        goto out;
-
     /*
      * Set up a signal handler to test the paravirtualised CPUID instruction.
      * If executed outside Xen PV context, the extended opcode will fault, we
-     * will longjmp via the signal handler, and print "Not running on Xen".
+     * will longjmp via the signal handler, then check for HVM.
      */
     detected = XEN_PV;
     if ( !setjmp(sigill_jmp)
@@ -148,6 +143,11 @@ int main(int argc, char **argv)
          && ((version = check_for_xen(1)) != 0) )
         goto out;
 
+    /* Check for execution in HVM context. */
+    detected = XEN_HVM;
+    if ( (version = check_for_xen(0)) != 0 )
+        goto out;
+
     detected = XEN_NONE;
 
  out:
Signature · 1 line
1.9.3
#8 lore raw

Re: [PATCH 2/2] xen-detect: check for XEN_PV before XEN_HVM

Quoted 6 lines · John Haxby

At some stage, the cpuid instruction used to detect a xen hvm domain also started working in a pv domain so pv domains were being identified as hvm (dom0 excepted). Change the order so that pv is tested for first.

This will have happened as a side effect of Intels CPUID-faulting ability present in IvyBridge servers and later, which permits Xen the ability to intercept regular cpuid instructions.

On the other hand, the forced emulation prefix is now valid in HVM guests in debug Xens with the "hvm_fep" command line option. In that case, the HVM domain would be erroneously identified as PV.

Perhaps it is worth having an explicit guest type available in the cpuid leaves themselves, so guest userspace need not guess at all?

~Andrew