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