xref: /openbmc/qemu/target/riscv/csr.c (revision 92371bd9)
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     /* loose check condition for fcsr in vector extension */
42     if ((csrno == CSR_FCSR) && (env->misa_ext & RVV)) {
43         return RISCV_EXCP_NONE;
44     }
45     if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
46         return RISCV_EXCP_ILLEGAL_INST;
47     }
48 #endif
49     return RISCV_EXCP_NONE;
50 }
51 
52 static RISCVException vs(CPURISCVState *env, int csrno)
53 {
54     if (env->misa_ext & RVV) {
55         return RISCV_EXCP_NONE;
56     }
57     return RISCV_EXCP_ILLEGAL_INST;
58 }
59 
60 static RISCVException ctr(CPURISCVState *env, int csrno)
61 {
62 #if !defined(CONFIG_USER_ONLY)
63     CPUState *cs = env_cpu(env);
64     RISCVCPU *cpu = RISCV_CPU(cs);
65 
66     if (!cpu->cfg.ext_counters) {
67         /* The Counters extensions is not enabled */
68         return RISCV_EXCP_ILLEGAL_INST;
69     }
70 
71     if (riscv_cpu_virt_enabled(env)) {
72         switch (csrno) {
73         case CSR_CYCLE:
74             if (!get_field(env->hcounteren, COUNTEREN_CY) &&
75                 get_field(env->mcounteren, COUNTEREN_CY)) {
76                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
77             }
78             break;
79         case CSR_TIME:
80             if (!get_field(env->hcounteren, COUNTEREN_TM) &&
81                 get_field(env->mcounteren, COUNTEREN_TM)) {
82                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
83             }
84             break;
85         case CSR_INSTRET:
86             if (!get_field(env->hcounteren, COUNTEREN_IR) &&
87                 get_field(env->mcounteren, COUNTEREN_IR)) {
88                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
89             }
90             break;
91         case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31:
92             if (!get_field(env->hcounteren, 1 << (csrno - CSR_HPMCOUNTER3)) &&
93                 get_field(env->mcounteren, 1 << (csrno - CSR_HPMCOUNTER3))) {
94                 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
95             }
96             break;
97         }
98         if (riscv_cpu_mxl(env) == MXL_RV32) {
99             switch (csrno) {
100             case CSR_CYCLEH:
101                 if (!get_field(env->hcounteren, COUNTEREN_CY) &&
102                     get_field(env->mcounteren, COUNTEREN_CY)) {
103                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
104                 }
105                 break;
106             case CSR_TIMEH:
107                 if (!get_field(env->hcounteren, COUNTEREN_TM) &&
108                     get_field(env->mcounteren, COUNTEREN_TM)) {
109                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
110                 }
111                 break;
112             case CSR_INSTRETH:
113                 if (!get_field(env->hcounteren, COUNTEREN_IR) &&
114                     get_field(env->mcounteren, COUNTEREN_IR)) {
115                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
116                 }
117                 break;
118             case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
119                 if (!get_field(env->hcounteren, 1 << (csrno - CSR_HPMCOUNTER3H)) &&
120                     get_field(env->mcounteren, 1 << (csrno - CSR_HPMCOUNTER3H))) {
121                     return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
122                 }
123                 break;
124             }
125         }
126     }
127 #endif
128     return RISCV_EXCP_NONE;
129 }
130 
131 static RISCVException ctr32(CPURISCVState *env, int csrno)
132 {
133     if (riscv_cpu_mxl(env) != MXL_RV32) {
134         return RISCV_EXCP_ILLEGAL_INST;
135     }
136 
137     return ctr(env, csrno);
138 }
139 
140 #if !defined(CONFIG_USER_ONLY)
141 static RISCVException any(CPURISCVState *env, int csrno)
142 {
143     return RISCV_EXCP_NONE;
144 }
145 
146 static RISCVException any32(CPURISCVState *env, int csrno)
147 {
148     if (riscv_cpu_mxl(env) != MXL_RV32) {
149         return RISCV_EXCP_ILLEGAL_INST;
150     }
151 
152     return any(env, csrno);
153 
154 }
155 
156 static RISCVException smode(CPURISCVState *env, int csrno)
157 {
158     if (riscv_has_ext(env, RVS)) {
159         return RISCV_EXCP_NONE;
160     }
161 
162     return RISCV_EXCP_ILLEGAL_INST;
163 }
164 
165 static RISCVException hmode(CPURISCVState *env, int csrno)
166 {
167     if (riscv_has_ext(env, RVS) &&
168         riscv_has_ext(env, RVH)) {
169         /* Hypervisor extension is supported */
170         if ((env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) ||
171             env->priv == PRV_M) {
172             return RISCV_EXCP_NONE;
173         } else {
174             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
175         }
176     }
177 
178     return RISCV_EXCP_ILLEGAL_INST;
179 }
180 
181 static RISCVException hmode32(CPURISCVState *env, int csrno)
182 {
183     if (riscv_cpu_mxl(env) != MXL_RV32) {
184         if (riscv_cpu_virt_enabled(env)) {
185             return RISCV_EXCP_ILLEGAL_INST;
186         } else {
187             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
188         }
189     }
190 
191     return hmode(env, csrno);
192 
193 }
194 
195 static RISCVException pmp(CPURISCVState *env, int csrno)
196 {
197     if (riscv_feature(env, RISCV_FEATURE_PMP)) {
198         return RISCV_EXCP_NONE;
199     }
200 
201     return RISCV_EXCP_ILLEGAL_INST;
202 }
203 
204 static RISCVException epmp(CPURISCVState *env, int csrno)
205 {
206     if (env->priv == PRV_M && riscv_feature(env, RISCV_FEATURE_EPMP)) {
207         return RISCV_EXCP_NONE;
208     }
209 
210     return RISCV_EXCP_ILLEGAL_INST;
211 }
212 #endif
213 
214 /* User Floating-Point CSRs */
215 static RISCVException read_fflags(CPURISCVState *env, int csrno,
216                                   target_ulong *val)
217 {
218     *val = riscv_cpu_get_fflags(env);
219     return RISCV_EXCP_NONE;
220 }
221 
222 static RISCVException write_fflags(CPURISCVState *env, int csrno,
223                                    target_ulong val)
224 {
225 #if !defined(CONFIG_USER_ONLY)
226     env->mstatus |= MSTATUS_FS;
227 #endif
228     riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
229     return RISCV_EXCP_NONE;
230 }
231 
232 static RISCVException read_frm(CPURISCVState *env, int csrno,
233                                target_ulong *val)
234 {
235     *val = env->frm;
236     return RISCV_EXCP_NONE;
237 }
238 
239 static RISCVException write_frm(CPURISCVState *env, int csrno,
240                                 target_ulong val)
241 {
242 #if !defined(CONFIG_USER_ONLY)
243     env->mstatus |= MSTATUS_FS;
244 #endif
245     env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
246     return RISCV_EXCP_NONE;
247 }
248 
249 static RISCVException read_fcsr(CPURISCVState *env, int csrno,
250                                 target_ulong *val)
251 {
252     *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
253         | (env->frm << FSR_RD_SHIFT);
254     if (vs(env, csrno) >= 0) {
255         *val |= (env->vxrm << FSR_VXRM_SHIFT)
256                 | (env->vxsat << FSR_VXSAT_SHIFT);
257     }
258     return RISCV_EXCP_NONE;
259 }
260 
261 static RISCVException write_fcsr(CPURISCVState *env, int csrno,
262                                  target_ulong val)
263 {
264 #if !defined(CONFIG_USER_ONLY)
265     env->mstatus |= MSTATUS_FS;
266 #endif
267     env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
268     if (vs(env, csrno) >= 0) {
269         env->vxrm = (val & FSR_VXRM) >> FSR_VXRM_SHIFT;
270         env->vxsat = (val & FSR_VXSAT) >> FSR_VXSAT_SHIFT;
271     }
272     riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
273     return RISCV_EXCP_NONE;
274 }
275 
276 static RISCVException read_vtype(CPURISCVState *env, int csrno,
277                                  target_ulong *val)
278 {
279     *val = env->vtype;
280     return RISCV_EXCP_NONE;
281 }
282 
283 static RISCVException read_vl(CPURISCVState *env, int csrno,
284                               target_ulong *val)
285 {
286     *val = env->vl;
287     return RISCV_EXCP_NONE;
288 }
289 
290 static RISCVException read_vxrm(CPURISCVState *env, int csrno,
291                                 target_ulong *val)
292 {
293     *val = env->vxrm;
294     return RISCV_EXCP_NONE;
295 }
296 
297 static RISCVException write_vxrm(CPURISCVState *env, int csrno,
298                                  target_ulong val)
299 {
300     env->vxrm = val;
301     return RISCV_EXCP_NONE;
302 }
303 
304 static RISCVException read_vxsat(CPURISCVState *env, int csrno,
305                                  target_ulong *val)
306 {
307     *val = env->vxsat;
308     return RISCV_EXCP_NONE;
309 }
310 
311 static RISCVException write_vxsat(CPURISCVState *env, int csrno,
312                                   target_ulong val)
313 {
314     env->vxsat = val;
315     return RISCV_EXCP_NONE;
316 }
317 
318 static RISCVException read_vstart(CPURISCVState *env, int csrno,
319                                   target_ulong *val)
320 {
321     *val = env->vstart;
322     return RISCV_EXCP_NONE;
323 }
324 
325 static RISCVException write_vstart(CPURISCVState *env, int csrno,
326                                    target_ulong val)
327 {
328     env->vstart = val;
329     return RISCV_EXCP_NONE;
330 }
331 
332 /* User Timers and Counters */
333 static RISCVException read_instret(CPURISCVState *env, int csrno,
334                                    target_ulong *val)
335 {
336 #if !defined(CONFIG_USER_ONLY)
337     if (icount_enabled()) {
338         *val = icount_get();
339     } else {
340         *val = cpu_get_host_ticks();
341     }
342 #else
343     *val = cpu_get_host_ticks();
344 #endif
345     return RISCV_EXCP_NONE;
346 }
347 
348 static RISCVException read_instreth(CPURISCVState *env, int csrno,
349                                     target_ulong *val)
350 {
351 #if !defined(CONFIG_USER_ONLY)
352     if (icount_enabled()) {
353         *val = icount_get() >> 32;
354     } else {
355         *val = cpu_get_host_ticks() >> 32;
356     }
357 #else
358     *val = cpu_get_host_ticks() >> 32;
359 #endif
360     return RISCV_EXCP_NONE;
361 }
362 
363 #if defined(CONFIG_USER_ONLY)
364 static RISCVException read_time(CPURISCVState *env, int csrno,
365                                 target_ulong *val)
366 {
367     *val = cpu_get_host_ticks();
368     return RISCV_EXCP_NONE;
369 }
370 
371 static RISCVException read_timeh(CPURISCVState *env, int csrno,
372                                  target_ulong *val)
373 {
374     *val = cpu_get_host_ticks() >> 32;
375     return RISCV_EXCP_NONE;
376 }
377 
378 #else /* CONFIG_USER_ONLY */
379 
380 static RISCVException read_time(CPURISCVState *env, int csrno,
381                                 target_ulong *val)
382 {
383     uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
384 
385     if (!env->rdtime_fn) {
386         return RISCV_EXCP_ILLEGAL_INST;
387     }
388 
389     *val = env->rdtime_fn(env->rdtime_fn_arg) + delta;
390     return RISCV_EXCP_NONE;
391 }
392 
393 static RISCVException read_timeh(CPURISCVState *env, int csrno,
394                                  target_ulong *val)
395 {
396     uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
397 
398     if (!env->rdtime_fn) {
399         return RISCV_EXCP_ILLEGAL_INST;
400     }
401 
402     *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32;
403     return RISCV_EXCP_NONE;
404 }
405 
406 /* Machine constants */
407 
408 #define M_MODE_INTERRUPTS  (MIP_MSIP | MIP_MTIP | MIP_MEIP)
409 #define S_MODE_INTERRUPTS  (MIP_SSIP | MIP_STIP | MIP_SEIP)
410 #define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)
411 
412 static const target_ulong delegable_ints = S_MODE_INTERRUPTS |
413                                            VS_MODE_INTERRUPTS;
414 static const target_ulong vs_delegable_ints = VS_MODE_INTERRUPTS;
415 static const target_ulong all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
416                                      VS_MODE_INTERRUPTS;
417 #define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \
418                          (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \
419                          (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \
420                          (1ULL << (RISCV_EXCP_BREAKPOINT)) | \
421                          (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | \
422                          (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | \
423                          (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | \
424                          (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | \
425                          (1ULL << (RISCV_EXCP_U_ECALL)) | \
426                          (1ULL << (RISCV_EXCP_S_ECALL)) | \
427                          (1ULL << (RISCV_EXCP_VS_ECALL)) | \
428                          (1ULL << (RISCV_EXCP_M_ECALL)) | \
429                          (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | \
430                          (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \
431                          (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \
432                          (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \
433                          (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \
434                          (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
435                          (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)))
436 static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &
437     ~((1ULL << (RISCV_EXCP_S_ECALL)) |
438       (1ULL << (RISCV_EXCP_VS_ECALL)) |
439       (1ULL << (RISCV_EXCP_M_ECALL)) |
440       (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) |
441       (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) |
442       (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) |
443       (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)));
444 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
445     SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
446     SSTATUS_SUM | SSTATUS_MXR;
447 static const target_ulong sip_writable_mask = SIP_SSIP | MIP_USIP | MIP_UEIP;
448 static const target_ulong hip_writable_mask = MIP_VSSIP;
449 static const target_ulong hvip_writable_mask = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP;
450 static const target_ulong vsip_writable_mask = MIP_VSSIP;
451 
452 static const char valid_vm_1_10_32[16] = {
453     [VM_1_10_MBARE] = 1,
454     [VM_1_10_SV32] = 1
455 };
456 
457 static const char valid_vm_1_10_64[16] = {
458     [VM_1_10_MBARE] = 1,
459     [VM_1_10_SV39] = 1,
460     [VM_1_10_SV48] = 1,
461     [VM_1_10_SV57] = 1
462 };
463 
464 /* Machine Information Registers */
465 static RISCVException read_zero(CPURISCVState *env, int csrno,
466                                 target_ulong *val)
467 {
468     *val = 0;
469     return RISCV_EXCP_NONE;
470 }
471 
472 static RISCVException read_mhartid(CPURISCVState *env, int csrno,
473                                    target_ulong *val)
474 {
475     *val = env->mhartid;
476     return RISCV_EXCP_NONE;
477 }
478 
479 /* Machine Trap Setup */
480 static RISCVException read_mstatus(CPURISCVState *env, int csrno,
481                                    target_ulong *val)
482 {
483     *val = env->mstatus;
484     return RISCV_EXCP_NONE;
485 }
486 
487 static int validate_vm(CPURISCVState *env, target_ulong vm)
488 {
489     if (riscv_cpu_mxl(env) == MXL_RV32) {
490         return valid_vm_1_10_32[vm & 0xf];
491     } else {
492         return valid_vm_1_10_64[vm & 0xf];
493     }
494 }
495 
496 static RISCVException write_mstatus(CPURISCVState *env, int csrno,
497                                     target_ulong val)
498 {
499     uint64_t mstatus = env->mstatus;
500     uint64_t mask = 0;
501     int dirty;
502 
503     /* flush tlb on mstatus fields that affect VM */
504     if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
505             MSTATUS_MPRV | MSTATUS_SUM)) {
506         tlb_flush(env_cpu(env));
507     }
508     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
509         MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM |
510         MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
511         MSTATUS_TW;
512 
513     if (riscv_cpu_mxl(env) != MXL_RV32) {
514         /*
515          * RV32: MPV and GVA are not in mstatus. The current plan is to
516          * add them to mstatush. For now, we just don't support it.
517          */
518         mask |= MSTATUS_MPV | MSTATUS_GVA;
519     }
520 
521     mstatus = (mstatus & ~mask) | (val & mask);
522 
523     dirty = ((mstatus & MSTATUS_FS) == MSTATUS_FS) |
524             ((mstatus & MSTATUS_XS) == MSTATUS_XS);
525     if (riscv_cpu_mxl(env) == MXL_RV32) {
526         mstatus = set_field(mstatus, MSTATUS32_SD, dirty);
527     } else {
528         mstatus = set_field(mstatus, MSTATUS64_SD, dirty);
529         /* SXL and UXL fields are for now read only */
530         mstatus = set_field(mstatus, MSTATUS64_SXL, MXL_RV64);
531         mstatus = set_field(mstatus, MSTATUS64_UXL, MXL_RV64);
532     }
533     env->mstatus = mstatus;
534 
535     return RISCV_EXCP_NONE;
536 }
537 
538 static RISCVException read_mstatush(CPURISCVState *env, int csrno,
539                                     target_ulong *val)
540 {
541     *val = env->mstatus >> 32;
542     return RISCV_EXCP_NONE;
543 }
544 
545 static RISCVException write_mstatush(CPURISCVState *env, int csrno,
546                                      target_ulong val)
547 {
548     uint64_t valh = (uint64_t)val << 32;
549     uint64_t mask = MSTATUS_MPV | MSTATUS_GVA;
550 
551     if ((valh ^ env->mstatus) & (MSTATUS_MPV)) {
552         tlb_flush(env_cpu(env));
553     }
554 
555     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
556 
557     return RISCV_EXCP_NONE;
558 }
559 
560 static RISCVException read_misa(CPURISCVState *env, int csrno,
561                                 target_ulong *val)
562 {
563     target_ulong misa;
564 
565     switch (env->misa_mxl) {
566     case MXL_RV32:
567         misa = (target_ulong)MXL_RV32 << 30;
568         break;
569 #ifdef TARGET_RISCV64
570     case MXL_RV64:
571         misa = (target_ulong)MXL_RV64 << 62;
572         break;
573 #endif
574     default:
575         g_assert_not_reached();
576     }
577 
578     *val = misa | env->misa_ext;
579     return RISCV_EXCP_NONE;
580 }
581 
582 static RISCVException write_misa(CPURISCVState *env, int csrno,
583                                  target_ulong val)
584 {
585     if (!riscv_feature(env, RISCV_FEATURE_MISA)) {
586         /* drop write to misa */
587         return RISCV_EXCP_NONE;
588     }
589 
590     /* 'I' or 'E' must be present */
591     if (!(val & (RVI | RVE))) {
592         /* It is not, drop write to misa */
593         return RISCV_EXCP_NONE;
594     }
595 
596     /* 'E' excludes all other extensions */
597     if (val & RVE) {
598         /* when we support 'E' we can do "val = RVE;" however
599          * for now we just drop writes if 'E' is present.
600          */
601         return RISCV_EXCP_NONE;
602     }
603 
604     /*
605      * misa.MXL writes are not supported by QEMU.
606      * Drop writes to those bits.
607      */
608 
609     /* Mask extensions that are not supported by this hart */
610     val &= env->misa_ext_mask;
611 
612     /* Mask extensions that are not supported by QEMU */
613     val &= (RVI | RVE | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
614 
615     /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
616     if ((val & RVD) && !(val & RVF)) {
617         val &= ~RVD;
618     }
619 
620     /* Suppress 'C' if next instruction is not aligned
621      * TODO: this should check next_pc
622      */
623     if ((val & RVC) && (GETPC() & ~3) != 0) {
624         val &= ~RVC;
625     }
626 
627     /* If nothing changed, do nothing. */
628     if (val == env->misa_ext) {
629         return RISCV_EXCP_NONE;
630     }
631 
632     /* flush translation cache */
633     tb_flush(env_cpu(env));
634     env->misa_ext = val;
635     return RISCV_EXCP_NONE;
636 }
637 
638 static RISCVException read_medeleg(CPURISCVState *env, int csrno,
639                                    target_ulong *val)
640 {
641     *val = env->medeleg;
642     return RISCV_EXCP_NONE;
643 }
644 
645 static RISCVException write_medeleg(CPURISCVState *env, int csrno,
646                                     target_ulong val)
647 {
648     env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS);
649     return RISCV_EXCP_NONE;
650 }
651 
652 static RISCVException read_mideleg(CPURISCVState *env, int csrno,
653                                    target_ulong *val)
654 {
655     *val = env->mideleg;
656     return RISCV_EXCP_NONE;
657 }
658 
659 static RISCVException write_mideleg(CPURISCVState *env, int csrno,
660                                     target_ulong val)
661 {
662     env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints);
663     if (riscv_has_ext(env, RVH)) {
664         env->mideleg |= VS_MODE_INTERRUPTS;
665     }
666     return RISCV_EXCP_NONE;
667 }
668 
669 static RISCVException read_mie(CPURISCVState *env, int csrno,
670                                target_ulong *val)
671 {
672     *val = env->mie;
673     return RISCV_EXCP_NONE;
674 }
675 
676 static RISCVException write_mie(CPURISCVState *env, int csrno,
677                                 target_ulong val)
678 {
679     env->mie = (env->mie & ~all_ints) | (val & all_ints);
680     return RISCV_EXCP_NONE;
681 }
682 
683 static RISCVException read_mtvec(CPURISCVState *env, int csrno,
684                                  target_ulong *val)
685 {
686     *val = env->mtvec;
687     return RISCV_EXCP_NONE;
688 }
689 
690 static RISCVException write_mtvec(CPURISCVState *env, int csrno,
691                                   target_ulong val)
692 {
693     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
694     if ((val & 3) < 2) {
695         env->mtvec = val;
696     } else {
697         qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
698     }
699     return RISCV_EXCP_NONE;
700 }
701 
702 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
703                                       target_ulong *val)
704 {
705     *val = env->mcounteren;
706     return RISCV_EXCP_NONE;
707 }
708 
709 static RISCVException write_mcounteren(CPURISCVState *env, int csrno,
710                                        target_ulong val)
711 {
712     env->mcounteren = val;
713     return RISCV_EXCP_NONE;
714 }
715 
716 /* Machine Trap Handling */
717 static RISCVException read_mscratch(CPURISCVState *env, int csrno,
718                                     target_ulong *val)
719 {
720     *val = env->mscratch;
721     return RISCV_EXCP_NONE;
722 }
723 
724 static RISCVException write_mscratch(CPURISCVState *env, int csrno,
725                                      target_ulong val)
726 {
727     env->mscratch = val;
728     return RISCV_EXCP_NONE;
729 }
730 
731 static RISCVException read_mepc(CPURISCVState *env, int csrno,
732                                      target_ulong *val)
733 {
734     *val = env->mepc;
735     return RISCV_EXCP_NONE;
736 }
737 
738 static RISCVException write_mepc(CPURISCVState *env, int csrno,
739                                      target_ulong val)
740 {
741     env->mepc = val;
742     return RISCV_EXCP_NONE;
743 }
744 
745 static RISCVException read_mcause(CPURISCVState *env, int csrno,
746                                      target_ulong *val)
747 {
748     *val = env->mcause;
749     return RISCV_EXCP_NONE;
750 }
751 
752 static RISCVException write_mcause(CPURISCVState *env, int csrno,
753                                      target_ulong val)
754 {
755     env->mcause = val;
756     return RISCV_EXCP_NONE;
757 }
758 
759 static RISCVException read_mtval(CPURISCVState *env, int csrno,
760                                  target_ulong *val)
761 {
762     *val = env->mtval;
763     return RISCV_EXCP_NONE;
764 }
765 
766 static RISCVException write_mtval(CPURISCVState *env, int csrno,
767                                   target_ulong val)
768 {
769     env->mtval = val;
770     return RISCV_EXCP_NONE;
771 }
772 
773 static RISCVException rmw_mip(CPURISCVState *env, int csrno,
774                               target_ulong *ret_value,
775                               target_ulong new_value, target_ulong write_mask)
776 {
777     RISCVCPU *cpu = env_archcpu(env);
778     /* Allow software control of delegable interrupts not claimed by hardware */
779     target_ulong mask = write_mask & delegable_ints & ~env->miclaim;
780     uint32_t old_mip;
781 
782     if (mask) {
783         old_mip = riscv_cpu_update_mip(cpu, mask, (new_value & mask));
784     } else {
785         old_mip = env->mip;
786     }
787 
788     if (ret_value) {
789         *ret_value = old_mip;
790     }
791 
792     return RISCV_EXCP_NONE;
793 }
794 
795 /* Supervisor Trap Setup */
796 static RISCVException read_sstatus(CPURISCVState *env, int csrno,
797                                    target_ulong *val)
798 {
799     target_ulong mask = (sstatus_v1_10_mask);
800 
801     if (riscv_cpu_mxl(env) == MXL_RV32) {
802         mask |= SSTATUS32_SD;
803     } else {
804         mask |= SSTATUS64_SD;
805     }
806 
807     *val = env->mstatus & mask;
808     return RISCV_EXCP_NONE;
809 }
810 
811 static RISCVException write_sstatus(CPURISCVState *env, int csrno,
812                                     target_ulong val)
813 {
814     target_ulong mask = (sstatus_v1_10_mask);
815     target_ulong newval = (env->mstatus & ~mask) | (val & mask);
816     return write_mstatus(env, CSR_MSTATUS, newval);
817 }
818 
819 static RISCVException read_vsie(CPURISCVState *env, int csrno,
820                                 target_ulong *val)
821 {
822     /* Shift the VS bits to their S bit location in vsie */
823     *val = (env->mie & env->hideleg & VS_MODE_INTERRUPTS) >> 1;
824     return RISCV_EXCP_NONE;
825 }
826 
827 static RISCVException read_sie(CPURISCVState *env, int csrno,
828                                target_ulong *val)
829 {
830     if (riscv_cpu_virt_enabled(env)) {
831         read_vsie(env, CSR_VSIE, val);
832     } else {
833         *val = env->mie & env->mideleg;
834     }
835     return RISCV_EXCP_NONE;
836 }
837 
838 static RISCVException write_vsie(CPURISCVState *env, int csrno,
839                                  target_ulong val)
840 {
841     /* Shift the S bits to their VS bit location in mie */
842     target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS) |
843                           ((val << 1) & env->hideleg & VS_MODE_INTERRUPTS);
844     return write_mie(env, CSR_MIE, newval);
845 }
846 
847 static int write_sie(CPURISCVState *env, int csrno, target_ulong val)
848 {
849     if (riscv_cpu_virt_enabled(env)) {
850         write_vsie(env, CSR_VSIE, val);
851     } else {
852         target_ulong newval = (env->mie & ~S_MODE_INTERRUPTS) |
853                               (val & S_MODE_INTERRUPTS);
854         write_mie(env, CSR_MIE, newval);
855     }
856 
857     return RISCV_EXCP_NONE;
858 }
859 
860 static RISCVException read_stvec(CPURISCVState *env, int csrno,
861                                  target_ulong *val)
862 {
863     *val = env->stvec;
864     return RISCV_EXCP_NONE;
865 }
866 
867 static RISCVException write_stvec(CPURISCVState *env, int csrno,
868                                   target_ulong val)
869 {
870     /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
871     if ((val & 3) < 2) {
872         env->stvec = val;
873     } else {
874         qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
875     }
876     return RISCV_EXCP_NONE;
877 }
878 
879 static RISCVException read_scounteren(CPURISCVState *env, int csrno,
880                                       target_ulong *val)
881 {
882     *val = env->scounteren;
883     return RISCV_EXCP_NONE;
884 }
885 
886 static RISCVException write_scounteren(CPURISCVState *env, int csrno,
887                                        target_ulong val)
888 {
889     env->scounteren = val;
890     return RISCV_EXCP_NONE;
891 }
892 
893 /* Supervisor Trap Handling */
894 static RISCVException read_sscratch(CPURISCVState *env, int csrno,
895                                     target_ulong *val)
896 {
897     *val = env->sscratch;
898     return RISCV_EXCP_NONE;
899 }
900 
901 static RISCVException write_sscratch(CPURISCVState *env, int csrno,
902                                      target_ulong val)
903 {
904     env->sscratch = val;
905     return RISCV_EXCP_NONE;
906 }
907 
908 static RISCVException read_sepc(CPURISCVState *env, int csrno,
909                                 target_ulong *val)
910 {
911     *val = env->sepc;
912     return RISCV_EXCP_NONE;
913 }
914 
915 static RISCVException write_sepc(CPURISCVState *env, int csrno,
916                                  target_ulong val)
917 {
918     env->sepc = val;
919     return RISCV_EXCP_NONE;
920 }
921 
922 static RISCVException read_scause(CPURISCVState *env, int csrno,
923                                   target_ulong *val)
924 {
925     *val = env->scause;
926     return RISCV_EXCP_NONE;
927 }
928 
929 static RISCVException write_scause(CPURISCVState *env, int csrno,
930                                    target_ulong val)
931 {
932     env->scause = val;
933     return RISCV_EXCP_NONE;
934 }
935 
936 static RISCVException read_stval(CPURISCVState *env, int csrno,
937                                  target_ulong *val)
938 {
939     *val = env->stval;
940     return RISCV_EXCP_NONE;
941 }
942 
943 static RISCVException write_stval(CPURISCVState *env, int csrno,
944                                   target_ulong val)
945 {
946     env->stval = val;
947     return RISCV_EXCP_NONE;
948 }
949 
950 static RISCVException rmw_vsip(CPURISCVState *env, int csrno,
951                                target_ulong *ret_value,
952                                target_ulong new_value, target_ulong write_mask)
953 {
954     /* Shift the S bits to their VS bit location in mip */
955     int ret = rmw_mip(env, 0, ret_value, new_value << 1,
956                       (write_mask << 1) & vsip_writable_mask & env->hideleg);
957 
958     if (ret_value) {
959         *ret_value &= VS_MODE_INTERRUPTS;
960         /* Shift the VS bits to their S bit location in vsip */
961         *ret_value >>= 1;
962     }
963     return ret;
964 }
965 
966 static RISCVException rmw_sip(CPURISCVState *env, int csrno,
967                               target_ulong *ret_value,
968                               target_ulong new_value, target_ulong write_mask)
969 {
970     int ret;
971 
972     if (riscv_cpu_virt_enabled(env)) {
973         ret = rmw_vsip(env, CSR_VSIP, ret_value, new_value, write_mask);
974     } else {
975         ret = rmw_mip(env, CSR_MSTATUS, ret_value, new_value,
976                       write_mask & env->mideleg & sip_writable_mask);
977     }
978 
979     if (ret_value) {
980         *ret_value &= env->mideleg;
981     }
982     return ret;
983 }
984 
985 /* Supervisor Protection and Translation */
986 static RISCVException read_satp(CPURISCVState *env, int csrno,
987                                 target_ulong *val)
988 {
989     if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
990         *val = 0;
991         return RISCV_EXCP_NONE;
992     }
993 
994     if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
995         return RISCV_EXCP_ILLEGAL_INST;
996     } else {
997         *val = env->satp;
998     }
999 
1000     return RISCV_EXCP_NONE;
1001 }
1002 
1003 static RISCVException write_satp(CPURISCVState *env, int csrno,
1004                                  target_ulong val)
1005 {
1006     target_ulong vm, mask, asid;
1007 
1008     if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
1009         return RISCV_EXCP_NONE;
1010     }
1011 
1012     if (riscv_cpu_mxl(env) == MXL_RV32) {
1013         vm = validate_vm(env, get_field(val, SATP32_MODE));
1014         mask = (val ^ env->satp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN);
1015         asid = (val ^ env->satp) & SATP32_ASID;
1016     } else {
1017         vm = validate_vm(env, get_field(val, SATP64_MODE));
1018         mask = (val ^ env->satp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN);
1019         asid = (val ^ env->satp) & SATP64_ASID;
1020     }
1021 
1022     if (vm && mask) {
1023         if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
1024             return RISCV_EXCP_ILLEGAL_INST;
1025         } else {
1026             if (asid) {
1027                 tlb_flush(env_cpu(env));
1028             }
1029             env->satp = val;
1030         }
1031     }
1032     return RISCV_EXCP_NONE;
1033 }
1034 
1035 /* Hypervisor Extensions */
1036 static RISCVException read_hstatus(CPURISCVState *env, int csrno,
1037                                    target_ulong *val)
1038 {
1039     *val = env->hstatus;
1040     if (riscv_cpu_mxl(env) != MXL_RV32) {
1041         /* We only support 64-bit VSXL */
1042         *val = set_field(*val, HSTATUS_VSXL, 2);
1043     }
1044     /* We only support little endian */
1045     *val = set_field(*val, HSTATUS_VSBE, 0);
1046     return RISCV_EXCP_NONE;
1047 }
1048 
1049 static RISCVException write_hstatus(CPURISCVState *env, int csrno,
1050                                     target_ulong val)
1051 {
1052     env->hstatus = val;
1053     if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) {
1054         qemu_log_mask(LOG_UNIMP, "QEMU does not support mixed HSXLEN options.");
1055     }
1056     if (get_field(val, HSTATUS_VSBE) != 0) {
1057         qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests.");
1058     }
1059     return RISCV_EXCP_NONE;
1060 }
1061 
1062 static RISCVException read_hedeleg(CPURISCVState *env, int csrno,
1063                                    target_ulong *val)
1064 {
1065     *val = env->hedeleg;
1066     return RISCV_EXCP_NONE;
1067 }
1068 
1069 static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
1070                                     target_ulong val)
1071 {
1072     env->hedeleg = val & vs_delegable_excps;
1073     return RISCV_EXCP_NONE;
1074 }
1075 
1076 static RISCVException read_hideleg(CPURISCVState *env, int csrno,
1077                                    target_ulong *val)
1078 {
1079     *val = env->hideleg;
1080     return RISCV_EXCP_NONE;
1081 }
1082 
1083 static RISCVException write_hideleg(CPURISCVState *env, int csrno,
1084                                     target_ulong val)
1085 {
1086     env->hideleg = val & vs_delegable_ints;
1087     return RISCV_EXCP_NONE;
1088 }
1089 
1090 static RISCVException rmw_hvip(CPURISCVState *env, int csrno,
1091                                target_ulong *ret_value,
1092                                target_ulong new_value, target_ulong write_mask)
1093 {
1094     int ret = rmw_mip(env, 0, ret_value, new_value,
1095                       write_mask & hvip_writable_mask);
1096 
1097     if (ret_value) {
1098         *ret_value &= hvip_writable_mask;
1099     }
1100     return ret;
1101 }
1102 
1103 static RISCVException rmw_hip(CPURISCVState *env, int csrno,
1104                               target_ulong *ret_value,
1105                               target_ulong new_value, target_ulong write_mask)
1106 {
1107     int ret = rmw_mip(env, 0, ret_value, new_value,
1108                       write_mask & hip_writable_mask);
1109 
1110     if (ret_value) {
1111         *ret_value &= hip_writable_mask;
1112     }
1113     return ret;
1114 }
1115 
1116 static RISCVException read_hie(CPURISCVState *env, int csrno,
1117                                target_ulong *val)
1118 {
1119     *val = env->mie & VS_MODE_INTERRUPTS;
1120     return RISCV_EXCP_NONE;
1121 }
1122 
1123 static RISCVException write_hie(CPURISCVState *env, int csrno,
1124                                 target_ulong val)
1125 {
1126     target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS) | (val & VS_MODE_INTERRUPTS);
1127     return write_mie(env, CSR_MIE, newval);
1128 }
1129 
1130 static RISCVException read_hcounteren(CPURISCVState *env, int csrno,
1131                                       target_ulong *val)
1132 {
1133     *val = env->hcounteren;
1134     return RISCV_EXCP_NONE;
1135 }
1136 
1137 static RISCVException write_hcounteren(CPURISCVState *env, int csrno,
1138                                        target_ulong val)
1139 {
1140     env->hcounteren = val;
1141     return RISCV_EXCP_NONE;
1142 }
1143 
1144 static RISCVException write_hgeie(CPURISCVState *env, int csrno,
1145                                   target_ulong val)
1146 {
1147     if (val) {
1148         qemu_log_mask(LOG_UNIMP, "No support for a non-zero GEILEN.");
1149     }
1150     return RISCV_EXCP_NONE;
1151 }
1152 
1153 static RISCVException read_htval(CPURISCVState *env, int csrno,
1154                                  target_ulong *val)
1155 {
1156     *val = env->htval;
1157     return RISCV_EXCP_NONE;
1158 }
1159 
1160 static RISCVException write_htval(CPURISCVState *env, int csrno,
1161                                   target_ulong val)
1162 {
1163     env->htval = val;
1164     return RISCV_EXCP_NONE;
1165 }
1166 
1167 static RISCVException read_htinst(CPURISCVState *env, int csrno,
1168                                   target_ulong *val)
1169 {
1170     *val = env->htinst;
1171     return RISCV_EXCP_NONE;
1172 }
1173 
1174 static RISCVException write_htinst(CPURISCVState *env, int csrno,
1175                                    target_ulong val)
1176 {
1177     return RISCV_EXCP_NONE;
1178 }
1179 
1180 static RISCVException write_hgeip(CPURISCVState *env, int csrno,
1181                                   target_ulong val)
1182 {
1183     if (val) {
1184         qemu_log_mask(LOG_UNIMP, "No support for a non-zero GEILEN.");
1185     }
1186     return RISCV_EXCP_NONE;
1187 }
1188 
1189 static RISCVException read_hgatp(CPURISCVState *env, int csrno,
1190                                  target_ulong *val)
1191 {
1192     *val = env->hgatp;
1193     return RISCV_EXCP_NONE;
1194 }
1195 
1196 static RISCVException write_hgatp(CPURISCVState *env, int csrno,
1197                                   target_ulong val)
1198 {
1199     env->hgatp = val;
1200     return RISCV_EXCP_NONE;
1201 }
1202 
1203 static RISCVException read_htimedelta(CPURISCVState *env, int csrno,
1204                                       target_ulong *val)
1205 {
1206     if (!env->rdtime_fn) {
1207         return RISCV_EXCP_ILLEGAL_INST;
1208     }
1209 
1210     *val = env->htimedelta;
1211     return RISCV_EXCP_NONE;
1212 }
1213 
1214 static RISCVException write_htimedelta(CPURISCVState *env, int csrno,
1215                                        target_ulong val)
1216 {
1217     if (!env->rdtime_fn) {
1218         return RISCV_EXCP_ILLEGAL_INST;
1219     }
1220 
1221     if (riscv_cpu_mxl(env) == MXL_RV32) {
1222         env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
1223     } else {
1224         env->htimedelta = val;
1225     }
1226     return RISCV_EXCP_NONE;
1227 }
1228 
1229 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno,
1230                                        target_ulong *val)
1231 {
1232     if (!env->rdtime_fn) {
1233         return RISCV_EXCP_ILLEGAL_INST;
1234     }
1235 
1236     *val = env->htimedelta >> 32;
1237     return RISCV_EXCP_NONE;
1238 }
1239 
1240 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno,
1241                                         target_ulong val)
1242 {
1243     if (!env->rdtime_fn) {
1244         return RISCV_EXCP_ILLEGAL_INST;
1245     }
1246 
1247     env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
1248     return RISCV_EXCP_NONE;
1249 }
1250 
1251 /* Virtual CSR Registers */
1252 static RISCVException read_vsstatus(CPURISCVState *env, int csrno,
1253                                     target_ulong *val)
1254 {
1255     *val = env->vsstatus;
1256     return RISCV_EXCP_NONE;
1257 }
1258 
1259 static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
1260                                      target_ulong val)
1261 {
1262     uint64_t mask = (target_ulong)-1;
1263     env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val;
1264     return RISCV_EXCP_NONE;
1265 }
1266 
1267 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val)
1268 {
1269     *val = env->vstvec;
1270     return RISCV_EXCP_NONE;
1271 }
1272 
1273 static RISCVException write_vstvec(CPURISCVState *env, int csrno,
1274                                    target_ulong val)
1275 {
1276     env->vstvec = val;
1277     return RISCV_EXCP_NONE;
1278 }
1279 
1280 static RISCVException read_vsscratch(CPURISCVState *env, int csrno,
1281                                      target_ulong *val)
1282 {
1283     *val = env->vsscratch;
1284     return RISCV_EXCP_NONE;
1285 }
1286 
1287 static RISCVException write_vsscratch(CPURISCVState *env, int csrno,
1288                                       target_ulong val)
1289 {
1290     env->vsscratch = val;
1291     return RISCV_EXCP_NONE;
1292 }
1293 
1294 static RISCVException read_vsepc(CPURISCVState *env, int csrno,
1295                                  target_ulong *val)
1296 {
1297     *val = env->vsepc;
1298     return RISCV_EXCP_NONE;
1299 }
1300 
1301 static RISCVException write_vsepc(CPURISCVState *env, int csrno,
1302                                   target_ulong val)
1303 {
1304     env->vsepc = val;
1305     return RISCV_EXCP_NONE;
1306 }
1307 
1308 static RISCVException read_vscause(CPURISCVState *env, int csrno,
1309                                    target_ulong *val)
1310 {
1311     *val = env->vscause;
1312     return RISCV_EXCP_NONE;
1313 }
1314 
1315 static RISCVException write_vscause(CPURISCVState *env, int csrno,
1316                                     target_ulong val)
1317 {
1318     env->vscause = val;
1319     return RISCV_EXCP_NONE;
1320 }
1321 
1322 static RISCVException read_vstval(CPURISCVState *env, int csrno,
1323                                   target_ulong *val)
1324 {
1325     *val = env->vstval;
1326     return RISCV_EXCP_NONE;
1327 }
1328 
1329 static RISCVException write_vstval(CPURISCVState *env, int csrno,
1330                                    target_ulong val)
1331 {
1332     env->vstval = val;
1333     return RISCV_EXCP_NONE;
1334 }
1335 
1336 static RISCVException read_vsatp(CPURISCVState *env, int csrno,
1337                                  target_ulong *val)
1338 {
1339     *val = env->vsatp;
1340     return RISCV_EXCP_NONE;
1341 }
1342 
1343 static RISCVException write_vsatp(CPURISCVState *env, int csrno,
1344                                   target_ulong val)
1345 {
1346     env->vsatp = val;
1347     return RISCV_EXCP_NONE;
1348 }
1349 
1350 static RISCVException read_mtval2(CPURISCVState *env, int csrno,
1351                                   target_ulong *val)
1352 {
1353     *val = env->mtval2;
1354     return RISCV_EXCP_NONE;
1355 }
1356 
1357 static RISCVException write_mtval2(CPURISCVState *env, int csrno,
1358                                    target_ulong val)
1359 {
1360     env->mtval2 = val;
1361     return RISCV_EXCP_NONE;
1362 }
1363 
1364 static RISCVException read_mtinst(CPURISCVState *env, int csrno,
1365                                   target_ulong *val)
1366 {
1367     *val = env->mtinst;
1368     return RISCV_EXCP_NONE;
1369 }
1370 
1371 static RISCVException write_mtinst(CPURISCVState *env, int csrno,
1372                                    target_ulong val)
1373 {
1374     env->mtinst = val;
1375     return RISCV_EXCP_NONE;
1376 }
1377 
1378 /* Physical Memory Protection */
1379 static RISCVException read_mseccfg(CPURISCVState *env, int csrno,
1380                                    target_ulong *val)
1381 {
1382     *val = mseccfg_csr_read(env);
1383     return RISCV_EXCP_NONE;
1384 }
1385 
1386 static RISCVException write_mseccfg(CPURISCVState *env, int csrno,
1387                          target_ulong val)
1388 {
1389     mseccfg_csr_write(env, val);
1390     return RISCV_EXCP_NONE;
1391 }
1392 
1393 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
1394                                   target_ulong *val)
1395 {
1396     *val = pmpcfg_csr_read(env, csrno - CSR_PMPCFG0);
1397     return RISCV_EXCP_NONE;
1398 }
1399 
1400 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno,
1401                                    target_ulong val)
1402 {
1403     pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val);
1404     return RISCV_EXCP_NONE;
1405 }
1406 
1407 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno,
1408                                    target_ulong *val)
1409 {
1410     *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
1411     return RISCV_EXCP_NONE;
1412 }
1413 
1414 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,
1415                                     target_ulong val)
1416 {
1417     pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
1418     return RISCV_EXCP_NONE;
1419 }
1420 
1421 #endif
1422 
1423 /*
1424  * riscv_csrrw - read and/or update control and status register
1425  *
1426  * csrr   <->  riscv_csrrw(env, csrno, ret_value, 0, 0);
1427  * csrrw  <->  riscv_csrrw(env, csrno, ret_value, value, -1);
1428  * csrrs  <->  riscv_csrrw(env, csrno, ret_value, -1, value);
1429  * csrrc  <->  riscv_csrrw(env, csrno, ret_value, 0, value);
1430  */
1431 
1432 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
1433                            target_ulong *ret_value,
1434                            target_ulong new_value, target_ulong write_mask)
1435 {
1436     RISCVException ret;
1437     target_ulong old_value;
1438     RISCVCPU *cpu = env_archcpu(env);
1439     int read_only = get_field(csrno, 0xC00) == 3;
1440 
1441     /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */
1442 #if !defined(CONFIG_USER_ONLY)
1443     int effective_priv = env->priv;
1444 
1445     if (riscv_has_ext(env, RVH) &&
1446         env->priv == PRV_S &&
1447         !riscv_cpu_virt_enabled(env)) {
1448         /*
1449          * We are in S mode without virtualisation, therefore we are in HS Mode.
1450          * Add 1 to the effective privledge level to allow us to access the
1451          * Hypervisor CSRs.
1452          */
1453         effective_priv++;
1454     }
1455 
1456     if (!env->debugger && (effective_priv < get_field(csrno, 0x300))) {
1457         return RISCV_EXCP_ILLEGAL_INST;
1458     }
1459 #endif
1460     if (write_mask && read_only) {
1461         return RISCV_EXCP_ILLEGAL_INST;
1462     }
1463 
1464     /* ensure the CSR extension is enabled. */
1465     if (!cpu->cfg.ext_icsr) {
1466         return RISCV_EXCP_ILLEGAL_INST;
1467     }
1468 
1469     /* check predicate */
1470     if (!csr_ops[csrno].predicate) {
1471         return RISCV_EXCP_ILLEGAL_INST;
1472     }
1473     ret = csr_ops[csrno].predicate(env, csrno);
1474     if (ret != RISCV_EXCP_NONE) {
1475         return ret;
1476     }
1477 
1478     /* execute combined read/write operation if it exists */
1479     if (csr_ops[csrno].op) {
1480         return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
1481     }
1482 
1483     /* if no accessor exists then return failure */
1484     if (!csr_ops[csrno].read) {
1485         return RISCV_EXCP_ILLEGAL_INST;
1486     }
1487     /* read old value */
1488     ret = csr_ops[csrno].read(env, csrno, &old_value);
1489     if (ret != RISCV_EXCP_NONE) {
1490         return ret;
1491     }
1492 
1493     /* write value if writable and write mask set, otherwise drop writes */
1494     if (write_mask) {
1495         new_value = (old_value & ~write_mask) | (new_value & write_mask);
1496         if (csr_ops[csrno].write) {
1497             ret = csr_ops[csrno].write(env, csrno, new_value);
1498             if (ret != RISCV_EXCP_NONE) {
1499                 return ret;
1500             }
1501         }
1502     }
1503 
1504     /* return old value */
1505     if (ret_value) {
1506         *ret_value = old_value;
1507     }
1508 
1509     return RISCV_EXCP_NONE;
1510 }
1511 
1512 /*
1513  * Debugger support.  If not in user mode, set env->debugger before the
1514  * riscv_csrrw call and clear it after the call.
1515  */
1516 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
1517                                  target_ulong *ret_value,
1518                                  target_ulong new_value,
1519                                  target_ulong write_mask)
1520 {
1521     RISCVException ret;
1522 #if !defined(CONFIG_USER_ONLY)
1523     env->debugger = true;
1524 #endif
1525     ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
1526 #if !defined(CONFIG_USER_ONLY)
1527     env->debugger = false;
1528 #endif
1529     return ret;
1530 }
1531 
1532 /* Control and Status Register function table */
1533 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
1534     /* User Floating-Point CSRs */
1535     [CSR_FFLAGS]   = { "fflags",   fs,     read_fflags,  write_fflags },
1536     [CSR_FRM]      = { "frm",      fs,     read_frm,     write_frm    },
1537     [CSR_FCSR]     = { "fcsr",     fs,     read_fcsr,    write_fcsr   },
1538     /* Vector CSRs */
1539     [CSR_VSTART]   = { "vstart",   vs,     read_vstart,  write_vstart },
1540     [CSR_VXSAT]    = { "vxsat",    vs,     read_vxsat,   write_vxsat  },
1541     [CSR_VXRM]     = { "vxrm",     vs,     read_vxrm,    write_vxrm   },
1542     [CSR_VL]       = { "vl",       vs,     read_vl                    },
1543     [CSR_VTYPE]    = { "vtype",    vs,     read_vtype                 },
1544     /* User Timers and Counters */
1545     [CSR_CYCLE]    = { "cycle",    ctr,    read_instret  },
1546     [CSR_INSTRET]  = { "instret",  ctr,    read_instret  },
1547     [CSR_CYCLEH]   = { "cycleh",   ctr32,  read_instreth },
1548     [CSR_INSTRETH] = { "instreth", ctr32,  read_instreth },
1549 
1550     /*
1551      * In privileged mode, the monitor will have to emulate TIME CSRs only if
1552      * rdtime callback is not provided by machine/platform emulation.
1553      */
1554     [CSR_TIME]  = { "time",  ctr,   read_time  },
1555     [CSR_TIMEH] = { "timeh", ctr32, read_timeh },
1556 
1557 #if !defined(CONFIG_USER_ONLY)
1558     /* Machine Timers and Counters */
1559     [CSR_MCYCLE]    = { "mcycle",    any,   read_instret  },
1560     [CSR_MINSTRET]  = { "minstret",  any,   read_instret  },
1561     [CSR_MCYCLEH]   = { "mcycleh",   any32, read_instreth },
1562     [CSR_MINSTRETH] = { "minstreth", any32, read_instreth },
1563 
1564     /* Machine Information Registers */
1565     [CSR_MVENDORID] = { "mvendorid", any,   read_zero    },
1566     [CSR_MARCHID]   = { "marchid",   any,   read_zero    },
1567     [CSR_MIMPID]    = { "mimpid",    any,   read_zero    },
1568     [CSR_MHARTID]   = { "mhartid",   any,   read_mhartid },
1569 
1570     /* Machine Trap Setup */
1571     [CSR_MSTATUS]     = { "mstatus",    any,   read_mstatus,     write_mstatus     },
1572     [CSR_MISA]        = { "misa",       any,   read_misa,        write_misa        },
1573     [CSR_MIDELEG]     = { "mideleg",    any,   read_mideleg,     write_mideleg     },
1574     [CSR_MEDELEG]     = { "medeleg",    any,   read_medeleg,     write_medeleg     },
1575     [CSR_MIE]         = { "mie",        any,   read_mie,         write_mie         },
1576     [CSR_MTVEC]       = { "mtvec",      any,   read_mtvec,       write_mtvec       },
1577     [CSR_MCOUNTEREN]  = { "mcounteren", any,   read_mcounteren,  write_mcounteren  },
1578 
1579     [CSR_MSTATUSH]    = { "mstatush",   any32, read_mstatush,    write_mstatush    },
1580 
1581     /* Machine Trap Handling */
1582     [CSR_MSCRATCH] = { "mscratch", any,  read_mscratch, write_mscratch },
1583     [CSR_MEPC]     = { "mepc",     any,  read_mepc,     write_mepc     },
1584     [CSR_MCAUSE]   = { "mcause",   any,  read_mcause,   write_mcause   },
1585     [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval    },
1586     [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip        },
1587 
1588     /* Supervisor Trap Setup */
1589     [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,    write_sstatus    },
1590     [CSR_SIE]        = { "sie",        smode, read_sie,        write_sie        },
1591     [CSR_STVEC]      = { "stvec",      smode, read_stvec,      write_stvec      },
1592     [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren, write_scounteren },
1593 
1594     /* Supervisor Trap Handling */
1595     [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch },
1596     [CSR_SEPC]     = { "sepc",     smode, read_sepc,     write_sepc     },
1597     [CSR_SCAUSE]   = { "scause",   smode, read_scause,   write_scause   },
1598     [CSR_STVAL]    = { "stval",    smode, read_stval,   write_stval   },
1599     [CSR_SIP]      = { "sip",      smode, NULL,    NULL, rmw_sip        },
1600 
1601     /* Supervisor Protection and Translation */
1602     [CSR_SATP]     = { "satp",     smode, read_satp,    write_satp      },
1603 
1604     [CSR_HSTATUS]     = { "hstatus",     hmode,   read_hstatus,     write_hstatus     },
1605     [CSR_HEDELEG]     = { "hedeleg",     hmode,   read_hedeleg,     write_hedeleg     },
1606     [CSR_HIDELEG]     = { "hideleg",     hmode,   read_hideleg,     write_hideleg     },
1607     [CSR_HVIP]        = { "hvip",        hmode,   NULL,   NULL,     rmw_hvip          },
1608     [CSR_HIP]         = { "hip",         hmode,   NULL,   NULL,     rmw_hip           },
1609     [CSR_HIE]         = { "hie",         hmode,   read_hie,         write_hie         },
1610     [CSR_HCOUNTEREN]  = { "hcounteren",  hmode,   read_hcounteren,  write_hcounteren  },
1611     [CSR_HGEIE]       = { "hgeie",       hmode,   read_zero,        write_hgeie       },
1612     [CSR_HTVAL]       = { "htval",       hmode,   read_htval,       write_htval       },
1613     [CSR_HTINST]      = { "htinst",      hmode,   read_htinst,      write_htinst      },
1614     [CSR_HGEIP]       = { "hgeip",       hmode,   read_zero,        write_hgeip       },
1615     [CSR_HGATP]       = { "hgatp",       hmode,   read_hgatp,       write_hgatp       },
1616     [CSR_HTIMEDELTA]  = { "htimedelta",  hmode,   read_htimedelta,  write_htimedelta  },
1617     [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah, write_htimedeltah },
1618 
1619     [CSR_VSSTATUS]    = { "vsstatus",    hmode,   read_vsstatus,    write_vsstatus    },
1620     [CSR_VSIP]        = { "vsip",        hmode,   NULL,    NULL,    rmw_vsip          },
1621     [CSR_VSIE]        = { "vsie",        hmode,   read_vsie,        write_vsie        },
1622     [CSR_VSTVEC]      = { "vstvec",      hmode,   read_vstvec,      write_vstvec      },
1623     [CSR_VSSCRATCH]   = { "vsscratch",   hmode,   read_vsscratch,   write_vsscratch   },
1624     [CSR_VSEPC]       = { "vsepc",       hmode,   read_vsepc,       write_vsepc       },
1625     [CSR_VSCAUSE]     = { "vscause",     hmode,   read_vscause,     write_vscause     },
1626     [CSR_VSTVAL]      = { "vstval",      hmode,   read_vstval,      write_vstval      },
1627     [CSR_VSATP]       = { "vsatp",       hmode,   read_vsatp,       write_vsatp       },
1628 
1629     [CSR_MTVAL2]      = { "mtval2",      hmode,   read_mtval2,      write_mtval2      },
1630     [CSR_MTINST]      = { "mtinst",      hmode,   read_mtinst,      write_mtinst      },
1631 
1632     /* Physical Memory Protection */
1633     [CSR_MSECCFG]    = { "mseccfg",  epmp, read_mseccfg, write_mseccfg },
1634     [CSR_PMPCFG0]    = { "pmpcfg0",   pmp, read_pmpcfg,  write_pmpcfg  },
1635     [CSR_PMPCFG1]    = { "pmpcfg1",   pmp, read_pmpcfg,  write_pmpcfg  },
1636     [CSR_PMPCFG2]    = { "pmpcfg2",   pmp, read_pmpcfg,  write_pmpcfg  },
1637     [CSR_PMPCFG3]    = { "pmpcfg3",   pmp, read_pmpcfg,  write_pmpcfg  },
1638     [CSR_PMPADDR0]   = { "pmpaddr0",  pmp, read_pmpaddr, write_pmpaddr },
1639     [CSR_PMPADDR1]   = { "pmpaddr1",  pmp, read_pmpaddr, write_pmpaddr },
1640     [CSR_PMPADDR2]   = { "pmpaddr2",  pmp, read_pmpaddr, write_pmpaddr },
1641     [CSR_PMPADDR3]   = { "pmpaddr3",  pmp, read_pmpaddr, write_pmpaddr },
1642     [CSR_PMPADDR4]   = { "pmpaddr4",  pmp, read_pmpaddr, write_pmpaddr },
1643     [CSR_PMPADDR5]   = { "pmpaddr5",  pmp, read_pmpaddr, write_pmpaddr },
1644     [CSR_PMPADDR6]   = { "pmpaddr6",  pmp, read_pmpaddr, write_pmpaddr },
1645     [CSR_PMPADDR7]   = { "pmpaddr7",  pmp, read_pmpaddr, write_pmpaddr },
1646     [CSR_PMPADDR8]   = { "pmpaddr8",  pmp, read_pmpaddr, write_pmpaddr },
1647     [CSR_PMPADDR9]   = { "pmpaddr9",  pmp, read_pmpaddr, write_pmpaddr },
1648     [CSR_PMPADDR10]  = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr },
1649     [CSR_PMPADDR11]  = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr },
1650     [CSR_PMPADDR12]  = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr },
1651     [CSR_PMPADDR13]  = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr },
1652     [CSR_PMPADDR14] =  { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr },
1653     [CSR_PMPADDR15] =  { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr },
1654 
1655     /* Performance Counters */
1656     [CSR_HPMCOUNTER3]    = { "hpmcounter3",    ctr,    read_zero },
1657     [CSR_HPMCOUNTER4]    = { "hpmcounter4",    ctr,    read_zero },
1658     [CSR_HPMCOUNTER5]    = { "hpmcounter5",    ctr,    read_zero },
1659     [CSR_HPMCOUNTER6]    = { "hpmcounter6",    ctr,    read_zero },
1660     [CSR_HPMCOUNTER7]    = { "hpmcounter7",    ctr,    read_zero },
1661     [CSR_HPMCOUNTER8]    = { "hpmcounter8",    ctr,    read_zero },
1662     [CSR_HPMCOUNTER9]    = { "hpmcounter9",    ctr,    read_zero },
1663     [CSR_HPMCOUNTER10]   = { "hpmcounter10",   ctr,    read_zero },
1664     [CSR_HPMCOUNTER11]   = { "hpmcounter11",   ctr,    read_zero },
1665     [CSR_HPMCOUNTER12]   = { "hpmcounter12",   ctr,    read_zero },
1666     [CSR_HPMCOUNTER13]   = { "hpmcounter13",   ctr,    read_zero },
1667     [CSR_HPMCOUNTER14]   = { "hpmcounter14",   ctr,    read_zero },
1668     [CSR_HPMCOUNTER15]   = { "hpmcounter15",   ctr,    read_zero },
1669     [CSR_HPMCOUNTER16]   = { "hpmcounter16",   ctr,    read_zero },
1670     [CSR_HPMCOUNTER17]   = { "hpmcounter17",   ctr,    read_zero },
1671     [CSR_HPMCOUNTER18]   = { "hpmcounter18",   ctr,    read_zero },
1672     [CSR_HPMCOUNTER19]   = { "hpmcounter19",   ctr,    read_zero },
1673     [CSR_HPMCOUNTER20]   = { "hpmcounter20",   ctr,    read_zero },
1674     [CSR_HPMCOUNTER21]   = { "hpmcounter21",   ctr,    read_zero },
1675     [CSR_HPMCOUNTER22]   = { "hpmcounter22",   ctr,    read_zero },
1676     [CSR_HPMCOUNTER23]   = { "hpmcounter23",   ctr,    read_zero },
1677     [CSR_HPMCOUNTER24]   = { "hpmcounter24",   ctr,    read_zero },
1678     [CSR_HPMCOUNTER25]   = { "hpmcounter25",   ctr,    read_zero },
1679     [CSR_HPMCOUNTER26]   = { "hpmcounter26",   ctr,    read_zero },
1680     [CSR_HPMCOUNTER27]   = { "hpmcounter27",   ctr,    read_zero },
1681     [CSR_HPMCOUNTER28]   = { "hpmcounter28",   ctr,    read_zero },
1682     [CSR_HPMCOUNTER29]   = { "hpmcounter29",   ctr,    read_zero },
1683     [CSR_HPMCOUNTER30]   = { "hpmcounter30",   ctr,    read_zero },
1684     [CSR_HPMCOUNTER31]   = { "hpmcounter31",   ctr,    read_zero },
1685 
1686     [CSR_MHPMCOUNTER3]   = { "mhpmcounter3",   any,    read_zero },
1687     [CSR_MHPMCOUNTER4]   = { "mhpmcounter4",   any,    read_zero },
1688     [CSR_MHPMCOUNTER5]   = { "mhpmcounter5",   any,    read_zero },
1689     [CSR_MHPMCOUNTER6]   = { "mhpmcounter6",   any,    read_zero },
1690     [CSR_MHPMCOUNTER7]   = { "mhpmcounter7",   any,    read_zero },
1691     [CSR_MHPMCOUNTER8]   = { "mhpmcounter8",   any,    read_zero },
1692     [CSR_MHPMCOUNTER9]   = { "mhpmcounter9",   any,    read_zero },
1693     [CSR_MHPMCOUNTER10]  = { "mhpmcounter10",  any,    read_zero },
1694     [CSR_MHPMCOUNTER11]  = { "mhpmcounter11",  any,    read_zero },
1695     [CSR_MHPMCOUNTER12]  = { "mhpmcounter12",  any,    read_zero },
1696     [CSR_MHPMCOUNTER13]  = { "mhpmcounter13",  any,    read_zero },
1697     [CSR_MHPMCOUNTER14]  = { "mhpmcounter14",  any,    read_zero },
1698     [CSR_MHPMCOUNTER15]  = { "mhpmcounter15",  any,    read_zero },
1699     [CSR_MHPMCOUNTER16]  = { "mhpmcounter16",  any,    read_zero },
1700     [CSR_MHPMCOUNTER17]  = { "mhpmcounter17",  any,    read_zero },
1701     [CSR_MHPMCOUNTER18]  = { "mhpmcounter18",  any,    read_zero },
1702     [CSR_MHPMCOUNTER19]  = { "mhpmcounter19",  any,    read_zero },
1703     [CSR_MHPMCOUNTER20]  = { "mhpmcounter20",  any,    read_zero },
1704     [CSR_MHPMCOUNTER21]  = { "mhpmcounter21",  any,    read_zero },
1705     [CSR_MHPMCOUNTER22]  = { "mhpmcounter22",  any,    read_zero },
1706     [CSR_MHPMCOUNTER23]  = { "mhpmcounter23",  any,    read_zero },
1707     [CSR_MHPMCOUNTER24]  = { "mhpmcounter24",  any,    read_zero },
1708     [CSR_MHPMCOUNTER25]  = { "mhpmcounter25",  any,    read_zero },
1709     [CSR_MHPMCOUNTER26]  = { "mhpmcounter26",  any,    read_zero },
1710     [CSR_MHPMCOUNTER27]  = { "mhpmcounter27",  any,    read_zero },
1711     [CSR_MHPMCOUNTER28]  = { "mhpmcounter28",  any,    read_zero },
1712     [CSR_MHPMCOUNTER29]  = { "mhpmcounter29",  any,    read_zero },
1713     [CSR_MHPMCOUNTER30]  = { "mhpmcounter30",  any,    read_zero },
1714     [CSR_MHPMCOUNTER31]  = { "mhpmcounter31",  any,    read_zero },
1715 
1716     [CSR_MHPMEVENT3]     = { "mhpmevent3",     any,    read_zero },
1717     [CSR_MHPMEVENT4]     = { "mhpmevent4",     any,    read_zero },
1718     [CSR_MHPMEVENT5]     = { "mhpmevent5",     any,    read_zero },
1719     [CSR_MHPMEVENT6]     = { "mhpmevent6",     any,    read_zero },
1720     [CSR_MHPMEVENT7]     = { "mhpmevent7",     any,    read_zero },
1721     [CSR_MHPMEVENT8]     = { "mhpmevent8",     any,    read_zero },
1722     [CSR_MHPMEVENT9]     = { "mhpmevent9",     any,    read_zero },
1723     [CSR_MHPMEVENT10]    = { "mhpmevent10",    any,    read_zero },
1724     [CSR_MHPMEVENT11]    = { "mhpmevent11",    any,    read_zero },
1725     [CSR_MHPMEVENT12]    = { "mhpmevent12",    any,    read_zero },
1726     [CSR_MHPMEVENT13]    = { "mhpmevent13",    any,    read_zero },
1727     [CSR_MHPMEVENT14]    = { "mhpmevent14",    any,    read_zero },
1728     [CSR_MHPMEVENT15]    = { "mhpmevent15",    any,    read_zero },
1729     [CSR_MHPMEVENT16]    = { "mhpmevent16",    any,    read_zero },
1730     [CSR_MHPMEVENT17]    = { "mhpmevent17",    any,    read_zero },
1731     [CSR_MHPMEVENT18]    = { "mhpmevent18",    any,    read_zero },
1732     [CSR_MHPMEVENT19]    = { "mhpmevent19",    any,    read_zero },
1733     [CSR_MHPMEVENT20]    = { "mhpmevent20",    any,    read_zero },
1734     [CSR_MHPMEVENT21]    = { "mhpmevent21",    any,    read_zero },
1735     [CSR_MHPMEVENT22]    = { "mhpmevent22",    any,    read_zero },
1736     [CSR_MHPMEVENT23]    = { "mhpmevent23",    any,    read_zero },
1737     [CSR_MHPMEVENT24]    = { "mhpmevent24",    any,    read_zero },
1738     [CSR_MHPMEVENT25]    = { "mhpmevent25",    any,    read_zero },
1739     [CSR_MHPMEVENT26]    = { "mhpmevent26",    any,    read_zero },
1740     [CSR_MHPMEVENT27]    = { "mhpmevent27",    any,    read_zero },
1741     [CSR_MHPMEVENT28]    = { "mhpmevent28",    any,    read_zero },
1742     [CSR_MHPMEVENT29]    = { "mhpmevent29",    any,    read_zero },
1743     [CSR_MHPMEVENT30]    = { "mhpmevent30",    any,    read_zero },
1744     [CSR_MHPMEVENT31]    = { "mhpmevent31",    any,    read_zero },
1745 
1746     [CSR_HPMCOUNTER3H]   = { "hpmcounter3h",   ctr32,  read_zero },
1747     [CSR_HPMCOUNTER4H]   = { "hpmcounter4h",   ctr32,  read_zero },
1748     [CSR_HPMCOUNTER5H]   = { "hpmcounter5h",   ctr32,  read_zero },
1749     [CSR_HPMCOUNTER6H]   = { "hpmcounter6h",   ctr32,  read_zero },
1750     [CSR_HPMCOUNTER7H]   = { "hpmcounter7h",   ctr32,  read_zero },
1751     [CSR_HPMCOUNTER8H]   = { "hpmcounter8h",   ctr32,  read_zero },
1752     [CSR_HPMCOUNTER9H]   = { "hpmcounter9h",   ctr32,  read_zero },
1753     [CSR_HPMCOUNTER10H]  = { "hpmcounter10h",  ctr32,  read_zero },
1754     [CSR_HPMCOUNTER11H]  = { "hpmcounter11h",  ctr32,  read_zero },
1755     [CSR_HPMCOUNTER12H]  = { "hpmcounter12h",  ctr32,  read_zero },
1756     [CSR_HPMCOUNTER13H]  = { "hpmcounter13h",  ctr32,  read_zero },
1757     [CSR_HPMCOUNTER14H]  = { "hpmcounter14h",  ctr32,  read_zero },
1758     [CSR_HPMCOUNTER15H]  = { "hpmcounter15h",  ctr32,  read_zero },
1759     [CSR_HPMCOUNTER16H]  = { "hpmcounter16h",  ctr32,  read_zero },
1760     [CSR_HPMCOUNTER17H]  = { "hpmcounter17h",  ctr32,  read_zero },
1761     [CSR_HPMCOUNTER18H]  = { "hpmcounter18h",  ctr32,  read_zero },
1762     [CSR_HPMCOUNTER19H]  = { "hpmcounter19h",  ctr32,  read_zero },
1763     [CSR_HPMCOUNTER20H]  = { "hpmcounter20h",  ctr32,  read_zero },
1764     [CSR_HPMCOUNTER21H]  = { "hpmcounter21h",  ctr32,  read_zero },
1765     [CSR_HPMCOUNTER22H]  = { "hpmcounter22h",  ctr32,  read_zero },
1766     [CSR_HPMCOUNTER23H]  = { "hpmcounter23h",  ctr32,  read_zero },
1767     [CSR_HPMCOUNTER24H]  = { "hpmcounter24h",  ctr32,  read_zero },
1768     [CSR_HPMCOUNTER25H]  = { "hpmcounter25h",  ctr32,  read_zero },
1769     [CSR_HPMCOUNTER26H]  = { "hpmcounter26h",  ctr32,  read_zero },
1770     [CSR_HPMCOUNTER27H]  = { "hpmcounter27h",  ctr32,  read_zero },
1771     [CSR_HPMCOUNTER28H]  = { "hpmcounter28h",  ctr32,  read_zero },
1772     [CSR_HPMCOUNTER29H]  = { "hpmcounter29h",  ctr32,  read_zero },
1773     [CSR_HPMCOUNTER30H]  = { "hpmcounter30h",  ctr32,  read_zero },
1774     [CSR_HPMCOUNTER31H]  = { "hpmcounter31h",  ctr32,  read_zero },
1775 
1776     [CSR_MHPMCOUNTER3H]  = { "mhpmcounter3h",  any32,  read_zero },
1777     [CSR_MHPMCOUNTER4H]  = { "mhpmcounter4h",  any32,  read_zero },
1778     [CSR_MHPMCOUNTER5H]  = { "mhpmcounter5h",  any32,  read_zero },
1779     [CSR_MHPMCOUNTER6H]  = { "mhpmcounter6h",  any32,  read_zero },
1780     [CSR_MHPMCOUNTER7H]  = { "mhpmcounter7h",  any32,  read_zero },
1781     [CSR_MHPMCOUNTER8H]  = { "mhpmcounter8h",  any32,  read_zero },
1782     [CSR_MHPMCOUNTER9H]  = { "mhpmcounter9h",  any32,  read_zero },
1783     [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", any32,  read_zero },
1784     [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", any32,  read_zero },
1785     [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", any32,  read_zero },
1786     [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", any32,  read_zero },
1787     [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", any32,  read_zero },
1788     [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", any32,  read_zero },
1789     [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", any32,  read_zero },
1790     [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", any32,  read_zero },
1791     [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", any32,  read_zero },
1792     [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", any32,  read_zero },
1793     [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", any32,  read_zero },
1794     [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", any32,  read_zero },
1795     [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", any32,  read_zero },
1796     [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", any32,  read_zero },
1797     [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", any32,  read_zero },
1798     [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", any32,  read_zero },
1799     [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", any32,  read_zero },
1800     [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", any32,  read_zero },
1801     [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", any32,  read_zero },
1802     [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", any32,  read_zero },
1803     [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", any32,  read_zero },
1804     [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", any32,  read_zero },
1805 #endif /* !CONFIG_USER_ONLY */
1806 };
1807