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