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 | |
| 33 | #include <asm/unified.h> |
| 34 | #include <asm/uaccess.h> |
| 35 | #include <asm/ptrace.h> |
| 36 | #include <asm/mman.h> |
| 37 | #include <asm/cputype.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 38 | #include <asm/tlbflush.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 39 | #include <asm/cacheflush.h> |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 40 | #include <asm/virt.h> |
| 41 | #include <asm/kvm_arm.h> |
| 42 | #include <asm/kvm_asm.h> |
| 43 | #include <asm/kvm_mmu.h> |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 44 | #include <asm/kvm_emulate.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 45 | #include <asm/kvm_coproc.h> |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 46 | #include <asm/kvm_psci.h> |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 47 | #include <asm/opcodes.h> |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 48 | |
| 49 | #ifdef REQUIRES_VIRT |
| 50 | __asm__(".arch_extension virt"); |
| 51 | #endif |
| 52 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 53 | static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); |
| 54 | static struct vfp_hard_struct __percpu *kvm_host_vfp_state; |
| 55 | static unsigned long hyp_default_vectors; |
| 56 | |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame^] | 57 | /* Per-CPU variable containing the currently running vcpu. */ |
| 58 | static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu); |
| 59 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 60 | /* The VMID used in the VTTBR */ |
| 61 | static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); |
| 62 | static u8 kvm_next_vmid; |
| 63 | static DEFINE_SPINLOCK(kvm_vmid_lock); |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 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) { |
| 187 | case KVM_CAP_USER_MEMORY: |
| 188 | case KVM_CAP_SYNC_MMU: |
| 189 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
| 190 | case KVM_CAP_ONE_REG: |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 191 | case KVM_CAP_ARM_PSCI: |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 192 | r = 1; |
| 193 | break; |
| 194 | case KVM_CAP_COALESCED_MMIO: |
| 195 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 196 | break; |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 197 | case KVM_CAP_ARM_SET_DEVICE_ADDR: |
| 198 | r = 1; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 199 | case KVM_CAP_NR_VCPUS: |
| 200 | r = num_online_cpus(); |
| 201 | break; |
| 202 | case KVM_CAP_MAX_VCPUS: |
| 203 | r = KVM_MAX_VCPUS; |
| 204 | break; |
| 205 | default: |
| 206 | r = 0; |
| 207 | break; |
| 208 | } |
| 209 | return r; |
| 210 | } |
| 211 | |
| 212 | long kvm_arch_dev_ioctl(struct file *filp, |
| 213 | unsigned int ioctl, unsigned long arg) |
| 214 | { |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | int kvm_arch_set_memory_region(struct kvm *kvm, |
| 219 | struct kvm_userspace_memory_region *mem, |
| 220 | struct kvm_memory_slot old, |
| 221 | int user_alloc) |
| 222 | { |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 227 | struct kvm_memory_slot *memslot, |
| 228 | struct kvm_memory_slot old, |
| 229 | struct kvm_userspace_memory_region *mem, |
| 230 | int user_alloc) |
| 231 | { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 236 | struct kvm_userspace_memory_region *mem, |
| 237 | struct kvm_memory_slot old, |
| 238 | int user_alloc) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 243 | { |
| 244 | } |
| 245 | |
| 246 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 247 | struct kvm_memory_slot *slot) |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 252 | { |
| 253 | int err; |
| 254 | struct kvm_vcpu *vcpu; |
| 255 | |
| 256 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
| 257 | if (!vcpu) { |
| 258 | err = -ENOMEM; |
| 259 | goto out; |
| 260 | } |
| 261 | |
| 262 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 263 | if (err) |
| 264 | goto free_vcpu; |
| 265 | |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 266 | err = create_hyp_mappings(vcpu, vcpu + 1); |
| 267 | if (err) |
| 268 | goto vcpu_uninit; |
| 269 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 270 | return vcpu; |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 271 | vcpu_uninit: |
| 272 | kvm_vcpu_uninit(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 273 | free_vcpu: |
| 274 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
| 275 | out: |
| 276 | return ERR_PTR(err); |
| 277 | } |
| 278 | |
| 279 | int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 280 | { |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 285 | { |
Christoffer Dall | d5d8184 | 2013-01-20 18:28:07 -0500 | [diff] [blame] | 286 | kvm_mmu_free_memory_caches(vcpu); |
| 287 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 291 | { |
| 292 | kvm_arch_vcpu_free(vcpu); |
| 293 | } |
| 294 | |
| 295 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 296 | { |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int __attribute_const__ kvm_target_cpu(void) |
| 301 | { |
| 302 | unsigned long implementor = read_cpuid_implementor(); |
| 303 | unsigned long part_number = read_cpuid_part_number(); |
| 304 | |
| 305 | if (implementor != ARM_CPU_IMP_ARM) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | switch (part_number) { |
| 309 | case ARM_CPU_PART_CORTEX_A15: |
| 310 | return KVM_ARM_TARGET_CORTEX_A15; |
| 311 | default: |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 317 | { |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 318 | /* Force users to call KVM_ARM_VCPU_INIT */ |
| 319 | vcpu->arch.target = -1; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 324 | { |
| 325 | } |
| 326 | |
| 327 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 328 | { |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 329 | vcpu->cpu = cpu; |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 330 | vcpu->arch.vfp_host = this_cpu_ptr(kvm_host_vfp_state); |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * Check whether this vcpu requires the cache to be flushed on |
| 334 | * this physical CPU. This is a consequence of doing dcache |
| 335 | * operations by set/way on this vcpu. We do it here to be in |
| 336 | * a non-preemptible section. |
| 337 | */ |
| 338 | if (cpumask_test_and_clear_cpu(cpu, &vcpu->arch.require_dcache_flush)) |
| 339 | flush_cache_all(); /* We'd really want v7_flush_dcache_all() */ |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame^] | 340 | |
| 341 | kvm_arm_set_running_vcpu(vcpu); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 345 | { |
Marc Zyngier | 1638a12 | 2013-01-21 19:36:11 -0500 | [diff] [blame^] | 346 | kvm_arm_set_running_vcpu(NULL); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 350 | struct kvm_guest_debug *dbg) |
| 351 | { |
| 352 | return -EINVAL; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 357 | struct kvm_mp_state *mp_state) |
| 358 | { |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
| 362 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 363 | struct kvm_mp_state *mp_state) |
| 364 | { |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 368 | /** |
| 369 | * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled |
| 370 | * @v: The VCPU pointer |
| 371 | * |
| 372 | * If the guest CPU is not waiting for interrupts or an interrupt line is |
| 373 | * asserted, the CPU is by definition runnable. |
| 374 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 375 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 376 | { |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 377 | return !!v->arch.irq_lines; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 380 | /* Just ensure a guest exit from a particular CPU */ |
| 381 | static void exit_vm_noop(void *info) |
| 382 | { |
| 383 | } |
| 384 | |
| 385 | void force_vm_exit(const cpumask_t *mask) |
| 386 | { |
| 387 | smp_call_function_many(mask, exit_vm_noop, NULL, true); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * need_new_vmid_gen - check that the VMID is still valid |
| 392 | * @kvm: The VM's VMID to checkt |
| 393 | * |
| 394 | * return true if there is a new generation of VMIDs being used |
| 395 | * |
| 396 | * The hardware supports only 256 values with the value zero reserved for the |
| 397 | * host, so we check if an assigned value belongs to a previous generation, |
| 398 | * which which requires us to assign a new value. If we're the first to use a |
| 399 | * VMID for the new generation, we must flush necessary caches and TLBs on all |
| 400 | * CPUs. |
| 401 | */ |
| 402 | static bool need_new_vmid_gen(struct kvm *kvm) |
| 403 | { |
| 404 | return unlikely(kvm->arch.vmid_gen != atomic64_read(&kvm_vmid_gen)); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * update_vttbr - Update the VTTBR with a valid VMID before the guest runs |
| 409 | * @kvm The guest that we are about to run |
| 410 | * |
| 411 | * Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the |
| 412 | * VM has a valid VMID, otherwise assigns a new one and flushes corresponding |
| 413 | * caches and TLBs. |
| 414 | */ |
| 415 | static void update_vttbr(struct kvm *kvm) |
| 416 | { |
| 417 | phys_addr_t pgd_phys; |
| 418 | u64 vmid; |
| 419 | |
| 420 | if (!need_new_vmid_gen(kvm)) |
| 421 | return; |
| 422 | |
| 423 | spin_lock(&kvm_vmid_lock); |
| 424 | |
| 425 | /* |
| 426 | * We need to re-check the vmid_gen here to ensure that if another vcpu |
| 427 | * already allocated a valid vmid for this vm, then this vcpu should |
| 428 | * use the same vmid. |
| 429 | */ |
| 430 | if (!need_new_vmid_gen(kvm)) { |
| 431 | spin_unlock(&kvm_vmid_lock); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | /* First user of a new VMID generation? */ |
| 436 | if (unlikely(kvm_next_vmid == 0)) { |
| 437 | atomic64_inc(&kvm_vmid_gen); |
| 438 | kvm_next_vmid = 1; |
| 439 | |
| 440 | /* |
| 441 | * On SMP we know no other CPUs can use this CPU's or each |
| 442 | * other's VMID after force_vm_exit returns since the |
| 443 | * kvm_vmid_lock blocks them from reentry to the guest. |
| 444 | */ |
| 445 | force_vm_exit(cpu_all_mask); |
| 446 | /* |
| 447 | * Now broadcast TLB + ICACHE invalidation over the inner |
| 448 | * shareable domain to make sure all data structures are |
| 449 | * clean. |
| 450 | */ |
| 451 | kvm_call_hyp(__kvm_flush_vm_context); |
| 452 | } |
| 453 | |
| 454 | kvm->arch.vmid_gen = atomic64_read(&kvm_vmid_gen); |
| 455 | kvm->arch.vmid = kvm_next_vmid; |
| 456 | kvm_next_vmid++; |
| 457 | |
| 458 | /* update vttbr to be used with the new vmid */ |
| 459 | pgd_phys = virt_to_phys(kvm->arch.pgd); |
| 460 | vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK; |
| 461 | kvm->arch.vttbr = pgd_phys & VTTBR_BADDR_MASK; |
| 462 | kvm->arch.vttbr |= vmid; |
| 463 | |
| 464 | spin_unlock(&kvm_vmid_lock); |
| 465 | } |
| 466 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 467 | static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 468 | { |
| 469 | /* SVC called from Hyp mode should never get here */ |
| 470 | kvm_debug("SVC called from Hyp mode shouldn't go here\n"); |
| 471 | BUG(); |
| 472 | return -EINVAL; /* Squash warning */ |
| 473 | } |
| 474 | |
| 475 | static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 476 | { |
| 477 | trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), |
| 478 | vcpu->arch.hsr & HSR_HVC_IMM_MASK); |
| 479 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 480 | if (kvm_psci_call(vcpu)) |
| 481 | return 1; |
| 482 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 483 | kvm_inject_undefined(vcpu); |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 488 | { |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 489 | if (kvm_psci_call(vcpu)) |
| 490 | return 1; |
| 491 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 492 | kvm_inject_undefined(vcpu); |
| 493 | return 1; |
| 494 | } |
| 495 | |
| 496 | static int handle_pabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 497 | { |
| 498 | /* The hypervisor should never cause aborts */ |
| 499 | kvm_err("Prefetch Abort taken from Hyp mode at %#08x (HSR: %#08x)\n", |
| 500 | vcpu->arch.hxfar, vcpu->arch.hsr); |
| 501 | return -EFAULT; |
| 502 | } |
| 503 | |
| 504 | static int handle_dabt_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 505 | { |
| 506 | /* This is either an error in the ws. code or an external abort */ |
| 507 | kvm_err("Data Abort taken from Hyp mode at %#08x (HSR: %#08x)\n", |
| 508 | vcpu->arch.hxfar, vcpu->arch.hsr); |
| 509 | return -EFAULT; |
| 510 | } |
| 511 | |
| 512 | typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *); |
| 513 | static exit_handle_fn arm_exit_handlers[] = { |
| 514 | [HSR_EC_WFI] = kvm_handle_wfi, |
| 515 | [HSR_EC_CP15_32] = kvm_handle_cp15_32, |
| 516 | [HSR_EC_CP15_64] = kvm_handle_cp15_64, |
| 517 | [HSR_EC_CP14_MR] = kvm_handle_cp14_access, |
| 518 | [HSR_EC_CP14_LS] = kvm_handle_cp14_load_store, |
| 519 | [HSR_EC_CP14_64] = kvm_handle_cp14_access, |
| 520 | [HSR_EC_CP_0_13] = kvm_handle_cp_0_13_access, |
| 521 | [HSR_EC_CP10_ID] = kvm_handle_cp10_id, |
| 522 | [HSR_EC_SVC_HYP] = handle_svc_hyp, |
| 523 | [HSR_EC_HVC] = handle_hvc, |
| 524 | [HSR_EC_SMC] = handle_smc, |
| 525 | [HSR_EC_IABT] = kvm_handle_guest_abort, |
| 526 | [HSR_EC_IABT_HYP] = handle_pabt_hyp, |
| 527 | [HSR_EC_DABT] = kvm_handle_guest_abort, |
| 528 | [HSR_EC_DABT_HYP] = handle_dabt_hyp, |
| 529 | }; |
| 530 | |
| 531 | /* |
| 532 | * A conditional instruction is allowed to trap, even though it |
| 533 | * wouldn't be executed. So let's re-implement the hardware, in |
| 534 | * software! |
| 535 | */ |
| 536 | static bool kvm_condition_valid(struct kvm_vcpu *vcpu) |
| 537 | { |
| 538 | unsigned long cpsr, cond, insn; |
| 539 | |
| 540 | /* |
| 541 | * Exception Code 0 can only happen if we set HCR.TGE to 1, to |
| 542 | * catch undefined instructions, and then we won't get past |
| 543 | * the arm_exit_handlers test anyway. |
| 544 | */ |
| 545 | BUG_ON(((vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT) == 0); |
| 546 | |
| 547 | /* Top two bits non-zero? Unconditional. */ |
| 548 | if (vcpu->arch.hsr >> 30) |
| 549 | return true; |
| 550 | |
| 551 | cpsr = *vcpu_cpsr(vcpu); |
| 552 | |
| 553 | /* Is condition field valid? */ |
| 554 | if ((vcpu->arch.hsr & HSR_CV) >> HSR_CV_SHIFT) |
| 555 | cond = (vcpu->arch.hsr & HSR_COND) >> HSR_COND_SHIFT; |
| 556 | else { |
| 557 | /* This can happen in Thumb mode: examine IT state. */ |
| 558 | unsigned long it; |
| 559 | |
| 560 | it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3); |
| 561 | |
| 562 | /* it == 0 => unconditional. */ |
| 563 | if (it == 0) |
| 564 | return true; |
| 565 | |
| 566 | /* The cond for this insn works out as the top 4 bits. */ |
| 567 | cond = (it >> 4); |
| 568 | } |
| 569 | |
| 570 | /* Shift makes it look like an ARM-mode instruction */ |
| 571 | insn = cond << 28; |
| 572 | return arm_check_condition(insn, cpsr) != ARM_OPCODE_CONDTEST_FAIL; |
| 573 | } |
| 574 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 575 | /* |
| 576 | * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on |
| 577 | * proper exit to QEMU. |
| 578 | */ |
| 579 | static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, |
| 580 | int exception_index) |
| 581 | { |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 582 | unsigned long hsr_ec; |
| 583 | |
| 584 | switch (exception_index) { |
| 585 | case ARM_EXCEPTION_IRQ: |
| 586 | return 1; |
| 587 | case ARM_EXCEPTION_UNDEFINED: |
| 588 | kvm_err("Undefined exception in Hyp mode at: %#08x\n", |
| 589 | vcpu->arch.hyp_pc); |
| 590 | BUG(); |
| 591 | panic("KVM: Hypervisor undefined exception!\n"); |
| 592 | case ARM_EXCEPTION_DATA_ABORT: |
| 593 | case ARM_EXCEPTION_PREF_ABORT: |
| 594 | case ARM_EXCEPTION_HVC: |
| 595 | hsr_ec = (vcpu->arch.hsr & HSR_EC) >> HSR_EC_SHIFT; |
| 596 | |
| 597 | if (hsr_ec >= ARRAY_SIZE(arm_exit_handlers) |
| 598 | || !arm_exit_handlers[hsr_ec]) { |
| 599 | kvm_err("Unkown exception class: %#08lx, " |
| 600 | "hsr: %#08x\n", hsr_ec, |
| 601 | (unsigned int)vcpu->arch.hsr); |
| 602 | BUG(); |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * See ARM ARM B1.14.1: "Hyp traps on instructions |
| 607 | * that fail their condition code check" |
| 608 | */ |
| 609 | if (!kvm_condition_valid(vcpu)) { |
| 610 | bool is_wide = vcpu->arch.hsr & HSR_IL; |
| 611 | kvm_skip_instr(vcpu, is_wide); |
| 612 | return 1; |
| 613 | } |
| 614 | |
| 615 | return arm_exit_handlers[hsr_ec](vcpu, run); |
| 616 | default: |
| 617 | kvm_pr_unimpl("Unsupported exception type: %d", |
| 618 | exception_index); |
| 619 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 620 | return 0; |
| 621 | } |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) |
| 625 | { |
| 626 | if (likely(vcpu->arch.has_run_once)) |
| 627 | return 0; |
| 628 | |
| 629 | vcpu->arch.has_run_once = true; |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 630 | |
| 631 | /* |
| 632 | * Handle the "start in power-off" case by calling into the |
| 633 | * PSCI code. |
| 634 | */ |
| 635 | if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) { |
| 636 | *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF; |
| 637 | kvm_psci_call(vcpu); |
| 638 | } |
| 639 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 640 | return 0; |
| 641 | } |
| 642 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 643 | static void vcpu_pause(struct kvm_vcpu *vcpu) |
| 644 | { |
| 645 | wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu); |
| 646 | |
| 647 | wait_event_interruptible(*wq, !vcpu->arch.pause); |
| 648 | } |
| 649 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 650 | /** |
| 651 | * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code |
| 652 | * @vcpu: The VCPU pointer |
| 653 | * @run: The kvm_run structure pointer used for userspace state exchange |
| 654 | * |
| 655 | * This function is called through the VCPU_RUN ioctl called from user space. It |
| 656 | * will execute VM code in a loop until the time slice for the process is used |
| 657 | * or some emulation is needed from user space in which case the function will |
| 658 | * return with return value 0 and with the kvm_run structure filled in with the |
| 659 | * required data for the requested emulation. |
| 660 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 661 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 662 | { |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 663 | int ret; |
| 664 | sigset_t sigsaved; |
| 665 | |
| 666 | /* Make sure they initialize the vcpu with KVM_ARM_VCPU_INIT */ |
| 667 | if (unlikely(vcpu->arch.target < 0)) |
| 668 | return -ENOEXEC; |
| 669 | |
| 670 | ret = kvm_vcpu_first_run_init(vcpu); |
| 671 | if (ret) |
| 672 | return ret; |
| 673 | |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 674 | if (run->exit_reason == KVM_EXIT_MMIO) { |
| 675 | ret = kvm_handle_mmio_return(vcpu, vcpu->run); |
| 676 | if (ret) |
| 677 | return ret; |
| 678 | } |
| 679 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 680 | if (vcpu->sigset_active) |
| 681 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 682 | |
| 683 | ret = 1; |
| 684 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 685 | while (ret > 0) { |
| 686 | /* |
| 687 | * Check conditions before entering the guest |
| 688 | */ |
| 689 | cond_resched(); |
| 690 | |
| 691 | update_vttbr(vcpu->kvm); |
| 692 | |
Marc Zyngier | aa024c2 | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 693 | if (vcpu->arch.pause) |
| 694 | vcpu_pause(vcpu); |
| 695 | |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 696 | local_irq_disable(); |
| 697 | |
| 698 | /* |
| 699 | * Re-check atomic conditions |
| 700 | */ |
| 701 | if (signal_pending(current)) { |
| 702 | ret = -EINTR; |
| 703 | run->exit_reason = KVM_EXIT_INTR; |
| 704 | } |
| 705 | |
| 706 | if (ret <= 0 || need_new_vmid_gen(vcpu->kvm)) { |
| 707 | local_irq_enable(); |
| 708 | continue; |
| 709 | } |
| 710 | |
| 711 | /************************************************************** |
| 712 | * Enter the guest |
| 713 | */ |
| 714 | trace_kvm_entry(*vcpu_pc(vcpu)); |
| 715 | kvm_guest_enter(); |
| 716 | vcpu->mode = IN_GUEST_MODE; |
| 717 | |
| 718 | ret = kvm_call_hyp(__kvm_vcpu_run, vcpu); |
| 719 | |
| 720 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 721 | vcpu->arch.last_pcpu = smp_processor_id(); |
Christoffer Dall | f7ed45b | 2013-01-20 18:47:42 -0500 | [diff] [blame] | 722 | kvm_guest_exit(); |
| 723 | trace_kvm_exit(*vcpu_pc(vcpu)); |
| 724 | /* |
| 725 | * We may have taken a host interrupt in HYP mode (ie |
| 726 | * while executing the guest). This interrupt is still |
| 727 | * pending, as we haven't serviced it yet! |
| 728 | * |
| 729 | * We're now back in SVC mode, with interrupts |
| 730 | * disabled. Enabling the interrupts now will have |
| 731 | * the effect of taking the interrupt again, in SVC |
| 732 | * mode this time. |
| 733 | */ |
| 734 | local_irq_enable(); |
| 735 | |
| 736 | /* |
| 737 | * Back from guest |
| 738 | *************************************************************/ |
| 739 | |
| 740 | ret = handle_exit(vcpu, run, ret); |
| 741 | } |
| 742 | |
| 743 | if (vcpu->sigset_active) |
| 744 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 745 | return ret; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 746 | } |
| 747 | |
Christoffer Dall | 86ce853 | 2013-01-20 18:28:08 -0500 | [diff] [blame] | 748 | static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) |
| 749 | { |
| 750 | int bit_index; |
| 751 | bool set; |
| 752 | unsigned long *ptr; |
| 753 | |
| 754 | if (number == KVM_ARM_IRQ_CPU_IRQ) |
| 755 | bit_index = __ffs(HCR_VI); |
| 756 | else /* KVM_ARM_IRQ_CPU_FIQ */ |
| 757 | bit_index = __ffs(HCR_VF); |
| 758 | |
| 759 | ptr = (unsigned long *)&vcpu->arch.irq_lines; |
| 760 | if (level) |
| 761 | set = test_and_set_bit(bit_index, ptr); |
| 762 | else |
| 763 | set = test_and_clear_bit(bit_index, ptr); |
| 764 | |
| 765 | /* |
| 766 | * If we didn't change anything, no need to wake up or kick other CPUs |
| 767 | */ |
| 768 | if (set == level) |
| 769 | return 0; |
| 770 | |
| 771 | /* |
| 772 | * The vcpu irq_lines field was updated, wake up sleeping VCPUs and |
| 773 | * trigger a world-switch round on the running physical CPU to set the |
| 774 | * virtual IRQ/FIQ fields in the HCR appropriately. |
| 775 | */ |
| 776 | kvm_vcpu_kick(vcpu); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level) |
| 782 | { |
| 783 | u32 irq = irq_level->irq; |
| 784 | unsigned int irq_type, vcpu_idx, irq_num; |
| 785 | int nrcpus = atomic_read(&kvm->online_vcpus); |
| 786 | struct kvm_vcpu *vcpu = NULL; |
| 787 | bool level = irq_level->level; |
| 788 | |
| 789 | irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; |
| 790 | vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; |
| 791 | irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; |
| 792 | |
| 793 | trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); |
| 794 | |
| 795 | if (irq_type != KVM_ARM_IRQ_TYPE_CPU) |
| 796 | return -EINVAL; |
| 797 | |
| 798 | if (vcpu_idx >= nrcpus) |
| 799 | return -EINVAL; |
| 800 | |
| 801 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); |
| 802 | if (!vcpu) |
| 803 | return -EINVAL; |
| 804 | |
| 805 | if (irq_num > KVM_ARM_IRQ_CPU_FIQ) |
| 806 | return -EINVAL; |
| 807 | |
| 808 | return vcpu_interrupt_line(vcpu, irq_num, level); |
| 809 | } |
| 810 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 811 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 812 | unsigned int ioctl, unsigned long arg) |
| 813 | { |
| 814 | struct kvm_vcpu *vcpu = filp->private_data; |
| 815 | void __user *argp = (void __user *)arg; |
| 816 | |
| 817 | switch (ioctl) { |
| 818 | case KVM_ARM_VCPU_INIT: { |
| 819 | struct kvm_vcpu_init init; |
| 820 | |
| 821 | if (copy_from_user(&init, argp, sizeof(init))) |
| 822 | return -EFAULT; |
| 823 | |
| 824 | return kvm_vcpu_set_target(vcpu, &init); |
| 825 | |
| 826 | } |
| 827 | case KVM_SET_ONE_REG: |
| 828 | case KVM_GET_ONE_REG: { |
| 829 | struct kvm_one_reg reg; |
| 830 | if (copy_from_user(®, argp, sizeof(reg))) |
| 831 | return -EFAULT; |
| 832 | if (ioctl == KVM_SET_ONE_REG) |
| 833 | return kvm_arm_set_reg(vcpu, ®); |
| 834 | else |
| 835 | return kvm_arm_get_reg(vcpu, ®); |
| 836 | } |
| 837 | case KVM_GET_REG_LIST: { |
| 838 | struct kvm_reg_list __user *user_list = argp; |
| 839 | struct kvm_reg_list reg_list; |
| 840 | unsigned n; |
| 841 | |
| 842 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
| 843 | return -EFAULT; |
| 844 | n = reg_list.n; |
| 845 | reg_list.n = kvm_arm_num_regs(vcpu); |
| 846 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
| 847 | return -EFAULT; |
| 848 | if (n < reg_list.n) |
| 849 | return -E2BIG; |
| 850 | return kvm_arm_copy_reg_indices(vcpu, user_list->reg); |
| 851 | } |
| 852 | default: |
| 853 | return -EINVAL; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
| 858 | { |
| 859 | return -EINVAL; |
| 860 | } |
| 861 | |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 862 | static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, |
| 863 | struct kvm_arm_device_addr *dev_addr) |
| 864 | { |
| 865 | return -ENODEV; |
| 866 | } |
| 867 | |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 868 | long kvm_arch_vm_ioctl(struct file *filp, |
| 869 | unsigned int ioctl, unsigned long arg) |
| 870 | { |
Christoffer Dall | 3401d546 | 2013-01-23 13:18:04 -0500 | [diff] [blame] | 871 | struct kvm *kvm = filp->private_data; |
| 872 | void __user *argp = (void __user *)arg; |
| 873 | |
| 874 | switch (ioctl) { |
| 875 | case KVM_ARM_SET_DEVICE_ADDR: { |
| 876 | struct kvm_arm_device_addr dev_addr; |
| 877 | |
| 878 | if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) |
| 879 | return -EFAULT; |
| 880 | return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); |
| 881 | } |
| 882 | default: |
| 883 | return -EINVAL; |
| 884 | } |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 885 | } |
| 886 | |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 887 | static void cpu_init_hyp_mode(void *vector) |
| 888 | { |
| 889 | unsigned long long pgd_ptr; |
| 890 | unsigned long pgd_low, pgd_high; |
| 891 | unsigned long hyp_stack_ptr; |
| 892 | unsigned long stack_page; |
| 893 | unsigned long vector_ptr; |
| 894 | |
| 895 | /* Switch from the HYP stub to our own HYP init vector */ |
| 896 | __hyp_set_vectors((unsigned long)vector); |
| 897 | |
| 898 | pgd_ptr = (unsigned long long)kvm_mmu_get_httbr(); |
| 899 | pgd_low = (pgd_ptr & ((1ULL << 32) - 1)); |
| 900 | pgd_high = (pgd_ptr >> 32ULL); |
| 901 | stack_page = __get_cpu_var(kvm_arm_hyp_stack_page); |
| 902 | hyp_stack_ptr = stack_page + PAGE_SIZE; |
| 903 | vector_ptr = (unsigned long)__kvm_hyp_vector; |
| 904 | |
| 905 | /* |
| 906 | * Call initialization code, and switch to the full blown |
| 907 | * HYP code. The init code doesn't need to preserve these registers as |
| 908 | * r1-r3 and r12 are already callee save according to the AAPCS. |
| 909 | * Note that we slightly misuse the prototype by casing the pgd_low to |
| 910 | * a void *. |
| 911 | */ |
| 912 | kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr); |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Inits Hyp-mode on all online CPUs |
| 917 | */ |
| 918 | static int init_hyp_mode(void) |
| 919 | { |
| 920 | phys_addr_t init_phys_addr; |
| 921 | int cpu; |
| 922 | int err = 0; |
| 923 | |
| 924 | /* |
| 925 | * Allocate Hyp PGD and setup Hyp identity mapping |
| 926 | */ |
| 927 | err = kvm_mmu_init(); |
| 928 | if (err) |
| 929 | goto out_err; |
| 930 | |
| 931 | /* |
| 932 | * It is probably enough to obtain the default on one |
| 933 | * CPU. It's unlikely to be different on the others. |
| 934 | */ |
| 935 | hyp_default_vectors = __hyp_get_vectors(); |
| 936 | |
| 937 | /* |
| 938 | * Allocate stack pages for Hypervisor-mode |
| 939 | */ |
| 940 | for_each_possible_cpu(cpu) { |
| 941 | unsigned long stack_page; |
| 942 | |
| 943 | stack_page = __get_free_page(GFP_KERNEL); |
| 944 | if (!stack_page) { |
| 945 | err = -ENOMEM; |
| 946 | goto out_free_stack_pages; |
| 947 | } |
| 948 | |
| 949 | per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * Execute the init code on each CPU. |
| 954 | * |
| 955 | * Note: The stack is not mapped yet, so don't do anything else than |
| 956 | * initializing the hypervisor mode on each CPU using a local stack |
| 957 | * space for temporary storage. |
| 958 | */ |
| 959 | init_phys_addr = virt_to_phys(__kvm_hyp_init); |
| 960 | for_each_online_cpu(cpu) { |
| 961 | smp_call_function_single(cpu, cpu_init_hyp_mode, |
| 962 | (void *)(long)init_phys_addr, 1); |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Unmap the identity mapping |
| 967 | */ |
| 968 | kvm_clear_hyp_idmap(); |
| 969 | |
| 970 | /* |
| 971 | * Map the Hyp-code called directly from the host |
| 972 | */ |
| 973 | err = create_hyp_mappings(__kvm_hyp_code_start, __kvm_hyp_code_end); |
| 974 | if (err) { |
| 975 | kvm_err("Cannot map world-switch code\n"); |
| 976 | goto out_free_mappings; |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Map the Hyp stack pages |
| 981 | */ |
| 982 | for_each_possible_cpu(cpu) { |
| 983 | char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); |
| 984 | err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE); |
| 985 | |
| 986 | if (err) { |
| 987 | kvm_err("Cannot map hyp stack\n"); |
| 988 | goto out_free_mappings; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * Map the host VFP structures |
| 994 | */ |
| 995 | kvm_host_vfp_state = alloc_percpu(struct vfp_hard_struct); |
| 996 | if (!kvm_host_vfp_state) { |
| 997 | err = -ENOMEM; |
| 998 | kvm_err("Cannot allocate host VFP state\n"); |
| 999 | goto out_free_mappings; |
| 1000 | } |
| 1001 | |
| 1002 | for_each_possible_cpu(cpu) { |
| 1003 | struct vfp_hard_struct *vfp; |
| 1004 | |
| 1005 | vfp = per_cpu_ptr(kvm_host_vfp_state, cpu); |
| 1006 | err = create_hyp_mappings(vfp, vfp + 1); |
| 1007 | |
| 1008 | if (err) { |
| 1009 | kvm_err("Cannot map host VFP state: %d\n", err); |
| 1010 | goto out_free_vfp; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | kvm_info("Hyp mode initialized successfully\n"); |
| 1015 | return 0; |
| 1016 | out_free_vfp: |
| 1017 | free_percpu(kvm_host_vfp_state); |
| 1018 | out_free_mappings: |
| 1019 | free_hyp_pmds(); |
| 1020 | out_free_stack_pages: |
| 1021 | for_each_possible_cpu(cpu) |
| 1022 | free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); |
| 1023 | out_err: |
| 1024 | kvm_err("error initializing Hyp mode: %d\n", err); |
| 1025 | return err; |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * Initialize Hyp-mode and memory mappings on all CPUs. |
| 1030 | */ |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1031 | int kvm_arch_init(void *opaque) |
| 1032 | { |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1033 | int err; |
| 1034 | |
| 1035 | if (!is_hyp_mode_available()) { |
| 1036 | kvm_err("HYP mode not available\n"); |
| 1037 | return -ENODEV; |
| 1038 | } |
| 1039 | |
| 1040 | if (kvm_target_cpu() < 0) { |
| 1041 | kvm_err("Target CPU not supported!\n"); |
| 1042 | return -ENODEV; |
| 1043 | } |
| 1044 | |
| 1045 | err = init_hyp_mode(); |
| 1046 | if (err) |
| 1047 | goto out_err; |
| 1048 | |
Christoffer Dall | 5b3e5e5 | 2013-01-20 18:28:09 -0500 | [diff] [blame] | 1049 | kvm_coproc_table_init(); |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1050 | return 0; |
Christoffer Dall | 342cd0a | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1051 | out_err: |
| 1052 | return err; |
Christoffer Dall | 749cf76c | 2013-01-20 18:28:06 -0500 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* NOP: Compiling as a module not supported */ |
| 1056 | void kvm_arch_exit(void) |
| 1057 | { |
| 1058 | } |
| 1059 | |
| 1060 | static int arm_init(void) |
| 1061 | { |
| 1062 | int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 1063 | return rc; |
| 1064 | } |
| 1065 | |
| 1066 | module_init(arm_init); |