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"
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"
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.
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.
65 CPUWatchpoint details
;
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))
78 * kvm_arm_init_debug() - check for guest debug capabilities
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
);
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)
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
)
139 .bcr
= 0x1, /* BCR E=1, enable */
140 .bvr
= sextract64(addr
, 0, 53)
143 if (cur_hw_bps
>= max_hw_bps
) {
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
);
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
)
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
);
176 * insert_hw_watchpoint()
177 * @addr: address of watch point
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)
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
)
211 .wcr
= 1, /* E=1, enable */
212 .wvr
= addr
& (~0x7ULL
),
213 .details
= { .vaddr
= addr
, .len
= len
}
216 if (cur_hw_wps
>= max_hw_wps
) {
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
= deposit32(wp
.wcr
, 1, 2, 3);
227 case GDB_WATCHPOINT_READ
:
228 wp
.wcr
= deposit32(wp
.wcr
, 3, 2, 1);
229 wp
.details
.flags
= BP_MEM_READ
;
231 case GDB_WATCHPOINT_WRITE
:
232 wp
.wcr
= deposit32(wp
.wcr
, 3, 2, 2);
233 wp
.details
.flags
= BP_MEM_WRITE
;
235 case GDB_WATCHPOINT_ACCESS
:
236 wp
.wcr
= deposit32(wp
.wcr
, 3, 2, 3);
237 wp
.details
.flags
= BP_MEM_ACCESS
;
240 g_assert_not_reached();
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
);
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
= deposit32(wp
.wcr
, 24, 4, bits
);
256 wp
.wcr
= deposit32(wp
.wcr
, 5, 8, 0xff);
262 g_array_append_val(hw_watchpoints
, wp
);
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);
275 addr_top
= addr_bottom
+ (1 << mask
);
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
) {
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
)
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
);
311 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
312 target_ulong len
, int type
)
315 case GDB_BREAKPOINT_HW
:
316 return insert_hw_breakpoint(addr
);
318 case GDB_WATCHPOINT_READ
:
319 case GDB_WATCHPOINT_WRITE
:
320 case GDB_WATCHPOINT_ACCESS
:
321 return insert_hw_watchpoint(addr
, len
, type
);
327 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
328 target_ulong len
, int 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
);
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
)
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
)
379 for (i
= 0; i
< cur_hw_bps
; i
++) {
380 HWBreakpoint
*bp
= get_hw_bp(i
);
388 static CPUWatchpoint
*find_hw_watchpoint(CPUState
*cpu
, target_ulong addr
)
392 for (i
= 0; i
< cur_hw_wps
; i
++) {
393 if (check_watchpoint_in_range(i
, addr
)) {
394 return &get_hw_wp(i
)->details
;
400 static bool kvm_arm_pmu_set_attr(CPUState
*cs
, struct kvm_device_attr
*attr
)
404 err
= kvm_vcpu_ioctl(cs
, KVM_HAS_DEVICE_ATTR
, attr
);
406 error_report("PMU: KVM_HAS_DEVICE_ATTR: %s", strerror(-err
));
410 err
= kvm_vcpu_ioctl(cs
, KVM_SET_DEVICE_ATTR
, attr
);
412 error_report("PMU: KVM_SET_DEVICE_ATTR: %s", strerror(-err
));
419 void kvm_arm_pmu_init(CPUState
*cs
)
421 struct kvm_device_attr attr
= {
422 .group
= KVM_ARM_VCPU_PMU_V3_CTRL
,
423 .attr
= KVM_ARM_VCPU_PMU_V3_INIT
,
426 if (!ARM_CPU(cs
)->has_pmu
) {
429 if (!kvm_arm_pmu_set_attr(cs
, &attr
)) {
430 error_report("failed to init PMU");
435 void kvm_arm_pmu_set_irq(CPUState
*cs
, int irq
)
437 struct kvm_device_attr attr
= {
438 .group
= KVM_ARM_VCPU_PMU_V3_CTRL
,
439 .addr
= (intptr_t)&irq
,
440 .attr
= KVM_ARM_VCPU_PMU_V3_IRQ
,
443 if (!ARM_CPU(cs
)->has_pmu
) {
446 if (!kvm_arm_pmu_set_attr(cs
, &attr
)) {
447 error_report("failed to set irq for PMU");
452 static int read_sys_reg32(int fd
, uint32_t *pret
, uint64_t id
)
455 struct kvm_one_reg idreg
= { .id
= id
, .addr
= (uintptr_t)&ret
};
458 assert((id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
);
459 err
= ioctl(fd
, KVM_GET_ONE_REG
, &idreg
);
467 static int read_sys_reg64(int fd
, uint64_t *pret
, uint64_t id
)
469 struct kvm_one_reg idreg
= { .id
= id
, .addr
= (uintptr_t)pret
};
471 assert((id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
);
472 return ioctl(fd
, KVM_GET_ONE_REG
, &idreg
);
475 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures
*ahcf
)
477 /* Identify the feature bits corresponding to the host CPU, and
478 * fill out the ARMHostCPUClass fields accordingly. To do this
479 * we have to create a scratch VM, create a single CPU inside it,
480 * and then query that CPU for the relevant ID registers.
484 uint64_t features
= 0;
488 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
489 * we know these will only support creating one kind of guest CPU,
490 * which is its preferred CPU type. Fortunately these old kernels
491 * support only a very limited number of CPUs.
493 static const uint32_t cpus_to_try
[] = {
494 KVM_ARM_TARGET_AEM_V8
,
495 KVM_ARM_TARGET_FOUNDATION_V8
,
496 KVM_ARM_TARGET_CORTEX_A57
,
497 QEMU_KVM_ARM_TARGET_NONE
500 * target = -1 informs kvm_arm_create_scratch_host_vcpu()
501 * to use the preferred target
503 struct kvm_vcpu_init init
= { .target
= -1, };
505 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try
, fdarray
, &init
)) {
509 ahcf
->target
= init
.target
;
510 ahcf
->dtb_compatible
= "arm,arm-v8";
512 err
= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64pfr0
,
513 ARM64_SYS_REG(3, 0, 0, 4, 0));
514 if (unlikely(err
< 0)) {
516 * Before v4.15, the kernel only exposed a limited number of system
517 * registers, not including any of the interesting AArch64 ID regs.
518 * For the most part we could leave these fields as zero with minimal
519 * effect, since this does not affect the values seen by the guest.
521 * However, it could cause problems down the line for QEMU,
522 * so provide a minimal v8.0 default.
524 * ??? Could read MIDR and use knowledge from cpu64.c.
525 * ??? Could map a page of memory into our temp guest and
526 * run the tiniest of hand-crafted kernels to extract
527 * the values seen by the guest.
528 * ??? Either of these sounds like too much effort just
529 * to work around running a modern host kernel.
531 ahcf
->isar
.id_aa64pfr0
= 0x00000011; /* EL1&0, AArch64 only */
534 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64pfr1
,
535 ARM64_SYS_REG(3, 0, 0, 4, 1));
536 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64dfr0
,
537 ARM64_SYS_REG(3, 0, 0, 5, 0));
538 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64dfr1
,
539 ARM64_SYS_REG(3, 0, 0, 5, 1));
540 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar0
,
541 ARM64_SYS_REG(3, 0, 0, 6, 0));
542 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar1
,
543 ARM64_SYS_REG(3, 0, 0, 6, 1));
544 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr0
,
545 ARM64_SYS_REG(3, 0, 0, 7, 0));
546 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr1
,
547 ARM64_SYS_REG(3, 0, 0, 7, 1));
548 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr2
,
549 ARM64_SYS_REG(3, 0, 0, 7, 2));
552 * Note that if AArch32 support is not present in the host,
553 * the AArch32 sysregs are present to be read, but will
554 * return UNKNOWN values. This is neither better nor worse
555 * than skipping the reads and leaving 0, as we must avoid
556 * considering the values in every case.
558 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_pfr0
,
559 ARM64_SYS_REG(3, 0, 0, 1, 0));
560 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_pfr1
,
561 ARM64_SYS_REG(3, 0, 0, 1, 1));
562 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_dfr0
,
563 ARM64_SYS_REG(3, 0, 0, 1, 2));
564 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr0
,
565 ARM64_SYS_REG(3, 0, 0, 1, 4));
566 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr1
,
567 ARM64_SYS_REG(3, 0, 0, 1, 5));
568 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr2
,
569 ARM64_SYS_REG(3, 0, 0, 1, 6));
570 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr3
,
571 ARM64_SYS_REG(3, 0, 0, 1, 7));
572 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar0
,
573 ARM64_SYS_REG(3, 0, 0, 2, 0));
574 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar1
,
575 ARM64_SYS_REG(3, 0, 0, 2, 1));
576 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar2
,
577 ARM64_SYS_REG(3, 0, 0, 2, 2));
578 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar3
,
579 ARM64_SYS_REG(3, 0, 0, 2, 3));
580 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar4
,
581 ARM64_SYS_REG(3, 0, 0, 2, 4));
582 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar5
,
583 ARM64_SYS_REG(3, 0, 0, 2, 5));
584 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr4
,
585 ARM64_SYS_REG(3, 0, 0, 2, 6));
586 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar6
,
587 ARM64_SYS_REG(3, 0, 0, 2, 7));
589 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr0
,
590 ARM64_SYS_REG(3, 0, 0, 3, 0));
591 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr1
,
592 ARM64_SYS_REG(3, 0, 0, 3, 1));
593 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr2
,
594 ARM64_SYS_REG(3, 0, 0, 3, 2));
597 * DBGDIDR is a bit complicated because the kernel doesn't
598 * provide an accessor for it in 64-bit mode, which is what this
599 * scratch VM is in, and there's no architected "64-bit sysreg
600 * which reads the same as the 32-bit register" the way there is
601 * for other ID registers. Instead we synthesize a value from the
602 * AArch64 ID_AA64DFR0, the same way the kernel code in
603 * arch/arm64/kvm/sys_regs.c:trap_dbgidr() does.
604 * We only do this if the CPU supports AArch32 at EL1.
606 if (FIELD_EX32(ahcf
->isar
.id_aa64pfr0
, ID_AA64PFR0
, EL1
) >= 2) {
607 int wrps
= FIELD_EX64(ahcf
->isar
.id_aa64dfr0
, ID_AA64DFR0
, WRPS
);
608 int brps
= FIELD_EX64(ahcf
->isar
.id_aa64dfr0
, ID_AA64DFR0
, BRPS
);
610 FIELD_EX64(ahcf
->isar
.id_aa64dfr0
, ID_AA64DFR0
, CTX_CMPS
);
611 int version
= 6; /* ARMv8 debug architecture */
613 !!FIELD_EX32(ahcf
->isar
.id_aa64pfr0
, ID_AA64PFR0
, EL3
);
614 uint32_t dbgdidr
= 0;
616 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, WRPS
, wrps
);
617 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, BRPS
, brps
);
618 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, CTX_CMPS
, ctx_cmps
);
619 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, VERSION
, version
);
620 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, NSUHD_IMP
, has_el3
);
621 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, SE_IMP
, has_el3
);
622 dbgdidr
|= (1 << 15); /* RES1 bit */
623 ahcf
->isar
.dbgdidr
= dbgdidr
;
627 sve_supported
= ioctl(fdarray
[0], KVM_CHECK_EXTENSION
, KVM_CAP_ARM_SVE
) > 0;
629 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
635 /* Add feature bits that can't appear until after VCPU init. */
637 t
= ahcf
->isar
.id_aa64pfr0
;
638 t
= FIELD_DP64(t
, ID_AA64PFR0
, SVE
, 1);
639 ahcf
->isar
.id_aa64pfr0
= t
;
643 * We can assume any KVM supporting CPU is at least a v8
644 * with VFPv4+Neon; this in turn implies most of the other
647 features
|= 1ULL << ARM_FEATURE_V8
;
648 features
|= 1ULL << ARM_FEATURE_NEON
;
649 features
|= 1ULL << ARM_FEATURE_AARCH64
;
650 features
|= 1ULL << ARM_FEATURE_PMU
;
651 features
|= 1ULL << ARM_FEATURE_GENERIC_TIMER
;
653 ahcf
->features
= features
;
658 bool kvm_arm_aarch32_supported(void)
660 return kvm_check_extension(kvm_state
, KVM_CAP_ARM_EL1_32BIT
);
663 bool kvm_arm_sve_supported(void)
665 return kvm_check_extension(kvm_state
, KVM_CAP_ARM_SVE
);
668 QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN
!= 1);
670 void kvm_arm_sve_get_vls(CPUState
*cs
, unsigned long *map
)
672 /* Only call this function if kvm_arm_sve_supported() returns true. */
673 static uint64_t vls
[KVM_ARM64_SVE_VLS_WORDS
];
678 bitmap_clear(map
, 0, ARM_MAX_VQ
);
681 * KVM ensures all host CPUs support the same set of vector lengths.
682 * So we only need to create the scratch VCPUs once and then cache
686 struct kvm_vcpu_init init
= {
688 .features
[0] = (1 << KVM_ARM_VCPU_SVE
),
690 struct kvm_one_reg reg
= {
691 .id
= KVM_REG_ARM64_SVE_VLS
,
692 .addr
= (uint64_t)&vls
[0],
698 if (!kvm_arm_create_scratch_host_vcpu(NULL
, fdarray
, &init
)) {
699 error_report("failed to create scratch VCPU with SVE enabled");
702 ret
= ioctl(fdarray
[2], KVM_GET_ONE_REG
, ®
);
703 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
705 error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
710 for (i
= KVM_ARM64_SVE_VLS_WORDS
- 1; i
>= 0; --i
) {
712 vq
= 64 - clz64(vls
[i
]) + i
* 64;
716 if (vq
> ARM_MAX_VQ
) {
717 warn_report("KVM supports vector lengths larger than "
722 for (i
= 0; i
< KVM_ARM64_SVE_VLS_WORDS
; ++i
) {
726 for (j
= 1; j
<= 64; ++j
) {
728 if (vq
> ARM_MAX_VQ
) {
731 if (vls
[i
] & (1UL << (j
- 1))) {
732 set_bit(vq
- 1, map
);
738 static int kvm_arm_sve_set_vls(CPUState
*cs
)
740 uint64_t vls
[KVM_ARM64_SVE_VLS_WORDS
] = {0};
741 struct kvm_one_reg reg
= {
742 .id
= KVM_REG_ARM64_SVE_VLS
,
743 .addr
= (uint64_t)&vls
[0],
745 ARMCPU
*cpu
= ARM_CPU(cs
);
749 assert(cpu
->sve_max_vq
<= KVM_ARM64_SVE_VQ_MAX
);
751 for (vq
= 1; vq
<= cpu
->sve_max_vq
; ++vq
) {
752 if (test_bit(vq
- 1, cpu
->sve_vq_map
)) {
759 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
762 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
764 int kvm_arch_init_vcpu(CPUState
*cs
)
768 ARMCPU
*cpu
= ARM_CPU(cs
);
769 CPUARMState
*env
= &cpu
->env
;
771 if (cpu
->kvm_target
== QEMU_KVM_ARM_TARGET_NONE
||
772 !object_dynamic_cast(OBJECT(cpu
), TYPE_AARCH64_CPU
)) {
773 error_report("KVM is not supported for this guest CPU type");
777 qemu_add_vm_change_state_handler(kvm_arm_vm_state_change
, cs
);
779 /* Determine init features for this CPU */
780 memset(cpu
->kvm_init_features
, 0, sizeof(cpu
->kvm_init_features
));
781 if (cs
->start_powered_off
) {
782 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_POWER_OFF
;
784 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PSCI_0_2
)) {
785 cpu
->psci_version
= 2;
786 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2
;
788 if (!arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
789 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT
;
791 if (!kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PMU_V3
)) {
792 cpu
->has_pmu
= false;
795 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PMU_V3
;
797 env
->features
&= ~(1ULL << ARM_FEATURE_PMU
);
799 if (cpu_isar_feature(aa64_sve
, cpu
)) {
800 assert(kvm_arm_sve_supported());
801 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_SVE
;
804 /* Do KVM_ARM_VCPU_INIT ioctl */
805 ret
= kvm_arm_vcpu_init(cs
);
810 if (cpu_isar_feature(aa64_sve
, cpu
)) {
811 ret
= kvm_arm_sve_set_vls(cs
);
815 ret
= kvm_arm_vcpu_finalize(cs
, KVM_ARM_VCPU_SVE
);
822 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
823 * Currently KVM has its own idea about MPIDR assignment, so we
824 * override our defaults with what we get from KVM.
826 ret
= kvm_get_one_reg(cs
, ARM64_SYS_REG(ARM_CPU_ID_MPIDR
), &mpidr
);
830 cpu
->mp_affinity
= mpidr
& ARM64_AFFINITY_MASK
;
832 kvm_arm_init_debug(cs
);
834 /* Check whether user space can specify guest syndrome value */
835 kvm_arm_init_serror_injection(cs
);
837 return kvm_arm_init_cpreg_list(cpu
);
840 int kvm_arch_destroy_vcpu(CPUState
*cs
)
845 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx
)
847 /* Return true if the regidx is a register we should synchronize
848 * via the cpreg_tuples array (ie is not a core or sve reg that
849 * we sync by hand in kvm_arch_get/put_registers())
851 switch (regidx
& KVM_REG_ARM_COPROC_MASK
) {
852 case KVM_REG_ARM_CORE
:
853 case KVM_REG_ARM64_SVE
:
860 typedef struct CPRegStateLevel
{
865 /* All system registers not listed in the following table are assumed to be
866 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
867 * often, you must add it to this table with a state of either
868 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
870 static const CPRegStateLevel non_runtime_cpregs
[] = {
871 { KVM_REG_ARM_TIMER_CNT
, KVM_PUT_FULL_STATE
},
874 int kvm_arm_cpreg_level(uint64_t regidx
)
878 for (i
= 0; i
< ARRAY_SIZE(non_runtime_cpregs
); i
++) {
879 const CPRegStateLevel
*l
= &non_runtime_cpregs
[i
];
880 if (l
->regidx
== regidx
) {
885 return KVM_PUT_RUNTIME_STATE
;
888 /* Callers must hold the iothread mutex lock */
889 static void kvm_inject_arm_sea(CPUState
*c
)
891 ARMCPU
*cpu
= ARM_CPU(c
);
892 CPUARMState
*env
= &cpu
->env
;
893 CPUClass
*cc
= CPU_GET_CLASS(c
);
897 c
->exception_index
= EXCP_DATA_ABORT
;
898 env
->exception
.target_el
= 1;
901 * Set the DFSC to synchronous external abort and set FnV to not valid,
902 * this will tell guest the FAR_ELx is UNKNOWN for this abort.
904 same_el
= arm_current_el(env
) == env
->exception
.target_el
;
905 esr
= syn_data_abort_no_iss(same_el
, 1, 0, 0, 0, 0, 0x10);
907 env
->exception
.syndrome
= esr
;
912 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
913 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
915 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
916 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
918 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
919 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
921 static int kvm_arch_put_fpsimd(CPUState
*cs
)
923 CPUARMState
*env
= &ARM_CPU(cs
)->env
;
924 struct kvm_one_reg reg
;
927 for (i
= 0; i
< 32; i
++) {
928 uint64_t *q
= aa64_vfp_qreg(env
, i
);
929 #ifdef HOST_WORDS_BIGENDIAN
930 uint64_t fp_val
[2] = { q
[1], q
[0] };
931 reg
.addr
= (uintptr_t)fp_val
;
933 reg
.addr
= (uintptr_t)q
;
935 reg
.id
= AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]);
936 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
946 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
947 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
948 * code the slice index to zero for now as it's unlikely we'll need more than
949 * one slice for quite some time.
951 static int kvm_arch_put_sve(CPUState
*cs
)
953 ARMCPU
*cpu
= ARM_CPU(cs
);
954 CPUARMState
*env
= &cpu
->env
;
955 uint64_t tmp
[ARM_MAX_VQ
* 2];
957 struct kvm_one_reg reg
;
960 for (n
= 0; n
< KVM_ARM64_SVE_NUM_ZREGS
; ++n
) {
961 r
= sve_bswap64(tmp
, &env
->vfp
.zregs
[n
].d
[0], cpu
->sve_max_vq
* 2);
962 reg
.addr
= (uintptr_t)r
;
963 reg
.id
= KVM_REG_ARM64_SVE_ZREG(n
, 0);
964 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
970 for (n
= 0; n
< KVM_ARM64_SVE_NUM_PREGS
; ++n
) {
971 r
= sve_bswap64(tmp
, r
= &env
->vfp
.pregs
[n
].p
[0],
972 DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
973 reg
.addr
= (uintptr_t)r
;
974 reg
.id
= KVM_REG_ARM64_SVE_PREG(n
, 0);
975 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
981 r
= sve_bswap64(tmp
, &env
->vfp
.pregs
[FFR_PRED_NUM
].p
[0],
982 DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
983 reg
.addr
= (uintptr_t)r
;
984 reg
.id
= KVM_REG_ARM64_SVE_FFR(0);
985 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
993 int kvm_arch_put_registers(CPUState
*cs
, int level
)
995 struct kvm_one_reg reg
;
1001 ARMCPU
*cpu
= ARM_CPU(cs
);
1002 CPUARMState
*env
= &cpu
->env
;
1004 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
1005 * AArch64 registers before pushing them out to 64-bit KVM.
1008 aarch64_sync_32_to_64(env
);
1011 for (i
= 0; i
< 31; i
++) {
1012 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
1013 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
1014 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1020 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
1021 * QEMU side we keep the current SP in xregs[31] as well.
1023 aarch64_save_sp(env
, 1);
1025 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
1026 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
1027 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1032 reg
.id
= AARCH64_CORE_REG(sp_el1
);
1033 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
1034 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1039 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
1041 val
= pstate_read(env
);
1043 val
= cpsr_read(env
);
1045 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
1046 reg
.addr
= (uintptr_t) &val
;
1047 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1052 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
1053 reg
.addr
= (uintptr_t) &env
->pc
;
1054 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1059 reg
.id
= AARCH64_CORE_REG(elr_el1
);
1060 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
1061 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1066 /* Saved Program State Registers
1068 * Before we restore from the banked_spsr[] array we need to
1069 * ensure that any modifications to env->spsr are correctly
1070 * reflected in the banks.
1072 el
= arm_current_el(env
);
1073 if (el
> 0 && !is_a64(env
)) {
1074 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
1075 env
->banked_spsr
[i
] = env
->spsr
;
1078 /* KVM 0-4 map to QEMU banks 1-5 */
1079 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
1080 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
1081 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
+ 1];
1082 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1088 if (cpu_isar_feature(aa64_sve
, cpu
)) {
1089 ret
= kvm_arch_put_sve(cs
);
1091 ret
= kvm_arch_put_fpsimd(cs
);
1097 reg
.addr
= (uintptr_t)(&fpr
);
1098 fpr
= vfp_get_fpsr(env
);
1099 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
);
1100 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1105 reg
.addr
= (uintptr_t)(&fpr
);
1106 fpr
= vfp_get_fpcr(env
);
1107 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
);
1108 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1113 write_cpustate_to_list(cpu
, true);
1115 if (!write_list_to_kvmstate(cpu
, level
)) {
1120 * Setting VCPU events should be triggered after syncing the registers
1121 * to avoid overwriting potential changes made by KVM upon calling
1122 * KVM_SET_VCPU_EVENTS ioctl
1124 ret
= kvm_put_vcpu_events(cpu
);
1129 kvm_arm_sync_mpstate_to_kvm(cpu
);
1134 static int kvm_arch_get_fpsimd(CPUState
*cs
)
1136 CPUARMState
*env
= &ARM_CPU(cs
)->env
;
1137 struct kvm_one_reg reg
;
1140 for (i
= 0; i
< 32; i
++) {
1141 uint64_t *q
= aa64_vfp_qreg(env
, i
);
1142 reg
.id
= AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]);
1143 reg
.addr
= (uintptr_t)q
;
1144 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1148 #ifdef HOST_WORDS_BIGENDIAN
1150 t
= q
[0], q
[0] = q
[1], q
[1] = t
;
1159 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
1160 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
1161 * code the slice index to zero for now as it's unlikely we'll need more than
1162 * one slice for quite some time.
1164 static int kvm_arch_get_sve(CPUState
*cs
)
1166 ARMCPU
*cpu
= ARM_CPU(cs
);
1167 CPUARMState
*env
= &cpu
->env
;
1168 struct kvm_one_reg reg
;
1172 for (n
= 0; n
< KVM_ARM64_SVE_NUM_ZREGS
; ++n
) {
1173 r
= &env
->vfp
.zregs
[n
].d
[0];
1174 reg
.addr
= (uintptr_t)r
;
1175 reg
.id
= KVM_REG_ARM64_SVE_ZREG(n
, 0);
1176 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1180 sve_bswap64(r
, r
, cpu
->sve_max_vq
* 2);
1183 for (n
= 0; n
< KVM_ARM64_SVE_NUM_PREGS
; ++n
) {
1184 r
= &env
->vfp
.pregs
[n
].p
[0];
1185 reg
.addr
= (uintptr_t)r
;
1186 reg
.id
= KVM_REG_ARM64_SVE_PREG(n
, 0);
1187 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1191 sve_bswap64(r
, r
, DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
1194 r
= &env
->vfp
.pregs
[FFR_PRED_NUM
].p
[0];
1195 reg
.addr
= (uintptr_t)r
;
1196 reg
.id
= KVM_REG_ARM64_SVE_FFR(0);
1197 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1201 sve_bswap64(r
, r
, DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
1206 int kvm_arch_get_registers(CPUState
*cs
)
1208 struct kvm_one_reg reg
;
1214 ARMCPU
*cpu
= ARM_CPU(cs
);
1215 CPUARMState
*env
= &cpu
->env
;
1217 for (i
= 0; i
< 31; i
++) {
1218 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
1219 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
1220 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1226 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
1227 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
1228 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1233 reg
.id
= AARCH64_CORE_REG(sp_el1
);
1234 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
1235 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1240 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
1241 reg
.addr
= (uintptr_t) &val
;
1242 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1247 env
->aarch64
= ((val
& PSTATE_nRW
) == 0);
1249 pstate_write(env
, val
);
1251 cpsr_write(env
, val
, 0xffffffff, CPSRWriteRaw
);
1254 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
1255 * QEMU side we keep the current SP in xregs[31] as well.
1257 aarch64_restore_sp(env
, 1);
1259 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
1260 reg
.addr
= (uintptr_t) &env
->pc
;
1261 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1266 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
1267 * incoming AArch64 regs received from 64-bit KVM.
1268 * We must perform this after all of the registers have been acquired from
1272 aarch64_sync_64_to_32(env
);
1275 reg
.id
= AARCH64_CORE_REG(elr_el1
);
1276 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
1277 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1282 /* Fetch the SPSR registers
1284 * KVM SPSRs 0-4 map to QEMU banks 1-5
1286 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
1287 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
1288 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
+ 1];
1289 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1295 el
= arm_current_el(env
);
1296 if (el
> 0 && !is_a64(env
)) {
1297 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
1298 env
->spsr
= env
->banked_spsr
[i
];
1301 if (cpu_isar_feature(aa64_sve
, cpu
)) {
1302 ret
= kvm_arch_get_sve(cs
);
1304 ret
= kvm_arch_get_fpsimd(cs
);
1310 reg
.addr
= (uintptr_t)(&fpr
);
1311 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
);
1312 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1316 vfp_set_fpsr(env
, fpr
);
1318 reg
.addr
= (uintptr_t)(&fpr
);
1319 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
);
1320 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
1324 vfp_set_fpcr(env
, fpr
);
1326 ret
= kvm_get_vcpu_events(cpu
);
1331 if (!write_kvmstate_to_list(cpu
)) {
1334 /* Note that it's OK to have registers which aren't in CPUState,
1335 * so we can ignore a failure return here.
1337 write_list_to_cpustate(cpu
);
1339 kvm_arm_sync_mpstate_to_qemu(cpu
);
1341 /* TODO: other registers */
1345 void kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
1347 ram_addr_t ram_addr
;
1349 Object
*obj
= qdev_get_machine();
1350 VirtMachineState
*vms
= VIRT_MACHINE(obj
);
1351 bool acpi_enabled
= virt_is_acpi_enabled(vms
);
1353 assert(code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
);
1355 if (acpi_enabled
&& addr
&&
1356 object_property_get_bool(obj
, "ras", NULL
)) {
1357 ram_addr
= qemu_ram_addr_from_host(addr
);
1358 if (ram_addr
!= RAM_ADDR_INVALID
&&
1359 kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
1360 kvm_hwpoison_page_add(ram_addr
);
1362 * If this is a BUS_MCEERR_AR, we know we have been called
1363 * synchronously from the vCPU thread, so we can easily
1364 * synchronize the state and inject an error.
1366 * TODO: we currently don't tell the guest at all about
1367 * BUS_MCEERR_AO. In that case we might either be being
1368 * called synchronously from the vCPU thread, or a bit
1369 * later from the main thread, so doing the injection of
1370 * the error would be more complicated.
1372 if (code
== BUS_MCEERR_AR
) {
1373 kvm_cpu_synchronize_state(c
);
1374 if (!acpi_ghes_record_errors(ACPI_HEST_SRC_ID_SEA
, paddr
)) {
1375 kvm_inject_arm_sea(c
);
1377 error_report("failed to record the error");
1383 if (code
== BUS_MCEERR_AO
) {
1384 error_report("Hardware memory error at addr %p for memory used by "
1385 "QEMU itself instead of guest system!", addr
);
1389 if (code
== BUS_MCEERR_AR
) {
1390 error_report("Hardware memory error!");
1395 /* C6.6.29 BRK instruction */
1396 static const uint32_t brk_insn
= 0xd4200000;
1398 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
1400 if (have_guest_debug
) {
1401 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 4, 0) ||
1402 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&brk_insn
, 4, 1)) {
1407 error_report("guest debug not supported on this kernel");
1412 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
1414 static uint32_t brk
;
1416 if (have_guest_debug
) {
1417 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&brk
, 4, 0) ||
1419 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 4, 1)) {
1424 error_report("guest debug not supported on this kernel");
1429 /* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
1431 * To minimise translating between kernel and user-space the kernel
1432 * ABI just provides user-space with the full exception syndrome
1433 * register value to be decoded in QEMU.
1436 bool kvm_arm_handle_debug(CPUState
*cs
, struct kvm_debug_exit_arch
*debug_exit
)
1438 int hsr_ec
= syn_get_ec(debug_exit
->hsr
);
1439 ARMCPU
*cpu
= ARM_CPU(cs
);
1440 CPUClass
*cc
= CPU_GET_CLASS(cs
);
1441 CPUARMState
*env
= &cpu
->env
;
1443 /* Ensure PC is synchronised */
1444 kvm_cpu_synchronize_state(cs
);
1447 case EC_SOFTWARESTEP
:
1448 if (cs
->singlestep_enabled
) {
1452 * The kernel should have suppressed the guest's ability to
1453 * single step at this point so something has gone wrong.
1455 error_report("%s: guest single-step while debugging unsupported"
1456 " (%"PRIx64
", %"PRIx32
")",
1457 __func__
, env
->pc
, debug_exit
->hsr
);
1462 if (kvm_find_sw_breakpoint(cs
, env
->pc
)) {
1467 if (find_hw_breakpoint(cs
, env
->pc
)) {
1473 CPUWatchpoint
*wp
= find_hw_watchpoint(cs
, debug_exit
->far
);
1475 cs
->watchpoint_hit
= wp
;
1481 error_report("%s: unhandled debug exit (%"PRIx32
", %"PRIx64
")",
1482 __func__
, debug_exit
->hsr
, env
->pc
);
1485 /* If we are not handling the debug exception it must belong to
1486 * the guest. Let's re-use the existing TCG interrupt code to set
1487 * everything up properly.
1489 cs
->exception_index
= EXCP_BKPT
;
1490 env
->exception
.syndrome
= debug_exit
->hsr
;
1491 env
->exception
.vaddress
= debug_exit
->far
;
1492 env
->exception
.target_el
= 1;
1493 qemu_mutex_lock_iothread();
1494 cc
->do_interrupt(cs
);
1495 qemu_mutex_unlock_iothread();
1500 #define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)
1501 #define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)
1506 * AARCH64: DFSC, bits [5:0]
1510 * FS[3:0] - DFSR[3:0]
1514 #define ESR_DFSC(aarch64, lpae, v) \
1515 ((aarch64 || (lpae)) ? ((v) & 0x3F) \
1516 : (((v) >> 6) | ((v) & 0x1F)))
1518 #define ESR_DFSC_EXTABT(aarch64, lpae) \
1519 ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)
1521 bool kvm_arm_verify_ext_dabt_pending(CPUState
*cs
)
1525 if (!kvm_get_one_reg(cs
, ARM64_REG_ESR_EL1
, &dfsr_val
)) {
1526 ARMCPU
*cpu
= ARM_CPU(cs
);
1527 CPUARMState
*env
= &cpu
->env
;
1528 int aarch64_mode
= arm_feature(env
, ARM_FEATURE_AARCH64
);
1531 if (!aarch64_mode
) {
1534 if (!kvm_get_one_reg(cs
, ARM64_REG_TCR_EL1
, &ttbcr
)) {
1535 lpae
= arm_feature(env
, ARM_FEATURE_LPAE
)
1536 && (ttbcr
& TTBCR_EAE
);
1540 * The verification here is based on the DFSC bits
1541 * of the ESR_EL1 reg only
1543 return (ESR_DFSC(aarch64_mode
, lpae
, dfsr_val
) ==
1544 ESR_DFSC_EXTABT(aarch64_mode
, lpae
));