blob: ce7c6f36471b139400d14d23e63ff7bd7dd4e4d2 [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
Dave Martin85acda32018-04-20 16:20:43 +010019#include <linux/bug.h>
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +010020#include <linux/cpu_pm.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050021#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/kvm_host.h>
Andre Przywara1085fdc2016-07-15 12:43:31 +010024#include <linux/list.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050025#include <linux/module.h>
26#include <linux/vmalloc.h>
27#include <linux/fs.h>
28#include <linux/mman.h>
29#include <linux/sched.h>
Christoffer Dall86ce8532013-01-20 18:28:08 -050030#include <linux/kvm.h>
Eric Auger24124052017-10-27 15:28:31 +010031#include <linux/kvm_irqfd.h>
32#include <linux/irqbypass.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050033#include <trace/events/kvm.h>
Shannon Zhaob02386e2016-02-26 19:29:19 +080034#include <kvm/arm_pmu.h>
Marc Zyngier1a2fb942018-02-06 17:56:08 +000035#include <kvm/arm_psci.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050036
37#define CREATE_TRACE_POINTS
38#include "trace.h"
39
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050041#include <asm/ptrace.h>
42#include <asm/mman.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050043#include <asm/tlbflush.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050044#include <asm/cacheflush.h>
Dave Martin85acda32018-04-20 16:20:43 +010045#include <asm/cpufeature.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050046#include <asm/virt.h>
47#include <asm/kvm_arm.h>
48#include <asm/kvm_asm.h>
49#include <asm/kvm_mmu.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050050#include <asm/kvm_emulate.h>
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050051#include <asm/kvm_coproc.h>
Marc Zyngier910917b2015-10-27 12:18:48 +000052#include <asm/sections.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050053
54#ifdef REQUIRES_VIRT
55__asm__(".arch_extension virt");
56#endif
57
James Morse36989e72018-01-08 15:38:04 +000058DEFINE_PER_CPU(kvm_cpu_context_t, kvm_host_cpu_state);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050059static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050060
Marc Zyngier1638a122013-01-21 19:36:11 -050061/* Per-CPU variable containing the currently running vcpu. */
62static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
63
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050064/* The VMID used in the VTTBR */
65static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
Vladimir Murzin20475f72015-11-16 11:28:18 +000066static u32 kvm_next_vmid;
67static unsigned int kvm_vmid_bits __read_mostly;
Marc Zyngierf0cf47d2018-04-04 14:48:24 +010068static DEFINE_RWLOCK(kvm_vmid_lock);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050069
Pavel Fedinc7da6fa2015-12-18 14:38:43 +030070static bool vgic_present;
71
AKASHI Takahiro67f69192016-04-27 17:47:05 +010072static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
73
Marc Zyngier1638a122013-01-21 19:36:11 -050074static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
75{
Christoph Lameter1436c1a2013-10-21 13:17:08 +010076 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050077}
78
Christoffer Dall61bbe382017-10-27 19:57:51 +020079DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
80
Marc Zyngier1638a122013-01-21 19:36:11 -050081/**
82 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
83 * Must be called from non-preemptible context
84 */
85struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
86{
Christoph Lameter1436c1a2013-10-21 13:17:08 +010087 return __this_cpu_read(kvm_arm_running_vcpu);
Marc Zyngier1638a122013-01-21 19:36:11 -050088}
89
90/**
91 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
92 */
Will Deacon4000be42014-08-26 15:13:21 +010093struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
Marc Zyngier1638a122013-01-21 19:36:11 -050094{
95 return &kvm_arm_running_vcpu;
96}
97
Christoffer Dall749cf76c2013-01-20 18:28:06 -050098int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
99{
100 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
101}
102
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500103int kvm_arch_hardware_setup(void)
104{
105 return 0;
106}
107
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500108void kvm_arch_check_processor_compat(void *rtn)
109{
110 *(int *)rtn = 0;
111}
112
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500113
Christoffer Dalld5d81842013-01-20 18:28:07 -0500114/**
115 * kvm_arch_init_vm - initializes a VM data structure
116 * @kvm: pointer to the KVM struct
117 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500118int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
119{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100120 int ret, cpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500121
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500122 if (type)
123 return -EINVAL;
124
Marc Zyngier94d0e592016-10-18 18:37:49 +0100125 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
126 if (!kvm->arch.last_vcpu_ran)
127 return -ENOMEM;
128
129 for_each_possible_cpu(cpu)
130 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
131
Christoffer Dalld5d81842013-01-20 18:28:07 -0500132 ret = kvm_alloc_stage2_pgd(kvm);
133 if (ret)
134 goto out_fail_alloc;
135
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100136 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500137 if (ret)
138 goto out_free_stage2_pgd;
139
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100140 kvm_vgic_early_init(kvm);
Christoffer Dalla1a64382013-11-16 10:51:25 -0800141
Christoffer Dalld5d81842013-01-20 18:28:07 -0500142 /* Mark the initial VMID generation invalid */
143 kvm->arch.vmid_gen = 0;
144
Andre Przywara3caa2d82014-06-02 16:26:01 +0200145 /* The maximum number of VCPUs is limited by the host's GIC model */
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300146 kvm->arch.max_vcpus = vgic_present ?
147 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
Andre Przywara3caa2d82014-06-02 16:26:01 +0200148
Christoffer Dalld5d81842013-01-20 18:28:07 -0500149 return ret;
150out_free_stage2_pgd:
151 kvm_free_stage2_pgd(kvm);
152out_fail_alloc:
Marc Zyngier94d0e592016-10-18 18:37:49 +0100153 free_percpu(kvm->arch.last_vcpu_ran);
154 kvm->arch.last_vcpu_ran = NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500155 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500156}
157
Luiz Capitulino235539b2016-09-07 14:47:23 -0400158bool kvm_arch_has_vcpu_debugfs(void)
159{
160 return false;
161}
162
163int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
164{
165 return 0;
166}
167
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500168int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
169{
170 return VM_FAULT_SIGBUS;
171}
172
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500173
Christoffer Dalld5d81842013-01-20 18:28:07 -0500174/**
175 * kvm_arch_destroy_vm - destroy the VM data structure
176 * @kvm: pointer to the KVM struct
177 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500178void kvm_arch_destroy_vm(struct kvm *kvm)
179{
180 int i;
181
Marc Zyngierb2c9a852017-10-27 15:28:34 +0100182 kvm_vgic_destroy(kvm);
183
Marc Zyngier94d0e592016-10-18 18:37:49 +0100184 free_percpu(kvm->arch.last_vcpu_ran);
185 kvm->arch.last_vcpu_ran = NULL;
186
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500187 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
188 if (kvm->vcpus[i]) {
189 kvm_arch_vcpu_free(kvm->vcpus[i]);
190 kvm->vcpus[i] = NULL;
191 }
192 }
Andrew Jones6b2ad812017-11-27 19:17:18 +0100193 atomic_set(&kvm->online_vcpus, 0);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500194}
195
Alexander Graf784aa3d2014-07-14 18:27:35 +0200196int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500197{
198 int r;
199 switch (ext) {
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500200 case KVM_CAP_IRQCHIP:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +0300201 r = vgic_present;
202 break;
Nikolay Nikolaevd44758c2015-01-24 12:00:02 +0000203 case KVM_CAP_IOEVENTFD:
Christoffer Dall73306722013-10-25 17:29:18 +0100204 case KVM_CAP_DEVICE_CTRL:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500205 case KVM_CAP_USER_MEMORY:
206 case KVM_CAP_SYNC_MMU:
207 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
208 case KVM_CAP_ONE_REG:
Marc Zyngieraa024c22013-01-20 18:28:13 -0500209 case KVM_CAP_ARM_PSCI:
Anup Patel4447a202014-04-29 11:24:25 +0530210 case KVM_CAP_ARM_PSCI_0_2:
Christoffer Dall98047882014-08-19 12:18:04 +0200211 case KVM_CAP_READONLY_MEM:
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000212 case KVM_CAP_MP_STATE:
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100213 case KVM_CAP_IMMEDIATE_EXIT:
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500214 r = 1;
215 break;
Christoffer Dall3401d5462013-01-23 13:18:04 -0500216 case KVM_CAP_ARM_SET_DEVICE_ADDR:
217 r = 1;
Marc Zyngierca46e102013-04-03 10:43:13 +0100218 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500219 case KVM_CAP_NR_VCPUS:
220 r = num_online_cpus();
221 break;
222 case KVM_CAP_MAX_VCPUS:
223 r = KVM_MAX_VCPUS;
224 break;
Linu Cherian7af4df82017-03-08 11:38:33 +0530225 case KVM_CAP_NR_MEMSLOTS:
226 r = KVM_USER_MEM_SLOTS;
227 break;
Vladimir Murzin29885092016-11-02 11:55:34 +0000228 case KVM_CAP_MSI_DEVID:
229 if (!kvm)
230 r = -EINVAL;
231 else
232 r = kvm->arch.vgic.msis_require_devid;
233 break;
Christoffer Dallf7214e62017-02-01 12:54:11 +0100234 case KVM_CAP_ARM_USER_IRQ:
235 /*
236 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
237 * (bump this number if adding more devices)
238 */
239 r = 1;
240 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500241 default:
Andre Przywarab46f01c2016-07-15 12:43:25 +0100242 r = kvm_arch_dev_ioctl_check_extension(kvm, ext);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500243 break;
244 }
245 return r;
246}
247
248long kvm_arch_dev_ioctl(struct file *filp,
249 unsigned int ioctl, unsigned long arg)
250{
251 return -EINVAL;
252}
253
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500254
255struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
256{
257 int err;
258 struct kvm_vcpu *vcpu;
259
Christoffer Dall716139d2014-12-09 14:33:45 +0100260 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
261 err = -EBUSY;
262 goto out;
263 }
264
Andre Przywara3caa2d82014-06-02 16:26:01 +0200265 if (id >= kvm->arch.max_vcpus) {
266 err = -EINVAL;
267 goto out;
268 }
269
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500270 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
271 if (!vcpu) {
272 err = -ENOMEM;
273 goto out;
274 }
275
276 err = kvm_vcpu_init(vcpu, kvm, id);
277 if (err)
278 goto free_vcpu;
279
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100280 err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500281 if (err)
282 goto vcpu_uninit;
283
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500284 return vcpu;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500285vcpu_uninit:
286 kvm_vcpu_uninit(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500287free_vcpu:
288 kmem_cache_free(kvm_vcpu_cache, vcpu);
289out:
290 return ERR_PTR(err);
291}
292
Dominik Dingel31928aa2014-12-04 15:47:07 +0100293void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500294{
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100295 kvm_vgic_vcpu_early_init(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500296}
297
298void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
299{
Christoffer Dallf1d72312018-01-25 18:32:29 +0100300 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
301 static_branch_dec(&userspace_irqchip_in_use);
302
Christoffer Dalld5d81842013-01-20 18:28:07 -0500303 kvm_mmu_free_memory_caches(vcpu);
Marc Zyngier967f8422013-01-23 13:21:59 -0500304 kvm_timer_vcpu_terminate(vcpu);
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800305 kvm_pmu_vcpu_destroy(vcpu);
James Morse591d2152016-06-08 17:24:45 +0100306 kvm_vcpu_uninit(vcpu);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500307 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500308}
309
310void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
311{
312 kvm_arch_vcpu_free(vcpu);
313}
314
315int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
316{
Christoffer Dall1c88ab72017-01-06 16:07:48 +0100317 return kvm_timer_is_pending(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500318}
319
Christoffer Dalld35268d2015-08-25 19:48:21 +0200320void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
321{
322 kvm_timer_schedule(vcpu);
Marc Zyngierdf9ba952017-10-27 15:28:49 +0100323 kvm_vgic_v4_enable_doorbell(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200324}
325
326void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
327{
328 kvm_timer_unschedule(vcpu);
Marc Zyngierdf9ba952017-10-27 15:28:49 +0100329 kvm_vgic_v4_disable_doorbell(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200330}
331
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500332int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
333{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500334 /* Force users to call KVM_ARM_VCPU_INIT */
335 vcpu->arch.target = -1;
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200336 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500337
Marc Zyngier967f8422013-01-23 13:21:59 -0500338 /* Set up the timer */
339 kvm_timer_vcpu_init(vcpu);
340
Alex Bennée84e690b2015-07-07 17:30:00 +0100341 kvm_arm_reset_debug_ptr(vcpu);
342
Christoffer Dall1aab6f42017-05-08 12:30:24 +0200343 return kvm_vgic_vcpu_init(vcpu);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500344}
345
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500346void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
347{
Marc Zyngier94d0e592016-10-18 18:37:49 +0100348 int *last_ran;
349
350 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
351
352 /*
353 * We might get preempted before the vCPU actually runs, but
354 * over-invalidation doesn't affect correctness.
355 */
356 if (*last_ran != vcpu->vcpu_id) {
357 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
358 *last_ran = vcpu->vcpu_id;
359 }
360
Christoffer Dall86ce8532013-01-20 18:28:08 -0500361 vcpu->cpu = cpu;
James Morse36989e72018-01-08 15:38:04 +0000362 vcpu->arch.host_cpu_context = this_cpu_ptr(&kvm_host_cpu_state);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500363
Marc Zyngier1638a122013-01-21 19:36:11 -0500364 kvm_arm_set_running_vcpu(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100365 kvm_vgic_load(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200366 kvm_timer_vcpu_load(vcpu);
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200367 kvm_vcpu_load_sysregs(vcpu);
Dave Martine6b673b2018-04-06 14:55:59 +0100368 kvm_arch_vcpu_load_fp(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);
377
Christoffer Dalle9b152c2013-12-11 20:29:11 -0800378 vcpu->cpu = -1;
379
Marc Zyngier1638a122013-01-21 19:36:11 -0500380 kvm_arm_set_running_vcpu(NULL);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500381}
382
Andrew Jones424c9892017-06-04 14:43:57 +0200383static void vcpu_power_off(struct kvm_vcpu *vcpu)
384{
385 vcpu->arch.power_off = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200386 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones424c9892017-06-04 14:43:57 +0200387 kvm_vcpu_kick(vcpu);
388}
389
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500390int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
391 struct kvm_mp_state *mp_state)
392{
Eric Auger37815282015-09-25 23:41:14 +0200393 if (vcpu->arch.power_off)
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000394 mp_state->mp_state = KVM_MP_STATE_STOPPED;
395 else
396 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
397
398 return 0;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500399}
400
401int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
402 struct kvm_mp_state *mp_state)
403{
Christoffer Dalle83dff52017-12-04 21:35:31 +0100404 int ret = 0;
405
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000406 switch (mp_state->mp_state) {
407 case KVM_MP_STATE_RUNNABLE:
Eric Auger37815282015-09-25 23:41:14 +0200408 vcpu->arch.power_off = false;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000409 break;
410 case KVM_MP_STATE_STOPPED:
Andrew Jones424c9892017-06-04 14:43:57 +0200411 vcpu_power_off(vcpu);
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000412 break;
413 default:
Christoffer Dalle83dff52017-12-04 21:35:31 +0100414 ret = -EINVAL;
Alex Bennéeecccf0c2015-03-13 17:02:52 +0000415 }
416
Christoffer Dalle83dff52017-12-04 21:35:31 +0100417 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500418}
419
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500420/**
421 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
422 * @v: The VCPU pointer
423 *
424 * If the guest CPU is not waiting for interrupts or an interrupt line is
425 * asserted, the CPU is by definition runnable.
426 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500427int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
428{
Christoffer Dall3df59d82017-08-03 12:09:05 +0200429 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
430 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
Eric Auger3b928302015-09-25 23:41:17 +0200431 && !v->arch.power_off && !v->arch.pause);
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500432}
433
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800434bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
435{
Longpeng(Mike)f01fbd22017-08-08 12:05:35 +0800436 return vcpu_mode_priv(vcpu);
Longpeng(Mike)199b5762017-08-08 12:05:32 +0800437}
438
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500439/* Just ensure a guest exit from a particular CPU */
440static void exit_vm_noop(void *info)
441{
442}
443
444void force_vm_exit(const cpumask_t *mask)
445{
Eric Auger898f9492016-03-07 23:50:36 +0700446 preempt_disable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500447 smp_call_function_many(mask, exit_vm_noop, NULL, true);
Eric Auger898f9492016-03-07 23:50:36 +0700448 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500449}
450
451/**
452 * need_new_vmid_gen - check that the VMID is still valid
Andrea Gelmini6a727b02016-05-21 13:48:35 +0200453 * @kvm: The VM's VMID to check
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500454 *
455 * return true if there is a new generation of VMIDs being used
456 *
457 * The hardware supports only 256 values with the value zero reserved for the
458 * host, so we check if an assigned value belongs to a previous generation,
459 * which which requires us to assign a new value. If we're the first to use a
460 * VMID for the new generation, we must flush necessary caches and TLBs on all
461 * CPUs.
462 */
463static bool need_new_vmid_gen(struct kvm *kvm)
464{
465 return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen));
466}
467
468/**
469 * update_vttbr - Update the VTTBR with a valid VMID before the guest runs
470 * @kvm The guest that we are about to run
471 *
472 * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
473 * VM has a valid VMID, otherwise assigns a new one and flushes corresponding
474 * caches and TLBs.
475 */
476static void update_vttbr(struct kvm *kvm)
477{
478 phys_addr_t pgd_phys;
479 u64 vmid;
Marc Zyngierf0cf47d2018-04-04 14:48:24 +0100480 bool new_gen;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500481
Marc Zyngierf0cf47d2018-04-04 14:48:24 +0100482 read_lock(&kvm_vmid_lock);
483 new_gen = need_new_vmid_gen(kvm);
484 read_unlock(&kvm_vmid_lock);
485
486 if (!new_gen)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500487 return;
488
Marc Zyngierf0cf47d2018-04-04 14:48:24 +0100489 write_lock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500490
491 /*
492 * We need to re-check the vmid_gen here to ensure that if another vcpu
493 * already allocated a valid vmid for this vm, then this vcpu should
494 * use the same vmid.
495 */
496 if (!need_new_vmid_gen(kvm)) {
Marc Zyngierf0cf47d2018-04-04 14:48:24 +0100497 write_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500498 return;
499 }
500
501 /* First user of a new VMID generation? */
502 if (unlikely(kvm_next_vmid == 0)) {
503 atomic64_inc(&kvm_vmid_gen);
504 kvm_next_vmid = 1;
505
506 /*
507 * On SMP we know no other CPUs can use this CPU's or each
508 * other's VMID after force_vm_exit returns since the
509 * kvm_vmid_lock blocks them from reentry to the guest.
510 */
511 force_vm_exit(cpu_all_mask);
512 /*
513 * Now broadcast TLB + ICACHE invalidation over the inner
514 * shareable domain to make sure all data structures are
515 * clean.
516 */
517 kvm_call_hyp(__kvm_flush_vm_context);
518 }
519
520 kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen);
521 kvm->arch.vmid = kvm_next_vmid;
522 kvm_next_vmid++;
Vladimir Murzin20475f72015-11-16 11:28:18 +0000523 kvm_next_vmid &= (1 << kvm_vmid_bits) - 1;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500524
525 /* update vttbr to be used with the new vmid */
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000526 pgd_phys = virt_to_phys(kvm->arch.pgd);
Joel Schoppdbff1242014-07-09 11:17:04 -0500527 BUG_ON(pgd_phys & ~VTTBR_BADDR_MASK);
Vladimir Murzin20475f72015-11-16 11:28:18 +0000528 vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits);
Kristina Martsenko529c4b02017-12-13 17:07:18 +0000529 kvm->arch.vttbr = kvm_phys_to_vttbr(pgd_phys) | vmid;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500530
Marc Zyngierf0cf47d2018-04-04 14:48:24 +0100531 write_unlock(&kvm_vmid_lock);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500532}
533
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500534static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
535{
Christoffer Dall05971122014-12-12 21:19:23 +0100536 struct kvm *kvm = vcpu->kvm;
Christoffer Dall41a54482016-05-18 16:26:00 +0100537 int ret = 0;
Christoffer Dalle1ba0202013-09-23 14:55:55 -0700538
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500539 if (likely(vcpu->arch.has_run_once))
540 return 0;
541
542 vcpu->arch.has_run_once = true;
Marc Zyngieraa024c22013-01-20 18:28:13 -0500543
Christoffer Dall61bbe382017-10-27 19:57:51 +0200544 if (likely(irqchip_in_kernel(kvm))) {
545 /*
546 * Map the VGIC hardware resources before running a vcpu the
547 * first time on this VM.
548 */
549 if (unlikely(!vgic_ready(kvm))) {
550 ret = kvm_vgic_map_resources(kvm);
551 if (ret)
552 return ret;
553 }
554 } else {
555 /*
556 * Tell the rest of the code that there are userspace irqchip
557 * VMs in the wild.
558 */
559 static_branch_inc(&userspace_irqchip_in_use);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500560 }
561
Alexander Grafd9e13972016-09-27 21:08:06 +0200562 ret = kvm_timer_enable(vcpu);
Christoffer Dalla2befac2017-05-02 13:41:02 +0200563 if (ret)
564 return ret;
565
566 ret = kvm_arm_pmu_v3_enable(vcpu);
Christoffer Dall05971122014-12-12 21:19:23 +0100567
Christoffer Dall41a54482016-05-18 16:26:00 +0100568 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500569}
570
Eric Augerc1426e42015-03-04 11:14:34 +0100571bool kvm_arch_intc_initialized(struct kvm *kvm)
572{
573 return vgic_initialized(kvm);
574}
575
Christoffer Dallb13216c2016-04-27 10:28:00 +0100576void kvm_arm_halt_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200577{
578 int i;
579 struct kvm_vcpu *vcpu;
580
581 kvm_for_each_vcpu(i, vcpu, kvm)
582 vcpu->arch.pause = true;
Andrew Jones7b244e22017-06-04 14:43:58 +0200583 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
Eric Auger3b928302015-09-25 23:41:17 +0200584}
585
Christoffer Dallb13216c2016-04-27 10:28:00 +0100586void kvm_arm_resume_guest(struct kvm *kvm)
Eric Auger3b928302015-09-25 23:41:17 +0200587{
588 int i;
589 struct kvm_vcpu *vcpu;
590
Christoffer Dallabd72292017-05-06 20:01:24 +0200591 kvm_for_each_vcpu(i, vcpu, kvm) {
592 vcpu->arch.pause = false;
593 swake_up(kvm_arch_vcpu_wq(vcpu));
594 }
Eric Auger3b928302015-09-25 23:41:17 +0200595}
596
Andrew Jones7b244e22017-06-04 14:43:58 +0200597static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c22013-01-20 18:28:13 -0500598{
Marcelo Tosatti85773702016-02-19 09:46:39 +0100599 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
Marc Zyngieraa024c22013-01-20 18:28:13 -0500600
Marcelo Tosatti85773702016-02-19 09:46:39 +0100601 swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
Eric Auger3b928302015-09-25 23:41:17 +0200602 (!vcpu->arch.pause)));
Andrew Jones0592c002017-06-04 14:43:55 +0200603
Andrew Jones424c9892017-06-04 14:43:57 +0200604 if (vcpu->arch.power_off || vcpu->arch.pause) {
Andrew Jones0592c002017-06-04 14:43:55 +0200605 /* Awaken to handle a signal, request we sleep again later. */
Andrew Jones7b244e22017-06-04 14:43:58 +0200606 kvm_make_request(KVM_REQ_SLEEP, vcpu);
Andrew Jones0592c002017-06-04 14:43:55 +0200607 }
Marc Zyngieraa024c22013-01-20 18:28:13 -0500608}
609
Andre Przywarae8180dc2013-05-09 00:28:06 +0200610static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
611{
612 return vcpu->arch.target >= 0;
613}
614
Andrew Jones0592c002017-06-04 14:43:55 +0200615static void check_vcpu_requests(struct kvm_vcpu *vcpu)
616{
617 if (kvm_request_pending(vcpu)) {
Andrew Jones7b244e22017-06-04 14:43:58 +0200618 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
619 vcpu_req_sleep(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200620
621 /*
622 * Clear IRQ_PENDING requests that were made to guarantee
623 * that a VCPU sees new virtual interrupts.
624 */
625 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
Andrew Jones0592c002017-06-04 14:43:55 +0200626 }
627}
628
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500629/**
630 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
631 * @vcpu: The VCPU pointer
632 * @run: The kvm_run structure pointer used for userspace state exchange
633 *
634 * This function is called through the VCPU_RUN ioctl called from user space. It
635 * will execute VM code in a loop until the time slice for the process is used
636 * or some emulation is needed from user space in which case the function will
637 * return with return value 0 and with the kvm_run structure filled in with the
638 * required data for the requested emulation.
639 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500640int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
641{
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500642 int ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500643
Andre Przywarae8180dc2013-05-09 00:28:06 +0200644 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500645 return -ENOEXEC;
646
647 ret = kvm_vcpu_first_run_init(vcpu);
648 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100649 return ret;
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500650
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500651 if (run->exit_reason == KVM_EXIT_MMIO) {
652 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
653 if (ret)
Christoffer Dall829a5862017-11-29 16:37:53 +0100654 return ret;
655 if (kvm_arm_handle_step_debug(vcpu, vcpu->run))
656 return 0;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500657 }
658
Christoffer Dall829a5862017-11-29 16:37:53 +0100659 if (run->immediate_exit)
660 return -EINTR;
661
662 vcpu_load(vcpu);
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100663
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100664 kvm_sigset_activate(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500665
666 ret = 1;
667 run->exit_reason = KVM_EXIT_UNKNOWN;
668 while (ret > 0) {
669 /*
670 * Check conditions before entering the guest
671 */
672 cond_resched();
673
674 update_vttbr(vcpu->kvm);
675
Andrew Jones0592c002017-06-04 14:43:55 +0200676 check_vcpu_requests(vcpu);
677
Marc Zyngierabdf5842015-06-08 15:00:28 +0100678 /*
Marc Zyngierabdf5842015-06-08 15:00:28 +0100679 * Preparing the interrupts to be injected also
680 * involves poking the GIC, which must be done in a
681 * non-preemptible context.
682 */
683 preempt_disable();
Christoffer Dall328e5662016-03-24 11:21:04 +0100684
Dave Martin17eed272017-10-31 15:51:16 +0000685 /* Flush FP/SIMD state that can't survive guest entry/exit */
686 kvm_fpsimd_flush_cpu_state();
687
Shannon Zhaob02386e2016-02-26 19:29:19 +0800688 kvm_pmu_flush_hwstate(vcpu);
Christoffer Dall328e5662016-03-24 11:21:04 +0100689
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500690 local_irq_disable();
691
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500692 kvm_vgic_flush_hwstate(vcpu);
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500693
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500694 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200695 * Exit if we have a signal pending so that we can deliver the
696 * signal to user space.
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500697 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200698 if (signal_pending(current)) {
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500699 ret = -EINTR;
700 run->exit_reason = KVM_EXIT_INTR;
701 }
702
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200703 /*
Christoffer Dall61bbe382017-10-27 19:57:51 +0200704 * If we're using a userspace irqchip, then check if we need
705 * to tell a userspace irqchip about timer or PMU level
706 * changes and if so, exit to userspace (the actual level
707 * state gets updated in kvm_timer_update_run and
708 * kvm_pmu_update_run below).
709 */
710 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
711 if (kvm_timer_should_notify_user(vcpu) ||
712 kvm_pmu_should_notify_user(vcpu)) {
713 ret = -EINTR;
714 run->exit_reason = KVM_EXIT_INTR;
715 }
716 }
717
718 /*
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200719 * Ensure we set mode to IN_GUEST_MODE after we disable
720 * interrupts and before the final VCPU requests check.
721 * See the comment in kvm_vcpu_exiting_guest_mode() and
722 * Documentation/virtual/kvm/vcpu-requests.rst
723 */
724 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
725
Eric Auger101d3da2015-09-25 23:41:16 +0200726 if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
Andrew Jones424c9892017-06-04 14:43:57 +0200727 kvm_request_pending(vcpu)) {
Andrew Jones6a6d73b2017-06-04 14:43:54 +0200728 vcpu->mode = OUTSIDE_GUEST_MODE;
Christoffer Dall771621b2017-10-04 23:42:32 +0200729 isb(); /* Ensure work in x_flush_hwstate is committed */
Shannon Zhaob02386e2016-02-26 19:29:19 +0800730 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dall61bbe382017-10-27 19:57:51 +0200731 if (static_branch_unlikely(&userspace_irqchip_in_use))
732 kvm_timer_sync_hwstate(vcpu);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500733 kvm_vgic_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200734 local_irq_enable();
Marc Zyngierabdf5842015-06-08 15:00:28 +0100735 preempt_enable();
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500736 continue;
737 }
738
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100739 kvm_arm_setup_debug(vcpu);
740
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500741 /**************************************************************
742 * Enter the guest
743 */
744 trace_kvm_entry(*vcpu_pc(vcpu));
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200745 guest_enter_irqoff();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200746
747 if (has_vhe()) {
James Morse4f5abad2018-01-15 19:39:00 +0000748 kvm_arm_vhe_guest_enter();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200749 ret = kvm_vcpu_run_vhe(vcpu);
James Morse4f5abad2018-01-15 19:39:00 +0000750 kvm_arm_vhe_guest_exit();
Christoffer Dall3f5c90b2017-10-03 14:02:12 +0200751 } else {
752 ret = kvm_call_hyp(__kvm_vcpu_run_nvhe, vcpu);
753 }
754
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500755 vcpu->mode = OUTSIDE_GUEST_MODE;
Amit Tomarb19e6892015-11-26 10:09:43 +0000756 vcpu->stat.exits++;
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100757 /*
758 * Back from guest
759 *************************************************************/
760
Alex Bennée56c7f5e2015-07-07 17:29:56 +0100761 kvm_arm_clear_debug(vcpu);
762
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500763 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200764 * We must sync the PMU state before the vgic state so
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200765 * that the vgic can properly sample the updated state of the
766 * interrupt line.
767 */
768 kvm_pmu_sync_hwstate(vcpu);
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200769
Christoffer Dallb103cc32016-10-16 20:30:38 +0200770 /*
771 * Sync the vgic state before syncing the timer state because
772 * the timer code needs to know if the virtual timer
773 * interrupts are active.
774 */
Christoffer Dallee9bb9a2016-10-16 20:24:30 +0200775 kvm_vgic_sync_hwstate(vcpu);
776
777 /*
Christoffer Dallb103cc32016-10-16 20:30:38 +0200778 * Sync the timer hardware state before enabling interrupts as
779 * we don't want vtimer interrupts to race with syncing the
780 * timer virtual interrupt state.
781 */
Christoffer Dall61bbe382017-10-27 19:57:51 +0200782 if (static_branch_unlikely(&userspace_irqchip_in_use))
783 kvm_timer_sync_hwstate(vcpu);
Christoffer Dallb103cc32016-10-16 20:30:38 +0200784
Dave Martine6b673b2018-04-06 14:55:59 +0100785 kvm_arch_vcpu_ctxsync_fp(vcpu);
786
Christoffer Dallb103cc32016-10-16 20:30:38 +0200787 /*
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500788 * We may have taken a host interrupt in HYP mode (ie
789 * while executing the guest). This interrupt is still
790 * pending, as we haven't serviced it yet!
791 *
792 * We're now back in SVC mode, with interrupts
793 * disabled. Enabling the interrupts now will have
794 * the effect of taking the interrupt again, in SVC
795 * mode this time.
796 */
797 local_irq_enable();
798
799 /*
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200800 * We do local_irq_enable() before calling guest_exit() so
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100801 * that if a timer interrupt hits while running the guest we
802 * account that tick as being spent in the guest. We enable
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200803 * preemption after calling guest_exit() so that if we get
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100804 * preempted we make sure ticks after that is not counted as
805 * guest time.
806 */
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200807 guest_exit();
Christoffer Dallb5905dc2015-08-30 15:55:22 +0200808 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
Christoffer Dall1b3d5462015-05-28 19:49:10 +0100809
James Morse3368bd82018-01-15 19:39:04 +0000810 /* Exit types that need handling before we can be preempted */
811 handle_exit_early(vcpu, run, ret);
812
Marc Zyngierabdf5842015-06-08 15:00:28 +0100813 preempt_enable();
814
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500815 ret = handle_exit(vcpu, run, ret);
816 }
817
Alexander Grafd9e13972016-09-27 21:08:06 +0200818 /* Tell userspace about in-kernel device output levels */
Christoffer Dall3dbbdf72017-02-01 12:51:52 +0100819 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
820 kvm_timer_update_run(vcpu);
821 kvm_pmu_update_run(vcpu);
822 }
Alexander Grafd9e13972016-09-27 21:08:06 +0200823
Jan H. Schönherr20b70352017-11-24 22:39:01 +0100824 kvm_sigset_deactivate(vcpu);
825
Christoffer Dallaccb7572017-12-04 21:35:25 +0100826 vcpu_put(vcpu);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500827 return ret;
Christoffer Dall749cf76c2013-01-20 18:28:06 -0500828}
829
Christoffer Dall86ce8532013-01-20 18:28:08 -0500830static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
831{
832 int bit_index;
833 bool set;
Christoffer Dall3df59d82017-08-03 12:09:05 +0200834 unsigned long *hcr;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500835
836 if (number == KVM_ARM_IRQ_CPU_IRQ)
837 bit_index = __ffs(HCR_VI);
838 else /* KVM_ARM_IRQ_CPU_FIQ */
839 bit_index = __ffs(HCR_VF);
840
Christoffer Dall3df59d82017-08-03 12:09:05 +0200841 hcr = vcpu_hcr(vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500842 if (level)
Christoffer Dall3df59d82017-08-03 12:09:05 +0200843 set = test_and_set_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500844 else
Christoffer Dall3df59d82017-08-03 12:09:05 +0200845 set = test_and_clear_bit(bit_index, hcr);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500846
847 /*
848 * If we didn't change anything, no need to wake up or kick other CPUs
849 */
850 if (set == level)
851 return 0;
852
853 /*
854 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
855 * trigger a world-switch round on the running physical CPU to set the
856 * virtual IRQ/FIQ fields in the HCR appropriately.
857 */
Andrew Jones325f9c62017-06-04 14:43:59 +0200858 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Christoffer Dall86ce8532013-01-20 18:28:08 -0500859 kvm_vcpu_kick(vcpu);
860
861 return 0;
862}
863
Alexander Graf79558f12013-04-16 19:21:41 +0200864int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
865 bool line_status)
Christoffer Dall86ce8532013-01-20 18:28:08 -0500866{
867 u32 irq = irq_level->irq;
868 unsigned int irq_type, vcpu_idx, irq_num;
869 int nrcpus = atomic_read(&kvm->online_vcpus);
870 struct kvm_vcpu *vcpu = NULL;
871 bool level = irq_level->level;
872
873 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
874 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
875 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
876
877 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
878
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500879 switch (irq_type) {
880 case KVM_ARM_IRQ_TYPE_CPU:
881 if (irqchip_in_kernel(kvm))
882 return -ENXIO;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500883
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500884 if (vcpu_idx >= nrcpus)
885 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500886
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500887 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
888 if (!vcpu)
889 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500890
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500891 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
892 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500893
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500894 return vcpu_interrupt_line(vcpu, irq_num, level);
895 case KVM_ARM_IRQ_TYPE_PPI:
896 if (!irqchip_in_kernel(kvm))
897 return -ENXIO;
898
899 if (vcpu_idx >= nrcpus)
900 return -EINVAL;
901
902 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
903 if (!vcpu)
904 return -EINVAL;
905
906 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
907 return -EINVAL;
908
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200909 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500910 case KVM_ARM_IRQ_TYPE_SPI:
911 if (!irqchip_in_kernel(kvm))
912 return -ENXIO;
913
Andre Przywarafd1d0dd2015-04-10 16:17:59 +0100914 if (irq_num < VGIC_NR_PRIVATE_IRQS)
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500915 return -EINVAL;
916
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200917 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500918 }
919
920 return -EINVAL;
Christoffer Dall86ce8532013-01-20 18:28:08 -0500921}
922
Christoffer Dallf7fa034d2014-10-16 16:40:53 +0200923static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
924 const struct kvm_vcpu_init *init)
925{
926 unsigned int i;
927 int phys_target = kvm_target_cpu();
928
929 if (init->target != phys_target)
930 return -EINVAL;
931
932 /*
933 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
934 * use the same target.
935 */
936 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
937 return -EINVAL;
938
939 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
940 for (i = 0; i < sizeof(init->features) * 8; i++) {
941 bool set = (init->features[i / 32] & (1 << (i % 32)));
942
943 if (set && i >= KVM_VCPU_MAX_FEATURES)
944 return -ENOENT;
945
946 /*
947 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
948 * use the same feature set.
949 */
950 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
951 test_bit(i, vcpu->arch.features) != set)
952 return -EINVAL;
953
954 if (set)
955 set_bit(i, vcpu->arch.features);
956 }
957
958 vcpu->arch.target = phys_target;
959
960 /* Now we know what it is, we can reset it. */
961 return kvm_reset_vcpu(vcpu);
962}
963
964
Christoffer Dall478a8232013-11-19 17:43:19 -0800965static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
966 struct kvm_vcpu_init *init)
967{
968 int ret;
969
970 ret = kvm_vcpu_set_target(vcpu, init);
971 if (ret)
972 return ret;
973
Christoffer Dall957db102014-11-27 10:35:03 +0100974 /*
975 * Ensure a rebooted VM will fault in RAM pages and detect if the
976 * guest MMU is turned off and flush the caches as needed.
977 */
978 if (vcpu->arch.has_run_once)
979 stage2_unmap_vm(vcpu->kvm);
980
Christoffer Dallb856a592014-10-16 17:21:16 +0200981 vcpu_reset_hcr(vcpu);
982
Christoffer Dall478a8232013-11-19 17:43:19 -0800983 /*
Eric Auger37815282015-09-25 23:41:14 +0200984 * Handle the "start in power-off" case.
Christoffer Dall478a8232013-11-19 17:43:19 -0800985 */
Christoffer Dall03f1d4c2014-12-02 15:27:51 +0100986 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
Andrew Jones424c9892017-06-04 14:43:57 +0200987 vcpu_power_off(vcpu);
Christoffer Dall3ad8b3d2014-10-16 16:14:43 +0200988 else
Eric Auger37815282015-09-25 23:41:14 +0200989 vcpu->arch.power_off = false;
Christoffer Dall478a8232013-11-19 17:43:19 -0800990
991 return 0;
992}
993
Shannon Zhaof577f6c2016-01-11 20:56:17 +0800994static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
995 struct kvm_device_attr *attr)
996{
997 int ret = -ENXIO;
998
999 switch (attr->group) {
1000 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001001 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001002 break;
1003 }
1004
1005 return ret;
1006}
1007
1008static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1009 struct kvm_device_attr *attr)
1010{
1011 int ret = -ENXIO;
1012
1013 switch (attr->group) {
1014 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001015 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001016 break;
1017 }
1018
1019 return ret;
1020}
1021
1022static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1023 struct kvm_device_attr *attr)
1024{
1025 int ret = -ENXIO;
1026
1027 switch (attr->group) {
1028 default:
Shannon Zhaobb0c70b2016-01-11 21:35:32 +08001029 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001030 break;
1031 }
1032
1033 return ret;
1034}
1035
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001036long kvm_arch_vcpu_ioctl(struct file *filp,
1037 unsigned int ioctl, unsigned long arg)
1038{
1039 struct kvm_vcpu *vcpu = filp->private_data;
1040 void __user *argp = (void __user *)arg;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001041 struct kvm_device_attr attr;
Christoffer Dall9b0624712017-12-04 21:35:36 +01001042 long r;
1043
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001044 switch (ioctl) {
1045 case KVM_ARM_VCPU_INIT: {
1046 struct kvm_vcpu_init init;
1047
Christoffer Dall9b0624712017-12-04 21:35:36 +01001048 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001049 if (copy_from_user(&init, argp, sizeof(init)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001050 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001051
Christoffer Dall9b0624712017-12-04 21:35:36 +01001052 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1053 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001054 }
1055 case KVM_SET_ONE_REG:
1056 case KVM_GET_ONE_REG: {
1057 struct kvm_one_reg reg;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001058
Christoffer Dall9b0624712017-12-04 21:35:36 +01001059 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001060 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001061 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001062
Christoffer Dall9b0624712017-12-04 21:35:36 +01001063 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001064 if (copy_from_user(&reg, argp, sizeof(reg)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001065 break;
1066
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001067 if (ioctl == KVM_SET_ONE_REG)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001068 r = kvm_arm_set_reg(vcpu, &reg);
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001069 else
Christoffer Dall9b0624712017-12-04 21:35:36 +01001070 r = kvm_arm_get_reg(vcpu, &reg);
1071 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001072 }
1073 case KVM_GET_REG_LIST: {
1074 struct kvm_reg_list __user *user_list = argp;
1075 struct kvm_reg_list reg_list;
1076 unsigned n;
1077
Christoffer Dall9b0624712017-12-04 21:35:36 +01001078 r = -ENOEXEC;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001079 if (unlikely(!kvm_vcpu_initialized(vcpu)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001080 break;
Andre Przywarae8180dc2013-05-09 00:28:06 +02001081
Christoffer Dall9b0624712017-12-04 21:35:36 +01001082 r = -EFAULT;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001083 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001084 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001085 n = reg_list.n;
1086 reg_list.n = kvm_arm_num_regs(vcpu);
1087 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001088 break;
1089 r = -E2BIG;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001090 if (n < reg_list.n)
Christoffer Dall9b0624712017-12-04 21:35:36 +01001091 break;
1092 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1093 break;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001094 }
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001095 case KVM_SET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001096 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001097 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001098 break;
1099 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1100 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001101 }
1102 case KVM_GET_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001103 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001104 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001105 break;
1106 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1107 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001108 }
1109 case KVM_HAS_DEVICE_ATTR: {
Christoffer Dall9b0624712017-12-04 21:35:36 +01001110 r = -EFAULT;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001111 if (copy_from_user(&attr, argp, sizeof(attr)))
Christoffer Dall9b0624712017-12-04 21:35:36 +01001112 break;
1113 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1114 break;
Shannon Zhaof577f6c2016-01-11 20:56:17 +08001115 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001116 default:
Christoffer Dall9b0624712017-12-04 21:35:36 +01001117 r = -EINVAL;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001118 }
Christoffer Dall9b0624712017-12-04 21:35:36 +01001119
Christoffer Dall9b0624712017-12-04 21:35:36 +01001120 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001121}
1122
Mario Smarduch53c810c2015-01-15 15:58:57 -08001123/**
1124 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1125 * @kvm: kvm instance
1126 * @log: slot id and address to which we copy the log
1127 *
1128 * Steps 1-4 below provide general overview of dirty page logging. See
1129 * kvm_get_dirty_log_protect() function description for additional details.
1130 *
1131 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1132 * always flush the TLB (step 4) even if previous step failed and the dirty
1133 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1134 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1135 * writes will be marked dirty for next log read.
1136 *
1137 * 1. Take a snapshot of the bit and clear it if needed.
1138 * 2. Write protect the corresponding page.
1139 * 3. Copy the snapshot to the userspace.
1140 * 4. Flush TLB's if needed.
1141 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001142int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1143{
Mario Smarduch53c810c2015-01-15 15:58:57 -08001144 bool is_dirty = false;
1145 int r;
1146
1147 mutex_lock(&kvm->slots_lock);
1148
1149 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
1150
1151 if (is_dirty)
1152 kvm_flush_remote_tlbs(kvm);
1153
1154 mutex_unlock(&kvm->slots_lock);
1155 return r;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001156}
1157
Christoffer Dall3401d5462013-01-23 13:18:04 -05001158static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1159 struct kvm_arm_device_addr *dev_addr)
1160{
Christoffer Dall330690c2013-01-21 19:36:13 -05001161 unsigned long dev_id, type;
1162
1163 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1164 KVM_ARM_DEVICE_ID_SHIFT;
1165 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1166 KVM_ARM_DEVICE_TYPE_SHIFT;
1167
1168 switch (dev_id) {
1169 case KVM_ARM_DEVICE_VGIC_V2:
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001170 if (!vgic_present)
1171 return -ENXIO;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001172 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
Christoffer Dall330690c2013-01-21 19:36:13 -05001173 default:
1174 return -ENODEV;
1175 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001176}
1177
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001178long kvm_arch_vm_ioctl(struct file *filp,
1179 unsigned int ioctl, unsigned long arg)
1180{
Christoffer Dall3401d5462013-01-23 13:18:04 -05001181 struct kvm *kvm = filp->private_data;
1182 void __user *argp = (void __user *)arg;
1183
1184 switch (ioctl) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001185 case KVM_CREATE_IRQCHIP: {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001186 int ret;
Pavel Fedinc7da6fa2015-12-18 14:38:43 +03001187 if (!vgic_present)
1188 return -ENXIO;
Christoffer Dalla28ebea2016-08-09 19:13:01 +02001189 mutex_lock(&kvm->lock);
1190 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1191 mutex_unlock(&kvm->lock);
1192 return ret;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001193 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001194 case KVM_ARM_SET_DEVICE_ADDR: {
1195 struct kvm_arm_device_addr dev_addr;
1196
1197 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1198 return -EFAULT;
1199 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1200 }
Anup Patel42c4e0c2013-09-30 14:20:07 +05301201 case KVM_ARM_PREFERRED_TARGET: {
1202 int err;
1203 struct kvm_vcpu_init init;
1204
1205 err = kvm_vcpu_preferred_target(&init);
1206 if (err)
1207 return err;
1208
1209 if (copy_to_user(argp, &init, sizeof(init)))
1210 return -EFAULT;
1211
1212 return 0;
1213 }
Christoffer Dall3401d5462013-01-23 13:18:04 -05001214 default:
1215 return -EINVAL;
1216 }
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001217}
1218
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001219static void cpu_init_hyp_mode(void *dummy)
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001220{
Marc Zyngierdac288f2013-05-14 12:11:37 +01001221 phys_addr_t pgd_ptr;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001222 unsigned long hyp_stack_ptr;
1223 unsigned long stack_page;
1224 unsigned long vector_ptr;
1225
1226 /* Switch from the HYP stub to our own HYP init vector */
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001227 __hyp_set_vectors(kvm_get_idmap_vector());
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001228
Marc Zyngierdac288f2013-05-14 12:11:37 +01001229 pgd_ptr = kvm_mmu_get_httbr();
Christoph Lameter1436c1a2013-10-21 13:17:08 +01001230 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001231 hyp_stack_ptr = stack_page + PAGE_SIZE;
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001232 vector_ptr = (unsigned long)kvm_get_hyp_vector();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001233
Marc Zyngier12fda812016-06-30 18:40:45 +01001234 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
Marc Zyngier35a24912016-02-01 17:54:35 +00001235 __cpu_init_stage2();
Alex Bennée56c7f5e2015-07-07 17:29:56 +01001236
1237 kvm_arm_init_debug();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001238}
1239
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001240static void cpu_hyp_reset(void)
1241{
1242 if (!is_kernel_in_hyp_mode())
1243 __hyp_reset_vectors();
1244}
1245
James Morse5f5560b2016-03-30 18:33:04 +01001246static void cpu_hyp_reinit(void)
1247{
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001248 cpu_hyp_reset();
1249
James Morse5f5560b2016-03-30 18:33:04 +01001250 if (is_kernel_in_hyp_mode()) {
1251 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001252 * __cpu_init_stage2() is safe to call even if the PM
James Morse5f5560b2016-03-30 18:33:04 +01001253 * event was cancelled before the CPU was reset.
1254 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001255 __cpu_init_stage2();
Hu Huajun02d50cd2017-06-12 22:37:48 +08001256 kvm_timer_init_vhe();
James Morse5f5560b2016-03-30 18:33:04 +01001257 } else {
Marc Zyngier47eb3cb2017-04-03 19:38:01 +01001258 cpu_init_hyp_mode(NULL);
James Morse5f5560b2016-03-30 18:33:04 +01001259 }
Christoffer Dall5b0d2cc2017-03-18 13:56:56 +01001260
1261 if (vgic_present)
1262 kvm_vgic_init_cpu_hardware();
James Morse5f5560b2016-03-30 18:33:04 +01001263}
1264
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001265static void _kvm_arch_hardware_enable(void *discard)
1266{
1267 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1268 cpu_hyp_reinit();
1269 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1270 }
1271}
1272
1273int kvm_arch_hardware_enable(void)
1274{
1275 _kvm_arch_hardware_enable(NULL);
1276 return 0;
1277}
1278
1279static void _kvm_arch_hardware_disable(void *discard)
1280{
1281 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1282 cpu_hyp_reset();
1283 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1284 }
1285}
1286
1287void kvm_arch_hardware_disable(void)
1288{
1289 _kvm_arch_hardware_disable(NULL);
1290}
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001291
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001292#ifdef CONFIG_CPU_PM
1293static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1294 unsigned long cmd,
1295 void *v)
1296{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001297 /*
1298 * kvm_arm_hardware_enabled is left with its old value over
1299 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1300 * re-enable hyp.
1301 */
1302 switch (cmd) {
1303 case CPU_PM_ENTER:
1304 if (__this_cpu_read(kvm_arm_hardware_enabled))
1305 /*
1306 * don't update kvm_arm_hardware_enabled here
1307 * so that the hardware will be re-enabled
1308 * when we resume. See below.
1309 */
1310 cpu_hyp_reset();
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001311
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001312 return NOTIFY_OK;
James Morse58d6b152018-01-22 18:19:06 +00001313 case CPU_PM_ENTER_FAILED:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001314 case CPU_PM_EXIT:
1315 if (__this_cpu_read(kvm_arm_hardware_enabled))
1316 /* The hardware was enabled before suspend. */
1317 cpu_hyp_reinit();
1318
1319 return NOTIFY_OK;
1320
1321 default:
1322 return NOTIFY_DONE;
1323 }
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001324}
1325
1326static struct notifier_block hyp_init_cpu_pm_nb = {
1327 .notifier_call = hyp_init_cpu_pm_notifier,
1328};
1329
1330static void __init hyp_cpu_pm_init(void)
1331{
1332 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1333}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001334static void __init hyp_cpu_pm_exit(void)
1335{
1336 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1337}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001338#else
1339static inline void hyp_cpu_pm_init(void)
1340{
1341}
Sudeep Holla06a71a22016-04-04 14:46:51 +01001342static inline void hyp_cpu_pm_exit(void)
1343{
1344}
Lorenzo Pieralisi1fcf7ce2013-08-05 15:04:46 +01001345#endif
1346
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001347static int init_common_resources(void)
1348{
Vladimir Murzin61349932016-09-22 09:18:01 +01001349 /* set size of VMID supported by CPU */
1350 kvm_vmid_bits = kvm_get_vmid_bits();
1351 kvm_info("%d-bit VMID\n", kvm_vmid_bits);
1352
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001353 return 0;
1354}
1355
1356static int init_subsystems(void)
1357{
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001358 int err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001359
1360 /*
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001361 * Enable hardware so that subsystem initialisation can access EL2.
James Morse5f5560b2016-03-30 18:33:04 +01001362 */
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001363 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
James Morse5f5560b2016-03-30 18:33:04 +01001364
1365 /*
1366 * Register CPU lower-power notifier
1367 */
1368 hyp_cpu_pm_init();
1369
1370 /*
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001371 * Init HYP view of VGIC
1372 */
1373 err = kvm_vgic_hyp_init();
1374 switch (err) {
1375 case 0:
1376 vgic_present = true;
1377 break;
1378 case -ENODEV:
1379 case -ENXIO:
1380 vgic_present = false;
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001381 err = 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001382 break;
1383 default:
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001384 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001385 }
1386
1387 /*
1388 * Init HYP architected timer support
1389 */
Marc Zyngierf384dcf2017-12-07 11:46:15 +00001390 err = kvm_timer_hyp_init(vgic_present);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001391 if (err)
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001392 goto out;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001393
1394 kvm_perf_init();
1395 kvm_coproc_table_init();
1396
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001397out:
1398 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1399
1400 return err;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001401}
1402
1403static void teardown_hyp_mode(void)
1404{
1405 int cpu;
1406
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001407 free_hyp_pgds();
1408 for_each_possible_cpu(cpu)
1409 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
Sudeep Holla06a71a22016-04-04 14:46:51 +01001410 hyp_cpu_pm_exit();
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001411}
1412
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001413/**
1414 * Inits Hyp-mode on all online CPUs
1415 */
1416static int init_hyp_mode(void)
1417{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001418 int cpu;
1419 int err = 0;
1420
1421 /*
1422 * Allocate Hyp PGD and setup Hyp identity mapping
1423 */
1424 err = kvm_mmu_init();
1425 if (err)
1426 goto out_err;
1427
1428 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001429 * Allocate stack pages for Hypervisor-mode
1430 */
1431 for_each_possible_cpu(cpu) {
1432 unsigned long stack_page;
1433
1434 stack_page = __get_free_page(GFP_KERNEL);
1435 if (!stack_page) {
1436 err = -ENOMEM;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001437 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001438 }
1439
1440 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1441 }
1442
1443 /*
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001444 * Map the Hyp-code called directly from the host
1445 */
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001446 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001447 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001448 if (err) {
1449 kvm_err("Cannot map world-switch code\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001450 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001451 }
1452
Ard Biesheuvela0bf9772016-02-16 13:52:39 +01001453 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
Marc Zyngier74a6b882016-06-13 15:00:47 +01001454 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
Marc Zyngier910917b2015-10-27 12:18:48 +00001455 if (err) {
1456 kvm_err("Cannot map rodata section\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001457 goto out_err;
Marc Zyngier910917b2015-10-27 12:18:48 +00001458 }
1459
Marc Zyngierc8ea0392016-10-20 10:17:21 +01001460 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1461 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1462 if (err) {
1463 kvm_err("Cannot map bss section\n");
1464 goto out_err;
1465 }
1466
Marc Zyngier6840bdd2018-01-03 16:38:35 +00001467 err = kvm_map_vectors();
1468 if (err) {
1469 kvm_err("Cannot map vectors\n");
1470 goto out_err;
1471 }
1472
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001473 /*
1474 * Map the Hyp stack pages
1475 */
1476 for_each_possible_cpu(cpu) {
1477 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001478 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1479 PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001480
1481 if (err) {
1482 kvm_err("Cannot map hyp stack\n");
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001483 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001484 }
1485 }
1486
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001487 for_each_possible_cpu(cpu) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001488 kvm_cpu_context_t *cpu_ctxt;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001489
James Morse36989e72018-01-08 15:38:04 +00001490 cpu_ctxt = per_cpu_ptr(&kvm_host_cpu_state, cpu);
Marc Zyngierc8dddec2016-06-13 15:00:45 +01001491 err = create_hyp_mappings(cpu_ctxt, cpu_ctxt + 1, PAGE_HYP);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001492
1493 if (err) {
Marc Zyngier3de50da2013-04-08 16:47:19 +01001494 kvm_err("Cannot map host CPU state: %d\n", err);
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001495 goto out_err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001496 }
1497 }
1498
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001499 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001500
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001501out_err:
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001502 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001503 kvm_err("error initializing Hyp mode: %d\n", err);
1504 return err;
1505}
1506
Andre Przywarad4e071c2013-04-17 12:52:01 +02001507static void check_kvm_target_cpu(void *ret)
1508{
1509 *(int *)ret = kvm_target_cpu();
1510}
1511
Andre Przywara4429fc62014-06-02 15:37:13 +02001512struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1513{
1514 struct kvm_vcpu *vcpu;
1515 int i;
1516
1517 mpidr &= MPIDR_HWID_BITMASK;
1518 kvm_for_each_vcpu(i, vcpu, kvm) {
1519 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1520 return vcpu;
1521 }
1522 return NULL;
1523}
1524
Eric Auger24124052017-10-27 15:28:31 +01001525bool kvm_arch_has_irq_bypass(void)
1526{
1527 return true;
1528}
1529
1530int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
1531 struct irq_bypass_producer *prod)
1532{
1533 struct kvm_kernel_irqfd *irqfd =
1534 container_of(cons, struct kvm_kernel_irqfd, consumer);
1535
Marc Zyngier196b1362017-10-27 15:28:39 +01001536 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
1537 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001538}
1539void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
1540 struct irq_bypass_producer *prod)
1541{
1542 struct kvm_kernel_irqfd *irqfd =
1543 container_of(cons, struct kvm_kernel_irqfd, consumer);
1544
Marc Zyngier196b1362017-10-27 15:28:39 +01001545 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
1546 &irqfd->irq_entry);
Eric Auger24124052017-10-27 15:28:31 +01001547}
1548
1549void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
1550{
1551 struct kvm_kernel_irqfd *irqfd =
1552 container_of(cons, struct kvm_kernel_irqfd, consumer);
1553
1554 kvm_arm_halt_guest(irqfd->kvm);
1555}
1556
1557void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
1558{
1559 struct kvm_kernel_irqfd *irqfd =
1560 container_of(cons, struct kvm_kernel_irqfd, consumer);
1561
1562 kvm_arm_resume_guest(irqfd->kvm);
1563}
1564
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001565/**
1566 * Initialize Hyp-mode and memory mappings on all CPUs.
1567 */
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001568int kvm_arch_init(void *opaque)
1569{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001570 int err;
Andre Przywarad4e071c2013-04-17 12:52:01 +02001571 int ret, cpu;
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001572 bool in_hyp_mode;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001573
1574 if (!is_hyp_mode_available()) {
Ard Biesheuvel58d0d192017-11-28 15:18:19 +00001575 kvm_info("HYP mode not available\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001576 return -ENODEV;
1577 }
1578
Dave Martin85acda32018-04-20 16:20:43 +01001579 if (!kvm_arch_check_sve_has_vhe()) {
1580 kvm_pr_unimpl("SVE system without VHE unsupported. Broken cpu?");
1581 return -ENODEV;
1582 }
1583
Andre Przywarad4e071c2013-04-17 12:52:01 +02001584 for_each_online_cpu(cpu) {
1585 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1586 if (ret < 0) {
1587 kvm_err("Error, CPU %d not supported!\n", cpu);
1588 return -ENODEV;
1589 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001590 }
1591
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001592 err = init_common_resources();
1593 if (err)
1594 return err;
Srivatsa S. Bhat81468752014-03-18 15:53:05 +05301595
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001596 in_hyp_mode = is_kernel_in_hyp_mode();
1597
1598 if (!in_hyp_mode) {
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001599 err = init_hyp_mode();
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001600 if (err)
1601 goto out_err;
1602 }
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001603
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001604 err = init_subsystems();
1605 if (err)
1606 goto out_hyp;
Marc Zyngierd157f4a2013-04-12 19:12:07 +01001607
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001608 if (in_hyp_mode)
1609 kvm_info("VHE mode initialized successfully\n");
1610 else
1611 kvm_info("Hyp mode initialized successfully\n");
1612
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001613 return 0;
Marc Zyngier1e947ba2015-01-29 11:59:54 +00001614
1615out_hyp:
Julien Thierryfe7d7b02017-10-20 12:34:16 +01001616 if (!in_hyp_mode)
1617 teardown_hyp_mode();
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001618out_err:
1619 return err;
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001620}
1621
1622/* NOP: Compiling as a module not supported */
1623void kvm_arch_exit(void)
1624{
Marc Zyngier210552c2013-03-05 03:18:00 +00001625 kvm_perf_teardown();
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001626}
1627
1628static int arm_init(void)
1629{
1630 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1631 return rc;
1632}
1633
1634module_init(arm_init);