xref: /openbmc/qemu/target/riscv/csr.c (revision c8f8a995)
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_MPV)) {
1298         tlb_flush(env_cpu(env));
1299     }
1300     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
1301         MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
1302         MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
1303         MSTATUS_TW | MSTATUS_VS;
1304 
1305     if (riscv_has_ext(env, RVF)) {
1306         mask |= MSTATUS_FS;
1307     }
1308 
1309     if (xl != MXL_RV32 || env->debugger) {
1310         /*
1311          * RV32: MPV and GVA are not in mstatus. The current plan is to
1312          * add them to mstatush. For now, we just don't support it.
1313          */
1314         mask |= MSTATUS_MPV | MSTATUS_GVA;
1315         if ((val & MSTATUS64_UXL) != 0) {
1316             mask |= MSTATUS64_UXL;
1317         }
1318     }
1319 
1320     mstatus = (mstatus & ~mask) | (val & mask);
1321 
1322     if (xl > MXL_RV32) {
1323         /* SXL field is for now read only */
1324         mstatus = set_field(mstatus, MSTATUS64_SXL, xl);
1325     }
1326     env->mstatus = mstatus;
1327     env->xl = cpu_recompute_xl(env);
1328 
1329     return RISCV_EXCP_NONE;
1330 }
1331 
1332 static RISCVException read_mstatush(CPURISCVState *env, int csrno,
1333                                     target_ulong *val)
1334 {
1335     *val = env->mstatus >> 32;
1336     return RISCV_EXCP_NONE;
1337 }
1338 
1339 static RISCVException write_mstatush(CPURISCVState *env, int csrno,
1340                                      target_ulong val)
1341 {
1342     uint64_t valh = (uint64_t)val << 32;
1343     uint64_t mask = MSTATUS_MPV | MSTATUS_GVA;
1344 
1345     if ((valh ^ env->mstatus) & (MSTATUS_MPV)) {
1346         tlb_flush(env_cpu(env));
1347     }
1348 
1349     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
1350 
1351     return RISCV_EXCP_NONE;
1352 }
1353 
1354 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno,
1355                                         Int128 *val)
1356 {
1357     *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128,
1358                                                       env->mstatus));
1359     return RISCV_EXCP_NONE;
1360 }
1361 
1362 static RISCVException read_misa_i128(CPURISCVState *env, int csrno,
1363                                      Int128 *val)
1364 {
1365     *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62);
1366     return RISCV_EXCP_NONE;
1367 }
1368 
1369 static RISCVException read_misa(CPURISCVState *env, int csrno,
1370                                 target_ulong *val)
1371 {
1372     target_ulong misa;
1373 
1374     switch (env->misa_mxl) {
1375     case MXL_RV32:
1376         misa = (target_ulong)MXL_RV32 << 30;
1377         break;
1378 #ifdef TARGET_RISCV64
1379     case MXL_RV64:
1380         misa = (target_ulong)MXL_RV64 << 62;
1381         break;
1382 #endif
1383     default:
1384         g_assert_not_reached();
1385     }
1386 
1387     *val = misa | env->misa_ext;
1388     return RISCV_EXCP_NONE;
1389 }
1390 
1391 static RISCVException write_misa(CPURISCVState *env, int csrno,
1392                                  target_ulong val)
1393 {
1394     if (!riscv_cpu_cfg(env)->misa_w) {
1395         /* drop write to misa */
1396         return RISCV_EXCP_NONE;
1397     }
1398 
1399     /* 'I' or 'E' must be present */
1400     if (!(val & (RVI | RVE))) {
1401         /* It is not, drop write to misa */
1402         return RISCV_EXCP_NONE;
1403     }
1404 
1405     /* 'E' excludes all other extensions */
1406     if (val & RVE) {
1407         /*
1408          * when we support 'E' we can do "val = RVE;" however
1409          * for now we just drop writes if 'E' is present.
1410          */
1411         return RISCV_EXCP_NONE;
1412     }
1413 
1414     /*
1415      * misa.MXL writes are not supported by QEMU.
1416      * Drop writes to those bits.
1417      */
1418 
1419     /* Mask extensions that are not supported by this hart */
1420     val &= env->misa_ext_mask;
1421 
1422     /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
1423     if ((val & RVD) && !(val & RVF)) {
1424         val &= ~RVD;
1425     }
1426 
1427     /*
1428      * Suppress 'C' if next instruction is not aligned
1429      * TODO: this should check next_pc
1430      */
1431     if ((val & RVC) && (GETPC() & ~3) != 0) {
1432         val &= ~RVC;
1433     }
1434 
1435     /* If nothing changed, do nothing. */
1436     if (val == env->misa_ext) {
1437         return RISCV_EXCP_NONE;
1438     }
1439 
1440     if (!(val & RVF)) {
1441         env->mstatus &= ~MSTATUS_FS;
1442     }
1443 
1444     /* flush translation cache */
1445     tb_flush(env_cpu(env));
1446     env->misa_ext = val;
1447     env->xl = riscv_cpu_mxl(env);
1448     return RISCV_EXCP_NONE;
1449 }
1450 
1451 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
1452                                    target_ulong *val)
1453 {
1454     *val = env->medeleg;
1455     return RISCV_EXCP_NONE;
1456 }
1457 
1458 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
1459                                     target_ulong val)
1460 {
1461     env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
1462     return RISCV_EXCP_NONE;
1463 }
1464 
1465 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno,
1466                                     uint64_t *ret_val,
1467                                     uint64_t new_val, uint64_t wr_mask)
1468 {
1469     uint64_t mask = wr_mask & delegable_ints;
1470 
1471     if (ret_val) {
1472         *ret_val = env->mideleg;
1473     }
1474 
1475     env->mideleg = (env->mideleg & ~mask) | (new_val & mask);
1476 
1477     if (riscv_has_ext(env, RVH)) {
1478         env->mideleg |= HS_MODE_INTERRUPTS;
1479     }
1480 
1481     return RISCV_EXCP_NONE;
1482 }
1483 
1484 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno,
1485                                   target_ulong *ret_val,
1486                                   target_ulong new_val, target_ulong wr_mask)
1487 {
1488     uint64_t rval;
1489     RISCVException ret;
1490 
1491     ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask);
1492     if (ret_val) {
1493         *ret_val = rval;
1494     }
1495 
1496     return ret;
1497 }
1498 
1499 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno,
1500                                    target_ulong *ret_val,
1501                                    target_ulong new_val,
1502                                    target_ulong wr_mask)
1503 {
1504     uint64_t rval;
1505     RISCVException ret;
1506 
1507     ret = rmw_mideleg64(env, csrno, &rval,
1508         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1509     if (ret_val) {
1510         *ret_val = rval >> 32;
1511     }
1512 
1513     return ret;
1514 }
1515 
1516 static RISCVException rmw_mie64(CPURISCVState *env, int csrno,
1517                                 uint64_t *ret_val,
1518                                 uint64_t new_val, uint64_t wr_mask)
1519 {
1520     uint64_t mask = wr_mask & all_ints;
1521 
1522     if (ret_val) {
1523         *ret_val = env->mie;
1524     }
1525 
1526     env->mie = (env->mie & ~mask) | (new_val & mask);
1527 
1528     if (!riscv_has_ext(env, RVH)) {
1529         env->mie &= ~((uint64_t)MIP_SGEIP);
1530     }
1531 
1532     return RISCV_EXCP_NONE;
1533 }
1534 
1535 static RISCVException rmw_mie(CPURISCVState *env, int csrno,
1536                               target_ulong *ret_val,
1537                               target_ulong new_val, target_ulong wr_mask)
1538 {
1539     uint64_t rval;
1540     RISCVException ret;
1541 
1542     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask);
1543     if (ret_val) {
1544         *ret_val = rval;
1545     }
1546 
1547     return ret;
1548 }
1549 
1550 static RISCVException rmw_mieh(CPURISCVState *env, int csrno,
1551                                target_ulong *ret_val,
1552                                target_ulong new_val, target_ulong wr_mask)
1553 {
1554     uint64_t rval;
1555     RISCVException ret;
1556 
1557     ret = rmw_mie64(env, csrno, &rval,
1558         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1559     if (ret_val) {
1560         *ret_val = rval >> 32;
1561     }
1562 
1563     return ret;
1564 }
1565 
1566 static int read_mtopi(CPURISCVState *env, int csrno, target_ulong *val)
1567 {
1568     int irq;
1569     uint8_t iprio;
1570 
1571     irq = riscv_cpu_mirq_pending(env);
1572     if (irq <= 0 || irq > 63) {
1573         *val = 0;
1574     } else {
1575         iprio = env->miprio[irq];
1576         if (!iprio) {
1577             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) {
1578                 iprio = IPRIO_MMAXIPRIO;
1579             }
1580         }
1581         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1582         *val |= iprio;
1583     }
1584 
1585     return RISCV_EXCP_NONE;
1586 }
1587 
1588 static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)
1589 {
1590     if (!env->virt_enabled) {
1591         return csrno;
1592     }
1593 
1594     switch (csrno) {
1595     case CSR_SISELECT:
1596         return CSR_VSISELECT;
1597     case CSR_SIREG:
1598         return CSR_VSIREG;
1599     case CSR_STOPEI:
1600         return CSR_VSTOPEI;
1601     default:
1602         return csrno;
1603     };
1604 }
1605 
1606 static int rmw_xiselect(CPURISCVState *env, int csrno, target_ulong *val,
1607                         target_ulong new_val, target_ulong wr_mask)
1608 {
1609     target_ulong *iselect;
1610 
1611     /* Translate CSR number for VS-mode */
1612     csrno = aia_xlate_vs_csrno(env, csrno);
1613 
1614     /* Find the iselect CSR based on CSR number */
1615     switch (csrno) {
1616     case CSR_MISELECT:
1617         iselect = &env->miselect;
1618         break;
1619     case CSR_SISELECT:
1620         iselect = &env->siselect;
1621         break;
1622     case CSR_VSISELECT:
1623         iselect = &env->vsiselect;
1624         break;
1625     default:
1626          return RISCV_EXCP_ILLEGAL_INST;
1627     };
1628 
1629     if (val) {
1630         *val = *iselect;
1631     }
1632 
1633     wr_mask &= ISELECT_MASK;
1634     if (wr_mask) {
1635         *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask);
1636     }
1637 
1638     return RISCV_EXCP_NONE;
1639 }
1640 
1641 static int rmw_iprio(target_ulong xlen,
1642                      target_ulong iselect, uint8_t *iprio,
1643                      target_ulong *val, target_ulong new_val,
1644                      target_ulong wr_mask, int ext_irq_no)
1645 {
1646     int i, firq, nirqs;
1647     target_ulong old_val;
1648 
1649     if (iselect < ISELECT_IPRIO0 || ISELECT_IPRIO15 < iselect) {
1650         return -EINVAL;
1651     }
1652     if (xlen != 32 && iselect & 0x1) {
1653         return -EINVAL;
1654     }
1655 
1656     nirqs = 4 * (xlen / 32);
1657     firq = ((iselect - ISELECT_IPRIO0) / (xlen / 32)) * (nirqs);
1658 
1659     old_val = 0;
1660     for (i = 0; i < nirqs; i++) {
1661         old_val |= ((target_ulong)iprio[firq + i]) << (IPRIO_IRQ_BITS * i);
1662     }
1663 
1664     if (val) {
1665         *val = old_val;
1666     }
1667 
1668     if (wr_mask) {
1669         new_val = (old_val & ~wr_mask) | (new_val & wr_mask);
1670         for (i = 0; i < nirqs; i++) {
1671             /*
1672              * M-level and S-level external IRQ priority always read-only
1673              * zero. This means default priority order is always preferred
1674              * for M-level and S-level external IRQs.
1675              */
1676             if ((firq + i) == ext_irq_no) {
1677                 continue;
1678             }
1679             iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) & 0xff;
1680         }
1681     }
1682 
1683     return 0;
1684 }
1685 
1686 static int rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val,
1687                      target_ulong new_val, target_ulong wr_mask)
1688 {
1689     bool virt;
1690     uint8_t *iprio;
1691     int ret = -EINVAL;
1692     target_ulong priv, isel, vgein;
1693 
1694     /* Translate CSR number for VS-mode */
1695     csrno = aia_xlate_vs_csrno(env, csrno);
1696 
1697     /* Decode register details from CSR number */
1698     virt = false;
1699     switch (csrno) {
1700     case CSR_MIREG:
1701         iprio = env->miprio;
1702         isel = env->miselect;
1703         priv = PRV_M;
1704         break;
1705     case CSR_SIREG:
1706         iprio = env->siprio;
1707         isel = env->siselect;
1708         priv = PRV_S;
1709         break;
1710     case CSR_VSIREG:
1711         iprio = env->hviprio;
1712         isel = env->vsiselect;
1713         priv = PRV_S;
1714         virt = true;
1715         break;
1716     default:
1717          goto done;
1718     };
1719 
1720     /* Find the selected guest interrupt file */
1721     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1722 
1723     if (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) {
1724         /* Local interrupt priority registers not available for VS-mode */
1725         if (!virt) {
1726             ret = rmw_iprio(riscv_cpu_mxl_bits(env),
1727                             isel, iprio, val, new_val, wr_mask,
1728                             (priv == PRV_M) ? IRQ_M_EXT : IRQ_S_EXT);
1729         }
1730     } else if (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST) {
1731         /* IMSIC registers only available when machine implements it. */
1732         if (env->aia_ireg_rmw_fn[priv]) {
1733             /* Selected guest interrupt file should not be zero */
1734             if (virt && (!vgein || env->geilen < vgein)) {
1735                 goto done;
1736             }
1737             /* Call machine specific IMSIC register emulation */
1738             ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1739                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
1740                                                   riscv_cpu_mxl_bits(env)),
1741                                     val, new_val, wr_mask);
1742         }
1743     }
1744 
1745 done:
1746     if (ret) {
1747         return (env->virt_enabled && virt) ?
1748                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1749     }
1750     return RISCV_EXCP_NONE;
1751 }
1752 
1753 static int rmw_xtopei(CPURISCVState *env, int csrno, target_ulong *val,
1754                       target_ulong new_val, target_ulong wr_mask)
1755 {
1756     bool virt;
1757     int ret = -EINVAL;
1758     target_ulong priv, vgein;
1759 
1760     /* Translate CSR number for VS-mode */
1761     csrno = aia_xlate_vs_csrno(env, csrno);
1762 
1763     /* Decode register details from CSR number */
1764     virt = false;
1765     switch (csrno) {
1766     case CSR_MTOPEI:
1767         priv = PRV_M;
1768         break;
1769     case CSR_STOPEI:
1770         priv = PRV_S;
1771         break;
1772     case CSR_VSTOPEI:
1773         priv = PRV_S;
1774         virt = true;
1775         break;
1776     default:
1777         goto done;
1778     };
1779 
1780     /* IMSIC CSRs only available when machine implements IMSIC. */
1781     if (!env->aia_ireg_rmw_fn[priv]) {
1782         goto done;
1783     }
1784 
1785     /* Find the selected guest interrupt file */
1786     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1787 
1788     /* Selected guest interrupt file should be valid */
1789     if (virt && (!vgein || env->geilen < vgein)) {
1790         goto done;
1791     }
1792 
1793     /* Call machine specific IMSIC register emulation for TOPEI */
1794     ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1795                     AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
1796                                   riscv_cpu_mxl_bits(env)),
1797                     val, new_val, wr_mask);
1798 
1799 done:
1800     if (ret) {
1801         return (env->virt_enabled && virt) ?
1802                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1803     }
1804     return RISCV_EXCP_NONE;
1805 }
1806 
1807 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
1808                                  target_ulong *val)
1809 {
1810     *val = env->mtvec;
1811     return RISCV_EXCP_NONE;
1812 }
1813 
1814 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
1815                                   target_ulong val)
1816 {
1817     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
1818     if ((val & 3) < 2) {
1819         env->mtvec = val;
1820     } else {
1821         qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
1822     }
1823     return RISCV_EXCP_NONE;
1824 }
1825 
1826 static RISCVException read_mcountinhibit(CPURISCVState *env, int csrno,
1827                                          target_ulong *val)
1828 {
1829     *val = env->mcountinhibit;
1830     return RISCV_EXCP_NONE;
1831 }
1832 
1833 static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno,
1834                                           target_ulong val)
1835 {
1836     int cidx;
1837     PMUCTRState *counter;
1838 
1839     env->mcountinhibit = val;
1840 
1841     /* Check if any other counter is also monitoring cycles/instructions */
1842     for (cidx = 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) {
1843         if (!get_field(env->mcountinhibit, BIT(cidx))) {
1844             counter = &env->pmu_ctrs[cidx];
1845             counter->started = true;
1846         }
1847     }
1848 
1849     return RISCV_EXCP_NONE;
1850 }
1851 
1852 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
1853                                       target_ulong *val)
1854 {
1855     *val = env->mcounteren;
1856     return RISCV_EXCP_NONE;
1857 }
1858 
1859 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
1860                                        target_ulong val)
1861 {
1862     env->mcounteren = val;
1863     return RISCV_EXCP_NONE;
1864 }
1865 
1866 /* Machine Trap Handling */
1867 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno,
1868                                          Int128 *val)
1869 {
1870     *val = int128_make128(env->mscratch, env->mscratchh);
1871     return RISCV_EXCP_NONE;
1872 }
1873 
1874 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno,
1875                                           Int128 val)
1876 {
1877     env->mscratch = int128_getlo(val);
1878     env->mscratchh = int128_gethi(val);
1879     return RISCV_EXCP_NONE;
1880 }
1881 
1882 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
1883                                     target_ulong *val)
1884 {
1885     *val = env->mscratch;
1886     return RISCV_EXCP_NONE;
1887 }
1888 
1889 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
1890                                      target_ulong val)
1891 {
1892     env->mscratch = val;
1893     return RISCV_EXCP_NONE;
1894 }
1895 
1896 static RISCVException read_mepc(CPURISCVState *env, int csrno,
1897                                 target_ulong *val)
1898 {
1899     *val = env->mepc;
1900     return RISCV_EXCP_NONE;
1901 }
1902 
1903 static RISCVException write_mepc(CPURISCVState *env, int csrno,
1904                                  target_ulong val)
1905 {
1906     env->mepc = val;
1907     return RISCV_EXCP_NONE;
1908 }
1909 
1910 static RISCVException read_mcause(CPURISCVState *env, int csrno,
1911                                   target_ulong *val)
1912 {
1913     *val = env->mcause;
1914     return RISCV_EXCP_NONE;
1915 }
1916 
1917 static RISCVException write_mcause(CPURISCVState *env, int csrno,
1918                                    target_ulong val)
1919 {
1920     env->mcause = val;
1921     return RISCV_EXCP_NONE;
1922 }
1923 
1924 static RISCVException read_mtval(CPURISCVState *env, int csrno,
1925                                  target_ulong *val)
1926 {
1927     *val = env->mtval;
1928     return RISCV_EXCP_NONE;
1929 }
1930 
1931 static RISCVException write_mtval(CPURISCVState *env, int csrno,
1932                                   target_ulong val)
1933 {
1934     env->mtval = val;
1935     return RISCV_EXCP_NONE;
1936 }
1937 
1938 /* Execution environment configuration setup */
1939 static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
1940                                    target_ulong *val)
1941 {
1942     *val = env->menvcfg;
1943     return RISCV_EXCP_NONE;
1944 }
1945 
1946 static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
1947                                     target_ulong val)
1948 {
1949     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
1950     uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE;
1951 
1952     if (riscv_cpu_mxl(env) == MXL_RV64) {
1953         mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
1954                 (cfg->ext_sstc ? MENVCFG_STCE : 0) |
1955                 (cfg->ext_svadu ? MENVCFG_HADE : 0);
1956     }
1957     env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
1958 
1959     return RISCV_EXCP_NONE;
1960 }
1961 
1962 static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
1963                                     target_ulong *val)
1964 {
1965     *val = env->menvcfg >> 32;
1966     return RISCV_EXCP_NONE;
1967 }
1968 
1969 static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
1970                                      target_ulong val)
1971 {
1972     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
1973     uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
1974                     (cfg->ext_sstc ? MENVCFG_STCE : 0) |
1975                     (cfg->ext_svadu ? MENVCFG_HADE : 0);
1976     uint64_t valh = (uint64_t)val << 32;
1977 
1978     env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);
1979 
1980     return RISCV_EXCP_NONE;
1981 }
1982 
1983 static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
1984                                    target_ulong *val)
1985 {
1986     RISCVException ret;
1987 
1988     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
1989     if (ret != RISCV_EXCP_NONE) {
1990         return ret;
1991     }
1992 
1993     *val = env->senvcfg;
1994     return RISCV_EXCP_NONE;
1995 }
1996 
1997 static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
1998                                     target_ulong val)
1999 {
2000     uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE;
2001     RISCVException ret;
2002 
2003     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2004     if (ret != RISCV_EXCP_NONE) {
2005         return ret;
2006     }
2007 
2008     env->senvcfg = (env->senvcfg & ~mask) | (val & mask);
2009     return RISCV_EXCP_NONE;
2010 }
2011 
2012 static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
2013                                    target_ulong *val)
2014 {
2015     RISCVException ret;
2016 
2017     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2018     if (ret != RISCV_EXCP_NONE) {
2019         return ret;
2020     }
2021 
2022     /*
2023      * henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0
2024      * henvcfg.stce is read_only 0 when menvcfg.stce = 0
2025      * henvcfg.hade is read_only 0 when menvcfg.hade = 0
2026      */
2027     *val = env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_HADE) |
2028                            env->menvcfg);
2029     return RISCV_EXCP_NONE;
2030 }
2031 
2032 static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
2033                                     target_ulong val)
2034 {
2035     uint64_t mask = HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCFG_CBZE;
2036     RISCVException ret;
2037 
2038     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2039     if (ret != RISCV_EXCP_NONE) {
2040         return ret;
2041     }
2042 
2043     if (riscv_cpu_mxl(env) == MXL_RV64) {
2044         mask |= env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_HADE);
2045     }
2046 
2047     env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
2048 
2049     return RISCV_EXCP_NONE;
2050 }
2051 
2052 static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
2053                                     target_ulong *val)
2054 {
2055     RISCVException ret;
2056 
2057     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2058     if (ret != RISCV_EXCP_NONE) {
2059         return ret;
2060     }
2061 
2062     *val = (env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_HADE) |
2063                             env->menvcfg)) >> 32;
2064     return RISCV_EXCP_NONE;
2065 }
2066 
2067 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
2068                                      target_ulong val)
2069 {
2070     uint64_t mask = env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE |
2071                                     HENVCFG_HADE);
2072     uint64_t valh = (uint64_t)val << 32;
2073     RISCVException ret;
2074 
2075     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2076     if (ret != RISCV_EXCP_NONE) {
2077         return ret;
2078     }
2079 
2080     env->henvcfg = (env->henvcfg & ~mask) | (valh & mask);
2081     return RISCV_EXCP_NONE;
2082 }
2083 
2084 static RISCVException read_mstateen(CPURISCVState *env, int csrno,
2085                                     target_ulong *val)
2086 {
2087     *val = env->mstateen[csrno - CSR_MSTATEEN0];
2088 
2089     return RISCV_EXCP_NONE;
2090 }
2091 
2092 static RISCVException write_mstateen(CPURISCVState *env, int csrno,
2093                                      uint64_t wr_mask, target_ulong new_val)
2094 {
2095     uint64_t *reg;
2096 
2097     reg = &env->mstateen[csrno - CSR_MSTATEEN0];
2098     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2099 
2100     return RISCV_EXCP_NONE;
2101 }
2102 
2103 static RISCVException write_mstateen0(CPURISCVState *env, int csrno,
2104                                       target_ulong new_val)
2105 {
2106     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2107 
2108     return write_mstateen(env, csrno, wr_mask, new_val);
2109 }
2110 
2111 static RISCVException write_mstateen_1_3(CPURISCVState *env, int csrno,
2112                                          target_ulong new_val)
2113 {
2114     return write_mstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2115 }
2116 
2117 static RISCVException read_mstateenh(CPURISCVState *env, int csrno,
2118                                      target_ulong *val)
2119 {
2120     *val = env->mstateen[csrno - CSR_MSTATEEN0H] >> 32;
2121 
2122     return RISCV_EXCP_NONE;
2123 }
2124 
2125 static RISCVException write_mstateenh(CPURISCVState *env, int csrno,
2126                                       uint64_t wr_mask, target_ulong new_val)
2127 {
2128     uint64_t *reg, val;
2129 
2130     reg = &env->mstateen[csrno - CSR_MSTATEEN0H];
2131     val = (uint64_t)new_val << 32;
2132     val |= *reg & 0xFFFFFFFF;
2133     *reg = (*reg & ~wr_mask) | (val & wr_mask);
2134 
2135     return RISCV_EXCP_NONE;
2136 }
2137 
2138 static RISCVException write_mstateen0h(CPURISCVState *env, int csrno,
2139                                        target_ulong new_val)
2140 {
2141     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2142 
2143     return write_mstateenh(env, csrno, wr_mask, new_val);
2144 }
2145 
2146 static RISCVException write_mstateenh_1_3(CPURISCVState *env, int csrno,
2147                                           target_ulong new_val)
2148 {
2149     return write_mstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2150 }
2151 
2152 static RISCVException read_hstateen(CPURISCVState *env, int csrno,
2153                                     target_ulong *val)
2154 {
2155     int index = csrno - CSR_HSTATEEN0;
2156 
2157     *val = env->hstateen[index] & env->mstateen[index];
2158 
2159     return RISCV_EXCP_NONE;
2160 }
2161 
2162 static RISCVException write_hstateen(CPURISCVState *env, int csrno,
2163                                      uint64_t mask, target_ulong new_val)
2164 {
2165     int index = csrno - CSR_HSTATEEN0;
2166     uint64_t *reg, wr_mask;
2167 
2168     reg = &env->hstateen[index];
2169     wr_mask = env->mstateen[index] & mask;
2170     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2171 
2172     return RISCV_EXCP_NONE;
2173 }
2174 
2175 static RISCVException write_hstateen0(CPURISCVState *env, int csrno,
2176                                       target_ulong new_val)
2177 {
2178     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2179 
2180     return write_hstateen(env, csrno, wr_mask, new_val);
2181 }
2182 
2183 static RISCVException write_hstateen_1_3(CPURISCVState *env, int csrno,
2184                                          target_ulong new_val)
2185 {
2186     return write_hstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2187 }
2188 
2189 static RISCVException read_hstateenh(CPURISCVState *env, int csrno,
2190                                      target_ulong *val)
2191 {
2192     int index = csrno - CSR_HSTATEEN0H;
2193 
2194     *val = (env->hstateen[index] >> 32) & (env->mstateen[index] >> 32);
2195 
2196     return RISCV_EXCP_NONE;
2197 }
2198 
2199 static RISCVException write_hstateenh(CPURISCVState *env, int csrno,
2200                                       uint64_t mask, target_ulong new_val)
2201 {
2202     int index = csrno - CSR_HSTATEEN0H;
2203     uint64_t *reg, wr_mask, val;
2204 
2205     reg = &env->hstateen[index];
2206     val = (uint64_t)new_val << 32;
2207     val |= *reg & 0xFFFFFFFF;
2208     wr_mask = env->mstateen[index] & mask;
2209     *reg = (*reg & ~wr_mask) | (val & wr_mask);
2210 
2211     return RISCV_EXCP_NONE;
2212 }
2213 
2214 static RISCVException write_hstateen0h(CPURISCVState *env, int csrno,
2215                                        target_ulong new_val)
2216 {
2217     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2218 
2219     return write_hstateenh(env, csrno, wr_mask, new_val);
2220 }
2221 
2222 static RISCVException write_hstateenh_1_3(CPURISCVState *env, int csrno,
2223                                           target_ulong new_val)
2224 {
2225     return write_hstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2226 }
2227 
2228 static RISCVException read_sstateen(CPURISCVState *env, int csrno,
2229                                     target_ulong *val)
2230 {
2231     bool virt = env->virt_enabled;
2232     int index = csrno - CSR_SSTATEEN0;
2233 
2234     *val = env->sstateen[index] & env->mstateen[index];
2235     if (virt) {
2236         *val &= env->hstateen[index];
2237     }
2238 
2239     return RISCV_EXCP_NONE;
2240 }
2241 
2242 static RISCVException write_sstateen(CPURISCVState *env, int csrno,
2243                                      uint64_t mask, target_ulong new_val)
2244 {
2245     bool virt = env->virt_enabled;
2246     int index = csrno - CSR_SSTATEEN0;
2247     uint64_t wr_mask;
2248     uint64_t *reg;
2249 
2250     wr_mask = env->mstateen[index] & mask;
2251     if (virt) {
2252         wr_mask &= env->hstateen[index];
2253     }
2254 
2255     reg = &env->sstateen[index];
2256     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2257 
2258     return RISCV_EXCP_NONE;
2259 }
2260 
2261 static RISCVException write_sstateen0(CPURISCVState *env, int csrno,
2262                                       target_ulong new_val)
2263 {
2264     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2265 
2266     return write_sstateen(env, csrno, wr_mask, new_val);
2267 }
2268 
2269 static RISCVException write_sstateen_1_3(CPURISCVState *env, int csrno,
2270                                       target_ulong new_val)
2271 {
2272     return write_sstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2273 }
2274 
2275 static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
2276                                 uint64_t *ret_val,
2277                                 uint64_t new_val, uint64_t wr_mask)
2278 {
2279     uint64_t old_mip, mask = wr_mask & delegable_ints;
2280     uint32_t gin;
2281 
2282     if (mask & MIP_SEIP) {
2283         env->software_seip = new_val & MIP_SEIP;
2284         new_val |= env->external_seip * MIP_SEIP;
2285     }
2286 
2287     if (riscv_cpu_cfg(env)->ext_sstc && (env->priv == PRV_M) &&
2288         get_field(env->menvcfg, MENVCFG_STCE)) {
2289         /* sstc extension forbids STIP & VSTIP to be writeable in mip */
2290         mask = mask & ~(MIP_STIP | MIP_VSTIP);
2291     }
2292 
2293     if (mask) {
2294         old_mip = riscv_cpu_update_mip(env, mask, (new_val & mask));
2295     } else {
2296         old_mip = env->mip;
2297     }
2298 
2299     if (csrno != CSR_HVIP) {
2300         gin = get_field(env->hstatus, HSTATUS_VGEIN);
2301         old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0;
2302         old_mip |= env->vstime_irq ? MIP_VSTIP : 0;
2303     }
2304 
2305     if (ret_val) {
2306         *ret_val = old_mip;
2307     }
2308 
2309     return RISCV_EXCP_NONE;
2310 }
2311 
2312 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
2313                               target_ulong *ret_val,
2314                               target_ulong new_val, target_ulong wr_mask)
2315 {
2316     uint64_t rval;
2317     RISCVException ret;
2318 
2319     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask);
2320     if (ret_val) {
2321         *ret_val = rval;
2322     }
2323 
2324     return ret;
2325 }
2326 
2327 static RISCVException rmw_miph(CPURISCVState *env, int csrno,
2328                                target_ulong *ret_val,
2329                                target_ulong new_val, target_ulong wr_mask)
2330 {
2331     uint64_t rval;
2332     RISCVException ret;
2333 
2334     ret = rmw_mip64(env, csrno, &rval,
2335         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2336     if (ret_val) {
2337         *ret_val = rval >> 32;
2338     }
2339 
2340     return ret;
2341 }
2342 
2343 /* Supervisor Trap Setup */
2344 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,
2345                                         Int128 *val)
2346 {
2347     uint64_t mask = sstatus_v1_10_mask;
2348     uint64_t sstatus = env->mstatus & mask;
2349     if (env->xl != MXL_RV32 || env->debugger) {
2350         mask |= SSTATUS64_UXL;
2351     }
2352 
2353     *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus));
2354     return RISCV_EXCP_NONE;
2355 }
2356 
2357 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
2358                                    target_ulong *val)
2359 {
2360     target_ulong mask = (sstatus_v1_10_mask);
2361     if (env->xl != MXL_RV32 || env->debugger) {
2362         mask |= SSTATUS64_UXL;
2363     }
2364     /* TODO: Use SXL not MXL. */
2365     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
2366     return RISCV_EXCP_NONE;
2367 }
2368 
2369 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
2370                                     target_ulong val)
2371 {
2372     target_ulong mask = (sstatus_v1_10_mask);
2373 
2374     if (env->xl != MXL_RV32 || env->debugger) {
2375         if ((val & SSTATUS64_UXL) != 0) {
2376             mask |= SSTATUS64_UXL;
2377         }
2378     }
2379     target_ulong newval = (env->mstatus & ~mask) | (val & mask);
2380     return write_mstatus(env, CSR_MSTATUS, newval);
2381 }
2382 
2383 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno,
2384                                  uint64_t *ret_val,
2385                                  uint64_t new_val, uint64_t wr_mask)
2386 {
2387     RISCVException ret;
2388     uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS;
2389 
2390     /* Bring VS-level bits to correct position */
2391     new_val = (new_val & (VS_MODE_INTERRUPTS >> 1)) << 1;
2392     wr_mask = (wr_mask & (VS_MODE_INTERRUPTS >> 1)) << 1;
2393 
2394     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & mask);
2395     if (ret_val) {
2396         *ret_val = (rval & mask) >> 1;
2397     }
2398 
2399     return ret;
2400 }
2401 
2402 static RISCVException rmw_vsie(CPURISCVState *env, int csrno,
2403                                target_ulong *ret_val,
2404                                target_ulong new_val, target_ulong wr_mask)
2405 {
2406     uint64_t rval;
2407     RISCVException ret;
2408 
2409     ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask);
2410     if (ret_val) {
2411         *ret_val = rval;
2412     }
2413 
2414     return ret;
2415 }
2416 
2417 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno,
2418                                 target_ulong *ret_val,
2419                                 target_ulong new_val, target_ulong wr_mask)
2420 {
2421     uint64_t rval;
2422     RISCVException ret;
2423 
2424     ret = rmw_vsie64(env, csrno, &rval,
2425         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2426     if (ret_val) {
2427         *ret_val = rval >> 32;
2428     }
2429 
2430     return ret;
2431 }
2432 
2433 static RISCVException rmw_sie64(CPURISCVState *env, int csrno,
2434                                 uint64_t *ret_val,
2435                                 uint64_t new_val, uint64_t wr_mask)
2436 {
2437     RISCVException ret;
2438     uint64_t mask = env->mideleg & S_MODE_INTERRUPTS;
2439 
2440     if (env->virt_enabled) {
2441         if (env->hvictl & HVICTL_VTI) {
2442             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2443         }
2444         ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask);
2445     } else {
2446         ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & mask);
2447     }
2448 
2449     if (ret_val) {
2450         *ret_val &= mask;
2451     }
2452 
2453     return ret;
2454 }
2455 
2456 static RISCVException rmw_sie(CPURISCVState *env, int csrno,
2457                               target_ulong *ret_val,
2458                               target_ulong new_val, target_ulong wr_mask)
2459 {
2460     uint64_t rval;
2461     RISCVException ret;
2462 
2463     ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask);
2464     if (ret == RISCV_EXCP_NONE && ret_val) {
2465         *ret_val = rval;
2466     }
2467 
2468     return ret;
2469 }
2470 
2471 static RISCVException rmw_sieh(CPURISCVState *env, int csrno,
2472                                target_ulong *ret_val,
2473                                target_ulong new_val, target_ulong wr_mask)
2474 {
2475     uint64_t rval;
2476     RISCVException ret;
2477 
2478     ret = rmw_sie64(env, csrno, &rval,
2479         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2480     if (ret_val) {
2481         *ret_val = rval >> 32;
2482     }
2483 
2484     return ret;
2485 }
2486 
2487 static RISCVException read_stvec(CPURISCVState *env, int csrno,
2488                                  target_ulong *val)
2489 {
2490     *val = env->stvec;
2491     return RISCV_EXCP_NONE;
2492 }
2493 
2494 static RISCVException write_stvec(CPURISCVState *env, int csrno,
2495                                   target_ulong val)
2496 {
2497     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
2498     if ((val & 3) < 2) {
2499         env->stvec = val;
2500     } else {
2501         qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
2502     }
2503     return RISCV_EXCP_NONE;
2504 }
2505 
2506 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
2507                                       target_ulong *val)
2508 {
2509     *val = env->scounteren;
2510     return RISCV_EXCP_NONE;
2511 }
2512 
2513 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
2514                                        target_ulong val)
2515 {
2516     env->scounteren = val;
2517     return RISCV_EXCP_NONE;
2518 }
2519 
2520 /* Supervisor Trap Handling */
2521 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno,
2522                                          Int128 *val)
2523 {
2524     *val = int128_make128(env->sscratch, env->sscratchh);
2525     return RISCV_EXCP_NONE;
2526 }
2527 
2528 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno,
2529                                           Int128 val)
2530 {
2531     env->sscratch = int128_getlo(val);
2532     env->sscratchh = int128_gethi(val);
2533     return RISCV_EXCP_NONE;
2534 }
2535 
2536 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
2537                                     target_ulong *val)
2538 {
2539     *val = env->sscratch;
2540     return RISCV_EXCP_NONE;
2541 }
2542 
2543 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
2544                                      target_ulong val)
2545 {
2546     env->sscratch = val;
2547     return RISCV_EXCP_NONE;
2548 }
2549 
2550 static RISCVException read_sepc(CPURISCVState *env, int csrno,
2551                                 target_ulong *val)
2552 {
2553     *val = env->sepc;
2554     return RISCV_EXCP_NONE;
2555 }
2556 
2557 static RISCVException write_sepc(CPURISCVState *env, int csrno,
2558                                  target_ulong val)
2559 {
2560     env->sepc = val;
2561     return RISCV_EXCP_NONE;
2562 }
2563 
2564 static RISCVException read_scause(CPURISCVState *env, int csrno,
2565                                   target_ulong *val)
2566 {
2567     *val = env->scause;
2568     return RISCV_EXCP_NONE;
2569 }
2570 
2571 static RISCVException write_scause(CPURISCVState *env, int csrno,
2572                                    target_ulong val)
2573 {
2574     env->scause = val;
2575     return RISCV_EXCP_NONE;
2576 }
2577 
2578 static RISCVException read_stval(CPURISCVState *env, int csrno,
2579                                  target_ulong *val)
2580 {
2581     *val = env->stval;
2582     return RISCV_EXCP_NONE;
2583 }
2584 
2585 static RISCVException write_stval(CPURISCVState *env, int csrno,
2586                                   target_ulong val)
2587 {
2588     env->stval = val;
2589     return RISCV_EXCP_NONE;
2590 }
2591 
2592 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno,
2593                                  uint64_t *ret_val,
2594                                  uint64_t new_val, uint64_t wr_mask)
2595 {
2596     RISCVException ret;
2597     uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS;
2598 
2599     /* Bring VS-level bits to correct position */
2600     new_val = (new_val & (VS_MODE_INTERRUPTS >> 1)) << 1;
2601     wr_mask = (wr_mask & (VS_MODE_INTERRUPTS >> 1)) << 1;
2602 
2603     ret = rmw_mip64(env, csrno, &rval, new_val,
2604                     wr_mask & mask & vsip_writable_mask);
2605     if (ret_val) {
2606         *ret_val = (rval & mask) >> 1;
2607     }
2608 
2609     return ret;
2610 }
2611 
2612 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
2613                                target_ulong *ret_val,
2614                                target_ulong new_val, target_ulong wr_mask)
2615 {
2616     uint64_t rval;
2617     RISCVException ret;
2618 
2619     ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask);
2620     if (ret_val) {
2621         *ret_val = rval;
2622     }
2623 
2624     return ret;
2625 }
2626 
2627 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno,
2628                                 target_ulong *ret_val,
2629                                 target_ulong new_val, target_ulong wr_mask)
2630 {
2631     uint64_t rval;
2632     RISCVException ret;
2633 
2634     ret = rmw_vsip64(env, csrno, &rval,
2635         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2636     if (ret_val) {
2637         *ret_val = rval >> 32;
2638     }
2639 
2640     return ret;
2641 }
2642 
2643 static RISCVException rmw_sip64(CPURISCVState *env, int csrno,
2644                                 uint64_t *ret_val,
2645                                 uint64_t new_val, uint64_t wr_mask)
2646 {
2647     RISCVException ret;
2648     uint64_t mask = env->mideleg & sip_writable_mask;
2649 
2650     if (env->virt_enabled) {
2651         if (env->hvictl & HVICTL_VTI) {
2652             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2653         }
2654         ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask);
2655     } else {
2656         ret = rmw_mip64(env, csrno, ret_val, new_val, wr_mask & mask);
2657     }
2658 
2659     if (ret_val) {
2660         *ret_val &= env->mideleg & S_MODE_INTERRUPTS;
2661     }
2662 
2663     return ret;
2664 }
2665 
2666 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
2667                               target_ulong *ret_val,
2668                               target_ulong new_val, target_ulong wr_mask)
2669 {
2670     uint64_t rval;
2671     RISCVException ret;
2672 
2673     ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask);
2674     if (ret_val) {
2675         *ret_val = rval;
2676     }
2677 
2678     return ret;
2679 }
2680 
2681 static RISCVException rmw_siph(CPURISCVState *env, int csrno,
2682                                target_ulong *ret_val,
2683                                target_ulong new_val, target_ulong wr_mask)
2684 {
2685     uint64_t rval;
2686     RISCVException ret;
2687 
2688     ret = rmw_sip64(env, csrno, &rval,
2689         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2690     if (ret_val) {
2691         *ret_val = rval >> 32;
2692     }
2693 
2694     return ret;
2695 }
2696 
2697 /* Supervisor Protection and Translation */
2698 static RISCVException read_satp(CPURISCVState *env, int csrno,
2699                                 target_ulong *val)
2700 {
2701     if (!riscv_cpu_cfg(env)->mmu) {
2702         *val = 0;
2703         return RISCV_EXCP_NONE;
2704     }
2705     *val = env->satp;
2706     return RISCV_EXCP_NONE;
2707 }
2708 
2709 static RISCVException write_satp(CPURISCVState *env, int csrno,
2710                                  target_ulong val)
2711 {
2712     target_ulong mask;
2713     bool vm;
2714 
2715     if (!riscv_cpu_cfg(env)->mmu) {
2716         return RISCV_EXCP_NONE;
2717     }
2718 
2719     if (riscv_cpu_mxl(env) == MXL_RV32) {
2720         vm = validate_vm(env, get_field(val, SATP32_MODE));
2721         mask = (val ^ env->satp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
2722     } else {
2723         vm = validate_vm(env, get_field(val, SATP64_MODE));
2724         mask = (val ^ env->satp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
2725     }
2726 
2727     if (vm && mask) {
2728         /*
2729          * The ISA defines SATP.MODE=Bare as "no translation", but we still
2730          * pass these through QEMU's TLB emulation as it improves
2731          * performance.  Flushing the TLB on SATP writes with paging
2732          * enabled avoids leaking those invalid cached mappings.
2733          */
2734         tlb_flush(env_cpu(env));
2735         env->satp = val;
2736     }
2737     return RISCV_EXCP_NONE;
2738 }
2739 
2740 static int read_vstopi(CPURISCVState *env, int csrno, target_ulong *val)
2741 {
2742     int irq, ret;
2743     target_ulong topei;
2744     uint64_t vseip, vsgein;
2745     uint32_t iid, iprio, hviid, hviprio, gein;
2746     uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS];
2747 
2748     gein = get_field(env->hstatus, HSTATUS_VGEIN);
2749     hviid = get_field(env->hvictl, HVICTL_IID);
2750     hviprio = get_field(env->hvictl, HVICTL_IPRIO);
2751 
2752     if (gein) {
2753         vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0;
2754         vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP;
2755         if (gein <= env->geilen && vseip) {
2756             siid[scount] = IRQ_S_EXT;
2757             siprio[scount] = IPRIO_MMAXIPRIO + 1;
2758             if (env->aia_ireg_rmw_fn[PRV_S]) {
2759                 /*
2760                  * Call machine specific IMSIC register emulation for
2761                  * reading TOPEI.
2762                  */
2763                 ret = env->aia_ireg_rmw_fn[PRV_S](
2764                         env->aia_ireg_rmw_fn_arg[PRV_S],
2765                         AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein,
2766                                       riscv_cpu_mxl_bits(env)),
2767                         &topei, 0, 0);
2768                 if (!ret && topei) {
2769                     siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK;
2770                 }
2771             }
2772             scount++;
2773         }
2774     } else {
2775         if (hviid == IRQ_S_EXT && hviprio) {
2776             siid[scount] = IRQ_S_EXT;
2777             siprio[scount] = hviprio;
2778             scount++;
2779         }
2780     }
2781 
2782     if (env->hvictl & HVICTL_VTI) {
2783         if (hviid != IRQ_S_EXT) {
2784             siid[scount] = hviid;
2785             siprio[scount] = hviprio;
2786             scount++;
2787         }
2788     } else {
2789         irq = riscv_cpu_vsirq_pending(env);
2790         if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) {
2791             siid[scount] = irq;
2792             siprio[scount] = env->hviprio[irq];
2793             scount++;
2794         }
2795     }
2796 
2797     iid = 0;
2798     iprio = UINT_MAX;
2799     for (s = 0; s < scount; s++) {
2800         if (siprio[s] < iprio) {
2801             iid = siid[s];
2802             iprio = siprio[s];
2803         }
2804     }
2805 
2806     if (iid) {
2807         if (env->hvictl & HVICTL_IPRIOM) {
2808             if (iprio > IPRIO_MMAXIPRIO) {
2809                 iprio = IPRIO_MMAXIPRIO;
2810             }
2811             if (!iprio) {
2812                 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) {
2813                     iprio = IPRIO_MMAXIPRIO;
2814                 }
2815             }
2816         } else {
2817             iprio = 1;
2818         }
2819     } else {
2820         iprio = 0;
2821     }
2822 
2823     *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT;
2824     *val |= iprio;
2825     return RISCV_EXCP_NONE;
2826 }
2827 
2828 static int read_stopi(CPURISCVState *env, int csrno, target_ulong *val)
2829 {
2830     int irq;
2831     uint8_t iprio;
2832 
2833     if (env->virt_enabled) {
2834         return read_vstopi(env, CSR_VSTOPI, val);
2835     }
2836 
2837     irq = riscv_cpu_sirq_pending(env);
2838     if (irq <= 0 || irq > 63) {
2839         *val = 0;
2840     } else {
2841         iprio = env->siprio[irq];
2842         if (!iprio) {
2843             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) {
2844                 iprio = IPRIO_MMAXIPRIO;
2845            }
2846         }
2847         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
2848         *val |= iprio;
2849     }
2850 
2851     return RISCV_EXCP_NONE;
2852 }
2853 
2854 /* Hypervisor Extensions */
2855 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
2856                                    target_ulong *val)
2857 {
2858     *val = env->hstatus;
2859     if (riscv_cpu_mxl(env) != MXL_RV32) {
2860         /* We only support 64-bit VSXL */
2861         *val = set_field(*val, HSTATUS_VSXL, 2);
2862     }
2863     /* We only support little endian */
2864     *val = set_field(*val, HSTATUS_VSBE, 0);
2865     return RISCV_EXCP_NONE;
2866 }
2867 
2868 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
2869                                     target_ulong val)
2870 {
2871     env->hstatus = val;
2872     if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
2873         qemu_log_mask(LOG_UNIMP,
2874                       "QEMU does not support mixed HSXLEN options.");
2875     }
2876     if (get_field(val, HSTATUS_VSBE) != 0) {
2877         qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
2878     }
2879     return RISCV_EXCP_NONE;
2880 }
2881 
2882 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
2883                                    target_ulong *val)
2884 {
2885     *val = env->hedeleg;
2886     return RISCV_EXCP_NONE;
2887 }
2888 
2889 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
2890                                     target_ulong val)
2891 {
2892     env->hedeleg = val & vs_delegable_excps;
2893     return RISCV_EXCP_NONE;
2894 }
2895 
2896 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno,
2897                                     uint64_t *ret_val,
2898                                     uint64_t new_val, uint64_t wr_mask)
2899 {
2900     uint64_t mask = wr_mask & vs_delegable_ints;
2901 
2902     if (ret_val) {
2903         *ret_val = env->hideleg & vs_delegable_ints;
2904     }
2905 
2906     env->hideleg = (env->hideleg & ~mask) | (new_val & mask);
2907     return RISCV_EXCP_NONE;
2908 }
2909 
2910 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno,
2911                                   target_ulong *ret_val,
2912                                   target_ulong new_val, target_ulong wr_mask)
2913 {
2914     uint64_t rval;
2915     RISCVException ret;
2916 
2917     ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask);
2918     if (ret_val) {
2919         *ret_val = rval;
2920     }
2921 
2922     return ret;
2923 }
2924 
2925 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno,
2926                                    target_ulong *ret_val,
2927                                    target_ulong new_val, target_ulong wr_mask)
2928 {
2929     uint64_t rval;
2930     RISCVException ret;
2931 
2932     ret = rmw_hideleg64(env, csrno, &rval,
2933         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2934     if (ret_val) {
2935         *ret_val = rval >> 32;
2936     }
2937 
2938     return ret;
2939 }
2940 
2941 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
2942                                  uint64_t *ret_val,
2943                                  uint64_t new_val, uint64_t wr_mask)
2944 {
2945     RISCVException ret;
2946 
2947     ret = rmw_mip64(env, csrno, ret_val, new_val,
2948                     wr_mask & hvip_writable_mask);
2949     if (ret_val) {
2950         *ret_val &= VS_MODE_INTERRUPTS;
2951     }
2952 
2953     return ret;
2954 }
2955 
2956 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
2957                                target_ulong *ret_val,
2958                                target_ulong new_val, target_ulong wr_mask)
2959 {
2960     uint64_t rval;
2961     RISCVException ret;
2962 
2963     ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask);
2964     if (ret_val) {
2965         *ret_val = rval;
2966     }
2967 
2968     return ret;
2969 }
2970 
2971 static RISCVException rmw_hviph(CPURISCVState *env, int csrno,
2972                                 target_ulong *ret_val,
2973                                 target_ulong new_val, target_ulong wr_mask)
2974 {
2975     uint64_t rval;
2976     RISCVException ret;
2977 
2978     ret = rmw_hvip64(env, csrno, &rval,
2979         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2980     if (ret_val) {
2981         *ret_val = rval >> 32;
2982     }
2983 
2984     return ret;
2985 }
2986 
2987 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
2988                               target_ulong *ret_value,
2989                               target_ulong new_value, target_ulong write_mask)
2990 {
2991     int ret = rmw_mip(env, csrno, ret_value, new_value,
2992                       write_mask & hip_writable_mask);
2993 
2994     if (ret_value) {
2995         *ret_value &= HS_MODE_INTERRUPTS;
2996     }
2997     return ret;
2998 }
2999 
3000 static RISCVException rmw_hie(CPURISCVState *env, int csrno,
3001                               target_ulong *ret_val,
3002                               target_ulong new_val, target_ulong wr_mask)
3003 {
3004     uint64_t rval;
3005     RISCVException ret;
3006 
3007     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS);
3008     if (ret_val) {
3009         *ret_val = rval & HS_MODE_INTERRUPTS;
3010     }
3011 
3012     return ret;
3013 }
3014 
3015 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
3016                                       target_ulong *val)
3017 {
3018     *val = env->hcounteren;
3019     return RISCV_EXCP_NONE;
3020 }
3021 
3022 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
3023                                        target_ulong val)
3024 {
3025     env->hcounteren = val;
3026     return RISCV_EXCP_NONE;
3027 }
3028 
3029 static RISCVException read_hgeie(CPURISCVState *env, int csrno,
3030                                  target_ulong *val)
3031 {
3032     if (val) {
3033         *val = env->hgeie;
3034     }
3035     return RISCV_EXCP_NONE;
3036 }
3037 
3038 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
3039                                   target_ulong val)
3040 {
3041     /* Only GEILEN:1 bits implemented and BIT0 is never implemented */
3042     val &= ((((target_ulong)1) << env->geilen) - 1) << 1;
3043     env->hgeie = val;
3044     /* Update mip.SGEIP bit */
3045     riscv_cpu_update_mip(env, MIP_SGEIP,
3046                          BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
3047     return RISCV_EXCP_NONE;
3048 }
3049 
3050 static RISCVException read_htval(CPURISCVState *env, int csrno,
3051                                  target_ulong *val)
3052 {
3053     *val = env->htval;
3054     return RISCV_EXCP_NONE;
3055 }
3056 
3057 static RISCVException write_htval(CPURISCVState *env, int csrno,
3058                                   target_ulong val)
3059 {
3060     env->htval = val;
3061     return RISCV_EXCP_NONE;
3062 }
3063 
3064 static RISCVException read_htinst(CPURISCVState *env, int csrno,
3065                                   target_ulong *val)
3066 {
3067     *val = env->htinst;
3068     return RISCV_EXCP_NONE;
3069 }
3070 
3071 static RISCVException write_htinst(CPURISCVState *env, int csrno,
3072                                    target_ulong val)
3073 {
3074     return RISCV_EXCP_NONE;
3075 }
3076 
3077 static RISCVException read_hgeip(CPURISCVState *env, int csrno,
3078                                  target_ulong *val)
3079 {
3080     if (val) {
3081         *val = env->hgeip;
3082     }
3083     return RISCV_EXCP_NONE;
3084 }
3085 
3086 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
3087                                  target_ulong *val)
3088 {
3089     *val = env->hgatp;
3090     return RISCV_EXCP_NONE;
3091 }
3092 
3093 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
3094                                   target_ulong val)
3095 {
3096     env->hgatp = val;
3097     return RISCV_EXCP_NONE;
3098 }
3099 
3100 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
3101                                       target_ulong *val)
3102 {
3103     if (!env->rdtime_fn) {
3104         return RISCV_EXCP_ILLEGAL_INST;
3105     }
3106 
3107     *val = env->htimedelta;
3108     return RISCV_EXCP_NONE;
3109 }
3110 
3111 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
3112                                        target_ulong val)
3113 {
3114     if (!env->rdtime_fn) {
3115         return RISCV_EXCP_ILLEGAL_INST;
3116     }
3117 
3118     if (riscv_cpu_mxl(env) == MXL_RV32) {
3119         env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
3120     } else {
3121         env->htimedelta = val;
3122     }
3123 
3124     if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3125         riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3126                                   env->htimedelta, MIP_VSTIP);
3127     }
3128 
3129     return RISCV_EXCP_NONE;
3130 }
3131 
3132 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
3133                                        target_ulong *val)
3134 {
3135     if (!env->rdtime_fn) {
3136         return RISCV_EXCP_ILLEGAL_INST;
3137     }
3138 
3139     *val = env->htimedelta >> 32;
3140     return RISCV_EXCP_NONE;
3141 }
3142 
3143 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
3144                                         target_ulong val)
3145 {
3146     if (!env->rdtime_fn) {
3147         return RISCV_EXCP_ILLEGAL_INST;
3148     }
3149 
3150     env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
3151 
3152     if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3153         riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3154                                   env->htimedelta, MIP_VSTIP);
3155     }
3156 
3157     return RISCV_EXCP_NONE;
3158 }
3159 
3160 static int read_hvictl(CPURISCVState *env, int csrno, target_ulong *val)
3161 {
3162     *val = env->hvictl;
3163     return RISCV_EXCP_NONE;
3164 }
3165 
3166 static int write_hvictl(CPURISCVState *env, int csrno, target_ulong val)
3167 {
3168     env->hvictl = val & HVICTL_VALID_MASK;
3169     return RISCV_EXCP_NONE;
3170 }
3171 
3172 static int read_hvipriox(CPURISCVState *env, int first_index,
3173                          uint8_t *iprio, target_ulong *val)
3174 {
3175     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3176 
3177     /* First index has to be a multiple of number of irqs per register */
3178     if (first_index % num_irqs) {
3179         return (env->virt_enabled) ?
3180                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3181     }
3182 
3183     /* Fill-up return value */
3184     *val = 0;
3185     for (i = 0; i < num_irqs; i++) {
3186         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3187             continue;
3188         }
3189         if (rdzero) {
3190             continue;
3191         }
3192         *val |= ((target_ulong)iprio[irq]) << (i * 8);
3193     }
3194 
3195     return RISCV_EXCP_NONE;
3196 }
3197 
3198 static int write_hvipriox(CPURISCVState *env, int first_index,
3199                           uint8_t *iprio, target_ulong val)
3200 {
3201     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3202 
3203     /* First index has to be a multiple of number of irqs per register */
3204     if (first_index % num_irqs) {
3205         return (env->virt_enabled) ?
3206                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3207     }
3208 
3209     /* Fill-up priority arrary */
3210     for (i = 0; i < num_irqs; i++) {
3211         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3212             continue;
3213         }
3214         if (rdzero) {
3215             iprio[irq] = 0;
3216         } else {
3217             iprio[irq] = (val >> (i * 8)) & 0xff;
3218         }
3219     }
3220 
3221     return RISCV_EXCP_NONE;
3222 }
3223 
3224 static int read_hviprio1(CPURISCVState *env, int csrno, target_ulong *val)
3225 {
3226     return read_hvipriox(env, 0, env->hviprio, val);
3227 }
3228 
3229 static int write_hviprio1(CPURISCVState *env, int csrno, target_ulong val)
3230 {
3231     return write_hvipriox(env, 0, env->hviprio, val);
3232 }
3233 
3234 static int read_hviprio1h(CPURISCVState *env, int csrno, target_ulong *val)
3235 {
3236     return read_hvipriox(env, 4, env->hviprio, val);
3237 }
3238 
3239 static int write_hviprio1h(CPURISCVState *env, int csrno, target_ulong val)
3240 {
3241     return write_hvipriox(env, 4, env->hviprio, val);
3242 }
3243 
3244 static int read_hviprio2(CPURISCVState *env, int csrno, target_ulong *val)
3245 {
3246     return read_hvipriox(env, 8, env->hviprio, val);
3247 }
3248 
3249 static int write_hviprio2(CPURISCVState *env, int csrno, target_ulong val)
3250 {
3251     return write_hvipriox(env, 8, env->hviprio, val);
3252 }
3253 
3254 static int read_hviprio2h(CPURISCVState *env, int csrno, target_ulong *val)
3255 {
3256     return read_hvipriox(env, 12, env->hviprio, val);
3257 }
3258 
3259 static int write_hviprio2h(CPURISCVState *env, int csrno, target_ulong val)
3260 {
3261     return write_hvipriox(env, 12, env->hviprio, val);
3262 }
3263 
3264 /* Virtual CSR Registers */
3265 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
3266                                     target_ulong *val)
3267 {
3268     *val = env->vsstatus;
3269     return RISCV_EXCP_NONE;
3270 }
3271 
3272 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
3273                                      target_ulong val)
3274 {
3275     uint64_t mask = (target_ulong)-1;
3276     if ((val & VSSTATUS64_UXL) == 0) {
3277         mask &= ~VSSTATUS64_UXL;
3278     }
3279     env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
3280     return RISCV_EXCP_NONE;
3281 }
3282 
3283 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val)
3284 {
3285     *val = env->vstvec;
3286     return RISCV_EXCP_NONE;
3287 }
3288 
3289 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
3290                                    target_ulong val)
3291 {
3292     env->vstvec = val;
3293     return RISCV_EXCP_NONE;
3294 }
3295 
3296 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
3297                                      target_ulong *val)
3298 {
3299     *val = env->vsscratch;
3300     return RISCV_EXCP_NONE;
3301 }
3302 
3303 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
3304                                       target_ulong val)
3305 {
3306     env->vsscratch = val;
3307     return RISCV_EXCP_NONE;
3308 }
3309 
3310 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
3311                                  target_ulong *val)
3312 {
3313     *val = env->vsepc;
3314     return RISCV_EXCP_NONE;
3315 }
3316 
3317 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
3318                                   target_ulong val)
3319 {
3320     env->vsepc = val;
3321     return RISCV_EXCP_NONE;
3322 }
3323 
3324 static RISCVException read_vscause(CPURISCVState *env, int csrno,
3325                                    target_ulong *val)
3326 {
3327     *val = env->vscause;
3328     return RISCV_EXCP_NONE;
3329 }
3330 
3331 static RISCVException write_vscause(CPURISCVState *env, int csrno,
3332                                     target_ulong val)
3333 {
3334     env->vscause = val;
3335     return RISCV_EXCP_NONE;
3336 }
3337 
3338 static RISCVException read_vstval(CPURISCVState *env, int csrno,
3339                                   target_ulong *val)
3340 {
3341     *val = env->vstval;
3342     return RISCV_EXCP_NONE;
3343 }
3344 
3345 static RISCVException write_vstval(CPURISCVState *env, int csrno,
3346                                    target_ulong val)
3347 {
3348     env->vstval = val;
3349     return RISCV_EXCP_NONE;
3350 }
3351 
3352 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
3353                                  target_ulong *val)
3354 {
3355     *val = env->vsatp;
3356     return RISCV_EXCP_NONE;
3357 }
3358 
3359 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
3360                                   target_ulong val)
3361 {
3362     env->vsatp = val;
3363     return RISCV_EXCP_NONE;
3364 }
3365 
3366 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
3367                                   target_ulong *val)
3368 {
3369     *val = env->mtval2;
3370     return RISCV_EXCP_NONE;
3371 }
3372 
3373 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
3374                                    target_ulong val)
3375 {
3376     env->mtval2 = val;
3377     return RISCV_EXCP_NONE;
3378 }
3379 
3380 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
3381                                   target_ulong *val)
3382 {
3383     *val = env->mtinst;
3384     return RISCV_EXCP_NONE;
3385 }
3386 
3387 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
3388                                    target_ulong val)
3389 {
3390     env->mtinst = val;
3391     return RISCV_EXCP_NONE;
3392 }
3393 
3394 /* Physical Memory Protection */
3395 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
3396                                    target_ulong *val)
3397 {
3398     *val = mseccfg_csr_read(env);
3399     return RISCV_EXCP_NONE;
3400 }
3401 
3402 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
3403                                     target_ulong val)
3404 {
3405     mseccfg_csr_write(env, val);
3406     return RISCV_EXCP_NONE;
3407 }
3408 
3409 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
3410                                   target_ulong *val)
3411 {
3412     uint32_t reg_index = csrno - CSR_PMPCFG0;
3413 
3414     *val = pmpcfg_csr_read(env, reg_index);
3415     return RISCV_EXCP_NONE;
3416 }
3417 
3418 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
3419                                    target_ulong val)
3420 {
3421     uint32_t reg_index = csrno - CSR_PMPCFG0;
3422 
3423     pmpcfg_csr_write(env, reg_index, val);
3424     return RISCV_EXCP_NONE;
3425 }
3426 
3427 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
3428                                    target_ulong *val)
3429 {
3430     *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
3431     return RISCV_EXCP_NONE;
3432 }
3433 
3434 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
3435                                     target_ulong val)
3436 {
3437     pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
3438     return RISCV_EXCP_NONE;
3439 }
3440 
3441 static RISCVException read_tselect(CPURISCVState *env, int csrno,
3442                                    target_ulong *val)
3443 {
3444     *val = tselect_csr_read(env);
3445     return RISCV_EXCP_NONE;
3446 }
3447 
3448 static RISCVException write_tselect(CPURISCVState *env, int csrno,
3449                                     target_ulong val)
3450 {
3451     tselect_csr_write(env, val);
3452     return RISCV_EXCP_NONE;
3453 }
3454 
3455 static RISCVException read_tdata(CPURISCVState *env, int csrno,
3456                                  target_ulong *val)
3457 {
3458     /* return 0 in tdata1 to end the trigger enumeration */
3459     if (env->trigger_cur >= RV_MAX_TRIGGERS && csrno == CSR_TDATA1) {
3460         *val = 0;
3461         return RISCV_EXCP_NONE;
3462     }
3463 
3464     if (!tdata_available(env, csrno - CSR_TDATA1)) {
3465         return RISCV_EXCP_ILLEGAL_INST;
3466     }
3467 
3468     *val = tdata_csr_read(env, csrno - CSR_TDATA1);
3469     return RISCV_EXCP_NONE;
3470 }
3471 
3472 static RISCVException write_tdata(CPURISCVState *env, int csrno,
3473                                   target_ulong val)
3474 {
3475     if (!tdata_available(env, csrno - CSR_TDATA1)) {
3476         return RISCV_EXCP_ILLEGAL_INST;
3477     }
3478 
3479     tdata_csr_write(env, csrno - CSR_TDATA1, val);
3480     return RISCV_EXCP_NONE;
3481 }
3482 
3483 static RISCVException read_tinfo(CPURISCVState *env, int csrno,
3484                                  target_ulong *val)
3485 {
3486     *val = tinfo_csr_read(env);
3487     return RISCV_EXCP_NONE;
3488 }
3489 
3490 /*
3491  * Functions to access Pointer Masking feature registers
3492  * We have to check if current priv lvl could modify
3493  * csr in given mode
3494  */
3495 static bool check_pm_current_disabled(CPURISCVState *env, int csrno)
3496 {
3497     int csr_priv = get_field(csrno, 0x300);
3498     int pm_current;
3499 
3500     if (env->debugger) {
3501         return false;
3502     }
3503     /*
3504      * If priv lvls differ that means we're accessing csr from higher priv lvl,
3505      * so allow the access
3506      */
3507     if (env->priv != csr_priv) {
3508         return false;
3509     }
3510     switch (env->priv) {
3511     case PRV_M:
3512         pm_current = get_field(env->mmte, M_PM_CURRENT);
3513         break;
3514     case PRV_S:
3515         pm_current = get_field(env->mmte, S_PM_CURRENT);
3516         break;
3517     case PRV_U:
3518         pm_current = get_field(env->mmte, U_PM_CURRENT);
3519         break;
3520     default:
3521         g_assert_not_reached();
3522     }
3523     /* It's same priv lvl, so we allow to modify csr only if pm.current==1 */
3524     return !pm_current;
3525 }
3526 
3527 static RISCVException read_mmte(CPURISCVState *env, int csrno,
3528                                 target_ulong *val)
3529 {
3530     *val = env->mmte & MMTE_MASK;
3531     return RISCV_EXCP_NONE;
3532 }
3533 
3534 static RISCVException write_mmte(CPURISCVState *env, int csrno,
3535                                  target_ulong val)
3536 {
3537     uint64_t mstatus;
3538     target_ulong wpri_val = val & MMTE_MASK;
3539 
3540     if (val != wpri_val) {
3541         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
3542                       TARGET_FMT_lx "\n", "MMTE: WPRI violation written 0x",
3543                       val, "vs expected 0x", wpri_val);
3544     }
3545     /* for machine mode pm.current is hardwired to 1 */
3546     wpri_val |= MMTE_M_PM_CURRENT;
3547 
3548     /* hardwiring pm.instruction bit to 0, since it's not supported yet */
3549     wpri_val &= ~(MMTE_M_PM_INSN | MMTE_S_PM_INSN | MMTE_U_PM_INSN);
3550     env->mmte = wpri_val | EXT_STATUS_DIRTY;
3551     riscv_cpu_update_mask(env);
3552 
3553     /* Set XS and SD bits, since PM CSRs are dirty */
3554     mstatus = env->mstatus | MSTATUS_XS;
3555     write_mstatus(env, csrno, mstatus);
3556     return RISCV_EXCP_NONE;
3557 }
3558 
3559 static RISCVException read_smte(CPURISCVState *env, int csrno,
3560                                 target_ulong *val)
3561 {
3562     *val = env->mmte & SMTE_MASK;
3563     return RISCV_EXCP_NONE;
3564 }
3565 
3566 static RISCVException write_smte(CPURISCVState *env, int csrno,
3567                                  target_ulong val)
3568 {
3569     target_ulong wpri_val = val & SMTE_MASK;
3570 
3571     if (val != wpri_val) {
3572         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
3573                       TARGET_FMT_lx "\n", "SMTE: WPRI violation written 0x",
3574                       val, "vs expected 0x", wpri_val);
3575     }
3576 
3577     /* if pm.current==0 we can't modify current PM CSRs */
3578     if (check_pm_current_disabled(env, csrno)) {
3579         return RISCV_EXCP_NONE;
3580     }
3581 
3582     wpri_val |= (env->mmte & ~SMTE_MASK);
3583     write_mmte(env, csrno, wpri_val);
3584     return RISCV_EXCP_NONE;
3585 }
3586 
3587 static RISCVException read_umte(CPURISCVState *env, int csrno,
3588                                 target_ulong *val)
3589 {
3590     *val = env->mmte & UMTE_MASK;
3591     return RISCV_EXCP_NONE;
3592 }
3593 
3594 static RISCVException write_umte(CPURISCVState *env, int csrno,
3595                                  target_ulong val)
3596 {
3597     target_ulong wpri_val = val & UMTE_MASK;
3598 
3599     if (val != wpri_val) {
3600         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
3601                       TARGET_FMT_lx "\n", "UMTE: WPRI violation written 0x",
3602                       val, "vs expected 0x", wpri_val);
3603     }
3604 
3605     if (check_pm_current_disabled(env, csrno)) {
3606         return RISCV_EXCP_NONE;
3607     }
3608 
3609     wpri_val |= (env->mmte & ~UMTE_MASK);
3610     write_mmte(env, csrno, wpri_val);
3611     return RISCV_EXCP_NONE;
3612 }
3613 
3614 static RISCVException read_mpmmask(CPURISCVState *env, int csrno,
3615                                    target_ulong *val)
3616 {
3617     *val = env->mpmmask;
3618     return RISCV_EXCP_NONE;
3619 }
3620 
3621 static RISCVException write_mpmmask(CPURISCVState *env, int csrno,
3622                                     target_ulong val)
3623 {
3624     uint64_t mstatus;
3625 
3626     env->mpmmask = val;
3627     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
3628         env->cur_pmmask = val;
3629     }
3630     env->mmte |= EXT_STATUS_DIRTY;
3631 
3632     /* Set XS and SD bits, since PM CSRs are dirty */
3633     mstatus = env->mstatus | MSTATUS_XS;
3634     write_mstatus(env, csrno, mstatus);
3635     return RISCV_EXCP_NONE;
3636 }
3637 
3638 static RISCVException read_spmmask(CPURISCVState *env, int csrno,
3639                                    target_ulong *val)
3640 {
3641     *val = env->spmmask;
3642     return RISCV_EXCP_NONE;
3643 }
3644 
3645 static RISCVException write_spmmask(CPURISCVState *env, int csrno,
3646                                     target_ulong val)
3647 {
3648     uint64_t mstatus;
3649 
3650     /* if pm.current==0 we can't modify current PM CSRs */
3651     if (check_pm_current_disabled(env, csrno)) {
3652         return RISCV_EXCP_NONE;
3653     }
3654     env->spmmask = val;
3655     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
3656         env->cur_pmmask = val;
3657     }
3658     env->mmte |= EXT_STATUS_DIRTY;
3659 
3660     /* Set XS and SD bits, since PM CSRs are dirty */
3661     mstatus = env->mstatus | MSTATUS_XS;
3662     write_mstatus(env, csrno, mstatus);
3663     return RISCV_EXCP_NONE;
3664 }
3665 
3666 static RISCVException read_upmmask(CPURISCVState *env, int csrno,
3667                                    target_ulong *val)
3668 {
3669     *val = env->upmmask;
3670     return RISCV_EXCP_NONE;
3671 }
3672 
3673 static RISCVException write_upmmask(CPURISCVState *env, int csrno,
3674                                     target_ulong val)
3675 {
3676     uint64_t mstatus;
3677 
3678     /* if pm.current==0 we can't modify current PM CSRs */
3679     if (check_pm_current_disabled(env, csrno)) {
3680         return RISCV_EXCP_NONE;
3681     }
3682     env->upmmask = val;
3683     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
3684         env->cur_pmmask = val;
3685     }
3686     env->mmte |= EXT_STATUS_DIRTY;
3687 
3688     /* Set XS and SD bits, since PM CSRs are dirty */
3689     mstatus = env->mstatus | MSTATUS_XS;
3690     write_mstatus(env, csrno, mstatus);
3691     return RISCV_EXCP_NONE;
3692 }
3693 
3694 static RISCVException read_mpmbase(CPURISCVState *env, int csrno,
3695                                    target_ulong *val)
3696 {
3697     *val = env->mpmbase;
3698     return RISCV_EXCP_NONE;
3699 }
3700 
3701 static RISCVException write_mpmbase(CPURISCVState *env, int csrno,
3702                                     target_ulong val)
3703 {
3704     uint64_t mstatus;
3705 
3706     env->mpmbase = val;
3707     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
3708         env->cur_pmbase = val;
3709     }
3710     env->mmte |= EXT_STATUS_DIRTY;
3711 
3712     /* Set XS and SD bits, since PM CSRs are dirty */
3713     mstatus = env->mstatus | MSTATUS_XS;
3714     write_mstatus(env, csrno, mstatus);
3715     return RISCV_EXCP_NONE;
3716 }
3717 
3718 static RISCVException read_spmbase(CPURISCVState *env, int csrno,
3719                                    target_ulong *val)
3720 {
3721     *val = env->spmbase;
3722     return RISCV_EXCP_NONE;
3723 }
3724 
3725 static RISCVException write_spmbase(CPURISCVState *env, int csrno,
3726                                     target_ulong val)
3727 {
3728     uint64_t mstatus;
3729 
3730     /* if pm.current==0 we can't modify current PM CSRs */
3731     if (check_pm_current_disabled(env, csrno)) {
3732         return RISCV_EXCP_NONE;
3733     }
3734     env->spmbase = val;
3735     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
3736         env->cur_pmbase = val;
3737     }
3738     env->mmte |= EXT_STATUS_DIRTY;
3739 
3740     /* Set XS and SD bits, since PM CSRs are dirty */
3741     mstatus = env->mstatus | MSTATUS_XS;
3742     write_mstatus(env, csrno, mstatus);
3743     return RISCV_EXCP_NONE;
3744 }
3745 
3746 static RISCVException read_upmbase(CPURISCVState *env, int csrno,
3747                                    target_ulong *val)
3748 {
3749     *val = env->upmbase;
3750     return RISCV_EXCP_NONE;
3751 }
3752 
3753 static RISCVException write_upmbase(CPURISCVState *env, int csrno,
3754                                     target_ulong val)
3755 {
3756     uint64_t mstatus;
3757 
3758     /* if pm.current==0 we can't modify current PM CSRs */
3759     if (check_pm_current_disabled(env, csrno)) {
3760         return RISCV_EXCP_NONE;
3761     }
3762     env->upmbase = val;
3763     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
3764         env->cur_pmbase = val;
3765     }
3766     env->mmte |= EXT_STATUS_DIRTY;
3767 
3768     /* Set XS and SD bits, since PM CSRs are dirty */
3769     mstatus = env->mstatus | MSTATUS_XS;
3770     write_mstatus(env, csrno, mstatus);
3771     return RISCV_EXCP_NONE;
3772 }
3773 
3774 #endif
3775 
3776 /* Crypto Extension */
3777 static RISCVException rmw_seed(CPURISCVState *env, int csrno,
3778                                target_ulong *ret_value,
3779                                target_ulong new_value,
3780                                target_ulong write_mask)
3781 {
3782     uint16_t random_v;
3783     Error *random_e = NULL;
3784     int random_r;
3785     target_ulong rval;
3786 
3787     random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
3788     if (unlikely(random_r < 0)) {
3789         /*
3790          * Failed, for unknown reasons in the crypto subsystem.
3791          * The best we can do is log the reason and return a
3792          * failure indication to the guest.  There is no reason
3793          * we know to expect the failure to be transitory, so
3794          * indicate DEAD to avoid having the guest spin on WAIT.
3795          */
3796         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
3797                       __func__, error_get_pretty(random_e));
3798         error_free(random_e);
3799         rval = SEED_OPST_DEAD;
3800     } else {
3801         rval = random_v | SEED_OPST_ES16;
3802     }
3803 
3804     if (ret_value) {
3805         *ret_value = rval;
3806     }
3807 
3808     return RISCV_EXCP_NONE;
3809 }
3810 
3811 /*
3812  * riscv_csrrw - read and/or update control and status register
3813  *
3814  * csrr   <->  riscv_csrrw(env, csrno, ret_value, 0, 0);
3815  * csrrw  <->  riscv_csrrw(env, csrno, ret_value, value, -1);
3816  * csrrs  <->  riscv_csrrw(env, csrno, ret_value, -1, value);
3817  * csrrc  <->  riscv_csrrw(env, csrno, ret_value, 0, value);
3818  */
3819 
3820 static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
3821                                                int csrno,
3822                                                bool write_mask)
3823 {
3824     /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
3825     bool read_only = get_field(csrno, 0xC00) == 3;
3826     int csr_min_priv = csr_ops[csrno].min_priv_ver;
3827 
3828     /* ensure the CSR extension is enabled */
3829     if (!riscv_cpu_cfg(env)->ext_icsr) {
3830         return RISCV_EXCP_ILLEGAL_INST;
3831     }
3832 
3833     /* privileged spec version check */
3834     if (env->priv_ver < csr_min_priv) {
3835         return RISCV_EXCP_ILLEGAL_INST;
3836     }
3837 
3838     /* read / write check */
3839     if (write_mask && read_only) {
3840         return RISCV_EXCP_ILLEGAL_INST;
3841     }
3842 
3843     /*
3844      * The predicate() not only does existence check but also does some
3845      * access control check which triggers for example virtual instruction
3846      * exception in some cases. When writing read-only CSRs in those cases
3847      * illegal instruction exception should be triggered instead of virtual
3848      * instruction exception. Hence this comes after the read / write check.
3849      */
3850     g_assert(csr_ops[csrno].predicate != NULL);
3851     RISCVException ret = csr_ops[csrno].predicate(env, csrno);
3852     if (ret != RISCV_EXCP_NONE) {
3853         return ret;
3854     }
3855 
3856 #if !defined(CONFIG_USER_ONLY)
3857     int csr_priv, effective_priv = env->priv;
3858 
3859     if (riscv_has_ext(env, RVH) && env->priv == PRV_S &&
3860         !env->virt_enabled) {
3861         /*
3862          * We are in HS mode. Add 1 to the effective privledge level to
3863          * allow us to access the Hypervisor CSRs.
3864          */
3865         effective_priv++;
3866     }
3867 
3868     csr_priv = get_field(csrno, 0x300);
3869     if (!env->debugger && (effective_priv < csr_priv)) {
3870         if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
3871             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
3872         }
3873         return RISCV_EXCP_ILLEGAL_INST;
3874     }
3875 #endif
3876     return RISCV_EXCP_NONE;
3877 }
3878 
3879 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,
3880                                        target_ulong *ret_value,
3881                                        target_ulong new_value,
3882                                        target_ulong write_mask)
3883 {
3884     RISCVException ret;
3885     target_ulong old_value;
3886 
3887     /* execute combined read/write operation if it exists */
3888     if (csr_ops[csrno].op) {
3889         return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
3890     }
3891 
3892     /* if no accessor exists then return failure */
3893     if (!csr_ops[csrno].read) {
3894         return RISCV_EXCP_ILLEGAL_INST;
3895     }
3896     /* read old value */
3897     ret = csr_ops[csrno].read(env, csrno, &old_value);
3898     if (ret != RISCV_EXCP_NONE) {
3899         return ret;
3900     }
3901 
3902     /* write value if writable and write mask set, otherwise drop writes */
3903     if (write_mask) {
3904         new_value = (old_value & ~write_mask) | (new_value & write_mask);
3905         if (csr_ops[csrno].write) {
3906             ret = csr_ops[csrno].write(env, csrno, new_value);
3907             if (ret != RISCV_EXCP_NONE) {
3908                 return ret;
3909             }
3910         }
3911     }
3912 
3913     /* return old value */
3914     if (ret_value) {
3915         *ret_value = old_value;
3916     }
3917 
3918     return RISCV_EXCP_NONE;
3919 }
3920 
3921 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
3922                            target_ulong *ret_value,
3923                            target_ulong new_value, target_ulong write_mask)
3924 {
3925     RISCVException ret = riscv_csrrw_check(env, csrno, write_mask);
3926     if (ret != RISCV_EXCP_NONE) {
3927         return ret;
3928     }
3929 
3930     return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask);
3931 }
3932 
3933 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,
3934                                         Int128 *ret_value,
3935                                         Int128 new_value,
3936                                         Int128 write_mask)
3937 {
3938     RISCVException ret;
3939     Int128 old_value;
3940 
3941     /* read old value */
3942     ret = csr_ops[csrno].read128(env, csrno, &old_value);
3943     if (ret != RISCV_EXCP_NONE) {
3944         return ret;
3945     }
3946 
3947     /* write value if writable and write mask set, otherwise drop writes */
3948     if (int128_nz(write_mask)) {
3949         new_value = int128_or(int128_and(old_value, int128_not(write_mask)),
3950                               int128_and(new_value, write_mask));
3951         if (csr_ops[csrno].write128) {
3952             ret = csr_ops[csrno].write128(env, csrno, new_value);
3953             if (ret != RISCV_EXCP_NONE) {
3954                 return ret;
3955             }
3956         } else if (csr_ops[csrno].write) {
3957             /* avoids having to write wrappers for all registers */
3958             ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value));
3959             if (ret != RISCV_EXCP_NONE) {
3960                 return ret;
3961             }
3962         }
3963     }
3964 
3965     /* return old value */
3966     if (ret_value) {
3967         *ret_value = old_value;
3968     }
3969 
3970     return RISCV_EXCP_NONE;
3971 }
3972 
3973 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
3974                                 Int128 *ret_value,
3975                                 Int128 new_value, Int128 write_mask)
3976 {
3977     RISCVException ret;
3978 
3979     ret = riscv_csrrw_check(env, csrno, int128_nz(write_mask));
3980     if (ret != RISCV_EXCP_NONE) {
3981         return ret;
3982     }
3983 
3984     if (csr_ops[csrno].read128) {
3985         return riscv_csrrw_do128(env, csrno, ret_value, new_value, write_mask);
3986     }
3987 
3988     /*
3989      * Fall back to 64-bit version for now, if the 128-bit alternative isn't
3990      * at all defined.
3991      * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
3992      * significant), for those, this fallback is correctly handling the
3993      * accesses
3994      */
3995     target_ulong old_value;
3996     ret = riscv_csrrw_do64(env, csrno, &old_value,
3997                            int128_getlo(new_value),
3998                            int128_getlo(write_mask));
3999     if (ret == RISCV_EXCP_NONE && ret_value) {
4000         *ret_value = int128_make64(old_value);
4001     }
4002     return ret;
4003 }
4004 
4005 /*
4006  * Debugger support.  If not in user mode, set env->debugger before the
4007  * riscv_csrrw call and clear it after the call.
4008  */
4009 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
4010                                  target_ulong *ret_value,
4011                                  target_ulong new_value,
4012                                  target_ulong write_mask)
4013 {
4014     RISCVException ret;
4015 #if !defined(CONFIG_USER_ONLY)
4016     env->debugger = true;
4017 #endif
4018     ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
4019 #if !defined(CONFIG_USER_ONLY)
4020     env->debugger = false;
4021 #endif
4022     return ret;
4023 }
4024 
4025 static RISCVException read_jvt(CPURISCVState *env, int csrno,
4026                                target_ulong *val)
4027 {
4028     *val = env->jvt;
4029     return RISCV_EXCP_NONE;
4030 }
4031 
4032 static RISCVException write_jvt(CPURISCVState *env, int csrno,
4033                                 target_ulong val)
4034 {
4035     env->jvt = val;
4036     return RISCV_EXCP_NONE;
4037 }
4038 
4039 /* Control and Status Register function table */
4040 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
4041     /* User Floating-Point CSRs */
4042     [CSR_FFLAGS]   = { "fflags",   fs,     read_fflags,  write_fflags },
4043     [CSR_FRM]      = { "frm",      fs,     read_frm,     write_frm    },
4044     [CSR_FCSR]     = { "fcsr",     fs,     read_fcsr,    write_fcsr   },
4045     /* Vector CSRs */
4046     [CSR_VSTART]   = { "vstart",   vs,     read_vstart,  write_vstart },
4047     [CSR_VXSAT]    = { "vxsat",    vs,     read_vxsat,   write_vxsat  },
4048     [CSR_VXRM]     = { "vxrm",     vs,     read_vxrm,    write_vxrm   },
4049     [CSR_VCSR]     = { "vcsr",     vs,     read_vcsr,    write_vcsr   },
4050     [CSR_VL]       = { "vl",       vs,     read_vl                    },
4051     [CSR_VTYPE]    = { "vtype",    vs,     read_vtype                 },
4052     [CSR_VLENB]    = { "vlenb",    vs,     read_vlenb                 },
4053     /* User Timers and Counters */
4054     [CSR_CYCLE]    = { "cycle",    ctr,    read_hpmcounter  },
4055     [CSR_INSTRET]  = { "instret",  ctr,    read_hpmcounter  },
4056     [CSR_CYCLEH]   = { "cycleh",   ctr32,  read_hpmcounterh },
4057     [CSR_INSTRETH] = { "instreth", ctr32,  read_hpmcounterh },
4058 
4059     /*
4060      * In privileged mode, the monitor will have to emulate TIME CSRs only if
4061      * rdtime callback is not provided by machine/platform emulation.
4062      */
4063     [CSR_TIME]  = { "time",  ctr,   read_time  },
4064     [CSR_TIMEH] = { "timeh", ctr32, read_timeh },
4065 
4066     /* Crypto Extension */
4067     [CSR_SEED] = { "seed", seed, NULL, NULL, rmw_seed },
4068 
4069     /* Zcmt Extension */
4070     [CSR_JVT] = {"jvt", zcmt, read_jvt, write_jvt},
4071 
4072 #if !defined(CONFIG_USER_ONLY)
4073     /* Machine Timers and Counters */
4074     [CSR_MCYCLE]    = { "mcycle",    any,   read_hpmcounter,
4075                         write_mhpmcounter                    },
4076     [CSR_MINSTRET]  = { "minstret",  any,   read_hpmcounter,
4077                         write_mhpmcounter                    },
4078     [CSR_MCYCLEH]   = { "mcycleh",   any32, read_hpmcounterh,
4079                         write_mhpmcounterh                   },
4080     [CSR_MINSTRETH] = { "minstreth", any32, read_hpmcounterh,
4081                         write_mhpmcounterh                   },
4082 
4083     /* Machine Information Registers */
4084     [CSR_MVENDORID] = { "mvendorid", any,   read_mvendorid },
4085     [CSR_MARCHID]   = { "marchid",   any,   read_marchid   },
4086     [CSR_MIMPID]    = { "mimpid",    any,   read_mimpid    },
4087     [CSR_MHARTID]   = { "mhartid",   any,   read_mhartid   },
4088 
4089     [CSR_MCONFIGPTR]  = { "mconfigptr", any,   read_zero,
4090                           .min_priv_ver = PRIV_VERSION_1_12_0 },
4091     /* Machine Trap Setup */
4092     [CSR_MSTATUS]     = { "mstatus",    any,   read_mstatus, write_mstatus,
4093                           NULL,                read_mstatus_i128           },
4094     [CSR_MISA]        = { "misa",       any,   read_misa,    write_misa,
4095                           NULL,                read_misa_i128              },
4096     [CSR_MIDELEG]     = { "mideleg",    any,   NULL, NULL,   rmw_mideleg   },
4097     [CSR_MEDELEG]     = { "medeleg",    any,   read_medeleg, write_medeleg },
4098     [CSR_MIE]         = { "mie",        any,   NULL, NULL,   rmw_mie       },
4099     [CSR_MTVEC]       = { "mtvec",      any,   read_mtvec,   write_mtvec   },
4100     [CSR_MCOUNTEREN]  = { "mcounteren", umode, read_mcounteren,
4101                           write_mcounteren                                 },
4102 
4103     [CSR_MSTATUSH]    = { "mstatush",   any32, read_mstatush,
4104                           write_mstatush                                   },
4105 
4106     /* Machine Trap Handling */
4107     [CSR_MSCRATCH] = { "mscratch", any,  read_mscratch, write_mscratch,
4108                        NULL, read_mscratch_i128, write_mscratch_i128   },
4109     [CSR_MEPC]     = { "mepc",     any,  read_mepc,     write_mepc     },
4110     [CSR_MCAUSE]   = { "mcause",   any,  read_mcause,   write_mcause   },
4111     [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval    },
4112     [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip        },
4113 
4114     /* Machine-Level Window to Indirectly Accessed Registers (AIA) */
4115     [CSR_MISELECT] = { "miselect", aia_any,   NULL, NULL,    rmw_xiselect },
4116     [CSR_MIREG]    = { "mireg",    aia_any,   NULL, NULL,    rmw_xireg },
4117 
4118     /* Machine-Level Interrupts (AIA) */
4119     [CSR_MTOPEI]   = { "mtopei",   aia_any, NULL, NULL, rmw_xtopei },
4120     [CSR_MTOPI]    = { "mtopi",    aia_any, read_mtopi },
4121 
4122     /* Virtual Interrupts for Supervisor Level (AIA) */
4123     [CSR_MVIEN]    = { "mvien",    aia_any, read_zero, write_ignore },
4124     [CSR_MVIP]     = { "mvip",     aia_any, read_zero, write_ignore },
4125 
4126     /* Machine-Level High-Half CSRs (AIA) */
4127     [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
4128     [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh     },
4129     [CSR_MVIENH]   = { "mvienh",   aia_any32, read_zero,  write_ignore },
4130     [CSR_MVIPH]    = { "mviph",    aia_any32, read_zero,  write_ignore },
4131     [CSR_MIPH]     = { "miph",     aia_any32, NULL, NULL, rmw_miph     },
4132 
4133     /* Execution environment configuration */
4134     [CSR_MENVCFG]  = { "menvcfg",  umode, read_menvcfg,  write_menvcfg,
4135                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4136     [CSR_MENVCFGH] = { "menvcfgh", umode32, read_menvcfgh, write_menvcfgh,
4137                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4138     [CSR_SENVCFG]  = { "senvcfg",  smode, read_senvcfg,  write_senvcfg,
4139                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4140     [CSR_HENVCFG]  = { "henvcfg",  hmode, read_henvcfg, write_henvcfg,
4141                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4142     [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh, write_henvcfgh,
4143                        .min_priv_ver = PRIV_VERSION_1_12_0              },
4144 
4145     /* Smstateen extension CSRs */
4146     [CSR_MSTATEEN0] = { "mstateen0", mstateen, read_mstateen, write_mstateen0,
4147                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4148     [CSR_MSTATEEN0H] = { "mstateen0h", mstateen, read_mstateenh,
4149                           write_mstateen0h,
4150                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4151     [CSR_MSTATEEN1] = { "mstateen1", mstateen, read_mstateen,
4152                         write_mstateen_1_3,
4153                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4154     [CSR_MSTATEEN1H] = { "mstateen1h", mstateen, read_mstateenh,
4155                          write_mstateenh_1_3,
4156                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4157     [CSR_MSTATEEN2] = { "mstateen2", mstateen, read_mstateen,
4158                         write_mstateen_1_3,
4159                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4160     [CSR_MSTATEEN2H] = { "mstateen2h", mstateen, read_mstateenh,
4161                          write_mstateenh_1_3,
4162                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4163     [CSR_MSTATEEN3] = { "mstateen3", mstateen, read_mstateen,
4164                         write_mstateen_1_3,
4165                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4166     [CSR_MSTATEEN3H] = { "mstateen3h", mstateen, read_mstateenh,
4167                          write_mstateenh_1_3,
4168                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4169     [CSR_HSTATEEN0] = { "hstateen0", hstateen, read_hstateen, write_hstateen0,
4170                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4171     [CSR_HSTATEEN0H] = { "hstateen0h", hstateenh, read_hstateenh,
4172                          write_hstateen0h,
4173                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4174     [CSR_HSTATEEN1] = { "hstateen1", hstateen, read_hstateen,
4175                         write_hstateen_1_3,
4176                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4177     [CSR_HSTATEEN1H] = { "hstateen1h", hstateenh, read_hstateenh,
4178                          write_hstateenh_1_3,
4179                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4180     [CSR_HSTATEEN2] = { "hstateen2", hstateen, read_hstateen,
4181                         write_hstateen_1_3,
4182                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4183     [CSR_HSTATEEN2H] = { "hstateen2h", hstateenh, read_hstateenh,
4184                          write_hstateenh_1_3,
4185                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4186     [CSR_HSTATEEN3] = { "hstateen3", hstateen, read_hstateen,
4187                         write_hstateen_1_3,
4188                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4189     [CSR_HSTATEEN3H] = { "hstateen3h", hstateenh, read_hstateenh,
4190                          write_hstateenh_1_3,
4191                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4192     [CSR_SSTATEEN0] = { "sstateen0", sstateen, read_sstateen, write_sstateen0,
4193                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4194     [CSR_SSTATEEN1] = { "sstateen1", sstateen, read_sstateen,
4195                         write_sstateen_1_3,
4196                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4197     [CSR_SSTATEEN2] = { "sstateen2", sstateen, read_sstateen,
4198                         write_sstateen_1_3,
4199                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4200     [CSR_SSTATEEN3] = { "sstateen3", sstateen, read_sstateen,
4201                         write_sstateen_1_3,
4202                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4203 
4204     /* Supervisor Trap Setup */
4205     [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,    write_sstatus,
4206                          NULL,                read_sstatus_i128              },
4207     [CSR_SIE]        = { "sie",        smode, NULL,   NULL,    rmw_sie       },
4208     [CSR_STVEC]      = { "stvec",      smode, read_stvec,      write_stvec   },
4209     [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren,
4210                          write_scounteren                                    },
4211 
4212     /* Supervisor Trap Handling */
4213     [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch,
4214                        NULL, read_sscratch_i128, write_sscratch_i128    },
4215     [CSR_SEPC]     = { "sepc",     smode, read_sepc,     write_sepc     },
4216     [CSR_SCAUSE]   = { "scause",   smode, read_scause,   write_scause   },
4217     [CSR_STVAL]    = { "stval",    smode, read_stval,    write_stval    },
4218     [CSR_SIP]      = { "sip",      smode, NULL,    NULL, rmw_sip        },
4219     [CSR_STIMECMP] = { "stimecmp", sstc, read_stimecmp, write_stimecmp,
4220                        .min_priv_ver = PRIV_VERSION_1_12_0 },
4221     [CSR_STIMECMPH] = { "stimecmph", sstc_32, read_stimecmph, write_stimecmph,
4222                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4223     [CSR_VSTIMECMP] = { "vstimecmp", sstc, read_vstimecmp,
4224                         write_vstimecmp,
4225                         .min_priv_ver = PRIV_VERSION_1_12_0 },
4226     [CSR_VSTIMECMPH] = { "vstimecmph", sstc_32, read_vstimecmph,
4227                          write_vstimecmph,
4228                          .min_priv_ver = PRIV_VERSION_1_12_0 },
4229 
4230     /* Supervisor Protection and Translation */
4231     [CSR_SATP]     = { "satp",     satp, read_satp,     write_satp     },
4232 
4233     /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */
4234     [CSR_SISELECT]   = { "siselect",   aia_smode, NULL, NULL, rmw_xiselect },
4235     [CSR_SIREG]      = { "sireg",      aia_smode, NULL, NULL, rmw_xireg },
4236 
4237     /* Supervisor-Level Interrupts (AIA) */
4238     [CSR_STOPEI]     = { "stopei",     aia_smode, NULL, NULL, rmw_xtopei },
4239     [CSR_STOPI]      = { "stopi",      aia_smode, read_stopi },
4240 
4241     /* Supervisor-Level High-Half CSRs (AIA) */
4242     [CSR_SIEH]       = { "sieh",   aia_smode32, NULL, NULL, rmw_sieh },
4243     [CSR_SIPH]       = { "siph",   aia_smode32, NULL, NULL, rmw_siph },
4244 
4245     [CSR_HSTATUS]     = { "hstatus",     hmode,   read_hstatus, write_hstatus,
4246                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4247     [CSR_HEDELEG]     = { "hedeleg",     hmode,   read_hedeleg, write_hedeleg,
4248                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4249     [CSR_HIDELEG]     = { "hideleg",     hmode,   NULL,   NULL, rmw_hideleg,
4250                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4251     [CSR_HVIP]        = { "hvip",        hmode,   NULL,   NULL, rmw_hvip,
4252                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4253     [CSR_HIP]         = { "hip",         hmode,   NULL,   NULL, rmw_hip,
4254                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4255     [CSR_HIE]         = { "hie",         hmode,   NULL,   NULL, rmw_hie,
4256                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4257     [CSR_HCOUNTEREN]  = { "hcounteren",  hmode,   read_hcounteren,
4258                           write_hcounteren,
4259                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4260     [CSR_HGEIE]       = { "hgeie",       hmode,   read_hgeie,   write_hgeie,
4261                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4262     [CSR_HTVAL]       = { "htval",       hmode,   read_htval,   write_htval,
4263                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4264     [CSR_HTINST]      = { "htinst",      hmode,   read_htinst,  write_htinst,
4265                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4266     [CSR_HGEIP]       = { "hgeip",       hmode,   read_hgeip,
4267                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4268     [CSR_HGATP]       = { "hgatp",       hgatp,   read_hgatp,   write_hgatp,
4269                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4270     [CSR_HTIMEDELTA]  = { "htimedelta",  hmode,   read_htimedelta,
4271                           write_htimedelta,
4272                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4273     [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah,
4274                           write_htimedeltah,
4275                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4276 
4277     [CSR_VSSTATUS]    = { "vsstatus",    hmode,   read_vsstatus,
4278                           write_vsstatus,
4279                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4280     [CSR_VSIP]        = { "vsip",        hmode,   NULL,    NULL, rmw_vsip,
4281                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4282     [CSR_VSIE]        = { "vsie",        hmode,   NULL,    NULL, rmw_vsie ,
4283                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4284     [CSR_VSTVEC]      = { "vstvec",      hmode,   read_vstvec,   write_vstvec,
4285                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4286     [CSR_VSSCRATCH]   = { "vsscratch",   hmode,   read_vsscratch,
4287                           write_vsscratch,
4288                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4289     [CSR_VSEPC]       = { "vsepc",       hmode,   read_vsepc,    write_vsepc,
4290                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4291     [CSR_VSCAUSE]     = { "vscause",     hmode,   read_vscause,  write_vscause,
4292                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4293     [CSR_VSTVAL]      = { "vstval",      hmode,   read_vstval,   write_vstval,
4294                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4295     [CSR_VSATP]       = { "vsatp",       hmode,   read_vsatp,    write_vsatp,
4296                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4297 
4298     [CSR_MTVAL2]      = { "mtval2",      hmode,   read_mtval2,   write_mtval2,
4299                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4300     [CSR_MTINST]      = { "mtinst",      hmode,   read_mtinst,   write_mtinst,
4301                           .min_priv_ver = PRIV_VERSION_1_12_0                },
4302 
4303     /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */
4304     [CSR_HVIEN]       = { "hvien",       aia_hmode, read_zero, write_ignore },
4305     [CSR_HVICTL]      = { "hvictl",      aia_hmode, read_hvictl,
4306                           write_hvictl                                      },
4307     [CSR_HVIPRIO1]    = { "hviprio1",    aia_hmode, read_hviprio1,
4308                           write_hviprio1                                    },
4309     [CSR_HVIPRIO2]    = { "hviprio2",    aia_hmode, read_hviprio2,
4310                           write_hviprio2                                    },
4311 
4312     /*
4313      * VS-Level Window to Indirectly Accessed Registers (H-extension with AIA)
4314      */
4315     [CSR_VSISELECT]   = { "vsiselect",   aia_hmode, NULL, NULL,
4316                           rmw_xiselect                                     },
4317     [CSR_VSIREG]      = { "vsireg",      aia_hmode, NULL, NULL, rmw_xireg  },
4318 
4319     /* VS-Level Interrupts (H-extension with AIA) */
4320     [CSR_VSTOPEI]     = { "vstopei",     aia_hmode, NULL, NULL, rmw_xtopei },
4321     [CSR_VSTOPI]      = { "vstopi",      aia_hmode, read_vstopi },
4322 
4323     /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
4324     [CSR_HIDELEGH]    = { "hidelegh",    aia_hmode32, NULL, NULL,
4325                           rmw_hidelegh                                      },
4326     [CSR_HVIENH]      = { "hvienh",      aia_hmode32, read_zero,
4327                           write_ignore                                      },
4328     [CSR_HVIPH]       = { "hviph",       aia_hmode32, NULL, NULL, rmw_hviph },
4329     [CSR_HVIPRIO1H]   = { "hviprio1h",   aia_hmode32, read_hviprio1h,
4330                           write_hviprio1h                                   },
4331     [CSR_HVIPRIO2H]   = { "hviprio2h",   aia_hmode32, read_hviprio2h,
4332                           write_hviprio2h                                   },
4333     [CSR_VSIEH]       = { "vsieh",       aia_hmode32, NULL, NULL, rmw_vsieh },
4334     [CSR_VSIPH]       = { "vsiph",       aia_hmode32, NULL, NULL, rmw_vsiph },
4335 
4336     /* Physical Memory Protection */
4337     [CSR_MSECCFG]    = { "mseccfg",  epmp, read_mseccfg, write_mseccfg,
4338                          .min_priv_ver = PRIV_VERSION_1_11_0           },
4339     [CSR_PMPCFG0]    = { "pmpcfg0",   pmp, read_pmpcfg,  write_pmpcfg  },
4340     [CSR_PMPCFG1]    = { "pmpcfg1",   pmp, read_pmpcfg,  write_pmpcfg  },
4341     [CSR_PMPCFG2]    = { "pmpcfg2",   pmp, read_pmpcfg,  write_pmpcfg  },
4342     [CSR_PMPCFG3]    = { "pmpcfg3",   pmp, read_pmpcfg,  write_pmpcfg  },
4343     [CSR_PMPADDR0]   = { "pmpaddr0",  pmp, read_pmpaddr, write_pmpaddr },
4344     [CSR_PMPADDR1]   = { "pmpaddr1",  pmp, read_pmpaddr, write_pmpaddr },
4345     [CSR_PMPADDR2]   = { "pmpaddr2",  pmp, read_pmpaddr, write_pmpaddr },
4346     [CSR_PMPADDR3]   = { "pmpaddr3",  pmp, read_pmpaddr, write_pmpaddr },
4347     [CSR_PMPADDR4]   = { "pmpaddr4",  pmp, read_pmpaddr, write_pmpaddr },
4348     [CSR_PMPADDR5]   = { "pmpaddr5",  pmp, read_pmpaddr, write_pmpaddr },
4349     [CSR_PMPADDR6]   = { "pmpaddr6",  pmp, read_pmpaddr, write_pmpaddr },
4350     [CSR_PMPADDR7]   = { "pmpaddr7",  pmp, read_pmpaddr, write_pmpaddr },
4351     [CSR_PMPADDR8]   = { "pmpaddr8",  pmp, read_pmpaddr, write_pmpaddr },
4352     [CSR_PMPADDR9]   = { "pmpaddr9",  pmp, read_pmpaddr, write_pmpaddr },
4353     [CSR_PMPADDR10]  = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr },
4354     [CSR_PMPADDR11]  = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr },
4355     [CSR_PMPADDR12]  = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr },
4356     [CSR_PMPADDR13]  = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr },
4357     [CSR_PMPADDR14] =  { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr },
4358     [CSR_PMPADDR15] =  { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr },
4359 
4360     /* Debug CSRs */
4361     [CSR_TSELECT]   =  { "tselect", debug, read_tselect, write_tselect },
4362     [CSR_TDATA1]    =  { "tdata1",  debug, read_tdata,   write_tdata   },
4363     [CSR_TDATA2]    =  { "tdata2",  debug, read_tdata,   write_tdata   },
4364     [CSR_TDATA3]    =  { "tdata3",  debug, read_tdata,   write_tdata   },
4365     [CSR_TINFO]     =  { "tinfo",   debug, read_tinfo,   write_ignore  },
4366 
4367     /* User Pointer Masking */
4368     [CSR_UMTE]    =    { "umte",    pointer_masking, read_umte,  write_umte },
4369     [CSR_UPMMASK] =    { "upmmask", pointer_masking, read_upmmask,
4370                          write_upmmask                                      },
4371     [CSR_UPMBASE] =    { "upmbase", pointer_masking, read_upmbase,
4372                          write_upmbase                                      },
4373     /* Machine Pointer Masking */
4374     [CSR_MMTE]    =    { "mmte",    pointer_masking, read_mmte,  write_mmte },
4375     [CSR_MPMMASK] =    { "mpmmask", pointer_masking, read_mpmmask,
4376                          write_mpmmask                                      },
4377     [CSR_MPMBASE] =    { "mpmbase", pointer_masking, read_mpmbase,
4378                          write_mpmbase                                      },
4379     /* Supervisor Pointer Masking */
4380     [CSR_SMTE]    =    { "smte",    pointer_masking, read_smte,  write_smte },
4381     [CSR_SPMMASK] =    { "spmmask", pointer_masking, read_spmmask,
4382                          write_spmmask                                      },
4383     [CSR_SPMBASE] =    { "spmbase", pointer_masking, read_spmbase,
4384                          write_spmbase                                      },
4385 
4386     /* Performance Counters */
4387     [CSR_HPMCOUNTER3]    = { "hpmcounter3",    ctr,    read_hpmcounter },
4388     [CSR_HPMCOUNTER4]    = { "hpmcounter4",    ctr,    read_hpmcounter },
4389     [CSR_HPMCOUNTER5]    = { "hpmcounter5",    ctr,    read_hpmcounter },
4390     [CSR_HPMCOUNTER6]    = { "hpmcounter6",    ctr,    read_hpmcounter },
4391     [CSR_HPMCOUNTER7]    = { "hpmcounter7",    ctr,    read_hpmcounter },
4392     [CSR_HPMCOUNTER8]    = { "hpmcounter8",    ctr,    read_hpmcounter },
4393     [CSR_HPMCOUNTER9]    = { "hpmcounter9",    ctr,    read_hpmcounter },
4394     [CSR_HPMCOUNTER10]   = { "hpmcounter10",   ctr,    read_hpmcounter },
4395     [CSR_HPMCOUNTER11]   = { "hpmcounter11",   ctr,    read_hpmcounter },
4396     [CSR_HPMCOUNTER12]   = { "hpmcounter12",   ctr,    read_hpmcounter },
4397     [CSR_HPMCOUNTER13]   = { "hpmcounter13",   ctr,    read_hpmcounter },
4398     [CSR_HPMCOUNTER14]   = { "hpmcounter14",   ctr,    read_hpmcounter },
4399     [CSR_HPMCOUNTER15]   = { "hpmcounter15",   ctr,    read_hpmcounter },
4400     [CSR_HPMCOUNTER16]   = { "hpmcounter16",   ctr,    read_hpmcounter },
4401     [CSR_HPMCOUNTER17]   = { "hpmcounter17",   ctr,    read_hpmcounter },
4402     [CSR_HPMCOUNTER18]   = { "hpmcounter18",   ctr,    read_hpmcounter },
4403     [CSR_HPMCOUNTER19]   = { "hpmcounter19",   ctr,    read_hpmcounter },
4404     [CSR_HPMCOUNTER20]   = { "hpmcounter20",   ctr,    read_hpmcounter },
4405     [CSR_HPMCOUNTER21]   = { "hpmcounter21",   ctr,    read_hpmcounter },
4406     [CSR_HPMCOUNTER22]   = { "hpmcounter22",   ctr,    read_hpmcounter },
4407     [CSR_HPMCOUNTER23]   = { "hpmcounter23",   ctr,    read_hpmcounter },
4408     [CSR_HPMCOUNTER24]   = { "hpmcounter24",   ctr,    read_hpmcounter },
4409     [CSR_HPMCOUNTER25]   = { "hpmcounter25",   ctr,    read_hpmcounter },
4410     [CSR_HPMCOUNTER26]   = { "hpmcounter26",   ctr,    read_hpmcounter },
4411     [CSR_HPMCOUNTER27]   = { "hpmcounter27",   ctr,    read_hpmcounter },
4412     [CSR_HPMCOUNTER28]   = { "hpmcounter28",   ctr,    read_hpmcounter },
4413     [CSR_HPMCOUNTER29]   = { "hpmcounter29",   ctr,    read_hpmcounter },
4414     [CSR_HPMCOUNTER30]   = { "hpmcounter30",   ctr,    read_hpmcounter },
4415     [CSR_HPMCOUNTER31]   = { "hpmcounter31",   ctr,    read_hpmcounter },
4416 
4417     [CSR_MHPMCOUNTER3]   = { "mhpmcounter3",   mctr,    read_hpmcounter,
4418                              write_mhpmcounter                         },
4419     [CSR_MHPMCOUNTER4]   = { "mhpmcounter4",   mctr,    read_hpmcounter,
4420                              write_mhpmcounter                         },
4421     [CSR_MHPMCOUNTER5]   = { "mhpmcounter5",   mctr,    read_hpmcounter,
4422                              write_mhpmcounter                         },
4423     [CSR_MHPMCOUNTER6]   = { "mhpmcounter6",   mctr,    read_hpmcounter,
4424                              write_mhpmcounter                         },
4425     [CSR_MHPMCOUNTER7]   = { "mhpmcounter7",   mctr,    read_hpmcounter,
4426                              write_mhpmcounter                         },
4427     [CSR_MHPMCOUNTER8]   = { "mhpmcounter8",   mctr,    read_hpmcounter,
4428                              write_mhpmcounter                         },
4429     [CSR_MHPMCOUNTER9]   = { "mhpmcounter9",   mctr,    read_hpmcounter,
4430                              write_mhpmcounter                         },
4431     [CSR_MHPMCOUNTER10]  = { "mhpmcounter10",  mctr,    read_hpmcounter,
4432                              write_mhpmcounter                         },
4433     [CSR_MHPMCOUNTER11]  = { "mhpmcounter11",  mctr,    read_hpmcounter,
4434                              write_mhpmcounter                         },
4435     [CSR_MHPMCOUNTER12]  = { "mhpmcounter12",  mctr,    read_hpmcounter,
4436                              write_mhpmcounter                         },
4437     [CSR_MHPMCOUNTER13]  = { "mhpmcounter13",  mctr,    read_hpmcounter,
4438                              write_mhpmcounter                         },
4439     [CSR_MHPMCOUNTER14]  = { "mhpmcounter14",  mctr,    read_hpmcounter,
4440                              write_mhpmcounter                         },
4441     [CSR_MHPMCOUNTER15]  = { "mhpmcounter15",  mctr,    read_hpmcounter,
4442                              write_mhpmcounter                         },
4443     [CSR_MHPMCOUNTER16]  = { "mhpmcounter16",  mctr,    read_hpmcounter,
4444                              write_mhpmcounter                         },
4445     [CSR_MHPMCOUNTER17]  = { "mhpmcounter17",  mctr,    read_hpmcounter,
4446                              write_mhpmcounter                         },
4447     [CSR_MHPMCOUNTER18]  = { "mhpmcounter18",  mctr,    read_hpmcounter,
4448                              write_mhpmcounter                         },
4449     [CSR_MHPMCOUNTER19]  = { "mhpmcounter19",  mctr,    read_hpmcounter,
4450                              write_mhpmcounter                         },
4451     [CSR_MHPMCOUNTER20]  = { "mhpmcounter20",  mctr,    read_hpmcounter,
4452                              write_mhpmcounter                         },
4453     [CSR_MHPMCOUNTER21]  = { "mhpmcounter21",  mctr,    read_hpmcounter,
4454                              write_mhpmcounter                         },
4455     [CSR_MHPMCOUNTER22]  = { "mhpmcounter22",  mctr,    read_hpmcounter,
4456                              write_mhpmcounter                         },
4457     [CSR_MHPMCOUNTER23]  = { "mhpmcounter23",  mctr,    read_hpmcounter,
4458                              write_mhpmcounter                         },
4459     [CSR_MHPMCOUNTER24]  = { "mhpmcounter24",  mctr,    read_hpmcounter,
4460                              write_mhpmcounter                         },
4461     [CSR_MHPMCOUNTER25]  = { "mhpmcounter25",  mctr,    read_hpmcounter,
4462                              write_mhpmcounter                         },
4463     [CSR_MHPMCOUNTER26]  = { "mhpmcounter26",  mctr,    read_hpmcounter,
4464                              write_mhpmcounter                         },
4465     [CSR_MHPMCOUNTER27]  = { "mhpmcounter27",  mctr,    read_hpmcounter,
4466                              write_mhpmcounter                         },
4467     [CSR_MHPMCOUNTER28]  = { "mhpmcounter28",  mctr,    read_hpmcounter,
4468                              write_mhpmcounter                         },
4469     [CSR_MHPMCOUNTER29]  = { "mhpmcounter29",  mctr,    read_hpmcounter,
4470                              write_mhpmcounter                         },
4471     [CSR_MHPMCOUNTER30]  = { "mhpmcounter30",  mctr,    read_hpmcounter,
4472                              write_mhpmcounter                         },
4473     [CSR_MHPMCOUNTER31]  = { "mhpmcounter31",  mctr,    read_hpmcounter,
4474                              write_mhpmcounter                         },
4475 
4476     [CSR_MCOUNTINHIBIT]  = { "mcountinhibit",  any, read_mcountinhibit,
4477                              write_mcountinhibit,
4478                              .min_priv_ver = PRIV_VERSION_1_11_0       },
4479 
4480     [CSR_MHPMEVENT3]     = { "mhpmevent3",     any,    read_mhpmevent,
4481                              write_mhpmevent                           },
4482     [CSR_MHPMEVENT4]     = { "mhpmevent4",     any,    read_mhpmevent,
4483                              write_mhpmevent                           },
4484     [CSR_MHPMEVENT5]     = { "mhpmevent5",     any,    read_mhpmevent,
4485                              write_mhpmevent                           },
4486     [CSR_MHPMEVENT6]     = { "mhpmevent6",     any,    read_mhpmevent,
4487                              write_mhpmevent                           },
4488     [CSR_MHPMEVENT7]     = { "mhpmevent7",     any,    read_mhpmevent,
4489                              write_mhpmevent                           },
4490     [CSR_MHPMEVENT8]     = { "mhpmevent8",     any,    read_mhpmevent,
4491                              write_mhpmevent                           },
4492     [CSR_MHPMEVENT9]     = { "mhpmevent9",     any,    read_mhpmevent,
4493                              write_mhpmevent                           },
4494     [CSR_MHPMEVENT10]    = { "mhpmevent10",    any,    read_mhpmevent,
4495                              write_mhpmevent                           },
4496     [CSR_MHPMEVENT11]    = { "mhpmevent11",    any,    read_mhpmevent,
4497                              write_mhpmevent                           },
4498     [CSR_MHPMEVENT12]    = { "mhpmevent12",    any,    read_mhpmevent,
4499                              write_mhpmevent                           },
4500     [CSR_MHPMEVENT13]    = { "mhpmevent13",    any,    read_mhpmevent,
4501                              write_mhpmevent                           },
4502     [CSR_MHPMEVENT14]    = { "mhpmevent14",    any,    read_mhpmevent,
4503                              write_mhpmevent                           },
4504     [CSR_MHPMEVENT15]    = { "mhpmevent15",    any,    read_mhpmevent,
4505                              write_mhpmevent                           },
4506     [CSR_MHPMEVENT16]    = { "mhpmevent16",    any,    read_mhpmevent,
4507                              write_mhpmevent                           },
4508     [CSR_MHPMEVENT17]    = { "mhpmevent17",    any,    read_mhpmevent,
4509                              write_mhpmevent                           },
4510     [CSR_MHPMEVENT18]    = { "mhpmevent18",    any,    read_mhpmevent,
4511                              write_mhpmevent                           },
4512     [CSR_MHPMEVENT19]    = { "mhpmevent19",    any,    read_mhpmevent,
4513                              write_mhpmevent                           },
4514     [CSR_MHPMEVENT20]    = { "mhpmevent20",    any,    read_mhpmevent,
4515                              write_mhpmevent                           },
4516     [CSR_MHPMEVENT21]    = { "mhpmevent21",    any,    read_mhpmevent,
4517                              write_mhpmevent                           },
4518     [CSR_MHPMEVENT22]    = { "mhpmevent22",    any,    read_mhpmevent,
4519                              write_mhpmevent                           },
4520     [CSR_MHPMEVENT23]    = { "mhpmevent23",    any,    read_mhpmevent,
4521                              write_mhpmevent                           },
4522     [CSR_MHPMEVENT24]    = { "mhpmevent24",    any,    read_mhpmevent,
4523                              write_mhpmevent                           },
4524     [CSR_MHPMEVENT25]    = { "mhpmevent25",    any,    read_mhpmevent,
4525                              write_mhpmevent                           },
4526     [CSR_MHPMEVENT26]    = { "mhpmevent26",    any,    read_mhpmevent,
4527                              write_mhpmevent                           },
4528     [CSR_MHPMEVENT27]    = { "mhpmevent27",    any,    read_mhpmevent,
4529                              write_mhpmevent                           },
4530     [CSR_MHPMEVENT28]    = { "mhpmevent28",    any,    read_mhpmevent,
4531                              write_mhpmevent                           },
4532     [CSR_MHPMEVENT29]    = { "mhpmevent29",    any,    read_mhpmevent,
4533                              write_mhpmevent                           },
4534     [CSR_MHPMEVENT30]    = { "mhpmevent30",    any,    read_mhpmevent,
4535                              write_mhpmevent                           },
4536     [CSR_MHPMEVENT31]    = { "mhpmevent31",    any,    read_mhpmevent,
4537                              write_mhpmevent                           },
4538 
4539     [CSR_MHPMEVENT3H]    = { "mhpmevent3h",    sscofpmf,  read_mhpmeventh,
4540                              write_mhpmeventh,
4541                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4542     [CSR_MHPMEVENT4H]    = { "mhpmevent4h",    sscofpmf,  read_mhpmeventh,
4543                              write_mhpmeventh,
4544                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4545     [CSR_MHPMEVENT5H]    = { "mhpmevent5h",    sscofpmf,  read_mhpmeventh,
4546                              write_mhpmeventh,
4547                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4548     [CSR_MHPMEVENT6H]    = { "mhpmevent6h",    sscofpmf,  read_mhpmeventh,
4549                              write_mhpmeventh,
4550                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4551     [CSR_MHPMEVENT7H]    = { "mhpmevent7h",    sscofpmf,  read_mhpmeventh,
4552                              write_mhpmeventh,
4553                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4554     [CSR_MHPMEVENT8H]    = { "mhpmevent8h",    sscofpmf,  read_mhpmeventh,
4555                              write_mhpmeventh,
4556                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4557     [CSR_MHPMEVENT9H]    = { "mhpmevent9h",    sscofpmf,  read_mhpmeventh,
4558                              write_mhpmeventh,
4559                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4560     [CSR_MHPMEVENT10H]   = { "mhpmevent10h",    sscofpmf,  read_mhpmeventh,
4561                              write_mhpmeventh,
4562                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4563     [CSR_MHPMEVENT11H]   = { "mhpmevent11h",    sscofpmf,  read_mhpmeventh,
4564                              write_mhpmeventh,
4565                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4566     [CSR_MHPMEVENT12H]   = { "mhpmevent12h",    sscofpmf,  read_mhpmeventh,
4567                              write_mhpmeventh,
4568                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4569     [CSR_MHPMEVENT13H]   = { "mhpmevent13h",    sscofpmf,  read_mhpmeventh,
4570                              write_mhpmeventh,
4571                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4572     [CSR_MHPMEVENT14H]   = { "mhpmevent14h",    sscofpmf,  read_mhpmeventh,
4573                              write_mhpmeventh,
4574                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4575     [CSR_MHPMEVENT15H]   = { "mhpmevent15h",    sscofpmf,  read_mhpmeventh,
4576                              write_mhpmeventh,
4577                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4578     [CSR_MHPMEVENT16H]   = { "mhpmevent16h",    sscofpmf,  read_mhpmeventh,
4579                              write_mhpmeventh,
4580                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4581     [CSR_MHPMEVENT17H]   = { "mhpmevent17h",    sscofpmf,  read_mhpmeventh,
4582                              write_mhpmeventh,
4583                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4584     [CSR_MHPMEVENT18H]   = { "mhpmevent18h",    sscofpmf,  read_mhpmeventh,
4585                              write_mhpmeventh,
4586                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4587     [CSR_MHPMEVENT19H]   = { "mhpmevent19h",    sscofpmf,  read_mhpmeventh,
4588                              write_mhpmeventh,
4589                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4590     [CSR_MHPMEVENT20H]   = { "mhpmevent20h",    sscofpmf,  read_mhpmeventh,
4591                              write_mhpmeventh,
4592                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4593     [CSR_MHPMEVENT21H]   = { "mhpmevent21h",    sscofpmf,  read_mhpmeventh,
4594                              write_mhpmeventh,
4595                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4596     [CSR_MHPMEVENT22H]   = { "mhpmevent22h",    sscofpmf,  read_mhpmeventh,
4597                              write_mhpmeventh,
4598                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4599     [CSR_MHPMEVENT23H]   = { "mhpmevent23h",    sscofpmf,  read_mhpmeventh,
4600                              write_mhpmeventh,
4601                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4602     [CSR_MHPMEVENT24H]   = { "mhpmevent24h",    sscofpmf,  read_mhpmeventh,
4603                              write_mhpmeventh,
4604                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4605     [CSR_MHPMEVENT25H]   = { "mhpmevent25h",    sscofpmf,  read_mhpmeventh,
4606                              write_mhpmeventh,
4607                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4608     [CSR_MHPMEVENT26H]   = { "mhpmevent26h",    sscofpmf,  read_mhpmeventh,
4609                              write_mhpmeventh,
4610                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4611     [CSR_MHPMEVENT27H]   = { "mhpmevent27h",    sscofpmf,  read_mhpmeventh,
4612                              write_mhpmeventh,
4613                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4614     [CSR_MHPMEVENT28H]   = { "mhpmevent28h",    sscofpmf,  read_mhpmeventh,
4615                              write_mhpmeventh,
4616                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4617     [CSR_MHPMEVENT29H]   = { "mhpmevent29h",    sscofpmf,  read_mhpmeventh,
4618                              write_mhpmeventh,
4619                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4620     [CSR_MHPMEVENT30H]   = { "mhpmevent30h",    sscofpmf,  read_mhpmeventh,
4621                              write_mhpmeventh,
4622                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4623     [CSR_MHPMEVENT31H]   = { "mhpmevent31h",    sscofpmf,  read_mhpmeventh,
4624                              write_mhpmeventh,
4625                              .min_priv_ver = PRIV_VERSION_1_12_0        },
4626 
4627     [CSR_HPMCOUNTER3H]   = { "hpmcounter3h",   ctr32,  read_hpmcounterh },
4628     [CSR_HPMCOUNTER4H]   = { "hpmcounter4h",   ctr32,  read_hpmcounterh },
4629     [CSR_HPMCOUNTER5H]   = { "hpmcounter5h",   ctr32,  read_hpmcounterh },
4630     [CSR_HPMCOUNTER6H]   = { "hpmcounter6h",   ctr32,  read_hpmcounterh },
4631     [CSR_HPMCOUNTER7H]   = { "hpmcounter7h",   ctr32,  read_hpmcounterh },
4632     [CSR_HPMCOUNTER8H]   = { "hpmcounter8h",   ctr32,  read_hpmcounterh },
4633     [CSR_HPMCOUNTER9H]   = { "hpmcounter9h",   ctr32,  read_hpmcounterh },
4634     [CSR_HPMCOUNTER10H]  = { "hpmcounter10h",  ctr32,  read_hpmcounterh },
4635     [CSR_HPMCOUNTER11H]  = { "hpmcounter11h",  ctr32,  read_hpmcounterh },
4636     [CSR_HPMCOUNTER12H]  = { "hpmcounter12h",  ctr32,  read_hpmcounterh },
4637     [CSR_HPMCOUNTER13H]  = { "hpmcounter13h",  ctr32,  read_hpmcounterh },
4638     [CSR_HPMCOUNTER14H]  = { "hpmcounter14h",  ctr32,  read_hpmcounterh },
4639     [CSR_HPMCOUNTER15H]  = { "hpmcounter15h",  ctr32,  read_hpmcounterh },
4640     [CSR_HPMCOUNTER16H]  = { "hpmcounter16h",  ctr32,  read_hpmcounterh },
4641     [CSR_HPMCOUNTER17H]  = { "hpmcounter17h",  ctr32,  read_hpmcounterh },
4642     [CSR_HPMCOUNTER18H]  = { "hpmcounter18h",  ctr32,  read_hpmcounterh },
4643     [CSR_HPMCOUNTER19H]  = { "hpmcounter19h",  ctr32,  read_hpmcounterh },
4644     [CSR_HPMCOUNTER20H]  = { "hpmcounter20h",  ctr32,  read_hpmcounterh },
4645     [CSR_HPMCOUNTER21H]  = { "hpmcounter21h",  ctr32,  read_hpmcounterh },
4646     [CSR_HPMCOUNTER22H]  = { "hpmcounter22h",  ctr32,  read_hpmcounterh },
4647     [CSR_HPMCOUNTER23H]  = { "hpmcounter23h",  ctr32,  read_hpmcounterh },
4648     [CSR_HPMCOUNTER24H]  = { "hpmcounter24h",  ctr32,  read_hpmcounterh },
4649     [CSR_HPMCOUNTER25H]  = { "hpmcounter25h",  ctr32,  read_hpmcounterh },
4650     [CSR_HPMCOUNTER26H]  = { "hpmcounter26h",  ctr32,  read_hpmcounterh },
4651     [CSR_HPMCOUNTER27H]  = { "hpmcounter27h",  ctr32,  read_hpmcounterh },
4652     [CSR_HPMCOUNTER28H]  = { "hpmcounter28h",  ctr32,  read_hpmcounterh },
4653     [CSR_HPMCOUNTER29H]  = { "hpmcounter29h",  ctr32,  read_hpmcounterh },
4654     [CSR_HPMCOUNTER30H]  = { "hpmcounter30h",  ctr32,  read_hpmcounterh },
4655     [CSR_HPMCOUNTER31H]  = { "hpmcounter31h",  ctr32,  read_hpmcounterh },
4656 
4657     [CSR_MHPMCOUNTER3H]  = { "mhpmcounter3h",  mctr32,  read_hpmcounterh,
4658                              write_mhpmcounterh                         },
4659     [CSR_MHPMCOUNTER4H]  = { "mhpmcounter4h",  mctr32,  read_hpmcounterh,
4660                              write_mhpmcounterh                         },
4661     [CSR_MHPMCOUNTER5H]  = { "mhpmcounter5h",  mctr32,  read_hpmcounterh,
4662                              write_mhpmcounterh                         },
4663     [CSR_MHPMCOUNTER6H]  = { "mhpmcounter6h",  mctr32,  read_hpmcounterh,
4664                              write_mhpmcounterh                         },
4665     [CSR_MHPMCOUNTER7H]  = { "mhpmcounter7h",  mctr32,  read_hpmcounterh,
4666                              write_mhpmcounterh                         },
4667     [CSR_MHPMCOUNTER8H]  = { "mhpmcounter8h",  mctr32,  read_hpmcounterh,
4668                              write_mhpmcounterh                         },
4669     [CSR_MHPMCOUNTER9H]  = { "mhpmcounter9h",  mctr32,  read_hpmcounterh,
4670                              write_mhpmcounterh                         },
4671     [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", mctr32,  read_hpmcounterh,
4672                              write_mhpmcounterh                         },
4673     [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", mctr32,  read_hpmcounterh,
4674                              write_mhpmcounterh                         },
4675     [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", mctr32,  read_hpmcounterh,
4676                              write_mhpmcounterh                         },
4677     [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", mctr32,  read_hpmcounterh,
4678                              write_mhpmcounterh                         },
4679     [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", mctr32,  read_hpmcounterh,
4680                              write_mhpmcounterh                         },
4681     [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", mctr32,  read_hpmcounterh,
4682                              write_mhpmcounterh                         },
4683     [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", mctr32,  read_hpmcounterh,
4684                              write_mhpmcounterh                         },
4685     [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", mctr32,  read_hpmcounterh,
4686                              write_mhpmcounterh                         },
4687     [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", mctr32,  read_hpmcounterh,
4688                              write_mhpmcounterh                         },
4689     [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", mctr32,  read_hpmcounterh,
4690                              write_mhpmcounterh                         },
4691     [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", mctr32,  read_hpmcounterh,
4692                              write_mhpmcounterh                         },
4693     [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", mctr32,  read_hpmcounterh,
4694                              write_mhpmcounterh                         },
4695     [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", mctr32,  read_hpmcounterh,
4696                              write_mhpmcounterh                         },
4697     [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", mctr32,  read_hpmcounterh,
4698                              write_mhpmcounterh                         },
4699     [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", mctr32,  read_hpmcounterh,
4700                              write_mhpmcounterh                         },
4701     [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", mctr32,  read_hpmcounterh,
4702                              write_mhpmcounterh                         },
4703     [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", mctr32,  read_hpmcounterh,
4704                              write_mhpmcounterh                         },
4705     [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", mctr32,  read_hpmcounterh,
4706                              write_mhpmcounterh                         },
4707     [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", mctr32,  read_hpmcounterh,
4708                              write_mhpmcounterh                         },
4709     [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", mctr32,  read_hpmcounterh,
4710                              write_mhpmcounterh                         },
4711     [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", mctr32,  read_hpmcounterh,
4712                              write_mhpmcounterh                         },
4713     [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32,  read_hpmcounterh,
4714                              write_mhpmcounterh                         },
4715     [CSR_SCOUNTOVF]      = { "scountovf", sscofpmf,  read_scountovf,
4716                              .min_priv_ver = PRIV_VERSION_1_12_0 },
4717 
4718 #endif /* !CONFIG_USER_ONLY */
4719 };
4720