Skip to contentSkip to conversation

x86/vhpet: Fix type size in timer_int_route_valid

PATCH In review Eslam Elnikety 5 participants 12 messages Jul 28, 2020 – Jul 31, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vhpet: Fix type size in timer_int_route_valid

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

---
 xen/arch/x86/hvm/hpet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +1 -1
index ca94e8b453..9afe6e6760 100644
@@ -66,7 +66,7 @@
     MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
 
 #define timer_int_route_valid(h, n) \
-    ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
+    ((1ULL << timer_int_route(h, n)) & timer_int_route_cap(h, n))
 
 static inline uint64_t hpet_read_maincounter(HPETState *h, uint64_t guest_time)
 {
Signature · 1 line
2.16.6
#1 lore raw
Quoted 2 lines · Eslam Elnikety

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

I'm very dull with this things, so forgive me.

Isn't the left side just promoted to an unsigned 64bit value?

Also timer_int_route will strictly be <= 31, which makes the shift safe?

I'm not opposed to switching to use unsigned long, but I think I'm not understanding the issue.

Thanks, Roger.

#2 lore raw

Hi Roger,

Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Eslam Elnikety

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

I'm very dull with this things, so forgive me.

Isn't the left side just promoted to an unsigned 64bit value?

Also timer_int_route will strictly be <= 31, which makes the shift safe?

This is all true. The size mismatch is indeed benign. The patch is only for code sanity.

Quoted 6 lines

I'm not opposed to switching to use unsigned long, but I think I'm not understanding the issue.

Thanks, Roger.

Regards,
Eslam

#3 lore raw
In reply to Eslam Elnikety
Quoted 16 lines · Eslam Elnikety

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

---
 xen/arch/x86/hvm/hpet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +0 -0
index ca94e8b453..9afe6e6760 100644

xen/arch/x86/hvm/hpet.c line 72

Quoted 8 lines
xen/arch/x86/hvm/hpet.c +1 -1
@@ -66,7 +66,7 @@
     MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
 
 #define timer_int_route_valid(h, n) \
-    ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
+    ((1ULL << timer_int_route(h, n)) & timer_int_route_cap(h, n))
 
 static inline uint64_t hpet_read_maincounter(HPETState *h, uint64_t guest_time)
 {

Does this work?

xen/arch/x86/hvm/hpet.c +0 -0
index ca94e8b453..638f6174de 100644
@@ -62,8 +62,7 @@


 #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n), HPET_TN_ROUTE)

-#define timer_int_route_cap(h, n) \
-    MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP) +#define timer_int_route_cap(h, n) (h)->hpet.timers[(n)].route
 #define timer_int_route_valid(h, n) \
     ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))

xen/include/asm-x86/hvm/vpt.h +0 -0
index f0e0eaec83..a41fc443cc 100644
@@ -73,7 +73,13 @@ struct hpet_registers {

     uint64_t isr;               /* interrupt status reg */ uint64_t mc64;              /* main counter */ struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */ +        union {
+            uint64_t config;    /* configuration/cap */ +            struct {
+                uint32_t _;
+                uint32_t route;
+            };
+        };
         uint64_t cmp;           /* comparator */ uint64_t fsb;           /* FSB route, not supported now */ } timers[HPET_TIMER_NUM];

#4 lore raw
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Eslam Elnikety

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

---
  xen/arch/x86/hvm/hpet.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +0 -0
index ca94e8b453..9afe6e6760 100644
@@ -66,7 +66,7 @@
      MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
 
Quoted 8 lines

xen/arch/x86/hvm/hpet.c line 72

Quoted 6 lines
xen/arch/x86/hvm/hpet.c +1 -1
  #define timer_int_route_valid(h, n) \
-    ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
+    ((1ULL << timer_int_route(h, n)) & timer_int_route_cap(h, n))
 
  static inline uint64_t hpet_read_maincounter(HPETState *h, uint64_t guest_time)
  {

Does this work?

Yes! This is better than my fix (and I like that it clarifies the route part of the config. Will you sign-off and send a patch?

Quoted 37 lines
xen/arch/x86/hvm/hpet.c +1 -2
index ca94e8b453..638f6174de 100644
@@ -62,8 +62,7 @@
 
  #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n),
 HPET_TN_ROUTE)
 
-#define timer_int_route_cap(h, n) \
-    MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
+#define timer_int_route_cap(h, n) (h)->hpet.timers[(n)].route
 
  #define timer_int_route_valid(h, n) \

     ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))

