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