This source file includes following definitions.
- kvm_arm_set_running_vcpu
- kvm_arm_get_running_vcpu
- kvm_get_running_vcpus
- kvm_arch_vcpu_should_kick
- kvm_arch_hardware_setup
- kvm_arch_check_processor_compat
- kvm_arch_init_vm
- kvm_arch_create_vcpu_debugfs
- kvm_arch_vcpu_fault
- kvm_arch_destroy_vm
- kvm_vm_ioctl_check_extension
- kvm_arch_dev_ioctl
- kvm_arch_alloc_vm
- kvm_arch_free_vm
- kvm_arch_vcpu_create
- kvm_arch_vcpu_postcreate
- kvm_arch_vcpu_free
- kvm_arch_vcpu_destroy
- kvm_cpu_has_pending_timer
- kvm_arch_vcpu_blocking
- kvm_arch_vcpu_unblocking
- kvm_arch_vcpu_init
- kvm_arch_vcpu_load
- kvm_arch_vcpu_put
- vcpu_power_off
- kvm_arch_vcpu_ioctl_get_mpstate
- kvm_arch_vcpu_ioctl_set_mpstate
- kvm_arch_vcpu_runnable
- kvm_arch_vcpu_in_kernel
- exit_vm_noop
- force_vm_exit
- need_new_vmid_gen
- update_vmid
- kvm_vcpu_first_run_init
- kvm_arch_intc_initialized
- kvm_arm_halt_guest
- kvm_arm_resume_guest
- vcpu_req_sleep
- kvm_vcpu_initialized
- check_vcpu_requests
- kvm_arch_vcpu_ioctl_run
- vcpu_interrupt_line
- kvm_vm_ioctl_irq_line
- kvm_vcpu_set_target
- kvm_arch_vcpu_ioctl_vcpu_init
- kvm_arm_vcpu_set_attr
- kvm_arm_vcpu_get_attr
- kvm_arm_vcpu_has_attr
- kvm_arm_vcpu_get_events
- kvm_arm_vcpu_set_events
- kvm_arch_vcpu_ioctl
- kvm_vm_ioctl_get_dirty_log
- kvm_vm_ioctl_clear_dirty_log
- kvm_vm_ioctl_set_device_addr
- kvm_arch_vm_ioctl
- cpu_init_hyp_mode
- cpu_hyp_reset
- cpu_hyp_reinit
- _kvm_arch_hardware_enable
- kvm_arch_hardware_enable
- _kvm_arch_hardware_disable
- kvm_arch_hardware_disable
- hyp_init_cpu_pm_notifier
- hyp_cpu_pm_init
- hyp_cpu_pm_exit
- hyp_cpu_pm_init
- hyp_cpu_pm_exit
- init_common_resources
- init_subsystems
- teardown_hyp_mode
- init_hyp_mode
- check_kvm_target_cpu
- kvm_mpidr_to_vcpu
- kvm_arch_has_irq_bypass
- kvm_arch_irq_bypass_add_producer
- kvm_arch_irq_bypass_del_producer
- kvm_arch_irq_bypass_stop
- kvm_arch_irq_bypass_start
- kvm_arch_init
- kvm_arch_exit
- arm_init
1
2
3
4
5
6
7 #include <linux/bug.h>
8 #include <linux/cpu_pm.h>
9 #include <linux/errno.h>
10 #include <linux/err.h>
11 #include <linux/kvm_host.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/vmalloc.h>
15 #include <linux/fs.h>
16 #include <linux/mman.h>
17 #include <linux/sched.h>
18 #include <linux/kvm.h>
19 #include <linux/kvm_irqfd.h>
20 #include <linux/irqbypass.h>
21 #include <linux/sched/stat.h>
22 #include <trace/events/kvm.h>
23 #include <kvm/arm_pmu.h>
24 #include <kvm/arm_psci.h>
25
26 #define CREATE_TRACE_POINTS
27 #include "trace.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_emulate.h>
40 #include <asm/kvm_coproc.h>
41 #include <asm/sections.h>
42
43 #ifdef REQUIRES_VIRT
44 __asm__(".arch_extension virt");
45 #endif
46
47 DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data);
48 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
49
50
51 static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
52
53
54 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
55 static u32 kvm_next_vmid;
56 static DEFINE_SPINLOCK(kvm_vmid_lock);
57
58 static bool vgic_present;
59
60 static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
61
62 static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
63 {
64 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
65 }
66
67 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
68
69
70
71
72
73 struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
74 {
75 return __this_cpu_read(kvm_arm_running_vcpu);
76 }
77
78
79
80
81 struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
82 {
83 return &kvm_arm_running_vcpu;
84 }
85
86 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
87 {
88 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
89 }
90
91 int kvm_arch_hardware_setup(void)
92 {
93 return 0;
94 }
95
96 int kvm_arch_check_processor_compat(void)
97 {
98 return 0;
99 }
100
101
102
103
104
105
106 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
107 {
108 int ret, cpu;
109
110 ret = kvm_arm_setup_stage2(kvm, type);
111 if (ret)
112 return ret;
113
114 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran));
115 if (!kvm->arch.last_vcpu_ran)
116 return -ENOMEM;
117
118 for_each_possible_cpu(cpu)
119 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1;
120
121 ret = kvm_alloc_stage2_pgd(kvm);
122 if (ret)
123 goto out_fail_alloc;
124
125 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
126 if (ret)
127 goto out_free_stage2_pgd;
128
129 kvm_vgic_early_init(kvm);
130
131
132 kvm->arch.vmid.vmid_gen = 0;
133
134
135 kvm->arch.max_vcpus = vgic_present ?
136 kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
137
138 return ret;
139 out_free_stage2_pgd:
140 kvm_free_stage2_pgd(kvm);
141 out_fail_alloc:
142 free_percpu(kvm->arch.last_vcpu_ran);
143 kvm->arch.last_vcpu_ran = NULL;
144 return ret;
145 }
146
147 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
148 {
149 return 0;
150 }
151
152 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
153 {
154 return VM_FAULT_SIGBUS;
155 }
156
157
158
159
160
161
162 void kvm_arch_destroy_vm(struct kvm *kvm)
163 {
164 int i;
165
166 kvm_vgic_destroy(kvm);
167
168 free_percpu(kvm->arch.last_vcpu_ran);
169 kvm->arch.last_vcpu_ran = NULL;
170
171 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
172 if (kvm->vcpus[i]) {
173 kvm_arch_vcpu_free(kvm->vcpus[i]);
174 kvm->vcpus[i] = NULL;
175 }
176 }
177 atomic_set(&kvm->online_vcpus, 0);
178 }
179
180 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
181 {
182 int r;
183 switch (ext) {
184 case KVM_CAP_IRQCHIP:
185 r = vgic_present;
186 break;
187 case KVM_CAP_IOEVENTFD:
188 case KVM_CAP_DEVICE_CTRL:
189 case KVM_CAP_USER_MEMORY:
190 case KVM_CAP_SYNC_MMU:
191 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
192 case KVM_CAP_ONE_REG:
193 case KVM_CAP_ARM_PSCI:
194 case KVM_CAP_ARM_PSCI_0_2:
195 case KVM_CAP_READONLY_MEM:
196 case KVM_CAP_MP_STATE:
197 case KVM_CAP_IMMEDIATE_EXIT:
198 case KVM_CAP_VCPU_EVENTS:
199 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
200 r = 1;
201 break;
202 case KVM_CAP_ARM_SET_DEVICE_ADDR:
203 r = 1;
204 break;
205 case KVM_CAP_NR_VCPUS:
206 r = num_online_cpus();
207 break;
208 case KVM_CAP_MAX_VCPUS:
209 r = KVM_MAX_VCPUS;
210 break;
211 case KVM_CAP_MAX_VCPU_ID:
212 r = KVM_MAX_VCPU_ID;
213 break;
214 case KVM_CAP_MSI_DEVID:
215 if (!kvm)
216 r = -EINVAL;
217 else
218 r = kvm->arch.vgic.msis_require_devid;
219 break;
220 case KVM_CAP_ARM_USER_IRQ:
221
222
223
224
225 r = 1;
226 break;
227 default:
228 r = kvm_arch_vm_ioctl_check_extension(kvm, ext);
229 break;
230 }
231 return r;
232 }
233
234 long kvm_arch_dev_ioctl(struct file *filp,
235 unsigned int ioctl, unsigned long arg)
236 {
237 return -EINVAL;
238 }
239
240 struct kvm *kvm_arch_alloc_vm(void)
241 {
242 if (!has_vhe())
243 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
244
245 return vzalloc(sizeof(struct kvm));
246 }
247
248 void kvm_arch_free_vm(struct kvm *kvm)
249 {
250 if (!has_vhe())
251 kfree(kvm);
252 else
253 vfree(kvm);
254 }
255
256 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
257 {
258 int err;
259 struct kvm_vcpu *vcpu;
260
261 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) {
262 err = -EBUSY;
263 goto out;
264 }
265
266 if (id >= kvm->arch.max_vcpus) {
267 err = -EINVAL;
268 goto out;
269 }
270
271 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
272 if (!vcpu) {
273 err = -ENOMEM;
274 goto out;
275 }
276
277 err = kvm_vcpu_init(vcpu, kvm, id);
278 if (err)
279 goto free_vcpu;
280
281 err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
282 if (err)
283 goto vcpu_uninit;
284
285 return vcpu;
286 vcpu_uninit:
287 kvm_vcpu_uninit(vcpu);
288 free_vcpu:
289 kmem_cache_free(kvm_vcpu_cache, vcpu);
290 out:
291 return ERR_PTR(err);
292 }
293
294 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
295 {
296 }
297
298 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
299 {
300 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
301 static_branch_dec(&userspace_irqchip_in_use);
302
303 kvm_mmu_free_memory_caches(vcpu);
304 kvm_timer_vcpu_terminate(vcpu);
305 kvm_pmu_vcpu_destroy(vcpu);
306 kvm_vcpu_uninit(vcpu);
307 kmem_cache_free(kvm_vcpu_cache, vcpu);
308 }
309
310 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
311 {
312 kvm_arch_vcpu_free(vcpu);
313 }
314
315 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
316 {
317 return kvm_timer_is_pending(vcpu);
318 }
319
320 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
321 {
322
323
324
325
326
327
328
329 preempt_disable();
330 kvm_vgic_vmcr_sync(vcpu);
331 preempt_enable();
332
333 kvm_vgic_v4_enable_doorbell(vcpu);
334 }
335
336 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
337 {
338 kvm_vgic_v4_disable_doorbell(vcpu);
339 }
340
341 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
342 {
343
344 vcpu->arch.target = -1;
345 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
346
347
348 kvm_timer_vcpu_init(vcpu);
349
350 kvm_pmu_vcpu_init(vcpu);
351
352 kvm_arm_reset_debug_ptr(vcpu);
353
354 return kvm_vgic_vcpu_init(vcpu);
355 }
356
357 #ifdef CONFIG_ARM64
358 #define __ptrauth_save_key(regs, key) \
359 ({ \
360 regs[key ## KEYLO_EL1] = read_sysreg_s(SYS_ ## key ## KEYLO_EL1); \
361 regs[key ## KEYHI_EL1] = read_sysreg_s(SYS_ ## key ## KEYHI_EL1); \
362 })
363 #else
364 #define __ptrauth_save_key(regs, key) do { } while (0)
365 #endif
366
367 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
368 {
369 int *last_ran;
370 kvm_host_data_t *cpu_data;
371
372 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
373 cpu_data = this_cpu_ptr(&kvm_host_data);
374
375
376
377
378
379 if (*last_ran != vcpu->vcpu_id) {
380 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu);
381 *last_ran = vcpu->vcpu_id;
382 }
383
384 vcpu->cpu = cpu;
385 vcpu->arch.host_cpu_context = &cpu_data->host_ctxt;
386
387 kvm_arm_set_running_vcpu(vcpu);
388 kvm_vgic_load(vcpu);
389 kvm_timer_vcpu_load(vcpu);
390 kvm_vcpu_load_sysregs(vcpu);
391 kvm_arch_vcpu_load_fp(vcpu);
392 kvm_vcpu_pmu_restore_guest(vcpu);
393
394 if (single_task_running())
395 vcpu_clear_wfe_traps(vcpu);
396 else
397 vcpu_set_wfe_traps(vcpu);
398
399 if (vcpu_has_ptrauth(vcpu)) {
400 struct kvm_cpu_context __maybe_unused *ctxt = vcpu->arch.host_cpu_context;
401
402 __ptrauth_save_key(ctxt->sys_regs, APIA);
403 __ptrauth_save_key(ctxt->sys_regs, APIB);
404 __ptrauth_save_key(ctxt->sys_regs, APDA);
405 __ptrauth_save_key(ctxt->sys_regs, APDB);
406 __ptrauth_save_key(ctxt->sys_regs, APGA);
407
408 vcpu_ptrauth_disable(vcpu);
409 }
410 }
411
412 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
413 {
414 kvm_arch_vcpu_put_fp(vcpu);
415 kvm_vcpu_put_sysregs(vcpu);
416 kvm_timer_vcpu_put(vcpu);
417 kvm_vgic_put(vcpu);
418 kvm_vcpu_pmu_restore_host(vcpu);
419
420 vcpu->cpu = -1;
421
422 kvm_arm_set_running_vcpu(NULL);
423 }
424
425 static void vcpu_power_off(struct kvm_vcpu *vcpu)
426 {
427 vcpu->arch.power_off = true;
428 kvm_make_request(KVM_REQ_SLEEP, vcpu);
429 kvm_vcpu_kick(vcpu);
430 }
431
432 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
433 struct kvm_mp_state *mp_state)
434 {
435 if (vcpu->arch.power_off)
436 mp_state->mp_state = KVM_MP_STATE_STOPPED;
437 else
438 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
439
440 return 0;
441 }
442
443 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
444 struct kvm_mp_state *mp_state)
445 {
446 int ret = 0;
447
448 switch (mp_state->mp_state) {
449 case KVM_MP_STATE_RUNNABLE:
450 vcpu->arch.power_off = false;
451 break;
452 case KVM_MP_STATE_STOPPED:
453 vcpu_power_off(vcpu);
454 break;
455 default:
456 ret = -EINVAL;
457 }
458
459 return ret;
460 }
461
462
463
464
465
466
467
468
469 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
470 {
471 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
472 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
473 && !v->arch.power_off && !v->arch.pause);
474 }
475
476 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
477 {
478 return vcpu_mode_priv(vcpu);
479 }
480
481
482 static void exit_vm_noop(void *info)
483 {
484 }
485
486 void force_vm_exit(const cpumask_t *mask)
487 {
488 preempt_disable();
489 smp_call_function_many(mask, exit_vm_noop, NULL, true);
490 preempt_enable();
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504
505 static bool need_new_vmid_gen(struct kvm_vmid *vmid)
506 {
507 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
508 smp_rmb();
509 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
510 }
511
512
513
514
515
516
517 static void update_vmid(struct kvm_vmid *vmid)
518 {
519 if (!need_new_vmid_gen(vmid))
520 return;
521
522 spin_lock(&kvm_vmid_lock);
523
524
525
526
527
528
529 if (!need_new_vmid_gen(vmid)) {
530 spin_unlock(&kvm_vmid_lock);
531 return;
532 }
533
534
535 if (unlikely(kvm_next_vmid == 0)) {
536 atomic64_inc(&kvm_vmid_gen);
537 kvm_next_vmid = 1;
538
539
540
541
542
543
544 force_vm_exit(cpu_all_mask);
545
546
547
548
549
550 kvm_call_hyp(__kvm_flush_vm_context);
551 }
552
553 vmid->vmid = kvm_next_vmid;
554 kvm_next_vmid++;
555 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
556
557 smp_wmb();
558 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
559
560 spin_unlock(&kvm_vmid_lock);
561 }
562
563 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
564 {
565 struct kvm *kvm = vcpu->kvm;
566 int ret = 0;
567
568 if (likely(vcpu->arch.has_run_once))
569 return 0;
570
571 if (!kvm_arm_vcpu_is_finalized(vcpu))
572 return -EPERM;
573
574 vcpu->arch.has_run_once = true;
575
576 if (likely(irqchip_in_kernel(kvm))) {
577
578
579
580
581 if (unlikely(!vgic_ready(kvm))) {
582 ret = kvm_vgic_map_resources(kvm);
583 if (ret)
584 return ret;
585 }
586 } else {
587
588
589
590
591 static_branch_inc(&userspace_irqchip_in_use);
592 }
593
594 ret = kvm_timer_enable(vcpu);
595 if (ret)
596 return ret;
597
598 ret = kvm_arm_pmu_v3_enable(vcpu);
599
600 return ret;
601 }
602
603 bool kvm_arch_intc_initialized(struct kvm *kvm)
604 {
605 return vgic_initialized(kvm);
606 }
607
608 void kvm_arm_halt_guest(struct kvm *kvm)
609 {
610 int i;
611 struct kvm_vcpu *vcpu;
612
613 kvm_for_each_vcpu(i, vcpu, kvm)
614 vcpu->arch.pause = true;
615 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
616 }
617
618 void kvm_arm_resume_guest(struct kvm *kvm)
619 {
620 int i;
621 struct kvm_vcpu *vcpu;
622
623 kvm_for_each_vcpu(i, vcpu, kvm) {
624 vcpu->arch.pause = false;
625 swake_up_one(kvm_arch_vcpu_wq(vcpu));
626 }
627 }
628
629 static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
630 {
631 struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
632
633 swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) &&
634 (!vcpu->arch.pause)));
635
636 if (vcpu->arch.power_off || vcpu->arch.pause) {
637
638 kvm_make_request(KVM_REQ_SLEEP, vcpu);
639 }
640
641
642
643
644
645
646 smp_rmb();
647 }
648
649 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
650 {
651 return vcpu->arch.target >= 0;
652 }
653
654 static void check_vcpu_requests(struct kvm_vcpu *vcpu)
655 {
656 if (kvm_request_pending(vcpu)) {
657 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
658 vcpu_req_sleep(vcpu);
659
660 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
661 kvm_reset_vcpu(vcpu);
662
663
664
665
666
667 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
668 }
669 }
670
671
672
673
674
675
676
677
678
679
680
681
682 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
683 {
684 int ret;
685
686 if (unlikely(!kvm_vcpu_initialized(vcpu)))
687 return -ENOEXEC;
688
689 ret = kvm_vcpu_first_run_init(vcpu);
690 if (ret)
691 return ret;
692
693 if (run->exit_reason == KVM_EXIT_MMIO) {
694 ret = kvm_handle_mmio_return(vcpu, vcpu->run);
695 if (ret)
696 return ret;
697 }
698
699 if (run->immediate_exit)
700 return -EINTR;
701
702 vcpu_load(vcpu);
703
704 kvm_sigset_activate(vcpu);
705
706 ret = 1;
707 run->exit_reason = KVM_EXIT_UNKNOWN;
708 while (ret > 0) {
709
710
711
712 cond_resched();
713
714 update_vmid(&vcpu->kvm->arch.vmid);
715
716 check_vcpu_requests(vcpu);
717
718
719
720
721
722
723 preempt_disable();
724
725 kvm_pmu_flush_hwstate(vcpu);
726
727 local_irq_disable();
728
729 kvm_vgic_flush_hwstate(vcpu);
730
731
732
733
734
735 if (signal_pending(current)) {
736 ret = -EINTR;
737 run->exit_reason = KVM_EXIT_INTR;
738 }
739
740
741
742
743
744
745
746
747 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
748 if (kvm_timer_should_notify_user(vcpu) ||
749 kvm_pmu_should_notify_user(vcpu)) {
750 ret = -EINTR;
751 run->exit_reason = KVM_EXIT_INTR;
752 }
753 }
754
755
756
757
758
759
760
761 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
762
763 if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) ||
764 kvm_request_pending(vcpu)) {
765 vcpu->mode = OUTSIDE_GUEST_MODE;
766 isb();
767 kvm_pmu_sync_hwstate(vcpu);
768 if (static_branch_unlikely(&userspace_irqchip_in_use))
769 kvm_timer_sync_hwstate(vcpu);
770 kvm_vgic_sync_hwstate(vcpu);
771 local_irq_enable();
772 preempt_enable();
773 continue;
774 }
775
776 kvm_arm_setup_debug(vcpu);
777
778
779
780
781 trace_kvm_entry(*vcpu_pc(vcpu));
782 guest_enter_irqoff();
783
784 if (has_vhe()) {
785 kvm_arm_vhe_guest_enter();
786 ret = kvm_vcpu_run_vhe(vcpu);
787 kvm_arm_vhe_guest_exit();
788 } else {
789 ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu);
790 }
791
792 vcpu->mode = OUTSIDE_GUEST_MODE;
793 vcpu->stat.exits++;
794
795
796
797
798 kvm_arm_clear_debug(vcpu);
799
800
801
802
803
804
805 kvm_pmu_sync_hwstate(vcpu);
806
807
808
809
810
811
812 kvm_vgic_sync_hwstate(vcpu);
813
814
815
816
817
818
819 if (static_branch_unlikely(&userspace_irqchip_in_use))
820 kvm_timer_sync_hwstate(vcpu);
821
822 kvm_arch_vcpu_ctxsync_fp(vcpu);
823
824
825
826
827
828
829
830
831
832
833
834 local_irq_enable();
835
836
837
838
839
840
841
842
843
844 guest_exit();
845 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
846
847
848 handle_exit_early(vcpu, run, ret);
849
850 preempt_enable();
851
852 ret = handle_exit(vcpu, run, ret);
853 }
854
855
856 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
857 kvm_timer_update_run(vcpu);
858 kvm_pmu_update_run(vcpu);
859 }
860
861 kvm_sigset_deactivate(vcpu);
862
863 vcpu_put(vcpu);
864 return ret;
865 }
866
867 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
868 {
869 int bit_index;
870 bool set;
871 unsigned long *hcr;
872
873 if (number == KVM_ARM_IRQ_CPU_IRQ)
874 bit_index = __ffs(HCR_VI);
875 else
876 bit_index = __ffs(HCR_VF);
877
878 hcr = vcpu_hcr(vcpu);
879 if (level)
880 set = test_and_set_bit(bit_index, hcr);
881 else
882 set = test_and_clear_bit(bit_index, hcr);
883
884
885
886
887 if (set == level)
888 return 0;
889
890
891
892
893
894
895 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
896 kvm_vcpu_kick(vcpu);
897
898 return 0;
899 }
900
901 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
902 bool line_status)
903 {
904 u32 irq = irq_level->irq;
905 unsigned int irq_type, vcpu_idx, irq_num;
906 int nrcpus = atomic_read(&kvm->online_vcpus);
907 struct kvm_vcpu *vcpu = NULL;
908 bool level = irq_level->level;
909
910 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
911 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
912 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
913 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
914
915 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
916
917 switch (irq_type) {
918 case KVM_ARM_IRQ_TYPE_CPU:
919 if (irqchip_in_kernel(kvm))
920 return -ENXIO;
921
922 if (vcpu_idx >= nrcpus)
923 return -EINVAL;
924
925 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
926 if (!vcpu)
927 return -EINVAL;
928
929 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
930 return -EINVAL;
931
932 return vcpu_interrupt_line(vcpu, irq_num, level);
933 case KVM_ARM_IRQ_TYPE_PPI:
934 if (!irqchip_in_kernel(kvm))
935 return -ENXIO;
936
937 if (vcpu_idx >= nrcpus)
938 return -EINVAL;
939
940 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
941 if (!vcpu)
942 return -EINVAL;
943
944 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
945 return -EINVAL;
946
947 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
948 case KVM_ARM_IRQ_TYPE_SPI:
949 if (!irqchip_in_kernel(kvm))
950 return -ENXIO;
951
952 if (irq_num < VGIC_NR_PRIVATE_IRQS)
953 return -EINVAL;
954
955 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
956 }
957
958 return -EINVAL;
959 }
960
961 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
962 const struct kvm_vcpu_init *init)
963 {
964 unsigned int i, ret;
965 int phys_target = kvm_target_cpu();
966
967 if (init->target != phys_target)
968 return -EINVAL;
969
970
971
972
973
974 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
975 return -EINVAL;
976
977
978 for (i = 0; i < sizeof(init->features) * 8; i++) {
979 bool set = (init->features[i / 32] & (1 << (i % 32)));
980
981 if (set && i >= KVM_VCPU_MAX_FEATURES)
982 return -ENOENT;
983
984
985
986
987
988 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
989 test_bit(i, vcpu->arch.features) != set)
990 return -EINVAL;
991
992 if (set)
993 set_bit(i, vcpu->arch.features);
994 }
995
996 vcpu->arch.target = phys_target;
997
998
999 ret = kvm_reset_vcpu(vcpu);
1000 if (ret) {
1001 vcpu->arch.target = -1;
1002 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1003 }
1004
1005 return ret;
1006 }
1007
1008 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1009 struct kvm_vcpu_init *init)
1010 {
1011 int ret;
1012
1013 ret = kvm_vcpu_set_target(vcpu, init);
1014 if (ret)
1015 return ret;
1016
1017
1018
1019
1020
1021 if (vcpu->arch.has_run_once)
1022 stage2_unmap_vm(vcpu->kvm);
1023
1024 vcpu_reset_hcr(vcpu);
1025
1026
1027
1028
1029 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
1030 vcpu_power_off(vcpu);
1031 else
1032 vcpu->arch.power_off = false;
1033
1034 return 0;
1035 }
1036
1037 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1038 struct kvm_device_attr *attr)
1039 {
1040 int ret = -ENXIO;
1041
1042 switch (attr->group) {
1043 default:
1044 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
1045 break;
1046 }
1047
1048 return ret;
1049 }
1050
1051 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1052 struct kvm_device_attr *attr)
1053 {
1054 int ret = -ENXIO;
1055
1056 switch (attr->group) {
1057 default:
1058 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
1059 break;
1060 }
1061
1062 return ret;
1063 }
1064
1065 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1066 struct kvm_device_attr *attr)
1067 {
1068 int ret = -ENXIO;
1069
1070 switch (attr->group) {
1071 default:
1072 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
1073 break;
1074 }
1075
1076 return ret;
1077 }
1078
1079 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1080 struct kvm_vcpu_events *events)
1081 {
1082 memset(events, 0, sizeof(*events));
1083
1084 return __kvm_arm_vcpu_get_events(vcpu, events);
1085 }
1086
1087 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1088 struct kvm_vcpu_events *events)
1089 {
1090 int i;
1091
1092
1093 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1094 if (events->reserved[i])
1095 return -EINVAL;
1096
1097
1098 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1099 if (events->exception.pad[i])
1100 return -EINVAL;
1101
1102 return __kvm_arm_vcpu_set_events(vcpu, events);
1103 }
1104
1105 long kvm_arch_vcpu_ioctl(struct file *filp,
1106 unsigned int ioctl, unsigned long arg)
1107 {
1108 struct kvm_vcpu *vcpu = filp->private_data;
1109 void __user *argp = (void __user *)arg;
1110 struct kvm_device_attr attr;
1111 long r;
1112
1113 switch (ioctl) {
1114 case KVM_ARM_VCPU_INIT: {
1115 struct kvm_vcpu_init init;
1116
1117 r = -EFAULT;
1118 if (copy_from_user(&init, argp, sizeof(init)))
1119 break;
1120
1121 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1122 break;
1123 }
1124 case KVM_SET_ONE_REG:
1125 case KVM_GET_ONE_REG: {
1126 struct kvm_one_reg reg;
1127
1128 r = -ENOEXEC;
1129 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1130 break;
1131
1132 r = -EFAULT;
1133 if (copy_from_user(®, argp, sizeof(reg)))
1134 break;
1135
1136 if (ioctl == KVM_SET_ONE_REG)
1137 r = kvm_arm_set_reg(vcpu, ®);
1138 else
1139 r = kvm_arm_get_reg(vcpu, ®);
1140 break;
1141 }
1142 case KVM_GET_REG_LIST: {
1143 struct kvm_reg_list __user *user_list = argp;
1144 struct kvm_reg_list reg_list;
1145 unsigned n;
1146
1147 r = -ENOEXEC;
1148 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1149 break;
1150
1151 r = -EPERM;
1152 if (!kvm_arm_vcpu_is_finalized(vcpu))
1153 break;
1154
1155 r = -EFAULT;
1156 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
1157 break;
1158 n = reg_list.n;
1159 reg_list.n = kvm_arm_num_regs(vcpu);
1160 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
1161 break;
1162 r = -E2BIG;
1163 if (n < reg_list.n)
1164 break;
1165 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1166 break;
1167 }
1168 case KVM_SET_DEVICE_ATTR: {
1169 r = -EFAULT;
1170 if (copy_from_user(&attr, argp, sizeof(attr)))
1171 break;
1172 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1173 break;
1174 }
1175 case KVM_GET_DEVICE_ATTR: {
1176 r = -EFAULT;
1177 if (copy_from_user(&attr, argp, sizeof(attr)))
1178 break;
1179 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1180 break;
1181 }
1182 case KVM_HAS_DEVICE_ATTR: {
1183 r = -EFAULT;
1184 if (copy_from_user(&attr, argp, sizeof(attr)))
1185 break;
1186 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1187 break;
1188 }
1189 case KVM_GET_VCPU_EVENTS: {
1190 struct kvm_vcpu_events events;
1191
1192 if (kvm_arm_vcpu_get_events(vcpu, &events))
1193 return -EINVAL;
1194
1195 if (copy_to_user(argp, &events, sizeof(events)))
1196 return -EFAULT;
1197
1198 return 0;
1199 }
1200 case KVM_SET_VCPU_EVENTS: {
1201 struct kvm_vcpu_events events;
1202
1203 if (copy_from_user(&events, argp, sizeof(events)))
1204 return -EFAULT;
1205
1206 return kvm_arm_vcpu_set_events(vcpu, &events);
1207 }
1208 case KVM_ARM_VCPU_FINALIZE: {
1209 int what;
1210
1211 if (!kvm_vcpu_initialized(vcpu))
1212 return -ENOEXEC;
1213
1214 if (get_user(what, (const int __user *)argp))
1215 return -EFAULT;
1216
1217 return kvm_arm_vcpu_finalize(vcpu, what);
1218 }
1219 default:
1220 r = -EINVAL;
1221 }
1222
1223 return r;
1224 }
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1246 {
1247 bool flush = false;
1248 int r;
1249
1250 mutex_lock(&kvm->slots_lock);
1251
1252 r = kvm_get_dirty_log_protect(kvm, log, &flush);
1253
1254 if (flush)
1255 kvm_flush_remote_tlbs(kvm);
1256
1257 mutex_unlock(&kvm->slots_lock);
1258 return r;
1259 }
1260
1261 int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
1262 {
1263 bool flush = false;
1264 int r;
1265
1266 mutex_lock(&kvm->slots_lock);
1267
1268 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
1269
1270 if (flush)
1271 kvm_flush_remote_tlbs(kvm);
1272
1273 mutex_unlock(&kvm->slots_lock);
1274 return r;
1275 }
1276
1277 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1278 struct kvm_arm_device_addr *dev_addr)
1279 {
1280 unsigned long dev_id, type;
1281
1282 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1283 KVM_ARM_DEVICE_ID_SHIFT;
1284 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1285 KVM_ARM_DEVICE_TYPE_SHIFT;
1286
1287 switch (dev_id) {
1288 case KVM_ARM_DEVICE_VGIC_V2:
1289 if (!vgic_present)
1290 return -ENXIO;
1291 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
1292 default:
1293 return -ENODEV;
1294 }
1295 }
1296
1297 long kvm_arch_vm_ioctl(struct file *filp,
1298 unsigned int ioctl, unsigned long arg)
1299 {
1300 struct kvm *kvm = filp->private_data;
1301 void __user *argp = (void __user *)arg;
1302
1303 switch (ioctl) {
1304 case KVM_CREATE_IRQCHIP: {
1305 int ret;
1306 if (!vgic_present)
1307 return -ENXIO;
1308 mutex_lock(&kvm->lock);
1309 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1310 mutex_unlock(&kvm->lock);
1311 return ret;
1312 }
1313 case KVM_ARM_SET_DEVICE_ADDR: {
1314 struct kvm_arm_device_addr dev_addr;
1315
1316 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1317 return -EFAULT;
1318 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1319 }
1320 case KVM_ARM_PREFERRED_TARGET: {
1321 int err;
1322 struct kvm_vcpu_init init;
1323
1324 err = kvm_vcpu_preferred_target(&init);
1325 if (err)
1326 return err;
1327
1328 if (copy_to_user(argp, &init, sizeof(init)))
1329 return -EFAULT;
1330
1331 return 0;
1332 }
1333 default:
1334 return -EINVAL;
1335 }
1336 }
1337
1338 static void cpu_init_hyp_mode(void *dummy)
1339 {
1340 phys_addr_t pgd_ptr;
1341 unsigned long hyp_stack_ptr;
1342 unsigned long stack_page;
1343 unsigned long vector_ptr;
1344
1345
1346 __hyp_set_vectors(kvm_get_idmap_vector());
1347
1348 pgd_ptr = kvm_mmu_get_httbr();
1349 stack_page = __this_cpu_read(kvm_arm_hyp_stack_page);
1350 hyp_stack_ptr = stack_page + PAGE_SIZE;
1351 vector_ptr = (unsigned long)kvm_get_hyp_vector();
1352
1353 __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr);
1354 __cpu_init_stage2();
1355 }
1356
1357 static void cpu_hyp_reset(void)
1358 {
1359 if (!is_kernel_in_hyp_mode())
1360 __hyp_reset_vectors();
1361 }
1362
1363 static void cpu_hyp_reinit(void)
1364 {
1365 kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt);
1366
1367 cpu_hyp_reset();
1368
1369 if (is_kernel_in_hyp_mode())
1370 kvm_timer_init_vhe();
1371 else
1372 cpu_init_hyp_mode(NULL);
1373
1374 kvm_arm_init_debug();
1375
1376 if (vgic_present)
1377 kvm_vgic_init_cpu_hardware();
1378 }
1379
1380 static void _kvm_arch_hardware_enable(void *discard)
1381 {
1382 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1383 cpu_hyp_reinit();
1384 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1385 }
1386 }
1387
1388 int kvm_arch_hardware_enable(void)
1389 {
1390 _kvm_arch_hardware_enable(NULL);
1391 return 0;
1392 }
1393
1394 static void _kvm_arch_hardware_disable(void *discard)
1395 {
1396 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1397 cpu_hyp_reset();
1398 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1399 }
1400 }
1401
1402 void kvm_arch_hardware_disable(void)
1403 {
1404 _kvm_arch_hardware_disable(NULL);
1405 }
1406
1407 #ifdef CONFIG_CPU_PM
1408 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1409 unsigned long cmd,
1410 void *v)
1411 {
1412
1413
1414
1415
1416
1417 switch (cmd) {
1418 case CPU_PM_ENTER:
1419 if (__this_cpu_read(kvm_arm_hardware_enabled))
1420
1421
1422
1423
1424
1425 cpu_hyp_reset();
1426
1427 return NOTIFY_OK;
1428 case CPU_PM_ENTER_FAILED:
1429 case CPU_PM_EXIT:
1430 if (__this_cpu_read(kvm_arm_hardware_enabled))
1431
1432 cpu_hyp_reinit();
1433
1434 return NOTIFY_OK;
1435
1436 default:
1437 return NOTIFY_DONE;
1438 }
1439 }
1440
1441 static struct notifier_block hyp_init_cpu_pm_nb = {
1442 .notifier_call = hyp_init_cpu_pm_notifier,
1443 };
1444
1445 static void __init hyp_cpu_pm_init(void)
1446 {
1447 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1448 }
1449 static void __init hyp_cpu_pm_exit(void)
1450 {
1451 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1452 }
1453 #else
1454 static inline void hyp_cpu_pm_init(void)
1455 {
1456 }
1457 static inline void hyp_cpu_pm_exit(void)
1458 {
1459 }
1460 #endif
1461
1462 static int init_common_resources(void)
1463 {
1464 kvm_set_ipa_limit();
1465
1466 return 0;
1467 }
1468
1469 static int init_subsystems(void)
1470 {
1471 int err = 0;
1472
1473
1474
1475
1476 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
1477
1478
1479
1480
1481 hyp_cpu_pm_init();
1482
1483
1484
1485
1486 err = kvm_vgic_hyp_init();
1487 switch (err) {
1488 case 0:
1489 vgic_present = true;
1490 break;
1491 case -ENODEV:
1492 case -ENXIO:
1493 vgic_present = false;
1494 err = 0;
1495 break;
1496 default:
1497 goto out;
1498 }
1499
1500
1501
1502
1503 err = kvm_timer_hyp_init(vgic_present);
1504 if (err)
1505 goto out;
1506
1507 kvm_perf_init();
1508 kvm_coproc_table_init();
1509
1510 out:
1511 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1512
1513 return err;
1514 }
1515
1516 static void teardown_hyp_mode(void)
1517 {
1518 int cpu;
1519
1520 free_hyp_pgds();
1521 for_each_possible_cpu(cpu)
1522 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1523 hyp_cpu_pm_exit();
1524 }
1525
1526
1527
1528
1529 static int init_hyp_mode(void)
1530 {
1531 int cpu;
1532 int err = 0;
1533
1534
1535
1536
1537 err = kvm_mmu_init();
1538 if (err)
1539 goto out_err;
1540
1541
1542
1543
1544 for_each_possible_cpu(cpu) {
1545 unsigned long stack_page;
1546
1547 stack_page = __get_free_page(GFP_KERNEL);
1548 if (!stack_page) {
1549 err = -ENOMEM;
1550 goto out_err;
1551 }
1552
1553 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
1554 }
1555
1556
1557
1558
1559 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
1560 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
1561 if (err) {
1562 kvm_err("Cannot map world-switch code\n");
1563 goto out_err;
1564 }
1565
1566 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
1567 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
1568 if (err) {
1569 kvm_err("Cannot map rodata section\n");
1570 goto out_err;
1571 }
1572
1573 err = create_hyp_mappings(kvm_ksym_ref(__bss_start),
1574 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
1575 if (err) {
1576 kvm_err("Cannot map bss section\n");
1577 goto out_err;
1578 }
1579
1580 err = kvm_map_vectors();
1581 if (err) {
1582 kvm_err("Cannot map vectors\n");
1583 goto out_err;
1584 }
1585
1586
1587
1588
1589 for_each_possible_cpu(cpu) {
1590 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
1591 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
1592 PAGE_HYP);
1593
1594 if (err) {
1595 kvm_err("Cannot map hyp stack\n");
1596 goto out_err;
1597 }
1598 }
1599
1600 for_each_possible_cpu(cpu) {
1601 kvm_host_data_t *cpu_data;
1602
1603 cpu_data = per_cpu_ptr(&kvm_host_data, cpu);
1604 err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP);
1605
1606 if (err) {
1607 kvm_err("Cannot map host CPU state: %d\n", err);
1608 goto out_err;
1609 }
1610 }
1611
1612 err = hyp_map_aux_data();
1613 if (err)
1614 kvm_err("Cannot map host auxiliary data: %d\n", err);
1615
1616 return 0;
1617
1618 out_err:
1619 teardown_hyp_mode();
1620 kvm_err("error initializing Hyp mode: %d\n", err);
1621 return err;
1622 }
1623
1624 static void check_kvm_target_cpu(void *ret)
1625 {
1626 *(int *)ret = kvm_target_cpu();
1627 }
1628
1629 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
1630 {
1631 struct kvm_vcpu *vcpu;
1632 int i;
1633
1634 mpidr &= MPIDR_HWID_BITMASK;
1635 kvm_for_each_vcpu(i, vcpu, kvm) {
1636 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
1637 return vcpu;
1638 }
1639 return NULL;
1640 }
1641
1642 bool kvm_arch_has_irq_bypass(void)
1643 {
1644 return true;
1645 }
1646
1647 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
1648 struct irq_bypass_producer *prod)
1649 {
1650 struct kvm_kernel_irqfd *irqfd =
1651 container_of(cons, struct kvm_kernel_irqfd, consumer);
1652
1653 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
1654 &irqfd->irq_entry);
1655 }
1656 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
1657 struct irq_bypass_producer *prod)
1658 {
1659 struct kvm_kernel_irqfd *irqfd =
1660 container_of(cons, struct kvm_kernel_irqfd, consumer);
1661
1662 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
1663 &irqfd->irq_entry);
1664 }
1665
1666 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
1667 {
1668 struct kvm_kernel_irqfd *irqfd =
1669 container_of(cons, struct kvm_kernel_irqfd, consumer);
1670
1671 kvm_arm_halt_guest(irqfd->kvm);
1672 }
1673
1674 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
1675 {
1676 struct kvm_kernel_irqfd *irqfd =
1677 container_of(cons, struct kvm_kernel_irqfd, consumer);
1678
1679 kvm_arm_resume_guest(irqfd->kvm);
1680 }
1681
1682
1683
1684
1685 int kvm_arch_init(void *opaque)
1686 {
1687 int err;
1688 int ret, cpu;
1689 bool in_hyp_mode;
1690
1691 if (!is_hyp_mode_available()) {
1692 kvm_info("HYP mode not available\n");
1693 return -ENODEV;
1694 }
1695
1696 in_hyp_mode = is_kernel_in_hyp_mode();
1697
1698 if (!in_hyp_mode && kvm_arch_requires_vhe()) {
1699 kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n");
1700 return -ENODEV;
1701 }
1702
1703 for_each_online_cpu(cpu) {
1704 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
1705 if (ret < 0) {
1706 kvm_err("Error, CPU %d not supported!\n", cpu);
1707 return -ENODEV;
1708 }
1709 }
1710
1711 err = init_common_resources();
1712 if (err)
1713 return err;
1714
1715 err = kvm_arm_init_sve();
1716 if (err)
1717 return err;
1718
1719 if (!in_hyp_mode) {
1720 err = init_hyp_mode();
1721 if (err)
1722 goto out_err;
1723 }
1724
1725 err = init_subsystems();
1726 if (err)
1727 goto out_hyp;
1728
1729 if (in_hyp_mode)
1730 kvm_info("VHE mode initialized successfully\n");
1731 else
1732 kvm_info("Hyp mode initialized successfully\n");
1733
1734 return 0;
1735
1736 out_hyp:
1737 if (!in_hyp_mode)
1738 teardown_hyp_mode();
1739 out_err:
1740 return err;
1741 }
1742
1743
1744 void kvm_arch_exit(void)
1745 {
1746 kvm_perf_teardown();
1747 }
1748
1749 static int arm_init(void)
1750 {
1751 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1752 return rc;
1753 }
1754
1755 module_init(arm_init);