Skip to contentSkip to conversation

semihosting/next (SYS_HEAPINFO)

PATCH In review Alex Bennée 3 participants 13 messages Feb 10, 2022 – Feb 21, 2022

lore
Share & tools
lore
#0 lore raw

v5 cover semihosting/next (SYS_HEAPINFO)

Hi,

I'm working through the backlog of stalled patches in my queue so here is the latest version of the semihosting info fixes with Peter's comments addressed. Both patches are still missing their elusive r-b tags ;-)

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                 |  14 +++
 hw/core/loader.c                    |  86 +++++++++++++++++++
 semihosting/arm-compat-semi.c       | 129 +++++++++++++++-------------
 tests/tcg/aarch64/system/semiheap.c |  93 ++++++++++++++++++++
 MAINTAINERS                         |   1 +
 5 files changed, 262 insertions(+), 61 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/semiheap.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v5 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[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
v4
- search for the largest RAM which should be the main RAM
- implement the biggest gap algorithm
- make stackbase the inverse of heap info
v5
- move rom_find_largest_gap description to above fn and reword
- add documentation of sort behaviour
- handle matching se flags (- -1 -1) and (- 1 1) == 0
- add helper function and sentinal
- fix off-by-ones in comparisons
- allow a rambase at 0

squash! semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO

---
 include/hw/loader.h           |  14 ++++
 hw/core/loader.c              |  86 +++++++++++++++++++++++
 semihosting/arm-compat-semi.c | 129 ++++++++++++++++++----------------
 3 files changed, 168 insertions(+), 61 deletions(-)
include/hw/loader.h +14 -0
index 4fa485bd61..5572108ba5 100644
@@ -343,4 +343,18 @@ int rom_add_option(const char *file, int32_t bootindex);
  * overflow on real hardware too. */
 #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
 
+typedef struct RomGap {
+    hwaddr base;
+    size_t size;
+} RomGap;
+
+/**
+ * rom_find_largest_gap_between: return largest gap between ROMs in given range
+ *
+ * Given a range of addresses, this function finds the largest
+ * contiguous subrange which has no ROMs loaded to it. That is,
+ * it finds the biggest gap which is free for use for other things.
+ */
+RomGap rom_find_largest_gap_between(hwaddr base, size_t size);
+
 #endif
hw/core/loader.c +86 -0

92 lines not on this page · raw message

semihosting/arm-compat-semi.c +68 -61

176 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

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

Hi Alex,

Quoted 15 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

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

Quoted 9 lines
semihosting/arm-compat-semi.c +7 -1
+static LayoutInfo common_semi_find_bases(CPUState *cs)
  {
-    MemoryRegion *subregion;
+    FlatView *fv;
+    LayoutInfo info = { 0, 0, 0, 0 };
+
+    RCU_READ_LOCK_GUARD();
+
+    fv = address_space_to_flatview(cs->as);

Why are we using the CPU view and not address_space_memory?

Does this function really need a CPUState argument?

Trying to find a counter example, if used on the ZynqMP, could a A-profile core would report one heap layout, and a R-profile core another layout?

Now if we want the per-CPU AS, shouldn't we pass the CPU AS ID and call cpu_get_address_space() instead of cs->as?

Quoted 29 lines
semihosting/arm-compat-semi.c +10 -14
+    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;
-            }
-        }
+    if (info.rambase || info.ramsize > 0) {
+        RomGap gap = rom_find_largest_gap_between(info.rambase, info.ramsize);
+        info.heapbase = gap.base;
+        info.heaplimit = gap.base + gap.size;
      }
-    return 0;
+
+    return info;
  }
#3 lore raw

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

Quoted 19 lines · Philippe Mathieu-Daudé

Hi Alex,

Quoted 14 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

Quoted 11 lines

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

