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