target/ppc: Rename PATB/PATBE -> PATE
[qemu/kevin.git] / target / riscv / csr.c
blob960d2b0aa951f7ef7f15be31333663f8b0a1b046
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->mstatus & MSTATUS_FS)) {
50 return -1;
52 #endif
53 return 0;
56 static int ctr(CPURISCVState *env, int csrno)
58 #if !defined(CONFIG_USER_ONLY)
59 uint32_t ctr_en = ~0u;
61 if (env->priv < PRV_M) {
62 ctr_en &= env->mcounteren;
64 if (env->priv < PRV_S) {
65 ctr_en &= env->scounteren;
67 if (!(ctr_en & (1u << (csrno & 31)))) {
68 return -1;
70 #endif
71 return 0;
74 #if !defined(CONFIG_USER_ONLY)
75 static int any(CPURISCVState *env, int csrno)
77 return 0;
80 static int smode(CPURISCVState *env, int csrno)
82 return -!riscv_has_ext(env, RVS);
85 static int pmp(CPURISCVState *env, int csrno)
87 return -!riscv_feature(env, RISCV_FEATURE_PMP);
89 #endif
91 /* User Floating-Point CSRs */
92 static int read_fflags(CPURISCVState *env, int csrno, target_ulong *val)
94 #if !defined(CONFIG_USER_ONLY)
95 if (!(env->mstatus & MSTATUS_FS)) {
96 return -1;
98 #endif
99 *val = riscv_cpu_get_fflags(env);
100 return 0;
103 static int write_fflags(CPURISCVState *env, int csrno, target_ulong val)
105 #if !defined(CONFIG_USER_ONLY)
106 if (!(env->mstatus & MSTATUS_FS)) {
107 return -1;
109 env->mstatus |= MSTATUS_FS;
110 #endif
111 riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT));
112 return 0;
115 static int read_frm(CPURISCVState *env, int csrno, target_ulong *val)
117 #if !defined(CONFIG_USER_ONLY)
118 if (!(env->mstatus & MSTATUS_FS)) {
119 return -1;
121 #endif
122 *val = env->frm;
123 return 0;
126 static int write_frm(CPURISCVState *env, int csrno, target_ulong val)
128 #if !defined(CONFIG_USER_ONLY)
129 if (!(env->mstatus & MSTATUS_FS)) {
130 return -1;
132 env->mstatus |= MSTATUS_FS;
133 #endif
134 env->frm = val & (FSR_RD >> FSR_RD_SHIFT);
135 return 0;
138 static int read_fcsr(CPURISCVState *env, int csrno, target_ulong *val)
140 #if !defined(CONFIG_USER_ONLY)
141 if (!(env->mstatus & MSTATUS_FS)) {
142 return -1;
144 #endif
145 *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT)
146 | (env->frm << FSR_RD_SHIFT);
147 return 0;
150 static int write_fcsr(CPURISCVState *env, int csrno, target_ulong val)
152 #if !defined(CONFIG_USER_ONLY)
153 if (!(env->mstatus & MSTATUS_FS)) {
154 return -1;
156 env->mstatus |= MSTATUS_FS;
157 #endif
158 env->frm = (val & FSR_RD) >> FSR_RD_SHIFT;
159 riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT);
160 return 0;
163 /* User Timers and Counters */
164 static int read_instret(CPURISCVState *env, int csrno, target_ulong *val)
166 #if !defined(CONFIG_USER_ONLY)
167 if (use_icount) {
168 *val = cpu_get_icount();
169 } else {
170 *val = cpu_get_host_ticks();
172 #else
173 *val = cpu_get_host_ticks();
174 #endif
175 return 0;
178 #if defined(TARGET_RISCV32)
179 static int read_instreth(CPURISCVState *env, int csrno, target_ulong *val)
181 #if !defined(CONFIG_USER_ONLY)
182 if (use_icount) {
183 *val = cpu_get_icount() >> 32;
184 } else {
185 *val = cpu_get_host_ticks() >> 32;
187 #else
188 *val = cpu_get_host_ticks() >> 32;
189 #endif
190 return 0;
192 #endif /* TARGET_RISCV32 */
194 #if defined(CONFIG_USER_ONLY)
195 static int read_time(CPURISCVState *env, int csrno, target_ulong *val)
197 *val = cpu_get_host_ticks();
198 return 0;
201 #if defined(TARGET_RISCV32)
202 static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val)
204 *val = cpu_get_host_ticks() >> 32;
205 return 0;
207 #endif
209 #else /* CONFIG_USER_ONLY */
211 /* Machine constants */
213 #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)
214 #define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)
216 static const target_ulong delegable_ints = S_MODE_INTERRUPTS;
217 static const target_ulong all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS;
218 static const target_ulong delegable_excps =
219 (1ULL << (RISCV_EXCP_INST_ADDR_MIS)) |
220 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) |
221 (1ULL << (RISCV_EXCP_ILLEGAL_INST)) |
222 (1ULL << (RISCV_EXCP_BREAKPOINT)) |
223 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) |
224 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) |
225 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) |
226 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) |
227 (1ULL << (RISCV_EXCP_U_ECALL)) |
228 (1ULL << (RISCV_EXCP_S_ECALL)) |
229 (1ULL << (RISCV_EXCP_H_ECALL)) |
230 (1ULL << (RISCV_EXCP_M_ECALL)) |
231 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) |
232 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) |
233 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT));
234 static const target_ulong sstatus_v1_9_mask = SSTATUS_SIE | SSTATUS_SPIE |
235 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
236 SSTATUS_SUM | SSTATUS_SD;
237 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE |
238 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS |
239 SSTATUS_SUM | SSTATUS_MXR | SSTATUS_SD;
241 #if defined(TARGET_RISCV32)
242 static const char valid_vm_1_09[16] = {
243 [VM_1_09_MBARE] = 1,
244 [VM_1_09_SV32] = 1,
246 static const char valid_vm_1_10[16] = {
247 [VM_1_10_MBARE] = 1,
248 [VM_1_10_SV32] = 1
250 #elif defined(TARGET_RISCV64)
251 static const char valid_vm_1_09[16] = {
252 [VM_1_09_MBARE] = 1,
253 [VM_1_09_SV39] = 1,
254 [VM_1_09_SV48] = 1,
256 static const char valid_vm_1_10[16] = {
257 [VM_1_10_MBARE] = 1,
258 [VM_1_10_SV39] = 1,
259 [VM_1_10_SV48] = 1,
260 [VM_1_10_SV57] = 1
262 #endif /* CONFIG_USER_ONLY */
264 /* Machine Information Registers */
265 static int read_zero(CPURISCVState *env, int csrno, target_ulong *val)
267 return *val = 0;
270 static int read_mhartid(CPURISCVState *env, int csrno, target_ulong *val)
272 *val = env->mhartid;
273 return 0;
276 /* Machine Trap Setup */
277 static int read_mstatus(CPURISCVState *env, int csrno, target_ulong *val)
279 *val = env->mstatus;
280 return 0;
283 static int validate_vm(CPURISCVState *env, target_ulong vm)
285 return (env->priv_ver >= PRIV_VERSION_1_10_0) ?
286 valid_vm_1_10[vm & 0xf] : valid_vm_1_09[vm & 0xf];
289 static int write_mstatus(CPURISCVState *env, int csrno, target_ulong val)
291 target_ulong mstatus = env->mstatus;
292 target_ulong mask = 0;
293 target_ulong mpp = get_field(val, MSTATUS_MPP);
295 /* flush tlb on mstatus fields that affect VM */
296 if (env->priv_ver <= PRIV_VERSION_1_09_1) {
297 if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP |
298 MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_VM)) {
299 tlb_flush(CPU(riscv_env_get_cpu(env)));
301 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
302 MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM |
303 MSTATUS_MPP | MSTATUS_MXR |
304 (validate_vm(env, get_field(val, MSTATUS_VM)) ?
305 MSTATUS_VM : 0);
307 if (env->priv_ver >= PRIV_VERSION_1_10_0) {
308 if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP |
309 MSTATUS_MPRV | MSTATUS_SUM)) {
310 tlb_flush(CPU(riscv_env_get_cpu(env)));
312 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
313 MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM |
314 MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
315 MSTATUS_TW;
318 /* silenty discard mstatus.mpp writes for unsupported modes */
319 if (mpp == PRV_H ||
320 (!riscv_has_ext(env, RVS) && mpp == PRV_S) ||
321 (!riscv_has_ext(env, RVU) && mpp == PRV_U)) {
322 mask &= ~MSTATUS_MPP;
325 mstatus = (mstatus & ~mask) | (val & mask);
327 int dirty = ((mstatus & MSTATUS_FS) == MSTATUS_FS) |
328 ((mstatus & MSTATUS_XS) == MSTATUS_XS);
329 mstatus = set_field(mstatus, MSTATUS_SD, dirty);
330 env->mstatus = mstatus;
332 return 0;
335 static int read_misa(CPURISCVState *env, int csrno, target_ulong *val)
337 *val = env->misa;
338 return 0;
341 static int write_misa(CPURISCVState *env, int csrno, target_ulong val)
343 if (!riscv_feature(env, RISCV_FEATURE_MISA)) {
344 /* drop write to misa */
345 return 0;
348 /* 'I' or 'E' must be present */
349 if (!(val & (RVI | RVE))) {
350 /* It is not, drop write to misa */
351 return 0;
354 /* 'E' excludes all other extensions */
355 if (val & RVE) {
356 /* when we support 'E' we can do "val = RVE;" however
357 * for now we just drop writes if 'E' is present.
359 return 0;
362 /* Mask extensions that are not supported by this hart */
363 val &= env->misa_mask;
365 /* Mask extensions that are not supported by QEMU */
366 val &= (RVI | RVE | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
368 /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
369 if ((val & RVD) && !(val & RVF)) {
370 val &= ~RVD;
373 /* Suppress 'C' if next instruction is not aligned
374 * TODO: this should check next_pc
376 if ((val & RVC) && (GETPC() & ~3) != 0) {
377 val &= ~RVC;
380 /* misa.MXL writes are not supported by QEMU */
381 val = (env->misa & MISA_MXL) | (val & ~MISA_MXL);
383 /* flush translation cache */
384 if (val != env->misa) {
385 tb_flush(CPU(riscv_env_get_cpu(env)));
388 env->misa = val;
390 return 0;
393 static int read_medeleg(CPURISCVState *env, int csrno, target_ulong *val)
395 *val = env->medeleg;
396 return 0;
399 static int write_medeleg(CPURISCVState *env, int csrno, target_ulong val)
401 env->medeleg = (env->medeleg & ~delegable_excps) | (val & delegable_excps);
402 return 0;
405 static int read_mideleg(CPURISCVState *env, int csrno, target_ulong *val)
407 *val = env->mideleg;
408 return 0;
411 static int write_mideleg(CPURISCVState *env, int csrno, target_ulong val)
413 env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints);
414 return 0;
417 static int read_mie(CPURISCVState *env, int csrno, target_ulong *val)
419 *val = env->mie;
420 return 0;
423 static int write_mie(CPURISCVState *env, int csrno, target_ulong val)
425 env->mie = (env->mie & ~all_ints) | (val & all_ints);
426 return 0;
429 static int read_mtvec(CPURISCVState *env, int csrno, target_ulong *val)
431 *val = env->mtvec;
432 return 0;
435 static int write_mtvec(CPURISCVState *env, int csrno, target_ulong val)
437 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
438 if ((val & 3) == 0) {
439 env->mtvec = val >> 2 << 2;
440 } else {
441 qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: vectored traps not supported");
443 return 0;
446 static int read_mcounteren(CPURISCVState *env, int csrno, target_ulong *val)
448 if (env->priv_ver < PRIV_VERSION_1_10_0) {
449 return -1;
451 *val = env->mcounteren;
452 return 0;
455 static int write_mcounteren(CPURISCVState *env, int csrno, target_ulong val)
457 if (env->priv_ver < PRIV_VERSION_1_10_0) {
458 return -1;
460 env->mcounteren = val;
461 return 0;
464 static int read_mscounteren(CPURISCVState *env, int csrno, target_ulong *val)
466 if (env->priv_ver > PRIV_VERSION_1_09_1) {
467 return -1;
469 *val = env->mcounteren;
470 return 0;
473 static int write_mscounteren(CPURISCVState *env, int csrno, target_ulong val)
475 if (env->priv_ver > PRIV_VERSION_1_09_1) {
476 return -1;
478 env->mcounteren = val;
479 return 0;
482 static int read_mucounteren(CPURISCVState *env, int csrno, target_ulong *val)
484 if (env->priv_ver > PRIV_VERSION_1_09_1) {
485 return -1;
487 *val = env->scounteren;
488 return 0;
491 static int write_mucounteren(CPURISCVState *env, int csrno, target_ulong val)
493 if (env->priv_ver > PRIV_VERSION_1_09_1) {
494 return -1;
496 env->scounteren = val;
497 return 0;
500 /* Machine Trap Handling */
501 static int read_mscratch(CPURISCVState *env, int csrno, target_ulong *val)
503 *val = env->mscratch;
504 return 0;
507 static int write_mscratch(CPURISCVState *env, int csrno, target_ulong val)
509 env->mscratch = val;
510 return 0;
513 static int read_mepc(CPURISCVState *env, int csrno, target_ulong *val)
515 *val = env->mepc;
516 return 0;
519 static int write_mepc(CPURISCVState *env, int csrno, target_ulong val)
521 env->mepc = val;
522 return 0;
525 static int read_mcause(CPURISCVState *env, int csrno, target_ulong *val)
527 *val = env->mcause;
528 return 0;
531 static int write_mcause(CPURISCVState *env, int csrno, target_ulong val)
533 env->mcause = val;
534 return 0;
537 static int read_mbadaddr(CPURISCVState *env, int csrno, target_ulong *val)
539 *val = env->mbadaddr;
540 return 0;
543 static int write_mbadaddr(CPURISCVState *env, int csrno, target_ulong val)
545 env->mbadaddr = val;
546 return 0;
549 static int rmw_mip(CPURISCVState *env, int csrno, target_ulong *ret_value,
550 target_ulong new_value, target_ulong write_mask)
552 RISCVCPU *cpu = riscv_env_get_cpu(env);
553 target_ulong mask = write_mask & delegable_ints;
554 uint32_t old_mip;
556 /* We can't allow the supervisor to control SEIP as this would allow the
557 * supervisor to clear a pending external interrupt which will result in
558 * lost a interrupt in the case a PLIC is attached. The SEIP bit must be
559 * hardware controlled when a PLIC is attached. This should be an option
560 * for CPUs with software-delegated Supervisor External Interrupts. */
561 mask &= ~MIP_SEIP;
563 if (mask) {
564 qemu_mutex_lock_iothread();
565 old_mip = riscv_cpu_update_mip(cpu, mask, (new_value & mask));
566 qemu_mutex_unlock_iothread();
567 } else {
568 old_mip = atomic_read(&env->mip);
571 if (ret_value) {
572 *ret_value = old_mip;
575 return 0;
578 /* Supervisor Trap Setup */
579 static int read_sstatus(CPURISCVState *env, int csrno, target_ulong *val)
581 target_ulong mask = ((env->priv_ver >= PRIV_VERSION_1_10_0) ?
582 sstatus_v1_10_mask : sstatus_v1_9_mask);
583 *val = env->mstatus & mask;
584 return 0;
587 static int write_sstatus(CPURISCVState *env, int csrno, target_ulong val)
589 target_ulong mask = ((env->priv_ver >= PRIV_VERSION_1_10_0) ?
590 sstatus_v1_10_mask : sstatus_v1_9_mask);
591 target_ulong newval = (env->mstatus & ~mask) | (val & mask);
592 return write_mstatus(env, CSR_MSTATUS, newval);
595 static int read_sie(CPURISCVState *env, int csrno, target_ulong *val)
597 *val = env->mie & env->mideleg;
598 return 0;
601 static int write_sie(CPURISCVState *env, int csrno, target_ulong val)
603 target_ulong newval = (env->mie & ~env->mideleg) | (val & env->mideleg);
604 return write_mie(env, CSR_MIE, newval);
607 static int read_stvec(CPURISCVState *env, int csrno, target_ulong *val)
609 *val = env->stvec;
610 return 0;
613 static int write_stvec(CPURISCVState *env, int csrno, target_ulong val)
615 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
616 if ((val & 3) == 0) {
617 env->stvec = val >> 2 << 2;
618 } else {
619 qemu_log_mask(LOG_UNIMP, "CSR_STVEC: vectored traps not supported");
621 return 0;
624 static int read_scounteren(CPURISCVState *env, int csrno, target_ulong *val)
626 if (env->priv_ver < PRIV_VERSION_1_10_0) {
627 return -1;
629 *val = env->scounteren;
630 return 0;
633 static int write_scounteren(CPURISCVState *env, int csrno, target_ulong val)
635 if (env->priv_ver < PRIV_VERSION_1_10_0) {
636 return -1;
638 env->scounteren = val;
639 return 0;
642 /* Supervisor Trap Handling */
643 static int read_sscratch(CPURISCVState *env, int csrno, target_ulong *val)
645 *val = env->sscratch;
646 return 0;
649 static int write_sscratch(CPURISCVState *env, int csrno, target_ulong val)
651 env->sscratch = val;
652 return 0;
655 static int read_sepc(CPURISCVState *env, int csrno, target_ulong *val)
657 *val = env->sepc;
658 return 0;
661 static int write_sepc(CPURISCVState *env, int csrno, target_ulong val)
663 env->sepc = val;
664 return 0;
667 static int read_scause(CPURISCVState *env, int csrno, target_ulong *val)
669 *val = env->scause;
670 return 0;
673 static int write_scause(CPURISCVState *env, int csrno, target_ulong val)
675 env->scause = val;
676 return 0;
679 static int read_sbadaddr(CPURISCVState *env, int csrno, target_ulong *val)
681 *val = env->sbadaddr;
682 return 0;
685 static int write_sbadaddr(CPURISCVState *env, int csrno, target_ulong val)
687 env->sbadaddr = val;
688 return 0;
691 static int rmw_sip(CPURISCVState *env, int csrno, target_ulong *ret_value,
692 target_ulong new_value, target_ulong write_mask)
694 return rmw_mip(env, CSR_MSTATUS, ret_value, new_value,
695 write_mask & env->mideleg);
698 /* Supervisor Protection and Translation */
699 static int read_satp(CPURISCVState *env, int csrno, target_ulong *val)
701 if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
702 *val = 0;
703 } else if (env->priv_ver >= PRIV_VERSION_1_10_0) {
704 if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
705 return -1;
706 } else {
707 *val = env->satp;
709 } else {
710 *val = env->sptbr;
712 return 0;
715 static int write_satp(CPURISCVState *env, int csrno, target_ulong val)
717 if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
718 return 0;
720 if (env->priv_ver <= PRIV_VERSION_1_09_1 && (val ^ env->sptbr)) {
721 tlb_flush(CPU(riscv_env_get_cpu(env)));
722 env->sptbr = val & (((target_ulong)
723 1 << (TARGET_PHYS_ADDR_SPACE_BITS - PGSHIFT)) - 1);
725 if (env->priv_ver >= PRIV_VERSION_1_10_0 &&
726 validate_vm(env, get_field(val, SATP_MODE)) &&
727 ((val ^ env->satp) & (SATP_MODE | SATP_ASID | SATP_PPN)))
729 if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) {
730 return -1;
731 } else {
732 tlb_flush(CPU(riscv_env_get_cpu(env)));
733 env->satp = val;
736 return 0;
739 /* Physical Memory Protection */
740 static int read_pmpcfg(CPURISCVState *env, int csrno, target_ulong *val)
742 *val = pmpcfg_csr_read(env, csrno - CSR_PMPCFG0);
743 return 0;
746 static int write_pmpcfg(CPURISCVState *env, int csrno, target_ulong val)
748 pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val);
749 return 0;
752 static int read_pmpaddr(CPURISCVState *env, int csrno, target_ulong *val)
754 *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0);
755 return 0;
758 static int write_pmpaddr(CPURISCVState *env, int csrno, target_ulong val)
760 pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val);
761 return 0;
764 #endif
767 * riscv_csrrw - read and/or update control and status register
769 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0);
770 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1);
771 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value);
772 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value);
775 int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value,
776 target_ulong new_value, target_ulong write_mask)
778 int ret;
779 target_ulong old_value;
781 /* check privileges and return -1 if check fails */
782 #if !defined(CONFIG_USER_ONLY)
783 int csr_priv = get_field(csrno, 0x300);
784 int read_only = get_field(csrno, 0xC00) == 3;
785 if ((write_mask && read_only) || (env->priv < csr_priv)) {
786 return -1;
788 #endif
790 /* check predicate */
791 if (!csr_ops[csrno].predicate || csr_ops[csrno].predicate(env, csrno) < 0) {
792 return -1;
795 /* execute combined read/write operation if it exists */
796 if (csr_ops[csrno].op) {
797 return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask);
800 /* if no accessor exists then return failure */
801 if (!csr_ops[csrno].read) {
802 return -1;
805 /* read old value */
806 ret = csr_ops[csrno].read(env, csrno, &old_value);
807 if (ret < 0) {
808 return ret;
811 /* write value if writable and write mask set, otherwise drop writes */
812 if (write_mask) {
813 new_value = (old_value & ~write_mask) | (new_value & write_mask);
814 if (csr_ops[csrno].write) {
815 ret = csr_ops[csrno].write(env, csrno, new_value);
816 if (ret < 0) {
817 return ret;
822 /* return old value */
823 if (ret_value) {
824 *ret_value = old_value;
827 return 0;
830 /* Control and Status Register function table */
831 static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
832 /* User Floating-Point CSRs */
833 [CSR_FFLAGS] = { fs, read_fflags, write_fflags },
834 [CSR_FRM] = { fs, read_frm, write_frm },
835 [CSR_FCSR] = { fs, read_fcsr, write_fcsr },
837 /* User Timers and Counters */
838 [CSR_CYCLE] = { ctr, read_instret },
839 [CSR_INSTRET] = { ctr, read_instret },
840 #if defined(TARGET_RISCV32)
841 [CSR_CYCLEH] = { ctr, read_instreth },
842 [CSR_INSTRETH] = { ctr, read_instreth },
843 #endif
845 /* User-level time CSRs are only available in linux-user
846 * In privileged mode, the monitor emulates these CSRs */
847 #if defined(CONFIG_USER_ONLY)
848 [CSR_TIME] = { ctr, read_time },
849 #if defined(TARGET_RISCV32)
850 [CSR_TIMEH] = { ctr, read_timeh },
851 #endif
852 #endif
854 #if !defined(CONFIG_USER_ONLY)
855 /* Machine Timers and Counters */
856 [CSR_MCYCLE] = { any, read_instret },
857 [CSR_MINSTRET] = { any, read_instret },
858 #if defined(TARGET_RISCV32)
859 [CSR_MCYCLEH] = { any, read_instreth },
860 [CSR_MINSTRETH] = { any, read_instreth },
861 #endif
863 /* Machine Information Registers */
864 [CSR_MVENDORID] = { any, read_zero },
865 [CSR_MARCHID] = { any, read_zero },
866 [CSR_MIMPID] = { any, read_zero },
867 [CSR_MHARTID] = { any, read_mhartid },
869 /* Machine Trap Setup */
870 [CSR_MSTATUS] = { any, read_mstatus, write_mstatus },
871 [CSR_MISA] = { any, read_misa, write_misa },
872 [CSR_MIDELEG] = { any, read_mideleg, write_mideleg },
873 [CSR_MEDELEG] = { any, read_medeleg, write_medeleg },
874 [CSR_MIE] = { any, read_mie, write_mie },
875 [CSR_MTVEC] = { any, read_mtvec, write_mtvec },
876 [CSR_MCOUNTEREN] = { any, read_mcounteren, write_mcounteren },
878 /* Legacy Counter Setup (priv v1.9.1) */
879 [CSR_MUCOUNTEREN] = { any, read_mucounteren, write_mucounteren },
880 [CSR_MSCOUNTEREN] = { any, read_mscounteren, write_mscounteren },
882 /* Machine Trap Handling */
883 [CSR_MSCRATCH] = { any, read_mscratch, write_mscratch },
884 [CSR_MEPC] = { any, read_mepc, write_mepc },
885 [CSR_MCAUSE] = { any, read_mcause, write_mcause },
886 [CSR_MBADADDR] = { any, read_mbadaddr, write_mbadaddr },
887 [CSR_MIP] = { any, NULL, NULL, rmw_mip },
889 /* Supervisor Trap Setup */
890 [CSR_SSTATUS] = { smode, read_sstatus, write_sstatus },
891 [CSR_SIE] = { smode, read_sie, write_sie },
892 [CSR_STVEC] = { smode, read_stvec, write_stvec },
893 [CSR_SCOUNTEREN] = { smode, read_scounteren, write_scounteren },
895 /* Supervisor Trap Handling */
896 [CSR_SSCRATCH] = { smode, read_sscratch, write_sscratch },
897 [CSR_SEPC] = { smode, read_sepc, write_sepc },
898 [CSR_SCAUSE] = { smode, read_scause, write_scause },
899 [CSR_SBADADDR] = { smode, read_sbadaddr, write_sbadaddr },
900 [CSR_SIP] = { smode, NULL, NULL, rmw_sip },
902 /* Supervisor Protection and Translation */
903 [CSR_SATP] = { smode, read_satp, write_satp },
905 /* Physical Memory Protection */
906 [CSR_PMPCFG0 ... CSR_PMPADDR9] = { pmp, read_pmpcfg, write_pmpcfg },
907 [CSR_PMPADDR0 ... CSR_PMPADDR15] = { pmp, read_pmpaddr, write_pmpaddr },
909 /* Performance Counters */
910 [CSR_HPMCOUNTER3 ... CSR_HPMCOUNTER31] = { ctr, read_zero },
911 [CSR_MHPMCOUNTER3 ... CSR_MHPMCOUNTER31] = { any, read_zero },
912 [CSR_MHPMEVENT3 ... CSR_MHPMEVENT31] = { any, read_zero },
913 #if defined(TARGET_RISCV32)
914 [CSR_HPMCOUNTER3H ... CSR_HPMCOUNTER31H] = { ctr, read_zero },
915 [CSR_MHPMCOUNTER3H ... CSR_MHPMCOUNTER31H] = { any, read_zero },
916 #endif
917 #endif /* !CONFIG_USER_ONLY */