riscv: Add semihosting support [v7]
Subsystems (2)
PATCH 1/1 riscv: Add semihosting support [v7]
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
v6:
Add support for semihosting in riscv64-linux-user and riscv32-linux-user
v7:
Add meson build support
--- default-configs/riscv32-softmmu.mak | 1 + default-configs/riscv64-softmmu.mak | 1 + linux-user/meson.build | 1 + linux-user/qemu.h | 4 +- linux-user/riscv/meson.build | 2 + linux-user/riscv/semihost.c | 76 ++ qemu-options.hx | 10 +- target/riscv/cpu.h | 7 + target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 9 + .../riscv/insn_trans/trans_privileged.c.inc | 24 +- target/riscv/meson.build | 1 + target/riscv/riscv-semi.c | 1086 +++++++++++++++++ target/riscv/translate.c | 11 + 14 files changed, 1228 insertions(+), 6 deletions(-) create mode 100644 linux-user/riscv/meson.build create mode 100644 linux-user/riscv/semihost.c create mode 100644 target/riscv/riscv-semi.c
default-configs/riscv32-softmmu.mak +1 -0
@@ -3,6 +3,7 @@ # Uncomment the following lines to disable these optional devices: # #CONFIG_PCI_DEVICES=n +CONFIG_SEMIHOSTING=y # Boards: #
default-configs/riscv64-softmmu.mak +1 -0
@@ -3,6 +3,7 @@ # Uncomment the following lines to disable these optional devices: # #CONFIG_PCI_DEVICES=n +CONFIG_SEMIHOSTING=y # Boards: #
linux-user/meson.build +1 -0
@@ -29,6 +29,7 @@ subdir('microblaze') subdir('mips64') subdir('mips') subdir('ppc') +subdir('riscv') subdir('s390x') subdir('sh4') subdir('sparc64')
linux-user/qemu.h +3 -1
@@ -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 )
@@ -118,7 +120,7 @@ typedef struct TaskState {#ifdef TARGET_M68K abi_ulong tp_value;#endif -#if defined(TARGET_ARM ) || defined(TARGET_M68K ) +#if defined(TARGET_ARM ) || defined(TARGET_M68K ) || defined(TARGET_RISCV )/* Extra fields for semihosted binaries. */ abi_ulong heap_base;abi_ulong heap_limit;
linux-user/riscv/meson.build new file +2 -0
@@ -0,0 +1,2 @@ +linux_user_ss.add(when: 'TARGET_RISCV64', if_true: files('semihost.c')) +linux_user_ss.add(when: 'TARGET_RISCV32', if_true: files('semihost.c'))
linux-user/riscv/semihost.c new file +76 -0
76 lines not on this page · raw message
qemu-options.hx +6 -4
@@ -4149,10 +4149,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, RISC-V only). Note that this allows guest direct access to the host filesystem, so should only be used with a trusted guest OS.
@@ -4164,10 +4164,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
@@ -4182,6 +4182,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/cpu.h +7 -0
@@ -460,4 +460,11 @@ void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);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
@@ -556,6 +556,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_VIRT_INSTRUCTION_FAULT 0x16
target/riscv/cpu_helper.c +9 -0
@@ -856,6 +856,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.c.inc +23 -1
@@ -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/meson.build +1 -0
@@ -22,6 +22,7 @@ riscv_ss.add(files( 'op_helper.c', 'vector_helper.c', 'translate.c', + 'riscv-semi.c' )) riscv_softmmu_ss = ss.source_set()
target/riscv/riscv-semi.c new file +1086 -0
1,086 lines not on this page · raw message
target/riscv/translate.c +11 -0
@@ -63,6 +63,7 @@ typedef struct DisasContext {uint16_t vlen;uint16_t mlen;bool vl_eq_vlmax; +CPUState *cs; } DisasContext;#ifdef TARGET_RISCV64
@@ -746,6 +747,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.c.inc" #include "insn_trans/trans_rvm.c.inc"
@@ -812,6 +822,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->lmul =FIELD_EX32 (tb_flags,TB_FLAGS ,LMUL ); ctx->mlen =1 << (ctx->sew +3 - ctx->lmul); ctx->vl_eq_vlmax =FIELD_EX32 (tb_flags,TB_FLAGS ,VL_EQ_VLMAX ); + ctx->cs = cs; }static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu)
Signature · 1 line
2.28.0
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:
N/A. Internal error while reading log file
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]
PATCH 1/1 riscv: Add semihosting support [v8]
In reply to Keith Packard viaAdapt 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
v6:
Add support for semihosting in riscv64-linux-user and riscv32-linux-user
v7:
Add meson build support
v8:
Fix errors reported by checkpatch that crept in.
--- default-configs/riscv32-softmmu.mak | 1 + default-configs/riscv64-softmmu.mak | 1 + linux-user/meson.build | 1 + linux-user/qemu.h | 4 +- linux-user/riscv/meson.build | 2 + linux-user/riscv/semihost.c | 76 ++ qemu-options.hx | 10 +- target/riscv/cpu.h | 7 + target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 9 + .../riscv/insn_trans/trans_privileged.c.inc | 24 +- target/riscv/meson.build | 1 + target/riscv/riscv-semi.c | 1086 +++++++++++++++++ target/riscv/translate.c | 11 + 14 files changed, 1228 insertions(+), 6 deletions(-) create mode 100644 linux-user/riscv/meson.build create mode 100644 linux-user/riscv/semihost.c create mode 100644 target/riscv/riscv-semi.c
default-configs/riscv32-softmmu.mak +1 -0
@@ -3,6 +3,7 @@ # Uncomment the following lines to disable these optional devices: # #CONFIG_PCI_DEVICES=n +CONFIG_SEMIHOSTING=y # Boards: #
default-configs/riscv64-softmmu.mak +1 -0
@@ -3,6 +3,7 @@ # Uncomment the following lines to disable these optional devices: # #CONFIG_PCI_DEVICES=n +CONFIG_SEMIHOSTING=y # Boards: #
linux-user/meson.build +1 -0
@@ -29,6 +29,7 @@ subdir('microblaze') subdir('mips64') subdir('mips') subdir('ppc') +subdir('riscv') subdir('s390x') subdir('sh4') subdir('sparc64')
linux-user/qemu.h +3 -1
@@ -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 )
@@ -118,7 +120,7 @@ typedef struct TaskState {#ifdef TARGET_M68K abi_ulong tp_value;#endif -#if defined(TARGET_ARM ) || defined(TARGET_M68K ) +#if defined(TARGET_ARM ) || defined(TARGET_M68K ) || defined(TARGET_RISCV )/* Extra fields for semihosted binaries. */ abi_ulong heap_base;abi_ulong heap_limit;
linux-user/riscv/meson.build new file +2 -0
@@ -0,0 +1,2 @@ +linux_user_ss.add(when: 'TARGET_RISCV64', if_true: files('semihost.c')) +linux_user_ss.add(when: 'TARGET_RISCV32', if_true: files('semihost.c'))
linux-user/riscv/semihost.c new file +76 -0
76 lines not on this page · raw message
qemu-options.hx +6 -4
@@ -4149,10 +4149,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, RISC-V only). Note that this allows guest direct access to the host filesystem, so should only be used with a trusted guest OS.
@@ -4164,10 +4164,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
@@ -4182,6 +4182,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/cpu.h +7 -0
@@ -460,4 +460,11 @@ void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);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
@@ -556,6 +556,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_VIRT_INSTRUCTION_FAULT 0x16
target/riscv/cpu_helper.c +9 -0
@@ -856,6 +856,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.c.inc +23 -1
@@ -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/meson.build +1 -0
@@ -22,6 +22,7 @@ riscv_ss.add(files( 'op_helper.c', 'vector_helper.c', 'translate.c', + 'riscv-semi.c' )) riscv_softmmu_ss = ss.source_set()
target/riscv/riscv-semi.c new file +1086 -0
1,086 lines not on this page · raw message
target/riscv/translate.c +11 -0
@@ -63,6 +63,7 @@ typedef struct DisasContext {uint16_t vlen;uint16_t mlen;bool vl_eq_vlmax; +CPUState *cs; } DisasContext;#ifdef TARGET_RISCV64
@@ -746,6 +747,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.c.inc" #include "insn_trans/trans_rvm.c.inc"
@@ -812,6 +822,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->lmul =FIELD_EX32 (tb_flags,TB_FLAGS ,LMUL ); ctx->mlen =1 << (ctx->sew +3 - ctx->lmul); ctx->vl_eq_vlmax =FIELD_EX32 (tb_flags,TB_FLAGS ,VL_EQ_VLMAX ); + ctx->cs = cs; }static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu)
Signature · 1 line
2.28.0
Re: [PATCH] riscv: Add semihosting support [v8]
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:
N/A. Internal error while reading log file
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]