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
);
62 if (!cpu
->cfg
.ext_counters
) {
63 /* The Counters extensions is not enabled */
70 #if !defined(CONFIG_USER_ONLY)
71 static int any(CPURISCVState
*env
, int csrno
)
76 static int smode(CPURISCVState
*env
, int csrno
)
78 return -!riscv_has_ext(env
, RVS
);
81 static int hmode(CPURISCVState
*env
, int csrno
)
83 if (riscv_has_ext(env
, RVS
) &&
84 riscv_has_ext(env
, RVH
)) {
85 /* Hypervisor extension is supported */
86 if ((env
->priv
== PRV_S
&& !riscv_cpu_virt_enabled(env
)) ||
95 static int pmp(CPURISCVState
*env
, int csrno
)
97 return -!riscv_feature(env
, RISCV_FEATURE_PMP
);
101 /* User Floating-Point CSRs */
102 static int read_fflags(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
104 #if !defined(CONFIG_USER_ONLY)
105 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
109 *val
= riscv_cpu_get_fflags(env
);
113 static int write_fflags(CPURISCVState
*env
, int csrno
, target_ulong val
)
115 #if !defined(CONFIG_USER_ONLY)
116 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
119 env
->mstatus
|= MSTATUS_FS
;
121 riscv_cpu_set_fflags(env
, val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
));
125 static int read_frm(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
127 #if !defined(CONFIG_USER_ONLY)
128 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
136 static int write_frm(CPURISCVState
*env
, int csrno
, target_ulong val
)
138 #if !defined(CONFIG_USER_ONLY)
139 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
142 env
->mstatus
|= MSTATUS_FS
;
144 env
->frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
148 static int read_fcsr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
150 #if !defined(CONFIG_USER_ONLY)
151 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
155 *val
= (riscv_cpu_get_fflags(env
) << FSR_AEXC_SHIFT
)
156 | (env
->frm
<< FSR_RD_SHIFT
);
160 static int write_fcsr(CPURISCVState
*env
, int csrno
, target_ulong val
)
162 #if !defined(CONFIG_USER_ONLY)
163 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
166 env
->mstatus
|= MSTATUS_FS
;
168 env
->frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
169 riscv_cpu_set_fflags(env
, (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
);
173 /* User Timers and Counters */
174 static int read_instret(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
176 #if !defined(CONFIG_USER_ONLY)
178 *val
= cpu_get_icount();
180 *val
= cpu_get_host_ticks();
183 *val
= cpu_get_host_ticks();
188 #if defined(TARGET_RISCV32)
189 static int read_instreth(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
191 #if !defined(CONFIG_USER_ONLY)
193 *val
= cpu_get_icount() >> 32;
195 *val
= cpu_get_host_ticks() >> 32;
198 *val
= cpu_get_host_ticks() >> 32;
202 #endif /* TARGET_RISCV32 */
204 #if defined(CONFIG_USER_ONLY)
205 static int read_time(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
207 *val
= cpu_get_host_ticks();
211 #if defined(TARGET_RISCV32)
212 static int read_timeh(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
214 *val
= cpu_get_host_ticks() >> 32;
219 #else /* CONFIG_USER_ONLY */
221 static int read_time(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
223 uint64_t delta
= riscv_cpu_virt_enabled(env
) ? env
->htimedelta
: 0;
225 if (!env
->rdtime_fn
) {
229 *val
= env
->rdtime_fn() + delta
;
233 #if defined(TARGET_RISCV32)
234 static int read_timeh(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
236 uint64_t delta
= riscv_cpu_virt_enabled(env
) ? env
->htimedelta
: 0;
238 if (!env
->rdtime_fn
) {
242 *val
= (env
->rdtime_fn() + delta
) >> 32;
247 /* Machine constants */
249 #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)
250 #define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)
251 #define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)
253 static const target_ulong delegable_ints
= S_MODE_INTERRUPTS
|
255 static const target_ulong all_ints
= M_MODE_INTERRUPTS
| S_MODE_INTERRUPTS
|
257 static const target_ulong delegable_excps
=
258 (1ULL << (RISCV_EXCP_INST_ADDR_MIS
)) |
259 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT
)) |
260 (1ULL << (RISCV_EXCP_ILLEGAL_INST
)) |
261 (1ULL << (RISCV_EXCP_BREAKPOINT
)) |
262 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS
)) |
263 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT
)) |
264 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS
)) |
265 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT
)) |
266 (1ULL << (RISCV_EXCP_U_ECALL
)) |
267 (1ULL << (RISCV_EXCP_S_ECALL
)) |
268 (1ULL << (RISCV_EXCP_VS_ECALL
)) |
269 (1ULL << (RISCV_EXCP_M_ECALL
)) |
270 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT
)) |
271 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT
)) |
272 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT
)) |
273 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT
)) |
274 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT
)) |
275 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT
));
276 static const target_ulong sstatus_v1_10_mask
= SSTATUS_SIE
| SSTATUS_SPIE
|
277 SSTATUS_UIE
| SSTATUS_UPIE
| SSTATUS_SPP
| SSTATUS_FS
| SSTATUS_XS
|
278 SSTATUS_SUM
| SSTATUS_MXR
| SSTATUS_SD
;
279 static const target_ulong sip_writable_mask
= SIP_SSIP
| MIP_USIP
| MIP_UEIP
;
280 static const target_ulong hip_writable_mask
= MIP_VSSIP
| MIP_VSTIP
| MIP_VSEIP
;
281 static const target_ulong vsip_writable_mask
= MIP_VSSIP
;
283 #if defined(TARGET_RISCV32)
284 static const char valid_vm_1_10
[16] = {
288 #elif defined(TARGET_RISCV64)
289 static const char valid_vm_1_10
[16] = {
295 #endif /* CONFIG_USER_ONLY */
297 /* Machine Information Registers */
298 static int read_zero(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
303 static int read_mhartid(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
309 /* Machine Trap Setup */
310 static int read_mstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
316 static int validate_vm(CPURISCVState
*env
, target_ulong vm
)
318 return valid_vm_1_10
[vm
& 0xf];
321 static int write_mstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
323 target_ulong mstatus
= env
->mstatus
;
324 target_ulong mask
= 0;
327 /* flush tlb on mstatus fields that affect VM */
328 if ((val
^ mstatus
) & (MSTATUS_MXR
| MSTATUS_MPP
| MSTATUS_MPV
|
329 MSTATUS_MPRV
| MSTATUS_SUM
)) {
330 tlb_flush(env_cpu(env
));
332 mask
= MSTATUS_SIE
| MSTATUS_SPIE
| MSTATUS_MIE
| MSTATUS_MPIE
|
333 MSTATUS_SPP
| MSTATUS_FS
| MSTATUS_MPRV
| MSTATUS_SUM
|
334 MSTATUS_MPP
| MSTATUS_MXR
| MSTATUS_TVM
| MSTATUS_TSR
|
336 #if defined(TARGET_RISCV64)
338 * RV32: MPV and MTL are not in mstatus. The current plan is to
339 * add them to mstatush. For now, we just don't support it.
341 mask
|= MSTATUS_MTL
| MSTATUS_MPV
;
344 mstatus
= (mstatus
& ~mask
) | (val
& mask
);
346 dirty
= ((mstatus
& MSTATUS_FS
) == MSTATUS_FS
) |
347 ((mstatus
& MSTATUS_XS
) == MSTATUS_XS
);
348 mstatus
= set_field(mstatus
, MSTATUS_SD
, dirty
);
349 env
->mstatus
= mstatus
;
354 #ifdef TARGET_RISCV32
355 static int read_mstatush(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
357 *val
= env
->mstatush
;
361 static int write_mstatush(CPURISCVState
*env
, int csrno
, target_ulong val
)
363 if ((val
^ env
->mstatush
) & (MSTATUS_MPV
)) {
364 tlb_flush(env_cpu(env
));
367 val
&= MSTATUS_MPV
| MSTATUS_MTL
;
375 static int read_misa(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
381 static int write_misa(CPURISCVState
*env
, int csrno
, target_ulong val
)
383 if (!riscv_feature(env
, RISCV_FEATURE_MISA
)) {
384 /* drop write to misa */
388 /* 'I' or 'E' must be present */
389 if (!(val
& (RVI
| RVE
))) {
390 /* It is not, drop write to misa */
394 /* 'E' excludes all other extensions */
396 /* when we support 'E' we can do "val = RVE;" however
397 * for now we just drop writes if 'E' is present.
402 /* Mask extensions that are not supported by this hart */
403 val
&= env
->misa_mask
;
405 /* Mask extensions that are not supported by QEMU */
406 val
&= (RVI
| RVE
| RVM
| RVA
| RVF
| RVD
| RVC
| RVS
| RVU
);
408 /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
409 if ((val
& RVD
) && !(val
& RVF
)) {
413 /* Suppress 'C' if next instruction is not aligned
414 * TODO: this should check next_pc
416 if ((val
& RVC
) && (GETPC() & ~3) != 0) {
420 /* misa.MXL writes are not supported by QEMU */
421 val
= (env
->misa
& MISA_MXL
) | (val
& ~MISA_MXL
);
423 /* flush translation cache */
424 if (val
!= env
->misa
) {
425 tb_flush(env_cpu(env
));
433 static int read_medeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
439 static int write_medeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
441 env
->medeleg
= (env
->medeleg
& ~delegable_excps
) | (val
& delegable_excps
);
445 static int read_mideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
451 static int write_mideleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
453 env
->mideleg
= (env
->mideleg
& ~delegable_ints
) | (val
& delegable_ints
);
454 if (riscv_has_ext(env
, RVH
)) {
455 env
->mideleg
|= VS_MODE_INTERRUPTS
;
460 static int read_mie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
466 static int write_mie(CPURISCVState
*env
, int csrno
, target_ulong val
)
468 env
->mie
= (env
->mie
& ~all_ints
) | (val
& all_ints
);
472 static int read_mtvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
478 static int write_mtvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
480 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
484 qemu_log_mask(LOG_UNIMP
, "CSR_MTVEC: reserved mode not supported\n");
489 static int read_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
491 *val
= env
->mcounteren
;
495 static int write_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
497 env
->mcounteren
= val
;
501 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
502 static int read_mscounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
504 if (env
->priv_ver
< PRIV_VERSION_1_11_0
) {
507 *val
= env
->mcounteren
;
511 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
512 static int write_mscounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
514 if (env
->priv_ver
< PRIV_VERSION_1_11_0
) {
517 env
->mcounteren
= 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
= (sstatus_v1_10_mask
);
595 *val
= env
->mstatus
& mask
;
599 static int write_sstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
601 target_ulong mask
= (sstatus_v1_10_mask
);
602 target_ulong newval
= (env
->mstatus
& ~mask
) | (val
& mask
);
603 return write_mstatus(env
, CSR_MSTATUS
, newval
);
606 static int read_sie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
608 if (riscv_cpu_virt_enabled(env
)) {
609 /* Tell the guest the VS bits, shifted to the S bit locations */
610 *val
= (env
->mie
& env
->mideleg
& VS_MODE_INTERRUPTS
) >> 1;
612 *val
= env
->mie
& env
->mideleg
;
617 static int write_sie(CPURISCVState
*env
, int csrno
, target_ulong val
)
621 if (riscv_cpu_virt_enabled(env
)) {
622 /* Shift the guests S bits to VS */
623 newval
= (env
->mie
& ~VS_MODE_INTERRUPTS
) |
624 ((val
<< 1) & VS_MODE_INTERRUPTS
);
626 newval
= (env
->mie
& ~S_MODE_INTERRUPTS
) | (val
& S_MODE_INTERRUPTS
);
629 return write_mie(env
, CSR_MIE
, newval
);
632 static int read_stvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
638 static int write_stvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
640 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
644 qemu_log_mask(LOG_UNIMP
, "CSR_STVEC: reserved mode not supported\n");
649 static int read_scounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
651 *val
= env
->scounteren
;
655 static int write_scounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
657 env
->scounteren
= val
;
661 /* Supervisor Trap Handling */
662 static int read_sscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
664 *val
= env
->sscratch
;
668 static int write_sscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
674 static int read_sepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
680 static int write_sepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
686 static int read_scause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
692 static int write_scause(CPURISCVState
*env
, int csrno
, target_ulong val
)
698 static int read_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
700 *val
= env
->sbadaddr
;
704 static int write_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
710 static int rmw_sip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
711 target_ulong new_value
, target_ulong write_mask
)
715 if (riscv_cpu_virt_enabled(env
)) {
716 /* Shift the new values to line up with the VS bits */
717 ret
= rmw_mip(env
, CSR_MSTATUS
, ret_value
, new_value
<< 1,
718 (write_mask
& sip_writable_mask
) << 1 & env
->mideleg
);
719 ret
&= vsip_writable_mask
;
722 ret
= rmw_mip(env
, CSR_MSTATUS
, ret_value
, new_value
,
723 write_mask
& env
->mideleg
& sip_writable_mask
);
726 *ret_value
&= env
->mideleg
;
730 /* Supervisor Protection and Translation */
731 static int read_satp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
733 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
738 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
747 static int write_satp(CPURISCVState
*env
, int csrno
, target_ulong val
)
749 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
752 if (validate_vm(env
, get_field(val
, SATP_MODE
)) &&
753 ((val
^ env
->satp
) & (SATP_MODE
| SATP_ASID
| SATP_PPN
)))
755 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
758 if((val
^ env
->satp
) & SATP_ASID
) {
759 tlb_flush(env_cpu(env
));
767 /* Hypervisor Extensions */
768 static int read_hstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
774 static int write_hstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
780 static int read_hedeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
786 static int write_hedeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
792 static int read_hideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
798 static int write_hideleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
804 static int rmw_hip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
805 target_ulong new_value
, target_ulong write_mask
)
807 int ret
= rmw_mip(env
, 0, ret_value
, new_value
,
808 write_mask
& hip_writable_mask
);
813 static int read_hie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
815 *val
= env
->mie
& VS_MODE_INTERRUPTS
;
819 static int write_hie(CPURISCVState
*env
, int csrno
, target_ulong val
)
821 target_ulong newval
= (env
->mie
& ~VS_MODE_INTERRUPTS
) | (val
& VS_MODE_INTERRUPTS
);
822 return write_mie(env
, CSR_MIE
, newval
);
825 static int read_hcounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
827 *val
= env
->hcounteren
;
831 static int write_hcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
833 env
->hcounteren
= val
;
837 static int read_htval(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
843 static int write_htval(CPURISCVState
*env
, int csrno
, target_ulong val
)
849 static int read_htinst(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
855 static int write_htinst(CPURISCVState
*env
, int csrno
, target_ulong val
)
861 static int read_hgatp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
867 static int write_hgatp(CPURISCVState
*env
, int csrno
, target_ulong val
)
873 static int read_htimedelta(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
875 if (!env
->rdtime_fn
) {
879 #if defined(TARGET_RISCV32)
880 *val
= env
->htimedelta
& 0xffffffff;
882 *val
= env
->htimedelta
;
887 static int write_htimedelta(CPURISCVState
*env
, int csrno
, target_ulong val
)
889 if (!env
->rdtime_fn
) {
893 #if defined(TARGET_RISCV32)
894 env
->htimedelta
= deposit64(env
->htimedelta
, 0, 32, (uint64_t)val
);
896 env
->htimedelta
= val
;
901 #if defined(TARGET_RISCV32)
902 static int read_htimedeltah(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
904 if (!env
->rdtime_fn
) {
908 *val
= env
->htimedelta
>> 32;
912 static int write_htimedeltah(CPURISCVState
*env
, int csrno
, target_ulong val
)
914 if (!env
->rdtime_fn
) {
918 env
->htimedelta
= deposit64(env
->htimedelta
, 32, 32, (uint64_t)val
);
923 /* Virtual CSR Registers */
924 static int read_vsstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
926 *val
= env
->vsstatus
;
930 static int write_vsstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
936 static int rmw_vsip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
937 target_ulong new_value
, target_ulong write_mask
)
939 int ret
= rmw_mip(env
, 0, ret_value
, new_value
,
940 write_mask
& env
->mideleg
& vsip_writable_mask
);
944 static int read_vsie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
946 *val
= env
->mie
& env
->mideleg
& VS_MODE_INTERRUPTS
;
950 static int write_vsie(CPURISCVState
*env
, int csrno
, target_ulong val
)
952 target_ulong newval
= (env
->mie
& ~env
->mideleg
) | (val
& env
->mideleg
& MIP_VSSIP
);
953 return write_mie(env
, CSR_MIE
, newval
);
956 static int read_vstvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
962 static int write_vstvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
968 static int read_vsscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
970 *val
= env
->vsscratch
;
974 static int write_vsscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
976 env
->vsscratch
= val
;
980 static int read_vsepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
986 static int write_vsepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
992 static int read_vscause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
998 static int write_vscause(CPURISCVState
*env
, int csrno
, target_ulong val
)
1004 static int read_vstval(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1010 static int write_vstval(CPURISCVState
*env
, int csrno
, target_ulong val
)
1016 static int read_vsatp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1022 static int write_vsatp(CPURISCVState
*env
, int csrno
, target_ulong val
)
1028 static int read_mtval2(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1034 static int write_mtval2(CPURISCVState
*env
, int csrno
, target_ulong val
)
1040 static int read_mtinst(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1046 static int write_mtinst(CPURISCVState
*env
, int csrno
, target_ulong val
)
1052 /* Physical Memory Protection */
1053 static int read_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1055 *val
= pmpcfg_csr_read(env
, csrno
- CSR_PMPCFG0
);
1059 static int write_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong val
)
1061 pmpcfg_csr_write(env
, csrno
- CSR_PMPCFG0
, val
);
1065 static int read_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1067 *val
= pmpaddr_csr_read(env
, csrno
- CSR_PMPADDR0
);
1071 static int write_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
1073 pmpaddr_csr_write(env
, csrno
- CSR_PMPADDR0
, val
);
1080 * riscv_csrrw - read and/or update control and status register
1082 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0);
1083 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1);
1084 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value);
1085 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value);
1088 int riscv_csrrw(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
1089 target_ulong new_value
, target_ulong write_mask
)
1092 target_ulong old_value
;
1093 RISCVCPU
*cpu
= env_archcpu(env
);
1095 /* check privileges and return -1 if check fails */
1096 #if !defined(CONFIG_USER_ONLY)
1097 int effective_priv
= env
->priv
;
1098 int read_only
= get_field(csrno
, 0xC00) == 3;
1100 if (riscv_has_ext(env
, RVH
) &&
1101 env
->priv
== PRV_S
&&
1102 !riscv_cpu_virt_enabled(env
)) {
1104 * We are in S mode without virtualisation, therefore we are in HS Mode.
1105 * Add 1 to the effective privledge level to allow us to access the
1111 if ((write_mask
&& read_only
) ||
1112 (!env
->debugger
&& (effective_priv
< get_field(csrno
, 0x300)))) {
1117 /* ensure the CSR extension is enabled. */
1118 if (!cpu
->cfg
.ext_icsr
) {
1122 /* check predicate */
1123 if (!csr_ops
[csrno
].predicate
|| csr_ops
[csrno
].predicate(env
, csrno
) < 0) {
1127 /* execute combined read/write operation if it exists */
1128 if (csr_ops
[csrno
].op
) {
1129 return csr_ops
[csrno
].op(env
, csrno
, ret_value
, new_value
, write_mask
);
1132 /* if no accessor exists then return failure */
1133 if (!csr_ops
[csrno
].read
) {
1137 /* read old value */
1138 ret
= csr_ops
[csrno
].read(env
, csrno
, &old_value
);
1143 /* write value if writable and write mask set, otherwise drop writes */
1145 new_value
= (old_value
& ~write_mask
) | (new_value
& write_mask
);
1146 if (csr_ops
[csrno
].write
) {
1147 ret
= csr_ops
[csrno
].write(env
, csrno
, new_value
);
1154 /* return old value */
1156 *ret_value
= old_value
;
1163 * Debugger support. If not in user mode, set env->debugger before the
1164 * riscv_csrrw call and clear it after the call.
1166 int riscv_csrrw_debug(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
1167 target_ulong new_value
, target_ulong write_mask
)
1170 #if !defined(CONFIG_USER_ONLY)
1171 env
->debugger
= true;
1173 ret
= riscv_csrrw(env
, csrno
, ret_value
, new_value
, write_mask
);
1174 #if !defined(CONFIG_USER_ONLY)
1175 env
->debugger
= false;
1180 /* Control and Status Register function table */
1181 static riscv_csr_operations csr_ops
[CSR_TABLE_SIZE
] = {
1182 /* User Floating-Point CSRs */
1183 [CSR_FFLAGS
] = { fs
, read_fflags
, write_fflags
},
1184 [CSR_FRM
] = { fs
, read_frm
, write_frm
},
1185 [CSR_FCSR
] = { fs
, read_fcsr
, write_fcsr
},
1187 /* User Timers and Counters */
1188 [CSR_CYCLE
] = { ctr
, read_instret
},
1189 [CSR_INSTRET
] = { ctr
, read_instret
},
1190 #if defined(TARGET_RISCV32)
1191 [CSR_CYCLEH
] = { ctr
, read_instreth
},
1192 [CSR_INSTRETH
] = { ctr
, read_instreth
},
1195 /* In privileged mode, the monitor will have to emulate TIME CSRs only if
1196 * rdtime callback is not provided by machine/platform emulation */
1197 [CSR_TIME
] = { ctr
, read_time
},
1198 #if defined(TARGET_RISCV32)
1199 [CSR_TIMEH
] = { ctr
, read_timeh
},
1202 #if !defined(CONFIG_USER_ONLY)
1203 /* Machine Timers and Counters */
1204 [CSR_MCYCLE
] = { any
, read_instret
},
1205 [CSR_MINSTRET
] = { any
, read_instret
},
1206 #if defined(TARGET_RISCV32)
1207 [CSR_MCYCLEH
] = { any
, read_instreth
},
1208 [CSR_MINSTRETH
] = { any
, read_instreth
},
1211 /* Machine Information Registers */
1212 [CSR_MVENDORID
] = { any
, read_zero
},
1213 [CSR_MARCHID
] = { any
, read_zero
},
1214 [CSR_MIMPID
] = { any
, read_zero
},
1215 [CSR_MHARTID
] = { any
, read_mhartid
},
1217 /* Machine Trap Setup */
1218 [CSR_MSTATUS
] = { any
, read_mstatus
, write_mstatus
},
1219 [CSR_MISA
] = { any
, read_misa
, write_misa
},
1220 [CSR_MIDELEG
] = { any
, read_mideleg
, write_mideleg
},
1221 [CSR_MEDELEG
] = { any
, read_medeleg
, write_medeleg
},
1222 [CSR_MIE
] = { any
, read_mie
, write_mie
},
1223 [CSR_MTVEC
] = { any
, read_mtvec
, write_mtvec
},
1224 [CSR_MCOUNTEREN
] = { any
, read_mcounteren
, write_mcounteren
},
1226 #if defined(TARGET_RISCV32)
1227 [CSR_MSTATUSH
] = { any
, read_mstatush
, write_mstatush
},
1230 [CSR_MSCOUNTEREN
] = { any
, read_mscounteren
, write_mscounteren
},
1232 /* Machine Trap Handling */
1233 [CSR_MSCRATCH
] = { any
, read_mscratch
, write_mscratch
},
1234 [CSR_MEPC
] = { any
, read_mepc
, write_mepc
},
1235 [CSR_MCAUSE
] = { any
, read_mcause
, write_mcause
},
1236 [CSR_MBADADDR
] = { any
, read_mbadaddr
, write_mbadaddr
},
1237 [CSR_MIP
] = { any
, NULL
, NULL
, rmw_mip
},
1239 /* Supervisor Trap Setup */
1240 [CSR_SSTATUS
] = { smode
, read_sstatus
, write_sstatus
},
1241 [CSR_SIE
] = { smode
, read_sie
, write_sie
},
1242 [CSR_STVEC
] = { smode
, read_stvec
, write_stvec
},
1243 [CSR_SCOUNTEREN
] = { smode
, read_scounteren
, write_scounteren
},
1245 /* Supervisor Trap Handling */
1246 [CSR_SSCRATCH
] = { smode
, read_sscratch
, write_sscratch
},
1247 [CSR_SEPC
] = { smode
, read_sepc
, write_sepc
},
1248 [CSR_SCAUSE
] = { smode
, read_scause
, write_scause
},
1249 [CSR_SBADADDR
] = { smode
, read_sbadaddr
, write_sbadaddr
},
1250 [CSR_SIP
] = { smode
, NULL
, NULL
, rmw_sip
},
1252 /* Supervisor Protection and Translation */
1253 [CSR_SATP
] = { smode
, read_satp
, write_satp
},
1255 [CSR_HSTATUS
] = { hmode
, read_hstatus
, write_hstatus
},
1256 [CSR_HEDELEG
] = { hmode
, read_hedeleg
, write_hedeleg
},
1257 [CSR_HIDELEG
] = { hmode
, read_hideleg
, write_hideleg
},
1258 [CSR_HIP
] = { hmode
, NULL
, NULL
, rmw_hip
},
1259 [CSR_HIE
] = { hmode
, read_hie
, write_hie
},
1260 [CSR_HCOUNTEREN
] = { hmode
, read_hcounteren
, write_hcounteren
},
1261 [CSR_HTVAL
] = { hmode
, read_htval
, write_htval
},
1262 [CSR_HTINST
] = { hmode
, read_htinst
, write_htinst
},
1263 [CSR_HGATP
] = { hmode
, read_hgatp
, write_hgatp
},
1264 [CSR_HTIMEDELTA
] = { hmode
, read_htimedelta
, write_htimedelta
},
1265 #if defined(TARGET_RISCV32)
1266 [CSR_HTIMEDELTAH
] = { hmode
, read_htimedeltah
, write_htimedeltah
},
1269 [CSR_VSSTATUS
] = { hmode
, read_vsstatus
, write_vsstatus
},
1270 [CSR_VSIP
] = { hmode
, NULL
, NULL
, rmw_vsip
},
1271 [CSR_VSIE
] = { hmode
, read_vsie
, write_vsie
},
1272 [CSR_VSTVEC
] = { hmode
, read_vstvec
, write_vstvec
},
1273 [CSR_VSSCRATCH
] = { hmode
, read_vsscratch
, write_vsscratch
},
1274 [CSR_VSEPC
] = { hmode
, read_vsepc
, write_vsepc
},
1275 [CSR_VSCAUSE
] = { hmode
, read_vscause
, write_vscause
},
1276 [CSR_VSTVAL
] = { hmode
, read_vstval
, write_vstval
},
1277 [CSR_VSATP
] = { hmode
, read_vsatp
, write_vsatp
},
1279 [CSR_MTVAL2
] = { hmode
, read_mtval2
, write_mtval2
},
1280 [CSR_MTINST
] = { hmode
, read_mtinst
, write_mtinst
},
1282 /* Physical Memory Protection */
1283 [CSR_PMPCFG0
... CSR_PMPADDR9
] = { pmp
, read_pmpcfg
, write_pmpcfg
},
1284 [CSR_PMPADDR0
... CSR_PMPADDR15
] = { pmp
, read_pmpaddr
, write_pmpaddr
},
1286 /* Performance Counters */
1287 [CSR_HPMCOUNTER3
... CSR_HPMCOUNTER31
] = { ctr
, read_zero
},
1288 [CSR_MHPMCOUNTER3
... CSR_MHPMCOUNTER31
] = { any
, read_zero
},
1289 [CSR_MHPMEVENT3
... CSR_MHPMEVENT31
] = { any
, read_zero
},
1290 #if defined(TARGET_RISCV32)
1291 [CSR_HPMCOUNTER3H
... CSR_HPMCOUNTER31H
] = { ctr
, read_zero
},
1292 [CSR_MHPMCOUNTER3H
... CSR_MHPMCOUNTER31H
] = { any
, read_zero
},
1294 #endif /* !CONFIG_USER_ONLY */