xref: /qemu/target/arm/kvm.c (revision bcb902a1)
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-common.h"
17fcf5ef2aSThomas Huth #include "qemu/timer.h"
18fcf5ef2aSThomas Huth #include "qemu/error-report.h"
19db725815SMarkus Armbruster #include "qemu/main-loop.h"
20dea101a1SAndrew Jones #include "qom/object.h"
21dea101a1SAndrew Jones #include "qapi/error.h"
22fcf5ef2aSThomas Huth #include "sysemu/sysemu.h"
23fcf5ef2aSThomas Huth #include "sysemu/kvm.h"
24a27382e2SEric Auger #include "sysemu/kvm_int.h"
25fcf5ef2aSThomas Huth #include "kvm_arm.h"
26fcf5ef2aSThomas Huth #include "cpu.h"
27b05c81d2SEric Auger #include "trace.h"
28fcf5ef2aSThomas Huth #include "internals.h"
29b05c81d2SEric Auger #include "hw/pci/pci.h"
30fcf5ef2aSThomas Huth #include "exec/memattrs.h"
31fcf5ef2aSThomas Huth #include "exec/address-spaces.h"
32fcf5ef2aSThomas Huth #include "hw/boards.h"
3364552b6bSMarkus Armbruster #include "hw/irq.h"
34fcf5ef2aSThomas Huth #include "qemu/log.h"
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
37fcf5ef2aSThomas Huth     KVM_CAP_LAST_INFO
38fcf5ef2aSThomas Huth };
39fcf5ef2aSThomas Huth 
40fcf5ef2aSThomas Huth static bool cap_has_mp_state;
41202ccb6bSDongjiu Geng static bool cap_has_inject_serror_esr;
42694bcaa8SBeata Michalska static bool cap_has_inject_ext_dabt;
43fcf5ef2aSThomas Huth 
44c4487d76SPeter Maydell static ARMHostCPUFeatures arm_host_cpu_features;
45c4487d76SPeter Maydell 
46fcf5ef2aSThomas Huth int kvm_arm_vcpu_init(CPUState *cs)
47fcf5ef2aSThomas Huth {
48fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
49fcf5ef2aSThomas Huth     struct kvm_vcpu_init init;
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     init.target = cpu->kvm_target;
52fcf5ef2aSThomas Huth     memcpy(init.features, cpu->kvm_init_features, sizeof(init.features));
53fcf5ef2aSThomas Huth 
54fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init);
55fcf5ef2aSThomas Huth }
56fcf5ef2aSThomas Huth 
5714e99e0fSAndrew Jones int kvm_arm_vcpu_finalize(CPUState *cs, int feature)
5814e99e0fSAndrew Jones {
5914e99e0fSAndrew Jones     return kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_FINALIZE, &feature);
6014e99e0fSAndrew Jones }
6114e99e0fSAndrew Jones 
62202ccb6bSDongjiu Geng void kvm_arm_init_serror_injection(CPUState *cs)
63202ccb6bSDongjiu Geng {
64202ccb6bSDongjiu Geng     cap_has_inject_serror_esr = kvm_check_extension(cs->kvm_state,
65202ccb6bSDongjiu Geng                                     KVM_CAP_ARM_INJECT_SERROR_ESR);
66202ccb6bSDongjiu Geng }
67202ccb6bSDongjiu Geng 
68fcf5ef2aSThomas Huth bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
69fcf5ef2aSThomas Huth                                       int *fdarray,
70fcf5ef2aSThomas Huth                                       struct kvm_vcpu_init *init)
71fcf5ef2aSThomas Huth {
720cdb4020SAndrew Jones     int ret = 0, kvmfd = -1, vmfd = -1, cpufd = -1;
73fcf5ef2aSThomas Huth 
74448058aaSDaniel P. Berrangé     kvmfd = qemu_open_old("/dev/kvm", O_RDWR);
75fcf5ef2aSThomas Huth     if (kvmfd < 0) {
76fcf5ef2aSThomas Huth         goto err;
77fcf5ef2aSThomas Huth     }
78fcf5ef2aSThomas Huth     vmfd = ioctl(kvmfd, KVM_CREATE_VM, 0);
79fcf5ef2aSThomas Huth     if (vmfd < 0) {
80fcf5ef2aSThomas Huth         goto err;
81fcf5ef2aSThomas Huth     }
82fcf5ef2aSThomas Huth     cpufd = ioctl(vmfd, KVM_CREATE_VCPU, 0);
83fcf5ef2aSThomas Huth     if (cpufd < 0) {
84fcf5ef2aSThomas Huth         goto err;
85fcf5ef2aSThomas Huth     }
86fcf5ef2aSThomas Huth 
87fcf5ef2aSThomas Huth     if (!init) {
88fcf5ef2aSThomas Huth         /* Caller doesn't want the VCPU to be initialized, so skip it */
89fcf5ef2aSThomas Huth         goto finish;
90fcf5ef2aSThomas Huth     }
91fcf5ef2aSThomas Huth 
920cdb4020SAndrew Jones     if (init->target == -1) {
930cdb4020SAndrew Jones         struct kvm_vcpu_init preferred;
940cdb4020SAndrew Jones 
950cdb4020SAndrew Jones         ret = ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, &preferred);
960cdb4020SAndrew Jones         if (!ret) {
970cdb4020SAndrew Jones             init->target = preferred.target;
980cdb4020SAndrew Jones         }
990cdb4020SAndrew Jones     }
100fcf5ef2aSThomas Huth     if (ret >= 0) {
101fcf5ef2aSThomas Huth         ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, init);
102fcf5ef2aSThomas Huth         if (ret < 0) {
103fcf5ef2aSThomas Huth             goto err;
104fcf5ef2aSThomas Huth         }
105fcf5ef2aSThomas Huth     } else if (cpus_to_try) {
106fcf5ef2aSThomas Huth         /* Old kernel which doesn't know about the
107fcf5ef2aSThomas Huth          * PREFERRED_TARGET ioctl: we know it will only support
108fcf5ef2aSThomas Huth          * creating one kind of guest CPU which is its preferred
109fcf5ef2aSThomas Huth          * CPU type.
110fcf5ef2aSThomas Huth          */
1110cdb4020SAndrew Jones         struct kvm_vcpu_init try;
1120cdb4020SAndrew Jones 
113fcf5ef2aSThomas Huth         while (*cpus_to_try != QEMU_KVM_ARM_TARGET_NONE) {
1140cdb4020SAndrew Jones             try.target = *cpus_to_try++;
1150cdb4020SAndrew Jones             memcpy(try.features, init->features, sizeof(init->features));
1160cdb4020SAndrew Jones             ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, &try);
117fcf5ef2aSThomas Huth             if (ret >= 0) {
118fcf5ef2aSThomas Huth                 break;
119fcf5ef2aSThomas Huth             }
120fcf5ef2aSThomas Huth         }
121fcf5ef2aSThomas Huth         if (ret < 0) {
122fcf5ef2aSThomas Huth             goto err;
123fcf5ef2aSThomas Huth         }
1240cdb4020SAndrew Jones         init->target = try.target;
125fcf5ef2aSThomas Huth     } else {
126fcf5ef2aSThomas Huth         /* Treat a NULL cpus_to_try argument the same as an empty
127fcf5ef2aSThomas Huth          * list, which means we will fail the call since this must
128fcf5ef2aSThomas Huth          * be an old kernel which doesn't support PREFERRED_TARGET.
129fcf5ef2aSThomas Huth          */
130fcf5ef2aSThomas Huth         goto err;
131fcf5ef2aSThomas Huth     }
132fcf5ef2aSThomas Huth 
133fcf5ef2aSThomas Huth finish:
134fcf5ef2aSThomas Huth     fdarray[0] = kvmfd;
135fcf5ef2aSThomas Huth     fdarray[1] = vmfd;
136fcf5ef2aSThomas Huth     fdarray[2] = cpufd;
137fcf5ef2aSThomas Huth 
138fcf5ef2aSThomas Huth     return true;
139fcf5ef2aSThomas Huth 
140fcf5ef2aSThomas Huth err:
141fcf5ef2aSThomas Huth     if (cpufd >= 0) {
142fcf5ef2aSThomas Huth         close(cpufd);
143fcf5ef2aSThomas Huth     }
144fcf5ef2aSThomas Huth     if (vmfd >= 0) {
145fcf5ef2aSThomas Huth         close(vmfd);
146fcf5ef2aSThomas Huth     }
147fcf5ef2aSThomas Huth     if (kvmfd >= 0) {
148fcf5ef2aSThomas Huth         close(kvmfd);
149fcf5ef2aSThomas Huth     }
150fcf5ef2aSThomas Huth 
151fcf5ef2aSThomas Huth     return false;
152fcf5ef2aSThomas Huth }
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth void kvm_arm_destroy_scratch_host_vcpu(int *fdarray)
155fcf5ef2aSThomas Huth {
156fcf5ef2aSThomas Huth     int i;
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth     for (i = 2; i >= 0; i--) {
159fcf5ef2aSThomas Huth         close(fdarray[i]);
160fcf5ef2aSThomas Huth     }
161fcf5ef2aSThomas Huth }
162fcf5ef2aSThomas Huth 
163c4487d76SPeter Maydell void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu)
164fcf5ef2aSThomas Huth {
165c4487d76SPeter Maydell     CPUARMState *env = &cpu->env;
166fcf5ef2aSThomas Huth 
167c4487d76SPeter Maydell     if (!arm_host_cpu_features.dtb_compatible) {
168c4487d76SPeter Maydell         if (!kvm_enabled() ||
169c4487d76SPeter Maydell             !kvm_arm_get_host_cpu_features(&arm_host_cpu_features)) {
170c4487d76SPeter Maydell             /* We can't report this error yet, so flag that we need to
171c4487d76SPeter Maydell              * in arm_cpu_realizefn().
172fcf5ef2aSThomas Huth              */
173c4487d76SPeter Maydell             cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
174c4487d76SPeter Maydell             cpu->host_cpu_probe_failed = true;
175c4487d76SPeter Maydell             return;
176fcf5ef2aSThomas Huth         }
177fcf5ef2aSThomas Huth     }
178fcf5ef2aSThomas Huth 
179c4487d76SPeter Maydell     cpu->kvm_target = arm_host_cpu_features.target;
180c4487d76SPeter Maydell     cpu->dtb_compatible = arm_host_cpu_features.dtb_compatible;
1814674097cSRichard Henderson     cpu->isar = arm_host_cpu_features.isar;
182c4487d76SPeter Maydell     env->features = arm_host_cpu_features.features;
183c4487d76SPeter Maydell }
184c4487d76SPeter Maydell 
185dea101a1SAndrew Jones static bool kvm_no_adjvtime_get(Object *obj, Error **errp)
186dea101a1SAndrew Jones {
187dea101a1SAndrew Jones     return !ARM_CPU(obj)->kvm_adjvtime;
188dea101a1SAndrew Jones }
189dea101a1SAndrew Jones 
190dea101a1SAndrew Jones static void kvm_no_adjvtime_set(Object *obj, bool value, Error **errp)
191dea101a1SAndrew Jones {
192dea101a1SAndrew Jones     ARM_CPU(obj)->kvm_adjvtime = !value;
193dea101a1SAndrew Jones }
194dea101a1SAndrew Jones 
19568970d1eSAndrew Jones static bool kvm_steal_time_get(Object *obj, Error **errp)
19668970d1eSAndrew Jones {
19768970d1eSAndrew Jones     return ARM_CPU(obj)->kvm_steal_time != ON_OFF_AUTO_OFF;
19868970d1eSAndrew Jones }
19968970d1eSAndrew Jones 
20068970d1eSAndrew Jones static void kvm_steal_time_set(Object *obj, bool value, Error **errp)
20168970d1eSAndrew Jones {
20268970d1eSAndrew Jones     ARM_CPU(obj)->kvm_steal_time = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
20368970d1eSAndrew Jones }
20468970d1eSAndrew Jones 
205dea101a1SAndrew Jones /* KVM VCPU properties should be prefixed with "kvm-". */
206dea101a1SAndrew Jones void kvm_arm_add_vcpu_properties(Object *obj)
207dea101a1SAndrew Jones {
2089e6f8d8aSfangying     ARMCPU *cpu = ARM_CPU(obj);
2099e6f8d8aSfangying     CPUARMState *env = &cpu->env;
210dea101a1SAndrew Jones 
2119e6f8d8aSfangying     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2129e6f8d8aSfangying         cpu->kvm_adjvtime = true;
213dea101a1SAndrew Jones         object_property_add_bool(obj, "kvm-no-adjvtime", kvm_no_adjvtime_get,
214d2623129SMarkus Armbruster                                  kvm_no_adjvtime_set);
215dea101a1SAndrew Jones         object_property_set_description(obj, "kvm-no-adjvtime",
216dea101a1SAndrew Jones                                         "Set on to disable the adjustment of "
217dea101a1SAndrew Jones                                         "the virtual counter. VM stopped time "
2187eecec7dSMarkus Armbruster                                         "will be counted.");
219dea101a1SAndrew Jones     }
22068970d1eSAndrew Jones 
22168970d1eSAndrew Jones     cpu->kvm_steal_time = ON_OFF_AUTO_AUTO;
22268970d1eSAndrew Jones     object_property_add_bool(obj, "kvm-steal-time", kvm_steal_time_get,
22368970d1eSAndrew Jones                              kvm_steal_time_set);
22468970d1eSAndrew Jones     object_property_set_description(obj, "kvm-steal-time",
22568970d1eSAndrew Jones                                     "Set off to disable KVM steal time.");
2269e6f8d8aSfangying }
227dea101a1SAndrew Jones 
2287d20e681SPhilippe Mathieu-Daudé bool kvm_arm_pmu_supported(void)
229ae502508SAndrew Jones {
2307d20e681SPhilippe Mathieu-Daudé     return kvm_check_extension(kvm_state, KVM_CAP_ARM_PMU_V3);
231ae502508SAndrew Jones }
232ae502508SAndrew Jones 
233*bcb902a1SAndrew Jones int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
234a27382e2SEric Auger {
235a27382e2SEric Auger     KVMState *s = KVM_STATE(ms->accelerator);
236a27382e2SEric Auger     int ret;
237a27382e2SEric Auger 
238a27382e2SEric Auger     ret = kvm_check_extension(s, KVM_CAP_ARM_VM_IPA_SIZE);
239*bcb902a1SAndrew Jones     *fixed_ipa = ret <= 0;
240*bcb902a1SAndrew Jones 
241a27382e2SEric Auger     return ret > 0 ? ret : 40;
242a27382e2SEric Auger }
243a27382e2SEric Auger 
244fcf5ef2aSThomas Huth int kvm_arch_init(MachineState *ms, KVMState *s)
245fcf5ef2aSThomas Huth {
246fff9f555SEric Auger     int ret = 0;
247fcf5ef2aSThomas Huth     /* For ARM interrupt delivery is always asynchronous,
248fcf5ef2aSThomas Huth      * whether we are using an in-kernel VGIC or not.
249fcf5ef2aSThomas Huth      */
250fcf5ef2aSThomas Huth     kvm_async_interrupts_allowed = true;
251fcf5ef2aSThomas Huth 
2525d721b78SAlexander Graf     /*
2535d721b78SAlexander Graf      * PSCI wakes up secondary cores, so we always need to
2545d721b78SAlexander Graf      * have vCPUs waiting in kernel space
2555d721b78SAlexander Graf      */
2565d721b78SAlexander Graf     kvm_halt_in_kernel_allowed = true;
2575d721b78SAlexander Graf 
258fcf5ef2aSThomas Huth     cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
259fcf5ef2aSThomas Huth 
260fff9f555SEric Auger     if (ms->smp.cpus > 256 &&
261fff9f555SEric Auger         !kvm_check_extension(s, KVM_CAP_ARM_IRQ_LINE_LAYOUT_2)) {
262fff9f555SEric Auger         error_report("Using more than 256 vcpus requires a host kernel "
263fff9f555SEric Auger                      "with KVM_CAP_ARM_IRQ_LINE_LAYOUT_2");
264fff9f555SEric Auger         ret = -EINVAL;
265fff9f555SEric Auger     }
266fff9f555SEric Auger 
267694bcaa8SBeata Michalska     if (kvm_check_extension(s, KVM_CAP_ARM_NISV_TO_USER)) {
268694bcaa8SBeata Michalska         if (kvm_vm_enable_cap(s, KVM_CAP_ARM_NISV_TO_USER, 0)) {
269694bcaa8SBeata Michalska             error_report("Failed to enable KVM_CAP_ARM_NISV_TO_USER cap");
270694bcaa8SBeata Michalska         } else {
271694bcaa8SBeata Michalska             /* Set status for supporting the external dabt injection */
272694bcaa8SBeata Michalska             cap_has_inject_ext_dabt = kvm_check_extension(s,
273694bcaa8SBeata Michalska                                     KVM_CAP_ARM_INJECT_EXT_DABT);
274694bcaa8SBeata Michalska         }
275694bcaa8SBeata Michalska     }
276694bcaa8SBeata Michalska 
277fff9f555SEric Auger     return ret;
278fcf5ef2aSThomas Huth }
279fcf5ef2aSThomas Huth 
280fcf5ef2aSThomas Huth unsigned long kvm_arch_vcpu_id(CPUState *cpu)
281fcf5ef2aSThomas Huth {
282fcf5ef2aSThomas Huth     return cpu->cpu_index;
283fcf5ef2aSThomas Huth }
284fcf5ef2aSThomas Huth 
285fcf5ef2aSThomas Huth /* We track all the KVM devices which need their memory addresses
286fcf5ef2aSThomas Huth  * passing to the kernel in a list of these structures.
287fcf5ef2aSThomas Huth  * When board init is complete we run through the list and
288fcf5ef2aSThomas Huth  * tell the kernel the base addresses of the memory regions.
289fcf5ef2aSThomas Huth  * We use a MemoryListener to track mapping and unmapping of
290fcf5ef2aSThomas Huth  * the regions during board creation, so the board models don't
291fcf5ef2aSThomas Huth  * need to do anything special for the KVM case.
29219d1bd0bSEric Auger  *
29319d1bd0bSEric Auger  * Sometimes the address must be OR'ed with some other fields
29419d1bd0bSEric Auger  * (for example for KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION).
29519d1bd0bSEric Auger  * @kda_addr_ormask aims at storing the value of those fields.
296fcf5ef2aSThomas Huth  */
297fcf5ef2aSThomas Huth typedef struct KVMDevice {
298fcf5ef2aSThomas Huth     struct kvm_arm_device_addr kda;
299fcf5ef2aSThomas Huth     struct kvm_device_attr kdattr;
30019d1bd0bSEric Auger     uint64_t kda_addr_ormask;
301fcf5ef2aSThomas Huth     MemoryRegion *mr;
302fcf5ef2aSThomas Huth     QSLIST_ENTRY(KVMDevice) entries;
303fcf5ef2aSThomas Huth     int dev_fd;
304fcf5ef2aSThomas Huth } KVMDevice;
305fcf5ef2aSThomas Huth 
306b58deb34SPaolo Bonzini static QSLIST_HEAD(, KVMDevice) kvm_devices_head;
307fcf5ef2aSThomas Huth 
308fcf5ef2aSThomas Huth static void kvm_arm_devlistener_add(MemoryListener *listener,
309fcf5ef2aSThomas Huth                                     MemoryRegionSection *section)
310fcf5ef2aSThomas Huth {
311fcf5ef2aSThomas Huth     KVMDevice *kd;
312fcf5ef2aSThomas Huth 
313fcf5ef2aSThomas Huth     QSLIST_FOREACH(kd, &kvm_devices_head, entries) {
314fcf5ef2aSThomas Huth         if (section->mr == kd->mr) {
315fcf5ef2aSThomas Huth             kd->kda.addr = section->offset_within_address_space;
316fcf5ef2aSThomas Huth         }
317fcf5ef2aSThomas Huth     }
318fcf5ef2aSThomas Huth }
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth static void kvm_arm_devlistener_del(MemoryListener *listener,
321fcf5ef2aSThomas Huth                                     MemoryRegionSection *section)
322fcf5ef2aSThomas Huth {
323fcf5ef2aSThomas Huth     KVMDevice *kd;
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth     QSLIST_FOREACH(kd, &kvm_devices_head, entries) {
326fcf5ef2aSThomas Huth         if (section->mr == kd->mr) {
327fcf5ef2aSThomas Huth             kd->kda.addr = -1;
328fcf5ef2aSThomas Huth         }
329fcf5ef2aSThomas Huth     }
330fcf5ef2aSThomas Huth }
331fcf5ef2aSThomas Huth 
332fcf5ef2aSThomas Huth static MemoryListener devlistener = {
333fcf5ef2aSThomas Huth     .region_add = kvm_arm_devlistener_add,
334fcf5ef2aSThomas Huth     .region_del = kvm_arm_devlistener_del,
335fcf5ef2aSThomas Huth };
336fcf5ef2aSThomas Huth 
337fcf5ef2aSThomas Huth static void kvm_arm_set_device_addr(KVMDevice *kd)
338fcf5ef2aSThomas Huth {
339fcf5ef2aSThomas Huth     struct kvm_device_attr *attr = &kd->kdattr;
340fcf5ef2aSThomas Huth     int ret;
341fcf5ef2aSThomas Huth 
342fcf5ef2aSThomas Huth     /* If the device control API is available and we have a device fd on the
343fcf5ef2aSThomas Huth      * KVMDevice struct, let's use the newer API
344fcf5ef2aSThomas Huth      */
345fcf5ef2aSThomas Huth     if (kd->dev_fd >= 0) {
346fcf5ef2aSThomas Huth         uint64_t addr = kd->kda.addr;
34719d1bd0bSEric Auger 
34819d1bd0bSEric Auger         addr |= kd->kda_addr_ormask;
349fcf5ef2aSThomas Huth         attr->addr = (uintptr_t)&addr;
350fcf5ef2aSThomas Huth         ret = kvm_device_ioctl(kd->dev_fd, KVM_SET_DEVICE_ATTR, attr);
351fcf5ef2aSThomas Huth     } else {
352fcf5ef2aSThomas Huth         ret = kvm_vm_ioctl(kvm_state, KVM_ARM_SET_DEVICE_ADDR, &kd->kda);
353fcf5ef2aSThomas Huth     }
354fcf5ef2aSThomas Huth 
355fcf5ef2aSThomas Huth     if (ret < 0) {
356fcf5ef2aSThomas Huth         fprintf(stderr, "Failed to set device address: %s\n",
357fcf5ef2aSThomas Huth                 strerror(-ret));
358fcf5ef2aSThomas Huth         abort();
359fcf5ef2aSThomas Huth     }
360fcf5ef2aSThomas Huth }
361fcf5ef2aSThomas Huth 
362fcf5ef2aSThomas Huth static void kvm_arm_machine_init_done(Notifier *notifier, void *data)
363fcf5ef2aSThomas Huth {
364fcf5ef2aSThomas Huth     KVMDevice *kd, *tkd;
365fcf5ef2aSThomas Huth 
366fcf5ef2aSThomas Huth     QSLIST_FOREACH_SAFE(kd, &kvm_devices_head, entries, tkd) {
367fcf5ef2aSThomas Huth         if (kd->kda.addr != -1) {
368fcf5ef2aSThomas Huth             kvm_arm_set_device_addr(kd);
369fcf5ef2aSThomas Huth         }
370fcf5ef2aSThomas Huth         memory_region_unref(kd->mr);
3715ff9aaabSZheng Xiang         QSLIST_REMOVE_HEAD(&kvm_devices_head, entries);
372fcf5ef2aSThomas Huth         g_free(kd);
373fcf5ef2aSThomas Huth     }
3740bbe4354SPeter Xu     memory_listener_unregister(&devlistener);
375fcf5ef2aSThomas Huth }
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth static Notifier notify = {
378fcf5ef2aSThomas Huth     .notify = kvm_arm_machine_init_done,
379fcf5ef2aSThomas Huth };
380fcf5ef2aSThomas Huth 
381fcf5ef2aSThomas Huth void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
38219d1bd0bSEric Auger                              uint64_t attr, int dev_fd, uint64_t addr_ormask)
383fcf5ef2aSThomas Huth {
384fcf5ef2aSThomas Huth     KVMDevice *kd;
385fcf5ef2aSThomas Huth 
386fcf5ef2aSThomas Huth     if (!kvm_irqchip_in_kernel()) {
387fcf5ef2aSThomas Huth         return;
388fcf5ef2aSThomas Huth     }
389fcf5ef2aSThomas Huth 
390fcf5ef2aSThomas Huth     if (QSLIST_EMPTY(&kvm_devices_head)) {
391fcf5ef2aSThomas Huth         memory_listener_register(&devlistener, &address_space_memory);
392fcf5ef2aSThomas Huth         qemu_add_machine_init_done_notifier(&notify);
393fcf5ef2aSThomas Huth     }
394fcf5ef2aSThomas Huth     kd = g_new0(KVMDevice, 1);
395fcf5ef2aSThomas Huth     kd->mr = mr;
396fcf5ef2aSThomas Huth     kd->kda.id = devid;
397fcf5ef2aSThomas Huth     kd->kda.addr = -1;
398fcf5ef2aSThomas Huth     kd->kdattr.flags = 0;
399fcf5ef2aSThomas Huth     kd->kdattr.group = group;
400fcf5ef2aSThomas Huth     kd->kdattr.attr = attr;
401fcf5ef2aSThomas Huth     kd->dev_fd = dev_fd;
40219d1bd0bSEric Auger     kd->kda_addr_ormask = addr_ormask;
403fcf5ef2aSThomas Huth     QSLIST_INSERT_HEAD(&kvm_devices_head, kd, entries);
404fcf5ef2aSThomas Huth     memory_region_ref(kd->mr);
405fcf5ef2aSThomas Huth }
406fcf5ef2aSThomas Huth 
407fcf5ef2aSThomas Huth static int compare_u64(const void *a, const void *b)
408fcf5ef2aSThomas Huth {
409fcf5ef2aSThomas Huth     if (*(uint64_t *)a > *(uint64_t *)b) {
410fcf5ef2aSThomas Huth         return 1;
411fcf5ef2aSThomas Huth     }
412fcf5ef2aSThomas Huth     if (*(uint64_t *)a < *(uint64_t *)b) {
413fcf5ef2aSThomas Huth         return -1;
414fcf5ef2aSThomas Huth     }
415fcf5ef2aSThomas Huth     return 0;
416fcf5ef2aSThomas Huth }
417fcf5ef2aSThomas Huth 
418e5ac4200SAndrew Jones /*
419e5ac4200SAndrew Jones  * cpreg_values are sorted in ascending order by KVM register ID
420e5ac4200SAndrew Jones  * (see kvm_arm_init_cpreg_list). This allows us to cheaply find
421e5ac4200SAndrew Jones  * the storage for a KVM register by ID with a binary search.
422e5ac4200SAndrew Jones  */
423e5ac4200SAndrew Jones static uint64_t *kvm_arm_get_cpreg_ptr(ARMCPU *cpu, uint64_t regidx)
424e5ac4200SAndrew Jones {
425e5ac4200SAndrew Jones     uint64_t *res;
426e5ac4200SAndrew Jones 
427e5ac4200SAndrew Jones     res = bsearch(&regidx, cpu->cpreg_indexes, cpu->cpreg_array_len,
428e5ac4200SAndrew Jones                   sizeof(uint64_t), compare_u64);
429e5ac4200SAndrew Jones     assert(res);
430e5ac4200SAndrew Jones 
431e5ac4200SAndrew Jones     return &cpu->cpreg_values[res - cpu->cpreg_indexes];
432e5ac4200SAndrew Jones }
433e5ac4200SAndrew Jones 
434c8a44709SDongjiu Geng /* Initialize the ARMCPU cpreg list according to the kernel's
435fcf5ef2aSThomas Huth  * definition of what CPU registers it knows about (and throw away
436fcf5ef2aSThomas Huth  * the previous TCG-created cpreg list).
437fcf5ef2aSThomas Huth  */
438fcf5ef2aSThomas Huth int kvm_arm_init_cpreg_list(ARMCPU *cpu)
439fcf5ef2aSThomas Huth {
440fcf5ef2aSThomas Huth     struct kvm_reg_list rl;
441fcf5ef2aSThomas Huth     struct kvm_reg_list *rlp;
442fcf5ef2aSThomas Huth     int i, ret, arraylen;
443fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
444fcf5ef2aSThomas Huth 
445fcf5ef2aSThomas Huth     rl.n = 0;
446fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl);
447fcf5ef2aSThomas Huth     if (ret != -E2BIG) {
448fcf5ef2aSThomas Huth         return ret;
449fcf5ef2aSThomas Huth     }
450fcf5ef2aSThomas Huth     rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t));
451fcf5ef2aSThomas Huth     rlp->n = rl.n;
452fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp);
453fcf5ef2aSThomas Huth     if (ret) {
454fcf5ef2aSThomas Huth         goto out;
455fcf5ef2aSThomas Huth     }
456fcf5ef2aSThomas Huth     /* Sort the list we get back from the kernel, since cpreg_tuples
457fcf5ef2aSThomas Huth      * must be in strictly ascending order.
458fcf5ef2aSThomas Huth      */
459fcf5ef2aSThomas Huth     qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64);
460fcf5ef2aSThomas Huth 
461fcf5ef2aSThomas Huth     for (i = 0, arraylen = 0; i < rlp->n; i++) {
462fcf5ef2aSThomas Huth         if (!kvm_arm_reg_syncs_via_cpreg_list(rlp->reg[i])) {
463fcf5ef2aSThomas Huth             continue;
464fcf5ef2aSThomas Huth         }
465fcf5ef2aSThomas Huth         switch (rlp->reg[i] & KVM_REG_SIZE_MASK) {
466fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U32:
467fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U64:
468fcf5ef2aSThomas Huth             break;
469fcf5ef2aSThomas Huth         default:
470fcf5ef2aSThomas Huth             fprintf(stderr, "Can't handle size of register in kernel list\n");
471fcf5ef2aSThomas Huth             ret = -EINVAL;
472fcf5ef2aSThomas Huth             goto out;
473fcf5ef2aSThomas Huth         }
474fcf5ef2aSThomas Huth 
475fcf5ef2aSThomas Huth         arraylen++;
476fcf5ef2aSThomas Huth     }
477fcf5ef2aSThomas Huth 
478fcf5ef2aSThomas Huth     cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen);
479fcf5ef2aSThomas Huth     cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen);
480fcf5ef2aSThomas Huth     cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes,
481fcf5ef2aSThomas Huth                                          arraylen);
482fcf5ef2aSThomas Huth     cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values,
483fcf5ef2aSThomas Huth                                         arraylen);
484fcf5ef2aSThomas Huth     cpu->cpreg_array_len = arraylen;
485fcf5ef2aSThomas Huth     cpu->cpreg_vmstate_array_len = arraylen;
486fcf5ef2aSThomas Huth 
487fcf5ef2aSThomas Huth     for (i = 0, arraylen = 0; i < rlp->n; i++) {
488fcf5ef2aSThomas Huth         uint64_t regidx = rlp->reg[i];
489fcf5ef2aSThomas Huth         if (!kvm_arm_reg_syncs_via_cpreg_list(regidx)) {
490fcf5ef2aSThomas Huth             continue;
491fcf5ef2aSThomas Huth         }
492fcf5ef2aSThomas Huth         cpu->cpreg_indexes[arraylen] = regidx;
493fcf5ef2aSThomas Huth         arraylen++;
494fcf5ef2aSThomas Huth     }
495fcf5ef2aSThomas Huth     assert(cpu->cpreg_array_len == arraylen);
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth     if (!write_kvmstate_to_list(cpu)) {
498fcf5ef2aSThomas Huth         /* Shouldn't happen unless kernel is inconsistent about
499fcf5ef2aSThomas Huth          * what registers exist.
500fcf5ef2aSThomas Huth          */
501fcf5ef2aSThomas Huth         fprintf(stderr, "Initial read of kernel register state failed\n");
502fcf5ef2aSThomas Huth         ret = -EINVAL;
503fcf5ef2aSThomas Huth         goto out;
504fcf5ef2aSThomas Huth     }
505fcf5ef2aSThomas Huth 
506fcf5ef2aSThomas Huth out:
507fcf5ef2aSThomas Huth     g_free(rlp);
508fcf5ef2aSThomas Huth     return ret;
509fcf5ef2aSThomas Huth }
510fcf5ef2aSThomas Huth 
511fcf5ef2aSThomas Huth bool write_kvmstate_to_list(ARMCPU *cpu)
512fcf5ef2aSThomas Huth {
513fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
514fcf5ef2aSThomas Huth     int i;
515fcf5ef2aSThomas Huth     bool ok = true;
516fcf5ef2aSThomas Huth 
517fcf5ef2aSThomas Huth     for (i = 0; i < cpu->cpreg_array_len; i++) {
518fcf5ef2aSThomas Huth         struct kvm_one_reg r;
519fcf5ef2aSThomas Huth         uint64_t regidx = cpu->cpreg_indexes[i];
520fcf5ef2aSThomas Huth         uint32_t v32;
521fcf5ef2aSThomas Huth         int ret;
522fcf5ef2aSThomas Huth 
523fcf5ef2aSThomas Huth         r.id = regidx;
524fcf5ef2aSThomas Huth 
525fcf5ef2aSThomas Huth         switch (regidx & KVM_REG_SIZE_MASK) {
526fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U32:
527fcf5ef2aSThomas Huth             r.addr = (uintptr_t)&v32;
528fcf5ef2aSThomas Huth             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
529fcf5ef2aSThomas Huth             if (!ret) {
530fcf5ef2aSThomas Huth                 cpu->cpreg_values[i] = v32;
531fcf5ef2aSThomas Huth             }
532fcf5ef2aSThomas Huth             break;
533fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U64:
534fcf5ef2aSThomas Huth             r.addr = (uintptr_t)(cpu->cpreg_values + i);
535fcf5ef2aSThomas Huth             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
536fcf5ef2aSThomas Huth             break;
537fcf5ef2aSThomas Huth         default:
538fcf5ef2aSThomas Huth             abort();
539fcf5ef2aSThomas Huth         }
540fcf5ef2aSThomas Huth         if (ret) {
541fcf5ef2aSThomas Huth             ok = false;
542fcf5ef2aSThomas Huth         }
543fcf5ef2aSThomas Huth     }
544fcf5ef2aSThomas Huth     return ok;
545fcf5ef2aSThomas Huth }
546fcf5ef2aSThomas Huth 
547fcf5ef2aSThomas Huth bool write_list_to_kvmstate(ARMCPU *cpu, int level)
548fcf5ef2aSThomas Huth {
549fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
550fcf5ef2aSThomas Huth     int i;
551fcf5ef2aSThomas Huth     bool ok = true;
552fcf5ef2aSThomas Huth 
553fcf5ef2aSThomas Huth     for (i = 0; i < cpu->cpreg_array_len; i++) {
554fcf5ef2aSThomas Huth         struct kvm_one_reg r;
555fcf5ef2aSThomas Huth         uint64_t regidx = cpu->cpreg_indexes[i];
556fcf5ef2aSThomas Huth         uint32_t v32;
557fcf5ef2aSThomas Huth         int ret;
558fcf5ef2aSThomas Huth 
559fcf5ef2aSThomas Huth         if (kvm_arm_cpreg_level(regidx) > level) {
560fcf5ef2aSThomas Huth             continue;
561fcf5ef2aSThomas Huth         }
562fcf5ef2aSThomas Huth 
563fcf5ef2aSThomas Huth         r.id = regidx;
564fcf5ef2aSThomas Huth         switch (regidx & KVM_REG_SIZE_MASK) {
565fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U32:
566fcf5ef2aSThomas Huth             v32 = cpu->cpreg_values[i];
567fcf5ef2aSThomas Huth             r.addr = (uintptr_t)&v32;
568fcf5ef2aSThomas Huth             break;
569fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U64:
570fcf5ef2aSThomas Huth             r.addr = (uintptr_t)(cpu->cpreg_values + i);
571fcf5ef2aSThomas Huth             break;
572fcf5ef2aSThomas Huth         default:
573fcf5ef2aSThomas Huth             abort();
574fcf5ef2aSThomas Huth         }
575fcf5ef2aSThomas Huth         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
576fcf5ef2aSThomas Huth         if (ret) {
577fcf5ef2aSThomas Huth             /* We might fail for "unknown register" and also for
578fcf5ef2aSThomas Huth              * "you tried to set a register which is constant with
579fcf5ef2aSThomas Huth              * a different value from what it actually contains".
580fcf5ef2aSThomas Huth              */
581fcf5ef2aSThomas Huth             ok = false;
582fcf5ef2aSThomas Huth         }
583fcf5ef2aSThomas Huth     }
584fcf5ef2aSThomas Huth     return ok;
585fcf5ef2aSThomas Huth }
586fcf5ef2aSThomas Huth 
587e5ac4200SAndrew Jones void kvm_arm_cpu_pre_save(ARMCPU *cpu)
588e5ac4200SAndrew Jones {
589e5ac4200SAndrew Jones     /* KVM virtual time adjustment */
590e5ac4200SAndrew Jones     if (cpu->kvm_vtime_dirty) {
591e5ac4200SAndrew Jones         *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT) = cpu->kvm_vtime;
592e5ac4200SAndrew Jones     }
593e5ac4200SAndrew Jones }
594e5ac4200SAndrew Jones 
595e5ac4200SAndrew Jones void kvm_arm_cpu_post_load(ARMCPU *cpu)
596e5ac4200SAndrew Jones {
597e5ac4200SAndrew Jones     /* KVM virtual time adjustment */
598e5ac4200SAndrew Jones     if (cpu->kvm_adjvtime) {
599e5ac4200SAndrew Jones         cpu->kvm_vtime = *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT);
600e5ac4200SAndrew Jones         cpu->kvm_vtime_dirty = true;
601e5ac4200SAndrew Jones     }
602e5ac4200SAndrew Jones }
603e5ac4200SAndrew Jones 
604fcf5ef2aSThomas Huth void kvm_arm_reset_vcpu(ARMCPU *cpu)
605fcf5ef2aSThomas Huth {
606fcf5ef2aSThomas Huth     int ret;
607fcf5ef2aSThomas Huth 
608fcf5ef2aSThomas Huth     /* Re-init VCPU so that all registers are set to
609fcf5ef2aSThomas Huth      * their respective reset values.
610fcf5ef2aSThomas Huth      */
611fcf5ef2aSThomas Huth     ret = kvm_arm_vcpu_init(CPU(cpu));
612fcf5ef2aSThomas Huth     if (ret < 0) {
613fcf5ef2aSThomas Huth         fprintf(stderr, "kvm_arm_vcpu_init failed: %s\n", strerror(-ret));
614fcf5ef2aSThomas Huth         abort();
615fcf5ef2aSThomas Huth     }
616fcf5ef2aSThomas Huth     if (!write_kvmstate_to_list(cpu)) {
617fcf5ef2aSThomas Huth         fprintf(stderr, "write_kvmstate_to_list failed\n");
618fcf5ef2aSThomas Huth         abort();
619fcf5ef2aSThomas Huth     }
620b698e4eeSPeter Maydell     /*
621b698e4eeSPeter Maydell      * Sync the reset values also into the CPUState. This is necessary
622b698e4eeSPeter Maydell      * because the next thing we do will be a kvm_arch_put_registers()
623b698e4eeSPeter Maydell      * which will update the list values from the CPUState before copying
624b698e4eeSPeter Maydell      * the list values back to KVM. It's OK to ignore failure returns here
625b698e4eeSPeter Maydell      * for the same reason we do so in kvm_arch_get_registers().
626b698e4eeSPeter Maydell      */
627b698e4eeSPeter Maydell     write_list_to_cpustate(cpu);
628fcf5ef2aSThomas Huth }
629fcf5ef2aSThomas Huth 
630fcf5ef2aSThomas Huth /*
631fcf5ef2aSThomas Huth  * Update KVM's MP_STATE based on what QEMU thinks it is
632fcf5ef2aSThomas Huth  */
633fcf5ef2aSThomas Huth int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu)
634fcf5ef2aSThomas Huth {
635fcf5ef2aSThomas Huth     if (cap_has_mp_state) {
636fcf5ef2aSThomas Huth         struct kvm_mp_state mp_state = {
637062ba099SAlex Bennée             .mp_state = (cpu->power_state == PSCI_OFF) ?
638062ba099SAlex Bennée             KVM_MP_STATE_STOPPED : KVM_MP_STATE_RUNNABLE
639fcf5ef2aSThomas Huth         };
640fcf5ef2aSThomas Huth         int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
641fcf5ef2aSThomas Huth         if (ret) {
642fcf5ef2aSThomas Huth             fprintf(stderr, "%s: failed to set MP_STATE %d/%s\n",
643fcf5ef2aSThomas Huth                     __func__, ret, strerror(-ret));
644fcf5ef2aSThomas Huth             return -1;
645fcf5ef2aSThomas Huth         }
646fcf5ef2aSThomas Huth     }
647fcf5ef2aSThomas Huth 
648fcf5ef2aSThomas Huth     return 0;
649fcf5ef2aSThomas Huth }
650fcf5ef2aSThomas Huth 
651fcf5ef2aSThomas Huth /*
652fcf5ef2aSThomas Huth  * Sync the KVM MP_STATE into QEMU
653fcf5ef2aSThomas Huth  */
654fcf5ef2aSThomas Huth int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu)
655fcf5ef2aSThomas Huth {
656fcf5ef2aSThomas Huth     if (cap_has_mp_state) {
657fcf5ef2aSThomas Huth         struct kvm_mp_state mp_state;
658fcf5ef2aSThomas Huth         int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MP_STATE, &mp_state);
659fcf5ef2aSThomas Huth         if (ret) {
660fcf5ef2aSThomas Huth             fprintf(stderr, "%s: failed to get MP_STATE %d/%s\n",
661fcf5ef2aSThomas Huth                     __func__, ret, strerror(-ret));
662fcf5ef2aSThomas Huth             abort();
663fcf5ef2aSThomas Huth         }
664062ba099SAlex Bennée         cpu->power_state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ?
665062ba099SAlex Bennée             PSCI_OFF : PSCI_ON;
666fcf5ef2aSThomas Huth     }
667fcf5ef2aSThomas Huth 
668fcf5ef2aSThomas Huth     return 0;
669fcf5ef2aSThomas Huth }
670fcf5ef2aSThomas Huth 
671e5ac4200SAndrew Jones void kvm_arm_get_virtual_time(CPUState *cs)
672e5ac4200SAndrew Jones {
673e5ac4200SAndrew Jones     ARMCPU *cpu = ARM_CPU(cs);
674e5ac4200SAndrew Jones     struct kvm_one_reg reg = {
675e5ac4200SAndrew Jones         .id = KVM_REG_ARM_TIMER_CNT,
676e5ac4200SAndrew Jones         .addr = (uintptr_t)&cpu->kvm_vtime,
677e5ac4200SAndrew Jones     };
678e5ac4200SAndrew Jones     int ret;
679e5ac4200SAndrew Jones 
680e5ac4200SAndrew Jones     if (cpu->kvm_vtime_dirty) {
681e5ac4200SAndrew Jones         return;
682e5ac4200SAndrew Jones     }
683e5ac4200SAndrew Jones 
684e5ac4200SAndrew Jones     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
685e5ac4200SAndrew Jones     if (ret) {
686e5ac4200SAndrew Jones         error_report("Failed to get KVM_REG_ARM_TIMER_CNT");
687e5ac4200SAndrew Jones         abort();
688e5ac4200SAndrew Jones     }
689e5ac4200SAndrew Jones 
690e5ac4200SAndrew Jones     cpu->kvm_vtime_dirty = true;
691e5ac4200SAndrew Jones }
692e5ac4200SAndrew Jones 
693e5ac4200SAndrew Jones void kvm_arm_put_virtual_time(CPUState *cs)
694e5ac4200SAndrew Jones {
695e5ac4200SAndrew Jones     ARMCPU *cpu = ARM_CPU(cs);
696e5ac4200SAndrew Jones     struct kvm_one_reg reg = {
697e5ac4200SAndrew Jones         .id = KVM_REG_ARM_TIMER_CNT,
698e5ac4200SAndrew Jones         .addr = (uintptr_t)&cpu->kvm_vtime,
699e5ac4200SAndrew Jones     };
700e5ac4200SAndrew Jones     int ret;
701e5ac4200SAndrew Jones 
702e5ac4200SAndrew Jones     if (!cpu->kvm_vtime_dirty) {
703e5ac4200SAndrew Jones         return;
704e5ac4200SAndrew Jones     }
705e5ac4200SAndrew Jones 
706e5ac4200SAndrew Jones     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
707e5ac4200SAndrew Jones     if (ret) {
708e5ac4200SAndrew Jones         error_report("Failed to set KVM_REG_ARM_TIMER_CNT");
709e5ac4200SAndrew Jones         abort();
710e5ac4200SAndrew Jones     }
711e5ac4200SAndrew Jones 
712e5ac4200SAndrew Jones     cpu->kvm_vtime_dirty = false;
713e5ac4200SAndrew Jones }
714e5ac4200SAndrew Jones 
715202ccb6bSDongjiu Geng int kvm_put_vcpu_events(ARMCPU *cpu)
716202ccb6bSDongjiu Geng {
717202ccb6bSDongjiu Geng     CPUARMState *env = &cpu->env;
718202ccb6bSDongjiu Geng     struct kvm_vcpu_events events;
719202ccb6bSDongjiu Geng     int ret;
720202ccb6bSDongjiu Geng 
721202ccb6bSDongjiu Geng     if (!kvm_has_vcpu_events()) {
722202ccb6bSDongjiu Geng         return 0;
723202ccb6bSDongjiu Geng     }
724202ccb6bSDongjiu Geng 
725202ccb6bSDongjiu Geng     memset(&events, 0, sizeof(events));
726202ccb6bSDongjiu Geng     events.exception.serror_pending = env->serror.pending;
727202ccb6bSDongjiu Geng 
728202ccb6bSDongjiu Geng     /* Inject SError to guest with specified syndrome if host kernel
729202ccb6bSDongjiu Geng      * supports it, otherwise inject SError without syndrome.
730202ccb6bSDongjiu Geng      */
731202ccb6bSDongjiu Geng     if (cap_has_inject_serror_esr) {
732202ccb6bSDongjiu Geng         events.exception.serror_has_esr = env->serror.has_esr;
733202ccb6bSDongjiu Geng         events.exception.serror_esr = env->serror.esr;
734202ccb6bSDongjiu Geng     }
735202ccb6bSDongjiu Geng 
736202ccb6bSDongjiu Geng     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);
737202ccb6bSDongjiu Geng     if (ret) {
738202ccb6bSDongjiu Geng         error_report("failed to put vcpu events");
739202ccb6bSDongjiu Geng     }
740202ccb6bSDongjiu Geng 
741202ccb6bSDongjiu Geng     return ret;
742202ccb6bSDongjiu Geng }
743202ccb6bSDongjiu Geng 
744202ccb6bSDongjiu Geng int kvm_get_vcpu_events(ARMCPU *cpu)
745202ccb6bSDongjiu Geng {
746202ccb6bSDongjiu Geng     CPUARMState *env = &cpu->env;
747202ccb6bSDongjiu Geng     struct kvm_vcpu_events events;
748202ccb6bSDongjiu Geng     int ret;
749202ccb6bSDongjiu Geng 
750202ccb6bSDongjiu Geng     if (!kvm_has_vcpu_events()) {
751202ccb6bSDongjiu Geng         return 0;
752202ccb6bSDongjiu Geng     }
753202ccb6bSDongjiu Geng 
754202ccb6bSDongjiu Geng     memset(&events, 0, sizeof(events));
755202ccb6bSDongjiu Geng     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events);
756202ccb6bSDongjiu Geng     if (ret) {
757202ccb6bSDongjiu Geng         error_report("failed to get vcpu events");
758202ccb6bSDongjiu Geng         return ret;
759202ccb6bSDongjiu Geng     }
760202ccb6bSDongjiu Geng 
761202ccb6bSDongjiu Geng     env->serror.pending = events.exception.serror_pending;
762202ccb6bSDongjiu Geng     env->serror.has_esr = events.exception.serror_has_esr;
763202ccb6bSDongjiu Geng     env->serror.esr = events.exception.serror_esr;
764202ccb6bSDongjiu Geng 
765202ccb6bSDongjiu Geng     return 0;
766202ccb6bSDongjiu Geng }
767202ccb6bSDongjiu Geng 
768fcf5ef2aSThomas Huth void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
769fcf5ef2aSThomas Huth {
7701711bfa5SBeata Michalska     ARMCPU *cpu = ARM_CPU(cs);
7711711bfa5SBeata Michalska     CPUARMState *env = &cpu->env;
7721711bfa5SBeata Michalska 
7731711bfa5SBeata Michalska     if (unlikely(env->ext_dabt_raised)) {
7741711bfa5SBeata Michalska         /*
7751711bfa5SBeata Michalska          * Verifying that the ext DABT has been properly injected,
7761711bfa5SBeata Michalska          * otherwise risking indefinitely re-running the faulting instruction
7771711bfa5SBeata Michalska          * Covering a very narrow case for kernels 5.5..5.5.4
7781711bfa5SBeata Michalska          * when injected abort was misconfigured to be
7791711bfa5SBeata Michalska          * an IMPLEMENTATION DEFINED exception (for 32-bit EL1)
7801711bfa5SBeata Michalska          */
7811711bfa5SBeata Michalska         if (!arm_feature(env, ARM_FEATURE_AARCH64) &&
7821711bfa5SBeata Michalska             unlikely(!kvm_arm_verify_ext_dabt_pending(cs))) {
7831711bfa5SBeata Michalska 
7841711bfa5SBeata Michalska             error_report("Data abort exception with no valid ISS generated by "
7851711bfa5SBeata Michalska                    "guest memory access. KVM unable to emulate faulting "
7861711bfa5SBeata Michalska                    "instruction. Failed to inject an external data abort "
7871711bfa5SBeata Michalska                    "into the guest.");
7881711bfa5SBeata Michalska             abort();
7891711bfa5SBeata Michalska        }
7901711bfa5SBeata Michalska        /* Clear the status */
7911711bfa5SBeata Michalska        env->ext_dabt_raised = 0;
7921711bfa5SBeata Michalska     }
793fcf5ef2aSThomas Huth }
794fcf5ef2aSThomas Huth 
795fcf5ef2aSThomas Huth MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
796fcf5ef2aSThomas Huth {
7975d721b78SAlexander Graf     ARMCPU *cpu;
7985d721b78SAlexander Graf     uint32_t switched_level;
7995d721b78SAlexander Graf 
8005d721b78SAlexander Graf     if (kvm_irqchip_in_kernel()) {
8015d721b78SAlexander Graf         /*
8025d721b78SAlexander Graf          * We only need to sync timer states with user-space interrupt
8035d721b78SAlexander Graf          * controllers, so return early and save cycles if we don't.
8045d721b78SAlexander Graf          */
8055d721b78SAlexander Graf         return MEMTXATTRS_UNSPECIFIED;
8065d721b78SAlexander Graf     }
8075d721b78SAlexander Graf 
8085d721b78SAlexander Graf     cpu = ARM_CPU(cs);
8095d721b78SAlexander Graf 
8105d721b78SAlexander Graf     /* Synchronize our shadowed in-kernel device irq lines with the kvm ones */
8115d721b78SAlexander Graf     if (run->s.regs.device_irq_level != cpu->device_irq_level) {
8125d721b78SAlexander Graf         switched_level = cpu->device_irq_level ^ run->s.regs.device_irq_level;
8135d721b78SAlexander Graf 
8145d721b78SAlexander Graf         qemu_mutex_lock_iothread();
8155d721b78SAlexander Graf 
8165d721b78SAlexander Graf         if (switched_level & KVM_ARM_DEV_EL1_VTIMER) {
8175d721b78SAlexander Graf             qemu_set_irq(cpu->gt_timer_outputs[GTIMER_VIRT],
8185d721b78SAlexander Graf                          !!(run->s.regs.device_irq_level &
8195d721b78SAlexander Graf                             KVM_ARM_DEV_EL1_VTIMER));
8205d721b78SAlexander Graf             switched_level &= ~KVM_ARM_DEV_EL1_VTIMER;
8215d721b78SAlexander Graf         }
8225d721b78SAlexander Graf 
8235d721b78SAlexander Graf         if (switched_level & KVM_ARM_DEV_EL1_PTIMER) {
8245d721b78SAlexander Graf             qemu_set_irq(cpu->gt_timer_outputs[GTIMER_PHYS],
8255d721b78SAlexander Graf                          !!(run->s.regs.device_irq_level &
8265d721b78SAlexander Graf                             KVM_ARM_DEV_EL1_PTIMER));
8275d721b78SAlexander Graf             switched_level &= ~KVM_ARM_DEV_EL1_PTIMER;
8285d721b78SAlexander Graf         }
8295d721b78SAlexander Graf 
830b1659527SAndrew Jones         if (switched_level & KVM_ARM_DEV_PMU) {
831b1659527SAndrew Jones             qemu_set_irq(cpu->pmu_interrupt,
832b1659527SAndrew Jones                          !!(run->s.regs.device_irq_level & KVM_ARM_DEV_PMU));
833b1659527SAndrew Jones             switched_level &= ~KVM_ARM_DEV_PMU;
834b1659527SAndrew Jones         }
8355d721b78SAlexander Graf 
8365d721b78SAlexander Graf         if (switched_level) {
8375d721b78SAlexander Graf             qemu_log_mask(LOG_UNIMP, "%s: unhandled in-kernel device IRQ %x\n",
8385d721b78SAlexander Graf                           __func__, switched_level);
8395d721b78SAlexander Graf         }
8405d721b78SAlexander Graf 
8415d721b78SAlexander Graf         /* We also mark unknown levels as processed to not waste cycles */
8425d721b78SAlexander Graf         cpu->device_irq_level = run->s.regs.device_irq_level;
8435d721b78SAlexander Graf         qemu_mutex_unlock_iothread();
8445d721b78SAlexander Graf     }
8455d721b78SAlexander Graf 
846fcf5ef2aSThomas Huth     return MEMTXATTRS_UNSPECIFIED;
847fcf5ef2aSThomas Huth }
848fcf5ef2aSThomas Huth 
849538f0497SPhilippe Mathieu-Daudé void kvm_arm_vm_state_change(void *opaque, bool running, RunState state)
850e5ac4200SAndrew Jones {
851e5ac4200SAndrew Jones     CPUState *cs = opaque;
852e5ac4200SAndrew Jones     ARMCPU *cpu = ARM_CPU(cs);
853e5ac4200SAndrew Jones 
854e5ac4200SAndrew Jones     if (running) {
855e5ac4200SAndrew Jones         if (cpu->kvm_adjvtime) {
856e5ac4200SAndrew Jones             kvm_arm_put_virtual_time(cs);
857e5ac4200SAndrew Jones         }
858e5ac4200SAndrew Jones     } else {
859e5ac4200SAndrew Jones         if (cpu->kvm_adjvtime) {
860e5ac4200SAndrew Jones             kvm_arm_get_virtual_time(cs);
861e5ac4200SAndrew Jones         }
862e5ac4200SAndrew Jones     }
863e5ac4200SAndrew Jones }
864fcf5ef2aSThomas Huth 
865694bcaa8SBeata Michalska /**
866694bcaa8SBeata Michalska  * kvm_arm_handle_dabt_nisv:
867694bcaa8SBeata Michalska  * @cs: CPUState
868694bcaa8SBeata Michalska  * @esr_iss: ISS encoding (limited) for the exception from Data Abort
869694bcaa8SBeata Michalska  *           ISV bit set to '0b0' -> no valid instruction syndrome
870694bcaa8SBeata Michalska  * @fault_ipa: faulting address for the synchronous data abort
871694bcaa8SBeata Michalska  *
872694bcaa8SBeata Michalska  * Returns: 0 if the exception has been handled, < 0 otherwise
873694bcaa8SBeata Michalska  */
874694bcaa8SBeata Michalska static int kvm_arm_handle_dabt_nisv(CPUState *cs, uint64_t esr_iss,
875694bcaa8SBeata Michalska                                     uint64_t fault_ipa)
876694bcaa8SBeata Michalska {
8771711bfa5SBeata Michalska     ARMCPU *cpu = ARM_CPU(cs);
8781711bfa5SBeata Michalska     CPUARMState *env = &cpu->env;
879694bcaa8SBeata Michalska     /*
880694bcaa8SBeata Michalska      * Request KVM to inject the external data abort into the guest
881694bcaa8SBeata Michalska      */
882694bcaa8SBeata Michalska     if (cap_has_inject_ext_dabt) {
883694bcaa8SBeata Michalska         struct kvm_vcpu_events events = { };
884694bcaa8SBeata Michalska         /*
885694bcaa8SBeata Michalska          * The external data abort event will be handled immediately by KVM
886694bcaa8SBeata Michalska          * using the address fault that triggered the exit on given VCPU.
887694bcaa8SBeata Michalska          * Requesting injection of the external data abort does not rely
888694bcaa8SBeata Michalska          * on any other VCPU state. Therefore, in this particular case, the VCPU
889694bcaa8SBeata Michalska          * synchronization can be exceptionally skipped.
890694bcaa8SBeata Michalska          */
891694bcaa8SBeata Michalska         events.exception.ext_dabt_pending = 1;
892694bcaa8SBeata Michalska         /* KVM_CAP_ARM_INJECT_EXT_DABT implies KVM_CAP_VCPU_EVENTS */
8931711bfa5SBeata Michalska         if (!kvm_vcpu_ioctl(cs, KVM_SET_VCPU_EVENTS, &events)) {
8941711bfa5SBeata Michalska             env->ext_dabt_raised = 1;
8951711bfa5SBeata Michalska             return 0;
8961711bfa5SBeata Michalska         }
897694bcaa8SBeata Michalska     } else {
898694bcaa8SBeata Michalska         error_report("Data abort exception triggered by guest memory access "
899694bcaa8SBeata Michalska                      "at physical address: 0x"  TARGET_FMT_lx,
900694bcaa8SBeata Michalska                      (target_ulong)fault_ipa);
901694bcaa8SBeata Michalska         error_printf("KVM unable to emulate faulting instruction.\n");
902694bcaa8SBeata Michalska     }
903694bcaa8SBeata Michalska     return -1;
904694bcaa8SBeata Michalska }
905694bcaa8SBeata Michalska 
906fcf5ef2aSThomas Huth int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
907fcf5ef2aSThomas Huth {
908fcf5ef2aSThomas Huth     int ret = 0;
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth     switch (run->exit_reason) {
911fcf5ef2aSThomas Huth     case KVM_EXIT_DEBUG:
912fcf5ef2aSThomas Huth         if (kvm_arm_handle_debug(cs, &run->debug.arch)) {
913fcf5ef2aSThomas Huth             ret = EXCP_DEBUG;
914fcf5ef2aSThomas Huth         } /* otherwise return to guest */
915fcf5ef2aSThomas Huth         break;
916694bcaa8SBeata Michalska     case KVM_EXIT_ARM_NISV:
917694bcaa8SBeata Michalska         /* External DABT with no valid iss to decode */
918694bcaa8SBeata Michalska         ret = kvm_arm_handle_dabt_nisv(cs, run->arm_nisv.esr_iss,
919694bcaa8SBeata Michalska                                        run->arm_nisv.fault_ipa);
920694bcaa8SBeata Michalska         break;
921fcf5ef2aSThomas Huth     default:
922fcf5ef2aSThomas Huth         qemu_log_mask(LOG_UNIMP, "%s: un-handled exit reason %d\n",
923fcf5ef2aSThomas Huth                       __func__, run->exit_reason);
924fcf5ef2aSThomas Huth         break;
925fcf5ef2aSThomas Huth     }
926fcf5ef2aSThomas Huth     return ret;
927fcf5ef2aSThomas Huth }
928fcf5ef2aSThomas Huth 
929fcf5ef2aSThomas Huth bool kvm_arch_stop_on_emulation_error(CPUState *cs)
930fcf5ef2aSThomas Huth {
931fcf5ef2aSThomas Huth     return true;
932fcf5ef2aSThomas Huth }
933fcf5ef2aSThomas Huth 
934fcf5ef2aSThomas Huth int kvm_arch_process_async_events(CPUState *cs)
935fcf5ef2aSThomas Huth {
936fcf5ef2aSThomas Huth     return 0;
937fcf5ef2aSThomas Huth }
938fcf5ef2aSThomas Huth 
939fcf5ef2aSThomas Huth void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
940fcf5ef2aSThomas Huth {
941fcf5ef2aSThomas Huth     if (kvm_sw_breakpoints_active(cs)) {
942fcf5ef2aSThomas Huth         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
943fcf5ef2aSThomas Huth     }
944fcf5ef2aSThomas Huth     if (kvm_arm_hw_debug_active(cs)) {
945fcf5ef2aSThomas Huth         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW;
946fcf5ef2aSThomas Huth         kvm_arm_copy_hw_debug_data(&dbg->arch);
947fcf5ef2aSThomas Huth     }
948fcf5ef2aSThomas Huth }
949fcf5ef2aSThomas Huth 
950fcf5ef2aSThomas Huth void kvm_arch_init_irq_routing(KVMState *s)
951fcf5ef2aSThomas Huth {
952fcf5ef2aSThomas Huth }
953fcf5ef2aSThomas Huth 
9544376c40dSPaolo Bonzini int kvm_arch_irqchip_create(KVMState *s)
955fcf5ef2aSThomas Huth {
9564376c40dSPaolo Bonzini     if (kvm_kernel_irqchip_split()) {
957fcf5ef2aSThomas Huth         perror("-machine kernel_irqchip=split is not supported on ARM.");
958fcf5ef2aSThomas Huth         exit(1);
959fcf5ef2aSThomas Huth     }
960fcf5ef2aSThomas Huth 
961fcf5ef2aSThomas Huth     /* If we can create the VGIC using the newer device control API, we
962fcf5ef2aSThomas Huth      * let the device do this when it initializes itself, otherwise we
963fcf5ef2aSThomas Huth      * fall back to the old API */
964fcf5ef2aSThomas Huth     return kvm_check_extension(s, KVM_CAP_DEVICE_CTRL);
965fcf5ef2aSThomas Huth }
966fcf5ef2aSThomas Huth 
967fcf5ef2aSThomas Huth int kvm_arm_vgic_probe(void)
968fcf5ef2aSThomas Huth {
969d45efe47SEric Auger     int val = 0;
970d45efe47SEric Auger 
971fcf5ef2aSThomas Huth     if (kvm_create_device(kvm_state,
972fcf5ef2aSThomas Huth                           KVM_DEV_TYPE_ARM_VGIC_V3, true) == 0) {
973d45efe47SEric Auger         val |= KVM_ARM_VGIC_V3;
974fcf5ef2aSThomas Huth     }
975d45efe47SEric Auger     if (kvm_create_device(kvm_state,
976d45efe47SEric Auger                           KVM_DEV_TYPE_ARM_VGIC_V2, true) == 0) {
977d45efe47SEric Auger         val |= KVM_ARM_VGIC_V2;
978d45efe47SEric Auger     }
979d45efe47SEric Auger     return val;
980fcf5ef2aSThomas Huth }
981fcf5ef2aSThomas Huth 
982f6530926SEric Auger int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level)
983f6530926SEric Auger {
984f6530926SEric Auger     int kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT) | irq;
985f6530926SEric Auger     int cpu_idx1 = cpu % 256;
986f6530926SEric Auger     int cpu_idx2 = cpu / 256;
987f6530926SEric Auger 
988f6530926SEric Auger     kvm_irq |= (cpu_idx1 << KVM_ARM_IRQ_VCPU_SHIFT) |
989f6530926SEric Auger                (cpu_idx2 << KVM_ARM_IRQ_VCPU2_SHIFT);
990f6530926SEric Auger 
991f6530926SEric Auger     return kvm_set_irq(kvm_state, kvm_irq, !!level);
992f6530926SEric Auger }
993f6530926SEric Auger 
994fcf5ef2aSThomas Huth int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
995fcf5ef2aSThomas Huth                              uint64_t address, uint32_t data, PCIDevice *dev)
996fcf5ef2aSThomas Huth {
997b05c81d2SEric Auger     AddressSpace *as = pci_device_iommu_address_space(dev);
998b05c81d2SEric Auger     hwaddr xlat, len, doorbell_gpa;
999b05c81d2SEric Auger     MemoryRegionSection mrs;
1000b05c81d2SEric Auger     MemoryRegion *mr;
1001b05c81d2SEric Auger     int ret = 1;
1002b05c81d2SEric Auger 
1003b05c81d2SEric Auger     if (as == &address_space_memory) {
1004fcf5ef2aSThomas Huth         return 0;
1005fcf5ef2aSThomas Huth     }
1006fcf5ef2aSThomas Huth 
1007b05c81d2SEric Auger     /* MSI doorbell address is translated by an IOMMU */
1008b05c81d2SEric Auger 
1009b05c81d2SEric Auger     rcu_read_lock();
1010bc6b1cecSPeter Maydell     mr = address_space_translate(as, address, &xlat, &len, true,
1011bc6b1cecSPeter Maydell                                  MEMTXATTRS_UNSPECIFIED);
1012b05c81d2SEric Auger     if (!mr) {
1013b05c81d2SEric Auger         goto unlock;
1014b05c81d2SEric Auger     }
1015b05c81d2SEric Auger     mrs = memory_region_find(mr, xlat, 1);
1016b05c81d2SEric Auger     if (!mrs.mr) {
1017b05c81d2SEric Auger         goto unlock;
1018b05c81d2SEric Auger     }
1019b05c81d2SEric Auger 
1020b05c81d2SEric Auger     doorbell_gpa = mrs.offset_within_address_space;
1021b05c81d2SEric Auger     memory_region_unref(mrs.mr);
1022b05c81d2SEric Auger 
1023b05c81d2SEric Auger     route->u.msi.address_lo = doorbell_gpa;
1024b05c81d2SEric Auger     route->u.msi.address_hi = doorbell_gpa >> 32;
1025b05c81d2SEric Auger 
1026b05c81d2SEric Auger     trace_kvm_arm_fixup_msi_route(address, doorbell_gpa);
1027b05c81d2SEric Auger 
1028b05c81d2SEric Auger     ret = 0;
1029b05c81d2SEric Auger 
1030b05c81d2SEric Auger unlock:
1031b05c81d2SEric Auger     rcu_read_unlock();
1032b05c81d2SEric Auger     return ret;
1033b05c81d2SEric Auger }
1034b05c81d2SEric Auger 
1035fcf5ef2aSThomas Huth int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
1036fcf5ef2aSThomas Huth                                 int vector, PCIDevice *dev)
1037fcf5ef2aSThomas Huth {
1038fcf5ef2aSThomas Huth     return 0;
1039fcf5ef2aSThomas Huth }
1040fcf5ef2aSThomas Huth 
1041fcf5ef2aSThomas Huth int kvm_arch_release_virq_post(int virq)
1042fcf5ef2aSThomas Huth {
1043fcf5ef2aSThomas Huth     return 0;
1044fcf5ef2aSThomas Huth }
1045fcf5ef2aSThomas Huth 
1046fcf5ef2aSThomas Huth int kvm_arch_msi_data_to_gsi(uint32_t data)
1047fcf5ef2aSThomas Huth {
1048fcf5ef2aSThomas Huth     return (data - 32) & 0xffff;
1049fcf5ef2aSThomas Huth }
105092a5199bSTom Lendacky 
105192a5199bSTom Lendacky bool kvm_arch_cpu_check_are_resettable(void)
105292a5199bSTom Lendacky {
105392a5199bSTom Lendacky     return true;
105492a5199bSTom Lendacky }
1055