Skip to contentSkip to conversation

riscv: Add semihosting support [v4]

PATCH In review Keith Packard via 4 participants 9 messages Jan 28, 2020 – Apr 6, 2020 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Add semihosting support [v4]

Adapt the arm semihosting support code for RISCV. This implementation is based on the standard for RISC-V semihosting as documented in

https://riscv.org/specifications/

---

v2:
Update PC after exception is handled to follow change in the ARM version for SYS_READC

v3:
Disallow semihosting in user mode; report a regular breakpoint in that case.

v4:
Fix errors reported by checkpatch

---
 default-configs/riscv32-softmmu.mak           |    1 +
 linux-user/qemu.h                             |    2 +
 qemu-options.hx                               |    4 +-
 target/riscv/Makefile.objs                    |    2 +-
 target/riscv/cpu.h                            |    2 +
 target/riscv/cpu_bits.h                       |    1 +
 target/riscv/cpu_helper.c                     |    9 +
 .../riscv/insn_trans/trans_privileged.inc.c   |   24 +-
 target/riscv/riscv-semi.c                     | 1084 +++++++++++++++++
 target/riscv/translate.c                      |   11 +
 10 files changed, 1136 insertions(+), 4 deletions(-)
 create mode 100644 target/riscv/riscv-semi.c
default-configs/riscv32-softmmu.mak +1 -0
index 1ae077ed87..21b7bedf19 100644
@@ -3,6 +3,7 @@
 # Uncomment the following lines to disable these optional devices:
 #
 #CONFIG_PCI_DEVICES=n
+CONFIG_SEMIHOSTING=y
 
 # Boards:
 #
linux-user/qemu.h +2 -0
index 560a68090e..122d95d6b6 100644
@@ -105,6 +105,8 @@ typedef struct TaskState {
     /* FPA state */
     FPA11 fpa;
 # endif
+#endif
+#if defined(TARGET_ARM) || defined(TARGET_RISCV)
     int swi_errno;
 #endif
 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
qemu-options.hx +2 -2
index 224a8e8712..4892e6b12c 100644
@@ -4109,7 +4109,7 @@ ETEXI
 DEF("semihosting", 0, QEMU_OPTION_semihosting,
     "-semihosting    semihosting mode\n",
     QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
-    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2)
+    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
 STEXI
 @item -semihosting
 @findex -semihosting
@@ -4119,7 +4119,7 @@ DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config,
     "-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,arg=str[,...]]\n" \
     "                semihosting configuration\n",
 QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
-QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2)
+QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
 STEXI
 @item -semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,arg=str[,...]]
 @findex -semihosting-config
target/riscv/Makefile.objs +1 -1
index ff651f69f6..6fd7f40e29 100644
@@ -1,4 +1,4 @@
-obj-y += translate.o op_helper.o cpu_helper.o cpu.o csr.o fpu_helper.o gdbstub.o
+obj-y += translate.o op_helper.o cpu_helper.o cpu.o csr.o fpu_helper.o gdbstub.o riscv-semi.o
 obj-$(CONFIG_SOFTMMU) += pmp.o
 
 ifeq ($(CONFIG_SOFTMMU),y)
target/riscv/cpu.h +2 -0
index de0a8d893a..310ec6ab40 100644
@@ -338,6 +338,8 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
 typedef CPURISCVState CPUArchState;
 typedef RISCVCPU ArchCPU;
 
+target_ulong do_riscv_semihosting(CPURISCVState *env);
+
 #include "exec/cpu-all.h"
 
 #endif /* RISCV_CPU_H */
target/riscv/cpu_bits.h +1 -0
index e99834856c..c45745d73b 100644
@@ -496,6 +496,7 @@
 #define RISCV_EXCP_INST_PAGE_FAULT         0xc /* since: priv-1.10.0 */
 #define RISCV_EXCP_LOAD_PAGE_FAULT         0xd /* since: priv-1.10.0 */
 #define RISCV_EXCP_STORE_PAGE_FAULT        0xf /* since: priv-1.10.0 */
+#define RISCV_EXCP_SEMIHOST                0x10
 
 #define RISCV_EXCP_INT_FLAG                0x80000000
 #define RISCV_EXCP_INT_MASK                0x7fffffff
target/riscv/cpu_helper.c +9 -0
index 85403da9c8..6a04469936 100644
@@ -532,6 +532,15 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         [PRV_M] = RISCV_EXCP_M_ECALL
     };
 
