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