xref: /qemu/target/arm/kvm.c (revision f6530926)
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"
20fcf5ef2aSThomas Huth #include "sysemu/sysemu.h"
21fcf5ef2aSThomas Huth #include "sysemu/kvm.h"
22a27382e2SEric Auger #include "sysemu/kvm_int.h"
23fcf5ef2aSThomas Huth #include "kvm_arm.h"
24fcf5ef2aSThomas Huth #include "cpu.h"
25b05c81d2SEric Auger #include "trace.h"
26fcf5ef2aSThomas Huth #include "internals.h"
27b05c81d2SEric Auger #include "hw/pci/pci.h"
28fcf5ef2aSThomas Huth #include "exec/memattrs.h"
29fcf5ef2aSThomas Huth #include "exec/address-spaces.h"
30fcf5ef2aSThomas Huth #include "hw/boards.h"
3164552b6bSMarkus Armbruster #include "hw/irq.h"
32fcf5ef2aSThomas Huth #include "qemu/log.h"
33fcf5ef2aSThomas Huth 
34fcf5ef2aSThomas Huth const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
35fcf5ef2aSThomas Huth     KVM_CAP_LAST_INFO
36fcf5ef2aSThomas Huth };
37fcf5ef2aSThomas Huth 
38fcf5ef2aSThomas Huth static bool cap_has_mp_state;
39202ccb6bSDongjiu Geng static bool cap_has_inject_serror_esr;
40fcf5ef2aSThomas Huth 
41c4487d76SPeter Maydell static ARMHostCPUFeatures arm_host_cpu_features;
42c4487d76SPeter Maydell 
43fcf5ef2aSThomas Huth int kvm_arm_vcpu_init(CPUState *cs)
44fcf5ef2aSThomas Huth {
45fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
46fcf5ef2aSThomas Huth     struct kvm_vcpu_init init;
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth     init.target = cpu->kvm_target;
49fcf5ef2aSThomas Huth     memcpy(init.features, cpu->kvm_init_features, sizeof(init.features));
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init);
52fcf5ef2aSThomas Huth }
53fcf5ef2aSThomas Huth 
54202ccb6bSDongjiu Geng void kvm_arm_init_serror_injection(CPUState *cs)
55202ccb6bSDongjiu Geng {
56202ccb6bSDongjiu Geng     cap_has_inject_serror_esr = kvm_check_extension(cs->kvm_state,
57202ccb6bSDongjiu Geng                                     KVM_CAP_ARM_INJECT_SERROR_ESR);
58202ccb6bSDongjiu Geng }
59202ccb6bSDongjiu Geng 
60fcf5ef2aSThomas Huth bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
61fcf5ef2aSThomas Huth                                       int *fdarray,
62fcf5ef2aSThomas Huth                                       struct kvm_vcpu_init *init)
63fcf5ef2aSThomas Huth {
64fcf5ef2aSThomas Huth     int ret, kvmfd = -1, vmfd = -1, cpufd = -1;
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth     kvmfd = qemu_open("/dev/kvm", O_RDWR);
67fcf5ef2aSThomas Huth     if (kvmfd < 0) {
68fcf5ef2aSThomas Huth         goto err;
69fcf5ef2aSThomas Huth     }
70fcf5ef2aSThomas Huth     vmfd = ioctl(kvmfd, KVM_CREATE_VM, 0);
71fcf5ef2aSThomas Huth     if (vmfd < 0) {
72fcf5ef2aSThomas Huth         goto err;
73fcf5ef2aSThomas Huth     }
74fcf5ef2aSThomas Huth     cpufd = ioctl(vmfd, KVM_CREATE_VCPU, 0);
75fcf5ef2aSThomas Huth     if (cpufd < 0) {
76fcf5ef2aSThomas Huth         goto err;
77fcf5ef2aSThomas Huth     }
78fcf5ef2aSThomas Huth 
79fcf5ef2aSThomas Huth     if (!init) {
80fcf5ef2aSThomas Huth         /* Caller doesn't want the VCPU to be initialized, so skip it */
81fcf5ef2aSThomas Huth         goto finish;
82fcf5ef2aSThomas Huth     }
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth     ret = ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, init);
85fcf5ef2aSThomas Huth     if (ret >= 0) {
86fcf5ef2aSThomas Huth         ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, init);
87fcf5ef2aSThomas Huth         if (ret < 0) {
88fcf5ef2aSThomas Huth             goto err;
89fcf5ef2aSThomas Huth         }
90fcf5ef2aSThomas Huth     } else if (cpus_to_try) {
91fcf5ef2aSThomas Huth         /* Old kernel which doesn't know about the
92fcf5ef2aSThomas Huth          * PREFERRED_TARGET ioctl: we know it will only support
93fcf5ef2aSThomas Huth          * creating one kind of guest CPU which is its preferred
94fcf5ef2aSThomas Huth          * CPU type.
95fcf5ef2aSThomas Huth          */
96fcf5ef2aSThomas Huth         while (*cpus_to_try != QEMU_KVM_ARM_TARGET_NONE) {
97fcf5ef2aSThomas Huth             init->target = *cpus_to_try++;
98fcf5ef2aSThomas Huth             memset(init->features, 0, sizeof(init->features));
99fcf5ef2aSThomas Huth             ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, init);
100fcf5ef2aSThomas Huth             if (ret >= 0) {
101fcf5ef2aSThomas Huth                 break;
102fcf5ef2aSThomas Huth             }
103fcf5ef2aSThomas Huth         }
104fcf5ef2aSThomas Huth         if (ret < 0) {
105fcf5ef2aSThomas Huth             goto err;
106fcf5ef2aSThomas Huth         }
107fcf5ef2aSThomas Huth     } else {
108fcf5ef2aSThomas Huth         /* Treat a NULL cpus_to_try argument the same as an empty
109fcf5ef2aSThomas Huth          * list, which means we will fail the call since this must
110fcf5ef2aSThomas Huth          * be an old kernel which doesn't support PREFERRED_TARGET.
111fcf5ef2aSThomas Huth          */
112fcf5ef2aSThomas Huth         goto err;
113fcf5ef2aSThomas Huth     }
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth finish:
116fcf5ef2aSThomas Huth     fdarray[0] = kvmfd;
117fcf5ef2aSThomas Huth     fdarray[1] = vmfd;
118fcf5ef2aSThomas Huth     fdarray[2] = cpufd;
119fcf5ef2aSThomas Huth 
120fcf5ef2aSThomas Huth     return true;
121fcf5ef2aSThomas Huth 
122fcf5ef2aSThomas Huth err:
123fcf5ef2aSThomas Huth     if (cpufd >= 0) {
124fcf5ef2aSThomas Huth         close(cpufd);
125fcf5ef2aSThomas Huth     }
126fcf5ef2aSThomas Huth     if (vmfd >= 0) {
127fcf5ef2aSThomas Huth         close(vmfd);
128fcf5ef2aSThomas Huth     }
129fcf5ef2aSThomas Huth     if (kvmfd >= 0) {
130fcf5ef2aSThomas Huth         close(kvmfd);
131fcf5ef2aSThomas Huth     }
132fcf5ef2aSThomas Huth 
133fcf5ef2aSThomas Huth     return false;
134fcf5ef2aSThomas Huth }
135fcf5ef2aSThomas Huth 
136fcf5ef2aSThomas Huth void kvm_arm_destroy_scratch_host_vcpu(int *fdarray)
137fcf5ef2aSThomas Huth {
138fcf5ef2aSThomas Huth     int i;
139fcf5ef2aSThomas Huth 
140fcf5ef2aSThomas Huth     for (i = 2; i >= 0; i--) {
141fcf5ef2aSThomas Huth         close(fdarray[i]);
142fcf5ef2aSThomas Huth     }
143fcf5ef2aSThomas Huth }
144fcf5ef2aSThomas Huth 
145c4487d76SPeter Maydell void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu)
146fcf5ef2aSThomas Huth {
147c4487d76SPeter Maydell     CPUARMState *env = &cpu->env;
148fcf5ef2aSThomas Huth 
149c4487d76SPeter Maydell     if (!arm_host_cpu_features.dtb_compatible) {
150c4487d76SPeter Maydell         if (!kvm_enabled() ||
151c4487d76SPeter Maydell             !kvm_arm_get_host_cpu_features(&arm_host_cpu_features)) {
152c4487d76SPeter Maydell             /* We can't report this error yet, so flag that we need to
153c4487d76SPeter Maydell              * in arm_cpu_realizefn().
154fcf5ef2aSThomas Huth              */
155c4487d76SPeter Maydell             cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
156c4487d76SPeter Maydell             cpu->host_cpu_probe_failed = true;
157c4487d76SPeter Maydell             return;
158fcf5ef2aSThomas Huth         }
159fcf5ef2aSThomas Huth     }
160fcf5ef2aSThomas Huth 
161c4487d76SPeter Maydell     cpu->kvm_target = arm_host_cpu_features.target;
162c4487d76SPeter Maydell     cpu->dtb_compatible = arm_host_cpu_features.dtb_compatible;
1634674097cSRichard Henderson     cpu->isar = arm_host_cpu_features.isar;
164c4487d76SPeter Maydell     env->features = arm_host_cpu_features.features;
165c4487d76SPeter Maydell }
166c4487d76SPeter Maydell 
167ae502508SAndrew Jones bool kvm_arm_pmu_supported(CPUState *cpu)
168ae502508SAndrew Jones {
169ae502508SAndrew Jones     KVMState *s = KVM_STATE(current_machine->accelerator);
170ae502508SAndrew Jones 
171ae502508SAndrew Jones     return kvm_check_extension(s, KVM_CAP_ARM_PMU_V3);
172ae502508SAndrew Jones }
173ae502508SAndrew Jones 
174a27382e2SEric Auger int kvm_arm_get_max_vm_ipa_size(MachineState *ms)
175a27382e2SEric Auger {
176a27382e2SEric Auger     KVMState *s = KVM_STATE(ms->accelerator);
177a27382e2SEric Auger     int ret;
178a27382e2SEric Auger 
179a27382e2SEric Auger     ret = kvm_check_extension(s, KVM_CAP_ARM_VM_IPA_SIZE);
180a27382e2SEric Auger     return ret > 0 ? ret : 40;
181a27382e2SEric Auger }
182a27382e2SEric Auger 
183fcf5ef2aSThomas Huth int kvm_arch_init(MachineState *ms, KVMState *s)
184fcf5ef2aSThomas Huth {
185fcf5ef2aSThomas Huth     /* For ARM interrupt delivery is always asynchronous,
186fcf5ef2aSThomas Huth      * whether we are using an in-kernel VGIC or not.
187fcf5ef2aSThomas Huth      */
188fcf5ef2aSThomas Huth     kvm_async_interrupts_allowed = true;
189fcf5ef2aSThomas Huth 
1905d721b78SAlexander Graf     /*
1915d721b78SAlexander Graf      * PSCI wakes up secondary cores, so we always need to
1925d721b78SAlexander Graf      * have vCPUs waiting in kernel space
1935d721b78SAlexander Graf      */
1945d721b78SAlexander Graf     kvm_halt_in_kernel_allowed = true;
1955d721b78SAlexander Graf 
196fcf5ef2aSThomas Huth     cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE);
197fcf5ef2aSThomas Huth 
198fcf5ef2aSThomas Huth     return 0;
199fcf5ef2aSThomas Huth }
200fcf5ef2aSThomas Huth 
201fcf5ef2aSThomas Huth unsigned long kvm_arch_vcpu_id(CPUState *cpu)
202fcf5ef2aSThomas Huth {
203fcf5ef2aSThomas Huth     return cpu->cpu_index;
204fcf5ef2aSThomas Huth }
205fcf5ef2aSThomas Huth 
206fcf5ef2aSThomas Huth /* We track all the KVM devices which need their memory addresses
207fcf5ef2aSThomas Huth  * passing to the kernel in a list of these structures.
208fcf5ef2aSThomas Huth  * When board init is complete we run through the list and
209fcf5ef2aSThomas Huth  * tell the kernel the base addresses of the memory regions.
210fcf5ef2aSThomas Huth  * We use a MemoryListener to track mapping and unmapping of
211fcf5ef2aSThomas Huth  * the regions during board creation, so the board models don't
212fcf5ef2aSThomas Huth  * need to do anything special for the KVM case.
21319d1bd0bSEric Auger  *
21419d1bd0bSEric Auger  * Sometimes the address must be OR'ed with some other fields
21519d1bd0bSEric Auger  * (for example for KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION).
21619d1bd0bSEric Auger  * @kda_addr_ormask aims at storing the value of those fields.
217fcf5ef2aSThomas Huth  */
218fcf5ef2aSThomas Huth typedef struct KVMDevice {
219fcf5ef2aSThomas Huth     struct kvm_arm_device_addr kda;
220fcf5ef2aSThomas Huth     struct kvm_device_attr kdattr;
22119d1bd0bSEric Auger     uint64_t kda_addr_ormask;
222fcf5ef2aSThomas Huth     MemoryRegion *mr;
223fcf5ef2aSThomas Huth     QSLIST_ENTRY(KVMDevice) entries;
224fcf5ef2aSThomas Huth     int dev_fd;
225fcf5ef2aSThomas Huth } KVMDevice;
226fcf5ef2aSThomas Huth 
227b58deb34SPaolo Bonzini static QSLIST_HEAD(, KVMDevice) kvm_devices_head;
228fcf5ef2aSThomas Huth 
229fcf5ef2aSThomas Huth static void kvm_arm_devlistener_add(MemoryListener *listener,
230fcf5ef2aSThomas Huth                                     MemoryRegionSection *section)
231fcf5ef2aSThomas Huth {
232fcf5ef2aSThomas Huth     KVMDevice *kd;
233fcf5ef2aSThomas Huth 
234fcf5ef2aSThomas Huth     QSLIST_FOREACH(kd, &kvm_devices_head, entries) {
235fcf5ef2aSThomas Huth         if (section->mr == kd->mr) {
236fcf5ef2aSThomas Huth             kd->kda.addr = section->offset_within_address_space;
237fcf5ef2aSThomas Huth         }
238fcf5ef2aSThomas Huth     }
239fcf5ef2aSThomas Huth }
240fcf5ef2aSThomas Huth 
241fcf5ef2aSThomas Huth static void kvm_arm_devlistener_del(MemoryListener *listener,
242fcf5ef2aSThomas Huth                                     MemoryRegionSection *section)
243fcf5ef2aSThomas Huth {
244fcf5ef2aSThomas Huth     KVMDevice *kd;
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth     QSLIST_FOREACH(kd, &kvm_devices_head, entries) {
247fcf5ef2aSThomas Huth         if (section->mr == kd->mr) {
248fcf5ef2aSThomas Huth             kd->kda.addr = -1;
249fcf5ef2aSThomas Huth         }
250fcf5ef2aSThomas Huth     }
251fcf5ef2aSThomas Huth }
252fcf5ef2aSThomas Huth 
253fcf5ef2aSThomas Huth static MemoryListener devlistener = {
254fcf5ef2aSThomas Huth     .region_add = kvm_arm_devlistener_add,
255fcf5ef2aSThomas Huth     .region_del = kvm_arm_devlistener_del,
256fcf5ef2aSThomas Huth };
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth static void kvm_arm_set_device_addr(KVMDevice *kd)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     struct kvm_device_attr *attr = &kd->kdattr;
261fcf5ef2aSThomas Huth     int ret;
262fcf5ef2aSThomas Huth 
263fcf5ef2aSThomas Huth     /* If the device control API is available and we have a device fd on the
264fcf5ef2aSThomas Huth      * KVMDevice struct, let's use the newer API
265fcf5ef2aSThomas Huth      */
266fcf5ef2aSThomas Huth     if (kd->dev_fd >= 0) {
267fcf5ef2aSThomas Huth         uint64_t addr = kd->kda.addr;
26819d1bd0bSEric Auger 
26919d1bd0bSEric Auger         addr |= kd->kda_addr_ormask;
270fcf5ef2aSThomas Huth         attr->addr = (uintptr_t)&addr;
271fcf5ef2aSThomas Huth         ret = kvm_device_ioctl(kd->dev_fd, KVM_SET_DEVICE_ATTR, attr);
272fcf5ef2aSThomas Huth     } else {
273fcf5ef2aSThomas Huth         ret = kvm_vm_ioctl(kvm_state, KVM_ARM_SET_DEVICE_ADDR, &kd->kda);
274fcf5ef2aSThomas Huth     }
275fcf5ef2aSThomas Huth 
276fcf5ef2aSThomas Huth     if (ret < 0) {
277fcf5ef2aSThomas Huth         fprintf(stderr, "Failed to set device address: %s\n",
278fcf5ef2aSThomas Huth                 strerror(-ret));
279fcf5ef2aSThomas Huth         abort();
280fcf5ef2aSThomas Huth     }
281fcf5ef2aSThomas Huth }
282fcf5ef2aSThomas Huth 
283fcf5ef2aSThomas Huth static void kvm_arm_machine_init_done(Notifier *notifier, void *data)
284fcf5ef2aSThomas Huth {
285fcf5ef2aSThomas Huth     KVMDevice *kd, *tkd;
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth     QSLIST_FOREACH_SAFE(kd, &kvm_devices_head, entries, tkd) {
288fcf5ef2aSThomas Huth         if (kd->kda.addr != -1) {
289fcf5ef2aSThomas Huth             kvm_arm_set_device_addr(kd);
290fcf5ef2aSThomas Huth         }
291fcf5ef2aSThomas Huth         memory_region_unref(kd->mr);
2925ff9aaabSZheng Xiang         QSLIST_REMOVE_HEAD(&kvm_devices_head, entries);
293fcf5ef2aSThomas Huth         g_free(kd);
294fcf5ef2aSThomas Huth     }
2950bbe4354SPeter Xu     memory_listener_unregister(&devlistener);
296fcf5ef2aSThomas Huth }
297fcf5ef2aSThomas Huth 
298fcf5ef2aSThomas Huth static Notifier notify = {
299fcf5ef2aSThomas Huth     .notify = kvm_arm_machine_init_done,
300fcf5ef2aSThomas Huth };
301fcf5ef2aSThomas Huth 
302fcf5ef2aSThomas Huth void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
30319d1bd0bSEric Auger                              uint64_t attr, int dev_fd, uint64_t addr_ormask)
304fcf5ef2aSThomas Huth {
305fcf5ef2aSThomas Huth     KVMDevice *kd;
306fcf5ef2aSThomas Huth 
307fcf5ef2aSThomas Huth     if (!kvm_irqchip_in_kernel()) {
308fcf5ef2aSThomas Huth         return;
309fcf5ef2aSThomas Huth     }
310fcf5ef2aSThomas Huth 
311fcf5ef2aSThomas Huth     if (QSLIST_EMPTY(&kvm_devices_head)) {
312fcf5ef2aSThomas Huth         memory_listener_register(&devlistener, &address_space_memory);
313fcf5ef2aSThomas Huth         qemu_add_machine_init_done_notifier(&notify);
314fcf5ef2aSThomas Huth     }
315fcf5ef2aSThomas Huth     kd = g_new0(KVMDevice, 1);
316fcf5ef2aSThomas Huth     kd->mr = mr;
317fcf5ef2aSThomas Huth     kd->kda.id = devid;
318fcf5ef2aSThomas Huth     kd->kda.addr = -1;
319fcf5ef2aSThomas Huth     kd->kdattr.flags = 0;
320fcf5ef2aSThomas Huth     kd->kdattr.group = group;
321fcf5ef2aSThomas Huth     kd->kdattr.attr = attr;
322fcf5ef2aSThomas Huth     kd->dev_fd = dev_fd;
32319d1bd0bSEric Auger     kd->kda_addr_ormask = addr_ormask;
324fcf5ef2aSThomas Huth     QSLIST_INSERT_HEAD(&kvm_devices_head, kd, entries);
325fcf5ef2aSThomas Huth     memory_region_ref(kd->mr);
326fcf5ef2aSThomas Huth }
327fcf5ef2aSThomas Huth 
328fcf5ef2aSThomas Huth static int compare_u64(const void *a, const void *b)
329fcf5ef2aSThomas Huth {
330fcf5ef2aSThomas Huth     if (*(uint64_t *)a > *(uint64_t *)b) {
331fcf5ef2aSThomas Huth         return 1;
332fcf5ef2aSThomas Huth     }
333fcf5ef2aSThomas Huth     if (*(uint64_t *)a < *(uint64_t *)b) {
334fcf5ef2aSThomas Huth         return -1;
335fcf5ef2aSThomas Huth     }
336fcf5ef2aSThomas Huth     return 0;
337fcf5ef2aSThomas Huth }
338fcf5ef2aSThomas Huth 
339c8a44709SDongjiu Geng /* Initialize the ARMCPU cpreg list according to the kernel's
340fcf5ef2aSThomas Huth  * definition of what CPU registers it knows about (and throw away
341fcf5ef2aSThomas Huth  * the previous TCG-created cpreg list).
342fcf5ef2aSThomas Huth  */
343fcf5ef2aSThomas Huth int kvm_arm_init_cpreg_list(ARMCPU *cpu)
344fcf5ef2aSThomas Huth {
345fcf5ef2aSThomas Huth     struct kvm_reg_list rl;
346fcf5ef2aSThomas Huth     struct kvm_reg_list *rlp;
347fcf5ef2aSThomas Huth     int i, ret, arraylen;
348fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
349fcf5ef2aSThomas Huth 
350fcf5ef2aSThomas Huth     rl.n = 0;
351fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl);
352fcf5ef2aSThomas Huth     if (ret != -E2BIG) {
353fcf5ef2aSThomas Huth         return ret;
354fcf5ef2aSThomas Huth     }
355fcf5ef2aSThomas Huth     rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t));
356fcf5ef2aSThomas Huth     rlp->n = rl.n;
357fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp);
358fcf5ef2aSThomas Huth     if (ret) {
359fcf5ef2aSThomas Huth         goto out;
360fcf5ef2aSThomas Huth     }
361fcf5ef2aSThomas Huth     /* Sort the list we get back from the kernel, since cpreg_tuples
362fcf5ef2aSThomas Huth      * must be in strictly ascending order.
363fcf5ef2aSThomas Huth      */
364fcf5ef2aSThomas Huth     qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64);
365fcf5ef2aSThomas Huth 
366fcf5ef2aSThomas Huth     for (i = 0, arraylen = 0; i < rlp->n; i++) {
367fcf5ef2aSThomas Huth         if (!kvm_arm_reg_syncs_via_cpreg_list(rlp->reg[i])) {
368fcf5ef2aSThomas Huth             continue;
369fcf5ef2aSThomas Huth         }
370fcf5ef2aSThomas Huth         switch (rlp->reg[i] & KVM_REG_SIZE_MASK) {
371fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U32:
372fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U64:
373fcf5ef2aSThomas Huth             break;
374fcf5ef2aSThomas Huth         default:
375fcf5ef2aSThomas Huth             fprintf(stderr, "Can't handle size of register in kernel list\n");
376fcf5ef2aSThomas Huth             ret = -EINVAL;
377fcf5ef2aSThomas Huth             goto out;
378fcf5ef2aSThomas Huth         }
379fcf5ef2aSThomas Huth 
380fcf5ef2aSThomas Huth         arraylen++;
381fcf5ef2aSThomas Huth     }
382fcf5ef2aSThomas Huth 
383fcf5ef2aSThomas Huth     cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen);
384fcf5ef2aSThomas Huth     cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen);
385fcf5ef2aSThomas Huth     cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes,
386fcf5ef2aSThomas Huth                                          arraylen);
387fcf5ef2aSThomas Huth     cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values,
388fcf5ef2aSThomas Huth                                         arraylen);
389fcf5ef2aSThomas Huth     cpu->cpreg_array_len = arraylen;
390fcf5ef2aSThomas Huth     cpu->cpreg_vmstate_array_len = arraylen;
391fcf5ef2aSThomas Huth 
392fcf5ef2aSThomas Huth     for (i = 0, arraylen = 0; i < rlp->n; i++) {
393fcf5ef2aSThomas Huth         uint64_t regidx = rlp->reg[i];
394fcf5ef2aSThomas Huth         if (!kvm_arm_reg_syncs_via_cpreg_list(regidx)) {
395fcf5ef2aSThomas Huth             continue;
396fcf5ef2aSThomas Huth         }
397fcf5ef2aSThomas Huth         cpu->cpreg_indexes[arraylen] = regidx;
398fcf5ef2aSThomas Huth         arraylen++;
399fcf5ef2aSThomas Huth     }
400fcf5ef2aSThomas Huth     assert(cpu->cpreg_array_len == arraylen);
401fcf5ef2aSThomas Huth 
402fcf5ef2aSThomas Huth     if (!write_kvmstate_to_list(cpu)) {
403fcf5ef2aSThomas Huth         /* Shouldn't happen unless kernel is inconsistent about
404fcf5ef2aSThomas Huth          * what registers exist.
405fcf5ef2aSThomas Huth          */
406fcf5ef2aSThomas Huth         fprintf(stderr, "Initial read of kernel register state failed\n");
407fcf5ef2aSThomas Huth         ret = -EINVAL;
408fcf5ef2aSThomas Huth         goto out;
409fcf5ef2aSThomas Huth     }
410fcf5ef2aSThomas Huth 
411fcf5ef2aSThomas Huth out:
412fcf5ef2aSThomas Huth     g_free(rlp);
413fcf5ef2aSThomas Huth     return ret;
414fcf5ef2aSThomas Huth }
415fcf5ef2aSThomas Huth 
416fcf5ef2aSThomas Huth bool write_kvmstate_to_list(ARMCPU *cpu)
417fcf5ef2aSThomas Huth {
418fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
419fcf5ef2aSThomas Huth     int i;
420fcf5ef2aSThomas Huth     bool ok = true;
421fcf5ef2aSThomas Huth 
422fcf5ef2aSThomas Huth     for (i = 0; i < cpu->cpreg_array_len; i++) {
423fcf5ef2aSThomas Huth         struct kvm_one_reg r;
424fcf5ef2aSThomas Huth         uint64_t regidx = cpu->cpreg_indexes[i];
425fcf5ef2aSThomas Huth         uint32_t v32;
426fcf5ef2aSThomas Huth         int ret;
427fcf5ef2aSThomas Huth 
428fcf5ef2aSThomas Huth         r.id = regidx;
429fcf5ef2aSThomas Huth 
430fcf5ef2aSThomas Huth         switch (regidx & KVM_REG_SIZE_MASK) {
431fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U32:
432fcf5ef2aSThomas Huth             r.addr = (uintptr_t)&v32;
433fcf5ef2aSThomas Huth             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
434fcf5ef2aSThomas Huth             if (!ret) {
435fcf5ef2aSThomas Huth                 cpu->cpreg_values[i] = v32;
436fcf5ef2aSThomas Huth             }
437fcf5ef2aSThomas Huth             break;
438fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U64:
439fcf5ef2aSThomas Huth             r.addr = (uintptr_t)(cpu->cpreg_values + i);
440fcf5ef2aSThomas Huth             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
441fcf5ef2aSThomas Huth             break;
442fcf5ef2aSThomas Huth         default:
443fcf5ef2aSThomas Huth             abort();
444fcf5ef2aSThomas Huth         }
445fcf5ef2aSThomas Huth         if (ret) {
446fcf5ef2aSThomas Huth             ok = false;
447fcf5ef2aSThomas Huth         }
448fcf5ef2aSThomas Huth     }
449fcf5ef2aSThomas Huth     return ok;
450fcf5ef2aSThomas Huth }
451fcf5ef2aSThomas Huth 
452fcf5ef2aSThomas Huth bool write_list_to_kvmstate(ARMCPU *cpu, int level)
453fcf5ef2aSThomas Huth {
454fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
455fcf5ef2aSThomas Huth     int i;
456fcf5ef2aSThomas Huth     bool ok = true;
457fcf5ef2aSThomas Huth 
458fcf5ef2aSThomas Huth     for (i = 0; i < cpu->cpreg_array_len; i++) {
459fcf5ef2aSThomas Huth         struct kvm_one_reg r;
460fcf5ef2aSThomas Huth         uint64_t regidx = cpu->cpreg_indexes[i];
461fcf5ef2aSThomas Huth         uint32_t v32;
462fcf5ef2aSThomas Huth         int ret;
463fcf5ef2aSThomas Huth 
464fcf5ef2aSThomas Huth         if (kvm_arm_cpreg_level(regidx) > level) {
465fcf5ef2aSThomas Huth             continue;
466fcf5ef2aSThomas Huth         }
467fcf5ef2aSThomas Huth 
468fcf5ef2aSThomas Huth         r.id = regidx;
469fcf5ef2aSThomas Huth         switch (regidx & KVM_REG_SIZE_MASK) {
470fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U32:
471fcf5ef2aSThomas Huth             v32 = cpu->cpreg_values[i];
472fcf5ef2aSThomas Huth             r.addr = (uintptr_t)&v32;
473fcf5ef2aSThomas Huth             break;
474fcf5ef2aSThomas Huth         case KVM_REG_SIZE_U64:
475fcf5ef2aSThomas Huth             r.addr = (uintptr_t)(cpu->cpreg_values + i);
476fcf5ef2aSThomas Huth             break;
477fcf5ef2aSThomas Huth         default:
478fcf5ef2aSThomas Huth             abort();
479fcf5ef2aSThomas Huth         }
480fcf5ef2aSThomas Huth         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
481fcf5ef2aSThomas Huth         if (ret) {
482fcf5ef2aSThomas Huth             /* We might fail for "unknown register" and also for
483fcf5ef2aSThomas Huth              * "you tried to set a register which is constant with
484fcf5ef2aSThomas Huth              * a different value from what it actually contains".
485fcf5ef2aSThomas Huth              */
486fcf5ef2aSThomas Huth             ok = false;
487fcf5ef2aSThomas Huth         }
488fcf5ef2aSThomas Huth     }
489fcf5ef2aSThomas Huth     return ok;
490fcf5ef2aSThomas Huth }
491fcf5ef2aSThomas Huth 
492fcf5ef2aSThomas Huth void kvm_arm_reset_vcpu(ARMCPU *cpu)
493fcf5ef2aSThomas Huth {
494fcf5ef2aSThomas Huth     int ret;
495fcf5ef2aSThomas Huth 
496fcf5ef2aSThomas Huth     /* Re-init VCPU so that all registers are set to
497fcf5ef2aSThomas Huth      * their respective reset values.
498fcf5ef2aSThomas Huth      */
499fcf5ef2aSThomas Huth     ret = kvm_arm_vcpu_init(CPU(cpu));
500fcf5ef2aSThomas Huth     if (ret < 0) {
501fcf5ef2aSThomas Huth         fprintf(stderr, "kvm_arm_vcpu_init failed: %s\n", strerror(-ret));
502fcf5ef2aSThomas Huth         abort();
503fcf5ef2aSThomas Huth     }
504fcf5ef2aSThomas Huth     if (!write_kvmstate_to_list(cpu)) {
505fcf5ef2aSThomas Huth         fprintf(stderr, "write_kvmstate_to_list failed\n");
506fcf5ef2aSThomas Huth         abort();
507fcf5ef2aSThomas Huth     }
508b698e4eeSPeter Maydell     /*
509b698e4eeSPeter Maydell      * Sync the reset values also into the CPUState. This is necessary
510b698e4eeSPeter Maydell      * because the next thing we do will be a kvm_arch_put_registers()
511b698e4eeSPeter Maydell      * which will update the list values from the CPUState before copying
512b698e4eeSPeter Maydell      * the list values back to KVM. It's OK to ignore failure returns here
513b698e4eeSPeter Maydell      * for the same reason we do so in kvm_arch_get_registers().
514b698e4eeSPeter Maydell      */
515b698e4eeSPeter Maydell     write_list_to_cpustate(cpu);
516fcf5ef2aSThomas Huth }
517fcf5ef2aSThomas Huth 
518fcf5ef2aSThomas Huth /*
519fcf5ef2aSThomas Huth  * Update KVM's MP_STATE based on what QEMU thinks it is
520fcf5ef2aSThomas Huth  */
521fcf5ef2aSThomas Huth int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu)
522fcf5ef2aSThomas Huth {
523fcf5ef2aSThomas Huth     if (cap_has_mp_state) {
524fcf5ef2aSThomas Huth         struct kvm_mp_state mp_state = {
525062ba099SAlex Bennée             .mp_state = (cpu->power_state == PSCI_OFF) ?
526062ba099SAlex Bennée             KVM_MP_STATE_STOPPED : KVM_MP_STATE_RUNNABLE
527fcf5ef2aSThomas Huth         };
528fcf5ef2aSThomas Huth         int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
529fcf5ef2aSThomas Huth         if (ret) {
530fcf5ef2aSThomas Huth             fprintf(stderr, "%s: failed to set MP_STATE %d/%s\n",
531fcf5ef2aSThomas Huth                     __func__, ret, strerror(-ret));
532fcf5ef2aSThomas Huth             return -1;
533fcf5ef2aSThomas Huth         }
534fcf5ef2aSThomas Huth     }
535fcf5ef2aSThomas Huth 
536fcf5ef2aSThomas Huth     return 0;
537fcf5ef2aSThomas Huth }
538fcf5ef2aSThomas Huth 
539fcf5ef2aSThomas Huth /*
540fcf5ef2aSThomas Huth  * Sync the KVM MP_STATE into QEMU
541fcf5ef2aSThomas Huth  */
542fcf5ef2aSThomas Huth int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu)
543fcf5ef2aSThomas Huth {
544fcf5ef2aSThomas Huth     if (cap_has_mp_state) {
545fcf5ef2aSThomas Huth         struct kvm_mp_state mp_state;
546fcf5ef2aSThomas Huth         int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MP_STATE, &mp_state);
547fcf5ef2aSThomas Huth         if (ret) {
548fcf5ef2aSThomas Huth             fprintf(stderr, "%s: failed to get MP_STATE %d/%s\n",
549fcf5ef2aSThomas Huth                     __func__, ret, strerror(-ret));
550fcf5ef2aSThomas Huth             abort();
551fcf5ef2aSThomas Huth         }
552062ba099SAlex Bennée         cpu->power_state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ?
553062ba099SAlex Bennée             PSCI_OFF : PSCI_ON;
554fcf5ef2aSThomas Huth     }
555fcf5ef2aSThomas Huth 
556fcf5ef2aSThomas Huth     return 0;
557fcf5ef2aSThomas Huth }
558fcf5ef2aSThomas Huth 
559202ccb6bSDongjiu Geng int kvm_put_vcpu_events(ARMCPU *cpu)
560202ccb6bSDongjiu Geng {
561202ccb6bSDongjiu Geng     CPUARMState *env = &cpu->env;
562202ccb6bSDongjiu Geng     struct kvm_vcpu_events events;
563202ccb6bSDongjiu Geng     int ret;
564202ccb6bSDongjiu Geng 
565202ccb6bSDongjiu Geng     if (!kvm_has_vcpu_events()) {
566202ccb6bSDongjiu Geng         return 0;
567202ccb6bSDongjiu Geng     }
568202ccb6bSDongjiu Geng 
569202ccb6bSDongjiu Geng     memset(&events, 0, sizeof(events));
570202ccb6bSDongjiu Geng     events.exception.serror_pending = env->serror.pending;
571202ccb6bSDongjiu Geng 
572202ccb6bSDongjiu Geng     /* Inject SError to guest with specified syndrome if host kernel
573202ccb6bSDongjiu Geng      * supports it, otherwise inject SError without syndrome.
574202ccb6bSDongjiu Geng      */
575202ccb6bSDongjiu Geng     if (cap_has_inject_serror_esr) {
576202ccb6bSDongjiu Geng         events.exception.serror_has_esr = env->serror.has_esr;
577202ccb6bSDongjiu Geng         events.exception.serror_esr = env->serror.esr;
578202ccb6bSDongjiu Geng     }
579202ccb6bSDongjiu Geng 
580202ccb6bSDongjiu Geng     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);
581202ccb6bSDongjiu Geng     if (ret) {
582202ccb6bSDongjiu Geng         error_report("failed to put vcpu events");
583202ccb6bSDongjiu Geng     }
584202ccb6bSDongjiu Geng 
585202ccb6bSDongjiu Geng     return ret;
586202ccb6bSDongjiu Geng }
587202ccb6bSDongjiu Geng 
588202ccb6bSDongjiu Geng int kvm_get_vcpu_events(ARMCPU *cpu)
589202ccb6bSDongjiu Geng {
590202ccb6bSDongjiu Geng     CPUARMState *env = &cpu->env;
591202ccb6bSDongjiu Geng     struct kvm_vcpu_events events;
592202ccb6bSDongjiu Geng     int ret;
593202ccb6bSDongjiu Geng 
594202ccb6bSDongjiu Geng     if (!kvm_has_vcpu_events()) {
595202ccb6bSDongjiu Geng         return 0;
596202ccb6bSDongjiu Geng     }
597202ccb6bSDongjiu Geng 
598202ccb6bSDongjiu Geng     memset(&events, 0, sizeof(events));
599202ccb6bSDongjiu Geng     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events);
600202ccb6bSDongjiu Geng     if (ret) {
601202ccb6bSDongjiu Geng         error_report("failed to get vcpu events");
602202ccb6bSDongjiu Geng         return ret;
603202ccb6bSDongjiu Geng     }
604202ccb6bSDongjiu Geng 
605202ccb6bSDongjiu Geng     env->serror.pending = events.exception.serror_pending;
606202ccb6bSDongjiu Geng     env->serror.has_esr = events.exception.serror_has_esr;
607202ccb6bSDongjiu Geng     env->serror.esr = events.exception.serror_esr;
608202ccb6bSDongjiu Geng 
609202ccb6bSDongjiu Geng     return 0;
610202ccb6bSDongjiu Geng }
611202ccb6bSDongjiu Geng 
612fcf5ef2aSThomas Huth void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
613fcf5ef2aSThomas Huth {
614fcf5ef2aSThomas Huth }
615fcf5ef2aSThomas Huth 
616fcf5ef2aSThomas Huth MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
617fcf5ef2aSThomas Huth {
6185d721b78SAlexander Graf     ARMCPU *cpu;
6195d721b78SAlexander Graf     uint32_t switched_level;
6205d721b78SAlexander Graf 
6215d721b78SAlexander Graf     if (kvm_irqchip_in_kernel()) {
6225d721b78SAlexander Graf         /*
6235d721b78SAlexander Graf          * We only need to sync timer states with user-space interrupt
6245d721b78SAlexander Graf          * controllers, so return early and save cycles if we don't.
6255d721b78SAlexander Graf          */
6265d721b78SAlexander Graf         return MEMTXATTRS_UNSPECIFIED;
6275d721b78SAlexander Graf     }
6285d721b78SAlexander Graf 
6295d721b78SAlexander Graf     cpu = ARM_CPU(cs);
6305d721b78SAlexander Graf 
6315d721b78SAlexander Graf     /* Synchronize our shadowed in-kernel device irq lines with the kvm ones */
6325d721b78SAlexander Graf     if (run->s.regs.device_irq_level != cpu->device_irq_level) {
6335d721b78SAlexander Graf         switched_level = cpu->device_irq_level ^ run->s.regs.device_irq_level;
6345d721b78SAlexander Graf 
6355d721b78SAlexander Graf         qemu_mutex_lock_iothread();
6365d721b78SAlexander Graf 
6375d721b78SAlexander Graf         if (switched_level & KVM_ARM_DEV_EL1_VTIMER) {
6385d721b78SAlexander Graf             qemu_set_irq(cpu->gt_timer_outputs[GTIMER_VIRT],
6395d721b78SAlexander Graf                          !!(run->s.regs.device_irq_level &
6405d721b78SAlexander Graf                             KVM_ARM_DEV_EL1_VTIMER));
6415d721b78SAlexander Graf             switched_level &= ~KVM_ARM_DEV_EL1_VTIMER;
6425d721b78SAlexander Graf         }
6435d721b78SAlexander Graf 
6445d721b78SAlexander Graf         if (switched_level & KVM_ARM_DEV_EL1_PTIMER) {
6455d721b78SAlexander Graf             qemu_set_irq(cpu->gt_timer_outputs[GTIMER_PHYS],
6465d721b78SAlexander Graf                          !!(run->s.regs.device_irq_level &
6475d721b78SAlexander Graf                             KVM_ARM_DEV_EL1_PTIMER));
6485d721b78SAlexander Graf             switched_level &= ~KVM_ARM_DEV_EL1_PTIMER;
6495d721b78SAlexander Graf         }
6505d721b78SAlexander Graf 
651b1659527SAndrew Jones         if (switched_level & KVM_ARM_DEV_PMU) {
652b1659527SAndrew Jones             qemu_set_irq(cpu->pmu_interrupt,
653b1659527SAndrew Jones                          !!(run->s.regs.device_irq_level & KVM_ARM_DEV_PMU));
654b1659527SAndrew Jones             switched_level &= ~KVM_ARM_DEV_PMU;
655b1659527SAndrew Jones         }
6565d721b78SAlexander Graf 
6575d721b78SAlexander Graf         if (switched_level) {
6585d721b78SAlexander Graf             qemu_log_mask(LOG_UNIMP, "%s: unhandled in-kernel device IRQ %x\n",
6595d721b78SAlexander Graf                           __func__, switched_level);
6605d721b78SAlexander Graf         }
6615d721b78SAlexander Graf 
6625d721b78SAlexander Graf         /* We also mark unknown levels as processed to not waste cycles */
6635d721b78SAlexander Graf         cpu->device_irq_level = run->s.regs.device_irq_level;
6645d721b78SAlexander Graf         qemu_mutex_unlock_iothread();
6655d721b78SAlexander Graf     }
6665d721b78SAlexander Graf 
667fcf5ef2aSThomas Huth     return MEMTXATTRS_UNSPECIFIED;
668fcf5ef2aSThomas Huth }
669fcf5ef2aSThomas Huth 
670fcf5ef2aSThomas Huth 
671fcf5ef2aSThomas Huth int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
672fcf5ef2aSThomas Huth {
673fcf5ef2aSThomas Huth     int ret = 0;
674fcf5ef2aSThomas Huth 
675fcf5ef2aSThomas Huth     switch (run->exit_reason) {
676fcf5ef2aSThomas Huth     case KVM_EXIT_DEBUG:
677fcf5ef2aSThomas Huth         if (kvm_arm_handle_debug(cs, &run->debug.arch)) {
678fcf5ef2aSThomas Huth             ret = EXCP_DEBUG;
679fcf5ef2aSThomas Huth         } /* otherwise return to guest */
680fcf5ef2aSThomas Huth         break;
681fcf5ef2aSThomas Huth     default:
682fcf5ef2aSThomas Huth         qemu_log_mask(LOG_UNIMP, "%s: un-handled exit reason %d\n",
683fcf5ef2aSThomas Huth                       __func__, run->exit_reason);
684fcf5ef2aSThomas Huth         break;
685fcf5ef2aSThomas Huth     }
686fcf5ef2aSThomas Huth     return ret;
687fcf5ef2aSThomas Huth }
688fcf5ef2aSThomas Huth 
689fcf5ef2aSThomas Huth bool kvm_arch_stop_on_emulation_error(CPUState *cs)
690fcf5ef2aSThomas Huth {
691fcf5ef2aSThomas Huth     return true;
692fcf5ef2aSThomas Huth }
693fcf5ef2aSThomas Huth 
694fcf5ef2aSThomas Huth int kvm_arch_process_async_events(CPUState *cs)
695fcf5ef2aSThomas Huth {
696fcf5ef2aSThomas Huth     return 0;
697fcf5ef2aSThomas Huth }
698fcf5ef2aSThomas Huth 
699fcf5ef2aSThomas Huth /* The #ifdef protections are until 32bit headers are imported and can
700fcf5ef2aSThomas Huth  * be removed once both 32 and 64 bit reach feature parity.
701fcf5ef2aSThomas Huth  */
702fcf5ef2aSThomas Huth void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
703fcf5ef2aSThomas Huth {
704fcf5ef2aSThomas Huth #ifdef KVM_GUESTDBG_USE_SW_BP
705fcf5ef2aSThomas Huth     if (kvm_sw_breakpoints_active(cs)) {
706fcf5ef2aSThomas Huth         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
707fcf5ef2aSThomas Huth     }
708fcf5ef2aSThomas Huth #endif
709fcf5ef2aSThomas Huth #ifdef KVM_GUESTDBG_USE_HW
710fcf5ef2aSThomas Huth     if (kvm_arm_hw_debug_active(cs)) {
711fcf5ef2aSThomas Huth         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW;
712fcf5ef2aSThomas Huth         kvm_arm_copy_hw_debug_data(&dbg->arch);
713fcf5ef2aSThomas Huth     }
714fcf5ef2aSThomas Huth #endif
715fcf5ef2aSThomas Huth }
716fcf5ef2aSThomas Huth 
717fcf5ef2aSThomas Huth void kvm_arch_init_irq_routing(KVMState *s)
718fcf5ef2aSThomas Huth {
719fcf5ef2aSThomas Huth }
720fcf5ef2aSThomas Huth 
721fcf5ef2aSThomas Huth int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
722fcf5ef2aSThomas Huth {
723fcf5ef2aSThomas Huth      if (machine_kernel_irqchip_split(ms)) {
724fcf5ef2aSThomas Huth          perror("-machine kernel_irqchip=split is not supported on ARM.");
725fcf5ef2aSThomas Huth          exit(1);
726fcf5ef2aSThomas Huth     }
727fcf5ef2aSThomas Huth 
728fcf5ef2aSThomas Huth     /* If we can create the VGIC using the newer device control API, we
729fcf5ef2aSThomas Huth      * let the device do this when it initializes itself, otherwise we
730fcf5ef2aSThomas Huth      * fall back to the old API */
731fcf5ef2aSThomas Huth     return kvm_check_extension(s, KVM_CAP_DEVICE_CTRL);
732fcf5ef2aSThomas Huth }
733fcf5ef2aSThomas Huth 
734fcf5ef2aSThomas Huth int kvm_arm_vgic_probe(void)
735fcf5ef2aSThomas Huth {
736fcf5ef2aSThomas Huth     if (kvm_create_device(kvm_state,
737fcf5ef2aSThomas Huth                           KVM_DEV_TYPE_ARM_VGIC_V3, true) == 0) {
738fcf5ef2aSThomas Huth         return 3;
739fcf5ef2aSThomas Huth     } else if (kvm_create_device(kvm_state,
740fcf5ef2aSThomas Huth                                  KVM_DEV_TYPE_ARM_VGIC_V2, true) == 0) {
741fcf5ef2aSThomas Huth         return 2;
742fcf5ef2aSThomas Huth     } else {
743fcf5ef2aSThomas Huth         return 0;
744fcf5ef2aSThomas Huth     }
745fcf5ef2aSThomas Huth }
746fcf5ef2aSThomas Huth 
747*f6530926SEric Auger int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level)
748*f6530926SEric Auger {
749*f6530926SEric Auger     int kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT) | irq;
750*f6530926SEric Auger     int cpu_idx1 = cpu % 256;
751*f6530926SEric Auger     int cpu_idx2 = cpu / 256;
752*f6530926SEric Auger 
753*f6530926SEric Auger     kvm_irq |= (cpu_idx1 << KVM_ARM_IRQ_VCPU_SHIFT) |
754*f6530926SEric Auger                (cpu_idx2 << KVM_ARM_IRQ_VCPU2_SHIFT);
755*f6530926SEric Auger 
756*f6530926SEric Auger     return kvm_set_irq(kvm_state, kvm_irq, !!level);
757*f6530926SEric Auger }
758*f6530926SEric Auger 
759fcf5ef2aSThomas Huth int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
760fcf5ef2aSThomas Huth                              uint64_t address, uint32_t data, PCIDevice *dev)
761fcf5ef2aSThomas Huth {
762b05c81d2SEric Auger     AddressSpace *as = pci_device_iommu_address_space(dev);
763b05c81d2SEric Auger     hwaddr xlat, len, doorbell_gpa;
764b05c81d2SEric Auger     MemoryRegionSection mrs;
765b05c81d2SEric Auger     MemoryRegion *mr;
766b05c81d2SEric Auger     int ret = 1;
767b05c81d2SEric Auger 
768b05c81d2SEric Auger     if (as == &address_space_memory) {
769fcf5ef2aSThomas Huth         return 0;
770fcf5ef2aSThomas Huth     }
771fcf5ef2aSThomas Huth 
772b05c81d2SEric Auger     /* MSI doorbell address is translated by an IOMMU */
773b05c81d2SEric Auger 
774b05c81d2SEric Auger     rcu_read_lock();
775bc6b1cecSPeter Maydell     mr = address_space_translate(as, address, &xlat, &len, true,
776bc6b1cecSPeter Maydell                                  MEMTXATTRS_UNSPECIFIED);
777b05c81d2SEric Auger     if (!mr) {
778b05c81d2SEric Auger         goto unlock;
779b05c81d2SEric Auger     }
780b05c81d2SEric Auger     mrs = memory_region_find(mr, xlat, 1);
781b05c81d2SEric Auger     if (!mrs.mr) {
782b05c81d2SEric Auger         goto unlock;
783b05c81d2SEric Auger     }
784b05c81d2SEric Auger 
785b05c81d2SEric Auger     doorbell_gpa = mrs.offset_within_address_space;
786b05c81d2SEric Auger     memory_region_unref(mrs.mr);
787b05c81d2SEric Auger 
788b05c81d2SEric Auger     route->u.msi.address_lo = doorbell_gpa;
789b05c81d2SEric Auger     route->u.msi.address_hi = doorbell_gpa >> 32;
790b05c81d2SEric Auger 
791b05c81d2SEric Auger     trace_kvm_arm_fixup_msi_route(address, doorbell_gpa);
792b05c81d2SEric Auger 
793b05c81d2SEric Auger     ret = 0;
794b05c81d2SEric Auger 
795b05c81d2SEric Auger unlock:
796b05c81d2SEric Auger     rcu_read_unlock();
797b05c81d2SEric Auger     return ret;
798b05c81d2SEric Auger }
799b05c81d2SEric Auger 
800fcf5ef2aSThomas Huth int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
801fcf5ef2aSThomas Huth                                 int vector, PCIDevice *dev)
802fcf5ef2aSThomas Huth {
803fcf5ef2aSThomas Huth     return 0;
804fcf5ef2aSThomas Huth }
805fcf5ef2aSThomas Huth 
806fcf5ef2aSThomas Huth int kvm_arch_release_virq_post(int virq)
807fcf5ef2aSThomas Huth {
808fcf5ef2aSThomas Huth     return 0;
809fcf5ef2aSThomas Huth }
810fcf5ef2aSThomas Huth 
811fcf5ef2aSThomas Huth int kvm_arch_msi_data_to_gsi(uint32_t data)
812fcf5ef2aSThomas Huth {
813fcf5ef2aSThomas Huth     return (data - 32) & 0xffff;
814fcf5ef2aSThomas Huth }
815