hw/registerfields: Prefix local variables with underscore in macros
[qemu/ar7.git] / target / riscv / csr.c
blob11d184cd164702e9257145da7d43697f5066a178
1 /*
2 * RISC-V Control and Status Registers.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017-2018 SiFive, Inc.
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.
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.
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/>.
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"
26 /* CSR function table */
27 static riscv_csr_operations csr_ops[];
29 /* CSR function table constants */
30 enum {
31 CSR_TABLE_SIZE = 0x1000
34 /* CSR function table public API */
35 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops)
37 *ops = csr_ops[csrno & (CSR_TABLE_SIZE - 1)];
40 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops)
42 csr_ops[csrno & (CSR_TABLE_SIZE - 1)] = *ops;
45 /* Predicates */
46 static int fs(CPURISCVState *env, int csrno)
48 #if !defined(CONFIG_USER_ONLY)
49 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
50 return -1;
52 #endif
53 return 0;
56 static int ctr(CPURISCVState *env, int csrno)
58 #if !defined(CONFIG_USER_ONLY)
59 CPUState *cs = env_cpu(env);
60 RISCVCPU *cpu = RISCV_CPU(cs);
61 uint32_t ctr_en = ~0u;
63 if (!cpu->cfg.ext_counters) {
64 /* The Counters extensions is not enabled */
65 return -1;
69 * The counters are always enabled at run time on newer priv specs, as the
70 * CSR has changed from controlling that the counters can be read to
71 * controlling that the counters increment.
73 if (env->priv_ver > PRIV_VERSION_1_09_1) {
74 return 0;
77 if (env->priv < PRV_M) {
78 ctr_en &= env->mcounteren;
80 if (env->priv < PRV_S) {
81 ctr_en &= env->scounteren;
83 if (!(ctr_en & (1u << (csrno & 31)))) {
84 return -1;
86 #endif
87 return 0;
90 #if !defined(CONFIG_USER_ONLY)
91 static int any(CPURISCVState *env, int csrno)
93 return 0;
96 static int smode(CPURISCVState *env, int csrno)
98 return -!riscv_has_ext(env, RVS);
101 static int hmode(CPURISCVState *env, int csrno)
103 if (riscv_has_ext(env, RVS) &&
104 riscv_has_ext(env, RVH)) {
105 /* Hypervisor extension is supported */
106 if ((env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) ||
107 env->priv == PRV_M) {
108 return 0;
112 return -1;
115 static int pmp(CPURISCVState *env, int csrno)
117 return -!riscv_feature(env, RISCV_FEATURE_PMP);
119 #endif
121 /* User Floating-Point CSRs */
122 static int read_fflags(CPURISCVState *env, int csrno, target_ulong *val)
124 #if !defined(CONFIG_USER_ONLY)
125 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
126 return -1;
128 #endif
129 *val = riscv_cpu_get_fflags(env);
130 return 0;
133 static int write_fflags(CPURISCVState *env, int csrno, target_ulong val)
135 #if !defined(CONFIG_USER_ONLY)
136 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
137 return -1;
139 env->mstatus |= MSTATUS_FS;
140 #endif
141 riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
142 return 0;
145 static int read_frm(CPURISCVState *env, int csrno, target_ulong *val)
147 #if !defined(CONFIG_USER_ONLY)
148 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
149 return -1;
151 #endif
152 *val = env->frm;
153 return 0;
156 static int write_frm(CPURISCVState *env, int csrno, target_ulong val)
158 #if !defined(CONFIG_USER_ONLY)
159 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
160 return -1;
162 env->mstatus |= MSTATUS_FS;
163 #endif
164 env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
165 return 0;
168 static int read_fcsr(CPURISCVState *env, int csrno, target_ulong *val)
170 #if !defined(CONFIG_USER_ONLY)
171 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
172 return -1;
174 #endif
175 *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
176 | (env->frm << FSR_RD_SHIFT);
177 return 0;
180 static int write_fcsr(CPURISCVState *env, int csrno, target_ulong val)
182 #if !defined(CONFIG_USER_ONLY)
183 if (!env->debugger && !riscv_cpu_fp_enabled(env)) {
184 return -1;
186 env->mstatus |= MSTATUS_FS;
187 #endif
188 env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
189 riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
190 return 0;
193 /* User Timers and Counters */
194 static int read_instret(CPURISCVState *env, int csrno, target_ulong *val)
196 #if !defined(CONFIG_USER_ONLY)
197 if (use_icount) {
198 *val = cpu_get_icount();
199 } else {
200 *val = cpu_get_host_ticks();
202 #else
203 *val = cpu_get_host_ticks();
204 #endif
205 return 0;
208 #if defined(TARGET_RISCV32)
209 static int read_instreth(CPURISCVState *env, int csrno, target_ulong *val)
211 #if !defined(CONFIG_USER_ONLY)
212 if (use_icount) {
213 *val = cpu_get_icount() >> 32;
214 } else {
215 *val = cpu_get_host_ticks() >> 32;
217 #else
218 *val = cpu_get_host_ticks() >> 32;
219 #endif
220 return 0;
222 #endif /* TARGET_RISCV32 */
224 #if defined(CONFIG_USER_ONLY)
225 static int read_time(CPURISCVState *env, int csrno, target_ulong *val)
227 *val = cpu_get_host_ticks();
228 return 0;
231 #if defined(TARGET_RISCV32)
232 static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val)
234 *val = cpu_get_host_ticks() >> 32;
235 return 0;
237 #endif
239 #else /* CONFIG_USER_ONLY */
241 static int read_time(CPURISCVState *env, int csrno, target_ulong *val)
243 uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
245 if (!env->rdtime_fn) {
246 return -1;
249 *val = env->rdtime_fn() + delta;
250 return 0;
253 #if defined(TARGET_RISCV32)
254 static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val)
256 uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0;
258 if (!env->rdtime_fn) {
259 return -1;
262 *val = (env->rdtime_fn() + delta) >> 32;
263 return 0;
265 #endif
267 /* Machine constants */
269 #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)
270 #define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)
271 #define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)
273 static const target_ulong delegable_ints = S_MODE_INTERRUPTS |
274 VS_MODE_INTERRUPTS;
275 static const target_ulong all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
276 VS_MODE_INTERRUPTS;
277 static const target_ulong delegable_excps =
278 (1ULL << (RISCV_EXCP_INST_ADDR_MIS)) |
279 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) |
280 (1ULL << (RISCV_EXCP_ILLEGAL_INST)) |
281 (1ULL << (RISCV_EXCP_BREAKPOINT)) |
282 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) |
283 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) |
284 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) |
285 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) |
286 (1ULL << (RISCV_EXCP_U_ECALL)) |
287 (1ULL << (RISCV_EXCP_S_ECALL)) |
288 (1ULL << (RISCV_EXCP_VS_ECALL)) |
289 (1ULL << (RISCV_EXCP_M_ECALL)) |
290 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) |
291 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) |
292 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) |
293 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) |
294 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) |
295 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT));
296 static const target_ulong sstatus_v1_9_mask = SSTATUS_SIE | SSTATUS_SPIE |
297 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
298 SSTATUS_SUM | SSTATUS_SD;
299 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
300 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
301 SSTATUS_SUM | SSTATUS_MXR | SSTATUS_SD;
302 static const target_ulong sip_writable_mask = SIP_SSIP | MIP_USIP | MIP_UEIP;
303 static const target_ulong hip_writable_mask = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP;
304 static const target_ulong vsip_writable_mask = MIP_VSSIP;
306 #if defined(TARGET_RISCV32)
307 static const char valid_vm_1_09[16] = {
308 [VM_1_09_MBARE] = 1,
309 [VM_1_09_SV32] = 1,
311 static const char valid_vm_1_10[16] = {
312 [VM_1_10_MBARE] = 1,
313 [VM_1_10_SV32] = 1
315 #elif defined(TARGET_RISCV64)
316 static const char valid_vm_1_09[16] = {
317 [VM_1_09_MBARE] = 1,
318 [VM_1_09_SV39] = 1,
319 [VM_1_09_SV48] = 1,
321 static const char valid_vm_1_10[16] = {
322 [VM_1_10_MBARE] = 1,
323 [VM_1_10_SV39] = 1,
324 [VM_1_10_SV48] = 1,
325 [VM_1_10_SV57] = 1
327 #endif /* CONFIG_USER_ONLY */
329 /* Machine Information Registers */
330 static int read_zero(CPURISCVState *env, int csrno, target_ulong *val)
332 return *val = 0;
335 static int read_mhartid(CPURISCVState *env, int csrno, target_ulong *val)
337 *val = env->mhartid;
338 return 0;
341 /* Machine Trap Setup */
342 static int read_mstatus(CPURISCVState *env, int csrno, target_ulong *val)
344 *val = env->mstatus;
345 return 0;
348 static int validate_vm(CPURISCVState *env, target_ulong vm)
350 return (env->priv_ver >= PRIV_VERSION_1_10_0) ?
351 valid_vm_1_10[vm & 0xf] : valid_vm_1_09[vm & 0xf];
354 static int write_mstatus(CPURISCVState *env, int csrno, target_ulong val)
356 target_ulong mstatus = env->mstatus;
357 target_ulong mask = 0;
358 int dirty;
360 /* flush tlb on mstatus fields that affect VM */
361 if (env->priv_ver <= PRIV_VERSION_1_09_1) {
362 if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP |
363 MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_VM)) {
364 tlb_flush(env_cpu(env));
366 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
367 MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM |
368 MSTATUS_MPP | MSTATUS_MXR |
369 (validate_vm(env, get_field(val, MSTATUS_VM)) ?
370 MSTATUS_VM : 0);
372 if (env->priv_ver >= PRIV_VERSION_1_10_0) {
373 if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
374 MSTATUS_MPRV | MSTATUS_SUM)) {
375 tlb_flush(env_cpu(env));
377 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
378 MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM |
379 MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
380 MSTATUS_TW;
381 #if defined(TARGET_RISCV64)
383 * RV32: MPV and MTL are not in mstatus. The current plan is to
384 * add them to mstatush. For now, we just don't support it.
386 mask |= MSTATUS_MTL | MSTATUS_MPV;
387 #endif
390 mstatus = (mstatus & ~mask) | (val & mask);
392 dirty = ((mstatus & MSTATUS_FS) == MSTATUS_FS) |
393 ((mstatus & MSTATUS_XS) == MSTATUS_XS);
394 mstatus = set_field(mstatus, MSTATUS_SD, dirty);
395 env->mstatus = mstatus;
397 return 0;
400 #ifdef TARGET_RISCV32
401 static int read_mstatush(CPURISCVState *env, int csrno, target_ulong *val)
403 *val = env->mstatush;
404 return 0;
407 static int write_mstatush(CPURISCVState *env, int csrno, target_ulong val)
409 if ((val ^ env->mstatush) & (MSTATUS_MPV)) {
410 tlb_flush(env_cpu(env));
413 val &= MSTATUS_MPV | MSTATUS_MTL;
415 env->mstatush = val;
417 return 0;
419 #endif
421 static int read_misa(CPURISCVState *env, int csrno, target_ulong *val)
423 *val = env->misa;
424 return 0;
427 static int write_misa(CPURISCVState *env, int csrno, target_ulong val)
429 if (!riscv_feature(env, RISCV_FEATURE_MISA)) {
430 /* drop write to misa */
431 return 0;
434 /* 'I' or 'E' must be present */
435 if (!(val & (RVI | RVE))) {
436 /* It is not, drop write to misa */
437 return 0;
440 /* 'E' excludes all other extensions */
441 if (val & RVE) {
442 /* when we support 'E' we can do "val = RVE;" however
443 * for now we just drop writes if 'E' is present.
445 return 0;
448 /* Mask extensions that are not supported by this hart */
449 val &= env->misa_mask;
451 /* Mask extensions that are not supported by QEMU */
452 val &= (RVI | RVE | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
454 /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
455 if ((val & RVD) && !(val & RVF)) {
456 val &= ~RVD;
459 /* Suppress 'C' if next instruction is not aligned
460 * TODO: this should check next_pc
462 if ((val & RVC) && (GETPC() & ~3) != 0) {
463 val &= ~RVC;
466 /* misa.MXL writes are not supported by QEMU */
467 val = (env->misa & MISA_MXL) | (val & ~MISA_MXL);
469 /* flush translation cache */
470 if (val != env->misa) {
471 tb_flush(env_cpu(env));
474 env->misa = val;
476 return 0;
479 static int read_medeleg(CPURISCVState *env, int csrno, target_ulong *val)
481 *val = env->medeleg;
482 return 0;
485 static int write_medeleg(CPURISCVState *env, int csrno, target_ulong val)
487 env->medeleg = (env->medeleg & ~delegable_excps) | (val & delegable_excps);
488 return 0;
491 static int read_mideleg(CPURISCVState *env, int csrno, target_ulong *val)
493 *val = env->mideleg;
494 return 0;
497 static int write_mideleg(CPURISCVState *env, int csrno, target_ulong val)
499 env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints);
500 if (riscv_has_ext(env, RVH)) {
501 env->mideleg |= VS_MODE_INTERRUPTS;
503 return 0;
506 static int read_mie(CPURISCVState *env, int csrno, target_ulong *val)
508 *val = env->mie;
509 return 0;
512 static int write_mie(CPURISCVState *env, int csrno, target_ulong val)
514 env->mie = (env->mie & ~all_ints) | (val & all_ints);
515 return 0;
518 static int read_mtvec(CPURISCVState *env, int csrno, target_ulong *val)
520 *val = env->mtvec;
521 return 0;
524 static int write_mtvec(CPURISCVState *env, int csrno, target_ulong val)
526 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
527 if ((val & 3) < 2) {
528 env->mtvec = val;
529 } else {
530 qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n");
532 return 0;
535 static int read_mcounteren(CPURISCVState *env, int csrno, target_ulong *val)
537 if (env->priv_ver < PRIV_VERSION_1_10_0) {
538 return -1;
540 *val = env->mcounteren;
541 return 0;
544 static int write_mcounteren(CPURISCVState *env, int csrno, target_ulong val)
546 if (env->priv_ver < PRIV_VERSION_1_10_0) {
547 return -1;
549 env->mcounteren = val;
550 return 0;
553 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
554 static int read_mscounteren(CPURISCVState *env, int csrno, target_ulong *val)
556 if (env->priv_ver > PRIV_VERSION_1_09_1
557 && env->priv_ver < PRIV_VERSION_1_11_0) {
558 return -1;
560 *val = env->mcounteren;
561 return 0;
564 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
565 static int write_mscounteren(CPURISCVState *env, int csrno, target_ulong val)
567 if (env->priv_ver > PRIV_VERSION_1_09_1
568 && env->priv_ver < PRIV_VERSION_1_11_0) {
569 return -1;
571 env->mcounteren = val;
572 return 0;
575 static int read_mucounteren(CPURISCVState *env, int csrno, target_ulong *val)
577 if (env->priv_ver > PRIV_VERSION_1_09_1) {
578 return -1;
580 *val = env->scounteren;
581 return 0;
584 static int write_mucounteren(CPURISCVState *env, int csrno, target_ulong val)
586 if (env->priv_ver > PRIV_VERSION_1_09_1) {
587 return -1;
589 env->scounteren = val;
590 return 0;
593 /* Machine Trap Handling */
594 static int read_mscratch(CPURISCVState *env, int csrno, target_ulong *val)
596 *val = env->mscratch;
597 return 0;
600 static int write_mscratch(CPURISCVState *env, int csrno, target_ulong val)
602 env->mscratch = val;
603 return 0;
606 static int read_mepc(CPURISCVState *env, int csrno, target_ulong *val)
608 *val = env->mepc;
609 return 0;
612 static int write_mepc(CPURISCVState *env, int csrno, target_ulong val)
614 env->mepc = val;
615 return 0;
618 static int read_mcause(CPURISCVState *env, int csrno, target_ulong *val)
620 *val = env->mcause;
621 return 0;
624 static int write_mcause(CPURISCVState *env, int csrno, target_ulong val)
626 env->mcause = val;
627 return 0;
630 static int read_mbadaddr(CPURISCVState *env, int csrno, target_ulong *val)
632 *val = env->mbadaddr;
633 return 0;
636 static int write_mbadaddr(CPURISCVState *env, int csrno, target_ulong val)
638 env->mbadaddr = val;
639 return 0;
642 static int rmw_mip(CPURISCVState *env, int csrno, target_ulong *ret_value,
643 target_ulong new_value, target_ulong write_mask)
645 RISCVCPU *cpu = env_archcpu(env);
646 /* Allow software control of delegable interrupts not claimed by hardware */
647 target_ulong mask = write_mask & delegable_ints & ~env->miclaim;
648 uint32_t old_mip;
650 if (mask) {
651 old_mip = riscv_cpu_update_mip(cpu, mask, (new_value & mask));
652 } else {
653 old_mip = env->mip;
656 if (ret_value) {
657 *ret_value = old_mip;
660 return 0;
663 /* Supervisor Trap Setup */
664 static int read_sstatus(CPURISCVState *env, int csrno, target_ulong *val)
666 target_ulong mask = ((env->priv_ver >= PRIV_VERSION_1_10_0) ?
667 sstatus_v1_10_mask : sstatus_v1_9_mask);
668 *val = env->mstatus & mask;
669 return 0;
672 static int write_sstatus(CPURISCVState *env, int csrno, target_ulong val)
674 target_ulong mask = ((env->priv_ver >= PRIV_VERSION_1_10_0) ?
675 sstatus_v1_10_mask : sstatus_v1_9_mask);
676 target_ulong newval = (env->mstatus & ~mask) | (val & mask);
677 return write_mstatus(env, CSR_MSTATUS, newval);
680 static int read_sie(CPURISCVState *env, int csrno, target_ulong *val)
682 if (riscv_cpu_virt_enabled(env)) {
683 /* Tell the guest the VS bits, shifted to the S bit locations */
684 *val = (env->mie & env->mideleg & VS_MODE_INTERRUPTS) >> 1;
685 } else {
686 *val = env->mie & env->mideleg;
688 return 0;
691 static int write_sie(CPURISCVState *env, int csrno, target_ulong val)
693 target_ulong newval;
695 if (riscv_cpu_virt_enabled(env)) {
696 /* Shift the guests S bits to VS */
697 newval = (env->mie & ~VS_MODE_INTERRUPTS) |
698 ((val << 1) & VS_MODE_INTERRUPTS);
699 } else {
700 newval = (env->mie & ~S_MODE_INTERRUPTS) | (val & S_MODE_INTERRUPTS);
703 return write_mie(env, CSR_MIE, newval);
706 static int read_stvec(CPURISCVState *env, int csrno, target_ulong *val)
708 *val = env->stvec;
709 return 0;
712 static int write_stvec(CPURISCVState *env, int csrno, target_ulong val)
714 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
715 if ((val & 3) < 2) {
716 env->stvec = val;
717 } else {
718 qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n");
720 return 0;
723 static int read_scounteren(CPURISCVState *env, int csrno, target_ulong *val)
725 if (env->priv_ver < PRIV_VERSION_1_10_0) {
726 return -1;
728 *val = env->scounteren;
729 return 0;
732 static int write_scounteren(CPURISCVState *env, int csrno, target_ulong val)
734 if (env->priv_ver < PRIV_VERSION_1_10_0) {
735 return -1;
737 env->scounteren = val;
738 return 0;
741 /* Supervisor Trap Handling */
742 static int read_sscratch(CPURISCVState *env, int csrno, target_ulong *val)
744 *val = env->sscratch;
745 return 0;
748 static int write_sscratch(CPURISCVState *env, int csrno, target_ulong val)
750 env->sscratch = val;
751 return 0;
754 static int read_sepc(CPURISCVState *env, int csrno, target_ulong *val)
756 *val = env->sepc;
757 return 0;
760 static int write_sepc(CPURISCVState *env, int csrno, target_ulong val)
762 env->sepc = val;
763 return 0;
766 static int read_scause(CPURISCVState *env, int csrno, target_ulong *val)
768 *val = env->scause;
769 return 0;
772 static int write_scause(CPURISCVState *env, int csrno, target_ulong val)
774 env->scause = val;
775 return 0;
778 static int read_sbadaddr(CPURISCVState *env, int csrno, target_ulong *val)
780 *val = env->sbadaddr;
781 return 0;
784 static int write_sbadaddr(CPURISCVState *env, int csrno, target_ulong val)
786 env->sbadaddr = val;
787 return 0;
790 static int rmw_sip(CPURISCVState *env, int csrno, target_ulong *ret_value,
791 target_ulong new_value, target_ulong write_mask)
793 int ret;
795 if (riscv_cpu_virt_enabled(env)) {
796 /* Shift the new values to line up with the VS bits */
797 ret = rmw_mip(env, CSR_MSTATUS, ret_value, new_value << 1,
798 (write_mask & sip_writable_mask) << 1 & env->mideleg);
799 ret &= vsip_writable_mask;
800 ret >>= 1;
801 } else {
802 ret = rmw_mip(env, CSR_MSTATUS, ret_value, new_value,
803 write_mask & env->mideleg & sip_writable_mask);
806 *ret_value &= env->mideleg;
807 return ret;
810 /* Supervisor Protection and Translation */
811 static int read_satp(CPURISCVState *env, int csrno, target_ulong *val)
813 if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
814 *val = 0;
815 } else if (env->priv_ver >= PRIV_VERSION_1_10_0) {
816 if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
817 return -1;
818 } else {
819 *val = env->satp;
821 } else {
822 *val = env->sptbr;
824 return 0;
827 static int write_satp(CPURISCVState *env, int csrno, target_ulong val)
829 if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
830 return 0;
832 if (env->priv_ver <= PRIV_VERSION_1_09_1 && (val ^ env->sptbr)) {
833 tlb_flush(env_cpu(env));
834 env->sptbr = val & (((target_ulong)
835 1 << (TARGET_PHYS_ADDR_SPACE_BITS - PGSHIFT)) - 1);
837 if (env->priv_ver >= PRIV_VERSION_1_10_0 &&
838 validate_vm(env, get_field(val, SATP_MODE)) &&
839 ((val ^ env->satp) & (SATP_MODE | SATP_ASID | SATP_PPN)))
841 if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
842 return -1;
843 } else {
844 if((val ^ env->satp) & SATP_ASID) {
845 tlb_flush(env_cpu(env));
847 env->satp = val;
850 return 0;
853 /* Hypervisor Extensions */
854 static int read_hstatus(CPURISCVState *env, int csrno, target_ulong *val)
856 *val = env->hstatus;
857 return 0;
860 static int write_hstatus(CPURISCVState *env, int csrno, target_ulong val)
862 env->hstatus = val;
863 return 0;
866 static int read_hedeleg(CPURISCVState *env, int csrno, target_ulong *val)
868 *val = env->hedeleg;
869 return 0;
872 static int write_hedeleg(CPURISCVState *env, int csrno, target_ulong val)
874 env->hedeleg = val;
875 return 0;
878 static int read_hideleg(CPURISCVState *env, int csrno, target_ulong *val)
880 *val = env->hideleg;
881 return 0;
884 static int write_hideleg(CPURISCVState *env, int csrno, target_ulong val)
886 env->hideleg = val;
887 return 0;
890 static int rmw_hip(CPURISCVState *env, int csrno, target_ulong *ret_value,
891 target_ulong new_value, target_ulong write_mask)
893 int ret = rmw_mip(env, 0, ret_value, new_value,
894 write_mask & hip_writable_mask);
896 return ret;
899 static int read_hie(CPURISCVState *env, int csrno, target_ulong *val)
901 *val = env->mie & VS_MODE_INTERRUPTS;
902 return 0;
905 static int write_hie(CPURISCVState *env, int csrno, target_ulong val)
907 target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS) | (val & VS_MODE_INTERRUPTS);
908 return write_mie(env, CSR_MIE, newval);
911 static int read_hcounteren(CPURISCVState *env, int csrno, target_ulong *val)
913 *val = env->hcounteren;
914 return 0;
917 static int write_hcounteren(CPURISCVState *env, int csrno, target_ulong val)
919 env->hcounteren = val;
920 return 0;
923 static int read_htval(CPURISCVState *env, int csrno, target_ulong *val)
925 *val = env->htval;
926 return 0;
929 static int write_htval(CPURISCVState *env, int csrno, target_ulong val)
931 env->htval = val;
932 return 0;
935 static int read_htinst(CPURISCVState *env, int csrno, target_ulong *val)
937 *val = env->htinst;
938 return 0;
941 static int write_htinst(CPURISCVState *env, int csrno, target_ulong val)
943 env->htinst = val;
944 return 0;
947 static int read_hgatp(CPURISCVState *env, int csrno, target_ulong *val)
949 *val = env->hgatp;
950 return 0;
953 static int write_hgatp(CPURISCVState *env, int csrno, target_ulong val)
955 env->hgatp = val;
956 return 0;
959 static int read_htimedelta(CPURISCVState *env, int csrno, target_ulong *val)
961 if (!env->rdtime_fn) {
962 return -1;
965 #if defined(TARGET_RISCV32)
966 *val = env->htimedelta & 0xffffffff;
967 #else
968 *val = env->htimedelta;
969 #endif
970 return 0;
973 static int write_htimedelta(CPURISCVState *env, int csrno, target_ulong val)
975 if (!env->rdtime_fn) {
976 return -1;
979 #if defined(TARGET_RISCV32)
980 env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val);
981 #else
982 env->htimedelta = val;
983 #endif
984 return 0;
987 #if defined(TARGET_RISCV32)
988 static int read_htimedeltah(CPURISCVState *env, int csrno, target_ulong *val)
990 if (!env->rdtime_fn) {
991 return -1;
994 *val = env->htimedelta >> 32;
995 return 0;
998 static int write_htimedeltah(CPURISCVState *env, int csrno, target_ulong val)
1000 if (!env->rdtime_fn) {
1001 return -1;
1004 env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val);
1005 return 0;
1007 #endif
1009 /* Virtual CSR Registers */
1010 static int read_vsstatus(CPURISCVState *env, int csrno, target_ulong *val)
1012 *val = env->vsstatus;
1013 return 0;
1016 static int write_vsstatus(CPURISCVState *env, int csrno, target_ulong val)
1018 env->vsstatus = val;
1019 return 0;
1022 static int rmw_vsip(CPURISCVState *env, int csrno, target_ulong *ret_value,
1023 target_ulong new_value, target_ulong write_mask)
1025 int ret = rmw_mip(env, 0, ret_value, new_value,
1026 write_mask & env->mideleg & vsip_writable_mask);
1027 return ret;
1030 static int read_vsie(CPURISCVState *env, int csrno, target_ulong *val)
1032 *val = env->mie & env->mideleg & VS_MODE_INTERRUPTS;
1033 return 0;
1036 static int write_vsie(CPURISCVState *env, int csrno, target_ulong val)
1038 target_ulong newval = (env->mie & ~env->mideleg) | (val & env->mideleg & MIP_VSSIP);
1039 return write_mie(env, CSR_MIE, newval);
1042 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val)
1044 *val = env->vstvec;
1045 return 0;
1048 static int write_vstvec(CPURISCVState *env, int csrno, target_ulong val)
1050 env->vstvec = val;
1051 return 0;
1054 static int read_vsscratch(CPURISCVState *env, int csrno, target_ulong *val)
1056 *val = env->vsscratch;
1057 return 0;
1060 static int write_vsscratch(CPURISCVState *env, int csrno, target_ulong val)
1062 env->vsscratch = val;
1063 return 0;
1066 static int read_vsepc(CPURISCVState *env, int csrno, target_ulong *val)
1068 *val = env->vsepc;
1069 return 0;
1072 static int write_vsepc(CPURISCVState *env, int csrno, target_ulong val)
1074 env->vsepc = val;
1075 return 0;
1078 static int read_vscause(CPURISCVState *env, int csrno, target_ulong *val)
1080 *val = env->vscause;
1081 return 0;
1084 static int write_vscause(CPURISCVState *env, int csrno, target_ulong val)
1086 env->vscause = val;
1087 return 0;
1090 static int read_vstval(CPURISCVState *env, int csrno, target_ulong *val)
1092 *val = env->vstval;
1093 return 0;
1096 static int write_vstval(CPURISCVState *env, int csrno, target_ulong val)
1098 env->vstval = val;
1099 return 0;
1102 static int read_vsatp(CPURISCVState *env, int csrno, target_ulong *val)
1104 *val = env->vsatp;
1105 return 0;
1108 static int write_vsatp(CPURISCVState *env, int csrno, target_ulong val)
1110 env->vsatp = val;
1111 return 0;
1114 static int read_mtval2(CPURISCVState *env, int csrno, target_ulong *val)
1116 *val = env->mtval2;
1117 return 0;
1120 static int write_mtval2(CPURISCVState *env, int csrno, target_ulong val)
1122 env->mtval2 = val;
1123 return 0;
1126 static int read_mtinst(CPURISCVState *env, int csrno, target_ulong *val)
1128 *val = env->mtinst;
1129 return 0;
1132 static int write_mtinst(CPURISCVState *env, int csrno, target_ulong val)
1134 env->mtinst = val;
1135 return 0;
1138 /* Physical Memory Protection */
1139 static int read_pmpcfg(CPURISCVState *env, int csrno, target_ulong *val)
1141 *val = pmpcfg_csr_read(env, csrno - CSR_PMPCFG0);
1142 return 0;
1145 static int write_pmpcfg(CPURISCVState *env, int csrno, target_ulong val)
1147 pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val);
1148 return 0;
1151 static int read_pmpaddr(CPURISCVState *env, int csrno, target_ulong *val)
1153 *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
1154 return 0;
1157 static int write_pmpaddr(CPURISCVState *env, int csrno, target_ulong val)
1159 pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
1160 return 0;
1163 #endif
1166 * riscv_csrrw - read and/or update control and status register
1168 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0);
1169 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1);
1170 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value);
1171 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value);
1174 int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value,
1175 target_ulong new_value, target_ulong write_mask)
1177 int ret;
1178 target_ulong old_value;
1179 RISCVCPU *cpu = env_archcpu(env);
1181 /* check privileges and return -1 if check fails */
1182 #if !defined(CONFIG_USER_ONLY)
1183 int effective_priv = env->priv;
1184 int read_only = get_field(csrno, 0xC00) == 3;
1186 if (riscv_has_ext(env, RVH) &&
1187 env->priv == PRV_S &&
1188 !riscv_cpu_virt_enabled(env)) {
1190 * We are in S mode without virtualisation, therefore we are in HS Mode.
1191 * Add 1 to the effective privledge level to allow us to access the
1192 * Hypervisor CSRs.
1194 effective_priv++;
1197 if ((write_mask && read_only) ||
1198 (!env->debugger && (effective_priv < get_field(csrno, 0x300)))) {
1199 return -1;
1201 #endif
1203 /* ensure the CSR extension is enabled. */
1204 if (!cpu->cfg.ext_icsr) {
1205 return -1;
1208 /* check predicate */
1209 if (!csr_ops[csrno].predicate || csr_ops[csrno].predicate(env, csrno) < 0) {
1210 return -1;
1213 /* execute combined read/write operation if it exists */
1214 if (csr_ops[csrno].op) {
1215 return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
1218 /* if no accessor exists then return failure */
1219 if (!csr_ops[csrno].read) {
1220 return -1;
1223 /* read old value */
1224 ret = csr_ops[csrno].read(env, csrno, &old_value);
1225 if (ret < 0) {
1226 return ret;
1229 /* write value if writable and write mask set, otherwise drop writes */
1230 if (write_mask) {
1231 new_value = (old_value & ~write_mask) | (new_value & write_mask);
1232 if (csr_ops[csrno].write) {
1233 ret = csr_ops[csrno].write(env, csrno, new_value);
1234 if (ret < 0) {
1235 return ret;
1240 /* return old value */
1241 if (ret_value) {
1242 *ret_value = old_value;
1245 return 0;
1249 * Debugger support. If not in user mode, set env->debugger before the
1250 * riscv_csrrw call and clear it after the call.
1252 int riscv_csrrw_debug(CPURISCVState *env, int csrno, target_ulong *ret_value,
1253 target_ulong new_value, target_ulong write_mask)
1255 int ret;
1256 #if !defined(CONFIG_USER_ONLY)
1257 env->debugger = true;
1258 #endif
1259 ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask);
1260 #if !defined(CONFIG_USER_ONLY)
1261 env->debugger = false;
1262 #endif
1263 return ret;
1266 /* Control and Status Register function table */
1267 static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
1268 /* User Floating-Point CSRs */
1269 [CSR_FFLAGS] = { fs, read_fflags, write_fflags },
1270 [CSR_FRM] = { fs, read_frm, write_frm },
1271 [CSR_FCSR] = { fs, read_fcsr, write_fcsr },
1273 /* User Timers and Counters */
1274 [CSR_CYCLE] = { ctr, read_instret },
1275 [CSR_INSTRET] = { ctr, read_instret },
1276 #if defined(TARGET_RISCV32)
1277 [CSR_CYCLEH] = { ctr, read_instreth },
1278 [CSR_INSTRETH] = { ctr, read_instreth },
1279 #endif
1281 /* In privileged mode, the monitor will have to emulate TIME CSRs only if
1282 * rdtime callback is not provided by machine/platform emulation */
1283 [CSR_TIME] = { ctr, read_time },
1284 #if defined(TARGET_RISCV32)
1285 [CSR_TIMEH] = { ctr, read_timeh },
1286 #endif
1288 #if !defined(CONFIG_USER_ONLY)
1289 /* Machine Timers and Counters */
1290 [CSR_MCYCLE] = { any, read_instret },
1291 [CSR_MINSTRET] = { any, read_instret },
1292 #if defined(TARGET_RISCV32)
1293 [CSR_MCYCLEH] = { any, read_instreth },
1294 [CSR_MINSTRETH] = { any, read_instreth },
1295 #endif
1297 /* Machine Information Registers */
1298 [CSR_MVENDORID] = { any, read_zero },
1299 [CSR_MARCHID] = { any, read_zero },
1300 [CSR_MIMPID] = { any, read_zero },
1301 [CSR_MHARTID] = { any, read_mhartid },
1303 /* Machine Trap Setup */
1304 [CSR_MSTATUS] = { any, read_mstatus, write_mstatus },
1305 [CSR_MISA] = { any, read_misa, write_misa },
1306 [CSR_MIDELEG] = { any, read_mideleg, write_mideleg },
1307 [CSR_MEDELEG] = { any, read_medeleg, write_medeleg },
1308 [CSR_MIE] = { any, read_mie, write_mie },
1309 [CSR_MTVEC] = { any, read_mtvec, write_mtvec },
1310 [CSR_MCOUNTEREN] = { any, read_mcounteren, write_mcounteren },
1312 #if defined(TARGET_RISCV32)
1313 [CSR_MSTATUSH] = { any, read_mstatush, write_mstatush },
1314 #endif
1316 /* Legacy Counter Setup (priv v1.9.1) */
1317 [CSR_MUCOUNTEREN] = { any, read_mucounteren, write_mucounteren },
1318 [CSR_MSCOUNTEREN] = { any, read_mscounteren, write_mscounteren },
1320 /* Machine Trap Handling */
1321 [CSR_MSCRATCH] = { any, read_mscratch, write_mscratch },
1322 [CSR_MEPC] = { any, read_mepc, write_mepc },
1323 [CSR_MCAUSE] = { any, read_mcause, write_mcause },
1324 [CSR_MBADADDR] = { any, read_mbadaddr, write_mbadaddr },
1325 [CSR_MIP] = { any, NULL, NULL, rmw_mip },
1327 /* Supervisor Trap Setup */
1328 [CSR_SSTATUS] = { smode, read_sstatus, write_sstatus },
1329 [CSR_SIE] = { smode, read_sie, write_sie },
1330 [CSR_STVEC] = { smode, read_stvec, write_stvec },
1331 [CSR_SCOUNTEREN] = { smode, read_scounteren, write_scounteren },
1333 /* Supervisor Trap Handling */
1334 [CSR_SSCRATCH] = { smode, read_sscratch, write_sscratch },
1335 [CSR_SEPC] = { smode, read_sepc, write_sepc },
1336 [CSR_SCAUSE] = { smode, read_scause, write_scause },
1337 [CSR_SBADADDR] = { smode, read_sbadaddr, write_sbadaddr },
1338 [CSR_SIP] = { smode, NULL, NULL, rmw_sip },
1340 /* Supervisor Protection and Translation */
1341 [CSR_SATP] = { smode, read_satp, write_satp },
1343 [CSR_HSTATUS] = { hmode, read_hstatus, write_hstatus },
1344 [CSR_HEDELEG] = { hmode, read_hedeleg, write_hedeleg },
1345 [CSR_HIDELEG] = { hmode, read_hideleg, write_hideleg },
1346 [CSR_HIP] = { hmode, NULL, NULL, rmw_hip },
1347 [CSR_HIE] = { hmode, read_hie, write_hie },
1348 [CSR_HCOUNTEREN] = { hmode, read_hcounteren, write_hcounteren },
1349 [CSR_HTVAL] = { hmode, read_htval, write_htval },
1350 [CSR_HTINST] = { hmode, read_htinst, write_htinst },
1351 [CSR_HGATP] = { hmode, read_hgatp, write_hgatp },
1352 [CSR_HTIMEDELTA] = { hmode, read_htimedelta, write_htimedelta },
1353 #if defined(TARGET_RISCV32)
1354 [CSR_HTIMEDELTAH] = { hmode, read_htimedeltah, write_htimedeltah},
1355 #endif
1357 [CSR_VSSTATUS] = { hmode, read_vsstatus, write_vsstatus },
1358 [CSR_VSIP] = { hmode, NULL, NULL, rmw_vsip },
1359 [CSR_VSIE] = { hmode, read_vsie, write_vsie },
1360 [CSR_VSTVEC] = { hmode, read_vstvec, write_vstvec },
1361 [CSR_VSSCRATCH] = { hmode, read_vsscratch, write_vsscratch },
1362 [CSR_VSEPC] = { hmode, read_vsepc, write_vsepc },
1363 [CSR_VSCAUSE] = { hmode, read_vscause, write_vscause },
1364 [CSR_VSTVAL] = { hmode, read_vstval, write_vstval },
1365 [CSR_VSATP] = { hmode, read_vsatp, write_vsatp },
1367 [CSR_MTVAL2] = { hmode, read_mtval2, write_mtval2 },
1368 [CSR_MTINST] = { hmode, read_mtinst, write_mtinst },
1370 /* Physical Memory Protection */
1371 [CSR_PMPCFG0 ... CSR_PMPADDR9] = { pmp, read_pmpcfg, write_pmpcfg },
1372 [CSR_PMPADDR0 ... CSR_PMPADDR15] = { pmp, read_pmpaddr, write_pmpaddr },
1374 /* Performance Counters */
1375 [CSR_HPMCOUNTER3 ... CSR_HPMCOUNTER31] = { ctr, read_zero },
1376 [CSR_MHPMCOUNTER3 ... CSR_MHPMCOUNTER31] = { any, read_zero },
1377 [CSR_MHPMEVENT3 ... CSR_MHPMEVENT31] = { any, read_zero },
1378 #if defined(TARGET_RISCV32)
1379 [CSR_HPMCOUNTER3H ... CSR_HPMCOUNTER31H] = { ctr, read_zero },
1380 [CSR_MHPMCOUNTER3H ... CSR_MHPMCOUNTER31H] = { any, read_zero },
1381 #endif
1382 #endif /* !CONFIG_USER_ONLY */