1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * ARM implementation of KVM hooks 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright Christoffer Dall 2009-2010 5fcf5ef2aSThomas Huth * 6fcf5ef2aSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later. 7fcf5ef2aSThomas Huth * See the COPYING file in the top-level directory. 8fcf5ef2aSThomas Huth * 9fcf5ef2aSThomas Huth */ 10fcf5ef2aSThomas Huth 11fcf5ef2aSThomas Huth #include "qemu/osdep.h" 12fcf5ef2aSThomas Huth #include <sys/ioctl.h> 13fcf5ef2aSThomas Huth 14fcf5ef2aSThomas Huth #include <linux/kvm.h> 15fcf5ef2aSThomas Huth 16fcf5ef2aSThomas Huth #include "qemu/timer.h" 17fcf5ef2aSThomas Huth #include "qemu/error-report.h" 18db725815SMarkus Armbruster #include "qemu/main-loop.h" 19dea101a1SAndrew Jones #include "qom/object.h" 20dea101a1SAndrew Jones #include "qapi/error.h" 21fcf5ef2aSThomas Huth #include "sysemu/sysemu.h" 22fcf5ef2aSThomas Huth #include "sysemu/kvm.h" 23a27382e2SEric Auger #include "sysemu/kvm_int.h" 24fcf5ef2aSThomas Huth #include "kvm_arm.h" 25fcf5ef2aSThomas Huth #include "cpu.h" 26b05c81d2SEric Auger #include "trace.h" 27fcf5ef2aSThomas Huth #include "internals.h" 28b05c81d2SEric Auger #include "hw/pci/pci.h" 29fcf5ef2aSThomas Huth #include "exec/memattrs.h" 30fcf5ef2aSThomas Huth #include "exec/address-spaces.h" 31fcf5ef2aSThomas Huth #include "hw/boards.h" 3264552b6bSMarkus Armbruster #include "hw/irq.h" 33fcf5ef2aSThomas Huth #include "qemu/log.h" 34fcf5ef2aSThomas Huth 35fcf5ef2aSThomas Huth const KVMCapabilityInfo kvm_arch_required_capabilities[] = { 36fcf5ef2aSThomas Huth KVM_CAP_LAST_INFO 37fcf5ef2aSThomas Huth }; 38fcf5ef2aSThomas Huth 39fcf5ef2aSThomas Huth static bool cap_has_mp_state; 40202ccb6bSDongjiu Geng static bool cap_has_inject_serror_esr; 41694bcaa8SBeata Michalska static bool cap_has_inject_ext_dabt; 42fcf5ef2aSThomas Huth 43c4487d76SPeter Maydell static ARMHostCPUFeatures arm_host_cpu_features; 44c4487d76SPeter Maydell 45fcf5ef2aSThomas Huth int kvm_arm_vcpu_init(CPUState *cs) 46fcf5ef2aSThomas Huth { 47fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(cs); 48fcf5ef2aSThomas Huth struct kvm_vcpu_init init; 49fcf5ef2aSThomas Huth 50fcf5ef2aSThomas Huth init.target = cpu->kvm_target; 51fcf5ef2aSThomas Huth memcpy(init.features, cpu->kvm_init_features, sizeof(init.features)); 52fcf5ef2aSThomas Huth 53fcf5ef2aSThomas Huth return kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init); 54fcf5ef2aSThomas Huth } 55fcf5ef2aSThomas Huth 5614e99e0fSAndrew Jones int kvm_arm_vcpu_finalize(CPUState *cs, int feature) 5714e99e0fSAndrew Jones { 5814e99e0fSAndrew Jones return kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_FINALIZE, &feature); 5914e99e0fSAndrew Jones } 6014e99e0fSAndrew Jones 61202ccb6bSDongjiu Geng void kvm_arm_init_serror_injection(CPUState *cs) 62202ccb6bSDongjiu Geng { 63202ccb6bSDongjiu Geng cap_has_inject_serror_esr = kvm_check_extension(cs->kvm_state, 64202ccb6bSDongjiu Geng KVM_CAP_ARM_INJECT_SERROR_ESR); 65202ccb6bSDongjiu Geng } 66202ccb6bSDongjiu Geng 67fcf5ef2aSThomas Huth bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try, 68fcf5ef2aSThomas Huth int *fdarray, 69fcf5ef2aSThomas Huth struct kvm_vcpu_init *init) 70fcf5ef2aSThomas Huth { 710cdb4020SAndrew Jones int ret = 0, kvmfd = -1, vmfd = -1, cpufd = -1; 72d26f2f93SMarc Zyngier int max_vm_pa_size; 73fcf5ef2aSThomas Huth 74448058aaSDaniel P. Berrangé kvmfd = qemu_open_old("/dev/kvm", O_RDWR); 75fcf5ef2aSThomas Huth if (kvmfd < 0) { 76fcf5ef2aSThomas Huth goto err; 77fcf5ef2aSThomas Huth } 78d26f2f93SMarc Zyngier max_vm_pa_size = ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_ARM_VM_IPA_SIZE); 79d26f2f93SMarc Zyngier if (max_vm_pa_size < 0) { 80d26f2f93SMarc Zyngier max_vm_pa_size = 0; 81d26f2f93SMarc Zyngier } 82bbde13cdSPeter Maydell do { 83d26f2f93SMarc Zyngier vmfd = ioctl(kvmfd, KVM_CREATE_VM, max_vm_pa_size); 84bbde13cdSPeter Maydell } while (vmfd == -1 && errno == EINTR); 85fcf5ef2aSThomas Huth if (vmfd < 0) { 86fcf5ef2aSThomas Huth goto err; 87fcf5ef2aSThomas Huth } 88fcf5ef2aSThomas Huth cpufd = ioctl(vmfd, KVM_CREATE_VCPU, 0); 89fcf5ef2aSThomas Huth if (cpufd < 0) { 90fcf5ef2aSThomas Huth goto err; 91fcf5ef2aSThomas Huth } 92fcf5ef2aSThomas Huth 93fcf5ef2aSThomas Huth if (!init) { 94fcf5ef2aSThomas Huth /* Caller doesn't want the VCPU to be initialized, so skip it */ 95fcf5ef2aSThomas Huth goto finish; 96fcf5ef2aSThomas Huth } 97fcf5ef2aSThomas Huth 980cdb4020SAndrew Jones if (init->target == -1) { 990cdb4020SAndrew Jones struct kvm_vcpu_init preferred; 1000cdb4020SAndrew Jones 1010cdb4020SAndrew Jones ret = ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, &preferred); 1020cdb4020SAndrew Jones if (!ret) { 1030cdb4020SAndrew Jones init->target = preferred.target; 1040cdb4020SAndrew Jones } 1050cdb4020SAndrew Jones } 106fcf5ef2aSThomas Huth if (ret >= 0) { 107fcf5ef2aSThomas Huth ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, init); 108fcf5ef2aSThomas Huth if (ret < 0) { 109fcf5ef2aSThomas Huth goto err; 110fcf5ef2aSThomas Huth } 111fcf5ef2aSThomas Huth } else if (cpus_to_try) { 112fcf5ef2aSThomas Huth /* Old kernel which doesn't know about the 113fcf5ef2aSThomas Huth * PREFERRED_TARGET ioctl: we know it will only support 114fcf5ef2aSThomas Huth * creating one kind of guest CPU which is its preferred 115fcf5ef2aSThomas Huth * CPU type. 116fcf5ef2aSThomas Huth */ 1170cdb4020SAndrew Jones struct kvm_vcpu_init try; 1180cdb4020SAndrew Jones 119fcf5ef2aSThomas Huth while (*cpus_to_try != QEMU_KVM_ARM_TARGET_NONE) { 1200cdb4020SAndrew Jones try.target = *cpus_to_try++; 1210cdb4020SAndrew Jones memcpy(try.features, init->features, sizeof(init->features)); 1220cdb4020SAndrew Jones ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, &try); 123fcf5ef2aSThomas Huth if (ret >= 0) { 124fcf5ef2aSThomas Huth break; 125fcf5ef2aSThomas Huth } 126fcf5ef2aSThomas Huth } 127fcf5ef2aSThomas Huth if (ret < 0) { 128fcf5ef2aSThomas Huth goto err; 129fcf5ef2aSThomas Huth } 1300cdb4020SAndrew Jones init->target = try.target; 131fcf5ef2aSThomas Huth } else { 132fcf5ef2aSThomas Huth /* Treat a NULL cpus_to_try argument the same as an empty 133fcf5ef2aSThomas Huth * list, which means we will fail the call since this must 134fcf5ef2aSThomas Huth * be an old kernel which doesn't support PREFERRED_TARGET. 135fcf5ef2aSThomas Huth */ 136fcf5ef2aSThomas Huth goto err; 137fcf5ef2aSThomas Huth } 138fcf5ef2aSThomas Huth 139fcf5ef2aSThomas Huth finish: 140fcf5ef2aSThomas Huth fdarray[0] = kvmfd; 141fcf5ef2aSThomas Huth fdarray[1] = vmfd; 142fcf5ef2aSThomas Huth fdarray[2] = cpufd; 143fcf5ef2aSThomas Huth 144fcf5ef2aSThomas Huth return true; 145fcf5ef2aSThomas Huth 146fcf5ef2aSThomas Huth err: 147fcf5ef2aSThomas Huth if (cpufd >= 0) { 148fcf5ef2aSThomas Huth close(cpufd); 149fcf5ef2aSThomas Huth } 150fcf5ef2aSThomas Huth if (vmfd >= 0) { 151fcf5ef2aSThomas Huth close(vmfd); 152fcf5ef2aSThomas Huth } 153fcf5ef2aSThomas Huth if (kvmfd >= 0) { 154fcf5ef2aSThomas Huth close(kvmfd); 155fcf5ef2aSThomas Huth } 156fcf5ef2aSThomas Huth 157fcf5ef2aSThomas Huth return false; 158fcf5ef2aSThomas Huth } 159fcf5ef2aSThomas Huth 160fcf5ef2aSThomas Huth void kvm_arm_destroy_scratch_host_vcpu(int *fdarray) 161fcf5ef2aSThomas Huth { 162fcf5ef2aSThomas Huth int i; 163fcf5ef2aSThomas Huth 164fcf5ef2aSThomas Huth for (i = 2; i >= 0; i--) { 165fcf5ef2aSThomas Huth close(fdarray[i]); 166fcf5ef2aSThomas Huth } 167fcf5ef2aSThomas Huth } 168fcf5ef2aSThomas Huth 169c4487d76SPeter Maydell void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu) 170fcf5ef2aSThomas Huth { 171c4487d76SPeter Maydell CPUARMState *env = &cpu->env; 172fcf5ef2aSThomas Huth 173c4487d76SPeter Maydell if (!arm_host_cpu_features.dtb_compatible) { 174c4487d76SPeter Maydell if (!kvm_enabled() || 175c4487d76SPeter Maydell !kvm_arm_get_host_cpu_features(&arm_host_cpu_features)) { 176c4487d76SPeter Maydell /* We can't report this error yet, so flag that we need to 177c4487d76SPeter Maydell * in arm_cpu_realizefn(). 178fcf5ef2aSThomas Huth */ 179c4487d76SPeter Maydell cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; 180c4487d76SPeter Maydell cpu->host_cpu_probe_failed = true; 181c4487d76SPeter Maydell return; 182fcf5ef2aSThomas Huth } 183fcf5ef2aSThomas Huth } 184fcf5ef2aSThomas Huth 185c4487d76SPeter Maydell cpu->kvm_target = arm_host_cpu_features.target; 186c4487d76SPeter Maydell cpu->dtb_compatible = arm_host_cpu_features.dtb_compatible; 1874674097cSRichard Henderson cpu->isar = arm_host_cpu_features.isar; 188c4487d76SPeter Maydell env->features = arm_host_cpu_features.features; 189c4487d76SPeter Maydell } 190c4487d76SPeter Maydell 191dea101a1SAndrew Jones static bool kvm_no_adjvtime_get(Object *obj, Error **errp) 192dea101a1SAndrew Jones { 193dea101a1SAndrew Jones return !ARM_CPU(obj)->kvm_adjvtime; 194dea101a1SAndrew Jones } 195dea101a1SAndrew Jones 196dea101a1SAndrew Jones static void kvm_no_adjvtime_set(Object *obj, bool value, Error **errp) 197dea101a1SAndrew Jones { 198dea101a1SAndrew Jones ARM_CPU(obj)->kvm_adjvtime = !value; 199dea101a1SAndrew Jones } 200dea101a1SAndrew Jones 20168970d1eSAndrew Jones static bool kvm_steal_time_get(Object *obj, Error **errp) 20268970d1eSAndrew Jones { 20368970d1eSAndrew Jones return ARM_CPU(obj)->kvm_steal_time != ON_OFF_AUTO_OFF; 20468970d1eSAndrew Jones } 20568970d1eSAndrew Jones 20668970d1eSAndrew Jones static void kvm_steal_time_set(Object *obj, bool value, Error **errp) 20768970d1eSAndrew Jones { 20868970d1eSAndrew Jones ARM_CPU(obj)->kvm_steal_time = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; 20968970d1eSAndrew Jones } 21068970d1eSAndrew Jones 211dea101a1SAndrew Jones /* KVM VCPU properties should be prefixed with "kvm-". */ 212dea101a1SAndrew Jones void kvm_arm_add_vcpu_properties(Object *obj) 213dea101a1SAndrew Jones { 2149e6f8d8aSfangying ARMCPU *cpu = ARM_CPU(obj); 2159e6f8d8aSfangying CPUARMState *env = &cpu->env; 216dea101a1SAndrew Jones 2179e6f8d8aSfangying if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 2189e6f8d8aSfangying cpu->kvm_adjvtime = true; 219dea101a1SAndrew Jones object_property_add_bool(obj, "kvm-no-adjvtime", kvm_no_adjvtime_get, 220d2623129SMarkus Armbruster kvm_no_adjvtime_set); 221dea101a1SAndrew Jones object_property_set_description(obj, "kvm-no-adjvtime", 222dea101a1SAndrew Jones "Set on to disable the adjustment of " 223dea101a1SAndrew Jones "the virtual counter. VM stopped time " 2247eecec7dSMarkus Armbruster "will be counted."); 225dea101a1SAndrew Jones } 22668970d1eSAndrew Jones 22768970d1eSAndrew Jones cpu->kvm_steal_time = ON_OFF_AUTO_AUTO; 22868970d1eSAndrew Jones object_property_add_bool(obj, "kvm-steal-time", kvm_steal_time_get, 22968970d1eSAndrew Jones kvm_steal_time_set); 23068970d1eSAndrew Jones object_property_set_description(obj, "kvm-steal-time", 23168970d1eSAndrew Jones "Set off to disable KVM steal time."); 2329e6f8d8aSfangying } 233dea101a1SAndrew Jones 2347d20e681SPhilippe Mathieu-Daudé bool kvm_arm_pmu_supported(void) 235ae502508SAndrew Jones { 2367d20e681SPhilippe Mathieu-Daudé return kvm_check_extension(kvm_state, KVM_CAP_ARM_PMU_V3); 237ae502508SAndrew Jones } 238ae502508SAndrew Jones 239bcb902a1SAndrew Jones int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa) 240a27382e2SEric Auger { 241a27382e2SEric Auger KVMState *s = KVM_STATE(ms->accelerator); 242a27382e2SEric Auger int ret; 243a27382e2SEric Auger 244a27382e2SEric Auger ret = kvm_check_extension(s, KVM_CAP_ARM_VM_IPA_SIZE); 245bcb902a1SAndrew Jones *fixed_ipa = ret <= 0; 246bcb902a1SAndrew Jones 247a27382e2SEric Auger return ret > 0 ? ret : 40; 248a27382e2SEric Auger } 249a27382e2SEric Auger 2505e0d6590SAkihiko Odaki int kvm_arch_get_default_type(MachineState *ms) 2515e0d6590SAkihiko Odaki { 2521ab445afSAkihiko Odaki bool fixed_ipa; 2531ab445afSAkihiko Odaki int size = kvm_arm_get_max_vm_ipa_size(ms, &fixed_ipa); 2541ab445afSAkihiko Odaki return fixed_ipa ? 0 : size; 2555e0d6590SAkihiko Odaki } 2565e0d6590SAkihiko Odaki 257fcf5ef2aSThomas Huth int kvm_arch_init(MachineState *ms, KVMState *s) 258fcf5ef2aSThomas Huth { 259fff9f555SEric Auger int ret = 0; 260fcf5ef2aSThomas Huth /* For ARM interrupt delivery is always asynchronous, 261fcf5ef2aSThomas Huth * whether we are using an in-kernel VGIC or not. 262fcf5ef2aSThomas Huth */ 263fcf5ef2aSThomas Huth kvm_async_interrupts_allowed = true; 264fcf5ef2aSThomas Huth 2655d721b78SAlexander Graf /* 2665d721b78SAlexander Graf * PSCI wakes up secondary cores, so we always need to 2675d721b78SAlexander Graf * have vCPUs waiting in kernel space 2685d721b78SAlexander Graf */ 2695d721b78SAlexander Graf kvm_halt_in_kernel_allowed = true; 2705d721b78SAlexander Graf 271fcf5ef2aSThomas Huth cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE); 272fcf5ef2aSThomas Huth 273fff9f555SEric Auger if (ms->smp.cpus > 256 && 274fff9f555SEric Auger !kvm_check_extension(s, KVM_CAP_ARM_IRQ_LINE_LAYOUT_2)) { 275fff9f555SEric Auger error_report("Using more than 256 vcpus requires a host kernel " 276fff9f555SEric Auger "with KVM_CAP_ARM_IRQ_LINE_LAYOUT_2"); 277fff9f555SEric Auger ret = -EINVAL; 278fff9f555SEric Auger } 279fff9f555SEric Auger 280694bcaa8SBeata Michalska if (kvm_check_extension(s, KVM_CAP_ARM_NISV_TO_USER)) { 281694bcaa8SBeata Michalska if (kvm_vm_enable_cap(s, KVM_CAP_ARM_NISV_TO_USER, 0)) { 282694bcaa8SBeata Michalska error_report("Failed to enable KVM_CAP_ARM_NISV_TO_USER cap"); 283694bcaa8SBeata Michalska } else { 284694bcaa8SBeata Michalska /* Set status for supporting the external dabt injection */ 285694bcaa8SBeata Michalska cap_has_inject_ext_dabt = kvm_check_extension(s, 286694bcaa8SBeata Michalska KVM_CAP_ARM_INJECT_EXT_DABT); 287694bcaa8SBeata Michalska } 288694bcaa8SBeata Michalska } 289694bcaa8SBeata Michalska 290ad5c6ddeSAkihiko Odaki kvm_arm_init_debug(s); 291ad5c6ddeSAkihiko Odaki 292fff9f555SEric Auger return ret; 293fcf5ef2aSThomas Huth } 294fcf5ef2aSThomas Huth 295fcf5ef2aSThomas Huth unsigned long kvm_arch_vcpu_id(CPUState *cpu) 296fcf5ef2aSThomas Huth { 297fcf5ef2aSThomas Huth return cpu->cpu_index; 298fcf5ef2aSThomas Huth } 299fcf5ef2aSThomas Huth 300fcf5ef2aSThomas Huth /* We track all the KVM devices which need their memory addresses 301fcf5ef2aSThomas Huth * passing to the kernel in a list of these structures. 302fcf5ef2aSThomas Huth * When board init is complete we run through the list and 303fcf5ef2aSThomas Huth * tell the kernel the base addresses of the memory regions. 304fcf5ef2aSThomas Huth * We use a MemoryListener to track mapping and unmapping of 305fcf5ef2aSThomas Huth * the regions during board creation, so the board models don't 306fcf5ef2aSThomas Huth * need to do anything special for the KVM case. 30719d1bd0bSEric Auger * 30819d1bd0bSEric Auger * Sometimes the address must be OR'ed with some other fields 30919d1bd0bSEric Auger * (for example for KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION). 31019d1bd0bSEric Auger * @kda_addr_ormask aims at storing the value of those fields. 311fcf5ef2aSThomas Huth */ 312fcf5ef2aSThomas Huth typedef struct KVMDevice { 313fcf5ef2aSThomas Huth struct kvm_arm_device_addr kda; 314fcf5ef2aSThomas Huth struct kvm_device_attr kdattr; 31519d1bd0bSEric Auger uint64_t kda_addr_ormask; 316fcf5ef2aSThomas Huth MemoryRegion *mr; 317fcf5ef2aSThomas Huth QSLIST_ENTRY(KVMDevice) entries; 318fcf5ef2aSThomas Huth int dev_fd; 319fcf5ef2aSThomas Huth } KVMDevice; 320fcf5ef2aSThomas Huth 321b58deb34SPaolo Bonzini static QSLIST_HEAD(, KVMDevice) kvm_devices_head; 322fcf5ef2aSThomas Huth 323fcf5ef2aSThomas Huth static void kvm_arm_devlistener_add(MemoryListener *listener, 324fcf5ef2aSThomas Huth MemoryRegionSection *section) 325fcf5ef2aSThomas Huth { 326fcf5ef2aSThomas Huth KVMDevice *kd; 327fcf5ef2aSThomas Huth 328fcf5ef2aSThomas Huth QSLIST_FOREACH(kd, &kvm_devices_head, entries) { 329fcf5ef2aSThomas Huth if (section->mr == kd->mr) { 330fcf5ef2aSThomas Huth kd->kda.addr = section->offset_within_address_space; 331fcf5ef2aSThomas Huth } 332fcf5ef2aSThomas Huth } 333fcf5ef2aSThomas Huth } 334fcf5ef2aSThomas Huth 335fcf5ef2aSThomas Huth static void kvm_arm_devlistener_del(MemoryListener *listener, 336fcf5ef2aSThomas Huth MemoryRegionSection *section) 337fcf5ef2aSThomas Huth { 338fcf5ef2aSThomas Huth KVMDevice *kd; 339fcf5ef2aSThomas Huth 340fcf5ef2aSThomas Huth QSLIST_FOREACH(kd, &kvm_devices_head, entries) { 341fcf5ef2aSThomas Huth if (section->mr == kd->mr) { 342fcf5ef2aSThomas Huth kd->kda.addr = -1; 343fcf5ef2aSThomas Huth } 344fcf5ef2aSThomas Huth } 345fcf5ef2aSThomas Huth } 346fcf5ef2aSThomas Huth 347fcf5ef2aSThomas Huth static MemoryListener devlistener = { 348142518bdSPeter Xu .name = "kvm-arm", 349fcf5ef2aSThomas Huth .region_add = kvm_arm_devlistener_add, 350fcf5ef2aSThomas Huth .region_del = kvm_arm_devlistener_del, 35114a868c6SIsaku Yamahata .priority = MEMORY_LISTENER_PRIORITY_MIN, 352fcf5ef2aSThomas Huth }; 353fcf5ef2aSThomas Huth 354fcf5ef2aSThomas Huth static void kvm_arm_set_device_addr(KVMDevice *kd) 355fcf5ef2aSThomas Huth { 356fcf5ef2aSThomas Huth struct kvm_device_attr *attr = &kd->kdattr; 357fcf5ef2aSThomas Huth int ret; 358fcf5ef2aSThomas Huth 359fcf5ef2aSThomas Huth /* If the device control API is available and we have a device fd on the 360fcf5ef2aSThomas Huth * KVMDevice struct, let's use the newer API 361fcf5ef2aSThomas Huth */ 362fcf5ef2aSThomas Huth if (kd->dev_fd >= 0) { 363fcf5ef2aSThomas Huth uint64_t addr = kd->kda.addr; 36419d1bd0bSEric Auger 36519d1bd0bSEric Auger addr |= kd->kda_addr_ormask; 366fcf5ef2aSThomas Huth attr->addr = (uintptr_t)&addr; 367fcf5ef2aSThomas Huth ret = kvm_device_ioctl(kd->dev_fd, KVM_SET_DEVICE_ATTR, attr); 368fcf5ef2aSThomas Huth } else { 369fcf5ef2aSThomas Huth ret = kvm_vm_ioctl(kvm_state, KVM_ARM_SET_DEVICE_ADDR, &kd->kda); 370fcf5ef2aSThomas Huth } 371fcf5ef2aSThomas Huth 372fcf5ef2aSThomas Huth if (ret < 0) { 373fcf5ef2aSThomas Huth fprintf(stderr, "Failed to set device address: %s\n", 374fcf5ef2aSThomas Huth strerror(-ret)); 375fcf5ef2aSThomas Huth abort(); 376fcf5ef2aSThomas Huth } 377fcf5ef2aSThomas Huth } 378fcf5ef2aSThomas Huth 379fcf5ef2aSThomas Huth static void kvm_arm_machine_init_done(Notifier *notifier, void *data) 380fcf5ef2aSThomas Huth { 381fcf5ef2aSThomas Huth KVMDevice *kd, *tkd; 382fcf5ef2aSThomas Huth 383fcf5ef2aSThomas Huth QSLIST_FOREACH_SAFE(kd, &kvm_devices_head, entries, tkd) { 384fcf5ef2aSThomas Huth if (kd->kda.addr != -1) { 385fcf5ef2aSThomas Huth kvm_arm_set_device_addr(kd); 386fcf5ef2aSThomas Huth } 387fcf5ef2aSThomas Huth memory_region_unref(kd->mr); 3885ff9aaabSZheng Xiang QSLIST_REMOVE_HEAD(&kvm_devices_head, entries); 389fcf5ef2aSThomas Huth g_free(kd); 390fcf5ef2aSThomas Huth } 3910bbe4354SPeter Xu memory_listener_unregister(&devlistener); 392fcf5ef2aSThomas Huth } 393fcf5ef2aSThomas Huth 394fcf5ef2aSThomas Huth static Notifier notify = { 395fcf5ef2aSThomas Huth .notify = kvm_arm_machine_init_done, 396fcf5ef2aSThomas Huth }; 397fcf5ef2aSThomas Huth 398fcf5ef2aSThomas Huth void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group, 39919d1bd0bSEric Auger uint64_t attr, int dev_fd, uint64_t addr_ormask) 400fcf5ef2aSThomas Huth { 401fcf5ef2aSThomas Huth KVMDevice *kd; 402fcf5ef2aSThomas Huth 403fcf5ef2aSThomas Huth if (!kvm_irqchip_in_kernel()) { 404fcf5ef2aSThomas Huth return; 405fcf5ef2aSThomas Huth } 406fcf5ef2aSThomas Huth 407fcf5ef2aSThomas Huth if (QSLIST_EMPTY(&kvm_devices_head)) { 408fcf5ef2aSThomas Huth memory_listener_register(&devlistener, &address_space_memory); 409fcf5ef2aSThomas Huth qemu_add_machine_init_done_notifier(¬ify); 410fcf5ef2aSThomas Huth } 411fcf5ef2aSThomas Huth kd = g_new0(KVMDevice, 1); 412fcf5ef2aSThomas Huth kd->mr = mr; 413fcf5ef2aSThomas Huth kd->kda.id = devid; 414fcf5ef2aSThomas Huth kd->kda.addr = -1; 415fcf5ef2aSThomas Huth kd->kdattr.flags = 0; 416fcf5ef2aSThomas Huth kd->kdattr.group = group; 417fcf5ef2aSThomas Huth kd->kdattr.attr = attr; 418fcf5ef2aSThomas Huth kd->dev_fd = dev_fd; 41919d1bd0bSEric Auger kd->kda_addr_ormask = addr_ormask; 420fcf5ef2aSThomas Huth QSLIST_INSERT_HEAD(&kvm_devices_head, kd, entries); 421fcf5ef2aSThomas Huth memory_region_ref(kd->mr); 422fcf5ef2aSThomas Huth } 423fcf5ef2aSThomas Huth 424fcf5ef2aSThomas Huth static int compare_u64(const void *a, const void *b) 425fcf5ef2aSThomas Huth { 426fcf5ef2aSThomas Huth if (*(uint64_t *)a > *(uint64_t *)b) { 427fcf5ef2aSThomas Huth return 1; 428fcf5ef2aSThomas Huth } 429fcf5ef2aSThomas Huth if (*(uint64_t *)a < *(uint64_t *)b) { 430fcf5ef2aSThomas Huth return -1; 431fcf5ef2aSThomas Huth } 432fcf5ef2aSThomas Huth return 0; 433fcf5ef2aSThomas Huth } 434fcf5ef2aSThomas Huth 435e5ac4200SAndrew Jones /* 436e5ac4200SAndrew Jones * cpreg_values are sorted in ascending order by KVM register ID 437e5ac4200SAndrew Jones * (see kvm_arm_init_cpreg_list). This allows us to cheaply find 438e5ac4200SAndrew Jones * the storage for a KVM register by ID with a binary search. 439e5ac4200SAndrew Jones */ 440e5ac4200SAndrew Jones static uint64_t *kvm_arm_get_cpreg_ptr(ARMCPU *cpu, uint64_t regidx) 441e5ac4200SAndrew Jones { 442e5ac4200SAndrew Jones uint64_t *res; 443e5ac4200SAndrew Jones 444e5ac4200SAndrew Jones res = bsearch(®idx, cpu->cpreg_indexes, cpu->cpreg_array_len, 445e5ac4200SAndrew Jones sizeof(uint64_t), compare_u64); 446e5ac4200SAndrew Jones assert(res); 447e5ac4200SAndrew Jones 448e5ac4200SAndrew Jones return &cpu->cpreg_values[res - cpu->cpreg_indexes]; 449e5ac4200SAndrew Jones } 450e5ac4200SAndrew Jones 451c8a44709SDongjiu Geng /* Initialize the ARMCPU cpreg list according to the kernel's 452fcf5ef2aSThomas Huth * definition of what CPU registers it knows about (and throw away 453fcf5ef2aSThomas Huth * the previous TCG-created cpreg list). 454fcf5ef2aSThomas Huth */ 455fcf5ef2aSThomas Huth int kvm_arm_init_cpreg_list(ARMCPU *cpu) 456fcf5ef2aSThomas Huth { 457fcf5ef2aSThomas Huth struct kvm_reg_list rl; 458fcf5ef2aSThomas Huth struct kvm_reg_list *rlp; 459fcf5ef2aSThomas Huth int i, ret, arraylen; 460fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 461fcf5ef2aSThomas Huth 462fcf5ef2aSThomas Huth rl.n = 0; 463fcf5ef2aSThomas Huth ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl); 464fcf5ef2aSThomas Huth if (ret != -E2BIG) { 465fcf5ef2aSThomas Huth return ret; 466fcf5ef2aSThomas Huth } 467fcf5ef2aSThomas Huth rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t)); 468fcf5ef2aSThomas Huth rlp->n = rl.n; 469fcf5ef2aSThomas Huth ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp); 470fcf5ef2aSThomas Huth if (ret) { 471fcf5ef2aSThomas Huth goto out; 472fcf5ef2aSThomas Huth } 473fcf5ef2aSThomas Huth /* Sort the list we get back from the kernel, since cpreg_tuples 474fcf5ef2aSThomas Huth * must be in strictly ascending order. 475fcf5ef2aSThomas Huth */ 476fcf5ef2aSThomas Huth qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64); 477fcf5ef2aSThomas Huth 478fcf5ef2aSThomas Huth for (i = 0, arraylen = 0; i < rlp->n; i++) { 479fcf5ef2aSThomas Huth if (!kvm_arm_reg_syncs_via_cpreg_list(rlp->reg[i])) { 480fcf5ef2aSThomas Huth continue; 481fcf5ef2aSThomas Huth } 482fcf5ef2aSThomas Huth switch (rlp->reg[i] & KVM_REG_SIZE_MASK) { 483fcf5ef2aSThomas Huth case KVM_REG_SIZE_U32: 484fcf5ef2aSThomas Huth case KVM_REG_SIZE_U64: 485fcf5ef2aSThomas Huth break; 486fcf5ef2aSThomas Huth default: 487fcf5ef2aSThomas Huth fprintf(stderr, "Can't handle size of register in kernel list\n"); 488fcf5ef2aSThomas Huth ret = -EINVAL; 489fcf5ef2aSThomas Huth goto out; 490fcf5ef2aSThomas Huth } 491fcf5ef2aSThomas Huth 492fcf5ef2aSThomas Huth arraylen++; 493fcf5ef2aSThomas Huth } 494fcf5ef2aSThomas Huth 495fcf5ef2aSThomas Huth cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen); 496fcf5ef2aSThomas Huth cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen); 497fcf5ef2aSThomas Huth cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes, 498fcf5ef2aSThomas Huth arraylen); 499fcf5ef2aSThomas Huth cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values, 500fcf5ef2aSThomas Huth arraylen); 501fcf5ef2aSThomas Huth cpu->cpreg_array_len = arraylen; 502fcf5ef2aSThomas Huth cpu->cpreg_vmstate_array_len = arraylen; 503fcf5ef2aSThomas Huth 504fcf5ef2aSThomas Huth for (i = 0, arraylen = 0; i < rlp->n; i++) { 505fcf5ef2aSThomas Huth uint64_t regidx = rlp->reg[i]; 506fcf5ef2aSThomas Huth if (!kvm_arm_reg_syncs_via_cpreg_list(regidx)) { 507fcf5ef2aSThomas Huth continue; 508fcf5ef2aSThomas Huth } 509fcf5ef2aSThomas Huth cpu->cpreg_indexes[arraylen] = regidx; 510fcf5ef2aSThomas Huth arraylen++; 511fcf5ef2aSThomas Huth } 512fcf5ef2aSThomas Huth assert(cpu->cpreg_array_len == arraylen); 513fcf5ef2aSThomas Huth 514fcf5ef2aSThomas Huth if (!write_kvmstate_to_list(cpu)) { 515fcf5ef2aSThomas Huth /* Shouldn't happen unless kernel is inconsistent about 516fcf5ef2aSThomas Huth * what registers exist. 517fcf5ef2aSThomas Huth */ 518fcf5ef2aSThomas Huth fprintf(stderr, "Initial read of kernel register state failed\n"); 519fcf5ef2aSThomas Huth ret = -EINVAL; 520fcf5ef2aSThomas Huth goto out; 521fcf5ef2aSThomas Huth } 522fcf5ef2aSThomas Huth 523fcf5ef2aSThomas Huth out: 524fcf5ef2aSThomas Huth g_free(rlp); 525fcf5ef2aSThomas Huth return ret; 526fcf5ef2aSThomas Huth } 527fcf5ef2aSThomas Huth 528fcf5ef2aSThomas Huth bool write_kvmstate_to_list(ARMCPU *cpu) 529fcf5ef2aSThomas Huth { 530fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 531fcf5ef2aSThomas Huth int i; 532fcf5ef2aSThomas Huth bool ok = true; 533fcf5ef2aSThomas Huth 534fcf5ef2aSThomas Huth for (i = 0; i < cpu->cpreg_array_len; i++) { 535fcf5ef2aSThomas Huth struct kvm_one_reg r; 536fcf5ef2aSThomas Huth uint64_t regidx = cpu->cpreg_indexes[i]; 537fcf5ef2aSThomas Huth uint32_t v32; 538fcf5ef2aSThomas Huth int ret; 539fcf5ef2aSThomas Huth 540fcf5ef2aSThomas Huth r.id = regidx; 541fcf5ef2aSThomas Huth 542fcf5ef2aSThomas Huth switch (regidx & KVM_REG_SIZE_MASK) { 543fcf5ef2aSThomas Huth case KVM_REG_SIZE_U32: 544fcf5ef2aSThomas Huth r.addr = (uintptr_t)&v32; 545fcf5ef2aSThomas Huth ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); 546fcf5ef2aSThomas Huth if (!ret) { 547fcf5ef2aSThomas Huth cpu->cpreg_values[i] = v32; 548fcf5ef2aSThomas Huth } 549fcf5ef2aSThomas Huth break; 550fcf5ef2aSThomas Huth case KVM_REG_SIZE_U64: 551fcf5ef2aSThomas Huth r.addr = (uintptr_t)(cpu->cpreg_values + i); 552fcf5ef2aSThomas Huth ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); 553fcf5ef2aSThomas Huth break; 554fcf5ef2aSThomas Huth default: 555d385a605SRichard Henderson g_assert_not_reached(); 556fcf5ef2aSThomas Huth } 557fcf5ef2aSThomas Huth if (ret) { 558fcf5ef2aSThomas Huth ok = false; 559fcf5ef2aSThomas Huth } 560fcf5ef2aSThomas Huth } 561fcf5ef2aSThomas Huth return ok; 562fcf5ef2aSThomas Huth } 563fcf5ef2aSThomas Huth 564fcf5ef2aSThomas Huth bool write_list_to_kvmstate(ARMCPU *cpu, int level) 565fcf5ef2aSThomas Huth { 566fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 567fcf5ef2aSThomas Huth int i; 568fcf5ef2aSThomas Huth bool ok = true; 569fcf5ef2aSThomas Huth 570fcf5ef2aSThomas Huth for (i = 0; i < cpu->cpreg_array_len; i++) { 571fcf5ef2aSThomas Huth struct kvm_one_reg r; 572fcf5ef2aSThomas Huth uint64_t regidx = cpu->cpreg_indexes[i]; 573fcf5ef2aSThomas Huth uint32_t v32; 574fcf5ef2aSThomas Huth int ret; 575fcf5ef2aSThomas Huth 576fcf5ef2aSThomas Huth if (kvm_arm_cpreg_level(regidx) > level) { 577fcf5ef2aSThomas Huth continue; 578fcf5ef2aSThomas Huth } 579fcf5ef2aSThomas Huth 580fcf5ef2aSThomas Huth r.id = regidx; 581fcf5ef2aSThomas Huth switch (regidx & KVM_REG_SIZE_MASK) { 582fcf5ef2aSThomas Huth case KVM_REG_SIZE_U32: 583fcf5ef2aSThomas Huth v32 = cpu->cpreg_values[i]; 584fcf5ef2aSThomas Huth r.addr = (uintptr_t)&v32; 585fcf5ef2aSThomas Huth break; 586fcf5ef2aSThomas Huth case KVM_REG_SIZE_U64: 587fcf5ef2aSThomas Huth r.addr = (uintptr_t)(cpu->cpreg_values + i); 588fcf5ef2aSThomas Huth break; 589fcf5ef2aSThomas Huth default: 590d385a605SRichard Henderson g_assert_not_reached(); 591fcf5ef2aSThomas Huth } 592fcf5ef2aSThomas Huth ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); 593fcf5ef2aSThomas Huth if (ret) { 594fcf5ef2aSThomas Huth /* We might fail for "unknown register" and also for 595fcf5ef2aSThomas Huth * "you tried to set a register which is constant with 596fcf5ef2aSThomas Huth * a different value from what it actually contains". 597fcf5ef2aSThomas Huth */ 598fcf5ef2aSThomas Huth ok = false; 599fcf5ef2aSThomas Huth } 600fcf5ef2aSThomas Huth } 601fcf5ef2aSThomas Huth return ok; 602fcf5ef2aSThomas Huth } 603fcf5ef2aSThomas Huth 604e5ac4200SAndrew Jones void kvm_arm_cpu_pre_save(ARMCPU *cpu) 605e5ac4200SAndrew Jones { 606e5ac4200SAndrew Jones /* KVM virtual time adjustment */ 607e5ac4200SAndrew Jones if (cpu->kvm_vtime_dirty) { 608e5ac4200SAndrew Jones *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT) = cpu->kvm_vtime; 609e5ac4200SAndrew Jones } 610e5ac4200SAndrew Jones } 611e5ac4200SAndrew Jones 612e5ac4200SAndrew Jones void kvm_arm_cpu_post_load(ARMCPU *cpu) 613e5ac4200SAndrew Jones { 614e5ac4200SAndrew Jones /* KVM virtual time adjustment */ 615e5ac4200SAndrew Jones if (cpu->kvm_adjvtime) { 616e5ac4200SAndrew Jones cpu->kvm_vtime = *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT); 617e5ac4200SAndrew Jones cpu->kvm_vtime_dirty = true; 618e5ac4200SAndrew Jones } 619e5ac4200SAndrew Jones } 620e5ac4200SAndrew Jones 621fcf5ef2aSThomas Huth void kvm_arm_reset_vcpu(ARMCPU *cpu) 622fcf5ef2aSThomas Huth { 623fcf5ef2aSThomas Huth int ret; 624fcf5ef2aSThomas Huth 625fcf5ef2aSThomas Huth /* Re-init VCPU so that all registers are set to 626fcf5ef2aSThomas Huth * their respective reset values. 627fcf5ef2aSThomas Huth */ 628fcf5ef2aSThomas Huth ret = kvm_arm_vcpu_init(CPU(cpu)); 629fcf5ef2aSThomas Huth if (ret < 0) { 630fcf5ef2aSThomas Huth fprintf(stderr, "kvm_arm_vcpu_init failed: %s\n", strerror(-ret)); 631fcf5ef2aSThomas Huth abort(); 632fcf5ef2aSThomas Huth } 633fcf5ef2aSThomas Huth if (!write_kvmstate_to_list(cpu)) { 634fcf5ef2aSThomas Huth fprintf(stderr, "write_kvmstate_to_list failed\n"); 635fcf5ef2aSThomas Huth abort(); 636fcf5ef2aSThomas Huth } 637b698e4eeSPeter Maydell /* 638b698e4eeSPeter Maydell * Sync the reset values also into the CPUState. This is necessary 639b698e4eeSPeter Maydell * because the next thing we do will be a kvm_arch_put_registers() 640b698e4eeSPeter Maydell * which will update the list values from the CPUState before copying 641b698e4eeSPeter Maydell * the list values back to KVM. It's OK to ignore failure returns here 642b698e4eeSPeter Maydell * for the same reason we do so in kvm_arch_get_registers(). 643b698e4eeSPeter Maydell */ 644b698e4eeSPeter Maydell write_list_to_cpustate(cpu); 645fcf5ef2aSThomas Huth } 646fcf5ef2aSThomas Huth 647fcf5ef2aSThomas Huth /* 648fcf5ef2aSThomas Huth * Update KVM's MP_STATE based on what QEMU thinks it is 649fcf5ef2aSThomas Huth */ 650fcf5ef2aSThomas Huth int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu) 651fcf5ef2aSThomas Huth { 652fcf5ef2aSThomas Huth if (cap_has_mp_state) { 653fcf5ef2aSThomas Huth struct kvm_mp_state mp_state = { 654062ba099SAlex Bennée .mp_state = (cpu->power_state == PSCI_OFF) ? 655062ba099SAlex Bennée KVM_MP_STATE_STOPPED : KVM_MP_STATE_RUNNABLE 656fcf5ef2aSThomas Huth }; 657fcf5ef2aSThomas Huth int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state); 658fcf5ef2aSThomas Huth if (ret) { 659fcf5ef2aSThomas Huth fprintf(stderr, "%s: failed to set MP_STATE %d/%s\n", 660fcf5ef2aSThomas Huth __func__, ret, strerror(-ret)); 661fcf5ef2aSThomas Huth return -1; 662fcf5ef2aSThomas Huth } 663fcf5ef2aSThomas Huth } 664fcf5ef2aSThomas Huth 665fcf5ef2aSThomas Huth return 0; 666fcf5ef2aSThomas Huth } 667fcf5ef2aSThomas Huth 668fcf5ef2aSThomas Huth /* 669fcf5ef2aSThomas Huth * Sync the KVM MP_STATE into QEMU 670fcf5ef2aSThomas Huth */ 671fcf5ef2aSThomas Huth int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu) 672fcf5ef2aSThomas Huth { 673fcf5ef2aSThomas Huth if (cap_has_mp_state) { 674fcf5ef2aSThomas Huth struct kvm_mp_state mp_state; 675fcf5ef2aSThomas Huth int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MP_STATE, &mp_state); 676fcf5ef2aSThomas Huth if (ret) { 677fcf5ef2aSThomas Huth fprintf(stderr, "%s: failed to get MP_STATE %d/%s\n", 678fcf5ef2aSThomas Huth __func__, ret, strerror(-ret)); 679fcf5ef2aSThomas Huth abort(); 680fcf5ef2aSThomas Huth } 681062ba099SAlex Bennée cpu->power_state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ? 682062ba099SAlex Bennée PSCI_OFF : PSCI_ON; 683fcf5ef2aSThomas Huth } 684fcf5ef2aSThomas Huth 685fcf5ef2aSThomas Huth return 0; 686fcf5ef2aSThomas Huth } 687fcf5ef2aSThomas Huth 688e5ac4200SAndrew Jones void kvm_arm_get_virtual_time(CPUState *cs) 689e5ac4200SAndrew Jones { 690e5ac4200SAndrew Jones ARMCPU *cpu = ARM_CPU(cs); 691e5ac4200SAndrew Jones struct kvm_one_reg reg = { 692e5ac4200SAndrew Jones .id = KVM_REG_ARM_TIMER_CNT, 693e5ac4200SAndrew Jones .addr = (uintptr_t)&cpu->kvm_vtime, 694e5ac4200SAndrew Jones }; 695e5ac4200SAndrew Jones int ret; 696e5ac4200SAndrew Jones 697e5ac4200SAndrew Jones if (cpu->kvm_vtime_dirty) { 698e5ac4200SAndrew Jones return; 699e5ac4200SAndrew Jones } 700e5ac4200SAndrew Jones 701e5ac4200SAndrew Jones ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 702e5ac4200SAndrew Jones if (ret) { 703e5ac4200SAndrew Jones error_report("Failed to get KVM_REG_ARM_TIMER_CNT"); 704e5ac4200SAndrew Jones abort(); 705e5ac4200SAndrew Jones } 706e5ac4200SAndrew Jones 707e5ac4200SAndrew Jones cpu->kvm_vtime_dirty = true; 708e5ac4200SAndrew Jones } 709e5ac4200SAndrew Jones 710e5ac4200SAndrew Jones void kvm_arm_put_virtual_time(CPUState *cs) 711e5ac4200SAndrew Jones { 712e5ac4200SAndrew Jones ARMCPU *cpu = ARM_CPU(cs); 713e5ac4200SAndrew Jones struct kvm_one_reg reg = { 714e5ac4200SAndrew Jones .id = KVM_REG_ARM_TIMER_CNT, 715e5ac4200SAndrew Jones .addr = (uintptr_t)&cpu->kvm_vtime, 716e5ac4200SAndrew Jones }; 717e5ac4200SAndrew Jones int ret; 718e5ac4200SAndrew Jones 719e5ac4200SAndrew Jones if (!cpu->kvm_vtime_dirty) { 720e5ac4200SAndrew Jones return; 721e5ac4200SAndrew Jones } 722e5ac4200SAndrew Jones 723e5ac4200SAndrew Jones ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 724e5ac4200SAndrew Jones if (ret) { 725e5ac4200SAndrew Jones error_report("Failed to set KVM_REG_ARM_TIMER_CNT"); 726e5ac4200SAndrew Jones abort(); 727e5ac4200SAndrew Jones } 728e5ac4200SAndrew Jones 729e5ac4200SAndrew Jones cpu->kvm_vtime_dirty = false; 730e5ac4200SAndrew Jones } 731e5ac4200SAndrew Jones 732202ccb6bSDongjiu Geng int kvm_put_vcpu_events(ARMCPU *cpu) 733202ccb6bSDongjiu Geng { 734202ccb6bSDongjiu Geng CPUARMState *env = &cpu->env; 735202ccb6bSDongjiu Geng struct kvm_vcpu_events events; 736202ccb6bSDongjiu Geng int ret; 737202ccb6bSDongjiu Geng 738202ccb6bSDongjiu Geng if (!kvm_has_vcpu_events()) { 739202ccb6bSDongjiu Geng return 0; 740202ccb6bSDongjiu Geng } 741202ccb6bSDongjiu Geng 742202ccb6bSDongjiu Geng memset(&events, 0, sizeof(events)); 743202ccb6bSDongjiu Geng events.exception.serror_pending = env->serror.pending; 744202ccb6bSDongjiu Geng 745202ccb6bSDongjiu Geng /* Inject SError to guest with specified syndrome if host kernel 746202ccb6bSDongjiu Geng * supports it, otherwise inject SError without syndrome. 747202ccb6bSDongjiu Geng */ 748202ccb6bSDongjiu Geng if (cap_has_inject_serror_esr) { 749202ccb6bSDongjiu Geng events.exception.serror_has_esr = env->serror.has_esr; 750202ccb6bSDongjiu Geng events.exception.serror_esr = env->serror.esr; 751202ccb6bSDongjiu Geng } 752202ccb6bSDongjiu Geng 753202ccb6bSDongjiu Geng ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events); 754202ccb6bSDongjiu Geng if (ret) { 755202ccb6bSDongjiu Geng error_report("failed to put vcpu events"); 756202ccb6bSDongjiu Geng } 757202ccb6bSDongjiu Geng 758202ccb6bSDongjiu Geng return ret; 759202ccb6bSDongjiu Geng } 760202ccb6bSDongjiu Geng 761202ccb6bSDongjiu Geng int kvm_get_vcpu_events(ARMCPU *cpu) 762202ccb6bSDongjiu Geng { 763202ccb6bSDongjiu Geng CPUARMState *env = &cpu->env; 764202ccb6bSDongjiu Geng struct kvm_vcpu_events events; 765202ccb6bSDongjiu Geng int ret; 766202ccb6bSDongjiu Geng 767202ccb6bSDongjiu Geng if (!kvm_has_vcpu_events()) { 768202ccb6bSDongjiu Geng return 0; 769202ccb6bSDongjiu Geng } 770202ccb6bSDongjiu Geng 771202ccb6bSDongjiu Geng memset(&events, 0, sizeof(events)); 772202ccb6bSDongjiu Geng ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events); 773202ccb6bSDongjiu Geng if (ret) { 774202ccb6bSDongjiu Geng error_report("failed to get vcpu events"); 775202ccb6bSDongjiu Geng return ret; 776202ccb6bSDongjiu Geng } 777202ccb6bSDongjiu Geng 778202ccb6bSDongjiu Geng env->serror.pending = events.exception.serror_pending; 779202ccb6bSDongjiu Geng env->serror.has_esr = events.exception.serror_has_esr; 780202ccb6bSDongjiu Geng env->serror.esr = events.exception.serror_esr; 781202ccb6bSDongjiu Geng 782202ccb6bSDongjiu Geng return 0; 783202ccb6bSDongjiu Geng } 784202ccb6bSDongjiu Geng 785fcf5ef2aSThomas Huth void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) 786fcf5ef2aSThomas Huth { 7871711bfa5SBeata Michalska ARMCPU *cpu = ARM_CPU(cs); 7881711bfa5SBeata Michalska CPUARMState *env = &cpu->env; 7891711bfa5SBeata Michalska 7901711bfa5SBeata Michalska if (unlikely(env->ext_dabt_raised)) { 7911711bfa5SBeata Michalska /* 7921711bfa5SBeata Michalska * Verifying that the ext DABT has been properly injected, 7931711bfa5SBeata Michalska * otherwise risking indefinitely re-running the faulting instruction 7941711bfa5SBeata Michalska * Covering a very narrow case for kernels 5.5..5.5.4 7951711bfa5SBeata Michalska * when injected abort was misconfigured to be 7961711bfa5SBeata Michalska * an IMPLEMENTATION DEFINED exception (for 32-bit EL1) 7971711bfa5SBeata Michalska */ 7981711bfa5SBeata Michalska if (!arm_feature(env, ARM_FEATURE_AARCH64) && 7991711bfa5SBeata Michalska unlikely(!kvm_arm_verify_ext_dabt_pending(cs))) { 8001711bfa5SBeata Michalska 8011711bfa5SBeata Michalska error_report("Data abort exception with no valid ISS generated by " 8021711bfa5SBeata Michalska "guest memory access. KVM unable to emulate faulting " 8031711bfa5SBeata Michalska "instruction. Failed to inject an external data abort " 8041711bfa5SBeata Michalska "into the guest."); 8051711bfa5SBeata Michalska abort(); 8061711bfa5SBeata Michalska } 8071711bfa5SBeata Michalska /* Clear the status */ 8081711bfa5SBeata Michalska env->ext_dabt_raised = 0; 8091711bfa5SBeata Michalska } 810fcf5ef2aSThomas Huth } 811fcf5ef2aSThomas Huth 812fcf5ef2aSThomas Huth MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) 813fcf5ef2aSThomas Huth { 8145d721b78SAlexander Graf ARMCPU *cpu; 8155d721b78SAlexander Graf uint32_t switched_level; 8165d721b78SAlexander Graf 8175d721b78SAlexander Graf if (kvm_irqchip_in_kernel()) { 8185d721b78SAlexander Graf /* 8195d721b78SAlexander Graf * We only need to sync timer states with user-space interrupt 8205d721b78SAlexander Graf * controllers, so return early and save cycles if we don't. 8215d721b78SAlexander Graf */ 8225d721b78SAlexander Graf return MEMTXATTRS_UNSPECIFIED; 8235d721b78SAlexander Graf } 8245d721b78SAlexander Graf 8255d721b78SAlexander Graf cpu = ARM_CPU(cs); 8265d721b78SAlexander Graf 8275d721b78SAlexander Graf /* Synchronize our shadowed in-kernel device irq lines with the kvm ones */ 8285d721b78SAlexander Graf if (run->s.regs.device_irq_level != cpu->device_irq_level) { 8295d721b78SAlexander Graf switched_level = cpu->device_irq_level ^ run->s.regs.device_irq_level; 8305d721b78SAlexander Graf 8315d721b78SAlexander Graf qemu_mutex_lock_iothread(); 8325d721b78SAlexander Graf 8335d721b78SAlexander Graf if (switched_level & KVM_ARM_DEV_EL1_VTIMER) { 8345d721b78SAlexander Graf qemu_set_irq(cpu->gt_timer_outputs[GTIMER_VIRT], 8355d721b78SAlexander Graf !!(run->s.regs.device_irq_level & 8365d721b78SAlexander Graf KVM_ARM_DEV_EL1_VTIMER)); 8375d721b78SAlexander Graf switched_level &= ~KVM_ARM_DEV_EL1_VTIMER; 8385d721b78SAlexander Graf } 8395d721b78SAlexander Graf 8405d721b78SAlexander Graf if (switched_level & KVM_ARM_DEV_EL1_PTIMER) { 8415d721b78SAlexander Graf qemu_set_irq(cpu->gt_timer_outputs[GTIMER_PHYS], 8425d721b78SAlexander Graf !!(run->s.regs.device_irq_level & 8435d721b78SAlexander Graf KVM_ARM_DEV_EL1_PTIMER)); 8445d721b78SAlexander Graf switched_level &= ~KVM_ARM_DEV_EL1_PTIMER; 8455d721b78SAlexander Graf } 8465d721b78SAlexander Graf 847b1659527SAndrew Jones if (switched_level & KVM_ARM_DEV_PMU) { 848b1659527SAndrew Jones qemu_set_irq(cpu->pmu_interrupt, 849b1659527SAndrew Jones !!(run->s.regs.device_irq_level & KVM_ARM_DEV_PMU)); 850b1659527SAndrew Jones switched_level &= ~KVM_ARM_DEV_PMU; 851b1659527SAndrew Jones } 8525d721b78SAlexander Graf 8535d721b78SAlexander Graf if (switched_level) { 8545d721b78SAlexander Graf qemu_log_mask(LOG_UNIMP, "%s: unhandled in-kernel device IRQ %x\n", 8555d721b78SAlexander Graf __func__, switched_level); 8565d721b78SAlexander Graf } 8575d721b78SAlexander Graf 8585d721b78SAlexander Graf /* We also mark unknown levels as processed to not waste cycles */ 8595d721b78SAlexander Graf cpu->device_irq_level = run->s.regs.device_irq_level; 8605d721b78SAlexander Graf qemu_mutex_unlock_iothread(); 8615d721b78SAlexander Graf } 8625d721b78SAlexander Graf 863fcf5ef2aSThomas Huth return MEMTXATTRS_UNSPECIFIED; 864fcf5ef2aSThomas Huth } 865fcf5ef2aSThomas Huth 866538f0497SPhilippe Mathieu-Daudé void kvm_arm_vm_state_change(void *opaque, bool running, RunState state) 867e5ac4200SAndrew Jones { 868e5ac4200SAndrew Jones CPUState *cs = opaque; 869e5ac4200SAndrew Jones ARMCPU *cpu = ARM_CPU(cs); 870e5ac4200SAndrew Jones 871e5ac4200SAndrew Jones if (running) { 872e5ac4200SAndrew Jones if (cpu->kvm_adjvtime) { 873e5ac4200SAndrew Jones kvm_arm_put_virtual_time(cs); 874e5ac4200SAndrew Jones } 875e5ac4200SAndrew Jones } else { 876e5ac4200SAndrew Jones if (cpu->kvm_adjvtime) { 877e5ac4200SAndrew Jones kvm_arm_get_virtual_time(cs); 878e5ac4200SAndrew Jones } 879e5ac4200SAndrew Jones } 880e5ac4200SAndrew Jones } 881fcf5ef2aSThomas Huth 882694bcaa8SBeata Michalska /** 883694bcaa8SBeata Michalska * kvm_arm_handle_dabt_nisv: 884694bcaa8SBeata Michalska * @cs: CPUState 885694bcaa8SBeata Michalska * @esr_iss: ISS encoding (limited) for the exception from Data Abort 886694bcaa8SBeata Michalska * ISV bit set to '0b0' -> no valid instruction syndrome 887694bcaa8SBeata Michalska * @fault_ipa: faulting address for the synchronous data abort 888694bcaa8SBeata Michalska * 889694bcaa8SBeata Michalska * Returns: 0 if the exception has been handled, < 0 otherwise 890694bcaa8SBeata Michalska */ 891694bcaa8SBeata Michalska static int kvm_arm_handle_dabt_nisv(CPUState *cs, uint64_t esr_iss, 892694bcaa8SBeata Michalska uint64_t fault_ipa) 893694bcaa8SBeata Michalska { 8941711bfa5SBeata Michalska ARMCPU *cpu = ARM_CPU(cs); 8951711bfa5SBeata Michalska CPUARMState *env = &cpu->env; 896694bcaa8SBeata Michalska /* 897694bcaa8SBeata Michalska * Request KVM to inject the external data abort into the guest 898694bcaa8SBeata Michalska */ 899694bcaa8SBeata Michalska if (cap_has_inject_ext_dabt) { 900694bcaa8SBeata Michalska struct kvm_vcpu_events events = { }; 901694bcaa8SBeata Michalska /* 902694bcaa8SBeata Michalska * The external data abort event will be handled immediately by KVM 903694bcaa8SBeata Michalska * using the address fault that triggered the exit on given VCPU. 904694bcaa8SBeata Michalska * Requesting injection of the external data abort does not rely 905694bcaa8SBeata Michalska * on any other VCPU state. Therefore, in this particular case, the VCPU 906694bcaa8SBeata Michalska * synchronization can be exceptionally skipped. 907694bcaa8SBeata Michalska */ 908694bcaa8SBeata Michalska events.exception.ext_dabt_pending = 1; 909694bcaa8SBeata Michalska /* KVM_CAP_ARM_INJECT_EXT_DABT implies KVM_CAP_VCPU_EVENTS */ 9101711bfa5SBeata Michalska if (!kvm_vcpu_ioctl(cs, KVM_SET_VCPU_EVENTS, &events)) { 9111711bfa5SBeata Michalska env->ext_dabt_raised = 1; 9121711bfa5SBeata Michalska return 0; 9131711bfa5SBeata Michalska } 914694bcaa8SBeata Michalska } else { 915694bcaa8SBeata Michalska error_report("Data abort exception triggered by guest memory access " 916694bcaa8SBeata Michalska "at physical address: 0x" TARGET_FMT_lx, 917694bcaa8SBeata Michalska (target_ulong)fault_ipa); 918694bcaa8SBeata Michalska error_printf("KVM unable to emulate faulting instruction.\n"); 919694bcaa8SBeata Michalska } 920694bcaa8SBeata Michalska return -1; 921694bcaa8SBeata Michalska } 922694bcaa8SBeata Michalska 923fcf5ef2aSThomas Huth int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) 924fcf5ef2aSThomas Huth { 925fcf5ef2aSThomas Huth int ret = 0; 926fcf5ef2aSThomas Huth 927fcf5ef2aSThomas Huth switch (run->exit_reason) { 928fcf5ef2aSThomas Huth case KVM_EXIT_DEBUG: 929fcf5ef2aSThomas Huth if (kvm_arm_handle_debug(cs, &run->debug.arch)) { 930fcf5ef2aSThomas Huth ret = EXCP_DEBUG; 931fcf5ef2aSThomas Huth } /* otherwise return to guest */ 932fcf5ef2aSThomas Huth break; 933694bcaa8SBeata Michalska case KVM_EXIT_ARM_NISV: 934694bcaa8SBeata Michalska /* External DABT with no valid iss to decode */ 935694bcaa8SBeata Michalska ret = kvm_arm_handle_dabt_nisv(cs, run->arm_nisv.esr_iss, 936694bcaa8SBeata Michalska run->arm_nisv.fault_ipa); 937694bcaa8SBeata Michalska break; 938fcf5ef2aSThomas Huth default: 939fcf5ef2aSThomas Huth qemu_log_mask(LOG_UNIMP, "%s: un-handled exit reason %d\n", 940fcf5ef2aSThomas Huth __func__, run->exit_reason); 941fcf5ef2aSThomas Huth break; 942fcf5ef2aSThomas Huth } 943fcf5ef2aSThomas Huth return ret; 944fcf5ef2aSThomas Huth } 945fcf5ef2aSThomas Huth 946fcf5ef2aSThomas Huth bool kvm_arch_stop_on_emulation_error(CPUState *cs) 947fcf5ef2aSThomas Huth { 948fcf5ef2aSThomas Huth return true; 949fcf5ef2aSThomas Huth } 950fcf5ef2aSThomas Huth 951fcf5ef2aSThomas Huth int kvm_arch_process_async_events(CPUState *cs) 952fcf5ef2aSThomas Huth { 953fcf5ef2aSThomas Huth return 0; 954fcf5ef2aSThomas Huth } 955fcf5ef2aSThomas Huth 956fcf5ef2aSThomas Huth void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg) 957fcf5ef2aSThomas Huth { 958fcf5ef2aSThomas Huth if (kvm_sw_breakpoints_active(cs)) { 959fcf5ef2aSThomas Huth dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; 960fcf5ef2aSThomas Huth } 961fcf5ef2aSThomas Huth if (kvm_arm_hw_debug_active(cs)) { 962fcf5ef2aSThomas Huth dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW; 963fcf5ef2aSThomas Huth kvm_arm_copy_hw_debug_data(&dbg->arch); 964fcf5ef2aSThomas Huth } 965fcf5ef2aSThomas Huth } 966fcf5ef2aSThomas Huth 967fcf5ef2aSThomas Huth void kvm_arch_init_irq_routing(KVMState *s) 968fcf5ef2aSThomas Huth { 969fcf5ef2aSThomas Huth } 970fcf5ef2aSThomas Huth 9714376c40dSPaolo Bonzini int kvm_arch_irqchip_create(KVMState *s) 972fcf5ef2aSThomas Huth { 9734376c40dSPaolo Bonzini if (kvm_kernel_irqchip_split()) { 97447c182feSCornelia Huck error_report("-machine kernel_irqchip=split is not supported on ARM."); 975fcf5ef2aSThomas Huth exit(1); 976fcf5ef2aSThomas Huth } 977fcf5ef2aSThomas Huth 978fcf5ef2aSThomas Huth /* If we can create the VGIC using the newer device control API, we 979fcf5ef2aSThomas Huth * let the device do this when it initializes itself, otherwise we 980fcf5ef2aSThomas Huth * fall back to the old API */ 981fcf5ef2aSThomas Huth return kvm_check_extension(s, KVM_CAP_DEVICE_CTRL); 982fcf5ef2aSThomas Huth } 983fcf5ef2aSThomas Huth 984fcf5ef2aSThomas Huth int kvm_arm_vgic_probe(void) 985fcf5ef2aSThomas Huth { 986d45efe47SEric Auger int val = 0; 987d45efe47SEric Auger 988fcf5ef2aSThomas Huth if (kvm_create_device(kvm_state, 989fcf5ef2aSThomas Huth KVM_DEV_TYPE_ARM_VGIC_V3, true) == 0) { 990d45efe47SEric Auger val |= KVM_ARM_VGIC_V3; 991fcf5ef2aSThomas Huth } 992d45efe47SEric Auger if (kvm_create_device(kvm_state, 993d45efe47SEric Auger KVM_DEV_TYPE_ARM_VGIC_V2, true) == 0) { 994d45efe47SEric Auger val |= KVM_ARM_VGIC_V2; 995d45efe47SEric Auger } 996d45efe47SEric Auger return val; 997fcf5ef2aSThomas Huth } 998fcf5ef2aSThomas Huth 999f6530926SEric Auger int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level) 1000f6530926SEric Auger { 1001f6530926SEric Auger int kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT) | irq; 1002f6530926SEric Auger int cpu_idx1 = cpu % 256; 1003f6530926SEric Auger int cpu_idx2 = cpu / 256; 1004f6530926SEric Auger 1005f6530926SEric Auger kvm_irq |= (cpu_idx1 << KVM_ARM_IRQ_VCPU_SHIFT) | 1006f6530926SEric Auger (cpu_idx2 << KVM_ARM_IRQ_VCPU2_SHIFT); 1007f6530926SEric Auger 1008f6530926SEric Auger return kvm_set_irq(kvm_state, kvm_irq, !!level); 1009f6530926SEric Auger } 1010f6530926SEric Auger 1011fcf5ef2aSThomas Huth int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 1012fcf5ef2aSThomas Huth uint64_t address, uint32_t data, PCIDevice *dev) 1013fcf5ef2aSThomas Huth { 1014b05c81d2SEric Auger AddressSpace *as = pci_device_iommu_address_space(dev); 1015b05c81d2SEric Auger hwaddr xlat, len, doorbell_gpa; 1016b05c81d2SEric Auger MemoryRegionSection mrs; 1017b05c81d2SEric Auger MemoryRegion *mr; 1018b05c81d2SEric Auger 1019b05c81d2SEric Auger if (as == &address_space_memory) { 1020fcf5ef2aSThomas Huth return 0; 1021fcf5ef2aSThomas Huth } 1022fcf5ef2aSThomas Huth 1023b05c81d2SEric Auger /* MSI doorbell address is translated by an IOMMU */ 1024b05c81d2SEric Auger 1025dfa0d9b8SHamza Mahfooz RCU_READ_LOCK_GUARD(); 1026dfa0d9b8SHamza Mahfooz 1027bc6b1cecSPeter Maydell mr = address_space_translate(as, address, &xlat, &len, true, 1028bc6b1cecSPeter Maydell MEMTXATTRS_UNSPECIFIED); 1029dfa0d9b8SHamza Mahfooz 1030b05c81d2SEric Auger if (!mr) { 1031dfa0d9b8SHamza Mahfooz return 1; 1032b05c81d2SEric Auger } 1033dfa0d9b8SHamza Mahfooz 1034b05c81d2SEric Auger mrs = memory_region_find(mr, xlat, 1); 1035dfa0d9b8SHamza Mahfooz 1036b05c81d2SEric Auger if (!mrs.mr) { 1037dfa0d9b8SHamza Mahfooz return 1; 1038b05c81d2SEric Auger } 1039b05c81d2SEric Auger 1040b05c81d2SEric Auger doorbell_gpa = mrs.offset_within_address_space; 1041b05c81d2SEric Auger memory_region_unref(mrs.mr); 1042b05c81d2SEric Auger 1043b05c81d2SEric Auger route->u.msi.address_lo = doorbell_gpa; 1044b05c81d2SEric Auger route->u.msi.address_hi = doorbell_gpa >> 32; 1045b05c81d2SEric Auger 1046b05c81d2SEric Auger trace_kvm_arm_fixup_msi_route(address, doorbell_gpa); 1047b05c81d2SEric Auger 1048dfa0d9b8SHamza Mahfooz return 0; 1049b05c81d2SEric Auger } 1050b05c81d2SEric Auger 1051fcf5ef2aSThomas Huth int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 1052fcf5ef2aSThomas Huth int vector, PCIDevice *dev) 1053fcf5ef2aSThomas Huth { 1054fcf5ef2aSThomas Huth return 0; 1055fcf5ef2aSThomas Huth } 1056fcf5ef2aSThomas Huth 1057fcf5ef2aSThomas Huth int kvm_arch_release_virq_post(int virq) 1058fcf5ef2aSThomas Huth { 1059fcf5ef2aSThomas Huth return 0; 1060fcf5ef2aSThomas Huth } 1061fcf5ef2aSThomas Huth 1062fcf5ef2aSThomas Huth int kvm_arch_msi_data_to_gsi(uint32_t data) 1063fcf5ef2aSThomas Huth { 1064fcf5ef2aSThomas Huth return (data - 32) & 0xffff; 1065fcf5ef2aSThomas Huth } 106692a5199bSTom Lendacky 106792a5199bSTom Lendacky bool kvm_arch_cpu_check_are_resettable(void) 106892a5199bSTom Lendacky { 106992a5199bSTom Lendacky return true; 107092a5199bSTom Lendacky } 10713dba0a33SPaolo Bonzini 10723dba0a33SPaolo Bonzini void kvm_arch_accel_class_init(ObjectClass *oc) 10733dba0a33SPaolo Bonzini { 10743dba0a33SPaolo Bonzini } 1075