target/arm: Correct definition of PMCRDP
[qemu/ar7.git] / target / arm / kvm64.c
blobe8d7cea74cbba0a41ed732651747290e1e184764
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 "qemu-common.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"
32 static bool have_guest_debug;
35 * Although the ARM implementation of hardware assisted debugging
36 * allows for different breakpoints per-core, the current GDB
37 * interface treats them as a global pool of registers (which seems to
38 * be the case for x86, ppc and s390). As a result we store one copy
39 * of registers which is used for all active cores.
41 * Write access is serialised by virtue of the GDB protocol which
42 * updates things. Read access (i.e. when the values are copied to the
43 * vCPU) is also gated by GDB's run control.
45 * This is not unreasonable as most of the time debugging kernels you
46 * never know which core will eventually execute your function.
49 typedef struct {
50 uint64_t bcr;
51 uint64_t bvr;
52 } HWBreakpoint;
54 /* The watchpoint registers can cover more area than the requested
55 * watchpoint so we need to store the additional information
56 * somewhere. We also need to supply a CPUWatchpoint to the GDB stub
57 * when the watchpoint is hit.
59 typedef struct {
60 uint64_t wcr;
61 uint64_t wvr;
62 CPUWatchpoint details;
63 } HWWatchpoint;
65 /* Maximum and current break/watch point counts */
66 int max_hw_bps, max_hw_wps;
67 GArray *hw_breakpoints, *hw_watchpoints;
69 #define cur_hw_wps (hw_watchpoints->len)
70 #define cur_hw_bps (hw_breakpoints->len)
71 #define get_hw_bp(i) (&g_array_index(hw_breakpoints, HWBreakpoint, i))
72 #define get_hw_wp(i) (&g_array_index(hw_watchpoints, HWWatchpoint, i))
74 /**
75 * kvm_arm_init_debug() - check for guest debug capabilities
76 * @cs: CPUState
78 * kvm_check_extension returns the number of debug registers we have
79 * or 0 if we have none.
82 static void kvm_arm_init_debug(CPUState *cs)
84 have_guest_debug = kvm_check_extension(cs->kvm_state,
85 KVM_CAP_SET_GUEST_DEBUG);
87 max_hw_wps = kvm_check_extension(cs->kvm_state, KVM_CAP_GUEST_DEBUG_HW_WPS);
88 hw_watchpoints = g_array_sized_new(true, true,
89 sizeof(HWWatchpoint), max_hw_wps);
91 max_hw_bps = kvm_check_extension(cs->kvm_state, KVM_CAP_GUEST_DEBUG_HW_BPS);
92 hw_breakpoints = g_array_sized_new(true, true,
93 sizeof(HWBreakpoint), max_hw_bps);
94 return;
97 /**
98 * insert_hw_breakpoint()
99 * @addr: address of breakpoint
101 * See ARM ARM D2.9.1 for details but here we are only going to create
102 * simple un-linked breakpoints (i.e. we don't chain breakpoints
103 * together to match address and context or vmid). The hardware is
104 * capable of fancier matching but that will require exposing that
105 * fanciness to GDB's interface
107 * DBGBCR<n>_EL1, Debug Breakpoint Control Registers
109 * 31 24 23 20 19 16 15 14 13 12 9 8 5 4 3 2 1 0
110 * +------+------+-------+-----+----+------+-----+------+-----+---+
111 * | RES0 | BT | LBN | SSC | HMC| RES0 | BAS | RES0 | PMC | E |
112 * +------+------+-------+-----+----+------+-----+------+-----+---+
114 * BT: Breakpoint type (0 = unlinked address match)
115 * LBN: Linked BP number (0 = unused)
116 * SSC/HMC/PMC: Security, Higher and Priv access control (Table D-12)
117 * BAS: Byte Address Select (RES1 for AArch64)
118 * E: Enable bit
120 * DBGBVR<n>_EL1, Debug Breakpoint Value Registers
122 * 63 53 52 49 48 2 1 0
123 * +------+-----------+----------+-----+
124 * | RESS | VA[52:49] | VA[48:2] | 0 0 |
125 * +------+-----------+----------+-----+
127 * Depending on the addressing mode bits the top bits of the register
128 * are a sign extension of the highest applicable VA bit. Some
129 * versions of GDB don't do it correctly so we ensure they are correct
130 * here so future PC comparisons will work properly.
133 static int insert_hw_breakpoint(target_ulong addr)
135 HWBreakpoint brk = {
136 .bcr = 0x1, /* BCR E=1, enable */
137 .bvr = sextract64(addr, 0, 53)
140 if (cur_hw_bps >= max_hw_bps) {
141 return -ENOBUFS;
144 brk.bcr = deposit32(brk.bcr, 1, 2, 0x3); /* PMC = 11 */
145 brk.bcr = deposit32(brk.bcr, 5, 4, 0xf); /* BAS = RES1 */
147 g_array_append_val(hw_breakpoints, brk);
149 return 0;
153 * delete_hw_breakpoint()
154 * @pc: address of breakpoint
156 * Delete a breakpoint and shuffle any above down
159 static int delete_hw_breakpoint(target_ulong pc)
161 int i;
162 for (i = 0; i < hw_breakpoints->len; i++) {
163 HWBreakpoint *brk = get_hw_bp(i);
164 if (brk->bvr == pc) {
165 g_array_remove_index(hw_breakpoints, i);
166 return 0;
169 return -ENOENT;
173 * insert_hw_watchpoint()
174 * @addr: address of watch point
175 * @len: size of area
176 * @type: type of watch point
178 * See ARM ARM D2.10. As with the breakpoints we can do some advanced
179 * stuff if we want to. The watch points can be linked with the break
180 * points above to make them context aware. However for simplicity
181 * currently we only deal with simple read/write watch points.
183 * D7.3.11 DBGWCR<n>_EL1, Debug Watchpoint Control Registers
185 * 31 29 28 24 23 21 20 19 16 15 14 13 12 5 4 3 2 1 0
186 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
187 * | RES0 | MASK | RES0 | WT | LBN | SSC | HMC | BAS | LSC | PAC | E |
188 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
190 * MASK: num bits addr mask (0=none,01/10=res,11=3 bits (8 bytes))
191 * WT: 0 - unlinked, 1 - linked (not currently used)
192 * LBN: Linked BP number (not currently used)
193 * SSC/HMC/PAC: Security, Higher and Priv access control (Table D2-11)
194 * BAS: Byte Address Select
195 * LSC: Load/Store control (01: load, 10: store, 11: both)
196 * E: Enable
198 * The bottom 2 bits of the value register are masked. Therefore to
199 * break on any sizes smaller than an unaligned word you need to set
200 * MASK=0, BAS=bit per byte in question. For larger regions (^2) you
201 * need to ensure you mask the address as required and set BAS=0xff
204 static int insert_hw_watchpoint(target_ulong addr,
205 target_ulong len, int type)
207 HWWatchpoint wp = {
208 .wcr = 1, /* E=1, enable */
209 .wvr = addr & (~0x7ULL),
210 .details = { .vaddr = addr, .len = len }
213 if (cur_hw_wps >= max_hw_wps) {
214 return -ENOBUFS;
218 * HMC=0 SSC=0 PAC=3 will hit EL0 or EL1, any security state,
219 * valid whether EL3 is implemented or not
221 wp.wcr = deposit32(wp.wcr, 1, 2, 3);
223 switch (type) {
224 case GDB_WATCHPOINT_READ:
225 wp.wcr = deposit32(wp.wcr, 3, 2, 1);
226 wp.details.flags = BP_MEM_READ;
227 break;
228 case GDB_WATCHPOINT_WRITE:
229 wp.wcr = deposit32(wp.wcr, 3, 2, 2);
230 wp.details.flags = BP_MEM_WRITE;
231 break;
232 case GDB_WATCHPOINT_ACCESS:
233 wp.wcr = deposit32(wp.wcr, 3, 2, 3);
234 wp.details.flags = BP_MEM_ACCESS;
235 break;
236 default:
237 g_assert_not_reached();
238 break;
240 if (len <= 8) {
241 /* we align the address and set the bits in BAS */
242 int off = addr & 0x7;
243 int bas = (1 << len) - 1;
245 wp.wcr = deposit32(wp.wcr, 5 + off, 8 - off, bas);
246 } else {
247 /* For ranges above 8 bytes we need to be a power of 2 */
248 if (is_power_of_2(len)) {
249 int bits = ctz64(len);
251 wp.wvr &= ~((1 << bits) - 1);
252 wp.wcr = deposit32(wp.wcr, 24, 4, bits);
253 wp.wcr = deposit32(wp.wcr, 5, 8, 0xff);
254 } else {
255 return -ENOBUFS;
259 g_array_append_val(hw_watchpoints, wp);
260 return 0;
264 static bool check_watchpoint_in_range(int i, target_ulong addr)
266 HWWatchpoint *wp = get_hw_wp(i);
267 uint64_t addr_top, addr_bottom = wp->wvr;
268 int bas = extract32(wp->wcr, 5, 8);
269 int mask = extract32(wp->wcr, 24, 4);
271 if (mask) {
272 addr_top = addr_bottom + (1 << mask);
273 } else {
274 /* BAS must be contiguous but can offset against the base
275 * address in DBGWVR */
276 addr_bottom = addr_bottom + ctz32(bas);
277 addr_top = addr_bottom + clo32(bas);
280 if (addr >= addr_bottom && addr <= addr_top) {
281 return true;
284 return false;
288 * delete_hw_watchpoint()
289 * @addr: address of breakpoint
291 * Delete a breakpoint and shuffle any above down
294 static int delete_hw_watchpoint(target_ulong addr,
295 target_ulong len, int type)
297 int i;
298 for (i = 0; i < cur_hw_wps; i++) {
299 if (check_watchpoint_in_range(i, addr)) {
300 g_array_remove_index(hw_watchpoints, i);
301 return 0;
304 return -ENOENT;
308 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
309 target_ulong len, int type)
311 switch (type) {
312 case GDB_BREAKPOINT_HW:
313 return insert_hw_breakpoint(addr);
314 break;
315 case GDB_WATCHPOINT_READ:
316 case GDB_WATCHPOINT_WRITE:
317 case GDB_WATCHPOINT_ACCESS:
318 return insert_hw_watchpoint(addr, len, type);
319 default:
320 return -ENOSYS;
324 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
325 target_ulong len, int type)
327 switch (type) {
328 case GDB_BREAKPOINT_HW:
329 return delete_hw_breakpoint(addr);
330 break;
331 case GDB_WATCHPOINT_READ:
332 case GDB_WATCHPOINT_WRITE:
333 case GDB_WATCHPOINT_ACCESS:
334 return delete_hw_watchpoint(addr, len, type);
335 default:
336 return -ENOSYS;
341 void kvm_arch_remove_all_hw_breakpoints(void)
343 if (cur_hw_wps > 0) {
344 g_array_remove_range(hw_watchpoints, 0, cur_hw_wps);
346 if (cur_hw_bps > 0) {
347 g_array_remove_range(hw_breakpoints, 0, cur_hw_bps);
351 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr)
353 int i;
354 memset(ptr, 0, sizeof(struct kvm_guest_debug_arch));
356 for (i = 0; i < max_hw_wps; i++) {
357 HWWatchpoint *wp = get_hw_wp(i);
358 ptr->dbg_wcr[i] = wp->wcr;
359 ptr->dbg_wvr[i] = wp->wvr;
361 for (i = 0; i < max_hw_bps; i++) {
362 HWBreakpoint *bp = get_hw_bp(i);
363 ptr->dbg_bcr[i] = bp->bcr;
364 ptr->dbg_bvr[i] = bp->bvr;
368 bool kvm_arm_hw_debug_active(CPUState *cs)
370 return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
373 static bool find_hw_breakpoint(CPUState *cpu, target_ulong pc)
375 int i;
377 for (i = 0; i < cur_hw_bps; i++) {
378 HWBreakpoint *bp = get_hw_bp(i);
379 if (bp->bvr == pc) {
380 return true;
383 return false;
386 static CPUWatchpoint *find_hw_watchpoint(CPUState *cpu, target_ulong addr)
388 int i;
390 for (i = 0; i < cur_hw_wps; i++) {
391 if (check_watchpoint_in_range(i, addr)) {
392 return &get_hw_wp(i)->details;
395 return NULL;
398 static bool kvm_arm_pmu_set_attr(CPUState *cs, struct kvm_device_attr *attr)
400 int err;
402 err = kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, attr);
403 if (err != 0) {
404 error_report("PMU: KVM_HAS_DEVICE_ATTR: %s", strerror(-err));
405 return false;
408 err = kvm_vcpu_ioctl(cs, KVM_SET_DEVICE_ATTR, attr);
409 if (err != 0) {
410 error_report("PMU: KVM_SET_DEVICE_ATTR: %s", strerror(-err));
411 return false;
414 return true;
417 void kvm_arm_pmu_init(CPUState *cs)
419 struct kvm_device_attr attr = {
420 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
421 .attr = KVM_ARM_VCPU_PMU_V3_INIT,
424 if (!ARM_CPU(cs)->has_pmu) {
425 return;
427 if (!kvm_arm_pmu_set_attr(cs, &attr)) {
428 error_report("failed to init PMU");
429 abort();
433 void kvm_arm_pmu_set_irq(CPUState *cs, int irq)
435 struct kvm_device_attr attr = {
436 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
437 .addr = (intptr_t)&irq,
438 .attr = KVM_ARM_VCPU_PMU_V3_IRQ,
441 if (!ARM_CPU(cs)->has_pmu) {
442 return;
444 if (!kvm_arm_pmu_set_attr(cs, &attr)) {
445 error_report("failed to set irq for PMU");
446 abort();
450 static inline void set_feature(uint64_t *features, int feature)
452 *features |= 1ULL << feature;
455 static inline void unset_feature(uint64_t *features, int feature)
457 *features &= ~(1ULL << feature);
460 static int read_sys_reg32(int fd, uint32_t *pret, uint64_t id)
462 uint64_t ret;
463 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)&ret };
464 int err;
466 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64);
467 err = ioctl(fd, KVM_GET_ONE_REG, &idreg);
468 if (err < 0) {
469 return -1;
471 *pret = ret;
472 return 0;
475 static int read_sys_reg64(int fd, uint64_t *pret, uint64_t id)
477 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)pret };
479 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64);
480 return ioctl(fd, KVM_GET_ONE_REG, &idreg);
483 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
485 /* Identify the feature bits corresponding to the host CPU, and
486 * fill out the ARMHostCPUClass fields accordingly. To do this
487 * we have to create a scratch VM, create a single CPU inside it,
488 * and then query that CPU for the relevant ID registers.
490 int fdarray[3];
491 bool sve_supported;
492 uint64_t features = 0;
493 uint64_t t;
494 int err;
496 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
497 * we know these will only support creating one kind of guest CPU,
498 * which is its preferred CPU type. Fortunately these old kernels
499 * support only a very limited number of CPUs.
501 static const uint32_t cpus_to_try[] = {
502 KVM_ARM_TARGET_AEM_V8,
503 KVM_ARM_TARGET_FOUNDATION_V8,
504 KVM_ARM_TARGET_CORTEX_A57,
505 QEMU_KVM_ARM_TARGET_NONE
508 * target = -1 informs kvm_arm_create_scratch_host_vcpu()
509 * to use the preferred target
511 struct kvm_vcpu_init init = { .target = -1, };
513 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
514 return false;
517 ahcf->target = init.target;
518 ahcf->dtb_compatible = "arm,arm-v8";
520 err = read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr0,
521 ARM64_SYS_REG(3, 0, 0, 4, 0));
522 if (unlikely(err < 0)) {
524 * Before v4.15, the kernel only exposed a limited number of system
525 * registers, not including any of the interesting AArch64 ID regs.
526 * For the most part we could leave these fields as zero with minimal
527 * effect, since this does not affect the values seen by the guest.
529 * However, it could cause problems down the line for QEMU,
530 * so provide a minimal v8.0 default.
532 * ??? Could read MIDR and use knowledge from cpu64.c.
533 * ??? Could map a page of memory into our temp guest and
534 * run the tiniest of hand-crafted kernels to extract
535 * the values seen by the guest.
536 * ??? Either of these sounds like too much effort just
537 * to work around running a modern host kernel.
539 ahcf->isar.id_aa64pfr0 = 0x00000011; /* EL1&0, AArch64 only */
540 err = 0;
541 } else {
542 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr1,
543 ARM64_SYS_REG(3, 0, 0, 4, 1));
544 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr0,
545 ARM64_SYS_REG(3, 0, 0, 5, 0));
546 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr1,
547 ARM64_SYS_REG(3, 0, 0, 5, 1));
548 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar0,
549 ARM64_SYS_REG(3, 0, 0, 6, 0));
550 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar1,
551 ARM64_SYS_REG(3, 0, 0, 6, 1));
552 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr0,
553 ARM64_SYS_REG(3, 0, 0, 7, 0));
554 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr1,
555 ARM64_SYS_REG(3, 0, 0, 7, 1));
556 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr2,
557 ARM64_SYS_REG(3, 0, 0, 7, 2));
560 * Note that if AArch32 support is not present in the host,
561 * the AArch32 sysregs are present to be read, but will
562 * return UNKNOWN values. This is neither better nor worse
563 * than skipping the reads and leaving 0, as we must avoid
564 * considering the values in every case.
566 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_dfr0,
567 ARM64_SYS_REG(3, 0, 0, 1, 2));
568 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar0,
569 ARM64_SYS_REG(3, 0, 0, 2, 0));
570 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar1,
571 ARM64_SYS_REG(3, 0, 0, 2, 1));
572 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar2,
573 ARM64_SYS_REG(3, 0, 0, 2, 2));
574 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar3,
575 ARM64_SYS_REG(3, 0, 0, 2, 3));
576 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar4,
577 ARM64_SYS_REG(3, 0, 0, 2, 4));
578 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar5,
579 ARM64_SYS_REG(3, 0, 0, 2, 5));
580 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar6,
581 ARM64_SYS_REG(3, 0, 0, 2, 7));
583 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr0,
584 ARM64_SYS_REG(3, 0, 0, 3, 0));
585 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr1,
586 ARM64_SYS_REG(3, 0, 0, 3, 1));
587 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr2,
588 ARM64_SYS_REG(3, 0, 0, 3, 2));
591 * DBGDIDR is a bit complicated because the kernel doesn't
592 * provide an accessor for it in 64-bit mode, which is what this
593 * scratch VM is in, and there's no architected "64-bit sysreg
594 * which reads the same as the 32-bit register" the way there is
595 * for other ID registers. Instead we synthesize a value from the
596 * AArch64 ID_AA64DFR0, the same way the kernel code in
597 * arch/arm64/kvm/sys_regs.c:trap_dbgidr() does.
598 * We only do this if the CPU supports AArch32 at EL1.
600 if (FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL1) >= 2) {
601 int wrps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, WRPS);
602 int brps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, BRPS);
603 int ctx_cmps =
604 FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, CTX_CMPS);
605 int version = 6; /* ARMv8 debug architecture */
606 bool has_el3 =
607 !!FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL3);
608 uint32_t dbgdidr = 0;
610 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, WRPS, wrps);
611 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, BRPS, brps);
612 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, CTX_CMPS, ctx_cmps);
613 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, VERSION, version);
614 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, NSUHD_IMP, has_el3);
615 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, SE_IMP, has_el3);
616 dbgdidr |= (1 << 15); /* RES1 bit */
617 ahcf->isar.dbgdidr = dbgdidr;
621 sve_supported = ioctl(fdarray[0], KVM_CHECK_EXTENSION, KVM_CAP_ARM_SVE) > 0;
623 kvm_arm_destroy_scratch_host_vcpu(fdarray);
625 if (err < 0) {
626 return false;
629 /* Add feature bits that can't appear until after VCPU init. */
630 if (sve_supported) {
631 t = ahcf->isar.id_aa64pfr0;
632 t = FIELD_DP64(t, ID_AA64PFR0, SVE, 1);
633 ahcf->isar.id_aa64pfr0 = t;
637 * We can assume any KVM supporting CPU is at least a v8
638 * with VFPv4+Neon; this in turn implies most of the other
639 * feature bits.
641 set_feature(&features, ARM_FEATURE_V8);
642 set_feature(&features, ARM_FEATURE_VFP4);
643 set_feature(&features, ARM_FEATURE_NEON);
644 set_feature(&features, ARM_FEATURE_AARCH64);
645 set_feature(&features, ARM_FEATURE_PMU);
646 set_feature(&features, ARM_FEATURE_GENERIC_TIMER);
648 ahcf->features = features;
650 return true;
653 bool kvm_arm_aarch32_supported(CPUState *cpu)
655 KVMState *s = KVM_STATE(current_accel());
657 return kvm_check_extension(s, KVM_CAP_ARM_EL1_32BIT);
660 bool kvm_arm_sve_supported(CPUState *cpu)
662 KVMState *s = KVM_STATE(current_accel());
664 return kvm_check_extension(s, KVM_CAP_ARM_SVE);
667 QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN != 1);
669 void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map)
671 /* Only call this function if kvm_arm_sve_supported() returns true. */
672 static uint64_t vls[KVM_ARM64_SVE_VLS_WORDS];
673 static bool probed;
674 uint32_t vq = 0;
675 int i, j;
677 bitmap_clear(map, 0, ARM_MAX_VQ);
680 * KVM ensures all host CPUs support the same set of vector lengths.
681 * So we only need to create the scratch VCPUs once and then cache
682 * the results.
684 if (!probed) {
685 struct kvm_vcpu_init init = {
686 .target = -1,
687 .features[0] = (1 << KVM_ARM_VCPU_SVE),
689 struct kvm_one_reg reg = {
690 .id = KVM_REG_ARM64_SVE_VLS,
691 .addr = (uint64_t)&vls[0],
693 int fdarray[3], ret;
695 probed = true;
697 if (!kvm_arm_create_scratch_host_vcpu(NULL, fdarray, &init)) {
698 error_report("failed to create scratch VCPU with SVE enabled");
699 abort();
701 ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
702 kvm_arm_destroy_scratch_host_vcpu(fdarray);
703 if (ret) {
704 error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
705 strerror(errno));
706 abort();
709 for (i = KVM_ARM64_SVE_VLS_WORDS - 1; i >= 0; --i) {
710 if (vls[i]) {
711 vq = 64 - clz64(vls[i]) + i * 64;
712 break;
715 if (vq > ARM_MAX_VQ) {
716 warn_report("KVM supports vector lengths larger than "
717 "QEMU can enable");
721 for (i = 0; i < KVM_ARM64_SVE_VLS_WORDS; ++i) {
722 if (!vls[i]) {
723 continue;
725 for (j = 1; j <= 64; ++j) {
726 vq = j + i * 64;
727 if (vq > ARM_MAX_VQ) {
728 return;
730 if (vls[i] & (1UL << (j - 1))) {
731 set_bit(vq - 1, map);
737 static int kvm_arm_sve_set_vls(CPUState *cs)
739 uint64_t vls[KVM_ARM64_SVE_VLS_WORDS] = {0};
740 struct kvm_one_reg reg = {
741 .id = KVM_REG_ARM64_SVE_VLS,
742 .addr = (uint64_t)&vls[0],
744 ARMCPU *cpu = ARM_CPU(cs);
745 uint32_t vq;
746 int i, j;
748 assert(cpu->sve_max_vq <= KVM_ARM64_SVE_VQ_MAX);
750 for (vq = 1; vq <= cpu->sve_max_vq; ++vq) {
751 if (test_bit(vq - 1, cpu->sve_vq_map)) {
752 i = (vq - 1) / 64;
753 j = (vq - 1) % 64;
754 vls[i] |= 1UL << j;
758 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
761 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
763 int kvm_arch_init_vcpu(CPUState *cs)
765 int ret;
766 uint64_t mpidr;
767 ARMCPU *cpu = ARM_CPU(cs);
768 CPUARMState *env = &cpu->env;
770 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
771 !object_dynamic_cast(OBJECT(cpu), TYPE_AARCH64_CPU)) {
772 error_report("KVM is not supported for this guest CPU type");
773 return -EINVAL;
776 qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
778 /* Determine init features for this CPU */
779 memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
780 if (cpu->start_powered_off) {
781 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
783 if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
784 cpu->psci_version = 2;
785 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
787 if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
788 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
790 if (!kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) {
791 cpu->has_pmu = false;
793 if (cpu->has_pmu) {
794 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
795 } else {
796 unset_feature(&env->features, ARM_FEATURE_PMU);
798 if (cpu_isar_feature(aa64_sve, cpu)) {
799 assert(kvm_arm_sve_supported(cs));
800 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_SVE;
803 /* Do KVM_ARM_VCPU_INIT ioctl */
804 ret = kvm_arm_vcpu_init(cs);
805 if (ret) {
806 return ret;
809 if (cpu_isar_feature(aa64_sve, cpu)) {
810 ret = kvm_arm_sve_set_vls(cs);
811 if (ret) {
812 return ret;
814 ret = kvm_arm_vcpu_finalize(cs, KVM_ARM_VCPU_SVE);
815 if (ret) {
816 return ret;
821 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
822 * Currently KVM has its own idea about MPIDR assignment, so we
823 * override our defaults with what we get from KVM.
825 ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
826 if (ret) {
827 return ret;
829 cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
831 kvm_arm_init_debug(cs);
833 /* Check whether user space can specify guest syndrome value */
834 kvm_arm_init_serror_injection(cs);
836 return kvm_arm_init_cpreg_list(cpu);
839 int kvm_arch_destroy_vcpu(CPUState *cs)
841 return 0;
844 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
846 /* Return true if the regidx is a register we should synchronize
847 * via the cpreg_tuples array (ie is not a core or sve reg that
848 * we sync by hand in kvm_arch_get/put_registers())
850 switch (regidx & KVM_REG_ARM_COPROC_MASK) {
851 case KVM_REG_ARM_CORE:
852 case KVM_REG_ARM64_SVE:
853 return false;
854 default:
855 return true;
859 typedef struct CPRegStateLevel {
860 uint64_t regidx;
861 int level;
862 } CPRegStateLevel;
864 /* All system registers not listed in the following table are assumed to be
865 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
866 * often, you must add it to this table with a state of either
867 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
869 static const CPRegStateLevel non_runtime_cpregs[] = {
870 { KVM_REG_ARM_TIMER_CNT, KVM_PUT_FULL_STATE },
873 int kvm_arm_cpreg_level(uint64_t regidx)
875 int i;
877 for (i = 0; i < ARRAY_SIZE(non_runtime_cpregs); i++) {
878 const CPRegStateLevel *l = &non_runtime_cpregs[i];
879 if (l->regidx == regidx) {
880 return l->level;
884 return KVM_PUT_RUNTIME_STATE;
887 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
888 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
890 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
891 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
893 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
894 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
896 static int kvm_arch_put_fpsimd(CPUState *cs)
898 CPUARMState *env = &ARM_CPU(cs)->env;
899 struct kvm_one_reg reg;
900 int i, ret;
902 for (i = 0; i < 32; i++) {
903 uint64_t *q = aa64_vfp_qreg(env, i);
904 #ifdef HOST_WORDS_BIGENDIAN
905 uint64_t fp_val[2] = { q[1], q[0] };
906 reg.addr = (uintptr_t)fp_val;
907 #else
908 reg.addr = (uintptr_t)q;
909 #endif
910 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
911 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
912 if (ret) {
913 return ret;
917 return 0;
921 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
922 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
923 * code the slice index to zero for now as it's unlikely we'll need more than
924 * one slice for quite some time.
926 static int kvm_arch_put_sve(CPUState *cs)
928 ARMCPU *cpu = ARM_CPU(cs);
929 CPUARMState *env = &cpu->env;
930 uint64_t tmp[ARM_MAX_VQ * 2];
931 uint64_t *r;
932 struct kvm_one_reg reg;
933 int n, ret;
935 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) {
936 r = sve_bswap64(tmp, &env->vfp.zregs[n].d[0], cpu->sve_max_vq * 2);
937 reg.addr = (uintptr_t)r;
938 reg.id = KVM_REG_ARM64_SVE_ZREG(n, 0);
939 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
940 if (ret) {
941 return ret;
945 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) {
946 r = sve_bswap64(tmp, r = &env->vfp.pregs[n].p[0],
947 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
948 reg.addr = (uintptr_t)r;
949 reg.id = KVM_REG_ARM64_SVE_PREG(n, 0);
950 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
951 if (ret) {
952 return ret;
956 r = sve_bswap64(tmp, &env->vfp.pregs[FFR_PRED_NUM].p[0],
957 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
958 reg.addr = (uintptr_t)r;
959 reg.id = KVM_REG_ARM64_SVE_FFR(0);
960 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
961 if (ret) {
962 return ret;
965 return 0;
968 int kvm_arch_put_registers(CPUState *cs, int level)
970 struct kvm_one_reg reg;
971 uint64_t val;
972 uint32_t fpr;
973 int i, ret;
974 unsigned int el;
976 ARMCPU *cpu = ARM_CPU(cs);
977 CPUARMState *env = &cpu->env;
979 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
980 * AArch64 registers before pushing them out to 64-bit KVM.
982 if (!is_a64(env)) {
983 aarch64_sync_32_to_64(env);
986 for (i = 0; i < 31; i++) {
987 reg.id = AARCH64_CORE_REG(regs.regs[i]);
988 reg.addr = (uintptr_t) &env->xregs[i];
989 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
990 if (ret) {
991 return ret;
995 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
996 * QEMU side we keep the current SP in xregs[31] as well.
998 aarch64_save_sp(env, 1);
1000 reg.id = AARCH64_CORE_REG(regs.sp);
1001 reg.addr = (uintptr_t) &env->sp_el[0];
1002 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1003 if (ret) {
1004 return ret;
1007 reg.id = AARCH64_CORE_REG(sp_el1);
1008 reg.addr = (uintptr_t) &env->sp_el[1];
1009 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1010 if (ret) {
1011 return ret;
1014 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
1015 if (is_a64(env)) {
1016 val = pstate_read(env);
1017 } else {
1018 val = cpsr_read(env);
1020 reg.id = AARCH64_CORE_REG(regs.pstate);
1021 reg.addr = (uintptr_t) &val;
1022 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1023 if (ret) {
1024 return ret;
1027 reg.id = AARCH64_CORE_REG(regs.pc);
1028 reg.addr = (uintptr_t) &env->pc;
1029 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1030 if (ret) {
1031 return ret;
1034 reg.id = AARCH64_CORE_REG(elr_el1);
1035 reg.addr = (uintptr_t) &env->elr_el[1];
1036 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1037 if (ret) {
1038 return ret;
1041 /* Saved Program State Registers
1043 * Before we restore from the banked_spsr[] array we need to
1044 * ensure that any modifications to env->spsr are correctly
1045 * reflected in the banks.
1047 el = arm_current_el(env);
1048 if (el > 0 && !is_a64(env)) {
1049 i = bank_number(env->uncached_cpsr & CPSR_M);
1050 env->banked_spsr[i] = env->spsr;
1053 /* KVM 0-4 map to QEMU banks 1-5 */
1054 for (i = 0; i < KVM_NR_SPSR; i++) {
1055 reg.id = AARCH64_CORE_REG(spsr[i]);
1056 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
1057 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1058 if (ret) {
1059 return ret;
1063 if (cpu_isar_feature(aa64_sve, cpu)) {
1064 ret = kvm_arch_put_sve(cs);
1065 } else {
1066 ret = kvm_arch_put_fpsimd(cs);
1068 if (ret) {
1069 return ret;
1072 reg.addr = (uintptr_t)(&fpr);
1073 fpr = vfp_get_fpsr(env);
1074 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
1075 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1076 if (ret) {
1077 return ret;
1080 reg.addr = (uintptr_t)(&fpr);
1081 fpr = vfp_get_fpcr(env);
1082 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
1083 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1084 if (ret) {
1085 return ret;
1088 ret = kvm_put_vcpu_events(cpu);
1089 if (ret) {
1090 return ret;
1093 write_cpustate_to_list(cpu, true);
1095 if (!write_list_to_kvmstate(cpu, level)) {
1096 return -EINVAL;
1099 kvm_arm_sync_mpstate_to_kvm(cpu);
1101 return ret;
1104 static int kvm_arch_get_fpsimd(CPUState *cs)
1106 CPUARMState *env = &ARM_CPU(cs)->env;
1107 struct kvm_one_reg reg;
1108 int i, ret;
1110 for (i = 0; i < 32; i++) {
1111 uint64_t *q = aa64_vfp_qreg(env, i);
1112 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
1113 reg.addr = (uintptr_t)q;
1114 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1115 if (ret) {
1116 return ret;
1117 } else {
1118 #ifdef HOST_WORDS_BIGENDIAN
1119 uint64_t t;
1120 t = q[0], q[0] = q[1], q[1] = t;
1121 #endif
1125 return 0;
1129 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
1130 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
1131 * code the slice index to zero for now as it's unlikely we'll need more than
1132 * one slice for quite some time.
1134 static int kvm_arch_get_sve(CPUState *cs)
1136 ARMCPU *cpu = ARM_CPU(cs);
1137 CPUARMState *env = &cpu->env;
1138 struct kvm_one_reg reg;
1139 uint64_t *r;
1140 int n, ret;
1142 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) {
1143 r = &env->vfp.zregs[n].d[0];
1144 reg.addr = (uintptr_t)r;
1145 reg.id = KVM_REG_ARM64_SVE_ZREG(n, 0);
1146 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1147 if (ret) {
1148 return ret;
1150 sve_bswap64(r, r, cpu->sve_max_vq * 2);
1153 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) {
1154 r = &env->vfp.pregs[n].p[0];
1155 reg.addr = (uintptr_t)r;
1156 reg.id = KVM_REG_ARM64_SVE_PREG(n, 0);
1157 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1158 if (ret) {
1159 return ret;
1161 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
1164 r = &env->vfp.pregs[FFR_PRED_NUM].p[0];
1165 reg.addr = (uintptr_t)r;
1166 reg.id = KVM_REG_ARM64_SVE_FFR(0);
1167 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1168 if (ret) {
1169 return ret;
1171 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8));
1173 return 0;
1176 int kvm_arch_get_registers(CPUState *cs)
1178 struct kvm_one_reg reg;
1179 uint64_t val;
1180 unsigned int el;
1181 uint32_t fpr;
1182 int i, ret;
1184 ARMCPU *cpu = ARM_CPU(cs);
1185 CPUARMState *env = &cpu->env;
1187 for (i = 0; i < 31; i++) {
1188 reg.id = AARCH64_CORE_REG(regs.regs[i]);
1189 reg.addr = (uintptr_t) &env->xregs[i];
1190 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1191 if (ret) {
1192 return ret;
1196 reg.id = AARCH64_CORE_REG(regs.sp);
1197 reg.addr = (uintptr_t) &env->sp_el[0];
1198 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1199 if (ret) {
1200 return ret;
1203 reg.id = AARCH64_CORE_REG(sp_el1);
1204 reg.addr = (uintptr_t) &env->sp_el[1];
1205 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1206 if (ret) {
1207 return ret;
1210 reg.id = AARCH64_CORE_REG(regs.pstate);
1211 reg.addr = (uintptr_t) &val;
1212 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1213 if (ret) {
1214 return ret;
1217 env->aarch64 = ((val & PSTATE_nRW) == 0);
1218 if (is_a64(env)) {
1219 pstate_write(env, val);
1220 } else {
1221 cpsr_write(env, val, 0xffffffff, CPSRWriteRaw);
1224 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
1225 * QEMU side we keep the current SP in xregs[31] as well.
1227 aarch64_restore_sp(env, 1);
1229 reg.id = AARCH64_CORE_REG(regs.pc);
1230 reg.addr = (uintptr_t) &env->pc;
1231 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1232 if (ret) {
1233 return ret;
1236 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
1237 * incoming AArch64 regs received from 64-bit KVM.
1238 * We must perform this after all of the registers have been acquired from
1239 * the kernel.
1241 if (!is_a64(env)) {
1242 aarch64_sync_64_to_32(env);
1245 reg.id = AARCH64_CORE_REG(elr_el1);
1246 reg.addr = (uintptr_t) &env->elr_el[1];
1247 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1248 if (ret) {
1249 return ret;
1252 /* Fetch the SPSR registers
1254 * KVM SPSRs 0-4 map to QEMU banks 1-5
1256 for (i = 0; i < KVM_NR_SPSR; i++) {
1257 reg.id = AARCH64_CORE_REG(spsr[i]);
1258 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
1259 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1260 if (ret) {
1261 return ret;
1265 el = arm_current_el(env);
1266 if (el > 0 && !is_a64(env)) {
1267 i = bank_number(env->uncached_cpsr & CPSR_M);
1268 env->spsr = env->banked_spsr[i];
1271 if (cpu_isar_feature(aa64_sve, cpu)) {
1272 ret = kvm_arch_get_sve(cs);
1273 } else {
1274 ret = kvm_arch_get_fpsimd(cs);
1276 if (ret) {
1277 return ret;
1280 reg.addr = (uintptr_t)(&fpr);
1281 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
1282 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1283 if (ret) {
1284 return ret;
1286 vfp_set_fpsr(env, fpr);
1288 reg.addr = (uintptr_t)(&fpr);
1289 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
1290 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
1291 if (ret) {
1292 return ret;
1294 vfp_set_fpcr(env, fpr);
1296 ret = kvm_get_vcpu_events(cpu);
1297 if (ret) {
1298 return ret;
1301 if (!write_kvmstate_to_list(cpu)) {
1302 return -EINVAL;
1304 /* Note that it's OK to have registers which aren't in CPUState,
1305 * so we can ignore a failure return here.
1307 write_list_to_cpustate(cpu);
1309 kvm_arm_sync_mpstate_to_qemu(cpu);
1311 /* TODO: other registers */
1312 return ret;
1315 /* C6.6.29 BRK instruction */
1316 static const uint32_t brk_insn = 0xd4200000;
1318 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1320 if (have_guest_debug) {
1321 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
1322 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk_insn, 4, 1)) {
1323 return -EINVAL;
1325 return 0;
1326 } else {
1327 error_report("guest debug not supported on this kernel");
1328 return -EINVAL;
1332 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1334 static uint32_t brk;
1336 if (have_guest_debug) {
1337 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk, 4, 0) ||
1338 brk != brk_insn ||
1339 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) {
1340 return -EINVAL;
1342 return 0;
1343 } else {
1344 error_report("guest debug not supported on this kernel");
1345 return -EINVAL;
1349 /* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
1351 * To minimise translating between kernel and user-space the kernel
1352 * ABI just provides user-space with the full exception syndrome
1353 * register value to be decoded in QEMU.
1356 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
1358 int hsr_ec = syn_get_ec(debug_exit->hsr);
1359 ARMCPU *cpu = ARM_CPU(cs);
1360 CPUClass *cc = CPU_GET_CLASS(cs);
1361 CPUARMState *env = &cpu->env;
1363 /* Ensure PC is synchronised */
1364 kvm_cpu_synchronize_state(cs);
1366 switch (hsr_ec) {
1367 case EC_SOFTWARESTEP:
1368 if (cs->singlestep_enabled) {
1369 return true;
1370 } else {
1372 * The kernel should have suppressed the guest's ability to
1373 * single step at this point so something has gone wrong.
1375 error_report("%s: guest single-step while debugging unsupported"
1376 " (%"PRIx64", %"PRIx32")",
1377 __func__, env->pc, debug_exit->hsr);
1378 return false;
1380 break;
1381 case EC_AA64_BKPT:
1382 if (kvm_find_sw_breakpoint(cs, env->pc)) {
1383 return true;
1385 break;
1386 case EC_BREAKPOINT:
1387 if (find_hw_breakpoint(cs, env->pc)) {
1388 return true;
1390 break;
1391 case EC_WATCHPOINT:
1393 CPUWatchpoint *wp = find_hw_watchpoint(cs, debug_exit->far);
1394 if (wp) {
1395 cs->watchpoint_hit = wp;
1396 return true;
1398 break;
1400 default:
1401 error_report("%s: unhandled debug exit (%"PRIx32", %"PRIx64")",
1402 __func__, debug_exit->hsr, env->pc);
1405 /* If we are not handling the debug exception it must belong to
1406 * the guest. Let's re-use the existing TCG interrupt code to set
1407 * everything up properly.
1409 cs->exception_index = EXCP_BKPT;
1410 env->exception.syndrome = debug_exit->hsr;
1411 env->exception.vaddress = debug_exit->far;
1412 env->exception.target_el = 1;
1413 qemu_mutex_lock_iothread();
1414 cc->do_interrupt(cs);
1415 qemu_mutex_unlock_iothread();
1417 return false;