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