blob: 9f788ebac55bec7222b703bcb8f258c26c9334bc [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
Marc Zyngierd157f4a2013-04-12 19:12:07 +010019#include <linux/cpu.h>
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +010020#include <linux/cpu_pm.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050021#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/kvm_host.h>
24#include <linux/module.h>
25#include <linux/vmalloc.h>
26#include <linux/fs.h>
27#include <linux/mman.h>
28#include <linux/sched.h>
Christoffer Dall86ce85352013-01-20 18:28:08 -050029#include <linux/kvm.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050030#include <trace/events/kvm.h>
31
32#define CREATE_TRACE_POINTS
33#include "trace.h"
34
Christoffer Dall749cf76c2013-01-20 18:28:06 -050035#include <asm/uaccess.h>
36#include <asm/ptrace.h>
37#include <asm/mman.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>
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050046#include <asm/kvm_psci.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);
Marc Zyngier3de50da2013-04-08 16:47:19 +010053static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050054static unsigned long hyp_default_vectors;
55
Marc Zyngier1638a122013-01-21 19:36:11 -050056/* Per-CPU variable containing the currently running vcpu. */
57static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
58
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050059/* The VMID used in the VTTBR */
60static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
61static u8 kvm_next_vmid;
62static DEFINE_SPINLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050063
Marc Zyngier1a89dd92013-01-21 19:36:12 -050064static bool vgic_present;
65
Marc Zyngier1638a122013-01-21 19:36:11 -050066static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
67{
68 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010069 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050070}
71
72/**
73 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
74 * Must be called from non-preemptible context
75 */
76struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
77{
78 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010079 return __this_cpu_read(kvm_arm_running_vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050080}
81
82/**
83 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
84 */
85struct kvm_vcpu __percpu **kvm_get_running_vcpus(void)
86{
87 return &kvm_arm_running_vcpu;
88}
89
Christoffer Dall749cf76c2013-01-20 18:28:06 -050090int kvm_arch_hardware_enable(void *garbage)
91{
92 return 0;
93}
94
95int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
96{
97 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
98}
99
100void kvm_arch_hardware_disable(void *garbage)
101{
102}
103
104int kvm_arch_hardware_setup(void)
105{
106 return 0;
107}
108
109void kvm_arch_hardware_unsetup(void)
110{
111}
112
113void kvm_arch_check_processor_compat(void *rtn)
114{
115 *(int *)rtn = 0;
116}
117
118void kvm_arch_sync_events(struct kvm *kvm)
119{
120}
121
Christoffer Dalld5d81842013-01-20 18:28:07 -0500122/**
123 * kvm_arch_init_vm - initializes a VM data structure
124 * @kvm: pointer to the KVM struct
125 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500126int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
127{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500128 int ret = 0;
129
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500130 if (type)
131 return -EINVAL;
132
Christoffer Dalld5d81842013-01-20 18:28:07 -0500133 ret = kvm_alloc_stage2_pgd(kvm);
134 if (ret)
135 goto out_fail_alloc;
136
137 ret = create_hyp_mappings(kvm, kvm + 1);
138 if (ret)
139 goto out_free_stage2_pgd;
140
Christoffer Dalla1a64382013-11-16 10:51:25 -0800141 kvm_timer_init(kvm);
142
Christoffer Dalld5d81842013-01-20 18:28:07 -0500143 /* Mark the initial VMID generation invalid */
144 kvm->arch.vmid_gen = 0;
145
146 return ret;
147out_free_stage2_pgd:
148 kvm_free_stage2_pgd(kvm);
149out_fail_alloc:
150 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500151}
152
153int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
154{
155 return VM_FAULT_SIGBUS;
156}
157
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500158
Christoffer Dalld5d81842013-01-20 18:28:07 -0500159/**
160 * kvm_arch_destroy_vm - destroy the VM data structure
161 * @kvm: pointer to the KVM struct
162 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500163void kvm_arch_destroy_vm(struct kvm *kvm)
164{
165 int i;
166
Christoffer Dalld5d81842013-01-20 18:28:07 -0500167 kvm_free_stage2_pgd(kvm);
168
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500169 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
170 if (kvm->vcpus[i]) {
171 kvm_arch_vcpu_free(kvm->vcpus[i]);
172 kvm->vcpus[i] = NULL;
173 }
174 }
175}
176
Alexander Graf784aa3d2014-07-14 18:27:35 +0200177int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500178{
179 int r;
180 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500181 case KVM_CAP_IRQCHIP:
182 r = vgic_present;
183 break;
Christoffer Dall73306722013-10-25 17:29:18 +0100184 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500185 case KVM_CAP_USER_MEMORY:
186 case KVM_CAP_SYNC_MMU:
187 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
188 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500189 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530190 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500191 r = 1;
192 break;
193 case KVM_CAP_COALESCED_MMIO:
194 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
195 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500196 case KVM_CAP_ARM_SET_DEVICE_ADDR:
197 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100198 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500199 case KVM_CAP_NR_VCPUS:
200 r = num_online_cpus();
201 break;
202 case KVM_CAP_MAX_VCPUS:
203 r = KVM_MAX_VCPUS;
204 break;
205 default:
Marc Zyngier17b1e312013-04-08 16:47:18 +0100206 r = kvm_arch_dev_ioctl_check_extension(ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500207 break;
208 }
209 return r;
210}
211
212long kvm_arch_dev_ioctl(struct file *filp,
213 unsigned int ioctl, unsigned long arg)
214{
215 return -EINVAL;
216}
217
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500218
219struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
220{
221 int err;
222 struct kvm_vcpu *vcpu;
223
224 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
225 if (!vcpu) {
226 err = -ENOMEM;
227 goto out;
228 }
229
230 err = kvm_vcpu_init(vcpu, kvm, id);
231 if (err)
232 goto free_vcpu;
233
Christoffer Dalld5d81842013-01-20 18:28:07 -0500234 err = create_hyp_mappings(vcpu, vcpu + 1);
235 if (err)
236 goto vcpu_uninit;
237
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500238 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500239vcpu_uninit:
240 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500241free_vcpu:
242 kmem_cache_free(kvm_vcpu_cache, vcpu);
243out:
244 return ERR_PTR(err);
245}
246
247int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
248{
249 return 0;
250}
251
252void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
253{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500254 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500255 kvm_timer_vcpu_terminate(vcpu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500256 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
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500269int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
270{
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500271 int ret;
272
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500273 /* Force users to call KVM_ARM_VCPU_INIT */
274 vcpu->arch.target = -1;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500275
276 /* Set up VGIC */
277 ret = kvm_vgic_vcpu_init(vcpu);
278 if (ret)
279 return ret;
280
Marc Zyngier967f8422013-01-23 13:21:59 -0500281 /* Set up the timer */
282 kvm_timer_vcpu_init(vcpu);
283
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500284 return 0;
285}
286
287void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
288{
289}
290
Radim Krčmáře790d9e2014-08-21 18:08:05 +0200291void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
292{
293}
294
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500295void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
296{
Christoffer Dall86ce85352013-01-20 18:28:08 -0500297 vcpu->cpu = cpu;
Marc Zyngier3de50da2013-04-08 16:47:19 +0100298 vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500299
300 /*
301 * Check whether this vcpu requires the cache to be flushed on
302 * this physical CPU. This is a consequence of doing dcache
303 * operations by set/way on this vcpu. We do it here to be in
304 * a non-preemptible section.
305 */
306 if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush))
307 flush_cache_all(); /* We'd really want v7_flush_dcache_all() */
Marc Zyngier1638a122013-01-21 19:36:11 -0500308
309 kvm_arm_set_running_vcpu(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500310}
311
312void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
313{
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800314 /*
315 * The arch-generic KVM code expects the cpu field of a vcpu to be -1
316 * if the vcpu is no longer assigned to a cpu. This is used for the
317 * optimized make_all_cpus_request path.
318 */
319 vcpu->cpu = -1;
320
Marc Zyngier1638a122013-01-21 19:36:11 -0500321 kvm_arm_set_running_vcpu(NULL);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500322}
323
324int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
325 struct kvm_guest_debug *dbg)
326{
327 return -EINVAL;
328}
329
330
331int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
332 struct kvm_mp_state *mp_state)
333{
334 return -EINVAL;
335}
336
337int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
338 struct kvm_mp_state *mp_state)
339{
340 return -EINVAL;
341}
342
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500343/**
344 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
345 * @v: The VCPU pointer
346 *
347 * If the guest CPU is not waiting for interrupts or an interrupt line is
348 * asserted, the CPU is by definition runnable.
349 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500350int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
351{
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500352 return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500353}
354
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500355/* Just ensure a guest exit from a particular CPU */
356static void exit_vm_noop(void *info)
357{
358}
359
360void force_vm_exit(const cpumask_t *mask)
361{
362 smp_call_function_many(mask, exit_vm_noop, NULL, true);
363}
364
365/**
366 * need_new_vmid_gen - check that the VMID is still valid
367 * @kvm: The VM's VMID to checkt
368 *
369 * return true if there is a new generation of VMIDs being used
370 *
371 * The hardware supports only 256 values with the value zero reserved for the
372 * host, so we check if an assigned value belongs to a previous generation,
373 * which which requires us to assign a new value. If we're the first to use a
374 * VMID for the new generation, we must flush necessary caches and TLBs on all
375 * CPUs.
376 */
377static bool need_new_vmid_gen(struct kvm *kvm)
378{
379 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
380}
381
382/**
383 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
384 * @kvm The guest that we are about to run
385 *
386 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
387 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
388 * caches and TLBs.
389 */
390static void update_vttbr(struct kvm *kvm)
391{
392 phys_addr_t pgd_phys;
393 u64 vmid;
394
395 if (!need_new_vmid_gen(kvm))
396 return;
397
398 spin_lock(&kvm_vmid_lock);
399
400 /*
401 * We need to re-check the vmid_gen here to ensure that if another vcpu
402 * already allocated a valid vmid for this vm, then this vcpu should
403 * use the same vmid.
404 */
405 if (!need_new_vmid_gen(kvm)) {
406 spin_unlock(&kvm_vmid_lock);
407 return;
408 }
409
410 /* First user of a new VMID generation? */
411 if (unlikely(kvm_next_vmid == 0)) {
412 atomic64_inc(&kvm_vmid_gen);
413 kvm_next_vmid = 1;
414
415 /*
416 * On SMP we know no other CPUs can use this CPU's or each
417 * other's VMID after force_vm_exit returns since the
418 * kvm_vmid_lock blocks them from reentry to the guest.
419 */
420 force_vm_exit(cpu_all_mask);
421 /*
422 * Now broadcast TLB + ICACHE invalidation over the inner
423 * shareable domain to make sure all data structures are
424 * clean.
425 */
426 kvm_call_hyp(__kvm_flush_vm_context);
427 }
428
429 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
430 kvm->arch.vmid = kvm_next_vmid;
431 kvm_next_vmid++;
432
433 /* update vttbr to be used with the new vmid */
434 pgd_phys = virt_to_phys(kvm->arch.pgd);
435 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
436 kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK;
437 kvm->arch.vttbr |= vmid;
438
439 spin_unlock(&kvm_vmid_lock);
440}
441
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500442static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
443{
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700444 int ret;
445
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500446 if (likely(vcpu->arch.has_run_once))
447 return 0;
448
449 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500450
451 /*
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500452 * Initialize the VGIC before running a vcpu the first time on
453 * this VM.
454 */
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700455 if (unlikely(!vgic_initialized(vcpu->kvm))) {
456 ret = kvm_vgic_init(vcpu->kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500457 if (ret)
458 return ret;
459 }
460
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500461 return 0;
462}
463
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500464static void vcpu_pause(struct kvm_vcpu *vcpu)
465{
466 wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
467
468 wait_event_interruptible(*wq, !vcpu->arch.pause);
469}
470
Andre Przywarae8180dc2013-05-09 00:28:06 +0200471static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
472{
473 return vcpu->arch.target >= 0;
474}
475
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500476/**
477 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
478 * @vcpu: The VCPU pointer
479 * @run: The kvm_run structure pointer used for userspace state exchange
480 *
481 * This function is called through the VCPU_RUN ioctl called from user space. It
482 * will execute VM code in a loop until the time slice for the process is used
483 * or some emulation is needed from user space in which case the function will
484 * return with return value 0 and with the kvm_run structure filled in with the
485 * required data for the requested emulation.
486 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500487int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
488{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500489 int ret;
490 sigset_t sigsaved;
491
Andre Przywarae8180dc2013-05-09 00:28:06 +0200492 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500493 return -ENOEXEC;
494
495 ret = kvm_vcpu_first_run_init(vcpu);
496 if (ret)
497 return ret;
498
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500499 if (run->exit_reason == KVM_EXIT_MMIO) {
500 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
501 if (ret)
502 return ret;
503 }
504
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500505 if (vcpu->sigset_active)
506 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
507
508 ret = 1;
509 run->exit_reason = KVM_EXIT_UNKNOWN;
510 while (ret > 0) {
511 /*
512 * Check conditions before entering the guest
513 */
514 cond_resched();
515
516 update_vttbr(vcpu->kvm);
517
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500518 if (vcpu->arch.pause)
519 vcpu_pause(vcpu);
520
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500521 kvm_vgic_flush_hwstate(vcpu);
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500522 kvm_timer_flush_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500523
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500524 local_irq_disable();
525
526 /*
527 * Re-check atomic conditions
528 */
529 if (signal_pending(current)) {
530 ret = -EINTR;
531 run->exit_reason = KVM_EXIT_INTR;
532 }
533
534 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
535 local_irq_enable();
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500536 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500537 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500538 continue;
539 }
540
541 /**************************************************************
542 * Enter the guest
543 */
544 trace_kvm_entry(*vcpu_pc(vcpu));
545 kvm_guest_enter();
546 vcpu->mode = IN_GUEST_MODE;
547
548 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
549
550 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500551 vcpu->arch.last_pcpu = smp_processor_id();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500552 kvm_guest_exit();
553 trace_kvm_exit(*vcpu_pc(vcpu));
554 /*
555 * We may have taken a host interrupt in HYP mode (ie
556 * while executing the guest). This interrupt is still
557 * pending, as we haven't serviced it yet!
558 *
559 * We're now back in SVC mode, with interrupts
560 * disabled. Enabling the interrupts now will have
561 * the effect of taking the interrupt again, in SVC
562 * mode this time.
563 */
564 local_irq_enable();
565
566 /*
567 * Back from guest
568 *************************************************************/
569
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500570 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500571 kvm_vgic_sync_hwstate(vcpu);
572
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500573 ret = handle_exit(vcpu, run, ret);
574 }
575
576 if (vcpu->sigset_active)
577 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
578 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500579}
580
Christoffer Dall86ce85352013-01-20 18:28:08 -0500581static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
582{
583 int bit_index;
584 bool set;
585 unsigned long *ptr;
586
587 if (number == KVM_ARM_IRQ_CPU_IRQ)
588 bit_index = __ffs(HCR_VI);
589 else /* KVM_ARM_IRQ_CPU_FIQ */
590 bit_index = __ffs(HCR_VF);
591
592 ptr = (unsigned long *)&vcpu->arch.irq_lines;
593 if (level)
594 set = test_and_set_bit(bit_index, ptr);
595 else
596 set = test_and_clear_bit(bit_index, ptr);
597
598 /*
599 * If we didn't change anything, no need to wake up or kick other CPUs
600 */
601 if (set == level)
602 return 0;
603
604 /*
605 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
606 * trigger a world-switch round on the running physical CPU to set the
607 * virtual IRQ/FIQ fields in the HCR appropriately.
608 */
609 kvm_vcpu_kick(vcpu);
610
611 return 0;
612}
613
Alexander Graf79558f12013-04-16 19:21:41 +0200614int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
615 bool line_status)
Christoffer Dall86ce85352013-01-20 18:28:08 -0500616{
617 u32 irq = irq_level->irq;
618 unsigned int irq_type, vcpu_idx, irq_num;
619 int nrcpus = atomic_read(&kvm->online_vcpus);
620 struct kvm_vcpu *vcpu = NULL;
621 bool level = irq_level->level;
622
623 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
624 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
625 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
626
627 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
628
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500629 switch (irq_type) {
630 case KVM_ARM_IRQ_TYPE_CPU:
631 if (irqchip_in_kernel(kvm))
632 return -ENXIO;
Christoffer Dall86ce85352013-01-20 18:28:08 -0500633
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500634 if (vcpu_idx >= nrcpus)
635 return -EINVAL;
Christoffer Dall86ce85352013-01-20 18:28:08 -0500636
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500637 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
638 if (!vcpu)
639 return -EINVAL;
Christoffer Dall86ce85352013-01-20 18:28:08 -0500640
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500641 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
642 return -EINVAL;
Christoffer Dall86ce85352013-01-20 18:28:08 -0500643
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500644 return vcpu_interrupt_line(vcpu, irq_num, level);
645 case KVM_ARM_IRQ_TYPE_PPI:
646 if (!irqchip_in_kernel(kvm))
647 return -ENXIO;
648
649 if (vcpu_idx >= nrcpus)
650 return -EINVAL;
651
652 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
653 if (!vcpu)
654 return -EINVAL;
655
656 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
657 return -EINVAL;
658
659 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
660 case KVM_ARM_IRQ_TYPE_SPI:
661 if (!irqchip_in_kernel(kvm))
662 return -ENXIO;
663
664 if (irq_num < VGIC_NR_PRIVATE_IRQS ||
665 irq_num > KVM_ARM_IRQ_GIC_MAX)
666 return -EINVAL;
667
668 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
669 }
670
671 return -EINVAL;
Christoffer Dall86ce85352013-01-20 18:28:08 -0500672}
673
Christoffer Dall478a8232013-11-19 17:43:19 -0800674static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
675 struct kvm_vcpu_init *init)
676{
677 int ret;
678
679 ret = kvm_vcpu_set_target(vcpu, init);
680 if (ret)
681 return ret;
682
683 /*
684 * Handle the "start in power-off" case by marking the VCPU as paused.
685 */
686 if (__test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
687 vcpu->arch.pause = true;
688
689 return 0;
690}
691
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500692long kvm_arch_vcpu_ioctl(struct file *filp,
693 unsigned int ioctl, unsigned long arg)
694{
695 struct kvm_vcpu *vcpu = filp->private_data;
696 void __user *argp = (void __user *)arg;
697
698 switch (ioctl) {
699 case KVM_ARM_VCPU_INIT: {
700 struct kvm_vcpu_init init;
701
702 if (copy_from_user(&init, argp, sizeof(init)))
703 return -EFAULT;
704
Christoffer Dall478a8232013-11-19 17:43:19 -0800705 return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500706 }
707 case KVM_SET_ONE_REG:
708 case KVM_GET_ONE_REG: {
709 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +0200710
711 if (unlikely(!kvm_vcpu_initialized(vcpu)))
712 return -ENOEXEC;
713
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500714 if (copy_from_user(&reg, argp, sizeof(reg)))
715 return -EFAULT;
716 if (ioctl == KVM_SET_ONE_REG)
717 return kvm_arm_set_reg(vcpu, &reg);
718 else
719 return kvm_arm_get_reg(vcpu, &reg);
720 }
721 case KVM_GET_REG_LIST: {
722 struct kvm_reg_list __user *user_list = argp;
723 struct kvm_reg_list reg_list;
724 unsigned n;
725
Andre Przywarae8180dc2013-05-09 00:28:06 +0200726 if (unlikely(!kvm_vcpu_initialized(vcpu)))
727 return -ENOEXEC;
728
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500729 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
730 return -EFAULT;
731 n = reg_list.n;
732 reg_list.n = kvm_arm_num_regs(vcpu);
733 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
734 return -EFAULT;
735 if (n < reg_list.n)
736 return -E2BIG;
737 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
738 }
739 default:
740 return -EINVAL;
741 }
742}
743
744int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
745{
746 return -EINVAL;
747}
748
Christoffer Dall3401d5462013-01-23 13:18:04 -0500749static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
750 struct kvm_arm_device_addr *dev_addr)
751{
Christoffer Dall330690c2013-01-21 19:36:13 -0500752 unsigned long dev_id, type;
753
754 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
755 KVM_ARM_DEVICE_ID_SHIFT;
756 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
757 KVM_ARM_DEVICE_TYPE_SHIFT;
758
759 switch (dev_id) {
760 case KVM_ARM_DEVICE_VGIC_V2:
761 if (!vgic_present)
762 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -0700763 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -0500764 default:
765 return -ENODEV;
766 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500767}
768
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500769long kvm_arch_vm_ioctl(struct file *filp,
770 unsigned int ioctl, unsigned long arg)
771{
Christoffer Dall3401d5462013-01-23 13:18:04 -0500772 struct kvm *kvm = filp->private_data;
773 void __user *argp = (void __user *)arg;
774
775 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500776 case KVM_CREATE_IRQCHIP: {
777 if (vgic_present)
778 return kvm_vgic_create(kvm);
779 else
780 return -ENXIO;
781 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500782 case KVM_ARM_SET_DEVICE_ADDR: {
783 struct kvm_arm_device_addr dev_addr;
784
785 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
786 return -EFAULT;
787 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
788 }
Anup Patel42c4e0c2013-09-30 14:20:07 +0530789 case KVM_ARM_PREFERRED_TARGET: {
790 int err;
791 struct kvm_vcpu_init init;
792
793 err = kvm_vcpu_preferred_target(&init);
794 if (err)
795 return err;
796
797 if (copy_to_user(argp, &init, sizeof(init)))
798 return -EFAULT;
799
800 return 0;
801 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500802 default:
803 return -EINVAL;
804 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500805}
806
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100807static void cpu_init_hyp_mode(void *dummy)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500808{
Marc Zyngierdac288f2013-05-14 12:11:37 +0100809 phys_addr_t boot_pgd_ptr;
810 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500811 unsigned long hyp_stack_ptr;
812 unsigned long stack_page;
813 unsigned long vector_ptr;
814
815 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100816 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500817
Marc Zyngierdac288f2013-05-14 12:11:37 +0100818 boot_pgd_ptr = kvm_mmu_get_boot_httbr();
819 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +0100820 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500821 hyp_stack_ptr = stack_page + PAGE_SIZE;
822 vector_ptr = (unsigned long)__kvm_hyp_vector;
823
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100824 __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500825}
826
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100827static int hyp_init_cpu_notify(struct notifier_block *self,
828 unsigned long action, void *cpu)
829{
830 switch (action) {
831 case CPU_STARTING:
832 case CPU_STARTING_FROZEN:
833 cpu_init_hyp_mode(NULL);
834 break;
835 }
836
837 return NOTIFY_OK;
838}
839
840static struct notifier_block hyp_init_cpu_nb = {
841 .notifier_call = hyp_init_cpu_notify,
842};
843
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +0100844#ifdef CONFIG_CPU_PM
845static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
846 unsigned long cmd,
847 void *v)
848{
Marc Zyngierb20c9f22014-02-26 18:47:36 +0000849 if (cmd == CPU_PM_EXIT &&
850 __hyp_get_vectors() == hyp_default_vectors) {
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +0100851 cpu_init_hyp_mode(NULL);
852 return NOTIFY_OK;
853 }
854
855 return NOTIFY_DONE;
856}
857
858static struct notifier_block hyp_init_cpu_pm_nb = {
859 .notifier_call = hyp_init_cpu_pm_notifier,
860};
861
862static void __init hyp_cpu_pm_init(void)
863{
864 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
865}
866#else
867static inline void hyp_cpu_pm_init(void)
868{
869}
870#endif
871
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500872/**
873 * Inits Hyp-mode on all online CPUs
874 */
875static int init_hyp_mode(void)
876{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500877 int cpu;
878 int err = 0;
879
880 /*
881 * Allocate Hyp PGD and setup Hyp identity mapping
882 */
883 err = kvm_mmu_init();
884 if (err)
885 goto out_err;
886
887 /*
888 * It is probably enough to obtain the default on one
889 * CPU. It's unlikely to be different on the others.
890 */
891 hyp_default_vectors = __hyp_get_vectors();
892
893 /*
894 * Allocate stack pages for Hypervisor-mode
895 */
896 for_each_possible_cpu(cpu) {
897 unsigned long stack_page;
898
899 stack_page = __get_free_page(GFP_KERNEL);
900 if (!stack_page) {
901 err = -ENOMEM;
902 goto out_free_stack_pages;
903 }
904
905 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
906 }
907
908 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500909 * Map the Hyp-code called directly from the host
910 */
911 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
912 if (err) {
913 kvm_err("Cannot map world-switch code\n");
914 goto out_free_mappings;
915 }
916
917 /*
918 * Map the Hyp stack pages
919 */
920 for_each_possible_cpu(cpu) {
921 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
922 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
923
924 if (err) {
925 kvm_err("Cannot map hyp stack\n");
926 goto out_free_mappings;
927 }
928 }
929
930 /*
Marc Zyngier3de50da2013-04-08 16:47:19 +0100931 * Map the host CPU structures
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500932 */
Marc Zyngier3de50da2013-04-08 16:47:19 +0100933 kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
934 if (!kvm_host_cpu_state) {
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500935 err = -ENOMEM;
Marc Zyngier3de50da2013-04-08 16:47:19 +0100936 kvm_err("Cannot allocate host CPU state\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500937 goto out_free_mappings;
938 }
939
940 for_each_possible_cpu(cpu) {
Marc Zyngier3de50da2013-04-08 16:47:19 +0100941 kvm_cpu_context_t *cpu_ctxt;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500942
Marc Zyngier3de50da2013-04-08 16:47:19 +0100943 cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
944 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500945
946 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +0100947 kvm_err("Cannot map host CPU state: %d\n", err);
948 goto out_free_context;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500949 }
950 }
951
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500952 /*
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100953 * Execute the init code on each CPU.
954 */
955 on_each_cpu(cpu_init_hyp_mode, NULL, 1);
956
957 /*
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500958 * Init HYP view of VGIC
959 */
960 err = kvm_vgic_hyp_init();
961 if (err)
Marc Zyngier3de50da2013-04-08 16:47:19 +0100962 goto out_free_context;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500963
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500964#ifdef CONFIG_KVM_ARM_VGIC
965 vgic_present = true;
966#endif
967
Marc Zyngier967f8422013-01-23 13:21:59 -0500968 /*
969 * Init HYP architected timer support
970 */
971 err = kvm_timer_hyp_init();
972 if (err)
973 goto out_free_mappings;
974
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100975#ifndef CONFIG_HOTPLUG_CPU
976 free_boot_hyp_pgd();
977#endif
978
Marc Zyngier210552c2013-03-05 03:18:00 +0000979 kvm_perf_init();
980
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500981 kvm_info("Hyp mode initialized successfully\n");
Marc Zyngier210552c2013-03-05 03:18:00 +0000982
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500983 return 0;
Marc Zyngier3de50da2013-04-08 16:47:19 +0100984out_free_context:
985 free_percpu(kvm_host_cpu_state);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500986out_free_mappings:
Marc Zyngier4f728272013-04-12 19:12:05 +0100987 free_hyp_pgds();
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500988out_free_stack_pages:
989 for_each_possible_cpu(cpu)
990 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
991out_err:
992 kvm_err("error initializing Hyp mode: %d\n", err);
993 return err;
994}
995
Andre Przywarad4e071c2013-04-17 12:52:01 +0200996static void check_kvm_target_cpu(void *ret)
997{
998 *(int *)ret = kvm_target_cpu();
999}
1000
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001001/**
1002 * Initialize Hyp-mode and memory mappings on all CPUs.
1003 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001004int kvm_arch_init(void *opaque)
1005{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001006 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001007 int ret, cpu;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001008
1009 if (!is_hyp_mode_available()) {
1010 kvm_err("HYP mode not available\n");
1011 return -ENODEV;
1012 }
1013
Andre Przywarad4e071c2013-04-17 12:52:01 +02001014 for_each_online_cpu(cpu) {
1015 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1016 if (ret < 0) {
1017 kvm_err("Error, CPU %d not supported!\n", cpu);
1018 return -ENODEV;
1019 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001020 }
1021
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301022 cpu_notifier_register_begin();
1023
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001024 err = init_hyp_mode();
1025 if (err)
1026 goto out_err;
1027
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301028 err = __register_cpu_notifier(&hyp_init_cpu_nb);
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001029 if (err) {
1030 kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
1031 goto out_err;
1032 }
1033
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301034 cpu_notifier_register_done();
1035
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001036 hyp_cpu_pm_init();
1037
Christoffer Dall5b3e5e52013-01-20 18:28:09 -05001038 kvm_coproc_table_init();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001039 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001040out_err:
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301041 cpu_notifier_register_done();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001042 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001043}
1044
1045/* NOP: Compiling as a module not supported */
1046void kvm_arch_exit(void)
1047{
Marc Zyngier210552c2013-03-05 03:18:00 +00001048 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001049}
1050
1051static int arm_init(void)
1052{
1053 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1054 return rc;
1055}
1056
1057module_init(arm_init);