All of lore.kernel.org
 help / color / mirror / Atom feed
From: Keith Packard via <qemu-devel@nongnu.org>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Alistair Francis" <Alistair.Francis@wdc.com>,
	"Bastian Koppelmann" <kbastian@mail.uni-paderborn.de>,
	"Laurent Vivier" <laurent@vivier.eu>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	qemu-arm@nongnu.org, qemu-riscv@nongnu.org,
	"Sagar Karandikar" <sagark@eecs.berkeley.edu>,
	"Keith Packard" <keithp@keithp.com>
Subject: [PATCH 4/8] semihosting: Support SYS_HEAPINFO when env->boot_info is not set
Date: Wed, 25 Nov 2020 13:36:13 -0800	[thread overview]
Message-ID: <20201125213617.2496935-5-keithp@keithp.com> (raw)
In-Reply-To: <20201125213617.2496935-1-keithp@keithp.com>

env->boot_info is only set in some ARM startup paths, so we cannot
rely on it to support the SYS_HEAPINFO semihosting function. When not
available, fallback to finding a RAM memory region containing the
current stack and use the base of that.

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 hw/semihosting/common-semi.c | 42 +++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/hw/semihosting/common-semi.c b/hw/semihosting/common-semi.c
index 27bdfd0e83..ddfa448cc2 100644
--- a/hw/semihosting/common-semi.c
+++ b/hw/semihosting/common-semi.c
@@ -133,6 +133,35 @@ 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
+ */
+static inline hwaddr
+common_semi_find_region_base(hwaddr addr)
+{
+    MemoryRegion *subregion;
+
+    /*
+     * 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.
+     */
+    QTAILQ_FOREACH(subregion, &get_system_memory()->subregions,
+                   subregions_link) {
+        if (subregion->ram && !subregion->readonly) {
+            Int128 top128 = int128_add(int128_make64(subregion->addr),
+                                       subregion->size);
+            if (subregion->addr <= addr && int128_lt(addr, top128)) {
+                return subregion->addr;
+            }
+        }
+    }
+    return 0;
+}
+#endif
+
 #ifdef TARGET_ARM
 static inline target_ulong
 common_semi_arg(CPUState *cs, int argno)