+    if  (cause == RISCV_EXCP_SEMIHOST) {
+        if (env->priv >= PRV_S) {
+            env->gpr[xA0] = do_riscv_semihosting(env);
+            env->pc += 4;
+            return;
+        }
+        cause = RISCV_EXCP_BREAKPOINT;
+    }
+
     if (!async) {
         /* set tval to badaddr for traps with address information */
         switch (cause) {
target/riscv/insn_trans/trans_privileged.inc.c +23 -1
index c5e4b3e49a..decd59bbf2 100644
@@ -29,7 +29,29 @@ static bool trans_ecall(DisasContext *ctx, arg_ecall *a)
 
 static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
 {
-    generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
+    uint32_t pre    = opcode_at(&ctx->base, ctx->base.pc_next - 4);
+    uint32_t ebreak = opcode_at(&ctx->base, ctx->base.pc_next);
+    uint32_t post   = opcode_at(&ctx->base, ctx->base.pc_next + 4);
+
+    /*
+     * The RISC-V semihosting spec specifies the following
+     * three-instruction sequence to flag a semihosting call:
+     *
+     *      slli zero, zero, 0x1f       0x01f01013
+     *      ebreak                      0x00100073
+     *      srai zero, zero, 0x7        0x40705013
+     *
+     * The two shift operations on the zero register are no-ops, used
+     * here to signify a semihosting exception, rather than a breakpoint.
+     *
+     * Uncompressed instructions are used so that the sequence is easy
+     * to validate.
+     */
+    if  (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
+        generate_exception(ctx, RISCV_EXCP_SEMIHOST);
+    } else {
+        generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
+    }
     exit_tb(ctx); /* no chaining */
     ctx->base.is_jmp = DISAS_NORETURN;
     return true;
target/riscv/riscv-semi.c new file +1084 -0

1,084 lines not on this page · raw message

target/riscv/translate.c +11 -0
index 14dc71156b..4ee0d75f86 100644
@@ -55,6 +55,7 @@ typedef struct DisasContext {
        to reset this known value.  */
     int frm;
     bool ext_ifencei;
+    CPUState *cs;
 } DisasContext;
 
 #ifdef TARGET_RISCV64
@@ -698,6 +699,15 @@ static bool gen_shift(DisasContext *ctx, arg_r *a,
     return true;
 }
 
+static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
+{
+    DisasContext *ctx = container_of(dcbase, DisasContext, base);
+    CPUState *cpu = ctx->cs;
+    CPURISCVState *env = cpu->env_ptr;
+
+    return cpu_ldl_code(env, pc);
+}
+
 /* Include insn module translation function */
 #include "insn_trans/trans_rvi.inc.c"
 #include "insn_trans/trans_rvm.inc.c"
@@ -743,6 +753,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
     ctx->misa = env->misa;
     ctx->frm = -1;  /* unknown rounding mode */
     ctx->ext_ifencei = cpu->cfg.ext_ifencei;
+    ctx->cs = cs;
 }
 
 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
Signature · 1 line
2.25.0
#1 lore raw
Quoted 6 lines · Keith Packard via

Adapt the arm semihosting support code for RISCV. This implementation is based on the standard for RISC-V semihosting as documented in

https://riscv.org/specifications/

Quoted 7 lines

---

+ * ARM Semihosting is documented in:
+ * Semihosting for AArch32 and AArch64 Release 2.0
+ * https://static.docs.arm.com/100863/0200/semihosting.pdf

True but irrelevant. You need to refer to a proper risc-v specification for your semihosting.

target/riscv/riscv-semi.c line 1049

Quoted 10 lines
target/riscv/riscv-semi.c new file +10 -0
+    case TARGET_SYS_EXIT:
+    case TARGET_SYS_EXIT_EXTENDED:
+        if (nr == TARGET_SYS_EXIT_EXTENDED || sizeof(target_ulong) == 0) {
+            /*
+             * The A64 version of SYS_EXIT takes a parameter block,
+             * so the application-exit type can return a subcode which
+             * is the exit status code from the application.
+             * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
+             * which allows A32/T32 guests to also provide a status code.
+             */

This code and comment describe Arm semihosting, where we made this bad decision about the API for 32-bit Arm, fixed it for 64-bit Arm and then put in an extension to add the more sensible API to 32-bit as a backwards-compatible new function. Nobody else should make this API choice from the start. What does RISC-V want to do here? This should be in your specification.

Quoted 19 lines
target/riscv/riscv-semi.c new file +19 -0
+            GET_ARG(0);
+            GET_ARG(1);
+
+            if (arg0 == ADP_Stopped_ApplicationExit) {
+                ret = arg1;
+            } else {
+                ret = 1;
+            }
+        } else {
+            /*
+             * The A32/T32 version of SYS_EXIT specifies only
+             * Stopped_ApplicationExit as normal exit, but does not
+             * allow the guest to specify the exit status code.
+             * Everything else is considered an error.
+             */
+            ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
+        }
+        gdb_exit(env, ret);
+        exit(ret);

target/riscv/riscv-semi.c line 1078

Quoted 10 lines
target/riscv/riscv-semi.c new file +10 -0
+    case TARGET_SYS_SYNCCACHE:
+        /*
+         * Clean the D-cache and invalidate the I-cache for the specified
+         * virtual address range. This is a nop for us since we don't
+         * implement caches. This is only present on A64.
+         */
+        if (sizeof(target_ulong) == 8) {
+            return 0;
+        }
+        /* fall through -- invalid for A32/T32 */

Again, this is an Arm-ism, where the old A32 ABI doesn't have this function but the new A64 one does. What does RISC-V want to specify here?

target/riscv/riscv-semi.c line 1082

Quoted 4 lines
target/riscv/riscv-semi.c new file +4 -0
+    default:
+        fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
+        cpu_dump_state(cs, stderr, 0);
+        abort();

This is repeating the Arm ABI mistake of allowing implementations to just crash and burn if they're handed a function call they don't recognize. Ideally it should be avoided in a new ABI.

Review

Quoted 2 lines
(quoted diff) +2 -0
+    }
+}

