Merge branch kvm-arm64/nv-trap-fixes into kvmarm/next
[linux-2.6-microblaze.git] / arch / arm64 / kvm / arm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5  */
6
7 #include <linux/bug.h>
8 #include <linux/cpu_pm.h>
9 #include <linux/entry-kvm.h>
10 #include <linux/errno.h>
11 #include <linux/err.h>
12 #include <linux/kvm_host.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/vmalloc.h>
16 #include <linux/fs.h>
17 #include <linux/mman.h>
18 #include <linux/sched.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_irqfd.h>
21 #include <linux/irqbypass.h>
22 #include <linux/sched/stat.h>
23 #include <linux/psci.h>
24 #include <trace/events/kvm.h>
25
26 #define CREATE_TRACE_POINTS
27 #include "trace_arm.h"
28
29 #include <linux/uaccess.h>
30 #include <asm/ptrace.h>
31 #include <asm/mman.h>
32 #include <asm/tlbflush.h>
33 #include <asm/cacheflush.h>
34 #include <asm/cpufeature.h>
35 #include <asm/virt.h>
36 #include <asm/kvm_arm.h>
37 #include <asm/kvm_asm.h>
38 #include <asm/kvm_mmu.h>
39 #include <asm/kvm_nested.h>
40 #include <asm/kvm_pkvm.h>
41 #include <asm/kvm_emulate.h>
42 #include <asm/sections.h>
43
44 #include <kvm/arm_hypercalls.h>
45 #include <kvm/arm_pmu.h>
46 #include <kvm/arm_psci.h>
47
48 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
49
50 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
51
52 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
53 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
54
55 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
56
57 static bool vgic_present, kvm_arm_initialised;
58
59 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
60 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
61
62 bool is_kvm_arm_initialised(void)
63 {
64         return kvm_arm_initialised;
65 }
66
67 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
68 {
69         return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
70 }
71
72 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
73                             struct kvm_enable_cap *cap)
74 {
75         int r;
76         u64 new_cap;
77
78         if (cap->flags)
79                 return -EINVAL;
80
81         switch (cap->cap) {
82         case KVM_CAP_ARM_NISV_TO_USER:
83                 r = 0;
84                 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
85                         &kvm->arch.flags);
86                 break;
87         case KVM_CAP_ARM_MTE:
88                 mutex_lock(&kvm->lock);
89                 if (!system_supports_mte() || kvm->created_vcpus) {
90                         r = -EINVAL;
91                 } else {
92                         r = 0;
93                         set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
94                 }
95                 mutex_unlock(&kvm->lock);
96                 break;
97         case KVM_CAP_ARM_SYSTEM_SUSPEND:
98                 r = 0;
99                 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
100                 break;
101         case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
102                 new_cap = cap->args[0];
103
104                 mutex_lock(&kvm->slots_lock);
105                 /*
106                  * To keep things simple, allow changing the chunk
107                  * size only when no memory slots have been created.
108                  */
109                 if (!kvm_are_all_memslots_empty(kvm)) {
110                         r = -EINVAL;
111                 } else if (new_cap && !kvm_is_block_size_supported(new_cap)) {
112                         r = -EINVAL;
113                 } else {
114                         r = 0;
115                         kvm->arch.mmu.split_page_chunk_size = new_cap;
116                 }
117                 mutex_unlock(&kvm->slots_lock);
118                 break;
119         default:
120                 r = -EINVAL;
121                 break;
122         }
123
124         return r;
125 }
126
127 static int kvm_arm_default_max_vcpus(void)
128 {
129         return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
130 }
131
132 /**
133  * kvm_arch_init_vm - initializes a VM data structure
134  * @kvm:        pointer to the KVM struct
135  */
136 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
137 {
138         int ret;
139
140         mutex_init(&kvm->arch.config_lock);
141
142 #ifdef CONFIG_LOCKDEP
143         /* Clue in lockdep that the config_lock must be taken inside kvm->lock */
144         mutex_lock(&kvm->lock);
145         mutex_lock(&kvm->arch.config_lock);
146         mutex_unlock(&kvm->arch.config_lock);
147         mutex_unlock(&kvm->lock);
148 #endif
149
150         ret = kvm_share_hyp(kvm, kvm + 1);
151         if (ret)
152                 return ret;
153
154         ret = pkvm_init_host_vm(kvm);
155         if (ret)
156                 goto err_unshare_kvm;
157
158         if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
159                 ret = -ENOMEM;
160                 goto err_unshare_kvm;
161         }
162         cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
163
164         ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
165         if (ret)
166                 goto err_free_cpumask;
167
168         kvm_vgic_early_init(kvm);
169
170         kvm_timer_init_vm(kvm);
171
172         /* The maximum number of VCPUs is limited by the host's GIC model */
173         kvm->max_vcpus = kvm_arm_default_max_vcpus();
174
175         kvm_arm_init_hypercalls(kvm);
176
177         bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
178
179         return 0;
180
181 err_free_cpumask:
182         free_cpumask_var(kvm->arch.supported_cpus);
183 err_unshare_kvm:
184         kvm_unshare_hyp(kvm, kvm + 1);
185         return ret;
186 }
187
188 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
189 {
190         return VM_FAULT_SIGBUS;
191 }
192
193
194 /**
195  * kvm_arch_destroy_vm - destroy the VM data structure
196  * @kvm:        pointer to the KVM struct
197  */
198 void kvm_arch_destroy_vm(struct kvm *kvm)
199 {
200         bitmap_free(kvm->arch.pmu_filter);
201         free_cpumask_var(kvm->arch.supported_cpus);
202
203         kvm_vgic_destroy(kvm);
204
205         if (is_protected_kvm_enabled())
206                 pkvm_destroy_hyp_vm(kvm);
207
208         kvm_destroy_vcpus(kvm);
209
210         kvm_unshare_hyp(kvm, kvm + 1);
211
212         kvm_arm_teardown_hypercalls(kvm);
213 }
214
215 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
216 {
217         int r;
218         switch (ext) {
219         case KVM_CAP_IRQCHIP:
220                 r = vgic_present;
221                 break;
222         case KVM_CAP_IOEVENTFD:
223         case KVM_CAP_DEVICE_CTRL:
224         case KVM_CAP_USER_MEMORY:
225         case KVM_CAP_SYNC_MMU:
226         case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
227         case KVM_CAP_ONE_REG:
228         case KVM_CAP_ARM_PSCI:
229         case KVM_CAP_ARM_PSCI_0_2:
230         case KVM_CAP_READONLY_MEM:
231         case KVM_CAP_MP_STATE:
232         case KVM_CAP_IMMEDIATE_EXIT:
233         case KVM_CAP_VCPU_EVENTS:
234         case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
235         case KVM_CAP_ARM_NISV_TO_USER:
236         case KVM_CAP_ARM_INJECT_EXT_DABT:
237         case KVM_CAP_SET_GUEST_DEBUG:
238         case KVM_CAP_VCPU_ATTRIBUTES:
239         case KVM_CAP_PTP_KVM:
240         case KVM_CAP_ARM_SYSTEM_SUSPEND:
241         case KVM_CAP_IRQFD_RESAMPLE:
242         case KVM_CAP_COUNTER_OFFSET:
243                 r = 1;
244                 break;
245         case KVM_CAP_SET_GUEST_DEBUG2:
246                 return KVM_GUESTDBG_VALID_MASK;
247         case KVM_CAP_ARM_SET_DEVICE_ADDR:
248                 r = 1;
249                 break;
250         case KVM_CAP_NR_VCPUS:
251                 /*
252                  * ARM64 treats KVM_CAP_NR_CPUS differently from all other
253                  * architectures, as it does not always bound it to
254                  * KVM_CAP_MAX_VCPUS. It should not matter much because
255                  * this is just an advisory value.
256                  */
257                 r = min_t(unsigned int, num_online_cpus(),
258                           kvm_arm_default_max_vcpus());
259                 break;
260         case KVM_CAP_MAX_VCPUS:
261         case KVM_CAP_MAX_VCPU_ID:
262                 if (kvm)
263                         r = kvm->max_vcpus;
264                 else
265                         r = kvm_arm_default_max_vcpus();
266                 break;
267         case KVM_CAP_MSI_DEVID:
268                 if (!kvm)
269                         r = -EINVAL;
270                 else
271                         r = kvm->arch.vgic.msis_require_devid;
272                 break;
273         case KVM_CAP_ARM_USER_IRQ:
274                 /*
275                  * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
276                  * (bump this number if adding more devices)
277                  */
278                 r = 1;
279                 break;
280         case KVM_CAP_ARM_MTE:
281                 r = system_supports_mte();
282                 break;
283         case KVM_CAP_STEAL_TIME:
284                 r = kvm_arm_pvtime_supported();
285                 break;
286         case KVM_CAP_ARM_EL1_32BIT:
287                 r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
288                 break;
289         case KVM_CAP_GUEST_DEBUG_HW_BPS:
290                 r = get_num_brps();
291                 break;
292         case KVM_CAP_GUEST_DEBUG_HW_WPS:
293                 r = get_num_wrps();
294                 break;
295         case KVM_CAP_ARM_PMU_V3:
296                 r = kvm_arm_support_pmu_v3();
297                 break;
298         case KVM_CAP_ARM_INJECT_SERROR_ESR:
299                 r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
300                 break;
301         case KVM_CAP_ARM_VM_IPA_SIZE:
302                 r = get_kvm_ipa_limit();
303                 break;
304         case KVM_CAP_ARM_SVE:
305                 r = system_supports_sve();
306                 break;
307         case KVM_CAP_ARM_PTRAUTH_ADDRESS:
308         case KVM_CAP_ARM_PTRAUTH_GENERIC:
309                 r = system_has_full_ptr_auth();
310                 break;
311         case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
312                 if (kvm)
313                         r = kvm->arch.mmu.split_page_chunk_size;
314                 else
315                         r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
316                 break;
317         case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
318                 r = kvm_supported_block_sizes();
319                 break;
320         default:
321                 r = 0;
322         }
323
324         return r;
325 }
326
327 long kvm_arch_dev_ioctl(struct file *filp,
328                         unsigned int ioctl, unsigned long arg)
329 {
330         return -EINVAL;
331 }
332
333 struct kvm *kvm_arch_alloc_vm(void)
334 {
335         size_t sz = sizeof(struct kvm);
336
337         if (!has_vhe())
338                 return kzalloc(sz, GFP_KERNEL_ACCOUNT);
339
340         return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
341 }
342
343 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
344 {
345         if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
346                 return -EBUSY;
347
348         if (id >= kvm->max_vcpus)
349                 return -EINVAL;
350
351         return 0;
352 }
353
354 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
355 {
356         int err;
357
358         spin_lock_init(&vcpu->arch.mp_state_lock);
359
360 #ifdef CONFIG_LOCKDEP
361         /* Inform lockdep that the config_lock is acquired after vcpu->mutex */
362         mutex_lock(&vcpu->mutex);
363         mutex_lock(&vcpu->kvm->arch.config_lock);
364         mutex_unlock(&vcpu->kvm->arch.config_lock);
365         mutex_unlock(&vcpu->mutex);
366 #endif
367
368         /* Force users to call KVM_ARM_VCPU_INIT */
369         vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
370
371         vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
372
373         /*
374          * Default value for the FP state, will be overloaded at load
375          * time if we support FP (pretty likely)
376          */
377         vcpu->arch.fp_state = FP_STATE_FREE;
378
379         /* Set up the timer */
380         kvm_timer_vcpu_init(vcpu);
381
382         kvm_pmu_vcpu_init(vcpu);
383
384         kvm_arm_reset_debug_ptr(vcpu);
385
386         kvm_arm_pvtime_vcpu_init(&vcpu->arch);
387
388         vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
389
390         err = kvm_vgic_vcpu_init(vcpu);
391         if (err)
392                 return err;
393
394         return kvm_share_hyp(vcpu, vcpu + 1);
395 }
396
397 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
398 {
399 }
400
401 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
402 {
403         if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
404                 static_branch_dec(&userspace_irqchip_in_use);
405
406         kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
407         kvm_timer_vcpu_terminate(vcpu);
408         kvm_pmu_vcpu_destroy(vcpu);
409
410         kvm_arm_vcpu_destroy(vcpu);
411 }
412
413 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
414 {
415
416 }
417
418 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
419 {
420
421 }
422
423 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
424 {
425         struct kvm_s2_mmu *mmu;
426         int *last_ran;
427
428         mmu = vcpu->arch.hw_mmu;
429         last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
430
431         /*
432          * We guarantee that both TLBs and I-cache are private to each
433          * vcpu. If detecting that a vcpu from the same VM has
434          * previously run on the same physical CPU, call into the
435          * hypervisor code to nuke the relevant contexts.
436          *
437          * We might get preempted before the vCPU actually runs, but
438          * over-invalidation doesn't affect correctness.
439          */
440         if (*last_ran != vcpu->vcpu_id) {
441                 kvm_call_hyp(__kvm_flush_cpu_context, mmu);
442                 *last_ran = vcpu->vcpu_id;
443         }
444
445         vcpu->cpu = cpu;
446
447         kvm_vgic_load(vcpu);
448         kvm_timer_vcpu_load(vcpu);
449         if (has_vhe())
450                 kvm_vcpu_load_sysregs_vhe(vcpu);
451         kvm_arch_vcpu_load_fp(vcpu);
452         kvm_vcpu_pmu_restore_guest(vcpu);
453         if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
454                 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
455
456         if (single_task_running())
457                 vcpu_clear_wfx_traps(vcpu);
458         else
459                 vcpu_set_wfx_traps(vcpu);
460
461         if (vcpu_has_ptrauth(vcpu))
462                 vcpu_ptrauth_disable(vcpu);
463         kvm_arch_vcpu_load_debug_state_flags(vcpu);
464
465         if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
466                 vcpu_set_on_unsupported_cpu(vcpu);
467 }
468
469 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
470 {
471         kvm_arch_vcpu_put_debug_state_flags(vcpu);
472         kvm_arch_vcpu_put_fp(vcpu);
473         if (has_vhe())
474                 kvm_vcpu_put_sysregs_vhe(vcpu);
475         kvm_timer_vcpu_put(vcpu);
476         kvm_vgic_put(vcpu);
477         kvm_vcpu_pmu_restore_host(vcpu);
478         kvm_arm_vmid_clear_active();
479
480         vcpu_clear_on_unsupported_cpu(vcpu);
481         vcpu->cpu = -1;
482 }
483
484 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
485 {
486         WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
487         kvm_make_request(KVM_REQ_SLEEP, vcpu);
488         kvm_vcpu_kick(vcpu);
489 }
490
491 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
492 {
493         spin_lock(&vcpu->arch.mp_state_lock);
494         __kvm_arm_vcpu_power_off(vcpu);
495         spin_unlock(&vcpu->arch.mp_state_lock);
496 }
497
498 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
499 {
500         return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
501 }
502
503 static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
504 {
505         WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
506         kvm_make_request(KVM_REQ_SUSPEND, vcpu);
507         kvm_vcpu_kick(vcpu);
508 }
509
510 static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
511 {
512         return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
513 }
514
515 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
516                                     struct kvm_mp_state *mp_state)
517 {
518         *mp_state = READ_ONCE(vcpu->arch.mp_state);
519
520         return 0;
521 }
522
523 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
524                                     struct kvm_mp_state *mp_state)
525 {
526         int ret = 0;
527
528         spin_lock(&vcpu->arch.mp_state_lock);
529
530         switch (mp_state->mp_state) {
531         case KVM_MP_STATE_RUNNABLE:
532                 WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
533                 break;
534         case KVM_MP_STATE_STOPPED:
535                 __kvm_arm_vcpu_power_off(vcpu);
536                 break;
537         case KVM_MP_STATE_SUSPENDED:
538                 kvm_arm_vcpu_suspend(vcpu);
539                 break;
540         default:
541                 ret = -EINVAL;
542         }
543
544         spin_unlock(&vcpu->arch.mp_state_lock);
545
546         return ret;
547 }
548
549 /**
550  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
551  * @v:          The VCPU pointer
552  *
553  * If the guest CPU is not waiting for interrupts or an interrupt line is
554  * asserted, the CPU is by definition runnable.
555  */
556 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
557 {
558         bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
559         return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
560                 && !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
561 }
562
563 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
564 {
565         return vcpu_mode_priv(vcpu);
566 }
567
568 #ifdef CONFIG_GUEST_PERF_EVENTS
569 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
570 {
571         return *vcpu_pc(vcpu);
572 }
573 #endif
574
575 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
576 {
577         return vcpu_get_flag(vcpu, VCPU_INITIALIZED);
578 }
579
580 /*
581  * Handle both the initialisation that is being done when the vcpu is
582  * run for the first time, as well as the updates that must be
583  * performed each time we get a new thread dealing with this vcpu.
584  */
585 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
586 {
587         struct kvm *kvm = vcpu->kvm;
588         int ret;
589
590         if (!kvm_vcpu_initialized(vcpu))
591                 return -ENOEXEC;
592
593         if (!kvm_arm_vcpu_is_finalized(vcpu))
594                 return -EPERM;
595
596         ret = kvm_arch_vcpu_run_map_fp(vcpu);
597         if (ret)
598                 return ret;
599
600         if (likely(vcpu_has_run_once(vcpu)))
601                 return 0;
602
603         kvm_arm_vcpu_init_debug(vcpu);
604
605         if (likely(irqchip_in_kernel(kvm))) {
606                 /*
607                  * Map the VGIC hardware resources before running a vcpu the
608                  * first time on this VM.
609                  */
610                 ret = kvm_vgic_map_resources(kvm);
611                 if (ret)
612                         return ret;
613         }
614
615         ret = kvm_timer_enable(vcpu);
616         if (ret)
617                 return ret;
618
619         ret = kvm_arm_pmu_v3_enable(vcpu);
620         if (ret)
621                 return ret;
622
623         if (is_protected_kvm_enabled()) {
624                 ret = pkvm_create_hyp_vm(kvm);
625                 if (ret)
626                         return ret;
627         }
628
629         if (!irqchip_in_kernel(kvm)) {
630                 /*
631                  * Tell the rest of the code that there are userspace irqchip
632                  * VMs in the wild.
633                  */
634                 static_branch_inc(&userspace_irqchip_in_use);
635         }
636
637         /*
638          * Initialize traps for protected VMs.
639          * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
640          * the code is in place for first run initialization at EL2.
641          */
642         if (kvm_vm_is_protected(kvm))
643                 kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
644
645         mutex_lock(&kvm->arch.config_lock);
646         set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
647         mutex_unlock(&kvm->arch.config_lock);
648
649         return ret;
650 }
651
652 bool kvm_arch_intc_initialized(struct kvm *kvm)
653 {
654         return vgic_initialized(kvm);
655 }
656
657 void kvm_arm_halt_guest(struct kvm *kvm)
658 {
659         unsigned long i;
660         struct kvm_vcpu *vcpu;
661
662         kvm_for_each_vcpu(i, vcpu, kvm)
663                 vcpu->arch.pause = true;
664         kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
665 }
666
667 void kvm_arm_resume_guest(struct kvm *kvm)
668 {
669         unsigned long i;
670         struct kvm_vcpu *vcpu;
671
672         kvm_for_each_vcpu(i, vcpu, kvm) {
673                 vcpu->arch.pause = false;
674                 __kvm_vcpu_wake_up(vcpu);
675         }
676 }
677
678 static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
679 {
680         struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
681
682         rcuwait_wait_event(wait,
683                            (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
684                            TASK_INTERRUPTIBLE);
685
686         if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
687                 /* Awaken to handle a signal, request we sleep again later. */
688                 kvm_make_request(KVM_REQ_SLEEP, vcpu);
689         }
690
691         /*
692          * Make sure we will observe a potential reset request if we've
693          * observed a change to the power state. Pairs with the smp_wmb() in
694          * kvm_psci_vcpu_on().
695          */
696         smp_rmb();
697 }
698
699 /**
700  * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
701  * @vcpu:       The VCPU pointer
702  *
703  * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
704  * the vCPU is runnable.  The vCPU may or may not be scheduled out, depending
705  * on when a wake event arrives, e.g. there may already be a pending wake event.
706  */
707 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
708 {
709         /*
710          * Sync back the state of the GIC CPU interface so that we have
711          * the latest PMR and group enables. This ensures that
712          * kvm_arch_vcpu_runnable has up-to-date data to decide whether
713          * we have pending interrupts, e.g. when determining if the
714          * vCPU should block.
715          *
716          * For the same reason, we want to tell GICv4 that we need
717          * doorbells to be signalled, should an interrupt become pending.
718          */
719         preempt_disable();
720         kvm_vgic_vmcr_sync(vcpu);
721         vcpu_set_flag(vcpu, IN_WFI);
722         vgic_v4_put(vcpu);
723         preempt_enable();
724
725         kvm_vcpu_halt(vcpu);
726         vcpu_clear_flag(vcpu, IN_WFIT);
727
728         preempt_disable();
729         vcpu_clear_flag(vcpu, IN_WFI);
730         vgic_v4_load(vcpu);
731         preempt_enable();
732 }
733
734 static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
735 {
736         if (!kvm_arm_vcpu_suspended(vcpu))
737                 return 1;
738
739         kvm_vcpu_wfi(vcpu);
740
741         /*
742          * The suspend state is sticky; we do not leave it until userspace
743          * explicitly marks the vCPU as runnable. Request that we suspend again
744          * later.
745          */
746         kvm_make_request(KVM_REQ_SUSPEND, vcpu);
747
748         /*
749          * Check to make sure the vCPU is actually runnable. If so, exit to
750          * userspace informing it of the wakeup condition.
751          */
752         if (kvm_arch_vcpu_runnable(vcpu)) {
753                 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
754                 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
755                 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
756                 return 0;
757         }
758
759         /*
760          * Otherwise, we were unblocked to process a different event, such as a
761          * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
762          * process the event.
763          */
764         return 1;
765 }
766
767 /**
768  * check_vcpu_requests - check and handle pending vCPU requests
769  * @vcpu:       the VCPU pointer
770  *
771  * Return: 1 if we should enter the guest
772  *         0 if we should exit to userspace
773  *         < 0 if we should exit to userspace, where the return value indicates
774  *         an error
775  */
776 static int check_vcpu_requests(struct kvm_vcpu *vcpu)
777 {
778         if (kvm_request_pending(vcpu)) {
779                 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
780                         kvm_vcpu_sleep(vcpu);
781
782                 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
783                         kvm_reset_vcpu(vcpu);
784
785                 /*
786                  * Clear IRQ_PENDING requests that were made to guarantee
787                  * that a VCPU sees new virtual interrupts.
788                  */
789                 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
790
791                 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
792                         kvm_update_stolen_time(vcpu);
793
794                 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
795                         /* The distributor enable bits were changed */
796                         preempt_disable();
797                         vgic_v4_put(vcpu);
798                         vgic_v4_load(vcpu);
799                         preempt_enable();
800                 }
801
802                 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
803                         kvm_pmu_handle_pmcr(vcpu,
804                                             __vcpu_sys_reg(vcpu, PMCR_EL0));
805
806                 if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu))
807                         kvm_vcpu_pmu_restore_guest(vcpu);
808
809                 if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
810                         return kvm_vcpu_suspend(vcpu);
811
812                 if (kvm_dirty_ring_check_request(vcpu))
813                         return 0;
814         }
815
816         return 1;
817 }
818
819 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
820 {
821         if (likely(!vcpu_mode_is_32bit(vcpu)))
822                 return false;
823
824         if (vcpu_has_nv(vcpu))
825                 return true;
826
827         return !kvm_supports_32bit_el0();
828 }
829
830 /**
831  * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
832  * @vcpu:       The VCPU pointer
833  * @ret:        Pointer to write optional return code
834  *
835  * Returns: true if the VCPU needs to return to a preemptible + interruptible
836  *          and skip guest entry.
837  *
838  * This function disambiguates between two different types of exits: exits to a
839  * preemptible + interruptible kernel context and exits to userspace. For an
840  * exit to userspace, this function will write the return code to ret and return
841  * true. For an exit to preemptible + interruptible kernel context (i.e. check
842  * for pending work and re-enter), return true without writing to ret.
843  */
844 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
845 {
846         struct kvm_run *run = vcpu->run;
847
848         /*
849          * If we're using a userspace irqchip, then check if we need
850          * to tell a userspace irqchip about timer or PMU level
851          * changes and if so, exit to userspace (the actual level
852          * state gets updated in kvm_timer_update_run and
853          * kvm_pmu_update_run below).
854          */
855         if (static_branch_unlikely(&userspace_irqchip_in_use)) {
856                 if (kvm_timer_should_notify_user(vcpu) ||
857                     kvm_pmu_should_notify_user(vcpu)) {
858                         *ret = -EINTR;
859                         run->exit_reason = KVM_EXIT_INTR;
860                         return true;
861                 }
862         }
863
864         if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
865                 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
866                 run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
867                 run->fail_entry.cpu = smp_processor_id();
868                 *ret = 0;
869                 return true;
870         }
871
872         return kvm_request_pending(vcpu) ||
873                         xfer_to_guest_mode_work_pending();
874 }
875
876 /*
877  * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
878  * the vCPU is running.
879  *
880  * This must be noinstr as instrumentation may make use of RCU, and this is not
881  * safe during the EQS.
882  */
883 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
884 {
885         int ret;
886
887         guest_state_enter_irqoff();
888         ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
889         guest_state_exit_irqoff();
890
891         return ret;
892 }
893
894 /**
895  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
896  * @vcpu:       The VCPU pointer
897  *
898  * This function is called through the VCPU_RUN ioctl called from user space. It
899  * will execute VM code in a loop until the time slice for the process is used
900  * or some emulation is needed from user space in which case the function will
901  * return with return value 0 and with the kvm_run structure filled in with the
902  * required data for the requested emulation.
903  */
904 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
905 {
906         struct kvm_run *run = vcpu->run;
907         int ret;
908
909         if (run->exit_reason == KVM_EXIT_MMIO) {
910                 ret = kvm_handle_mmio_return(vcpu);
911                 if (ret)
912                         return ret;
913         }
914
915         vcpu_load(vcpu);
916
917         if (run->immediate_exit) {
918                 ret = -EINTR;
919                 goto out;
920         }
921
922         kvm_sigset_activate(vcpu);
923
924         ret = 1;
925         run->exit_reason = KVM_EXIT_UNKNOWN;
926         run->flags = 0;
927         while (ret > 0) {
928                 /*
929                  * Check conditions before entering the guest
930                  */
931                 ret = xfer_to_guest_mode_handle_work(vcpu);
932                 if (!ret)
933                         ret = 1;
934
935                 if (ret > 0)
936                         ret = check_vcpu_requests(vcpu);
937
938                 /*
939                  * Preparing the interrupts to be injected also
940                  * involves poking the GIC, which must be done in a
941                  * non-preemptible context.
942                  */
943                 preempt_disable();
944
945                 /*
946                  * The VMID allocator only tracks active VMIDs per
947                  * physical CPU, and therefore the VMID allocated may not be
948                  * preserved on VMID roll-over if the task was preempted,
949                  * making a thread's VMID inactive. So we need to call
950                  * kvm_arm_vmid_update() in non-premptible context.
951                  */
952                 kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
953
954                 kvm_pmu_flush_hwstate(vcpu);
955
956                 local_irq_disable();
957
958                 kvm_vgic_flush_hwstate(vcpu);
959
960                 kvm_pmu_update_vcpu_events(vcpu);
961
962                 /*
963                  * Ensure we set mode to IN_GUEST_MODE after we disable
964                  * interrupts and before the final VCPU requests check.
965                  * See the comment in kvm_vcpu_exiting_guest_mode() and
966                  * Documentation/virt/kvm/vcpu-requests.rst
967                  */
968                 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
969
970                 if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
971                         vcpu->mode = OUTSIDE_GUEST_MODE;
972                         isb(); /* Ensure work in x_flush_hwstate is committed */
973                         kvm_pmu_sync_hwstate(vcpu);
974                         if (static_branch_unlikely(&userspace_irqchip_in_use))
975                                 kvm_timer_sync_user(vcpu);
976                         kvm_vgic_sync_hwstate(vcpu);
977                         local_irq_enable();
978                         preempt_enable();
979                         continue;
980                 }
981
982                 kvm_arm_setup_debug(vcpu);
983                 kvm_arch_vcpu_ctxflush_fp(vcpu);
984
985                 /**************************************************************
986                  * Enter the guest
987                  */
988                 trace_kvm_entry(*vcpu_pc(vcpu));
989                 guest_timing_enter_irqoff();
990
991                 ret = kvm_arm_vcpu_enter_exit(vcpu);
992
993                 vcpu->mode = OUTSIDE_GUEST_MODE;
994                 vcpu->stat.exits++;
995                 /*
996                  * Back from guest
997                  *************************************************************/
998
999                 kvm_arm_clear_debug(vcpu);
1000
1001                 /*
1002                  * We must sync the PMU state before the vgic state so
1003                  * that the vgic can properly sample the updated state of the
1004                  * interrupt line.
1005                  */
1006                 kvm_pmu_sync_hwstate(vcpu);
1007
1008                 /*
1009                  * Sync the vgic state before syncing the timer state because
1010                  * the timer code needs to know if the virtual timer
1011                  * interrupts are active.
1012                  */
1013                 kvm_vgic_sync_hwstate(vcpu);
1014
1015                 /*
1016                  * Sync the timer hardware state before enabling interrupts as
1017                  * we don't want vtimer interrupts to race with syncing the
1018                  * timer virtual interrupt state.
1019                  */
1020                 if (static_branch_unlikely(&userspace_irqchip_in_use))
1021                         kvm_timer_sync_user(vcpu);
1022
1023                 kvm_arch_vcpu_ctxsync_fp(vcpu);
1024
1025                 /*
1026                  * We must ensure that any pending interrupts are taken before
1027                  * we exit guest timing so that timer ticks are accounted as
1028                  * guest time. Transiently unmask interrupts so that any
1029                  * pending interrupts are taken.
1030                  *
1031                  * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
1032                  * context synchronization event) is necessary to ensure that
1033                  * pending interrupts are taken.
1034                  */
1035                 if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
1036                         local_irq_enable();
1037                         isb();
1038                         local_irq_disable();
1039                 }
1040
1041                 guest_timing_exit_irqoff();
1042
1043                 local_irq_enable();
1044
1045                 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1046
1047                 /* Exit types that need handling before we can be preempted */
1048                 handle_exit_early(vcpu, ret);
1049
1050                 preempt_enable();
1051
1052                 /*
1053                  * The ARMv8 architecture doesn't give the hypervisor
1054                  * a mechanism to prevent a guest from dropping to AArch32 EL0
1055                  * if implemented by the CPU. If we spot the guest in such
1056                  * state and that we decided it wasn't supposed to do so (like
1057                  * with the asymmetric AArch32 case), return to userspace with
1058                  * a fatal error.
1059                  */
1060                 if (vcpu_mode_is_bad_32bit(vcpu)) {
1061                         /*
1062                          * As we have caught the guest red-handed, decide that
1063                          * it isn't fit for purpose anymore by making the vcpu
1064                          * invalid. The VMM can try and fix it by issuing  a
1065                          * KVM_ARM_VCPU_INIT if it really wants to.
1066                          */
1067                         vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
1068                         ret = ARM_EXCEPTION_IL;
1069                 }
1070
1071                 ret = handle_exit(vcpu, ret);
1072         }
1073
1074         /* Tell userspace about in-kernel device output levels */
1075         if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
1076                 kvm_timer_update_run(vcpu);
1077                 kvm_pmu_update_run(vcpu);
1078         }
1079
1080         kvm_sigset_deactivate(vcpu);
1081
1082 out:
1083         /*
1084          * In the unlikely event that we are returning to userspace
1085          * with pending exceptions or PC adjustment, commit these
1086          * adjustments in order to give userspace a consistent view of
1087          * the vcpu state. Note that this relies on __kvm_adjust_pc()
1088          * being preempt-safe on VHE.
1089          */
1090         if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1091                      vcpu_get_flag(vcpu, INCREMENT_PC)))
1092                 kvm_call_hyp(__kvm_adjust_pc, vcpu);
1093
1094         vcpu_put(vcpu);
1095         return ret;
1096 }
1097
1098 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
1099 {
1100         int bit_index;
1101         bool set;
1102         unsigned long *hcr;
1103
1104         if (number == KVM_ARM_IRQ_CPU_IRQ)
1105                 bit_index = __ffs(HCR_VI);
1106         else /* KVM_ARM_IRQ_CPU_FIQ */
1107                 bit_index = __ffs(HCR_VF);
1108
1109         hcr = vcpu_hcr(vcpu);
1110         if (level)
1111                 set = test_and_set_bit(bit_index, hcr);
1112         else
1113                 set = test_and_clear_bit(bit_index, hcr);
1114
1115         /*
1116          * If we didn't change anything, no need to wake up or kick other CPUs
1117          */
1118         if (set == level)
1119                 return 0;
1120
1121         /*
1122          * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
1123          * trigger a world-switch round on the running physical CPU to set the
1124          * virtual IRQ/FIQ fields in the HCR appropriately.
1125          */
1126         kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
1127         kvm_vcpu_kick(vcpu);
1128
1129         return 0;
1130 }
1131
1132 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
1133                           bool line_status)
1134 {
1135         u32 irq = irq_level->irq;
1136         unsigned int irq_type, vcpu_idx, irq_num;
1137         int nrcpus = atomic_read(&kvm->online_vcpus);
1138         struct kvm_vcpu *vcpu = NULL;
1139         bool level = irq_level->level;
1140
1141         irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
1142         vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
1143         vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
1144         irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
1145
1146         trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
1147
1148         switch (irq_type) {
1149         case KVM_ARM_IRQ_TYPE_CPU:
1150                 if (irqchip_in_kernel(kvm))
1151                         return -ENXIO;
1152
1153                 if (vcpu_idx >= nrcpus)
1154                         return -EINVAL;
1155
1156                 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1157                 if (!vcpu)
1158                         return -EINVAL;
1159
1160                 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
1161                         return -EINVAL;
1162
1163                 return vcpu_interrupt_line(vcpu, irq_num, level);
1164         case KVM_ARM_IRQ_TYPE_PPI:
1165                 if (!irqchip_in_kernel(kvm))
1166                         return -ENXIO;
1167
1168                 if (vcpu_idx >= nrcpus)
1169                         return -EINVAL;
1170
1171                 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1172                 if (!vcpu)
1173                         return -EINVAL;
1174
1175                 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
1176                         return -EINVAL;
1177
1178                 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
1179         case KVM_ARM_IRQ_TYPE_SPI:
1180                 if (!irqchip_in_kernel(kvm))
1181                         return -ENXIO;
1182
1183                 if (irq_num < VGIC_NR_PRIVATE_IRQS)
1184                         return -EINVAL;
1185
1186                 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
1187         }
1188
1189         return -EINVAL;
1190 }
1191
1192 static unsigned long system_supported_vcpu_features(void)
1193 {
1194         unsigned long features = KVM_VCPU_VALID_FEATURES;
1195
1196         if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1))
1197                 clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features);
1198
1199         if (!kvm_arm_support_pmu_v3())
1200                 clear_bit(KVM_ARM_VCPU_PMU_V3, &features);
1201
1202         if (!system_supports_sve())
1203                 clear_bit(KVM_ARM_VCPU_SVE, &features);
1204
1205         if (!system_has_full_ptr_auth()) {
1206                 clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
1207                 clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
1208         }
1209
1210         if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
1211                 clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
1212
1213         return features;
1214 }
1215
1216 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
1217                                         const struct kvm_vcpu_init *init)
1218 {
1219         unsigned long features = init->features[0];
1220         int i;
1221
1222         if (features & ~KVM_VCPU_VALID_FEATURES)
1223                 return -ENOENT;
1224
1225         for (i = 1; i < ARRAY_SIZE(init->features); i++) {
1226                 if (init->features[i])
1227                         return -ENOENT;
1228         }
1229
1230         if (features & ~system_supported_vcpu_features())
1231                 return -EINVAL;
1232
1233         /*
1234          * For now make sure that both address/generic pointer authentication
1235          * features are requested by the userspace together.
1236          */
1237         if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) !=
1238             test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))
1239                 return -EINVAL;
1240
1241         /* Disallow NV+SVE for the time being */
1242         if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features) &&
1243             test_bit(KVM_ARM_VCPU_SVE, &features))
1244                 return -EINVAL;
1245
1246         if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
1247                 return 0;
1248
1249         /* MTE is incompatible with AArch32 */
1250         if (kvm_has_mte(vcpu->kvm))
1251                 return -EINVAL;
1252
1253         /* NV is incompatible with AArch32 */
1254         if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
1255                 return -EINVAL;
1256
1257         return 0;
1258 }
1259
1260 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
1261                                   const struct kvm_vcpu_init *init)
1262 {
1263         unsigned long features = init->features[0];
1264
1265         return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features,
1266                              KVM_VCPU_MAX_FEATURES);
1267 }
1268
1269 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1270                                  const struct kvm_vcpu_init *init)
1271 {
1272         unsigned long features = init->features[0];
1273         struct kvm *kvm = vcpu->kvm;
1274         int ret = -EINVAL;
1275
1276         mutex_lock(&kvm->arch.config_lock);
1277
1278         if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
1279             kvm_vcpu_init_changed(vcpu, init))
1280                 goto out_unlock;
1281
1282         bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
1283
1284         /* Now we know what it is, we can reset it. */
1285         kvm_reset_vcpu(vcpu);
1286
1287         set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
1288         vcpu_set_flag(vcpu, VCPU_INITIALIZED);
1289         ret = 0;
1290 out_unlock:
1291         mutex_unlock(&kvm->arch.config_lock);
1292         return ret;
1293 }
1294
1295 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1296                                const struct kvm_vcpu_init *init)
1297 {
1298         int ret;
1299
1300         if (init->target != KVM_ARM_TARGET_GENERIC_V8 &&
1301             init->target != kvm_target_cpu())
1302                 return -EINVAL;
1303
1304         ret = kvm_vcpu_init_check_features(vcpu, init);
1305         if (ret)
1306                 return ret;
1307
1308         if (!kvm_vcpu_initialized(vcpu))
1309                 return __kvm_vcpu_set_target(vcpu, init);
1310
1311         if (kvm_vcpu_init_changed(vcpu, init))
1312                 return -EINVAL;
1313
1314         kvm_reset_vcpu(vcpu);
1315         return 0;
1316 }
1317
1318 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1319                                          struct kvm_vcpu_init *init)
1320 {
1321         bool power_off = false;
1322         int ret;
1323
1324         /*
1325          * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
1326          * reflecting it in the finalized feature set, thus limiting its scope
1327          * to a single KVM_ARM_VCPU_INIT call.
1328          */
1329         if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
1330                 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
1331                 power_off = true;
1332         }
1333
1334         ret = kvm_vcpu_set_target(vcpu, init);
1335         if (ret)
1336                 return ret;
1337
1338         /*
1339          * Ensure a rebooted VM will fault in RAM pages and detect if the
1340          * guest MMU is turned off and flush the caches as needed.
1341          *
1342          * S2FWB enforces all memory accesses to RAM being cacheable,
1343          * ensuring that the data side is always coherent. We still
1344          * need to invalidate the I-cache though, as FWB does *not*
1345          * imply CTR_EL0.DIC.
1346          */
1347         if (vcpu_has_run_once(vcpu)) {
1348                 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
1349                         stage2_unmap_vm(vcpu->kvm);
1350                 else
1351                         icache_inval_all_pou();
1352         }
1353
1354         vcpu_reset_hcr(vcpu);
1355         vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu);
1356
1357         /*
1358          * Handle the "start in power-off" case.
1359          */
1360         spin_lock(&vcpu->arch.mp_state_lock);
1361
1362         if (power_off)
1363                 __kvm_arm_vcpu_power_off(vcpu);
1364         else
1365                 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
1366
1367         spin_unlock(&vcpu->arch.mp_state_lock);
1368
1369         return 0;
1370 }
1371
1372 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1373                                  struct kvm_device_attr *attr)
1374 {
1375         int ret = -ENXIO;
1376
1377         switch (attr->group) {
1378         default:
1379                 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
1380                 break;
1381         }
1382
1383         return ret;
1384 }
1385
1386 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1387                                  struct kvm_device_attr *attr)
1388 {
1389         int ret = -ENXIO;
1390
1391         switch (attr->group) {
1392         default:
1393                 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
1394                 break;
1395         }
1396
1397         return ret;
1398 }
1399
1400 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1401                                  struct kvm_device_attr *attr)
1402 {
1403         int ret = -ENXIO;
1404
1405         switch (attr->group) {
1406         default:
1407                 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
1408                 break;
1409         }
1410
1411         return ret;
1412 }
1413
1414 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1415                                    struct kvm_vcpu_events *events)
1416 {
1417         memset(events, 0, sizeof(*events));
1418
1419         return __kvm_arm_vcpu_get_events(vcpu, events);
1420 }
1421
1422 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1423                                    struct kvm_vcpu_events *events)
1424 {
1425         int i;
1426
1427         /* check whether the reserved field is zero */
1428         for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1429                 if (events->reserved[i])
1430                         return -EINVAL;
1431
1432         /* check whether the pad field is zero */
1433         for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1434                 if (events->exception.pad[i])
1435                         return -EINVAL;
1436
1437         return __kvm_arm_vcpu_set_events(vcpu, events);
1438 }
1439
1440 long kvm_arch_vcpu_ioctl(struct file *filp,
1441                          unsigned int ioctl, unsigned long arg)
1442 {
1443         struct kvm_vcpu *vcpu = filp->private_data;
1444         void __user *argp = (void __user *)arg;
1445         struct kvm_device_attr attr;
1446         long r;
1447
1448         switch (ioctl) {
1449         case KVM_ARM_VCPU_INIT: {
1450                 struct kvm_vcpu_init init;
1451
1452                 r = -EFAULT;
1453                 if (copy_from_user(&init, argp, sizeof(init)))
1454                         break;
1455
1456                 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1457                 break;
1458         }
1459         case KVM_SET_ONE_REG:
1460         case KVM_GET_ONE_REG: {
1461                 struct kvm_one_reg reg;
1462
1463                 r = -ENOEXEC;
1464                 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1465                         break;
1466
1467                 r = -EFAULT;
1468                 if (copy_from_user(&reg, argp, sizeof(reg)))
1469                         break;
1470
1471                 /*
1472                  * We could owe a reset due to PSCI. Handle the pending reset
1473                  * here to ensure userspace register accesses are ordered after
1474                  * the reset.
1475                  */
1476                 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
1477                         kvm_reset_vcpu(vcpu);
1478
1479                 if (ioctl == KVM_SET_ONE_REG)
1480                         r = kvm_arm_set_reg(vcpu, &reg);
1481                 else
1482                         r = kvm_arm_get_reg(vcpu, &reg);
1483                 break;
1484         }
1485         case KVM_GET_REG_LIST: {
1486                 struct kvm_reg_list __user *user_list = argp;
1487                 struct kvm_reg_list reg_list;
1488                 unsigned n;
1489
1490                 r = -ENOEXEC;
1491                 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1492                         break;
1493
1494                 r = -EPERM;
1495                 if (!kvm_arm_vcpu_is_finalized(vcpu))
1496                         break;
1497
1498                 r = -EFAULT;
1499                 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1500                         break;
1501                 n = reg_list.n;
1502                 reg_list.n = kvm_arm_num_regs(vcpu);
1503                 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1504                         break;
1505                 r = -E2BIG;
1506                 if (n < reg_list.n)
1507                         break;
1508                 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1509                 break;
1510         }
1511         case KVM_SET_DEVICE_ATTR: {
1512                 r = -EFAULT;
1513                 if (copy_from_user(&attr, argp, sizeof(attr)))
1514                         break;
1515                 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1516                 break;
1517         }
1518         case KVM_GET_DEVICE_ATTR: {
1519                 r = -EFAULT;
1520                 if (copy_from_user(&attr, argp, sizeof(attr)))
1521                         break;
1522                 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1523                 break;
1524         }
1525         case KVM_HAS_DEVICE_ATTR: {
1526                 r = -EFAULT;
1527                 if (copy_from_user(&attr, argp, sizeof(attr)))
1528                         break;
1529                 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1530                 break;
1531         }
1532         case KVM_GET_VCPU_EVENTS: {
1533                 struct kvm_vcpu_events events;
1534
1535                 if (kvm_arm_vcpu_get_events(vcpu, &events))
1536                         return -EINVAL;
1537
1538                 if (copy_to_user(argp, &events, sizeof(events)))
1539                         return -EFAULT;
1540
1541                 return 0;
1542         }
1543         case KVM_SET_VCPU_EVENTS: {
1544                 struct kvm_vcpu_events events;
1545
1546                 if (copy_from_user(&events, argp, sizeof(events)))
1547                         return -EFAULT;
1548
1549                 return kvm_arm_vcpu_set_events(vcpu, &events);
1550         }
1551         case KVM_ARM_VCPU_FINALIZE: {
1552                 int what;
1553
1554                 if (!kvm_vcpu_initialized(vcpu))
1555                         return -ENOEXEC;
1556
1557                 if (get_user(what, (const int __user *)argp))
1558                         return -EFAULT;
1559
1560                 return kvm_arm_vcpu_finalize(vcpu, what);
1561         }
1562         default:
1563                 r = -EINVAL;
1564         }
1565
1566         return r;
1567 }
1568
1569 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1570 {
1571
1572 }
1573
1574 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1575                                         struct kvm_arm_device_addr *dev_addr)
1576 {
1577         switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
1578         case KVM_ARM_DEVICE_VGIC_V2:
1579                 if (!vgic_present)
1580                         return -ENXIO;
1581                 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
1582         default:
1583                 return -ENODEV;
1584         }
1585 }
1586
1587 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1588 {
1589         switch (attr->group) {
1590         case KVM_ARM_VM_SMCCC_CTRL:
1591                 return kvm_vm_smccc_has_attr(kvm, attr);
1592         default:
1593                 return -ENXIO;
1594         }
1595 }
1596
1597 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1598 {
1599         switch (attr->group) {
1600         case KVM_ARM_VM_SMCCC_CTRL:
1601                 return kvm_vm_smccc_set_attr(kvm, attr);
1602         default:
1603                 return -ENXIO;
1604         }
1605 }
1606
1607 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1608 {
1609         struct kvm *kvm = filp->private_data;
1610         void __user *argp = (void __user *)arg;
1611         struct kvm_device_attr attr;
1612
1613         switch (ioctl) {
1614         case KVM_CREATE_IRQCHIP: {
1615                 int ret;
1616                 if (!vgic_present)
1617                         return -ENXIO;
1618                 mutex_lock(&kvm->lock);
1619                 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1620                 mutex_unlock(&kvm->lock);
1621                 return ret;
1622         }
1623         case KVM_ARM_SET_DEVICE_ADDR: {
1624                 struct kvm_arm_device_addr dev_addr;
1625
1626                 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1627                         return -EFAULT;
1628                 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1629         }
1630         case KVM_ARM_PREFERRED_TARGET: {
1631                 struct kvm_vcpu_init init = {
1632                         .target = KVM_ARM_TARGET_GENERIC_V8,
1633                 };
1634
1635                 if (copy_to_user(argp, &init, sizeof(init)))
1636                         return -EFAULT;
1637
1638                 return 0;
1639         }
1640         case KVM_ARM_MTE_COPY_TAGS: {
1641                 struct kvm_arm_copy_mte_tags copy_tags;
1642
1643                 if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1644                         return -EFAULT;
1645                 return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1646         }
1647         case KVM_ARM_SET_COUNTER_OFFSET: {
1648                 struct kvm_arm_counter_offset offset;
1649
1650                 if (copy_from_user(&offset, argp, sizeof(offset)))
1651                         return -EFAULT;
1652                 return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
1653         }
1654         case KVM_HAS_DEVICE_ATTR: {
1655                 if (copy_from_user(&attr, argp, sizeof(attr)))
1656                         return -EFAULT;
1657
1658                 return kvm_vm_has_attr(kvm, &attr);
1659         }
1660         case KVM_SET_DEVICE_ATTR: {
1661                 if (copy_from_user(&attr, argp, sizeof(attr)))
1662                         return -EFAULT;
1663
1664                 return kvm_vm_set_attr(kvm, &attr);
1665         }
1666         default:
1667                 return -EINVAL;
1668         }
1669 }
1670
1671 /* unlocks vcpus from @vcpu_lock_idx and smaller */
1672 static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx)
1673 {
1674         struct kvm_vcpu *tmp_vcpu;
1675
1676         for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1677                 tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1678                 mutex_unlock(&tmp_vcpu->mutex);
1679         }
1680 }
1681
1682 void unlock_all_vcpus(struct kvm *kvm)
1683 {
1684         lockdep_assert_held(&kvm->lock);
1685
1686         unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1);
1687 }
1688
1689 /* Returns true if all vcpus were locked, false otherwise */
1690 bool lock_all_vcpus(struct kvm *kvm)
1691 {
1692         struct kvm_vcpu *tmp_vcpu;
1693         unsigned long c;
1694
1695         lockdep_assert_held(&kvm->lock);
1696
1697         /*
1698          * Any time a vcpu is in an ioctl (including running), the
1699          * core KVM code tries to grab the vcpu->mutex.
1700          *
1701          * By grabbing the vcpu->mutex of all VCPUs we ensure that no
1702          * other VCPUs can fiddle with the state while we access it.
1703          */
1704         kvm_for_each_vcpu(c, tmp_vcpu, kvm) {
1705                 if (!mutex_trylock(&tmp_vcpu->mutex)) {
1706                         unlock_vcpus(kvm, c - 1);
1707                         return false;
1708                 }
1709         }
1710
1711         return true;
1712 }
1713
1714 static unsigned long nvhe_percpu_size(void)
1715 {
1716         return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
1717                 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
1718 }
1719
1720 static unsigned long nvhe_percpu_order(void)
1721 {
1722         unsigned long size = nvhe_percpu_size();
1723
1724         return size ? get_order(size) : 0;
1725 }
1726
1727 /* A lookup table holding the hypervisor VA for each vector slot */
1728 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1729
1730 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1731 {
1732         hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
1733 }
1734
1735 static int kvm_init_vector_slots(void)
1736 {
1737         int err;
1738         void *base;
1739
1740         base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1741         kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1742
1743         base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1744         kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1745
1746         if (kvm_system_needs_idmapped_vectors() &&
1747             !is_protected_kvm_enabled()) {
1748                 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1749                                                __BP_HARDEN_HYP_VECS_SZ, &base);
1750                 if (err)
1751                         return err;
1752         }
1753
1754         kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1755         kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
1756         return 0;
1757 }
1758
1759 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
1760 {
1761         struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1762         unsigned long tcr;
1763
1764         /*
1765          * Calculate the raw per-cpu offset without a translation from the
1766          * kernel's mapping to the linear mapping, and store it in tpidr_el2
1767          * so that we can use adr_l to access per-cpu variables in EL2.
1768          * Also drop the KASAN tag which gets in the way...
1769          */
1770         params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
1771                             (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
1772
1773         params->mair_el2 = read_sysreg(mair_el1);
1774
1775         tcr = read_sysreg(tcr_el1);
1776         if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
1777                 tcr |= TCR_EPD1_MASK;
1778         } else {
1779                 tcr &= TCR_EL2_MASK;
1780                 tcr |= TCR_EL2_RES1;
1781         }
1782         tcr &= ~TCR_T0SZ_MASK;
1783         tcr |= TCR_T0SZ(hyp_va_bits);
1784         params->tcr_el2 = tcr;
1785
1786         params->pgd_pa = kvm_mmu_get_httbr();
1787         if (is_protected_kvm_enabled())
1788                 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1789         else
1790                 params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1791         if (cpus_have_final_cap(ARM64_KVM_HVHE))
1792                 params->hcr_el2 |= HCR_E2H;
1793         params->vttbr = params->vtcr = 0;
1794
1795         /*
1796          * Flush the init params from the data cache because the struct will
1797          * be read while the MMU is off.
1798          */
1799         kvm_flush_dcache_to_poc(params, sizeof(*params));
1800 }
1801
1802 static void hyp_install_host_vector(void)
1803 {
1804         struct kvm_nvhe_init_params *params;
1805         struct arm_smccc_res res;
1806
1807         /* Switch from the HYP stub to our own HYP init vector */
1808         __hyp_set_vectors(kvm_get_idmap_vector());
1809
1810         /*
1811          * Call initialization code, and switch to the full blown HYP code.
1812          * If the cpucaps haven't been finalized yet, something has gone very
1813          * wrong, and hyp will crash and burn when it uses any
1814          * cpus_have_const_cap() wrapper.
1815          */
1816         BUG_ON(!system_capabilities_finalized());
1817         params = this_cpu_ptr_nvhe_sym(kvm_init_params);
1818         arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
1819         WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1820 }
1821
1822 static void cpu_init_hyp_mode(void)
1823 {
1824         hyp_install_host_vector();
1825
1826         /*
1827          * Disabling SSBD on a non-VHE system requires us to enable SSBS
1828          * at EL2.
1829          */
1830         if (this_cpu_has_cap(ARM64_SSBS) &&
1831             arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
1832                 kvm_call_hyp_nvhe(__kvm_enable_ssbs);
1833         }
1834 }
1835
1836 static void cpu_hyp_reset(void)
1837 {
1838         if (!is_kernel_in_hyp_mode())
1839                 __hyp_reset_vectors();
1840 }
1841
1842 /*
1843  * EL2 vectors can be mapped and rerouted in a number of ways,
1844  * depending on the kernel configuration and CPU present:
1845  *
1846  * - If the CPU is affected by Spectre-v2, the hardening sequence is
1847  *   placed in one of the vector slots, which is executed before jumping
1848  *   to the real vectors.
1849  *
1850  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1851  *   containing the hardening sequence is mapped next to the idmap page,
1852  *   and executed before jumping to the real vectors.
1853  *
1854  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1855  *   empty slot is selected, mapped next to the idmap page, and
1856  *   executed before jumping to the real vectors.
1857  *
1858  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1859  * VHE, as we don't have hypervisor-specific mappings. If the system
1860  * is VHE and yet selects this capability, it will be ignored.
1861  */
1862 static void cpu_set_hyp_vector(void)
1863 {
1864         struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1865         void *vector = hyp_spectre_vector_selector[data->slot];
1866
1867         if (!is_protected_kvm_enabled())
1868                 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1869         else
1870                 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1871 }
1872
1873 static void cpu_hyp_init_context(void)
1874 {
1875         kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
1876
1877         if (!is_kernel_in_hyp_mode())
1878                 cpu_init_hyp_mode();
1879 }
1880
1881 static void cpu_hyp_init_features(void)
1882 {
1883         cpu_set_hyp_vector();
1884         kvm_arm_init_debug();
1885
1886         if (is_kernel_in_hyp_mode())
1887                 kvm_timer_init_vhe();
1888
1889         if (vgic_present)
1890                 kvm_vgic_init_cpu_hardware();
1891 }
1892
1893 static void cpu_hyp_reinit(void)
1894 {
1895         cpu_hyp_reset();
1896         cpu_hyp_init_context();
1897         cpu_hyp_init_features();
1898 }
1899
1900 static void cpu_hyp_init(void *discard)
1901 {
1902         if (!__this_cpu_read(kvm_hyp_initialized)) {
1903                 cpu_hyp_reinit();
1904                 __this_cpu_write(kvm_hyp_initialized, 1);
1905         }
1906 }
1907
1908 static void cpu_hyp_uninit(void *discard)
1909 {
1910         if (__this_cpu_read(kvm_hyp_initialized)) {
1911                 cpu_hyp_reset();
1912                 __this_cpu_write(kvm_hyp_initialized, 0);
1913         }
1914 }
1915
1916 int kvm_arch_hardware_enable(void)
1917 {
1918         /*
1919          * Most calls to this function are made with migration
1920          * disabled, but not with preemption disabled. The former is
1921          * enough to ensure correctness, but most of the helpers
1922          * expect the later and will throw a tantrum otherwise.
1923          */
1924         preempt_disable();
1925
1926         cpu_hyp_init(NULL);
1927
1928         kvm_vgic_cpu_up();
1929         kvm_timer_cpu_up();
1930
1931         preempt_enable();
1932
1933         return 0;
1934 }
1935
1936 void kvm_arch_hardware_disable(void)
1937 {
1938         kvm_timer_cpu_down();
1939         kvm_vgic_cpu_down();
1940
1941         if (!is_protected_kvm_enabled())
1942                 cpu_hyp_uninit(NULL);
1943 }
1944
1945 #ifdef CONFIG_CPU_PM
1946 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1947                                     unsigned long cmd,
1948                                     void *v)
1949 {
1950         /*
1951          * kvm_hyp_initialized is left with its old value over
1952          * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1953          * re-enable hyp.
1954          */
1955         switch (cmd) {
1956         case CPU_PM_ENTER:
1957                 if (__this_cpu_read(kvm_hyp_initialized))
1958                         /*
1959                          * don't update kvm_hyp_initialized here
1960                          * so that the hyp will be re-enabled
1961                          * when we resume. See below.
1962                          */
1963                         cpu_hyp_reset();
1964
1965                 return NOTIFY_OK;
1966         case CPU_PM_ENTER_FAILED:
1967         case CPU_PM_EXIT:
1968                 if (__this_cpu_read(kvm_hyp_initialized))
1969                         /* The hyp was enabled before suspend. */
1970                         cpu_hyp_reinit();
1971
1972                 return NOTIFY_OK;
1973
1974         default:
1975                 return NOTIFY_DONE;
1976         }
1977 }
1978
1979 static struct notifier_block hyp_init_cpu_pm_nb = {
1980         .notifier_call = hyp_init_cpu_pm_notifier,
1981 };
1982
1983 static void __init hyp_cpu_pm_init(void)
1984 {
1985         if (!is_protected_kvm_enabled())
1986                 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1987 }
1988 static void __init hyp_cpu_pm_exit(void)
1989 {
1990         if (!is_protected_kvm_enabled())
1991                 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1992 }
1993 #else
1994 static inline void __init hyp_cpu_pm_init(void)
1995 {
1996 }
1997 static inline void __init hyp_cpu_pm_exit(void)
1998 {
1999 }
2000 #endif
2001
2002 static void __init init_cpu_logical_map(void)
2003 {
2004         unsigned int cpu;
2005
2006         /*
2007          * Copy the MPIDR <-> logical CPU ID mapping to hyp.
2008          * Only copy the set of online CPUs whose features have been checked
2009          * against the finalized system capabilities. The hypervisor will not
2010          * allow any other CPUs from the `possible` set to boot.
2011          */
2012         for_each_online_cpu(cpu)
2013                 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
2014 }
2015
2016 #define init_psci_0_1_impl_state(config, what)  \
2017         config.psci_0_1_ ## what ## _implemented = psci_ops.what
2018
2019 static bool __init init_psci_relay(void)
2020 {
2021         /*
2022          * If PSCI has not been initialized, protected KVM cannot install
2023          * itself on newly booted CPUs.
2024          */
2025         if (!psci_ops.get_version) {
2026                 kvm_err("Cannot initialize protected mode without PSCI\n");
2027                 return false;
2028         }
2029
2030         kvm_host_psci_config.version = psci_ops.get_version();
2031         kvm_host_psci_config.smccc_version = arm_smccc_get_version();
2032
2033         if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
2034                 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
2035                 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
2036                 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
2037                 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
2038                 init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
2039         }
2040         return true;
2041 }
2042
2043 static int __init init_subsystems(void)
2044 {
2045         int err = 0;
2046
2047         /*
2048          * Enable hardware so that subsystem initialisation can access EL2.
2049          */
2050         on_each_cpu(cpu_hyp_init, NULL, 1);
2051
2052         /*
2053          * Register CPU lower-power notifier
2054          */
2055         hyp_cpu_pm_init();
2056
2057         /*
2058          * Init HYP view of VGIC
2059          */
2060         err = kvm_vgic_hyp_init();
2061         switch (err) {
2062         case 0:
2063                 vgic_present = true;
2064                 break;
2065         case -ENODEV:
2066         case -ENXIO:
2067                 vgic_present = false;
2068                 err = 0;
2069                 break;
2070         default:
2071                 goto out;
2072         }
2073
2074         /*
2075          * Init HYP architected timer support
2076          */
2077         err = kvm_timer_hyp_init(vgic_present);
2078         if (err)
2079                 goto out;
2080
2081         kvm_register_perf_callbacks(NULL);
2082
2083 out:
2084         if (err)
2085                 hyp_cpu_pm_exit();
2086
2087         if (err || !is_protected_kvm_enabled())
2088                 on_each_cpu(cpu_hyp_uninit, NULL, 1);
2089
2090         return err;
2091 }
2092
2093 static void __init teardown_subsystems(void)
2094 {
2095         kvm_unregister_perf_callbacks();
2096         hyp_cpu_pm_exit();
2097 }
2098
2099 static void __init teardown_hyp_mode(void)
2100 {
2101         int cpu;
2102
2103         free_hyp_pgds();
2104         for_each_possible_cpu(cpu) {
2105                 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
2106                 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
2107         }
2108 }
2109
2110 static int __init do_pkvm_init(u32 hyp_va_bits)
2111 {
2112         void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
2113         int ret;
2114
2115         preempt_disable();
2116         cpu_hyp_init_context();
2117         ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
2118                                 num_possible_cpus(), kern_hyp_va(per_cpu_base),
2119                                 hyp_va_bits);
2120         cpu_hyp_init_features();
2121
2122         /*
2123          * The stub hypercalls are now disabled, so set our local flag to
2124          * prevent a later re-init attempt in kvm_arch_hardware_enable().
2125          */
2126         __this_cpu_write(kvm_hyp_initialized, 1);
2127         preempt_enable();
2128
2129         return ret;
2130 }
2131
2132 static u64 get_hyp_id_aa64pfr0_el1(void)
2133 {
2134         /*
2135          * Track whether the system isn't affected by spectre/meltdown in the
2136          * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
2137          * Although this is per-CPU, we make it global for simplicity, e.g., not
2138          * to have to worry about vcpu migration.
2139          *
2140          * Unlike for non-protected VMs, userspace cannot override this for
2141          * protected VMs.
2142          */
2143         u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
2144
2145         val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
2146                  ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
2147
2148         val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
2149                           arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
2150         val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
2151                           arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
2152
2153         return val;
2154 }
2155
2156 static void kvm_hyp_init_symbols(void)
2157 {
2158         kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
2159         kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
2160         kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
2161         kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
2162         kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
2163         kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
2164         kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
2165         kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
2166         kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
2167         kvm_nvhe_sym(__icache_flags) = __icache_flags;
2168         kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
2169 }
2170
2171 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
2172 {
2173         void *addr = phys_to_virt(hyp_mem_base);
2174         int ret;
2175
2176         ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
2177         if (ret)
2178                 return ret;
2179
2180         ret = do_pkvm_init(hyp_va_bits);
2181         if (ret)
2182                 return ret;
2183
2184         free_hyp_pgds();
2185
2186         return 0;
2187 }
2188
2189 static void pkvm_hyp_init_ptrauth(void)
2190 {
2191         struct kvm_cpu_context *hyp_ctxt;
2192         int cpu;
2193
2194         for_each_possible_cpu(cpu) {
2195                 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
2196                 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
2197                 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
2198                 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
2199                 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
2200                 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
2201                 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
2202                 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
2203                 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
2204                 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
2205                 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
2206         }
2207 }
2208
2209 /* Inits Hyp-mode on all online CPUs */
2210 static int __init init_hyp_mode(void)
2211 {
2212         u32 hyp_va_bits;
2213         int cpu;
2214         int err = -ENOMEM;
2215
2216         /*
2217          * The protected Hyp-mode cannot be initialized if the memory pool
2218          * allocation has failed.
2219          */
2220         if (is_protected_kvm_enabled() && !hyp_mem_base)
2221                 goto out_err;
2222
2223         /*
2224          * Allocate Hyp PGD and setup Hyp identity mapping
2225          */
2226         err = kvm_mmu_init(&hyp_va_bits);
2227         if (err)
2228                 goto out_err;
2229
2230         /*
2231          * Allocate stack pages for Hypervisor-mode
2232          */
2233         for_each_possible_cpu(cpu) {
2234                 unsigned long stack_page;
2235
2236                 stack_page = __get_free_page(GFP_KERNEL);
2237                 if (!stack_page) {
2238                         err = -ENOMEM;
2239                         goto out_err;
2240                 }
2241
2242                 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
2243         }
2244
2245         /*
2246          * Allocate and initialize pages for Hypervisor-mode percpu regions.
2247          */
2248         for_each_possible_cpu(cpu) {
2249                 struct page *page;
2250                 void *page_addr;
2251
2252                 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
2253                 if (!page) {
2254                         err = -ENOMEM;
2255                         goto out_err;
2256                 }
2257
2258                 page_addr = page_address(page);
2259                 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2260                 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
2261         }
2262
2263         /*
2264          * Map the Hyp-code called directly from the host
2265          */
2266         err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
2267                                   kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
2268         if (err) {
2269                 kvm_err("Cannot map world-switch code\n");
2270                 goto out_err;
2271         }
2272
2273         err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
2274                                   kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
2275         if (err) {
2276                 kvm_err("Cannot map .hyp.rodata section\n");
2277                 goto out_err;
2278         }
2279
2280         err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
2281                                   kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
2282         if (err) {
2283                 kvm_err("Cannot map rodata section\n");
2284                 goto out_err;
2285         }
2286
2287         /*
2288          * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2289          * section thanks to an assertion in the linker script. Map it RW and
2290          * the rest of .bss RO.
2291          */
2292         err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2293                                   kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2294         if (err) {
2295                 kvm_err("Cannot map hyp bss section: %d\n", err);
2296                 goto out_err;
2297         }
2298
2299         err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
2300                                   kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
2301         if (err) {
2302                 kvm_err("Cannot map bss section\n");
2303                 goto out_err;
2304         }
2305
2306         /*
2307          * Map the Hyp stack pages
2308          */
2309         for_each_possible_cpu(cpu) {
2310                 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
2311                 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
2312
2313                 err = create_hyp_stack(__pa(stack_page), &params->stack_hyp_va);
2314                 if (err) {
2315                         kvm_err("Cannot map hyp stack\n");
2316                         goto out_err;
2317                 }
2318
2319                 /*
2320                  * Save the stack PA in nvhe_init_params. This will be needed
2321                  * to recreate the stack mapping in protected nVHE mode.
2322                  * __hyp_pa() won't do the right thing there, since the stack
2323                  * has been mapped in the flexible private VA space.
2324                  */
2325                 params->stack_pa = __pa(stack_page);
2326         }
2327
2328         for_each_possible_cpu(cpu) {
2329                 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
2330                 char *percpu_end = percpu_begin + nvhe_percpu_size();
2331
2332                 /* Map Hyp percpu pages */
2333                 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
2334                 if (err) {
2335                         kvm_err("Cannot map hyp percpu region\n");
2336                         goto out_err;
2337                 }
2338
2339                 /* Prepare the CPU initialization parameters */
2340                 cpu_prepare_hyp_mode(cpu, hyp_va_bits);
2341         }
2342
2343         kvm_hyp_init_symbols();
2344
2345         if (is_protected_kvm_enabled()) {
2346                 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
2347                     cpus_have_const_cap(ARM64_HAS_ADDRESS_AUTH))
2348                         pkvm_hyp_init_ptrauth();
2349
2350                 init_cpu_logical_map();
2351
2352                 if (!init_psci_relay()) {
2353                         err = -ENODEV;
2354                         goto out_err;
2355                 }
2356
2357                 err = kvm_hyp_init_protection(hyp_va_bits);
2358                 if (err) {
2359                         kvm_err("Failed to init hyp memory protection\n");
2360                         goto out_err;
2361                 }
2362         }
2363
2364         return 0;
2365
2366 out_err:
2367         teardown_hyp_mode();
2368         kvm_err("error initializing Hyp mode: %d\n", err);
2369         return err;
2370 }
2371
2372 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
2373 {
2374         struct kvm_vcpu *vcpu;
2375         unsigned long i;
2376
2377         mpidr &= MPIDR_HWID_BITMASK;
2378         kvm_for_each_vcpu(i, vcpu, kvm) {
2379                 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
2380                         return vcpu;
2381         }
2382         return NULL;
2383 }
2384
2385 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2386 {
2387         return irqchip_in_kernel(kvm);
2388 }
2389
2390 bool kvm_arch_has_irq_bypass(void)
2391 {
2392         return true;
2393 }
2394
2395 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
2396                                       struct irq_bypass_producer *prod)
2397 {
2398         struct kvm_kernel_irqfd *irqfd =
2399                 container_of(cons, struct kvm_kernel_irqfd, consumer);
2400
2401         return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
2402                                           &irqfd->irq_entry);
2403 }
2404 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
2405                                       struct irq_bypass_producer *prod)
2406 {
2407         struct kvm_kernel_irqfd *irqfd =
2408                 container_of(cons, struct kvm_kernel_irqfd, consumer);
2409
2410         kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
2411                                      &irqfd->irq_entry);
2412 }
2413
2414 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
2415 {
2416         struct kvm_kernel_irqfd *irqfd =
2417                 container_of(cons, struct kvm_kernel_irqfd, consumer);
2418
2419         kvm_arm_halt_guest(irqfd->kvm);
2420 }
2421
2422 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
2423 {
2424         struct kvm_kernel_irqfd *irqfd =
2425                 container_of(cons, struct kvm_kernel_irqfd, consumer);
2426
2427         kvm_arm_resume_guest(irqfd->kvm);
2428 }
2429
2430 /* Initialize Hyp-mode and memory mappings on all CPUs */
2431 static __init int kvm_arm_init(void)
2432 {
2433         int err;
2434         bool in_hyp_mode;
2435
2436         if (!is_hyp_mode_available()) {
2437                 kvm_info("HYP mode not available\n");
2438                 return -ENODEV;
2439         }
2440
2441         if (kvm_get_mode() == KVM_MODE_NONE) {
2442                 kvm_info("KVM disabled from command line\n");
2443                 return -ENODEV;
2444         }
2445
2446         err = kvm_sys_reg_table_init();
2447         if (err) {
2448                 kvm_info("Error initializing system register tables");
2449                 return err;
2450         }
2451
2452         in_hyp_mode = is_kernel_in_hyp_mode();
2453
2454         if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
2455             cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2456                 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2457                          "Only trusted guests should be used on this system.\n");
2458
2459         err = kvm_set_ipa_limit();
2460         if (err)
2461                 return err;
2462
2463         err = kvm_arm_init_sve();
2464         if (err)
2465                 return err;
2466
2467         err = kvm_arm_vmid_alloc_init();
2468         if (err) {
2469                 kvm_err("Failed to initialize VMID allocator.\n");
2470                 return err;
2471         }
2472
2473         if (!in_hyp_mode) {
2474                 err = init_hyp_mode();
2475                 if (err)
2476                         goto out_err;
2477         }
2478
2479         err = kvm_init_vector_slots();
2480         if (err) {
2481                 kvm_err("Cannot initialise vector slots\n");
2482                 goto out_hyp;
2483         }
2484
2485         err = init_subsystems();
2486         if (err)
2487                 goto out_hyp;
2488
2489         if (is_protected_kvm_enabled()) {
2490                 kvm_info("Protected nVHE mode initialized successfully\n");
2491         } else if (in_hyp_mode) {
2492                 kvm_info("VHE mode initialized successfully\n");
2493         } else {
2494                 kvm_info("Hyp mode initialized successfully\n");
2495         }
2496
2497         /*
2498          * FIXME: Do something reasonable if kvm_init() fails after pKVM
2499          * hypervisor protection is finalized.
2500          */
2501         err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2502         if (err)
2503                 goto out_subs;
2504
2505         kvm_arm_initialised = true;
2506
2507         return 0;
2508
2509 out_subs:
2510         teardown_subsystems();
2511 out_hyp:
2512         if (!in_hyp_mode)
2513                 teardown_hyp_mode();
2514 out_err:
2515         kvm_arm_vmid_alloc_free();
2516         return err;
2517 }
2518
2519 static int __init early_kvm_mode_cfg(char *arg)
2520 {
2521         if (!arg)
2522                 return -EINVAL;
2523
2524         if (strcmp(arg, "none") == 0) {
2525                 kvm_mode = KVM_MODE_NONE;
2526                 return 0;
2527         }
2528
2529         if (!is_hyp_mode_available()) {
2530                 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2531                 return 0;
2532         }
2533
2534         if (strcmp(arg, "protected") == 0) {
2535                 if (!is_kernel_in_hyp_mode())
2536                         kvm_mode = KVM_MODE_PROTECTED;
2537                 else
2538                         pr_warn_once("Protected KVM not available with VHE\n");
2539
2540                 return 0;
2541         }
2542
2543         if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2544                 kvm_mode = KVM_MODE_DEFAULT;
2545                 return 0;
2546         }
2547
2548         if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2549                 kvm_mode = KVM_MODE_NV;
2550                 return 0;
2551         }
2552
2553         return -EINVAL;
2554 }
2555 early_param("kvm-arm.mode", early_kvm_mode_cfg);
2556
2557 enum kvm_mode kvm_get_mode(void)
2558 {
2559         return kvm_mode;
2560 }
2561
2562 module_init(kvm_arm_init);