xref: /openbmc/qemu/target/riscv/csr.c (revision 50a92d9b)
1 /*
2  * RISC-V Control and Status Registers.
3  *
4  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5  * Copyright (c) 2017-2018 SiFive, Inc.
6  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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"
32 
33 
34 /* CSR function table public API */
35 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops)
36 {
37     *ops = csr_ops[csrno & (CSR_TABLE_SIZE - 1)];
38 }
39 
40 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops)
41 {
42     csr_ops[csrno & (CSR_TABLE_SIZE - 1)] = *ops;
43 }
44 
45 /* Predicates */
46 #if !defined(CONFIG_USER_ONLY)
47 RISCVException smstateen_acc_ok(CPURISCVState *env, int index, uint64_t bit)
48 {
49     bool virt = env->virt_enabled;
50 
51     if (env->priv == PRV_M || !riscv_cpu_cfg(env)->ext_smstateen) {
52         return RISCV_EXCP_NONE;
53     }
54 
55     if (!(env->mstateen[index] & bit)) {
56         return RISCV_EXCP_ILLEGAL_INST;
57     }
58 
59     if (virt) {
60         if (!(env->hstateen[index] & bit)) {
61             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
62         }
63 
64         if (env->priv == PRV_U && !(env->sstateen[index] & bit)) {
65             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
66         }
67     }
68 
69     if (env->priv == PRV_U && riscv_has_ext(env, RVS)) {
70         if (!(env->sstateen[index] & bit)) {
71             return RISCV_EXCP_ILLEGAL_INST;
72         }
73     }
74 
75     return RISCV_EXCP_NONE;
76 }
77 #endif
78 
79 static RISCVException fs(CPURISCVState *env, int csrno)
80 {
81 #if !defined(CONFIG_USER_ONLY)
82     if (!env->debugger && !riscv_cpu_fp_enabled(env) &&
83         !riscv_cpu_cfg(env)->ext_zfinx) {
84         return RISCV_EXCP_ILLEGAL_INST;
85     }
86 
87     if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
88         return smstateen_acc_ok(env, 0, SMSTATEEN0_FCSR);
89     }
90 #endif
91     return RISCV_EXCP_NONE;
92 }
93 
94 static RISCVException vs(CPURISCVState *env, int csrno)
95 {
96     if (riscv_cpu_cfg(env)->ext_zve32x) {
97 #if !defined(CONFIG_USER_ONLY)
98         if (!env->debugger && !riscv_cpu_vector_enabled(env)) {
99             return RISCV_EXCP_ILLEGAL_INST;
100         }
101 #endif
102         return RISCV_EXCP_NONE;
103     }
104     return RISCV_EXCP_ILLEGAL_INST;
105 }
106 
107 static RISCVException ctr(CPURISCVState *env, int csrno)
108 {
109 #if !defined(CONFIG_USER_ONLY)
110     RISCVCPU *cpu = env_archcpu(env);
111     int ctr_index;
112     target_ulong ctr_mask;
113     int base_csrno = CSR_CYCLE;
114     bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false;
115 
116     if (rv32 && csrno >= CSR_CYCLEH) {
117         /* Offset for RV32 hpmcounternh counters */
118         base_csrno += 0x80;
119     }
120     ctr_index = csrno - base_csrno;
121     ctr_mask = BIT(ctr_index);
122 
123     if ((csrno >= CSR_CYCLE && csrno <= CSR_INSTRET) ||
124         (csrno >= CSR_CYCLEH && csrno <= CSR_INSTRETH)) {
125         if (!riscv_cpu_cfg(env)->ext_zicntr) {
126             return RISCV_EXCP_ILLEGAL_INST;
127         }
128 
129         goto skip_ext_pmu_check;
130     }
131 
132     if (!(cpu->pmu_avail_ctrs & ctr_mask)) {
133         /* No counter is enabled in PMU or the counter is out of range */
134         return RISCV_EXCP_ILLEGAL_INST;
135     }
136 
137 skip_ext_pmu_check:
138 
139     if (env->debugger) {
140         return RISCV_EXCP_NONE;
141     }
142 
143     if (env->priv < PRV_M && !get_field(env->mcounteren, ctr_mask)) {
144         return RISCV_EXCP_ILLEGAL_INST;
145     }
146 
147     if (env->virt_enabled) {
148         if (!get_field(env->hcounteren, ctr_mask) ||
149             (env->priv == PRV_U && !get_field(env->scounteren, ctr_mask))) {
150             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
151         }
152     }
153 
154     if (riscv_has_ext(env, RVS) && env->priv == PRV_U &&
155         !get_field(env->scounteren, ctr_mask)) {
156         return RISCV_EXCP_ILLEGAL_INST;
157     }
158 
159 #endif
160     return RISCV_EXCP_NONE;
161 }
162 
163 static RISCVException ctr32(CPURISCVState *env, int csrno)
164 {
165     if (riscv_cpu_mxl(env) != MXL_RV32) {
166         return RISCV_EXCP_ILLEGAL_INST;
167     }
168 
169     return ctr(env, csrno);
170 }
171 
172 static RISCVException zcmt(CPURISCVState *env, int csrno)
173 {
174     if (!riscv_cpu_cfg(env)->ext_zcmt) {
175         return RISCV_EXCP_ILLEGAL_INST;
176     }
177 
178 #if !defined(CONFIG_USER_ONLY)
179     RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_JVT);
180     if (ret != RISCV_EXCP_NONE) {
181         return ret;
182     }
183 #endif
184 
185     return RISCV_EXCP_NONE;
186 }
187 
188 #if !defined(CONFIG_USER_ONLY)
189 static RISCVException mctr(CPURISCVState *env, int csrno)
190 {
191     RISCVCPU *cpu = env_archcpu(env);
192     uint32_t pmu_avail_ctrs = cpu->pmu_avail_ctrs;
193     int ctr_index;
194     int base_csrno = CSR_MHPMCOUNTER3;
195 
196     if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) {
197         /* Offset for RV32 mhpmcounternh counters */
198         csrno -= 0x80;
199     }
200 
201     g_assert(csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31);
202 
203     ctr_index = csrno - base_csrno;
204     if ((BIT(ctr_index) & pmu_avail_ctrs >> 3) == 0) {
205         /* The PMU is not enabled or counter is out of range */
206         return RISCV_EXCP_ILLEGAL_INST;
207     }
208 
209     return RISCV_EXCP_NONE;
210 }
211 
212 static RISCVException mctr32(CPURISCVState *env, int csrno)
213 {
214     if (riscv_cpu_mxl(env) != MXL_RV32) {
215         return RISCV_EXCP_ILLEGAL_INST;
216     }
217 
218     return mctr(env, csrno);
219 }
220 
221 static RISCVException sscofpmf(CPURISCVState *env, int csrno)
222 {
223     if (!riscv_cpu_cfg(env)->ext_sscofpmf) {
224         return RISCV_EXCP_ILLEGAL_INST;
225     }
226 
227     return RISCV_EXCP_NONE;
228 }
229 
230 static RISCVException any(CPURISCVState *env, int csrno)
231 {
232     return RISCV_EXCP_NONE;
233 }
234 
235 static RISCVException any32(CPURISCVState *env, int csrno)
236 {
237     if (riscv_cpu_mxl(env) != MXL_RV32) {
238         return RISCV_EXCP_ILLEGAL_INST;
239     }
240 
241     return any(env, csrno);
242 
243 }
244 
245 static RISCVException aia_any(CPURISCVState *env, int csrno)
246 {
247     if (!riscv_cpu_cfg(env)->ext_smaia) {
248         return RISCV_EXCP_ILLEGAL_INST;
249     }
250 
251     return any(env, csrno);
252 }
253 
254 static RISCVException aia_any32(CPURISCVState *env, int csrno)
255 {
256     if (!riscv_cpu_cfg(env)->ext_smaia) {
257         return RISCV_EXCP_ILLEGAL_INST;
258     }
259 
260     return any32(env, csrno);
261 }
262 
263 static RISCVException smode(CPURISCVState *env, int csrno)
264 {
265     if (riscv_has_ext(env, RVS)) {
266         return RISCV_EXCP_NONE;
267     }
268 
269     return RISCV_EXCP_ILLEGAL_INST;
270 }
271 
272 static RISCVException smode32(CPURISCVState *env, int csrno)
273 {
274     if (riscv_cpu_mxl(env) != MXL_RV32) {
275         return RISCV_EXCP_ILLEGAL_INST;
276     }
277 
278     return smode(env, csrno);
279 }
280 
281 static RISCVException aia_smode(CPURISCVState *env, int csrno)
282 {
283     if (!riscv_cpu_cfg(env)->ext_ssaia) {
284         return RISCV_EXCP_ILLEGAL_INST;
285     }
286 
287     return smode(env, csrno);
288 }
289 
290 static RISCVException aia_smode32(CPURISCVState *env, int csrno)
291 {
292     if (!riscv_cpu_cfg(env)->ext_ssaia) {
293         return RISCV_EXCP_ILLEGAL_INST;
294     }
295 
296     return smode32(env, csrno);
297 }
298 
299 static RISCVException hmode(CPURISCVState *env, int csrno)
300 {
301     if (riscv_has_ext(env, RVH)) {
302         return RISCV_EXCP_NONE;
303     }
304 
305     return RISCV_EXCP_ILLEGAL_INST;
306 }
307 
308 static RISCVException hmode32(CPURISCVState *env, int csrno)
309 {
310     if (riscv_cpu_mxl(env) != MXL_RV32) {
311         return RISCV_EXCP_ILLEGAL_INST;
312     }
313 
314     return hmode(env, csrno);
315 
316 }
317 
318 static RISCVException umode(CPURISCVState *env, int csrno)
319 {
320     if (riscv_has_ext(env, RVU)) {
321         return RISCV_EXCP_NONE;
322     }
323 
324     return RISCV_EXCP_ILLEGAL_INST;
325 }
326 
327 static RISCVException umode32(CPURISCVState *env, int csrno)
328 {
329     if (riscv_cpu_mxl(env) != MXL_RV32) {
330         return RISCV_EXCP_ILLEGAL_INST;
331     }
332 
333     return umode(env, csrno);
334 }
335 
336 static RISCVException mstateen(CPURISCVState *env, int csrno)
337 {
338     if (!riscv_cpu_cfg(env)->ext_smstateen) {
339         return RISCV_EXCP_ILLEGAL_INST;
340     }
341 
342     return any(env, csrno);
343 }
344 
345 static RISCVException hstateen_pred(CPURISCVState *env, int csrno, int base)
346 {
347     if (!riscv_cpu_cfg(env)->ext_smstateen) {
348         return RISCV_EXCP_ILLEGAL_INST;
349     }
350 
351     RISCVException ret = hmode(env, csrno);
352     if (ret != RISCV_EXCP_NONE) {
353         return ret;
354     }
355 
356     if (env->debugger) {
357         return RISCV_EXCP_NONE;
358     }
359 
360     if (env->priv < PRV_M) {
361         if (!(env->mstateen[csrno - base] & SMSTATEEN_STATEEN)) {
362             return RISCV_EXCP_ILLEGAL_INST;
363         }
364     }
365 
366     return RISCV_EXCP_NONE;
367 }
368 
369 static RISCVException hstateen(CPURISCVState *env, int csrno)
370 {
371     return hstateen_pred(env, csrno, CSR_HSTATEEN0);
372 }
373 
374 static RISCVException hstateenh(CPURISCVState *env, int csrno)
375 {
376     return hstateen_pred(env, csrno, CSR_HSTATEEN0H);
377 }
378 
379 static RISCVException sstateen(CPURISCVState *env, int csrno)
380 {
381     bool virt = env->virt_enabled;
382     int index = csrno - CSR_SSTATEEN0;
383 
384     if (!riscv_cpu_cfg(env)->ext_smstateen) {
385         return RISCV_EXCP_ILLEGAL_INST;
386     }
387 
388     RISCVException ret = smode(env, csrno);
389     if (ret != RISCV_EXCP_NONE) {
390         return ret;
391     }
392 
393     if (env->debugger) {
394         return RISCV_EXCP_NONE;
395     }
396 
397     if (env->priv < PRV_M) {
398         if (!(env->mstateen[index] & SMSTATEEN_STATEEN)) {
399             return RISCV_EXCP_ILLEGAL_INST;
400         }
401 
402         if (virt) {
403             if (!(env->hstateen[index] & SMSTATEEN_STATEEN)) {
404                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
405             }
406         }
407     }
408 
409     return RISCV_EXCP_NONE;
410 }
411 
412 static RISCVException sstc(CPURISCVState *env, int csrno)
413 {
414     bool hmode_check = false;
415 
416     if (!riscv_cpu_cfg(env)->ext_sstc || !env->rdtime_fn) {
417         return RISCV_EXCP_ILLEGAL_INST;
418     }
419 
420     if ((csrno == CSR_VSTIMECMP) || (csrno == CSR_VSTIMECMPH)) {
421         hmode_check = true;
422     }
423 
424     RISCVException ret = hmode_check ? hmode(env, csrno) : smode(env, csrno);
425     if (ret != RISCV_EXCP_NONE) {
426         return ret;
427     }
428 
429     if (env->debugger) {
430         return RISCV_EXCP_NONE;
431     }
432 
433     if (env->priv == PRV_M) {
434         return RISCV_EXCP_NONE;
435     }
436 
437     /*
438      * No need of separate function for rv32 as menvcfg stores both menvcfg
439      * menvcfgh for RV32.
440      */
441     if (!(get_field(env->mcounteren, COUNTEREN_TM) &&
442           get_field(env->menvcfg, MENVCFG_STCE))) {
443         return RISCV_EXCP_ILLEGAL_INST;
444     }
445 
446     if (env->virt_enabled) {
447         if (!(get_field(env->hcounteren, COUNTEREN_TM) &&
448               get_field(env->henvcfg, HENVCFG_STCE))) {
449             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
450         }
451     }
452 
453     return RISCV_EXCP_NONE;
454 }
455 
456 static RISCVException sstc_32(CPURISCVState *env, int csrno)
457 {
458     if (riscv_cpu_mxl(env) != MXL_RV32) {
459         return RISCV_EXCP_ILLEGAL_INST;
460     }
461 
462     return sstc(env, csrno);
463 }
464 
465 static RISCVException satp(CPURISCVState *env, int csrno)
466 {
467     if (env->priv == PRV_S && !env->virt_enabled &&
468         get_field(env->mstatus, MSTATUS_TVM)) {
469         return RISCV_EXCP_ILLEGAL_INST;
470     }
471     if (env->priv == PRV_S && env->virt_enabled &&
472         get_field(env->hstatus, HSTATUS_VTVM)) {
473         return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
474     }
475 
476     return smode(env, csrno);
477 }
478 
479 static RISCVException hgatp(CPURISCVState *env, int csrno)
480 {
481     if (env->priv == PRV_S && !env->virt_enabled &&
482         get_field(env->mstatus, MSTATUS_TVM)) {
483         return RISCV_EXCP_ILLEGAL_INST;
484     }
485 
486     return hmode(env, csrno);
487 }
488 
489 /* Checks if PointerMasking registers could be accessed */
490 static RISCVException pointer_masking(CPURISCVState *env, int csrno)
491 {
492     /* Check if j-ext is present */
493     if (riscv_has_ext(env, RVJ)) {
494         return RISCV_EXCP_NONE;
495     }
496     return RISCV_EXCP_ILLEGAL_INST;
497 }
498 
499 static RISCVException aia_hmode(CPURISCVState *env, int csrno)
500 {
501     if (!riscv_cpu_cfg(env)->ext_ssaia) {
502         return RISCV_EXCP_ILLEGAL_INST;
503      }
504 
505      return hmode(env, csrno);
506 }
507 
508 static RISCVException aia_hmode32(CPURISCVState *env, int csrno)
509 {
510     if (!riscv_cpu_cfg(env)->ext_ssaia) {
511         return RISCV_EXCP_ILLEGAL_INST;
512     }
513 
514     return hmode32(env, csrno);
515 }
516 
517 static RISCVException pmp(CPURISCVState *env, int csrno)
518 {
519     if (riscv_cpu_cfg(env)->pmp) {
520         if (csrno <= CSR_PMPCFG3) {
521             uint32_t reg_index = csrno - CSR_PMPCFG0;
522 
523             /* TODO: RV128 restriction check */
524             if ((reg_index & 1) && (riscv_cpu_mxl(env) == MXL_RV64)) {
525                 return RISCV_EXCP_ILLEGAL_INST;
526             }
527         }
528 
529         return RISCV_EXCP_NONE;
530     }
531 
532     return RISCV_EXCP_ILLEGAL_INST;
533 }
534 
535 static RISCVException have_mseccfg(CPURISCVState *env, int csrno)
536 {
537     if (riscv_cpu_cfg(env)->ext_smepmp) {
538         return RISCV_EXCP_NONE;
539     }
540     if (riscv_cpu_cfg(env)->ext_zkr) {
541         return RISCV_EXCP_NONE;
542     }
543 
544     return RISCV_EXCP_ILLEGAL_INST;
545 }
546 
547 static RISCVException debug(CPURISCVState *env, int csrno)
548 {
549     if (riscv_cpu_cfg(env)->debug) {
550         return RISCV_EXCP_NONE;
551     }
552 
553     return RISCV_EXCP_ILLEGAL_INST;
554 }
555 #endif
556 
557 static RISCVException seed(CPURISCVState *env, int csrno)
558 {
559     if (!riscv_cpu_cfg(env)->ext_zkr) {
560         return RISCV_EXCP_ILLEGAL_INST;
561     }
562 
563 #if !defined(CONFIG_USER_ONLY)
564     if (env->debugger) {
565         return RISCV_EXCP_NONE;
566     }
567 
568     /*
569      * With a CSR read-write instruction:
570      * 1) The seed CSR is always available in machine mode as normal.
571      * 2) Attempted access to seed from virtual modes VS and VU always raises
572      * an exception(virtual instruction exception only if mseccfg.sseed=1).
573      * 3) Without the corresponding access control bit set to 1, any attempted
574      * access to seed from U, S or HS modes will raise an illegal instruction
575      * exception.
576      */
577     if (env->priv == PRV_M) {
578         return RISCV_EXCP_NONE;
579     } else if (env->virt_enabled) {
580         if (env->mseccfg & MSECCFG_SSEED) {
581             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
582         } else {
583             return RISCV_EXCP_ILLEGAL_INST;
584         }
585     } else {
586         if (env->priv == PRV_S && (env->mseccfg & MSECCFG_SSEED)) {
587             return RISCV_EXCP_NONE;
588         } else if (env->priv == PRV_U && (env->mseccfg & MSECCFG_USEED)) {
589             return RISCV_EXCP_NONE;
590         } else {
591             return RISCV_EXCP_ILLEGAL_INST;
592         }
593     }
594 #else
595     return RISCV_EXCP_NONE;
596 #endif
597 }
598 
599 /* User Floating-Point CSRs */
600 static RISCVException read_fflags(CPURISCVState *env, int csrno,
601                                   target_ulong *val)
602 {
603     *val = riscv_cpu_get_fflags(env);
604     return RISCV_EXCP_NONE;
605 }
606 
607 static RISCVException write_fflags(CPURISCVState *env, int csrno,
608                                    target_ulong val)
609 {
610 #if !defined(CONFIG_USER_ONLY)
611     if (riscv_has_ext(env, RVF)) {
612         env->mstatus |= MSTATUS_FS;
613     }
614 #endif
615     riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
616     return RISCV_EXCP_NONE;
617 }
618 
619 static RISCVException read_frm(CPURISCVState *env, int csrno,
620                                target_ulong *val)
621 {
622     *val = env->frm;
623     return RISCV_EXCP_NONE;
624 }
625 
626 static RISCVException write_frm(CPURISCVState *env, int csrno,
627                                 target_ulong val)
628 {
629 #if !defined(CONFIG_USER_ONLY)
630     if (riscv_has_ext(env, RVF)) {
631         env->mstatus |= MSTATUS_FS;
632     }
633 #endif
634     env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
635     return RISCV_EXCP_NONE;
636 }
637 
638 static RISCVException read_fcsr(CPURISCVState *env, int csrno,
639                                 target_ulong *val)
640 {
641     *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
642         | (env->frm << FSR_RD_SHIFT);
643     return RISCV_EXCP_NONE;
644 }
645 
646 static RISCVException write_fcsr(CPURISCVState *env, int csrno,
647                                  target_ulong val)
648 {
649 #if !defined(CONFIG_USER_ONLY)
650     if (riscv_has_ext(env, RVF)) {
651         env->mstatus |= MSTATUS_FS;
652     }
653 #endif
654     env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
655     riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
656     return RISCV_EXCP_NONE;
657 }
658 
659 static RISCVException read_vtype(CPURISCVState *env, int csrno,
660                                  target_ulong *val)
661 {
662     uint64_t vill;
663     switch (env->xl) {
664     case MXL_RV32:
665         vill = (uint32_t)env->vill << 31;
666         break;
667     case MXL_RV64:
668         vill = (uint64_t)env->vill << 63;
669         break;
670     default:
671         g_assert_not_reached();
672     }
673     *val = (target_ulong)vill | env->vtype;
674     return RISCV_EXCP_NONE;
675 }
676 
677 static RISCVException read_vl(CPURISCVState *env, int csrno,
678                               target_ulong *val)
679 {
680     *val = env->vl;
681     return RISCV_EXCP_NONE;
682 }
683 
684 static RISCVException read_vlenb(CPURISCVState *env, int csrno,
685                                  target_ulong *val)
686 {
687     *val = riscv_cpu_cfg(env)->vlenb;
688     return RISCV_EXCP_NONE;
689 }
690 
691 static RISCVException read_vxrm(CPURISCVState *env, int csrno,
692                                 target_ulong *val)
693 {
694     *val = env->vxrm;
695     return RISCV_EXCP_NONE;
696 }
697 
698 static RISCVException write_vxrm(CPURISCVState *env, int csrno,
699                                  target_ulong val)
700 {
701 #if !defined(CONFIG_USER_ONLY)
702     env->mstatus |= MSTATUS_VS;
703 #endif
704     env->vxrm = val;
705     return RISCV_EXCP_NONE;
706 }
707 
708 static RISCVException read_vxsat(CPURISCVState *env, int csrno,
709                                  target_ulong *val)
710 {
711     *val = env->vxsat;
712     return RISCV_EXCP_NONE;
713 }
714 
715 static RISCVException write_vxsat(CPURISCVState *env, int csrno,
716                                   target_ulong val)
717 {
718 #if !defined(CONFIG_USER_ONLY)
719     env->mstatus |= MSTATUS_VS;
720 #endif
721     env->vxsat = val;
722     return RISCV_EXCP_NONE;
723 }
724 
725 static RISCVException read_vstart(CPURISCVState *env, int csrno,
726                                   target_ulong *val)
727 {
728     *val = env->vstart;
729     return RISCV_EXCP_NONE;
730 }
731 
732 static RISCVException write_vstart(CPURISCVState *env, int csrno,
733                                    target_ulong val)
734 {
735 #if !defined(CONFIG_USER_ONLY)
736     env->mstatus |= MSTATUS_VS;
737 #endif
738     /*
739      * The vstart CSR is defined to have only enough writable bits
740      * to hold the largest element index, i.e. lg2(VLEN) bits.
741      */
742     env->vstart = val & ~(~0ULL << ctzl(riscv_cpu_cfg(env)->vlenb << 3));
743     return RISCV_EXCP_NONE;
744 }
745 
746 static RISCVException read_vcsr(CPURISCVState *env, int csrno,
747                                 target_ulong *val)
748 {
749     *val = (env->vxrm << VCSR_VXRM_SHIFT) | (env->vxsat << VCSR_VXSAT_SHIFT);
750     return RISCV_EXCP_NONE;
751 }
752 
753 static RISCVException write_vcsr(CPURISCVState *env, int csrno,
754                                  target_ulong val)
755 {
756 #if !defined(CONFIG_USER_ONLY)
757     env->mstatus |= MSTATUS_VS;
758 #endif
759     env->vxrm = (val & VCSR_VXRM) >> VCSR_VXRM_SHIFT;
760     env->vxsat = (val & VCSR_VXSAT) >> VCSR_VXSAT_SHIFT;
761     return RISCV_EXCP_NONE;
762 }
763 
764 /* User Timers and Counters */
765 static target_ulong get_ticks(bool shift)
766 {
767     int64_t val;
768     target_ulong result;
769 
770 #if !defined(CONFIG_USER_ONLY)
771     if (icount_enabled()) {
772         val = icount_get();
773     } else {
774         val = cpu_get_host_ticks();
775     }
776 #else
777     val = cpu_get_host_ticks();
778 #endif
779 
780     if (shift) {
781         result = val >> 32;
782     } else {
783         result = val;
784     }
785 
786     return result;
787 }
788 
789 #if defined(CONFIG_USER_ONLY)
790 static RISCVException read_time(CPURISCVState *env, int csrno,
791                                 target_ulong *val)
792 {
793     *val = cpu_get_host_ticks();
794     return RISCV_EXCP_NONE;
795 }
796 
797 static RISCVException read_timeh(CPURISCVState *env, int csrno,
798                                  target_ulong *val)
799 {
800     *val = cpu_get_host_ticks() >> 32;
801     return RISCV_EXCP_NONE;
802 }
803 
804 static RISCVException read_hpmcounter(CPURISCVState *env, int csrno,
805                                       target_ulong *val)
806 {
807     *val = get_ticks(false);
808     return RISCV_EXCP_NONE;
809 }
810 
811 static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno,
812                                        target_ulong *val)
813 {
814     *val = get_ticks(true);
815     return RISCV_EXCP_NONE;
816 }
817 
818 #else /* CONFIG_USER_ONLY */
819 
820 static RISCVException read_mhpmevent(CPURISCVState *env, int csrno,
821                                      target_ulong *val)
822 {
823     int evt_index = csrno - CSR_MCOUNTINHIBIT;
824 
825     *val = env->mhpmevent_val[evt_index];
826 
827     return RISCV_EXCP_NONE;
828 }
829 
830 static RISCVException write_mhpmevent(CPURISCVState *env, int csrno,
831                                       target_ulong val)
832 {
833     int evt_index = csrno - CSR_MCOUNTINHIBIT;
834     uint64_t mhpmevt_val = val;
835 
836     env->mhpmevent_val[evt_index] = val;
837 
838     if (riscv_cpu_mxl(env) == MXL_RV32) {
839         mhpmevt_val = mhpmevt_val |
840                       ((uint64_t)env->mhpmeventh_val[evt_index] << 32);
841     }
842     riscv_pmu_update_event_map(env, mhpmevt_val, evt_index);
843 
844     return RISCV_EXCP_NONE;
845 }
846 
847 static RISCVException read_mhpmeventh(CPURISCVState *env, int csrno,
848                                       target_ulong *val)
849 {
850     int evt_index = csrno - CSR_MHPMEVENT3H + 3;
851 
852     *val = env->mhpmeventh_val[evt_index];
853 
854     return RISCV_EXCP_NONE;
855 }
856 
857 static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
858                                        target_ulong val)
859 {
860     int evt_index = csrno - CSR_MHPMEVENT3H + 3;
861     uint64_t mhpmevth_val = val;
862     uint64_t mhpmevt_val = env->mhpmevent_val[evt_index];
863 
864     mhpmevt_val = mhpmevt_val | (mhpmevth_val << 32);
865     env->mhpmeventh_val[evt_index] = val;
866 
867     riscv_pmu_update_event_map(env, mhpmevt_val, evt_index);
868 
869     return RISCV_EXCP_NONE;
870 }
871 
872 static RISCVException write_mhpmcounter(CPURISCVState *env, int csrno,
873                                         target_ulong val)
874 {
875     int ctr_idx = csrno - CSR_MCYCLE;
876     PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
877     uint64_t mhpmctr_val = val;
878 
879     counter->mhpmcounter_val = val;
880     if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
881         riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
882         counter->mhpmcounter_prev = get_ticks(false);
883         if (ctr_idx > 2) {
884             if (riscv_cpu_mxl(env) == MXL_RV32) {
885                 mhpmctr_val = mhpmctr_val |
886                               ((uint64_t)counter->mhpmcounterh_val << 32);
887             }
888             riscv_pmu_setup_timer(env, mhpmctr_val, ctr_idx);
889         }
890      } else {
891         /* Other counters can keep incrementing from the given value */
892         counter->mhpmcounter_prev = val;
893     }
894 
895     return RISCV_EXCP_NONE;
896 }
897 
898 static RISCVException write_mhpmcounterh(CPURISCVState *env, int csrno,
899                                          target_ulong val)
900 {
901     int ctr_idx = csrno - CSR_MCYCLEH;
902     PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
903     uint64_t mhpmctr_val = counter->mhpmcounter_val;
904     uint64_t mhpmctrh_val = val;
905 
906     counter->mhpmcounterh_val = val;
907     mhpmctr_val = mhpmctr_val | (mhpmctrh_val << 32);
908     if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
909         riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
910         counter->mhpmcounterh_prev = get_ticks(true);
911         if (ctr_idx > 2) {
912             riscv_pmu_setup_timer(env, mhpmctr_val, ctr_idx);
913         }
914     } else {
915         counter->mhpmcounterh_prev = val;
916     }
917 
918     return RISCV_EXCP_NONE;
919 }
920 
921 static RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
922                                          bool upper_half, uint32_t ctr_idx)
923 {
924     PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
925     target_ulong ctr_prev = upper_half ? counter->mhpmcounterh_prev :
926                                          counter->mhpmcounter_prev;
927     target_ulong ctr_val = upper_half ? counter->mhpmcounterh_val :
928                                         counter->mhpmcounter_val;
929 
930     if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
931         /*
932          * Counter should not increment if inhibit bit is set. We can't really
933          * stop the icount counting. Just return the counter value written by
934          * the supervisor to indicate that counter was not incremented.
935          */
936         if (!counter->started) {
937             *val = ctr_val;
938             return RISCV_EXCP_NONE;
939         } else {
940             /* Mark that the counter has been stopped */
941             counter->started = false;
942         }
943     }
944 
945     /*
946      * The kernel computes the perf delta by subtracting the current value from
947      * the value it initialized previously (ctr_val).
948      */
949     if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
950         riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
951         *val = get_ticks(upper_half) - ctr_prev + ctr_val;
952     } else {
953         *val = ctr_val;
954     }
955 
956     return RISCV_EXCP_NONE;
957 }
958 
959 static RISCVException read_hpmcounter(CPURISCVState *env, int csrno,
960                                       target_ulong *val)
961 {
962     uint16_t ctr_index;
963 
964     if (csrno >= CSR_MCYCLE && csrno <= CSR_MHPMCOUNTER31) {
965         ctr_index = csrno - CSR_MCYCLE;
966     } else if (csrno >= CSR_CYCLE && csrno <= CSR_HPMCOUNTER31) {
967         ctr_index = csrno - CSR_CYCLE;
968     } else {
969         return RISCV_EXCP_ILLEGAL_INST;
970     }
971 
972     return riscv_pmu_read_ctr(env, val, false, ctr_index);
973 }
974 
975 static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno,
976                                        target_ulong *val)
977 {
978     uint16_t ctr_index;
979 
980     if (csrno >= CSR_MCYCLEH && csrno <= CSR_MHPMCOUNTER31H) {
981         ctr_index = csrno - CSR_MCYCLEH;
982     } else if (csrno >= CSR_CYCLEH && csrno <= CSR_HPMCOUNTER31H) {
983         ctr_index = csrno - CSR_CYCLEH;
984     } else {
985         return RISCV_EXCP_ILLEGAL_INST;
986     }
987 
988     return riscv_pmu_read_ctr(env, val, true, ctr_index);
989 }
990 
991 static RISCVException read_scountovf(CPURISCVState *env, int csrno,
992                                      target_ulong *val)
993 {
994     int mhpmevt_start = CSR_MHPMEVENT3 - CSR_MCOUNTINHIBIT;
995     int i;
996     *val = 0;
997     target_ulong *mhpm_evt_val;
998     uint64_t of_bit_mask;
999 
1000     if (riscv_cpu_mxl(env) == MXL_RV32) {
1001         mhpm_evt_val = env->mhpmeventh_val;
1002         of_bit_mask = MHPMEVENTH_BIT_OF;
1003     } else {
1004         mhpm_evt_val = env->mhpmevent_val;
1005         of_bit_mask = MHPMEVENT_BIT_OF;
1006     }
1007 
1008     for (i = mhpmevt_start; i < RV_MAX_MHPMEVENTS; i++) {
1009         if ((get_field(env->mcounteren, BIT(i))) &&
1010             (mhpm_evt_val[i] & of_bit_mask)) {
1011                     *val |= BIT(i);
1012             }
1013     }
1014 
1015     return RISCV_EXCP_NONE;
1016 }
1017 
1018 static RISCVException read_time(CPURISCVState *env, int csrno,
1019                                 target_ulong *val)
1020 {
1021     uint64_t delta = env->virt_enabled ? env->htimedelta : 0;
1022 
1023     if (!env->rdtime_fn) {
1024         return RISCV_EXCP_ILLEGAL_INST;
1025     }
1026 
1027     *val = env->rdtime_fn(env->rdtime_fn_arg) + delta;
1028     return RISCV_EXCP_NONE;
1029 }
1030 
1031 static RISCVException read_timeh(CPURISCVState *env, int csrno,
1032                                  target_ulong *val)
1033 {
1034     uint64_t delta = env->virt_enabled ? env->htimedelta : 0;
1035 
1036     if (!env->rdtime_fn) {
1037         return RISCV_EXCP_ILLEGAL_INST;
1038     }
1039 
1040     *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32;
1041     return RISCV_EXCP_NONE;
1042 }
1043 
1044 static RISCVException read_vstimecmp(CPURISCVState *env, int csrno,
1045                                      target_ulong *val)
1046 {
1047     *val = env->vstimecmp;
1048 
1049     return RISCV_EXCP_NONE;
1050 }
1051 
1052 static RISCVException read_vstimecmph(CPURISCVState *env, int csrno,
1053                                       target_ulong *val)
1054 {
1055     *val = env->vstimecmp >> 32;
1056 
1057     return RISCV_EXCP_NONE;
1058 }
1059 
1060 static RISCVException write_vstimecmp(CPURISCVState *env, int csrno,
1061                                       target_ulong val)
1062 {
1063     if (riscv_cpu_mxl(env) == MXL_RV32) {
1064         env->vstimecmp = deposit64(env->vstimecmp, 0, 32, (uint64_t)val);
1065     } else {
1066         env->vstimecmp = val;
1067     }
1068 
1069     riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
1070                               env->htimedelta, MIP_VSTIP);
1071 
1072     return RISCV_EXCP_NONE;
1073 }
1074 
1075 static RISCVException write_vstimecmph(CPURISCVState *env, int csrno,
1076                                        target_ulong val)
1077 {
1078     env->vstimecmp = deposit64(env->vstimecmp, 32, 32, (uint64_t)val);
1079     riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
1080                               env->htimedelta, MIP_VSTIP);
1081 
1082     return RISCV_EXCP_NONE;
1083 }
1084 
1085 static RISCVException read_stimecmp(CPURISCVState *env, int csrno,
1086                                     target_ulong *val)
1087 {
1088     if (env->virt_enabled) {
1089         *val = env->vstimecmp;
1090     } else {
1091         *val = env->stimecmp;
1092     }
1093 
1094     return RISCV_EXCP_NONE;
1095 }
1096 
1097 static RISCVException read_stimecmph(CPURISCVState *env, int csrno,
1098                                      target_ulong *val)
1099 {
1100     if (env->virt_enabled) {
1101         *val = env->vstimecmp >> 32;
1102     } else {
1103         *val = env->stimecmp >> 32;
1104     }
1105 
1106     return RISCV_EXCP_NONE;
1107 }
1108 
1109 static RISCVException write_stimecmp(CPURISCVState *env, int csrno,
1110                                      target_ulong val)
1111 {
1112     if (env->virt_enabled) {
1113         if (env->hvictl & HVICTL_VTI) {
1114             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1115         }
1116         return write_vstimecmp(env, csrno, val);
1117     }
1118 
1119     if (riscv_cpu_mxl(env) == MXL_RV32) {
1120         env->stimecmp = deposit64(env->stimecmp, 0, 32, (uint64_t)val);
1121     } else {
1122         env->stimecmp = val;
1123     }
1124 
1125     riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, MIP_STIP);
1126 
1127     return RISCV_EXCP_NONE;
1128 }
1129 
1130 static RISCVException write_stimecmph(CPURISCVState *env, int csrno,
1131                                       target_ulong val)
1132 {
1133     if (env->virt_enabled) {
1134         if (env->hvictl & HVICTL_VTI) {
1135             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1136         }
1137         return write_vstimecmph(env, csrno, val);
1138     }
1139 
1140     env->stimecmp = deposit64(env->stimecmp, 32, 32, (uint64_t)val);
1141     riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, MIP_STIP);
1142 
1143     return RISCV_EXCP_NONE;
1144 }
1145 
1146 #define VSTOPI_NUM_SRCS 5
1147 
1148 /*
1149  * All core local interrupts except the fixed ones 0:12. This macro is for
1150  * virtual interrupts logic so please don't change this to avoid messing up
1151  * the whole support, For reference see AIA spec: `5.3 Interrupt filtering and
1152  * virtual interrupts for supervisor level` and `6.3.2 Virtual interrupts for
1153  * VS level`.
1154  */
1155 #define LOCAL_INTERRUPTS   (~0x1FFFULL)
1156 
1157 static const uint64_t delegable_ints =
1158     S_MODE_INTERRUPTS | VS_MODE_INTERRUPTS | MIP_LCOFIP;
1159 static const uint64_t vs_delegable_ints =
1160     (VS_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & ~MIP_LCOFIP;
1161 static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
1162                                      HS_MODE_INTERRUPTS | LOCAL_INTERRUPTS;
1163 #define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \
1164                          (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \
1165                          (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \
1166                          (1ULL << (RISCV_EXCP_BREAKPOINT)) | \
1167                          (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | \
1168                          (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | \
1169                          (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | \
1170                          (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | \
1171                          (1ULL << (RISCV_EXCP_U_ECALL)) | \
1172                          (1ULL << (RISCV_EXCP_S_ECALL)) | \
1173                          (1ULL << (RISCV_EXCP_VS_ECALL)) | \
1174                          (1ULL << (RISCV_EXCP_M_ECALL)) | \
1175                          (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | \
1176                          (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \
1177                          (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \
1178                          (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \
1179                          (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \
1180                          (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
1181                          (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)))
1182 static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &
1183     ~((1ULL << (RISCV_EXCP_S_ECALL)) |
1184       (1ULL << (RISCV_EXCP_VS_ECALL)) |
1185       (1ULL << (RISCV_EXCP_M_ECALL)) |
1186       (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) |
1187       (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) |
1188       (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) |
1189       (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)));
1190 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
1191     SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
1192     SSTATUS_SUM | SSTATUS_MXR | SSTATUS_VS;
1193 
1194 /*
1195  * Spec allows for bits 13:63 to be either read-only or writable.
1196  * So far we have interrupt LCOFIP in that region which is writable.
1197  *
1198  * Also, spec allows to inject virtual interrupts in this region even
1199  * without any hardware interrupts for that interrupt number.
1200  *
1201  * For now interrupt in 13:63 region are all kept writable. 13 being
1202  * LCOFIP and 14:63 being virtual only. Change this in future if we
1203  * introduce more interrupts that are not writable.
1204  */
1205 
1206 /* Bit STIP can be an alias of mip.STIP that's why it's writable in mvip. */
1207 static const uint64_t mvip_writable_mask = MIP_SSIP | MIP_STIP | MIP_SEIP |
1208                                     LOCAL_INTERRUPTS;
1209 static const uint64_t mvien_writable_mask = MIP_SSIP | MIP_SEIP |
1210                                     LOCAL_INTERRUPTS;
1211 
1212 static const uint64_t sip_writable_mask = SIP_SSIP | LOCAL_INTERRUPTS;
1213 static const uint64_t hip_writable_mask = MIP_VSSIP;
1214 static const uint64_t hvip_writable_mask = MIP_VSSIP | MIP_VSTIP |
1215                                     MIP_VSEIP | LOCAL_INTERRUPTS;
1216 static const uint64_t hvien_writable_mask = LOCAL_INTERRUPTS;
1217 
1218 static const uint64_t vsip_writable_mask = MIP_VSSIP | LOCAL_INTERRUPTS;
1219 
1220 const bool valid_vm_1_10_32[16] = {
1221     [VM_1_10_MBARE] = true,
1222     [VM_1_10_SV32] = true
1223 };
1224 
1225 const bool valid_vm_1_10_64[16] = {
1226     [VM_1_10_MBARE] = true,
1227     [VM_1_10_SV39] = true,
1228     [VM_1_10_SV48] = true,
1229     [VM_1_10_SV57] = true
1230 };
1231 
1232 /* Machine Information Registers */
1233 static RISCVException read_zero(CPURISCVState *env, int csrno,
1234                                 target_ulong *val)
1235 {
1236     *val = 0;
1237     return RISCV_EXCP_NONE;
1238 }
1239 
1240 static RISCVException write_ignore(CPURISCVState *env, int csrno,
1241                                    target_ulong val)
1242 {
1243     return RISCV_EXCP_NONE;
1244 }
1245 
1246 static RISCVException read_mvendorid(CPURISCVState *env, int csrno,
1247                                      target_ulong *val)
1248 {
1249     *val = riscv_cpu_cfg(env)->mvendorid;
1250     return RISCV_EXCP_NONE;
1251 }
1252 
1253 static RISCVException read_marchid(CPURISCVState *env, int csrno,
1254                                    target_ulong *val)
1255 {
1256     *val = riscv_cpu_cfg(env)->marchid;
1257     return RISCV_EXCP_NONE;
1258 }
1259 
1260 static RISCVException read_mimpid(CPURISCVState *env, int csrno,
1261                                   target_ulong *val)
1262 {
1263     *val = riscv_cpu_cfg(env)->mimpid;
1264     return RISCV_EXCP_NONE;
1265 }
1266 
1267 static RISCVException read_mhartid(CPURISCVState *env, int csrno,
1268                                    target_ulong *val)
1269 {
1270     *val = env->mhartid;
1271     return RISCV_EXCP_NONE;
1272 }
1273 
1274 /* Machine Trap Setup */
1275 
1276 /* We do not store SD explicitly, only compute it on demand. */
1277 static uint64_t add_status_sd(RISCVMXL xl, uint64_t status)
1278 {
1279     if ((status & MSTATUS_FS) == MSTATUS_FS ||
1280         (status & MSTATUS_VS) == MSTATUS_VS ||
1281         (status & MSTATUS_XS) == MSTATUS_XS) {
1282         switch (xl) {
1283         case MXL_RV32:
1284             return status | MSTATUS32_SD;
1285         case MXL_RV64:
1286             return status | MSTATUS64_SD;
1287         case MXL_RV128:
1288             return MSTATUSH128_SD;
1289         default:
1290             g_assert_not_reached();
1291         }
1292     }
1293     return status;
1294 }
1295 
1296 static RISCVException read_mstatus(CPURISCVState *env, int csrno,
1297                                    target_ulong *val)
1298 {
1299     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus);
1300     return RISCV_EXCP_NONE;
1301 }
1302 
1303 static bool validate_vm(CPURISCVState *env, target_ulong vm)
1304 {
1305     uint64_t mode_supported = riscv_cpu_cfg(env)->satp_mode.map;
1306     return get_field(mode_supported, (1 << vm));
1307 }
1308 
1309 static target_ulong legalize_xatp(CPURISCVState *env, target_ulong old_xatp,
1310                                   target_ulong val)
1311 {
1312     target_ulong mask;
1313     bool vm;
1314     if (riscv_cpu_mxl(env) == MXL_RV32) {
1315         vm = validate_vm(env, get_field(val, SATP32_MODE));
1316         mask = (val ^ old_xatp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
1317     } else {
1318         vm = validate_vm(env, get_field(val, SATP64_MODE));
1319         mask = (val ^ old_xatp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
1320     }
1321 
1322     if (vm && mask) {
1323         /*
1324          * The ISA defines SATP.MODE=Bare as "no translation", but we still
1325          * pass these through QEMU's TLB emulation as it improves
1326          * performance.  Flushing the TLB on SATP writes with paging
1327          * enabled avoids leaking those invalid cached mappings.
1328          */
1329         tlb_flush(env_cpu(env));
1330         return val;
1331     }
1332     return old_xatp;
1333 }
1334 
1335 static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp,
1336                                  target_ulong val)
1337 {
1338     bool valid = false;
1339     target_ulong new_mpp = get_field(val, MSTATUS_MPP);
1340 
1341     switch (new_mpp) {
1342     case PRV_M:
1343         valid = true;
1344         break;
1345     case PRV_S:
1346         valid = riscv_has_ext(env, RVS);
1347         break;
1348     case PRV_U:
1349         valid = riscv_has_ext(env, RVU);
1350         break;
1351     }
1352 
1353     /* Remain field unchanged if new_mpp value is invalid */
1354     if (!valid) {
1355         val = set_field(val, MSTATUS_MPP, old_mpp);
1356     }
1357 
1358     return val;
1359 }
1360 
1361 static RISCVException write_mstatus(CPURISCVState *env, int csrno,
1362                                     target_ulong val)
1363 {
1364     uint64_t mstatus = env->mstatus;
1365     uint64_t mask = 0;
1366     RISCVMXL xl = riscv_cpu_mxl(env);
1367 
1368     /*
1369      * MPP field have been made WARL since priv version 1.11. However,
1370      * legalization for it will not break any software running on 1.10.
1371      */
1372     val = legalize_mpp(env, get_field(mstatus, MSTATUS_MPP), val);
1373 
1374     /* flush tlb on mstatus fields that affect VM */
1375     if ((val ^ mstatus) & MSTATUS_MXR) {
1376         tlb_flush(env_cpu(env));
1377     }
1378     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
1379         MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
1380         MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
1381         MSTATUS_TW;
1382 
1383     if (riscv_has_ext(env, RVF)) {
1384         mask |= MSTATUS_FS;
1385     }
1386     if (riscv_has_ext(env, RVV)) {
1387         mask |= MSTATUS_VS;
1388     }
1389 
1390     if (xl != MXL_RV32 || env->debugger) {
1391         if (riscv_has_ext(env, RVH)) {
1392             mask |= MSTATUS_MPV | MSTATUS_GVA;
1393         }
1394         if ((val & MSTATUS64_UXL) != 0) {
1395             mask |= MSTATUS64_UXL;
1396         }
1397     }
1398 
1399     mstatus = (mstatus & ~mask) | (val & mask);
1400 
1401     env->mstatus = mstatus;
1402 
1403     /*
1404      * Except in debug mode, UXL/SXL can only be modified by higher
1405      * privilege mode. So xl will not be changed in normal mode.
1406      */
1407     if (env->debugger) {
1408         env->xl = cpu_recompute_xl(env);
1409     }
1410 
1411     riscv_cpu_update_mask(env);
1412     return RISCV_EXCP_NONE;
1413 }
1414 
1415 static RISCVException read_mstatush(CPURISCVState *env, int csrno,
1416                                     target_ulong *val)
1417 {
1418     *val = env->mstatus >> 32;
1419     return RISCV_EXCP_NONE;
1420 }
1421 
1422 static RISCVException write_mstatush(CPURISCVState *env, int csrno,
1423                                      target_ulong val)
1424 {
1425     uint64_t valh = (uint64_t)val << 32;
1426     uint64_t mask = riscv_has_ext(env, RVH) ? MSTATUS_MPV | MSTATUS_GVA : 0;
1427 
1428     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
1429 
1430     return RISCV_EXCP_NONE;
1431 }
1432 
1433 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno,
1434                                         Int128 *val)
1435 {
1436     *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128,
1437                                                       env->mstatus));
1438     return RISCV_EXCP_NONE;
1439 }
1440 
1441 static RISCVException read_misa_i128(CPURISCVState *env, int csrno,
1442                                      Int128 *val)
1443 {
1444     *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62);
1445     return RISCV_EXCP_NONE;
1446 }
1447 
1448 static RISCVException read_misa(CPURISCVState *env, int csrno,
1449                                 target_ulong *val)
1450 {
1451     target_ulong misa;
1452 
1453     switch (env->misa_mxl) {
1454     case MXL_RV32:
1455         misa = (target_ulong)MXL_RV32 << 30;
1456         break;
1457 #ifdef TARGET_RISCV64
1458     case MXL_RV64:
1459         misa = (target_ulong)MXL_RV64 << 62;
1460         break;
1461 #endif
1462     default:
1463         g_assert_not_reached();
1464     }
1465 
1466     *val = misa | env->misa_ext;
1467     return RISCV_EXCP_NONE;
1468 }
1469 
1470 static RISCVException write_misa(CPURISCVState *env, int csrno,
1471                                  target_ulong val)
1472 {
1473     RISCVCPU *cpu = env_archcpu(env);
1474     uint32_t orig_misa_ext = env->misa_ext;
1475     Error *local_err = NULL;
1476 
1477     if (!riscv_cpu_cfg(env)->misa_w) {
1478         /* drop write to misa */
1479         return RISCV_EXCP_NONE;
1480     }
1481 
1482     /* Mask extensions that are not supported by this hart */
1483     val &= env->misa_ext_mask;
1484 
1485     /*
1486      * Suppress 'C' if next instruction is not aligned
1487      * TODO: this should check next_pc
1488      */
1489     if ((val & RVC) && (GETPC() & ~3) != 0) {
1490         val &= ~RVC;
1491     }
1492 
1493     /* Disable RVG if any of its dependencies are disabled */
1494     if (!(val & RVI && val & RVM && val & RVA &&
1495           val & RVF && val & RVD)) {
1496         val &= ~RVG;
1497     }
1498 
1499     /* If nothing changed, do nothing. */
1500     if (val == env->misa_ext) {
1501         return RISCV_EXCP_NONE;
1502     }
1503 
1504     env->misa_ext = val;
1505     riscv_cpu_validate_set_extensions(cpu, &local_err);
1506     if (local_err != NULL) {
1507         /* Rollback on validation error */
1508         qemu_log_mask(LOG_GUEST_ERROR, "Unable to write MISA ext value "
1509                       "0x%x, keeping existing MISA ext 0x%x\n",
1510                       env->misa_ext, orig_misa_ext);
1511 
1512         env->misa_ext = orig_misa_ext;
1513 
1514         return RISCV_EXCP_NONE;
1515     }
1516 
1517     if (!(env->misa_ext & RVF)) {
1518         env->mstatus &= ~MSTATUS_FS;
1519     }
1520 
1521     /* flush translation cache */
1522     tb_flush(env_cpu(env));
1523     env->xl = riscv_cpu_mxl(env);
1524     return RISCV_EXCP_NONE;
1525 }
1526 
1527 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
1528                                    target_ulong *val)
1529 {
1530     *val = env->medeleg;
1531     return RISCV_EXCP_NONE;
1532 }
1533 
1534 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
1535                                     target_ulong val)
1536 {
1537     env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
1538     return RISCV_EXCP_NONE;
1539 }
1540 
1541 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno,
1542                                     uint64_t *ret_val,
1543                                     uint64_t new_val, uint64_t wr_mask)
1544 {
1545     uint64_t mask = wr_mask & delegable_ints;
1546 
1547     if (ret_val) {
1548         *ret_val = env->mideleg;
1549     }
1550 
1551     env->mideleg = (env->mideleg & ~mask) | (new_val & mask);
1552 
1553     if (riscv_has_ext(env, RVH)) {
1554         env->mideleg |= HS_MODE_INTERRUPTS;
1555     }
1556 
1557     return RISCV_EXCP_NONE;
1558 }
1559 
1560 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno,
1561                                   target_ulong *ret_val,
1562                                   target_ulong new_val, target_ulong wr_mask)
1563 {
1564     uint64_t rval;
1565     RISCVException ret;
1566 
1567     ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask);
1568     if (ret_val) {
1569         *ret_val = rval;
1570     }
1571 
1572     return ret;
1573 }
1574 
1575 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno,
1576                                    target_ulong *ret_val,
1577                                    target_ulong new_val,
1578                                    target_ulong wr_mask)
1579 {
1580     uint64_t rval;
1581     RISCVException ret;
1582 
1583     ret = rmw_mideleg64(env, csrno, &rval,
1584         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1585     if (ret_val) {
1586         *ret_val = rval >> 32;
1587     }
1588 
1589     return ret;
1590 }
1591 
1592 static RISCVException rmw_mie64(CPURISCVState *env, int csrno,
1593                                 uint64_t *ret_val,
1594                                 uint64_t new_val, uint64_t wr_mask)
1595 {
1596     uint64_t mask = wr_mask & all_ints;
1597 
1598     if (ret_val) {
1599         *ret_val = env->mie;
1600     }
1601 
1602     env->mie = (env->mie & ~mask) | (new_val & mask);
1603 
1604     if (!riscv_has_ext(env, RVH)) {
1605         env->mie &= ~((uint64_t)HS_MODE_INTERRUPTS);
1606     }
1607 
1608     return RISCV_EXCP_NONE;
1609 }
1610 
1611 static RISCVException rmw_mie(CPURISCVState *env, int csrno,
1612                               target_ulong *ret_val,
1613                               target_ulong new_val, target_ulong wr_mask)
1614 {
1615     uint64_t rval;
1616     RISCVException ret;
1617 
1618     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask);
1619     if (ret_val) {
1620         *ret_val = rval;
1621     }
1622 
1623     return ret;
1624 }
1625 
1626 static RISCVException rmw_mieh(CPURISCVState *env, int csrno,
1627                                target_ulong *ret_val,
1628                                target_ulong new_val, target_ulong wr_mask)
1629 {
1630     uint64_t rval;
1631     RISCVException ret;
1632 
1633     ret = rmw_mie64(env, csrno, &rval,
1634         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1635     if (ret_val) {
1636         *ret_val = rval >> 32;
1637     }
1638 
1639     return ret;
1640 }
1641 
1642 static RISCVException rmw_mvien64(CPURISCVState *env, int csrno,
1643                                 uint64_t *ret_val,
1644                                 uint64_t new_val, uint64_t wr_mask)
1645 {
1646     uint64_t mask = wr_mask & mvien_writable_mask;
1647 
1648     if (ret_val) {
1649         *ret_val = env->mvien;
1650     }
1651 
1652     env->mvien = (env->mvien & ~mask) | (new_val & mask);
1653 
1654     return RISCV_EXCP_NONE;
1655 }
1656 
1657 static RISCVException rmw_mvien(CPURISCVState *env, int csrno,
1658                               target_ulong *ret_val,
1659                               target_ulong new_val, target_ulong wr_mask)
1660 {
1661     uint64_t rval;
1662     RISCVException ret;
1663 
1664     ret = rmw_mvien64(env, csrno, &rval, new_val, wr_mask);
1665     if (ret_val) {
1666         *ret_val = rval;
1667     }
1668 
1669     return ret;
1670 }
1671 
1672 static RISCVException rmw_mvienh(CPURISCVState *env, int csrno,
1673                                 target_ulong *ret_val,
1674                                 target_ulong new_val, target_ulong wr_mask)
1675 {
1676     uint64_t rval;
1677     RISCVException ret;
1678 
1679     ret = rmw_mvien64(env, csrno, &rval,
1680         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1681     if (ret_val) {
1682         *ret_val = rval >> 32;
1683     }
1684 
1685     return ret;
1686 }
1687 
1688 static RISCVException read_mtopi(CPURISCVState *env, int csrno,
1689                                  target_ulong *val)
1690 {
1691     int irq;
1692     uint8_t iprio;
1693 
1694     irq = riscv_cpu_mirq_pending(env);
1695     if (irq <= 0 || irq > 63) {
1696         *val = 0;
1697     } else {
1698         iprio = env->miprio[irq];
1699         if (!iprio) {
1700             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) {
1701                 iprio = IPRIO_MMAXIPRIO;
1702             }
1703         }
1704         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1705         *val |= iprio;
1706     }
1707 
1708     return RISCV_EXCP_NONE;
1709 }
1710 
1711 static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)
1712 {
1713     if (!env->virt_enabled) {
1714         return csrno;
1715     }
1716 
1717     switch (csrno) {
1718     case CSR_SISELECT:
1719         return CSR_VSISELECT;
1720     case CSR_SIREG:
1721         return CSR_VSIREG;
1722     case CSR_STOPEI:
1723         return CSR_VSTOPEI;
1724     default:
1725         return csrno;
1726     };
1727 }
1728 
1729 static RISCVException rmw_xiselect(CPURISCVState *env, int csrno,
1730                                    target_ulong *val, target_ulong new_val,
1731                                    target_ulong wr_mask)
1732 {
1733     target_ulong *iselect;
1734 
1735     /* Translate CSR number for VS-mode */
1736     csrno = aia_xlate_vs_csrno(env, csrno);
1737 
1738     /* Find the iselect CSR based on CSR number */
1739     switch (csrno) {
1740     case CSR_MISELECT:
1741         iselect = &env->miselect;
1742         break;
1743     case CSR_SISELECT:
1744         iselect = &env->siselect;
1745         break;
1746     case CSR_VSISELECT:
1747         iselect = &env->vsiselect;
1748         break;
1749     default:
1750          return RISCV_EXCP_ILLEGAL_INST;
1751     };
1752 
1753     if (val) {
1754         *val = *iselect;
1755     }
1756 
1757     wr_mask &= ISELECT_MASK;
1758     if (wr_mask) {
1759         *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask);
1760     }
1761 
1762     return RISCV_EXCP_NONE;
1763 }
1764 
1765 static int rmw_iprio(target_ulong xlen,
1766                      target_ulong iselect, uint8_t *iprio,
1767                      target_ulong *val, target_ulong new_val,
1768                      target_ulong wr_mask, int ext_irq_no)
1769 {
1770     int i, firq, nirqs;
1771     target_ulong old_val;
1772 
1773     if (iselect < ISELECT_IPRIO0 || ISELECT_IPRIO15 < iselect) {
1774         return -EINVAL;
1775     }
1776     if (xlen != 32 && iselect & 0x1) {
1777         return -EINVAL;
1778     }
1779 
1780     nirqs = 4 * (xlen / 32);
1781     firq = ((iselect - ISELECT_IPRIO0) / (xlen / 32)) * (nirqs);
1782 
1783     old_val = 0;
1784     for (i = 0; i < nirqs; i++) {
1785         old_val |= ((target_ulong)iprio[firq + i]) << (IPRIO_IRQ_BITS * i);
1786     }
1787 
1788     if (val) {
1789         *val = old_val;
1790     }
1791 
1792     if (wr_mask) {
1793         new_val = (old_val & ~wr_mask) | (new_val & wr_mask);
1794         for (i = 0; i < nirqs; i++) {
1795             /*
1796              * M-level and S-level external IRQ priority always read-only
1797              * zero. This means default priority order is always preferred
1798              * for M-level and S-level external IRQs.
1799              */
1800             if ((firq + i) == ext_irq_no) {
1801                 continue;
1802             }
1803             iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) & 0xff;
1804         }
1805     }
1806 
1807     return 0;
1808 }
1809 
1810 static RISCVException rmw_xireg(CPURISCVState *env, int csrno,
1811                                 target_ulong *val, target_ulong new_val,
1812                                 target_ulong wr_mask)
1813 {
1814     bool virt, isel_reserved;
1815     uint8_t *iprio;
1816     int ret = -EINVAL;
1817     target_ulong priv, isel, vgein;
1818 
1819     /* Translate CSR number for VS-mode */
1820     csrno = aia_xlate_vs_csrno(env, csrno);
1821 
1822     /* Decode register details from CSR number */
1823     virt = false;
1824     isel_reserved = false;
1825     switch (csrno) {
1826     case CSR_MIREG:
1827         iprio = env->miprio;
1828         isel = env->miselect;
1829         priv = PRV_M;
1830         break;
1831     case CSR_SIREG:
1832         if (env->priv == PRV_S && env->mvien & MIP_SEIP &&
1833             env->siselect >= ISELECT_IMSIC_EIDELIVERY &&
1834             env->siselect <= ISELECT_IMSIC_EIE63) {
1835             goto done;
1836         }
1837         iprio = env->siprio;
1838         isel = env->siselect;
1839         priv = PRV_S;
1840         break;
1841     case CSR_VSIREG:
1842         iprio = env->hviprio;
1843         isel = env->vsiselect;
1844         priv = PRV_S;
1845         virt = true;
1846         break;
1847     default:
1848          goto done;
1849     };
1850 
1851     /* Find the selected guest interrupt file */
1852     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1853 
1854     if (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) {
1855         /* Local interrupt priority registers not available for VS-mode */
1856         if (!virt) {
1857             ret = rmw_iprio(riscv_cpu_mxl_bits(env),
1858                             isel, iprio, val, new_val, wr_mask,
1859                             (priv == PRV_M) ? IRQ_M_EXT : IRQ_S_EXT);
1860         }
1861     } else if (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST) {
1862         /* IMSIC registers only available when machine implements it. */
1863         if (env->aia_ireg_rmw_fn[priv]) {
1864             /* Selected guest interrupt file should not be zero */
1865             if (virt && (!vgein || env->geilen < vgein)) {
1866                 goto done;
1867             }
1868             /* Call machine specific IMSIC register emulation */
1869             ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1870                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
1871                                                   riscv_cpu_mxl_bits(env)),
1872                                     val, new_val, wr_mask);
1873         }
1874     } else {
1875         isel_reserved = true;
1876     }
1877 
1878 done:
1879     if (ret) {
1880         return (env->virt_enabled && virt && !isel_reserved) ?
1881                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1882     }
1883     return RISCV_EXCP_NONE;
1884 }
1885 
1886 static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
1887                                  target_ulong *val, target_ulong new_val,
1888                                  target_ulong wr_mask)
1889 {
1890     bool virt;
1891     int ret = -EINVAL;
1892     target_ulong priv, vgein;
1893 
1894     /* Translate CSR number for VS-mode */
1895     csrno = aia_xlate_vs_csrno(env, csrno);
1896 
1897     /* Decode register details from CSR number */
1898     virt = false;
1899     switch (csrno) {
1900     case CSR_MTOPEI:
1901         priv = PRV_M;
1902         break;
1903     case CSR_STOPEI:
1904         if (env->mvien & MIP_SEIP && env->priv == PRV_S) {
1905             goto done;
1906         }
1907         priv = PRV_S;
1908         break;
1909     case CSR_VSTOPEI:
1910         priv = PRV_S;
1911         virt = true;
1912         break;
1913     default:
1914         goto done;
1915     };
1916 
1917     /* IMSIC CSRs only available when machine implements IMSIC. */
1918     if (!env->aia_ireg_rmw_fn[priv]) {
1919         goto done;
1920     }
1921 
1922     /* Find the selected guest interrupt file */
1923     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1924 
1925     /* Selected guest interrupt file should be valid */
1926     if (virt && (!vgein || env->geilen < vgein)) {
1927         goto done;
1928     }
1929 
1930     /* Call machine specific IMSIC register emulation for TOPEI */
1931     ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1932                     AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
1933                                   riscv_cpu_mxl_bits(env)),
1934                     val, new_val, wr_mask);
1935 
1936 done:
1937     if (ret) {
1938         return (env->virt_enabled && virt) ?
1939                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1940     }
1941     return RISCV_EXCP_NONE;
1942 }
1943 
1944 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
1945                                  target_ulong *val)
1946 {
1947     *val = env->mtvec;
1948     return RISCV_EXCP_NONE;
1949 }
1950 
1951 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
1952                                   target_ulong val)
1953 {
1954     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
1955     if ((val & 3) < 2) {
1956         env->mtvec = val;
1957     } else {
1958         qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
1959     }
1960     return RISCV_EXCP_NONE;
1961 }
1962 
1963 static RISCVException read_mcountinhibit(CPURISCVState *env, int csrno,
1964                                          target_ulong *val)
1965 {
1966     *val = env->mcountinhibit;
1967     return RISCV_EXCP_NONE;
1968 }
1969 
1970 static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno,
1971                                           target_ulong val)
1972 {
1973     int cidx;
1974     PMUCTRState *counter;
1975     RISCVCPU *cpu = env_archcpu(env);
1976 
1977     /* WARL register - disable unavailable counters; TM bit is always 0 */
1978     env->mcountinhibit =
1979         val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_IR);
1980 
1981     /* Check if any other counter is also monitoring cycles/instructions */
1982     for (cidx = 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) {
1983         if (!get_field(env->mcountinhibit, BIT(cidx))) {
1984             counter = &env->pmu_ctrs[cidx];
1985             counter->started = true;
1986         }
1987     }
1988 
1989     return RISCV_EXCP_NONE;
1990 }
1991 
1992 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
1993                                       target_ulong *val)
1994 {
1995     *val = env->mcounteren;
1996     return RISCV_EXCP_NONE;
1997 }
1998 
1999 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
2000                                        target_ulong val)
2001 {
2002     RISCVCPU *cpu = env_archcpu(env);
2003 
2004     /* WARL register - disable unavailable counters */
2005     env->mcounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM |
2006                              COUNTEREN_IR);
2007     return RISCV_EXCP_NONE;
2008 }
2009 
2010 /* Machine Trap Handling */
2011 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno,
2012                                          Int128 *val)
2013 {
2014     *val = int128_make128(env->mscratch, env->mscratchh);
2015     return RISCV_EXCP_NONE;
2016 }
2017 
2018 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno,
2019                                           Int128 val)
2020 {
2021     env->mscratch = int128_getlo(val);
2022     env->mscratchh = int128_gethi(val);
2023     return RISCV_EXCP_NONE;
2024 }
2025 
2026 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
2027                                     target_ulong *val)
2028 {
2029     *val = env->mscratch;
2030     return RISCV_EXCP_NONE;
2031 }
2032 
2033 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
2034                                      target_ulong val)
2035 {
2036     env->mscratch = val;
2037     return RISCV_EXCP_NONE;
2038 }
2039 
2040 static RISCVException read_mepc(CPURISCVState *env, int csrno,
2041                                 target_ulong *val)
2042 {
2043     *val = env->mepc;
2044     return RISCV_EXCP_NONE;
2045 }
2046 
2047 static RISCVException write_mepc(CPURISCVState *env, int csrno,
2048                                  target_ulong val)
2049 {
2050     env->mepc = val;
2051     return RISCV_EXCP_NONE;
2052 }
2053 
2054 static RISCVException read_mcause(CPURISCVState *env, int csrno,
2055                                   target_ulong *val)
2056 {
2057     *val = env->mcause;
2058     return RISCV_EXCP_NONE;
2059 }
2060 
2061 static RISCVException write_mcause(CPURISCVState *env, int csrno,
2062                                    target_ulong val)
2063 {
2064     env->mcause = val;
2065     return RISCV_EXCP_NONE;
2066 }
2067 
2068 static RISCVException read_mtval(CPURISCVState *env, int csrno,
2069                                  target_ulong *val)
2070 {
2071     *val = env->mtval;
2072     return RISCV_EXCP_NONE;
2073 }
2074 
2075 static RISCVException write_mtval(CPURISCVState *env, int csrno,
2076                                   target_ulong val)
2077 {
2078     env->mtval = val;
2079     return RISCV_EXCP_NONE;
2080 }
2081 
2082 /* Execution environment configuration setup */
2083 static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
2084                                    target_ulong *val)
2085 {
2086     *val = env->menvcfg;
2087     return RISCV_EXCP_NONE;
2088 }
2089 
2090 static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
2091                                     target_ulong val)
2092 {
2093     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
2094     uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE;
2095 
2096     if (riscv_cpu_mxl(env) == MXL_RV64) {
2097         mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
2098                 (cfg->ext_sstc ? MENVCFG_STCE : 0) |
2099                 (cfg->ext_svadu ? MENVCFG_ADUE : 0);
2100     }
2101     env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
2102 
2103     return RISCV_EXCP_NONE;
2104 }
2105 
2106 static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
2107                                     target_ulong *val)
2108 {
2109     *val = env->menvcfg >> 32;
2110     return RISCV_EXCP_NONE;
2111 }
2112 
2113 static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
2114                                      target_ulong val)
2115 {
2116     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
2117     uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
2118                     (cfg->ext_sstc ? MENVCFG_STCE : 0) |
2119                     (cfg->ext_svadu ? MENVCFG_ADUE : 0);
2120     uint64_t valh = (uint64_t)val << 32;
2121 
2122     env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);
2123 
2124     return RISCV_EXCP_NONE;
2125 }
2126 
2127 static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
2128                                    target_ulong *val)
2129 {
2130     RISCVException ret;
2131 
2132     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2133     if (ret != RISCV_EXCP_NONE) {
2134         return ret;
2135     }
2136 
2137     *val = env->senvcfg;
2138     return RISCV_EXCP_NONE;
2139 }
2140 
2141 static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
2142                                     target_ulong val)
2143 {
2144     uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE;
2145     RISCVException ret;
2146 
2147     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2148     if (ret != RISCV_EXCP_NONE) {
2149         return ret;
2150     }
2151 
2152     env->senvcfg = (env->senvcfg & ~mask) | (val & mask);
2153     return RISCV_EXCP_NONE;
2154 }
2155 
2156 static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
2157                                    target_ulong *val)
2158 {
2159     RISCVException ret;
2160 
2161     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2162     if (ret != RISCV_EXCP_NONE) {
2163         return ret;
2164     }
2165 
2166     /*
2167      * henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0
2168      * henvcfg.stce is read_only 0 when menvcfg.stce = 0
2169      * henvcfg.adue is read_only 0 when menvcfg.adue = 0
2170      */
2171     *val = env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE) |
2172                            env->menvcfg);
2173     return RISCV_EXCP_NONE;
2174 }
2175 
2176 static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
2177                                     target_ulong val)
2178 {
2179     uint64_t mask = HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCFG_CBZE;
2180     RISCVException ret;
2181 
2182     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2183     if (ret != RISCV_EXCP_NONE) {
2184         return ret;
2185     }
2186 
2187     if (riscv_cpu_mxl(env) == MXL_RV64) {
2188         mask |= env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE);
2189     }
2190 
2191     env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
2192 
2193     return RISCV_EXCP_NONE;
2194 }
2195 
2196 static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
2197                                     target_ulong *val)
2198 {
2199     RISCVException ret;
2200 
2201     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2202     if (ret != RISCV_EXCP_NONE) {
2203         return ret;
2204     }
2205 
2206     *val = (env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE) |
2207                             env->menvcfg)) >> 32;
2208     return RISCV_EXCP_NONE;
2209 }
2210 
2211 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
2212                                      target_ulong val)
2213 {
2214     uint64_t mask = env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE |
2215                                     HENVCFG_ADUE);
2216     uint64_t valh = (uint64_t)val << 32;
2217     RISCVException ret;
2218 
2219     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2220     if (ret != RISCV_EXCP_NONE) {
2221         return ret;
2222     }
2223 
2224     env->henvcfg = (env->henvcfg & ~mask) | (valh & mask);
2225     return RISCV_EXCP_NONE;
2226 }
2227 
2228 static RISCVException read_mstateen(CPURISCVState *env, int csrno,
2229                                     target_ulong *val)
2230 {
2231     *val = env->mstateen[csrno - CSR_MSTATEEN0];
2232 
2233     return RISCV_EXCP_NONE;
2234 }
2235 
2236 static RISCVException write_mstateen(CPURISCVState *env, int csrno,
2237                                      uint64_t wr_mask, target_ulong new_val)
2238 {
2239     uint64_t *reg;
2240 
2241     reg = &env->mstateen[csrno - CSR_MSTATEEN0];
2242     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2243 
2244     return RISCV_EXCP_NONE;
2245 }
2246 
2247 static RISCVException write_mstateen0(CPURISCVState *env, int csrno,
2248                                       target_ulong new_val)
2249 {
2250     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2251     if (!riscv_has_ext(env, RVF)) {
2252         wr_mask |= SMSTATEEN0_FCSR;
2253     }
2254 
2255     return write_mstateen(env, csrno, wr_mask, new_val);
2256 }
2257 
2258 static RISCVException write_mstateen_1_3(CPURISCVState *env, int csrno,
2259                                          target_ulong new_val)
2260 {
2261     return write_mstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2262 }
2263 
2264 static RISCVException read_mstateenh(CPURISCVState *env, int csrno,
2265                                      target_ulong *val)
2266 {
2267     *val = env->mstateen[csrno - CSR_MSTATEEN0H] >> 32;
2268 
2269     return RISCV_EXCP_NONE;
2270 }
2271 
2272 static RISCVException write_mstateenh(CPURISCVState *env, int csrno,
2273                                       uint64_t wr_mask, target_ulong new_val)
2274 {
2275     uint64_t *reg, val;
2276 
2277     reg = &env->mstateen[csrno - CSR_MSTATEEN0H];
2278     val = (uint64_t)new_val << 32;
2279     val |= *reg & 0xFFFFFFFF;
2280     *reg = (*reg & ~wr_mask) | (val & wr_mask);
2281 
2282     return RISCV_EXCP_NONE;
2283 }
2284 
2285 static RISCVException write_mstateen0h(CPURISCVState *env, int csrno,
2286                                        target_ulong new_val)
2287 {
2288     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2289 
2290     return write_mstateenh(env, csrno, wr_mask, new_val);
2291 }
2292 
2293 static RISCVException write_mstateenh_1_3(CPURISCVState *env, int csrno,
2294                                           target_ulong new_val)
2295 {
2296     return write_mstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2297 }
2298 
2299 static RISCVException read_hstateen(CPURISCVState *env, int csrno,
2300                                     target_ulong *val)
2301 {
2302     int index = csrno - CSR_HSTATEEN0;
2303 
2304     *val = env->hstateen[index] & env->mstateen[index];
2305 
2306     return RISCV_EXCP_NONE;
2307 }
2308 
2309 static RISCVException write_hstateen(CPURISCVState *env, int csrno,
2310                                      uint64_t mask, target_ulong new_val)
2311 {
2312     int index = csrno - CSR_HSTATEEN0;
2313     uint64_t *reg, wr_mask;
2314 
2315     reg = &env->hstateen[index];
2316     wr_mask = env->mstateen[index] & mask;
2317     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2318 
2319     return RISCV_EXCP_NONE;
2320 }
2321 
2322 static RISCVException write_hstateen0(CPURISCVState *env, int csrno,
2323                                       target_ulong new_val)
2324 {
2325     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2326 
2327     if (!riscv_has_ext(env, RVF)) {
2328         wr_mask |= SMSTATEEN0_FCSR;
2329     }
2330 
2331     return write_hstateen(env, csrno, wr_mask, new_val);
2332 }
2333 
2334 static RISCVException write_hstateen_1_3(CPURISCVState *env, int csrno,
2335                                          target_ulong new_val)
2336 {
2337     return write_hstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2338 }
2339 
2340 static RISCVException read_hstateenh(CPURISCVState *env, int csrno,
2341                                      target_ulong *val)
2342 {
2343     int index = csrno - CSR_HSTATEEN0H;
2344 
2345     *val = (env->hstateen[index] >> 32) & (env->mstateen[index] >> 32);
2346 
2347     return RISCV_EXCP_NONE;
2348 }
2349 
2350 static RISCVException write_hstateenh(CPURISCVState *env, int csrno,
2351                                       uint64_t mask, target_ulong new_val)
2352 {
2353     int index = csrno - CSR_HSTATEEN0H;
2354     uint64_t *reg, wr_mask, val;
2355 
2356     reg = &env->hstateen[index];
2357     val = (uint64_t)new_val << 32;
2358     val |= *reg & 0xFFFFFFFF;
2359     wr_mask = env->mstateen[index] & mask;
2360     *reg = (*reg & ~wr_mask) | (val & wr_mask);
2361 
2362     return RISCV_EXCP_NONE;
2363 }
2364 
2365 static RISCVException write_hstateen0h(CPURISCVState *env, int csrno,
2366                                        target_ulong new_val)
2367 {
2368     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2369 
2370     return write_hstateenh(env, csrno, wr_mask, new_val);
2371 }
2372 
2373 static RISCVException write_hstateenh_1_3(CPURISCVState *env, int csrno,
2374                                           target_ulong new_val)
2375 {
2376     return write_hstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2377 }
2378 
2379 static RISCVException read_sstateen(CPURISCVState *env, int csrno,
2380                                     target_ulong *val)
2381 {
2382     bool virt = env->virt_enabled;
2383     int index = csrno - CSR_SSTATEEN0;
2384 
2385     *val = env->sstateen[index] & env->mstateen[index];
2386     if (virt) {
2387         *val &= env->hstateen[index];
2388     }
2389 
2390     return RISCV_EXCP_NONE;
2391 }
2392 
2393 static RISCVException write_sstateen(CPURISCVState *env, int csrno,
2394                                      uint64_t mask, target_ulong new_val)
2395 {
2396     bool virt = env->virt_enabled;
2397     int index = csrno - CSR_SSTATEEN0;
2398     uint64_t wr_mask;
2399     uint64_t *reg;
2400 
2401     wr_mask = env->mstateen[index] & mask;
2402     if (virt) {
2403         wr_mask &= env->hstateen[index];
2404     }
2405 
2406     reg = &env->sstateen[index];
2407     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2408 
2409     return RISCV_EXCP_NONE;
2410 }
2411 
2412 static RISCVException write_sstateen0(CPURISCVState *env, int csrno,
2413                                       target_ulong new_val)
2414 {
2415     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2416 
2417     if (!riscv_has_ext(env, RVF)) {
2418         wr_mask |= SMSTATEEN0_FCSR;
2419     }
2420 
2421     return write_sstateen(env, csrno, wr_mask, new_val);
2422 }
2423 
2424 static RISCVException write_sstateen_1_3(CPURISCVState *env, int csrno,
2425                                       target_ulong new_val)
2426 {
2427     return write_sstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2428 }
2429 
2430 static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
2431                                 uint64_t *ret_val,
2432                                 uint64_t new_val, uint64_t wr_mask)
2433 {
2434     uint64_t old_mip, mask = wr_mask & delegable_ints;
2435     uint32_t gin;
2436 
2437     if (mask & MIP_SEIP) {
2438         env->software_seip = new_val & MIP_SEIP;
2439         new_val |= env->external_seip * MIP_SEIP;
2440     }
2441 
2442     if (riscv_cpu_cfg(env)->ext_sstc && (env->priv == PRV_M) &&
2443         get_field(env->menvcfg, MENVCFG_STCE)) {
2444         /* sstc extension forbids STIP & VSTIP to be writeable in mip */
2445         mask = mask & ~(MIP_STIP | MIP_VSTIP);
2446     }
2447 
2448     if (mask) {
2449         old_mip = riscv_cpu_update_mip(env, mask, (new_val & mask));
2450     } else {
2451         old_mip = env->mip;
2452     }
2453 
2454     if (csrno != CSR_HVIP) {
2455         gin = get_field(env->hstatus, HSTATUS_VGEIN);
2456         old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0;
2457         old_mip |= env->vstime_irq ? MIP_VSTIP : 0;
2458     }
2459 
2460     if (ret_val) {
2461         *ret_val = old_mip;
2462     }
2463 
2464     return RISCV_EXCP_NONE;
2465 }
2466 
2467 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
2468                               target_ulong *ret_val,
2469                               target_ulong new_val, target_ulong wr_mask)
2470 {
2471     uint64_t rval;
2472     RISCVException ret;
2473 
2474     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask);
2475     if (ret_val) {
2476         *ret_val = rval;
2477     }
2478 
2479     return ret;
2480 }
2481 
2482 static RISCVException rmw_miph(CPURISCVState *env, int csrno,
2483                                target_ulong *ret_val,
2484                                target_ulong new_val, target_ulong wr_mask)
2485 {
2486     uint64_t rval;
2487     RISCVException ret;
2488 
2489     ret = rmw_mip64(env, csrno, &rval,
2490         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2491     if (ret_val) {
2492         *ret_val = rval >> 32;
2493     }
2494 
2495     return ret;
2496 }
2497 
2498 /*
2499  * The function is written for two use-cases:
2500  * 1- To access mvip csr as is for m-mode access.
2501  * 2- To access sip as a combination of mip and mvip for s-mode.
2502  *
2503  * Both report bits 1, 5, 9 and 13:63 but with the exception of
2504  * STIP being read-only zero in case of mvip when sstc extension
2505  * is present.
2506  * Also, sip needs to be read-only zero when both mideleg[i] and
2507  * mvien[i] are zero but mvip needs to be an alias of mip.
2508  */
2509 static RISCVException rmw_mvip64(CPURISCVState *env, int csrno,
2510                                 uint64_t *ret_val,
2511                                 uint64_t new_val, uint64_t wr_mask)
2512 {
2513     RISCVCPU *cpu = env_archcpu(env);
2514     target_ulong ret_mip = 0;
2515     RISCVException ret;
2516     uint64_t old_mvip;
2517 
2518     /*
2519      * mideleg[i]  mvien[i]
2520      *   0           0      No delegation. mvip[i] is alias of mip[i].
2521      *   0           1      mvip[i] becomes source of interrupt, mip bypassed.
2522      *   1           X      mip[i] is source of interrupt and mvip[i] aliases
2523      *                      mip[i].
2524      *
2525      *   So alias condition would be for bits:
2526      *      ((S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & (mideleg | ~mvien)) |
2527      *          (!sstc & MIP_STIP)
2528      *
2529      *   Non-alias condition will be for bits:
2530      *      (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & (~mideleg & mvien)
2531      *
2532      *  alias_mask denotes the bits that come from mip nalias_mask denotes bits
2533      *  that come from hvip.
2534      */
2535     uint64_t alias_mask = ((S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) &
2536         (env->mideleg | ~env->mvien)) | MIP_STIP;
2537     uint64_t nalias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) &
2538         (~env->mideleg & env->mvien);
2539     uint64_t wr_mask_mvip;
2540     uint64_t wr_mask_mip;
2541 
2542     /*
2543      * mideleg[i]  mvien[i]
2544      *   0           0      sip[i] read-only zero.
2545      *   0           1      sip[i] alias of mvip[i].
2546      *   1           X      sip[i] alias of mip[i].
2547      *
2548      *  Both alias and non-alias mask remain same for sip except for bits
2549      *  which are zero in both mideleg and mvien.
2550      */
2551     if (csrno == CSR_SIP) {
2552         /* Remove bits that are zero in both mideleg and mvien. */
2553         alias_mask &= (env->mideleg | env->mvien);
2554         nalias_mask &= (env->mideleg | env->mvien);
2555     }
2556 
2557     /*
2558      * If sstc is present, mvip.STIP is not an alias of mip.STIP so clear
2559      * that our in mip returned value.
2560      */
2561     if (cpu->cfg.ext_sstc && (env->priv == PRV_M) &&
2562         get_field(env->menvcfg, MENVCFG_STCE)) {
2563         alias_mask &= ~MIP_STIP;
2564     }
2565 
2566     wr_mask_mip = wr_mask & alias_mask & mvip_writable_mask;
2567     wr_mask_mvip = wr_mask & nalias_mask & mvip_writable_mask;
2568 
2569     /*
2570      * For bits set in alias_mask, mvip needs to be alias of mip, so forward
2571      * this to rmw_mip.
2572      */
2573     ret = rmw_mip(env, CSR_MIP, &ret_mip, new_val, wr_mask_mip);
2574     if (ret != RISCV_EXCP_NONE) {
2575         return ret;
2576     }
2577 
2578     old_mvip = env->mvip;
2579 
2580     /*
2581      * Write to mvip. Update only non-alias bits. Alias bits were updated
2582      * in mip in rmw_mip above.
2583      */
2584     if (wr_mask_mvip) {
2585         env->mvip = (env->mvip & ~wr_mask_mvip) | (new_val & wr_mask_mvip);
2586 
2587         /*
2588          * Given mvip is separate source from mip, we need to trigger interrupt
2589          * from here separately. Normally this happen from riscv_cpu_update_mip.
2590          */
2591         riscv_cpu_interrupt(env);
2592     }
2593 
2594     if (ret_val) {
2595         ret_mip &= alias_mask;
2596         old_mvip &= nalias_mask;
2597 
2598         *ret_val = old_mvip | ret_mip;
2599     }
2600 
2601     return RISCV_EXCP_NONE;
2602 }
2603 
2604 static RISCVException rmw_mvip(CPURISCVState *env, int csrno,
2605                               target_ulong *ret_val,
2606                               target_ulong new_val, target_ulong wr_mask)
2607 {
2608     uint64_t rval;
2609     RISCVException ret;
2610 
2611     ret = rmw_mvip64(env, csrno, &rval, new_val, wr_mask);
2612     if (ret_val) {
2613         *ret_val = rval;
2614     }
2615 
2616     return ret;
2617 }
2618 
2619 static RISCVException rmw_mviph(CPURISCVState *env, int csrno,
2620                                target_ulong *ret_val,
2621                                target_ulong new_val, target_ulong wr_mask)
2622 {
2623     uint64_t rval;
2624     RISCVException ret;
2625 
2626     ret = rmw_mvip64(env, csrno, &rval,
2627         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2628     if (ret_val) {
2629         *ret_val = rval >> 32;
2630     }
2631 
2632     return ret;
2633 }
2634 
2635 /* Supervisor Trap Setup */
2636 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,
2637                                         Int128 *val)
2638 {
2639     uint64_t mask = sstatus_v1_10_mask;
2640     uint64_t sstatus = env->mstatus & mask;
2641     if (env->xl != MXL_RV32 || env->debugger) {
2642         mask |= SSTATUS64_UXL;
2643     }
2644 
2645     *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus));
2646     return RISCV_EXCP_NONE;
2647 }
2648 
2649 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
2650                                    target_ulong *val)
2651 {
2652     target_ulong mask = (sstatus_v1_10_mask);
2653     if (env->xl != MXL_RV32 || env->debugger) {
2654         mask |= SSTATUS64_UXL;
2655     }
2656     /* TODO: Use SXL not MXL. */
2657     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
2658     return RISCV_EXCP_NONE;
2659 }
2660 
2661 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
2662                                     target_ulong val)
2663 {
2664     target_ulong mask = (sstatus_v1_10_mask);
2665 
2666     if (env->xl != MXL_RV32 || env->debugger) {
2667         if ((val & SSTATUS64_UXL) != 0) {
2668             mask |= SSTATUS64_UXL;
2669         }
2670     }
2671     target_ulong newval = (env->mstatus & ~mask) | (val & mask);
2672     return write_mstatus(env, CSR_MSTATUS, newval);
2673 }
2674 
2675 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno,
2676                                  uint64_t *ret_val,
2677                                  uint64_t new_val, uint64_t wr_mask)
2678 {
2679     uint64_t alias_mask = (LOCAL_INTERRUPTS | VS_MODE_INTERRUPTS) &
2680                             env->hideleg;
2681     uint64_t nalias_mask = LOCAL_INTERRUPTS & (~env->hideleg & env->hvien);
2682     uint64_t rval, rval_vs, vsbits;
2683     uint64_t wr_mask_vsie;
2684     uint64_t wr_mask_mie;
2685     RISCVException ret;
2686 
2687     /* Bring VS-level bits to correct position */
2688     vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
2689     new_val &= ~(VS_MODE_INTERRUPTS >> 1);
2690     new_val |= vsbits << 1;
2691 
2692     vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
2693     wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
2694     wr_mask |= vsbits << 1;
2695 
2696     wr_mask_mie = wr_mask & alias_mask;
2697     wr_mask_vsie = wr_mask & nalias_mask;
2698 
2699     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask_mie);
2700 
2701     rval_vs = env->vsie & nalias_mask;
2702     env->vsie = (env->vsie & ~wr_mask_vsie) | (new_val & wr_mask_vsie);
2703 
2704     if (ret_val) {
2705         rval &= alias_mask;
2706         vsbits = rval & VS_MODE_INTERRUPTS;
2707         rval &= ~VS_MODE_INTERRUPTS;
2708         *ret_val = rval | (vsbits >> 1) | rval_vs;
2709     }
2710 
2711     return ret;
2712 }
2713 
2714 static RISCVException rmw_vsie(CPURISCVState *env, int csrno,
2715                                target_ulong *ret_val,
2716                                target_ulong new_val, target_ulong wr_mask)
2717 {
2718     uint64_t rval;
2719     RISCVException ret;
2720 
2721     ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask);
2722     if (ret_val) {
2723         *ret_val = rval;
2724     }
2725 
2726     return ret;
2727 }
2728 
2729 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno,
2730                                 target_ulong *ret_val,
2731                                 target_ulong new_val, target_ulong wr_mask)
2732 {
2733     uint64_t rval;
2734     RISCVException ret;
2735 
2736     ret = rmw_vsie64(env, csrno, &rval,
2737         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2738     if (ret_val) {
2739         *ret_val = rval >> 32;
2740     }
2741 
2742     return ret;
2743 }
2744 
2745 static RISCVException rmw_sie64(CPURISCVState *env, int csrno,
2746                                 uint64_t *ret_val,
2747                                 uint64_t new_val, uint64_t wr_mask)
2748 {
2749     uint64_t nalias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) &
2750         (~env->mideleg & env->mvien);
2751     uint64_t alias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & env->mideleg;
2752     uint64_t sie_mask = wr_mask & nalias_mask;
2753     RISCVException ret;
2754 
2755     /*
2756      * mideleg[i]  mvien[i]
2757      *   0           0      sie[i] read-only zero.
2758      *   0           1      sie[i] is a separate writable bit.
2759      *   1           X      sie[i] alias of mie[i].
2760      *
2761      *  Both alias and non-alias mask remain same for sip except for bits
2762      *  which are zero in both mideleg and mvien.
2763      */
2764     if (env->virt_enabled) {
2765         if (env->hvictl & HVICTL_VTI) {
2766             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2767         }
2768         ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask);
2769         if (ret_val) {
2770             *ret_val &= alias_mask;
2771         }
2772     } else {
2773         ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & alias_mask);
2774         if (ret_val) {
2775             *ret_val &= alias_mask;
2776             *ret_val |= env->sie & nalias_mask;
2777         }
2778 
2779         env->sie = (env->sie & ~sie_mask) | (new_val & sie_mask);
2780     }
2781 
2782     return ret;
2783 }
2784 
2785 static RISCVException rmw_sie(CPURISCVState *env, int csrno,
2786                               target_ulong *ret_val,
2787                               target_ulong new_val, target_ulong wr_mask)
2788 {
2789     uint64_t rval;
2790     RISCVException ret;
2791 
2792     ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask);
2793     if (ret == RISCV_EXCP_NONE && ret_val) {
2794         *ret_val = rval;
2795     }
2796 
2797     return ret;
2798 }
2799 
2800 static RISCVException rmw_sieh(CPURISCVState *env, int csrno,
2801                                target_ulong *ret_val,
2802                                target_ulong new_val, target_ulong wr_mask)
2803 {
2804     uint64_t rval;
2805     RISCVException ret;
2806 
2807     ret = rmw_sie64(env, csrno, &rval,
2808         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2809     if (ret_val) {
2810         *ret_val = rval >> 32;
2811     }
2812 
2813     return ret;
2814 }
2815 
2816 static RISCVException read_stvec(CPURISCVState *env, int csrno,
2817                                  target_ulong *val)
2818 {
2819     *val = env->stvec;
2820     return RISCV_EXCP_NONE;
2821 }
2822 
2823 static RISCVException write_stvec(CPURISCVState *env, int csrno,
2824                                   target_ulong val)
2825 {
2826     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
2827     if ((val & 3) < 2) {
2828         env->stvec = val;
2829     } else {
2830         qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
2831     }
2832     return RISCV_EXCP_NONE;
2833 }
2834 
2835 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
2836                                       target_ulong *val)
2837 {
2838     *val = env->scounteren;
2839     return RISCV_EXCP_NONE;
2840 }
2841 
2842 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
2843                                        target_ulong val)
2844 {
2845     env->scounteren = val;
2846     return RISCV_EXCP_NONE;
2847 }
2848 
2849 /* Supervisor Trap Handling */
2850 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno,
2851                                          Int128 *val)
2852 {
2853     *val = int128_make128(env->sscratch, env->sscratchh);
2854     return RISCV_EXCP_NONE;
2855 }
2856 
2857 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno,
2858                                           Int128 val)
2859 {
2860     env->sscratch = int128_getlo(val);
2861     env->sscratchh = int128_gethi(val);
2862     return RISCV_EXCP_NONE;
2863 }
2864 
2865 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
2866                                     target_ulong *val)
2867 {
2868     *val = env->sscratch;
2869     return RISCV_EXCP_NONE;
2870 }
2871 
2872 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
2873                                      target_ulong val)
2874 {
2875     env->sscratch = val;
2876     return RISCV_EXCP_NONE;
2877 }
2878 
2879 static RISCVException read_sepc(CPURISCVState *env, int csrno,
2880                                 target_ulong *val)
2881 {
2882     *val = env->sepc;
2883     return RISCV_EXCP_NONE;
2884 }
2885 
2886 static RISCVException write_sepc(CPURISCVState *env, int csrno,
2887                                  target_ulong val)
2888 {
2889     env->sepc = val;
2890     return RISCV_EXCP_NONE;
2891 }
2892 
2893 static RISCVException read_scause(CPURISCVState *env, int csrno,
2894                                   target_ulong *val)
2895 {
2896     *val = env->scause;
2897     return RISCV_EXCP_NONE;
2898 }
2899 
2900 static RISCVException write_scause(CPURISCVState *env, int csrno,
2901                                    target_ulong val)
2902 {
2903     env->scause = val;
2904     return RISCV_EXCP_NONE;
2905 }
2906 
2907 static RISCVException read_stval(CPURISCVState *env, int csrno,
2908                                  target_ulong *val)
2909 {
2910     *val = env->stval;
2911     return RISCV_EXCP_NONE;
2912 }
2913 
2914 static RISCVException write_stval(CPURISCVState *env, int csrno,
2915                                   target_ulong val)
2916 {
2917     env->stval = val;
2918     return RISCV_EXCP_NONE;
2919 }
2920 
2921 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
2922                                  uint64_t *ret_val,
2923                                  uint64_t new_val, uint64_t wr_mask);
2924 
2925 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno,
2926                                  uint64_t *ret_val,
2927                                  uint64_t new_val, uint64_t wr_mask)
2928 {
2929     RISCVException ret;
2930     uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS;
2931     uint64_t vsbits;
2932 
2933     /* Add virtualized bits into vsip mask. */
2934     mask |= env->hvien & ~env->hideleg;
2935 
2936     /* Bring VS-level bits to correct position */
2937     vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
2938     new_val &= ~(VS_MODE_INTERRUPTS >> 1);
2939     new_val |= vsbits << 1;
2940     vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
2941     wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
2942     wr_mask |= vsbits << 1;
2943 
2944     ret = rmw_hvip64(env, csrno, &rval, new_val,
2945                      wr_mask & mask & vsip_writable_mask);
2946     if (ret_val) {
2947         rval &= mask;
2948         vsbits = rval & VS_MODE_INTERRUPTS;
2949         rval &= ~VS_MODE_INTERRUPTS;
2950         *ret_val = rval | (vsbits >> 1);
2951     }
2952 
2953     return ret;
2954 }
2955 
2956 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
2957                                target_ulong *ret_val,
2958                                target_ulong new_val, target_ulong wr_mask)
2959 {
2960     uint64_t rval;
2961     RISCVException ret;
2962 
2963     ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask);
2964     if (ret_val) {
2965         *ret_val = rval;
2966     }
2967 
2968     return ret;
2969 }
2970 
2971 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno,
2972                                 target_ulong *ret_val,
2973                                 target_ulong new_val, target_ulong wr_mask)
2974 {
2975     uint64_t rval;
2976     RISCVException ret;
2977 
2978     ret = rmw_vsip64(env, csrno, &rval,
2979         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2980     if (ret_val) {
2981         *ret_val = rval >> 32;
2982     }
2983 
2984     return ret;
2985 }
2986 
2987 static RISCVException rmw_sip64(CPURISCVState *env, int csrno,
2988                                 uint64_t *ret_val,
2989                                 uint64_t new_val, uint64_t wr_mask)
2990 {
2991     RISCVException ret;
2992     uint64_t mask = (env->mideleg | env->mvien) & sip_writable_mask;
2993 
2994     if (env->virt_enabled) {
2995         if (env->hvictl & HVICTL_VTI) {
2996             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2997         }
2998         ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask);
2999     } else {
3000         ret = rmw_mvip64(env, csrno, ret_val, new_val, wr_mask & mask);
3001     }
3002 
3003     if (ret_val) {
3004         *ret_val &= (env->mideleg | env->mvien) &
3005             (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS);
3006     }
3007 
3008     return ret;
3009 }
3010 
3011 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
3012                               target_ulong *ret_val,
3013                               target_ulong new_val, target_ulong wr_mask)
3014 {
3015     uint64_t rval;
3016     RISCVException ret;
3017 
3018     ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask);
3019     if (ret_val) {
3020         *ret_val = rval;
3021     }
3022 
3023     return ret;
3024 }
3025 
3026 static RISCVException rmw_siph(CPURISCVState *env, int csrno,
3027                                target_ulong *ret_val,
3028                                target_ulong new_val, target_ulong wr_mask)
3029 {
3030     uint64_t rval;
3031     RISCVException ret;
3032 
3033     ret = rmw_sip64(env, csrno, &rval,
3034         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3035     if (ret_val) {
3036         *ret_val = rval >> 32;
3037     }
3038 
3039     return ret;
3040 }
3041 
3042 /* Supervisor Protection and Translation */
3043 static RISCVException read_satp(CPURISCVState *env, int csrno,
3044                                 target_ulong *val)
3045 {
3046     if (!riscv_cpu_cfg(env)->mmu) {
3047         *val = 0;
3048         return RISCV_EXCP_NONE;
3049     }
3050     *val = env->satp;
3051     return RISCV_EXCP_NONE;
3052 }
3053 
3054 static RISCVException write_satp(CPURISCVState *env, int csrno,
3055                                  target_ulong val)
3056 {
3057     if (!riscv_cpu_cfg(env)->mmu) {
3058         return RISCV_EXCP_NONE;
3059     }
3060 
3061     env->satp = legalize_xatp(env, env->satp, val);
3062     return RISCV_EXCP_NONE;
3063 }
3064 
3065 static RISCVException read_vstopi(CPURISCVState *env, int csrno,
3066                                   target_ulong *val)
3067 {
3068     int irq, ret;
3069     target_ulong topei;
3070     uint64_t vseip, vsgein;
3071     uint32_t iid, iprio, hviid, hviprio, gein;
3072     uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS];
3073 
3074     gein = get_field(env->hstatus, HSTATUS_VGEIN);
3075     hviid = get_field(env->hvictl, HVICTL_IID);
3076     hviprio = get_field(env->hvictl, HVICTL_IPRIO);
3077 
3078     if (gein) {
3079         vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0;
3080         vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP;
3081         if (gein <= env->geilen && vseip) {
3082             siid[scount] = IRQ_S_EXT;
3083             siprio[scount] = IPRIO_MMAXIPRIO + 1;
3084             if (env->aia_ireg_rmw_fn[PRV_S]) {
3085                 /*
3086                  * Call machine specific IMSIC register emulation for
3087                  * reading TOPEI.
3088                  */
3089                 ret = env->aia_ireg_rmw_fn[PRV_S](
3090                         env->aia_ireg_rmw_fn_arg[PRV_S],
3091                         AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein,
3092                                       riscv_cpu_mxl_bits(env)),
3093                         &topei, 0, 0);
3094                 if (!ret && topei) {
3095                     siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK;
3096                 }
3097             }
3098             scount++;
3099         }
3100     } else {
3101         if (hviid == IRQ_S_EXT && hviprio) {
3102             siid[scount] = IRQ_S_EXT;
3103             siprio[scount] = hviprio;
3104             scount++;
3105         }
3106     }
3107 
3108     if (env->hvictl & HVICTL_VTI) {
3109         if (hviid != IRQ_S_EXT) {
3110             siid[scount] = hviid;
3111             siprio[scount] = hviprio;
3112             scount++;
3113         }
3114     } else {
3115         irq = riscv_cpu_vsirq_pending(env);
3116         if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) {
3117             siid[scount] = irq;
3118             siprio[scount] = env->hviprio[irq];
3119             scount++;
3120         }
3121     }
3122 
3123     iid = 0;
3124     iprio = UINT_MAX;
3125     for (s = 0; s < scount; s++) {
3126         if (siprio[s] < iprio) {
3127             iid = siid[s];
3128             iprio = siprio[s];
3129         }
3130     }
3131 
3132     if (iid) {
3133         if (env->hvictl & HVICTL_IPRIOM) {
3134             if (iprio > IPRIO_MMAXIPRIO) {
3135                 iprio = IPRIO_MMAXIPRIO;
3136             }
3137             if (!iprio) {
3138                 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) {
3139                     iprio = IPRIO_MMAXIPRIO;
3140                 }
3141             }
3142         } else {
3143             iprio = 1;
3144         }
3145     } else {
3146         iprio = 0;
3147     }
3148 
3149     *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT;
3150     *val |= iprio;
3151 
3152     return RISCV_EXCP_NONE;
3153 }
3154 
3155 static RISCVException read_stopi(CPURISCVState *env, int csrno,
3156                                  target_ulong *val)
3157 {
3158     int irq;
3159     uint8_t iprio;
3160 
3161     if (env->virt_enabled) {
3162         return read_vstopi(env, CSR_VSTOPI, val);
3163     }
3164 
3165     irq = riscv_cpu_sirq_pending(env);
3166     if (irq <= 0 || irq > 63) {
3167         *val = 0;
3168     } else {
3169         iprio = env->siprio[irq];
3170         if (!iprio) {
3171             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) {
3172                 iprio = IPRIO_MMAXIPRIO;
3173            }
3174         }
3175         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
3176         *val |= iprio;
3177     }
3178 
3179     return RISCV_EXCP_NONE;
3180 }
3181 
3182 /* Hypervisor Extensions */
3183 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
3184                                    target_ulong *val)
3185 {
3186     *val = env->hstatus;
3187     if (riscv_cpu_mxl(env) != MXL_RV32) {
3188         /* We only support 64-bit VSXL */
3189         *val = set_field(*val, HSTATUS_VSXL, 2);
3190     }
3191     /* We only support little endian */
3192     *val = set_field(*val, HSTATUS_VSBE, 0);
3193     return RISCV_EXCP_NONE;
3194 }
3195 
3196 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
3197                                     target_ulong val)
3198 {
3199     env->hstatus = val;
3200     if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
3201         qemu_log_mask(LOG_UNIMP,
3202                       "QEMU does not support mixed HSXLEN options.");
3203     }
3204     if (get_field(val, HSTATUS_VSBE) != 0) {
3205         qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
3206     }
3207     return RISCV_EXCP_NONE;
3208 }
3209 
3210 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
3211                                    target_ulong *val)
3212 {
3213     *val = env->hedeleg;
3214     return RISCV_EXCP_NONE;
3215 }
3216 
3217 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
3218                                     target_ulong val)
3219 {
3220     env->hedeleg = val & vs_delegable_excps;
3221     return RISCV_EXCP_NONE;
3222 }
3223 
3224 static RISCVException rmw_hvien64(CPURISCVState *env, int csrno,
3225                                     uint64_t *ret_val,
3226                                     uint64_t new_val, uint64_t wr_mask)
3227 {
3228     uint64_t mask = wr_mask & hvien_writable_mask;
3229 
3230     if (ret_val) {
3231         *ret_val = env->hvien;
3232     }
3233 
3234     env->hvien = (env->hvien & ~mask) | (new_val & mask);
3235 
3236     return RISCV_EXCP_NONE;
3237 }
3238 
3239 static RISCVException rmw_hvien(CPURISCVState *env, int csrno,
3240                                target_ulong *ret_val,
3241                                target_ulong new_val, target_ulong wr_mask)
3242 {
3243     uint64_t rval;
3244     RISCVException ret;
3245 
3246     ret = rmw_hvien64(env, csrno, &rval, new_val, wr_mask);
3247     if (ret_val) {
3248         *ret_val = rval;
3249     }
3250 
3251     return ret;
3252 }
3253 
3254 static RISCVException rmw_hvienh(CPURISCVState *env, int csrno,
3255                                    target_ulong *ret_val,
3256                                    target_ulong new_val, target_ulong wr_mask)
3257 {
3258     uint64_t rval;
3259     RISCVException ret;
3260 
3261     ret = rmw_hvien64(env, csrno, &rval,
3262         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3263     if (ret_val) {
3264         *ret_val = rval >> 32;
3265     }
3266 
3267     return ret;
3268 }
3269 
3270 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno,
3271                                     uint64_t *ret_val,
3272                                     uint64_t new_val, uint64_t wr_mask)
3273 {
3274     uint64_t mask = wr_mask & vs_delegable_ints;
3275 
3276     if (ret_val) {
3277         *ret_val = env->hideleg & vs_delegable_ints;
3278     }
3279 
3280     env->hideleg = (env->hideleg & ~mask) | (new_val & mask);
3281     return RISCV_EXCP_NONE;
3282 }
3283 
3284 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno,
3285                                   target_ulong *ret_val,
3286                                   target_ulong new_val, target_ulong wr_mask)
3287 {
3288     uint64_t rval;
3289     RISCVException ret;
3290 
3291     ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask);
3292     if (ret_val) {
3293         *ret_val = rval;
3294     }
3295 
3296     return ret;
3297 }
3298 
3299 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno,
3300                                    target_ulong *ret_val,
3301                                    target_ulong new_val, target_ulong wr_mask)
3302 {
3303     uint64_t rval;
3304     RISCVException ret;
3305 
3306     ret = rmw_hideleg64(env, csrno, &rval,
3307         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3308     if (ret_val) {
3309         *ret_val = rval >> 32;
3310     }
3311 
3312     return ret;
3313 }
3314 
3315 /*
3316  * The function is written for two use-cases:
3317  * 1- To access hvip csr as is for HS-mode access.
3318  * 2- To access vsip as a combination of hvip, and mip for vs-mode.
3319  *
3320  * Both report bits 2, 6, 10 and 13:63.
3321  * vsip needs to be read-only zero when both hideleg[i] and
3322  * hvien[i] are zero.
3323  */
3324 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
3325                                  uint64_t *ret_val,
3326                                  uint64_t new_val, uint64_t wr_mask)
3327 {
3328     RISCVException ret;
3329     uint64_t old_hvip;
3330     uint64_t ret_mip;
3331 
3332     /*
3333      * For bits 10, 6 and 2, vsip[i] is an alias of hip[i]. These bits are
3334      * present in hip, hvip and mip. Where mip[i] is alias of hip[i] and hvip[i]
3335      * is OR'ed in hip[i] to inject virtual interrupts from hypervisor. These
3336      * bits are actually being maintained in mip so we read them from there.
3337      * This way we have a single source of truth and allows for easier
3338      * implementation.
3339      *
3340      * For bits 13:63 we have:
3341      *
3342      * hideleg[i]  hvien[i]
3343      *   0           0      No delegation. vsip[i] readonly zero.
3344      *   0           1      vsip[i] is alias of hvip[i], sip bypassed.
3345      *   1           X      vsip[i] is alias of sip[i], hvip bypassed.
3346      *
3347      *  alias_mask denotes the bits that come from sip (mip here given we
3348      *  maintain all bits there). nalias_mask denotes bits that come from
3349      *  hvip.
3350      */
3351     uint64_t alias_mask = (env->hideleg | ~env->hvien) | VS_MODE_INTERRUPTS;
3352     uint64_t nalias_mask = (~env->hideleg & env->hvien);
3353     uint64_t wr_mask_hvip;
3354     uint64_t wr_mask_mip;
3355 
3356     /*
3357      * Both alias and non-alias mask remain same for vsip except:
3358      *  1- For VS* bits if they are zero in hideleg.
3359      *  2- For 13:63 bits if they are zero in both hideleg and hvien.
3360      */
3361     if (csrno == CSR_VSIP) {
3362         /* zero-out VS* bits that are not delegated to VS mode. */
3363         alias_mask &= (env->hideleg | ~VS_MODE_INTERRUPTS);
3364 
3365         /*
3366          * zero-out 13:63 bits that are zero in both hideleg and hvien.
3367          * nalias_mask mask can not contain any VS* bits so only second
3368          * condition applies on it.
3369          */
3370         nalias_mask &= (env->hideleg | env->hvien);
3371         alias_mask &= (env->hideleg | env->hvien);
3372     }
3373 
3374     wr_mask_hvip = wr_mask & nalias_mask & hvip_writable_mask;
3375     wr_mask_mip = wr_mask & alias_mask & hvip_writable_mask;
3376 
3377     /* Aliased bits, bits 10, 6, 2 need to come from mip. */
3378     ret = rmw_mip64(env, csrno, &ret_mip, new_val, wr_mask_mip);
3379     if (ret != RISCV_EXCP_NONE) {
3380         return ret;
3381     }
3382 
3383     old_hvip = env->hvip;
3384 
3385     if (wr_mask_hvip) {
3386         env->hvip = (env->hvip & ~wr_mask_hvip) | (new_val & wr_mask_hvip);
3387 
3388         /*
3389          * Given hvip is separate source from mip, we need to trigger interrupt
3390          * from here separately. Normally this happen from riscv_cpu_update_mip.
3391          */
3392         riscv_cpu_interrupt(env);
3393     }
3394 
3395     if (ret_val) {
3396         /* Only take VS* bits from mip. */
3397         ret_mip &= alias_mask;
3398 
3399         /* Take in non-delegated 13:63 bits from hvip. */
3400         old_hvip &= nalias_mask;
3401 
3402         *ret_val = ret_mip | old_hvip;
3403     }
3404 
3405     return ret;
3406 }
3407 
3408 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
3409                                target_ulong *ret_val,
3410                                target_ulong new_val, target_ulong wr_mask)
3411 {
3412     uint64_t rval;
3413     RISCVException ret;
3414 
3415     ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask);
3416     if (ret_val) {
3417         *ret_val = rval;
3418     }
3419 
3420     return ret;
3421 }
3422 
3423 static RISCVException rmw_hviph(CPURISCVState *env, int csrno,
3424                                 target_ulong *ret_val,
3425                                 target_ulong new_val, target_ulong wr_mask)
3426 {
3427     uint64_t rval;
3428     RISCVException ret;
3429 
3430     ret = rmw_hvip64(env, csrno, &rval,
3431         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
3432     if (ret_val) {
3433         *ret_val = rval >> 32;
3434     }
3435 
3436     return ret;
3437 }
3438 
3439 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
3440                               target_ulong *ret_value,
3441                               target_ulong new_value, target_ulong write_mask)
3442 {
3443     int ret = rmw_mip(env, csrno, ret_value, new_value,
3444                       write_mask & hip_writable_mask);
3445 
3446     if (ret_value) {
3447         *ret_value &= HS_MODE_INTERRUPTS;
3448     }
3449     return ret;
3450 }
3451 
3452 static RISCVException rmw_hie(CPURISCVState *env, int csrno,
3453                               target_ulong *ret_val,
3454                               target_ulong new_val, target_ulong wr_mask)
3455 {
3456     uint64_t rval;
3457     RISCVException ret;
3458 
3459     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS);
3460     if (ret_val) {
3461         *ret_val = rval & HS_MODE_INTERRUPTS;
3462     }
3463 
3464     return ret;
3465 }
3466 
3467 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
3468                                       target_ulong *val)
3469 {
3470     *val = env->hcounteren;
3471     return RISCV_EXCP_NONE;
3472 }
3473 
3474 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
3475                                        target_ulong val)
3476 {
3477     env->hcounteren = val;
3478     return RISCV_EXCP_NONE;
3479 }
3480 
3481 static RISCVException read_hgeie(CPURISCVState *env, int csrno,
3482                                  target_ulong *val)
3483 {
3484     if (val) {
3485         *val = env->hgeie;
3486     }
3487     return RISCV_EXCP_NONE;
3488 }
3489 
3490 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
3491                                   target_ulong val)
3492 {
3493     /* Only GEILEN:1 bits implemented and BIT0 is never implemented */
3494     val &= ((((target_ulong)1) << env->geilen) - 1) << 1;
3495     env->hgeie = val;
3496     /* Update mip.SGEIP bit */
3497     riscv_cpu_update_mip(env, MIP_SGEIP,
3498                          BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
3499     return RISCV_EXCP_NONE;
3500 }
3501 
3502 static RISCVException read_htval(CPURISCVState *env, int csrno,
3503                                  target_ulong *val)
3504 {
3505     *val = env->htval;
3506     return RISCV_EXCP_NONE;
3507 }
3508 
3509 static RISCVException write_htval(CPURISCVState *env, int csrno,
3510                                   target_ulong val)
3511 {
3512     env->htval = val;
3513     return RISCV_EXCP_NONE;
3514 }
3515 
3516 static RISCVException read_htinst(CPURISCVState *env, int csrno,
3517                                   target_ulong *val)
3518 {
3519     *val = env->htinst;
3520     return RISCV_EXCP_NONE;
3521 }
3522 
3523 static RISCVException write_htinst(CPURISCVState *env, int csrno,
3524                                    target_ulong val)
3525 {
3526     return RISCV_EXCP_NONE;
3527 }
3528 
3529 static RISCVException read_hgeip(CPURISCVState *env, int csrno,
3530                                  target_ulong *val)
3531 {
3532     if (val) {
3533         *val = env->hgeip;
3534     }
3535     return RISCV_EXCP_NONE;
3536 }
3537 
3538 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
3539                                  target_ulong *val)
3540 {
3541     *val = env->hgatp;
3542     return RISCV_EXCP_NONE;
3543 }
3544 
3545 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
3546                                   target_ulong val)
3547 {
3548     env->hgatp = legalize_xatp(env, env->hgatp, val);
3549     return RISCV_EXCP_NONE;
3550 }
3551 
3552 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
3553                                       target_ulong *val)
3554 {
3555     if (!env->rdtime_fn) {
3556         return RISCV_EXCP_ILLEGAL_INST;
3557     }
3558 
3559     *val = env->htimedelta;
3560     return RISCV_EXCP_NONE;
3561 }
3562 
3563 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
3564                                        target_ulong val)
3565 {
3566     if (!env->rdtime_fn) {
3567         return RISCV_EXCP_ILLEGAL_INST;
3568     }
3569 
3570     if (riscv_cpu_mxl(env) == MXL_RV32) {
3571         env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
3572     } else {
3573         env->htimedelta = val;
3574     }
3575 
3576     if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3577         riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3578                                   env->htimedelta, MIP_VSTIP);
3579     }
3580 
3581     return RISCV_EXCP_NONE;
3582 }
3583 
3584 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
3585                                        target_ulong *val)
3586 {
3587     if (!env->rdtime_fn) {
3588         return RISCV_EXCP_ILLEGAL_INST;
3589     }
3590 
3591     *val = env->htimedelta >> 32;
3592     return RISCV_EXCP_NONE;
3593 }
3594 
3595 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
3596                                         target_ulong val)
3597 {
3598     if (!env->rdtime_fn) {
3599         return RISCV_EXCP_ILLEGAL_INST;
3600     }
3601 
3602     env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
3603 
3604     if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3605         riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3606                                   env->htimedelta, MIP_VSTIP);
3607     }
3608 
3609     return RISCV_EXCP_NONE;
3610 }
3611 
3612 static RISCVException read_hvictl(CPURISCVState *env, int csrno,
3613                                   target_ulong *val)
3614 {
3615     *val = env->hvictl;
3616     return RISCV_EXCP_NONE;
3617 }
3618 
3619 static RISCVException write_hvictl(CPURISCVState *env, int csrno,
3620                                    target_ulong val)
3621 {
3622     env->hvictl = val & HVICTL_VALID_MASK;
3623     return RISCV_EXCP_NONE;
3624 }
3625 
3626 static RISCVException read_hvipriox(CPURISCVState *env, int first_index,
3627                          uint8_t *iprio, target_ulong *val)
3628 {
3629     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3630 
3631     /* First index has to be a multiple of number of irqs per register */
3632     if (first_index % num_irqs) {
3633         return (env->virt_enabled) ?
3634                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3635     }
3636 
3637     /* Fill-up return value */
3638     *val = 0;
3639     for (i = 0; i < num_irqs; i++) {
3640         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3641             continue;
3642         }
3643         if (rdzero) {
3644             continue;
3645         }
3646         *val |= ((target_ulong)iprio[irq]) << (i * 8);
3647     }
3648 
3649     return RISCV_EXCP_NONE;
3650 }
3651 
3652 static RISCVException write_hvipriox(CPURISCVState *env, int first_index,
3653                           uint8_t *iprio, target_ulong val)
3654 {
3655     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3656 
3657     /* First index has to be a multiple of number of irqs per register */
3658     if (first_index % num_irqs) {
3659         return (env->virt_enabled) ?
3660                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3661     }
3662 
3663     /* Fill-up priority array */
3664     for (i = 0; i < num_irqs; i++) {
3665         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3666             continue;
3667         }
3668         if (rdzero) {
3669             iprio[irq] = 0;
3670         } else {
3671             iprio[irq] = (val >> (i * 8)) & 0xff;
3672         }
3673     }
3674 
3675     return RISCV_EXCP_NONE;
3676 }
3677 
3678 static RISCVException read_hviprio1(CPURISCVState *env, int csrno,
3679                                     target_ulong *val)
3680 {
3681     return read_hvipriox(env, 0, env->hviprio, val);
3682 }
3683 
3684 static RISCVException write_hviprio1(CPURISCVState *env, int csrno,
3685                                      target_ulong val)
3686 {
3687     return write_hvipriox(env, 0, env->hviprio, val);
3688 }
3689 
3690 static RISCVException read_hviprio1h(CPURISCVState *env, int csrno,
3691                                      target_ulong *val)
3692 {
3693     return read_hvipriox(env, 4, env->hviprio, val);
3694 }
3695 
3696 static RISCVException write_hviprio1h(CPURISCVState *env, int csrno,
3697                                       target_ulong val)
3698 {
3699     return write_hvipriox(env, 4, env->hviprio, val);
3700 }
3701 
3702 static RISCVException read_hviprio2(CPURISCVState *env, int csrno,
3703                                     target_ulong *val)
3704 {
3705     return read_hvipriox(env, 8, env->hviprio, val);
3706 }
3707 
3708 static RISCVException write_hviprio2(CPURISCVState *env, int csrno,
3709                                      target_ulong val)
3710 {
3711     return write_hvipriox(env, 8, env->hviprio, val);
3712 }
3713 
3714 static RISCVException read_hviprio2h(CPURISCVState *env, int csrno,
3715                                      target_ulong *val)
3716 {
3717     return read_hvipriox(env, 12, env->hviprio, val);
3718 }
3719 
3720 static RISCVException write_hviprio2h(CPURISCVState *env, int csrno,
3721                                       target_ulong val)
3722 {
3723     return write_hvipriox(env, 12, env->hviprio, val);
3724 }
3725 
3726 /* Virtual CSR Registers */
3727 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
3728                                     target_ulong *val)
3729 {
3730     *val = env->vsstatus;
3731     return RISCV_EXCP_NONE;
3732 }
3733 
3734 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
3735                                      target_ulong val)
3736 {
3737     uint64_t mask = (target_ulong)-1;
3738     if ((val & VSSTATUS64_UXL) == 0) {
3739         mask &= ~VSSTATUS64_UXL;
3740     }
3741     env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
3742     return RISCV_EXCP_NONE;
3743 }
3744 
3745 static RISCVException read_vstvec(CPURISCVState *env, int csrno,
3746                                   target_ulong *val)
3747 {
3748     *val = env->vstvec;
3749     return RISCV_EXCP_NONE;
3750 }
3751 
3752 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
3753                                    target_ulong val)
3754 {
3755     env->vstvec = val;
3756     return RISCV_EXCP_NONE;
3757 }
3758 
3759 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
3760                                      target_ulong *val)
3761 {
3762     *val = env->vsscratch;
3763     return RISCV_EXCP_NONE;
3764 }
3765 
3766 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
3767                                       target_ulong val)
3768 {
3769     env->vsscratch = val;
3770     return RISCV_EXCP_NONE;
3771 }
3772 
3773 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
3774                                  target_ulong *val)
3775 {
3776     *val = env->vsepc;
3777     return RISCV_EXCP_NONE;
3778 }
3779 
3780 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
3781                                   target_ulong val)
3782 {
3783     env->vsepc = val;
3784     return RISCV_EXCP_NONE;
3785 }
3786 
3787 static RISCVException read_vscause(CPURISCVState *env, int csrno,
3788                                    target_ulong *val)
3789 {
3790     *val = env->vscause;
3791     return RISCV_EXCP_NONE;
3792 }
3793 
3794 static RISCVException write_vscause(CPURISCVState *env, int csrno,
3795                                     target_ulong val)
3796 {
3797     env->vscause = val;
3798     return RISCV_EXCP_NONE;
3799 }
3800 
3801 static RISCVException read_vstval(CPURISCVState *env, int csrno,
3802                                   target_ulong *val)
3803 {
3804     *val = env->vstval;
3805     return RISCV_EXCP_NONE;
3806 }
3807 
3808 static RISCVException write_vstval(CPURISCVState *env, int csrno,
3809                                    target_ulong val)
3810 {
3811     env->vstval = val;
3812     return RISCV_EXCP_NONE;
3813 }
3814 
3815 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
3816                                  target_ulong *val)
3817 {
3818     *val = env->vsatp;
3819     return RISCV_EXCP_NONE;
3820 }
3821 
3822 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
3823                                   target_ulong val)
3824 {
3825     env->vsatp = legalize_xatp(env, env->vsatp, val);
3826     return RISCV_EXCP_NONE;
3827 }
3828 
3829 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
3830                                   target_ulong *val)
3831 {
3832     *val = env->mtval2;
3833     return RISCV_EXCP_NONE;
3834 }
3835 
3836 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
3837                                    target_ulong val)
3838 {
3839     env->mtval2 = val;
3840     return RISCV_EXCP_NONE;
3841 }
3842 
3843 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
3844                                   target_ulong *val)
3845 {
3846     *val = env->mtinst;
3847     return RISCV_EXCP_NONE;
3848 }
3849 
3850 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
3851                                    target_ulong val)
3852 {
3853     env->mtinst = val;
3854     return RISCV_EXCP_NONE;
3855 }
3856 
3857 /* Physical Memory Protection */
3858 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
3859                                    target_ulong *val)
3860 {
3861     *val = mseccfg_csr_read(env);
3862     return RISCV_EXCP_NONE;
3863 }
3864 
3865 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
3866                                     target_ulong val)
3867 {
3868     mseccfg_csr_write(env, val);
3869     return RISCV_EXCP_NONE;
3870 }
3871 
3872 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
3873                                   target_ulong *val)
3874 {
3875     uint32_t reg_index = csrno - CSR_PMPCFG0;
3876 
3877     *val = pmpcfg_csr_read(env, reg_index);
3878     return RISCV_EXCP_NONE;
3879 }
3880 
3881 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
3882                                    target_ulong val)
3883 {
3884     uint32_t reg_index = csrno - CSR_PMPCFG0;
3885 
3886     pmpcfg_csr_write(env, reg_index, val);
3887     return RISCV_EXCP_NONE;
3888 }
3889 
3890 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
3891                                    target_ulong *val)
3892 {
3893     *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
3894     return RISCV_EXCP_NONE;
3895 }
3896 
3897 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
3898                                     target_ulong val)
3899 {
3900     pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
3901     return RISCV_EXCP_NONE;
3902 }
3903 
3904 static RISCVException read_tselect(CPURISCVState *env, int csrno,
3905                                    target_ulong *val)
3906 {
3907     *val = tselect_csr_read(env);
3908     return RISCV_EXCP_NONE;
3909 }
3910 
3911 static RISCVException write_tselect(CPURISCVState *env, int csrno,
3912                                     target_ulong val)
3913 {
3914     tselect_csr_write(env, val);
3915     return RISCV_EXCP_NONE;
3916 }
3917 
3918 static RISCVException read_tdata(CPURISCVState *env, int csrno,
3919                                  target_ulong *val)
3920 {
3921     /* return 0 in tdata1 to end the trigger enumeration */
3922     if (env->trigger_cur >= RV_MAX_TRIGGERS && csrno == CSR_TDATA1) {
3923         *val = 0;
3924         return RISCV_EXCP_NONE;
3925     }
3926 
3927     if (!tdata_available(env, csrno - CSR_TDATA1)) {
3928         return RISCV_EXCP_ILLEGAL_INST;
3929     }
3930 
3931     *val = tdata_csr_read(env, csrno - CSR_TDATA1);
3932     return RISCV_EXCP_NONE;
3933 }
3934 
3935 static RISCVException write_tdata(CPURISCVState *env, int csrno,
3936                                   target_ulong val)
3937 {
3938     if (!tdata_available(env, csrno - CSR_TDATA1)) {
3939         return RISCV_EXCP_ILLEGAL_INST;
3940     }
3941 
3942     tdata_csr_write(env, csrno - CSR_TDATA1, val);
3943     return RISCV_EXCP_NONE;
3944 }
3945 
3946 static RISCVException read_tinfo(CPURISCVState *env, int csrno,
3947                                  target_ulong *val)
3948 {
3949     *val = tinfo_csr_read(env);
3950     return RISCV_EXCP_NONE;
3951 }
3952 
3953 static RISCVException read_mcontext(CPURISCVState *env, int csrno,
3954                                     target_ulong *val)
3955 {
3956     *val = env->mcontext;
3957     return RISCV_EXCP_NONE;
3958 }
3959 
3960 static RISCVException write_mcontext(CPURISCVState *env, int csrno,
3961                                      target_ulong val)
3962 {
3963     bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false;
3964     int32_t mask;
3965 
3966     if (riscv_has_ext(env, RVH)) {
3967         /* Spec suggest 7-bit for RV32 and 14-bit for RV64 w/ H extension */
3968         mask = rv32 ? MCONTEXT32_HCONTEXT : MCONTEXT64_HCONTEXT;
3969     } else {
3970         /* Spec suggest 6-bit for RV32 and 13-bit for RV64 w/o H extension */
3971         mask = rv32 ? MCONTEXT32 : MCONTEXT64;
3972     }
3973 
3974     env->mcontext = val & mask;
3975     return RISCV_EXCP_NONE;
3976 }
3977 
3978 /*
3979  * Functions to access Pointer Masking feature registers
3980  * We have to check if current priv lvl could modify
3981  * csr in given mode
3982  */
3983 static bool check_pm_current_disabled(CPURISCVState *env, int csrno)
3984 {
3985     int csr_priv = get_field(csrno, 0x300);
3986     int pm_current;
3987 
3988     if (env->debugger) {
3989         return false;
3990     }
3991     /*
3992      * If priv lvls differ that means we're accessing csr from higher priv lvl,
3993      * so allow the access
3994      */
3995     if (env->priv != csr_priv) {
3996         return false;
3997     }
3998     switch (env->priv) {
3999     case PRV_M:
4000         pm_current = get_field(env->mmte, M_PM_CURRENT);
4001         break;
4002     case PRV_S:
4003         pm_current = get_field(env->mmte, S_PM_CURRENT);
4004         break;
4005     case PRV_U:
4006         pm_current = get_field(env->mmte, U_PM_CURRENT);
4007         break;
4008     default:
4009         g_assert_not_reached();
4010     }
4011     /* It's same priv lvl, so we allow to modify csr only if pm.current==1 */
4012     return !pm_current;
4013 }
4014 
4015 static RISCVException read_mmte(CPURISCVState *env, int csrno,
4016                                 target_ulong *val)
4017 {
4018     *val = env->mmte & MMTE_MASK;
4019     return RISCV_EXCP_NONE;
4020 }
4021 
4022 static RISCVException write_mmte(CPURISCVState *env, int csrno,
4023                                  target_ulong val)
4024 {
4025     uint64_t mstatus;
4026     target_ulong wpri_val = val & MMTE_MASK;
4027 
4028     if (val != wpri_val) {
4029         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
4030                       TARGET_FMT_lx "\n", "MMTE: WPRI violation written 0x",
4031                       val, "vs expected 0x", wpri_val);
4032     }
4033     /* for machine mode pm.current is hardwired to 1 */
4034     wpri_val |= MMTE_M_PM_CURRENT;
4035 
4036     /* hardwiring pm.instruction bit to 0, since it's not supported yet */
4037     wpri_val &= ~(MMTE_M_PM_INSN | MMTE_S_PM_INSN | MMTE_U_PM_INSN);
4038     env->mmte = wpri_val | EXT_STATUS_DIRTY;
4039     riscv_cpu_update_mask(env);
4040 
4041     /* Set XS and SD bits, since PM CSRs are dirty */
4042     mstatus = env->mstatus | MSTATUS_XS;
4043     write_mstatus(env, csrno, mstatus);
4044     return RISCV_EXCP_NONE;
4045 }
4046 
4047 static RISCVException read_smte(CPURISCVState *env, int csrno,
4048                                 target_ulong *val)
4049 {
4050     *val = env->mmte & SMTE_MASK;
4051     return RISCV_EXCP_NONE;
4052 }
4053 
4054 static RISCVException write_smte(CPURISCVState *env, int csrno,
4055                                  target_ulong val)
4056 {
4057     target_ulong wpri_val = val & SMTE_MASK;
4058 
4059     if (val != wpri_val) {
4060         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
4061                       TARGET_FMT_lx "\n", "SMTE: WPRI violation written 0x",
4062                       val, "vs expected 0x", wpri_val);
4063     }
4064 
4065     /* if pm.current==0 we can't modify current PM CSRs */
4066     if (check_pm_current_disabled(env, csrno)) {
4067         return RISCV_EXCP_NONE;
4068     }
4069 
4070     wpri_val |= (env->mmte & ~SMTE_MASK);
4071     write_mmte(env, csrno, wpri_val);
4072     return RISCV_EXCP_NONE;
4073 }
4074 
4075 static RISCVException read_umte(CPURISCVState *env, int csrno,
4076                                 target_ulong *val)
4077 {
4078     *val = env->mmte & UMTE_MASK;
4079     return RISCV_EXCP_NONE;
4080 }
4081 
4082 static RISCVException write_umte(CPURISCVState *env, int csrno,
4083                                  target_ulong val)
4084 {
4085     target_ulong wpri_val = val & UMTE_MASK;
4086 
4087     if (val != wpri_val) {
4088         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
4089                       TARGET_FMT_lx "\n", "UMTE: WPRI violation written 0x",
4090                       val, "vs expected 0x", wpri_val);
4091     }
4092 
4093     if (check_pm_current_disabled(env, csrno)) {
4094         return RISCV_EXCP_NONE;
4095     }
4096 
4097     wpri_val |= (env->mmte & ~UMTE_MASK);
4098     write_mmte(env, csrno, wpri_val);
4099     return RISCV_EXCP_NONE;
4100 }
4101 
4102 static RISCVException read_mpmmask(CPURISCVState *env, int csrno,
4103                                    target_ulong *val)
4104 {
4105     *val = env->mpmmask;
4106     return RISCV_EXCP_NONE;
4107 }
4108 
4109 static RISCVException write_mpmmask(CPURISCVState *env, int csrno,
4110                                     target_ulong val)
4111 {
4112     uint64_t mstatus;
4113 
4114     env->mpmmask = val;
4115     if ((cpu_address_mode(env) == PRV_M) && (env->mmte & M_PM_ENABLE)) {
4116         env->cur_pmmask = val;
4117     }
4118     env->mmte |= EXT_STATUS_DIRTY;
4119 
4120     /* Set XS and SD bits, since PM CSRs are dirty */
4121     mstatus = env->mstatus | MSTATUS_XS;
4122     write_mstatus(env, csrno, mstatus);
4123     return RISCV_EXCP_NONE;
4124 }
4125 
4126 static RISCVException read_spmmask(CPURISCVState *env, int csrno,
4127                                    target_ulong *val)
4128 {
4129     *val = env->spmmask;
4130     return RISCV_EXCP_NONE;
4131 }
4132 
4133 static RISCVException write_spmmask(CPURISCVState *env, int csrno,
4134                                     target_ulong val)
4135 {
4136     uint64_t mstatus;
4137 
4138     /* if pm.current==0 we can't modify current PM CSRs */
4139     if (check_pm_current_disabled(env, csrno)) {
4140         return RISCV_EXCP_NONE;
4141     }
4142     env->spmmask = val;
4143     if ((cpu_address_mode(env) == PRV_S) && (env->mmte & S_PM_ENABLE)) {
4144         env->cur_pmmask = val;
4145         if (cpu_get_xl(env, PRV_S) == MXL_RV32) {
4146             env->cur_pmmask &= UINT32_MAX;
4147         }
4148     }
4149     env->mmte |= EXT_STATUS_DIRTY;
4150 
4151     /* Set XS and SD bits, since PM CSRs are dirty */
4152     mstatus = env->mstatus | MSTATUS_XS;
4153     write_mstatus(env, csrno, mstatus);
4154     return RISCV_EXCP_NONE;
4155 }
4156 
4157 static RISCVException read_upmmask(CPURISCVState *env, int csrno,
4158                                    target_ulong *val)
4159 {
4160     *val = env->upmmask;
4161     return RISCV_EXCP_NONE;
4162 }
4163 
4164 static RISCVException write_upmmask(CPURISCVState *env, int csrno,
4165                                     target_ulong val)
4166 {
4167     uint64_t mstatus;
4168 
4169     /* if pm.current==0 we can't modify current PM CSRs */
4170     if (check_pm_current_disabled(env, csrno)) {
4171         return RISCV_EXCP_NONE;
4172     }
4173     env->upmmask = val;
4174     if ((cpu_address_mode(env) == PRV_U) && (env->mmte & U_PM_ENABLE)) {
4175         env->cur_pmmask = val;
4176         if (cpu_get_xl(env, PRV_U) == MXL_RV32) {
4177             env->cur_pmmask &= UINT32_MAX;
4178         }
4179     }
4180     env->mmte |= EXT_STATUS_DIRTY;
4181 
4182     /* Set XS and SD bits, since PM CSRs are dirty */
4183     mstatus = env->mstatus | MSTATUS_XS;
4184     write_mstatus(env, csrno, mstatus);
4185     return RISCV_EXCP_NONE;
4186 }
4187 
4188 static RISCVException read_mpmbase(CPURISCVState *env, int csrno,
4189                                    target_ulong *val)
4190 {
4191     *val = env->mpmbase;
4192     return RISCV_EXCP_NONE;
4193 }
4194 
4195 static RISCVException write_mpmbase(CPURISCVState *env, int csrno,
4196                                     target_ulong val)
4197 {
4198     uint64_t mstatus;
4199 
4200     env->mpmbase = val;
4201     if ((cpu_address_mode(env) == PRV_M) && (env->mmte & M_PM_ENABLE)) {
4202         env->cur_pmbase = val;
4203     }
4204     env->mmte |= EXT_STATUS_DIRTY;
4205 
4206     /* Set XS and SD bits, since PM CSRs are dirty */
4207     mstatus = env->mstatus | MSTATUS_XS;
4208     write_mstatus(env, csrno, mstatus);
4209     return RISCV_EXCP_NONE;
4210 }
4211 
4212 static RISCVException read_spmbase(CPURISCVState *env, int csrno,
4213                                    target_ulong *val)
4214 {
4215     *val = env->spmbase;
4216     return RISCV_EXCP_NONE;
4217 }
4218 
4219 static RISCVException write_spmbase(CPURISCVState *env, int csrno,
4220                                     target_ulong val)
4221 {
4222     uint64_t mstatus;
4223 
4224     /* if pm.current==0 we can't modify current PM CSRs */
4225     if (check_pm_current_disabled(env, csrno)) {
4226         return RISCV_EXCP_NONE;
4227     }
4228     env->spmbase = val;
4229     if ((cpu_address_mode(env) == PRV_S) && (env->mmte & S_PM_ENABLE)) {
4230         env->cur_pmbase = val;
4231         if (cpu_get_xl(env, PRV_S) == MXL_RV32) {
4232             env->cur_pmbase &= UINT32_MAX;
4233         }
4234     }
4235     env->mmte |= EXT_STATUS_DIRTY;
4236 
4237     /* Set XS and SD bits, since PM CSRs are dirty */
4238     mstatus = env->mstatus | MSTATUS_XS;
4239     write_mstatus(env, csrno, mstatus);
4240     return RISCV_EXCP_NONE;
4241 }
4242 
4243 static RISCVException read_upmbase(CPURISCVState *env, int csrno,
4244                                    target_ulong *val)
4245 {
4246     *val = env->upmbase;
4247     return RISCV_EXCP_NONE;
4248 }
4249 
4250 static RISCVException write_upmbase(CPURISCVState *env, int csrno,
4251                                     target_ulong val)
4252 {
4253     uint64_t mstatus;
4254 
4255     /* if pm.current==0 we can't modify current PM CSRs */
4256     if (check_pm_current_disabled(env, csrno)) {
4257         return RISCV_EXCP_NONE;
4258     }
4259     env->upmbase = val;
4260     if ((cpu_address_mode(env) == PRV_U) && (env->mmte & U_PM_ENABLE)) {
4261         env->cur_pmbase = val;
4262         if (cpu_get_xl(env, PRV_U) == MXL_RV32) {
4263             env->cur_pmbase &= UINT32_MAX;
4264         }
4265     }
4266     env->mmte |= EXT_STATUS_DIRTY;
4267 
4268     /* Set XS and SD bits, since PM CSRs are dirty */
4269     mstatus = env->mstatus | MSTATUS_XS;
4270     write_mstatus(env, csrno, mstatus);
4271     return RISCV_EXCP_NONE;
4272 }
4273 
4274 #endif
4275 
4276 /* Crypto Extension */
4277 target_ulong riscv_new_csr_seed(target_ulong new_value,
4278                                 target_ulong write_mask)
4279 {
4280     uint16_t random_v;
4281     Error *random_e = NULL;
4282     int random_r;
4283     target_ulong rval;
4284 
4285     random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
4286     if (unlikely(random_r < 0)) {
4287         /*
4288          * Failed, for unknown reasons in the crypto subsystem.
4289          * The best we can do is log the reason and return a
4290          * failure indication to the guest.  There is no reason
4291          * we know to expect the failure to be transitory, so
4292          * indicate DEAD to avoid having the guest spin on WAIT.
4293          */
4294         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
4295                       __func__, error_get_pretty(random_e));
4296         error_free(random_e);
4297         rval = SEED_OPST_DEAD;
4298     } else {
4299         rval = random_v | SEED_OPST_ES16;
4300     }
4301 
4302     return rval;
4303 }
4304 
4305 static RISCVException rmw_seed(CPURISCVState *env, int csrno,
4306                                target_ulong *ret_value,
4307                                target_ulong new_value,
4308                                target_ulong write_mask)
4309 {
4310     target_ulong rval;
4311 
4312     rval = riscv_new_csr_seed(new_value, write_mask);
4313 
4314     if (ret_value) {
4315         *ret_value = rval;
4316     }
4317 
4318     return RISCV_EXCP_NONE;
4319 }
4320 
4321 /*
4322  * riscv_csrrw - read and/or update control and status register
4323  *
4324  * csrr   <->  riscv_csrrw(env, csrno, ret_value, 0, 0);
4325  * csrrw  <->  riscv_csrrw(env, csrno, ret_value, value, -1);
4326  * csrrs  <->  riscv_csrrw(env, csrno, ret_value, -1, value);
4327  * csrrc  <->  riscv_csrrw(env, csrno, ret_value, 0, value);
4328  */
4329 
4330 static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
4331                                                int csrno,
4332                                                bool write_mask)
4333 {
4334     /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
4335     bool read_only = get_field(csrno, 0xC00) == 3;
4336     int csr_min_priv = csr_ops[csrno].min_priv_ver;
4337 
4338     /* ensure the CSR extension is enabled */
4339     if (!riscv_cpu_cfg(env)->ext_zicsr) {
4340         return RISCV_EXCP_ILLEGAL_INST;
4341     }
4342 
4343     /* ensure CSR is implemented by checking predicate */
4344     if (!csr_ops[csrno].predicate) {
4345         return RISCV_EXCP_ILLEGAL_INST;
4346     }
4347 
4348     /* privileged spec version check */
4349     if (env->priv_ver < csr_min_priv) {
4350         return RISCV_EXCP_ILLEGAL_INST;
4351     }
4352 
4353     /* read / write check */
4354     if (write_mask && read_only) {
4355         return RISCV_EXCP_ILLEGAL_INST;
4356     }
4357 
4358     /*
4359      * The predicate() not only does existence check but also does some
4360      * access control check which triggers for example virtual instruction
4361      * exception in some cases. When writing read-only CSRs in those cases
4362      * illegal instruction exception should be triggered instead of virtual
4363      * instruction exception. Hence this comes after the read / write check.
4364      */
4365     RISCVException ret = csr_ops[csrno].predicate(env, csrno);
4366     if (ret != RISCV_EXCP_NONE) {
4367         return ret;
4368     }
4369 
4370 #if !defined(CONFIG_USER_ONLY)
4371     int csr_priv, effective_priv = env->priv;
4372 
4373     if (riscv_has_ext(env, RVH) && env->priv == PRV_S &&
4374         !env->virt_enabled) {
4375         /*
4376          * We are in HS mode. Add 1 to the effective privilege level to
4377          * allow us to access the Hypervisor CSRs.
4378          */
4379         effective_priv++;
4380     }
4381 
4382     csr_priv = get_field(csrno, 0x300);
4383     if (!env->debugger && (effective_priv < csr_priv)) {
4384         if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
4385             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
4386         }
4387         return RISCV_EXCP_ILLEGAL_INST;
4388     }
4389 #endif
4390     return RISCV_EXCP_NONE;
4391 }
4392 
4393 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,
4394                                        target_ulong *ret_value,
4395                                        target_ulong new_value,
4396                                        target_ulong write_mask)
4397 {
4398     RISCVException ret;
4399     target_ulong old_value = 0;
4400 
4401     /* execute combined read/write operation if it exists */
4402     if (csr_ops[csrno].op) {
4403         return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
4404     }
4405 
4406     /*
4407      * ret_value == NULL means that rd=x0 and we're coming from helper_csrw()
4408      * and we can't throw side effects caused by CSR reads.
4409      */
4410     if (ret_value) {
4411         /* if no accessor exists then return failure */
4412         if (!csr_ops[csrno].read) {
4413             return RISCV_EXCP_ILLEGAL_INST;
4414         }
4415         /* read old value */
4416         ret = csr_ops[csrno].read(env, csrno, &old_value);
4417         if (ret != RISCV_EXCP_NONE) {
4418             return ret;
4419         }
4420     }
4421 
4422     /* write value if writable and write mask set, otherwise drop writes */
4423     if (write_mask) {
4424         new_value = (old_value & ~write_mask) | (new_value & write_mask);
4425         if (csr_ops[csrno].write) {
4426             ret = csr_ops[csrno].write(env, csrno, new_value);
4427             if (ret != RISCV_EXCP_NONE) {
4428                 return ret;
4429             }
4430         }
4431     }
4432 
4433     /* return old value */
4434     if (ret_value) {
4435         *ret_value = old_value;
4436     }
4437 
4438     return RISCV_EXCP_NONE;
4439 }
4440 
4441 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
4442                            target_ulong *ret_value,
4443                            target_ulong new_value, target_ulong write_mask)
4444 {
4445     RISCVException ret = riscv_csrrw_check(env, csrno, write_mask);
4446     if (ret != RISCV_EXCP_NONE) {
4447         return ret;
4448     }
4449 
4450     return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask);
4451 }
4452 
4453 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,
4454                                         Int128 *ret_value,
4455                                         Int128 new_value,
4456                                         Int128 write_mask)
4457 {
4458     RISCVException ret;
4459     Int128 old_value;
4460 
4461     /* read old value */
4462     ret = csr_ops[csrno].read128(env, csrno, &old_value);
4463     if (ret != RISCV_EXCP_NONE) {
4464         return ret;
4465     }
4466 
4467     /* write value if writable and write mask set, otherwise drop writes */
4468     if (int128_nz(write_mask)) {
4469         new_value = int128_or(int128_and(old_value, int128_not(write_mask)),
4470                               int128_and(new_value, write_mask));
4471         if (csr_ops[csrno].write128) {
4472             ret = csr_ops[csrno].write128(env, csrno, new_value);
4473             if (ret != RISCV_EXCP_NONE) {
4474                 return ret;
4475             }
4476         } else if (csr_ops[csrno].write) {
4477             /* avoids having to write wrappers for all registers */
4478             ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value));
4479             if (ret != RISCV_EXCP_NONE) {
4480                 return ret;
4481             }
4482         }
4483     }
4484 
4485     /* return old value */
4486     if (ret_value) {
4487         *ret_value = old_value;
4488     }
4489 
4490     return RISCV_EXCP_NONE;
4491 }
4492 
4493 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
4494                                 Int128 *ret_value,
4495                                 Int128 new_value, Int128 write_mask)
4496 {
4497     RISCVException ret;
4498 
4499     ret = riscv_csrrw_check(env, csrno, int128_nz(write_mask));
4500     if (ret != RISCV_EXCP_NONE) {
4501         return ret;
4502     }
4503 
4504     if (csr_ops[csrno].read128) {
4505         return riscv_csrrw_do128(env, csrno, ret_value, new_value, write_mask);
4506     }
4507 
4508     /*
4509      * Fall back to 64-bit version for now, if the 128-bit alternative isn't
4510      * at all defined.
4511      * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
4512      * significant), for those, this fallback is correctly handling the
4513      * accesses
4514      */
4515     target_ulong old_value;
4516     ret = riscv_csrrw_do64(env, csrno, &old_value,
4517                            int128_getlo(new_value),
4518                            int128_getlo(write_mask));
4519     if (ret == RISCV_EXCP_NONE && ret_value) {
4520         *ret_value = int128_make64(old_value);
4521     }
4522     return ret;
4523 }
4524 
4525 /*
4526  * Debugger support.  If not in user mode, set env->debugger before the
4527  * riscv_csrrw call and clear it after the call.
4528  */
4529 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
4530                                  target_ulong *ret_value,
4531                                  target_ulong new_value,
4532                                  target_ulong write_mask)
4533 {
4534     RISCVException ret;
4535 #if !defined(CONFIG_USER_ONLY)
4536     env->debugger = true;
4537 #endif
4538     ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
4539 #if !defined(CONFIG_USER_ONLY)
4540     env->debugger = false;
4541 #endif
4542     return ret;
4543 }
4544 
4545 static RISCVException read_jvt(CPURISCVState *env, int csrno,
4546                                target_ulong *val)
4547 {
4548     *val = env->jvt;
4549     return RISCV_EXCP_NONE;
4550 }
4551 
4552 static RISCVException write_jvt(CPURISCVState *env, int csrno,
4553                                 target_ulong val)
4554 {
4555     env->jvt = val;
4556     return RISCV_EXCP_NONE;
4557 }
4558 
4559 /*
4560  * Control and Status Register function table
4561  * riscv_csr_operations::predicate() must be provided for an implemented CSR
4562  */
4563 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
4564     /* User Floating-Point CSRs */
4565     [CSR_FFLAGS]   = { "fflags",   fs,     read_fflags,  write_fflags },
4566     [CSR_FRM]      = { "frm",      fs,     read_frm,     write_frm    },
4567     [CSR_FCSR]     = { "fcsr",     fs,     read_fcsr,    write_fcsr   },
4568     /* Vector CSRs */
4569     [CSR_VSTART]   = { "vstart",   vs,     read_vstart,  write_vstart },
4570     [CSR_VXSAT]    = { "vxsat",    vs,     read_vxsat,   write_vxsat  },
4571     [CSR_VXRM]     = { "vxrm",     vs,     read_vxrm,    write_vxrm   },
4572     [CSR_VCSR]     = { "vcsr",     vs,     read_vcsr,    write_vcsr   },
4573     [CSR_VL]       = { "vl",       vs,     read_vl                    },
4574     [CSR_VTYPE]    = { "vtype",    vs,     read_vtype                 },
4575     [CSR_VLENB]    = { "vlenb",    vs,     read_vlenb                 },
4576     /* User Timers and Counters */
4577     [CSR_CYCLE]    = { "cycle",    ctr,    read_hpmcounter  },
4578     [CSR_INSTRET]  = { "instret",  ctr,    read_hpmcounter  },
4579     [CSR_CYCLEH]   = { "cycleh",   ctr32,  read_hpmcounterh },
4580     [CSR_INSTRETH] = { "instreth", ctr32,  read_hpmcounterh },
4581 
4582     /*
4583      * In privileged mode, the monitor will have to emulate TIME CSRs only if
4584      * rdtime callback is not provided by machine/platform emulation.
4585      */
4586     [CSR_TIME]  = { "time",  ctr,   read_time  },
4587     [CSR_TIMEH] = { "timeh", ctr32, read_timeh },
4588 
4589     /* Crypto Extension */
4590     [CSR_SEED] = { "seed", seed, NULL, NULL, rmw_seed },
4591 
4592     /* Zcmt Extension */
4593     [CSR_JVT] = {"jvt", zcmt, read_jvt, write_jvt},
4594 
4595 #if !defined(CONFIG_USER_ONLY)
4596     /* Machine Timers and Counters */
4597     [CSR_MCYCLE]    = { "mcycle",    any,   read_hpmcounter,
4598                         write_mhpmcounter                    },
4599     [CSR_MINSTRET]  = { "minstret",  any,   read_hpmcounter,
4600                         write_mhpmcounter                    },
4601     [CSR_MCYCLEH]   = { "mcycleh",   any32, read_hpmcounterh,
4602                         write_mhpmcounterh                   },
4603     [CSR_MINSTRETH] = { "minstreth", any32, read_hpmcounterh,
4604                         write_mhpmcounterh                   },
4605 
4606     /* Machine Information Registers */
4607     [CSR_MVENDORID] = { "mvendorid", any,   read_mvendorid },
4608     [CSR_MARCHID]   = { "marchid",   any,   read_marchid   },
4609     [CSR_MIMPID]    = { "mimpid",    any,   read_mimpid    },
4610     [CSR_MHARTID]   = { "mhartid",   any,   read_mhartid   },
4611 
4612     [CSR_MCONFIGPTR]  = { "mconfigptr", any,   read_zero,
4613                           .min_priv_ver = PRIV_VERSION_1_12_0 },
4614     /* Machine Trap Setup */
4615     [CSR_MSTATUS]     = { "mstatus",    any,   read_mstatus, write_mstatus,
4616                           NULL,                read_mstatus_i128           },
4617     [CSR_MISA]        = { "misa",       any,   read_misa,    write_misa,
4618                           NULL,                read_misa_i128              },
4619     [CSR_MIDELEG]     = { "mideleg",    any,   NULL, NULL,   rmw_mideleg   },
4620     [CSR_MEDELEG]     = { "medeleg",    any,   read_medeleg, write_medeleg },
4621     [CSR_MIE]         = { "mie",        any,   NULL, NULL,   rmw_mie       },
4622     [CSR_MTVEC]       = { "mtvec",      any,   read_mtvec,   write_mtvec   },
4623     [CSR_MCOUNTEREN]  = { "mcounteren", umode, read_mcounteren,
4624                           write_mcounteren                                 },
4625 
4626     [CSR_MSTATUSH]    = { "mstatush",   any32, read_mstatush,
4627                           write_mstatush                                   },
4628 
4629     /* Machine Trap Handling */
4630     [CSR_MSCRATCH] = { "mscratch", any,  read_mscratch, write_mscratch,
4631                        NULL, read_mscratch_i128, write_mscratch_i128   },
4632     [CSR_MEPC]     = { "mepc",     any,  read_mepc,     write_mepc     },
4633     [CSR_MCAUSE]   = { "mcause",   any,  read_mcause,   write_mcause   },
4634     [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval    },
4635     [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip        },
4636 
4637     /* Machine-Level Window to Indirectly Accessed Registers (AIA) */
4638     [CSR_MISELECT] = { "miselect", aia_any,   NULL, NULL,    rmw_xiselect },
4639     [CSR_MIREG]    = { "mireg",    aia_any,   NULL, NULL,    rmw_xireg },
4640 
4641     /* Machine-Level Interrupts (AIA) */
4642     [CSR_MTOPEI]   = { "mtopei",   aia_any, NULL, NULL, rmw_xtopei },
4643     [CSR_MTOPI]    = { "mtopi",    aia_any, read_mtopi },
4644 
4645     /* Virtual Interrupts for Supervisor Level (AIA) */
4646     [CSR_MVIEN]    = { "mvien",    aia_any, NULL, NULL, rmw_mvien   },
4647     [CSR_MVIP]     = { "mvip",     aia_any, NULL, NULL, rmw_mvip    },
4648 
4649     /* Machine-Level High-Half CSRs (AIA) */
4650     [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
4651     [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh     },
4652     [CSR_MVIENH]   = { "mvienh",   aia_any32, NULL, NULL, rmw_mvienh   },
4653     [CSR_MVIPH]    = { "mviph",    aia_any32, NULL, NULL, rmw_mviph    },
4654     [CSR_MIPH]     = { "miph",     aia_any32, NULL, NULL, rmw_miph     },
4655 
4656     /* Execution environment configuration */
4657     [CSR_MENVCFG]  = { "menvcfg",  umode, read_menvcfg,  write_menvcfg,
4658                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4659     [CSR_MENVCFGH] = { "menvcfgh", umode32, read_menvcfgh, write_menvcfgh,
4660                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4661     [CSR_SENVCFG]  = { "senvcfg",  smode, read_senvcfg,  write_senvcfg,
4662                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4663     [CSR_HENVCFG]  = { "henvcfg",  hmode, read_henvcfg, write_henvcfg,
4664                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4665     [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh, write_henvcfgh,
4666                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4667 
4668     /* Smstateen extension CSRs */
4669     [CSR_MSTATEEN0] = { "mstateen0", mstateen, read_mstateen, write_mstateen0,
4670                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4671     [CSR_MSTATEEN0H] = { "mstateen0h", mstateen, read_mstateenh,
4672                           write_mstateen0h,
4673                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4674     [CSR_MSTATEEN1] = { "mstateen1", mstateen, read_mstateen,
4675                         write_mstateen_1_3,
4676                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4677     [CSR_MSTATEEN1H] = { "mstateen1h", mstateen, read_mstateenh,
4678                          write_mstateenh_1_3,
4679                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4680     [CSR_MSTATEEN2] = { "mstateen2", mstateen, read_mstateen,
4681                         write_mstateen_1_3,
4682                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4683     [CSR_MSTATEEN2H] = { "mstateen2h", mstateen, read_mstateenh,
4684                          write_mstateenh_1_3,
4685                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4686     [CSR_MSTATEEN3] = { "mstateen3", mstateen, read_mstateen,
4687                         write_mstateen_1_3,
4688                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4689     [CSR_MSTATEEN3H] = { "mstateen3h", mstateen, read_mstateenh,
4690                          write_mstateenh_1_3,
4691                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4692     [CSR_HSTATEEN0] = { "hstateen0", hstateen, read_hstateen, write_hstateen0,
4693                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4694     [CSR_HSTATEEN0H] = { "hstateen0h", hstateenh, read_hstateenh,
4695                          write_hstateen0h,
4696                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4697     [CSR_HSTATEEN1] = { "hstateen1", hstateen, read_hstateen,
4698                         write_hstateen_1_3,
4699                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4700     [CSR_HSTATEEN1H] = { "hstateen1h", hstateenh, read_hstateenh,
4701                          write_hstateenh_1_3,
4702                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4703     [CSR_HSTATEEN2] = { "hstateen2", hstateen, read_hstateen,
4704                         write_hstateen_1_3,
4705                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4706     [CSR_HSTATEEN2H] = { "hstateen2h", hstateenh, read_hstateenh,
4707                          write_hstateenh_1_3,
4708                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4709     [CSR_HSTATEEN3] = { "hstateen3", hstateen, read_hstateen,
4710                         write_hstateen_1_3,
4711                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4712     [CSR_HSTATEEN3H] = { "hstateen3h", hstateenh, read_hstateenh,
4713                          write_hstateenh_1_3,
4714                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4715     [CSR_SSTATEEN0] = { "sstateen0", sstateen, read_sstateen, write_sstateen0,
4716                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4717     [CSR_SSTATEEN1] = { "sstateen1", sstateen, read_sstateen,
4718                         write_sstateen_1_3,
4719                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4720     [CSR_SSTATEEN2] = { "sstateen2", sstateen, read_sstateen,
4721                         write_sstateen_1_3,
4722                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4723     [CSR_SSTATEEN3] = { "sstateen3", sstateen, read_sstateen,
4724                         write_sstateen_1_3,
4725                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4726 
4727     /* Supervisor Trap Setup */
4728     [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,    write_sstatus,
4729                          NULL,                read_sstatus_i128              },
4730     [CSR_SIE]        = { "sie",        smode, NULL,   NULL,    rmw_sie       },
4731     [CSR_STVEC]      = { "stvec",      smode, read_stvec,      write_stvec   },
4732     [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren,
4733                          write_scounteren                                    },
4734 
4735     /* Supervisor Trap Handling */
4736     [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch,
4737                        NULL, read_sscratch_i128, write_sscratch_i128    },
4738     [CSR_SEPC]     = { "sepc",     smode, read_sepc,     write_sepc     },
4739     [CSR_SCAUSE]   = { "scause",   smode, read_scause,   write_scause   },
4740     [CSR_STVAL]    = { "stval",    smode, read_stval,    write_stval    },
4741     [CSR_SIP]      = { "sip",      smode, NULL,    NULL, rmw_sip        },
4742     [CSR_STIMECMP] = { "stimecmp", sstc, read_stimecmp, write_stimecmp,
4743                        .min_priv_ver = PRIV_VERSION_1_12_0 },
4744     [CSR_STIMECMPH] = { "stimecmph", sstc_32, read_stimecmph, write_stimecmph,
4745                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4746     [CSR_VSTIMECMP] = { "vstimecmp", sstc, read_vstimecmp,
4747                         write_vstimecmp,
4748                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4749     [CSR_VSTIMECMPH] = { "vstimecmph", sstc_32, read_vstimecmph,
4750                          write_vstimecmph,
4751                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4752 
4753     /* Supervisor Protection and Translation */
4754     [CSR_SATP]     = { "satp",     satp, read_satp,     write_satp     },
4755 
4756     /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */
4757     [CSR_SISELECT]   = { "siselect",   aia_smode, NULL, NULL, rmw_xiselect },
4758     [CSR_SIREG]      = { "sireg",      aia_smode, NULL, NULL, rmw_xireg },
4759 
4760     /* Supervisor-Level Interrupts (AIA) */
4761     [CSR_STOPEI]     = { "stopei",     aia_smode, NULL, NULL, rmw_xtopei },
4762     [CSR_STOPI]      = { "stopi",      aia_smode, read_stopi },
4763 
4764     /* Supervisor-Level High-Half CSRs (AIA) */
4765     [CSR_SIEH]       = { "sieh",   aia_smode32, NULL, NULL, rmw_sieh },
4766     [CSR_SIPH]       = { "siph",   aia_smode32, NULL, NULL, rmw_siph },
4767 
4768     [CSR_HSTATUS]     = { "hstatus",     hmode,   read_hstatus, write_hstatus,
4769                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4770     [CSR_HEDELEG]     = { "hedeleg",     hmode,   read_hedeleg, write_hedeleg,
4771                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4772     [CSR_HIDELEG]     = { "hideleg",     hmode,   NULL,   NULL, rmw_hideleg,
4773                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4774     [CSR_HVIP]        = { "hvip",        hmode,   NULL,   NULL, rmw_hvip,
4775                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4776     [CSR_HIP]         = { "hip",         hmode,   NULL,   NULL, rmw_hip,
4777                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4778     [CSR_HIE]         = { "hie",         hmode,   NULL,   NULL, rmw_hie,
4779                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4780     [CSR_HCOUNTEREN]  = { "hcounteren",  hmode,   read_hcounteren,
4781                           write_hcounteren,
4782                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4783     [CSR_HGEIE]       = { "hgeie",       hmode,   read_hgeie,   write_hgeie,
4784                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4785     [CSR_HTVAL]       = { "htval",       hmode,   read_htval,   write_htval,
4786                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4787     [CSR_HTINST]      = { "htinst",      hmode,   read_htinst,  write_htinst,
4788                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4789     [CSR_HGEIP]       = { "hgeip",       hmode,   read_hgeip,
4790                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4791     [CSR_HGATP]       = { "hgatp",       hgatp,   read_hgatp,   write_hgatp,
4792                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4793     [CSR_HTIMEDELTA]  = { "htimedelta",  hmode,   read_htimedelta,
4794                           write_htimedelta,
4795                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4796     [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah,
4797                           write_htimedeltah,
4798                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4799 
4800     [CSR_VSSTATUS]    = { "vsstatus",    hmode,   read_vsstatus,
4801                           write_vsstatus,
4802                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4803     [CSR_VSIP]        = { "vsip",        hmode,   NULL,    NULL, rmw_vsip,
4804                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4805     [CSR_VSIE]        = { "vsie",        hmode,   NULL,    NULL, rmw_vsie ,
4806                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4807     [CSR_VSTVEC]      = { "vstvec",      hmode,   read_vstvec,   write_vstvec,
4808                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4809     [CSR_VSSCRATCH]   = { "vsscratch",   hmode,   read_vsscratch,
4810                           write_vsscratch,
4811                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4812     [CSR_VSEPC]       = { "vsepc",       hmode,   read_vsepc,    write_vsepc,
4813                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4814     [CSR_VSCAUSE]     = { "vscause",     hmode,   read_vscause,  write_vscause,
4815                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4816     [CSR_VSTVAL]      = { "vstval",      hmode,   read_vstval,   write_vstval,
4817                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4818     [CSR_VSATP]       = { "vsatp",       hmode,   read_vsatp,    write_vsatp,
4819                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4820 
4821     [CSR_MTVAL2]      = { "mtval2",      hmode,   read_mtval2,   write_mtval2,
4822                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4823     [CSR_MTINST]      = { "mtinst",      hmode,   read_mtinst,   write_mtinst,
4824                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4825 
4826     /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */
4827     [CSR_HVIEN]       = { "hvien",       aia_hmode, NULL, NULL, rmw_hvien },
4828     [CSR_HVICTL]      = { "hvictl",      aia_hmode, read_hvictl,
4829                           write_hvictl                                      },
4830     [CSR_HVIPRIO1]    = { "hviprio1",    aia_hmode, read_hviprio1,
4831                           write_hviprio1                                    },
4832     [CSR_HVIPRIO2]    = { "hviprio2",    aia_hmode, read_hviprio2,
4833                           write_hviprio2                                    },
4834     /*
4835      * VS-Level Window to Indirectly Accessed Registers (H-extension with AIA)
4836      */
4837     [CSR_VSISELECT]   = { "vsiselect",   aia_hmode, NULL, NULL,
4838                           rmw_xiselect                                     },
4839     [CSR_VSIREG]      = { "vsireg",      aia_hmode, NULL, NULL, rmw_xireg  },
4840 
4841     /* VS-Level Interrupts (H-extension with AIA) */
4842     [CSR_VSTOPEI]     = { "vstopei",     aia_hmode, NULL, NULL, rmw_xtopei },
4843     [CSR_VSTOPI]      = { "vstopi",      aia_hmode, read_vstopi },
4844 
4845     /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
4846     [CSR_HIDELEGH]    = { "hidelegh",    aia_hmode32, NULL, NULL,
4847                           rmw_hidelegh                                      },
4848     [CSR_HVIENH]      = { "hvienh",      aia_hmode32, NULL, NULL, rmw_hvienh },
4849     [CSR_HVIPH]       = { "hviph",       aia_hmode32, NULL, NULL, rmw_hviph },
4850     [CSR_HVIPRIO1H]   = { "hviprio1h",   aia_hmode32, read_hviprio1h,
4851                           write_hviprio1h                                   },
4852     [CSR_HVIPRIO2H]   = { "hviprio2h",   aia_hmode32, read_hviprio2h,
4853                           write_hviprio2h                                   },
4854     [CSR_VSIEH]       = { "vsieh",       aia_hmode32, NULL, NULL, rmw_vsieh },
4855     [CSR_VSIPH]       = { "vsiph",       aia_hmode32, NULL, NULL, rmw_vsiph },
4856 
4857     /* Physical Memory Protection */
4858     [CSR_MSECCFG]    = { "mseccfg",   have_mseccfg, read_mseccfg, write_mseccfg,
4859                          .min_priv_ver = PRIV_VERSION_1_11_0           },
4860     [CSR_PMPCFG0]    = { "pmpcfg0",   pmp, read_pmpcfg,  write_pmpcfg  },
4861     [CSR_PMPCFG1]    = { "pmpcfg1",   pmp, read_pmpcfg,  write_pmpcfg  },
4862     [CSR_PMPCFG2]    = { "pmpcfg2",   pmp, read_pmpcfg,  write_pmpcfg  },
4863     [CSR_PMPCFG3]    = { "pmpcfg3",   pmp, read_pmpcfg,  write_pmpcfg  },
4864     [CSR_PMPADDR0]   = { "pmpaddr0",  pmp, read_pmpaddr, write_pmpaddr },
4865     [CSR_PMPADDR1]   = { "pmpaddr1",  pmp, read_pmpaddr, write_pmpaddr },
4866     [CSR_PMPADDR2]   = { "pmpaddr2",  pmp, read_pmpaddr, write_pmpaddr },
4867     [CSR_PMPADDR3]   = { "pmpaddr3",  pmp, read_pmpaddr, write_pmpaddr },
4868     [CSR_PMPADDR4]   = { "pmpaddr4",  pmp, read_pmpaddr, write_pmpaddr },
4869     [CSR_PMPADDR5]   = { "pmpaddr5",  pmp, read_pmpaddr, write_pmpaddr },
4870     [CSR_PMPADDR6]   = { "pmpaddr6",  pmp, read_pmpaddr, write_pmpaddr },
4871     [CSR_PMPADDR7]   = { "pmpaddr7",  pmp, read_pmpaddr, write_pmpaddr },
4872     [CSR_PMPADDR8]   = { "pmpaddr8",  pmp, read_pmpaddr, write_pmpaddr },
4873     [CSR_PMPADDR9]   = { "pmpaddr9",  pmp, read_pmpaddr, write_pmpaddr },
4874     [CSR_PMPADDR10]  = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr },
4875     [CSR_PMPADDR11]  = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr },
4876     [CSR_PMPADDR12]  = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr },
4877     [CSR_PMPADDR13]  = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr },
4878     [CSR_PMPADDR14] =  { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr },
4879     [CSR_PMPADDR15] =  { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr },
4880 
4881     /* Debug CSRs */
4882     [CSR_TSELECT]   =  { "tselect",  debug, read_tselect,  write_tselect  },
4883     [CSR_TDATA1]    =  { "tdata1",   debug, read_tdata,    write_tdata    },
4884     [CSR_TDATA2]    =  { "tdata2",   debug, read_tdata,    write_tdata    },
4885     [CSR_TDATA3]    =  { "tdata3",   debug, read_tdata,    write_tdata    },
4886     [CSR_TINFO]     =  { "tinfo",    debug, read_tinfo,    write_ignore   },
4887     [CSR_MCONTEXT]  =  { "mcontext", debug, read_mcontext, write_mcontext },
4888 
4889     /* User Pointer Masking */
4890     [CSR_UMTE]    =    { "umte",    pointer_masking, read_umte,  write_umte },
4891     [CSR_UPMMASK] =    { "upmmask", pointer_masking, read_upmmask,
4892                          write_upmmask                                      },
4893     [CSR_UPMBASE] =    { "upmbase", pointer_masking, read_upmbase,
4894                          write_upmbase                                      },
4895     /* Machine Pointer Masking */
4896     [CSR_MMTE]    =    { "mmte",    pointer_masking, read_mmte,  write_mmte },
4897     [CSR_MPMMASK] =    { "mpmmask", pointer_masking, read_mpmmask,
4898                          write_mpmmask                                      },
4899     [CSR_MPMBASE] =    { "mpmbase", pointer_masking, read_mpmbase,
4900                          write_mpmbase                                      },
4901     /* Supervisor Pointer Masking */
4902     [CSR_SMTE]    =    { "smte",    pointer_masking, read_smte,  write_smte },
4903     [CSR_SPMMASK] =    { "spmmask", pointer_masking, read_spmmask,
4904                          write_spmmask                                      },
4905     [CSR_SPMBASE] =    { "spmbase", pointer_masking, read_spmbase,
4906                          write_spmbase                                      },
4907 
4908     /* Performance Counters */
4909     [CSR_HPMCOUNTER3]    = { "hpmcounter3",    ctr,    read_hpmcounter },
4910     [CSR_HPMCOUNTER4]    = { "hpmcounter4",    ctr,    read_hpmcounter },
4911     [CSR_HPMCOUNTER5]    = { "hpmcounter5",    ctr,    read_hpmcounter },
4912     [CSR_HPMCOUNTER6]    = { "hpmcounter6",    ctr,    read_hpmcounter },
4913     [CSR_HPMCOUNTER7]    = { "hpmcounter7",    ctr,    read_hpmcounter },
4914     [CSR_HPMCOUNTER8]    = { "hpmcounter8",    ctr,    read_hpmcounter },
4915     [CSR_HPMCOUNTER9]    = { "hpmcounter9",    ctr,    read_hpmcounter },
4916     [CSR_HPMCOUNTER10]   = { "hpmcounter10",   ctr,    read_hpmcounter },
4917     [CSR_HPMCOUNTER11]   = { "hpmcounter11",   ctr,    read_hpmcounter },
4918     [CSR_HPMCOUNTER12]   = { "hpmcounter12",   ctr,    read_hpmcounter },
4919     [CSR_HPMCOUNTER13]   = { "hpmcounter13",   ctr,    read_hpmcounter },
4920     [CSR_HPMCOUNTER14]   = { "hpmcounter14",   ctr,    read_hpmcounter },
4921     [CSR_HPMCOUNTER15]   = { "hpmcounter15",   ctr,    read_hpmcounter },
4922     [CSR_HPMCOUNTER16]   = { "hpmcounter16",   ctr,    read_hpmcounter },
4923     [CSR_HPMCOUNTER17]   = { "hpmcounter17",   ctr,    read_hpmcounter },
4924     [CSR_HPMCOUNTER18]   = { "hpmcounter18",   ctr,    read_hpmcounter },
4925     [CSR_HPMCOUNTER19]   = { "hpmcounter19",   ctr,    read_hpmcounter },
4926     [CSR_HPMCOUNTER20]   = { "hpmcounter20",   ctr,    read_hpmcounter },
4927     [CSR_HPMCOUNTER21]   = { "hpmcounter21",   ctr,    read_hpmcounter },
4928     [CSR_HPMCOUNTER22]   = { "hpmcounter22",   ctr,    read_hpmcounter },
4929     [CSR_HPMCOUNTER23]   = { "hpmcounter23",   ctr,    read_hpmcounter },
4930     [CSR_HPMCOUNTER24]   = { "hpmcounter24",   ctr,    read_hpmcounter },
4931     [CSR_HPMCOUNTER25]   = { "hpmcounter25",   ctr,    read_hpmcounter },
4932     [CSR_HPMCOUNTER26]   = { "hpmcounter26",   ctr,    read_hpmcounter },
4933     [CSR_HPMCOUNTER27]   = { "hpmcounter27",   ctr,    read_hpmcounter },
4934     [CSR_HPMCOUNTER28]   = { "hpmcounter28",   ctr,    read_hpmcounter },
4935     [CSR_HPMCOUNTER29]   = { "hpmcounter29",   ctr,    read_hpmcounter },
4936     [CSR_HPMCOUNTER30]   = { "hpmcounter30",   ctr,    read_hpmcounter },
4937     [CSR_HPMCOUNTER31]   = { "hpmcounter31",   ctr,    read_hpmcounter },
4938 
4939     [CSR_MHPMCOUNTER3]   = { "mhpmcounter3",   mctr,    read_hpmcounter,
4940                              write_mhpmcounter                         },
4941     [CSR_MHPMCOUNTER4]   = { "mhpmcounter4",   mctr,    read_hpmcounter,
4942                              write_mhpmcounter                         },
4943     [CSR_MHPMCOUNTER5]   = { "mhpmcounter5",   mctr,    read_hpmcounter,
4944                              write_mhpmcounter                         },
4945     [CSR_MHPMCOUNTER6]   = { "mhpmcounter6",   mctr,    read_hpmcounter,
4946                              write_mhpmcounter                         },
4947     [CSR_MHPMCOUNTER7]   = { "mhpmcounter7",   mctr,    read_hpmcounter,
4948                              write_mhpmcounter                         },
4949     [CSR_MHPMCOUNTER8]   = { "mhpmcounter8",   mctr,    read_hpmcounter,
4950                              write_mhpmcounter                         },
4951     [CSR_MHPMCOUNTER9]   = { "mhpmcounter9",   mctr,    read_hpmcounter,
4952                              write_mhpmcounter                         },
4953     [CSR_MHPMCOUNTER10]  = { "mhpmcounter10",  mctr,    read_hpmcounter,
4954                              write_mhpmcounter                         },
4955     [CSR_MHPMCOUNTER11]  = { "mhpmcounter11",  mctr,    read_hpmcounter,
4956                              write_mhpmcounter                         },
4957     [CSR_MHPMCOUNTER12]  = { "mhpmcounter12",  mctr,    read_hpmcounter,
4958                              write_mhpmcounter                         },
4959     [CSR_MHPMCOUNTER13]  = { "mhpmcounter13",  mctr,    read_hpmcounter,
4960                              write_mhpmcounter                         },
4961     [CSR_MHPMCOUNTER14]  = { "mhpmcounter14",  mctr,    read_hpmcounter,
4962                              write_mhpmcounter                         },
4963     [CSR_MHPMCOUNTER15]  = { "mhpmcounter15",  mctr,    read_hpmcounter,
4964                              write_mhpmcounter                         },
4965     [CSR_MHPMCOUNTER16]  = { "mhpmcounter16",  mctr,    read_hpmcounter,
4966                              write_mhpmcounter                         },
4967     [CSR_MHPMCOUNTER17]  = { "mhpmcounter17",  mctr,    read_hpmcounter,
4968                              write_mhpmcounter                         },
4969     [CSR_MHPMCOUNTER18]  = { "mhpmcounter18",  mctr,    read_hpmcounter,
4970                              write_mhpmcounter                         },
4971     [CSR_MHPMCOUNTER19]  = { "mhpmcounter19",  mctr,    read_hpmcounter,
4972                              write_mhpmcounter                         },
4973     [CSR_MHPMCOUNTER20]  = { "mhpmcounter20",  mctr,    read_hpmcounter,
4974                              write_mhpmcounter                         },
4975     [CSR_MHPMCOUNTER21]  = { "mhpmcounter21",  mctr,    read_hpmcounter,
4976                              write_mhpmcounter                         },
4977     [CSR_MHPMCOUNTER22]  = { "mhpmcounter22",  mctr,    read_hpmcounter,
4978                              write_mhpmcounter                         },
4979     [CSR_MHPMCOUNTER23]  = { "mhpmcounter23",  mctr,    read_hpmcounter,
4980                              write_mhpmcounter                         },
4981     [CSR_MHPMCOUNTER24]  = { "mhpmcounter24",  mctr,    read_hpmcounter,
4982                              write_mhpmcounter                         },
4983     [CSR_MHPMCOUNTER25]  = { "mhpmcounter25",  mctr,    read_hpmcounter,
4984                              write_mhpmcounter                         },
4985     [CSR_MHPMCOUNTER26]  = { "mhpmcounter26",  mctr,    read_hpmcounter,
4986                              write_mhpmcounter                         },
4987     [CSR_MHPMCOUNTER27]  = { "mhpmcounter27",  mctr,    read_hpmcounter,
4988                              write_mhpmcounter                         },
4989     [CSR_MHPMCOUNTER28]  = { "mhpmcounter28",  mctr,    read_hpmcounter,
4990                              write_mhpmcounter                         },
4991     [CSR_MHPMCOUNTER29]  = { "mhpmcounter29",  mctr,    read_hpmcounter,
4992                              write_mhpmcounter                         },
4993     [CSR_MHPMCOUNTER30]  = { "mhpmcounter30",  mctr,    read_hpmcounter,
4994                              write_mhpmcounter                         },
4995     [CSR_MHPMCOUNTER31]  = { "mhpmcounter31",  mctr,    read_hpmcounter,
4996                              write_mhpmcounter                         },
4997 
4998     [CSR_MCOUNTINHIBIT]  = { "mcountinhibit",  any, read_mcountinhibit,
4999                              write_mcountinhibit,
5000                              .min_priv_ver = PRIV_VERSION_1_11_0       },
5001 
5002     [CSR_MHPMEVENT3]     = { "mhpmevent3",     any,    read_mhpmevent,
5003                              write_mhpmevent                           },
5004     [CSR_MHPMEVENT4]     = { "mhpmevent4",     any,    read_mhpmevent,
5005                              write_mhpmevent                           },
5006     [CSR_MHPMEVENT5]     = { "mhpmevent5",     any,    read_mhpmevent,
5007                              write_mhpmevent                           },
5008     [CSR_MHPMEVENT6]     = { "mhpmevent6",     any,    read_mhpmevent,
5009                              write_mhpmevent                           },
5010     [CSR_MHPMEVENT7]     = { "mhpmevent7",     any,    read_mhpmevent,
5011                              write_mhpmevent                           },
5012     [CSR_MHPMEVENT8]     = { "mhpmevent8",     any,    read_mhpmevent,
5013                              write_mhpmevent                           },
5014     [CSR_MHPMEVENT9]     = { "mhpmevent9",     any,    read_mhpmevent,
5015                              write_mhpmevent                           },
5016     [CSR_MHPMEVENT10]    = { "mhpmevent10",    any,    read_mhpmevent,
5017                              write_mhpmevent                           },
5018     [CSR_MHPMEVENT11]    = { "mhpmevent11",    any,    read_mhpmevent,
5019                              write_mhpmevent                           },
5020     [CSR_MHPMEVENT12]    = { "mhpmevent12",    any,    read_mhpmevent,
5021                              write_mhpmevent                           },
5022     [CSR_MHPMEVENT13]    = { "mhpmevent13",    any,    read_mhpmevent,
5023                              write_mhpmevent                           },
5024     [CSR_MHPMEVENT14]    = { "mhpmevent14",    any,    read_mhpmevent,
5025                              write_mhpmevent                           },
5026     [CSR_MHPMEVENT15]    = { "mhpmevent15",    any,    read_mhpmevent,
5027                              write_mhpmevent                           },
5028     [CSR_MHPMEVENT16]    = { "mhpmevent16",    any,    read_mhpmevent,
5029                              write_mhpmevent                           },
5030     [CSR_MHPMEVENT17]    = { "mhpmevent17",    any,    read_mhpmevent,
5031                              write_mhpmevent                           },
5032     [CSR_MHPMEVENT18]    = { "mhpmevent18",    any,    read_mhpmevent,
5033                              write_mhpmevent                           },
5034     [CSR_MHPMEVENT19]    = { "mhpmevent19",    any,    read_mhpmevent,
5035                              write_mhpmevent                           },
5036     [CSR_MHPMEVENT20]    = { "mhpmevent20",    any,    read_mhpmevent,
5037                              write_mhpmevent                           },
5038     [CSR_MHPMEVENT21]    = { "mhpmevent21",    any,    read_mhpmevent,
5039                              write_mhpmevent                           },
5040     [CSR_MHPMEVENT22]    = { "mhpmevent22",    any,    read_mhpmevent,
5041                              write_mhpmevent                           },
5042     [CSR_MHPMEVENT23]    = { "mhpmevent23",    any,    read_mhpmevent,
5043                              write_mhpmevent                           },
5044     [CSR_MHPMEVENT24]    = { "mhpmevent24",    any,    read_mhpmevent,
5045                              write_mhpmevent                           },
5046     [CSR_MHPMEVENT25]    = { "mhpmevent25",    any,    read_mhpmevent,
5047                              write_mhpmevent                           },
5048     [CSR_MHPMEVENT26]    = { "mhpmevent26",    any,    read_mhpmevent,
5049                              write_mhpmevent                           },
5050     [CSR_MHPMEVENT27]    = { "mhpmevent27",    any,    read_mhpmevent,
5051                              write_mhpmevent                           },
5052     [CSR_MHPMEVENT28]    = { "mhpmevent28",    any,    read_mhpmevent,
5053                              write_mhpmevent                           },
5054     [CSR_MHPMEVENT29]    = { "mhpmevent29",    any,    read_mhpmevent,
5055                              write_mhpmevent                           },
5056     [CSR_MHPMEVENT30]    = { "mhpmevent30",    any,    read_mhpmevent,
5057                              write_mhpmevent                           },
5058     [CSR_MHPMEVENT31]    = { "mhpmevent31",    any,    read_mhpmevent,
5059                              write_mhpmevent                           },
5060 
5061     [CSR_MHPMEVENT3H]    = { "mhpmevent3h",    sscofpmf,  read_mhpmeventh,
5062                              write_mhpmeventh,
5063                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5064     [CSR_MHPMEVENT4H]    = { "mhpmevent4h",    sscofpmf,  read_mhpmeventh,
5065                              write_mhpmeventh,
5066                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5067     [CSR_MHPMEVENT5H]    = { "mhpmevent5h",    sscofpmf,  read_mhpmeventh,
5068                              write_mhpmeventh,
5069                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5070     [CSR_MHPMEVENT6H]    = { "mhpmevent6h",    sscofpmf,  read_mhpmeventh,
5071                              write_mhpmeventh,
5072                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5073     [CSR_MHPMEVENT7H]    = { "mhpmevent7h",    sscofpmf,  read_mhpmeventh,
5074                              write_mhpmeventh,
5075                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5076     [CSR_MHPMEVENT8H]    = { "mhpmevent8h",    sscofpmf,  read_mhpmeventh,
5077                              write_mhpmeventh,
5078                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5079     [CSR_MHPMEVENT9H]    = { "mhpmevent9h",    sscofpmf,  read_mhpmeventh,
5080                              write_mhpmeventh,
5081                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5082     [CSR_MHPMEVENT10H]   = { "mhpmevent10h",    sscofpmf,  read_mhpmeventh,
5083                              write_mhpmeventh,
5084                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5085     [CSR_MHPMEVENT11H]   = { "mhpmevent11h",    sscofpmf,  read_mhpmeventh,
5086                              write_mhpmeventh,
5087                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5088     [CSR_MHPMEVENT12H]   = { "mhpmevent12h",    sscofpmf,  read_mhpmeventh,
5089                              write_mhpmeventh,
5090                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5091     [CSR_MHPMEVENT13H]   = { "mhpmevent13h",    sscofpmf,  read_mhpmeventh,
5092                              write_mhpmeventh,
5093                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5094     [CSR_MHPMEVENT14H]   = { "mhpmevent14h",    sscofpmf,  read_mhpmeventh,
5095                              write_mhpmeventh,
5096                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5097     [CSR_MHPMEVENT15H]   = { "mhpmevent15h",    sscofpmf,  read_mhpmeventh,
5098                              write_mhpmeventh,
5099                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5100     [CSR_MHPMEVENT16H]   = { "mhpmevent16h",    sscofpmf,  read_mhpmeventh,
5101                              write_mhpmeventh,
5102                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5103     [CSR_MHPMEVENT17H]   = { "mhpmevent17h",    sscofpmf,  read_mhpmeventh,
5104                              write_mhpmeventh,
5105                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5106     [CSR_MHPMEVENT18H]   = { "mhpmevent18h",    sscofpmf,  read_mhpmeventh,
5107                              write_mhpmeventh,
5108                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5109     [CSR_MHPMEVENT19H]   = { "mhpmevent19h",    sscofpmf,  read_mhpmeventh,
5110                              write_mhpmeventh,
5111                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5112     [CSR_MHPMEVENT20H]   = { "mhpmevent20h",    sscofpmf,  read_mhpmeventh,
5113                              write_mhpmeventh,
5114                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5115     [CSR_MHPMEVENT21H]   = { "mhpmevent21h",    sscofpmf,  read_mhpmeventh,
5116                              write_mhpmeventh,
5117                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5118     [CSR_MHPMEVENT22H]   = { "mhpmevent22h",    sscofpmf,  read_mhpmeventh,
5119                              write_mhpmeventh,
5120                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5121     [CSR_MHPMEVENT23H]   = { "mhpmevent23h",    sscofpmf,  read_mhpmeventh,
5122                              write_mhpmeventh,
5123                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5124     [CSR_MHPMEVENT24H]   = { "mhpmevent24h",    sscofpmf,  read_mhpmeventh,
5125                              write_mhpmeventh,
5126                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5127     [CSR_MHPMEVENT25H]   = { "mhpmevent25h",    sscofpmf,  read_mhpmeventh,
5128                              write_mhpmeventh,
5129                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5130     [CSR_MHPMEVENT26H]   = { "mhpmevent26h",    sscofpmf,  read_mhpmeventh,
5131                              write_mhpmeventh,
5132                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5133     [CSR_MHPMEVENT27H]   = { "mhpmevent27h",    sscofpmf,  read_mhpmeventh,
5134                              write_mhpmeventh,
5135                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5136     [CSR_MHPMEVENT28H]   = { "mhpmevent28h",    sscofpmf,  read_mhpmeventh,
5137                              write_mhpmeventh,
5138                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5139     [CSR_MHPMEVENT29H]   = { "mhpmevent29h",    sscofpmf,  read_mhpmeventh,
5140                              write_mhpmeventh,
5141                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5142     [CSR_MHPMEVENT30H]   = { "mhpmevent30h",    sscofpmf,  read_mhpmeventh,
5143                              write_mhpmeventh,
5144                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5145     [CSR_MHPMEVENT31H]   = { "mhpmevent31h",    sscofpmf,  read_mhpmeventh,
5146                              write_mhpmeventh,
5147                              .min_priv_ver = PRIV_VERSION_1_12_0        },
5148 
5149     [CSR_HPMCOUNTER3H]   = { "hpmcounter3h",   ctr32,  read_hpmcounterh },
5150     [CSR_HPMCOUNTER4H]   = { "hpmcounter4h",   ctr32,  read_hpmcounterh },
5151     [CSR_HPMCOUNTER5H]   = { "hpmcounter5h",   ctr32,  read_hpmcounterh },
5152     [CSR_HPMCOUNTER6H]   = { "hpmcounter6h",   ctr32,  read_hpmcounterh },
5153     [CSR_HPMCOUNTER7H]   = { "hpmcounter7h",   ctr32,  read_hpmcounterh },
5154     [CSR_HPMCOUNTER8H]   = { "hpmcounter8h",   ctr32,  read_hpmcounterh },
5155     [CSR_HPMCOUNTER9H]   = { "hpmcounter9h",   ctr32,  read_hpmcounterh },
5156     [CSR_HPMCOUNTER10H]  = { "hpmcounter10h",  ctr32,  read_hpmcounterh },
5157     [CSR_HPMCOUNTER11H]  = { "hpmcounter11h",  ctr32,  read_hpmcounterh },
5158     [CSR_HPMCOUNTER12H]  = { "hpmcounter12h",  ctr32,  read_hpmcounterh },
5159     [CSR_HPMCOUNTER13H]  = { "hpmcounter13h",  ctr32,  read_hpmcounterh },
5160     [CSR_HPMCOUNTER14H]  = { "hpmcounter14h",  ctr32,  read_hpmcounterh },
5161     [CSR_HPMCOUNTER15H]  = { "hpmcounter15h",  ctr32,  read_hpmcounterh },
5162     [CSR_HPMCOUNTER16H]  = { "hpmcounter16h",  ctr32,  read_hpmcounterh },
5163     [CSR_HPMCOUNTER17H]  = { "hpmcounter17h",  ctr32,  read_hpmcounterh },
5164     [CSR_HPMCOUNTER18H]  = { "hpmcounter18h",  ctr32,  read_hpmcounterh },
5165     [CSR_HPMCOUNTER19H]  = { "hpmcounter19h",  ctr32,  read_hpmcounterh },
5166     [CSR_HPMCOUNTER20H]  = { "hpmcounter20h",  ctr32,  read_hpmcounterh },
5167     [CSR_HPMCOUNTER21H]  = { "hpmcounter21h",  ctr32,  read_hpmcounterh },
5168     [CSR_HPMCOUNTER22H]  = { "hpmcounter22h",  ctr32,  read_hpmcounterh },
5169     [CSR_HPMCOUNTER23H]  = { "hpmcounter23h",  ctr32,  read_hpmcounterh },
5170     [CSR_HPMCOUNTER24H]  = { "hpmcounter24h",  ctr32,  read_hpmcounterh },
5171     [CSR_HPMCOUNTER25H]  = { "hpmcounter25h",  ctr32,  read_hpmcounterh },
5172     [CSR_HPMCOUNTER26H]  = { "hpmcounter26h",  ctr32,  read_hpmcounterh },
5173     [CSR_HPMCOUNTER27H]  = { "hpmcounter27h",  ctr32,  read_hpmcounterh },
5174     [CSR_HPMCOUNTER28H]  = { "hpmcounter28h",  ctr32,  read_hpmcounterh },
5175     [CSR_HPMCOUNTER29H]  = { "hpmcounter29h",  ctr32,  read_hpmcounterh },
5176     [CSR_HPMCOUNTER30H]  = { "hpmcounter30h",  ctr32,  read_hpmcounterh },
5177     [CSR_HPMCOUNTER31H]  = { "hpmcounter31h",  ctr32,  read_hpmcounterh },
5178 
5179     [CSR_MHPMCOUNTER3H]  = { "mhpmcounter3h",  mctr32,  read_hpmcounterh,
5180                              write_mhpmcounterh                         },
5181     [CSR_MHPMCOUNTER4H]  = { "mhpmcounter4h",  mctr32,  read_hpmcounterh,
5182                              write_mhpmcounterh                         },
5183     [CSR_MHPMCOUNTER5H]  = { "mhpmcounter5h",  mctr32,  read_hpmcounterh,
5184                              write_mhpmcounterh                         },
5185     [CSR_MHPMCOUNTER6H]  = { "mhpmcounter6h",  mctr32,  read_hpmcounterh,
5186                              write_mhpmcounterh                         },
5187     [CSR_MHPMCOUNTER7H]  = { "mhpmcounter7h",  mctr32,  read_hpmcounterh,
5188                              write_mhpmcounterh                         },
5189     [CSR_MHPMCOUNTER8H]  = { "mhpmcounter8h",  mctr32,  read_hpmcounterh,
5190                              write_mhpmcounterh                         },
5191     [CSR_MHPMCOUNTER9H]  = { "mhpmcounter9h",  mctr32,  read_hpmcounterh,
5192                              write_mhpmcounterh                         },
5193     [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", mctr32,  read_hpmcounterh,
5194                              write_mhpmcounterh                         },
5195     [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", mctr32,  read_hpmcounterh,
5196                              write_mhpmcounterh                         },
5197     [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", mctr32,  read_hpmcounterh,
5198                              write_mhpmcounterh                         },
5199     [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", mctr32,  read_hpmcounterh,
5200                              write_mhpmcounterh                         },
5201     [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", mctr32,  read_hpmcounterh,
5202                              write_mhpmcounterh                         },
5203     [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", mctr32,  read_hpmcounterh,
5204                              write_mhpmcounterh                         },
5205     [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", mctr32,  read_hpmcounterh,
5206                              write_mhpmcounterh                         },
5207     [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", mctr32,  read_hpmcounterh,
5208                              write_mhpmcounterh                         },
5209     [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", mctr32,  read_hpmcounterh,
5210                              write_mhpmcounterh                         },
5211     [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", mctr32,  read_hpmcounterh,
5212                              write_mhpmcounterh                         },
5213     [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", mctr32,  read_hpmcounterh,
5214                              write_mhpmcounterh                         },
5215     [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", mctr32,  read_hpmcounterh,
5216                              write_mhpmcounterh                         },
5217     [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", mctr32,  read_hpmcounterh,
5218                              write_mhpmcounterh                         },
5219     [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", mctr32,  read_hpmcounterh,
5220                              write_mhpmcounterh                         },
5221     [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", mctr32,  read_hpmcounterh,
5222                              write_mhpmcounterh                         },
5223     [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", mctr32,  read_hpmcounterh,
5224                              write_mhpmcounterh                         },
5225     [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", mctr32,  read_hpmcounterh,
5226                              write_mhpmcounterh                         },
5227     [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", mctr32,  read_hpmcounterh,
5228                              write_mhpmcounterh                         },
5229     [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", mctr32,  read_hpmcounterh,
5230                              write_mhpmcounterh                         },
5231     [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", mctr32,  read_hpmcounterh,
5232                              write_mhpmcounterh                         },
5233     [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", mctr32,  read_hpmcounterh,
5234                              write_mhpmcounterh                         },
5235     [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32,  read_hpmcounterh,
5236                              write_mhpmcounterh                         },
5237     [CSR_SCOUNTOVF]      = { "scountovf", sscofpmf,  read_scountovf,
5238                              .min_priv_ver = PRIV_VERSION_1_12_0 },
5239 
5240 #endif /* !CONFIG_USER_ONLY */
5241 };
5242