On 4/3/26 19:35, Peter Xu wrote:
On Thu, Feb 26, 2026 at 11:02:25PM +0100, Philippe Mathieu-Daudé wrote:
Use the size of a MR / AS to decide how many address chars
need to be used.

For example, using 'qemu-system-x86_64 -M q35 -S -monitor stdio'
to run 'info mtree' displays before:

   address-space: I/O
     0000000000000000-000000000000ffff (prio 0, i/o): io
         0000000000000000-0000000000000003 (prio 0, i/o): acpi-evt
         0000000000000004-0000000000000005 (prio 0, i/o): acpi-cnt
         0000000000000008-000000000000000b (prio 0, i/o): acpi-tmr
         0000000000000020-000000000000002f (prio 0, i/o): acpi-gpe0
         0000000000000030-0000000000000037 (prio 0, i/o): acpi-smi
         0000000000000060-000000000000007f (prio 0, i/o): sm-tco
       0000000000000000-0000000000000007 (prio 0, i/o): dma-chan
       0000000000000008-000000000000000f (prio 0, i/o): dma-cont
       0000000000000020-0000000000000021 (prio 0, i/o): pic
       0000000000000040-0000000000000043 (prio 0, i/o): pit
       ...
   memory-region: pc.ram
     0000000000000000-0000000007ffffff (prio 0, ram): pc.ram
   memory-region: pc.bios
     00000000fffc0000-00000000ffffffff (prio 0, rom): pc.bios
   memory-region: pci
     0000000000000000-ffffffffffffffff (prio -1, i/o): pci
       00000000000a0000-00000000000bffff (prio 1, i/o): vga-lowmem
       00000000000c0000-00000000000dffff (prio 1, rom): pc.rom
       00000000000e0000-00000000000fffff (prio 1, rom): alias isa-bios @pc.bios 
0000000000020000-000000000003ffff
       00000000fffc0000-00000000ffffffff (prio 0, rom): pc.bios
   ...

and after:

   address-space: I/O
     0000-ffff (prio 0, container): io
         0000-0003 (prio 0, i/o): acpi-evt
         0004-0005 (prio 0, i/o): acpi-cnt
         0008-000b (prio 0, i/o): acpi-tmr
         0020-002f (prio 0, i/o): acpi-gpe0
         0030-0037 (prio 0, i/o): acpi-smi
         0060-007f (prio 0, i/o): sm-tco
       0000-0007 (prio 0, i/o): dma-chan
       0008-000f (prio 0, i/o): dma-cont
       0020-0021 (prio 0, i/o): pic
       0040-0043 (prio 0, i/o): pit
       ...
   memory-region: pc.ram
     00000000-07ffffff (prio 0, ram): pc.ram
   memory-region: pc.bios
     fffc0000-ffffffff (prio 0, rom): pc.bios
   memory-region: pci
     0000000000000000-ffffffffffffffff (prio -1, i/o): pci
       00000000000a0000-00000000000bffff (prio 1, i/o): vga-lowmem
       00000000000c0000-00000000000dffff (prio 1, rom): pc.rom
       00000000000e0000-00000000000fffff (prio 1, rom): alias isa-bios @pc.bios 
0000000000020000-000000000003ffff
       00000000fffc0000-00000000ffffffff (prio 0, rom): pc.bios
   ...

Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
---
  system/memory.c | 27 +++++++++++++++++++++++----
  1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/system/memory.c b/system/memory.c
index 4d276307da5..abcbdaadcd7 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -3321,7 +3321,26 @@ typedef QTAILQ_HEAD(, MemoryRegionList) 
MemoryRegionListHead;
#define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
                             int128_sub((size), int128_one())) : 0)
-#define MR_ADDR_WIDTH   16
+
+static unsigned mr_addr_fmt_width(const MemoryRegion *mr)
+{
+    const unsigned int bits = 127 - clz128(mr->size);
+    unsigned int width;
+
+    if (bits <= 8) {
+        width = 2;
+    } else if (bits <= 16) {
+        width = 4;
+    } else if (bits <= 32) {
+        width = 8;
+    } else if (bits <= 64) {
+        width = 16;
+    } else {
+        width = 20;

Is there any case this can happen?  I thought we don't have MR larger than
1<<64.  Not a huge deal, though.

Reviewed-by: Peter Xu <[email protected]>

There are few uses of 0x1.0000.0000.0000.0000:

$ git grep int128_2_64
hw/vfio/container-legacy.c:184: Int128 llsize = int128_rshift(int128_2_64(), 1);
hw/vfio/listener.c:727:        if (int128_eq(llsize, int128_2_64())) {
hw/virtio/vhost-vdpa.c:469:    if (int128_eq(llsize, int128_2_64())) {
system/memory.c:757: addrrange_make(int128_zero(), int128_2_64()),
system/memory.c:1226:        mr->size = int128_2_64();
system/memory.c:1809:    if (int128_eq(mr->size, int128_2_64())) {
system/memory.c:2649:        s = int128_2_64();
system/physmem.c:2996:        .size = int128_2_64(),

include/qemu/int128.h:55:static inline Int128 int128_2_64(void)
include/qemu/int128.h-56-{
include/qemu/int128.h-57-    return (Int128)1 << 64;
include/qemu/int128.h-58-}


Reply via email to