blob: e52f2b2305b5fac713c586033cb209885cf06d78 [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
Marc Zyngier9ed24f42020-05-13 11:40:34 +010025#include "trace_arm.h"
Christoffer Dall749cf76c2013-01-20 18:28:06 -050026
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
Sean Christophersonb9904082020-03-21 13:25:55 -070067int kvm_arch_hardware_setup(void *opaque)
Christoffer Dall749cf76c2013-01-20 18:28:06 -050068{
69 return 0;
70}
71
Sean Christophersonb9904082020-03-21 13:25:55 -070072int kvm_arch_check_processor_compat(void *opaque)
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
Marc Zyngier51070002020-04-27 15:15:07 +010098static int kvm_arm_default_max_vcpus(void)
99{
100 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
101}
102
Christoffer Dalld5d81842013-01-20 18:28:07 -0500103/**
104 * kvm_arch_init_vm - initializes a VM data structure
105 * @kvm: pointer to the KVM struct
106 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500107int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
108{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100109 int ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500110
Marc Zyngierbca607e2018-10-01 13:40:36 +0100111 ret = kvm_arm_setup_stage2(kvm, type);
Suzuki K Poulose5b6c6742018-09-26 17:32:42 +0100112 if (ret)
113 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500114
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100115 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500116 if (ret)
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100117 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500118
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100119 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500120 if (ret)
121 goto out_free_stage2_pgd;
122
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100123 kvm_vgic_early_init(kvm);
Christoffer Dalla1a64382013-11-16 10:51:25 -0800124
Andre Przywara3caa2d82014-06-02 16:26:01 +0200125 /* The maximum number of VCPUs is limited by the host's GIC model */
Marc Zyngier51070002020-04-27 15:15:07 +0100126 kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
Andre Przywara3caa2d82014-06-02 16:26:01 +0200127
Christoffer Dalld5d81842013-01-20 18:28:07 -0500128 return ret;
129out_free_stage2_pgd:
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100130 kvm_free_stage2_pgd(&kvm->arch.mmu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500131 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500132}
133
Souptick Joarder1499fa82018-04-19 00:49:58 +0530134vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500135{
136 return VM_FAULT_SIGBUS;
137}
138
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500139
Christoffer Dalld5d81842013-01-20 18:28:07 -0500140/**
141 * kvm_arch_destroy_vm - destroy the VM data structure
142 * @kvm: pointer to the KVM struct
143 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500144void kvm_arch_destroy_vm(struct kvm *kvm)
145{
146 int i;
147
Marc Zyngierb2c9a852017-10-27 15:28:34 +0100148 kvm_vgic_destroy(kvm);
149
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500150 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
151 if (kvm->vcpus[i]) {
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800152 kvm_vcpu_destroy(kvm->vcpus[i]);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500153 kvm->vcpus[i] = NULL;
154 }
155 }
Andrew Jones6b2ad812017-11-27 19:17:18 +0100156 atomic_set(&kvm->online_vcpus, 0);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500157}
158
Alexander Graf784aa3d2014-07-14 18:27:35 +0200159int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500160{
161 int r;
162 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500163 case KVM_CAP_IRQCHIP:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300164 r = vgic_present;
165 break;
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000166 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100167 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500168 case KVM_CAP_USER_MEMORY:
169 case KVM_CAP_SYNC_MMU:
170 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
171 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500172 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530173 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200174 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000175 case KVM_CAP_MP_STATE:
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100176 case KVM_CAP_IMMEDIATE_EXIT:
Dongjiu Geng58bf4372018-10-13 00:12:49 +0800177 case KVM_CAP_VCPU_EVENTS:
Marc Zyngier92f35b72019-08-18 14:09:47 +0100178 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
Christoffer Dallc7262002019-10-11 13:07:05 +0200179 case KVM_CAP_ARM_NISV_TO_USER:
Christoffer Dallda345172019-10-11 13:07:06 +0200180 case KVM_CAP_ARM_INJECT_EXT_DABT:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500181 r = 1;
182 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500183 case KVM_CAP_ARM_SET_DEVICE_ADDR:
184 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100185 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500186 case KVM_CAP_NR_VCPUS:
187 r = num_online_cpus();
188 break;
189 case KVM_CAP_MAX_VCPUS:
Thomas Hutha86cb412019-05-23 18:43:08 +0200190 case KVM_CAP_MAX_VCPU_ID:
Marc Zyngier51070002020-04-27 15:15:07 +0100191 if (kvm)
192 r = kvm->arch.max_vcpus;
193 else
194 r = kvm_arm_default_max_vcpus();
Thomas Hutha86cb412019-05-23 18:43:08 +0200195 break;
Vladimir Murzin29885092016-11-02 11:55:34 +0000196 case KVM_CAP_MSI_DEVID:
197 if (!kvm)
198 r = -EINVAL;
199 else
200 r = kvm->arch.vgic.msis_require_devid;
201 break;
Christoffer Dallf7214e62017-02-01 12:54:11 +0100202 case KVM_CAP_ARM_USER_IRQ:
203 /*
204 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
205 * (bump this number if adding more devices)
206 */
207 r = 1;
208 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500209 default:
Dongjiu Geng375bdd32018-10-13 00:12:48 +0800210 r = kvm_arch_vm_ioctl_check_extension(kvm, ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500211 break;
212 }
213 return r;
214}
215
216long kvm_arch_dev_ioctl(struct file *filp,
217 unsigned int ioctl, unsigned long arg)
218{
219 return -EINVAL;
220}
221
Marc Orrd1e5b0e2018-05-15 04:37:37 -0700222struct kvm *kvm_arch_alloc_vm(void)
223{
224 if (!has_vhe())
225 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
226
227 return vzalloc(sizeof(struct kvm));
228}
229
230void kvm_arch_free_vm(struct kvm *kvm)
231{
232 if (!has_vhe())
233 kfree(kvm);
234 else
235 vfree(kvm);
236}
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500237
Sean Christopherson897cc382019-12-18 13:55:09 -0800238int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
239{
240 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
241 return -EBUSY;
242
243 if (id >= kvm->arch.max_vcpus)
244 return -EINVAL;
245
246 return 0;
247}
248
Sean Christophersone529ef62019-12-18 13:55:15 -0800249int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500250{
Sean Christopherson39a93a82019-12-18 13:55:25 -0800251 int err;
252
253 /* Force users to call KVM_ARM_VCPU_INIT */
254 vcpu->arch.target = -1;
255 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
256
257 /* Set up the timer */
258 kvm_timer_vcpu_init(vcpu);
259
260 kvm_pmu_vcpu_init(vcpu);
261
262 kvm_arm_reset_debug_ptr(vcpu);
263
264 kvm_arm_pvtime_vcpu_init(&vcpu->arch);
265
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100266 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
267
Sean Christopherson39a93a82019-12-18 13:55:25 -0800268 err = kvm_vgic_vcpu_init(vcpu);
269 if (err)
270 return err;
271
Sean Christophersone529ef62019-12-18 13:55:15 -0800272 return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500273}
274
Dominik Dingel31928aa2014-12-04 15:47:07 +0100275void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500276{
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500277}
278
Sean Christopherson4b8fff72019-12-18 13:55:04 -0800279void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500280{
Christoffer Dallf1d72312018-01-25 18:32:29 +0100281 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
282 static_branch_dec(&userspace_irqchip_in_use);
283
Christoffer Dalld5d81842013-01-20 18:28:07 -0500284 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500285 kvm_timer_vcpu_terminate(vcpu);
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800286 kvm_pmu_vcpu_destroy(vcpu);
Sean Christopherson19bcc892019-12-18 13:55:27 -0800287
288 kvm_arm_vcpu_destroy(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500289}
290
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500291int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
292{
Christoffer Dall1c88ab72017-01-06 16:07:48 +0100293 return kvm_timer_is_pending(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500294}
295
Christoffer Dalld35268d2015-08-25 19:48:21 +0200296void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
297{
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100298 /*
299 * If we're about to block (most likely because we've just hit a
300 * WFI), we need to sync back the state of the GIC CPU interface
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000301 * so that we have the latest PMR and group enables. This ensures
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100302 * that kvm_arch_vcpu_runnable has up-to-date data to decide
303 * whether we have pending interrupts.
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000304 *
305 * For the same reason, we want to tell GICv4 that we need
306 * doorbells to be signalled, should an interrupt become pending.
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100307 */
308 preempt_disable();
309 kvm_vgic_vmcr_sync(vcpu);
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000310 vgic_v4_put(vcpu, true);
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100311 preempt_enable();
Christoffer Dalld35268d2015-08-25 19:48:21 +0200312}
313
314void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
315{
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000316 preempt_disable();
317 vgic_v4_load(vcpu);
318 preempt_enable();
Christoffer Dalld35268d2015-08-25 19:48:21 +0200319}
320
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500321void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
322{
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100323 struct kvm_s2_mmu *mmu;
Marc Zyngier94d0e592016-10-18 18:37:49 +0100324 int *last_ran;
325
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100326 mmu = vcpu->arch.hw_mmu;
327 last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
Marc Zyngier94d0e592016-10-18 18:37:49 +0100328
329 /*
330 * We might get preempted before the vCPU actually runs, but
331 * over-invalidation doesn't affect correctness.
332 */
333 if (*last_ran != vcpu->vcpu_id) {
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100334 kvm_call_hyp(__kvm_tlb_flush_local_vmid, mmu);
Marc Zyngier94d0e592016-10-18 18:37:49 +0100335 *last_ran = vcpu->vcpu_id;
336 }
337
Christoffer Dall86ce8532013-01-20 18:28:08 -0500338 vcpu->cpu = cpu;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500339
Christoffer Dall328e5662016-03-24 11:21:04 +0100340 kvm_vgic_load(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200341 kvm_timer_vcpu_load(vcpu);
David Brazdil13aeb9b2020-06-25 14:14:16 +0100342 if (has_vhe())
343 kvm_vcpu_load_sysregs_vhe(vcpu);
Dave Martine6b673b2018-04-06 14:55:59 +0100344 kvm_arch_vcpu_load_fp(vcpu);
Andrew Murray435e53f2019-04-09 20:22:15 +0100345 kvm_vcpu_pmu_restore_guest(vcpu);
Steven Price8564d632019-10-21 16:28:18 +0100346 if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
347 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
Marc Zyngierde737082018-06-21 10:43:59 +0100348
349 if (single_task_running())
Marc Zyngieref2e78d2019-11-07 16:04:12 +0000350 vcpu_clear_wfx_traps(vcpu);
Marc Zyngierde737082018-06-21 10:43:59 +0100351 else
Marc Zyngieref2e78d2019-11-07 16:04:12 +0000352 vcpu_set_wfx_traps(vcpu);
Mark Rutland384b40c2019-04-23 10:12:35 +0530353
Marc Zyngier29eb5a32020-06-04 11:14:00 +0100354 if (vcpu_has_ptrauth(vcpu))
Marc Zyngieref3e40a2020-06-03 18:24:01 +0100355 vcpu_ptrauth_disable(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500356}
357
358void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
359{
Dave Martine6b673b2018-04-06 14:55:59 +0100360 kvm_arch_vcpu_put_fp(vcpu);
David Brazdil13aeb9b2020-06-25 14:14:16 +0100361 if (has_vhe())
362 kvm_vcpu_put_sysregs_vhe(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200363 kvm_timer_vcpu_put(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100364 kvm_vgic_put(vcpu);
Andrew Murray435e53f2019-04-09 20:22:15 +0100365 kvm_vcpu_pmu_restore_host(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100366
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800367 vcpu->cpu = -1;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500368}
369
Andrew Jones424c9892017-06-04 14:43:57 +0200370static void vcpu_power_off(struct kvm_vcpu *vcpu)
371{
372 vcpu->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200373 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones424c9892017-06-04 14:43:57 +0200374 kvm_vcpu_kick(vcpu);
375}
376
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500377int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
378 struct kvm_mp_state *mp_state)
379{
Eric Auger37815282015-09-25 23:41:14 +0200380 if (vcpu->arch.power_off)
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000381 mp_state->mp_state = KVM_MP_STATE_STOPPED;
382 else
383 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
384
385 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500386}
387
388int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
389 struct kvm_mp_state *mp_state)
390{
Christoffer Dalle83dff52017-12-04 21:35:31 +0100391 int ret = 0;
392
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000393 switch (mp_state->mp_state) {
394 case KVM_MP_STATE_RUNNABLE:
Eric Auger37815282015-09-25 23:41:14 +0200395 vcpu->arch.power_off = false;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000396 break;
397 case KVM_MP_STATE_STOPPED:
Andrew Jones424c9892017-06-04 14:43:57 +0200398 vcpu_power_off(vcpu);
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000399 break;
400 default:
Christoffer Dalle83dff52017-12-04 21:35:31 +0100401 ret = -EINVAL;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000402 }
403
Christoffer Dalle83dff52017-12-04 21:35:31 +0100404 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500405}
406
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500407/**
408 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
409 * @v: The VCPU pointer
410 *
411 * If the guest CPU is not waiting for interrupts or an interrupt line is
412 * asserted, the CPU is by definition runnable.
413 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500414int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
415{
Christoffer Dall3df59d82017-08-03 12:09:05 +0200416 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
417 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
Eric Auger3b928302015-09-25 23:41:17 +0200418 && !v->arch.power_off && !v->arch.pause);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500419}
420
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800421bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
422{
Longpeng(Mike)f01fbd22017-08-08 12:05:35 +0800423 return vcpu_mode_priv(vcpu);
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800424}
425
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500426/* Just ensure a guest exit from a particular CPU */
427static void exit_vm_noop(void *info)
428{
429}
430
431void force_vm_exit(const cpumask_t *mask)
432{
Eric Auger898f9492016-03-07 23:50:36 +0700433 preempt_disable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500434 smp_call_function_many(mask, exit_vm_noop, NULL, true);
Eric Auger898f9492016-03-07 23:50:36 +0700435 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500436}
437
438/**
439 * need_new_vmid_gen - check that the VMID is still valid
Christoffer Dalle329fb72018-12-11 15:26:31 +0100440 * @vmid: The VMID to check
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500441 *
442 * return true if there is a new generation of VMIDs being used
443 *
Christoffer Dalle329fb72018-12-11 15:26:31 +0100444 * The hardware supports a limited set of values with the value zero reserved
445 * for the host, so we check if an assigned value belongs to a previous
Fuad Tabba656012c2020-04-01 15:03:10 +0100446 * generation, which requires us to assign a new value. If we're the first to
447 * use a VMID for the new generation, we must flush necessary caches and TLBs
448 * on all CPUs.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500449 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100450static bool need_new_vmid_gen(struct kvm_vmid *vmid)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500451{
Christoffer Dallfb544d12018-12-11 13:23:57 +0100452 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
453 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100454 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500455}
456
457/**
Christoffer Dalle329fb72018-12-11 15:26:31 +0100458 * update_vmid - Update the vmid with a valid VMID for the current generation
459 * @kvm: The guest that struct vmid belongs to
460 * @vmid: The stage-2 VMID information struct
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500461 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100462static void update_vmid(struct kvm_vmid *vmid)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500463{
Christoffer Dalle329fb72018-12-11 15:26:31 +0100464 if (!need_new_vmid_gen(vmid))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500465 return;
466
Christoffer Dallfb544d12018-12-11 13:23:57 +0100467 spin_lock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500468
469 /*
470 * We need to re-check the vmid_gen here to ensure that if another vcpu
471 * already allocated a valid vmid for this vm, then this vcpu should
472 * use the same vmid.
473 */
Christoffer Dalle329fb72018-12-11 15:26:31 +0100474 if (!need_new_vmid_gen(vmid)) {
Christoffer Dallfb544d12018-12-11 13:23:57 +0100475 spin_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500476 return;
477 }
478
479 /* First user of a new VMID generation? */
480 if (unlikely(kvm_next_vmid == 0)) {
481 atomic64_inc(&kvm_vmid_gen);
482 kvm_next_vmid = 1;
483
484 /*
485 * On SMP we know no other CPUs can use this CPU's or each
486 * other's VMID after force_vm_exit returns since the
487 * kvm_vmid_lock blocks them from reentry to the guest.
488 */
489 force_vm_exit(cpu_all_mask);
490 /*
491 * Now broadcast TLB + ICACHE invalidation over the inner
492 * shareable domain to make sure all data structures are
493 * clean.
494 */
495 kvm_call_hyp(__kvm_flush_vm_context);
496 }
497
Christoffer Dalle329fb72018-12-11 15:26:31 +0100498 vmid->vmid = kvm_next_vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500499 kvm_next_vmid++;
Christoffer Dalle329fb72018-12-11 15:26:31 +0100500 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500501
Christoffer Dallfb544d12018-12-11 13:23:57 +0100502 smp_wmb();
Christoffer Dalle329fb72018-12-11 15:26:31 +0100503 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
Christoffer Dallfb544d12018-12-11 13:23:57 +0100504
505 spin_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500506}
507
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500508static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
509{
Christoffer Dall05971122014-12-12 21:19:23 +0100510 struct kvm *kvm = vcpu->kvm;
Christoffer Dall41a54482016-05-18 16:26:00 +0100511 int ret = 0;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700512
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500513 if (likely(vcpu->arch.has_run_once))
514 return 0;
515
Dave Martin7dd32a02018-12-19 14:27:01 +0000516 if (!kvm_arm_vcpu_is_finalized(vcpu))
517 return -EPERM;
518
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500519 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500520
Christoffer Dall61bbe382017-10-27 19:57:51 +0200521 if (likely(irqchip_in_kernel(kvm))) {
522 /*
523 * Map the VGIC hardware resources before running a vcpu the
524 * first time on this VM.
525 */
526 if (unlikely(!vgic_ready(kvm))) {
527 ret = kvm_vgic_map_resources(kvm);
528 if (ret)
529 return ret;
530 }
531 } else {
532 /*
533 * Tell the rest of the code that there are userspace irqchip
534 * VMs in the wild.
535 */
536 static_branch_inc(&userspace_irqchip_in_use);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500537 }
538
Alexander Grafd9e13972016-09-27 21:08:06 +0200539 ret = kvm_timer_enable(vcpu);
Christoffer Dalla2befac2017-05-02 13:41:02 +0200540 if (ret)
541 return ret;
542
543 ret = kvm_arm_pmu_v3_enable(vcpu);
Christoffer Dall05971122014-12-12 21:19:23 +0100544
Christoffer Dall41a54482016-05-18 16:26:00 +0100545 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500546}
547
Eric Augerc1426e42015-03-04 11:14:34 +0100548bool kvm_arch_intc_initialized(struct kvm *kvm)
549{
550 return vgic_initialized(kvm);
551}
552
Christoffer Dallb13216c2016-04-27 10:28:00 +0100553void kvm_arm_halt_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200554{
555 int i;
556 struct kvm_vcpu *vcpu;
557
558 kvm_for_each_vcpu(i, vcpu, kvm)
559 vcpu->arch.pause = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200560 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
Eric Auger3b928302015-09-25 23:41:17 +0200561}
562
Christoffer Dallb13216c2016-04-27 10:28:00 +0100563void kvm_arm_resume_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200564{
565 int i;
566 struct kvm_vcpu *vcpu;
567
Christoffer Dallabd72292017-05-06 20:01:24 +0200568 kvm_for_each_vcpu(i, vcpu, kvm) {
569 vcpu->arch.pause = false;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700570 rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu));
Christoffer Dallabd72292017-05-06 20:01:24 +0200571 }
Eric Auger3b928302015-09-25 23:41:17 +0200572}
573
Andrew Jones7b244e22017-06-04 14:43:58 +0200574static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500575{
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700576 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500577
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700578 rcuwait_wait_event(wait,
579 (!vcpu->arch.power_off) &&(!vcpu->arch.pause),
580 TASK_INTERRUPTIBLE);
Andrew Jones0592c002017-06-04 14:43:55 +0200581
Andrew Jones424c9892017-06-04 14:43:57 +0200582 if (vcpu->arch.power_off || vcpu->arch.pause) {
Andrew Jones0592c002017-06-04 14:43:55 +0200583 /* Awaken to handle a signal, request we sleep again later. */
Andrew Jones7b244e22017-06-04 14:43:58 +0200584 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones0592c002017-06-04 14:43:55 +0200585 }
Marc Zyngier358b28f2018-12-20 11:36:07 +0000586
587 /*
588 * Make sure we will observe a potential reset request if we've
589 * observed a change to the power state. Pairs with the smp_wmb() in
590 * kvm_psci_vcpu_on().
591 */
592 smp_rmb();
Marc Zyngieraa024c22013-01-20 18:28:13 -0500593}
594
Andre Przywarae8180dc2013-05-09 00:28:06 +0200595static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
596{
597 return vcpu->arch.target >= 0;
598}
599
Andrew Jones0592c002017-06-04 14:43:55 +0200600static void check_vcpu_requests(struct kvm_vcpu *vcpu)
601{
602 if (kvm_request_pending(vcpu)) {
Andrew Jones7b244e22017-06-04 14:43:58 +0200603 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
604 vcpu_req_sleep(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200605
Marc Zyngier358b28f2018-12-20 11:36:07 +0000606 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
607 kvm_reset_vcpu(vcpu);
608
Andrew Jones325f9c62017-06-04 14:43:59 +0200609 /*
610 * Clear IRQ_PENDING requests that were made to guarantee
611 * that a VCPU sees new virtual interrupts.
612 */
613 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
Steven Price8564d632019-10-21 16:28:18 +0100614
615 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
616 kvm_update_stolen_time(vcpu);
Marc Zyngierd9c38722020-03-04 20:33:28 +0000617
618 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
619 /* The distributor enable bits were changed */
620 preempt_disable();
621 vgic_v4_put(vcpu, false);
622 vgic_v4_load(vcpu);
623 preempt_enable();
624 }
Andrew Jones0592c002017-06-04 14:43:55 +0200625 }
626}
627
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500628/**
629 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
630 * @vcpu: The VCPU pointer
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500631 *
632 * This function is called through the VCPU_RUN ioctl called from user space. It
633 * will execute VM code in a loop until the time slice for the process is used
634 * or some emulation is needed from user space in which case the function will
635 * return with return value 0 and with the kvm_run structure filled in with the
636 * required data for the requested emulation.
637 */
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +0800638int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500639{
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +0800640 struct kvm_run *run = vcpu->run;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500641 int ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500642
Andre Przywarae8180dc2013-05-09 00:28:06 +0200643 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500644 return -ENOEXEC;
645
646 ret = kvm_vcpu_first_run_init(vcpu);
647 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100648 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500649
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500650 if (run->exit_reason == KVM_EXIT_MMIO) {
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +0800651 ret = kvm_handle_mmio_return(vcpu, run);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500652 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100653 return ret;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500654 }
655
Christoffer Dall829a5862017-11-29 16:37:53 +0100656 if (run->immediate_exit)
657 return -EINTR;
658
659 vcpu_load(vcpu);
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100660
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100661 kvm_sigset_activate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500662
663 ret = 1;
664 run->exit_reason = KVM_EXIT_UNKNOWN;
665 while (ret > 0) {
666 /*
667 * Check conditions before entering the guest
668 */
669 cond_resched();
670
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100671 update_vmid(&vcpu->arch.hw_mmu->vmid);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500672
Andrew Jones0592c002017-06-04 14:43:55 +0200673 check_vcpu_requests(vcpu);
674
Marc Zyngierabdf5842015-06-08 15:00:28 +0100675 /*
Marc Zyngierabdf5842015-06-08 15:00:28 +0100676 * Preparing the interrupts to be injected also
677 * involves poking the GIC, which must be done in a
678 * non-preemptible context.
679 */
680 preempt_disable();
Christoffer Dall328e5662016-03-24 11:21:04 +0100681
Shannon Zhaob02386e2016-02-26 19:29:19 +0800682 kvm_pmu_flush_hwstate(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100683
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500684 local_irq_disable();
685
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500686 kvm_vgic_flush_hwstate(vcpu);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500687
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500688 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200689 * Exit if we have a signal pending so that we can deliver the
690 * signal to user space.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500691 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200692 if (signal_pending(current)) {
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500693 ret = -EINTR;
694 run->exit_reason = KVM_EXIT_INTR;
695 }
696
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200697 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200698 * If we're using a userspace irqchip, then check if we need
699 * to tell a userspace irqchip about timer or PMU level
700 * changes and if so, exit to userspace (the actual level
701 * state gets updated in kvm_timer_update_run and
702 * kvm_pmu_update_run below).
703 */
704 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
705 if (kvm_timer_should_notify_user(vcpu) ||
706 kvm_pmu_should_notify_user(vcpu)) {
707 ret = -EINTR;
708 run->exit_reason = KVM_EXIT_INTR;
709 }
710 }
711
712 /*
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200713 * Ensure we set mode to IN_GUEST_MODE after we disable
714 * interrupts and before the final VCPU requests check.
715 * See the comment in kvm_vcpu_exiting_guest_mode() and
Christoph Hellwig2f5947d2019-07-24 09:24:49 +0200716 * Documentation/virt/kvm/vcpu-requests.rst
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200717 */
718 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
719
Christoffer Dalla0e50aa2019-01-04 21:09:05 +0100720 if (ret <= 0 || need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) ||
Andrew Jones424c9892017-06-04 14:43:57 +0200721 kvm_request_pending(vcpu)) {
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200722 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall771621b2017-10-04 23:42:32 +0200723 isb(); /* Ensure work in x_flush_hwstate is committed */
Shannon Zhaob02386e2016-02-26 19:29:19 +0800724 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dall61bbe382017-10-27 19:57:51 +0200725 if (static_branch_unlikely(&userspace_irqchip_in_use))
Marc Zyngier3c5ff0c2020-04-22 08:58:22 +0100726 kvm_timer_sync_user(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500727 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200728 local_irq_enable();
Marc Zyngierabdf5842015-06-08 15:00:28 +0100729 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500730 continue;
731 }
732
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100733 kvm_arm_setup_debug(vcpu);
734
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500735 /**************************************************************
736 * Enter the guest
737 */
738 trace_kvm_entry(*vcpu_pc(vcpu));
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200739 guest_enter_irqoff();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200740
David Brazdil09cf57e2020-06-25 14:14:14 +0100741 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200742
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500743 vcpu->mode = OUTSIDE_GUEST_MODE;
Amit Tomarb19e6892015-11-26 10:09:43 +0000744 vcpu->stat.exits++;
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100745 /*
746 * Back from guest
747 *************************************************************/
748
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100749 kvm_arm_clear_debug(vcpu);
750
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500751 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200752 * We must sync the PMU state before the vgic state so
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200753 * that the vgic can properly sample the updated state of the
754 * interrupt line.
755 */
756 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200757
Christoffer Dallb103cc32016-10-16 20:30:38 +0200758 /*
759 * Sync the vgic state before syncing the timer state because
760 * the timer code needs to know if the virtual timer
761 * interrupts are active.
762 */
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200763 kvm_vgic_sync_hwstate(vcpu);
764
765 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200766 * Sync the timer hardware state before enabling interrupts as
767 * we don't want vtimer interrupts to race with syncing the
768 * timer virtual interrupt state.
769 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200770 if (static_branch_unlikely(&userspace_irqchip_in_use))
Marc Zyngier3c5ff0c2020-04-22 08:58:22 +0100771 kvm_timer_sync_user(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200772
Dave Martine6b673b2018-04-06 14:55:59 +0100773 kvm_arch_vcpu_ctxsync_fp(vcpu);
774
Christoffer Dallb103cc32016-10-16 20:30:38 +0200775 /*
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500776 * We may have taken a host interrupt in HYP mode (ie
777 * while executing the guest). This interrupt is still
778 * pending, as we haven't serviced it yet!
779 *
780 * We're now back in SVC mode, with interrupts
781 * disabled. Enabling the interrupts now will have
782 * the effect of taking the interrupt again, in SVC
783 * mode this time.
784 */
785 local_irq_enable();
786
787 /*
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200788 * We do local_irq_enable() before calling guest_exit() so
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100789 * that if a timer interrupt hits while running the guest we
790 * account that tick as being spent in the guest. We enable
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200791 * preemption after calling guest_exit() so that if we get
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100792 * preempted we make sure ticks after that is not counted as
793 * guest time.
794 */
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200795 guest_exit();
Christoffer Dallb5905dc2015-08-30 15:55:22 +0200796 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100797
James Morse3368bd82018-01-15 19:39:04 +0000798 /* Exit types that need handling before we can be preempted */
799 handle_exit_early(vcpu, run, ret);
800
Marc Zyngierabdf5842015-06-08 15:00:28 +0100801 preempt_enable();
802
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500803 ret = handle_exit(vcpu, run, ret);
804 }
805
Alexander Grafd9e13972016-09-27 21:08:06 +0200806 /* Tell userspace about in-kernel device output levels */
Christoffer Dall3dbbdf72017-02-01 12:51:52 +0100807 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
808 kvm_timer_update_run(vcpu);
809 kvm_pmu_update_run(vcpu);
810 }
Alexander Grafd9e13972016-09-27 21:08:06 +0200811
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100812 kvm_sigset_deactivate(vcpu);
813
Christoffer Dallaccb7572017-12-04 21:35:25 +0100814 vcpu_put(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500815 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500816}
817
Christoffer Dall86ce8532013-01-20 18:28:08 -0500818static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
819{
820 int bit_index;
821 bool set;
Christoffer Dall3df59d82017-08-03 12:09:05 +0200822 unsigned long *hcr;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500823
824 if (number == KVM_ARM_IRQ_CPU_IRQ)
825 bit_index = __ffs(HCR_VI);
826 else /* KVM_ARM_IRQ_CPU_FIQ */
827 bit_index = __ffs(HCR_VF);
828
Christoffer Dall3df59d82017-08-03 12:09:05 +0200829 hcr = vcpu_hcr(vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500830 if (level)
Christoffer Dall3df59d82017-08-03 12:09:05 +0200831 set = test_and_set_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500832 else
Christoffer Dall3df59d82017-08-03 12:09:05 +0200833 set = test_and_clear_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500834
835 /*
836 * If we didn't change anything, no need to wake up or kick other CPUs
837 */
838 if (set == level)
839 return 0;
840
841 /*
842 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
843 * trigger a world-switch round on the running physical CPU to set the
844 * virtual IRQ/FIQ fields in the HCR appropriately.
845 */
Andrew Jones325f9c62017-06-04 14:43:59 +0200846 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500847 kvm_vcpu_kick(vcpu);
848
849 return 0;
850}
851
Alexander Graf79558f12013-04-16 19:21:41 +0200852int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
853 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500854{
855 u32 irq = irq_level->irq;
856 unsigned int irq_type, vcpu_idx, irq_num;
857 int nrcpus = atomic_read(&kvm->online_vcpus);
858 struct kvm_vcpu *vcpu = NULL;
859 bool level = irq_level->level;
860
861 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
862 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
Marc Zyngier92f35b72019-08-18 14:09:47 +0100863 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 -0500864 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
865
866 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
867
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500868 switch (irq_type) {
869 case KVM_ARM_IRQ_TYPE_CPU:
870 if (irqchip_in_kernel(kvm))
871 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500872
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500873 if (vcpu_idx >= nrcpus)
874 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500875
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500876 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
877 if (!vcpu)
878 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500879
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500880 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
881 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500882
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500883 return vcpu_interrupt_line(vcpu, irq_num, level);
884 case KVM_ARM_IRQ_TYPE_PPI:
885 if (!irqchip_in_kernel(kvm))
886 return -ENXIO;
887
888 if (vcpu_idx >= nrcpus)
889 return -EINVAL;
890
891 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
892 if (!vcpu)
893 return -EINVAL;
894
895 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
896 return -EINVAL;
897
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200898 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500899 case KVM_ARM_IRQ_TYPE_SPI:
900 if (!irqchip_in_kernel(kvm))
901 return -ENXIO;
902
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100903 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500904 return -EINVAL;
905
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200906 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500907 }
908
909 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500910}
911
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200912static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
913 const struct kvm_vcpu_init *init)
914{
Andrew Jones811328f2019-04-04 19:42:30 +0200915 unsigned int i, ret;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200916 int phys_target = kvm_target_cpu();
917
918 if (init->target != phys_target)
919 return -EINVAL;
920
921 /*
922 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
923 * use the same target.
924 */
925 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
926 return -EINVAL;
927
928 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
929 for (i = 0; i < sizeof(init->features) * 8; i++) {
930 bool set = (init->features[i / 32] & (1 << (i % 32)));
931
932 if (set && i >= KVM_VCPU_MAX_FEATURES)
933 return -ENOENT;
934
935 /*
936 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
937 * use the same feature set.
938 */
939 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
940 test_bit(i, vcpu->arch.features) != set)
941 return -EINVAL;
942
943 if (set)
944 set_bit(i, vcpu->arch.features);
945 }
946
947 vcpu->arch.target = phys_target;
948
949 /* Now we know what it is, we can reset it. */
Andrew Jones811328f2019-04-04 19:42:30 +0200950 ret = kvm_reset_vcpu(vcpu);
951 if (ret) {
952 vcpu->arch.target = -1;
953 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
954 }
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200955
Andrew Jones811328f2019-04-04 19:42:30 +0200956 return ret;
957}
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200958
Christoffer Dall478a8232013-11-19 17:43:19 -0800959static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
960 struct kvm_vcpu_init *init)
961{
962 int ret;
963
964 ret = kvm_vcpu_set_target(vcpu, init);
965 if (ret)
966 return ret;
967
Christoffer Dall957db102014-11-27 10:35:03 +0100968 /*
969 * Ensure a rebooted VM will fault in RAM pages and detect if the
970 * guest MMU is turned off and flush the caches as needed.
Zenghui Yu892713e2020-04-15 15:28:35 +0800971 *
Marc Zyngier7ae2f3d2020-05-30 17:22:19 +0100972 * S2FWB enforces all memory accesses to RAM being cacheable,
973 * ensuring that the data side is always coherent. We still
974 * need to invalidate the I-cache though, as FWB does *not*
975 * imply CTR_EL0.DIC.
Christoffer Dall957db102014-11-27 10:35:03 +0100976 */
Marc Zyngier7ae2f3d2020-05-30 17:22:19 +0100977 if (vcpu->arch.has_run_once) {
978 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
979 stage2_unmap_vm(vcpu->kvm);
980 else
981 __flush_icache_all();
982 }
Christoffer Dall957db102014-11-27 10:35:03 +0100983
Christoffer Dallb856a592014-10-16 17:21:16 +0200984 vcpu_reset_hcr(vcpu);
985
Christoffer Dall478a8232013-11-19 17:43:19 -0800986 /*
Eric Auger37815282015-09-25 23:41:14 +0200987 * Handle the "start in power-off" case.
Christoffer Dall478a8232013-11-19 17:43:19 -0800988 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100989 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Andrew Jones424c9892017-06-04 14:43:57 +0200990 vcpu_power_off(vcpu);
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +0200991 else
Eric Auger37815282015-09-25 23:41:14 +0200992 vcpu->arch.power_off = false;
Christoffer Dall478a8232013-11-19 17:43:19 -0800993
994 return 0;
995}
996
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800997static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
998 struct kvm_device_attr *attr)
999{
1000 int ret = -ENXIO;
1001
1002 switch (attr->group) {
1003 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001004 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001005 break;
1006 }
1007
1008 return ret;
1009}
1010
1011static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1012 struct kvm_device_attr *attr)
1013{
1014 int ret = -ENXIO;
1015
1016 switch (attr->group) {
1017 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001018 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001019 break;
1020 }
1021
1022 return ret;
1023}
1024
1025static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1026 struct kvm_device_attr *attr)
1027{
1028 int ret = -ENXIO;
1029
1030 switch (attr->group) {
1031 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001032 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001033 break;
1034 }
1035
1036 return ret;
1037}
1038
James Morse539aee02018-07-19 16:24:24 +01001039static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1040 struct kvm_vcpu_events *events)
1041{
1042 memset(events, 0, sizeof(*events));
1043
1044 return __kvm_arm_vcpu_get_events(vcpu, events);
1045}
1046
1047static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1048 struct kvm_vcpu_events *events)
1049{
1050 int i;
1051
1052 /* check whether the reserved field is zero */
1053 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1054 if (events->reserved[i])
1055 return -EINVAL;
1056
1057 /* check whether the pad field is zero */
1058 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1059 if (events->exception.pad[i])
1060 return -EINVAL;
1061
1062 return __kvm_arm_vcpu_set_events(vcpu, events);
1063}
James Morse539aee02018-07-19 16:24:24 +01001064
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001065long kvm_arch_vcpu_ioctl(struct file *filp,
1066 unsigned int ioctl, unsigned long arg)
1067{
1068 struct kvm_vcpu *vcpu = filp->private_data;
1069 void __user *argp = (void __user *)arg;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001070 struct kvm_device_attr attr;
Christoffer Dall9b0624712017-12-04 21:35:36 +01001071 long r;
1072
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001073 switch (ioctl) {
1074 case KVM_ARM_VCPU_INIT: {
1075 struct kvm_vcpu_init init;
1076
Christoffer Dall9b0624712017-12-04 21:35:36 +01001077 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001078 if (copy_from_user(&init, argp, sizeof(init)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001079 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001080
Christoffer Dall9b0624712017-12-04 21:35:36 +01001081 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1082 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001083 }
1084 case KVM_SET_ONE_REG:
1085 case KVM_GET_ONE_REG: {
1086 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001087
Christoffer Dall9b0624712017-12-04 21:35:36 +01001088 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001089 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001090 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001091
Christoffer Dall9b0624712017-12-04 21:35:36 +01001092 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001093 if (copy_from_user(&reg, argp, sizeof(reg)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001094 break;
1095
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001096 if (ioctl == KVM_SET_ONE_REG)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001097 r = kvm_arm_set_reg(vcpu, &reg);
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001098 else
Christoffer Dall9b0624712017-12-04 21:35:36 +01001099 r = kvm_arm_get_reg(vcpu, &reg);
1100 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001101 }
1102 case KVM_GET_REG_LIST: {
1103 struct kvm_reg_list __user *user_list = argp;
1104 struct kvm_reg_list reg_list;
1105 unsigned n;
1106
Christoffer Dall9b0624712017-12-04 21:35:36 +01001107 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001108 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001109 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001110
Dave Martin7dd32a02018-12-19 14:27:01 +00001111 r = -EPERM;
1112 if (!kvm_arm_vcpu_is_finalized(vcpu))
1113 break;
1114
Christoffer Dall9b0624712017-12-04 21:35:36 +01001115 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001116 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001117 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001118 n = reg_list.n;
1119 reg_list.n = kvm_arm_num_regs(vcpu);
1120 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001121 break;
1122 r = -E2BIG;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001123 if (n < reg_list.n)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001124 break;
1125 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1126 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001127 }
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001128 case KVM_SET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001129 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001130 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001131 break;
1132 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1133 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001134 }
1135 case KVM_GET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001136 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001137 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001138 break;
1139 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1140 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001141 }
1142 case KVM_HAS_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001143 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001144 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001145 break;
1146 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1147 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001148 }
Dongjiu Gengb7b27fa2018-07-19 16:24:22 +01001149 case KVM_GET_VCPU_EVENTS: {
1150 struct kvm_vcpu_events events;
1151
1152 if (kvm_arm_vcpu_get_events(vcpu, &events))
1153 return -EINVAL;
1154
1155 if (copy_to_user(argp, &events, sizeof(events)))
1156 return -EFAULT;
1157
1158 return 0;
1159 }
1160 case KVM_SET_VCPU_EVENTS: {
1161 struct kvm_vcpu_events events;
1162
1163 if (copy_from_user(&events, argp, sizeof(events)))
1164 return -EFAULT;
1165
1166 return kvm_arm_vcpu_set_events(vcpu, &events);
1167 }
Dave Martin7dd32a02018-12-19 14:27:01 +00001168 case KVM_ARM_VCPU_FINALIZE: {
1169 int what;
1170
1171 if (!kvm_vcpu_initialized(vcpu))
1172 return -ENOEXEC;
1173
1174 if (get_user(what, (const int __user *)argp))
1175 return -EFAULT;
1176
1177 return kvm_arm_vcpu_finalize(vcpu, what);
1178 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001179 default:
Christoffer Dall9b0624712017-12-04 21:35:36 +01001180 r = -EINVAL;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001181 }
Christoffer Dall9b0624712017-12-04 21:35:36 +01001182
Christoffer Dall9b0624712017-12-04 21:35:36 +01001183 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001184}
1185
Sean Christopherson0dff0842020-02-18 13:07:29 -08001186void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001187{
Mario Smarduch53c810c2015-01-15 15:58:57 -08001188
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001189}
1190
Sean Christopherson0dff0842020-02-18 13:07:29 -08001191void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
1192 struct kvm_memory_slot *memslot)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001193{
Sean Christopherson0dff0842020-02-18 13:07:29 -08001194 kvm_flush_remote_tlbs(kvm);
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001195}
1196
Christoffer Dall3401d5462013-01-23 13:18:04 -05001197static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1198 struct kvm_arm_device_addr *dev_addr)
1199{
Christoffer Dall330690c2013-01-21 19:36:13 -05001200 unsigned long dev_id, type;
1201
1202 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1203 KVM_ARM_DEVICE_ID_SHIFT;
1204 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1205 KVM_ARM_DEVICE_TYPE_SHIFT;
1206
1207 switch (dev_id) {
1208 case KVM_ARM_DEVICE_VGIC_V2:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001209 if (!vgic_present)
1210 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001211 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -05001212 default:
1213 return -ENODEV;
1214 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001215}
1216
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001217long kvm_arch_vm_ioctl(struct file *filp,
1218 unsigned int ioctl, unsigned long arg)
1219{
Christoffer Dall3401d5462013-01-23 13:18:04 -05001220 struct kvm *kvm = filp->private_data;
1221 void __user *argp = (void __user *)arg;
1222
1223 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001224 case KVM_CREATE_IRQCHIP: {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001225 int ret;
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001226 if (!vgic_present)
1227 return -ENXIO;
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001228 mutex_lock(&kvm->lock);
1229 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1230 mutex_unlock(&kvm->lock);
1231 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001232 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001233 case KVM_ARM_SET_DEVICE_ADDR: {
1234 struct kvm_arm_device_addr dev_addr;
1235
1236 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1237 return -EFAULT;
1238 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1239 }
Anup Patel42c4e0c2013-09-30 14:20:07 +05301240 case KVM_ARM_PREFERRED_TARGET: {
1241 int err;
1242 struct kvm_vcpu_init init;
1243
1244 err = kvm_vcpu_preferred_target(&init);
1245 if (err)
1246 return err;
1247
1248 if (copy_to_user(argp, &init, sizeof(init)))
1249 return -EFAULT;
1250
1251 return 0;
1252 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001253 default:
1254 return -EINVAL;
1255 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001256}
1257
Miaohe Lin7e0befd2019-11-21 15:15:59 +08001258static void cpu_init_hyp_mode(void)
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001259{
Marc Zyngierdac288f2013-05-14 12:11:37 +01001260 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001261 unsigned long hyp_stack_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001262 unsigned long vector_ptr;
David Brazdil71b3ec52020-05-15 16:20:56 +01001263 unsigned long tpidr_el2;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001264
1265 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001266 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001267
David Brazdil71b3ec52020-05-15 16:20:56 +01001268 /*
1269 * Calculate the raw per-cpu offset without a translation from the
1270 * kernel's mapping to the linear mapping, and store it in tpidr_el2
1271 * so that we can use adr_l to access per-cpu variables in EL2.
1272 */
1273 tpidr_el2 = ((unsigned long)this_cpu_ptr(&kvm_host_data) -
David Brazdilb877e982020-06-25 14:14:11 +01001274 (unsigned long)kvm_ksym_ref(&kvm_host_data));
David Brazdil71b3ec52020-05-15 16:20:56 +01001275
Marc Zyngierdac288f2013-05-14 12:11:37 +01001276 pgd_ptr = kvm_mmu_get_httbr();
David Brazdil71b3ec52020-05-15 16:20:56 +01001277 hyp_stack_ptr = __this_cpu_read(kvm_arm_hyp_stack_page) + PAGE_SIZE;
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001278 vector_ptr = (unsigned long)kvm_get_hyp_vector();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001279
David Brazdil71b3ec52020-05-15 16:20:56 +01001280 /*
1281 * Call initialization code, and switch to the full blown HYP code.
1282 * If the cpucaps haven't been finalized yet, something has gone very
1283 * wrong, and hyp will crash and burn when it uses any
1284 * cpus_have_const_cap() wrapper.
1285 */
1286 BUG_ON(!system_capabilities_finalized());
1287 __kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr, tpidr_el2);
1288
1289 /*
1290 * Disabling SSBD on a non-VHE system requires us to enable SSBS
1291 * at EL2.
1292 */
1293 if (this_cpu_has_cap(ARM64_SSBS) &&
1294 arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) {
David Brazdil13aeb9b2020-06-25 14:14:16 +01001295 kvm_call_hyp_nvhe(__kvm_enable_ssbs);
David Brazdil71b3ec52020-05-15 16:20:56 +01001296 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001297}
1298
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001299static void cpu_hyp_reset(void)
1300{
1301 if (!is_kernel_in_hyp_mode())
1302 __hyp_reset_vectors();
1303}
1304
James Morse5f5560b2016-03-30 18:33:04 +01001305static void cpu_hyp_reinit(void)
1306{
Marc Zyngier1e0cf162019-07-05 23:35:56 +01001307 kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt);
1308
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001309 cpu_hyp_reset();
1310
Marc Zyngier9d47bb02018-10-01 13:41:32 +01001311 if (is_kernel_in_hyp_mode())
Hu Huajun02d50cd2017-06-12 22:37:48 +08001312 kvm_timer_init_vhe();
Marc Zyngier9d47bb02018-10-01 13:41:32 +01001313 else
Miaohe Lin7e0befd2019-11-21 15:15:59 +08001314 cpu_init_hyp_mode();
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001315
Mark Rutlandda5a3ce2018-10-17 17:42:10 +01001316 kvm_arm_init_debug();
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001317
1318 if (vgic_present)
1319 kvm_vgic_init_cpu_hardware();
James Morse5f5560b2016-03-30 18:33:04 +01001320}
1321
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001322static void _kvm_arch_hardware_enable(void *discard)
1323{
1324 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1325 cpu_hyp_reinit();
1326 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1327 }
1328}
1329
1330int kvm_arch_hardware_enable(void)
1331{
1332 _kvm_arch_hardware_enable(NULL);
1333 return 0;
1334}
1335
1336static void _kvm_arch_hardware_disable(void *discard)
1337{
1338 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1339 cpu_hyp_reset();
1340 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1341 }
1342}
1343
1344void kvm_arch_hardware_disable(void)
1345{
1346 _kvm_arch_hardware_disable(NULL);
1347}
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001348
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001349#ifdef CONFIG_CPU_PM
1350static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1351 unsigned long cmd,
1352 void *v)
1353{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001354 /*
1355 * kvm_arm_hardware_enabled is left with its old value over
1356 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1357 * re-enable hyp.
1358 */
1359 switch (cmd) {
1360 case CPU_PM_ENTER:
1361 if (__this_cpu_read(kvm_arm_hardware_enabled))
1362 /*
1363 * don't update kvm_arm_hardware_enabled here
1364 * so that the hardware will be re-enabled
1365 * when we resume. See below.
1366 */
1367 cpu_hyp_reset();
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001368
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001369 return NOTIFY_OK;
James Morse58d6b152018-01-22 18:19:06 +00001370 case CPU_PM_ENTER_FAILED:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001371 case CPU_PM_EXIT:
1372 if (__this_cpu_read(kvm_arm_hardware_enabled))
1373 /* The hardware was enabled before suspend. */
1374 cpu_hyp_reinit();
1375
1376 return NOTIFY_OK;
1377
1378 default:
1379 return NOTIFY_DONE;
1380 }
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001381}
1382
1383static struct notifier_block hyp_init_cpu_pm_nb = {
1384 .notifier_call = hyp_init_cpu_pm_notifier,
1385};
1386
1387static void __init hyp_cpu_pm_init(void)
1388{
1389 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1390}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001391static void __init hyp_cpu_pm_exit(void)
1392{
1393 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1394}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001395#else
1396static inline void hyp_cpu_pm_init(void)
1397{
1398}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001399static inline void hyp_cpu_pm_exit(void)
1400{
1401}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001402#endif
1403
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001404static int init_common_resources(void)
1405{
Marc Zyngierb130a8f2020-05-28 14:12:58 +01001406 return kvm_set_ipa_limit();
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001407}
1408
1409static int init_subsystems(void)
1410{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001411 int err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001412
1413 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001414 * Enable hardware so that subsystem initialisation can access EL2.
James Morse5f5560b2016-03-30 18:33:04 +01001415 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001416 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
James Morse5f5560b2016-03-30 18:33:04 +01001417
1418 /*
1419 * Register CPU lower-power notifier
1420 */
1421 hyp_cpu_pm_init();
1422
1423 /*
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001424 * Init HYP view of VGIC
1425 */
1426 err = kvm_vgic_hyp_init();
1427 switch (err) {
1428 case 0:
1429 vgic_present = true;
1430 break;
1431 case -ENODEV:
1432 case -ENXIO:
1433 vgic_present = false;
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001434 err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001435 break;
1436 default:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001437 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001438 }
1439
1440 /*
1441 * Init HYP architected timer support
1442 */
Marc Zyngierf384dcf2017-12-07 11:46:15 +00001443 err = kvm_timer_hyp_init(vgic_present);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001444 if (err)
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001445 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001446
1447 kvm_perf_init();
1448 kvm_coproc_table_init();
1449
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001450out:
1451 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1452
1453 return err;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001454}
1455
1456static void teardown_hyp_mode(void)
1457{
1458 int cpu;
1459
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001460 free_hyp_pgds();
1461 for_each_possible_cpu(cpu)
1462 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1463}
1464
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001465/**
1466 * Inits Hyp-mode on all online CPUs
1467 */
1468static int init_hyp_mode(void)
1469{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001470 int cpu;
1471 int err = 0;
1472
1473 /*
1474 * Allocate Hyp PGD and setup Hyp identity mapping
1475 */
1476 err = kvm_mmu_init();
1477 if (err)
1478 goto out_err;
1479
1480 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001481 * Allocate stack pages for Hypervisor-mode
1482 */
1483 for_each_possible_cpu(cpu) {
1484 unsigned long stack_page;
1485
1486 stack_page = __get_free_page(GFP_KERNEL);
1487 if (!stack_page) {
1488 err = -ENOMEM;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001489 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001490 }
1491
1492 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1493 }
1494
1495 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001496 * Map the Hyp-code called directly from the host
1497 */
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001498 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001499 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001500 if (err) {
1501 kvm_err("Cannot map world-switch code\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001502 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001503 }
1504
Ard Biesheuvela0bf9772016-02-16 13:52:39 +01001505 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
Marc Zyngier74a6b882016-06-13 15:00:47 +01001506 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
Marc Zyngier910917b2015-10-27 12:18:48 +00001507 if (err) {
1508 kvm_err("Cannot map rodata section\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001509 goto out_err;
Marc Zyngier910917b2015-10-27 12:18:48 +00001510 }
1511
Marc Zyngierc8ea0392016-10-20 10:17:21 +01001512 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1513 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1514 if (err) {
1515 kvm_err("Cannot map bss section\n");
1516 goto out_err;
1517 }
1518
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001519 err = kvm_map_vectors();
1520 if (err) {
1521 kvm_err("Cannot map vectors\n");
1522 goto out_err;
1523 }
1524
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001525 /*
1526 * Map the Hyp stack pages
1527 */
1528 for_each_possible_cpu(cpu) {
1529 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001530 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1531 PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001532
1533 if (err) {
1534 kvm_err("Cannot map hyp stack\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001535 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001536 }
1537 }
1538
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001539 for_each_possible_cpu(cpu) {
Andrew Murray630a1682019-04-09 20:22:11 +01001540 kvm_host_data_t *cpu_data;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001541
Andrew Murray630a1682019-04-09 20:22:11 +01001542 cpu_data = per_cpu_ptr(&kvm_host_data, cpu);
Andrew Murray630a1682019-04-09 20:22:11 +01001543 err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001544
1545 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001546 kvm_err("Cannot map host CPU state: %d\n", err);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001547 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001548 }
1549 }
1550
Marc Zyngier55e37482018-05-29 13:11:16 +01001551 err = hyp_map_aux_data();
1552 if (err)
Colin Ian Kinga37f0c32019-02-17 22:36:26 +00001553 kvm_err("Cannot map host auxiliary data: %d\n", err);
Marc Zyngier55e37482018-05-29 13:11:16 +01001554
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001555 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001556
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001557out_err:
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001558 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001559 kvm_err("error initializing Hyp mode: %d\n", err);
1560 return err;
1561}
1562
Andre Przywarad4e071c2013-04-17 12:52:01 +02001563static void check_kvm_target_cpu(void *ret)
1564{
1565 *(int *)ret = kvm_target_cpu();
1566}
1567
Andre Przywara4429fc62014-06-02 15:37:13 +02001568struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1569{
1570 struct kvm_vcpu *vcpu;
1571 int i;
1572
1573 mpidr &= MPIDR_HWID_BITMASK;
1574 kvm_for_each_vcpu(i, vcpu, kvm) {
1575 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1576 return vcpu;
1577 }
1578 return NULL;
1579}
1580
Eric Auger24124052017-10-27 15:28:31 +01001581bool kvm_arch_has_irq_bypass(void)
1582{
1583 return true;
1584}
1585
1586int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
1587 struct irq_bypass_producer *prod)
1588{
1589 struct kvm_kernel_irqfd *irqfd =
1590 container_of(cons, struct kvm_kernel_irqfd, consumer);
1591
Marc Zyngier196b1362017-10-27 15:28:39 +01001592 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
1593 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001594}
1595void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
1596 struct irq_bypass_producer *prod)
1597{
1598 struct kvm_kernel_irqfd *irqfd =
1599 container_of(cons, struct kvm_kernel_irqfd, consumer);
1600
Marc Zyngier196b1362017-10-27 15:28:39 +01001601 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
1602 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001603}
1604
1605void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
1606{
1607 struct kvm_kernel_irqfd *irqfd =
1608 container_of(cons, struct kvm_kernel_irqfd, consumer);
1609
1610 kvm_arm_halt_guest(irqfd->kvm);
1611}
1612
1613void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
1614{
1615 struct kvm_kernel_irqfd *irqfd =
1616 container_of(cons, struct kvm_kernel_irqfd, consumer);
1617
1618 kvm_arm_resume_guest(irqfd->kvm);
1619}
1620
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001621/**
1622 * Initialize Hyp-mode and memory mappings on all CPUs.
1623 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001624int kvm_arch_init(void *opaque)
1625{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001626 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001627 int ret, cpu;
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001628 bool in_hyp_mode;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001629
1630 if (!is_hyp_mode_available()) {
Ard Biesheuvel58d0d192017-11-28 15:18:19 +00001631 kvm_info("HYP mode not available\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001632 return -ENODEV;
1633 }
1634
Marc Zyngier33e5f4e2018-12-06 17:31:20 +00001635 in_hyp_mode = is_kernel_in_hyp_mode();
1636
1637 if (!in_hyp_mode && kvm_arch_requires_vhe()) {
1638 kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n");
Dave Martin85acda32018-04-20 16:20:43 +01001639 return -ENODEV;
1640 }
1641
Andre Przywarad4e071c2013-04-17 12:52:01 +02001642 for_each_online_cpu(cpu) {
1643 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1644 if (ret < 0) {
1645 kvm_err("Error, CPU %d not supported!\n", cpu);
1646 return -ENODEV;
1647 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001648 }
1649
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001650 err = init_common_resources();
1651 if (err)
1652 return err;
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301653
Dave Martina3be8362019-04-12 15:30:58 +01001654 err = kvm_arm_init_sve();
Dave Martin0f062bf2019-02-28 18:33:00 +00001655 if (err)
1656 return err;
1657
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001658 if (!in_hyp_mode) {
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001659 err = init_hyp_mode();
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001660 if (err)
1661 goto out_err;
1662 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001663
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001664 err = init_subsystems();
1665 if (err)
1666 goto out_hyp;
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001667
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001668 if (in_hyp_mode)
1669 kvm_info("VHE mode initialized successfully\n");
1670 else
1671 kvm_info("Hyp mode initialized successfully\n");
1672
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001673 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001674
1675out_hyp:
Shannon Zhaoc3e35402019-12-02 15:42:11 +08001676 hyp_cpu_pm_exit();
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001677 if (!in_hyp_mode)
1678 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001679out_err:
1680 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001681}
1682
1683/* NOP: Compiling as a module not supported */
1684void kvm_arch_exit(void)
1685{
Marc Zyngier210552c2013-03-05 03:18:00 +00001686 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001687}
1688
1689static int arm_init(void)
1690{
1691 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1692 return rc;
1693}
1694
1695module_init(arm_init);