target/arm: Create ARMVQMap
[qemu/kevin.git] / target / arm / kvm64.c
blobd16d4ea2500b69f3b01d3931ae5c1d745b59ebcb
1 /*
2 * ARM implementation of KVM hooks, 64 bit specific code
4 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems
5 * Copyright Alex Bennée 2014, Linaro
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.
12 #include "qemu/osdep.h"
13 #include <sys/ioctl.h>
14 #include <sys/ptrace.h>
16 #include <linux/elf.h>
17 #include <linux/kvm.h>
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 #include "hw/arm/virt.h"
35 static bool have_guest_debug;
38 * Although the ARM implementation of hardware assisted debugging
39 * allows for different breakpoints per-core, the current GDB
40 * interface treats them as a global pool of registers (which seems to
41 * be the case for x86, ppc and s390). As a result we store one copy
42 * of registers which is used for all active cores.
44 * Write access is serialised by virtue of the GDB protocol which
45 * updates things. Read access (i.e. when the values are copied to the
46 * vCPU) is also gated by GDB's run control.
48 * This is not unreasonable as most of the time debugging kernels you
49 * never know which core will eventually execute your function.
52 typedef struct {
53 uint64_t bcr;
54 uint64_t bvr;
55 } HWBreakpoint;
57 /* The watchpoint registers can cover more area than the requested
58 * watchpoint so we need to store the additional information
59 * somewhere. We also need to supply a CPUWatchpoint to the GDB stub
60 * when the watchpoint is hit.
62 typedef struct {
63 uint64_t wcr;
64 uint64_t wvr;
65 CPUWatchpoint details;
66 } HWWatchpoint;
68 /* Maximum and current break/watch point counts */
69 int max_hw_bps, max_hw_wps;
70 GArray *hw_breakpoints, *hw_watchpoints;
72 #define cur_hw_wps (hw_watchpoints->len)
73 #define cur_hw_bps (hw_breakpoints->len)
74 #define get_hw_bp(i) (&g_array_index(hw_breakpoints, HWBreakpoint, i))
75 #define get_hw_wp(i) (&g_array_index(hw_watchpoints, HWWatchpoint, i))
77 /**
78 * kvm_arm_init_debug() - check for guest debug capabilities
79 * @cs: CPUState
81 * kvm_check_extension returns the number of debug registers we have
82 * or 0 if we have none.
85 static void kvm_arm_init_debug(CPUState *cs)
87 have_guest_debug = kvm_check_extension(cs->kvm_state,
88 KVM_CAP_SET_GUEST_DEBUG);
90 max_hw_wps = kvm_check_extension(cs->kvm_state, KVM_CAP_GUEST_DEBUG_HW_WPS);
91 hw_watchpoints = g_array_sized_new(true, true,
92 sizeof(HWWatchpoint), max_hw_wps);
94 max_hw_bps = kvm_check_extension(cs->kvm_state, KVM_CAP_GUEST_DEBUG_HW_BPS);
95 hw_breakpoints = g_array_sized_new(true, true,
96 sizeof(HWBreakpoint), max_hw_bps);
97 return;
101 * insert_hw_breakpoint()
102 * @addr: address of breakpoint
104 * See ARM ARM D2.9.1 for details but here we are only going to create
105 * simple un-linked breakpoints (i.e. we don't chain breakpoints
106 * together to match address and context or vmid). The hardware is
107 * capable of fancier matching but that will require exposing that
108 * fanciness to GDB's interface
110 * DBGBCR<n>_EL1, Debug Breakpoint Control Registers
112 * 31 24 23 20 19 16 15 14 13 12 9 8 5 4 3 2 1 0
113 * +------+------+-------+-----+----+------+-----+------+-----+---+
114 * | RES0 | BT | LBN | SSC | HMC| RES0 | BAS | RES0 | PMC | E |
115 * +------+------+-------+-----+----+------+-----+------+-----+---+
117 * BT: Breakpoint type (0 = unlinked address match)
118 * LBN: Linked BP number (0 = unused)
119 * SSC/HMC/PMC: Security, Higher and Priv access control (Table D-12)
120 * BAS: Byte Address Select (RES1 for AArch64)
121 * E: Enable bit
123 * DBGBVR<n>_EL1, Debug Breakpoint Value Registers
125 * 63 53 52 49 48 2 1 0
126 * +------+-----------+----------+-----+
127 * | RESS | VA[52:49] | VA[48:2] | 0 0 |
128 * +------+-----------+----------+-----+
130 * Depending on the addressing mode bits the top bits of the register
131 * are a sign extension of the highest applicable VA bit. Some
132 * versions of GDB don't do it correctly so we ensure they are correct
133 * here so future PC comparisons will work properly.
136 static int insert_hw_breakpoint(target_ulong addr)
138 HWBreakpoint brk = {
139 .bcr = 0x1, /* BCR E=1, enable */
140 .bvr = sextract64(addr, 0, 53)
143 if (cur_hw_bps >= max_hw_bps) {
144 return -ENOBUFS;
147 brk.bcr = deposit32(brk.bcr, 1, 2, 0x3); /* PMC = 11 */
148 brk.bcr = deposit32(brk.bcr, 5, 4, 0xf); /* BAS = RES1 */
150 g_array_append_val(hw_breakpoints, brk);
152 return 0;
156 * delete_hw_breakpoint()
157 * @pc: address of breakpoint
159 * Delete a breakpoint and shuffle any above down
162 static int delete_hw_breakpoint(target_ulong pc)
164 int i;
165 for (i = 0; i < hw_breakpoints->len; i++) {
166 HWBreakpoint *brk = get_hw_bp(i);
167 if (brk->bvr == pc) {
168 g_array_remove_index(hw_breakpoints, i);
169 return 0;
172 return -ENOENT;
176 * insert_hw_watchpoint()
177 * @addr: address of watch point
178 * @len: size of area
179 * @type: type of watch point
181 * See ARM ARM D2.10. As with the breakpoints we can do some advanced
182 * stuff if we want to. The watch points can be linked with the break
183 * points above to make them context aware. However for simplicity
184 * currently we only deal with simple read/write watch points.
186 * D7.3.11 DBGWCR<n>_EL1, Debug Watchpoint Control Registers
188 * 31 29 28 24 23 21 20 19 16 15 14 13 12 5 4 3 2 1 0
189 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
190 * | RES0 | MASK | RES0 | WT | LBN | SSC | HMC | BAS | LSC | PAC | E |
191 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
193 * MASK: num bits addr mask (0=none,01/10=res,11=3 bits (8 bytes))
194 * WT: 0 - unlinked, 1 - linked (not currently used)
195 * LBN: Linked BP number (not currently used)
196 * SSC/HMC/PAC: Security, Higher and Priv access control (Table D2-11)
197 * BAS: Byte Address Select
198 * LSC: Load/Store control (01: load, 10: store, 11: both)
199 * E: Enable
201 * The bottom 2 bits of the value register are masked. Therefore to
202 * break on any sizes smaller than an unaligned word you need to set
203 * MASK=0, BAS=bit per byte in question. For larger regions (^2) you
204 * need to ensure you mask the address as required and set BAS=0xff
207 static int insert_hw_watchpoint(target_ulong addr,
208 target_ulong len, int type)
210 HWWatchpoint wp = {
211 .wcr = R_DBGWCR_E_MASK, /* E=1, enable */
212 .wvr = addr & (~0x7ULL),
213 .details = { .vaddr = addr, .len = len }
216 if (cur_hw_wps >= max_hw_wps) {
217 return -ENOBUFS;
221 * HMC=0 SSC=0 PAC=3 will hit EL0 or EL1, any security state,
222 * valid whether EL3 is implemented or not
224 wp.wcr = FIELD_DP64(wp.wcr, DBGWCR, PAC, 3);
226 switch (type) {
227 case GDB_WATCHPOINT_READ:
228 wp.wcr = FIELD_DP64(wp.wcr, DBGWCR, LSC, 1);
229 wp.details.flags = BP_MEM_READ;
230 break;
231 case GDB_WATCHPOINT_WRITE:
232 wp.wcr = FIELD_DP64(wp.wcr, DBGWCR, LSC, 2);
233 wp.details.flags = BP_MEM_WRITE;
234 break;
235 case GDB_WATCHPOINT_ACCESS:
236 wp.wcr = FIELD_DP64(wp.wcr, DBGWCR, LSC, 3);
237 wp.details.flags = BP_MEM_ACCESS;
238 break;
239 default:
240 g_assert_not_reached();
241 break;
243 if (len <= 8) {
244 /* we align the address and set the bits in BAS */
245 int off = addr & 0x7;
246 int bas = (1 << len) - 1;
248 wp.wcr = deposit32(wp.wcr, 5 + off, 8 - off, bas);
249 } else {
250 /* For ranges above 8 bytes we need to be a power of 2 */
251 if (is_power_of_2(len)) {
252 int bits = ctz64(len);
254 wp.wvr &= ~((1 << bits) - 1);
255 wp.wcr = FIELD_DP64(wp.wcr, DBGWCR, MASK, bits);
256 wp.wcr = FIELD_DP64(wp.wcr, DBGWCR, BAS, 0xff);
257 } else {
258 return -ENOBUFS;
262 g_array_append_val(hw_watchpoints, wp);
263 return 0;
267 static bool check_watchpoint_in_range(int i, target_ulong addr)
269 HWWatchpoint *wp = get_hw_wp(i);
270 uint64_t addr_top, addr_bottom = wp->wvr;
271 int bas = extract32(wp->wcr, 5, 8);
272 int mask = extract32(wp->wcr, 24, 4);
274 if (mask) {
275 addr_top = addr_bottom + (1 << mask);
276 } else {
277 /* BAS must be contiguous but can offset against the base
278 * address in DBGWVR */
279 addr_bottom = addr_bottom + ctz32(bas);
280 addr_top = addr_bottom + clo32(bas);
283 if (addr >= addr_bottom && addr <= addr_top) {
284 return true;
287 return false;
291 * delete_hw_watchpoint()
292 * @addr: address of breakpoint
294 * Delete a breakpoint and shuffle any above down
297 static int delete_hw_watchpoint(target_ulong addr,
298 target_ulong len, int type)
300 int i;
301 for (i = 0; i < cur_hw_wps; i++) {
302 if (check_watchpoint_in_range(i, addr)) {
303 g_array_remove_index(hw_watchpoints, i);
304 return 0;
307 return -ENOENT;
311 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
312 target_ulong len, int type)
314 switch (type) {
315 case GDB_BREAKPOINT_HW:
316 return insert_hw_breakpoint(addr);
317 break;
318 case GDB_WATCHPOINT_READ:
319 case GDB_WATCHPOINT_WRITE:
320 case GDB_WATCHPOINT_ACCESS:
321 return insert_hw_watchpoint(addr, len, type);
322 default:
323 return -ENOSYS;
327 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
328 target_ulong len, int type)
330 switch (type) {
331 case GDB_BREAKPOINT_HW:
332 return delete_hw_breakpoint(addr);
333 case GDB_WATCHPOINT_READ:
334 case GDB_WATCHPOINT_WRITE:
335 case GDB_WATCHPOINT_ACCESS:
336 return delete_hw_watchpoint(addr, len, type);
337 default:
338 return -ENOSYS;
343 void kvm_arch_remove_all_hw_breakpoints(void)
345 if (cur_hw_wps > 0) {
346 g_array_remove_range(hw_watchpoints, 0, cur_hw_wps);
348 if (cur_hw_bps > 0) {
349 g_array_remove_range(hw_breakpoints, 0, cur_hw_bps);
353 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr)
355 int i;
356 memset(ptr, 0, sizeof(struct kvm_guest_debug_arch));
358 for (i = 0; i < max_hw_wps; i++) {
359 HWWatchpoint *wp = get_hw_wp(i);
360 ptr->dbg_wcr[i] = wp->wcr;
361 ptr->dbg_wvr[i] = wp->wvr;
363 for (i = 0; i < max_hw_bps; i++) {
364 HWBreakpoint *bp = get_hw_bp(i);
365 ptr->dbg_bcr[i] = bp->bcr;
366 ptr->dbg_bvr[i] = bp->bvr;
370 bool kvm_arm_hw_debug_active(CPUState *cs)
372 return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
375 static bool find_hw_breakpoint(CPUState *cpu, target_ulong pc)
377 int i;
379 for (i = 0; i < cur_hw_bps; i++) {
380 HWBreakpoint *bp = get_hw_bp(i);
381 if (bp->bvr == pc) {
382 return true;
385 return false;
388 static CPUWatchpoint *find_hw_watchpoint(CPUState *cpu, target_ulong addr)
390 int i;
392 for (i = 0; i < cur_hw_wps; i++) {
393 if (check_watchpoint_in_range(i, addr)) {
394 return &get_hw_wp(i)->details;
397 return NULL;
400 static bool kvm_arm_set_device_attr(CPUState *cs, struct kvm_device_attr *attr,
401 const char *name)
403 int err;
405 err = kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, attr);
406 if (err != 0) {
407 error_report("%s: KVM_HAS_DEVICE_ATTR: %s", name, strerror(-err));
408 return false;
411 err = kvm_vcpu_ioctl(cs, KVM_SET_DEVICE_ATTR, attr);
412 if (err != 0) {
413 error_report("%s: KVM_SET_DEVICE_ATTR: %s", name, strerror(-err));
414 return false;
417 return true;
420 void kvm_arm_pmu_init(CPUState *cs)
422 struct kvm_device_attr attr = {
423 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
424 .attr = KVM_ARM_VCPU_PMU_V3_INIT,
427 if (!ARM_CPU(cs)->has_pmu) {
428 return;
430 if (!kvm_arm_set_device_attr(cs, &attr, "PMU")) {
431 error_report("failed to init PMU");
432 abort();
436 void kvm_arm_pmu_set_irq(CPUState *cs, int irq)
438 struct kvm_device_attr attr = {
439 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
440 .addr = (intptr_t)&irq,
441 .attr = KVM_ARM_VCPU_PMU_V3_IRQ,
444 if (!ARM_CPU(cs)->has_pmu) {
445 return;
447 if (!kvm_arm_set_device_attr(cs, &attr, "PMU")) {
448 error_report("failed to set irq for PMU");
449 abort();
453 void kvm_arm_pvtime_init(CPUState *cs, uint64_t ipa)
455 struct kvm_device_attr attr = {
456 .group = KVM_ARM_VCPU_PVTIME_CTRL,
457 .attr = KVM_ARM_VCPU_PVTIME_IPA,
458 .addr = (uint64_t)&ipa,
461 if (ARM_CPU(cs)->kvm_steal_time == ON_OFF_AUTO_OFF) {
462 return;
464 if (!kvm_arm_set_device_attr(cs, &attr, "PVTIME IPA")) {
465 error_report("failed to init PVTIME IPA");
466 abort();
470 static int read_sys_reg32(int fd, uint32_t *pret, uint64_t id)
472 uint64_t ret;
473 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)&ret };
474 int err;
476 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64);
477 err = ioctl(fd, KVM_GET_ONE_REG, &idreg);
478 if (err < 0) {
479 return -1;
481 *pret = ret;
482 return 0;
485 static int read_sys_reg64(int fd, uint64_t *pret, uint64_t id)
487 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)pret };
489 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64);
490 return ioctl(fd, KVM_GET_ONE_REG, &idreg);
493 static bool kvm_arm_pauth_supported(void)
495 return (kvm_check_extension(kvm_state, KVM_CAP_ARM_PTRAUTH_ADDRESS) &&
496 kvm_check_extension(kvm_state, KVM_CAP_ARM_PTRAUTH_GENERIC));
499 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
501 /* Identify the feature bits corresponding to the host CPU, and
502 * fill out the ARMHostCPUClass fields accordingly. To do this
503 * we have to create a scratch VM, create a single CPU inside it,
504 * and then query that CPU for the relevant ID registers.
506 int fdarray[3];
507 bool sve_supported;
508 bool pmu_supported = false;
509 uint64_t features = 0;
510 uint64_t t;
511 int err;
513 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
514 * we know these will only support creating one kind of guest CPU,
515 * which is its preferred CPU type. Fortunately these old kernels
516 * support only a very limited number of CPUs.
518 static const uint32_t cpus_to_try[] = {
519 KVM_ARM_TARGET_AEM_V8,
520 KVM_ARM_TARGET_FOUNDATION_V8,
521 KVM_ARM_TARGET_CORTEX_A57,
522 QEMU_KVM_ARM_TARGET_NONE
525 * target = -1 informs kvm_arm_create_scratch_host_vcpu()
526 * to use the preferred target
528 struct kvm_vcpu_init init = { .target = -1, };
531 * Ask for Pointer Authentication if supported. We can't play the
532 * SVE trick of synthesising the ID reg as KVM won't tell us
533 * whether we have the architected or IMPDEF version of PAuth, so
534 * we have to use the actual ID regs.
536 if (kvm_arm_pauth_supported()) {
537 init.features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS |
538 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
541 if (kvm_arm_pmu_supported()) {
542 init.features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
543 pmu_supported = true;
546 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
547 return false;
550 ahcf->target = init.target;
551 ahcf->dtb_compatible = "arm,arm-v8";
553 err = read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr0,
554 ARM64_SYS_REG(3, 0, 0, 4, 0));
555 if (unlikely(err < 0)) {
557 * Before v4.15, the kernel only exposed a limited number of system
558 * registers, not including any of the interesting AArch64 ID regs.
559 * For the most part we could leave these fields as zero with minimal
560 * effect, since this does not affect the values seen by the guest.
562 * However, it could cause problems down the line for QEMU,
563 * so provide a minimal v8.0 default.
565 * ??? Could read MIDR and use knowledge from cpu64.c.
566 * ??? Could map a page of memory into our temp guest and
567 * run the tiniest of hand-crafted kernels to extract
568 * the values seen by the guest.
569 * ??? Either of these sounds like too much effort just
570 * to work around running a modern host kernel.
572 ahcf->isar.id_aa64pfr0 = 0x00000011; /* EL1&0, AArch64 only */
573 err = 0;
574 } else {
575 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr1,
576 ARM64_SYS_REG(3, 0, 0, 4, 1));
577 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64smfr0,
578 ARM64_SYS_REG(3, 0, 0, 4, 5));
579 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr0,
580 ARM64_SYS_REG(3, 0, 0, 5, 0));
581 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr1,
582 ARM64_SYS_REG(3, 0, 0, 5, 1));
583 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar0,
584 ARM64_SYS_REG(3, 0, 0, 6, 0));
585 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar1,
586 ARM64_SYS_REG(3, 0, 0, 6, 1));
587 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr0,
588 ARM64_SYS_REG(3, 0, 0, 7, 0));
589 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr1,
590 ARM64_SYS_REG(3, 0, 0, 7, 1));
591 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr2,
592 ARM64_SYS_REG(3, 0, 0, 7, 2));
595 * Note that if AArch32 support is not present in the host,
596 * the AArch32 sysregs are present to be read, but will
597 * return UNKNOWN values. This is neither better nor worse
598 * than skipping the reads and leaving 0, as we must avoid
599 * considering the values in every case.
601 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr0,
602 ARM64_SYS_REG(3, 0, 0, 1, 0));
603 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr1,
604 ARM64_SYS_REG(3, 0, 0, 1, 1));
605 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr2,
606 ARM64_SYS_REG(3, 0, 0, 3, 4));
607 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_dfr0,
608 ARM64_SYS_REG(3, 0, 0, 1, 2));
609 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr0,
610 ARM64_SYS_REG(3, 0, 0, 1, 4));
611 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr1,
612 ARM64_SYS_REG(3, 0, 0, 1, 5));
613 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr2,
614 ARM64_SYS_REG(3, 0, 0, 1, 6));
615 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr3,
616 ARM64_SYS_REG(3, 0, 0, 1, 7));
617 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar0,
618 ARM64_SYS_REG(3, 0, 0, 2, 0));
619 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar1,
620 ARM64_SYS_REG(3, 0, 0, 2, 1));
621 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar2,
622 ARM64_SYS_REG(3, 0, 0, 2, 2));
623 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar3,
624 ARM64_SYS_REG(3, 0, 0, 2, 3));
625 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar4,
626 ARM64_SYS_REG(3, 0, 0, 2, 4));
627 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar5,
628 ARM64_SYS_REG(3, 0, 0, 2, 5));
629 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr4,
630 ARM64_SYS_REG(3, 0, 0, 2, 6));
631 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar6,
632 ARM64_SYS_REG(3, 0, 0, 2, 7));
634 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr0,
635 ARM64_SYS_REG(3, 0, 0, 3, 0));
636 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr1,
637 ARM64_SYS_REG(3, 0, 0, 3, 1));
638 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr2,
639 ARM64_SYS_REG(3, 0, 0, 3, 2));
642 * DBGDIDR is a bit complicated because the kernel doesn't
643 * provide an accessor for it in 64-bit mode, which is what this
644 * scratch VM is in, and there's no architected "64-bit sysreg
645 * which reads the same as the 32-bit register" the way there is
646 * for other ID registers. Instead we synthesize a value from the
647 * AArch64 ID_AA64DFR0, the same way the kernel code in
648 * arch/arm64/kvm/sys_regs.c:trap_dbgidr() does.
649 * We only do this if the CPU supports AArch32 at EL1.
651 if (FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL1) >= 2) {
652 int wrps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, WRPS);
653 int brps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, BRPS);
654 int ctx_cmps =
655 FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, CTX_CMPS);
656 int version = 6; /* ARMv8 debug architecture */
657 bool has_el3 =
658 !!FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL3);
659 uint32_t dbgdidr = 0;
661 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, WRPS, wrps);
662 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, BRPS, brps);
663 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, CTX_CMPS, ctx_cmps);
664 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, VERSION, version);
665 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, NSUHD_IMP, has_el3);
666 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, SE_IMP, has_el3);
667 dbgdidr |= (1 << 15); /* RES1 bit */
668 ahcf->isar.dbgdidr = dbgdidr;
671 if (pmu_supported) {
672 /* PMCR_EL0 is only accessible if the vCPU has feature PMU_V3 */
673 err |= read_sys_reg64(fdarray[2], &ahcf->isar.reset_pmcr_el0,
674 ARM64_SYS_REG(3, 3, 9, 12, 0));
678 sve_supported = ioctl(fdarray[0], KVM_CHECK_EXTENSION, KVM_CAP_ARM_SVE) > 0;
680 /* Add feature bits that can't appear until after VCPU init. */
681 if (sve_supported) {
682 t = ahcf->isar.id_aa64pfr0;
683 t = FIELD_DP64(t, ID_AA64PFR0, SVE, 1);
684 ahcf->isar.id_aa64pfr0 = t;
687 * There is a range of kernels between kernel commit 73433762fcae
688 * and f81cb2c3ad41 which have a bug where the kernel doesn't expose
689 * SYS_ID_AA64ZFR0_EL1 via the ONE_REG API unless the VM has enabled
690 * SVE support, so we only read it here, rather than together with all
691 * the other ID registers earlier.
693 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64zfr0,
694 ARM64_SYS_REG(3, 0, 0, 4, 4));
697 kvm_arm_destroy_scratch_host_vcpu(fdarray);
699 if (err < 0) {
700 return false;
704 * We can assume any KVM supporting CPU is at least a v8
705 * with VFPv4+Neon; this in turn implies most of the other
706 * feature bits.
708 features |= 1ULL << ARM_FEATURE_V8;
709 features |= 1ULL << ARM_FEATURE_NEON;
710 features |= 1ULL << ARM_FEATURE_AARCH64;
711 features |= 1ULL << ARM_FEATURE_PMU;
712 features |= 1ULL << ARM_FEATURE_GENERIC_TIMER;
714 ahcf->features = features;
716 return true;
719 void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp)
721 bool has_steal_time = kvm_arm_steal_time_supported();
723 if (cpu->kvm_steal_time == ON_OFF_AUTO_AUTO) {
724 if (!has_steal_time || !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
725 cpu->kvm_steal_time = ON_OFF_AUTO_OFF;
726 } else {
727 cpu->kvm_steal_time = ON_OFF_AUTO_ON;
729 } else if (cpu->kvm_steal_time == ON_OFF_AUTO_ON) {
730 if (!has_steal_time) {
731 error_setg(errp, "'kvm-steal-time' cannot be enabled "
732 "on this host");
733 return;
734 } else if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
736 * DEN0057A chapter 2 says "This specification only covers
737 * systems in which the Execution state of the hypervisor
738 * as well as EL1 of virtual machines is AArch64.". And,
739 * to ensure that, the smc/hvc calls are only specified as
740 * smc64/hvc64.
742 error_setg(errp, "'kvm-steal-time' cannot be enabled "
743 "for AArch32 guests");
744 return;
749 bool kvm_arm_aarch32_supported(void)
751 return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL1_32BIT);
754 bool kvm_arm_sve_supported(void)
756 return kvm_check_extension(kvm_state, KVM_CAP_ARM_SVE);
759 bool kvm_arm_steal_time_supported(void)
761 return kvm_check_extension(kvm_state, KVM_CAP_STEAL_TIME);
764 QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN != 1);
766 uint32_t kvm_arm_sve_get_vls(CPUState *cs)
768 /* Only call this function if kvm_arm_sve_supported() returns true. */
769 static uint64_t vls[KVM_ARM64_SVE_VLS_WORDS];
770 static bool probed;
771 uint32_t vq = 0;
772 int i;
775 * KVM ensures all host CPUs support the same set of vector lengths.
776 * So we only need to create the scratch VCPUs once and then cache
777 * the results.
779 if (!probed) {
780 struct kvm_vcpu_init init = {
781 .target = -1,
782 .features[0] = (1 << KVM_ARM_VCPU_SVE),
784 struct kvm_one_reg reg = {
785 .id = KVM_REG_ARM64_SVE_VLS,
786 .addr = (uint64_t)&vls[0],
788 int fdarray[3], ret;
790 probed = true;
792 if (!kvm_arm_create_scratch_host_vcpu(NULL, fdarray, &init)) {
793 error_report("failed to create scratch VCPU with SVE enabled");
794 abort();
796 ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
797 kvm_arm_destroy_scratch_host_vcpu(fdarray);
798 if (ret) {
799 error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
800 strerror(errno));
801 abort();
804 for (i = KVM_ARM64_SVE_VLS_WORDS - 1; i >= 0; --i) {
805 if (vls[i]) {
806 vq = 64 - clz64(vls[i]) + i * 64;
807 break;
810 if (vq > ARM_MAX_VQ) {
811 warn_report("KVM supports vector lengths larger than "
812 "QEMU can enable");
813 vls[0] &= MAKE_64BIT_MASK(0, ARM_MAX_VQ);
817 return vls[0];
820 static int kvm_arm_sve_set_vls(CPUState *cs)
822 ARMCPU *cpu = ARM_CPU(cs);
823 uint64_t vls[KVM_ARM64_SVE_VLS_WORDS] = { cpu->sve_vq.map };
824 struct kvm_one_reg reg = {
825 .id = KVM_REG_ARM64_SVE_VLS,
826 .addr = (uint64_t)&vls[0],
829 assert(cpu->sve_max_vq <= KVM_ARM64_SVE_VQ_MAX);
831 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
834 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
836 int kvm_arch_init_vcpu(CPUState *cs)
838 int ret;
839 uint64_t mpidr;
840 ARMCPU *cpu = ARM_CPU(cs);
841 CPUARMState *env = &cpu->env;
842 uint64_t psciver;
844 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
845 !object_dynamic_cast(OBJECT(cpu), TYPE_AARCH64_CPU)) {
846 error_report("KVM is not supported for this guest CPU type");
847 return -EINVAL;
850 qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
852 /* Determine init features for this CPU */
853 memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
854 if (cs->start_powered_off) {
855 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
857 if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
858 cpu->psci_version = QEMU_PSCI_VERSION_0_2;
859 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
861 if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
862 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
864 if (!kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) {
865 cpu->has_pmu = false;
867 if (cpu->has_pmu) {
868 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
869 } else {
870 env->features &= ~(1ULL << ARM_FEATURE_PMU);
872 if (cpu_isar_feature(aa64_sve, cpu)) {
873 assert(kvm_arm_sve_supported());
874 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_SVE;
876 if (cpu_isar_feature(aa64_pauth, cpu)) {
877 cpu->kvm_init_features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS |
878 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
881 /* Do KVM_ARM_VCPU_INIT ioctl */
882 ret = kvm_arm_vcpu_init(cs);
883 if (ret) {
884 return ret;
887 if (cpu_isar_feature(aa64_sve, cpu)) {
888 ret = kvm_arm_sve_set_vls(cs);
889 if (ret) {
890 return ret;
892 ret = kvm_arm_vcpu_finalize(cs, KVM_ARM_VCPU_SVE);
893 if (ret) {
894 return ret;
899 * KVM reports the exact PSCI version it is implementing via a
900 * special sysreg. If it is present, use its contents to determine
901 * what to report to the guest in the dtb (it is the PSCI version,
902 * in the same 15-bits major 16-bits minor format that PSCI_VERSION
903 * returns).
905 if (!kvm_get_one_reg(cs, KVM_REG_ARM_PSCI_VERSION, &psciver)) {
906 cpu->psci_version = psciver;
910 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
911 * Currently KVM has its own idea about MPIDR assignment, so we
912 * override our defaults with what we get from KVM.
914 ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
915 if (ret) {
916 return ret;
918 cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
920 kvm_arm_init_debug(cs);
922 /* Check whether user space can specify guest syndrome value */
923 kvm_arm_init_serror_injection(cs);
925 return kvm_arm_init_cpreg_list(cpu);
928 int kvm_arch_destroy_vcpu(CPUState *cs)
930 return 0;
933 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
935 /* Return true if the regidx is a register we should synchronize
936 * via the cpreg_tuples array (ie is not a core or sve reg that
937 * we sync by hand in kvm_arch_get/put_registers())
939 switch (regidx & KVM_REG_ARM_COPROC_MASK) {
940 case KVM_REG_ARM_CORE:
941 case KVM_REG_ARM64_SVE:
942 return false;
943 default:
944 return true;
948 typedef struct CPRegStateLevel {
949 uint64_t regidx;
950 int level;
951 } CPRegStateLevel;
953 /* All system registers not listed in the following table are assumed to be
954 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
955 * often, you must add it to this table with a state of either
956 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
958 static const CPRegStateLevel non_runtime_cpregs[] = {
959 { KVM_REG_ARM_TIMER_CNT, KVM_PUT_FULL_STATE },
962 int kvm_arm_cpreg_level(uint64_t regidx)
964 int i;
966 for (i = 0; i < ARRAY_SIZE(non_runtime_cpregs); i++) {
967 const CPRegStateLevel *l = &non_runtime_cpregs[i];
968 if (l->regidx == regidx) {
969 return l->level;
973 return KVM_PUT_RUNTIME_STATE;
976 /* Callers must hold the iothread mutex lock */
977 static void kvm_inject_arm_sea(CPUState *c)
979 ARMCPU *cpu = ARM_CPU(c);
980 CPUARMState *env = &cpu->env;
981 uint32_t esr;
982 bool same_el;
984 c->exception_index = EXCP_DATA_ABORT;
985 env->exception.target_el = 1;
988 * Set the DFSC to synchronous external abort and set FnV to not valid,
989 * this will tell guest the FAR_ELx is UNKNOWN for this abort.
991 same_el = arm_current_el(env) == env->exception.target_el;
992 esr = syn_data_abort_no_iss(same_el, 1, 0, 0, 0, 0, 0x10);
994 env->exception.syndrome = esr;
996 arm_cpu_do_interrupt(c);
999 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
1000 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
1002 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
1003 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
1005 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
1006 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
1008 static int kvm_arch_put_fpsimd(CPUState *cs)
1010 CPUARMState *env = &ARM_CPU(cs)->env;
1011 struct kvm_one_reg reg;
1012 int i, ret;
1014 for (i = 0; i < 32; i++) {
1015 uint64_t *q = aa64_vfp_qreg(env, i);
1016 #if HOST_BIG_ENDIAN
1017 uint64_t fp_val[2] = { q[1], q[0] };
1018 reg.addr = (uintptr_t)fp_val;
1019 #else
1020 reg.addr = (uintptr_t)q;
1021 #endif
1022 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
1023 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1024 if (ret) {
1025 return ret;
1029 return 0;
1033 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
1034 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
1035 * code the slice index to zero for now as it's unlikely we'll need more than
1036 * one slice for quite some time.
1038 static int kvm_arch_put_sve(CPUState *cs)
1040 ARMCPU *cpu = ARM_CPU(cs);
1041 CPUARMState *env = &cpu->env;
1042 uint64_t tmp[ARM_MAX_VQ * 2];
1043 uint64_t *r;
1044 struct kvm_one_reg reg;
1045 int n, ret;
1047 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) {
1048 r = sve_bswap64(tmp, &env->vfp.zregs[n].d[0], cpu->sve_max_vq * 2);
1049 reg.addr = (uintptr_t)r;
1050 reg.id = KVM_REG_ARM64_SVE_ZREG(n, 0);
1051 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1052 if (ret) {
1053 return ret;
1057 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) {
1058 r = sve_bswap64(tmp, r = &env->vfp.pregs[n].p[0],
1059 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
1060 reg.addr = (uintptr_t)r;
1061 reg.id = KVM_REG_ARM64_SVE_PREG(n, 0);
1062 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1063 if (ret) {
1064 return ret;
1068 r = sve_bswap64(tmp, &env->vfp.pregs[FFR_PRED_NUM].p[0],
1069 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
1070 reg.addr = (uintptr_t)r;
1071 reg.id = KVM_REG_ARM64_SVE_FFR(0);
1072 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1073 if (ret) {
1074 return ret;
1077 return 0;
1080 int kvm_arch_put_registers(CPUState *cs, int level)
1082 struct kvm_one_reg reg;
1083 uint64_t val;
1084 uint32_t fpr;
1085 int i, ret;
1086 unsigned int el;
1088 ARMCPU *cpu = ARM_CPU(cs);
1089 CPUARMState *env = &cpu->env;
1091 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
1092 * AArch64 registers before pushing them out to 64-bit KVM.
1094 if (!is_a64(env)) {
1095 aarch64_sync_32_to_64(env);
1098 for (i = 0; i < 31; i++) {
1099 reg.id = AARCH64_CORE_REG(regs.regs[i]);
1100 reg.addr = (uintptr_t) &env->xregs[i];
1101 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1102 if (ret) {
1103 return ret;
1107 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
1108 * QEMU side we keep the current SP in xregs[31] as well.
1110 aarch64_save_sp(env, 1);
1112 reg.id = AARCH64_CORE_REG(regs.sp);
1113 reg.addr = (uintptr_t) &env->sp_el[0];
1114 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1115 if (ret) {
1116 return ret;
1119 reg.id = AARCH64_CORE_REG(sp_el1);
1120 reg.addr = (uintptr_t) &env->sp_el[1];
1121 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1122 if (ret) {
1123 return ret;
1126 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
1127 if (is_a64(env)) {
1128 val = pstate_read(env);
1129 } else {
1130 val = cpsr_read(env);
1132 reg.id = AARCH64_CORE_REG(regs.pstate);
1133 reg.addr = (uintptr_t) &val;
1134 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1135 if (ret) {
1136 return ret;
1139 reg.id = AARCH64_CORE_REG(regs.pc);
1140 reg.addr = (uintptr_t) &env->pc;
1141 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1142 if (ret) {
1143 return ret;
1146 reg.id = AARCH64_CORE_REG(elr_el1);
1147 reg.addr = (uintptr_t) &env->elr_el[1];
1148 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1149 if (ret) {
1150 return ret;
1153 /* Saved Program State Registers
1155 * Before we restore from the banked_spsr[] array we need to
1156 * ensure that any modifications to env->spsr are correctly
1157 * reflected in the banks.
1159 el = arm_current_el(env);
1160 if (el > 0 && !is_a64(env)) {
1161 i = bank_number(env->uncached_cpsr & CPSR_M);
1162 env->banked_spsr[i] = env->spsr;
1165 /* KVM 0-4 map to QEMU banks 1-5 */
1166 for (i = 0; i < KVM_NR_SPSR; i++) {
1167 reg.id = AARCH64_CORE_REG(spsr[i]);
1168 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
1169 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1170 if (ret) {
1171 return ret;
1175 if (cpu_isar_feature(aa64_sve, cpu)) {
1176 ret = kvm_arch_put_sve(cs);
1177 } else {
1178 ret = kvm_arch_put_fpsimd(cs);
1180 if (ret) {
1181 return ret;
1184 reg.addr = (uintptr_t)(&fpr);
1185 fpr = vfp_get_fpsr(env);
1186 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
1187 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1188 if (ret) {
1189 return ret;
1192 reg.addr = (uintptr_t)(&fpr);
1193 fpr = vfp_get_fpcr(env);
1194 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
1195 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1196 if (ret) {
1197 return ret;
1200 write_cpustate_to_list(cpu, true);
1202 if (!write_list_to_kvmstate(cpu, level)) {
1203 return -EINVAL;
1207 * Setting VCPU events should be triggered after syncing the registers
1208 * to avoid overwriting potential changes made by KVM upon calling
1209 * KVM_SET_VCPU_EVENTS ioctl
1211 ret = kvm_put_vcpu_events(cpu);
1212 if (ret) {
1213 return ret;
1216 kvm_arm_sync_mpstate_to_kvm(cpu);
1218 return ret;
1221 static int kvm_arch_get_fpsimd(CPUState *cs)
1223 CPUARMState *env = &ARM_CPU(cs)->env;
1224 struct kvm_one_reg reg;
1225 int i, ret;
1227 for (i = 0; i < 32; i++) {
1228 uint64_t *q = aa64_vfp_qreg(env, i);
1229 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
1230 reg.addr = (uintptr_t)q;
1231 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1232 if (ret) {
1233 return ret;
1234 } else {
1235 #if HOST_BIG_ENDIAN
1236 uint64_t t;
1237 t = q[0], q[0] = q[1], q[1] = t;
1238 #endif
1242 return 0;
1246 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
1247 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
1248 * code the slice index to zero for now as it's unlikely we'll need more than
1249 * one slice for quite some time.
1251 static int kvm_arch_get_sve(CPUState *cs)
1253 ARMCPU *cpu = ARM_CPU(cs);
1254 CPUARMState *env = &cpu->env;
1255 struct kvm_one_reg reg;
1256 uint64_t *r;
1257 int n, ret;
1259 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) {
1260 r = &env->vfp.zregs[n].d[0];
1261 reg.addr = (uintptr_t)r;
1262 reg.id = KVM_REG_ARM64_SVE_ZREG(n, 0);
1263 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1264 if (ret) {
1265 return ret;
1267 sve_bswap64(r, r, cpu->sve_max_vq * 2);
1270 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) {
1271 r = &env->vfp.pregs[n].p[0];
1272 reg.addr = (uintptr_t)r;
1273 reg.id = KVM_REG_ARM64_SVE_PREG(n, 0);
1274 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1275 if (ret) {
1276 return ret;
1278 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
1281 r = &env->vfp.pregs[FFR_PRED_NUM].p[0];
1282 reg.addr = (uintptr_t)r;
1283 reg.id = KVM_REG_ARM64_SVE_FFR(0);
1284 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1285 if (ret) {
1286 return ret;
1288 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
1290 return 0;
1293 int kvm_arch_get_registers(CPUState *cs)
1295 struct kvm_one_reg reg;
1296 uint64_t val;
1297 unsigned int el;
1298 uint32_t fpr;
1299 int i, ret;
1301 ARMCPU *cpu = ARM_CPU(cs);
1302 CPUARMState *env = &cpu->env;
1304 for (i = 0; i < 31; i++) {
1305 reg.id = AARCH64_CORE_REG(regs.regs[i]);
1306 reg.addr = (uintptr_t) &env->xregs[i];
1307 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1308 if (ret) {
1309 return ret;
1313 reg.id = AARCH64_CORE_REG(regs.sp);
1314 reg.addr = (uintptr_t) &env->sp_el[0];
1315 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1316 if (ret) {
1317 return ret;
1320 reg.id = AARCH64_CORE_REG(sp_el1);
1321 reg.addr = (uintptr_t) &env->sp_el[1];
1322 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1323 if (ret) {
1324 return ret;
1327 reg.id = AARCH64_CORE_REG(regs.pstate);
1328 reg.addr = (uintptr_t) &val;
1329 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1330 if (ret) {
1331 return ret;
1334 env->aarch64 = ((val & PSTATE_nRW) == 0);
1335 if (is_a64(env)) {
1336 pstate_write(env, val);
1337 } else {
1338 cpsr_write(env, val, 0xffffffff, CPSRWriteRaw);
1341 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
1342 * QEMU side we keep the current SP in xregs[31] as well.
1344 aarch64_restore_sp(env, 1);
1346 reg.id = AARCH64_CORE_REG(regs.pc);
1347 reg.addr = (uintptr_t) &env->pc;
1348 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1349 if (ret) {
1350 return ret;
1353 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
1354 * incoming AArch64 regs received from 64-bit KVM.
1355 * We must perform this after all of the registers have been acquired from
1356 * the kernel.
1358 if (!is_a64(env)) {
1359 aarch64_sync_64_to_32(env);
1362 reg.id = AARCH64_CORE_REG(elr_el1);
1363 reg.addr = (uintptr_t) &env->elr_el[1];
1364 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1365 if (ret) {
1366 return ret;
1369 /* Fetch the SPSR registers
1371 * KVM SPSRs 0-4 map to QEMU banks 1-5
1373 for (i = 0; i < KVM_NR_SPSR; i++) {
1374 reg.id = AARCH64_CORE_REG(spsr[i]);
1375 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
1376 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1377 if (ret) {
1378 return ret;
1382 el = arm_current_el(env);
1383 if (el > 0 && !is_a64(env)) {
1384 i = bank_number(env->uncached_cpsr & CPSR_M);
1385 env->spsr = env->banked_spsr[i];
1388 if (cpu_isar_feature(aa64_sve, cpu)) {
1389 ret = kvm_arch_get_sve(cs);
1390 } else {
1391 ret = kvm_arch_get_fpsimd(cs);
1393 if (ret) {
1394 return ret;
1397 reg.addr = (uintptr_t)(&fpr);
1398 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
1399 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1400 if (ret) {
1401 return ret;
1403 vfp_set_fpsr(env, fpr);
1405 reg.addr = (uintptr_t)(&fpr);
1406 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
1407 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1408 if (ret) {
1409 return ret;
1411 vfp_set_fpcr(env, fpr);
1413 ret = kvm_get_vcpu_events(cpu);
1414 if (ret) {
1415 return ret;
1418 if (!write_kvmstate_to_list(cpu)) {
1419 return -EINVAL;
1421 /* Note that it's OK to have registers which aren't in CPUState,
1422 * so we can ignore a failure return here.
1424 write_list_to_cpustate(cpu);
1426 kvm_arm_sync_mpstate_to_qemu(cpu);
1428 /* TODO: other registers */
1429 return ret;
1432 void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
1434 ram_addr_t ram_addr;
1435 hwaddr paddr;
1437 assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO);
1439 if (acpi_ghes_present() && addr) {
1440 ram_addr = qemu_ram_addr_from_host(addr);
1441 if (ram_addr != RAM_ADDR_INVALID &&
1442 kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
1443 kvm_hwpoison_page_add(ram_addr);
1445 * If this is a BUS_MCEERR_AR, we know we have been called
1446 * synchronously from the vCPU thread, so we can easily
1447 * synchronize the state and inject an error.
1449 * TODO: we currently don't tell the guest at all about
1450 * BUS_MCEERR_AO. In that case we might either be being
1451 * called synchronously from the vCPU thread, or a bit
1452 * later from the main thread, so doing the injection of
1453 * the error would be more complicated.
1455 if (code == BUS_MCEERR_AR) {
1456 kvm_cpu_synchronize_state(c);
1457 if (!acpi_ghes_record_errors(ACPI_HEST_SRC_ID_SEA, paddr)) {
1458 kvm_inject_arm_sea(c);
1459 } else {
1460 error_report("failed to record the error");
1461 abort();
1464 return;
1466 if (code == BUS_MCEERR_AO) {
1467 error_report("Hardware memory error at addr %p for memory used by "
1468 "QEMU itself instead of guest system!", addr);
1472 if (code == BUS_MCEERR_AR) {
1473 error_report("Hardware memory error!");
1474 exit(1);
1478 /* C6.6.29 BRK instruction */
1479 static const uint32_t brk_insn = 0xd4200000;
1481 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1483 if (have_guest_debug) {
1484 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
1485 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk_insn, 4, 1)) {
1486 return -EINVAL;
1488 return 0;
1489 } else {
1490 error_report("guest debug not supported on this kernel");
1491 return -EINVAL;
1495 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1497 static uint32_t brk;
1499 if (have_guest_debug) {
1500 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk, 4, 0) ||
1501 brk != brk_insn ||
1502 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) {
1503 return -EINVAL;
1505 return 0;
1506 } else {
1507 error_report("guest debug not supported on this kernel");
1508 return -EINVAL;
1512 /* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
1514 * To minimise translating between kernel and user-space the kernel
1515 * ABI just provides user-space with the full exception syndrome
1516 * register value to be decoded in QEMU.
1519 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
1521 int hsr_ec = syn_get_ec(debug_exit->hsr);
1522 ARMCPU *cpu = ARM_CPU(cs);
1523 CPUARMState *env = &cpu->env;
1525 /* Ensure PC is synchronised */
1526 kvm_cpu_synchronize_state(cs);
1528 switch (hsr_ec) {
1529 case EC_SOFTWARESTEP:
1530 if (cs->singlestep_enabled) {
1531 return true;
1532 } else {
1534 * The kernel should have suppressed the guest's ability to
1535 * single step at this point so something has gone wrong.
1537 error_report("%s: guest single-step while debugging unsupported"
1538 " (%"PRIx64", %"PRIx32")",
1539 __func__, env->pc, debug_exit->hsr);
1540 return false;
1542 break;
1543 case EC_AA64_BKPT:
1544 if (kvm_find_sw_breakpoint(cs, env->pc)) {
1545 return true;
1547 break;
1548 case EC_BREAKPOINT:
1549 if (find_hw_breakpoint(cs, env->pc)) {
1550 return true;
1552 break;
1553 case EC_WATCHPOINT:
1555 CPUWatchpoint *wp = find_hw_watchpoint(cs, debug_exit->far);
1556 if (wp) {
1557 cs->watchpoint_hit = wp;
1558 return true;
1560 break;
1562 default:
1563 error_report("%s: unhandled debug exit (%"PRIx32", %"PRIx64")",
1564 __func__, debug_exit->hsr, env->pc);
1567 /* If we are not handling the debug exception it must belong to
1568 * the guest. Let's re-use the existing TCG interrupt code to set
1569 * everything up properly.
1571 cs->exception_index = EXCP_BKPT;
1572 env->exception.syndrome = debug_exit->hsr;
1573 env->exception.vaddress = debug_exit->far;
1574 env->exception.target_el = 1;
1575 qemu_mutex_lock_iothread();
1576 arm_cpu_do_interrupt(cs);
1577 qemu_mutex_unlock_iothread();
1579 return false;
1582 #define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)
1583 #define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)
1586 * ESR_EL1
1587 * ISS encoding
1588 * AARCH64: DFSC, bits [5:0]
1589 * AARCH32:
1590 * TTBCR.EAE == 0
1591 * FS[4] - DFSR[10]
1592 * FS[3:0] - DFSR[3:0]
1593 * TTBCR.EAE == 1
1594 * FS, bits [5:0]
1596 #define ESR_DFSC(aarch64, lpae, v) \
1597 ((aarch64 || (lpae)) ? ((v) & 0x3F) \
1598 : (((v) >> 6) | ((v) & 0x1F)))
1600 #define ESR_DFSC_EXTABT(aarch64, lpae) \
1601 ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)
1603 bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
1605 uint64_t dfsr_val;
1607 if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &dfsr_val)) {
1608 ARMCPU *cpu = ARM_CPU(cs);
1609 CPUARMState *env = &cpu->env;
1610 int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64);
1611 int lpae = 0;
1613 if (!aarch64_mode) {
1614 uint64_t ttbcr;
1616 if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &ttbcr)) {
1617 lpae = arm_feature(env, ARM_FEATURE_LPAE)
1618 && (ttbcr & TTBCR_EAE);
1622 * The verification here is based on the DFSC bits
1623 * of the ESR_EL1 reg only
1625 return (ESR_DFSC(aarch64_mode, lpae, dfsr_val) ==
1626 ESR_DFSC_EXTABT(aarch64_mode, lpae));
1628 return false;