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