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 "exec/gdbstub.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/kvm.h"
28 #include "internals.h"
29 #include "hw/arm/arm.h"
31 static bool have_guest_debug
;
34 * Although the ARM implementation of hardware assisted debugging
35 * allows for different breakpoints per-core, the current GDB
36 * interface treats them as a global pool of registers (which seems to
37 * be the case for x86, ppc and s390). As a result we store one copy
38 * of registers which is used for all active cores.
40 * Write access is serialised by virtue of the GDB protocol which
41 * updates things. Read access (i.e. when the values are copied to the
42 * vCPU) is also gated by GDB's run control.
44 * This is not unreasonable as most of the time debugging kernels you
45 * never know which core will eventually execute your function.
53 /* The watchpoint registers can cover more area than the requested
54 * watchpoint so we need to store the additional information
55 * somewhere. We also need to supply a CPUWatchpoint to the GDB stub
56 * when the watchpoint is hit.
61 CPUWatchpoint details
;
64 /* Maximum and current break/watch point counts */
65 int max_hw_bps
, max_hw_wps
;
66 GArray
*hw_breakpoints
, *hw_watchpoints
;
68 #define cur_hw_wps (hw_watchpoints->len)
69 #define cur_hw_bps (hw_breakpoints->len)
70 #define get_hw_bp(i) (&g_array_index(hw_breakpoints, HWBreakpoint, i))
71 #define get_hw_wp(i) (&g_array_index(hw_watchpoints, HWWatchpoint, i))
74 * kvm_arm_init_debug() - check for guest debug capabilities
77 * kvm_check_extension returns the number of debug registers we have
78 * or 0 if we have none.
81 static void kvm_arm_init_debug(CPUState
*cs
)
83 have_guest_debug
= kvm_check_extension(cs
->kvm_state
,
84 KVM_CAP_SET_GUEST_DEBUG
);
86 max_hw_wps
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GUEST_DEBUG_HW_WPS
);
87 hw_watchpoints
= g_array_sized_new(true, true,
88 sizeof(HWWatchpoint
), max_hw_wps
);
90 max_hw_bps
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GUEST_DEBUG_HW_BPS
);
91 hw_breakpoints
= g_array_sized_new(true, true,
92 sizeof(HWBreakpoint
), max_hw_bps
);
97 * insert_hw_breakpoint()
98 * @addr: address of breakpoint
100 * See ARM ARM D2.9.1 for details but here we are only going to create
101 * simple un-linked breakpoints (i.e. we don't chain breakpoints
102 * together to match address and context or vmid). The hardware is
103 * capable of fancier matching but that will require exposing that
104 * fanciness to GDB's interface
106 * DBGBCR<n>_EL1, Debug Breakpoint Control Registers
108 * 31 24 23 20 19 16 15 14 13 12 9 8 5 4 3 2 1 0
109 * +------+------+-------+-----+----+------+-----+------+-----+---+
110 * | RES0 | BT | LBN | SSC | HMC| RES0 | BAS | RES0 | PMC | E |
111 * +------+------+-------+-----+----+------+-----+------+-----+---+
113 * BT: Breakpoint type (0 = unlinked address match)
114 * LBN: Linked BP number (0 = unused)
115 * SSC/HMC/PMC: Security, Higher and Priv access control (Table D-12)
116 * BAS: Byte Address Select (RES1 for AArch64)
119 * DBGBVR<n>_EL1, Debug Breakpoint Value Registers
121 * 63 53 52 49 48 2 1 0
122 * +------+-----------+----------+-----+
123 * | RESS | VA[52:49] | VA[48:2] | 0 0 |
124 * +------+-----------+----------+-----+
126 * Depending on the addressing mode bits the top bits of the register
127 * are a sign extension of the highest applicable VA bit. Some
128 * versions of GDB don't do it correctly so we ensure they are correct
129 * here so future PC comparisons will work properly.
132 static int insert_hw_breakpoint(target_ulong addr
)
135 .bcr
= 0x1, /* BCR E=1, enable */
136 .bvr
= sextract64(addr
, 0, 53)
139 if (cur_hw_bps
>= max_hw_bps
) {
143 brk
.bcr
= deposit32(brk
.bcr
, 1, 2, 0x3); /* PMC = 11 */
144 brk
.bcr
= deposit32(brk
.bcr
, 5, 4, 0xf); /* BAS = RES1 */
146 g_array_append_val(hw_breakpoints
, brk
);
152 * delete_hw_breakpoint()
153 * @pc: address of breakpoint
155 * Delete a breakpoint and shuffle any above down
158 static int delete_hw_breakpoint(target_ulong pc
)
161 for (i
= 0; i
< hw_breakpoints
->len
; i
++) {
162 HWBreakpoint
*brk
= get_hw_bp(i
);
163 if (brk
->bvr
== pc
) {
164 g_array_remove_index(hw_breakpoints
, i
);
172 * insert_hw_watchpoint()
173 * @addr: address of watch point
175 * @type: type of watch point
177 * See ARM ARM D2.10. As with the breakpoints we can do some advanced
178 * stuff if we want to. The watch points can be linked with the break
179 * points above to make them context aware. However for simplicity
180 * currently we only deal with simple read/write watch points.
182 * D7.3.11 DBGWCR<n>_EL1, Debug Watchpoint Control Registers
184 * 31 29 28 24 23 21 20 19 16 15 14 13 12 5 4 3 2 1 0
185 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
186 * | RES0 | MASK | RES0 | WT | LBN | SSC | HMC | BAS | LSC | PAC | E |
187 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
189 * MASK: num bits addr mask (0=none,01/10=res,11=3 bits (8 bytes))
190 * WT: 0 - unlinked, 1 - linked (not currently used)
191 * LBN: Linked BP number (not currently used)
192 * SSC/HMC/PAC: Security, Higher and Priv access control (Table D2-11)
193 * BAS: Byte Address Select
194 * LSC: Load/Store control (01: load, 10: store, 11: both)
197 * The bottom 2 bits of the value register are masked. Therefore to
198 * break on any sizes smaller than an unaligned word you need to set
199 * MASK=0, BAS=bit per byte in question. For larger regions (^2) you
200 * need to ensure you mask the address as required and set BAS=0xff
203 static int insert_hw_watchpoint(target_ulong addr
,
204 target_ulong len
, int type
)
207 .wcr
= 1, /* E=1, enable */
208 .wvr
= addr
& (~0x7ULL
),
209 .details
= { .vaddr
= addr
, .len
= len
}
212 if (cur_hw_wps
>= max_hw_wps
) {
217 * HMC=0 SSC=0 PAC=3 will hit EL0 or EL1, any security state,
218 * valid whether EL3 is implemented or not
220 wp
.wcr
= deposit32(wp
.wcr
, 1, 2, 3);
223 case GDB_WATCHPOINT_READ
:
224 wp
.wcr
= deposit32(wp
.wcr
, 3, 2, 1);
225 wp
.details
.flags
= BP_MEM_READ
;
227 case GDB_WATCHPOINT_WRITE
:
228 wp
.wcr
= deposit32(wp
.wcr
, 3, 2, 2);
229 wp
.details
.flags
= BP_MEM_WRITE
;
231 case GDB_WATCHPOINT_ACCESS
:
232 wp
.wcr
= deposit32(wp
.wcr
, 3, 2, 3);
233 wp
.details
.flags
= BP_MEM_ACCESS
;
236 g_assert_not_reached();
240 /* we align the address and set the bits in BAS */
241 int off
= addr
& 0x7;
242 int bas
= (1 << len
) - 1;
244 wp
.wcr
= deposit32(wp
.wcr
, 5 + off
, 8 - off
, bas
);
246 /* For ranges above 8 bytes we need to be a power of 2 */
247 if (is_power_of_2(len
)) {
248 int bits
= ctz64(len
);
250 wp
.wvr
&= ~((1 << bits
) - 1);
251 wp
.wcr
= deposit32(wp
.wcr
, 24, 4, bits
);
252 wp
.wcr
= deposit32(wp
.wcr
, 5, 8, 0xff);
258 g_array_append_val(hw_watchpoints
, wp
);
263 static bool check_watchpoint_in_range(int i
, target_ulong addr
)
265 HWWatchpoint
*wp
= get_hw_wp(i
);
266 uint64_t addr_top
, addr_bottom
= wp
->wvr
;
267 int bas
= extract32(wp
->wcr
, 5, 8);
268 int mask
= extract32(wp
->wcr
, 24, 4);
271 addr_top
= addr_bottom
+ (1 << mask
);
273 /* BAS must be contiguous but can offset against the base
274 * address in DBGWVR */
275 addr_bottom
= addr_bottom
+ ctz32(bas
);
276 addr_top
= addr_bottom
+ clo32(bas
);
279 if (addr
>= addr_bottom
&& addr
<= addr_top
) {
287 * delete_hw_watchpoint()
288 * @addr: address of breakpoint
290 * Delete a breakpoint and shuffle any above down
293 static int delete_hw_watchpoint(target_ulong addr
,
294 target_ulong len
, int type
)
297 for (i
= 0; i
< cur_hw_wps
; i
++) {
298 if (check_watchpoint_in_range(i
, addr
)) {
299 g_array_remove_index(hw_watchpoints
, i
);
307 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
308 target_ulong len
, int type
)
311 case GDB_BREAKPOINT_HW
:
312 return insert_hw_breakpoint(addr
);
314 case GDB_WATCHPOINT_READ
:
315 case GDB_WATCHPOINT_WRITE
:
316 case GDB_WATCHPOINT_ACCESS
:
317 return insert_hw_watchpoint(addr
, len
, type
);
323 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
324 target_ulong len
, int type
)
327 case GDB_BREAKPOINT_HW
:
328 return delete_hw_breakpoint(addr
);
330 case GDB_WATCHPOINT_READ
:
331 case GDB_WATCHPOINT_WRITE
:
332 case GDB_WATCHPOINT_ACCESS
:
333 return delete_hw_watchpoint(addr
, len
, type
);
340 void kvm_arch_remove_all_hw_breakpoints(void)
342 if (cur_hw_wps
> 0) {
343 g_array_remove_range(hw_watchpoints
, 0, cur_hw_wps
);
345 if (cur_hw_bps
> 0) {
346 g_array_remove_range(hw_breakpoints
, 0, cur_hw_bps
);
350 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch
*ptr
)
353 memset(ptr
, 0, sizeof(struct kvm_guest_debug_arch
));
355 for (i
= 0; i
< max_hw_wps
; i
++) {
356 HWWatchpoint
*wp
= get_hw_wp(i
);
357 ptr
->dbg_wcr
[i
] = wp
->wcr
;
358 ptr
->dbg_wvr
[i
] = wp
->wvr
;
360 for (i
= 0; i
< max_hw_bps
; i
++) {
361 HWBreakpoint
*bp
= get_hw_bp(i
);
362 ptr
->dbg_bcr
[i
] = bp
->bcr
;
363 ptr
->dbg_bvr
[i
] = bp
->bvr
;
367 bool kvm_arm_hw_debug_active(CPUState
*cs
)
369 return ((cur_hw_wps
> 0) || (cur_hw_bps
> 0));
372 static bool find_hw_breakpoint(CPUState
*cpu
, target_ulong pc
)
376 for (i
= 0; i
< cur_hw_bps
; i
++) {
377 HWBreakpoint
*bp
= get_hw_bp(i
);
385 static CPUWatchpoint
*find_hw_watchpoint(CPUState
*cpu
, target_ulong addr
)
389 for (i
= 0; i
< cur_hw_wps
; i
++) {
390 if (check_watchpoint_in_range(i
, addr
)) {
391 return &get_hw_wp(i
)->details
;
397 static bool kvm_arm_pmu_set_attr(CPUState
*cs
, struct kvm_device_attr
*attr
)
401 err
= kvm_vcpu_ioctl(cs
, KVM_HAS_DEVICE_ATTR
, attr
);
403 error_report("PMU: KVM_HAS_DEVICE_ATTR: %s", strerror(-err
));
407 err
= kvm_vcpu_ioctl(cs
, KVM_SET_DEVICE_ATTR
, attr
);
409 error_report("PMU: KVM_SET_DEVICE_ATTR: %s", strerror(-err
));
416 void kvm_arm_pmu_init(CPUState
*cs
)
418 struct kvm_device_attr attr
= {
419 .group
= KVM_ARM_VCPU_PMU_V3_CTRL
,
420 .attr
= KVM_ARM_VCPU_PMU_V3_INIT
,
423 if (!ARM_CPU(cs
)->has_pmu
) {
426 if (!kvm_arm_pmu_set_attr(cs
, &attr
)) {
427 error_report("failed to init PMU");
432 void kvm_arm_pmu_set_irq(CPUState
*cs
, int irq
)
434 struct kvm_device_attr attr
= {
435 .group
= KVM_ARM_VCPU_PMU_V3_CTRL
,
436 .addr
= (intptr_t)&irq
,
437 .attr
= KVM_ARM_VCPU_PMU_V3_IRQ
,
440 if (!ARM_CPU(cs
)->has_pmu
) {
443 if (!kvm_arm_pmu_set_attr(cs
, &attr
)) {
444 error_report("failed to set irq for PMU");
449 static inline void set_feature(uint64_t *features
, int feature
)
451 *features
|= 1ULL << feature
;
454 static inline void unset_feature(uint64_t *features
, int feature
)
456 *features
&= ~(1ULL << feature
);
459 static int read_sys_reg32(int fd
, uint32_t *pret
, uint64_t id
)
462 struct kvm_one_reg idreg
= { .id
= id
, .addr
= (uintptr_t)&ret
};
465 assert((id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
);
466 err
= ioctl(fd
, KVM_GET_ONE_REG
, &idreg
);
474 static int read_sys_reg64(int fd
, uint64_t *pret
, uint64_t id
)
476 struct kvm_one_reg idreg
= { .id
= id
, .addr
= (uintptr_t)pret
};
478 assert((id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
);
479 return ioctl(fd
, KVM_GET_ONE_REG
, &idreg
);
482 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures
*ahcf
)
484 /* Identify the feature bits corresponding to the host CPU, and
485 * fill out the ARMHostCPUClass fields accordingly. To do this
486 * we have to create a scratch VM, create a single CPU inside it,
487 * and then query that CPU for the relevant ID registers.
490 uint64_t features
= 0;
493 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
494 * we know these will only support creating one kind of guest CPU,
495 * which is its preferred CPU type. Fortunately these old kernels
496 * support only a very limited number of CPUs.
498 static const uint32_t cpus_to_try
[] = {
499 KVM_ARM_TARGET_AEM_V8
,
500 KVM_ARM_TARGET_FOUNDATION_V8
,
501 KVM_ARM_TARGET_CORTEX_A57
,
502 QEMU_KVM_ARM_TARGET_NONE
504 struct kvm_vcpu_init init
;
506 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try
, fdarray
, &init
)) {
510 ahcf
->target
= init
.target
;
511 ahcf
->dtb_compatible
= "arm,arm-v8";
513 err
= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64pfr0
,
514 ARM64_SYS_REG(3, 0, 0, 4, 0));
515 if (unlikely(err
< 0)) {
517 * Before v4.15, the kernel only exposed a limited number of system
518 * registers, not including any of the interesting AArch64 ID regs.
519 * For the most part we could leave these fields as zero with minimal
520 * effect, since this does not affect the values seen by the guest.
522 * However, it could cause problems down the line for QEMU,
523 * so provide a minimal v8.0 default.
525 * ??? Could read MIDR and use knowledge from cpu64.c.
526 * ??? Could map a page of memory into our temp guest and
527 * run the tiniest of hand-crafted kernels to extract
528 * the values seen by the guest.
529 * ??? Either of these sounds like too much effort just
530 * to work around running a modern host kernel.
532 ahcf
->isar
.id_aa64pfr0
= 0x00000011; /* EL1&0, AArch64 only */
535 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64pfr1
,
536 ARM64_SYS_REG(3, 0, 0, 4, 1));
537 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar0
,
538 ARM64_SYS_REG(3, 0, 0, 6, 0));
539 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar1
,
540 ARM64_SYS_REG(3, 0, 0, 6, 1));
541 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr0
,
542 ARM64_SYS_REG(3, 0, 0, 7, 0));
543 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr1
,
544 ARM64_SYS_REG(3, 0, 0, 7, 1));
547 * Note that if AArch32 support is not present in the host,
548 * the AArch32 sysregs are present to be read, but will
549 * return UNKNOWN values. This is neither better nor worse
550 * than skipping the reads and leaving 0, as we must avoid
551 * considering the values in every case.
553 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar0
,
554 ARM64_SYS_REG(3, 0, 0, 2, 0));
555 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar1
,
556 ARM64_SYS_REG(3, 0, 0, 2, 1));
557 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar2
,
558 ARM64_SYS_REG(3, 0, 0, 2, 2));
559 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar3
,
560 ARM64_SYS_REG(3, 0, 0, 2, 3));
561 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar4
,
562 ARM64_SYS_REG(3, 0, 0, 2, 4));
563 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar5
,
564 ARM64_SYS_REG(3, 0, 0, 2, 5));
565 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar6
,
566 ARM64_SYS_REG(3, 0, 0, 2, 7));
568 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr0
,
569 ARM64_SYS_REG(3, 0, 0, 3, 0));
570 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr1
,
571 ARM64_SYS_REG(3, 0, 0, 3, 1));
572 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr2
,
573 ARM64_SYS_REG(3, 0, 0, 3, 2));
576 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
582 /* We can assume any KVM supporting CPU is at least a v8
583 * with VFPv4+Neon; this in turn implies most of the other
586 set_feature(&features
, ARM_FEATURE_V8
);
587 set_feature(&features
, ARM_FEATURE_VFP4
);
588 set_feature(&features
, ARM_FEATURE_NEON
);
589 set_feature(&features
, ARM_FEATURE_AARCH64
);
590 set_feature(&features
, ARM_FEATURE_PMU
);
592 ahcf
->features
= features
;
597 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
599 int kvm_arch_init_vcpu(CPUState
*cs
)
603 ARMCPU
*cpu
= ARM_CPU(cs
);
604 CPUARMState
*env
= &cpu
->env
;
606 if (cpu
->kvm_target
== QEMU_KVM_ARM_TARGET_NONE
||
607 !object_dynamic_cast(OBJECT(cpu
), TYPE_AARCH64_CPU
)) {
608 fprintf(stderr
, "KVM is not supported for this guest CPU type\n");
612 /* Determine init features for this CPU */
613 memset(cpu
->kvm_init_features
, 0, sizeof(cpu
->kvm_init_features
));
614 if (cpu
->start_powered_off
) {
615 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_POWER_OFF
;
617 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PSCI_0_2
)) {
618 cpu
->psci_version
= 2;
619 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2
;
621 if (!arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
622 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT
;
624 if (!kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PMU_V3
)) {
625 cpu
->has_pmu
= false;
628 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PMU_V3
;
630 unset_feature(&env
->features
, ARM_FEATURE_PMU
);
633 /* Do KVM_ARM_VCPU_INIT ioctl */
634 ret
= kvm_arm_vcpu_init(cs
);
640 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
641 * Currently KVM has its own idea about MPIDR assignment, so we
642 * override our defaults with what we get from KVM.
644 ret
= kvm_get_one_reg(cs
, ARM64_SYS_REG(ARM_CPU_ID_MPIDR
), &mpidr
);
648 cpu
->mp_affinity
= mpidr
& ARM64_AFFINITY_MASK
;
650 kvm_arm_init_debug(cs
);
652 /* Check whether user space can specify guest syndrome value */
653 kvm_arm_init_serror_injection(cs
);
655 return kvm_arm_init_cpreg_list(cpu
);
658 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx
)
660 /* Return true if the regidx is a register we should synchronize
661 * via the cpreg_tuples array (ie is not a core reg we sync by
662 * hand in kvm_arch_get/put_registers())
664 switch (regidx
& KVM_REG_ARM_COPROC_MASK
) {
665 case KVM_REG_ARM_CORE
:
672 typedef struct CPRegStateLevel
{
677 /* All system registers not listed in the following table are assumed to be
678 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
679 * often, you must add it to this table with a state of either
680 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
682 static const CPRegStateLevel non_runtime_cpregs
[] = {
683 { KVM_REG_ARM_TIMER_CNT
, KVM_PUT_FULL_STATE
},
686 int kvm_arm_cpreg_level(uint64_t regidx
)
690 for (i
= 0; i
< ARRAY_SIZE(non_runtime_cpregs
); i
++) {
691 const CPRegStateLevel
*l
= &non_runtime_cpregs
[i
];
692 if (l
->regidx
== regidx
) {
697 return KVM_PUT_RUNTIME_STATE
;
700 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
701 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
703 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
704 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
706 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
707 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
709 int kvm_arch_put_registers(CPUState
*cs
, int level
)
711 struct kvm_one_reg reg
;
718 ARMCPU
*cpu
= ARM_CPU(cs
);
719 CPUARMState
*env
= &cpu
->env
;
721 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
722 * AArch64 registers before pushing them out to 64-bit KVM.
725 aarch64_sync_32_to_64(env
);
728 for (i
= 0; i
< 31; i
++) {
729 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
730 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
731 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
737 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
738 * QEMU side we keep the current SP in xregs[31] as well.
740 aarch64_save_sp(env
, 1);
742 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
743 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
744 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
749 reg
.id
= AARCH64_CORE_REG(sp_el1
);
750 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
751 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
756 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
758 val
= pstate_read(env
);
760 val
= cpsr_read(env
);
762 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
763 reg
.addr
= (uintptr_t) &val
;
764 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
769 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
770 reg
.addr
= (uintptr_t) &env
->pc
;
771 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
776 reg
.id
= AARCH64_CORE_REG(elr_el1
);
777 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
778 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
783 /* Saved Program State Registers
785 * Before we restore from the banked_spsr[] array we need to
786 * ensure that any modifications to env->spsr are correctly
787 * reflected in the banks.
789 el
= arm_current_el(env
);
790 if (el
> 0 && !is_a64(env
)) {
791 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
792 env
->banked_spsr
[i
] = env
->spsr
;
795 /* KVM 0-4 map to QEMU banks 1-5 */
796 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
797 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
798 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
+ 1];
799 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
805 /* Advanced SIMD and FP registers. */
806 for (i
= 0; i
< 32; i
++) {
807 uint64_t *q
= aa64_vfp_qreg(env
, i
);
808 #ifdef HOST_WORDS_BIGENDIAN
809 uint64_t fp_val
[2] = { q
[1], q
[0] };
810 reg
.addr
= (uintptr_t)fp_val
;
812 reg
.addr
= (uintptr_t)q
;
814 reg
.id
= AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]);
815 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
821 reg
.addr
= (uintptr_t)(&fpr
);
822 fpr
= vfp_get_fpsr(env
);
823 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
);
824 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
829 fpr
= vfp_get_fpcr(env
);
830 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
);
831 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
836 ret
= kvm_put_vcpu_events(cpu
);
841 if (!write_list_to_kvmstate(cpu
, level
)) {
845 kvm_arm_sync_mpstate_to_kvm(cpu
);
850 int kvm_arch_get_registers(CPUState
*cs
)
852 struct kvm_one_reg reg
;
859 ARMCPU
*cpu
= ARM_CPU(cs
);
860 CPUARMState
*env
= &cpu
->env
;
862 for (i
= 0; i
< 31; i
++) {
863 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
864 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
865 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
871 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
872 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
873 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
878 reg
.id
= AARCH64_CORE_REG(sp_el1
);
879 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
880 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
885 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
886 reg
.addr
= (uintptr_t) &val
;
887 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
892 env
->aarch64
= ((val
& PSTATE_nRW
) == 0);
894 pstate_write(env
, val
);
896 cpsr_write(env
, val
, 0xffffffff, CPSRWriteRaw
);
899 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
900 * QEMU side we keep the current SP in xregs[31] as well.
902 aarch64_restore_sp(env
, 1);
904 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
905 reg
.addr
= (uintptr_t) &env
->pc
;
906 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
911 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
912 * incoming AArch64 regs received from 64-bit KVM.
913 * We must perform this after all of the registers have been acquired from
917 aarch64_sync_64_to_32(env
);
920 reg
.id
= AARCH64_CORE_REG(elr_el1
);
921 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
922 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
927 /* Fetch the SPSR registers
929 * KVM SPSRs 0-4 map to QEMU banks 1-5
931 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
932 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
933 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
+ 1];
934 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
940 el
= arm_current_el(env
);
941 if (el
> 0 && !is_a64(env
)) {
942 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
943 env
->spsr
= env
->banked_spsr
[i
];
946 /* Advanced SIMD and FP registers */
947 for (i
= 0; i
< 32; i
++) {
948 uint64_t *q
= aa64_vfp_qreg(env
, i
);
949 reg
.id
= AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]);
950 reg
.addr
= (uintptr_t)q
;
951 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
955 #ifdef HOST_WORDS_BIGENDIAN
957 t
= q
[0], q
[0] = q
[1], q
[1] = t
;
962 reg
.addr
= (uintptr_t)(&fpr
);
963 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
);
964 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
968 vfp_set_fpsr(env
, fpr
);
970 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
);
971 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
975 vfp_set_fpcr(env
, fpr
);
977 ret
= kvm_get_vcpu_events(cpu
);
982 if (!write_kvmstate_to_list(cpu
)) {
985 /* Note that it's OK to have registers which aren't in CPUState,
986 * so we can ignore a failure return here.
988 write_list_to_cpustate(cpu
);
990 kvm_arm_sync_mpstate_to_qemu(cpu
);
992 /* TODO: other registers */
996 /* C6.6.29 BRK instruction */
997 static const uint32_t brk_insn
= 0xd4200000;
999 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
1001 if (have_guest_debug
) {
1002 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 4, 0) ||
1003 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&brk_insn
, 4, 1)) {
1008 error_report("guest debug not supported on this kernel");
1013 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
1015 static uint32_t brk
;
1017 if (have_guest_debug
) {
1018 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&brk
, 4, 0) ||
1020 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 4, 1)) {
1025 error_report("guest debug not supported on this kernel");
1030 /* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
1032 * To minimise translating between kernel and user-space the kernel
1033 * ABI just provides user-space with the full exception syndrome
1034 * register value to be decoded in QEMU.
1037 bool kvm_arm_handle_debug(CPUState
*cs
, struct kvm_debug_exit_arch
*debug_exit
)
1039 int hsr_ec
= syn_get_ec(debug_exit
->hsr
);
1040 ARMCPU
*cpu
= ARM_CPU(cs
);
1041 CPUClass
*cc
= CPU_GET_CLASS(cs
);
1042 CPUARMState
*env
= &cpu
->env
;
1044 /* Ensure PC is synchronised */
1045 kvm_cpu_synchronize_state(cs
);
1048 case EC_SOFTWARESTEP
:
1049 if (cs
->singlestep_enabled
) {
1053 * The kernel should have suppressed the guest's ability to
1054 * single step at this point so something has gone wrong.
1056 error_report("%s: guest single-step while debugging unsupported"
1057 " (%"PRIx64
", %"PRIx32
")",
1058 __func__
, env
->pc
, debug_exit
->hsr
);
1063 if (kvm_find_sw_breakpoint(cs
, env
->pc
)) {
1068 if (find_hw_breakpoint(cs
, env
->pc
)) {
1074 CPUWatchpoint
*wp
= find_hw_watchpoint(cs
, debug_exit
->far
);
1076 cs
->watchpoint_hit
= wp
;
1082 error_report("%s: unhandled debug exit (%"PRIx32
", %"PRIx64
")",
1083 __func__
, debug_exit
->hsr
, env
->pc
);
1086 /* If we are not handling the debug exception it must belong to
1087 * the guest. Let's re-use the existing TCG interrupt code to set
1088 * everything up properly.
1090 cs
->exception_index
= EXCP_BKPT
;
1091 env
->exception
.syndrome
= debug_exit
->hsr
;
1092 env
->exception
.vaddress
= debug_exit
->far
;
1093 env
->exception
.target_el
= 1;
1094 qemu_mutex_lock_iothread();
1095 cc
->do_interrupt(cs
);
1096 qemu_mutex_unlock_iothread();