osdep: Make os-win32.h and os-posix.h handle 'extern "C"' themselves
[qemu/ar7.git] / target / riscv / op_helper.c
blobf0bbd73ca521357607ffef2873902028447f8eec
1 /*
2 * RISC-V Emulation Helpers for QEMU.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017-2018 SiFive, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "qemu/main-loop.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
26 /* Exceptions processing helpers */
27 void QEMU_NORETURN riscv_raise_exception(CPURISCVState *env,
28 uint32_t exception, uintptr_t pc)
30 CPUState *cs = env_cpu(env);
31 cs->exception_index = exception;
32 cpu_loop_exit_restore(cs, pc);
35 void helper_raise_exception(CPURISCVState *env, uint32_t exception)
37 riscv_raise_exception(env, exception, 0);
40 target_ulong helper_csrrw(CPURISCVState *env, target_ulong src,
41 target_ulong csr)
43 target_ulong val = 0;
44 int ret = riscv_csrrw(env, csr, &val, src, -1);
46 if (ret < 0) {
47 riscv_raise_exception(env, -ret, GETPC());
49 return val;
52 target_ulong helper_csrrs(CPURISCVState *env, target_ulong src,
53 target_ulong csr, target_ulong rs1_pass)
55 target_ulong val = 0;
56 int ret = riscv_csrrw(env, csr, &val, -1, rs1_pass ? src : 0);
58 if (ret < 0) {
59 riscv_raise_exception(env, -ret, GETPC());
61 return val;
64 target_ulong helper_csrrc(CPURISCVState *env, target_ulong src,
65 target_ulong csr, target_ulong rs1_pass)
67 target_ulong val = 0;
68 int ret = riscv_csrrw(env, csr, &val, 0, rs1_pass ? src : 0);
70 if (ret < 0) {
71 riscv_raise_exception(env, -ret, GETPC());
73 return val;
76 #ifndef CONFIG_USER_ONLY
78 target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb)
80 uint64_t mstatus;
81 target_ulong prev_priv, prev_virt;
83 if (!(env->priv >= PRV_S)) {
84 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
87 target_ulong retpc = env->sepc;
88 if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
89 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
92 if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) {
93 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
96 if (riscv_has_ext(env, RVH) && riscv_cpu_virt_enabled(env) &&
97 get_field(env->hstatus, HSTATUS_VTSR)) {
98 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
101 mstatus = env->mstatus;
103 if (riscv_has_ext(env, RVH) && !riscv_cpu_virt_enabled(env)) {
104 /* We support Hypervisor extensions and virtulisation is disabled */
105 target_ulong hstatus = env->hstatus;
107 prev_priv = get_field(mstatus, MSTATUS_SPP);
108 prev_virt = get_field(hstatus, HSTATUS_SPV);
110 hstatus = set_field(hstatus, HSTATUS_SPV, 0);
111 mstatus = set_field(mstatus, MSTATUS_SPP, 0);
112 mstatus = set_field(mstatus, SSTATUS_SIE,
113 get_field(mstatus, SSTATUS_SPIE));
114 mstatus = set_field(mstatus, SSTATUS_SPIE, 1);
116 env->mstatus = mstatus;
117 env->hstatus = hstatus;
119 if (prev_virt) {
120 riscv_cpu_swap_hypervisor_regs(env);
123 riscv_cpu_set_virt_enabled(env, prev_virt);
124 } else {
125 prev_priv = get_field(mstatus, MSTATUS_SPP);
127 mstatus = set_field(mstatus, MSTATUS_SIE,
128 get_field(mstatus, MSTATUS_SPIE));
129 mstatus = set_field(mstatus, MSTATUS_SPIE, 1);
130 mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U);
131 env->mstatus = mstatus;
134 riscv_cpu_set_mode(env, prev_priv);
136 return retpc;
139 target_ulong helper_mret(CPURISCVState *env, target_ulong cpu_pc_deb)
141 if (!(env->priv >= PRV_M)) {
142 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
145 target_ulong retpc = env->mepc;
146 if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
147 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
150 uint64_t mstatus = env->mstatus;
151 target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
153 if (!pmp_get_num_rules(env) && (prev_priv != PRV_M)) {
154 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
157 target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV);
158 mstatus = set_field(mstatus, MSTATUS_MIE,
159 get_field(mstatus, MSTATUS_MPIE));
160 mstatus = set_field(mstatus, MSTATUS_MPIE, 1);
161 mstatus = set_field(mstatus, MSTATUS_MPP, PRV_U);
162 mstatus = set_field(mstatus, MSTATUS_MPV, 0);
163 env->mstatus = mstatus;
164 riscv_cpu_set_mode(env, prev_priv);
166 if (riscv_has_ext(env, RVH)) {
167 if (prev_virt) {
168 riscv_cpu_swap_hypervisor_regs(env);
171 riscv_cpu_set_virt_enabled(env, prev_virt);
174 return retpc;
177 void helper_wfi(CPURISCVState *env)
179 CPUState *cs = env_cpu(env);
181 if ((env->priv == PRV_S &&
182 get_field(env->mstatus, MSTATUS_TW)) ||
183 riscv_cpu_virt_enabled(env)) {
184 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
185 } else {
186 cs->halted = 1;
187 cs->exception_index = EXCP_HLT;
188 cpu_loop_exit(cs);
192 void helper_tlb_flush(CPURISCVState *env)
194 CPUState *cs = env_cpu(env);
195 if (!(env->priv >= PRV_S) ||
196 (env->priv == PRV_S &&
197 get_field(env->mstatus, MSTATUS_TVM))) {
198 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
199 } else if (riscv_has_ext(env, RVH) && riscv_cpu_virt_enabled(env) &&
200 get_field(env->hstatus, HSTATUS_VTVM)) {
201 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
202 } else {
203 tlb_flush(cs);
207 void helper_hyp_tlb_flush(CPURISCVState *env)
209 CPUState *cs = env_cpu(env);
211 if (env->priv == PRV_S && riscv_cpu_virt_enabled(env)) {
212 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
215 if (env->priv == PRV_M ||
216 (env->priv == PRV_S && !riscv_cpu_virt_enabled(env))) {
217 tlb_flush(cs);
218 return;
221 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
224 void helper_hyp_gvma_tlb_flush(CPURISCVState *env)
226 if (env->priv == PRV_S && !riscv_cpu_virt_enabled(env) &&
227 get_field(env->mstatus, MSTATUS_TVM)) {
228 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
231 helper_hyp_tlb_flush(env);
234 target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong address)
236 int mmu_idx = cpu_mmu_index(env, true) | TB_FLAGS_PRIV_HYP_ACCESS_MASK;
238 return cpu_lduw_mmuidx_ra(env, address, mmu_idx, GETPC());
241 target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong address)
243 int mmu_idx = cpu_mmu_index(env, true) | TB_FLAGS_PRIV_HYP_ACCESS_MASK;
245 return cpu_ldl_mmuidx_ra(env, address, mmu_idx, GETPC());
248 #endif /* !CONFIG_USER_ONLY */