Skip to contentSkip to conversation

int128: reparing broken 128 bit memory calculations

PATCH In review Pierre Morel 2 participants 14 messages Nov 5, 2015 – Nov 10, 2015 1 message not in the archive Arm Samsung Exynos

lore
Share & tools
lore
#0 lore raw

cover int128: reparing broken 128 bit memory calculations

The size of a memory area can never be negative. It follows it must be defined as an unsigned value.

Let's modify the memory regions size to unsigned 128 integer and modify accordingly the 128 bit arithmetic.

This makes memory size calculations easier and easier to understand.

I fear loud protest but really, it is a broken concept that obfuscate the code.

Pierre Morel (7):
int128: use unsigned 128 bit arithmetic
memory: modify memory size for an unsigned 128bit int 128bit: adapt core files for unsigned 128bits 128bit: adapt sparc mmu_helper for UInt128 128bit: adapt VFIO for UInt128 arithmetic 128bit: adapt Virtio for UInt128 arithmetic 128bits: some HW components use 128bit arithmetic.

 exec.c                       |   32 ++++----
 hw/core/loader.c             |    2 +-
 hw/display/exynos4210_fimd.c |    4 +-
 hw/display/framebuffer.c     |    2 +-
 hw/mem/pc-dimm.c             |    6 +-
 hw/pci-host/ppce500.c        |    2 +-
 hw/vfio/common.c             |   20 +++---
 hw/virtio/dataplane/vring.c  |    2 +-
 hw/virtio/vhost.c            |    4 +-
 hw/virtio/virtio-balloon.c   |    2 +-
 include/exec/memory.h        |    4 +-
 include/qemu/int128.h        |   95 ++++++++++-----------
 kvm-all.c                    |   16 ++--
 memory.c                     |  184 +++++++++++++++++++++---------------------
 memory_mapping.c             |    2 +-
 target-sparc/mmu_helper.c    |    2 +-
 16 files changed, 187 insertions(+), 192 deletions(-)
#1 lore raw

PATCH 1/7 int128: use unsigned 128 bit arithmetic

Let's change Int128 definition to UInt128,
modify the internal element to uint64_t and
the arithmetic functions accordingly.

---
 include/qemu/int128.h |   95 +++++++++++++++++++++++--------------------------
 1 files changed, 45 insertions(+), 50 deletions(-)
include/qemu/int128.h +45 -50
index fb782aa..c0465a2 100644
@@ -5,64 +5,64 @@
 #include <stdint.h>
 #include <stdbool.h>
 
-typedef struct Int128 Int128;
+typedef struct UInt128 UInt128;
 
