blob: 314eb6abe1ff9879272fdae542c2e0043f3759eb [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>
Andre Przywara1085fdc2016-07-15 12:43:31 +010023#include <linux/list.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050024#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>
Shannon Zhaob02386e2016-02-26 19:29:19 +080031#include <kvm/arm_pmu.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050032
33#define CREATE_TRACE_POINTS
34#include "trace.h"
35
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050037#include <asm/ptrace.h>
38#include <asm/mman.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050039#include <asm/tlbflush.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050040#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050041#include <asm/virt.h>
42#include <asm/kvm_arm.h>
43#include <asm/kvm_asm.h>
44#include <asm/kvm_mmu.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050045#include <asm/kvm_emulate.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050046#include <asm/kvm_coproc.h>
Marc Zyngieraa024c22013-01-20 18:28:13 -050047#include <asm/kvm_psci.h>
Marc Zyngier910917b2015-10-27 12:18:48 +000048#include <asm/sections.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050049
50#ifdef REQUIRES_VIRT
51__asm__(".arch_extension virt");
52#endif
53
Christoffer Dall342cd0a2013-01-20 18:28:06 -050054static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
Marc Zyngier3de50da2013-04-08 16:47:19 +010055static kvm_cpu_context_t __percpu *kvm_host_cpu_state;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050056static unsigned long hyp_default_vectors;
57
Marc Zyngier1638a122013-01-21 19:36:11 -050058/* Per-CPU variable containing the currently running vcpu. */
59static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
60
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050061/* The VMID used in the VTTBR */
62static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
Vladimir Murzin20475f72015-11-16 11:28:18 +000063static u32 kvm_next_vmid;
64static unsigned int kvm_vmid_bits __read_mostly;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050065static DEFINE_SPINLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050066
Pavel Fedinc7da6fa2015-12-18 14:38:43 +030067static bool vgic_present;
68
AKASHI Takahiro67f69192016-04-27 17:47:05 +010069static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
70
Marc Zyngier1638a122013-01-21 19:36:11 -050071static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
72{
73 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010074 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050075}
76
77/**
78 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
79 * Must be called from non-preemptible context
80 */
81struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
82{
83 BUG_ON(preemptible());
Christoph Lameter1436c1a2013-10-21 13:17:08 +010084 return __this_cpu_read(kvm_arm_running_vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050085}
86
87/**
88 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
89 */
Will Deacon4000be42014-08-26 15:13:21 +010090struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
Marc Zyngier1638a122013-01-21 19:36:11 -050091{
92 return &kvm_arm_running_vcpu;
93}
94
Christoffer Dall749cf76c2013-01-20 18:28:06 -050095int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
96{
97 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
98}
99
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500100int kvm_arch_hardware_setup(void)
101{
102 return 0;
103}
104
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500105void kvm_arch_check_processor_compat(void *rtn)
106{
107 *(int *)rtn = 0;
108}
109
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500110
Christoffer Dalld5d81842013-01-20 18:28:07 -0500111/**
112 * kvm_arch_init_vm - initializes a VM data structure
113 * @kvm: pointer to the KVM struct
114 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500115int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
116{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100117 int ret, cpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500118
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500119 if (type)
120 return -EINVAL;
121
Marc Zyngier94d0e592016-10-18 18:37:49 +0100122 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
123 if (!kvm->arch.last_vcpu_ran)
124 return -ENOMEM;
125
126 for_each_possible_cpu(cpu)
127 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
128
Christoffer Dalld5d81842013-01-20 18:28:07 -0500129 ret = kvm_alloc_stage2_pgd(kvm);
130 if (ret)
131 goto out_fail_alloc;
132
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100133 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500134 if (ret)
135 goto out_free_stage2_pgd;
136
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100137 kvm_vgic_early_init(kvm);
Christoffer Dalla1a64382013-11-16 10:51:25 -0800138
Christoffer Dalld5d81842013-01-20 18:28:07 -0500139 /* Mark the initial VMID generation invalid */
140 kvm->arch.vmid_gen = 0;
141
Andre Przywara3caa2d82014-06-02 16:26:01 +0200142 /* The maximum number of VCPUs is limited by the host's GIC model */
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300143 kvm->arch.max_vcpus = vgic_present ?
144 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
Andre Przywara3caa2d82014-06-02 16:26:01 +0200145
Christoffer Dalld5d81842013-01-20 18:28:07 -0500146 return ret;
147out_free_stage2_pgd:
148 kvm_free_stage2_pgd(kvm);
149out_fail_alloc:
Marc Zyngier94d0e592016-10-18 18:37:49 +0100150 free_percpu(kvm->arch.last_vcpu_ran);
151 kvm->arch.last_vcpu_ran = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500152 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500153}
154
Luiz Capitulino235539b2016-09-07 14:47:23 -0400155bool kvm_arch_has_vcpu_debugfs(void)
156{
157 return false;
158}
159
160int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
161{
162 return 0;
163}
164
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500165int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
166{
167 return VM_FAULT_SIGBUS;
168}
169
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500170
Christoffer Dalld5d81842013-01-20 18:28:07 -0500171/**
172 * kvm_arch_destroy_vm - destroy the VM data structure
173 * @kvm: pointer to the KVM struct
174 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500175void kvm_arch_destroy_vm(struct kvm *kvm)
176{
177 int i;
178
Marc Zyngier94d0e592016-10-18 18:37:49 +0100179 free_percpu(kvm->arch.last_vcpu_ran);
180 kvm->arch.last_vcpu_ran = NULL;
181
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500182 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
183 if (kvm->vcpus[i]) {
184 kvm_arch_vcpu_free(kvm->vcpus[i]);
185 kvm->vcpus[i] = NULL;
186 }
187 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100188
189 kvm_vgic_destroy(kvm);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500190}
191
Alexander Graf784aa3d2014-07-14 18:27:35 +0200192int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500193{
194 int r;
195 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500196 case KVM_CAP_IRQCHIP:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300197 r = vgic_present;
198 break;
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000199 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100200 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500201 case KVM_CAP_USER_MEMORY:
202 case KVM_CAP_SYNC_MMU:
203 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
204 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500205 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530206 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200207 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000208 case KVM_CAP_MP_STATE:
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100209 case KVM_CAP_IMMEDIATE_EXIT:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500210 r = 1;
211 break;
212 case KVM_CAP_COALESCED_MMIO:
213 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
214 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500215 case KVM_CAP_ARM_SET_DEVICE_ADDR:
216 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100217 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500218 case KVM_CAP_NR_VCPUS:
219 r = num_online_cpus();
220 break;
221 case KVM_CAP_MAX_VCPUS:
222 r = KVM_MAX_VCPUS;
223 break;
Linu Cherian7af4df82017-03-08 11:38:33 +0530224 case KVM_CAP_NR_MEMSLOTS:
225 r = KVM_USER_MEM_SLOTS;
226 break;
Vladimir Murzin29885092016-11-02 11:55:34 +0000227 case KVM_CAP_MSI_DEVID:
228 if (!kvm)
229 r = -EINVAL;
230 else
231 r = kvm->arch.vgic.msis_require_devid;
232 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500233 default:
Andre Przywarab46f01c2016-07-15 12:43:25 +0100234 r = kvm_arch_dev_ioctl_check_extension(kvm, ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500235 break;
236 }
237 return r;
238}
239
240long kvm_arch_dev_ioctl(struct file *filp,
241 unsigned int ioctl, unsigned long arg)
242{
243 return -EINVAL;
244}
245
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500246
247struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
248{
249 int err;
250 struct kvm_vcpu *vcpu;
251
Christoffer Dall716139d2014-12-09 14:33:45 +0100252 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
253 err = -EBUSY;
254 goto out;
255 }
256
Andre Przywara3caa2d82014-06-02 16:26:01 +0200257 if (id >= kvm->arch.max_vcpus) {
258 err = -EINVAL;
259 goto out;
260 }
261
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500262 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
263 if (!vcpu) {
264 err = -ENOMEM;
265 goto out;
266 }
267
268 err = kvm_vcpu_init(vcpu, kvm, id);
269 if (err)
270 goto free_vcpu;
271
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100272 err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500273 if (err)
274 goto vcpu_uninit;
275
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500276 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500277vcpu_uninit:
278 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500279free_vcpu:
280 kmem_cache_free(kvm_vcpu_cache, vcpu);
281out:
282 return ERR_PTR(err);
283}
284
Dominik Dingel31928aa2014-12-04 15:47:07 +0100285void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500286{
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100287 kvm_vgic_vcpu_early_init(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500288}
289
290void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
291{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500292 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500293 kvm_timer_vcpu_terminate(vcpu);
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100294 kvm_vgic_vcpu_destroy(vcpu);
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800295 kvm_pmu_vcpu_destroy(vcpu);
James Morse591d2152016-06-08 17:24:45 +0100296 kvm_vcpu_uninit(vcpu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500297 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500298}
299
300void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
301{
302 kvm_arch_vcpu_free(vcpu);
303}
304
305int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
306{
Jintack Limfb280e92017-02-03 10:20:05 -0500307 return kvm_timer_should_fire(vcpu_vtimer(vcpu)) ||
308 kvm_timer_should_fire(vcpu_ptimer(vcpu));
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500309}
310
Christoffer Dalld35268d2015-08-25 19:48:21 +0200311void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
312{
313 kvm_timer_schedule(vcpu);
314}
315
316void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
317{
318 kvm_timer_unschedule(vcpu);
319}
320
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500321int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
322{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500323 /* Force users to call KVM_ARM_VCPU_INIT */
324 vcpu->arch.target = -1;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200325 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500326
Marc Zyngier967f8422013-01-23 13:21:59 -0500327 /* Set up the timer */
328 kvm_timer_vcpu_init(vcpu);
329
Alex Bennée84e690b2015-07-07 17:30:00 +0100330 kvm_arm_reset_debug_ptr(vcpu);
331
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500332 return 0;
333}
334
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500335void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
336{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100337 int *last_ran;
338
339 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
340
341 /*
342 * We might get preempted before the vCPU actually runs, but
343 * over-invalidation doesn't affect correctness.
344 */
345 if (*last_ran != vcpu->vcpu_id) {
346 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
347 *last_ran = vcpu->vcpu_id;
348 }
349
Christoffer Dall86ce8532013-01-20 18:28:08 -0500350 vcpu->cpu = cpu;
Marc Zyngier3de50da2013-04-08 16:47:19 +0100351 vcpu->arch.host_cpu_context = this_cpu_ptr(kvm_host_cpu_state);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500352
Marc Zyngier1638a122013-01-21 19:36:11 -0500353 kvm_arm_set_running_vcpu(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500354}
355
356void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
357{
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800358 /*
359 * The arch-generic KVM code expects the cpu field of a vcpu to be -1
360 * if the vcpu is no longer assigned to a cpu. This is used for the
361 * optimized make_all_cpus_request path.
362 */
363 vcpu->cpu = -1;
364
Marc Zyngier1638a122013-01-21 19:36:11 -0500365 kvm_arm_set_running_vcpu(NULL);
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000366 kvm_timer_vcpu_put(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500367}
368
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500369int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
370 struct kvm_mp_state *mp_state)
371{
Eric Auger37815282015-09-25 23:41:14 +0200372 if (vcpu->arch.power_off)
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000373 mp_state->mp_state = KVM_MP_STATE_STOPPED;
374 else
375 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
376
377 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500378}
379
380int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
381 struct kvm_mp_state *mp_state)
382{
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000383 switch (mp_state->mp_state) {
384 case KVM_MP_STATE_RUNNABLE:
Eric Auger37815282015-09-25 23:41:14 +0200385 vcpu->arch.power_off = false;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000386 break;
387 case KVM_MP_STATE_STOPPED:
Eric Auger37815282015-09-25 23:41:14 +0200388 vcpu->arch.power_off = true;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000389 break;
390 default:
391 return -EINVAL;
392 }
393
394 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500395}
396
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500397/**
398 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
399 * @v: The VCPU pointer
400 *
401 * If the guest CPU is not waiting for interrupts or an interrupt line is
402 * asserted, the CPU is by definition runnable.
403 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500404int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
405{
Eric Auger4f5f1dc2015-09-25 23:41:15 +0200406 return ((!!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v))
Eric Auger3b928302015-09-25 23:41:17 +0200407 && !v->arch.power_off && !v->arch.pause);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500408}
409
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500410/* Just ensure a guest exit from a particular CPU */
411static void exit_vm_noop(void *info)
412{
413}
414
415void force_vm_exit(const cpumask_t *mask)
416{
Eric Auger898f9492016-03-07 23:50:36 +0700417 preempt_disable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500418 smp_call_function_many(mask, exit_vm_noop, NULL, true);
Eric Auger898f9492016-03-07 23:50:36 +0700419 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500420}
421
422/**
423 * need_new_vmid_gen - check that the VMID is still valid
Andrea Gelmini6a727b02016-05-21 13:48:35 +0200424 * @kvm: The VM's VMID to check
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500425 *
426 * return true if there is a new generation of VMIDs being used
427 *
428 * The hardware supports only 256 values with the value zero reserved for the
429 * host, so we check if an assigned value belongs to a previous generation,
430 * which which requires us to assign a new value. If we're the first to use a
431 * VMID for the new generation, we must flush necessary caches and TLBs on all
432 * CPUs.
433 */
434static bool need_new_vmid_gen(struct kvm *kvm)
435{
436 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
437}
438
439/**
440 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
441 * @kvm The guest that we are about to run
442 *
443 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
444 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
445 * caches and TLBs.
446 */
447static void update_vttbr(struct kvm *kvm)
448{
449 phys_addr_t pgd_phys;
450 u64 vmid;
451
452 if (!need_new_vmid_gen(kvm))
453 return;
454
455 spin_lock(&kvm_vmid_lock);
456
457 /*
458 * We need to re-check the vmid_gen here to ensure that if another vcpu
459 * already allocated a valid vmid for this vm, then this vcpu should
460 * use the same vmid.
461 */
462 if (!need_new_vmid_gen(kvm)) {
463 spin_unlock(&kvm_vmid_lock);
464 return;
465 }
466
467 /* First user of a new VMID generation? */
468 if (unlikely(kvm_next_vmid == 0)) {
469 atomic64_inc(&kvm_vmid_gen);
470 kvm_next_vmid = 1;
471
472 /*
473 * On SMP we know no other CPUs can use this CPU's or each
474 * other's VMID after force_vm_exit returns since the
475 * kvm_vmid_lock blocks them from reentry to the guest.
476 */
477 force_vm_exit(cpu_all_mask);
478 /*
479 * Now broadcast TLB + ICACHE invalidation over the inner
480 * shareable domain to make sure all data structures are
481 * clean.
482 */
483 kvm_call_hyp(__kvm_flush_vm_context);
484 }
485
486 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
487 kvm->arch.vmid = kvm_next_vmid;
488 kvm_next_vmid++;
Vladimir Murzin20475f72015-11-16 11:28:18 +0000489 kvm_next_vmid &= (1 << kvm_vmid_bits) - 1;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500490
491 /* update vttbr to be used with the new vmid */
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000492 pgd_phys = virt_to_phys(kvm->arch.pgd);
Joel Schoppdbff1242014-07-09 11:17:04 -0500493 BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
Vladimir Murzin20475f72015-11-16 11:28:18 +0000494 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits);
Joel Schoppdbff1242014-07-09 11:17:04 -0500495 kvm->arch.vttbr = pgd_phys | vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500496
497 spin_unlock(&kvm_vmid_lock);
498}
499
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500500static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
501{
Christoffer Dall05971122014-12-12 21:19:23 +0100502 struct kvm *kvm = vcpu->kvm;
Christoffer Dall41a54482016-05-18 16:26:00 +0100503 int ret = 0;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700504
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500505 if (likely(vcpu->arch.has_run_once))
506 return 0;
507
508 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500509
510 /*
Peter Maydell6d3cfbe2014-12-04 15:02:24 +0000511 * Map the VGIC hardware resources before running a vcpu the first
512 * time on this VM.
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500513 */
Pavel Fedinc2f58512015-08-05 11:53:57 +0100514 if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) {
Christoffer Dall05971122014-12-12 21:19:23 +0100515 ret = kvm_vgic_map_resources(kvm);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500516 if (ret)
517 return ret;
518 }
519
Christoffer Dall05971122014-12-12 21:19:23 +0100520 /*
521 * Enable the arch timers only if we have an in-kernel VGIC
522 * and it has been properly initialized, since we cannot handle
523 * interrupts from the virtual timer with a userspace gic.
524 */
525 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
Christoffer Dall41a54482016-05-18 16:26:00 +0100526 ret = kvm_timer_enable(vcpu);
Christoffer Dall05971122014-12-12 21:19:23 +0100527
Christoffer Dall41a54482016-05-18 16:26:00 +0100528 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500529}
530
Eric Augerc1426e42015-03-04 11:14:34 +0100531bool kvm_arch_intc_initialized(struct kvm *kvm)
532{
533 return vgic_initialized(kvm);
534}
535
Christoffer Dallb13216c2016-04-27 10:28:00 +0100536void kvm_arm_halt_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200537{
538 int i;
539 struct kvm_vcpu *vcpu;
540
541 kvm_for_each_vcpu(i, vcpu, kvm)
542 vcpu->arch.pause = true;
Christoffer Dallb13216c2016-04-27 10:28:00 +0100543 kvm_make_all_cpus_request(kvm, KVM_REQ_VCPU_EXIT);
Eric Auger3b928302015-09-25 23:41:17 +0200544}
545
Christoffer Dall35a2d582016-05-20 15:25:28 +0200546void kvm_arm_halt_vcpu(struct kvm_vcpu *vcpu)
547{
548 vcpu->arch.pause = true;
549 kvm_vcpu_kick(vcpu);
550}
551
552void kvm_arm_resume_vcpu(struct kvm_vcpu *vcpu)
Christoffer Dallb13216c2016-04-27 10:28:00 +0100553{
554 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
555
556 vcpu->arch.pause = false;
557 swake_up(wq);
558}
559
560void kvm_arm_resume_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200561{
562 int i;
563 struct kvm_vcpu *vcpu;
564
Christoffer Dallb13216c2016-04-27 10:28:00 +0100565 kvm_for_each_vcpu(i, vcpu, kvm)
566 kvm_arm_resume_vcpu(vcpu);
Eric Auger3b928302015-09-25 23:41:17 +0200567}
568
Eric Auger37815282015-09-25 23:41:14 +0200569static void vcpu_sleep(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500570{
Marcelo Tosatti85773702016-02-19 09:46:39 +0100571 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500572
Marcelo Tosatti85773702016-02-19 09:46:39 +0100573 swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
Eric Auger3b928302015-09-25 23:41:17 +0200574 (!vcpu->arch.pause)));
Marc Zyngieraa024c22013-01-20 18:28:13 -0500575}
576
Andre Przywarae8180dc2013-05-09 00:28:06 +0200577static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
578{
579 return vcpu->arch.target >= 0;
580}
581
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500582/**
583 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
584 * @vcpu: The VCPU pointer
585 * @run: The kvm_run structure pointer used for userspace state exchange
586 *
587 * This function is called through the VCPU_RUN ioctl called from user space. It
588 * will execute VM code in a loop until the time slice for the process is used
589 * or some emulation is needed from user space in which case the function will
590 * return with return value 0 and with the kvm_run structure filled in with the
591 * required data for the requested emulation.
592 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500593int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
594{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500595 int ret;
596 sigset_t sigsaved;
597
Andre Przywarae8180dc2013-05-09 00:28:06 +0200598 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500599 return -ENOEXEC;
600
601 ret = kvm_vcpu_first_run_init(vcpu);
602 if (ret)
603 return ret;
604
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500605 if (run->exit_reason == KVM_EXIT_MMIO) {
606 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
607 if (ret)
608 return ret;
609 }
610
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100611 if (run->immediate_exit)
612 return -EINTR;
613
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500614 if (vcpu->sigset_active)
615 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
616
617 ret = 1;
618 run->exit_reason = KVM_EXIT_UNKNOWN;
619 while (ret > 0) {
620 /*
621 * Check conditions before entering the guest
622 */
623 cond_resched();
624
625 update_vttbr(vcpu->kvm);
626
Eric Auger3b928302015-09-25 23:41:17 +0200627 if (vcpu->arch.power_off || vcpu->arch.pause)
Eric Auger37815282015-09-25 23:41:14 +0200628 vcpu_sleep(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500629
Marc Zyngierabdf5842015-06-08 15:00:28 +0100630 /*
Marc Zyngierabdf5842015-06-08 15:00:28 +0100631 * Preparing the interrupts to be injected also
632 * involves poking the GIC, which must be done in a
633 * non-preemptible context.
634 */
635 preempt_disable();
Shannon Zhaob02386e2016-02-26 19:29:19 +0800636 kvm_pmu_flush_hwstate(vcpu);
Christoffer Dall7e16aa82015-11-24 10:31:07 +0100637 kvm_timer_flush_hwstate(vcpu);
Marc Zyngier9a99d052015-06-05 09:33:28 +0100638 kvm_vgic_flush_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500639
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500640 local_irq_disable();
641
642 /*
643 * Re-check atomic conditions
644 */
645 if (signal_pending(current)) {
646 ret = -EINTR;
647 run->exit_reason = KVM_EXIT_INTR;
648 }
649
Eric Auger101d3da2015-09-25 23:41:16 +0200650 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
Eric Auger3b928302015-09-25 23:41:17 +0200651 vcpu->arch.power_off || vcpu->arch.pause) {
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500652 local_irq_enable();
Shannon Zhaob02386e2016-02-26 19:29:19 +0800653 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200654 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500655 kvm_vgic_sync_hwstate(vcpu);
Marc Zyngierabdf5842015-06-08 15:00:28 +0100656 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500657 continue;
658 }
659
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100660 kvm_arm_setup_debug(vcpu);
661
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500662 /**************************************************************
663 * Enter the guest
664 */
665 trace_kvm_entry(*vcpu_pc(vcpu));
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200666 guest_enter_irqoff();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500667 vcpu->mode = IN_GUEST_MODE;
668
669 ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
670
671 vcpu->mode = OUTSIDE_GUEST_MODE;
Amit Tomarb19e6892015-11-26 10:09:43 +0000672 vcpu->stat.exits++;
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100673 /*
674 * Back from guest
675 *************************************************************/
676
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100677 kvm_arm_clear_debug(vcpu);
678
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500679 /*
680 * We may have taken a host interrupt in HYP mode (ie
681 * while executing the guest). This interrupt is still
682 * pending, as we haven't serviced it yet!
683 *
684 * We're now back in SVC mode, with interrupts
685 * disabled. Enabling the interrupts now will have
686 * the effect of taking the interrupt again, in SVC
687 * mode this time.
688 */
689 local_irq_enable();
690
691 /*
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200692 * We do local_irq_enable() before calling guest_exit() so
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100693 * that if a timer interrupt hits while running the guest we
694 * account that tick as being spent in the guest. We enable
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200695 * preemption after calling guest_exit() so that if we get
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100696 * preempted we make sure ticks after that is not counted as
697 * guest time.
698 */
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200699 guest_exit();
Christoffer Dallb5905dc2015-08-30 15:55:22 +0200700 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100701
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200702 /*
Shannon Zhaob02386e2016-02-26 19:29:19 +0800703 * We must sync the PMU and timer state before the vgic state so
704 * that the vgic can properly sample the updated state of the
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200705 * interrupt line.
706 */
Shannon Zhaob02386e2016-02-26 19:29:19 +0800707 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200708 kvm_timer_sync_hwstate(vcpu);
709
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500710 kvm_vgic_sync_hwstate(vcpu);
Marc Zyngierabdf5842015-06-08 15:00:28 +0100711
712 preempt_enable();
713
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500714 ret = handle_exit(vcpu, run, ret);
715 }
716
717 if (vcpu->sigset_active)
718 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
719 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500720}
721
Christoffer Dall86ce8532013-01-20 18:28:08 -0500722static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
723{
724 int bit_index;
725 bool set;
726 unsigned long *ptr;
727
728 if (number == KVM_ARM_IRQ_CPU_IRQ)
729 bit_index = __ffs(HCR_VI);
730 else /* KVM_ARM_IRQ_CPU_FIQ */
731 bit_index = __ffs(HCR_VF);
732
733 ptr = (unsigned long *)&vcpu->arch.irq_lines;
734 if (level)
735 set = test_and_set_bit(bit_index, ptr);
736 else
737 set = test_and_clear_bit(bit_index, ptr);
738
739 /*
740 * If we didn't change anything, no need to wake up or kick other CPUs
741 */
742 if (set == level)
743 return 0;
744
745 /*
746 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
747 * trigger a world-switch round on the running physical CPU to set the
748 * virtual IRQ/FIQ fields in the HCR appropriately.
749 */
750 kvm_vcpu_kick(vcpu);
751
752 return 0;
753}
754
Alexander Graf79558f12013-04-16 19:21:41 +0200755int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
756 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500757{
758 u32 irq = irq_level->irq;
759 unsigned int irq_type, vcpu_idx, irq_num;
760 int nrcpus = atomic_read(&kvm->online_vcpus);
761 struct kvm_vcpu *vcpu = NULL;
762 bool level = irq_level->level;
763
764 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
765 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
766 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
767
768 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
769
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500770 switch (irq_type) {
771 case KVM_ARM_IRQ_TYPE_CPU:
772 if (irqchip_in_kernel(kvm))
773 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500774
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500775 if (vcpu_idx >= nrcpus)
776 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500777
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500778 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
779 if (!vcpu)
780 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500781
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500782 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
783 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500784
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500785 return vcpu_interrupt_line(vcpu, irq_num, level);
786 case KVM_ARM_IRQ_TYPE_PPI:
787 if (!irqchip_in_kernel(kvm))
788 return -ENXIO;
789
790 if (vcpu_idx >= nrcpus)
791 return -EINVAL;
792
793 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
794 if (!vcpu)
795 return -EINVAL;
796
797 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
798 return -EINVAL;
799
800 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level);
801 case KVM_ARM_IRQ_TYPE_SPI:
802 if (!irqchip_in_kernel(kvm))
803 return -ENXIO;
804
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100805 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500806 return -EINVAL;
807
808 return kvm_vgic_inject_irq(kvm, 0, irq_num, level);
809 }
810
811 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500812}
813
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200814static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
815 const struct kvm_vcpu_init *init)
816{
817 unsigned int i;
818 int phys_target = kvm_target_cpu();
819
820 if (init->target != phys_target)
821 return -EINVAL;
822
823 /*
824 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
825 * use the same target.
826 */
827 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
828 return -EINVAL;
829
830 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
831 for (i = 0; i < sizeof(init->features) * 8; i++) {
832 bool set = (init->features[i / 32] & (1 << (i % 32)));
833
834 if (set && i >= KVM_VCPU_MAX_FEATURES)
835 return -ENOENT;
836
837 /*
838 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
839 * use the same feature set.
840 */
841 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
842 test_bit(i, vcpu->arch.features) != set)
843 return -EINVAL;
844
845 if (set)
846 set_bit(i, vcpu->arch.features);
847 }
848
849 vcpu->arch.target = phys_target;
850
851 /* Now we know what it is, we can reset it. */
852 return kvm_reset_vcpu(vcpu);
853}
854
855
Christoffer Dall478a8232013-11-19 17:43:19 -0800856static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
857 struct kvm_vcpu_init *init)
858{
859 int ret;
860
861 ret = kvm_vcpu_set_target(vcpu, init);
862 if (ret)
863 return ret;
864
Christoffer Dall957db102014-11-27 10:35:03 +0100865 /*
866 * Ensure a rebooted VM will fault in RAM pages and detect if the
867 * guest MMU is turned off and flush the caches as needed.
868 */
869 if (vcpu->arch.has_run_once)
870 stage2_unmap_vm(vcpu->kvm);
871
Christoffer Dallb856a592014-10-16 17:21:16 +0200872 vcpu_reset_hcr(vcpu);
873
Christoffer Dall478a8232013-11-19 17:43:19 -0800874 /*
Eric Auger37815282015-09-25 23:41:14 +0200875 * Handle the "start in power-off" case.
Christoffer Dall478a8232013-11-19 17:43:19 -0800876 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100877 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Eric Auger37815282015-09-25 23:41:14 +0200878 vcpu->arch.power_off = true;
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +0200879 else
Eric Auger37815282015-09-25 23:41:14 +0200880 vcpu->arch.power_off = false;
Christoffer Dall478a8232013-11-19 17:43:19 -0800881
882 return 0;
883}
884
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800885static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
886 struct kvm_device_attr *attr)
887{
888 int ret = -ENXIO;
889
890 switch (attr->group) {
891 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800892 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800893 break;
894 }
895
896 return ret;
897}
898
899static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
900 struct kvm_device_attr *attr)
901{
902 int ret = -ENXIO;
903
904 switch (attr->group) {
905 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800906 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800907 break;
908 }
909
910 return ret;
911}
912
913static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
914 struct kvm_device_attr *attr)
915{
916 int ret = -ENXIO;
917
918 switch (attr->group) {
919 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800920 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800921 break;
922 }
923
924 return ret;
925}
926
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500927long kvm_arch_vcpu_ioctl(struct file *filp,
928 unsigned int ioctl, unsigned long arg)
929{
930 struct kvm_vcpu *vcpu = filp->private_data;
931 void __user *argp = (void __user *)arg;
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800932 struct kvm_device_attr attr;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500933
934 switch (ioctl) {
935 case KVM_ARM_VCPU_INIT: {
936 struct kvm_vcpu_init init;
937
938 if (copy_from_user(&init, argp, sizeof(init)))
939 return -EFAULT;
940
Christoffer Dall478a8232013-11-19 17:43:19 -0800941 return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500942 }
943 case KVM_SET_ONE_REG:
944 case KVM_GET_ONE_REG: {
945 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +0200946
947 if (unlikely(!kvm_vcpu_initialized(vcpu)))
948 return -ENOEXEC;
949
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500950 if (copy_from_user(&reg, argp, sizeof(reg)))
951 return -EFAULT;
952 if (ioctl == KVM_SET_ONE_REG)
953 return kvm_arm_set_reg(vcpu, &reg);
954 else
955 return kvm_arm_get_reg(vcpu, &reg);
956 }
957 case KVM_GET_REG_LIST: {
958 struct kvm_reg_list __user *user_list = argp;
959 struct kvm_reg_list reg_list;
960 unsigned n;
961
Andre Przywarae8180dc2013-05-09 00:28:06 +0200962 if (unlikely(!kvm_vcpu_initialized(vcpu)))
963 return -ENOEXEC;
964
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500965 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
966 return -EFAULT;
967 n = reg_list.n;
968 reg_list.n = kvm_arm_num_regs(vcpu);
969 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
970 return -EFAULT;
971 if (n < reg_list.n)
972 return -E2BIG;
973 return kvm_arm_copy_reg_indices(vcpu, user_list->reg);
974 }
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800975 case KVM_SET_DEVICE_ATTR: {
976 if (copy_from_user(&attr, argp, sizeof(attr)))
977 return -EFAULT;
978 return kvm_arm_vcpu_set_attr(vcpu, &attr);
979 }
980 case KVM_GET_DEVICE_ATTR: {
981 if (copy_from_user(&attr, argp, sizeof(attr)))
982 return -EFAULT;
983 return kvm_arm_vcpu_get_attr(vcpu, &attr);
984 }
985 case KVM_HAS_DEVICE_ATTR: {
986 if (copy_from_user(&attr, argp, sizeof(attr)))
987 return -EFAULT;
988 return kvm_arm_vcpu_has_attr(vcpu, &attr);
989 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500990 default:
991 return -EINVAL;
992 }
993}
994
Mario Smarduch53c810c2015-01-15 15:58:57 -0800995/**
996 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
997 * @kvm: kvm instance
998 * @log: slot id and address to which we copy the log
999 *
1000 * Steps 1-4 below provide general overview of dirty page logging. See
1001 * kvm_get_dirty_log_protect() function description for additional details.
1002 *
1003 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1004 * always flush the TLB (step 4) even if previous step failed and the dirty
1005 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1006 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1007 * writes will be marked dirty for next log read.
1008 *
1009 * 1. Take a snapshot of the bit and clear it if needed.
1010 * 2. Write protect the corresponding page.
1011 * 3. Copy the snapshot to the userspace.
1012 * 4. Flush TLB's if needed.
1013 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001014int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1015{
Mario Smarduch53c810c2015-01-15 15:58:57 -08001016 bool is_dirty = false;
1017 int r;
1018
1019 mutex_lock(&kvm->slots_lock);
1020
1021 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
1022
1023 if (is_dirty)
1024 kvm_flush_remote_tlbs(kvm);
1025
1026 mutex_unlock(&kvm->slots_lock);
1027 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001028}
1029
Christoffer Dall3401d5462013-01-23 13:18:04 -05001030static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1031 struct kvm_arm_device_addr *dev_addr)
1032{
Christoffer Dall330690c2013-01-21 19:36:13 -05001033 unsigned long dev_id, type;
1034
1035 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1036 KVM_ARM_DEVICE_ID_SHIFT;
1037 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1038 KVM_ARM_DEVICE_TYPE_SHIFT;
1039
1040 switch (dev_id) {
1041 case KVM_ARM_DEVICE_VGIC_V2:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001042 if (!vgic_present)
1043 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001044 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -05001045 default:
1046 return -ENODEV;
1047 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001048}
1049
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001050long kvm_arch_vm_ioctl(struct file *filp,
1051 unsigned int ioctl, unsigned long arg)
1052{
Christoffer Dall3401d5462013-01-23 13:18:04 -05001053 struct kvm *kvm = filp->private_data;
1054 void __user *argp = (void __user *)arg;
1055
1056 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001057 case KVM_CREATE_IRQCHIP: {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001058 int ret;
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001059 if (!vgic_present)
1060 return -ENXIO;
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001061 mutex_lock(&kvm->lock);
1062 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1063 mutex_unlock(&kvm->lock);
1064 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001065 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001066 case KVM_ARM_SET_DEVICE_ADDR: {
1067 struct kvm_arm_device_addr dev_addr;
1068
1069 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1070 return -EFAULT;
1071 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1072 }
Anup Patel42c4e0c2013-09-30 14:20:07 +05301073 case KVM_ARM_PREFERRED_TARGET: {
1074 int err;
1075 struct kvm_vcpu_init init;
1076
1077 err = kvm_vcpu_preferred_target(&init);
1078 if (err)
1079 return err;
1080
1081 if (copy_to_user(argp, &init, sizeof(init)))
1082 return -EFAULT;
1083
1084 return 0;
1085 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001086 default:
1087 return -EINVAL;
1088 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001089}
1090
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001091static void cpu_init_hyp_mode(void *dummy)
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001092{
Marc Zyngierdac288f2013-05-14 12:11:37 +01001093 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001094 unsigned long hyp_stack_ptr;
1095 unsigned long stack_page;
1096 unsigned long vector_ptr;
1097
1098 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001099 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001100
Marc Zyngierdac288f2013-05-14 12:11:37 +01001101 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +01001102 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001103 hyp_stack_ptr = stack_page + PAGE_SIZE;
Ard Biesheuvela0bf9772016-02-16 13:52:39 +01001104 vector_ptr = (unsigned long)kvm_ksym_ref(__kvm_hyp_vector);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001105
Marc Zyngier12fda812016-06-30 18:40:45 +01001106 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
Marc Zyngier35a24912016-02-01 17:54:35 +00001107 __cpu_init_stage2();
Alex Bennée56c7f5e2015-07-07 17:29:56 +01001108
Jintack Lim488f94d2016-12-01 14:32:05 -05001109 if (is_kernel_in_hyp_mode())
1110 kvm_timer_init_vhe();
1111
Alex Bennée56c7f5e2015-07-07 17:29:56 +01001112 kvm_arm_init_debug();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001113}
1114
James Morse5f5560b2016-03-30 18:33:04 +01001115static void cpu_hyp_reinit(void)
1116{
1117 if (is_kernel_in_hyp_mode()) {
1118 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001119 * __cpu_init_stage2() is safe to call even if the PM
James Morse5f5560b2016-03-30 18:33:04 +01001120 * event was cancelled before the CPU was reset.
1121 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001122 __cpu_init_stage2();
James Morse5f5560b2016-03-30 18:33:04 +01001123 } else {
1124 if (__hyp_get_vectors() == hyp_default_vectors)
1125 cpu_init_hyp_mode(NULL);
1126 }
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001127
1128 if (vgic_present)
1129 kvm_vgic_init_cpu_hardware();
James Morse5f5560b2016-03-30 18:33:04 +01001130}
1131
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001132static void cpu_hyp_reset(void)
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001133{
Marc Zyngier12fda812016-06-30 18:40:45 +01001134 if (!is_kernel_in_hyp_mode())
Marc Zyngiere537ecd2016-06-30 18:40:48 +01001135 __cpu_reset_hyp_mode(hyp_default_vectors,
1136 kvm_get_idmap_start());
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001137}
1138
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001139static void _kvm_arch_hardware_enable(void *discard)
1140{
1141 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1142 cpu_hyp_reinit();
1143 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1144 }
1145}
1146
1147int kvm_arch_hardware_enable(void)
1148{
1149 _kvm_arch_hardware_enable(NULL);
1150 return 0;
1151}
1152
1153static void _kvm_arch_hardware_disable(void *discard)
1154{
1155 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1156 cpu_hyp_reset();
1157 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1158 }
1159}
1160
1161void kvm_arch_hardware_disable(void)
1162{
1163 _kvm_arch_hardware_disable(NULL);
1164}
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001165
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001166#ifdef CONFIG_CPU_PM
1167static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1168 unsigned long cmd,
1169 void *v)
1170{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001171 /*
1172 * kvm_arm_hardware_enabled is left with its old value over
1173 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1174 * re-enable hyp.
1175 */
1176 switch (cmd) {
1177 case CPU_PM_ENTER:
1178 if (__this_cpu_read(kvm_arm_hardware_enabled))
1179 /*
1180 * don't update kvm_arm_hardware_enabled here
1181 * so that the hardware will be re-enabled
1182 * when we resume. See below.
1183 */
1184 cpu_hyp_reset();
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001185
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001186 return NOTIFY_OK;
1187 case CPU_PM_EXIT:
1188 if (__this_cpu_read(kvm_arm_hardware_enabled))
1189 /* The hardware was enabled before suspend. */
1190 cpu_hyp_reinit();
1191
1192 return NOTIFY_OK;
1193
1194 default:
1195 return NOTIFY_DONE;
1196 }
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001197}
1198
1199static struct notifier_block hyp_init_cpu_pm_nb = {
1200 .notifier_call = hyp_init_cpu_pm_notifier,
1201};
1202
1203static void __init hyp_cpu_pm_init(void)
1204{
1205 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1206}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001207static void __init hyp_cpu_pm_exit(void)
1208{
1209 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1210}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001211#else
1212static inline void hyp_cpu_pm_init(void)
1213{
1214}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001215static inline void hyp_cpu_pm_exit(void)
1216{
1217}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001218#endif
1219
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001220static void teardown_common_resources(void)
1221{
1222 free_percpu(kvm_host_cpu_state);
1223}
1224
1225static int init_common_resources(void)
1226{
1227 kvm_host_cpu_state = alloc_percpu(kvm_cpu_context_t);
1228 if (!kvm_host_cpu_state) {
1229 kvm_err("Cannot allocate host CPU state\n");
1230 return -ENOMEM;
1231 }
1232
Vladimir Murzin61349932016-09-22 09:18:01 +01001233 /* set size of VMID supported by CPU */
1234 kvm_vmid_bits = kvm_get_vmid_bits();
1235 kvm_info("%d-bit VMID\n", kvm_vmid_bits);
1236
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001237 return 0;
1238}
1239
1240static int init_subsystems(void)
1241{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001242 int err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001243
1244 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001245 * Enable hardware so that subsystem initialisation can access EL2.
James Morse5f5560b2016-03-30 18:33:04 +01001246 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001247 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
James Morse5f5560b2016-03-30 18:33:04 +01001248
1249 /*
1250 * Register CPU lower-power notifier
1251 */
1252 hyp_cpu_pm_init();
1253
1254 /*
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001255 * Init HYP view of VGIC
1256 */
1257 err = kvm_vgic_hyp_init();
1258 switch (err) {
1259 case 0:
1260 vgic_present = true;
1261 break;
1262 case -ENODEV:
1263 case -ENXIO:
1264 vgic_present = false;
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001265 err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001266 break;
1267 default:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001268 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001269 }
1270
1271 /*
1272 * Init HYP architected timer support
1273 */
1274 err = kvm_timer_hyp_init();
1275 if (err)
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001276 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001277
1278 kvm_perf_init();
1279 kvm_coproc_table_init();
1280
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001281out:
1282 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1283
1284 return err;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001285}
1286
1287static void teardown_hyp_mode(void)
1288{
1289 int cpu;
1290
1291 if (is_kernel_in_hyp_mode())
1292 return;
1293
1294 free_hyp_pgds();
1295 for_each_possible_cpu(cpu)
1296 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
Sudeep Holla06a71a22016-04-04 14:46:51 +01001297 hyp_cpu_pm_exit();
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001298}
1299
1300static int init_vhe_mode(void)
1301{
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001302 kvm_info("VHE mode initialized successfully\n");
1303 return 0;
1304}
1305
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001306/**
1307 * Inits Hyp-mode on all online CPUs
1308 */
1309static int init_hyp_mode(void)
1310{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001311 int cpu;
1312 int err = 0;
1313
1314 /*
1315 * Allocate Hyp PGD and setup Hyp identity mapping
1316 */
1317 err = kvm_mmu_init();
1318 if (err)
1319 goto out_err;
1320
1321 /*
1322 * It is probably enough to obtain the default on one
1323 * CPU. It's unlikely to be different on the others.
1324 */
1325 hyp_default_vectors = __hyp_get_vectors();
1326
1327 /*
1328 * Allocate stack pages for Hypervisor-mode
1329 */
1330 for_each_possible_cpu(cpu) {
1331 unsigned long stack_page;
1332
1333 stack_page = __get_free_page(GFP_KERNEL);
1334 if (!stack_page) {
1335 err = -ENOMEM;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001336 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001337 }
1338
1339 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1340 }
1341
1342 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001343 * Map the Hyp-code called directly from the host
1344 */
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001345 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001346 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001347 if (err) {
1348 kvm_err("Cannot map world-switch code\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001349 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001350 }
1351
Ard Biesheuvela0bf9772016-02-16 13:52:39 +01001352 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
Marc Zyngier74a6b882016-06-13 15:00:47 +01001353 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
Marc Zyngier910917b2015-10-27 12:18:48 +00001354 if (err) {
1355 kvm_err("Cannot map rodata section\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001356 goto out_err;
Marc Zyngier910917b2015-10-27 12:18:48 +00001357 }
1358
Marc Zyngierc8ea0392016-10-20 10:17:21 +01001359 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1360 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1361 if (err) {
1362 kvm_err("Cannot map bss section\n");
1363 goto out_err;
1364 }
1365
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001366 /*
1367 * Map the Hyp stack pages
1368 */
1369 for_each_possible_cpu(cpu) {
1370 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001371 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1372 PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001373
1374 if (err) {
1375 kvm_err("Cannot map hyp stack\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001376 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001377 }
1378 }
1379
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001380 for_each_possible_cpu(cpu) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001381 kvm_cpu_context_t *cpu_ctxt;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001382
Marc Zyngier3de50da2013-04-08 16:47:19 +01001383 cpu_ctxt = per_cpu_ptr(kvm_host_cpu_state, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001384 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1, PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001385
1386 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001387 kvm_err("Cannot map host CPU state: %d\n", err);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001388 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001389 }
1390 }
1391
1392 kvm_info("Hyp mode initialized successfully\n");
Marc Zyngier210552c2013-03-05 03:18:00 +00001393
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001394 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001395
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001396out_err:
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001397 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001398 kvm_err("error initializing Hyp mode: %d\n", err);
1399 return err;
1400}
1401
Andre Przywarad4e071c2013-04-17 12:52:01 +02001402static void check_kvm_target_cpu(void *ret)
1403{
1404 *(int *)ret = kvm_target_cpu();
1405}
1406
Andre Przywara4429fc62014-06-02 15:37:13 +02001407struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1408{
1409 struct kvm_vcpu *vcpu;
1410 int i;
1411
1412 mpidr &= MPIDR_HWID_BITMASK;
1413 kvm_for_each_vcpu(i, vcpu, kvm) {
1414 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1415 return vcpu;
1416 }
1417 return NULL;
1418}
1419
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001420/**
1421 * Initialize Hyp-mode and memory mappings on all CPUs.
1422 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001423int kvm_arch_init(void *opaque)
1424{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001425 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001426 int ret, cpu;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001427
1428 if (!is_hyp_mode_available()) {
1429 kvm_err("HYP mode not available\n");
1430 return -ENODEV;
1431 }
1432
Andre Przywarad4e071c2013-04-17 12:52:01 +02001433 for_each_online_cpu(cpu) {
1434 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1435 if (ret < 0) {
1436 kvm_err("Error, CPU %d not supported!\n", cpu);
1437 return -ENODEV;
1438 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001439 }
1440
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001441 err = init_common_resources();
1442 if (err)
1443 return err;
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301444
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001445 if (is_kernel_in_hyp_mode())
1446 err = init_vhe_mode();
1447 else
1448 err = init_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001449 if (err)
1450 goto out_err;
1451
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001452 err = init_subsystems();
1453 if (err)
1454 goto out_hyp;
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001455
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001456 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001457
1458out_hyp:
1459 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001460out_err:
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001461 teardown_common_resources();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001462 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001463}
1464
1465/* NOP: Compiling as a module not supported */
1466void kvm_arch_exit(void)
1467{
Marc Zyngier210552c2013-03-05 03:18:00 +00001468 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001469}
1470
1471static int arm_init(void)
1472{
1473 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1474 return rc;
1475}
1476
1477module_init(arm_init);