Skip to contentSkip to conversation

semihosting/next (SYS_HEAPINFO)

PATCH Superseded Alex Bennée 2 participants 6 messages Jun 15, 2021 – Jun 18, 2021

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover semihosting/next (SYS_HEAPINFO)

Hi,

Hopefully the 3rd time is a charm for this series. I've removed all the review and tested tags as it is a full rewrite. The code is hopefully generic and doesn't have to play tricks with guest registers to work out where RAM is.

Please review.

Alex Bennée (2):
semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO tests/tcg: port SYS_HEAPINFO to a system test

 include/hw/loader.h                 |  10 +++
 hw/core/loader.c                    |  19 ++++
 semihosting/arm-compat-semi.c       | 131 +++++++++++++++-------------
 tests/tcg/aarch64/system/semiheap.c |  74 ++++++++++++++++
 4 files changed, 173 insertions(+), 61 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/semiheap.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/2 semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO

The previous numbers were a guess at best and rather arbitrary without taking into account anything that might be loaded. Instead of using guesses based on the state of registers implement a new function that scans MemoryRegions for the RAM of the current address space and then looks for the lowest address above any ROM blobs (which include -kernel loaded code).

Cc Andrew Strauss <[email protected]>

Message-Id: <[email protected]>

---
v2
- report some known information (limits)
- reword the commit message
v3
- rework to use the ROM blob scanning suggested by Peter
- drop arch specific wrappers
- dropped rb/tb tags as it's a rework

---
 include/hw/loader.h           |  10 +++
 hw/core/loader.c              |  19 +++++
 semihosting/arm-compat-semi.c | 131 ++++++++++++++++++----------------
 3 files changed, 99 insertions(+), 61 deletions(-)
include/hw/loader.h +10 -0
index cbfc184873..037828e94d 100644
@@ -349,4 +349,14 @@ int rom_add_option(const char *file, int32_t bootindex);
  * overflow on real hardware too. */
 #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
 
+/**
+ * rom_find_highest_addr: return highest address of ROM in region
+ *
+ * This function is used to find the highest ROM address (or loaded
+ * blob) so we can advise where true heap memory may be.
+ *
+ * Returns: highest found address in region
+ */
+hwaddr rom_find_highest_addr(hwaddr base, size_t size);
+
 #endif
hw/core/loader.c +19 -0
index 5b34869a54..05003556ee 100644
@@ -1310,6 +1310,25 @@ static Rom *find_rom(hwaddr addr, size_t size)
     return NULL;
 }
 
+hwaddr rom_find_highest_addr(hwaddr base, size_t size)
+{
+    Rom *rom;
+    hwaddr lowest = base;
+
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->addr < base) {
+            continue;
+        }
+        if (rom->addr + rom->romsize > base + size) {
+            continue;
+        }
+        if (rom->addr + rom->romsize > lowest) {
+            lowest = rom->addr + rom->romsize;
+        }
+    }
+    return lowest;
+}
+
 /*
  * Copies memory from registered ROMs to dest. Any memory that is contained in
  * a ROM between addr and addr + size is copied. Note that this can involve
semihosting/arm-compat-semi.c +70 -61

178 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v3 1/2] semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO

Quoted 50 lines · Alex Bennée

The previous numbers were a guess at best and rather arbitrary without taking into account anything that might be loaded. Instead of using guesses based on the state of registers implement a new function that scans MemoryRegions for the RAM of the current address space and then looks for the lowest address above any ROM blobs (which include -kernel loaded code).

Cc Andrew Strauss <[email protected]>

Message-Id: <[email protected]>

---
v2
- report some known information (limits)
- reword the commit message
v3
- rework to use the ROM blob scanning suggested by Peter
- drop arch specific wrappers
- dropped rb/tb tags as it's a rework

---
 include/hw/loader.h           |  10 +++
 hw/core/loader.c              |  19 +++++
 semihosting/arm-compat-semi.c | 131 ++++++++++++++++++----------------
 3 files changed, 99 insertions(+), 61 deletions(-)
include/hw/loader.h +10 -0
index cbfc184873..037828e94d 100644
@@ -349,4 +349,14 @@ int rom_add_option(const char *file, int32_t bootindex);
  * overflow on real hardware too. */
 #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
 
