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 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
) {
115 static int pmp(CPURISCVState
*env
, int csrno
)
117 return -!riscv_feature(env
, RISCV_FEATURE_PMP
);
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
)) {
129 *val
= riscv_cpu_get_fflags(env
);
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
)) {
139 env
->mstatus
|= MSTATUS_FS
;
141 riscv_cpu_set_fflags(env
, val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
));
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
)) {
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
)) {
162 env
->mstatus
|= MSTATUS_FS
;
164 env
->frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
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
)) {
175 *val
= (riscv_cpu_get_fflags(env
) << FSR_AEXC_SHIFT
)
176 | (env
->frm
<< FSR_RD_SHIFT
);
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
)) {
186 env
->mstatus
|= MSTATUS_FS
;
188 env
->frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
189 riscv_cpu_set_fflags(env
, (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
);
193 /* User Timers and Counters */
194 static int read_instret(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
196 #if !defined(CONFIG_USER_ONLY)
198 *val
= cpu_get_icount();
200 *val
= cpu_get_host_ticks();
203 *val
= cpu_get_host_ticks();
208 #if defined(TARGET_RISCV32)
209 static int read_instreth(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
211 #if !defined(CONFIG_USER_ONLY)
213 *val
= cpu_get_icount() >> 32;
215 *val
= cpu_get_host_ticks() >> 32;
218 *val
= cpu_get_host_ticks() >> 32;
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();
231 #if defined(TARGET_RISCV32)
232 static int read_timeh(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
234 *val
= cpu_get_host_ticks() >> 32;
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
) {
249 *val
= env
->rdtime_fn() + delta
;
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
) {
262 *val
= (env
->rdtime_fn() + delta
) >> 32;
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
|
275 static const target_ulong all_ints
= M_MODE_INTERRUPTS
| S_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] = {
311 static const char valid_vm_1_10
[16] = {
315 #elif defined(TARGET_RISCV64)
316 static const char valid_vm_1_09
[16] = {
321 static const char valid_vm_1_10
[16] = {
327 #endif /* CONFIG_USER_ONLY */
329 /* Machine Information Registers */
330 static int read_zero(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
335 static int read_mhartid(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
341 /* Machine Trap Setup */
342 static int read_mstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
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;
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
)) ?
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
|
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
;
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
;
400 #ifdef TARGET_RISCV32
401 static int read_mstatush(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
403 *val
= env
->mstatush
;
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
;
421 static int read_misa(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
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 */
434 /* 'I' or 'E' must be present */
435 if (!(val
& (RVI
| RVE
))) {
436 /* It is not, drop write to misa */
440 /* 'E' excludes all other extensions */
442 /* when we support 'E' we can do "val = RVE;" however
443 * for now we just drop writes if 'E' is present.
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
)) {
459 /* Suppress 'C' if next instruction is not aligned
460 * TODO: this should check next_pc
462 if ((val
& RVC
) && (GETPC() & ~3) != 0) {
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
));
479 static int read_medeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
485 static int write_medeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
487 env
->medeleg
= (env
->medeleg
& ~delegable_excps
) | (val
& delegable_excps
);
491 static int read_mideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
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
;
506 static int read_mie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
512 static int write_mie(CPURISCVState
*env
, int csrno
, target_ulong val
)
514 env
->mie
= (env
->mie
& ~all_ints
) | (val
& all_ints
);
518 static int read_mtvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
524 static int write_mtvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
526 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
530 qemu_log_mask(LOG_UNIMP
, "CSR_MTVEC: reserved mode not supported\n");
535 static int read_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
537 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
540 *val
= env
->mcounteren
;
544 static int write_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
546 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
549 env
->mcounteren
= val
;
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
) {
560 *val
= env
->mcounteren
;
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
) {
571 env
->mcounteren
= val
;
575 static int read_mucounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
577 if (env
->priv_ver
> PRIV_VERSION_1_09_1
) {
580 *val
= env
->scounteren
;
584 static int write_mucounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
586 if (env
->priv_ver
> PRIV_VERSION_1_09_1
) {
589 env
->scounteren
= val
;
593 /* Machine Trap Handling */
594 static int read_mscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
596 *val
= env
->mscratch
;
600 static int write_mscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
606 static int read_mepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
612 static int write_mepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
618 static int read_mcause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
624 static int write_mcause(CPURISCVState
*env
, int csrno
, target_ulong val
)
630 static int read_mbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
632 *val
= env
->mbadaddr
;
636 static int write_mbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
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
;
651 old_mip
= riscv_cpu_update_mip(cpu
, mask
, (new_value
& mask
));
657 *ret_value
= old_mip
;
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
;
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;
686 *val
= env
->mie
& env
->mideleg
;
691 static int write_sie(CPURISCVState
*env
, int csrno
, target_ulong val
)
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
);
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
)
712 static int write_stvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
714 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
718 qemu_log_mask(LOG_UNIMP
, "CSR_STVEC: reserved mode not supported\n");
723 static int read_scounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
725 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
728 *val
= env
->scounteren
;
732 static int write_scounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
734 if (env
->priv_ver
< PRIV_VERSION_1_10_0
) {
737 env
->scounteren
= val
;
741 /* Supervisor Trap Handling */
742 static int read_sscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
744 *val
= env
->sscratch
;
748 static int write_sscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
754 static int read_sepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
760 static int write_sepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
766 static int read_scause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
772 static int write_scause(CPURISCVState
*env
, int csrno
, target_ulong val
)
778 static int read_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
780 *val
= env
->sbadaddr
;
784 static int write_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
790 static int rmw_sip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
791 target_ulong new_value
, target_ulong write_mask
)
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
;
802 ret
= rmw_mip(env
, CSR_MSTATUS
, ret_value
, new_value
,
803 write_mask
& env
->mideleg
& sip_writable_mask
);
806 *ret_value
&= env
->mideleg
;
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
)) {
815 } else if (env
->priv_ver
>= PRIV_VERSION_1_10_0
) {
816 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
827 static int write_satp(CPURISCVState
*env
, int csrno
, target_ulong val
)
829 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
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
)) {
844 if((val
^ env
->satp
) & SATP_ASID
) {
845 tlb_flush(env_cpu(env
));
853 /* Hypervisor Extensions */
854 static int read_hstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
860 static int write_hstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
866 static int read_hedeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
872 static int write_hedeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
878 static int read_hideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
884 static int write_hideleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
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
);
899 static int read_hie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
901 *val
= env
->mie
& VS_MODE_INTERRUPTS
;
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
;
917 static int write_hcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
919 env
->hcounteren
= val
;
923 static int read_htval(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
929 static int write_htval(CPURISCVState
*env
, int csrno
, target_ulong val
)
935 static int read_htinst(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
941 static int write_htinst(CPURISCVState
*env
, int csrno
, target_ulong val
)
947 static int read_hgatp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
953 static int write_hgatp(CPURISCVState
*env
, int csrno
, target_ulong val
)
959 static int read_htimedelta(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
961 if (!env
->rdtime_fn
) {
965 #if defined(TARGET_RISCV32)
966 *val
= env
->htimedelta
& 0xffffffff;
968 *val
= env
->htimedelta
;
973 static int write_htimedelta(CPURISCVState
*env
, int csrno
, target_ulong val
)
975 if (!env
->rdtime_fn
) {
979 #if defined(TARGET_RISCV32)
980 env
->htimedelta
= deposit64(env
->htimedelta
, 0, 32, (uint64_t)val
);
982 env
->htimedelta
= val
;
987 #if defined(TARGET_RISCV32)
988 static int read_htimedeltah(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
990 if (!env
->rdtime_fn
) {
994 *val
= env
->htimedelta
>> 32;
998 static int write_htimedeltah(CPURISCVState
*env
, int csrno
, target_ulong val
)
1000 if (!env
->rdtime_fn
) {
1004 env
->htimedelta
= deposit64(env
->htimedelta
, 32, 32, (uint64_t)val
);
1009 /* Virtual CSR Registers */
1010 static int read_vsstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1012 *val
= env
->vsstatus
;
1016 static int write_vsstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
1018 env
->vsstatus
= val
;
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
);
1030 static int read_vsie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1032 *val
= env
->mie
& env
->mideleg
& VS_MODE_INTERRUPTS
;
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
)
1048 static int write_vstvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
1054 static int read_vsscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1056 *val
= env
->vsscratch
;
1060 static int write_vsscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
1062 env
->vsscratch
= val
;
1066 static int read_vsepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1072 static int write_vsepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
1078 static int read_vscause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1080 *val
= env
->vscause
;
1084 static int write_vscause(CPURISCVState
*env
, int csrno
, target_ulong val
)
1090 static int read_vstval(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1096 static int write_vstval(CPURISCVState
*env
, int csrno
, target_ulong val
)
1102 static int read_vsatp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1108 static int write_vsatp(CPURISCVState
*env
, int csrno
, target_ulong val
)
1114 static int read_mtval2(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1120 static int write_mtval2(CPURISCVState
*env
, int csrno
, target_ulong val
)
1126 static int read_mtinst(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1132 static int write_mtinst(CPURISCVState
*env
, int csrno
, target_ulong val
)
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
);
1145 static int write_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong val
)
1147 pmpcfg_csr_write(env
, csrno
- CSR_PMPCFG0
, val
);
1151 static int read_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1153 *val
= pmpaddr_csr_read(env
, csrno
- CSR_PMPADDR0
);
1157 static int write_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
1159 pmpaddr_csr_write(env
, csrno
- CSR_PMPADDR0
, val
);
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
)
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
1197 if ((write_mask
&& read_only
) ||
1198 (!env
->debugger
&& (effective_priv
< get_field(csrno
, 0x300)))) {
1203 /* ensure the CSR extension is enabled. */
1204 if (!cpu
->cfg
.ext_icsr
) {
1208 /* check predicate */
1209 if (!csr_ops
[csrno
].predicate
|| csr_ops
[csrno
].predicate(env
, csrno
) < 0) {
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
) {
1223 /* read old value */
1224 ret
= csr_ops
[csrno
].read(env
, csrno
, &old_value
);
1229 /* write value if writable and write mask set, otherwise drop writes */
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
);
1240 /* return old value */
1242 *ret_value
= old_value
;
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
)
1256 #if !defined(CONFIG_USER_ONLY)
1257 env
->debugger
= true;
1259 ret
= riscv_csrrw(env
, csrno
, ret_value
, new_value
, write_mask
);
1260 #if !defined(CONFIG_USER_ONLY)
1261 env
->debugger
= false;
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
},
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
},
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
},
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
},
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
},
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
},
1382 #endif /* !CONFIG_USER_ONLY */