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