blob: 60a8d3ef22ae3fe42ed128e2bd7214cfe85cdf9b [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
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +010019#include <linux/cpu_pm.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050020#include <linux/errno.h>
21#include <linux/err.h>
22#include <linux/kvm_host.h>
23#include <linux/module.h>
24#include <linux/vmalloc.h>
25#include <linux/fs.h>
26#include <linux/mman.h>
27#include <linux/sched.h>
Christoffer Dall86ce8532013-01-20 18:28:08 -050028#include <linux/kvm.h>
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +010029#include <linux/cpu.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 Zyngieraa024c22013-01-20 18:28:13 -050046#include <asm/kvm_psci.h>
Marc Zyngier29f183d2015-10-27 12:18:48 +000047#include <asm/sections.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050048
49#ifdef REQUIRES_VIRT
50__asm__(".arch_extension virt");
51#endif
52
Christoffer Dall342cd0a2013-01-20 18:28:06 -050053static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
Marc Zyngier3de50da2013-04-08 16:47:19 +010054static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050055static unsigned long hyp_default_vectors;
56
Marc Zyngier1638a122013-01-21 19:36:11 -050057/* Per-CPU variable containing the currently running vcpu. */
58static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
59
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050060/* The VMID used in the VTTBR */
61static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
62static u8 kvm_next_vmid;
63static DEFINE_SPINLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050064
Pavel Fedinf943cd92015-12-18 14:38:43 +030065static bool vgic_present;
66
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +010067static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
68
Marc Zyngier1638a122013-01-21 19:36:11 -050069static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
70{
71 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010072 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050073}
74
75/**
76 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
77 * Must be called from non-preemptible context
78 */
79struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
80{
81 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010082 return __this_cpu_read(kvm_arm_running_vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050083}
84
85/**
86 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
87 */
Will Deacon4000be42014-08-26 15:13:21 +010088struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
Marc Zyngier1638a122013-01-21 19:36:11 -050089{
90 return &kvm_arm_running_vcpu;
91}
92
Christoffer Dall749cf76c2013-01-20 18:28:06 -050093int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
94{
95 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
96}
97
Christoffer Dall749cf76c2013-01-20 18:28:06 -050098int kvm_arch_hardware_setup(void)
99{
100 return 0;
101}
102
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500103void kvm_arch_check_processor_compat(void *rtn)
104{
105 *(int *)rtn = 0;
106}
107
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500108
Christoffer Dalld5d81842013-01-20 18:28:07 -0500109/**
110 * kvm_arch_init_vm - initializes a VM data structure
111 * @kvm: pointer to the KVM struct
112 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500113int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
114{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500115 int ret = 0;
116
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500117 if (type)
118 return -EINVAL;
119
Christoffer Dalld5d81842013-01-20 18:28:07 -0500120 ret = kvm_alloc_stage2_pgd(kvm);
121 if (ret)
122 goto out_fail_alloc;
123
124 ret = create_hyp_mappings(kvm, kvm + 1);
125 if (ret)
126 goto out_free_stage2_pgd;
127
Christoffer Dalla1a64382013-11-16 10:51:25 -0800128 kvm_timer_init(kvm);
129
Christoffer Dalld5d81842013-01-20 18:28:07 -0500130 /* Mark the initial VMID generation invalid */
131 kvm->arch.vmid_gen = 0;
132
Andre Przywara3caa2d82014-06-02 16:26:01 +0200133 /* The maximum number of VCPUs is limited by the host's GIC model */
Pavel Fedinf943cd92015-12-18 14:38:43 +0300134 kvm->arch.max_vcpus = vgic_present ?
135 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
Andre Przywara3caa2d82014-06-02 16:26:01 +0200136
Christoffer Dalld5d81842013-01-20 18:28:07 -0500137 return ret;
138out_free_stage2_pgd:
139 kvm_free_stage2_pgd(kvm);
140out_fail_alloc:
141 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500142}
143
144int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
145{
146 return VM_FAULT_SIGBUS;
147}
148
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500149
Christoffer Dalld5d81842013-01-20 18:28:07 -0500150/**
151 * kvm_arch_destroy_vm - destroy the VM data structure
152 * @kvm: pointer to the KVM struct
153 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500154void kvm_arch_destroy_vm(struct kvm *kvm)
155{
156 int i;
157
Christoffer Dalld5d81842013-01-20 18:28:07 -0500158 kvm_free_stage2_pgd(kvm);
159
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500160 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
161 if (kvm->vcpus[i]) {
162 kvm_arch_vcpu_free(kvm->vcpus[i]);
163 kvm->vcpus[i] = NULL;
164 }
165 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100166
167 kvm_vgic_destroy(kvm);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500168}
169
Alexander Graf784aa3d2014-07-14 18:27:35 +0200170int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500171{
172 int r;
173 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500174 case KVM_CAP_IRQCHIP:
Pavel Fedinf943cd92015-12-18 14:38:43 +0300175 r = vgic_present;
176 break;
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000177 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100178 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500179 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:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500183 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530184 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200185 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000186 case KVM_CAP_MP_STATE:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500187 r = 1;
188 break;
189 case KVM_CAP_COALESCED_MMIO:
190 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
191 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500192 case KVM_CAP_ARM_SET_DEVICE_ADDR:
193 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100194 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500195 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:
Marc Zyngier17b1e312013-04-08 16:47:18 +0100202 r = kvm_arch_dev_ioctl_check_extension(ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500203 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
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500214
215struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
216{
217 int err;
218 struct kvm_vcpu *vcpu;
219
Christoffer Dall716139d2014-12-09 14:33:45 +0100220 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
221 err = -EBUSY;
222 goto out;
223 }
224
Andre Przywara3caa2d82014-06-02 16:26:01 +0200225 if (id >= kvm->arch.max_vcpus) {
226 err = -EINVAL;
227 goto out;
228 }
229
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500230 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
Christoffer Dalld5d81842013-01-20 18:28:07 -0500240 err = create_hyp_mappings(vcpu, vcpu + 1);
241 if (err)
242 goto vcpu_uninit;
243
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500244 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500245vcpu_uninit:
246 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500247free_vcpu:
248 kmem_cache_free(kvm_vcpu_cache, vcpu);
249out:
250 return ERR_PTR(err);
251}
252
Dominik Dingel31928aa2014-12-04 15:47:07 +0100253void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500254{
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500255}
256
257void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
258{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500259 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500260 kvm_timer_vcpu_terminate(vcpu);
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100261 kvm_vgic_vcpu_destroy(vcpu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500262 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500263}
264
265void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
266{
267 kvm_arch_vcpu_free(vcpu);
268}
269
270int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
271{
Christoffer Dall1a748472015-03-13 17:02:55 +0000272 return kvm_timer_should_fire(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500273}
274
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500275int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
276{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500277 /* Force users to call KVM_ARM_VCPU_INIT */
278 vcpu->arch.target = -1;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200279 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500280
Marc Zyngier967f8422013-01-23 13:21:59 -0500281 /* Set up the timer */
282 kvm_timer_vcpu_init(vcpu);
283
Alex Bennée9ca83082015-07-07 17:30:00 +0100284 kvm_arm_reset_debug_ptr(vcpu);
285
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500286 return 0;
287}
288
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500289void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
290{
Christoffer Dall86ce8532013-01-20 18:28:08 -0500291 vcpu->cpu = cpu;
Marc Zyngier3de50da2013-04-08 16:47:19 +0100292 vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500293
Marc Zyngier1638a122013-01-21 19:36:11 -0500294 kvm_arm_set_running_vcpu(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500295}
296
297void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
298{
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800299 /*
300 * The arch-generic KVM code expects the cpu field of a vcpu to be -1
301 * if the vcpu is no longer assigned to a cpu. This is used for the
302 * optimized make_all_cpus_request path.
303 */
304 vcpu->cpu = -1;
305
Marc Zyngier1638a122013-01-21 19:36:11 -0500306 kvm_arm_set_running_vcpu(NULL);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500307}
308
309int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
310 struct kvm_guest_debug *dbg)
311{
312 return -EINVAL;
313}
314
315
316int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
317 struct kvm_mp_state *mp_state)
318{
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000319 if (vcpu->arch.pause)
320 mp_state->mp_state = KVM_MP_STATE_STOPPED;
321 else
322 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
323
324 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500325}
326
327int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
328 struct kvm_mp_state *mp_state)
329{
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000330 switch (mp_state->mp_state) {
331 case KVM_MP_STATE_RUNNABLE:
332 vcpu->arch.pause = false;
333 break;
334 case KVM_MP_STATE_STOPPED:
335 vcpu->arch.pause = true;
336 break;
337 default:
338 return -EINVAL;
339 }
340
341 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500342}
343
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500344/**
345 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
346 * @v: The VCPU pointer
347 *
348 * If the guest CPU is not waiting for interrupts or an interrupt line is
349 * asserted, the CPU is by definition runnable.
350 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500351int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
352{
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500353 return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500354}
355
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500356/* Just ensure a guest exit from a particular CPU */
357static void exit_vm_noop(void *info)
358{
359}
360
361void force_vm_exit(const cpumask_t *mask)
362{
363 smp_call_function_many(mask, exit_vm_noop, NULL, true);
364}
365
366/**
367 * need_new_vmid_gen - check that the VMID is still valid
368 * @kvm: The VM's VMID to checkt
369 *
370 * return true if there is a new generation of VMIDs being used
371 *
372 * The hardware supports only 256 values with the value zero reserved for the
373 * host, so we check if an assigned value belongs to a previous generation,
374 * which which requires us to assign a new value. If we're the first to use a
375 * VMID for the new generation, we must flush necessary caches and TLBs on all
376 * CPUs.
377 */
378static bool need_new_vmid_gen(struct kvm *kvm)
379{
380 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
381}
382
383/**
384 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
385 * @kvm The guest that we are about to run
386 *
387 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
388 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
389 * caches and TLBs.
390 */
391static void update_vttbr(struct kvm *kvm)
392{
393 phys_addr_t pgd_phys;
394 u64 vmid;
395
396 if (!need_new_vmid_gen(kvm))
397 return;
398
399 spin_lock(&kvm_vmid_lock);
400
401 /*
402 * We need to re-check the vmid_gen here to ensure that if another vcpu
403 * already allocated a valid vmid for this vm, then this vcpu should
404 * use the same vmid.
405 */
406 if (!need_new_vmid_gen(kvm)) {
407 spin_unlock(&kvm_vmid_lock);
408 return;
409 }
410
411 /* First user of a new VMID generation? */
412 if (unlikely(kvm_next_vmid == 0)) {
413 atomic64_inc(&kvm_vmid_gen);
414 kvm_next_vmid = 1;
415
416 /*
417 * On SMP we know no other CPUs can use this CPU's or each
418 * other's VMID after force_vm_exit returns since the
419 * kvm_vmid_lock blocks them from reentry to the guest.
420 */
421 force_vm_exit(cpu_all_mask);
422 /*
423 * Now broadcast TLB + ICACHE invalidation over the inner
424 * shareable domain to make sure all data structures are
425 * clean.
426 */
427 kvm_call_hyp(__kvm_flush_vm_context);
428 }
429
430 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
431 kvm->arch.vmid = kvm_next_vmid;
432 kvm_next_vmid++;
433
434 /* update vttbr to be used with the new vmid */
Christoffer Dall38f791a2014-10-10 12:14:28 +0200435 pgd_phys = virt_to_phys(kvm_get_hwpgd(kvm));
Joel Schoppdbff1242014-07-09 11:17:04 -0500436 BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500437 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
Joel Schoppdbff1242014-07-09 11:17:04 -0500438 kvm->arch.vttbr = pgd_phys | vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500439
440 spin_unlock(&kvm_vmid_lock);
441}
442
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500443static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
444{
Christoffer Dall05971122014-12-12 21:19:23 +0100445 struct kvm *kvm = vcpu->kvm;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700446 int ret;
447
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500448 if (likely(vcpu->arch.has_run_once))
449 return 0;
450
451 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500452
453 /*
Peter Maydell6d3cfbe2014-12-04 15:02:24 +0000454 * Map the VGIC hardware resources before running a vcpu the first
455 * time on this VM.
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500456 */
Pavel Fedin26550ff2015-08-05 11:53:57 +0100457 if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) {
Christoffer Dall05971122014-12-12 21:19:23 +0100458 ret = kvm_vgic_map_resources(kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500459 if (ret)
460 return ret;
461 }
462
Christoffer Dall05971122014-12-12 21:19:23 +0100463 /*
464 * Enable the arch timers only if we have an in-kernel VGIC
465 * and it has been properly initialized, since we cannot handle
466 * interrupts from the virtual timer with a userspace gic.
467 */
468 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
469 kvm_timer_enable(kvm);
470
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500471 return 0;
472}
473
Eric Augerc1426e42015-03-04 11:14:34 +0100474bool kvm_arch_intc_initialized(struct kvm *kvm)
475{
476 return vgic_initialized(kvm);
477}
478
Marc Zyngieraa024c22013-01-20 18:28:13 -0500479static void vcpu_pause(struct kvm_vcpu *vcpu)
480{
481 wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
482
483 wait_event_interruptible(*wq, !vcpu->arch.pause);
484}
485
Andre Przywarae8180dc2013-05-09 00:28:06 +0200486static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
487{
488 return vcpu->arch.target >= 0;
489}
490
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500491/**
492 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
493 * @vcpu: The VCPU pointer
494 * @run: The kvm_run structure pointer used for userspace state exchange
495 *
496 * This function is called through the VCPU_RUN ioctl called from user space. It
497 * will execute VM code in a loop until the time slice for the process is used
498 * or some emulation is needed from user space in which case the function will
499 * return with return value 0 and with the kvm_run structure filled in with the
500 * required data for the requested emulation.
501 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500502int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
503{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500504 int ret;
505 sigset_t sigsaved;
506
Andre Przywarae8180dc2013-05-09 00:28:06 +0200507 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500508 return -ENOEXEC;
509
510 ret = kvm_vcpu_first_run_init(vcpu);
511 if (ret)
512 return ret;
513
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500514 if (run->exit_reason == KVM_EXIT_MMIO) {
515 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
516 if (ret)
517 return ret;
518 }
519
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500520 if (vcpu->sigset_active)
521 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
522
523 ret = 1;
524 run->exit_reason = KVM_EXIT_UNKNOWN;
525 while (ret > 0) {
526 /*
527 * Check conditions before entering the guest
528 */
529 cond_resched();
530
531 update_vttbr(vcpu->kvm);
532
Marc Zyngieraa024c22013-01-20 18:28:13 -0500533 if (vcpu->arch.pause)
534 vcpu_pause(vcpu);
535
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500536 kvm_vgic_flush_hwstate(vcpu);
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500537 kvm_timer_flush_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500538
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500539 local_irq_disable();
540
541 /*
542 * Re-check atomic conditions
543 */
544 if (signal_pending(current)) {
545 ret = -EINTR;
546 run->exit_reason = KVM_EXIT_INTR;
547 }
548
549 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
550 local_irq_enable();
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500551 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500552 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500553 continue;
554 }
555
Alex Shifa237192016-05-24 14:52:57 +0800556 kvm_arm_setup_debug(vcpu);
557
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500558 /**************************************************************
559 * Enter the guest
560 */
561 trace_kvm_entry(*vcpu_pc(vcpu));
Christian Borntraegercc84e652015-04-30 13:43:31 +0200562 __kvm_guest_enter();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500563 vcpu->mode = IN_GUEST_MODE;
564
565 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
566
567 vcpu->mode = OUTSIDE_GUEST_MODE;
Christian Borntraegercc84e652015-04-30 13:43:31 +0200568 __kvm_guest_exit();
Wei Huang91314cb2015-01-30 13:09:26 -0500569 trace_kvm_exit(kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500570 /*
Alex Shifa237192016-05-24 14:52:57 +0800571 * Back from guest
572 *************************************************************/
573
574 kvm_arm_clear_debug(vcpu);
575
576 /*
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500577 * We may have taken a host interrupt in HYP mode (ie
578 * while executing the guest). This interrupt is still
579 * pending, as we haven't serviced it yet!
580 *
581 * We're now back in SVC mode, with interrupts
582 * disabled. Enabling the interrupts now will have
583 * the effect of taking the interrupt again, in SVC
584 * mode this time.
585 */
586 local_irq_enable();
587
588 /*
589 * Back from guest
590 *************************************************************/
591
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500592 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500593 kvm_vgic_sync_hwstate(vcpu);
594
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500595 ret = handle_exit(vcpu, run, ret);
596 }
597
598 if (vcpu->sigset_active)
599 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
600 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500601}
602
Christoffer Dall86ce8532013-01-20 18:28:08 -0500603static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
604{
605 int bit_index;
606 bool set;
607 unsigned long *ptr;
608
609 if (number == KVM_ARM_IRQ_CPU_IRQ)
610 bit_index = __ffs(HCR_VI);
611 else /* KVM_ARM_IRQ_CPU_FIQ */
612 bit_index = __ffs(HCR_VF);
613
614 ptr = (unsigned long *)&vcpu->arch.irq_lines;
615 if (level)
616 set = test_and_set_bit(bit_index, ptr);
617 else
618 set = test_and_clear_bit(bit_index, ptr);
619
620 /*
621 * If we didn't change anything, no need to wake up or kick other CPUs
622 */
623 if (set == level)
624 return 0;
625
626 /*
627 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
628 * trigger a world-switch round on the running physical CPU to set the
629 * virtual IRQ/FIQ fields in the HCR appropriately.
630 */
631 kvm_vcpu_kick(vcpu);
632
633 return 0;
634}
635
Alexander Graf79558f12013-04-16 19:21:41 +0200636int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
637 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500638{
639 u32 irq = irq_level->irq;
640 unsigned int irq_type, vcpu_idx, irq_num;
641 int nrcpus = atomic_read(&kvm->online_vcpus);
642 struct kvm_vcpu *vcpu = NULL;
643 bool level = irq_level->level;
644
645 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
646 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
647 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
648
649 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
650
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500651 switch (irq_type) {
652 case KVM_ARM_IRQ_TYPE_CPU:
653 if (irqchip_in_kernel(kvm))
654 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500655
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500656 if (vcpu_idx >= nrcpus)
657 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500658
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500659 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
660 if (!vcpu)
661 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500662
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500663 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
664 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500665
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500666 return vcpu_interrupt_line(vcpu, irq_num, level);
667 case KVM_ARM_IRQ_TYPE_PPI:
668 if (!irqchip_in_kernel(kvm))
669 return -ENXIO;
670
671 if (vcpu_idx >= nrcpus)
672 return -EINVAL;
673
674 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
675 if (!vcpu)
676 return -EINVAL;
677
678 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
679 return -EINVAL;
680
681 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
682 case KVM_ARM_IRQ_TYPE_SPI:
683 if (!irqchip_in_kernel(kvm))
684 return -ENXIO;
685
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100686 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500687 return -EINVAL;
688
689 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
690 }
691
692 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500693}
694
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200695static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
696 const struct kvm_vcpu_init *init)
697{
698 unsigned int i;
699 int phys_target = kvm_target_cpu();
700
701 if (init->target != phys_target)
702 return -EINVAL;
703
704 /*
705 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
706 * use the same target.
707 */
708 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
709 return -EINVAL;
710
711 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
712 for (i = 0; i < sizeof(init->features) * 8; i++) {
713 bool set = (init->features[i / 32] & (1 << (i % 32)));
714
715 if (set && i >= KVM_VCPU_MAX_FEATURES)
716 return -ENOENT;
717
718 /*
719 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
720 * use the same feature set.
721 */
722 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
723 test_bit(i, vcpu->arch.features) != set)
724 return -EINVAL;
725
726 if (set)
727 set_bit(i, vcpu->arch.features);
728 }
729
730 vcpu->arch.target = phys_target;
731
732 /* Now we know what it is, we can reset it. */
733 return kvm_reset_vcpu(vcpu);
734}
735
736
Christoffer Dall478a8232013-11-19 17:43:19 -0800737static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
738 struct kvm_vcpu_init *init)
739{
740 int ret;
741
742 ret = kvm_vcpu_set_target(vcpu, init);
743 if (ret)
744 return ret;
745
Christoffer Dall957db102014-11-27 10:35:03 +0100746 /*
747 * Ensure a rebooted VM will fault in RAM pages and detect if the
748 * guest MMU is turned off and flush the caches as needed.
749 */
750 if (vcpu->arch.has_run_once)
751 stage2_unmap_vm(vcpu->kvm);
752
Christoffer Dallb856a592014-10-16 17:21:16 +0200753 vcpu_reset_hcr(vcpu);
754
Christoffer Dall478a8232013-11-19 17:43:19 -0800755 /*
756 * Handle the "start in power-off" case by marking the VCPU as paused.
757 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100758 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Christoffer Dall478a8232013-11-19 17:43:19 -0800759 vcpu->arch.pause = true;
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +0200760 else
761 vcpu->arch.pause = false;
Christoffer Dall478a8232013-11-19 17:43:19 -0800762
763 return 0;
764}
765
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500766long kvm_arch_vcpu_ioctl(struct file *filp,
767 unsigned int ioctl, unsigned long arg)
768{
769 struct kvm_vcpu *vcpu = filp->private_data;
770 void __user *argp = (void __user *)arg;
771
772 switch (ioctl) {
773 case KVM_ARM_VCPU_INIT: {
774 struct kvm_vcpu_init init;
775
776 if (copy_from_user(&init, argp, sizeof(init)))
777 return -EFAULT;
778
Christoffer Dall478a8232013-11-19 17:43:19 -0800779 return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500780 }
781 case KVM_SET_ONE_REG:
782 case KVM_GET_ONE_REG: {
783 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +0200784
785 if (unlikely(!kvm_vcpu_initialized(vcpu)))
786 return -ENOEXEC;
787
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500788 if (copy_from_user(&reg, argp, sizeof(reg)))
789 return -EFAULT;
790 if (ioctl == KVM_SET_ONE_REG)
791 return kvm_arm_set_reg(vcpu, &reg);
792 else
793 return kvm_arm_get_reg(vcpu, &reg);
794 }
795 case KVM_GET_REG_LIST: {
796 struct kvm_reg_list __user *user_list = argp;
797 struct kvm_reg_list reg_list;
798 unsigned n;
799
Andre Przywarae8180dc2013-05-09 00:28:06 +0200800 if (unlikely(!kvm_vcpu_initialized(vcpu)))
801 return -ENOEXEC;
802
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500803 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
804 return -EFAULT;
805 n = reg_list.n;
806 reg_list.n = kvm_arm_num_regs(vcpu);
807 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
808 return -EFAULT;
809 if (n < reg_list.n)
810 return -E2BIG;
811 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
812 }
813 default:
814 return -EINVAL;
815 }
816}
817
Mario Smarduch53c810c2015-01-15 15:58:57 -0800818/**
819 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
820 * @kvm: kvm instance
821 * @log: slot id and address to which we copy the log
822 *
823 * Steps 1-4 below provide general overview of dirty page logging. See
824 * kvm_get_dirty_log_protect() function description for additional details.
825 *
826 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
827 * always flush the TLB (step 4) even if previous step failed and the dirty
828 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
829 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
830 * writes will be marked dirty for next log read.
831 *
832 * 1. Take a snapshot of the bit and clear it if needed.
833 * 2. Write protect the corresponding page.
834 * 3. Copy the snapshot to the userspace.
835 * 4. Flush TLB's if needed.
836 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500837int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
838{
Mario Smarduch53c810c2015-01-15 15:58:57 -0800839 bool is_dirty = false;
840 int r;
841
842 mutex_lock(&kvm->slots_lock);
843
844 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
845
846 if (is_dirty)
847 kvm_flush_remote_tlbs(kvm);
848
849 mutex_unlock(&kvm->slots_lock);
850 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500851}
852
Christoffer Dall3401d5462013-01-23 13:18:04 -0500853static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
854 struct kvm_arm_device_addr *dev_addr)
855{
Christoffer Dall330690c2013-01-21 19:36:13 -0500856 unsigned long dev_id, type;
857
858 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
859 KVM_ARM_DEVICE_ID_SHIFT;
860 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
861 KVM_ARM_DEVICE_TYPE_SHIFT;
862
863 switch (dev_id) {
864 case KVM_ARM_DEVICE_VGIC_V2:
Pavel Fedinf943cd92015-12-18 14:38:43 +0300865 if (!vgic_present)
866 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -0700867 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -0500868 default:
869 return -ENODEV;
870 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500871}
872
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500873long kvm_arch_vm_ioctl(struct file *filp,
874 unsigned int ioctl, unsigned long arg)
875{
Christoffer Dall3401d5462013-01-23 13:18:04 -0500876 struct kvm *kvm = filp->private_data;
877 void __user *argp = (void __user *)arg;
878
879 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500880 case KVM_CREATE_IRQCHIP: {
Pavel Fedinf943cd92015-12-18 14:38:43 +0300881 if (!vgic_present)
882 return -ENXIO;
Paolo Bonzini69ff5c62015-03-05 12:26:06 +0100883 return kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500884 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500885 case KVM_ARM_SET_DEVICE_ADDR: {
886 struct kvm_arm_device_addr dev_addr;
887
888 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
889 return -EFAULT;
890 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
891 }
Anup Patel42c4e0c2013-09-30 14:20:07 +0530892 case KVM_ARM_PREFERRED_TARGET: {
893 int err;
894 struct kvm_vcpu_init init;
895
896 err = kvm_vcpu_preferred_target(&init);
897 if (err)
898 return err;
899
900 if (copy_to_user(argp, &init, sizeof(init)))
901 return -EFAULT;
902
903 return 0;
904 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500905 default:
906 return -EINVAL;
907 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500908}
909
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100910static void cpu_init_hyp_mode(void *dummy)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500911{
Marc Zyngierdac288f2013-05-14 12:11:37 +0100912 phys_addr_t boot_pgd_ptr;
913 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500914 unsigned long hyp_stack_ptr;
915 unsigned long stack_page;
916 unsigned long vector_ptr;
917
918 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100919 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500920
Marc Zyngierdac288f2013-05-14 12:11:37 +0100921 boot_pgd_ptr = kvm_mmu_get_boot_httbr();
922 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +0100923 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500924 hyp_stack_ptr = stack_page + PAGE_SIZE;
925 vector_ptr = (unsigned long)__kvm_hyp_vector;
926
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100927 __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
Marc Zyngier83613e32016-02-01 17:54:35 +0000928 __cpu_init_stage2();
Alex Shifa237192016-05-24 14:52:57 +0800929
930 kvm_arm_init_debug();
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500931}
932
James Morsed6c13762016-03-30 18:33:04 +0100933static void cpu_hyp_reinit(void)
934{
935 if (is_kernel_in_hyp_mode()) {
936 /*
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100937 * __cpu_init_stage2() is safe to call even if the PM
James Morsed6c13762016-03-30 18:33:04 +0100938 * event was cancelled before the CPU was reset.
939 */
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100940 __cpu_init_stage2();
James Morsed6c13762016-03-30 18:33:04 +0100941 } else {
942 if (__hyp_get_vectors() == hyp_default_vectors)
943 cpu_init_hyp_mode(NULL);
944 }
945}
946
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100947static void cpu_hyp_reset(void)
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100948{
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100949 phys_addr_t boot_pgd_ptr;
950 phys_addr_t phys_idmap_start;
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100951
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100952 if (!is_kernel_in_hyp_mode()) {
953 boot_pgd_ptr = kvm_mmu_get_boot_httbr();
954 phys_idmap_start = kvm_get_idmap_start();
955
956 __cpu_reset_hyp_mode(boot_pgd_ptr, phys_idmap_start);
957 }
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100958}
959
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100960static void _kvm_arch_hardware_enable(void *discard)
961{
962 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
963 cpu_hyp_reinit();
964 __this_cpu_write(kvm_arm_hardware_enabled, 1);
965 }
966}
967
968int kvm_arch_hardware_enable(void)
969{
970 _kvm_arch_hardware_enable(NULL);
971 return 0;
972}
973
974static void _kvm_arch_hardware_disable(void *discard)
975{
976 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
977 cpu_hyp_reset();
978 __this_cpu_write(kvm_arm_hardware_enabled, 0);
979 }
980}
981
982void kvm_arch_hardware_disable(void)
983{
984 _kvm_arch_hardware_disable(NULL);
985}
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100986
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +0100987#ifdef CONFIG_CPU_PM
988static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
989 unsigned long cmd,
990 void *v)
991{
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +0100992 /*
993 * kvm_arm_hardware_enabled is left with its old value over
994 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
995 * re-enable hyp.
996 */
997 switch (cmd) {
998 case CPU_PM_ENTER:
999 if (__this_cpu_read(kvm_arm_hardware_enabled))
1000 /*
1001 * don't update kvm_arm_hardware_enabled here
1002 * so that the hardware will be re-enabled
1003 * when we resume. See below.
1004 */
1005 cpu_hyp_reset();
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001006
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001007 return NOTIFY_OK;
1008 case CPU_PM_EXIT:
1009 if (__this_cpu_read(kvm_arm_hardware_enabled))
1010 /* The hardware was enabled before suspend. */
1011 cpu_hyp_reinit();
1012
1013 return NOTIFY_OK;
1014
1015 default:
1016 return NOTIFY_DONE;
1017 }
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001018}
1019
1020static struct notifier_block hyp_init_cpu_pm_nb = {
1021 .notifier_call = hyp_init_cpu_pm_notifier,
1022};
1023
1024static void __init hyp_cpu_pm_init(void)
1025{
1026 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1027}
Sudeep Holla5e0afcb2016-04-04 14:46:51 +01001028static void __init hyp_cpu_pm_exit(void)
1029{
1030 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1031}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001032#else
1033static inline void hyp_cpu_pm_init(void)
1034{
1035}
Sudeep Holla5e0afcb2016-04-04 14:46:51 +01001036static inline void hyp_cpu_pm_exit(void)
1037{
1038}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001039#endif
1040
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001041static void teardown_common_resources(void)
1042{
1043 free_percpu(kvm_host_cpu_state);
1044}
1045
1046static int init_common_resources(void)
1047{
1048 kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
1049 if (!kvm_host_cpu_state) {
1050 kvm_err("Cannot allocate host CPU state\n");
1051 return -ENOMEM;
1052 }
1053
1054 return 0;
1055}
1056
1057static int init_subsystems(void)
1058{
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001059 int err = 0;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001060
1061 /*
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001062 * Enable hardware so that subsystem initialisation can access EL2.
James Morsed6c13762016-03-30 18:33:04 +01001063 */
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001064 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
James Morsed6c13762016-03-30 18:33:04 +01001065
1066 /*
1067 * Register CPU lower-power notifier
1068 */
1069 hyp_cpu_pm_init();
1070
1071 /*
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001072 * Init HYP view of VGIC
1073 */
1074 err = kvm_vgic_hyp_init();
1075 switch (err) {
1076 case 0:
1077 vgic_present = true;
1078 break;
1079 case -ENODEV:
1080 case -ENXIO:
1081 vgic_present = false;
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001082 err = 0;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001083 break;
1084 default:
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001085 goto out;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001086 }
1087
1088 /*
1089 * Init HYP architected timer support
1090 */
1091 err = kvm_timer_hyp_init();
1092 if (err)
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001093 goto out;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001094
1095 kvm_perf_init();
1096 kvm_coproc_table_init();
1097
AKASHI Takahiroa7611ce2016-04-27 17:47:05 +01001098out:
1099 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1100
1101 return err;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001102}
1103
1104static void teardown_hyp_mode(void)
1105{
1106 int cpu;
1107
1108 if (is_kernel_in_hyp_mode())
1109 return;
1110
1111 free_hyp_pgds();
1112 for_each_possible_cpu(cpu)
1113 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
Sudeep Holla5e0afcb2016-04-04 14:46:51 +01001114 hyp_cpu_pm_exit();
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001115}
1116
1117static int init_vhe_mode(void)
1118{
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001119
1120 kvm_info("VHE mode initialized successfully\n");
1121 return 0;
1122}
1123
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001124/**
1125 * Inits Hyp-mode on all online CPUs
1126 */
1127static int init_hyp_mode(void)
1128{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001129 int cpu;
1130 int err = 0;
1131
1132 /*
1133 * Allocate Hyp PGD and setup Hyp identity mapping
1134 */
1135 err = kvm_mmu_init();
1136 if (err)
1137 goto out_err;
1138
1139 /*
1140 * It is probably enough to obtain the default on one
1141 * CPU. It's unlikely to be different on the others.
1142 */
1143 hyp_default_vectors = __hyp_get_vectors();
1144
1145 /*
1146 * Allocate stack pages for Hypervisor-mode
1147 */
1148 for_each_possible_cpu(cpu) {
1149 unsigned long stack_page;
1150
1151 stack_page = __get_free_page(GFP_KERNEL);
1152 if (!stack_page) {
1153 err = -ENOMEM;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001154 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001155 }
1156
1157 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1158 }
1159
1160 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001161 * Map the Hyp-code called directly from the host
1162 */
Marc Zyngierd55c85c2016-01-02 14:04:48 +00001163 err = create_hyp_mappings(__hyp_text_start, __hyp_text_end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001164 if (err) {
1165 kvm_err("Cannot map world-switch code\n");
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001166 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001167 }
1168
Marc Zyngier29f183d2015-10-27 12:18:48 +00001169 err = create_hyp_mappings(__start_rodata, __end_rodata);
1170 if (err) {
1171 kvm_err("Cannot map rodata section\n");
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001172 goto out_err;
Marc Zyngier29f183d2015-10-27 12:18:48 +00001173 }
1174
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001175 /*
1176 * Map the Hyp stack pages
1177 */
1178 for_each_possible_cpu(cpu) {
1179 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
1180 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
1181
1182 if (err) {
1183 kvm_err("Cannot map hyp stack\n");
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001184 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001185 }
1186 }
1187
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001188 for_each_possible_cpu(cpu) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001189 kvm_cpu_context_t *cpu_ctxt;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001190
Marc Zyngier3de50da2013-04-08 16:47:19 +01001191 cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
1192 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001193
1194 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001195 kvm_err("Cannot map host CPU state: %d\n", err);
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001196 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001197 }
1198 }
1199
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001200#ifndef CONFIG_HOTPLUG_CPU
1201 free_boot_hyp_pgd();
1202#endif
1203
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001204 kvm_info("Hyp mode initialized successfully\n");
Marc Zyngier210552c2013-03-05 03:18:00 +00001205
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001206 return 0;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001207
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001208out_err:
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001209 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001210 kvm_err("error initializing Hyp mode: %d\n", err);
1211 return err;
1212}
1213
Andre Przywarad4e071c2013-04-17 12:52:01 +02001214static void check_kvm_target_cpu(void *ret)
1215{
1216 *(int *)ret = kvm_target_cpu();
1217}
1218
Andre Przywara4429fc62014-06-02 15:37:13 +02001219struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1220{
1221 struct kvm_vcpu *vcpu;
1222 int i;
1223
1224 mpidr &= MPIDR_HWID_BITMASK;
1225 kvm_for_each_vcpu(i, vcpu, kvm) {
1226 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1227 return vcpu;
1228 }
1229 return NULL;
1230}
1231
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001232/**
1233 * Initialize Hyp-mode and memory mappings on all CPUs.
1234 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001235int kvm_arch_init(void *opaque)
1236{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001237 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001238 int ret, cpu;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001239
1240 if (!is_hyp_mode_available()) {
1241 kvm_err("HYP mode not available\n");
1242 return -ENODEV;
1243 }
1244
Andre Przywarad4e071c2013-04-17 12:52:01 +02001245 for_each_online_cpu(cpu) {
1246 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1247 if (ret < 0) {
1248 kvm_err("Error, CPU %d not supported!\n", cpu);
1249 return -ENODEV;
1250 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001251 }
1252
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001253 err = init_common_resources();
1254 if (err)
1255 return err;
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301256
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001257 if (is_kernel_in_hyp_mode())
1258 err = init_vhe_mode();
1259 else
1260 err = init_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001261 if (err)
1262 goto out_err;
1263
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001264 err = init_subsystems();
1265 if (err)
1266 goto out_hyp;
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001267
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001268 return 0;
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001269
1270out_hyp:
1271 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001272out_err:
Marc Zyngier5aa75c82015-01-29 11:59:54 +00001273 teardown_common_resources();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001274 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001275}
1276
1277/* NOP: Compiling as a module not supported */
1278void kvm_arch_exit(void)
1279{
Marc Zyngier210552c2013-03-05 03:18:00 +00001280 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001281}
1282
1283static int arm_init(void)
1284{
1285 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1286 return rc;
1287}
1288
1289module_init(arm_init);