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