xref: /qemu/target/riscv/op_helper.c (revision 2af4a82ab2cce3412ffc92cd4c96bd870e33bc8e)
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;
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)357 static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc,
358                                   target_ulong prev_priv)
359 {
360     if (!(env->priv >= PRV_M)) {
361         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
362     }
363 
364     if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
365                                     env->priv_ver,
366                                     env->misa_ext) && (retpc & 0x3)) {
367         riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
368     }
369 
370     if (riscv_cpu_cfg(env)->pmp &&
371         !pmp_get_num_rules(env) && (prev_priv != PRV_M)) {
372         riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC());
373     }
374 }
ssdbltrp_mxret(CPURISCVState * env,target_ulong mstatus,target_ulong prev_priv,target_ulong prev_virt)375 static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus,
376                                    target_ulong prev_priv,
377                                    target_ulong prev_virt)
378 {
379     /* If returning to U, VS or VU, sstatus.sdt = 0 */
380     if (prev_priv == PRV_U || (prev_virt &&
381         (prev_priv == PRV_S || prev_priv == PRV_U))) {
382         mstatus = set_field(mstatus, MSTATUS_SDT, 0);
383         /* If returning to VU, vsstatus.sdt = 0 */
384         if (prev_virt && prev_priv == PRV_U) {
385             env->vsstatus = set_field(env->vsstatus, MSTATUS_SDT, 0);
386         }
387     }
388 
389     return mstatus;
390 }
391 
helper_mret(CPURISCVState * env)392 target_ulong helper_mret(CPURISCVState *env)
393 {
394     target_ulong retpc = env->mepc;
395     uint64_t mstatus = env->mstatus;
396     target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
397 
398     check_ret_from_m_mode(env, retpc, prev_priv);
399 
400     target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) &&
401                              (prev_priv != PRV_M);
402     mstatus = set_field(mstatus, MSTATUS_MIE,
403                         get_field(mstatus, MSTATUS_MPIE));
404     mstatus = set_field(mstatus, MSTATUS_MPIE, 1);
405     mstatus = set_field(mstatus, MSTATUS_MPP,
406                         riscv_has_ext(env, RVU) ? PRV_U : PRV_M);
407     mstatus = set_field(mstatus, MSTATUS_MPV, 0);
408     if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
409         mstatus = ssdbltrp_mxret(env, mstatus, prev_priv, prev_virt);
410     }
411     if (riscv_cpu_cfg(env)->ext_smdbltrp) {
412         mstatus = set_field(mstatus, MSTATUS_MDT, 0);
413     }
414     if ((env->priv_ver >= PRIV_VERSION_1_12_0) && (prev_priv != PRV_M)) {
415         mstatus = set_field(mstatus, MSTATUS_MPRV, 0);
416     }
417     env->mstatus = mstatus;
418 
419     if (riscv_has_ext(env, RVH) && prev_virt) {
420         riscv_cpu_swap_hypervisor_regs(env);
421     }
422 
423     riscv_cpu_set_mode(env, prev_priv, prev_virt);
424     /*
425      * If forward cfi enabled for new priv, restore elp status
426      * and clear mpelp in mstatus
427      */
428     if (cpu_get_fcfien(env)) {
429         env->elp = get_field(env->mstatus, MSTATUS_MPELP);
430     }
431     env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, 0);
432 
433     if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) {
434         riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET,
435                             PRV_M, false);
436     }
437 
438     return retpc;
439 }
440 
helper_mnret(CPURISCVState * env)441 target_ulong helper_mnret(CPURISCVState *env)
442 {
443     target_ulong retpc = env->mnepc;
444     target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP);
445     target_ulong prev_virt;
446 
447     check_ret_from_m_mode(env, retpc, prev_priv);
448 
449     prev_virt = get_field(env->mnstatus, MNSTATUS_MNPV) &&
450                 (prev_priv != PRV_M);
451     env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, true);
452 
453     /*
454      * If MNRET changes the privilege mode to a mode
455      * less privileged than M, it also sets mstatus.MPRV to 0.
456      */
457     if (prev_priv < PRV_M) {
458         env->mstatus = set_field(env->mstatus, MSTATUS_MPRV, false);
459     }
460     if (riscv_cpu_cfg(env)->ext_ssdbltrp) {
461         env->mstatus = ssdbltrp_mxret(env, env->mstatus, prev_priv, prev_virt);
462     }
463 
464     if (riscv_cpu_cfg(env)->ext_smdbltrp) {
465         if (prev_priv < PRV_M) {
466             env->mstatus = set_field(env->mstatus, MSTATUS_MDT, 0);
467         }
468     }
469 
470     if (riscv_has_ext(env, RVH) && prev_virt) {
471         riscv_cpu_swap_hypervisor_regs(env);
472     }
473 
474     riscv_cpu_set_mode(env, prev_priv, prev_virt);
475 
476     /*
477      * If forward cfi enabled for new priv, restore elp status
478      * and clear mnpelp in mnstatus
479      */
480     if (cpu_get_fcfien(env)) {
481         env->elp = get_field(env->mnstatus, MNSTATUS_MNPELP);
482     }
483     env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, 0);
484 
485     return retpc;
486 }
487 
helper_ctr_add_entry(CPURISCVState * env,target_ulong src,target_ulong dest,target_ulong type)488 void helper_ctr_add_entry(CPURISCVState *env, target_ulong src,
489                           target_ulong dest, target_ulong type)
490 {
491     riscv_ctr_add_entry(env, src, dest, (enum CTRType)type,
492                         env->priv, env->virt_enabled);
493 }
494 
helper_ctr_clear(CPURISCVState * env)495 void helper_ctr_clear(CPURISCVState *env)
496 {
497     /*
498      * It's safe to call smstateen_acc_ok() for umode access regardless of the
499      * state of bit 54 (CTR bit in case of m/hstateen) of sstateen. If the bit
500      * is zero, smstateen_acc_ok() will return the correct exception code and
501      * if it's one, smstateen_acc_ok() will return RISCV_EXCP_NONE. In that
502      * scenario the U-mode check below will handle that case.
503      */
504     RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_CTR);
505     if (ret != RISCV_EXCP_NONE) {
506         riscv_raise_exception(env, ret, GETPC());
507     }
508 
509     if (env->priv == PRV_U) {
510         /*
511          * One corner case is when sctrclr is executed from VU-mode and
512          * mstateen.CTR = 0, in which case we are supposed to raise
513          * RISCV_EXCP_ILLEGAL_INST. This case is already handled in
514          * smstateen_acc_ok().
515          */
516         uint32_t excep = env->virt_enabled ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT :
517             RISCV_EXCP_ILLEGAL_INST;
518         riscv_raise_exception(env, excep, GETPC());
519     }
520 
521     riscv_ctr_clear(env);
522 }
523 
helper_wfi(CPURISCVState * env)524 void helper_wfi(CPURISCVState *env)
525 {
526     CPUState *cs = env_cpu(env);
527     bool rvs = riscv_has_ext(env, RVS);
528     bool prv_u = env->priv == PRV_U;
529     bool prv_s = env->priv == PRV_S;
530 
531     if (((prv_s || (!rvs && prv_u)) && get_field(env->mstatus, MSTATUS_TW)) ||
532         (rvs && prv_u && !env->virt_enabled)) {
533         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
534     } else if (env->virt_enabled &&
535                (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) {
536         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
537     } else {
538         cs->halted = 1;
539         cs->exception_index = EXCP_HLT;
540         cpu_loop_exit(cs);
541     }
542 }
543 
helper_wrs_nto(CPURISCVState * env)544 void helper_wrs_nto(CPURISCVState *env)
545 {
546     if (env->virt_enabled && (env->priv == PRV_S || env->priv == PRV_U) &&
547         get_field(env->hstatus, HSTATUS_VTW) &&
548         !get_field(env->mstatus, MSTATUS_TW)) {
549         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
550     } else if (env->priv != PRV_M && get_field(env->mstatus, MSTATUS_TW)) {
551         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
552     }
553 }
554 
helper_tlb_flush(CPURISCVState * env)555 void helper_tlb_flush(CPURISCVState *env)
556 {
557     CPUState *cs = env_cpu(env);
558     if (!env->virt_enabled &&
559         (env->priv == PRV_U ||
560          (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)))) {
561         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
562     } else if (env->virt_enabled &&
563                (env->priv == PRV_U || get_field(env->hstatus, HSTATUS_VTVM))) {
564         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
565     } else {
566         tlb_flush(cs);
567     }
568 }
569 
helper_tlb_flush_all(CPURISCVState * env)570 void helper_tlb_flush_all(CPURISCVState *env)
571 {
572     CPUState *cs = env_cpu(env);
573     tlb_flush_all_cpus_synced(cs);
574 }
575 
helper_hyp_tlb_flush(CPURISCVState * env)576 void helper_hyp_tlb_flush(CPURISCVState *env)
577 {
578     CPUState *cs = env_cpu(env);
579 
580     if (env->virt_enabled) {
581         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
582     }
583 
584     if (env->priv == PRV_M ||
585         (env->priv == PRV_S && !env->virt_enabled)) {
586         tlb_flush(cs);
587         return;
588     }
589 
590     riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
591 }
592 
helper_hyp_gvma_tlb_flush(CPURISCVState * env)593 void helper_hyp_gvma_tlb_flush(CPURISCVState *env)
594 {
595     if (env->priv == PRV_S && !env->virt_enabled &&
596         get_field(env->mstatus, MSTATUS_TVM)) {
597         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
598     }
599 
600     helper_hyp_tlb_flush(env);
601 }
602 
check_access_hlsv(CPURISCVState * env,bool x,uintptr_t ra)603 static int check_access_hlsv(CPURISCVState *env, bool x, uintptr_t ra)
604 {
605     if (env->priv == PRV_M) {
606         /* always allowed */
607     } else if (env->virt_enabled) {
608         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra);
609     } else if (env->priv == PRV_U && !get_field(env->hstatus, HSTATUS_HU)) {
610         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
611     }
612 
613     int mode = get_field(env->hstatus, HSTATUS_SPVP);
614     if (!x && mode == PRV_S && get_field(env->vsstatus, MSTATUS_SUM)) {
615         mode = MMUIdx_S_SUM;
616     }
617     return mode | MMU_2STAGE_BIT;
618 }
619 
helper_hyp_hlv_bu(CPURISCVState * env,target_ulong addr)620 target_ulong helper_hyp_hlv_bu(CPURISCVState *env, target_ulong addr)
621 {
622     uintptr_t ra = GETPC();
623     int mmu_idx = check_access_hlsv(env, false, ra);
624     MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
625 
626     return cpu_ldb_mmu(env, adjust_addr_virt(env, addr), oi, ra);
627 }
628 
helper_hyp_hlv_hu(CPURISCVState * env,target_ulong addr)629 target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr)
630 {
631     uintptr_t ra = GETPC();
632     int mmu_idx = check_access_hlsv(env, false, ra);
633     MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx);
634 
635     return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra);
636 }
637 
helper_hyp_hlv_wu(CPURISCVState * env,target_ulong addr)638 target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr)
639 {
640     uintptr_t ra = GETPC();
641     int mmu_idx = check_access_hlsv(env, false, ra);
642     MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx);
643 
644     return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra);
645 }
646 
helper_hyp_hlv_d(CPURISCVState * env,target_ulong addr)647 target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr)
648 {
649     uintptr_t ra = GETPC();
650     int mmu_idx = check_access_hlsv(env, false, ra);
651     MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx);
652 
653     return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra);
654 }
655 
helper_hyp_hsv_b(CPURISCVState * env,target_ulong addr,target_ulong val)656 void helper_hyp_hsv_b(CPURISCVState *env, target_ulong addr, target_ulong val)
657 {
658     uintptr_t ra = GETPC();
659     int mmu_idx = check_access_hlsv(env, false, ra);
660     MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
661 
662     cpu_stb_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
663 }
664 
helper_hyp_hsv_h(CPURISCVState * env,target_ulong addr,target_ulong val)665 void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val)
666 {
667     uintptr_t ra = GETPC();
668     int mmu_idx = check_access_hlsv(env, false, ra);
669     MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx);
670 
671     cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
672 }
673 
helper_hyp_hsv_w(CPURISCVState * env,target_ulong addr,target_ulong val)674 void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val)
675 {
676     uintptr_t ra = GETPC();
677     int mmu_idx = check_access_hlsv(env, false, ra);
678     MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx);
679 
680     cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
681 }
682 
helper_hyp_hsv_d(CPURISCVState * env,target_ulong addr,target_ulong val)683 void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val)
684 {
685     uintptr_t ra = GETPC();
686     int mmu_idx = check_access_hlsv(env, false, ra);
687     MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx);
688 
689     cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
690 }
691 
692 /*
693  * TODO: These implementations are not quite correct.  They perform the
694  * access using execute permission just fine, but the final PMP check
695  * is supposed to have read permission as well.  Without replicating
696  * a fair fraction of cputlb.c, fixing this requires adding new mmu_idx
697  * which would imply that exact check in tlb_fill.
698  */
helper_hyp_hlvx_hu(CPURISCVState * env,target_ulong addr)699 target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr)
700 {
701     uintptr_t ra = GETPC();
702     int mmu_idx = check_access_hlsv(env, true, ra);
703     MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx);
704 
705     return cpu_ldw_code_mmu(env, addr, oi, GETPC());
706 }
707 
helper_hyp_hlvx_wu(CPURISCVState * env,target_ulong addr)708 target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr)
709 {
710     uintptr_t ra = GETPC();
711     int mmu_idx = check_access_hlsv(env, true, ra);
712     MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx);
713 
714     return cpu_ldl_code_mmu(env, addr, oi, ra);
715 }
716 
717 #endif /* !CONFIG_USER_ONLY */
718