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 /* loose check condition for fcsr in vector extension */
50 if ((csrno
== CSR_FCSR
) && (env
->misa
& RVV
)) {
53 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
60 static int vs(CPURISCVState
*env
, int csrno
)
62 if (env
->misa
& RVV
) {
68 static int ctr(CPURISCVState
*env
, int csrno
)
70 #if !defined(CONFIG_USER_ONLY)
71 CPUState
*cs
= env_cpu(env
);
72 RISCVCPU
*cpu
= RISCV_CPU(cs
);
74 if (!cpu
->cfg
.ext_counters
) {
75 /* The Counters extensions is not enabled */
82 #if !defined(CONFIG_USER_ONLY)
83 static int any(CPURISCVState
*env
, int csrno
)
88 static int smode(CPURISCVState
*env
, int csrno
)
90 return -!riscv_has_ext(env
, RVS
);
93 static int hmode(CPURISCVState
*env
, int csrno
)
95 if (riscv_has_ext(env
, RVS
) &&
96 riscv_has_ext(env
, RVH
)) {
97 /* Hypervisor extension is supported */
98 if ((env
->priv
== PRV_S
&& !riscv_cpu_virt_enabled(env
)) ||
107 static int pmp(CPURISCVState
*env
, int csrno
)
109 return -!riscv_feature(env
, RISCV_FEATURE_PMP
);
113 /* User Floating-Point CSRs */
114 static int read_fflags(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
116 #if !defined(CONFIG_USER_ONLY)
117 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
121 *val
= riscv_cpu_get_fflags(env
);
125 static int write_fflags(CPURISCVState
*env
, int csrno
, target_ulong val
)
127 #if !defined(CONFIG_USER_ONLY)
128 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
131 env
->mstatus
|= MSTATUS_FS
;
133 riscv_cpu_set_fflags(env
, val
& (FSR_AEXC
>> FSR_AEXC_SHIFT
));
137 static int read_frm(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
139 #if !defined(CONFIG_USER_ONLY)
140 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
148 static int write_frm(CPURISCVState
*env
, int csrno
, target_ulong val
)
150 #if !defined(CONFIG_USER_ONLY)
151 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
154 env
->mstatus
|= MSTATUS_FS
;
156 env
->frm
= val
& (FSR_RD
>> FSR_RD_SHIFT
);
160 static int read_fcsr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
162 #if !defined(CONFIG_USER_ONLY)
163 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
167 *val
= (riscv_cpu_get_fflags(env
) << FSR_AEXC_SHIFT
)
168 | (env
->frm
<< FSR_RD_SHIFT
);
169 if (vs(env
, csrno
) >= 0) {
170 *val
|= (env
->vxrm
<< FSR_VXRM_SHIFT
)
171 | (env
->vxsat
<< FSR_VXSAT_SHIFT
);
176 static int write_fcsr(CPURISCVState
*env
, int csrno
, target_ulong val
)
178 #if !defined(CONFIG_USER_ONLY)
179 if (!env
->debugger
&& !riscv_cpu_fp_enabled(env
)) {
182 env
->mstatus
|= MSTATUS_FS
;
184 env
->frm
= (val
& FSR_RD
) >> FSR_RD_SHIFT
;
185 if (vs(env
, csrno
) >= 0) {
186 env
->vxrm
= (val
& FSR_VXRM
) >> FSR_VXRM_SHIFT
;
187 env
->vxsat
= (val
& FSR_VXSAT
) >> FSR_VXSAT_SHIFT
;
189 riscv_cpu_set_fflags(env
, (val
& FSR_AEXC
) >> FSR_AEXC_SHIFT
);
193 static int read_vtype(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
199 static int read_vl(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
205 static int read_vxrm(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
211 static int write_vxrm(CPURISCVState
*env
, int csrno
, target_ulong val
)
217 static int read_vxsat(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
223 static int write_vxsat(CPURISCVState
*env
, int csrno
, target_ulong val
)
229 static int read_vstart(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
235 static int write_vstart(CPURISCVState
*env
, int csrno
, target_ulong val
)
241 /* User Timers and Counters */
242 static int read_instret(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
244 #if !defined(CONFIG_USER_ONLY)
246 *val
= cpu_get_icount();
248 *val
= cpu_get_host_ticks();
251 *val
= cpu_get_host_ticks();
256 #if defined(TARGET_RISCV32)
257 static int read_instreth(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
259 #if !defined(CONFIG_USER_ONLY)
261 *val
= cpu_get_icount() >> 32;
263 *val
= cpu_get_host_ticks() >> 32;
266 *val
= cpu_get_host_ticks() >> 32;
270 #endif /* TARGET_RISCV32 */
272 #if defined(CONFIG_USER_ONLY)
273 static int read_time(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
275 *val
= cpu_get_host_ticks();
279 #if defined(TARGET_RISCV32)
280 static int read_timeh(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
282 *val
= cpu_get_host_ticks() >> 32;
287 #else /* CONFIG_USER_ONLY */
289 static int read_time(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
291 uint64_t delta
= riscv_cpu_virt_enabled(env
) ? env
->htimedelta
: 0;
293 if (!env
->rdtime_fn
) {
297 *val
= env
->rdtime_fn() + delta
;
301 #if defined(TARGET_RISCV32)
302 static int read_timeh(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
304 uint64_t delta
= riscv_cpu_virt_enabled(env
) ? env
->htimedelta
: 0;
306 if (!env
->rdtime_fn
) {
310 *val
= (env
->rdtime_fn() + delta
) >> 32;
315 /* Machine constants */
317 #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)
318 #define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP)
319 #define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)
321 static const target_ulong delegable_ints
= S_MODE_INTERRUPTS
|
323 static const target_ulong all_ints
= M_MODE_INTERRUPTS
| S_MODE_INTERRUPTS
|
325 static const target_ulong delegable_excps
=
326 (1ULL << (RISCV_EXCP_INST_ADDR_MIS
)) |
327 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT
)) |
328 (1ULL << (RISCV_EXCP_ILLEGAL_INST
)) |
329 (1ULL << (RISCV_EXCP_BREAKPOINT
)) |
330 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS
)) |
331 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT
)) |
332 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS
)) |
333 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT
)) |
334 (1ULL << (RISCV_EXCP_U_ECALL
)) |
335 (1ULL << (RISCV_EXCP_S_ECALL
)) |
336 (1ULL << (RISCV_EXCP_VS_ECALL
)) |
337 (1ULL << (RISCV_EXCP_M_ECALL
)) |
338 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT
)) |
339 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT
)) |
340 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT
)) |
341 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT
)) |
342 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT
)) |
343 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT
));
344 static const target_ulong sstatus_v1_10_mask
= SSTATUS_SIE
| SSTATUS_SPIE
|
345 SSTATUS_UIE
| SSTATUS_UPIE
| SSTATUS_SPP
| SSTATUS_FS
| SSTATUS_XS
|
346 SSTATUS_SUM
| SSTATUS_MXR
| SSTATUS_SD
;
347 static const target_ulong sip_writable_mask
= SIP_SSIP
| MIP_USIP
| MIP_UEIP
;
348 static const target_ulong hip_writable_mask
= MIP_VSSIP
| MIP_VSTIP
| MIP_VSEIP
;
349 static const target_ulong vsip_writable_mask
= MIP_VSSIP
;
351 #if defined(TARGET_RISCV32)
352 static const char valid_vm_1_10
[16] = {
356 #elif defined(TARGET_RISCV64)
357 static const char valid_vm_1_10
[16] = {
363 #endif /* CONFIG_USER_ONLY */
365 /* Machine Information Registers */
366 static int read_zero(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
371 static int read_mhartid(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
377 /* Machine Trap Setup */
378 static int read_mstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
384 static int validate_vm(CPURISCVState
*env
, target_ulong vm
)
386 return valid_vm_1_10
[vm
& 0xf];
389 static int write_mstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
391 target_ulong mstatus
= env
->mstatus
;
392 target_ulong mask
= 0;
395 /* flush tlb on mstatus fields that affect VM */
396 if ((val
^ mstatus
) & (MSTATUS_MXR
| MSTATUS_MPP
| MSTATUS_MPV
|
397 MSTATUS_MPRV
| MSTATUS_SUM
)) {
398 tlb_flush(env_cpu(env
));
400 mask
= MSTATUS_SIE
| MSTATUS_SPIE
| MSTATUS_MIE
| MSTATUS_MPIE
|
401 MSTATUS_SPP
| MSTATUS_FS
| MSTATUS_MPRV
| MSTATUS_SUM
|
402 MSTATUS_MPP
| MSTATUS_MXR
| MSTATUS_TVM
| MSTATUS_TSR
|
404 #if defined(TARGET_RISCV64)
406 * RV32: MPV and MTL are not in mstatus. The current plan is to
407 * add them to mstatush. For now, we just don't support it.
409 mask
|= MSTATUS_MTL
| MSTATUS_MPV
;
412 mstatus
= (mstatus
& ~mask
) | (val
& mask
);
414 dirty
= ((mstatus
& MSTATUS_FS
) == MSTATUS_FS
) |
415 ((mstatus
& MSTATUS_XS
) == MSTATUS_XS
);
416 mstatus
= set_field(mstatus
, MSTATUS_SD
, dirty
);
417 env
->mstatus
= mstatus
;
422 #ifdef TARGET_RISCV32
423 static int read_mstatush(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
425 *val
= env
->mstatush
;
429 static int write_mstatush(CPURISCVState
*env
, int csrno
, target_ulong val
)
431 if ((val
^ env
->mstatush
) & (MSTATUS_MPV
)) {
432 tlb_flush(env_cpu(env
));
435 val
&= MSTATUS_MPV
| MSTATUS_MTL
;
443 static int read_misa(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
449 static int write_misa(CPURISCVState
*env
, int csrno
, target_ulong val
)
451 if (!riscv_feature(env
, RISCV_FEATURE_MISA
)) {
452 /* drop write to misa */
456 /* 'I' or 'E' must be present */
457 if (!(val
& (RVI
| RVE
))) {
458 /* It is not, drop write to misa */
462 /* 'E' excludes all other extensions */
464 /* when we support 'E' we can do "val = RVE;" however
465 * for now we just drop writes if 'E' is present.
470 /* Mask extensions that are not supported by this hart */
471 val
&= env
->misa_mask
;
473 /* Mask extensions that are not supported by QEMU */
474 val
&= (RVI
| RVE
| RVM
| RVA
| RVF
| RVD
| RVC
| RVS
| RVU
);
476 /* 'D' depends on 'F', so clear 'D' if 'F' is not present */
477 if ((val
& RVD
) && !(val
& RVF
)) {
481 /* Suppress 'C' if next instruction is not aligned
482 * TODO: this should check next_pc
484 if ((val
& RVC
) && (GETPC() & ~3) != 0) {
488 /* misa.MXL writes are not supported by QEMU */
489 val
= (env
->misa
& MISA_MXL
) | (val
& ~MISA_MXL
);
491 /* flush translation cache */
492 if (val
!= env
->misa
) {
493 tb_flush(env_cpu(env
));
501 static int read_medeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
507 static int write_medeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
509 env
->medeleg
= (env
->medeleg
& ~delegable_excps
) | (val
& delegable_excps
);
513 static int read_mideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
519 static int write_mideleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
521 env
->mideleg
= (env
->mideleg
& ~delegable_ints
) | (val
& delegable_ints
);
522 if (riscv_has_ext(env
, RVH
)) {
523 env
->mideleg
|= VS_MODE_INTERRUPTS
;
528 static int read_mie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
534 static int write_mie(CPURISCVState
*env
, int csrno
, target_ulong val
)
536 env
->mie
= (env
->mie
& ~all_ints
) | (val
& all_ints
);
540 static int read_mtvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
546 static int write_mtvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
548 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
552 qemu_log_mask(LOG_UNIMP
, "CSR_MTVEC: reserved mode not supported\n");
557 static int read_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
559 *val
= env
->mcounteren
;
563 static int write_mcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
565 env
->mcounteren
= val
;
569 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
570 static int read_mscounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
572 if (env
->priv_ver
< PRIV_VERSION_1_11_0
) {
575 *val
= env
->mcounteren
;
579 /* This regiser is replaced with CSR_MCOUNTINHIBIT in 1.11.0 */
580 static int write_mscounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
582 if (env
->priv_ver
< PRIV_VERSION_1_11_0
) {
585 env
->mcounteren
= val
;
589 /* Machine Trap Handling */
590 static int read_mscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
592 *val
= env
->mscratch
;
596 static int write_mscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
602 static int read_mepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
608 static int write_mepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
614 static int read_mcause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
620 static int write_mcause(CPURISCVState
*env
, int csrno
, target_ulong val
)
626 static int read_mbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
628 *val
= env
->mbadaddr
;
632 static int write_mbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
638 static int rmw_mip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
639 target_ulong new_value
, target_ulong write_mask
)
641 RISCVCPU
*cpu
= env_archcpu(env
);
642 /* Allow software control of delegable interrupts not claimed by hardware */
643 target_ulong mask
= write_mask
& delegable_ints
& ~env
->miclaim
;
647 old_mip
= riscv_cpu_update_mip(cpu
, mask
, (new_value
& mask
));
653 *ret_value
= old_mip
;
659 /* Supervisor Trap Setup */
660 static int read_sstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
662 target_ulong mask
= (sstatus_v1_10_mask
);
663 *val
= env
->mstatus
& mask
;
667 static int write_sstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
669 target_ulong mask
= (sstatus_v1_10_mask
);
670 target_ulong newval
= (env
->mstatus
& ~mask
) | (val
& mask
);
671 return write_mstatus(env
, CSR_MSTATUS
, newval
);
674 static int read_sie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
676 if (riscv_cpu_virt_enabled(env
)) {
677 /* Tell the guest the VS bits, shifted to the S bit locations */
678 *val
= (env
->mie
& env
->mideleg
& VS_MODE_INTERRUPTS
) >> 1;
680 *val
= env
->mie
& env
->mideleg
;
685 static int write_sie(CPURISCVState
*env
, int csrno
, target_ulong val
)
689 if (riscv_cpu_virt_enabled(env
)) {
690 /* Shift the guests S bits to VS */
691 newval
= (env
->mie
& ~VS_MODE_INTERRUPTS
) |
692 ((val
<< 1) & VS_MODE_INTERRUPTS
);
694 newval
= (env
->mie
& ~S_MODE_INTERRUPTS
) | (val
& S_MODE_INTERRUPTS
);
697 return write_mie(env
, CSR_MIE
, newval
);
700 static int read_stvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
706 static int write_stvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
708 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */
712 qemu_log_mask(LOG_UNIMP
, "CSR_STVEC: reserved mode not supported\n");
717 static int read_scounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
719 *val
= env
->scounteren
;
723 static int write_scounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
725 env
->scounteren
= val
;
729 /* Supervisor Trap Handling */
730 static int read_sscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
732 *val
= env
->sscratch
;
736 static int write_sscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
742 static int read_sepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
748 static int write_sepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
754 static int read_scause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
760 static int write_scause(CPURISCVState
*env
, int csrno
, target_ulong val
)
766 static int read_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
768 *val
= env
->sbadaddr
;
772 static int write_sbadaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
778 static int rmw_sip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
779 target_ulong new_value
, target_ulong write_mask
)
783 if (riscv_cpu_virt_enabled(env
)) {
784 /* Shift the new values to line up with the VS bits */
785 ret
= rmw_mip(env
, CSR_MSTATUS
, ret_value
, new_value
<< 1,
786 (write_mask
& sip_writable_mask
) << 1 & env
->mideleg
);
787 ret
&= vsip_writable_mask
;
790 ret
= rmw_mip(env
, CSR_MSTATUS
, ret_value
, new_value
,
791 write_mask
& env
->mideleg
& sip_writable_mask
);
794 *ret_value
&= env
->mideleg
;
798 /* Supervisor Protection and Translation */
799 static int read_satp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
801 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
806 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
815 static int write_satp(CPURISCVState
*env
, int csrno
, target_ulong val
)
817 if (!riscv_feature(env
, RISCV_FEATURE_MMU
)) {
820 if (validate_vm(env
, get_field(val
, SATP_MODE
)) &&
821 ((val
^ env
->satp
) & (SATP_MODE
| SATP_ASID
| SATP_PPN
)))
823 if (env
->priv
== PRV_S
&& get_field(env
->mstatus
, MSTATUS_TVM
)) {
826 if((val
^ env
->satp
) & SATP_ASID
) {
827 tlb_flush(env_cpu(env
));
835 /* Hypervisor Extensions */
836 static int read_hstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
842 static int write_hstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
848 static int read_hedeleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
854 static int write_hedeleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
860 static int read_hideleg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
866 static int write_hideleg(CPURISCVState
*env
, int csrno
, target_ulong val
)
872 static int rmw_hip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
873 target_ulong new_value
, target_ulong write_mask
)
875 int ret
= rmw_mip(env
, 0, ret_value
, new_value
,
876 write_mask
& hip_writable_mask
);
881 static int read_hie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
883 *val
= env
->mie
& VS_MODE_INTERRUPTS
;
887 static int write_hie(CPURISCVState
*env
, int csrno
, target_ulong val
)
889 target_ulong newval
= (env
->mie
& ~VS_MODE_INTERRUPTS
) | (val
& VS_MODE_INTERRUPTS
);
890 return write_mie(env
, CSR_MIE
, newval
);
893 static int read_hcounteren(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
895 *val
= env
->hcounteren
;
899 static int write_hcounteren(CPURISCVState
*env
, int csrno
, target_ulong val
)
901 env
->hcounteren
= val
;
905 static int read_htval(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
911 static int write_htval(CPURISCVState
*env
, int csrno
, target_ulong val
)
917 static int read_htinst(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
923 static int write_htinst(CPURISCVState
*env
, int csrno
, target_ulong val
)
929 static int read_hgatp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
935 static int write_hgatp(CPURISCVState
*env
, int csrno
, target_ulong val
)
941 static int read_htimedelta(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
943 if (!env
->rdtime_fn
) {
947 #if defined(TARGET_RISCV32)
948 *val
= env
->htimedelta
& 0xffffffff;
950 *val
= env
->htimedelta
;
955 static int write_htimedelta(CPURISCVState
*env
, int csrno
, target_ulong val
)
957 if (!env
->rdtime_fn
) {
961 #if defined(TARGET_RISCV32)
962 env
->htimedelta
= deposit64(env
->htimedelta
, 0, 32, (uint64_t)val
);
964 env
->htimedelta
= val
;
969 #if defined(TARGET_RISCV32)
970 static int read_htimedeltah(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
972 if (!env
->rdtime_fn
) {
976 *val
= env
->htimedelta
>> 32;
980 static int write_htimedeltah(CPURISCVState
*env
, int csrno
, target_ulong val
)
982 if (!env
->rdtime_fn
) {
986 env
->htimedelta
= deposit64(env
->htimedelta
, 32, 32, (uint64_t)val
);
991 /* Virtual CSR Registers */
992 static int read_vsstatus(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
994 *val
= env
->vsstatus
;
998 static int write_vsstatus(CPURISCVState
*env
, int csrno
, target_ulong val
)
1000 env
->vsstatus
= val
;
1004 static int rmw_vsip(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
1005 target_ulong new_value
, target_ulong write_mask
)
1007 int ret
= rmw_mip(env
, 0, ret_value
, new_value
,
1008 write_mask
& env
->mideleg
& vsip_writable_mask
);
1012 static int read_vsie(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1014 *val
= env
->mie
& env
->mideleg
& VS_MODE_INTERRUPTS
;
1018 static int write_vsie(CPURISCVState
*env
, int csrno
, target_ulong val
)
1020 target_ulong newval
= (env
->mie
& ~env
->mideleg
) | (val
& env
->mideleg
& MIP_VSSIP
);
1021 return write_mie(env
, CSR_MIE
, newval
);
1024 static int read_vstvec(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1030 static int write_vstvec(CPURISCVState
*env
, int csrno
, target_ulong val
)
1036 static int read_vsscratch(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1038 *val
= env
->vsscratch
;
1042 static int write_vsscratch(CPURISCVState
*env
, int csrno
, target_ulong val
)
1044 env
->vsscratch
= val
;
1048 static int read_vsepc(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1054 static int write_vsepc(CPURISCVState
*env
, int csrno
, target_ulong val
)
1060 static int read_vscause(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1062 *val
= env
->vscause
;
1066 static int write_vscause(CPURISCVState
*env
, int csrno
, target_ulong val
)
1072 static int read_vstval(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1078 static int write_vstval(CPURISCVState
*env
, int csrno
, target_ulong val
)
1084 static int read_vsatp(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1090 static int write_vsatp(CPURISCVState
*env
, int csrno
, target_ulong val
)
1096 static int read_mtval2(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1102 static int write_mtval2(CPURISCVState
*env
, int csrno
, target_ulong val
)
1108 static int read_mtinst(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1114 static int write_mtinst(CPURISCVState
*env
, int csrno
, target_ulong val
)
1120 /* Physical Memory Protection */
1121 static int read_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1123 *val
= pmpcfg_csr_read(env
, csrno
- CSR_PMPCFG0
);
1127 static int write_pmpcfg(CPURISCVState
*env
, int csrno
, target_ulong val
)
1129 pmpcfg_csr_write(env
, csrno
- CSR_PMPCFG0
, val
);
1133 static int read_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong
*val
)
1135 *val
= pmpaddr_csr_read(env
, csrno
- CSR_PMPADDR0
);
1139 static int write_pmpaddr(CPURISCVState
*env
, int csrno
, target_ulong val
)
1141 pmpaddr_csr_write(env
, csrno
- CSR_PMPADDR0
, val
);
1148 * riscv_csrrw - read and/or update control and status register
1150 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0);
1151 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1);
1152 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value);
1153 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value);
1156 int riscv_csrrw(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
1157 target_ulong new_value
, target_ulong write_mask
)
1160 target_ulong old_value
;
1161 RISCVCPU
*cpu
= env_archcpu(env
);
1163 /* check privileges and return -1 if check fails */
1164 #if !defined(CONFIG_USER_ONLY)
1165 int effective_priv
= env
->priv
;
1166 int read_only
= get_field(csrno
, 0xC00) == 3;
1168 if (riscv_has_ext(env
, RVH
) &&
1169 env
->priv
== PRV_S
&&
1170 !riscv_cpu_virt_enabled(env
)) {
1172 * We are in S mode without virtualisation, therefore we are in HS Mode.
1173 * Add 1 to the effective privledge level to allow us to access the
1179 if ((write_mask
&& read_only
) ||
1180 (!env
->debugger
&& (effective_priv
< get_field(csrno
, 0x300)))) {
1185 /* ensure the CSR extension is enabled. */
1186 if (!cpu
->cfg
.ext_icsr
) {
1190 /* check predicate */
1191 if (!csr_ops
[csrno
].predicate
|| csr_ops
[csrno
].predicate(env
, csrno
) < 0) {
1195 /* execute combined read/write operation if it exists */
1196 if (csr_ops
[csrno
].op
) {
1197 return csr_ops
[csrno
].op(env
, csrno
, ret_value
, new_value
, write_mask
);
1200 /* if no accessor exists then return failure */
1201 if (!csr_ops
[csrno
].read
) {
1205 /* read old value */
1206 ret
= csr_ops
[csrno
].read(env
, csrno
, &old_value
);
1211 /* write value if writable and write mask set, otherwise drop writes */
1213 new_value
= (old_value
& ~write_mask
) | (new_value
& write_mask
);
1214 if (csr_ops
[csrno
].write
) {
1215 ret
= csr_ops
[csrno
].write(env
, csrno
, new_value
);
1222 /* return old value */
1224 *ret_value
= old_value
;
1231 * Debugger support. If not in user mode, set env->debugger before the
1232 * riscv_csrrw call and clear it after the call.
1234 int riscv_csrrw_debug(CPURISCVState
*env
, int csrno
, target_ulong
*ret_value
,
1235 target_ulong new_value
, target_ulong write_mask
)
1238 #if !defined(CONFIG_USER_ONLY)
1239 env
->debugger
= true;
1241 ret
= riscv_csrrw(env
, csrno
, ret_value
, new_value
, write_mask
);
1242 #if !defined(CONFIG_USER_ONLY)
1243 env
->debugger
= false;
1248 /* Control and Status Register function table */
1249 static riscv_csr_operations csr_ops
[CSR_TABLE_SIZE
] = {
1250 /* User Floating-Point CSRs */
1251 [CSR_FFLAGS
] = { fs
, read_fflags
, write_fflags
},
1252 [CSR_FRM
] = { fs
, read_frm
, write_frm
},
1253 [CSR_FCSR
] = { fs
, read_fcsr
, write_fcsr
},
1255 [CSR_VSTART
] = { vs
, read_vstart
, write_vstart
},
1256 [CSR_VXSAT
] = { vs
, read_vxsat
, write_vxsat
},
1257 [CSR_VXRM
] = { vs
, read_vxrm
, write_vxrm
},
1258 [CSR_VL
] = { vs
, read_vl
},
1259 [CSR_VTYPE
] = { vs
, read_vtype
},
1260 /* User Timers and Counters */
1261 [CSR_CYCLE
] = { ctr
, read_instret
},
1262 [CSR_INSTRET
] = { ctr
, read_instret
},
1263 #if defined(TARGET_RISCV32)
1264 [CSR_CYCLEH
] = { ctr
, read_instreth
},
1265 [CSR_INSTRETH
] = { ctr
, read_instreth
},
1268 /* In privileged mode, the monitor will have to emulate TIME CSRs only if
1269 * rdtime callback is not provided by machine/platform emulation */
1270 [CSR_TIME
] = { ctr
, read_time
},
1271 #if defined(TARGET_RISCV32)
1272 [CSR_TIMEH
] = { ctr
, read_timeh
},
1275 #if !defined(CONFIG_USER_ONLY)
1276 /* Machine Timers and Counters */
1277 [CSR_MCYCLE
] = { any
, read_instret
},
1278 [CSR_MINSTRET
] = { any
, read_instret
},
1279 #if defined(TARGET_RISCV32)
1280 [CSR_MCYCLEH
] = { any
, read_instreth
},
1281 [CSR_MINSTRETH
] = { any
, read_instreth
},
1284 /* Machine Information Registers */
1285 [CSR_MVENDORID
] = { any
, read_zero
},
1286 [CSR_MARCHID
] = { any
, read_zero
},
1287 [CSR_MIMPID
] = { any
, read_zero
},
1288 [CSR_MHARTID
] = { any
, read_mhartid
},
1290 /* Machine Trap Setup */
1291 [CSR_MSTATUS
] = { any
, read_mstatus
, write_mstatus
},
1292 [CSR_MISA
] = { any
, read_misa
, write_misa
},
1293 [CSR_MIDELEG
] = { any
, read_mideleg
, write_mideleg
},
1294 [CSR_MEDELEG
] = { any
, read_medeleg
, write_medeleg
},
1295 [CSR_MIE
] = { any
, read_mie
, write_mie
},
1296 [CSR_MTVEC
] = { any
, read_mtvec
, write_mtvec
},
1297 [CSR_MCOUNTEREN
] = { any
, read_mcounteren
, write_mcounteren
},
1299 #if defined(TARGET_RISCV32)
1300 [CSR_MSTATUSH
] = { any
, read_mstatush
, write_mstatush
},
1303 [CSR_MSCOUNTEREN
] = { any
, read_mscounteren
, write_mscounteren
},
1305 /* Machine Trap Handling */
1306 [CSR_MSCRATCH
] = { any
, read_mscratch
, write_mscratch
},
1307 [CSR_MEPC
] = { any
, read_mepc
, write_mepc
},
1308 [CSR_MCAUSE
] = { any
, read_mcause
, write_mcause
},
1309 [CSR_MBADADDR
] = { any
, read_mbadaddr
, write_mbadaddr
},
1310 [CSR_MIP
] = { any
, NULL
, NULL
, rmw_mip
},
1312 /* Supervisor Trap Setup */
1313 [CSR_SSTATUS
] = { smode
, read_sstatus
, write_sstatus
},
1314 [CSR_SIE
] = { smode
, read_sie
, write_sie
},
1315 [CSR_STVEC
] = { smode
, read_stvec
, write_stvec
},
1316 [CSR_SCOUNTEREN
] = { smode
, read_scounteren
, write_scounteren
},
1318 /* Supervisor Trap Handling */
1319 [CSR_SSCRATCH
] = { smode
, read_sscratch
, write_sscratch
},
1320 [CSR_SEPC
] = { smode
, read_sepc
, write_sepc
},
1321 [CSR_SCAUSE
] = { smode
, read_scause
, write_scause
},
1322 [CSR_SBADADDR
] = { smode
, read_sbadaddr
, write_sbadaddr
},
1323 [CSR_SIP
] = { smode
, NULL
, NULL
, rmw_sip
},
1325 /* Supervisor Protection and Translation */
1326 [CSR_SATP
] = { smode
, read_satp
, write_satp
},
1328 [CSR_HSTATUS
] = { hmode
, read_hstatus
, write_hstatus
},
1329 [CSR_HEDELEG
] = { hmode
, read_hedeleg
, write_hedeleg
},
1330 [CSR_HIDELEG
] = { hmode
, read_hideleg
, write_hideleg
},
1331 [CSR_HIP
] = { hmode
, NULL
, NULL
, rmw_hip
},
1332 [CSR_HIE
] = { hmode
, read_hie
, write_hie
},
1333 [CSR_HCOUNTEREN
] = { hmode
, read_hcounteren
, write_hcounteren
},
1334 [CSR_HTVAL
] = { hmode
, read_htval
, write_htval
},
1335 [CSR_HTINST
] = { hmode
, read_htinst
, write_htinst
},
1336 [CSR_HGATP
] = { hmode
, read_hgatp
, write_hgatp
},
1337 [CSR_HTIMEDELTA
] = { hmode
, read_htimedelta
, write_htimedelta
},
1338 #if defined(TARGET_RISCV32)
1339 [CSR_HTIMEDELTAH
] = { hmode
, read_htimedeltah
, write_htimedeltah
},
1342 [CSR_VSSTATUS
] = { hmode
, read_vsstatus
, write_vsstatus
},
1343 [CSR_VSIP
] = { hmode
, NULL
, NULL
, rmw_vsip
},
1344 [CSR_VSIE
] = { hmode
, read_vsie
, write_vsie
},
1345 [CSR_VSTVEC
] = { hmode
, read_vstvec
, write_vstvec
},
1346 [CSR_VSSCRATCH
] = { hmode
, read_vsscratch
, write_vsscratch
},
1347 [CSR_VSEPC
] = { hmode
, read_vsepc
, write_vsepc
},
1348 [CSR_VSCAUSE
] = { hmode
, read_vscause
, write_vscause
},
1349 [CSR_VSTVAL
] = { hmode
, read_vstval
, write_vstval
},
1350 [CSR_VSATP
] = { hmode
, read_vsatp
, write_vsatp
},
1352 [CSR_MTVAL2
] = { hmode
, read_mtval2
, write_mtval2
},
1353 [CSR_MTINST
] = { hmode
, read_mtinst
, write_mtinst
},
1355 /* Physical Memory Protection */
1356 [CSR_PMPCFG0
... CSR_PMPADDR9
] = { pmp
, read_pmpcfg
, write_pmpcfg
},
1357 [CSR_PMPADDR0
... CSR_PMPADDR15
] = { pmp
, read_pmpaddr
, write_pmpaddr
},
1359 /* Performance Counters */
1360 [CSR_HPMCOUNTER3
... CSR_HPMCOUNTER31
] = { ctr
, read_zero
},
1361 [CSR_MHPMCOUNTER3
... CSR_MHPMCOUNTER31
] = { any
, read_zero
},
1362 [CSR_MHPMEVENT3
... CSR_MHPMEVENT31
] = { any
, read_zero
},
1363 #if defined(TARGET_RISCV32)
1364 [CSR_HPMCOUNTER3H
... CSR_HPMCOUNTER31H
] = { ctr
, read_zero
},
1365 [CSR_MHPMCOUNTER3H
... CSR_MHPMCOUNTER31H
] = { any
, read_zero
},
1367 #endif /* !CONFIG_USER_ONLY */