Skip to contentSkip to conversation

semihosting/next (move from hw, heapinfo)

PATCH In review Alex Bennée 5 participants 15 messages Mar 5, 2021 – Mar 8, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover semihosting/next (move from hw, heapinfo)

Hi,

Not much in this series apart from moving semihosting out of hw (where it sat a bit weirdly) and an attempt at fixing a bug in the SYS_HEAPINFO code. It works AFAICT but still seems a little fugly to me. See:

- semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Alex Bennée (1):
semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Philippe Mathieu-Daudé (2):
semihosting: Move include/hw/semihosting/ -> include/semihosting/
semihosting: Move hw/semihosting/ -> semihosting/

 meson.build                                   |   1 +
 include/{hw => }/semihosting/console.h        |   0
 include/{hw => }/semihosting/semihost.h       |   0
 {hw/semihosting => semihosting}/common-semi.h |   0
 tests/tcg/arm/semicall.h                      |   1 +
 gdbstub.c                                     |   2 +-
 hw/mips/malta.c                               |   2 +-
 linux-user/aarch64/cpu_loop.c                 |   2 +-
 linux-user/arm/cpu_loop.c                     |   2 +-
 linux-user/riscv/cpu_loop.c                   |   2 +-
 linux-user/semihost.c                         |   2 +-
 .../arm-compat-semi.c                         | 135 ++++++++++--------
 {hw/semihosting => semihosting}/config.c      |   2 +-
 {hw/semihosting => semihosting}/console.c     |   4 +-
 softmmu/vl.c                                  |   2 +-
 stubs/semihost.c                              |   2 +-
 target/arm/helper.c                           |   4 +-
 target/arm/m_helper.c                         |   4 +-
 target/arm/translate-a64.c                    |   2 +-
 target/arm/translate.c                        |   2 +-
 target/lm32/helper.c                          |   2 +-
 target/m68k/op_helper.c                       |   2 +-
 target/mips/cpu.c                             |   2 +-
 target/mips/mips-semi.c                       |   4 +-
 target/mips/translate.c                       |   2 +-
 target/nios2/helper.c                         |   2 +-
 target/riscv/cpu_helper.c                     |   2 +-
 target/unicore32/helper.c                     |   2 +-
 target/xtensa/translate.c                     |   2 +-
 target/xtensa/xtensa-semi.c                   |   2 +-
 tests/tcg/arm/semihosting.c                   |  34 ++++-
 Kconfig                                       |   1 +
 MAINTAINERS                                   |   4 +-
 hw/Kconfig                                    |   1 -
 hw/meson.build                                |   1 -
 {hw/semihosting => semihosting}/Kconfig       |   0
 {hw/semihosting => semihosting}/meson.build   |   0
 37 files changed, 142 insertions(+), 92 deletions(-)
 rename include/{hw => }/semihosting/console.h (100%)
 rename include/{hw => }/semihosting/semihost.h (100%)
 rename {hw/semihosting => semihosting}/common-semi.h (100%)
 rename {hw/semihosting => semihosting}/arm-compat-semi.c (94%)
 rename {hw/semihosting => semihosting}/config.c (99%)
 rename {hw/semihosting => semihosting}/console.c (98%)
 rename {hw/semihosting => semihosting}/Kconfig (100%)
 rename {hw/semihosting => semihosting}/meson.build (100%)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/3 semihosting: Move include/hw/semihosting/ -> include/semihosting/

From: Philippe Mathieu-Daudé <[email protected]>

We want to move the semihosting code out of hw/ in the next patch.

This patch contains the mechanical steps, created using:

  $ git mv include/hw/semihosting/ include/
  $ sed -i s,hw/semihosting,semihosting, $(git grep -l hw/semihosting)

Message-Id: <[email protected]>