Quoted 9 lines
semihosting/arm-compat-semi.c +7 -1
+static LayoutInfo common_semi_find_bases(CPUState *cs)
  {
-    MemoryRegion *subregion;
+    FlatView *fv;
+    LayoutInfo info = { 0, 0, 0, 0 };
+
+    RCU_READ_LOCK_GUARD();
+
+    fv = address_space_to_flatview(cs->as);

Why are we using the CPU view and not address_space_memory?

If you have a choice between "use the right view of an address space" and "use the global address_space_memory", it's better to prefer the former, I think. We use the latter in lots of places, but it's not conceptually the right way to think about how the memory system works IMHO.

-- PMM

#4 lore raw

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

Quoted 32 lines · Peter Maydell
Quoted 19 lines · Philippe Mathieu-Daudé

Hi Alex,

Quoted 14 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

Quoted 11 lines

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

Quoted 9 lines
semihosting/arm-compat-semi.c +7 -1
+static LayoutInfo common_semi_find_bases(CPUState *cs)
  {
-    MemoryRegion *subregion;
+    FlatView *fv;
+    LayoutInfo info = { 0, 0, 0, 0 };
+
+    RCU_READ_LOCK_GUARD();
+
+    fv = address_space_to_flatview(cs->as);

Why are we using the CPU view and not address_space_memory?

Quoted 5 lines

If you have a choice between "use the right view of an address space" and "use the global address_space_memory", it's better to prefer the former, I think. We use the latter in lots of places, but it's not conceptually the right way to think about how the memory system works IMHO.

In this case the addresses have to be as the CPU sees them because it's between the CPU and the semihosting backend to share data.

Signature · 1 line
Alex Bennée
#5 lore raw

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

Quoted 35 lines · Alex Bennée
Quoted 32 lines · Peter Maydell
Quoted 30 lines · Philippe Mathieu-Daudé

Hi Alex,

Quoted 15 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

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

Quoted 9 lines
semihosting/arm-compat-semi.c +7 -1
+static LayoutInfo common_semi_find_bases(CPUState *cs)
   {
-    MemoryRegion *subregion;
+    FlatView *fv;
+    LayoutInfo info = { 0, 0, 0, 0 };
+
+    RCU_READ_LOCK_GUARD();
+
+    fv = address_space_to_flatview(cs->as);

Why are we using the CPU view and not address_space_memory?

Quoted 5 lines
Quoted 5 lines

If you have a choice between "use the right view of an address space" and "use the global address_space_memory", it's better to prefer the former, I think. We use the latter in lots of places, but it's not conceptually the right way to think about how the memory system works IMHO.

Yes I agree.

For user-mode, this patch makes sense. For system-mode it is not obvious to make sense of SYS_HEAPINFO (except focusing in cores targeting embedded systems eventually).

Quoted 2 lines

In this case the addresses have to be as the CPU sees them because it's between the CPU and the semihosting backend to share data.

Beside the sysemu curiosity, the patch logic is fine, so:

Thanks,

Phil.

#6 lore raw

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

Quoted 3 lines · Philippe Mathieu-Daudé

For user-mode, this patch makes sense. For system-mode it is not obvious to make sense of SYS_HEAPINFO (except focusing in cores targeting embedded systems eventually).

The main user of semihosting in system mode is standalone binaries compiled with the gnu toolchain using a newlib that targets semihosting. These generally use SYS_HEAPINFO on startup to find out where to put the stack etc, rather than hardwiring it in the linker map. This applies to both M-profile and A-profile.

-- PMM

Reviewed-by on v5 1/2
#7 lore raw

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

In reply to Alex Bennée
Quoted 25 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

hw/core/loader.c +7 -0
+/*
+ * Sort into address order. We break ties between rom-startpoints
+ * and rom-endpoints in favour of the startpoint, by sorting the 0->1
+ * transition before the 1->0 transition. Either way round would
+ * work, but this way saves a little work later by avoiding
+ * dealing with "gaps" of 0 length.
+ */

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

Quoted 10 lines
hw/core/loader.c +10 -0
+static gint sort_secs(gconstpointer a, gconstpointer b)
+{
+    RomSec *ra = (RomSec *) a;
+    RomSec *rb = (RomSec *) b;
+
+    if (ra->base == rb->base) {
+        return ra->se - rb->se;
+    }
+    return ra->base > rb->base ? 1 : -1;
+}

This sort comparator still doesn't report the equality case as actually equal.

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

Quoted 6 lines
     /*
semihosting/arm-compat-semi.c +2 -3
-     * 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

"work out"

Quoted 8 lines
semihosting/arm-compat-semi.c +1 -1
+     * equally between stack and heap.
      */
@@ -1201,12 +1205,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 1212

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.
+             */

What is this comment referring to? We aren't obviously reporting 0 here...

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

Quoted 7 lines
semihosting/arm-compat-semi.c +4 -0
+            retvals[0] = info.heapbase;  /* Heap Base */
+            retvals[1] = info.heaplimit; /* Heap Limit */
+            retvals[2] = info.heaplimit; /* Stack base */
+            retvals[3] = info.heapbase;  /* Stack limit.  */
 #endif
 
             for (i = 0; i < ARRAY_SIZE(retvals); i++) {

Otherwise

-- PMM

#8 lore raw

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

Quoted 31 lines · Peter Maydell
Quoted 30 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

hw/core/loader.c +12 -0
+/*
+ * Sort into address order. We break ties between rom-startpoints
+ * and rom-endpoints in favour of the startpoint, by sorting the 0->1
+ * transition before the 1->0 transition. Either way round would
+ * work, but this way saves a little work later by avoiding
+ * dealing with "gaps" of 0 length.
+ */
+static gint sort_secs(gconstpointer a, gconstpointer b)
+{
+    RomSec *ra = (RomSec *) a;
+    RomSec *rb = (RomSec *) b;
+
Quoted 8 lines

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

Quoted 5 lines
hw/core/loader.c +5 -0
+    if (ra->base == rb->base) {
+        return ra->se - rb->se;
+    }
+    return ra->base > rb->base ? 1 : -1;
+}

This sort comparator still doesn't report the equality case as actually equal.

When ra->se and rb->se are the same it returns 0. Is that not what you want?

Quoted 23 lines

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

Quoted 7 lines
     /*
semihosting/arm-compat-semi.c +2 -3
-     * 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

"work out"

Quoted 13 lines
semihosting/arm-compat-semi.c +1 -6
+     * equally between stack and heap.
      */
@@ -1201,12 +1205,15 @@ target_ulong do_common_semihosting(CPUState *cs)
             retvals[2] = ts->stack_base;
             retvals[3] = 0; /* Stack limit.  */
 #else
-            limit = current_machine->ram_size;
-            /* 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.  */
Quoted 8 lines

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

Quoted 5 lines
semihosting/arm-compat-semi.c +5 -0
+            /*
+             * Reporting 0 indicates we couldn't calculate the real
+             * values which should force most software to fall back to
+             * using information it has.
+             */

What is this comment referring to? We aren't obviously reporting 0 here...

Stale comment, deleted.

Quoted 13 lines

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

Quoted 8 lines
semihosting/arm-compat-semi.c +4 -0
+            retvals[0] = info.heapbase;  /* Heap Base */
+            retvals[1] = info.heaplimit; /* Heap Limit */
+            retvals[2] = info.heaplimit; /* Stack base */
+            retvals[3] = info.heapbase;  /* Stack limit.  */
 #endif
 
             for (i = 0; i < ARRAY_SIZE(retvals); i++) {

Otherwise

-- PMM

Signature · 1 line
Alex Bennée
#9 lore raw

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

Quoted 44 lines · Alex Bennée
Quoted 31 lines · Peter Maydell
Quoted 30 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

hw/core/loader.c +12 -0
+/*
+ * Sort into address order. We break ties between rom-startpoints
+ * and rom-endpoints in favour of the startpoint, by sorting the 0->1
+ * transition before the 1->0 transition. Either way round would
+ * work, but this way saves a little work later by avoiding
+ * dealing with "gaps" of 0 length.
+ */
+static gint sort_secs(gconstpointer a, gconstpointer b)
+{
+    RomSec *ra = (RomSec *) a;
+    RomSec *rb = (RomSec *) b;
+
Quoted 8 lines

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

Quoted 5 lines
hw/core/loader.c +5 -0
+    if (ra->base == rb->base) {
+        return ra->se - rb->se;
+    }
+    return ra->base > rb->base ? 1 : -1;
+}

This sort comparator still doesn't report the equality case as actually equal.

Quoted 1 line

When ra->se and rb->se are the same it returns 0. Is that not what you want?

Oops, yes it does. I misread it because I was expecting it to be structured differently. (AFAIK there is no "standard" way to structure a comparator-function that works with multiple fields, so the way you have it is fine.)

-- PMM

#10 lore raw

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

Quoted 45 lines · Peter Maydell
Quoted 44 lines · Alex Bennée
Quoted 39 lines · Peter Maydell
Quoted 25 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:

a) scans the MemoryRegions for the largest RAM block
b) iterates through all "ROM" blobs looking for the biggest gap

The "ROM" blobs include all code loaded via -kernel and the various -device loader techniques.

Cc Andrew Strauss <[email protected]>
Cc Keith Packard <[email protected]>

Message-Id: <[email protected]>

hw/core/loader.c +7 -0
+/*
+ * Sort into address order. We break ties between rom-startpoints
+ * and rom-endpoints in favour of the startpoint, by sorting the 0->1
+ * transition before the 1->0 transition. Either way round would
+ * work, but this way saves a little work later by avoiding
+ * dealing with "gaps" of 0 length.
+ */

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

Quoted 10 lines
hw/core/loader.c +10 -0
+static gint sort_secs(gconstpointer a, gconstpointer b)
+{
+    RomSec *ra = (RomSec *) a;
+    RomSec *rb = (RomSec *) b;
+
+    if (ra->base == rb->base) {
+        return ra->se - rb->se;
+    }
+    return ra->base > rb->base ? 1 : -1;
+}

This sort comparator still doesn't report the equality case as actually equal.

Quoted 6 lines
Quoted 1 line

When ra->se and rb->se are the same it returns 0. Is that not what you want?

Oops, yes it does. I misread it because I was expecting it to be structured differently. (AFAIK there is no "standard" way to structure a comparator-function that works with multiple fields, so the way you have it is fine.)

The other options were all ugly ;-)

Signature · 1 line
Alex Bennée
#11 lore raw

PATCH v5 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.

---
v5
- static init of heapinfo structure
- clean-up comment on why we can test stack position
- add memory clobber for semicall
- test we can read/write to a portion of the heap
- fix MAINTAINERS

---
 tests/tcg/aarch64/system/semiheap.c | 93 +++++++++++++++++++++++++++++
 MAINTAINERS                         |  1 +
 2 files changed, 94 insertions(+)
 create mode 100644 tests/tcg/aarch64/system/semiheap.c
tests/tcg/aarch64/system/semiheap.c new file +93 -0
new file mode 100644
index 0000000000..4ed258476d
@@ -0,0 +1,93 @@
+/*
+ * 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)
+        : "memory" );
+
+    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;
+    uint32_t *ptr_to_heap;
+    int i;
+
+    ml_printf("Semihosting Heap Info Test\n");
+
+    __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;
+    }
+
+    /*
+     * boot.S put our stack somewhere inside the data segment of the
+     * ELF file, and we know that SYS_HEAPINFO won't pick a range
+     * that overlaps with part of a loaded ELF file. So the info
+     * struct (on the stack) should not be inside the reported 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);
+
+    /* finally can we read/write the heap */
+    ptr_to_heap = (uint32_t *) info.heap_base;
+    for (i = 0; i < 512; i++) {
+        *ptr_to_heap++ = i;
+    }
+    ptr_to_heap = (uint32_t *) info.heap_base;
+    for (i = 0; i < 512; i++) {
+        uint32_t tmp = *ptr_to_heap;
+        if (tmp != i) {
+            ml_printf("unexpected value in heap: %d @ %p", tmp, ptr_to_heap);
+            return -5;
+        }
+        ptr_to_heap++;
+    }
+    ml_printf("r/w to heap upto %p\n", ptr_to_heap);
+
+    ml_printf("Passed HeapInfo checks\n");
+    return 0;
+}
MAINTAINERS +1 -0
index b0b845f445..251f96af9e 100644
@@ -3536,6 +3536,7 @@ S: Maintained
 F: semihosting/
 F: include/semihosting/
 F: tests/tcg/multiarch/arm-compat-semi/
+F: tests/tcg/aarch64/system/semiheap.c
 
 Multi-process QEMU
 M: Elena Ufimtseva <[email protected]>
Signature · 1 line
2.30.2