1 /* 2 * ARM implementation of KVM hooks 3 * 4 * Copyright Christoffer Dall 2009-2010 5 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems 6 * Copyright Alex Bennée 2014, Linaro 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or later. 9 * See the COPYING file in the top-level directory. 10 * 11 */ 12 13 #include "qemu/osdep.h" 14 #include <sys/ioctl.h> 15 16 #include <linux/kvm.h> 17 18 #include "qemu/timer.h" 19 #include "qemu/error-report.h" 20 #include "qemu/main-loop.h" 21 #include "qom/object.h" 22 #include "qapi/error.h" 23 #include "sysemu/sysemu.h" 24 #include "sysemu/runstate.h" 25 #include "sysemu/kvm.h" 26 #include "sysemu/kvm_int.h" 27 #include "kvm_arm.h" 28 #include "cpu.h" 29 #include "trace.h" 30 #include "internals.h" 31 #include "hw/pci/pci.h" 32 #include "exec/memattrs.h" 33 #include "exec/address-spaces.h" 34 #include "exec/gdbstub.h" 35 #include "hw/boards.h" 36 #include "hw/irq.h" 37 #include "qapi/visitor.h" 38 #include "qemu/log.h" 39 #include "hw/acpi/acpi.h" 40 #include "hw/acpi/ghes.h" 41 #include "target/arm/gtimer.h" 42 43 const KVMCapabilityInfo kvm_arch_required_capabilities[] = { 44 KVM_CAP_LAST_INFO 45 }; 46 47 static bool cap_has_mp_state; 48 static bool cap_has_inject_serror_esr; 49 static bool cap_has_inject_ext_dabt; 50 51 /** 52 * ARMHostCPUFeatures: information about the host CPU (identified 53 * by asking the host kernel) 54 */ 55 typedef struct ARMHostCPUFeatures { 56 ARMISARegisters isar; 57 uint64_t features; 58 uint32_t target; 59 const char *dtb_compatible; 60 } ARMHostCPUFeatures; 61 62 static ARMHostCPUFeatures arm_host_cpu_features; 63 64 /** 65 * kvm_arm_vcpu_init: 66 * @cpu: ARMCPU 67 * 68 * Initialize (or reinitialize) the VCPU by invoking the 69 * KVM_ARM_VCPU_INIT ioctl with the CPU type and feature 70 * bitmask specified in the CPUState. 71 * 72 * Returns: 0 if success else < 0 error code 73 */ 74 static int kvm_arm_vcpu_init(ARMCPU *cpu) 75 { 76 struct kvm_vcpu_init init; 77 78 init.target = cpu->kvm_target; 79 memcpy(init.features, cpu->kvm_init_features, sizeof(init.features)); 80 81 return kvm_vcpu_ioctl(CPU(cpu), KVM_ARM_VCPU_INIT, &init); 82 } 83 84 /** 85 * kvm_arm_vcpu_finalize: 86 * @cpu: ARMCPU 87 * @feature: feature to finalize 88 * 89 * Finalizes the configuration of the specified VCPU feature by 90 * invoking the KVM_ARM_VCPU_FINALIZE ioctl. Features requiring 91 * this are documented in the "KVM_ARM_VCPU_FINALIZE" section of 92 * KVM's API documentation. 93 * 94 * Returns: 0 if success else < 0 error code 95 */ 96 static int kvm_arm_vcpu_finalize(ARMCPU *cpu, int feature) 97 { 98 return kvm_vcpu_ioctl(CPU(cpu), KVM_ARM_VCPU_FINALIZE, &feature); 99 } 100 101 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try, 102 int *fdarray, 103 struct kvm_vcpu_init *init) 104 { 105 int ret = 0, kvmfd = -1, vmfd = -1, cpufd = -1; 106 int max_vm_pa_size; 107 108 kvmfd = qemu_open_old("/dev/kvm", O_RDWR); 109 if (kvmfd < 0) { 110 goto err; 111 } 112 max_vm_pa_size = ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_ARM_VM_IPA_SIZE); 113 if (max_vm_pa_size < 0) { 114 max_vm_pa_size = 0; 115 } 116 do { 117 vmfd = ioctl(kvmfd, KVM_CREATE_VM, max_vm_pa_size); 118 } while (vmfd == -1 && errno == EINTR); 119 if (vmfd < 0) { 120 goto err; 121 } 122 cpufd = ioctl(vmfd, KVM_CREATE_VCPU, 0); 123 if (cpufd < 0) { 124 goto err; 125 } 126 127 if (!init) { 128 /* Caller doesn't want the VCPU to be initialized, so skip it */ 129 goto finish; 130 } 131 132 if (init->target == -1) { 133 struct kvm_vcpu_init preferred; 134 135 ret = ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, &preferred); 136 if (!ret) { 137 init->target = preferred.target; 138 } 139 } 140 if (ret >= 0) { 141 ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, init); 142 if (ret < 0) { 143 goto err; 144 } 145 } else if (cpus_to_try) { 146 /* Old kernel which doesn't know about the 147 * PREFERRED_TARGET ioctl: we know it will only support 148 * creating one kind of guest CPU which is its preferred 149 * CPU type. 150 */ 151 struct kvm_vcpu_init try; 152 153 while (*cpus_to_try != QEMU_KVM_ARM_TARGET_NONE) { 154 try.target = *cpus_to_try++; 155 memcpy(try.features, init->features, sizeof(init->features)); 156 ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, &try); 157 if (ret >= 0) { 158 break; 159 } 160 } 161 if (ret < 0) { 162 goto err; 163 } 164 init->target = try.target; 165 } else { 166 /* Treat a NULL cpus_to_try argument the same as an empty 167 * list, which means we will fail the call since this must 168 * be an old kernel which doesn't support PREFERRED_TARGET. 169 */ 170 goto err; 171 } 172 173 finish: 174 fdarray[0] = kvmfd; 175 fdarray[1] = vmfd; 176 fdarray[2] = cpufd; 177 178 return true; 179 180 err: 181 if (cpufd >= 0) { 182 close(cpufd); 183 } 184 if (vmfd >= 0) { 185 close(vmfd); 186 } 187 if (kvmfd >= 0) { 188 close(kvmfd); 189 } 190 191 return false; 192 } 193 194 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray) 195 { 196 int i; 197 198 for (i = 2; i >= 0; i--) { 199 close(fdarray[i]); 200 } 201 } 202 203 static int read_sys_reg32(int fd, uint32_t *pret, uint64_t id) 204 { 205 uint64_t ret; 206 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)&ret }; 207 int err; 208 209 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64); 210 err = ioctl(fd, KVM_GET_ONE_REG, &idreg); 211 if (err < 0) { 212 return -1; 213 } 214 *pret = ret; 215 return 0; 216 } 217 218 static int read_sys_reg64(int fd, uint64_t *pret, uint64_t id) 219 { 220 struct kvm_one_reg idreg = { .id = id, .addr = (uintptr_t)pret }; 221 222 assert((id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64); 223 return ioctl(fd, KVM_GET_ONE_REG, &idreg); 224 } 225 226 static bool kvm_arm_pauth_supported(void) 227 { 228 return (kvm_check_extension(kvm_state, KVM_CAP_ARM_PTRAUTH_ADDRESS) && 229 kvm_check_extension(kvm_state, KVM_CAP_ARM_PTRAUTH_GENERIC)); 230 } 231 232 static bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf) 233 { 234 /* Identify the feature bits corresponding to the host CPU, and 235 * fill out the ARMHostCPUClass fields accordingly. To do this 236 * we have to create a scratch VM, create a single CPU inside it, 237 * and then query that CPU for the relevant ID registers. 238 */ 239 int fdarray[3]; 240 bool sve_supported; 241 bool pmu_supported = false; 242 uint64_t features = 0; 243 int err; 244 245 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however 246 * we know these will only support creating one kind of guest CPU, 247 * which is its preferred CPU type. Fortunately these old kernels 248 * support only a very limited number of CPUs. 249 */ 250 static const uint32_t cpus_to_try[] = { 251 KVM_ARM_TARGET_AEM_V8, 252 KVM_ARM_TARGET_FOUNDATION_V8, 253 KVM_ARM_TARGET_CORTEX_A57, 254 QEMU_KVM_ARM_TARGET_NONE 255 }; 256 /* 257 * target = -1 informs kvm_arm_create_scratch_host_vcpu() 258 * to use the preferred target 259 */ 260 struct kvm_vcpu_init init = { .target = -1, }; 261 262 /* 263 * Ask for SVE if supported, so that we can query ID_AA64ZFR0, 264 * which is otherwise RAZ. 265 */ 266 sve_supported = kvm_arm_sve_supported(); 267 if (sve_supported) { 268 init.features[0] |= 1 << KVM_ARM_VCPU_SVE; 269 } 270 271 /* 272 * Ask for Pointer Authentication if supported, so that we get 273 * the unsanitized field values for AA64ISAR1_EL1. 274 */ 275 if (kvm_arm_pauth_supported()) { 276 init.features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS | 277 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC); 278 } 279 280 if (kvm_arm_pmu_supported()) { 281 init.features[0] |= 1 << KVM_ARM_VCPU_PMU_V3; 282 pmu_supported = true; 283 } 284 285 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) { 286 return false; 287 } 288 289 ahcf->target = init.target; 290 ahcf->dtb_compatible = "arm,arm-v8"; 291 292 err = read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr0, 293 ARM64_SYS_REG(3, 0, 0, 4, 0)); 294 if (unlikely(err < 0)) { 295 /* 296 * Before v4.15, the kernel only exposed a limited number of system 297 * registers, not including any of the interesting AArch64 ID regs. 298 * For the most part we could leave these fields as zero with minimal 299 * effect, since this does not affect the values seen by the guest. 300 * 301 * However, it could cause problems down the line for QEMU, 302 * so provide a minimal v8.0 default. 303 * 304 * ??? Could read MIDR and use knowledge from cpu64.c. 305 * ??? Could map a page of memory into our temp guest and 306 * run the tiniest of hand-crafted kernels to extract 307 * the values seen by the guest. 308 * ??? Either of these sounds like too much effort just 309 * to work around running a modern host kernel. 310 */ 311 ahcf->isar.id_aa64pfr0 = 0x00000011; /* EL1&0, AArch64 only */ 312 err = 0; 313 } else { 314 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64pfr1, 315 ARM64_SYS_REG(3, 0, 0, 4, 1)); 316 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64smfr0, 317 ARM64_SYS_REG(3, 0, 0, 4, 5)); 318 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr0, 319 ARM64_SYS_REG(3, 0, 0, 5, 0)); 320 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64dfr1, 321 ARM64_SYS_REG(3, 0, 0, 5, 1)); 322 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar0, 323 ARM64_SYS_REG(3, 0, 0, 6, 0)); 324 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar1, 325 ARM64_SYS_REG(3, 0, 0, 6, 1)); 326 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64isar2, 327 ARM64_SYS_REG(3, 0, 0, 6, 2)); 328 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr0, 329 ARM64_SYS_REG(3, 0, 0, 7, 0)); 330 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr1, 331 ARM64_SYS_REG(3, 0, 0, 7, 1)); 332 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr2, 333 ARM64_SYS_REG(3, 0, 0, 7, 2)); 334 335 /* 336 * Note that if AArch32 support is not present in the host, 337 * the AArch32 sysregs are present to be read, but will 338 * return UNKNOWN values. This is neither better nor worse 339 * than skipping the reads and leaving 0, as we must avoid 340 * considering the values in every case. 341 */ 342 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr0, 343 ARM64_SYS_REG(3, 0, 0, 1, 0)); 344 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr1, 345 ARM64_SYS_REG(3, 0, 0, 1, 1)); 346 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_dfr0, 347 ARM64_SYS_REG(3, 0, 0, 1, 2)); 348 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr0, 349 ARM64_SYS_REG(3, 0, 0, 1, 4)); 350 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr1, 351 ARM64_SYS_REG(3, 0, 0, 1, 5)); 352 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr2, 353 ARM64_SYS_REG(3, 0, 0, 1, 6)); 354 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr3, 355 ARM64_SYS_REG(3, 0, 0, 1, 7)); 356 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar0, 357 ARM64_SYS_REG(3, 0, 0, 2, 0)); 358 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar1, 359 ARM64_SYS_REG(3, 0, 0, 2, 1)); 360 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar2, 361 ARM64_SYS_REG(3, 0, 0, 2, 2)); 362 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar3, 363 ARM64_SYS_REG(3, 0, 0, 2, 3)); 364 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar4, 365 ARM64_SYS_REG(3, 0, 0, 2, 4)); 366 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar5, 367 ARM64_SYS_REG(3, 0, 0, 2, 5)); 368 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr4, 369 ARM64_SYS_REG(3, 0, 0, 2, 6)); 370 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_isar6, 371 ARM64_SYS_REG(3, 0, 0, 2, 7)); 372 373 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr0, 374 ARM64_SYS_REG(3, 0, 0, 3, 0)); 375 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr1, 376 ARM64_SYS_REG(3, 0, 0, 3, 1)); 377 err |= read_sys_reg32(fdarray[2], &ahcf->isar.mvfr2, 378 ARM64_SYS_REG(3, 0, 0, 3, 2)); 379 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_pfr2, 380 ARM64_SYS_REG(3, 0, 0, 3, 4)); 381 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_dfr1, 382 ARM64_SYS_REG(3, 0, 0, 3, 5)); 383 err |= read_sys_reg32(fdarray[2], &ahcf->isar.id_mmfr5, 384 ARM64_SYS_REG(3, 0, 0, 3, 6)); 385 386 /* 387 * DBGDIDR is a bit complicated because the kernel doesn't 388 * provide an accessor for it in 64-bit mode, which is what this 389 * scratch VM is in, and there's no architected "64-bit sysreg 390 * which reads the same as the 32-bit register" the way there is 391 * for other ID registers. Instead we synthesize a value from the 392 * AArch64 ID_AA64DFR0, the same way the kernel code in 393 * arch/arm64/kvm/sys_regs.c:trap_dbgidr() does. 394 * We only do this if the CPU supports AArch32 at EL1. 395 */ 396 if (FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL1) >= 2) { 397 int wrps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, WRPS); 398 int brps = FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, BRPS); 399 int ctx_cmps = 400 FIELD_EX64(ahcf->isar.id_aa64dfr0, ID_AA64DFR0, CTX_CMPS); 401 int version = 6; /* ARMv8 debug architecture */ 402 bool has_el3 = 403 !!FIELD_EX32(ahcf->isar.id_aa64pfr0, ID_AA64PFR0, EL3); 404 uint32_t dbgdidr = 0; 405 406 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, WRPS, wrps); 407 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, BRPS, brps); 408 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, CTX_CMPS, ctx_cmps); 409 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, VERSION, version); 410 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, NSUHD_IMP, has_el3); 411 dbgdidr = FIELD_DP32(dbgdidr, DBGDIDR, SE_IMP, has_el3); 412 dbgdidr |= (1 << 15); /* RES1 bit */ 413 ahcf->isar.dbgdidr = dbgdidr; 414 } 415 416 if (pmu_supported) { 417 /* PMCR_EL0 is only accessible if the vCPU has feature PMU_V3 */ 418 err |= read_sys_reg64(fdarray[2], &ahcf->isar.reset_pmcr_el0, 419 ARM64_SYS_REG(3, 3, 9, 12, 0)); 420 } 421 422 if (sve_supported) { 423 /* 424 * There is a range of kernels between kernel commit 73433762fcae 425 * and f81cb2c3ad41 which have a bug where the kernel doesn't 426 * expose SYS_ID_AA64ZFR0_EL1 via the ONE_REG API unless the VM has 427 * enabled SVE support, which resulted in an error rather than RAZ. 428 * So only read the register if we set KVM_ARM_VCPU_SVE above. 429 */ 430 err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64zfr0, 431 ARM64_SYS_REG(3, 0, 0, 4, 4)); 432 } 433 } 434 435 kvm_arm_destroy_scratch_host_vcpu(fdarray); 436 437 if (err < 0) { 438 return false; 439 } 440 441 /* 442 * We can assume any KVM supporting CPU is at least a v8 443 * with VFPv4+Neon; this in turn implies most of the other 444 * feature bits. 445 */ 446 features |= 1ULL << ARM_FEATURE_V8; 447 features |= 1ULL << ARM_FEATURE_NEON; 448 features |= 1ULL << ARM_FEATURE_AARCH64; 449 features |= 1ULL << ARM_FEATURE_PMU; 450 features |= 1ULL << ARM_FEATURE_GENERIC_TIMER; 451 452 ahcf->features = features; 453 454 return true; 455 } 456 457 void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu) 458 { 459 CPUARMState *env = &cpu->env; 460 461 if (!arm_host_cpu_features.dtb_compatible) { 462 if (!kvm_enabled() || 463 !kvm_arm_get_host_cpu_features(&arm_host_cpu_features)) { 464 /* We can't report this error yet, so flag that we need to 465 * in arm_cpu_realizefn(). 466 */ 467 cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; 468 cpu->host_cpu_probe_failed = true; 469 return; 470 } 471 } 472 473 cpu->kvm_target = arm_host_cpu_features.target; 474 cpu->dtb_compatible = arm_host_cpu_features.dtb_compatible; 475 cpu->isar = arm_host_cpu_features.isar; 476 env->features = arm_host_cpu_features.features; 477 } 478 479 static bool kvm_no_adjvtime_get(Object *obj, Error **errp) 480 { 481 return !ARM_CPU(obj)->kvm_adjvtime; 482 } 483 484 static void kvm_no_adjvtime_set(Object *obj, bool value, Error **errp) 485 { 486 ARM_CPU(obj)->kvm_adjvtime = !value; 487 } 488 489 static bool kvm_steal_time_get(Object *obj, Error **errp) 490 { 491 return ARM_CPU(obj)->kvm_steal_time != ON_OFF_AUTO_OFF; 492 } 493 494 static void kvm_steal_time_set(Object *obj, bool value, Error **errp) 495 { 496 ARM_CPU(obj)->kvm_steal_time = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; 497 } 498 499 /* KVM VCPU properties should be prefixed with "kvm-". */ 500 void kvm_arm_add_vcpu_properties(ARMCPU *cpu) 501 { 502 CPUARMState *env = &cpu->env; 503 Object *obj = OBJECT(cpu); 504 505 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 506 cpu->kvm_adjvtime = true; 507 object_property_add_bool(obj, "kvm-no-adjvtime", kvm_no_adjvtime_get, 508 kvm_no_adjvtime_set); 509 object_property_set_description(obj, "kvm-no-adjvtime", 510 "Set on to disable the adjustment of " 511 "the virtual counter. VM stopped time " 512 "will be counted."); 513 } 514 515 cpu->kvm_steal_time = ON_OFF_AUTO_AUTO; 516 object_property_add_bool(obj, "kvm-steal-time", kvm_steal_time_get, 517 kvm_steal_time_set); 518 object_property_set_description(obj, "kvm-steal-time", 519 "Set off to disable KVM steal time."); 520 } 521 522 bool kvm_arm_pmu_supported(void) 523 { 524 return kvm_check_extension(kvm_state, KVM_CAP_ARM_PMU_V3); 525 } 526 527 int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa) 528 { 529 KVMState *s = KVM_STATE(ms->accelerator); 530 int ret; 531 532 ret = kvm_check_extension(s, KVM_CAP_ARM_VM_IPA_SIZE); 533 *fixed_ipa = ret <= 0; 534 535 return ret > 0 ? ret : 40; 536 } 537 538 int kvm_arch_get_default_type(MachineState *ms) 539 { 540 bool fixed_ipa; 541 int size = kvm_arm_get_max_vm_ipa_size(ms, &fixed_ipa); 542 return fixed_ipa ? 0 : size; 543 } 544 545 int kvm_arch_init(MachineState *ms, KVMState *s) 546 { 547 int ret = 0; 548 /* For ARM interrupt delivery is always asynchronous, 549 * whether we are using an in-kernel VGIC or not. 550 */ 551 kvm_async_interrupts_allowed = true; 552 553 /* 554 * PSCI wakes up secondary cores, so we always need to 555 * have vCPUs waiting in kernel space 556 */ 557 kvm_halt_in_kernel_allowed = true; 558 559 cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE); 560 561 /* Check whether user space can specify guest syndrome value */ 562 cap_has_inject_serror_esr = 563 kvm_check_extension(s, KVM_CAP_ARM_INJECT_SERROR_ESR); 564 565 if (ms->smp.cpus > 256 && 566 !kvm_check_extension(s, KVM_CAP_ARM_IRQ_LINE_LAYOUT_2)) { 567 error_report("Using more than 256 vcpus requires a host kernel " 568 "with KVM_CAP_ARM_IRQ_LINE_LAYOUT_2"); 569 ret = -EINVAL; 570 } 571 572 if (kvm_check_extension(s, KVM_CAP_ARM_NISV_TO_USER)) { 573 if (kvm_vm_enable_cap(s, KVM_CAP_ARM_NISV_TO_USER, 0)) { 574 error_report("Failed to enable KVM_CAP_ARM_NISV_TO_USER cap"); 575 } else { 576 /* Set status for supporting the external dabt injection */ 577 cap_has_inject_ext_dabt = kvm_check_extension(s, 578 KVM_CAP_ARM_INJECT_EXT_DABT); 579 } 580 } 581 582 if (s->kvm_eager_split_size) { 583 uint32_t sizes; 584 585 sizes = kvm_vm_check_extension(s, KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES); 586 if (!sizes) { 587 s->kvm_eager_split_size = 0; 588 warn_report("Eager Page Split support not available"); 589 } else if (!(s->kvm_eager_split_size & sizes)) { 590 error_report("Eager Page Split requested chunk size not valid"); 591 ret = -EINVAL; 592 } else { 593 ret = kvm_vm_enable_cap(s, KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE, 0, 594 s->kvm_eager_split_size); 595 if (ret < 0) { 596 error_report("Enabling of Eager Page Split failed: %s", 597 strerror(-ret)); 598 } 599 } 600 } 601 602 max_hw_wps = kvm_check_extension(s, KVM_CAP_GUEST_DEBUG_HW_WPS); 603 hw_watchpoints = g_array_sized_new(true, true, 604 sizeof(HWWatchpoint), max_hw_wps); 605 606 max_hw_bps = kvm_check_extension(s, KVM_CAP_GUEST_DEBUG_HW_BPS); 607 hw_breakpoints = g_array_sized_new(true, true, 608 sizeof(HWBreakpoint), max_hw_bps); 609 610 return ret; 611 } 612 613 unsigned long kvm_arch_vcpu_id(CPUState *cpu) 614 { 615 return cpu->cpu_index; 616 } 617 618 /* We track all the KVM devices which need their memory addresses 619 * passing to the kernel in a list of these structures. 620 * When board init is complete we run through the list and 621 * tell the kernel the base addresses of the memory regions. 622 * We use a MemoryListener to track mapping and unmapping of 623 * the regions during board creation, so the board models don't 624 * need to do anything special for the KVM case. 625 * 626 * Sometimes the address must be OR'ed with some other fields 627 * (for example for KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION). 628 * @kda_addr_ormask aims at storing the value of those fields. 629 */ 630 typedef struct KVMDevice { 631 struct kvm_arm_device_addr kda; 632 struct kvm_device_attr kdattr; 633 uint64_t kda_addr_ormask; 634 MemoryRegion *mr; 635 QSLIST_ENTRY(KVMDevice) entries; 636 int dev_fd; 637 } KVMDevice; 638 639 static QSLIST_HEAD(, KVMDevice) kvm_devices_head; 640 641 static void kvm_arm_devlistener_add(MemoryListener *listener, 642 MemoryRegionSection *section) 643 { 644 KVMDevice *kd; 645 646 QSLIST_FOREACH(kd, &kvm_devices_head, entries) { 647 if (section->mr == kd->mr) { 648 kd->kda.addr = section->offset_within_address_space; 649 } 650 } 651 } 652 653 static void kvm_arm_devlistener_del(MemoryListener *listener, 654 MemoryRegionSection *section) 655 { 656 KVMDevice *kd; 657 658 QSLIST_FOREACH(kd, &kvm_devices_head, entries) { 659 if (section->mr == kd->mr) { 660 kd->kda.addr = -1; 661 } 662 } 663 } 664 665 static MemoryListener devlistener = { 666 .name = "kvm-arm", 667 .region_add = kvm_arm_devlistener_add, 668 .region_del = kvm_arm_devlistener_del, 669 .priority = MEMORY_LISTENER_PRIORITY_MIN, 670 }; 671 672 static void kvm_arm_set_device_addr(KVMDevice *kd) 673 { 674 struct kvm_device_attr *attr = &kd->kdattr; 675 int ret; 676 677 /* If the device control API is available and we have a device fd on the 678 * KVMDevice struct, let's use the newer API 679 */ 680 if (kd->dev_fd >= 0) { 681 uint64_t addr = kd->kda.addr; 682 683 addr |= kd->kda_addr_ormask; 684 attr->addr = (uintptr_t)&addr; 685 ret = kvm_device_ioctl(kd->dev_fd, KVM_SET_DEVICE_ATTR, attr); 686 } else { 687 ret = kvm_vm_ioctl(kvm_state, KVM_ARM_SET_DEVICE_ADDR, &kd->kda); 688 } 689 690 if (ret < 0) { 691 fprintf(stderr, "Failed to set device address: %s\n", 692 strerror(-ret)); 693 abort(); 694 } 695 } 696 697 static void kvm_arm_machine_init_done(Notifier *notifier, void *data) 698 { 699 KVMDevice *kd, *tkd; 700 701 QSLIST_FOREACH_SAFE(kd, &kvm_devices_head, entries, tkd) { 702 if (kd->kda.addr != -1) { 703 kvm_arm_set_device_addr(kd); 704 } 705 memory_region_unref(kd->mr); 706 QSLIST_REMOVE_HEAD(&kvm_devices_head, entries); 707 g_free(kd); 708 } 709 memory_listener_unregister(&devlistener); 710 } 711 712 static Notifier notify = { 713 .notify = kvm_arm_machine_init_done, 714 }; 715 716 void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group, 717 uint64_t attr, int dev_fd, uint64_t addr_ormask) 718 { 719 KVMDevice *kd; 720 721 if (!kvm_irqchip_in_kernel()) { 722 return; 723 } 724 725 if (QSLIST_EMPTY(&kvm_devices_head)) { 726 memory_listener_register(&devlistener, &address_space_memory); 727 qemu_add_machine_init_done_notifier(¬ify); 728 } 729 kd = g_new0(KVMDevice, 1); 730 kd->mr = mr; 731 kd->kda.id = devid; 732 kd->kda.addr = -1; 733 kd->kdattr.flags = 0; 734 kd->kdattr.group = group; 735 kd->kdattr.attr = attr; 736 kd->dev_fd = dev_fd; 737 kd->kda_addr_ormask = addr_ormask; 738 QSLIST_INSERT_HEAD(&kvm_devices_head, kd, entries); 739 memory_region_ref(kd->mr); 740 } 741 742 static int compare_u64(const void *a, const void *b) 743 { 744 if (*(uint64_t *)a > *(uint64_t *)b) { 745 return 1; 746 } 747 if (*(uint64_t *)a < *(uint64_t *)b) { 748 return -1; 749 } 750 return 0; 751 } 752 753 /* 754 * cpreg_values are sorted in ascending order by KVM register ID 755 * (see kvm_arm_init_cpreg_list). This allows us to cheaply find 756 * the storage for a KVM register by ID with a binary search. 757 */ 758 static uint64_t *kvm_arm_get_cpreg_ptr(ARMCPU *cpu, uint64_t regidx) 759 { 760 uint64_t *res; 761 762 res = bsearch(®idx, cpu->cpreg_indexes, cpu->cpreg_array_len, 763 sizeof(uint64_t), compare_u64); 764 assert(res); 765 766 return &cpu->cpreg_values[res - cpu->cpreg_indexes]; 767 } 768 769 /** 770 * kvm_arm_reg_syncs_via_cpreg_list: 771 * @regidx: KVM register index 772 * 773 * Return true if this KVM register should be synchronized via the 774 * cpreg list of arbitrary system registers, false if it is synchronized 775 * by hand using code in kvm_arch_get/put_registers(). 776 */ 777 static bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx) 778 { 779 switch (regidx & KVM_REG_ARM_COPROC_MASK) { 780 case KVM_REG_ARM_CORE: 781 case KVM_REG_ARM64_SVE: 782 return false; 783 default: 784 return true; 785 } 786 } 787 788 /** 789 * kvm_arm_init_cpreg_list: 790 * @cpu: ARMCPU 791 * 792 * Initialize the ARMCPU cpreg list according to the kernel's 793 * definition of what CPU registers it knows about (and throw away 794 * the previous TCG-created cpreg list). 795 * 796 * Returns: 0 if success, else < 0 error code 797 */ 798 static int kvm_arm_init_cpreg_list(ARMCPU *cpu) 799 { 800 struct kvm_reg_list rl; 801 struct kvm_reg_list *rlp; 802 int i, ret, arraylen; 803 CPUState *cs = CPU(cpu); 804 805 rl.n = 0; 806 ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl); 807 if (ret != -E2BIG) { 808 return ret; 809 } 810 rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t)); 811 rlp->n = rl.n; 812 ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp); 813 if (ret) { 814 goto out; 815 } 816 /* Sort the list we get back from the kernel, since cpreg_tuples 817 * must be in strictly ascending order. 818 */ 819 qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64); 820 821 for (i = 0, arraylen = 0; i < rlp->n; i++) { 822 if (!kvm_arm_reg_syncs_via_cpreg_list(rlp->reg[i])) { 823 continue; 824 } 825 switch (rlp->reg[i] & KVM_REG_SIZE_MASK) { 826 case KVM_REG_SIZE_U32: 827 case KVM_REG_SIZE_U64: 828 break; 829 default: 830 fprintf(stderr, "Can't handle size of register in kernel list\n"); 831 ret = -EINVAL; 832 goto out; 833 } 834 835 arraylen++; 836 } 837 838 cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen); 839 cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen); 840 cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes, 841 arraylen); 842 cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values, 843 arraylen); 844 cpu->cpreg_array_len = arraylen; 845 cpu->cpreg_vmstate_array_len = arraylen; 846 847 for (i = 0, arraylen = 0; i < rlp->n; i++) { 848 uint64_t regidx = rlp->reg[i]; 849 if (!kvm_arm_reg_syncs_via_cpreg_list(regidx)) { 850 continue; 851 } 852 cpu->cpreg_indexes[arraylen] = regidx; 853 arraylen++; 854 } 855 assert(cpu->cpreg_array_len == arraylen); 856 857 if (!write_kvmstate_to_list(cpu)) { 858 /* Shouldn't happen unless kernel is inconsistent about 859 * what registers exist. 860 */ 861 fprintf(stderr, "Initial read of kernel register state failed\n"); 862 ret = -EINVAL; 863 goto out; 864 } 865 866 out: 867 g_free(rlp); 868 return ret; 869 } 870 871 /** 872 * kvm_arm_cpreg_level: 873 * @regidx: KVM register index 874 * 875 * Return the level of this coprocessor/system register. Return value is 876 * either KVM_PUT_RUNTIME_STATE, KVM_PUT_RESET_STATE, or KVM_PUT_FULL_STATE. 877 */ 878 static int kvm_arm_cpreg_level(uint64_t regidx) 879 { 880 /* 881 * All system registers are assumed to be level KVM_PUT_RUNTIME_STATE. 882 * If a register should be written less often, you must add it here 883 * with a state of either KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE. 884 */ 885 switch (regidx) { 886 case KVM_REG_ARM_TIMER_CNT: 887 case KVM_REG_ARM_PTIMER_CNT: 888 return KVM_PUT_FULL_STATE; 889 } 890 return KVM_PUT_RUNTIME_STATE; 891 } 892 893 bool write_kvmstate_to_list(ARMCPU *cpu) 894 { 895 CPUState *cs = CPU(cpu); 896 int i; 897 bool ok = true; 898 899 for (i = 0; i < cpu->cpreg_array_len; i++) { 900 uint64_t regidx = cpu->cpreg_indexes[i]; 901 uint32_t v32; 902 int ret; 903 904 switch (regidx & KVM_REG_SIZE_MASK) { 905 case KVM_REG_SIZE_U32: 906 ret = kvm_get_one_reg(cs, regidx, &v32); 907 if (!ret) { 908 cpu->cpreg_values[i] = v32; 909 } 910 break; 911 case KVM_REG_SIZE_U64: 912 ret = kvm_get_one_reg(cs, regidx, cpu->cpreg_values + i); 913 break; 914 default: 915 g_assert_not_reached(); 916 } 917 if (ret) { 918 ok = false; 919 } 920 } 921 return ok; 922 } 923 924 bool write_list_to_kvmstate(ARMCPU *cpu, int level) 925 { 926 CPUState *cs = CPU(cpu); 927 int i; 928 bool ok = true; 929 930 for (i = 0; i < cpu->cpreg_array_len; i++) { 931 uint64_t regidx = cpu->cpreg_indexes[i]; 932 uint32_t v32; 933 int ret; 934 935 if (kvm_arm_cpreg_level(regidx) > level) { 936 continue; 937 } 938 939 switch (regidx & KVM_REG_SIZE_MASK) { 940 case KVM_REG_SIZE_U32: 941 v32 = cpu->cpreg_values[i]; 942 ret = kvm_set_one_reg(cs, regidx, &v32); 943 break; 944 case KVM_REG_SIZE_U64: 945 ret = kvm_set_one_reg(cs, regidx, cpu->cpreg_values + i); 946 break; 947 default: 948 g_assert_not_reached(); 949 } 950 if (ret) { 951 /* We might fail for "unknown register" and also for 952 * "you tried to set a register which is constant with 953 * a different value from what it actually contains". 954 */ 955 ok = false; 956 } 957 } 958 return ok; 959 } 960 961 void kvm_arm_cpu_pre_save(ARMCPU *cpu) 962 { 963 /* KVM virtual time adjustment */ 964 if (cpu->kvm_vtime_dirty) { 965 *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT) = cpu->kvm_vtime; 966 } 967 } 968 969 void kvm_arm_cpu_post_load(ARMCPU *cpu) 970 { 971 /* KVM virtual time adjustment */ 972 if (cpu->kvm_adjvtime) { 973 cpu->kvm_vtime = *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT); 974 cpu->kvm_vtime_dirty = true; 975 } 976 } 977 978 void kvm_arm_reset_vcpu(ARMCPU *cpu) 979 { 980 int ret; 981 982 /* Re-init VCPU so that all registers are set to 983 * their respective reset values. 984 */ 985 ret = kvm_arm_vcpu_init(cpu); 986 if (ret < 0) { 987 fprintf(stderr, "kvm_arm_vcpu_init failed: %s\n", strerror(-ret)); 988 abort(); 989 } 990 if (!write_kvmstate_to_list(cpu)) { 991 fprintf(stderr, "write_kvmstate_to_list failed\n"); 992 abort(); 993 } 994 /* 995 * Sync the reset values also into the CPUState. This is necessary 996 * because the next thing we do will be a kvm_arch_put_registers() 997 * which will update the list values from the CPUState before copying 998 * the list values back to KVM. It's OK to ignore failure returns here 999 * for the same reason we do so in kvm_arch_get_registers(). 1000 */ 1001 write_list_to_cpustate(cpu); 1002 } 1003 1004 /* 1005 * Update KVM's MP_STATE based on what QEMU thinks it is 1006 */ 1007 static int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu) 1008 { 1009 if (cap_has_mp_state) { 1010 struct kvm_mp_state mp_state = { 1011 .mp_state = (cpu->power_state == PSCI_OFF) ? 1012 KVM_MP_STATE_STOPPED : KVM_MP_STATE_RUNNABLE 1013 }; 1014 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state); 1015 } 1016 return 0; 1017 } 1018 1019 /* 1020 * Sync the KVM MP_STATE into QEMU 1021 */ 1022 static int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu) 1023 { 1024 if (cap_has_mp_state) { 1025 struct kvm_mp_state mp_state; 1026 int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MP_STATE, &mp_state); 1027 if (ret) { 1028 return ret; 1029 } 1030 cpu->power_state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ? 1031 PSCI_OFF : PSCI_ON; 1032 } 1033 return 0; 1034 } 1035 1036 /** 1037 * kvm_arm_get_virtual_time: 1038 * @cpu: ARMCPU 1039 * 1040 * Gets the VCPU's virtual counter and stores it in the KVM CPU state. 1041 */ 1042 static void kvm_arm_get_virtual_time(ARMCPU *cpu) 1043 { 1044 int ret; 1045 1046 if (cpu->kvm_vtime_dirty) { 1047 return; 1048 } 1049 1050 ret = kvm_get_one_reg(CPU(cpu), KVM_REG_ARM_TIMER_CNT, &cpu->kvm_vtime); 1051 if (ret) { 1052 error_report("Failed to get KVM_REG_ARM_TIMER_CNT"); 1053 abort(); 1054 } 1055 1056 cpu->kvm_vtime_dirty = true; 1057 } 1058 1059 /** 1060 * kvm_arm_put_virtual_time: 1061 * @cpu: ARMCPU 1062 * 1063 * Sets the VCPU's virtual counter to the value stored in the KVM CPU state. 1064 */ 1065 static void kvm_arm_put_virtual_time(ARMCPU *cpu) 1066 { 1067 int ret; 1068 1069 if (!cpu->kvm_vtime_dirty) { 1070 return; 1071 } 1072 1073 ret = kvm_set_one_reg(CPU(cpu), KVM_REG_ARM_TIMER_CNT, &cpu->kvm_vtime); 1074 if (ret) { 1075 error_report("Failed to set KVM_REG_ARM_TIMER_CNT"); 1076 abort(); 1077 } 1078 1079 cpu->kvm_vtime_dirty = false; 1080 } 1081 1082 /** 1083 * kvm_put_vcpu_events: 1084 * @cpu: ARMCPU 1085 * 1086 * Put VCPU related state to kvm. 1087 * 1088 * Returns: 0 if success else < 0 error code 1089 */ 1090 static int kvm_put_vcpu_events(ARMCPU *cpu) 1091 { 1092 CPUARMState *env = &cpu->env; 1093 struct kvm_vcpu_events events; 1094 int ret; 1095 1096 if (!kvm_has_vcpu_events()) { 1097 return 0; 1098 } 1099 1100 memset(&events, 0, sizeof(events)); 1101 events.exception.serror_pending = env->serror.pending; 1102 1103 /* Inject SError to guest with specified syndrome if host kernel 1104 * supports it, otherwise inject SError without syndrome. 1105 */ 1106 if (cap_has_inject_serror_esr) { 1107 events.exception.serror_has_esr = env->serror.has_esr; 1108 events.exception.serror_esr = env->serror.esr; 1109 } 1110 1111 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events); 1112 if (ret) { 1113 error_report("failed to put vcpu events"); 1114 } 1115 1116 return ret; 1117 } 1118 1119 /** 1120 * kvm_get_vcpu_events: 1121 * @cpu: ARMCPU 1122 * 1123 * Get VCPU related state from kvm. 1124 * 1125 * Returns: 0 if success else < 0 error code 1126 */ 1127 static int kvm_get_vcpu_events(ARMCPU *cpu) 1128 { 1129 CPUARMState *env = &cpu->env; 1130 struct kvm_vcpu_events events; 1131 int ret; 1132 1133 if (!kvm_has_vcpu_events()) { 1134 return 0; 1135 } 1136 1137 memset(&events, 0, sizeof(events)); 1138 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events); 1139 if (ret) { 1140 error_report("failed to get vcpu events"); 1141 return ret; 1142 } 1143 1144 env->serror.pending = events.exception.serror_pending; 1145 env->serror.has_esr = events.exception.serror_has_esr; 1146 env->serror.esr = events.exception.serror_esr; 1147 1148 return 0; 1149 } 1150 1151 #define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0) 1152 #define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2) 1153 1154 /* 1155 * ESR_EL1 1156 * ISS encoding 1157 * AARCH64: DFSC, bits [5:0] 1158 * AARCH32: 1159 * TTBCR.EAE == 0 1160 * FS[4] - DFSR[10] 1161 * FS[3:0] - DFSR[3:0] 1162 * TTBCR.EAE == 1 1163 * FS, bits [5:0] 1164 */ 1165 #define ESR_DFSC(aarch64, lpae, v) \ 1166 ((aarch64 || (lpae)) ? ((v) & 0x3F) \ 1167 : (((v) >> 6) | ((v) & 0x1F))) 1168 1169 #define ESR_DFSC_EXTABT(aarch64, lpae) \ 1170 ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8) 1171 1172 /** 1173 * kvm_arm_verify_ext_dabt_pending: 1174 * @cpu: ARMCPU 1175 * 1176 * Verify the fault status code wrt the Ext DABT injection 1177 * 1178 * Returns: true if the fault status code is as expected, false otherwise 1179 */ 1180 static bool kvm_arm_verify_ext_dabt_pending(ARMCPU *cpu) 1181 { 1182 CPUState *cs = CPU(cpu); 1183 uint64_t dfsr_val; 1184 1185 if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &dfsr_val)) { 1186 CPUARMState *env = &cpu->env; 1187 int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64); 1188 int lpae = 0; 1189 1190 if (!aarch64_mode) { 1191 uint64_t ttbcr; 1192 1193 if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &ttbcr)) { 1194 lpae = arm_feature(env, ARM_FEATURE_LPAE) 1195 && (ttbcr & TTBCR_EAE); 1196 } 1197 } 1198 /* 1199 * The verification here is based on the DFSC bits 1200 * of the ESR_EL1 reg only 1201 */ 1202 return (ESR_DFSC(aarch64_mode, lpae, dfsr_val) == 1203 ESR_DFSC_EXTABT(aarch64_mode, lpae)); 1204 } 1205 return false; 1206 } 1207 1208 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) 1209 { 1210 ARMCPU *cpu = ARM_CPU(cs); 1211 CPUARMState *env = &cpu->env; 1212 1213 if (unlikely(env->ext_dabt_raised)) { 1214 /* 1215 * Verifying that the ext DABT has been properly injected, 1216 * otherwise risking indefinitely re-running the faulting instruction 1217 * Covering a very narrow case for kernels 5.5..5.5.4 1218 * when injected abort was misconfigured to be 1219 * an IMPLEMENTATION DEFINED exception (for 32-bit EL1) 1220 */ 1221 if (!arm_feature(env, ARM_FEATURE_AARCH64) && 1222 unlikely(!kvm_arm_verify_ext_dabt_pending(cpu))) { 1223 1224 error_report("Data abort exception with no valid ISS generated by " 1225 "guest memory access. KVM unable to emulate faulting " 1226 "instruction. Failed to inject an external data abort " 1227 "into the guest."); 1228 abort(); 1229 } 1230 /* Clear the status */ 1231 env->ext_dabt_raised = 0; 1232 } 1233 } 1234 1235 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) 1236 { 1237 ARMCPU *cpu; 1238 uint32_t switched_level; 1239 1240 if (kvm_irqchip_in_kernel()) { 1241 /* 1242 * We only need to sync timer states with user-space interrupt 1243 * controllers, so return early and save cycles if we don't. 1244 */ 1245 return MEMTXATTRS_UNSPECIFIED; 1246 } 1247 1248 cpu = ARM_CPU(cs); 1249 1250 /* Synchronize our shadowed in-kernel device irq lines with the kvm ones */ 1251 if (run->s.regs.device_irq_level != cpu->device_irq_level) { 1252 switched_level = cpu->device_irq_level ^ run->s.regs.device_irq_level; 1253 1254 bql_lock(); 1255 1256 if (switched_level & KVM_ARM_DEV_EL1_VTIMER) { 1257 qemu_set_irq(cpu->gt_timer_outputs[GTIMER_VIRT], 1258 !!(run->s.regs.device_irq_level & 1259 KVM_ARM_DEV_EL1_VTIMER)); 1260 switched_level &= ~KVM_ARM_DEV_EL1_VTIMER; 1261 } 1262 1263 if (switched_level & KVM_ARM_DEV_EL1_PTIMER) { 1264 qemu_set_irq(cpu->gt_timer_outputs[GTIMER_PHYS], 1265 !!(run->s.regs.device_irq_level & 1266 KVM_ARM_DEV_EL1_PTIMER)); 1267 switched_level &= ~KVM_ARM_DEV_EL1_PTIMER; 1268 } 1269 1270 if (switched_level & KVM_ARM_DEV_PMU) { 1271 qemu_set_irq(cpu->pmu_interrupt, 1272 !!(run->s.regs.device_irq_level & KVM_ARM_DEV_PMU)); 1273 switched_level &= ~KVM_ARM_DEV_PMU; 1274 } 1275 1276 if (switched_level) { 1277 qemu_log_mask(LOG_UNIMP, "%s: unhandled in-kernel device IRQ %x\n", 1278 __func__, switched_level); 1279 } 1280 1281 /* We also mark unknown levels as processed to not waste cycles */ 1282 cpu->device_irq_level = run->s.regs.device_irq_level; 1283 bql_unlock(); 1284 } 1285 1286 return MEMTXATTRS_UNSPECIFIED; 1287 } 1288 1289 static void kvm_arm_vm_state_change(void *opaque, bool running, RunState state) 1290 { 1291 ARMCPU *cpu = opaque; 1292 1293 if (running) { 1294 if (cpu->kvm_adjvtime) { 1295 kvm_arm_put_virtual_time(cpu); 1296 } 1297 } else { 1298 if (cpu->kvm_adjvtime) { 1299 kvm_arm_get_virtual_time(cpu); 1300 } 1301 } 1302 } 1303 1304 /** 1305 * kvm_arm_handle_dabt_nisv: 1306 * @cpu: ARMCPU 1307 * @esr_iss: ISS encoding (limited) for the exception from Data Abort 1308 * ISV bit set to '0b0' -> no valid instruction syndrome 1309 * @fault_ipa: faulting address for the synchronous data abort 1310 * 1311 * Returns: 0 if the exception has been handled, < 0 otherwise 1312 */ 1313 static int kvm_arm_handle_dabt_nisv(ARMCPU *cpu, uint64_t esr_iss, 1314 uint64_t fault_ipa) 1315 { 1316 CPUARMState *env = &cpu->env; 1317 /* 1318 * Request KVM to inject the external data abort into the guest 1319 */ 1320 if (cap_has_inject_ext_dabt) { 1321 struct kvm_vcpu_events events = { }; 1322 /* 1323 * The external data abort event will be handled immediately by KVM 1324 * using the address fault that triggered the exit on given VCPU. 1325 * Requesting injection of the external data abort does not rely 1326 * on any other VCPU state. Therefore, in this particular case, the VCPU 1327 * synchronization can be exceptionally skipped. 1328 */ 1329 events.exception.ext_dabt_pending = 1; 1330 /* KVM_CAP_ARM_INJECT_EXT_DABT implies KVM_CAP_VCPU_EVENTS */ 1331 if (!kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events)) { 1332 env->ext_dabt_raised = 1; 1333 return 0; 1334 } 1335 } else { 1336 error_report("Data abort exception triggered by guest memory access " 1337 "at physical address: 0x" TARGET_FMT_lx, 1338 (target_ulong)fault_ipa); 1339 error_printf("KVM unable to emulate faulting instruction.\n"); 1340 } 1341 return -1; 1342 } 1343 1344 /** 1345 * kvm_arm_handle_debug: 1346 * @cpu: ARMCPU 1347 * @debug_exit: debug part of the KVM exit structure 1348 * 1349 * Returns: TRUE if the debug exception was handled. 1350 * 1351 * See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register 1352 * 1353 * To minimise translating between kernel and user-space the kernel 1354 * ABI just provides user-space with the full exception syndrome 1355 * register value to be decoded in QEMU. 1356 */ 1357 static bool kvm_arm_handle_debug(ARMCPU *cpu, 1358 struct kvm_debug_exit_arch *debug_exit) 1359 { 1360 int hsr_ec = syn_get_ec(debug_exit->hsr); 1361 CPUState *cs = CPU(cpu); 1362 CPUARMState *env = &cpu->env; 1363 1364 /* Ensure PC is synchronised */ 1365 kvm_cpu_synchronize_state(cs); 1366 1367 switch (hsr_ec) { 1368 case EC_SOFTWARESTEP: 1369 if (cs->singlestep_enabled) { 1370 return true; 1371 } else { 1372 /* 1373 * The kernel should have suppressed the guest's ability to 1374 * single step at this point so something has gone wrong. 1375 */ 1376 error_report("%s: guest single-step while debugging unsupported" 1377 " (%"PRIx64", %"PRIx32")", 1378 __func__, env->pc, debug_exit->hsr); 1379 return false; 1380 } 1381 break; 1382 case EC_AA64_BKPT: 1383 if (kvm_find_sw_breakpoint(cs, env->pc)) { 1384 return true; 1385 } 1386 break; 1387 case EC_BREAKPOINT: 1388 if (find_hw_breakpoint(cs, env->pc)) { 1389 return true; 1390 } 1391 break; 1392 case EC_WATCHPOINT: 1393 { 1394 CPUWatchpoint *wp = find_hw_watchpoint(cs, debug_exit->far); 1395 if (wp) { 1396 cs->watchpoint_hit = wp; 1397 return true; 1398 } 1399 break; 1400 } 1401 default: 1402 error_report("%s: unhandled debug exit (%"PRIx32", %"PRIx64")", 1403 __func__, debug_exit->hsr, env->pc); 1404 } 1405 1406 /* If we are not handling the debug exception it must belong to 1407 * the guest. Let's re-use the existing TCG interrupt code to set 1408 * everything up properly. 1409 */ 1410 cs->exception_index = EXCP_BKPT; 1411 env->exception.syndrome = debug_exit->hsr; 1412 env->exception.vaddress = debug_exit->far; 1413 env->exception.target_el = 1; 1414 bql_lock(); 1415 arm_cpu_do_interrupt(cs); 1416 bql_unlock(); 1417 1418 return false; 1419 } 1420 1421 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) 1422 { 1423 ARMCPU *cpu = ARM_CPU(cs); 1424 int ret = 0; 1425 1426 switch (run->exit_reason) { 1427 case KVM_EXIT_DEBUG: 1428 if (kvm_arm_handle_debug(cpu, &run->debug.arch)) { 1429 ret = EXCP_DEBUG; 1430 } /* otherwise return to guest */ 1431 break; 1432 case KVM_EXIT_ARM_NISV: 1433 /* External DABT with no valid iss to decode */ 1434 ret = kvm_arm_handle_dabt_nisv(cpu, run->arm_nisv.esr_iss, 1435 run->arm_nisv.fault_ipa); 1436 break; 1437 default: 1438 qemu_log_mask(LOG_UNIMP, "%s: un-handled exit reason %d\n", 1439 __func__, run->exit_reason); 1440 break; 1441 } 1442 return ret; 1443 } 1444 1445 bool kvm_arch_stop_on_emulation_error(CPUState *cs) 1446 { 1447 return true; 1448 } 1449 1450 int kvm_arch_process_async_events(CPUState *cs) 1451 { 1452 return 0; 1453 } 1454 1455 /** 1456 * kvm_arm_hw_debug_active: 1457 * @cpu: ARMCPU 1458 * 1459 * Return: TRUE if any hardware breakpoints in use. 1460 */ 1461 static bool kvm_arm_hw_debug_active(ARMCPU *cpu) 1462 { 1463 return ((cur_hw_wps > 0) || (cur_hw_bps > 0)); 1464 } 1465 1466 /** 1467 * kvm_arm_copy_hw_debug_data: 1468 * @ptr: kvm_guest_debug_arch structure 1469 * 1470 * Copy the architecture specific debug registers into the 1471 * kvm_guest_debug ioctl structure. 1472 */ 1473 static void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr) 1474 { 1475 int i; 1476 memset(ptr, 0, sizeof(struct kvm_guest_debug_arch)); 1477 1478 for (i = 0; i < max_hw_wps; i++) { 1479 HWWatchpoint *wp = get_hw_wp(i); 1480 ptr->dbg_wcr[i] = wp->wcr; 1481 ptr->dbg_wvr[i] = wp->wvr; 1482 } 1483 for (i = 0; i < max_hw_bps; i++) { 1484 HWBreakpoint *bp = get_hw_bp(i); 1485 ptr->dbg_bcr[i] = bp->bcr; 1486 ptr->dbg_bvr[i] = bp->bvr; 1487 } 1488 } 1489 1490 void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg) 1491 { 1492 if (kvm_sw_breakpoints_active(cs)) { 1493 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; 1494 } 1495 if (kvm_arm_hw_debug_active(ARM_CPU(cs))) { 1496 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW; 1497 kvm_arm_copy_hw_debug_data(&dbg->arch); 1498 } 1499 } 1500 1501 void kvm_arch_init_irq_routing(KVMState *s) 1502 { 1503 } 1504 1505 int kvm_arch_irqchip_create(KVMState *s) 1506 { 1507 if (kvm_kernel_irqchip_split()) { 1508 error_report("-machine kernel_irqchip=split is not supported on ARM."); 1509 exit(1); 1510 } 1511 1512 /* If we can create the VGIC using the newer device control API, we 1513 * let the device do this when it initializes itself, otherwise we 1514 * fall back to the old API */ 1515 return kvm_check_extension(s, KVM_CAP_DEVICE_CTRL); 1516 } 1517 1518 int kvm_arm_vgic_probe(void) 1519 { 1520 int val = 0; 1521 1522 if (kvm_create_device(kvm_state, 1523 KVM_DEV_TYPE_ARM_VGIC_V3, true) == 0) { 1524 val |= KVM_ARM_VGIC_V3; 1525 } 1526 if (kvm_create_device(kvm_state, 1527 KVM_DEV_TYPE_ARM_VGIC_V2, true) == 0) { 1528 val |= KVM_ARM_VGIC_V2; 1529 } 1530 return val; 1531 } 1532 1533 int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level) 1534 { 1535 int kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT) | irq; 1536 int cpu_idx1 = cpu % 256; 1537 int cpu_idx2 = cpu / 256; 1538 1539 kvm_irq |= (cpu_idx1 << KVM_ARM_IRQ_VCPU_SHIFT) | 1540 (cpu_idx2 << KVM_ARM_IRQ_VCPU2_SHIFT); 1541 1542 return kvm_set_irq(kvm_state, kvm_irq, !!level); 1543 } 1544 1545 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 1546 uint64_t address, uint32_t data, PCIDevice *dev) 1547 { 1548 AddressSpace *as = pci_device_iommu_address_space(dev); 1549 hwaddr xlat, len, doorbell_gpa; 1550 MemoryRegionSection mrs; 1551 MemoryRegion *mr; 1552 1553 if (as == &address_space_memory) { 1554 return 0; 1555 } 1556 1557 /* MSI doorbell address is translated by an IOMMU */ 1558 1559 RCU_READ_LOCK_GUARD(); 1560 1561 mr = address_space_translate(as, address, &xlat, &len, true, 1562 MEMTXATTRS_UNSPECIFIED); 1563 1564 if (!mr) { 1565 return 1; 1566 } 1567 1568 mrs = memory_region_find(mr, xlat, 1); 1569 1570 if (!mrs.mr) { 1571 return 1; 1572 } 1573 1574 doorbell_gpa = mrs.offset_within_address_space; 1575 memory_region_unref(mrs.mr); 1576 1577 route->u.msi.address_lo = doorbell_gpa; 1578 route->u.msi.address_hi = doorbell_gpa >> 32; 1579 1580 trace_kvm_arm_fixup_msi_route(address, doorbell_gpa); 1581 1582 return 0; 1583 } 1584 1585 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 1586 int vector, PCIDevice *dev) 1587 { 1588 return 0; 1589 } 1590 1591 int kvm_arch_release_virq_post(int virq) 1592 { 1593 return 0; 1594 } 1595 1596 int kvm_arch_msi_data_to_gsi(uint32_t data) 1597 { 1598 return (data - 32) & 0xffff; 1599 } 1600 1601 static void kvm_arch_get_eager_split_size(Object *obj, Visitor *v, 1602 const char *name, void *opaque, 1603 Error **errp) 1604 { 1605 KVMState *s = KVM_STATE(obj); 1606 uint64_t value = s->kvm_eager_split_size; 1607 1608 visit_type_size(v, name, &value, errp); 1609 } 1610 1611 static void kvm_arch_set_eager_split_size(Object *obj, Visitor *v, 1612 const char *name, void *opaque, 1613 Error **errp) 1614 { 1615 KVMState *s = KVM_STATE(obj); 1616 uint64_t value; 1617 1618 if (s->fd != -1) { 1619 error_setg(errp, "Unable to set early-split-size after KVM has been initialized"); 1620 return; 1621 } 1622 1623 if (!visit_type_size(v, name, &value, errp)) { 1624 return; 1625 } 1626 1627 if (value && !is_power_of_2(value)) { 1628 error_setg(errp, "early-split-size must be a power of two"); 1629 return; 1630 } 1631 1632 s->kvm_eager_split_size = value; 1633 } 1634 1635 void kvm_arch_accel_class_init(ObjectClass *oc) 1636 { 1637 object_class_property_add(oc, "eager-split-size", "size", 1638 kvm_arch_get_eager_split_size, 1639 kvm_arch_set_eager_split_size, NULL, NULL); 1640 1641 object_class_property_set_description(oc, "eager-split-size", 1642 "Eager Page Split chunk size for hugepages. (default: 0, disabled)"); 1643 } 1644 1645 int kvm_arch_insert_hw_breakpoint(vaddr addr, vaddr len, int type) 1646 { 1647 switch (type) { 1648 case GDB_BREAKPOINT_HW: 1649 return insert_hw_breakpoint(addr); 1650 break; 1651 case GDB_WATCHPOINT_READ: 1652 case GDB_WATCHPOINT_WRITE: 1653 case GDB_WATCHPOINT_ACCESS: 1654 return insert_hw_watchpoint(addr, len, type); 1655 default: 1656 return -ENOSYS; 1657 } 1658 } 1659 1660 int kvm_arch_remove_hw_breakpoint(vaddr addr, vaddr len, int type) 1661 { 1662 switch (type) { 1663 case GDB_BREAKPOINT_HW: 1664 return delete_hw_breakpoint(addr); 1665 case GDB_WATCHPOINT_READ: 1666 case GDB_WATCHPOINT_WRITE: 1667 case GDB_WATCHPOINT_ACCESS: 1668 return delete_hw_watchpoint(addr, len, type); 1669 default: 1670 return -ENOSYS; 1671 } 1672 } 1673 1674 void kvm_arch_remove_all_hw_breakpoints(void) 1675 { 1676 if (cur_hw_wps > 0) { 1677 g_array_remove_range(hw_watchpoints, 0, cur_hw_wps); 1678 } 1679 if (cur_hw_bps > 0) { 1680 g_array_remove_range(hw_breakpoints, 0, cur_hw_bps); 1681 } 1682 } 1683 1684 static bool kvm_arm_set_device_attr(ARMCPU *cpu, struct kvm_device_attr *attr, 1685 const char *name) 1686 { 1687 int err; 1688 1689 err = kvm_vcpu_ioctl(CPU(cpu), KVM_HAS_DEVICE_ATTR, attr); 1690 if (err != 0) { 1691 error_report("%s: KVM_HAS_DEVICE_ATTR: %s", name, strerror(-err)); 1692 return false; 1693 } 1694 1695 err = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_DEVICE_ATTR, attr); 1696 if (err != 0) { 1697 error_report("%s: KVM_SET_DEVICE_ATTR: %s", name, strerror(-err)); 1698 return false; 1699 } 1700 1701 return true; 1702 } 1703 1704 void kvm_arm_pmu_init(ARMCPU *cpu) 1705 { 1706 struct kvm_device_attr attr = { 1707 .group = KVM_ARM_VCPU_PMU_V3_CTRL, 1708 .attr = KVM_ARM_VCPU_PMU_V3_INIT, 1709 }; 1710 1711 if (!cpu->has_pmu) { 1712 return; 1713 } 1714 if (!kvm_arm_set_device_attr(cpu, &attr, "PMU")) { 1715 error_report("failed to init PMU"); 1716 abort(); 1717 } 1718 } 1719 1720 void kvm_arm_pmu_set_irq(ARMCPU *cpu, int irq) 1721 { 1722 struct kvm_device_attr attr = { 1723 .group = KVM_ARM_VCPU_PMU_V3_CTRL, 1724 .addr = (intptr_t)&irq, 1725 .attr = KVM_ARM_VCPU_PMU_V3_IRQ, 1726 }; 1727 1728 if (!cpu->has_pmu) { 1729 return; 1730 } 1731 if (!kvm_arm_set_device_attr(cpu, &attr, "PMU")) { 1732 error_report("failed to set irq for PMU"); 1733 abort(); 1734 } 1735 } 1736 1737 void kvm_arm_pvtime_init(ARMCPU *cpu, uint64_t ipa) 1738 { 1739 struct kvm_device_attr attr = { 1740 .group = KVM_ARM_VCPU_PVTIME_CTRL, 1741 .attr = KVM_ARM_VCPU_PVTIME_IPA, 1742 .addr = (uint64_t)&ipa, 1743 }; 1744 1745 if (cpu->kvm_steal_time == ON_OFF_AUTO_OFF) { 1746 return; 1747 } 1748 if (!kvm_arm_set_device_attr(cpu, &attr, "PVTIME IPA")) { 1749 error_report("failed to init PVTIME IPA"); 1750 abort(); 1751 } 1752 } 1753 1754 void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp) 1755 { 1756 bool has_steal_time = kvm_check_extension(kvm_state, KVM_CAP_STEAL_TIME); 1757 1758 if (cpu->kvm_steal_time == ON_OFF_AUTO_AUTO) { 1759 if (!has_steal_time || !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 1760 cpu->kvm_steal_time = ON_OFF_AUTO_OFF; 1761 } else { 1762 cpu->kvm_steal_time = ON_OFF_AUTO_ON; 1763 } 1764 } else if (cpu->kvm_steal_time == ON_OFF_AUTO_ON) { 1765 if (!has_steal_time) { 1766 error_setg(errp, "'kvm-steal-time' cannot be enabled " 1767 "on this host"); 1768 return; 1769 } else if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 1770 /* 1771 * DEN0057A chapter 2 says "This specification only covers 1772 * systems in which the Execution state of the hypervisor 1773 * as well as EL1 of virtual machines is AArch64.". And, 1774 * to ensure that, the smc/hvc calls are only specified as 1775 * smc64/hvc64. 1776 */ 1777 error_setg(errp, "'kvm-steal-time' cannot be enabled " 1778 "for AArch32 guests"); 1779 return; 1780 } 1781 } 1782 } 1783 1784 bool kvm_arm_aarch32_supported(void) 1785 { 1786 return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL1_32BIT); 1787 } 1788 1789 bool kvm_arm_sve_supported(void) 1790 { 1791 return kvm_check_extension(kvm_state, KVM_CAP_ARM_SVE); 1792 } 1793 1794 QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN != 1); 1795 1796 uint32_t kvm_arm_sve_get_vls(ARMCPU *cpu) 1797 { 1798 /* Only call this function if kvm_arm_sve_supported() returns true. */ 1799 static uint64_t vls[KVM_ARM64_SVE_VLS_WORDS]; 1800 static bool probed; 1801 uint32_t vq = 0; 1802 int i; 1803 1804 /* 1805 * KVM ensures all host CPUs support the same set of vector lengths. 1806 * So we only need to create the scratch VCPUs once and then cache 1807 * the results. 1808 */ 1809 if (!probed) { 1810 struct kvm_vcpu_init init = { 1811 .target = -1, 1812 .features[0] = (1 << KVM_ARM_VCPU_SVE), 1813 }; 1814 struct kvm_one_reg reg = { 1815 .id = KVM_REG_ARM64_SVE_VLS, 1816 .addr = (uint64_t)&vls[0], 1817 }; 1818 int fdarray[3], ret; 1819 1820 probed = true; 1821 1822 if (!kvm_arm_create_scratch_host_vcpu(NULL, fdarray, &init)) { 1823 error_report("failed to create scratch VCPU with SVE enabled"); 1824 abort(); 1825 } 1826 ret = ioctl(fdarray[2], KVM_GET_ONE_REG, ®); 1827 kvm_arm_destroy_scratch_host_vcpu(fdarray); 1828 if (ret) { 1829 error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s", 1830 strerror(errno)); 1831 abort(); 1832 } 1833 1834 for (i = KVM_ARM64_SVE_VLS_WORDS - 1; i >= 0; --i) { 1835 if (vls[i]) { 1836 vq = 64 - clz64(vls[i]) + i * 64; 1837 break; 1838 } 1839 } 1840 if (vq > ARM_MAX_VQ) { 1841 warn_report("KVM supports vector lengths larger than " 1842 "QEMU can enable"); 1843 vls[0] &= MAKE_64BIT_MASK(0, ARM_MAX_VQ); 1844 } 1845 } 1846 1847 return vls[0]; 1848 } 1849 1850 static int kvm_arm_sve_set_vls(ARMCPU *cpu) 1851 { 1852 uint64_t vls[KVM_ARM64_SVE_VLS_WORDS] = { cpu->sve_vq.map }; 1853 1854 assert(cpu->sve_max_vq <= KVM_ARM64_SVE_VQ_MAX); 1855 1856 return kvm_set_one_reg(CPU(cpu), KVM_REG_ARM64_SVE_VLS, &vls[0]); 1857 } 1858 1859 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5 1860 1861 int kvm_arch_init_vcpu(CPUState *cs) 1862 { 1863 int ret; 1864 uint64_t mpidr; 1865 ARMCPU *cpu = ARM_CPU(cs); 1866 CPUARMState *env = &cpu->env; 1867 uint64_t psciver; 1868 1869 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE || 1870 !object_dynamic_cast(OBJECT(cpu), TYPE_AARCH64_CPU)) { 1871 error_report("KVM is not supported for this guest CPU type"); 1872 return -EINVAL; 1873 } 1874 1875 qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cpu); 1876 1877 /* Determine init features for this CPU */ 1878 memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features)); 1879 if (cs->start_powered_off) { 1880 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF; 1881 } 1882 if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) { 1883 cpu->psci_version = QEMU_PSCI_VERSION_0_2; 1884 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2; 1885 } 1886 if (!arm_feature(env, ARM_FEATURE_AARCH64)) { 1887 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT; 1888 } 1889 if (!kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) { 1890 cpu->has_pmu = false; 1891 } 1892 if (cpu->has_pmu) { 1893 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PMU_V3; 1894 } else { 1895 env->features &= ~(1ULL << ARM_FEATURE_PMU); 1896 } 1897 if (cpu_isar_feature(aa64_sve, cpu)) { 1898 assert(kvm_arm_sve_supported()); 1899 cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_SVE; 1900 } 1901 if (cpu_isar_feature(aa64_pauth, cpu)) { 1902 cpu->kvm_init_features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS | 1903 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC); 1904 } 1905 1906 /* Do KVM_ARM_VCPU_INIT ioctl */ 1907 ret = kvm_arm_vcpu_init(cpu); 1908 if (ret) { 1909 return ret; 1910 } 1911 1912 if (cpu_isar_feature(aa64_sve, cpu)) { 1913 ret = kvm_arm_sve_set_vls(cpu); 1914 if (ret) { 1915 return ret; 1916 } 1917 ret = kvm_arm_vcpu_finalize(cpu, KVM_ARM_VCPU_SVE); 1918 if (ret) { 1919 return ret; 1920 } 1921 } 1922 1923 /* 1924 * KVM reports the exact PSCI version it is implementing via a 1925 * special sysreg. If it is present, use its contents to determine 1926 * what to report to the guest in the dtb (it is the PSCI version, 1927 * in the same 15-bits major 16-bits minor format that PSCI_VERSION 1928 * returns). 1929 */ 1930 if (!kvm_get_one_reg(cs, KVM_REG_ARM_PSCI_VERSION, &psciver)) { 1931 cpu->psci_version = psciver; 1932 } 1933 1934 /* 1935 * When KVM is in use, PSCI is emulated in-kernel and not by qemu. 1936 * Currently KVM has its own idea about MPIDR assignment, so we 1937 * override our defaults with what we get from KVM. 1938 */ 1939 ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr); 1940 if (ret) { 1941 return ret; 1942 } 1943 cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK; 1944 1945 return kvm_arm_init_cpreg_list(cpu); 1946 } 1947 1948 int kvm_arch_destroy_vcpu(CPUState *cs) 1949 { 1950 return 0; 1951 } 1952 1953 /* Callers must hold the iothread mutex lock */ 1954 static void kvm_inject_arm_sea(CPUState *c) 1955 { 1956 ARMCPU *cpu = ARM_CPU(c); 1957 CPUARMState *env = &cpu->env; 1958 uint32_t esr; 1959 bool same_el; 1960 1961 c->exception_index = EXCP_DATA_ABORT; 1962 env->exception.target_el = 1; 1963 1964 /* 1965 * Set the DFSC to synchronous external abort and set FnV to not valid, 1966 * this will tell guest the FAR_ELx is UNKNOWN for this abort. 1967 */ 1968 same_el = arm_current_el(env) == env->exception.target_el; 1969 esr = syn_data_abort_no_iss(same_el, 1, 0, 0, 0, 0, 0x10); 1970 1971 env->exception.syndrome = esr; 1972 1973 arm_cpu_do_interrupt(c); 1974 } 1975 1976 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ 1977 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) 1978 1979 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \ 1980 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) 1981 1982 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \ 1983 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) 1984 1985 static int kvm_arch_put_fpsimd(CPUState *cs) 1986 { 1987 CPUARMState *env = &ARM_CPU(cs)->env; 1988 int i, ret; 1989 1990 for (i = 0; i < 32; i++) { 1991 uint64_t *q = aa64_vfp_qreg(env, i); 1992 #if HOST_BIG_ENDIAN 1993 uint64_t fp_val[2] = { q[1], q[0] }; 1994 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]), 1995 fp_val); 1996 #else 1997 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]), q); 1998 #endif 1999 if (ret) { 2000 return ret; 2001 } 2002 } 2003 2004 return 0; 2005 } 2006 2007 /* 2008 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits 2009 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard 2010 * code the slice index to zero for now as it's unlikely we'll need more than 2011 * one slice for quite some time. 2012 */ 2013 static int kvm_arch_put_sve(CPUState *cs) 2014 { 2015 ARMCPU *cpu = ARM_CPU(cs); 2016 CPUARMState *env = &cpu->env; 2017 uint64_t tmp[ARM_MAX_VQ * 2]; 2018 uint64_t *r; 2019 int n, ret; 2020 2021 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) { 2022 r = sve_bswap64(tmp, &env->vfp.zregs[n].d[0], cpu->sve_max_vq * 2); 2023 ret = kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_ZREG(n, 0), r); 2024 if (ret) { 2025 return ret; 2026 } 2027 } 2028 2029 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) { 2030 r = sve_bswap64(tmp, r = &env->vfp.pregs[n].p[0], 2031 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8)); 2032 ret = kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_PREG(n, 0), r); 2033 if (ret) { 2034 return ret; 2035 } 2036 } 2037 2038 r = sve_bswap64(tmp, &env->vfp.pregs[FFR_PRED_NUM].p[0], 2039 DIV_ROUND_UP(cpu->sve_max_vq * 2, 8)); 2040 ret = kvm_set_one_reg(cs, KVM_REG_ARM64_SVE_FFR(0), r); 2041 if (ret) { 2042 return ret; 2043 } 2044 2045 return 0; 2046 } 2047 2048 int kvm_arch_put_registers(CPUState *cs, int level) 2049 { 2050 uint64_t val; 2051 uint32_t fpr; 2052 int i, ret; 2053 unsigned int el; 2054 2055 ARMCPU *cpu = ARM_CPU(cs); 2056 CPUARMState *env = &cpu->env; 2057 2058 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the 2059 * AArch64 registers before pushing them out to 64-bit KVM. 2060 */ 2061 if (!is_a64(env)) { 2062 aarch64_sync_32_to_64(env); 2063 } 2064 2065 for (i = 0; i < 31; i++) { 2066 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.regs[i]), 2067 &env->xregs[i]); 2068 if (ret) { 2069 return ret; 2070 } 2071 } 2072 2073 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the 2074 * QEMU side we keep the current SP in xregs[31] as well. 2075 */ 2076 aarch64_save_sp(env, 1); 2077 2078 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.sp), &env->sp_el[0]); 2079 if (ret) { 2080 return ret; 2081 } 2082 2083 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(sp_el1), &env->sp_el[1]); 2084 if (ret) { 2085 return ret; 2086 } 2087 2088 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */ 2089 if (is_a64(env)) { 2090 val = pstate_read(env); 2091 } else { 2092 val = cpsr_read(env); 2093 } 2094 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.pstate), &val); 2095 if (ret) { 2096 return ret; 2097 } 2098 2099 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(regs.pc), &env->pc); 2100 if (ret) { 2101 return ret; 2102 } 2103 2104 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(elr_el1), &env->elr_el[1]); 2105 if (ret) { 2106 return ret; 2107 } 2108 2109 /* Saved Program State Registers 2110 * 2111 * Before we restore from the banked_spsr[] array we need to 2112 * ensure that any modifications to env->spsr are correctly 2113 * reflected in the banks. 2114 */ 2115 el = arm_current_el(env); 2116 if (el > 0 && !is_a64(env)) { 2117 i = bank_number(env->uncached_cpsr & CPSR_M); 2118 env->banked_spsr[i] = env->spsr; 2119 } 2120 2121 /* KVM 0-4 map to QEMU banks 1-5 */ 2122 for (i = 0; i < KVM_NR_SPSR; i++) { 2123 ret = kvm_set_one_reg(cs, AARCH64_CORE_REG(spsr[i]), 2124 &env->banked_spsr[i + 1]); 2125 if (ret) { 2126 return ret; 2127 } 2128 } 2129 2130 if (cpu_isar_feature(aa64_sve, cpu)) { 2131 ret = kvm_arch_put_sve(cs); 2132 } else { 2133 ret = kvm_arch_put_fpsimd(cs); 2134 } 2135 if (ret) { 2136 return ret; 2137 } 2138 2139 fpr = vfp_get_fpsr(env); 2140 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpsr), &fpr); 2141 if (ret) { 2142 return ret; 2143 } 2144 2145 fpr = vfp_get_fpcr(env); 2146 ret = kvm_set_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpcr), &fpr); 2147 if (ret) { 2148 return ret; 2149 } 2150 2151 write_cpustate_to_list(cpu, true); 2152 2153 if (!write_list_to_kvmstate(cpu, level)) { 2154 return -EINVAL; 2155 } 2156 2157 /* 2158 * Setting VCPU events should be triggered after syncing the registers 2159 * to avoid overwriting potential changes made by KVM upon calling 2160 * KVM_SET_VCPU_EVENTS ioctl 2161 */ 2162 ret = kvm_put_vcpu_events(cpu); 2163 if (ret) { 2164 return ret; 2165 } 2166 2167 return kvm_arm_sync_mpstate_to_kvm(cpu); 2168 } 2169 2170 static int kvm_arch_get_fpsimd(CPUState *cs) 2171 { 2172 CPUARMState *env = &ARM_CPU(cs)->env; 2173 int i, ret; 2174 2175 for (i = 0; i < 32; i++) { 2176 uint64_t *q = aa64_vfp_qreg(env, i); 2177 ret = kvm_get_one_reg(cs, AARCH64_SIMD_CORE_REG(fp_regs.vregs[i]), q); 2178 if (ret) { 2179 return ret; 2180 } else { 2181 #if HOST_BIG_ENDIAN 2182 uint64_t t; 2183 t = q[0], q[0] = q[1], q[1] = t; 2184 #endif 2185 } 2186 } 2187 2188 return 0; 2189 } 2190 2191 /* 2192 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits 2193 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard 2194 * code the slice index to zero for now as it's unlikely we'll need more than 2195 * one slice for quite some time. 2196 */ 2197 static int kvm_arch_get_sve(CPUState *cs) 2198 { 2199 ARMCPU *cpu = ARM_CPU(cs); 2200 CPUARMState *env = &cpu->env; 2201 uint64_t *r; 2202 int n, ret; 2203 2204 for (n = 0; n < KVM_ARM64_SVE_NUM_ZREGS; ++n) { 2205 r = &env->vfp.zregs[n].d[0]; 2206 ret = kvm_get_one_reg(cs, KVM_REG_ARM64_SVE_ZREG(n, 0), r); 2207 if (ret) { 2208 return ret; 2209 } 2210 sve_bswap64(r, r, cpu->sve_max_vq * 2); 2211 } 2212 2213 for (n = 0; n < KVM_ARM64_SVE_NUM_PREGS; ++n) { 2214 r = &env->vfp.pregs[n].p[0]; 2215 ret = kvm_get_one_reg(cs, KVM_REG_ARM64_SVE_PREG(n, 0), r); 2216 if (ret) { 2217 return ret; 2218 } 2219 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8)); 2220 } 2221 2222 r = &env->vfp.pregs[FFR_PRED_NUM].p[0]; 2223 ret = kvm_get_one_reg(cs, KVM_REG_ARM64_SVE_FFR(0), r); 2224 if (ret) { 2225 return ret; 2226 } 2227 sve_bswap64(r, r, DIV_ROUND_UP(cpu->sve_max_vq * 2, 8)); 2228 2229 return 0; 2230 } 2231 2232 int kvm_arch_get_registers(CPUState *cs) 2233 { 2234 uint64_t val; 2235 unsigned int el; 2236 uint32_t fpr; 2237 int i, ret; 2238 2239 ARMCPU *cpu = ARM_CPU(cs); 2240 CPUARMState *env = &cpu->env; 2241 2242 for (i = 0; i < 31; i++) { 2243 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.regs[i]), 2244 &env->xregs[i]); 2245 if (ret) { 2246 return ret; 2247 } 2248 } 2249 2250 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.sp), &env->sp_el[0]); 2251 if (ret) { 2252 return ret; 2253 } 2254 2255 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(sp_el1), &env->sp_el[1]); 2256 if (ret) { 2257 return ret; 2258 } 2259 2260 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.pstate), &val); 2261 if (ret) { 2262 return ret; 2263 } 2264 2265 env->aarch64 = ((val & PSTATE_nRW) == 0); 2266 if (is_a64(env)) { 2267 pstate_write(env, val); 2268 } else { 2269 cpsr_write(env, val, 0xffffffff, CPSRWriteRaw); 2270 } 2271 2272 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the 2273 * QEMU side we keep the current SP in xregs[31] as well. 2274 */ 2275 aarch64_restore_sp(env, 1); 2276 2277 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(regs.pc), &env->pc); 2278 if (ret) { 2279 return ret; 2280 } 2281 2282 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the 2283 * incoming AArch64 regs received from 64-bit KVM. 2284 * We must perform this after all of the registers have been acquired from 2285 * the kernel. 2286 */ 2287 if (!is_a64(env)) { 2288 aarch64_sync_64_to_32(env); 2289 } 2290 2291 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(elr_el1), &env->elr_el[1]); 2292 if (ret) { 2293 return ret; 2294 } 2295 2296 /* Fetch the SPSR registers 2297 * 2298 * KVM SPSRs 0-4 map to QEMU banks 1-5 2299 */ 2300 for (i = 0; i < KVM_NR_SPSR; i++) { 2301 ret = kvm_get_one_reg(cs, AARCH64_CORE_REG(spsr[i]), 2302 &env->banked_spsr[i + 1]); 2303 if (ret) { 2304 return ret; 2305 } 2306 } 2307 2308 el = arm_current_el(env); 2309 if (el > 0 && !is_a64(env)) { 2310 i = bank_number(env->uncached_cpsr & CPSR_M); 2311 env->spsr = env->banked_spsr[i]; 2312 } 2313 2314 if (cpu_isar_feature(aa64_sve, cpu)) { 2315 ret = kvm_arch_get_sve(cs); 2316 } else { 2317 ret = kvm_arch_get_fpsimd(cs); 2318 } 2319 if (ret) { 2320 return ret; 2321 } 2322 2323 ret = kvm_get_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpsr), &fpr); 2324 if (ret) { 2325 return ret; 2326 } 2327 vfp_set_fpsr(env, fpr); 2328 2329 ret = kvm_get_one_reg(cs, AARCH64_SIMD_CTRL_REG(fp_regs.fpcr), &fpr); 2330 if (ret) { 2331 return ret; 2332 } 2333 vfp_set_fpcr(env, fpr); 2334 2335 ret = kvm_get_vcpu_events(cpu); 2336 if (ret) { 2337 return ret; 2338 } 2339 2340 if (!write_kvmstate_to_list(cpu)) { 2341 return -EINVAL; 2342 } 2343 /* Note that it's OK to have registers which aren't in CPUState, 2344 * so we can ignore a failure return here. 2345 */ 2346 write_list_to_cpustate(cpu); 2347 2348 ret = kvm_arm_sync_mpstate_to_qemu(cpu); 2349 2350 /* TODO: other registers */ 2351 return ret; 2352 } 2353 2354 void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr) 2355 { 2356 ram_addr_t ram_addr; 2357 hwaddr paddr; 2358 2359 assert(code == BUS_MCEERR_AR || code == BUS_MCEERR_AO); 2360 2361 if (acpi_ghes_present() && addr) { 2362 ram_addr = qemu_ram_addr_from_host(addr); 2363 if (ram_addr != RAM_ADDR_INVALID && 2364 kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) { 2365 kvm_hwpoison_page_add(ram_addr); 2366 /* 2367 * If this is a BUS_MCEERR_AR, we know we have been called 2368 * synchronously from the vCPU thread, so we can easily 2369 * synchronize the state and inject an error. 2370 * 2371 * TODO: we currently don't tell the guest at all about 2372 * BUS_MCEERR_AO. In that case we might either be being 2373 * called synchronously from the vCPU thread, or a bit 2374 * later from the main thread, so doing the injection of 2375 * the error would be more complicated. 2376 */ 2377 if (code == BUS_MCEERR_AR) { 2378 kvm_cpu_synchronize_state(c); 2379 if (!acpi_ghes_record_errors(ACPI_HEST_SRC_ID_SEA, paddr)) { 2380 kvm_inject_arm_sea(c); 2381 } else { 2382 error_report("failed to record the error"); 2383 abort(); 2384 } 2385 } 2386 return; 2387 } 2388 if (code == BUS_MCEERR_AO) { 2389 error_report("Hardware memory error at addr %p for memory used by " 2390 "QEMU itself instead of guest system!", addr); 2391 } 2392 } 2393 2394 if (code == BUS_MCEERR_AR) { 2395 error_report("Hardware memory error!"); 2396 exit(1); 2397 } 2398 } 2399 2400 /* C6.6.29 BRK instruction */ 2401 static const uint32_t brk_insn = 0xd4200000; 2402 2403 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) 2404 { 2405 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) || 2406 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk_insn, 4, 1)) { 2407 return -EINVAL; 2408 } 2409 return 0; 2410 } 2411 2412 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) 2413 { 2414 static uint32_t brk; 2415 2416 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&brk, 4, 0) || 2417 brk != brk_insn || 2418 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) { 2419 return -EINVAL; 2420 } 2421 return 0; 2422 } 2423