]>
Commit | Line | Data |
---|---|---|
0c3e702a | 1 | /* |
df354dd4 | 2 | * RISC-V CPU helpers for qemu. |
0c3e702a MC |
3 | * |
4 | * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu | |
5 | * Copyright (c) 2017-2018 SiFive, Inc. | |
6 | * | |
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. | |
10 | * | |
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 | |
14 | * more details. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
20 | #include "qemu/osdep.h" | |
21 | #include "qemu/log.h" | |
7ec5d303 | 22 | #include "qemu/main-loop.h" |
0c3e702a MC |
23 | #include "cpu.h" |
24 | #include "exec/exec-all.h" | |
dcb32f1d | 25 | #include "tcg/tcg-op.h" |
929f0a7f | 26 | #include "trace.h" |
0c3e702a MC |
27 | |
28 | int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch) | |
29 | { | |
30 | #ifdef CONFIG_USER_ONLY | |
31 | return 0; | |
32 | #else | |
33 | return env->priv; | |
34 | #endif | |
35 | } | |
36 | ||
37 | #ifndef CONFIG_USER_ONLY | |
efbdbc26 | 38 | static int riscv_cpu_local_irq_pending(CPURISCVState *env) |
0c3e702a | 39 | { |
efbdbc26 MC |
40 | target_ulong mstatus_mie = get_field(env->mstatus, MSTATUS_MIE); |
41 | target_ulong mstatus_sie = get_field(env->mstatus, MSTATUS_SIE); | |
7ec5d303 | 42 | target_ulong pending = env->mip & env->mie; |
efbdbc26 MC |
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); | |
0c3e702a | 47 | |
efbdbc26 MC |
48 | if (irqs) { |
49 | return ctz64(irqs); /* since non-zero */ | |
0c3e702a MC |
50 | } else { |
51 | return EXCP_NONE; /* indicates no pending interrupt */ | |
52 | } | |
53 | } | |
54 | #endif | |
55 | ||
56 | bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) | |
57 | { | |
58 | #if !defined(CONFIG_USER_ONLY) | |
59 | if (interrupt_request & CPU_INTERRUPT_HARD) { | |
60 | RISCVCPU *cpu = RISCV_CPU(cs); | |
61 | CPURISCVState *env = &cpu->env; | |
efbdbc26 | 62 | int interruptno = riscv_cpu_local_irq_pending(env); |
0c3e702a MC |
63 | if (interruptno >= 0) { |
64 | cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno; | |
65 | riscv_cpu_do_interrupt(cs); | |
66 | return true; | |
67 | } | |
68 | } | |
69 | #endif | |
70 | return false; | |
71 | } | |
72 | ||
73 | #if !defined(CONFIG_USER_ONLY) | |
74 | ||
b345b480 AF |
75 | /* Return true is floating point support is currently enabled */ |
76 | bool riscv_cpu_fp_enabled(CPURISCVState *env) | |
77 | { | |
78 | if (env->mstatus & MSTATUS_FS) { | |
79 | return true; | |
80 | } | |
81 | ||
82 | return false; | |
83 | } | |
84 | ||
ef6bb7b6 AF |
85 | bool riscv_cpu_virt_enabled(CPURISCVState *env) |
86 | { | |
87 | if (!riscv_has_ext(env, RVH)) { | |
88 | return false; | |
89 | } | |
90 | ||
91 | return get_field(env->virt, VIRT_ONOFF); | |
92 | } | |
93 | ||
94 | void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable) | |
95 | { | |
96 | if (!riscv_has_ext(env, RVH)) { | |
97 | return; | |
98 | } | |
99 | ||
100 | env->virt = set_field(env->virt, VIRT_ONOFF, enable); | |
101 | } | |
102 | ||
e3e7039c MC |
103 | int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts) |
104 | { | |
105 | CPURISCVState *env = &cpu->env; | |
106 | if (env->miclaim & interrupts) { | |
107 | return -1; | |
108 | } else { | |
109 | env->miclaim |= interrupts; | |
110 | return 0; | |
111 | } | |
112 | } | |
113 | ||
df354dd4 MC |
114 | uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) |
115 | { | |
116 | CPURISCVState *env = &cpu->env; | |
0a01f2ee | 117 | CPUState *cs = CPU(cpu); |
7ec5d303 AF |
118 | uint32_t old = env->mip; |
119 | bool locked = false; | |
120 | ||
121 | if (!qemu_mutex_iothread_locked()) { | |
122 | locked = true; | |
123 | qemu_mutex_lock_iothread(); | |
124 | } | |
df354dd4 | 125 | |
7ec5d303 | 126 | env->mip = (env->mip & ~mask) | (value & mask); |
df354dd4 | 127 | |
7ec5d303 AF |
128 | if (env->mip) { |
129 | cpu_interrupt(cs, CPU_INTERRUPT_HARD); | |
130 | } else { | |
131 | cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); | |
132 | } | |
0a01f2ee | 133 | |
7ec5d303 AF |
134 | if (locked) { |
135 | qemu_mutex_unlock_iothread(); | |
136 | } | |
df354dd4 MC |
137 | |
138 | return old; | |
139 | } | |
140 | ||
fb738839 | 141 | void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) |
df354dd4 MC |
142 | { |
143 | if (newpriv > PRV_M) { | |
144 | g_assert_not_reached(); | |
145 | } | |
146 | if (newpriv == PRV_H) { | |
147 | newpriv = PRV_U; | |
148 | } | |
149 | /* tlb_flush is unnecessary as mode is contained in mmu_idx */ | |
150 | env->priv = newpriv; | |
c13b169f JS |
151 | |
152 | /* | |
153 | * Clear the load reservation - otherwise a reservation placed in one | |
154 | * context/process can be used by another, resulting in an SC succeeding | |
155 | * incorrectly. Version 2.2 of the ISA specification explicitly requires | |
156 | * this behaviour, while later revisions say that the kernel "should" use | |
157 | * an SC instruction to force the yielding of a load reservation on a | |
158 | * preemptive context switch. As a result, do both. | |
159 | */ | |
160 | env->load_res = -1; | |
df354dd4 MC |
161 | } |
162 | ||
0c3e702a MC |
163 | /* get_physical_address - get the physical address for this virtual address |
164 | * | |
165 | * Do a page table walk to obtain the physical address corresponding to a | |
166 | * virtual address. Returns 0 if the translation was successful | |
167 | * | |
168 | * Adapted from Spike's mmu_t::translate and mmu_t::walk | |
169 | * | |
170 | */ | |
171 | static int get_physical_address(CPURISCVState *env, hwaddr *physical, | |
172 | int *prot, target_ulong addr, | |
173 | int access_type, int mmu_idx) | |
174 | { | |
175 | /* NOTE: the env->pc value visible here will not be | |
176 | * correct, but the value visible to the exception handler | |
177 | * (riscv_cpu_do_interrupt) is correct */ | |
aacb578f PD |
178 | MemTxResult res; |
179 | MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; | |
0c3e702a MC |
180 | int mode = mmu_idx; |
181 | ||
182 | if (mode == PRV_M && access_type != MMU_INST_FETCH) { | |
183 | if (get_field(env->mstatus, MSTATUS_MPRV)) { | |
184 | mode = get_field(env->mstatus, MSTATUS_MPP); | |
185 | } | |
186 | } | |
187 | ||
188 | if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) { | |
189 | *physical = addr; | |
190 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
191 | return TRANSLATE_SUCCESS; | |
192 | } | |
193 | ||
194 | *prot = 0; | |
195 | ||
ddf78132 | 196 | hwaddr base; |
0c3e702a MC |
197 | int levels, ptidxbits, ptesize, vm, sum; |
198 | int mxr = get_field(env->mstatus, MSTATUS_MXR); | |
199 | ||
200 | if (env->priv_ver >= PRIV_VERSION_1_10_0) { | |
ddf78132 | 201 | base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT; |
0c3e702a MC |
202 | sum = get_field(env->mstatus, MSTATUS_SUM); |
203 | vm = get_field(env->satp, SATP_MODE); | |
204 | switch (vm) { | |
205 | case VM_1_10_SV32: | |
206 | levels = 2; ptidxbits = 10; ptesize = 4; break; | |
207 | case VM_1_10_SV39: | |
208 | levels = 3; ptidxbits = 9; ptesize = 8; break; | |
209 | case VM_1_10_SV48: | |
210 | levels = 4; ptidxbits = 9; ptesize = 8; break; | |
211 | case VM_1_10_SV57: | |
212 | levels = 5; ptidxbits = 9; ptesize = 8; break; | |
213 | case VM_1_10_MBARE: | |
214 | *physical = addr; | |
215 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
216 | return TRANSLATE_SUCCESS; | |
217 | default: | |
218 | g_assert_not_reached(); | |
219 | } | |
220 | } else { | |
ddf78132 | 221 | base = (hwaddr)(env->sptbr) << PGSHIFT; |
0c3e702a MC |
222 | sum = !get_field(env->mstatus, MSTATUS_PUM); |
223 | vm = get_field(env->mstatus, MSTATUS_VM); | |
224 | switch (vm) { | |
225 | case VM_1_09_SV32: | |
226 | levels = 2; ptidxbits = 10; ptesize = 4; break; | |
227 | case VM_1_09_SV39: | |
228 | levels = 3; ptidxbits = 9; ptesize = 8; break; | |
229 | case VM_1_09_SV48: | |
230 | levels = 4; ptidxbits = 9; ptesize = 8; break; | |
231 | case VM_1_09_MBARE: | |
232 | *physical = addr; | |
233 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
234 | return TRANSLATE_SUCCESS; | |
235 | default: | |
236 | g_assert_not_reached(); | |
237 | } | |
238 | } | |
239 | ||
3109cd98 | 240 | CPUState *cs = env_cpu(env); |
0c3e702a MC |
241 | int va_bits = PGSHIFT + levels * ptidxbits; |
242 | target_ulong mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1; | |
243 | target_ulong masked_msbs = (addr >> (va_bits - 1)) & mask; | |
244 | if (masked_msbs != 0 && masked_msbs != mask) { | |
245 | return TRANSLATE_FAIL; | |
246 | } | |
247 | ||
248 | int ptshift = (levels - 1) * ptidxbits; | |
249 | int i; | |
250 | ||
251 | #if !TCG_OVERSIZED_GUEST | |
252 | restart: | |
253 | #endif | |
254 | for (i = 0; i < levels; i++, ptshift -= ptidxbits) { | |
255 | target_ulong idx = (addr >> (PGSHIFT + ptshift)) & | |
256 | ((1 << ptidxbits) - 1); | |
257 | ||
258 | /* check that physical address of PTE is legal */ | |
ddf78132 | 259 | hwaddr pte_addr = base + idx * ptesize; |
1f447aec HA |
260 | |
261 | if (riscv_feature(env, RISCV_FEATURE_PMP) && | |
262 | !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong), | |
263 | 1 << MMU_DATA_LOAD, PRV_S)) { | |
264 | return TRANSLATE_PMP_FAIL; | |
265 | } | |
aacb578f | 266 | |
0c3e702a | 267 | #if defined(TARGET_RISCV32) |
aacb578f | 268 | target_ulong pte = address_space_ldl(cs->as, pte_addr, attrs, &res); |
0c3e702a | 269 | #elif defined(TARGET_RISCV64) |
aacb578f | 270 | target_ulong pte = address_space_ldq(cs->as, pte_addr, attrs, &res); |
0c3e702a | 271 | #endif |
aacb578f PD |
272 | if (res != MEMTX_OK) { |
273 | return TRANSLATE_FAIL; | |
274 | } | |
275 | ||
ddf78132 | 276 | hwaddr ppn = pte >> PTE_PPN_SHIFT; |
0c3e702a | 277 | |
c3b03e58 MC |
278 | if (!(pte & PTE_V)) { |
279 | /* Invalid PTE */ | |
280 | return TRANSLATE_FAIL; | |
281 | } else if (!(pte & (PTE_R | PTE_W | PTE_X))) { | |
282 | /* Inner PTE, continue walking */ | |
0c3e702a | 283 | base = ppn << PGSHIFT; |
c3b03e58 MC |
284 | } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) { |
285 | /* Reserved leaf PTE flags: PTE_W */ | |
286 | return TRANSLATE_FAIL; | |
287 | } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) { | |
288 | /* Reserved leaf PTE flags: PTE_W + PTE_X */ | |
289 | return TRANSLATE_FAIL; | |
290 | } else if ((pte & PTE_U) && ((mode != PRV_U) && | |
291 | (!sum || access_type == MMU_INST_FETCH))) { | |
292 | /* User PTE flags when not U mode and mstatus.SUM is not set, | |
293 | or the access type is an instruction fetch */ | |
294 | return TRANSLATE_FAIL; | |
295 | } else if (!(pte & PTE_U) && (mode != PRV_S)) { | |
296 | /* Supervisor PTE flags when not S mode */ | |
297 | return TRANSLATE_FAIL; | |
298 | } else if (ppn & ((1ULL << ptshift) - 1)) { | |
299 | /* Misaligned PPN */ | |
300 | return TRANSLATE_FAIL; | |
301 | } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) || | |
302 | ((pte & PTE_X) && mxr))) { | |
303 | /* Read access check failed */ | |
304 | return TRANSLATE_FAIL; | |
305 | } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) { | |
306 | /* Write access check failed */ | |
307 | return TRANSLATE_FAIL; | |
308 | } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) { | |
309 | /* Fetch access check failed */ | |
310 | return TRANSLATE_FAIL; | |
0c3e702a MC |
311 | } else { |
312 | /* if necessary, set accessed and dirty bits. */ | |
313 | target_ulong updated_pte = pte | PTE_A | | |
314 | (access_type == MMU_DATA_STORE ? PTE_D : 0); | |
315 | ||
316 | /* Page table updates need to be atomic with MTTCG enabled */ | |
317 | if (updated_pte != pte) { | |
c3b03e58 MC |
318 | /* |
319 | * - if accessed or dirty bits need updating, and the PTE is | |
320 | * in RAM, then we do so atomically with a compare and swap. | |
321 | * - if the PTE is in IO space or ROM, then it can't be updated | |
322 | * and we return TRANSLATE_FAIL. | |
323 | * - if the PTE changed by the time we went to update it, then | |
324 | * it is no longer valid and we must re-walk the page table. | |
325 | */ | |
0c3e702a MC |
326 | MemoryRegion *mr; |
327 | hwaddr l = sizeof(target_ulong), addr1; | |
328 | mr = address_space_translate(cs->as, pte_addr, | |
bc6b1cec | 329 | &addr1, &l, false, MEMTXATTRS_UNSPECIFIED); |
c3b03e58 | 330 | if (memory_region_is_ram(mr)) { |
0c3e702a MC |
331 | target_ulong *pte_pa = |
332 | qemu_map_ram_ptr(mr->ram_block, addr1); | |
333 | #if TCG_OVERSIZED_GUEST | |
334 | /* MTTCG is not enabled on oversized TCG guests so | |
335 | * page table updates do not need to be atomic */ | |
336 | *pte_pa = pte = updated_pte; | |
337 | #else | |
338 | target_ulong old_pte = | |
339 | atomic_cmpxchg(pte_pa, pte, updated_pte); | |
340 | if (old_pte != pte) { | |
341 | goto restart; | |
342 | } else { | |
343 | pte = updated_pte; | |
344 | } | |
345 | #endif | |
346 | } else { | |
347 | /* misconfigured PTE in ROM (AD bits are not preset) or | |
348 | * PTE is in IO space and can't be updated atomically */ | |
349 | return TRANSLATE_FAIL; | |
350 | } | |
351 | } | |
352 | ||
353 | /* for superpage mappings, make a fake leaf PTE for the TLB's | |
354 | benefit. */ | |
355 | target_ulong vpn = addr >> PGSHIFT; | |
356 | *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; | |
357 | ||
c3b03e58 MC |
358 | /* set permissions on the TLB entry */ |
359 | if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) { | |
0c3e702a MC |
360 | *prot |= PAGE_READ; |
361 | } | |
362 | if ((pte & PTE_X)) { | |
363 | *prot |= PAGE_EXEC; | |
364 | } | |
c3b03e58 MC |
365 | /* add write permission on stores or if the page is already dirty, |
366 | so that we TLB miss on later writes to update the dirty bit */ | |
0c3e702a MC |
367 | if ((pte & PTE_W) && |
368 | (access_type == MMU_DATA_STORE || (pte & PTE_D))) { | |
369 | *prot |= PAGE_WRITE; | |
370 | } | |
371 | return TRANSLATE_SUCCESS; | |
372 | } | |
373 | } | |
374 | return TRANSLATE_FAIL; | |
375 | } | |
376 | ||
377 | static void raise_mmu_exception(CPURISCVState *env, target_ulong address, | |
635b0b0e | 378 | MMUAccessType access_type, bool pmp_violation) |
0c3e702a | 379 | { |
3109cd98 | 380 | CPUState *cs = env_cpu(env); |
0c3e702a MC |
381 | int page_fault_exceptions = |
382 | (env->priv_ver >= PRIV_VERSION_1_10_0) && | |
635b0b0e HA |
383 | get_field(env->satp, SATP_MODE) != VM_1_10_MBARE && |
384 | !pmp_violation; | |
0c3e702a MC |
385 | switch (access_type) { |
386 | case MMU_INST_FETCH: | |
387 | cs->exception_index = page_fault_exceptions ? | |
388 | RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT; | |
389 | break; | |
390 | case MMU_DATA_LOAD: | |
391 | cs->exception_index = page_fault_exceptions ? | |
392 | RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT; | |
393 | break; | |
394 | case MMU_DATA_STORE: | |
395 | cs->exception_index = page_fault_exceptions ? | |
396 | RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT; | |
397 | break; | |
398 | default: | |
399 | g_assert_not_reached(); | |
400 | } | |
401 | env->badaddr = address; | |
402 | } | |
403 | ||
404 | hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) | |
405 | { | |
406 | RISCVCPU *cpu = RISCV_CPU(cs); | |
407 | hwaddr phys_addr; | |
408 | int prot; | |
409 | int mmu_idx = cpu_mmu_index(&cpu->env, false); | |
410 | ||
411 | if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0, mmu_idx)) { | |
412 | return -1; | |
413 | } | |
414 | return phys_addr; | |
415 | } | |
416 | ||
37207e12 PD |
417 | void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, |
418 | vaddr addr, unsigned size, | |
419 | MMUAccessType access_type, | |
420 | int mmu_idx, MemTxAttrs attrs, | |
421 | MemTxResult response, uintptr_t retaddr) | |
cbf58276 MC |
422 | { |
423 | RISCVCPU *cpu = RISCV_CPU(cs); | |
424 | CPURISCVState *env = &cpu->env; | |
425 | ||
37207e12 | 426 | if (access_type == MMU_DATA_STORE) { |
cbf58276 MC |
427 | cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT; |
428 | } else { | |
429 | cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT; | |
430 | } | |
431 | ||
432 | env->badaddr = addr; | |
37207e12 | 433 | riscv_raise_exception(&cpu->env, cs->exception_index, retaddr); |
cbf58276 MC |
434 | } |
435 | ||
0c3e702a MC |
436 | void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, |
437 | MMUAccessType access_type, int mmu_idx, | |
438 | uintptr_t retaddr) | |
439 | { | |
440 | RISCVCPU *cpu = RISCV_CPU(cs); | |
441 | CPURISCVState *env = &cpu->env; | |
442 | switch (access_type) { | |
443 | case MMU_INST_FETCH: | |
444 | cs->exception_index = RISCV_EXCP_INST_ADDR_MIS; | |
445 | break; | |
446 | case MMU_DATA_LOAD: | |
447 | cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS; | |
448 | break; | |
449 | case MMU_DATA_STORE: | |
450 | cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS; | |
451 | break; | |
452 | default: | |
453 | g_assert_not_reached(); | |
454 | } | |
455 | env->badaddr = addr; | |
fb738839 | 456 | riscv_raise_exception(env, cs->exception_index, retaddr); |
0c3e702a | 457 | } |
0c3e702a MC |
458 | #endif |
459 | ||
8a4ca3c1 RH |
460 | bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, |
461 | MMUAccessType access_type, int mmu_idx, | |
462 | bool probe, uintptr_t retaddr) | |
0c3e702a MC |
463 | { |
464 | RISCVCPU *cpu = RISCV_CPU(cs); | |
465 | CPURISCVState *env = &cpu->env; | |
2921343b | 466 | #ifndef CONFIG_USER_ONLY |
0c3e702a MC |
467 | hwaddr pa = 0; |
468 | int prot; | |
635b0b0e | 469 | bool pmp_violation = false; |
0c3e702a | 470 | int ret = TRANSLATE_FAIL; |
cc0fdb29 | 471 | int mode = mmu_idx; |
0c3e702a | 472 | |
8a4ca3c1 RH |
473 | qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", |
474 | __func__, address, access_type, mmu_idx); | |
475 | ||
476 | ret = get_physical_address(env, &pa, &prot, address, access_type, mmu_idx); | |
0c3e702a | 477 | |
cc0fdb29 HA |
478 | if (mode == PRV_M && access_type != MMU_INST_FETCH) { |
479 | if (get_field(env->mstatus, MSTATUS_MPRV)) { | |
480 | mode = get_field(env->mstatus, MSTATUS_MPP); | |
481 | } | |
482 | } | |
483 | ||
0c3e702a | 484 | qemu_log_mask(CPU_LOG_MMU, |
8a4ca3c1 RH |
485 | "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx |
486 | " prot %d\n", __func__, address, ret, pa, prot); | |
487 | ||
a88365c1 | 488 | if (riscv_feature(env, RISCV_FEATURE_PMP) && |
e0f8fa72 | 489 | (ret == TRANSLATE_SUCCESS) && |
db21e6f7 | 490 | !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) { |
1f447aec HA |
491 | ret = TRANSLATE_PMP_FAIL; |
492 | } | |
493 | if (ret == TRANSLATE_PMP_FAIL) { | |
635b0b0e | 494 | pmp_violation = true; |
0c3e702a MC |
495 | } |
496 | if (ret == TRANSLATE_SUCCESS) { | |
497 | tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK, | |
498 | prot, mmu_idx, TARGET_PAGE_SIZE); | |
8a4ca3c1 RH |
499 | return true; |
500 | } else if (probe) { | |
501 | return false; | |
502 | } else { | |
635b0b0e | 503 | raise_mmu_exception(env, address, access_type, pmp_violation); |
8a4ca3c1 | 504 | riscv_raise_exception(env, cs->exception_index, retaddr); |
0c3e702a MC |
505 | } |
506 | #else | |
8a4ca3c1 | 507 | switch (access_type) { |
0c3e702a MC |
508 | case MMU_INST_FETCH: |
509 | cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT; | |
510 | break; | |
511 | case MMU_DATA_LOAD: | |
512 | cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT; | |
513 | break; | |
514 | case MMU_DATA_STORE: | |
515 | cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT; | |
516 | break; | |
2921343b GM |
517 | default: |
518 | g_assert_not_reached(); | |
0c3e702a | 519 | } |
2921343b | 520 | env->badaddr = address; |
8a4ca3c1 | 521 | cpu_loop_exit_restore(cs, retaddr); |
0c3e702a | 522 | #endif |
0c3e702a MC |
523 | } |
524 | ||
525 | /* | |
526 | * Handle Traps | |
527 | * | |
528 | * Adapted from Spike's processor_t::take_trap. | |
529 | * | |
530 | */ | |
531 | void riscv_cpu_do_interrupt(CPUState *cs) | |
532 | { | |
533 | #if !defined(CONFIG_USER_ONLY) | |
534 | ||
535 | RISCVCPU *cpu = RISCV_CPU(cs); | |
536 | CPURISCVState *env = &cpu->env; | |
537 | ||
acbbb94e MC |
538 | /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide |
539 | * so we mask off the MSB and separate into trap type and cause. | |
540 | */ | |
541 | bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG); | |
542 | target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK; | |
543 | target_ulong deleg = async ? env->mideleg : env->medeleg; | |
544 | target_ulong tval = 0; | |
545 | ||
546 | static const int ecall_cause_map[] = { | |
547 | [PRV_U] = RISCV_EXCP_U_ECALL, | |
548 | [PRV_S] = RISCV_EXCP_S_ECALL, | |
ab67a1d0 | 549 | [PRV_H] = RISCV_EXCP_VS_ECALL, |
acbbb94e MC |
550 | [PRV_M] = RISCV_EXCP_M_ECALL |
551 | }; | |
552 | ||
553 | if (!async) { | |
554 | /* set tval to badaddr for traps with address information */ | |
555 | switch (cause) { | |
ab67a1d0 AF |
556 | case RISCV_EXCP_INST_GUEST_PAGE_FAULT: |
557 | case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT: | |
558 | case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT: | |
acbbb94e MC |
559 | case RISCV_EXCP_INST_ADDR_MIS: |
560 | case RISCV_EXCP_INST_ACCESS_FAULT: | |
561 | case RISCV_EXCP_LOAD_ADDR_MIS: | |
562 | case RISCV_EXCP_STORE_AMO_ADDR_MIS: | |
563 | case RISCV_EXCP_LOAD_ACCESS_FAULT: | |
564 | case RISCV_EXCP_STORE_AMO_ACCESS_FAULT: | |
565 | case RISCV_EXCP_INST_PAGE_FAULT: | |
566 | case RISCV_EXCP_LOAD_PAGE_FAULT: | |
567 | case RISCV_EXCP_STORE_PAGE_FAULT: | |
568 | tval = env->badaddr; | |
569 | break; | |
570 | default: | |
571 | break; | |
0c3e702a | 572 | } |
acbbb94e MC |
573 | /* ecall is dispatched as one cause so translate based on mode */ |
574 | if (cause == RISCV_EXCP_U_ECALL) { | |
575 | assert(env->priv <= 3); | |
576 | cause = ecall_cause_map[env->priv]; | |
0c3e702a MC |
577 | } |
578 | } | |
579 | ||
ab67a1d0 | 580 | trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, cause < 23 ? |
929f0a7f | 581 | (async ? riscv_intr_names : riscv_excp_names)[cause] : "(unknown)"); |
0c3e702a | 582 | |
acbbb94e MC |
583 | if (env->priv <= PRV_S && |
584 | cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) { | |
0c3e702a | 585 | /* handle the trap in S-mode */ |
0c3e702a MC |
586 | target_ulong s = env->mstatus; |
587 | s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? | |
588 | get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv)); | |
589 | s = set_field(s, MSTATUS_SPP, env->priv); | |
590 | s = set_field(s, MSTATUS_SIE, 0); | |
c7b95171 | 591 | env->mstatus = s; |
16fdb8ff | 592 | env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1)); |
acbbb94e MC |
593 | env->sepc = env->pc; |
594 | env->sbadaddr = tval; | |
595 | env->pc = (env->stvec >> 2 << 2) + | |
596 | ((async && (env->stvec & 3) == 1) ? cause * 4 : 0); | |
fb738839 | 597 | riscv_cpu_set_mode(env, PRV_S); |
0c3e702a | 598 | } else { |
acbbb94e | 599 | /* handle the trap in M-mode */ |
0c3e702a MC |
600 | target_ulong s = env->mstatus; |
601 | s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? | |
602 | get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv)); | |
603 | s = set_field(s, MSTATUS_MPP, env->priv); | |
604 | s = set_field(s, MSTATUS_MIE, 0); | |
c7b95171 | 605 | env->mstatus = s; |
acbbb94e MC |
606 | env->mcause = cause | ~(((target_ulong)-1) >> async); |
607 | env->mepc = env->pc; | |
608 | env->mbadaddr = tval; | |
609 | env->pc = (env->mtvec >> 2 << 2) + | |
610 | ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); | |
fb738839 | 611 | riscv_cpu_set_mode(env, PRV_M); |
0c3e702a | 612 | } |
d9360e96 MC |
613 | |
614 | /* NOTE: it is not necessary to yield load reservations here. It is only | |
615 | * necessary for an SC from "another hart" to cause a load reservation | |
616 | * to be yielded. Refer to the memory consistency model section of the | |
617 | * RISC-V ISA Specification. | |
618 | */ | |
619 | ||
0c3e702a MC |
620 | #endif |
621 | cs->exception_index = EXCP_NONE; /* mark handled to qemu */ | |
622 | } |