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