]>
Commit | Line | Data |
---|---|---|
dec9c2d4 AF |
1 | /* |
2 | * QEMU ARM CPU | |
3 | * | |
4 | * Copyright (c) 2012 SUSE LINUX Products GmbH | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see | |
18 | * <http://www.gnu.org/licenses/gpl-2.0.html> | |
19 | */ | |
20 | ||
74c21bd0 | 21 | #include "qemu/osdep.h" |
86480615 | 22 | #include "qemu/qemu-print.h" |
b8012ecf | 23 | #include "qemu/timer.h" |
8cc2246c | 24 | #include "qemu/log.h" |
ec5f7ca8 | 25 | #include "exec/page-vary.h" |
181962fd | 26 | #include "target/arm/idau.h" |
0b8fa32f | 27 | #include "qemu/module.h" |
da34e65c | 28 | #include "qapi/error.h" |
778c3a06 | 29 | #include "cpu.h" |
78271684 CF |
30 | #ifdef CONFIG_TCG |
31 | #include "hw/core/tcg-cpu-ops.h" | |
32 | #endif /* CONFIG_TCG */ | |
ccd38087 | 33 | #include "internals.h" |
5a534314 | 34 | #include "cpu-features.h" |
63c91552 | 35 | #include "exec/exec-all.h" |
5de16430 | 36 | #include "hw/qdev-properties.h" |
3c30dd5a PM |
37 | #if !defined(CONFIG_USER_ONLY) |
38 | #include "hw/loader.h" | |
cc7d44c2 | 39 | #include "hw/boards.h" |
165876f2 | 40 | #ifdef CONFIG_TCG |
8f4e07c9 | 41 | #include "hw/intc/armv7m_nvic.h" |
165876f2 PMD |
42 | #endif /* CONFIG_TCG */ |
43 | #endif /* !CONFIG_USER_ONLY */ | |
14a48c1d | 44 | #include "sysemu/tcg.h" |
045e5064 | 45 | #include "sysemu/qtest.h" |
b3946626 | 46 | #include "sysemu/hw_accel.h" |
50a2c6e5 | 47 | #include "kvm_arm.h" |
110f6c70 | 48 | #include "disas/capstone.h" |
24f91e81 | 49 | #include "fpu/softfloat.h" |
cf7c6d10 | 50 | #include "cpregs.h" |
dec9c2d4 | 51 | |
f45748f1 AF |
52 | static void arm_cpu_set_pc(CPUState *cs, vaddr value) |
53 | { | |
54 | ARMCPU *cpu = ARM_CPU(cs); | |
42f6ed91 JS |
55 | CPUARMState *env = &cpu->env; |
56 | ||
57 | if (is_a64(env)) { | |
58 | env->pc = value; | |
063bbd80 | 59 | env->thumb = false; |
42f6ed91 JS |
60 | } else { |
61 | env->regs[15] = value & ~1; | |
62 | env->thumb = value & 1; | |
63 | } | |
64 | } | |
f45748f1 | 65 | |
e4fdf9df RH |
66 | static vaddr arm_cpu_get_pc(CPUState *cs) |
67 | { | |
68 | ARMCPU *cpu = ARM_CPU(cs); | |
69 | CPUARMState *env = &cpu->env; | |
70 | ||
71 | if (is_a64(env)) { | |
72 | return env->pc; | |
73 | } else { | |
74 | return env->regs[15]; | |
75 | } | |
76 | } | |
77 | ||
ec62595b | 78 | #ifdef CONFIG_TCG |
78271684 CF |
79 | void arm_cpu_synchronize_from_tb(CPUState *cs, |
80 | const TranslationBlock *tb) | |
42f6ed91 | 81 | { |
03a648c4 AJ |
82 | /* The program counter is always up to date with CF_PCREL. */ |
83 | if (!(tb_cflags(tb) & CF_PCREL)) { | |
b77af26e | 84 | CPUARMState *env = cpu_env(cs); |
abb80995 RH |
85 | /* |
86 | * It's OK to look at env for the current mode here, because it's | |
87 | * never possible for an AArch64 TB to chain to an AArch32 TB. | |
88 | */ | |
89 | if (is_a64(env)) { | |
f51a1dd7 | 90 | env->pc = tb->pc; |
abb80995 | 91 | } else { |
f51a1dd7 | 92 | env->regs[15] = tb->pc; |
abb80995 | 93 | } |
42f6ed91 | 94 | } |
f45748f1 | 95 | } |
56c6c98d | 96 | |
475e56b6 EE |
97 | void arm_restore_state_to_opc(CPUState *cs, |
98 | const TranslationBlock *tb, | |
99 | const uint64_t *data) | |
56c6c98d | 100 | { |
b77af26e | 101 | CPUARMState *env = cpu_env(cs); |
56c6c98d RH |
102 | |
103 | if (is_a64(env)) { | |
03a648c4 | 104 | if (tb_cflags(tb) & CF_PCREL) { |
56c6c98d RH |
105 | env->pc = (env->pc & TARGET_PAGE_MASK) | data[0]; |
106 | } else { | |
107 | env->pc = data[0]; | |
108 | } | |
109 | env->condexec_bits = 0; | |
110 | env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT; | |
111 | } else { | |
03a648c4 | 112 | if (tb_cflags(tb) & CF_PCREL) { |
56c6c98d RH |
113 | env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0]; |
114 | } else { | |
115 | env->regs[15] = data[0]; | |
116 | } | |
117 | env->condexec_bits = data[1]; | |
118 | env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT; | |
119 | } | |
120 | } | |
ec62595b | 121 | #endif /* CONFIG_TCG */ |
f45748f1 | 122 | |
8c2e1b00 AF |
123 | static bool arm_cpu_has_work(CPUState *cs) |
124 | { | |
543486db RH |
125 | ARMCPU *cpu = ARM_CPU(cs); |
126 | ||
062ba099 | 127 | return (cpu->power_state != PSCI_OFF) |
543486db | 128 | && cs->interrupt_request & |
136e67e9 | 129 | (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD |
3c29632f | 130 | | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR |
136e67e9 | 131 | | CPU_INTERRUPT_EXITTB); |
8c2e1b00 AF |
132 | } |
133 | ||
b5c53d1b AL |
134 | void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook, |
135 | void *opaque) | |
136 | { | |
137 | ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1); | |
138 | ||
139 | entry->hook = hook; | |
140 | entry->opaque = opaque; | |
141 | ||
142 | QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node); | |
143 | } | |
144 | ||
08267487 | 145 | void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook, |
bd7d00fc PM |
146 | void *opaque) |
147 | { | |
08267487 AL |
148 | ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1); |
149 | ||
150 | entry->hook = hook; | |
151 | entry->opaque = opaque; | |
152 | ||
153 | QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node); | |
bd7d00fc PM |
154 | } |
155 | ||
4b6a83fb PM |
156 | static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque) |
157 | { | |
158 | /* Reset a single ARMCPRegInfo register */ | |
159 | ARMCPRegInfo *ri = value; | |
160 | ARMCPU *cpu = opaque; | |
161 | ||
87c3f0f2 | 162 | if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) { |
4b6a83fb PM |
163 | return; |
164 | } | |
165 | ||
166 | if (ri->resetfn) { | |
167 | ri->resetfn(&cpu->env, ri); | |
168 | return; | |
169 | } | |
170 | ||
171 | /* A zero offset is never possible as it would be regs[0] | |
172 | * so we use it to indicate that reset is being handled elsewhere. | |
173 | * This is basically only used for fields in non-core coprocessors | |
174 | * (like the pxa2xx ones). | |
175 | */ | |
176 | if (!ri->fieldoffset) { | |
177 | return; | |
178 | } | |
179 | ||
67ed771d | 180 | if (cpreg_field_is_64bit(ri)) { |
4b6a83fb PM |
181 | CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue; |
182 | } else { | |
183 | CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue; | |
184 | } | |
185 | } | |
186 | ||
49a66191 PM |
187 | static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque) |
188 | { | |
189 | /* Purely an assertion check: we've already done reset once, | |
190 | * so now check that running the reset for the cpreg doesn't | |
191 | * change its value. This traps bugs where two different cpregs | |
192 | * both try to reset the same state field but to different values. | |
193 | */ | |
194 | ARMCPRegInfo *ri = value; | |
195 | ARMCPU *cpu = opaque; | |
196 | uint64_t oldvalue, newvalue; | |
197 | ||
87c3f0f2 | 198 | if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) { |
49a66191 PM |
199 | return; |
200 | } | |
201 | ||
202 | oldvalue = read_raw_cp_reg(&cpu->env, ri); | |
203 | cp_reg_reset(key, value, opaque); | |
204 | newvalue = read_raw_cp_reg(&cpu->env, ri); | |
205 | assert(oldvalue == newvalue); | |
206 | } | |
207 | ||
9130cade | 208 | static void arm_cpu_reset_hold(Object *obj) |
dec9c2d4 | 209 | { |
9130cade | 210 | CPUState *s = CPU(obj); |
dec9c2d4 AF |
211 | ARMCPU *cpu = ARM_CPU(s); |
212 | ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu); | |
3c30dd5a | 213 | CPUARMState *env = &cpu->env; |
3c30dd5a | 214 | |
9130cade PM |
215 | if (acc->parent_phases.hold) { |
216 | acc->parent_phases.hold(obj); | |
217 | } | |
dec9c2d4 | 218 | |
1f5c00cf AB |
219 | memset(env, 0, offsetof(CPUARMState, end_reset_fields)); |
220 | ||
4b6a83fb | 221 | g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu); |
49a66191 PM |
222 | g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu); |
223 | ||
3c30dd5a | 224 | env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid; |
47576b94 RH |
225 | env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0; |
226 | env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1; | |
227 | env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2; | |
3c30dd5a | 228 | |
c1b70158 | 229 | cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON; |
543486db | 230 | |
3c30dd5a PM |
231 | if (arm_feature(env, ARM_FEATURE_IWMMXT)) { |
232 | env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q'; | |
233 | } | |
234 | ||
3926cc84 AG |
235 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { |
236 | /* 64 bit CPUs always start in 64 bit mode */ | |
53221552 | 237 | env->aarch64 = true; |
d356312f PM |
238 | #if defined(CONFIG_USER_ONLY) |
239 | env->pstate = PSTATE_MODE_EL0t; | |
14e5f106 | 240 | /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */ |
137feaa9 | 241 | env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE; |
276c6e81 RH |
242 | /* Enable all PAC keys. */ |
243 | env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB | | |
244 | SCTLR_EnDA | SCTLR_EnDB); | |
cda86e2b RH |
245 | /* Trap on btype=3 for PACIxSP. */ |
246 | env->cp15.sctlr_el[1] |= SCTLR_BT0; | |
d03396a8 RH |
247 | /* Trap on implementation defined registers. */ |
248 | if (cpu_isar_feature(aa64_tidcp1, cpu)) { | |
249 | env->cp15.sctlr_el[1] |= SCTLR_TIDCP; | |
250 | } | |
8c6afa6a | 251 | /* and to the FP/Neon instructions */ |
fab8ad39 RH |
252 | env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, |
253 | CPACR_EL1, FPEN, 3); | |
46303535 | 254 | /* and to the SVE instructions, with default vector length */ |
7b6a2198 | 255 | if (cpu_isar_feature(aa64_sve, cpu)) { |
46303535 RH |
256 | env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, |
257 | CPACR_EL1, ZEN, 3); | |
87252bde | 258 | env->vfp.zcr_el[1] = cpu->sve_default_vq - 1; |
7b6a2198 | 259 | } |
78011586 RH |
260 | /* and for SME instructions, with default vector length, and TPIDR2 */ |
261 | if (cpu_isar_feature(aa64_sme, cpu)) { | |
262 | env->cp15.sctlr_el[1] |= SCTLR_EnTP2; | |
263 | env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, | |
264 | CPACR_EL1, SMEN, 3); | |
265 | env->vfp.smcr_el[1] = cpu->sme_default_vq - 1; | |
266 | if (cpu_isar_feature(aa64_sme_fa64, cpu)) { | |
267 | env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1], | |
268 | SMCR, FA64, 1); | |
269 | } | |
270 | } | |
f6a148fe | 271 | /* |
691f1ffd | 272 | * Enable 48-bit address space (TODO: take reserved_va into account). |
16c84978 RH |
273 | * Enable TBI0 but not TBI1. |
274 | * Note that this must match useronly_clean_ptr. | |
f6a148fe | 275 | */ |
cb4a0a34 | 276 | env->cp15.tcr_el[1] = 5 | (1ULL << 37); |
e3232864 RH |
277 | |
278 | /* Enable MTE */ | |
279 | if (cpu_isar_feature(aa64_mte, cpu)) { | |
280 | /* Enable tag access, but leave TCF0 as No Effect (0). */ | |
281 | env->cp15.sctlr_el[1] |= SCTLR_ATA0; | |
282 | /* | |
283 | * Exclude all tags, so that tag 0 is always used. | |
284 | * This corresponds to Linux current->thread.gcr_incl = 0. | |
285 | * | |
286 | * Set RRND, so that helper_irg() will generate a seed later. | |
287 | * Here in cpu_reset(), the crypto subsystem has not yet been | |
288 | * initialized. | |
289 | */ | |
290 | env->cp15.gcr_el1 = 0x1ffff; | |
291 | } | |
7cb1e618 RH |
292 | /* |
293 | * Disable access to SCXTNUM_EL0 from CSV2_1p2. | |
294 | * This is not yet exposed from the Linux kernel in any way. | |
295 | */ | |
296 | env->cp15.sctlr_el[1] |= SCTLR_TSCXT; | |
f9ac7788 ZS |
297 | /* Disable access to Debug Communication Channel (DCC). */ |
298 | env->cp15.mdscr_el1 |= 1 << 12; | |
a6b2c5a0 PM |
299 | /* Enable FEAT_MOPS */ |
300 | env->cp15.sctlr_el[1] |= SCTLR_MSCEN; | |
d356312f | 301 | #else |
5097227c GB |
302 | /* Reset into the highest available EL */ |
303 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
304 | env->pstate = PSTATE_MODE_EL3h; | |
305 | } else if (arm_feature(env, ARM_FEATURE_EL2)) { | |
306 | env->pstate = PSTATE_MODE_EL2h; | |
307 | } else { | |
308 | env->pstate = PSTATE_MODE_EL1h; | |
309 | } | |
4a7319b7 EI |
310 | |
311 | /* Sample rvbar at reset. */ | |
312 | env->cp15.rvbar = cpu->rvbar_prop; | |
313 | env->pc = env->cp15.rvbar; | |
8c6afa6a PM |
314 | #endif |
315 | } else { | |
316 | #if defined(CONFIG_USER_ONLY) | |
317 | /* Userspace expects access to cp10 and cp11 for FP/Neon */ | |
fab8ad39 RH |
318 | env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, |
319 | CPACR, CP10, 3); | |
320 | env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, | |
321 | CPACR, CP11, 3); | |
d356312f | 322 | #endif |
910e4f24 TR |
323 | if (arm_feature(env, ARM_FEATURE_V8)) { |
324 | env->cp15.rvbar = cpu->rvbar_prop; | |
325 | env->regs[15] = cpu->rvbar_prop; | |
326 | } | |
3926cc84 AG |
327 | } |
328 | ||
3c30dd5a PM |
329 | #if defined(CONFIG_USER_ONLY) |
330 | env->uncached_cpsr = ARM_CPU_MODE_USR; | |
331 | /* For user mode we must enable access to coprocessors */ | |
332 | env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30; | |
333 | if (arm_feature(env, ARM_FEATURE_IWMMXT)) { | |
334 | env->cp15.c15_cpar = 3; | |
335 | } else if (arm_feature(env, ARM_FEATURE_XSCALE)) { | |
336 | env->cp15.c15_cpar = 1; | |
337 | } | |
338 | #else | |
060a65df PM |
339 | |
340 | /* | |
341 | * If the highest available EL is EL2, AArch32 will start in Hyp | |
342 | * mode; otherwise it starts in SVC. Note that if we start in | |
343 | * AArch64 then these values in the uncached_cpsr will be ignored. | |
344 | */ | |
345 | if (arm_feature(env, ARM_FEATURE_EL2) && | |
346 | !arm_feature(env, ARM_FEATURE_EL3)) { | |
347 | env->uncached_cpsr = ARM_CPU_MODE_HYP; | |
348 | } else { | |
349 | env->uncached_cpsr = ARM_CPU_MODE_SVC; | |
350 | } | |
4cc35614 | 351 | env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F; |
1426f244 PM |
352 | |
353 | /* AArch32 has a hard highvec setting of 0xFFFF0000. If we are currently | |
354 | * executing as AArch32 then check if highvecs are enabled and | |
355 | * adjust the PC accordingly. | |
356 | */ | |
357 | if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { | |
358 | env->regs[15] = 0xFFFF0000; | |
359 | } | |
360 | ||
361 | env->vfp.xregs[ARM_VFP_FPEXC] = 0; | |
b62ceeaf | 362 | #endif |
dc7abe4d | 363 | |
531c60a9 | 364 | if (arm_feature(env, ARM_FEATURE_M)) { |
b62ceeaf | 365 | #ifndef CONFIG_USER_ONLY |
6e3cf5df MG |
366 | uint32_t initial_msp; /* Loaded from 0x0 */ |
367 | uint32_t initial_pc; /* Loaded from 0x4 */ | |
3c30dd5a | 368 | uint8_t *rom; |
38e2a77c | 369 | uint32_t vecbase; |
b62ceeaf | 370 | #endif |
6e3cf5df | 371 | |
8128c8e8 PM |
372 | if (cpu_isar_feature(aa32_lob, cpu)) { |
373 | /* | |
374 | * LTPSIZE is constant 4 if MVE not implemented, and resets | |
375 | * to an UNKNOWN value if MVE is implemented. We choose to | |
376 | * always reset to 4. | |
377 | */ | |
378 | env->v7m.ltpsize = 4; | |
99c7834f PM |
379 | /* The LTPSIZE field in FPDSCR is constant and reads as 4. */ |
380 | env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT; | |
381 | env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT; | |
8128c8e8 PM |
382 | } |
383 | ||
1e577cc7 PM |
384 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { |
385 | env->v7m.secure = true; | |
3b2e9344 PM |
386 | } else { |
387 | /* This bit resets to 0 if security is supported, but 1 if | |
388 | * it is not. The bit is not present in v7M, but we set it | |
389 | * here so we can avoid having to make checks on it conditional | |
390 | * on ARM_FEATURE_V8 (we don't let the guest see the bit). | |
391 | */ | |
392 | env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK; | |
02ac2f7f PM |
393 | /* |
394 | * Set NSACR to indicate "NS access permitted to everything"; | |
395 | * this avoids having to have all the tests of it being | |
396 | * conditional on ARM_FEATURE_M_SECURITY. Note also that from | |
397 | * v8.1M the guest-visible value of NSACR in a CPU without the | |
398 | * Security Extension is 0xcff. | |
399 | */ | |
400 | env->v7m.nsacr = 0xcff; | |
1e577cc7 PM |
401 | } |
402 | ||
9d40cd8a | 403 | /* In v7M the reset value of this bit is IMPDEF, but ARM recommends |
2c4da50d | 404 | * that it resets to 1, so QEMU always does that rather than making |
9d40cd8a | 405 | * it dependent on CPU model. In v8M it is RES1. |
2c4da50d | 406 | */ |
9d40cd8a PM |
407 | env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK; |
408 | env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK; | |
409 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
410 | /* in v8M the NONBASETHRDENA bit [0] is RES1 */ | |
411 | env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK; | |
412 | env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK; | |
413 | } | |
22ab3460 JS |
414 | if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { |
415 | env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK; | |
416 | env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK; | |
417 | } | |
2c4da50d | 418 | |
7fbc6a40 | 419 | if (cpu_isar_feature(aa32_vfp_simd, cpu)) { |
d33abe82 PM |
420 | env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK; |
421 | env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK | | |
422 | R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK; | |
423 | } | |
b62ceeaf PM |
424 | |
425 | #ifndef CONFIG_USER_ONLY | |
056f43df PM |
426 | /* Unlike A/R profile, M profile defines the reset LR value */ |
427 | env->regs[14] = 0xffffffff; | |
428 | ||
38e2a77c | 429 | env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80; |
7cda2149 | 430 | env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80; |
38e2a77c PM |
431 | |
432 | /* Load the initial SP and PC from offset 0 and 4 in the vector table */ | |
433 | vecbase = env->v7m.vecbase[env->v7m.secure]; | |
75ce72b7 | 434 | rom = rom_ptr_for_as(s->as, vecbase, 8); |
3c30dd5a | 435 | if (rom) { |
6e3cf5df MG |
436 | /* Address zero is covered by ROM which hasn't yet been |
437 | * copied into physical memory. | |
438 | */ | |
439 | initial_msp = ldl_p(rom); | |
440 | initial_pc = ldl_p(rom + 4); | |
441 | } else { | |
442 | /* Address zero not covered by a ROM blob, or the ROM blob | |
443 | * is in non-modifiable memory and this is a second reset after | |
444 | * it got copied into memory. In the latter case, rom_ptr | |
445 | * will return a NULL pointer and we should use ldl_phys instead. | |
446 | */ | |
38e2a77c PM |
447 | initial_msp = ldl_phys(s->as, vecbase); |
448 | initial_pc = ldl_phys(s->as, vecbase + 4); | |
3c30dd5a | 449 | } |
6e3cf5df | 450 | |
8cc2246c PM |
451 | qemu_log_mask(CPU_LOG_INT, |
452 | "Loaded reset SP 0x%x PC 0x%x from vector table\n", | |
453 | initial_msp, initial_pc); | |
454 | ||
6e3cf5df MG |
455 | env->regs[13] = initial_msp & 0xFFFFFFFC; |
456 | env->regs[15] = initial_pc & ~1; | |
457 | env->thumb = initial_pc & 1; | |
b62ceeaf PM |
458 | #else |
459 | /* | |
460 | * For user mode we run non-secure and with access to the FPU. | |
461 | * The FPU context is active (ie does not need further setup) | |
462 | * and is owned by non-secure. | |
463 | */ | |
464 | env->v7m.secure = false; | |
465 | env->v7m.nsacr = 0xcff; | |
466 | env->v7m.cpacr[M_REG_NS] = 0xf0ffff; | |
467 | env->v7m.fpccr[M_REG_S] &= | |
468 | ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK); | |
469 | env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK; | |
470 | #endif | |
3c30dd5a | 471 | } |
387f9806 | 472 | |
dc3c4c14 PM |
473 | /* M profile requires that reset clears the exclusive monitor; |
474 | * A profile does not, but clearing it makes more sense than having it | |
475 | * set with an exclusive access on address zero. | |
476 | */ | |
477 | arm_clear_exclusive(env); | |
478 | ||
0e1a46bb | 479 | if (arm_feature(env, ARM_FEATURE_PMSA)) { |
69ceea64 | 480 | if (cpu->pmsav7_dregion > 0) { |
0e1a46bb | 481 | if (arm_feature(env, ARM_FEATURE_V8)) { |
62c58ee0 PM |
482 | memset(env->pmsav8.rbar[M_REG_NS], 0, |
483 | sizeof(*env->pmsav8.rbar[M_REG_NS]) | |
484 | * cpu->pmsav7_dregion); | |
485 | memset(env->pmsav8.rlar[M_REG_NS], 0, | |
486 | sizeof(*env->pmsav8.rlar[M_REG_NS]) | |
487 | * cpu->pmsav7_dregion); | |
488 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
489 | memset(env->pmsav8.rbar[M_REG_S], 0, | |
490 | sizeof(*env->pmsav8.rbar[M_REG_S]) | |
491 | * cpu->pmsav7_dregion); | |
492 | memset(env->pmsav8.rlar[M_REG_S], 0, | |
493 | sizeof(*env->pmsav8.rlar[M_REG_S]) | |
494 | * cpu->pmsav7_dregion); | |
495 | } | |
0e1a46bb PM |
496 | } else if (arm_feature(env, ARM_FEATURE_V7)) { |
497 | memset(env->pmsav7.drbar, 0, | |
498 | sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion); | |
499 | memset(env->pmsav7.drsr, 0, | |
500 | sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion); | |
501 | memset(env->pmsav7.dracr, 0, | |
502 | sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion); | |
503 | } | |
69ceea64 | 504 | } |
761c4642 TR |
505 | |
506 | if (cpu->pmsav8r_hdregion > 0) { | |
507 | memset(env->pmsav8.hprbar, 0, | |
508 | sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion); | |
509 | memset(env->pmsav8.hprlar, 0, | |
510 | sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion); | |
511 | } | |
512 | ||
1bc04a88 PM |
513 | env->pmsav7.rnr[M_REG_NS] = 0; |
514 | env->pmsav7.rnr[M_REG_S] = 0; | |
4125e6fe PM |
515 | env->pmsav8.mair0[M_REG_NS] = 0; |
516 | env->pmsav8.mair0[M_REG_S] = 0; | |
517 | env->pmsav8.mair1[M_REG_NS] = 0; | |
518 | env->pmsav8.mair1[M_REG_S] = 0; | |
69ceea64 PM |
519 | } |
520 | ||
9901c576 PM |
521 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { |
522 | if (cpu->sau_sregion > 0) { | |
523 | memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion); | |
524 | memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion); | |
525 | } | |
526 | env->sau.rnr = 0; | |
527 | /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what | |
528 | * the Cortex-M33 does. | |
529 | */ | |
530 | env->sau.ctrl = 0; | |
531 | } | |
532 | ||
3c30dd5a PM |
533 | set_flush_to_zero(1, &env->vfp.standard_fp_status); |
534 | set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status); | |
535 | set_default_nan_mode(1, &env->vfp.standard_fp_status); | |
aaae563b | 536 | set_default_nan_mode(1, &env->vfp.standard_fp_status_f16); |
3c30dd5a PM |
537 | set_float_detect_tininess(float_tininess_before_rounding, |
538 | &env->vfp.fp_status); | |
539 | set_float_detect_tininess(float_tininess_before_rounding, | |
540 | &env->vfp.standard_fp_status); | |
bcc531f0 PM |
541 | set_float_detect_tininess(float_tininess_before_rounding, |
542 | &env->vfp.fp_status_f16); | |
aaae563b PM |
543 | set_float_detect_tininess(float_tininess_before_rounding, |
544 | &env->vfp.standard_fp_status_f16); | |
50a2c6e5 PB |
545 | #ifndef CONFIG_USER_ONLY |
546 | if (kvm_enabled()) { | |
547 | kvm_arm_reset_vcpu(cpu); | |
548 | } | |
549 | #endif | |
9ee98ce8 | 550 | |
fa05d1ab FR |
551 | if (tcg_enabled()) { |
552 | hw_breakpoint_update_all(cpu); | |
553 | hw_watchpoint_update_all(cpu); | |
2b77ad4d FR |
554 | |
555 | arm_rebuild_hflags(env); | |
fa05d1ab | 556 | } |
dec9c2d4 AF |
557 | } |
558 | ||
3a45f4f5 PM |
559 | void arm_emulate_firmware_reset(CPUState *cpustate, int target_el) |
560 | { | |
561 | ARMCPU *cpu = ARM_CPU(cpustate); | |
562 | CPUARMState *env = &cpu->env; | |
563 | bool have_el3 = arm_feature(env, ARM_FEATURE_EL3); | |
564 | bool have_el2 = arm_feature(env, ARM_FEATURE_EL2); | |
565 | ||
566 | /* | |
567 | * Check we have the EL we're aiming for. If that is the | |
568 | * highest implemented EL, then cpu_reset has already done | |
569 | * all the work. | |
570 | */ | |
571 | switch (target_el) { | |
572 | case 3: | |
573 | assert(have_el3); | |
574 | return; | |
575 | case 2: | |
576 | assert(have_el2); | |
577 | if (!have_el3) { | |
578 | return; | |
579 | } | |
580 | break; | |
581 | case 1: | |
582 | if (!have_el3 && !have_el2) { | |
583 | return; | |
584 | } | |
585 | break; | |
586 | default: | |
587 | g_assert_not_reached(); | |
588 | } | |
589 | ||
590 | if (have_el3) { | |
591 | /* | |
592 | * Set the EL3 state so code can run at EL2. This should match | |
593 | * the requirements set by Linux in its booting spec. | |
594 | */ | |
595 | if (env->aarch64) { | |
596 | env->cp15.scr_el3 |= SCR_RW; | |
597 | if (cpu_isar_feature(aa64_pauth, cpu)) { | |
598 | env->cp15.scr_el3 |= SCR_API | SCR_APK; | |
599 | } | |
600 | if (cpu_isar_feature(aa64_mte, cpu)) { | |
601 | env->cp15.scr_el3 |= SCR_ATA; | |
602 | } | |
603 | if (cpu_isar_feature(aa64_sve, cpu)) { | |
604 | env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK; | |
605 | env->vfp.zcr_el[3] = 0xf; | |
606 | } | |
607 | if (cpu_isar_feature(aa64_sme, cpu)) { | |
608 | env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK; | |
609 | env->cp15.scr_el3 |= SCR_ENTP2; | |
610 | env->vfp.smcr_el[3] = 0xf; | |
611 | } | |
612 | if (cpu_isar_feature(aa64_hcx, cpu)) { | |
613 | env->cp15.scr_el3 |= SCR_HXEN; | |
614 | } | |
615 | if (cpu_isar_feature(aa64_fgt, cpu)) { | |
616 | env->cp15.scr_el3 |= SCR_FGTEN; | |
617 | } | |
618 | } | |
619 | ||
620 | if (target_el == 2) { | |
621 | /* If the guest is at EL2 then Linux expects the HVC insn to work */ | |
622 | env->cp15.scr_el3 |= SCR_HCE; | |
623 | } | |
624 | ||
625 | /* Put CPU into non-secure state */ | |
626 | env->cp15.scr_el3 |= SCR_NS; | |
627 | /* Set NSACR.{CP11,CP10} so NS can access the FPU */ | |
628 | env->cp15.nsacr |= 3 << 10; | |
629 | } | |
630 | ||
631 | if (have_el2 && target_el < 2) { | |
632 | /* Set EL2 state so code can run at EL1. */ | |
633 | if (env->aarch64) { | |
634 | env->cp15.hcr_el2 |= HCR_RW; | |
635 | } | |
636 | } | |
637 | ||
638 | /* Set the CPU to the desired state */ | |
639 | if (env->aarch64) { | |
640 | env->pstate = aarch64_pstate_mode(target_el, true); | |
641 | } else { | |
642 | static const uint32_t mode_for_el[] = { | |
643 | 0, | |
644 | ARM_CPU_MODE_SVC, | |
645 | ARM_CPU_MODE_HYP, | |
646 | ARM_CPU_MODE_SVC, | |
647 | }; | |
648 | ||
649 | cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw); | |
650 | } | |
651 | } | |
652 | ||
653 | ||
9e406eea | 654 | #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) |
083afd18 | 655 | |
310cedf3 | 656 | static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, |
be879556 RH |
657 | unsigned int target_el, |
658 | unsigned int cur_el, bool secure, | |
659 | uint64_t hcr_el2) | |
310cedf3 | 660 | { |
b77af26e | 661 | CPUARMState *env = cpu_env(cs); |
310cedf3 | 662 | bool pstate_unmasked; |
16e07f78 | 663 | bool unmasked = false; |
310cedf3 RH |
664 | |
665 | /* | |
666 | * Don't take exceptions if they target a lower EL. | |
667 | * This check should catch any exceptions that would not be taken | |
668 | * but left pending. | |
669 | */ | |
670 | if (cur_el > target_el) { | |
671 | return false; | |
672 | } | |
673 | ||
310cedf3 RH |
674 | switch (excp_idx) { |
675 | case EXCP_FIQ: | |
676 | pstate_unmasked = !(env->daif & PSTATE_F); | |
677 | break; | |
678 | ||
679 | case EXCP_IRQ: | |
680 | pstate_unmasked = !(env->daif & PSTATE_I); | |
681 | break; | |
682 | ||
683 | case EXCP_VFIQ: | |
cc974d5c RDC |
684 | if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) { |
685 | /* VFIQs are only taken when hypervized. */ | |
310cedf3 RH |
686 | return false; |
687 | } | |
688 | return !(env->daif & PSTATE_F); | |
689 | case EXCP_VIRQ: | |
cc974d5c RDC |
690 | if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) { |
691 | /* VIRQs are only taken when hypervized. */ | |
310cedf3 RH |
692 | return false; |
693 | } | |
694 | return !(env->daif & PSTATE_I); | |
3c29632f RH |
695 | case EXCP_VSERR: |
696 | if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) { | |
697 | /* VIRQs are only taken when hypervized. */ | |
698 | return false; | |
699 | } | |
700 | return !(env->daif & PSTATE_A); | |
310cedf3 RH |
701 | default: |
702 | g_assert_not_reached(); | |
703 | } | |
704 | ||
705 | /* | |
706 | * Use the target EL, current execution state and SCR/HCR settings to | |
707 | * determine whether the corresponding CPSR bit is used to mask the | |
708 | * interrupt. | |
709 | */ | |
710 | if ((target_el > cur_el) && (target_el != 1)) { | |
711 | /* Exceptions targeting a higher EL may not be maskable */ | |
712 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { | |
c939a7c7 AK |
713 | switch (target_el) { |
714 | case 2: | |
715 | /* | |
716 | * According to ARM DDI 0487H.a, an interrupt can be masked | |
717 | * when HCR_E2H and HCR_TGE are both set regardless of the | |
718 | * current Security state. Note that we need to revisit this | |
719 | * part again once we need to support NMI. | |
720 | */ | |
721 | if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { | |
722 | unmasked = true; | |
723 | } | |
724 | break; | |
725 | case 3: | |
726 | /* Interrupt cannot be masked when the target EL is 3 */ | |
16e07f78 | 727 | unmasked = true; |
c939a7c7 AK |
728 | break; |
729 | default: | |
730 | g_assert_not_reached(); | |
310cedf3 RH |
731 | } |
732 | } else { | |
733 | /* | |
734 | * The old 32-bit-only environment has a more complicated | |
735 | * masking setup. HCR and SCR bits not only affect interrupt | |
736 | * routing but also change the behaviour of masking. | |
737 | */ | |
738 | bool hcr, scr; | |
739 | ||
740 | switch (excp_idx) { | |
741 | case EXCP_FIQ: | |
742 | /* | |
743 | * If FIQs are routed to EL3 or EL2 then there are cases where | |
744 | * we override the CPSR.F in determining if the exception is | |
745 | * masked or not. If neither of these are set then we fall back | |
746 | * to the CPSR.F setting otherwise we further assess the state | |
747 | * below. | |
748 | */ | |
749 | hcr = hcr_el2 & HCR_FMO; | |
750 | scr = (env->cp15.scr_el3 & SCR_FIQ); | |
751 | ||
752 | /* | |
753 | * When EL3 is 32-bit, the SCR.FW bit controls whether the | |
754 | * CPSR.F bit masks FIQ interrupts when taken in non-secure | |
755 | * state. If SCR.FW is set then FIQs can be masked by CPSR.F | |
756 | * when non-secure but only when FIQs are only routed to EL3. | |
757 | */ | |
758 | scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr); | |
759 | break; | |
760 | case EXCP_IRQ: | |
761 | /* | |
762 | * When EL3 execution state is 32-bit, if HCR.IMO is set then | |
763 | * we may override the CPSR.I masking when in non-secure state. | |
764 | * The SCR.IRQ setting has already been taken into consideration | |
765 | * when setting the target EL, so it does not have a further | |
766 | * affect here. | |
767 | */ | |
768 | hcr = hcr_el2 & HCR_IMO; | |
769 | scr = false; | |
770 | break; | |
771 | default: | |
772 | g_assert_not_reached(); | |
773 | } | |
774 | ||
775 | if ((scr || hcr) && !secure) { | |
16e07f78 | 776 | unmasked = true; |
310cedf3 RH |
777 | } |
778 | } | |
779 | } | |
780 | ||
781 | /* | |
673d8215 | 782 | * The PSTATE bits only mask the interrupt if we have not overridden the |
310cedf3 RH |
783 | * ability above. |
784 | */ | |
785 | return unmasked || pstate_unmasked; | |
786 | } | |
787 | ||
083afd18 | 788 | static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request) |
e8925712 RH |
789 | { |
790 | CPUClass *cc = CPU_GET_CLASS(cs); | |
b77af26e | 791 | CPUARMState *env = cpu_env(cs); |
012a906b GB |
792 | uint32_t cur_el = arm_current_el(env); |
793 | bool secure = arm_is_secure(env); | |
be879556 | 794 | uint64_t hcr_el2 = arm_hcr_el2_eff(env); |
012a906b GB |
795 | uint32_t target_el; |
796 | uint32_t excp_idx; | |
d63d0ec5 RH |
797 | |
798 | /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */ | |
e8925712 | 799 | |
012a906b GB |
800 | if (interrupt_request & CPU_INTERRUPT_FIQ) { |
801 | excp_idx = EXCP_FIQ; | |
802 | target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); | |
be879556 RH |
803 | if (arm_excp_unmasked(cs, excp_idx, target_el, |
804 | cur_el, secure, hcr_el2)) { | |
d63d0ec5 | 805 | goto found; |
012a906b | 806 | } |
e8925712 | 807 | } |
012a906b GB |
808 | if (interrupt_request & CPU_INTERRUPT_HARD) { |
809 | excp_idx = EXCP_IRQ; | |
810 | target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); | |
be879556 RH |
811 | if (arm_excp_unmasked(cs, excp_idx, target_el, |
812 | cur_el, secure, hcr_el2)) { | |
d63d0ec5 | 813 | goto found; |
012a906b | 814 | } |
e8925712 | 815 | } |
012a906b GB |
816 | if (interrupt_request & CPU_INTERRUPT_VIRQ) { |
817 | excp_idx = EXCP_VIRQ; | |
818 | target_el = 1; | |
be879556 RH |
819 | if (arm_excp_unmasked(cs, excp_idx, target_el, |
820 | cur_el, secure, hcr_el2)) { | |
d63d0ec5 | 821 | goto found; |
012a906b | 822 | } |
136e67e9 | 823 | } |
012a906b GB |
824 | if (interrupt_request & CPU_INTERRUPT_VFIQ) { |
825 | excp_idx = EXCP_VFIQ; | |
826 | target_el = 1; | |
be879556 RH |
827 | if (arm_excp_unmasked(cs, excp_idx, target_el, |
828 | cur_el, secure, hcr_el2)) { | |
d63d0ec5 | 829 | goto found; |
012a906b | 830 | } |
136e67e9 | 831 | } |
3c29632f RH |
832 | if (interrupt_request & CPU_INTERRUPT_VSERR) { |
833 | excp_idx = EXCP_VSERR; | |
834 | target_el = 1; | |
835 | if (arm_excp_unmasked(cs, excp_idx, target_el, | |
836 | cur_el, secure, hcr_el2)) { | |
837 | /* Taking a virtual abort clears HCR_EL2.VSE */ | |
838 | env->cp15.hcr_el2 &= ~HCR_VSE; | |
839 | cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR); | |
840 | goto found; | |
841 | } | |
842 | } | |
d63d0ec5 | 843 | return false; |
e8925712 | 844 | |
d63d0ec5 RH |
845 | found: |
846 | cs->exception_index = excp_idx; | |
847 | env->exception.target_el = target_el; | |
78271684 | 848 | cc->tcg_ops->do_interrupt(cs); |
d63d0ec5 | 849 | return true; |
e8925712 | 850 | } |
9e406eea PMD |
851 | |
852 | #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */ | |
e8925712 | 853 | |
89430fc6 PM |
854 | void arm_cpu_update_virq(ARMCPU *cpu) |
855 | { | |
856 | /* | |
857 | * Update the interrupt level for VIRQ, which is the logical OR of | |
858 | * the HCR_EL2.VI bit and the input line level from the GIC. | |
859 | */ | |
860 | CPUARMState *env = &cpu->env; | |
861 | CPUState *cs = CPU(cpu); | |
862 | ||
863 | bool new_state = (env->cp15.hcr_el2 & HCR_VI) || | |
864 | (env->irq_line_state & CPU_INTERRUPT_VIRQ); | |
865 | ||
866 | if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) { | |
867 | if (new_state) { | |
868 | cpu_interrupt(cs, CPU_INTERRUPT_VIRQ); | |
869 | } else { | |
870 | cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ); | |
871 | } | |
872 | } | |
873 | } | |
874 | ||
875 | void arm_cpu_update_vfiq(ARMCPU *cpu) | |
876 | { | |
877 | /* | |
878 | * Update the interrupt level for VFIQ, which is the logical OR of | |
879 | * the HCR_EL2.VF bit and the input line level from the GIC. | |
880 | */ | |
881 | CPUARMState *env = &cpu->env; | |
882 | CPUState *cs = CPU(cpu); | |
883 | ||
884 | bool new_state = (env->cp15.hcr_el2 & HCR_VF) || | |
885 | (env->irq_line_state & CPU_INTERRUPT_VFIQ); | |
886 | ||
887 | if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) { | |
888 | if (new_state) { | |
889 | cpu_interrupt(cs, CPU_INTERRUPT_VFIQ); | |
890 | } else { | |
891 | cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ); | |
892 | } | |
893 | } | |
894 | } | |
895 | ||
3c29632f RH |
896 | void arm_cpu_update_vserr(ARMCPU *cpu) |
897 | { | |
898 | /* | |
899 | * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit. | |
900 | */ | |
901 | CPUARMState *env = &cpu->env; | |
902 | CPUState *cs = CPU(cpu); | |
903 | ||
904 | bool new_state = env->cp15.hcr_el2 & HCR_VSE; | |
905 | ||
906 | if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) { | |
907 | if (new_state) { | |
908 | cpu_interrupt(cs, CPU_INTERRUPT_VSERR); | |
909 | } else { | |
910 | cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR); | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
7c1840b6 PM |
915 | #ifndef CONFIG_USER_ONLY |
916 | static void arm_cpu_set_irq(void *opaque, int irq, int level) | |
917 | { | |
918 | ARMCPU *cpu = opaque; | |
136e67e9 | 919 | CPUARMState *env = &cpu->env; |
7c1840b6 | 920 | CPUState *cs = CPU(cpu); |
136e67e9 EI |
921 | static const int mask[] = { |
922 | [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD, | |
923 | [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ, | |
924 | [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ, | |
925 | [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ | |
926 | }; | |
7c1840b6 | 927 | |
9acd2d33 PM |
928 | if (!arm_feature(env, ARM_FEATURE_EL2) && |
929 | (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) { | |
930 | /* | |
931 | * The GIC might tell us about VIRQ and VFIQ state, but if we don't | |
932 | * have EL2 support we don't care. (Unless the guest is doing something | |
933 | * silly this will only be calls saying "level is still 0".) | |
934 | */ | |
935 | return; | |
936 | } | |
937 | ||
ed89f078 PM |
938 | if (level) { |
939 | env->irq_line_state |= mask[irq]; | |
940 | } else { | |
941 | env->irq_line_state &= ~mask[irq]; | |
942 | } | |
943 | ||
7c1840b6 | 944 | switch (irq) { |
136e67e9 | 945 | case ARM_CPU_VIRQ: |
89430fc6 PM |
946 | arm_cpu_update_virq(cpu); |
947 | break; | |
136e67e9 | 948 | case ARM_CPU_VFIQ: |
89430fc6 PM |
949 | arm_cpu_update_vfiq(cpu); |
950 | break; | |
136e67e9 | 951 | case ARM_CPU_IRQ: |
7c1840b6 PM |
952 | case ARM_CPU_FIQ: |
953 | if (level) { | |
136e67e9 | 954 | cpu_interrupt(cs, mask[irq]); |
7c1840b6 | 955 | } else { |
136e67e9 | 956 | cpu_reset_interrupt(cs, mask[irq]); |
7c1840b6 PM |
957 | } |
958 | break; | |
959 | default: | |
8f6fd322 | 960 | g_assert_not_reached(); |
7c1840b6 PM |
961 | } |
962 | } | |
963 | ||
964 | static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level) | |
965 | { | |
966 | #ifdef CONFIG_KVM | |
967 | ARMCPU *cpu = opaque; | |
ed89f078 | 968 | CPUARMState *env = &cpu->env; |
7c1840b6 | 969 | CPUState *cs = CPU(cpu); |
ed89f078 | 970 | uint32_t linestate_bit; |
f6530926 | 971 | int irq_id; |
7c1840b6 PM |
972 | |
973 | switch (irq) { | |
974 | case ARM_CPU_IRQ: | |
f6530926 | 975 | irq_id = KVM_ARM_IRQ_CPU_IRQ; |
ed89f078 | 976 | linestate_bit = CPU_INTERRUPT_HARD; |
7c1840b6 PM |
977 | break; |
978 | case ARM_CPU_FIQ: | |
f6530926 | 979 | irq_id = KVM_ARM_IRQ_CPU_FIQ; |
ed89f078 | 980 | linestate_bit = CPU_INTERRUPT_FIQ; |
7c1840b6 PM |
981 | break; |
982 | default: | |
8f6fd322 | 983 | g_assert_not_reached(); |
7c1840b6 | 984 | } |
ed89f078 PM |
985 | |
986 | if (level) { | |
987 | env->irq_line_state |= linestate_bit; | |
988 | } else { | |
989 | env->irq_line_state &= ~linestate_bit; | |
990 | } | |
f6530926 | 991 | kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level); |
7c1840b6 PM |
992 | #endif |
993 | } | |
84f2bed3 | 994 | |
ed50ff78 | 995 | static bool arm_cpu_virtio_is_big_endian(CPUState *cs) |
84f2bed3 PS |
996 | { |
997 | ARMCPU *cpu = ARM_CPU(cs); | |
998 | CPUARMState *env = &cpu->env; | |
84f2bed3 PS |
999 | |
1000 | cpu_synchronize_state(cs); | |
ed50ff78 | 1001 | return arm_cpu_data_is_big_endian(env); |
84f2bed3 PS |
1002 | } |
1003 | ||
7c1840b6 PM |
1004 | #endif |
1005 | ||
48440620 PC |
1006 | static void arm_disas_set_info(CPUState *cpu, disassemble_info *info) |
1007 | { | |
1008 | ARMCPU *ac = ARM_CPU(cpu); | |
1009 | CPUARMState *env = &ac->env; | |
7bcdbf51 | 1010 | bool sctlr_b; |
48440620 PC |
1011 | |
1012 | if (is_a64(env)) { | |
110f6c70 | 1013 | info->cap_arch = CS_ARCH_ARM64; |
15fa1a0a RH |
1014 | info->cap_insn_unit = 4; |
1015 | info->cap_insn_split = 4; | |
48440620 | 1016 | } else { |
110f6c70 RH |
1017 | int cap_mode; |
1018 | if (env->thumb) { | |
15fa1a0a RH |
1019 | info->cap_insn_unit = 2; |
1020 | info->cap_insn_split = 4; | |
110f6c70 RH |
1021 | cap_mode = CS_MODE_THUMB; |
1022 | } else { | |
15fa1a0a RH |
1023 | info->cap_insn_unit = 4; |
1024 | info->cap_insn_split = 4; | |
110f6c70 RH |
1025 | cap_mode = CS_MODE_ARM; |
1026 | } | |
1027 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
1028 | cap_mode |= CS_MODE_V8; | |
1029 | } | |
1030 | if (arm_feature(env, ARM_FEATURE_M)) { | |
1031 | cap_mode |= CS_MODE_MCLASS; | |
1032 | } | |
1033 | info->cap_arch = CS_ARCH_ARM; | |
1034 | info->cap_mode = cap_mode; | |
48440620 | 1035 | } |
7bcdbf51 RH |
1036 | |
1037 | sctlr_b = arm_sctlr_b(env); | |
1038 | if (bswap_code(sctlr_b)) { | |
ee3eb3a7 | 1039 | #if TARGET_BIG_ENDIAN |
48440620 PC |
1040 | info->endian = BFD_ENDIAN_LITTLE; |
1041 | #else | |
1042 | info->endian = BFD_ENDIAN_BIG; | |
1043 | #endif | |
1044 | } | |
f7478a92 | 1045 | info->flags &= ~INSN_ARM_BE32; |
7bcdbf51 RH |
1046 | #ifndef CONFIG_USER_ONLY |
1047 | if (sctlr_b) { | |
f7478a92 JB |
1048 | info->flags |= INSN_ARM_BE32; |
1049 | } | |
7bcdbf51 | 1050 | #endif |
48440620 PC |
1051 | } |
1052 | ||
86480615 PMD |
1053 | #ifdef TARGET_AARCH64 |
1054 | ||
1055 | static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags) | |
1056 | { | |
1057 | ARMCPU *cpu = ARM_CPU(cs); | |
1058 | CPUARMState *env = &cpu->env; | |
1059 | uint32_t psr = pstate_read(env); | |
a9d84070 | 1060 | int i, j; |
86480615 | 1061 | int el = arm_current_el(env); |
bde0e60b | 1062 | uint64_t hcr = arm_hcr_el2_eff(env); |
86480615 | 1063 | const char *ns_status; |
7a867dd5 | 1064 | bool sve; |
86480615 PMD |
1065 | |
1066 | qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc); | |
1067 | for (i = 0; i < 32; i++) { | |
1068 | if (i == 31) { | |
1069 | qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]); | |
1070 | } else { | |
1071 | qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i], | |
1072 | (i + 2) % 3 ? " " : "\n"); | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) { | |
1077 | ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S "; | |
1078 | } else { | |
1079 | ns_status = ""; | |
1080 | } | |
1081 | qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c", | |
1082 | psr, | |
1083 | psr & PSTATE_N ? 'N' : '-', | |
1084 | psr & PSTATE_Z ? 'Z' : '-', | |
1085 | psr & PSTATE_C ? 'C' : '-', | |
1086 | psr & PSTATE_V ? 'V' : '-', | |
1087 | ns_status, | |
1088 | el, | |
1089 | psr & PSTATE_SP ? 'h' : 't'); | |
1090 | ||
7a867dd5 RH |
1091 | if (cpu_isar_feature(aa64_sme, cpu)) { |
1092 | qemu_fprintf(f, " SVCR=%08" PRIx64 " %c%c", | |
1093 | env->svcr, | |
1094 | (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'), | |
1095 | (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-')); | |
1096 | } | |
86480615 PMD |
1097 | if (cpu_isar_feature(aa64_bti, cpu)) { |
1098 | qemu_fprintf(f, " BTYPE=%d", (psr & PSTATE_BTYPE) >> 10); | |
1099 | } | |
bde0e60b PM |
1100 | qemu_fprintf(f, "%s%s%s", |
1101 | (hcr & HCR_NV) ? " NV" : "", | |
1102 | (hcr & HCR_NV1) ? " NV1" : "", | |
1103 | (hcr & HCR_NV2) ? " NV2" : ""); | |
86480615 PMD |
1104 | if (!(flags & CPU_DUMP_FPU)) { |
1105 | qemu_fprintf(f, "\n"); | |
1106 | return; | |
1107 | } | |
1108 | if (fp_exception_el(env, el) != 0) { | |
1109 | qemu_fprintf(f, " FPU disabled\n"); | |
1110 | return; | |
1111 | } | |
1112 | qemu_fprintf(f, " FPCR=%08x FPSR=%08x\n", | |
1113 | vfp_get_fpcr(env), vfp_get_fpsr(env)); | |
1114 | ||
7a867dd5 RH |
1115 | if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) { |
1116 | sve = sme_exception_el(env, el) == 0; | |
1117 | } else if (cpu_isar_feature(aa64_sve, cpu)) { | |
1118 | sve = sve_exception_el(env, el) == 0; | |
1119 | } else { | |
1120 | sve = false; | |
1121 | } | |
1122 | ||
1123 | if (sve) { | |
a9d84070 | 1124 | int zcr_len = sve_vqm1_for_el(env, el); |
86480615 PMD |
1125 | |
1126 | for (i = 0; i <= FFR_PRED_NUM; i++) { | |
1127 | bool eol; | |
1128 | if (i == FFR_PRED_NUM) { | |
1129 | qemu_fprintf(f, "FFR="); | |
1130 | /* It's last, so end the line. */ | |
1131 | eol = true; | |
1132 | } else { | |
1133 | qemu_fprintf(f, "P%02d=", i); | |
1134 | switch (zcr_len) { | |
1135 | case 0: | |
1136 | eol = i % 8 == 7; | |
1137 | break; | |
1138 | case 1: | |
1139 | eol = i % 6 == 5; | |
1140 | break; | |
1141 | case 2: | |
1142 | case 3: | |
1143 | eol = i % 3 == 2; | |
1144 | break; | |
1145 | default: | |
1146 | /* More than one quadword per predicate. */ | |
1147 | eol = true; | |
1148 | break; | |
1149 | } | |
1150 | } | |
1151 | for (j = zcr_len / 4; j >= 0; j--) { | |
1152 | int digits; | |
1153 | if (j * 4 + 4 <= zcr_len + 1) { | |
1154 | digits = 16; | |
1155 | } else { | |
1156 | digits = (zcr_len % 4 + 1) * 4; | |
1157 | } | |
1158 | qemu_fprintf(f, "%0*" PRIx64 "%s", digits, | |
1159 | env->vfp.pregs[i].p[j], | |
1160 | j ? ":" : eol ? "\n" : " "); | |
1161 | } | |
1162 | } | |
1163 | ||
a9d84070 RH |
1164 | if (zcr_len == 0) { |
1165 | /* | |
1166 | * With vl=16, there are only 37 columns per register, | |
1167 | * so output two registers per line. | |
1168 | */ | |
1169 | for (i = 0; i < 32; i++) { | |
86480615 PMD |
1170 | qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s", |
1171 | i, env->vfp.zregs[i].d[1], | |
1172 | env->vfp.zregs[i].d[0], i & 1 ? "\n" : " "); | |
a9d84070 RH |
1173 | } |
1174 | } else { | |
1175 | for (i = 0; i < 32; i++) { | |
1176 | qemu_fprintf(f, "Z%02d=", i); | |
86480615 | 1177 | for (j = zcr_len; j >= 0; j--) { |
86480615 PMD |
1178 | qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s", |
1179 | env->vfp.zregs[i].d[j * 2 + 1], | |
a9d84070 RH |
1180 | env->vfp.zregs[i].d[j * 2 + 0], |
1181 | j ? ":" : "\n"); | |
86480615 PMD |
1182 | } |
1183 | } | |
1184 | } | |
1185 | } else { | |
1186 | for (i = 0; i < 32; i++) { | |
1187 | uint64_t *q = aa64_vfp_qreg(env, i); | |
1188 | qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s", | |
1189 | i, q[1], q[0], (i & 1 ? "\n" : " ")); | |
1190 | } | |
1191 | } | |
270bea47 RH |
1192 | |
1193 | if (cpu_isar_feature(aa64_sme, cpu) && | |
1194 | FIELD_EX64(env->svcr, SVCR, ZA) && | |
1195 | sme_exception_el(env, el) == 0) { | |
1196 | int zcr_len = sve_vqm1_for_el_sm(env, el, true); | |
1197 | int svl = (zcr_len + 1) * 16; | |
1198 | int svl_lg10 = svl < 100 ? 2 : 3; | |
1199 | ||
1200 | for (i = 0; i < svl; i++) { | |
1201 | qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i); | |
1202 | for (j = zcr_len; j >= 0; --j) { | |
1203 | qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c", | |
1204 | env->zarray[i].d[2 * j + 1], | |
1205 | env->zarray[i].d[2 * j], | |
1206 | j ? ':' : '\n'); | |
1207 | } | |
1208 | } | |
1209 | } | |
86480615 PMD |
1210 | } |
1211 | ||
1212 | #else | |
1213 | ||
1214 | static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags) | |
1215 | { | |
1216 | g_assert_not_reached(); | |
1217 | } | |
1218 | ||
1219 | #endif | |
1220 | ||
1221 | static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags) | |
1222 | { | |
1223 | ARMCPU *cpu = ARM_CPU(cs); | |
1224 | CPUARMState *env = &cpu->env; | |
1225 | int i; | |
1226 | ||
1227 | if (is_a64(env)) { | |
1228 | aarch64_cpu_dump_state(cs, f, flags); | |
1229 | return; | |
1230 | } | |
1231 | ||
1232 | for (i = 0; i < 16; i++) { | |
1233 | qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]); | |
1234 | if ((i % 4) == 3) { | |
1235 | qemu_fprintf(f, "\n"); | |
1236 | } else { | |
1237 | qemu_fprintf(f, " "); | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | if (arm_feature(env, ARM_FEATURE_M)) { | |
1242 | uint32_t xpsr = xpsr_read(env); | |
1243 | const char *mode; | |
1244 | const char *ns_status = ""; | |
1245 | ||
1246 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
1247 | ns_status = env->v7m.secure ? "S " : "NS "; | |
1248 | } | |
1249 | ||
1250 | if (xpsr & XPSR_EXCP) { | |
1251 | mode = "handler"; | |
1252 | } else { | |
1253 | if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) { | |
1254 | mode = "unpriv-thread"; | |
1255 | } else { | |
1256 | mode = "priv-thread"; | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n", | |
1261 | xpsr, | |
1262 | xpsr & XPSR_N ? 'N' : '-', | |
1263 | xpsr & XPSR_Z ? 'Z' : '-', | |
1264 | xpsr & XPSR_C ? 'C' : '-', | |
1265 | xpsr & XPSR_V ? 'V' : '-', | |
1266 | xpsr & XPSR_T ? 'T' : 'A', | |
1267 | ns_status, | |
1268 | mode); | |
1269 | } else { | |
1270 | uint32_t psr = cpsr_read(env); | |
1271 | const char *ns_status = ""; | |
1272 | ||
1273 | if (arm_feature(env, ARM_FEATURE_EL3) && | |
1274 | (psr & CPSR_M) != ARM_CPU_MODE_MON) { | |
1275 | ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S "; | |
1276 | } | |
1277 | ||
1278 | qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n", | |
1279 | psr, | |
1280 | psr & CPSR_N ? 'N' : '-', | |
1281 | psr & CPSR_Z ? 'Z' : '-', | |
1282 | psr & CPSR_C ? 'C' : '-', | |
1283 | psr & CPSR_V ? 'V' : '-', | |
1284 | psr & CPSR_T ? 'T' : 'A', | |
1285 | ns_status, | |
1286 | aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26); | |
1287 | } | |
1288 | ||
1289 | if (flags & CPU_DUMP_FPU) { | |
1290 | int numvfpregs = 0; | |
a6627f5f RH |
1291 | if (cpu_isar_feature(aa32_simd_r32, cpu)) { |
1292 | numvfpregs = 32; | |
7fbc6a40 | 1293 | } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) { |
a6627f5f | 1294 | numvfpregs = 16; |
86480615 PMD |
1295 | } |
1296 | for (i = 0; i < numvfpregs; i++) { | |
1297 | uint64_t v = *aa32_vfp_dreg(env, i); | |
1298 | qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n", | |
1299 | i * 2, (uint32_t)v, | |
1300 | i * 2 + 1, (uint32_t)(v >> 32), | |
1301 | i, v); | |
1302 | } | |
1303 | qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env)); | |
aa291908 PM |
1304 | if (cpu_isar_feature(aa32_mve, cpu)) { |
1305 | qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr); | |
1306 | } | |
86480615 PMD |
1307 | } |
1308 | } | |
1309 | ||
46de5913 IM |
1310 | uint64_t arm_cpu_mp_affinity(int idx, uint8_t clustersz) |
1311 | { | |
1312 | uint32_t Aff1 = idx / clustersz; | |
1313 | uint32_t Aff0 = idx % clustersz; | |
1314 | return (Aff1 << ARM_AFF1_SHIFT) | Aff0; | |
1315 | } | |
1316 | ||
777dc784 PM |
1317 | static void arm_cpu_initfn(Object *obj) |
1318 | { | |
1319 | ARMCPU *cpu = ARM_CPU(obj); | |
1320 | ||
5860362d | 1321 | cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
c27f5d3a | 1322 | NULL, g_free); |
79614b78 | 1323 | |
b5c53d1b | 1324 | QLIST_INIT(&cpu->pre_el_change_hooks); |
08267487 AL |
1325 | QLIST_INIT(&cpu->el_change_hooks); |
1326 | ||
b3d52804 RH |
1327 | #ifdef CONFIG_USER_ONLY |
1328 | # ifdef TARGET_AARCH64 | |
1329 | /* | |
e74c0976 RH |
1330 | * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME. |
1331 | * These values were chosen to fit within the default signal frame. | |
1332 | * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length, | |
1333 | * and our corresponding cpu property. | |
b3d52804 RH |
1334 | */ |
1335 | cpu->sve_default_vq = 4; | |
e74c0976 | 1336 | cpu->sme_default_vq = 2; |
b3d52804 RH |
1337 | # endif |
1338 | #else | |
7c1840b6 PM |
1339 | /* Our inbound IRQ and FIQ lines */ |
1340 | if (kvm_enabled()) { | |
136e67e9 EI |
1341 | /* VIRQ and VFIQ are unused with KVM but we add them to maintain |
1342 | * the same interface as non-KVM CPUs. | |
1343 | */ | |
1344 | qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4); | |
7c1840b6 | 1345 | } else { |
136e67e9 | 1346 | qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4); |
7c1840b6 | 1347 | } |
55d284af | 1348 | |
55d284af PM |
1349 | qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs, |
1350 | ARRAY_SIZE(cpu->gt_timer_outputs)); | |
aa1b3111 PM |
1351 | |
1352 | qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt, | |
1353 | "gicv3-maintenance-interrupt", 1); | |
07f48730 AJ |
1354 | qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt, |
1355 | "pmu-interrupt", 1); | |
7c1840b6 PM |
1356 | #endif |
1357 | ||
54d3e3f5 PM |
1358 | /* DTB consumers generally don't in fact care what the 'compatible' |
1359 | * string is, so always provide some string and trust that a hypothetical | |
1360 | * picky DTB consumer will also provide a helpful error message. | |
1361 | */ | |
1362 | cpu->dtb_compatible = "qemu,unknown"; | |
0dc71c70 | 1363 | cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */ |
3541addc | 1364 | cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; |
54d3e3f5 | 1365 | |
2c9c0bf9 | 1366 | if (tcg_enabled() || hvf_enabled()) { |
0dc71c70 AO |
1367 | /* TCG and HVF implement PSCI 1.1 */ |
1368 | cpu->psci_version = QEMU_PSCI_VERSION_1_1; | |
79614b78 | 1369 | } |
4b6a83fb PM |
1370 | } |
1371 | ||
96eec6b2 AJ |
1372 | static Property arm_cpu_gt_cntfrq_property = |
1373 | DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, | |
1374 | NANOSECONDS_PER_SECOND / GTIMER_SCALE); | |
1375 | ||
07a5b0d2 | 1376 | static Property arm_cpu_reset_cbar_property = |
f318cec6 | 1377 | DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0); |
07a5b0d2 | 1378 | |
68e0a40a AP |
1379 | static Property arm_cpu_reset_hivecs_property = |
1380 | DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false); | |
1381 | ||
45ca3a14 | 1382 | #ifndef CONFIG_USER_ONLY |
c25bd18a PM |
1383 | static Property arm_cpu_has_el2_property = |
1384 | DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true); | |
1385 | ||
51942aee GB |
1386 | static Property arm_cpu_has_el3_property = |
1387 | DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true); | |
45ca3a14 | 1388 | #endif |
51942aee | 1389 | |
3a062d57 JB |
1390 | static Property arm_cpu_cfgend_property = |
1391 | DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false); | |
1392 | ||
97a28b0e PM |
1393 | static Property arm_cpu_has_vfp_property = |
1394 | DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true); | |
1395 | ||
42bea956 CLG |
1396 | static Property arm_cpu_has_vfp_d32_property = |
1397 | DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true); | |
1398 | ||
97a28b0e PM |
1399 | static Property arm_cpu_has_neon_property = |
1400 | DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true); | |
1401 | ||
ea90db0a PM |
1402 | static Property arm_cpu_has_dsp_property = |
1403 | DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true); | |
1404 | ||
8f325f56 PC |
1405 | static Property arm_cpu_has_mpu_property = |
1406 | DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true); | |
1407 | ||
8d92e26b PM |
1408 | /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value, |
1409 | * because the CPU initfn will have already set cpu->pmsav7_dregion to | |
1410 | * the right value for that particular CPU type, and we don't want | |
1411 | * to override that with an incorrect constant value. | |
1412 | */ | |
3281af81 | 1413 | static Property arm_cpu_pmsav7_dregion_property = |
8d92e26b PM |
1414 | DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU, |
1415 | pmsav7_dregion, | |
1416 | qdev_prop_uint32, uint32_t); | |
3281af81 | 1417 | |
ae502508 AJ |
1418 | static bool arm_get_pmu(Object *obj, Error **errp) |
1419 | { | |
1420 | ARMCPU *cpu = ARM_CPU(obj); | |
1421 | ||
1422 | return cpu->has_pmu; | |
1423 | } | |
1424 | ||
1425 | static void arm_set_pmu(Object *obj, bool value, Error **errp) | |
1426 | { | |
1427 | ARMCPU *cpu = ARM_CPU(obj); | |
1428 | ||
1429 | if (value) { | |
7d20e681 | 1430 | if (kvm_enabled() && !kvm_arm_pmu_supported()) { |
ae502508 AJ |
1431 | error_setg(errp, "'pmu' feature not supported by KVM on this host"); |
1432 | return; | |
1433 | } | |
1434 | set_feature(&cpu->env, ARM_FEATURE_PMU); | |
1435 | } else { | |
1436 | unset_feature(&cpu->env, ARM_FEATURE_PMU); | |
1437 | } | |
1438 | cpu->has_pmu = value; | |
1439 | } | |
1440 | ||
7def8754 AJ |
1441 | unsigned int gt_cntfrq_period_ns(ARMCPU *cpu) |
1442 | { | |
96eec6b2 AJ |
1443 | /* |
1444 | * The exact approach to calculating guest ticks is: | |
1445 | * | |
1446 | * muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz, | |
1447 | * NANOSECONDS_PER_SECOND); | |
1448 | * | |
1449 | * We don't do that. Rather we intentionally use integer division | |
1450 | * truncation below and in the caller for the conversion of host monotonic | |
1451 | * time to guest ticks to provide the exact inverse for the semantics of | |
1452 | * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so | |
1453 | * it loses precision when representing frequencies where | |
1454 | * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to | |
1455 | * provide an exact inverse leads to scheduling timers with negative | |
1456 | * periods, which in turn leads to sticky behaviour in the guest. | |
1457 | * | |
1458 | * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor | |
1459 | * cannot become zero. | |
1460 | */ | |
7def8754 AJ |
1461 | return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ? |
1462 | NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1; | |
1463 | } | |
1464 | ||
b8f7959f PM |
1465 | static void arm_cpu_propagate_feature_implications(ARMCPU *cpu) |
1466 | { | |
1467 | CPUARMState *env = &cpu->env; | |
1468 | bool no_aa32 = false; | |
1469 | ||
1470 | /* | |
1471 | * Some features automatically imply others: set the feature | |
1472 | * bits explicitly for these cases. | |
1473 | */ | |
1474 | ||
1475 | if (arm_feature(env, ARM_FEATURE_M)) { | |
1476 | set_feature(env, ARM_FEATURE_PMSA); | |
1477 | } | |
1478 | ||
1479 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
1480 | if (arm_feature(env, ARM_FEATURE_M)) { | |
1481 | set_feature(env, ARM_FEATURE_V7); | |
1482 | } else { | |
1483 | set_feature(env, ARM_FEATURE_V7VE); | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | /* | |
1488 | * There exist AArch64 cpus without AArch32 support. When KVM | |
1489 | * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN. | |
1490 | * Similarly, we cannot check ID_AA64PFR0 without AArch64 support. | |
1491 | * As a general principle, we also do not make ID register | |
1492 | * consistency checks anywhere unless using TCG, because only | |
1493 | * for TCG would a consistency-check failure be a QEMU bug. | |
1494 | */ | |
1495 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { | |
1496 | no_aa32 = !cpu_isar_feature(aa64_aa32, cpu); | |
1497 | } | |
1498 | ||
1499 | if (arm_feature(env, ARM_FEATURE_V7VE)) { | |
1500 | /* | |
1501 | * v7 Virtualization Extensions. In real hardware this implies | |
1502 | * EL2 and also the presence of the Security Extensions. | |
1503 | * For QEMU, for backwards-compatibility we implement some | |
1504 | * CPUs or CPU configs which have no actual EL2 or EL3 but do | |
1505 | * include the various other features that V7VE implies. | |
1506 | * Presence of EL2 itself is ARM_FEATURE_EL2, and of the | |
1507 | * Security Extensions is ARM_FEATURE_EL3. | |
1508 | */ | |
1509 | assert(!tcg_enabled() || no_aa32 || | |
1510 | cpu_isar_feature(aa32_arm_div, cpu)); | |
1511 | set_feature(env, ARM_FEATURE_LPAE); | |
1512 | set_feature(env, ARM_FEATURE_V7); | |
1513 | } | |
1514 | if (arm_feature(env, ARM_FEATURE_V7)) { | |
1515 | set_feature(env, ARM_FEATURE_VAPA); | |
1516 | set_feature(env, ARM_FEATURE_THUMB2); | |
1517 | set_feature(env, ARM_FEATURE_MPIDR); | |
1518 | if (!arm_feature(env, ARM_FEATURE_M)) { | |
1519 | set_feature(env, ARM_FEATURE_V6K); | |
1520 | } else { | |
1521 | set_feature(env, ARM_FEATURE_V6); | |
1522 | } | |
1523 | ||
1524 | /* | |
1525 | * Always define VBAR for V7 CPUs even if it doesn't exist in | |
1526 | * non-EL3 configs. This is needed by some legacy boards. | |
1527 | */ | |
1528 | set_feature(env, ARM_FEATURE_VBAR); | |
1529 | } | |
1530 | if (arm_feature(env, ARM_FEATURE_V6K)) { | |
1531 | set_feature(env, ARM_FEATURE_V6); | |
1532 | set_feature(env, ARM_FEATURE_MVFR); | |
1533 | } | |
1534 | if (arm_feature(env, ARM_FEATURE_V6)) { | |
1535 | set_feature(env, ARM_FEATURE_V5); | |
1536 | if (!arm_feature(env, ARM_FEATURE_M)) { | |
1537 | assert(!tcg_enabled() || no_aa32 || | |
1538 | cpu_isar_feature(aa32_jazelle, cpu)); | |
1539 | set_feature(env, ARM_FEATURE_AUXCR); | |
1540 | } | |
1541 | } | |
1542 | if (arm_feature(env, ARM_FEATURE_V5)) { | |
1543 | set_feature(env, ARM_FEATURE_V4T); | |
1544 | } | |
1545 | if (arm_feature(env, ARM_FEATURE_LPAE)) { | |
1546 | set_feature(env, ARM_FEATURE_V7MP); | |
1547 | } | |
1548 | if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { | |
1549 | set_feature(env, ARM_FEATURE_CBAR); | |
1550 | } | |
1551 | if (arm_feature(env, ARM_FEATURE_THUMB2) && | |
1552 | !arm_feature(env, ARM_FEATURE_M)) { | |
1553 | set_feature(env, ARM_FEATURE_THUMB_DSP); | |
1554 | } | |
1555 | } | |
1556 | ||
51e5ef45 | 1557 | void arm_cpu_post_init(Object *obj) |
07a5b0d2 PC |
1558 | { |
1559 | ARMCPU *cpu = ARM_CPU(obj); | |
07a5b0d2 | 1560 | |
b8f7959f PM |
1561 | /* |
1562 | * Some features imply others. Figure this out now, because we | |
1563 | * are going to look at the feature bits in deciding which | |
1564 | * properties to add. | |
790a1150 | 1565 | */ |
b8f7959f | 1566 | arm_cpu_propagate_feature_implications(cpu); |
790a1150 | 1567 | |
f318cec6 PM |
1568 | if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) || |
1569 | arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) { | |
94d912d1 | 1570 | qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property); |
07a5b0d2 | 1571 | } |
68e0a40a AP |
1572 | |
1573 | if (!arm_feature(&cpu->env, ARM_FEATURE_M)) { | |
94d912d1 | 1574 | qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property); |
68e0a40a | 1575 | } |
3933443e | 1576 | |
910e4f24 | 1577 | if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { |
4a7319b7 EI |
1578 | object_property_add_uint64_ptr(obj, "rvbar", |
1579 | &cpu->rvbar_prop, | |
1580 | OBJ_PROP_FLAG_READWRITE); | |
3933443e | 1581 | } |
51942aee | 1582 | |
45ca3a14 | 1583 | #ifndef CONFIG_USER_ONLY |
51942aee GB |
1584 | if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { |
1585 | /* Add the has_el3 state CPU property only if EL3 is allowed. This will | |
1586 | * prevent "has_el3" from existing on CPUs which cannot support EL3. | |
1587 | */ | |
94d912d1 | 1588 | qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property); |
9e273ef2 | 1589 | |
9e273ef2 PM |
1590 | object_property_add_link(obj, "secure-memory", |
1591 | TYPE_MEMORY_REGION, | |
1592 | (Object **)&cpu->secure_memory, | |
1593 | qdev_prop_allow_set_link_before_realize, | |
d2623129 | 1594 | OBJ_PROP_LINK_STRONG); |
51942aee | 1595 | } |
8f325f56 | 1596 | |
c25bd18a | 1597 | if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) { |
94d912d1 | 1598 | qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property); |
c25bd18a | 1599 | } |
45ca3a14 | 1600 | #endif |
c25bd18a | 1601 | |
929e754d | 1602 | if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) { |
ae502508 | 1603 | cpu->has_pmu = true; |
d2623129 | 1604 | object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu); |
929e754d WH |
1605 | } |
1606 | ||
97a28b0e PM |
1607 | /* |
1608 | * Allow user to turn off VFP and Neon support, but only for TCG -- | |
1609 | * KVM does not currently allow us to lie to the guest about its | |
1610 | * ID/feature registers, so the guest always sees what the host has. | |
1611 | */ | |
4315f7c6 RH |
1612 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { |
1613 | if (cpu_isar_feature(aa64_fp_simd, cpu)) { | |
1614 | cpu->has_vfp = true; | |
1615 | cpu->has_vfp_d32 = true; | |
1616 | if (tcg_enabled() || qtest_enabled()) { | |
1617 | qdev_property_add_static(DEVICE(obj), | |
1618 | &arm_cpu_has_vfp_property); | |
1619 | } | |
97a28b0e | 1620 | } |
4315f7c6 RH |
1621 | } else if (cpu_isar_feature(aa32_vfp, cpu)) { |
1622 | cpu->has_vfp = true; | |
1623 | if (cpu_isar_feature(aa32_simd_r32, cpu)) { | |
1624 | cpu->has_vfp_d32 = true; | |
42bea956 CLG |
1625 | /* |
1626 | * The permitted values of the SIMDReg bits [3:0] on | |
1627 | * Armv8-A are either 0b0000 and 0b0010. On such CPUs, | |
1628 | * make sure that has_vfp_d32 can not be set to false. | |
1629 | */ | |
4315f7c6 RH |
1630 | if ((tcg_enabled() || qtest_enabled()) |
1631 | && !(arm_feature(&cpu->env, ARM_FEATURE_V8) | |
1632 | && !arm_feature(&cpu->env, ARM_FEATURE_M))) { | |
42bea956 CLG |
1633 | qdev_property_add_static(DEVICE(obj), |
1634 | &arm_cpu_has_vfp_d32_property); | |
1635 | } | |
1636 | } | |
1637 | } | |
1638 | ||
97a28b0e PM |
1639 | if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) { |
1640 | cpu->has_neon = true; | |
1641 | if (!kvm_enabled()) { | |
94d912d1 | 1642 | qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property); |
97a28b0e PM |
1643 | } |
1644 | } | |
1645 | ||
ea90db0a PM |
1646 | if (arm_feature(&cpu->env, ARM_FEATURE_M) && |
1647 | arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) { | |
94d912d1 | 1648 | qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property); |
ea90db0a PM |
1649 | } |
1650 | ||
452a0955 | 1651 | if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) { |
94d912d1 | 1652 | qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property); |
3281af81 PC |
1653 | if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { |
1654 | qdev_property_add_static(DEVICE(obj), | |
94d912d1 | 1655 | &arm_cpu_pmsav7_dregion_property); |
3281af81 | 1656 | } |
8f325f56 PC |
1657 | } |
1658 | ||
181962fd PM |
1659 | if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) { |
1660 | object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau, | |
1661 | qdev_prop_allow_set_link_before_realize, | |
d2623129 | 1662 | OBJ_PROP_LINK_STRONG); |
f9f62e4c PM |
1663 | /* |
1664 | * M profile: initial value of the Secure VTOR. We can't just use | |
1665 | * a simple DEFINE_PROP_UINT32 for this because we want to permit | |
1666 | * the property to be set after realize. | |
1667 | */ | |
64a7b8de FF |
1668 | object_property_add_uint32_ptr(obj, "init-svtor", |
1669 | &cpu->init_svtor, | |
d2623129 | 1670 | OBJ_PROP_FLAG_READWRITE); |
181962fd | 1671 | } |
7cda2149 PM |
1672 | if (arm_feature(&cpu->env, ARM_FEATURE_M)) { |
1673 | /* | |
1674 | * Initial value of the NS VTOR (for cores without the Security | |
1675 | * extension, this is the only VTOR) | |
1676 | */ | |
1677 | object_property_add_uint32_ptr(obj, "init-nsvtor", | |
1678 | &cpu->init_nsvtor, | |
1679 | OBJ_PROP_FLAG_READWRITE); | |
1680 | } | |
181962fd | 1681 | |
bddd892e PM |
1682 | /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */ |
1683 | object_property_add_uint32_ptr(obj, "psci-conduit", | |
1684 | &cpu->psci_conduit, | |
1685 | OBJ_PROP_FLAG_READWRITE); | |
1686 | ||
94d912d1 | 1687 | qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property); |
96eec6b2 AJ |
1688 | |
1689 | if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) { | |
94d912d1 | 1690 | qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property); |
96eec6b2 | 1691 | } |
9e6f8d8a | 1692 | |
1693 | if (kvm_enabled()) { | |
cac675b5 | 1694 | kvm_arm_add_vcpu_properties(cpu); |
9e6f8d8a | 1695 | } |
8bce44a2 RH |
1696 | |
1697 | #ifndef CONFIG_USER_ONLY | |
1698 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && | |
1699 | cpu_isar_feature(aa64_mte, cpu)) { | |
1700 | object_property_add_link(obj, "tag-memory", | |
1701 | TYPE_MEMORY_REGION, | |
1702 | (Object **)&cpu->tag_memory, | |
1703 | qdev_prop_allow_set_link_before_realize, | |
1704 | OBJ_PROP_LINK_STRONG); | |
1705 | ||
1706 | if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { | |
1707 | object_property_add_link(obj, "secure-tag-memory", | |
1708 | TYPE_MEMORY_REGION, | |
1709 | (Object **)&cpu->secure_tag_memory, | |
1710 | qdev_prop_allow_set_link_before_realize, | |
1711 | OBJ_PROP_LINK_STRONG); | |
1712 | } | |
1713 | } | |
1714 | #endif | |
07a5b0d2 PC |
1715 | } |
1716 | ||
4b6a83fb PM |
1717 | static void arm_cpu_finalizefn(Object *obj) |
1718 | { | |
1719 | ARMCPU *cpu = ARM_CPU(obj); | |
08267487 AL |
1720 | ARMELChangeHook *hook, *next; |
1721 | ||
4b6a83fb | 1722 | g_hash_table_destroy(cpu->cp_regs); |
08267487 | 1723 | |
b5c53d1b AL |
1724 | QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) { |
1725 | QLIST_REMOVE(hook, node); | |
1726 | g_free(hook); | |
1727 | } | |
08267487 AL |
1728 | QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) { |
1729 | QLIST_REMOVE(hook, node); | |
1730 | g_free(hook); | |
1731 | } | |
4e7beb0c AL |
1732 | #ifndef CONFIG_USER_ONLY |
1733 | if (cpu->pmu_timer) { | |
4e7beb0c AL |
1734 | timer_free(cpu->pmu_timer); |
1735 | } | |
1736 | #endif | |
777dc784 PM |
1737 | } |
1738 | ||
0df9142d AJ |
1739 | void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp) |
1740 | { | |
1741 | Error *local_err = NULL; | |
1742 | ||
07301161 | 1743 | #ifdef TARGET_AARCH64 |
0df9142d AJ |
1744 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { |
1745 | arm_cpu_sve_finalize(cpu, &local_err); | |
68970d1e AJ |
1746 | if (local_err != NULL) { |
1747 | error_propagate(errp, local_err); | |
1748 | return; | |
1749 | } | |
eb94284d | 1750 | |
f7767ca3 PM |
1751 | /* |
1752 | * FEAT_SME is not architecturally dependent on FEAT_SVE (unless | |
1753 | * FEAT_SME_FA64 is present). However our implementation currently | |
1754 | * assumes it, so if the user asked for sve=off then turn off SME also. | |
1755 | * (KVM doesn't currently support SME at all.) | |
1756 | */ | |
1757 | if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) { | |
1758 | object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort); | |
1759 | } | |
1760 | ||
e74c0976 RH |
1761 | arm_cpu_sme_finalize(cpu, &local_err); |
1762 | if (local_err != NULL) { | |
1763 | error_propagate(errp, local_err); | |
1764 | return; | |
1765 | } | |
1766 | ||
95ea96e8 MZ |
1767 | arm_cpu_pauth_finalize(cpu, &local_err); |
1768 | if (local_err != NULL) { | |
1769 | error_propagate(errp, local_err); | |
1770 | return; | |
eb94284d | 1771 | } |
69b2265d RH |
1772 | |
1773 | arm_cpu_lpa2_finalize(cpu, &local_err); | |
1774 | if (local_err != NULL) { | |
1775 | error_propagate(errp, local_err); | |
1776 | return; | |
1777 | } | |
68970d1e | 1778 | } |
07301161 | 1779 | #endif |
68970d1e AJ |
1780 | |
1781 | if (kvm_enabled()) { | |
1782 | kvm_arm_steal_time_finalize(cpu, &local_err); | |
0df9142d AJ |
1783 | if (local_err != NULL) { |
1784 | error_propagate(errp, local_err); | |
1785 | return; | |
1786 | } | |
1787 | } | |
1788 | } | |
1789 | ||
14969266 | 1790 | static void arm_cpu_realizefn(DeviceState *dev, Error **errp) |
581be094 | 1791 | { |
14a10fc3 | 1792 | CPUState *cs = CPU(dev); |
14969266 AF |
1793 | ARMCPU *cpu = ARM_CPU(dev); |
1794 | ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev); | |
581be094 | 1795 | CPUARMState *env = &cpu->env; |
e97da98f | 1796 | int pagebits; |
ce5b1bbf LV |
1797 | Error *local_err = NULL; |
1798 | ||
b94b8c60 | 1799 | #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) |
e607ea39 | 1800 | /* Use pc-relative instructions in system-mode */ |
e607ea39 AJ |
1801 | cs->tcg_cflags |= CF_PCREL; |
1802 | #endif | |
1803 | ||
c4487d76 PM |
1804 | /* If we needed to query the host kernel for the CPU features |
1805 | * then it's possible that might have failed in the initfn, but | |
1806 | * this is the first point where we can report it. | |
1807 | */ | |
1808 | if (cpu->host_cpu_probe_failed) { | |
585df85e PM |
1809 | if (!kvm_enabled() && !hvf_enabled()) { |
1810 | error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF"); | |
c4487d76 PM |
1811 | } else { |
1812 | error_setg(errp, "Failed to retrieve host CPU features"); | |
1813 | } | |
1814 | return; | |
1815 | } | |
1816 | ||
95f87565 PM |
1817 | #ifndef CONFIG_USER_ONLY |
1818 | /* The NVIC and M-profile CPU are two halves of a single piece of | |
1819 | * hardware; trying to use one without the other is a command line | |
1820 | * error and will result in segfaults if not caught here. | |
1821 | */ | |
1822 | if (arm_feature(env, ARM_FEATURE_M)) { | |
1823 | if (!env->nvic) { | |
1824 | error_setg(errp, "This board cannot be used with Cortex-M CPUs"); | |
1825 | return; | |
1826 | } | |
1827 | } else { | |
1828 | if (env->nvic) { | |
1829 | error_setg(errp, "This board can only be used with Cortex-M CPUs"); | |
1830 | return; | |
1831 | } | |
1832 | } | |
397cd31f | 1833 | |
045e5064 | 1834 | if (!tcg_enabled() && !qtest_enabled()) { |
49e7f191 | 1835 | /* |
045e5064 AG |
1836 | * We assume that no accelerator except TCG (and the "not really an |
1837 | * accelerator" qtest) can handle these features, because Arm hardware | |
1838 | * virtualization can't virtualize them. | |
1839 | * | |
49e7f191 PM |
1840 | * Catch all the cases which might cause us to create more than one |
1841 | * address space for the CPU (otherwise we will assert() later in | |
1842 | * cpu_address_space_init()). | |
1843 | */ | |
1844 | if (arm_feature(env, ARM_FEATURE_M)) { | |
1845 | error_setg(errp, | |
045e5064 AG |
1846 | "Cannot enable %s when using an M-profile guest CPU", |
1847 | current_accel_name()); | |
49e7f191 PM |
1848 | return; |
1849 | } | |
1850 | if (cpu->has_el3) { | |
1851 | error_setg(errp, | |
045e5064 AG |
1852 | "Cannot enable %s when guest CPU has EL3 enabled", |
1853 | current_accel_name()); | |
49e7f191 PM |
1854 | return; |
1855 | } | |
1856 | if (cpu->tag_memory) { | |
1857 | error_setg(errp, | |
d009607d | 1858 | "Cannot enable %s when guest CPUs has MTE enabled", |
045e5064 | 1859 | current_accel_name()); |
49e7f191 PM |
1860 | return; |
1861 | } | |
1862 | } | |
1863 | ||
96eec6b2 AJ |
1864 | { |
1865 | uint64_t scale; | |
1866 | ||
1867 | if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { | |
1868 | if (!cpu->gt_cntfrq_hz) { | |
1869 | error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz", | |
1870 | cpu->gt_cntfrq_hz); | |
1871 | return; | |
1872 | } | |
1873 | scale = gt_cntfrq_period_ns(cpu); | |
1874 | } else { | |
1875 | scale = GTIMER_SCALE; | |
1876 | } | |
1877 | ||
1878 | cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale, | |
1879 | arm_gt_ptimer_cb, cpu); | |
1880 | cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, | |
1881 | arm_gt_vtimer_cb, cpu); | |
1882 | cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale, | |
1883 | arm_gt_htimer_cb, cpu); | |
1884 | cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale, | |
1885 | arm_gt_stimer_cb, cpu); | |
8c94b071 RH |
1886 | cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, |
1887 | arm_gt_hvtimer_cb, cpu); | |
96eec6b2 | 1888 | } |
95f87565 PM |
1889 | #endif |
1890 | ||
ce5b1bbf LV |
1891 | cpu_exec_realizefn(cs, &local_err); |
1892 | if (local_err != NULL) { | |
1893 | error_propagate(errp, local_err); | |
1894 | return; | |
1895 | } | |
14969266 | 1896 | |
0df9142d AJ |
1897 | arm_cpu_finalize_features(cpu, &local_err); |
1898 | if (local_err != NULL) { | |
1899 | error_propagate(errp, local_err); | |
1900 | return; | |
1901 | } | |
1902 | ||
9719f125 JH |
1903 | #ifdef CONFIG_USER_ONLY |
1904 | /* | |
1905 | * User mode relies on IC IVAU instructions to catch modification of | |
1906 | * dual-mapped code. | |
1907 | * | |
1908 | * Clear CTR_EL0.DIC to ensure that software that honors these flags uses | |
1909 | * IC IVAU even if the emulated processor does not normally require it. | |
1910 | */ | |
1911 | cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0); | |
1912 | #endif | |
1913 | ||
97a28b0e PM |
1914 | if (arm_feature(env, ARM_FEATURE_AARCH64) && |
1915 | cpu->has_vfp != cpu->has_neon) { | |
1916 | /* | |
1917 | * This is an architectural requirement for AArch64; AArch32 is | |
1918 | * more flexible and permits VFP-no-Neon and Neon-no-VFP. | |
1919 | */ | |
1920 | error_setg(errp, | |
1921 | "AArch64 CPUs must have both VFP and Neon or neither"); | |
1922 | return; | |
1923 | } | |
1924 | ||
42bea956 CLG |
1925 | if (cpu->has_vfp_d32 != cpu->has_neon) { |
1926 | error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither"); | |
1927 | return; | |
1928 | } | |
1929 | ||
1930 | if (!cpu->has_vfp_d32) { | |
1931 | uint32_t u; | |
1932 | ||
1933 | u = cpu->isar.mvfr0; | |
1934 | u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */ | |
1935 | cpu->isar.mvfr0 = u; | |
1936 | } | |
1937 | ||
97a28b0e PM |
1938 | if (!cpu->has_vfp) { |
1939 | uint64_t t; | |
1940 | uint32_t u; | |
1941 | ||
97a28b0e PM |
1942 | t = cpu->isar.id_aa64isar1; |
1943 | t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0); | |
1944 | cpu->isar.id_aa64isar1 = t; | |
1945 | ||
1946 | t = cpu->isar.id_aa64pfr0; | |
1947 | t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf); | |
1948 | cpu->isar.id_aa64pfr0 = t; | |
1949 | ||
1950 | u = cpu->isar.id_isar6; | |
1951 | u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0); | |
3c93dfa4 | 1952 | u = FIELD_DP32(u, ID_ISAR6, BF16, 0); |
97a28b0e PM |
1953 | cpu->isar.id_isar6 = u; |
1954 | ||
1955 | u = cpu->isar.mvfr0; | |
1956 | u = FIELD_DP32(u, MVFR0, FPSP, 0); | |
1957 | u = FIELD_DP32(u, MVFR0, FPDP, 0); | |
97a28b0e PM |
1958 | u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0); |
1959 | u = FIELD_DP32(u, MVFR0, FPSQRT, 0); | |
97a28b0e | 1960 | u = FIELD_DP32(u, MVFR0, FPROUND, 0); |
532a3af5 PM |
1961 | if (!arm_feature(env, ARM_FEATURE_M)) { |
1962 | u = FIELD_DP32(u, MVFR0, FPTRAP, 0); | |
1963 | u = FIELD_DP32(u, MVFR0, FPSHVEC, 0); | |
1964 | } | |
97a28b0e PM |
1965 | cpu->isar.mvfr0 = u; |
1966 | ||
1967 | u = cpu->isar.mvfr1; | |
1968 | u = FIELD_DP32(u, MVFR1, FPFTZ, 0); | |
1969 | u = FIELD_DP32(u, MVFR1, FPDNAN, 0); | |
1970 | u = FIELD_DP32(u, MVFR1, FPHP, 0); | |
532a3af5 PM |
1971 | if (arm_feature(env, ARM_FEATURE_M)) { |
1972 | u = FIELD_DP32(u, MVFR1, FP16, 0); | |
1973 | } | |
97a28b0e PM |
1974 | cpu->isar.mvfr1 = u; |
1975 | ||
1976 | u = cpu->isar.mvfr2; | |
1977 | u = FIELD_DP32(u, MVFR2, FPMISC, 0); | |
1978 | cpu->isar.mvfr2 = u; | |
1979 | } | |
1980 | ||
1981 | if (!cpu->has_neon) { | |
1982 | uint64_t t; | |
1983 | uint32_t u; | |
1984 | ||
1985 | unset_feature(env, ARM_FEATURE_NEON); | |
1986 | ||
1987 | t = cpu->isar.id_aa64isar0; | |
eb851c11 DH |
1988 | t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0); |
1989 | t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0); | |
1990 | t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0); | |
1991 | t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0); | |
1992 | t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0); | |
1993 | t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0); | |
97a28b0e PM |
1994 | t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0); |
1995 | cpu->isar.id_aa64isar0 = t; | |
1996 | ||
1997 | t = cpu->isar.id_aa64isar1; | |
1998 | t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0); | |
3c93dfa4 | 1999 | t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0); |
f8680aaa | 2000 | t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0); |
97a28b0e PM |
2001 | cpu->isar.id_aa64isar1 = t; |
2002 | ||
2003 | t = cpu->isar.id_aa64pfr0; | |
2004 | t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf); | |
2005 | cpu->isar.id_aa64pfr0 = t; | |
2006 | ||
2007 | u = cpu->isar.id_isar5; | |
eb851c11 DH |
2008 | u = FIELD_DP32(u, ID_ISAR5, AES, 0); |
2009 | u = FIELD_DP32(u, ID_ISAR5, SHA1, 0); | |
2010 | u = FIELD_DP32(u, ID_ISAR5, SHA2, 0); | |
97a28b0e PM |
2011 | u = FIELD_DP32(u, ID_ISAR5, RDM, 0); |
2012 | u = FIELD_DP32(u, ID_ISAR5, VCMA, 0); | |
2013 | cpu->isar.id_isar5 = u; | |
2014 | ||
2015 | u = cpu->isar.id_isar6; | |
2016 | u = FIELD_DP32(u, ID_ISAR6, DP, 0); | |
2017 | u = FIELD_DP32(u, ID_ISAR6, FHM, 0); | |
3c93dfa4 | 2018 | u = FIELD_DP32(u, ID_ISAR6, BF16, 0); |
f8680aaa | 2019 | u = FIELD_DP32(u, ID_ISAR6, I8MM, 0); |
97a28b0e PM |
2020 | cpu->isar.id_isar6 = u; |
2021 | ||
532a3af5 PM |
2022 | if (!arm_feature(env, ARM_FEATURE_M)) { |
2023 | u = cpu->isar.mvfr1; | |
2024 | u = FIELD_DP32(u, MVFR1, SIMDLS, 0); | |
2025 | u = FIELD_DP32(u, MVFR1, SIMDINT, 0); | |
2026 | u = FIELD_DP32(u, MVFR1, SIMDSP, 0); | |
2027 | u = FIELD_DP32(u, MVFR1, SIMDHP, 0); | |
2028 | cpu->isar.mvfr1 = u; | |
2029 | ||
2030 | u = cpu->isar.mvfr2; | |
2031 | u = FIELD_DP32(u, MVFR2, SIMDMISC, 0); | |
2032 | cpu->isar.mvfr2 = u; | |
2033 | } | |
97a28b0e PM |
2034 | } |
2035 | ||
2036 | if (!cpu->has_neon && !cpu->has_vfp) { | |
2037 | uint64_t t; | |
2038 | uint32_t u; | |
2039 | ||
2040 | t = cpu->isar.id_aa64isar0; | |
2041 | t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0); | |
2042 | cpu->isar.id_aa64isar0 = t; | |
2043 | ||
2044 | t = cpu->isar.id_aa64isar1; | |
2045 | t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0); | |
2046 | cpu->isar.id_aa64isar1 = t; | |
2047 | ||
2048 | u = cpu->isar.mvfr0; | |
2049 | u = FIELD_DP32(u, MVFR0, SIMDREG, 0); | |
2050 | cpu->isar.mvfr0 = u; | |
c52881bb RH |
2051 | |
2052 | /* Despite the name, this field covers both VFP and Neon */ | |
2053 | u = cpu->isar.mvfr1; | |
2054 | u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0); | |
2055 | cpu->isar.mvfr1 = u; | |
97a28b0e PM |
2056 | } |
2057 | ||
ea90db0a PM |
2058 | if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) { |
2059 | uint32_t u; | |
2060 | ||
2061 | unset_feature(env, ARM_FEATURE_THUMB_DSP); | |
2062 | ||
2063 | u = cpu->isar.id_isar1; | |
2064 | u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1); | |
2065 | cpu->isar.id_isar1 = u; | |
2066 | ||
2067 | u = cpu->isar.id_isar2; | |
2068 | u = FIELD_DP32(u, ID_ISAR2, MULTU, 1); | |
2069 | u = FIELD_DP32(u, ID_ISAR2, MULTS, 1); | |
2070 | cpu->isar.id_isar2 = u; | |
2071 | ||
2072 | u = cpu->isar.id_isar3; | |
2073 | u = FIELD_DP32(u, ID_ISAR3, SIMD, 1); | |
2074 | u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0); | |
2075 | cpu->isar.id_isar3 = u; | |
2076 | } | |
2077 | ||
2ceb98c0 | 2078 | |
ea7ac69d PM |
2079 | /* |
2080 | * We rely on no XScale CPU having VFP so we can use the same bits in the | |
2081 | * TB flags field for VECSTRIDE and XSCALE_CPAR. | |
2082 | */ | |
7d63183f RH |
2083 | assert(arm_feature(&cpu->env, ARM_FEATURE_AARCH64) || |
2084 | !cpu_isar_feature(aa32_vfp_simd, cpu) || | |
2085 | !arm_feature(env, ARM_FEATURE_XSCALE)); | |
ea7ac69d | 2086 | |
e97da98f PM |
2087 | if (arm_feature(env, ARM_FEATURE_V7) && |
2088 | !arm_feature(env, ARM_FEATURE_M) && | |
452a0955 | 2089 | !arm_feature(env, ARM_FEATURE_PMSA)) { |
e97da98f PM |
2090 | /* v7VMSA drops support for the old ARMv5 tiny pages, so we |
2091 | * can use 4K pages. | |
2092 | */ | |
2093 | pagebits = 12; | |
2094 | } else { | |
2095 | /* For CPUs which might have tiny 1K pages, or which have an | |
2096 | * MPU and might have small region sizes, stick with 1K pages. | |
2097 | */ | |
2098 | pagebits = 10; | |
2099 | } | |
2100 | if (!set_preferred_target_page_bits(pagebits)) { | |
2101 | /* This can only ever happen for hotplugging a CPU, or if | |
2102 | * the board code incorrectly creates a CPU which it has | |
2103 | * promised via minimum_page_size that it will not. | |
2104 | */ | |
2105 | error_setg(errp, "This CPU requires a smaller page size than the " | |
2106 | "system is using"); | |
2107 | return; | |
2108 | } | |
2109 | ||
ce5b1bbf LV |
2110 | /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it. |
2111 | * We don't support setting cluster ID ([16..23]) (known as Aff2 | |
2112 | * in later ARM ARM versions), or any of the higher affinity level fields, | |
2113 | * so these bits always RAZ. | |
2114 | */ | |
2115 | if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) { | |
46de5913 IM |
2116 | cpu->mp_affinity = arm_cpu_mp_affinity(cs->cpu_index, |
2117 | ARM_DEFAULT_CPUS_PER_CLUSTER); | |
ce5b1bbf LV |
2118 | } |
2119 | ||
68e0a40a AP |
2120 | if (cpu->reset_hivecs) { |
2121 | cpu->reset_sctlr |= (1 << 13); | |
2122 | } | |
2123 | ||
3a062d57 JB |
2124 | if (cpu->cfgend) { |
2125 | if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { | |
2126 | cpu->reset_sctlr |= SCTLR_EE; | |
2127 | } else { | |
2128 | cpu->reset_sctlr |= SCTLR_B; | |
2129 | } | |
2130 | } | |
2131 | ||
40188188 | 2132 | if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) { |
51942aee GB |
2133 | /* If the has_el3 CPU property is disabled then we need to disable the |
2134 | * feature. | |
2135 | */ | |
2136 | unset_feature(env, ARM_FEATURE_EL3); | |
2137 | ||
b13c91c0 RH |
2138 | /* |
2139 | * Disable the security extension feature bits in the processor | |
2140 | * feature registers as well. | |
51942aee | 2141 | */ |
b13c91c0 | 2142 | cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0); |
033a4f15 | 2143 | cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0); |
b13c91c0 RH |
2144 | cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, |
2145 | ID_AA64PFR0, EL3, 0); | |
b9f335c2 RH |
2146 | |
2147 | /* Disable the realm management extension, which requires EL3. */ | |
2148 | cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, | |
2149 | ID_AA64PFR0, RME, 0); | |
51942aee GB |
2150 | } |
2151 | ||
c25bd18a PM |
2152 | if (!cpu->has_el2) { |
2153 | unset_feature(env, ARM_FEATURE_EL2); | |
2154 | } | |
2155 | ||
d6f02ce3 | 2156 | if (!cpu->has_pmu) { |
929e754d | 2157 | unset_feature(env, ARM_FEATURE_PMU); |
57a4a11b AL |
2158 | } |
2159 | if (arm_feature(env, ARM_FEATURE_PMU)) { | |
bf8d0969 | 2160 | pmu_init(cpu); |
57a4a11b AL |
2161 | |
2162 | if (!kvm_enabled()) { | |
2163 | arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0); | |
2164 | arm_register_el_change_hook(cpu, &pmu_post_el_change, 0); | |
2165 | } | |
4e7beb0c AL |
2166 | |
2167 | #ifndef CONFIG_USER_ONLY | |
2168 | cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb, | |
2169 | cpu); | |
2170 | #endif | |
57a4a11b | 2171 | } else { |
2a609df8 PM |
2172 | cpu->isar.id_aa64dfr0 = |
2173 | FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0); | |
a6179538 | 2174 | cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0); |
57a4a11b AL |
2175 | cpu->pmceid0 = 0; |
2176 | cpu->pmceid1 = 0; | |
929e754d WH |
2177 | } |
2178 | ||
3c2f7bb3 | 2179 | if (!arm_feature(env, ARM_FEATURE_EL2)) { |
b13c91c0 RH |
2180 | /* |
2181 | * Disable the hypervisor feature bits in the processor feature | |
2182 | * registers if we don't have EL2. | |
3c2f7bb3 | 2183 | */ |
b13c91c0 RH |
2184 | cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, |
2185 | ID_AA64PFR0, EL2, 0); | |
2186 | cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, | |
2187 | ID_PFR1, VIRTUALIZATION, 0); | |
3c2f7bb3 PM |
2188 | } |
2189 | ||
7134cb07 RH |
2190 | if (cpu_isar_feature(aa64_mte, cpu)) { |
2191 | /* | |
2192 | * The architectural range of GM blocksize is 2-6, however qemu | |
2193 | * doesn't support blocksize of 2 (see HELPER(ldgm)). | |
2194 | */ | |
2195 | if (tcg_enabled()) { | |
2196 | assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6); | |
2197 | } | |
2198 | ||
6f4e1405 | 2199 | #ifndef CONFIG_USER_ONLY |
6f4e1405 | 2200 | /* |
cd305b5f RH |
2201 | * If we do not have tag-memory provided by the machine, |
2202 | * reduce MTE support to instructions enabled at EL0. | |
2203 | * This matches Cortex-A710 BROADCASTMTE input being LOW. | |
6f4e1405 | 2204 | */ |
7134cb07 RH |
2205 | if (cpu->tag_memory == NULL) { |
2206 | cpu->isar.id_aa64pfr1 = | |
cd305b5f | 2207 | FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1); |
7134cb07 | 2208 | } |
6f4e1405 | 2209 | #endif |
7134cb07 | 2210 | } |
6f4e1405 | 2211 | |
2daf518d PM |
2212 | if (tcg_enabled()) { |
2213 | /* | |
7d8c283e PM |
2214 | * Don't report some architectural features in the ID registers |
2215 | * where TCG does not yet implement it (not even a minimal | |
2216 | * stub version). This avoids guests falling over when they | |
2217 | * try to access the non-existent system registers for them. | |
2daf518d | 2218 | */ |
7d8c283e | 2219 | /* FEAT_SPE (Statistical Profiling Extension) */ |
2daf518d PM |
2220 | cpu->isar.id_aa64dfr0 = |
2221 | FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0); | |
3d5f45ec RH |
2222 | /* FEAT_TRBE (Trace Buffer Extension) */ |
2223 | cpu->isar.id_aa64dfr0 = | |
2224 | FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0); | |
7d8c283e PM |
2225 | /* FEAT_TRF (Self-hosted Trace Extension) */ |
2226 | cpu->isar.id_aa64dfr0 = | |
2227 | FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0); | |
2228 | cpu->isar.id_dfr0 = | |
2229 | FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0); | |
2230 | /* Trace Macrocell system register access */ | |
2231 | cpu->isar.id_aa64dfr0 = | |
2232 | FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0); | |
2233 | cpu->isar.id_dfr0 = | |
2234 | FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0); | |
2235 | /* Memory mapped trace */ | |
2236 | cpu->isar.id_dfr0 = | |
2237 | FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0); | |
2238 | /* FEAT_AMU (Activity Monitors Extension) */ | |
2239 | cpu->isar.id_aa64pfr0 = | |
2240 | FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0); | |
2241 | cpu->isar.id_pfr0 = | |
2242 | FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0); | |
2243 | /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */ | |
2244 | cpu->isar.id_aa64pfr0 = | |
2245 | FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0); | |
2daf518d PM |
2246 | } |
2247 | ||
f50cd314 PM |
2248 | /* MPU can be configured out of a PMSA CPU either by setting has-mpu |
2249 | * to false or by setting pmsav7-dregion to 0. | |
2250 | */ | |
761c4642 | 2251 | if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) { |
f50cd314 | 2252 | cpu->has_mpu = false; |
761c4642 TR |
2253 | cpu->pmsav7_dregion = 0; |
2254 | cpu->pmsav8r_hdregion = 0; | |
8f325f56 PC |
2255 | } |
2256 | ||
452a0955 | 2257 | if (arm_feature(env, ARM_FEATURE_PMSA) && |
3281af81 PC |
2258 | arm_feature(env, ARM_FEATURE_V7)) { |
2259 | uint32_t nr = cpu->pmsav7_dregion; | |
2260 | ||
2261 | if (nr > 0xff) { | |
9af9e0fe | 2262 | error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr); |
3281af81 PC |
2263 | return; |
2264 | } | |
6cb0b013 PC |
2265 | |
2266 | if (nr) { | |
0e1a46bb PM |
2267 | if (arm_feature(env, ARM_FEATURE_V8)) { |
2268 | /* PMSAv8 */ | |
62c58ee0 PM |
2269 | env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr); |
2270 | env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr); | |
2271 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { | |
2272 | env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr); | |
2273 | env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr); | |
2274 | } | |
0e1a46bb PM |
2275 | } else { |
2276 | env->pmsav7.drbar = g_new0(uint32_t, nr); | |
2277 | env->pmsav7.drsr = g_new0(uint32_t, nr); | |
2278 | env->pmsav7.dracr = g_new0(uint32_t, nr); | |
2279 | } | |
6cb0b013 | 2280 | } |
761c4642 TR |
2281 | |
2282 | if (cpu->pmsav8r_hdregion > 0xff) { | |
2283 | error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32, | |
2284 | cpu->pmsav8r_hdregion); | |
2285 | return; | |
2286 | } | |
2287 | ||
2288 | if (cpu->pmsav8r_hdregion) { | |
2289 | env->pmsav8.hprbar = g_new0(uint32_t, | |
2290 | cpu->pmsav8r_hdregion); | |
2291 | env->pmsav8.hprlar = g_new0(uint32_t, | |
2292 | cpu->pmsav8r_hdregion); | |
2293 | } | |
3281af81 PC |
2294 | } |
2295 | ||
9901c576 PM |
2296 | if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { |
2297 | uint32_t nr = cpu->sau_sregion; | |
2298 | ||
2299 | if (nr > 0xff) { | |
2300 | error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr); | |
2301 | return; | |
2302 | } | |
2303 | ||
2304 | if (nr) { | |
2305 | env->sau.rbar = g_new0(uint32_t, nr); | |
2306 | env->sau.rlar = g_new0(uint32_t, nr); | |
2307 | } | |
2308 | } | |
2309 | ||
91db4642 CLG |
2310 | if (arm_feature(env, ARM_FEATURE_EL3)) { |
2311 | set_feature(env, ARM_FEATURE_VBAR); | |
2312 | } | |
2313 | ||
f6fc36de JPB |
2314 | #ifndef CONFIG_USER_ONLY |
2315 | if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) { | |
2316 | arm_register_el_change_hook(cpu, >_rme_post_el_change, 0); | |
2317 | } | |
2318 | #endif | |
2319 | ||
2ceb98c0 | 2320 | register_cp_regs_for_features(cpu); |
14969266 AF |
2321 | arm_cpu_register_gdb_regs_for_features(cpu); |
2322 | ||
721fae12 PM |
2323 | init_cpreg_list(cpu); |
2324 | ||
9e273ef2 | 2325 | #ifndef CONFIG_USER_ONLY |
cc7d44c2 LX |
2326 | MachineState *ms = MACHINE(qdev_get_machine()); |
2327 | unsigned int smp_cpus = ms->smp.cpus; | |
8bce44a2 | 2328 | bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY); |
cc7d44c2 | 2329 | |
8bce44a2 RH |
2330 | /* |
2331 | * We must set cs->num_ases to the final value before | |
2332 | * the first call to cpu_address_space_init. | |
2333 | */ | |
2334 | if (cpu->tag_memory != NULL) { | |
2335 | cs->num_ases = 3 + has_secure; | |
2336 | } else { | |
2337 | cs->num_ases = 1 + has_secure; | |
2338 | } | |
1d2091bc | 2339 | |
8bce44a2 | 2340 | if (has_secure) { |
9e273ef2 PM |
2341 | if (!cpu->secure_memory) { |
2342 | cpu->secure_memory = cs->memory; | |
2343 | } | |
80ceb07a PX |
2344 | cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory", |
2345 | cpu->secure_memory); | |
9e273ef2 | 2346 | } |
8bce44a2 RH |
2347 | |
2348 | if (cpu->tag_memory != NULL) { | |
2349 | cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory", | |
2350 | cpu->tag_memory); | |
2351 | if (has_secure) { | |
2352 | cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory", | |
2353 | cpu->secure_tag_memory); | |
2354 | } | |
8bce44a2 RH |
2355 | } |
2356 | ||
80ceb07a | 2357 | cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory); |
f9a69711 AF |
2358 | |
2359 | /* No core_count specified, default to smp_cpus. */ | |
2360 | if (cpu->core_count == -1) { | |
2361 | cpu->core_count = smp_cpus; | |
2362 | } | |
9e273ef2 PM |
2363 | #endif |
2364 | ||
a4157b80 RH |
2365 | if (tcg_enabled()) { |
2366 | int dcz_blocklen = 4 << cpu->dcz_blocksize; | |
2367 | ||
2368 | /* | |
2369 | * We only support DCZ blocklen that fits on one page. | |
2370 | * | |
2371 | * Architectually this is always true. However TARGET_PAGE_SIZE | |
2372 | * is variable and, for compatibility with -machine virt-2.7, | |
2373 | * is only 1KiB, as an artifact of legacy ARMv5 subpage support. | |
2374 | * But even then, while the largest architectural DCZ blocklen | |
2375 | * is 2KiB, no cpu actually uses such a large blocklen. | |
2376 | */ | |
2377 | assert(dcz_blocklen <= TARGET_PAGE_SIZE); | |
2378 | ||
2379 | /* | |
2380 | * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say | |
2381 | * both nibbles of each byte storing tag data may be written at once. | |
2382 | * Since TAG_GRANULE is 16, this means that blocklen must be >= 32. | |
2383 | */ | |
2384 | if (cpu_isar_feature(aa64_mte, cpu)) { | |
2385 | assert(dcz_blocklen >= 2 * TAG_GRANULE); | |
2386 | } | |
2387 | } | |
2388 | ||
14a10fc3 | 2389 | qemu_init_vcpu(cs); |
00d0f7cb | 2390 | cpu_reset(cs); |
14969266 AF |
2391 | |
2392 | acc->parent_realize(dev, errp); | |
581be094 PM |
2393 | } |
2394 | ||
5900d6b2 AF |
2395 | static ObjectClass *arm_cpu_class_by_name(const char *cpu_model) |
2396 | { | |
2397 | ObjectClass *oc; | |
51492fd1 | 2398 | char *typename; |
fb8d6c24 | 2399 | char **cpuname; |
a0032cc5 | 2400 | const char *cpunamestr; |
5900d6b2 | 2401 | |
fb8d6c24 | 2402 | cpuname = g_strsplit(cpu_model, ",", 1); |
a0032cc5 PM |
2403 | cpunamestr = cpuname[0]; |
2404 | #ifdef CONFIG_USER_ONLY | |
2405 | /* For backwards compatibility usermode emulation allows "-cpu any", | |
2406 | * which has the same semantics as "-cpu max". | |
2407 | */ | |
2408 | if (!strcmp(cpunamestr, "any")) { | |
2409 | cpunamestr = "max"; | |
2410 | } | |
2411 | #endif | |
2412 | typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr); | |
51492fd1 | 2413 | oc = object_class_by_name(typename); |
fb8d6c24 | 2414 | g_strfreev(cpuname); |
51492fd1 | 2415 | g_free(typename); |
d5be19f5 | 2416 | |
5900d6b2 AF |
2417 | return oc; |
2418 | } | |
2419 | ||
5de16430 | 2420 | static Property arm_cpu_properties[] = { |
e544f800 | 2421 | DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0), |
ce5b1bbf LV |
2422 | DEFINE_PROP_UINT64("mp-affinity", ARMCPU, |
2423 | mp_affinity, ARM64_AFFINITY_INVALID), | |
15f8b142 | 2424 | DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID), |
f9a69711 | 2425 | DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1), |
5de16430 PM |
2426 | DEFINE_PROP_END_OF_LIST() |
2427 | }; | |
2428 | ||
a6506838 | 2429 | static const gchar *arm_gdb_arch_name(CPUState *cs) |
b3820e6c DH |
2430 | { |
2431 | ARMCPU *cpu = ARM_CPU(cs); | |
2432 | CPUARMState *env = &cpu->env; | |
2433 | ||
2434 | if (arm_feature(env, ARM_FEATURE_IWMMXT)) { | |
a6506838 | 2435 | return "iwmmxt"; |
b3820e6c | 2436 | } |
a6506838 | 2437 | return "arm"; |
b3820e6c DH |
2438 | } |
2439 | ||
8b80bd28 PMD |
2440 | #ifndef CONFIG_USER_ONLY |
2441 | #include "hw/core/sysemu-cpu-ops.h" | |
2442 | ||
2443 | static const struct SysemuCPUOps arm_sysemu_ops = { | |
08928c6d | 2444 | .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug, |
faf39e82 | 2445 | .asidx_from_attrs = arm_asidx_from_attrs, |
715e3c1a PMD |
2446 | .write_elf32_note = arm_cpu_write_elf32_note, |
2447 | .write_elf64_note = arm_cpu_write_elf64_note, | |
da383e02 | 2448 | .virtio_is_big_endian = arm_cpu_virtio_is_big_endian, |
feece4d0 | 2449 | .legacy_vmsd = &vmstate_arm_cpu, |
8b80bd28 PMD |
2450 | }; |
2451 | #endif | |
2452 | ||
78271684 | 2453 | #ifdef CONFIG_TCG |
11906557 | 2454 | static const struct TCGCPUOps arm_tcg_ops = { |
78271684 CF |
2455 | .initialize = arm_translate_init, |
2456 | .synchronize_from_tb = arm_cpu_synchronize_from_tb, | |
78271684 | 2457 | .debug_excp_handler = arm_debug_excp_handler, |
56c6c98d | 2458 | .restore_state_to_opc = arm_restore_state_to_opc, |
78271684 | 2459 | |
9b12b6b4 RH |
2460 | #ifdef CONFIG_USER_ONLY |
2461 | .record_sigsegv = arm_cpu_record_sigsegv, | |
39a099ca | 2462 | .record_sigbus = arm_cpu_record_sigbus, |
9b12b6b4 RH |
2463 | #else |
2464 | .tlb_fill = arm_cpu_tlb_fill, | |
083afd18 | 2465 | .cpu_exec_interrupt = arm_cpu_exec_interrupt, |
78271684 CF |
2466 | .do_interrupt = arm_cpu_do_interrupt, |
2467 | .do_transaction_failed = arm_cpu_do_transaction_failed, | |
2468 | .do_unaligned_access = arm_cpu_do_unaligned_access, | |
2469 | .adjust_watchpoint_address = arm_adjust_watchpoint_address, | |
2470 | .debug_check_watchpoint = arm_debug_check_watchpoint, | |
b00d86bc | 2471 | .debug_check_breakpoint = arm_debug_check_breakpoint, |
78271684 CF |
2472 | #endif /* !CONFIG_USER_ONLY */ |
2473 | }; | |
2474 | #endif /* CONFIG_TCG */ | |
2475 | ||
dec9c2d4 AF |
2476 | static void arm_cpu_class_init(ObjectClass *oc, void *data) |
2477 | { | |
2478 | ARMCPUClass *acc = ARM_CPU_CLASS(oc); | |
2479 | CPUClass *cc = CPU_CLASS(acc); | |
14969266 | 2480 | DeviceClass *dc = DEVICE_CLASS(oc); |
9130cade | 2481 | ResettableClass *rc = RESETTABLE_CLASS(oc); |
14969266 | 2482 | |
bf853881 PMD |
2483 | device_class_set_parent_realize(dc, arm_cpu_realizefn, |
2484 | &acc->parent_realize); | |
dec9c2d4 | 2485 | |
4f67d30b | 2486 | device_class_set_props(dc, arm_cpu_properties); |
9130cade PM |
2487 | |
2488 | resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL, | |
2489 | &acc->parent_phases); | |
5900d6b2 AF |
2490 | |
2491 | cc->class_by_name = arm_cpu_class_by_name; | |
8c2e1b00 | 2492 | cc->has_work = arm_cpu_has_work; |
878096ee | 2493 | cc->dump_state = arm_cpu_dump_state; |
f45748f1 | 2494 | cc->set_pc = arm_cpu_set_pc; |
e4fdf9df | 2495 | cc->get_pc = arm_cpu_get_pc; |
5b50e790 AF |
2496 | cc->gdb_read_register = arm_cpu_gdb_read_register; |
2497 | cc->gdb_write_register = arm_cpu_gdb_write_register; | |
7350d553 | 2498 | #ifndef CONFIG_USER_ONLY |
8b80bd28 | 2499 | cc->sysemu_ops = &arm_sysemu_ops; |
00b941e5 | 2500 | #endif |
a0e372f0 | 2501 | cc->gdb_num_core_regs = 26; |
b3820e6c | 2502 | cc->gdb_arch_name = arm_gdb_arch_name; |
200bf5b7 | 2503 | cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml; |
2472b6c0 | 2504 | cc->gdb_stop_before_watchpoint = true; |
48440620 | 2505 | cc->disas_set_info = arm_disas_set_info; |
78271684 | 2506 | |
74d7fc7f | 2507 | #ifdef CONFIG_TCG |
78271684 | 2508 | cc->tcg_ops = &arm_tcg_ops; |
cbc183d2 | 2509 | #endif /* CONFIG_TCG */ |
dec9c2d4 AF |
2510 | } |
2511 | ||
51e5ef45 MAL |
2512 | static void arm_cpu_instance_init(Object *obj) |
2513 | { | |
2514 | ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj); | |
2515 | ||
2516 | acc->info->initfn(obj); | |
2517 | arm_cpu_post_init(obj); | |
2518 | } | |
2519 | ||
2520 | static void cpu_register_class_init(ObjectClass *oc, void *data) | |
2521 | { | |
2522 | ARMCPUClass *acc = ARM_CPU_CLASS(oc); | |
48de6462 | 2523 | CPUClass *cc = CPU_CLASS(acc); |
51e5ef45 MAL |
2524 | |
2525 | acc->info = data; | |
48de6462 | 2526 | cc->gdb_core_xml_file = "arm-core.xml"; |
51e5ef45 MAL |
2527 | } |
2528 | ||
37bcf244 | 2529 | void arm_cpu_register(const ARMCPUInfo *info) |
777dc784 PM |
2530 | { |
2531 | TypeInfo type_info = { | |
777dc784 | 2532 | .parent = TYPE_ARM_CPU, |
51e5ef45 | 2533 | .instance_init = arm_cpu_instance_init, |
51e5ef45 MAL |
2534 | .class_init = info->class_init ?: cpu_register_class_init, |
2535 | .class_data = (void *)info, | |
777dc784 PM |
2536 | }; |
2537 | ||
51492fd1 | 2538 | type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name); |
918fd083 | 2539 | type_register(&type_info); |
51492fd1 | 2540 | g_free((void *)type_info.name); |
777dc784 PM |
2541 | } |
2542 | ||
dec9c2d4 AF |
2543 | static const TypeInfo arm_cpu_type_info = { |
2544 | .name = TYPE_ARM_CPU, | |
2545 | .parent = TYPE_CPU, | |
2546 | .instance_size = sizeof(ARMCPU), | |
d03087bd | 2547 | .instance_align = __alignof__(ARMCPU), |
777dc784 | 2548 | .instance_init = arm_cpu_initfn, |
4b6a83fb | 2549 | .instance_finalize = arm_cpu_finalizefn, |
777dc784 | 2550 | .abstract = true, |
dec9c2d4 AF |
2551 | .class_size = sizeof(ARMCPUClass), |
2552 | .class_init = arm_cpu_class_init, | |
2553 | }; | |
2554 | ||
2555 | static void arm_cpu_register_types(void) | |
2556 | { | |
2557 | type_register_static(&arm_cpu_type_info); | |
2558 | } | |
2559 | ||
2560 | type_init(arm_cpu_register_types) |