]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/kvm64.c
4bb68646e434e4c46a7eaceda4d9998d3e2b6fbe
[mirror_qemu.git] / target / arm / kvm64.c
1 /*
2 * ARM implementation of KVM hooks, 64 bit specific code
3 *
4 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems
5 * Copyright Alex Bennée 2014, Linaro
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 *
10 */
11
12 #include "qemu/osdep.h"
13 #include <sys/ioctl.h>
14 #include <sys/ptrace.h>
15
16 #include <linux/elf.h>
17 #include <linux/kvm.h>
18
19 #include "qapi/error.h"
20 #include "cpu.h"
21 #include "qemu/timer.h"
22 #include "qemu/error-report.h"
23 #include "qemu/host-utils.h"
24 #include "qemu/main-loop.h"
25 #include "exec/gdbstub.h"
26 #include "sysemu/runstate.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/kvm_int.h"
29 #include "kvm_arm.h"
30 #include "internals.h"
31 #include "hw/acpi/acpi.h"
32 #include "hw/acpi/ghes.h"
33
34 static bool have_guest_debug;
35
36 void kvm_arm_init_debug(KVMState *s)
37 {
38 have_guest_debug = kvm_check_extension(s,
39 KVM_CAP_SET_GUEST_DEBUG);
40
41 max_hw_wps = kvm_check_extension(s, KVM_CAP_GUEST_DEBUG_HW_WPS);
42 hw_watchpoints = g_array_sized_new(true, true,
43 sizeof(HWWatchpoint), max_hw_wps);
44
45 max_hw_bps = kvm_check_extension(s, KVM_CAP_GUEST_DEBUG_HW_BPS);
46 hw_breakpoints = g_array_sized_new(true, true,
47 sizeof(HWBreakpoint), max_hw_bps);
48 return;
49 }
50
51 int kvm_arch_insert_hw_breakpoint(vaddr addr, vaddr len, int type)
52 {
53 switch (type) {
54 case GDB_BREAKPOINT_HW:
55 return insert_hw_breakpoint(addr);
56 break;
57 case GDB_WATCHPOINT_READ:
58 case GDB_WATCHPOINT_WRITE:
59 case GDB_WATCHPOINT_ACCESS:
60 return insert_hw_watchpoint(addr, len, type);
61 default:
62 return -ENOSYS;
63 }
64 }
65
66 int kvm_arch_remove_hw_breakpoint(vaddr addr, vaddr len, int type)
67 {
68 switch (type) {
69 case GDB_BREAKPOINT_HW:
70 return delete_hw_breakpoint(addr);
71 case GDB_WATCHPOINT_READ:
72 case GDB_WATCHPOINT_WRITE:
73 case GDB_WATCHPOINT_ACCESS:
74 return delete_hw_watchpoint(addr, len, type);
75 default:
76 return -ENOSYS;
77 }
78 }
79
80
81 void kvm_arch_remove_all_hw_breakpoints(void)
82 {
83 if (cur_hw_wps > 0) {
84 g_array_remove_range(hw_watchpoints, 0, cur_hw_wps);
85 }
86 if (cur_hw_bps > 0) {
87 g_array_remove_range(hw_breakpoints, 0, cur_hw_bps);
88 }
89 }
90
91 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr)
92 {
93 int i;
94 memset(ptr, 0, sizeof(struct kvm_guest_debug_arch));
95
96 for (i = 0; i < max_hw_wps; i++) {
97 HWWatchpoint *wp = get_hw_wp(i);
98 ptr->dbg_wcr[i] = wp->wcr;
99 ptr->dbg_wvr[i] = wp->wvr;
100 }
101 for (i = 0; i < max_hw_bps; i++) {
102 HWBreakpoint *bp = get_hw_bp(i);
103 ptr->dbg_bcr[i] = bp->bcr;
104 ptr->dbg_bvr[i] = bp->bvr;
105 }
106 }
107
108 bool kvm_arm_hw_debug_active(CPUState *cs)
109 {
110 return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
111 }
112
113 static bool kvm_arm_set_device_attr(CPUState *cs, struct kvm_device_attr *attr,
114 const char *name)
115 {
116 int err;
117
118 err = kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, attr);
119 if (err != 0) {
120 error_report("%s: KVM_HAS_DEVICE_ATTR: %s", name, strerror(-err));
121 return false;
122 }
123
124 err = kvm_vcpu_ioctl(cs, KVM_SET_DEVICE_ATTR, attr);
125 if (err != 0) {
126 error_report("%s: KVM_SET_DEVICE_ATTR: %s", name, strerror(-err));
127 return false;
128 }
129
130 return true;
131 }
132
133 void kvm_arm_pmu_init(CPUState *cs)
134 {
135 struct kvm_device_attr attr = {
136 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
137 .attr = KVM_ARM_VCPU_PMU_V3_INIT,
138 };
139
140 if (!ARM_CPU(cs)->has_pmu) {
141 return;
142 }
143 if (!kvm_arm_set_device_attr(cs, &attr, "PMU")) {
144 error_report("failed to init PMU");
145 abort();
146 }
147 }
148
149 void kvm_arm_pmu_set_irq(CPUState *cs, int irq)
150 {
151 struct kvm_device_attr attr = {
152 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
153 .addr = (intptr_t)&irq,
154 .attr = KVM_ARM_VCPU_PMU_V3_IRQ,
155 };
156
157 if (!ARM_CPU(cs)->has_pmu) {
158 return;
159 }
160 if (!kvm_arm_set_device_attr(cs, &attr, "PMU")) {
161 error_report("failed to set irq for PMU");
162 abort();
163 }
164 }
165
166 void kvm_arm_pvtime_init(CPUState *cs, uint64_t ipa)
167 {
168 struct kvm_device_attr attr = {
169 .group = KVM_ARM_VCPU_PVTIME_CTRL,
170 .attr = KVM_ARM_VCPU_PVTIME_IPA,
171 .addr = (uint64_t)&ipa,
172 };
173
174 if (ARM_CPU(cs)->kvm_steal_time == ON_OFF_AUTO_OFF) {
175 return;
176 }
177 if (!kvm_arm_set_device_attr(cs, &attr, "PVTIME IPA")) {
178 error_report("failed to init PVTIME IPA");
179 abort();
180 }
181 }
182
183 static int read_sys_reg32(int fd, uint32_t *pret, uint64_t id)
184 {
185 uint64_t ret;
186 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)&ret };
187 int err;
188
189 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64);
190 err = ioctl(fd, KVM_GET_ONE_REG, &idreg);
191 if (err < 0) {
192 return -1;
193 }
194 *pret = ret;
195 return 0;
196 }
197
198 static int read_sys_reg64(int fd, uint64_t *pret, uint64_t id)
199 {
200 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)pret };
201
202 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64);
203 return ioctl(fd, KVM_GET_ONE_REG, &idreg);
204 }
205
206 static bool kvm_arm_pauth_supported(void)
207 {
208 return (kvm_check_extension(kvm_state, KVM_CAP_ARM_PTRAUTH_ADDRESS) &&
209 kvm_check_extension(kvm_state, KVM_CAP_ARM_PTRAUTH_GENERIC));
210 }
211
212 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
213 {
214 /* Identify the feature bits corresponding to the host CPU, and
215 * fill out the ARMHostCPUClass fields accordingly. To do this
216 * we have to create a scratch VM, create a single CPU inside it,
217 * and then query that CPU for the relevant ID registers.
218 */
219 int fdarray[3];
220 bool sve_supported;
221 bool pmu_supported = false;
222 uint64_t features = 0;
223 int err;
224
225 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
226 * we know these will only support creating one kind of guest CPU,
227 * which is its preferred CPU type. Fortunately these old kernels
228 * support only a very limited number of CPUs.
229 */
230 static const uint32_t cpus_to_try[] = {
231 KVM_ARM_TARGET_AEM_V8,
232 KVM_ARM_TARGET_FOUNDATION_V8,
233 KVM_ARM_TARGET_CORTEX_A57,
234 QEMU_KVM_ARM_TARGET_NONE
235 };
236 /*
237 * target = -1 informs kvm_arm_create_scratch_host_vcpu()
238 * to use the preferred target
239 */
240 struct kvm_vcpu_init init = { .target = -1, };
241
242 /*
243 * Ask for SVE if supported, so that we can query ID_AA64ZFR0,
244 * which is otherwise RAZ.
245 */
246 sve_supported = kvm_arm_sve_supported();
247 if (sve_supported) {
248 init.features[0] |= 1 << KVM_ARM_VCPU_SVE;
249 }
250
251 /*
252 * Ask for Pointer Authentication if supported, so that we get
253 * the unsanitized field values for AA64ISAR1_EL1.
254 */
255 if (kvm_arm_pauth_supported()) {
256 init.features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS |
257 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
258 }
259
260 if (kvm_arm_pmu_supported()) {
261 init.features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
262 pmu_supported = true;
263 }
264
265 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
266 return false;
267 }
268
269 ahcf->target = init.target;
270 ahcf->dtb_compatible = "arm,arm-v8";
271
272 err = read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr0,
273 ARM64_SYS_REG(3, 0, 0, 4, 0));
274 if (unlikely(err < 0)) {
275 /*
276 * Before v4.15, the kernel only exposed a limited number of system
277 * registers, not including any of the interesting AArch64 ID regs.
278 * For the most part we could leave these fields as zero with minimal
279 * effect, since this does not affect the values seen by the guest.
280 *
281 * However, it could cause problems down the line for QEMU,
282 * so provide a minimal v8.0 default.
283 *
284 * ??? Could read MIDR and use knowledge from cpu64.c.
285 * ??? Could map a page of memory into our temp guest and
286 * run the tiniest of hand-crafted kernels to extract
287 * the values seen by the guest.
288 * ??? Either of these sounds like too much effort just
289 * to work around running a modern host kernel.
290 */
291 ahcf->isar.id_aa64pfr0 = 0x00000011; /* EL1&0, AArch64 only */
292 err = 0;
293 } else {
294 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr1,
295 ARM64_SYS_REG(3, 0, 0, 4, 1));
296 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64smfr0,
297 ARM64_SYS_REG(3, 0, 0, 4, 5));
298 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr0,
299 ARM64_SYS_REG(3, 0, 0, 5, 0));
300 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr1,
301 ARM64_SYS_REG(3, 0, 0, 5, 1));
302 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar0,
303 ARM64_SYS_REG(3, 0, 0, 6, 0));
304 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar1,
305 ARM64_SYS_REG(3, 0, 0, 6, 1));
306 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar2,
307 ARM64_SYS_REG(3, 0, 0, 6, 2));
308 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr0,
309 ARM64_SYS_REG(3, 0, 0, 7, 0));
310 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr1,
311 ARM64_SYS_REG(3, 0, 0, 7, 1));
312 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr2,
313 ARM64_SYS_REG(3, 0, 0, 7, 2));
314
315 /*
316 * Note that if AArch32 support is not present in the host,
317 * the AArch32 sysregs are present to be read, but will
318 * return UNKNOWN values. This is neither better nor worse
319 * than skipping the reads and leaving 0, as we must avoid
320 * considering the values in every case.
321 */
322 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr0,
323 ARM64_SYS_REG(3, 0, 0, 1, 0));
324 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr1,
325 ARM64_SYS_REG(3, 0, 0, 1, 1));
326 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_dfr0,
327 ARM64_SYS_REG(3, 0, 0, 1, 2));
328 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr0,
329 ARM64_SYS_REG(3, 0, 0, 1, 4));
330 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr1,
331 ARM64_SYS_REG(3, 0, 0, 1, 5));
332 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr2,
333 ARM64_SYS_REG(3, 0, 0, 1, 6));
334 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr3,
335 ARM64_SYS_REG(3, 0, 0, 1, 7));
336 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar0,
337 ARM64_SYS_REG(3, 0, 0, 2, 0));
338 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar1,
339 ARM64_SYS_REG(3, 0, 0, 2, 1));
340 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar2,
341 ARM64_SYS_REG(3, 0, 0, 2, 2));
342 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar3,
343 ARM64_SYS_REG(3, 0, 0, 2, 3));
344 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar4,
345 ARM64_SYS_REG(3, 0, 0, 2, 4));
346 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar5,
347 ARM64_SYS_REG(3, 0, 0, 2, 5));
348 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr4,
349 ARM64_SYS_REG(3, 0, 0, 2, 6));
350 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar6,
351 ARM64_SYS_REG(3, 0, 0, 2, 7));
352
353 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr0,
354 ARM64_SYS_REG(3, 0, 0, 3, 0));
355 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr1,
356 ARM64_SYS_REG(3, 0, 0, 3, 1));
357 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr2,
358 ARM64_SYS_REG(3, 0, 0, 3, 2));
359 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr2,
360 ARM64_SYS_REG(3, 0, 0, 3, 4));
361 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_dfr1,
362 ARM64_SYS_REG(3, 0, 0, 3, 5));
363 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr5,
364 ARM64_SYS_REG(3, 0, 0, 3, 6));
365
366 /*
367 * DBGDIDR is a bit complicated because the kernel doesn't
368 * provide an accessor for it in 64-bit mode, which is what this
369 * scratch VM is in, and there's no architected "64-bit sysreg
370 * which reads the same as the 32-bit register" the way there is
371 * for other ID registers. Instead we synthesize a value from the
372 * AArch64 ID_AA64DFR0, the same way the kernel code in
373 * arch/arm64/kvm/sys_regs.c:trap_dbgidr() does.
374 * We only do this if the CPU supports AArch32 at EL1.
375 */
376 if (FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL1) >= 2) {
377 int wrps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, WRPS);
378 int brps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, BRPS);
379 int ctx_cmps =
380 FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, CTX_CMPS);
381 int version = 6; /* ARMv8 debug architecture */
382 bool has_el3 =
383 !!FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL3);
384 uint32_t dbgdidr = 0;
385
386 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, WRPS, wrps);
387 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, BRPS, brps);
388 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, CTX_CMPS, ctx_cmps);
389 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, VERSION, version);
390 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, NSUHD_IMP, has_el3);
391 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, SE_IMP, has_el3);
392 dbgdidr |= (1 << 15); /* RES1 bit */
393 ahcf->isar.dbgdidr = dbgdidr;
394 }
395
396 if (pmu_supported) {
397 /* PMCR_EL0 is only accessible if the vCPU has feature PMU_V3 */
398 err |= read_sys_reg64(fdarray[2], &ahcf->isar.reset_pmcr_el0,
399 ARM64_SYS_REG(3, 3, 9, 12, 0));
400 }
401
402 if (sve_supported) {
403 /*
404 * There is a range of kernels between kernel commit 73433762fcae
405 * and f81cb2c3ad41 which have a bug where the kernel doesn't
406 * expose SYS_ID_AA64ZFR0_EL1 via the ONE_REG API unless the VM has
407 * enabled SVE support, which resulted in an error rather than RAZ.
408 * So only read the register if we set KVM_ARM_VCPU_SVE above.
409 */
410 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64zfr0,
411 ARM64_SYS_REG(3, 0, 0, 4, 4));
412 }
413 }
414
415 kvm_arm_destroy_scratch_host_vcpu(fdarray);
416
417 if (err < 0) {
418 return false;
419 }
420
421 /*
422 * We can assume any KVM supporting CPU is at least a v8
423 * with VFPv4+Neon; this in turn implies most of the other
424 * feature bits.
425 */
426 features |= 1ULL << ARM_FEATURE_V8;
427 features |= 1ULL << ARM_FEATURE_NEON;
428 features |= 1ULL << ARM_FEATURE_AARCH64;
429 features |= 1ULL << ARM_FEATURE_PMU;
430 features |= 1ULL << ARM_FEATURE_GENERIC_TIMER;
431
432 ahcf->features = features;
433
434 return true;
435 }
436
437 void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp)
438 {
439 bool has_steal_time = kvm_arm_steal_time_supported();
440
441 if (cpu->kvm_steal_time == ON_OFF_AUTO_AUTO) {
442 if (!has_steal_time || !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
443 cpu->kvm_steal_time = ON_OFF_AUTO_OFF;
444 } else {
445 cpu->kvm_steal_time = ON_OFF_AUTO_ON;
446 }
447 } else if (cpu->kvm_steal_time == ON_OFF_AUTO_ON) {
448 if (!has_steal_time) {
449 error_setg(errp, "'kvm-steal-time' cannot be enabled "
450 "on this host");
451 return;
452 } else if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
453 /*
454 * DEN0057A chapter 2 says "This specification only covers
455 * systems in which the Execution state of the hypervisor
456 * as well as EL1 of virtual machines is AArch64.". And,
457 * to ensure that, the smc/hvc calls are only specified as
458 * smc64/hvc64.
459 */
460 error_setg(errp, "'kvm-steal-time' cannot be enabled "
461 "for AArch32 guests");
462 return;
463 }
464 }
465 }
466
467 bool kvm_arm_aarch32_supported(void)
468 {
469 return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL1_32BIT);
470 }
471
472 bool kvm_arm_sve_supported(void)
473 {
474 return kvm_check_extension(kvm_state, KVM_CAP_ARM_SVE);
475 }
476
477 bool kvm_arm_steal_time_supported(void)
478 {
479 return kvm_check_extension(kvm_state, KVM_CAP_STEAL_TIME);
480 }
481
482 QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN != 1);
483
484 uint32_t kvm_arm_sve_get_vls(CPUState *cs)
485 {
486 /* Only call this function if kvm_arm_sve_supported() returns true. */
487 static uint64_t vls[KVM_ARM64_SVE_VLS_WORDS];
488 static bool probed;
489 uint32_t vq = 0;
490 int i;
491
492 /*
493 * KVM ensures all host CPUs support the same set of vector lengths.
494 * So we only need to create the scratch VCPUs once and then cache
495 * the results.
496 */
497 if (!probed) {
498 struct kvm_vcpu_init init = {
499 .target = -1,
500 .features[0] = (1 << KVM_ARM_VCPU_SVE),
501 };
502 struct kvm_one_reg reg = {
503 .id = KVM_REG_ARM64_SVE_VLS,
504 .addr = (uint64_t)&vls[0],
505 };
506 int fdarray[3], ret;
507
508 probed = true;
509
510 if (!kvm_arm_create_scratch_host_vcpu(NULL, fdarray, &init)) {
511 error_report("failed to create scratch VCPU with SVE enabled");
512 abort();
513 }
514 ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
515 kvm_arm_destroy_scratch_host_vcpu(fdarray);
516 if (ret) {
517 error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
518 strerror(errno));
519 abort();
520 }
521
522 for (i = KVM_ARM64_SVE_VLS_WORDS - 1; i >= 0; --i) {
523 if (vls[i]) {
524 vq = 64 - clz64(vls[i]) + i * 64;
525 break;
526 }
527 }
528 if (vq > ARM_MAX_VQ) {
529 warn_report("KVM supports vector lengths larger than "
530 "QEMU can enable");
531 vls[0] &= MAKE_64BIT_MASK(0, ARM_MAX_VQ);
532 }
533 }
534
535 return vls[0];
536 }
537
538 static int kvm_arm_sve_set_vls(CPUState *cs)
539 {
540 ARMCPU *cpu = ARM_CPU(cs);
541 uint64_t vls[KVM_ARM64_SVE_VLS_WORDS] = { cpu->sve_vq.map };
542
543 assert(cpu->sve_max_vq <= KVM_ARM64_SVE_VQ_MAX);
544
545 return kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_VLS, &vls[0]);
546 }
547
548 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
549
550 int kvm_arch_init_vcpu(CPUState *cs)
551 {
552 int ret;
553 uint64_t mpidr;
554 ARMCPU *cpu = ARM_CPU(cs);
555 CPUARMState *env = &cpu->env;
556 uint64_t psciver;
557
558 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
559 !object_dynamic_cast(OBJECT(cpu), TYPE_AARCH64_CPU)) {
560 error_report("KVM is not supported for this guest CPU type");
561 return -EINVAL;
562 }
563
564 qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
565
566 /* Determine init features for this CPU */
567 memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
568 if (cs->start_powered_off) {
569 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
570 }
571 if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
572 cpu->psci_version = QEMU_PSCI_VERSION_0_2;
573 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
574 }
575 if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
576 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
577 }
578 if (!kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) {
579 cpu->has_pmu = false;
580 }
581 if (cpu->has_pmu) {
582 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
583 } else {
584 env->features &= ~(1ULL << ARM_FEATURE_PMU);
585 }
586 if (cpu_isar_feature(aa64_sve, cpu)) {
587 assert(kvm_arm_sve_supported());
588 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_SVE;
589 }
590 if (cpu_isar_feature(aa64_pauth, cpu)) {
591 cpu->kvm_init_features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS |
592 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
593 }
594
595 /* Do KVM_ARM_VCPU_INIT ioctl */
596 ret = kvm_arm_vcpu_init(cs);
597 if (ret) {
598 return ret;
599 }
600
601 if (cpu_isar_feature(aa64_sve, cpu)) {
602 ret = kvm_arm_sve_set_vls(cs);
603 if (ret) {
604 return ret;
605 }
606 ret = kvm_arm_vcpu_finalize(cs, KVM_ARM_VCPU_SVE);
607 if (ret) {
608 return ret;
609 }
610 }
611
612 /*
613 * KVM reports the exact PSCI version it is implementing via a
614 * special sysreg. If it is present, use its contents to determine
615 * what to report to the guest in the dtb (it is the PSCI version,
616 * in the same 15-bits major 16-bits minor format that PSCI_VERSION
617 * returns).
618 */
619 if (!kvm_get_one_reg(cs, KVM_REG_ARM_PSCI_VERSION, &psciver)) {
620 cpu->psci_version = psciver;
621 }
622
623 /*
624 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
625 * Currently KVM has its own idea about MPIDR assignment, so we
626 * override our defaults with what we get from KVM.
627 */
628 ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
629 if (ret) {
630 return ret;
631 }
632 cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
633
634 /* Check whether user space can specify guest syndrome value */
635 kvm_arm_init_serror_injection(cs);
636
637 return kvm_arm_init_cpreg_list(cpu);
638 }
639
640 int kvm_arch_destroy_vcpu(CPUState *cs)
641 {
642 return 0;
643 }
644
645 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
646 {
647 /* Return true if the regidx is a register we should synchronize
648 * via the cpreg_tuples array (ie is not a core or sve reg that
649 * we sync by hand in kvm_arch_get/put_registers())
650 */
651 switch (regidx & KVM_REG_ARM_COPROC_MASK) {
652 case KVM_REG_ARM_CORE:
653 case KVM_REG_ARM64_SVE:
654 return false;
655 default:
656 return true;
657 }
658 }
659
660 typedef struct CPRegStateLevel {
661 uint64_t regidx;
662 int level;
663 } CPRegStateLevel;
664
665 /* All system registers not listed in the following table are assumed to be
666 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
667 * often, you must add it to this table with a state of either
668 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
669 */
670 static const CPRegStateLevel non_runtime_cpregs[] = {
671 { KVM_REG_ARM_TIMER_CNT, KVM_PUT_FULL_STATE },
672 { KVM_REG_ARM_PTIMER_CNT, KVM_PUT_FULL_STATE },
673 };
674
675 int kvm_arm_cpreg_level(uint64_t regidx)
676 {
677 int i;
678
679 for (i = 0; i < ARRAY_SIZE(non_runtime_cpregs); i++) {
680 const CPRegStateLevel *l = &non_runtime_cpregs[i];
681 if (l->regidx == regidx) {
682 return l->level;
683 }
684 }
685
686 return KVM_PUT_RUNTIME_STATE;
687 }
688
689 /* Callers must hold the iothread mutex lock */
690 static void kvm_inject_arm_sea(CPUState *c)
691 {
692 ARMCPU *cpu = ARM_CPU(c);
693 CPUARMState *env = &cpu->env;
694 uint32_t esr;
695 bool same_el;
696
697 c->exception_index = EXCP_DATA_ABORT;
698 env->exception.target_el = 1;
699
700 /*
701 * Set the DFSC to synchronous external abort and set FnV to not valid,
702 * this will tell guest the FAR_ELx is UNKNOWN for this abort.
703 */
704 same_el = arm_current_el(env) == env->exception.target_el;
705 esr = syn_data_abort_no_iss(same_el, 1, 0, 0, 0, 0, 0x10);
706
707 env->exception.syndrome = esr;
708
709 arm_cpu_do_interrupt(c);
710 }
711
712 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
713 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
714
715 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
716 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
717
718 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
719 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
720
721 static int kvm_arch_put_fpsimd(CPUState *cs)
722 {
723 CPUARMState *env = &ARM_CPU(cs)->env;
724 int i, ret;
725
726 for (i = 0; i < 32; i++) {
727 uint64_t *q = aa64_vfp_qreg(env, i);
728 #if HOST_BIG_ENDIAN
729 uint64_t fp_val[2] = { q[1], q[0] };
730 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]),
731 fp_val);
732 #else
733 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]), q);
734 #endif
735 if (ret) {
736 return ret;
737 }
738 }
739
740 return 0;
741 }
742
743 /*
744 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
745 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
746 * code the slice index to zero for now as it's unlikely we'll need more than
747 * one slice for quite some time.
748 */
749 static int kvm_arch_put_sve(CPUState *cs)
750 {
751 ARMCPU *cpu = ARM_CPU(cs);
752 CPUARMState *env = &cpu->env;
753 uint64_t tmp[ARM_MAX_VQ * 2];
754 uint64_t *r;
755 int n, ret;
756
757 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) {
758 r = sve_bswap64(tmp, &env->vfp.zregs[n].d[0], cpu->sve_max_vq * 2);
759 ret = kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_ZREG(n, 0), r);
760 if (ret) {
761 return ret;
762 }
763 }
764
765 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) {
766 r = sve_bswap64(tmp, r = &env->vfp.pregs[n].p[0],
767 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
768 ret = kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_PREG(n, 0), r);
769 if (ret) {
770 return ret;
771 }
772 }
773
774 r = sve_bswap64(tmp, &env->vfp.pregs[FFR_PRED_NUM].p[0],
775 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
776 ret = kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_FFR(0), r);
777 if (ret) {
778 return ret;
779 }
780
781 return 0;
782 }
783
784 int kvm_arch_put_registers(CPUState *cs, int level)
785 {
786 uint64_t val;
787 uint32_t fpr;
788 int i, ret;
789 unsigned int el;
790
791 ARMCPU *cpu = ARM_CPU(cs);
792 CPUARMState *env = &cpu->env;
793
794 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
795 * AArch64 registers before pushing them out to 64-bit KVM.
796 */
797 if (!is_a64(env)) {
798 aarch64_sync_32_to_64(env);
799 }
800
801 for (i = 0; i < 31; i++) {
802 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.regs[i]),
803 &env->xregs[i]);
804 if (ret) {
805 return ret;
806 }
807 }
808
809 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
810 * QEMU side we keep the current SP in xregs[31] as well.
811 */
812 aarch64_save_sp(env, 1);
813
814 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.sp), &env->sp_el[0]);
815 if (ret) {
816 return ret;
817 }
818
819 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(sp_el1), &env->sp_el[1]);
820 if (ret) {
821 return ret;
822 }
823
824 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
825 if (is_a64(env)) {
826 val = pstate_read(env);
827 } else {
828 val = cpsr_read(env);
829 }
830 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.pstate), &val);
831 if (ret) {
832 return ret;
833 }
834
835 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.pc), &env->pc);
836 if (ret) {
837 return ret;
838 }
839
840 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(elr_el1), &env->elr_el[1]);
841 if (ret) {
842 return ret;
843 }
844
845 /* Saved Program State Registers
846 *
847 * Before we restore from the banked_spsr[] array we need to
848 * ensure that any modifications to env->spsr are correctly
849 * reflected in the banks.
850 */
851 el = arm_current_el(env);
852 if (el > 0 && !is_a64(env)) {
853 i = bank_number(env->uncached_cpsr & CPSR_M);
854 env->banked_spsr[i] = env->spsr;
855 }
856
857 /* KVM 0-4 map to QEMU banks 1-5 */
858 for (i = 0; i < KVM_NR_SPSR; i++) {
859 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(spsr[i]),
860 &env->banked_spsr[i + 1]);
861 if (ret) {
862 return ret;
863 }
864 }
865
866 if (cpu_isar_feature(aa64_sve, cpu)) {
867 ret = kvm_arch_put_sve(cs);
868 } else {
869 ret = kvm_arch_put_fpsimd(cs);
870 }
871 if (ret) {
872 return ret;
873 }
874
875 fpr = vfp_get_fpsr(env);
876 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpsr), &fpr);
877 if (ret) {
878 return ret;
879 }
880
881 fpr = vfp_get_fpcr(env);
882 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpcr), &fpr);
883 if (ret) {
884 return ret;
885 }
886
887 write_cpustate_to_list(cpu, true);
888
889 if (!write_list_to_kvmstate(cpu, level)) {
890 return -EINVAL;
891 }
892
893 /*
894 * Setting VCPU events should be triggered after syncing the registers
895 * to avoid overwriting potential changes made by KVM upon calling
896 * KVM_SET_VCPU_EVENTS ioctl
897 */
898 ret = kvm_put_vcpu_events(cpu);
899 if (ret) {
900 return ret;
901 }
902
903 kvm_arm_sync_mpstate_to_kvm(cpu);
904
905 return ret;
906 }
907
908 static int kvm_arch_get_fpsimd(CPUState *cs)
909 {
910 CPUARMState *env = &ARM_CPU(cs)->env;
911 int i, ret;
912
913 for (i = 0; i < 32; i++) {
914 uint64_t *q = aa64_vfp_qreg(env, i);
915 ret = kvm_get_one_reg(cs, AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]), q);
916 if (ret) {
917 return ret;
918 } else {
919 #if HOST_BIG_ENDIAN
920 uint64_t t;
921 t = q[0], q[0] = q[1], q[1] = t;
922 #endif
923 }
924 }
925
926 return 0;
927 }
928
929 /*
930 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
931 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
932 * code the slice index to zero for now as it's unlikely we'll need more than
933 * one slice for quite some time.
934 */
935 static int kvm_arch_get_sve(CPUState *cs)
936 {
937 ARMCPU *cpu = ARM_CPU(cs);
938 CPUARMState *env = &cpu->env;
939 uint64_t *r;
940 int n, ret;
941
942 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) {
943 r = &env->vfp.zregs[n].d[0];
944 ret = kvm_get_one_reg(cs, KVM_REG_ARM64_SVE_ZREG(n, 0), r);
945 if (ret) {
946 return ret;
947 }
948 sve_bswap64(r, r, cpu->sve_max_vq * 2);
949 }
950
951 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) {
952 r = &env->vfp.pregs[n].p[0];
953 ret = kvm_get_one_reg(cs, KVM_REG_ARM64_SVE_PREG(n, 0), r);
954 if (ret) {
955 return ret;
956 }
957 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
958 }
959
960 r = &env->vfp.pregs[FFR_PRED_NUM].p[0];
961 ret = kvm_get_one_reg(cs, KVM_REG_ARM64_SVE_FFR(0), r);
962 if (ret) {
963 return ret;
964 }
965 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
966
967 return 0;
968 }
969
970 int kvm_arch_get_registers(CPUState *cs)
971 {
972 uint64_t val;
973 unsigned int el;
974 uint32_t fpr;
975 int i, ret;
976
977 ARMCPU *cpu = ARM_CPU(cs);
978 CPUARMState *env = &cpu->env;
979
980 for (i = 0; i < 31; i++) {
981 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.regs[i]),
982 &env->xregs[i]);
983 if (ret) {
984 return ret;
985 }
986 }
987
988 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.sp), &env->sp_el[0]);
989 if (ret) {
990 return ret;
991 }
992
993 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(sp_el1), &env->sp_el[1]);
994 if (ret) {
995 return ret;
996 }
997
998 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.pstate), &val);
999 if (ret) {
1000 return ret;
1001 }
1002
1003 env->aarch64 = ((val & PSTATE_nRW) == 0);
1004 if (is_a64(env)) {
1005 pstate_write(env, val);
1006 } else {
1007 cpsr_write(env, val, 0xffffffff, CPSRWriteRaw);
1008 }
1009
1010 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
1011 * QEMU side we keep the current SP in xregs[31] as well.
1012 */
1013 aarch64_restore_sp(env, 1);
1014
1015 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.pc), &env->pc);
1016 if (ret) {
1017 return ret;
1018 }
1019
1020 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
1021 * incoming AArch64 regs received from 64-bit KVM.
1022 * We must perform this after all of the registers have been acquired from
1023 * the kernel.
1024 */
1025 if (!is_a64(env)) {
1026 aarch64_sync_64_to_32(env);
1027 }
1028
1029 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(elr_el1), &env->elr_el[1]);
1030 if (ret) {
1031 return ret;
1032 }
1033
1034 /* Fetch the SPSR registers
1035 *
1036 * KVM SPSRs 0-4 map to QEMU banks 1-5
1037 */
1038 for (i = 0; i < KVM_NR_SPSR; i++) {
1039 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(spsr[i]),
1040 &env->banked_spsr[i + 1]);
1041 if (ret) {
1042 return ret;
1043 }
1044 }
1045
1046 el = arm_current_el(env);
1047 if (el > 0 && !is_a64(env)) {
1048 i = bank_number(env->uncached_cpsr & CPSR_M);
1049 env->spsr = env->banked_spsr[i];
1050 }
1051
1052 if (cpu_isar_feature(aa64_sve, cpu)) {
1053 ret = kvm_arch_get_sve(cs);
1054 } else {
1055 ret = kvm_arch_get_fpsimd(cs);
1056 }
1057 if (ret) {
1058 return ret;
1059 }
1060
1061 ret = kvm_get_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpsr), &fpr);
1062 if (ret) {
1063 return ret;
1064 }
1065 vfp_set_fpsr(env, fpr);
1066
1067 ret = kvm_get_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpcr), &fpr);
1068 if (ret) {
1069 return ret;
1070 }
1071 vfp_set_fpcr(env, fpr);
1072
1073 ret = kvm_get_vcpu_events(cpu);
1074 if (ret) {
1075 return ret;
1076 }
1077
1078 if (!write_kvmstate_to_list(cpu)) {
1079 return -EINVAL;
1080 }
1081 /* Note that it's OK to have registers which aren't in CPUState,
1082 * so we can ignore a failure return here.
1083 */
1084 write_list_to_cpustate(cpu);
1085
1086 kvm_arm_sync_mpstate_to_qemu(cpu);
1087
1088 /* TODO: other registers */
1089 return ret;
1090 }
1091
1092 void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
1093 {
1094 ram_addr_t ram_addr;
1095 hwaddr paddr;
1096
1097 assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);
1098
1099 if (acpi_ghes_present() && addr) {
1100 ram_addr = qemu_ram_addr_from_host(addr);
1101 if (ram_addr != RAM_ADDR_INVALID &&
1102 kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
1103 kvm_hwpoison_page_add(ram_addr);
1104 /*
1105 * If this is a BUS_MCEERR_AR, we know we have been called
1106 * synchronously from the vCPU thread, so we can easily
1107 * synchronize the state and inject an error.
1108 *
1109 * TODO: we currently don't tell the guest at all about
1110 * BUS_MCEERR_AO. In that case we might either be being
1111 * called synchronously from the vCPU thread, or a bit
1112 * later from the main thread, so doing the injection of
1113 * the error would be more complicated.
1114 */
1115 if (code == BUS_MCEERR_AR) {
1116 kvm_cpu_synchronize_state(c);
1117 if (!acpi_ghes_record_errors(ACPI_HEST_SRC_ID_SEA, paddr)) {
1118 kvm_inject_arm_sea(c);
1119 } else {
1120 error_report("failed to record the error");
1121 abort();
1122 }
1123 }
1124 return;
1125 }
1126 if (code == BUS_MCEERR_AO) {
1127 error_report("Hardware memory error at addr %p for memory used by "
1128 "QEMU itself instead of guest system!", addr);
1129 }
1130 }
1131
1132 if (code == BUS_MCEERR_AR) {
1133 error_report("Hardware memory error!");
1134 exit(1);
1135 }
1136 }
1137
1138 /* C6.6.29 BRK instruction */
1139 static const uint32_t brk_insn = 0xd4200000;
1140
1141 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1142 {
1143 if (have_guest_debug) {
1144 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
1145 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk_insn, 4, 1)) {
1146 return -EINVAL;
1147 }
1148 return 0;
1149 } else {
1150 error_report("guest debug not supported on this kernel");
1151 return -EINVAL;
1152 }
1153 }
1154
1155 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1156 {
1157 static uint32_t brk;
1158
1159 if (have_guest_debug) {
1160 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk, 4, 0) ||
1161 brk != brk_insn ||
1162 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) {
1163 return -EINVAL;
1164 }
1165 return 0;
1166 } else {
1167 error_report("guest debug not supported on this kernel");
1168 return -EINVAL;
1169 }
1170 }
1171
1172 /* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
1173 *
1174 * To minimise translating between kernel and user-space the kernel
1175 * ABI just provides user-space with the full exception syndrome
1176 * register value to be decoded in QEMU.
1177 */
1178
1179 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
1180 {
1181 int hsr_ec = syn_get_ec(debug_exit->hsr);
1182 ARMCPU *cpu = ARM_CPU(cs);
1183 CPUARMState *env = &cpu->env;
1184
1185 /* Ensure PC is synchronised */
1186 kvm_cpu_synchronize_state(cs);
1187
1188 switch (hsr_ec) {
1189 case EC_SOFTWARESTEP:
1190 if (cs->singlestep_enabled) {
1191 return true;
1192 } else {
1193 /*
1194 * The kernel should have suppressed the guest's ability to
1195 * single step at this point so something has gone wrong.
1196 */
1197 error_report("%s: guest single-step while debugging unsupported"
1198 " (%"PRIx64", %"PRIx32")",
1199 __func__, env->pc, debug_exit->hsr);
1200 return false;
1201 }
1202 break;
1203 case EC_AA64_BKPT:
1204 if (kvm_find_sw_breakpoint(cs, env->pc)) {
1205 return true;
1206 }
1207 break;
1208 case EC_BREAKPOINT:
1209 if (find_hw_breakpoint(cs, env->pc)) {
1210 return true;
1211 }
1212 break;
1213 case EC_WATCHPOINT:
1214 {
1215 CPUWatchpoint *wp = find_hw_watchpoint(cs, debug_exit->far);
1216 if (wp) {
1217 cs->watchpoint_hit = wp;
1218 return true;
1219 }
1220 break;
1221 }
1222 default:
1223 error_report("%s: unhandled debug exit (%"PRIx32", %"PRIx64")",
1224 __func__, debug_exit->hsr, env->pc);
1225 }
1226
1227 /* If we are not handling the debug exception it must belong to
1228 * the guest. Let's re-use the existing TCG interrupt code to set
1229 * everything up properly.
1230 */
1231 cs->exception_index = EXCP_BKPT;
1232 env->exception.syndrome = debug_exit->hsr;
1233 env->exception.vaddress = debug_exit->far;
1234 env->exception.target_el = 1;
1235 qemu_mutex_lock_iothread();
1236 arm_cpu_do_interrupt(cs);
1237 qemu_mutex_unlock_iothread();
1238
1239 return false;
1240 }
1241
1242 #define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)
1243 #define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)
1244
1245 /*
1246 * ESR_EL1
1247 * ISS encoding
1248 * AARCH64: DFSC, bits [5:0]
1249 * AARCH32:
1250 * TTBCR.EAE == 0
1251 * FS[4] - DFSR[10]
1252 * FS[3:0] - DFSR[3:0]
1253 * TTBCR.EAE == 1
1254 * FS, bits [5:0]
1255 */
1256 #define ESR_DFSC(aarch64, lpae, v) \
1257 ((aarch64 || (lpae)) ? ((v) & 0x3F) \
1258 : (((v) >> 6) | ((v) & 0x1F)))
1259
1260 #define ESR_DFSC_EXTABT(aarch64, lpae) \
1261 ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)
1262
1263 bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
1264 {
1265 uint64_t dfsr_val;
1266
1267 if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &dfsr_val)) {
1268 ARMCPU *cpu = ARM_CPU(cs);
1269 CPUARMState *env = &cpu->env;
1270 int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64);
1271 int lpae = 0;
1272
1273 if (!aarch64_mode) {
1274 uint64_t ttbcr;
1275
1276 if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &ttbcr)) {
1277 lpae = arm_feature(env, ARM_FEATURE_LPAE)
1278 && (ttbcr & TTBCR_EAE);
1279 }
1280 }
1281 /*
1282 * The verification here is based on the DFSC bits
1283 * of the ESR_EL1 reg only
1284 */
1285 return (ESR_DFSC(aarch64_mode, lpae, dfsr_val) ==
1286 ESR_DFSC_EXTABT(aarch64_mode, lpae));
1287 }
1288 return false;
1289 }