xref: /openbmc/qemu/target/riscv/cpu_helper.c (revision 36a18664)
1 /*
2  * RISC-V CPU helpers for qemu.
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 "qemu/main-loop.h"
23 #include "cpu.h"
24 #include "exec/exec-all.h"
25 #include "tcg/tcg-op.h"
26 #include "trace.h"
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
38 static int riscv_cpu_local_irq_pending(CPURISCVState *env)
39 {
40     target_ulong irqs;
41 
42     target_ulong mstatus_mie = get_field(env->mstatus, MSTATUS_MIE);
43     target_ulong mstatus_sie = get_field(env->mstatus, MSTATUS_SIE);
44     target_ulong hs_mstatus_sie = get_field(env->mstatus_hs, MSTATUS_SIE);
45 
46     target_ulong pending = env->mip & env->mie &
47                                ~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP);
48     target_ulong vspending = (env->mip & env->mie &
49                               (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)) >> 1;
50 
51     target_ulong mie    = env->priv < PRV_M ||
52                           (env->priv == PRV_M && mstatus_mie);
53     target_ulong sie    = env->priv < PRV_S ||
54                           (env->priv == PRV_S && mstatus_sie);
55     target_ulong hs_sie = env->priv < PRV_S ||
56                           (env->priv == PRV_S && hs_mstatus_sie);
57 
58     if (riscv_cpu_virt_enabled(env)) {
59         target_ulong pending_hs_irq = pending & -hs_sie;
60 
61         if (pending_hs_irq) {
62             riscv_cpu_set_force_hs_excep(env, FORCE_HS_EXCEP);
63             return ctz64(pending_hs_irq);
64         }
65 
66         pending = vspending;
67     }
68 
69     irqs = (pending & ~env->mideleg & -mie) | (pending &  env->mideleg & -sie);
70 
71     if (irqs) {
72         return ctz64(irqs); /* since non-zero */
73     } else {
74         return EXCP_NONE; /* indicates no pending interrupt */
75     }
76 }
77 #endif
78 
79 bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
80 {
81 #if !defined(CONFIG_USER_ONLY)
82     if (interrupt_request & CPU_INTERRUPT_HARD) {
83         RISCVCPU *cpu = RISCV_CPU(cs);
84         CPURISCVState *env = &cpu->env;
85         int interruptno = riscv_cpu_local_irq_pending(env);
86         if (interruptno >= 0) {
87             cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno;
88             riscv_cpu_do_interrupt(cs);
89             return true;
90         }
91     }
92 #endif
93     return false;
94 }
95 
96 #if !defined(CONFIG_USER_ONLY)
97 
98 /* Return true is floating point support is currently enabled */
99 bool riscv_cpu_fp_enabled(CPURISCVState *env)
100 {
101     if (env->mstatus & MSTATUS_FS) {
102         if (riscv_cpu_virt_enabled(env) && !(env->mstatus_hs & MSTATUS_FS)) {
103             return false;
104         }
105         return true;
106     }
107 
108     return false;
109 }
110 
111 void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env)
112 {
113     target_ulong mstatus_mask = MSTATUS_MXR | MSTATUS_SUM | MSTATUS_FS |
114                                 MSTATUS_SPP | MSTATUS_SPIE | MSTATUS_SIE;
115     bool current_virt = riscv_cpu_virt_enabled(env);
116 
117     g_assert(riscv_has_ext(env, RVH));
118 
119 #if defined(TARGET_RISCV64)
120     mstatus_mask |= MSTATUS64_UXL;
121 #endif
122 
123     if (current_virt) {
124         /* Current V=1 and we are about to change to V=0 */
125         env->vsstatus = env->mstatus & mstatus_mask;
126         env->mstatus &= ~mstatus_mask;
127         env->mstatus |= env->mstatus_hs;
128 
129         env->vstvec = env->stvec;
130         env->stvec = env->stvec_hs;
131 
132         env->vsscratch = env->sscratch;
133         env->sscratch = env->sscratch_hs;
134 
135         env->vsepc = env->sepc;
136         env->sepc = env->sepc_hs;
137 
138         env->vscause = env->scause;
139         env->scause = env->scause_hs;
140 
141         env->vstval = env->sbadaddr;
142         env->sbadaddr = env->stval_hs;
143 
144         env->vsatp = env->satp;
145         env->satp = env->satp_hs;
146     } else {
147         /* Current V=0 and we are about to change to V=1 */
148         env->mstatus_hs = env->mstatus & mstatus_mask;
149         env->mstatus &= ~mstatus_mask;
150         env->mstatus |= env->vsstatus;
151 
152         env->stvec_hs = env->stvec;
153         env->stvec = env->vstvec;
154 
155         env->sscratch_hs = env->sscratch;
156         env->sscratch = env->vsscratch;
157 
158         env->sepc_hs = env->sepc;
159         env->sepc = env->vsepc;
160 
161         env->scause_hs = env->scause;
162         env->scause = env->vscause;
163 
164         env->stval_hs = env->sbadaddr;
165         env->sbadaddr = env->vstval;
166 
167         env->satp_hs = env->satp;
168         env->satp = env->vsatp;
169     }
170 }
171 
172 bool riscv_cpu_virt_enabled(CPURISCVState *env)
173 {
174     if (!riscv_has_ext(env, RVH)) {
175         return false;
176     }
177 
178     return get_field(env->virt, VIRT_ONOFF);
179 }
180 
181 void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable)
182 {
183     if (!riscv_has_ext(env, RVH)) {
184         return;
185     }
186 
187     /* Flush the TLB on all virt mode changes. */
188     if (get_field(env->virt, VIRT_ONOFF) != enable) {
189         tlb_flush(env_cpu(env));
190     }
191 
192     env->virt = set_field(env->virt, VIRT_ONOFF, enable);
193 }
194 
195 bool riscv_cpu_force_hs_excep_enabled(CPURISCVState *env)
196 {
197     if (!riscv_has_ext(env, RVH)) {
198         return false;
199     }
200 
201     return get_field(env->virt, FORCE_HS_EXCEP);
202 }
203 
204 void riscv_cpu_set_force_hs_excep(CPURISCVState *env, bool enable)
205 {
206     if (!riscv_has_ext(env, RVH)) {
207         return;
208     }
209 
210     env->virt = set_field(env->virt, FORCE_HS_EXCEP, enable);
211 }
212 
213 int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts)
214 {
215     CPURISCVState *env = &cpu->env;
216     if (env->miclaim & interrupts) {
217         return -1;
218     } else {
219         env->miclaim |= interrupts;
220         return 0;
221     }
222 }
223 
224 uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value)
225 {
226     CPURISCVState *env = &cpu->env;
227     CPUState *cs = CPU(cpu);
228     uint32_t old = env->mip;
229     bool locked = false;
230 
231     if (!qemu_mutex_iothread_locked()) {
232         locked = true;
233         qemu_mutex_lock_iothread();
234     }
235 
236     env->mip = (env->mip & ~mask) | (value & mask);
237 
238     if (env->mip) {
239         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
240     } else {
241         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
242     }
243 
244     if (locked) {
245         qemu_mutex_unlock_iothread();
246     }
247 
248     return old;
249 }
250 
251 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv)
252 {
253     if (newpriv > PRV_M) {
254         g_assert_not_reached();
255     }
256     if (newpriv == PRV_H) {
257         newpriv = PRV_U;
258     }
259     /* tlb_flush is unnecessary as mode is contained in mmu_idx */
260     env->priv = newpriv;
261 
262     /*
263      * Clear the load reservation - otherwise a reservation placed in one
264      * context/process can be used by another, resulting in an SC succeeding
265      * incorrectly. Version 2.2 of the ISA specification explicitly requires
266      * this behaviour, while later revisions say that the kernel "should" use
267      * an SC instruction to force the yielding of a load reservation on a
268      * preemptive context switch. As a result, do both.
269      */
270     env->load_res = -1;
271 }
272 
273 /* get_physical_address - get the physical address for this virtual address
274  *
275  * Do a page table walk to obtain the physical address corresponding to a
276  * virtual address. Returns 0 if the translation was successful
277  *
278  * Adapted from Spike's mmu_t::translate and mmu_t::walk
279  *
280  * @env: CPURISCVState
281  * @physical: This will be set to the calculated physical address
282  * @prot: The returned protection attributes
283  * @addr: The virtual address to be translated
284  * @access_type: The type of MMU access
285  * @mmu_idx: Indicates current privilege level
286  * @first_stage: Are we in first stage translation?
287  *               Second stage is used for hypervisor guest translation
288  * @two_stage: Are we going to perform two stage translation
289  */
290 static int get_physical_address(CPURISCVState *env, hwaddr *physical,
291                                 int *prot, target_ulong addr,
292                                 int access_type, int mmu_idx,
293                                 bool first_stage, bool two_stage)
294 {
295     /* NOTE: the env->pc value visible here will not be
296      * correct, but the value visible to the exception handler
297      * (riscv_cpu_do_interrupt) is correct */
298     MemTxResult res;
299     MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
300     int mode = mmu_idx;
301     bool use_background = false;
302 
303     /*
304      * Check if we should use the background registers for the two
305      * stage translation. We don't need to check if we actually need
306      * two stage translation as that happened before this function
307      * was called. Background registers will be used if the guest has
308      * forced a two stage translation to be on (in HS or M mode).
309      */
310     if (mode == PRV_M && access_type != MMU_INST_FETCH) {
311         if (get_field(env->mstatus, MSTATUS_MPRV)) {
312             mode = get_field(env->mstatus, MSTATUS_MPP);
313 
314             if (riscv_has_ext(env, RVH) &&
315                 get_field(env->mstatus, MSTATUS_MPV)) {
316                 use_background = true;
317             }
318         }
319     }
320 
321     if (mode == PRV_S && access_type != MMU_INST_FETCH &&
322         riscv_has_ext(env, RVH) && !riscv_cpu_virt_enabled(env)) {
323         if (get_field(env->hstatus, HSTATUS_SPRV)) {
324             mode = get_field(env->mstatus, SSTATUS_SPP);
325             use_background = true;
326         }
327     }
328 
329     if (first_stage == false) {
330         /* We are in stage 2 translation, this is similar to stage 1. */
331         /* Stage 2 is always taken as U-mode */
332         mode = PRV_U;
333     }
334 
335     if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) {
336         *physical = addr;
337         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
338         return TRANSLATE_SUCCESS;
339     }
340 
341     *prot = 0;
342 
343     hwaddr base;
344     int levels, ptidxbits, ptesize, vm, sum, mxr, widened;
345 
346     if (first_stage == true) {
347         mxr = get_field(env->mstatus, MSTATUS_MXR);
348     } else {
349         mxr = get_field(env->vsstatus, MSTATUS_MXR);
350     }
351 
352     if (env->priv_ver >= PRIV_VERSION_1_10_0) {
353         if (first_stage == true) {
354             if (use_background) {
355                 base = (hwaddr)get_field(env->vsatp, SATP_PPN) << PGSHIFT;
356                 vm = get_field(env->vsatp, SATP_MODE);
357             } else {
358                 base = (hwaddr)get_field(env->satp, SATP_PPN) << PGSHIFT;
359                 vm = get_field(env->satp, SATP_MODE);
360             }
361             widened = 0;
362         } else {
363             base = (hwaddr)get_field(env->hgatp, HGATP_PPN) << PGSHIFT;
364             vm = get_field(env->hgatp, HGATP_MODE);
365             widened = 2;
366         }
367         sum = get_field(env->mstatus, MSTATUS_SUM);
368         switch (vm) {
369         case VM_1_10_SV32:
370           levels = 2; ptidxbits = 10; ptesize = 4; break;
371         case VM_1_10_SV39:
372           levels = 3; ptidxbits = 9; ptesize = 8; break;
373         case VM_1_10_SV48:
374           levels = 4; ptidxbits = 9; ptesize = 8; break;
375         case VM_1_10_SV57:
376           levels = 5; ptidxbits = 9; ptesize = 8; break;
377         case VM_1_10_MBARE:
378             *physical = addr;
379             *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
380             return TRANSLATE_SUCCESS;
381         default:
382           g_assert_not_reached();
383         }
384     } else {
385         widened = 0;
386         base = (hwaddr)(env->sptbr) << PGSHIFT;
387         sum = !get_field(env->mstatus, MSTATUS_PUM);
388         vm = get_field(env->mstatus, MSTATUS_VM);
389         switch (vm) {
390         case VM_1_09_SV32:
391           levels = 2; ptidxbits = 10; ptesize = 4; break;
392         case VM_1_09_SV39:
393           levels = 3; ptidxbits = 9; ptesize = 8; break;
394         case VM_1_09_SV48:
395           levels = 4; ptidxbits = 9; ptesize = 8; break;
396         case VM_1_09_MBARE:
397             *physical = addr;
398             *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
399             return TRANSLATE_SUCCESS;
400         default:
401           g_assert_not_reached();
402         }
403     }
404 
405     CPUState *cs = env_cpu(env);
406     int va_bits = PGSHIFT + levels * ptidxbits + widened;
407     target_ulong mask, masked_msbs;
408 
409     if (TARGET_LONG_BITS > (va_bits - 1)) {
410         mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1;
411     } else {
412         mask = 0;
413     }
414     masked_msbs = (addr >> (va_bits - 1)) & mask;
415 
416     if (masked_msbs != 0 && masked_msbs != mask) {
417         return TRANSLATE_FAIL;
418     }
419 
420     int ptshift = (levels - 1) * ptidxbits;
421     int i;
422 
423 #if !TCG_OVERSIZED_GUEST
424 restart:
425 #endif
426     for (i = 0; i < levels; i++, ptshift -= ptidxbits) {
427         target_ulong idx;
428         if (i == 0) {
429             idx = (addr >> (PGSHIFT + ptshift)) &
430                            ((1 << (ptidxbits + widened)) - 1);
431         } else {
432             idx = (addr >> (PGSHIFT + ptshift)) &
433                            ((1 << ptidxbits) - 1);
434         }
435 
436         /* check that physical address of PTE is legal */
437         hwaddr pte_addr;
438 
439         if (two_stage && first_stage) {
440             hwaddr vbase;
441 
442             /* Do the second stage translation on the base PTE address. */
443             get_physical_address(env, &vbase, prot, base, access_type,
444                                  mmu_idx, false, true);
445 
446             pte_addr = vbase + idx * ptesize;
447         } else {
448             pte_addr = base + idx * ptesize;
449         }
450 
451         if (riscv_feature(env, RISCV_FEATURE_PMP) &&
452             !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong),
453             1 << MMU_DATA_LOAD, PRV_S)) {
454             return TRANSLATE_PMP_FAIL;
455         }
456 
457 #if defined(TARGET_RISCV32)
458         target_ulong pte = address_space_ldl(cs->as, pte_addr, attrs, &res);
459 #elif defined(TARGET_RISCV64)
460         target_ulong pte = address_space_ldq(cs->as, pte_addr, attrs, &res);
461 #endif
462         if (res != MEMTX_OK) {
463             return TRANSLATE_FAIL;
464         }
465 
466         hwaddr ppn = pte >> PTE_PPN_SHIFT;
467 
468         if (!(pte & PTE_V)) {
469             /* Invalid PTE */
470             return TRANSLATE_FAIL;
471         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
472             /* Inner PTE, continue walking */
473             base = ppn << PGSHIFT;
474         } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
475             /* Reserved leaf PTE flags: PTE_W */
476             return TRANSLATE_FAIL;
477         } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) {
478             /* Reserved leaf PTE flags: PTE_W + PTE_X */
479             return TRANSLATE_FAIL;
480         } else if ((pte & PTE_U) && ((mode != PRV_U) &&
481                    (!sum || access_type == MMU_INST_FETCH))) {
482             /* User PTE flags when not U mode and mstatus.SUM is not set,
483                or the access type is an instruction fetch */
484             return TRANSLATE_FAIL;
485         } else if (!(pte & PTE_U) && (mode != PRV_S)) {
486             /* Supervisor PTE flags when not S mode */
487             return TRANSLATE_FAIL;
488         } else if (ppn & ((1ULL << ptshift) - 1)) {
489             /* Misaligned PPN */
490             return TRANSLATE_FAIL;
491         } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) ||
492                    ((pte & PTE_X) && mxr))) {
493             /* Read access check failed */
494             return TRANSLATE_FAIL;
495         } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) {
496             /* Write access check failed */
497             return TRANSLATE_FAIL;
498         } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) {
499             /* Fetch access check failed */
500             return TRANSLATE_FAIL;
501         } else {
502             /* if necessary, set accessed and dirty bits. */
503             target_ulong updated_pte = pte | PTE_A |
504                 (access_type == MMU_DATA_STORE ? PTE_D : 0);
505 
506             /* Page table updates need to be atomic with MTTCG enabled */
507             if (updated_pte != pte) {
508                 /*
509                  * - if accessed or dirty bits need updating, and the PTE is
510                  *   in RAM, then we do so atomically with a compare and swap.
511                  * - if the PTE is in IO space or ROM, then it can't be updated
512                  *   and we return TRANSLATE_FAIL.
513                  * - if the PTE changed by the time we went to update it, then
514                  *   it is no longer valid and we must re-walk the page table.
515                  */
516                 MemoryRegion *mr;
517                 hwaddr l = sizeof(target_ulong), addr1;
518                 mr = address_space_translate(cs->as, pte_addr,
519                     &addr1, &l, false, MEMTXATTRS_UNSPECIFIED);
520                 if (memory_region_is_ram(mr)) {
521                     target_ulong *pte_pa =
522                         qemu_map_ram_ptr(mr->ram_block, addr1);
523 #if TCG_OVERSIZED_GUEST
524                     /* MTTCG is not enabled on oversized TCG guests so
525                      * page table updates do not need to be atomic */
526                     *pte_pa = pte = updated_pte;
527 #else
528                     target_ulong old_pte =
529                         atomic_cmpxchg(pte_pa, pte, updated_pte);
530                     if (old_pte != pte) {
531                         goto restart;
532                     } else {
533                         pte = updated_pte;
534                     }
535 #endif
536                 } else {
537                     /* misconfigured PTE in ROM (AD bits are not preset) or
538                      * PTE is in IO space and can't be updated atomically */
539                     return TRANSLATE_FAIL;
540                 }
541             }
542 
543             /* for superpage mappings, make a fake leaf PTE for the TLB's
544                benefit. */
545             target_ulong vpn = addr >> PGSHIFT;
546             if (i == 0) {
547                 *physical = (ppn | (vpn & ((1L << (ptshift + widened)) - 1))) <<
548                              PGSHIFT;
549             } else {
550                 *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT;
551             }
552 
553             /* set permissions on the TLB entry */
554             if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) {
555                 *prot |= PAGE_READ;
556             }
557             if ((pte & PTE_X)) {
558                 *prot |= PAGE_EXEC;
559             }
560             /* add write permission on stores or if the page is already dirty,
561                so that we TLB miss on later writes to update the dirty bit */
562             if ((pte & PTE_W) &&
563                     (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
564                 *prot |= PAGE_WRITE;
565             }
566             return TRANSLATE_SUCCESS;
567         }
568     }
569     return TRANSLATE_FAIL;
570 }
571 
572 static void raise_mmu_exception(CPURISCVState *env, target_ulong address,
573                                 MMUAccessType access_type, bool pmp_violation,
574                                 bool first_stage)
575 {
576     CPUState *cs = env_cpu(env);
577     int page_fault_exceptions;
578     if (first_stage) {
579         page_fault_exceptions =
580             (env->priv_ver >= PRIV_VERSION_1_10_0) &&
581             get_field(env->satp, SATP_MODE) != VM_1_10_MBARE &&
582             !pmp_violation;
583     } else {
584         page_fault_exceptions =
585             get_field(env->hgatp, HGATP_MODE) != VM_1_10_MBARE &&
586             !pmp_violation;
587     }
588     switch (access_type) {
589     case MMU_INST_FETCH:
590         cs->exception_index = page_fault_exceptions ?
591             RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT;
592         break;
593     case MMU_DATA_LOAD:
594         cs->exception_index = page_fault_exceptions ?
595             RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT;
596         break;
597     case MMU_DATA_STORE:
598         cs->exception_index = page_fault_exceptions ?
599             RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
600         break;
601     default:
602         g_assert_not_reached();
603     }
604     env->badaddr = address;
605 }
606 
607 hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
608 {
609     RISCVCPU *cpu = RISCV_CPU(cs);
610     CPURISCVState *env = &cpu->env;
611     hwaddr phys_addr;
612     int prot;
613     int mmu_idx = cpu_mmu_index(&cpu->env, false);
614 
615     if (get_physical_address(env, &phys_addr, &prot, addr, 0, mmu_idx,
616                              true, riscv_cpu_virt_enabled(env))) {
617         return -1;
618     }
619 
620     if (riscv_cpu_virt_enabled(env)) {
621         if (get_physical_address(env, &phys_addr, &prot, phys_addr,
622                                  0, mmu_idx, false, true)) {
623             return -1;
624         }
625     }
626 
627     return phys_addr;
628 }
629 
630 void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
631                                      vaddr addr, unsigned size,
632                                      MMUAccessType access_type,
633                                      int mmu_idx, MemTxAttrs attrs,
634                                      MemTxResult response, uintptr_t retaddr)
635 {
636     RISCVCPU *cpu = RISCV_CPU(cs);
637     CPURISCVState *env = &cpu->env;
638 
639     if (access_type == MMU_DATA_STORE) {
640         cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
641     } else {
642         cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT;
643     }
644 
645     env->badaddr = addr;
646     riscv_raise_exception(&cpu->env, cs->exception_index, retaddr);
647 }
648 
649 void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
650                                    MMUAccessType access_type, int mmu_idx,
651                                    uintptr_t retaddr)
652 {
653     RISCVCPU *cpu = RISCV_CPU(cs);
654     CPURISCVState *env = &cpu->env;
655     switch (access_type) {
656     case MMU_INST_FETCH:
657         cs->exception_index = RISCV_EXCP_INST_ADDR_MIS;
658         break;
659     case MMU_DATA_LOAD:
660         cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS;
661         break;
662     case MMU_DATA_STORE:
663         cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS;
664         break;
665     default:
666         g_assert_not_reached();
667     }
668     env->badaddr = addr;
669     riscv_raise_exception(env, cs->exception_index, retaddr);
670 }
671 #endif
672 
673 bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
674                         MMUAccessType access_type, int mmu_idx,
675                         bool probe, uintptr_t retaddr)
676 {
677     RISCVCPU *cpu = RISCV_CPU(cs);
678     CPURISCVState *env = &cpu->env;
679 #ifndef CONFIG_USER_ONLY
680     vaddr im_address;
681     hwaddr pa = 0;
682     int prot;
683     bool pmp_violation = false;
684     bool m_mode_two_stage = false;
685     bool hs_mode_two_stage = false;
686     bool first_stage_error = true;
687     int ret = TRANSLATE_FAIL;
688     int mode = mmu_idx;
689 
690     env->guest_phys_fault_addr = 0;
691 
692     qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n",
693                   __func__, address, access_type, mmu_idx);
694 
695     /*
696      * Determine if we are in M mode and MPRV is set or in HS mode and SPRV is
697      * set and we want to access a virtulisation address.
698      */
699     if (riscv_has_ext(env, RVH)) {
700         m_mode_two_stage = env->priv == PRV_M &&
701                            access_type != MMU_INST_FETCH &&
702                            get_field(env->mstatus, MSTATUS_MPRV) &&
703                            get_field(env->mstatus, MSTATUS_MPV);
704 
705         hs_mode_two_stage = env->priv == PRV_S &&
706                             !riscv_cpu_virt_enabled(env) &&
707                             access_type != MMU_INST_FETCH &&
708                             get_field(env->hstatus, HSTATUS_SPRV) &&
709                             get_field(env->hstatus, HSTATUS_SPV);
710     }
711 
712     if (mode == PRV_M && access_type != MMU_INST_FETCH) {
713         if (get_field(env->mstatus, MSTATUS_MPRV)) {
714             mode = get_field(env->mstatus, MSTATUS_MPP);
715         }
716     }
717 
718     if (riscv_cpu_virt_enabled(env) || m_mode_two_stage || hs_mode_two_stage) {
719         /* Two stage lookup */
720         ret = get_physical_address(env, &pa, &prot, address, access_type,
721                                    mmu_idx, true, true);
722 
723         qemu_log_mask(CPU_LOG_MMU,
724                       "%s 1st-stage address=%" VADDR_PRIx " ret %d physical "
725                       TARGET_FMT_plx " prot %d\n",
726                       __func__, address, ret, pa, prot);
727 
728         if (ret != TRANSLATE_FAIL) {
729             /* Second stage lookup */
730             im_address = pa;
731 
732             ret = get_physical_address(env, &pa, &prot, im_address,
733                                        access_type, mmu_idx, false, true);
734 
735             qemu_log_mask(CPU_LOG_MMU,
736                     "%s 2nd-stage address=%" VADDR_PRIx " ret %d physical "
737                     TARGET_FMT_plx " prot %d\n",
738                     __func__, im_address, ret, pa, prot);
739 
740             if (riscv_feature(env, RISCV_FEATURE_PMP) &&
741                 (ret == TRANSLATE_SUCCESS) &&
742                 !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
743                 ret = TRANSLATE_PMP_FAIL;
744             }
745 
746             if (ret != TRANSLATE_SUCCESS) {
747                 /*
748                  * Guest physical address translation failed, this is a HS
749                  * level exception
750                  */
751                 first_stage_error = false;
752                 env->guest_phys_fault_addr = (im_address |
753                                               (address &
754                                                (TARGET_PAGE_SIZE - 1))) >> 2;
755             }
756         }
757     } else {
758         /* Single stage lookup */
759         ret = get_physical_address(env, &pa, &prot, address, access_type,
760                                    mmu_idx, true, false);
761 
762         qemu_log_mask(CPU_LOG_MMU,
763                       "%s address=%" VADDR_PRIx " ret %d physical "
764                       TARGET_FMT_plx " prot %d\n",
765                       __func__, address, ret, pa, prot);
766     }
767 
768     if (riscv_feature(env, RISCV_FEATURE_PMP) &&
769         (ret == TRANSLATE_SUCCESS) &&
770         !pmp_hart_has_privs(env, pa, size, 1 << access_type, mode)) {
771         ret = TRANSLATE_PMP_FAIL;
772     }
773     if (ret == TRANSLATE_PMP_FAIL) {
774         pmp_violation = true;
775     }
776 
777     if (ret == TRANSLATE_SUCCESS) {
778         tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK,
779                      prot, mmu_idx, TARGET_PAGE_SIZE);
780         return true;
781     } else if (probe) {
782         return false;
783     } else {
784         raise_mmu_exception(env, address, access_type, pmp_violation, first_stage_error);
785         riscv_raise_exception(env, cs->exception_index, retaddr);
786     }
787 
788     return true;
789 
790 #else
791     switch (access_type) {
792     case MMU_INST_FETCH:
793         cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT;
794         break;
795     case MMU_DATA_LOAD:
796         cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT;
797         break;
798     case MMU_DATA_STORE:
799         cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT;
800         break;
801     default:
802         g_assert_not_reached();
803     }
804     env->badaddr = address;
805     cpu_loop_exit_restore(cs, retaddr);
806 #endif
807 }
808 
809 /*
810  * Handle Traps
811  *
812  * Adapted from Spike's processor_t::take_trap.
813  *
814  */
815 void riscv_cpu_do_interrupt(CPUState *cs)
816 {
817 #if !defined(CONFIG_USER_ONLY)
818 
819     RISCVCPU *cpu = RISCV_CPU(cs);
820     CPURISCVState *env = &cpu->env;
821     bool force_hs_execp = riscv_cpu_force_hs_excep_enabled(env);
822     target_ulong s;
823 
824     /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide
825      * so we mask off the MSB and separate into trap type and cause.
826      */
827     bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG);
828     target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK;
829     target_ulong deleg = async ? env->mideleg : env->medeleg;
830     target_ulong tval = 0;
831 
832     if (!async) {
833         /* set tval to badaddr for traps with address information */
834         switch (cause) {
835         case RISCV_EXCP_INST_GUEST_PAGE_FAULT:
836         case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT:
837         case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT:
838             force_hs_execp = true;
839             /* fallthrough */
840         case RISCV_EXCP_INST_ADDR_MIS:
841         case RISCV_EXCP_INST_ACCESS_FAULT:
842         case RISCV_EXCP_LOAD_ADDR_MIS:
843         case RISCV_EXCP_STORE_AMO_ADDR_MIS:
844         case RISCV_EXCP_LOAD_ACCESS_FAULT:
845         case RISCV_EXCP_STORE_AMO_ACCESS_FAULT:
846         case RISCV_EXCP_INST_PAGE_FAULT:
847         case RISCV_EXCP_LOAD_PAGE_FAULT:
848         case RISCV_EXCP_STORE_PAGE_FAULT:
849             tval = env->badaddr;
850             break;
851         default:
852             break;
853         }
854         /* ecall is dispatched as one cause so translate based on mode */
855         if (cause == RISCV_EXCP_U_ECALL) {
856             assert(env->priv <= 3);
857 
858             if (env->priv == PRV_M) {
859                 cause = RISCV_EXCP_M_ECALL;
860             } else if (env->priv == PRV_S && riscv_cpu_virt_enabled(env)) {
861                 cause = RISCV_EXCP_VS_ECALL;
862             } else if (env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) {
863                 cause = RISCV_EXCP_S_ECALL;
864             } else if (env->priv == PRV_U) {
865                 cause = RISCV_EXCP_U_ECALL;
866             }
867         }
868     }
869 
870     trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, cause < 23 ?
871         (async ? riscv_intr_names : riscv_excp_names)[cause] : "(unknown)");
872 
873     if (env->priv <= PRV_S &&
874             cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) {
875         /* handle the trap in S-mode */
876         if (riscv_has_ext(env, RVH)) {
877             target_ulong hdeleg = async ? env->hideleg : env->hedeleg;
878 
879             if (riscv_cpu_virt_enabled(env) && ((hdeleg >> cause) & 1) &&
880                 !force_hs_execp) {
881                 /* Trap to VS mode */
882             } else if (riscv_cpu_virt_enabled(env)) {
883                 /* Trap into HS mode, from virt */
884                 riscv_cpu_swap_hypervisor_regs(env);
885                 env->hstatus = set_field(env->hstatus, HSTATUS_SP2V,
886                                          get_field(env->hstatus, HSTATUS_SPV));
887                 env->hstatus = set_field(env->hstatus, HSTATUS_SP2P,
888                                          get_field(env->mstatus, SSTATUS_SPP));
889                 env->hstatus = set_field(env->hstatus, HSTATUS_SPV,
890                                          riscv_cpu_virt_enabled(env));
891 
892                 riscv_cpu_set_virt_enabled(env, 0);
893                 riscv_cpu_set_force_hs_excep(env, 0);
894             } else {
895                 /* Trap into HS mode */
896                 env->hstatus = set_field(env->hstatus, HSTATUS_SP2V,
897                                          get_field(env->hstatus, HSTATUS_SPV));
898                 env->hstatus = set_field(env->hstatus, HSTATUS_SP2P,
899                                          get_field(env->mstatus, SSTATUS_SPP));
900                 env->hstatus = set_field(env->hstatus, HSTATUS_SPV,
901                                          riscv_cpu_virt_enabled(env));
902             }
903         }
904 
905         s = env->mstatus;
906         s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ?
907             get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv));
908         s = set_field(s, MSTATUS_SPP, env->priv);
909         s = set_field(s, MSTATUS_SIE, 0);
910         env->mstatus = s;
911         env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1));
912         env->sepc = env->pc;
913         env->sbadaddr = tval;
914         env->pc = (env->stvec >> 2 << 2) +
915             ((async && (env->stvec & 3) == 1) ? cause * 4 : 0);
916         riscv_cpu_set_mode(env, PRV_S);
917     } else {
918         /* handle the trap in M-mode */
919         if (riscv_has_ext(env, RVH)) {
920             if (riscv_cpu_virt_enabled(env)) {
921                 riscv_cpu_swap_hypervisor_regs(env);
922             }
923             env->mstatus = set_field(env->mstatus, MSTATUS_MPV,
924                                       riscv_cpu_virt_enabled(env));
925             env->mstatus = set_field(env->mstatus, MSTATUS_MTL,
926                                       riscv_cpu_force_hs_excep_enabled(env));
927 
928             /* Trapping to M mode, virt is disabled */
929             riscv_cpu_set_virt_enabled(env, 0);
930             riscv_cpu_set_force_hs_excep(env, 0);
931         }
932 
933         s = env->mstatus;
934         s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ?
935             get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv));
936         s = set_field(s, MSTATUS_MPP, env->priv);
937         s = set_field(s, MSTATUS_MIE, 0);
938         env->mstatus = s;
939         env->mcause = cause | ~(((target_ulong)-1) >> async);
940         env->mepc = env->pc;
941         env->mbadaddr = tval;
942         env->pc = (env->mtvec >> 2 << 2) +
943             ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0);
944         riscv_cpu_set_mode(env, PRV_M);
945     }
946 
947     /* NOTE: it is not necessary to yield load reservations here. It is only
948      * necessary for an SC from "another hart" to cause a load reservation
949      * to be yielded. Refer to the memory consistency model section of the
950      * RISC-V ISA Specification.
951      */
952 
953 #endif
954     cs->exception_index = EXCP_NONE; /* mark handled to qemu */
955 }
956