Skip to contentSkip to conversation

cpu_defs: Simplify CPUTLB padding logic

PATCH Superseded Peter Crosthwaite 4 participants 7 messages Jul 5, 2015 – Jul 6, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu_defs: Simplify CPUTLB padding logic

There was a complicated subtractive arithmetic for determining the padding on the CPUTLBEntry structure. Simplify this with a union.

---
 include/exec/cpu-defs.h | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +12 -11
index 98b9cff..5093be2 100644
@@ -105,17 +105,18 @@ typedef struct CPUTLBEntry {
        bit 3                      : indicates that the entry is invalid
        bit 2..0                   : zero
     */
-    target_ulong addr_read;
-    target_ulong addr_write;
-    target_ulong addr_code;
-    /* Addend to virtual address to get host address.  IO accesses
-       use the corresponding iotlb value.  */
-    uintptr_t addend;
-    /* padding to get a power of two size */
-    uint8_t dummy[(1 << CPU_TLB_ENTRY_BITS) -
-                  (sizeof(target_ulong) * 3 +
-                   ((-sizeof(target_ulong) * 3) & (sizeof(uintptr_t) - 1)) +
-                   sizeof(uintptr_t))];
+    union {
+        struct {
+            target_ulong addr_read;
+            target_ulong addr_write;
+            target_ulong addr_code;
+            /* Addend to virtual address to get host address.  IO accesses
+               use the corresponding iotlb value.  */
+            uintptr_t addend;
+        };
+        /* padding to get a power of two size */
+        uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
+    };
 } CPUTLBEntry;
 
 QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
Signature · 1 line
1.9.1
#1 lore raw
Quoted 18 lines · Peter Crosthwaite

There was a complicated subtractive arithmetic for determining the padding on the CPUTLBEntry structure. Simplify this with a union.

---
 include/exec/cpu-defs.h | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +0 -2
index 98b9cff..5093be2 100644
@@ -105,17 +105,18 @@ typedef struct CPUTLBEntry {
        bit 3                      : indicates that the entry is invalid
        bit 2..0                   : zero
     */
-    target_ulong addr_read;
-    target_ulong addr_write;

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 108

Quoted 10 lines
include/exec/cpu-defs.h +1 -9
-    target_ulong addr_code;
-    /* Addend to virtual address to get host address.  IO accesses
-       use the corresponding iotlb value.  */
-    uintptr_t addend;
-    /* padding to get a power of two size */
-    uint8_t dummy[(1 << CPU_TLB_ENTRY_BITS) -
-                  (sizeof(target_ulong) * 3 +
-                   ((-sizeof(target_ulong) * 3) & (sizeof(uintptr_t) - 1)) +
-                   sizeof(uintptr_t))];
+    union {

The struct CPUTLBEntry can be changed to union CPUTLBEntry directly, with no need for the anonymous struct.

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 116

Quoted 8 lines
include/exec/cpu-defs.h +8 -0
+        struct {
+            target_ulong addr_read;
+            target_ulong addr_write;
+            target_ulong addr_code;
+            /* Addend to virtual address to get host address.  IO accesses
+               use the corresponding iotlb value.  */
+            uintptr_t addend;
+        };

Which compiler version started implementing anonymous structs?

Or can we just add

	 __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)))

to the struct? I'm not sure if it affects the sizeof too, so that requires some care. Alternatively, an

	uint8_t padding[0]
		__attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)));

could maybe work? Neither is exactly the same, as they also bump the alignment of the overall struct, but they do not require anonymous structs.

Paolo

Quoted 7 lines
include/exec/cpu-defs.h +3 -0
+        /* padding to get a power of two size */
+        uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
+    };
 } CPUTLBEntry;
 
 QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
#2 lore raw
Quoted 26 lines · Paolo Bonzini
Quoted 23 lines · Peter Crosthwaite

There was a complicated subtractive arithmetic for determining the padding on the CPUTLBEntry structure. Simplify this with a union.

---
 include/exec/cpu-defs.h | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +0 -7
