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