Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / target / riscv / csr.c
blobea3560342c4fc2f78d069e3d2278105415767c52
1 /*
2 * RISC-V Control and Status Registers.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017-2018 SiFive, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu/log.h"
22 #include "qemu/timer.h"
23 #include "cpu.h"
24 #include "tcg/tcg-cpu.h"
25 #include "pmu.h"
26 #include "time_helper.h"
27 #include "exec/exec-all.h"
28 #include "exec/tb-flush.h"
29 #include "sysemu/cpu-timers.h"
30 #include "qemu/guest-random.h"
31 #include "qapi/error.h"
33 /* CSR function table public API */
34 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops)
36 *ops = csr_ops[csrno & (CSR_TABLE_SIZE - 1)];
39 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops)
41 csr_ops[csrno & (CSR_TABLE_SIZE - 1)] = *ops;
44 /* Predicates */
45 #if !defined(CONFIG_USER_ONLY)
46 RISCVException smstateen_acc_ok(CPURISCVState *env, int index, uint64_t bit)
48 bool virt = env->virt_enabled;
50 if (env->priv == PRV_M || !riscv_cpu_cfg(env)->ext_smstateen) {
51 return RISCV_EXCP_NONE;
54 if (!(env->mstateen[index] & bit)) {
55 return RISCV_EXCP_ILLEGAL_INST;
58 if (virt) {
59 if (!(env->hstateen[index] & bit)) {
60 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
63 if (env->priv == PRV_U && !(env->sstateen[index] & bit)) {
64 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
68 if (env->priv == PRV_U && riscv_has_ext(env, RVS)) {
69 if (!(env->sstateen[index] & bit)) {
70 return RISCV_EXCP_ILLEGAL_INST;
74 return RISCV_EXCP_NONE;
76 #endif
78 static RISCVException fs(CPURISCVState *env, int csrno)
80 #if !defined(CONFIG_USER_ONLY)
81 if (!env->debugger && !riscv_cpu_fp_enabled(env) &&
82 !riscv_cpu_cfg(env)->ext_zfinx) {
83 return RISCV_EXCP_ILLEGAL_INST;
86 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
87 return smstateen_acc_ok(env, 0, SMSTATEEN0_FCSR);
89 #endif
90 return RISCV_EXCP_NONE;
93 static RISCVException vs(CPURISCVState *env, int csrno)
95 if (riscv_cpu_cfg(env)->ext_zve32x) {
96 #if !defined(CONFIG_USER_ONLY)
97 if (!env->debugger && !riscv_cpu_vector_enabled(env)) {
98 return RISCV_EXCP_ILLEGAL_INST;
100 #endif
101 return RISCV_EXCP_NONE;
103 return RISCV_EXCP_ILLEGAL_INST;
106 static RISCVException ctr(CPURISCVState *env, int csrno)
108 #if !defined(CONFIG_USER_ONLY)
109 RISCVCPU *cpu = env_archcpu(env);
110 int ctr_index;
111 target_ulong ctr_mask;
112 int base_csrno = CSR_CYCLE;
113 bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false;
115 if (rv32 && csrno >= CSR_CYCLEH) {
116 /* Offset for RV32 hpmcounternh counters */
117 base_csrno += 0x80;
119 ctr_index = csrno - base_csrno;
120 ctr_mask = BIT(ctr_index);
122 if ((csrno >= CSR_CYCLE && csrno <= CSR_INSTRET) ||
123 (csrno >= CSR_CYCLEH && csrno <= CSR_INSTRETH)) {
124 if (!riscv_cpu_cfg(env)->ext_zicntr) {
125 return RISCV_EXCP_ILLEGAL_INST;
128 goto skip_ext_pmu_check;
131 if (!(cpu->pmu_avail_ctrs & ctr_mask)) {
132 /* No counter is enabled in PMU or the counter is out of range */
133 return RISCV_EXCP_ILLEGAL_INST;
136 skip_ext_pmu_check:
138 if (env->debugger) {
139 return RISCV_EXCP_NONE;
142 if (env->priv < PRV_M && !get_field(env->mcounteren, ctr_mask)) {
143 return RISCV_EXCP_ILLEGAL_INST;
146 if (env->virt_enabled) {
147 if (!get_field(env->hcounteren, ctr_mask) ||
148 (env->priv == PRV_U && !get_field(env->scounteren, ctr_mask))) {
149 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
153 if (riscv_has_ext(env, RVS) && env->priv == PRV_U &&
154 !get_field(env->scounteren, ctr_mask)) {
155 return RISCV_EXCP_ILLEGAL_INST;
158 #endif
159 return RISCV_EXCP_NONE;
162 static RISCVException ctr32(CPURISCVState *env, int csrno)
164 if (riscv_cpu_mxl(env) != MXL_RV32) {
165 return RISCV_EXCP_ILLEGAL_INST;
168 return ctr(env, csrno);
171 static RISCVException zcmt(CPURISCVState *env, int csrno)
173 if (!riscv_cpu_cfg(env)->ext_zcmt) {
174 return RISCV_EXCP_ILLEGAL_INST;
177 #if !defined(CONFIG_USER_ONLY)
178 RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_JVT);
179 if (ret != RISCV_EXCP_NONE) {
180 return ret;
182 #endif
184 return RISCV_EXCP_NONE;
187 #if !defined(CONFIG_USER_ONLY)
188 static RISCVException mctr(CPURISCVState *env, int csrno)
190 RISCVCPU *cpu = env_archcpu(env);
191 uint32_t pmu_avail_ctrs = cpu->pmu_avail_ctrs;
192 int ctr_index;
193 int base_csrno = CSR_MHPMCOUNTER3;
195 if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) {
196 /* Offset for RV32 mhpmcounternh counters */
197 csrno -= 0x80;
200 g_assert(csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31);
202 ctr_index = csrno - base_csrno;
203 if ((BIT(ctr_index) & pmu_avail_ctrs >> 3) == 0) {
204 /* The PMU is not enabled or counter is out of range */
205 return RISCV_EXCP_ILLEGAL_INST;
208 return RISCV_EXCP_NONE;
211 static RISCVException mctr32(CPURISCVState *env, int csrno)
213 if (riscv_cpu_mxl(env) != MXL_RV32) {
214 return RISCV_EXCP_ILLEGAL_INST;
217 return mctr(env, csrno);
220 static RISCVException sscofpmf(CPURISCVState *env, int csrno)
222 if (!riscv_cpu_cfg(env)->ext_sscofpmf) {
223 return RISCV_EXCP_ILLEGAL_INST;
226 return RISCV_EXCP_NONE;
229 static RISCVException sscofpmf_32(CPURISCVState *env, int csrno)
231 if (riscv_cpu_mxl(env) != MXL_RV32) {
232 return RISCV_EXCP_ILLEGAL_INST;
235 return sscofpmf(env, csrno);
238 static RISCVException smcntrpmf(CPURISCVState *env, int csrno)
240 if (!riscv_cpu_cfg(env)->ext_smcntrpmf) {
241 return RISCV_EXCP_ILLEGAL_INST;
244 return RISCV_EXCP_NONE;
247 static RISCVException smcntrpmf_32(CPURISCVState *env, int csrno)
249 if (riscv_cpu_mxl(env) != MXL_RV32) {
250 return RISCV_EXCP_ILLEGAL_INST;
253 return smcntrpmf(env, csrno);
256 static RISCVException any(CPURISCVState *env, int csrno)
258 return RISCV_EXCP_NONE;
261 static RISCVException any32(CPURISCVState *env, int csrno)
263 if (riscv_cpu_mxl(env) != MXL_RV32) {
264 return RISCV_EXCP_ILLEGAL_INST;
267 return any(env, csrno);
271 static RISCVException aia_any(CPURISCVState *env, int csrno)
273 if (!riscv_cpu_cfg(env)->ext_smaia) {
274 return RISCV_EXCP_ILLEGAL_INST;
277 return any(env, csrno);
280 static RISCVException aia_any32(CPURISCVState *env, int csrno)
282 if (!riscv_cpu_cfg(env)->ext_smaia) {
283 return RISCV_EXCP_ILLEGAL_INST;
286 return any32(env, csrno);
289 static RISCVException smode(CPURISCVState *env, int csrno)
291 if (riscv_has_ext(env, RVS)) {
292 return RISCV_EXCP_NONE;
295 return RISCV_EXCP_ILLEGAL_INST;
298 static RISCVException smode32(CPURISCVState *env, int csrno)
300 if (riscv_cpu_mxl(env) != MXL_RV32) {
301 return RISCV_EXCP_ILLEGAL_INST;
304 return smode(env, csrno);
307 static RISCVException aia_smode(CPURISCVState *env, int csrno)
309 if (!riscv_cpu_cfg(env)->ext_ssaia) {
310 return RISCV_EXCP_ILLEGAL_INST;
313 return smode(env, csrno);
316 static RISCVException aia_smode32(CPURISCVState *env, int csrno)
318 if (!riscv_cpu_cfg(env)->ext_ssaia) {
319 return RISCV_EXCP_ILLEGAL_INST;
322 return smode32(env, csrno);
325 static RISCVException hmode(CPURISCVState *env, int csrno)
327 if (riscv_has_ext(env, RVH)) {
328 return RISCV_EXCP_NONE;
331 return RISCV_EXCP_ILLEGAL_INST;
334 static RISCVException hmode32(CPURISCVState *env, int csrno)
336 if (riscv_cpu_mxl(env) != MXL_RV32) {
337 return RISCV_EXCP_ILLEGAL_INST;
340 return hmode(env, csrno);
344 static RISCVException umode(CPURISCVState *env, int csrno)
346 if (riscv_has_ext(env, RVU)) {
347 return RISCV_EXCP_NONE;
350 return RISCV_EXCP_ILLEGAL_INST;
353 static RISCVException umode32(CPURISCVState *env, int csrno)
355 if (riscv_cpu_mxl(env) != MXL_RV32) {
356 return RISCV_EXCP_ILLEGAL_INST;
359 return umode(env, csrno);
362 static RISCVException mstateen(CPURISCVState *env, int csrno)
364 if (!riscv_cpu_cfg(env)->ext_smstateen) {
365 return RISCV_EXCP_ILLEGAL_INST;
368 return any(env, csrno);
371 static RISCVException hstateen_pred(CPURISCVState *env, int csrno, int base)
373 if (!riscv_cpu_cfg(env)->ext_smstateen) {
374 return RISCV_EXCP_ILLEGAL_INST;
377 RISCVException ret = hmode(env, csrno);
378 if (ret != RISCV_EXCP_NONE) {
379 return ret;
382 if (env->debugger) {
383 return RISCV_EXCP_NONE;
386 if (env->priv < PRV_M) {
387 if (!(env->mstateen[csrno - base] & SMSTATEEN_STATEEN)) {
388 return RISCV_EXCP_ILLEGAL_INST;
392 return RISCV_EXCP_NONE;
395 static RISCVException hstateen(CPURISCVState *env, int csrno)
397 return hstateen_pred(env, csrno, CSR_HSTATEEN0);
400 static RISCVException hstateenh(CPURISCVState *env, int csrno)
402 return hstateen_pred(env, csrno, CSR_HSTATEEN0H);
405 static RISCVException sstateen(CPURISCVState *env, int csrno)
407 bool virt = env->virt_enabled;
408 int index = csrno - CSR_SSTATEEN0;
410 if (!riscv_cpu_cfg(env)->ext_smstateen) {
411 return RISCV_EXCP_ILLEGAL_INST;
414 RISCVException ret = smode(env, csrno);
415 if (ret != RISCV_EXCP_NONE) {
416 return ret;
419 if (env->debugger) {
420 return RISCV_EXCP_NONE;
423 if (env->priv < PRV_M) {
424 if (!(env->mstateen[index] & SMSTATEEN_STATEEN)) {
425 return RISCV_EXCP_ILLEGAL_INST;
428 if (virt) {
429 if (!(env->hstateen[index] & SMSTATEEN_STATEEN)) {
430 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
435 return RISCV_EXCP_NONE;
438 static RISCVException sstc(CPURISCVState *env, int csrno)
440 bool hmode_check = false;
442 if (!riscv_cpu_cfg(env)->ext_sstc || !env->rdtime_fn) {
443 return RISCV_EXCP_ILLEGAL_INST;
446 if ((csrno == CSR_VSTIMECMP) || (csrno == CSR_VSTIMECMPH)) {
447 hmode_check = true;
450 RISCVException ret = hmode_check ? hmode(env, csrno) : smode(env, csrno);
451 if (ret != RISCV_EXCP_NONE) {
452 return ret;
455 if (env->debugger) {
456 return RISCV_EXCP_NONE;
459 if (env->priv == PRV_M) {
460 return RISCV_EXCP_NONE;
464 * No need of separate function for rv32 as menvcfg stores both menvcfg
465 * menvcfgh for RV32.
467 if (!(get_field(env->mcounteren, COUNTEREN_TM) &&
468 get_field(env->menvcfg, MENVCFG_STCE))) {
469 return RISCV_EXCP_ILLEGAL_INST;
472 if (env->virt_enabled) {
473 if (!(get_field(env->hcounteren, COUNTEREN_TM) &&
474 get_field(env->henvcfg, HENVCFG_STCE))) {
475 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
479 return RISCV_EXCP_NONE;
482 static RISCVException sstc_32(CPURISCVState *env, int csrno)
484 if (riscv_cpu_mxl(env) != MXL_RV32) {
485 return RISCV_EXCP_ILLEGAL_INST;
488 return sstc(env, csrno);
491 static RISCVException satp(CPURISCVState *env, int csrno)
493 if (env->priv == PRV_S && !env->virt_enabled &&
494 get_field(env->mstatus, MSTATUS_TVM)) {
495 return RISCV_EXCP_ILLEGAL_INST;
497 if (env->priv == PRV_S && env->virt_enabled &&
498 get_field(env->hstatus, HSTATUS_VTVM)) {
499 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
502 return smode(env, csrno);
505 static RISCVException hgatp(CPURISCVState *env, int csrno)
507 if (env->priv == PRV_S && !env->virt_enabled &&
508 get_field(env->mstatus, MSTATUS_TVM)) {
509 return RISCV_EXCP_ILLEGAL_INST;
512 return hmode(env, csrno);
515 /* Checks if PointerMasking registers could be accessed */
516 static RISCVException pointer_masking(CPURISCVState *env, int csrno)
518 /* Check if j-ext is present */
519 if (riscv_has_ext(env, RVJ)) {
520 return RISCV_EXCP_NONE;
522 return RISCV_EXCP_ILLEGAL_INST;
525 static RISCVException aia_hmode(CPURISCVState *env, int csrno)
527 if (!riscv_cpu_cfg(env)->ext_ssaia) {
528 return RISCV_EXCP_ILLEGAL_INST;
531 return hmode(env, csrno);
534 static RISCVException aia_hmode32(CPURISCVState *env, int csrno)
536 if (!riscv_cpu_cfg(env)->ext_ssaia) {
537 return RISCV_EXCP_ILLEGAL_INST;
540 return hmode32(env, csrno);
543 static RISCVException pmp(CPURISCVState *env, int csrno)
545 if (riscv_cpu_cfg(env)->pmp) {
546 if (csrno <= CSR_PMPCFG3) {
547 uint32_t reg_index = csrno - CSR_PMPCFG0;
549 /* TODO: RV128 restriction check */
550 if ((reg_index & 1) && (riscv_cpu_mxl(env) == MXL_RV64)) {
551 return RISCV_EXCP_ILLEGAL_INST;
555 return RISCV_EXCP_NONE;
558 return RISCV_EXCP_ILLEGAL_INST;
561 static RISCVException have_mseccfg(CPURISCVState *env, int csrno)
563 if (riscv_cpu_cfg(env)->ext_smepmp) {
564 return RISCV_EXCP_NONE;
566 if (riscv_cpu_cfg(env)->ext_zkr) {
567 return RISCV_EXCP_NONE;
570 return RISCV_EXCP_ILLEGAL_INST;
573 static RISCVException debug(CPURISCVState *env, int csrno)
575 if (riscv_cpu_cfg(env)->debug) {
576 return RISCV_EXCP_NONE;
579 return RISCV_EXCP_ILLEGAL_INST;
581 #endif
583 static RISCVException seed(CPURISCVState *env, int csrno)
585 if (!riscv_cpu_cfg(env)->ext_zkr) {
586 return RISCV_EXCP_ILLEGAL_INST;
589 #if !defined(CONFIG_USER_ONLY)
590 if (env->debugger) {
591 return RISCV_EXCP_NONE;
595 * With a CSR read-write instruction:
596 * 1) The seed CSR is always available in machine mode as normal.
597 * 2) Attempted access to seed from virtual modes VS and VU always raises
598 * an exception(virtual instruction exception only if mseccfg.sseed=1).
599 * 3) Without the corresponding access control bit set to 1, any attempted
600 * access to seed from U, S or HS modes will raise an illegal instruction
601 * exception.
603 if (env->priv == PRV_M) {
604 return RISCV_EXCP_NONE;
605 } else if (env->virt_enabled) {
606 if (env->mseccfg & MSECCFG_SSEED) {
607 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
608 } else {
609 return RISCV_EXCP_ILLEGAL_INST;
611 } else {
612 if (env->priv == PRV_S && (env->mseccfg & MSECCFG_SSEED)) {
613 return RISCV_EXCP_NONE;
614 } else if (env->priv == PRV_U && (env->mseccfg & MSECCFG_USEED)) {
615 return RISCV_EXCP_NONE;
616 } else {
617 return RISCV_EXCP_ILLEGAL_INST;
620 #else
621 return RISCV_EXCP_NONE;
622 #endif
625 /* User Floating-Point CSRs */
626 static RISCVException read_fflags(CPURISCVState *env, int csrno,
627 target_ulong *val)
629 *val = riscv_cpu_get_fflags(env);
630 return RISCV_EXCP_NONE;
633 static RISCVException write_fflags(CPURISCVState *env, int csrno,
634 target_ulong val)
636 #if !defined(CONFIG_USER_ONLY)
637 if (riscv_has_ext(env, RVF)) {
638 env->mstatus |= MSTATUS_FS;
640 #endif
641 riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
642 return RISCV_EXCP_NONE;
645 static RISCVException read_frm(CPURISCVState *env, int csrno,
646 target_ulong *val)
648 *val = env->frm;
649 return RISCV_EXCP_NONE;
652 static RISCVException write_frm(CPURISCVState *env, int csrno,
653 target_ulong val)
655 #if !defined(CONFIG_USER_ONLY)
656 if (riscv_has_ext(env, RVF)) {
657 env->mstatus |= MSTATUS_FS;
659 #endif
660 env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
661 return RISCV_EXCP_NONE;
664 static RISCVException read_fcsr(CPURISCVState *env, int csrno,
665 target_ulong *val)
667 *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
668 | (env->frm << FSR_RD_SHIFT);
669 return RISCV_EXCP_NONE;
672 static RISCVException write_fcsr(CPURISCVState *env, int csrno,
673 target_ulong val)
675 #if !defined(CONFIG_USER_ONLY)
676 if (riscv_has_ext(env, RVF)) {
677 env->mstatus |= MSTATUS_FS;
679 #endif
680 env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
681 riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
682 return RISCV_EXCP_NONE;
685 static RISCVException read_vtype(CPURISCVState *env, int csrno,
686 target_ulong *val)
688 uint64_t vill;
689 switch (env->xl) {
690 case MXL_RV32:
691 vill = (uint32_t)env->vill << 31;
692 break;
693 case MXL_RV64:
694 vill = (uint64_t)env->vill << 63;
695 break;
696 default:
697 g_assert_not_reached();
699 *val = (target_ulong)vill | env->vtype;
700 return RISCV_EXCP_NONE;
703 static RISCVException read_vl(CPURISCVState *env, int csrno,
704 target_ulong *val)
706 *val = env->vl;
707 return RISCV_EXCP_NONE;
710 static RISCVException read_vlenb(CPURISCVState *env, int csrno,
711 target_ulong *val)
713 *val = riscv_cpu_cfg(env)->vlenb;
714 return RISCV_EXCP_NONE;
717 static RISCVException read_vxrm(CPURISCVState *env, int csrno,
718 target_ulong *val)
720 *val = env->vxrm;
721 return RISCV_EXCP_NONE;
724 static RISCVException write_vxrm(CPURISCVState *env, int csrno,
725 target_ulong val)
727 #if !defined(CONFIG_USER_ONLY)
728 env->mstatus |= MSTATUS_VS;
729 #endif
730 env->vxrm = val;
731 return RISCV_EXCP_NONE;
734 static RISCVException read_vxsat(CPURISCVState *env, int csrno,
735 target_ulong *val)
737 *val = env->vxsat;
738 return RISCV_EXCP_NONE;
741 static RISCVException write_vxsat(CPURISCVState *env, int csrno,
742 target_ulong val)
744 #if !defined(CONFIG_USER_ONLY)
745 env->mstatus |= MSTATUS_VS;
746 #endif
747 env->vxsat = val;
748 return RISCV_EXCP_NONE;
751 static RISCVException read_vstart(CPURISCVState *env, int csrno,
752 target_ulong *val)
754 *val = env->vstart;
755 return RISCV_EXCP_NONE;
758 static RISCVException write_vstart(CPURISCVState *env, int csrno,
759 target_ulong val)
761 #if !defined(CONFIG_USER_ONLY)
762 env->mstatus |= MSTATUS_VS;
763 #endif
765 * The vstart CSR is defined to have only enough writable bits
766 * to hold the largest element index, i.e. lg2(VLEN) bits.
768 env->vstart = val & ~(~0ULL << ctzl(riscv_cpu_cfg(env)->vlenb << 3));
769 return RISCV_EXCP_NONE;
772 static RISCVException read_vcsr(CPURISCVState *env, int csrno,
773 target_ulong *val)
775 *val = (env->vxrm << VCSR_VXRM_SHIFT) | (env->vxsat << VCSR_VXSAT_SHIFT);
776 return RISCV_EXCP_NONE;
779 static RISCVException write_vcsr(CPURISCVState *env, int csrno,
780 target_ulong val)
782 #if !defined(CONFIG_USER_ONLY)
783 env->mstatus |= MSTATUS_VS;
784 #endif
785 env->vxrm = (val & VCSR_VXRM) >> VCSR_VXRM_SHIFT;
786 env->vxsat = (val & VCSR_VXSAT) >> VCSR_VXSAT_SHIFT;
787 return RISCV_EXCP_NONE;
790 #if defined(CONFIG_USER_ONLY)
791 /* User Timers and Counters */
792 static target_ulong get_ticks(bool shift)
794 int64_t val = cpu_get_host_ticks();
795 target_ulong result = shift ? val >> 32 : val;
797 return result;
800 static RISCVException read_time(CPURISCVState *env, int csrno,
801 target_ulong *val)
803 *val = cpu_get_host_ticks();
804 return RISCV_EXCP_NONE;
807 static RISCVException read_timeh(CPURISCVState *env, int csrno,
808 target_ulong *val)
810 *val = cpu_get_host_ticks() >> 32;
811 return RISCV_EXCP_NONE;
814 static RISCVException read_hpmcounter(CPURISCVState *env, int csrno,
815 target_ulong *val)
817 *val = get_ticks(false);
818 return RISCV_EXCP_NONE;
821 static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno,
822 target_ulong *val)
824 *val = get_ticks(true);
825 return RISCV_EXCP_NONE;
828 #else /* CONFIG_USER_ONLY */
830 static RISCVException read_mcyclecfg(CPURISCVState *env, int csrno,
831 target_ulong *val)
833 *val = env->mcyclecfg;
834 return RISCV_EXCP_NONE;
837 static RISCVException write_mcyclecfg(CPURISCVState *env, int csrno,
838 target_ulong val)
840 uint64_t inh_avail_mask;
842 if (riscv_cpu_mxl(env) == MXL_RV32) {
843 env->mcyclecfg = val;
844 } else {
845 /* Set xINH fields if priv mode supported */
846 inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MCYCLECFG_BIT_MINH;
847 inh_avail_mask |= riscv_has_ext(env, RVU) ? MCYCLECFG_BIT_UINH : 0;
848 inh_avail_mask |= riscv_has_ext(env, RVS) ? MCYCLECFG_BIT_SINH : 0;
849 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
850 riscv_has_ext(env, RVU)) ? MCYCLECFG_BIT_VUINH : 0;
851 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
852 riscv_has_ext(env, RVS)) ? MCYCLECFG_BIT_VSINH : 0;
853 env->mcyclecfg = val & inh_avail_mask;
856 return RISCV_EXCP_NONE;
859 static RISCVException read_mcyclecfgh(CPURISCVState *env, int csrno,
860 target_ulong *val)
862 *val = env->mcyclecfgh;
863 return RISCV_EXCP_NONE;
866 static RISCVException write_mcyclecfgh(CPURISCVState *env, int csrno,
867 target_ulong val)
869 target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK |
870 MCYCLECFGH_BIT_MINH);
872 /* Set xINH fields if priv mode supported */
873 inh_avail_mask |= riscv_has_ext(env, RVU) ? MCYCLECFGH_BIT_UINH : 0;
874 inh_avail_mask |= riscv_has_ext(env, RVS) ? MCYCLECFGH_BIT_SINH : 0;
875 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
876 riscv_has_ext(env, RVU)) ? MCYCLECFGH_BIT_VUINH : 0;
877 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
878 riscv_has_ext(env, RVS)) ? MCYCLECFGH_BIT_VSINH : 0;
880 env->mcyclecfgh = val & inh_avail_mask;
881 return RISCV_EXCP_NONE;
884 static RISCVException read_minstretcfg(CPURISCVState *env, int csrno,
885 target_ulong *val)
887 *val = env->minstretcfg;
888 return RISCV_EXCP_NONE;
891 static RISCVException write_minstretcfg(CPURISCVState *env, int csrno,
892 target_ulong val)
894 uint64_t inh_avail_mask;
896 if (riscv_cpu_mxl(env) == MXL_RV32) {
897 env->minstretcfg = val;
898 } else {
899 inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MINSTRETCFG_BIT_MINH;
900 inh_avail_mask |= riscv_has_ext(env, RVU) ? MINSTRETCFG_BIT_UINH : 0;
901 inh_avail_mask |= riscv_has_ext(env, RVS) ? MINSTRETCFG_BIT_SINH : 0;
902 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
903 riscv_has_ext(env, RVU)) ? MINSTRETCFG_BIT_VUINH : 0;
904 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
905 riscv_has_ext(env, RVS)) ? MINSTRETCFG_BIT_VSINH : 0;
906 env->minstretcfg = val & inh_avail_mask;
908 return RISCV_EXCP_NONE;
911 static RISCVException read_minstretcfgh(CPURISCVState *env, int csrno,
912 target_ulong *val)
914 *val = env->minstretcfgh;
915 return RISCV_EXCP_NONE;
918 static RISCVException write_minstretcfgh(CPURISCVState *env, int csrno,
919 target_ulong val)
921 target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK |
922 MINSTRETCFGH_BIT_MINH);
924 inh_avail_mask |= riscv_has_ext(env, RVU) ? MINSTRETCFGH_BIT_UINH : 0;
925 inh_avail_mask |= riscv_has_ext(env, RVS) ? MINSTRETCFGH_BIT_SINH : 0;
926 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
927 riscv_has_ext(env, RVU)) ? MINSTRETCFGH_BIT_VUINH : 0;
928 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
929 riscv_has_ext(env, RVS)) ? MINSTRETCFGH_BIT_VSINH : 0;
931 env->minstretcfgh = val & inh_avail_mask;
932 return RISCV_EXCP_NONE;
935 static RISCVException read_mhpmevent(CPURISCVState *env, int csrno,
936 target_ulong *val)
938 int evt_index = csrno - CSR_MCOUNTINHIBIT;
940 *val = env->mhpmevent_val[evt_index];
942 return RISCV_EXCP_NONE;
945 static RISCVException write_mhpmevent(CPURISCVState *env, int csrno,
946 target_ulong val)
948 int evt_index = csrno - CSR_MCOUNTINHIBIT;
949 uint64_t mhpmevt_val = val;
950 uint64_t inh_avail_mask;
952 if (riscv_cpu_mxl(env) == MXL_RV32) {
953 env->mhpmevent_val[evt_index] = val;
954 mhpmevt_val = mhpmevt_val |
955 ((uint64_t)env->mhpmeventh_val[evt_index] << 32);
956 } else {
957 inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MHPMEVENT_BIT_MINH;
958 inh_avail_mask |= riscv_has_ext(env, RVU) ? MHPMEVENT_BIT_UINH : 0;
959 inh_avail_mask |= riscv_has_ext(env, RVS) ? MHPMEVENT_BIT_SINH : 0;
960 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
961 riscv_has_ext(env, RVU)) ? MHPMEVENT_BIT_VUINH : 0;
962 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
963 riscv_has_ext(env, RVS)) ? MHPMEVENT_BIT_VSINH : 0;
964 mhpmevt_val = val & inh_avail_mask;
965 env->mhpmevent_val[evt_index] = mhpmevt_val;
968 riscv_pmu_update_event_map(env, mhpmevt_val, evt_index);
970 return RISCV_EXCP_NONE;
973 static RISCVException read_mhpmeventh(CPURISCVState *env, int csrno,
974 target_ulong *val)
976 int evt_index = csrno - CSR_MHPMEVENT3H + 3;
978 *val = env->mhpmeventh_val[evt_index];
980 return RISCV_EXCP_NONE;
983 static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
984 target_ulong val)
986 int evt_index = csrno - CSR_MHPMEVENT3H + 3;
987 uint64_t mhpmevth_val;
988 uint64_t mhpmevt_val = env->mhpmevent_val[evt_index];
989 target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK |
990 MHPMEVENTH_BIT_MINH);
992 inh_avail_mask |= riscv_has_ext(env, RVU) ? MHPMEVENTH_BIT_UINH : 0;
993 inh_avail_mask |= riscv_has_ext(env, RVS) ? MHPMEVENTH_BIT_SINH : 0;
994 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
995 riscv_has_ext(env, RVU)) ? MHPMEVENTH_BIT_VUINH : 0;
996 inh_avail_mask |= (riscv_has_ext(env, RVH) &&
997 riscv_has_ext(env, RVS)) ? MHPMEVENTH_BIT_VSINH : 0;
999 mhpmevth_val = val & inh_avail_mask;
1000 mhpmevt_val = mhpmevt_val | (mhpmevth_val << 32);
1001 env->mhpmeventh_val[evt_index] = mhpmevth_val;
1003 riscv_pmu_update_event_map(env, mhpmevt_val, evt_index);
1005 return RISCV_EXCP_NONE;
1008 static target_ulong riscv_pmu_ctr_get_fixed_counters_val(CPURISCVState *env,
1009 int counter_idx,
1010 bool upper_half)
1012 int inst = riscv_pmu_ctr_monitor_instructions(env, counter_idx);
1013 uint64_t *counter_arr_virt = env->pmu_fixed_ctrs[inst].counter_virt;
1014 uint64_t *counter_arr = env->pmu_fixed_ctrs[inst].counter;
1015 target_ulong result = 0;
1016 uint64_t curr_val = 0;
1017 uint64_t cfg_val = 0;
1019 if (counter_idx == 0) {
1020 cfg_val = upper_half ? ((uint64_t)env->mcyclecfgh << 32) :
1021 env->mcyclecfg;
1022 } else if (counter_idx == 2) {
1023 cfg_val = upper_half ? ((uint64_t)env->minstretcfgh << 32) :
1024 env->minstretcfg;
1025 } else {
1026 cfg_val = upper_half ?
1027 ((uint64_t)env->mhpmeventh_val[counter_idx] << 32) :
1028 env->mhpmevent_val[counter_idx];
1029 cfg_val &= MHPMEVENT_FILTER_MASK;
1032 if (!cfg_val) {
1033 if (icount_enabled()) {
1034 curr_val = inst ? icount_get_raw() : icount_get();
1035 } else {
1036 curr_val = cpu_get_host_ticks();
1039 goto done;
1042 /* Update counter before reading. */
1043 riscv_pmu_update_fixed_ctrs(env, env->priv, env->virt_enabled);
1045 if (!(cfg_val & MCYCLECFG_BIT_MINH)) {
1046 curr_val += counter_arr[PRV_M];
1049 if (!(cfg_val & MCYCLECFG_BIT_SINH)) {
1050 curr_val += counter_arr[PRV_S];
1053 if (!(cfg_val & MCYCLECFG_BIT_UINH)) {
1054 curr_val += counter_arr[PRV_U];
1057 if (!(cfg_val & MCYCLECFG_BIT_VSINH)) {
1058 curr_val += counter_arr_virt[PRV_S];
1061 if (!(cfg_val & MCYCLECFG_BIT_VUINH)) {
1062 curr_val += counter_arr_virt[PRV_U];
1065 done:
1066 if (riscv_cpu_mxl(env) == MXL_RV32) {
1067 result = upper_half ? curr_val >> 32 : curr_val;
1068 } else {
1069 result = curr_val;
1072 return result;
1075 static RISCVException write_mhpmcounter(CPURISCVState *env, int csrno,
1076 target_ulong val)
1078 int ctr_idx = csrno - CSR_MCYCLE;
1079 PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
1080 uint64_t mhpmctr_val = val;
1082 counter->mhpmcounter_val = val;
1083 if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
1084 (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
1085 riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
1086 counter->mhpmcounter_prev = riscv_pmu_ctr_get_fixed_counters_val(env,
1087 ctr_idx, false);
1088 if (ctr_idx > 2) {
1089 if (riscv_cpu_mxl(env) == MXL_RV32) {
1090 mhpmctr_val = mhpmctr_val |
1091 ((uint64_t)counter->mhpmcounterh_val << 32);
1093 riscv_pmu_setup_timer(env, mhpmctr_val, ctr_idx);
1095 } else {
1096 /* Other counters can keep incrementing from the given value */
1097 counter->mhpmcounter_prev = val;
1100 return RISCV_EXCP_NONE;
1103 static RISCVException write_mhpmcounterh(CPURISCVState *env, int csrno,
1104 target_ulong val)
1106 int ctr_idx = csrno - CSR_MCYCLEH;
1107 PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
1108 uint64_t mhpmctr_val = counter->mhpmcounter_val;
1109 uint64_t mhpmctrh_val = val;
1111 counter->mhpmcounterh_val = val;
1112 mhpmctr_val = mhpmctr_val | (mhpmctrh_val << 32);
1113 if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
1114 (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
1115 riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
1116 counter->mhpmcounterh_prev = riscv_pmu_ctr_get_fixed_counters_val(env,
1117 ctr_idx, true);
1118 if (ctr_idx > 2) {
1119 riscv_pmu_setup_timer(env, mhpmctr_val, ctr_idx);
1121 } else {
1122 counter->mhpmcounterh_prev = val;
1125 return RISCV_EXCP_NONE;
1128 RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
1129 bool upper_half, uint32_t ctr_idx)
1131 PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
1132 target_ulong ctr_prev = upper_half ? counter->mhpmcounterh_prev :
1133 counter->mhpmcounter_prev;
1134 target_ulong ctr_val = upper_half ? counter->mhpmcounterh_val :
1135 counter->mhpmcounter_val;
1137 if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
1139 * Counter should not increment if inhibit bit is set. Just return the
1140 * current counter value.
1142 *val = ctr_val;
1143 return RISCV_EXCP_NONE;
1147 * The kernel computes the perf delta by subtracting the current value from
1148 * the value it initialized previously (ctr_val).
1150 if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
1151 riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
1152 *val = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx, upper_half) -
1153 ctr_prev + ctr_val;
1154 } else {
1155 *val = ctr_val;
1158 return RISCV_EXCP_NONE;
1161 static RISCVException read_hpmcounter(CPURISCVState *env, int csrno,
1162 target_ulong *val)
1164 uint16_t ctr_index;
1166 if (csrno >= CSR_MCYCLE && csrno <= CSR_MHPMCOUNTER31) {
1167 ctr_index = csrno - CSR_MCYCLE;
1168 } else if (csrno >= CSR_CYCLE && csrno <= CSR_HPMCOUNTER31) {
1169 ctr_index = csrno - CSR_CYCLE;
1170 } else {
1171 return RISCV_EXCP_ILLEGAL_INST;
1174 return riscv_pmu_read_ctr(env, val, false, ctr_index);
1177 static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno,
1178 target_ulong *val)
1180 uint16_t ctr_index;
1182 if (csrno >= CSR_MCYCLEH && csrno <= CSR_MHPMCOUNTER31H) {
1183 ctr_index = csrno - CSR_MCYCLEH;
1184 } else if (csrno >= CSR_CYCLEH && csrno <= CSR_HPMCOUNTER31H) {
1185 ctr_index = csrno - CSR_CYCLEH;
1186 } else {
1187 return RISCV_EXCP_ILLEGAL_INST;
1190 return riscv_pmu_read_ctr(env, val, true, ctr_index);
1193 static RISCVException read_scountovf(CPURISCVState *env, int csrno,
1194 target_ulong *val)
1196 int mhpmevt_start = CSR_MHPMEVENT3 - CSR_MCOUNTINHIBIT;
1197 int i;
1198 *val = 0;
1199 target_ulong *mhpm_evt_val;
1200 uint64_t of_bit_mask;
1202 if (riscv_cpu_mxl(env) == MXL_RV32) {
1203 mhpm_evt_val = env->mhpmeventh_val;
1204 of_bit_mask = MHPMEVENTH_BIT_OF;
1205 } else {
1206 mhpm_evt_val = env->mhpmevent_val;
1207 of_bit_mask = MHPMEVENT_BIT_OF;
1210 for (i = mhpmevt_start; i < RV_MAX_MHPMEVENTS; i++) {
1211 if ((get_field(env->mcounteren, BIT(i))) &&
1212 (mhpm_evt_val[i] & of_bit_mask)) {
1213 *val |= BIT(i);
1217 return RISCV_EXCP_NONE;
1220 static RISCVException read_time(CPURISCVState *env, int csrno,
1221 target_ulong *val)
1223 uint64_t delta = env->virt_enabled ? env->htimedelta : 0;
1225 if (!env->rdtime_fn) {
1226 return RISCV_EXCP_ILLEGAL_INST;
1229 *val = env->rdtime_fn(env->rdtime_fn_arg) + delta;
1230 return RISCV_EXCP_NONE;
1233 static RISCVException read_timeh(CPURISCVState *env, int csrno,
1234 target_ulong *val)
1236 uint64_t delta = env->virt_enabled ? env->htimedelta : 0;
1238 if (!env->rdtime_fn) {
1239 return RISCV_EXCP_ILLEGAL_INST;
1242 *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32;
1243 return RISCV_EXCP_NONE;
1246 static RISCVException read_vstimecmp(CPURISCVState *env, int csrno,
1247 target_ulong *val)
1249 *val = env->vstimecmp;
1251 return RISCV_EXCP_NONE;
1254 static RISCVException read_vstimecmph(CPURISCVState *env, int csrno,
1255 target_ulong *val)
1257 *val = env->vstimecmp >> 32;
1259 return RISCV_EXCP_NONE;
1262 static RISCVException write_vstimecmp(CPURISCVState *env, int csrno,
1263 target_ulong val)
1265 if (riscv_cpu_mxl(env) == MXL_RV32) {
1266 env->vstimecmp = deposit64(env->vstimecmp, 0, 32, (uint64_t)val);
1267 } else {
1268 env->vstimecmp = val;
1271 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
1272 env->htimedelta, MIP_VSTIP);
1274 return RISCV_EXCP_NONE;
1277 static RISCVException write_vstimecmph(CPURISCVState *env, int csrno,
1278 target_ulong val)
1280 env->vstimecmp = deposit64(env->vstimecmp, 32, 32, (uint64_t)val);
1281 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
1282 env->htimedelta, MIP_VSTIP);
1284 return RISCV_EXCP_NONE;
1287 static RISCVException read_stimecmp(CPURISCVState *env, int csrno,
1288 target_ulong *val)
1290 if (env->virt_enabled) {
1291 *val = env->vstimecmp;
1292 } else {
1293 *val = env->stimecmp;
1296 return RISCV_EXCP_NONE;
1299 static RISCVException read_stimecmph(CPURISCVState *env, int csrno,
1300 target_ulong *val)
1302 if (env->virt_enabled) {
1303 *val = env->vstimecmp >> 32;
1304 } else {
1305 *val = env->stimecmp >> 32;
1308 return RISCV_EXCP_NONE;
1311 static RISCVException write_stimecmp(CPURISCVState *env, int csrno,
1312 target_ulong val)
1314 if (env->virt_enabled) {
1315 if (env->hvictl & HVICTL_VTI) {
1316 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1318 return write_vstimecmp(env, csrno, val);
1321 if (riscv_cpu_mxl(env) == MXL_RV32) {
1322 env->stimecmp = deposit64(env->stimecmp, 0, 32, (uint64_t)val);
1323 } else {
1324 env->stimecmp = val;
1327 riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, MIP_STIP);
1329 return RISCV_EXCP_NONE;
1332 static RISCVException write_stimecmph(CPURISCVState *env, int csrno,
1333 target_ulong val)
1335 if (env->virt_enabled) {
1336 if (env->hvictl & HVICTL_VTI) {
1337 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1339 return write_vstimecmph(env, csrno, val);
1342 env->stimecmp = deposit64(env->stimecmp, 32, 32, (uint64_t)val);
1343 riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, MIP_STIP);
1345 return RISCV_EXCP_NONE;
1348 #define VSTOPI_NUM_SRCS 5
1351 * All core local interrupts except the fixed ones 0:12. This macro is for
1352 * virtual interrupts logic so please don't change this to avoid messing up
1353 * the whole support, For reference see AIA spec: `5.3 Interrupt filtering and
1354 * virtual interrupts for supervisor level` and `6.3.2 Virtual interrupts for
1355 * VS level`.
1357 #define LOCAL_INTERRUPTS (~0x1FFFULL)
1359 static const uint64_t delegable_ints =
1360 S_MODE_INTERRUPTS | VS_MODE_INTERRUPTS | MIP_LCOFIP;
1361 static const uint64_t vs_delegable_ints =
1362 (VS_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & ~MIP_LCOFIP;
1363 static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
1364 HS_MODE_INTERRUPTS | LOCAL_INTERRUPTS;
1365 #define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \
1366 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \
1367 (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \
1368 (1ULL << (RISCV_EXCP_BREAKPOINT)) | \
1369 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | \
1370 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | \
1371 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | \
1372 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | \
1373 (1ULL << (RISCV_EXCP_U_ECALL)) | \
1374 (1ULL << (RISCV_EXCP_S_ECALL)) | \
1375 (1ULL << (RISCV_EXCP_VS_ECALL)) | \
1376 (1ULL << (RISCV_EXCP_M_ECALL)) | \
1377 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | \
1378 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \
1379 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \
1380 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \
1381 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \
1382 (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
1383 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)))
1384 static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &
1385 ~((1ULL << (RISCV_EXCP_S_ECALL)) |
1386 (1ULL << (RISCV_EXCP_VS_ECALL)) |
1387 (1ULL << (RISCV_EXCP_M_ECALL)) |
1388 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) |
1389 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) |
1390 (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) |
1391 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)));
1392 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
1393 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
1394 SSTATUS_SUM | SSTATUS_MXR | SSTATUS_VS;
1397 * Spec allows for bits 13:63 to be either read-only or writable.
1398 * So far we have interrupt LCOFIP in that region which is writable.
1400 * Also, spec allows to inject virtual interrupts in this region even
1401 * without any hardware interrupts for that interrupt number.
1403 * For now interrupt in 13:63 region are all kept writable. 13 being
1404 * LCOFIP and 14:63 being virtual only. Change this in future if we
1405 * introduce more interrupts that are not writable.
1408 /* Bit STIP can be an alias of mip.STIP that's why it's writable in mvip. */
1409 static const uint64_t mvip_writable_mask = MIP_SSIP | MIP_STIP | MIP_SEIP |
1410 LOCAL_INTERRUPTS;
1411 static const uint64_t mvien_writable_mask = MIP_SSIP | MIP_SEIP |
1412 LOCAL_INTERRUPTS;
1414 static const uint64_t sip_writable_mask = SIP_SSIP | LOCAL_INTERRUPTS;
1415 static const uint64_t hip_writable_mask = MIP_VSSIP;
1416 static const uint64_t hvip_writable_mask = MIP_VSSIP | MIP_VSTIP |
1417 MIP_VSEIP | LOCAL_INTERRUPTS;
1418 static const uint64_t hvien_writable_mask = LOCAL_INTERRUPTS;
1420 static const uint64_t vsip_writable_mask = MIP_VSSIP | LOCAL_INTERRUPTS;
1422 const bool valid_vm_1_10_32[16] = {
1423 [VM_1_10_MBARE] = true,
1424 [VM_1_10_SV32] = true
1427 const bool valid_vm_1_10_64[16] = {
1428 [VM_1_10_MBARE] = true,
1429 [VM_1_10_SV39] = true,
1430 [VM_1_10_SV48] = true,
1431 [VM_1_10_SV57] = true
1434 /* Machine Information Registers */
1435 static RISCVException read_zero(CPURISCVState *env, int csrno,
1436 target_ulong *val)
1438 *val = 0;
1439 return RISCV_EXCP_NONE;
1442 static RISCVException write_ignore(CPURISCVState *env, int csrno,
1443 target_ulong val)
1445 return RISCV_EXCP_NONE;
1448 static RISCVException read_mvendorid(CPURISCVState *env, int csrno,
1449 target_ulong *val)
1451 *val = riscv_cpu_cfg(env)->mvendorid;
1452 return RISCV_EXCP_NONE;
1455 static RISCVException read_marchid(CPURISCVState *env, int csrno,
1456 target_ulong *val)
1458 *val = riscv_cpu_cfg(env)->marchid;
1459 return RISCV_EXCP_NONE;
1462 static RISCVException read_mimpid(CPURISCVState *env, int csrno,
1463 target_ulong *val)
1465 *val = riscv_cpu_cfg(env)->mimpid;
1466 return RISCV_EXCP_NONE;
1469 static RISCVException read_mhartid(CPURISCVState *env, int csrno,
1470 target_ulong *val)
1472 *val = env->mhartid;
1473 return RISCV_EXCP_NONE;
1476 /* Machine Trap Setup */
1478 /* We do not store SD explicitly, only compute it on demand. */
1479 static uint64_t add_status_sd(RISCVMXL xl, uint64_t status)
1481 if ((status & MSTATUS_FS) == MSTATUS_FS ||
1482 (status & MSTATUS_VS) == MSTATUS_VS ||
1483 (status & MSTATUS_XS) == MSTATUS_XS) {
1484 switch (xl) {
1485 case MXL_RV32:
1486 return status | MSTATUS32_SD;
1487 case MXL_RV64:
1488 return status | MSTATUS64_SD;
1489 case MXL_RV128:
1490 return MSTATUSH128_SD;
1491 default:
1492 g_assert_not_reached();
1495 return status;
1498 static RISCVException read_mstatus(CPURISCVState *env, int csrno,
1499 target_ulong *val)
1501 *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus);
1502 return RISCV_EXCP_NONE;
1505 static bool validate_vm(CPURISCVState *env, target_ulong vm)
1507 uint64_t mode_supported = riscv_cpu_cfg(env)->satp_mode.map;
1508 return get_field(mode_supported, (1 << vm));
1511 static target_ulong legalize_xatp(CPURISCVState *env, target_ulong old_xatp,
1512 target_ulong val)
1514 target_ulong mask;
1515 bool vm;
1516 if (riscv_cpu_mxl(env) == MXL_RV32) {
1517 vm = validate_vm(env, get_field(val, SATP32_MODE));
1518 mask = (val ^ old_xatp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
1519 } else {
1520 vm = validate_vm(env, get_field(val, SATP64_MODE));
1521 mask = (val ^ old_xatp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
1524 if (vm && mask) {
1526 * The ISA defines SATP.MODE=Bare as "no translation", but we still
1527 * pass these through QEMU's TLB emulation as it improves
1528 * performance. Flushing the TLB on SATP writes with paging
1529 * enabled avoids leaking those invalid cached mappings.
1531 tlb_flush(env_cpu(env));
1532 return val;
1534 return old_xatp;
1537 static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp,
1538 target_ulong val)
1540 bool valid = false;
1541 target_ulong new_mpp = get_field(val, MSTATUS_MPP);
1543 switch (new_mpp) {
1544 case PRV_M:
1545 valid = true;
1546 break;
1547 case PRV_S:
1548 valid = riscv_has_ext(env, RVS);
1549 break;
1550 case PRV_U:
1551 valid = riscv_has_ext(env, RVU);
1552 break;
1555 /* Remain field unchanged if new_mpp value is invalid */
1556 if (!valid) {
1557 val = set_field(val, MSTATUS_MPP, old_mpp);
1560 return val;
1563 static RISCVException write_mstatus(CPURISCVState *env, int csrno,
1564 target_ulong val)
1566 uint64_t mstatus = env->mstatus;
1567 uint64_t mask = 0;
1568 RISCVMXL xl = riscv_cpu_mxl(env);
1571 * MPP field have been made WARL since priv version 1.11. However,
1572 * legalization for it will not break any software running on 1.10.
1574 val = legalize_mpp(env, get_field(mstatus, MSTATUS_MPP), val);
1576 /* flush tlb on mstatus fields that affect VM */
1577 if ((val ^ mstatus) & MSTATUS_MXR) {
1578 tlb_flush(env_cpu(env));
1580 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
1581 MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
1582 MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
1583 MSTATUS_TW;
1585 if (riscv_has_ext(env, RVF)) {
1586 mask |= MSTATUS_FS;
1588 if (riscv_has_ext(env, RVV)) {
1589 mask |= MSTATUS_VS;
1592 if (xl != MXL_RV32 || env->debugger) {
1593 if (riscv_has_ext(env, RVH)) {
1594 mask |= MSTATUS_MPV | MSTATUS_GVA;
1596 if ((val & MSTATUS64_UXL) != 0) {
1597 mask |= MSTATUS64_UXL;
1601 mstatus = (mstatus & ~mask) | (val & mask);
1603 env->mstatus = mstatus;
1606 * Except in debug mode, UXL/SXL can only be modified by higher
1607 * privilege mode. So xl will not be changed in normal mode.
1609 if (env->debugger) {
1610 env->xl = cpu_recompute_xl(env);
1613 riscv_cpu_update_mask(env);
1614 return RISCV_EXCP_NONE;
1617 static RISCVException read_mstatush(CPURISCVState *env, int csrno,
1618 target_ulong *val)
1620 *val = env->mstatus >> 32;
1621 return RISCV_EXCP_NONE;
1624 static RISCVException write_mstatush(CPURISCVState *env, int csrno,
1625 target_ulong val)
1627 uint64_t valh = (uint64_t)val << 32;
1628 uint64_t mask = riscv_has_ext(env, RVH) ? MSTATUS_MPV | MSTATUS_GVA : 0;
1630 env->mstatus = (env->mstatus & ~mask) | (valh & mask);
1632 return RISCV_EXCP_NONE;
1635 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno,
1636 Int128 *val)
1638 *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128,
1639 env->mstatus));
1640 return RISCV_EXCP_NONE;
1643 static RISCVException read_misa_i128(CPURISCVState *env, int csrno,
1644 Int128 *val)
1646 *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62);
1647 return RISCV_EXCP_NONE;
1650 static RISCVException read_misa(CPURISCVState *env, int csrno,
1651 target_ulong *val)
1653 target_ulong misa;
1655 switch (env->misa_mxl) {
1656 case MXL_RV32:
1657 misa = (target_ulong)MXL_RV32 << 30;
1658 break;
1659 #ifdef TARGET_RISCV64
1660 case MXL_RV64:
1661 misa = (target_ulong)MXL_RV64 << 62;
1662 break;
1663 #endif
1664 default:
1665 g_assert_not_reached();
1668 *val = misa | env->misa_ext;
1669 return RISCV_EXCP_NONE;
1672 static RISCVException write_misa(CPURISCVState *env, int csrno,
1673 target_ulong val)
1675 RISCVCPU *cpu = env_archcpu(env);
1676 uint32_t orig_misa_ext = env->misa_ext;
1677 Error *local_err = NULL;
1679 if (!riscv_cpu_cfg(env)->misa_w) {
1680 /* drop write to misa */
1681 return RISCV_EXCP_NONE;
1684 /* Mask extensions that are not supported by this hart */
1685 val &= env->misa_ext_mask;
1688 * Suppress 'C' if next instruction is not aligned
1689 * TODO: this should check next_pc
1691 if ((val & RVC) && (GETPC() & ~3) != 0) {
1692 val &= ~RVC;
1695 /* Disable RVG if any of its dependencies are disabled */
1696 if (!(val & RVI && val & RVM && val & RVA &&
1697 val & RVF && val & RVD)) {
1698 val &= ~RVG;
1701 /* If nothing changed, do nothing. */
1702 if (val == env->misa_ext) {
1703 return RISCV_EXCP_NONE;
1706 env->misa_ext = val;
1707 riscv_cpu_validate_set_extensions(cpu, &local_err);
1708 if (local_err != NULL) {
1709 /* Rollback on validation error */
1710 qemu_log_mask(LOG_GUEST_ERROR, "Unable to write MISA ext value "
1711 "0x%x, keeping existing MISA ext 0x%x\n",
1712 env->misa_ext, orig_misa_ext);
1714 env->misa_ext = orig_misa_ext;
1716 return RISCV_EXCP_NONE;
1719 if (!(env->misa_ext & RVF)) {
1720 env->mstatus &= ~MSTATUS_FS;
1723 /* flush translation cache */
1724 tb_flush(env_cpu(env));
1725 env->xl = riscv_cpu_mxl(env);
1726 return RISCV_EXCP_NONE;
1729 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
1730 target_ulong *val)
1732 *val = env->medeleg;
1733 return RISCV_EXCP_NONE;
1736 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
1737 target_ulong val)
1739 env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
1740 return RISCV_EXCP_NONE;
1743 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno,
1744 uint64_t *ret_val,
1745 uint64_t new_val, uint64_t wr_mask)
1747 uint64_t mask = wr_mask & delegable_ints;
1749 if (ret_val) {
1750 *ret_val = env->mideleg;
1753 env->mideleg = (env->mideleg & ~mask) | (new_val & mask);
1755 if (riscv_has_ext(env, RVH)) {
1756 env->mideleg |= HS_MODE_INTERRUPTS;
1759 return RISCV_EXCP_NONE;
1762 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno,
1763 target_ulong *ret_val,
1764 target_ulong new_val, target_ulong wr_mask)
1766 uint64_t rval;
1767 RISCVException ret;
1769 ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask);
1770 if (ret_val) {
1771 *ret_val = rval;
1774 return ret;
1777 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno,
1778 target_ulong *ret_val,
1779 target_ulong new_val,
1780 target_ulong wr_mask)
1782 uint64_t rval;
1783 RISCVException ret;
1785 ret = rmw_mideleg64(env, csrno, &rval,
1786 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1787 if (ret_val) {
1788 *ret_val = rval >> 32;
1791 return ret;
1794 static RISCVException rmw_mie64(CPURISCVState *env, int csrno,
1795 uint64_t *ret_val,
1796 uint64_t new_val, uint64_t wr_mask)
1798 uint64_t mask = wr_mask & all_ints;
1800 if (ret_val) {
1801 *ret_val = env->mie;
1804 env->mie = (env->mie & ~mask) | (new_val & mask);
1806 if (!riscv_has_ext(env, RVH)) {
1807 env->mie &= ~((uint64_t)HS_MODE_INTERRUPTS);
1810 return RISCV_EXCP_NONE;
1813 static RISCVException rmw_mie(CPURISCVState *env, int csrno,
1814 target_ulong *ret_val,
1815 target_ulong new_val, target_ulong wr_mask)
1817 uint64_t rval;
1818 RISCVException ret;
1820 ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask);
1821 if (ret_val) {
1822 *ret_val = rval;
1825 return ret;
1828 static RISCVException rmw_mieh(CPURISCVState *env, int csrno,
1829 target_ulong *ret_val,
1830 target_ulong new_val, target_ulong wr_mask)
1832 uint64_t rval;
1833 RISCVException ret;
1835 ret = rmw_mie64(env, csrno, &rval,
1836 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1837 if (ret_val) {
1838 *ret_val = rval >> 32;
1841 return ret;
1844 static RISCVException rmw_mvien64(CPURISCVState *env, int csrno,
1845 uint64_t *ret_val,
1846 uint64_t new_val, uint64_t wr_mask)
1848 uint64_t mask = wr_mask & mvien_writable_mask;
1850 if (ret_val) {
1851 *ret_val = env->mvien;
1854 env->mvien = (env->mvien & ~mask) | (new_val & mask);
1856 return RISCV_EXCP_NONE;
1859 static RISCVException rmw_mvien(CPURISCVState *env, int csrno,
1860 target_ulong *ret_val,
1861 target_ulong new_val, target_ulong wr_mask)
1863 uint64_t rval;
1864 RISCVException ret;
1866 ret = rmw_mvien64(env, csrno, &rval, new_val, wr_mask);
1867 if (ret_val) {
1868 *ret_val = rval;
1871 return ret;
1874 static RISCVException rmw_mvienh(CPURISCVState *env, int csrno,
1875 target_ulong *ret_val,
1876 target_ulong new_val, target_ulong wr_mask)
1878 uint64_t rval;
1879 RISCVException ret;
1881 ret = rmw_mvien64(env, csrno, &rval,
1882 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1883 if (ret_val) {
1884 *ret_val = rval >> 32;
1887 return ret;
1890 static RISCVException read_mtopi(CPURISCVState *env, int csrno,
1891 target_ulong *val)
1893 int irq;
1894 uint8_t iprio;
1896 irq = riscv_cpu_mirq_pending(env);
1897 if (irq <= 0 || irq > 63) {
1898 *val = 0;
1899 } else {
1900 iprio = env->miprio[irq];
1901 if (!iprio) {
1902 if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) {
1903 iprio = IPRIO_MMAXIPRIO;
1906 *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1907 *val |= iprio;
1910 return RISCV_EXCP_NONE;
1913 static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)
1915 if (!env->virt_enabled) {
1916 return csrno;
1919 switch (csrno) {
1920 case CSR_SISELECT:
1921 return CSR_VSISELECT;
1922 case CSR_SIREG:
1923 return CSR_VSIREG;
1924 case CSR_STOPEI:
1925 return CSR_VSTOPEI;
1926 default:
1927 return csrno;
1931 static RISCVException rmw_xiselect(CPURISCVState *env, int csrno,
1932 target_ulong *val, target_ulong new_val,
1933 target_ulong wr_mask)
1935 target_ulong *iselect;
1937 /* Translate CSR number for VS-mode */
1938 csrno = aia_xlate_vs_csrno(env, csrno);
1940 /* Find the iselect CSR based on CSR number */
1941 switch (csrno) {
1942 case CSR_MISELECT:
1943 iselect = &env->miselect;
1944 break;
1945 case CSR_SISELECT:
1946 iselect = &env->siselect;
1947 break;
1948 case CSR_VSISELECT:
1949 iselect = &env->vsiselect;
1950 break;
1951 default:
1952 return RISCV_EXCP_ILLEGAL_INST;
1955 if (val) {
1956 *val = *iselect;
1959 wr_mask &= ISELECT_MASK;
1960 if (wr_mask) {
1961 *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask);
1964 return RISCV_EXCP_NONE;
1967 static int rmw_iprio(target_ulong xlen,
1968 target_ulong iselect, uint8_t *iprio,
1969 target_ulong *val, target_ulong new_val,
1970 target_ulong wr_mask, int ext_irq_no)
1972 int i, firq, nirqs;
1973 target_ulong old_val;
1975 if (iselect < ISELECT_IPRIO0 || ISELECT_IPRIO15 < iselect) {
1976 return -EINVAL;
1978 if (xlen != 32 && iselect & 0x1) {
1979 return -EINVAL;
1982 nirqs = 4 * (xlen / 32);
1983 firq = ((iselect - ISELECT_IPRIO0) / (xlen / 32)) * (nirqs);
1985 old_val = 0;
1986 for (i = 0; i < nirqs; i++) {
1987 old_val |= ((target_ulong)iprio[firq + i]) << (IPRIO_IRQ_BITS * i);
1990 if (val) {
1991 *val = old_val;
1994 if (wr_mask) {
1995 new_val = (old_val & ~wr_mask) | (new_val & wr_mask);
1996 for (i = 0; i < nirqs; i++) {
1998 * M-level and S-level external IRQ priority always read-only
1999 * zero. This means default priority order is always preferred
2000 * for M-level and S-level external IRQs.
2002 if ((firq + i) == ext_irq_no) {
2003 continue;
2005 iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) & 0xff;
2009 return 0;
2012 static RISCVException rmw_xireg(CPURISCVState *env, int csrno,
2013 target_ulong *val, target_ulong new_val,
2014 target_ulong wr_mask)
2016 bool virt, isel_reserved;
2017 uint8_t *iprio;
2018 int ret = -EINVAL;
2019 target_ulong priv, isel, vgein;
2021 /* Translate CSR number for VS-mode */
2022 csrno = aia_xlate_vs_csrno(env, csrno);
2024 /* Decode register details from CSR number */
2025 virt = false;
2026 isel_reserved = false;
2027 switch (csrno) {
2028 case CSR_MIREG:
2029 iprio = env->miprio;
2030 isel = env->miselect;
2031 priv = PRV_M;
2032 break;
2033 case CSR_SIREG:
2034 if (env->priv == PRV_S && env->mvien & MIP_SEIP &&
2035 env->siselect >= ISELECT_IMSIC_EIDELIVERY &&
2036 env->siselect <= ISELECT_IMSIC_EIE63) {
2037 goto done;
2039 iprio = env->siprio;
2040 isel = env->siselect;
2041 priv = PRV_S;
2042 break;
2043 case CSR_VSIREG:
2044 iprio = env->hviprio;
2045 isel = env->vsiselect;
2046 priv = PRV_S;
2047 virt = true;
2048 break;
2049 default:
2050 goto done;
2053 /* Find the selected guest interrupt file */
2054 vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
2056 if (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) {
2057 /* Local interrupt priority registers not available for VS-mode */
2058 if (!virt) {
2059 ret = rmw_iprio(riscv_cpu_mxl_bits(env),
2060 isel, iprio, val, new_val, wr_mask,
2061 (priv == PRV_M) ? IRQ_M_EXT : IRQ_S_EXT);
2063 } else if (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST) {
2064 /* IMSIC registers only available when machine implements it. */
2065 if (env->aia_ireg_rmw_fn[priv]) {
2066 /* Selected guest interrupt file should not be zero */
2067 if (virt && (!vgein || env->geilen < vgein)) {
2068 goto done;
2070 /* Call machine specific IMSIC register emulation */
2071 ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
2072 AIA_MAKE_IREG(isel, priv, virt, vgein,
2073 riscv_cpu_mxl_bits(env)),
2074 val, new_val, wr_mask);
2076 } else {
2077 isel_reserved = true;
2080 done:
2081 if (ret) {
2082 return (env->virt_enabled && virt && !isel_reserved) ?
2083 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
2085 return RISCV_EXCP_NONE;
2088 static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
2089 target_ulong *val, target_ulong new_val,
2090 target_ulong wr_mask)
2092 bool virt;
2093 int ret = -EINVAL;
2094 target_ulong priv, vgein;
2096 /* Translate CSR number for VS-mode */
2097 csrno = aia_xlate_vs_csrno(env, csrno);
2099 /* Decode register details from CSR number */
2100 virt = false;
2101 switch (csrno) {
2102 case CSR_MTOPEI:
2103 priv = PRV_M;
2104 break;
2105 case CSR_STOPEI:
2106 if (env->mvien & MIP_SEIP && env->priv == PRV_S) {
2107 goto done;
2109 priv = PRV_S;
2110 break;
2111 case CSR_VSTOPEI:
2112 priv = PRV_S;
2113 virt = true;
2114 break;
2115 default:
2116 goto done;
2119 /* IMSIC CSRs only available when machine implements IMSIC. */
2120 if (!env->aia_ireg_rmw_fn[priv]) {
2121 goto done;
2124 /* Find the selected guest interrupt file */
2125 vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
2127 /* Selected guest interrupt file should be valid */
2128 if (virt && (!vgein || env->geilen < vgein)) {
2129 goto done;
2132 /* Call machine specific IMSIC register emulation for TOPEI */
2133 ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
2134 AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
2135 riscv_cpu_mxl_bits(env)),
2136 val, new_val, wr_mask);
2138 done:
2139 if (ret) {
2140 return (env->virt_enabled && virt) ?
2141 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
2143 return RISCV_EXCP_NONE;
2146 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
2147 target_ulong *val)
2149 *val = env->mtvec;
2150 return RISCV_EXCP_NONE;
2153 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
2154 target_ulong val)
2156 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
2157 if ((val & 3) < 2) {
2158 env->mtvec = val;
2159 } else {
2160 qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
2162 return RISCV_EXCP_NONE;
2165 static RISCVException read_mcountinhibit(CPURISCVState *env, int csrno,
2166 target_ulong *val)
2168 *val = env->mcountinhibit;
2169 return RISCV_EXCP_NONE;
2172 static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno,
2173 target_ulong val)
2175 int cidx;
2176 PMUCTRState *counter;
2177 RISCVCPU *cpu = env_archcpu(env);
2178 uint32_t present_ctrs = cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_IR;
2179 target_ulong updated_ctrs = (env->mcountinhibit ^ val) & present_ctrs;
2180 uint64_t mhpmctr_val, prev_count, curr_count;
2182 /* WARL register - disable unavailable counters; TM bit is always 0 */
2183 env->mcountinhibit = val & present_ctrs;
2185 /* Check if any other counter is also monitoring cycles/instructions */
2186 for (cidx = 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) {
2187 if (!(updated_ctrs & BIT(cidx)) ||
2188 (!riscv_pmu_ctr_monitor_cycles(env, cidx) &&
2189 !riscv_pmu_ctr_monitor_instructions(env, cidx))) {
2190 continue;
2193 counter = &env->pmu_ctrs[cidx];
2195 if (!get_field(env->mcountinhibit, BIT(cidx))) {
2196 counter->mhpmcounter_prev =
2197 riscv_pmu_ctr_get_fixed_counters_val(env, cidx, false);
2198 if (riscv_cpu_mxl(env) == MXL_RV32) {
2199 counter->mhpmcounterh_prev =
2200 riscv_pmu_ctr_get_fixed_counters_val(env, cidx, true);
2203 if (cidx > 2) {
2204 mhpmctr_val = counter->mhpmcounter_val;
2205 if (riscv_cpu_mxl(env) == MXL_RV32) {
2206 mhpmctr_val = mhpmctr_val |
2207 ((uint64_t)counter->mhpmcounterh_val << 32);
2209 riscv_pmu_setup_timer(env, mhpmctr_val, cidx);
2211 } else {
2212 curr_count = riscv_pmu_ctr_get_fixed_counters_val(env, cidx, false);
2214 mhpmctr_val = counter->mhpmcounter_val;
2215 prev_count = counter->mhpmcounter_prev;
2216 if (riscv_cpu_mxl(env) == MXL_RV32) {
2217 uint64_t tmp =
2218 riscv_pmu_ctr_get_fixed_counters_val(env, cidx, true);
2220 curr_count = curr_count | (tmp << 32);
2221 mhpmctr_val = mhpmctr_val |
2222 ((uint64_t)counter->mhpmcounterh_val << 32);
2223 prev_count = prev_count |
2224 ((uint64_t)counter->mhpmcounterh_prev << 32);
2227 /* Adjust the counter for later reads. */
2228 mhpmctr_val = curr_count - prev_count + mhpmctr_val;
2229 counter->mhpmcounter_val = mhpmctr_val;
2230 if (riscv_cpu_mxl(env) == MXL_RV32) {
2231 counter->mhpmcounterh_val = mhpmctr_val >> 32;
2236 return RISCV_EXCP_NONE;
2239 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
2240 target_ulong *val)
2242 *val = env->mcounteren;
2243 return RISCV_EXCP_NONE;
2246 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
2247 target_ulong val)
2249 RISCVCPU *cpu = env_archcpu(env);
2251 /* WARL register - disable unavailable counters */
2252 env->mcounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM |
2253 COUNTEREN_IR);
2254 return RISCV_EXCP_NONE;
2257 /* Machine Trap Handling */
2258 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno,
2259 Int128 *val)
2261 *val = int128_make128(env->mscratch, env->mscratchh);
2262 return RISCV_EXCP_NONE;
2265 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno,
2266 Int128 val)
2268 env->mscratch = int128_getlo(val);
2269 env->mscratchh = int128_gethi(val);
2270 return RISCV_EXCP_NONE;
2273 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
2274 target_ulong *val)
2276 *val = env->mscratch;
2277 return RISCV_EXCP_NONE;
2280 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
2281 target_ulong val)
2283 env->mscratch = val;
2284 return RISCV_EXCP_NONE;
2287 static RISCVException read_mepc(CPURISCVState *env, int csrno,
2288 target_ulong *val)
2290 *val = env->mepc;
2291 return RISCV_EXCP_NONE;
2294 static RISCVException write_mepc(CPURISCVState *env, int csrno,
2295 target_ulong val)
2297 env->mepc = val;
2298 return RISCV_EXCP_NONE;
2301 static RISCVException read_mcause(CPURISCVState *env, int csrno,
2302 target_ulong *val)
2304 *val = env->mcause;
2305 return RISCV_EXCP_NONE;
2308 static RISCVException write_mcause(CPURISCVState *env, int csrno,
2309 target_ulong val)
2311 env->mcause = val;
2312 return RISCV_EXCP_NONE;
2315 static RISCVException read_mtval(CPURISCVState *env, int csrno,
2316 target_ulong *val)
2318 *val = env->mtval;
2319 return RISCV_EXCP_NONE;
2322 static RISCVException write_mtval(CPURISCVState *env, int csrno,
2323 target_ulong val)
2325 env->mtval = val;
2326 return RISCV_EXCP_NONE;
2329 /* Execution environment configuration setup */
2330 static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
2331 target_ulong *val)
2333 *val = env->menvcfg;
2334 return RISCV_EXCP_NONE;
2337 static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
2338 target_ulong val)
2340 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
2341 uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE;
2343 if (riscv_cpu_mxl(env) == MXL_RV64) {
2344 mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
2345 (cfg->ext_sstc ? MENVCFG_STCE : 0) |
2346 (cfg->ext_svadu ? MENVCFG_ADUE : 0);
2348 env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
2350 return RISCV_EXCP_NONE;
2353 static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
2354 target_ulong *val)
2356 *val = env->menvcfg >> 32;
2357 return RISCV_EXCP_NONE;
2360 static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
2361 target_ulong val)
2363 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
2364 uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
2365 (cfg->ext_sstc ? MENVCFG_STCE : 0) |
2366 (cfg->ext_svadu ? MENVCFG_ADUE : 0);
2367 uint64_t valh = (uint64_t)val << 32;
2369 env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);
2371 return RISCV_EXCP_NONE;
2374 static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
2375 target_ulong *val)
2377 RISCVException ret;
2379 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2380 if (ret != RISCV_EXCP_NONE) {
2381 return ret;
2384 *val = env->senvcfg;
2385 return RISCV_EXCP_NONE;
2388 static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
2389 target_ulong val)
2391 uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE;
2392 RISCVException ret;
2394 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2395 if (ret != RISCV_EXCP_NONE) {
2396 return ret;
2399 env->senvcfg = (env->senvcfg & ~mask) | (val & mask);
2400 return RISCV_EXCP_NONE;
2403 static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
2404 target_ulong *val)
2406 RISCVException ret;
2408 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2409 if (ret != RISCV_EXCP_NONE) {
2410 return ret;
2414 * henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0
2415 * henvcfg.stce is read_only 0 when menvcfg.stce = 0
2416 * henvcfg.adue is read_only 0 when menvcfg.adue = 0
2418 *val = env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE) |
2419 env->menvcfg);
2420 return RISCV_EXCP_NONE;
2423 static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
2424 target_ulong val)
2426 uint64_t mask = HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCFG_CBZE;
2427 RISCVException ret;
2429 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2430 if (ret != RISCV_EXCP_NONE) {
2431 return ret;
2434 if (riscv_cpu_mxl(env) == MXL_RV64) {
2435 mask |= env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE);
2438 env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
2440 return RISCV_EXCP_NONE;
2443 static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
2444 target_ulong *val)
2446 RISCVException ret;
2448 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2449 if (ret != RISCV_EXCP_NONE) {
2450 return ret;
2453 *val = (env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE) |
2454 env->menvcfg)) >> 32;
2455 return RISCV_EXCP_NONE;
2458 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
2459 target_ulong val)
2461 uint64_t mask = env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE |
2462 HENVCFG_ADUE);
2463 uint64_t valh = (uint64_t)val << 32;
2464 RISCVException ret;
2466 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2467 if (ret != RISCV_EXCP_NONE) {
2468 return ret;
2471 env->henvcfg = (env->henvcfg & ~mask) | (valh & mask);
2472 return RISCV_EXCP_NONE;
2475 static RISCVException read_mstateen(CPURISCVState *env, int csrno,
2476 target_ulong *val)
2478 *val = env->mstateen[csrno - CSR_MSTATEEN0];
2480 return RISCV_EXCP_NONE;
2483 static RISCVException write_mstateen(CPURISCVState *env, int csrno,
2484 uint64_t wr_mask, target_ulong new_val)
2486 uint64_t *reg;
2488 reg = &env->mstateen[csrno - CSR_MSTATEEN0];
2489 *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2491 return RISCV_EXCP_NONE;
2494 static RISCVException write_mstateen0(CPURISCVState *env, int csrno,
2495 target_ulong new_val)
2497 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2498 if (!riscv_has_ext(env, RVF)) {
2499 wr_mask |= SMSTATEEN0_FCSR;
2502 if (env->priv_ver >= PRIV_VERSION_1_13_0) {
2503 wr_mask |= SMSTATEEN0_P1P13;
2506 return write_mstateen(env, csrno, wr_mask, new_val);
2509 static RISCVException write_mstateen_1_3(CPURISCVState *env, int csrno,
2510 target_ulong new_val)
2512 return write_mstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2515 static RISCVException read_mstateenh(CPURISCVState *env, int csrno,
2516 target_ulong *val)
2518 *val = env->mstateen[csrno - CSR_MSTATEEN0H] >> 32;
2520 return RISCV_EXCP_NONE;
2523 static RISCVException write_mstateenh(CPURISCVState *env, int csrno,
2524 uint64_t wr_mask, target_ulong new_val)
2526 uint64_t *reg, val;
2528 reg = &env->mstateen[csrno - CSR_MSTATEEN0H];
2529 val = (uint64_t)new_val << 32;
2530 val |= *reg & 0xFFFFFFFF;
2531 *reg = (*reg & ~wr_mask) | (val & wr_mask);
2533 return RISCV_EXCP_NONE;
2536 static RISCVException write_mstateen0h(CPURISCVState *env, int csrno,
2537 target_ulong new_val)
2539 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2541 if (env->priv_ver >= PRIV_VERSION_1_13_0) {
2542 wr_mask |= SMSTATEEN0_P1P13;
2545 return write_mstateenh(env, csrno, wr_mask, new_val);
2548 static RISCVException write_mstateenh_1_3(CPURISCVState *env, int csrno,
2549 target_ulong new_val)
2551 return write_mstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2554 static RISCVException read_hstateen(CPURISCVState *env, int csrno,
2555 target_ulong *val)
2557 int index = csrno - CSR_HSTATEEN0;
2559 *val = env->hstateen[index] & env->mstateen[index];
2561 return RISCV_EXCP_NONE;
2564 static RISCVException write_hstateen(CPURISCVState *env, int csrno,
2565 uint64_t mask, target_ulong new_val)
2567 int index = csrno - CSR_HSTATEEN0;
2568 uint64_t *reg, wr_mask;
2570 reg = &env->hstateen[index];
2571 wr_mask = env->mstateen[index] & mask;
2572 *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2574 return RISCV_EXCP_NONE;
2577 static RISCVException write_hstateen0(CPURISCVState *env, int csrno,
2578 target_ulong new_val)
2580 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2582 if (!riscv_has_ext(env, RVF)) {
2583 wr_mask |= SMSTATEEN0_FCSR;
2586 return write_hstateen(env, csrno, wr_mask, new_val);
2589 static RISCVException write_hstateen_1_3(CPURISCVState *env, int csrno,
2590 target_ulong new_val)
2592 return write_hstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2595 static RISCVException read_hstateenh(CPURISCVState *env, int csrno,
2596 target_ulong *val)
2598 int index = csrno - CSR_HSTATEEN0H;
2600 *val = (env->hstateen[index] >> 32) & (env->mstateen[index] >> 32);
2602 return RISCV_EXCP_NONE;
2605 static RISCVException write_hstateenh(CPURISCVState *env, int csrno,
2606 uint64_t mask, target_ulong new_val)
2608 int index = csrno - CSR_HSTATEEN0H;
2609 uint64_t *reg, wr_mask, val;
2611 reg = &env->hstateen[index];
2612 val = (uint64_t)new_val << 32;
2613 val |= *reg & 0xFFFFFFFF;
2614 wr_mask = env->mstateen[index] & mask;
2615 *reg = (*reg & ~wr_mask) | (val & wr_mask);
2617 return RISCV_EXCP_NONE;
2620 static RISCVException write_hstateen0h(CPURISCVState *env, int csrno,
2621 target_ulong new_val)
2623 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2625 return write_hstateenh(env, csrno, wr_mask, new_val);
2628 static RISCVException write_hstateenh_1_3(CPURISCVState *env, int csrno,
2629 target_ulong new_val)
2631 return write_hstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2634 static RISCVException read_sstateen(CPURISCVState *env, int csrno,
2635 target_ulong *val)
2637 bool virt = env->virt_enabled;
2638 int index = csrno - CSR_SSTATEEN0;
2640 *val = env->sstateen[index] & env->mstateen[index];
2641 if (virt) {
2642 *val &= env->hstateen[index];
2645 return RISCV_EXCP_NONE;
2648 static RISCVException write_sstateen(CPURISCVState *env, int csrno,
2649 uint64_t mask, target_ulong new_val)
2651 bool virt = env->virt_enabled;
2652 int index = csrno - CSR_SSTATEEN0;
2653 uint64_t wr_mask;
2654 uint64_t *reg;
2656 wr_mask = env->mstateen[index] & mask;
2657 if (virt) {
2658 wr_mask &= env->hstateen[index];
2661 reg = &env->sstateen[index];
2662 *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2664 return RISCV_EXCP_NONE;
2667 static RISCVException write_sstateen0(CPURISCVState *env, int csrno,
2668 target_ulong new_val)
2670 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2672 if (!riscv_has_ext(env, RVF)) {
2673 wr_mask |= SMSTATEEN0_FCSR;
2676 return write_sstateen(env, csrno, wr_mask, new_val);
2679 static RISCVException write_sstateen_1_3(CPURISCVState *env, int csrno,
2680 target_ulong new_val)
2682 return write_sstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2685 static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
2686 uint64_t *ret_val,
2687 uint64_t new_val, uint64_t wr_mask)
2689 uint64_t old_mip, mask = wr_mask & delegable_ints;
2690 uint32_t gin;
2692 if (mask & MIP_SEIP) {
2693 env->software_seip = new_val & MIP_SEIP;
2694 new_val |= env->external_seip * MIP_SEIP;
2697 if (riscv_cpu_cfg(env)->ext_sstc && (env->priv == PRV_M) &&
2698 get_field(env->menvcfg, MENVCFG_STCE)) {
2699 /* sstc extension forbids STIP & VSTIP to be writeable in mip */
2700 mask = mask & ~(MIP_STIP | MIP_VSTIP);
2703 if (mask) {
2704 old_mip = riscv_cpu_update_mip(env, mask, (new_val & mask));
2705 } else {
2706 old_mip = env->mip;
2709 if (csrno != CSR_HVIP) {
2710 gin = get_field(env->hstatus, HSTATUS_VGEIN);
2711 old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0;
2712 old_mip |= env->vstime_irq ? MIP_VSTIP : 0;
2715 if (ret_val) {
2716 *ret_val = old_mip;
2719 return RISCV_EXCP_NONE;
2722 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
2723 target_ulong *ret_val,
2724 target_ulong new_val, target_ulong wr_mask)
2726 uint64_t rval;
2727 RISCVException ret;
2729 ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask);
2730 if (ret_val) {
2731 *ret_val = rval;
2734 return ret;
2737 static RISCVException rmw_miph(CPURISCVState *env, int csrno,
2738 target_ulong *ret_val,
2739 target_ulong new_val, target_ulong wr_mask)
2741 uint64_t rval;
2742 RISCVException ret;
2744 ret = rmw_mip64(env, csrno, &rval,
2745 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2746 if (ret_val) {
2747 *ret_val = rval >> 32;
2750 return ret;
2754 * The function is written for two use-cases:
2755 * 1- To access mvip csr as is for m-mode access.
2756 * 2- To access sip as a combination of mip and mvip for s-mode.
2758 * Both report bits 1, 5, 9 and 13:63 but with the exception of
2759 * STIP being read-only zero in case of mvip when sstc extension
2760 * is present.
2761 * Also, sip needs to be read-only zero when both mideleg[i] and
2762 * mvien[i] are zero but mvip needs to be an alias of mip.
2764 static RISCVException rmw_mvip64(CPURISCVState *env, int csrno,
2765 uint64_t *ret_val,
2766 uint64_t new_val, uint64_t wr_mask)
2768 RISCVCPU *cpu = env_archcpu(env);
2769 target_ulong ret_mip = 0;
2770 RISCVException ret;
2771 uint64_t old_mvip;
2774 * mideleg[i] mvien[i]
2775 * 0 0 No delegation. mvip[i] is alias of mip[i].
2776 * 0 1 mvip[i] becomes source of interrupt, mip bypassed.
2777 * 1 X mip[i] is source of interrupt and mvip[i] aliases
2778 * mip[i].
2780 * So alias condition would be for bits:
2781 * ((S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & (mideleg | ~mvien)) |
2782 * (!sstc & MIP_STIP)
2784 * Non-alias condition will be for bits:
2785 * (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & (~mideleg & mvien)
2787 * alias_mask denotes the bits that come from mip nalias_mask denotes bits
2788 * that come from hvip.
2790 uint64_t alias_mask = ((S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) &
2791 (env->mideleg | ~env->mvien)) | MIP_STIP;
2792 uint64_t nalias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) &
2793 (~env->mideleg & env->mvien);
2794 uint64_t wr_mask_mvip;
2795 uint64_t wr_mask_mip;
2798 * mideleg[i] mvien[i]
2799 * 0 0 sip[i] read-only zero.
2800 * 0 1 sip[i] alias of mvip[i].
2801 * 1 X sip[i] alias of mip[i].
2803 * Both alias and non-alias mask remain same for sip except for bits
2804 * which are zero in both mideleg and mvien.
2806 if (csrno == CSR_SIP) {
2807 /* Remove bits that are zero in both mideleg and mvien. */
2808 alias_mask &= (env->mideleg | env->mvien);
2809 nalias_mask &= (env->mideleg | env->mvien);
2813 * If sstc is present, mvip.STIP is not an alias of mip.STIP so clear
2814 * that our in mip returned value.
2816 if (cpu->cfg.ext_sstc && (env->priv == PRV_M) &&
2817 get_field(env->menvcfg, MENVCFG_STCE)) {
2818 alias_mask &= ~MIP_STIP;
2821 wr_mask_mip = wr_mask & alias_mask & mvip_writable_mask;
2822 wr_mask_mvip = wr_mask & nalias_mask & mvip_writable_mask;
2825 * For bits set in alias_mask, mvip needs to be alias of mip, so forward
2826 * this to rmw_mip.
2828 ret = rmw_mip(env, CSR_MIP, &ret_mip, new_val, wr_mask_mip);
2829 if (ret != RISCV_EXCP_NONE) {
2830 return ret;
2833 old_mvip = env->mvip;
2836 * Write to mvip. Update only non-alias bits. Alias bits were updated
2837 * in mip in rmw_mip above.
2839 if (wr_mask_mvip) {
2840 env->mvip = (env->mvip & ~wr_mask_mvip) | (new_val & wr_mask_mvip);
2843 * Given mvip is separate source from mip, we need to trigger interrupt
2844 * from here separately. Normally this happen from riscv_cpu_update_mip.
2846 riscv_cpu_interrupt(env);
2849 if (ret_val) {
2850 ret_mip &= alias_mask;
2851 old_mvip &= nalias_mask;
2853 *ret_val = old_mvip | ret_mip;
2856 return RISCV_EXCP_NONE;
2859 static RISCVException rmw_mvip(CPURISCVState *env, int csrno,
2860 target_ulong *ret_val,
2861 target_ulong new_val, target_ulong wr_mask)
2863 uint64_t rval;
2864 RISCVException ret;
2866 ret = rmw_mvip64(env, csrno, &rval, new_val, wr_mask);
2867 if (ret_val) {
2868 *ret_val = rval;
2871 return ret;
2874 static RISCVException rmw_mviph(CPURISCVState *env, int csrno,
2875 target_ulong *ret_val,
2876 target_ulong new_val, target_ulong wr_mask)
2878 uint64_t rval;
2879 RISCVException ret;
2881 ret = rmw_mvip64(env, csrno, &rval,
2882 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2883 if (ret_val) {
2884 *ret_val = rval >> 32;
2887 return ret;
2890 /* Supervisor Trap Setup */
2891 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,
2892 Int128 *val)
2894 uint64_t mask = sstatus_v1_10_mask;
2895 uint64_t sstatus = env->mstatus & mask;
2896 if (env->xl != MXL_RV32 || env->debugger) {
2897 mask |= SSTATUS64_UXL;
2900 *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus));
2901 return RISCV_EXCP_NONE;
2904 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
2905 target_ulong *val)
2907 target_ulong mask = (sstatus_v1_10_mask);
2908 if (env->xl != MXL_RV32 || env->debugger) {
2909 mask |= SSTATUS64_UXL;
2911 /* TODO: Use SXL not MXL. */
2912 *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
2913 return RISCV_EXCP_NONE;
2916 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
2917 target_ulong val)
2919 target_ulong mask = (sstatus_v1_10_mask);
2921 if (env->xl != MXL_RV32 || env->debugger) {
2922 if ((val & SSTATUS64_UXL) != 0) {
2923 mask |= SSTATUS64_UXL;
2926 target_ulong newval = (env->mstatus & ~mask) | (val & mask);
2927 return write_mstatus(env, CSR_MSTATUS, newval);
2930 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno,
2931 uint64_t *ret_val,
2932 uint64_t new_val, uint64_t wr_mask)
2934 uint64_t alias_mask = (LOCAL_INTERRUPTS | VS_MODE_INTERRUPTS) &
2935 env->hideleg;
2936 uint64_t nalias_mask = LOCAL_INTERRUPTS & (~env->hideleg & env->hvien);
2937 uint64_t rval, rval_vs, vsbits;
2938 uint64_t wr_mask_vsie;
2939 uint64_t wr_mask_mie;
2940 RISCVException ret;
2942 /* Bring VS-level bits to correct position */
2943 vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
2944 new_val &= ~(VS_MODE_INTERRUPTS >> 1);
2945 new_val |= vsbits << 1;
2947 vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
2948 wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
2949 wr_mask |= vsbits << 1;
2951 wr_mask_mie = wr_mask & alias_mask;
2952 wr_mask_vsie = wr_mask & nalias_mask;
2954 ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask_mie);
2956 rval_vs = env->vsie & nalias_mask;
2957 env->vsie = (env->vsie & ~wr_mask_vsie) | (new_val & wr_mask_vsie);
2959 if (ret_val) {
2960 rval &= alias_mask;
2961 vsbits = rval & VS_MODE_INTERRUPTS;
2962 rval &= ~VS_MODE_INTERRUPTS;
2963 *ret_val = rval | (vsbits >> 1) | rval_vs;
2966 return ret;
2969 static RISCVException rmw_vsie(CPURISCVState *env, int csrno,
2970 target_ulong *ret_val,
2971 target_ulong new_val, target_ulong wr_mask)
2973 uint64_t rval;
2974 RISCVException ret;
2976 ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask);
2977 if (ret_val) {
2978 *ret_val = rval;
2981 return ret;
2984 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno,
2985 target_ulong *ret_val,
2986 target_ulong new_val, target_ulong wr_mask)
2988 uint64_t rval;
2989 RISCVException ret;
2991 ret = rmw_vsie64(env, csrno, &rval,
2992 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2993 if (ret_val) {
2994 *ret_val = rval >> 32;
2997 return ret;
3000 static RISCVException rmw_sie64(CPURISCVState *env, int csrno,
3001 uint64_t *ret_val,
3002 uint64_t new_val, uint64_t wr_mask)
3004 uint64_t nalias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) &
3005 (~env->mideleg & env->mvien);
3006 uint64_t alias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & env->mideleg;
3007 uint64_t sie_mask = wr_mask & nalias_mask;
3008 RISCVException ret;
3011 * mideleg[i] mvien[i]
3012 * 0 0 sie[i] read-only zero.
3013 * 0 1 sie[i] is a separate writable bit.
3014 * 1 X sie[i] alias of mie[i].
3016 * Both alias and non-alias mask remain same for sip except for bits
3017 * which are zero in both mideleg and mvien.
3019 if (env->virt_enabled) {
3020 if (env->hvictl & HVICTL_VTI) {
3021 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
3023 ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask);
3024 if (ret_val) {
3025 *ret_val &= alias_mask;
3027 } else {
3028 ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & alias_mask);
3029 if (ret_val) {
3030 *ret_val &= alias_mask;
3031 *ret_val |= env->sie & nalias_mask;
3034 env->sie = (env->sie & ~sie_mask) | (new_val & sie_mask);
3037 return ret;
3040 static RISCVException rmw_sie(CPURISCVState *env, int csrno,
3041 target_ulong *ret_val,
3042 target_ulong new_val, target_ulong wr_mask)
3044 uint64_t rval;
3045 RISCVException ret;
3047 ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask);
3048 if (ret == RISCV_EXCP_NONE && ret_val) {
3049 *ret_val = rval;
3052 return ret;
3055 static RISCVException rmw_sieh(CPURISCVState *env, int csrno,
3056 target_ulong *ret_val,
3057 target_ulong new_val, target_ulong wr_mask)
3059 uint64_t rval;
3060 RISCVException ret;
3062 ret = rmw_sie64(env, csrno, &rval,
3063 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3064 if (ret_val) {
3065 *ret_val = rval >> 32;
3068 return ret;
3071 static RISCVException read_stvec(CPURISCVState *env, int csrno,
3072 target_ulong *val)
3074 *val = env->stvec;
3075 return RISCV_EXCP_NONE;
3078 static RISCVException write_stvec(CPURISCVState *env, int csrno,
3079 target_ulong val)
3081 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
3082 if ((val & 3) < 2) {
3083 env->stvec = val;
3084 } else {
3085 qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
3087 return RISCV_EXCP_NONE;
3090 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
3091 target_ulong *val)
3093 *val = env->scounteren;
3094 return RISCV_EXCP_NONE;
3097 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
3098 target_ulong val)
3100 RISCVCPU *cpu = env_archcpu(env);
3102 /* WARL register - disable unavailable counters */
3103 env->scounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM |
3104 COUNTEREN_IR);
3105 return RISCV_EXCP_NONE;
3108 /* Supervisor Trap Handling */
3109 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno,
3110 Int128 *val)
3112 *val = int128_make128(env->sscratch, env->sscratchh);
3113 return RISCV_EXCP_NONE;
3116 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno,
3117 Int128 val)
3119 env->sscratch = int128_getlo(val);
3120 env->sscratchh = int128_gethi(val);
3121 return RISCV_EXCP_NONE;
3124 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
3125 target_ulong *val)
3127 *val = env->sscratch;
3128 return RISCV_EXCP_NONE;
3131 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
3132 target_ulong val)
3134 env->sscratch = val;
3135 return RISCV_EXCP_NONE;
3138 static RISCVException read_sepc(CPURISCVState *env, int csrno,
3139 target_ulong *val)
3141 *val = env->sepc;
3142 return RISCV_EXCP_NONE;
3145 static RISCVException write_sepc(CPURISCVState *env, int csrno,
3146 target_ulong val)
3148 env->sepc = val;
3149 return RISCV_EXCP_NONE;
3152 static RISCVException read_scause(CPURISCVState *env, int csrno,
3153 target_ulong *val)
3155 *val = env->scause;
3156 return RISCV_EXCP_NONE;
3159 static RISCVException write_scause(CPURISCVState *env, int csrno,
3160 target_ulong val)
3162 env->scause = val;
3163 return RISCV_EXCP_NONE;
3166 static RISCVException read_stval(CPURISCVState *env, int csrno,
3167 target_ulong *val)
3169 *val = env->stval;
3170 return RISCV_EXCP_NONE;
3173 static RISCVException write_stval(CPURISCVState *env, int csrno,
3174 target_ulong val)
3176 env->stval = val;
3177 return RISCV_EXCP_NONE;
3180 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
3181 uint64_t *ret_val,
3182 uint64_t new_val, uint64_t wr_mask);
3184 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno,
3185 uint64_t *ret_val,
3186 uint64_t new_val, uint64_t wr_mask)
3188 RISCVException ret;
3189 uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS;
3190 uint64_t vsbits;
3192 /* Add virtualized bits into vsip mask. */
3193 mask |= env->hvien & ~env->hideleg;
3195 /* Bring VS-level bits to correct position */
3196 vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
3197 new_val &= ~(VS_MODE_INTERRUPTS >> 1);
3198 new_val |= vsbits << 1;
3199 vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
3200 wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
3201 wr_mask |= vsbits << 1;
3203 ret = rmw_hvip64(env, csrno, &rval, new_val,
3204 wr_mask & mask & vsip_writable_mask);
3205 if (ret_val) {
3206 rval &= mask;
3207 vsbits = rval & VS_MODE_INTERRUPTS;
3208 rval &= ~VS_MODE_INTERRUPTS;
3209 *ret_val = rval | (vsbits >> 1);
3212 return ret;
3215 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
3216 target_ulong *ret_val,
3217 target_ulong new_val, target_ulong wr_mask)
3219 uint64_t rval;
3220 RISCVException ret;
3222 ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask);
3223 if (ret_val) {
3224 *ret_val = rval;
3227 return ret;
3230 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno,
3231 target_ulong *ret_val,
3232 target_ulong new_val, target_ulong wr_mask)
3234 uint64_t rval;
3235 RISCVException ret;
3237 ret = rmw_vsip64(env, csrno, &rval,
3238 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3239 if (ret_val) {
3240 *ret_val = rval >> 32;
3243 return ret;
3246 static RISCVException rmw_sip64(CPURISCVState *env, int csrno,
3247 uint64_t *ret_val,
3248 uint64_t new_val, uint64_t wr_mask)
3250 RISCVException ret;
3251 uint64_t mask = (env->mideleg | env->mvien) & sip_writable_mask;
3253 if (env->virt_enabled) {
3254 if (env->hvictl & HVICTL_VTI) {
3255 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
3257 ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask);
3258 } else {
3259 ret = rmw_mvip64(env, csrno, ret_val, new_val, wr_mask & mask);
3262 if (ret_val) {
3263 *ret_val &= (env->mideleg | env->mvien) &
3264 (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS);
3267 return ret;
3270 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
3271 target_ulong *ret_val,
3272 target_ulong new_val, target_ulong wr_mask)
3274 uint64_t rval;
3275 RISCVException ret;
3277 ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask);
3278 if (ret_val) {
3279 *ret_val = rval;
3282 return ret;
3285 static RISCVException rmw_siph(CPURISCVState *env, int csrno,
3286 target_ulong *ret_val,
3287 target_ulong new_val, target_ulong wr_mask)
3289 uint64_t rval;
3290 RISCVException ret;
3292 ret = rmw_sip64(env, csrno, &rval,
3293 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3294 if (ret_val) {
3295 *ret_val = rval >> 32;
3298 return ret;
3301 /* Supervisor Protection and Translation */
3302 static RISCVException read_satp(CPURISCVState *env, int csrno,
3303 target_ulong *val)
3305 if (!riscv_cpu_cfg(env)->mmu) {
3306 *val = 0;
3307 return RISCV_EXCP_NONE;
3309 *val = env->satp;
3310 return RISCV_EXCP_NONE;
3313 static RISCVException write_satp(CPURISCVState *env, int csrno,
3314 target_ulong val)
3316 if (!riscv_cpu_cfg(env)->mmu) {
3317 return RISCV_EXCP_NONE;
3320 env->satp = legalize_xatp(env, env->satp, val);
3321 return RISCV_EXCP_NONE;
3324 static RISCVException read_vstopi(CPURISCVState *env, int csrno,
3325 target_ulong *val)
3327 int irq, ret;
3328 target_ulong topei;
3329 uint64_t vseip, vsgein;
3330 uint32_t iid, iprio, hviid, hviprio, gein;
3331 uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS];
3333 gein = get_field(env->hstatus, HSTATUS_VGEIN);
3334 hviid = get_field(env->hvictl, HVICTL_IID);
3335 hviprio = get_field(env->hvictl, HVICTL_IPRIO);
3337 if (gein) {
3338 vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0;
3339 vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP;
3340 if (gein <= env->geilen && vseip) {
3341 siid[scount] = IRQ_S_EXT;
3342 siprio[scount] = IPRIO_MMAXIPRIO + 1;
3343 if (env->aia_ireg_rmw_fn[PRV_S]) {
3345 * Call machine specific IMSIC register emulation for
3346 * reading TOPEI.
3348 ret = env->aia_ireg_rmw_fn[PRV_S](
3349 env->aia_ireg_rmw_fn_arg[PRV_S],
3350 AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein,
3351 riscv_cpu_mxl_bits(env)),
3352 &topei, 0, 0);
3353 if (!ret && topei) {
3354 siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK;
3357 scount++;
3359 } else {
3360 if (hviid == IRQ_S_EXT && hviprio) {
3361 siid[scount] = IRQ_S_EXT;
3362 siprio[scount] = hviprio;
3363 scount++;
3367 if (env->hvictl & HVICTL_VTI) {
3368 if (hviid != IRQ_S_EXT) {
3369 siid[scount] = hviid;
3370 siprio[scount] = hviprio;
3371 scount++;
3373 } else {
3374 irq = riscv_cpu_vsirq_pending(env);
3375 if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) {
3376 siid[scount] = irq;
3377 siprio[scount] = env->hviprio[irq];
3378 scount++;
3382 iid = 0;
3383 iprio = UINT_MAX;
3384 for (s = 0; s < scount; s++) {
3385 if (siprio[s] < iprio) {
3386 iid = siid[s];
3387 iprio = siprio[s];
3391 if (iid) {
3392 if (env->hvictl & HVICTL_IPRIOM) {
3393 if (iprio > IPRIO_MMAXIPRIO) {
3394 iprio = IPRIO_MMAXIPRIO;
3396 if (!iprio) {
3397 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) {
3398 iprio = IPRIO_MMAXIPRIO;
3401 } else {
3402 iprio = 1;
3404 } else {
3405 iprio = 0;
3408 *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT;
3409 *val |= iprio;
3411 return RISCV_EXCP_NONE;
3414 static RISCVException read_stopi(CPURISCVState *env, int csrno,
3415 target_ulong *val)
3417 int irq;
3418 uint8_t iprio;
3420 if (env->virt_enabled) {
3421 return read_vstopi(env, CSR_VSTOPI, val);
3424 irq = riscv_cpu_sirq_pending(env);
3425 if (irq <= 0 || irq > 63) {
3426 *val = 0;
3427 } else {
3428 iprio = env->siprio[irq];
3429 if (!iprio) {
3430 if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) {
3431 iprio = IPRIO_MMAXIPRIO;
3434 *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
3435 *val |= iprio;
3438 return RISCV_EXCP_NONE;
3441 /* Hypervisor Extensions */
3442 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
3443 target_ulong *val)
3445 *val = env->hstatus;
3446 if (riscv_cpu_mxl(env) != MXL_RV32) {
3447 /* We only support 64-bit VSXL */
3448 *val = set_field(*val, HSTATUS_VSXL, 2);
3450 /* We only support little endian */
3451 *val = set_field(*val, HSTATUS_VSBE, 0);
3452 return RISCV_EXCP_NONE;
3455 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
3456 target_ulong val)
3458 env->hstatus = val;
3459 if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
3460 qemu_log_mask(LOG_UNIMP,
3461 "QEMU does not support mixed HSXLEN options.");
3463 if (get_field(val, HSTATUS_VSBE) != 0) {
3464 qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
3466 return RISCV_EXCP_NONE;
3469 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
3470 target_ulong *val)
3472 *val = env->hedeleg;
3473 return RISCV_EXCP_NONE;
3476 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
3477 target_ulong val)
3479 env->hedeleg = val & vs_delegable_excps;
3480 return RISCV_EXCP_NONE;
3483 static RISCVException read_hedelegh(CPURISCVState *env, int csrno,
3484 target_ulong *val)
3486 RISCVException ret;
3487 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_P1P13);
3488 if (ret != RISCV_EXCP_NONE) {
3489 return ret;
3492 /* Reserved, now read zero */
3493 *val = 0;
3494 return RISCV_EXCP_NONE;
3497 static RISCVException write_hedelegh(CPURISCVState *env, int csrno,
3498 target_ulong val)
3500 RISCVException ret;
3501 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_P1P13);
3502 if (ret != RISCV_EXCP_NONE) {
3503 return ret;
3506 /* Reserved, now write ignore */
3507 return RISCV_EXCP_NONE;
3510 static RISCVException rmw_hvien64(CPURISCVState *env, int csrno,
3511 uint64_t *ret_val,
3512 uint64_t new_val, uint64_t wr_mask)
3514 uint64_t mask = wr_mask & hvien_writable_mask;
3516 if (ret_val) {
3517 *ret_val = env->hvien;
3520 env->hvien = (env->hvien & ~mask) | (new_val & mask);
3522 return RISCV_EXCP_NONE;
3525 static RISCVException rmw_hvien(CPURISCVState *env, int csrno,
3526 target_ulong *ret_val,
3527 target_ulong new_val, target_ulong wr_mask)
3529 uint64_t rval;
3530 RISCVException ret;
3532 ret = rmw_hvien64(env, csrno, &rval, new_val, wr_mask);
3533 if (ret_val) {
3534 *ret_val = rval;
3537 return ret;
3540 static RISCVException rmw_hvienh(CPURISCVState *env, int csrno,
3541 target_ulong *ret_val,
3542 target_ulong new_val, target_ulong wr_mask)
3544 uint64_t rval;
3545 RISCVException ret;
3547 ret = rmw_hvien64(env, csrno, &rval,
3548 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3549 if (ret_val) {
3550 *ret_val = rval >> 32;
3553 return ret;
3556 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno,
3557 uint64_t *ret_val,
3558 uint64_t new_val, uint64_t wr_mask)
3560 uint64_t mask = wr_mask & vs_delegable_ints;
3562 if (ret_val) {
3563 *ret_val = env->hideleg & vs_delegable_ints;
3566 env->hideleg = (env->hideleg & ~mask) | (new_val & mask);
3567 return RISCV_EXCP_NONE;
3570 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno,
3571 target_ulong *ret_val,
3572 target_ulong new_val, target_ulong wr_mask)
3574 uint64_t rval;
3575 RISCVException ret;
3577 ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask);
3578 if (ret_val) {
3579 *ret_val = rval;
3582 return ret;
3585 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno,
3586 target_ulong *ret_val,
3587 target_ulong new_val, target_ulong wr_mask)
3589 uint64_t rval;
3590 RISCVException ret;
3592 ret = rmw_hideleg64(env, csrno, &rval,
3593 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3594 if (ret_val) {
3595 *ret_val = rval >> 32;
3598 return ret;
3602 * The function is written for two use-cases:
3603 * 1- To access hvip csr as is for HS-mode access.
3604 * 2- To access vsip as a combination of hvip, and mip for vs-mode.
3606 * Both report bits 2, 6, 10 and 13:63.
3607 * vsip needs to be read-only zero when both hideleg[i] and
3608 * hvien[i] are zero.
3610 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
3611 uint64_t *ret_val,
3612 uint64_t new_val, uint64_t wr_mask)
3614 RISCVException ret;
3615 uint64_t old_hvip;
3616 uint64_t ret_mip;
3619 * For bits 10, 6 and 2, vsip[i] is an alias of hip[i]. These bits are
3620 * present in hip, hvip and mip. Where mip[i] is alias of hip[i] and hvip[i]
3621 * is OR'ed in hip[i] to inject virtual interrupts from hypervisor. These
3622 * bits are actually being maintained in mip so we read them from there.
3623 * This way we have a single source of truth and allows for easier
3624 * implementation.
3626 * For bits 13:63 we have:
3628 * hideleg[i] hvien[i]
3629 * 0 0 No delegation. vsip[i] readonly zero.
3630 * 0 1 vsip[i] is alias of hvip[i], sip bypassed.
3631 * 1 X vsip[i] is alias of sip[i], hvip bypassed.
3633 * alias_mask denotes the bits that come from sip (mip here given we
3634 * maintain all bits there). nalias_mask denotes bits that come from
3635 * hvip.
3637 uint64_t alias_mask = (env->hideleg | ~env->hvien) | VS_MODE_INTERRUPTS;
3638 uint64_t nalias_mask = (~env->hideleg & env->hvien);
3639 uint64_t wr_mask_hvip;
3640 uint64_t wr_mask_mip;
3643 * Both alias and non-alias mask remain same for vsip except:
3644 * 1- For VS* bits if they are zero in hideleg.
3645 * 2- For 13:63 bits if they are zero in both hideleg and hvien.
3647 if (csrno == CSR_VSIP) {
3648 /* zero-out VS* bits that are not delegated to VS mode. */
3649 alias_mask &= (env->hideleg | ~VS_MODE_INTERRUPTS);
3652 * zero-out 13:63 bits that are zero in both hideleg and hvien.
3653 * nalias_mask mask can not contain any VS* bits so only second
3654 * condition applies on it.
3656 nalias_mask &= (env->hideleg | env->hvien);
3657 alias_mask &= (env->hideleg | env->hvien);
3660 wr_mask_hvip = wr_mask & nalias_mask & hvip_writable_mask;
3661 wr_mask_mip = wr_mask & alias_mask & hvip_writable_mask;
3663 /* Aliased bits, bits 10, 6, 2 need to come from mip. */
3664 ret = rmw_mip64(env, csrno, &ret_mip, new_val, wr_mask_mip);
3665 if (ret != RISCV_EXCP_NONE) {
3666 return ret;
3669 old_hvip = env->hvip;
3671 if (wr_mask_hvip) {
3672 env->hvip = (env->hvip & ~wr_mask_hvip) | (new_val & wr_mask_hvip);
3675 * Given hvip is separate source from mip, we need to trigger interrupt
3676 * from here separately. Normally this happen from riscv_cpu_update_mip.
3678 riscv_cpu_interrupt(env);
3681 if (ret_val) {
3682 /* Only take VS* bits from mip. */
3683 ret_mip &= alias_mask;
3685 /* Take in non-delegated 13:63 bits from hvip. */
3686 old_hvip &= nalias_mask;
3688 *ret_val = ret_mip | old_hvip;
3691 return ret;
3694 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
3695 target_ulong *ret_val,
3696 target_ulong new_val, target_ulong wr_mask)
3698 uint64_t rval;
3699 RISCVException ret;
3701 ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask);
3702 if (ret_val) {
3703 *ret_val = rval;
3706 return ret;
3709 static RISCVException rmw_hviph(CPURISCVState *env, int csrno,
3710 target_ulong *ret_val,
3711 target_ulong new_val, target_ulong wr_mask)
3713 uint64_t rval;
3714 RISCVException ret;
3716 ret = rmw_hvip64(env, csrno, &rval,
3717 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3718 if (ret_val) {
3719 *ret_val = rval >> 32;
3722 return ret;
3725 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
3726 target_ulong *ret_value,
3727 target_ulong new_value, target_ulong write_mask)
3729 int ret = rmw_mip(env, csrno, ret_value, new_value,
3730 write_mask & hip_writable_mask);
3732 if (ret_value) {
3733 *ret_value &= HS_MODE_INTERRUPTS;
3735 return ret;
3738 static RISCVException rmw_hie(CPURISCVState *env, int csrno,
3739 target_ulong *ret_val,
3740 target_ulong new_val, target_ulong wr_mask)
3742 uint64_t rval;
3743 RISCVException ret;
3745 ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS);
3746 if (ret_val) {
3747 *ret_val = rval & HS_MODE_INTERRUPTS;
3750 return ret;
3753 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
3754 target_ulong *val)
3756 *val = env->hcounteren;
3757 return RISCV_EXCP_NONE;
3760 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
3761 target_ulong val)
3763 RISCVCPU *cpu = env_archcpu(env);
3765 /* WARL register - disable unavailable counters */
3766 env->hcounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM |
3767 COUNTEREN_IR);
3768 return RISCV_EXCP_NONE;
3771 static RISCVException read_hgeie(CPURISCVState *env, int csrno,
3772 target_ulong *val)
3774 if (val) {
3775 *val = env->hgeie;
3777 return RISCV_EXCP_NONE;
3780 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
3781 target_ulong val)
3783 /* Only GEILEN:1 bits implemented and BIT0 is never implemented */
3784 val &= ((((target_ulong)1) << env->geilen) - 1) << 1;
3785 env->hgeie = val;
3786 /* Update mip.SGEIP bit */
3787 riscv_cpu_update_mip(env, MIP_SGEIP,
3788 BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
3789 return RISCV_EXCP_NONE;
3792 static RISCVException read_htval(CPURISCVState *env, int csrno,
3793 target_ulong *val)
3795 *val = env->htval;
3796 return RISCV_EXCP_NONE;
3799 static RISCVException write_htval(CPURISCVState *env, int csrno,
3800 target_ulong val)
3802 env->htval = val;
3803 return RISCV_EXCP_NONE;
3806 static RISCVException read_htinst(CPURISCVState *env, int csrno,
3807 target_ulong *val)
3809 *val = env->htinst;
3810 return RISCV_EXCP_NONE;
3813 static RISCVException write_htinst(CPURISCVState *env, int csrno,
3814 target_ulong val)
3816 return RISCV_EXCP_NONE;
3819 static RISCVException read_hgeip(CPURISCVState *env, int csrno,
3820 target_ulong *val)
3822 if (val) {
3823 *val = env->hgeip;
3825 return RISCV_EXCP_NONE;
3828 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
3829 target_ulong *val)
3831 *val = env->hgatp;
3832 return RISCV_EXCP_NONE;
3835 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
3836 target_ulong val)
3838 env->hgatp = legalize_xatp(env, env->hgatp, val);
3839 return RISCV_EXCP_NONE;
3842 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
3843 target_ulong *val)
3845 if (!env->rdtime_fn) {
3846 return RISCV_EXCP_ILLEGAL_INST;
3849 *val = env->htimedelta;
3850 return RISCV_EXCP_NONE;
3853 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
3854 target_ulong val)
3856 if (!env->rdtime_fn) {
3857 return RISCV_EXCP_ILLEGAL_INST;
3860 if (riscv_cpu_mxl(env) == MXL_RV32) {
3861 env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
3862 } else {
3863 env->htimedelta = val;
3866 if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3867 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3868 env->htimedelta, MIP_VSTIP);
3871 return RISCV_EXCP_NONE;
3874 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
3875 target_ulong *val)
3877 if (!env->rdtime_fn) {
3878 return RISCV_EXCP_ILLEGAL_INST;
3881 *val = env->htimedelta >> 32;
3882 return RISCV_EXCP_NONE;
3885 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
3886 target_ulong val)
3888 if (!env->rdtime_fn) {
3889 return RISCV_EXCP_ILLEGAL_INST;
3892 env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
3894 if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3895 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3896 env->htimedelta, MIP_VSTIP);
3899 return RISCV_EXCP_NONE;
3902 static RISCVException read_hvictl(CPURISCVState *env, int csrno,
3903 target_ulong *val)
3905 *val = env->hvictl;
3906 return RISCV_EXCP_NONE;
3909 static RISCVException write_hvictl(CPURISCVState *env, int csrno,
3910 target_ulong val)
3912 env->hvictl = val & HVICTL_VALID_MASK;
3913 return RISCV_EXCP_NONE;
3916 static RISCVException read_hvipriox(CPURISCVState *env, int first_index,
3917 uint8_t *iprio, target_ulong *val)
3919 int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3921 /* First index has to be a multiple of number of irqs per register */
3922 if (first_index % num_irqs) {
3923 return (env->virt_enabled) ?
3924 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3927 /* Fill-up return value */
3928 *val = 0;
3929 for (i = 0; i < num_irqs; i++) {
3930 if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3931 continue;
3933 if (rdzero) {
3934 continue;
3936 *val |= ((target_ulong)iprio[irq]) << (i * 8);
3939 return RISCV_EXCP_NONE;
3942 static RISCVException write_hvipriox(CPURISCVState *env, int first_index,
3943 uint8_t *iprio, target_ulong val)
3945 int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3947 /* First index has to be a multiple of number of irqs per register */
3948 if (first_index % num_irqs) {
3949 return (env->virt_enabled) ?
3950 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3953 /* Fill-up priority array */
3954 for (i = 0; i < num_irqs; i++) {
3955 if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3956 continue;
3958 if (rdzero) {
3959 iprio[irq] = 0;
3960 } else {
3961 iprio[irq] = (val >> (i * 8)) & 0xff;
3965 return RISCV_EXCP_NONE;
3968 static RISCVException read_hviprio1(CPURISCVState *env, int csrno,
3969 target_ulong *val)
3971 return read_hvipriox(env, 0, env->hviprio, val);
3974 static RISCVException write_hviprio1(CPURISCVState *env, int csrno,
3975 target_ulong val)
3977 return write_hvipriox(env, 0, env->hviprio, val);
3980 static RISCVException read_hviprio1h(CPURISCVState *env, int csrno,
3981 target_ulong *val)
3983 return read_hvipriox(env, 4, env->hviprio, val);
3986 static RISCVException write_hviprio1h(CPURISCVState *env, int csrno,
3987 target_ulong val)
3989 return write_hvipriox(env, 4, env->hviprio, val);
3992 static RISCVException read_hviprio2(CPURISCVState *env, int csrno,
3993 target_ulong *val)
3995 return read_hvipriox(env, 8, env->hviprio, val);
3998 static RISCVException write_hviprio2(CPURISCVState *env, int csrno,
3999 target_ulong val)
4001 return write_hvipriox(env, 8, env->hviprio, val);
4004 static RISCVException read_hviprio2h(CPURISCVState *env, int csrno,
4005 target_ulong *val)
4007 return read_hvipriox(env, 12, env->hviprio, val);
4010 static RISCVException write_hviprio2h(CPURISCVState *env, int csrno,
4011 target_ulong val)
4013 return write_hvipriox(env, 12, env->hviprio, val);
4016 /* Virtual CSR Registers */
4017 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
4018 target_ulong *val)
4020 *val = env->vsstatus;
4021 return RISCV_EXCP_NONE;
4024 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
4025 target_ulong val)
4027 uint64_t mask = (target_ulong)-1;
4028 if ((val & VSSTATUS64_UXL) == 0) {
4029 mask &= ~VSSTATUS64_UXL;
4031 env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
4032 return RISCV_EXCP_NONE;
4035 static RISCVException read_vstvec(CPURISCVState *env, int csrno,
4036 target_ulong *val)
4038 *val = env->vstvec;
4039 return RISCV_EXCP_NONE;
4042 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
4043 target_ulong val)
4045 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
4046 if ((val & 3) < 2) {
4047 env->vstvec = val;
4048 } else {
4049 qemu_log_mask(LOG_UNIMP, "CSR_VSTVEC: reserved mode not supported\n");
4051 return RISCV_EXCP_NONE;
4054 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
4055 target_ulong *val)
4057 *val = env->vsscratch;
4058 return RISCV_EXCP_NONE;
4061 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
4062 target_ulong val)
4064 env->vsscratch = val;
4065 return RISCV_EXCP_NONE;
4068 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
4069 target_ulong *val)
4071 *val = env->vsepc;
4072 return RISCV_EXCP_NONE;
4075 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
4076 target_ulong val)
4078 env->vsepc = val;
4079 return RISCV_EXCP_NONE;
4082 static RISCVException read_vscause(CPURISCVState *env, int csrno,
4083 target_ulong *val)
4085 *val = env->vscause;
4086 return RISCV_EXCP_NONE;
4089 static RISCVException write_vscause(CPURISCVState *env, int csrno,
4090 target_ulong val)
4092 env->vscause = val;
4093 return RISCV_EXCP_NONE;
4096 static RISCVException read_vstval(CPURISCVState *env, int csrno,
4097 target_ulong *val)
4099 *val = env->vstval;
4100 return RISCV_EXCP_NONE;
4103 static RISCVException write_vstval(CPURISCVState *env, int csrno,
4104 target_ulong val)
4106 env->vstval = val;
4107 return RISCV_EXCP_NONE;
4110 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
4111 target_ulong *val)
4113 *val = env->vsatp;
4114 return RISCV_EXCP_NONE;
4117 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
4118 target_ulong val)
4120 env->vsatp = legalize_xatp(env, env->vsatp, val);
4121 return RISCV_EXCP_NONE;
4124 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
4125 target_ulong *val)
4127 *val = env->mtval2;
4128 return RISCV_EXCP_NONE;
4131 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
4132 target_ulong val)
4134 env->mtval2 = val;
4135 return RISCV_EXCP_NONE;
4138 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
4139 target_ulong *val)
4141 *val = env->mtinst;
4142 return RISCV_EXCP_NONE;
4145 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
4146 target_ulong val)
4148 env->mtinst = val;
4149 return RISCV_EXCP_NONE;
4152 /* Physical Memory Protection */
4153 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
4154 target_ulong *val)
4156 *val = mseccfg_csr_read(env);
4157 return RISCV_EXCP_NONE;
4160 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
4161 target_ulong val)
4163 mseccfg_csr_write(env, val);
4164 return RISCV_EXCP_NONE;
4167 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
4168 target_ulong *val)
4170 uint32_t reg_index = csrno - CSR_PMPCFG0;
4172 *val = pmpcfg_csr_read(env, reg_index);
4173 return RISCV_EXCP_NONE;
4176 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
4177 target_ulong val)
4179 uint32_t reg_index = csrno - CSR_PMPCFG0;
4181 pmpcfg_csr_write(env, reg_index, val);
4182 return RISCV_EXCP_NONE;
4185 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
4186 target_ulong *val)
4188 *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
4189 return RISCV_EXCP_NONE;
4192 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
4193 target_ulong val)
4195 pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
4196 return RISCV_EXCP_NONE;
4199 static RISCVException read_tselect(CPURISCVState *env, int csrno,
4200 target_ulong *val)
4202 *val = tselect_csr_read(env);
4203 return RISCV_EXCP_NONE;
4206 static RISCVException write_tselect(CPURISCVState *env, int csrno,
4207 target_ulong val)
4209 tselect_csr_write(env, val);
4210 return RISCV_EXCP_NONE;
4213 static RISCVException read_tdata(CPURISCVState *env, int csrno,
4214 target_ulong *val)
4216 /* return 0 in tdata1 to end the trigger enumeration */
4217 if (env->trigger_cur >= RV_MAX_TRIGGERS && csrno == CSR_TDATA1) {
4218 *val = 0;
4219 return RISCV_EXCP_NONE;
4222 if (!tdata_available(env, csrno - CSR_TDATA1)) {
4223 return RISCV_EXCP_ILLEGAL_INST;
4226 *val = tdata_csr_read(env, csrno - CSR_TDATA1);
4227 return RISCV_EXCP_NONE;
4230 static RISCVException write_tdata(CPURISCVState *env, int csrno,
4231 target_ulong val)
4233 if (!tdata_available(env, csrno - CSR_TDATA1)) {
4234 return RISCV_EXCP_ILLEGAL_INST;
4237 tdata_csr_write(env, csrno - CSR_TDATA1, val);
4238 return RISCV_EXCP_NONE;
4241 static RISCVException read_tinfo(CPURISCVState *env, int csrno,
4242 target_ulong *val)
4244 *val = tinfo_csr_read(env);
4245 return RISCV_EXCP_NONE;
4248 static RISCVException read_mcontext(CPURISCVState *env, int csrno,
4249 target_ulong *val)
4251 *val = env->mcontext;
4252 return RISCV_EXCP_NONE;
4255 static RISCVException write_mcontext(CPURISCVState *env, int csrno,
4256 target_ulong val)
4258 bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false;
4259 int32_t mask;
4261 if (riscv_has_ext(env, RVH)) {
4262 /* Spec suggest 7-bit for RV32 and 14-bit for RV64 w/ H extension */
4263 mask = rv32 ? MCONTEXT32_HCONTEXT : MCONTEXT64_HCONTEXT;
4264 } else {
4265 /* Spec suggest 6-bit for RV32 and 13-bit for RV64 w/o H extension */
4266 mask = rv32 ? MCONTEXT32 : MCONTEXT64;
4269 env->mcontext = val & mask;
4270 return RISCV_EXCP_NONE;
4274 * Functions to access Pointer Masking feature registers
4275 * We have to check if current priv lvl could modify
4276 * csr in given mode
4278 static bool check_pm_current_disabled(CPURISCVState *env, int csrno)
4280 int csr_priv = get_field(csrno, 0x300);
4281 int pm_current;
4283 if (env->debugger) {
4284 return false;
4287 * If priv lvls differ that means we're accessing csr from higher priv lvl,
4288 * so allow the access
4290 if (env->priv != csr_priv) {
4291 return false;
4293 switch (env->priv) {
4294 case PRV_M:
4295 pm_current = get_field(env->mmte, M_PM_CURRENT);
4296 break;
4297 case PRV_S:
4298 pm_current = get_field(env->mmte, S_PM_CURRENT);
4299 break;
4300 case PRV_U:
4301 pm_current = get_field(env->mmte, U_PM_CURRENT);
4302 break;
4303 default:
4304 g_assert_not_reached();
4306 /* It's same priv lvl, so we allow to modify csr only if pm.current==1 */
4307 return !pm_current;
4310 static RISCVException read_mmte(CPURISCVState *env, int csrno,
4311 target_ulong *val)
4313 *val = env->mmte & MMTE_MASK;
4314 return RISCV_EXCP_NONE;
4317 static RISCVException write_mmte(CPURISCVState *env, int csrno,
4318 target_ulong val)
4320 uint64_t mstatus;
4321 target_ulong wpri_val = val & MMTE_MASK;
4323 if (val != wpri_val) {
4324 qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
4325 TARGET_FMT_lx "\n", "MMTE: WPRI violation written 0x",
4326 val, "vs expected 0x", wpri_val);
4328 /* for machine mode pm.current is hardwired to 1 */
4329 wpri_val |= MMTE_M_PM_CURRENT;
4331 /* hardwiring pm.instruction bit to 0, since it's not supported yet */
4332 wpri_val &= ~(MMTE_M_PM_INSN | MMTE_S_PM_INSN | MMTE_U_PM_INSN);
4333 env->mmte = wpri_val | EXT_STATUS_DIRTY;
4334 riscv_cpu_update_mask(env);
4336 /* Set XS and SD bits, since PM CSRs are dirty */
4337 mstatus = env->mstatus | MSTATUS_XS;
4338 write_mstatus(env, csrno, mstatus);
4339 return RISCV_EXCP_NONE;
4342 static RISCVException read_smte(CPURISCVState *env, int csrno,
4343 target_ulong *val)
4345 *val = env->mmte & SMTE_MASK;
4346 return RISCV_EXCP_NONE;
4349 static RISCVException write_smte(CPURISCVState *env, int csrno,
4350 target_ulong val)
4352 target_ulong wpri_val = val & SMTE_MASK;
4354 if (val != wpri_val) {
4355 qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
4356 TARGET_FMT_lx "\n", "SMTE: WPRI violation written 0x",
4357 val, "vs expected 0x", wpri_val);
4360 /* if pm.current==0 we can't modify current PM CSRs */
4361 if (check_pm_current_disabled(env, csrno)) {
4362 return RISCV_EXCP_NONE;
4365 wpri_val |= (env->mmte & ~SMTE_MASK);
4366 write_mmte(env, csrno, wpri_val);
4367 return RISCV_EXCP_NONE;
4370 static RISCVException read_umte(CPURISCVState *env, int csrno,
4371 target_ulong *val)
4373 *val = env->mmte & UMTE_MASK;
4374 return RISCV_EXCP_NONE;
4377 static RISCVException write_umte(CPURISCVState *env, int csrno,
4378 target_ulong val)
4380 target_ulong wpri_val = val & UMTE_MASK;
4382 if (val != wpri_val) {
4383 qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
4384 TARGET_FMT_lx "\n", "UMTE: WPRI violation written 0x",
4385 val, "vs expected 0x", wpri_val);
4388 if (check_pm_current_disabled(env, csrno)) {
4389 return RISCV_EXCP_NONE;
4392 wpri_val |= (env->mmte & ~UMTE_MASK);
4393 write_mmte(env, csrno, wpri_val);
4394 return RISCV_EXCP_NONE;
4397 static RISCVException read_mpmmask(CPURISCVState *env, int csrno,
4398 target_ulong *val)
4400 *val = env->mpmmask;
4401 return RISCV_EXCP_NONE;
4404 static RISCVException write_mpmmask(CPURISCVState *env, int csrno,
4405 target_ulong val)
4407 uint64_t mstatus;
4409 env->mpmmask = val;
4410 if ((cpu_address_mode(env) == PRV_M) && (env->mmte & M_PM_ENABLE)) {
4411 env->cur_pmmask = val;
4413 env->mmte |= EXT_STATUS_DIRTY;
4415 /* Set XS and SD bits, since PM CSRs are dirty */
4416 mstatus = env->mstatus | MSTATUS_XS;
4417 write_mstatus(env, csrno, mstatus);
4418 return RISCV_EXCP_NONE;
4421 static RISCVException read_spmmask(CPURISCVState *env, int csrno,
4422 target_ulong *val)
4424 *val = env->spmmask;
4425 return RISCV_EXCP_NONE;
4428 static RISCVException write_spmmask(CPURISCVState *env, int csrno,
4429 target_ulong val)
4431 uint64_t mstatus;
4433 /* if pm.current==0 we can't modify current PM CSRs */
4434 if (check_pm_current_disabled(env, csrno)) {
4435 return RISCV_EXCP_NONE;
4437 env->spmmask = val;
4438 if ((cpu_address_mode(env) == PRV_S) && (env->mmte & S_PM_ENABLE)) {
4439 env->cur_pmmask = val;
4440 if (cpu_get_xl(env, PRV_S) == MXL_RV32) {
4441 env->cur_pmmask &= UINT32_MAX;
4444 env->mmte |= EXT_STATUS_DIRTY;
4446 /* Set XS and SD bits, since PM CSRs are dirty */
4447 mstatus = env->mstatus | MSTATUS_XS;
4448 write_mstatus(env, csrno, mstatus);
4449 return RISCV_EXCP_NONE;
4452 static RISCVException read_upmmask(CPURISCVState *env, int csrno,
4453 target_ulong *val)
4455 *val = env->upmmask;
4456 return RISCV_EXCP_NONE;
4459 static RISCVException write_upmmask(CPURISCVState *env, int csrno,
4460 target_ulong val)
4462 uint64_t mstatus;
4464 /* if pm.current==0 we can't modify current PM CSRs */
4465 if (check_pm_current_disabled(env, csrno)) {
4466 return RISCV_EXCP_NONE;
4468 env->upmmask = val;
4469 if ((cpu_address_mode(env) == PRV_U) && (env->mmte & U_PM_ENABLE)) {
4470 env->cur_pmmask = val;
4471 if (cpu_get_xl(env, PRV_U) == MXL_RV32) {
4472 env->cur_pmmask &= UINT32_MAX;
4475 env->mmte |= EXT_STATUS_DIRTY;
4477 /* Set XS and SD bits, since PM CSRs are dirty */
4478 mstatus = env->mstatus | MSTATUS_XS;
4479 write_mstatus(env, csrno, mstatus);
4480 return RISCV_EXCP_NONE;
4483 static RISCVException read_mpmbase(CPURISCVState *env, int csrno,
4484 target_ulong *val)
4486 *val = env->mpmbase;
4487 return RISCV_EXCP_NONE;
4490 static RISCVException write_mpmbase(CPURISCVState *env, int csrno,
4491 target_ulong val)
4493 uint64_t mstatus;
4495 env->mpmbase = val;
4496 if ((cpu_address_mode(env) == PRV_M) && (env->mmte & M_PM_ENABLE)) {
4497 env->cur_pmbase = val;
4499 env->mmte |= EXT_STATUS_DIRTY;
4501 /* Set XS and SD bits, since PM CSRs are dirty */
4502 mstatus = env->mstatus | MSTATUS_XS;
4503 write_mstatus(env, csrno, mstatus);
4504 return RISCV_EXCP_NONE;
4507 static RISCVException read_spmbase(CPURISCVState *env, int csrno,
4508 target_ulong *val)
4510 *val = env->spmbase;
4511 return RISCV_EXCP_NONE;
4514 static RISCVException write_spmbase(CPURISCVState *env, int csrno,
4515 target_ulong val)
4517 uint64_t mstatus;
4519 /* if pm.current==0 we can't modify current PM CSRs */
4520 if (check_pm_current_disabled(env, csrno)) {
4521 return RISCV_EXCP_NONE;
4523 env->spmbase = val;
4524 if ((cpu_address_mode(env) == PRV_S) && (env->mmte & S_PM_ENABLE)) {
4525 env->cur_pmbase = val;
4526 if (cpu_get_xl(env, PRV_S) == MXL_RV32) {
4527 env->cur_pmbase &= UINT32_MAX;
4530 env->mmte |= EXT_STATUS_DIRTY;
4532 /* Set XS and SD bits, since PM CSRs are dirty */
4533 mstatus = env->mstatus | MSTATUS_XS;
4534 write_mstatus(env, csrno, mstatus);
4535 return RISCV_EXCP_NONE;
4538 static RISCVException read_upmbase(CPURISCVState *env, int csrno,
4539 target_ulong *val)
4541 *val = env->upmbase;
4542 return RISCV_EXCP_NONE;
4545 static RISCVException write_upmbase(CPURISCVState *env, int csrno,
4546 target_ulong val)
4548 uint64_t mstatus;
4550 /* if pm.current==0 we can't modify current PM CSRs */
4551 if (check_pm_current_disabled(env, csrno)) {
4552 return RISCV_EXCP_NONE;
4554 env->upmbase = val;
4555 if ((cpu_address_mode(env) == PRV_U) && (env->mmte & U_PM_ENABLE)) {
4556 env->cur_pmbase = val;
4557 if (cpu_get_xl(env, PRV_U) == MXL_RV32) {
4558 env->cur_pmbase &= UINT32_MAX;
4561 env->mmte |= EXT_STATUS_DIRTY;
4563 /* Set XS and SD bits, since PM CSRs are dirty */
4564 mstatus = env->mstatus | MSTATUS_XS;
4565 write_mstatus(env, csrno, mstatus);
4566 return RISCV_EXCP_NONE;
4569 #endif
4571 /* Crypto Extension */
4572 target_ulong riscv_new_csr_seed(target_ulong new_value,
4573 target_ulong write_mask)
4575 uint16_t random_v;
4576 Error *random_e = NULL;
4577 int random_r;
4578 target_ulong rval;
4580 random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
4581 if (unlikely(random_r < 0)) {
4583 * Failed, for unknown reasons in the crypto subsystem.
4584 * The best we can do is log the reason and return a
4585 * failure indication to the guest. There is no reason
4586 * we know to expect the failure to be transitory, so
4587 * indicate DEAD to avoid having the guest spin on WAIT.
4589 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
4590 __func__, error_get_pretty(random_e));
4591 error_free(random_e);
4592 rval = SEED_OPST_DEAD;
4593 } else {
4594 rval = random_v | SEED_OPST_ES16;
4597 return rval;
4600 static RISCVException rmw_seed(CPURISCVState *env, int csrno,
4601 target_ulong *ret_value,
4602 target_ulong new_value,
4603 target_ulong write_mask)
4605 target_ulong rval;
4607 rval = riscv_new_csr_seed(new_value, write_mask);
4609 if (ret_value) {
4610 *ret_value = rval;
4613 return RISCV_EXCP_NONE;
4617 * riscv_csrrw - read and/or update control and status register
4619 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0);
4620 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1);
4621 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value);
4622 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value);
4625 static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
4626 int csrno,
4627 bool write)
4629 /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
4630 bool read_only = get_field(csrno, 0xC00) == 3;
4631 int csr_min_priv = csr_ops[csrno].min_priv_ver;
4633 /* ensure the CSR extension is enabled */
4634 if (!riscv_cpu_cfg(env)->ext_zicsr) {
4635 return RISCV_EXCP_ILLEGAL_INST;
4638 /* ensure CSR is implemented by checking predicate */
4639 if (!csr_ops[csrno].predicate) {
4640 return RISCV_EXCP_ILLEGAL_INST;
4643 /* privileged spec version check */
4644 if (env->priv_ver < csr_min_priv) {
4645 return RISCV_EXCP_ILLEGAL_INST;
4648 /* read / write check */
4649 if (write && read_only) {
4650 return RISCV_EXCP_ILLEGAL_INST;
4654 * The predicate() not only does existence check but also does some
4655 * access control check which triggers for example virtual instruction
4656 * exception in some cases. When writing read-only CSRs in those cases
4657 * illegal instruction exception should be triggered instead of virtual
4658 * instruction exception. Hence this comes after the read / write check.
4660 RISCVException ret = csr_ops[csrno].predicate(env, csrno);
4661 if (ret != RISCV_EXCP_NONE) {
4662 return ret;
4665 #if !defined(CONFIG_USER_ONLY)
4666 int csr_priv, effective_priv = env->priv;
4668 if (riscv_has_ext(env, RVH) && env->priv == PRV_S &&
4669 !env->virt_enabled) {
4671 * We are in HS mode. Add 1 to the effective privilege level to
4672 * allow us to access the Hypervisor CSRs.
4674 effective_priv++;
4677 csr_priv = get_field(csrno, 0x300);
4678 if (!env->debugger && (effective_priv < csr_priv)) {
4679 if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
4680 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
4682 return RISCV_EXCP_ILLEGAL_INST;
4684 #endif
4685 return RISCV_EXCP_NONE;
4688 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,
4689 target_ulong *ret_value,
4690 target_ulong new_value,
4691 target_ulong write_mask)
4693 RISCVException ret;
4694 target_ulong old_value = 0;
4696 /* execute combined read/write operation if it exists */
4697 if (csr_ops[csrno].op) {
4698 return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
4702 * ret_value == NULL means that rd=x0 and we're coming from helper_csrw()
4703 * and we can't throw side effects caused by CSR reads.
4705 if (ret_value) {
4706 /* if no accessor exists then return failure */
4707 if (!csr_ops[csrno].read) {
4708 return RISCV_EXCP_ILLEGAL_INST;
4710 /* read old value */
4711 ret = csr_ops[csrno].read(env, csrno, &old_value);
4712 if (ret != RISCV_EXCP_NONE) {
4713 return ret;
4717 /* write value if writable and write mask set, otherwise drop writes */
4718 if (write_mask) {
4719 new_value = (old_value & ~write_mask) | (new_value & write_mask);
4720 if (csr_ops[csrno].write) {
4721 ret = csr_ops[csrno].write(env, csrno, new_value);
4722 if (ret != RISCV_EXCP_NONE) {
4723 return ret;
4728 /* return old value */
4729 if (ret_value) {
4730 *ret_value = old_value;
4733 return RISCV_EXCP_NONE;
4736 RISCVException riscv_csrr(CPURISCVState *env, int csrno,
4737 target_ulong *ret_value)
4739 RISCVException ret = riscv_csrrw_check(env, csrno, false);
4740 if (ret != RISCV_EXCP_NONE) {
4741 return ret;
4744 return riscv_csrrw_do64(env, csrno, ret_value, 0, 0);
4747 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
4748 target_ulong *ret_value,
4749 target_ulong new_value, target_ulong write_mask)
4751 RISCVException ret = riscv_csrrw_check(env, csrno, true);
4752 if (ret != RISCV_EXCP_NONE) {
4753 return ret;
4756 return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask);
4759 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,
4760 Int128 *ret_value,
4761 Int128 new_value,
4762 Int128 write_mask)
4764 RISCVException ret;
4765 Int128 old_value;
4767 /* read old value */
4768 ret = csr_ops[csrno].read128(env, csrno, &old_value);
4769 if (ret != RISCV_EXCP_NONE) {
4770 return ret;
4773 /* write value if writable and write mask set, otherwise drop writes */
4774 if (int128_nz(write_mask)) {
4775 new_value = int128_or(int128_and(old_value, int128_not(write_mask)),
4776 int128_and(new_value, write_mask));
4777 if (csr_ops[csrno].write128) {
4778 ret = csr_ops[csrno].write128(env, csrno, new_value);
4779 if (ret != RISCV_EXCP_NONE) {
4780 return ret;
4782 } else if (csr_ops[csrno].write) {
4783 /* avoids having to write wrappers for all registers */
4784 ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value));
4785 if (ret != RISCV_EXCP_NONE) {
4786 return ret;
4791 /* return old value */
4792 if (ret_value) {
4793 *ret_value = old_value;
4796 return RISCV_EXCP_NONE;
4799 RISCVException riscv_csrr_i128(CPURISCVState *env, int csrno,
4800 Int128 *ret_value)
4802 RISCVException ret;
4804 ret = riscv_csrrw_check(env, csrno, false);
4805 if (ret != RISCV_EXCP_NONE) {
4806 return ret;
4809 if (csr_ops[csrno].read128) {
4810 return riscv_csrrw_do128(env, csrno, ret_value,
4811 int128_zero(), int128_zero());
4815 * Fall back to 64-bit version for now, if the 128-bit alternative isn't
4816 * at all defined.
4817 * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
4818 * significant), for those, this fallback is correctly handling the
4819 * accesses
4821 target_ulong old_value;
4822 ret = riscv_csrrw_do64(env, csrno, &old_value,
4823 (target_ulong)0,
4824 (target_ulong)0);
4825 if (ret == RISCV_EXCP_NONE && ret_value) {
4826 *ret_value = int128_make64(old_value);
4828 return ret;
4831 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
4832 Int128 *ret_value,
4833 Int128 new_value, Int128 write_mask)
4835 RISCVException ret;
4837 ret = riscv_csrrw_check(env, csrno, true);
4838 if (ret != RISCV_EXCP_NONE) {
4839 return ret;
4842 if (csr_ops[csrno].read128) {
4843 return riscv_csrrw_do128(env, csrno, ret_value, new_value, write_mask);
4847 * Fall back to 64-bit version for now, if the 128-bit alternative isn't
4848 * at all defined.
4849 * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
4850 * significant), for those, this fallback is correctly handling the
4851 * accesses
4853 target_ulong old_value;
4854 ret = riscv_csrrw_do64(env, csrno, &old_value,
4855 int128_getlo(new_value),
4856 int128_getlo(write_mask));
4857 if (ret == RISCV_EXCP_NONE && ret_value) {
4858 *ret_value = int128_make64(old_value);
4860 return ret;
4864 * Debugger support. If not in user mode, set env->debugger before the
4865 * riscv_csrrw call and clear it after the call.
4867 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
4868 target_ulong *ret_value,
4869 target_ulong new_value,
4870 target_ulong write_mask)
4872 RISCVException ret;
4873 #if !defined(CONFIG_USER_ONLY)
4874 env->debugger = true;
4875 #endif
4876 if (!write_mask) {
4877 ret = riscv_csrr(env, csrno, ret_value);
4878 } else {
4879 ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
4881 #if !defined(CONFIG_USER_ONLY)
4882 env->debugger = false;
4883 #endif
4884 return ret;
4887 static RISCVException read_jvt(CPURISCVState *env, int csrno,
4888 target_ulong *val)
4890 *val = env->jvt;
4891 return RISCV_EXCP_NONE;
4894 static RISCVException write_jvt(CPURISCVState *env, int csrno,
4895 target_ulong val)
4897 env->jvt = val;
4898 return RISCV_EXCP_NONE;
4902 * Control and Status Register function table
4903 * riscv_csr_operations::predicate() must be provided for an implemented CSR
4905 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
4906 /* User Floating-Point CSRs */
4907 [CSR_FFLAGS] = { "fflags", fs, read_fflags, write_fflags },
4908 [CSR_FRM] = { "frm", fs, read_frm, write_frm },
4909 [CSR_FCSR] = { "fcsr", fs, read_fcsr, write_fcsr },
4910 /* Vector CSRs */
4911 [CSR_VSTART] = { "vstart", vs, read_vstart, write_vstart },
4912 [CSR_VXSAT] = { "vxsat", vs, read_vxsat, write_vxsat },
4913 [CSR_VXRM] = { "vxrm", vs, read_vxrm, write_vxrm },
4914 [CSR_VCSR] = { "vcsr", vs, read_vcsr, write_vcsr },
4915 [CSR_VL] = { "vl", vs, read_vl },
4916 [CSR_VTYPE] = { "vtype", vs, read_vtype },
4917 [CSR_VLENB] = { "vlenb", vs, read_vlenb },
4918 /* User Timers and Counters */
4919 [CSR_CYCLE] = { "cycle", ctr, read_hpmcounter },
4920 [CSR_INSTRET] = { "instret", ctr, read_hpmcounter },
4921 [CSR_CYCLEH] = { "cycleh", ctr32, read_hpmcounterh },
4922 [CSR_INSTRETH] = { "instreth", ctr32, read_hpmcounterh },
4925 * In privileged mode, the monitor will have to emulate TIME CSRs only if
4926 * rdtime callback is not provided by machine/platform emulation.
4928 [CSR_TIME] = { "time", ctr, read_time },
4929 [CSR_TIMEH] = { "timeh", ctr32, read_timeh },
4931 /* Crypto Extension */
4932 [CSR_SEED] = { "seed", seed, NULL, NULL, rmw_seed },
4934 /* Zcmt Extension */
4935 [CSR_JVT] = {"jvt", zcmt, read_jvt, write_jvt},
4937 #if !defined(CONFIG_USER_ONLY)
4938 /* Machine Timers and Counters */
4939 [CSR_MCYCLE] = { "mcycle", any, read_hpmcounter,
4940 write_mhpmcounter },
4941 [CSR_MINSTRET] = { "minstret", any, read_hpmcounter,
4942 write_mhpmcounter },
4943 [CSR_MCYCLEH] = { "mcycleh", any32, read_hpmcounterh,
4944 write_mhpmcounterh },
4945 [CSR_MINSTRETH] = { "minstreth", any32, read_hpmcounterh,
4946 write_mhpmcounterh },
4948 /* Machine Information Registers */
4949 [CSR_MVENDORID] = { "mvendorid", any, read_mvendorid },
4950 [CSR_MARCHID] = { "marchid", any, read_marchid },
4951 [CSR_MIMPID] = { "mimpid", any, read_mimpid },
4952 [CSR_MHARTID] = { "mhartid", any, read_mhartid },
4954 [CSR_MCONFIGPTR] = { "mconfigptr", any, read_zero,
4955 .min_priv_ver = PRIV_VERSION_1_12_0 },
4956 /* Machine Trap Setup */
4957 [CSR_MSTATUS] = { "mstatus", any, read_mstatus, write_mstatus,
4958 NULL, read_mstatus_i128 },
4959 [CSR_MISA] = { "misa", any, read_misa, write_misa,
4960 NULL, read_misa_i128 },
4961 [CSR_MIDELEG] = { "mideleg", any, NULL, NULL, rmw_mideleg },
4962 [CSR_MEDELEG] = { "medeleg", any, read_medeleg, write_medeleg },
4963 [CSR_MIE] = { "mie", any, NULL, NULL, rmw_mie },
4964 [CSR_MTVEC] = { "mtvec", any, read_mtvec, write_mtvec },
4965 [CSR_MCOUNTEREN] = { "mcounteren", umode, read_mcounteren,
4966 write_mcounteren },
4968 [CSR_MSTATUSH] = { "mstatush", any32, read_mstatush,
4969 write_mstatush },
4970 [CSR_MEDELEGH] = { "medelegh", any32, read_zero, write_ignore,
4971 .min_priv_ver = PRIV_VERSION_1_13_0 },
4972 [CSR_HEDELEGH] = { "hedelegh", hmode32, read_hedelegh, write_hedelegh,
4973 .min_priv_ver = PRIV_VERSION_1_13_0 },
4975 /* Machine Trap Handling */
4976 [CSR_MSCRATCH] = { "mscratch", any, read_mscratch, write_mscratch,
4977 NULL, read_mscratch_i128, write_mscratch_i128 },
4978 [CSR_MEPC] = { "mepc", any, read_mepc, write_mepc },
4979 [CSR_MCAUSE] = { "mcause", any, read_mcause, write_mcause },
4980 [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval },
4981 [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip },
4983 /* Machine-Level Window to Indirectly Accessed Registers (AIA) */
4984 [CSR_MISELECT] = { "miselect", aia_any, NULL, NULL, rmw_xiselect },
4985 [CSR_MIREG] = { "mireg", aia_any, NULL, NULL, rmw_xireg },
4987 /* Machine-Level Interrupts (AIA) */
4988 [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei },
4989 [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi },
4991 /* Virtual Interrupts for Supervisor Level (AIA) */
4992 [CSR_MVIEN] = { "mvien", aia_any, NULL, NULL, rmw_mvien },
4993 [CSR_MVIP] = { "mvip", aia_any, NULL, NULL, rmw_mvip },
4995 /* Machine-Level High-Half CSRs (AIA) */
4996 [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
4997 [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh },
4998 [CSR_MVIENH] = { "mvienh", aia_any32, NULL, NULL, rmw_mvienh },
4999 [CSR_MVIPH] = { "mviph", aia_any32, NULL, NULL, rmw_mviph },
5000 [CSR_MIPH] = { "miph", aia_any32, NULL, NULL, rmw_miph },
5002 /* Execution environment configuration */
5003 [CSR_MENVCFG] = { "menvcfg", umode, read_menvcfg, write_menvcfg,
5004 .min_priv_ver = PRIV_VERSION_1_12_0 },
5005 [CSR_MENVCFGH] = { "menvcfgh", umode32, read_menvcfgh, write_menvcfgh,
5006 .min_priv_ver = PRIV_VERSION_1_12_0 },
5007 [CSR_SENVCFG] = { "senvcfg", smode, read_senvcfg, write_senvcfg,
5008 .min_priv_ver = PRIV_VERSION_1_12_0 },
5009 [CSR_HENVCFG] = { "henvcfg", hmode, read_henvcfg, write_henvcfg,
5010 .min_priv_ver = PRIV_VERSION_1_12_0 },
5011 [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh, write_henvcfgh,
5012 .min_priv_ver = PRIV_VERSION_1_12_0 },
5014 /* Smstateen extension CSRs */
5015 [CSR_MSTATEEN0] = { "mstateen0", mstateen, read_mstateen, write_mstateen0,
5016 .min_priv_ver = PRIV_VERSION_1_12_0 },
5017 [CSR_MSTATEEN0H] = { "mstateen0h", mstateen, read_mstateenh,
5018 write_mstateen0h,
5019 .min_priv_ver = PRIV_VERSION_1_12_0 },
5020 [CSR_MSTATEEN1] = { "mstateen1", mstateen, read_mstateen,
5021 write_mstateen_1_3,
5022 .min_priv_ver = PRIV_VERSION_1_12_0 },
5023 [CSR_MSTATEEN1H] = { "mstateen1h", mstateen, read_mstateenh,
5024 write_mstateenh_1_3,
5025 .min_priv_ver = PRIV_VERSION_1_12_0 },
5026 [CSR_MSTATEEN2] = { "mstateen2", mstateen, read_mstateen,
5027 write_mstateen_1_3,
5028 .min_priv_ver = PRIV_VERSION_1_12_0 },
5029 [CSR_MSTATEEN2H] = { "mstateen2h", mstateen, read_mstateenh,
5030 write_mstateenh_1_3,
5031 .min_priv_ver = PRIV_VERSION_1_12_0 },
5032 [CSR_MSTATEEN3] = { "mstateen3", mstateen, read_mstateen,
5033 write_mstateen_1_3,
5034 .min_priv_ver = PRIV_VERSION_1_12_0 },
5035 [CSR_MSTATEEN3H] = { "mstateen3h", mstateen, read_mstateenh,
5036 write_mstateenh_1_3,
5037 .min_priv_ver = PRIV_VERSION_1_12_0 },
5038 [CSR_HSTATEEN0] = { "hstateen0", hstateen, read_hstateen, write_hstateen0,
5039 .min_priv_ver = PRIV_VERSION_1_12_0 },
5040 [CSR_HSTATEEN0H] = { "hstateen0h", hstateenh, read_hstateenh,
5041 write_hstateen0h,
5042 .min_priv_ver = PRIV_VERSION_1_12_0 },
5043 [CSR_HSTATEEN1] = { "hstateen1", hstateen, read_hstateen,
5044 write_hstateen_1_3,
5045 .min_priv_ver = PRIV_VERSION_1_12_0 },
5046 [CSR_HSTATEEN1H] = { "hstateen1h", hstateenh, read_hstateenh,
5047 write_hstateenh_1_3,
5048 .min_priv_ver = PRIV_VERSION_1_12_0 },
5049 [CSR_HSTATEEN2] = { "hstateen2", hstateen, read_hstateen,
5050 write_hstateen_1_3,
5051 .min_priv_ver = PRIV_VERSION_1_12_0 },
5052 [CSR_HSTATEEN2H] = { "hstateen2h", hstateenh, read_hstateenh,
5053 write_hstateenh_1_3,
5054 .min_priv_ver = PRIV_VERSION_1_12_0 },
5055 [CSR_HSTATEEN3] = { "hstateen3", hstateen, read_hstateen,
5056 write_hstateen_1_3,
5057 .min_priv_ver = PRIV_VERSION_1_12_0 },
5058 [CSR_HSTATEEN3H] = { "hstateen3h", hstateenh, read_hstateenh,
5059 write_hstateenh_1_3,
5060 .min_priv_ver = PRIV_VERSION_1_12_0 },
5061 [CSR_SSTATEEN0] = { "sstateen0", sstateen, read_sstateen, write_sstateen0,
5062 .min_priv_ver = PRIV_VERSION_1_12_0 },
5063 [CSR_SSTATEEN1] = { "sstateen1", sstateen, read_sstateen,
5064 write_sstateen_1_3,
5065 .min_priv_ver = PRIV_VERSION_1_12_0 },
5066 [CSR_SSTATEEN2] = { "sstateen2", sstateen, read_sstateen,
5067 write_sstateen_1_3,
5068 .min_priv_ver = PRIV_VERSION_1_12_0 },
5069 [CSR_SSTATEEN3] = { "sstateen3", sstateen, read_sstateen,
5070 write_sstateen_1_3,
5071 .min_priv_ver = PRIV_VERSION_1_12_0 },
5073 /* Supervisor Trap Setup */
5074 [CSR_SSTATUS] = { "sstatus", smode, read_sstatus, write_sstatus,
5075 NULL, read_sstatus_i128 },
5076 [CSR_SIE] = { "sie", smode, NULL, NULL, rmw_sie },
5077 [CSR_STVEC] = { "stvec", smode, read_stvec, write_stvec },
5078 [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren,
5079 write_scounteren },
5081 /* Supervisor Trap Handling */
5082 [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch,
5083 NULL, read_sscratch_i128, write_sscratch_i128 },
5084 [CSR_SEPC] = { "sepc", smode, read_sepc, write_sepc },
5085 [CSR_SCAUSE] = { "scause", smode, read_scause, write_scause },
5086 [CSR_STVAL] = { "stval", smode, read_stval, write_stval },
5087 [CSR_SIP] = { "sip", smode, NULL, NULL, rmw_sip },
5088 [CSR_STIMECMP] = { "stimecmp", sstc, read_stimecmp, write_stimecmp,
5089 .min_priv_ver = PRIV_VERSION_1_12_0 },
5090 [CSR_STIMECMPH] = { "stimecmph", sstc_32, read_stimecmph, write_stimecmph,
5091 .min_priv_ver = PRIV_VERSION_1_12_0 },
5092 [CSR_VSTIMECMP] = { "vstimecmp", sstc, read_vstimecmp,
5093 write_vstimecmp,
5094 .min_priv_ver = PRIV_VERSION_1_12_0 },
5095 [CSR_VSTIMECMPH] = { "vstimecmph", sstc_32, read_vstimecmph,
5096 write_vstimecmph,
5097 .min_priv_ver = PRIV_VERSION_1_12_0 },
5099 /* Supervisor Protection and Translation */
5100 [CSR_SATP] = { "satp", satp, read_satp, write_satp },
5102 /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */
5103 [CSR_SISELECT] = { "siselect", aia_smode, NULL, NULL, rmw_xiselect },
5104 [CSR_SIREG] = { "sireg", aia_smode, NULL, NULL, rmw_xireg },
5106 /* Supervisor-Level Interrupts (AIA) */
5107 [CSR_STOPEI] = { "stopei", aia_smode, NULL, NULL, rmw_xtopei },
5108 [CSR_STOPI] = { "stopi", aia_smode, read_stopi },
5110 /* Supervisor-Level High-Half CSRs (AIA) */
5111 [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh },
5112 [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph },
5114 [CSR_HSTATUS] = { "hstatus", hmode, read_hstatus, write_hstatus,
5115 .min_priv_ver = PRIV_VERSION_1_12_0 },
5116 [CSR_HEDELEG] = { "hedeleg", hmode, read_hedeleg, write_hedeleg,
5117 .min_priv_ver = PRIV_VERSION_1_12_0 },
5118 [CSR_HIDELEG] = { "hideleg", hmode, NULL, NULL, rmw_hideleg,
5119 .min_priv_ver = PRIV_VERSION_1_12_0 },
5120 [CSR_HVIP] = { "hvip", hmode, NULL, NULL, rmw_hvip,
5121 .min_priv_ver = PRIV_VERSION_1_12_0 },
5122 [CSR_HIP] = { "hip", hmode, NULL, NULL, rmw_hip,
5123 .min_priv_ver = PRIV_VERSION_1_12_0 },
5124 [CSR_HIE] = { "hie", hmode, NULL, NULL, rmw_hie,
5125 .min_priv_ver = PRIV_VERSION_1_12_0 },
5126 [CSR_HCOUNTEREN] = { "hcounteren", hmode, read_hcounteren,
5127 write_hcounteren,
5128 .min_priv_ver = PRIV_VERSION_1_12_0 },
5129 [CSR_HGEIE] = { "hgeie", hmode, read_hgeie, write_hgeie,
5130 .min_priv_ver = PRIV_VERSION_1_12_0 },
5131 [CSR_HTVAL] = { "htval", hmode, read_htval, write_htval,
5132 .min_priv_ver = PRIV_VERSION_1_12_0 },
5133 [CSR_HTINST] = { "htinst", hmode, read_htinst, write_htinst,
5134 .min_priv_ver = PRIV_VERSION_1_12_0 },
5135 [CSR_HGEIP] = { "hgeip", hmode, read_hgeip,
5136 .min_priv_ver = PRIV_VERSION_1_12_0 },
5137 [CSR_HGATP] = { "hgatp", hgatp, read_hgatp, write_hgatp,
5138 .min_priv_ver = PRIV_VERSION_1_12_0 },
5139 [CSR_HTIMEDELTA] = { "htimedelta", hmode, read_htimedelta,
5140 write_htimedelta,
5141 .min_priv_ver = PRIV_VERSION_1_12_0 },
5142 [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah,
5143 write_htimedeltah,
5144 .min_priv_ver = PRIV_VERSION_1_12_0 },
5146 [CSR_VSSTATUS] = { "vsstatus", hmode, read_vsstatus,
5147 write_vsstatus,
5148 .min_priv_ver = PRIV_VERSION_1_12_0 },
5149 [CSR_VSIP] = { "vsip", hmode, NULL, NULL, rmw_vsip,
5150 .min_priv_ver = PRIV_VERSION_1_12_0 },
5151 [CSR_VSIE] = { "vsie", hmode, NULL, NULL, rmw_vsie ,
5152 .min_priv_ver = PRIV_VERSION_1_12_0 },
5153 [CSR_VSTVEC] = { "vstvec", hmode, read_vstvec, write_vstvec,
5154 .min_priv_ver = PRIV_VERSION_1_12_0 },
5155 [CSR_VSSCRATCH] = { "vsscratch", hmode, read_vsscratch,
5156 write_vsscratch,
5157 .min_priv_ver = PRIV_VERSION_1_12_0 },
5158 [CSR_VSEPC] = { "vsepc", hmode, read_vsepc, write_vsepc,
5159 .min_priv_ver = PRIV_VERSION_1_12_0 },
5160 [CSR_VSCAUSE] = { "vscause", hmode, read_vscause, write_vscause,
5161 .min_priv_ver = PRIV_VERSION_1_12_0 },
5162 [CSR_VSTVAL] = { "vstval", hmode, read_vstval, write_vstval,
5163 .min_priv_ver = PRIV_VERSION_1_12_0 },
5164 [CSR_VSATP] = { "vsatp", hmode, read_vsatp, write_vsatp,
5165 .min_priv_ver = PRIV_VERSION_1_12_0 },
5167 [CSR_MTVAL2] = { "mtval2", hmode, read_mtval2, write_mtval2,
5168 .min_priv_ver = PRIV_VERSION_1_12_0 },
5169 [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst,
5170 .min_priv_ver = PRIV_VERSION_1_12_0 },
5172 /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */
5173 [CSR_HVIEN] = { "hvien", aia_hmode, NULL, NULL, rmw_hvien },
5174 [CSR_HVICTL] = { "hvictl", aia_hmode, read_hvictl,
5175 write_hvictl },
5176 [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1,
5177 write_hviprio1 },
5178 [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2,
5179 write_hviprio2 },
5181 * VS-Level Window to Indirectly Accessed Registers (H-extension with AIA)
5183 [CSR_VSISELECT] = { "vsiselect", aia_hmode, NULL, NULL,
5184 rmw_xiselect },
5185 [CSR_VSIREG] = { "vsireg", aia_hmode, NULL, NULL, rmw_xireg },
5187 /* VS-Level Interrupts (H-extension with AIA) */
5188 [CSR_VSTOPEI] = { "vstopei", aia_hmode, NULL, NULL, rmw_xtopei },
5189 [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi },
5191 /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
5192 [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL,
5193 rmw_hidelegh },
5194 [CSR_HVIENH] = { "hvienh", aia_hmode32, NULL, NULL, rmw_hvienh },
5195 [CSR_HVIPH] = { "hviph", aia_hmode32, NULL, NULL, rmw_hviph },
5196 [CSR_HVIPRIO1H] = { "hviprio1h", aia_hmode32, read_hviprio1h,
5197 write_hviprio1h },
5198 [CSR_HVIPRIO2H] = { "hviprio2h", aia_hmode32, read_hviprio2h,
5199 write_hviprio2h },
5200 [CSR_VSIEH] = { "vsieh", aia_hmode32, NULL, NULL, rmw_vsieh },
5201 [CSR_VSIPH] = { "vsiph", aia_hmode32, NULL, NULL, rmw_vsiph },
5203 /* Physical Memory Protection */
5204 [CSR_MSECCFG] = { "mseccfg", have_mseccfg, read_mseccfg, write_mseccfg,
5205 .min_priv_ver = PRIV_VERSION_1_11_0 },
5206 [CSR_PMPCFG0] = { "pmpcfg0", pmp, read_pmpcfg, write_pmpcfg },
5207 [CSR_PMPCFG1] = { "pmpcfg1", pmp, read_pmpcfg, write_pmpcfg },
5208 [CSR_PMPCFG2] = { "pmpcfg2", pmp, read_pmpcfg, write_pmpcfg },
5209 [CSR_PMPCFG3] = { "pmpcfg3", pmp, read_pmpcfg, write_pmpcfg },
5210 [CSR_PMPADDR0] = { "pmpaddr0", pmp, read_pmpaddr, write_pmpaddr },
5211 [CSR_PMPADDR1] = { "pmpaddr1", pmp, read_pmpaddr, write_pmpaddr },
5212 [CSR_PMPADDR2] = { "pmpaddr2", pmp, read_pmpaddr, write_pmpaddr },
5213 [CSR_PMPADDR3] = { "pmpaddr3", pmp, read_pmpaddr, write_pmpaddr },
5214 [CSR_PMPADDR4] = { "pmpaddr4", pmp, read_pmpaddr, write_pmpaddr },
5215 [CSR_PMPADDR5] = { "pmpaddr5", pmp, read_pmpaddr, write_pmpaddr },
5216 [CSR_PMPADDR6] = { "pmpaddr6", pmp, read_pmpaddr, write_pmpaddr },
5217 [CSR_PMPADDR7] = { "pmpaddr7", pmp, read_pmpaddr, write_pmpaddr },
5218 [CSR_PMPADDR8] = { "pmpaddr8", pmp, read_pmpaddr, write_pmpaddr },
5219 [CSR_PMPADDR9] = { "pmpaddr9", pmp, read_pmpaddr, write_pmpaddr },
5220 [CSR_PMPADDR10] = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr },
5221 [CSR_PMPADDR11] = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr },
5222 [CSR_PMPADDR12] = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr },
5223 [CSR_PMPADDR13] = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr },
5224 [CSR_PMPADDR14] = { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr },
5225 [CSR_PMPADDR15] = { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr },
5227 /* Debug CSRs */
5228 [CSR_TSELECT] = { "tselect", debug, read_tselect, write_tselect },
5229 [CSR_TDATA1] = { "tdata1", debug, read_tdata, write_tdata },
5230 [CSR_TDATA2] = { "tdata2", debug, read_tdata, write_tdata },
5231 [CSR_TDATA3] = { "tdata3", debug, read_tdata, write_tdata },
5232 [CSR_TINFO] = { "tinfo", debug, read_tinfo, write_ignore },
5233 [CSR_MCONTEXT] = { "mcontext", debug, read_mcontext, write_mcontext },
5235 /* User Pointer Masking */
5236 [CSR_UMTE] = { "umte", pointer_masking, read_umte, write_umte },
5237 [CSR_UPMMASK] = { "upmmask", pointer_masking, read_upmmask,
5238 write_upmmask },
5239 [CSR_UPMBASE] = { "upmbase", pointer_masking, read_upmbase,
5240 write_upmbase },
5241 /* Machine Pointer Masking */
5242 [CSR_MMTE] = { "mmte", pointer_masking, read_mmte, write_mmte },
5243 [CSR_MPMMASK] = { "mpmmask", pointer_masking, read_mpmmask,
5244 write_mpmmask },
5245 [CSR_MPMBASE] = { "mpmbase", pointer_masking, read_mpmbase,
5246 write_mpmbase },
5247 /* Supervisor Pointer Masking */
5248 [CSR_SMTE] = { "smte", pointer_masking, read_smte, write_smte },
5249 [CSR_SPMMASK] = { "spmmask", pointer_masking, read_spmmask,
5250 write_spmmask },
5251 [CSR_SPMBASE] = { "spmbase", pointer_masking, read_spmbase,
5252 write_spmbase },
5254 /* Performance Counters */
5255 [CSR_HPMCOUNTER3] = { "hpmcounter3", ctr, read_hpmcounter },
5256 [CSR_HPMCOUNTER4] = { "hpmcounter4", ctr, read_hpmcounter },
5257 [CSR_HPMCOUNTER5] = { "hpmcounter5", ctr, read_hpmcounter },
5258 [CSR_HPMCOUNTER6] = { "hpmcounter6", ctr, read_hpmcounter },
5259 [CSR_HPMCOUNTER7] = { "hpmcounter7", ctr, read_hpmcounter },
5260 [CSR_HPMCOUNTER8] = { "hpmcounter8", ctr, read_hpmcounter },
5261 [CSR_HPMCOUNTER9] = { "hpmcounter9", ctr, read_hpmcounter },
5262 [CSR_HPMCOUNTER10] = { "hpmcounter10", ctr, read_hpmcounter },
5263 [CSR_HPMCOUNTER11] = { "hpmcounter11", ctr, read_hpmcounter },
5264 [CSR_HPMCOUNTER12] = { "hpmcounter12", ctr, read_hpmcounter },
5265 [CSR_HPMCOUNTER13] = { "hpmcounter13", ctr, read_hpmcounter },
5266 [CSR_HPMCOUNTER14] = { "hpmcounter14", ctr, read_hpmcounter },
5267 [CSR_HPMCOUNTER15] = { "hpmcounter15", ctr, read_hpmcounter },
5268 [CSR_HPMCOUNTER16] = { "hpmcounter16", ctr, read_hpmcounter },
5269 [CSR_HPMCOUNTER17] = { "hpmcounter17", ctr, read_hpmcounter },
5270 [CSR_HPMCOUNTER18] = { "hpmcounter18", ctr, read_hpmcounter },
5271 [CSR_HPMCOUNTER19] = { "hpmcounter19", ctr, read_hpmcounter },
5272 [CSR_HPMCOUNTER20] = { "hpmcounter20", ctr, read_hpmcounter },
5273 [CSR_HPMCOUNTER21] = { "hpmcounter21", ctr, read_hpmcounter },
5274 [CSR_HPMCOUNTER22] = { "hpmcounter22", ctr, read_hpmcounter },
5275 [CSR_HPMCOUNTER23] = { "hpmcounter23", ctr, read_hpmcounter },
5276 [CSR_HPMCOUNTER24] = { "hpmcounter24", ctr, read_hpmcounter },
5277 [CSR_HPMCOUNTER25] = { "hpmcounter25", ctr, read_hpmcounter },
5278 [CSR_HPMCOUNTER26] = { "hpmcounter26", ctr, read_hpmcounter },
5279 [CSR_HPMCOUNTER27] = { "hpmcounter27", ctr, read_hpmcounter },
5280 [CSR_HPMCOUNTER28] = { "hpmcounter28", ctr, read_hpmcounter },
5281 [CSR_HPMCOUNTER29] = { "hpmcounter29", ctr, read_hpmcounter },
5282 [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_hpmcounter },
5283 [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_hpmcounter },
5285 [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_hpmcounter,
5286 write_mhpmcounter },
5287 [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_hpmcounter,
5288 write_mhpmcounter },
5289 [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_hpmcounter,
5290 write_mhpmcounter },
5291 [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_hpmcounter,
5292 write_mhpmcounter },
5293 [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_hpmcounter,
5294 write_mhpmcounter },
5295 [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_hpmcounter,
5296 write_mhpmcounter },
5297 [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_hpmcounter,
5298 write_mhpmcounter },
5299 [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_hpmcounter,
5300 write_mhpmcounter },
5301 [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_hpmcounter,
5302 write_mhpmcounter },
5303 [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_hpmcounter,
5304 write_mhpmcounter },
5305 [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_hpmcounter,
5306 write_mhpmcounter },
5307 [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_hpmcounter,
5308 write_mhpmcounter },
5309 [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_hpmcounter,
5310 write_mhpmcounter },
5311 [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_hpmcounter,
5312 write_mhpmcounter },
5313 [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_hpmcounter,
5314 write_mhpmcounter },
5315 [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_hpmcounter,
5316 write_mhpmcounter },
5317 [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_hpmcounter,
5318 write_mhpmcounter },
5319 [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_hpmcounter,
5320 write_mhpmcounter },
5321 [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_hpmcounter,
5322 write_mhpmcounter },
5323 [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_hpmcounter,
5324 write_mhpmcounter },
5325 [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_hpmcounter,
5326 write_mhpmcounter },
5327 [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_hpmcounter,
5328 write_mhpmcounter },
5329 [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_hpmcounter,
5330 write_mhpmcounter },
5331 [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_hpmcounter,
5332 write_mhpmcounter },
5333 [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_hpmcounter,
5334 write_mhpmcounter },
5335 [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_hpmcounter,
5336 write_mhpmcounter },
5337 [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_hpmcounter,
5338 write_mhpmcounter },
5339 [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_hpmcounter,
5340 write_mhpmcounter },
5341 [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_hpmcounter,
5342 write_mhpmcounter },
5344 [CSR_MCOUNTINHIBIT] = { "mcountinhibit", any, read_mcountinhibit,
5345 write_mcountinhibit,
5346 .min_priv_ver = PRIV_VERSION_1_11_0 },
5348 [CSR_MCYCLECFG] = { "mcyclecfg", smcntrpmf, read_mcyclecfg,
5349 write_mcyclecfg,
5350 .min_priv_ver = PRIV_VERSION_1_12_0 },
5351 [CSR_MINSTRETCFG] = { "minstretcfg", smcntrpmf, read_minstretcfg,
5352 write_minstretcfg,
5353 .min_priv_ver = PRIV_VERSION_1_12_0 },
5355 [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_mhpmevent,
5356 write_mhpmevent },
5357 [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_mhpmevent,
5358 write_mhpmevent },
5359 [CSR_MHPMEVENT5] = { "mhpmevent5", any, read_mhpmevent,
5360 write_mhpmevent },
5361 [CSR_MHPMEVENT6] = { "mhpmevent6", any, read_mhpmevent,
5362 write_mhpmevent },
5363 [CSR_MHPMEVENT7] = { "mhpmevent7", any, read_mhpmevent,
5364 write_mhpmevent },
5365 [CSR_MHPMEVENT8] = { "mhpmevent8", any, read_mhpmevent,
5366 write_mhpmevent },
5367 [CSR_MHPMEVENT9] = { "mhpmevent9", any, read_mhpmevent,
5368 write_mhpmevent },
5369 [CSR_MHPMEVENT10] = { "mhpmevent10", any, read_mhpmevent,
5370 write_mhpmevent },
5371 [CSR_MHPMEVENT11] = { "mhpmevent11", any, read_mhpmevent,
5372 write_mhpmevent },
5373 [CSR_MHPMEVENT12] = { "mhpmevent12", any, read_mhpmevent,
5374 write_mhpmevent },
5375 [CSR_MHPMEVENT13] = { "mhpmevent13", any, read_mhpmevent,
5376 write_mhpmevent },
5377 [CSR_MHPMEVENT14] = { "mhpmevent14", any, read_mhpmevent,
5378 write_mhpmevent },
5379 [CSR_MHPMEVENT15] = { "mhpmevent15", any, read_mhpmevent,
5380 write_mhpmevent },
5381 [CSR_MHPMEVENT16] = { "mhpmevent16", any, read_mhpmevent,
5382 write_mhpmevent },
5383 [CSR_MHPMEVENT17] = { "mhpmevent17", any, read_mhpmevent,
5384 write_mhpmevent },
5385 [CSR_MHPMEVENT18] = { "mhpmevent18", any, read_mhpmevent,
5386 write_mhpmevent },
5387 [CSR_MHPMEVENT19] = { "mhpmevent19", any, read_mhpmevent,
5388 write_mhpmevent },
5389 [CSR_MHPMEVENT20] = { "mhpmevent20", any, read_mhpmevent,
5390 write_mhpmevent },
5391 [CSR_MHPMEVENT21] = { "mhpmevent21", any, read_mhpmevent,
5392 write_mhpmevent },
5393 [CSR_MHPMEVENT22] = { "mhpmevent22", any, read_mhpmevent,
5394 write_mhpmevent },
5395 [CSR_MHPMEVENT23] = { "mhpmevent23", any, read_mhpmevent,
5396 write_mhpmevent },
5397 [CSR_MHPMEVENT24] = { "mhpmevent24", any, read_mhpmevent,
5398 write_mhpmevent },
5399 [CSR_MHPMEVENT25] = { "mhpmevent25", any, read_mhpmevent,
5400 write_mhpmevent },
5401 [CSR_MHPMEVENT26] = { "mhpmevent26", any, read_mhpmevent,
5402 write_mhpmevent },
5403 [CSR_MHPMEVENT27] = { "mhpmevent27", any, read_mhpmevent,
5404 write_mhpmevent },
5405 [CSR_MHPMEVENT28] = { "mhpmevent28", any, read_mhpmevent,
5406 write_mhpmevent },
5407 [CSR_MHPMEVENT29] = { "mhpmevent29", any, read_mhpmevent,
5408 write_mhpmevent },
5409 [CSR_MHPMEVENT30] = { "mhpmevent30", any, read_mhpmevent,
5410 write_mhpmevent },
5411 [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_mhpmevent,
5412 write_mhpmevent },
5414 [CSR_MCYCLECFGH] = { "mcyclecfgh", smcntrpmf_32, read_mcyclecfgh,
5415 write_mcyclecfgh,
5416 .min_priv_ver = PRIV_VERSION_1_12_0 },
5417 [CSR_MINSTRETCFGH] = { "minstretcfgh", smcntrpmf_32, read_minstretcfgh,
5418 write_minstretcfgh,
5419 .min_priv_ver = PRIV_VERSION_1_12_0 },
5421 [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf_32, read_mhpmeventh,
5422 write_mhpmeventh,
5423 .min_priv_ver = PRIV_VERSION_1_12_0 },
5424 [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf_32, read_mhpmeventh,
5425 write_mhpmeventh,
5426 .min_priv_ver = PRIV_VERSION_1_12_0 },
5427 [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf_32, read_mhpmeventh,
5428 write_mhpmeventh,
5429 .min_priv_ver = PRIV_VERSION_1_12_0 },
5430 [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf_32, read_mhpmeventh,
5431 write_mhpmeventh,
5432 .min_priv_ver = PRIV_VERSION_1_12_0 },
5433 [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf_32, read_mhpmeventh,
5434 write_mhpmeventh,
5435 .min_priv_ver = PRIV_VERSION_1_12_0 },
5436 [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf_32, read_mhpmeventh,
5437 write_mhpmeventh,
5438 .min_priv_ver = PRIV_VERSION_1_12_0 },
5439 [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf_32, read_mhpmeventh,
5440 write_mhpmeventh,
5441 .min_priv_ver = PRIV_VERSION_1_12_0 },
5442 [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf_32, read_mhpmeventh,
5443 write_mhpmeventh,
5444 .min_priv_ver = PRIV_VERSION_1_12_0 },
5445 [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf_32, read_mhpmeventh,
5446 write_mhpmeventh,
5447 .min_priv_ver = PRIV_VERSION_1_12_0 },
5448 [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf_32, read_mhpmeventh,
5449 write_mhpmeventh,
5450 .min_priv_ver = PRIV_VERSION_1_12_0 },
5451 [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf_32, read_mhpmeventh,
5452 write_mhpmeventh,
5453 .min_priv_ver = PRIV_VERSION_1_12_0 },
5454 [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf_32, read_mhpmeventh,
5455 write_mhpmeventh,
5456 .min_priv_ver = PRIV_VERSION_1_12_0 },
5457 [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf_32, read_mhpmeventh,
5458 write_mhpmeventh,
5459 .min_priv_ver = PRIV_VERSION_1_12_0 },
5460 [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf_32, read_mhpmeventh,
5461 write_mhpmeventh,
5462 .min_priv_ver = PRIV_VERSION_1_12_0 },
5463 [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf_32, read_mhpmeventh,
5464 write_mhpmeventh,
5465 .min_priv_ver = PRIV_VERSION_1_12_0 },
5466 [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf_32, read_mhpmeventh,
5467 write_mhpmeventh,
5468 .min_priv_ver = PRIV_VERSION_1_12_0 },
5469 [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf_32, read_mhpmeventh,
5470 write_mhpmeventh,
5471 .min_priv_ver = PRIV_VERSION_1_12_0 },
5472 [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf_32, read_mhpmeventh,
5473 write_mhpmeventh,
5474 .min_priv_ver = PRIV_VERSION_1_12_0 },
5475 [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf_32, read_mhpmeventh,
5476 write_mhpmeventh,
5477 .min_priv_ver = PRIV_VERSION_1_12_0 },
5478 [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf_32, read_mhpmeventh,
5479 write_mhpmeventh,
5480 .min_priv_ver = PRIV_VERSION_1_12_0 },
5481 [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf_32, read_mhpmeventh,
5482 write_mhpmeventh,
5483 .min_priv_ver = PRIV_VERSION_1_12_0 },
5484 [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf_32, read_mhpmeventh,
5485 write_mhpmeventh,
5486 .min_priv_ver = PRIV_VERSION_1_12_0 },
5487 [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf_32, read_mhpmeventh,
5488 write_mhpmeventh,
5489 .min_priv_ver = PRIV_VERSION_1_12_0 },
5490 [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf_32, read_mhpmeventh,
5491 write_mhpmeventh,
5492 .min_priv_ver = PRIV_VERSION_1_12_0 },
5493 [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf_32, read_mhpmeventh,
5494 write_mhpmeventh,
5495 .min_priv_ver = PRIV_VERSION_1_12_0 },
5496 [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf_32, read_mhpmeventh,
5497 write_mhpmeventh,
5498 .min_priv_ver = PRIV_VERSION_1_12_0 },
5499 [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf_32, read_mhpmeventh,
5500 write_mhpmeventh,
5501 .min_priv_ver = PRIV_VERSION_1_12_0 },
5502 [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf_32, read_mhpmeventh,
5503 write_mhpmeventh,
5504 .min_priv_ver = PRIV_VERSION_1_12_0 },
5505 [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf_32, read_mhpmeventh,
5506 write_mhpmeventh,
5507 .min_priv_ver = PRIV_VERSION_1_12_0 },
5509 [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh },
5510 [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh },
5511 [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_hpmcounterh },
5512 [CSR_HPMCOUNTER6H] = { "hpmcounter6h", ctr32, read_hpmcounterh },
5513 [CSR_HPMCOUNTER7H] = { "hpmcounter7h", ctr32, read_hpmcounterh },
5514 [CSR_HPMCOUNTER8H] = { "hpmcounter8h", ctr32, read_hpmcounterh },
5515 [CSR_HPMCOUNTER9H] = { "hpmcounter9h", ctr32, read_hpmcounterh },
5516 [CSR_HPMCOUNTER10H] = { "hpmcounter10h", ctr32, read_hpmcounterh },
5517 [CSR_HPMCOUNTER11H] = { "hpmcounter11h", ctr32, read_hpmcounterh },
5518 [CSR_HPMCOUNTER12H] = { "hpmcounter12h", ctr32, read_hpmcounterh },
5519 [CSR_HPMCOUNTER13H] = { "hpmcounter13h", ctr32, read_hpmcounterh },
5520 [CSR_HPMCOUNTER14H] = { "hpmcounter14h", ctr32, read_hpmcounterh },
5521 [CSR_HPMCOUNTER15H] = { "hpmcounter15h", ctr32, read_hpmcounterh },
5522 [CSR_HPMCOUNTER16H] = { "hpmcounter16h", ctr32, read_hpmcounterh },
5523 [CSR_HPMCOUNTER17H] = { "hpmcounter17h", ctr32, read_hpmcounterh },
5524 [CSR_HPMCOUNTER18H] = { "hpmcounter18h", ctr32, read_hpmcounterh },
5525 [CSR_HPMCOUNTER19H] = { "hpmcounter19h", ctr32, read_hpmcounterh },
5526 [CSR_HPMCOUNTER20H] = { "hpmcounter20h", ctr32, read_hpmcounterh },
5527 [CSR_HPMCOUNTER21H] = { "hpmcounter21h", ctr32, read_hpmcounterh },
5528 [CSR_HPMCOUNTER22H] = { "hpmcounter22h", ctr32, read_hpmcounterh },
5529 [CSR_HPMCOUNTER23H] = { "hpmcounter23h", ctr32, read_hpmcounterh },
5530 [CSR_HPMCOUNTER24H] = { "hpmcounter24h", ctr32, read_hpmcounterh },
5531 [CSR_HPMCOUNTER25H] = { "hpmcounter25h", ctr32, read_hpmcounterh },
5532 [CSR_HPMCOUNTER26H] = { "hpmcounter26h", ctr32, read_hpmcounterh },
5533 [CSR_HPMCOUNTER27H] = { "hpmcounter27h", ctr32, read_hpmcounterh },
5534 [CSR_HPMCOUNTER28H] = { "hpmcounter28h", ctr32, read_hpmcounterh },
5535 [CSR_HPMCOUNTER29H] = { "hpmcounter29h", ctr32, read_hpmcounterh },
5536 [CSR_HPMCOUNTER30H] = { "hpmcounter30h", ctr32, read_hpmcounterh },
5537 [CSR_HPMCOUNTER31H] = { "hpmcounter31h", ctr32, read_hpmcounterh },
5539 [CSR_MHPMCOUNTER3H] = { "mhpmcounter3h", mctr32, read_hpmcounterh,
5540 write_mhpmcounterh },
5541 [CSR_MHPMCOUNTER4H] = { "mhpmcounter4h", mctr32, read_hpmcounterh,
5542 write_mhpmcounterh },
5543 [CSR_MHPMCOUNTER5H] = { "mhpmcounter5h", mctr32, read_hpmcounterh,
5544 write_mhpmcounterh },
5545 [CSR_MHPMCOUNTER6H] = { "mhpmcounter6h", mctr32, read_hpmcounterh,
5546 write_mhpmcounterh },
5547 [CSR_MHPMCOUNTER7H] = { "mhpmcounter7h", mctr32, read_hpmcounterh,
5548 write_mhpmcounterh },
5549 [CSR_MHPMCOUNTER8H] = { "mhpmcounter8h", mctr32, read_hpmcounterh,
5550 write_mhpmcounterh },
5551 [CSR_MHPMCOUNTER9H] = { "mhpmcounter9h", mctr32, read_hpmcounterh,
5552 write_mhpmcounterh },
5553 [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", mctr32, read_hpmcounterh,
5554 write_mhpmcounterh },
5555 [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", mctr32, read_hpmcounterh,
5556 write_mhpmcounterh },
5557 [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", mctr32, read_hpmcounterh,
5558 write_mhpmcounterh },
5559 [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", mctr32, read_hpmcounterh,
5560 write_mhpmcounterh },
5561 [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", mctr32, read_hpmcounterh,
5562 write_mhpmcounterh },
5563 [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", mctr32, read_hpmcounterh,
5564 write_mhpmcounterh },
5565 [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", mctr32, read_hpmcounterh,
5566 write_mhpmcounterh },
5567 [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", mctr32, read_hpmcounterh,
5568 write_mhpmcounterh },
5569 [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", mctr32, read_hpmcounterh,
5570 write_mhpmcounterh },
5571 [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", mctr32, read_hpmcounterh,
5572 write_mhpmcounterh },
5573 [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", mctr32, read_hpmcounterh,
5574 write_mhpmcounterh },
5575 [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", mctr32, read_hpmcounterh,
5576 write_mhpmcounterh },
5577 [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", mctr32, read_hpmcounterh,
5578 write_mhpmcounterh },
5579 [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", mctr32, read_hpmcounterh,
5580 write_mhpmcounterh },
5581 [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", mctr32, read_hpmcounterh,
5582 write_mhpmcounterh },
5583 [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", mctr32, read_hpmcounterh,
5584 write_mhpmcounterh },
5585 [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", mctr32, read_hpmcounterh,
5586 write_mhpmcounterh },
5587 [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", mctr32, read_hpmcounterh,
5588 write_mhpmcounterh },
5589 [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", mctr32, read_hpmcounterh,
5590 write_mhpmcounterh },
5591 [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", mctr32, read_hpmcounterh,
5592 write_mhpmcounterh },
5593 [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", mctr32, read_hpmcounterh,
5594 write_mhpmcounterh },
5595 [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh,
5596 write_mhpmcounterh },
5597 [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf,
5598 .min_priv_ver = PRIV_VERSION_1_12_0 },
5600 #endif /* !CONFIG_USER_ONLY */