-struct Int128 {
+struct UInt128 {
     uint64_t lo;
-    int64_t hi;
+    uint64_t hi;
 };
 
-static inline Int128 int128_make64(uint64_t a)
+static inline UInt128 uint128_from_64(uint64_t a)
 {
-    return (Int128) { a, 0 };
+    return (UInt128) { a, 0 };
 }
 
-static inline uint64_t int128_get64(Int128 a)
+static inline uint64_t uint128_to_64(UInt128 a)
 {
     assert(!a.hi);
     return a.lo;
 }
 
-static inline Int128 int128_zero(void)
+static inline UInt128 uint128_zero(void)
 {
-    return int128_make64(0);
+    return uint128_from_64(0);
 }
 
-static inline Int128 int128_one(void)
+static inline UInt128 uint128_one(void)
 {
-    return int128_make64(1);
+    return uint128_from_64(1);
 }
 
-static inline Int128 int128_2_64(void)
+static inline UInt128 uint128_2_64(void)
 {
-    return (Int128) { 0, 1 };
+    return (UInt128) { UINT64_MAX, 0 };
 }
 
-static inline Int128 int128_exts64(int64_t a)
+static inline UInt128 uint128_exts64(int64_t a)
 {
-    return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
+    return (UInt128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
 }
 
-static inline Int128 int128_and(Int128 a, Int128 b)
+static inline UInt128 uint128_and(UInt128 a, UInt128 b)
 {
-    return (Int128) { a.lo & b.lo, a.hi & b.hi };
+    return (UInt128) { a.lo & b.lo, a.hi & b.hi };
 }
 
-static inline Int128 int128_rshift(Int128 a, int n)
+static inline UInt128 uint128_rshift(UInt128 a, unsigned int n)
 {
-    int64_t h;
+    uint64_t h;
     if (!n) {
         return a;
     }
     h = a.hi >> (n & 63);
     if (n >= 64) {
-        return (Int128) { h, h >> 63 };
+        return (UInt128) { h, h >> 63 };
     } else {
-        return (Int128) { (a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h };
+        return (UInt128) { (a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h };
     }
 }
 
-static inline Int128 int128_add(Int128 a, Int128 b)
+static inline UInt128 uint128_add(UInt128 a, UInt128 b)
 {
     uint64_t lo = a.lo + b.lo;
 
@@ -72,78 +72,73 @@ static inline Int128 int128_add(Int128 a, Int128 b)
      *
      * So the carry is lo < a.lo.
      */
-    return (Int128) { lo, (uint64_t)a.hi + b.hi + (lo < a.lo) };
+    return (UInt128) { lo, a.hi + b.hi + (lo < a.lo) };
 }
 
-static inline Int128 int128_neg(Int128 a)
+static inline UInt128 uint128_neg(UInt128 a)
 {
     uint64_t lo = -a.lo;
-    return (Int128) { lo, ~(uint64_t)a.hi + !lo };
+    return (UInt128) { lo, ~a.hi + !lo };
 }
 
-static inline Int128 int128_sub(Int128 a, Int128 b)
+static inline UInt128 uint128_sub(UInt128 a, UInt128 b)
 {
-    return (Int128){ a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo) };
+    return (UInt128){ a.lo - b.lo, a.hi - b.hi - (a.lo < b.lo) };
 }
 
-static inline bool int128_nonneg(Int128 a)
-{
-    return a.hi >= 0;
-}
-
-static inline bool int128_eq(Int128 a, Int128 b)
+static inline bool uint128_eq(UInt128 a, UInt128 b)
 {
     return a.lo == b.lo && a.hi == b.hi;
 }
 
-static inline bool int128_ne(Int128 a, Int128 b)
+static inline bool uint128_ne(UInt128 a, UInt128 b)
 {
-    return !int128_eq(a, b);
+    return !uint128_eq(a, b);
 }
 
-static inline bool int128_ge(Int128 a, Int128 b)
+static inline bool uint128_ge(UInt128 a, UInt128 b)
 {
     return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo);
 }
 
-static inline bool int128_lt(Int128 a, Int128 b)
+static inline bool uint128_lt(UInt128 a, UInt128 b)
 {
-    return !int128_ge(a, b);
+    return !uint128_ge(a, b);
 }
 
-static inline bool int128_le(Int128 a, Int128 b)
+static inline bool uint128_le(UInt128 a, UInt128 b)
 {
-    return int128_ge(b, a);
+    return uint128_ge(b, a);
 }
 
-static inline bool int128_gt(Int128 a, Int128 b)
+static inline bool uint128_gt(UInt128 a, UInt128 b)
 {
-    return !int128_le(a, b);
+    return !uint128_le(a, b);
 }
 
-static inline bool int128_nz(Int128 a)
+static inline bool uint128_nz(UInt128 a)
 {
     return a.lo || a.hi;
 }
 
-static inline Int128 int128_min(Int128 a, Int128 b)
+static inline UInt128 uint128_min(UInt128 a, UInt128 b)
 {
-    return int128_le(a, b) ? a : b;
+    return uint128_le(a, b) ? a : b;
 }
 
-static inline Int128 int128_max(Int128 a, Int128 b)
+static inline UInt128 uint128_max(UInt128 a, UInt128 b)
 {
-    return int128_ge(a, b) ? a : b;
+    return uint128_ge(a, b) ? a : b;
 }
 
-static inline void int128_addto(Int128 *a, Int128 b)
+static inline void uint128_addto(UInt128 *a, UInt128 b)
 {
-    *a = int128_add(*a, b);
+    *a = uint128_add(*a, b);
 }
 
-static inline void int128_subfrom(Int128 *a, Int128 b)
+static inline void uint128_subfrom(UInt128 *a, UInt128 b)
 {
-    *a = int128_sub(*a, b);
+    *a = uint128_sub(*a, b);
 }
 
 #endif
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/7 memory: modify memory size for an unsigned 128bit int

In reply to Pierre Morel

The size of a memory area can never be negative. It follows it must be defined as an unsigned value.

Let's modify the memory regions size to unsigned 128 integer and accordingly use the unsigned 128 bit arithmetic.

---
 include/exec/memory.h |    4 +-
 memory.c              |  184 ++++++++++++++++++++++++------------------------
 memory_mapping.c      |    2 +-
 3 files changed, 95 insertions(+), 95 deletions(-)
include/exec/memory.h +2 -2
index 94d20ea..42457f5 100644
@@ -164,7 +164,7 @@ struct MemoryRegion {
     const MemoryRegionIOMMUOps *iommu_ops;
     void *opaque;
     MemoryRegion *container;
-    Int128 size;
+    UInt128 size;
     hwaddr addr;
     void (*destructor)(MemoryRegion *mr);
     ram_addr_t ram_addr;
@@ -264,7 +264,7 @@ struct MemoryRegionSection {
     MemoryRegion *mr;
     AddressSpace *address_space;
     hwaddr offset_within_region;
-    Int128 size;
+    UInt128 size;
     hwaddr offset_within_address_space;
     bool readonly;
 };
memory.c +92 -92

443 lines not on this page · raw message

memory_mapping.c +1 -1
index 36d6b26..ce87c6b 100644
@@ -209,7 +209,7 @@ static void guest_phys_blocks_region_add(MemoryListener *listener,
     }
 
     g            = container_of(listener, GuestPhysListener, listener);
-    section_size = int128_get64(section->size);
+    section_size = uint128_to_64(section->size);
     target_start = section->offset_within_address_space;
     target_end   = target_start + section_size;
     host_addr    = memory_region_get_ram_ptr(section->mr) +
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/7 128bit: adapt core files for unsigned 128bits

In reply to Pierre Morel

Adapt the core files for unsigned 128 bit arithmetic.

---
 exec.c    |   32 ++++++++++++++++----------------
 kvm-all.c |   16 ++++++++--------
 2 files changed, 24 insertions(+), 24 deletions(-)
exec.c +16 -16
index 0a4a0c5..7828f0b 100644
@@ -343,7 +343,7 @@ address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *x
 {
     MemoryRegionSection *section;
     MemoryRegion *mr;
-    Int128 diff;
+    UInt128 diff;
 
     section = address_space_lookup_region(d, addr, resolve_subpage);
     /* Compute offset within MemoryRegionSection */
@@ -366,8 +366,8 @@ address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *x
      * the caller really has to do the clamping through memory_access_size.
      */
     if (memory_region_is_ram(mr)) {
-        diff = int128_sub(section->size, int128_make64(addr));
-        *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
+        diff = uint128_sub(section->size, uint128_from_64(addr));
+        *plen = uint128_to_64(uint128_min(diff, uint128_from_64(*plen)));
     }
     return section;
 }
@@ -1046,7 +1046,7 @@ static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *secti
                                                    d->map.nodes, d->map.sections);
     MemoryRegionSection subsection = {
         .offset_within_address_space = base,
-        .size = int128_make64(TARGET_PAGE_SIZE),
+        .size = uint128_from_64(TARGET_PAGE_SIZE),
     };
     hwaddr start, end;
 
@@ -1062,7 +1062,7 @@ static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *secti
         subpage = container_of(existing->mr, subpage_t, iomem);
     }
     start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
-    end = start + int128_get64(section->size) - 1;
+    end = start + uint128_to_64(section->size) - 1;
     subpage_register(subpage, start, end,
                      phys_section_add(&d->map, section));
 }
