blob: 4d864f857ac8f39657c72ef5a84dc46d516baaef [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>
23
24#define CREATE_TRACE_POINTS
25#include "trace.h"
26
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050028#include <asm/ptrace.h>
29#include <asm/mman.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050030#include <asm/tlbflush.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050031#include <asm/cacheflush.h>
Dave Martin85acda32018-04-20 16:20:43 +010032#include <asm/cpufeature.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050033#include <asm/virt.h>
34#include <asm/kvm_arm.h>
35#include <asm/kvm_asm.h>
36#include <asm/kvm_mmu.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050037#include <asm/kvm_emulate.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050038#include <asm/kvm_coproc.h>
Marc Zyngier910917b2015-10-27 12:18:48 +000039#include <asm/sections.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050040
Steven Price8564d632019-10-21 16:28:18 +010041#include <kvm/arm_hypercalls.h>
42#include <kvm/arm_pmu.h>
43#include <kvm/arm_psci.h>
44
Christoffer Dall749cf76c2013-01-20 18:28:06 -050045#ifdef REQUIRES_VIRT
46__asm__(".arch_extension virt");
47#endif
48
Andrew Murray630a1682019-04-09 20:22:11 +010049DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050050static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050051
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050052/* The VMID used in the VTTBR */
53static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
Vladimir Murzin20475f72015-11-16 11:28:18 +000054static u32 kvm_next_vmid;
Christoffer Dallfb544d12018-12-11 13:23:57 +010055static DEFINE_SPINLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050056
Pavel Fedinc7da6fa2015-12-18 14:38:43 +030057static bool vgic_present;
58
AKASHI Takahiro67f69192016-04-27 17:47:05 +010059static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
Christoffer Dall61bbe382017-10-27 19:57:51 +020060DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
61
Christoffer Dall749cf76c2013-01-20 18:28:06 -050062int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
63{
64 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
65}
66
Christoffer Dall749cf76c2013-01-20 18:28:06 -050067int kvm_arch_hardware_setup(void)
68{
69 return 0;
70}
71
Sean Christophersonf257d6d2019-04-19 22:18:17 -070072int kvm_arch_check_processor_compat(void)
Christoffer Dall749cf76c2013-01-20 18:28:06 -050073{
Sean Christophersonf257d6d2019-04-19 22:18:17 -070074 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -050075}
76
Christoffer Dallc7262002019-10-11 13:07:05 +020077int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
78 struct kvm_enable_cap *cap)
79{
80 int r;
81
82 if (cap->flags)
83 return -EINVAL;
84
85 switch (cap->cap) {
86 case KVM_CAP_ARM_NISV_TO_USER:
87 r = 0;
88 kvm->arch.return_nisv_io_abort_to_user = true;
89 break;
90 default:
91 r = -EINVAL;
92 break;
93 }
94
95 return r;
96}
Christoffer Dall749cf76c2013-01-20 18:28:06 -050097
Christoffer Dalld5d81842013-01-20 18:28:07 -050098/**
99 * kvm_arch_init_vm - initializes a VM data structure
100 * @kvm: pointer to the KVM struct
101 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500102int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
103{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100104 int ret, cpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500105
Marc Zyngierbca607e2018-10-01 13:40:36 +0100106 ret = kvm_arm_setup_stage2(kvm, type);
Suzuki K Poulose5b6c6742018-09-26 17:32:42 +0100107 if (ret)
108 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500109
Marc Zyngier94d0e592016-10-18 18:37:49 +0100110 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
111 if (!kvm->arch.last_vcpu_ran)
112 return -ENOMEM;
113
114 for_each_possible_cpu(cpu)
115 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
116
Christoffer Dalld5d81842013-01-20 18:28:07 -0500117 ret = kvm_alloc_stage2_pgd(kvm);
118 if (ret)
119 goto out_fail_alloc;
120
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100121 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500122 if (ret)
123 goto out_free_stage2_pgd;
124
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100125 kvm_vgic_early_init(kvm);
Christoffer Dalla1a64382013-11-16 10:51:25 -0800126
Christoffer Dalld5d81842013-01-20 18:28:07 -0500127 /* Mark the initial VMID generation invalid */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100128 kvm->arch.vmid.vmid_gen = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500129
Andre Przywara3caa2d82014-06-02 16:26:01 +0200130 /* The maximum number of VCPUs is limited by the host's GIC model */
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300131 kvm->arch.max_vcpus = vgic_present ?
132 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
Andre Przywara3caa2d82014-06-02 16:26:01 +0200133
Christoffer Dalld5d81842013-01-20 18:28:07 -0500134 return ret;
135out_free_stage2_pgd:
136 kvm_free_stage2_pgd(kvm);
137out_fail_alloc:
Marc Zyngier94d0e592016-10-18 18:37:49 +0100138 free_percpu(kvm->arch.last_vcpu_ran);
139 kvm->arch.last_vcpu_ran = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500140 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500141}
142
Luiz Capitulino235539b2016-09-07 14:47:23 -0400143int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
144{
145 return 0;
146}
147
Souptick Joarder1499fa82018-04-19 00:49:58 +0530148vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500149{
150 return VM_FAULT_SIGBUS;
151}
152
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500153
Christoffer Dalld5d81842013-01-20 18:28:07 -0500154/**
155 * kvm_arch_destroy_vm - destroy the VM data structure
156 * @kvm: pointer to the KVM struct
157 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500158void kvm_arch_destroy_vm(struct kvm *kvm)
159{
160 int i;
161
Marc Zyngierb2c9a852017-10-27 15:28:34 +0100162 kvm_vgic_destroy(kvm);
163
Marc Zyngier94d0e592016-10-18 18:37:49 +0100164 free_percpu(kvm->arch.last_vcpu_ran);
165 kvm->arch.last_vcpu_ran = NULL;
166
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500167 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
168 if (kvm->vcpus[i]) {
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800169 kvm_vcpu_destroy(kvm->vcpus[i]);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500170 kvm->vcpus[i] = NULL;
171 }
172 }
Andrew Jones6b2ad812017-11-27 19:17:18 +0100173 atomic_set(&kvm->online_vcpus, 0);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500174}
175
Alexander Graf784aa3d2014-07-14 18:27:35 +0200176int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500177{
178 int r;
179 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500180 case KVM_CAP_IRQCHIP:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300181 r = vgic_present;
182 break;
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000183 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100184 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500185 case KVM_CAP_USER_MEMORY:
186 case KVM_CAP_SYNC_MMU:
187 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
188 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500189 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530190 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200191 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000192 case KVM_CAP_MP_STATE:
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100193 case KVM_CAP_IMMEDIATE_EXIT:
Dongjiu Geng58bf4372018-10-13 00:12:49 +0800194 case KVM_CAP_VCPU_EVENTS:
Marc Zyngier92f35b72019-08-18 14:09:47 +0100195 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
Christoffer Dallc7262002019-10-11 13:07:05 +0200196 case KVM_CAP_ARM_NISV_TO_USER:
Christoffer Dallda345172019-10-11 13:07:06 +0200197 case KVM_CAP_ARM_INJECT_EXT_DABT:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500198 r = 1;
199 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500200 case KVM_CAP_ARM_SET_DEVICE_ADDR:
201 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100202 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500203 case KVM_CAP_NR_VCPUS:
204 r = num_online_cpus();
205 break;
206 case KVM_CAP_MAX_VCPUS:
207 r = KVM_MAX_VCPUS;
208 break;
Thomas Hutha86cb412019-05-23 18:43:08 +0200209 case KVM_CAP_MAX_VCPU_ID:
210 r = KVM_MAX_VCPU_ID;
211 break;
Vladimir Murzin29885092016-11-02 11:55:34 +0000212 case KVM_CAP_MSI_DEVID:
213 if (!kvm)
214 r = -EINVAL;
215 else
216 r = kvm->arch.vgic.msis_require_devid;
217 break;
Christoffer Dallf7214e62017-02-01 12:54:11 +0100218 case KVM_CAP_ARM_USER_IRQ:
219 /*
220 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
221 * (bump this number if adding more devices)
222 */
223 r = 1;
224 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500225 default:
Dongjiu Geng375bdd32018-10-13 00:12:48 +0800226 r = kvm_arch_vm_ioctl_check_extension(kvm, ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500227 break;
228 }
229 return r;
230}
231
232long kvm_arch_dev_ioctl(struct file *filp,
233 unsigned int ioctl, unsigned long arg)
234{
235 return -EINVAL;
236}
237
Marc Orrd1e5b0e2018-05-15 04:37:37 -0700238struct kvm *kvm_arch_alloc_vm(void)
239{
240 if (!has_vhe())
241 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
242
243 return vzalloc(sizeof(struct kvm));
244}
245
246void kvm_arch_free_vm(struct kvm *kvm)
247{
248 if (!has_vhe())
249 kfree(kvm);
250 else
251 vfree(kvm);
252}
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500253
Sean Christopherson897cc382019-12-18 13:55:09 -0800254int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
255{
256 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
257 return -EBUSY;
258
259 if (id >= kvm->arch.max_vcpus)
260 return -EINVAL;
261
262 return 0;
263}
264
Sean Christophersone529ef62019-12-18 13:55:15 -0800265int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500266{
Sean Christopherson39a93a82019-12-18 13:55:25 -0800267 int err;
268
269 /* Force users to call KVM_ARM_VCPU_INIT */
270 vcpu->arch.target = -1;
271 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
272
273 /* Set up the timer */
274 kvm_timer_vcpu_init(vcpu);
275
276 kvm_pmu_vcpu_init(vcpu);
277
278 kvm_arm_reset_debug_ptr(vcpu);
279
280 kvm_arm_pvtime_vcpu_init(&vcpu->arch);
281
282 err = kvm_vgic_vcpu_init(vcpu);
283 if (err)
284 return err;
285
Sean Christophersone529ef62019-12-18 13:55:15 -0800286 return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500287}
288
Dominik Dingel31928aa2014-12-04 15:47:07 +0100289void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500290{
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500291}
292
Sean Christopherson4b8fff72019-12-18 13:55:04 -0800293void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500294{
Christoffer Dallf1d72312018-01-25 18:32:29 +0100295 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
296 static_branch_dec(&userspace_irqchip_in_use);
297
Christoffer Dalld5d81842013-01-20 18:28:07 -0500298 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500299 kvm_timer_vcpu_terminate(vcpu);
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800300 kvm_pmu_vcpu_destroy(vcpu);
Sean Christopherson19bcc892019-12-18 13:55:27 -0800301
302 kvm_arm_vcpu_destroy(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500303}
304
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500305int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
306{
Christoffer Dall1c88ab72017-01-06 16:07:48 +0100307 return kvm_timer_is_pending(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500308}
309
Christoffer Dalld35268d2015-08-25 19:48:21 +0200310void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
311{
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100312 /*
313 * If we're about to block (most likely because we've just hit a
314 * WFI), we need to sync back the state of the GIC CPU interface
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000315 * so that we have the latest PMR and group enables. This ensures
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100316 * that kvm_arch_vcpu_runnable has up-to-date data to decide
317 * whether we have pending interrupts.
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000318 *
319 * For the same reason, we want to tell GICv4 that we need
320 * doorbells to be signalled, should an interrupt become pending.
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100321 */
322 preempt_disable();
323 kvm_vgic_vmcr_sync(vcpu);
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000324 vgic_v4_put(vcpu, true);
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100325 preempt_enable();
Christoffer Dalld35268d2015-08-25 19:48:21 +0200326}
327
328void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
329{
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000330 preempt_disable();
331 vgic_v4_load(vcpu);
332 preempt_enable();
Christoffer Dalld35268d2015-08-25 19:48:21 +0200333}
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;
Andrew Murray630a1682019-04-09 20:22:11 +0100338 kvm_host_data_t *cpu_data;
Marc Zyngier94d0e592016-10-18 18:37:49 +0100339
340 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
Andrew Murray630a1682019-04-09 20:22:11 +0100341 cpu_data = this_cpu_ptr(&kvm_host_data);
Marc Zyngier94d0e592016-10-18 18:37:49 +0100342
343 /*
344 * We might get preempted before the vCPU actually runs, but
345 * over-invalidation doesn't affect correctness.
346 */
347 if (*last_ran != vcpu->vcpu_id) {
348 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
349 *last_ran = vcpu->vcpu_id;
350 }
351
Christoffer Dall86ce8532013-01-20 18:28:08 -0500352 vcpu->cpu = cpu;
Andrew Murray630a1682019-04-09 20:22:11 +0100353 vcpu->arch.host_cpu_context = &cpu_data->host_ctxt;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500354
Christoffer Dall328e5662016-03-24 11:21:04 +0100355 kvm_vgic_load(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200356 kvm_timer_vcpu_load(vcpu);
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200357 kvm_vcpu_load_sysregs(vcpu);
Dave Martine6b673b2018-04-06 14:55:59 +0100358 kvm_arch_vcpu_load_fp(vcpu);
Andrew Murray435e53f2019-04-09 20:22:15 +0100359 kvm_vcpu_pmu_restore_guest(vcpu);
Steven Price8564d632019-10-21 16:28:18 +0100360 if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
361 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
Marc Zyngierde737082018-06-21 10:43:59 +0100362
363 if (single_task_running())
Marc Zyngieref2e78d2019-11-07 16:04:12 +0000364 vcpu_clear_wfx_traps(vcpu);
Marc Zyngierde737082018-06-21 10:43:59 +0100365 else
Marc Zyngieref2e78d2019-11-07 16:04:12 +0000366 vcpu_set_wfx_traps(vcpu);
Mark Rutland384b40c2019-04-23 10:12:35 +0530367
368 vcpu_ptrauth_setup_lazy(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500369}
370
371void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
372{
Dave Martine6b673b2018-04-06 14:55:59 +0100373 kvm_arch_vcpu_put_fp(vcpu);
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200374 kvm_vcpu_put_sysregs(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200375 kvm_timer_vcpu_put(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100376 kvm_vgic_put(vcpu);
Andrew Murray435e53f2019-04-09 20:22:15 +0100377 kvm_vcpu_pmu_restore_host(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100378
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800379 vcpu->cpu = -1;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500380}
381
Andrew Jones424c9892017-06-04 14:43:57 +0200382static void vcpu_power_off(struct kvm_vcpu *vcpu)
383{
384 vcpu->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200385 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones424c9892017-06-04 14:43:57 +0200386 kvm_vcpu_kick(vcpu);
387}
388
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500389int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
390 struct kvm_mp_state *mp_state)
391{
Eric Auger37815282015-09-25 23:41:14 +0200392 if (vcpu->arch.power_off)
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000393 mp_state->mp_state = KVM_MP_STATE_STOPPED;
394 else
395 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
396
397 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500398}
399
400int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
401 struct kvm_mp_state *mp_state)
402{
Christoffer Dalle83dff52017-12-04 21:35:31 +0100403 int ret = 0;
404
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000405 switch (mp_state->mp_state) {
406 case KVM_MP_STATE_RUNNABLE:
Eric Auger37815282015-09-25 23:41:14 +0200407 vcpu->arch.power_off = false;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000408 break;
409 case KVM_MP_STATE_STOPPED:
Andrew Jones424c9892017-06-04 14:43:57 +0200410 vcpu_power_off(vcpu);
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000411 break;
412 default:
Christoffer Dalle83dff52017-12-04 21:35:31 +0100413 ret = -EINVAL;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000414 }
415
Christoffer Dalle83dff52017-12-04 21:35:31 +0100416 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500417}
418
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500419/**
420 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
421 * @v: The VCPU pointer
422 *
423 * If the guest CPU is not waiting for interrupts or an interrupt line is
424 * asserted, the CPU is by definition runnable.
425 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500426int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
427{
Christoffer Dall3df59d82017-08-03 12:09:05 +0200428 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
429 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
Eric Auger3b928302015-09-25 23:41:17 +0200430 && !v->arch.power_off && !v->arch.pause);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500431}
432
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800433bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
434{
Longpeng(Mike)f01fbd22017-08-08 12:05:35 +0800435 return vcpu_mode_priv(vcpu);
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800436}
437
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500438/* Just ensure a guest exit from a particular CPU */
439static void exit_vm_noop(void *info)
440{
441}
442
443void force_vm_exit(const cpumask_t *mask)
444{
Eric Auger898f9492016-03-07 23:50:36 +0700445 preempt_disable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500446 smp_call_function_many(mask, exit_vm_noop, NULL, true);
Eric Auger898f9492016-03-07 23:50:36 +0700447 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500448}
449
450/**
451 * need_new_vmid_gen - check that the VMID is still valid
Christoffer Dalle329fb72018-12-11 15:26:31 +0100452 * @vmid: The VMID to check
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500453 *
454 * return true if there is a new generation of VMIDs being used
455 *
Christoffer Dalle329fb72018-12-11 15:26:31 +0100456 * The hardware supports a limited set of values with the value zero reserved
457 * for the host, so we check if an assigned value belongs to a previous
458 * generation, which which requires us to assign a new value. If we're the
459 * first to use a VMID for the new generation, we must flush necessary caches
460 * and TLBs on all CPUs.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500461 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100462static bool need_new_vmid_gen(struct kvm_vmid *vmid)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500463{
Christoffer Dallfb544d12018-12-11 13:23:57 +0100464 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
465 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100466 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500467}
468
469/**
Christoffer Dalle329fb72018-12-11 15:26:31 +0100470 * update_vmid - Update the vmid with a valid VMID for the current generation
471 * @kvm: The guest that struct vmid belongs to
472 * @vmid: The stage-2 VMID information struct
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500473 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100474static void update_vmid(struct kvm_vmid *vmid)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500475{
Christoffer Dalle329fb72018-12-11 15:26:31 +0100476 if (!need_new_vmid_gen(vmid))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500477 return;
478
Christoffer Dallfb544d12018-12-11 13:23:57 +0100479 spin_lock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500480
481 /*
482 * We need to re-check the vmid_gen here to ensure that if another vcpu
483 * already allocated a valid vmid for this vm, then this vcpu should
484 * use the same vmid.
485 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100486 if (!need_new_vmid_gen(vmid)) {
Christoffer Dallfb544d12018-12-11 13:23:57 +0100487 spin_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500488 return;
489 }
490
491 /* First user of a new VMID generation? */
492 if (unlikely(kvm_next_vmid == 0)) {
493 atomic64_inc(&kvm_vmid_gen);
494 kvm_next_vmid = 1;
495
496 /*
497 * On SMP we know no other CPUs can use this CPU's or each
498 * other's VMID after force_vm_exit returns since the
499 * kvm_vmid_lock blocks them from reentry to the guest.
500 */
501 force_vm_exit(cpu_all_mask);
502 /*
503 * Now broadcast TLB + ICACHE invalidation over the inner
504 * shareable domain to make sure all data structures are
505 * clean.
506 */
507 kvm_call_hyp(__kvm_flush_vm_context);
508 }
509
Christoffer Dalle329fb72018-12-11 15:26:31 +0100510 vmid->vmid = kvm_next_vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500511 kvm_next_vmid++;
Christoffer Dalle329fb72018-12-11 15:26:31 +0100512 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500513
Christoffer Dallfb544d12018-12-11 13:23:57 +0100514 smp_wmb();
Christoffer Dalle329fb72018-12-11 15:26:31 +0100515 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
Christoffer Dallfb544d12018-12-11 13:23:57 +0100516
517 spin_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500518}
519
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500520static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
521{
Christoffer Dall05971122014-12-12 21:19:23 +0100522 struct kvm *kvm = vcpu->kvm;
Christoffer Dall41a54482016-05-18 16:26:00 +0100523 int ret = 0;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700524
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500525 if (likely(vcpu->arch.has_run_once))
526 return 0;
527
Dave Martin7dd32a02018-12-19 14:27:01 +0000528 if (!kvm_arm_vcpu_is_finalized(vcpu))
529 return -EPERM;
530
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500531 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500532
Christoffer Dall61bbe382017-10-27 19:57:51 +0200533 if (likely(irqchip_in_kernel(kvm))) {
534 /*
535 * Map the VGIC hardware resources before running a vcpu the
536 * first time on this VM.
537 */
538 if (unlikely(!vgic_ready(kvm))) {
539 ret = kvm_vgic_map_resources(kvm);
540 if (ret)
541 return ret;
542 }
543 } else {
544 /*
545 * Tell the rest of the code that there are userspace irqchip
546 * VMs in the wild.
547 */
548 static_branch_inc(&userspace_irqchip_in_use);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500549 }
550
Alexander Grafd9e13972016-09-27 21:08:06 +0200551 ret = kvm_timer_enable(vcpu);
Christoffer Dalla2befac2017-05-02 13:41:02 +0200552 if (ret)
553 return ret;
554
555 ret = kvm_arm_pmu_v3_enable(vcpu);
Christoffer Dall05971122014-12-12 21:19:23 +0100556
Christoffer Dall41a54482016-05-18 16:26:00 +0100557 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500558}
559
Eric Augerc1426e42015-03-04 11:14:34 +0100560bool kvm_arch_intc_initialized(struct kvm *kvm)
561{
562 return vgic_initialized(kvm);
563}
564
Christoffer Dallb13216c2016-04-27 10:28:00 +0100565void kvm_arm_halt_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200566{
567 int i;
568 struct kvm_vcpu *vcpu;
569
570 kvm_for_each_vcpu(i, vcpu, kvm)
571 vcpu->arch.pause = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200572 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
Eric Auger3b928302015-09-25 23:41:17 +0200573}
574
Christoffer Dallb13216c2016-04-27 10:28:00 +0100575void kvm_arm_resume_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200576{
577 int i;
578 struct kvm_vcpu *vcpu;
579
Christoffer Dallabd72292017-05-06 20:01:24 +0200580 kvm_for_each_vcpu(i, vcpu, kvm) {
581 vcpu->arch.pause = false;
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200582 swake_up_one(kvm_arch_vcpu_wq(vcpu));
Christoffer Dallabd72292017-05-06 20:01:24 +0200583 }
Eric Auger3b928302015-09-25 23:41:17 +0200584}
585
Andrew Jones7b244e22017-06-04 14:43:58 +0200586static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500587{
Marcelo Tosatti85773702016-02-19 09:46:39 +0100588 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500589
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200590 swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) &&
Eric Auger3b928302015-09-25 23:41:17 +0200591 (!vcpu->arch.pause)));
Andrew Jones0592c002017-06-04 14:43:55 +0200592
Andrew Jones424c9892017-06-04 14:43:57 +0200593 if (vcpu->arch.power_off || vcpu->arch.pause) {
Andrew Jones0592c002017-06-04 14:43:55 +0200594 /* Awaken to handle a signal, request we sleep again later. */
Andrew Jones7b244e22017-06-04 14:43:58 +0200595 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones0592c002017-06-04 14:43:55 +0200596 }
Marc Zyngier358b28f2018-12-20 11:36:07 +0000597
598 /*
599 * Make sure we will observe a potential reset request if we've
600 * observed a change to the power state. Pairs with the smp_wmb() in
601 * kvm_psci_vcpu_on().
602 */
603 smp_rmb();
Marc Zyngieraa024c22013-01-20 18:28:13 -0500604}
605
Andre Przywarae8180dc2013-05-09 00:28:06 +0200606static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
607{
608 return vcpu->arch.target >= 0;
609}
610
Andrew Jones0592c002017-06-04 14:43:55 +0200611static void check_vcpu_requests(struct kvm_vcpu *vcpu)
612{
613 if (kvm_request_pending(vcpu)) {
Andrew Jones7b244e22017-06-04 14:43:58 +0200614 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
615 vcpu_req_sleep(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200616
Marc Zyngier358b28f2018-12-20 11:36:07 +0000617 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
618 kvm_reset_vcpu(vcpu);
619
Andrew Jones325f9c62017-06-04 14:43:59 +0200620 /*
621 * Clear IRQ_PENDING requests that were made to guarantee
622 * that a VCPU sees new virtual interrupts.
623 */
624 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
Steven Price8564d632019-10-21 16:28:18 +0100625
626 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
627 kvm_update_stolen_time(vcpu);
Marc Zyngierd9c38722020-03-04 20:33:28 +0000628
629 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
630 /* The distributor enable bits were changed */
631 preempt_disable();
632 vgic_v4_put(vcpu, false);
633 vgic_v4_load(vcpu);
634 preempt_enable();
635 }
Andrew Jones0592c002017-06-04 14:43:55 +0200636 }
637}
638
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500639/**
640 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
641 * @vcpu: The VCPU pointer
642 * @run: The kvm_run structure pointer used for userspace state exchange
643 *
644 * This function is called through the VCPU_RUN ioctl called from user space. It
645 * will execute VM code in a loop until the time slice for the process is used
646 * or some emulation is needed from user space in which case the function will
647 * return with return value 0 and with the kvm_run structure filled in with the
648 * required data for the requested emulation.
649 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500650int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
651{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500652 int ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500653
Andre Przywarae8180dc2013-05-09 00:28:06 +0200654 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500655 return -ENOEXEC;
656
657 ret = kvm_vcpu_first_run_init(vcpu);
658 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100659 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500660
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500661 if (run->exit_reason == KVM_EXIT_MMIO) {
662 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
663 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100664 return ret;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500665 }
666
Christoffer Dall829a5862017-11-29 16:37:53 +0100667 if (run->immediate_exit)
668 return -EINTR;
669
670 vcpu_load(vcpu);
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100671
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100672 kvm_sigset_activate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500673
674 ret = 1;
675 run->exit_reason = KVM_EXIT_UNKNOWN;
676 while (ret > 0) {
677 /*
678 * Check conditions before entering the guest
679 */
680 cond_resched();
681
Christoffer Dalle329fb72018-12-11 15:26:31 +0100682 update_vmid(&vcpu->kvm->arch.vmid);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500683
Andrew Jones0592c002017-06-04 14:43:55 +0200684 check_vcpu_requests(vcpu);
685
Marc Zyngierabdf5842015-06-08 15:00:28 +0100686 /*
Marc Zyngierabdf5842015-06-08 15:00:28 +0100687 * Preparing the interrupts to be injected also
688 * involves poking the GIC, which must be done in a
689 * non-preemptible context.
690 */
691 preempt_disable();
Christoffer Dall328e5662016-03-24 11:21:04 +0100692
Shannon Zhaob02386e2016-02-26 19:29:19 +0800693 kvm_pmu_flush_hwstate(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100694
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500695 local_irq_disable();
696
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500697 kvm_vgic_flush_hwstate(vcpu);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500698
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500699 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200700 * Exit if we have a signal pending so that we can deliver the
701 * signal to user space.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500702 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200703 if (signal_pending(current)) {
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500704 ret = -EINTR;
705 run->exit_reason = KVM_EXIT_INTR;
706 }
707
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200708 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200709 * If we're using a userspace irqchip, then check if we need
710 * to tell a userspace irqchip about timer or PMU level
711 * changes and if so, exit to userspace (the actual level
712 * state gets updated in kvm_timer_update_run and
713 * kvm_pmu_update_run below).
714 */
715 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
716 if (kvm_timer_should_notify_user(vcpu) ||
717 kvm_pmu_should_notify_user(vcpu)) {
718 ret = -EINTR;
719 run->exit_reason = KVM_EXIT_INTR;
720 }
721 }
722
723 /*
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200724 * Ensure we set mode to IN_GUEST_MODE after we disable
725 * interrupts and before the final VCPU requests check.
726 * See the comment in kvm_vcpu_exiting_guest_mode() and
Christoph Hellwig2f5947d2019-07-24 09:24:49 +0200727 * Documentation/virt/kvm/vcpu-requests.rst
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200728 */
729 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
730
Christoffer Dalle329fb72018-12-11 15:26:31 +0100731 if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) ||
Andrew Jones424c9892017-06-04 14:43:57 +0200732 kvm_request_pending(vcpu)) {
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200733 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall771621b2017-10-04 23:42:32 +0200734 isb(); /* Ensure work in x_flush_hwstate is committed */
Shannon Zhaob02386e2016-02-26 19:29:19 +0800735 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dall61bbe382017-10-27 19:57:51 +0200736 if (static_branch_unlikely(&userspace_irqchip_in_use))
737 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500738 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200739 local_irq_enable();
Marc Zyngierabdf5842015-06-08 15:00:28 +0100740 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500741 continue;
742 }
743
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100744 kvm_arm_setup_debug(vcpu);
745
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500746 /**************************************************************
747 * Enter the guest
748 */
749 trace_kvm_entry(*vcpu_pc(vcpu));
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200750 guest_enter_irqoff();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200751
752 if (has_vhe()) {
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200753 ret = kvm_vcpu_run_vhe(vcpu);
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200754 } else {
Marc Zyngier7aa8d142019-01-05 15:49:50 +0000755 ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu);
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200756 }
757
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500758 vcpu->mode = OUTSIDE_GUEST_MODE;
Amit Tomarb19e6892015-11-26 10:09:43 +0000759 vcpu->stat.exits++;
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100760 /*
761 * Back from guest
762 *************************************************************/
763
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100764 kvm_arm_clear_debug(vcpu);
765
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500766 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200767 * We must sync the PMU state before the vgic state so
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200768 * that the vgic can properly sample the updated state of the
769 * interrupt line.
770 */
771 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200772
Christoffer Dallb103cc32016-10-16 20:30:38 +0200773 /*
774 * Sync the vgic state before syncing the timer state because
775 * the timer code needs to know if the virtual timer
776 * interrupts are active.
777 */
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200778 kvm_vgic_sync_hwstate(vcpu);
779
780 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200781 * Sync the timer hardware state before enabling interrupts as
782 * we don't want vtimer interrupts to race with syncing the
783 * timer virtual interrupt state.
784 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200785 if (static_branch_unlikely(&userspace_irqchip_in_use))
786 kvm_timer_sync_hwstate(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200787
Dave Martine6b673b2018-04-06 14:55:59 +0100788 kvm_arch_vcpu_ctxsync_fp(vcpu);
789
Christoffer Dallb103cc32016-10-16 20:30:38 +0200790 /*
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500791 * We may have taken a host interrupt in HYP mode (ie
792 * while executing the guest). This interrupt is still
793 * pending, as we haven't serviced it yet!
794 *
795 * We're now back in SVC mode, with interrupts
796 * disabled. Enabling the interrupts now will have
797 * the effect of taking the interrupt again, in SVC
798 * mode this time.
799 */
800 local_irq_enable();
801
802 /*
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200803 * We do local_irq_enable() before calling guest_exit() so
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100804 * that if a timer interrupt hits while running the guest we
805 * account that tick as being spent in the guest. We enable
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200806 * preemption after calling guest_exit() so that if we get
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100807 * preempted we make sure ticks after that is not counted as
808 * guest time.
809 */
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200810 guest_exit();
Christoffer Dallb5905dc2015-08-30 15:55:22 +0200811 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100812
James Morse3368bd82018-01-15 19:39:04 +0000813 /* Exit types that need handling before we can be preempted */
814 handle_exit_early(vcpu, run, ret);
815
Marc Zyngierabdf5842015-06-08 15:00:28 +0100816 preempt_enable();
817
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500818 ret = handle_exit(vcpu, run, ret);
819 }
820
Alexander Grafd9e13972016-09-27 21:08:06 +0200821 /* Tell userspace about in-kernel device output levels */
Christoffer Dall3dbbdf72017-02-01 12:51:52 +0100822 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
823 kvm_timer_update_run(vcpu);
824 kvm_pmu_update_run(vcpu);
825 }
Alexander Grafd9e13972016-09-27 21:08:06 +0200826
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100827 kvm_sigset_deactivate(vcpu);
828
Christoffer Dallaccb7572017-12-04 21:35:25 +0100829 vcpu_put(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500830 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500831}
832
Christoffer Dall86ce8532013-01-20 18:28:08 -0500833static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
834{
835 int bit_index;
836 bool set;
Christoffer Dall3df59d82017-08-03 12:09:05 +0200837 unsigned long *hcr;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500838
839 if (number == KVM_ARM_IRQ_CPU_IRQ)
840 bit_index = __ffs(HCR_VI);
841 else /* KVM_ARM_IRQ_CPU_FIQ */
842 bit_index = __ffs(HCR_VF);
843
Christoffer Dall3df59d82017-08-03 12:09:05 +0200844 hcr = vcpu_hcr(vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500845 if (level)
Christoffer Dall3df59d82017-08-03 12:09:05 +0200846 set = test_and_set_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500847 else
Christoffer Dall3df59d82017-08-03 12:09:05 +0200848 set = test_and_clear_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500849
850 /*
851 * If we didn't change anything, no need to wake up or kick other CPUs
852 */
853 if (set == level)
854 return 0;
855
856 /*
857 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
858 * trigger a world-switch round on the running physical CPU to set the
859 * virtual IRQ/FIQ fields in the HCR appropriately.
860 */
Andrew Jones325f9c62017-06-04 14:43:59 +0200861 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500862 kvm_vcpu_kick(vcpu);
863
864 return 0;
865}
866
Alexander Graf79558f12013-04-16 19:21:41 +0200867int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
868 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500869{
870 u32 irq = irq_level->irq;
871 unsigned int irq_type, vcpu_idx, irq_num;
872 int nrcpus = atomic_read(&kvm->online_vcpus);
873 struct kvm_vcpu *vcpu = NULL;
874 bool level = irq_level->level;
875
876 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
877 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
Marc Zyngier92f35b72019-08-18 14:09:47 +0100878 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500879 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
880
881 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
882
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500883 switch (irq_type) {
884 case KVM_ARM_IRQ_TYPE_CPU:
885 if (irqchip_in_kernel(kvm))
886 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500887
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500888 if (vcpu_idx >= nrcpus)
889 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500890
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500891 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
892 if (!vcpu)
893 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500894
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500895 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
896 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500897
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500898 return vcpu_interrupt_line(vcpu, irq_num, level);
899 case KVM_ARM_IRQ_TYPE_PPI:
900 if (!irqchip_in_kernel(kvm))
901 return -ENXIO;
902
903 if (vcpu_idx >= nrcpus)
904 return -EINVAL;
905
906 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
907 if (!vcpu)
908 return -EINVAL;
909
910 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
911 return -EINVAL;
912
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200913 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500914 case KVM_ARM_IRQ_TYPE_SPI:
915 if (!irqchip_in_kernel(kvm))
916 return -ENXIO;
917
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100918 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500919 return -EINVAL;
920
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200921 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500922 }
923
924 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500925}
926
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200927static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
928 const struct kvm_vcpu_init *init)
929{
Andrew Jones811328f2019-04-04 19:42:30 +0200930 unsigned int i, ret;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200931 int phys_target = kvm_target_cpu();
932
933 if (init->target != phys_target)
934 return -EINVAL;
935
936 /*
937 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
938 * use the same target.
939 */
940 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
941 return -EINVAL;
942
943 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
944 for (i = 0; i < sizeof(init->features) * 8; i++) {
945 bool set = (init->features[i / 32] & (1 << (i % 32)));
946
947 if (set && i >= KVM_VCPU_MAX_FEATURES)
948 return -ENOENT;
949
950 /*
951 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
952 * use the same feature set.
953 */
954 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
955 test_bit(i, vcpu->arch.features) != set)
956 return -EINVAL;
957
958 if (set)
959 set_bit(i, vcpu->arch.features);
960 }
961
962 vcpu->arch.target = phys_target;
963
964 /* Now we know what it is, we can reset it. */
Andrew Jones811328f2019-04-04 19:42:30 +0200965 ret = kvm_reset_vcpu(vcpu);
966 if (ret) {
967 vcpu->arch.target = -1;
968 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
969 }
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200970
Andrew Jones811328f2019-04-04 19:42:30 +0200971 return ret;
972}
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200973
Christoffer Dall478a8232013-11-19 17:43:19 -0800974static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
975 struct kvm_vcpu_init *init)
976{
977 int ret;
978
979 ret = kvm_vcpu_set_target(vcpu, init);
980 if (ret)
981 return ret;
982
Christoffer Dall957db102014-11-27 10:35:03 +0100983 /*
984 * Ensure a rebooted VM will fault in RAM pages and detect if the
985 * guest MMU is turned off and flush the caches as needed.
986 */
987 if (vcpu->arch.has_run_once)
988 stage2_unmap_vm(vcpu->kvm);
989
Christoffer Dallb856a592014-10-16 17:21:16 +0200990 vcpu_reset_hcr(vcpu);
991
Christoffer Dall478a8232013-11-19 17:43:19 -0800992 /*
Eric Auger37815282015-09-25 23:41:14 +0200993 * Handle the "start in power-off" case.
Christoffer Dall478a8232013-11-19 17:43:19 -0800994 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100995 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Andrew Jones424c9892017-06-04 14:43:57 +0200996 vcpu_power_off(vcpu);
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +0200997 else
Eric Auger37815282015-09-25 23:41:14 +0200998 vcpu->arch.power_off = false;
Christoffer Dall478a8232013-11-19 17:43:19 -0800999
1000 return 0;
1001}
1002
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001003static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1004 struct kvm_device_attr *attr)
1005{
1006 int ret = -ENXIO;
1007
1008 switch (attr->group) {
1009 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001010 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001011 break;
1012 }
1013
1014 return ret;
1015}
1016
1017static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1018 struct kvm_device_attr *attr)
1019{
1020 int ret = -ENXIO;
1021
1022 switch (attr->group) {
1023 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001024 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001025 break;
1026 }
1027
1028 return ret;
1029}
1030
1031static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1032 struct kvm_device_attr *attr)
1033{
1034 int ret = -ENXIO;
1035
1036 switch (attr->group) {
1037 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001038 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001039 break;
1040 }
1041
1042 return ret;
1043}
1044
James Morse539aee02018-07-19 16:24:24 +01001045static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1046 struct kvm_vcpu_events *events)
1047{
1048 memset(events, 0, sizeof(*events));
1049
1050 return __kvm_arm_vcpu_get_events(vcpu, events);
1051}
1052
1053static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1054 struct kvm_vcpu_events *events)
1055{
1056 int i;
1057
1058 /* check whether the reserved field is zero */
1059 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1060 if (events->reserved[i])
1061 return -EINVAL;
1062
1063 /* check whether the pad field is zero */
1064 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1065 if (events->exception.pad[i])
1066 return -EINVAL;
1067
1068 return __kvm_arm_vcpu_set_events(vcpu, events);
1069}
James Morse539aee02018-07-19 16:24:24 +01001070
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001071long kvm_arch_vcpu_ioctl(struct file *filp,
1072 unsigned int ioctl, unsigned long arg)
1073{
1074 struct kvm_vcpu *vcpu = filp->private_data;
1075 void __user *argp = (void __user *)arg;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001076 struct kvm_device_attr attr;
Christoffer Dall9b0624712017-12-04 21:35:36 +01001077 long r;
1078
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001079 switch (ioctl) {
1080 case KVM_ARM_VCPU_INIT: {
1081 struct kvm_vcpu_init init;
1082
Christoffer Dall9b0624712017-12-04 21:35:36 +01001083 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001084 if (copy_from_user(&init, argp, sizeof(init)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001085 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001086
Christoffer Dall9b0624712017-12-04 21:35:36 +01001087 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1088 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001089 }
1090 case KVM_SET_ONE_REG:
1091 case KVM_GET_ONE_REG: {
1092 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001093
Christoffer Dall9b0624712017-12-04 21:35:36 +01001094 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001095 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001096 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001097
Christoffer Dall9b0624712017-12-04 21:35:36 +01001098 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001099 if (copy_from_user(&reg, argp, sizeof(reg)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001100 break;
1101
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001102 if (ioctl == KVM_SET_ONE_REG)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001103 r = kvm_arm_set_reg(vcpu, &reg);
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001104 else
Christoffer Dall9b0624712017-12-04 21:35:36 +01001105 r = kvm_arm_get_reg(vcpu, &reg);
1106 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001107 }
1108 case KVM_GET_REG_LIST: {
1109 struct kvm_reg_list __user *user_list = argp;
1110 struct kvm_reg_list reg_list;
1111 unsigned n;
1112
Christoffer Dall9b0624712017-12-04 21:35:36 +01001113 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001114 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001115 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001116
Dave Martin7dd32a02018-12-19 14:27:01 +00001117 r = -EPERM;
1118 if (!kvm_arm_vcpu_is_finalized(vcpu))
1119 break;
1120
Christoffer Dall9b0624712017-12-04 21:35:36 +01001121 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001122 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001123 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001124 n = reg_list.n;
1125 reg_list.n = kvm_arm_num_regs(vcpu);
1126 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001127 break;
1128 r = -E2BIG;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001129 if (n < reg_list.n)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001130 break;
1131 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1132 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001133 }
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001134 case KVM_SET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001135 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001136 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001137 break;
1138 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1139 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001140 }
1141 case KVM_GET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001142 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001143 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001144 break;
1145 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1146 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001147 }
1148 case KVM_HAS_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001149 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001150 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001151 break;
1152 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1153 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001154 }
Dongjiu Gengb7b27fa2018-07-19 16:24:22 +01001155 case KVM_GET_VCPU_EVENTS: {
1156 struct kvm_vcpu_events events;
1157
1158 if (kvm_arm_vcpu_get_events(vcpu, &events))
1159 return -EINVAL;
1160
1161 if (copy_to_user(argp, &events, sizeof(events)))
1162 return -EFAULT;
1163
1164 return 0;
1165 }
1166 case KVM_SET_VCPU_EVENTS: {
1167 struct kvm_vcpu_events events;
1168
1169 if (copy_from_user(&events, argp, sizeof(events)))
1170 return -EFAULT;
1171
1172 return kvm_arm_vcpu_set_events(vcpu, &events);
1173 }
Dave Martin7dd32a02018-12-19 14:27:01 +00001174 case KVM_ARM_VCPU_FINALIZE: {
1175 int what;
1176
1177 if (!kvm_vcpu_initialized(vcpu))
1178 return -ENOEXEC;
1179
1180 if (get_user(what, (const int __user *)argp))
1181 return -EFAULT;
1182
1183 return kvm_arm_vcpu_finalize(vcpu, what);
1184 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001185 default:
Christoffer Dall9b0624712017-12-04 21:35:36 +01001186 r = -EINVAL;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001187 }
Christoffer Dall9b0624712017-12-04 21:35:36 +01001188
Christoffer Dall9b0624712017-12-04 21:35:36 +01001189 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001190}
1191
Mario Smarduch53c810c2015-01-15 15:58:57 -08001192/**
1193 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1194 * @kvm: kvm instance
1195 * @log: slot id and address to which we copy the log
1196 *
1197 * Steps 1-4 below provide general overview of dirty page logging. See
1198 * kvm_get_dirty_log_protect() function description for additional details.
1199 *
1200 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1201 * always flush the TLB (step 4) even if previous step failed and the dirty
1202 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1203 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1204 * writes will be marked dirty for next log read.
1205 *
1206 * 1. Take a snapshot of the bit and clear it if needed.
1207 * 2. Write protect the corresponding page.
1208 * 3. Copy the snapshot to the userspace.
1209 * 4. Flush TLB's if needed.
1210 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001211int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1212{
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02001213 bool flush = false;
Mario Smarduch53c810c2015-01-15 15:58:57 -08001214 int r;
1215
1216 mutex_lock(&kvm->slots_lock);
1217
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02001218 r = kvm_get_dirty_log_protect(kvm, log, &flush);
Mario Smarduch53c810c2015-01-15 15:58:57 -08001219
Paolo Bonzini8fe65a82018-10-23 02:18:42 +02001220 if (flush)
Mario Smarduch53c810c2015-01-15 15:58:57 -08001221 kvm_flush_remote_tlbs(kvm);
1222
1223 mutex_unlock(&kvm->slots_lock);
1224 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001225}
1226
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001227int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
1228{
1229 bool flush = false;
1230 int r;
1231
1232 mutex_lock(&kvm->slots_lock);
1233
1234 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
1235
1236 if (flush)
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001237 kvm_flush_remote_tlbs(kvm);
1238
1239 mutex_unlock(&kvm->slots_lock);
1240 return r;
1241}
1242
Christoffer Dall3401d5462013-01-23 13:18:04 -05001243static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1244 struct kvm_arm_device_addr *dev_addr)
1245{
Christoffer Dall330690c2013-01-21 19:36:13 -05001246 unsigned long dev_id, type;
1247
1248 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1249 KVM_ARM_DEVICE_ID_SHIFT;
1250 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1251 KVM_ARM_DEVICE_TYPE_SHIFT;
1252
1253 switch (dev_id) {
1254 case KVM_ARM_DEVICE_VGIC_V2:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001255 if (!vgic_present)
1256 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001257 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -05001258 default:
1259 return -ENODEV;
1260 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001261}
1262
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001263long kvm_arch_vm_ioctl(struct file *filp,
1264 unsigned int ioctl, unsigned long arg)
1265{
Christoffer Dall3401d5462013-01-23 13:18:04 -05001266 struct kvm *kvm = filp->private_data;
1267 void __user *argp = (void __user *)arg;
1268
1269 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001270 case KVM_CREATE_IRQCHIP: {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001271 int ret;
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001272 if (!vgic_present)
1273 return -ENXIO;
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001274 mutex_lock(&kvm->lock);
1275 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1276 mutex_unlock(&kvm->lock);
1277 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001278 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001279 case KVM_ARM_SET_DEVICE_ADDR: {
1280 struct kvm_arm_device_addr dev_addr;
1281
1282 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1283 return -EFAULT;
1284 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1285 }
Anup Patel42c4e0c2013-09-30 14:20:07 +05301286 case KVM_ARM_PREFERRED_TARGET: {
1287 int err;
1288 struct kvm_vcpu_init init;
1289
1290 err = kvm_vcpu_preferred_target(&init);
1291 if (err)
1292 return err;
1293
1294 if (copy_to_user(argp, &init, sizeof(init)))
1295 return -EFAULT;
1296
1297 return 0;
1298 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001299 default:
1300 return -EINVAL;
1301 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001302}
1303
Miaohe Lin7e0befd2019-11-21 15:15:59 +08001304static void cpu_init_hyp_mode(void)
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001305{
Marc Zyngierdac288f2013-05-14 12:11:37 +01001306 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001307 unsigned long hyp_stack_ptr;
1308 unsigned long stack_page;
1309 unsigned long vector_ptr;
1310
1311 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001312 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001313
Marc Zyngierdac288f2013-05-14 12:11:37 +01001314 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +01001315 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001316 hyp_stack_ptr = stack_page + PAGE_SIZE;
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001317 vector_ptr = (unsigned long)kvm_get_hyp_vector();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001318
Marc Zyngier12fda812016-06-30 18:40:45 +01001319 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
Marc Zyngier35a24912016-02-01 17:54:35 +00001320 __cpu_init_stage2();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001321}
1322
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001323static void cpu_hyp_reset(void)
1324{
1325 if (!is_kernel_in_hyp_mode())
1326 __hyp_reset_vectors();
1327}
1328
James Morse5f5560b2016-03-30 18:33:04 +01001329static void cpu_hyp_reinit(void)
1330{
Marc Zyngier1e0cf162019-07-05 23:35:56 +01001331 kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt);
1332
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001333 cpu_hyp_reset();
1334
Marc Zyngier9d47bb02018-10-01 13:41:32 +01001335 if (is_kernel_in_hyp_mode())
Hu Huajun02d50cd2017-06-12 22:37:48 +08001336 kvm_timer_init_vhe();
Marc Zyngier9d47bb02018-10-01 13:41:32 +01001337 else
Miaohe Lin7e0befd2019-11-21 15:15:59 +08001338 cpu_init_hyp_mode();
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001339
Mark Rutlandda5a3ce2018-10-17 17:42:10 +01001340 kvm_arm_init_debug();
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001341
1342 if (vgic_present)
1343 kvm_vgic_init_cpu_hardware();
James Morse5f5560b2016-03-30 18:33:04 +01001344}
1345
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001346static void _kvm_arch_hardware_enable(void *discard)
1347{
1348 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1349 cpu_hyp_reinit();
1350 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1351 }
1352}
1353
1354int kvm_arch_hardware_enable(void)
1355{
1356 _kvm_arch_hardware_enable(NULL);
1357 return 0;
1358}
1359
1360static void _kvm_arch_hardware_disable(void *discard)
1361{
1362 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1363 cpu_hyp_reset();
1364 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1365 }
1366}
1367
1368void kvm_arch_hardware_disable(void)
1369{
1370 _kvm_arch_hardware_disable(NULL);
1371}
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001372
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001373#ifdef CONFIG_CPU_PM
1374static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1375 unsigned long cmd,
1376 void *v)
1377{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001378 /*
1379 * kvm_arm_hardware_enabled is left with its old value over
1380 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1381 * re-enable hyp.
1382 */
1383 switch (cmd) {
1384 case CPU_PM_ENTER:
1385 if (__this_cpu_read(kvm_arm_hardware_enabled))
1386 /*
1387 * don't update kvm_arm_hardware_enabled here
1388 * so that the hardware will be re-enabled
1389 * when we resume. See below.
1390 */
1391 cpu_hyp_reset();
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001392
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001393 return NOTIFY_OK;
James Morse58d6b152018-01-22 18:19:06 +00001394 case CPU_PM_ENTER_FAILED:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001395 case CPU_PM_EXIT:
1396 if (__this_cpu_read(kvm_arm_hardware_enabled))
1397 /* The hardware was enabled before suspend. */
1398 cpu_hyp_reinit();
1399
1400 return NOTIFY_OK;
1401
1402 default:
1403 return NOTIFY_DONE;
1404 }
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001405}
1406
1407static struct notifier_block hyp_init_cpu_pm_nb = {
1408 .notifier_call = hyp_init_cpu_pm_notifier,
1409};
1410
1411static void __init hyp_cpu_pm_init(void)
1412{
1413 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1414}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001415static void __init hyp_cpu_pm_exit(void)
1416{
1417 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1418}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001419#else
1420static inline void hyp_cpu_pm_init(void)
1421{
1422}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001423static inline void hyp_cpu_pm_exit(void)
1424{
1425}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001426#endif
1427
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001428static int init_common_resources(void)
1429{
Suzuki K Poulose0f62f0e2018-09-26 17:32:52 +01001430 kvm_set_ipa_limit();
1431
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001432 return 0;
1433}
1434
1435static int init_subsystems(void)
1436{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001437 int err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001438
1439 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001440 * Enable hardware so that subsystem initialisation can access EL2.
James Morse5f5560b2016-03-30 18:33:04 +01001441 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001442 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
James Morse5f5560b2016-03-30 18:33:04 +01001443
1444 /*
1445 * Register CPU lower-power notifier
1446 */
1447 hyp_cpu_pm_init();
1448
1449 /*
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001450 * Init HYP view of VGIC
1451 */
1452 err = kvm_vgic_hyp_init();
1453 switch (err) {
1454 case 0:
1455 vgic_present = true;
1456 break;
1457 case -ENODEV:
1458 case -ENXIO:
1459 vgic_present = false;
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001460 err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001461 break;
1462 default:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001463 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001464 }
1465
1466 /*
1467 * Init HYP architected timer support
1468 */
Marc Zyngierf384dcf2017-12-07 11:46:15 +00001469 err = kvm_timer_hyp_init(vgic_present);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001470 if (err)
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001471 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001472
1473 kvm_perf_init();
1474 kvm_coproc_table_init();
1475
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001476out:
1477 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1478
1479 return err;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001480}
1481
1482static void teardown_hyp_mode(void)
1483{
1484 int cpu;
1485
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001486 free_hyp_pgds();
1487 for_each_possible_cpu(cpu)
1488 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1489}
1490
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001491/**
1492 * Inits Hyp-mode on all online CPUs
1493 */
1494static int init_hyp_mode(void)
1495{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001496 int cpu;
1497 int err = 0;
1498
1499 /*
1500 * Allocate Hyp PGD and setup Hyp identity mapping
1501 */
1502 err = kvm_mmu_init();
1503 if (err)
1504 goto out_err;
1505
1506 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001507 * Allocate stack pages for Hypervisor-mode
1508 */
1509 for_each_possible_cpu(cpu) {
1510 unsigned long stack_page;
1511
1512 stack_page = __get_free_page(GFP_KERNEL);
1513 if (!stack_page) {
1514 err = -ENOMEM;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001515 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001516 }
1517
1518 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1519 }
1520
1521 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001522 * Map the Hyp-code called directly from the host
1523 */
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001524 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001525 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001526 if (err) {
1527 kvm_err("Cannot map world-switch code\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001528 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001529 }
1530
Ard Biesheuvela0bf9772016-02-16 13:52:39 +01001531 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
Marc Zyngier74a6b882016-06-13 15:00:47 +01001532 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
Marc Zyngier910917b2015-10-27 12:18:48 +00001533 if (err) {
1534 kvm_err("Cannot map rodata section\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001535 goto out_err;
Marc Zyngier910917b2015-10-27 12:18:48 +00001536 }
1537
Marc Zyngierc8ea0392016-10-20 10:17:21 +01001538 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1539 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1540 if (err) {
1541 kvm_err("Cannot map bss section\n");
1542 goto out_err;
1543 }
1544
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001545 err = kvm_map_vectors();
1546 if (err) {
1547 kvm_err("Cannot map vectors\n");
1548 goto out_err;
1549 }
1550
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001551 /*
1552 * Map the Hyp stack pages
1553 */
1554 for_each_possible_cpu(cpu) {
1555 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001556 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1557 PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001558
1559 if (err) {
1560 kvm_err("Cannot map hyp stack\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001561 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001562 }
1563 }
1564
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001565 for_each_possible_cpu(cpu) {
Andrew Murray630a1682019-04-09 20:22:11 +01001566 kvm_host_data_t *cpu_data;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001567
Andrew Murray630a1682019-04-09 20:22:11 +01001568 cpu_data = per_cpu_ptr(&kvm_host_data, cpu);
Andrew Murray630a1682019-04-09 20:22:11 +01001569 err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001570
1571 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001572 kvm_err("Cannot map host CPU state: %d\n", err);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001573 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001574 }
1575 }
1576
Marc Zyngier55e37482018-05-29 13:11:16 +01001577 err = hyp_map_aux_data();
1578 if (err)
Colin Ian Kinga37f0c32019-02-17 22:36:26 +00001579 kvm_err("Cannot map host auxiliary data: %d\n", err);
Marc Zyngier55e37482018-05-29 13:11:16 +01001580
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001581 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001582
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001583out_err:
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001584 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001585 kvm_err("error initializing Hyp mode: %d\n", err);
1586 return err;
1587}
1588
Andre Przywarad4e071c2013-04-17 12:52:01 +02001589static void check_kvm_target_cpu(void *ret)
1590{
1591 *(int *)ret = kvm_target_cpu();
1592}
1593
Andre Przywara4429fc62014-06-02 15:37:13 +02001594struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1595{
1596 struct kvm_vcpu *vcpu;
1597 int i;
1598
1599 mpidr &= MPIDR_HWID_BITMASK;
1600 kvm_for_each_vcpu(i, vcpu, kvm) {
1601 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1602 return vcpu;
1603 }
1604 return NULL;
1605}
1606
Eric Auger24124052017-10-27 15:28:31 +01001607bool kvm_arch_has_irq_bypass(void)
1608{
1609 return true;
1610}
1611
1612int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
1613 struct irq_bypass_producer *prod)
1614{
1615 struct kvm_kernel_irqfd *irqfd =
1616 container_of(cons, struct kvm_kernel_irqfd, consumer);
1617
Marc Zyngier196b1362017-10-27 15:28:39 +01001618 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
1619 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001620}
1621void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
1622 struct irq_bypass_producer *prod)
1623{
1624 struct kvm_kernel_irqfd *irqfd =
1625 container_of(cons, struct kvm_kernel_irqfd, consumer);
1626
Marc Zyngier196b1362017-10-27 15:28:39 +01001627 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
1628 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001629}
1630
1631void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
1632{
1633 struct kvm_kernel_irqfd *irqfd =
1634 container_of(cons, struct kvm_kernel_irqfd, consumer);
1635
1636 kvm_arm_halt_guest(irqfd->kvm);
1637}
1638
1639void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
1640{
1641 struct kvm_kernel_irqfd *irqfd =
1642 container_of(cons, struct kvm_kernel_irqfd, consumer);
1643
1644 kvm_arm_resume_guest(irqfd->kvm);
1645}
1646
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001647/**
1648 * Initialize Hyp-mode and memory mappings on all CPUs.
1649 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001650int kvm_arch_init(void *opaque)
1651{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001652 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001653 int ret, cpu;
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001654 bool in_hyp_mode;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001655
1656 if (!is_hyp_mode_available()) {
Ard Biesheuvel58d0d192017-11-28 15:18:19 +00001657 kvm_info("HYP mode not available\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001658 return -ENODEV;
1659 }
1660
Marc Zyngier33e5f4e2018-12-06 17:31:20 +00001661 in_hyp_mode = is_kernel_in_hyp_mode();
1662
1663 if (!in_hyp_mode && kvm_arch_requires_vhe()) {
1664 kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n");
Dave Martin85acda32018-04-20 16:20:43 +01001665 return -ENODEV;
1666 }
1667
Andre Przywarad4e071c2013-04-17 12:52:01 +02001668 for_each_online_cpu(cpu) {
1669 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1670 if (ret < 0) {
1671 kvm_err("Error, CPU %d not supported!\n", cpu);
1672 return -ENODEV;
1673 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001674 }
1675
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001676 err = init_common_resources();
1677 if (err)
1678 return err;
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301679
Dave Martina3be8362019-04-12 15:30:58 +01001680 err = kvm_arm_init_sve();
Dave Martin0f062bf2019-02-28 18:33:00 +00001681 if (err)
1682 return err;
1683
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001684 if (!in_hyp_mode) {
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001685 err = init_hyp_mode();
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001686 if (err)
1687 goto out_err;
1688 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001689
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001690 err = init_subsystems();
1691 if (err)
1692 goto out_hyp;
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001693
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001694 if (in_hyp_mode)
1695 kvm_info("VHE mode initialized successfully\n");
1696 else
1697 kvm_info("Hyp mode initialized successfully\n");
1698
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001699 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001700
1701out_hyp:
Shannon Zhaoc3e35402019-12-02 15:42:11 +08001702 hyp_cpu_pm_exit();
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001703 if (!in_hyp_mode)
1704 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001705out_err:
1706 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001707}
1708
1709/* NOP: Compiling as a module not supported */
1710void kvm_arch_exit(void)
1711{
Marc Zyngier210552c2013-03-05 03:18:00 +00001712 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001713}
1714
1715static int arm_init(void)
1716{
1717 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1718 return rc;
1719}
1720
1721module_init(arm_init);