thanks
-- PMM

#2 lore raw
Quoted 2 lines · Peter Maydell

True but irrelevant. You need to refer to a proper risc-v specification for your semihosting.

The RISC-V Foundation defined semihosting as relative to the existing ARM specification, so using a link to that is appropriate here.

Here's the current specification of the unprivileged ISA, which includes the definition of semihosting

https://riscv.org/specifications/

While it may be nice in some abstract sense to create a "better" semihosting spec, that's not what the RISC-V foundation has decided to do.

Signature · 1 line
-keith
#3 lore raw

The text you are referencing (the couple italic paragraphs below section 2.8 in the unprivileged ISA) is non-normative and "can be skipped if the reader is only interested in the specification itself". This convention of making indented italic text non-normative is described at the bottom of page 1 of the linked document.

Jonathan

Quoted 20 lines · Keith Packard via
Quoted 2 lines · Peter Maydell

True but irrelevant. You need to refer to a proper risc-v specification for your semihosting.

The RISC-V Foundation defined semihosting as relative to the existing ARM specification, so using a link to that is appropriate here.

Here's the current specification of the unprivileged ISA, which includes the definition of semihosting

https://riscv.org/specifications/

While it may be nice in some abstract sense to create a "better" semihosting spec, that's not what the RISC-V foundation has decided to do.

--
-keith

#4 lore raw
In reply to Keith Packard via
Quoted 9 lines · Keith Packard
Quoted 2 lines · Peter Maydell

True but irrelevant. You need to refer to a proper risc-v specification for your semihosting.

The RISC-V Foundation defined semihosting as relative to the existing ARM specification, so using a link to that is appropriate here.

Quoted 8 lines

Here's the current specification of the unprivileged ISA, which includes the definition of semihosting

https://riscv.org/specifications/

While it may be nice in some abstract sense to create a "better" semihosting spec, that's not what the RISC-V foundation has decided to do.

We've gone round this several times. You can't just say "it's the arm spec", because you're not arm and your architecture is different. You need to actually document what the architecture-specific parts are, even if you want to mostly say "and we follow the Arm spec most of the time". If you really really want to say "32 bit RISC-V is gratuitously different from 64-bit RISC-V in these areas because we just blindly copied the way Arm happened to have historically developed" then you can do that if you like, but you need to actually write it down in a document somewhere.

You're trying to implement an ABI which has multiple different implementations of both consumers and producers; it's not a good idea to shortcut the process of actually writing down what the requirements between the two ends are.

thanks
-- PMM

