xref: /openbmc/qemu/target/riscv/csr.c (revision b1675eeb)
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 "qemu/main-loop.h"
25 #include "exec/exec-all.h"
26 #include "sysemu/cpu-timers.h"
27 #include "qemu/guest-random.h"
28 #include "qapi/error.h"
29 
30 /* CSR function table public API */
31 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops)
32 {
33     *ops = csr_ops[csrno & (CSR_TABLE_SIZE - 1)];
34 }
35 
36 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops)
37 {
38     csr_ops[csrno & (CSR_TABLE_SIZE - 1)] = *ops;
39 }
40 
41 /* Predicates */
42 static RISCVException fs(CPURISCVState *env, int csrno)
43 {
44 #if !defined(CONFIG_USER_ONLY)
45     if (!env->debugger && !riscv_cpu_fp_enabled(env) &&
46         !RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) {
47         return RISCV_EXCP_ILLEGAL_INST;
48     }
49 #endif
50     return RISCV_EXCP_NONE;
51 }
52 
53 static RISCVException vs(CPURISCVState *env, int csrno)
54 {
55     CPUState *cs = env_cpu(env);
56     RISCVCPU *cpu = RISCV_CPU(cs);
57 
58     if (env->misa_ext & RVV ||
59         cpu->cfg.ext_zve32f || cpu->cfg.ext_zve64f) {
60 #if !defined(CONFIG_USER_ONLY)
61         if (!env->debugger && !riscv_cpu_vector_enabled(env)) {
62             return RISCV_EXCP_ILLEGAL_INST;
63         }
64 #endif
65         return RISCV_EXCP_NONE;
66     }
67     return RISCV_EXCP_ILLEGAL_INST;
68 }
69 
70 static RISCVException ctr(CPURISCVState *env, int csrno)
71 {
72 #if !defined(CONFIG_USER_ONLY)
73     CPUState *cs = env_cpu(env);
74     RISCVCPU *cpu = RISCV_CPU(cs);
75     int ctr_index;
76     int base_csrno = CSR_HPMCOUNTER3;
77     bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false;
78 
79     if (rv32 && csrno >= CSR_CYCLEH) {
80         /* Offset for RV32 hpmcounternh counters */
81         base_csrno += 0x80;
82     }
83     ctr_index = csrno - base_csrno;
84 
85     if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) {
86         /* No counter is enabled in PMU or the counter is out of range */
87         return RISCV_EXCP_ILLEGAL_INST;
88     }
89 
90     if (env->priv == PRV_S) {
91         switch (csrno) {
92         case CSR_CYCLE:
93             if (!get_field(env->mcounteren, COUNTEREN_CY)) {
94                 return RISCV_EXCP_ILLEGAL_INST;
95             }
96             break;
97         case CSR_TIME:
98             if (!get_field(env->mcounteren, COUNTEREN_TM)) {
99                 return RISCV_EXCP_ILLEGAL_INST;
100             }
101             break;
102         case CSR_INSTRET:
103             if (!get_field(env->mcounteren, COUNTEREN_IR)) {
104                 return RISCV_EXCP_ILLEGAL_INST;
105             }
106             break;
107         case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31:
108             ctr_index = csrno - CSR_CYCLE;
109             if (!get_field(env->mcounteren, 1 << ctr_index)) {
110                 return RISCV_EXCP_ILLEGAL_INST;
111             }
112             break;
113         }
114         if (rv32) {
115             switch (csrno) {
116             case CSR_CYCLEH:
117                 if (!get_field(env->mcounteren, COUNTEREN_CY)) {
118                     return RISCV_EXCP_ILLEGAL_INST;
119                 }
120                 break;
121             case CSR_TIMEH:
122                 if (!get_field(env->mcounteren, COUNTEREN_TM)) {
123                     return RISCV_EXCP_ILLEGAL_INST;
124                 }
125                 break;
126             case CSR_INSTRETH:
127                 if (!get_field(env->mcounteren, COUNTEREN_IR)) {
128                     return RISCV_EXCP_ILLEGAL_INST;
129                 }
130                 break;
131             case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
132                 ctr_index = csrno - CSR_CYCLEH;
133                 if (!get_field(env->mcounteren, 1 << ctr_index)) {
134                     return RISCV_EXCP_ILLEGAL_INST;
135                 }
136                 break;
137             }
138         }
139     }
140 
141     if (riscv_cpu_virt_enabled(env)) {
142         switch (csrno) {
143         case CSR_CYCLE:
144             if (!get_field(env->hcounteren, COUNTEREN_CY) &&
145                 get_field(env->mcounteren, COUNTEREN_CY)) {
146                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
147             }
148             break;
149         case CSR_TIME:
150             if (!get_field(env->hcounteren, COUNTEREN_TM) &&
151                 get_field(env->mcounteren, COUNTEREN_TM)) {
152                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
153             }
154             break;
155         case CSR_INSTRET:
156             if (!get_field(env->hcounteren, COUNTEREN_IR) &&
157                 get_field(env->mcounteren, COUNTEREN_IR)) {
158                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
159             }
160             break;
161         case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31:
162             ctr_index = csrno - CSR_CYCLE;
163             if (!get_field(env->hcounteren, 1 << ctr_index) &&
164                  get_field(env->mcounteren, 1 << ctr_index)) {
165                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
166             }
167             break;
168         }
169         if (rv32) {
170             switch (csrno) {
171             case CSR_CYCLEH:
172                 if (!get_field(env->hcounteren, COUNTEREN_CY) &&
173                     get_field(env->mcounteren, COUNTEREN_CY)) {
174                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
175                 }
176                 break;
177             case CSR_TIMEH:
178                 if (!get_field(env->hcounteren, COUNTEREN_TM) &&
179                     get_field(env->mcounteren, COUNTEREN_TM)) {
180                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
181                 }
182                 break;
183             case CSR_INSTRETH:
184                 if (!get_field(env->hcounteren, COUNTEREN_IR) &&
185                     get_field(env->mcounteren, COUNTEREN_IR)) {
186                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
187                 }
188                 break;
189             case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
190                 ctr_index = csrno - CSR_CYCLEH;
191                 if (!get_field(env->hcounteren, 1 << ctr_index) &&
192                      get_field(env->mcounteren, 1 << ctr_index)) {
193                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
194                 }
195                 break;
196             }
197         }
198     }
199 #endif
200     return RISCV_EXCP_NONE;
201 }
202 
203 static RISCVException ctr32(CPURISCVState *env, int csrno)
204 {
205     if (riscv_cpu_mxl(env) != MXL_RV32) {
206         return RISCV_EXCP_ILLEGAL_INST;
207     }
208 
209     return ctr(env, csrno);
210 }
211 
212 #if !defined(CONFIG_USER_ONLY)
213 static RISCVException mctr(CPURISCVState *env, int csrno)
214 {
215     CPUState *cs = env_cpu(env);
216     RISCVCPU *cpu = RISCV_CPU(cs);
217     int ctr_index;
218     int base_csrno = CSR_MHPMCOUNTER3;
219 
220     if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) {
221         /* Offset for RV32 mhpmcounternh counters */
222         base_csrno += 0x80;
223     }
224     ctr_index = csrno - base_csrno;
225     if (!cpu->cfg.pmu_num || ctr_index >= cpu->cfg.pmu_num) {
226         /* The PMU is not enabled or counter is out of range*/
227         return RISCV_EXCP_ILLEGAL_INST;
228     }
229 
230     return RISCV_EXCP_NONE;
231 }
232 
233 static RISCVException any(CPURISCVState *env, int csrno)
234 {
235     return RISCV_EXCP_NONE;
236 }
237 
238 static RISCVException any32(CPURISCVState *env, int csrno)
239 {
240     if (riscv_cpu_mxl(env) != MXL_RV32) {
241         return RISCV_EXCP_ILLEGAL_INST;
242     }
243 
244     return any(env, csrno);
245 
246 }
247 
248 static int aia_any(CPURISCVState *env, int csrno)
249 {
250     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
251         return RISCV_EXCP_ILLEGAL_INST;
252     }
253 
254     return any(env, csrno);
255 }
256 
257 static int aia_any32(CPURISCVState *env, int csrno)
258 {
259     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
260         return RISCV_EXCP_ILLEGAL_INST;
261     }
262 
263     return any32(env, csrno);
264 }
265 
266 static RISCVException smode(CPURISCVState *env, int csrno)
267 {
268     if (riscv_has_ext(env, RVS)) {
269         return RISCV_EXCP_NONE;
270     }
271 
272     return RISCV_EXCP_ILLEGAL_INST;
273 }
274 
275 static int smode32(CPURISCVState *env, int csrno)
276 {
277     if (riscv_cpu_mxl(env) != MXL_RV32) {
278         return RISCV_EXCP_ILLEGAL_INST;
279     }
280 
281     return smode(env, csrno);
282 }
283 
284 static int aia_smode(CPURISCVState *env, int csrno)
285 {
286     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
287         return RISCV_EXCP_ILLEGAL_INST;
288     }
289 
290     return smode(env, csrno);
291 }
292 
293 static int aia_smode32(CPURISCVState *env, int csrno)
294 {
295     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
296         return RISCV_EXCP_ILLEGAL_INST;
297     }
298 
299     return smode32(env, csrno);
300 }
301 
302 static RISCVException hmode(CPURISCVState *env, int csrno)
303 {
304     if (riscv_has_ext(env, RVS) &&
305         riscv_has_ext(env, RVH)) {
306         /* Hypervisor extension is supported */
307         if ((env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) ||
308             env->priv == PRV_M) {
309             return RISCV_EXCP_NONE;
310         } else {
311             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
312         }
313     }
314 
315     return RISCV_EXCP_ILLEGAL_INST;
316 }
317 
318 static RISCVException hmode32(CPURISCVState *env, int csrno)
319 {
320     if (riscv_cpu_mxl(env) != MXL_RV32) {
321         if (!riscv_cpu_virt_enabled(env)) {
322             return RISCV_EXCP_ILLEGAL_INST;
323         } else {
324             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
325         }
326     }
327 
328     return hmode(env, csrno);
329 
330 }
331 
332 /* Checks if PointerMasking registers could be accessed */
333 static RISCVException pointer_masking(CPURISCVState *env, int csrno)
334 {
335     /* Check if j-ext is present */
336     if (riscv_has_ext(env, RVJ)) {
337         return RISCV_EXCP_NONE;
338     }
339     return RISCV_EXCP_ILLEGAL_INST;
340 }
341 
342 static int aia_hmode(CPURISCVState *env, int csrno)
343 {
344     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
345         return RISCV_EXCP_ILLEGAL_INST;
346      }
347 
348      return hmode(env, csrno);
349 }
350 
351 static int aia_hmode32(CPURISCVState *env, int csrno)
352 {
353     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
354         return RISCV_EXCP_ILLEGAL_INST;
355     }
356 
357     return hmode32(env, csrno);
358 }
359 
360 static RISCVException pmp(CPURISCVState *env, int csrno)
361 {
362     if (riscv_feature(env, RISCV_FEATURE_PMP)) {
363         return RISCV_EXCP_NONE;
364     }
365 
366     return RISCV_EXCP_ILLEGAL_INST;
367 }
368 
369 static RISCVException epmp(CPURISCVState *env, int csrno)
370 {
371     if (env->priv == PRV_M && riscv_feature(env, RISCV_FEATURE_EPMP)) {
372         return RISCV_EXCP_NONE;
373     }
374 
375     return RISCV_EXCP_ILLEGAL_INST;
376 }
377 
378 static RISCVException debug(CPURISCVState *env, int csrno)
379 {
380     if (riscv_feature(env, RISCV_FEATURE_DEBUG)) {
381         return RISCV_EXCP_NONE;
382     }
383 
384     return RISCV_EXCP_ILLEGAL_INST;
385 }
386 #endif
387 
388 static RISCVException seed(CPURISCVState *env, int csrno)
389 {
390     RISCVCPU *cpu = env_archcpu(env);
391 
392     if (!cpu->cfg.ext_zkr) {
393         return RISCV_EXCP_ILLEGAL_INST;
394     }
395 
396 #if !defined(CONFIG_USER_ONLY)
397     /*
398      * With a CSR read-write instruction:
399      * 1) The seed CSR is always available in machine mode as normal.
400      * 2) Attempted access to seed from virtual modes VS and VU always raises
401      * an exception(virtual instruction exception only if mseccfg.sseed=1).
402      * 3) Without the corresponding access control bit set to 1, any attempted
403      * access to seed from U, S or HS modes will raise an illegal instruction
404      * exception.
405      */
406     if (env->priv == PRV_M) {
407         return RISCV_EXCP_NONE;
408     } else if (riscv_cpu_virt_enabled(env)) {
409         if (env->mseccfg & MSECCFG_SSEED) {
410             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
411         } else {
412             return RISCV_EXCP_ILLEGAL_INST;
413         }
414     } else {
415         if (env->priv == PRV_S && (env->mseccfg & MSECCFG_SSEED)) {
416             return RISCV_EXCP_NONE;
417         } else if (env->priv == PRV_U && (env->mseccfg & MSECCFG_USEED)) {
418             return RISCV_EXCP_NONE;
419         } else {
420             return RISCV_EXCP_ILLEGAL_INST;
421         }
422     }
423 #else
424     return RISCV_EXCP_NONE;
425 #endif
426 }
427 
428 /* User Floating-Point CSRs */
429 static RISCVException read_fflags(CPURISCVState *env, int csrno,
430                                   target_ulong *val)
431 {
432     *val = riscv_cpu_get_fflags(env);
433     return RISCV_EXCP_NONE;
434 }
435 
436 static RISCVException write_fflags(CPURISCVState *env, int csrno,
437                                    target_ulong val)
438 {
439 #if !defined(CONFIG_USER_ONLY)
440     if (riscv_has_ext(env, RVF)) {
441         env->mstatus |= MSTATUS_FS;
442     }
443 #endif
444     riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
445     return RISCV_EXCP_NONE;
446 }
447 
448 static RISCVException read_frm(CPURISCVState *env, int csrno,
449                                target_ulong *val)
450 {
451     *val = env->frm;
452     return RISCV_EXCP_NONE;
453 }
454 
455 static RISCVException write_frm(CPURISCVState *env, int csrno,
456                                 target_ulong val)
457 {
458 #if !defined(CONFIG_USER_ONLY)
459     if (riscv_has_ext(env, RVF)) {
460         env->mstatus |= MSTATUS_FS;
461     }
462 #endif
463     env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
464     return RISCV_EXCP_NONE;
465 }
466 
467 static RISCVException read_fcsr(CPURISCVState *env, int csrno,
468                                 target_ulong *val)
469 {
470     *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
471         | (env->frm << FSR_RD_SHIFT);
472     return RISCV_EXCP_NONE;
473 }
474 
475 static RISCVException write_fcsr(CPURISCVState *env, int csrno,
476                                  target_ulong val)
477 {
478 #if !defined(CONFIG_USER_ONLY)
479     if (riscv_has_ext(env, RVF)) {
480         env->mstatus |= MSTATUS_FS;
481     }
482 #endif
483     env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
484     riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
485     return RISCV_EXCP_NONE;
486 }
487 
488 static RISCVException read_vtype(CPURISCVState *env, int csrno,
489                                  target_ulong *val)
490 {
491     uint64_t vill;
492     switch (env->xl) {
493     case MXL_RV32:
494         vill = (uint32_t)env->vill << 31;
495         break;
496     case MXL_RV64:
497         vill = (uint64_t)env->vill << 63;
498         break;
499     default:
500         g_assert_not_reached();
501     }
502     *val = (target_ulong)vill | env->vtype;
503     return RISCV_EXCP_NONE;
504 }
505 
506 static RISCVException read_vl(CPURISCVState *env, int csrno,
507                               target_ulong *val)
508 {
509     *val = env->vl;
510     return RISCV_EXCP_NONE;
511 }
512 
513 static int read_vlenb(CPURISCVState *env, int csrno, target_ulong *val)
514 {
515     *val = env_archcpu(env)->cfg.vlen >> 3;
516     return RISCV_EXCP_NONE;
517 }
518 
519 static RISCVException read_vxrm(CPURISCVState *env, int csrno,
520                                 target_ulong *val)
521 {
522     *val = env->vxrm;
523     return RISCV_EXCP_NONE;
524 }
525 
526 static RISCVException write_vxrm(CPURISCVState *env, int csrno,
527                                  target_ulong val)
528 {
529 #if !defined(CONFIG_USER_ONLY)
530     env->mstatus |= MSTATUS_VS;
531 #endif
532     env->vxrm = val;
533     return RISCV_EXCP_NONE;
534 }
535 
536 static RISCVException read_vxsat(CPURISCVState *env, int csrno,
537                                  target_ulong *val)
538 {
539     *val = env->vxsat;
540     return RISCV_EXCP_NONE;
541 }
542 
543 static RISCVException write_vxsat(CPURISCVState *env, int csrno,
544                                   target_ulong val)
545 {
546 #if !defined(CONFIG_USER_ONLY)
547     env->mstatus |= MSTATUS_VS;
548 #endif
549     env->vxsat = val;
550     return RISCV_EXCP_NONE;
551 }
552 
553 static RISCVException read_vstart(CPURISCVState *env, int csrno,
554                                   target_ulong *val)
555 {
556     *val = env->vstart;
557     return RISCV_EXCP_NONE;
558 }
559 
560 static RISCVException write_vstart(CPURISCVState *env, int csrno,
561                                    target_ulong val)
562 {
563 #if !defined(CONFIG_USER_ONLY)
564     env->mstatus |= MSTATUS_VS;
565 #endif
566     /*
567      * The vstart CSR is defined to have only enough writable bits
568      * to hold the largest element index, i.e. lg2(VLEN) bits.
569      */
570     env->vstart = val & ~(~0ULL << ctzl(env_archcpu(env)->cfg.vlen));
571     return RISCV_EXCP_NONE;
572 }
573 
574 static int read_vcsr(CPURISCVState *env, int csrno, target_ulong *val)
575 {
576     *val = (env->vxrm << VCSR_VXRM_SHIFT) | (env->vxsat << VCSR_VXSAT_SHIFT);
577     return RISCV_EXCP_NONE;
578 }
579 
580 static int write_vcsr(CPURISCVState *env, int csrno, target_ulong val)
581 {
582 #if !defined(CONFIG_USER_ONLY)
583     env->mstatus |= MSTATUS_VS;
584 #endif
585     env->vxrm = (val & VCSR_VXRM) >> VCSR_VXRM_SHIFT;
586     env->vxsat = (val & VCSR_VXSAT) >> VCSR_VXSAT_SHIFT;
587     return RISCV_EXCP_NONE;
588 }
589 
590 /* User Timers and Counters */
591 static RISCVException read_instret(CPURISCVState *env, int csrno,
592                                    target_ulong *val)
593 {
594 #if !defined(CONFIG_USER_ONLY)
595     if (icount_enabled()) {
596         *val = icount_get();
597     } else {
598         *val = cpu_get_host_ticks();
599     }
600 #else
601     *val = cpu_get_host_ticks();
602 #endif
603     return RISCV_EXCP_NONE;
604 }
605 
606 static RISCVException read_instreth(CPURISCVState *env, int csrno,
607                                     target_ulong *val)
608 {
609 #if !defined(CONFIG_USER_ONLY)
610     if (icount_enabled()) {
611         *val = icount_get() >> 32;
612     } else {
613         *val = cpu_get_host_ticks() >> 32;
614     }
615 #else
616     *val = cpu_get_host_ticks() >> 32;
617 #endif
618     return RISCV_EXCP_NONE;
619 }
620 
621 #if defined(CONFIG_USER_ONLY)
622 static RISCVException read_time(CPURISCVState *env, int csrno,
623                                 target_ulong *val)
624 {
625     *val = cpu_get_host_ticks();
626     return RISCV_EXCP_NONE;
627 }
628 
629 static RISCVException read_timeh(CPURISCVState *env, int csrno,
630                                  target_ulong *val)
631 {
632     *val = cpu_get_host_ticks() >> 32;
633     return RISCV_EXCP_NONE;
634 }
635 
636 #else /* CONFIG_USER_ONLY */
637 
638 static RISCVException read_time(CPURISCVState *env, int csrno,
639                                 target_ulong *val)
640 {
641     uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
642 
643     if (!env->rdtime_fn) {
644         return RISCV_EXCP_ILLEGAL_INST;
645     }
646 
647     *val = env->rdtime_fn(env->rdtime_fn_arg) + delta;
648     return RISCV_EXCP_NONE;
649 }
650 
651 static RISCVException read_timeh(CPURISCVState *env, int csrno,
652                                  target_ulong *val)
653 {
654     uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
655 
656     if (!env->rdtime_fn) {
657         return RISCV_EXCP_ILLEGAL_INST;
658     }
659 
660     *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32;
661     return RISCV_EXCP_NONE;
662 }
663 
664 /* Machine constants */
665 
666 #define M_MODE_INTERRUPTS  ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP))
667 #define S_MODE_INTERRUPTS  ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP))
668 #define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))
669 #define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS))
670 
671 #define VSTOPI_NUM_SRCS 5
672 
673 static const uint64_t delegable_ints = S_MODE_INTERRUPTS |
674                                            VS_MODE_INTERRUPTS;
675 static const uint64_t vs_delegable_ints = VS_MODE_INTERRUPTS;
676 static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
677                                      HS_MODE_INTERRUPTS;
678 #define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \
679                          (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \
680                          (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \
681                          (1ULL << (RISCV_EXCP_BREAKPOINT)) | \
682                          (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | \
683                          (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | \
684                          (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | \
685                          (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | \
686                          (1ULL << (RISCV_EXCP_U_ECALL)) | \
687                          (1ULL << (RISCV_EXCP_S_ECALL)) | \
688                          (1ULL << (RISCV_EXCP_VS_ECALL)) | \
689                          (1ULL << (RISCV_EXCP_M_ECALL)) | \
690                          (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | \
691                          (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \
692                          (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \
693                          (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \
694                          (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \
695                          (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
696                          (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)))
697 static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &
698     ~((1ULL << (RISCV_EXCP_S_ECALL)) |
699       (1ULL << (RISCV_EXCP_VS_ECALL)) |
700       (1ULL << (RISCV_EXCP_M_ECALL)) |
701       (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) |
702       (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) |
703       (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) |
704       (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)));
705 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
706     SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
707     SSTATUS_SUM | SSTATUS_MXR | SSTATUS_VS;
708 static const target_ulong sip_writable_mask = SIP_SSIP | MIP_USIP | MIP_UEIP;
709 static const target_ulong hip_writable_mask = MIP_VSSIP;
710 static const target_ulong hvip_writable_mask = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP;
711 static const target_ulong vsip_writable_mask = MIP_VSSIP;
712 
713 static const char valid_vm_1_10_32[16] = {
714     [VM_1_10_MBARE] = 1,
715     [VM_1_10_SV32] = 1
716 };
717 
718 static const char valid_vm_1_10_64[16] = {
719     [VM_1_10_MBARE] = 1,
720     [VM_1_10_SV39] = 1,
721     [VM_1_10_SV48] = 1,
722     [VM_1_10_SV57] = 1
723 };
724 
725 /* Machine Information Registers */
726 static RISCVException read_zero(CPURISCVState *env, int csrno,
727                                 target_ulong *val)
728 {
729     *val = 0;
730     return RISCV_EXCP_NONE;
731 }
732 
733 static RISCVException write_ignore(CPURISCVState *env, int csrno,
734                                    target_ulong val)
735 {
736     return RISCV_EXCP_NONE;
737 }
738 
739 static RISCVException read_mvendorid(CPURISCVState *env, int csrno,
740                                      target_ulong *val)
741 {
742     CPUState *cs = env_cpu(env);
743     RISCVCPU *cpu = RISCV_CPU(cs);
744 
745     *val = cpu->cfg.mvendorid;
746     return RISCV_EXCP_NONE;
747 }
748 
749 static RISCVException read_marchid(CPURISCVState *env, int csrno,
750                                    target_ulong *val)
751 {
752     CPUState *cs = env_cpu(env);
753     RISCVCPU *cpu = RISCV_CPU(cs);
754 
755     *val = cpu->cfg.marchid;
756     return RISCV_EXCP_NONE;
757 }
758 
759 static RISCVException read_mimpid(CPURISCVState *env, int csrno,
760                                   target_ulong *val)
761 {
762     CPUState *cs = env_cpu(env);
763     RISCVCPU *cpu = RISCV_CPU(cs);
764 
765     *val = cpu->cfg.mimpid;
766     return RISCV_EXCP_NONE;
767 }
768 
769 static RISCVException read_mhartid(CPURISCVState *env, int csrno,
770                                    target_ulong *val)
771 {
772     *val = env->mhartid;
773     return RISCV_EXCP_NONE;
774 }
775 
776 /* Machine Trap Setup */
777 
778 /* We do not store SD explicitly, only compute it on demand. */
779 static uint64_t add_status_sd(RISCVMXL xl, uint64_t status)
780 {
781     if ((status & MSTATUS_FS) == MSTATUS_FS ||
782         (status & MSTATUS_VS) == MSTATUS_VS ||
783         (status & MSTATUS_XS) == MSTATUS_XS) {
784         switch (xl) {
785         case MXL_RV32:
786             return status | MSTATUS32_SD;
787         case MXL_RV64:
788             return status | MSTATUS64_SD;
789         case MXL_RV128:
790             return MSTATUSH128_SD;
791         default:
792             g_assert_not_reached();
793         }
794     }
795     return status;
796 }
797 
798 static RISCVException read_mstatus(CPURISCVState *env, int csrno,
799                                    target_ulong *val)
800 {
801     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus);
802     return RISCV_EXCP_NONE;
803 }
804 
805 static int validate_vm(CPURISCVState *env, target_ulong vm)
806 {
807     if (riscv_cpu_mxl(env) == MXL_RV32) {
808         return valid_vm_1_10_32[vm & 0xf];
809     } else {
810         return valid_vm_1_10_64[vm & 0xf];
811     }
812 }
813 
814 static RISCVException write_mstatus(CPURISCVState *env, int csrno,
815                                     target_ulong val)
816 {
817     uint64_t mstatus = env->mstatus;
818     uint64_t mask = 0;
819     RISCVMXL xl = riscv_cpu_mxl(env);
820 
821     /* flush tlb on mstatus fields that affect VM */
822     if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
823             MSTATUS_MPRV | MSTATUS_SUM)) {
824         tlb_flush(env_cpu(env));
825     }
826     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
827         MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
828         MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
829         MSTATUS_TW | MSTATUS_VS;
830 
831     if (riscv_has_ext(env, RVF)) {
832         mask |= MSTATUS_FS;
833     }
834 
835     if (xl != MXL_RV32 || env->debugger) {
836         /*
837          * RV32: MPV and GVA are not in mstatus. The current plan is to
838          * add them to mstatush. For now, we just don't support it.
839          */
840         mask |= MSTATUS_MPV | MSTATUS_GVA;
841         if ((val & MSTATUS64_UXL) != 0) {
842             mask |= MSTATUS64_UXL;
843         }
844     }
845 
846     mstatus = (mstatus & ~mask) | (val & mask);
847 
848     if (xl > MXL_RV32) {
849         /* SXL field is for now read only */
850         mstatus = set_field(mstatus, MSTATUS64_SXL, xl);
851     }
852     env->mstatus = mstatus;
853     env->xl = cpu_recompute_xl(env);
854 
855     return RISCV_EXCP_NONE;
856 }
857 
858 static RISCVException read_mstatush(CPURISCVState *env, int csrno,
859                                     target_ulong *val)
860 {
861     *val = env->mstatus >> 32;
862     return RISCV_EXCP_NONE;
863 }
864 
865 static RISCVException write_mstatush(CPURISCVState *env, int csrno,
866                                      target_ulong val)
867 {
868     uint64_t valh = (uint64_t)val << 32;
869     uint64_t mask = MSTATUS_MPV | MSTATUS_GVA;
870 
871     if ((valh ^ env->mstatus) & (MSTATUS_MPV)) {
872         tlb_flush(env_cpu(env));
873     }
874 
875     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
876 
877     return RISCV_EXCP_NONE;
878 }
879 
880 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno,
881                                         Int128 *val)
882 {
883     *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128, env->mstatus));
884     return RISCV_EXCP_NONE;
885 }
886 
887 static RISCVException read_misa_i128(CPURISCVState *env, int csrno,
888                                      Int128 *val)
889 {
890     *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62);
891     return RISCV_EXCP_NONE;
892 }
893 
894 static RISCVException read_misa(CPURISCVState *env, int csrno,
895                                 target_ulong *val)
896 {
897     target_ulong misa;
898 
899     switch (env->misa_mxl) {
900     case MXL_RV32:
901         misa = (target_ulong)MXL_RV32 << 30;
902         break;
903 #ifdef TARGET_RISCV64
904     case MXL_RV64:
905         misa = (target_ulong)MXL_RV64 << 62;
906         break;
907 #endif
908     default:
909         g_assert_not_reached();
910     }
911 
912     *val = misa | env->misa_ext;
913     return RISCV_EXCP_NONE;
914 }
915 
916 static RISCVException write_misa(CPURISCVState *env, int csrno,
917                                  target_ulong val)
918 {
919     if (!riscv_feature(env, RISCV_FEATURE_MISA)) {
920         /* drop write to misa */
921         return RISCV_EXCP_NONE;
922     }
923 
924     /* 'I' or 'E' must be present */
925     if (!(val & (RVI | RVE))) {
926         /* It is not, drop write to misa */
927         return RISCV_EXCP_NONE;
928     }
929 
930     /* 'E' excludes all other extensions */
931     if (val & RVE) {
932         /* when we support 'E' we can do "val = RVE;" however
933          * for now we just drop writes if 'E' is present.
934          */
935         return RISCV_EXCP_NONE;
936     }
937 
938     /*
939      * misa.MXL writes are not supported by QEMU.
940      * Drop writes to those bits.
941      */
942 
943     /* Mask extensions that are not supported by this hart */
944     val &= env->misa_ext_mask;
945 
946     /* Mask extensions that are not supported by QEMU */
947     val &= (RVI | RVE | RVM | RVA | RVF | RVD | RVC | RVS | RVU | RVV);
948 
949     /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
950     if ((val & RVD) && !(val & RVF)) {
951         val &= ~RVD;
952     }
953 
954     /* Suppress 'C' if next instruction is not aligned
955      * TODO: this should check next_pc
956      */
957     if ((val & RVC) && (GETPC() & ~3) != 0) {
958         val &= ~RVC;
959     }
960 
961     /* If nothing changed, do nothing. */
962     if (val == env->misa_ext) {
963         return RISCV_EXCP_NONE;
964     }
965 
966     if (!(val & RVF)) {
967         env->mstatus &= ~MSTATUS_FS;
968     }
969 
970     /* flush translation cache */
971     tb_flush(env_cpu(env));
972     env->misa_ext = val;
973     env->xl = riscv_cpu_mxl(env);
974     return RISCV_EXCP_NONE;
975 }
976 
977 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
978                                    target_ulong *val)
979 {
980     *val = env->medeleg;
981     return RISCV_EXCP_NONE;
982 }
983 
984 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
985                                     target_ulong val)
986 {
987     env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
988     return RISCV_EXCP_NONE;
989 }
990 
991 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno,
992                                     uint64_t *ret_val,
993                                     uint64_t new_val, uint64_t wr_mask)
994 {
995     uint64_t mask = wr_mask & delegable_ints;
996 
997     if (ret_val) {
998         *ret_val = env->mideleg;
999     }
1000 
1001     env->mideleg = (env->mideleg & ~mask) | (new_val & mask);
1002 
1003     if (riscv_has_ext(env, RVH)) {
1004         env->mideleg |= HS_MODE_INTERRUPTS;
1005     }
1006 
1007     return RISCV_EXCP_NONE;
1008 }
1009 
1010 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno,
1011                                   target_ulong *ret_val,
1012                                   target_ulong new_val, target_ulong wr_mask)
1013 {
1014     uint64_t rval;
1015     RISCVException ret;
1016 
1017     ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask);
1018     if (ret_val) {
1019         *ret_val = rval;
1020     }
1021 
1022     return ret;
1023 }
1024 
1025 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno,
1026                                    target_ulong *ret_val,
1027                                    target_ulong new_val,
1028                                    target_ulong wr_mask)
1029 {
1030     uint64_t rval;
1031     RISCVException ret;
1032 
1033     ret = rmw_mideleg64(env, csrno, &rval,
1034         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1035     if (ret_val) {
1036         *ret_val = rval >> 32;
1037     }
1038 
1039     return ret;
1040 }
1041 
1042 static RISCVException rmw_mie64(CPURISCVState *env, int csrno,
1043                                 uint64_t *ret_val,
1044                                 uint64_t new_val, uint64_t wr_mask)
1045 {
1046     uint64_t mask = wr_mask & all_ints;
1047 
1048     if (ret_val) {
1049         *ret_val = env->mie;
1050     }
1051 
1052     env->mie = (env->mie & ~mask) | (new_val & mask);
1053 
1054     if (!riscv_has_ext(env, RVH)) {
1055         env->mie &= ~((uint64_t)MIP_SGEIP);
1056     }
1057 
1058     return RISCV_EXCP_NONE;
1059 }
1060 
1061 static RISCVException rmw_mie(CPURISCVState *env, int csrno,
1062                               target_ulong *ret_val,
1063                               target_ulong new_val, target_ulong wr_mask)
1064 {
1065     uint64_t rval;
1066     RISCVException ret;
1067 
1068     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask);
1069     if (ret_val) {
1070         *ret_val = rval;
1071     }
1072 
1073     return ret;
1074 }
1075 
1076 static RISCVException rmw_mieh(CPURISCVState *env, int csrno,
1077                                target_ulong *ret_val,
1078                                target_ulong new_val, target_ulong wr_mask)
1079 {
1080     uint64_t rval;
1081     RISCVException ret;
1082 
1083     ret = rmw_mie64(env, csrno, &rval,
1084         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1085     if (ret_val) {
1086         *ret_val = rval >> 32;
1087     }
1088 
1089     return ret;
1090 }
1091 
1092 static int read_mtopi(CPURISCVState *env, int csrno, target_ulong *val)
1093 {
1094     int irq;
1095     uint8_t iprio;
1096 
1097     irq = riscv_cpu_mirq_pending(env);
1098     if (irq <= 0 || irq > 63) {
1099         *val = 0;
1100     } else {
1101         iprio = env->miprio[irq];
1102         if (!iprio) {
1103             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) {
1104                 iprio = IPRIO_MMAXIPRIO;
1105             }
1106         }
1107         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1108         *val |= iprio;
1109     }
1110 
1111     return RISCV_EXCP_NONE;
1112 }
1113 
1114 static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno)
1115 {
1116     if (!riscv_cpu_virt_enabled(env)) {
1117         return csrno;
1118     }
1119 
1120     switch (csrno) {
1121     case CSR_SISELECT:
1122         return CSR_VSISELECT;
1123     case CSR_SIREG:
1124         return CSR_VSIREG;
1125     case CSR_SSETEIPNUM:
1126         return CSR_VSSETEIPNUM;
1127     case CSR_SCLREIPNUM:
1128         return CSR_VSCLREIPNUM;
1129     case CSR_SSETEIENUM:
1130         return CSR_VSSETEIENUM;
1131     case CSR_SCLREIENUM:
1132         return CSR_VSCLREIENUM;
1133     case CSR_STOPEI:
1134         return CSR_VSTOPEI;
1135     default:
1136         return csrno;
1137     };
1138 }
1139 
1140 static int rmw_xiselect(CPURISCVState *env, int csrno, target_ulong *val,
1141                         target_ulong new_val, target_ulong wr_mask)
1142 {
1143     target_ulong *iselect;
1144 
1145     /* Translate CSR number for VS-mode */
1146     csrno = aia_xlate_vs_csrno(env, csrno);
1147 
1148     /* Find the iselect CSR based on CSR number */
1149     switch (csrno) {
1150     case CSR_MISELECT:
1151         iselect = &env->miselect;
1152         break;
1153     case CSR_SISELECT:
1154         iselect = &env->siselect;
1155         break;
1156     case CSR_VSISELECT:
1157         iselect = &env->vsiselect;
1158         break;
1159     default:
1160          return RISCV_EXCP_ILLEGAL_INST;
1161     };
1162 
1163     if (val) {
1164         *val = *iselect;
1165     }
1166 
1167     wr_mask &= ISELECT_MASK;
1168     if (wr_mask) {
1169         *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask);
1170     }
1171 
1172     return RISCV_EXCP_NONE;
1173 }
1174 
1175 static int rmw_iprio(target_ulong xlen,
1176                      target_ulong iselect, uint8_t *iprio,
1177                      target_ulong *val, target_ulong new_val,
1178                      target_ulong wr_mask, int ext_irq_no)
1179 {
1180     int i, firq, nirqs;
1181     target_ulong old_val;
1182 
1183     if (iselect < ISELECT_IPRIO0 || ISELECT_IPRIO15 < iselect) {
1184         return -EINVAL;
1185     }
1186     if (xlen != 32 && iselect & 0x1) {
1187         return -EINVAL;
1188     }
1189 
1190     nirqs = 4 * (xlen / 32);
1191     firq = ((iselect - ISELECT_IPRIO0) / (xlen / 32)) * (nirqs);
1192 
1193     old_val = 0;
1194     for (i = 0; i < nirqs; i++) {
1195         old_val |= ((target_ulong)iprio[firq + i]) << (IPRIO_IRQ_BITS * i);
1196     }
1197 
1198     if (val) {
1199         *val = old_val;
1200     }
1201 
1202     if (wr_mask) {
1203         new_val = (old_val & ~wr_mask) | (new_val & wr_mask);
1204         for (i = 0; i < nirqs; i++) {
1205             /*
1206              * M-level and S-level external IRQ priority always read-only
1207              * zero. This means default priority order is always preferred
1208              * for M-level and S-level external IRQs.
1209              */
1210             if ((firq + i) == ext_irq_no) {
1211                 continue;
1212             }
1213             iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) & 0xff;
1214         }
1215     }
1216 
1217     return 0;
1218 }
1219 
1220 static int rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val,
1221                      target_ulong new_val, target_ulong wr_mask)
1222 {
1223     bool virt;
1224     uint8_t *iprio;
1225     int ret = -EINVAL;
1226     target_ulong priv, isel, vgein;
1227 
1228     /* Translate CSR number for VS-mode */
1229     csrno = aia_xlate_vs_csrno(env, csrno);
1230 
1231     /* Decode register details from CSR number */
1232     virt = false;
1233     switch (csrno) {
1234     case CSR_MIREG:
1235         iprio = env->miprio;
1236         isel = env->miselect;
1237         priv = PRV_M;
1238         break;
1239     case CSR_SIREG:
1240         iprio = env->siprio;
1241         isel = env->siselect;
1242         priv = PRV_S;
1243         break;
1244     case CSR_VSIREG:
1245         iprio = env->hviprio;
1246         isel = env->vsiselect;
1247         priv = PRV_S;
1248         virt = true;
1249         break;
1250     default:
1251          goto done;
1252     };
1253 
1254     /* Find the selected guest interrupt file */
1255     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1256 
1257     if (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) {
1258         /* Local interrupt priority registers not available for VS-mode */
1259         if (!virt) {
1260             ret = rmw_iprio(riscv_cpu_mxl_bits(env),
1261                             isel, iprio, val, new_val, wr_mask,
1262                             (priv == PRV_M) ? IRQ_M_EXT : IRQ_S_EXT);
1263         }
1264     } else if (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST) {
1265         /* IMSIC registers only available when machine implements it. */
1266         if (env->aia_ireg_rmw_fn[priv]) {
1267             /* Selected guest interrupt file should not be zero */
1268             if (virt && (!vgein || env->geilen < vgein)) {
1269                 goto done;
1270             }
1271             /* Call machine specific IMSIC register emulation */
1272             ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1273                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
1274                                                   riscv_cpu_mxl_bits(env)),
1275                                     val, new_val, wr_mask);
1276         }
1277     }
1278 
1279 done:
1280     if (ret) {
1281         return (riscv_cpu_virt_enabled(env) && virt) ?
1282                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1283     }
1284     return RISCV_EXCP_NONE;
1285 }
1286 
1287 static int rmw_xsetclreinum(CPURISCVState *env, int csrno, target_ulong *val,
1288                             target_ulong new_val, target_ulong wr_mask)
1289 {
1290     int ret = -EINVAL;
1291     bool set, pend, virt;
1292     target_ulong priv, isel, vgein, xlen, nval, wmask;
1293 
1294     /* Translate CSR number for VS-mode */
1295     csrno = aia_xlate_vs_csrno(env, csrno);
1296 
1297     /* Decode register details from CSR number */
1298     virt = set = pend = false;
1299     switch (csrno) {
1300     case CSR_MSETEIPNUM:
1301         priv = PRV_M;
1302         set = true;
1303         pend = true;
1304         break;
1305     case CSR_MCLREIPNUM:
1306         priv = PRV_M;
1307         pend = true;
1308         break;
1309     case CSR_MSETEIENUM:
1310         priv = PRV_M;
1311         set = true;
1312         break;
1313     case CSR_MCLREIENUM:
1314         priv = PRV_M;
1315         break;
1316     case CSR_SSETEIPNUM:
1317         priv = PRV_S;
1318         set = true;
1319         pend = true;
1320         break;
1321     case CSR_SCLREIPNUM:
1322         priv = PRV_S;
1323         pend = true;
1324         break;
1325     case CSR_SSETEIENUM:
1326         priv = PRV_S;
1327         set = true;
1328         break;
1329     case CSR_SCLREIENUM:
1330         priv = PRV_S;
1331         break;
1332     case CSR_VSSETEIPNUM:
1333         priv = PRV_S;
1334         virt = true;
1335         set = true;
1336         pend = true;
1337         break;
1338     case CSR_VSCLREIPNUM:
1339         priv = PRV_S;
1340         virt = true;
1341         pend = true;
1342         break;
1343     case CSR_VSSETEIENUM:
1344         priv = PRV_S;
1345         virt = true;
1346         set = true;
1347         break;
1348     case CSR_VSCLREIENUM:
1349         priv = PRV_S;
1350         virt = true;
1351         break;
1352     default:
1353          goto done;
1354     };
1355 
1356     /* IMSIC CSRs only available when machine implements IMSIC. */
1357     if (!env->aia_ireg_rmw_fn[priv]) {
1358         goto done;
1359     }
1360 
1361     /* Find the selected guest interrupt file */
1362     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1363 
1364     /* Selected guest interrupt file should be valid */
1365     if (virt && (!vgein || env->geilen < vgein)) {
1366         goto done;
1367     }
1368 
1369     /* Set/Clear CSRs always read zero */
1370     if (val) {
1371         *val = 0;
1372     }
1373 
1374     if (wr_mask) {
1375         /* Get interrupt number */
1376         new_val &= wr_mask;
1377 
1378         /* Find target interrupt pending/enable register */
1379         xlen = riscv_cpu_mxl_bits(env);
1380         isel = (new_val / xlen);
1381         isel *= (xlen / IMSIC_EIPx_BITS);
1382         isel += (pend) ? ISELECT_IMSIC_EIP0 : ISELECT_IMSIC_EIE0;
1383 
1384         /* Find the interrupt bit to be set/clear */
1385         wmask = ((target_ulong)1) << (new_val % xlen);
1386         nval = (set) ? wmask : 0;
1387 
1388         /* Call machine specific IMSIC register emulation */
1389         ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1390                                          AIA_MAKE_IREG(isel, priv, virt,
1391                                                        vgein, xlen),
1392                                          NULL, nval, wmask);
1393     } else {
1394         ret = 0;
1395     }
1396 
1397 done:
1398     if (ret) {
1399         return (riscv_cpu_virt_enabled(env) && virt) ?
1400                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1401     }
1402     return RISCV_EXCP_NONE;
1403 }
1404 
1405 static int rmw_xtopei(CPURISCVState *env, int csrno, target_ulong *val,
1406                       target_ulong new_val, target_ulong wr_mask)
1407 {
1408     bool virt;
1409     int ret = -EINVAL;
1410     target_ulong priv, vgein;
1411 
1412     /* Translate CSR number for VS-mode */
1413     csrno = aia_xlate_vs_csrno(env, csrno);
1414 
1415     /* Decode register details from CSR number */
1416     virt = false;
1417     switch (csrno) {
1418     case CSR_MTOPEI:
1419         priv = PRV_M;
1420         break;
1421     case CSR_STOPEI:
1422         priv = PRV_S;
1423         break;
1424     case CSR_VSTOPEI:
1425         priv = PRV_S;
1426         virt = true;
1427         break;
1428     default:
1429         goto done;
1430     };
1431 
1432     /* IMSIC CSRs only available when machine implements IMSIC. */
1433     if (!env->aia_ireg_rmw_fn[priv]) {
1434         goto done;
1435     }
1436 
1437     /* Find the selected guest interrupt file */
1438     vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0;
1439 
1440     /* Selected guest interrupt file should be valid */
1441     if (virt && (!vgein || env->geilen < vgein)) {
1442         goto done;
1443     }
1444 
1445     /* Call machine specific IMSIC register emulation for TOPEI */
1446     ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv],
1447                     AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
1448                                   riscv_cpu_mxl_bits(env)),
1449                     val, new_val, wr_mask);
1450 
1451 done:
1452     if (ret) {
1453         return (riscv_cpu_virt_enabled(env) && virt) ?
1454                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1455     }
1456     return RISCV_EXCP_NONE;
1457 }
1458 
1459 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
1460                                  target_ulong *val)
1461 {
1462     *val = env->mtvec;
1463     return RISCV_EXCP_NONE;
1464 }
1465 
1466 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
1467                                   target_ulong val)
1468 {
1469     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
1470     if ((val & 3) < 2) {
1471         env->mtvec = val;
1472     } else {
1473         qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
1474     }
1475     return RISCV_EXCP_NONE;
1476 }
1477 
1478 static RISCVException read_mcountinhibit(CPURISCVState *env, int csrno,
1479                                          target_ulong *val)
1480 {
1481     if (env->priv_ver < PRIV_VERSION_1_11_0) {
1482         return RISCV_EXCP_ILLEGAL_INST;
1483     }
1484 
1485     *val = env->mcountinhibit;
1486     return RISCV_EXCP_NONE;
1487 }
1488 
1489 static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno,
1490                                           target_ulong val)
1491 {
1492     if (env->priv_ver < PRIV_VERSION_1_11_0) {
1493         return RISCV_EXCP_ILLEGAL_INST;
1494     }
1495 
1496     env->mcountinhibit = val;
1497     return RISCV_EXCP_NONE;
1498 }
1499 
1500 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
1501                                       target_ulong *val)
1502 {
1503     *val = env->mcounteren;
1504     return RISCV_EXCP_NONE;
1505 }
1506 
1507 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
1508                                        target_ulong val)
1509 {
1510     env->mcounteren = val;
1511     return RISCV_EXCP_NONE;
1512 }
1513 
1514 /* Machine Trap Handling */
1515 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno,
1516                                          Int128 *val)
1517 {
1518     *val = int128_make128(env->mscratch, env->mscratchh);
1519     return RISCV_EXCP_NONE;
1520 }
1521 
1522 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno,
1523                                           Int128 val)
1524 {
1525     env->mscratch = int128_getlo(val);
1526     env->mscratchh = int128_gethi(val);
1527     return RISCV_EXCP_NONE;
1528 }
1529 
1530 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
1531                                     target_ulong *val)
1532 {
1533     *val = env->mscratch;
1534     return RISCV_EXCP_NONE;
1535 }
1536 
1537 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
1538                                      target_ulong val)
1539 {
1540     env->mscratch = val;
1541     return RISCV_EXCP_NONE;
1542 }
1543 
1544 static RISCVException read_mepc(CPURISCVState *env, int csrno,
1545                                      target_ulong *val)
1546 {
1547     *val = env->mepc;
1548     return RISCV_EXCP_NONE;
1549 }
1550 
1551 static RISCVException write_mepc(CPURISCVState *env, int csrno,
1552                                      target_ulong val)
1553 {
1554     env->mepc = val;
1555     return RISCV_EXCP_NONE;
1556 }
1557 
1558 static RISCVException read_mcause(CPURISCVState *env, int csrno,
1559                                      target_ulong *val)
1560 {
1561     *val = env->mcause;
1562     return RISCV_EXCP_NONE;
1563 }
1564 
1565 static RISCVException write_mcause(CPURISCVState *env, int csrno,
1566                                      target_ulong val)
1567 {
1568     env->mcause = val;
1569     return RISCV_EXCP_NONE;
1570 }
1571 
1572 static RISCVException read_mtval(CPURISCVState *env, int csrno,
1573                                  target_ulong *val)
1574 {
1575     *val = env->mtval;
1576     return RISCV_EXCP_NONE;
1577 }
1578 
1579 static RISCVException write_mtval(CPURISCVState *env, int csrno,
1580                                   target_ulong val)
1581 {
1582     env->mtval = val;
1583     return RISCV_EXCP_NONE;
1584 }
1585 
1586 /* Execution environment configuration setup */
1587 static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
1588                                  target_ulong *val)
1589 {
1590     *val = env->menvcfg;
1591     return RISCV_EXCP_NONE;
1592 }
1593 
1594 static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
1595                                   target_ulong val)
1596 {
1597     uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | MENVCFG_CBZE;
1598 
1599     if (riscv_cpu_mxl(env) == MXL_RV64) {
1600         mask |= MENVCFG_PBMTE | MENVCFG_STCE;
1601     }
1602     env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
1603 
1604     return RISCV_EXCP_NONE;
1605 }
1606 
1607 static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
1608                                  target_ulong *val)
1609 {
1610     *val = env->menvcfg >> 32;
1611     return RISCV_EXCP_NONE;
1612 }
1613 
1614 static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
1615                                   target_ulong val)
1616 {
1617     uint64_t mask = MENVCFG_PBMTE | MENVCFG_STCE;
1618     uint64_t valh = (uint64_t)val << 32;
1619 
1620     env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);
1621 
1622     return RISCV_EXCP_NONE;
1623 }
1624 
1625 static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
1626                                  target_ulong *val)
1627 {
1628     *val = env->senvcfg;
1629     return RISCV_EXCP_NONE;
1630 }
1631 
1632 static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
1633                                   target_ulong val)
1634 {
1635     uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE;
1636 
1637     env->senvcfg = (env->senvcfg & ~mask) | (val & mask);
1638 
1639     return RISCV_EXCP_NONE;
1640 }
1641 
1642 static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
1643                                  target_ulong *val)
1644 {
1645     *val = env->henvcfg;
1646     return RISCV_EXCP_NONE;
1647 }
1648 
1649 static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
1650                                   target_ulong val)
1651 {
1652     uint64_t mask = HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCFG_CBZE;
1653 
1654     if (riscv_cpu_mxl(env) == MXL_RV64) {
1655         mask |= HENVCFG_PBMTE | HENVCFG_STCE;
1656     }
1657 
1658     env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
1659 
1660     return RISCV_EXCP_NONE;
1661 }
1662 
1663 static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
1664                                  target_ulong *val)
1665 {
1666     *val = env->henvcfg >> 32;
1667     return RISCV_EXCP_NONE;
1668 }
1669 
1670 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
1671                                   target_ulong val)
1672 {
1673     uint64_t mask = HENVCFG_PBMTE | HENVCFG_STCE;
1674     uint64_t valh = (uint64_t)val << 32;
1675 
1676     env->henvcfg = (env->henvcfg & ~mask) | (valh & mask);
1677 
1678     return RISCV_EXCP_NONE;
1679 }
1680 
1681 static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
1682                                 uint64_t *ret_val,
1683                                 uint64_t new_val, uint64_t wr_mask)
1684 {
1685     RISCVCPU *cpu = env_archcpu(env);
1686     uint64_t old_mip, mask = wr_mask & delegable_ints;
1687     uint32_t gin;
1688 
1689     if (mask & MIP_SEIP) {
1690         env->software_seip = new_val & MIP_SEIP;
1691         new_val |= env->external_seip * MIP_SEIP;
1692     }
1693 
1694     if (mask) {
1695         old_mip = riscv_cpu_update_mip(cpu, mask, (new_val & mask));
1696     } else {
1697         old_mip = env->mip;
1698     }
1699 
1700     if (csrno != CSR_HVIP) {
1701         gin = get_field(env->hstatus, HSTATUS_VGEIN);
1702         old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0;
1703     }
1704 
1705     if (ret_val) {
1706         *ret_val = old_mip;
1707     }
1708 
1709     return RISCV_EXCP_NONE;
1710 }
1711 
1712 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
1713                               target_ulong *ret_val,
1714                               target_ulong new_val, target_ulong wr_mask)
1715 {
1716     uint64_t rval;
1717     RISCVException ret;
1718 
1719     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask);
1720     if (ret_val) {
1721         *ret_val = rval;
1722     }
1723 
1724     return ret;
1725 }
1726 
1727 static RISCVException rmw_miph(CPURISCVState *env, int csrno,
1728                                target_ulong *ret_val,
1729                                target_ulong new_val, target_ulong wr_mask)
1730 {
1731     uint64_t rval;
1732     RISCVException ret;
1733 
1734     ret = rmw_mip64(env, csrno, &rval,
1735         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1736     if (ret_val) {
1737         *ret_val = rval >> 32;
1738     }
1739 
1740     return ret;
1741 }
1742 
1743 /* Supervisor Trap Setup */
1744 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,
1745                                         Int128 *val)
1746 {
1747     uint64_t mask = sstatus_v1_10_mask;
1748     uint64_t sstatus = env->mstatus & mask;
1749     if (env->xl != MXL_RV32 || env->debugger) {
1750         mask |= SSTATUS64_UXL;
1751     }
1752 
1753     *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus));
1754     return RISCV_EXCP_NONE;
1755 }
1756 
1757 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
1758                                    target_ulong *val)
1759 {
1760     target_ulong mask = (sstatus_v1_10_mask);
1761     if (env->xl != MXL_RV32 || env->debugger) {
1762         mask |= SSTATUS64_UXL;
1763     }
1764     /* TODO: Use SXL not MXL. */
1765     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
1766     return RISCV_EXCP_NONE;
1767 }
1768 
1769 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
1770                                     target_ulong val)
1771 {
1772     target_ulong mask = (sstatus_v1_10_mask);
1773 
1774     if (env->xl != MXL_RV32 || env->debugger) {
1775         if ((val & SSTATUS64_UXL) != 0) {
1776             mask |= SSTATUS64_UXL;
1777         }
1778     }
1779     target_ulong newval = (env->mstatus & ~mask) | (val & mask);
1780     return write_mstatus(env, CSR_MSTATUS, newval);
1781 }
1782 
1783 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno,
1784                                  uint64_t *ret_val,
1785                                  uint64_t new_val, uint64_t wr_mask)
1786 {
1787     RISCVException ret;
1788     uint64_t rval, vsbits, mask = env->hideleg & VS_MODE_INTERRUPTS;
1789 
1790     /* Bring VS-level bits to correct position */
1791     vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
1792     new_val &= ~(VS_MODE_INTERRUPTS >> 1);
1793     new_val |= vsbits << 1;
1794     vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
1795     wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
1796     wr_mask |= vsbits << 1;
1797 
1798     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & mask);
1799     if (ret_val) {
1800         rval &= mask;
1801         vsbits = rval & VS_MODE_INTERRUPTS;
1802         rval &= ~VS_MODE_INTERRUPTS;
1803         *ret_val = rval | (vsbits >> 1);
1804     }
1805 
1806     return ret;
1807 }
1808 
1809 static RISCVException rmw_vsie(CPURISCVState *env, int csrno,
1810                                target_ulong *ret_val,
1811                                target_ulong new_val, target_ulong wr_mask)
1812 {
1813     uint64_t rval;
1814     RISCVException ret;
1815 
1816     ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask);
1817     if (ret_val) {
1818         *ret_val = rval;
1819     }
1820 
1821     return ret;
1822 }
1823 
1824 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno,
1825                                 target_ulong *ret_val,
1826                                 target_ulong new_val, target_ulong wr_mask)
1827 {
1828     uint64_t rval;
1829     RISCVException ret;
1830 
1831     ret = rmw_vsie64(env, csrno, &rval,
1832         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1833     if (ret_val) {
1834         *ret_val = rval >> 32;
1835     }
1836 
1837     return ret;
1838 }
1839 
1840 static RISCVException rmw_sie64(CPURISCVState *env, int csrno,
1841                                 uint64_t *ret_val,
1842                                 uint64_t new_val, uint64_t wr_mask)
1843 {
1844     RISCVException ret;
1845     uint64_t mask = env->mideleg & S_MODE_INTERRUPTS;
1846 
1847     if (riscv_cpu_virt_enabled(env)) {
1848         if (env->hvictl & HVICTL_VTI) {
1849             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1850         }
1851         ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask);
1852     } else {
1853         ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & mask);
1854     }
1855 
1856     if (ret_val) {
1857         *ret_val &= mask;
1858     }
1859 
1860     return ret;
1861 }
1862 
1863 static RISCVException rmw_sie(CPURISCVState *env, int csrno,
1864                               target_ulong *ret_val,
1865                               target_ulong new_val, target_ulong wr_mask)
1866 {
1867     uint64_t rval;
1868     RISCVException ret;
1869 
1870     ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask);
1871     if (ret == RISCV_EXCP_NONE && ret_val) {
1872         *ret_val = rval;
1873     }
1874 
1875     return ret;
1876 }
1877 
1878 static RISCVException rmw_sieh(CPURISCVState *env, int csrno,
1879                                target_ulong *ret_val,
1880                                target_ulong new_val, target_ulong wr_mask)
1881 {
1882     uint64_t rval;
1883     RISCVException ret;
1884 
1885     ret = rmw_sie64(env, csrno, &rval,
1886         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1887     if (ret_val) {
1888         *ret_val = rval >> 32;
1889     }
1890 
1891     return ret;
1892 }
1893 
1894 static RISCVException read_stvec(CPURISCVState *env, int csrno,
1895                                  target_ulong *val)
1896 {
1897     *val = env->stvec;
1898     return RISCV_EXCP_NONE;
1899 }
1900 
1901 static RISCVException write_stvec(CPURISCVState *env, int csrno,
1902                                   target_ulong val)
1903 {
1904     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
1905     if ((val & 3) < 2) {
1906         env->stvec = val;
1907     } else {
1908         qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
1909     }
1910     return RISCV_EXCP_NONE;
1911 }
1912 
1913 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
1914                                       target_ulong *val)
1915 {
1916     *val = env->scounteren;
1917     return RISCV_EXCP_NONE;
1918 }
1919 
1920 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
1921                                        target_ulong val)
1922 {
1923     env->scounteren = val;
1924     return RISCV_EXCP_NONE;
1925 }
1926 
1927 /* Supervisor Trap Handling */
1928 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno,
1929                                          Int128 *val)
1930 {
1931     *val = int128_make128(env->sscratch, env->sscratchh);
1932     return RISCV_EXCP_NONE;
1933 }
1934 
1935 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno,
1936                                           Int128 val)
1937 {
1938     env->sscratch = int128_getlo(val);
1939     env->sscratchh = int128_gethi(val);
1940     return RISCV_EXCP_NONE;
1941 }
1942 
1943 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
1944                                     target_ulong *val)
1945 {
1946     *val = env->sscratch;
1947     return RISCV_EXCP_NONE;
1948 }
1949 
1950 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
1951                                      target_ulong val)
1952 {
1953     env->sscratch = val;
1954     return RISCV_EXCP_NONE;
1955 }
1956 
1957 static RISCVException read_sepc(CPURISCVState *env, int csrno,
1958                                 target_ulong *val)
1959 {
1960     *val = env->sepc;
1961     return RISCV_EXCP_NONE;
1962 }
1963 
1964 static RISCVException write_sepc(CPURISCVState *env, int csrno,
1965                                  target_ulong val)
1966 {
1967     env->sepc = val;
1968     return RISCV_EXCP_NONE;
1969 }
1970 
1971 static RISCVException read_scause(CPURISCVState *env, int csrno,
1972                                   target_ulong *val)
1973 {
1974     *val = env->scause;
1975     return RISCV_EXCP_NONE;
1976 }
1977 
1978 static RISCVException write_scause(CPURISCVState *env, int csrno,
1979                                    target_ulong val)
1980 {
1981     env->scause = val;
1982     return RISCV_EXCP_NONE;
1983 }
1984 
1985 static RISCVException read_stval(CPURISCVState *env, int csrno,
1986                                  target_ulong *val)
1987 {
1988     *val = env->stval;
1989     return RISCV_EXCP_NONE;
1990 }
1991 
1992 static RISCVException write_stval(CPURISCVState *env, int csrno,
1993                                   target_ulong val)
1994 {
1995     env->stval = val;
1996     return RISCV_EXCP_NONE;
1997 }
1998 
1999 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno,
2000                                  uint64_t *ret_val,
2001                                  uint64_t new_val, uint64_t wr_mask)
2002 {
2003     RISCVException ret;
2004     uint64_t rval, vsbits, mask = env->hideleg & vsip_writable_mask;
2005 
2006     /* Bring VS-level bits to correct position */
2007     vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
2008     new_val &= ~(VS_MODE_INTERRUPTS >> 1);
2009     new_val |= vsbits << 1;
2010     vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
2011     wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
2012     wr_mask |= vsbits << 1;
2013 
2014     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask & mask);
2015     if (ret_val) {
2016         rval &= mask;
2017         vsbits = rval & VS_MODE_INTERRUPTS;
2018         rval &= ~VS_MODE_INTERRUPTS;
2019         *ret_val = rval | (vsbits >> 1);
2020     }
2021 
2022     return ret;
2023 }
2024 
2025 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
2026                                target_ulong *ret_val,
2027                                target_ulong new_val, target_ulong wr_mask)
2028 {
2029     uint64_t rval;
2030     RISCVException ret;
2031 
2032     ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask);
2033     if (ret_val) {
2034         *ret_val = rval;
2035     }
2036 
2037     return ret;
2038 }
2039 
2040 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno,
2041                                 target_ulong *ret_val,
2042                                 target_ulong new_val, target_ulong wr_mask)
2043 {
2044     uint64_t rval;
2045     RISCVException ret;
2046 
2047     ret = rmw_vsip64(env, csrno, &rval,
2048         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2049     if (ret_val) {
2050         *ret_val = rval >> 32;
2051     }
2052 
2053     return ret;
2054 }
2055 
2056 static RISCVException rmw_sip64(CPURISCVState *env, int csrno,
2057                                 uint64_t *ret_val,
2058                                 uint64_t new_val, uint64_t wr_mask)
2059 {
2060     RISCVException ret;
2061     uint64_t mask = env->mideleg & sip_writable_mask;
2062 
2063     if (riscv_cpu_virt_enabled(env)) {
2064         if (env->hvictl & HVICTL_VTI) {
2065             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
2066         }
2067         ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask);
2068     } else {
2069         ret = rmw_mip64(env, csrno, ret_val, new_val, wr_mask & mask);
2070     }
2071 
2072     if (ret_val) {
2073         *ret_val &= env->mideleg & S_MODE_INTERRUPTS;
2074     }
2075 
2076     return ret;
2077 }
2078 
2079 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
2080                               target_ulong *ret_val,
2081                               target_ulong new_val, target_ulong wr_mask)
2082 {
2083     uint64_t rval;
2084     RISCVException ret;
2085 
2086     ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask);
2087     if (ret_val) {
2088         *ret_val = rval;
2089     }
2090 
2091     return ret;
2092 }
2093 
2094 static RISCVException rmw_siph(CPURISCVState *env, int csrno,
2095                                target_ulong *ret_val,
2096                                target_ulong new_val, target_ulong wr_mask)
2097 {
2098     uint64_t rval;
2099     RISCVException ret;
2100 
2101     ret = rmw_sip64(env, csrno, &rval,
2102         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2103     if (ret_val) {
2104         *ret_val = rval >> 32;
2105     }
2106 
2107     return ret;
2108 }
2109 
2110 /* Supervisor Protection and Translation */
2111 static RISCVException read_satp(CPURISCVState *env, int csrno,
2112                                 target_ulong *val)
2113 {
2114     if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
2115         *val = 0;
2116         return RISCV_EXCP_NONE;
2117     }
2118 
2119     if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
2120         return RISCV_EXCP_ILLEGAL_INST;
2121     } else {
2122         *val = env->satp;
2123     }
2124 
2125     return RISCV_EXCP_NONE;
2126 }
2127 
2128 static RISCVException write_satp(CPURISCVState *env, int csrno,
2129                                  target_ulong val)
2130 {
2131     target_ulong vm, mask;
2132 
2133     if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
2134         return RISCV_EXCP_NONE;
2135     }
2136 
2137     if (riscv_cpu_mxl(env) == MXL_RV32) {
2138         vm = validate_vm(env, get_field(val, SATP32_MODE));
2139         mask = (val ^ env->satp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
2140     } else {
2141         vm = validate_vm(env, get_field(val, SATP64_MODE));
2142         mask = (val ^ env->satp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
2143     }
2144 
2145     if (vm && mask) {
2146         if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
2147             return RISCV_EXCP_ILLEGAL_INST;
2148         } else {
2149             /*
2150              * The ISA defines SATP.MODE=Bare as "no translation", but we still
2151              * pass these through QEMU's TLB emulation as it improves
2152              * performance.  Flushing the TLB on SATP writes with paging
2153              * enabled avoids leaking those invalid cached mappings.
2154              */
2155             tlb_flush(env_cpu(env));
2156             env->satp = val;
2157         }
2158     }
2159     return RISCV_EXCP_NONE;
2160 }
2161 
2162 static int read_vstopi(CPURISCVState *env, int csrno, target_ulong *val)
2163 {
2164     int irq, ret;
2165     target_ulong topei;
2166     uint64_t vseip, vsgein;
2167     uint32_t iid, iprio, hviid, hviprio, gein;
2168     uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS];
2169 
2170     gein = get_field(env->hstatus, HSTATUS_VGEIN);
2171     hviid = get_field(env->hvictl, HVICTL_IID);
2172     hviprio = get_field(env->hvictl, HVICTL_IPRIO);
2173 
2174     if (gein) {
2175         vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0;
2176         vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP;
2177         if (gein <= env->geilen && vseip) {
2178             siid[scount] = IRQ_S_EXT;
2179             siprio[scount] = IPRIO_MMAXIPRIO + 1;
2180             if (env->aia_ireg_rmw_fn[PRV_S]) {
2181                 /*
2182                  * Call machine specific IMSIC register emulation for
2183                  * reading TOPEI.
2184                  */
2185                 ret = env->aia_ireg_rmw_fn[PRV_S](
2186                         env->aia_ireg_rmw_fn_arg[PRV_S],
2187                         AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein,
2188                                       riscv_cpu_mxl_bits(env)),
2189                         &topei, 0, 0);
2190                 if (!ret && topei) {
2191                     siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK;
2192                 }
2193             }
2194             scount++;
2195         }
2196     } else {
2197         if (hviid == IRQ_S_EXT && hviprio) {
2198             siid[scount] = IRQ_S_EXT;
2199             siprio[scount] = hviprio;
2200             scount++;
2201         }
2202     }
2203 
2204     if (env->hvictl & HVICTL_VTI) {
2205         if (hviid != IRQ_S_EXT) {
2206             siid[scount] = hviid;
2207             siprio[scount] = hviprio;
2208             scount++;
2209         }
2210     } else {
2211         irq = riscv_cpu_vsirq_pending(env);
2212         if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) {
2213             siid[scount] = irq;
2214             siprio[scount] = env->hviprio[irq];
2215             scount++;
2216         }
2217     }
2218 
2219     iid = 0;
2220     iprio = UINT_MAX;
2221     for (s = 0; s < scount; s++) {
2222         if (siprio[s] < iprio) {
2223             iid = siid[s];
2224             iprio = siprio[s];
2225         }
2226     }
2227 
2228     if (iid) {
2229         if (env->hvictl & HVICTL_IPRIOM) {
2230             if (iprio > IPRIO_MMAXIPRIO) {
2231                 iprio = IPRIO_MMAXIPRIO;
2232             }
2233             if (!iprio) {
2234                 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) {
2235                     iprio = IPRIO_MMAXIPRIO;
2236                 }
2237             }
2238         } else {
2239             iprio = 1;
2240         }
2241     } else {
2242         iprio = 0;
2243     }
2244 
2245     *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT;
2246     *val |= iprio;
2247     return RISCV_EXCP_NONE;
2248 }
2249 
2250 static int read_stopi(CPURISCVState *env, int csrno, target_ulong *val)
2251 {
2252     int irq;
2253     uint8_t iprio;
2254 
2255     if (riscv_cpu_virt_enabled(env)) {
2256         return read_vstopi(env, CSR_VSTOPI, val);
2257     }
2258 
2259     irq = riscv_cpu_sirq_pending(env);
2260     if (irq <= 0 || irq > 63) {
2261         *val = 0;
2262     } else {
2263         iprio = env->siprio[irq];
2264         if (!iprio) {
2265             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) {
2266                 iprio = IPRIO_MMAXIPRIO;
2267            }
2268         }
2269         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
2270         *val |= iprio;
2271     }
2272 
2273     return RISCV_EXCP_NONE;
2274 }
2275 
2276 /* Hypervisor Extensions */
2277 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
2278                                    target_ulong *val)
2279 {
2280     *val = env->hstatus;
2281     if (riscv_cpu_mxl(env) != MXL_RV32) {
2282         /* We only support 64-bit VSXL */
2283         *val = set_field(*val, HSTATUS_VSXL, 2);
2284     }
2285     /* We only support little endian */
2286     *val = set_field(*val, HSTATUS_VSBE, 0);
2287     return RISCV_EXCP_NONE;
2288 }
2289 
2290 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
2291                                     target_ulong val)
2292 {
2293     env->hstatus = val;
2294     if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
2295         qemu_log_mask(LOG_UNIMP, "QEMU does not support mixed HSXLEN options.");
2296     }
2297     if (get_field(val, HSTATUS_VSBE) != 0) {
2298         qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
2299     }
2300     return RISCV_EXCP_NONE;
2301 }
2302 
2303 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
2304                                    target_ulong *val)
2305 {
2306     *val = env->hedeleg;
2307     return RISCV_EXCP_NONE;
2308 }
2309 
2310 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
2311                                     target_ulong val)
2312 {
2313     env->hedeleg = val & vs_delegable_excps;
2314     return RISCV_EXCP_NONE;
2315 }
2316 
2317 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno,
2318                                     uint64_t *ret_val,
2319                                     uint64_t new_val, uint64_t wr_mask)
2320 {
2321     uint64_t mask = wr_mask & vs_delegable_ints;
2322 
2323     if (ret_val) {
2324         *ret_val = env->hideleg & vs_delegable_ints;
2325     }
2326 
2327     env->hideleg = (env->hideleg & ~mask) | (new_val & mask);
2328     return RISCV_EXCP_NONE;
2329 }
2330 
2331 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno,
2332                                   target_ulong *ret_val,
2333                                   target_ulong new_val, target_ulong wr_mask)
2334 {
2335     uint64_t rval;
2336     RISCVException ret;
2337 
2338     ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask);
2339     if (ret_val) {
2340         *ret_val = rval;
2341     }
2342 
2343     return ret;
2344 }
2345 
2346 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno,
2347                                    target_ulong *ret_val,
2348                                    target_ulong new_val, target_ulong wr_mask)
2349 {
2350     uint64_t rval;
2351     RISCVException ret;
2352 
2353     ret = rmw_hideleg64(env, csrno, &rval,
2354         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2355     if (ret_val) {
2356         *ret_val = rval >> 32;
2357     }
2358 
2359     return ret;
2360 }
2361 
2362 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
2363                                  uint64_t *ret_val,
2364                                  uint64_t new_val, uint64_t wr_mask)
2365 {
2366     RISCVException ret;
2367 
2368     ret = rmw_mip64(env, csrno, ret_val, new_val,
2369                     wr_mask & hvip_writable_mask);
2370     if (ret_val) {
2371         *ret_val &= VS_MODE_INTERRUPTS;
2372     }
2373 
2374     return ret;
2375 }
2376 
2377 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
2378                                target_ulong *ret_val,
2379                                target_ulong new_val, target_ulong wr_mask)
2380 {
2381     uint64_t rval;
2382     RISCVException ret;
2383 
2384     ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask);
2385     if (ret_val) {
2386         *ret_val = rval;
2387     }
2388 
2389     return ret;
2390 }
2391 
2392 static RISCVException rmw_hviph(CPURISCVState *env, int csrno,
2393                                 target_ulong *ret_val,
2394                                 target_ulong new_val, target_ulong wr_mask)
2395 {
2396     uint64_t rval;
2397     RISCVException ret;
2398 
2399     ret = rmw_hvip64(env, csrno, &rval,
2400         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
2401     if (ret_val) {
2402         *ret_val = rval >> 32;
2403     }
2404 
2405     return ret;
2406 }
2407 
2408 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
2409                               target_ulong *ret_value,
2410                               target_ulong new_value, target_ulong write_mask)
2411 {
2412     int ret = rmw_mip(env, csrno, ret_value, new_value,
2413                       write_mask & hip_writable_mask);
2414 
2415     if (ret_value) {
2416         *ret_value &= HS_MODE_INTERRUPTS;
2417     }
2418     return ret;
2419 }
2420 
2421 static RISCVException rmw_hie(CPURISCVState *env, int csrno,
2422                               target_ulong *ret_val,
2423                               target_ulong new_val, target_ulong wr_mask)
2424 {
2425     uint64_t rval;
2426     RISCVException ret;
2427 
2428     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS);
2429     if (ret_val) {
2430         *ret_val = rval & HS_MODE_INTERRUPTS;
2431     }
2432 
2433     return ret;
2434 }
2435 
2436 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
2437                                       target_ulong *val)
2438 {
2439     *val = env->hcounteren;
2440     return RISCV_EXCP_NONE;
2441 }
2442 
2443 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
2444                                        target_ulong val)
2445 {
2446     env->hcounteren = val;
2447     return RISCV_EXCP_NONE;
2448 }
2449 
2450 static RISCVException read_hgeie(CPURISCVState *env, int csrno,
2451                                  target_ulong *val)
2452 {
2453     if (val) {
2454         *val = env->hgeie;
2455     }
2456     return RISCV_EXCP_NONE;
2457 }
2458 
2459 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
2460                                   target_ulong val)
2461 {
2462     /* Only GEILEN:1 bits implemented and BIT0 is never implemented */
2463     val &= ((((target_ulong)1) << env->geilen) - 1) << 1;
2464     env->hgeie = val;
2465     /* Update mip.SGEIP bit */
2466     riscv_cpu_update_mip(env_archcpu(env), MIP_SGEIP,
2467                          BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
2468     return RISCV_EXCP_NONE;
2469 }
2470 
2471 static RISCVException read_htval(CPURISCVState *env, int csrno,
2472                                  target_ulong *val)
2473 {
2474     *val = env->htval;
2475     return RISCV_EXCP_NONE;
2476 }
2477 
2478 static RISCVException write_htval(CPURISCVState *env, int csrno,
2479                                   target_ulong val)
2480 {
2481     env->htval = val;
2482     return RISCV_EXCP_NONE;
2483 }
2484 
2485 static RISCVException read_htinst(CPURISCVState *env, int csrno,
2486                                   target_ulong *val)
2487 {
2488     *val = env->htinst;
2489     return RISCV_EXCP_NONE;
2490 }
2491 
2492 static RISCVException write_htinst(CPURISCVState *env, int csrno,
2493                                    target_ulong val)
2494 {
2495     return RISCV_EXCP_NONE;
2496 }
2497 
2498 static RISCVException read_hgeip(CPURISCVState *env, int csrno,
2499                                  target_ulong *val)
2500 {
2501     if (val) {
2502         *val = env->hgeip;
2503     }
2504     return RISCV_EXCP_NONE;
2505 }
2506 
2507 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
2508                                  target_ulong *val)
2509 {
2510     *val = env->hgatp;
2511     return RISCV_EXCP_NONE;
2512 }
2513 
2514 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
2515                                   target_ulong val)
2516 {
2517     env->hgatp = val;
2518     return RISCV_EXCP_NONE;
2519 }
2520 
2521 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
2522                                       target_ulong *val)
2523 {
2524     if (!env->rdtime_fn) {
2525         return RISCV_EXCP_ILLEGAL_INST;
2526     }
2527 
2528     *val = env->htimedelta;
2529     return RISCV_EXCP_NONE;
2530 }
2531 
2532 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
2533                                        target_ulong val)
2534 {
2535     if (!env->rdtime_fn) {
2536         return RISCV_EXCP_ILLEGAL_INST;
2537     }
2538 
2539     if (riscv_cpu_mxl(env) == MXL_RV32) {
2540         env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
2541     } else {
2542         env->htimedelta = val;
2543     }
2544     return RISCV_EXCP_NONE;
2545 }
2546 
2547 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
2548                                        target_ulong *val)
2549 {
2550     if (!env->rdtime_fn) {
2551         return RISCV_EXCP_ILLEGAL_INST;
2552     }
2553 
2554     *val = env->htimedelta >> 32;
2555     return RISCV_EXCP_NONE;
2556 }
2557 
2558 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
2559                                         target_ulong val)
2560 {
2561     if (!env->rdtime_fn) {
2562         return RISCV_EXCP_ILLEGAL_INST;
2563     }
2564 
2565     env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
2566     return RISCV_EXCP_NONE;
2567 }
2568 
2569 static int read_hvictl(CPURISCVState *env, int csrno, target_ulong *val)
2570 {
2571     *val = env->hvictl;
2572     return RISCV_EXCP_NONE;
2573 }
2574 
2575 static int write_hvictl(CPURISCVState *env, int csrno, target_ulong val)
2576 {
2577     env->hvictl = val & HVICTL_VALID_MASK;
2578     return RISCV_EXCP_NONE;
2579 }
2580 
2581 static int read_hvipriox(CPURISCVState *env, int first_index,
2582                          uint8_t *iprio, target_ulong *val)
2583 {
2584     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
2585 
2586     /* First index has to be a multiple of number of irqs per register */
2587     if (first_index % num_irqs) {
2588         return (riscv_cpu_virt_enabled(env)) ?
2589                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
2590     }
2591 
2592     /* Fill-up return value */
2593     *val = 0;
2594     for (i = 0; i < num_irqs; i++) {
2595         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
2596             continue;
2597         }
2598         if (rdzero) {
2599             continue;
2600         }
2601         *val |= ((target_ulong)iprio[irq]) << (i * 8);
2602     }
2603 
2604     return RISCV_EXCP_NONE;
2605 }
2606 
2607 static int write_hvipriox(CPURISCVState *env, int first_index,
2608                           uint8_t *iprio, target_ulong val)
2609 {
2610     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
2611 
2612     /* First index has to be a multiple of number of irqs per register */
2613     if (first_index % num_irqs) {
2614         return (riscv_cpu_virt_enabled(env)) ?
2615                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
2616     }
2617 
2618     /* Fill-up priority arrary */
2619     for (i = 0; i < num_irqs; i++) {
2620         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
2621             continue;
2622         }
2623         if (rdzero) {
2624             iprio[irq] = 0;
2625         } else {
2626             iprio[irq] = (val >> (i * 8)) & 0xff;
2627         }
2628     }
2629 
2630     return RISCV_EXCP_NONE;
2631 }
2632 
2633 static int read_hviprio1(CPURISCVState *env, int csrno, target_ulong *val)
2634 {
2635     return read_hvipriox(env, 0, env->hviprio, val);
2636 }
2637 
2638 static int write_hviprio1(CPURISCVState *env, int csrno, target_ulong val)
2639 {
2640     return write_hvipriox(env, 0, env->hviprio, val);
2641 }
2642 
2643 static int read_hviprio1h(CPURISCVState *env, int csrno, target_ulong *val)
2644 {
2645     return read_hvipriox(env, 4, env->hviprio, val);
2646 }
2647 
2648 static int write_hviprio1h(CPURISCVState *env, int csrno, target_ulong val)
2649 {
2650     return write_hvipriox(env, 4, env->hviprio, val);
2651 }
2652 
2653 static int read_hviprio2(CPURISCVState *env, int csrno, target_ulong *val)
2654 {
2655     return read_hvipriox(env, 8, env->hviprio, val);
2656 }
2657 
2658 static int write_hviprio2(CPURISCVState *env, int csrno, target_ulong val)
2659 {
2660     return write_hvipriox(env, 8, env->hviprio, val);
2661 }
2662 
2663 static int read_hviprio2h(CPURISCVState *env, int csrno, target_ulong *val)
2664 {
2665     return read_hvipriox(env, 12, env->hviprio, val);
2666 }
2667 
2668 static int write_hviprio2h(CPURISCVState *env, int csrno, target_ulong val)
2669 {
2670     return write_hvipriox(env, 12, env->hviprio, val);
2671 }
2672 
2673 /* Virtual CSR Registers */
2674 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
2675                                     target_ulong *val)
2676 {
2677     *val = env->vsstatus;
2678     return RISCV_EXCP_NONE;
2679 }
2680 
2681 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
2682                                      target_ulong val)
2683 {
2684     uint64_t mask = (target_ulong)-1;
2685     if ((val & VSSTATUS64_UXL) == 0) {
2686         mask &= ~VSSTATUS64_UXL;
2687     }
2688     env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
2689     return RISCV_EXCP_NONE;
2690 }
2691 
2692 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val)
2693 {
2694     *val = env->vstvec;
2695     return RISCV_EXCP_NONE;
2696 }
2697 
2698 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
2699                                    target_ulong val)
2700 {
2701     env->vstvec = val;
2702     return RISCV_EXCP_NONE;
2703 }
2704 
2705 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
2706                                      target_ulong *val)
2707 {
2708     *val = env->vsscratch;
2709     return RISCV_EXCP_NONE;
2710 }
2711 
2712 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
2713                                       target_ulong val)
2714 {
2715     env->vsscratch = val;
2716     return RISCV_EXCP_NONE;
2717 }
2718 
2719 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
2720                                  target_ulong *val)
2721 {
2722     *val = env->vsepc;
2723     return RISCV_EXCP_NONE;
2724 }
2725 
2726 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
2727                                   target_ulong val)
2728 {
2729     env->vsepc = val;
2730     return RISCV_EXCP_NONE;
2731 }
2732 
2733 static RISCVException read_vscause(CPURISCVState *env, int csrno,
2734                                    target_ulong *val)
2735 {
2736     *val = env->vscause;
2737     return RISCV_EXCP_NONE;
2738 }
2739 
2740 static RISCVException write_vscause(CPURISCVState *env, int csrno,
2741                                     target_ulong val)
2742 {
2743     env->vscause = val;
2744     return RISCV_EXCP_NONE;
2745 }
2746 
2747 static RISCVException read_vstval(CPURISCVState *env, int csrno,
2748                                   target_ulong *val)
2749 {
2750     *val = env->vstval;
2751     return RISCV_EXCP_NONE;
2752 }
2753 
2754 static RISCVException write_vstval(CPURISCVState *env, int csrno,
2755                                    target_ulong val)
2756 {
2757     env->vstval = val;
2758     return RISCV_EXCP_NONE;
2759 }
2760 
2761 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
2762                                  target_ulong *val)
2763 {
2764     *val = env->vsatp;
2765     return RISCV_EXCP_NONE;
2766 }
2767 
2768 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
2769                                   target_ulong val)
2770 {
2771     env->vsatp = val;
2772     return RISCV_EXCP_NONE;
2773 }
2774 
2775 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
2776                                   target_ulong *val)
2777 {
2778     *val = env->mtval2;
2779     return RISCV_EXCP_NONE;
2780 }
2781 
2782 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
2783                                    target_ulong val)
2784 {
2785     env->mtval2 = val;
2786     return RISCV_EXCP_NONE;
2787 }
2788 
2789 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
2790                                   target_ulong *val)
2791 {
2792     *val = env->mtinst;
2793     return RISCV_EXCP_NONE;
2794 }
2795 
2796 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
2797                                    target_ulong val)
2798 {
2799     env->mtinst = val;
2800     return RISCV_EXCP_NONE;
2801 }
2802 
2803 /* Physical Memory Protection */
2804 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
2805                                    target_ulong *val)
2806 {
2807     *val = mseccfg_csr_read(env);
2808     return RISCV_EXCP_NONE;
2809 }
2810 
2811 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
2812                          target_ulong val)
2813 {
2814     mseccfg_csr_write(env, val);
2815     return RISCV_EXCP_NONE;
2816 }
2817 
2818 static bool check_pmp_reg_index(CPURISCVState *env, uint32_t reg_index)
2819 {
2820     /* TODO: RV128 restriction check */
2821     if ((reg_index & 1) && (riscv_cpu_mxl(env) == MXL_RV64)) {
2822         return false;
2823     }
2824     return true;
2825 }
2826 
2827 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
2828                                   target_ulong *val)
2829 {
2830     uint32_t reg_index = csrno - CSR_PMPCFG0;
2831 
2832     if (!check_pmp_reg_index(env, reg_index)) {
2833         return RISCV_EXCP_ILLEGAL_INST;
2834     }
2835     *val = pmpcfg_csr_read(env, csrno - CSR_PMPCFG0);
2836     return RISCV_EXCP_NONE;
2837 }
2838 
2839 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
2840                                    target_ulong val)
2841 {
2842     uint32_t reg_index = csrno - CSR_PMPCFG0;
2843 
2844     if (!check_pmp_reg_index(env, reg_index)) {
2845         return RISCV_EXCP_ILLEGAL_INST;
2846     }
2847     pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val);
2848     return RISCV_EXCP_NONE;
2849 }
2850 
2851 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
2852                                    target_ulong *val)
2853 {
2854     *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
2855     return RISCV_EXCP_NONE;
2856 }
2857 
2858 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
2859                                     target_ulong val)
2860 {
2861     pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
2862     return RISCV_EXCP_NONE;
2863 }
2864 
2865 static RISCVException read_tselect(CPURISCVState *env, int csrno,
2866                                    target_ulong *val)
2867 {
2868     *val = tselect_csr_read(env);
2869     return RISCV_EXCP_NONE;
2870 }
2871 
2872 static RISCVException write_tselect(CPURISCVState *env, int csrno,
2873                                     target_ulong val)
2874 {
2875     tselect_csr_write(env, val);
2876     return RISCV_EXCP_NONE;
2877 }
2878 
2879 static RISCVException read_tdata(CPURISCVState *env, int csrno,
2880                                  target_ulong *val)
2881 {
2882     /* return 0 in tdata1 to end the trigger enumeration */
2883     if (env->trigger_cur >= TRIGGER_NUM && csrno == CSR_TDATA1) {
2884         *val = 0;
2885         return RISCV_EXCP_NONE;
2886     }
2887 
2888     if (!tdata_available(env, csrno - CSR_TDATA1)) {
2889         return RISCV_EXCP_ILLEGAL_INST;
2890     }
2891 
2892     *val = tdata_csr_read(env, csrno - CSR_TDATA1);
2893     return RISCV_EXCP_NONE;
2894 }
2895 
2896 static RISCVException write_tdata(CPURISCVState *env, int csrno,
2897                                   target_ulong val)
2898 {
2899     if (!tdata_available(env, csrno - CSR_TDATA1)) {
2900         return RISCV_EXCP_ILLEGAL_INST;
2901     }
2902 
2903     tdata_csr_write(env, csrno - CSR_TDATA1, val);
2904     return RISCV_EXCP_NONE;
2905 }
2906 
2907 /*
2908  * Functions to access Pointer Masking feature registers
2909  * We have to check if current priv lvl could modify
2910  * csr in given mode
2911  */
2912 static bool check_pm_current_disabled(CPURISCVState *env, int csrno)
2913 {
2914     int csr_priv = get_field(csrno, 0x300);
2915     int pm_current;
2916 
2917     if (env->debugger) {
2918         return false;
2919     }
2920     /*
2921      * If priv lvls differ that means we're accessing csr from higher priv lvl,
2922      * so allow the access
2923      */
2924     if (env->priv != csr_priv) {
2925         return false;
2926     }
2927     switch (env->priv) {
2928     case PRV_M:
2929         pm_current = get_field(env->mmte, M_PM_CURRENT);
2930         break;
2931     case PRV_S:
2932         pm_current = get_field(env->mmte, S_PM_CURRENT);
2933         break;
2934     case PRV_U:
2935         pm_current = get_field(env->mmte, U_PM_CURRENT);
2936         break;
2937     default:
2938         g_assert_not_reached();
2939     }
2940     /* It's same priv lvl, so we allow to modify csr only if pm.current==1 */
2941     return !pm_current;
2942 }
2943 
2944 static RISCVException read_mmte(CPURISCVState *env, int csrno,
2945                                 target_ulong *val)
2946 {
2947     *val = env->mmte & MMTE_MASK;
2948     return RISCV_EXCP_NONE;
2949 }
2950 
2951 static RISCVException write_mmte(CPURISCVState *env, int csrno,
2952                                  target_ulong val)
2953 {
2954     uint64_t mstatus;
2955     target_ulong wpri_val = val & MMTE_MASK;
2956 
2957     if (val != wpri_val) {
2958         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s" TARGET_FMT_lx "\n",
2959                       "MMTE: WPRI violation written 0x", val,
2960                       "vs expected 0x", wpri_val);
2961     }
2962     /* for machine mode pm.current is hardwired to 1 */
2963     wpri_val |= MMTE_M_PM_CURRENT;
2964 
2965     /* hardwiring pm.instruction bit to 0, since it's not supported yet */
2966     wpri_val &= ~(MMTE_M_PM_INSN | MMTE_S_PM_INSN | MMTE_U_PM_INSN);
2967     env->mmte = wpri_val | PM_EXT_DIRTY;
2968     riscv_cpu_update_mask(env);
2969 
2970     /* Set XS and SD bits, since PM CSRs are dirty */
2971     mstatus = env->mstatus | MSTATUS_XS;
2972     write_mstatus(env, csrno, mstatus);
2973     return RISCV_EXCP_NONE;
2974 }
2975 
2976 static RISCVException read_smte(CPURISCVState *env, int csrno,
2977                                 target_ulong *val)
2978 {
2979     *val = env->mmte & SMTE_MASK;
2980     return RISCV_EXCP_NONE;
2981 }
2982 
2983 static RISCVException write_smte(CPURISCVState *env, int csrno,
2984                                  target_ulong val)
2985 {
2986     target_ulong wpri_val = val & SMTE_MASK;
2987 
2988     if (val != wpri_val) {
2989         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s" TARGET_FMT_lx "\n",
2990                       "SMTE: WPRI violation written 0x", val,
2991                       "vs expected 0x", wpri_val);
2992     }
2993 
2994     /* if pm.current==0 we can't modify current PM CSRs */
2995     if (check_pm_current_disabled(env, csrno)) {
2996         return RISCV_EXCP_NONE;
2997     }
2998 
2999     wpri_val |= (env->mmte & ~SMTE_MASK);
3000     write_mmte(env, csrno, wpri_val);
3001     return RISCV_EXCP_NONE;
3002 }
3003 
3004 static RISCVException read_umte(CPURISCVState *env, int csrno,
3005                                 target_ulong *val)
3006 {
3007     *val = env->mmte & UMTE_MASK;
3008     return RISCV_EXCP_NONE;
3009 }
3010 
3011 static RISCVException write_umte(CPURISCVState *env, int csrno,
3012                                  target_ulong val)
3013 {
3014     target_ulong wpri_val = val & UMTE_MASK;
3015 
3016     if (val != wpri_val) {
3017         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s" TARGET_FMT_lx "\n",
3018                       "UMTE: WPRI violation written 0x", val,
3019                       "vs expected 0x", wpri_val);
3020     }
3021 
3022     if (check_pm_current_disabled(env, csrno)) {
3023         return RISCV_EXCP_NONE;
3024     }
3025 
3026     wpri_val |= (env->mmte & ~UMTE_MASK);
3027     write_mmte(env, csrno, wpri_val);
3028     return RISCV_EXCP_NONE;
3029 }
3030 
3031 static RISCVException read_mpmmask(CPURISCVState *env, int csrno,
3032                                    target_ulong *val)
3033 {
3034     *val = env->mpmmask;
3035     return RISCV_EXCP_NONE;
3036 }
3037 
3038 static RISCVException write_mpmmask(CPURISCVState *env, int csrno,
3039                                     target_ulong val)
3040 {
3041     uint64_t mstatus;
3042 
3043     env->mpmmask = val;
3044     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
3045         env->cur_pmmask = val;
3046     }
3047     env->mmte |= PM_EXT_DIRTY;
3048 
3049     /* Set XS and SD bits, since PM CSRs are dirty */
3050     mstatus = env->mstatus | MSTATUS_XS;
3051     write_mstatus(env, csrno, mstatus);
3052     return RISCV_EXCP_NONE;
3053 }
3054 
3055 static RISCVException read_spmmask(CPURISCVState *env, int csrno,
3056                                    target_ulong *val)
3057 {
3058     *val = env->spmmask;
3059     return RISCV_EXCP_NONE;
3060 }
3061 
3062 static RISCVException write_spmmask(CPURISCVState *env, int csrno,
3063                                     target_ulong val)
3064 {
3065     uint64_t mstatus;
3066 
3067     /* if pm.current==0 we can't modify current PM CSRs */
3068     if (check_pm_current_disabled(env, csrno)) {
3069         return RISCV_EXCP_NONE;
3070     }
3071     env->spmmask = val;
3072     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
3073         env->cur_pmmask = val;
3074     }
3075     env->mmte |= PM_EXT_DIRTY;
3076 
3077     /* Set XS and SD bits, since PM CSRs are dirty */
3078     mstatus = env->mstatus | MSTATUS_XS;
3079     write_mstatus(env, csrno, mstatus);
3080     return RISCV_EXCP_NONE;
3081 }
3082 
3083 static RISCVException read_upmmask(CPURISCVState *env, int csrno,
3084                                    target_ulong *val)
3085 {
3086     *val = env->upmmask;
3087     return RISCV_EXCP_NONE;
3088 }
3089 
3090 static RISCVException write_upmmask(CPURISCVState *env, int csrno,
3091                                     target_ulong val)
3092 {
3093     uint64_t mstatus;
3094 
3095     /* if pm.current==0 we can't modify current PM CSRs */
3096     if (check_pm_current_disabled(env, csrno)) {
3097         return RISCV_EXCP_NONE;
3098     }
3099     env->upmmask = val;
3100     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
3101         env->cur_pmmask = val;
3102     }
3103     env->mmte |= PM_EXT_DIRTY;
3104 
3105     /* Set XS and SD bits, since PM CSRs are dirty */
3106     mstatus = env->mstatus | MSTATUS_XS;
3107     write_mstatus(env, csrno, mstatus);
3108     return RISCV_EXCP_NONE;
3109 }
3110 
3111 static RISCVException read_mpmbase(CPURISCVState *env, int csrno,
3112                                    target_ulong *val)
3113 {
3114     *val = env->mpmbase;
3115     return RISCV_EXCP_NONE;
3116 }
3117 
3118 static RISCVException write_mpmbase(CPURISCVState *env, int csrno,
3119                                     target_ulong val)
3120 {
3121     uint64_t mstatus;
3122 
3123     env->mpmbase = val;
3124     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
3125         env->cur_pmbase = val;
3126     }
3127     env->mmte |= PM_EXT_DIRTY;
3128 
3129     /* Set XS and SD bits, since PM CSRs are dirty */
3130     mstatus = env->mstatus | MSTATUS_XS;
3131     write_mstatus(env, csrno, mstatus);
3132     return RISCV_EXCP_NONE;
3133 }
3134 
3135 static RISCVException read_spmbase(CPURISCVState *env, int csrno,
3136                                    target_ulong *val)
3137 {
3138     *val = env->spmbase;
3139     return RISCV_EXCP_NONE;
3140 }
3141 
3142 static RISCVException write_spmbase(CPURISCVState *env, int csrno,
3143                                     target_ulong val)
3144 {
3145     uint64_t mstatus;
3146 
3147     /* if pm.current==0 we can't modify current PM CSRs */
3148     if (check_pm_current_disabled(env, csrno)) {
3149         return RISCV_EXCP_NONE;
3150     }
3151     env->spmbase = val;
3152     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
3153         env->cur_pmbase = val;
3154     }
3155     env->mmte |= PM_EXT_DIRTY;
3156 
3157     /* Set XS and SD bits, since PM CSRs are dirty */
3158     mstatus = env->mstatus | MSTATUS_XS;
3159     write_mstatus(env, csrno, mstatus);
3160     return RISCV_EXCP_NONE;
3161 }
3162 
3163 static RISCVException read_upmbase(CPURISCVState *env, int csrno,
3164                                    target_ulong *val)
3165 {
3166     *val = env->upmbase;
3167     return RISCV_EXCP_NONE;
3168 }
3169 
3170 static RISCVException write_upmbase(CPURISCVState *env, int csrno,
3171                                     target_ulong val)
3172 {
3173     uint64_t mstatus;
3174 
3175     /* if pm.current==0 we can't modify current PM CSRs */
3176     if (check_pm_current_disabled(env, csrno)) {
3177         return RISCV_EXCP_NONE;
3178     }
3179     env->upmbase = val;
3180     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
3181         env->cur_pmbase = val;
3182     }
3183     env->mmte |= PM_EXT_DIRTY;
3184 
3185     /* Set XS and SD bits, since PM CSRs are dirty */
3186     mstatus = env->mstatus | MSTATUS_XS;
3187     write_mstatus(env, csrno, mstatus);
3188     return RISCV_EXCP_NONE;
3189 }
3190 
3191 #endif
3192 
3193 /* Crypto Extension */
3194 static RISCVException rmw_seed(CPURISCVState *env, int csrno,
3195                                target_ulong *ret_value,
3196                                target_ulong new_value,
3197                                target_ulong write_mask)
3198 {
3199     uint16_t random_v;
3200     Error *random_e = NULL;
3201     int random_r;
3202     target_ulong rval;
3203 
3204     random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
3205     if (unlikely(random_r < 0)) {
3206         /*
3207          * Failed, for unknown reasons in the crypto subsystem.
3208          * The best we can do is log the reason and return a
3209          * failure indication to the guest.  There is no reason
3210          * we know to expect the failure to be transitory, so
3211          * indicate DEAD to avoid having the guest spin on WAIT.
3212          */
3213         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
3214                       __func__, error_get_pretty(random_e));
3215         error_free(random_e);
3216         rval = SEED_OPST_DEAD;
3217     } else {
3218         rval = random_v | SEED_OPST_ES16;
3219     }
3220 
3221     if (ret_value) {
3222         *ret_value = rval;
3223     }
3224 
3225     return RISCV_EXCP_NONE;
3226 }
3227 
3228 /*
3229  * riscv_csrrw - read and/or update control and status register
3230  *
3231  * csrr   <->  riscv_csrrw(env, csrno, ret_value, 0, 0);
3232  * csrrw  <->  riscv_csrrw(env, csrno, ret_value, value, -1);
3233  * csrrs  <->  riscv_csrrw(env, csrno, ret_value, -1, value);
3234  * csrrc  <->  riscv_csrrw(env, csrno, ret_value, 0, value);
3235  */
3236 
3237 static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
3238                                                int csrno,
3239                                                bool write_mask,
3240                                                RISCVCPU *cpu)
3241 {
3242     /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
3243     int read_only = get_field(csrno, 0xC00) == 3;
3244     int csr_min_priv = csr_ops[csrno].min_priv_ver;
3245 #if !defined(CONFIG_USER_ONLY)
3246     int csr_priv, effective_priv = env->priv;
3247 
3248     if (riscv_has_ext(env, RVH) && env->priv == PRV_S) {
3249         /*
3250          * We are in either HS or VS mode.
3251          * Add 1 to the effective privledge level to allow us to access the
3252          * Hypervisor CSRs. The `hmode` predicate will determine if access
3253          * should be allowed(HS) or if a virtual instruction exception should be
3254          * raised(VS).
3255          */
3256         effective_priv++;
3257     }
3258 
3259     csr_priv = get_field(csrno, 0x300);
3260     if (!env->debugger && (effective_priv < csr_priv)) {
3261         if (csr_priv == (PRV_S + 1) && riscv_cpu_virt_enabled(env)) {
3262             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
3263         }
3264         return RISCV_EXCP_ILLEGAL_INST;
3265     }
3266 #endif
3267     if (write_mask && read_only) {
3268         return RISCV_EXCP_ILLEGAL_INST;
3269     }
3270 
3271     /* ensure the CSR extension is enabled. */
3272     if (!cpu->cfg.ext_icsr) {
3273         return RISCV_EXCP_ILLEGAL_INST;
3274     }
3275 
3276     /* check predicate */
3277     if (!csr_ops[csrno].predicate) {
3278         return RISCV_EXCP_ILLEGAL_INST;
3279     }
3280 
3281     if (env->priv_ver < csr_min_priv) {
3282         return RISCV_EXCP_ILLEGAL_INST;
3283     }
3284 
3285     return csr_ops[csrno].predicate(env, csrno);
3286 }
3287 
3288 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,
3289                                        target_ulong *ret_value,
3290                                        target_ulong new_value,
3291                                        target_ulong write_mask)
3292 {
3293     RISCVException ret;
3294     target_ulong old_value;
3295 
3296     /* execute combined read/write operation if it exists */
3297     if (csr_ops[csrno].op) {
3298         return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
3299     }
3300 
3301     /* if no accessor exists then return failure */
3302     if (!csr_ops[csrno].read) {
3303         return RISCV_EXCP_ILLEGAL_INST;
3304     }
3305     /* read old value */
3306     ret = csr_ops[csrno].read(env, csrno, &old_value);
3307     if (ret != RISCV_EXCP_NONE) {
3308         return ret;
3309     }
3310 
3311     /* write value if writable and write mask set, otherwise drop writes */
3312     if (write_mask) {
3313         new_value = (old_value & ~write_mask) | (new_value & write_mask);
3314         if (csr_ops[csrno].write) {
3315             ret = csr_ops[csrno].write(env, csrno, new_value);
3316             if (ret != RISCV_EXCP_NONE) {
3317                 return ret;
3318             }
3319         }
3320     }
3321 
3322     /* return old value */
3323     if (ret_value) {
3324         *ret_value = old_value;
3325     }
3326 
3327     return RISCV_EXCP_NONE;
3328 }
3329 
3330 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
3331                            target_ulong *ret_value,
3332                            target_ulong new_value, target_ulong write_mask)
3333 {
3334     RISCVCPU *cpu = env_archcpu(env);
3335 
3336     RISCVException ret = riscv_csrrw_check(env, csrno, write_mask, cpu);
3337     if (ret != RISCV_EXCP_NONE) {
3338         return ret;
3339     }
3340 
3341     return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask);
3342 }
3343 
3344 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,
3345                                         Int128 *ret_value,
3346                                         Int128 new_value,
3347                                         Int128 write_mask)
3348 {
3349     RISCVException ret;
3350     Int128 old_value;
3351 
3352     /* read old value */
3353     ret = csr_ops[csrno].read128(env, csrno, &old_value);
3354     if (ret != RISCV_EXCP_NONE) {
3355         return ret;
3356     }
3357 
3358     /* write value if writable and write mask set, otherwise drop writes */
3359     if (int128_nz(write_mask)) {
3360         new_value = int128_or(int128_and(old_value, int128_not(write_mask)),
3361                               int128_and(new_value, write_mask));
3362         if (csr_ops[csrno].write128) {
3363             ret = csr_ops[csrno].write128(env, csrno, new_value);
3364             if (ret != RISCV_EXCP_NONE) {
3365                 return ret;
3366             }
3367         } else if (csr_ops[csrno].write) {
3368             /* avoids having to write wrappers for all registers */
3369             ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value));
3370             if (ret != RISCV_EXCP_NONE) {
3371                 return ret;
3372             }
3373         }
3374     }
3375 
3376     /* return old value */
3377     if (ret_value) {
3378         *ret_value = old_value;
3379     }
3380 
3381     return RISCV_EXCP_NONE;
3382 }
3383 
3384 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
3385                                 Int128 *ret_value,
3386                                 Int128 new_value, Int128 write_mask)
3387 {
3388     RISCVException ret;
3389     RISCVCPU *cpu = env_archcpu(env);
3390 
3391     ret = riscv_csrrw_check(env, csrno, int128_nz(write_mask), cpu);
3392     if (ret != RISCV_EXCP_NONE) {
3393         return ret;
3394     }
3395 
3396     if (csr_ops[csrno].read128) {
3397         return riscv_csrrw_do128(env, csrno, ret_value, new_value, write_mask);
3398     }
3399 
3400     /*
3401      * Fall back to 64-bit version for now, if the 128-bit alternative isn't
3402      * at all defined.
3403      * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
3404      * significant), for those, this fallback is correctly handling the accesses
3405      */
3406     target_ulong old_value;
3407     ret = riscv_csrrw_do64(env, csrno, &old_value,
3408                            int128_getlo(new_value),
3409                            int128_getlo(write_mask));
3410     if (ret == RISCV_EXCP_NONE && ret_value) {
3411         *ret_value = int128_make64(old_value);
3412     }
3413     return ret;
3414 }
3415 
3416 /*
3417  * Debugger support.  If not in user mode, set env->debugger before the
3418  * riscv_csrrw call and clear it after the call.
3419  */
3420 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
3421                                  target_ulong *ret_value,
3422                                  target_ulong new_value,
3423                                  target_ulong write_mask)
3424 {
3425     RISCVException ret;
3426 #if !defined(CONFIG_USER_ONLY)
3427     env->debugger = true;
3428 #endif
3429     ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
3430 #if !defined(CONFIG_USER_ONLY)
3431     env->debugger = false;
3432 #endif
3433     return ret;
3434 }
3435 
3436 /* Control and Status Register function table */
3437 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
3438     /* User Floating-Point CSRs */
3439     [CSR_FFLAGS]   = { "fflags",   fs,     read_fflags,  write_fflags },
3440     [CSR_FRM]      = { "frm",      fs,     read_frm,     write_frm    },
3441     [CSR_FCSR]     = { "fcsr",     fs,     read_fcsr,    write_fcsr   },
3442     /* Vector CSRs */
3443     [CSR_VSTART]   = { "vstart",   vs,    read_vstart,  write_vstart,
3444                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3445     [CSR_VXSAT]    = { "vxsat",    vs,    read_vxsat,   write_vxsat,
3446                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3447     [CSR_VXRM]     = { "vxrm",     vs,    read_vxrm,    write_vxrm,
3448                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3449     [CSR_VCSR]     = { "vcsr",     vs,    read_vcsr,    write_vcsr,
3450                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3451     [CSR_VL]       = { "vl",       vs,    read_vl,
3452                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3453     [CSR_VTYPE]    = { "vtype",    vs,    read_vtype,
3454                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3455     [CSR_VLENB]    = { "vlenb",    vs,    read_vlenb,
3456                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3457     /* User Timers and Counters */
3458     [CSR_CYCLE]    = { "cycle",    ctr,    read_instret  },
3459     [CSR_INSTRET]  = { "instret",  ctr,    read_instret  },
3460     [CSR_CYCLEH]   = { "cycleh",   ctr32,  read_instreth },
3461     [CSR_INSTRETH] = { "instreth", ctr32,  read_instreth },
3462 
3463     /*
3464      * In privileged mode, the monitor will have to emulate TIME CSRs only if
3465      * rdtime callback is not provided by machine/platform emulation.
3466      */
3467     [CSR_TIME]  = { "time",  ctr,   read_time  },
3468     [CSR_TIMEH] = { "timeh", ctr32, read_timeh },
3469 
3470     /* Crypto Extension */
3471     [CSR_SEED] = { "seed", seed, NULL, NULL, rmw_seed },
3472 
3473 #if !defined(CONFIG_USER_ONLY)
3474     /* Machine Timers and Counters */
3475     [CSR_MCYCLE]    = { "mcycle",    any,   read_instret  },
3476     [CSR_MINSTRET]  = { "minstret",  any,   read_instret  },
3477     [CSR_MCYCLEH]   = { "mcycleh",   any32, read_instreth },
3478     [CSR_MINSTRETH] = { "minstreth", any32, read_instreth },
3479 
3480     /* Machine Information Registers */
3481     [CSR_MVENDORID] = { "mvendorid", any,   read_mvendorid },
3482     [CSR_MARCHID]   = { "marchid",   any,   read_marchid   },
3483     [CSR_MIMPID]    = { "mimpid",    any,   read_mimpid    },
3484     [CSR_MHARTID]   = { "mhartid",   any,   read_mhartid   },
3485 
3486     [CSR_MCONFIGPTR]  = { "mconfigptr", any,   read_zero,
3487                                         .min_priv_ver = PRIV_VERSION_1_12_0 },
3488     /* Machine Trap Setup */
3489     [CSR_MSTATUS]     = { "mstatus",    any,   read_mstatus,     write_mstatus, NULL,
3490                                                read_mstatus_i128                   },
3491     [CSR_MISA]        = { "misa",       any,   read_misa,        write_misa, NULL,
3492                                                read_misa_i128                      },
3493     [CSR_MIDELEG]     = { "mideleg",    any,   NULL,    NULL,    rmw_mideleg       },
3494     [CSR_MEDELEG]     = { "medeleg",    any,   read_medeleg,     write_medeleg     },
3495     [CSR_MIE]         = { "mie",        any,   NULL,    NULL,    rmw_mie           },
3496     [CSR_MTVEC]       = { "mtvec",      any,   read_mtvec,       write_mtvec       },
3497     [CSR_MCOUNTEREN]  = { "mcounteren", any,   read_mcounteren,  write_mcounteren  },
3498 
3499     [CSR_MSTATUSH]    = { "mstatush",   any32, read_mstatush,    write_mstatush    },
3500 
3501     /* Machine Trap Handling */
3502     [CSR_MSCRATCH] = { "mscratch", any,  read_mscratch,      write_mscratch, NULL,
3503                                          read_mscratch_i128, write_mscratch_i128   },
3504     [CSR_MEPC]     = { "mepc",     any,  read_mepc,     write_mepc     },
3505     [CSR_MCAUSE]   = { "mcause",   any,  read_mcause,   write_mcause   },
3506     [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval    },
3507     [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip        },
3508 
3509     /* Machine-Level Window to Indirectly Accessed Registers (AIA) */
3510     [CSR_MISELECT] = { "miselect", aia_any,   NULL, NULL,    rmw_xiselect },
3511     [CSR_MIREG]    = { "mireg",    aia_any,   NULL, NULL,    rmw_xireg },
3512 
3513     /* Machine-Level Interrupts (AIA) */
3514     [CSR_MTOPI]    = { "mtopi",    aia_any,   read_mtopi },
3515 
3516     /* Machine-Level IMSIC Interface (AIA) */
3517     [CSR_MSETEIPNUM] = { "mseteipnum", aia_any, NULL, NULL, rmw_xsetclreinum },
3518     [CSR_MCLREIPNUM] = { "mclreipnum", aia_any, NULL, NULL, rmw_xsetclreinum },
3519     [CSR_MSETEIENUM] = { "mseteienum", aia_any, NULL, NULL, rmw_xsetclreinum },
3520     [CSR_MCLREIENUM] = { "mclreienum", aia_any, NULL, NULL, rmw_xsetclreinum },
3521     [CSR_MTOPEI]     = { "mtopei",     aia_any, NULL, NULL, rmw_xtopei },
3522 
3523     /* Virtual Interrupts for Supervisor Level (AIA) */
3524     [CSR_MVIEN]      = { "mvien", aia_any, read_zero, write_ignore },
3525     [CSR_MVIP]       = { "mvip",  aia_any, read_zero, write_ignore },
3526 
3527     /* Machine-Level High-Half CSRs (AIA) */
3528     [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
3529     [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh     },
3530     [CSR_MVIENH]   = { "mvienh",   aia_any32, read_zero,  write_ignore },
3531     [CSR_MVIPH]    = { "mviph",    aia_any32, read_zero,  write_ignore },
3532     [CSR_MIPH]     = { "miph",     aia_any32, NULL, NULL, rmw_miph     },
3533 
3534     /* Execution environment configuration */
3535     [CSR_MENVCFG]  = { "menvcfg",  any,   read_menvcfg,  write_menvcfg,
3536                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3537     [CSR_MENVCFGH] = { "menvcfgh", any32, read_menvcfgh, write_menvcfgh,
3538                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3539     [CSR_SENVCFG]  = { "senvcfg",  smode, read_senvcfg,  write_senvcfg,
3540                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3541     [CSR_HENVCFG]  = { "henvcfg",  hmode, read_henvcfg, write_henvcfg,
3542                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3543     [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh, write_henvcfgh,
3544                                           .min_priv_ver = PRIV_VERSION_1_12_0 },
3545 
3546     /* Supervisor Trap Setup */
3547     [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,    write_sstatus, NULL,
3548                                               read_sstatus_i128                 },
3549     [CSR_SIE]        = { "sie",        smode, NULL,   NULL,    rmw_sie          },
3550     [CSR_STVEC]      = { "stvec",      smode, read_stvec,      write_stvec      },
3551     [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren, write_scounteren },
3552 
3553     /* Supervisor Trap Handling */
3554     [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch, NULL,
3555                                           read_sscratch_i128, write_sscratch_i128  },
3556     [CSR_SEPC]     = { "sepc",     smode, read_sepc,     write_sepc     },
3557     [CSR_SCAUSE]   = { "scause",   smode, read_scause,   write_scause   },
3558     [CSR_STVAL]    = { "stval",    smode, read_stval,   write_stval   },
3559     [CSR_SIP]      = { "sip",      smode, NULL,    NULL, rmw_sip        },
3560 
3561     /* Supervisor Protection and Translation */
3562     [CSR_SATP]     = { "satp",     smode, read_satp,    write_satp      },
3563 
3564     /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */
3565     [CSR_SISELECT]   = { "siselect",   aia_smode, NULL, NULL, rmw_xiselect },
3566     [CSR_SIREG]      = { "sireg",      aia_smode, NULL, NULL, rmw_xireg },
3567 
3568     /* Supervisor-Level Interrupts (AIA) */
3569     [CSR_STOPI]      = { "stopi",      aia_smode, read_stopi },
3570 
3571     /* Supervisor-Level IMSIC Interface (AIA) */
3572     [CSR_SSETEIPNUM] = { "sseteipnum", aia_smode, NULL, NULL, rmw_xsetclreinum },
3573     [CSR_SCLREIPNUM] = { "sclreipnum", aia_smode, NULL, NULL, rmw_xsetclreinum },
3574     [CSR_SSETEIENUM] = { "sseteienum", aia_smode, NULL, NULL, rmw_xsetclreinum },
3575     [CSR_SCLREIENUM] = { "sclreienum", aia_smode, NULL, NULL, rmw_xsetclreinum },
3576     [CSR_STOPEI]     = { "stopei",     aia_smode, NULL, NULL, rmw_xtopei },
3577 
3578     /* Supervisor-Level High-Half CSRs (AIA) */
3579     [CSR_SIEH]       = { "sieh",   aia_smode32, NULL, NULL, rmw_sieh },
3580     [CSR_SIPH]       = { "siph",   aia_smode32, NULL, NULL, rmw_siph },
3581 
3582     [CSR_HSTATUS]     = { "hstatus",     hmode,   read_hstatus,   write_hstatus,
3583                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3584     [CSR_HEDELEG]     = { "hedeleg",     hmode,   read_hedeleg,   write_hedeleg,
3585                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3586     [CSR_HIDELEG]     = { "hideleg",     hmode,   NULL,   NULL, rmw_hideleg,
3587                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3588     [CSR_HVIP]        = { "hvip",        hmode,   NULL,   NULL,   rmw_hvip,
3589                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3590     [CSR_HIP]         = { "hip",         hmode,   NULL,   NULL,   rmw_hip,
3591                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3592     [CSR_HIE]         = { "hie",         hmode,   NULL,   NULL,    rmw_hie,
3593                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3594     [CSR_HCOUNTEREN]  = { "hcounteren",  hmode,   read_hcounteren, write_hcounteren,
3595                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3596     [CSR_HGEIE]       = { "hgeie",       hmode,   read_hgeie,       write_hgeie,
3597                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3598     [CSR_HTVAL]       = { "htval",       hmode,   read_htval,     write_htval,
3599                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3600     [CSR_HTINST]      = { "htinst",      hmode,   read_htinst,    write_htinst,
3601                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3602     [CSR_HGEIP]       = { "hgeip",       hmode,   read_hgeip,
3603                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3604     [CSR_HGATP]       = { "hgatp",       hmode,   read_hgatp,     write_hgatp,
3605                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3606     [CSR_HTIMEDELTA]  = { "htimedelta",  hmode,   read_htimedelta, write_htimedelta,
3607                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3608     [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah, write_htimedeltah,
3609                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3610 
3611     [CSR_VSSTATUS]    = { "vsstatus",    hmode,   read_vsstatus,  write_vsstatus,
3612                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3613     [CSR_VSIP]        = { "vsip",        hmode,   NULL,    NULL,  rmw_vsip,
3614                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3615     [CSR_VSIE]        = { "vsie",        hmode,   NULL,    NULL,    rmw_vsie ,
3616                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3617     [CSR_VSTVEC]      = { "vstvec",      hmode,   read_vstvec,    write_vstvec,
3618                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3619     [CSR_VSSCRATCH]   = { "vsscratch",   hmode,   read_vsscratch, write_vsscratch,
3620                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3621     [CSR_VSEPC]       = { "vsepc",       hmode,   read_vsepc,     write_vsepc,
3622                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3623     [CSR_VSCAUSE]     = { "vscause",     hmode,   read_vscause,   write_vscause,
3624                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3625     [CSR_VSTVAL]      = { "vstval",      hmode,   read_vstval,    write_vstval,
3626                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3627     [CSR_VSATP]       = { "vsatp",       hmode,   read_vsatp,     write_vsatp,
3628                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3629 
3630     [CSR_MTVAL2]      = { "mtval2",      hmode,   read_mtval2,    write_mtval2,
3631                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3632     [CSR_MTINST]      = { "mtinst",      hmode,   read_mtinst,    write_mtinst,
3633                                          .min_priv_ver = PRIV_VERSION_1_12_0 },
3634 
3635     /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */
3636     [CSR_HVIEN]       = { "hvien",       aia_hmode, read_zero, write_ignore },
3637     [CSR_HVICTL]      = { "hvictl",      aia_hmode, read_hvictl, write_hvictl },
3638     [CSR_HVIPRIO1]    = { "hviprio1",    aia_hmode, read_hviprio1,   write_hviprio1 },
3639     [CSR_HVIPRIO2]    = { "hviprio2",    aia_hmode, read_hviprio2,   write_hviprio2 },
3640 
3641     /*
3642      * VS-Level Window to Indirectly Accessed Registers (H-extension with AIA)
3643      */
3644     [CSR_VSISELECT]   = { "vsiselect",   aia_hmode, NULL, NULL,      rmw_xiselect },
3645     [CSR_VSIREG]      = { "vsireg",      aia_hmode, NULL, NULL,      rmw_xireg },
3646 
3647     /* VS-Level Interrupts (H-extension with AIA) */
3648     [CSR_VSTOPI]      = { "vstopi",      aia_hmode, read_vstopi },
3649 
3650     /* VS-Level IMSIC Interface (H-extension with AIA) */
3651     [CSR_VSSETEIPNUM] = { "vsseteipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum },
3652     [CSR_VSCLREIPNUM] = { "vsclreipnum", aia_hmode, NULL, NULL, rmw_xsetclreinum },
3653     [CSR_VSSETEIENUM] = { "vsseteienum", aia_hmode, NULL, NULL, rmw_xsetclreinum },
3654     [CSR_VSCLREIENUM] = { "vsclreienum", aia_hmode, NULL, NULL, rmw_xsetclreinum },
3655     [CSR_VSTOPEI]     = { "vstopei",     aia_hmode, NULL, NULL, rmw_xtopei },
3656 
3657     /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
3658     [CSR_HIDELEGH]    = { "hidelegh",    aia_hmode32, NULL, NULL, rmw_hidelegh },
3659     [CSR_HVIENH]      = { "hvienh",      aia_hmode32, read_zero, write_ignore },
3660     [CSR_HVIPH]       = { "hviph",       aia_hmode32, NULL, NULL, rmw_hviph },
3661     [CSR_HVIPRIO1H]   = { "hviprio1h",   aia_hmode32, read_hviprio1h, write_hviprio1h },
3662     [CSR_HVIPRIO2H]   = { "hviprio2h",   aia_hmode32, read_hviprio2h, write_hviprio2h },
3663     [CSR_VSIEH]       = { "vsieh",       aia_hmode32, NULL, NULL, rmw_vsieh },
3664     [CSR_VSIPH]       = { "vsiph",       aia_hmode32, NULL, NULL, rmw_vsiph },
3665 
3666     /* Physical Memory Protection */
3667     [CSR_MSECCFG]    = { "mseccfg",  epmp, read_mseccfg, write_mseccfg,
3668                                      .min_priv_ver = PRIV_VERSION_1_12_0 },
3669     [CSR_PMPCFG0]    = { "pmpcfg0",   pmp, read_pmpcfg,  write_pmpcfg  },
3670     [CSR_PMPCFG1]    = { "pmpcfg1",   pmp, read_pmpcfg,  write_pmpcfg  },
3671     [CSR_PMPCFG2]    = { "pmpcfg2",   pmp, read_pmpcfg,  write_pmpcfg  },
3672     [CSR_PMPCFG3]    = { "pmpcfg3",   pmp, read_pmpcfg,  write_pmpcfg  },
3673     [CSR_PMPADDR0]   = { "pmpaddr0",  pmp, read_pmpaddr, write_pmpaddr },
3674     [CSR_PMPADDR1]   = { "pmpaddr1",  pmp, read_pmpaddr, write_pmpaddr },
3675     [CSR_PMPADDR2]   = { "pmpaddr2",  pmp, read_pmpaddr, write_pmpaddr },
3676     [CSR_PMPADDR3]   = { "pmpaddr3",  pmp, read_pmpaddr, write_pmpaddr },
3677     [CSR_PMPADDR4]   = { "pmpaddr4",  pmp, read_pmpaddr, write_pmpaddr },
3678     [CSR_PMPADDR5]   = { "pmpaddr5",  pmp, read_pmpaddr, write_pmpaddr },
3679     [CSR_PMPADDR6]   = { "pmpaddr6",  pmp, read_pmpaddr, write_pmpaddr },
3680     [CSR_PMPADDR7]   = { "pmpaddr7",  pmp, read_pmpaddr, write_pmpaddr },
3681     [CSR_PMPADDR8]   = { "pmpaddr8",  pmp, read_pmpaddr, write_pmpaddr },
3682     [CSR_PMPADDR9]   = { "pmpaddr9",  pmp, read_pmpaddr, write_pmpaddr },
3683     [CSR_PMPADDR10]  = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr },
3684     [CSR_PMPADDR11]  = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr },
3685     [CSR_PMPADDR12]  = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr },
3686     [CSR_PMPADDR13]  = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr },
3687     [CSR_PMPADDR14] =  { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr },
3688     [CSR_PMPADDR15] =  { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr },
3689 
3690     /* Debug CSRs */
3691     [CSR_TSELECT]   =  { "tselect", debug, read_tselect, write_tselect },
3692     [CSR_TDATA1]    =  { "tdata1",  debug, read_tdata,   write_tdata   },
3693     [CSR_TDATA2]    =  { "tdata2",  debug, read_tdata,   write_tdata   },
3694     [CSR_TDATA3]    =  { "tdata3",  debug, read_tdata,   write_tdata   },
3695 
3696     /* User Pointer Masking */
3697     [CSR_UMTE]    =    { "umte",    pointer_masking, read_umte,    write_umte    },
3698     [CSR_UPMMASK] =    { "upmmask", pointer_masking, read_upmmask, write_upmmask },
3699     [CSR_UPMBASE] =    { "upmbase", pointer_masking, read_upmbase, write_upmbase },
3700     /* Machine Pointer Masking */
3701     [CSR_MMTE]    =    { "mmte",    pointer_masking, read_mmte,    write_mmte    },
3702     [CSR_MPMMASK] =    { "mpmmask", pointer_masking, read_mpmmask, write_mpmmask },
3703     [CSR_MPMBASE] =    { "mpmbase", pointer_masking, read_mpmbase, write_mpmbase },
3704     /* Supervisor Pointer Masking */
3705     [CSR_SMTE]    =    { "smte",    pointer_masking, read_smte,    write_smte    },
3706     [CSR_SPMMASK] =    { "spmmask", pointer_masking, read_spmmask, write_spmmask },
3707     [CSR_SPMBASE] =    { "spmbase", pointer_masking, read_spmbase, write_spmbase },
3708 
3709     /* Performance Counters */
3710     [CSR_HPMCOUNTER3]    = { "hpmcounter3",    ctr,    read_zero },
3711     [CSR_HPMCOUNTER4]    = { "hpmcounter4",    ctr,    read_zero },
3712     [CSR_HPMCOUNTER5]    = { "hpmcounter5",    ctr,    read_zero },
3713     [CSR_HPMCOUNTER6]    = { "hpmcounter6",    ctr,    read_zero },
3714     [CSR_HPMCOUNTER7]    = { "hpmcounter7",    ctr,    read_zero },
3715     [CSR_HPMCOUNTER8]    = { "hpmcounter8",    ctr,    read_zero },
3716     [CSR_HPMCOUNTER9]    = { "hpmcounter9",    ctr,    read_zero },
3717     [CSR_HPMCOUNTER10]   = { "hpmcounter10",   ctr,    read_zero },
3718     [CSR_HPMCOUNTER11]   = { "hpmcounter11",   ctr,    read_zero },
3719     [CSR_HPMCOUNTER12]   = { "hpmcounter12",   ctr,    read_zero },
3720     [CSR_HPMCOUNTER13]   = { "hpmcounter13",   ctr,    read_zero },
3721     [CSR_HPMCOUNTER14]   = { "hpmcounter14",   ctr,    read_zero },
3722     [CSR_HPMCOUNTER15]   = { "hpmcounter15",   ctr,    read_zero },
3723     [CSR_HPMCOUNTER16]   = { "hpmcounter16",   ctr,    read_zero },
3724     [CSR_HPMCOUNTER17]   = { "hpmcounter17",   ctr,    read_zero },
3725     [CSR_HPMCOUNTER18]   = { "hpmcounter18",   ctr,    read_zero },
3726     [CSR_HPMCOUNTER19]   = { "hpmcounter19",   ctr,    read_zero },
3727     [CSR_HPMCOUNTER20]   = { "hpmcounter20",   ctr,    read_zero },
3728     [CSR_HPMCOUNTER21]   = { "hpmcounter21",   ctr,    read_zero },
3729     [CSR_HPMCOUNTER22]   = { "hpmcounter22",   ctr,    read_zero },
3730     [CSR_HPMCOUNTER23]   = { "hpmcounter23",   ctr,    read_zero },
3731     [CSR_HPMCOUNTER24]   = { "hpmcounter24",   ctr,    read_zero },
3732     [CSR_HPMCOUNTER25]   = { "hpmcounter25",   ctr,    read_zero },
3733     [CSR_HPMCOUNTER26]   = { "hpmcounter26",   ctr,    read_zero },
3734     [CSR_HPMCOUNTER27]   = { "hpmcounter27",   ctr,    read_zero },
3735     [CSR_HPMCOUNTER28]   = { "hpmcounter28",   ctr,    read_zero },
3736     [CSR_HPMCOUNTER29]   = { "hpmcounter29",   ctr,    read_zero },
3737     [CSR_HPMCOUNTER30]   = { "hpmcounter30",   ctr,    read_zero },
3738     [CSR_HPMCOUNTER31]   = { "hpmcounter31",   ctr,    read_zero },
3739 
3740     [CSR_MHPMCOUNTER3]   = { "mhpmcounter3",   mctr,   read_zero },
3741     [CSR_MHPMCOUNTER4]   = { "mhpmcounter4",   mctr,   read_zero },
3742     [CSR_MHPMCOUNTER5]   = { "mhpmcounter5",   mctr,   read_zero },
3743     [CSR_MHPMCOUNTER6]   = { "mhpmcounter6",   mctr,   read_zero },
3744     [CSR_MHPMCOUNTER7]   = { "mhpmcounter7",   mctr,   read_zero },
3745     [CSR_MHPMCOUNTER8]   = { "mhpmcounter8",   mctr,   read_zero },
3746     [CSR_MHPMCOUNTER9]   = { "mhpmcounter9",   mctr,   read_zero },
3747     [CSR_MHPMCOUNTER10]  = { "mhpmcounter10",  mctr,   read_zero },
3748     [CSR_MHPMCOUNTER11]  = { "mhpmcounter11",  mctr,   read_zero },
3749     [CSR_MHPMCOUNTER12]  = { "mhpmcounter12",  mctr,   read_zero },
3750     [CSR_MHPMCOUNTER13]  = { "mhpmcounter13",  mctr,   read_zero },
3751     [CSR_MHPMCOUNTER14]  = { "mhpmcounter14",  mctr,   read_zero },
3752     [CSR_MHPMCOUNTER15]  = { "mhpmcounter15",  mctr,   read_zero },
3753     [CSR_MHPMCOUNTER16]  = { "mhpmcounter16",  mctr,   read_zero },
3754     [CSR_MHPMCOUNTER17]  = { "mhpmcounter17",  mctr,   read_zero },
3755     [CSR_MHPMCOUNTER18]  = { "mhpmcounter18",  mctr,   read_zero },
3756     [CSR_MHPMCOUNTER19]  = { "mhpmcounter19",  mctr,   read_zero },
3757     [CSR_MHPMCOUNTER20]  = { "mhpmcounter20",  mctr,   read_zero },
3758     [CSR_MHPMCOUNTER21]  = { "mhpmcounter21",  mctr,   read_zero },
3759     [CSR_MHPMCOUNTER22]  = { "mhpmcounter22",  mctr,   read_zero },
3760     [CSR_MHPMCOUNTER23]  = { "mhpmcounter23",  mctr,   read_zero },
3761     [CSR_MHPMCOUNTER24]  = { "mhpmcounter24",  mctr,   read_zero },
3762     [CSR_MHPMCOUNTER25]  = { "mhpmcounter25",  mctr,   read_zero },
3763     [CSR_MHPMCOUNTER26]  = { "mhpmcounter26",  mctr,   read_zero },
3764     [CSR_MHPMCOUNTER27]  = { "mhpmcounter27",  mctr,   read_zero },
3765     [CSR_MHPMCOUNTER28]  = { "mhpmcounter28",  mctr,   read_zero },
3766     [CSR_MHPMCOUNTER29]  = { "mhpmcounter29",  mctr,   read_zero },
3767     [CSR_MHPMCOUNTER30]  = { "mhpmcounter30",  mctr,   read_zero },
3768     [CSR_MHPMCOUNTER31]  = { "mhpmcounter31",  mctr,   read_zero },
3769 
3770     [CSR_MCOUNTINHIBIT]  = { "mcountinhibit",   any,    read_mcountinhibit,
3771                                                        write_mcountinhibit },
3772 
3773     [CSR_MHPMEVENT3]     = { "mhpmevent3",     any,    read_zero },
3774     [CSR_MHPMEVENT4]     = { "mhpmevent4",     any,    read_zero },
3775     [CSR_MHPMEVENT5]     = { "mhpmevent5",     any,    read_zero },
3776     [CSR_MHPMEVENT6]     = { "mhpmevent6",     any,    read_zero },
3777     [CSR_MHPMEVENT7]     = { "mhpmevent7",     any,    read_zero },
3778     [CSR_MHPMEVENT8]     = { "mhpmevent8",     any,    read_zero },
3779     [CSR_MHPMEVENT9]     = { "mhpmevent9",     any,    read_zero },
3780     [CSR_MHPMEVENT10]    = { "mhpmevent10",    any,    read_zero },
3781     [CSR_MHPMEVENT11]    = { "mhpmevent11",    any,    read_zero },
3782     [CSR_MHPMEVENT12]    = { "mhpmevent12",    any,    read_zero },
3783     [CSR_MHPMEVENT13]    = { "mhpmevent13",    any,    read_zero },
3784     [CSR_MHPMEVENT14]    = { "mhpmevent14",    any,    read_zero },
3785     [CSR_MHPMEVENT15]    = { "mhpmevent15",    any,    read_zero },
3786     [CSR_MHPMEVENT16]    = { "mhpmevent16",    any,    read_zero },
3787     [CSR_MHPMEVENT17]    = { "mhpmevent17",    any,    read_zero },
3788     [CSR_MHPMEVENT18]    = { "mhpmevent18",    any,    read_zero },
3789     [CSR_MHPMEVENT19]    = { "mhpmevent19",    any,    read_zero },
3790     [CSR_MHPMEVENT20]    = { "mhpmevent20",    any,    read_zero },
3791     [CSR_MHPMEVENT21]    = { "mhpmevent21",    any,    read_zero },
3792     [CSR_MHPMEVENT22]    = { "mhpmevent22",    any,    read_zero },
3793     [CSR_MHPMEVENT23]    = { "mhpmevent23",    any,    read_zero },
3794     [CSR_MHPMEVENT24]    = { "mhpmevent24",    any,    read_zero },
3795     [CSR_MHPMEVENT25]    = { "mhpmevent25",    any,    read_zero },
3796     [CSR_MHPMEVENT26]    = { "mhpmevent26",    any,    read_zero },
3797     [CSR_MHPMEVENT27]    = { "mhpmevent27",    any,    read_zero },
3798     [CSR_MHPMEVENT28]    = { "mhpmevent28",    any,    read_zero },
3799     [CSR_MHPMEVENT29]    = { "mhpmevent29",    any,    read_zero },
3800     [CSR_MHPMEVENT30]    = { "mhpmevent30",    any,    read_zero },
3801     [CSR_MHPMEVENT31]    = { "mhpmevent31",    any,    read_zero },
3802 
3803     [CSR_HPMCOUNTER3H]   = { "hpmcounter3h",   ctr32,  read_zero },
3804     [CSR_HPMCOUNTER4H]   = { "hpmcounter4h",   ctr32,  read_zero },
3805     [CSR_HPMCOUNTER5H]   = { "hpmcounter5h",   ctr32,  read_zero },
3806     [CSR_HPMCOUNTER6H]   = { "hpmcounter6h",   ctr32,  read_zero },
3807     [CSR_HPMCOUNTER7H]   = { "hpmcounter7h",   ctr32,  read_zero },
3808     [CSR_HPMCOUNTER8H]   = { "hpmcounter8h",   ctr32,  read_zero },
3809     [CSR_HPMCOUNTER9H]   = { "hpmcounter9h",   ctr32,  read_zero },
3810     [CSR_HPMCOUNTER10H]  = { "hpmcounter10h",  ctr32,  read_zero },
3811     [CSR_HPMCOUNTER11H]  = { "hpmcounter11h",  ctr32,  read_zero },
3812     [CSR_HPMCOUNTER12H]  = { "hpmcounter12h",  ctr32,  read_zero },
3813     [CSR_HPMCOUNTER13H]  = { "hpmcounter13h",  ctr32,  read_zero },
3814     [CSR_HPMCOUNTER14H]  = { "hpmcounter14h",  ctr32,  read_zero },
3815     [CSR_HPMCOUNTER15H]  = { "hpmcounter15h",  ctr32,  read_zero },
3816     [CSR_HPMCOUNTER16H]  = { "hpmcounter16h",  ctr32,  read_zero },
3817     [CSR_HPMCOUNTER17H]  = { "hpmcounter17h",  ctr32,  read_zero },
3818     [CSR_HPMCOUNTER18H]  = { "hpmcounter18h",  ctr32,  read_zero },
3819     [CSR_HPMCOUNTER19H]  = { "hpmcounter19h",  ctr32,  read_zero },
3820     [CSR_HPMCOUNTER20H]  = { "hpmcounter20h",  ctr32,  read_zero },
3821     [CSR_HPMCOUNTER21H]  = { "hpmcounter21h",  ctr32,  read_zero },
3822     [CSR_HPMCOUNTER22H]  = { "hpmcounter22h",  ctr32,  read_zero },
3823     [CSR_HPMCOUNTER23H]  = { "hpmcounter23h",  ctr32,  read_zero },
3824     [CSR_HPMCOUNTER24H]  = { "hpmcounter24h",  ctr32,  read_zero },
3825     [CSR_HPMCOUNTER25H]  = { "hpmcounter25h",  ctr32,  read_zero },
3826     [CSR_HPMCOUNTER26H]  = { "hpmcounter26h",  ctr32,  read_zero },
3827     [CSR_HPMCOUNTER27H]  = { "hpmcounter27h",  ctr32,  read_zero },
3828     [CSR_HPMCOUNTER28H]  = { "hpmcounter28h",  ctr32,  read_zero },
3829     [CSR_HPMCOUNTER29H]  = { "hpmcounter29h",  ctr32,  read_zero },
3830     [CSR_HPMCOUNTER30H]  = { "hpmcounter30h",  ctr32,  read_zero },
3831     [CSR_HPMCOUNTER31H]  = { "hpmcounter31h",  ctr32,  read_zero },
3832 
3833     [CSR_MHPMCOUNTER3H]  = { "mhpmcounter3h",  any32,  read_zero },
3834     [CSR_MHPMCOUNTER4H]  = { "mhpmcounter4h",  any32,  read_zero },
3835     [CSR_MHPMCOUNTER5H]  = { "mhpmcounter5h",  any32,  read_zero },
3836     [CSR_MHPMCOUNTER6H]  = { "mhpmcounter6h",  any32,  read_zero },
3837     [CSR_MHPMCOUNTER7H]  = { "mhpmcounter7h",  any32,  read_zero },
3838     [CSR_MHPMCOUNTER8H]  = { "mhpmcounter8h",  any32,  read_zero },
3839     [CSR_MHPMCOUNTER9H]  = { "mhpmcounter9h",  any32,  read_zero },
3840     [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", any32,  read_zero },
3841     [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", any32,  read_zero },
3842     [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", any32,  read_zero },
3843     [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", any32,  read_zero },
3844     [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", any32,  read_zero },
3845     [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", any32,  read_zero },
3846     [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", any32,  read_zero },
3847     [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", any32,  read_zero },
3848     [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", any32,  read_zero },
3849     [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", any32,  read_zero },
3850     [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", any32,  read_zero },
3851     [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", any32,  read_zero },
3852     [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", any32,  read_zero },
3853     [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", any32,  read_zero },
3854     [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", any32,  read_zero },
3855     [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", any32,  read_zero },
3856     [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", any32,  read_zero },
3857     [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", any32,  read_zero },
3858     [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", any32,  read_zero },
3859     [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", any32,  read_zero },
3860     [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", any32,  read_zero },
3861     [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", any32,  read_zero },
3862 #endif /* !CONFIG_USER_ONLY */
3863 };
3864