---
 include/{hw => }/semihosting/console.h  | 0
 include/{hw => }/semihosting/semihost.h | 0
 gdbstub.c                               | 2 +-
 hw/mips/malta.c                         | 2 +-
 hw/semihosting/arm-compat-semi.c        | 6 +++---
 hw/semihosting/config.c                 | 2 +-
 hw/semihosting/console.c                | 4 ++--
 linux-user/aarch64/cpu_loop.c           | 2 +-
 linux-user/arm/cpu_loop.c               | 2 +-
 linux-user/riscv/cpu_loop.c             | 2 +-
 linux-user/semihost.c                   | 2 +-
 softmmu/vl.c                            | 2 +-
 stubs/semihost.c                        | 2 +-
 target/arm/helper.c                     | 4 ++--
 target/arm/m_helper.c                   | 4 ++--
 target/arm/translate-a64.c              | 2 +-
 target/arm/translate.c                  | 2 +-
 target/lm32/helper.c                    | 2 +-
 target/m68k/op_helper.c                 | 2 +-
 target/mips/cpu.c                       | 2 +-
 target/mips/mips-semi.c                 | 4 ++--
 target/mips/translate.c                 | 2 +-
 target/nios2/helper.c                   | 2 +-
 target/riscv/cpu_helper.c               | 2 +-
 target/unicore32/helper.c               | 2 +-
 target/xtensa/translate.c               | 2 +-
 target/xtensa/xtensa-semi.c             | 2 +-
 MAINTAINERS                             | 2 +-
 28 files changed, 32 insertions(+), 32 deletions(-)
 rename include/{hw => }/semihosting/console.h (100%)
 rename include/{hw => }/semihosting/semihost.h (100%)
include/semihosting/console.h renamed from include/hw/semihosting/console.h +0 -0
similarity index 100%
rename from include/hw/semihosting/console.h
rename to include/semihosting/console.h
include/semihosting/semihost.h renamed from include/hw/semihosting/semihost.h +0 -0
similarity index 100%
rename from include/hw/semihosting/semihost.h
rename to include/semihosting/semihost.h
gdbstub.c +1 -1
index 3ee40479b6..e51e33cc70 100644
@@ -49,7 +49,7 @@
 #include "sysemu/hw_accel.h"
 #include "sysemu/kvm.h"
 #include "sysemu/runstate.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "exec/exec-all.h"
 #include "sysemu/replay.h"
 
hw/mips/malta.c +1 -1
index 9afc0b427b..26e7b1bd9f 100644
@@ -58,7 +58,7 @@
 #include "qemu/error-report.h"
 #include "hw/misc/empty_slot.h"
 #include "sysemu/kvm.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "hw/mips/cps.h"
 #include "hw/qdev-clock.h"
 
hw/semihosting/arm-compat-semi.c +3 -3
index 23c6e3edcb..94950b6c56 100644
@@ -34,9 +34,9 @@
 #include "qemu/osdep.h"
 
 #include "cpu.h"
-#include "hw/semihosting/semihost.h"
-#include "hw/semihosting/console.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/semihost.h"
+#include "semihosting/console.h"
+#include "semihosting/common-semi.h"
 #include "qemu/log.h"
 #include "qemu/timer.h"
 #ifdef CONFIG_USER_ONLY
hw/semihosting/config.c +1 -1
index 9807f10cb0..3548e0f627 100644
@@ -22,7 +22,7 @@
 #include "qemu/option.h"
 #include "qemu/config-file.h"
 #include "qemu/error-report.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "chardev/char.h"
 #include "sysemu/sysemu.h"
 
hw/semihosting/console.c +2 -2
index 9b4fee9260..c9ebd6fdd0 100644
@@ -17,8 +17,8 @@
 
 #include "qemu/osdep.h"
 #include "cpu.h"
-#include "hw/semihosting/semihost.h"
-#include "hw/semihosting/console.h"
+#include "semihosting/semihost.h"
+#include "semihosting/console.h"
 #include "exec/gdbstub.h"
 #include "exec/exec-all.h"
 #include "qemu/log.h"
linux-user/aarch64/cpu_loop.c +1 -1
index 7c42f65706..ee72a1c20f 100644
@@ -22,7 +22,7 @@
 #include "qemu.h"
 #include "cpu_loop-common.h"
 #include "qemu/guest-random.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/common-semi.h"
 #include "target/arm/syndrome.h"
 
 #define get_user_code_u32(x, gaddr, env)                \
