KVM: arm: Drop kvm_arch_vcpu_free()
[linux-2.6-block.git] / virt / kvm / arm / arm.c
CommitLineData
d94d71cb 1// SPDX-License-Identifier: GPL-2.0-only
749cf76c
CD
2/*
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
749cf76c
CD
5 */
6
85acda3b 7#include <linux/bug.h>
1fcf7ce0 8#include <linux/cpu_pm.h>
749cf76c
CD
9#include <linux/errno.h>
10#include <linux/err.h>
11#include <linux/kvm_host.h>
1085fdc6 12#include <linux/list.h>
749cf76c
CD
13#include <linux/module.h>
14#include <linux/vmalloc.h>
15#include <linux/fs.h>
16#include <linux/mman.h>
17#include <linux/sched.h>
86ce8535 18#include <linux/kvm.h>
2412405b
EA
19#include <linux/kvm_irqfd.h>
20#include <linux/irqbypass.h>
de737089 21#include <linux/sched/stat.h>
749cf76c 22#include <trace/events/kvm.h>
b02386eb 23#include <kvm/arm_pmu.h>
1a2fb94e 24#include <kvm/arm_psci.h>
749cf76c
CD
25
26#define CREATE_TRACE_POINTS
27#include "trace.h"
28
7c0f6ba6 29#include <linux/uaccess.h>
749cf76c
CD
30#include <asm/ptrace.h>
31#include <asm/mman.h>
342cd0ab 32#include <asm/tlbflush.h>
5b3e5e5b 33#include <asm/cacheflush.h>
85acda3b 34#include <asm/cpufeature.h>
342cd0ab
CD
35#include <asm/virt.h>
36#include <asm/kvm_arm.h>
37#include <asm/kvm_asm.h>
38#include <asm/kvm_mmu.h>
f7ed45be 39#include <asm/kvm_emulate.h>
5b3e5e5b 40#include <asm/kvm_coproc.h>
910917bb 41#include <asm/sections.h>
749cf76c 42
8564d637
SP
43#include <kvm/arm_hypercalls.h>
44#include <kvm/arm_pmu.h>
45#include <kvm/arm_psci.h>
46
749cf76c
CD
47#ifdef REQUIRES_VIRT
48__asm__(".arch_extension virt");
49#endif
50
630a1685 51DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data);
342cd0ab 52static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
342cd0ab 53
1638a12d
MZ
54/* Per-CPU variable containing the currently running vcpu. */
55static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
56
f7ed45be
CD
57/* The VMID used in the VTTBR */
58static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
20475f78 59static u32 kvm_next_vmid;
fb544d1c 60static DEFINE_SPINLOCK(kvm_vmid_lock);
342cd0ab 61
c7da6fa4
PF
62static bool vgic_present;
63
67f69197
AT
64static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
65
1638a12d
MZ
66static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
67{
1436c1aa 68 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
1638a12d
MZ
69}
70
61bbe380
CD
71DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
72
1638a12d
MZ
73/**
74 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
75 * Must be called from non-preemptible context
76 */
77struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
78{
1436c1aa 79 return __this_cpu_read(kvm_arm_running_vcpu);
1638a12d
MZ
80}
81
82/**
83 * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus.
84 */
4000be42 85struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
1638a12d
MZ
86{
87 return &kvm_arm_running_vcpu;
88}
89
749cf76c
CD
90int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
91{
92 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
93}
94
749cf76c
CD
95int kvm_arch_hardware_setup(void)
96{
97 return 0;
98}
99
f257d6dc 100int kvm_arch_check_processor_compat(void)
749cf76c 101{
f257d6dc 102 return 0;
749cf76c
CD
103}
104
c726200d
CD
105int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
106 struct kvm_enable_cap *cap)
107{
108 int r;
109
110 if (cap->flags)
111 return -EINVAL;
112
113 switch (cap->cap) {
114 case KVM_CAP_ARM_NISV_TO_USER:
115 r = 0;
116 kvm->arch.return_nisv_io_abort_to_user = true;
117 break;
118 default:
119 r = -EINVAL;
120 break;
121 }
122
123 return r;
124}
749cf76c 125
d5d8184d
CD
126/**
127 * kvm_arch_init_vm - initializes a VM data structure
128 * @kvm: pointer to the KVM struct
129 */
749cf76c
CD
130int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
131{
94d0e598 132 int ret, cpu;
d5d8184d 133
bca607eb 134 ret = kvm_arm_setup_stage2(kvm, type);
5b6c6742
SP
135 if (ret)
136 return ret;
749cf76c 137
94d0e598
MZ
138 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
139 if (!kvm->arch.last_vcpu_ran)
140 return -ENOMEM;
141
142 for_each_possible_cpu(cpu)
143 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
144
d5d8184d
CD
145 ret = kvm_alloc_stage2_pgd(kvm);
146 if (ret)
147 goto out_fail_alloc;
148
c8dddecd 149 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
d5d8184d
CD
150 if (ret)
151 goto out_free_stage2_pgd;
152
6c3d63c9 153 kvm_vgic_early_init(kvm);
a1a64387 154
d5d8184d 155 /* Mark the initial VMID generation invalid */
e329fb75 156 kvm->arch.vmid.vmid_gen = 0;
d5d8184d 157
3caa2d8c 158 /* The maximum number of VCPUs is limited by the host's GIC model */
c7da6fa4
PF
159 kvm->arch.max_vcpus = vgic_present ?
160 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
3caa2d8c 161
d5d8184d
CD
162 return ret;
163out_free_stage2_pgd:
164 kvm_free_stage2_pgd(kvm);
165out_fail_alloc:
94d0e598
MZ
166 free_percpu(kvm->arch.last_vcpu_ran);
167 kvm->arch.last_vcpu_ran = NULL;
d5d8184d 168 return ret;
749cf76c
CD
169}
170
235539b4
LC
171int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
172{
173 return 0;
174}
175
1499fa80 176vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
749cf76c
CD
177{
178 return VM_FAULT_SIGBUS;
179}
180
749cf76c 181
d5d8184d
CD
182/**
183 * kvm_arch_destroy_vm - destroy the VM data structure
184 * @kvm: pointer to the KVM struct
185 */
749cf76c
CD
186void kvm_arch_destroy_vm(struct kvm *kvm)
187{
188 int i;
189
b2c9a85d
MZ
190 kvm_vgic_destroy(kvm);
191
94d0e598
MZ
192 free_percpu(kvm->arch.last_vcpu_ran);
193 kvm->arch.last_vcpu_ran = NULL;
194
749cf76c
CD
195 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
196 if (kvm->vcpus[i]) {
4b8fff78 197 kvm_arch_vcpu_destroy(kvm->vcpus[i]);
749cf76c
CD
198 kvm->vcpus[i] = NULL;
199 }
200 }
6b2ad81b 201 atomic_set(&kvm->online_vcpus, 0);
749cf76c
CD
202}
203
784aa3d7 204int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
749cf76c
CD
205{
206 int r;
207 switch (ext) {
1a89dd91 208 case KVM_CAP_IRQCHIP:
c7da6fa4
PF
209 r = vgic_present;
210 break;
d44758c0 211 case KVM_CAP_IOEVENTFD:
7330672b 212 case KVM_CAP_DEVICE_CTRL:
749cf76c
CD
213 case KVM_CAP_USER_MEMORY:
214 case KVM_CAP_SYNC_MMU:
215 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
216 case KVM_CAP_ONE_REG:
aa024c2f 217 case KVM_CAP_ARM_PSCI:
4447a208 218 case KVM_CAP_ARM_PSCI_0_2:
98047888 219 case KVM_CAP_READONLY_MEM:
ecccf0cc 220 case KVM_CAP_MP_STATE:
460df4c1 221 case KVM_CAP_IMMEDIATE_EXIT:
58bf437f 222 case KVM_CAP_VCPU_EVENTS:
92f35b75 223 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
c726200d 224 case KVM_CAP_ARM_NISV_TO_USER:
da345174 225 case KVM_CAP_ARM_INJECT_EXT_DABT:
749cf76c
CD
226 r = 1;
227 break;
3401d546
CD
228 case KVM_CAP_ARM_SET_DEVICE_ADDR:
229 r = 1;
ca46e10f 230 break;
749cf76c
CD
231 case KVM_CAP_NR_VCPUS:
232 r = num_online_cpus();
233 break;
234 case KVM_CAP_MAX_VCPUS:
235 r = KVM_MAX_VCPUS;
236 break;
a86cb413
TH
237 case KVM_CAP_MAX_VCPU_ID:
238 r = KVM_MAX_VCPU_ID;
239 break;
2988509d
VM
240 case KVM_CAP_MSI_DEVID:
241 if (!kvm)
242 r = -EINVAL;
243 else
244 r = kvm->arch.vgic.msis_require_devid;
245 break;
f7214e60
CD
246 case KVM_CAP_ARM_USER_IRQ:
247 /*
248 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
249 * (bump this number if adding more devices)
250 */
251 r = 1;
252 break;
749cf76c 253 default:
375bdd3b 254 r = kvm_arch_vm_ioctl_check_extension(kvm, ext);
749cf76c
CD
255 break;
256 }
257 return r;
258}
259
260long kvm_arch_dev_ioctl(struct file *filp,
261 unsigned int ioctl, unsigned long arg)
262{
263 return -EINVAL;
264}
265
d1e5b0e9
MO
266struct kvm *kvm_arch_alloc_vm(void)
267{
268 if (!has_vhe())
269 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
270
271 return vzalloc(sizeof(struct kvm));
272}
273
274void kvm_arch_free_vm(struct kvm *kvm)
275{
276 if (!has_vhe())
277 kfree(kvm);
278 else
279 vfree(kvm);
280}
749cf76c
CD
281
282struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
283{
284 int err;
285 struct kvm_vcpu *vcpu;
286
716139df
CD
287 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
288 err = -EBUSY;
289 goto out;
290 }
291
3caa2d8c
AP
292 if (id >= kvm->arch.max_vcpus) {
293 err = -EINVAL;
294 goto out;
295 }
296
749cf76c
CD
297 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
298 if (!vcpu) {
299 err = -ENOMEM;
300 goto out;
301 }
302
303 err = kvm_vcpu_init(vcpu, kvm, id);
304 if (err)
305 goto free_vcpu;
306
c8dddecd 307 err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
d5d8184d
CD
308 if (err)
309 goto vcpu_uninit;
310
749cf76c 311 return vcpu;
d5d8184d
CD
312vcpu_uninit:
313 kvm_vcpu_uninit(vcpu);
749cf76c
CD
314free_vcpu:
315 kmem_cache_free(kvm_vcpu_cache, vcpu);
316out:
317 return ERR_PTR(err);
318}
319
31928aa5 320void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
749cf76c 321{
749cf76c
CD
322}
323
4b8fff78 324void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
749cf76c 325{
f1d7231c
CD
326 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
327 static_branch_dec(&userspace_irqchip_in_use);
328
d5d8184d 329 kvm_mmu_free_memory_caches(vcpu);
967f8427 330 kvm_timer_vcpu_terminate(vcpu);
5f0a714a 331 kvm_pmu_vcpu_destroy(vcpu);
591d215a 332 kvm_vcpu_uninit(vcpu);
d5d8184d 333 kmem_cache_free(kvm_vcpu_cache, vcpu);
749cf76c
CD
334}
335
749cf76c
CD
336int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
337{
1c88ab7e 338 return kvm_timer_is_pending(vcpu);
749cf76c
CD
339}
340
d35268da
CD
341void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
342{
5eeaf10e
MZ
343 /*
344 * If we're about to block (most likely because we've just hit a
345 * WFI), we need to sync back the state of the GIC CPU interface
8e01d9a3 346 * so that we have the latest PMR and group enables. This ensures
5eeaf10e
MZ
347 * that kvm_arch_vcpu_runnable has up-to-date data to decide
348 * whether we have pending interrupts.
8e01d9a3
MZ
349 *
350 * For the same reason, we want to tell GICv4 that we need
351 * doorbells to be signalled, should an interrupt become pending.
5eeaf10e
MZ
352 */
353 preempt_disable();
354 kvm_vgic_vmcr_sync(vcpu);
8e01d9a3 355 vgic_v4_put(vcpu, true);
5eeaf10e 356 preempt_enable();
d35268da
CD
357}
358
359void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
360{
8e01d9a3
MZ
361 preempt_disable();
362 vgic_v4_load(vcpu);
363 preempt_enable();
d35268da
CD
364}
365
749cf76c
CD
366int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
367{
f7ed45be
CD
368 /* Force users to call KVM_ARM_VCPU_INIT */
369 vcpu->arch.target = -1;
f7fa034d 370 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1a89dd91 371
967f8427
MZ
372 /* Set up the timer */
373 kvm_timer_vcpu_init(vcpu);
374
bca031e2
ZY
375 kvm_pmu_vcpu_init(vcpu);
376
84e690bf
AB
377 kvm_arm_reset_debug_ptr(vcpu);
378
8564d637
SP
379 kvm_arm_pvtime_vcpu_init(&vcpu->arch);
380
1aab6f46 381 return kvm_vgic_vcpu_init(vcpu);
749cf76c
CD
382}
383
749cf76c
CD
384void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
385{
94d0e598 386 int *last_ran;
630a1685 387 kvm_host_data_t *cpu_data;
94d0e598
MZ
388
389 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
630a1685 390 cpu_data = this_cpu_ptr(&kvm_host_data);
94d0e598
MZ
391
392 /*
393 * We might get preempted before the vCPU actually runs, but
394 * over-invalidation doesn't affect correctness.
395 */
396 if (*last_ran != vcpu->vcpu_id) {
397 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
398 *last_ran = vcpu->vcpu_id;
399 }
400
86ce8535 401 vcpu->cpu = cpu;
630a1685 402 vcpu->arch.host_cpu_context = &cpu_data->host_ctxt;
5b3e5e5b 403
1638a12d 404 kvm_arm_set_running_vcpu(vcpu);
328e5664 405 kvm_vgic_load(vcpu);
b103cc3f 406 kvm_timer_vcpu_load(vcpu);
bc192cee 407 kvm_vcpu_load_sysregs(vcpu);
e6b673b7 408 kvm_arch_vcpu_load_fp(vcpu);
435e53fb 409 kvm_vcpu_pmu_restore_guest(vcpu);
8564d637
SP
410 if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
411 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
de737089
MZ
412
413 if (single_task_running())
ef2e78dd 414 vcpu_clear_wfx_traps(vcpu);
de737089 415 else
ef2e78dd 416 vcpu_set_wfx_traps(vcpu);
384b40ca
MR
417
418 vcpu_ptrauth_setup_lazy(vcpu);
749cf76c
CD
419}
420
421void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
422{
e6b673b7 423 kvm_arch_vcpu_put_fp(vcpu);
bc192cee 424 kvm_vcpu_put_sysregs(vcpu);
b103cc3f 425 kvm_timer_vcpu_put(vcpu);
328e5664 426 kvm_vgic_put(vcpu);
435e53fb 427 kvm_vcpu_pmu_restore_host(vcpu);
328e5664 428
e9b152cb
CD
429 vcpu->cpu = -1;
430
1638a12d 431 kvm_arm_set_running_vcpu(NULL);
749cf76c
CD
432}
433
424c989b
AJ
434static void vcpu_power_off(struct kvm_vcpu *vcpu)
435{
436 vcpu->arch.power_off = true;
7b244e2b 437 kvm_make_request(KVM_REQ_SLEEP, vcpu);
424c989b
AJ
438 kvm_vcpu_kick(vcpu);
439}
440
749cf76c
CD
441int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
442 struct kvm_mp_state *mp_state)
443{
3781528e 444 if (vcpu->arch.power_off)
ecccf0cc
AB
445 mp_state->mp_state = KVM_MP_STATE_STOPPED;
446 else
447 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
448
449 return 0;
749cf76c
CD
450}
451
452int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
453 struct kvm_mp_state *mp_state)
454{
e83dff5e
CD
455 int ret = 0;
456
ecccf0cc
AB
457 switch (mp_state->mp_state) {
458 case KVM_MP_STATE_RUNNABLE:
3781528e 459 vcpu->arch.power_off = false;
ecccf0cc
AB
460 break;
461 case KVM_MP_STATE_STOPPED:
424c989b 462 vcpu_power_off(vcpu);
ecccf0cc
AB
463 break;
464 default:
e83dff5e 465 ret = -EINVAL;
ecccf0cc
AB
466 }
467
e83dff5e 468 return ret;
749cf76c
CD
469}
470
5b3e5e5b
CD
471/**
472 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
473 * @v: The VCPU pointer
474 *
475 * If the guest CPU is not waiting for interrupts or an interrupt line is
476 * asserted, the CPU is by definition runnable.
477 */
749cf76c
CD
478int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
479{
3df59d8d
CD
480 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
481 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
3b92830a 482 && !v->arch.power_off && !v->arch.pause);
749cf76c
CD
483}
484
199b5763
LM
485bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
486{
f01fbd2f 487 return vcpu_mode_priv(vcpu);
199b5763
LM
488}
489
f7ed45be
CD
490/* Just ensure a guest exit from a particular CPU */
491static void exit_vm_noop(void *info)
492{
493}
494
495void force_vm_exit(const cpumask_t *mask)
496{
898f949f 497 preempt_disable();
f7ed45be 498 smp_call_function_many(mask, exit_vm_noop, NULL, true);
898f949f 499 preempt_enable();
f7ed45be
CD
500}
501
502/**
503 * need_new_vmid_gen - check that the VMID is still valid
e329fb75 504 * @vmid: The VMID to check
f7ed45be
CD
505 *
506 * return true if there is a new generation of VMIDs being used
507 *
e329fb75
CD
508 * The hardware supports a limited set of values with the value zero reserved
509 * for the host, so we check if an assigned value belongs to a previous
510 * generation, which which requires us to assign a new value. If we're the
511 * first to use a VMID for the new generation, we must flush necessary caches
512 * and TLBs on all CPUs.
f7ed45be 513 */
e329fb75 514static bool need_new_vmid_gen(struct kvm_vmid *vmid)
f7ed45be 515{
fb544d1c
CD
516 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
517 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
e329fb75 518 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
f7ed45be
CD
519}
520
521/**
e329fb75
CD
522 * update_vmid - Update the vmid with a valid VMID for the current generation
523 * @kvm: The guest that struct vmid belongs to
524 * @vmid: The stage-2 VMID information struct
f7ed45be 525 */
e329fb75 526static void update_vmid(struct kvm_vmid *vmid)
f7ed45be 527{
e329fb75 528 if (!need_new_vmid_gen(vmid))
f7ed45be
CD
529 return;
530
fb544d1c 531 spin_lock(&kvm_vmid_lock);
f7ed45be
CD
532
533 /*
534 * We need to re-check the vmid_gen here to ensure that if another vcpu
535 * already allocated a valid vmid for this vm, then this vcpu should
536 * use the same vmid.
537 */
e329fb75 538 if (!need_new_vmid_gen(vmid)) {
fb544d1c 539 spin_unlock(&kvm_vmid_lock);
f7ed45be
CD
540 return;
541 }
542
543 /* First user of a new VMID generation? */
544 if (unlikely(kvm_next_vmid == 0)) {
545 atomic64_inc(&kvm_vmid_gen);
546 kvm_next_vmid = 1;
547
548 /*
549 * On SMP we know no other CPUs can use this CPU's or each
550 * other's VMID after force_vm_exit returns since the
551 * kvm_vmid_lock blocks them from reentry to the guest.
552 */
553 force_vm_exit(cpu_all_mask);
554 /*
555 * Now broadcast TLB + ICACHE invalidation over the inner
556 * shareable domain to make sure all data structures are
557 * clean.
558 */
559 kvm_call_hyp(__kvm_flush_vm_context);
560 }
561
e329fb75 562 vmid->vmid = kvm_next_vmid;
f7ed45be 563 kvm_next_vmid++;
e329fb75 564 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
f7ed45be 565
fb544d1c 566 smp_wmb();
e329fb75 567 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
fb544d1c
CD
568
569 spin_unlock(&kvm_vmid_lock);
f7ed45be
CD
570}
571
f7ed45be
CD
572static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
573{
05971120 574 struct kvm *kvm = vcpu->kvm;
41a54482 575 int ret = 0;
e1ba0207 576
f7ed45be
CD
577 if (likely(vcpu->arch.has_run_once))
578 return 0;
579
7dd32a0d
DM
580 if (!kvm_arm_vcpu_is_finalized(vcpu))
581 return -EPERM;
582
f7ed45be 583 vcpu->arch.has_run_once = true;
aa024c2f 584
61bbe380
CD
585 if (likely(irqchip_in_kernel(kvm))) {
586 /*
587 * Map the VGIC hardware resources before running a vcpu the
588 * first time on this VM.
589 */
590 if (unlikely(!vgic_ready(kvm))) {
591 ret = kvm_vgic_map_resources(kvm);
592 if (ret)
593 return ret;
594 }
595 } else {
596 /*
597 * Tell the rest of the code that there are userspace irqchip
598 * VMs in the wild.
599 */
600 static_branch_inc(&userspace_irqchip_in_use);
01ac5e34
MZ
601 }
602
d9e13977 603 ret = kvm_timer_enable(vcpu);
a2befacf
CD
604 if (ret)
605 return ret;
606
607 ret = kvm_arm_pmu_v3_enable(vcpu);
05971120 608
41a54482 609 return ret;
f7ed45be
CD
610}
611
c1426e4c
EA
612bool kvm_arch_intc_initialized(struct kvm *kvm)
613{
614 return vgic_initialized(kvm);
615}
616
b13216cf 617void kvm_arm_halt_guest(struct kvm *kvm)
3b92830a
EA
618{
619 int i;
620 struct kvm_vcpu *vcpu;
621
622 kvm_for_each_vcpu(i, vcpu, kvm)
623 vcpu->arch.pause = true;
7b244e2b 624 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
3b92830a
EA
625}
626
b13216cf 627void kvm_arm_resume_guest(struct kvm *kvm)
3b92830a
EA
628{
629 int i;
630 struct kvm_vcpu *vcpu;
631
abd72296
CD
632 kvm_for_each_vcpu(i, vcpu, kvm) {
633 vcpu->arch.pause = false;
b3dae109 634 swake_up_one(kvm_arch_vcpu_wq(vcpu));
abd72296 635 }
3b92830a
EA
636}
637
7b244e2b 638static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
aa024c2f 639{
8577370f 640 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
aa024c2f 641
b3dae109 642 swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) &&
3b92830a 643 (!vcpu->arch.pause)));
0592c005 644
424c989b 645 if (vcpu->arch.power_off || vcpu->arch.pause) {
0592c005 646 /* Awaken to handle a signal, request we sleep again later. */
7b244e2b 647 kvm_make_request(KVM_REQ_SLEEP, vcpu);
0592c005 648 }
358b28f0
MZ
649
650 /*
651 * Make sure we will observe a potential reset request if we've
652 * observed a change to the power state. Pairs with the smp_wmb() in
653 * kvm_psci_vcpu_on().
654 */
655 smp_rmb();
aa024c2f
MZ
656}
657
e8180dca
AP
658static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
659{
660 return vcpu->arch.target >= 0;
661}
662
0592c005
AJ
663static void check_vcpu_requests(struct kvm_vcpu *vcpu)
664{
665 if (kvm_request_pending(vcpu)) {
7b244e2b
AJ
666 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
667 vcpu_req_sleep(vcpu);
325f9c64 668
358b28f0
MZ
669 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
670 kvm_reset_vcpu(vcpu);
671
325f9c64
AJ
672 /*
673 * Clear IRQ_PENDING requests that were made to guarantee
674 * that a VCPU sees new virtual interrupts.
675 */
676 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
8564d637
SP
677
678 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
679 kvm_update_stolen_time(vcpu);
0592c005
AJ
680 }
681}
682
f7ed45be
CD
683/**
684 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
685 * @vcpu: The VCPU pointer
686 * @run: The kvm_run structure pointer used for userspace state exchange
687 *
688 * This function is called through the VCPU_RUN ioctl called from user space. It
689 * will execute VM code in a loop until the time slice for the process is used
690 * or some emulation is needed from user space in which case the function will
691 * return with return value 0 and with the kvm_run structure filled in with the
692 * required data for the requested emulation.
693 */
749cf76c
CD
694int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
695{
f7ed45be 696 int ret;
f7ed45be 697
e8180dca 698 if (unlikely(!kvm_vcpu_initialized(vcpu)))
f7ed45be
CD
699 return -ENOEXEC;
700
701 ret = kvm_vcpu_first_run_init(vcpu);
702 if (ret)
829a5863 703 return ret;
f7ed45be 704
45e96ea6
CD
705 if (run->exit_reason == KVM_EXIT_MMIO) {
706 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
707 if (ret)
829a5863 708 return ret;
45e96ea6
CD
709 }
710
829a5863
CD
711 if (run->immediate_exit)
712 return -EINTR;
713
714 vcpu_load(vcpu);
460df4c1 715
20b7035c 716 kvm_sigset_activate(vcpu);
f7ed45be
CD
717
718 ret = 1;
719 run->exit_reason = KVM_EXIT_UNKNOWN;
720 while (ret > 0) {
721 /*
722 * Check conditions before entering the guest
723 */
724 cond_resched();
725
e329fb75 726 update_vmid(&vcpu->kvm->arch.vmid);
f7ed45be 727
0592c005
AJ
728 check_vcpu_requests(vcpu);
729
abdf5843
MZ
730 /*
731 * Preparing the interrupts to be injected also
732 * involves poking the GIC, which must be done in a
733 * non-preemptible context.
734 */
1b3d546d 735 preempt_disable();
328e5664 736
b02386eb 737 kvm_pmu_flush_hwstate(vcpu);
328e5664 738
f7ed45be
CD
739 local_irq_disable();
740
abdf5843
MZ
741 kvm_vgic_flush_hwstate(vcpu);
742
f7ed45be 743 /*
61bbe380
CD
744 * Exit if we have a signal pending so that we can deliver the
745 * signal to user space.
f7ed45be 746 */
61bbe380 747 if (signal_pending(current)) {
f7ed45be
CD
748 ret = -EINTR;
749 run->exit_reason = KVM_EXIT_INTR;
750 }
751
61bbe380
CD
752 /*
753 * If we're using a userspace irqchip, then check if we need
754 * to tell a userspace irqchip about timer or PMU level
755 * changes and if so, exit to userspace (the actual level
756 * state gets updated in kvm_timer_update_run and
757 * kvm_pmu_update_run below).
758 */
759 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
760 if (kvm_timer_should_notify_user(vcpu) ||
761 kvm_pmu_should_notify_user(vcpu)) {
762 ret = -EINTR;
763 run->exit_reason = KVM_EXIT_INTR;
764 }
765 }
766
6a6d73be
AJ
767 /*
768 * Ensure we set mode to IN_GUEST_MODE after we disable
769 * interrupts and before the final VCPU requests check.
770 * See the comment in kvm_vcpu_exiting_guest_mode() and
2f5947df 771 * Documentation/virt/kvm/vcpu-requests.rst
6a6d73be
AJ
772 */
773 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
774
e329fb75 775 if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) ||
424c989b 776 kvm_request_pending(vcpu)) {
6a6d73be 777 vcpu->mode = OUTSIDE_GUEST_MODE;
771621b0 778 isb(); /* Ensure work in x_flush_hwstate is committed */
b02386eb 779 kvm_pmu_sync_hwstate(vcpu);
61bbe380
CD
780 if (static_branch_unlikely(&userspace_irqchip_in_use))
781 kvm_timer_sync_hwstate(vcpu);
1a89dd91 782 kvm_vgic_sync_hwstate(vcpu);
ee9bb9a1 783 local_irq_enable();
abdf5843 784 preempt_enable();
f7ed45be
CD
785 continue;
786 }
787
56c7f5e7
AB
788 kvm_arm_setup_debug(vcpu);
789
f7ed45be
CD
790 /**************************************************************
791 * Enter the guest
792 */
793 trace_kvm_entry(*vcpu_pc(vcpu));
6edaa530 794 guest_enter_irqoff();
f7ed45be 795
3f5c90b8
CD
796 if (has_vhe()) {
797 kvm_arm_vhe_guest_enter();
798 ret = kvm_vcpu_run_vhe(vcpu);
4f5abad9 799 kvm_arm_vhe_guest_exit();
3f5c90b8 800 } else {
7aa8d146 801 ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu);
3f5c90b8
CD
802 }
803
f7ed45be 804 vcpu->mode = OUTSIDE_GUEST_MODE;
b19e6892 805 vcpu->stat.exits++;
1b3d546d
CD
806 /*
807 * Back from guest
808 *************************************************************/
809
56c7f5e7
AB
810 kvm_arm_clear_debug(vcpu);
811
ee9bb9a1 812 /*
b103cc3f 813 * We must sync the PMU state before the vgic state so
ee9bb9a1
CD
814 * that the vgic can properly sample the updated state of the
815 * interrupt line.
816 */
817 kvm_pmu_sync_hwstate(vcpu);
ee9bb9a1 818
b103cc3f
CD
819 /*
820 * Sync the vgic state before syncing the timer state because
821 * the timer code needs to know if the virtual timer
822 * interrupts are active.
823 */
ee9bb9a1
CD
824 kvm_vgic_sync_hwstate(vcpu);
825
b103cc3f
CD
826 /*
827 * Sync the timer hardware state before enabling interrupts as
828 * we don't want vtimer interrupts to race with syncing the
829 * timer virtual interrupt state.
830 */
61bbe380
CD
831 if (static_branch_unlikely(&userspace_irqchip_in_use))
832 kvm_timer_sync_hwstate(vcpu);
b103cc3f 833
e6b673b7
DM
834 kvm_arch_vcpu_ctxsync_fp(vcpu);
835
f7ed45be
CD
836 /*
837 * We may have taken a host interrupt in HYP mode (ie
838 * while executing the guest). This interrupt is still
839 * pending, as we haven't serviced it yet!
840 *
841 * We're now back in SVC mode, with interrupts
842 * disabled. Enabling the interrupts now will have
843 * the effect of taking the interrupt again, in SVC
844 * mode this time.
845 */
846 local_irq_enable();
847
848 /*
6edaa530 849 * We do local_irq_enable() before calling guest_exit() so
1b3d546d
CD
850 * that if a timer interrupt hits while running the guest we
851 * account that tick as being spent in the guest. We enable
6edaa530 852 * preemption after calling guest_exit() so that if we get
1b3d546d
CD
853 * preempted we make sure ticks after that is not counted as
854 * guest time.
855 */
6edaa530 856 guest_exit();
b5905dc1 857 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1b3d546d 858
3368bd80
JM
859 /* Exit types that need handling before we can be preempted */
860 handle_exit_early(vcpu, run, ret);
861
abdf5843
MZ
862 preempt_enable();
863
f7ed45be
CD
864 ret = handle_exit(vcpu, run, ret);
865 }
866
d9e13977 867 /* Tell userspace about in-kernel device output levels */
3dbbdf78
CD
868 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
869 kvm_timer_update_run(vcpu);
870 kvm_pmu_update_run(vcpu);
871 }
d9e13977 872
20b7035c
JS
873 kvm_sigset_deactivate(vcpu);
874
accb757d 875 vcpu_put(vcpu);
f7ed45be 876 return ret;
749cf76c
CD
877}
878
86ce8535
CD
879static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
880{
881 int bit_index;
882 bool set;
3df59d8d 883 unsigned long *hcr;
86ce8535
CD
884
885 if (number == KVM_ARM_IRQ_CPU_IRQ)
886 bit_index = __ffs(HCR_VI);
887 else /* KVM_ARM_IRQ_CPU_FIQ */
888 bit_index = __ffs(HCR_VF);
889
3df59d8d 890 hcr = vcpu_hcr(vcpu);
86ce8535 891 if (level)
3df59d8d 892 set = test_and_set_bit(bit_index, hcr);
86ce8535 893 else
3df59d8d 894 set = test_and_clear_bit(bit_index, hcr);
86ce8535
CD
895
896 /*
897 * If we didn't change anything, no need to wake up or kick other CPUs
898 */
899 if (set == level)
900 return 0;
901
902 /*
903 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
904 * trigger a world-switch round on the running physical CPU to set the
905 * virtual IRQ/FIQ fields in the HCR appropriately.
906 */
325f9c64 907 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
86ce8535
CD
908 kvm_vcpu_kick(vcpu);
909
910 return 0;
911}
912
79558f11
AG
913int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
914 bool line_status)
86ce8535
CD
915{
916 u32 irq = irq_level->irq;
917 unsigned int irq_type, vcpu_idx, irq_num;
918 int nrcpus = atomic_read(&kvm->online_vcpus);
919 struct kvm_vcpu *vcpu = NULL;
920 bool level = irq_level->level;
921
922 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
923 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
92f35b75 924 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
86ce8535
CD
925 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
926
927 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
928
5863c2ce
MZ
929 switch (irq_type) {
930 case KVM_ARM_IRQ_TYPE_CPU:
931 if (irqchip_in_kernel(kvm))
932 return -ENXIO;
86ce8535 933
5863c2ce
MZ
934 if (vcpu_idx >= nrcpus)
935 return -EINVAL;
86ce8535 936
5863c2ce
MZ
937 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
938 if (!vcpu)
939 return -EINVAL;
86ce8535 940
5863c2ce
MZ
941 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
942 return -EINVAL;
943
944 return vcpu_interrupt_line(vcpu, irq_num, level);
945 case KVM_ARM_IRQ_TYPE_PPI:
946 if (!irqchip_in_kernel(kvm))
947 return -ENXIO;
948
949 if (vcpu_idx >= nrcpus)
950 return -EINVAL;
951
952 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
953 if (!vcpu)
954 return -EINVAL;
955
956 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
957 return -EINVAL;
86ce8535 958
cb3f0ad8 959 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
5863c2ce
MZ
960 case KVM_ARM_IRQ_TYPE_SPI:
961 if (!irqchip_in_kernel(kvm))
962 return -ENXIO;
963
fd1d0ddf 964 if (irq_num < VGIC_NR_PRIVATE_IRQS)
5863c2ce
MZ
965 return -EINVAL;
966
cb3f0ad8 967 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
5863c2ce
MZ
968 }
969
970 return -EINVAL;
86ce8535
CD
971}
972
f7fa034d
CD
973static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
974 const struct kvm_vcpu_init *init)
975{
811328fc 976 unsigned int i, ret;
f7fa034d
CD
977 int phys_target = kvm_target_cpu();
978
979 if (init->target != phys_target)
980 return -EINVAL;
981
982 /*
983 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
984 * use the same target.
985 */
986 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
987 return -EINVAL;
988
989 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
990 for (i = 0; i < sizeof(init->features) * 8; i++) {
991 bool set = (init->features[i / 32] & (1 << (i % 32)));
992
993 if (set && i >= KVM_VCPU_MAX_FEATURES)
994 return -ENOENT;
995
996 /*
997 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
998 * use the same feature set.
999 */
1000 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
1001 test_bit(i, vcpu->arch.features) != set)
1002 return -EINVAL;
1003
1004 if (set)
1005 set_bit(i, vcpu->arch.features);
1006 }
1007
1008 vcpu->arch.target = phys_target;
1009
1010 /* Now we know what it is, we can reset it. */
811328fc
AJ
1011 ret = kvm_reset_vcpu(vcpu);
1012 if (ret) {
1013 vcpu->arch.target = -1;
1014 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1015 }
f7fa034d 1016
811328fc
AJ
1017 return ret;
1018}
f7fa034d 1019
478a8237
CD
1020static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1021 struct kvm_vcpu_init *init)
1022{
1023 int ret;
1024
1025 ret = kvm_vcpu_set_target(vcpu, init);
1026 if (ret)
1027 return ret;
1028
957db105
CD
1029 /*
1030 * Ensure a rebooted VM will fault in RAM pages and detect if the
1031 * guest MMU is turned off and flush the caches as needed.
1032 */
1033 if (vcpu->arch.has_run_once)
1034 stage2_unmap_vm(vcpu->kvm);
1035
b856a591
CD
1036 vcpu_reset_hcr(vcpu);
1037
478a8237 1038 /*
3781528e 1039 * Handle the "start in power-off" case.
478a8237 1040 */
03f1d4c1 1041 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
424c989b 1042 vcpu_power_off(vcpu);
3ad8b3de 1043 else
3781528e 1044 vcpu->arch.power_off = false;
478a8237
CD
1045
1046 return 0;
1047}
1048
f577f6c2
SZ
1049static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1050 struct kvm_device_attr *attr)
1051{
1052 int ret = -ENXIO;
1053
1054 switch (attr->group) {
1055 default:
bb0c70bc 1056 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
f577f6c2
SZ
1057 break;
1058 }
1059
1060 return ret;
1061}
1062
1063static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1064 struct kvm_device_attr *attr)
1065{
1066 int ret = -ENXIO;
1067
1068 switch (attr->group) {
1069 default:
bb0c70bc 1070 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
f577f6c2
SZ
1071 break;
1072 }
1073
1074 return ret;
1075}
1076
1077static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1078 struct kvm_device_attr *attr)
1079{
1080 int ret = -ENXIO;
1081
1082 switch (attr->group) {
1083 default:
bb0c70bc 1084 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
f577f6c2
SZ
1085 break;
1086 }
1087
1088 return ret;
1089}
1090
539aee0e
JM
1091static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1092 struct kvm_vcpu_events *events)
1093{
1094 memset(events, 0, sizeof(*events));
1095
1096 return __kvm_arm_vcpu_get_events(vcpu, events);
1097}
1098
1099static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1100 struct kvm_vcpu_events *events)
1101{
1102 int i;
1103
1104 /* check whether the reserved field is zero */
1105 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1106 if (events->reserved[i])
1107 return -EINVAL;
1108
1109 /* check whether the pad field is zero */
1110 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1111 if (events->exception.pad[i])
1112 return -EINVAL;
1113
1114 return __kvm_arm_vcpu_set_events(vcpu, events);
1115}
539aee0e 1116
749cf76c
CD
1117long kvm_arch_vcpu_ioctl(struct file *filp,
1118 unsigned int ioctl, unsigned long arg)
1119{
1120 struct kvm_vcpu *vcpu = filp->private_data;
1121 void __user *argp = (void __user *)arg;
f577f6c2 1122 struct kvm_device_attr attr;
9b062471
CD
1123 long r;
1124
749cf76c
CD
1125 switch (ioctl) {
1126 case KVM_ARM_VCPU_INIT: {
1127 struct kvm_vcpu_init init;
1128
9b062471 1129 r = -EFAULT;
749cf76c 1130 if (copy_from_user(&init, argp, sizeof(init)))
9b062471 1131 break;
749cf76c 1132
9b062471
CD
1133 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1134 break;
749cf76c
CD
1135 }
1136 case KVM_SET_ONE_REG:
1137 case KVM_GET_ONE_REG: {
1138 struct kvm_one_reg reg;
e8180dca 1139
9b062471 1140 r = -ENOEXEC;
e8180dca 1141 if (unlikely(!kvm_vcpu_initialized(vcpu)))
9b062471 1142 break;
e8180dca 1143
9b062471 1144 r = -EFAULT;
749cf76c 1145 if (copy_from_user(&reg, argp, sizeof(reg)))
9b062471
CD
1146 break;
1147
749cf76c 1148 if (ioctl == KVM_SET_ONE_REG)
9b062471 1149 r = kvm_arm_set_reg(vcpu, &reg);
749cf76c 1150 else
9b062471
CD
1151 r = kvm_arm_get_reg(vcpu, &reg);
1152 break;
749cf76c
CD
1153 }
1154 case KVM_GET_REG_LIST: {
1155 struct kvm_reg_list __user *user_list = argp;
1156 struct kvm_reg_list reg_list;
1157 unsigned n;
1158
9b062471 1159 r = -ENOEXEC;
e8180dca 1160 if (unlikely(!kvm_vcpu_initialized(vcpu)))
9b062471 1161 break;
e8180dca 1162
7dd32a0d
DM
1163 r = -EPERM;
1164 if (!kvm_arm_vcpu_is_finalized(vcpu))
1165 break;
1166
9b062471 1167 r = -EFAULT;
749cf76c 1168 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
9b062471 1169 break;
749cf76c
CD
1170 n = reg_list.n;
1171 reg_list.n = kvm_arm_num_regs(vcpu);
1172 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
9b062471
CD
1173 break;
1174 r = -E2BIG;
749cf76c 1175 if (n < reg_list.n)
9b062471
CD
1176 break;
1177 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1178 break;
749cf76c 1179 }
f577f6c2 1180 case KVM_SET_DEVICE_ATTR: {
9b062471 1181 r = -EFAULT;
f577f6c2 1182 if (copy_from_user(&attr, argp, sizeof(attr)))
9b062471
CD
1183 break;
1184 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1185 break;
f577f6c2
SZ
1186 }
1187 case KVM_GET_DEVICE_ATTR: {
9b062471 1188 r = -EFAULT;
f577f6c2 1189 if (copy_from_user(&attr, argp, sizeof(attr)))
9b062471
CD
1190 break;
1191 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1192 break;
f577f6c2
SZ
1193 }
1194 case KVM_HAS_DEVICE_ATTR: {
9b062471 1195 r = -EFAULT;
f577f6c2 1196 if (copy_from_user(&attr, argp, sizeof(attr)))
9b062471
CD
1197 break;
1198 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1199 break;
f577f6c2 1200 }
b7b27fac
DG
1201 case KVM_GET_VCPU_EVENTS: {
1202 struct kvm_vcpu_events events;
1203
1204 if (kvm_arm_vcpu_get_events(vcpu, &events))
1205 return -EINVAL;
1206
1207 if (copy_to_user(argp, &events, sizeof(events)))
1208 return -EFAULT;
1209
1210 return 0;
1211 }
1212 case KVM_SET_VCPU_EVENTS: {
1213 struct kvm_vcpu_events events;
1214
1215 if (copy_from_user(&events, argp, sizeof(events)))
1216 return -EFAULT;
1217
1218 return kvm_arm_vcpu_set_events(vcpu, &events);
1219 }
7dd32a0d
DM
1220 case KVM_ARM_VCPU_FINALIZE: {
1221 int what;
1222
1223 if (!kvm_vcpu_initialized(vcpu))
1224 return -ENOEXEC;
1225
1226 if (get_user(what, (const int __user *)argp))
1227 return -EFAULT;
1228
1229 return kvm_arm_vcpu_finalize(vcpu, what);
1230 }
749cf76c 1231 default:
9b062471 1232 r = -EINVAL;
749cf76c 1233 }
9b062471 1234
9b062471 1235 return r;
749cf76c
CD
1236}
1237
53c810c3
MS
1238/**
1239 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1240 * @kvm: kvm instance
1241 * @log: slot id and address to which we copy the log
1242 *
1243 * Steps 1-4 below provide general overview of dirty page logging. See
1244 * kvm_get_dirty_log_protect() function description for additional details.
1245 *
1246 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1247 * always flush the TLB (step 4) even if previous step failed and the dirty
1248 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1249 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1250 * writes will be marked dirty for next log read.
1251 *
1252 * 1. Take a snapshot of the bit and clear it if needed.
1253 * 2. Write protect the corresponding page.
1254 * 3. Copy the snapshot to the userspace.
1255 * 4. Flush TLB's if needed.
1256 */
749cf76c
CD
1257int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1258{
8fe65a82 1259 bool flush = false;
53c810c3
MS
1260 int r;
1261
1262 mutex_lock(&kvm->slots_lock);
1263
8fe65a82 1264 r = kvm_get_dirty_log_protect(kvm, log, &flush);
53c810c3 1265
8fe65a82 1266 if (flush)
53c810c3
MS
1267 kvm_flush_remote_tlbs(kvm);
1268
1269 mutex_unlock(&kvm->slots_lock);
1270 return r;
749cf76c
CD
1271}
1272
2a31b9db
PB
1273int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
1274{
1275 bool flush = false;
53c810c3
MS
1276 int r;
1277
1278 mutex_lock(&kvm->slots_lock);
1279
2a31b9db 1280 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
53c810c3 1281
2a31b9db 1282 if (flush)
53c810c3
MS
1283 kvm_flush_remote_tlbs(kvm);
1284
1285 mutex_unlock(&kvm->slots_lock);
1286 return r;
749cf76c
CD
1287}
1288
3401d546
CD
1289static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1290 struct kvm_arm_device_addr *dev_addr)
1291{
330690cd
CD
1292 unsigned long dev_id, type;
1293
1294 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1295 KVM_ARM_DEVICE_ID_SHIFT;
1296 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1297 KVM_ARM_DEVICE_TYPE_SHIFT;
1298
1299 switch (dev_id) {
1300 case KVM_ARM_DEVICE_VGIC_V2:
c7da6fa4
PF
1301 if (!vgic_present)
1302 return -ENXIO;
ce01e4e8 1303 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
330690cd
CD
1304 default:
1305 return -ENODEV;
1306 }
3401d546
CD
1307}
1308
749cf76c
CD
1309long kvm_arch_vm_ioctl(struct file *filp,
1310 unsigned int ioctl, unsigned long arg)
1311{
3401d546
CD
1312 struct kvm *kvm = filp->private_data;
1313 void __user *argp = (void __user *)arg;
1314
1315 switch (ioctl) {
5863c2ce 1316 case KVM_CREATE_IRQCHIP: {
a28ebea2 1317 int ret;
c7da6fa4
PF
1318 if (!vgic_present)
1319 return -ENXIO;
a28ebea2
CD
1320 mutex_lock(&kvm->lock);
1321 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1322 mutex_unlock(&kvm->lock);
1323 return ret;
5863c2ce 1324 }
3401d546
CD
1325 case KVM_ARM_SET_DEVICE_ADDR: {
1326 struct kvm_arm_device_addr dev_addr;
1327
1328 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1329 return -EFAULT;
1330 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1331 }
42c4e0c7
AP
1332 case KVM_ARM_PREFERRED_TARGET: {
1333 int err;
1334 struct kvm_vcpu_init init;
1335
1336 err = kvm_vcpu_preferred_target(&init);
1337 if (err)
1338 return err;
1339
1340 if (copy_to_user(argp, &init, sizeof(init)))
1341 return -EFAULT;
1342
1343 return 0;
1344 }
3401d546
CD
1345 default:
1346 return -EINVAL;
1347 }
749cf76c
CD
1348}
1349
7e0befd5 1350static void cpu_init_hyp_mode(void)
342cd0ab 1351{
dac288f7 1352 phys_addr_t pgd_ptr;
342cd0ab
CD
1353 unsigned long hyp_stack_ptr;
1354 unsigned long stack_page;
1355 unsigned long vector_ptr;
1356
1357 /* Switch from the HYP stub to our own HYP init vector */
5a677ce0 1358 __hyp_set_vectors(kvm_get_idmap_vector());
342cd0ab 1359
dac288f7 1360 pgd_ptr = kvm_mmu_get_httbr();
1436c1aa 1361 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
342cd0ab 1362 hyp_stack_ptr = stack_page + PAGE_SIZE;
6840bdd7 1363 vector_ptr = (unsigned long)kvm_get_hyp_vector();
342cd0ab 1364
12fda812 1365 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
35a2491a 1366 __cpu_init_stage2();
342cd0ab
CD
1367}
1368
47eb3cba
MZ
1369static void cpu_hyp_reset(void)
1370{
1371 if (!is_kernel_in_hyp_mode())
1372 __hyp_reset_vectors();
1373}
1374
5f5560b1
JM
1375static void cpu_hyp_reinit(void)
1376{
1e0cf16c
MZ
1377 kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt);
1378
47eb3cba
MZ
1379 cpu_hyp_reset();
1380
9d47bb0d 1381 if (is_kernel_in_hyp_mode())
02d50cda 1382 kvm_timer_init_vhe();
9d47bb0d 1383 else
7e0befd5 1384 cpu_init_hyp_mode();
5b0d2cc2 1385
da5a3ce6 1386 kvm_arm_init_debug();
5b0d2cc2
CD
1387
1388 if (vgic_present)
1389 kvm_vgic_init_cpu_hardware();
5f5560b1
JM
1390}
1391
67f69197
AT
1392static void _kvm_arch_hardware_enable(void *discard)
1393{
1394 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
5f5560b1 1395 cpu_hyp_reinit();
67f69197 1396 __this_cpu_write(kvm_arm_hardware_enabled, 1);
d157f4a5 1397 }
67f69197 1398}
d157f4a5 1399
67f69197
AT
1400int kvm_arch_hardware_enable(void)
1401{
1402 _kvm_arch_hardware_enable(NULL);
1403 return 0;
342cd0ab
CD
1404}
1405
67f69197
AT
1406static void _kvm_arch_hardware_disable(void *discard)
1407{
1408 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1409 cpu_hyp_reset();
1410 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1411 }
1412}
1413
1414void kvm_arch_hardware_disable(void)
1415{
1416 _kvm_arch_hardware_disable(NULL);
1417}
d157f4a5 1418
1fcf7ce0
LP
1419#ifdef CONFIG_CPU_PM
1420static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1421 unsigned long cmd,
1422 void *v)
1423{
67f69197
AT
1424 /*
1425 * kvm_arm_hardware_enabled is left with its old value over
1426 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1427 * re-enable hyp.
1428 */
1429 switch (cmd) {
1430 case CPU_PM_ENTER:
1431 if (__this_cpu_read(kvm_arm_hardware_enabled))
1432 /*
1433 * don't update kvm_arm_hardware_enabled here
1434 * so that the hardware will be re-enabled
1435 * when we resume. See below.
1436 */
1437 cpu_hyp_reset();
1438
1fcf7ce0 1439 return NOTIFY_OK;
58d6b15e 1440 case CPU_PM_ENTER_FAILED:
67f69197
AT
1441 case CPU_PM_EXIT:
1442 if (__this_cpu_read(kvm_arm_hardware_enabled))
1443 /* The hardware was enabled before suspend. */
1444 cpu_hyp_reinit();
1fcf7ce0 1445
67f69197
AT
1446 return NOTIFY_OK;
1447
1448 default:
1449 return NOTIFY_DONE;
1450 }
1fcf7ce0
LP
1451}
1452
1453static struct notifier_block hyp_init_cpu_pm_nb = {
1454 .notifier_call = hyp_init_cpu_pm_notifier,
1455};
1456
1457static void __init hyp_cpu_pm_init(void)
1458{
1459 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1460}
06a71a24
SH
1461static void __init hyp_cpu_pm_exit(void)
1462{
1463 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1464}
1fcf7ce0
LP
1465#else
1466static inline void hyp_cpu_pm_init(void)
1467{
1468}
06a71a24
SH
1469static inline void hyp_cpu_pm_exit(void)
1470{
1471}
1fcf7ce0
LP
1472#endif
1473
1e947bad
MZ
1474static int init_common_resources(void)
1475{
0f62f0e9
SP
1476 kvm_set_ipa_limit();
1477
1e947bad
MZ
1478 return 0;
1479}
1480
1481static int init_subsystems(void)
1482{
67f69197 1483 int err = 0;
1e947bad 1484
5f5560b1 1485 /*
67f69197 1486 * Enable hardware so that subsystem initialisation can access EL2.
5f5560b1 1487 */
67f69197 1488 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
5f5560b1
JM
1489
1490 /*
1491 * Register CPU lower-power notifier
1492 */
1493 hyp_cpu_pm_init();
1494
1e947bad
MZ
1495 /*
1496 * Init HYP view of VGIC
1497 */
1498 err = kvm_vgic_hyp_init();
1499 switch (err) {
1500 case 0:
1501 vgic_present = true;
1502 break;
1503 case -ENODEV:
1504 case -ENXIO:
1505 vgic_present = false;
67f69197 1506 err = 0;
1e947bad
MZ
1507 break;
1508 default:
67f69197 1509 goto out;
1e947bad
MZ
1510 }
1511
1512 /*
1513 * Init HYP architected timer support
1514 */
f384dcfe 1515 err = kvm_timer_hyp_init(vgic_present);
1e947bad 1516 if (err)
67f69197 1517 goto out;
1e947bad
MZ
1518
1519 kvm_perf_init();
1520 kvm_coproc_table_init();
1521
67f69197
AT
1522out:
1523 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1524
1525 return err;
1e947bad
MZ
1526}
1527
1528static void teardown_hyp_mode(void)
1529{
1530 int cpu;
1531
1e947bad
MZ
1532 free_hyp_pgds();
1533 for_each_possible_cpu(cpu)
1534 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
06a71a24 1535 hyp_cpu_pm_exit();
1e947bad
MZ
1536}
1537
342cd0ab
CD
1538/**
1539 * Inits Hyp-mode on all online CPUs
1540 */
1541static int init_hyp_mode(void)
1542{
342cd0ab
CD
1543 int cpu;
1544 int err = 0;
1545
1546 /*
1547 * Allocate Hyp PGD and setup Hyp identity mapping
1548 */
1549 err = kvm_mmu_init();
1550 if (err)
1551 goto out_err;
1552
342cd0ab
CD
1553 /*
1554 * Allocate stack pages for Hypervisor-mode
1555 */
1556 for_each_possible_cpu(cpu) {
1557 unsigned long stack_page;
1558
1559 stack_page = __get_free_page(GFP_KERNEL);
1560 if (!stack_page) {
1561 err = -ENOMEM;
1e947bad 1562 goto out_err;
342cd0ab
CD
1563 }
1564
1565 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1566 }
1567
342cd0ab
CD
1568 /*
1569 * Map the Hyp-code called directly from the host
1570 */
588ab3f9 1571 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
59002705 1572 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
342cd0ab
CD
1573 if (err) {
1574 kvm_err("Cannot map world-switch code\n");
1e947bad 1575 goto out_err;
342cd0ab
CD
1576 }
1577
a0bf9776 1578 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
74a6b888 1579 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
910917bb
MZ
1580 if (err) {
1581 kvm_err("Cannot map rodata section\n");
c8ea0395
MZ
1582 goto out_err;
1583 }
1584
1585 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1586 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1587 if (err) {
1588 kvm_err("Cannot map bss section\n");
1e947bad 1589 goto out_err;
910917bb
MZ
1590 }
1591
6840bdd7
MZ
1592 err = kvm_map_vectors();
1593 if (err) {
1594 kvm_err("Cannot map vectors\n");
1595 goto out_err;
1596 }
1597
342cd0ab
CD
1598 /*
1599 * Map the Hyp stack pages
1600 */
1601 for_each_possible_cpu(cpu) {
1602 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
c8dddecd
MZ
1603 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1604 PAGE_HYP);
342cd0ab
CD
1605
1606 if (err) {
1607 kvm_err("Cannot map hyp stack\n");
1e947bad 1608 goto out_err;
342cd0ab
CD
1609 }
1610 }
1611
342cd0ab 1612 for_each_possible_cpu(cpu) {
630a1685 1613 kvm_host_data_t *cpu_data;
342cd0ab 1614
630a1685 1615 cpu_data = per_cpu_ptr(&kvm_host_data, cpu);
630a1685 1616 err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP);
342cd0ab
CD
1617
1618 if (err) {
3de50da6 1619 kvm_err("Cannot map host CPU state: %d\n", err);
1e947bad 1620 goto out_err;
342cd0ab
CD
1621 }
1622 }
1623
55e3748e
MZ
1624 err = hyp_map_aux_data();
1625 if (err)
a37f0c3c 1626 kvm_err("Cannot map host auxiliary data: %d\n", err);
55e3748e 1627
342cd0ab 1628 return 0;
1e947bad 1629
342cd0ab 1630out_err:
1e947bad 1631 teardown_hyp_mode();
342cd0ab
CD
1632 kvm_err("error initializing Hyp mode: %d\n", err);
1633 return err;
1634}
1635
d4e071ce
AP
1636static void check_kvm_target_cpu(void *ret)
1637{
1638 *(int *)ret = kvm_target_cpu();
1639}
1640
4429fc64
AP
1641struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1642{
1643 struct kvm_vcpu *vcpu;
1644 int i;
1645
1646 mpidr &= MPIDR_HWID_BITMASK;
1647 kvm_for_each_vcpu(i, vcpu, kvm) {
1648 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1649 return vcpu;
1650 }
1651 return NULL;
1652}
1653
2412405b
EA
1654bool kvm_arch_has_irq_bypass(void)
1655{
1656 return true;
1657}
1658
1659int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
1660 struct irq_bypass_producer *prod)
1661{
1662 struct kvm_kernel_irqfd *irqfd =
1663 container_of(cons, struct kvm_kernel_irqfd, consumer);
1664
196b1364
MZ
1665 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
1666 &irqfd->irq_entry);
2412405b
EA
1667}
1668void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
1669 struct irq_bypass_producer *prod)
1670{
1671 struct kvm_kernel_irqfd *irqfd =
1672 container_of(cons, struct kvm_kernel_irqfd, consumer);
1673
196b1364
MZ
1674 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
1675 &irqfd->irq_entry);
2412405b
EA
1676}
1677
1678void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
1679{
1680 struct kvm_kernel_irqfd *irqfd =
1681 container_of(cons, struct kvm_kernel_irqfd, consumer);
1682
1683 kvm_arm_halt_guest(irqfd->kvm);
1684}
1685
1686void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
1687{
1688 struct kvm_kernel_irqfd *irqfd =
1689 container_of(cons, struct kvm_kernel_irqfd, consumer);
1690
1691 kvm_arm_resume_guest(irqfd->kvm);
1692}
1693
342cd0ab
CD
1694/**
1695 * Initialize Hyp-mode and memory mappings on all CPUs.
1696 */
749cf76c
CD
1697int kvm_arch_init(void *opaque)
1698{
342cd0ab 1699 int err;
d4e071ce 1700 int ret, cpu;
fe7d7b03 1701 bool in_hyp_mode;
342cd0ab
CD
1702
1703 if (!is_hyp_mode_available()) {
58d0d19a 1704 kvm_info("HYP mode not available\n");
342cd0ab
CD
1705 return -ENODEV;
1706 }
1707
33e5f4e5
MZ
1708 in_hyp_mode = is_kernel_in_hyp_mode();
1709
1710 if (!in_hyp_mode && kvm_arch_requires_vhe()) {
1711 kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n");
85acda3b
DM
1712 return -ENODEV;
1713 }
1714
d4e071ce
AP
1715 for_each_online_cpu(cpu) {
1716 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1717 if (ret < 0) {
1718 kvm_err("Error, CPU %d not supported!\n", cpu);
1719 return -ENODEV;
1720 }
342cd0ab
CD
1721 }
1722
1e947bad 1723 err = init_common_resources();
342cd0ab 1724 if (err)
1e947bad 1725 return err;
342cd0ab 1726
a3be836d 1727 err = kvm_arm_init_sve();
0f062bfe
DM
1728 if (err)
1729 return err;
1730
fe7d7b03 1731 if (!in_hyp_mode) {
1e947bad 1732 err = init_hyp_mode();
fe7d7b03
JT
1733 if (err)
1734 goto out_err;
1735 }
8146875d 1736
1e947bad
MZ
1737 err = init_subsystems();
1738 if (err)
1739 goto out_hyp;
1fcf7ce0 1740
fe7d7b03
JT
1741 if (in_hyp_mode)
1742 kvm_info("VHE mode initialized successfully\n");
1743 else
1744 kvm_info("Hyp mode initialized successfully\n");
1745
749cf76c 1746 return 0;
1e947bad
MZ
1747
1748out_hyp:
fe7d7b03
JT
1749 if (!in_hyp_mode)
1750 teardown_hyp_mode();
342cd0ab
CD
1751out_err:
1752 return err;
749cf76c
CD
1753}
1754
1755/* NOP: Compiling as a module not supported */
1756void kvm_arch_exit(void)
1757{
210552c1 1758 kvm_perf_teardown();
749cf76c
CD
1759}
1760
1761static int arm_init(void)
1762{
1763 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1764 return rc;
1765}
1766
1767module_init(arm_init);