xen-pvdevice: convert to realize()
[qemu/ar7.git] / target-arm / kvm64.c
blobbb9531f33c1e07b380bca37b562167b9ae8ba479
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 <stdio.h>
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
15 #include <sys/mman.h>
16 #include <sys/ptrace.h>
18 #include <linux/elf.h>
19 #include <linux/kvm.h>
21 #include "config-host.h"
22 #include "qemu-common.h"
23 #include "qemu/timer.h"
24 #include "qemu/error-report.h"
25 #include "qemu/host-utils.h"
26 #include "exec/gdbstub.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/kvm.h"
29 #include "kvm_arm.h"
30 #include "cpu.h"
31 #include "internals.h"
32 #include "hw/arm/arm.h"
34 static bool have_guest_debug;
37 * Although the ARM implementation of hardware assisted debugging
38 * allows for different breakpoints per-core, the current GDB
39 * interface treats them as a global pool of registers (which seems to
40 * be the case for x86, ppc and s390). As a result we store one copy
41 * of registers which is used for all active cores.
43 * Write access is serialised by virtue of the GDB protocol which
44 * updates things. Read access (i.e. when the values are copied to the
45 * vCPU) is also gated by GDB's run control.
47 * This is not unreasonable as most of the time debugging kernels you
48 * never know which core will eventually execute your function.
51 typedef struct {
52 uint64_t bcr;
53 uint64_t bvr;
54 } HWBreakpoint;
56 /* The watchpoint registers can cover more area than the requested
57 * watchpoint so we need to store the additional information
58 * somewhere. We also need to supply a CPUWatchpoint to the GDB stub
59 * when the watchpoint is hit.
61 typedef struct {
62 uint64_t wcr;
63 uint64_t wvr;
64 CPUWatchpoint details;
65 } HWWatchpoint;
67 /* Maximum and current break/watch point counts */
68 int max_hw_bps, max_hw_wps;
69 GArray *hw_breakpoints, *hw_watchpoints;
71 #define cur_hw_wps (hw_watchpoints->len)
72 #define cur_hw_bps (hw_breakpoints->len)
73 #define get_hw_bp(i) (&g_array_index(hw_breakpoints, HWBreakpoint, i))
74 #define get_hw_wp(i) (&g_array_index(hw_watchpoints, HWWatchpoint, i))
76 /**
77 * kvm_arm_init_debug() - check for guest debug capabilities
78 * @cs: CPUState
80 * kvm_check_extension returns the number of debug registers we have
81 * or 0 if we have none.
84 static void kvm_arm_init_debug(CPUState *cs)
86 have_guest_debug = kvm_check_extension(cs->kvm_state,
87 KVM_CAP_SET_GUEST_DEBUG);
89 max_hw_wps = kvm_check_extension(cs->kvm_state, KVM_CAP_GUEST_DEBUG_HW_WPS);
90 hw_watchpoints = g_array_sized_new(true, true,
91 sizeof(HWWatchpoint), max_hw_wps);
93 max_hw_bps = kvm_check_extension(cs->kvm_state, KVM_CAP_GUEST_DEBUG_HW_BPS);
94 hw_breakpoints = g_array_sized_new(true, true,
95 sizeof(HWBreakpoint), max_hw_bps);
96 return;
99 /**
100 * insert_hw_breakpoint()
101 * @addr: address of breakpoint
103 * See ARM ARM D2.9.1 for details but here we are only going to create
104 * simple un-linked breakpoints (i.e. we don't chain breakpoints
105 * together to match address and context or vmid). The hardware is
106 * capable of fancier matching but that will require exposing that
107 * fanciness to GDB's interface
109 * D7.3.2 DBGBCR<n>_EL1, Debug Breakpoint Control Registers
111 * 31 24 23 20 19 16 15 14 13 12 9 8 5 4 3 2 1 0
112 * +------+------+-------+-----+----+------+-----+------+-----+---+
113 * | RES0 | BT | LBN | SSC | HMC| RES0 | BAS | RES0 | PMC | E |
114 * +------+------+-------+-----+----+------+-----+------+-----+---+
116 * BT: Breakpoint type (0 = unlinked address match)
117 * LBN: Linked BP number (0 = unused)
118 * SSC/HMC/PMC: Security, Higher and Priv access control (Table D-12)
119 * BAS: Byte Address Select (RES1 for AArch64)
120 * E: Enable bit
122 static int insert_hw_breakpoint(target_ulong addr)
124 HWBreakpoint brk = {
125 .bcr = 0x1, /* BCR E=1, enable */
126 .bvr = addr
129 if (cur_hw_bps >= max_hw_bps) {
130 return -ENOBUFS;
133 brk.bcr = deposit32(brk.bcr, 1, 2, 0x3); /* PMC = 11 */
134 brk.bcr = deposit32(brk.bcr, 5, 4, 0xf); /* BAS = RES1 */
136 g_array_append_val(hw_breakpoints, brk);
138 return 0;
142 * delete_hw_breakpoint()
143 * @pc: address of breakpoint
145 * Delete a breakpoint and shuffle any above down
148 static int delete_hw_breakpoint(target_ulong pc)
150 int i;
151 for (i = 0; i < hw_breakpoints->len; i++) {
152 HWBreakpoint *brk = get_hw_bp(i);
153 if (brk->bvr == pc) {
154 g_array_remove_index(hw_breakpoints, i);
155 return 0;
158 return -ENOENT;
162 * insert_hw_watchpoint()
163 * @addr: address of watch point
164 * @len: size of area
165 * @type: type of watch point
167 * See ARM ARM D2.10. As with the breakpoints we can do some advanced
168 * stuff if we want to. The watch points can be linked with the break
169 * points above to make them context aware. However for simplicity
170 * currently we only deal with simple read/write watch points.
172 * D7.3.11 DBGWCR<n>_EL1, Debug Watchpoint Control Registers
174 * 31 29 28 24 23 21 20 19 16 15 14 13 12 5 4 3 2 1 0
175 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
176 * | RES0 | MASK | RES0 | WT | LBN | SSC | HMC | BAS | LSC | PAC | E |
177 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
179 * MASK: num bits addr mask (0=none,01/10=res,11=3 bits (8 bytes))
180 * WT: 0 - unlinked, 1 - linked (not currently used)
181 * LBN: Linked BP number (not currently used)
182 * SSC/HMC/PAC: Security, Higher and Priv access control (Table D2-11)
183 * BAS: Byte Address Select
184 * LSC: Load/Store control (01: load, 10: store, 11: both)
185 * E: Enable
187 * The bottom 2 bits of the value register are masked. Therefore to
188 * break on any sizes smaller than an unaligned word you need to set
189 * MASK=0, BAS=bit per byte in question. For larger regions (^2) you
190 * need to ensure you mask the address as required and set BAS=0xff
193 static int insert_hw_watchpoint(target_ulong addr,
194 target_ulong len, int type)
196 HWWatchpoint wp = {
197 .wcr = 1, /* E=1, enable */
198 .wvr = addr & (~0x7ULL),
199 .details = { .vaddr = addr, .len = len }
202 if (cur_hw_wps >= max_hw_wps) {
203 return -ENOBUFS;
207 * HMC=0 SSC=0 PAC=3 will hit EL0 or EL1, any security state,
208 * valid whether EL3 is implemented or not
210 wp.wcr = deposit32(wp.wcr, 1, 2, 3);
212 switch (type) {
213 case GDB_WATCHPOINT_READ:
214 wp.wcr = deposit32(wp.wcr, 3, 2, 1);
215 wp.details.flags = BP_MEM_READ;
216 break;
217 case GDB_WATCHPOINT_WRITE:
218 wp.wcr = deposit32(wp.wcr, 3, 2, 2);
219 wp.details.flags = BP_MEM_WRITE;
220 break;
221 case GDB_WATCHPOINT_ACCESS:
222 wp.wcr = deposit32(wp.wcr, 3, 2, 3);
223 wp.details.flags = BP_MEM_ACCESS;
224 break;
225 default:
226 g_assert_not_reached();
227 break;
229 if (len <= 8) {
230 /* we align the address and set the bits in BAS */
231 int off = addr & 0x7;
232 int bas = (1 << len) - 1;
234 wp.wcr = deposit32(wp.wcr, 5 + off, 8 - off, bas);
235 } else {
236 /* For ranges above 8 bytes we need to be a power of 2 */
237 if (is_power_of_2(len)) {
238 int bits = ctz64(len);
240 wp.wvr &= ~((1 << bits) - 1);
241 wp.wcr = deposit32(wp.wcr, 24, 4, bits);
242 wp.wcr = deposit32(wp.wcr, 5, 8, 0xff);
243 } else {
244 return -ENOBUFS;
248 g_array_append_val(hw_watchpoints, wp);
249 return 0;
253 static bool check_watchpoint_in_range(int i, target_ulong addr)
255 HWWatchpoint *wp = get_hw_wp(i);
256 uint64_t addr_top, addr_bottom = wp->wvr;
257 int bas = extract32(wp->wcr, 5, 8);
258 int mask = extract32(wp->wcr, 24, 4);
260 if (mask) {
261 addr_top = addr_bottom + (1 << mask);
262 } else {
263 /* BAS must be contiguous but can offset against the base
264 * address in DBGWVR */
265 addr_bottom = addr_bottom + ctz32(bas);
266 addr_top = addr_bottom + clo32(bas);
269 if (addr >= addr_bottom && addr <= addr_top) {
270 return true;
273 return false;
277 * delete_hw_watchpoint()
278 * @addr: address of breakpoint
280 * Delete a breakpoint and shuffle any above down
283 static int delete_hw_watchpoint(target_ulong addr,
284 target_ulong len, int type)
286 int i;
287 for (i = 0; i < cur_hw_wps; i++) {
288 if (check_watchpoint_in_range(i, addr)) {
289 g_array_remove_index(hw_watchpoints, i);
290 return 0;
293 return -ENOENT;
297 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
298 target_ulong len, int type)
300 switch (type) {
301 case GDB_BREAKPOINT_HW:
302 return insert_hw_breakpoint(addr);
303 break;
304 case GDB_WATCHPOINT_READ:
305 case GDB_WATCHPOINT_WRITE:
306 case GDB_WATCHPOINT_ACCESS:
307 return insert_hw_watchpoint(addr, len, type);
308 default:
309 return -ENOSYS;
313 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
314 target_ulong len, int type)
316 switch (type) {
317 case GDB_BREAKPOINT_HW:
318 return delete_hw_breakpoint(addr);
319 break;
320 case GDB_WATCHPOINT_READ:
321 case GDB_WATCHPOINT_WRITE:
322 case GDB_WATCHPOINT_ACCESS:
323 return delete_hw_watchpoint(addr, len, type);
324 default:
325 return -ENOSYS;
330 void kvm_arch_remove_all_hw_breakpoints(void)
332 if (cur_hw_wps > 0) {
333 g_array_remove_range(hw_watchpoints, 0, cur_hw_wps);
335 if (cur_hw_bps > 0) {
336 g_array_remove_range(hw_breakpoints, 0, cur_hw_bps);
340 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr)
342 int i;
343 memset(ptr, 0, sizeof(struct kvm_guest_debug_arch));
345 for (i = 0; i < max_hw_wps; i++) {
346 HWWatchpoint *wp = get_hw_wp(i);
347 ptr->dbg_wcr[i] = wp->wcr;
348 ptr->dbg_wvr[i] = wp->wvr;
350 for (i = 0; i < max_hw_bps; i++) {
351 HWBreakpoint *bp = get_hw_bp(i);
352 ptr->dbg_bcr[i] = bp->bcr;
353 ptr->dbg_bvr[i] = bp->bvr;
357 bool kvm_arm_hw_debug_active(CPUState *cs)
359 return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
362 static bool find_hw_breakpoint(CPUState *cpu, target_ulong pc)
364 int i;
366 for (i = 0; i < cur_hw_bps; i++) {
367 HWBreakpoint *bp = get_hw_bp(i);
368 if (bp->bvr == pc) {
369 return true;
372 return false;
375 static CPUWatchpoint *find_hw_watchpoint(CPUState *cpu, target_ulong addr)
377 int i;
379 for (i = 0; i < cur_hw_wps; i++) {
380 if (check_watchpoint_in_range(i, addr)) {
381 return &get_hw_wp(i)->details;
384 return NULL;
388 static inline void set_feature(uint64_t *features, int feature)
390 *features |= 1ULL << feature;
393 bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
395 /* Identify the feature bits corresponding to the host CPU, and
396 * fill out the ARMHostCPUClass fields accordingly. To do this
397 * we have to create a scratch VM, create a single CPU inside it,
398 * and then query that CPU for the relevant ID registers.
399 * For AArch64 we currently don't care about ID registers at
400 * all; we just want to know the CPU type.
402 int fdarray[3];
403 uint64_t features = 0;
404 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
405 * we know these will only support creating one kind of guest CPU,
406 * which is its preferred CPU type. Fortunately these old kernels
407 * support only a very limited number of CPUs.
409 static const uint32_t cpus_to_try[] = {
410 KVM_ARM_TARGET_AEM_V8,
411 KVM_ARM_TARGET_FOUNDATION_V8,
412 KVM_ARM_TARGET_CORTEX_A57,
413 QEMU_KVM_ARM_TARGET_NONE
415 struct kvm_vcpu_init init;
417 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
418 return false;
421 ahcc->target = init.target;
422 ahcc->dtb_compatible = "arm,arm-v8";
424 kvm_arm_destroy_scratch_host_vcpu(fdarray);
426 /* We can assume any KVM supporting CPU is at least a v8
427 * with VFPv4+Neon; this in turn implies most of the other
428 * feature bits.
430 set_feature(&features, ARM_FEATURE_V8);
431 set_feature(&features, ARM_FEATURE_VFP4);
432 set_feature(&features, ARM_FEATURE_NEON);
433 set_feature(&features, ARM_FEATURE_AARCH64);
435 ahcc->features = features;
437 return true;
440 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
442 int kvm_arch_init_vcpu(CPUState *cs)
444 int ret;
445 uint64_t mpidr;
446 ARMCPU *cpu = ARM_CPU(cs);
448 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
449 !object_dynamic_cast(OBJECT(cpu), TYPE_AARCH64_CPU)) {
450 fprintf(stderr, "KVM is not supported for this guest CPU type\n");
451 return -EINVAL;
454 /* Determine init features for this CPU */
455 memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
456 if (cpu->start_powered_off) {
457 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
459 if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
460 cpu->psci_version = 2;
461 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
463 if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
464 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
467 /* Do KVM_ARM_VCPU_INIT ioctl */
468 ret = kvm_arm_vcpu_init(cs);
469 if (ret) {
470 return ret;
474 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
475 * Currently KVM has its own idea about MPIDR assignment, so we
476 * override our defaults with what we get from KVM.
478 ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
479 if (ret) {
480 return ret;
482 cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
484 kvm_arm_init_debug(cs);
486 return kvm_arm_init_cpreg_list(cpu);
489 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
491 /* Return true if the regidx is a register we should synchronize
492 * via the cpreg_tuples array (ie is not a core reg we sync by
493 * hand in kvm_arch_get/put_registers())
495 switch (regidx & KVM_REG_ARM_COPROC_MASK) {
496 case KVM_REG_ARM_CORE:
497 return false;
498 default:
499 return true;
503 typedef struct CPRegStateLevel {
504 uint64_t regidx;
505 int level;
506 } CPRegStateLevel;
508 /* All system registers not listed in the following table are assumed to be
509 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
510 * often, you must add it to this table with a state of either
511 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
513 static const CPRegStateLevel non_runtime_cpregs[] = {
514 { KVM_REG_ARM_TIMER_CNT, KVM_PUT_FULL_STATE },
517 int kvm_arm_cpreg_level(uint64_t regidx)
519 int i;
521 for (i = 0; i < ARRAY_SIZE(non_runtime_cpregs); i++) {
522 const CPRegStateLevel *l = &non_runtime_cpregs[i];
523 if (l->regidx == regidx) {
524 return l->level;
528 return KVM_PUT_RUNTIME_STATE;
531 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
532 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
534 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
535 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
537 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
538 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
540 int kvm_arch_put_registers(CPUState *cs, int level)
542 struct kvm_one_reg reg;
543 uint32_t fpr;
544 uint64_t val;
545 int i;
546 int ret;
547 unsigned int el;
549 ARMCPU *cpu = ARM_CPU(cs);
550 CPUARMState *env = &cpu->env;
552 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
553 * AArch64 registers before pushing them out to 64-bit KVM.
555 if (!is_a64(env)) {
556 aarch64_sync_32_to_64(env);
559 for (i = 0; i < 31; i++) {
560 reg.id = AARCH64_CORE_REG(regs.regs[i]);
561 reg.addr = (uintptr_t) &env->xregs[i];
562 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
563 if (ret) {
564 return ret;
568 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
569 * QEMU side we keep the current SP in xregs[31] as well.
571 aarch64_save_sp(env, 1);
573 reg.id = AARCH64_CORE_REG(regs.sp);
574 reg.addr = (uintptr_t) &env->sp_el[0];
575 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
576 if (ret) {
577 return ret;
580 reg.id = AARCH64_CORE_REG(sp_el1);
581 reg.addr = (uintptr_t) &env->sp_el[1];
582 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
583 if (ret) {
584 return ret;
587 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
588 if (is_a64(env)) {
589 val = pstate_read(env);
590 } else {
591 val = cpsr_read(env);
593 reg.id = AARCH64_CORE_REG(regs.pstate);
594 reg.addr = (uintptr_t) &val;
595 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
596 if (ret) {
597 return ret;
600 reg.id = AARCH64_CORE_REG(regs.pc);
601 reg.addr = (uintptr_t) &env->pc;
602 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
603 if (ret) {
604 return ret;
607 reg.id = AARCH64_CORE_REG(elr_el1);
608 reg.addr = (uintptr_t) &env->elr_el[1];
609 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
610 if (ret) {
611 return ret;
614 /* Saved Program State Registers
616 * Before we restore from the banked_spsr[] array we need to
617 * ensure that any modifications to env->spsr are correctly
618 * reflected in the banks.
620 el = arm_current_el(env);
621 if (el > 0 && !is_a64(env)) {
622 i = bank_number(env->uncached_cpsr & CPSR_M);
623 env->banked_spsr[i] = env->spsr;
626 /* KVM 0-4 map to QEMU banks 1-5 */
627 for (i = 0; i < KVM_NR_SPSR; i++) {
628 reg.id = AARCH64_CORE_REG(spsr[i]);
629 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
630 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
631 if (ret) {
632 return ret;
636 /* Advanced SIMD and FP registers
637 * We map Qn = regs[2n+1]:regs[2n]
639 for (i = 0; i < 32; i++) {
640 int rd = i << 1;
641 uint64_t fp_val[2];
642 #ifdef HOST_WORDS_BIGENDIAN
643 fp_val[0] = env->vfp.regs[rd + 1];
644 fp_val[1] = env->vfp.regs[rd];
645 #else
646 fp_val[1] = env->vfp.regs[rd + 1];
647 fp_val[0] = env->vfp.regs[rd];
648 #endif
649 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
650 reg.addr = (uintptr_t)(&fp_val);
651 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
652 if (ret) {
653 return ret;
657 reg.addr = (uintptr_t)(&fpr);
658 fpr = vfp_get_fpsr(env);
659 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
660 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
661 if (ret) {
662 return ret;
665 fpr = vfp_get_fpcr(env);
666 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
667 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
668 if (ret) {
669 return ret;
672 if (!write_list_to_kvmstate(cpu, level)) {
673 return EINVAL;
676 kvm_arm_sync_mpstate_to_kvm(cpu);
678 return ret;
681 int kvm_arch_get_registers(CPUState *cs)
683 struct kvm_one_reg reg;
684 uint64_t val;
685 uint32_t fpr;
686 unsigned int el;
687 int i;
688 int ret;
690 ARMCPU *cpu = ARM_CPU(cs);
691 CPUARMState *env = &cpu->env;
693 for (i = 0; i < 31; i++) {
694 reg.id = AARCH64_CORE_REG(regs.regs[i]);
695 reg.addr = (uintptr_t) &env->xregs[i];
696 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
697 if (ret) {
698 return ret;
702 reg.id = AARCH64_CORE_REG(regs.sp);
703 reg.addr = (uintptr_t) &env->sp_el[0];
704 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
705 if (ret) {
706 return ret;
709 reg.id = AARCH64_CORE_REG(sp_el1);
710 reg.addr = (uintptr_t) &env->sp_el[1];
711 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
712 if (ret) {
713 return ret;
716 reg.id = AARCH64_CORE_REG(regs.pstate);
717 reg.addr = (uintptr_t) &val;
718 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
719 if (ret) {
720 return ret;
723 env->aarch64 = ((val & PSTATE_nRW) == 0);
724 if (is_a64(env)) {
725 pstate_write(env, val);
726 } else {
727 env->uncached_cpsr = val & CPSR_M;
728 cpsr_write(env, val, 0xffffffff);
731 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
732 * QEMU side we keep the current SP in xregs[31] as well.
734 aarch64_restore_sp(env, 1);
736 reg.id = AARCH64_CORE_REG(regs.pc);
737 reg.addr = (uintptr_t) &env->pc;
738 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
739 if (ret) {
740 return ret;
743 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
744 * incoming AArch64 regs received from 64-bit KVM.
745 * We must perform this after all of the registers have been acquired from
746 * the kernel.
748 if (!is_a64(env)) {
749 aarch64_sync_64_to_32(env);
752 reg.id = AARCH64_CORE_REG(elr_el1);
753 reg.addr = (uintptr_t) &env->elr_el[1];
754 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
755 if (ret) {
756 return ret;
759 /* Fetch the SPSR registers
761 * KVM SPSRs 0-4 map to QEMU banks 1-5
763 for (i = 0; i < KVM_NR_SPSR; i++) {
764 reg.id = AARCH64_CORE_REG(spsr[i]);
765 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
766 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
767 if (ret) {
768 return ret;
772 el = arm_current_el(env);
773 if (el > 0 && !is_a64(env)) {
774 i = bank_number(env->uncached_cpsr & CPSR_M);
775 env->spsr = env->banked_spsr[i];
778 /* Advanced SIMD and FP registers
779 * We map Qn = regs[2n+1]:regs[2n]
781 for (i = 0; i < 32; i++) {
782 uint64_t fp_val[2];
783 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
784 reg.addr = (uintptr_t)(&fp_val);
785 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
786 if (ret) {
787 return ret;
788 } else {
789 int rd = i << 1;
790 #ifdef HOST_WORDS_BIGENDIAN
791 env->vfp.regs[rd + 1] = fp_val[0];
792 env->vfp.regs[rd] = fp_val[1];
793 #else
794 env->vfp.regs[rd + 1] = fp_val[1];
795 env->vfp.regs[rd] = fp_val[0];
796 #endif
800 reg.addr = (uintptr_t)(&fpr);
801 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
802 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
803 if (ret) {
804 return ret;
806 vfp_set_fpsr(env, fpr);
808 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
809 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
810 if (ret) {
811 return ret;
813 vfp_set_fpcr(env, fpr);
815 if (!write_kvmstate_to_list(cpu)) {
816 return EINVAL;
818 /* Note that it's OK to have registers which aren't in CPUState,
819 * so we can ignore a failure return here.
821 write_list_to_cpustate(cpu);
823 kvm_arm_sync_mpstate_to_qemu(cpu);
825 /* TODO: other registers */
826 return ret;
829 /* C6.6.29 BRK instruction */
830 static const uint32_t brk_insn = 0xd4200000;
832 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
834 if (have_guest_debug) {
835 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
836 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk_insn, 4, 1)) {
837 return -EINVAL;
839 return 0;
840 } else {
841 error_report("guest debug not supported on this kernel");
842 return -EINVAL;
846 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
848 static uint32_t brk;
850 if (have_guest_debug) {
851 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk, 4, 0) ||
852 brk != brk_insn ||
853 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) {
854 return -EINVAL;
856 return 0;
857 } else {
858 error_report("guest debug not supported on this kernel");
859 return -EINVAL;
863 /* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
865 * To minimise translating between kernel and user-space the kernel
866 * ABI just provides user-space with the full exception syndrome
867 * register value to be decoded in QEMU.
870 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
872 int hsr_ec = debug_exit->hsr >> ARM_EL_EC_SHIFT;
873 ARMCPU *cpu = ARM_CPU(cs);
874 CPUClass *cc = CPU_GET_CLASS(cs);
875 CPUARMState *env = &cpu->env;
877 /* Ensure PC is synchronised */
878 kvm_cpu_synchronize_state(cs);
880 switch (hsr_ec) {
881 case EC_SOFTWARESTEP:
882 if (cs->singlestep_enabled) {
883 return true;
884 } else {
886 * The kernel should have suppressed the guest's ability to
887 * single step at this point so something has gone wrong.
889 error_report("%s: guest single-step while debugging unsupported"
890 " (%"PRIx64", %"PRIx32")\n",
891 __func__, env->pc, debug_exit->hsr);
892 return false;
894 break;
895 case EC_AA64_BKPT:
896 if (kvm_find_sw_breakpoint(cs, env->pc)) {
897 return true;
899 break;
900 case EC_BREAKPOINT:
901 if (find_hw_breakpoint(cs, env->pc)) {
902 return true;
904 break;
905 case EC_WATCHPOINT:
907 CPUWatchpoint *wp = find_hw_watchpoint(cs, debug_exit->far);
908 if (wp) {
909 cs->watchpoint_hit = wp;
910 return true;
912 break;
914 default:
915 error_report("%s: unhandled debug exit (%"PRIx32", %"PRIx64")\n",
916 __func__, debug_exit->hsr, env->pc);
919 /* If we are not handling the debug exception it must belong to
920 * the guest. Let's re-use the existing TCG interrupt code to set
921 * everything up properly.
923 cs->exception_index = EXCP_BKPT;
924 env->exception.syndrome = debug_exit->hsr;
925 env->exception.vaddress = debug_exit->far;
926 cc->do_interrupt(cs);
928 return false;