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