@@ -171,7 +200,18 @@ common_semi_rambase(CPUState *cs)
 {
     CPUArchState *env = cs->env_ptr;
     const struct arm_boot_info *info = env->boot_info;
-    return info->loader_start;
+    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
 
-- 
2.29.2



WARNING: multiple messages have this Message-ID (diff)
From: Keith Packard <keithp@keithp.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Alistair Francis" <Alistair.Francis@wdc.com>,
	"Bastian Koppelmann" <kbastian@mail.uni-paderborn.de>,
	"Laurent Vivier" <laurent@vivier.eu>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	qemu-arm@nongnu.org, qemu-riscv@nongnu.org,
	"Sagar Karandikar" <sagark@eecs.berkeley.edu>,
	"Keith Packard" <keithp@keithp.com>
Subject: [PATCH 4/8] semihosting: Support SYS_HEAPINFO when env->boot_info is not set
Date: Wed, 25 Nov 2020 13:36:13 -0800	[thread overview]
Message-ID: <20201125213617.2496935-5-keithp@keithp.com> (raw)
In-Reply-To: <20201125213617.2496935-1-keithp@keithp.com>

env->boot_info is only set in some ARM startup paths, so we cannot
rely on it to support the SYS_HEAPINFO semihosting function. When not
available, fallback to finding a RAM memory region containing the
current stack and use the base of that.

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 hw/semihosting/common-semi.c | 42 +++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/hw/semihosting/common-semi.c b/hw/semihosting/common-semi.c
index 27bdfd0e83..ddfa448cc2 100644
--- a/hw/semihosting/common-semi.c
+++ b/hw/semihosting/common-semi.c
@@ -133,6 +133,35 @@ 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
+ */
+static inline hwaddr
+common_semi_find_region_base(hwaddr addr)
+{
+    MemoryRegion *subregion;
+
+    /*
+     * 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.
+     */
+    QTAILQ_FOREACH(subregion, &get_system_memory()->subregions,
+                   subregions_link) {
+        if (subregion->ram && !subregion->readonly) {
+            Int128 top128 = int128_add(int128_make64(subregion->addr),
+                                       subregion->size);
+            if (subregion->addr <= addr && int128_lt(addr, top128)) {
+                return subregion->addr;
+            }
+        }
+    }
+    return 0;
+}
+#endif
+
 #ifdef TARGET_ARM
 static inline target_ulong
 common_semi_arg(CPUState *cs, int argno)
@@ -171,7 +200,18 @@ common_semi_rambase(CPUState *cs)
 {
     CPUArchState *env = cs->env_ptr;
     const struct arm_boot_info *info = env->boot_info;
-    return info->loader_start;
+    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
 
-- 
2.29.2



  parent reply	other threads:[~2020-11-25 21:44 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-25 21:36 [PATCH 0/8] Add RISC-V semihosting 0.2. Finish ARM semihosting 2.0 Keith Packard via
2020-11-25 21:36 ` Keith Packard
2020-11-25 21:36 ` [PATCH 1/8] semihosting: Move ARM semihosting code to shared directories [v3] Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-11-25 21:36 ` [PATCH 2/8] semihosting: Change common-semi API to be architecture-independent Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-11-25 21:36 ` [PATCH 3/8] semihosting: Change internal common-semi interfaces to use CPUState * [v2] Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-11-25 21:36 ` Keith Packard via [this message]
2020-11-25 21:36   ` [PATCH 4/8] semihosting: Support SYS_HEAPINFO when env->boot_info is not set Keith Packard
2020-11-25 21:36 ` [PATCH 5/8] riscv: Add semihosting support [v13] Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-12-09  7:58   ` Kito Cheng
2020-12-09  7:58     ` Kito Cheng
2020-12-09 16:29     ` Keith Packard via
2020-12-09 16:29       ` Keith Packard
2020-12-10  3:39       ` Kito Cheng
2020-12-10  3:39         ` Kito Cheng
2020-12-10  6:21         ` Keith Packard via
2020-12-10  6:21           ` Keith Packard
2020-12-14 11:29         ` Alex Bennée
2020-12-14 11:29           ` Alex Bennée
2020-11-25 21:36 ` [PATCH 6/8] semihosting: Implement SYS_ELAPSED and SYS_TICKFREQ Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-11-25 21:36 ` [PATCH 7/8] semihosting: Implement SYS_TMPNAM Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-11-25 21:36 ` [PATCH 8/8] semihosting: Implement SYS_ISERROR Keith Packard via
2020-11-25 21:36   ` Keith Packard
2020-12-14 11:24 ` [PATCH 0/8] Add RISC-V semihosting 0.2. Finish ARM semihosting 2.0 Alex Bennée
2020-12-14 11:24   ` Alex Bennée
2020-12-14 14:58   ` Alex Bennée
2020-12-14 14:58     ` Alex Bennée
2020-12-14 20:06     ` Keith Packard via
2020-12-14 20:06       ` Keith Packard
2020-12-14 20:07     ` [PATCH 0/9] " Keith Packard via
2020-12-14 20:07       ` Keith Packard
2020-12-14 20:07       ` [PATCH 1/9] semihosting: Move ARM semihosting code to shared directories Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 2/9] semihosting: Change common-semi API to be architecture-independent Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 3/9] semihosting: Change internal common-semi interfaces to use CPUState * Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 4/9] semihosting: Support SYS_HEAPINFO when env->boot_info is not set Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 5/9] riscv: Add semihosting support Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 6/9] riscv: Add semihosting support for user mode Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 7/9] semihosting: Implement SYS_ELAPSED and SYS_TICKFREQ Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 8/9] semihosting: Implement SYS_TMPNAM Keith Packard via
2020-12-14 20:07         ` Keith Packard
2020-12-14 20:07       ` [PATCH 9/9] semihosting: Implement SYS_ISERROR Keith Packard via
2020-12-14 20:07         ` Keith Packard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201125213617.2496935-5-keithp@keithp.com \
    --to=qemu-devel@nongnu.org \
    --cc=Alistair.Francis@wdc.com \
    --cc=alex.bennee@linaro.org \
    --cc=kbastian@mail.uni-paderborn.de \
    --cc=keithp@keithp.com \
    --cc=laurent@vivier.eu \
    --cc=palmer@dabbelt.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=sagark@eecs.berkeley.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.