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