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