#5 lore raw
Quoted 31 lines · Peter Maydell
Quoted 9 lines · Keith Packard
Quoted 2 lines · Peter Maydell

True but irrelevant. You need to refer to a proper risc-v specification for your semihosting.

The RISC-V Foundation defined semihosting as relative to the existing ARM specification, so using a link to that is appropriate here.

Quoted 8 lines

Here's the current specification of the unprivileged ISA, which includes the definition of semihosting

https://riscv.org/specifications/

While it may be nice in some abstract sense to create a "better" semihosting spec, that's not what the RISC-V foundation has decided to do.

We've gone round this several times. You can't just say "it's the arm spec", because you're not arm and your architecture is different. You need to actually document what the architecture-specific parts are, even if you want to mostly say "and we follow the Arm spec most of the time". If you really really want to say "32 bit RISC-V is gratuitously different from 64-bit RISC-V in these areas because we just blindly copied the way Arm happened to have historically developed" then you can do that if you like, but you need to actually write it down in a document somewhere.

Quoted 5 lines

You're trying to implement an ABI which has multiple different implementations of both consumers and producers; it's not a good idea to shortcut the process of actually writing down what the requirements between the two ends are.

I think, more concretely, the issues here are:

* The only mention of "semihosting" in the RISC-V specification is in the commentary, which is not considered a normative part of the specification.
* The interface that commentary contains is defined as maybe being obsolete already, but no new concrete interface is defined.
* The semihosting comment doesn't define the semihosting call numbers, just the sequence to get to a call. That said, we haven't written down the Linux ABI either -- though there's a much larger breadth of software out there that implements it and won't break ABI compatibility, so maybe that's considered sufficient in Linux land where it's not for semihosting.

It seems to me like the right way to go forward is to put together a semihosting ABI specification as a RISC-V specification. That would give everyone who cares about their pre-specification implementations a chance to get together to figure out which parts they want to keep, and give us a path to move forwards. There's more work that needs to be done here to put together a system that will actually work (for example, how to we insert breakpoints if we're using ebreak to indicate semihosting and haven't reserved NOP sequences?), so this does warrant a bit of thought.

I know it's a bit of a headache in the short term to get a specification together, but we've had so many headaches of the "there's no spec" sort that at least it'll be a change of pace :)

Quoted 3 lines

thanks
-- PMM

#6 lore raw
Quoted 5 lines · Palmer Dabbelt

* The semihosting comment doesn't define the semihosting call numbers, just the sequence to get to a call. That said, we haven't written down the Linux ABI either -- though there's a much larger breadth of software out there that implements it and won't break ABI compatibility, so maybe that's considered sufficient in Linux land where it's not for semihosting.

