Skip to contentSkip to conversation

riscv: Add semihosting support [v3]

PATCH In review Keith Packard via 2 participants 3 messages Jan 28, 2020 RISC-V

lore
Share & tools
lore
#0 lore raw

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

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.

---
 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   |   23 +-
 target/riscv/riscv-semi.c                     | 1073 +++++++++++++++++
 target/riscv/translate.c                      |   11 +
 10 files changed, 1124 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 +22 -1
index c5e4b3e49a..ddc26e889f 100644
@@ -29,7 +29,28 @@ 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 +1073 -0

1,073 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 2 lines · Keith Packard

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

Sorry for the noise on the list; I failed to run checkpatch.pl before submitting. I've re-submittd [v4] with the formatting errors corrected.

Signature · 1 line
-keith
#2 lore raw
In reply to Keith Packard via
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [PATCH] riscv: Add semihosting support [v3]

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
a3ca2be riscv: Add semihosting support [v3]

=== OUTPUT BEGIN ===
WARNING: Block comments use a leading /* on a separate line #131: FILE: target/riscv/insn_trans/trans_privileged.inc.c:36:
+ /* The RISC-V semihosting spec specifies the following

WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #153:
new file mode 100644

WARNING: Block comments use a leading /* on a separate line #223: FILE: target/riscv/riscv-semi.c:66:
+/* ADP_Stopped_ApplicationExit is used for exit(0),

WARNING: Block comments use a trailing */ on a separate line #224: FILE: target/riscv/riscv-semi.c:67:
+ * anything else is implemented as exit(1) */

WARNING: Block comments use a leading /* on a separate line #453: FILE: target/riscv/riscv-semi.c:296:
+ /* Return an address in target memory of 64 bytes where the remote

WARNING: Block comments use a leading /* on a separate line #470: FILE: target/riscv/riscv-semi.c:313:
+ /* The size is always stored in big-endian order, extract

WARNING: Block comments use * on subsequent lines
#471: FILE: target/riscv/riscv-semi.c:314:
+ /* The size is always stored in big-endian order, extract
+ the value. We assume the size always fit in 32 bits. */

WARNING: Block comments use a trailing */ on a separate line #471: FILE: target/riscv/riscv-semi.c:314:
+ the value. We assume the size always fit in 32 bits. */

WARNING: Block comments use a leading /* on a separate line #778: FILE: target/riscv/riscv-semi.c:621:
+/* Read the input value from the argument block; fail the semihosting

ERROR: "foo * bar" should be "foo *bar"
#806: FILE: target/riscv/riscv-semi.c:649:
+ char * s;

WARNING: line over 80 characters
#875: FILE: target/riscv/riscv-semi.c:718:
+ ret = riscv_gdb_syscall(cpu, riscv_semi_open_cb, "open,%s,%x,1a4", arg0,

ERROR: spaces required around that '+' (ctx:VxV)
#876: FILE: target/riscv/riscv-semi.c:719:

+                                  (int)arg2+1, gdb_open_modeflags[arg1]);
                                            ^

ERROR: spaces required around that '+' (ctx:VxV)
#973: FILE: target/riscv/riscv-semi.c:816:

+                                  arg0, (int)arg1+1);
                                                  ^

ERROR: spaces required around that '+' (ctx:VxV)
#991: FILE: target/riscv/riscv-semi.c:834:

+                                   arg0, (int)arg1+1, arg2, (int)arg3+1);
                                                   ^

ERROR: spaces required around that '+' (ctx:VxV)
#991: FILE: target/riscv/riscv-semi.c:834:

+                                   arg0, (int)arg1+1, arg2, (int)arg3+1);
                                                                      ^

ERROR: braces {} are necessary for all arms of this statement #1002: FILE: target/riscv/riscv-semi.c:845:
+ if (s2)
[...]

ERROR: braces {} are necessary for all arms of this statement #1004: FILE: target/riscv/riscv-semi.c:847:
+ if (s)
[...]

ERROR: spaces required around that '+' (ctx:VxV)
#1017: FILE: target/riscv/riscv-semi.c:860:

+                                   arg0, (int)arg1+1);
                                                   ^

WARNING: Block comments use a leading /* on a separate line #1032: FILE: target/riscv/riscv-semi.c:875:
+ /* Build a command-line from the original argv.

WARNING: line over 80 characters
#1176: FILE: target/riscv/riscv-semi.c:1019:
+ fail = put_user_ual(retvals[i], arg0 + i * sizeof(target_ulong));

total: 8 errors, 12 warnings, 1197 lines checked

Commit a3ca2be73efc (riscv: Add semihosting support [v3]) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]