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