I think the difference with Linux is that there's a clear single authoritative source for what the ABI is -- Linus's 'mainline' kernel (so for instance random out-of-tree forks, syscall patch proposals, etc that have not yet hit mainline don't count as being fixed-in-stone ABI). Semihosting doesn't have the same single clear "owning project", at least on the Arm side of things.

I don't think a semihosting specification for RISC-V needs necessarily to be a very heavyweight thing -- it is, after all, a debug API at heart -- but I do agree that you should have one.

thanks
-- PMM

#7 lore raw

Re: [PATCH] riscv: Add semihosting support [v5]

Quoted 4 lines · Peter Maydell

I don't think a semihosting specification for RISC-V needs necessarily to be a very heavyweight thing -- it is, after all, a debug API at heart -- but I do agree that you should have one.

We've had a couple of weeks of discussion in the RISC-V sw-dev group and have created a new specification for RISC-V semihosting. It is located within the RISC-V consortium github account here:

https://github.com/riscv/riscv-semihosting-spec

The current version is 0.2, and this patch to QEMU implements that version.

From 404ff408fa00ab7f65d1842966854824c418020c Mon Sep 17 00:00:00 2001
From: Keith Packard <[email protected]>
Date: Mon, 21 Oct 2019 01:12:13 -0700
Subject: [PATCH] riscv: Add semihosting support [v5]

Adapt the arm semihosting support code for RISCV. This implementation is based on the standard for RISC-V semihosting version 0.2 as documented in

https://github.com/riscv/riscv-semihosting-spec/releases/tag/0.2

---

v2:
Update PC after exception is handled to follow change in the ARM version for SYS_READC

v3:
Disallow semihosting in user mode; report a regular breakpoint in that case.

v4:
Fix errors reported by checkpatch

v5:
Reference current RISC-V semihosting specification

---
 default-configs/riscv32-softmmu.mak           |    1 +
 linux-user/qemu.h                             |    2 +
 qemu-options.hx                               |   10 +-
 target/riscv/Makefile.objs                    |    2 +-
 target/riscv/cpu.h                            |    2 +
 target/riscv/cpu_bits.h                       |    1 +
 target/riscv/cpu_helper.c                     |    9 +
 .../riscv/insn_trans/trans_privileged.inc.c   |   24 +-
 target/riscv/riscv-semi.c                     | 1084 +++++++++++++++++
 target/riscv/translate.c                      |   11 +
 10 files changed, 1140 insertions(+), 6 deletions(-)
 create mode 100644 target/riscv/riscv-semi.c
default-configs/riscv32-softmmu.mak +1 -0
index 1ae077ed87..21b7bedf19 100644
@@ -3,6 +3,7 @@
 # Uncomment the following lines to disable these optional devices:
 #
 #CONFIG_PCI_DEVICES=n
+CONFIG_SEMIHOSTING=y
 
 # Boards:
 #
linux-user/qemu.h +2 -0
index 792c74290f..fb59776a34 100644
@@ -105,6 +105,8 @@ typedef struct TaskState {
     /* FPA state */
     FPA11 fpa;
 # endif
+#endif
+#if defined(TARGET_ARM) || defined(TARGET_RISCV)
     int swi_errno;
 #endif
 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
qemu-options.hx +6 -4
index 16debd03cb..917b32ef32 100644
@@ -4103,10 +4103,10 @@ ERST
 DEF("semihosting", 0, QEMU_OPTION_semihosting,
     "-semihosting    semihosting mode\n",
     QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
-    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2)
+    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
 SRST
 ``-semihosting``
-    Enable semihosting mode (ARM, M68K, Xtensa, MIPS, Nios II only).
+    Enable semihosting mode (ARM, M68K, Xtensa, MIPS, Nios II only, RISC-V).
 
     Note that this allows guest direct access to the host filesystem, so
     should only be used with a trusted guest OS.
@@ -4118,10 +4118,10 @@ DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config,
     "-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,arg=str[,...]]\n" \
     "                semihosting configuration\n",
 QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
-QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2)
+QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
 SRST
 ``-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,arg=str[,...]]``
