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 <trace/events/kvm.h>
29 #define CREATE_TRACE_POINTS
32 #include <asm/unified.h>
33 #include <asm/uaccess.h>
34 #include <asm/ptrace.h>
36 #include <asm/cputype.h>
37 #include <asm/tlbflush.h>
39 #include <asm/kvm_arm.h>
40 #include <asm/kvm_asm.h>
41 #include <asm/kvm_mmu.h>
44 __asm__(".arch_extension virt");
47 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
48 static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
49 static unsigned long hyp_default_vectors;
52 int kvm_arch_hardware_enable(void *garbage)
57 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
59 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
62 void kvm_arch_hardware_disable(void *garbage)
66 int kvm_arch_hardware_setup(void)
71 void kvm_arch_hardware_unsetup(void)
75 void kvm_arch_check_processor_compat(void *rtn)
80 void kvm_arch_sync_events(struct kvm *kvm)
85 * kvm_arch_init_vm - initializes a VM data structure
86 * @kvm: pointer to the KVM struct
88 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
95 ret = kvm_alloc_stage2_pgd(kvm);
99 ret = create_hyp_mappings(kvm, kvm + 1);
101 goto out_free_stage2_pgd;
103 /* Mark the initial VMID generation invalid */
104 kvm->arch.vmid_gen = 0;
108 kvm_free_stage2_pgd(kvm);
113 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
115 return VM_FAULT_SIGBUS;
118 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
119 struct kvm_memory_slot *dont)
123 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
129 * kvm_arch_destroy_vm - destroy the VM data structure
130 * @kvm: pointer to the KVM struct
132 void kvm_arch_destroy_vm(struct kvm *kvm)
136 kvm_free_stage2_pgd(kvm);
138 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
140 kvm_arch_vcpu_free(kvm->vcpus[i]);
141 kvm->vcpus[i] = NULL;
146 int kvm_dev_ioctl_check_extension(long ext)
150 case KVM_CAP_USER_MEMORY:
151 case KVM_CAP_SYNC_MMU:
152 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
153 case KVM_CAP_ONE_REG:
156 case KVM_CAP_COALESCED_MMIO:
157 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
159 case KVM_CAP_NR_VCPUS:
160 r = num_online_cpus();
162 case KVM_CAP_MAX_VCPUS:
172 long kvm_arch_dev_ioctl(struct file *filp,
173 unsigned int ioctl, unsigned long arg)
178 int kvm_arch_set_memory_region(struct kvm *kvm,
179 struct kvm_userspace_memory_region *mem,
180 struct kvm_memory_slot old,
186 int kvm_arch_prepare_memory_region(struct kvm *kvm,
187 struct kvm_memory_slot *memslot,
188 struct kvm_memory_slot old,
189 struct kvm_userspace_memory_region *mem,
195 void kvm_arch_commit_memory_region(struct kvm *kvm,
196 struct kvm_userspace_memory_region *mem,
197 struct kvm_memory_slot old,
202 void kvm_arch_flush_shadow_all(struct kvm *kvm)
206 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
207 struct kvm_memory_slot *slot)
211 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
214 struct kvm_vcpu *vcpu;
216 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
222 err = kvm_vcpu_init(vcpu, kvm, id);
226 err = create_hyp_mappings(vcpu, vcpu + 1);
232 kvm_vcpu_uninit(vcpu);
234 kmem_cache_free(kvm_vcpu_cache, vcpu);
239 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
244 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
246 kvm_mmu_free_memory_caches(vcpu);
247 kmem_cache_free(kvm_vcpu_cache, vcpu);
250 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
252 kvm_arch_vcpu_free(vcpu);
255 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
260 int __attribute_const__ kvm_target_cpu(void)
262 unsigned long implementor = read_cpuid_implementor();
263 unsigned long part_number = read_cpuid_part_number();
265 if (implementor != ARM_CPU_IMP_ARM)
268 switch (part_number) {
269 case ARM_CPU_PART_CORTEX_A15:
270 return KVM_ARM_TARGET_CORTEX_A15;
276 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
281 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
285 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
289 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
293 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
294 struct kvm_guest_debug *dbg)
300 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
301 struct kvm_mp_state *mp_state)
306 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
307 struct kvm_mp_state *mp_state)
312 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
317 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
322 long kvm_arch_vcpu_ioctl(struct file *filp,
323 unsigned int ioctl, unsigned long arg)
325 struct kvm_vcpu *vcpu = filp->private_data;
326 void __user *argp = (void __user *)arg;
329 case KVM_ARM_VCPU_INIT: {
330 struct kvm_vcpu_init init;
332 if (copy_from_user(&init, argp, sizeof(init)))
335 return kvm_vcpu_set_target(vcpu, &init);
338 case KVM_SET_ONE_REG:
339 case KVM_GET_ONE_REG: {
340 struct kvm_one_reg reg;
341 if (copy_from_user(®, argp, sizeof(reg)))
343 if (ioctl == KVM_SET_ONE_REG)
344 return kvm_arm_set_reg(vcpu, ®);
346 return kvm_arm_get_reg(vcpu, ®);
348 case KVM_GET_REG_LIST: {
349 struct kvm_reg_list __user *user_list = argp;
350 struct kvm_reg_list reg_list;
353 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
356 reg_list.n = kvm_arm_num_regs(vcpu);
357 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
361 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
368 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
373 long kvm_arch_vm_ioctl(struct file *filp,
374 unsigned int ioctl, unsigned long arg)
379 static void cpu_init_hyp_mode(void *vector)
381 unsigned long long pgd_ptr;
382 unsigned long pgd_low, pgd_high;
383 unsigned long hyp_stack_ptr;
384 unsigned long stack_page;
385 unsigned long vector_ptr;
387 /* Switch from the HYP stub to our own HYP init vector */
388 __hyp_set_vectors((unsigned long)vector);
390 pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
391 pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
392 pgd_high = (pgd_ptr >> 32ULL);
393 stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
394 hyp_stack_ptr = stack_page + PAGE_SIZE;
395 vector_ptr = (unsigned long)__kvm_hyp_vector;
398 * Call initialization code, and switch to the full blown
399 * HYP code. The init code doesn't need to preserve these registers as
400 * r1-r3 and r12 are already callee save according to the AAPCS.
401 * Note that we slightly misuse the prototype by casing the pgd_low to
404 kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
408 * Inits Hyp-mode on all online CPUs
410 static int init_hyp_mode(void)
412 phys_addr_t init_phys_addr;
417 * Allocate Hyp PGD and setup Hyp identity mapping
419 err = kvm_mmu_init();
424 * It is probably enough to obtain the default on one
425 * CPU. It's unlikely to be different on the others.
427 hyp_default_vectors = __hyp_get_vectors();
430 * Allocate stack pages for Hypervisor-mode
432 for_each_possible_cpu(cpu) {
433 unsigned long stack_page;
435 stack_page = __get_free_page(GFP_KERNEL);
438 goto out_free_stack_pages;
441 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
445 * Execute the init code on each CPU.
447 * Note: The stack is not mapped yet, so don't do anything else than
448 * initializing the hypervisor mode on each CPU using a local stack
449 * space for temporary storage.
451 init_phys_addr = virt_to_phys(__kvm_hyp_init);
452 for_each_online_cpu(cpu) {
453 smp_call_function_single(cpu, cpu_init_hyp_mode,
454 (void *)(long)init_phys_addr, 1);
458 * Unmap the identity mapping
460 kvm_clear_hyp_idmap();
463 * Map the Hyp-code called directly from the host
465 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
467 kvm_err("Cannot map world-switch code\n");
468 goto out_free_mappings;
472 * Map the Hyp stack pages
474 for_each_possible_cpu(cpu) {
475 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
476 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
479 kvm_err("Cannot map hyp stack\n");
480 goto out_free_mappings;
485 * Map the host VFP structures
487 kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
488 if (!kvm_host_vfp_state) {
490 kvm_err("Cannot allocate host VFP state\n");
491 goto out_free_mappings;
494 for_each_possible_cpu(cpu) {
495 struct vfp_hard_struct *vfp;
497 vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
498 err = create_hyp_mappings(vfp, vfp + 1);
501 kvm_err("Cannot map host VFP state: %d\n", err);
506 kvm_info("Hyp mode initialized successfully\n");
509 free_percpu(kvm_host_vfp_state);
512 out_free_stack_pages:
513 for_each_possible_cpu(cpu)
514 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
516 kvm_err("error initializing Hyp mode: %d\n", err);
521 * Initialize Hyp-mode and memory mappings on all CPUs.
523 int kvm_arch_init(void *opaque)
527 if (!is_hyp_mode_available()) {
528 kvm_err("HYP mode not available\n");
532 if (kvm_target_cpu() < 0) {
533 kvm_err("Target CPU not supported!\n");
537 err = init_hyp_mode();
546 /* NOP: Compiling as a module not supported */
547 void kvm_arch_exit(void)
551 static int arm_init(void)
553 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
557 module_init(arm_init);