blob: 8680b9ffd2aea6d2dfe9c5dd76257daf0afd884f [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
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>
Christoffer Dall86ce8532013-01-20 18:28:08 -050027#include <linux/kvm.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050028#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>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050038#include <asm/tlbflush.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050039#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050040#include <asm/virt.h>
41#include <asm/kvm_arm.h>
42#include <asm/kvm_asm.h>
43#include <asm/kvm_mmu.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050044#include <asm/kvm_emulate.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050045#include <asm/kvm_coproc.h>
46#include <asm/opcodes.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050047
48#ifdef REQUIRES_VIRT
49__asm__(".arch_extension virt");
50#endif
51
Christoffer Dall342cd0a2013-01-20 18:28:06 -050052static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
53static struct vfp_hard_struct __percpu *kvm_host_vfp_state;
54static unsigned long hyp_default_vectors;
55
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050056/* The VMID used in the VTTBR */
57static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
58static u8 kvm_next_vmid;
59static DEFINE_SPINLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050060
Christoffer Dall749cf76c2013-01-20 18:28:06 -050061int kvm_arch_hardware_enable(void *garbage)
62{
63 return 0;
64}
65
66int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
67{
68 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
69}
70
71void kvm_arch_hardware_disable(void *garbage)
72{
73}
74
75int kvm_arch_hardware_setup(void)
76{
77 return 0;
78}
79
80void kvm_arch_hardware_unsetup(void)
81{
82}
83
84void kvm_arch_check_processor_compat(void *rtn)
85{
86 *(int *)rtn = 0;
87}
88
89void kvm_arch_sync_events(struct kvm *kvm)
90{
91}
92
Christoffer Dalld5d81842013-01-20 18:28:07 -050093/**
94 * kvm_arch_init_vm - initializes a VM data structure
95 * @kvm: pointer to the KVM struct
96 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -050097int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
98{
Christoffer Dalld5d81842013-01-20 18:28:07 -050099 int ret = 0;
100
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500101 if (type)
102 return -EINVAL;
103
Christoffer Dalld5d81842013-01-20 18:28:07 -0500104 ret = kvm_alloc_stage2_pgd(kvm);
105 if (ret)
106 goto out_fail_alloc;
107
108 ret = create_hyp_mappings(kvm, kvm + 1);
109 if (ret)
110 goto out_free_stage2_pgd;
111
112 /* Mark the initial VMID generation invalid */
113 kvm->arch.vmid_gen = 0;
114
115 return ret;
116out_free_stage2_pgd:
117 kvm_free_stage2_pgd(kvm);
118out_fail_alloc:
119 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500120}
121
122int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
123{
124 return VM_FAULT_SIGBUS;
125}
126
127void kvm_arch_free_memslot(struct kvm_memory_slot *free,
128 struct kvm_memory_slot *dont)
129{
130}
131
132int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
133{
134 return 0;
135}
136
Christoffer Dalld5d81842013-01-20 18:28:07 -0500137/**
138 * kvm_arch_destroy_vm - destroy the VM data structure
139 * @kvm: pointer to the KVM struct
140 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500141void kvm_arch_destroy_vm(struct kvm *kvm)
142{
143 int i;
144
Christoffer Dalld5d81842013-01-20 18:28:07 -0500145 kvm_free_stage2_pgd(kvm);
146
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500147 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
148 if (kvm->vcpus[i]) {
149 kvm_arch_vcpu_free(kvm->vcpus[i]);
150 kvm->vcpus[i] = NULL;
151 }
152 }
153}
154
155int kvm_dev_ioctl_check_extension(long ext)
156{
157 int r;
158 switch (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:
163 r = 1;
164 break;
165 case KVM_CAP_COALESCED_MMIO:
166 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
167 break;
168 case KVM_CAP_NR_VCPUS:
169 r = num_online_cpus();
170 break;
171 case KVM_CAP_MAX_VCPUS:
172 r = KVM_MAX_VCPUS;
173 break;
174 default:
175 r = 0;
176 break;
177 }
178 return r;
179}
180
181long kvm_arch_dev_ioctl(struct file *filp,
182 unsigned int ioctl, unsigned long arg)
183{
184 return -EINVAL;
185}
186
187int kvm_arch_set_memory_region(struct kvm *kvm,
188 struct kvm_userspace_memory_region *mem,
189 struct kvm_memory_slot old,
190 int user_alloc)
191{
192 return 0;
193}
194
195int 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,
199 int user_alloc)
200{
201 return 0;
202}
203
204void kvm_arch_commit_memory_region(struct kvm *kvm,
205 struct kvm_userspace_memory_region *mem,
206 struct kvm_memory_slot old,
207 int user_alloc)
208{
209}
210
211void kvm_arch_flush_shadow_all(struct kvm *kvm)
212{
213}
214
215void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
216 struct kvm_memory_slot *slot)
217{
218}
219
220struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
221{
222 int err;
223 struct kvm_vcpu *vcpu;
224
225 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
226 if (!vcpu) {
227 err = -ENOMEM;
228 goto out;
229 }
230
231 err = kvm_vcpu_init(vcpu, kvm, id);
232 if (err)
233 goto free_vcpu;
234
Christoffer Dalld5d81842013-01-20 18:28:07 -0500235 err = create_hyp_mappings(vcpu, vcpu + 1);
236 if (err)
237 goto vcpu_uninit;
238
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500239 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500240vcpu_uninit:
241 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500242free_vcpu:
243 kmem_cache_free(kvm_vcpu_cache, vcpu);
244out:
245 return ERR_PTR(err);
246}
247
248int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
249{
250 return 0;
251}
252
253void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
254{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500255 kvm_mmu_free_memory_caches(vcpu);
256 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500257}
258
259void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
260{
261 kvm_arch_vcpu_free(vcpu);
262}
263
264int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
265{
266 return 0;
267}
268
269int __attribute_const__ kvm_target_cpu(void)
270{
271 unsigned long implementor = read_cpuid_implementor();
272 unsigned long part_number = read_cpuid_part_number();
273
274 if (implementor != ARM_CPU_IMP_ARM)
275 return -EINVAL;
276
277 switch (part_number) {
278 case ARM_CPU_PART_CORTEX_A15:
279 return KVM_ARM_TARGET_CORTEX_A15;
280 default:
281 return -EINVAL;
282 }
283}
284
285int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
286{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500287 /* Force users to call KVM_ARM_VCPU_INIT */
288 vcpu->arch.target = -1;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500289 return 0;
290}
291
292void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
293{
294}
295
296void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
297{
Christoffer Dall86ce8532013-01-20 18:28:08 -0500298 vcpu->cpu = cpu;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500299 vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500300
301 /*
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.
306 */
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() */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500309}
310
311void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
312{
313}
314
315int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
316 struct kvm_guest_debug *dbg)
317{
318 return -EINVAL;
319}
320
321
322int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
323 struct kvm_mp_state *mp_state)
324{
325 return -EINVAL;
326}
327
328int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
329 struct kvm_mp_state *mp_state)
330{
331 return -EINVAL;
332}
333
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500334/**
335 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
336 * @v: The VCPU pointer
337 *
338 * If the guest CPU is not waiting for interrupts or an interrupt line is
339 * asserted, the CPU is by definition runnable.
340 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500341int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
342{
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500343 return !!v->arch.irq_lines;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500344}
345
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500346/* Just ensure a guest exit from a particular CPU */
347static void exit_vm_noop(void *info)
348{
349}
350
351void force_vm_exit(const cpumask_t *mask)
352{
353 smp_call_function_many(mask, exit_vm_noop, NULL, true);
354}
355
356/**
357 * need_new_vmid_gen - check that the VMID is still valid
358 * @kvm: The VM's VMID to checkt
359 *
360 * return true if there is a new generation of VMIDs being used
361 *
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
366 * CPUs.
367 */
368static bool need_new_vmid_gen(struct kvm *kvm)
369{
370 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
371}
372
373/**
374 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
375 * @kvm The guest that we are about to run
376 *
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
379 * caches and TLBs.
380 */
381static void update_vttbr(struct kvm *kvm)
382{
383 phys_addr_t pgd_phys;
384 u64 vmid;
385
386 if (!need_new_vmid_gen(kvm))
387 return;
388
389 spin_lock(&kvm_vmid_lock);
390
391 /*
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
394 * use the same vmid.
395 */
396 if (!need_new_vmid_gen(kvm)) {
397 spin_unlock(&kvm_vmid_lock);
398 return;
399 }
400
401 /* First user of a new VMID generation? */
402 if (unlikely(kvm_next_vmid == 0)) {
403 atomic64_inc(&kvm_vmid_gen);
404 kvm_next_vmid = 1;
405
406 /*
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.
410 */
411 force_vm_exit(cpu_all_mask);
412 /*
413 * Now broadcast TLB + ICACHE invalidation over the inner
414 * shareable domain to make sure all data structures are
415 * clean.
416 */
417 kvm_call_hyp(__kvm_flush_vm_context);
418 }
419
420 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
421 kvm->arch.vmid = kvm_next_vmid;
422 kvm_next_vmid++;
423
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;
429
430 spin_unlock(&kvm_vmid_lock);
431}
432
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500433static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
434{
435 /* SVC called from Hyp mode should never get here */
436 kvm_debug("SVC called from Hyp mode shouldn't go here\n");
437 BUG();
438 return -EINVAL; /* Squash warning */
439}
440
441static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)
442{
443 trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0),
444 vcpu->arch.hsr & HSR_HVC_IMM_MASK);
445
446 kvm_inject_undefined(vcpu);
447 return 1;
448}
449
450static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run)
451{
452 /* We don't support SMC; don't do that. */
453 kvm_debug("smc: at %08x", *vcpu_pc(vcpu));
454 kvm_inject_undefined(vcpu);
455 return 1;
456}
457
458static int handle_pabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
459{
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);
463 return -EFAULT;
464}
465
466static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run)
467{
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);
471 return -EFAULT;
472}
473
474typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *);
475static 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,
491};
492
493/*
494 * A conditional instruction is allowed to trap, even though it
495 * wouldn't be executed. So let's re-implement the hardware, in
496 * software!
497 */
498static bool kvm_condition_valid(struct kvm_vcpu *vcpu)
499{
500 unsigned long cpsr, cond, insn;
501
502 /*
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.
506 */
507 BUG_ON(((vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT) == 0);
508
509 /* Top two bits non-zero? Unconditional. */
510 if (vcpu->arch.hsr >> 30)
511 return true;
512
513 cpsr = *vcpu_cpsr(vcpu);
514
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;
518 else {
519 /* This can happen in Thumb mode: examine IT state. */
520 unsigned long it;
521
522 it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
523
524 /* it == 0 => unconditional. */
525 if (it == 0)
526 return true;
527
528 /* The cond for this insn works out as the top 4 bits. */
529 cond = (it >> 4);
530 }
531
532 /* Shift makes it look like an ARM-mode instruction */
533 insn = cond << 28;
534 return arm_check_condition(insn, cpsr) != ARM_OPCODE_CONDTEST_FAIL;
535}
536
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500537/*
538 * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
539 * proper exit to QEMU.
540 */
541static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
542 int exception_index)
543{
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500544 unsigned long hsr_ec;
545
546 switch (exception_index) {
547 case ARM_EXCEPTION_IRQ:
548 return 1;
549 case ARM_EXCEPTION_UNDEFINED:
550 kvm_err("Undefined exception in Hyp mode at: %#08x\n",
551 vcpu->arch.hyp_pc);
552 BUG();
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;
558
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);
564 BUG();
565 }
566
567 /*
568 * See ARM ARM B1.14.1: "Hyp traps on instructions
569 * that fail their condition code check"
570 */
571 if (!kvm_condition_valid(vcpu)) {
572 bool is_wide = vcpu->arch.hsr & HSR_IL;
573 kvm_skip_instr(vcpu, is_wide);
574 return 1;
575 }
576
577 return arm_exit_handlers[hsr_ec](vcpu, run);
578 default:
579 kvm_pr_unimpl("Unsupported exception type: %d",
580 exception_index);
581 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
582 return 0;
583 }
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500584}
585
586static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
587{
588 if (likely(vcpu->arch.has_run_once))
589 return 0;
590
591 vcpu->arch.has_run_once = true;
592 return 0;
593}
594
595/**
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
599 *
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.
605 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500606int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
607{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500608 int ret;
609 sigset_t sigsaved;
610
611 /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */
612 if (unlikely(vcpu->arch.target < 0))
613 return -ENOEXEC;
614
615 ret = kvm_vcpu_first_run_init(vcpu);
616 if (ret)
617 return ret;
618
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500619 if (run->exit_reason == KVM_EXIT_MMIO) {
620 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
621 if (ret)
622 return ret;
623 }
624
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500625 if (vcpu->sigset_active)
626 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
627
628 ret = 1;
629 run->exit_reason = KVM_EXIT_UNKNOWN;
630 while (ret > 0) {
631 /*
632 * Check conditions before entering the guest
633 */
634 cond_resched();
635
636 update_vttbr(vcpu->kvm);
637
638 local_irq_disable();
639
640 /*
641 * Re-check atomic conditions
642 */
643 if (signal_pending(current)) {
644 ret = -EINTR;
645 run->exit_reason = KVM_EXIT_INTR;
646 }
647
648 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
649 local_irq_enable();
650 continue;
651 }
652
653 /**************************************************************
654 * Enter the guest
655 */
656 trace_kvm_entry(*vcpu_pc(vcpu));
657 kvm_guest_enter();
658 vcpu->mode = IN_GUEST_MODE;
659
660 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
661
662 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500663 vcpu->arch.last_pcpu = smp_processor_id();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500664 kvm_guest_exit();
665 trace_kvm_exit(*vcpu_pc(vcpu));
666 /*
667 * We may have taken a host interrupt in HYP mode (ie
668 * while executing the guest). This interrupt is still
669 * pending, as we haven't serviced it yet!
670 *
671 * We're now back in SVC mode, with interrupts
672 * disabled. Enabling the interrupts now will have
673 * the effect of taking the interrupt again, in SVC
674 * mode this time.
675 */
676 local_irq_enable();
677
678 /*
679 * Back from guest
680 *************************************************************/
681
682 ret = handle_exit(vcpu, run, ret);
683 }
684
685 if (vcpu->sigset_active)
686 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
687 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500688}
689
Christoffer Dall86ce8532013-01-20 18:28:08 -0500690static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
691{
692 int bit_index;
693 bool set;
694 unsigned long *ptr;
695
696 if (number == KVM_ARM_IRQ_CPU_IRQ)
697 bit_index = __ffs(HCR_VI);
698 else /* KVM_ARM_IRQ_CPU_FIQ */
699 bit_index = __ffs(HCR_VF);
700
701 ptr = (unsigned long *)&vcpu->arch.irq_lines;
702 if (level)
703 set = test_and_set_bit(bit_index, ptr);
704 else
705 set = test_and_clear_bit(bit_index, ptr);
706
707 /*
708 * If we didn't change anything, no need to wake up or kick other CPUs
709 */
710 if (set == level)
711 return 0;
712
713 /*
714 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
715 * trigger a world-switch round on the running physical CPU to set the
716 * virtual IRQ/FIQ fields in the HCR appropriately.
717 */
718 kvm_vcpu_kick(vcpu);
719
720 return 0;
721}
722
723int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
724{
725 u32 irq = irq_level->irq;
726 unsigned int irq_type, vcpu_idx, irq_num;
727 int nrcpus = atomic_read(&kvm->online_vcpus);
728 struct kvm_vcpu *vcpu = NULL;
729 bool level = irq_level->level;
730
731 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
732 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
733 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
734
735 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
736
737 if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
738 return -EINVAL;
739
740 if (vcpu_idx >= nrcpus)
741 return -EINVAL;
742
743 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
744 if (!vcpu)
745 return -EINVAL;
746
747 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
748 return -EINVAL;
749
750 return vcpu_interrupt_line(vcpu, irq_num, level);
751}
752
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500753long kvm_arch_vcpu_ioctl(struct file *filp,
754 unsigned int ioctl, unsigned long arg)
755{
756 struct kvm_vcpu *vcpu = filp->private_data;
757 void __user *argp = (void __user *)arg;
758
759 switch (ioctl) {
760 case KVM_ARM_VCPU_INIT: {
761 struct kvm_vcpu_init init;
762
763 if (copy_from_user(&init, argp, sizeof(init)))
764 return -EFAULT;
765
766 return kvm_vcpu_set_target(vcpu, &init);
767
768 }
769 case KVM_SET_ONE_REG:
770 case KVM_GET_ONE_REG: {
771 struct kvm_one_reg reg;
772 if (copy_from_user(&reg, argp, sizeof(reg)))
773 return -EFAULT;
774 if (ioctl == KVM_SET_ONE_REG)
775 return kvm_arm_set_reg(vcpu, &reg);
776 else
777 return kvm_arm_get_reg(vcpu, &reg);
778 }
779 case KVM_GET_REG_LIST: {
780 struct kvm_reg_list __user *user_list = argp;
781 struct kvm_reg_list reg_list;
782 unsigned n;
783
784 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
785 return -EFAULT;
786 n = reg_list.n;
787 reg_list.n = kvm_arm_num_regs(vcpu);
788 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
789 return -EFAULT;
790 if (n < reg_list.n)
791 return -E2BIG;
792 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
793 }
794 default:
795 return -EINVAL;
796 }
797}
798
799int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
800{
801 return -EINVAL;
802}
803
804long kvm_arch_vm_ioctl(struct file *filp,
805 unsigned int ioctl, unsigned long arg)
806{
807 return -EINVAL;
808}
809
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500810static void cpu_init_hyp_mode(void *vector)
811{
812 unsigned long long pgd_ptr;
813 unsigned long pgd_low, pgd_high;
814 unsigned long hyp_stack_ptr;
815 unsigned long stack_page;
816 unsigned long vector_ptr;
817
818 /* Switch from the HYP stub to our own HYP init vector */
819 __hyp_set_vectors((unsigned long)vector);
820
821 pgd_ptr = (unsigned long long)kvm_mmu_get_httbr();
822 pgd_low = (pgd_ptr & ((1ULL << 32) - 1));
823 pgd_high = (pgd_ptr >> 32ULL);
824 stack_page = __get_cpu_var(kvm_arm_hyp_stack_page);
825 hyp_stack_ptr = stack_page + PAGE_SIZE;
826 vector_ptr = (unsigned long)__kvm_hyp_vector;
827
828 /*
829 * Call initialization code, and switch to the full blown
830 * HYP code. The init code doesn't need to preserve these registers as
831 * r1-r3 and r12 are already callee save according to the AAPCS.
832 * Note that we slightly misuse the prototype by casing the pgd_low to
833 * a void *.
834 */
835 kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
836}
837
838/**
839 * Inits Hyp-mode on all online CPUs
840 */
841static int init_hyp_mode(void)
842{
843 phys_addr_t init_phys_addr;
844 int cpu;
845 int err = 0;
846
847 /*
848 * Allocate Hyp PGD and setup Hyp identity mapping
849 */
850 err = kvm_mmu_init();
851 if (err)
852 goto out_err;
853
854 /*
855 * It is probably enough to obtain the default on one
856 * CPU. It's unlikely to be different on the others.
857 */
858 hyp_default_vectors = __hyp_get_vectors();
859
860 /*
861 * Allocate stack pages for Hypervisor-mode
862 */
863 for_each_possible_cpu(cpu) {
864 unsigned long stack_page;
865
866 stack_page = __get_free_page(GFP_KERNEL);
867 if (!stack_page) {
868 err = -ENOMEM;
869 goto out_free_stack_pages;
870 }
871
872 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
873 }
874
875 /*
876 * Execute the init code on each CPU.
877 *
878 * Note: The stack is not mapped yet, so don't do anything else than
879 * initializing the hypervisor mode on each CPU using a local stack
880 * space for temporary storage.
881 */
882 init_phys_addr = virt_to_phys(__kvm_hyp_init);
883 for_each_online_cpu(cpu) {
884 smp_call_function_single(cpu, cpu_init_hyp_mode,
885 (void *)(long)init_phys_addr, 1);
886 }
887
888 /*
889 * Unmap the identity mapping
890 */
891 kvm_clear_hyp_idmap();
892
893 /*
894 * Map the Hyp-code called directly from the host
895 */
896 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
897 if (err) {
898 kvm_err("Cannot map world-switch code\n");
899 goto out_free_mappings;
900 }
901
902 /*
903 * Map the Hyp stack pages
904 */
905 for_each_possible_cpu(cpu) {
906 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
907 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
908
909 if (err) {
910 kvm_err("Cannot map hyp stack\n");
911 goto out_free_mappings;
912 }
913 }
914
915 /*
916 * Map the host VFP structures
917 */
918 kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
919 if (!kvm_host_vfp_state) {
920 err = -ENOMEM;
921 kvm_err("Cannot allocate host VFP state\n");
922 goto out_free_mappings;
923 }
924
925 for_each_possible_cpu(cpu) {
926 struct vfp_hard_struct *vfp;
927
928 vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
929 err = create_hyp_mappings(vfp, vfp + 1);
930
931 if (err) {
932 kvm_err("Cannot map host VFP state: %d\n", err);
933 goto out_free_vfp;
934 }
935 }
936
937 kvm_info("Hyp mode initialized successfully\n");
938 return 0;
939out_free_vfp:
940 free_percpu(kvm_host_vfp_state);
941out_free_mappings:
942 free_hyp_pmds();
943out_free_stack_pages:
944 for_each_possible_cpu(cpu)
945 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
946out_err:
947 kvm_err("error initializing Hyp mode: %d\n", err);
948 return err;
949}
950
951/**
952 * Initialize Hyp-mode and memory mappings on all CPUs.
953 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500954int kvm_arch_init(void *opaque)
955{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500956 int err;
957
958 if (!is_hyp_mode_available()) {
959 kvm_err("HYP mode not available\n");
960 return -ENODEV;
961 }
962
963 if (kvm_target_cpu() < 0) {
964 kvm_err("Target CPU not supported!\n");
965 return -ENODEV;
966 }
967
968 err = init_hyp_mode();
969 if (err)
970 goto out_err;
971
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500972 kvm_coproc_table_init();
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500973 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500974out_err:
975 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500976}
977
978/* NOP: Compiling as a module not supported */
979void kvm_arch_exit(void)
980{
981}
982
983static int arm_init(void)
984{
985 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
986 return rc;
987}
988
989module_init(arm_init);