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