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