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
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"
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 */
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
;
46 static int fs(CPURISCVState
*env
, int csrno
)
48 #if !defined(CONFIG_USER_ONLY)
49 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
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 */
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
) {
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)))) {
90 #if !defined(CONFIG_USER_ONLY)
91 static int any(CPURISCVState
*env
, int csrno
)
96 static int smode(CPURISCVState
*env
, int csrno
)
98 return -!riscv_has_ext(env
, RVS
);
101 static int pmp(CPURISCVState
*env
, int csrno
)
103 return -!riscv_feature(env
, RISCV_FEATURE_PMP
);
107 /* User Floating-Point CSRs */
108 static int read_fflags(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
110 #if !defined(CONFIG_USER_ONLY)
111 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
115 *val
= riscv_cpu_get_fflags(env
);
119 static int write_fflags(CPURISCVState
*env
, int csrno
, target_ulong val
)
121 #if !defined(CONFIG_USER_ONLY)
122 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
125 env
->mstatus
|= MSTATUS_FS
;
127 riscv_cpu_set_fflags(env
, val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
));
131 static int read_frm(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
133 #if !defined(CONFIG_USER_ONLY)
134 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
142 static int write_frm(CPURISCVState
*env
, int csrno
, target_ulong val
)
144 #if !defined(CONFIG_USER_ONLY)
145 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
148 env
->mstatus
|= MSTATUS_FS
;
150 env
->frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
154 static int read_fcsr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
156 #if !defined(CONFIG_USER_ONLY)
157 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
161 *val
= (riscv_cpu_get_fflags(env
) << FSR_AEXC_SHIFT
)
162 | (env
->frm
<< FSR_RD_SHIFT
);
166 static int write_fcsr(CPURISCVState
*env
, int csrno
, target_ulong val
)
168 #if !defined(CONFIG_USER_ONLY)
169 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
172 env
->mstatus
|= MSTATUS_FS
;
174 env
->frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
175 riscv_cpu_set_fflags(env
, (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
);
179 /* User Timers and Counters */
180 static int read_instret(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
182 #if !defined(CONFIG_USER_ONLY)
184 *val
= cpu_get_icount();
186 *val
= cpu_get_host_ticks();
189 *val
= cpu_get_host_ticks();
194 #if defined(TARGET_RISCV32)
195 static int read_instreth(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
197 #if !defined(CONFIG_USER_ONLY)
199 *val
= cpu_get_icount() >> 32;
201 *val
= cpu_get_host_ticks() >> 32;
204 *val
= cpu_get_host_ticks() >> 32;
208 #endif /* TARGET_RISCV32 */
210 #if defined(CONFIG_USER_ONLY)
211 static int read_time(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
213 *val
= cpu_get_host_ticks();
217 #if defined(TARGET_RISCV32)
218 static int read_timeh(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
220 *val
= cpu_get_host_ticks() >> 32;
225 #else /* CONFIG_USER_ONLY */
227 /* Machine constants */
229 #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)
230 #define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)
232 static const target_ulong delegable_ints
= S_MODE_INTERRUPTS
;
233 static const target_ulong all_ints
= M_MODE_INTERRUPTS
| S_MODE_INTERRUPTS
;
234 static const target_ulong delegable_excps
=
235 (1ULL << (RISCV_EXCP_INST_ADDR_MIS
)) |
236 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT
)) |
237 (1ULL << (RISCV_EXCP_ILLEGAL_INST
)) |
238 (1ULL << (RISCV_EXCP_BREAKPOINT
)) |
239 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS
)) |
240 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT
)) |
241 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS
)) |
242 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT
)) |
243 (1ULL << (RISCV_EXCP_U_ECALL
)) |
244 (1ULL << (RISCV_EXCP_S_ECALL
)) |
245 (1ULL << (RISCV_EXCP_H_ECALL
)) |
246 (1ULL << (RISCV_EXCP_M_ECALL
)) |
247 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT
)) |
248 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT
)) |
249 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT
));
250 static const target_ulong sstatus_v1_9_mask
= SSTATUS_SIE
| SSTATUS_SPIE
|
251 SSTATUS_UIE
| SSTATUS_UPIE
| SSTATUS_SPP
| SSTATUS_FS
| SSTATUS_XS
|
252 SSTATUS_SUM
| SSTATUS_SD
;
253 static const target_ulong sstatus_v1_10_mask
= SSTATUS_SIE
| SSTATUS_SPIE
|
254 SSTATUS_UIE
| SSTATUS_UPIE
| SSTATUS_SPP
| SSTATUS_FS
| SSTATUS_XS
|
255 SSTATUS_SUM
| SSTATUS_MXR
| SSTATUS_SD
;
256 static const target_ulong sip_writable_mask
= SIP_SSIP
| MIP_USIP
| MIP_UEIP
;
258 #if defined(TARGET_RISCV32)
259 static const char valid_vm_1_09
[16] = {
263 static const char valid_vm_1_10
[16] = {
267 #elif defined(TARGET_RISCV64)
268 static const char valid_vm_1_09
[16] = {
273 static const char valid_vm_1_10
[16] = {
279 #endif /* CONFIG_USER_ONLY */
281 /* Machine Information Registers */
282 static int read_zero(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
287 static int read_mhartid(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
293 /* Machine Trap Setup */
294 static int read_mstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
300 static int validate_vm(CPURISCVState
*env
, target_ulong vm
)
302 return (env
->priv_ver
>= PRIV_VERSION_1_10_0
) ?
303 valid_vm_1_10
[vm
& 0xf] : valid_vm_1_09
[vm
& 0xf];
306 static int write_mstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
308 target_ulong mstatus
= env
->mstatus
;
309 target_ulong mask
= 0;
312 /* flush tlb on mstatus fields that affect VM */
313 if (env
->priv_ver
<= PRIV_VERSION_1_09_1
) {
314 if ((val
^ mstatus
) & (MSTATUS_MXR
| MSTATUS_MPP
|
315 MSTATUS_MPRV
| MSTATUS_SUM
| MSTATUS_VM
)) {
316 tlb_flush(env_cpu(env
));
318 mask
= MSTATUS_SIE
| MSTATUS_SPIE
| MSTATUS_MIE
| MSTATUS_MPIE
|
319 MSTATUS_SPP
| MSTATUS_FS
| MSTATUS_MPRV
| MSTATUS_SUM
|
320 MSTATUS_MPP
| MSTATUS_MXR
|
321 (validate_vm(env
, get_field(val
, MSTATUS_VM
)) ?
324 if (env
->priv_ver
>= PRIV_VERSION_1_10_0
) {
325 if ((val
^ mstatus
) & (MSTATUS_MXR
| MSTATUS_MPP
| MSTATUS_MPV
|
326 MSTATUS_MPRV
| MSTATUS_SUM
)) {
327 tlb_flush(env_cpu(env
));
329 mask
= MSTATUS_SIE
| MSTATUS_SPIE
| MSTATUS_MIE
| MSTATUS_MPIE
|
330 MSTATUS_SPP
| MSTATUS_FS
| MSTATUS_MPRV
| MSTATUS_SUM
|
331 MSTATUS_MPP
| MSTATUS_MXR
| MSTATUS_TVM
| MSTATUS_TSR
|
333 #if defined(TARGET_RISCV64)
335 * RV32: MPV and MTL are not in mstatus. The current plan is to
336 * add them to mstatush. For now, we just don't support it.
338 mask
|= MSTATUS_MTL
| MSTATUS_MPV
;
342 mstatus
= (mstatus
& ~mask
) | (val
& mask
);
344 dirty
= ((mstatus
& MSTATUS_FS
) == MSTATUS_FS
) |
345 ((mstatus
& MSTATUS_XS
) == MSTATUS_XS
);
346 mstatus
= set_field(mstatus
, MSTATUS_SD
, dirty
);
347 env
->mstatus
= mstatus
;
352 static int read_misa(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
358 static int write_misa(CPURISCVState
*env
, int csrno
, target_ulong val
)
360 if (!riscv_feature(env
, RISCV_FEATURE_MISA
)) {
361 /* drop write to misa */
365 /* 'I' or 'E' must be present */
366 if (!(val
& (RVI
| RVE
))) {
367 /* It is not, drop write to misa */
371 /* 'E' excludes all other extensions */
373 /* when we support 'E' we can do "val = RVE;" however
374 * for now we just drop writes if 'E' is present.
379 /* Mask extensions that are not supported by this hart */
380 val
&= env
->misa_mask
;
382 /* Mask extensions that are not supported by QEMU */
383 val
&= (RVI
| RVE
| RVM
| RVA
| RVF
| RVD
| RVC
| RVS
| RVU
);
385 /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
386 if ((val
& RVD
) && !(val
& RVF
)) {
390 /* Suppress 'C' if next instruction is not aligned
391 * TODO: this should check next_pc
393 if ((val
& RVC
) && (GETPC() & ~3) != 0) {
397 /* misa.MXL writes are not supported by QEMU */
398 val
= (env
->misa
& MISA_MXL
) | (val
& ~MISA_MXL
);
400 /* flush translation cache */
401 if (val
!= env
->misa
) {
402 tb_flush(env_cpu(env
));
410 static int read_medeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
416 static int write_medeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
418 env
->medeleg
= (env
->medeleg
& ~delegable_excps
) | (val
& delegable_excps
);
422 static int read_mideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
428 static int write_mideleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
430 env
->mideleg
= (env
->mideleg
& ~delegable_ints
) | (val
& delegable_ints
);
434 static int read_mie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
440 static int write_mie(CPURISCVState
*env
, int csrno
, target_ulong val
)
442 env
->mie
= (env
->mie
& ~all_ints
) | (val
& all_ints
);
446 static int read_mtvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
452 static int write_mtvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
454 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
458 qemu_log_mask(LOG_UNIMP
, "CSR_MTVEC: reserved mode not supported\n");
463 static int read_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
465 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
468 *val
= env
->mcounteren
;
472 static int write_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
474 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
477 env
->mcounteren
= val
;
481 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
482 static int read_mscounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
484 if (env
->priv_ver
> PRIV_VERSION_1_09_1
485 && env
->priv_ver
< PRIV_VERSION_1_11_0
) {
488 *val
= env
->mcounteren
;
492 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
493 static int write_mscounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
495 if (env
->priv_ver
> PRIV_VERSION_1_09_1
496 && env
->priv_ver
< PRIV_VERSION_1_11_0
) {
499 env
->mcounteren
= val
;
503 static int read_mucounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
505 if (env
->priv_ver
> PRIV_VERSION_1_09_1
) {
508 *val
= env
->scounteren
;
512 static int write_mucounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
514 if (env
->priv_ver
> PRIV_VERSION_1_09_1
) {
517 env
->scounteren
= val
;
521 /* Machine Trap Handling */
522 static int read_mscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
524 *val
= env
->mscratch
;
528 static int write_mscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
534 static int read_mepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
540 static int write_mepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
546 static int read_mcause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
552 static int write_mcause(CPURISCVState
*env
, int csrno
, target_ulong val
)
558 static int read_mbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
560 *val
= env
->mbadaddr
;
564 static int write_mbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
570 static int rmw_mip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
571 target_ulong new_value
, target_ulong write_mask
)
573 RISCVCPU
*cpu
= env_archcpu(env
);
574 /* Allow software control of delegable interrupts not claimed by hardware */
575 target_ulong mask
= write_mask
& delegable_ints
& ~env
->miclaim
;
579 old_mip
= riscv_cpu_update_mip(cpu
, mask
, (new_value
& mask
));
585 *ret_value
= old_mip
;
591 /* Supervisor Trap Setup */
592 static int read_sstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
594 target_ulong mask
= ((env
->priv_ver
>= PRIV_VERSION_1_10_0
) ?
595 sstatus_v1_10_mask
: sstatus_v1_9_mask
);
596 *val
= env
->mstatus
& mask
;
600 static int write_sstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
602 target_ulong mask
= ((env
->priv_ver
>= PRIV_VERSION_1_10_0
) ?
603 sstatus_v1_10_mask
: sstatus_v1_9_mask
);
604 target_ulong newval
= (env
->mstatus
& ~mask
) | (val
& mask
);
605 return write_mstatus(env
, CSR_MSTATUS
, newval
);
608 static int read_sie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
610 *val
= env
->mie
& env
->mideleg
;
614 static int write_sie(CPURISCVState
*env
, int csrno
, target_ulong val
)
616 target_ulong newval
= (env
->mie
& ~env
->mideleg
) | (val
& env
->mideleg
);
617 return write_mie(env
, CSR_MIE
, newval
);
620 static int read_stvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
626 static int write_stvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
628 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
632 qemu_log_mask(LOG_UNIMP
, "CSR_STVEC: reserved mode not supported\n");
637 static int read_scounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
639 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
642 *val
= env
->scounteren
;
646 static int write_scounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
648 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
651 env
->scounteren
= val
;
655 /* Supervisor Trap Handling */
656 static int read_sscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
658 *val
= env
->sscratch
;
662 static int write_sscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
668 static int read_sepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
674 static int write_sepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
680 static int read_scause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
686 static int write_scause(CPURISCVState
*env
, int csrno
, target_ulong val
)
692 static int read_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
694 *val
= env
->sbadaddr
;
698 static int write_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
704 static int rmw_sip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
705 target_ulong new_value
, target_ulong write_mask
)
707 int ret
= rmw_mip(env
, CSR_MSTATUS
, ret_value
, new_value
,
708 write_mask
& env
->mideleg
& sip_writable_mask
);
709 *ret_value
&= env
->mideleg
;
713 /* Supervisor Protection and Translation */
714 static int read_satp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
716 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
718 } else if (env
->priv_ver
>= PRIV_VERSION_1_10_0
) {
719 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
730 static int write_satp(CPURISCVState
*env
, int csrno
, target_ulong val
)
732 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
735 if (env
->priv_ver
<= PRIV_VERSION_1_09_1
&& (val
^ env
->sptbr
)) {
736 tlb_flush(env_cpu(env
));
737 env
->sptbr
= val
& (((target_ulong
)
738 1 << (TARGET_PHYS_ADDR_SPACE_BITS
- PGSHIFT
)) - 1);
740 if (env
->priv_ver
>= PRIV_VERSION_1_10_0
&&
741 validate_vm(env
, get_field(val
, SATP_MODE
)) &&
742 ((val
^ env
->satp
) & (SATP_MODE
| SATP_ASID
| SATP_PPN
)))
744 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
747 if((val
^ env
->satp
) & SATP_ASID
) {
748 tlb_flush(env_cpu(env
));
756 /* Physical Memory Protection */
757 static int read_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
759 *val
= pmpcfg_csr_read(env
, csrno
- CSR_PMPCFG0
);
763 static int write_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong val
)
765 pmpcfg_csr_write(env
, csrno
- CSR_PMPCFG0
, val
);
769 static int read_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
771 *val
= pmpaddr_csr_read(env
, csrno
- CSR_PMPADDR0
);
775 static int write_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
777 pmpaddr_csr_write(env
, csrno
- CSR_PMPADDR0
, val
);
784 * riscv_csrrw - read and/or update control and status register
786 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0);
787 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1);
788 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value);
789 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value);
792 int riscv_csrrw(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
793 target_ulong new_value
, target_ulong write_mask
)
796 target_ulong old_value
;
797 RISCVCPU
*cpu
= env_archcpu(env
);
799 /* check privileges and return -1 if check fails */
800 #if !defined(CONFIG_USER_ONLY)
801 int csr_priv
= get_field(csrno
, 0x300);
802 int read_only
= get_field(csrno
, 0xC00) == 3;
803 if ((!env
->debugger
) && (env
->priv
< csr_priv
)) {
806 if (write_mask
&& read_only
) {
811 /* ensure the CSR extension is enabled. */
812 if (!cpu
->cfg
.ext_icsr
) {
816 /* check predicate */
817 if (!csr_ops
[csrno
].predicate
|| csr_ops
[csrno
].predicate(env
, csrno
) < 0) {
821 /* execute combined read/write operation if it exists */
822 if (csr_ops
[csrno
].op
) {
823 return csr_ops
[csrno
].op(env
, csrno
, ret_value
, new_value
, write_mask
);
826 /* if no accessor exists then return failure */
827 if (!csr_ops
[csrno
].read
) {
832 ret
= csr_ops
[csrno
].read(env
, csrno
, &old_value
);
837 /* write value if writable and write mask set, otherwise drop writes */
839 new_value
= (old_value
& ~write_mask
) | (new_value
& write_mask
);
840 if (csr_ops
[csrno
].write
) {
841 ret
= csr_ops
[csrno
].write(env
, csrno
, new_value
);
848 /* return old value */
850 *ret_value
= old_value
;
857 * Debugger support. If not in user mode, set env->debugger before the
858 * riscv_csrrw call and clear it after the call.
860 int riscv_csrrw_debug(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
861 target_ulong new_value
, target_ulong write_mask
)
864 #if !defined(CONFIG_USER_ONLY)
865 env
->debugger
= true;
867 ret
= riscv_csrrw(env
, csrno
, ret_value
, new_value
, write_mask
);
868 #if !defined(CONFIG_USER_ONLY)
869 env
->debugger
= false;
874 /* Control and Status Register function table */
875 static riscv_csr_operations csr_ops
[CSR_TABLE_SIZE
] = {
876 /* User Floating-Point CSRs */
877 [CSR_FFLAGS
] = { fs
, read_fflags
, write_fflags
},
878 [CSR_FRM
] = { fs
, read_frm
, write_frm
},
879 [CSR_FCSR
] = { fs
, read_fcsr
, write_fcsr
},
881 /* User Timers and Counters */
882 [CSR_CYCLE
] = { ctr
, read_instret
},
883 [CSR_INSTRET
] = { ctr
, read_instret
},
884 #if defined(TARGET_RISCV32)
885 [CSR_CYCLEH
] = { ctr
, read_instreth
},
886 [CSR_INSTRETH
] = { ctr
, read_instreth
},
889 /* User-level time CSRs are only available in linux-user
890 * In privileged mode, the monitor emulates these CSRs */
891 #if defined(CONFIG_USER_ONLY)
892 [CSR_TIME
] = { ctr
, read_time
},
893 #if defined(TARGET_RISCV32)
894 [CSR_TIMEH
] = { ctr
, read_timeh
},
898 #if !defined(CONFIG_USER_ONLY)
899 /* Machine Timers and Counters */
900 [CSR_MCYCLE
] = { any
, read_instret
},
901 [CSR_MINSTRET
] = { any
, read_instret
},
902 #if defined(TARGET_RISCV32)
903 [CSR_MCYCLEH
] = { any
, read_instreth
},
904 [CSR_MINSTRETH
] = { any
, read_instreth
},
907 /* Machine Information Registers */
908 [CSR_MVENDORID
] = { any
, read_zero
},
909 [CSR_MARCHID
] = { any
, read_zero
},
910 [CSR_MIMPID
] = { any
, read_zero
},
911 [CSR_MHARTID
] = { any
, read_mhartid
},
913 /* Machine Trap Setup */
914 [CSR_MSTATUS
] = { any
, read_mstatus
, write_mstatus
},
915 [CSR_MISA
] = { any
, read_misa
, write_misa
},
916 [CSR_MIDELEG
] = { any
, read_mideleg
, write_mideleg
},
917 [CSR_MEDELEG
] = { any
, read_medeleg
, write_medeleg
},
918 [CSR_MIE
] = { any
, read_mie
, write_mie
},
919 [CSR_MTVEC
] = { any
, read_mtvec
, write_mtvec
},
920 [CSR_MCOUNTEREN
] = { any
, read_mcounteren
, write_mcounteren
},
922 /* Legacy Counter Setup (priv v1.9.1) */
923 [CSR_MUCOUNTEREN
] = { any
, read_mucounteren
, write_mucounteren
},
924 [CSR_MSCOUNTEREN
] = { any
, read_mscounteren
, write_mscounteren
},
926 /* Machine Trap Handling */
927 [CSR_MSCRATCH
] = { any
, read_mscratch
, write_mscratch
},
928 [CSR_MEPC
] = { any
, read_mepc
, write_mepc
},
929 [CSR_MCAUSE
] = { any
, read_mcause
, write_mcause
},
930 [CSR_MBADADDR
] = { any
, read_mbadaddr
, write_mbadaddr
},
931 [CSR_MIP
] = { any
, NULL
, NULL
, rmw_mip
},
933 /* Supervisor Trap Setup */
934 [CSR_SSTATUS
] = { smode
, read_sstatus
, write_sstatus
},
935 [CSR_SIE
] = { smode
, read_sie
, write_sie
},
936 [CSR_STVEC
] = { smode
, read_stvec
, write_stvec
},
937 [CSR_SCOUNTEREN
] = { smode
, read_scounteren
, write_scounteren
},
939 /* Supervisor Trap Handling */
940 [CSR_SSCRATCH
] = { smode
, read_sscratch
, write_sscratch
},
941 [CSR_SEPC
] = { smode
, read_sepc
, write_sepc
},
942 [CSR_SCAUSE
] = { smode
, read_scause
, write_scause
},
943 [CSR_SBADADDR
] = { smode
, read_sbadaddr
, write_sbadaddr
},
944 [CSR_SIP
] = { smode
, NULL
, NULL
, rmw_sip
},
946 /* Supervisor Protection and Translation */
947 [CSR_SATP
] = { smode
, read_satp
, write_satp
},
949 /* Physical Memory Protection */
950 [CSR_PMPCFG0
... CSR_PMPADDR9
] = { pmp
, read_pmpcfg
, write_pmpcfg
},
951 [CSR_PMPADDR0
... CSR_PMPADDR15
] = { pmp
, read_pmpaddr
, write_pmpaddr
},
953 /* Performance Counters */
954 [CSR_HPMCOUNTER3
... CSR_HPMCOUNTER31
] = { ctr
, read_zero
},
955 [CSR_MHPMCOUNTER3
... CSR_MHPMCOUNTER31
] = { any
, read_zero
},
956 [CSR_MHPMEVENT3
... CSR_MHPMEVENT31
] = { any
, read_zero
},
957 #if defined(TARGET_RISCV32)
958 [CSR_HPMCOUNTER3H
... CSR_HPMCOUNTER31H
] = { ctr
, read_zero
},
959 [CSR_MHPMCOUNTER3H
... CSR_MHPMCOUNTER31H
] = { any
, read_zero
},
961 #endif /* !CONFIG_USER_ONLY */