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