Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 3 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/kvm_host.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/mman.h> |
| 26 | #include <linux/sched.h> |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 27 | #include <linux/kvm.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 28 | #include <trace/events/kvm.h> |
| 29 | |
| 30 | #define CREATE_TRACE_POINTS |
| 31 | #include "trace.h" |
| 32 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 33 | #include <asm/uaccess.h> |
| 34 | #include <asm/ptrace.h> |
| 35 | #include <asm/mman.h> |
| 36 | #include <asm/cputype.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 37 | #include <asm/tlbflush.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 38 | #include <asm/cacheflush.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 39 | #include <asm/virt.h> |
| 40 | #include <asm/kvm_arm.h> |
| 41 | #include <asm/kvm_asm.h> |
| 42 | #include <asm/kvm_mmu.h> |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 43 | #include <asm/kvm_emulate.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 44 | #include <asm/kvm_coproc.h> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 45 | #include <asm/kvm_psci.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 46 | |
| 47 | #ifdef REQUIRES_VIRT |
| 48 | __asm__(".arch_extension virt"); |
| 49 | #endif |
| 50 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 51 | static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); |
| 52 | static struct vfp_hard_struct __percpu *kvm_host_vfp_state; |
| 53 | static unsigned long hyp_default_vectors; |
| 54 | |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame] | 55 | /* Per-CPU variable containing the currently running vcpu. */ |
| 56 | static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu); |
| 57 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 58 | /* The VMID used in the VTTBR */ |
| 59 | static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); |
| 60 | static u8 kvm_next_vmid; |
| 61 | static DEFINE_SPINLOCK(kvm_vmid_lock); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 62 | |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 63 | static bool vgic_present; |
| 64 | |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame] | 65 | static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu) |
| 66 | { |
| 67 | BUG_ON(preemptible()); |
| 68 | __get_cpu_var(kvm_arm_running_vcpu) = vcpu; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU. |
| 73 | * Must be called from non-preemptible context |
| 74 | */ |
| 75 | struct kvm_vcpu *kvm_arm_get_running_vcpu(void) |
| 76 | { |
| 77 | BUG_ON(preemptible()); |
| 78 | return __get_cpu_var(kvm_arm_running_vcpu); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus. |
| 83 | */ |
| 84 | struct kvm_vcpu __percpu **kvm_get_running_vcpus(void) |
| 85 | { |
| 86 | return &kvm_arm_running_vcpu; |
| 87 | } |
| 88 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 89 | int kvm_arch_hardware_enable(void *garbage) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 95 | { |
| 96 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
| 97 | } |
| 98 | |
| 99 | void kvm_arch_hardware_disable(void *garbage) |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | int kvm_arch_hardware_setup(void) |
| 104 | { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | void kvm_arch_hardware_unsetup(void) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | void kvm_arch_check_processor_compat(void *rtn) |
| 113 | { |
| 114 | *(int *)rtn = 0; |
| 115 | } |
| 116 | |
| 117 | void kvm_arch_sync_events(struct kvm *kvm) |
| 118 | { |
| 119 | } |
| 120 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 121 | /** |
| 122 | * kvm_arch_init_vm - initializes a VM data structure |
| 123 | * @kvm: pointer to the KVM struct |
| 124 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 125 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 126 | { |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 127 | int ret = 0; |
| 128 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 129 | if (type) |
| 130 | return -EINVAL; |
| 131 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 132 | ret = kvm_alloc_stage2_pgd(kvm); |
| 133 | if (ret) |
| 134 | goto out_fail_alloc; |
| 135 | |
| 136 | ret = create_hyp_mappings(kvm, kvm + 1); |
| 137 | if (ret) |
| 138 | goto out_free_stage2_pgd; |
| 139 | |
| 140 | /* Mark the initial VMID generation invalid */ |
| 141 | kvm->arch.vmid_gen = 0; |
| 142 | |
| 143 | return ret; |
| 144 | out_free_stage2_pgd: |
| 145 | kvm_free_stage2_pgd(kvm); |
| 146 | out_fail_alloc: |
| 147 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 151 | { |
| 152 | return VM_FAULT_SIGBUS; |
| 153 | } |
| 154 | |
| 155 | void kvm_arch_free_memslot(struct kvm_memory_slot *free, |
| 156 | struct kvm_memory_slot *dont) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages) |
| 161 | { |
| 162 | return 0; |
| 163 | } |
| 164 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 165 | /** |
| 166 | * kvm_arch_destroy_vm - destroy the VM data structure |
| 167 | * @kvm: pointer to the KVM struct |
| 168 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 169 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 170 | { |
| 171 | int i; |
| 172 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 173 | kvm_free_stage2_pgd(kvm); |
| 174 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 175 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 176 | if (kvm->vcpus[i]) { |
| 177 | kvm_arch_vcpu_free(kvm->vcpus[i]); |
| 178 | kvm->vcpus[i] = NULL; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | int kvm_dev_ioctl_check_extension(long ext) |
| 184 | { |
| 185 | int r; |
| 186 | switch (ext) { |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 187 | case KVM_CAP_IRQCHIP: |
| 188 | r = vgic_present; |
| 189 | break; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 190 | case KVM_CAP_USER_MEMORY: |
| 191 | case KVM_CAP_SYNC_MMU: |
| 192 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
| 193 | case KVM_CAP_ONE_REG: |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 194 | case KVM_CAP_ARM_PSCI: |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 195 | r = 1; |
| 196 | break; |
| 197 | case KVM_CAP_COALESCED_MMIO: |
| 198 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 199 | break; |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 200 | case KVM_CAP_ARM_SET_DEVICE_ADDR: |
| 201 | r = 1; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 202 | case KVM_CAP_NR_VCPUS: |
| 203 | r = num_online_cpus(); |
| 204 | break; |
| 205 | case KVM_CAP_MAX_VCPUS: |
| 206 | r = KVM_MAX_VCPUS; |
| 207 | break; |
| 208 | default: |
| 209 | r = 0; |
| 210 | break; |
| 211 | } |
| 212 | return r; |
| 213 | } |
| 214 | |
| 215 | long kvm_arch_dev_ioctl(struct file *filp, |
| 216 | unsigned int ioctl, unsigned long arg) |
| 217 | { |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | int kvm_arch_set_memory_region(struct kvm *kvm, |
| 222 | struct kvm_userspace_memory_region *mem, |
| 223 | struct kvm_memory_slot old, |
| 224 | int user_alloc) |
| 225 | { |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 230 | struct kvm_memory_slot *memslot, |
| 231 | struct kvm_memory_slot old, |
| 232 | struct kvm_userspace_memory_region *mem, |
Marc Zyngier | bef103a | 2013-02-15 19:20:06 +0000 | [diff] [blame] | 233 | bool user_alloc) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 234 | { |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 239 | struct kvm_userspace_memory_region *mem, |
| 240 | struct kvm_memory_slot old, |
Marc Zyngier | bef103a | 2013-02-15 19:20:06 +0000 | [diff] [blame] | 241 | bool user_alloc) |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 242 | { |
| 243 | } |
| 244 | |
| 245 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 246 | { |
| 247 | } |
| 248 | |
| 249 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 250 | struct kvm_memory_slot *slot) |
| 251 | { |
| 252 | } |
| 253 | |
| 254 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 255 | { |
| 256 | int err; |
| 257 | struct kvm_vcpu *vcpu; |
| 258 | |
| 259 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
| 260 | if (!vcpu) { |
| 261 | err = -ENOMEM; |
| 262 | goto out; |
| 263 | } |
| 264 | |
| 265 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 266 | if (err) |
| 267 | goto free_vcpu; |
| 268 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 269 | err = create_hyp_mappings(vcpu, vcpu + 1); |
| 270 | if (err) |
| 271 | goto vcpu_uninit; |
| 272 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 273 | return vcpu; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 274 | vcpu_uninit: |
| 275 | kvm_vcpu_uninit(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 276 | free_vcpu: |
| 277 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
| 278 | out: |
| 279 | return ERR_PTR(err); |
| 280 | } |
| 281 | |
| 282 | int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 283 | { |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 288 | { |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 289 | kvm_mmu_free_memory_caches(vcpu); |
Marc Zyngier | 967f842 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 290 | kvm_timer_vcpu_terminate(vcpu); |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 291 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 295 | { |
| 296 | kvm_arch_vcpu_free(vcpu); |
| 297 | } |
| 298 | |
| 299 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 300 | { |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | int __attribute_const__ kvm_target_cpu(void) |
| 305 | { |
| 306 | unsigned long implementor = read_cpuid_implementor(); |
| 307 | unsigned long part_number = read_cpuid_part_number(); |
| 308 | |
| 309 | if (implementor != ARM_CPU_IMP_ARM) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | switch (part_number) { |
| 313 | case ARM_CPU_PART_CORTEX_A15: |
| 314 | return KVM_ARM_TARGET_CORTEX_A15; |
| 315 | default: |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 321 | { |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 322 | int ret; |
| 323 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 324 | /* Force users to call KVM_ARM_VCPU_INIT */ |
| 325 | vcpu->arch.target = -1; |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 326 | |
| 327 | /* Set up VGIC */ |
| 328 | ret = kvm_vgic_vcpu_init(vcpu); |
| 329 | if (ret) |
| 330 | return ret; |
| 331 | |
Marc Zyngier | 967f842 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 332 | /* Set up the timer */ |
| 333 | kvm_timer_vcpu_init(vcpu); |
| 334 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 343 | { |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 344 | vcpu->cpu = cpu; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 345 | vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state); |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Check whether this vcpu requires the cache to be flushed on |
| 349 | * this physical CPU. This is a consequence of doing dcache |
| 350 | * operations by set/way on this vcpu. We do it here to be in |
| 351 | * a non-preemptible section. |
| 352 | */ |
| 353 | if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush)) |
| 354 | flush_cache_all(); /* We'd really want v7_flush_dcache_all() */ |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame] | 355 | |
| 356 | kvm_arm_set_running_vcpu(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 360 | { |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame] | 361 | kvm_arm_set_running_vcpu(NULL); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 365 | struct kvm_guest_debug *dbg) |
| 366 | { |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 372 | struct kvm_mp_state *mp_state) |
| 373 | { |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 378 | struct kvm_mp_state *mp_state) |
| 379 | { |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 383 | /** |
| 384 | * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled |
| 385 | * @v: The VCPU pointer |
| 386 | * |
| 387 | * If the guest CPU is not waiting for interrupts or an interrupt line is |
| 388 | * asserted, the CPU is by definition runnable. |
| 389 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 390 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 391 | { |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 392 | return !!v->arch.irq_lines || kvm_vgic_vcpu_pending_irq(v); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 395 | /* Just ensure a guest exit from a particular CPU */ |
| 396 | static void exit_vm_noop(void *info) |
| 397 | { |
| 398 | } |
| 399 | |
| 400 | void force_vm_exit(const cpumask_t *mask) |
| 401 | { |
| 402 | smp_call_function_many(mask, exit_vm_noop, NULL, true); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * need_new_vmid_gen - check that the VMID is still valid |
| 407 | * @kvm: The VM's VMID to checkt |
| 408 | * |
| 409 | * return true if there is a new generation of VMIDs being used |
| 410 | * |
| 411 | * The hardware supports only 256 values with the value zero reserved for the |
| 412 | * host, so we check if an assigned value belongs to a previous generation, |
| 413 | * which which requires us to assign a new value. If we're the first to use a |
| 414 | * VMID for the new generation, we must flush necessary caches and TLBs on all |
| 415 | * CPUs. |
| 416 | */ |
| 417 | static bool need_new_vmid_gen(struct kvm *kvm) |
| 418 | { |
| 419 | return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen)); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * update_vttbr - Update the VTTBR with a valid VMID before the guest runs |
| 424 | * @kvm The guest that we are about to run |
| 425 | * |
| 426 | * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the |
| 427 | * VM has a valid VMID, otherwise assigns a new one and flushes corresponding |
| 428 | * caches and TLBs. |
| 429 | */ |
| 430 | static void update_vttbr(struct kvm *kvm) |
| 431 | { |
| 432 | phys_addr_t pgd_phys; |
| 433 | u64 vmid; |
| 434 | |
| 435 | if (!need_new_vmid_gen(kvm)) |
| 436 | return; |
| 437 | |
| 438 | spin_lock(&kvm_vmid_lock); |
| 439 | |
| 440 | /* |
| 441 | * We need to re-check the vmid_gen here to ensure that if another vcpu |
| 442 | * already allocated a valid vmid for this vm, then this vcpu should |
| 443 | * use the same vmid. |
| 444 | */ |
| 445 | if (!need_new_vmid_gen(kvm)) { |
| 446 | spin_unlock(&kvm_vmid_lock); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | /* First user of a new VMID generation? */ |
| 451 | if (unlikely(kvm_next_vmid == 0)) { |
| 452 | atomic64_inc(&kvm_vmid_gen); |
| 453 | kvm_next_vmid = 1; |
| 454 | |
| 455 | /* |
| 456 | * On SMP we know no other CPUs can use this CPU's or each |
| 457 | * other's VMID after force_vm_exit returns since the |
| 458 | * kvm_vmid_lock blocks them from reentry to the guest. |
| 459 | */ |
| 460 | force_vm_exit(cpu_all_mask); |
| 461 | /* |
| 462 | * Now broadcast TLB + ICACHE invalidation over the inner |
| 463 | * shareable domain to make sure all data structures are |
| 464 | * clean. |
| 465 | */ |
| 466 | kvm_call_hyp(__kvm_flush_vm_context); |
| 467 | } |
| 468 | |
| 469 | kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen); |
| 470 | kvm->arch.vmid = kvm_next_vmid; |
| 471 | kvm_next_vmid++; |
| 472 | |
| 473 | /* update vttbr to be used with the new vmid */ |
| 474 | pgd_phys = virt_to_phys(kvm->arch.pgd); |
| 475 | vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK; |
| 476 | kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK; |
| 477 | kvm->arch.vttbr |= vmid; |
| 478 | |
| 479 | spin_unlock(&kvm_vmid_lock); |
| 480 | } |
| 481 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 482 | static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) |
| 483 | { |
| 484 | if (likely(vcpu->arch.has_run_once)) |
| 485 | return 0; |
| 486 | |
| 487 | vcpu->arch.has_run_once = true; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 488 | |
| 489 | /* |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 490 | * Initialize the VGIC before running a vcpu the first time on |
| 491 | * this VM. |
| 492 | */ |
| 493 | if (irqchip_in_kernel(vcpu->kvm) && |
| 494 | unlikely(!vgic_initialized(vcpu->kvm))) { |
| 495 | int ret = kvm_vgic_init(vcpu->kvm); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | /* |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 501 | * Handle the "start in power-off" case by calling into the |
| 502 | * PSCI code. |
| 503 | */ |
| 504 | if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) { |
| 505 | *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF; |
| 506 | kvm_psci_call(vcpu); |
| 507 | } |
| 508 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 512 | static void vcpu_pause(struct kvm_vcpu *vcpu) |
| 513 | { |
| 514 | wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu); |
| 515 | |
| 516 | wait_event_interruptible(*wq, !vcpu->arch.pause); |
| 517 | } |
| 518 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 519 | /** |
| 520 | * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code |
| 521 | * @vcpu: The VCPU pointer |
| 522 | * @run: The kvm_run structure pointer used for userspace state exchange |
| 523 | * |
| 524 | * This function is called through the VCPU_RUN ioctl called from user space. It |
| 525 | * will execute VM code in a loop until the time slice for the process is used |
| 526 | * or some emulation is needed from user space in which case the function will |
| 527 | * return with return value 0 and with the kvm_run structure filled in with the |
| 528 | * required data for the requested emulation. |
| 529 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 530 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 531 | { |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 532 | int ret; |
| 533 | sigset_t sigsaved; |
| 534 | |
| 535 | /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */ |
| 536 | if (unlikely(vcpu->arch.target < 0)) |
| 537 | return -ENOEXEC; |
| 538 | |
| 539 | ret = kvm_vcpu_first_run_init(vcpu); |
| 540 | if (ret) |
| 541 | return ret; |
| 542 | |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 543 | if (run->exit_reason == KVM_EXIT_MMIO) { |
| 544 | ret = kvm_handle_mmio_return(vcpu, vcpu->run); |
| 545 | if (ret) |
| 546 | return ret; |
| 547 | } |
| 548 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 549 | if (vcpu->sigset_active) |
| 550 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 551 | |
| 552 | ret = 1; |
| 553 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 554 | while (ret > 0) { |
| 555 | /* |
| 556 | * Check conditions before entering the guest |
| 557 | */ |
| 558 | cond_resched(); |
| 559 | |
| 560 | update_vttbr(vcpu->kvm); |
| 561 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 562 | if (vcpu->arch.pause) |
| 563 | vcpu_pause(vcpu); |
| 564 | |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 565 | kvm_vgic_flush_hwstate(vcpu); |
Marc Zyngier | c7e3ba6 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 566 | kvm_timer_flush_hwstate(vcpu); |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 567 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 568 | local_irq_disable(); |
| 569 | |
| 570 | /* |
| 571 | * Re-check atomic conditions |
| 572 | */ |
| 573 | if (signal_pending(current)) { |
| 574 | ret = -EINTR; |
| 575 | run->exit_reason = KVM_EXIT_INTR; |
| 576 | } |
| 577 | |
| 578 | if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) { |
| 579 | local_irq_enable(); |
Marc Zyngier | c7e3ba6 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 580 | kvm_timer_sync_hwstate(vcpu); |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 581 | kvm_vgic_sync_hwstate(vcpu); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 582 | continue; |
| 583 | } |
| 584 | |
| 585 | /************************************************************** |
| 586 | * Enter the guest |
| 587 | */ |
| 588 | trace_kvm_entry(*vcpu_pc(vcpu)); |
| 589 | kvm_guest_enter(); |
| 590 | vcpu->mode = IN_GUEST_MODE; |
| 591 | |
| 592 | ret = kvm_call_hyp(__kvm_vcpu_run, vcpu); |
| 593 | |
| 594 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 595 | vcpu->arch.last_pcpu = smp_processor_id(); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 596 | kvm_guest_exit(); |
| 597 | trace_kvm_exit(*vcpu_pc(vcpu)); |
| 598 | /* |
| 599 | * We may have taken a host interrupt in HYP mode (ie |
| 600 | * while executing the guest). This interrupt is still |
| 601 | * pending, as we haven't serviced it yet! |
| 602 | * |
| 603 | * We're now back in SVC mode, with interrupts |
| 604 | * disabled. Enabling the interrupts now will have |
| 605 | * the effect of taking the interrupt again, in SVC |
| 606 | * mode this time. |
| 607 | */ |
| 608 | local_irq_enable(); |
| 609 | |
| 610 | /* |
| 611 | * Back from guest |
| 612 | *************************************************************/ |
| 613 | |
Marc Zyngier | c7e3ba6 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 614 | kvm_timer_sync_hwstate(vcpu); |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 615 | kvm_vgic_sync_hwstate(vcpu); |
| 616 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 617 | ret = handle_exit(vcpu, run, ret); |
| 618 | } |
| 619 | |
| 620 | if (vcpu->sigset_active) |
| 621 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 622 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 625 | static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) |
| 626 | { |
| 627 | int bit_index; |
| 628 | bool set; |
| 629 | unsigned long *ptr; |
| 630 | |
| 631 | if (number == KVM_ARM_IRQ_CPU_IRQ) |
| 632 | bit_index = __ffs(HCR_VI); |
| 633 | else /* KVM_ARM_IRQ_CPU_FIQ */ |
| 634 | bit_index = __ffs(HCR_VF); |
| 635 | |
| 636 | ptr = (unsigned long *)&vcpu->arch.irq_lines; |
| 637 | if (level) |
| 638 | set = test_and_set_bit(bit_index, ptr); |
| 639 | else |
| 640 | set = test_and_clear_bit(bit_index, ptr); |
| 641 | |
| 642 | /* |
| 643 | * If we didn't change anything, no need to wake up or kick other CPUs |
| 644 | */ |
| 645 | if (set == level) |
| 646 | return 0; |
| 647 | |
| 648 | /* |
| 649 | * The vcpu irq_lines field was updated, wake up sleeping VCPUs and |
| 650 | * trigger a world-switch round on the running physical CPU to set the |
| 651 | * virtual IRQ/FIQ fields in the HCR appropriately. |
| 652 | */ |
| 653 | kvm_vcpu_kick(vcpu); |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level) |
| 659 | { |
| 660 | u32 irq = irq_level->irq; |
| 661 | unsigned int irq_type, vcpu_idx, irq_num; |
| 662 | int nrcpus = atomic_read(&kvm->online_vcpus); |
| 663 | struct kvm_vcpu *vcpu = NULL; |
| 664 | bool level = irq_level->level; |
| 665 | |
| 666 | irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; |
| 667 | vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; |
| 668 | irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; |
| 669 | |
| 670 | trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); |
| 671 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 672 | switch (irq_type) { |
| 673 | case KVM_ARM_IRQ_TYPE_CPU: |
| 674 | if (irqchip_in_kernel(kvm)) |
| 675 | return -ENXIO; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 676 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 677 | if (vcpu_idx >= nrcpus) |
| 678 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 679 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 680 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); |
| 681 | if (!vcpu) |
| 682 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 683 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 684 | if (irq_num > KVM_ARM_IRQ_CPU_FIQ) |
| 685 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 686 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 687 | return vcpu_interrupt_line(vcpu, irq_num, level); |
| 688 | case KVM_ARM_IRQ_TYPE_PPI: |
| 689 | if (!irqchip_in_kernel(kvm)) |
| 690 | return -ENXIO; |
| 691 | |
| 692 | if (vcpu_idx >= nrcpus) |
| 693 | return -EINVAL; |
| 694 | |
| 695 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); |
| 696 | if (!vcpu) |
| 697 | return -EINVAL; |
| 698 | |
| 699 | if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level); |
| 703 | case KVM_ARM_IRQ_TYPE_SPI: |
| 704 | if (!irqchip_in_kernel(kvm)) |
| 705 | return -ENXIO; |
| 706 | |
| 707 | if (irq_num < VGIC_NR_PRIVATE_IRQS || |
| 708 | irq_num > KVM_ARM_IRQ_GIC_MAX) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | return kvm_vgic_inject_irq(kvm, 0, irq_num, level); |
| 712 | } |
| 713 | |
| 714 | return -EINVAL; |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 717 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 718 | unsigned int ioctl, unsigned long arg) |
| 719 | { |
| 720 | struct kvm_vcpu *vcpu = filp->private_data; |
| 721 | void __user *argp = (void __user *)arg; |
| 722 | |
| 723 | switch (ioctl) { |
| 724 | case KVM_ARM_VCPU_INIT: { |
| 725 | struct kvm_vcpu_init init; |
| 726 | |
| 727 | if (copy_from_user(&init, argp, sizeof(init))) |
| 728 | return -EFAULT; |
| 729 | |
| 730 | return kvm_vcpu_set_target(vcpu, &init); |
| 731 | |
| 732 | } |
| 733 | case KVM_SET_ONE_REG: |
| 734 | case KVM_GET_ONE_REG: { |
| 735 | struct kvm_one_reg reg; |
| 736 | if (copy_from_user(®, argp, sizeof(reg))) |
| 737 | return -EFAULT; |
| 738 | if (ioctl == KVM_SET_ONE_REG) |
| 739 | return kvm_arm_set_reg(vcpu, ®); |
| 740 | else |
| 741 | return kvm_arm_get_reg(vcpu, ®); |
| 742 | } |
| 743 | case KVM_GET_REG_LIST: { |
| 744 | struct kvm_reg_list __user *user_list = argp; |
| 745 | struct kvm_reg_list reg_list; |
| 746 | unsigned n; |
| 747 | |
| 748 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
| 749 | return -EFAULT; |
| 750 | n = reg_list.n; |
| 751 | reg_list.n = kvm_arm_num_regs(vcpu); |
| 752 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
| 753 | return -EFAULT; |
| 754 | if (n < reg_list.n) |
| 755 | return -E2BIG; |
| 756 | return kvm_arm_copy_reg_indices(vcpu, user_list->reg); |
| 757 | } |
| 758 | default: |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
| 764 | { |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 768 | static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, |
| 769 | struct kvm_arm_device_addr *dev_addr) |
| 770 | { |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 771 | unsigned long dev_id, type; |
| 772 | |
| 773 | dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >> |
| 774 | KVM_ARM_DEVICE_ID_SHIFT; |
| 775 | type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >> |
| 776 | KVM_ARM_DEVICE_TYPE_SHIFT; |
| 777 | |
| 778 | switch (dev_id) { |
| 779 | case KVM_ARM_DEVICE_VGIC_V2: |
| 780 | if (!vgic_present) |
| 781 | return -ENXIO; |
| 782 | return kvm_vgic_set_addr(kvm, type, dev_addr->addr); |
| 783 | default: |
| 784 | return -ENODEV; |
| 785 | } |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 786 | } |
| 787 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 788 | long kvm_arch_vm_ioctl(struct file *filp, |
| 789 | unsigned int ioctl, unsigned long arg) |
| 790 | { |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 791 | struct kvm *kvm = filp->private_data; |
| 792 | void __user *argp = (void __user *)arg; |
| 793 | |
| 794 | switch (ioctl) { |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 795 | case KVM_CREATE_IRQCHIP: { |
| 796 | if (vgic_present) |
| 797 | return kvm_vgic_create(kvm); |
| 798 | else |
| 799 | return -ENXIO; |
| 800 | } |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 801 | case KVM_ARM_SET_DEVICE_ADDR: { |
| 802 | struct kvm_arm_device_addr dev_addr; |
| 803 | |
| 804 | if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) |
| 805 | return -EFAULT; |
| 806 | return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); |
| 807 | } |
| 808 | default: |
| 809 | return -EINVAL; |
| 810 | } |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 811 | } |
| 812 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 813 | static void cpu_init_hyp_mode(void *vector) |
| 814 | { |
| 815 | unsigned long long pgd_ptr; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 816 | unsigned long hyp_stack_ptr; |
| 817 | unsigned long stack_page; |
| 818 | unsigned long vector_ptr; |
| 819 | |
| 820 | /* Switch from the HYP stub to our own HYP init vector */ |
| 821 | __hyp_set_vectors((unsigned long)vector); |
| 822 | |
| 823 | pgd_ptr = (unsigned long long)kvm_mmu_get_httbr(); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 824 | stack_page = __get_cpu_var(kvm_arm_hyp_stack_page); |
| 825 | hyp_stack_ptr = stack_page + PAGE_SIZE; |
| 826 | vector_ptr = (unsigned long)__kvm_hyp_vector; |
| 827 | |
Marc Zyngier | e7858c5 | 2012-10-05 15:10:44 +0100 | [diff] [blame^] | 828 | __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Inits Hyp-mode on all online CPUs |
| 833 | */ |
| 834 | static int init_hyp_mode(void) |
| 835 | { |
| 836 | phys_addr_t init_phys_addr; |
| 837 | int cpu; |
| 838 | int err = 0; |
| 839 | |
| 840 | /* |
| 841 | * Allocate Hyp PGD and setup Hyp identity mapping |
| 842 | */ |
| 843 | err = kvm_mmu_init(); |
| 844 | if (err) |
| 845 | goto out_err; |
| 846 | |
| 847 | /* |
| 848 | * It is probably enough to obtain the default on one |
| 849 | * CPU. It's unlikely to be different on the others. |
| 850 | */ |
| 851 | hyp_default_vectors = __hyp_get_vectors(); |
| 852 | |
| 853 | /* |
| 854 | * Allocate stack pages for Hypervisor-mode |
| 855 | */ |
| 856 | for_each_possible_cpu(cpu) { |
| 857 | unsigned long stack_page; |
| 858 | |
| 859 | stack_page = __get_free_page(GFP_KERNEL); |
| 860 | if (!stack_page) { |
| 861 | err = -ENOMEM; |
| 862 | goto out_free_stack_pages; |
| 863 | } |
| 864 | |
| 865 | per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * Execute the init code on each CPU. |
| 870 | * |
| 871 | * Note: The stack is not mapped yet, so don't do anything else than |
| 872 | * initializing the hypervisor mode on each CPU using a local stack |
| 873 | * space for temporary storage. |
| 874 | */ |
| 875 | init_phys_addr = virt_to_phys(__kvm_hyp_init); |
| 876 | for_each_online_cpu(cpu) { |
| 877 | smp_call_function_single(cpu, cpu_init_hyp_mode, |
| 878 | (void *)(long)init_phys_addr, 1); |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Unmap the identity mapping |
| 883 | */ |
| 884 | kvm_clear_hyp_idmap(); |
| 885 | |
| 886 | /* |
| 887 | * Map the Hyp-code called directly from the host |
| 888 | */ |
| 889 | err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end); |
| 890 | if (err) { |
| 891 | kvm_err("Cannot map world-switch code\n"); |
| 892 | goto out_free_mappings; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Map the Hyp stack pages |
| 897 | */ |
| 898 | for_each_possible_cpu(cpu) { |
| 899 | char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); |
| 900 | err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE); |
| 901 | |
| 902 | if (err) { |
| 903 | kvm_err("Cannot map hyp stack\n"); |
| 904 | goto out_free_mappings; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Map the host VFP structures |
| 910 | */ |
| 911 | kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct); |
| 912 | if (!kvm_host_vfp_state) { |
| 913 | err = -ENOMEM; |
| 914 | kvm_err("Cannot allocate host VFP state\n"); |
| 915 | goto out_free_mappings; |
| 916 | } |
| 917 | |
| 918 | for_each_possible_cpu(cpu) { |
| 919 | struct vfp_hard_struct *vfp; |
| 920 | |
| 921 | vfp = per_cpu_ptr(kvm_host_vfp_state, cpu); |
| 922 | err = create_hyp_mappings(vfp, vfp + 1); |
| 923 | |
| 924 | if (err) { |
| 925 | kvm_err("Cannot map host VFP state: %d\n", err); |
| 926 | goto out_free_vfp; |
| 927 | } |
| 928 | } |
| 929 | |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 930 | /* |
| 931 | * Init HYP view of VGIC |
| 932 | */ |
| 933 | err = kvm_vgic_hyp_init(); |
| 934 | if (err) |
| 935 | goto out_free_vfp; |
| 936 | |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 937 | #ifdef CONFIG_KVM_ARM_VGIC |
| 938 | vgic_present = true; |
| 939 | #endif |
| 940 | |
Marc Zyngier | 967f842 | 2013-01-23 13:21:59 -0500 | [diff] [blame] | 941 | /* |
| 942 | * Init HYP architected timer support |
| 943 | */ |
| 944 | err = kvm_timer_hyp_init(); |
| 945 | if (err) |
| 946 | goto out_free_mappings; |
| 947 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 948 | kvm_info("Hyp mode initialized successfully\n"); |
| 949 | return 0; |
| 950 | out_free_vfp: |
| 951 | free_percpu(kvm_host_vfp_state); |
| 952 | out_free_mappings: |
| 953 | free_hyp_pmds(); |
| 954 | out_free_stack_pages: |
| 955 | for_each_possible_cpu(cpu) |
| 956 | free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); |
| 957 | out_err: |
| 958 | kvm_err("error initializing Hyp mode: %d\n", err); |
| 959 | return err; |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * Initialize Hyp-mode and memory mappings on all CPUs. |
| 964 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 965 | int kvm_arch_init(void *opaque) |
| 966 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 967 | int err; |
| 968 | |
| 969 | if (!is_hyp_mode_available()) { |
| 970 | kvm_err("HYP mode not available\n"); |
| 971 | return -ENODEV; |
| 972 | } |
| 973 | |
| 974 | if (kvm_target_cpu() < 0) { |
| 975 | kvm_err("Target CPU not supported!\n"); |
| 976 | return -ENODEV; |
| 977 | } |
| 978 | |
| 979 | err = init_hyp_mode(); |
| 980 | if (err) |
| 981 | goto out_err; |
| 982 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 983 | kvm_coproc_table_init(); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 984 | return 0; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 985 | out_err: |
| 986 | return err; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* NOP: Compiling as a module not supported */ |
| 990 | void kvm_arch_exit(void) |
| 991 | { |
| 992 | } |
| 993 | |
| 994 | static int arm_init(void) |
| 995 | { |
| 996 | int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 997 | return rc; |
| 998 | } |
| 999 | |
| 1000 | module_init(arm_init); |