2 * ARM TLB (Translation lookaside buffer) helpers.
4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "qemu/osdep.h"
10 #include "internals.h"
11 #include "exec/exec-all.h"
12 #include "exec/helper-proto.h"
14 static inline uint32_t merge_syn_data_abort(uint32_t template_syn
,
15 unsigned int target_el
,
16 bool same_el
, bool ea
,
17 bool s1ptw
, bool is_write
,
23 * ISV is only set for data aborts routed to EL2 and
24 * never for stage-1 page table walks faulting on stage 2.
26 * Furthermore, ISV is only set for certain kinds of load/stores.
27 * If the template syndrome does not have ISV set, we should leave
30 * See ARMv8 specs, D7-1974:
31 * ISS encoding for an exception from a Data Abort, the
34 if (!(template_syn
& ARM_EL_ISV
) || target_el
!= 2 || s1ptw
) {
35 syn
= syn_data_abort_no_iss(same_el
, 0,
36 ea
, 0, s1ptw
, is_write
, fsc
);
39 * Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
40 * syndrome created at translation time.
41 * Now we create the runtime syndrome with the remaining fields.
43 syn
= syn_data_abort_with_iss(same_el
,
45 ea
, 0, s1ptw
, is_write
, fsc
,
47 /* Merge the runtime syndrome with the template syndrome. */
53 static uint32_t compute_fsr_fsc(CPUARMState
*env
, ARMMMUFaultInfo
*fi
,
54 int target_el
, int mmu_idx
, uint32_t *ret_fsc
)
56 ARMMMUIdx arm_mmu_idx
= core_to_arm_mmu_idx(env
, mmu_idx
);
59 if (target_el
== 2 || arm_el_is_aa64(env
, target_el
) ||
60 arm_s1_regime_using_lpae_format(env
, arm_mmu_idx
)) {
62 * LPAE format fault status register : bottom 6 bits are
63 * status code in the same form as needed for syndrome
65 fsr
= arm_fi_to_lfsc(fi
);
66 fsc
= extract32(fsr
, 0, 6);
68 fsr
= arm_fi_to_sfsc(fi
);
70 * Short format FSR : this fault will never actually be reported
71 * to an EL that uses a syndrome register. Use a (currently)
72 * reserved FSR code in case the constructed syndrome does leak
73 * into the guest somehow.
83 void arm_deliver_fault(ARMCPU
*cpu
, vaddr addr
,
84 MMUAccessType access_type
,
85 int mmu_idx
, ARMMMUFaultInfo
*fi
)
87 CPUARMState
*env
= &cpu
->env
;
90 uint32_t syn
, exc
, fsr
, fsc
;
92 target_el
= exception_target_el(env
);
95 env
->cp15
.hpfar_el2
= extract64(fi
->s2addr
, 12, 47) << 4;
96 if (arm_is_secure_below_el3(env
) && fi
->s1ns
) {
97 env
->cp15
.hpfar_el2
|= HPFAR_NS
;
100 same_el
= (arm_current_el(env
) == target_el
);
102 fsr
= compute_fsr_fsc(env
, fi
, target_el
, mmu_idx
, &fsc
);
104 if (access_type
== MMU_INST_FETCH
) {
105 syn
= syn_insn_abort(same_el
, fi
->ea
, fi
->s1ptw
, fsc
);
106 exc
= EXCP_PREFETCH_ABORT
;
108 syn
= merge_syn_data_abort(env
->exception
.syndrome
, target_el
,
109 same_el
, fi
->ea
, fi
->s1ptw
,
110 access_type
== MMU_DATA_STORE
,
112 if (access_type
== MMU_DATA_STORE
113 && arm_feature(env
, ARM_FEATURE_V6
)) {
116 exc
= EXCP_DATA_ABORT
;
119 env
->exception
.vaddress
= addr
;
120 env
->exception
.fsr
= fsr
;
121 raise_exception(env
, exc
, syn
, target_el
);
124 /* Raise a data fault alignment exception for the specified virtual address */
125 void arm_cpu_do_unaligned_access(CPUState
*cs
, vaddr vaddr
,
126 MMUAccessType access_type
,
127 int mmu_idx
, uintptr_t retaddr
)
129 ARMCPU
*cpu
= ARM_CPU(cs
);
130 ARMMMUFaultInfo fi
= {};
132 /* now we have a real cpu fault */
133 cpu_restore_state(cs
, retaddr
, true);
135 fi
.type
= ARMFault_Alignment
;
136 arm_deliver_fault(cpu
, vaddr
, access_type
, mmu_idx
, &fi
);
139 void helper_exception_pc_alignment(CPUARMState
*env
, target_ulong pc
)
141 ARMMMUFaultInfo fi
= { .type
= ARMFault_Alignment
};
142 int target_el
= exception_target_el(env
);
143 int mmu_idx
= cpu_mmu_index(env
, true);
146 env
->exception
.vaddress
= pc
;
149 * Note that the fsc is not applicable to this exception,
150 * since any syndrome is pcalignment not insn_abort.
152 env
->exception
.fsr
= compute_fsr_fsc(env
, &fi
, target_el
, mmu_idx
, &fsc
);
153 raise_exception(env
, EXCP_PREFETCH_ABORT
, syn_pcalignment(), target_el
);
156 #if !defined(CONFIG_USER_ONLY)
159 * arm_cpu_do_transaction_failed: handle a memory system error response
160 * (eg "no device/memory present at address") by raising an external abort
163 void arm_cpu_do_transaction_failed(CPUState
*cs
, hwaddr physaddr
,
164 vaddr addr
, unsigned size
,
165 MMUAccessType access_type
,
166 int mmu_idx
, MemTxAttrs attrs
,
167 MemTxResult response
, uintptr_t retaddr
)
169 ARMCPU
*cpu
= ARM_CPU(cs
);
170 ARMMMUFaultInfo fi
= {};
172 /* now we have a real cpu fault */
173 cpu_restore_state(cs
, retaddr
, true);
175 fi
.ea
= arm_extabort_type(response
);
176 fi
.type
= ARMFault_SyncExternal
;
177 arm_deliver_fault(cpu
, addr
, access_type
, mmu_idx
, &fi
);
180 bool arm_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
181 MMUAccessType access_type
, int mmu_idx
,
182 bool probe
, uintptr_t retaddr
)
184 ARMCPU
*cpu
= ARM_CPU(cs
);
185 ARMMMUFaultInfo fi
= {};
187 target_ulong page_size
;
189 MemTxAttrs attrs
= {};
190 ARMCacheAttrs cacheattrs
= {};
193 * Walk the page table and (if the mapping exists) add the page
194 * to the TLB. On success, return true. Otherwise, if probing,
195 * return false. Otherwise populate fsr with ARM DFSR/IFSR fault
196 * register format, and signal the fault.
198 ret
= get_phys_addr(&cpu
->env
, address
, access_type
,
199 core_to_arm_mmu_idx(&cpu
->env
, mmu_idx
),
200 &phys_addr
, &attrs
, &prot
, &page_size
,
204 * Map a single [sub]page. Regions smaller than our declared
205 * target page size are handled specially, so for those we
206 * pass in the exact addresses.
208 if (page_size
>= TARGET_PAGE_SIZE
) {
209 phys_addr
&= TARGET_PAGE_MASK
;
210 address
&= TARGET_PAGE_MASK
;
212 /* Notice and record tagged memory. */
213 if (cpu_isar_feature(aa64_mte
, cpu
) && cacheattrs
.attrs
== 0xf0) {
214 arm_tlb_mte_tagged(&attrs
) = true;
217 tlb_set_page_with_attrs(cs
, address
, phys_addr
, attrs
,
218 prot
, mmu_idx
, page_size
);
223 /* now we have a real cpu fault */
224 cpu_restore_state(cs
, retaddr
, true);
225 arm_deliver_fault(cpu
, address
, access_type
, mmu_idx
, &fi
);
229 void arm_cpu_record_sigsegv(CPUState
*cs
, vaddr addr
,
230 MMUAccessType access_type
,
231 bool maperr
, uintptr_t ra
)
233 ARMMMUFaultInfo fi
= {
234 .type
= maperr
? ARMFault_Translation
: ARMFault_Permission
,
237 ARMCPU
*cpu
= ARM_CPU(cs
);
240 * We report both ESR and FAR to signal handlers.
241 * For now, it's easiest to deliver the fault normally.
243 cpu_restore_state(cs
, ra
, true);
244 arm_deliver_fault(cpu
, addr
, access_type
, MMU_USER_IDX
, &fi
);
247 void arm_cpu_record_sigbus(CPUState
*cs
, vaddr addr
,
248 MMUAccessType access_type
, uintptr_t ra
)
250 arm_cpu_do_unaligned_access(cs
, addr
, access_type
, MMU_USER_IDX
, ra
);
252 #endif /* !defined(CONFIG_USER_ONLY) */