-    Enable and configure semihosting (ARM, M68K, Xtensa, MIPS, Nios II
+    Enable and configure semihosting (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V
     only).
 
     Note that this allows guest direct access to the host filesystem, so
@@ -4136,6 +4136,8 @@ SRST
     open/read/write/seek/select. Tensilica baremetal libc for ISS and
     linux platform "sim" use this interface.
 
+    On RISC-V this implements the standard semihosting API, version 0.2.
+
     ``target=native|gdb|auto``
         Defines where the semihosting calls will be addressed, to QEMU
         (``native``) or to GDB (``gdb``). The default is ``auto``, which
target/riscv/Makefile.objs +1 -1
index ff651f69f6..6fd7f40e29 100644
@@ -1,4 +1,4 @@
-obj-y += translate.o op_helper.o cpu_helper.o cpu.o csr.o fpu_helper.o gdbstub.o
+obj-y += translate.o op_helper.o cpu_helper.o cpu.o csr.o fpu_helper.o gdbstub.o riscv-semi.o
 obj-$(CONFIG_SOFTMMU) += pmp.o
 
 ifeq ($(CONFIG_SOFTMMU),y)
target/riscv/cpu.h +2 -0
index 7d21addbab..f605f6b70a 100644
@@ -397,6 +397,8 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
 typedef CPURISCVState CPUArchState;
 typedef RISCVCPU ArchCPU;
 
+target_ulong do_riscv_semihosting(CPURISCVState *env);
+
 #include "exec/cpu-all.h"
 
 #endif /* RISCV_CPU_H */
target/riscv/cpu_bits.h +1 -0
index 7f64ee1174..b100f79044 100644
@@ -527,6 +527,7 @@
 #define RISCV_EXCP_INST_PAGE_FAULT               0xc /* since: priv-1.10.0 */
 #define RISCV_EXCP_LOAD_PAGE_FAULT               0xd /* since: priv-1.10.0 */
 #define RISCV_EXCP_STORE_PAGE_FAULT              0xf /* since: priv-1.10.0 */
+#define RISCV_EXCP_SEMIHOST                      0x10
 #define RISCV_EXCP_INST_GUEST_PAGE_FAULT         0x14
 #define RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT       0x15
 #define RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT  0x17
target/riscv/cpu_helper.c +9 -0
index d3ba9efb02..96776a97dc 100644
@@ -858,6 +858,15 @@ void riscv_cpu_do_interrupt(CPUState *cs)
     target_ulong htval = 0;
     target_ulong mtval2 = 0;
 
+    if  (cause == RISCV_EXCP_SEMIHOST) {
+        if (env->priv >= PRV_S) {
+            env->gpr[xA0] = do_riscv_semihosting(env);
+            env->pc += 4;
+            return;
+        }
+        cause = RISCV_EXCP_BREAKPOINT;
+    }
+
     if (!async) {
         /* set tval to badaddr for traps with address information */
         switch (cause) {
target/riscv/insn_trans/trans_privileged.inc.c +23 -1
index 76c2fad71c..57f393847f 100644
@@ -29,7 +29,29 @@ static bool trans_ecall(DisasContext *ctx, arg_ecall *a)
 
 static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
 {
-    generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
+    uint32_t pre    = opcode_at(&ctx->base, ctx->base.pc_next - 4);
+    uint32_t ebreak = opcode_at(&ctx->base, ctx->base.pc_next);
+    uint32_t post   = opcode_at(&ctx->base, ctx->base.pc_next + 4);
+
+    /*
+     * The RISC-V semihosting spec specifies the following
+     * three-instruction sequence to flag a semihosting call:
+     *
+     *      slli zero, zero, 0x1f       0x01f01013
+     *      ebreak                      0x00100073
+     *      srai zero, zero, 0x7        0x40705013
+     *
+     * The two shift operations on the zero register are no-ops, used
+     * here to signify a semihosting exception, rather than a breakpoint.
+     *
+     * Uncompressed instructions are used so that the sequence is easy
+     * to validate.
+     */
+    if  (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
+        generate_exception(ctx, RISCV_EXCP_SEMIHOST);
+    } else {
+        generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
+    }
     exit_tb(ctx); /* no chaining */
     ctx->base.is_jmp = DISAS_NORETURN;
     return true;
target/riscv/riscv-semi.c new file +1084 -0

1,084 lines not on this page · raw message

target/riscv/translate.c +11 -0
index 43bf7e39a6..eb65ace367 100644
@@ -56,6 +56,7 @@ typedef struct DisasContext {
        to reset this known value.  */
     int frm;
     bool ext_ifencei;
+    CPUState *cs;
 } DisasContext;
 
 #ifdef TARGET_RISCV64
@@ -705,6 +706,15 @@ static bool gen_shift(DisasContext *ctx, arg_r *a,
     return true;
 }
 
+static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
+{
+    DisasContext *ctx = container_of(dcbase, DisasContext, base);
+    CPUState *cpu = ctx->cs;
+    CPURISCVState *env = cpu->env_ptr;
+
+    return cpu_ldl_code(env, pc);
+}
+
 /* Include insn module translation function */
 #include "insn_trans/trans_rvi.inc.c"
 #include "insn_trans/trans_rvm.inc.c"
@@ -772,6 +782,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
     ctx->misa = env->misa;
     ctx->frm = -1;  /* unknown rounding mode */
     ctx->ext_ifencei = cpu->cfg.ext_ifencei;
+    ctx->cs = cs;
 }
 
 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
Signature · 1 line
2.25.1
Signature · 2 lines
-- 
-keith
#8 lore raw
In reply to Palmer Dabbelt
Quoted 5 lines · Palmer Dabbelt

I think, more concretely, the issues here are:

* The only mention of "semihosting" in the RISC-V specification is in the commentary, which is not considered a normative part of the
specification.

Ok, we're starting the process of creating a 'real' RISC-V semihosting spec. This references the existing 2.0 version of the ARM semihosting specification, defining only the RISC-V specific details necessary. I've created a public github repository containing this work; that is likely to eventually move over to the RISC-V github repository.

https://github.com/keith-packard/riscv-semihosting-spec/

The goal is to enable RISC-V semihosting implementations that share most of the underlying details with existing ARM semihosting implemenations.

Signature · 1 line
-keith