@@ -1073,7 +1073,7 @@ static void register_multipage(AddressSpaceDispatch *d,
 {
     hwaddr start_addr = section->offset_within_address_space;
     uint16_t section_index = phys_section_add(&d->map, section);
-    uint64_t num_pages = int128_get64(int128_rshift(section->size,
+    uint64_t num_pages = uint128_to_64(uint128_rshift(section->size,
                                                     TARGET_PAGE_BITS));
 
     assert(num_pages);
@@ -1085,29 +1085,29 @@ static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
     AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
     AddressSpaceDispatch *d = as->next_dispatch;
     MemoryRegionSection now = *section, remain = *section;
-    Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
+    UInt128 page_size = uint128_from_64(TARGET_PAGE_SIZE);
 
     if (now.offset_within_address_space & ~TARGET_PAGE_MASK) {
         uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space)
                        - now.offset_within_address_space;
 
-        now.size = int128_min(int128_make64(left), now.size);
+        now.size = uint128_min(uint128_from_64(left), now.size);
         register_subpage(d, &now);
     } else {
-        now.size = int128_zero();
+        now.size = uint128_zero();
     }
-    while (int128_ne(remain.size, now.size)) {
-        remain.size = int128_sub(remain.size, now.size);
-        remain.offset_within_address_space += int128_get64(now.size);
-        remain.offset_within_region += int128_get64(now.size);
+    while (uint128_ne(remain.size, now.size)) {
+        remain.size = uint128_sub(remain.size, now.size);
+        remain.offset_within_address_space += uint128_to_64(now.size);
+        remain.offset_within_region += uint128_to_64(now.size);
         now = remain;
-        if (int128_lt(remain.size, page_size)) {
+        if (uint128_lt(remain.size, page_size)) {
             register_subpage(d, &now);
         } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) {
             now.size = page_size;
             register_subpage(d, &now);
         } else {
-            now.size = int128_and(now.size, int128_neg(page_size));
+            now.size = uint128_and(now.size, uint128_neg(page_size));
             register_multipage(d, &now);
         }
     }
@@ -2155,7 +2155,7 @@ static uint16_t dummy_section(PhysPageMap *map, AddressSpace *as,
         .mr = mr,
         .offset_within_address_space = 0,
         .offset_within_region = 0,
-        .size = int128_2_64(),
+        .size = uint128_2_64(),
     };
 
     return phys_section_add(map, &section);
kvm-all.c +8 -8
index 06e06f2..6a155a9 100644
@@ -316,7 +316,7 @@ static int kvm_section_update_flags(KVMMemoryListener *kml,
                                     MemoryRegionSection *section)
 {
     hwaddr phys_addr = section->offset_within_address_space;
-    ram_addr_t size = int128_get64(section->size);
+    ram_addr_t size = uint128_to_64(section->size);
     KVMSlot *mem = kvm_lookup_matching_slot(kml, phys_addr, phys_addr + size);
 
     if (mem == NULL)  {
@@ -365,7 +365,7 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
                                          unsigned long *bitmap)
 {
     ram_addr_t start = section->offset_within_region + section->mr->ram_addr;
-    ram_addr_t pages = int128_get64(section->size) / getpagesize();
+    ram_addr_t pages = uint128_to_64(section->size) / getpagesize();
 
     cpu_physical_memory_set_dirty_lebitmap(bitmap, start, pages);
     return 0;
@@ -391,7 +391,7 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml,
     KVMSlot *mem;
     int ret = 0;
     hwaddr start_addr = section->offset_within_address_space;
-    hwaddr end_addr = start_addr + int128_get64(section->size);
+    hwaddr end_addr = start_addr + uint128_to_64(section->size);
 
     d.dirty_bitmap = NULL;
     while (start_addr < end_addr) {
@@ -634,7 +634,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
     MemoryRegion *mr = section->mr;
     bool writeable = !mr->readonly && !mr->rom_device;
     hwaddr start_addr = section->offset_within_address_space;
-    ram_addr_t size = int128_get64(section->size);
+    ram_addr_t size = uint128_to_64(section->size);
     void *ram = NULL;
     unsigned delta;
 
@@ -825,7 +825,7 @@ static void kvm_mem_ioeventfd_add(MemoryListener *listener,
     int r;
 
     r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
-                               data, true, int128_get64(section->size),
+                               data, true, uint128_to_64(section->size),
                                match_data);
     if (r < 0) {
         fprintf(stderr, "%s: error adding ioeventfd: %s\n",
@@ -843,7 +843,7 @@ static void kvm_mem_ioeventfd_del(MemoryListener *listener,
     int r;
 
     r = kvm_set_ioeventfd_mmio(fd, section->offset_within_address_space,
-                               data, false, int128_get64(section->size),
+                               data, false, uint128_to_64(section->size),
                                match_data);
     if (r < 0) {
         abort();
@@ -859,7 +859,7 @@ static void kvm_io_ioeventfd_add(MemoryListener *listener,
     int r;
 
     r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space,
-                              data, true, int128_get64(section->size),
+                              data, true, uint128_to_64(section->size),
                               match_data);
     if (r < 0) {
         fprintf(stderr, "%s: error adding ioeventfd: %s\n",
@@ -878,7 +878,7 @@ static void kvm_io_ioeventfd_del(MemoryListener *listener,
     int r;
 
     r = kvm_set_ioeventfd_pio(fd, section->offset_within_address_space,
-                              data, false, int128_get64(section->size),
+                              data, false, uint128_to_64(section->size),
                               match_data);
     if (r < 0) {
         abort();
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/7 128bit: adapt sparc mmu_helper for UInt128

In reply to Pierre Morel

Adaptation for target-sparc/mmu_helper.c to
the 128 bit arithmetic.

---
 target-sparc/mmu_helper.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
target-sparc/mmu_helper.c +1 -1
index 2a0c6f0..2c1156e 100644
@@ -859,7 +859,7 @@ hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
     }
     section = memory_region_find(get_system_memory(), phys_addr, 1);
     memory_region_unref(section.mr);
-    if (!int128_nz(section.size)) {
+    if (!uint128_nz(section.size)) {
         return -1;
     }
     return phys_addr;
Signature · 1 line
1.7.1
#5 lore raw

PATCH 5/7 128bit: adapt VFIO for UInt128 arithmetic

In reply to Pierre Morel

VFIO is using 128bit arithmetic for memory areas.
Let's adapt to unsigned 128bit arithmetic.

---
 hw/vfio/common.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
hw/vfio/common.c +10 -10
index 85ee9b0..f819cd5 100644
@@ -318,7 +318,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
     VFIOContainer *container = container_of(listener, VFIOContainer,
                                             iommu_data.type1.listener);
     hwaddr iova, end;
-    Int128 llend;
+    UInt128 llend;
     void *vaddr;
     int ret;
 
@@ -326,7 +326,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
         trace_vfio_listener_region_add_skip(
                 section->offset_within_address_space,
                 section->offset_within_address_space +
-                int128_get64(int128_sub(section->size, int128_one())));
+                uint128_to_64(uint128_sub(section->size, uint128_one())));
         return;
     }
 
@@ -337,11 +337,11 @@ static void vfio_listener_region_add(MemoryListener *listener,
     }
 
     iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
-    llend = int128_make64(section->offset_within_address_space);
-    llend = int128_add(llend, section->size);
-    llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
+    llend = uint128_from_64(section->offset_within_address_space);
+    llend = uint128_add(llend, section->size);
+    llend = uint128_and(llend, uint128_exts64(TARGET_PAGE_MASK));
 
-    if (int128_ge(int128_make64(iova), llend)) {
+    if (uint128_ge(uint128_from_64(iova), llend)) {
         return;
     }
 
@@ -351,7 +351,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
         VFIOGuestIOMMU *giommu;
 
         trace_vfio_listener_region_add_iommu(iova,
-                    int128_get64(int128_sub(llend, int128_one())));
+                    uint128_to_64(uint128_sub(llend, uint128_one())));
         /*
          * FIXME: We should do some checking to see if the
          * capabilities of the host VFIO IOMMU are adequate to model
@@ -388,7 +388,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
 
     /* Here we assume that memory_region_is_ram(section->mr)==true */
 
-    end = int128_get64(llend);
+    end = uint128_to_64(llend);
     vaddr = memory_region_get_ram_ptr(section->mr) +
             section->offset_within_region +
             (iova - section->offset_within_address_space);
@@ -428,7 +428,7 @@ static void vfio_listener_region_del(MemoryListener *listener,
         trace_vfio_listener_region_del_skip(
                 section->offset_within_address_space,
                 section->offset_within_address_space +
-                int128_get64(int128_sub(section->size, int128_one())));
+                uint128_to_64(uint128_sub(section->size, uint128_one())));
         return;
     }
 
@@ -460,7 +460,7 @@ static void vfio_listener_region_del(MemoryListener *listener,
     }
 
     iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
-    end = (section->offset_within_address_space + int128_get64(section->size)) &
+    end = (section->offset_within_address_space + uint128_to_64(section->size)) &
           TARGET_PAGE_MASK;
 
     if (iova >= end) {
Signature · 1 line
1.7.1
#6 lore raw

PATCH 6/7 128bit: adapt Virtio for UInt128 arithmetic

In reply to Pierre Morel

Virtio uses 128 bit arithmetic for memory areas.
Let's adapt to UInt128 bit arithmetic.

---
 hw/virtio/dataplane/vring.c |    2 +-
 hw/virtio/vhost.c           |    4 ++--
 hw/virtio/virtio-balloon.c  |    2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
hw/virtio/dataplane/vring.c +1 -1
index 68f1994..2551a42 100644
@@ -31,7 +31,7 @@ static void *vring_map(MemoryRegion **mr, hwaddr phys, hwaddr len,
 {
     MemoryRegionSection section = memory_region_find(get_system_memory(), phys, len);
 
-    if (!section.mr || int128_get64(section.size) < len) {
+    if (!section.mr || uint128_to_64(section.size) < len) {
         goto out;
     }
     if (is_write && section.readonly) {
hw/virtio/vhost.c +2 -2
index a08c36b..143ea6f 100644
@@ -84,7 +84,7 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
         return 0;
     }
     start_addr = section->offset_within_address_space;
-    end_addr = range_get_last(start_addr, int128_get64(section->size));
+    end_addr = range_get_last(start_addr, uint128_to_64(section->size));
     start_addr = MAX(first, start_addr);
     end_addr = MIN(last, end_addr);
 
@@ -417,7 +417,7 @@ static void vhost_set_memory(MemoryListener *listener,
     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
                                          memory_listener);
     hwaddr start_addr = section->offset_within_address_space;
-    ram_addr_t size = int128_get64(section->size);
+    ram_addr_t size = uint128_to_64(section->size);
     bool log_dirty =
         memory_region_get_dirty_log_mask(section->mr) & ~(1 << DIRTY_MEMORY_MIGRATION);
     int s = offsetof(struct vhost_memory, regions) +
hw/virtio/virtio-balloon.c +1 -1
index c419b17..d0db511 100644
@@ -220,7 +220,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 
             /* FIXME: remove get_system_memory(), but how? */
             section = memory_region_find(get_system_memory(), pa, 1);
-            if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
+            if (!uint128_nz(section.size) || !memory_region_is_ram(section.mr))
                 continue;
 
             trace_virtio_balloon_handle_output(memory_region_name(section.mr),
Signature · 1 line
1.7.1
#7 lore raw

PATCH 7/7 128bits: some HW components use 128bit arithmetic.

In reply to Pierre Morel

Some HW components use 128bit arithmetic.
Let's adapt to unsigned 128 bit calculations.

---
 hw/core/loader.c             |    2 +-
 hw/display/exynos4210_fimd.c |    4 ++--
 hw/display/framebuffer.c     |    2 +-
 hw/mem/pc-dimm.c             |    6 +++---
 hw/pci-host/ppce500.c        |    2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)
hw/core/loader.c +1 -1
index 216eeeb..97c7b54 100644
@@ -953,7 +953,7 @@ int rom_check_and_register_reset(void)
         addr  = rom->addr;
         addr += rom->romsize;
         section = memory_region_find(get_system_memory(), rom->addr, 1);
-        rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
+        rom->isrom = uint128_nz(section.size) && memory_region_is_rom(section.mr);
         memory_region_unref(section.mr);
     }
     qemu_register_reset(rom_reset, NULL);
hw/display/exynos4210_fimd.c +2 -2
index 603ef50..dbedb45 100644
@@ -1154,7 +1154,7 @@ static void fimd_update_memory_section(Exynos4210fimdState *s, unsigned win)
     DPRINT_TRACE("Window %u framebuffer changed: address=0x%08x, len=0x%x\n",
             win, fb_start_addr, w->fb_len);
 
-    if (int128_get64(w->mem_section.size) != w->fb_len ||
+    if (uint128_to_64(w->mem_section.size) != w->fb_len ||
             !memory_region_is_ram(w->mem_section.mr)) {
         DPRINT_ERROR("Failed to find window %u framebuffer region\n", win);
         goto error_return;
@@ -1179,7 +1179,7 @@ static void fimd_update_memory_section(Exynos4210fimdState *s, unsigned win)
 error_return:
     memory_region_unref(w->mem_section.mr);
     w->mem_section.mr = NULL;
-    w->mem_section.size = int128_zero();
+    w->mem_section.size = uint128_zero();
     w->host_fb_addr = NULL;
     w->fb_len = 0;
 }
hw/display/framebuffer.c +1 -1
index 7f075ce..19e3d39 100644
@@ -41,7 +41,7 @@ void framebuffer_update_memory_section(
         return;
     }
 
-    if (int128_get64(mem_section->size) < src_len ||
+    if (uint128_to_64(mem_section->size) < src_len ||
             !memory_region_is_ram(mem_section->mr)) {
         memory_region_unref(mem_section->mr);
         mem_section->mr = NULL;
hw/mem/pc-dimm.c +3 -3
index bb04862..0be2a18 100644
@@ -260,11 +260,11 @@ static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
 {
     PCDIMMDevice *x = PC_DIMM(a);
     PCDIMMDevice *y = PC_DIMM(b);
-    Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));
+    UInt128 diff = uint128_sub(uint128_from_64(x->addr), uint128_from_64(y->addr));
 
-    if (int128_lt(diff, int128_zero())) {
+    if (uint128_lt(diff, uint128_zero())) {
         return -1;
-    } else if (int128_gt(diff, int128_zero())) {
+    } else if (uint128_gt(diff, uint128_zero())) {
         return 1;
     }
     return 0;
hw/pci-host/ppce500.c +1 -1
index 613ba73..9002c51 100644
@@ -428,7 +428,7 @@ static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
         PCI_HEADER_TYPE_BRIDGE;
 
     memory_region_init_alias(&b->bar0, OBJECT(ccsr), "e500-pci-bar0", &ccsr->ccsr_space,
-                             0, int128_get64(ccsr->ccsr_space.size));
+                             0, uint128_to_64(ccsr->ccsr_space.size));
     pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &b->bar0);
 }
 
Signature · 1 line
1.7.1
#8 lore raw
In reply to Pierre Morel
Quoted 10 lines · Pierre Morel

The size of a memory area can never be negative. It follows it must be defined as an unsigned value.

Let's modify the memory regions size to unsigned 128 integer and modify accordingly the 128 bit arithmetic.

This makes memory size calculations easier and easier to understand.

I fear loud protest but really, it is a broken concept that obfuscate the code.

You are right in fearing loud protest, though the protest is for the lack of explanation of what is broken.

Since the values are never going to be > 2^65, there is no chance of overflow. On the other hand there are cases where we compute start+size-1, and size-1 *could* overflow if you use unsigned integers.

So I am not sure... why?

Paolo

#9 lore raw
Quoted 15 lines · Paolo Bonzini
Quoted 10 lines · Pierre Morel

The size of a memory area can never be negative. It follows it must be defined as an unsigned value.

Let's modify the memory regions size to unsigned 128 integer and modify accordingly the 128 bit arithmetic.

This makes memory size calculations easier and easier to understand.

I fear loud protest but really, it is a broken concept that obfuscate the code.

You are right in fearing loud protest, though the protest is for the lack of explanation of what is broken.

Quoted 8 lines

Since the values are never going to be > 2^65, there is no chance of overflow. On the other hand there are cases where we compute start+size-1, and size-1 *could* overflow if you use unsigned integers.

So I am not sure... why?

Paolo

Paolo,

The calculation are not broken and it works for actual usage but:

For me, it is the design that is broken, as it uses an integer to represent something that is fundamentally unsigned like the size of a memory area.

This leads to have UINT64_MAX represented with {1, 0} instead of {0, UINT64_MAX} while {1, 0} is 2^64.
This again leads to have unnecessary and obfuscating transformations with int128_2_64() to test for UINT64_MAX and return {1,0} in memory_region_init() while using inverse translation test{1,0} and return UINT64_MAX in memory_region_size()

Another concern is that pushing the 31th bit to the 32nd bit may be confusing when used, if ever, with IOMMU or other hardware components using 128bits registers. Considering some Intel IOMMU for example.

The modifications I proposed are also not perfect, I just seen I forgot to change a -1 to UINT64_MAX and I forgot some unnecessary cast. At least.

Pierre

#10 lore raw
Quoted 1 line · Pierre Morel

The calculation are not broken and it works for actual usage

Thanks for confirming.

Quoted 2 lines

For me, it is the design that is broken, as it uses an integer to represent something that is fundamentally unsigned like the size of a memory area.

But it uses a very large integer. :)

Consider that every time you do math on uint16_t, C is actually using a 32-bit signed integer. This is roughly the same.

Quoted 7 lines

This leads to have UINT64_MAX represented with {1, 0} instead of {0, UINT64_MAX}
while {1, 0} is 2^64.
This again leads to have unnecessary and obfuscating transformations with int128_2_64()
to test for UINT64_MAX and return {1,0} in memory_region_init() while using inverse translation test{1,0} and return UINT64_MAX in memory_region_size()

Yes, the use of UINT64_MAX for 2^64 is a hack, but it is unrelated to the signedness of Int128.

Paolo

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw
Quoted 7 lines · Pierre Morel
Quoted 7 lines
Quoted 6 lines

This leads to have UINT64_MAX represented with {1, 0} instead of {0, UINT64_MAX} while {1, 0} is 2^64. This again leads to have unnecessary and obfuscating transformations with int128_2_64() to test for UINT64_MAX and return {1,0} in memory_region_init() while using inverse translation test{1,0} and return UINT64_MAX in memory_region_size()>>

Quoted 6 lines
Quoted 2 lines

Yes, the use of UINT64_MAX for 2^64 is a hack, but it is unrelated to the signedness of Int128.

OK, we agree it is a hack,
but sorry, I should have missed something, because I do not understand what this hack is useful for.

It's used in the size argument of memory_region_init*, so that it can remain an uint64_t. The size is usually small (up to 2^40, say) unless it is 2^64 meaning "the whole address space". The latter case is covered by UINT64_MAX.

Paolo

#13 lore raw
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Pierre Morel
Quoted 8 lines
Quoted 6 lines

This leads to have UINT64_MAX represented with {1, 0} instead of {0, UINT64_MAX} while {1, 0} is 2^64. This again leads to have unnecessary and obfuscating transformations with int128_2_64() to test for UINT64_MAX and return {1,0} in memory_region_init() while using inverse translation test{1,0} and return UINT64_MAX in memory_region_size()>>

Yes, the use of UINT64_MAX for 2^64 is a hack, but it is unrelated to the signedness of Int128.

OK, we agree it is a hack,
but sorry, I should have missed something, because I do not understand what this hack is useful for.

Quoted 7 lines

It's used in the size argument of memory_region_init*, so that it can remain an uint64_t. The size is usually small (up to 2^40, say) unless it is 2^64 meaning "the whole address space". The latter case is covered by UINT64_MAX.

Paolo

OK, I understand, thanks for having taking time for me.

To sum-up size is a size :-) and not an offset in memory.

Size of UINT64_MAX does not exist but we can live without it, having a description for "whole address space", 2^64, can be useful.

Even there may be other solutions like taking 0 for 2^64, if a memory size of 0 has no meaning,
but it could be misleading too.

So I do not see better solution for this interesting problematic.

#14 lore raw
Quoted 28 lines · Pierre Morel
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Pierre Morel
Quoted 8 lines
Quoted 6 lines

This leads to have UINT64_MAX represented with {1, 0} instead of {0, UINT64_MAX} while {1, 0} is 2^64. This again leads to have unnecessary and obfuscating transformations with int128_2_64() to test for UINT64_MAX and return {1,0} in memory_region_init() while using inverse translation test{1,0} and return UINT64_MAX in memory_region_size()>>

Yes, the use of UINT64_MAX for 2^64 is a hack, but it is unrelated to the signedness of Int128.

OK, we agree it is a hack,
but sorry, I should have missed something, because I do not understand what this hack is useful for.

Quoted 7 lines

It's used in the size argument of memory_region_init*, so that it can remain an uint64_t. The size is usually small (up to 2^40, say) unless it is 2^64 meaning "the whole address space". The latter case is covered by UINT64_MAX.

Paolo

OK, I understand, thanks for having taking time for me.

To sum-up size is a size :-) and not an offset in memory.

Quoted 11 lines

Size of UINT64_MAX does not exist but we can live without it, having a description for "whole address space", 2^64, can be useful.

Even there may be other solutions like taking 0 for 2^64, if a memory size of 0 has no meaning,
but it could be misleading too.

So I do not see better solution for this interesting problematic.

My problem with this came because usually, on hardware, region are easier described by start/end rather than by start/size.
But changing this in the actual implementation would be too much.