blob: 77151b111d32d3f6c00b6d7fd2e4516a0fb59211 [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 Dall86ce8532013-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 Zyngieraa024c22013-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 Zyngier1638a122013-01-21 19:36:11 -050064static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
65{
66 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010067 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050068}
69
70/**
71 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
72 * Must be called from non-preemptible context
73 */
74struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
75{
76 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010077 return __this_cpu_read(kvm_arm_running_vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050078}
79
80/**
81 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
82 */
Will Deacon4000be42014-08-26 15:13:21 +010083struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
Marc Zyngier1638a122013-01-21 19:36:11 -050084{
85 return &kvm_arm_running_vcpu;
86}
87
Radim Krčmář13a34e02014-08-28 15:13:03 +020088int kvm_arch_hardware_enable(void)
Christoffer Dall749cf76c2013-01-20 18:28:06 -050089{
90 return 0;
91}
92
93int 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 */
134 kvm->arch.max_vcpus = kvm_vgic_get_max_vcpus();
135
Christoffer Dalld5d81842013-01-20 18:28:07 -0500136 return ret;
137out_free_stage2_pgd:
138 kvm_free_stage2_pgd(kvm);
139out_fail_alloc:
140 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500141}
142
143int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
144{
145 return VM_FAULT_SIGBUS;
146}
147
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500148
Christoffer Dalld5d81842013-01-20 18:28:07 -0500149/**
150 * kvm_arch_destroy_vm - destroy the VM data structure
151 * @kvm: pointer to the KVM struct
152 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500153void kvm_arch_destroy_vm(struct kvm *kvm)
154{
155 int i;
156
Christoffer Dalld5d81842013-01-20 18:28:07 -0500157 kvm_free_stage2_pgd(kvm);
158
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500159 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
160 if (kvm->vcpus[i]) {
161 kvm_arch_vcpu_free(kvm->vcpus[i]);
162 kvm->vcpus[i] = NULL;
163 }
164 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100165
166 kvm_vgic_destroy(kvm);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500167}
168
Alexander Graf784aa3d2014-07-14 18:27:35 +0200169int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500170{
171 int r;
172 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500173 case KVM_CAP_IRQCHIP:
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000174 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100175 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500176 case KVM_CAP_USER_MEMORY:
177 case KVM_CAP_SYNC_MMU:
178 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
179 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500180 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530181 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200182 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000183 case KVM_CAP_MP_STATE:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500184 r = 1;
185 break;
186 case KVM_CAP_COALESCED_MMIO:
187 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
188 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500189 case KVM_CAP_ARM_SET_DEVICE_ADDR:
190 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100191 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500192 case KVM_CAP_NR_VCPUS:
193 r = num_online_cpus();
194 break;
195 case KVM_CAP_MAX_VCPUS:
196 r = KVM_MAX_VCPUS;
197 break;
198 default:
Marc Zyngier17b1e312013-04-08 16:47:18 +0100199 r = kvm_arch_dev_ioctl_check_extension(ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500200 break;
201 }
202 return r;
203}
204
205long kvm_arch_dev_ioctl(struct file *filp,
206 unsigned int ioctl, unsigned long arg)
207{
208 return -EINVAL;
209}
210
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500211
212struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
213{
214 int err;
215 struct kvm_vcpu *vcpu;
216
Christoffer Dall716139d2014-12-09 14:33:45 +0100217 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
218 err = -EBUSY;
219 goto out;
220 }
221
Andre Przywara3caa2d82014-06-02 16:26:01 +0200222 if (id >= kvm->arch.max_vcpus) {
223 err = -EINVAL;
224 goto out;
225 }
226
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500227 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
228 if (!vcpu) {
229 err = -ENOMEM;
230 goto out;
231 }
232
233 err = kvm_vcpu_init(vcpu, kvm, id);
234 if (err)
235 goto free_vcpu;
236
Christoffer Dalld5d81842013-01-20 18:28:07 -0500237 err = create_hyp_mappings(vcpu, vcpu + 1);
238 if (err)
239 goto vcpu_uninit;
240
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500241 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500242vcpu_uninit:
243 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500244free_vcpu:
245 kmem_cache_free(kvm_vcpu_cache, vcpu);
246out:
247 return ERR_PTR(err);
248}
249
Dominik Dingel31928aa2014-12-04 15:47:07 +0100250void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500251{
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500252}
253
254void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
255{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500256 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500257 kvm_timer_vcpu_terminate(vcpu);
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100258 kvm_vgic_vcpu_destroy(vcpu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500259 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500260}
261
262void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
263{
264 kvm_arch_vcpu_free(vcpu);
265}
266
267int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
268{
Christoffer Dall1a748472015-03-13 17:02:55 +0000269 return kvm_timer_should_fire(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500270}
271
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500272int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
273{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500274 /* Force users to call KVM_ARM_VCPU_INIT */
275 vcpu->arch.target = -1;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200276 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500277
Marc Zyngier967f8422013-01-23 13:21:59 -0500278 /* Set up the timer */
279 kvm_timer_vcpu_init(vcpu);
280
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500281 return 0;
282}
283
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500284void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
285{
Christoffer Dall86ce8532013-01-20 18:28:08 -0500286 vcpu->cpu = cpu;
Marc Zyngier3de50da2013-04-08 16:47:19 +0100287 vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500288
Marc Zyngier1638a122013-01-21 19:36:11 -0500289 kvm_arm_set_running_vcpu(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500290}
291
292void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
293{
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800294 /*
295 * The arch-generic KVM code expects the cpu field of a vcpu to be -1
296 * if the vcpu is no longer assigned to a cpu. This is used for the
297 * optimized make_all_cpus_request path.
298 */
299 vcpu->cpu = -1;
300
Marc Zyngier1638a122013-01-21 19:36:11 -0500301 kvm_arm_set_running_vcpu(NULL);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500302}
303
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500304int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
305 struct kvm_mp_state *mp_state)
306{
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000307 if (vcpu->arch.pause)
308 mp_state->mp_state = KVM_MP_STATE_STOPPED;
309 else
310 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
311
312 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500313}
314
315int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
316 struct kvm_mp_state *mp_state)
317{
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000318 switch (mp_state->mp_state) {
319 case KVM_MP_STATE_RUNNABLE:
320 vcpu->arch.pause = false;
321 break;
322 case KVM_MP_STATE_STOPPED:
323 vcpu->arch.pause = true;
324 break;
325 default:
326 return -EINVAL;
327 }
328
329 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500330}
331
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500332/**
333 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
334 * @v: The VCPU pointer
335 *
336 * If the guest CPU is not waiting for interrupts or an interrupt line is
337 * asserted, the CPU is by definition runnable.
338 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500339int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
340{
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500341 return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500342}
343
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500344/* Just ensure a guest exit from a particular CPU */
345static void exit_vm_noop(void *info)
346{
347}
348
349void force_vm_exit(const cpumask_t *mask)
350{
351 smp_call_function_many(mask, exit_vm_noop, NULL, true);
352}
353
354/**
355 * need_new_vmid_gen - check that the VMID is still valid
356 * @kvm: The VM's VMID to checkt
357 *
358 * return true if there is a new generation of VMIDs being used
359 *
360 * The hardware supports only 256 values with the value zero reserved for the
361 * host, so we check if an assigned value belongs to a previous generation,
362 * which which requires us to assign a new value. If we're the first to use a
363 * VMID for the new generation, we must flush necessary caches and TLBs on all
364 * CPUs.
365 */
366static bool need_new_vmid_gen(struct kvm *kvm)
367{
368 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
369}
370
371/**
372 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
373 * @kvm The guest that we are about to run
374 *
375 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
376 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
377 * caches and TLBs.
378 */
379static void update_vttbr(struct kvm *kvm)
380{
381 phys_addr_t pgd_phys;
382 u64 vmid;
383
384 if (!need_new_vmid_gen(kvm))
385 return;
386
387 spin_lock(&kvm_vmid_lock);
388
389 /*
390 * We need to re-check the vmid_gen here to ensure that if another vcpu
391 * already allocated a valid vmid for this vm, then this vcpu should
392 * use the same vmid.
393 */
394 if (!need_new_vmid_gen(kvm)) {
395 spin_unlock(&kvm_vmid_lock);
396 return;
397 }
398
399 /* First user of a new VMID generation? */
400 if (unlikely(kvm_next_vmid == 0)) {
401 atomic64_inc(&kvm_vmid_gen);
402 kvm_next_vmid = 1;
403
404 /*
405 * On SMP we know no other CPUs can use this CPU's or each
406 * other's VMID after force_vm_exit returns since the
407 * kvm_vmid_lock blocks them from reentry to the guest.
408 */
409 force_vm_exit(cpu_all_mask);
410 /*
411 * Now broadcast TLB + ICACHE invalidation over the inner
412 * shareable domain to make sure all data structures are
413 * clean.
414 */
415 kvm_call_hyp(__kvm_flush_vm_context);
416 }
417
418 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
419 kvm->arch.vmid = kvm_next_vmid;
420 kvm_next_vmid++;
421
422 /* update vttbr to be used with the new vmid */
Christoffer Dall38f791a2014-10-10 12:14:28 +0200423 pgd_phys = virt_to_phys(kvm_get_hwpgd(kvm));
Joel Schoppdbff1242014-07-09 11:17:04 -0500424 BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500425 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK;
Joel Schoppdbff1242014-07-09 11:17:04 -0500426 kvm->arch.vttbr = pgd_phys | vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500427
428 spin_unlock(&kvm_vmid_lock);
429}
430
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500431static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
432{
Christoffer Dall05971122014-12-12 21:19:23 +0100433 struct kvm *kvm = vcpu->kvm;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700434 int ret;
435
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500436 if (likely(vcpu->arch.has_run_once))
437 return 0;
438
439 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500440
441 /*
Peter Maydell6d3cfbe2014-12-04 15:02:24 +0000442 * Map the VGIC hardware resources before running a vcpu the first
443 * time on this VM.
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500444 */
Christoffer Dall05971122014-12-12 21:19:23 +0100445 if (unlikely(!vgic_ready(kvm))) {
446 ret = kvm_vgic_map_resources(kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500447 if (ret)
448 return ret;
449 }
450
Christoffer Dall05971122014-12-12 21:19:23 +0100451 /*
452 * Enable the arch timers only if we have an in-kernel VGIC
453 * and it has been properly initialized, since we cannot handle
454 * interrupts from the virtual timer with a userspace gic.
455 */
456 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
457 kvm_timer_enable(kvm);
458
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500459 return 0;
460}
461
Eric Augerc1426e42015-03-04 11:14:34 +0100462bool kvm_arch_intc_initialized(struct kvm *kvm)
463{
464 return vgic_initialized(kvm);
465}
466
Marc Zyngieraa024c22013-01-20 18:28:13 -0500467static void vcpu_pause(struct kvm_vcpu *vcpu)
468{
469 wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
470
471 wait_event_interruptible(*wq, !vcpu->arch.pause);
472}
473
Andre Przywarae8180dc2013-05-09 00:28:06 +0200474static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
475{
476 return vcpu->arch.target >= 0;
477}
478
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500479/**
480 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
481 * @vcpu: The VCPU pointer
482 * @run: The kvm_run structure pointer used for userspace state exchange
483 *
484 * This function is called through the VCPU_RUN ioctl called from user space. It
485 * will execute VM code in a loop until the time slice for the process is used
486 * or some emulation is needed from user space in which case the function will
487 * return with return value 0 and with the kvm_run structure filled in with the
488 * required data for the requested emulation.
489 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500490int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
491{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500492 int ret;
493 sigset_t sigsaved;
494
Andre Przywarae8180dc2013-05-09 00:28:06 +0200495 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500496 return -ENOEXEC;
497
498 ret = kvm_vcpu_first_run_init(vcpu);
499 if (ret)
500 return ret;
501
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500502 if (run->exit_reason == KVM_EXIT_MMIO) {
503 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
504 if (ret)
505 return ret;
506 }
507
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500508 if (vcpu->sigset_active)
509 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
510
511 ret = 1;
512 run->exit_reason = KVM_EXIT_UNKNOWN;
513 while (ret > 0) {
514 /*
515 * Check conditions before entering the guest
516 */
517 cond_resched();
518
519 update_vttbr(vcpu->kvm);
520
Marc Zyngieraa024c22013-01-20 18:28:13 -0500521 if (vcpu->arch.pause)
522 vcpu_pause(vcpu);
523
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500524 kvm_vgic_flush_hwstate(vcpu);
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500525 kvm_timer_flush_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500526
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100527 preempt_disable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500528 local_irq_disable();
529
530 /*
531 * Re-check atomic conditions
532 */
533 if (signal_pending(current)) {
534 ret = -EINTR;
535 run->exit_reason = KVM_EXIT_INTR;
536 }
537
538 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) {
539 local_irq_enable();
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100540 preempt_enable();
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500541 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500542 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500543 continue;
544 }
545
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100546 kvm_arm_setup_debug(vcpu);
547
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500548 /**************************************************************
549 * Enter the guest
550 */
551 trace_kvm_entry(*vcpu_pc(vcpu));
Christian Borntraegerccf73aaf2015-04-30 13:43:31 +0200552 __kvm_guest_enter();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500553 vcpu->mode = IN_GUEST_MODE;
554
555 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
556
557 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100558 /*
559 * Back from guest
560 *************************************************************/
561
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100562 kvm_arm_clear_debug(vcpu);
563
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500564 /*
565 * We may have taken a host interrupt in HYP mode (ie
566 * while executing the guest). This interrupt is still
567 * pending, as we haven't serviced it yet!
568 *
569 * We're now back in SVC mode, with interrupts
570 * disabled. Enabling the interrupts now will have
571 * the effect of taking the interrupt again, in SVC
572 * mode this time.
573 */
574 local_irq_enable();
575
576 /*
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100577 * We do local_irq_enable() before calling kvm_guest_exit() so
578 * that if a timer interrupt hits while running the guest we
579 * account that tick as being spent in the guest. We enable
580 * preemption after calling kvm_guest_exit() so that if we get
581 * preempted we make sure ticks after that is not counted as
582 * guest time.
583 */
584 kvm_guest_exit();
585 trace_kvm_exit(kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
586 preempt_enable();
587
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500588
Marc Zyngierc7e3ba62013-01-23 13:21:59 -0500589 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500590 kvm_vgic_sync_hwstate(vcpu);
591
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500592 ret = handle_exit(vcpu, run, ret);
593 }
594
595 if (vcpu->sigset_active)
596 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
597 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500598}
599
Christoffer Dall86ce8532013-01-20 18:28:08 -0500600static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
601{
602 int bit_index;
603 bool set;
604 unsigned long *ptr;
605
606 if (number == KVM_ARM_IRQ_CPU_IRQ)
607 bit_index = __ffs(HCR_VI);
608 else /* KVM_ARM_IRQ_CPU_FIQ */
609 bit_index = __ffs(HCR_VF);
610
611 ptr = (unsigned long *)&vcpu->arch.irq_lines;
612 if (level)
613 set = test_and_set_bit(bit_index, ptr);
614 else
615 set = test_and_clear_bit(bit_index, ptr);
616
617 /*
618 * If we didn't change anything, no need to wake up or kick other CPUs
619 */
620 if (set == level)
621 return 0;
622
623 /*
624 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
625 * trigger a world-switch round on the running physical CPU to set the
626 * virtual IRQ/FIQ fields in the HCR appropriately.
627 */
628 kvm_vcpu_kick(vcpu);
629
630 return 0;
631}
632
Alexander Graf79558f12013-04-16 19:21:41 +0200633int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
634 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500635{
636 u32 irq = irq_level->irq;
637 unsigned int irq_type, vcpu_idx, irq_num;
638 int nrcpus = atomic_read(&kvm->online_vcpus);
639 struct kvm_vcpu *vcpu = NULL;
640 bool level = irq_level->level;
641
642 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
643 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
644 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
645
646 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
647
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500648 switch (irq_type) {
649 case KVM_ARM_IRQ_TYPE_CPU:
650 if (irqchip_in_kernel(kvm))
651 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500652
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500653 if (vcpu_idx >= nrcpus)
654 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500655
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500656 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
657 if (!vcpu)
658 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500659
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500660 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
661 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500662
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500663 return vcpu_interrupt_line(vcpu, irq_num, level);
664 case KVM_ARM_IRQ_TYPE_PPI:
665 if (!irqchip_in_kernel(kvm))
666 return -ENXIO;
667
668 if (vcpu_idx >= nrcpus)
669 return -EINVAL;
670
671 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
672 if (!vcpu)
673 return -EINVAL;
674
675 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
676 return -EINVAL;
677
678 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
679 case KVM_ARM_IRQ_TYPE_SPI:
680 if (!irqchip_in_kernel(kvm))
681 return -ENXIO;
682
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100683 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500684 return -EINVAL;
685
686 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
687 }
688
689 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500690}
691
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200692static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
693 const struct kvm_vcpu_init *init)
694{
695 unsigned int i;
696 int phys_target = kvm_target_cpu();
697
698 if (init->target != phys_target)
699 return -EINVAL;
700
701 /*
702 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
703 * use the same target.
704 */
705 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
706 return -EINVAL;
707
708 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
709 for (i = 0; i < sizeof(init->features) * 8; i++) {
710 bool set = (init->features[i / 32] & (1 << (i % 32)));
711
712 if (set && i >= KVM_VCPU_MAX_FEATURES)
713 return -ENOENT;
714
715 /*
716 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
717 * use the same feature set.
718 */
719 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
720 test_bit(i, vcpu->arch.features) != set)
721 return -EINVAL;
722
723 if (set)
724 set_bit(i, vcpu->arch.features);
725 }
726
727 vcpu->arch.target = phys_target;
728
729 /* Now we know what it is, we can reset it. */
730 return kvm_reset_vcpu(vcpu);
731}
732
733
Christoffer Dall478a8232013-11-19 17:43:19 -0800734static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
735 struct kvm_vcpu_init *init)
736{
737 int ret;
738
739 ret = kvm_vcpu_set_target(vcpu, init);
740 if (ret)
741 return ret;
742
Christoffer Dall957db102014-11-27 10:35:03 +0100743 /*
744 * Ensure a rebooted VM will fault in RAM pages and detect if the
745 * guest MMU is turned off and flush the caches as needed.
746 */
747 if (vcpu->arch.has_run_once)
748 stage2_unmap_vm(vcpu->kvm);
749
Christoffer Dallb856a592014-10-16 17:21:16 +0200750 vcpu_reset_hcr(vcpu);
751
Christoffer Dall478a8232013-11-19 17:43:19 -0800752 /*
753 * Handle the "start in power-off" case by marking the VCPU as paused.
754 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100755 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Christoffer Dall478a8232013-11-19 17:43:19 -0800756 vcpu->arch.pause = true;
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +0200757 else
758 vcpu->arch.pause = false;
Christoffer Dall478a8232013-11-19 17:43:19 -0800759
760 return 0;
761}
762
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500763long kvm_arch_vcpu_ioctl(struct file *filp,
764 unsigned int ioctl, unsigned long arg)
765{
766 struct kvm_vcpu *vcpu = filp->private_data;
767 void __user *argp = (void __user *)arg;
768
769 switch (ioctl) {
770 case KVM_ARM_VCPU_INIT: {
771 struct kvm_vcpu_init init;
772
773 if (copy_from_user(&init, argp, sizeof(init)))
774 return -EFAULT;
775
Christoffer Dall478a8232013-11-19 17:43:19 -0800776 return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500777 }
778 case KVM_SET_ONE_REG:
779 case KVM_GET_ONE_REG: {
780 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +0200781
782 if (unlikely(!kvm_vcpu_initialized(vcpu)))
783 return -ENOEXEC;
784
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500785 if (copy_from_user(&reg, argp, sizeof(reg)))
786 return -EFAULT;
787 if (ioctl == KVM_SET_ONE_REG)
788 return kvm_arm_set_reg(vcpu, &reg);
789 else
790 return kvm_arm_get_reg(vcpu, &reg);
791 }
792 case KVM_GET_REG_LIST: {
793 struct kvm_reg_list __user *user_list = argp;
794 struct kvm_reg_list reg_list;
795 unsigned n;
796
Andre Przywarae8180dc2013-05-09 00:28:06 +0200797 if (unlikely(!kvm_vcpu_initialized(vcpu)))
798 return -ENOEXEC;
799
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500800 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
801 return -EFAULT;
802 n = reg_list.n;
803 reg_list.n = kvm_arm_num_regs(vcpu);
804 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
805 return -EFAULT;
806 if (n < reg_list.n)
807 return -E2BIG;
808 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
809 }
810 default:
811 return -EINVAL;
812 }
813}
814
Mario Smarduch53c810c2015-01-15 15:58:57 -0800815/**
816 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
817 * @kvm: kvm instance
818 * @log: slot id and address to which we copy the log
819 *
820 * Steps 1-4 below provide general overview of dirty page logging. See
821 * kvm_get_dirty_log_protect() function description for additional details.
822 *
823 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
824 * always flush the TLB (step 4) even if previous step failed and the dirty
825 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
826 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
827 * writes will be marked dirty for next log read.
828 *
829 * 1. Take a snapshot of the bit and clear it if needed.
830 * 2. Write protect the corresponding page.
831 * 3. Copy the snapshot to the userspace.
832 * 4. Flush TLB's if needed.
833 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500834int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
835{
Mario Smarduch53c810c2015-01-15 15:58:57 -0800836 bool is_dirty = false;
837 int r;
838
839 mutex_lock(&kvm->slots_lock);
840
841 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
842
843 if (is_dirty)
844 kvm_flush_remote_tlbs(kvm);
845
846 mutex_unlock(&kvm->slots_lock);
847 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500848}
849
Christoffer Dall3401d5462013-01-23 13:18:04 -0500850static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
851 struct kvm_arm_device_addr *dev_addr)
852{
Christoffer Dall330690c2013-01-21 19:36:13 -0500853 unsigned long dev_id, type;
854
855 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
856 KVM_ARM_DEVICE_ID_SHIFT;
857 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
858 KVM_ARM_DEVICE_TYPE_SHIFT;
859
860 switch (dev_id) {
861 case KVM_ARM_DEVICE_VGIC_V2:
Christoffer Dallce01e4e2013-09-23 14:55:56 -0700862 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -0500863 default:
864 return -ENODEV;
865 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500866}
867
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500868long kvm_arch_vm_ioctl(struct file *filp,
869 unsigned int ioctl, unsigned long arg)
870{
Christoffer Dall3401d5462013-01-23 13:18:04 -0500871 struct kvm *kvm = filp->private_data;
872 void __user *argp = (void __user *)arg;
873
874 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500875 case KVM_CREATE_IRQCHIP: {
Paolo Bonzini69ff5c62015-03-05 12:26:06 +0100876 return kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500877 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500878 case KVM_ARM_SET_DEVICE_ADDR: {
879 struct kvm_arm_device_addr dev_addr;
880
881 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
882 return -EFAULT;
883 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
884 }
Anup Patel42c4e0c2013-09-30 14:20:07 +0530885 case KVM_ARM_PREFERRED_TARGET: {
886 int err;
887 struct kvm_vcpu_init init;
888
889 err = kvm_vcpu_preferred_target(&init);
890 if (err)
891 return err;
892
893 if (copy_to_user(argp, &init, sizeof(init)))
894 return -EFAULT;
895
896 return 0;
897 }
Christoffer Dall3401d5462013-01-23 13:18:04 -0500898 default:
899 return -EINVAL;
900 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500901}
902
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100903static void cpu_init_hyp_mode(void *dummy)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500904{
Marc Zyngierdac288f2013-05-14 12:11:37 +0100905 phys_addr_t boot_pgd_ptr;
906 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500907 unsigned long hyp_stack_ptr;
908 unsigned long stack_page;
909 unsigned long vector_ptr;
910
911 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100912 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500913
Marc Zyngierdac288f2013-05-14 12:11:37 +0100914 boot_pgd_ptr = kvm_mmu_get_boot_httbr();
915 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +0100916 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500917 hyp_stack_ptr = stack_page + PAGE_SIZE;
918 vector_ptr = (unsigned long)__kvm_hyp_vector;
919
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100920 __cpu_init_hyp_mode(boot_pgd_ptr, pgd_ptr, hyp_stack_ptr, vector_ptr);
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100921
922 kvm_arm_init_debug();
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500923}
924
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100925static int hyp_init_cpu_notify(struct notifier_block *self,
926 unsigned long action, void *cpu)
927{
928 switch (action) {
929 case CPU_STARTING:
930 case CPU_STARTING_FROZEN:
Vladimir Murzin37a34ac2014-09-22 15:52:48 +0100931 if (__hyp_get_vectors() == hyp_default_vectors)
932 cpu_init_hyp_mode(NULL);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100933 break;
934 }
935
936 return NOTIFY_OK;
937}
938
939static struct notifier_block hyp_init_cpu_nb = {
940 .notifier_call = hyp_init_cpu_notify,
941};
942
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +0100943#ifdef CONFIG_CPU_PM
944static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
945 unsigned long cmd,
946 void *v)
947{
Marc Zyngierb20c9f22014-02-26 18:47:36 +0000948 if (cmd == CPU_PM_EXIT &&
949 __hyp_get_vectors() == hyp_default_vectors) {
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +0100950 cpu_init_hyp_mode(NULL);
951 return NOTIFY_OK;
952 }
953
954 return NOTIFY_DONE;
955}
956
957static struct notifier_block hyp_init_cpu_pm_nb = {
958 .notifier_call = hyp_init_cpu_pm_notifier,
959};
960
961static void __init hyp_cpu_pm_init(void)
962{
963 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
964}
965#else
966static inline void hyp_cpu_pm_init(void)
967{
968}
969#endif
970
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500971/**
972 * Inits Hyp-mode on all online CPUs
973 */
974static int init_hyp_mode(void)
975{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500976 int cpu;
977 int err = 0;
978
979 /*
980 * Allocate Hyp PGD and setup Hyp identity mapping
981 */
982 err = kvm_mmu_init();
983 if (err)
984 goto out_err;
985
986 /*
987 * It is probably enough to obtain the default on one
988 * CPU. It's unlikely to be different on the others.
989 */
990 hyp_default_vectors = __hyp_get_vectors();
991
992 /*
993 * Allocate stack pages for Hypervisor-mode
994 */
995 for_each_possible_cpu(cpu) {
996 unsigned long stack_page;
997
998 stack_page = __get_free_page(GFP_KERNEL);
999 if (!stack_page) {
1000 err = -ENOMEM;
1001 goto out_free_stack_pages;
1002 }
1003
1004 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1005 }
1006
1007 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001008 * Map the Hyp-code called directly from the host
1009 */
1010 err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end);
1011 if (err) {
1012 kvm_err("Cannot map world-switch code\n");
1013 goto out_free_mappings;
1014 }
1015
1016 /*
1017 * Map the Hyp stack pages
1018 */
1019 for_each_possible_cpu(cpu) {
1020 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
1021 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE);
1022
1023 if (err) {
1024 kvm_err("Cannot map hyp stack\n");
1025 goto out_free_mappings;
1026 }
1027 }
1028
1029 /*
Marc Zyngier3de50da2013-04-08 16:47:19 +01001030 * Map the host CPU structures
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001031 */
Marc Zyngier3de50da2013-04-08 16:47:19 +01001032 kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
1033 if (!kvm_host_cpu_state) {
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001034 err = -ENOMEM;
Marc Zyngier3de50da2013-04-08 16:47:19 +01001035 kvm_err("Cannot allocate host CPU state\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001036 goto out_free_mappings;
1037 }
1038
1039 for_each_possible_cpu(cpu) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001040 kvm_cpu_context_t *cpu_ctxt;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001041
Marc Zyngier3de50da2013-04-08 16:47:19 +01001042 cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
1043 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001044
1045 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001046 kvm_err("Cannot map host CPU state: %d\n", err);
1047 goto out_free_context;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001048 }
1049 }
1050
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001051 /*
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001052 * Execute the init code on each CPU.
1053 */
1054 on_each_cpu(cpu_init_hyp_mode, NULL, 1);
1055
1056 /*
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001057 * Init HYP view of VGIC
1058 */
1059 err = kvm_vgic_hyp_init();
1060 if (err)
Marc Zyngier3de50da2013-04-08 16:47:19 +01001061 goto out_free_context;
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001062
Marc Zyngier967f8422013-01-23 13:21:59 -05001063 /*
1064 * Init HYP architected timer support
1065 */
1066 err = kvm_timer_hyp_init();
1067 if (err)
1068 goto out_free_mappings;
1069
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001070#ifndef CONFIG_HOTPLUG_CPU
1071 free_boot_hyp_pgd();
1072#endif
1073
Marc Zyngier210552c2013-03-05 03:18:00 +00001074 kvm_perf_init();
1075
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001076 kvm_info("Hyp mode initialized successfully\n");
Marc Zyngier210552c2013-03-05 03:18:00 +00001077
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001078 return 0;
Marc Zyngier3de50da2013-04-08 16:47:19 +01001079out_free_context:
1080 free_percpu(kvm_host_cpu_state);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001081out_free_mappings:
Marc Zyngier4f728272013-04-12 19:12:05 +01001082 free_hyp_pgds();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001083out_free_stack_pages:
1084 for_each_possible_cpu(cpu)
1085 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1086out_err:
1087 kvm_err("error initializing Hyp mode: %d\n", err);
1088 return err;
1089}
1090
Andre Przywarad4e071c2013-04-17 12:52:01 +02001091static void check_kvm_target_cpu(void *ret)
1092{
1093 *(int *)ret = kvm_target_cpu();
1094}
1095
Andre Przywara4429fc62014-06-02 15:37:13 +02001096struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1097{
1098 struct kvm_vcpu *vcpu;
1099 int i;
1100
1101 mpidr &= MPIDR_HWID_BITMASK;
1102 kvm_for_each_vcpu(i, vcpu, kvm) {
1103 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1104 return vcpu;
1105 }
1106 return NULL;
1107}
1108
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001109/**
1110 * Initialize Hyp-mode and memory mappings on all CPUs.
1111 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001112int kvm_arch_init(void *opaque)
1113{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001114 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001115 int ret, cpu;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001116
1117 if (!is_hyp_mode_available()) {
1118 kvm_err("HYP mode not available\n");
1119 return -ENODEV;
1120 }
1121
Andre Przywarad4e071c2013-04-17 12:52:01 +02001122 for_each_online_cpu(cpu) {
1123 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1124 if (ret < 0) {
1125 kvm_err("Error, CPU %d not supported!\n", cpu);
1126 return -ENODEV;
1127 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001128 }
1129
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301130 cpu_notifier_register_begin();
1131
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001132 err = init_hyp_mode();
1133 if (err)
1134 goto out_err;
1135
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301136 err = __register_cpu_notifier(&hyp_init_cpu_nb);
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001137 if (err) {
1138 kvm_err("Cannot register HYP init CPU notifier (%d)\n", err);
1139 goto out_err;
1140 }
1141
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301142 cpu_notifier_register_done();
1143
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001144 hyp_cpu_pm_init();
1145
Christoffer Dall5b3e5e52013-01-20 18:28:09 -05001146 kvm_coproc_table_init();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001147 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001148out_err:
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301149 cpu_notifier_register_done();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001150 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001151}
1152
1153/* NOP: Compiling as a module not supported */
1154void kvm_arch_exit(void)
1155{
Marc Zyngier210552c2013-03-05 03:18:00 +00001156 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001157}
1158
1159static int arm_init(void)
1160{
1161 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1162 return rc;
1163}
1164
1165module_init(arm_init);