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