2 * RISC-V translation routines for the RISC-V privileged instructions.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2018 Peer Adelt, peer.adelt@hni.uni-paderborn.de
6 * Bastian Koppelmann, kbastian@mail.uni-paderborn.de
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2 or later, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 static bool trans_ecall(DisasContext *ctx, arg_ecall *a)
23 /* always generates U-level ECALL, fixed in do_interrupt handler */
24 generate_exception(ctx, RISCV_EXCP_U_ECALL);
28 static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a)
30 target_ulong ebreak_addr = ctx->base.pc_next;
31 target_ulong pre_addr = ebreak_addr - 4;
32 target_ulong post_addr = ebreak_addr + 4;
38 * The RISC-V semihosting spec specifies the following
39 * three-instruction sequence to flag a semihosting call:
41 * slli zero, zero, 0x1f 0x01f01013
43 * srai zero, zero, 0x7 0x40705013
45 * The two shift operations on the zero register are no-ops, used
46 * here to signify a semihosting exception, rather than a breakpoint.
48 * Uncompressed instructions are required so that the sequence is easy
51 * The three instructions are required to lie in the same page so
52 * that no exception will be raised when fetching them.
55 if (semihosting_enabled(ctx->priv == PRV_U) &&
56 (pre_addr & TARGET_PAGE_MASK) == (post_addr & TARGET_PAGE_MASK)) {
57 pre = opcode_at(&ctx->base, pre_addr);
58 ebreak = opcode_at(&ctx->base, ebreak_addr);
59 post = opcode_at(&ctx->base, post_addr);
62 if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) {
63 generate_exception(ctx, RISCV_EXCP_SEMIHOST);
65 generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
70 static bool trans_uret(DisasContext *ctx, arg_uret *a)
75 static bool trans_sret(DisasContext *ctx, arg_sret *a)
77 #ifndef CONFIG_USER_ONLY
78 if (has_ext(ctx, RVS)) {
80 translator_io_start(&ctx->base);
81 gen_helper_sret(cpu_pc, cpu_env);
82 exit_tb(ctx); /* no chaining */
83 ctx->base.is_jmp = DISAS_NORETURN;
93 static bool trans_mret(DisasContext *ctx, arg_mret *a)
95 #ifndef CONFIG_USER_ONLY
97 translator_io_start(&ctx->base);
98 gen_helper_mret(cpu_pc, cpu_env);
99 exit_tb(ctx); /* no chaining */
100 ctx->base.is_jmp = DISAS_NORETURN;
107 static bool trans_wfi(DisasContext *ctx, arg_wfi *a)
109 #ifndef CONFIG_USER_ONLY
110 decode_save_opc(ctx);
111 gen_update_pc(ctx, ctx->cur_insn_len);
112 gen_helper_wfi(cpu_env);
119 static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a)
121 #ifndef CONFIG_USER_ONLY
122 decode_save_opc(ctx);
123 gen_helper_tlb_flush(cpu_env);
129 static bool trans_sfence_vm(DisasContext *ctx, arg_sfence_vm *a)