MAINTAINERS: add git URL for KVM/ARM
[deliverable/linux.git] / arch / arm / kvm / arm.c
CommitLineData
749cf76c
CD
1/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
d157f4a5 19#include <linux/cpu.h>
1fcf7ce0 20#include <linux/cpu_pm.h>
749cf76c
CD
21#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>
86ce8535 29#include <linux/kvm.h>
749cf76c
CD
30#include <trace/events/kvm.h>
31
32#define CREATE_TRACE_POINTS
33#include "trace.h"
34
749cf76c
CD
35#include <asm/uaccess.h>
36#include <asm/ptrace.h>
37#include <asm/mman.h>
342cd0ab 38#include <asm/tlbflush.h>
5b3e5e5b 39#include <asm/cacheflush.h>
342cd0ab
CD
40#include <asm/virt.h>
41#include <asm/kvm_arm.h>
42#include <asm/kvm_asm.h>
43#include <asm/kvm_mmu.h>
f7ed45be 44#include <asm/kvm_emulate.h>
5b3e5e5b 45#include <asm/kvm_coproc.h>
aa024c2f 46#include <asm/kvm_psci.h>
910917bb 47#include <asm/sections.h>
749cf76c
CD
48
49#ifdef REQUIRES_VIRT
50__asm__(".arch_extension virt");
51#endif
52
342cd0ab 53static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
3de50da6 54static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
342cd0ab
CD
55static unsigned long hyp_default_vectors;
56
1638a12d
MZ
57/* Per-CPU variable containing the currently running vcpu. */
58static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
59
f7ed45be
CD
60/* The VMID used in the VTTBR */
61static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
20475f78
VM
62static u32 kvm_next_vmid;
63static unsigned int kvm_vmid_bits __read_mostly;
f7ed45be 64static DEFINE_SPINLOCK(kvm_vmid_lock);
342cd0ab 65
1638a12d
MZ
66static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
67{
68 BUG_ON(preemptible());
1436c1aa 69 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
1638a12d
MZ
70}
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());
1436c1aa 79 return __this_cpu_read(kvm_arm_running_vcpu);
1638a12d
MZ
80}
81
82/**
83 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
84 */
4000be42 85struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
1638a12d
MZ
86{
87 return &kvm_arm_running_vcpu;
88}
89
13a34e06 90int kvm_arch_hardware_enable(void)
749cf76c
CD
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
749cf76c
CD
100int kvm_arch_hardware_setup(void)
101{
102 return 0;
103}
104
749cf76c
CD
105void kvm_arch_check_processor_compat(void *rtn)
106{
107 *(int *)rtn = 0;
108}
109
749cf76c 110
d5d8184d
CD
111/**
112 * kvm_arch_init_vm - initializes a VM data structure
113 * @kvm: pointer to the KVM struct
114 */
749cf76c
CD
115int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
116{
d5d8184d
CD
117 int ret = 0;
118
749cf76c
CD
119 if (type)
120 return -EINVAL;
121
d5d8184d
CD
122 ret = kvm_alloc_stage2_pgd(kvm);
123 if (ret)
124 goto out_fail_alloc;
125
126 ret = create_hyp_mappings(kvm, kvm + 1);
127 if (ret)
128 goto out_free_stage2_pgd;
129
6c3d63c9 130 kvm_vgic_early_init(kvm);
a1a64387
CD
131 kvm_timer_init(kvm);
132
d5d8184d
CD
133 /* Mark the initial VMID generation invalid */
134 kvm->arch.vmid_gen = 0;
135
3caa2d8c
AP
136 /* The maximum number of VCPUs is limited by the host's GIC model */
137 kvm->arch.max_vcpus = kvm_vgic_get_max_vcpus();
138
d5d8184d
CD
139 return ret;
140out_free_stage2_pgd:
141 kvm_free_stage2_pgd(kvm);
142out_fail_alloc:
143 return ret;
749cf76c
CD
144}
145
146int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
147{
148 return VM_FAULT_SIGBUS;
149}
150
749cf76c 151
d5d8184d
CD
152/**
153 * kvm_arch_destroy_vm - destroy the VM data structure
154 * @kvm: pointer to the KVM struct
155 */
749cf76c
CD
156void kvm_arch_destroy_vm(struct kvm *kvm)
157{
158 int i;
159
d5d8184d
CD
160 kvm_free_stage2_pgd(kvm);
161
749cf76c
CD
162 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
163 if (kvm->vcpus[i]) {
164 kvm_arch_vcpu_free(kvm->vcpus[i]);
165 kvm->vcpus[i] = NULL;
166 }
167 }
c1bfb577
MZ
168
169 kvm_vgic_destroy(kvm);
749cf76c
CD
170}
171
784aa3d7 172int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
749cf76c
CD
173{
174 int r;
175 switch (ext) {
1a89dd91 176 case KVM_CAP_IRQCHIP:
d44758c0 177 case KVM_CAP_IOEVENTFD:
7330672b 178 case KVM_CAP_DEVICE_CTRL:
749cf76c
CD
179 case KVM_CAP_USER_MEMORY:
180 case KVM_CAP_SYNC_MMU:
181 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
182 case KVM_CAP_ONE_REG:
aa024c2f 183 case KVM_CAP_ARM_PSCI:
4447a208 184 case KVM_CAP_ARM_PSCI_0_2:
98047888 185 case KVM_CAP_READONLY_MEM:
ecccf0cc 186 case KVM_CAP_MP_STATE:
749cf76c
CD
187 r = 1;
188 break;
189 case KVM_CAP_COALESCED_MMIO:
190 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
191 break;
3401d546
CD
192 case KVM_CAP_ARM_SET_DEVICE_ADDR:
193 r = 1;
ca46e10f 194 break;
749cf76c
CD
195 case KVM_CAP_NR_VCPUS:
196 r = num_online_cpus();
197 break;
198 case KVM_CAP_MAX_VCPUS:
199 r = KVM_MAX_VCPUS;
200 break;
201 default:
17b1e31f 202 r = kvm_arch_dev_ioctl_check_extension(ext);
749cf76c
CD
203 break;
204 }
205 return r;
206}
207
208long kvm_arch_dev_ioctl(struct file *filp,
209 unsigned int ioctl, unsigned long arg)
210{
211 return -EINVAL;
212}
213
749cf76c
CD
214
215struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
216{
217 int err;
218 struct kvm_vcpu *vcpu;
219
716139df
CD
220 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
221 err = -EBUSY;
222 goto out;
223 }
224
3caa2d8c
AP
225 if (id >= kvm->arch.max_vcpus) {
226 err = -EINVAL;
227 goto out;
228 }
229
749cf76c
CD
230 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
231 if (!vcpu) {
232 err = -ENOMEM;
233 goto out;
234 }
235
236 err = kvm_vcpu_init(vcpu, kvm, id);
237 if (err)
238 goto free_vcpu;
239
d5d8184d
CD
240 err = create_hyp_mappings(vcpu, vcpu + 1);
241 if (err)
242 goto vcpu_uninit;
243
749cf76c 244 return vcpu;
d5d8184d
CD
245vcpu_uninit:
246 kvm_vcpu_uninit(vcpu);
749cf76c
CD
247free_vcpu:
248 kmem_cache_free(kvm_vcpu_cache, vcpu);
249out:
250 return ERR_PTR(err);
251}
252
31928aa5 253void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
749cf76c 254{
6c3d63c9 255 kvm_vgic_vcpu_early_init(vcpu);
749cf76c
CD
256}
257
258void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
259{
d5d8184d 260 kvm_mmu_free_memory_caches(vcpu);
967f8427 261 kvm_timer_vcpu_terminate(vcpu);
c1bfb577 262 kvm_vgic_vcpu_destroy(vcpu);
d5d8184d 263 kmem_cache_free(kvm_vcpu_cache, vcpu);
749cf76c
CD
264}
265
266void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
267{
268 kvm_arch_vcpu_free(vcpu);
269}
270
271int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
272{
1a748478 273 return kvm_timer_should_fire(vcpu);
749cf76c
CD
274}
275
d35268da
CD
276void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
277{
278 kvm_timer_schedule(vcpu);
279}
280
281void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
282{
283 kvm_timer_unschedule(vcpu);
284}
285
749cf76c
CD
286int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
287{
f7ed45be
CD
288 /* Force users to call KVM_ARM_VCPU_INIT */
289 vcpu->arch.target = -1;
f7fa034d 290 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1a89dd91 291
967f8427
MZ
292 /* Set up the timer */
293 kvm_timer_vcpu_init(vcpu);
294
84e690bf
AB
295 kvm_arm_reset_debug_ptr(vcpu);
296
749cf76c
CD
297 return 0;
298}
299
749cf76c
CD
300void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
301{
86ce8535 302 vcpu->cpu = cpu;
3de50da6 303 vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
5b3e5e5b 304
1638a12d 305 kvm_arm_set_running_vcpu(vcpu);
749cf76c
CD
306}
307
308void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
309{
e9b152cb
CD
310 /*
311 * The arch-generic KVM code expects the cpu field of a vcpu to be -1
312 * if the vcpu is no longer assigned to a cpu. This is used for the
313 * optimized make_all_cpus_request path.
314 */
315 vcpu->cpu = -1;
316
1638a12d 317 kvm_arm_set_running_vcpu(NULL);
749cf76c
CD
318}
319
749cf76c
CD
320int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
321 struct kvm_mp_state *mp_state)
322{
3781528e 323 if (vcpu->arch.power_off)
ecccf0cc
AB
324 mp_state->mp_state = KVM_MP_STATE_STOPPED;
325 else
326 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
327
328 return 0;
749cf76c
CD
329}
330
331int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
332 struct kvm_mp_state *mp_state)
333{
ecccf0cc
AB
334 switch (mp_state->mp_state) {
335 case KVM_MP_STATE_RUNNABLE:
3781528e 336 vcpu->arch.power_off = false;
ecccf0cc
AB
337 break;
338 case KVM_MP_STATE_STOPPED:
3781528e 339 vcpu->arch.power_off = true;
ecccf0cc
AB
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 return 0;
749cf76c
CD
346}
347
5b3e5e5b
CD
348/**
349 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
350 * @v: The VCPU pointer
351 *
352 * If the guest CPU is not waiting for interrupts or an interrupt line is
353 * asserted, the CPU is by definition runnable.
354 */
749cf76c
CD
355int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
356{
4f5f1dc0 357 return ((!!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v))
3b92830a 358 && !v->arch.power_off && !v->arch.pause);
749cf76c
CD
359}
360
f7ed45be
CD
361/* Just ensure a guest exit from a particular CPU */
362static void exit_vm_noop(void *info)
363{
364}
365
366void force_vm_exit(const cpumask_t *mask)
367{
368 smp_call_function_many(mask, exit_vm_noop, NULL, true);
369}
370
371/**
372 * need_new_vmid_gen - check that the VMID is still valid
373 * @kvm: The VM's VMID to checkt
374 *
375 * return true if there is a new generation of VMIDs being used
376 *
377 * The hardware supports only 256 values with the value zero reserved for the
378 * host, so we check if an assigned value belongs to a previous generation,
379 * which which requires us to assign a new value. If we're the first to use a
380 * VMID for the new generation, we must flush necessary caches and TLBs on all
381 * CPUs.
382 */
383static bool need_new_vmid_gen(struct kvm *kvm)
384{
385 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
386}
387
388/**
389 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
390 * @kvm The guest that we are about to run
391 *
392 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
393 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
394 * caches and TLBs.
395 */
396static void update_vttbr(struct kvm *kvm)
397{
398 phys_addr_t pgd_phys;
399 u64 vmid;
400
401 if (!need_new_vmid_gen(kvm))
402 return;
403
404 spin_lock(&kvm_vmid_lock);
405
406 /*
407 * We need to re-check the vmid_gen here to ensure that if another vcpu
408 * already allocated a valid vmid for this vm, then this vcpu should
409 * use the same vmid.
410 */
411 if (!need_new_vmid_gen(kvm)) {
412 spin_unlock(&kvm_vmid_lock);
413 return;
414 }
415
416 /* First user of a new VMID generation? */
417 if (unlikely(kvm_next_vmid == 0)) {
418 atomic64_inc(&kvm_vmid_gen);
419 kvm_next_vmid = 1;
420
421 /*
422 * On SMP we know no other CPUs can use this CPU's or each
423 * other's VMID after force_vm_exit returns since the
424 * kvm_vmid_lock blocks them from reentry to the guest.
425 */
426 force_vm_exit(cpu_all_mask);
427 /*
428 * Now broadcast TLB + ICACHE invalidation over the inner
429 * shareable domain to make sure all data structures are
430 * clean.
431 */
432 kvm_call_hyp(__kvm_flush_vm_context);
433 }
434
435 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
436 kvm->arch.vmid = kvm_next_vmid;
437 kvm_next_vmid++;
20475f78 438 kvm_next_vmid &= (1 << kvm_vmid_bits) - 1;
f7ed45be
CD
439
440 /* update vttbr to be used with the new vmid */
38f791a4 441 pgd_phys = virt_to_phys(kvm_get_hwpgd(kvm));
dbff124e 442 BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
20475f78 443 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits);
dbff124e 444 kvm->arch.vttbr = pgd_phys | vmid;
f7ed45be
CD
445
446 spin_unlock(&kvm_vmid_lock);
447}
448
f7ed45be
CD
449static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
450{
05971120 451 struct kvm *kvm = vcpu->kvm;
e1ba0207
CD
452 int ret;
453
f7ed45be
CD
454 if (likely(vcpu->arch.has_run_once))
455 return 0;
456
457 vcpu->arch.has_run_once = true;
aa024c2f 458
01ac5e34 459 /*
6d3cfbe2
PM
460 * Map the VGIC hardware resources before running a vcpu the first
461 * time on this VM.
01ac5e34 462 */
c2f58514 463 if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) {
05971120 464 ret = kvm_vgic_map_resources(kvm);
01ac5e34
MZ
465 if (ret)
466 return ret;
467 }
468
05971120
CD
469 /*
470 * Enable the arch timers only if we have an in-kernel VGIC
471 * and it has been properly initialized, since we cannot handle
472 * interrupts from the virtual timer with a userspace gic.
473 */
474 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
475 kvm_timer_enable(kvm);
476
f7ed45be
CD
477 return 0;
478}
479
c1426e4c
EA
480bool kvm_arch_intc_initialized(struct kvm *kvm)
481{
482 return vgic_initialized(kvm);
483}
484
3b92830a
EA
485static void kvm_arm_halt_guest(struct kvm *kvm) __maybe_unused;
486static void kvm_arm_resume_guest(struct kvm *kvm) __maybe_unused;
487
488static void kvm_arm_halt_guest(struct kvm *kvm)
489{
490 int i;
491 struct kvm_vcpu *vcpu;
492
493 kvm_for_each_vcpu(i, vcpu, kvm)
494 vcpu->arch.pause = true;
495 force_vm_exit(cpu_all_mask);
496}
497
498static void kvm_arm_resume_guest(struct kvm *kvm)
499{
500 int i;
501 struct kvm_vcpu *vcpu;
502
503 kvm_for_each_vcpu(i, vcpu, kvm) {
504 wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
505
506 vcpu->arch.pause = false;
507 wake_up_interruptible(wq);
508 }
509}
510
3781528e 511static void vcpu_sleep(struct kvm_vcpu *vcpu)
aa024c2f
MZ
512{
513 wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
514
3b92830a
EA
515 wait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
516 (!vcpu->arch.pause)));
aa024c2f
MZ
517}
518
e8180dca
AP
519static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
520{
521 return vcpu->arch.target >= 0;
522}
523
f7ed45be
CD
524/**
525 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
526 * @vcpu: The VCPU pointer
527 * @run: The kvm_run structure pointer used for userspace state exchange
528 *
529 * This function is called through the VCPU_RUN ioctl called from user space. It
530 * will execute VM code in a loop until the time slice for the process is used
531 * or some emulation is needed from user space in which case the function will
532 * return with return value 0 and with the kvm_run structure filled in with the
533 * required data for the requested emulation.
534 */
749cf76c
CD
535int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
536{
f7ed45be
CD
537 int ret;
538 sigset_t sigsaved;
539
e8180dca 540 if (unlikely(!kvm_vcpu_initialized(vcpu)))
f7ed45be
CD
541 return -ENOEXEC;
542
543 ret = kvm_vcpu_first_run_init(vcpu);
544 if (ret)
545 return ret;
546
45e96ea6
CD
547 if (run->exit_reason == KVM_EXIT_MMIO) {
548 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
549 if (ret)
550 return ret;
551 }
552
f7ed45be
CD
553 if (vcpu->sigset_active)
554 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
555
556 ret = 1;
557 run->exit_reason = KVM_EXIT_UNKNOWN;
558 while (ret > 0) {
559 /*
560 * Check conditions before entering the guest
561 */
562 cond_resched();
563
564 update_vttbr(vcpu->kvm);
565
3b92830a 566 if (vcpu->arch.power_off || vcpu->arch.pause)
3781528e 567 vcpu_sleep(vcpu);
aa024c2f 568
abdf5843
MZ
569 /*
570 * Preparing the interrupts to be injected also
571 * involves poking the GIC, which must be done in a
572 * non-preemptible context.
573 */
1b3d546d 574 preempt_disable();
7e16aa81 575 kvm_timer_flush_hwstate(vcpu);
abdf5843
MZ
576 kvm_vgic_flush_hwstate(vcpu);
577
f7ed45be
CD
578 local_irq_disable();
579
580 /*
581 * Re-check atomic conditions
582 */
583 if (signal_pending(current)) {
584 ret = -EINTR;
585 run->exit_reason = KVM_EXIT_INTR;
586 }
587
101d3da0 588 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
3b92830a 589 vcpu->arch.power_off || vcpu->arch.pause) {
f7ed45be 590 local_irq_enable();
4b4b4512 591 kvm_timer_sync_hwstate(vcpu);
1a89dd91 592 kvm_vgic_sync_hwstate(vcpu);
abdf5843 593 preempt_enable();
f7ed45be
CD
594 continue;
595 }
596
56c7f5e7
AB
597 kvm_arm_setup_debug(vcpu);
598
f7ed45be
CD
599 /**************************************************************
600 * Enter the guest
601 */
602 trace_kvm_entry(*vcpu_pc(vcpu));
ccf73aaf 603 __kvm_guest_enter();
f7ed45be
CD
604 vcpu->mode = IN_GUEST_MODE;
605
606 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
607
608 vcpu->mode = OUTSIDE_GUEST_MODE;
b19e6892 609 vcpu->stat.exits++;
1b3d546d
CD
610 /*
611 * Back from guest
612 *************************************************************/
613
56c7f5e7
AB
614 kvm_arm_clear_debug(vcpu);
615
f7ed45be
CD
616 /*
617 * We may have taken a host interrupt in HYP mode (ie
618 * while executing the guest). This interrupt is still
619 * pending, as we haven't serviced it yet!
620 *
621 * We're now back in SVC mode, with interrupts
622 * disabled. Enabling the interrupts now will have
623 * the effect of taking the interrupt again, in SVC
624 * mode this time.
625 */
626 local_irq_enable();
627
628 /*
1b3d546d
CD
629 * We do local_irq_enable() before calling kvm_guest_exit() so
630 * that if a timer interrupt hits while running the guest we
631 * account that tick as being spent in the guest. We enable
632 * preemption after calling kvm_guest_exit() so that if we get
633 * preempted we make sure ticks after that is not counted as
634 * guest time.
635 */
636 kvm_guest_exit();
b5905dc1 637 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1b3d546d 638
4b4b4512
CD
639 /*
640 * We must sync the timer state before the vgic state so that
641 * the vgic can properly sample the updated state of the
642 * interrupt line.
643 */
644 kvm_timer_sync_hwstate(vcpu);
645
1a89dd91 646 kvm_vgic_sync_hwstate(vcpu);
abdf5843
MZ
647
648 preempt_enable();
649
f7ed45be
CD
650 ret = handle_exit(vcpu, run, ret);
651 }
652
653 if (vcpu->sigset_active)
654 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
655 return ret;
749cf76c
CD
656}
657
86ce8535
CD
658static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
659{
660 int bit_index;
661 bool set;
662 unsigned long *ptr;
663
664 if (number == KVM_ARM_IRQ_CPU_IRQ)
665 bit_index = __ffs(HCR_VI);
666 else /* KVM_ARM_IRQ_CPU_FIQ */
667 bit_index = __ffs(HCR_VF);
668
669 ptr = (unsigned long *)&vcpu->arch.irq_lines;
670 if (level)
671 set = test_and_set_bit(bit_index, ptr);
672 else
673 set = test_and_clear_bit(bit_index, ptr);
674
675 /*
676 * If we didn't change anything, no need to wake up or kick other CPUs
677 */
678 if (set == level)
679 return 0;
680
681 /*
682 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
683 * trigger a world-switch round on the running physical CPU to set the
684 * virtual IRQ/FIQ fields in the HCR appropriately.
685 */
686 kvm_vcpu_kick(vcpu);
687
688 return 0;
689}
690
79558f11
AG
691int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
692 bool line_status)
86ce8535
CD
693{
694 u32 irq = irq_level->irq;
695 unsigned int irq_type, vcpu_idx, irq_num;
696 int nrcpus = atomic_read(&kvm->online_vcpus);
697 struct kvm_vcpu *vcpu = NULL;
698 bool level = irq_level->level;
699
700 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
701 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
702 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
703
704 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
705
5863c2ce
MZ
706 switch (irq_type) {
707 case KVM_ARM_IRQ_TYPE_CPU:
708 if (irqchip_in_kernel(kvm))
709 return -ENXIO;
86ce8535 710
5863c2ce
MZ
711 if (vcpu_idx >= nrcpus)
712 return -EINVAL;
86ce8535 713
5863c2ce
MZ
714 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
715 if (!vcpu)
716 return -EINVAL;
86ce8535 717
5863c2ce
MZ
718 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
719 return -EINVAL;
720
721 return vcpu_interrupt_line(vcpu, irq_num, level);
722 case KVM_ARM_IRQ_TYPE_PPI:
723 if (!irqchip_in_kernel(kvm))
724 return -ENXIO;
725
726 if (vcpu_idx >= nrcpus)
727 return -EINVAL;
728
729 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
730 if (!vcpu)
731 return -EINVAL;
732
733 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
734 return -EINVAL;
86ce8535 735
5863c2ce
MZ
736 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
737 case KVM_ARM_IRQ_TYPE_SPI:
738 if (!irqchip_in_kernel(kvm))
739 return -ENXIO;
740
fd1d0ddf 741 if (irq_num < VGIC_NR_PRIVATE_IRQS)
5863c2ce
MZ
742 return -EINVAL;
743
744 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
745 }
746
747 return -EINVAL;
86ce8535
CD
748}
749
f7fa034d
CD
750static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
751 const struct kvm_vcpu_init *init)
752{
753 unsigned int i;
754 int phys_target = kvm_target_cpu();
755
756 if (init->target != phys_target)
757 return -EINVAL;
758
759 /*
760 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
761 * use the same target.
762 */
763 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
764 return -EINVAL;
765
766 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
767 for (i = 0; i < sizeof(init->features) * 8; i++) {
768 bool set = (init->features[i / 32] & (1 << (i % 32)));
769
770 if (set && i >= KVM_VCPU_MAX_FEATURES)
771 return -ENOENT;
772
773 /*
774 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
775 * use the same feature set.
776 */
777 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
778 test_bit(i, vcpu->arch.features) != set)
779 return -EINVAL;
780
781 if (set)
782 set_bit(i, vcpu->arch.features);
783 }
784
785 vcpu->arch.target = phys_target;
786
787 /* Now we know what it is, we can reset it. */
788 return kvm_reset_vcpu(vcpu);
789}
790
791
478a8237
CD
792static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
793 struct kvm_vcpu_init *init)
794{
795 int ret;
796
797 ret = kvm_vcpu_set_target(vcpu, init);
798 if (ret)
799 return ret;
800
957db105
CD
801 /*
802 * Ensure a rebooted VM will fault in RAM pages and detect if the
803 * guest MMU is turned off and flush the caches as needed.
804 */
805 if (vcpu->arch.has_run_once)
806 stage2_unmap_vm(vcpu->kvm);
807
b856a591
CD
808 vcpu_reset_hcr(vcpu);
809
478a8237 810 /*
3781528e 811 * Handle the "start in power-off" case.
478a8237 812 */
03f1d4c1 813 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
3781528e 814 vcpu->arch.power_off = true;
3ad8b3de 815 else
3781528e 816 vcpu->arch.power_off = false;
478a8237
CD
817
818 return 0;
819}
820
749cf76c
CD
821long kvm_arch_vcpu_ioctl(struct file *filp,
822 unsigned int ioctl, unsigned long arg)
823{
824 struct kvm_vcpu *vcpu = filp->private_data;
825 void __user *argp = (void __user *)arg;
826
827 switch (ioctl) {
828 case KVM_ARM_VCPU_INIT: {
829 struct kvm_vcpu_init init;
830
831 if (copy_from_user(&init, argp, sizeof(init)))
832 return -EFAULT;
833
478a8237 834 return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
749cf76c
CD
835 }
836 case KVM_SET_ONE_REG:
837 case KVM_GET_ONE_REG: {
838 struct kvm_one_reg reg;
e8180dca
AP
839
840 if (unlikely(!kvm_vcpu_initialized(vcpu)))
841 return -ENOEXEC;
842
749cf76c
CD
843 if (copy_from_user(&reg, argp, sizeof(reg)))
844 return -EFAULT;
845 if (ioctl == KVM_SET_ONE_REG)
846 return kvm_arm_set_reg(vcpu, &reg);
847 else
848 return kvm_arm_get_reg(vcpu, &reg);
849 }
850 case KVM_GET_REG_LIST: {
851 struct kvm_reg_list __user *user_list = argp;
852 struct kvm_reg_list reg_list;
853 unsigned n;
854
e8180dca
AP
855 if (unlikely(!kvm_vcpu_initialized(vcpu)))
856 return -ENOEXEC;
857
749cf76c
CD
858 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
859 return -EFAULT;
860 n = reg_list.n;
861 reg_list.n = kvm_arm_num_regs(vcpu);
862 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
863 return -EFAULT;
864 if (n < reg_list.n)
865 return -E2BIG;
866 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
867 }
868 default:
869 return -EINVAL;
870 }
871}
872
53c810c3
MS
873/**
874 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
875 * @kvm: kvm instance
876 * @log: slot id and address to which we copy the log
877 *
878 * Steps 1-4 below provide general overview of dirty page logging. See
879 * kvm_get_dirty_log_protect() function description for additional details.
880 *
881 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
882 * always flush the TLB (step 4) even if previous step failed and the dirty
883 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
884 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
885 * writes will be marked dirty for next log read.
886 *
887 * 1. Take a snapshot of the bit and clear it if needed.
888 * 2. Write protect the corresponding page.
889 * 3. Copy the snapshot to the userspace.
890 * 4. Flush TLB's if needed.
891 */
749cf76c
CD
892int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
893{
53c810c3
MS
894 bool is_dirty = false;
895 int r;
896
897 mutex_lock(&kvm->slots_lock);
898
899 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
900
901 if (is_dirty)
902 kvm_flush_remote_tlbs(kvm);
903
904 mutex_unlock(&kvm->slots_lock);
905 return r;
749cf76c
CD
906}
907
3401d546
CD
908static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
909 struct kvm_arm_device_addr *dev_addr)
910{
330690cd
CD
911 unsigned long dev_id, type;
912
913 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
914 KVM_ARM_DEVICE_ID_SHIFT;
915 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
916 KVM_ARM_DEVICE_TYPE_SHIFT;
917
918 switch (dev_id) {
919 case KVM_ARM_DEVICE_VGIC_V2:
ce01e4e8 920 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
330690cd
CD
921 default:
922 return -ENODEV;
923 }
3401d546
CD
924}
925
749cf76c
CD
926long kvm_arch_vm_ioctl(struct file *filp,
927 unsigned int ioctl, unsigned long arg)
928{
3401d546
CD
929 struct kvm *kvm = filp->private_data;
930 void __user *argp = (void __user *)arg;
931
932 switch (ioctl) {
5863c2ce 933 case KVM_CREATE_IRQCHIP: {
69ff5c61 934 return kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
5863c2ce 935 }
3401d546
CD
936 case KVM_ARM_SET_DEVICE_ADDR: {
937 struct kvm_arm_device_addr dev_addr;
938
939 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
940 return -EFAULT;
941 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
942 }
42c4e0c7
AP
943 case KVM_ARM_PREFERRED_TARGET: {
944 int err;
945 struct kvm_vcpu_init init;
946
947 err = kvm_vcpu_preferred_target(&init);
948 if (err)
949 return err;
950
951 if (copy_to_user(argp, &init, sizeof(init)))
952 return -EFAULT;
953
954 return 0;
955 }
3401d546
CD
956 default:
957 return -EINVAL;
958 }
749cf76c
CD
959}
960
d157f4a5 961static void cpu_init_hyp_mode(void *dummy)
342cd0ab 962{
dac288f7
MZ
963 phys_addr_t boot_pgd_ptr;
964 phys_addr_t pgd_ptr;
342cd0ab
CD
965 unsigned long hyp_stack_ptr;
966 unsigned long stack_page;
967 unsigned long vector_ptr;
968
969 /* Switch from the HYP stub to our own HYP init vector */
5a677ce0 970 __hyp_set_vectors(kvm_get_idmap_vector());
342cd0ab 971
dac288f7
MZ
972 boot_pgd_ptr = kvm_mmu_get_boot_httbr();
973 pgd_ptr = kvm_mmu_get_httbr();
1436c1aa 974 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
342cd0ab
CD
975 hyp_stack_ptr = stack_page + PAGE_SIZE;
976 vector_ptr = (unsigned long)__kvm_hyp_vector;
977
5a677ce0 978 __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
56c7f5e7
AB
979
980 kvm_arm_init_debug();
342cd0ab
CD
981}
982
d157f4a5
MZ
983static int hyp_init_cpu_notify(struct notifier_block *self,
984 unsigned long action, void *cpu)
985{
986 switch (action) {
987 case CPU_STARTING:
988 case CPU_STARTING_FROZEN:
37a34ac1
VM
989 if (__hyp_get_vectors() == hyp_default_vectors)
990 cpu_init_hyp_mode(NULL);
d157f4a5
MZ
991 break;
992 }
993
994 return NOTIFY_OK;
342cd0ab
CD
995}
996
d157f4a5
MZ
997static struct notifier_block hyp_init_cpu_nb = {
998 .notifier_call = hyp_init_cpu_notify,
999};
1000
1fcf7ce0
LP
1001#ifdef CONFIG_CPU_PM
1002static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1003 unsigned long cmd,
1004 void *v)
1005{
b20c9f29
MZ
1006 if (cmd == CPU_PM_EXIT &&
1007 __hyp_get_vectors() == hyp_default_vectors) {
1fcf7ce0
LP
1008 cpu_init_hyp_mode(NULL);
1009 return NOTIFY_OK;
1010 }
1011
1012 return NOTIFY_DONE;
1013}
1014
1015static struct notifier_block hyp_init_cpu_pm_nb = {
1016 .notifier_call = hyp_init_cpu_pm_notifier,
1017};
1018
1019static void __init hyp_cpu_pm_init(void)
1020{
1021 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1022}
1023#else
1024static inline void hyp_cpu_pm_init(void)
1025{
1026}
1027#endif
1028
342cd0ab
CD
1029/**
1030 * Inits Hyp-mode on all online CPUs
1031 */
1032static int init_hyp_mode(void)
1033{
342cd0ab
CD
1034 int cpu;
1035 int err = 0;
1036
1037 /*
1038 * Allocate Hyp PGD and setup Hyp identity mapping
1039 */
1040 err = kvm_mmu_init();
1041 if (err)
1042 goto out_err;
1043
1044 /*
1045 * It is probably enough to obtain the default on one
1046 * CPU. It's unlikely to be different on the others.
1047 */
1048 hyp_default_vectors = __hyp_get_vectors();
1049
1050 /*
1051 * Allocate stack pages for Hypervisor-mode
1052 */
1053 for_each_possible_cpu(cpu) {
1054 unsigned long stack_page;
1055
1056 stack_page = __get_free_page(GFP_KERNEL);
1057 if (!stack_page) {
1058 err = -ENOMEM;
1059 goto out_free_stack_pages;
1060 }
1061
1062 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1063 }
1064
342cd0ab
CD
1065 /*
1066 * Map the Hyp-code called directly from the host
1067 */
1068 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
1069 if (err) {
1070 kvm_err("Cannot map world-switch code\n");
1071 goto out_free_mappings;
1072 }
1073
910917bb
MZ
1074 err = create_hyp_mappings(__start_rodata, __end_rodata);
1075 if (err) {
1076 kvm_err("Cannot map rodata section\n");
1077 goto out_free_mappings;
1078 }
1079
342cd0ab
CD
1080 /*
1081 * Map the Hyp stack pages
1082 */
1083 for_each_possible_cpu(cpu) {
1084 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
1085 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
1086
1087 if (err) {
1088 kvm_err("Cannot map hyp stack\n");
1089 goto out_free_mappings;
1090 }
1091 }
1092
1093 /*
3de50da6 1094 * Map the host CPU structures
342cd0ab 1095 */
3de50da6
MZ
1096 kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
1097 if (!kvm_host_cpu_state) {
342cd0ab 1098 err = -ENOMEM;
3de50da6 1099 kvm_err("Cannot allocate host CPU state\n");
342cd0ab
CD
1100 goto out_free_mappings;
1101 }
1102
1103 for_each_possible_cpu(cpu) {
3de50da6 1104 kvm_cpu_context_t *cpu_ctxt;
342cd0ab 1105
3de50da6
MZ
1106 cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
1107 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
342cd0ab
CD
1108
1109 if (err) {
3de50da6
MZ
1110 kvm_err("Cannot map host CPU state: %d\n", err);
1111 goto out_free_context;
342cd0ab
CD
1112 }
1113 }
1114
d157f4a5
MZ
1115 /*
1116 * Execute the init code on each CPU.
1117 */
1118 on_each_cpu(cpu_init_hyp_mode, NULL, 1);
1119
1a89dd91
MZ
1120 /*
1121 * Init HYP view of VGIC
1122 */
1123 err = kvm_vgic_hyp_init();
1124 if (err)
3de50da6 1125 goto out_free_context;
1a89dd91 1126
967f8427
MZ
1127 /*
1128 * Init HYP architected timer support
1129 */
1130 err = kvm_timer_hyp_init();
1131 if (err)
399ea0f6 1132 goto out_free_context;
967f8427 1133
d157f4a5
MZ
1134#ifndef CONFIG_HOTPLUG_CPU
1135 free_boot_hyp_pgd();
1136#endif
1137
210552c1
MZ
1138 kvm_perf_init();
1139
20475f78
VM
1140 /* set size of VMID supported by CPU */
1141 kvm_vmid_bits = kvm_get_vmid_bits();
1142 kvm_info("%d-bit VMID\n", kvm_vmid_bits);
1143
342cd0ab 1144 kvm_info("Hyp mode initialized successfully\n");
210552c1 1145
342cd0ab 1146 return 0;
3de50da6
MZ
1147out_free_context:
1148 free_percpu(kvm_host_cpu_state);
342cd0ab 1149out_free_mappings:
4f728276 1150 free_hyp_pgds();
342cd0ab
CD
1151out_free_stack_pages:
1152 for_each_possible_cpu(cpu)
1153 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1154out_err:
1155 kvm_err("error initializing Hyp mode: %d\n", err);
1156 return err;
1157}
1158
d4e071ce
AP
1159static void check_kvm_target_cpu(void *ret)
1160{
1161 *(int *)ret = kvm_target_cpu();
1162}
1163
4429fc64
AP
1164struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1165{
1166 struct kvm_vcpu *vcpu;
1167 int i;
1168
1169 mpidr &= MPIDR_HWID_BITMASK;
1170 kvm_for_each_vcpu(i, vcpu, kvm) {
1171 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1172 return vcpu;
1173 }
1174 return NULL;
1175}
1176
342cd0ab
CD
1177/**
1178 * Initialize Hyp-mode and memory mappings on all CPUs.
1179 */
749cf76c
CD
1180int kvm_arch_init(void *opaque)
1181{
342cd0ab 1182 int err;
d4e071ce 1183 int ret, cpu;
342cd0ab
CD
1184
1185 if (!is_hyp_mode_available()) {
1186 kvm_err("HYP mode not available\n");
1187 return -ENODEV;
1188 }
1189
d4e071ce
AP
1190 for_each_online_cpu(cpu) {
1191 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1192 if (ret < 0) {
1193 kvm_err("Error, CPU %d not supported!\n", cpu);
1194 return -ENODEV;
1195 }
342cd0ab
CD
1196 }
1197
8146875d
SB
1198 cpu_notifier_register_begin();
1199
342cd0ab
CD
1200 err = init_hyp_mode();
1201 if (err)
1202 goto out_err;
1203
8146875d 1204 err = __register_cpu_notifier(&hyp_init_cpu_nb);
d157f4a5
MZ
1205 if (err) {
1206 kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
1207 goto out_err;
1208 }
1209
8146875d
SB
1210 cpu_notifier_register_done();
1211
1fcf7ce0
LP
1212 hyp_cpu_pm_init();
1213
5b3e5e5b 1214 kvm_coproc_table_init();
749cf76c 1215 return 0;
342cd0ab 1216out_err:
8146875d 1217 cpu_notifier_register_done();
342cd0ab 1218 return err;
749cf76c
CD
1219}
1220
1221/* NOP: Compiling as a module not supported */
1222void kvm_arch_exit(void)
1223{
210552c1 1224 kvm_perf_teardown();
749cf76c
CD
1225}
1226
1227static int arm_init(void)
1228{
1229 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1230 return rc;
1231}
1232
1233module_init(arm_init);
This page took 0.198901 seconds and 5 git commands to generate.