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