index 98b9cff..5093be2 100644
@@ -105,17 +105,18 @@ typedef struct CPUTLBEntry {
        bit 3                      : indicates that the entry is invalid
        bit 2..0                   : zero
     */
-    target_ulong addr_read;
-    target_ulong addr_write;
-    target_ulong addr_code;
-    /* Addend to virtual address to get host address.  IO accesses
-       use the corresponding iotlb value.  */
-    uintptr_t addend;
-    /* padding to get a power of two size */
Quoted 9 lines

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 108

Quoted 5 lines
include/exec/cpu-defs.h +1 -4
-    uint8_t dummy[(1 << CPU_TLB_ENTRY_BITS) -
-                  (sizeof(target_ulong) * 3 +
-                   ((-sizeof(target_ulong) * 3) & (sizeof(uintptr_t) - 1)) +
-                   sizeof(uintptr_t))];
+    union {

The struct CPUTLBEntry can be changed to union CPUTLBEntry directly, with no need for the anonymous struct.

True. Code gets even simpler! :)

Quoted 11 lines

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 116

Quoted 8 lines
include/exec/cpu-defs.h +8 -0
+        struct {
+            target_ulong addr_read;
+            target_ulong addr_write;
+            target_ulong addr_code;
+            /* Addend to virtual address to get host address.  IO accesses
+               use the corresponding iotlb value.  */
+            uintptr_t addend;
+        };

Which compiler version started implementing anonymous structs?

ISO C11 standardises it apparently. But various parts of the tree use them now. target-arm/cpu.h, target-i386/kvm.c, linux-user/syscall_defs.h and linux-headers/linux/kvm.h have liberal use to name a few. I have seen it used in devs from time to time for unionifying individual registers with an array form for SW access.

This led me to consider it open season on anonymous structs and unions.

Quoted 4 lines

Or can we just add

         __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)))

Is that more or less standard than anonymous structs?

Regards,
Peter

Quoted 19 lines

to the struct? I'm not sure if it affects the sizeof too, so that requires some care. Alternatively, an

        uint8_t padding[0]
                __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)));

could maybe work? Neither is exactly the same, as they also bump the alignment of the overall struct, but they do not require anonymous structs.

Paolo

Quoted 7 lines
include/exec/cpu-defs.h +3 -0
+        /* padding to get a power of two size */
+        uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
+    };
 } CPUTLBEntry;
 
 QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
#3 lore raw
Quoted 9 lines · Peter Crosthwaite
Quoted 2 lines
Quoted 2 lines

Which compiler version started implementing anonymous structs?

ISO C11 standardises it apparently. But various parts of the tree use them now. target-arm/cpu.h, target-i386/kvm.c, linux-user/syscall_defs.h and linux-headers/linux/kvm.h have liberal use to name a few. I have seen it used in devs from time to time for unionifying individual registers with an array form for SW access.

This led me to consider it open season on anonymous structs and unions.

Ok, I wasn't sure about anonymous structs. We definitely use anonymous unions a lot.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Or can we just add

         __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)))

Is that more or less standard than anonymous structs?

Not standard (though there is something in C11 too) but decades old as a GCC extension.

Paolo

#4 lore raw
In reply to Paolo Bonzini
Quoted 26 lines · Paolo Bonzini
Quoted 23 lines · Peter Crosthwaite

There was a complicated subtractive arithmetic for determining the padding on the CPUTLBEntry structure. Simplify this with a union.

---
  include/exec/cpu-defs.h | 23 ++++++++++++-----------
  1 file changed, 12 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +0 -7
index 98b9cff..5093be2 100644
@@ -105,17 +105,18 @@ typedef struct CPUTLBEntry {
         bit 3                      : indicates that the entry is invalid
         bit 2..0                   : zero
      */
-    target_ulong addr_read;
-    target_ulong addr_write;
-    target_ulong addr_code;
-    /* Addend to virtual address to get host address.  IO accesses
-       use the corresponding iotlb value.  */
-    uintptr_t addend;
-    /* padding to get a power of two size */
Quoted 8 lines

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 108

Quoted 5 lines
include/exec/cpu-defs.h +1 -4
-    uint8_t dummy[(1 << CPU_TLB_ENTRY_BITS) -
-                  (sizeof(target_ulong) * 3 +
-                   ((-sizeof(target_ulong) * 3) & (sizeof(uintptr_t) - 1)) +
-                   sizeof(uintptr_t))];
+    union {

The struct CPUTLBEntry can be changed to union CPUTLBEntry directly, with no need for the anonymous struct.

Um, no it can't. That would put all of the members at the same address.

Quoted 1 line

Which compiler version started implementing anonymous structs?

A long long time ago -- gcc 2 era.

Quoted 3 lines

Or can we just add

	 __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)))

The structure isn't currently aligned, and it needn't be. We only need the size to be a power of two for the addressing.

