xref: /openbmc/qemu/target/riscv/csr.c (revision c7de92b4)
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 "cpu.h"
23 #include "qemu/main-loop.h"
24 #include "exec/exec-all.h"
25 
26 /* CSR function table public API */
27 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops)
28 {
29     *ops = csr_ops[csrno & (CSR_TABLE_SIZE - 1)];
30 }
31 
32 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops)
33 {
34     csr_ops[csrno & (CSR_TABLE_SIZE - 1)] = *ops;
35 }
36 
37 /* Predicates */
38 static RISCVException fs(CPURISCVState *env, int csrno)
39 {
40 #if !defined(CONFIG_USER_ONLY)
41     if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
42         return RISCV_EXCP_ILLEGAL_INST;
43     }
44 #endif
45     return RISCV_EXCP_NONE;
46 }
47 
48 static RISCVException vs(CPURISCVState *env, int csrno)
49 {
50     CPUState *cs = env_cpu(env);
51     RISCVCPU *cpu = RISCV_CPU(cs);
52 
53     if (env->misa_ext & RVV ||
54         cpu->cfg.ext_zve32f || cpu->cfg.ext_zve64f) {
55 #if !defined(CONFIG_USER_ONLY)
56         if (!env->debugger && !riscv_cpu_vector_enabled(env)) {
57             return RISCV_EXCP_ILLEGAL_INST;
58         }
59 #endif
60         return RISCV_EXCP_NONE;
61     }
62     return RISCV_EXCP_ILLEGAL_INST;
63 }
64 
65 static RISCVException ctr(CPURISCVState *env, int csrno)
66 {
67 #if !defined(CONFIG_USER_ONLY)
68     CPUState *cs = env_cpu(env);
69     RISCVCPU *cpu = RISCV_CPU(cs);
70 
71     if (!cpu->cfg.ext_counters) {
72         /* The Counters extensions is not enabled */
73         return RISCV_EXCP_ILLEGAL_INST;
74     }
75 
76     if (riscv_cpu_virt_enabled(env)) {
77         switch (csrno) {
78         case CSR_CYCLE:
79             if (!get_field(env->hcounteren, COUNTEREN_CY) &&
80                 get_field(env->mcounteren, COUNTEREN_CY)) {
81                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
82             }
83             break;
84         case CSR_TIME:
85             if (!get_field(env->hcounteren, COUNTEREN_TM) &&
86                 get_field(env->mcounteren, COUNTEREN_TM)) {
87                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
88             }
89             break;
90         case CSR_INSTRET:
91             if (!get_field(env->hcounteren, COUNTEREN_IR) &&
92                 get_field(env->mcounteren, COUNTEREN_IR)) {
93                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
94             }
95             break;
96         case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31:
97             if (!get_field(env->hcounteren, 1 << (csrno - CSR_HPMCOUNTER3)) &&
98                 get_field(env->mcounteren, 1 << (csrno - CSR_HPMCOUNTER3))) {
99                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
100             }
101             break;
102         }
103         if (riscv_cpu_mxl(env) == MXL_RV32) {
104             switch (csrno) {
105             case CSR_CYCLEH:
106                 if (!get_field(env->hcounteren, COUNTEREN_CY) &&
107                     get_field(env->mcounteren, COUNTEREN_CY)) {
108                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
109                 }
110                 break;
111             case CSR_TIMEH:
112                 if (!get_field(env->hcounteren, COUNTEREN_TM) &&
113                     get_field(env->mcounteren, COUNTEREN_TM)) {
114                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
115                 }
116                 break;
117             case CSR_INSTRETH:
118                 if (!get_field(env->hcounteren, COUNTEREN_IR) &&
119                     get_field(env->mcounteren, COUNTEREN_IR)) {
120                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
121                 }
122                 break;
123             case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
124                 if (!get_field(env->hcounteren, 1 << (csrno - CSR_HPMCOUNTER3H)) &&
125                     get_field(env->mcounteren, 1 << (csrno - CSR_HPMCOUNTER3H))) {
126                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
127                 }
128                 break;
129             }
130         }
131     }
132 #endif
133     return RISCV_EXCP_NONE;
134 }
135 
136 static RISCVException ctr32(CPURISCVState *env, int csrno)
137 {
138     if (riscv_cpu_mxl(env) != MXL_RV32) {
139         return RISCV_EXCP_ILLEGAL_INST;
140     }
141 
142     return ctr(env, csrno);
143 }
144 
145 #if !defined(CONFIG_USER_ONLY)
146 static RISCVException any(CPURISCVState *env, int csrno)
147 {
148     return RISCV_EXCP_NONE;
149 }
150 
151 static RISCVException any32(CPURISCVState *env, int csrno)
152 {
153     if (riscv_cpu_mxl(env) != MXL_RV32) {
154         return RISCV_EXCP_ILLEGAL_INST;
155     }
156 
157     return any(env, csrno);
158 
159 }
160 
161 static int aia_any(CPURISCVState *env, int csrno)
162 {
163     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
164         return RISCV_EXCP_ILLEGAL_INST;
165     }
166 
167     return any(env, csrno);
168 }
169 
170 static int aia_any32(CPURISCVState *env, int csrno)
171 {
172     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
173         return RISCV_EXCP_ILLEGAL_INST;
174     }
175 
176     return any32(env, csrno);
177 }
178 
179 static RISCVException smode(CPURISCVState *env, int csrno)
180 {
181     if (riscv_has_ext(env, RVS)) {
182         return RISCV_EXCP_NONE;
183     }
184 
185     return RISCV_EXCP_ILLEGAL_INST;
186 }
187 
188 static int smode32(CPURISCVState *env, int csrno)
189 {
190     if (riscv_cpu_mxl(env) != MXL_RV32) {
191         return RISCV_EXCP_ILLEGAL_INST;
192     }
193 
194     return smode(env, csrno);
195 }
196 
197 static int aia_smode(CPURISCVState *env, int csrno)
198 {
199     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
200         return RISCV_EXCP_ILLEGAL_INST;
201     }
202 
203     return smode(env, csrno);
204 }
205 
206 static int aia_smode32(CPURISCVState *env, int csrno)
207 {
208     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
209         return RISCV_EXCP_ILLEGAL_INST;
210     }
211 
212     return smode32(env, csrno);
213 }
214 
215 static RISCVException hmode(CPURISCVState *env, int csrno)
216 {
217     if (riscv_has_ext(env, RVS) &&
218         riscv_has_ext(env, RVH)) {
219         /* Hypervisor extension is supported */
220         if ((env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) ||
221             env->priv == PRV_M) {
222             return RISCV_EXCP_NONE;
223         } else {
224             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
225         }
226     }
227 
228     return RISCV_EXCP_ILLEGAL_INST;
229 }
230 
231 static RISCVException hmode32(CPURISCVState *env, int csrno)
232 {
233     if (riscv_cpu_mxl(env) != MXL_RV32) {
234         if (!riscv_cpu_virt_enabled(env)) {
235             return RISCV_EXCP_ILLEGAL_INST;
236         } else {
237             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
238         }
239     }
240 
241     return hmode(env, csrno);
242 
243 }
244 
245 /* Checks if PointerMasking registers could be accessed */
246 static RISCVException pointer_masking(CPURISCVState *env, int csrno)
247 {
248     /* Check if j-ext is present */
249     if (riscv_has_ext(env, RVJ)) {
250         return RISCV_EXCP_NONE;
251     }
252     return RISCV_EXCP_ILLEGAL_INST;
253 }
254 
255 static int aia_hmode(CPURISCVState *env, int csrno)
256 {
257     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
258         return RISCV_EXCP_ILLEGAL_INST;
259      }
260 
261      return hmode(env, csrno);
262 }
263 
264 static int aia_hmode32(CPURISCVState *env, int csrno)
265 {
266     if (!riscv_feature(env, RISCV_FEATURE_AIA)) {
267         return RISCV_EXCP_ILLEGAL_INST;
268     }
269 
270     return hmode32(env, csrno);
271 }
272 
273 static RISCVException pmp(CPURISCVState *env, int csrno)
274 {
275     if (riscv_feature(env, RISCV_FEATURE_PMP)) {
276         return RISCV_EXCP_NONE;
277     }
278 
279     return RISCV_EXCP_ILLEGAL_INST;
280 }
281 
282 static RISCVException epmp(CPURISCVState *env, int csrno)
283 {
284     if (env->priv == PRV_M && riscv_feature(env, RISCV_FEATURE_EPMP)) {
285         return RISCV_EXCP_NONE;
286     }
287 
288     return RISCV_EXCP_ILLEGAL_INST;
289 }
290 #endif
291 
292 /* User Floating-Point CSRs */
293 static RISCVException read_fflags(CPURISCVState *env, int csrno,
294                                   target_ulong *val)
295 {
296     *val = riscv_cpu_get_fflags(env);
297     return RISCV_EXCP_NONE;
298 }
299 
300 static RISCVException write_fflags(CPURISCVState *env, int csrno,
301                                    target_ulong val)
302 {
303 #if !defined(CONFIG_USER_ONLY)
304     env->mstatus |= MSTATUS_FS;
305 #endif
306     riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
307     return RISCV_EXCP_NONE;
308 }
309 
310 static RISCVException read_frm(CPURISCVState *env, int csrno,
311                                target_ulong *val)
312 {
313     *val = env->frm;
314     return RISCV_EXCP_NONE;
315 }
316 
317 static RISCVException write_frm(CPURISCVState *env, int csrno,
318                                 target_ulong val)
319 {
320 #if !defined(CONFIG_USER_ONLY)
321     env->mstatus |= MSTATUS_FS;
322 #endif
323     env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
324     return RISCV_EXCP_NONE;
325 }
326 
327 static RISCVException read_fcsr(CPURISCVState *env, int csrno,
328                                 target_ulong *val)
329 {
330     *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
331         | (env->frm << FSR_RD_SHIFT);
332     return RISCV_EXCP_NONE;
333 }
334 
335 static RISCVException write_fcsr(CPURISCVState *env, int csrno,
336                                  target_ulong val)
337 {
338 #if !defined(CONFIG_USER_ONLY)
339     env->mstatus |= MSTATUS_FS;
340 #endif
341     env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
342     riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
343     return RISCV_EXCP_NONE;
344 }
345 
346 static RISCVException read_vtype(CPURISCVState *env, int csrno,
347                                  target_ulong *val)
348 {
349     uint64_t vill;
350     switch (env->xl) {
351     case MXL_RV32:
352         vill = (uint32_t)env->vill << 31;
353         break;
354     case MXL_RV64:
355         vill = (uint64_t)env->vill << 63;
356         break;
357     default:
358         g_assert_not_reached();
359     }
360     *val = (target_ulong)vill | env->vtype;
361     return RISCV_EXCP_NONE;
362 }
363 
364 static RISCVException read_vl(CPURISCVState *env, int csrno,
365                               target_ulong *val)
366 {
367     *val = env->vl;
368     return RISCV_EXCP_NONE;
369 }
370 
371 static int read_vlenb(CPURISCVState *env, int csrno, target_ulong *val)
372 {
373     *val = env_archcpu(env)->cfg.vlen >> 3;
374     return RISCV_EXCP_NONE;
375 }
376 
377 static RISCVException read_vxrm(CPURISCVState *env, int csrno,
378                                 target_ulong *val)
379 {
380     *val = env->vxrm;
381     return RISCV_EXCP_NONE;
382 }
383 
384 static RISCVException write_vxrm(CPURISCVState *env, int csrno,
385                                  target_ulong val)
386 {
387 #if !defined(CONFIG_USER_ONLY)
388     env->mstatus |= MSTATUS_VS;
389 #endif
390     env->vxrm = val;
391     return RISCV_EXCP_NONE;
392 }
393 
394 static RISCVException read_vxsat(CPURISCVState *env, int csrno,
395                                  target_ulong *val)
396 {
397     *val = env->vxsat;
398     return RISCV_EXCP_NONE;
399 }
400 
401 static RISCVException write_vxsat(CPURISCVState *env, int csrno,
402                                   target_ulong val)
403 {
404 #if !defined(CONFIG_USER_ONLY)
405     env->mstatus |= MSTATUS_VS;
406 #endif
407     env->vxsat = val;
408     return RISCV_EXCP_NONE;
409 }
410 
411 static RISCVException read_vstart(CPURISCVState *env, int csrno,
412                                   target_ulong *val)
413 {
414     *val = env->vstart;
415     return RISCV_EXCP_NONE;
416 }
417 
418 static RISCVException write_vstart(CPURISCVState *env, int csrno,
419                                    target_ulong val)
420 {
421 #if !defined(CONFIG_USER_ONLY)
422     env->mstatus |= MSTATUS_VS;
423 #endif
424     /*
425      * The vstart CSR is defined to have only enough writable bits
426      * to hold the largest element index, i.e. lg2(VLEN) bits.
427      */
428     env->vstart = val & ~(~0ULL << ctzl(env_archcpu(env)->cfg.vlen));
429     return RISCV_EXCP_NONE;
430 }
431 
432 static int read_vcsr(CPURISCVState *env, int csrno, target_ulong *val)
433 {
434     *val = (env->vxrm << VCSR_VXRM_SHIFT) | (env->vxsat << VCSR_VXSAT_SHIFT);
435     return RISCV_EXCP_NONE;
436 }
437 
438 static int write_vcsr(CPURISCVState *env, int csrno, target_ulong val)
439 {
440 #if !defined(CONFIG_USER_ONLY)
441     env->mstatus |= MSTATUS_VS;
442 #endif
443     env->vxrm = (val & VCSR_VXRM) >> VCSR_VXRM_SHIFT;
444     env->vxsat = (val & VCSR_VXSAT) >> VCSR_VXSAT_SHIFT;
445     return RISCV_EXCP_NONE;
446 }
447 
448 /* User Timers and Counters */
449 static RISCVException read_instret(CPURISCVState *env, int csrno,
450                                    target_ulong *val)
451 {
452 #if !defined(CONFIG_USER_ONLY)
453     if (icount_enabled()) {
454         *val = icount_get();
455     } else {
456         *val = cpu_get_host_ticks();
457     }
458 #else
459     *val = cpu_get_host_ticks();
460 #endif
461     return RISCV_EXCP_NONE;
462 }
463 
464 static RISCVException read_instreth(CPURISCVState *env, int csrno,
465                                     target_ulong *val)
466 {
467 #if !defined(CONFIG_USER_ONLY)
468     if (icount_enabled()) {
469         *val = icount_get() >> 32;
470     } else {
471         *val = cpu_get_host_ticks() >> 32;
472     }
473 #else
474     *val = cpu_get_host_ticks() >> 32;
475 #endif
476     return RISCV_EXCP_NONE;
477 }
478 
479 #if defined(CONFIG_USER_ONLY)
480 static RISCVException read_time(CPURISCVState *env, int csrno,
481                                 target_ulong *val)
482 {
483     *val = cpu_get_host_ticks();
484     return RISCV_EXCP_NONE;
485 }
486 
487 static RISCVException read_timeh(CPURISCVState *env, int csrno,
488                                  target_ulong *val)
489 {
490     *val = cpu_get_host_ticks() >> 32;
491     return RISCV_EXCP_NONE;
492 }
493 
494 #else /* CONFIG_USER_ONLY */
495 
496 static RISCVException read_time(CPURISCVState *env, int csrno,
497                                 target_ulong *val)
498 {
499     uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
500 
501     if (!env->rdtime_fn) {
502         return RISCV_EXCP_ILLEGAL_INST;
503     }
504 
505     *val = env->rdtime_fn(env->rdtime_fn_arg) + delta;
506     return RISCV_EXCP_NONE;
507 }
508 
509 static RISCVException read_timeh(CPURISCVState *env, int csrno,
510                                  target_ulong *val)
511 {
512     uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
513 
514     if (!env->rdtime_fn) {
515         return RISCV_EXCP_ILLEGAL_INST;
516     }
517 
518     *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32;
519     return RISCV_EXCP_NONE;
520 }
521 
522 /* Machine constants */
523 
524 #define M_MODE_INTERRUPTS  ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP))
525 #define S_MODE_INTERRUPTS  ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP))
526 #define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))
527 #define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS))
528 
529 #define VSTOPI_NUM_SRCS 5
530 
531 static const uint64_t delegable_ints = S_MODE_INTERRUPTS |
532                                            VS_MODE_INTERRUPTS;
533 static const uint64_t vs_delegable_ints = VS_MODE_INTERRUPTS;
534 static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
535                                      HS_MODE_INTERRUPTS;
536 #define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \
537                          (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \
538                          (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \
539                          (1ULL << (RISCV_EXCP_BREAKPOINT)) | \
540                          (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | \
541                          (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | \
542                          (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | \
543                          (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | \
544                          (1ULL << (RISCV_EXCP_U_ECALL)) | \
545                          (1ULL << (RISCV_EXCP_S_ECALL)) | \
546                          (1ULL << (RISCV_EXCP_VS_ECALL)) | \
547                          (1ULL << (RISCV_EXCP_M_ECALL)) | \
548                          (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | \
549                          (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \
550                          (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \
551                          (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \
552                          (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \
553                          (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
554                          (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)))
555 static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &
556     ~((1ULL << (RISCV_EXCP_S_ECALL)) |
557       (1ULL << (RISCV_EXCP_VS_ECALL)) |
558       (1ULL << (RISCV_EXCP_M_ECALL)) |
559       (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) |
560       (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) |
561       (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) |
562       (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)));
563 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
564     SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
565     SSTATUS_SUM | SSTATUS_MXR | SSTATUS_VS;
566 static const target_ulong sip_writable_mask = SIP_SSIP | MIP_USIP | MIP_UEIP;
567 static const target_ulong hip_writable_mask = MIP_VSSIP;
568 static const target_ulong hvip_writable_mask = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP;
569 static const target_ulong vsip_writable_mask = MIP_VSSIP;
570 
571 static const char valid_vm_1_10_32[16] = {
572     [VM_1_10_MBARE] = 1,
573     [VM_1_10_SV32] = 1
574 };
575 
576 static const char valid_vm_1_10_64[16] = {
577     [VM_1_10_MBARE] = 1,
578     [VM_1_10_SV39] = 1,
579     [VM_1_10_SV48] = 1,
580     [VM_1_10_SV57] = 1
581 };
582 
583 /* Machine Information Registers */
584 static RISCVException read_zero(CPURISCVState *env, int csrno,
585                                 target_ulong *val)
586 {
587     *val = 0;
588     return RISCV_EXCP_NONE;
589 }
590 
591 static RISCVException write_ignore(CPURISCVState *env, int csrno,
592                                    target_ulong val)
593 {
594     return RISCV_EXCP_NONE;
595 }
596 
597 static RISCVException read_mhartid(CPURISCVState *env, int csrno,
598                                    target_ulong *val)
599 {
600     *val = env->mhartid;
601     return RISCV_EXCP_NONE;
602 }
603 
604 /* Machine Trap Setup */
605 
606 /* We do not store SD explicitly, only compute it on demand. */
607 static uint64_t add_status_sd(RISCVMXL xl, uint64_t status)
608 {
609     if ((status & MSTATUS_FS) == MSTATUS_FS ||
610         (status & MSTATUS_VS) == MSTATUS_VS ||
611         (status & MSTATUS_XS) == MSTATUS_XS) {
612         switch (xl) {
613         case MXL_RV32:
614             return status | MSTATUS32_SD;
615         case MXL_RV64:
616             return status | MSTATUS64_SD;
617         case MXL_RV128:
618             return MSTATUSH128_SD;
619         default:
620             g_assert_not_reached();
621         }
622     }
623     return status;
624 }
625 
626 static RISCVException read_mstatus(CPURISCVState *env, int csrno,
627                                    target_ulong *val)
628 {
629     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus);
630     return RISCV_EXCP_NONE;
631 }
632 
633 static int validate_vm(CPURISCVState *env, target_ulong vm)
634 {
635     if (riscv_cpu_mxl(env) == MXL_RV32) {
636         return valid_vm_1_10_32[vm & 0xf];
637     } else {
638         return valid_vm_1_10_64[vm & 0xf];
639     }
640 }
641 
642 static RISCVException write_mstatus(CPURISCVState *env, int csrno,
643                                     target_ulong val)
644 {
645     uint64_t mstatus = env->mstatus;
646     uint64_t mask = 0;
647     RISCVMXL xl = riscv_cpu_mxl(env);
648 
649     /* flush tlb on mstatus fields that affect VM */
650     if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
651             MSTATUS_MPRV | MSTATUS_SUM)) {
652         tlb_flush(env_cpu(env));
653     }
654     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
655         MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM |
656         MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
657         MSTATUS_TW | MSTATUS_VS;
658 
659     if (xl != MXL_RV32 || env->debugger) {
660         /*
661          * RV32: MPV and GVA are not in mstatus. The current plan is to
662          * add them to mstatush. For now, we just don't support it.
663          */
664         mask |= MSTATUS_MPV | MSTATUS_GVA;
665         if ((val & MSTATUS64_UXL) != 0) {
666             mask |= MSTATUS64_UXL;
667         }
668     }
669 
670     mstatus = (mstatus & ~mask) | (val & mask);
671 
672     if (xl > MXL_RV32) {
673         /* SXL field is for now read only */
674         mstatus = set_field(mstatus, MSTATUS64_SXL, xl);
675     }
676     env->mstatus = mstatus;
677     env->xl = cpu_recompute_xl(env);
678 
679     return RISCV_EXCP_NONE;
680 }
681 
682 static RISCVException read_mstatush(CPURISCVState *env, int csrno,
683                                     target_ulong *val)
684 {
685     *val = env->mstatus >> 32;
686     return RISCV_EXCP_NONE;
687 }
688 
689 static RISCVException write_mstatush(CPURISCVState *env, int csrno,
690                                      target_ulong val)
691 {
692     uint64_t valh = (uint64_t)val << 32;
693     uint64_t mask = MSTATUS_MPV | MSTATUS_GVA;
694 
695     if ((valh ^ env->mstatus) & (MSTATUS_MPV)) {
696         tlb_flush(env_cpu(env));
697     }
698 
699     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
700 
701     return RISCV_EXCP_NONE;
702 }
703 
704 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno,
705                                         Int128 *val)
706 {
707     *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128, env->mstatus));
708     return RISCV_EXCP_NONE;
709 }
710 
711 static RISCVException read_misa_i128(CPURISCVState *env, int csrno,
712                                      Int128 *val)
713 {
714     *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62);
715     return RISCV_EXCP_NONE;
716 }
717 
718 static RISCVException read_misa(CPURISCVState *env, int csrno,
719                                 target_ulong *val)
720 {
721     target_ulong misa;
722 
723     switch (env->misa_mxl) {
724     case MXL_RV32:
725         misa = (target_ulong)MXL_RV32 << 30;
726         break;
727 #ifdef TARGET_RISCV64
728     case MXL_RV64:
729         misa = (target_ulong)MXL_RV64 << 62;
730         break;
731 #endif
732     default:
733         g_assert_not_reached();
734     }
735 
736     *val = misa | env->misa_ext;
737     return RISCV_EXCP_NONE;
738 }
739 
740 static RISCVException write_misa(CPURISCVState *env, int csrno,
741                                  target_ulong val)
742 {
743     if (!riscv_feature(env, RISCV_FEATURE_MISA)) {
744         /* drop write to misa */
745         return RISCV_EXCP_NONE;
746     }
747 
748     /* 'I' or 'E' must be present */
749     if (!(val & (RVI | RVE))) {
750         /* It is not, drop write to misa */
751         return RISCV_EXCP_NONE;
752     }
753 
754     /* 'E' excludes all other extensions */
755     if (val & RVE) {
756         /* when we support 'E' we can do "val = RVE;" however
757          * for now we just drop writes if 'E' is present.
758          */
759         return RISCV_EXCP_NONE;
760     }
761 
762     /*
763      * misa.MXL writes are not supported by QEMU.
764      * Drop writes to those bits.
765      */
766 
767     /* Mask extensions that are not supported by this hart */
768     val &= env->misa_ext_mask;
769 
770     /* Mask extensions that are not supported by QEMU */
771     val &= (RVI | RVE | RVM | RVA | RVF | RVD | RVC | RVS | RVU | RVV);
772 
773     /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
774     if ((val & RVD) && !(val & RVF)) {
775         val &= ~RVD;
776     }
777 
778     /* Suppress 'C' if next instruction is not aligned
779      * TODO: this should check next_pc
780      */
781     if ((val & RVC) && (GETPC() & ~3) != 0) {
782         val &= ~RVC;
783     }
784 
785     /* If nothing changed, do nothing. */
786     if (val == env->misa_ext) {
787         return RISCV_EXCP_NONE;
788     }
789 
790     /* flush translation cache */
791     tb_flush(env_cpu(env));
792     env->misa_ext = val;
793     env->xl = riscv_cpu_mxl(env);
794     return RISCV_EXCP_NONE;
795 }
796 
797 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
798                                    target_ulong *val)
799 {
800     *val = env->medeleg;
801     return RISCV_EXCP_NONE;
802 }
803 
804 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
805                                     target_ulong val)
806 {
807     env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
808     return RISCV_EXCP_NONE;
809 }
810 
811 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno,
812                                     uint64_t *ret_val,
813                                     uint64_t new_val, uint64_t wr_mask)
814 {
815     uint64_t mask = wr_mask & delegable_ints;
816 
817     if (ret_val) {
818         *ret_val = env->mideleg;
819     }
820 
821     env->mideleg = (env->mideleg & ~mask) | (new_val & mask);
822 
823     if (riscv_has_ext(env, RVH)) {
824         env->mideleg |= HS_MODE_INTERRUPTS;
825     }
826 
827     return RISCV_EXCP_NONE;
828 }
829 
830 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno,
831                                   target_ulong *ret_val,
832                                   target_ulong new_val, target_ulong wr_mask)
833 {
834     uint64_t rval;
835     RISCVException ret;
836 
837     ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask);
838     if (ret_val) {
839         *ret_val = rval;
840     }
841 
842     return ret;
843 }
844 
845 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno,
846                                    target_ulong *ret_val,
847                                    target_ulong new_val,
848                                    target_ulong wr_mask)
849 {
850     uint64_t rval;
851     RISCVException ret;
852 
853     ret = rmw_mideleg64(env, csrno, &rval,
854         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
855     if (ret_val) {
856         *ret_val = rval >> 32;
857     }
858 
859     return ret;
860 }
861 
862 static RISCVException rmw_mie64(CPURISCVState *env, int csrno,
863                                 uint64_t *ret_val,
864                                 uint64_t new_val, uint64_t wr_mask)
865 {
866     uint64_t mask = wr_mask & all_ints;
867 
868     if (ret_val) {
869         *ret_val = env->mie;
870     }
871 
872     env->mie = (env->mie & ~mask) | (new_val & mask);
873 
874     if (!riscv_has_ext(env, RVH)) {
875         env->mie &= ~((uint64_t)MIP_SGEIP);
876     }
877 
878     return RISCV_EXCP_NONE;
879 }
880 
881 static RISCVException rmw_mie(CPURISCVState *env, int csrno,
882                               target_ulong *ret_val,
883                               target_ulong new_val, target_ulong wr_mask)
884 {
885     uint64_t rval;
886     RISCVException ret;
887 
888     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask);
889     if (ret_val) {
890         *ret_val = rval;
891     }
892 
893     return ret;
894 }
895 
896 static RISCVException rmw_mieh(CPURISCVState *env, int csrno,
897                                target_ulong *ret_val,
898                                target_ulong new_val, target_ulong wr_mask)
899 {
900     uint64_t rval;
901     RISCVException ret;
902 
903     ret = rmw_mie64(env, csrno, &rval,
904         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
905     if (ret_val) {
906         *ret_val = rval >> 32;
907     }
908 
909     return ret;
910 }
911 
912 static int read_mtopi(CPURISCVState *env, int csrno, target_ulong *val)
913 {
914     int irq;
915     uint8_t iprio;
916 
917     irq = riscv_cpu_mirq_pending(env);
918     if (irq <= 0 || irq > 63) {
919         *val = 0;
920     } else {
921         iprio = env->miprio[irq];
922         if (!iprio) {
923             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) {
924                 iprio = IPRIO_MMAXIPRIO;
925             }
926         }
927         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
928         *val |= iprio;
929     }
930 
931     return RISCV_EXCP_NONE;
932 }
933 
934 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
935                                  target_ulong *val)
936 {
937     *val = env->mtvec;
938     return RISCV_EXCP_NONE;
939 }
940 
941 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
942                                   target_ulong val)
943 {
944     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
945     if ((val & 3) < 2) {
946         env->mtvec = val;
947     } else {
948         qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
949     }
950     return RISCV_EXCP_NONE;
951 }
952 
953 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
954                                       target_ulong *val)
955 {
956     *val = env->mcounteren;
957     return RISCV_EXCP_NONE;
958 }
959 
960 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
961                                        target_ulong val)
962 {
963     env->mcounteren = val;
964     return RISCV_EXCP_NONE;
965 }
966 
967 /* Machine Trap Handling */
968 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno,
969                                          Int128 *val)
970 {
971     *val = int128_make128(env->mscratch, env->mscratchh);
972     return RISCV_EXCP_NONE;
973 }
974 
975 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno,
976                                           Int128 val)
977 {
978     env->mscratch = int128_getlo(val);
979     env->mscratchh = int128_gethi(val);
980     return RISCV_EXCP_NONE;
981 }
982 
983 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
984                                     target_ulong *val)
985 {
986     *val = env->mscratch;
987     return RISCV_EXCP_NONE;
988 }
989 
990 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
991                                      target_ulong val)
992 {
993     env->mscratch = val;
994     return RISCV_EXCP_NONE;
995 }
996 
997 static RISCVException read_mepc(CPURISCVState *env, int csrno,
998                                      target_ulong *val)
999 {
1000     *val = env->mepc;
1001     return RISCV_EXCP_NONE;
1002 }
1003 
1004 static RISCVException write_mepc(CPURISCVState *env, int csrno,
1005                                      target_ulong val)
1006 {
1007     env->mepc = val;
1008     return RISCV_EXCP_NONE;
1009 }
1010 
1011 static RISCVException read_mcause(CPURISCVState *env, int csrno,
1012                                      target_ulong *val)
1013 {
1014     *val = env->mcause;
1015     return RISCV_EXCP_NONE;
1016 }
1017 
1018 static RISCVException write_mcause(CPURISCVState *env, int csrno,
1019                                      target_ulong val)
1020 {
1021     env->mcause = val;
1022     return RISCV_EXCP_NONE;
1023 }
1024 
1025 static RISCVException read_mtval(CPURISCVState *env, int csrno,
1026                                  target_ulong *val)
1027 {
1028     *val = env->mtval;
1029     return RISCV_EXCP_NONE;
1030 }
1031 
1032 static RISCVException write_mtval(CPURISCVState *env, int csrno,
1033                                   target_ulong val)
1034 {
1035     env->mtval = val;
1036     return RISCV_EXCP_NONE;
1037 }
1038 
1039 static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
1040                                 uint64_t *ret_val,
1041                                 uint64_t new_val, uint64_t wr_mask)
1042 {
1043     RISCVCPU *cpu = env_archcpu(env);
1044     /* Allow software control of delegable interrupts not claimed by hardware */
1045     uint64_t old_mip, mask = wr_mask & delegable_ints & ~env->miclaim;
1046     uint32_t gin;
1047 
1048     if (mask) {
1049         old_mip = riscv_cpu_update_mip(cpu, mask, (new_val & mask));
1050     } else {
1051         old_mip = env->mip;
1052     }
1053 
1054     if (csrno != CSR_HVIP) {
1055         gin = get_field(env->hstatus, HSTATUS_VGEIN);
1056         old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0;
1057     }
1058 
1059     if (ret_val) {
1060         *ret_val = old_mip;
1061     }
1062 
1063     return RISCV_EXCP_NONE;
1064 }
1065 
1066 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
1067                               target_ulong *ret_val,
1068                               target_ulong new_val, target_ulong wr_mask)
1069 {
1070     uint64_t rval;
1071     RISCVException ret;
1072 
1073     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask);
1074     if (ret_val) {
1075         *ret_val = rval;
1076     }
1077 
1078     return ret;
1079 }
1080 
1081 static RISCVException rmw_miph(CPURISCVState *env, int csrno,
1082                                target_ulong *ret_val,
1083                                target_ulong new_val, target_ulong wr_mask)
1084 {
1085     uint64_t rval;
1086     RISCVException ret;
1087 
1088     ret = rmw_mip64(env, csrno, &rval,
1089         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1090     if (ret_val) {
1091         *ret_val = rval >> 32;
1092     }
1093 
1094     return ret;
1095 }
1096 
1097 /* Supervisor Trap Setup */
1098 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno,
1099                                         Int128 *val)
1100 {
1101     uint64_t mask = sstatus_v1_10_mask;
1102     uint64_t sstatus = env->mstatus & mask;
1103     if (env->xl != MXL_RV32 || env->debugger) {
1104         mask |= SSTATUS64_UXL;
1105     }
1106 
1107     *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus));
1108     return RISCV_EXCP_NONE;
1109 }
1110 
1111 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
1112                                    target_ulong *val)
1113 {
1114     target_ulong mask = (sstatus_v1_10_mask);
1115     if (env->xl != MXL_RV32 || env->debugger) {
1116         mask |= SSTATUS64_UXL;
1117     }
1118     /* TODO: Use SXL not MXL. */
1119     *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask);
1120     return RISCV_EXCP_NONE;
1121 }
1122 
1123 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
1124                                     target_ulong val)
1125 {
1126     target_ulong mask = (sstatus_v1_10_mask);
1127 
1128     if (env->xl != MXL_RV32 || env->debugger) {
1129         if ((val & SSTATUS64_UXL) != 0) {
1130             mask |= SSTATUS64_UXL;
1131         }
1132     }
1133     target_ulong newval = (env->mstatus & ~mask) | (val & mask);
1134     return write_mstatus(env, CSR_MSTATUS, newval);
1135 }
1136 
1137 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno,
1138                                  uint64_t *ret_val,
1139                                  uint64_t new_val, uint64_t wr_mask)
1140 {
1141     RISCVException ret;
1142     uint64_t rval, vsbits, mask = env->hideleg & VS_MODE_INTERRUPTS;
1143 
1144     /* Bring VS-level bits to correct position */
1145     vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
1146     new_val &= ~(VS_MODE_INTERRUPTS >> 1);
1147     new_val |= vsbits << 1;
1148     vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
1149     wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
1150     wr_mask |= vsbits << 1;
1151 
1152     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & mask);
1153     if (ret_val) {
1154         rval &= mask;
1155         vsbits = rval & VS_MODE_INTERRUPTS;
1156         rval &= ~VS_MODE_INTERRUPTS;
1157         *ret_val = rval | (vsbits >> 1);
1158     }
1159 
1160     return ret;
1161 }
1162 
1163 static RISCVException rmw_vsie(CPURISCVState *env, int csrno,
1164                                target_ulong *ret_val,
1165                                target_ulong new_val, target_ulong wr_mask)
1166 {
1167     uint64_t rval;
1168     RISCVException ret;
1169 
1170     ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask);
1171     if (ret_val) {
1172         *ret_val = rval;
1173     }
1174 
1175     return ret;
1176 }
1177 
1178 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno,
1179                                 target_ulong *ret_val,
1180                                 target_ulong new_val, target_ulong wr_mask)
1181 {
1182     uint64_t rval;
1183     RISCVException ret;
1184 
1185     ret = rmw_vsie64(env, csrno, &rval,
1186         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1187     if (ret_val) {
1188         *ret_val = rval >> 32;
1189     }
1190 
1191     return ret;
1192 }
1193 
1194 static RISCVException rmw_sie64(CPURISCVState *env, int csrno,
1195                                 uint64_t *ret_val,
1196                                 uint64_t new_val, uint64_t wr_mask)
1197 {
1198     RISCVException ret;
1199     uint64_t mask = env->mideleg & S_MODE_INTERRUPTS;
1200 
1201     if (riscv_cpu_virt_enabled(env)) {
1202         if (env->hvictl & HVICTL_VTI) {
1203             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1204         }
1205         ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask);
1206     } else {
1207         ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & mask);
1208     }
1209 
1210     if (ret_val) {
1211         *ret_val &= mask;
1212     }
1213 
1214     return ret;
1215 }
1216 
1217 static RISCVException rmw_sie(CPURISCVState *env, int csrno,
1218                               target_ulong *ret_val,
1219                               target_ulong new_val, target_ulong wr_mask)
1220 {
1221     uint64_t rval;
1222     RISCVException ret;
1223 
1224     ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask);
1225     if (ret == RISCV_EXCP_NONE && ret_val) {
1226         *ret_val = rval;
1227     }
1228 
1229     return ret;
1230 }
1231 
1232 static RISCVException rmw_sieh(CPURISCVState *env, int csrno,
1233                                target_ulong *ret_val,
1234                                target_ulong new_val, target_ulong wr_mask)
1235 {
1236     uint64_t rval;
1237     RISCVException ret;
1238 
1239     ret = rmw_sie64(env, csrno, &rval,
1240         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1241     if (ret_val) {
1242         *ret_val = rval >> 32;
1243     }
1244 
1245     return ret;
1246 }
1247 
1248 static RISCVException read_stvec(CPURISCVState *env, int csrno,
1249                                  target_ulong *val)
1250 {
1251     *val = env->stvec;
1252     return RISCV_EXCP_NONE;
1253 }
1254 
1255 static RISCVException write_stvec(CPURISCVState *env, int csrno,
1256                                   target_ulong val)
1257 {
1258     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
1259     if ((val & 3) < 2) {
1260         env->stvec = val;
1261     } else {
1262         qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
1263     }
1264     return RISCV_EXCP_NONE;
1265 }
1266 
1267 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
1268                                       target_ulong *val)
1269 {
1270     *val = env->scounteren;
1271     return RISCV_EXCP_NONE;
1272 }
1273 
1274 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
1275                                        target_ulong val)
1276 {
1277     env->scounteren = val;
1278     return RISCV_EXCP_NONE;
1279 }
1280 
1281 /* Supervisor Trap Handling */
1282 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno,
1283                                          Int128 *val)
1284 {
1285     *val = int128_make128(env->sscratch, env->sscratchh);
1286     return RISCV_EXCP_NONE;
1287 }
1288 
1289 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno,
1290                                           Int128 val)
1291 {
1292     env->sscratch = int128_getlo(val);
1293     env->sscratchh = int128_gethi(val);
1294     return RISCV_EXCP_NONE;
1295 }
1296 
1297 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
1298                                     target_ulong *val)
1299 {
1300     *val = env->sscratch;
1301     return RISCV_EXCP_NONE;
1302 }
1303 
1304 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
1305                                      target_ulong val)
1306 {
1307     env->sscratch = val;
1308     return RISCV_EXCP_NONE;
1309 }
1310 
1311 static RISCVException read_sepc(CPURISCVState *env, int csrno,
1312                                 target_ulong *val)
1313 {
1314     *val = env->sepc;
1315     return RISCV_EXCP_NONE;
1316 }
1317 
1318 static RISCVException write_sepc(CPURISCVState *env, int csrno,
1319                                  target_ulong val)
1320 {
1321     env->sepc = val;
1322     return RISCV_EXCP_NONE;
1323 }
1324 
1325 static RISCVException read_scause(CPURISCVState *env, int csrno,
1326                                   target_ulong *val)
1327 {
1328     *val = env->scause;
1329     return RISCV_EXCP_NONE;
1330 }
1331 
1332 static RISCVException write_scause(CPURISCVState *env, int csrno,
1333                                    target_ulong val)
1334 {
1335     env->scause = val;
1336     return RISCV_EXCP_NONE;
1337 }
1338 
1339 static RISCVException read_stval(CPURISCVState *env, int csrno,
1340                                  target_ulong *val)
1341 {
1342     *val = env->stval;
1343     return RISCV_EXCP_NONE;
1344 }
1345 
1346 static RISCVException write_stval(CPURISCVState *env, int csrno,
1347                                   target_ulong val)
1348 {
1349     env->stval = val;
1350     return RISCV_EXCP_NONE;
1351 }
1352 
1353 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno,
1354                                  uint64_t *ret_val,
1355                                  uint64_t new_val, uint64_t wr_mask)
1356 {
1357     RISCVException ret;
1358     uint64_t rval, vsbits, mask = env->hideleg & vsip_writable_mask;
1359 
1360     /* Bring VS-level bits to correct position */
1361     vsbits = new_val & (VS_MODE_INTERRUPTS >> 1);
1362     new_val &= ~(VS_MODE_INTERRUPTS >> 1);
1363     new_val |= vsbits << 1;
1364     vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1);
1365     wr_mask &= ~(VS_MODE_INTERRUPTS >> 1);
1366     wr_mask |= vsbits << 1;
1367 
1368     ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask & mask);
1369     if (ret_val) {
1370         rval &= mask;
1371         vsbits = rval & VS_MODE_INTERRUPTS;
1372         rval &= ~VS_MODE_INTERRUPTS;
1373         *ret_val = rval | (vsbits >> 1);
1374     }
1375 
1376     return ret;
1377 }
1378 
1379 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
1380                                target_ulong *ret_val,
1381                                target_ulong new_val, target_ulong wr_mask)
1382 {
1383     uint64_t rval;
1384     RISCVException ret;
1385 
1386     ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask);
1387     if (ret_val) {
1388         *ret_val = rval;
1389     }
1390 
1391     return ret;
1392 }
1393 
1394 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno,
1395                                 target_ulong *ret_val,
1396                                 target_ulong new_val, target_ulong wr_mask)
1397 {
1398     uint64_t rval;
1399     RISCVException ret;
1400 
1401     ret = rmw_vsip64(env, csrno, &rval,
1402         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1403     if (ret_val) {
1404         *ret_val = rval >> 32;
1405     }
1406 
1407     return ret;
1408 }
1409 
1410 static RISCVException rmw_sip64(CPURISCVState *env, int csrno,
1411                                 uint64_t *ret_val,
1412                                 uint64_t new_val, uint64_t wr_mask)
1413 {
1414     RISCVException ret;
1415     uint64_t mask = env->mideleg & sip_writable_mask;
1416 
1417     if (riscv_cpu_virt_enabled(env)) {
1418         if (env->hvictl & HVICTL_VTI) {
1419             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
1420         }
1421         ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask);
1422     } else {
1423         ret = rmw_mip64(env, csrno, ret_val, new_val, wr_mask & mask);
1424     }
1425 
1426     if (ret_val) {
1427         *ret_val &= env->mideleg & S_MODE_INTERRUPTS;
1428     }
1429 
1430     return ret;
1431 }
1432 
1433 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
1434                               target_ulong *ret_val,
1435                               target_ulong new_val, target_ulong wr_mask)
1436 {
1437     uint64_t rval;
1438     RISCVException ret;
1439 
1440     ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask);
1441     if (ret_val) {
1442         *ret_val = rval;
1443     }
1444 
1445     return ret;
1446 }
1447 
1448 static RISCVException rmw_siph(CPURISCVState *env, int csrno,
1449                                target_ulong *ret_val,
1450                                target_ulong new_val, target_ulong wr_mask)
1451 {
1452     uint64_t rval;
1453     RISCVException ret;
1454 
1455     ret = rmw_sip64(env, csrno, &rval,
1456         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1457     if (ret_val) {
1458         *ret_val = rval >> 32;
1459     }
1460 
1461     return ret;
1462 }
1463 
1464 /* Supervisor Protection and Translation */
1465 static RISCVException read_satp(CPURISCVState *env, int csrno,
1466                                 target_ulong *val)
1467 {
1468     if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
1469         *val = 0;
1470         return RISCV_EXCP_NONE;
1471     }
1472 
1473     if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
1474         return RISCV_EXCP_ILLEGAL_INST;
1475     } else {
1476         *val = env->satp;
1477     }
1478 
1479     return RISCV_EXCP_NONE;
1480 }
1481 
1482 static RISCVException write_satp(CPURISCVState *env, int csrno,
1483                                  target_ulong val)
1484 {
1485     target_ulong vm, mask, asid;
1486 
1487     if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
1488         return RISCV_EXCP_NONE;
1489     }
1490 
1491     if (riscv_cpu_mxl(env) == MXL_RV32) {
1492         vm = validate_vm(env, get_field(val, SATP32_MODE));
1493         mask = (val ^ env->satp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
1494         asid = (val ^ env->satp) & SATP32_ASID;
1495     } else {
1496         vm = validate_vm(env, get_field(val, SATP64_MODE));
1497         mask = (val ^ env->satp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
1498         asid = (val ^ env->satp) & SATP64_ASID;
1499     }
1500 
1501     if (vm && mask) {
1502         if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
1503             return RISCV_EXCP_ILLEGAL_INST;
1504         } else {
1505             if (asid) {
1506                 tlb_flush(env_cpu(env));
1507             }
1508             env->satp = val;
1509         }
1510     }
1511     return RISCV_EXCP_NONE;
1512 }
1513 
1514 static int read_vstopi(CPURISCVState *env, int csrno, target_ulong *val)
1515 {
1516     int irq, ret;
1517     target_ulong topei;
1518     uint64_t vseip, vsgein;
1519     uint32_t iid, iprio, hviid, hviprio, gein;
1520     uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS];
1521 
1522     gein = get_field(env->hstatus, HSTATUS_VGEIN);
1523     hviid = get_field(env->hvictl, HVICTL_IID);
1524     hviprio = get_field(env->hvictl, HVICTL_IPRIO);
1525 
1526     if (gein) {
1527         vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0;
1528         vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP;
1529         if (gein <= env->geilen && vseip) {
1530             siid[scount] = IRQ_S_EXT;
1531             siprio[scount] = IPRIO_MMAXIPRIO + 1;
1532             if (env->aia_ireg_rmw_fn[PRV_S]) {
1533                 /*
1534                  * Call machine specific IMSIC register emulation for
1535                  * reading TOPEI.
1536                  */
1537                 ret = env->aia_ireg_rmw_fn[PRV_S](
1538                         env->aia_ireg_rmw_fn_arg[PRV_S],
1539                         AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein,
1540                                       riscv_cpu_mxl_bits(env)),
1541                         &topei, 0, 0);
1542                 if (!ret && topei) {
1543                     siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK;
1544                 }
1545             }
1546             scount++;
1547         }
1548     } else {
1549         if (hviid == IRQ_S_EXT && hviprio) {
1550             siid[scount] = IRQ_S_EXT;
1551             siprio[scount] = hviprio;
1552             scount++;
1553         }
1554     }
1555 
1556     if (env->hvictl & HVICTL_VTI) {
1557         if (hviid != IRQ_S_EXT) {
1558             siid[scount] = hviid;
1559             siprio[scount] = hviprio;
1560             scount++;
1561         }
1562     } else {
1563         irq = riscv_cpu_vsirq_pending(env);
1564         if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) {
1565             siid[scount] = irq;
1566             siprio[scount] = env->hviprio[irq];
1567             scount++;
1568         }
1569     }
1570 
1571     iid = 0;
1572     iprio = UINT_MAX;
1573     for (s = 0; s < scount; s++) {
1574         if (siprio[s] < iprio) {
1575             iid = siid[s];
1576             iprio = siprio[s];
1577         }
1578     }
1579 
1580     if (iid) {
1581         if (env->hvictl & HVICTL_IPRIOM) {
1582             if (iprio > IPRIO_MMAXIPRIO) {
1583                 iprio = IPRIO_MMAXIPRIO;
1584             }
1585             if (!iprio) {
1586                 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) {
1587                     iprio = IPRIO_MMAXIPRIO;
1588                 }
1589             }
1590         } else {
1591             iprio = 1;
1592         }
1593     } else {
1594         iprio = 0;
1595     }
1596 
1597     *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1598     *val |= iprio;
1599     return RISCV_EXCP_NONE;
1600 }
1601 
1602 static int read_stopi(CPURISCVState *env, int csrno, target_ulong *val)
1603 {
1604     int irq;
1605     uint8_t iprio;
1606 
1607     if (riscv_cpu_virt_enabled(env)) {
1608         return read_vstopi(env, CSR_VSTOPI, val);
1609     }
1610 
1611     irq = riscv_cpu_sirq_pending(env);
1612     if (irq <= 0 || irq > 63) {
1613         *val = 0;
1614     } else {
1615         iprio = env->siprio[irq];
1616         if (!iprio) {
1617             if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) {
1618                 iprio = IPRIO_MMAXIPRIO;
1619            }
1620         }
1621         *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT;
1622         *val |= iprio;
1623     }
1624 
1625     return RISCV_EXCP_NONE;
1626 }
1627 
1628 /* Hypervisor Extensions */
1629 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
1630                                    target_ulong *val)
1631 {
1632     *val = env->hstatus;
1633     if (riscv_cpu_mxl(env) != MXL_RV32) {
1634         /* We only support 64-bit VSXL */
1635         *val = set_field(*val, HSTATUS_VSXL, 2);
1636     }
1637     /* We only support little endian */
1638     *val = set_field(*val, HSTATUS_VSBE, 0);
1639     return RISCV_EXCP_NONE;
1640 }
1641 
1642 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
1643                                     target_ulong val)
1644 {
1645     env->hstatus = val;
1646     if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
1647         qemu_log_mask(LOG_UNIMP, "QEMU does not support mixed HSXLEN options.");
1648     }
1649     if (get_field(val, HSTATUS_VSBE) != 0) {
1650         qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
1651     }
1652     return RISCV_EXCP_NONE;
1653 }
1654 
1655 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
1656                                    target_ulong *val)
1657 {
1658     *val = env->hedeleg;
1659     return RISCV_EXCP_NONE;
1660 }
1661 
1662 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
1663                                     target_ulong val)
1664 {
1665     env->hedeleg = val & vs_delegable_excps;
1666     return RISCV_EXCP_NONE;
1667 }
1668 
1669 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno,
1670                                     uint64_t *ret_val,
1671                                     uint64_t new_val, uint64_t wr_mask)
1672 {
1673     uint64_t mask = wr_mask & vs_delegable_ints;
1674 
1675     if (ret_val) {
1676         *ret_val = env->hideleg & vs_delegable_ints;
1677     }
1678 
1679     env->hideleg = (env->hideleg & ~mask) | (new_val & mask);
1680     return RISCV_EXCP_NONE;
1681 }
1682 
1683 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno,
1684                                   target_ulong *ret_val,
1685                                   target_ulong new_val, target_ulong wr_mask)
1686 {
1687     uint64_t rval;
1688     RISCVException ret;
1689 
1690     ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask);
1691     if (ret_val) {
1692         *ret_val = rval;
1693     }
1694 
1695     return ret;
1696 }
1697 
1698 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno,
1699                                    target_ulong *ret_val,
1700                                    target_ulong new_val, target_ulong wr_mask)
1701 {
1702     uint64_t rval;
1703     RISCVException ret;
1704 
1705     ret = rmw_hideleg64(env, csrno, &rval,
1706         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1707     if (ret_val) {
1708         *ret_val = rval >> 32;
1709     }
1710 
1711     return ret;
1712 }
1713 
1714 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
1715                                  uint64_t *ret_val,
1716                                  uint64_t new_val, uint64_t wr_mask)
1717 {
1718     RISCVException ret;
1719 
1720     ret = rmw_mip64(env, csrno, ret_val, new_val,
1721                     wr_mask & hvip_writable_mask);
1722     if (ret_val) {
1723         *ret_val &= VS_MODE_INTERRUPTS;
1724     }
1725 
1726     return ret;
1727 }
1728 
1729 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
1730                                target_ulong *ret_val,
1731                                target_ulong new_val, target_ulong wr_mask)
1732 {
1733     uint64_t rval;
1734     RISCVException ret;
1735 
1736     ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask);
1737     if (ret_val) {
1738         *ret_val = rval;
1739     }
1740 
1741     return ret;
1742 }
1743 
1744 static RISCVException rmw_hviph(CPURISCVState *env, int csrno,
1745                                 target_ulong *ret_val,
1746                                 target_ulong new_val, target_ulong wr_mask)
1747 {
1748     uint64_t rval;
1749     RISCVException ret;
1750 
1751     ret = rmw_hvip64(env, csrno, &rval,
1752         ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
1753     if (ret_val) {
1754         *ret_val = rval >> 32;
1755     }
1756 
1757     return ret;
1758 }
1759 
1760 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
1761                               target_ulong *ret_value,
1762                               target_ulong new_value, target_ulong write_mask)
1763 {
1764     int ret = rmw_mip(env, csrno, ret_value, new_value,
1765                       write_mask & hip_writable_mask);
1766 
1767     if (ret_value) {
1768         *ret_value &= HS_MODE_INTERRUPTS;
1769     }
1770     return ret;
1771 }
1772 
1773 static RISCVException rmw_hie(CPURISCVState *env, int csrno,
1774                               target_ulong *ret_val,
1775                               target_ulong new_val, target_ulong wr_mask)
1776 {
1777     uint64_t rval;
1778     RISCVException ret;
1779 
1780     ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS);
1781     if (ret_val) {
1782         *ret_val = rval & HS_MODE_INTERRUPTS;
1783     }
1784 
1785     return ret;
1786 }
1787 
1788 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
1789                                       target_ulong *val)
1790 {
1791     *val = env->hcounteren;
1792     return RISCV_EXCP_NONE;
1793 }
1794 
1795 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
1796                                        target_ulong val)
1797 {
1798     env->hcounteren = val;
1799     return RISCV_EXCP_NONE;
1800 }
1801 
1802 static RISCVException read_hgeie(CPURISCVState *env, int csrno,
1803                                  target_ulong *val)
1804 {
1805     if (val) {
1806         *val = env->hgeie;
1807     }
1808     return RISCV_EXCP_NONE;
1809 }
1810 
1811 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
1812                                   target_ulong val)
1813 {
1814     /* Only GEILEN:1 bits implemented and BIT0 is never implemented */
1815     val &= ((((target_ulong)1) << env->geilen) - 1) << 1;
1816     env->hgeie = val;
1817     /* Update mip.SGEIP bit */
1818     riscv_cpu_update_mip(env_archcpu(env), MIP_SGEIP,
1819                          BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
1820     return RISCV_EXCP_NONE;
1821 }
1822 
1823 static RISCVException read_htval(CPURISCVState *env, int csrno,
1824                                  target_ulong *val)
1825 {
1826     *val = env->htval;
1827     return RISCV_EXCP_NONE;
1828 }
1829 
1830 static RISCVException write_htval(CPURISCVState *env, int csrno,
1831                                   target_ulong val)
1832 {
1833     env->htval = val;
1834     return RISCV_EXCP_NONE;
1835 }
1836 
1837 static RISCVException read_htinst(CPURISCVState *env, int csrno,
1838                                   target_ulong *val)
1839 {
1840     *val = env->htinst;
1841     return RISCV_EXCP_NONE;
1842 }
1843 
1844 static RISCVException write_htinst(CPURISCVState *env, int csrno,
1845                                    target_ulong val)
1846 {
1847     return RISCV_EXCP_NONE;
1848 }
1849 
1850 static RISCVException read_hgeip(CPURISCVState *env, int csrno,
1851                                  target_ulong *val)
1852 {
1853     if (val) {
1854         *val = env->hgeip;
1855     }
1856     return RISCV_EXCP_NONE;
1857 }
1858 
1859 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
1860                                  target_ulong *val)
1861 {
1862     *val = env->hgatp;
1863     return RISCV_EXCP_NONE;
1864 }
1865 
1866 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
1867                                   target_ulong val)
1868 {
1869     env->hgatp = val;
1870     return RISCV_EXCP_NONE;
1871 }
1872 
1873 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
1874                                       target_ulong *val)
1875 {
1876     if (!env->rdtime_fn) {
1877         return RISCV_EXCP_ILLEGAL_INST;
1878     }
1879 
1880     *val = env->htimedelta;
1881     return RISCV_EXCP_NONE;
1882 }
1883 
1884 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
1885                                        target_ulong val)
1886 {
1887     if (!env->rdtime_fn) {
1888         return RISCV_EXCP_ILLEGAL_INST;
1889     }
1890 
1891     if (riscv_cpu_mxl(env) == MXL_RV32) {
1892         env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
1893     } else {
1894         env->htimedelta = val;
1895     }
1896     return RISCV_EXCP_NONE;
1897 }
1898 
1899 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
1900                                        target_ulong *val)
1901 {
1902     if (!env->rdtime_fn) {
1903         return RISCV_EXCP_ILLEGAL_INST;
1904     }
1905 
1906     *val = env->htimedelta >> 32;
1907     return RISCV_EXCP_NONE;
1908 }
1909 
1910 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
1911                                         target_ulong val)
1912 {
1913     if (!env->rdtime_fn) {
1914         return RISCV_EXCP_ILLEGAL_INST;
1915     }
1916 
1917     env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
1918     return RISCV_EXCP_NONE;
1919 }
1920 
1921 static int read_hvictl(CPURISCVState *env, int csrno, target_ulong *val)
1922 {
1923     *val = env->hvictl;
1924     return RISCV_EXCP_NONE;
1925 }
1926 
1927 static int write_hvictl(CPURISCVState *env, int csrno, target_ulong val)
1928 {
1929     env->hvictl = val & HVICTL_VALID_MASK;
1930     return RISCV_EXCP_NONE;
1931 }
1932 
1933 static int read_hvipriox(CPURISCVState *env, int first_index,
1934                          uint8_t *iprio, target_ulong *val)
1935 {
1936     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
1937 
1938     /* First index has to be a multiple of number of irqs per register */
1939     if (first_index % num_irqs) {
1940         return (riscv_cpu_virt_enabled(env)) ?
1941                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1942     }
1943 
1944     /* Fill-up return value */
1945     *val = 0;
1946     for (i = 0; i < num_irqs; i++) {
1947         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
1948             continue;
1949         }
1950         if (rdzero) {
1951             continue;
1952         }
1953         *val |= ((target_ulong)iprio[irq]) << (i * 8);
1954     }
1955 
1956     return RISCV_EXCP_NONE;
1957 }
1958 
1959 static int write_hvipriox(CPURISCVState *env, int first_index,
1960                           uint8_t *iprio, target_ulong val)
1961 {
1962     int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32);
1963 
1964     /* First index has to be a multiple of number of irqs per register */
1965     if (first_index % num_irqs) {
1966         return (riscv_cpu_virt_enabled(env)) ?
1967                RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
1968     }
1969 
1970     /* Fill-up priority arrary */
1971     for (i = 0; i < num_irqs; i++) {
1972         if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) {
1973             continue;
1974         }
1975         if (rdzero) {
1976             iprio[irq] = 0;
1977         } else {
1978             iprio[irq] = (val >> (i * 8)) & 0xff;
1979         }
1980     }
1981 
1982     return RISCV_EXCP_NONE;
1983 }
1984 
1985 static int read_hviprio1(CPURISCVState *env, int csrno, target_ulong *val)
1986 {
1987     return read_hvipriox(env, 0, env->hviprio, val);
1988 }
1989 
1990 static int write_hviprio1(CPURISCVState *env, int csrno, target_ulong val)
1991 {
1992     return write_hvipriox(env, 0, env->hviprio, val);
1993 }
1994 
1995 static int read_hviprio1h(CPURISCVState *env, int csrno, target_ulong *val)
1996 {
1997     return read_hvipriox(env, 4, env->hviprio, val);
1998 }
1999 
2000 static int write_hviprio1h(CPURISCVState *env, int csrno, target_ulong val)
2001 {
2002     return write_hvipriox(env, 4, env->hviprio, val);
2003 }
2004 
2005 static int read_hviprio2(CPURISCVState *env, int csrno, target_ulong *val)
2006 {
2007     return read_hvipriox(env, 8, env->hviprio, val);
2008 }
2009 
2010 static int write_hviprio2(CPURISCVState *env, int csrno, target_ulong val)
2011 {
2012     return write_hvipriox(env, 8, env->hviprio, val);
2013 }
2014 
2015 static int read_hviprio2h(CPURISCVState *env, int csrno, target_ulong *val)
2016 {
2017     return read_hvipriox(env, 12, env->hviprio, val);
2018 }
2019 
2020 static int write_hviprio2h(CPURISCVState *env, int csrno, target_ulong val)
2021 {
2022     return write_hvipriox(env, 12, env->hviprio, val);
2023 }
2024 
2025 /* Virtual CSR Registers */
2026 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
2027                                     target_ulong *val)
2028 {
2029     *val = env->vsstatus;
2030     return RISCV_EXCP_NONE;
2031 }
2032 
2033 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
2034                                      target_ulong val)
2035 {
2036     uint64_t mask = (target_ulong)-1;
2037     if ((val & VSSTATUS64_UXL) == 0) {
2038         mask &= ~VSSTATUS64_UXL;
2039     }
2040     env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
2041     return RISCV_EXCP_NONE;
2042 }
2043 
2044 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val)
2045 {
2046     *val = env->vstvec;
2047     return RISCV_EXCP_NONE;
2048 }
2049 
2050 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
2051                                    target_ulong val)
2052 {
2053     env->vstvec = val;
2054     return RISCV_EXCP_NONE;
2055 }
2056 
2057 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
2058                                      target_ulong *val)
2059 {
2060     *val = env->vsscratch;
2061     return RISCV_EXCP_NONE;
2062 }
2063 
2064 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
2065                                       target_ulong val)
2066 {
2067     env->vsscratch = val;
2068     return RISCV_EXCP_NONE;
2069 }
2070 
2071 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
2072                                  target_ulong *val)
2073 {
2074     *val = env->vsepc;
2075     return RISCV_EXCP_NONE;
2076 }
2077 
2078 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
2079                                   target_ulong val)
2080 {
2081     env->vsepc = val;
2082     return RISCV_EXCP_NONE;
2083 }
2084 
2085 static RISCVException read_vscause(CPURISCVState *env, int csrno,
2086                                    target_ulong *val)
2087 {
2088     *val = env->vscause;
2089     return RISCV_EXCP_NONE;
2090 }
2091 
2092 static RISCVException write_vscause(CPURISCVState *env, int csrno,
2093                                     target_ulong val)
2094 {
2095     env->vscause = val;
2096     return RISCV_EXCP_NONE;
2097 }
2098 
2099 static RISCVException read_vstval(CPURISCVState *env, int csrno,
2100                                   target_ulong *val)
2101 {
2102     *val = env->vstval;
2103     return RISCV_EXCP_NONE;
2104 }
2105 
2106 static RISCVException write_vstval(CPURISCVState *env, int csrno,
2107                                    target_ulong val)
2108 {
2109     env->vstval = val;
2110     return RISCV_EXCP_NONE;
2111 }
2112 
2113 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
2114                                  target_ulong *val)
2115 {
2116     *val = env->vsatp;
2117     return RISCV_EXCP_NONE;
2118 }
2119 
2120 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
2121                                   target_ulong val)
2122 {
2123     env->vsatp = val;
2124     return RISCV_EXCP_NONE;
2125 }
2126 
2127 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
2128                                   target_ulong *val)
2129 {
2130     *val = env->mtval2;
2131     return RISCV_EXCP_NONE;
2132 }
2133 
2134 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
2135                                    target_ulong val)
2136 {
2137     env->mtval2 = val;
2138     return RISCV_EXCP_NONE;
2139 }
2140 
2141 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
2142                                   target_ulong *val)
2143 {
2144     *val = env->mtinst;
2145     return RISCV_EXCP_NONE;
2146 }
2147 
2148 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
2149                                    target_ulong val)
2150 {
2151     env->mtinst = val;
2152     return RISCV_EXCP_NONE;
2153 }
2154 
2155 /* Physical Memory Protection */
2156 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
2157                                    target_ulong *val)
2158 {
2159     *val = mseccfg_csr_read(env);
2160     return RISCV_EXCP_NONE;
2161 }
2162 
2163 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
2164                          target_ulong val)
2165 {
2166     mseccfg_csr_write(env, val);
2167     return RISCV_EXCP_NONE;
2168 }
2169 
2170 static bool check_pmp_reg_index(CPURISCVState *env, uint32_t reg_index)
2171 {
2172     /* TODO: RV128 restriction check */
2173     if ((reg_index & 1) && (riscv_cpu_mxl(env) == MXL_RV64)) {
2174         return false;
2175     }
2176     return true;
2177 }
2178 
2179 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
2180                                   target_ulong *val)
2181 {
2182     uint32_t reg_index = csrno - CSR_PMPCFG0;
2183 
2184     if (!check_pmp_reg_index(env, reg_index)) {
2185         return RISCV_EXCP_ILLEGAL_INST;
2186     }
2187     *val = pmpcfg_csr_read(env, csrno - CSR_PMPCFG0);
2188     return RISCV_EXCP_NONE;
2189 }
2190 
2191 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
2192                                    target_ulong val)
2193 {
2194     uint32_t reg_index = csrno - CSR_PMPCFG0;
2195 
2196     if (!check_pmp_reg_index(env, reg_index)) {
2197         return RISCV_EXCP_ILLEGAL_INST;
2198     }
2199     pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val);
2200     return RISCV_EXCP_NONE;
2201 }
2202 
2203 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
2204                                    target_ulong *val)
2205 {
2206     *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
2207     return RISCV_EXCP_NONE;
2208 }
2209 
2210 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
2211                                     target_ulong val)
2212 {
2213     pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
2214     return RISCV_EXCP_NONE;
2215 }
2216 
2217 /*
2218  * Functions to access Pointer Masking feature registers
2219  * We have to check if current priv lvl could modify
2220  * csr in given mode
2221  */
2222 static bool check_pm_current_disabled(CPURISCVState *env, int csrno)
2223 {
2224     int csr_priv = get_field(csrno, 0x300);
2225     int pm_current;
2226 
2227     if (env->debugger) {
2228         return false;
2229     }
2230     /*
2231      * If priv lvls differ that means we're accessing csr from higher priv lvl,
2232      * so allow the access
2233      */
2234     if (env->priv != csr_priv) {
2235         return false;
2236     }
2237     switch (env->priv) {
2238     case PRV_M:
2239         pm_current = get_field(env->mmte, M_PM_CURRENT);
2240         break;
2241     case PRV_S:
2242         pm_current = get_field(env->mmte, S_PM_CURRENT);
2243         break;
2244     case PRV_U:
2245         pm_current = get_field(env->mmte, U_PM_CURRENT);
2246         break;
2247     default:
2248         g_assert_not_reached();
2249     }
2250     /* It's same priv lvl, so we allow to modify csr only if pm.current==1 */
2251     return !pm_current;
2252 }
2253 
2254 static RISCVException read_mmte(CPURISCVState *env, int csrno,
2255                                 target_ulong *val)
2256 {
2257     *val = env->mmte & MMTE_MASK;
2258     return RISCV_EXCP_NONE;
2259 }
2260 
2261 static RISCVException write_mmte(CPURISCVState *env, int csrno,
2262                                  target_ulong val)
2263 {
2264     uint64_t mstatus;
2265     target_ulong wpri_val = val & MMTE_MASK;
2266 
2267     if (val != wpri_val) {
2268         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s" TARGET_FMT_lx "\n",
2269                       "MMTE: WPRI violation written 0x", val,
2270                       "vs expected 0x", wpri_val);
2271     }
2272     /* for machine mode pm.current is hardwired to 1 */
2273     wpri_val |= MMTE_M_PM_CURRENT;
2274 
2275     /* hardwiring pm.instruction bit to 0, since it's not supported yet */
2276     wpri_val &= ~(MMTE_M_PM_INSN | MMTE_S_PM_INSN | MMTE_U_PM_INSN);
2277     env->mmte = wpri_val | PM_EXT_DIRTY;
2278     riscv_cpu_update_mask(env);
2279 
2280     /* Set XS and SD bits, since PM CSRs are dirty */
2281     mstatus = env->mstatus | MSTATUS_XS;
2282     write_mstatus(env, csrno, mstatus);
2283     return RISCV_EXCP_NONE;
2284 }
2285 
2286 static RISCVException read_smte(CPURISCVState *env, int csrno,
2287                                 target_ulong *val)
2288 {
2289     *val = env->mmte & SMTE_MASK;
2290     return RISCV_EXCP_NONE;
2291 }
2292 
2293 static RISCVException write_smte(CPURISCVState *env, int csrno,
2294                                  target_ulong val)
2295 {
2296     target_ulong wpri_val = val & SMTE_MASK;
2297 
2298     if (val != wpri_val) {
2299         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s" TARGET_FMT_lx "\n",
2300                       "SMTE: WPRI violation written 0x", val,
2301                       "vs expected 0x", wpri_val);
2302     }
2303 
2304     /* if pm.current==0 we can't modify current PM CSRs */
2305     if (check_pm_current_disabled(env, csrno)) {
2306         return RISCV_EXCP_NONE;
2307     }
2308 
2309     wpri_val |= (env->mmte & ~SMTE_MASK);
2310     write_mmte(env, csrno, wpri_val);
2311     return RISCV_EXCP_NONE;
2312 }
2313 
2314 static RISCVException read_umte(CPURISCVState *env, int csrno,
2315                                 target_ulong *val)
2316 {
2317     *val = env->mmte & UMTE_MASK;
2318     return RISCV_EXCP_NONE;
2319 }
2320 
2321 static RISCVException write_umte(CPURISCVState *env, int csrno,
2322                                  target_ulong val)
2323 {
2324     target_ulong wpri_val = val & UMTE_MASK;
2325 
2326     if (val != wpri_val) {
2327         qemu_log_mask(LOG_GUEST_ERROR, "%s" TARGET_FMT_lx " %s" TARGET_FMT_lx "\n",
2328                       "UMTE: WPRI violation written 0x", val,
2329                       "vs expected 0x", wpri_val);
2330     }
2331 
2332     if (check_pm_current_disabled(env, csrno)) {
2333         return RISCV_EXCP_NONE;
2334     }
2335 
2336     wpri_val |= (env->mmte & ~UMTE_MASK);
2337     write_mmte(env, csrno, wpri_val);
2338     return RISCV_EXCP_NONE;
2339 }
2340 
2341 static RISCVException read_mpmmask(CPURISCVState *env, int csrno,
2342                                    target_ulong *val)
2343 {
2344     *val = env->mpmmask;
2345     return RISCV_EXCP_NONE;
2346 }
2347 
2348 static RISCVException write_mpmmask(CPURISCVState *env, int csrno,
2349                                     target_ulong val)
2350 {
2351     uint64_t mstatus;
2352 
2353     env->mpmmask = val;
2354     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
2355         env->cur_pmmask = val;
2356     }
2357     env->mmte |= PM_EXT_DIRTY;
2358 
2359     /* Set XS and SD bits, since PM CSRs are dirty */
2360     mstatus = env->mstatus | MSTATUS_XS;
2361     write_mstatus(env, csrno, mstatus);
2362     return RISCV_EXCP_NONE;
2363 }
2364 
2365 static RISCVException read_spmmask(CPURISCVState *env, int csrno,
2366                                    target_ulong *val)
2367 {
2368     *val = env->spmmask;
2369     return RISCV_EXCP_NONE;
2370 }
2371 
2372 static RISCVException write_spmmask(CPURISCVState *env, int csrno,
2373                                     target_ulong val)
2374 {
2375     uint64_t mstatus;
2376 
2377     /* if pm.current==0 we can't modify current PM CSRs */
2378     if (check_pm_current_disabled(env, csrno)) {
2379         return RISCV_EXCP_NONE;
2380     }
2381     env->spmmask = val;
2382     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
2383         env->cur_pmmask = val;
2384     }
2385     env->mmte |= PM_EXT_DIRTY;
2386 
2387     /* Set XS and SD bits, since PM CSRs are dirty */
2388     mstatus = env->mstatus | MSTATUS_XS;
2389     write_mstatus(env, csrno, mstatus);
2390     return RISCV_EXCP_NONE;
2391 }
2392 
2393 static RISCVException read_upmmask(CPURISCVState *env, int csrno,
2394                                    target_ulong *val)
2395 {
2396     *val = env->upmmask;
2397     return RISCV_EXCP_NONE;
2398 }
2399 
2400 static RISCVException write_upmmask(CPURISCVState *env, int csrno,
2401                                     target_ulong val)
2402 {
2403     uint64_t mstatus;
2404 
2405     /* if pm.current==0 we can't modify current PM CSRs */
2406     if (check_pm_current_disabled(env, csrno)) {
2407         return RISCV_EXCP_NONE;
2408     }
2409     env->upmmask = val;
2410     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
2411         env->cur_pmmask = val;
2412     }
2413     env->mmte |= PM_EXT_DIRTY;
2414 
2415     /* Set XS and SD bits, since PM CSRs are dirty */
2416     mstatus = env->mstatus | MSTATUS_XS;
2417     write_mstatus(env, csrno, mstatus);
2418     return RISCV_EXCP_NONE;
2419 }
2420 
2421 static RISCVException read_mpmbase(CPURISCVState *env, int csrno,
2422                                    target_ulong *val)
2423 {
2424     *val = env->mpmbase;
2425     return RISCV_EXCP_NONE;
2426 }
2427 
2428 static RISCVException write_mpmbase(CPURISCVState *env, int csrno,
2429                                     target_ulong val)
2430 {
2431     uint64_t mstatus;
2432 
2433     env->mpmbase = val;
2434     if ((env->priv == PRV_M) && (env->mmte & M_PM_ENABLE)) {
2435         env->cur_pmbase = val;
2436     }
2437     env->mmte |= PM_EXT_DIRTY;
2438 
2439     /* Set XS and SD bits, since PM CSRs are dirty */
2440     mstatus = env->mstatus | MSTATUS_XS;
2441     write_mstatus(env, csrno, mstatus);
2442     return RISCV_EXCP_NONE;
2443 }
2444 
2445 static RISCVException read_spmbase(CPURISCVState *env, int csrno,
2446                                    target_ulong *val)
2447 {
2448     *val = env->spmbase;
2449     return RISCV_EXCP_NONE;
2450 }
2451 
2452 static RISCVException write_spmbase(CPURISCVState *env, int csrno,
2453                                     target_ulong val)
2454 {
2455     uint64_t mstatus;
2456 
2457     /* if pm.current==0 we can't modify current PM CSRs */
2458     if (check_pm_current_disabled(env, csrno)) {
2459         return RISCV_EXCP_NONE;
2460     }
2461     env->spmbase = val;
2462     if ((env->priv == PRV_S) && (env->mmte & S_PM_ENABLE)) {
2463         env->cur_pmbase = val;
2464     }
2465     env->mmte |= PM_EXT_DIRTY;
2466 
2467     /* Set XS and SD bits, since PM CSRs are dirty */
2468     mstatus = env->mstatus | MSTATUS_XS;
2469     write_mstatus(env, csrno, mstatus);
2470     return RISCV_EXCP_NONE;
2471 }
2472 
2473 static RISCVException read_upmbase(CPURISCVState *env, int csrno,
2474                                    target_ulong *val)
2475 {
2476     *val = env->upmbase;
2477     return RISCV_EXCP_NONE;
2478 }
2479 
2480 static RISCVException write_upmbase(CPURISCVState *env, int csrno,
2481                                     target_ulong val)
2482 {
2483     uint64_t mstatus;
2484 
2485     /* if pm.current==0 we can't modify current PM CSRs */
2486     if (check_pm_current_disabled(env, csrno)) {
2487         return RISCV_EXCP_NONE;
2488     }
2489     env->upmbase = val;
2490     if ((env->priv == PRV_U) && (env->mmte & U_PM_ENABLE)) {
2491         env->cur_pmbase = val;
2492     }
2493     env->mmte |= PM_EXT_DIRTY;
2494 
2495     /* Set XS and SD bits, since PM CSRs are dirty */
2496     mstatus = env->mstatus | MSTATUS_XS;
2497     write_mstatus(env, csrno, mstatus);
2498     return RISCV_EXCP_NONE;
2499 }
2500 
2501 #endif
2502 
2503 /*
2504  * riscv_csrrw - read and/or update control and status register
2505  *
2506  * csrr   <->  riscv_csrrw(env, csrno, ret_value, 0, 0);
2507  * csrrw  <->  riscv_csrrw(env, csrno, ret_value, value, -1);
2508  * csrrs  <->  riscv_csrrw(env, csrno, ret_value, -1, value);
2509  * csrrc  <->  riscv_csrrw(env, csrno, ret_value, 0, value);
2510  */
2511 
2512 static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
2513                                                int csrno,
2514                                                bool write_mask,
2515                                                RISCVCPU *cpu)
2516 {
2517     /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
2518     int read_only = get_field(csrno, 0xC00) == 3;
2519 #if !defined(CONFIG_USER_ONLY)
2520     int effective_priv = env->priv;
2521 
2522     if (riscv_has_ext(env, RVH) &&
2523         env->priv == PRV_S &&
2524         !riscv_cpu_virt_enabled(env)) {
2525         /*
2526          * We are in S mode without virtualisation, therefore we are in HS Mode.
2527          * Add 1 to the effective privledge level to allow us to access the
2528          * Hypervisor CSRs.
2529          */
2530         effective_priv++;
2531     }
2532 
2533     if (!env->debugger && (effective_priv < get_field(csrno, 0x300))) {
2534         return RISCV_EXCP_ILLEGAL_INST;
2535     }
2536 #endif
2537     if (write_mask && read_only) {
2538         return RISCV_EXCP_ILLEGAL_INST;
2539     }
2540 
2541     /* ensure the CSR extension is enabled. */
2542     if (!cpu->cfg.ext_icsr) {
2543         return RISCV_EXCP_ILLEGAL_INST;
2544     }
2545 
2546     /* check predicate */
2547     if (!csr_ops[csrno].predicate) {
2548         return RISCV_EXCP_ILLEGAL_INST;
2549     }
2550 
2551     return csr_ops[csrno].predicate(env, csrno);
2552 }
2553 
2554 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,
2555                                        target_ulong *ret_value,
2556                                        target_ulong new_value,
2557                                        target_ulong write_mask)
2558 {
2559     RISCVException ret;
2560     target_ulong old_value;
2561 
2562     /* execute combined read/write operation if it exists */
2563     if (csr_ops[csrno].op) {
2564         return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
2565     }
2566 
2567     /* if no accessor exists then return failure */
2568     if (!csr_ops[csrno].read) {
2569         return RISCV_EXCP_ILLEGAL_INST;
2570     }
2571     /* read old value */
2572     ret = csr_ops[csrno].read(env, csrno, &old_value);
2573     if (ret != RISCV_EXCP_NONE) {
2574         return ret;
2575     }
2576 
2577     /* write value if writable and write mask set, otherwise drop writes */
2578     if (write_mask) {
2579         new_value = (old_value & ~write_mask) | (new_value & write_mask);
2580         if (csr_ops[csrno].write) {
2581             ret = csr_ops[csrno].write(env, csrno, new_value);
2582             if (ret != RISCV_EXCP_NONE) {
2583                 return ret;
2584             }
2585         }
2586     }
2587 
2588     /* return old value */
2589     if (ret_value) {
2590         *ret_value = old_value;
2591     }
2592 
2593     return RISCV_EXCP_NONE;
2594 }
2595 
2596 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
2597                            target_ulong *ret_value,
2598                            target_ulong new_value, target_ulong write_mask)
2599 {
2600     RISCVCPU *cpu = env_archcpu(env);
2601 
2602     RISCVException ret = riscv_csrrw_check(env, csrno, write_mask, cpu);
2603     if (ret != RISCV_EXCP_NONE) {
2604         return ret;
2605     }
2606 
2607     return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask);
2608 }
2609 
2610 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,
2611                                         Int128 *ret_value,
2612                                         Int128 new_value,
2613                                         Int128 write_mask)
2614 {
2615     RISCVException ret;
2616     Int128 old_value;
2617 
2618     /* read old value */
2619     ret = csr_ops[csrno].read128(env, csrno, &old_value);
2620     if (ret != RISCV_EXCP_NONE) {
2621         return ret;
2622     }
2623 
2624     /* write value if writable and write mask set, otherwise drop writes */
2625     if (int128_nz(write_mask)) {
2626         new_value = int128_or(int128_and(old_value, int128_not(write_mask)),
2627                               int128_and(new_value, write_mask));
2628         if (csr_ops[csrno].write128) {
2629             ret = csr_ops[csrno].write128(env, csrno, new_value);
2630             if (ret != RISCV_EXCP_NONE) {
2631                 return ret;
2632             }
2633         } else if (csr_ops[csrno].write) {
2634             /* avoids having to write wrappers for all registers */
2635             ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value));
2636             if (ret != RISCV_EXCP_NONE) {
2637                 return ret;
2638             }
2639         }
2640     }
2641 
2642     /* return old value */
2643     if (ret_value) {
2644         *ret_value = old_value;
2645     }
2646 
2647     return RISCV_EXCP_NONE;
2648 }
2649 
2650 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
2651                                 Int128 *ret_value,
2652                                 Int128 new_value, Int128 write_mask)
2653 {
2654     RISCVException ret;
2655     RISCVCPU *cpu = env_archcpu(env);
2656 
2657     ret = riscv_csrrw_check(env, csrno, int128_nz(write_mask), cpu);
2658     if (ret != RISCV_EXCP_NONE) {
2659         return ret;
2660     }
2661 
2662     if (csr_ops[csrno].read128) {
2663         return riscv_csrrw_do128(env, csrno, ret_value, new_value, write_mask);
2664     }
2665 
2666     /*
2667      * Fall back to 64-bit version for now, if the 128-bit alternative isn't
2668      * at all defined.
2669      * Note, some CSRs don't need to extend to MXLEN (64 upper bits non
2670      * significant), for those, this fallback is correctly handling the accesses
2671      */
2672     target_ulong old_value;
2673     ret = riscv_csrrw_do64(env, csrno, &old_value,
2674                            int128_getlo(new_value),
2675                            int128_getlo(write_mask));
2676     if (ret == RISCV_EXCP_NONE && ret_value) {
2677         *ret_value = int128_make64(old_value);
2678     }
2679     return ret;
2680 }
2681 
2682 /*
2683  * Debugger support.  If not in user mode, set env->debugger before the
2684  * riscv_csrrw call and clear it after the call.
2685  */
2686 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
2687                                  target_ulong *ret_value,
2688                                  target_ulong new_value,
2689                                  target_ulong write_mask)
2690 {
2691     RISCVException ret;
2692 #if !defined(CONFIG_USER_ONLY)
2693     env->debugger = true;
2694 #endif
2695     ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
2696 #if !defined(CONFIG_USER_ONLY)
2697     env->debugger = false;
2698 #endif
2699     return ret;
2700 }
2701 
2702 /* Control and Status Register function table */
2703 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
2704     /* User Floating-Point CSRs */
2705     [CSR_FFLAGS]   = { "fflags",   fs,     read_fflags,  write_fflags },
2706     [CSR_FRM]      = { "frm",      fs,     read_frm,     write_frm    },
2707     [CSR_FCSR]     = { "fcsr",     fs,     read_fcsr,    write_fcsr   },
2708     /* Vector CSRs */
2709     [CSR_VSTART]   = { "vstart",   vs,     read_vstart,  write_vstart },
2710     [CSR_VXSAT]    = { "vxsat",    vs,     read_vxsat,   write_vxsat  },
2711     [CSR_VXRM]     = { "vxrm",     vs,     read_vxrm,    write_vxrm   },
2712     [CSR_VCSR]     = { "vcsr",     vs,     read_vcsr,    write_vcsr   },
2713     [CSR_VL]       = { "vl",       vs,     read_vl                    },
2714     [CSR_VTYPE]    = { "vtype",    vs,     read_vtype                 },
2715     [CSR_VLENB]    = { "vlenb",    vs,     read_vlenb                 },
2716     /* User Timers and Counters */
2717     [CSR_CYCLE]    = { "cycle",    ctr,    read_instret  },
2718     [CSR_INSTRET]  = { "instret",  ctr,    read_instret  },
2719     [CSR_CYCLEH]   = { "cycleh",   ctr32,  read_instreth },
2720     [CSR_INSTRETH] = { "instreth", ctr32,  read_instreth },
2721 
2722     /*
2723      * In privileged mode, the monitor will have to emulate TIME CSRs only if
2724      * rdtime callback is not provided by machine/platform emulation.
2725      */
2726     [CSR_TIME]  = { "time",  ctr,   read_time  },
2727     [CSR_TIMEH] = { "timeh", ctr32, read_timeh },
2728 
2729 #if !defined(CONFIG_USER_ONLY)
2730     /* Machine Timers and Counters */
2731     [CSR_MCYCLE]    = { "mcycle",    any,   read_instret  },
2732     [CSR_MINSTRET]  = { "minstret",  any,   read_instret  },
2733     [CSR_MCYCLEH]   = { "mcycleh",   any32, read_instreth },
2734     [CSR_MINSTRETH] = { "minstreth", any32, read_instreth },
2735 
2736     /* Machine Information Registers */
2737     [CSR_MVENDORID] = { "mvendorid", any,   read_zero    },
2738     [CSR_MARCHID]   = { "marchid",   any,   read_zero    },
2739     [CSR_MIMPID]    = { "mimpid",    any,   read_zero    },
2740     [CSR_MHARTID]   = { "mhartid",   any,   read_mhartid },
2741 
2742     /* Machine Trap Setup */
2743     [CSR_MSTATUS]     = { "mstatus",    any,   read_mstatus,     write_mstatus, NULL,
2744                                                read_mstatus_i128                   },
2745     [CSR_MISA]        = { "misa",       any,   read_misa,        write_misa, NULL,
2746                                                read_misa_i128                      },
2747     [CSR_MIDELEG]     = { "mideleg",    any,   NULL,    NULL,    rmw_mideleg       },
2748     [CSR_MEDELEG]     = { "medeleg",    any,   read_medeleg,     write_medeleg     },
2749     [CSR_MIE]         = { "mie",        any,   NULL,    NULL,    rmw_mie           },
2750     [CSR_MTVEC]       = { "mtvec",      any,   read_mtvec,       write_mtvec       },
2751     [CSR_MCOUNTEREN]  = { "mcounteren", any,   read_mcounteren,  write_mcounteren  },
2752 
2753     [CSR_MSTATUSH]    = { "mstatush",   any32, read_mstatush,    write_mstatush    },
2754 
2755     /* Machine Trap Handling */
2756     [CSR_MSCRATCH] = { "mscratch", any,  read_mscratch,      write_mscratch, NULL,
2757                                          read_mscratch_i128, write_mscratch_i128   },
2758     [CSR_MEPC]     = { "mepc",     any,  read_mepc,     write_mepc     },
2759     [CSR_MCAUSE]   = { "mcause",   any,  read_mcause,   write_mcause   },
2760     [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval    },
2761     [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip        },
2762 
2763     /* Machine-Level Interrupts (AIA) */
2764     [CSR_MTOPI]    = { "mtopi",    aia_any,   read_mtopi },
2765 
2766     /* Virtual Interrupts for Supervisor Level (AIA) */
2767     [CSR_MVIEN]      = { "mvien", aia_any, read_zero, write_ignore },
2768     [CSR_MVIP]       = { "mvip",  aia_any, read_zero, write_ignore },
2769 
2770     /* Machine-Level High-Half CSRs (AIA) */
2771     [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh },
2772     [CSR_MIEH]     = { "mieh",     aia_any32, NULL, NULL, rmw_mieh     },
2773     [CSR_MVIENH]   = { "mvienh",   aia_any32, read_zero,  write_ignore },
2774     [CSR_MVIPH]    = { "mviph",    aia_any32, read_zero,  write_ignore },
2775     [CSR_MIPH]     = { "miph",     aia_any32, NULL, NULL, rmw_miph     },
2776 
2777     /* Supervisor Trap Setup */
2778     [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,    write_sstatus, NULL,
2779                                               read_sstatus_i128                 },
2780     [CSR_SIE]        = { "sie",        smode, NULL,   NULL,    rmw_sie          },
2781     [CSR_STVEC]      = { "stvec",      smode, read_stvec,      write_stvec      },
2782     [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren, write_scounteren },
2783 
2784     /* Supervisor Trap Handling */
2785     [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch, NULL,
2786                                           read_sscratch_i128, write_sscratch_i128  },
2787     [CSR_SEPC]     = { "sepc",     smode, read_sepc,     write_sepc     },
2788     [CSR_SCAUSE]   = { "scause",   smode, read_scause,   write_scause   },
2789     [CSR_STVAL]    = { "stval",    smode, read_stval,   write_stval   },
2790     [CSR_SIP]      = { "sip",      smode, NULL,    NULL, rmw_sip        },
2791 
2792     /* Supervisor Protection and Translation */
2793     [CSR_SATP]     = { "satp",     smode, read_satp,    write_satp      },
2794 
2795     /* Supervisor-Level Interrupts (AIA) */
2796     [CSR_STOPI]      = { "stopi",      aia_smode, read_stopi },
2797 
2798     /* Supervisor-Level High-Half CSRs (AIA) */
2799     [CSR_SIEH]       = { "sieh",   aia_smode32, NULL, NULL, rmw_sieh },
2800     [CSR_SIPH]       = { "siph",   aia_smode32, NULL, NULL, rmw_siph },
2801 
2802     [CSR_HSTATUS]     = { "hstatus",     hmode,   read_hstatus,     write_hstatus     },
2803     [CSR_HEDELEG]     = { "hedeleg",     hmode,   read_hedeleg,     write_hedeleg     },
2804     [CSR_HIDELEG]     = { "hideleg",     hmode,   NULL,   NULL,     rmw_hideleg       },
2805     [CSR_HVIP]        = { "hvip",        hmode,   NULL,   NULL,     rmw_hvip          },
2806     [CSR_HIP]         = { "hip",         hmode,   NULL,   NULL,     rmw_hip           },
2807     [CSR_HIE]         = { "hie",         hmode,   NULL,   NULL,     rmw_hie           },
2808     [CSR_HCOUNTEREN]  = { "hcounteren",  hmode,   read_hcounteren,  write_hcounteren  },
2809     [CSR_HGEIE]       = { "hgeie",       hmode,   read_hgeie,       write_hgeie       },
2810     [CSR_HTVAL]       = { "htval",       hmode,   read_htval,       write_htval       },
2811     [CSR_HTINST]      = { "htinst",      hmode,   read_htinst,      write_htinst      },
2812     [CSR_HGEIP]       = { "hgeip",       hmode,   read_hgeip,       NULL              },
2813     [CSR_HGATP]       = { "hgatp",       hmode,   read_hgatp,       write_hgatp       },
2814     [CSR_HTIMEDELTA]  = { "htimedelta",  hmode,   read_htimedelta,  write_htimedelta  },
2815     [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah, write_htimedeltah },
2816 
2817     [CSR_VSSTATUS]    = { "vsstatus",    hmode,   read_vsstatus,    write_vsstatus    },
2818     [CSR_VSIP]        = { "vsip",        hmode,   NULL,    NULL,    rmw_vsip          },
2819     [CSR_VSIE]        = { "vsie",        hmode,   NULL,    NULL,    rmw_vsie          },
2820     [CSR_VSTVEC]      = { "vstvec",      hmode,   read_vstvec,      write_vstvec      },
2821     [CSR_VSSCRATCH]   = { "vsscratch",   hmode,   read_vsscratch,   write_vsscratch   },
2822     [CSR_VSEPC]       = { "vsepc",       hmode,   read_vsepc,       write_vsepc       },
2823     [CSR_VSCAUSE]     = { "vscause",     hmode,   read_vscause,     write_vscause     },
2824     [CSR_VSTVAL]      = { "vstval",      hmode,   read_vstval,      write_vstval      },
2825     [CSR_VSATP]       = { "vsatp",       hmode,   read_vsatp,       write_vsatp       },
2826 
2827     [CSR_MTVAL2]      = { "mtval2",      hmode,   read_mtval2,      write_mtval2      },
2828     [CSR_MTINST]      = { "mtinst",      hmode,   read_mtinst,      write_mtinst      },
2829 
2830     /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */
2831     [CSR_HVIEN]       = { "hvien",       aia_hmode, read_zero, write_ignore },
2832     [CSR_HVICTL]      = { "hvictl",      aia_hmode, read_hvictl, write_hvictl },
2833     [CSR_HVIPRIO1]    = { "hviprio1",    aia_hmode, read_hviprio1,   write_hviprio1 },
2834     [CSR_HVIPRIO2]    = { "hviprio2",    aia_hmode, read_hviprio2,   write_hviprio2 },
2835 
2836     /* VS-Level Interrupts (H-extension with AIA) */
2837     [CSR_VSTOPI]      = { "vstopi",      aia_hmode, read_vstopi },
2838 
2839     /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
2840     [CSR_HIDELEGH]    = { "hidelegh",    aia_hmode32, NULL, NULL, rmw_hidelegh },
2841     [CSR_HVIENH]      = { "hvienh",      aia_hmode32, read_zero, write_ignore },
2842     [CSR_HVIPH]       = { "hviph",       aia_hmode32, NULL, NULL, rmw_hviph },
2843     [CSR_HVIPRIO1H]   = { "hviprio1h",   aia_hmode32, read_hviprio1h, write_hviprio1h },
2844     [CSR_HVIPRIO2H]   = { "hviprio2h",   aia_hmode32, read_hviprio2h, write_hviprio2h },
2845     [CSR_VSIEH]       = { "vsieh",       aia_hmode32, NULL, NULL, rmw_vsieh },
2846     [CSR_VSIPH]       = { "vsiph",       aia_hmode32, NULL, NULL, rmw_vsiph },
2847 
2848     /* Physical Memory Protection */
2849     [CSR_MSECCFG]    = { "mseccfg",  epmp, read_mseccfg, write_mseccfg },
2850     [CSR_PMPCFG0]    = { "pmpcfg0",   pmp, read_pmpcfg,  write_pmpcfg  },
2851     [CSR_PMPCFG1]    = { "pmpcfg1",   pmp, read_pmpcfg,  write_pmpcfg  },
2852     [CSR_PMPCFG2]    = { "pmpcfg2",   pmp, read_pmpcfg,  write_pmpcfg  },
2853     [CSR_PMPCFG3]    = { "pmpcfg3",   pmp, read_pmpcfg,  write_pmpcfg  },
2854     [CSR_PMPADDR0]   = { "pmpaddr0",  pmp, read_pmpaddr, write_pmpaddr },
2855     [CSR_PMPADDR1]   = { "pmpaddr1",  pmp, read_pmpaddr, write_pmpaddr },
2856     [CSR_PMPADDR2]   = { "pmpaddr2",  pmp, read_pmpaddr, write_pmpaddr },
2857     [CSR_PMPADDR3]   = { "pmpaddr3",  pmp, read_pmpaddr, write_pmpaddr },
2858     [CSR_PMPADDR4]   = { "pmpaddr4",  pmp, read_pmpaddr, write_pmpaddr },
2859     [CSR_PMPADDR5]   = { "pmpaddr5",  pmp, read_pmpaddr, write_pmpaddr },
2860     [CSR_PMPADDR6]   = { "pmpaddr6",  pmp, read_pmpaddr, write_pmpaddr },
2861     [CSR_PMPADDR7]   = { "pmpaddr7",  pmp, read_pmpaddr, write_pmpaddr },
2862     [CSR_PMPADDR8]   = { "pmpaddr8",  pmp, read_pmpaddr, write_pmpaddr },
2863     [CSR_PMPADDR9]   = { "pmpaddr9",  pmp, read_pmpaddr, write_pmpaddr },
2864     [CSR_PMPADDR10]  = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr },
2865     [CSR_PMPADDR11]  = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr },
2866     [CSR_PMPADDR12]  = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr },
2867     [CSR_PMPADDR13]  = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr },
2868     [CSR_PMPADDR14] =  { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr },
2869     [CSR_PMPADDR15] =  { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr },
2870 
2871     /* User Pointer Masking */
2872     [CSR_UMTE]    =    { "umte",    pointer_masking, read_umte,    write_umte    },
2873     [CSR_UPMMASK] =    { "upmmask", pointer_masking, read_upmmask, write_upmmask },
2874     [CSR_UPMBASE] =    { "upmbase", pointer_masking, read_upmbase, write_upmbase },
2875     /* Machine Pointer Masking */
2876     [CSR_MMTE]    =    { "mmte",    pointer_masking, read_mmte,    write_mmte    },
2877     [CSR_MPMMASK] =    { "mpmmask", pointer_masking, read_mpmmask, write_mpmmask },
2878     [CSR_MPMBASE] =    { "mpmbase", pointer_masking, read_mpmbase, write_mpmbase },
2879     /* Supervisor Pointer Masking */
2880     [CSR_SMTE]    =    { "smte",    pointer_masking, read_smte,    write_smte    },
2881     [CSR_SPMMASK] =    { "spmmask", pointer_masking, read_spmmask, write_spmmask },
2882     [CSR_SPMBASE] =    { "spmbase", pointer_masking, read_spmbase, write_spmbase },
2883 
2884     /* Performance Counters */
2885     [CSR_HPMCOUNTER3]    = { "hpmcounter3",    ctr,    read_zero },
2886     [CSR_HPMCOUNTER4]    = { "hpmcounter4",    ctr,    read_zero },
2887     [CSR_HPMCOUNTER5]    = { "hpmcounter5",    ctr,    read_zero },
2888     [CSR_HPMCOUNTER6]    = { "hpmcounter6",    ctr,    read_zero },
2889     [CSR_HPMCOUNTER7]    = { "hpmcounter7",    ctr,    read_zero },
2890     [CSR_HPMCOUNTER8]    = { "hpmcounter8",    ctr,    read_zero },
2891     [CSR_HPMCOUNTER9]    = { "hpmcounter9",    ctr,    read_zero },
2892     [CSR_HPMCOUNTER10]   = { "hpmcounter10",   ctr,    read_zero },
2893     [CSR_HPMCOUNTER11]   = { "hpmcounter11",   ctr,    read_zero },
2894     [CSR_HPMCOUNTER12]   = { "hpmcounter12",   ctr,    read_zero },
2895     [CSR_HPMCOUNTER13]   = { "hpmcounter13",   ctr,    read_zero },
2896     [CSR_HPMCOUNTER14]   = { "hpmcounter14",   ctr,    read_zero },
2897     [CSR_HPMCOUNTER15]   = { "hpmcounter15",   ctr,    read_zero },
2898     [CSR_HPMCOUNTER16]   = { "hpmcounter16",   ctr,    read_zero },
2899     [CSR_HPMCOUNTER17]   = { "hpmcounter17",   ctr,    read_zero },
2900     [CSR_HPMCOUNTER18]   = { "hpmcounter18",   ctr,    read_zero },
2901     [CSR_HPMCOUNTER19]   = { "hpmcounter19",   ctr,    read_zero },
2902     [CSR_HPMCOUNTER20]   = { "hpmcounter20",   ctr,    read_zero },
2903     [CSR_HPMCOUNTER21]   = { "hpmcounter21",   ctr,    read_zero },
2904     [CSR_HPMCOUNTER22]   = { "hpmcounter22",   ctr,    read_zero },
2905     [CSR_HPMCOUNTER23]   = { "hpmcounter23",   ctr,    read_zero },
2906     [CSR_HPMCOUNTER24]   = { "hpmcounter24",   ctr,    read_zero },
2907     [CSR_HPMCOUNTER25]   = { "hpmcounter25",   ctr,    read_zero },
2908     [CSR_HPMCOUNTER26]   = { "hpmcounter26",   ctr,    read_zero },
2909     [CSR_HPMCOUNTER27]   = { "hpmcounter27",   ctr,    read_zero },
2910     [CSR_HPMCOUNTER28]   = { "hpmcounter28",   ctr,    read_zero },
2911     [CSR_HPMCOUNTER29]   = { "hpmcounter29",   ctr,    read_zero },
2912     [CSR_HPMCOUNTER30]   = { "hpmcounter30",   ctr,    read_zero },
2913     [CSR_HPMCOUNTER31]   = { "hpmcounter31",   ctr,    read_zero },
2914 
2915     [CSR_MHPMCOUNTER3]   = { "mhpmcounter3",   any,    read_zero },
2916     [CSR_MHPMCOUNTER4]   = { "mhpmcounter4",   any,    read_zero },
2917     [CSR_MHPMCOUNTER5]   = { "mhpmcounter5",   any,    read_zero },
2918     [CSR_MHPMCOUNTER6]   = { "mhpmcounter6",   any,    read_zero },
2919     [CSR_MHPMCOUNTER7]   = { "mhpmcounter7",   any,    read_zero },
2920     [CSR_MHPMCOUNTER8]   = { "mhpmcounter8",   any,    read_zero },
2921     [CSR_MHPMCOUNTER9]   = { "mhpmcounter9",   any,    read_zero },
2922     [CSR_MHPMCOUNTER10]  = { "mhpmcounter10",  any,    read_zero },
2923     [CSR_MHPMCOUNTER11]  = { "mhpmcounter11",  any,    read_zero },
2924     [CSR_MHPMCOUNTER12]  = { "mhpmcounter12",  any,    read_zero },
2925     [CSR_MHPMCOUNTER13]  = { "mhpmcounter13",  any,    read_zero },
2926     [CSR_MHPMCOUNTER14]  = { "mhpmcounter14",  any,    read_zero },
2927     [CSR_MHPMCOUNTER15]  = { "mhpmcounter15",  any,    read_zero },
2928     [CSR_MHPMCOUNTER16]  = { "mhpmcounter16",  any,    read_zero },
2929     [CSR_MHPMCOUNTER17]  = { "mhpmcounter17",  any,    read_zero },
2930     [CSR_MHPMCOUNTER18]  = { "mhpmcounter18",  any,    read_zero },
2931     [CSR_MHPMCOUNTER19]  = { "mhpmcounter19",  any,    read_zero },
2932     [CSR_MHPMCOUNTER20]  = { "mhpmcounter20",  any,    read_zero },
2933     [CSR_MHPMCOUNTER21]  = { "mhpmcounter21",  any,    read_zero },
2934     [CSR_MHPMCOUNTER22]  = { "mhpmcounter22",  any,    read_zero },
2935     [CSR_MHPMCOUNTER23]  = { "mhpmcounter23",  any,    read_zero },
2936     [CSR_MHPMCOUNTER24]  = { "mhpmcounter24",  any,    read_zero },
2937     [CSR_MHPMCOUNTER25]  = { "mhpmcounter25",  any,    read_zero },
2938     [CSR_MHPMCOUNTER26]  = { "mhpmcounter26",  any,    read_zero },
2939     [CSR_MHPMCOUNTER27]  = { "mhpmcounter27",  any,    read_zero },
2940     [CSR_MHPMCOUNTER28]  = { "mhpmcounter28",  any,    read_zero },
2941     [CSR_MHPMCOUNTER29]  = { "mhpmcounter29",  any,    read_zero },
2942     [CSR_MHPMCOUNTER30]  = { "mhpmcounter30",  any,    read_zero },
2943     [CSR_MHPMCOUNTER31]  = { "mhpmcounter31",  any,    read_zero },
2944 
2945     [CSR_MHPMEVENT3]     = { "mhpmevent3",     any,    read_zero },
2946     [CSR_MHPMEVENT4]     = { "mhpmevent4",     any,    read_zero },
2947     [CSR_MHPMEVENT5]     = { "mhpmevent5",     any,    read_zero },
2948     [CSR_MHPMEVENT6]     = { "mhpmevent6",     any,    read_zero },
2949     [CSR_MHPMEVENT7]     = { "mhpmevent7",     any,    read_zero },
2950     [CSR_MHPMEVENT8]     = { "mhpmevent8",     any,    read_zero },
2951     [CSR_MHPMEVENT9]     = { "mhpmevent9",     any,    read_zero },
2952     [CSR_MHPMEVENT10]    = { "mhpmevent10",    any,    read_zero },
2953     [CSR_MHPMEVENT11]    = { "mhpmevent11",    any,    read_zero },
2954     [CSR_MHPMEVENT12]    = { "mhpmevent12",    any,    read_zero },
2955     [CSR_MHPMEVENT13]    = { "mhpmevent13",    any,    read_zero },
2956     [CSR_MHPMEVENT14]    = { "mhpmevent14",    any,    read_zero },
2957     [CSR_MHPMEVENT15]    = { "mhpmevent15",    any,    read_zero },
2958     [CSR_MHPMEVENT16]    = { "mhpmevent16",    any,    read_zero },
2959     [CSR_MHPMEVENT17]    = { "mhpmevent17",    any,    read_zero },
2960     [CSR_MHPMEVENT18]    = { "mhpmevent18",    any,    read_zero },
2961     [CSR_MHPMEVENT19]    = { "mhpmevent19",    any,    read_zero },
2962     [CSR_MHPMEVENT20]    = { "mhpmevent20",    any,    read_zero },
2963     [CSR_MHPMEVENT21]    = { "mhpmevent21",    any,    read_zero },
2964     [CSR_MHPMEVENT22]    = { "mhpmevent22",    any,    read_zero },
2965     [CSR_MHPMEVENT23]    = { "mhpmevent23",    any,    read_zero },
2966     [CSR_MHPMEVENT24]    = { "mhpmevent24",    any,    read_zero },
2967     [CSR_MHPMEVENT25]    = { "mhpmevent25",    any,    read_zero },
2968     [CSR_MHPMEVENT26]    = { "mhpmevent26",    any,    read_zero },
2969     [CSR_MHPMEVENT27]    = { "mhpmevent27",    any,    read_zero },
2970     [CSR_MHPMEVENT28]    = { "mhpmevent28",    any,    read_zero },
2971     [CSR_MHPMEVENT29]    = { "mhpmevent29",    any,    read_zero },
2972     [CSR_MHPMEVENT30]    = { "mhpmevent30",    any,    read_zero },
2973     [CSR_MHPMEVENT31]    = { "mhpmevent31",    any,    read_zero },
2974 
2975     [CSR_HPMCOUNTER3H]   = { "hpmcounter3h",   ctr32,  read_zero },
2976     [CSR_HPMCOUNTER4H]   = { "hpmcounter4h",   ctr32,  read_zero },
2977     [CSR_HPMCOUNTER5H]   = { "hpmcounter5h",   ctr32,  read_zero },
2978     [CSR_HPMCOUNTER6H]   = { "hpmcounter6h",   ctr32,  read_zero },
2979     [CSR_HPMCOUNTER7H]   = { "hpmcounter7h",   ctr32,  read_zero },
2980     [CSR_HPMCOUNTER8H]   = { "hpmcounter8h",   ctr32,  read_zero },
2981     [CSR_HPMCOUNTER9H]   = { "hpmcounter9h",   ctr32,  read_zero },
2982     [CSR_HPMCOUNTER10H]  = { "hpmcounter10h",  ctr32,  read_zero },
2983     [CSR_HPMCOUNTER11H]  = { "hpmcounter11h",  ctr32,  read_zero },
2984     [CSR_HPMCOUNTER12H]  = { "hpmcounter12h",  ctr32,  read_zero },
2985     [CSR_HPMCOUNTER13H]  = { "hpmcounter13h",  ctr32,  read_zero },
2986     [CSR_HPMCOUNTER14H]  = { "hpmcounter14h",  ctr32,  read_zero },
2987     [CSR_HPMCOUNTER15H]  = { "hpmcounter15h",  ctr32,  read_zero },
2988     [CSR_HPMCOUNTER16H]  = { "hpmcounter16h",  ctr32,  read_zero },
2989     [CSR_HPMCOUNTER17H]  = { "hpmcounter17h",  ctr32,  read_zero },
2990     [CSR_HPMCOUNTER18H]  = { "hpmcounter18h",  ctr32,  read_zero },
2991     [CSR_HPMCOUNTER19H]  = { "hpmcounter19h",  ctr32,  read_zero },
2992     [CSR_HPMCOUNTER20H]  = { "hpmcounter20h",  ctr32,  read_zero },
2993     [CSR_HPMCOUNTER21H]  = { "hpmcounter21h",  ctr32,  read_zero },
2994     [CSR_HPMCOUNTER22H]  = { "hpmcounter22h",  ctr32,  read_zero },
2995     [CSR_HPMCOUNTER23H]  = { "hpmcounter23h",  ctr32,  read_zero },
2996     [CSR_HPMCOUNTER24H]  = { "hpmcounter24h",  ctr32,  read_zero },
2997     [CSR_HPMCOUNTER25H]  = { "hpmcounter25h",  ctr32,  read_zero },
2998     [CSR_HPMCOUNTER26H]  = { "hpmcounter26h",  ctr32,  read_zero },
2999     [CSR_HPMCOUNTER27H]  = { "hpmcounter27h",  ctr32,  read_zero },
3000     [CSR_HPMCOUNTER28H]  = { "hpmcounter28h",  ctr32,  read_zero },
3001     [CSR_HPMCOUNTER29H]  = { "hpmcounter29h",  ctr32,  read_zero },
3002     [CSR_HPMCOUNTER30H]  = { "hpmcounter30h",  ctr32,  read_zero },
3003     [CSR_HPMCOUNTER31H]  = { "hpmcounter31h",  ctr32,  read_zero },
3004 
3005     [CSR_MHPMCOUNTER3H]  = { "mhpmcounter3h",  any32,  read_zero },
3006     [CSR_MHPMCOUNTER4H]  = { "mhpmcounter4h",  any32,  read_zero },
3007     [CSR_MHPMCOUNTER5H]  = { "mhpmcounter5h",  any32,  read_zero },
3008     [CSR_MHPMCOUNTER6H]  = { "mhpmcounter6h",  any32,  read_zero },
3009     [CSR_MHPMCOUNTER7H]  = { "mhpmcounter7h",  any32,  read_zero },
3010     [CSR_MHPMCOUNTER8H]  = { "mhpmcounter8h",  any32,  read_zero },
3011     [CSR_MHPMCOUNTER9H]  = { "mhpmcounter9h",  any32,  read_zero },
3012     [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", any32,  read_zero },
3013     [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", any32,  read_zero },
3014     [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", any32,  read_zero },
3015     [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", any32,  read_zero },
3016     [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", any32,  read_zero },
3017     [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", any32,  read_zero },
3018     [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", any32,  read_zero },
3019     [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", any32,  read_zero },
3020     [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", any32,  read_zero },
3021     [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", any32,  read_zero },
3022     [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", any32,  read_zero },
3023     [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", any32,  read_zero },
3024     [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", any32,  read_zero },
3025     [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", any32,  read_zero },
3026     [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", any32,  read_zero },
3027     [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", any32,  read_zero },
3028     [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", any32,  read_zero },
3029     [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", any32,  read_zero },
3030     [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", any32,  read_zero },
3031     [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", any32,  read_zero },
3032     [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", any32,  read_zero },
3033     [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", any32,  read_zero },
3034 #endif /* !CONFIG_USER_ONLY */
3035 };
3036