]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/arm/kvm/arm.c
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi into...
[mirror_ubuntu-artful-kernel.git] / arch / arm / kvm / arm.c
1 /*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <linux/cpu_pm.h>
20 #include <linux/errno.h>
21 #include <linux/err.h>
22 #include <linux/kvm_host.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/vmalloc.h>
26 #include <linux/fs.h>
27 #include <linux/mman.h>
28 #include <linux/sched.h>
29 #include <linux/kvm.h>
30 #include <trace/events/kvm.h>
31 #include <kvm/arm_pmu.h>
32
33 #define CREATE_TRACE_POINTS
34 #include "trace.h"
35
36 #include <linux/uaccess.h>
37 #include <asm/ptrace.h>
38 #include <asm/mman.h>
39 #include <asm/tlbflush.h>
40 #include <asm/cacheflush.h>
41 #include <asm/virt.h>
42 #include <asm/kvm_arm.h>
43 #include <asm/kvm_asm.h>
44 #include <asm/kvm_mmu.h>
45 #include <asm/kvm_emulate.h>
46 #include <asm/kvm_coproc.h>
47 #include <asm/kvm_psci.h>
48 #include <asm/sections.h>
49
50 #ifdef REQUIRES_VIRT
51 __asm__(".arch_extension virt");
52 #endif
53
54 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
55 static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
56 static unsigned long hyp_default_vectors;
57
58 /* Per-CPU variable containing the currently running vcpu. */
59 static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
60
61 /* The VMID used in the VTTBR */
62 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
63 static u32 kvm_next_vmid;
64 static unsigned int kvm_vmid_bits __read_mostly;
65 static DEFINE_SPINLOCK(kvm_vmid_lock);
66
67 static bool vgic_present;
68
69 static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
70
71 static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
72 {
73 BUG_ON(preemptible());
74 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
75 }
76
77 /**
78 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
79 * Must be called from non-preemptible context
80 */
81 struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
82 {
83 BUG_ON(preemptible());
84 return __this_cpu_read(kvm_arm_running_vcpu);
85 }
86
87 /**
88 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
89 */
90 struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
91 {
92 return &kvm_arm_running_vcpu;
93 }
94
95 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
96 {
97 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
98 }
99
100 int kvm_arch_hardware_setup(void)
101 {
102 return 0;
103 }
104
105 void kvm_arch_check_processor_compat(void *rtn)
106 {
107 *(int *)rtn = 0;
108 }
109
110
111 /**
112 * kvm_arch_init_vm - initializes a VM data structure
113 * @kvm: pointer to the KVM struct
114 */
115 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
116 {
117 int ret, cpu;
118
119 if (type)
120 return -EINVAL;
121
122 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
123 if (!kvm->arch.last_vcpu_ran)
124 return -ENOMEM;
125
126 for_each_possible_cpu(cpu)
127 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
128
129 ret = kvm_alloc_stage2_pgd(kvm);
130 if (ret)
131 goto out_fail_alloc;
132
133 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
134 if (ret)
135 goto out_free_stage2_pgd;
136
137 kvm_vgic_early_init(kvm);
138
139 /* Mark the initial VMID generation invalid */
140 kvm->arch.vmid_gen = 0;
141
142 /* The maximum number of VCPUs is limited by the host's GIC model */
143 kvm->arch.max_vcpus = vgic_present ?
144 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
145
146 return ret;
147 out_free_stage2_pgd:
148 kvm_free_stage2_pgd(kvm);
149 out_fail_alloc:
150 free_percpu(kvm->arch.last_vcpu_ran);
151 kvm->arch.last_vcpu_ran = NULL;
152 return ret;
153 }
154
155 bool kvm_arch_has_vcpu_debugfs(void)
156 {
157 return false;
158 }
159
160 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
161 {
162 return 0;
163 }
164
165 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
166 {
167 return VM_FAULT_SIGBUS;
168 }
169
170
171 /**
172 * kvm_arch_destroy_vm - destroy the VM data structure
173 * @kvm: pointer to the KVM struct
174 */
175 void kvm_arch_destroy_vm(struct kvm *kvm)
176 {
177 int i;
178
179 free_percpu(kvm->arch.last_vcpu_ran);
180 kvm->arch.last_vcpu_ran = NULL;
181
182 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
183 if (kvm->vcpus[i]) {
184 kvm_arch_vcpu_free(kvm->vcpus[i]);
185 kvm->vcpus[i] = NULL;
186 }
187 }
188
189 kvm_vgic_destroy(kvm);
190 }
191
192 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
193 {
194 int r;
195 switch (ext) {
196 case KVM_CAP_IRQCHIP:
197 r = vgic_present;
198 break;
199 case KVM_CAP_IOEVENTFD:
200 case KVM_CAP_DEVICE_CTRL:
201 case KVM_CAP_USER_MEMORY:
202 case KVM_CAP_SYNC_MMU:
203 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
204 case KVM_CAP_ONE_REG:
205 case KVM_CAP_ARM_PSCI:
206 case KVM_CAP_ARM_PSCI_0_2:
207 case KVM_CAP_READONLY_MEM:
208 case KVM_CAP_MP_STATE:
209 case KVM_CAP_IMMEDIATE_EXIT:
210 r = 1;
211 break;
212 case KVM_CAP_COALESCED_MMIO:
213 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
214 break;
215 case KVM_CAP_ARM_SET_DEVICE_ADDR:
216 r = 1;
217 break;
218 case KVM_CAP_NR_VCPUS:
219 r = num_online_cpus();
220 break;
221 case KVM_CAP_MAX_VCPUS:
222 r = KVM_MAX_VCPUS;
223 break;
224 case KVM_CAP_NR_MEMSLOTS:
225 r = KVM_USER_MEM_SLOTS;
226 break;
227 case KVM_CAP_MSI_DEVID:
228 if (!kvm)
229 r = -EINVAL;
230 else
231 r = kvm->arch.vgic.msis_require_devid;
232 break;
233 default:
234 r = kvm_arch_dev_ioctl_check_extension(kvm, ext);
235 break;
236 }
237 return r;
238 }
239
240 long kvm_arch_dev_ioctl(struct file *filp,
241 unsigned int ioctl, unsigned long arg)
242 {
243 return -EINVAL;
244 }
245
246
247 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
248 {
249 int err;
250 struct kvm_vcpu *vcpu;
251
252 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
253 err = -EBUSY;
254 goto out;
255 }
256
257 if (id >= kvm->arch.max_vcpus) {
258 err = -EINVAL;
259 goto out;
260 }
261
262 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
263 if (!vcpu) {
264 err = -ENOMEM;
265 goto out;
266 }
267
268 err = kvm_vcpu_init(vcpu, kvm, id);
269 if (err)
270 goto free_vcpu;
271
272 err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
273 if (err)
274 goto vcpu_uninit;
275
276 return vcpu;
277 vcpu_uninit:
278 kvm_vcpu_uninit(vcpu);
279 free_vcpu:
280 kmem_cache_free(kvm_vcpu_cache, vcpu);
281 out:
282 return ERR_PTR(err);
283 }
284
285 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
286 {
287 kvm_vgic_vcpu_early_init(vcpu);
288 }
289
290 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
291 {
292 kvm_mmu_free_memory_caches(vcpu);
293 kvm_timer_vcpu_terminate(vcpu);
294 kvm_vgic_vcpu_destroy(vcpu);
295 kvm_pmu_vcpu_destroy(vcpu);
296 kvm_vcpu_uninit(vcpu);
297 kmem_cache_free(kvm_vcpu_cache, vcpu);
298 }
299
300 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
301 {
302 kvm_arch_vcpu_free(vcpu);
303 }
304
305 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
306 {
307 return kvm_timer_should_fire(vcpu_vtimer(vcpu)) ||
308 kvm_timer_should_fire(vcpu_ptimer(vcpu));
309 }
310
311 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
312 {
313 kvm_timer_schedule(vcpu);
314 }
315
316 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
317 {
318 kvm_timer_unschedule(vcpu);
319 }
320
321 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
322 {
323 /* Force users to call KVM_ARM_VCPU_INIT */
324 vcpu->arch.target = -1;
325 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
326
327 /* Set up the timer */
328 kvm_timer_vcpu_init(vcpu);
329
330 kvm_arm_reset_debug_ptr(vcpu);
331
332 return 0;
333 }
334
335 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
336 {
337 int *last_ran;
338
339 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
340
341 /*
342 * We might get preempted before the vCPU actually runs, but
343 * over-invalidation doesn't affect correctness.
344 */
345 if (*last_ran != vcpu->vcpu_id) {
346 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
347 *last_ran = vcpu->vcpu_id;
348 }
349
350 vcpu->cpu = cpu;
351 vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
352
353 kvm_arm_set_running_vcpu(vcpu);
354 }
355
356 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
357 {
358 /*
359 * The arch-generic KVM code expects the cpu field of a vcpu to be -1
360 * if the vcpu is no longer assigned to a cpu. This is used for the
361 * optimized make_all_cpus_request path.
362 */
363 vcpu->cpu = -1;
364
365 kvm_arm_set_running_vcpu(NULL);
366 kvm_timer_vcpu_put(vcpu);
367 }
368
369 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
370 struct kvm_mp_state *mp_state)
371 {
372 if (vcpu->arch.power_off)
373 mp_state->mp_state = KVM_MP_STATE_STOPPED;
374 else
375 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
376
377 return 0;
378 }
379
380 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
381 struct kvm_mp_state *mp_state)
382 {
383 switch (mp_state->mp_state) {
384 case KVM_MP_STATE_RUNNABLE:
385 vcpu->arch.power_off = false;
386 break;
387 case KVM_MP_STATE_STOPPED:
388 vcpu->arch.power_off = true;
389 break;
390 default:
391 return -EINVAL;
392 }
393
394 return 0;
395 }
396
397 /**
398 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
399 * @v: The VCPU pointer
400 *
401 * If the guest CPU is not waiting for interrupts or an interrupt line is
402 * asserted, the CPU is by definition runnable.
403 */
404 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
405 {
406 return ((!!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v))
407 && !v->arch.power_off && !v->arch.pause);
408 }
409
410 /* Just ensure a guest exit from a particular CPU */
411 static void exit_vm_noop(void *info)
412 {
413 }
414
415 void force_vm_exit(const cpumask_t *mask)
416 {
417 preempt_disable();
418 smp_call_function_many(mask, exit_vm_noop, NULL, true);
419 preempt_enable();
420 }
421
422 /**
423 * need_new_vmid_gen - check that the VMID is still valid
424 * @kvm: The VM's VMID to check
425 *
426 * return true if there is a new generation of VMIDs being used
427 *
428 * The hardware supports only 256 values with the value zero reserved for the
429 * host, so we check if an assigned value belongs to a previous generation,
430 * which which requires us to assign a new value. If we're the first to use a
431 * VMID for the new generation, we must flush necessary caches and TLBs on all
432 * CPUs.
433 */
434 static bool need_new_vmid_gen(struct kvm *kvm)
435 {
436 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
437 }
438
439 /**
440 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
441 * @kvm The guest that we are about to run
442 *
443 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
444 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
445 * caches and TLBs.
446 */
447 static void update_vttbr(struct kvm *kvm)
448 {
449 phys_addr_t pgd_phys;
450 u64 vmid;
451
452 if (!need_new_vmid_gen(kvm))
453 return;
454
455 spin_lock(&kvm_vmid_lock);
456
457 /*
458 * We need to re-check the vmid_gen here to ensure that if another vcpu
459 * already allocated a valid vmid for this vm, then this vcpu should
460 * use the same vmid.
461 */
462 if (!need_new_vmid_gen(kvm)) {
463 spin_unlock(&kvm_vmid_lock);
464 return;
465 }
466
467 /* First user of a new VMID generation? */
468 if (unlikely(kvm_next_vmid == 0)) {
469 atomic64_inc(&kvm_vmid_gen);
470 kvm_next_vmid = 1;
471
472 /*
473 * On SMP we know no other CPUs can use this CPU's or each
474 * other's VMID after force_vm_exit returns since the
475 * kvm_vmid_lock blocks them from reentry to the guest.
476 */
477 force_vm_exit(cpu_all_mask);
478 /*
479 * Now broadcast TLB + ICACHE invalidation over the inner
480 * shareable domain to make sure all data structures are
481 * clean.
482 */
483 kvm_call_hyp(__kvm_flush_vm_context);
484 }
485
486 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
487 kvm->arch.vmid = kvm_next_vmid;
488 kvm_next_vmid++;
489 kvm_next_vmid &= (1 << kvm_vmid_bits) - 1;
490
491 /* update vttbr to be used with the new vmid */
492 pgd_phys = virt_to_phys(kvm->arch.pgd);
493 BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
494 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits);
495 kvm->arch.vttbr = pgd_phys | vmid;
496
497 spin_unlock(&kvm_vmid_lock);
498 }
499
500 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
501 {
502 struct kvm *kvm = vcpu->kvm;
503 int ret = 0;
504
505 if (likely(vcpu->arch.has_run_once))
506 return 0;
507
508 vcpu->arch.has_run_once = true;
509
510 /*
511 * Map the VGIC hardware resources before running a vcpu the first
512 * time on this VM.
513 */
514 if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) {
515 ret = kvm_vgic_map_resources(kvm);
516 if (ret)
517 return ret;
518 }
519
520 /*
521 * Enable the arch timers only if we have an in-kernel VGIC
522 * and it has been properly initialized, since we cannot handle
523 * interrupts from the virtual timer with a userspace gic.
524 */
525 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
526 ret = kvm_timer_enable(vcpu);
527
528 return ret;
529 }
530
531 bool kvm_arch_intc_initialized(struct kvm *kvm)
532 {
533 return vgic_initialized(kvm);
534 }
535
536 void kvm_arm_halt_guest(struct kvm *kvm)
537 {
538 int i;
539 struct kvm_vcpu *vcpu;
540
541 kvm_for_each_vcpu(i, vcpu, kvm)
542 vcpu->arch.pause = true;
543 kvm_make_all_cpus_request(kvm, KVM_REQ_VCPU_EXIT);
544 }
545
546 void kvm_arm_halt_vcpu(struct kvm_vcpu *vcpu)
547 {
548 vcpu->arch.pause = true;
549 kvm_vcpu_kick(vcpu);
550 }
551
552 void kvm_arm_resume_vcpu(struct kvm_vcpu *vcpu)
553 {
554 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
555
556 vcpu->arch.pause = false;
557 swake_up(wq);
558 }
559
560 void kvm_arm_resume_guest(struct kvm *kvm)
561 {
562 int i;
563 struct kvm_vcpu *vcpu;
564
565 kvm_for_each_vcpu(i, vcpu, kvm)
566 kvm_arm_resume_vcpu(vcpu);
567 }
568
569 static void vcpu_sleep(struct kvm_vcpu *vcpu)
570 {
571 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
572
573 swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
574 (!vcpu->arch.pause)));
575 }
576
577 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
578 {
579 return vcpu->arch.target >= 0;
580 }
581
582 /**
583 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
584 * @vcpu: The VCPU pointer
585 * @run: The kvm_run structure pointer used for userspace state exchange
586 *
587 * This function is called through the VCPU_RUN ioctl called from user space. It
588 * will execute VM code in a loop until the time slice for the process is used
589 * or some emulation is needed from user space in which case the function will
590 * return with return value 0 and with the kvm_run structure filled in with the
591 * required data for the requested emulation.
592 */
593 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
594 {
595 int ret;
596 sigset_t sigsaved;
597
598 if (unlikely(!kvm_vcpu_initialized(vcpu)))
599 return -ENOEXEC;
600
601 ret = kvm_vcpu_first_run_init(vcpu);
602 if (ret)
603 return ret;
604
605 if (run->exit_reason == KVM_EXIT_MMIO) {
606 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
607 if (ret)
608 return ret;
609 }
610
611 if (run->immediate_exit)
612 return -EINTR;
613
614 if (vcpu->sigset_active)
615 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
616
617 ret = 1;
618 run->exit_reason = KVM_EXIT_UNKNOWN;
619 while (ret > 0) {
620 /*
621 * Check conditions before entering the guest
622 */
623 cond_resched();
624
625 update_vttbr(vcpu->kvm);
626
627 if (vcpu->arch.power_off || vcpu->arch.pause)
628 vcpu_sleep(vcpu);
629
630 /*
631 * Preparing the interrupts to be injected also
632 * involves poking the GIC, which must be done in a
633 * non-preemptible context.
634 */
635 preempt_disable();
636 kvm_pmu_flush_hwstate(vcpu);
637 kvm_timer_flush_hwstate(vcpu);
638 kvm_vgic_flush_hwstate(vcpu);
639
640 local_irq_disable();
641
642 /*
643 * Re-check atomic conditions
644 */
645 if (signal_pending(current)) {
646 ret = -EINTR;
647 run->exit_reason = KVM_EXIT_INTR;
648 }
649
650 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
651 vcpu->arch.power_off || vcpu->arch.pause) {
652 local_irq_enable();
653 kvm_pmu_sync_hwstate(vcpu);
654 kvm_timer_sync_hwstate(vcpu);
655 kvm_vgic_sync_hwstate(vcpu);
656 preempt_enable();
657 continue;
658 }
659
660 kvm_arm_setup_debug(vcpu);
661
662 /**************************************************************
663 * Enter the guest
664 */
665 trace_kvm_entry(*vcpu_pc(vcpu));
666 guest_enter_irqoff();
667 vcpu->mode = IN_GUEST_MODE;
668
669 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
670
671 vcpu->mode = OUTSIDE_GUEST_MODE;
672 vcpu->stat.exits++;
673 /*
674 * Back from guest
675 *************************************************************/
676
677 kvm_arm_clear_debug(vcpu);
678
679 /*
680 * We may have taken a host interrupt in HYP mode (ie
681 * while executing the guest). This interrupt is still
682 * pending, as we haven't serviced it yet!
683 *
684 * We're now back in SVC mode, with interrupts
685 * disabled. Enabling the interrupts now will have
686 * the effect of taking the interrupt again, in SVC
687 * mode this time.
688 */
689 local_irq_enable();
690
691 /*
692 * We do local_irq_enable() before calling guest_exit() so
693 * that if a timer interrupt hits while running the guest we
694 * account that tick as being spent in the guest. We enable
695 * preemption after calling guest_exit() so that if we get
696 * preempted we make sure ticks after that is not counted as
697 * guest time.
698 */
699 guest_exit();
700 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
701
702 /*
703 * We must sync the PMU and timer state before the vgic state so
704 * that the vgic can properly sample the updated state of the
705 * interrupt line.
706 */
707 kvm_pmu_sync_hwstate(vcpu);
708 kvm_timer_sync_hwstate(vcpu);
709
710 kvm_vgic_sync_hwstate(vcpu);
711
712 preempt_enable();
713
714 ret = handle_exit(vcpu, run, ret);
715 }
716
717 if (vcpu->sigset_active)
718 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
719 return ret;
720 }
721
722 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
723 {
724 int bit_index;
725 bool set;
726 unsigned long *ptr;
727
728 if (number == KVM_ARM_IRQ_CPU_IRQ)
729 bit_index = __ffs(HCR_VI);
730 else /* KVM_ARM_IRQ_CPU_FIQ */
731 bit_index = __ffs(HCR_VF);
732
733 ptr = (unsigned long *)&vcpu->arch.irq_lines;
734 if (level)
735 set = test_and_set_bit(bit_index, ptr);
736 else
737 set = test_and_clear_bit(bit_index, ptr);
738
739 /*
740 * If we didn't change anything, no need to wake up or kick other CPUs
741 */
742 if (set == level)
743 return 0;
744
745 /*
746 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
747 * trigger a world-switch round on the running physical CPU to set the
748 * virtual IRQ/FIQ fields in the HCR appropriately.
749 */
750 kvm_vcpu_kick(vcpu);
751
752 return 0;
753 }
754
755 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
756 bool line_status)
757 {
758 u32 irq = irq_level->irq;
759 unsigned int irq_type, vcpu_idx, irq_num;
760 int nrcpus = atomic_read(&kvm->online_vcpus);
761 struct kvm_vcpu *vcpu = NULL;
762 bool level = irq_level->level;
763
764 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
765 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
766 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
767
768 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
769
770 switch (irq_type) {
771 case KVM_ARM_IRQ_TYPE_CPU:
772 if (irqchip_in_kernel(kvm))
773 return -ENXIO;
774
775 if (vcpu_idx >= nrcpus)
776 return -EINVAL;
777
778 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
779 if (!vcpu)
780 return -EINVAL;
781
782 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
783 return -EINVAL;
784
785 return vcpu_interrupt_line(vcpu, irq_num, level);
786 case KVM_ARM_IRQ_TYPE_PPI:
787 if (!irqchip_in_kernel(kvm))
788 return -ENXIO;
789
790 if (vcpu_idx >= nrcpus)
791 return -EINVAL;
792
793 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
794 if (!vcpu)
795 return -EINVAL;
796
797 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
798 return -EINVAL;
799
800 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
801 case KVM_ARM_IRQ_TYPE_SPI:
802 if (!irqchip_in_kernel(kvm))
803 return -ENXIO;
804
805 if (irq_num < VGIC_NR_PRIVATE_IRQS)
806 return -EINVAL;
807
808 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
809 }
810
811 return -EINVAL;
812 }
813
814 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
815 const struct kvm_vcpu_init *init)
816 {
817 unsigned int i;
818 int phys_target = kvm_target_cpu();
819
820 if (init->target != phys_target)
821 return -EINVAL;
822
823 /*
824 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
825 * use the same target.
826 */
827 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
828 return -EINVAL;
829
830 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
831 for (i = 0; i < sizeof(init->features) * 8; i++) {
832 bool set = (init->features[i / 32] & (1 << (i % 32)));
833
834 if (set && i >= KVM_VCPU_MAX_FEATURES)
835 return -ENOENT;
836
837 /*
838 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
839 * use the same feature set.
840 */
841 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
842 test_bit(i, vcpu->arch.features) != set)
843 return -EINVAL;
844
845 if (set)
846 set_bit(i, vcpu->arch.features);
847 }
848
849 vcpu->arch.target = phys_target;
850
851 /* Now we know what it is, we can reset it. */
852 return kvm_reset_vcpu(vcpu);
853 }
854
855
856 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
857 struct kvm_vcpu_init *init)
858 {
859 int ret;
860
861 ret = kvm_vcpu_set_target(vcpu, init);
862 if (ret)
863 return ret;
864
865 /*
866 * Ensure a rebooted VM will fault in RAM pages and detect if the
867 * guest MMU is turned off and flush the caches as needed.
868 */
869 if (vcpu->arch.has_run_once)
870 stage2_unmap_vm(vcpu->kvm);
871
872 vcpu_reset_hcr(vcpu);
873
874 /*
875 * Handle the "start in power-off" case.
876 */
877 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
878 vcpu->arch.power_off = true;
879 else
880 vcpu->arch.power_off = false;
881
882 return 0;
883 }
884
885 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
886 struct kvm_device_attr *attr)
887 {
888 int ret = -ENXIO;
889
890 switch (attr->group) {
891 default:
892 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
893 break;
894 }
895
896 return ret;
897 }
898
899 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
900 struct kvm_device_attr *attr)
901 {
902 int ret = -ENXIO;
903
904 switch (attr->group) {
905 default:
906 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
907 break;
908 }
909
910 return ret;
911 }
912
913 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
914 struct kvm_device_attr *attr)
915 {
916 int ret = -ENXIO;
917
918 switch (attr->group) {
919 default:
920 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
921 break;
922 }
923
924 return ret;
925 }
926
927 long kvm_arch_vcpu_ioctl(struct file *filp,
928 unsigned int ioctl, unsigned long arg)
929 {
930 struct kvm_vcpu *vcpu = filp->private_data;
931 void __user *argp = (void __user *)arg;
932 struct kvm_device_attr attr;
933
934 switch (ioctl) {
935 case KVM_ARM_VCPU_INIT: {
936 struct kvm_vcpu_init init;
937
938 if (copy_from_user(&init, argp, sizeof(init)))
939 return -EFAULT;
940
941 return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
942 }
943 case KVM_SET_ONE_REG:
944 case KVM_GET_ONE_REG: {
945 struct kvm_one_reg reg;
946
947 if (unlikely(!kvm_vcpu_initialized(vcpu)))
948 return -ENOEXEC;
949
950 if (copy_from_user(&reg, argp, sizeof(reg)))
951 return -EFAULT;
952 if (ioctl == KVM_SET_ONE_REG)
953 return kvm_arm_set_reg(vcpu, &reg);
954 else
955 return kvm_arm_get_reg(vcpu, &reg);
956 }
957 case KVM_GET_REG_LIST: {
958 struct kvm_reg_list __user *user_list = argp;
959 struct kvm_reg_list reg_list;
960 unsigned n;
961
962 if (unlikely(!kvm_vcpu_initialized(vcpu)))
963 return -ENOEXEC;
964
965 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
966 return -EFAULT;
967 n = reg_list.n;
968 reg_list.n = kvm_arm_num_regs(vcpu);
969 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
970 return -EFAULT;
971 if (n < reg_list.n)
972 return -E2BIG;
973 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
974 }
975 case KVM_SET_DEVICE_ATTR: {
976 if (copy_from_user(&attr, argp, sizeof(attr)))
977 return -EFAULT;
978 return kvm_arm_vcpu_set_attr(vcpu, &attr);
979 }
980 case KVM_GET_DEVICE_ATTR: {
981 if (copy_from_user(&attr, argp, sizeof(attr)))
982 return -EFAULT;
983 return kvm_arm_vcpu_get_attr(vcpu, &attr);
984 }
985 case KVM_HAS_DEVICE_ATTR: {
986 if (copy_from_user(&attr, argp, sizeof(attr)))
987 return -EFAULT;
988 return kvm_arm_vcpu_has_attr(vcpu, &attr);
989 }
990 default:
991 return -EINVAL;
992 }
993 }
994
995 /**
996 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
997 * @kvm: kvm instance
998 * @log: slot id and address to which we copy the log
999 *
1000 * Steps 1-4 below provide general overview of dirty page logging. See
1001 * kvm_get_dirty_log_protect() function description for additional details.
1002 *
1003 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1004 * always flush the TLB (step 4) even if previous step failed and the dirty
1005 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1006 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1007 * writes will be marked dirty for next log read.
1008 *
1009 * 1. Take a snapshot of the bit and clear it if needed.
1010 * 2. Write protect the corresponding page.
1011 * 3. Copy the snapshot to the userspace.
1012 * 4. Flush TLB's if needed.
1013 */
1014 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1015 {
1016 bool is_dirty = false;
1017 int r;
1018
1019 mutex_lock(&kvm->slots_lock);
1020
1021 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
1022
1023 if (is_dirty)
1024 kvm_flush_remote_tlbs(kvm);
1025
1026 mutex_unlock(&kvm->slots_lock);
1027 return r;
1028 }
1029
1030 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1031 struct kvm_arm_device_addr *dev_addr)
1032 {
1033 unsigned long dev_id, type;
1034
1035 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1036 KVM_ARM_DEVICE_ID_SHIFT;
1037 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1038 KVM_ARM_DEVICE_TYPE_SHIFT;
1039
1040 switch (dev_id) {
1041 case KVM_ARM_DEVICE_VGIC_V2:
1042 if (!vgic_present)
1043 return -ENXIO;
1044 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
1045 default:
1046 return -ENODEV;
1047 }
1048 }
1049
1050 long kvm_arch_vm_ioctl(struct file *filp,
1051 unsigned int ioctl, unsigned long arg)
1052 {
1053 struct kvm *kvm = filp->private_data;
1054 void __user *argp = (void __user *)arg;
1055
1056 switch (ioctl) {
1057 case KVM_CREATE_IRQCHIP: {
1058 int ret;
1059 if (!vgic_present)
1060 return -ENXIO;
1061 mutex_lock(&kvm->lock);
1062 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1063 mutex_unlock(&kvm->lock);
1064 return ret;
1065 }
1066 case KVM_ARM_SET_DEVICE_ADDR: {
1067 struct kvm_arm_device_addr dev_addr;
1068
1069 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1070 return -EFAULT;
1071 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1072 }
1073 case KVM_ARM_PREFERRED_TARGET: {
1074 int err;
1075 struct kvm_vcpu_init init;
1076
1077 err = kvm_vcpu_preferred_target(&init);
1078 if (err)
1079 return err;
1080
1081 if (copy_to_user(argp, &init, sizeof(init)))
1082 return -EFAULT;
1083
1084 return 0;
1085 }
1086 default:
1087 return -EINVAL;
1088 }
1089 }
1090
1091 static void cpu_init_hyp_mode(void *dummy)
1092 {
1093 phys_addr_t pgd_ptr;
1094 unsigned long hyp_stack_ptr;
1095 unsigned long stack_page;
1096 unsigned long vector_ptr;
1097
1098 /* Switch from the HYP stub to our own HYP init vector */
1099 __hyp_set_vectors(kvm_get_idmap_vector());
1100
1101 pgd_ptr = kvm_mmu_get_httbr();
1102 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
1103 hyp_stack_ptr = stack_page + PAGE_SIZE;
1104 vector_ptr = (unsigned long)kvm_ksym_ref(__kvm_hyp_vector);
1105
1106 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
1107 __cpu_init_stage2();
1108
1109 if (is_kernel_in_hyp_mode())
1110 kvm_timer_init_vhe();
1111
1112 kvm_arm_init_debug();
1113 }
1114
1115 static void cpu_hyp_reinit(void)
1116 {
1117 if (is_kernel_in_hyp_mode()) {
1118 /*
1119 * __cpu_init_stage2() is safe to call even if the PM
1120 * event was cancelled before the CPU was reset.
1121 */
1122 __cpu_init_stage2();
1123 } else {
1124 if (__hyp_get_vectors() == hyp_default_vectors)
1125 cpu_init_hyp_mode(NULL);
1126 }
1127 }
1128
1129 static void cpu_hyp_reset(void)
1130 {
1131 if (!is_kernel_in_hyp_mode())
1132 __cpu_reset_hyp_mode(hyp_default_vectors,
1133 kvm_get_idmap_start());
1134 }
1135
1136 static void _kvm_arch_hardware_enable(void *discard)
1137 {
1138 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1139 cpu_hyp_reinit();
1140 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1141 }
1142 }
1143
1144 int kvm_arch_hardware_enable(void)
1145 {
1146 _kvm_arch_hardware_enable(NULL);
1147 return 0;
1148 }
1149
1150 static void _kvm_arch_hardware_disable(void *discard)
1151 {
1152 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1153 cpu_hyp_reset();
1154 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1155 }
1156 }
1157
1158 void kvm_arch_hardware_disable(void)
1159 {
1160 _kvm_arch_hardware_disable(NULL);
1161 }
1162
1163 #ifdef CONFIG_CPU_PM
1164 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1165 unsigned long cmd,
1166 void *v)
1167 {
1168 /*
1169 * kvm_arm_hardware_enabled is left with its old value over
1170 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1171 * re-enable hyp.
1172 */
1173 switch (cmd) {
1174 case CPU_PM_ENTER:
1175 if (__this_cpu_read(kvm_arm_hardware_enabled))
1176 /*
1177 * don't update kvm_arm_hardware_enabled here
1178 * so that the hardware will be re-enabled
1179 * when we resume. See below.
1180 */
1181 cpu_hyp_reset();
1182
1183 return NOTIFY_OK;
1184 case CPU_PM_EXIT:
1185 if (__this_cpu_read(kvm_arm_hardware_enabled))
1186 /* The hardware was enabled before suspend. */
1187 cpu_hyp_reinit();
1188
1189 return NOTIFY_OK;
1190
1191 default:
1192 return NOTIFY_DONE;
1193 }
1194 }
1195
1196 static struct notifier_block hyp_init_cpu_pm_nb = {
1197 .notifier_call = hyp_init_cpu_pm_notifier,
1198 };
1199
1200 static void __init hyp_cpu_pm_init(void)
1201 {
1202 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1203 }
1204 static void __init hyp_cpu_pm_exit(void)
1205 {
1206 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1207 }
1208 #else
1209 static inline void hyp_cpu_pm_init(void)
1210 {
1211 }
1212 static inline void hyp_cpu_pm_exit(void)
1213 {
1214 }
1215 #endif
1216
1217 static void teardown_common_resources(void)
1218 {
1219 free_percpu(kvm_host_cpu_state);
1220 }
1221
1222 static int init_common_resources(void)
1223 {
1224 kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
1225 if (!kvm_host_cpu_state) {
1226 kvm_err("Cannot allocate host CPU state\n");
1227 return -ENOMEM;
1228 }
1229
1230 /* set size of VMID supported by CPU */
1231 kvm_vmid_bits = kvm_get_vmid_bits();
1232 kvm_info("%d-bit VMID\n", kvm_vmid_bits);
1233
1234 return 0;
1235 }
1236
1237 static int init_subsystems(void)
1238 {
1239 int err = 0;
1240
1241 /*
1242 * Enable hardware so that subsystem initialisation can access EL2.
1243 */
1244 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
1245
1246 /*
1247 * Register CPU lower-power notifier
1248 */
1249 hyp_cpu_pm_init();
1250
1251 /*
1252 * Init HYP view of VGIC
1253 */
1254 err = kvm_vgic_hyp_init();
1255 switch (err) {
1256 case 0:
1257 vgic_present = true;
1258 break;
1259 case -ENODEV:
1260 case -ENXIO:
1261 vgic_present = false;
1262 err = 0;
1263 break;
1264 default:
1265 goto out;
1266 }
1267
1268 /*
1269 * Init HYP architected timer support
1270 */
1271 err = kvm_timer_hyp_init();
1272 if (err)
1273 goto out;
1274
1275 kvm_perf_init();
1276 kvm_coproc_table_init();
1277
1278 out:
1279 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1280
1281 return err;
1282 }
1283
1284 static void teardown_hyp_mode(void)
1285 {
1286 int cpu;
1287
1288 if (is_kernel_in_hyp_mode())
1289 return;
1290
1291 free_hyp_pgds();
1292 for_each_possible_cpu(cpu)
1293 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1294 hyp_cpu_pm_exit();
1295 }
1296
1297 static int init_vhe_mode(void)
1298 {
1299 kvm_info("VHE mode initialized successfully\n");
1300 return 0;
1301 }
1302
1303 /**
1304 * Inits Hyp-mode on all online CPUs
1305 */
1306 static int init_hyp_mode(void)
1307 {
1308 int cpu;
1309 int err = 0;
1310
1311 /*
1312 * Allocate Hyp PGD and setup Hyp identity mapping
1313 */
1314 err = kvm_mmu_init();
1315 if (err)
1316 goto out_err;
1317
1318 /*
1319 * It is probably enough to obtain the default on one
1320 * CPU. It's unlikely to be different on the others.
1321 */
1322 hyp_default_vectors = __hyp_get_vectors();
1323
1324 /*
1325 * Allocate stack pages for Hypervisor-mode
1326 */
1327 for_each_possible_cpu(cpu) {
1328 unsigned long stack_page;
1329
1330 stack_page = __get_free_page(GFP_KERNEL);
1331 if (!stack_page) {
1332 err = -ENOMEM;
1333 goto out_err;
1334 }
1335
1336 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1337 }
1338
1339 /*
1340 * Map the Hyp-code called directly from the host
1341 */
1342 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
1343 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
1344 if (err) {
1345 kvm_err("Cannot map world-switch code\n");
1346 goto out_err;
1347 }
1348
1349 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
1350 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
1351 if (err) {
1352 kvm_err("Cannot map rodata section\n");
1353 goto out_err;
1354 }
1355
1356 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1357 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1358 if (err) {
1359 kvm_err("Cannot map bss section\n");
1360 goto out_err;
1361 }
1362
1363 /*
1364 * Map the Hyp stack pages
1365 */
1366 for_each_possible_cpu(cpu) {
1367 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
1368 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1369 PAGE_HYP);
1370
1371 if (err) {
1372 kvm_err("Cannot map hyp stack\n");
1373 goto out_err;
1374 }
1375 }
1376
1377 for_each_possible_cpu(cpu) {
1378 kvm_cpu_context_t *cpu_ctxt;
1379
1380 cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
1381 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1, PAGE_HYP);
1382
1383 if (err) {
1384 kvm_err("Cannot map host CPU state: %d\n", err);
1385 goto out_err;
1386 }
1387 }
1388
1389 kvm_info("Hyp mode initialized successfully\n");
1390
1391 return 0;
1392
1393 out_err:
1394 teardown_hyp_mode();
1395 kvm_err("error initializing Hyp mode: %d\n", err);
1396 return err;
1397 }
1398
1399 static void check_kvm_target_cpu(void *ret)
1400 {
1401 *(int *)ret = kvm_target_cpu();
1402 }
1403
1404 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1405 {
1406 struct kvm_vcpu *vcpu;
1407 int i;
1408
1409 mpidr &= MPIDR_HWID_BITMASK;
1410 kvm_for_each_vcpu(i, vcpu, kvm) {
1411 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1412 return vcpu;
1413 }
1414 return NULL;
1415 }
1416
1417 /**
1418 * Initialize Hyp-mode and memory mappings on all CPUs.
1419 */
1420 int kvm_arch_init(void *opaque)
1421 {
1422 int err;
1423 int ret, cpu;
1424
1425 if (!is_hyp_mode_available()) {
1426 kvm_err("HYP mode not available\n");
1427 return -ENODEV;
1428 }
1429
1430 for_each_online_cpu(cpu) {
1431 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1432 if (ret < 0) {
1433 kvm_err("Error, CPU %d not supported!\n", cpu);
1434 return -ENODEV;
1435 }
1436 }
1437
1438 err = init_common_resources();
1439 if (err)
1440 return err;
1441
1442 if (is_kernel_in_hyp_mode())
1443 err = init_vhe_mode();
1444 else
1445 err = init_hyp_mode();
1446 if (err)
1447 goto out_err;
1448
1449 err = init_subsystems();
1450 if (err)
1451 goto out_hyp;
1452
1453 return 0;
1454
1455 out_hyp:
1456 teardown_hyp_mode();
1457 out_err:
1458 teardown_common_resources();
1459 return err;
1460 }
1461
1462 /* NOP: Compiling as a module not supported */
1463 void kvm_arch_exit(void)
1464 {
1465 kvm_perf_teardown();
1466 }
1467
1468 static int arm_init(void)
1469 {
1470 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1471 return rc;
1472 }
1473
1474 module_init(arm_init);