linux-user/arm/cpu_loop.c +1 -1
index cadfb7fa43..989d03cd89 100644
@@ -22,7 +22,7 @@
 #include "qemu.h"
 #include "elf.h"
 #include "cpu_loop-common.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/common-semi.h"
 
 #define get_user_code_u32(x, gaddr, env)                \
     ({ abi_long __r = get_user_u32((x), (gaddr));       \
linux-user/riscv/cpu_loop.c +1 -1
index 9665dabb09..6767f941e8 100644
@@ -23,7 +23,7 @@
 #include "qemu.h"
 #include "cpu_loop-common.h"
 #include "elf.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/common-semi.h"
 
 void cpu_loop(CPURISCVState *env)
 {
linux-user/semihost.c +1 -1
index c0015ee7f6..82013b8b48 100644
@@ -12,7 +12,7 @@
 
 #include "qemu/osdep.h"
 #include "cpu.h"
-#include "hw/semihosting/console.h"
+#include "semihosting/console.h"
 #include "qemu.h"
 #include <termios.h>
 
softmmu/vl.c +1 -1
index 10bd8a10a3..ac06dfbae0 100644
@@ -108,7 +108,7 @@
 #include "qapi/opts-visitor.h"
 #include "qapi/clone-visitor.h"
 #include "qom/object_interfaces.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "crypto/init.h"
 #include "sysemu/replay.h"
 #include "qapi/qapi-events-run-state.h"
stubs/semihost.c +1 -1
index 1d8b37f7b2..1b30f38b03 100644
@@ -11,7 +11,7 @@
 #include "qemu/osdep.h"
 #include "qemu/option.h"
 #include "qemu/error-report.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "sysemu/sysemu.h"
 
 /* Empty config */
target/arm/helper.c +2 -2
index 0e1a3b9421..d763f376c6 100644
@@ -22,7 +22,7 @@
 #include "exec/exec-all.h"
 #include <zlib.h> /* For crc32 */
 #include "hw/irq.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "sysemu/cpus.h"
 #include "sysemu/cpu-timers.h"
 #include "sysemu/kvm.h"
@@ -34,7 +34,7 @@
 #ifdef CONFIG_TCG
 #include "arm_ldst.h"
 #include "exec/cpu_ldst.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/common-semi.h"
 #endif
 
 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
target/arm/m_helper.c +2 -2
index 731c435c00..d63ae465e1 100644
@@ -21,7 +21,7 @@
 #include "qemu/qemu-print.h"
 #include "exec/exec-all.h"
 #include <zlib.h> /* For crc32 */
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "sysemu/cpus.h"
 #include "sysemu/kvm.h"
 #include "qemu/range.h"
@@ -31,7 +31,7 @@
 #ifdef CONFIG_TCG
 #include "arm_ldst.h"
 #include "exec/cpu_ldst.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/common-semi.h"
 #endif
 
 static void v7m_msr_xpsr(CPUARMState *env, uint32_t mask,
target/arm/translate-a64.c +1 -1
index b23a8975d5..6d002e2c63 100644
@@ -28,7 +28,7 @@
 #include "internals.h"
 #include "qemu/host-utils.h"
 
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "exec/gen-icount.h"
 
 #include "exec/helper-proto.h"
target/arm/translate.c +1 -1
index 1653cca1aa..62b1c2081b 100644
@@ -29,7 +29,7 @@
 #include "qemu/log.h"
 #include "qemu/bitops.h"
 #include "arm_ldst.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 
 #include "exec/helper-proto.h"
 #include "exec/helper-gen.h"
target/lm32/helper.c +1 -1
index 7c52ae76d6..01cc3c53a5 100644
@@ -21,7 +21,7 @@
 #include "cpu.h"
 #include "exec/exec-all.h"
 #include "qemu/host-utils.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "exec/log.h"
 
 bool lm32_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
target/m68k/op_helper.c +1 -1
index 202498deb5..730cdf7744 100644
@@ -21,7 +21,7 @@
 #include "exec/helper-proto.h"
 #include "exec/exec-all.h"
 #include "exec/cpu_ldst.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 
 #if defined(CONFIG_USER_ONLY)
 
target/mips/cpu.c +1 -1
index bf70c77295..bd4dca571f 100644
@@ -31,7 +31,7 @@
 #include "exec/exec-all.h"
 #include "hw/qdev-properties.h"
 #include "hw/qdev-clock.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "qapi/qapi-commands-machine-target.h"
 #include "fpu_helper.h"
 
target/mips/mips-semi.c +2 -2
index 898251aa02..6de60fa6dd 100644
@@ -22,8 +22,8 @@
 #include "qemu/log.h"
 #include "exec/helper-proto.h"
 #include "exec/softmmu-semi.h"
-#include "hw/semihosting/semihost.h"
-#include "hw/semihosting/console.h"
+#include "semihosting/semihost.h"
+#include "semihosting/console.h"
 
 typedef enum UHIOp {
     UHI_exit = 1,
target/mips/translate.c +1 -1
index 70891c37cd..0b6d82d228 100644
@@ -29,7 +29,7 @@
 #include "exec/translator.h"
 #include "exec/helper-proto.h"
 #include "exec/helper-gen.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 
 #include "target/mips/trace.h"
 #include "trace-tcg.h"
target/nios2/helper.c +1 -1
index 57c97bde3c..53be8398e9 100644
@@ -26,7 +26,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/log.h"
 #include "exec/helper-proto.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 
 #if defined(CONFIG_USER_ONLY)
 
target/riscv/cpu_helper.c +1 -1
index 2f43939fb6..83a6bcfad0 100644
@@ -24,7 +24,7 @@
 #include "exec/exec-all.h"
 #include "tcg/tcg-op.h"
 #include "trace.h"
-#include "hw/semihosting/common-semi.h"
+#include "semihosting/common-semi.h"
 
 int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch)
 {
target/unicore32/helper.c +1 -1
index 54c26871fe..704393c27f 100644
@@ -14,7 +14,7 @@
 #include "cpu.h"
 #include "exec/exec-all.h"
 #include "exec/helper-proto.h"
-#include "hw/semihosting/console.h"
+#include "semihosting/console.h"
 
 #undef DEBUG_UC32
 
target/xtensa/translate.c +1 -1
index 944a157747..0ae4efc48a 100644
@@ -37,7 +37,7 @@
 #include "qemu/log.h"
 #include "qemu/qemu-print.h"
 #include "exec/cpu_ldst.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "exec/translator.h"
 
 #include "exec/helper-proto.h"
target/xtensa/xtensa-semi.c +1 -1
index 25f57a6500..79f2b043f2 100644
@@ -29,7 +29,7 @@
 #include "cpu.h"
 #include "chardev/char-fe.h"
 #include "exec/helper-proto.h"
-#include "hw/semihosting/semihost.h"
+#include "semihosting/semihost.h"
 #include "qapi/error.h"
 #include "qemu/log.h"
 
MAINTAINERS +1 -1
index 1443278059..37ddf90669 100644
@@ -3227,7 +3227,7 @@ Semihosting
 M: Alex Bennée <[email protected]>
 S: Maintained
 F: hw/semihosting/
-F: include/hw/semihosting/
+F: include/semihosting/
 
 Multi-process QEMU
 M: Elena Ufimtseva <[email protected]>
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/3 semihosting: Move hw/semihosting/ -> semihosting/

In reply to Alex Bennée

From: Philippe Mathieu-Daudé <[email protected]>

With the exception of hw/core/, the hw/ directory only contains device models used in system emulation. Semihosting is also used by user emulation. As a generic feature, move it out of hw/ directory.

Message-Id: <[email protected]>

---
 meson.build                                       | 1 +
 {hw/semihosting => semihosting}/common-semi.h     | 0
 {hw/semihosting => semihosting}/arm-compat-semi.c | 0
 {hw/semihosting => semihosting}/config.c          | 0
 {hw/semihosting => semihosting}/console.c         | 0
 Kconfig                                           | 1 +
 MAINTAINERS                                       | 2 +-
 hw/Kconfig                                        | 1 -
 hw/meson.build                                    | 1 -
 {hw/semihosting => semihosting}/Kconfig           | 0
 {hw/semihosting => semihosting}/meson.build       | 0
 11 files changed, 3 insertions(+), 3 deletions(-)
 rename {hw/semihosting => semihosting}/common-semi.h (100%)
 rename {hw/semihosting => semihosting}/arm-compat-semi.c (100%)
 rename {hw/semihosting => semihosting}/config.c (100%)
 rename {hw/semihosting => semihosting}/console.c (100%)
 rename {hw/semihosting => semihosting}/Kconfig (100%)
 rename {hw/semihosting => semihosting}/meson.build (100%)
meson.build +1 -0
index 07bc23129a..3578692315 100644
@@ -1939,6 +1939,7 @@ subdir('migration')
 subdir('monitor')
 subdir('net')
 subdir('replay')
+subdir('semihosting')
 subdir('hw')
 subdir('accel')
 subdir('plugins')
semihosting/common-semi.h renamed from hw/semihosting/common-semi.h +0 -0
similarity index 100%
rename from hw/semihosting/common-semi.h
rename to semihosting/common-semi.h
semihosting/arm-compat-semi.c renamed from hw/semihosting/arm-compat-semi.c +0 -0
similarity index 100%
rename from hw/semihosting/arm-compat-semi.c
rename to semihosting/arm-compat-semi.c
semihosting/config.c renamed from hw/semihosting/config.c +0 -0
similarity index 100%
rename from hw/semihosting/config.c
rename to semihosting/config.c
semihosting/console.c renamed from hw/semihosting/console.c +0 -0
similarity index 100%
rename from hw/semihosting/console.c
rename to semihosting/console.c
Kconfig +1 -0
index bf694c42af..d52ebd839b 100644
@@ -2,3 +2,4 @@ source Kconfig.host
 source backends/Kconfig
 source accel/Kconfig
 source hw/Kconfig
+source semihosting/Kconfig
MAINTAINERS +1 -1
index 37ddf90669..90118a537a 100644
@@ -3226,7 +3226,7 @@ F: qapi/rdma.json
 Semihosting
 M: Alex Bennée <[email protected]>
 S: Maintained
-F: hw/semihosting/
+F: semihosting/
 F: include/semihosting/
 
 Multi-process QEMU
hw/Kconfig +0 -1
index 8ea26479c4..ff40bd3f7b 100644
@@ -31,7 +31,6 @@ source remote/Kconfig
 source rtc/Kconfig
 source scsi/Kconfig
 source sd/Kconfig
-source semihosting/Kconfig
 source smbios/Kconfig
 source ssi/Kconfig
 source timer/Kconfig
hw/meson.build +0 -1
index e615d72d4d..8ba79b1a52 100644
@@ -30,7 +30,6 @@ subdir('rdma')
 subdir('rtc')
 subdir('scsi')
 subdir('sd')
-subdir('semihosting')
 subdir('smbios')
 subdir('ssi')
 subdir('timer')
semihosting/Kconfig renamed from hw/semihosting/Kconfig +0 -0
similarity index 100%
rename from hw/semihosting/Kconfig
rename to semihosting/Kconfig
semihosting/meson.build renamed from hw/semihosting/meson.build +0 -0
similarity index 100%
rename from hw/semihosting/meson.build
rename to semihosting/meson.build
Signature · 1 line
2.20.1
#3 lore raw

PATCH 3/3 semihosting/arg-compat: fix up handling of SYS_HEAPINFO

In reply to Alex Bennée

I'm not sure this every worked properly and it's certainly not exercised by check-tcg or Peter's semihosting tests. Hoist it into it's own helper function and attempt to validate the results in the linux-user semihosting test at the least.

---
 tests/tcg/arm/semicall.h      |   1 +
 semihosting/arm-compat-semi.c | 129 +++++++++++++++++++---------------
 tests/tcg/arm/semihosting.c   |  34 ++++++++-
 3 files changed, 107 insertions(+), 57 deletions(-)
tests/tcg/arm/semicall.h +1 -0
index d4f6818192..676a542be5 100644
@@ -9,6 +9,7 @@
 
 #define SYS_WRITE0      0x04
 #define SYS_READC       0x07
+#define SYS_HEAPINFO    0x16
 #define SYS_REPORTEXC   0x18
 
 uintptr_t __semi_call(uintptr_t type, uintptr_t arg0)
semihosting/arm-compat-semi.c +73 -56

142 lines not on this page · raw message

tests/tcg/arm/semihosting.c +33 -1
index 33faac9916..fd5780ec3c 100644
@@ -7,7 +7,13 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
+#define _GNU_SOURCE  /* asprintf is a GNU extension */
+
 #include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
 #include "semicall.h"
 
 int main(int argc, char *argv[argc])
@@ -18,8 +24,34 @@ int main(int argc, char *argv[argc])
     uintptr_t exit_block[2] = {0x20026, 0};
     uintptr_t exit_code = (uintptr_t) &exit_block;
 #endif
+    struct {
+        void *heap_base;
+        void *heap_limit;
+        void *stack_base;
+        void *stack_limit;
+    } info;
+    void *ptr_to_info = (void *) &info;
+    char *heap_info, *stack_info;
+    void *brk = sbrk(0);
+
+    __semi_call(SYS_WRITE0, (uintptr_t) "Hello World\n");
+
+    memset(&info, 0, sizeof(info));
+    __semi_call(SYS_HEAPINFO, (uintptr_t) &ptr_to_info);
+
+    asprintf(&heap_info, "heap: %p -> %p\n", info.heap_base, info.heap_limit);
+    __semi_call(SYS_WRITE0, (uintptr_t) heap_info);
+    if (info.heap_base != brk) {
+        sprintf(heap_info, "heap mismatch: %p\n", brk);
+        __semi_call(SYS_WRITE0, (uintptr_t) heap_info);
+        return -1;
+    }
+
+    asprintf(&stack_info, "stack: %p -> %p\n", info.stack_base, info.stack_limit);
+    __semi_call(SYS_WRITE0, (uintptr_t) stack_info);
+    free(heap_info);
+    free(stack_info);
 
-    __semi_call(SYS_WRITE0, (uintptr_t) "Hello World");
     __semi_call(SYS_REPORTEXC, exit_code);
     /* if we get here we failed */
     return -1;
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 15 lines · Alex Bennée

I'm not sure this every worked properly and it's certainly not exercised by check-tcg or Peter's semihosting tests. Hoist it into it's own helper function and attempt to validate the results in the linux-user semihosting test at the least.

---
 tests/tcg/arm/semicall.h      |   1 +
 semihosting/arm-compat-semi.c | 129 +++++++++++++++++++---------------
 tests/tcg/arm/semihosting.c   |  34 ++++++++-
 3 files changed, 107 insertions(+), 57 deletions(-)

semihosting/arm-compat-semi.c static const GuestFDFunctions guestfd_fns[] = { · line 886

Quoted 9 lines
semihosting/arm-compat-semi.c +9 -0
+#else
+    limit = current_machine->ram_size;
+    /* TODO: Make this use the limit of the loaded application.  */
+    info.heap_base = rambase + limit / 2;
+    info.heap_limit = rambase + limit;
+    info.stack_base = rambase + limit; /* Stack base */
+    info.stack_limit = rambase; /* Stack limit.  */
+
+    if (cpu_memory_rw_debug(cs, arg0, &info, sizeof(info), true)) {

Blatting a C struct into guest memory has endianness and padding problems. Why not just do things the way the old Arm code did it ?

Also, you don't seem to have the correct "is the CPU in 32-bit or 64-bit mode" test here: you cannot rely on target_ulong being the right size, you must make a runtime check.

I suggested in the other email the way I think we should fix this.

thanks
-- PMM

#5 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 3 lines · Peter Maydell

Also, you don't seem to have the correct "is the CPU in 32-bit or 64-bit mode" test here: you cannot rely on target_ulong being the right size, you must make a runtime check.

Do you mean whether a dual aarch64/arm core is in arm or aarch64 mode, or whether an aarch64 is running a 32-bit ABI?

Signature · 1 line
-keith
#6 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 9 lines · Keith Packard
Quoted 3 lines · Peter Maydell

Also, you don't seem to have the correct "is the CPU in 32-bit or 64-bit mode" test here: you cannot rely on target_ulong being the right size, you must make a runtime check.

Do you mean whether a dual aarch64/arm core is in arm or aarch64 mode, or whether an aarch64 is running a 32-bit ABI?

For semihosting for Arm what matters is "what state is the core in at the point where it makes the semihosting SVC/HLT/etc insn?".

How does RISCV specify it?

thanks
-- PMM

#7 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 2 lines · Peter Maydell

For semihosting for Arm what matters is "what state is the core in at the point where it makes the semihosting SVC/HLT/etc insn?".

Ok, that means we *aren't* talking about -mabi=ilp32, which is good -- in my current picolibc implementation, the semihosting code uses a pure 64-bit interface for aarch64 targets, even when using ilp32 ABI.

Quoted 1 line

How does RISCV specify it?

Because the ISA is identical between 64- and 32- bit (and 128-bit) execution modes, the only difference between the two is the Machine XLEN value which encodes the native base integer ISA width. You switch modes by modifying this value.

I don't know of any implementation in hardware or software that supports modifying this value. I'm not sure we need to support this in the semihosting code for qemu as I'm pretty sure getting qemu to support dynamic XLEN values would be a large project (a project which I don't personally feel would offer much value).

Signature · 1 line
-keith
#8 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 4 lines · Keith Packard via

I don't know of any implementation in hardware or software that supports modifying this value. I'm not sure we need to support this in the semihosting code for qemu as I'm pretty sure getting qemu to support dynamic XLEN values would be a large project

I think it would be fairly trivial, now. We have riscv_cpu_is_32bit (which checks misa, not xlen, fwiw). It's not examined in cpu_get_tb_cpu_state(), but it is many other places.

Quoted 2 lines

(a project which I don't
personally feel would offer much value).

Now that's a different story. I'll take no position there.

However! We do have a function, that can be made into a TCG hook, which could then be queried by semihosting/.

r~

#9 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

In reply to Keith Packard
Quoted 9 lines · Keith Packard
Quoted 2 lines · Peter Maydell

For semihosting for Arm what matters is "what state is the core in at the point where it makes the semihosting SVC/HLT/etc insn?".

Ok, that means we *aren't* talking about -mabi=ilp32, which is good -- in my current picolibc implementation, the semihosting code uses a pure 64-bit interface for aarch64 targets, even when using ilp32 ABI.

ILP32 for AArch64 is a zombie target -- it is kinda-sorta supported in some toolchains but has no support in eg the Linux syscall ABI. The semihosting ABI does not implement any kind of ILP32 variant -- you can have A32/T32 (AArch32) semihosting, where register and field sizes are 32 bit, or you can have A64 (AArch64) semihosting, where register and field sizes are 64 bit.

Quoted 6 lines
Quoted 1 line

How does RISCV specify it?

Because the ISA is identical between 64- and 32- bit (and 128-bit) execution modes, the only difference between the two is the Machine XLEN value which encodes the native base integer ISA width. You switch modes by modifying this value.

I meant, how does the RISCV semihosting ABI specify what the field size is? To answer my own question, I just looked at the spec doc and it says "depends on whether the caller is 32-bit or 64-bit", which implies that we need to look at the current state of the CPU in some way.

Quoted 5 lines

I don't know of any implementation in hardware or software that supports modifying this value. I'm not sure we need to support this in the semihosting code for qemu as I'm pretty sure getting qemu to support dynamic XLEN values would be a large project (a project which I don't personally feel would offer much value).

Part of why I asked is that the current RISCV implementation is just looking at sizeof(target_ulong); but the qemu-system-riscv64 executable AIUI now supports emulating both "this is a 64 bit guest CPU" and "this is a 32 bit host CPU", and so looking at a QEMU-compile-time value like "sizeof(target_ulong)" will produce the wrong answer for 32-bit CPUs emulated in
the qemu-system-riscv64 binary. My guess is maybe
it should be looking at the result of riscv_cpu_is_32bit() instead.

thanks
-- PMM

#10 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 7 lines · Peter Maydell

ILP32 for AArch64 is a zombie target -- it is kinda-sorta supported in some toolchains but has no support in eg the Linux syscall ABI. The semihosting ABI does not implement any kind of ILP32 variant -- you can have A32/T32 (AArch32) semihosting, where register and field sizes are 32 bit, or you can have A64 (AArch64) semihosting, where register and field sizes are 64 bit.

Yeah, I did ILP32 support for Picolibc; all of the aarch64 asm support needed fixing as ilp32 doesn't specify that register arguments clear the top 32 bits. Seems pretty obvious that it's little used.

For semihosting, as the ABI isn't visible to the hardware/emulator, the only reasonable answer that I could come up with was to treat ILP32 the same as the LP64 and pass 64 bit parameters.

As picolibc is designed for bare-metal environments, it's pretty easy to support ilp32 otherwise.

Quoted 5 lines

I meant, how does the RISCV semihosting ABI specify what the field size is? To answer my own question, I just looked at the spec doc and it says "depends on whether the caller is 32-bit or 64-bit", which implies that we need to look at the current state of the CPU in some way.

Yes. As qemu currently fixes that value based on compilation parameters, we can use the relevant native types directly and ignore the CPU state. Adding dynamic XLEN support to qemu would involve a bunch of work as the same code can be run in both 64- and 32- bit modes, so you'd have to translate it twice and select which to execute based on the CPU state.

Quoted 8 lines

Part of why I asked is that the current RISCV implementation is just looking at sizeof(target_ulong); but the qemu-system-riscv64 executable AIUI now supports emulating both "this is a 64 bit guest CPU" and "this is a 32 bit host CPU", and so looking at a QEMU-compile-time value like "sizeof(target_ulong)" will produce the wrong answer for 32-bit CPUs emulated in
the qemu-system-riscv64 binary. My guess is maybe
it should be looking at the result of riscv_cpu_is_32bit() instead.

Wow. I read through the code and couldn't find anything that looked like it supported that, sounds like I must have missed something?

Signature · 1 line
-keith
#11 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 11 lines · Keith Packard
Quoted 8 lines · Peter Maydell

Part of why I asked is that the current RISCV implementation is just looking at sizeof(target_ulong); but the qemu-system-riscv64 executable AIUI now supports emulating both "this is a 64 bit guest CPU" and "this is a 32 bit host CPU", and so looking at a QEMU-compile-time value like "sizeof(target_ulong)" will produce the wrong answer for 32-bit CPUs emulated in
the qemu-system-riscv64 binary. My guess is maybe
it should be looking at the result of riscv_cpu_is_32bit() instead.

Quoted 2 lines

Wow. I read through the code and couldn't find anything that looked like it supported that, sounds like I must have missed something?

I thought Alistair had done that work (which brings riscv into line with the other 32/64 bit QEMU targets, which all support the 32-bit CPU types in the 64-bit-capable executable). But maybe it hasn't landed in master yet?

thanks
-- PMM

#12 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 13 lines · Peter Maydell
Quoted 11 lines · Keith Packard
Quoted 8 lines · Peter Maydell

Part of why I asked is that the current RISCV implementation is just looking at sizeof(target_ulong); but the qemu-system-riscv64 executable AIUI now supports emulating both "this is a 64 bit guest CPU" and "this is a 32 bit host CPU", and so looking at a QEMU-compile-time value like "sizeof(target_ulong)" will produce the wrong answer for 32-bit CPUs emulated in
the qemu-system-riscv64 binary. My guess is maybe
it should be looking at the result of riscv_cpu_is_32bit() instead.

Quoted 2 lines
Quoted 2 lines

Wow. I read through the code and couldn't find anything that looked like it supported that, sounds like I must have missed something?

riscv_cpu_is_32bit() is somewhat new, so it might not have been there when you wrote the patches.

Quoted 5 lines

I thought Alistair had done that work (which brings riscv into line with the other 32/64 bit QEMU targets, which all support the 32-bit CPU types in the 64-bit-capable executable). But maybe it hasn't landed in master yet?

I have started on the effort, but I have not finished yet. Adding riscv_cpu_is_32bit() was the first step there and I have some more patches locally but I don't have anything working yet.

Alistair

Quoted 3 lines

thanks
-- PMM

#13 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

Quoted 3 lines · Alistair Francis

I have started on the effort, but I have not finished yet. Adding riscv_cpu_is_32bit() was the first step there and I have some more patches locally but I don't have anything working yet.

That's awesome. I think waiting until we see what APIs you're developing for detecting and operating in 32-bit mode on a 64-bit capable processor seems like a good idea for now.

Signature · 1 line
-keith
#14 lore raw

Re: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO

In reply to Alex Bennée
Quoted 4 lines · Alex Bennée

I'm not sure this every worked properly and it's certainly not exercised by check-tcg or Peter's semihosting tests. Hoist it into it's own helper function and attempt to validate the results in the linux-user semihosting test at the least.

The patch is mostly code motion, moving the existing heapinfo stuff into a separate function. That makes it really hard to see how you've changed the values being returned. I'd love to see a two patch series, one of which moves the code as-is and a second patch which fixes whatever bugs you've found.

Signature · 1 line
-keith