+/**
+ * rom_find_highest_addr: return highest address of ROM in region
+ *
+ * This function is used to find the highest ROM address (or loaded
+ * blob) so we can advise where true heap memory may be.
+ *
+ * Returns: highest found address in region
+ */
+hwaddr rom_find_highest_addr(hwaddr base, size_t size);
+
 #endif
hw/core/loader.c +0 -0
index 5b34869a54..05003556ee 100644

hw/core/loader.c static Rom *find_rom(hwaddr addr, size_t size) · line 1318

Quoted 9 lines
hw/core/loader.c +6 -0
@@ -1310,6 +1310,25 @@ static Rom *find_rom(hwaddr addr, size_t size)
     return NULL;
 }
 
+hwaddr rom_find_highest_addr(hwaddr base, size_t size)
+{
+    Rom *rom;
+    hwaddr lowest = base;
+
+    QTAILQ_FOREACH(rom, &roms, next) {

You should ignore roms with rom->mr non-NULL (which are rom blobs that have been set up to be loaded into a specific MR), and ones with fw_file non NULL (which are rom blobs that have been set up to be loaded into the fw_cfg device).

hw/core/loader.c static Rom *find_rom(hwaddr addr, size_t size) · line 1324

Quoted 6 lines
hw/core/loader.c +6 -0
+        if (rom->addr < base) {
+            continue;
+        }
+        if (rom->addr + rom->romsize > base + size) {
+            continue;
+        }

This will incorrectly ignore ROM blobs that start below 'base' but end partway through it, as well as blobs that start within the region but end after it.

hw/core/loader.c static Rom *find_rom(hwaddr addr, size_t size) · line 1328

Quoted 4 lines
hw/core/loader.c +4 -0
+        if (rom->addr + rom->romsize > lowest) {
+            lowest = rom->addr + rom->romsize;
+        }
+    }

There's a cute algorithm (suggested by a friend) which you could use to find the largest unoccupied chunk of (base, size) rather than merely looking for the unoccupied part at the top of it:

(1) iterate through the rom list, constructing a list of tuples (hwaddr addr, int count). For every relevant rom, add two entries to the tuple list: (rom->addr, 1) and (rom->addr + rom->romsize, -1)
(2) Sort the tuple list by addr; break ties by sorting (x,1) before (x,-1)
(3) Set gapstart to 0.
(4) iterate through the sorted tuple list, keeping a running count (increment the count for each (x, 1) tuple, decrement for (x, -1) tuples). If the count goes from 0 to 1, you've just left an empty section of the range, and there was a gap from gapstart to x-1 (so if that's the largest gap you've seen so far, remember it). If the count goes from 1 to 0, that's the beginning of a new gap, so set gapstart to x.
(5) Return whatever is the largest gap we saw.

Exercises for the reader:
(a) locate any off-by-one errors in the above sketch
(b) account for the fact that gaps outside our (base,size) are of no use to us and should be ignored, and gaps that overlap with our range need to be clipped
(c) decide whether you think this is worth doing :-)

Quoted 37 lines
hw/core/loader.c +3 -0
+    return lowest;
+}
+
 /*
  * Copies memory from registered ROMs to dest. Any memory that is contained in
  * a ROM between addr and addr + size is copied. Note that this can involve
semihosting/arm-compat-semi.c +12 -3
index 1c29146dcf..a276161181 100644
@@ -44,6 +44,7 @@
 #else
 #include "exec/gdbstub.h"
 #include "qemu/cutils.h"
+#include "hw/loader.h"
 #ifdef TARGET_ARM
 #include "hw/arm/boot.h"
 #endif
@@ -144,33 +145,71 @@ typedef struct GuestFD {
 static GArray *guestfd_array;
 
 #ifndef CONFIG_USER_ONLY
-#include "exec/address-spaces.h"
-/*
- * Find the base of a RAM region containing the specified address
+
+/**
+ * common_semi_find_bases: find information about ram and heap base
+ *
+ * This function attempts to provide meaningful numbers for RAM and
+ * HEAP base addresses. The rambase is simply the lowest addressable
+ * RAM position. For the heapbase we scan though the address space and
+ * return the first available address above any ROM regions created by
+ * the loaders.
+ *
+ * Returns: a structure with the numbers we need.
  */

