Thomas Gleixner | d94d71c | 2019-05-29 07:12:40 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 4 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 7 | #include <linux/bug.h> |
Lorenzo Pieralisi | 1fcf7ce | 2013-08-05 15:04:46 +0100 | [diff] [blame] | 8 | #include <linux/cpu_pm.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 9 | #include <linux/errno.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/kvm_host.h> |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 12 | #include <linux/list.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 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> |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 18 | #include <linux/kvm.h> |
Eric Auger | 2412405 | 2017-10-27 15:28:31 +0100 | [diff] [blame] | 19 | #include <linux/kvm_irqfd.h> |
| 20 | #include <linux/irqbypass.h> |
Marc Zyngier | de73708 | 2018-06-21 10:43:59 +0100 | [diff] [blame] | 21 | #include <linux/sched/stat.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 22 | #include <trace/events/kvm.h> |
| 23 | |
| 24 | #define CREATE_TRACE_POINTS |
Marc Zyngier | 9ed24f4 | 2020-05-13 11:40:34 +0100 | [diff] [blame] | 25 | #include "trace_arm.h" |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 26 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 28 | #include <asm/ptrace.h> |
| 29 | #include <asm/mman.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 30 | #include <asm/tlbflush.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 31 | #include <asm/cacheflush.h> |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 32 | #include <asm/cpufeature.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 33 | #include <asm/virt.h> |
| 34 | #include <asm/kvm_arm.h> |
| 35 | #include <asm/kvm_asm.h> |
| 36 | #include <asm/kvm_mmu.h> |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 37 | #include <asm/kvm_emulate.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 38 | #include <asm/kvm_coproc.h> |
Marc Zyngier | 910917b | 2015-10-27 12:18:48 +0000 | [diff] [blame] | 39 | #include <asm/sections.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 40 | |
Steven Price | 8564d63 | 2019-10-21 16:28:18 +0100 | [diff] [blame] | 41 | #include <kvm/arm_hypercalls.h> |
| 42 | #include <kvm/arm_pmu.h> |
| 43 | #include <kvm/arm_psci.h> |
| 44 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 45 | #ifdef REQUIRES_VIRT |
| 46 | __asm__(".arch_extension virt"); |
| 47 | #endif |
| 48 | |
Andrew Murray | 630a168 | 2019-04-09 20:22:11 +0100 | [diff] [blame] | 49 | DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 50 | static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 51 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 52 | /* The VMID used in the VTTBR */ |
| 53 | static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); |
Vladimir Murzin | 20475f7 | 2015-11-16 11:28:18 +0000 | [diff] [blame] | 54 | static u32 kvm_next_vmid; |
Christoffer Dall | fb544d1 | 2018-12-11 13:23:57 +0100 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(kvm_vmid_lock); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 56 | |
Pavel Fedin | c7da6fa | 2015-12-18 14:38:43 +0300 | [diff] [blame] | 57 | static bool vgic_present; |
| 58 | |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 59 | static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled); |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 60 | DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use); |
| 61 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 62 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 63 | { |
| 64 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
| 65 | } |
| 66 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 67 | int kvm_arch_hardware_setup(void *opaque) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 72 | int kvm_arch_check_processor_compat(void *opaque) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 73 | { |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 74 | return 0; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Christoffer Dall | c726200 | 2019-10-11 13:07:05 +0200 | [diff] [blame] | 77 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 78 | struct kvm_enable_cap *cap) |
| 79 | { |
| 80 | int r; |
| 81 | |
| 82 | if (cap->flags) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | switch (cap->cap) { |
| 86 | case KVM_CAP_ARM_NISV_TO_USER: |
| 87 | r = 0; |
| 88 | kvm->arch.return_nisv_io_abort_to_user = true; |
| 89 | break; |
| 90 | default: |
| 91 | r = -EINVAL; |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | return r; |
| 96 | } |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 97 | |
Marc Zyngier | 5107000 | 2020-04-27 15:15:07 +0100 | [diff] [blame] | 98 | static int kvm_arm_default_max_vcpus(void) |
| 99 | { |
| 100 | return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; |
| 101 | } |
| 102 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 103 | /** |
| 104 | * kvm_arch_init_vm - initializes a VM data structure |
| 105 | * @kvm: pointer to the KVM struct |
| 106 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 107 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 108 | { |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 109 | int ret; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 110 | |
Marc Zyngier | bca607e | 2018-10-01 13:40:36 +0100 | [diff] [blame] | 111 | ret = kvm_arm_setup_stage2(kvm, type); |
Suzuki K Poulose | 5b6c674 | 2018-09-26 17:32:42 +0100 | [diff] [blame] | 112 | if (ret) |
| 113 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 114 | |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 115 | ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 116 | if (ret) |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 117 | return ret; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 118 | |
Marc Zyngier | c8dddec | 2016-06-13 15:00:45 +0100 | [diff] [blame] | 119 | ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 120 | if (ret) |
| 121 | goto out_free_stage2_pgd; |
| 122 | |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 123 | kvm_vgic_early_init(kvm); |
Christoffer Dall | a1a6438 | 2013-11-16 10:51:25 -0800 | [diff] [blame] | 124 | |
Andre Przywara | 3caa2d8 | 2014-06-02 16:26:01 +0200 | [diff] [blame] | 125 | /* The maximum number of VCPUs is limited by the host's GIC model */ |
Marc Zyngier | 5107000 | 2020-04-27 15:15:07 +0100 | [diff] [blame] | 126 | kvm->arch.max_vcpus = kvm_arm_default_max_vcpus(); |
Andre Przywara | 3caa2d8 | 2014-06-02 16:26:01 +0200 | [diff] [blame] | 127 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 128 | return ret; |
| 129 | out_free_stage2_pgd: |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 130 | kvm_free_stage2_pgd(&kvm->arch.mmu); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 131 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Souptick Joarder | 1499fa8 | 2018-04-19 00:49:58 +0530 | [diff] [blame] | 134 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 135 | { |
| 136 | return VM_FAULT_SIGBUS; |
| 137 | } |
| 138 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 139 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 140 | /** |
| 141 | * kvm_arch_destroy_vm - destroy the VM data structure |
| 142 | * @kvm: pointer to the KVM struct |
| 143 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 144 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 145 | { |
| 146 | int i; |
| 147 | |
Marc Zyngier | b2c9a85 | 2017-10-27 15:28:34 +0100 | [diff] [blame] | 148 | kvm_vgic_destroy(kvm); |
| 149 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 150 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 151 | if (kvm->vcpus[i]) { |
Sean Christopherson | 4543bdc | 2019-12-18 13:55:14 -0800 | [diff] [blame] | 152 | kvm_vcpu_destroy(kvm->vcpus[i]); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 153 | kvm->vcpus[i] = NULL; |
| 154 | } |
| 155 | } |
Andrew Jones | 6b2ad81 | 2017-11-27 19:17:18 +0100 | [diff] [blame] | 156 | atomic_set(&kvm->online_vcpus, 0); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 159 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 160 | { |
| 161 | int r; |
| 162 | switch (ext) { |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 163 | case KVM_CAP_IRQCHIP: |
Pavel Fedin | c7da6fa | 2015-12-18 14:38:43 +0300 | [diff] [blame] | 164 | r = vgic_present; |
| 165 | break; |
Nikolay Nikolaev | d44758c | 2015-01-24 12:00:02 +0000 | [diff] [blame] | 166 | case KVM_CAP_IOEVENTFD: |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 167 | case KVM_CAP_DEVICE_CTRL: |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 168 | case KVM_CAP_USER_MEMORY: |
| 169 | case KVM_CAP_SYNC_MMU: |
| 170 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
| 171 | case KVM_CAP_ONE_REG: |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 172 | case KVM_CAP_ARM_PSCI: |
Anup Patel | 4447a20 | 2014-04-29 11:24:25 +0530 | [diff] [blame] | 173 | case KVM_CAP_ARM_PSCI_0_2: |
Christoffer Dall | 9804788 | 2014-08-19 12:18:04 +0200 | [diff] [blame] | 174 | case KVM_CAP_READONLY_MEM: |
Alex Bennée | ecccf0c | 2015-03-13 17:02:52 +0000 | [diff] [blame] | 175 | case KVM_CAP_MP_STATE: |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 176 | case KVM_CAP_IMMEDIATE_EXIT: |
Dongjiu Geng | 58bf437 | 2018-10-13 00:12:49 +0800 | [diff] [blame] | 177 | case KVM_CAP_VCPU_EVENTS: |
Marc Zyngier | 92f35b7 | 2019-08-18 14:09:47 +0100 | [diff] [blame] | 178 | case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: |
Christoffer Dall | c726200 | 2019-10-11 13:07:05 +0200 | [diff] [blame] | 179 | case KVM_CAP_ARM_NISV_TO_USER: |
Christoffer Dall | da34517 | 2019-10-11 13:07:06 +0200 | [diff] [blame] | 180 | case KVM_CAP_ARM_INJECT_EXT_DABT: |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 181 | r = 1; |
| 182 | break; |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 183 | case KVM_CAP_ARM_SET_DEVICE_ADDR: |
| 184 | r = 1; |
Marc Zyngier | ca46e10 | 2013-04-03 10:43:13 +0100 | [diff] [blame] | 185 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 186 | case KVM_CAP_NR_VCPUS: |
| 187 | r = num_online_cpus(); |
| 188 | break; |
| 189 | case KVM_CAP_MAX_VCPUS: |
Thomas Huth | a86cb41 | 2019-05-23 18:43:08 +0200 | [diff] [blame] | 190 | case KVM_CAP_MAX_VCPU_ID: |
Marc Zyngier | 5107000 | 2020-04-27 15:15:07 +0100 | [diff] [blame] | 191 | if (kvm) |
| 192 | r = kvm->arch.max_vcpus; |
| 193 | else |
| 194 | r = kvm_arm_default_max_vcpus(); |
Thomas Huth | a86cb41 | 2019-05-23 18:43:08 +0200 | [diff] [blame] | 195 | break; |
Vladimir Murzin | 2988509 | 2016-11-02 11:55:34 +0000 | [diff] [blame] | 196 | case KVM_CAP_MSI_DEVID: |
| 197 | if (!kvm) |
| 198 | r = -EINVAL; |
| 199 | else |
| 200 | r = kvm->arch.vgic.msis_require_devid; |
| 201 | break; |
Christoffer Dall | f7214e6 | 2017-02-01 12:54:11 +0100 | [diff] [blame] | 202 | case KVM_CAP_ARM_USER_IRQ: |
| 203 | /* |
| 204 | * 1: EL1_VTIMER, EL1_PTIMER, and PMU. |
| 205 | * (bump this number if adding more devices) |
| 206 | */ |
| 207 | r = 1; |
| 208 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 209 | default: |
Dongjiu Geng | 375bdd3 | 2018-10-13 00:12:48 +0800 | [diff] [blame] | 210 | r = kvm_arch_vm_ioctl_check_extension(kvm, ext); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | return r; |
| 214 | } |
| 215 | |
| 216 | long kvm_arch_dev_ioctl(struct file *filp, |
| 217 | unsigned int ioctl, unsigned long arg) |
| 218 | { |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 222 | struct kvm *kvm_arch_alloc_vm(void) |
| 223 | { |
| 224 | if (!has_vhe()) |
| 225 | return kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 226 | |
| 227 | return vzalloc(sizeof(struct kvm)); |
| 228 | } |
| 229 | |
| 230 | void kvm_arch_free_vm(struct kvm *kvm) |
| 231 | { |
| 232 | if (!has_vhe()) |
| 233 | kfree(kvm); |
| 234 | else |
| 235 | vfree(kvm); |
| 236 | } |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 237 | |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 238 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
| 239 | { |
| 240 | if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) |
| 241 | return -EBUSY; |
| 242 | |
| 243 | if (id >= kvm->arch.max_vcpus) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 249 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 250 | { |
Sean Christopherson | 39a93a8 | 2019-12-18 13:55:25 -0800 | [diff] [blame] | 251 | int err; |
| 252 | |
| 253 | /* Force users to call KVM_ARM_VCPU_INIT */ |
| 254 | vcpu->arch.target = -1; |
| 255 | bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); |
| 256 | |
| 257 | /* Set up the timer */ |
| 258 | kvm_timer_vcpu_init(vcpu); |
| 259 | |
| 260 | kvm_pmu_vcpu_init(vcpu); |
| 261 | |
| 262 | kvm_arm_reset_debug_ptr(vcpu); |
| 263 | |
| 264 | kvm_arm_pvtime_vcpu_init(&vcpu->arch); |
| 265 | |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 266 | vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; |
| 267 | |
Sean Christopherson | 39a93a8 | 2019-12-18 13:55:25 -0800 | [diff] [blame] | 268 | err = kvm_vgic_vcpu_init(vcpu); |
| 269 | if (err) |
| 270 | return err; |
| 271 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 272 | return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 275 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 276 | { |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Sean Christopherson | 4b8fff7 | 2019-12-18 13:55:04 -0800 | [diff] [blame] | 279 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 280 | { |
Christoffer Dall | f1d7231 | 2018-01-25 18:32:29 +0100 | [diff] [blame] | 281 | if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm))) |
| 282 | static_branch_dec(&userspace_irqchip_in_use); |
| 283 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 284 | kvm_mmu_free_memory_caches(vcpu); |
Marc Zyngier | 967f842 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 285 | kvm_timer_vcpu_terminate(vcpu); |
Shannon Zhao | 5f0a714 | 2015-09-11 15:18:05 +0800 | [diff] [blame] | 286 | kvm_pmu_vcpu_destroy(vcpu); |
Sean Christopherson | 19bcc89 | 2019-12-18 13:55:27 -0800 | [diff] [blame] | 287 | |
| 288 | kvm_arm_vcpu_destroy(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 291 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 292 | { |
Christoffer Dall | 1c88ab7 | 2017-01-06 16:07:48 +0100 | [diff] [blame] | 293 | return kvm_timer_is_pending(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 296 | void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) |
| 297 | { |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 298 | /* |
| 299 | * If we're about to block (most likely because we've just hit a |
| 300 | * WFI), we need to sync back the state of the GIC CPU interface |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 301 | * so that we have the latest PMR and group enables. This ensures |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 302 | * that kvm_arch_vcpu_runnable has up-to-date data to decide |
| 303 | * whether we have pending interrupts. |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 304 | * |
| 305 | * For the same reason, we want to tell GICv4 that we need |
| 306 | * doorbells to be signalled, should an interrupt become pending. |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 307 | */ |
| 308 | preempt_disable(); |
| 309 | kvm_vgic_vmcr_sync(vcpu); |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 310 | vgic_v4_put(vcpu, true); |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 311 | preempt_enable(); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) |
| 315 | { |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 316 | preempt_disable(); |
| 317 | vgic_v4_load(vcpu); |
| 318 | preempt_enable(); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 321 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 322 | { |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 323 | struct kvm_s2_mmu *mmu; |
Marc Zyngier | 94d0e59 | 2016-10-18 18:37:49 +0100 | [diff] [blame] | 324 | int *last_ran; |
| 325 | |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 326 | mmu = vcpu->arch.hw_mmu; |
| 327 | last_ran = this_cpu_ptr(mmu->last_vcpu_ran); |
Marc Zyngier | 94d0e59 | 2016-10-18 18:37:49 +0100 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * We might get preempted before the vCPU actually runs, but |
| 331 | * over-invalidation doesn't affect correctness. |
| 332 | */ |
| 333 | if (*last_ran != vcpu->vcpu_id) { |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 334 | kvm_call_hyp(__kvm_tlb_flush_local_vmid, mmu); |
Marc Zyngier | 94d0e59 | 2016-10-18 18:37:49 +0100 | [diff] [blame] | 335 | *last_ran = vcpu->vcpu_id; |
| 336 | } |
| 337 | |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 338 | vcpu->cpu = cpu; |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 339 | |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 340 | kvm_vgic_load(vcpu); |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 341 | kvm_timer_vcpu_load(vcpu); |
David Brazdil | 13aeb9b | 2020-06-25 14:14:16 +0100 | [diff] [blame] | 342 | if (has_vhe()) |
| 343 | kvm_vcpu_load_sysregs_vhe(vcpu); |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 344 | kvm_arch_vcpu_load_fp(vcpu); |
Andrew Murray | 435e53f | 2019-04-09 20:22:15 +0100 | [diff] [blame] | 345 | kvm_vcpu_pmu_restore_guest(vcpu); |
Steven Price | 8564d63 | 2019-10-21 16:28:18 +0100 | [diff] [blame] | 346 | if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) |
| 347 | kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); |
Marc Zyngier | de73708 | 2018-06-21 10:43:59 +0100 | [diff] [blame] | 348 | |
| 349 | if (single_task_running()) |
Marc Zyngier | ef2e78d | 2019-11-07 16:04:12 +0000 | [diff] [blame] | 350 | vcpu_clear_wfx_traps(vcpu); |
Marc Zyngier | de73708 | 2018-06-21 10:43:59 +0100 | [diff] [blame] | 351 | else |
Marc Zyngier | ef2e78d | 2019-11-07 16:04:12 +0000 | [diff] [blame] | 352 | vcpu_set_wfx_traps(vcpu); |
Mark Rutland | 384b40c | 2019-04-23 10:12:35 +0530 | [diff] [blame] | 353 | |
Marc Zyngier | 29eb5a3 | 2020-06-04 11:14:00 +0100 | [diff] [blame] | 354 | if (vcpu_has_ptrauth(vcpu)) |
Marc Zyngier | ef3e40a | 2020-06-03 18:24:01 +0100 | [diff] [blame] | 355 | vcpu_ptrauth_disable(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 359 | { |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 360 | kvm_arch_vcpu_put_fp(vcpu); |
David Brazdil | 13aeb9b | 2020-06-25 14:14:16 +0100 | [diff] [blame] | 361 | if (has_vhe()) |
| 362 | kvm_vcpu_put_sysregs_vhe(vcpu); |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 363 | kvm_timer_vcpu_put(vcpu); |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 364 | kvm_vgic_put(vcpu); |
Andrew Murray | 435e53f | 2019-04-09 20:22:15 +0100 | [diff] [blame] | 365 | kvm_vcpu_pmu_restore_host(vcpu); |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 366 | |
Christoffer Dall | e9b152c | 2013-12-11 20:29:11 -0800 | [diff] [blame] | 367 | vcpu->cpu = -1; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 370 | static void vcpu_power_off(struct kvm_vcpu *vcpu) |
| 371 | { |
| 372 | vcpu->arch.power_off = true; |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 373 | kvm_make_request(KVM_REQ_SLEEP, vcpu); |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 374 | kvm_vcpu_kick(vcpu); |
| 375 | } |
| 376 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 377 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 378 | struct kvm_mp_state *mp_state) |
| 379 | { |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 380 | if (vcpu->arch.power_off) |
Alex Bennée | ecccf0c | 2015-03-13 17:02:52 +0000 | [diff] [blame] | 381 | mp_state->mp_state = KVM_MP_STATE_STOPPED; |
| 382 | else |
| 383 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; |
| 384 | |
| 385 | return 0; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 389 | struct kvm_mp_state *mp_state) |
| 390 | { |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 391 | int ret = 0; |
| 392 | |
Alex Bennée | ecccf0c | 2015-03-13 17:02:52 +0000 | [diff] [blame] | 393 | switch (mp_state->mp_state) { |
| 394 | case KVM_MP_STATE_RUNNABLE: |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 395 | vcpu->arch.power_off = false; |
Alex Bennée | ecccf0c | 2015-03-13 17:02:52 +0000 | [diff] [blame] | 396 | break; |
| 397 | case KVM_MP_STATE_STOPPED: |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 398 | vcpu_power_off(vcpu); |
Alex Bennée | ecccf0c | 2015-03-13 17:02:52 +0000 | [diff] [blame] | 399 | break; |
| 400 | default: |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 401 | ret = -EINVAL; |
Alex Bennée | ecccf0c | 2015-03-13 17:02:52 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Christoffer Dall | e83dff5 | 2017-12-04 21:35:31 +0100 | [diff] [blame] | 404 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 405 | } |
| 406 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 407 | /** |
| 408 | * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled |
| 409 | * @v: The VCPU pointer |
| 410 | * |
| 411 | * If the guest CPU is not waiting for interrupts or an interrupt line is |
| 412 | * asserted, the CPU is by definition runnable. |
| 413 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 414 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 415 | { |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 416 | bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF); |
| 417 | return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) |
Eric Auger | 3b92830 | 2015-09-25 23:41:17 +0200 | [diff] [blame] | 418 | && !v->arch.power_off && !v->arch.pause); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 421 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 422 | { |
Longpeng(Mike) | f01fbd2 | 2017-08-08 12:05:35 +0800 | [diff] [blame] | 423 | return vcpu_mode_priv(vcpu); |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 424 | } |
| 425 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 426 | /* Just ensure a guest exit from a particular CPU */ |
| 427 | static void exit_vm_noop(void *info) |
| 428 | { |
| 429 | } |
| 430 | |
| 431 | void force_vm_exit(const cpumask_t *mask) |
| 432 | { |
Eric Auger | 898f949 | 2016-03-07 23:50:36 +0700 | [diff] [blame] | 433 | preempt_disable(); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 434 | smp_call_function_many(mask, exit_vm_noop, NULL, true); |
Eric Auger | 898f949 | 2016-03-07 23:50:36 +0700 | [diff] [blame] | 435 | preempt_enable(); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * need_new_vmid_gen - check that the VMID is still valid |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 440 | * @vmid: The VMID to check |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 441 | * |
| 442 | * return true if there is a new generation of VMIDs being used |
| 443 | * |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 444 | * The hardware supports a limited set of values with the value zero reserved |
| 445 | * for the host, so we check if an assigned value belongs to a previous |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 446 | * generation, which requires us to assign a new value. If we're the first to |
| 447 | * use a VMID for the new generation, we must flush necessary caches and TLBs |
| 448 | * on all CPUs. |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 449 | */ |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 450 | static bool need_new_vmid_gen(struct kvm_vmid *vmid) |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 451 | { |
Christoffer Dall | fb544d1 | 2018-12-11 13:23:57 +0100 | [diff] [blame] | 452 | u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen); |
| 453 | smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */ |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 454 | return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 458 | * update_vmid - Update the vmid with a valid VMID for the current generation |
| 459 | * @kvm: The guest that struct vmid belongs to |
| 460 | * @vmid: The stage-2 VMID information struct |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 461 | */ |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 462 | static void update_vmid(struct kvm_vmid *vmid) |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 463 | { |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 464 | if (!need_new_vmid_gen(vmid)) |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 465 | return; |
| 466 | |
Christoffer Dall | fb544d1 | 2018-12-11 13:23:57 +0100 | [diff] [blame] | 467 | spin_lock(&kvm_vmid_lock); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * We need to re-check the vmid_gen here to ensure that if another vcpu |
| 471 | * already allocated a valid vmid for this vm, then this vcpu should |
| 472 | * use the same vmid. |
| 473 | */ |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 474 | if (!need_new_vmid_gen(vmid)) { |
Christoffer Dall | fb544d1 | 2018-12-11 13:23:57 +0100 | [diff] [blame] | 475 | spin_unlock(&kvm_vmid_lock); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | /* First user of a new VMID generation? */ |
| 480 | if (unlikely(kvm_next_vmid == 0)) { |
| 481 | atomic64_inc(&kvm_vmid_gen); |
| 482 | kvm_next_vmid = 1; |
| 483 | |
| 484 | /* |
| 485 | * On SMP we know no other CPUs can use this CPU's or each |
| 486 | * other's VMID after force_vm_exit returns since the |
| 487 | * kvm_vmid_lock blocks them from reentry to the guest. |
| 488 | */ |
| 489 | force_vm_exit(cpu_all_mask); |
| 490 | /* |
| 491 | * Now broadcast TLB + ICACHE invalidation over the inner |
| 492 | * shareable domain to make sure all data structures are |
| 493 | * clean. |
| 494 | */ |
| 495 | kvm_call_hyp(__kvm_flush_vm_context); |
| 496 | } |
| 497 | |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 498 | vmid->vmid = kvm_next_vmid; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 499 | kvm_next_vmid++; |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 500 | kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 501 | |
Christoffer Dall | fb544d1 | 2018-12-11 13:23:57 +0100 | [diff] [blame] | 502 | smp_wmb(); |
Christoffer Dall | e329fb7 | 2018-12-11 15:26:31 +0100 | [diff] [blame] | 503 | WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen)); |
Christoffer Dall | fb544d1 | 2018-12-11 13:23:57 +0100 | [diff] [blame] | 504 | |
| 505 | spin_unlock(&kvm_vmid_lock); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 508 | static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) |
| 509 | { |
Christoffer Dall | 0597112 | 2014-12-12 21:19:23 +0100 | [diff] [blame] | 510 | struct kvm *kvm = vcpu->kvm; |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 511 | int ret = 0; |
Christoffer Dall | e1ba020 | 2013-09-23 14:55:55 -0700 | [diff] [blame] | 512 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 513 | if (likely(vcpu->arch.has_run_once)) |
| 514 | return 0; |
| 515 | |
Dave Martin | 7dd32a0 | 2018-12-19 14:27:01 +0000 | [diff] [blame] | 516 | if (!kvm_arm_vcpu_is_finalized(vcpu)) |
| 517 | return -EPERM; |
| 518 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 519 | vcpu->arch.has_run_once = true; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 520 | |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 521 | if (likely(irqchip_in_kernel(kvm))) { |
| 522 | /* |
| 523 | * Map the VGIC hardware resources before running a vcpu the |
| 524 | * first time on this VM. |
| 525 | */ |
| 526 | if (unlikely(!vgic_ready(kvm))) { |
| 527 | ret = kvm_vgic_map_resources(kvm); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | } |
| 531 | } else { |
| 532 | /* |
| 533 | * Tell the rest of the code that there are userspace irqchip |
| 534 | * VMs in the wild. |
| 535 | */ |
| 536 | static_branch_inc(&userspace_irqchip_in_use); |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame] | 539 | ret = kvm_timer_enable(vcpu); |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 540 | if (ret) |
| 541 | return ret; |
| 542 | |
| 543 | ret = kvm_arm_pmu_v3_enable(vcpu); |
Christoffer Dall | 0597112 | 2014-12-12 21:19:23 +0100 | [diff] [blame] | 544 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 545 | return ret; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Eric Auger | c1426e4 | 2015-03-04 11:14:34 +0100 | [diff] [blame] | 548 | bool kvm_arch_intc_initialized(struct kvm *kvm) |
| 549 | { |
| 550 | return vgic_initialized(kvm); |
| 551 | } |
| 552 | |
Christoffer Dall | b13216c | 2016-04-27 10:28:00 +0100 | [diff] [blame] | 553 | void kvm_arm_halt_guest(struct kvm *kvm) |
Eric Auger | 3b92830 | 2015-09-25 23:41:17 +0200 | [diff] [blame] | 554 | { |
| 555 | int i; |
| 556 | struct kvm_vcpu *vcpu; |
| 557 | |
| 558 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 559 | vcpu->arch.pause = true; |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 560 | kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); |
Eric Auger | 3b92830 | 2015-09-25 23:41:17 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Christoffer Dall | b13216c | 2016-04-27 10:28:00 +0100 | [diff] [blame] | 563 | void kvm_arm_resume_guest(struct kvm *kvm) |
Eric Auger | 3b92830 | 2015-09-25 23:41:17 +0200 | [diff] [blame] | 564 | { |
| 565 | int i; |
| 566 | struct kvm_vcpu *vcpu; |
| 567 | |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 568 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 569 | vcpu->arch.pause = false; |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 570 | rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 571 | } |
Eric Auger | 3b92830 | 2015-09-25 23:41:17 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 574 | static void vcpu_req_sleep(struct kvm_vcpu *vcpu) |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 575 | { |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 576 | struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 577 | |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 578 | rcuwait_wait_event(wait, |
| 579 | (!vcpu->arch.power_off) &&(!vcpu->arch.pause), |
| 580 | TASK_INTERRUPTIBLE); |
Andrew Jones | 0592c00 | 2017-06-04 14:43:55 +0200 | [diff] [blame] | 581 | |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 582 | if (vcpu->arch.power_off || vcpu->arch.pause) { |
Andrew Jones | 0592c00 | 2017-06-04 14:43:55 +0200 | [diff] [blame] | 583 | /* Awaken to handle a signal, request we sleep again later. */ |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 584 | kvm_make_request(KVM_REQ_SLEEP, vcpu); |
Andrew Jones | 0592c00 | 2017-06-04 14:43:55 +0200 | [diff] [blame] | 585 | } |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Make sure we will observe a potential reset request if we've |
| 589 | * observed a change to the power state. Pairs with the smp_wmb() in |
| 590 | * kvm_psci_vcpu_on(). |
| 591 | */ |
| 592 | smp_rmb(); |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 593 | } |
| 594 | |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 595 | static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) |
| 596 | { |
| 597 | return vcpu->arch.target >= 0; |
| 598 | } |
| 599 | |
Andrew Jones | 0592c00 | 2017-06-04 14:43:55 +0200 | [diff] [blame] | 600 | static void check_vcpu_requests(struct kvm_vcpu *vcpu) |
| 601 | { |
| 602 | if (kvm_request_pending(vcpu)) { |
Andrew Jones | 7b244e2 | 2017-06-04 14:43:58 +0200 | [diff] [blame] | 603 | if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) |
| 604 | vcpu_req_sleep(vcpu); |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 605 | |
Marc Zyngier | 358b28f | 2018-12-20 11:36:07 +0000 | [diff] [blame] | 606 | if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) |
| 607 | kvm_reset_vcpu(vcpu); |
| 608 | |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 609 | /* |
| 610 | * Clear IRQ_PENDING requests that were made to guarantee |
| 611 | * that a VCPU sees new virtual interrupts. |
| 612 | */ |
| 613 | kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); |
Steven Price | 8564d63 | 2019-10-21 16:28:18 +0100 | [diff] [blame] | 614 | |
| 615 | if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) |
| 616 | kvm_update_stolen_time(vcpu); |
Marc Zyngier | d9c3872 | 2020-03-04 20:33:28 +0000 | [diff] [blame] | 617 | |
| 618 | if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { |
| 619 | /* The distributor enable bits were changed */ |
| 620 | preempt_disable(); |
| 621 | vgic_v4_put(vcpu, false); |
| 622 | vgic_v4_load(vcpu); |
| 623 | preempt_enable(); |
| 624 | } |
Andrew Jones | 0592c00 | 2017-06-04 14:43:55 +0200 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 628 | /** |
| 629 | * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code |
| 630 | * @vcpu: The VCPU pointer |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 631 | * |
| 632 | * This function is called through the VCPU_RUN ioctl called from user space. It |
| 633 | * will execute VM code in a loop until the time slice for the process is used |
| 634 | * or some emulation is needed from user space in which case the function will |
| 635 | * return with return value 0 and with the kvm_run structure filled in with the |
| 636 | * required data for the requested emulation. |
| 637 | */ |
Tianjia Zhang | 1b94f6f | 2020-04-16 13:10:57 +0800 | [diff] [blame] | 638 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 639 | { |
Tianjia Zhang | 1b94f6f | 2020-04-16 13:10:57 +0800 | [diff] [blame] | 640 | struct kvm_run *run = vcpu->run; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 641 | int ret; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 642 | |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 643 | if (unlikely(!kvm_vcpu_initialized(vcpu))) |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 644 | return -ENOEXEC; |
| 645 | |
| 646 | ret = kvm_vcpu_first_run_init(vcpu); |
| 647 | if (ret) |
Christoffer Dall | 829a586 | 2017-11-29 16:37:53 +0100 | [diff] [blame] | 648 | return ret; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 649 | |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 650 | if (run->exit_reason == KVM_EXIT_MMIO) { |
Tianjia Zhang | 1b94f6f | 2020-04-16 13:10:57 +0800 | [diff] [blame] | 651 | ret = kvm_handle_mmio_return(vcpu, run); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 652 | if (ret) |
Christoffer Dall | 829a586 | 2017-11-29 16:37:53 +0100 | [diff] [blame] | 653 | return ret; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 654 | } |
| 655 | |
Christoffer Dall | 829a586 | 2017-11-29 16:37:53 +0100 | [diff] [blame] | 656 | if (run->immediate_exit) |
| 657 | return -EINTR; |
| 658 | |
| 659 | vcpu_load(vcpu); |
Paolo Bonzini | 460df4c | 2017-02-08 11:50:15 +0100 | [diff] [blame] | 660 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 661 | kvm_sigset_activate(vcpu); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 662 | |
| 663 | ret = 1; |
| 664 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 665 | while (ret > 0) { |
| 666 | /* |
| 667 | * Check conditions before entering the guest |
| 668 | */ |
| 669 | cond_resched(); |
| 670 | |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 671 | update_vmid(&vcpu->arch.hw_mmu->vmid); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 672 | |
Andrew Jones | 0592c00 | 2017-06-04 14:43:55 +0200 | [diff] [blame] | 673 | check_vcpu_requests(vcpu); |
| 674 | |
Marc Zyngier | abdf584 | 2015-06-08 15:00:28 +0100 | [diff] [blame] | 675 | /* |
Marc Zyngier | abdf584 | 2015-06-08 15:00:28 +0100 | [diff] [blame] | 676 | * Preparing the interrupts to be injected also |
| 677 | * involves poking the GIC, which must be done in a |
| 678 | * non-preemptible context. |
| 679 | */ |
| 680 | preempt_disable(); |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 681 | |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 682 | kvm_pmu_flush_hwstate(vcpu); |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 683 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 684 | local_irq_disable(); |
| 685 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 686 | kvm_vgic_flush_hwstate(vcpu); |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 687 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 688 | /* |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 689 | * Exit if we have a signal pending so that we can deliver the |
| 690 | * signal to user space. |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 691 | */ |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 692 | if (signal_pending(current)) { |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 693 | ret = -EINTR; |
| 694 | run->exit_reason = KVM_EXIT_INTR; |
| 695 | } |
| 696 | |
Andrew Jones | 6a6d73b | 2017-06-04 14:43:54 +0200 | [diff] [blame] | 697 | /* |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 698 | * If we're using a userspace irqchip, then check if we need |
| 699 | * to tell a userspace irqchip about timer or PMU level |
| 700 | * changes and if so, exit to userspace (the actual level |
| 701 | * state gets updated in kvm_timer_update_run and |
| 702 | * kvm_pmu_update_run below). |
| 703 | */ |
| 704 | if (static_branch_unlikely(&userspace_irqchip_in_use)) { |
| 705 | if (kvm_timer_should_notify_user(vcpu) || |
| 706 | kvm_pmu_should_notify_user(vcpu)) { |
| 707 | ret = -EINTR; |
| 708 | run->exit_reason = KVM_EXIT_INTR; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* |
Andrew Jones | 6a6d73b | 2017-06-04 14:43:54 +0200 | [diff] [blame] | 713 | * Ensure we set mode to IN_GUEST_MODE after we disable |
| 714 | * interrupts and before the final VCPU requests check. |
| 715 | * See the comment in kvm_vcpu_exiting_guest_mode() and |
Christoph Hellwig | 2f5947d | 2019-07-24 09:24:49 +0200 | [diff] [blame] | 716 | * Documentation/virt/kvm/vcpu-requests.rst |
Andrew Jones | 6a6d73b | 2017-06-04 14:43:54 +0200 | [diff] [blame] | 717 | */ |
| 718 | smp_store_mb(vcpu->mode, IN_GUEST_MODE); |
| 719 | |
Christoffer Dall | a0e50aa | 2019-01-04 21:09:05 +0100 | [diff] [blame] | 720 | if (ret <= 0 || need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) || |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 721 | kvm_request_pending(vcpu)) { |
Andrew Jones | 6a6d73b | 2017-06-04 14:43:54 +0200 | [diff] [blame] | 722 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Christoffer Dall | 771621b | 2017-10-04 23:42:32 +0200 | [diff] [blame] | 723 | isb(); /* Ensure work in x_flush_hwstate is committed */ |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 724 | kvm_pmu_sync_hwstate(vcpu); |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 725 | if (static_branch_unlikely(&userspace_irqchip_in_use)) |
Marc Zyngier | 3c5ff0c | 2020-04-22 08:58:22 +0100 | [diff] [blame^] | 726 | kvm_timer_sync_user(vcpu); |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 727 | kvm_vgic_sync_hwstate(vcpu); |
Christoffer Dall | ee9bb9a | 2016-10-16 20:24:30 +0200 | [diff] [blame] | 728 | local_irq_enable(); |
Marc Zyngier | abdf584 | 2015-06-08 15:00:28 +0100 | [diff] [blame] | 729 | preempt_enable(); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 730 | continue; |
| 731 | } |
| 732 | |
Alex Bennée | 56c7f5e | 2015-07-07 17:29:56 +0100 | [diff] [blame] | 733 | kvm_arm_setup_debug(vcpu); |
| 734 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 735 | /************************************************************** |
| 736 | * Enter the guest |
| 737 | */ |
| 738 | trace_kvm_entry(*vcpu_pc(vcpu)); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 739 | guest_enter_irqoff(); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 740 | |
David Brazdil | 09cf57e | 2020-06-25 14:14:14 +0100 | [diff] [blame] | 741 | ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu); |
Christoffer Dall | 3f5c90b | 2017-10-03 14:02:12 +0200 | [diff] [blame] | 742 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 743 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Amit Tomar | b19e689 | 2015-11-26 10:09:43 +0000 | [diff] [blame] | 744 | vcpu->stat.exits++; |
Christoffer Dall | 1b3d546 | 2015-05-28 19:49:10 +0100 | [diff] [blame] | 745 | /* |
| 746 | * Back from guest |
| 747 | *************************************************************/ |
| 748 | |
Alex Bennée | 56c7f5e | 2015-07-07 17:29:56 +0100 | [diff] [blame] | 749 | kvm_arm_clear_debug(vcpu); |
| 750 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 751 | /* |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 752 | * We must sync the PMU state before the vgic state so |
Christoffer Dall | ee9bb9a | 2016-10-16 20:24:30 +0200 | [diff] [blame] | 753 | * that the vgic can properly sample the updated state of the |
| 754 | * interrupt line. |
| 755 | */ |
| 756 | kvm_pmu_sync_hwstate(vcpu); |
Christoffer Dall | ee9bb9a | 2016-10-16 20:24:30 +0200 | [diff] [blame] | 757 | |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 758 | /* |
| 759 | * Sync the vgic state before syncing the timer state because |
| 760 | * the timer code needs to know if the virtual timer |
| 761 | * interrupts are active. |
| 762 | */ |
Christoffer Dall | ee9bb9a | 2016-10-16 20:24:30 +0200 | [diff] [blame] | 763 | kvm_vgic_sync_hwstate(vcpu); |
| 764 | |
| 765 | /* |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 766 | * Sync the timer hardware state before enabling interrupts as |
| 767 | * we don't want vtimer interrupts to race with syncing the |
| 768 | * timer virtual interrupt state. |
| 769 | */ |
Christoffer Dall | 61bbe38 | 2017-10-27 19:57:51 +0200 | [diff] [blame] | 770 | if (static_branch_unlikely(&userspace_irqchip_in_use)) |
Marc Zyngier | 3c5ff0c | 2020-04-22 08:58:22 +0100 | [diff] [blame^] | 771 | kvm_timer_sync_user(vcpu); |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 772 | |
Dave Martin | e6b673b | 2018-04-06 14:55:59 +0100 | [diff] [blame] | 773 | kvm_arch_vcpu_ctxsync_fp(vcpu); |
| 774 | |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 775 | /* |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 776 | * We may have taken a host interrupt in HYP mode (ie |
| 777 | * while executing the guest). This interrupt is still |
| 778 | * pending, as we haven't serviced it yet! |
| 779 | * |
| 780 | * We're now back in SVC mode, with interrupts |
| 781 | * disabled. Enabling the interrupts now will have |
| 782 | * the effect of taking the interrupt again, in SVC |
| 783 | * mode this time. |
| 784 | */ |
| 785 | local_irq_enable(); |
| 786 | |
| 787 | /* |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 788 | * We do local_irq_enable() before calling guest_exit() so |
Christoffer Dall | 1b3d546 | 2015-05-28 19:49:10 +0100 | [diff] [blame] | 789 | * that if a timer interrupt hits while running the guest we |
| 790 | * account that tick as being spent in the guest. We enable |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 791 | * preemption after calling guest_exit() so that if we get |
Christoffer Dall | 1b3d546 | 2015-05-28 19:49:10 +0100 | [diff] [blame] | 792 | * preempted we make sure ticks after that is not counted as |
| 793 | * guest time. |
| 794 | */ |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 795 | guest_exit(); |
Christoffer Dall | b5905dc | 2015-08-30 15:55:22 +0200 | [diff] [blame] | 796 | trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); |
Christoffer Dall | 1b3d546 | 2015-05-28 19:49:10 +0100 | [diff] [blame] | 797 | |
James Morse | 3368bd8 | 2018-01-15 19:39:04 +0000 | [diff] [blame] | 798 | /* Exit types that need handling before we can be preempted */ |
| 799 | handle_exit_early(vcpu, run, ret); |
| 800 | |
Marc Zyngier | abdf584 | 2015-06-08 15:00:28 +0100 | [diff] [blame] | 801 | preempt_enable(); |
| 802 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 803 | ret = handle_exit(vcpu, run, ret); |
| 804 | } |
| 805 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame] | 806 | /* Tell userspace about in-kernel device output levels */ |
Christoffer Dall | 3dbbdf7 | 2017-02-01 12:51:52 +0100 | [diff] [blame] | 807 | if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { |
| 808 | kvm_timer_update_run(vcpu); |
| 809 | kvm_pmu_update_run(vcpu); |
| 810 | } |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame] | 811 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 812 | kvm_sigset_deactivate(vcpu); |
| 813 | |
Christoffer Dall | accb757 | 2017-12-04 21:35:25 +0100 | [diff] [blame] | 814 | vcpu_put(vcpu); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 815 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 816 | } |
| 817 | |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 818 | static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) |
| 819 | { |
| 820 | int bit_index; |
| 821 | bool set; |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 822 | unsigned long *hcr; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 823 | |
| 824 | if (number == KVM_ARM_IRQ_CPU_IRQ) |
| 825 | bit_index = __ffs(HCR_VI); |
| 826 | else /* KVM_ARM_IRQ_CPU_FIQ */ |
| 827 | bit_index = __ffs(HCR_VF); |
| 828 | |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 829 | hcr = vcpu_hcr(vcpu); |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 830 | if (level) |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 831 | set = test_and_set_bit(bit_index, hcr); |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 832 | else |
Christoffer Dall | 3df59d8 | 2017-08-03 12:09:05 +0200 | [diff] [blame] | 833 | set = test_and_clear_bit(bit_index, hcr); |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * If we didn't change anything, no need to wake up or kick other CPUs |
| 837 | */ |
| 838 | if (set == level) |
| 839 | return 0; |
| 840 | |
| 841 | /* |
| 842 | * The vcpu irq_lines field was updated, wake up sleeping VCPUs and |
| 843 | * trigger a world-switch round on the running physical CPU to set the |
| 844 | * virtual IRQ/FIQ fields in the HCR appropriately. |
| 845 | */ |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 846 | kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 847 | kvm_vcpu_kick(vcpu); |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
Alexander Graf | 79558f1 | 2013-04-16 19:21:41 +0200 | [diff] [blame] | 852 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, |
| 853 | bool line_status) |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 854 | { |
| 855 | u32 irq = irq_level->irq; |
| 856 | unsigned int irq_type, vcpu_idx, irq_num; |
| 857 | int nrcpus = atomic_read(&kvm->online_vcpus); |
| 858 | struct kvm_vcpu *vcpu = NULL; |
| 859 | bool level = irq_level->level; |
| 860 | |
| 861 | irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; |
| 862 | vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; |
Marc Zyngier | 92f35b7 | 2019-08-18 14:09:47 +0100 | [diff] [blame] | 863 | vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 864 | irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; |
| 865 | |
| 866 | trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); |
| 867 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 868 | switch (irq_type) { |
| 869 | case KVM_ARM_IRQ_TYPE_CPU: |
| 870 | if (irqchip_in_kernel(kvm)) |
| 871 | return -ENXIO; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 872 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 873 | if (vcpu_idx >= nrcpus) |
| 874 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 875 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 876 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); |
| 877 | if (!vcpu) |
| 878 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 879 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 880 | if (irq_num > KVM_ARM_IRQ_CPU_FIQ) |
| 881 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 882 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 883 | return vcpu_interrupt_line(vcpu, irq_num, level); |
| 884 | case KVM_ARM_IRQ_TYPE_PPI: |
| 885 | if (!irqchip_in_kernel(kvm)) |
| 886 | return -ENXIO; |
| 887 | |
| 888 | if (vcpu_idx >= nrcpus) |
| 889 | return -EINVAL; |
| 890 | |
| 891 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); |
| 892 | if (!vcpu) |
| 893 | return -EINVAL; |
| 894 | |
| 895 | if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) |
| 896 | return -EINVAL; |
| 897 | |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 898 | return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 899 | case KVM_ARM_IRQ_TYPE_SPI: |
| 900 | if (!irqchip_in_kernel(kvm)) |
| 901 | return -ENXIO; |
| 902 | |
Andre Przywara | fd1d0dd | 2015-04-10 16:17:59 +0100 | [diff] [blame] | 903 | if (irq_num < VGIC_NR_PRIVATE_IRQS) |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 904 | return -EINVAL; |
| 905 | |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 906 | return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 910 | } |
| 911 | |
Christoffer Dall | f7fa034d | 2014-10-16 16:40:53 +0200 | [diff] [blame] | 912 | static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, |
| 913 | const struct kvm_vcpu_init *init) |
| 914 | { |
Andrew Jones | 811328f | 2019-04-04 19:42:30 +0200 | [diff] [blame] | 915 | unsigned int i, ret; |
Christoffer Dall | f7fa034d | 2014-10-16 16:40:53 +0200 | [diff] [blame] | 916 | int phys_target = kvm_target_cpu(); |
| 917 | |
| 918 | if (init->target != phys_target) |
| 919 | return -EINVAL; |
| 920 | |
| 921 | /* |
| 922 | * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must |
| 923 | * use the same target. |
| 924 | */ |
| 925 | if (vcpu->arch.target != -1 && vcpu->arch.target != init->target) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ |
| 929 | for (i = 0; i < sizeof(init->features) * 8; i++) { |
| 930 | bool set = (init->features[i / 32] & (1 << (i % 32))); |
| 931 | |
| 932 | if (set && i >= KVM_VCPU_MAX_FEATURES) |
| 933 | return -ENOENT; |
| 934 | |
| 935 | /* |
| 936 | * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must |
| 937 | * use the same feature set. |
| 938 | */ |
| 939 | if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES && |
| 940 | test_bit(i, vcpu->arch.features) != set) |
| 941 | return -EINVAL; |
| 942 | |
| 943 | if (set) |
| 944 | set_bit(i, vcpu->arch.features); |
| 945 | } |
| 946 | |
| 947 | vcpu->arch.target = phys_target; |
| 948 | |
| 949 | /* Now we know what it is, we can reset it. */ |
Andrew Jones | 811328f | 2019-04-04 19:42:30 +0200 | [diff] [blame] | 950 | ret = kvm_reset_vcpu(vcpu); |
| 951 | if (ret) { |
| 952 | vcpu->arch.target = -1; |
| 953 | bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); |
| 954 | } |
Christoffer Dall | f7fa034d | 2014-10-16 16:40:53 +0200 | [diff] [blame] | 955 | |
Andrew Jones | 811328f | 2019-04-04 19:42:30 +0200 | [diff] [blame] | 956 | return ret; |
| 957 | } |
Christoffer Dall | f7fa034d | 2014-10-16 16:40:53 +0200 | [diff] [blame] | 958 | |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 959 | static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, |
| 960 | struct kvm_vcpu_init *init) |
| 961 | { |
| 962 | int ret; |
| 963 | |
| 964 | ret = kvm_vcpu_set_target(vcpu, init); |
| 965 | if (ret) |
| 966 | return ret; |
| 967 | |
Christoffer Dall | 957db10 | 2014-11-27 10:35:03 +0100 | [diff] [blame] | 968 | /* |
| 969 | * Ensure a rebooted VM will fault in RAM pages and detect if the |
| 970 | * guest MMU is turned off and flush the caches as needed. |
Zenghui Yu | 892713e | 2020-04-15 15:28:35 +0800 | [diff] [blame] | 971 | * |
Marc Zyngier | 7ae2f3d | 2020-05-30 17:22:19 +0100 | [diff] [blame] | 972 | * S2FWB enforces all memory accesses to RAM being cacheable, |
| 973 | * ensuring that the data side is always coherent. We still |
| 974 | * need to invalidate the I-cache though, as FWB does *not* |
| 975 | * imply CTR_EL0.DIC. |
Christoffer Dall | 957db10 | 2014-11-27 10:35:03 +0100 | [diff] [blame] | 976 | */ |
Marc Zyngier | 7ae2f3d | 2020-05-30 17:22:19 +0100 | [diff] [blame] | 977 | if (vcpu->arch.has_run_once) { |
| 978 | if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) |
| 979 | stage2_unmap_vm(vcpu->kvm); |
| 980 | else |
| 981 | __flush_icache_all(); |
| 982 | } |
Christoffer Dall | 957db10 | 2014-11-27 10:35:03 +0100 | [diff] [blame] | 983 | |
Christoffer Dall | b856a59 | 2014-10-16 17:21:16 +0200 | [diff] [blame] | 984 | vcpu_reset_hcr(vcpu); |
| 985 | |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 986 | /* |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 987 | * Handle the "start in power-off" case. |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 988 | */ |
Christoffer Dall | 03f1d4c | 2014-12-02 15:27:51 +0100 | [diff] [blame] | 989 | if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) |
Andrew Jones | 424c989 | 2017-06-04 14:43:57 +0200 | [diff] [blame] | 990 | vcpu_power_off(vcpu); |
Christoffer Dall | 3ad8b3d | 2014-10-16 16:14:43 +0200 | [diff] [blame] | 991 | else |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 992 | vcpu->arch.power_off = false; |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 997 | static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, |
| 998 | struct kvm_device_attr *attr) |
| 999 | { |
| 1000 | int ret = -ENXIO; |
| 1001 | |
| 1002 | switch (attr->group) { |
| 1003 | default: |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 1004 | ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, |
| 1012 | struct kvm_device_attr *attr) |
| 1013 | { |
| 1014 | int ret = -ENXIO; |
| 1015 | |
| 1016 | switch (attr->group) { |
| 1017 | default: |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 1018 | ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, |
| 1026 | struct kvm_device_attr *attr) |
| 1027 | { |
| 1028 | int ret = -ENXIO; |
| 1029 | |
| 1030 | switch (attr->group) { |
| 1031 | default: |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 1032 | ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1033 | break; |
| 1034 | } |
| 1035 | |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
James Morse | 539aee0 | 2018-07-19 16:24:24 +0100 | [diff] [blame] | 1039 | static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, |
| 1040 | struct kvm_vcpu_events *events) |
| 1041 | { |
| 1042 | memset(events, 0, sizeof(*events)); |
| 1043 | |
| 1044 | return __kvm_arm_vcpu_get_events(vcpu, events); |
| 1045 | } |
| 1046 | |
| 1047 | static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, |
| 1048 | struct kvm_vcpu_events *events) |
| 1049 | { |
| 1050 | int i; |
| 1051 | |
| 1052 | /* check whether the reserved field is zero */ |
| 1053 | for (i = 0; i < ARRAY_SIZE(events->reserved); i++) |
| 1054 | if (events->reserved[i]) |
| 1055 | return -EINVAL; |
| 1056 | |
| 1057 | /* check whether the pad field is zero */ |
| 1058 | for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) |
| 1059 | if (events->exception.pad[i]) |
| 1060 | return -EINVAL; |
| 1061 | |
| 1062 | return __kvm_arm_vcpu_set_events(vcpu, events); |
| 1063 | } |
James Morse | 539aee0 | 2018-07-19 16:24:24 +0100 | [diff] [blame] | 1064 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1065 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 1066 | unsigned int ioctl, unsigned long arg) |
| 1067 | { |
| 1068 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1069 | void __user *argp = (void __user *)arg; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1070 | struct kvm_device_attr attr; |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1071 | long r; |
| 1072 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1073 | switch (ioctl) { |
| 1074 | case KVM_ARM_VCPU_INIT: { |
| 1075 | struct kvm_vcpu_init init; |
| 1076 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1077 | r = -EFAULT; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1078 | if (copy_from_user(&init, argp, sizeof(init))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1079 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1080 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1081 | r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); |
| 1082 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1083 | } |
| 1084 | case KVM_SET_ONE_REG: |
| 1085 | case KVM_GET_ONE_REG: { |
| 1086 | struct kvm_one_reg reg; |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 1087 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1088 | r = -ENOEXEC; |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 1089 | if (unlikely(!kvm_vcpu_initialized(vcpu))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1090 | break; |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 1091 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1092 | r = -EFAULT; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1093 | if (copy_from_user(®, argp, sizeof(reg))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1094 | break; |
| 1095 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1096 | if (ioctl == KVM_SET_ONE_REG) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1097 | r = kvm_arm_set_reg(vcpu, ®); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1098 | else |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1099 | r = kvm_arm_get_reg(vcpu, ®); |
| 1100 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1101 | } |
| 1102 | case KVM_GET_REG_LIST: { |
| 1103 | struct kvm_reg_list __user *user_list = argp; |
| 1104 | struct kvm_reg_list reg_list; |
| 1105 | unsigned n; |
| 1106 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1107 | r = -ENOEXEC; |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 1108 | if (unlikely(!kvm_vcpu_initialized(vcpu))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1109 | break; |
Andre Przywara | e8180dc | 2013-05-09 00:28:06 +0200 | [diff] [blame] | 1110 | |
Dave Martin | 7dd32a0 | 2018-12-19 14:27:01 +0000 | [diff] [blame] | 1111 | r = -EPERM; |
| 1112 | if (!kvm_arm_vcpu_is_finalized(vcpu)) |
| 1113 | break; |
| 1114 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1115 | r = -EFAULT; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1116 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1117 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1118 | n = reg_list.n; |
| 1119 | reg_list.n = kvm_arm_num_regs(vcpu); |
| 1120 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1121 | break; |
| 1122 | r = -E2BIG; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1123 | if (n < reg_list.n) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1124 | break; |
| 1125 | r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); |
| 1126 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1127 | } |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1128 | case KVM_SET_DEVICE_ATTR: { |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1129 | r = -EFAULT; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1130 | if (copy_from_user(&attr, argp, sizeof(attr))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1131 | break; |
| 1132 | r = kvm_arm_vcpu_set_attr(vcpu, &attr); |
| 1133 | break; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1134 | } |
| 1135 | case KVM_GET_DEVICE_ATTR: { |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1136 | r = -EFAULT; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1137 | if (copy_from_user(&attr, argp, sizeof(attr))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1138 | break; |
| 1139 | r = kvm_arm_vcpu_get_attr(vcpu, &attr); |
| 1140 | break; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1141 | } |
| 1142 | case KVM_HAS_DEVICE_ATTR: { |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1143 | r = -EFAULT; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1144 | if (copy_from_user(&attr, argp, sizeof(attr))) |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1145 | break; |
| 1146 | r = kvm_arm_vcpu_has_attr(vcpu, &attr); |
| 1147 | break; |
Shannon Zhao | f577f6c | 2016-01-11 20:56:17 +0800 | [diff] [blame] | 1148 | } |
Dongjiu Geng | b7b27fa | 2018-07-19 16:24:22 +0100 | [diff] [blame] | 1149 | case KVM_GET_VCPU_EVENTS: { |
| 1150 | struct kvm_vcpu_events events; |
| 1151 | |
| 1152 | if (kvm_arm_vcpu_get_events(vcpu, &events)) |
| 1153 | return -EINVAL; |
| 1154 | |
| 1155 | if (copy_to_user(argp, &events, sizeof(events))) |
| 1156 | return -EFAULT; |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
| 1160 | case KVM_SET_VCPU_EVENTS: { |
| 1161 | struct kvm_vcpu_events events; |
| 1162 | |
| 1163 | if (copy_from_user(&events, argp, sizeof(events))) |
| 1164 | return -EFAULT; |
| 1165 | |
| 1166 | return kvm_arm_vcpu_set_events(vcpu, &events); |
| 1167 | } |
Dave Martin | 7dd32a0 | 2018-12-19 14:27:01 +0000 | [diff] [blame] | 1168 | case KVM_ARM_VCPU_FINALIZE: { |
| 1169 | int what; |
| 1170 | |
| 1171 | if (!kvm_vcpu_initialized(vcpu)) |
| 1172 | return -ENOEXEC; |
| 1173 | |
| 1174 | if (get_user(what, (const int __user *)argp)) |
| 1175 | return -EFAULT; |
| 1176 | |
| 1177 | return kvm_arm_vcpu_finalize(vcpu, what); |
| 1178 | } |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1179 | default: |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1180 | r = -EINVAL; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1181 | } |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1182 | |
Christoffer Dall | 9b062471 | 2017-12-04 21:35:36 +0100 | [diff] [blame] | 1183 | return r; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1184 | } |
| 1185 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1186 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1187 | { |
Mario Smarduch | 53c810c | 2015-01-15 15:58:57 -0800 | [diff] [blame] | 1188 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1189 | } |
| 1190 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1191 | void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, |
| 1192 | struct kvm_memory_slot *memslot) |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1193 | { |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1194 | kvm_flush_remote_tlbs(kvm); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1195 | } |
| 1196 | |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 1197 | static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, |
| 1198 | struct kvm_arm_device_addr *dev_addr) |
| 1199 | { |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 1200 | unsigned long dev_id, type; |
| 1201 | |
| 1202 | dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >> |
| 1203 | KVM_ARM_DEVICE_ID_SHIFT; |
| 1204 | type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >> |
| 1205 | KVM_ARM_DEVICE_TYPE_SHIFT; |
| 1206 | |
| 1207 | switch (dev_id) { |
| 1208 | case KVM_ARM_DEVICE_VGIC_V2: |
Pavel Fedin | c7da6fa | 2015-12-18 14:38:43 +0300 | [diff] [blame] | 1209 | if (!vgic_present) |
| 1210 | return -ENXIO; |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 1211 | return kvm_vgic_addr(kvm, type, &dev_addr->addr, true); |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 1212 | default: |
| 1213 | return -ENODEV; |
| 1214 | } |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 1215 | } |
| 1216 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1217 | long kvm_arch_vm_ioctl(struct file *filp, |
| 1218 | unsigned int ioctl, unsigned long arg) |
| 1219 | { |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 1220 | struct kvm *kvm = filp->private_data; |
| 1221 | void __user *argp = (void __user *)arg; |
| 1222 | |
| 1223 | switch (ioctl) { |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1224 | case KVM_CREATE_IRQCHIP: { |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 1225 | int ret; |
Pavel Fedin | c7da6fa | 2015-12-18 14:38:43 +0300 | [diff] [blame] | 1226 | if (!vgic_present) |
| 1227 | return -ENXIO; |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 1228 | mutex_lock(&kvm->lock); |
| 1229 | ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); |
| 1230 | mutex_unlock(&kvm->lock); |
| 1231 | return ret; |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1232 | } |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 1233 | case KVM_ARM_SET_DEVICE_ADDR: { |
| 1234 | struct kvm_arm_device_addr dev_addr; |
| 1235 | |
| 1236 | if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) |
| 1237 | return -EFAULT; |
| 1238 | return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); |
| 1239 | } |
Anup Patel | 42c4e0c | 2013-09-30 14:20:07 +0530 | [diff] [blame] | 1240 | case KVM_ARM_PREFERRED_TARGET: { |
| 1241 | int err; |
| 1242 | struct kvm_vcpu_init init; |
| 1243 | |
| 1244 | err = kvm_vcpu_preferred_target(&init); |
| 1245 | if (err) |
| 1246 | return err; |
| 1247 | |
| 1248 | if (copy_to_user(argp, &init, sizeof(init))) |
| 1249 | return -EFAULT; |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 1253 | default: |
| 1254 | return -EINVAL; |
| 1255 | } |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1256 | } |
| 1257 | |
Miaohe Lin | 7e0befd | 2019-11-21 15:15:59 +0800 | [diff] [blame] | 1258 | static void cpu_init_hyp_mode(void) |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1259 | { |
Marc Zyngier | dac288f | 2013-05-14 12:11:37 +0100 | [diff] [blame] | 1260 | phys_addr_t pgd_ptr; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1261 | unsigned long hyp_stack_ptr; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1262 | unsigned long vector_ptr; |
David Brazdil | 71b3ec5 | 2020-05-15 16:20:56 +0100 | [diff] [blame] | 1263 | unsigned long tpidr_el2; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1264 | |
| 1265 | /* Switch from the HYP stub to our own HYP init vector */ |
Marc Zyngier | 5a677ce | 2013-04-12 19:12:06 +0100 | [diff] [blame] | 1266 | __hyp_set_vectors(kvm_get_idmap_vector()); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1267 | |
David Brazdil | 71b3ec5 | 2020-05-15 16:20:56 +0100 | [diff] [blame] | 1268 | /* |
| 1269 | * Calculate the raw per-cpu offset without a translation from the |
| 1270 | * kernel's mapping to the linear mapping, and store it in tpidr_el2 |
| 1271 | * so that we can use adr_l to access per-cpu variables in EL2. |
| 1272 | */ |
| 1273 | tpidr_el2 = ((unsigned long)this_cpu_ptr(&kvm_host_data) - |
David Brazdil | b877e98 | 2020-06-25 14:14:11 +0100 | [diff] [blame] | 1274 | (unsigned long)kvm_ksym_ref(&kvm_host_data)); |
David Brazdil | 71b3ec5 | 2020-05-15 16:20:56 +0100 | [diff] [blame] | 1275 | |
Marc Zyngier | dac288f | 2013-05-14 12:11:37 +0100 | [diff] [blame] | 1276 | pgd_ptr = kvm_mmu_get_httbr(); |
David Brazdil | 71b3ec5 | 2020-05-15 16:20:56 +0100 | [diff] [blame] | 1277 | hyp_stack_ptr = __this_cpu_read(kvm_arm_hyp_stack_page) + PAGE_SIZE; |
Marc Zyngier | 6840bdd | 2018-01-03 16:38:35 +0000 | [diff] [blame] | 1278 | vector_ptr = (unsigned long)kvm_get_hyp_vector(); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1279 | |
David Brazdil | 71b3ec5 | 2020-05-15 16:20:56 +0100 | [diff] [blame] | 1280 | /* |
| 1281 | * Call initialization code, and switch to the full blown HYP code. |
| 1282 | * If the cpucaps haven't been finalized yet, something has gone very |
| 1283 | * wrong, and hyp will crash and burn when it uses any |
| 1284 | * cpus_have_const_cap() wrapper. |
| 1285 | */ |
| 1286 | BUG_ON(!system_capabilities_finalized()); |
| 1287 | __kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr, tpidr_el2); |
| 1288 | |
| 1289 | /* |
| 1290 | * Disabling SSBD on a non-VHE system requires us to enable SSBS |
| 1291 | * at EL2. |
| 1292 | */ |
| 1293 | if (this_cpu_has_cap(ARM64_SSBS) && |
| 1294 | arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) { |
David Brazdil | 13aeb9b | 2020-06-25 14:14:16 +0100 | [diff] [blame] | 1295 | kvm_call_hyp_nvhe(__kvm_enable_ssbs); |
David Brazdil | 71b3ec5 | 2020-05-15 16:20:56 +0100 | [diff] [blame] | 1296 | } |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1297 | } |
| 1298 | |
Marc Zyngier | 47eb3cb | 2017-04-03 19:38:01 +0100 | [diff] [blame] | 1299 | static void cpu_hyp_reset(void) |
| 1300 | { |
| 1301 | if (!is_kernel_in_hyp_mode()) |
| 1302 | __hyp_reset_vectors(); |
| 1303 | } |
| 1304 | |
James Morse | 5f5560b | 2016-03-30 18:33:04 +0100 | [diff] [blame] | 1305 | static void cpu_hyp_reinit(void) |
| 1306 | { |
Marc Zyngier | 1e0cf16 | 2019-07-05 23:35:56 +0100 | [diff] [blame] | 1307 | kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt); |
| 1308 | |
Marc Zyngier | 47eb3cb | 2017-04-03 19:38:01 +0100 | [diff] [blame] | 1309 | cpu_hyp_reset(); |
| 1310 | |
Marc Zyngier | 9d47bb0 | 2018-10-01 13:41:32 +0100 | [diff] [blame] | 1311 | if (is_kernel_in_hyp_mode()) |
Hu Huajun | 02d50cd | 2017-06-12 22:37:48 +0800 | [diff] [blame] | 1312 | kvm_timer_init_vhe(); |
Marc Zyngier | 9d47bb0 | 2018-10-01 13:41:32 +0100 | [diff] [blame] | 1313 | else |
Miaohe Lin | 7e0befd | 2019-11-21 15:15:59 +0800 | [diff] [blame] | 1314 | cpu_init_hyp_mode(); |
Christoffer Dall | 5b0d2cc | 2017-03-18 13:56:56 +0100 | [diff] [blame] | 1315 | |
Mark Rutland | da5a3ce | 2018-10-17 17:42:10 +0100 | [diff] [blame] | 1316 | kvm_arm_init_debug(); |
Christoffer Dall | 5b0d2cc | 2017-03-18 13:56:56 +0100 | [diff] [blame] | 1317 | |
| 1318 | if (vgic_present) |
| 1319 | kvm_vgic_init_cpu_hardware(); |
James Morse | 5f5560b | 2016-03-30 18:33:04 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1322 | static void _kvm_arch_hardware_enable(void *discard) |
| 1323 | { |
| 1324 | if (!__this_cpu_read(kvm_arm_hardware_enabled)) { |
| 1325 | cpu_hyp_reinit(); |
| 1326 | __this_cpu_write(kvm_arm_hardware_enabled, 1); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | int kvm_arch_hardware_enable(void) |
| 1331 | { |
| 1332 | _kvm_arch_hardware_enable(NULL); |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | static void _kvm_arch_hardware_disable(void *discard) |
| 1337 | { |
| 1338 | if (__this_cpu_read(kvm_arm_hardware_enabled)) { |
| 1339 | cpu_hyp_reset(); |
| 1340 | __this_cpu_write(kvm_arm_hardware_enabled, 0); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | void kvm_arch_hardware_disable(void) |
| 1345 | { |
| 1346 | _kvm_arch_hardware_disable(NULL); |
| 1347 | } |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 1348 | |
Lorenzo Pieralisi | 1fcf7ce | 2013-08-05 15:04:46 +0100 | [diff] [blame] | 1349 | #ifdef CONFIG_CPU_PM |
| 1350 | static int hyp_init_cpu_pm_notifier(struct notifier_block *self, |
| 1351 | unsigned long cmd, |
| 1352 | void *v) |
| 1353 | { |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1354 | /* |
| 1355 | * kvm_arm_hardware_enabled is left with its old value over |
| 1356 | * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should |
| 1357 | * re-enable hyp. |
| 1358 | */ |
| 1359 | switch (cmd) { |
| 1360 | case CPU_PM_ENTER: |
| 1361 | if (__this_cpu_read(kvm_arm_hardware_enabled)) |
| 1362 | /* |
| 1363 | * don't update kvm_arm_hardware_enabled here |
| 1364 | * so that the hardware will be re-enabled |
| 1365 | * when we resume. See below. |
| 1366 | */ |
| 1367 | cpu_hyp_reset(); |
Lorenzo Pieralisi | 1fcf7ce | 2013-08-05 15:04:46 +0100 | [diff] [blame] | 1368 | |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1369 | return NOTIFY_OK; |
James Morse | 58d6b15 | 2018-01-22 18:19:06 +0000 | [diff] [blame] | 1370 | case CPU_PM_ENTER_FAILED: |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1371 | case CPU_PM_EXIT: |
| 1372 | if (__this_cpu_read(kvm_arm_hardware_enabled)) |
| 1373 | /* The hardware was enabled before suspend. */ |
| 1374 | cpu_hyp_reinit(); |
| 1375 | |
| 1376 | return NOTIFY_OK; |
| 1377 | |
| 1378 | default: |
| 1379 | return NOTIFY_DONE; |
| 1380 | } |
Lorenzo Pieralisi | 1fcf7ce | 2013-08-05 15:04:46 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | static struct notifier_block hyp_init_cpu_pm_nb = { |
| 1384 | .notifier_call = hyp_init_cpu_pm_notifier, |
| 1385 | }; |
| 1386 | |
| 1387 | static void __init hyp_cpu_pm_init(void) |
| 1388 | { |
| 1389 | cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); |
| 1390 | } |
Sudeep Holla | 06a71a2 | 2016-04-04 14:46:51 +0100 | [diff] [blame] | 1391 | static void __init hyp_cpu_pm_exit(void) |
| 1392 | { |
| 1393 | cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); |
| 1394 | } |
Lorenzo Pieralisi | 1fcf7ce | 2013-08-05 15:04:46 +0100 | [diff] [blame] | 1395 | #else |
| 1396 | static inline void hyp_cpu_pm_init(void) |
| 1397 | { |
| 1398 | } |
Sudeep Holla | 06a71a2 | 2016-04-04 14:46:51 +0100 | [diff] [blame] | 1399 | static inline void hyp_cpu_pm_exit(void) |
| 1400 | { |
| 1401 | } |
Lorenzo Pieralisi | 1fcf7ce | 2013-08-05 15:04:46 +0100 | [diff] [blame] | 1402 | #endif |
| 1403 | |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1404 | static int init_common_resources(void) |
| 1405 | { |
Marc Zyngier | b130a8f | 2020-05-28 14:12:58 +0100 | [diff] [blame] | 1406 | return kvm_set_ipa_limit(); |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | static int init_subsystems(void) |
| 1410 | { |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1411 | int err = 0; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1412 | |
| 1413 | /* |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1414 | * Enable hardware so that subsystem initialisation can access EL2. |
James Morse | 5f5560b | 2016-03-30 18:33:04 +0100 | [diff] [blame] | 1415 | */ |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1416 | on_each_cpu(_kvm_arch_hardware_enable, NULL, 1); |
James Morse | 5f5560b | 2016-03-30 18:33:04 +0100 | [diff] [blame] | 1417 | |
| 1418 | /* |
| 1419 | * Register CPU lower-power notifier |
| 1420 | */ |
| 1421 | hyp_cpu_pm_init(); |
| 1422 | |
| 1423 | /* |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1424 | * Init HYP view of VGIC |
| 1425 | */ |
| 1426 | err = kvm_vgic_hyp_init(); |
| 1427 | switch (err) { |
| 1428 | case 0: |
| 1429 | vgic_present = true; |
| 1430 | break; |
| 1431 | case -ENODEV: |
| 1432 | case -ENXIO: |
| 1433 | vgic_present = false; |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1434 | err = 0; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | default: |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1437 | goto out; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * Init HYP architected timer support |
| 1442 | */ |
Marc Zyngier | f384dcf | 2017-12-07 11:46:15 +0000 | [diff] [blame] | 1443 | err = kvm_timer_hyp_init(vgic_present); |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1444 | if (err) |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1445 | goto out; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1446 | |
| 1447 | kvm_perf_init(); |
| 1448 | kvm_coproc_table_init(); |
| 1449 | |
AKASHI Takahiro | 67f6919 | 2016-04-27 17:47:05 +0100 | [diff] [blame] | 1450 | out: |
| 1451 | on_each_cpu(_kvm_arch_hardware_disable, NULL, 1); |
| 1452 | |
| 1453 | return err; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static void teardown_hyp_mode(void) |
| 1457 | { |
| 1458 | int cpu; |
| 1459 | |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1460 | free_hyp_pgds(); |
| 1461 | for_each_possible_cpu(cpu) |
| 1462 | free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); |
| 1463 | } |
| 1464 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1465 | /** |
| 1466 | * Inits Hyp-mode on all online CPUs |
| 1467 | */ |
| 1468 | static int init_hyp_mode(void) |
| 1469 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1470 | int cpu; |
| 1471 | int err = 0; |
| 1472 | |
| 1473 | /* |
| 1474 | * Allocate Hyp PGD and setup Hyp identity mapping |
| 1475 | */ |
| 1476 | err = kvm_mmu_init(); |
| 1477 | if (err) |
| 1478 | goto out_err; |
| 1479 | |
| 1480 | /* |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1481 | * Allocate stack pages for Hypervisor-mode |
| 1482 | */ |
| 1483 | for_each_possible_cpu(cpu) { |
| 1484 | unsigned long stack_page; |
| 1485 | |
| 1486 | stack_page = __get_free_page(GFP_KERNEL); |
| 1487 | if (!stack_page) { |
| 1488 | err = -ENOMEM; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1489 | goto out_err; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1496 | * Map the Hyp-code called directly from the host |
| 1497 | */ |
Linus Torvalds | 588ab3f | 2016-03-17 20:03:47 -0700 | [diff] [blame] | 1498 | err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), |
Marc Zyngier | 5900270 | 2016-06-13 15:00:48 +0100 | [diff] [blame] | 1499 | kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1500 | if (err) { |
| 1501 | kvm_err("Cannot map world-switch code\n"); |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1502 | goto out_err; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1503 | } |
| 1504 | |
Ard Biesheuvel | a0bf977 | 2016-02-16 13:52:39 +0100 | [diff] [blame] | 1505 | err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), |
Marc Zyngier | 74a6b88 | 2016-06-13 15:00:47 +0100 | [diff] [blame] | 1506 | kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); |
Marc Zyngier | 910917b | 2015-10-27 12:18:48 +0000 | [diff] [blame] | 1507 | if (err) { |
| 1508 | kvm_err("Cannot map rodata section\n"); |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1509 | goto out_err; |
Marc Zyngier | 910917b | 2015-10-27 12:18:48 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Marc Zyngier | c8ea039 | 2016-10-20 10:17:21 +0100 | [diff] [blame] | 1512 | err = create_hyp_mappings(kvm_ksym_ref(__bss_start), |
| 1513 | kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); |
| 1514 | if (err) { |
| 1515 | kvm_err("Cannot map bss section\n"); |
| 1516 | goto out_err; |
| 1517 | } |
| 1518 | |
Marc Zyngier | 6840bdd | 2018-01-03 16:38:35 +0000 | [diff] [blame] | 1519 | err = kvm_map_vectors(); |
| 1520 | if (err) { |
| 1521 | kvm_err("Cannot map vectors\n"); |
| 1522 | goto out_err; |
| 1523 | } |
| 1524 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1525 | /* |
| 1526 | * Map the Hyp stack pages |
| 1527 | */ |
| 1528 | for_each_possible_cpu(cpu) { |
| 1529 | char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); |
Marc Zyngier | c8dddec | 2016-06-13 15:00:45 +0100 | [diff] [blame] | 1530 | err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, |
| 1531 | PAGE_HYP); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1532 | |
| 1533 | if (err) { |
| 1534 | kvm_err("Cannot map hyp stack\n"); |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1535 | goto out_err; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1539 | for_each_possible_cpu(cpu) { |
Andrew Murray | 630a168 | 2019-04-09 20:22:11 +0100 | [diff] [blame] | 1540 | kvm_host_data_t *cpu_data; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1541 | |
Andrew Murray | 630a168 | 2019-04-09 20:22:11 +0100 | [diff] [blame] | 1542 | cpu_data = per_cpu_ptr(&kvm_host_data, cpu); |
Andrew Murray | 630a168 | 2019-04-09 20:22:11 +0100 | [diff] [blame] | 1543 | err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1544 | |
| 1545 | if (err) { |
Marc Zyngier | 3de50da | 2013-04-08 16:47:19 +0100 | [diff] [blame] | 1546 | kvm_err("Cannot map host CPU state: %d\n", err); |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1547 | goto out_err; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 1551 | err = hyp_map_aux_data(); |
| 1552 | if (err) |
Colin Ian King | a37f0c3 | 2019-02-17 22:36:26 +0000 | [diff] [blame] | 1553 | kvm_err("Cannot map host auxiliary data: %d\n", err); |
Marc Zyngier | 55e3748 | 2018-05-29 13:11:16 +0100 | [diff] [blame] | 1554 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1555 | return 0; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1556 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1557 | out_err: |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1558 | teardown_hyp_mode(); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1559 | kvm_err("error initializing Hyp mode: %d\n", err); |
| 1560 | return err; |
| 1561 | } |
| 1562 | |
Andre Przywara | d4e071c | 2013-04-17 12:52:01 +0200 | [diff] [blame] | 1563 | static void check_kvm_target_cpu(void *ret) |
| 1564 | { |
| 1565 | *(int *)ret = kvm_target_cpu(); |
| 1566 | } |
| 1567 | |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 1568 | struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) |
| 1569 | { |
| 1570 | struct kvm_vcpu *vcpu; |
| 1571 | int i; |
| 1572 | |
| 1573 | mpidr &= MPIDR_HWID_BITMASK; |
| 1574 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1575 | if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) |
| 1576 | return vcpu; |
| 1577 | } |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | |
Eric Auger | 2412405 | 2017-10-27 15:28:31 +0100 | [diff] [blame] | 1581 | bool kvm_arch_has_irq_bypass(void) |
| 1582 | { |
| 1583 | return true; |
| 1584 | } |
| 1585 | |
| 1586 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
| 1587 | struct irq_bypass_producer *prod) |
| 1588 | { |
| 1589 | struct kvm_kernel_irqfd *irqfd = |
| 1590 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 1591 | |
Marc Zyngier | 196b136 | 2017-10-27 15:28:39 +0100 | [diff] [blame] | 1592 | return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, |
| 1593 | &irqfd->irq_entry); |
Eric Auger | 2412405 | 2017-10-27 15:28:31 +0100 | [diff] [blame] | 1594 | } |
| 1595 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, |
| 1596 | struct irq_bypass_producer *prod) |
| 1597 | { |
| 1598 | struct kvm_kernel_irqfd *irqfd = |
| 1599 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 1600 | |
Marc Zyngier | 196b136 | 2017-10-27 15:28:39 +0100 | [diff] [blame] | 1601 | kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, |
| 1602 | &irqfd->irq_entry); |
Eric Auger | 2412405 | 2017-10-27 15:28:31 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) |
| 1606 | { |
| 1607 | struct kvm_kernel_irqfd *irqfd = |
| 1608 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 1609 | |
| 1610 | kvm_arm_halt_guest(irqfd->kvm); |
| 1611 | } |
| 1612 | |
| 1613 | void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) |
| 1614 | { |
| 1615 | struct kvm_kernel_irqfd *irqfd = |
| 1616 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 1617 | |
| 1618 | kvm_arm_resume_guest(irqfd->kvm); |
| 1619 | } |
| 1620 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1621 | /** |
| 1622 | * Initialize Hyp-mode and memory mappings on all CPUs. |
| 1623 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1624 | int kvm_arch_init(void *opaque) |
| 1625 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1626 | int err; |
Andre Przywara | d4e071c | 2013-04-17 12:52:01 +0200 | [diff] [blame] | 1627 | int ret, cpu; |
Julien Thierry | fe7d7b0 | 2017-10-20 12:34:16 +0100 | [diff] [blame] | 1628 | bool in_hyp_mode; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1629 | |
| 1630 | if (!is_hyp_mode_available()) { |
Ard Biesheuvel | 58d0d19 | 2017-11-28 15:18:19 +0000 | [diff] [blame] | 1631 | kvm_info("HYP mode not available\n"); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1632 | return -ENODEV; |
| 1633 | } |
| 1634 | |
Marc Zyngier | 33e5f4e | 2018-12-06 17:31:20 +0000 | [diff] [blame] | 1635 | in_hyp_mode = is_kernel_in_hyp_mode(); |
| 1636 | |
| 1637 | if (!in_hyp_mode && kvm_arch_requires_vhe()) { |
| 1638 | kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n"); |
Dave Martin | 85acda3 | 2018-04-20 16:20:43 +0100 | [diff] [blame] | 1639 | return -ENODEV; |
| 1640 | } |
| 1641 | |
Andre Przywara | d4e071c | 2013-04-17 12:52:01 +0200 | [diff] [blame] | 1642 | for_each_online_cpu(cpu) { |
| 1643 | smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1); |
| 1644 | if (ret < 0) { |
| 1645 | kvm_err("Error, CPU %d not supported!\n", cpu); |
| 1646 | return -ENODEV; |
| 1647 | } |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1648 | } |
| 1649 | |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1650 | err = init_common_resources(); |
| 1651 | if (err) |
| 1652 | return err; |
Srivatsa S. Bhat | 8146875 | 2014-03-18 15:53:05 +0530 | [diff] [blame] | 1653 | |
Dave Martin | a3be836 | 2019-04-12 15:30:58 +0100 | [diff] [blame] | 1654 | err = kvm_arm_init_sve(); |
Dave Martin | 0f062bf | 2019-02-28 18:33:00 +0000 | [diff] [blame] | 1655 | if (err) |
| 1656 | return err; |
| 1657 | |
Julien Thierry | fe7d7b0 | 2017-10-20 12:34:16 +0100 | [diff] [blame] | 1658 | if (!in_hyp_mode) { |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1659 | err = init_hyp_mode(); |
Julien Thierry | fe7d7b0 | 2017-10-20 12:34:16 +0100 | [diff] [blame] | 1660 | if (err) |
| 1661 | goto out_err; |
| 1662 | } |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1663 | |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1664 | err = init_subsystems(); |
| 1665 | if (err) |
| 1666 | goto out_hyp; |
Marc Zyngier | d157f4a | 2013-04-12 19:12:07 +0100 | [diff] [blame] | 1667 | |
Julien Thierry | fe7d7b0 | 2017-10-20 12:34:16 +0100 | [diff] [blame] | 1668 | if (in_hyp_mode) |
| 1669 | kvm_info("VHE mode initialized successfully\n"); |
| 1670 | else |
| 1671 | kvm_info("Hyp mode initialized successfully\n"); |
| 1672 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1673 | return 0; |
Marc Zyngier | 1e947ba | 2015-01-29 11:59:54 +0000 | [diff] [blame] | 1674 | |
| 1675 | out_hyp: |
Shannon Zhao | c3e3540 | 2019-12-02 15:42:11 +0800 | [diff] [blame] | 1676 | hyp_cpu_pm_exit(); |
Julien Thierry | fe7d7b0 | 2017-10-20 12:34:16 +0100 | [diff] [blame] | 1677 | if (!in_hyp_mode) |
| 1678 | teardown_hyp_mode(); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1679 | out_err: |
| 1680 | return err; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* NOP: Compiling as a module not supported */ |
| 1684 | void kvm_arch_exit(void) |
| 1685 | { |
Marc Zyngier | 210552c | 2013-03-05 03:18:00 +0000 | [diff] [blame] | 1686 | kvm_perf_teardown(); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static int arm_init(void) |
| 1690 | { |
| 1691 | int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 1692 | return rc; |
| 1693 | } |
| 1694 | |
| 1695 | module_init(arm_init); |