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