xen/include/asm-x86/hvm/vpt.h +7 -1
index f0e0eaec83..a41fc443cc 100644
@@ -73,7 +73,13 @@ struct hpet_registers {
      uint64_t isr;               /* interrupt status reg */
      uint64_t mc64;              /* main counter */
      struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */
+        union {
+            uint64_t config;    /* configuration/cap */
+            struct {
+                uint32_t _;
+                uint32_t route;
+            };
+        };
          uint64_t cmp;           /* comparator */
          uint64_t fsb;           /* FSB route, not supported now */
      } timers[HPET_TIMER_NUM];
#5 lore raw
Quoted 28 lines · Eslam Elnikety
Quoted 12 lines · Andrew Cooper
Quoted 11 lines · Eslam Elnikety

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

---
  xen/arch/x86/hvm/hpet.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

xen/arch/x86/hvm/hpet.c line 71

Quoted 12 lines
xen/arch/x86/hvm/hpet.c +1 -1
index ca94e8b453..9afe6e6760 100644
@@ -66,7 +66,7 @@
       MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
     #define timer_int_route_valid(h, n) \
-    ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
+    ((1ULL << timer_int_route(h, n)) & timer_int_route_cap(h, n))
     static inline uint64_t hpet_read_maincounter(HPETState *h,
 uint64_t guest_time)
   {

Does this work?

Quoted 2 lines

Yes! This is better than my fix (and I like that it clarifies the route part of the config. Will you sign-off and send a patch?

Any chance I can persuade you, or someone else to do this?  Loads of the macros can be removed by filling in proper bitfield names in place of '_', resulting in rather better code.

~Andrew

Quoted 37 lines
Quoted 36 lines
xen/arch/x86/hvm/hpet.c +1 -1
index ca94e8b453..638f6174de 100644
@@ -62,8 +62,7 @@
     #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n),
 HPET_TN_ROUTE)
   -#define timer_int_route_cap(h, n) \
-    MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
+#define timer_int_route_cap(h, n) (h)->hpet.timers[(n)].route
     #define timer_int_route_valid(h, n) \
       ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
xen/include/asm-x86/hvm/vpt.h +0 -0
index f0e0eaec83..a41fc443cc 100644
@@ -73,7 +73,13 @@ struct hpet_registers {

      uint64_t isr;               /* interrupt status reg */ uint64_t mc64;              /* main counter */ struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */ +        union {
+            uint64_t config;    /* configuration/cap */ +            struct {
+                uint32_t _;
+                uint32_t route;
+            };
+        };
          uint64_t cmp;           /* comparator */ uint64_t fsb;           /* FSB route, not supported now */ } timers[HPET_TIMER_NUM];

#6 lore raw
Quoted 29 lines · Andrew Cooper
Quoted 28 lines · Eslam Elnikety
Quoted 26 lines · Andrew Cooper
Quoted 16 lines · Eslam Elnikety

The macro timer_int_route_cap evalutes to a 64 bit value. Extend the size of left side of timer_int_route_valid to match.

This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc.

---
   xen/arch/x86/hvm/hpet.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +0 -0
index ca94e8b453..9afe6e6760 100644

xen/arch/x86/hvm/hpet.c line 71

Quoted 7 lines
xen/arch/x86/hvm/hpet.c +1 -1
@@ -66,7 +66,7 @@
       MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
     #define timer_int_route_valid(h, n) \
-    ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
+    ((1ULL << timer_int_route(h, n)) & timer_int_route_cap(h, n))
     static inline uint64_t hpet_read_maincounter(HPETState *h,
 uint64_t guest_time)
   {

Does this work?

Quoted 9 lines
Quoted 2 lines

Yes! This is better than my fix (and I like that it clarifies the route part of the config. Will you sign-off and send a patch?

Any chance I can persuade you, or someone else to do this?  Loads of the macros can be removed by filling in proper bitfield names in place of '_', resulting in rather better code.

~Andrew

Sure, I can send a patch for this one occurrence at hand right away -- and I will keep my eye on the general pattern. Since the patch will be mostly your diff, please send your sign-off (or another tag as you see fit).

Thanks,
Eslam

Quoted 39 lines
Quoted 37 lines
Quoted 36 lines
xen/arch/x86/hvm/hpet.c +1 -1
index ca94e8b453..638f6174de 100644
@@ -62,8 +62,7 @@
     #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n),
 HPET_TN_ROUTE)
   -#define timer_int_route_cap(h, n) \
-    MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
+#define timer_int_route_cap(h, n) (h)->hpet.timers[(n)].route
     #define timer_int_route_valid(h, n) \
       ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n))
