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