2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
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.
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.
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.
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>
25 #include <linux/mman.h>
26 #include <linux/sched.h>
27 #include <linux/kvm.h>
28 #include <trace/events/kvm.h>
30 #define CREATE_TRACE_POINTS
33 #include <asm/unified.h>
34 #include <asm/uaccess.h>
35 #include <asm/ptrace.h>
37 #include <asm/cputype.h>
38 #include <asm/tlbflush.h>
39 #include <asm/cacheflush.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/opcodes.h>
49 __asm__(".arch_extension virt");
52 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
53 static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
54 static unsigned long hyp_default_vectors;
56 /* The VMID used in the VTTBR */
57 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
58 static u8 kvm_next_vmid;
59 static DEFINE_SPINLOCK(kvm_vmid_lock);
61 int kvm_arch_hardware_enable(void *garbage)
66 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
68 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
71 void kvm_arch_hardware_disable(void *garbage)
75 int kvm_arch_hardware_setup(void)
80 void kvm_arch_hardware_unsetup(void)
84 void kvm_arch_check_processor_compat(void *rtn)
89 void kvm_arch_sync_events(struct kvm *kvm)
94 * kvm_arch_init_vm - initializes a VM data structure
95 * @kvm: pointer to the KVM struct
97 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
104 ret = kvm_alloc_stage2_pgd(kvm);
108 ret = create_hyp_mappings(kvm, kvm + 1);
110 goto out_free_stage2_pgd;
112 /* Mark the initial VMID generation invalid */
113 kvm->arch.vmid_gen = 0;
117 kvm_free_stage2_pgd(kvm);
122 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
124 return VM_FAULT_SIGBUS;
127 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
128 struct kvm_memory_slot *dont)
132 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
138 * kvm_arch_destroy_vm - destroy the VM data structure
139 * @kvm: pointer to the KVM struct
141 void kvm_arch_destroy_vm(struct kvm *kvm)
145 kvm_free_stage2_pgd(kvm);
147 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
149 kvm_arch_vcpu_free(kvm->vcpus[i]);
150 kvm->vcpus[i] = NULL;
155 int kvm_dev_ioctl_check_extension(long ext)
159 case KVM_CAP_USER_MEMORY:
160 case KVM_CAP_SYNC_MMU:
161 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
162 case KVM_CAP_ONE_REG:
165 case KVM_CAP_COALESCED_MMIO:
166 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
168 case KVM_CAP_NR_VCPUS:
169 r = num_online_cpus();
171 case KVM_CAP_MAX_VCPUS:
181 long kvm_arch_dev_ioctl(struct file *filp,
182 unsigned int ioctl, unsigned long arg)
187 int kvm_arch_set_memory_region(struct kvm *kvm,
188 struct kvm_userspace_memory_region *mem,
189 struct kvm_memory_slot old,
195 int kvm_arch_prepare_memory_region(struct kvm *kvm,
196 struct kvm_memory_slot *memslot,
197 struct kvm_memory_slot old,
198 struct kvm_userspace_memory_region *mem,
204 void kvm_arch_commit_memory_region(struct kvm *kvm,
205 struct kvm_userspace_memory_region *mem,
206 struct kvm_memory_slot old,
211 void kvm_arch_flush_shadow_all(struct kvm *kvm)
215 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
216 struct kvm_memory_slot *slot)
220 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
223 struct kvm_vcpu *vcpu;
225 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
231 err = kvm_vcpu_init(vcpu, kvm, id);
235 err = create_hyp_mappings(vcpu, vcpu + 1);
241 kvm_vcpu_uninit(vcpu);
243 kmem_cache_free(kvm_vcpu_cache, vcpu);
248 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
253 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
255 kvm_mmu_free_memory_caches(vcpu);
256 kmem_cache_free(kvm_vcpu_cache, vcpu);
259 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
261 kvm_arch_vcpu_free(vcpu);
264 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
269 int __attribute_const__ kvm_target_cpu(void)
271 unsigned long implementor = read_cpuid_implementor();
272 unsigned long part_number = read_cpuid_part_number();
274 if (implementor != ARM_CPU_IMP_ARM)
277 switch (part_number) {
278 case ARM_CPU_PART_CORTEX_A15:
279 return KVM_ARM_TARGET_CORTEX_A15;
285 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
287 /* Force users to call KVM_ARM_VCPU_INIT */
288 vcpu->arch.target = -1;
292 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
296 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
299 vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
302 * Check whether this vcpu requires the cache to be flushed on
303 * this physical CPU. This is a consequence of doing dcache
304 * operations by set/way on this vcpu. We do it here to be in
305 * a non-preemptible section.
307 if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
308 flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
311 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
315 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
316 struct kvm_guest_debug *dbg)
322 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
323 struct kvm_mp_state *mp_state)
328 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
329 struct kvm_mp_state *mp_state)
335 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
336 * @v: The VCPU pointer
338 * If the guest CPU is not waiting for interrupts or an interrupt line is
339 * asserted, the CPU is by definition runnable.
341 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
343 return !!v->arch.irq_lines;
346 /* Just ensure a guest exit from a particular CPU */
347 static void exit_vm_noop(void *info)
351 void force_vm_exit(const cpumask_t *mask)
353 smp_call_function_many(mask, exit_vm_noop, NULL, true);
357 * need_new_vmid_gen - check that the VMID is still valid
358 * @kvm: The VM's VMID to checkt
360 * return true if there is a new generation of VMIDs being used
362 * The hardware supports only 256 values with the value zero reserved for the
363 * host, so we check if an assigned value belongs to a previous generation,
364 * which which requires us to assign a new value. If we're the first to use a
365 * VMID for the new generation, we must flush necessary caches and TLBs on all
368 static bool need_new_vmid_gen(struct kvm *kvm)
370 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
374 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
375 * @kvm The guest that we are about to run
377 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
378 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
381 static void update_vttbr(struct kvm *kvm)
383 phys_addr_t pgd_phys;
386 if (!need_new_vmid_gen(kvm))
389 spin_lock(&kvm_vmid_lock);
392 * We need to re-check the vmid_gen here to ensure that if another vcpu
393 * already allocated a valid vmid for this vm, then this vcpu should
396 if (!need_new_vmid_gen(kvm)) {
397 spin_unlock(&kvm_vmid_lock);
401 /* First user of a new VMID generation? */
402 if (unlikely(kvm_next_vmid == 0)) {
403 atomic64_inc(&kvm_vmid_gen);
407 * On SMP we know no other CPUs can use this CPU's or each
408 * other's VMID after force_vm_exit returns since the
409 * kvm_vmid_lock blocks them from reentry to the guest.
411 force_vm_exit(cpu_all_mask);
413 * Now broadcast TLB + ICACHE invalidation over the inner
414 * shareable domain to make sure all data structures are
417 kvm_call_hyp(__kvm_flush_vm_context);
420 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
421 kvm->arch.vmid = kvm_next_vmid;
424 /* update vttbr to be used with the new vmid */
425 pgd_phys = virt_to_phys(kvm->arch.pgd);
426 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
427 kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
428 kvm->arch.vttbr |= vmid;
430 spin_unlock(&kvm_vmid_lock);
433 static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
435 /* SVC called from Hyp mode should never get here */
436 kvm_debug("SVC called from Hyp mode shouldn't go here\n");
438 return -EINVAL; /* Squash warning */
441 static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)
443 trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0),
444 vcpu->arch.hsr & HSR_HVC_IMM_MASK);
446 kvm_inject_undefined(vcpu);
450 static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run)
452 /* We don't support SMC; don't do that. */
453 kvm_debug("smc: at %08x", *vcpu_pc(vcpu));
454 kvm_inject_undefined(vcpu);
458 static int handle_pabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
460 /* The hypervisor should never cause aborts */
461 kvm_err("Prefetch Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
462 vcpu->arch.hxfar, vcpu->arch.hsr);
466 static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
468 /* This is either an error in the ws. code or an external abort */
469 kvm_err("Data Abort taken from Hyp mode at %#08x (HSR: %#08x)\n",
470 vcpu->arch.hxfar, vcpu->arch.hsr);
474 typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *);
475 static exit_handle_fn arm_exit_handlers[] = {
476 [HSR_EC_WFI] = kvm_handle_wfi,
477 [HSR_EC_CP15_32] = kvm_handle_cp15_32,
478 [HSR_EC_CP15_64] = kvm_handle_cp15_64,
479 [HSR_EC_CP14_MR] = kvm_handle_cp14_access,
480 [HSR_EC_CP14_LS] = kvm_handle_cp14_load_store,
481 [HSR_EC_CP14_64] = kvm_handle_cp14_access,
482 [HSR_EC_CP_0_13] = kvm_handle_cp_0_13_access,
483 [HSR_EC_CP10_ID] = kvm_handle_cp10_id,
484 [HSR_EC_SVC_HYP] = handle_svc_hyp,
485 [HSR_EC_HVC] = handle_hvc,
486 [HSR_EC_SMC] = handle_smc,
487 [HSR_EC_IABT] = kvm_handle_guest_abort,
488 [HSR_EC_IABT_HYP] = handle_pabt_hyp,
489 [HSR_EC_DABT] = kvm_handle_guest_abort,
490 [HSR_EC_DABT_HYP] = handle_dabt_hyp,
494 * A conditional instruction is allowed to trap, even though it
495 * wouldn't be executed. So let's re-implement the hardware, in
498 static bool kvm_condition_valid(struct kvm_vcpu *vcpu)
500 unsigned long cpsr, cond, insn;
503 * Exception Code 0 can only happen if we set HCR.TGE to 1, to
504 * catch undefined instructions, and then we won't get past
505 * the arm_exit_handlers test anyway.
507 BUG_ON(((vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT) == 0);
509 /* Top two bits non-zero? Unconditional. */
510 if (vcpu->arch.hsr >> 30)
513 cpsr = *vcpu_cpsr(vcpu);
515 /* Is condition field valid? */
516 if ((vcpu->arch.hsr & HSR_CV) >> HSR_CV_SHIFT)
517 cond = (vcpu->arch.hsr & HSR_COND) >> HSR_COND_SHIFT;
519 /* This can happen in Thumb mode: examine IT state. */
522 it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
524 /* it == 0 => unconditional. */
528 /* The cond for this insn works out as the top 4 bits. */
532 /* Shift makes it look like an ARM-mode instruction */
534 return arm_check_condition(insn, cpsr) != ARM_OPCODE_CONDTEST_FAIL;
538 * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
539 * proper exit to QEMU.
541 static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
544 unsigned long hsr_ec;
546 switch (exception_index) {
547 case ARM_EXCEPTION_IRQ:
549 case ARM_EXCEPTION_UNDEFINED:
550 kvm_err("Undefined exception in Hyp mode at: %#08x\n",
553 panic("KVM: Hypervisor undefined exception!\n");
554 case ARM_EXCEPTION_DATA_ABORT:
555 case ARM_EXCEPTION_PREF_ABORT:
556 case ARM_EXCEPTION_HVC:
557 hsr_ec = (vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT;
559 if (hsr_ec >= ARRAY_SIZE(arm_exit_handlers)
560 || !arm_exit_handlers[hsr_ec]) {
561 kvm_err("Unkown exception class: %#08lx, "
562 "hsr: %#08x\n", hsr_ec,
563 (unsigned int)vcpu->arch.hsr);
568 * See ARM ARM B1.14.1: "Hyp traps on instructions
569 * that fail their condition code check"
571 if (!kvm_condition_valid(vcpu)) {
572 bool is_wide = vcpu->arch.hsr & HSR_IL;
573 kvm_skip_instr(vcpu, is_wide);
577 return arm_exit_handlers[hsr_ec](vcpu, run);
579 kvm_pr_unimpl("Unsupported exception type: %d",
581 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
586 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
588 if (likely(vcpu->arch.has_run_once))
591 vcpu->arch.has_run_once = true;
596 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
597 * @vcpu: The VCPU pointer
598 * @run: The kvm_run structure pointer used for userspace state exchange
600 * This function is called through the VCPU_RUN ioctl called from user space. It
601 * will execute VM code in a loop until the time slice for the process is used
602 * or some emulation is needed from user space in which case the function will
603 * return with return value 0 and with the kvm_run structure filled in with the
604 * required data for the requested emulation.
606 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
611 /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
612 if (unlikely(vcpu->arch.target < 0))
615 ret = kvm_vcpu_first_run_init(vcpu);
619 if (vcpu->sigset_active)
620 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
623 run->exit_reason = KVM_EXIT_UNKNOWN;
626 * Check conditions before entering the guest
630 update_vttbr(vcpu->kvm);
635 * Re-check atomic conditions
637 if (signal_pending(current)) {
639 run->exit_reason = KVM_EXIT_INTR;
642 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
647 /**************************************************************
650 trace_kvm_entry(*vcpu_pc(vcpu));
652 vcpu->mode = IN_GUEST_MODE;
654 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
656 vcpu->mode = OUTSIDE_GUEST_MODE;
657 vcpu->arch.last_pcpu = smp_processor_id();
659 trace_kvm_exit(*vcpu_pc(vcpu));
661 * We may have taken a host interrupt in HYP mode (ie
662 * while executing the guest). This interrupt is still
663 * pending, as we haven't serviced it yet!
665 * We're now back in SVC mode, with interrupts
666 * disabled. Enabling the interrupts now will have
667 * the effect of taking the interrupt again, in SVC
674 *************************************************************/
676 ret = handle_exit(vcpu, run, ret);
679 if (vcpu->sigset_active)
680 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
684 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
690 if (number == KVM_ARM_IRQ_CPU_IRQ)
691 bit_index = __ffs(HCR_VI);
692 else /* KVM_ARM_IRQ_CPU_FIQ */
693 bit_index = __ffs(HCR_VF);
695 ptr = (unsigned long *)&vcpu->arch.irq_lines;
697 set = test_and_set_bit(bit_index, ptr);
699 set = test_and_clear_bit(bit_index, ptr);
702 * If we didn't change anything, no need to wake up or kick other CPUs
708 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
709 * trigger a world-switch round on the running physical CPU to set the
710 * virtual IRQ/FIQ fields in the HCR appropriately.
717 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
719 u32 irq = irq_level->irq;
720 unsigned int irq_type, vcpu_idx, irq_num;
721 int nrcpus = atomic_read(&kvm->online_vcpus);
722 struct kvm_vcpu *vcpu = NULL;
723 bool level = irq_level->level;
725 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
726 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
727 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
729 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
731 if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
734 if (vcpu_idx >= nrcpus)
737 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
741 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
744 return vcpu_interrupt_line(vcpu, irq_num, level);
747 long kvm_arch_vcpu_ioctl(struct file *filp,
748 unsigned int ioctl, unsigned long arg)
750 struct kvm_vcpu *vcpu = filp->private_data;
751 void __user *argp = (void __user *)arg;
754 case KVM_ARM_VCPU_INIT: {
755 struct kvm_vcpu_init init;
757 if (copy_from_user(&init, argp, sizeof(init)))
760 return kvm_vcpu_set_target(vcpu, &init);
763 case KVM_SET_ONE_REG:
764 case KVM_GET_ONE_REG: {
765 struct kvm_one_reg reg;
766 if (copy_from_user(®, argp, sizeof(reg)))
768 if (ioctl == KVM_SET_ONE_REG)
769 return kvm_arm_set_reg(vcpu, ®);
771 return kvm_arm_get_reg(vcpu, ®);
773 case KVM_GET_REG_LIST: {
774 struct kvm_reg_list __user *user_list = argp;
775 struct kvm_reg_list reg_list;
778 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
781 reg_list.n = kvm_arm_num_regs(vcpu);
782 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
786 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
793 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
798 long kvm_arch_vm_ioctl(struct file *filp,
799 unsigned int ioctl, unsigned long arg)
804 static void cpu_init_hyp_mode(void *vector)
806 unsigned long long pgd_ptr;
807 unsigned long pgd_low, pgd_high;
808 unsigned long hyp_stack_ptr;
809 unsigned long stack_page;
810 unsigned long vector_ptr;
812 /* Switch from the HYP stub to our own HYP init vector */
813 __hyp_set_vectors((unsigned long)vector);
815 pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
816 pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
817 pgd_high = (pgd_ptr >> 32ULL);
818 stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
819 hyp_stack_ptr = stack_page + PAGE_SIZE;
820 vector_ptr = (unsigned long)__kvm_hyp_vector;
823 * Call initialization code, and switch to the full blown
824 * HYP code. The init code doesn't need to preserve these registers as
825 * r1-r3 and r12 are already callee save according to the AAPCS.
826 * Note that we slightly misuse the prototype by casing the pgd_low to
829 kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
833 * Inits Hyp-mode on all online CPUs
835 static int init_hyp_mode(void)
837 phys_addr_t init_phys_addr;
842 * Allocate Hyp PGD and setup Hyp identity mapping
844 err = kvm_mmu_init();
849 * It is probably enough to obtain the default on one
850 * CPU. It's unlikely to be different on the others.
852 hyp_default_vectors = __hyp_get_vectors();
855 * Allocate stack pages for Hypervisor-mode
857 for_each_possible_cpu(cpu) {
858 unsigned long stack_page;
860 stack_page = __get_free_page(GFP_KERNEL);
863 goto out_free_stack_pages;
866 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
870 * Execute the init code on each CPU.
872 * Note: The stack is not mapped yet, so don't do anything else than
873 * initializing the hypervisor mode on each CPU using a local stack
874 * space for temporary storage.
876 init_phys_addr = virt_to_phys(__kvm_hyp_init);
877 for_each_online_cpu(cpu) {
878 smp_call_function_single(cpu, cpu_init_hyp_mode,
879 (void *)(long)init_phys_addr, 1);
883 * Unmap the identity mapping
885 kvm_clear_hyp_idmap();
888 * Map the Hyp-code called directly from the host
890 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
892 kvm_err("Cannot map world-switch code\n");
893 goto out_free_mappings;
897 * Map the Hyp stack pages
899 for_each_possible_cpu(cpu) {
900 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
901 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
904 kvm_err("Cannot map hyp stack\n");
905 goto out_free_mappings;
910 * Map the host VFP structures
912 kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
913 if (!kvm_host_vfp_state) {
915 kvm_err("Cannot allocate host VFP state\n");
916 goto out_free_mappings;
919 for_each_possible_cpu(cpu) {
920 struct vfp_hard_struct *vfp;
922 vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
923 err = create_hyp_mappings(vfp, vfp + 1);
926 kvm_err("Cannot map host VFP state: %d\n", err);
931 kvm_info("Hyp mode initialized successfully\n");
934 free_percpu(kvm_host_vfp_state);
937 out_free_stack_pages:
938 for_each_possible_cpu(cpu)
939 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
941 kvm_err("error initializing Hyp mode: %d\n", err);
946 * Initialize Hyp-mode and memory mappings on all CPUs.
948 int kvm_arch_init(void *opaque)
952 if (!is_hyp_mode_available()) {
953 kvm_err("HYP mode not available\n");
957 if (kvm_target_cpu() < 0) {
958 kvm_err("Target CPU not supported!\n");
962 err = init_hyp_mode();
966 kvm_coproc_table_init();
972 /* NOP: Compiling as a module not supported */
973 void kvm_arch_exit(void)
977 static int arm_init(void)
979 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
983 module_init(arm_init);