xen/include/asm-x86/hvm/vpt.h +7 -1
index f0e0eaec83..a41fc443cc 100644
@@ -73,7 +73,13 @@ struct hpet_registers {
       uint64_t isr;               /* interrupt status reg */
       uint64_t mc64;              /* main counter */
       struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */
+        union {
+            uint64_t config;    /* configuration/cap */
+            struct {
+                uint32_t _;
+                uint32_t route;
+            };
+        };
           uint64_t cmp;           /* comparator */
           uint64_t fsb;           /* FSB route, not supported now */
       } timers[HPET_TIMER_NUM];
#7 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

Does this work?

xen/arch/x86/hvm/hpet.c +0 -0
index ca94e8b453..638f6174de 100644

xen/arch/x86/hvm/hpet.c line 66

Quoted 7 lines
xen/arch/x86/hvm/hpet.c +1 -2
@@ -62,8 +62,7 @@
 
  #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n),
 HPET_TN_ROUTE)
 
-#define timer_int_route_cap(h, n) \
-    MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
+#define timer_int_route_cap(h, n) (h)->hpet.timers[(n)].route

Seeing that this is likely the route taken here, and hence to avoid an extra round trip, two remarks: Here I see no need for the parentheses inside the square brackets.

Quoted 6 lines
xen/include/asm-x86/hvm/vpt.h +0 -0
index f0e0eaec83..a41fc443cc 100644
@@ -73,7 +73,13 @@ struct hpet_registers {
      uint64_t isr;               /* interrupt status reg */

xen/include/asm-x86/hvm/vpt.h struct hpet_registers { · line 82

Quoted 10 lines
xen/include/asm-x86/hvm/vpt.h +7 -1
      uint64_t mc64;              /* main counter */
      struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */
+        union {
+            uint64_t config;    /* configuration/cap */
+            struct {
+                uint32_t _;
+                uint32_t route;
+            };
+        };

So long as there are no static initializers for this construct that would then suffer the old-gcc problem, this is of course a fine arrangement to make.

Jan

#8 lore raw
Quoted 18 lines · Jan Beulich

xen/arch/x86/hvm/hpet.c line 64

Quoted 12 lines · Andrew Cooper

Does this work?

xen/arch/x86/hvm/hpet.c +1 -2
index ca94e8b453..638f6174de 100644
@@ -62,8 +62,7 @@
   #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n),
 HPET_TN_ROUTE)
-#define timer_int_route_cap(h, n) \
-    MASK_EXTR(timer_config(h, n), HPET_TN_INT_ROUTE_CAP)
+#define timer_int_route_cap(h, n) (h)->hpet.timers[(n)].route

Seeing that this is likely the route taken here, and hence to avoid an extra round trip, two remarks: Here I see no need for the parentheses inside the square brackets.

Will take of this in v2.

Quoted 12 lines
Quoted 12 lines
xen/include/asm-x86/hvm/vpt.h +0 -0
index f0e0eaec83..a41fc443cc 100644
@@ -73,7 +73,13 @@ struct hpet_registers {

      uint64_t isr;               /* interrupt status reg */ uint64_t mc64;              /* main counter */ struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */ +        union {
+            uint64_t config;    /* configuration/cap */ +            struct {

Quoted 9 lines
Quoted 4 lines

+                uint32_t _;
+                uint32_t route;
+            };
+        };

So long as there are no static initializers for this construct that would then suffer the old-gcc problem, this is of course a fine arrangement to make.

I have to admit that I have no clue what the "old-gcc" problem is. I am curious, and I would appreciate pointers to figure out if/how to resolve. Is that an old, existing problem? Or a problem that was present in older versions of gcc? If the latter, is that a gcc version that we still care about? Thanks, Jan.

-- Eslam

Quoted 2 lines

Jan

#9 lore raw
Quoted 21 lines · Eslam Elnikety
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
xen/include/asm-x86/hvm/vpt.h +0 -0
@@ -73,7 +73,13 @@ struct hpet_registers {

      uint64_t isr;               /* interrupt status reg */ uint64_t mc64;              /* main counter */ struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */ +        union {
+            uint64_t config;    /* configuration/cap */ +            struct {

Quoted 8 lines
Quoted 4 lines

+                uint32_t _;
+                uint32_t route;
+            };
+        };

So long as there are no static initializers for this construct that would then suffer the old-gcc problem, this is of course a fine arrangement to make.

Quoted 4 lines

I have to admit that I have no clue what the "old-gcc" problem is. I am curious, and I would appreciate pointers to figure out if/how to resolve. Is that an old, existing problem? Or a problem that was present in older versions of gcc?

Well, as already said - the problem is with old gcc not dealing well with initializers of structs/unions with unnamed fields.

Quoted 1 line

If the latter, is that a gcc version that we still care about?

Until someone makes a (justified) proposal what the new minimum version(s) ought to be, I'm afraid we still have to care. This topic came up very recently in another context, and I've proposed to put it on the agenda of the next community call.

Jan

#10 lore raw

Hi Jan,

Quoted 22 lines · Jan Beulich
Quoted 21 lines · Eslam Elnikety
Quoted 19 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
xen/include/asm-x86/hvm/vpt.h +0 -0
@@ -73,7 +73,13 @@ struct hpet_registers {
       uint64_t isr;               /* interrupt status reg */

xen/include/asm-x86/hvm/vpt.h struct hpet_registers { · line 82

Quoted 10 lines
xen/include/asm-x86/hvm/vpt.h +7 -1
       uint64_t mc64;              /* main counter */
       struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */
+        union {
+            uint64_t config;    /* configuration/cap */
+            struct {
+                uint32_t _;
+                uint32_t route;
+            };
+        };

So long as there are no static initializers for this construct that would then suffer the old-gcc problem, this is of course a fine arrangement to make.

Quoted 7 lines
Quoted 4 lines

I have to admit that I have no clue what the "old-gcc" problem is. I am curious, and I would appreciate pointers to figure out if/how to resolve. Is that an old, existing problem? Or a problem that was present in older versions of gcc?

Well, as already said - the problem is with old gcc not dealing well with initializers of structs/unions with unnamed fields.

You seem to know quite well the problem. So would you mind to give us more details on which GCC version is believed to be broken?

Quoted 7 lines
Quoted 2 lines

If the latter, is that a gcc version that we still care about?

Until someone makes a (justified) proposal what the new minimum version(s) ought to be, I'm afraid we still have to care. This topic came up very recently in another context, and I've proposed to put it on the agenda of the next community call.

I don't think Eslam was requesting to change the limits. He was just asking whether one of the compiler we support is affected.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw
Quoted 30 lines · Julien Grall

Hi Jan,

Quoted 27 lines · Jan Beulich
Quoted 25 lines · Eslam Elnikety
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
xen/include/asm-x86/hvm/vpt.h +3 -1
@@ -73,7 +73,13 @@ struct hpet_registers {
       uint64_t isr;               /* interrupt status reg */
       uint64_t mc64;              /* main counter */
       struct {                    /* timers */
-        uint64_t config;        /* configuration/cap */
+        union {
+            uint64_t config;    /* configuration/cap */
+            struct {
Quoted 8 lines

xen/include/asm-x86/hvm/vpt.h struct hpet_registers { · line 82

Quoted 4 lines
xen/include/asm-x86/hvm/vpt.h +4 -0
+                uint32_t _;
+                uint32_t route;
+            };
+        };

So long as there are no static initializers for this construct that would then suffer the old-gcc problem, this is of course a fine arrangement to make.

I have to admit that I have no clue what the "old-gcc" problem is. I am curious, and I would appreciate pointers to figure out if/how to resolve. Is that an old, existing problem? Or a problem that was present in older versions of gcc?

Quoted 5 lines
Quoted 2 lines

Well, as already said - the problem is with old gcc not dealing well with initializers of structs/unions with unnamed fields.

You seem to know quite well the problem. So would you mind to give us more details on which GCC version is believed to be broken?

I don't recall for sure, but iirc anything before 4.4.

Jan