blob: be06c5de51e3e4791baacf86e2e4574393ff7400 [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
619 if (vcpu->sigset_active)
620 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
621
622 ret = 1;
623 run->exit_reason = KVM_EXIT_UNKNOWN;
624 while (ret > 0) {
625 /*
626 * Check conditions before entering the guest
627 */
628 cond_resched();
629
630 update_vttbr(vcpu->kvm);
631
632 local_irq_disable();
633
634 /*
635 * Re-check atomic conditions
636 */
637 if (signal_pending(current)) {
638 ret = -EINTR;
639 run->exit_reason = KVM_EXIT_INTR;
640 }
641
642 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
643 local_irq_enable();
644 continue;
645 }
646
647 /**************************************************************
648 * Enter the guest
649 */
650 trace_kvm_entry(*vcpu_pc(vcpu));
651 kvm_guest_enter();
652 vcpu->mode = IN_GUEST_MODE;
653
654 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
655
656 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500657 vcpu->arch.last_pcpu = smp_processor_id();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500658 kvm_guest_exit();
659 trace_kvm_exit(*vcpu_pc(vcpu));
660 /*
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!
664 *
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
668 * mode this time.
669 */
670 local_irq_enable();
671
672 /*
673 * Back from guest
674 *************************************************************/
675
676 ret = handle_exit(vcpu, run, ret);
677 }
678
679 if (vcpu->sigset_active)
680 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
681 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500682}
683
Christoffer Dall86ce8532013-01-20 18:28:08 -0500684static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
685{
686 int bit_index;
687 bool set;
688 unsigned long *ptr;
689
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);
694
695 ptr = (unsigned long *)&vcpu->arch.irq_lines;
696 if (level)
697 set = test_and_set_bit(bit_index, ptr);
698 else
699 set = test_and_clear_bit(bit_index, ptr);
700
701 /*
702 * If we didn't change anything, no need to wake up or kick other CPUs
703 */
704 if (set == level)
705 return 0;
706
707 /*
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.
711 */
712 kvm_vcpu_kick(vcpu);
713
714 return 0;
715}
716
717int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level)
718{
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;
724
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;
728
729 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
730
731 if (irq_type != KVM_ARM_IRQ_TYPE_CPU)
732 return -EINVAL;
733
734 if (vcpu_idx >= nrcpus)
735 return -EINVAL;
736
737 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
738 if (!vcpu)
739 return -EINVAL;
740
741 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
742 return -EINVAL;
743
744 return vcpu_interrupt_line(vcpu, irq_num, level);
745}
746
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500747long kvm_arch_vcpu_ioctl(struct file *filp,
748 unsigned int ioctl, unsigned long arg)
749{
750 struct kvm_vcpu *vcpu = filp->private_data;
751 void __user *argp = (void __user *)arg;
752
753 switch (ioctl) {
754 case KVM_ARM_VCPU_INIT: {
755 struct kvm_vcpu_init init;
756
757 if (copy_from_user(&init, argp, sizeof(init)))
758 return -EFAULT;
759
760 return kvm_vcpu_set_target(vcpu, &init);
761
762 }
763 case KVM_SET_ONE_REG:
764 case KVM_GET_ONE_REG: {
765 struct kvm_one_reg reg;
766 if (copy_from_user(&reg, argp, sizeof(reg)))
767 return -EFAULT;
768 if (ioctl == KVM_SET_ONE_REG)
769 return kvm_arm_set_reg(vcpu, &reg);
770 else
771 return kvm_arm_get_reg(vcpu, &reg);
772 }
773 case KVM_GET_REG_LIST: {
774 struct kvm_reg_list __user *user_list = argp;
775 struct kvm_reg_list reg_list;
776 unsigned n;
777
778 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
779 return -EFAULT;
780 n = reg_list.n;
781 reg_list.n = kvm_arm_num_regs(vcpu);
782 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
783 return -EFAULT;
784 if (n < reg_list.n)
785 return -E2BIG;
786 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
787 }
788 default:
789 return -EINVAL;
790 }
791}
792
793int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
794{
795 return -EINVAL;
796}
797
798long kvm_arch_vm_ioctl(struct file *filp,
799 unsigned int ioctl, unsigned long arg)
800{
801 return -EINVAL;
802}
803
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500804static void cpu_init_hyp_mode(void *vector)
805{
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;
811
812 /* Switch from the HYP stub to our own HYP init vector */
813 __hyp_set_vectors((unsigned long)vector);
814
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;
821
822 /*
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
827 * a void *.
828 */
829 kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr);
830}
831
832/**
833 * Inits Hyp-mode on all online CPUs
834 */
835static int init_hyp_mode(void)
836{
837 phys_addr_t init_phys_addr;
838 int cpu;
839 int err = 0;
840
841 /*
842 * Allocate Hyp PGD and setup Hyp identity mapping
843 */
844 err = kvm_mmu_init();
845 if (err)
846 goto out_err;
847
848 /*
849 * It is probably enough to obtain the default on one
850 * CPU. It's unlikely to be different on the others.
851 */
852 hyp_default_vectors = __hyp_get_vectors();
853
854 /*
855 * Allocate stack pages for Hypervisor-mode
856 */
857 for_each_possible_cpu(cpu) {
858 unsigned long stack_page;
859
860 stack_page = __get_free_page(GFP_KERNEL);
861 if (!stack_page) {
862 err = -ENOMEM;
863 goto out_free_stack_pages;
864 }
865
866 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
867 }
868
869 /*
870 * Execute the init code on each CPU.
871 *
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.
875 */
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);
880 }
881
882 /*
883 * Unmap the identity mapping
884 */
885 kvm_clear_hyp_idmap();
886
887 /*
888 * Map the Hyp-code called directly from the host
889 */
890 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
891 if (err) {
892 kvm_err("Cannot map world-switch code\n");
893 goto out_free_mappings;
894 }
895
896 /*
897 * Map the Hyp stack pages
898 */
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);
902
903 if (err) {
904 kvm_err("Cannot map hyp stack\n");
905 goto out_free_mappings;
906 }
907 }
908
909 /*
910 * Map the host VFP structures
911 */
912 kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct);
913 if (!kvm_host_vfp_state) {
914 err = -ENOMEM;
915 kvm_err("Cannot allocate host VFP state\n");
916 goto out_free_mappings;
917 }
918
919 for_each_possible_cpu(cpu) {
920 struct vfp_hard_struct *vfp;
921
922 vfp = per_cpu_ptr(kvm_host_vfp_state, cpu);
923 err = create_hyp_mappings(vfp, vfp + 1);
924
925 if (err) {
926 kvm_err("Cannot map host VFP state: %d\n", err);
927 goto out_free_vfp;
928 }
929 }
930
931 kvm_info("Hyp mode initialized successfully\n");
932 return 0;
933out_free_vfp:
934 free_percpu(kvm_host_vfp_state);
935out_free_mappings:
936 free_hyp_pmds();
937out_free_stack_pages:
938 for_each_possible_cpu(cpu)
939 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
940out_err:
941 kvm_err("error initializing Hyp mode: %d\n", err);
942 return err;
943}
944
945/**
946 * Initialize Hyp-mode and memory mappings on all CPUs.
947 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500948int kvm_arch_init(void *opaque)
949{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500950 int err;
951
952 if (!is_hyp_mode_available()) {
953 kvm_err("HYP mode not available\n");
954 return -ENODEV;
955 }
956
957 if (kvm_target_cpu() < 0) {
958 kvm_err("Target CPU not supported!\n");
959 return -ENODEV;
960 }
961
962 err = init_hyp_mode();
963 if (err)
964 goto out_err;
965
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500966 kvm_coproc_table_init();
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500967 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500968out_err:
969 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500970}
971
972/* NOP: Compiling as a module not supported */
973void kvm_arch_exit(void)
974{
975}
976
977static int arm_init(void)
978{
979 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
980 return rc;
981}
982
983module_init(arm_init);