]> Git Repo - qemu.git/blame - target/arm/kvm64.c
target/arm64: hold BQL when calling do_interrupt()
[qemu.git] / target / arm / kvm64.c
CommitLineData
26861c7c
MH
1/*
2 * ARM implementation of KVM hooks, 64 bit specific code
3 *
4 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems
e4482ab7 5 * Copyright Alex Bennée 2014, Linaro
26861c7c
MH
6 *
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.
9 *
10 */
11
74c21bd0 12#include "qemu/osdep.h"
26861c7c 13#include <sys/ioctl.h>
e4482ab7 14#include <sys/ptrace.h>
26861c7c 15
e4482ab7 16#include <linux/elf.h>
26861c7c
MH
17#include <linux/kvm.h>
18
19#include "qemu-common.h"
33c11879 20#include "cpu.h"
26861c7c 21#include "qemu/timer.h"
2ecb2027 22#include "qemu/error-report.h"
e4482ab7
AB
23#include "qemu/host-utils.h"
24#include "exec/gdbstub.h"
26861c7c
MH
25#include "sysemu/sysemu.h"
26#include "sysemu/kvm.h"
27#include "kvm_arm.h"
9208b961 28#include "internals.h"
26861c7c
MH
29#include "hw/arm/arm.h"
30
29eb3d9a
AB
31static bool have_guest_debug;
32
e4482ab7
AB
33/*
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.
39 *
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.
43 *
44 * This is not unreasonable as most of the time debugging kernels you
45 * never know which core will eventually execute your function.
46 */
47
48typedef struct {
49 uint64_t bcr;
50 uint64_t bvr;
51} HWBreakpoint;
52
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.
57 */
58typedef struct {
59 uint64_t wcr;
60 uint64_t wvr;
61 CPUWatchpoint details;
62} HWWatchpoint;
63
64/* Maximum and current break/watch point counts */
65int max_hw_bps, max_hw_wps;
66GArray *hw_breakpoints, *hw_watchpoints;
67
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))
72
29eb3d9a 73/**
e4482ab7 74 * kvm_arm_init_debug() - check for guest debug capabilities
29eb3d9a
AB
75 * @cs: CPUState
76 *
e4482ab7
AB
77 * kvm_check_extension returns the number of debug registers we have
78 * or 0 if we have none.
29eb3d9a
AB
79 *
80 */
81static void kvm_arm_init_debug(CPUState *cs)
82{
83 have_guest_debug = kvm_check_extension(cs->kvm_state,
84 KVM_CAP_SET_GUEST_DEBUG);
e4482ab7
AB
85
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);
89
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);
29eb3d9a
AB
93 return;
94}
95
e4482ab7
AB
96/**
97 * insert_hw_breakpoint()
98 * @addr: address of breakpoint
99 *
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
105 *
864df205 106 * DBGBCR<n>_EL1, Debug Breakpoint Control Registers
e4482ab7
AB
107 *
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 * +------+------+-------+-----+----+------+-----+------+-----+---+
112 *
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)
117 * E: Enable bit
864df205
AB
118 *
119 * DBGBVR<n>_EL1, Debug Breakpoint Value Registers
120 *
121 * 63 53 52 49 48 2 1 0
122 * +------+-----------+----------+-----+
123 * | RESS | VA[52:49] | VA[48:2] | 0 0 |
124 * +------+-----------+----------+-----+
125 *
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.
e4482ab7 130 */
864df205 131
e4482ab7
AB
132static int insert_hw_breakpoint(target_ulong addr)
133{
134 HWBreakpoint brk = {
135 .bcr = 0x1, /* BCR E=1, enable */
864df205 136 .bvr = sextract64(addr, 0, 53)
e4482ab7
AB
137 };
138
139 if (cur_hw_bps >= max_hw_bps) {
140 return -ENOBUFS;
141 }
142
143 brk.bcr = deposit32(brk.bcr, 1, 2, 0x3); /* PMC = 11 */
144 brk.bcr = deposit32(brk.bcr, 5, 4, 0xf); /* BAS = RES1 */
145
146 g_array_append_val(hw_breakpoints, brk);
147
148 return 0;
149}
150
151/**
152 * delete_hw_breakpoint()
153 * @pc: address of breakpoint
154 *
155 * Delete a breakpoint and shuffle any above down
156 */
157
158static int delete_hw_breakpoint(target_ulong pc)
159{
160 int i;
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);
165 return 0;
166 }
167 }
168 return -ENOENT;
169}
170
171/**
172 * insert_hw_watchpoint()
173 * @addr: address of watch point
174 * @len: size of area
175 * @type: type of watch point
176 *
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.
181 *
182 * D7.3.11 DBGWCR<n>_EL1, Debug Watchpoint Control Registers
183 *
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 * +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+
188 *
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)
195 * E: Enable
196 *
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
201 */
202
203static int insert_hw_watchpoint(target_ulong addr,
204 target_ulong len, int type)
205{
206 HWWatchpoint wp = {
207 .wcr = 1, /* E=1, enable */
208 .wvr = addr & (~0x7ULL),
209 .details = { .vaddr = addr, .len = len }
210 };
211
212 if (cur_hw_wps >= max_hw_wps) {
213 return -ENOBUFS;
214 }
215
216 /*
217 * HMC=0 SSC=0 PAC=3 will hit EL0 or EL1, any security state,
218 * valid whether EL3 is implemented or not
219 */
220 wp.wcr = deposit32(wp.wcr, 1, 2, 3);
221
222 switch (type) {
223 case GDB_WATCHPOINT_READ:
224 wp.wcr = deposit32(wp.wcr, 3, 2, 1);
225 wp.details.flags = BP_MEM_READ;
226 break;
227 case GDB_WATCHPOINT_WRITE:
228 wp.wcr = deposit32(wp.wcr, 3, 2, 2);
229 wp.details.flags = BP_MEM_WRITE;
230 break;
231 case GDB_WATCHPOINT_ACCESS:
232 wp.wcr = deposit32(wp.wcr, 3, 2, 3);
233 wp.details.flags = BP_MEM_ACCESS;
234 break;
235 default:
236 g_assert_not_reached();
237 break;
238 }
239 if (len <= 8) {
240 /* we align the address and set the bits in BAS */
241 int off = addr & 0x7;
242 int bas = (1 << len) - 1;
243
244 wp.wcr = deposit32(wp.wcr, 5 + off, 8 - off, bas);
245 } else {
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);
249
250 wp.wvr &= ~((1 << bits) - 1);
251 wp.wcr = deposit32(wp.wcr, 24, 4, bits);
252 wp.wcr = deposit32(wp.wcr, 5, 8, 0xff);
253 } else {
254 return -ENOBUFS;
255 }
256 }
257
258 g_array_append_val(hw_watchpoints, wp);
259 return 0;
260}
261
262
263static bool check_watchpoint_in_range(int i, target_ulong addr)
264{
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);
269
270 if (mask) {
271 addr_top = addr_bottom + (1 << mask);
272 } else {
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);
277 }
278
279 if (addr >= addr_bottom && addr <= addr_top) {
280 return true;
281 }
282
283 return false;
284}
285
286/**
287 * delete_hw_watchpoint()
288 * @addr: address of breakpoint
289 *
290 * Delete a breakpoint and shuffle any above down
291 */
292
293static int delete_hw_watchpoint(target_ulong addr,
294 target_ulong len, int type)
295{
296 int i;
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);
300 return 0;
301 }
302 }
303 return -ENOENT;
304}
305
306
307int kvm_arch_insert_hw_breakpoint(target_ulong addr,
308 target_ulong len, int type)
309{
310 switch (type) {
311 case GDB_BREAKPOINT_HW:
312 return insert_hw_breakpoint(addr);
313 break;
314 case GDB_WATCHPOINT_READ:
315 case GDB_WATCHPOINT_WRITE:
316 case GDB_WATCHPOINT_ACCESS:
317 return insert_hw_watchpoint(addr, len, type);
318 default:
319 return -ENOSYS;
320 }
321}
322
323int kvm_arch_remove_hw_breakpoint(target_ulong addr,
324 target_ulong len, int type)
325{
326 switch (type) {
327 case GDB_BREAKPOINT_HW:
328 return delete_hw_breakpoint(addr);
329 break;
330 case GDB_WATCHPOINT_READ:
331 case GDB_WATCHPOINT_WRITE:
332 case GDB_WATCHPOINT_ACCESS:
333 return delete_hw_watchpoint(addr, len, type);
334 default:
335 return -ENOSYS;
336 }
337}
338
339
340void kvm_arch_remove_all_hw_breakpoints(void)
341{
342 if (cur_hw_wps > 0) {
343 g_array_remove_range(hw_watchpoints, 0, cur_hw_wps);
344 }
345 if (cur_hw_bps > 0) {
346 g_array_remove_range(hw_breakpoints, 0, cur_hw_bps);
347 }
348}
349
350void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr)
351{
352 int i;
353 memset(ptr, 0, sizeof(struct kvm_guest_debug_arch));
354
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;
359 }
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;
364 }
365}
366
367bool kvm_arm_hw_debug_active(CPUState *cs)
368{
369 return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
370}
371
372static bool find_hw_breakpoint(CPUState *cpu, target_ulong pc)
373{
374 int i;
375
376 for (i = 0; i < cur_hw_bps; i++) {
377 HWBreakpoint *bp = get_hw_bp(i);
378 if (bp->bvr == pc) {
379 return true;
380 }
381 }
382 return false;
383}
384
385static CPUWatchpoint *find_hw_watchpoint(CPUState *cpu, target_ulong addr)
386{
387 int i;
388
389 for (i = 0; i < cur_hw_wps; i++) {
390 if (check_watchpoint_in_range(i, addr)) {
391 return &get_hw_wp(i)->details;
392 }
393 }
394 return NULL;
395}
396
3f07cb2a 397static bool kvm_arm_pmu_set_attr(CPUState *cs, struct kvm_device_attr *attr)
01fe6b60
SZ
398{
399 int err;
400
3f07cb2a
AJ
401 err = kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, attr);
402 if (err != 0) {
b2bfe9f7 403 error_report("PMU: KVM_HAS_DEVICE_ATTR: %s", strerror(-err));
3f07cb2a 404 return false;
01fe6b60
SZ
405 }
406
3f07cb2a 407 err = kvm_vcpu_ioctl(cs, KVM_SET_DEVICE_ATTR, attr);
b2bfe9f7
AJ
408 if (err != 0) {
409 error_report("PMU: KVM_SET_DEVICE_ATTR: %s", strerror(-err));
410 return false;
01fe6b60
SZ
411 }
412
3f07cb2a
AJ
413 return true;
414}
01fe6b60 415
b2bfe9f7 416void kvm_arm_pmu_init(CPUState *cs)
3f07cb2a
AJ
417{
418 struct kvm_device_attr attr = {
419 .group = KVM_ARM_VCPU_PMU_V3_CTRL,
420 .attr = KVM_ARM_VCPU_PMU_V3_INIT,
421 };
422
b2bfe9f7
AJ
423 if (!ARM_CPU(cs)->has_pmu) {
424 return;
425 }
426 if (!kvm_arm_pmu_set_attr(cs, &attr)) {
427 error_report("failed to init PMU");
428 abort();
429 }
3f07cb2a
AJ
430}
431
b2bfe9f7 432void kvm_arm_pmu_set_irq(CPUState *cs, int irq)
3f07cb2a
AJ
433{
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,
438 };
01fe6b60 439
b2bfe9f7
AJ
440 if (!ARM_CPU(cs)->has_pmu) {
441 return;
442 }
443 if (!kvm_arm_pmu_set_attr(cs, &attr)) {
444 error_report("failed to set irq for PMU");
445 abort();
446 }
01fe6b60 447}
e4482ab7 448
26861c7c
MH
449static inline void set_feature(uint64_t *features, int feature)
450{
451 *features |= 1ULL << feature;
452}
453
929e754d
WH
454static inline void unset_feature(uint64_t *features, int feature)
455{
456 *features &= ~(1ULL << feature);
457}
458
c4487d76 459bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
26861c7c
MH
460{
461 /* Identify the feature bits corresponding to the host CPU, and
462 * fill out the ARMHostCPUClass fields accordingly. To do this
463 * we have to create a scratch VM, create a single CPU inside it,
464 * and then query that CPU for the relevant ID registers.
465 * For AArch64 we currently don't care about ID registers at
466 * all; we just want to know the CPU type.
467 */
468 int fdarray[3];
469 uint64_t features = 0;
470 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
471 * we know these will only support creating one kind of guest CPU,
472 * which is its preferred CPU type. Fortunately these old kernels
473 * support only a very limited number of CPUs.
474 */
475 static const uint32_t cpus_to_try[] = {
476 KVM_ARM_TARGET_AEM_V8,
477 KVM_ARM_TARGET_FOUNDATION_V8,
478 KVM_ARM_TARGET_CORTEX_A57,
479 QEMU_KVM_ARM_TARGET_NONE
480 };
481 struct kvm_vcpu_init init;
482
483 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
484 return false;
485 }
486
c4487d76
PM
487 ahcf->target = init.target;
488 ahcf->dtb_compatible = "arm,arm-v8";
26861c7c
MH
489
490 kvm_arm_destroy_scratch_host_vcpu(fdarray);
491
492 /* We can assume any KVM supporting CPU is at least a v8
493 * with VFPv4+Neon; this in turn implies most of the other
494 * feature bits.
495 */
496 set_feature(&features, ARM_FEATURE_V8);
497 set_feature(&features, ARM_FEATURE_VFP4);
498 set_feature(&features, ARM_FEATURE_NEON);
499 set_feature(&features, ARM_FEATURE_AARCH64);
929e754d 500 set_feature(&features, ARM_FEATURE_PMU);
26861c7c 501
c4487d76 502 ahcf->features = features;
26861c7c
MH
503
504 return true;
505}
506
eb5e1d3c
PF
507#define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
508
26861c7c
MH
509int kvm_arch_init_vcpu(CPUState *cs)
510{
26861c7c 511 int ret;
eb5e1d3c 512 uint64_t mpidr;
228d5e04 513 ARMCPU *cpu = ARM_CPU(cs);
929e754d 514 CPUARMState *env = &cpu->env;
26861c7c
MH
515
516 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
56073970 517 !object_dynamic_cast(OBJECT(cpu), TYPE_AARCH64_CPU)) {
26861c7c
MH
518 fprintf(stderr, "KVM is not supported for this guest CPU type\n");
519 return -EINVAL;
520 }
521
228d5e04
PS
522 /* Determine init features for this CPU */
523 memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
26861c7c 524 if (cpu->start_powered_off) {
228d5e04
PS
525 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
526 }
7cd62e53 527 if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
dd032e34 528 cpu->psci_version = 2;
7cd62e53
PS
529 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
530 }
56073970
GB
531 if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
532 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
533 }
b1659527 534 if (!kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) {
929e754d
WH
535 cpu->has_pmu = false;
536 }
537 if (cpu->has_pmu) {
5c0a3819 538 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
929e754d
WH
539 } else {
540 unset_feature(&env->features, ARM_FEATURE_PMU);
5c0a3819 541 }
228d5e04
PS
542
543 /* Do KVM_ARM_VCPU_INIT ioctl */
544 ret = kvm_arm_vcpu_init(cs);
545 if (ret) {
546 return ret;
26861c7c 547 }
26861c7c 548
eb5e1d3c
PF
549 /*
550 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
551 * Currently KVM has its own idea about MPIDR assignment, so we
552 * override our defaults with what we get from KVM.
553 */
554 ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
555 if (ret) {
556 return ret;
557 }
0f4a9e45 558 cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
eb5e1d3c 559
29eb3d9a
AB
560 kvm_arm_init_debug(cs);
561
202ccb6b
DG
562 /* Check whether user space can specify guest syndrome value */
563 kvm_arm_init_serror_injection(cs);
564
38df27c8
AB
565 return kvm_arm_init_cpreg_list(cpu);
566}
26861c7c 567
38df27c8
AB
568bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
569{
570 /* Return true if the regidx is a register we should synchronize
571 * via the cpreg_tuples array (ie is not a core reg we sync by
572 * hand in kvm_arch_get/put_registers())
573 */
574 switch (regidx & KVM_REG_ARM_COPROC_MASK) {
575 case KVM_REG_ARM_CORE:
576 return false;
577 default:
578 return true;
579 }
26861c7c
MH
580}
581
4b7a6bf4
CD
582typedef struct CPRegStateLevel {
583 uint64_t regidx;
584 int level;
585} CPRegStateLevel;
586
587/* All system registers not listed in the following table are assumed to be
588 * of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
589 * often, you must add it to this table with a state of either
590 * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
591 */
592static const CPRegStateLevel non_runtime_cpregs[] = {
593 { KVM_REG_ARM_TIMER_CNT, KVM_PUT_FULL_STATE },
594};
595
596int kvm_arm_cpreg_level(uint64_t regidx)
597{
598 int i;
599
600 for (i = 0; i < ARRAY_SIZE(non_runtime_cpregs); i++) {
601 const CPRegStateLevel *l = &non_runtime_cpregs[i];
602 if (l->regidx == regidx) {
603 return l->level;
604 }
605 }
606
607 return KVM_PUT_RUNTIME_STATE;
608}
609
26861c7c
MH
610#define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
611 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
612
0e4b5869
AB
613#define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
614 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
615
616#define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
617 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
618
26861c7c
MH
619int kvm_arch_put_registers(CPUState *cs, int level)
620{
621 struct kvm_one_reg reg;
0e4b5869 622 uint32_t fpr;
26861c7c
MH
623 uint64_t val;
624 int i;
625 int ret;
25b9fb10 626 unsigned int el;
26861c7c
MH
627
628 ARMCPU *cpu = ARM_CPU(cs);
629 CPUARMState *env = &cpu->env;
630
56073970
GB
631 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
632 * AArch64 registers before pushing them out to 64-bit KVM.
633 */
634 if (!is_a64(env)) {
635 aarch64_sync_32_to_64(env);
636 }
637
26861c7c
MH
638 for (i = 0; i < 31; i++) {
639 reg.id = AARCH64_CORE_REG(regs.regs[i]);
640 reg.addr = (uintptr_t) &env->xregs[i];
641 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
642 if (ret) {
643 return ret;
644 }
645 }
646
f502cfc2
PM
647 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
648 * QEMU side we keep the current SP in xregs[31] as well.
649 */
9208b961 650 aarch64_save_sp(env, 1);
f502cfc2 651
26861c7c 652 reg.id = AARCH64_CORE_REG(regs.sp);
f502cfc2
PM
653 reg.addr = (uintptr_t) &env->sp_el[0];
654 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
655 if (ret) {
656 return ret;
657 }
658
659 reg.id = AARCH64_CORE_REG(sp_el1);
660 reg.addr = (uintptr_t) &env->sp_el[1];
26861c7c
MH
661 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
662 if (ret) {
663 return ret;
664 }
665
666 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
56073970
GB
667 if (is_a64(env)) {
668 val = pstate_read(env);
669 } else {
670 val = cpsr_read(env);
671 }
26861c7c
MH
672 reg.id = AARCH64_CORE_REG(regs.pstate);
673 reg.addr = (uintptr_t) &val;
674 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
675 if (ret) {
676 return ret;
677 }
678
679 reg.id = AARCH64_CORE_REG(regs.pc);
680 reg.addr = (uintptr_t) &env->pc;
681 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
682 if (ret) {
683 return ret;
684 }
685
a0618a19 686 reg.id = AARCH64_CORE_REG(elr_el1);
6947f059 687 reg.addr = (uintptr_t) &env->elr_el[1];
a0618a19
PM
688 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
689 if (ret) {
690 return ret;
691 }
692
25b9fb10
AB
693 /* Saved Program State Registers
694 *
695 * Before we restore from the banked_spsr[] array we need to
696 * ensure that any modifications to env->spsr are correctly
697 * reflected in the banks.
698 */
699 el = arm_current_el(env);
700 if (el > 0 && !is_a64(env)) {
701 i = bank_number(env->uncached_cpsr & CPSR_M);
702 env->banked_spsr[i] = env->spsr;
703 }
704
705 /* KVM 0-4 map to QEMU banks 1-5 */
a65f1de9
PM
706 for (i = 0; i < KVM_NR_SPSR; i++) {
707 reg.id = AARCH64_CORE_REG(spsr[i]);
25b9fb10 708 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
a65f1de9
PM
709 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
710 if (ret) {
711 return ret;
712 }
713 }
714
9a2b5256 715 /* Advanced SIMD and FP registers. */
0e4b5869 716 for (i = 0; i < 32; i++) {
9a2b5256 717 uint64_t *q = aa64_vfp_qreg(env, i);
0e4b5869 718#ifdef HOST_WORDS_BIGENDIAN
9a2b5256
RH
719 uint64_t fp_val[2] = { q[1], q[0] };
720 reg.addr = (uintptr_t)fp_val;
0e4b5869 721#else
9a2b5256 722 reg.addr = (uintptr_t)q;
0e4b5869
AB
723#endif
724 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
0e4b5869
AB
725 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
726 if (ret) {
727 return ret;
728 }
729 }
730
731 reg.addr = (uintptr_t)(&fpr);
732 fpr = vfp_get_fpsr(env);
733 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
734 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
735 if (ret) {
736 return ret;
737 }
738
739 fpr = vfp_get_fpcr(env);
740 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
741 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
742 if (ret) {
743 return ret;
744 }
745
202ccb6b
DG
746 ret = kvm_put_vcpu_events(cpu);
747 if (ret) {
748 return ret;
749 }
750
4b7a6bf4 751 if (!write_list_to_kvmstate(cpu, level)) {
568bab1f
PS
752 return EINVAL;
753 }
754
1a1753f7
AB
755 kvm_arm_sync_mpstate_to_kvm(cpu);
756
26861c7c
MH
757 return ret;
758}
759
760int kvm_arch_get_registers(CPUState *cs)
761{
762 struct kvm_one_reg reg;
763 uint64_t val;
0e4b5869 764 uint32_t fpr;
25b9fb10 765 unsigned int el;
26861c7c
MH
766 int i;
767 int ret;
768
769 ARMCPU *cpu = ARM_CPU(cs);
770 CPUARMState *env = &cpu->env;
771
772 for (i = 0; i < 31; i++) {
773 reg.id = AARCH64_CORE_REG(regs.regs[i]);
774 reg.addr = (uintptr_t) &env->xregs[i];
775 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
776 if (ret) {
777 return ret;
778 }
779 }
780
781 reg.id = AARCH64_CORE_REG(regs.sp);
f502cfc2
PM
782 reg.addr = (uintptr_t) &env->sp_el[0];
783 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
784 if (ret) {
785 return ret;
786 }
787
788 reg.id = AARCH64_CORE_REG(sp_el1);
789 reg.addr = (uintptr_t) &env->sp_el[1];
26861c7c
MH
790 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
791 if (ret) {
792 return ret;
793 }
794
795 reg.id = AARCH64_CORE_REG(regs.pstate);
796 reg.addr = (uintptr_t) &val;
797 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
798 if (ret) {
799 return ret;
800 }
56073970
GB
801
802 env->aarch64 = ((val & PSTATE_nRW) == 0);
803 if (is_a64(env)) {
804 pstate_write(env, val);
805 } else {
50866ba5 806 cpsr_write(env, val, 0xffffffff, CPSRWriteRaw);
56073970 807 }
26861c7c 808
f502cfc2
PM
809 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
810 * QEMU side we keep the current SP in xregs[31] as well.
811 */
9208b961 812 aarch64_restore_sp(env, 1);
f502cfc2 813
26861c7c
MH
814 reg.id = AARCH64_CORE_REG(regs.pc);
815 reg.addr = (uintptr_t) &env->pc;
816 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
817 if (ret) {
818 return ret;
819 }
820
56073970
GB
821 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
822 * incoming AArch64 regs received from 64-bit KVM.
823 * We must perform this after all of the registers have been acquired from
824 * the kernel.
825 */
826 if (!is_a64(env)) {
827 aarch64_sync_64_to_32(env);
828 }
829
a0618a19 830 reg.id = AARCH64_CORE_REG(elr_el1);
6947f059 831 reg.addr = (uintptr_t) &env->elr_el[1];
a0618a19
PM
832 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
833 if (ret) {
834 return ret;
835 }
836
25b9fb10
AB
837 /* Fetch the SPSR registers
838 *
839 * KVM SPSRs 0-4 map to QEMU banks 1-5
840 */
a65f1de9
PM
841 for (i = 0; i < KVM_NR_SPSR; i++) {
842 reg.id = AARCH64_CORE_REG(spsr[i]);
25b9fb10 843 reg.addr = (uintptr_t) &env->banked_spsr[i + 1];
a65f1de9
PM
844 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
845 if (ret) {
846 return ret;
847 }
848 }
849
25b9fb10
AB
850 el = arm_current_el(env);
851 if (el > 0 && !is_a64(env)) {
852 i = bank_number(env->uncached_cpsr & CPSR_M);
853 env->spsr = env->banked_spsr[i];
854 }
855
9a2b5256 856 /* Advanced SIMD and FP registers */
0e4b5869 857 for (i = 0; i < 32; i++) {
9a2b5256 858 uint64_t *q = aa64_vfp_qreg(env, i);
0e4b5869 859 reg.id = AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]);
9a2b5256 860 reg.addr = (uintptr_t)q;
0e4b5869
AB
861 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
862 if (ret) {
863 return ret;
864 } else {
0e4b5869 865#ifdef HOST_WORDS_BIGENDIAN
9a2b5256
RH
866 uint64_t t;
867 t = q[0], q[0] = q[1], q[1] = t;
0e4b5869
AB
868#endif
869 }
870 }
871
872 reg.addr = (uintptr_t)(&fpr);
873 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpsr);
874 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
875 if (ret) {
876 return ret;
877 }
878 vfp_set_fpsr(env, fpr);
879
880 reg.id = AARCH64_SIMD_CTRL_REG(fp_regs.fpcr);
881 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
882 if (ret) {
883 return ret;
884 }
885 vfp_set_fpcr(env, fpr);
886
202ccb6b
DG
887 ret = kvm_get_vcpu_events(cpu);
888 if (ret) {
889 return ret;
890 }
891
568bab1f
PS
892 if (!write_kvmstate_to_list(cpu)) {
893 return EINVAL;
894 }
895 /* Note that it's OK to have registers which aren't in CPUState,
896 * so we can ignore a failure return here.
897 */
898 write_list_to_cpustate(cpu);
899
1a1753f7
AB
900 kvm_arm_sync_mpstate_to_qemu(cpu);
901
26861c7c
MH
902 /* TODO: other registers */
903 return ret;
904}
2ecb2027
AB
905
906/* C6.6.29 BRK instruction */
907static const uint32_t brk_insn = 0xd4200000;
908
909int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
910{
911 if (have_guest_debug) {
912 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
913 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk_insn, 4, 1)) {
914 return -EINVAL;
915 }
916 return 0;
917 } else {
918 error_report("guest debug not supported on this kernel");
919 return -EINVAL;
920 }
921}
922
923int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
924{
925 static uint32_t brk;
926
927 if (have_guest_debug) {
928 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk, 4, 0) ||
929 brk != brk_insn ||
930 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) {
931 return -EINVAL;
932 }
933 return 0;
934 } else {
935 error_report("guest debug not supported on this kernel");
936 return -EINVAL;
937 }
938}
939
940/* See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
941 *
942 * To minimise translating between kernel and user-space the kernel
943 * ABI just provides user-space with the full exception syndrome
944 * register value to be decoded in QEMU.
945 */
946
947bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
948{
64b91e3f 949 int hsr_ec = syn_get_ec(debug_exit->hsr);
2ecb2027 950 ARMCPU *cpu = ARM_CPU(cs);
34c45d53 951 CPUClass *cc = CPU_GET_CLASS(cs);
2ecb2027
AB
952 CPUARMState *env = &cpu->env;
953
954 /* Ensure PC is synchronised */
955 kvm_cpu_synchronize_state(cs);
956
957 switch (hsr_ec) {
26ae5934
AB
958 case EC_SOFTWARESTEP:
959 if (cs->singlestep_enabled) {
960 return true;
961 } else {
34c45d53
AB
962 /*
963 * The kernel should have suppressed the guest's ability to
964 * single step at this point so something has gone wrong.
965 */
966 error_report("%s: guest single-step while debugging unsupported"
dffc5851 967 " (%"PRIx64", %"PRIx32")",
34c45d53
AB
968 __func__, env->pc, debug_exit->hsr);
969 return false;
26ae5934
AB
970 }
971 break;
2ecb2027
AB
972 case EC_AA64_BKPT:
973 if (kvm_find_sw_breakpoint(cs, env->pc)) {
974 return true;
975 }
976 break;
e4482ab7
AB
977 case EC_BREAKPOINT:
978 if (find_hw_breakpoint(cs, env->pc)) {
979 return true;
980 }
981 break;
982 case EC_WATCHPOINT:
983 {
984 CPUWatchpoint *wp = find_hw_watchpoint(cs, debug_exit->far);
985 if (wp) {
986 cs->watchpoint_hit = wp;
987 return true;
988 }
989 break;
990 }
2ecb2027 991 default:
dffc5851 992 error_report("%s: unhandled debug exit (%"PRIx32", %"PRIx64")",
2ecb2027
AB
993 __func__, debug_exit->hsr, env->pc);
994 }
995
34c45d53
AB
996 /* If we are not handling the debug exception it must belong to
997 * the guest. Let's re-use the existing TCG interrupt code to set
998 * everything up properly.
999 */
1000 cs->exception_index = EXCP_BKPT;
1001 env->exception.syndrome = debug_exit->hsr;
1002 env->exception.vaddress = debug_exit->far;
9b16ec43 1003 qemu_mutex_lock_iothread();
34c45d53 1004 cc->do_interrupt(cs);
9b16ec43 1005 qemu_mutex_unlock_iothread();
2ecb2027
AB
1006
1007 return false;
1008}
This page took 0.361595 seconds and 4 git commands to generate.