blob: acc43242a3101cbae075e6a589cc5450027b40e9 [file] [log] [blame]
Thomas Gleixnerd94d71c2019-05-29 07:12:40 -07001// SPDX-License-Identifier: GPL-2.0-only
Christoffer Dall749cf76c2013-01-20 18:28:06 -05002/*
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
Christoffer Dall749cf76c2013-01-20 18:28:06 -05005 */
6
Dave Martin85acda32018-04-20 16:20:43 +01007#include <linux/bug.h>
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01008#include <linux/cpu_pm.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -05009#include <linux/errno.h>
10#include <linux/err.h>
11#include <linux/kvm_host.h>
Andre Przywara1085fdc2016-07-15 12:43:31 +010012#include <linux/list.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050013#include <linux/module.h>
14#include <linux/vmalloc.h>
15#include <linux/fs.h>
16#include <linux/mman.h>
17#include <linux/sched.h>
Christoffer Dall86ce8532013-01-20 18:28:08 -050018#include <linux/kvm.h>
Eric Auger24124052017-10-27 15:28:31 +010019#include <linux/kvm_irqfd.h>
20#include <linux/irqbypass.h>
Marc Zyngierde737082018-06-21 10:43:59 +010021#include <linux/sched/stat.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050022#include <trace/events/kvm.h>
Shannon Zhaob02386e2016-02-26 19:29:19 +080023#include <kvm/arm_pmu.h>
Marc Zyngier1a2fb942018-02-06 17:56:08 +000024#include <kvm/arm_psci.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050025
26#define CREATE_TRACE_POINTS
27#include "trace.h"
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050030#include <asm/ptrace.h>
31#include <asm/mman.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050032#include <asm/tlbflush.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050033#include <asm/cacheflush.h>
Dave Martin85acda32018-04-20 16:20:43 +010034#include <asm/cpufeature.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050035#include <asm/virt.h>
36#include <asm/kvm_arm.h>
37#include <asm/kvm_asm.h>
38#include <asm/kvm_mmu.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050039#include <asm/kvm_emulate.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050040#include <asm/kvm_coproc.h>
Marc Zyngier910917b2015-10-27 12:18:48 +000041#include <asm/sections.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050042
43#ifdef REQUIRES_VIRT
44__asm__(".arch_extension virt");
45#endif
46
Andrew Murray630a1682019-04-09 20:22:11 +010047DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050048static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050049
Marc Zyngier1638a122013-01-21 19:36:11 -050050/* Per-CPU variable containing the currently running vcpu. */
51static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
52
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050053/* The VMID used in the VTTBR */
54static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
Vladimir Murzin20475f72015-11-16 11:28:18 +000055static u32 kvm_next_vmid;
Christoffer Dallfb544d12018-12-11 13:23:57 +010056static DEFINE_SPINLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050057
Pavel Fedinc7da6fa2015-12-18 14:38:43 +030058static bool vgic_present;
59
AKASHI Takahiro67f69192016-04-27 17:47:05 +010060static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
61
Marc Zyngier1638a122013-01-21 19:36:11 -050062static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
63{
Christoph Lameter1436c1a2013-10-21 13:17:08 +010064 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050065}
66
Christoffer Dall61bbe382017-10-27 19:57:51 +020067DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
68
Marc Zyngier1638a122013-01-21 19:36:11 -050069/**
70 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
71 * Must be called from non-preemptible context
72 */
73struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
74{
Christoph Lameter1436c1a2013-10-21 13:17:08 +010075 return __this_cpu_read(kvm_arm_running_vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050076}
77
78/**
79 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
80 */
Will Deacon4000be42014-08-26 15:13:21 +010081struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
Marc Zyngier1638a122013-01-21 19:36:11 -050082{
83 return &kvm_arm_running_vcpu;
84}
85
Christoffer Dall749cf76c2013-01-20 18:28:06 -050086int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
87{
88 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
89}
90
Christoffer Dall749cf76c2013-01-20 18:28:06 -050091int kvm_arch_hardware_setup(void)
92{
93 return 0;
94}
95
Sean Christophersonf257d6d2019-04-19 22:18:17 -070096int kvm_arch_check_processor_compat(void)
Christoffer Dall749cf76c2013-01-20 18:28:06 -050097{
Sean Christophersonf257d6d2019-04-19 22:18:17 -070098 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -050099}
100
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500101
Christoffer Dalld5d81842013-01-20 18:28:07 -0500102/**
103 * kvm_arch_init_vm - initializes a VM data structure
104 * @kvm: pointer to the KVM struct
105 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500106int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
107{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100108 int ret, cpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500109
Marc Zyngierbca607e2018-10-01 13:40:36 +0100110 ret = kvm_arm_setup_stage2(kvm, type);
Suzuki K Poulose5b6c6742018-09-26 17:32:42 +0100111 if (ret)
112 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500113
Marc Zyngier94d0e592016-10-18 18:37:49 +0100114 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
115 if (!kvm->arch.last_vcpu_ran)
116 return -ENOMEM;
117
118 for_each_possible_cpu(cpu)
119 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
120
Christoffer Dalld5d81842013-01-20 18:28:07 -0500121 ret = kvm_alloc_stage2_pgd(kvm);
122 if (ret)
123 goto out_fail_alloc;
124
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100125 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500126 if (ret)
127 goto out_free_stage2_pgd;
128
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100129 kvm_vgic_early_init(kvm);
Christoffer Dalla1a64382013-11-16 10:51:25 -0800130
Christoffer Dalld5d81842013-01-20 18:28:07 -0500131 /* Mark the initial VMID generation invalid */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100132 kvm->arch.vmid.vmid_gen = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500133
Andre Przywara3caa2d82014-06-02 16:26:01 +0200134 /* The maximum number of VCPUs is limited by the host's GIC model */
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300135 kvm->arch.max_vcpus = vgic_present ?
136 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
Andre Przywara3caa2d82014-06-02 16:26:01 +0200137
Christoffer Dalld5d81842013-01-20 18:28:07 -0500138 return ret;
139out_free_stage2_pgd:
140 kvm_free_stage2_pgd(kvm);
141out_fail_alloc:
Marc Zyngier94d0e592016-10-18 18:37:49 +0100142 free_percpu(kvm->arch.last_vcpu_ran);
143 kvm->arch.last_vcpu_ran = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500144 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500145}
146
Luiz Capitulino235539b2016-09-07 14:47:23 -0400147bool kvm_arch_has_vcpu_debugfs(void)
148{
149 return false;
150}
151
152int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
153{
154 return 0;
155}
156
Souptick Joarder1499fa82018-04-19 00:49:58 +0530157vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500158{
159 return VM_FAULT_SIGBUS;
160}
161
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500162
Christoffer Dalld5d81842013-01-20 18:28:07 -0500163/**
164 * kvm_arch_destroy_vm - destroy the VM data structure
165 * @kvm: pointer to the KVM struct
166 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500167void kvm_arch_destroy_vm(struct kvm *kvm)
168{
169 int i;
170
Marc Zyngierb2c9a852017-10-27 15:28:34 +0100171 kvm_vgic_destroy(kvm);
172
Marc Zyngier94d0e592016-10-18 18:37:49 +0100173 free_percpu(kvm->arch.last_vcpu_ran);
174 kvm->arch.last_vcpu_ran = NULL;
175
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500176 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
177 if (kvm->vcpus[i]) {
178 kvm_arch_vcpu_free(kvm->vcpus[i]);
179 kvm->vcpus[i] = NULL;
180 }
181 }
Andrew Jones6b2ad812017-11-27 19:17:18 +0100182 atomic_set(&kvm->online_vcpus, 0);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500183}
184
Alexander Graf784aa3d2014-07-14 18:27:35 +0200185int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500186{
187 int r;
188 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500189 case KVM_CAP_IRQCHIP:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300190 r = vgic_present;
191 break;
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000192 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100193 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500194 case KVM_CAP_USER_MEMORY:
195 case KVM_CAP_SYNC_MMU:
196 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
197 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500198 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530199 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200200 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000201 case KVM_CAP_MP_STATE:
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100202 case KVM_CAP_IMMEDIATE_EXIT:
Dongjiu Geng58bf4372018-10-13 00:12:49 +0800203 case KVM_CAP_VCPU_EVENTS:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500204 r = 1;
205 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500206 case KVM_CAP_ARM_SET_DEVICE_ADDR:
207 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100208 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500209 case KVM_CAP_NR_VCPUS:
210 r = num_online_cpus();
211 break;
212 case KVM_CAP_MAX_VCPUS:
213 r = KVM_MAX_VCPUS;
214 break;
Thomas Hutha86cb412019-05-23 18:43:08 +0200215 case KVM_CAP_MAX_VCPU_ID:
216 r = KVM_MAX_VCPU_ID;
217 break;
Vladimir Murzin29885092016-11-02 11:55:34 +0000218 case KVM_CAP_MSI_DEVID:
219 if (!kvm)
220 r = -EINVAL;
221 else
222 r = kvm->arch.vgic.msis_require_devid;
223 break;
Christoffer Dallf7214e62017-02-01 12:54:11 +0100224 case KVM_CAP_ARM_USER_IRQ:
225 /*
226 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
227 * (bump this number if adding more devices)
228 */
229 r = 1;
230 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500231 default:
Dongjiu Geng375bdd32018-10-13 00:12:48 +0800232 r = kvm_arch_vm_ioctl_check_extension(kvm, ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500233 break;
234 }
235 return r;
236}
237
238long kvm_arch_dev_ioctl(struct file *filp,
239 unsigned int ioctl, unsigned long arg)
240{
241 return -EINVAL;
242}
243
Marc Orrd1e5b0e2018-05-15 04:37:37 -0700244struct kvm *kvm_arch_alloc_vm(void)
245{
246 if (!has_vhe())
247 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
248
249 return vzalloc(sizeof(struct kvm));
250}
251
252void kvm_arch_free_vm(struct kvm *kvm)
253{
254 if (!has_vhe())
255 kfree(kvm);
256 else
257 vfree(kvm);
258}
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500259
260struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
261{
262 int err;
263 struct kvm_vcpu *vcpu;
264
Christoffer Dall716139d2014-12-09 14:33:45 +0100265 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
266 err = -EBUSY;
267 goto out;
268 }
269
Andre Przywara3caa2d82014-06-02 16:26:01 +0200270 if (id >= kvm->arch.max_vcpus) {
271 err = -EINVAL;
272 goto out;
273 }
274
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500275 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
276 if (!vcpu) {
277 err = -ENOMEM;
278 goto out;
279 }
280
281 err = kvm_vcpu_init(vcpu, kvm, id);
282 if (err)
283 goto free_vcpu;
284
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100285 err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500286 if (err)
287 goto vcpu_uninit;
288
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500289 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500290vcpu_uninit:
291 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500292free_vcpu:
293 kmem_cache_free(kvm_vcpu_cache, vcpu);
294out:
295 return ERR_PTR(err);
296}
297
Dominik Dingel31928aa2014-12-04 15:47:07 +0100298void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500299{
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500300}
301
302void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
303{
Christoffer Dallf1d72312018-01-25 18:32:29 +0100304 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
305 static_branch_dec(&userspace_irqchip_in_use);
306
Christoffer Dalld5d81842013-01-20 18:28:07 -0500307 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500308 kvm_timer_vcpu_terminate(vcpu);
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800309 kvm_pmu_vcpu_destroy(vcpu);
James Morse591d2152016-06-08 17:24:45 +0100310 kvm_vcpu_uninit(vcpu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500311 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500312}
313
314void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
315{
316 kvm_arch_vcpu_free(vcpu);
317}
318
319int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
320{
Christoffer Dall1c88ab72017-01-06 16:07:48 +0100321 return kvm_timer_is_pending(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500322}
323
Christoffer Dalld35268d2015-08-25 19:48:21 +0200324void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
325{
Marc Zyngierdf9ba952017-10-27 15:28:49 +0100326 kvm_vgic_v4_enable_doorbell(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200327}
328
329void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
330{
Marc Zyngierdf9ba952017-10-27 15:28:49 +0100331 kvm_vgic_v4_disable_doorbell(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200332}
333
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500334int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
335{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500336 /* Force users to call KVM_ARM_VCPU_INIT */
337 vcpu->arch.target = -1;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200338 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500339
Marc Zyngier967f8422013-01-23 13:21:59 -0500340 /* Set up the timer */
341 kvm_timer_vcpu_init(vcpu);
342
Alex Bennée84e690b2015-07-07 17:30:00 +0100343 kvm_arm_reset_debug_ptr(vcpu);
344
Christoffer Dall1aab6f42017-05-08 12:30:24 +0200345 return kvm_vgic_vcpu_init(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500346}
347
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500348void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
349{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100350 int *last_ran;
Andrew Murray630a1682019-04-09 20:22:11 +0100351 kvm_host_data_t *cpu_data;
Marc Zyngier94d0e592016-10-18 18:37:49 +0100352
353 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
Andrew Murray630a1682019-04-09 20:22:11 +0100354 cpu_data = this_cpu_ptr(&kvm_host_data);
Marc Zyngier94d0e592016-10-18 18:37:49 +0100355
356 /*
357 * We might get preempted before the vCPU actually runs, but
358 * over-invalidation doesn't affect correctness.
359 */
360 if (*last_ran != vcpu->vcpu_id) {
361 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
362 *last_ran = vcpu->vcpu_id;
363 }
364
Christoffer Dall86ce8532013-01-20 18:28:08 -0500365 vcpu->cpu = cpu;
Andrew Murray630a1682019-04-09 20:22:11 +0100366 vcpu->arch.host_cpu_context = &cpu_data->host_ctxt;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500367
Marc Zyngier1638a122013-01-21 19:36:11 -0500368 kvm_arm_set_running_vcpu(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100369 kvm_vgic_load(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200370 kvm_timer_vcpu_load(vcpu);
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200371 kvm_vcpu_load_sysregs(vcpu);
Dave Martine6b673b2018-04-06 14:55:59 +0100372 kvm_arch_vcpu_load_fp(vcpu);
Andrew Murray435e53f2019-04-09 20:22:15 +0100373 kvm_vcpu_pmu_restore_guest(vcpu);
Marc Zyngierde737082018-06-21 10:43:59 +0100374
375 if (single_task_running())
376 vcpu_clear_wfe_traps(vcpu);
377 else
378 vcpu_set_wfe_traps(vcpu);
Mark Rutland384b40c2019-04-23 10:12:35 +0530379
380 vcpu_ptrauth_setup_lazy(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500381}
382
383void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
384{
Dave Martine6b673b2018-04-06 14:55:59 +0100385 kvm_arch_vcpu_put_fp(vcpu);
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200386 kvm_vcpu_put_sysregs(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200387 kvm_timer_vcpu_put(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100388 kvm_vgic_put(vcpu);
Andrew Murray435e53f2019-04-09 20:22:15 +0100389 kvm_vcpu_pmu_restore_host(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100390
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800391 vcpu->cpu = -1;
392
Marc Zyngier1638a122013-01-21 19:36:11 -0500393 kvm_arm_set_running_vcpu(NULL);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500394}
395
Andrew Jones424c9892017-06-04 14:43:57 +0200396static void vcpu_power_off(struct kvm_vcpu *vcpu)
397{
398 vcpu->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200399 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones424c9892017-06-04 14:43:57 +0200400 kvm_vcpu_kick(vcpu);
401}
402
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500403int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
404 struct kvm_mp_state *mp_state)
405{
Eric Auger37815282015-09-25 23:41:14 +0200406 if (vcpu->arch.power_off)
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000407 mp_state->mp_state = KVM_MP_STATE_STOPPED;
408 else
409 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
410
411 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500412}
413
414int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
415 struct kvm_mp_state *mp_state)
416{
Christoffer Dalle83dff52017-12-04 21:35:31 +0100417 int ret = 0;
418
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000419 switch (mp_state->mp_state) {
420 case KVM_MP_STATE_RUNNABLE:
Eric Auger37815282015-09-25 23:41:14 +0200421 vcpu->arch.power_off = false;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000422 break;
423 case KVM_MP_STATE_STOPPED:
Andrew Jones424c9892017-06-04 14:43:57 +0200424 vcpu_power_off(vcpu);
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000425 break;
426 default:
Christoffer Dalle83dff52017-12-04 21:35:31 +0100427 ret = -EINVAL;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000428 }
429
Christoffer Dalle83dff52017-12-04 21:35:31 +0100430 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500431}
432
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500433/**
434 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
435 * @v: The VCPU pointer
436 *
437 * If the guest CPU is not waiting for interrupts or an interrupt line is
438 * asserted, the CPU is by definition runnable.
439 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500440int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
441{
Christoffer Dall3df59d82017-08-03 12:09:05 +0200442 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
443 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
Eric Auger3b928302015-09-25 23:41:17 +0200444 && !v->arch.power_off && !v->arch.pause);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500445}
446
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800447bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
448{
Longpeng(Mike)f01fbd22017-08-08 12:05:35 +0800449 return vcpu_mode_priv(vcpu);
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800450}
451
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500452/* Just ensure a guest exit from a particular CPU */
453static void exit_vm_noop(void *info)
454{
455}
456
457void force_vm_exit(const cpumask_t *mask)
458{
Eric Auger898f9492016-03-07 23:50:36 +0700459 preempt_disable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500460 smp_call_function_many(mask, exit_vm_noop, NULL, true);
Eric Auger898f9492016-03-07 23:50:36 +0700461 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500462}
463
464/**
465 * need_new_vmid_gen - check that the VMID is still valid
Christoffer Dalle329fb72018-12-11 15:26:31 +0100466 * @vmid: The VMID to check
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500467 *
468 * return true if there is a new generation of VMIDs being used
469 *
Christoffer Dalle329fb72018-12-11 15:26:31 +0100470 * The hardware supports a limited set of values with the value zero reserved
471 * for the host, so we check if an assigned value belongs to a previous
472 * generation, which which requires us to assign a new value. If we're the
473 * first to use a VMID for the new generation, we must flush necessary caches
474 * and TLBs on all CPUs.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500475 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100476static bool need_new_vmid_gen(struct kvm_vmid *vmid)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500477{
Christoffer Dallfb544d12018-12-11 13:23:57 +0100478 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
479 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100480 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500481}
482
483/**
Christoffer Dalle329fb72018-12-11 15:26:31 +0100484 * update_vmid - Update the vmid with a valid VMID for the current generation
485 * @kvm: The guest that struct vmid belongs to
486 * @vmid: The stage-2 VMID information struct
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500487 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100488static void update_vmid(struct kvm_vmid *vmid)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500489{
Christoffer Dalle329fb72018-12-11 15:26:31 +0100490 if (!need_new_vmid_gen(vmid))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500491 return;
492
Christoffer Dallfb544d12018-12-11 13:23:57 +0100493 spin_lock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500494
495 /*
496 * We need to re-check the vmid_gen here to ensure that if another vcpu
497 * already allocated a valid vmid for this vm, then this vcpu should
498 * use the same vmid.
499 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100500 if (!need_new_vmid_gen(vmid)) {
Christoffer Dallfb544d12018-12-11 13:23:57 +0100501 spin_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500502 return;
503 }
504
505 /* First user of a new VMID generation? */
506 if (unlikely(kvm_next_vmid == 0)) {
507 atomic64_inc(&kvm_vmid_gen);
508 kvm_next_vmid = 1;
509
510 /*
511 * On SMP we know no other CPUs can use this CPU's or each
512 * other's VMID after force_vm_exit returns since the
513 * kvm_vmid_lock blocks them from reentry to the guest.
514 */
515 force_vm_exit(cpu_all_mask);
516 /*
517 * Now broadcast TLB + ICACHE invalidation over the inner
518 * shareable domain to make sure all data structures are
519 * clean.
520 */
521 kvm_call_hyp(__kvm_flush_vm_context);
522 }
523
Christoffer Dalle329fb72018-12-11 15:26:31 +0100524 vmid->vmid = kvm_next_vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500525 kvm_next_vmid++;
Christoffer Dalle329fb72018-12-11 15:26:31 +0100526 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500527
Christoffer Dallfb544d12018-12-11 13:23:57 +0100528 smp_wmb();
Christoffer Dalle329fb72018-12-11 15:26:31 +0100529 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
Christoffer Dallfb544d12018-12-11 13:23:57 +0100530
531 spin_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500532}
533
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500534static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
535{
Christoffer Dall05971122014-12-12 21:19:23 +0100536 struct kvm *kvm = vcpu->kvm;
Christoffer Dall41a54482016-05-18 16:26:00 +0100537 int ret = 0;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700538
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500539 if (likely(vcpu->arch.has_run_once))
540 return 0;
541
Dave Martin7dd32a02018-12-19 14:27:01 +0000542 if (!kvm_arm_vcpu_is_finalized(vcpu))
543 return -EPERM;
544
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500545 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500546
Christoffer Dall61bbe382017-10-27 19:57:51 +0200547 if (likely(irqchip_in_kernel(kvm))) {
548 /*
549 * Map the VGIC hardware resources before running a vcpu the
550 * first time on this VM.
551 */
552 if (unlikely(!vgic_ready(kvm))) {
553 ret = kvm_vgic_map_resources(kvm);
554 if (ret)
555 return ret;
556 }
557 } else {
558 /*
559 * Tell the rest of the code that there are userspace irqchip
560 * VMs in the wild.
561 */
562 static_branch_inc(&userspace_irqchip_in_use);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500563 }
564
Alexander Grafd9e13972016-09-27 21:08:06 +0200565 ret = kvm_timer_enable(vcpu);
Christoffer Dalla2befac2017-05-02 13:41:02 +0200566 if (ret)
567 return ret;
568
569 ret = kvm_arm_pmu_v3_enable(vcpu);
Christoffer Dall05971122014-12-12 21:19:23 +0100570
Christoffer Dall41a54482016-05-18 16:26:00 +0100571 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500572}
573
Eric Augerc1426e42015-03-04 11:14:34 +0100574bool kvm_arch_intc_initialized(struct kvm *kvm)
575{
576 return vgic_initialized(kvm);
577}
578
Christoffer Dallb13216c2016-04-27 10:28:00 +0100579void kvm_arm_halt_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200580{
581 int i;
582 struct kvm_vcpu *vcpu;
583
584 kvm_for_each_vcpu(i, vcpu, kvm)
585 vcpu->arch.pause = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200586 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
Eric Auger3b928302015-09-25 23:41:17 +0200587}
588
Christoffer Dallb13216c2016-04-27 10:28:00 +0100589void kvm_arm_resume_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200590{
591 int i;
592 struct kvm_vcpu *vcpu;
593
Christoffer Dallabd72292017-05-06 20:01:24 +0200594 kvm_for_each_vcpu(i, vcpu, kvm) {
595 vcpu->arch.pause = false;
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200596 swake_up_one(kvm_arch_vcpu_wq(vcpu));
Christoffer Dallabd72292017-05-06 20:01:24 +0200597 }
Eric Auger3b928302015-09-25 23:41:17 +0200598}
599
Andrew Jones7b244e22017-06-04 14:43:58 +0200600static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500601{
Marcelo Tosatti85773702016-02-19 09:46:39 +0100602 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500603
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200604 swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) &&
Eric Auger3b928302015-09-25 23:41:17 +0200605 (!vcpu->arch.pause)));
Andrew Jones0592c002017-06-04 14:43:55 +0200606
Andrew Jones424c9892017-06-04 14:43:57 +0200607 if (vcpu->arch.power_off || vcpu->arch.pause) {
Andrew Jones0592c002017-06-04 14:43:55 +0200608 /* Awaken to handle a signal, request we sleep again later. */
Andrew Jones7b244e22017-06-04 14:43:58 +0200609 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones0592c002017-06-04 14:43:55 +0200610 }
Marc Zyngier358b28f2018-12-20 11:36:07 +0000611
612 /*
613 * Make sure we will observe a potential reset request if we've
614 * observed a change to the power state. Pairs with the smp_wmb() in
615 * kvm_psci_vcpu_on().
616 */
617 smp_rmb();
Marc Zyngieraa024c22013-01-20 18:28:13 -0500618}
619
Andre Przywarae8180dc2013-05-09 00:28:06 +0200620static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
621{
622 return vcpu->arch.target >= 0;
623}
624
Andrew Jones0592c002017-06-04 14:43:55 +0200625static void check_vcpu_requests(struct kvm_vcpu *vcpu)
626{
627 if (kvm_request_pending(vcpu)) {
Andrew Jones7b244e22017-06-04 14:43:58 +0200628 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
629 vcpu_req_sleep(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200630
Marc Zyngier358b28f2018-12-20 11:36:07 +0000631 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
632 kvm_reset_vcpu(vcpu);
633
Andrew Jones325f9c62017-06-04 14:43:59 +0200634 /*
635 * Clear IRQ_PENDING requests that were made to guarantee
636 * that a VCPU sees new virtual interrupts.
637 */
638 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
Andrew Jones0592c002017-06-04 14:43:55 +0200639 }
640}
641
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500642/**
643 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
644 * @vcpu: The VCPU pointer
645 * @run: The kvm_run structure pointer used for userspace state exchange
646 *
647 * This function is called through the VCPU_RUN ioctl called from user space. It
648 * will execute VM code in a loop until the time slice for the process is used
649 * or some emulation is needed from user space in which case the function will
650 * return with return value 0 and with the kvm_run structure filled in with the
651 * required data for the requested emulation.
652 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500653int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
654{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500655 int ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500656
Andre Przywarae8180dc2013-05-09 00:28:06 +0200657 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500658 return -ENOEXEC;
659
660 ret = kvm_vcpu_first_run_init(vcpu);
661 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100662 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500663
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500664 if (run->exit_reason == KVM_EXIT_MMIO) {
665 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
666 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100667 return ret;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500668 }
669
Christoffer Dall829a5862017-11-29 16:37:53 +0100670 if (run->immediate_exit)
671 return -EINTR;
672
673 vcpu_load(vcpu);
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100674
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100675 kvm_sigset_activate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500676
677 ret = 1;
678 run->exit_reason = KVM_EXIT_UNKNOWN;
679 while (ret > 0) {
680 /*
681 * Check conditions before entering the guest
682 */
683 cond_resched();
684
Christoffer Dalle329fb72018-12-11 15:26:31 +0100685 update_vmid(&vcpu->kvm->arch.vmid);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500686
Andrew Jones0592c002017-06-04 14:43:55 +0200687 check_vcpu_requests(vcpu);
688
Marc Zyngierabdf5842015-06-08 15:00:28 +0100689 /*
Marc Zyngierabdf5842015-06-08 15:00:28 +0100690 * Preparing the interrupts to be injected also
691 * involves poking the GIC, which must be done in a
692 * non-preemptible context.
693 */
694 preempt_disable();
Christoffer Dall328e5662016-03-24 11:21:04 +0100695
Shannon Zhaob02386e2016-02-26 19:29:19 +0800696 kvm_pmu_flush_hwstate(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100697
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500698 local_irq_disable();
699
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500700 kvm_vgic_flush_hwstate(vcpu);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500701
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500702 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200703 * Exit if we have a signal pending so that we can deliver the
704 * signal to user space.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500705 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200706 if (signal_pending(current)) {
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500707 ret = -EINTR;
708 run->exit_reason = KVM_EXIT_INTR;
709 }
710
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200711 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200712 * If we're using a userspace irqchip, then check if we need
713 * to tell a userspace irqchip about timer or PMU level
714 * changes and if so, exit to userspace (the actual level
715 * state gets updated in kvm_timer_update_run and
716 * kvm_pmu_update_run below).
717 */
718 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
719 if (kvm_timer_should_notify_user(vcpu) ||
720 kvm_pmu_should_notify_user(vcpu)) {
721 ret = -EINTR;
722 run->exit_reason = KVM_EXIT_INTR;
723 }
724 }
725
726 /*
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200727 * Ensure we set mode to IN_GUEST_MODE after we disable
728 * interrupts and before the final VCPU requests check.
729 * See the comment in kvm_vcpu_exiting_guest_mode() and
Christoph Hellwig2f5947d2019-07-24 09:24:49 +0200730 * Documentation/virt/kvm/vcpu-requests.rst
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200731 */
732 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
733
Christoffer Dalle329fb72018-12-11 15:26:31 +0100734 if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) ||
Andrew Jones424c9892017-06-04 14:43:57 +0200735 kvm_request_pending(vcpu)) {
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200736 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall771621b2017-10-04 23:42:32 +0200737 isb(); /* Ensure work in x_flush_hwstate is committed */
Shannon Zhaob02386e2016-02-26 19:29:19 +0800738 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dall61bbe382017-10-27 19:57:51 +0200739 if (static_branch_unlikely(&userspace_irqchip_in_use))
740 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500741 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200742 local_irq_enable();
Marc Zyngierabdf5842015-06-08 15:00:28 +0100743 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500744 continue;
745 }
746
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100747 kvm_arm_setup_debug(vcpu);
748
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500749 /**************************************************************
750 * Enter the guest
751 */
752 trace_kvm_entry(*vcpu_pc(vcpu));
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200753 guest_enter_irqoff();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200754
755 if (has_vhe()) {
James Morse4f5abad2018-01-15 19:39:00 +0000756 kvm_arm_vhe_guest_enter();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200757 ret = kvm_vcpu_run_vhe(vcpu);
James Morse4f5abad2018-01-15 19:39:00 +0000758 kvm_arm_vhe_guest_exit();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200759 } else {
Marc Zyngier7aa8d142019-01-05 15:49:50 +0000760 ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu);
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200761 }
762
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500763 vcpu->mode = OUTSIDE_GUEST_MODE;
Amit Tomarb19e6892015-11-26 10:09:43 +0000764 vcpu->stat.exits++;
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100765 /*
766 * Back from guest
767 *************************************************************/
768
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100769 kvm_arm_clear_debug(vcpu);
770
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500771 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200772 * We must sync the PMU state before the vgic state so
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200773 * that the vgic can properly sample the updated state of the
774 * interrupt line.
775 */
776 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200777
Christoffer Dallb103cc32016-10-16 20:30:38 +0200778 /*
779 * Sync the vgic state before syncing the timer state because
780 * the timer code needs to know if the virtual timer
781 * interrupts are active.
782 */
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200783 kvm_vgic_sync_hwstate(vcpu);
784
785 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200786 * Sync the timer hardware state before enabling interrupts as
787 * we don't want vtimer interrupts to race with syncing the
788 * timer virtual interrupt state.
789 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200790 if (static_branch_unlikely(&userspace_irqchip_in_use))
791 kvm_timer_sync_hwstate(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200792
Dave Martine6b673b2018-04-06 14:55:59 +0100793 kvm_arch_vcpu_ctxsync_fp(vcpu);
794
Christoffer Dallb103cc32016-10-16 20:30:38 +0200795 /*
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500796 * We may have taken a host interrupt in HYP mode (ie
797 * while executing the guest). This interrupt is still
798 * pending, as we haven't serviced it yet!
799 *
800 * We're now back in SVC mode, with interrupts
801 * disabled. Enabling the interrupts now will have
802 * the effect of taking the interrupt again, in SVC
803 * mode this time.
804 */
805 local_irq_enable();
806
807 /*
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200808 * We do local_irq_enable() before calling guest_exit() so
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100809 * that if a timer interrupt hits while running the guest we
810 * account that tick as being spent in the guest. We enable
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200811 * preemption after calling guest_exit() so that if we get
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100812 * preempted we make sure ticks after that is not counted as
813 * guest time.
814 */
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200815 guest_exit();
Christoffer Dallb5905dc2015-08-30 15:55:22 +0200816 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100817
James Morse3368bd82018-01-15 19:39:04 +0000818 /* Exit types that need handling before we can be preempted */
819 handle_exit_early(vcpu, run, ret);
820
Marc Zyngierabdf5842015-06-08 15:00:28 +0100821 preempt_enable();
822
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500823 ret = handle_exit(vcpu, run, ret);
824 }
825
Alexander Grafd9e13972016-09-27 21:08:06 +0200826 /* Tell userspace about in-kernel device output levels */
Christoffer Dall3dbbdf72017-02-01 12:51:52 +0100827 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
828 kvm_timer_update_run(vcpu);
829 kvm_pmu_update_run(vcpu);
830 }
Alexander Grafd9e13972016-09-27 21:08:06 +0200831
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100832 kvm_sigset_deactivate(vcpu);
833
Christoffer Dallaccb7572017-12-04 21:35:25 +0100834 vcpu_put(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500835 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500836}
837
Christoffer Dall86ce8532013-01-20 18:28:08 -0500838static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
839{
840 int bit_index;
841 bool set;
Christoffer Dall3df59d82017-08-03 12:09:05 +0200842 unsigned long *hcr;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500843
844 if (number == KVM_ARM_IRQ_CPU_IRQ)
845 bit_index = __ffs(HCR_VI);
846 else /* KVM_ARM_IRQ_CPU_FIQ */
847 bit_index = __ffs(HCR_VF);
848
Christoffer Dall3df59d82017-08-03 12:09:05 +0200849 hcr = vcpu_hcr(vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500850 if (level)
Christoffer Dall3df59d82017-08-03 12:09:05 +0200851 set = test_and_set_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500852 else
Christoffer Dall3df59d82017-08-03 12:09:05 +0200853 set = test_and_clear_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500854
855 /*
856 * If we didn't change anything, no need to wake up or kick other CPUs
857 */
858 if (set == level)
859 return 0;
860
861 /*
862 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
863 * trigger a world-switch round on the running physical CPU to set the
864 * virtual IRQ/FIQ fields in the HCR appropriately.
865 */
Andrew Jones325f9c62017-06-04 14:43:59 +0200866 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500867 kvm_vcpu_kick(vcpu);
868
869 return 0;
870}
871
Alexander Graf79558f12013-04-16 19:21:41 +0200872int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
873 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500874{
875 u32 irq = irq_level->irq;
876 unsigned int irq_type, vcpu_idx, irq_num;
877 int nrcpus = atomic_read(&kvm->online_vcpus);
878 struct kvm_vcpu *vcpu = NULL;
879 bool level = irq_level->level;
880
881 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
882 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
883 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
884
885 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
886
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500887 switch (irq_type) {
888 case KVM_ARM_IRQ_TYPE_CPU:
889 if (irqchip_in_kernel(kvm))
890 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500891
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500892 if (vcpu_idx >= nrcpus)
893 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500894
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500895 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
896 if (!vcpu)
897 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500898
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500899 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
900 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500901
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500902 return vcpu_interrupt_line(vcpu, irq_num, level);
903 case KVM_ARM_IRQ_TYPE_PPI:
904 if (!irqchip_in_kernel(kvm))
905 return -ENXIO;
906
907 if (vcpu_idx >= nrcpus)
908 return -EINVAL;
909
910 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
911 if (!vcpu)
912 return -EINVAL;
913
914 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
915 return -EINVAL;
916
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200917 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500918 case KVM_ARM_IRQ_TYPE_SPI:
919 if (!irqchip_in_kernel(kvm))
920 return -ENXIO;
921
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100922 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500923 return -EINVAL;
924
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200925 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500926 }
927
928 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500929}
930
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200931static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
932 const struct kvm_vcpu_init *init)
933{
Andrew Jones811328f2019-04-04 19:42:30 +0200934 unsigned int i, ret;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200935 int phys_target = kvm_target_cpu();
936
937 if (init->target != phys_target)
938 return -EINVAL;
939
940 /*
941 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
942 * use the same target.
943 */
944 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
945 return -EINVAL;
946
947 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
948 for (i = 0; i < sizeof(init->features) * 8; i++) {
949 bool set = (init->features[i / 32] & (1 << (i % 32)));
950
951 if (set && i >= KVM_VCPU_MAX_FEATURES)
952 return -ENOENT;
953
954 /*
955 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
956 * use the same feature set.
957 */
958 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
959 test_bit(i, vcpu->arch.features) != set)
960 return -EINVAL;
961
962 if (set)
963 set_bit(i, vcpu->arch.features);
964 }
965
966 vcpu->arch.target = phys_target;
967
968 /* Now we know what it is, we can reset it. */
Andrew Jones811328f2019-04-04 19:42:30 +0200969 ret = kvm_reset_vcpu(vcpu);
970 if (ret) {
971 vcpu->arch.target = -1;
972 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
973 }
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200974
Andrew Jones811328f2019-04-04 19:42:30 +0200975 return ret;
976}
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200977
Christoffer Dall478a8232013-11-19 17:43:19 -0800978static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
979 struct kvm_vcpu_init *init)
980{
981 int ret;
982
983 ret = kvm_vcpu_set_target(vcpu, init);
984 if (ret)
985 return ret;
986
Christoffer Dall957db102014-11-27 10:35:03 +0100987 /*
988 * Ensure a rebooted VM will fault in RAM pages and detect if the
989 * guest MMU is turned off and flush the caches as needed.
990 */
991 if (vcpu->arch.has_run_once)
992 stage2_unmap_vm(vcpu->kvm);
993
Christoffer Dallb856a592014-10-16 17:21:16 +0200994 vcpu_reset_hcr(vcpu);
995
Christoffer Dall478a8232013-11-19 17:43:19 -0800996 /*
Eric Auger37815282015-09-25 23:41:14 +0200997 * Handle the "start in power-off" case.
Christoffer Dall478a8232013-11-19 17:43:19 -0800998 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100999 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Andrew Jones424c9892017-06-04 14:43:57 +02001000 vcpu_power_off(vcpu);
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +02001001 else
Eric Auger37815282015-09-25 23:41:14 +02001002 vcpu->arch.power_off = false;
Christoffer Dall478a8232013-11-19 17:43:19 -08001003
1004 return 0;
1005}
1006
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001007static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1008 struct kvm_device_attr *attr)
1009{
1010 int ret = -ENXIO;
1011
1012 switch (attr->group) {
1013 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001014 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001015 break;
1016 }
1017
1018 return ret;
1019}
1020
1021static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1022 struct kvm_device_attr *attr)
1023{
1024 int ret = -ENXIO;
1025
1026 switch (attr->group) {
1027 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001028 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001029 break;
1030 }
1031
1032 return ret;
1033}
1034
1035static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1036 struct kvm_device_attr *attr)
1037{
1038 int ret = -ENXIO;
1039
1040 switch (attr->group) {
1041 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001042 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001043 break;
1044 }
1045
1046 return ret;
1047}
1048
James Morse539aee02018-07-19 16:24:24 +01001049static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1050 struct kvm_vcpu_events *events)
1051{
1052 memset(events, 0, sizeof(*events));
1053
1054 return __kvm_arm_vcpu_get_events(vcpu, events);
1055}
1056
1057static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1058 struct kvm_vcpu_events *events)
1059{
1060 int i;
1061
1062 /* check whether the reserved field is zero */
1063 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1064 if (events->reserved[i])
1065 return -EINVAL;
1066
1067 /* check whether the pad field is zero */
1068 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1069 if (events->exception.pad[i])
1070 return -EINVAL;
1071
1072 return __kvm_arm_vcpu_set_events(vcpu, events);
1073}
James Morse539aee02018-07-19 16:24:24 +01001074
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001075long kvm_arch_vcpu_ioctl(struct file *filp,
1076 unsigned int ioctl, unsigned long arg)
1077{
1078 struct kvm_vcpu *vcpu = filp->private_data;
1079 void __user *argp = (void __user *)arg;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001080 struct kvm_device_attr attr;
Christoffer Dall9b0624712017-12-04 21:35:36 +01001081 long r;
1082
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001083 switch (ioctl) {
1084 case KVM_ARM_VCPU_INIT: {
1085 struct kvm_vcpu_init init;
1086
Christoffer Dall9b0624712017-12-04 21:35:36 +01001087 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001088 if (copy_from_user(&init, argp, sizeof(init)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001089 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001090
Christoffer Dall9b0624712017-12-04 21:35:36 +01001091 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1092 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001093 }
1094 case KVM_SET_ONE_REG:
1095 case KVM_GET_ONE_REG: {
1096 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001097
Christoffer Dall9b0624712017-12-04 21:35:36 +01001098 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001099 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001100 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001101
Christoffer Dall9b0624712017-12-04 21:35:36 +01001102 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001103 if (copy_from_user(&reg, argp, sizeof(reg)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001104 break;
1105
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001106 if (ioctl == KVM_SET_ONE_REG)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001107 r = kvm_arm_set_reg(vcpu, &reg);
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001108 else
Christoffer Dall9b0624712017-12-04 21:35:36 +01001109 r = kvm_arm_get_reg(vcpu, &reg);
1110 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001111 }
1112 case KVM_GET_REG_LIST: {
1113 struct kvm_reg_list __user *user_list = argp;
1114 struct kvm_reg_list reg_list;
1115 unsigned n;
1116
Christoffer Dall9b0624712017-12-04 21:35:36 +01001117 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001118 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001119 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001120
Dave Martin7dd32a02018-12-19 14:27:01 +00001121 r = -EPERM;
1122 if (!kvm_arm_vcpu_is_finalized(vcpu))
1123 break;
1124
Christoffer Dall9b0624712017-12-04 21:35:36 +01001125 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001126 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001127 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001128 n = reg_list.n;
1129 reg_list.n = kvm_arm_num_regs(vcpu);
1130 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001131 break;
1132 r = -E2BIG;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001133 if (n < reg_list.n)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001134 break;
1135 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1136 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001137 }
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001138 case KVM_SET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001139 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001140 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001141 break;
1142 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1143 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001144 }
1145 case KVM_GET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001146 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001147 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001148 break;
1149 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1150 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001151 }
1152 case KVM_HAS_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001153 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001154 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001155 break;
1156 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1157 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001158 }
Dongjiu Gengb7b27fa2018-07-19 16:24:22 +01001159 case KVM_GET_VCPU_EVENTS: {
1160 struct kvm_vcpu_events events;
1161
1162 if (kvm_arm_vcpu_get_events(vcpu, &events))
1163 return -EINVAL;
1164
1165 if (copy_to_user(argp, &events, sizeof(events)))
1166 return -EFAULT;
1167
1168 return 0;
1169 }
1170 case KVM_SET_VCPU_EVENTS: {
1171 struct kvm_vcpu_events events;
1172
1173 if (copy_from_user(&events, argp, sizeof(events)))
1174 return -EFAULT;
1175
1176 return kvm_arm_vcpu_set_events(vcpu, &events);
1177 }
Dave Martin7dd32a02018-12-19 14:27:01 +00001178 case KVM_ARM_VCPU_FINALIZE: {
1179 int what;
1180
1181 if (!kvm_vcpu_initialized(vcpu))
1182 return -ENOEXEC;
1183
1184 if (get_user(what, (const int __user *)argp))
1185 return -EFAULT;
1186
1187 return kvm_arm_vcpu_finalize(vcpu, what);
1188 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001189 default:
Christoffer Dall9b0624712017-12-04 21:35:36 +01001190 r = -EINVAL;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001191 }
Christoffer Dall9b0624712017-12-04 21:35:36 +01001192
Christoffer Dall9b0624712017-12-04 21:35:36 +01001193 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001194}
1195
Mario Smarduch53c810c2015-01-15 15:58:57 -08001196/**
1197 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1198 * @kvm: kvm instance
1199 * @log: slot id and address to which we copy the log
1200 *
1201 * Steps 1-4 below provide general overview of dirty page logging. See
1202 * kvm_get_dirty_log_protect() function description for additional details.
1203 *
1204 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1205 * always flush the TLB (step 4) even if previous step failed and the dirty
1206 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1207 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1208 * writes will be marked dirty for next log read.
1209 *
1210 * 1. Take a snapshot of the bit and clear it if needed.
1211 * 2. Write protect the corresponding page.
1212 * 3. Copy the snapshot to the userspace.
1213 * 4. Flush TLB's if needed.
1214 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001215int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1216{
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02001217 bool flush = false;
Mario Smarduch53c810c2015-01-15 15:58:57 -08001218 int r;
1219
1220 mutex_lock(&kvm->slots_lock);
1221
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02001222 r = kvm_get_dirty_log_protect(kvm, log, &flush);
Mario Smarduch53c810c2015-01-15 15:58:57 -08001223
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02001224 if (flush)
Mario Smarduch53c810c2015-01-15 15:58:57 -08001225 kvm_flush_remote_tlbs(kvm);
1226
1227 mutex_unlock(&kvm->slots_lock);
1228 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001229}
1230
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001231int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
1232{
1233 bool flush = false;
1234 int r;
1235
1236 mutex_lock(&kvm->slots_lock);
1237
1238 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
1239
1240 if (flush)
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001241 kvm_flush_remote_tlbs(kvm);
1242
1243 mutex_unlock(&kvm->slots_lock);
1244 return r;
1245}
1246
Christoffer Dall3401d5462013-01-23 13:18:04 -05001247static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1248 struct kvm_arm_device_addr *dev_addr)
1249{
Christoffer Dall330690c2013-01-21 19:36:13 -05001250 unsigned long dev_id, type;
1251
1252 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1253 KVM_ARM_DEVICE_ID_SHIFT;
1254 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1255 KVM_ARM_DEVICE_TYPE_SHIFT;
1256
1257 switch (dev_id) {
1258 case KVM_ARM_DEVICE_VGIC_V2:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001259 if (!vgic_present)
1260 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001261 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -05001262 default:
1263 return -ENODEV;
1264 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001265}
1266
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001267long kvm_arch_vm_ioctl(struct file *filp,
1268 unsigned int ioctl, unsigned long arg)
1269{
Christoffer Dall3401d5462013-01-23 13:18:04 -05001270 struct kvm *kvm = filp->private_data;
1271 void __user *argp = (void __user *)arg;
1272
1273 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001274 case KVM_CREATE_IRQCHIP: {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001275 int ret;
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001276 if (!vgic_present)
1277 return -ENXIO;
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001278 mutex_lock(&kvm->lock);
1279 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1280 mutex_unlock(&kvm->lock);
1281 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001282 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001283 case KVM_ARM_SET_DEVICE_ADDR: {
1284 struct kvm_arm_device_addr dev_addr;
1285
1286 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1287 return -EFAULT;
1288 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1289 }
Anup Patel42c4e0c2013-09-30 14:20:07 +05301290 case KVM_ARM_PREFERRED_TARGET: {
1291 int err;
1292 struct kvm_vcpu_init init;
1293
1294 err = kvm_vcpu_preferred_target(&init);
1295 if (err)
1296 return err;
1297
1298 if (copy_to_user(argp, &init, sizeof(init)))
1299 return -EFAULT;
1300
1301 return 0;
1302 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001303 default:
1304 return -EINVAL;
1305 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001306}
1307
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001308static void cpu_init_hyp_mode(void *dummy)
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001309{
Marc Zyngierdac288f2013-05-14 12:11:37 +01001310 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001311 unsigned long hyp_stack_ptr;
1312 unsigned long stack_page;
1313 unsigned long vector_ptr;
1314
1315 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001316 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001317
Marc Zyngierdac288f2013-05-14 12:11:37 +01001318 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +01001319 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001320 hyp_stack_ptr = stack_page + PAGE_SIZE;
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001321 vector_ptr = (unsigned long)kvm_get_hyp_vector();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001322
Marc Zyngier12fda812016-06-30 18:40:45 +01001323 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
Marc Zyngier35a24912016-02-01 17:54:35 +00001324 __cpu_init_stage2();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001325}
1326
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001327static void cpu_hyp_reset(void)
1328{
1329 if (!is_kernel_in_hyp_mode())
1330 __hyp_reset_vectors();
1331}
1332
James Morse5f5560b2016-03-30 18:33:04 +01001333static void cpu_hyp_reinit(void)
1334{
Marc Zyngier1e0cf162019-07-05 23:35:56 +01001335 kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt);
1336
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001337 cpu_hyp_reset();
1338
Marc Zyngier9d47bb02018-10-01 13:41:32 +01001339 if (is_kernel_in_hyp_mode())
Hu Huajun02d50cd2017-06-12 22:37:48 +08001340 kvm_timer_init_vhe();
Marc Zyngier9d47bb02018-10-01 13:41:32 +01001341 else
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001342 cpu_init_hyp_mode(NULL);
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001343
Mark Rutlandda5a3ce2018-10-17 17:42:10 +01001344 kvm_arm_init_debug();
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001345
1346 if (vgic_present)
1347 kvm_vgic_init_cpu_hardware();
James Morse5f5560b2016-03-30 18:33:04 +01001348}
1349
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001350static void _kvm_arch_hardware_enable(void *discard)
1351{
1352 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1353 cpu_hyp_reinit();
1354 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1355 }
1356}
1357
1358int kvm_arch_hardware_enable(void)
1359{
1360 _kvm_arch_hardware_enable(NULL);
1361 return 0;
1362}
1363
1364static void _kvm_arch_hardware_disable(void *discard)
1365{
1366 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1367 cpu_hyp_reset();
1368 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1369 }
1370}
1371
1372void kvm_arch_hardware_disable(void)
1373{
1374 _kvm_arch_hardware_disable(NULL);
1375}
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001376
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001377#ifdef CONFIG_CPU_PM
1378static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1379 unsigned long cmd,
1380 void *v)
1381{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001382 /*
1383 * kvm_arm_hardware_enabled is left with its old value over
1384 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1385 * re-enable hyp.
1386 */
1387 switch (cmd) {
1388 case CPU_PM_ENTER:
1389 if (__this_cpu_read(kvm_arm_hardware_enabled))
1390 /*
1391 * don't update kvm_arm_hardware_enabled here
1392 * so that the hardware will be re-enabled
1393 * when we resume. See below.
1394 */
1395 cpu_hyp_reset();
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001396
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001397 return NOTIFY_OK;
James Morse58d6b152018-01-22 18:19:06 +00001398 case CPU_PM_ENTER_FAILED:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001399 case CPU_PM_EXIT:
1400 if (__this_cpu_read(kvm_arm_hardware_enabled))
1401 /* The hardware was enabled before suspend. */
1402 cpu_hyp_reinit();
1403
1404 return NOTIFY_OK;
1405
1406 default:
1407 return NOTIFY_DONE;
1408 }
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001409}
1410
1411static struct notifier_block hyp_init_cpu_pm_nb = {
1412 .notifier_call = hyp_init_cpu_pm_notifier,
1413};
1414
1415static void __init hyp_cpu_pm_init(void)
1416{
1417 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1418}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001419static void __init hyp_cpu_pm_exit(void)
1420{
1421 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1422}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001423#else
1424static inline void hyp_cpu_pm_init(void)
1425{
1426}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001427static inline void hyp_cpu_pm_exit(void)
1428{
1429}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001430#endif
1431
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001432static int init_common_resources(void)
1433{
Suzuki K Poulose0f62f0e2018-09-26 17:32:52 +01001434 kvm_set_ipa_limit();
1435
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001436 return 0;
1437}
1438
1439static int init_subsystems(void)
1440{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001441 int err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001442
1443 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001444 * Enable hardware so that subsystem initialisation can access EL2.
James Morse5f5560b2016-03-30 18:33:04 +01001445 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001446 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
James Morse5f5560b2016-03-30 18:33:04 +01001447
1448 /*
1449 * Register CPU lower-power notifier
1450 */
1451 hyp_cpu_pm_init();
1452
1453 /*
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001454 * Init HYP view of VGIC
1455 */
1456 err = kvm_vgic_hyp_init();
1457 switch (err) {
1458 case 0:
1459 vgic_present = true;
1460 break;
1461 case -ENODEV:
1462 case -ENXIO:
1463 vgic_present = false;
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001464 err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001465 break;
1466 default:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001467 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001468 }
1469
1470 /*
1471 * Init HYP architected timer support
1472 */
Marc Zyngierf384dcf2017-12-07 11:46:15 +00001473 err = kvm_timer_hyp_init(vgic_present);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001474 if (err)
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001475 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001476
1477 kvm_perf_init();
1478 kvm_coproc_table_init();
1479
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001480out:
1481 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1482
1483 return err;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001484}
1485
1486static void teardown_hyp_mode(void)
1487{
1488 int cpu;
1489
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001490 free_hyp_pgds();
1491 for_each_possible_cpu(cpu)
1492 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
Sudeep Holla06a71a22016-04-04 14:46:51 +01001493 hyp_cpu_pm_exit();
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001494}
1495
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001496/**
1497 * Inits Hyp-mode on all online CPUs
1498 */
1499static int init_hyp_mode(void)
1500{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001501 int cpu;
1502 int err = 0;
1503
1504 /*
1505 * Allocate Hyp PGD and setup Hyp identity mapping
1506 */
1507 err = kvm_mmu_init();
1508 if (err)
1509 goto out_err;
1510
1511 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001512 * Allocate stack pages for Hypervisor-mode
1513 */
1514 for_each_possible_cpu(cpu) {
1515 unsigned long stack_page;
1516
1517 stack_page = __get_free_page(GFP_KERNEL);
1518 if (!stack_page) {
1519 err = -ENOMEM;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001520 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001521 }
1522
1523 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1524 }
1525
1526 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001527 * Map the Hyp-code called directly from the host
1528 */
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001529 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001530 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001531 if (err) {
1532 kvm_err("Cannot map world-switch code\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001533 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001534 }
1535
Ard Biesheuvela0bf9772016-02-16 13:52:39 +01001536 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
Marc Zyngier74a6b882016-06-13 15:00:47 +01001537 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
Marc Zyngier910917b2015-10-27 12:18:48 +00001538 if (err) {
1539 kvm_err("Cannot map rodata section\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001540 goto out_err;
Marc Zyngier910917b2015-10-27 12:18:48 +00001541 }
1542
Marc Zyngierc8ea0392016-10-20 10:17:21 +01001543 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1544 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1545 if (err) {
1546 kvm_err("Cannot map bss section\n");
1547 goto out_err;
1548 }
1549
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001550 err = kvm_map_vectors();
1551 if (err) {
1552 kvm_err("Cannot map vectors\n");
1553 goto out_err;
1554 }
1555
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001556 /*
1557 * Map the Hyp stack pages
1558 */
1559 for_each_possible_cpu(cpu) {
1560 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001561 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1562 PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001563
1564 if (err) {
1565 kvm_err("Cannot map hyp stack\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001566 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001567 }
1568 }
1569
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001570 for_each_possible_cpu(cpu) {
Andrew Murray630a1682019-04-09 20:22:11 +01001571 kvm_host_data_t *cpu_data;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001572
Andrew Murray630a1682019-04-09 20:22:11 +01001573 cpu_data = per_cpu_ptr(&kvm_host_data, cpu);
Andrew Murray630a1682019-04-09 20:22:11 +01001574 err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001575
1576 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001577 kvm_err("Cannot map host CPU state: %d\n", err);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001578 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001579 }
1580 }
1581
Marc Zyngier55e37482018-05-29 13:11:16 +01001582 err = hyp_map_aux_data();
1583 if (err)
Colin Ian Kinga37f0c32019-02-17 22:36:26 +00001584 kvm_err("Cannot map host auxiliary data: %d\n", err);
Marc Zyngier55e37482018-05-29 13:11:16 +01001585
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001586 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001587
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001588out_err:
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001589 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001590 kvm_err("error initializing Hyp mode: %d\n", err);
1591 return err;
1592}
1593
Andre Przywarad4e071c2013-04-17 12:52:01 +02001594static void check_kvm_target_cpu(void *ret)
1595{
1596 *(int *)ret = kvm_target_cpu();
1597}
1598
Andre Przywara4429fc62014-06-02 15:37:13 +02001599struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1600{
1601 struct kvm_vcpu *vcpu;
1602 int i;
1603
1604 mpidr &= MPIDR_HWID_BITMASK;
1605 kvm_for_each_vcpu(i, vcpu, kvm) {
1606 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1607 return vcpu;
1608 }
1609 return NULL;
1610}
1611
Eric Auger24124052017-10-27 15:28:31 +01001612bool kvm_arch_has_irq_bypass(void)
1613{
1614 return true;
1615}
1616
1617int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
1618 struct irq_bypass_producer *prod)
1619{
1620 struct kvm_kernel_irqfd *irqfd =
1621 container_of(cons, struct kvm_kernel_irqfd, consumer);
1622
Marc Zyngier196b1362017-10-27 15:28:39 +01001623 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
1624 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001625}
1626void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
1627 struct irq_bypass_producer *prod)
1628{
1629 struct kvm_kernel_irqfd *irqfd =
1630 container_of(cons, struct kvm_kernel_irqfd, consumer);
1631
Marc Zyngier196b1362017-10-27 15:28:39 +01001632 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
1633 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001634}
1635
1636void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
1637{
1638 struct kvm_kernel_irqfd *irqfd =
1639 container_of(cons, struct kvm_kernel_irqfd, consumer);
1640
1641 kvm_arm_halt_guest(irqfd->kvm);
1642}
1643
1644void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
1645{
1646 struct kvm_kernel_irqfd *irqfd =
1647 container_of(cons, struct kvm_kernel_irqfd, consumer);
1648
1649 kvm_arm_resume_guest(irqfd->kvm);
1650}
1651
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001652/**
1653 * Initialize Hyp-mode and memory mappings on all CPUs.
1654 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001655int kvm_arch_init(void *opaque)
1656{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001657 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001658 int ret, cpu;
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001659 bool in_hyp_mode;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001660
1661 if (!is_hyp_mode_available()) {
Ard Biesheuvel58d0d192017-11-28 15:18:19 +00001662 kvm_info("HYP mode not available\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001663 return -ENODEV;
1664 }
1665
Marc Zyngier33e5f4e2018-12-06 17:31:20 +00001666 in_hyp_mode = is_kernel_in_hyp_mode();
1667
1668 if (!in_hyp_mode && kvm_arch_requires_vhe()) {
1669 kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n");
Dave Martin85acda32018-04-20 16:20:43 +01001670 return -ENODEV;
1671 }
1672
Andre Przywarad4e071c2013-04-17 12:52:01 +02001673 for_each_online_cpu(cpu) {
1674 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1675 if (ret < 0) {
1676 kvm_err("Error, CPU %d not supported!\n", cpu);
1677 return -ENODEV;
1678 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001679 }
1680
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001681 err = init_common_resources();
1682 if (err)
1683 return err;
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301684
Dave Martina3be8362019-04-12 15:30:58 +01001685 err = kvm_arm_init_sve();
Dave Martin0f062bf2019-02-28 18:33:00 +00001686 if (err)
1687 return err;
1688
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001689 if (!in_hyp_mode) {
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001690 err = init_hyp_mode();
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001691 if (err)
1692 goto out_err;
1693 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001694
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001695 err = init_subsystems();
1696 if (err)
1697 goto out_hyp;
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001698
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001699 if (in_hyp_mode)
1700 kvm_info("VHE mode initialized successfully\n");
1701 else
1702 kvm_info("Hyp mode initialized successfully\n");
1703
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001704 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001705
1706out_hyp:
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001707 if (!in_hyp_mode)
1708 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001709out_err:
1710 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001711}
1712
1713/* NOP: Compiling as a module not supported */
1714void kvm_arch_exit(void)
1715{
Marc Zyngier210552c2013-03-05 03:18:00 +00001716 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001717}
1718
1719static int arm_init(void)
1720{
1721 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1722 return rc;
1723}
1724
1725module_init(arm_init);