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