semihosting/arm-compat-semi.c typedef struct GuestFD { · line 167

Quoted 10 lines
semihosting/arm-compat-semi.c +8 -2
-static inline hwaddr
-common_semi_find_region_base(hwaddr addr)
+
+typedef struct LayoutInfo {
+    target_ulong rambase;
+    size_t ramsize;
+    target_ulong heapbase;
+    target_ulong heaplimit;
+    target_ulong stackbase;
+    target_ulong stacklimit;

You should work in 'hwaddr's at least while you're working with addresses from the memory subsystem; convert to target_ulong only at the end (and it would probably be a good idea to catch the improbable case of "turns out the RAM we found for the heap is beyond the range of a target_ulong"...)

Quoted 6 lines
semihosting/arm-compat-semi.c +4 -1
+} LayoutInfo;
+
+static bool find_ram_cb(Int128 start, Int128 len, const MemoryRegion *mr,
+                        hwaddr offset_in_region, void *opaque)
 {
-    MemoryRegion *subregion;

semihosting/arm-compat-semi.c typedef struct GuestFD { · line 182

Quoted 10 lines
semihosting/arm-compat-semi.c +10 -0
+    LayoutInfo *info = (LayoutInfo *) opaque;
+
+    if (!mr->ram || mr->readonly) {
+        return false;
+    }
+
+    info->rambase = mr->addr;
+    info->ramsize = int128_get64(len);
+
+    return true;

This will pick the first MR it finds that happens to be RAM (which could be some tiny thing). You don't want that, you want specifically whatever the board decided was the system RAM, which is the MemoryRegion MachineState::ram.

mr->addr is also not the address of the MR in the flatview, it's the address of the MR in its container, which might not start at the base of the address space.

In this case what we want is the address within the flatrange of the first visible bit of that MR, which is just 'start'.

Quoted 28 lines
semihosting/arm-compat-semi.c +14 -11
+}
+
+static LayoutInfo common_semi_find_bases(CPUState *cs)
+{
+    FlatView *fv;
+    LayoutInfo info = { 0, 0, 0, 0, 0, 0 };
+
+    RCU_READ_LOCK_GUARD();
+
+    fv = address_space_to_flatview(cs->as);
+    flatview_for_each_range(fv, find_ram_cb, &info);
 
     /*
-     * Find the chunk of R/W memory containing the address.  This is
-     * used for the SYS_HEAPINFO semihosting call, which should
-     * probably be using information from the loaded application.
+     * If we have found the RAM lets iterate through the ROM blobs to
+     * workout the best place for the remainder of RAM and split it
+     * equally between stack and heap.
      */
-    QTAILQ_FOREACH(subregion, &get_system_memory()->subregions,
-                   subregions_link) {
-        if (subregion->ram && !subregion->readonly) {
-            Int128 top128 = int128_add(int128_make64(subregion->addr),
-                                       subregion->size);
-            Int128 addr128 = int128_make64(addr);
-            if (subregion->addr <= addr && int128_lt(addr128, top128)) {
-                return subregion->addr;

semihosting/arm-compat-semi.c typedef struct GuestFD { · line 207

Quoted 10 lines
semihosting/arm-compat-semi.c +8 -2
-            }
-        }
+    if (info.rambase && info.ramsize) {
+        hwaddr limit = info.rambase + info.ramsize;
+        size_t space;
+        info.heapbase = rom_find_highest_addr(info.rambase, info.ramsize);
+        space = QEMU_ALIGN_DOWN((limit - info.heapbase) / 2, TARGET_PAGE_SIZE);
+        info.heaplimit = info.heapbase + space;
+        info.stackbase = info.rambase + info.ramsize;
+        info.stacklimit = info.stackbase - space;

I don't think we need to divide the memory into separate stack and heap like this -- guests can probably handle the heaplimit being the same as the stackbase and the stacklimit being the same as the heapbase, which is what we've been giving them so far.

semihosting/arm-compat-semi.c typedef struct GuestFD { · line 210

Quoted 4 lines
semihosting/arm-compat-semi.c +2 -1
     }
-    return 0;
+
+    return info;

This will recalculate it for every call, but I don't suppose the guest is going to call it more than once so that's fine.

Quoted 72 lines
semihosting/arm-compat-semi.c +3 -36
 }
+
 #endif
 
 #ifdef TARGET_ARM
@@ -204,28 +243,6 @@ common_semi_sys_exit_extended(CPUState *cs, int nr)
     return (nr == TARGET_SYS_EXIT_EXTENDED || is_a64(cs->env_ptr));
 }
 
-#ifndef CONFIG_USER_ONLY
-#include "hw/arm/boot.h"
-static inline target_ulong
-common_semi_rambase(CPUState *cs)
-{
-    CPUArchState *env = cs->env_ptr;
-    const struct arm_boot_info *info = env->boot_info;
-    target_ulong sp;
-
-    if (info) {
-        return info->loader_start;
-    }
-
-    if (is_a64(env)) {
-        sp = env->xregs[31];
-    } else {
-        sp = env->regs[13];
-    }
-    return common_semi_find_region_base(sp);
-}
-#endif
-
 #endif /* TARGET_ARM */
 
 #ifdef TARGET_RISCV
@@ -251,17 +268,6 @@ common_semi_sys_exit_extended(CPUState *cs, int nr)
     return (nr == TARGET_SYS_EXIT_EXTENDED || sizeof(target_ulong) == 8);
 }
 
-#ifndef CONFIG_USER_ONLY
-
-static inline target_ulong
-common_semi_rambase(CPUState *cs)
-{
-    RISCVCPU *cpu = RISCV_CPU(cs);
-    CPURISCVState *env = &cpu->env;
-    return common_semi_find_region_base(env->gpr[xSP]);
-}
-#endif
-
 #endif
 
 /*
@@ -1165,12 +1171,12 @@ target_ulong do_common_semihosting(CPUState *cs)
     case TARGET_SYS_HEAPINFO:
         {
             target_ulong retvals[4];
-            target_ulong limit;
             int i;
 #ifdef CONFIG_USER_ONLY
             TaskState *ts = cs->opaque;
+            target_ulong limit;
 #else
-            target_ulong rambase = common_semi_rambase(cs);
+            LayoutInfo info = common_semi_find_bases(cs);
 #endif
 
             GET_ARG(0);
@@ -1201,12 +1207,15 @@ target_ulong do_common_semihosting(CPUState *cs)
             retvals[2] = ts->stack_base;
             retvals[3] = 0; /* Stack limit.  */
 #else
-            limit = current_machine->ram_size;

semihosting/arm-compat-semi.c target_ulong do_common_semihosting(CPUState *cs) · line 1214

Quoted 10 lines
semihosting/arm-compat-semi.c +5 -5
-            /* TODO: Make this use the limit of the loaded application.  */
-            retvals[0] = rambase + limit / 2;
-            retvals[1] = rambase + limit;
-            retvals[2] = rambase + limit; /* Stack base */
-            retvals[3] = rambase; /* Stack limit.  */
+            /*
+             * Reporting 0 indicates we couldn't calculate the real
+             * values which should force most software to fall back to
+             * using information it has.
+             */

AIUI this is true for the specific case of "zero heapbase" but probably not for zero anything else. (That bit of Arm compiler docs that had a note not in the semihosting spec is apparently accidentally over-optimistic about what the Arm compiler libc implementation can handle.) Still, if we have no idea I guess returning all-zeroes is the best we can do.

semihosting/arm-compat-semi.c target_ulong do_common_semihosting(CPUState *cs) · line 1219

Quoted 5 lines
semihosting/arm-compat-semi.c +4 -0
+            retvals[0] = info.heapbase; /* Heap Base */
+            retvals[1] = info.heaplimit; /* Heap Limit */
+            retvals[2] = info.stackbase; /* Stack base */
+            retvals[3] = info.stacklimit; /* Stack limit.  */
 #endif

-- PMM

#3 lore raw

Re: [PATCH v3 1/2] semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO

Quoted 4 lines · Peter Maydell

This will pick the first MR it finds that happens to be RAM (which could be some tiny thing). You don't want that, you want specifically whatever the board decided was the system RAM, which is the MemoryRegion MachineState::ram.

As a concrete example, on the mps3-an547 machine this code finds the 512KB ITCM at 0x0 rather than the much more useful 2GB DRAM at 0x6000_0000...

-- PMM

#4 lore raw

Re: [PATCH v3 1/2] semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO

Quoted 10 lines · Peter Maydell
Quoted 4 lines · Peter Maydell

This will pick the first MR it finds that happens to be RAM (which could be some tiny thing). You don't want that, you want specifically whatever the board decided was the system RAM, which is the MemoryRegion MachineState::ram.

As a concrete example, on the mps3-an547 machine this code finds the 512KB ITCM at 0x0 rather than the much more useful 2GB DRAM at 0x6000_0000...

Turns out that "look for MachineState::ram" won't find you the 2GB DRAM, though, because it's lurking behind a Memory Protection Controller so it doesn't appear directly in the flatview. Not sure what to do about that -- maybe we should forget about MachineState::ram and just go for "largest RAM we can see" ? Trying to get this code to be able to 'look through' IOMMUs sounds like it would be massively painful.

-- PMM

#5 lore raw

PATCH v3 2/2 tests/tcg: port SYS_HEAPINFO to a system test

In reply to Alex Bennée

This allows us to check our new SYS_HEAPINFO implementation generates sane values.

---
 tests/tcg/aarch64/system/semiheap.c | 74 +++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100644 tests/tcg/aarch64/system/semiheap.c
tests/tcg/aarch64/system/semiheap.c new file +74 -0
new file mode 100644
index 0000000000..d5613dca59
@@ -0,0 +1,74 @@
+/*
+ * Semihosting System HEAPINFO Test
+ *
+ * Copyright (c) 2021 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <inttypes.h>
+#include <stddef.h>
+#include <minilib.h>
+
+#define SYS_HEAPINFO    0x16
+
+uintptr_t __semi_call(uintptr_t type, uintptr_t arg0)
+{
+    register uintptr_t t asm("x0") = type;
+    register uintptr_t a0 asm("x1") = arg0;
+    asm("hlt 0xf000"
+        : "=r" (t)
+        : "r" (t), "r" (a0));
+
+    return t;
+}
+
+int main(int argc, char *argv[argc])
+{
+    struct {
+        void *heap_base;
+        void *heap_limit;
+        void *stack_base;
+        void *stack_limit;
+    } info;
+    void *ptr_to_info = (void *) &info;
+
+    ml_printf("Semihosting Heap Info Test\n");
+
+    /* memset(&info, 0, sizeof(info)); */
+    __semi_call(SYS_HEAPINFO, (uintptr_t) &ptr_to_info);
+
+    if (info.heap_base == NULL || info.heap_limit == NULL) {
+        ml_printf("null heap: %p -> %p\n", info.heap_base, info.heap_limit);
+        return -1;
+    }
+
+    /* Error if heap base is above limit */
+    if ((uintptr_t) info.heap_base >= (uintptr_t) info.heap_limit) {
+        ml_printf("heap base %p >= heap_limit %p\n",
+               info.heap_base, info.heap_limit);
+        return -2;
+    }
+
+    if (info.stack_base == NULL) {
+        ml_printf("null stack: %p -> %p\n", info.stack_base, info.stack_limit);
+        return -3;
+    }
+
+    /*
+     * We don't check our local variables are inside the reported
+     * stack because the runtime may select a different stack area (as
+     * our boot.S code does). However we can check we don't clash with
+     * the heap.
+     */
+    if (ptr_to_info > info.heap_base && ptr_to_info < info.heap_limit) {
+        ml_printf("info appears to be inside the heap: %p in %p:%p\n",
+               ptr_to_info, info.heap_base, info.heap_limit);
+        return -4;
+    }
+
+    ml_printf("heap: %p -> %p\n", info.heap_base, info.heap_limit);
+    ml_printf("stack: %p <- %p\n", info.stack_limit, info.stack_base);
+    ml_printf("Passed HeapInfo checks\n");
+    return 0;
+}
Signature · 1 line
2.20.1