r~

#5 lore raw
Quoted 38 lines · Richard Henderson
Quoted 28 lines · Paolo Bonzini
Quoted 25 lines · Peter Crosthwaite

There was a complicated subtractive arithmetic for determining the padding on the CPUTLBEntry structure. Simplify this with a union.

---
  include/exec/cpu-defs.h | 23 ++++++++++++-----------
  1 file changed, 12 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +0 -0
index 98b9cff..5093be2 100644
@@ -105,17 +105,18 @@ typedef struct CPUTLBEntry {
         bit 3                      : indicates that the entry is

invalid
bit 2..0 : zero
*/

include/exec/cpu-defs.h +0 -8
-    target_ulong addr_read;
-    target_ulong addr_write;
-    target_ulong addr_code;
-    /* Addend to virtual address to get host address.  IO accesses
-       use the corresponding iotlb value.  */
-    uintptr_t addend;
-    /* padding to get a power of two size */
-    uint8_t dummy[(1 << CPU_TLB_ENTRY_BITS) -
Quoted 8 lines

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 108

Quoted 5 lines
include/exec/cpu-defs.h +1 -4
-                  (sizeof(target_ulong) * 3 +
-                   ((-sizeof(target_ulong) * 3) & (sizeof(uintptr_t)
- 1)) +
-                   sizeof(uintptr_t))];
+    union {

The struct CPUTLBEntry can be changed to union CPUTLBEntry directly, with no need for the anonymous struct.

Quoted 1 line

Um, no it can't. That would put all of the members at the same address.

Of course. :-( With no need for the anonymous _union_. *blush*.

Quoted 3 lines
Quoted 1 line

Which compiler version started implementing anonymous structs?

A long long time ago -- gcc 2 era.

Great. I now remember that the recent feature is anonymous tagged structs, coming from the Plan 9 compiler.

Paolo

Quoted 10 lines
Quoted 3 lines

Or can we just add

     __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)))

The structure isn't currently aligned, and it needn't be. We only need the size to be a power of two for the addressing.

r~

#6 lore raw
Quoted 41 lines · Paolo Bonzini
Quoted 38 lines · Richard Henderson
Quoted 36 lines · Paolo Bonzini
Quoted 20 lines · Peter Crosthwaite

There was a complicated subtractive arithmetic for determining the padding on the CPUTLBEntry structure. Simplify this with a union.

---
  include/exec/cpu-defs.h | 23 ++++++++++++-----------
  1 file changed, 12 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +0 -0
index 98b9cff..5093be2 100644
@@ -105,17 +105,18 @@ typedef struct CPUTLBEntry {
         bit 3                      : indicates that the entry is

invalid
bit 2..0 : zero
*/

include/exec/cpu-defs.h +0 -3
-    target_ulong addr_read;
-    target_ulong addr_write;
-    target_ulong addr_code;

include/exec/cpu-defs.h typedef struct CPUTLBEntry { · line 108

Quoted 10 lines
include/exec/cpu-defs.h +1 -9
-    /* Addend to virtual address to get host address.  IO accesses
-       use the corresponding iotlb value.  */
-    uintptr_t addend;
-    /* padding to get a power of two size */
-    uint8_t dummy[(1 << CPU_TLB_ENTRY_BITS) -
-                  (sizeof(target_ulong) * 3 +
-                   ((-sizeof(target_ulong) * 3) & (sizeof(uintptr_t)
- 1)) +
-                   sizeof(uintptr_t))];
+    union {

The struct CPUTLBEntry can be changed to union CPUTLBEntry directly, with no need for the anonymous struct.

Quoted 4 lines
Quoted 1 line

Um, no it can't. That would put all of the members at the same address.

Of course. :-( With no need for the anonymous _union_. *blush*.

Yeh this is what I assumed you meant. You still need one anonymous struct, but it saves one level of indent and one less anonymous thing.

Regards,
Peter

Quoted 20 lines
Quoted 3 lines
Quoted 1 line

Which compiler version started implementing anonymous structs?

A long long time ago -- gcc 2 era.

Great. I now remember that the recent feature is anonymous tagged structs, coming from the Plan 9 compiler.

Paolo

Quoted 10 lines
Quoted 3 lines

Or can we just add

     __attribute__((__aligned__(1 << CPU_TLB_ENTRY_BITS)))

The structure isn't currently aligned, and it needn't be. We only need the size to be a power of two for the addressing.

r~