2 * RISC-V emulation helpers for qemu.
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 "exec/exec-all.h"
26 #define RISCV_DEBUG_INTERRUPT 0
28 int riscv_cpu_mmu_index(CPURISCVState
*env
, bool ifetch
)
30 #ifdef CONFIG_USER_ONLY
37 #ifndef CONFIG_USER_ONLY
38 static int riscv_cpu_local_irq_pending(CPURISCVState
*env
)
40 target_ulong mstatus_mie
= get_field(env
->mstatus
, MSTATUS_MIE
);
41 target_ulong mstatus_sie
= get_field(env
->mstatus
, MSTATUS_SIE
);
42 target_ulong pending
= atomic_read(&env
->mip
) & env
->mie
;
43 target_ulong mie
= env
->priv
< PRV_M
|| (env
->priv
== PRV_M
&& mstatus_mie
);
44 target_ulong sie
= env
->priv
< PRV_S
|| (env
->priv
== PRV_S
&& mstatus_sie
);
45 target_ulong irqs
= (pending
& ~env
->mideleg
& -mie
) |
46 (pending
& env
->mideleg
& -sie
);
49 return ctz64(irqs
); /* since non-zero */
51 return EXCP_NONE
; /* indicates no pending interrupt */
56 bool riscv_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
58 #if !defined(CONFIG_USER_ONLY)
59 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
60 RISCVCPU
*cpu
= RISCV_CPU(cs
);
61 CPURISCVState
*env
= &cpu
->env
;
62 int interruptno
= riscv_cpu_local_irq_pending(env
);
63 if (interruptno
>= 0) {
64 cs
->exception_index
= RISCV_EXCP_INT_FLAG
| interruptno
;
65 riscv_cpu_do_interrupt(cs
);
73 #if !defined(CONFIG_USER_ONLY)
75 /* get_physical_address - get the physical address for this virtual address
77 * Do a page table walk to obtain the physical address corresponding to a
78 * virtual address. Returns 0 if the translation was successful
80 * Adapted from Spike's mmu_t::translate and mmu_t::walk
83 static int get_physical_address(CPURISCVState
*env
, hwaddr
*physical
,
84 int *prot
, target_ulong addr
,
85 int access_type
, int mmu_idx
)
87 /* NOTE: the env->pc value visible here will not be
88 * correct, but the value visible to the exception handler
89 * (riscv_cpu_do_interrupt) is correct */
93 if (mode
== PRV_M
&& access_type
!= MMU_INST_FETCH
) {
94 if (get_field(env
->mstatus
, MSTATUS_MPRV
)) {
95 mode
= get_field(env
->mstatus
, MSTATUS_MPP
);
99 if (mode
== PRV_M
|| !riscv_feature(env
, RISCV_FEATURE_MMU
)) {
101 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
102 return TRANSLATE_SUCCESS
;
108 int levels
, ptidxbits
, ptesize
, vm
, sum
;
109 int mxr
= get_field(env
->mstatus
, MSTATUS_MXR
);
111 if (env
->priv_ver
>= PRIV_VERSION_1_10_0
) {
112 base
= get_field(env
->satp
, SATP_PPN
) << PGSHIFT
;
113 sum
= get_field(env
->mstatus
, MSTATUS_SUM
);
114 vm
= get_field(env
->satp
, SATP_MODE
);
117 levels
= 2; ptidxbits
= 10; ptesize
= 4; break;
119 levels
= 3; ptidxbits
= 9; ptesize
= 8; break;
121 levels
= 4; ptidxbits
= 9; ptesize
= 8; break;
123 levels
= 5; ptidxbits
= 9; ptesize
= 8; break;
126 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
127 return TRANSLATE_SUCCESS
;
129 g_assert_not_reached();
132 base
= env
->sptbr
<< PGSHIFT
;
133 sum
= !get_field(env
->mstatus
, MSTATUS_PUM
);
134 vm
= get_field(env
->mstatus
, MSTATUS_VM
);
137 levels
= 2; ptidxbits
= 10; ptesize
= 4; break;
139 levels
= 3; ptidxbits
= 9; ptesize
= 8; break;
141 levels
= 4; ptidxbits
= 9; ptesize
= 8; break;
144 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
145 return TRANSLATE_SUCCESS
;
147 g_assert_not_reached();
151 CPUState
*cs
= CPU(riscv_env_get_cpu(env
));
152 int va_bits
= PGSHIFT
+ levels
* ptidxbits
;
153 target_ulong mask
= (1L << (TARGET_LONG_BITS
- (va_bits
- 1))) - 1;
154 target_ulong masked_msbs
= (addr
>> (va_bits
- 1)) & mask
;
155 if (masked_msbs
!= 0 && masked_msbs
!= mask
) {
156 return TRANSLATE_FAIL
;
159 int ptshift
= (levels
- 1) * ptidxbits
;
162 #if !TCG_OVERSIZED_GUEST
165 for (i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
166 target_ulong idx
= (addr
>> (PGSHIFT
+ ptshift
)) &
167 ((1 << ptidxbits
) - 1);
169 /* check that physical address of PTE is legal */
170 target_ulong pte_addr
= base
+ idx
* ptesize
;
171 #if defined(TARGET_RISCV32)
172 target_ulong pte
= ldl_phys(cs
->as
, pte_addr
);
173 #elif defined(TARGET_RISCV64)
174 target_ulong pte
= ldq_phys(cs
->as
, pte_addr
);
176 target_ulong ppn
= pte
>> PTE_PPN_SHIFT
;
178 if (!(pte
& PTE_V
)) {
180 return TRANSLATE_FAIL
;
181 } else if (!(pte
& (PTE_R
| PTE_W
| PTE_X
))) {
182 /* Inner PTE, continue walking */
183 base
= ppn
<< PGSHIFT
;
184 } else if ((pte
& (PTE_R
| PTE_W
| PTE_X
)) == PTE_W
) {
185 /* Reserved leaf PTE flags: PTE_W */
186 return TRANSLATE_FAIL
;
187 } else if ((pte
& (PTE_R
| PTE_W
| PTE_X
)) == (PTE_W
| PTE_X
)) {
188 /* Reserved leaf PTE flags: PTE_W + PTE_X */
189 return TRANSLATE_FAIL
;
190 } else if ((pte
& PTE_U
) && ((mode
!= PRV_U
) &&
191 (!sum
|| access_type
== MMU_INST_FETCH
))) {
192 /* User PTE flags when not U mode and mstatus.SUM is not set,
193 or the access type is an instruction fetch */
194 return TRANSLATE_FAIL
;
195 } else if (!(pte
& PTE_U
) && (mode
!= PRV_S
)) {
196 /* Supervisor PTE flags when not S mode */
197 return TRANSLATE_FAIL
;
198 } else if (ppn
& ((1ULL << ptshift
) - 1)) {
200 return TRANSLATE_FAIL
;
201 } else if (access_type
== MMU_DATA_LOAD
&& !((pte
& PTE_R
) ||
202 ((pte
& PTE_X
) && mxr
))) {
203 /* Read access check failed */
204 return TRANSLATE_FAIL
;
205 } else if (access_type
== MMU_DATA_STORE
&& !(pte
& PTE_W
)) {
206 /* Write access check failed */
207 return TRANSLATE_FAIL
;
208 } else if (access_type
== MMU_INST_FETCH
&& !(pte
& PTE_X
)) {
209 /* Fetch access check failed */
210 return TRANSLATE_FAIL
;
212 /* if necessary, set accessed and dirty bits. */
213 target_ulong updated_pte
= pte
| PTE_A
|
214 (access_type
== MMU_DATA_STORE
? PTE_D
: 0);
216 /* Page table updates need to be atomic with MTTCG enabled */
217 if (updated_pte
!= pte
) {
219 * - if accessed or dirty bits need updating, and the PTE is
220 * in RAM, then we do so atomically with a compare and swap.
221 * - if the PTE is in IO space or ROM, then it can't be updated
222 * and we return TRANSLATE_FAIL.
223 * - if the PTE changed by the time we went to update it, then
224 * it is no longer valid and we must re-walk the page table.
227 hwaddr l
= sizeof(target_ulong
), addr1
;
228 mr
= address_space_translate(cs
->as
, pte_addr
,
229 &addr1
, &l
, false, MEMTXATTRS_UNSPECIFIED
);
230 if (memory_region_is_ram(mr
)) {
231 target_ulong
*pte_pa
=
232 qemu_map_ram_ptr(mr
->ram_block
, addr1
);
233 #if TCG_OVERSIZED_GUEST
234 /* MTTCG is not enabled on oversized TCG guests so
235 * page table updates do not need to be atomic */
236 *pte_pa
= pte
= updated_pte
;
238 target_ulong old_pte
=
239 atomic_cmpxchg(pte_pa
, pte
, updated_pte
);
240 if (old_pte
!= pte
) {
247 /* misconfigured PTE in ROM (AD bits are not preset) or
248 * PTE is in IO space and can't be updated atomically */
249 return TRANSLATE_FAIL
;
253 /* for superpage mappings, make a fake leaf PTE for the TLB's
255 target_ulong vpn
= addr
>> PGSHIFT
;
256 *physical
= (ppn
| (vpn
& ((1L << ptshift
) - 1))) << PGSHIFT
;
258 /* set permissions on the TLB entry */
259 if ((pte
& PTE_R
) || ((pte
& PTE_X
) && mxr
)) {
265 /* add write permission on stores or if the page is already dirty,
266 so that we TLB miss on later writes to update the dirty bit */
268 (access_type
== MMU_DATA_STORE
|| (pte
& PTE_D
))) {
271 return TRANSLATE_SUCCESS
;
274 return TRANSLATE_FAIL
;
277 static void raise_mmu_exception(CPURISCVState
*env
, target_ulong address
,
278 MMUAccessType access_type
)
280 CPUState
*cs
= CPU(riscv_env_get_cpu(env
));
281 int page_fault_exceptions
=
282 (env
->priv_ver
>= PRIV_VERSION_1_10_0
) &&
283 get_field(env
->satp
, SATP_MODE
) != VM_1_10_MBARE
;
284 switch (access_type
) {
286 cs
->exception_index
= page_fault_exceptions
?
287 RISCV_EXCP_INST_PAGE_FAULT
: RISCV_EXCP_INST_ACCESS_FAULT
;
290 cs
->exception_index
= page_fault_exceptions
?
291 RISCV_EXCP_LOAD_PAGE_FAULT
: RISCV_EXCP_LOAD_ACCESS_FAULT
;
294 cs
->exception_index
= page_fault_exceptions
?
295 RISCV_EXCP_STORE_PAGE_FAULT
: RISCV_EXCP_STORE_AMO_ACCESS_FAULT
;
298 g_assert_not_reached();
300 env
->badaddr
= address
;
303 hwaddr
riscv_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
305 RISCVCPU
*cpu
= RISCV_CPU(cs
);
308 int mmu_idx
= cpu_mmu_index(&cpu
->env
, false);
310 if (get_physical_address(&cpu
->env
, &phys_addr
, &prot
, addr
, 0, mmu_idx
)) {
316 void riscv_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
317 MMUAccessType access_type
, int mmu_idx
,
320 RISCVCPU
*cpu
= RISCV_CPU(cs
);
321 CPURISCVState
*env
= &cpu
->env
;
322 switch (access_type
) {
324 cs
->exception_index
= RISCV_EXCP_INST_ADDR_MIS
;
327 cs
->exception_index
= RISCV_EXCP_LOAD_ADDR_MIS
;
330 cs
->exception_index
= RISCV_EXCP_STORE_AMO_ADDR_MIS
;
333 g_assert_not_reached();
336 do_raise_exception_err(env
, cs
->exception_index
, retaddr
);
339 /* called by qemu's softmmu to fill the qemu tlb */
340 void tlb_fill(CPUState
*cs
, target_ulong addr
, int size
,
341 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
344 ret
= riscv_cpu_handle_mmu_fault(cs
, addr
, size
, access_type
, mmu_idx
);
345 if (ret
== TRANSLATE_FAIL
) {
346 RISCVCPU
*cpu
= RISCV_CPU(cs
);
347 CPURISCVState
*env
= &cpu
->env
;
348 do_raise_exception_err(env
, cs
->exception_index
, retaddr
);
354 int riscv_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int size
,
357 RISCVCPU
*cpu
= RISCV_CPU(cs
);
358 CPURISCVState
*env
= &cpu
->env
;
359 #if !defined(CONFIG_USER_ONLY)
363 int ret
= TRANSLATE_FAIL
;
365 qemu_log_mask(CPU_LOG_MMU
,
366 "%s pc " TARGET_FMT_lx
" ad %" VADDR_PRIx
" rw %d mmu_idx \
367 %d\n", __func__
, env
->pc
, address
, rw
, mmu_idx
);
369 #if !defined(CONFIG_USER_ONLY)
370 ret
= get_physical_address(env
, &pa
, &prot
, address
, rw
, mmu_idx
);
371 qemu_log_mask(CPU_LOG_MMU
,
372 "%s address=%" VADDR_PRIx
" ret %d physical " TARGET_FMT_plx
373 " prot %d\n", __func__
, address
, ret
, pa
, prot
);
374 if (!pmp_hart_has_privs(env
, pa
, TARGET_PAGE_SIZE
, 1 << rw
)) {
375 ret
= TRANSLATE_FAIL
;
377 if (ret
== TRANSLATE_SUCCESS
) {
378 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
, pa
& TARGET_PAGE_MASK
,
379 prot
, mmu_idx
, TARGET_PAGE_SIZE
);
380 } else if (ret
== TRANSLATE_FAIL
) {
381 raise_mmu_exception(env
, address
, rw
);
386 cs
->exception_index
= RISCV_EXCP_INST_PAGE_FAULT
;
389 cs
->exception_index
= RISCV_EXCP_LOAD_PAGE_FAULT
;
392 cs
->exception_index
= RISCV_EXCP_STORE_PAGE_FAULT
;
402 * Adapted from Spike's processor_t::take_trap.
405 void riscv_cpu_do_interrupt(CPUState
*cs
)
407 #if !defined(CONFIG_USER_ONLY)
409 RISCVCPU
*cpu
= RISCV_CPU(cs
);
410 CPURISCVState
*env
= &cpu
->env
;
412 if (RISCV_DEBUG_INTERRUPT
) {
413 int log_cause
= cs
->exception_index
& RISCV_EXCP_INT_MASK
;
414 if (cs
->exception_index
& RISCV_EXCP_INT_FLAG
) {
415 qemu_log_mask(LOG_TRACE
, "core 0: trap %s, epc 0x" TARGET_FMT_lx
,
416 riscv_intr_names
[log_cause
], env
->pc
);
418 qemu_log_mask(LOG_TRACE
, "core 0: intr %s, epc 0x" TARGET_FMT_lx
,
419 riscv_excp_names
[log_cause
], env
->pc
);
423 target_ulong fixed_cause
= 0;
424 if (cs
->exception_index
& (RISCV_EXCP_INT_FLAG
)) {
425 /* hacky for now. the MSB (bit 63) indicates interrupt but cs->exception
426 index is only 32 bits wide */
427 fixed_cause
= cs
->exception_index
& RISCV_EXCP_INT_MASK
;
428 fixed_cause
|= ((target_ulong
)1) << (TARGET_LONG_BITS
- 1);
430 /* fixup User ECALL -> correct priv ECALL */
431 if (cs
->exception_index
== RISCV_EXCP_U_ECALL
) {
434 fixed_cause
= RISCV_EXCP_U_ECALL
;
437 fixed_cause
= RISCV_EXCP_S_ECALL
;
440 fixed_cause
= RISCV_EXCP_H_ECALL
;
443 fixed_cause
= RISCV_EXCP_M_ECALL
;
447 fixed_cause
= cs
->exception_index
;
451 target_ulong backup_epc
= env
->pc
;
453 target_ulong bit
= fixed_cause
;
454 target_ulong deleg
= env
->medeleg
;
457 (fixed_cause
== RISCV_EXCP_INST_ADDR_MIS
) ||
458 (fixed_cause
== RISCV_EXCP_INST_ACCESS_FAULT
) ||
459 (fixed_cause
== RISCV_EXCP_LOAD_ADDR_MIS
) ||
460 (fixed_cause
== RISCV_EXCP_STORE_AMO_ADDR_MIS
) ||
461 (fixed_cause
== RISCV_EXCP_LOAD_ACCESS_FAULT
) ||
462 (fixed_cause
== RISCV_EXCP_STORE_AMO_ACCESS_FAULT
) ||
463 (fixed_cause
== RISCV_EXCP_INST_PAGE_FAULT
) ||
464 (fixed_cause
== RISCV_EXCP_LOAD_PAGE_FAULT
) ||
465 (fixed_cause
== RISCV_EXCP_STORE_PAGE_FAULT
);
467 if (bit
& ((target_ulong
)1 << (TARGET_LONG_BITS
- 1))) {
468 deleg
= env
->mideleg
;
469 bit
&= ~((target_ulong
)1 << (TARGET_LONG_BITS
- 1));
472 if (env
->priv
<= PRV_S
&& bit
< 64 && ((deleg
>> bit
) & 1)) {
473 /* handle the trap in S-mode */
474 /* No need to check STVEC for misaligned - lower 2 bits cannot be set */
475 env
->pc
= env
->stvec
;
476 env
->scause
= fixed_cause
;
477 env
->sepc
= backup_epc
;
480 if (RISCV_DEBUG_INTERRUPT
) {
481 qemu_log_mask(LOG_TRACE
, "core " TARGET_FMT_ld
482 ": badaddr 0x" TARGET_FMT_lx
, env
->mhartid
, env
->badaddr
);
484 env
->sbadaddr
= env
->badaddr
;
486 /* otherwise we must clear sbadaddr/stval
487 * todo: support populating stval on illegal instructions */
491 target_ulong s
= env
->mstatus
;
492 s
= set_field(s
, MSTATUS_SPIE
, env
->priv_ver
>= PRIV_VERSION_1_10_0
?
493 get_field(s
, MSTATUS_SIE
) : get_field(s
, MSTATUS_UIE
<< env
->priv
));
494 s
= set_field(s
, MSTATUS_SPP
, env
->priv
);
495 s
= set_field(s
, MSTATUS_SIE
, 0);
496 csr_write_helper(env
, s
, CSR_MSTATUS
);
497 riscv_set_mode(env
, PRV_S
);
499 /* No need to check MTVEC for misaligned - lower 2 bits cannot be set */
500 env
->pc
= env
->mtvec
;
501 env
->mepc
= backup_epc
;
502 env
->mcause
= fixed_cause
;
505 if (RISCV_DEBUG_INTERRUPT
) {
506 qemu_log_mask(LOG_TRACE
, "core " TARGET_FMT_ld
507 ": badaddr 0x" TARGET_FMT_lx
, env
->mhartid
, env
->badaddr
);
509 env
->mbadaddr
= env
->badaddr
;
511 /* otherwise we must clear mbadaddr/mtval
512 * todo: support populating mtval on illegal instructions */
516 target_ulong s
= env
->mstatus
;
517 s
= set_field(s
, MSTATUS_MPIE
, env
->priv_ver
>= PRIV_VERSION_1_10_0
?
518 get_field(s
, MSTATUS_MIE
) : get_field(s
, MSTATUS_UIE
<< env
->priv
));
519 s
= set_field(s
, MSTATUS_MPP
, env
->priv
);
520 s
= set_field(s
, MSTATUS_MIE
, 0);
521 csr_write_helper(env
, s
, CSR_MSTATUS
);
522 riscv_set_mode(env
, PRV_M
);
524 /* TODO yield load reservation */
526 cs
->exception_index
= EXCP_NONE
; /* mark handled to qemu */