xref: /openbmc/qemu/target/riscv/csr.c (revision faf3b5d8)
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) {
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     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
1346 
1347     return RISCV_EXCP_NONE;
1348 }
1349 
1350 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno,
1351                                         Int128 *val)
1352 {
1353     *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128,
1354                                                       env->mstatus));
1355     return RISCV_EXCP_NONE;
1356 }
1357 
1358 static RISCVException read_misa_i128(CPURISCVState *env, int csrno,
1359                                      Int128 *val)
1360 {
1361     *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62);
1362     return RISCV_EXCP_NONE;
1363 }
1364 
1365 static RISCVException read_misa(CPURISCVState *env, int csrno,
1366                                 target_ulong *val)
1367 {
1368     target_ulong misa;
1369 
1370     switch (env->misa_mxl) {
1371     case MXL_RV32:
1372         misa = (target_ulong)MXL_RV32 << 30;
1373         break;
1374 #ifdef TARGET_RISCV64
1375     case MXL_RV64:
1376         misa = (target_ulong)MXL_RV64 << 62;
1377         break;
1378 #endif
1379     default:
1380         g_assert_not_reached();
1381     }
1382 
1383     *val = misa | env->misa_ext;
1384     return RISCV_EXCP_NONE;
1385 }
1386 
1387 static RISCVException write_misa(CPURISCVState *env, int csrno,
1388                                  target_ulong val)
1389 {
1390     RISCVCPU *cpu = env_archcpu(env);
1391     uint32_t orig_misa_ext = env->misa_ext;
1392     Error *local_err = NULL;
1393 
1394     if (!riscv_cpu_cfg(env)->misa_w) {
1395         /* drop write to misa */
1396         return RISCV_EXCP_NONE;
1397     }
1398 
1399     /* Mask extensions that are not supported by this hart */
1400     val &= env->misa_ext_mask;
1401 
1402     /*
1403      * Suppress 'C' if next instruction is not aligned
1404      * TODO: this should check next_pc
1405      */
1406     if ((val & RVC) && (GETPC() & ~3) != 0) {
1407         val &= ~RVC;
1408     }
1409 
1410     /* Disable RVG if any of its dependencies are disabled */
1411     if (!(val & RVI && val & RVM && val & RVA &&
1412           val & RVF && val & RVD)) {
1413         val &= ~RVG;
1414     }
1415 
1416     /* If nothing changed, do nothing. */
1417     if (val == env->misa_ext) {
1418         return RISCV_EXCP_NONE;
1419     }
1420 
1421     env->misa_ext = val;
1422     riscv_cpu_validate_set_extensions(cpu, &local_err);
1423     if (local_err != NULL) {
1424         /* Rollback on validation error */
1425         qemu_log_mask(LOG_GUEST_ERROR, "Unable to write MISA ext value "
1426                       "0x%x, keeping existing MISA ext 0x%x\n",
1427                       env->misa_ext, orig_misa_ext);
1428 
1429         env->misa_ext = orig_misa_ext;
1430 
1431         return RISCV_EXCP_NONE;
1432     }
1433 
1434     if (!(env->misa_ext & RVF)) {
1435         env->mstatus &= ~MSTATUS_FS;
1436     }
1437 
1438     /* flush translation cache */
1439     tb_flush(env_cpu(env));
1440     env->xl = riscv_cpu_mxl(env);
1441     return RISCV_EXCP_NONE;
1442 }
1443 
1444 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
1445                                    target_ulong *val)
1446 {
1447     *val = env->medeleg;
1448     return RISCV_EXCP_NONE;
1449 }
1450 
1451 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
1452                                     target_ulong val)
1453 {
1454     env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
1455     return RISCV_EXCP_NONE;
1456 }
1457 
1458 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno,
1459                                     uint64_t *ret_val,
1460                                     uint64_t new_val, uint64_t wr_mask)
1461 {
1462     uint64_t mask = wr_mask & delegable_ints;
1463 
1464     if (ret_val) {
1465         *ret_val = env->mideleg;
1466     }
1467 
1468     env->mideleg = (env->mideleg & ~mask) | (new_val & mask);
1469 
1470     if (riscv_has_ext(env, RVH)) {
1471         env->mideleg |= HS_MODE_INTERRUPTS;
1472     }
1473 
1474     return RISCV_EXCP_NONE;
1475 }
1476 
1477 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno,
1478                                   target_ulong *ret_val,
1479                                   target_ulong new_val, target_ulong wr_mask)
1480 {
1481     uint64_t rval;
1482     RISCVException ret;
1483 
1484     ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask);
1485     if (ret_val) {
1486         *ret_val = rval;
1487     }
1488 
1489     return ret;
1490 }
1491 
1492 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno,
1493                                    target_ulong *ret_val,
1494                                    target_ulong new_val,
1495                                    target_ulong wr_mask)
1496 {
1497     uint64_t rval;
1498     RISCVException ret;
1499 
1500     ret = rmw_mideleg64(env, csrno, &rval,
1501         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1502     if (ret_val) {
1503         *ret_val = rval >> 32;
1504     }
1505 
1506     return ret;
1507 }
1508 
1509 static RISCVException rmw_mie64(CPURISCVState *env, int csrno,
1510                                 uint64_t *ret_val,
1511                                 uint64_t new_val, uint64_t wr_mask)
1512 {
1513     uint64_t mask = wr_mask & all_ints;
1514 
1515     if (ret_val) {
1516         *ret_val = env->mie;
1517     }
1518 
1519     env->mie = (env->mie & ~mask) | (new_val & mask);
1520 
1521     if (!riscv_has_ext(env, RVH)) {
1522         env->mie &= ~((uint64_t)MIP_SGEIP);
1523     }
1524 
1525     return RISCV_EXCP_NONE;
1526 }
1527 
1528 static RISCVException rmw_mie(CPURISCVState *env, int csrno,
1529                               target_ulong *ret_val,
1530                               target_ulong new_val, target_ulong wr_mask)
1531 {
1532     uint64_t rval;
1533     RISCVException ret;
1534 
1535     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask);
1536     if (ret_val) {
1537         *ret_val = rval;
1538     }
1539 
1540     return ret;
1541 }
1542 
1543 static RISCVException rmw_mieh(CPURISCVState *env, int csrno,
1544                                target_ulong *ret_val,
1545                                target_ulong new_val, target_ulong wr_mask)
1546 {
1547     uint64_t rval;
1548     RISCVException ret;
1549 
1550     ret = rmw_mie64(env, csrno, &rval,
1551         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1552     if (ret_val) {
1553         *ret_val = rval >> 32;
1554     }
1555 
1556     return ret;
1557 }
1558 
1559 static int read_mtopi(CPURISCVState *env, int csrno, target_ulong *val)
1560 {
1561     int irq;
1562     uint8_t iprio;
1563 
1564     irq = riscv_cpu_mirq_pending(env);
1565     if (irq <= 0 || irq > 63) {
1566         *val = 0;
1567     } else {
1568         iprio = env->miprio[irq];
1569         if (!iprio) {
1570             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) {
1571                 iprio = IPRIO_MMAXIPRIO;
1572             }
1573         }
1574         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1575         *val |= iprio;
1576     }
1577 
1578     return RISCV_EXCP_NONE;
1579 }
1580 
1581 static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)
1582 {
1583     if (!env->virt_enabled) {
1584         return csrno;
1585     }
1586 
1587     switch (csrno) {
1588     case CSR_SISELECT:
1589         return CSR_VSISELECT;
1590     case CSR_SIREG:
1591         return CSR_VSIREG;
1592     case CSR_STOPEI:
1593         return CSR_VSTOPEI;
1594     default:
1595         return csrno;
1596     };
1597 }
1598 
1599 static int rmw_xiselect(CPURISCVState *env, int csrno, target_ulong *val,
1600                         target_ulong new_val, target_ulong wr_mask)
1601 {
1602     target_ulong *iselect;
1603 
1604     /* Translate CSR number for VS-mode */
1605     csrno = aia_xlate_vs_csrno(env, csrno);
1606 
1607     /* Find the iselect CSR based on CSR number */
1608     switch (csrno) {
1609     case CSR_MISELECT:
1610         iselect = &env->miselect;
1611         break;
1612     case CSR_SISELECT:
1613         iselect = &env->siselect;
1614         break;
1615     case CSR_VSISELECT:
1616         iselect = &env->vsiselect;
1617         break;
1618     default:
1619          return RISCV_EXCP_ILLEGAL_INST;
1620     };
1621 
1622     if (val) {
1623         *val = *iselect;
1624     }
1625 
1626     wr_mask &= ISELECT_MASK;
1627     if (wr_mask) {
1628         *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask);
1629     }
1630 
1631     return RISCV_EXCP_NONE;
1632 }
1633 
1634 static int rmw_iprio(target_ulong xlen,
1635                      target_ulong iselect, uint8_t *iprio,
1636                      target_ulong *val, target_ulong new_val,
1637                      target_ulong wr_mask, int ext_irq_no)
1638 {
1639     int i, firq, nirqs;
1640     target_ulong old_val;
1641 
1642     if (iselect < ISELECT_IPRIO0 || ISELECT_IPRIO15 < iselect) {
1643         return -EINVAL;
1644     }
1645     if (xlen != 32 && iselect & 0x1) {
1646         return -EINVAL;
1647     }
1648 
1649     nirqs = 4 * (xlen / 32);
1650     firq = ((iselect - ISELECT_IPRIO0) / (xlen / 32)) * (nirqs);
1651 
1652     old_val = 0;
1653     for (i = 0; i < nirqs; i++) {
1654         old_val |= ((target_ulong)iprio[firq + i]) << (IPRIO_IRQ_BITS * i);
1655     }
1656 
1657     if (val) {
1658         *val = old_val;
1659     }
1660 
1661     if (wr_mask) {
1662         new_val = (old_val & ~wr_mask) | (new_val & wr_mask);
1663         for (i = 0; i < nirqs; i++) {
1664             /*
1665              * M-level and S-level external IRQ priority always read-only
1666              * zero. This means default priority order is always preferred
1667              * for M-level and S-level external IRQs.
1668              */
1669             if ((firq + i) == ext_irq_no) {
1670                 continue;
1671             }
1672             iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) & 0xff;
1673         }
1674     }
1675 
1676     return 0;
1677 }
1678 
1679 static int rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val,
1680                      target_ulong new_val, target_ulong wr_mask)
1681 {
1682     bool virt;
1683     uint8_t *iprio;
1684     int ret = -EINVAL;
1685     target_ulong priv, isel, vgein;
1686 
1687     /* Translate CSR number for VS-mode */
1688     csrno = aia_xlate_vs_csrno(env, csrno);
1689 
1690     /* Decode register details from CSR number */
1691     virt = false;
1692     switch (csrno) {
1693     case CSR_MIREG:
1694         iprio = env->miprio;
1695         isel = env->miselect;
1696         priv = PRV_M;
1697         break;
1698     case CSR_SIREG:
1699         iprio = env->siprio;
1700         isel = env->siselect;
1701         priv = PRV_S;
1702         break;
1703     case CSR_VSIREG:
1704         iprio = env->hviprio;
1705         isel = env->vsiselect;
1706         priv = PRV_S;
1707         virt = true;
1708         break;
1709     default:
1710          goto done;
1711     };
1712 
1713     /* Find the selected guest interrupt file */
1714     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1715 
1716     if (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) {
1717         /* Local interrupt priority registers not available for VS-mode */
1718         if (!virt) {
1719             ret = rmw_iprio(riscv_cpu_mxl_bits(env),
1720                             isel, iprio, val, new_val, wr_mask,
1721                             (priv == PRV_M) ? IRQ_M_EXT : IRQ_S_EXT);
1722         }
1723     } else if (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST) {
1724         /* IMSIC registers only available when machine implements it. */
1725         if (env->aia_ireg_rmw_fn[priv]) {
1726             /* Selected guest interrupt file should not be zero */
1727             if (virt && (!vgein || env->geilen < vgein)) {
1728                 goto done;
1729             }
1730             /* Call machine specific IMSIC register emulation */
1731             ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1732                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
1733                                                   riscv_cpu_mxl_bits(env)),
1734                                     val, new_val, wr_mask);
1735         }
1736     }
1737 
1738 done:
1739     if (ret) {
1740         return (env->virt_enabled && virt) ?
1741                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1742     }
1743     return RISCV_EXCP_NONE;
1744 }
1745 
1746 static int rmw_xtopei(CPURISCVState *env, int csrno, target_ulong *val,
1747                       target_ulong new_val, target_ulong wr_mask)
1748 {
1749     bool virt;
1750     int ret = -EINVAL;
1751     target_ulong priv, vgein;
1752 
1753     /* Translate CSR number for VS-mode */
1754     csrno = aia_xlate_vs_csrno(env, csrno);
1755 
1756     /* Decode register details from CSR number */
1757     virt = false;
1758     switch (csrno) {
1759     case CSR_MTOPEI:
1760         priv = PRV_M;
1761         break;
1762     case CSR_STOPEI:
1763         priv = PRV_S;
1764         break;
1765     case CSR_VSTOPEI:
1766         priv = PRV_S;
1767         virt = true;
1768         break;
1769     default:
1770         goto done;
1771     };
1772 
1773     /* IMSIC CSRs only available when machine implements IMSIC. */
1774     if (!env->aia_ireg_rmw_fn[priv]) {
1775         goto done;
1776     }
1777 
1778     /* Find the selected guest interrupt file */
1779     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1780 
1781     /* Selected guest interrupt file should be valid */
1782     if (virt && (!vgein || env->geilen < vgein)) {
1783         goto done;
1784     }
1785 
1786     /* Call machine specific IMSIC register emulation for TOPEI */
1787     ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1788                     AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
1789                                   riscv_cpu_mxl_bits(env)),
1790                     val, new_val, wr_mask);
1791 
1792 done:
1793     if (ret) {
1794         return (env->virt_enabled && virt) ?
1795                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1796     }
1797     return RISCV_EXCP_NONE;
1798 }
1799 
1800 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
1801                                  target_ulong *val)
1802 {
1803     *val = env->mtvec;
1804     return RISCV_EXCP_NONE;
1805 }
1806 
1807 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
1808                                   target_ulong val)
1809 {
1810     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
1811     if ((val & 3) < 2) {
1812         env->mtvec = val;
1813     } else {
1814         qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
1815     }
1816     return RISCV_EXCP_NONE;
1817 }
1818 
1819 static RISCVException read_mcountinhibit(CPURISCVState *env, int csrno,
1820                                          target_ulong *val)
1821 {
1822     *val = env->mcountinhibit;
1823     return RISCV_EXCP_NONE;
1824 }
1825 
1826 static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno,
1827                                           target_ulong val)
1828 {
1829     int cidx;
1830     PMUCTRState *counter;
1831 
1832     env->mcountinhibit = val;
1833 
1834     /* Check if any other counter is also monitoring cycles/instructions */
1835     for (cidx = 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) {
1836         if (!get_field(env->mcountinhibit, BIT(cidx))) {
1837             counter = &env->pmu_ctrs[cidx];
1838             counter->started = true;
1839         }
1840     }
1841 
1842     return RISCV_EXCP_NONE;
1843 }
1844 
1845 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
1846                                       target_ulong *val)
1847 {
1848     *val = env->mcounteren;
1849     return RISCV_EXCP_NONE;
1850 }
1851 
1852 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
1853                                        target_ulong val)
1854 {
1855     env->mcounteren = val;
1856     return RISCV_EXCP_NONE;
1857 }
1858 
1859 /* Machine Trap Handling */
1860 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno,
1861                                          Int128 *val)
1862 {
1863     *val = int128_make128(env->mscratch, env->mscratchh);
1864     return RISCV_EXCP_NONE;
1865 }
1866 
1867 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno,
1868                                           Int128 val)
1869 {
1870     env->mscratch = int128_getlo(val);
1871     env->mscratchh = int128_gethi(val);
1872     return RISCV_EXCP_NONE;
1873 }
1874 
1875 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
1876                                     target_ulong *val)
1877 {
1878     *val = env->mscratch;
1879     return RISCV_EXCP_NONE;
1880 }
1881 
1882 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
1883                                      target_ulong val)
1884 {
1885     env->mscratch = val;
1886     return RISCV_EXCP_NONE;
1887 }
1888 
1889 static RISCVException read_mepc(CPURISCVState *env, int csrno,
1890                                 target_ulong *val)
1891 {
1892     *val = env->mepc;
1893     return RISCV_EXCP_NONE;
1894 }
1895 
1896 static RISCVException write_mepc(CPURISCVState *env, int csrno,
1897                                  target_ulong val)
1898 {
1899     env->mepc = val;
1900     return RISCV_EXCP_NONE;
1901 }
1902 
1903 static RISCVException read_mcause(CPURISCVState *env, int csrno,
1904                                   target_ulong *val)
1905 {
1906     *val = env->mcause;
1907     return RISCV_EXCP_NONE;
1908 }
1909 
1910 static RISCVException write_mcause(CPURISCVState *env, int csrno,
1911                                    target_ulong val)
1912 {
1913     env->mcause = val;
1914     return RISCV_EXCP_NONE;
1915 }
1916 
1917 static RISCVException read_mtval(CPURISCVState *env, int csrno,
1918                                  target_ulong *val)
1919 {
1920     *val = env->mtval;
1921     return RISCV_EXCP_NONE;
1922 }
1923 
1924 static RISCVException write_mtval(CPURISCVState *env, int csrno,
1925                                   target_ulong val)
1926 {
1927     env->mtval = val;
1928     return RISCV_EXCP_NONE;
1929 }
1930 
1931 /* Execution environment configuration setup */
1932 static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
1933                                    target_ulong *val)
1934 {
1935     *val = env->menvcfg;
1936     return RISCV_EXCP_NONE;
1937 }
1938 
1939 static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
1940                                     target_ulong val)
1941 {
1942     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
1943     uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE;
1944 
1945     if (riscv_cpu_mxl(env) == MXL_RV64) {
1946         mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
1947                 (cfg->ext_sstc ? MENVCFG_STCE : 0) |
1948                 (cfg->ext_svadu ? MENVCFG_HADE : 0);
1949     }
1950     env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
1951 
1952     return RISCV_EXCP_NONE;
1953 }
1954 
1955 static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
1956                                     target_ulong *val)
1957 {
1958     *val = env->menvcfg >> 32;
1959     return RISCV_EXCP_NONE;
1960 }
1961 
1962 static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
1963                                      target_ulong val)
1964 {
1965     const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
1966     uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
1967                     (cfg->ext_sstc ? MENVCFG_STCE : 0) |
1968                     (cfg->ext_svadu ? MENVCFG_HADE : 0);
1969     uint64_t valh = (uint64_t)val << 32;
1970 
1971     env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);
1972 
1973     return RISCV_EXCP_NONE;
1974 }
1975 
1976 static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
1977                                    target_ulong *val)
1978 {
1979     RISCVException ret;
1980 
1981     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
1982     if (ret != RISCV_EXCP_NONE) {
1983         return ret;
1984     }
1985 
1986     *val = env->senvcfg;
1987     return RISCV_EXCP_NONE;
1988 }
1989 
1990 static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
1991                                     target_ulong val)
1992 {
1993     uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE;
1994     RISCVException ret;
1995 
1996     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
1997     if (ret != RISCV_EXCP_NONE) {
1998         return ret;
1999     }
2000 
2001     env->senvcfg = (env->senvcfg & ~mask) | (val & mask);
2002     return RISCV_EXCP_NONE;
2003 }
2004 
2005 static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
2006                                    target_ulong *val)
2007 {
2008     RISCVException ret;
2009 
2010     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2011     if (ret != RISCV_EXCP_NONE) {
2012         return ret;
2013     }
2014 
2015     /*
2016      * henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0
2017      * henvcfg.stce is read_only 0 when menvcfg.stce = 0
2018      * henvcfg.hade is read_only 0 when menvcfg.hade = 0
2019      */
2020     *val = env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_HADE) |
2021                            env->menvcfg);
2022     return RISCV_EXCP_NONE;
2023 }
2024 
2025 static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
2026                                     target_ulong val)
2027 {
2028     uint64_t mask = HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCFG_CBZE;
2029     RISCVException ret;
2030 
2031     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2032     if (ret != RISCV_EXCP_NONE) {
2033         return ret;
2034     }
2035 
2036     if (riscv_cpu_mxl(env) == MXL_RV64) {
2037         mask |= env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_HADE);
2038     }
2039 
2040     env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
2041 
2042     return RISCV_EXCP_NONE;
2043 }
2044 
2045 static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
2046                                     target_ulong *val)
2047 {
2048     RISCVException ret;
2049 
2050     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2051     if (ret != RISCV_EXCP_NONE) {
2052         return ret;
2053     }
2054 
2055     *val = (env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_HADE) |
2056                             env->menvcfg)) >> 32;
2057     return RISCV_EXCP_NONE;
2058 }
2059 
2060 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
2061                                      target_ulong val)
2062 {
2063     uint64_t mask = env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE |
2064                                     HENVCFG_HADE);
2065     uint64_t valh = (uint64_t)val << 32;
2066     RISCVException ret;
2067 
2068     ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG);
2069     if (ret != RISCV_EXCP_NONE) {
2070         return ret;
2071     }
2072 
2073     env->henvcfg = (env->henvcfg & ~mask) | (valh & mask);
2074     return RISCV_EXCP_NONE;
2075 }
2076 
2077 static RISCVException read_mstateen(CPURISCVState *env, int csrno,
2078                                     target_ulong *val)
2079 {
2080     *val = env->mstateen[csrno - CSR_MSTATEEN0];
2081 
2082     return RISCV_EXCP_NONE;
2083 }
2084 
2085 static RISCVException write_mstateen(CPURISCVState *env, int csrno,
2086                                      uint64_t wr_mask, target_ulong new_val)
2087 {
2088     uint64_t *reg;
2089 
2090     reg = &env->mstateen[csrno - CSR_MSTATEEN0];
2091     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2092 
2093     return RISCV_EXCP_NONE;
2094 }
2095 
2096 static RISCVException write_mstateen0(CPURISCVState *env, int csrno,
2097                                       target_ulong new_val)
2098 {
2099     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2100 
2101     return write_mstateen(env, csrno, wr_mask, new_val);
2102 }
2103 
2104 static RISCVException write_mstateen_1_3(CPURISCVState *env, int csrno,
2105                                          target_ulong new_val)
2106 {
2107     return write_mstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2108 }
2109 
2110 static RISCVException read_mstateenh(CPURISCVState *env, int csrno,
2111                                      target_ulong *val)
2112 {
2113     *val = env->mstateen[csrno - CSR_MSTATEEN0H] >> 32;
2114 
2115     return RISCV_EXCP_NONE;
2116 }
2117 
2118 static RISCVException write_mstateenh(CPURISCVState *env, int csrno,
2119                                       uint64_t wr_mask, target_ulong new_val)
2120 {
2121     uint64_t *reg, val;
2122 
2123     reg = &env->mstateen[csrno - CSR_MSTATEEN0H];
2124     val = (uint64_t)new_val << 32;
2125     val |= *reg & 0xFFFFFFFF;
2126     *reg = (*reg & ~wr_mask) | (val & wr_mask);
2127 
2128     return RISCV_EXCP_NONE;
2129 }
2130 
2131 static RISCVException write_mstateen0h(CPURISCVState *env, int csrno,
2132                                        target_ulong new_val)
2133 {
2134     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2135 
2136     return write_mstateenh(env, csrno, wr_mask, new_val);
2137 }
2138 
2139 static RISCVException write_mstateenh_1_3(CPURISCVState *env, int csrno,
2140                                           target_ulong new_val)
2141 {
2142     return write_mstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2143 }
2144 
2145 static RISCVException read_hstateen(CPURISCVState *env, int csrno,
2146                                     target_ulong *val)
2147 {
2148     int index = csrno - CSR_HSTATEEN0;
2149 
2150     *val = env->hstateen[index] & env->mstateen[index];
2151 
2152     return RISCV_EXCP_NONE;
2153 }
2154 
2155 static RISCVException write_hstateen(CPURISCVState *env, int csrno,
2156                                      uint64_t mask, target_ulong new_val)
2157 {
2158     int index = csrno - CSR_HSTATEEN0;
2159     uint64_t *reg, wr_mask;
2160 
2161     reg = &env->hstateen[index];
2162     wr_mask = env->mstateen[index] & mask;
2163     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2164 
2165     return RISCV_EXCP_NONE;
2166 }
2167 
2168 static RISCVException write_hstateen0(CPURISCVState *env, int csrno,
2169                                       target_ulong new_val)
2170 {
2171     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2172 
2173     return write_hstateen(env, csrno, wr_mask, new_val);
2174 }
2175 
2176 static RISCVException write_hstateen_1_3(CPURISCVState *env, int csrno,
2177                                          target_ulong new_val)
2178 {
2179     return write_hstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2180 }
2181 
2182 static RISCVException read_hstateenh(CPURISCVState *env, int csrno,
2183                                      target_ulong *val)
2184 {
2185     int index = csrno - CSR_HSTATEEN0H;
2186 
2187     *val = (env->hstateen[index] >> 32) & (env->mstateen[index] >> 32);
2188 
2189     return RISCV_EXCP_NONE;
2190 }
2191 
2192 static RISCVException write_hstateenh(CPURISCVState *env, int csrno,
2193                                       uint64_t mask, target_ulong new_val)
2194 {
2195     int index = csrno - CSR_HSTATEEN0H;
2196     uint64_t *reg, wr_mask, val;
2197 
2198     reg = &env->hstateen[index];
2199     val = (uint64_t)new_val << 32;
2200     val |= *reg & 0xFFFFFFFF;
2201     wr_mask = env->mstateen[index] & mask;
2202     *reg = (*reg & ~wr_mask) | (val & wr_mask);
2203 
2204     return RISCV_EXCP_NONE;
2205 }
2206 
2207 static RISCVException write_hstateen0h(CPURISCVState *env, int csrno,
2208                                        target_ulong new_val)
2209 {
2210     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2211 
2212     return write_hstateenh(env, csrno, wr_mask, new_val);
2213 }
2214 
2215 static RISCVException write_hstateenh_1_3(CPURISCVState *env, int csrno,
2216                                           target_ulong new_val)
2217 {
2218     return write_hstateenh(env, csrno, SMSTATEEN_STATEEN, new_val);
2219 }
2220 
2221 static RISCVException read_sstateen(CPURISCVState *env, int csrno,
2222                                     target_ulong *val)
2223 {
2224     bool virt = env->virt_enabled;
2225     int index = csrno - CSR_SSTATEEN0;
2226 
2227     *val = env->sstateen[index] & env->mstateen[index];
2228     if (virt) {
2229         *val &= env->hstateen[index];
2230     }
2231 
2232     return RISCV_EXCP_NONE;
2233 }
2234 
2235 static RISCVException write_sstateen(CPURISCVState *env, int csrno,
2236                                      uint64_t mask, target_ulong new_val)
2237 {
2238     bool virt = env->virt_enabled;
2239     int index = csrno - CSR_SSTATEEN0;
2240     uint64_t wr_mask;
2241     uint64_t *reg;
2242 
2243     wr_mask = env->mstateen[index] & mask;
2244     if (virt) {
2245         wr_mask &= env->hstateen[index];
2246     }
2247 
2248     reg = &env->sstateen[index];
2249     *reg = (*reg & ~wr_mask) | (new_val & wr_mask);
2250 
2251     return RISCV_EXCP_NONE;
2252 }
2253 
2254 static RISCVException write_sstateen0(CPURISCVState *env, int csrno,
2255                                       target_ulong new_val)
2256 {
2257     uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
2258 
2259     return write_sstateen(env, csrno, wr_mask, new_val);
2260 }
2261 
2262 static RISCVException write_sstateen_1_3(CPURISCVState *env, int csrno,
2263                                       target_ulong new_val)
2264 {
2265     return write_sstateen(env, csrno, SMSTATEEN_STATEEN, new_val);
2266 }
2267 
2268 static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
2269                                 uint64_t *ret_val,
2270                                 uint64_t new_val, uint64_t wr_mask)
2271 {
2272     uint64_t old_mip, mask = wr_mask & delegable_ints;
2273     uint32_t gin;
2274 
2275     if (mask & MIP_SEIP) {
2276         env->software_seip = new_val & MIP_SEIP;
2277         new_val |= env->external_seip * MIP_SEIP;
2278     }
2279 
2280     if (riscv_cpu_cfg(env)->ext_sstc && (env->priv == PRV_M) &&
2281         get_field(env->menvcfg, MENVCFG_STCE)) {
2282         /* sstc extension forbids STIP & VSTIP to be writeable in mip */
2283         mask = mask & ~(MIP_STIP | MIP_VSTIP);
2284     }
2285 
2286     if (mask) {
2287         old_mip = riscv_cpu_update_mip(env, mask, (new_val & mask));
2288     } else {
2289         old_mip = env->mip;
2290     }
2291 
2292     if (csrno != CSR_HVIP) {
2293         gin = get_field(env->hstatus, HSTATUS_VGEIN);
2294         old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0;
2295         old_mip |= env->vstime_irq ? MIP_VSTIP : 0;
2296     }
2297 
2298     if (ret_val) {
2299         *ret_val = old_mip;
2300     }
2301 
2302     return RISCV_EXCP_NONE;
2303 }
2304 
2305 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
2306                               target_ulong *ret_val,
2307                               target_ulong new_val, target_ulong wr_mask)
2308 {
2309     uint64_t rval;
2310     RISCVException ret;
2311 
2312     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask);
2313     if (ret_val) {
2314         *ret_val = rval;
2315     }
2316 
2317     return ret;
2318 }
2319 
2320 static RISCVException rmw_miph(CPURISCVState *env, int csrno,
2321                                target_ulong *ret_val,
2322                                target_ulong new_val, target_ulong wr_mask)
2323 {
2324     uint64_t rval;
2325     RISCVException ret;
2326 
2327     ret = rmw_mip64(env, csrno, &rval,
2328         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2329     if (ret_val) {
2330         *ret_val = rval >> 32;
2331     }
2332 
2333     return ret;
2334 }
2335 
2336 /* Supervisor Trap Setup */
2337 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,
2338                                         Int128 *val)
2339 {
2340     uint64_t mask = sstatus_v1_10_mask;
2341     uint64_t sstatus = env->mstatus & mask;
2342     if (env->xl != MXL_RV32 || env->debugger) {
2343         mask |= SSTATUS64_UXL;
2344     }
2345 
2346     *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus));
2347     return RISCV_EXCP_NONE;
2348 }
2349 
2350 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
2351                                    target_ulong *val)
2352 {
2353     target_ulong mask = (sstatus_v1_10_mask);
2354     if (env->xl != MXL_RV32 || env->debugger) {
2355         mask |= SSTATUS64_UXL;
2356     }
2357     /* TODO: Use SXL not MXL. */
2358     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
2359     return RISCV_EXCP_NONE;
2360 }
2361 
2362 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
2363                                     target_ulong val)
2364 {
2365     target_ulong mask = (sstatus_v1_10_mask);
2366 
2367     if (env->xl != MXL_RV32 || env->debugger) {
2368         if ((val & SSTATUS64_UXL) != 0) {
2369             mask |= SSTATUS64_UXL;
2370         }
2371     }
2372     target_ulong newval = (env->mstatus & ~mask) | (val & mask);
2373     return write_mstatus(env, CSR_MSTATUS, newval);
2374 }
2375 
2376 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno,
2377                                  uint64_t *ret_val,
2378                                  uint64_t new_val, uint64_t wr_mask)
2379 {
2380     RISCVException ret;
2381     uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS;
2382 
2383     /* Bring VS-level bits to correct position */
2384     new_val = (new_val & (VS_MODE_INTERRUPTS >> 1)) << 1;
2385     wr_mask = (wr_mask & (VS_MODE_INTERRUPTS >> 1)) << 1;
2386 
2387     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & mask);
2388     if (ret_val) {
2389         *ret_val = (rval & mask) >> 1;
2390     }
2391 
2392     return ret;
2393 }
2394 
2395 static RISCVException rmw_vsie(CPURISCVState *env, int csrno,
2396                                target_ulong *ret_val,
2397                                target_ulong new_val, target_ulong wr_mask)
2398 {
2399     uint64_t rval;
2400     RISCVException ret;
2401 
2402     ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask);
2403     if (ret_val) {
2404         *ret_val = rval;
2405     }
2406 
2407     return ret;
2408 }
2409 
2410 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno,
2411                                 target_ulong *ret_val,
2412                                 target_ulong new_val, target_ulong wr_mask)
2413 {
2414     uint64_t rval;
2415     RISCVException ret;
2416 
2417     ret = rmw_vsie64(env, csrno, &rval,
2418         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2419     if (ret_val) {
2420         *ret_val = rval >> 32;
2421     }
2422 
2423     return ret;
2424 }
2425 
2426 static RISCVException rmw_sie64(CPURISCVState *env, int csrno,
2427                                 uint64_t *ret_val,
2428                                 uint64_t new_val, uint64_t wr_mask)
2429 {
2430     RISCVException ret;
2431     uint64_t mask = env->mideleg & S_MODE_INTERRUPTS;
2432 
2433     if (env->virt_enabled) {
2434         if (env->hvictl & HVICTL_VTI) {
2435             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2436         }
2437         ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask);
2438     } else {
2439         ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & mask);
2440     }
2441 
2442     if (ret_val) {
2443         *ret_val &= mask;
2444     }
2445 
2446     return ret;
2447 }
2448 
2449 static RISCVException rmw_sie(CPURISCVState *env, int csrno,
2450                               target_ulong *ret_val,
2451                               target_ulong new_val, target_ulong wr_mask)
2452 {
2453     uint64_t rval;
2454     RISCVException ret;
2455 
2456     ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask);
2457     if (ret == RISCV_EXCP_NONE && ret_val) {
2458         *ret_val = rval;
2459     }
2460 
2461     return ret;
2462 }
2463 
2464 static RISCVException rmw_sieh(CPURISCVState *env, int csrno,
2465                                target_ulong *ret_val,
2466                                target_ulong new_val, target_ulong wr_mask)
2467 {
2468     uint64_t rval;
2469     RISCVException ret;
2470 
2471     ret = rmw_sie64(env, csrno, &rval,
2472         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2473     if (ret_val) {
2474         *ret_val = rval >> 32;
2475     }
2476 
2477     return ret;
2478 }
2479 
2480 static RISCVException read_stvec(CPURISCVState *env, int csrno,
2481                                  target_ulong *val)
2482 {
2483     *val = env->stvec;
2484     return RISCV_EXCP_NONE;
2485 }
2486 
2487 static RISCVException write_stvec(CPURISCVState *env, int csrno,
2488                                   target_ulong val)
2489 {
2490     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
2491     if ((val & 3) < 2) {
2492         env->stvec = val;
2493     } else {
2494         qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
2495     }
2496     return RISCV_EXCP_NONE;
2497 }
2498 
2499 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
2500                                       target_ulong *val)
2501 {
2502     *val = env->scounteren;
2503     return RISCV_EXCP_NONE;
2504 }
2505 
2506 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
2507                                        target_ulong val)
2508 {
2509     env->scounteren = val;
2510     return RISCV_EXCP_NONE;
2511 }
2512 
2513 /* Supervisor Trap Handling */
2514 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno,
2515                                          Int128 *val)
2516 {
2517     *val = int128_make128(env->sscratch, env->sscratchh);
2518     return RISCV_EXCP_NONE;
2519 }
2520 
2521 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno,
2522                                           Int128 val)
2523 {
2524     env->sscratch = int128_getlo(val);
2525     env->sscratchh = int128_gethi(val);
2526     return RISCV_EXCP_NONE;
2527 }
2528 
2529 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
2530                                     target_ulong *val)
2531 {
2532     *val = env->sscratch;
2533     return RISCV_EXCP_NONE;
2534 }
2535 
2536 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
2537                                      target_ulong val)
2538 {
2539     env->sscratch = val;
2540     return RISCV_EXCP_NONE;
2541 }
2542 
2543 static RISCVException read_sepc(CPURISCVState *env, int csrno,
2544                                 target_ulong *val)
2545 {
2546     *val = env->sepc;
2547     return RISCV_EXCP_NONE;
2548 }
2549 
2550 static RISCVException write_sepc(CPURISCVState *env, int csrno,
2551                                  target_ulong val)
2552 {
2553     env->sepc = val;
2554     return RISCV_EXCP_NONE;
2555 }
2556 
2557 static RISCVException read_scause(CPURISCVState *env, int csrno,
2558                                   target_ulong *val)
2559 {
2560     *val = env->scause;
2561     return RISCV_EXCP_NONE;
2562 }
2563 
2564 static RISCVException write_scause(CPURISCVState *env, int csrno,
2565                                    target_ulong val)
2566 {
2567     env->scause = val;
2568     return RISCV_EXCP_NONE;
2569 }
2570 
2571 static RISCVException read_stval(CPURISCVState *env, int csrno,
2572                                  target_ulong *val)
2573 {
2574     *val = env->stval;
2575     return RISCV_EXCP_NONE;
2576 }
2577 
2578 static RISCVException write_stval(CPURISCVState *env, int csrno,
2579                                   target_ulong val)
2580 {
2581     env->stval = val;
2582     return RISCV_EXCP_NONE;
2583 }
2584 
2585 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno,
2586                                  uint64_t *ret_val,
2587                                  uint64_t new_val, uint64_t wr_mask)
2588 {
2589     RISCVException ret;
2590     uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS;
2591 
2592     /* Bring VS-level bits to correct position */
2593     new_val = (new_val & (VS_MODE_INTERRUPTS >> 1)) << 1;
2594     wr_mask = (wr_mask & (VS_MODE_INTERRUPTS >> 1)) << 1;
2595 
2596     ret = rmw_mip64(env, csrno, &rval, new_val,
2597                     wr_mask & mask & vsip_writable_mask);
2598     if (ret_val) {
2599         *ret_val = (rval & mask) >> 1;
2600     }
2601 
2602     return ret;
2603 }
2604 
2605 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
2606                                target_ulong *ret_val,
2607                                target_ulong new_val, target_ulong wr_mask)
2608 {
2609     uint64_t rval;
2610     RISCVException ret;
2611 
2612     ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask);
2613     if (ret_val) {
2614         *ret_val = rval;
2615     }
2616 
2617     return ret;
2618 }
2619 
2620 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno,
2621                                 target_ulong *ret_val,
2622                                 target_ulong new_val, target_ulong wr_mask)
2623 {
2624     uint64_t rval;
2625     RISCVException ret;
2626 
2627     ret = rmw_vsip64(env, csrno, &rval,
2628         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2629     if (ret_val) {
2630         *ret_val = rval >> 32;
2631     }
2632 
2633     return ret;
2634 }
2635 
2636 static RISCVException rmw_sip64(CPURISCVState *env, int csrno,
2637                                 uint64_t *ret_val,
2638                                 uint64_t new_val, uint64_t wr_mask)
2639 {
2640     RISCVException ret;
2641     uint64_t mask = env->mideleg & sip_writable_mask;
2642 
2643     if (env->virt_enabled) {
2644         if (env->hvictl & HVICTL_VTI) {
2645             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2646         }
2647         ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask);
2648     } else {
2649         ret = rmw_mip64(env, csrno, ret_val, new_val, wr_mask & mask);
2650     }
2651 
2652     if (ret_val) {
2653         *ret_val &= env->mideleg & S_MODE_INTERRUPTS;
2654     }
2655 
2656     return ret;
2657 }
2658 
2659 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
2660                               target_ulong *ret_val,
2661                               target_ulong new_val, target_ulong wr_mask)
2662 {
2663     uint64_t rval;
2664     RISCVException ret;
2665 
2666     ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask);
2667     if (ret_val) {
2668         *ret_val = rval;
2669     }
2670 
2671     return ret;
2672 }
2673 
2674 static RISCVException rmw_siph(CPURISCVState *env, int csrno,
2675                                target_ulong *ret_val,
2676                                target_ulong new_val, target_ulong wr_mask)
2677 {
2678     uint64_t rval;
2679     RISCVException ret;
2680 
2681     ret = rmw_sip64(env, csrno, &rval,
2682         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2683     if (ret_val) {
2684         *ret_val = rval >> 32;
2685     }
2686 
2687     return ret;
2688 }
2689 
2690 /* Supervisor Protection and Translation */
2691 static RISCVException read_satp(CPURISCVState *env, int csrno,
2692                                 target_ulong *val)
2693 {
2694     if (!riscv_cpu_cfg(env)->mmu) {
2695         *val = 0;
2696         return RISCV_EXCP_NONE;
2697     }
2698     *val = env->satp;
2699     return RISCV_EXCP_NONE;
2700 }
2701 
2702 static RISCVException write_satp(CPURISCVState *env, int csrno,
2703                                  target_ulong val)
2704 {
2705     target_ulong mask;
2706     bool vm;
2707 
2708     if (!riscv_cpu_cfg(env)->mmu) {
2709         return RISCV_EXCP_NONE;
2710     }
2711 
2712     if (riscv_cpu_mxl(env) == MXL_RV32) {
2713         vm = validate_vm(env, get_field(val, SATP32_MODE));
2714         mask = (val ^ env->satp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
2715     } else {
2716         vm = validate_vm(env, get_field(val, SATP64_MODE));
2717         mask = (val ^ env->satp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
2718     }
2719 
2720     if (vm && mask) {
2721         /*
2722          * The ISA defines SATP.MODE=Bare as "no translation", but we still
2723          * pass these through QEMU's TLB emulation as it improves
2724          * performance.  Flushing the TLB on SATP writes with paging
2725          * enabled avoids leaking those invalid cached mappings.
2726          */
2727         tlb_flush(env_cpu(env));
2728         env->satp = val;
2729     }
2730     return RISCV_EXCP_NONE;
2731 }
2732 
2733 static int read_vstopi(CPURISCVState *env, int csrno, target_ulong *val)
2734 {
2735     int irq, ret;
2736     target_ulong topei;
2737     uint64_t vseip, vsgein;
2738     uint32_t iid, iprio, hviid, hviprio, gein;
2739     uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS];
2740 
2741     gein = get_field(env->hstatus, HSTATUS_VGEIN);
2742     hviid = get_field(env->hvictl, HVICTL_IID);
2743     hviprio = get_field(env->hvictl, HVICTL_IPRIO);
2744 
2745     if (gein) {
2746         vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0;
2747         vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP;
2748         if (gein <= env->geilen && vseip) {
2749             siid[scount] = IRQ_S_EXT;
2750             siprio[scount] = IPRIO_MMAXIPRIO + 1;
2751             if (env->aia_ireg_rmw_fn[PRV_S]) {
2752                 /*
2753                  * Call machine specific IMSIC register emulation for
2754                  * reading TOPEI.
2755                  */
2756                 ret = env->aia_ireg_rmw_fn[PRV_S](
2757                         env->aia_ireg_rmw_fn_arg[PRV_S],
2758                         AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein,
2759                                       riscv_cpu_mxl_bits(env)),
2760                         &topei, 0, 0);
2761                 if (!ret && topei) {
2762                     siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK;
2763                 }
2764             }
2765             scount++;
2766         }
2767     } else {
2768         if (hviid == IRQ_S_EXT && hviprio) {
2769             siid[scount] = IRQ_S_EXT;
2770             siprio[scount] = hviprio;
2771             scount++;
2772         }
2773     }
2774 
2775     if (env->hvictl & HVICTL_VTI) {
2776         if (hviid != IRQ_S_EXT) {
2777             siid[scount] = hviid;
2778             siprio[scount] = hviprio;
2779             scount++;
2780         }
2781     } else {
2782         irq = riscv_cpu_vsirq_pending(env);
2783         if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) {
2784             siid[scount] = irq;
2785             siprio[scount] = env->hviprio[irq];
2786             scount++;
2787         }
2788     }
2789 
2790     iid = 0;
2791     iprio = UINT_MAX;
2792     for (s = 0; s < scount; s++) {
2793         if (siprio[s] < iprio) {
2794             iid = siid[s];
2795             iprio = siprio[s];
2796         }
2797     }
2798 
2799     if (iid) {
2800         if (env->hvictl & HVICTL_IPRIOM) {
2801             if (iprio > IPRIO_MMAXIPRIO) {
2802                 iprio = IPRIO_MMAXIPRIO;
2803             }
2804             if (!iprio) {
2805                 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) {
2806                     iprio = IPRIO_MMAXIPRIO;
2807                 }
2808             }
2809         } else {
2810             iprio = 1;
2811         }
2812     } else {
2813         iprio = 0;
2814     }
2815 
2816     *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT;
2817     *val |= iprio;
2818     return RISCV_EXCP_NONE;
2819 }
2820 
2821 static int read_stopi(CPURISCVState *env, int csrno, target_ulong *val)
2822 {
2823     int irq;
2824     uint8_t iprio;
2825 
2826     if (env->virt_enabled) {
2827         return read_vstopi(env, CSR_VSTOPI, val);
2828     }
2829 
2830     irq = riscv_cpu_sirq_pending(env);
2831     if (irq <= 0 || irq > 63) {
2832         *val = 0;
2833     } else {
2834         iprio = env->siprio[irq];
2835         if (!iprio) {
2836             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) {
2837                 iprio = IPRIO_MMAXIPRIO;
2838            }
2839         }
2840         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
2841         *val |= iprio;
2842     }
2843 
2844     return RISCV_EXCP_NONE;
2845 }
2846 
2847 /* Hypervisor Extensions */
2848 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
2849                                    target_ulong *val)
2850 {
2851     *val = env->hstatus;
2852     if (riscv_cpu_mxl(env) != MXL_RV32) {
2853         /* We only support 64-bit VSXL */
2854         *val = set_field(*val, HSTATUS_VSXL, 2);
2855     }
2856     /* We only support little endian */
2857     *val = set_field(*val, HSTATUS_VSBE, 0);
2858     return RISCV_EXCP_NONE;
2859 }
2860 
2861 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
2862                                     target_ulong val)
2863 {
2864     env->hstatus = val;
2865     if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
2866         qemu_log_mask(LOG_UNIMP,
2867                       "QEMU does not support mixed HSXLEN options.");
2868     }
2869     if (get_field(val, HSTATUS_VSBE) != 0) {
2870         qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
2871     }
2872     return RISCV_EXCP_NONE;
2873 }
2874 
2875 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
2876                                    target_ulong *val)
2877 {
2878     *val = env->hedeleg;
2879     return RISCV_EXCP_NONE;
2880 }
2881 
2882 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
2883                                     target_ulong val)
2884 {
2885     env->hedeleg = val & vs_delegable_excps;
2886     return RISCV_EXCP_NONE;
2887 }
2888 
2889 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno,
2890                                     uint64_t *ret_val,
2891                                     uint64_t new_val, uint64_t wr_mask)
2892 {
2893     uint64_t mask = wr_mask & vs_delegable_ints;
2894 
2895     if (ret_val) {
2896         *ret_val = env->hideleg & vs_delegable_ints;
2897     }
2898 
2899     env->hideleg = (env->hideleg & ~mask) | (new_val & mask);
2900     return RISCV_EXCP_NONE;
2901 }
2902 
2903 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno,
2904                                   target_ulong *ret_val,
2905                                   target_ulong new_val, target_ulong wr_mask)
2906 {
2907     uint64_t rval;
2908     RISCVException ret;
2909 
2910     ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask);
2911     if (ret_val) {
2912         *ret_val = rval;
2913     }
2914 
2915     return ret;
2916 }
2917 
2918 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno,
2919                                    target_ulong *ret_val,
2920                                    target_ulong new_val, target_ulong wr_mask)
2921 {
2922     uint64_t rval;
2923     RISCVException ret;
2924 
2925     ret = rmw_hideleg64(env, csrno, &rval,
2926         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2927     if (ret_val) {
2928         *ret_val = rval >> 32;
2929     }
2930 
2931     return ret;
2932 }
2933 
2934 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
2935                                  uint64_t *ret_val,
2936                                  uint64_t new_val, uint64_t wr_mask)
2937 {
2938     RISCVException ret;
2939 
2940     ret = rmw_mip64(env, csrno, ret_val, new_val,
2941                     wr_mask & hvip_writable_mask);
2942     if (ret_val) {
2943         *ret_val &= VS_MODE_INTERRUPTS;
2944     }
2945 
2946     return ret;
2947 }
2948 
2949 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
2950                                target_ulong *ret_val,
2951                                target_ulong new_val, target_ulong wr_mask)
2952 {
2953     uint64_t rval;
2954     RISCVException ret;
2955 
2956     ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask);
2957     if (ret_val) {
2958         *ret_val = rval;
2959     }
2960 
2961     return ret;
2962 }
2963 
2964 static RISCVException rmw_hviph(CPURISCVState *env, int csrno,
2965                                 target_ulong *ret_val,
2966                                 target_ulong new_val, target_ulong wr_mask)
2967 {
2968     uint64_t rval;
2969     RISCVException ret;
2970 
2971     ret = rmw_hvip64(env, csrno, &rval,
2972         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2973     if (ret_val) {
2974         *ret_val = rval >> 32;
2975     }
2976 
2977     return ret;
2978 }
2979 
2980 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
2981                               target_ulong *ret_value,
2982                               target_ulong new_value, target_ulong write_mask)
2983 {
2984     int ret = rmw_mip(env, csrno, ret_value, new_value,
2985                       write_mask & hip_writable_mask);
2986 
2987     if (ret_value) {
2988         *ret_value &= HS_MODE_INTERRUPTS;
2989     }
2990     return ret;
2991 }
2992 
2993 static RISCVException rmw_hie(CPURISCVState *env, int csrno,
2994                               target_ulong *ret_val,
2995                               target_ulong new_val, target_ulong wr_mask)
2996 {
2997     uint64_t rval;
2998     RISCVException ret;
2999 
3000     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS);
3001     if (ret_val) {
3002         *ret_val = rval & HS_MODE_INTERRUPTS;
3003     }
3004 
3005     return ret;
3006 }
3007 
3008 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
3009                                       target_ulong *val)
3010 {
3011     *val = env->hcounteren;
3012     return RISCV_EXCP_NONE;
3013 }
3014 
3015 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
3016                                        target_ulong val)
3017 {
3018     env->hcounteren = val;
3019     return RISCV_EXCP_NONE;
3020 }
3021 
3022 static RISCVException read_hgeie(CPURISCVState *env, int csrno,
3023                                  target_ulong *val)
3024 {
3025     if (val) {
3026         *val = env->hgeie;
3027     }
3028     return RISCV_EXCP_NONE;
3029 }
3030 
3031 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
3032                                   target_ulong val)
3033 {
3034     /* Only GEILEN:1 bits implemented and BIT0 is never implemented */
3035     val &= ((((target_ulong)1) << env->geilen) - 1) << 1;
3036     env->hgeie = val;
3037     /* Update mip.SGEIP bit */
3038     riscv_cpu_update_mip(env, MIP_SGEIP,
3039                          BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
3040     return RISCV_EXCP_NONE;
3041 }
3042 
3043 static RISCVException read_htval(CPURISCVState *env, int csrno,
3044                                  target_ulong *val)
3045 {
3046     *val = env->htval;
3047     return RISCV_EXCP_NONE;
3048 }
3049 
3050 static RISCVException write_htval(CPURISCVState *env, int csrno,
3051                                   target_ulong val)
3052 {
3053     env->htval = val;
3054     return RISCV_EXCP_NONE;
3055 }
3056 
3057 static RISCVException read_htinst(CPURISCVState *env, int csrno,
3058                                   target_ulong *val)
3059 {
3060     *val = env->htinst;
3061     return RISCV_EXCP_NONE;
3062 }
3063 
3064 static RISCVException write_htinst(CPURISCVState *env, int csrno,
3065                                    target_ulong val)
3066 {
3067     return RISCV_EXCP_NONE;
3068 }
3069 
3070 static RISCVException read_hgeip(CPURISCVState *env, int csrno,
3071                                  target_ulong *val)
3072 {
3073     if (val) {
3074         *val = env->hgeip;
3075     }
3076     return RISCV_EXCP_NONE;
3077 }
3078 
3079 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
3080                                  target_ulong *val)
3081 {
3082     *val = env->hgatp;
3083     return RISCV_EXCP_NONE;
3084 }
3085 
3086 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
3087                                   target_ulong val)
3088 {
3089     env->hgatp = val;
3090     return RISCV_EXCP_NONE;
3091 }
3092 
3093 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
3094                                       target_ulong *val)
3095 {
3096     if (!env->rdtime_fn) {
3097         return RISCV_EXCP_ILLEGAL_INST;
3098     }
3099 
3100     *val = env->htimedelta;
3101     return RISCV_EXCP_NONE;
3102 }
3103 
3104 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
3105                                        target_ulong val)
3106 {
3107     if (!env->rdtime_fn) {
3108         return RISCV_EXCP_ILLEGAL_INST;
3109     }
3110 
3111     if (riscv_cpu_mxl(env) == MXL_RV32) {
3112         env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
3113     } else {
3114         env->htimedelta = val;
3115     }
3116 
3117     if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3118         riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3119                                   env->htimedelta, MIP_VSTIP);
3120     }
3121 
3122     return RISCV_EXCP_NONE;
3123 }
3124 
3125 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
3126                                        target_ulong *val)
3127 {
3128     if (!env->rdtime_fn) {
3129         return RISCV_EXCP_ILLEGAL_INST;
3130     }
3131 
3132     *val = env->htimedelta >> 32;
3133     return RISCV_EXCP_NONE;
3134 }
3135 
3136 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
3137                                         target_ulong val)
3138 {
3139     if (!env->rdtime_fn) {
3140         return RISCV_EXCP_ILLEGAL_INST;
3141     }
3142 
3143     env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
3144 
3145     if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) {
3146         riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp,
3147                                   env->htimedelta, MIP_VSTIP);
3148     }
3149 
3150     return RISCV_EXCP_NONE;
3151 }
3152 
3153 static int read_hvictl(CPURISCVState *env, int csrno, target_ulong *val)
3154 {
3155     *val = env->hvictl;
3156     return RISCV_EXCP_NONE;
3157 }
3158 
3159 static int write_hvictl(CPURISCVState *env, int csrno, target_ulong val)
3160 {
3161     env->hvictl = val & HVICTL_VALID_MASK;
3162     return RISCV_EXCP_NONE;
3163 }
3164 
3165 static int read_hvipriox(CPURISCVState *env, int first_index,
3166                          uint8_t *iprio, target_ulong *val)
3167 {
3168     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3169 
3170     /* First index has to be a multiple of number of irqs per register */
3171     if (first_index % num_irqs) {
3172         return (env->virt_enabled) ?
3173                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3174     }
3175 
3176     /* Fill-up return value */
3177     *val = 0;
3178     for (i = 0; i < num_irqs; i++) {
3179         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3180             continue;
3181         }
3182         if (rdzero) {
3183             continue;
3184         }
3185         *val |= ((target_ulong)iprio[irq]) << (i * 8);
3186     }
3187 
3188     return RISCV_EXCP_NONE;
3189 }
3190 
3191 static int write_hvipriox(CPURISCVState *env, int first_index,
3192                           uint8_t *iprio, target_ulong val)
3193 {
3194     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
3195 
3196     /* First index has to be a multiple of number of irqs per register */
3197     if (first_index % num_irqs) {
3198         return (env->virt_enabled) ?
3199                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
3200     }
3201 
3202     /* Fill-up priority arrary */
3203     for (i = 0; i < num_irqs; i++) {
3204         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
3205             continue;
3206         }
3207         if (rdzero) {
3208             iprio[irq] = 0;
3209         } else {
3210             iprio[irq] = (val >> (i * 8)) & 0xff;
3211         }
3212     }
3213 
3214     return RISCV_EXCP_NONE;
3215 }
3216 
3217 static int read_hviprio1(CPURISCVState *env, int csrno, target_ulong *val)
3218 {
3219     return read_hvipriox(env, 0, env->hviprio, val);
3220 }
3221 
3222 static int write_hviprio1(CPURISCVState *env, int csrno, target_ulong val)
3223 {
3224     return write_hvipriox(env, 0, env->hviprio, val);
3225 }
3226 
3227 static int read_hviprio1h(CPURISCVState *env, int csrno, target_ulong *val)
3228 {
3229     return read_hvipriox(env, 4, env->hviprio, val);
3230 }
3231 
3232 static int write_hviprio1h(CPURISCVState *env, int csrno, target_ulong val)
3233 {
3234     return write_hvipriox(env, 4, env->hviprio, val);
3235 }
3236 
3237 static int read_hviprio2(CPURISCVState *env, int csrno, target_ulong *val)
3238 {
3239     return read_hvipriox(env, 8, env->hviprio, val);
3240 }
3241 
3242 static int write_hviprio2(CPURISCVState *env, int csrno, target_ulong val)
3243 {
3244     return write_hvipriox(env, 8, env->hviprio, val);
3245 }
3246 
3247 static int read_hviprio2h(CPURISCVState *env, int csrno, target_ulong *val)
3248 {
3249     return read_hvipriox(env, 12, env->hviprio, val);
3250 }
3251 
3252 static int write_hviprio2h(CPURISCVState *env, int csrno, target_ulong val)
3253 {
3254     return write_hvipriox(env, 12, env->hviprio, val);
3255 }
3256 
3257 /* Virtual CSR Registers */
3258 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
3259                                     target_ulong *val)
3260 {
3261     *val = env->vsstatus;
3262     return RISCV_EXCP_NONE;
3263 }
3264 
3265 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
3266                                      target_ulong val)
3267 {
3268     uint64_t mask = (target_ulong)-1;
3269     if ((val & VSSTATUS64_UXL) == 0) {
3270         mask &= ~VSSTATUS64_UXL;
3271     }
3272     env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
3273     return RISCV_EXCP_NONE;
3274 }
3275 
3276 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val)
3277 {
3278     *val = env->vstvec;
3279     return RISCV_EXCP_NONE;
3280 }
3281 
3282 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
3283                                    target_ulong val)
3284 {
3285     env->vstvec = val;
3286     return RISCV_EXCP_NONE;
3287 }
3288 
3289 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
3290                                      target_ulong *val)
3291 {
3292     *val = env->vsscratch;
3293     return RISCV_EXCP_NONE;
3294 }
3295 
3296 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
3297                                       target_ulong val)
3298 {
3299     env->vsscratch = val;
3300     return RISCV_EXCP_NONE;
3301 }
3302 
3303 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
3304                                  target_ulong *val)
3305 {
3306     *val = env->vsepc;
3307     return RISCV_EXCP_NONE;
3308 }
3309 
3310 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
3311                                   target_ulong val)
3312 {
3313     env->vsepc = val;
3314     return RISCV_EXCP_NONE;
3315 }
3316 
3317 static RISCVException read_vscause(CPURISCVState *env, int csrno,
3318                                    target_ulong *val)
3319 {
3320     *val = env->vscause;
3321     return RISCV_EXCP_NONE;
3322 }
3323 
3324 static RISCVException write_vscause(CPURISCVState *env, int csrno,
3325                                     target_ulong val)
3326 {
3327     env->vscause = val;
3328     return RISCV_EXCP_NONE;
3329 }
3330 
3331 static RISCVException read_vstval(CPURISCVState *env, int csrno,
3332                                   target_ulong *val)
3333 {
3334     *val = env->vstval;
3335     return RISCV_EXCP_NONE;
3336 }
3337 
3338 static RISCVException write_vstval(CPURISCVState *env, int csrno,
3339                                    target_ulong val)
3340 {
3341     env->vstval = val;
3342     return RISCV_EXCP_NONE;
3343 }
3344 
3345 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
3346                                  target_ulong *val)
3347 {
3348     *val = env->vsatp;
3349     return RISCV_EXCP_NONE;
3350 }
3351 
3352 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
3353                                   target_ulong val)
3354 {
3355     env->vsatp = val;
3356     return RISCV_EXCP_NONE;
3357 }
3358 
3359 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
3360                                   target_ulong *val)
3361 {
3362     *val = env->mtval2;
3363     return RISCV_EXCP_NONE;
3364 }
3365 
3366 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
3367                                    target_ulong val)
3368 {
3369     env->mtval2 = val;
3370     return RISCV_EXCP_NONE;
3371 }
3372 
3373 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
3374                                   target_ulong *val)
3375 {
3376     *val = env->mtinst;
3377     return RISCV_EXCP_NONE;
3378 }
3379 
3380 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
3381                                    target_ulong val)
3382 {
3383     env->mtinst = val;
3384     return RISCV_EXCP_NONE;
3385 }
3386 
3387 /* Physical Memory Protection */
3388 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
3389                                    target_ulong *val)
3390 {
3391     *val = mseccfg_csr_read(env);
3392     return RISCV_EXCP_NONE;
3393 }
3394 
3395 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
3396                                     target_ulong val)
3397 {
3398     mseccfg_csr_write(env, val);
3399     return RISCV_EXCP_NONE;
3400 }
3401 
3402 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
3403                                   target_ulong *val)
3404 {
3405     uint32_t reg_index = csrno - CSR_PMPCFG0;
3406 
3407     *val = pmpcfg_csr_read(env, reg_index);
3408     return RISCV_EXCP_NONE;
3409 }
3410 
3411 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
3412                                    target_ulong val)
3413 {
3414     uint32_t reg_index = csrno - CSR_PMPCFG0;
3415 
3416     pmpcfg_csr_write(env, reg_index, val);
3417     return RISCV_EXCP_NONE;
3418 }
3419 
3420 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
3421                                    target_ulong *val)
3422 {
3423     *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
3424     return RISCV_EXCP_NONE;
3425 }
3426 
3427 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
3428                                     target_ulong val)
3429 {
3430     pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
3431     return RISCV_EXCP_NONE;
3432 }
3433 
3434 static RISCVException read_tselect(CPURISCVState *env, int csrno,
3435                                    target_ulong *val)
3436 {
3437     *val = tselect_csr_read(env);
3438     return RISCV_EXCP_NONE;
3439 }
3440 
3441 static RISCVException write_tselect(CPURISCVState *env, int csrno,
3442                                     target_ulong val)
3443 {
3444     tselect_csr_write(env, val);
3445     return RISCV_EXCP_NONE;
3446 }
3447 
3448 static RISCVException read_tdata(CPURISCVState *env, int csrno,
3449                                  target_ulong *val)
3450 {
3451     /* return 0 in tdata1 to end the trigger enumeration */
3452     if (env->trigger_cur >= RV_MAX_TRIGGERS && csrno == CSR_TDATA1) {
3453         *val = 0;
3454         return RISCV_EXCP_NONE;
3455     }
3456 
3457     if (!tdata_available(env, csrno - CSR_TDATA1)) {
3458         return RISCV_EXCP_ILLEGAL_INST;
3459     }
3460 
3461     *val = tdata_csr_read(env, csrno - CSR_TDATA1);
3462     return RISCV_EXCP_NONE;
3463 }
3464 
3465 static RISCVException write_tdata(CPURISCVState *env, int csrno,
3466                                   target_ulong val)
3467 {
3468     if (!tdata_available(env, csrno - CSR_TDATA1)) {
3469         return RISCV_EXCP_ILLEGAL_INST;
3470     }
3471 
3472     tdata_csr_write(env, csrno - CSR_TDATA1, val);
3473     return RISCV_EXCP_NONE;
3474 }
3475 
3476 static RISCVException read_tinfo(CPURISCVState *env, int csrno,
3477                                  target_ulong *val)
3478 {
3479     *val = tinfo_csr_read(env);
3480     return RISCV_EXCP_NONE;
3481 }
3482 
3483 /*
3484  * Functions to access Pointer Masking feature registers
3485  * We have to check if current priv lvl could modify
3486  * csr in given mode
3487  */
3488 static bool check_pm_current_disabled(CPURISCVState *env, int csrno)
3489 {
3490     int csr_priv = get_field(csrno, 0x300);
3491     int pm_current;
3492 
3493     if (env->debugger) {
3494         return false;
3495     }
3496     /*
3497      * If priv lvls differ that means we're accessing csr from higher priv lvl,
3498      * so allow the access
3499      */
3500     if (env->priv != csr_priv) {
3501         return false;
3502     }
3503     switch (env->priv) {
3504     case PRV_M:
3505         pm_current = get_field(env->mmte, M_PM_CURRENT);
3506         break;
3507     case PRV_S:
3508         pm_current = get_field(env->mmte, S_PM_CURRENT);
3509         break;
3510     case PRV_U:
3511         pm_current = get_field(env->mmte, U_PM_CURRENT);
3512         break;
3513     default:
3514         g_assert_not_reached();
3515     }
3516     /* It's same priv lvl, so we allow to modify csr only if pm.current==1 */
3517     return !pm_current;
3518 }
3519 
3520 static RISCVException read_mmte(CPURISCVState *env, int csrno,
3521                                 target_ulong *val)
3522 {
3523     *val = env->mmte & MMTE_MASK;
3524     return RISCV_EXCP_NONE;
3525 }
3526 
3527 static RISCVException write_mmte(CPURISCVState *env, int csrno,
3528                                  target_ulong val)
3529 {
3530     uint64_t mstatus;
3531     target_ulong wpri_val = val & MMTE_MASK;
3532 
3533     if (val != wpri_val) {
3534         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
3535                       TARGET_FMT_lx "\n", "MMTE: WPRI violation written 0x",
3536                       val, "vs expected 0x", wpri_val);
3537     }
3538     /* for machine mode pm.current is hardwired to 1 */
3539     wpri_val |= MMTE_M_PM_CURRENT;
3540 
3541     /* hardwiring pm.instruction bit to 0, since it's not supported yet */
3542     wpri_val &= ~(MMTE_M_PM_INSN | MMTE_S_PM_INSN | MMTE_U_PM_INSN);
3543     env->mmte = wpri_val | EXT_STATUS_DIRTY;
3544     riscv_cpu_update_mask(env);
3545 
3546     /* Set XS and SD bits, since PM CSRs are dirty */
3547     mstatus = env->mstatus | MSTATUS_XS;
3548     write_mstatus(env, csrno, mstatus);
3549     return RISCV_EXCP_NONE;
3550 }
3551 
3552 static RISCVException read_smte(CPURISCVState *env, int csrno,
3553                                 target_ulong *val)
3554 {
3555     *val = env->mmte & SMTE_MASK;
3556     return RISCV_EXCP_NONE;
3557 }
3558 
3559 static RISCVException write_smte(CPURISCVState *env, int csrno,
3560                                  target_ulong val)
3561 {
3562     target_ulong wpri_val = val & SMTE_MASK;
3563 
3564     if (val != wpri_val) {
3565         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
3566                       TARGET_FMT_lx "\n", "SMTE: WPRI violation written 0x",
3567                       val, "vs expected 0x", wpri_val);
3568     }
3569 
3570     /* if pm.current==0 we can't modify current PM CSRs */
3571     if (check_pm_current_disabled(env, csrno)) {
3572         return RISCV_EXCP_NONE;
3573     }
3574 
3575     wpri_val |= (env->mmte & ~SMTE_MASK);
3576     write_mmte(env, csrno, wpri_val);
3577     return RISCV_EXCP_NONE;
3578 }
3579 
3580 static RISCVException read_umte(CPURISCVState *env, int csrno,
3581                                 target_ulong *val)
3582 {
3583     *val = env->mmte & UMTE_MASK;
3584     return RISCV_EXCP_NONE;
3585 }
3586 
3587 static RISCVException write_umte(CPURISCVState *env, int csrno,
3588                                  target_ulong val)
3589 {
3590     target_ulong wpri_val = val & UMTE_MASK;
3591 
3592     if (val != wpri_val) {
3593         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s"
3594                       TARGET_FMT_lx "\n", "UMTE: WPRI violation written 0x",
3595                       val, "vs expected 0x", wpri_val);
3596     }
3597 
3598     if (check_pm_current_disabled(env, csrno)) {
3599         return RISCV_EXCP_NONE;
3600     }
3601 
3602     wpri_val |= (env->mmte & ~UMTE_MASK);
3603     write_mmte(env, csrno, wpri_val);
3604     return RISCV_EXCP_NONE;
3605 }
3606 
3607 static RISCVException read_mpmmask(CPURISCVState *env, int csrno,
3608                                    target_ulong *val)
3609 {
3610     *val = env->mpmmask;
3611     return RISCV_EXCP_NONE;
3612 }
3613 
3614 static RISCVException write_mpmmask(CPURISCVState *env, int csrno,
3615                                     target_ulong val)
3616 {
3617     uint64_t mstatus;
3618 
3619     env->mpmmask = val;
3620     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
3621         env->cur_pmmask = val;
3622     }
3623     env->mmte |= EXT_STATUS_DIRTY;
3624 
3625     /* Set XS and SD bits, since PM CSRs are dirty */
3626     mstatus = env->mstatus | MSTATUS_XS;
3627     write_mstatus(env, csrno, mstatus);
3628     return RISCV_EXCP_NONE;
3629 }
3630 
3631 static RISCVException read_spmmask(CPURISCVState *env, int csrno,
3632                                    target_ulong *val)
3633 {
3634     *val = env->spmmask;
3635     return RISCV_EXCP_NONE;
3636 }
3637 
3638 static RISCVException write_spmmask(CPURISCVState *env, int csrno,
3639                                     target_ulong val)
3640 {
3641     uint64_t mstatus;
3642 
3643     /* if pm.current==0 we can't modify current PM CSRs */
3644     if (check_pm_current_disabled(env, csrno)) {
3645         return RISCV_EXCP_NONE;
3646     }
3647     env->spmmask = val;
3648     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
3649         env->cur_pmmask = val;
3650     }
3651     env->mmte |= EXT_STATUS_DIRTY;
3652 
3653     /* Set XS and SD bits, since PM CSRs are dirty */
3654     mstatus = env->mstatus | MSTATUS_XS;
3655     write_mstatus(env, csrno, mstatus);
3656     return RISCV_EXCP_NONE;
3657 }
3658 
3659 static RISCVException read_upmmask(CPURISCVState *env, int csrno,
3660                                    target_ulong *val)
3661 {
3662     *val = env->upmmask;
3663     return RISCV_EXCP_NONE;
3664 }
3665 
3666 static RISCVException write_upmmask(CPURISCVState *env, int csrno,
3667                                     target_ulong val)
3668 {
3669     uint64_t mstatus;
3670 
3671     /* if pm.current==0 we can't modify current PM CSRs */
3672     if (check_pm_current_disabled(env, csrno)) {
3673         return RISCV_EXCP_NONE;
3674     }
3675     env->upmmask = val;
3676     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
3677         env->cur_pmmask = val;
3678     }
3679     env->mmte |= EXT_STATUS_DIRTY;
3680 
3681     /* Set XS and SD bits, since PM CSRs are dirty */
3682     mstatus = env->mstatus | MSTATUS_XS;
3683     write_mstatus(env, csrno, mstatus);
3684     return RISCV_EXCP_NONE;
3685 }
3686 
3687 static RISCVException read_mpmbase(CPURISCVState *env, int csrno,
3688                                    target_ulong *val)
3689 {
3690     *val = env->mpmbase;
3691     return RISCV_EXCP_NONE;
3692 }
3693 
3694 static RISCVException write_mpmbase(CPURISCVState *env, int csrno,
3695                                     target_ulong val)
3696 {
3697     uint64_t mstatus;
3698 
3699     env->mpmbase = val;
3700     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
3701         env->cur_pmbase = val;
3702     }
3703     env->mmte |= EXT_STATUS_DIRTY;
3704 
3705     /* Set XS and SD bits, since PM CSRs are dirty */
3706     mstatus = env->mstatus | MSTATUS_XS;
3707     write_mstatus(env, csrno, mstatus);
3708     return RISCV_EXCP_NONE;
3709 }
3710 
3711 static RISCVException read_spmbase(CPURISCVState *env, int csrno,
3712                                    target_ulong *val)
3713 {
3714     *val = env->spmbase;
3715     return RISCV_EXCP_NONE;
3716 }
3717 
3718 static RISCVException write_spmbase(CPURISCVState *env, int csrno,
3719                                     target_ulong val)
3720 {
3721     uint64_t mstatus;
3722 
3723     /* if pm.current==0 we can't modify current PM CSRs */
3724     if (check_pm_current_disabled(env, csrno)) {
3725         return RISCV_EXCP_NONE;
3726     }
3727     env->spmbase = val;
3728     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
3729         env->cur_pmbase = val;
3730     }
3731     env->mmte |= EXT_STATUS_DIRTY;
3732 
3733     /* Set XS and SD bits, since PM CSRs are dirty */
3734     mstatus = env->mstatus | MSTATUS_XS;
3735     write_mstatus(env, csrno, mstatus);
3736     return RISCV_EXCP_NONE;
3737 }
3738 
3739 static RISCVException read_upmbase(CPURISCVState *env, int csrno,
3740                                    target_ulong *val)
3741 {
3742     *val = env->upmbase;
3743     return RISCV_EXCP_NONE;
3744 }
3745 
3746 static RISCVException write_upmbase(CPURISCVState *env, int csrno,
3747                                     target_ulong val)
3748 {
3749     uint64_t mstatus;
3750 
3751     /* if pm.current==0 we can't modify current PM CSRs */
3752     if (check_pm_current_disabled(env, csrno)) {
3753         return RISCV_EXCP_NONE;
3754     }
3755     env->upmbase = val;
3756     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
3757         env->cur_pmbase = val;
3758     }
3759     env->mmte |= EXT_STATUS_DIRTY;
3760 
3761     /* Set XS and SD bits, since PM CSRs are dirty */
3762     mstatus = env->mstatus | MSTATUS_XS;
3763     write_mstatus(env, csrno, mstatus);
3764     return RISCV_EXCP_NONE;
3765 }
3766 
3767 #endif
3768 
3769 /* Crypto Extension */
3770 static RISCVException rmw_seed(CPURISCVState *env, int csrno,
3771                                target_ulong *ret_value,
3772                                target_ulong new_value,
3773                                target_ulong write_mask)
3774 {
3775     uint16_t random_v;
3776     Error *random_e = NULL;
3777     int random_r;
3778     target_ulong rval;
3779 
3780     random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
3781     if (unlikely(random_r < 0)) {
3782         /*
3783          * Failed, for unknown reasons in the crypto subsystem.
3784          * The best we can do is log the reason and return a
3785          * failure indication to the guest.  There is no reason
3786          * we know to expect the failure to be transitory, so
3787          * indicate DEAD to avoid having the guest spin on WAIT.
3788          */
3789         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
3790                       __func__, error_get_pretty(random_e));
3791         error_free(random_e);
3792         rval = SEED_OPST_DEAD;
3793     } else {
3794         rval = random_v | SEED_OPST_ES16;
3795     }
3796 
3797     if (ret_value) {
3798         *ret_value = rval;
3799     }
3800 
3801     return RISCV_EXCP_NONE;
3802 }
3803 
3804 /*
3805  * riscv_csrrw - read and/or update control and status register
3806  *
3807  * csrr   <->  riscv_csrrw(env, csrno, ret_value, 0, 0);
3808  * csrrw  <->  riscv_csrrw(env, csrno, ret_value, value, -1);
3809  * csrrs  <->  riscv_csrrw(env, csrno, ret_value, -1, value);
3810  * csrrc  <->  riscv_csrrw(env, csrno, ret_value, 0, value);
3811  */
3812 
3813 static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
3814                                                int csrno,
3815                                                bool write_mask)
3816 {
3817     /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
3818     bool read_only = get_field(csrno, 0xC00) == 3;
3819     int csr_min_priv = csr_ops[csrno].min_priv_ver;
3820 
3821     /* ensure the CSR extension is enabled */
3822     if (!riscv_cpu_cfg(env)->ext_icsr) {
3823         return RISCV_EXCP_ILLEGAL_INST;
3824     }
3825 
3826     /* ensure CSR is implemented by checking predicate */
3827     if (!csr_ops[csrno].predicate) {
3828         return RISCV_EXCP_ILLEGAL_INST;
3829     }
3830 
3831     /* privileged spec version check */
3832     if (env->priv_ver < csr_min_priv) {
3833         return RISCV_EXCP_ILLEGAL_INST;
3834     }
3835 
3836     /* read / write check */
3837     if (write_mask && read_only) {
3838         return RISCV_EXCP_ILLEGAL_INST;
3839     }
3840 
3841     /*
3842      * The predicate() not only does existence check but also does some
3843      * access control check which triggers for example virtual instruction
3844      * exception in some cases. When writing read-only CSRs in those cases
3845      * illegal instruction exception should be triggered instead of virtual
3846      * instruction exception. Hence this comes after the read / write check.
3847      */
3848     RISCVException ret = csr_ops[csrno].predicate(env, csrno);
3849     if (ret != RISCV_EXCP_NONE) {
3850         return ret;
3851     }
3852 
3853 #if !defined(CONFIG_USER_ONLY)
3854     int csr_priv, effective_priv = env->priv;
3855 
3856     if (riscv_has_ext(env, RVH) && env->priv == PRV_S &&
3857         !env->virt_enabled) {
3858         /*
3859          * We are in HS mode. Add 1 to the effective privledge level to
3860          * allow us to access the Hypervisor CSRs.
3861          */
3862         effective_priv++;
3863     }
3864 
3865     csr_priv = get_field(csrno, 0x300);
3866     if (!env->debugger && (effective_priv < csr_priv)) {
3867         if (csr_priv == (PRV_S + 1) && env->virt_enabled) {
3868             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
3869         }
3870         return RISCV_EXCP_ILLEGAL_INST;
3871     }
3872 #endif
3873     return RISCV_EXCP_NONE;
3874 }
3875 
3876 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,
3877                                        target_ulong *ret_value,
3878                                        target_ulong new_value,
3879                                        target_ulong write_mask)
3880 {
3881     RISCVException ret;
3882     target_ulong old_value;
3883 
3884     /* execute combined read/write operation if it exists */
3885     if (csr_ops[csrno].op) {
3886         return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
3887     }
3888 
3889     /* if no accessor exists then return failure */
3890     if (!csr_ops[csrno].read) {
3891         return RISCV_EXCP_ILLEGAL_INST;
3892     }
3893     /* read old value */
3894     ret = csr_ops[csrno].read(env, csrno, &old_value);
3895     if (ret != RISCV_EXCP_NONE) {
3896         return ret;
3897     }
3898 
3899     /* write value if writable and write mask set, otherwise drop writes */
3900     if (write_mask) {
3901         new_value = (old_value & ~write_mask) | (new_value & write_mask);
3902         if (csr_ops[csrno].write) {
3903             ret = csr_ops[csrno].write(env, csrno, new_value);
3904             if (ret != RISCV_EXCP_NONE) {
3905                 return ret;
3906             }
3907         }
3908     }
3909 
3910     /* return old value */
3911     if (ret_value) {
3912         *ret_value = old_value;
3913     }
3914 
3915     return RISCV_EXCP_NONE;
3916 }
3917 
3918 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
3919                            target_ulong *ret_value,
3920                            target_ulong new_value, target_ulong write_mask)
3921 {
3922     RISCVException ret = riscv_csrrw_check(env, csrno, write_mask);
3923     if (ret != RISCV_EXCP_NONE) {
3924         return ret;
3925     }
3926 
3927     return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask);
3928 }
3929 
3930 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,
3931                                         Int128 *ret_value,
3932                                         Int128 new_value,
3933                                         Int128 write_mask)
3934 {
3935     RISCVException ret;
3936     Int128 old_value;
3937 
3938     /* read old value */
3939     ret = csr_ops[csrno].read128(env, csrno, &old_value);
3940     if (ret != RISCV_EXCP_NONE) {
3941         return ret;
3942     }
3943 
3944     /* write value if writable and write mask set, otherwise drop writes */
3945     if (int128_nz(write_mask)) {
3946         new_value = int128_or(int128_and(old_value, int128_not(write_mask)),
3947                               int128_and(new_value, write_mask));
3948         if (csr_ops[csrno].write128) {
3949             ret = csr_ops[csrno].write128(env, csrno, new_value);
3950             if (ret != RISCV_EXCP_NONE) {
3951                 return ret;
3952             }
3953         } else if (csr_ops[csrno].write) {
3954             /* avoids having to write wrappers for all registers */
3955             ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value));
3956             if (ret != RISCV_EXCP_NONE) {
3957                 return ret;
3958             }
3959         }
3960     }
3961 
3962     /* return old value */
3963     if (ret_value) {
3964         *ret_value = old_value;
3965     }
3966 
3967     return RISCV_EXCP_NONE;
3968 }
3969 
3970 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
3971                                 Int128 *ret_value,
3972                                 Int128 new_value, Int128 write_mask)
3973 {
3974     RISCVException ret;
3975 
3976     ret = riscv_csrrw_check(env, csrno, int128_nz(write_mask));
3977     if (ret != RISCV_EXCP_NONE) {
3978         return ret;
3979     }
3980 
3981     if (csr_ops[csrno].read128) {
3982         return riscv_csrrw_do128(env, csrno, ret_value, new_value, write_mask);
3983     }
3984 
3985     /*
3986      * Fall back to 64-bit version for now, if the 128-bit alternative isn't
3987      * at all defined.
3988      * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
3989      * significant), for those, this fallback is correctly handling the
3990      * accesses
3991      */
3992     target_ulong old_value;
3993     ret = riscv_csrrw_do64(env, csrno, &old_value,
3994                            int128_getlo(new_value),
3995                            int128_getlo(write_mask));
3996     if (ret == RISCV_EXCP_NONE && ret_value) {
3997         *ret_value = int128_make64(old_value);
3998     }
3999     return ret;
4000 }
4001 
4002 /*
4003  * Debugger support.  If not in user mode, set env->debugger before the
4004  * riscv_csrrw call and clear it after the call.
4005  */
4006 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
4007                                  target_ulong *ret_value,
4008                                  target_ulong new_value,
4009                                  target_ulong write_mask)
4010 {
4011     RISCVException ret;
4012 #if !defined(CONFIG_USER_ONLY)
4013     env->debugger = true;
4014 #endif
4015     ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
4016 #if !defined(CONFIG_USER_ONLY)
4017     env->debugger = false;
4018 #endif
4019     return ret;
4020 }
4021 
4022 static RISCVException read_jvt(CPURISCVState *env, int csrno,
4023                                target_ulong *val)
4024 {
4025     *val = env->jvt;
4026     return RISCV_EXCP_NONE;
4027 }
4028 
4029 static RISCVException write_jvt(CPURISCVState *env, int csrno,
4030                                 target_ulong val)
4031 {
4032     env->jvt = val;
4033     return RISCV_EXCP_NONE;
4034 }
4035 
4036 /*
4037  * Control and Status Register function table
4038  * riscv_csr_operations::predicate() must be provided for an implemented CSR
4039  */
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