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