1 /*
2 * RISC-V Emulation Helpers for QEMU.
3 *
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017-2018 SiFive, Inc.
6 * Copyright (c) 2022 VRULL GmbH
7 *
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.
11 *
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
15 * more details.
16 *
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/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "internals.h"
24 #include "exec/cputlb.h"
25 #include "accel/tcg/cpu-ldst.h"
26 #include "accel/tcg/probe.h"
27 #include "exec/helper-proto.h"
28 #include "exec/tlb-flags.h"
29 #include "trace.h"
30
31 /* Exceptions processing helpers */
riscv_raise_exception(CPURISCVState * env,RISCVException exception,uintptr_t pc)32 G_NORETURN void riscv_raise_exception(CPURISCVState *env,
33 RISCVException exception,
34 uintptr_t pc)
35 {
36 CPUState *cs = env_cpu(env);
37
38 trace_riscv_exception(exception,
39 riscv_cpu_get_trap_name(exception, false),
40 env->pc);
41
42 cs->exception_index = exception;
43 cpu_loop_exit_restore(cs, pc);
44 }
45
helper_raise_exception(CPURISCVState * env,uint32_t exception)46 void helper_raise_exception(CPURISCVState *env, uint32_t exception)
47 {
48 riscv_raise_exception(env, exception, 0);
49 }
50
helper_csrr(CPURISCVState * env,int csr)51 target_ulong helper_csrr(CPURISCVState *env, int csr)
52 {
53 /*
54 * The seed CSR must be accessed with a read-write instruction. A
55 * read-only instruction such as CSRRS/CSRRC with rs1=x0 or CSRRSI/
56 * CSRRCI with uimm=0 will raise an illegal instruction exception.
57 */
58 if (csr == CSR_SEED) {
59 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
60 }
61
62 target_ulong val = 0;
63 RISCVException ret = riscv_csrr(env, csr, &val);
64
65 if (ret != RISCV_EXCP_NONE) {
66 riscv_raise_exception(env, ret, GETPC());
67 }
68 return val;
69 }
70
helper_csrw(CPURISCVState * env,int csr,target_ulong src)71 void helper_csrw(CPURISCVState *env, int csr, target_ulong src)
72 {
73 target_ulong mask = env->xl == MXL_RV32 ? UINT32_MAX : (target_ulong)-1;
74 RISCVException ret = riscv_csrrw(env, csr, NULL, src, mask, GETPC());
75
76 if (ret != RISCV_EXCP_NONE) {
77 riscv_raise_exception(env, ret, GETPC());
78 }
79 }
80
helper_csrrw(CPURISCVState * env,int csr,target_ulong src,target_ulong write_mask)81 target_ulong helper_csrrw(CPURISCVState *env, int csr,
82 target_ulong src, target_ulong write_mask)
83 {
84 target_ulong val = 0;
85 RISCVException ret = riscv_csrrw(env, csr, &val, src, write_mask, GETPC());
86
87 if (ret != RISCV_EXCP_NONE) {
88 riscv_raise_exception(env, ret, GETPC());
89 }
90 return val;
91 }
92
helper_csrr_i128(CPURISCVState * env,int csr)93 target_ulong helper_csrr_i128(CPURISCVState *env, int csr)
94 {
95 Int128 rv = int128_zero();
96 RISCVException ret = riscv_csrr_i128(env, csr, &rv);
97
98 if (ret != RISCV_EXCP_NONE) {
99 riscv_raise_exception(env, ret, GETPC());
100 }
101
102 env->retxh = int128_gethi(rv);
103 return int128_getlo(rv);
104 }
105
helper_csrw_i128(CPURISCVState * env,int csr,target_ulong srcl,target_ulong srch)106 void helper_csrw_i128(CPURISCVState *env, int csr,
107 target_ulong srcl, target_ulong srch)
108 {
109 RISCVException ret = riscv_csrrw_i128(env, csr, NULL,
110 int128_make128(srcl, srch),
111 UINT128_MAX, GETPC());
112
113 if (ret != RISCV_EXCP_NONE) {
114 riscv_raise_exception(env, ret, GETPC());
115 }
116 }
117
helper_csrrw_i128(CPURISCVState * env,int csr,target_ulong srcl,target_ulong srch,target_ulong maskl,target_ulong maskh)118 target_ulong helper_csrrw_i128(CPURISCVState *env, int csr,
119 target_ulong srcl, target_ulong srch,
120 target_ulong maskl, target_ulong maskh)
121 {
122 Int128 rv = int128_zero();
123 RISCVException ret = riscv_csrrw_i128(env, csr, &rv,
124 int128_make128(srcl, srch),
125 int128_make128(maskl, maskh),
126 GETPC());
127
128 if (ret != RISCV_EXCP_NONE) {
129 riscv_raise_exception(env, ret, GETPC());
130 }
131
132 env->retxh = int128_gethi(rv);
133 return int128_getlo(rv);
134 }
135
136
137 /*
138 * check_zicbo_envcfg
139 *
140 * Raise virtual exceptions and illegal instruction exceptions for
141 * Zicbo[mz] instructions based on the settings of [mhs]envcfg as
142 * specified in section 2.5.1 of the CMO specification.
143 */
check_zicbo_envcfg(CPURISCVState * env,target_ulong envbits,uintptr_t ra)144 static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits,
145 uintptr_t ra)
146 {
147 #ifndef CONFIG_USER_ONLY
148 if ((env->priv < PRV_M) && !get_field(env->menvcfg, envbits)) {
149 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
150 }
151
152 if (env->virt_enabled &&
153 (((env->priv <= PRV_S) && !get_field(env->henvcfg, envbits)) ||
154 ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)))) {
155 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra);
156 }
157
158 if ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)) {
159 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
160 }
161 #endif
162 }
163
helper_cbo_zero(CPURISCVState * env,target_ulong address)164 void helper_cbo_zero(CPURISCVState *env, target_ulong address)
165 {
166 RISCVCPU *cpu = env_archcpu(env);
167 uint16_t cbozlen = cpu->cfg.cboz_blocksize;
168 int mmu_idx = riscv_env_mmu_index(env, false);
169 uintptr_t ra = GETPC();
170 void *mem;
171
172 check_zicbo_envcfg(env, MENVCFG_CBZE, ra);
173
174 /* Mask off low-bits to align-down to the cache-block. */
175 address &= ~(cbozlen - 1);
176
177 /*
178 * cbo.zero requires MMU_DATA_STORE access. Do a probe_write()
179 * to raise any exceptions, including PMP.
180 */
181 mem = probe_write(env, address, cbozlen, mmu_idx, ra);
182
183 if (likely(mem)) {
184 memset(mem, 0, cbozlen);
185 } else {
186 /*
187 * This means that we're dealing with an I/O page. Section 4.2
188 * of cmobase v1.0.1 says:
189 *
190 * "Cache-block zero instructions store zeros independently
191 * of whether data from the underlying memory locations are
192 * cacheable."
193 *
194 * Write zeros in address + cbozlen regardless of not being
195 * a RAM page.
196 */
197 for (int i = 0; i < cbozlen; i++) {
198 cpu_stb_mmuidx_ra(env, address + i, 0, mmu_idx, ra);
199 }
200 }
201 }
202
203 /*
204 * check_zicbom_access
205 *
206 * Check access permissions (LOAD, STORE or FETCH as specified in
207 * section 2.5.2 of the CMO specification) for Zicbom, raising
208 * either store page-fault (non-virtualized) or store guest-page
209 * fault (virtualized).
210 */
check_zicbom_access(CPURISCVState * env,target_ulong address,uintptr_t ra)211 static void check_zicbom_access(CPURISCVState *env,
212 target_ulong address,
213 uintptr_t ra)
214 {
215 RISCVCPU *cpu = env_archcpu(env);
216 int mmu_idx = riscv_env_mmu_index(env, false);
217 uint16_t cbomlen = cpu->cfg.cbom_blocksize;
218 void *phost;
219 int ret;
220
221 /* Mask off low-bits to align-down to the cache-block. */
222 address &= ~(cbomlen - 1);
223
224 /*
225 * Section 2.5.2 of cmobase v1.0.1:
226 *
227 * "A cache-block management instruction is permitted to
228 * access the specified cache block whenever a load instruction
229 * or store instruction is permitted to access the corresponding
230 * physical addresses. If neither a load instruction nor store
231 * instruction is permitted to access the physical addresses,
232 * but an instruction fetch is permitted to access the physical
233 * addresses, whether a cache-block management instruction is
234 * permitted to access the cache block is UNSPECIFIED."
235 */
236 ret = probe_access_flags(env, address, cbomlen, MMU_DATA_LOAD,
237 mmu_idx, true, &phost, ra);
238 if (ret != TLB_INVALID_MASK) {
239 /* Success: readable */
240 return;
241 }
242
243 /*
244 * Since not readable, must be writable. On failure, store
245 * fault/store guest amo fault will be raised by
246 * riscv_cpu_tlb_fill(). PMP exceptions will be caught
247 * there as well.
248 */
249 probe_write(env, address, cbomlen, mmu_idx, ra);
250 }
251
helper_cbo_clean_flush(CPURISCVState * env,target_ulong address)252 void helper_cbo_clean_flush(CPURISCVState *env, target_ulong address)
253 {
254 uintptr_t ra = GETPC();
255 check_zicbo_envcfg(env, MENVCFG_CBCFE, ra);
256 check_zicbom_access(env, address, ra);
257
258 /* We don't emulate the cache-hierarchy, so we're done. */
259 }
260
helper_cbo_inval(CPURISCVState * env,target_ulong address)261 void helper_cbo_inval(CPURISCVState *env, target_ulong address)
262 {
263 uintptr_t ra = GETPC();
264 check_zicbo_envcfg(env, MENVCFG_CBIE, ra);
265 check_zicbom_access(env, address, ra);
266
267 /* We don't emulate the cache-hierarchy, so we're done. */
268 }
269
270 #ifndef CONFIG_USER_ONLY
271
helper_sret(CPURISCVState * env)272 target_ulong helper_sret(CPURISCVState *env)
273 {
274 uint64_t mstatus;
275 target_ulong prev_priv, prev_virt = env->virt_enabled;
276 const target_ulong src_priv = env->priv;
277 const bool src_virt = env->virt_enabled;
278
279 if (!(env->priv >= PRV_S)) {
280 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
281 }
282
283 target_ulong retpc = env->sepc & get_xepc_mask(env);
284 if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
285 env->priv_ver,
286 env->misa_ext) && (retpc & 0x3)) {
287 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
288 }
289
290 if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) {
291 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
292 }
293
294 if (env->virt_enabled && get_field(env->hstatus, HSTATUS_VTSR)) {
295 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
296 }
297
298 mstatus = env->mstatus;
299 prev_priv = get_field(mstatus, MSTATUS_SPP);
300 mstatus = set_field(mstatus, MSTATUS_SIE,
301 get_field(mstatus, MSTATUS_SPIE));
302 mstatus = set_field(mstatus, MSTATUS_SPIE, 1);
303 mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U);
304
305 if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
306 if (riscv_has_ext(env, RVH)) {
307 target_ulong prev_vu = get_field(env->hstatus, HSTATUS_SPV) &&
308 prev_priv == PRV_U;
309 /* Returning to VU from HS, vsstatus.sdt = 0 */
310 if (!env->virt_enabled && prev_vu) {
311 env->vsstatus = set_field(env->vsstatus, MSTATUS_SDT, 0);
312 }
313 }
314 mstatus = set_field(mstatus, MSTATUS_SDT, 0);
315 }
316 if (riscv_cpu_cfg(env)->ext_smdbltrp && env->priv >= PRV_M) {
317 mstatus = set_field(mstatus, MSTATUS_MDT, 0);
318 }
319 if (env->priv_ver >= PRIV_VERSION_1_12_0) {
320 mstatus = set_field(mstatus, MSTATUS_MPRV, 0);
321 }
322 env->mstatus = mstatus;
323
324 if (riscv_has_ext(env, RVH) && !env->virt_enabled) {
325 /* We support Hypervisor extensions and virtulisation is disabled */
326 target_ulong hstatus = env->hstatus;
327
328 prev_virt = get_field(hstatus, HSTATUS_SPV);
329 hstatus = set_field(hstatus, HSTATUS_SPV, 0);
330
331 env->hstatus = hstatus;
332
333 if (prev_virt) {
334 riscv_cpu_swap_hypervisor_regs(env);
335 }
336 }
337
338 riscv_cpu_set_mode(env, prev_priv, prev_virt);
339
340 /*
341 * If forward cfi enabled for new priv, restore elp status
342 * and clear spelp in mstatus
343 */
344 if (cpu_get_fcfien(env)) {
345 env->elp = get_field(env->mstatus, MSTATUS_SPELP);
346 }
347 env->mstatus = set_field(env->mstatus, MSTATUS_SPELP, 0);
348
349 if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) {
350 riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET,
351 src_priv, src_virt);
352 }
353
354 return retpc;
355 }
356
check_ret_from_m_mode(CPURISCVState * env,target_ulong retpc,target_ulong prev_priv,uintptr_t ra)357 static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc,
358 target_ulong prev_priv,
359 uintptr_t ra)
360 {
361 if (!(env->priv >= PRV_M)) {
362 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
363 }
364
365 if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
366 env->priv_ver,
367 env->misa_ext) && (retpc & 0x3)) {
368 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, ra);
369 }
370
371 if (riscv_cpu_cfg(env)->pmp &&
372 !pmp_get_num_rules(env) && (prev_priv != PRV_M)) {
373 riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, ra);
374 }
375 }
ssdbltrp_mxret(CPURISCVState * env,target_ulong mstatus,target_ulong prev_priv,target_ulong prev_virt)376 static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus,
377 target_ulong prev_priv,
378 target_ulong prev_virt)
379 {
380 /* If returning to U, VS or VU, sstatus.sdt = 0 */
381 if (prev_priv == PRV_U || (prev_virt &&
382 (prev_priv == PRV_S || prev_priv == PRV_U))) {
383 mstatus = set_field(mstatus, MSTATUS_SDT, 0);
384 /* If returning to VU, vsstatus.sdt = 0 */
385 if (prev_virt && prev_priv == PRV_U) {
386 env->vsstatus = set_field(env->vsstatus, MSTATUS_SDT, 0);
387 }
388 }
389
390 return mstatus;
391 }
392
helper_mret(CPURISCVState * env)393 target_ulong helper_mret(CPURISCVState *env)
394 {
395 target_ulong retpc = env->mepc & get_xepc_mask(env);
396 uint64_t mstatus = env->mstatus;
397 target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
398 uintptr_t ra = GETPC();
399
400 check_ret_from_m_mode(env, retpc, prev_priv, ra);
401
402 target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) &&
403 (prev_priv != PRV_M);
404 mstatus = set_field(mstatus, MSTATUS_MIE,
405 get_field(mstatus, MSTATUS_MPIE));
406 mstatus = set_field(mstatus, MSTATUS_MPIE, 1);
407 mstatus = set_field(mstatus, MSTATUS_MPP,
408 riscv_has_ext(env, RVU) ? PRV_U : PRV_M);
409 mstatus = set_field(mstatus, MSTATUS_MPV, 0);
410 if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
411 mstatus = ssdbltrp_mxret(env, mstatus, prev_priv, prev_virt);
412 }
413 if (riscv_cpu_cfg(env)->ext_smdbltrp) {
414 mstatus = set_field(mstatus, MSTATUS_MDT, 0);
415 }
416 if ((env->priv_ver >= PRIV_VERSION_1_12_0) && (prev_priv != PRV_M)) {
417 mstatus = set_field(mstatus, MSTATUS_MPRV, 0);
418 }
419 env->mstatus = mstatus;
420
421 if (riscv_has_ext(env, RVH) && prev_virt) {
422 riscv_cpu_swap_hypervisor_regs(env);
423 }
424
425 riscv_cpu_set_mode(env, prev_priv, prev_virt);
426 /*
427 * If forward cfi enabled for new priv, restore elp status
428 * and clear mpelp in mstatus
429 */
430 if (cpu_get_fcfien(env)) {
431 env->elp = get_field(env->mstatus, MSTATUS_MPELP);
432 }
433 env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, 0);
434
435 if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) {
436 riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET,
437 PRV_M, false);
438 }
439
440 return retpc;
441 }
442
helper_mnret(CPURISCVState * env)443 target_ulong helper_mnret(CPURISCVState *env)
444 {
445 target_ulong retpc = env->mnepc;
446 target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP);
447 target_ulong prev_virt;
448 uintptr_t ra = GETPC();
449
450 check_ret_from_m_mode(env, retpc, prev_priv, ra);
451
452 prev_virt = get_field(env->mnstatus, MNSTATUS_MNPV) &&
453 (prev_priv != PRV_M);
454 env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, true);
455
456 /*
457 * If MNRET changes the privilege mode to a mode
458 * less privileged than M, it also sets mstatus.MPRV to 0.
459 */
460 if (prev_priv < PRV_M) {
461 env->mstatus = set_field(env->mstatus, MSTATUS_MPRV, false);
462 }
463 if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
464 env->mstatus = ssdbltrp_mxret(env, env->mstatus, prev_priv, prev_virt);
465 }
466
467 if (riscv_cpu_cfg(env)->ext_smdbltrp) {
468 if (prev_priv < PRV_M) {
469 env->mstatus = set_field(env->mstatus, MSTATUS_MDT, 0);
470 }
471 }
472
473 if (riscv_has_ext(env, RVH) && prev_virt) {
474 riscv_cpu_swap_hypervisor_regs(env);
475 }
476
477 riscv_cpu_set_mode(env, prev_priv, prev_virt);
478
479 /*
480 * If forward cfi enabled for new priv, restore elp status
481 * and clear mnpelp in mnstatus
482 */
483 if (cpu_get_fcfien(env)) {
484 env->elp = get_field(env->mnstatus, MNSTATUS_MNPELP);
485 }
486 env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, 0);
487
488 return retpc;
489 }
490
helper_ctr_add_entry(CPURISCVState * env,target_ulong src,target_ulong dest,target_ulong type)491 void helper_ctr_add_entry(CPURISCVState *env, target_ulong src,
492 target_ulong dest, target_ulong type)
493 {
494 riscv_ctr_add_entry(env, src, dest, (enum CTRType)type,
495 env->priv, env->virt_enabled);
496 }
497
helper_ctr_clear(CPURISCVState * env)498 void helper_ctr_clear(CPURISCVState *env)
499 {
500 /*
501 * It's safe to call smstateen_acc_ok() for umode access regardless of the
502 * state of bit 54 (CTR bit in case of m/hstateen) of sstateen. If the bit
503 * is zero, smstateen_acc_ok() will return the correct exception code and
504 * if it's one, smstateen_acc_ok() will return RISCV_EXCP_NONE. In that
505 * scenario the U-mode check below will handle that case.
506 */
507 RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);
508 if (ret != RISCV_EXCP_NONE) {
509 riscv_raise_exception(env, ret, GETPC());
510 }
511
512 if (env->priv == PRV_U) {
513 /*
514 * One corner case is when sctrclr is executed from VU-mode and
515 * mstateen.CTR = 0, in which case we are supposed to raise
516 * RISCV_EXCP_ILLEGAL_INST. This case is already handled in
517 * smstateen_acc_ok().
518 */
519 uint32_t excep = env->virt_enabled ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT :
520 RISCV_EXCP_ILLEGAL_INST;
521 riscv_raise_exception(env, excep, GETPC());
522 }
523
524 riscv_ctr_clear(env);
525 }
526
helper_wfi(CPURISCVState * env)527 void helper_wfi(CPURISCVState *env)
528 {
529 CPUState *cs = env_cpu(env);
530 bool rvs = riscv_has_ext(env, RVS);
531 bool prv_u = env->priv == PRV_U;
532 bool prv_s = env->priv == PRV_S;
533
534 if (((prv_s || (!rvs && prv_u)) && get_field(env->mstatus, MSTATUS_TW)) ||
535 (rvs && prv_u && !env->virt_enabled)) {
536 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
537 } else if (env->virt_enabled &&
538 (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) {
539 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
540 } else {
541 cs->halted = 1;
542 cs->exception_index = EXCP_HLT;
543 cpu_loop_exit(cs);
544 }
545 }
546
helper_wrs_nto(CPURISCVState * env)547 void helper_wrs_nto(CPURISCVState *env)
548 {
549 if (env->virt_enabled && (env->priv == PRV_S || env->priv == PRV_U) &&
550 get_field(env->hstatus, HSTATUS_VTW) &&
551 !get_field(env->mstatus, MSTATUS_TW)) {
552 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
553 } else if (env->priv != PRV_M && get_field(env->mstatus, MSTATUS_TW)) {
554 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
555 }
556 }
557
helper_tlb_flush(CPURISCVState * env)558 void helper_tlb_flush(CPURISCVState *env)
559 {
560 CPUState *cs = env_cpu(env);
561 if (!env->virt_enabled &&
562 (env->priv == PRV_U ||
563 (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)))) {
564 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
565 } else if (env->virt_enabled &&
566 (env->priv == PRV_U || get_field(env->hstatus, HSTATUS_VTVM))) {
567 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
568 } else {
569 tlb_flush(cs);
570 }
571 }
572
helper_tlb_flush_all(CPURISCVState * env)573 void helper_tlb_flush_all(CPURISCVState *env)
574 {
575 CPUState *cs = env_cpu(env);
576 tlb_flush_all_cpus_synced(cs);
577 }
578
helper_hyp_tlb_flush(CPURISCVState * env)579 void helper_hyp_tlb_flush(CPURISCVState *env)
580 {
581 CPUState *cs = env_cpu(env);
582
583 if (env->virt_enabled) {
584 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
585 }
586
587 if (env->priv == PRV_M ||
588 (env->priv == PRV_S && !env->virt_enabled)) {
589 tlb_flush(cs);
590 return;
591 }
592
593 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
594 }
595
helper_hyp_gvma_tlb_flush(CPURISCVState * env)596 void helper_hyp_gvma_tlb_flush(CPURISCVState *env)
597 {
598 if (env->priv == PRV_S && !env->virt_enabled &&
599 get_field(env->mstatus, MSTATUS_TVM)) {
600 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
601 }
602
603 helper_hyp_tlb_flush(env);
604 }
605
check_access_hlsv(CPURISCVState * env,bool x,uintptr_t ra)606 static int check_access_hlsv(CPURISCVState *env, bool x, uintptr_t ra)
607 {
608 if (env->priv == PRV_M) {
609 /* always allowed */
610 } else if (env->virt_enabled) {
611 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra);
612 } else if (env->priv == PRV_U && !get_field(env->hstatus, HSTATUS_HU)) {
613 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
614 }
615
616 int mode = get_field(env->hstatus, HSTATUS_SPVP);
617 if (!x && mode == PRV_S && get_field(env->vsstatus, MSTATUS_SUM)) {
618 mode = MMUIdx_S_SUM;
619 }
620 return mode | MMU_2STAGE_BIT;
621 }
622
helper_hyp_hlv_bu(CPURISCVState * env,target_ulong addr)623 target_ulong helper_hyp_hlv_bu(CPURISCVState *env, target_ulong addr)
624 {
625 uintptr_t ra = GETPC();
626 int mmu_idx = check_access_hlsv(env, false, ra);
627 MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
628
629 return cpu_ldb_mmu(env, adjust_addr_virt(env, addr), oi, ra);
630 }
631
helper_hyp_hlv_hu(CPURISCVState * env,target_ulong addr)632 target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr)
633 {
634 uintptr_t ra = GETPC();
635 int mmu_idx = check_access_hlsv(env, false, ra);
636 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx);
637
638 return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra);
639 }
640
helper_hyp_hlv_wu(CPURISCVState * env,target_ulong addr)641 target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr)
642 {
643 uintptr_t ra = GETPC();
644 int mmu_idx = check_access_hlsv(env, false, ra);
645 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx);
646
647 return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra);
648 }
649
helper_hyp_hlv_d(CPURISCVState * env,target_ulong addr)650 target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr)
651 {
652 uintptr_t ra = GETPC();
653 int mmu_idx = check_access_hlsv(env, false, ra);
654 MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx);
655
656 return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra);
657 }
658
helper_hyp_hsv_b(CPURISCVState * env,target_ulong addr,target_ulong val)659 void helper_hyp_hsv_b(CPURISCVState *env, target_ulong addr, target_ulong val)
660 {
661 uintptr_t ra = GETPC();
662 int mmu_idx = check_access_hlsv(env, false, ra);
663 MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
664
665 cpu_stb_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
666 }
667
helper_hyp_hsv_h(CPURISCVState * env,target_ulong addr,target_ulong val)668 void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val)
669 {
670 uintptr_t ra = GETPC();
671 int mmu_idx = check_access_hlsv(env, false, ra);
672 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx);
673
674 cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
675 }
676
helper_hyp_hsv_w(CPURISCVState * env,target_ulong addr,target_ulong val)677 void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val)
678 {
679 uintptr_t ra = GETPC();
680 int mmu_idx = check_access_hlsv(env, false, ra);
681 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx);
682
683 cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
684 }
685
helper_hyp_hsv_d(CPURISCVState * env,target_ulong addr,target_ulong val)686 void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val)
687 {
688 uintptr_t ra = GETPC();
689 int mmu_idx = check_access_hlsv(env, false, ra);
690 MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx);
691
692 cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
693 }
694
695 /*
696 * TODO: These implementations are not quite correct. They perform the
697 * access using execute permission just fine, but the final PMP check
698 * is supposed to have read permission as well. Without replicating
699 * a fair fraction of cputlb.c, fixing this requires adding new mmu_idx
700 * which would imply that exact check in tlb_fill.
701 */
helper_hyp_hlvx_hu(CPURISCVState * env,target_ulong addr)702 target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr)
703 {
704 uintptr_t ra = GETPC();
705 int mmu_idx = check_access_hlsv(env, true, ra);
706 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx);
707
708 return cpu_ldw_code_mmu(env, addr, oi, GETPC());
709 }
710
helper_hyp_hlvx_wu(CPURISCVState * env,target_ulong addr)711 target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr)
712 {
713 uintptr_t ra = GETPC();
714 int mmu_idx = check_access_hlsv(env, true, ra);
715 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx);
716
717 return cpu_ldl_code_mmu(env, addr, oi, ra);
718 }
719
720 #endif /* !CONFIG_USER_ONLY */
721