]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm/kvm/arm.c
Merge tag 'v3.9-rc5' into kvm-arm-next
[karo-tx-linux.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/errno.h>
20 #include <linux/err.h>
21 #include <linux/kvm_host.h>
22 #include <linux/module.h>
23 #include <linux/vmalloc.h>
24 #include <linux/fs.h>
25 #include <linux/mman.h>
26 #include <linux/sched.h>
27 #include <linux/kvm.h>
28 #include <trace/events/kvm.h>
29
30 #define CREATE_TRACE_POINTS
31 #include "trace.h"
32
33 #include <asm/uaccess.h>
34 #include <asm/ptrace.h>
35 #include <asm/mman.h>
36 #include <asm/tlbflush.h>
37 #include <asm/cacheflush.h>
38 #include <asm/virt.h>
39 #include <asm/kvm_arm.h>
40 #include <asm/kvm_asm.h>
41 #include <asm/kvm_mmu.h>
42 #include <asm/kvm_emulate.h>
43 #include <asm/kvm_coproc.h>
44 #include <asm/kvm_psci.h>
45
46 #ifdef REQUIRES_VIRT
47 __asm__(".arch_extension        virt");
48 #endif
49
50 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
51 static kvm_kernel_vfp_t __percpu *kvm_host_vfp_state;
52 static unsigned long hyp_default_vectors;
53
54 /* Per-CPU variable containing the currently running vcpu. */
55 static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
56
57 /* The VMID used in the VTTBR */
58 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
59 static u8 kvm_next_vmid;
60 static DEFINE_SPINLOCK(kvm_vmid_lock);
61
62 static bool vgic_present;
63
64 static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
65 {
66         BUG_ON(preemptible());
67         __get_cpu_var(kvm_arm_running_vcpu) = vcpu;
68 }
69
70 /**
71  * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
72  * Must be called from non-preemptible context
73  */
74 struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
75 {
76         BUG_ON(preemptible());
77         return __get_cpu_var(kvm_arm_running_vcpu);
78 }
79
80 /**
81  * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
82  */
83 struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
84 {
85         return &kvm_arm_running_vcpu;
86 }
87
88 int kvm_arch_hardware_enable(void *garbage)
89 {
90         return 0;
91 }
92
93 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
94 {
95         return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
96 }
97
98 void kvm_arch_hardware_disable(void *garbage)
99 {
100 }
101
102 int kvm_arch_hardware_setup(void)
103 {
104         return 0;
105 }
106
107 void kvm_arch_hardware_unsetup(void)
108 {
109 }
110
111 void kvm_arch_check_processor_compat(void *rtn)
112 {
113         *(int *)rtn = 0;
114 }
115
116 void kvm_arch_sync_events(struct kvm *kvm)
117 {
118 }
119
120 /**
121  * kvm_arch_init_vm - initializes a VM data structure
122  * @kvm:        pointer to the KVM struct
123  */
124 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
125 {
126         int ret = 0;
127
128         if (type)
129                 return -EINVAL;
130
131         ret = kvm_alloc_stage2_pgd(kvm);
132         if (ret)
133                 goto out_fail_alloc;
134
135         ret = create_hyp_mappings(kvm, kvm + 1);
136         if (ret)
137                 goto out_free_stage2_pgd;
138
139         /* Mark the initial VMID generation invalid */
140         kvm->arch.vmid_gen = 0;
141
142         return ret;
143 out_free_stage2_pgd:
144         kvm_free_stage2_pgd(kvm);
145 out_fail_alloc:
146         return ret;
147 }
148
149 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
150 {
151         return VM_FAULT_SIGBUS;
152 }
153
154 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
155                            struct kvm_memory_slot *dont)
156 {
157 }
158
159 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
160 {
161         return 0;
162 }
163
164 /**
165  * kvm_arch_destroy_vm - destroy the VM data structure
166  * @kvm:        pointer to the KVM struct
167  */
168 void kvm_arch_destroy_vm(struct kvm *kvm)
169 {
170         int i;
171
172         kvm_free_stage2_pgd(kvm);
173
174         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
175                 if (kvm->vcpus[i]) {
176                         kvm_arch_vcpu_free(kvm->vcpus[i]);
177                         kvm->vcpus[i] = NULL;
178                 }
179         }
180 }
181
182 int kvm_dev_ioctl_check_extension(long ext)
183 {
184         int r;
185         switch (ext) {
186         case KVM_CAP_IRQCHIP:
187                 r = vgic_present;
188                 break;
189         case KVM_CAP_USER_MEMORY:
190         case KVM_CAP_SYNC_MMU:
191         case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
192         case KVM_CAP_ONE_REG:
193         case KVM_CAP_ARM_PSCI:
194                 r = 1;
195                 break;
196         case KVM_CAP_COALESCED_MMIO:
197                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
198                 break;
199         case KVM_CAP_ARM_SET_DEVICE_ADDR:
200                 r = 1;
201         case KVM_CAP_NR_VCPUS:
202                 r = num_online_cpus();
203                 break;
204         case KVM_CAP_MAX_VCPUS:
205                 r = KVM_MAX_VCPUS;
206                 break;
207         default:
208                 r = 0;
209                 break;
210         }
211         return r;
212 }
213
214 long kvm_arch_dev_ioctl(struct file *filp,
215                         unsigned int ioctl, unsigned long arg)
216 {
217         return -EINVAL;
218 }
219
220 int kvm_arch_set_memory_region(struct kvm *kvm,
221                                struct kvm_userspace_memory_region *mem,
222                                struct kvm_memory_slot old,
223                                int user_alloc)
224 {
225         return 0;
226 }
227
228 int kvm_arch_prepare_memory_region(struct kvm *kvm,
229                                    struct kvm_memory_slot *memslot,
230                                    struct kvm_memory_slot old,
231                                    struct kvm_userspace_memory_region *mem,
232                                    bool user_alloc)
233 {
234         return 0;
235 }
236
237 void kvm_arch_commit_memory_region(struct kvm *kvm,
238                                    struct kvm_userspace_memory_region *mem,
239                                    struct kvm_memory_slot old,
240                                    bool user_alloc)
241 {
242 }
243
244 void kvm_arch_flush_shadow_all(struct kvm *kvm)
245 {
246 }
247
248 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
249                                    struct kvm_memory_slot *slot)
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         vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
259         if (!vcpu) {
260                 err = -ENOMEM;
261                 goto out;
262         }
263
264         err = kvm_vcpu_init(vcpu, kvm, id);
265         if (err)
266                 goto free_vcpu;
267
268         err = create_hyp_mappings(vcpu, vcpu + 1);
269         if (err)
270                 goto vcpu_uninit;
271
272         return vcpu;
273 vcpu_uninit:
274         kvm_vcpu_uninit(vcpu);
275 free_vcpu:
276         kmem_cache_free(kvm_vcpu_cache, vcpu);
277 out:
278         return ERR_PTR(err);
279 }
280
281 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
282 {
283         return 0;
284 }
285
286 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
287 {
288         kvm_mmu_free_memory_caches(vcpu);
289         kvm_timer_vcpu_terminate(vcpu);
290         kmem_cache_free(kvm_vcpu_cache, vcpu);
291 }
292
293 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
294 {
295         kvm_arch_vcpu_free(vcpu);
296 }
297
298 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
299 {
300         return 0;
301 }
302
303 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
304 {
305         int ret;
306
307         /* Force users to call KVM_ARM_VCPU_INIT */
308         vcpu->arch.target = -1;
309
310         /* Set up VGIC */
311         ret = kvm_vgic_vcpu_init(vcpu);
312         if (ret)
313                 return ret;
314
315         /* Set up the timer */
316         kvm_timer_vcpu_init(vcpu);
317
318         return 0;
319 }
320
321 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
322 {
323 }
324
325 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
326 {
327         vcpu->cpu = cpu;
328         vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
329
330         /*
331          * Check whether this vcpu requires the cache to be flushed on
332          * this physical CPU. This is a consequence of doing dcache
333          * operations by set/way on this vcpu. We do it here to be in
334          * a non-preemptible section.
335          */
336         if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
337                 flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
338
339         kvm_arm_set_running_vcpu(vcpu);
340 }
341
342 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
343 {
344         kvm_arm_set_running_vcpu(NULL);
345 }
346
347 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
348                                         struct kvm_guest_debug *dbg)
349 {
350         return -EINVAL;
351 }
352
353
354 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
355                                     struct kvm_mp_state *mp_state)
356 {
357         return -EINVAL;
358 }
359
360 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
361                                     struct kvm_mp_state *mp_state)
362 {
363         return -EINVAL;
364 }
365
366 /**
367  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
368  * @v:          The VCPU pointer
369  *
370  * If the guest CPU is not waiting for interrupts or an interrupt line is
371  * asserted, the CPU is by definition runnable.
372  */
373 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
374 {
375         return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
376 }
377
378 /* Just ensure a guest exit from a particular CPU */
379 static void exit_vm_noop(void *info)
380 {
381 }
382
383 void force_vm_exit(const cpumask_t *mask)
384 {
385         smp_call_function_many(mask, exit_vm_noop, NULL, true);
386 }
387
388 /**
389  * need_new_vmid_gen - check that the VMID is still valid
390  * @kvm: The VM's VMID to checkt
391  *
392  * return true if there is a new generation of VMIDs being used
393  *
394  * The hardware supports only 256 values with the value zero reserved for the
395  * host, so we check if an assigned value belongs to a previous generation,
396  * which which requires us to assign a new value. If we're the first to use a
397  * VMID for the new generation, we must flush necessary caches and TLBs on all
398  * CPUs.
399  */
400 static bool need_new_vmid_gen(struct kvm *kvm)
401 {
402         return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
403 }
404
405 /**
406  * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
407  * @kvm The guest that we are about to run
408  *
409  * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
410  * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
411  * caches and TLBs.
412  */
413 static void update_vttbr(struct kvm *kvm)
414 {
415         phys_addr_t pgd_phys;
416         u64 vmid;
417
418         if (!need_new_vmid_gen(kvm))
419                 return;
420
421         spin_lock(&kvm_vmid_lock);
422
423         /*
424          * We need to re-check the vmid_gen here to ensure that if another vcpu
425          * already allocated a valid vmid for this vm, then this vcpu should
426          * use the same vmid.
427          */
428         if (!need_new_vmid_gen(kvm)) {
429                 spin_unlock(&kvm_vmid_lock);
430                 return;
431         }
432
433         /* First user of a new VMID generation? */
434         if (unlikely(kvm_next_vmid == 0)) {
435                 atomic64_inc(&kvm_vmid_gen);
436                 kvm_next_vmid = 1;
437
438                 /*
439                  * On SMP we know no other CPUs can use this CPU's or each
440                  * other's VMID after force_vm_exit returns since the
441                  * kvm_vmid_lock blocks them from reentry to the guest.
442                  */
443                 force_vm_exit(cpu_all_mask);
444                 /*
445                  * Now broadcast TLB + ICACHE invalidation over the inner
446                  * shareable domain to make sure all data structures are
447                  * clean.
448                  */
449                 kvm_call_hyp(__kvm_flush_vm_context);
450         }
451
452         kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
453         kvm->arch.vmid = kvm_next_vmid;
454         kvm_next_vmid++;
455
456         /* update vttbr to be used with the new vmid */
457         pgd_phys = virt_to_phys(kvm->arch.pgd);
458         vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
459         kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
460         kvm->arch.vttbr |= vmid;
461
462         spin_unlock(&kvm_vmid_lock);
463 }
464
465 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
466 {
467         if (likely(vcpu->arch.has_run_once))
468                 return 0;
469
470         vcpu->arch.has_run_once = true;
471
472         /*
473          * Initialize the VGIC before running a vcpu the first time on
474          * this VM.
475          */
476         if (irqchip_in_kernel(vcpu->kvm) &&
477             unlikely(!vgic_initialized(vcpu->kvm))) {
478                 int ret = kvm_vgic_init(vcpu->kvm);
479                 if (ret)
480                         return ret;
481         }
482
483         /*
484          * Handle the "start in power-off" case by calling into the
485          * PSCI code.
486          */
487         if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
488                 *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
489                 kvm_psci_call(vcpu);
490         }
491
492         return 0;
493 }
494
495 static void vcpu_pause(struct kvm_vcpu *vcpu)
496 {
497         wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
498
499         wait_event_interruptible(*wq, !vcpu->arch.pause);
500 }
501
502 /**
503  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
504  * @vcpu:       The VCPU pointer
505  * @run:        The kvm_run structure pointer used for userspace state exchange
506  *
507  * This function is called through the VCPU_RUN ioctl called from user space. It
508  * will execute VM code in a loop until the time slice for the process is used
509  * or some emulation is needed from user space in which case the function will
510  * return with return value 0 and with the kvm_run structure filled in with the
511  * required data for the requested emulation.
512  */
513 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
514 {
515         int ret;
516         sigset_t sigsaved;
517
518         /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
519         if (unlikely(vcpu->arch.target < 0))
520                 return -ENOEXEC;
521
522         ret = kvm_vcpu_first_run_init(vcpu);
523         if (ret)
524                 return ret;
525
526         if (run->exit_reason == KVM_EXIT_MMIO) {
527                 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
528                 if (ret)
529                         return ret;
530         }
531
532         if (vcpu->sigset_active)
533                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
534
535         ret = 1;
536         run->exit_reason = KVM_EXIT_UNKNOWN;
537         while (ret > 0) {
538                 /*
539                  * Check conditions before entering the guest
540                  */
541                 cond_resched();
542
543                 update_vttbr(vcpu->kvm);
544
545                 if (vcpu->arch.pause)
546                         vcpu_pause(vcpu);
547
548                 kvm_vgic_flush_hwstate(vcpu);
549                 kvm_timer_flush_hwstate(vcpu);
550
551                 local_irq_disable();
552
553                 /*
554                  * Re-check atomic conditions
555                  */
556                 if (signal_pending(current)) {
557                         ret = -EINTR;
558                         run->exit_reason = KVM_EXIT_INTR;
559                 }
560
561                 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
562                         local_irq_enable();
563                         kvm_timer_sync_hwstate(vcpu);
564                         kvm_vgic_sync_hwstate(vcpu);
565                         continue;
566                 }
567
568                 /**************************************************************
569                  * Enter the guest
570                  */
571                 trace_kvm_entry(*vcpu_pc(vcpu));
572                 kvm_guest_enter();
573                 vcpu->mode = IN_GUEST_MODE;
574
575                 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
576
577                 vcpu->mode = OUTSIDE_GUEST_MODE;
578                 vcpu->arch.last_pcpu = smp_processor_id();
579                 kvm_guest_exit();
580                 trace_kvm_exit(*vcpu_pc(vcpu));
581                 /*
582                  * We may have taken a host interrupt in HYP mode (ie
583                  * while executing the guest). This interrupt is still
584                  * pending, as we haven't serviced it yet!
585                  *
586                  * We're now back in SVC mode, with interrupts
587                  * disabled.  Enabling the interrupts now will have
588                  * the effect of taking the interrupt again, in SVC
589                  * mode this time.
590                  */
591                 local_irq_enable();
592
593                 /*
594                  * Back from guest
595                  *************************************************************/
596
597                 kvm_timer_sync_hwstate(vcpu);
598                 kvm_vgic_sync_hwstate(vcpu);
599
600                 ret = handle_exit(vcpu, run, ret);
601         }
602
603         if (vcpu->sigset_active)
604                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
605         return ret;
606 }
607
608 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
609 {
610         int bit_index;
611         bool set;
612         unsigned long *ptr;
613
614         if (number == KVM_ARM_IRQ_CPU_IRQ)
615                 bit_index = __ffs(HCR_VI);
616         else /* KVM_ARM_IRQ_CPU_FIQ */
617                 bit_index = __ffs(HCR_VF);
618
619         ptr = (unsigned long *)&vcpu->arch.irq_lines;
620         if (level)
621                 set = test_and_set_bit(bit_index, ptr);
622         else
623                 set = test_and_clear_bit(bit_index, ptr);
624
625         /*
626          * If we didn't change anything, no need to wake up or kick other CPUs
627          */
628         if (set == level)
629                 return 0;
630
631         /*
632          * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
633          * trigger a world-switch round on the running physical CPU to set the
634          * virtual IRQ/FIQ fields in the HCR appropriately.
635          */
636         kvm_vcpu_kick(vcpu);
637
638         return 0;
639 }
640
641 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
642 {
643         u32 irq = irq_level->irq;
644         unsigned int irq_type, vcpu_idx, irq_num;
645         int nrcpus = atomic_read(&kvm->online_vcpus);
646         struct kvm_vcpu *vcpu = NULL;
647         bool level = irq_level->level;
648
649         irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
650         vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
651         irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
652
653         trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
654
655         switch (irq_type) {
656         case KVM_ARM_IRQ_TYPE_CPU:
657                 if (irqchip_in_kernel(kvm))
658                         return -ENXIO;
659
660                 if (vcpu_idx >= nrcpus)
661                         return -EINVAL;
662
663                 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
664                 if (!vcpu)
665                         return -EINVAL;
666
667                 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
668                         return -EINVAL;
669
670                 return vcpu_interrupt_line(vcpu, irq_num, level);
671         case KVM_ARM_IRQ_TYPE_PPI:
672                 if (!irqchip_in_kernel(kvm))
673                         return -ENXIO;
674
675                 if (vcpu_idx >= nrcpus)
676                         return -EINVAL;
677
678                 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
679                 if (!vcpu)
680                         return -EINVAL;
681
682                 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
683                         return -EINVAL;
684
685                 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
686         case KVM_ARM_IRQ_TYPE_SPI:
687                 if (!irqchip_in_kernel(kvm))
688                         return -ENXIO;
689
690                 if (irq_num < VGIC_NR_PRIVATE_IRQS ||
691                     irq_num > KVM_ARM_IRQ_GIC_MAX)
692                         return -EINVAL;
693
694                 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
695         }
696
697         return -EINVAL;
698 }
699
700 long kvm_arch_vcpu_ioctl(struct file *filp,
701                          unsigned int ioctl, unsigned long arg)
702 {
703         struct kvm_vcpu *vcpu = filp->private_data;
704         void __user *argp = (void __user *)arg;
705
706         switch (ioctl) {
707         case KVM_ARM_VCPU_INIT: {
708                 struct kvm_vcpu_init init;
709
710                 if (copy_from_user(&init, argp, sizeof(init)))
711                         return -EFAULT;
712
713                 return kvm_vcpu_set_target(vcpu, &init);
714
715         }
716         case KVM_SET_ONE_REG:
717         case KVM_GET_ONE_REG: {
718                 struct kvm_one_reg reg;
719                 if (copy_from_user(&reg, argp, sizeof(reg)))
720                         return -EFAULT;
721                 if (ioctl == KVM_SET_ONE_REG)
722                         return kvm_arm_set_reg(vcpu, &reg);
723                 else
724                         return kvm_arm_get_reg(vcpu, &reg);
725         }
726         case KVM_GET_REG_LIST: {
727                 struct kvm_reg_list __user *user_list = argp;
728                 struct kvm_reg_list reg_list;
729                 unsigned n;
730
731                 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
732                         return -EFAULT;
733                 n = reg_list.n;
734                 reg_list.n = kvm_arm_num_regs(vcpu);
735                 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
736                         return -EFAULT;
737                 if (n < reg_list.n)
738                         return -E2BIG;
739                 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
740         }
741         default:
742                 return -EINVAL;
743         }
744 }
745
746 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
747 {
748         return -EINVAL;
749 }
750
751 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
752                                         struct kvm_arm_device_addr *dev_addr)
753 {
754         unsigned long dev_id, type;
755
756         dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
757                 KVM_ARM_DEVICE_ID_SHIFT;
758         type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
759                 KVM_ARM_DEVICE_TYPE_SHIFT;
760
761         switch (dev_id) {
762         case KVM_ARM_DEVICE_VGIC_V2:
763                 if (!vgic_present)
764                         return -ENXIO;
765                 return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
766         default:
767                 return -ENODEV;
768         }
769 }
770
771 long kvm_arch_vm_ioctl(struct file *filp,
772                        unsigned int ioctl, unsigned long arg)
773 {
774         struct kvm *kvm = filp->private_data;
775         void __user *argp = (void __user *)arg;
776
777         switch (ioctl) {
778         case KVM_CREATE_IRQCHIP: {
779                 if (vgic_present)
780                         return kvm_vgic_create(kvm);
781                 else
782                         return -ENXIO;
783         }
784         case KVM_ARM_SET_DEVICE_ADDR: {
785                 struct kvm_arm_device_addr dev_addr;
786
787                 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
788                         return -EFAULT;
789                 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
790         }
791         default:
792                 return -EINVAL;
793         }
794 }
795
796 static void cpu_init_hyp_mode(void *vector)
797 {
798         unsigned long long pgd_ptr;
799         unsigned long hyp_stack_ptr;
800         unsigned long stack_page;
801         unsigned long vector_ptr;
802
803         /* Switch from the HYP stub to our own HYP init vector */
804         __hyp_set_vectors((unsigned long)vector);
805
806         pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
807         stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
808         hyp_stack_ptr = stack_page + PAGE_SIZE;
809         vector_ptr = (unsigned long)__kvm_hyp_vector;
810
811         __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
812 }
813
814 /**
815  * Inits Hyp-mode on all online CPUs
816  */
817 static int init_hyp_mode(void)
818 {
819         phys_addr_t init_phys_addr;
820         int cpu;
821         int err = 0;
822
823         /*
824          * Allocate Hyp PGD and setup Hyp identity mapping
825          */
826         err = kvm_mmu_init();
827         if (err)
828                 goto out_err;
829
830         /*
831          * It is probably enough to obtain the default on one
832          * CPU. It's unlikely to be different on the others.
833          */
834         hyp_default_vectors = __hyp_get_vectors();
835
836         /*
837          * Allocate stack pages for Hypervisor-mode
838          */
839         for_each_possible_cpu(cpu) {
840                 unsigned long stack_page;
841
842                 stack_page = __get_free_page(GFP_KERNEL);
843                 if (!stack_page) {
844                         err = -ENOMEM;
845                         goto out_free_stack_pages;
846                 }
847
848                 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
849         }
850
851         /*
852          * Execute the init code on each CPU.
853          *
854          * Note: The stack is not mapped yet, so don't do anything else than
855          * initializing the hypervisor mode on each CPU using a local stack
856          * space for temporary storage.
857          */
858         init_phys_addr = virt_to_phys(__kvm_hyp_init);
859         for_each_online_cpu(cpu) {
860                 smp_call_function_single(cpu, cpu_init_hyp_mode,
861                                          (void *)(long)init_phys_addr, 1);
862         }
863
864         /*
865          * Unmap the identity mapping
866          */
867         kvm_clear_hyp_idmap();
868
869         /*
870          * Map the Hyp-code called directly from the host
871          */
872         err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
873         if (err) {
874                 kvm_err("Cannot map world-switch code\n");
875                 goto out_free_mappings;
876         }
877
878         /*
879          * Map the Hyp stack pages
880          */
881         for_each_possible_cpu(cpu) {
882                 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
883                 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
884
885                 if (err) {
886                         kvm_err("Cannot map hyp stack\n");
887                         goto out_free_mappings;
888                 }
889         }
890
891         /*
892          * Map the host VFP structures
893          */
894         kvm_host_vfp_state = alloc_percpu(kvm_kernel_vfp_t);
895         if (!kvm_host_vfp_state) {
896                 err = -ENOMEM;
897                 kvm_err("Cannot allocate host VFP state\n");
898                 goto out_free_mappings;
899         }
900
901         for_each_possible_cpu(cpu) {
902                 kvm_kernel_vfp_t *vfp;
903
904                 vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
905                 err = create_hyp_mappings(vfp, vfp + 1);
906
907                 if (err) {
908                         kvm_err("Cannot map host VFP state: %d\n", err);
909                         goto out_free_vfp;
910                 }
911         }
912
913         /*
914          * Init HYP view of VGIC
915          */
916         err = kvm_vgic_hyp_init();
917         if (err)
918                 goto out_free_vfp;
919
920 #ifdef CONFIG_KVM_ARM_VGIC
921                 vgic_present = true;
922 #endif
923
924         /*
925          * Init HYP architected timer support
926          */
927         err = kvm_timer_hyp_init();
928         if (err)
929                 goto out_free_mappings;
930
931         kvm_perf_init();
932
933         kvm_info("Hyp mode initialized successfully\n");
934
935         return 0;
936 out_free_vfp:
937         free_percpu(kvm_host_vfp_state);
938 out_free_mappings:
939         free_hyp_pmds();
940 out_free_stack_pages:
941         for_each_possible_cpu(cpu)
942                 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
943 out_err:
944         kvm_err("error initializing Hyp mode: %d\n", err);
945         return err;
946 }
947
948 /**
949  * Initialize Hyp-mode and memory mappings on all CPUs.
950  */
951 int kvm_arch_init(void *opaque)
952 {
953         int err;
954
955         if (!is_hyp_mode_available()) {
956                 kvm_err("HYP mode not available\n");
957                 return -ENODEV;
958         }
959
960         if (kvm_target_cpu() < 0) {
961                 kvm_err("Target CPU not supported!\n");
962                 return -ENODEV;
963         }
964
965         err = init_hyp_mode();
966         if (err)
967                 goto out_err;
968
969         kvm_coproc_table_init();
970         return 0;
971 out_err:
972         return err;
973 }
974
975 /* NOP: Compiling as a module not supported */
976 void kvm_arch_exit(void)
977 {
978         kvm_perf_teardown();
979 }
980
981 static int arm_init(void)
982 {
983         int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
984         return rc;
985 }
986
987 module_init(arm_init);