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