KVM: ARM: Power State Coordination Interface implementation
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / arm / kvm / arm.c
1 /*
2  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License, version 2, as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <linux/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/unified.h>
34 #include <asm/uaccess.h>
35 #include <asm/ptrace.h>
36 #include <asm/mman.h>
37 #include <asm/cputype.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 #include <asm/opcodes.h>
48
49 #ifdef REQUIRES_VIRT
50 __asm__(".arch_extension        virt");
51 #endif
52
53 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
54 static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
55 static unsigned long hyp_default_vectors;
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 int kvm_arch_hardware_enable(void *garbage)
63 {
64         return 0;
65 }
66
67 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
68 {
69         return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
70 }
71
72 void kvm_arch_hardware_disable(void *garbage)
73 {
74 }
75
76 int kvm_arch_hardware_setup(void)
77 {
78         return 0;
79 }
80
81 void kvm_arch_hardware_unsetup(void)
82 {
83 }
84
85 void kvm_arch_check_processor_compat(void *rtn)
86 {
87         *(int *)rtn = 0;
88 }
89
90 void kvm_arch_sync_events(struct kvm *kvm)
91 {
92 }
93
94 /**
95  * kvm_arch_init_vm - initializes a VM data structure
96  * @kvm:        pointer to the KVM struct
97  */
98 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
99 {
100         int ret = 0;
101
102         if (type)
103                 return -EINVAL;
104
105         ret = kvm_alloc_stage2_pgd(kvm);
106         if (ret)
107                 goto out_fail_alloc;
108
109         ret = create_hyp_mappings(kvm, kvm + 1);
110         if (ret)
111                 goto out_free_stage2_pgd;
112
113         /* Mark the initial VMID generation invalid */
114         kvm->arch.vmid_gen = 0;
115
116         return ret;
117 out_free_stage2_pgd:
118         kvm_free_stage2_pgd(kvm);
119 out_fail_alloc:
120         return ret;
121 }
122
123 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
124 {
125         return VM_FAULT_SIGBUS;
126 }
127
128 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
129                            struct kvm_memory_slot *dont)
130 {
131 }
132
133 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
134 {
135         return 0;
136 }
137
138 /**
139  * kvm_arch_destroy_vm - destroy the VM data structure
140  * @kvm:        pointer to the KVM struct
141  */
142 void kvm_arch_destroy_vm(struct kvm *kvm)
143 {
144         int i;
145
146         kvm_free_stage2_pgd(kvm);
147
148         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
149                 if (kvm->vcpus[i]) {
150                         kvm_arch_vcpu_free(kvm->vcpus[i]);
151                         kvm->vcpus[i] = NULL;
152                 }
153         }
154 }
155
156 int kvm_dev_ioctl_check_extension(long ext)
157 {
158         int r;
159         switch (ext) {
160         case KVM_CAP_USER_MEMORY:
161         case KVM_CAP_SYNC_MMU:
162         case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
163         case KVM_CAP_ONE_REG:
164         case KVM_CAP_ARM_PSCI:
165                 r = 1;
166                 break;
167         case KVM_CAP_COALESCED_MMIO:
168                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
169                 break;
170         case KVM_CAP_NR_VCPUS:
171                 r = num_online_cpus();
172                 break;
173         case KVM_CAP_MAX_VCPUS:
174                 r = KVM_MAX_VCPUS;
175                 break;
176         default:
177                 r = 0;
178                 break;
179         }
180         return r;
181 }
182
183 long kvm_arch_dev_ioctl(struct file *filp,
184                         unsigned int ioctl, unsigned long arg)
185 {
186         return -EINVAL;
187 }
188
189 int kvm_arch_set_memory_region(struct kvm *kvm,
190                                struct kvm_userspace_memory_region *mem,
191                                struct kvm_memory_slot old,
192                                int user_alloc)
193 {
194         return 0;
195 }
196
197 int kvm_arch_prepare_memory_region(struct kvm *kvm,
198                                    struct kvm_memory_slot *memslot,
199                                    struct kvm_memory_slot old,
200                                    struct kvm_userspace_memory_region *mem,
201                                    int user_alloc)
202 {
203         return 0;
204 }
205
206 void kvm_arch_commit_memory_region(struct kvm *kvm,
207                                    struct kvm_userspace_memory_region *mem,
208                                    struct kvm_memory_slot old,
209                                    int user_alloc)
210 {
211 }
212
213 void kvm_arch_flush_shadow_all(struct kvm *kvm)
214 {
215 }
216
217 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
218                                    struct kvm_memory_slot *slot)
219 {
220 }
221
222 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
223 {
224         int err;
225         struct kvm_vcpu *vcpu;
226
227         vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
228         if (!vcpu) {
229                 err = -ENOMEM;
230                 goto out;
231         }
232
233         err = kvm_vcpu_init(vcpu, kvm, id);
234         if (err)
235                 goto free_vcpu;
236
237         err = create_hyp_mappings(vcpu, vcpu + 1);
238         if (err)
239                 goto vcpu_uninit;
240
241         return vcpu;
242 vcpu_uninit:
243         kvm_vcpu_uninit(vcpu);
244 free_vcpu:
245         kmem_cache_free(kvm_vcpu_cache, vcpu);
246 out:
247         return ERR_PTR(err);
248 }
249
250 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
251 {
252         return 0;
253 }
254
255 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
256 {
257         kvm_mmu_free_memory_caches(vcpu);
258         kmem_cache_free(kvm_vcpu_cache, vcpu);
259 }
260
261 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
262 {
263         kvm_arch_vcpu_free(vcpu);
264 }
265
266 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
267 {
268         return 0;
269 }
270
271 int __attribute_const__ kvm_target_cpu(void)
272 {
273         unsigned long implementor = read_cpuid_implementor();
274         unsigned long part_number = read_cpuid_part_number();
275
276         if (implementor != ARM_CPU_IMP_ARM)
277                 return -EINVAL;
278
279         switch (part_number) {
280         case ARM_CPU_PART_CORTEX_A15:
281                 return KVM_ARM_TARGET_CORTEX_A15;
282         default:
283                 return -EINVAL;
284         }
285 }
286
287 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
288 {
289         /* Force users to call KVM_ARM_VCPU_INIT */
290         vcpu->arch.target = -1;
291         return 0;
292 }
293
294 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
295 {
296 }
297
298 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
299 {
300         vcpu->cpu = cpu;
301         vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
302
303         /*
304          * Check whether this vcpu requires the cache to be flushed on
305          * this physical CPU. This is a consequence of doing dcache
306          * operations by set/way on this vcpu. We do it here to be in
307          * a non-preemptible section.
308          */
309         if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
310                 flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
311 }
312
313 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
314 {
315 }
316
317 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
318                                         struct kvm_guest_debug *dbg)
319 {
320         return -EINVAL;
321 }
322
323
324 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
325                                     struct kvm_mp_state *mp_state)
326 {
327         return -EINVAL;
328 }
329
330 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
331                                     struct kvm_mp_state *mp_state)
332 {
333         return -EINVAL;
334 }
335
336 /**
337  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
338  * @v:          The VCPU pointer
339  *
340  * If the guest CPU is not waiting for interrupts or an interrupt line is
341  * asserted, the CPU is by definition runnable.
342  */
343 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
344 {
345         return !!v->arch.irq_lines;
346 }
347
348 /* Just ensure a guest exit from a particular CPU */
349 static void exit_vm_noop(void *info)
350 {
351 }
352
353 void force_vm_exit(const cpumask_t *mask)
354 {
355         smp_call_function_many(mask, exit_vm_noop, NULL, true);
356 }
357
358 /**
359  * need_new_vmid_gen - check that the VMID is still valid
360  * @kvm: The VM's VMID to checkt
361  *
362  * return true if there is a new generation of VMIDs being used
363  *
364  * The hardware supports only 256 values with the value zero reserved for the
365  * host, so we check if an assigned value belongs to a previous generation,
366  * which which requires us to assign a new value. If we're the first to use a
367  * VMID for the new generation, we must flush necessary caches and TLBs on all
368  * CPUs.
369  */
370 static bool need_new_vmid_gen(struct kvm *kvm)
371 {
372         return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
373 }
374
375 /**
376  * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
377  * @kvm The guest that we are about to run
378  *
379  * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
380  * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
381  * caches and TLBs.
382  */
383 static void update_vttbr(struct kvm *kvm)
384 {
385         phys_addr_t pgd_phys;
386         u64 vmid;
387
388         if (!need_new_vmid_gen(kvm))
389                 return;
390
391         spin_lock(&kvm_vmid_lock);
392
393         /*
394          * We need to re-check the vmid_gen here to ensure that if another vcpu
395          * already allocated a valid vmid for this vm, then this vcpu should
396          * use the same vmid.
397          */
398         if (!need_new_vmid_gen(kvm)) {
399                 spin_unlock(&kvm_vmid_lock);
400                 return;
401         }
402
403         /* First user of a new VMID generation? */
404         if (unlikely(kvm_next_vmid == 0)) {
405                 atomic64_inc(&kvm_vmid_gen);
406                 kvm_next_vmid = 1;
407
408                 /*
409                  * On SMP we know no other CPUs can use this CPU's or each
410                  * other's VMID after force_vm_exit returns since the
411                  * kvm_vmid_lock blocks them from reentry to the guest.
412                  */
413                 force_vm_exit(cpu_all_mask);
414                 /*
415                  * Now broadcast TLB + ICACHE invalidation over the inner
416                  * shareable domain to make sure all data structures are
417                  * clean.
418                  */
419                 kvm_call_hyp(__kvm_flush_vm_context);
420         }
421
422         kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
423         kvm->arch.vmid = kvm_next_vmid;
424         kvm_next_vmid++;
425
426         /* update vttbr to be used with the new vmid */
427         pgd_phys = virt_to_phys(kvm->arch.pgd);
428         vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
429         kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
430         kvm->arch.vttbr |= vmid;
431
432         spin_unlock(&kvm_vmid_lock);
433 }
434
435 static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
436 {
437         /* SVC called from Hyp mode should never get here */
438         kvm_debug("SVC called from Hyp mode shouldn't go here\n");
439         BUG();
440         return -EINVAL; /* Squash warning */
441 }
442
443 static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)
444 {
445         trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0),
446                       vcpu->arch.hsr & HSR_HVC_IMM_MASK);
447
448         if (kvm_psci_call(vcpu))
449                 return 1;
450
451         kvm_inject_undefined(vcpu);
452         return 1;
453 }
454
455 static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run)
456 {
457         if (kvm_psci_call(vcpu))
458                 return 1;
459
460         kvm_inject_undefined(vcpu);
461         return 1;
462 }
463
464 static int handle_pabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
465 {
466         /* The hypervisor should never cause aborts */
467         kvm_err("Prefetch Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
468                 vcpu->arch.hxfar, vcpu->arch.hsr);
469         return -EFAULT;
470 }
471
472 static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
473 {
474         /* This is either an error in the ws. code or an external abort */
475         kvm_err("Data Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
476                 vcpu->arch.hxfar, vcpu->arch.hsr);
477         return -EFAULT;
478 }
479
480 typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *);
481 static exit_handle_fn arm_exit_handlers[] = {
482         [HSR_EC_WFI]            = kvm_handle_wfi,
483         [HSR_EC_CP15_32]        = kvm_handle_cp15_32,
484         [HSR_EC_CP15_64]        = kvm_handle_cp15_64,
485         [HSR_EC_CP14_MR]        = kvm_handle_cp14_access,
486         [HSR_EC_CP14_LS]        = kvm_handle_cp14_load_store,
487         [HSR_EC_CP14_64]        = kvm_handle_cp14_access,
488         [HSR_EC_CP_0_13]        = kvm_handle_cp_0_13_access,
489         [HSR_EC_CP10_ID]        = kvm_handle_cp10_id,
490         [HSR_EC_SVC_HYP]        = handle_svc_hyp,
491         [HSR_EC_HVC]            = handle_hvc,
492         [HSR_EC_SMC]            = handle_smc,
493         [HSR_EC_IABT]           = kvm_handle_guest_abort,
494         [HSR_EC_IABT_HYP]       = handle_pabt_hyp,
495         [HSR_EC_DABT]           = kvm_handle_guest_abort,
496         [HSR_EC_DABT_HYP]       = handle_dabt_hyp,
497 };
498
499 /*
500  * A conditional instruction is allowed to trap, even though it
501  * wouldn't be executed.  So let's re-implement the hardware, in
502  * software!
503  */
504 static bool kvm_condition_valid(struct kvm_vcpu *vcpu)
505 {
506         unsigned long cpsr, cond, insn;
507
508         /*
509          * Exception Code 0 can only happen if we set HCR.TGE to 1, to
510          * catch undefined instructions, and then we won't get past
511          * the arm_exit_handlers test anyway.
512          */
513         BUG_ON(((vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT) == 0);
514
515         /* Top two bits non-zero?  Unconditional. */
516         if (vcpu->arch.hsr >> 30)
517                 return true;
518
519         cpsr = *vcpu_cpsr(vcpu);
520
521         /* Is condition field valid? */
522         if ((vcpu->arch.hsr & HSR_CV) >> HSR_CV_SHIFT)
523                 cond = (vcpu->arch.hsr & HSR_COND) >> HSR_COND_SHIFT;
524         else {
525                 /* This can happen in Thumb mode: examine IT state. */
526                 unsigned long it;
527
528                 it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
529
530                 /* it == 0 => unconditional. */
531                 if (it == 0)
532                         return true;
533
534                 /* The cond for this insn works out as the top 4 bits. */
535                 cond = (it >> 4);
536         }
537
538         /* Shift makes it look like an ARM-mode instruction */
539         insn = cond << 28;
540         return arm_check_condition(insn, cpsr) != ARM_OPCODE_CONDTEST_FAIL;
541 }
542
543 /*
544  * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
545  * proper exit to QEMU.
546  */
547 static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
548                        int exception_index)
549 {
550         unsigned long hsr_ec;
551
552         switch (exception_index) {
553         case ARM_EXCEPTION_IRQ:
554                 return 1;
555         case ARM_EXCEPTION_UNDEFINED:
556                 kvm_err("Undefined exception in Hyp mode at: %#08x\n",
557                         vcpu->arch.hyp_pc);
558                 BUG();
559                 panic("KVM: Hypervisor undefined exception!\n");
560         case ARM_EXCEPTION_DATA_ABORT:
561         case ARM_EXCEPTION_PREF_ABORT:
562         case ARM_EXCEPTION_HVC:
563                 hsr_ec = (vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT;
564
565                 if (hsr_ec >= ARRAY_SIZE(arm_exit_handlers)
566                     || !arm_exit_handlers[hsr_ec]) {
567                         kvm_err("Unkown exception class: %#08lx, "
568                                 "hsr: %#08x\n", hsr_ec,
569                                 (unsigned int)vcpu->arch.hsr);
570                         BUG();
571                 }
572
573                 /*
574                  * See ARM ARM B1.14.1: "Hyp traps on instructions
575                  * that fail their condition code check"
576                  */
577                 if (!kvm_condition_valid(vcpu)) {
578                         bool is_wide = vcpu->arch.hsr & HSR_IL;
579                         kvm_skip_instr(vcpu, is_wide);
580                         return 1;
581                 }
582
583                 return arm_exit_handlers[hsr_ec](vcpu, run);
584         default:
585                 kvm_pr_unimpl("Unsupported exception type: %d",
586                               exception_index);
587                 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
588                 return 0;
589         }
590 }
591
592 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
593 {
594         if (likely(vcpu->arch.has_run_once))
595                 return 0;
596
597         vcpu->arch.has_run_once = true;
598
599         /*
600          * Handle the "start in power-off" case by calling into the
601          * PSCI code.
602          */
603         if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) {
604                 *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF;
605                 kvm_psci_call(vcpu);
606         }
607
608         return 0;
609 }
610
611 static void vcpu_pause(struct kvm_vcpu *vcpu)
612 {
613         wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
614
615         wait_event_interruptible(*wq, !vcpu->arch.pause);
616 }
617
618 /**
619  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
620  * @vcpu:       The VCPU pointer
621  * @run:        The kvm_run structure pointer used for userspace state exchange
622  *
623  * This function is called through the VCPU_RUN ioctl called from user space. It
624  * will execute VM code in a loop until the time slice for the process is used
625  * or some emulation is needed from user space in which case the function will
626  * return with return value 0 and with the kvm_run structure filled in with the
627  * required data for the requested emulation.
628  */
629 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
630 {
631         int ret;
632         sigset_t sigsaved;
633
634         /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
635         if (unlikely(vcpu->arch.target < 0))
636                 return -ENOEXEC;
637
638         ret = kvm_vcpu_first_run_init(vcpu);
639         if (ret)
640                 return ret;
641
642         if (run->exit_reason == KVM_EXIT_MMIO) {
643                 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
644                 if (ret)
645                         return ret;
646         }
647
648         if (vcpu->sigset_active)
649                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
650
651         ret = 1;
652         run->exit_reason = KVM_EXIT_UNKNOWN;
653         while (ret > 0) {
654                 /*
655                  * Check conditions before entering the guest
656                  */
657                 cond_resched();
658
659                 update_vttbr(vcpu->kvm);
660
661                 if (vcpu->arch.pause)
662                         vcpu_pause(vcpu);
663
664                 local_irq_disable();
665
666                 /*
667                  * Re-check atomic conditions
668                  */
669                 if (signal_pending(current)) {
670                         ret = -EINTR;
671                         run->exit_reason = KVM_EXIT_INTR;
672                 }
673
674                 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
675                         local_irq_enable();
676                         continue;
677                 }
678
679                 /**************************************************************
680                  * Enter the guest
681                  */
682                 trace_kvm_entry(*vcpu_pc(vcpu));
683                 kvm_guest_enter();
684                 vcpu->mode = IN_GUEST_MODE;
685
686                 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
687
688                 vcpu->mode = OUTSIDE_GUEST_MODE;
689                 vcpu->arch.last_pcpu = smp_processor_id();
690                 kvm_guest_exit();
691                 trace_kvm_exit(*vcpu_pc(vcpu));
692                 /*
693                  * We may have taken a host interrupt in HYP mode (ie
694                  * while executing the guest). This interrupt is still
695                  * pending, as we haven't serviced it yet!
696                  *
697                  * We're now back in SVC mode, with interrupts
698                  * disabled.  Enabling the interrupts now will have
699                  * the effect of taking the interrupt again, in SVC
700                  * mode this time.
701                  */
702                 local_irq_enable();
703
704                 /*
705                  * Back from guest
706                  *************************************************************/
707
708                 ret = handle_exit(vcpu, run, ret);
709         }
710
711         if (vcpu->sigset_active)
712                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
713         return ret;
714 }
715
716 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
717 {
718         int bit_index;
719         bool set;
720         unsigned long *ptr;
721
722         if (number == KVM_ARM_IRQ_CPU_IRQ)
723                 bit_index = __ffs(HCR_VI);
724         else /* KVM_ARM_IRQ_CPU_FIQ */
725                 bit_index = __ffs(HCR_VF);
726
727         ptr = (unsigned long *)&vcpu->arch.irq_lines;
728         if (level)
729                 set = test_and_set_bit(bit_index, ptr);
730         else
731                 set = test_and_clear_bit(bit_index, ptr);
732
733         /*
734          * If we didn't change anything, no need to wake up or kick other CPUs
735          */
736         if (set == level)
737                 return 0;
738
739         /*
740          * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
741          * trigger a world-switch round on the running physical CPU to set the
742          * virtual IRQ/FIQ fields in the HCR appropriately.
743          */
744         kvm_vcpu_kick(vcpu);
745
746         return 0;
747 }
748
749 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
750 {
751         u32 irq = irq_level->irq;
752         unsigned int irq_type, vcpu_idx, irq_num;
753         int nrcpus = atomic_read(&kvm->online_vcpus);
754         struct kvm_vcpu *vcpu = NULL;
755         bool level = irq_level->level;
756
757         irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
758         vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
759         irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
760
761         trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
762
763         if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
764                 return -EINVAL;
765
766         if (vcpu_idx >= nrcpus)
767                 return -EINVAL;
768
769         vcpu = kvm_get_vcpu(kvm, vcpu_idx);
770         if (!vcpu)
771                 return -EINVAL;
772
773         if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
774                 return -EINVAL;
775
776         return vcpu_interrupt_line(vcpu, irq_num, level);
777 }
778
779 long kvm_arch_vcpu_ioctl(struct file *filp,
780                          unsigned int ioctl, unsigned long arg)
781 {
782         struct kvm_vcpu *vcpu = filp->private_data;
783         void __user *argp = (void __user *)arg;
784
785         switch (ioctl) {
786         case KVM_ARM_VCPU_INIT: {
787                 struct kvm_vcpu_init init;
788
789                 if (copy_from_user(&init, argp, sizeof(init)))
790                         return -EFAULT;
791
792                 return kvm_vcpu_set_target(vcpu, &init);
793
794         }
795         case KVM_SET_ONE_REG:
796         case KVM_GET_ONE_REG: {
797                 struct kvm_one_reg reg;
798                 if (copy_from_user(&reg, argp, sizeof(reg)))
799                         return -EFAULT;
800                 if (ioctl == KVM_SET_ONE_REG)
801                         return kvm_arm_set_reg(vcpu, &reg);
802                 else
803                         return kvm_arm_get_reg(vcpu, &reg);
804         }
805         case KVM_GET_REG_LIST: {
806                 struct kvm_reg_list __user *user_list = argp;
807                 struct kvm_reg_list reg_list;
808                 unsigned n;
809
810                 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
811                         return -EFAULT;
812                 n = reg_list.n;
813                 reg_list.n = kvm_arm_num_regs(vcpu);
814                 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
815                         return -EFAULT;
816                 if (n < reg_list.n)
817                         return -E2BIG;
818                 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
819         }
820         default:
821                 return -EINVAL;
822         }
823 }
824
825 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
826 {
827         return -EINVAL;
828 }
829
830 long kvm_arch_vm_ioctl(struct file *filp,
831                        unsigned int ioctl, unsigned long arg)
832 {
833         return -EINVAL;
834 }
835
836 static void cpu_init_hyp_mode(void *vector)
837 {
838         unsigned long long pgd_ptr;
839         unsigned long pgd_low, pgd_high;
840         unsigned long hyp_stack_ptr;
841         unsigned long stack_page;
842         unsigned long vector_ptr;
843
844         /* Switch from the HYP stub to our own HYP init vector */
845         __hyp_set_vectors((unsigned long)vector);
846
847         pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
848         pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
849         pgd_high = (pgd_ptr >> 32ULL);
850         stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
851         hyp_stack_ptr = stack_page + PAGE_SIZE;
852         vector_ptr = (unsigned long)__kvm_hyp_vector;
853
854         /*
855          * Call initialization code, and switch to the full blown
856          * HYP code. The init code doesn't need to preserve these registers as
857          * r1-r3 and r12 are already callee save according to the AAPCS.
858          * Note that we slightly misuse the prototype by casing the pgd_low to
859          * a void *.
860          */
861         kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
862 }
863
864 /**
865  * Inits Hyp-mode on all online CPUs
866  */
867 static int init_hyp_mode(void)
868 {
869         phys_addr_t init_phys_addr;
870         int cpu;
871         int err = 0;
872
873         /*
874          * Allocate Hyp PGD and setup Hyp identity mapping
875          */
876         err = kvm_mmu_init();
877         if (err)
878                 goto out_err;
879
880         /*
881          * It is probably enough to obtain the default on one
882          * CPU. It's unlikely to be different on the others.
883          */
884         hyp_default_vectors = __hyp_get_vectors();
885
886         /*
887          * Allocate stack pages for Hypervisor-mode
888          */
889         for_each_possible_cpu(cpu) {
890                 unsigned long stack_page;
891
892                 stack_page = __get_free_page(GFP_KERNEL);
893                 if (!stack_page) {
894                         err = -ENOMEM;
895                         goto out_free_stack_pages;
896                 }
897
898                 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
899         }
900
901         /*
902          * Execute the init code on each CPU.
903          *
904          * Note: The stack is not mapped yet, so don't do anything else than
905          * initializing the hypervisor mode on each CPU using a local stack
906          * space for temporary storage.
907          */
908         init_phys_addr = virt_to_phys(__kvm_hyp_init);
909         for_each_online_cpu(cpu) {
910                 smp_call_function_single(cpu, cpu_init_hyp_mode,
911                                          (void *)(long)init_phys_addr, 1);
912         }
913
914         /*
915          * Unmap the identity mapping
916          */
917         kvm_clear_hyp_idmap();
918
919         /*
920          * Map the Hyp-code called directly from the host
921          */
922         err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
923         if (err) {
924                 kvm_err("Cannot map world-switch code\n");
925                 goto out_free_mappings;
926         }
927
928         /*
929          * Map the Hyp stack pages
930          */
931         for_each_possible_cpu(cpu) {
932                 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
933                 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
934
935                 if (err) {
936                         kvm_err("Cannot map hyp stack\n");
937                         goto out_free_mappings;
938                 }
939         }
940
941         /*
942          * Map the host VFP structures
943          */
944         kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
945         if (!kvm_host_vfp_state) {
946                 err = -ENOMEM;
947                 kvm_err("Cannot allocate host VFP state\n");
948                 goto out_free_mappings;
949         }
950
951         for_each_possible_cpu(cpu) {
952                 struct vfp_hard_struct *vfp;
953
954                 vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
955                 err = create_hyp_mappings(vfp, vfp + 1);
956
957                 if (err) {
958                         kvm_err("Cannot map host VFP state: %d\n", err);
959                         goto out_free_vfp;
960                 }
961         }
962
963         kvm_info("Hyp mode initialized successfully\n");
964         return 0;
965 out_free_vfp:
966         free_percpu(kvm_host_vfp_state);
967 out_free_mappings:
968         free_hyp_pmds();
969 out_free_stack_pages:
970         for_each_possible_cpu(cpu)
971                 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
972 out_err:
973         kvm_err("error initializing Hyp mode: %d\n", err);
974         return err;
975 }
976
977 /**
978  * Initialize Hyp-mode and memory mappings on all CPUs.
979  */
980 int kvm_arch_init(void *opaque)
981 {
982         int err;
983
984         if (!is_hyp_mode_available()) {
985                 kvm_err("HYP mode not available\n");
986                 return -ENODEV;
987         }
988
989         if (kvm_target_cpu() < 0) {
990                 kvm_err("Target CPU not supported!\n");
991                 return -ENODEV;
992         }
993
994         err = init_hyp_mode();
995         if (err)
996                 goto out_err;
997
998         kvm_coproc_table_init();
999         return 0;
1000 out_err:
1001         return err;
1002 }
1003
1004 /* NOP: Compiling as a module not supported */
1005 void kvm_arch_exit(void)
1006 {
1007 }
1008
1009 static int arm_init(void)
1010 {
1011         int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1012         return rc;
1013 }
1014
1015 module_init(arm_init);