Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * KVM/MIPS: MIPS specific KVM APIs |
| 7 | * |
| 8 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 9 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 10 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/bootmem.h> |
| 18 | #include <asm/page.h> |
| 19 | #include <asm/cacheflush.h> |
| 20 | #include <asm/mmu_context.h> |
| 21 | |
| 22 | #include <linux/kvm_host.h> |
| 23 | |
| 24 | #include "kvm_mips_int.h" |
| 25 | #include "kvm_mips_comm.h" |
| 26 | |
| 27 | #define CREATE_TRACE_POINTS |
| 28 | #include "trace.h" |
| 29 | |
| 30 | #ifndef VECTORSPACING |
| 31 | #define VECTORSPACING 0x100 /* for EI/VI mode */ |
| 32 | #endif |
| 33 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 34 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 35 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 36 | { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU }, |
| 37 | { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU }, |
| 38 | { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU }, |
| 39 | { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU }, |
| 40 | { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU }, |
| 41 | { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU }, |
| 42 | { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU }, |
| 43 | { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU }, |
| 44 | { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU }, |
| 45 | { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU }, |
| 46 | { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU }, |
| 47 | { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU }, |
| 48 | { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU }, |
| 49 | { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU }, |
| 50 | { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU }, |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 51 | {NULL} |
| 52 | }; |
| 53 | |
| 54 | static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu) |
| 55 | { |
| 56 | int i; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 57 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 58 | for_each_possible_cpu(i) { |
| 59 | vcpu->arch.guest_kernel_asid[i] = 0; |
| 60 | vcpu->arch.guest_user_asid[i] = 0; |
| 61 | } |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 62 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 66 | /* |
| 67 | * XXXKYMA: We are simulatoring a processor that has the WII bit set in |
| 68 | * Config7, so we are "runnable" if interrupts are pending |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 69 | */ |
| 70 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 71 | { |
| 72 | return !!(vcpu->arch.pending_exceptions); |
| 73 | } |
| 74 | |
| 75 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 76 | { |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | int kvm_arch_hardware_enable(void *garbage) |
| 81 | { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | void kvm_arch_hardware_disable(void *garbage) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | int kvm_arch_hardware_setup(void) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | void kvm_arch_hardware_unsetup(void) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void kvm_arch_check_processor_compat(void *rtn) |
| 99 | { |
| 100 | int *r = (int *)rtn; |
| 101 | *r = 0; |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | static void kvm_mips_init_tlbs(struct kvm *kvm) |
| 106 | { |
| 107 | unsigned long wired; |
| 108 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Add a wired entry to the TLB, it is used to map the commpage to |
| 111 | * the Guest kernel |
| 112 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 113 | wired = read_c0_wired(); |
| 114 | write_c0_wired(wired + 1); |
| 115 | mtc0_tlbw_hazard(); |
| 116 | kvm->arch.commpage_tlb = wired; |
| 117 | |
| 118 | kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(), |
| 119 | kvm->arch.commpage_tlb); |
| 120 | } |
| 121 | |
| 122 | static void kvm_mips_init_vm_percpu(void *arg) |
| 123 | { |
| 124 | struct kvm *kvm = (struct kvm *)arg; |
| 125 | |
| 126 | kvm_mips_init_tlbs(kvm); |
| 127 | kvm_mips_callbacks->vm_init(kvm); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 132 | { |
| 133 | if (atomic_inc_return(&kvm_mips_instance) == 1) { |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 134 | kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n", |
| 135 | __func__); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 136 | on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1); |
| 137 | } |
| 138 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | void kvm_mips_free_vcpus(struct kvm *kvm) |
| 143 | { |
| 144 | unsigned int i; |
| 145 | struct kvm_vcpu *vcpu; |
| 146 | |
| 147 | /* Put the pages we reserved for the guest pmap */ |
| 148 | for (i = 0; i < kvm->arch.guest_pmap_npages; i++) { |
| 149 | if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE) |
| 150 | kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]); |
| 151 | } |
James Hogan | c6c0a66 | 2014-05-29 10:16:44 +0100 | [diff] [blame] | 152 | kfree(kvm->arch.guest_pmap); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 153 | |
| 154 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 155 | kvm_arch_vcpu_free(vcpu); |
| 156 | } |
| 157 | |
| 158 | mutex_lock(&kvm->lock); |
| 159 | |
| 160 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 161 | kvm->vcpus[i] = NULL; |
| 162 | |
| 163 | atomic_set(&kvm->online_vcpus, 0); |
| 164 | |
| 165 | mutex_unlock(&kvm->lock); |
| 166 | } |
| 167 | |
| 168 | void kvm_arch_sync_events(struct kvm *kvm) |
| 169 | { |
| 170 | } |
| 171 | |
| 172 | static void kvm_mips_uninit_tlbs(void *arg) |
| 173 | { |
| 174 | /* Restore wired count */ |
| 175 | write_c0_wired(0); |
| 176 | mtc0_tlbw_hazard(); |
| 177 | /* Clear out all the TLBs */ |
| 178 | kvm_local_flush_tlb_all(); |
| 179 | } |
| 180 | |
| 181 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 182 | { |
| 183 | kvm_mips_free_vcpus(kvm); |
| 184 | |
| 185 | /* If this is the last instance, restore wired count */ |
| 186 | if (atomic_dec_return(&kvm_mips_instance) == 0) { |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 187 | kvm_debug("%s: last KVM instance, restoring TLB parameters\n", |
| 188 | __func__); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 189 | on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1); |
| 190 | } |
| 191 | } |
| 192 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 193 | long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, |
| 194 | unsigned long arg) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 195 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 196 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 199 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 200 | struct kvm_memory_slot *dont) |
| 201 | { |
| 202 | } |
| 203 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 204 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 205 | unsigned long npages) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 206 | { |
| 207 | return 0; |
| 208 | } |
| 209 | |
Takuya Yoshikawa | e59dbe0 | 2013-07-04 13:40:29 +0900 | [diff] [blame] | 210 | void kvm_arch_memslots_updated(struct kvm *kvm) |
| 211 | { |
| 212 | } |
| 213 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 214 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 215 | struct kvm_memory_slot *memslot, |
| 216 | struct kvm_userspace_memory_region *mem, |
| 217 | enum kvm_mr_change change) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 218 | { |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 223 | struct kvm_userspace_memory_region *mem, |
| 224 | const struct kvm_memory_slot *old, |
| 225 | enum kvm_mr_change change) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 226 | { |
| 227 | unsigned long npages = 0; |
| 228 | int i, err = 0; |
| 229 | |
| 230 | kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n", |
| 231 | __func__, kvm, mem->slot, mem->guest_phys_addr, |
| 232 | mem->memory_size, mem->userspace_addr); |
| 233 | |
| 234 | /* Setup Guest PMAP table */ |
| 235 | if (!kvm->arch.guest_pmap) { |
| 236 | if (mem->slot == 0) |
| 237 | npages = mem->memory_size >> PAGE_SHIFT; |
| 238 | |
| 239 | if (npages) { |
| 240 | kvm->arch.guest_pmap_npages = npages; |
| 241 | kvm->arch.guest_pmap = |
| 242 | kzalloc(npages * sizeof(unsigned long), GFP_KERNEL); |
| 243 | |
| 244 | if (!kvm->arch.guest_pmap) { |
| 245 | kvm_err("Failed to allocate guest PMAP"); |
| 246 | err = -ENOMEM; |
| 247 | goto out; |
| 248 | } |
| 249 | |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 250 | kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n", |
| 251 | npages, kvm->arch.guest_pmap); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 252 | |
| 253 | /* Now setup the page table */ |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 254 | for (i = 0; i < npages; i++) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 255 | kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | out: |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 263 | { |
| 264 | } |
| 265 | |
| 266 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 267 | struct kvm_memory_slot *slot) |
| 268 | { |
| 269 | } |
| 270 | |
| 271 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 272 | { |
| 273 | } |
| 274 | |
| 275 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 276 | { |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 277 | int err, size, offset; |
| 278 | void *gebase; |
| 279 | int i; |
| 280 | |
| 281 | struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL); |
| 282 | |
| 283 | if (!vcpu) { |
| 284 | err = -ENOMEM; |
| 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 289 | |
| 290 | if (err) |
| 291 | goto out_free_cpu; |
| 292 | |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 293 | kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 294 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 295 | /* |
| 296 | * Allocate space for host mode exception handlers that handle |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 297 | * guest mode exits |
| 298 | */ |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 299 | if (cpu_has_veic || cpu_has_vint) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 300 | size = 0x200 + VECTORSPACING * 64; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 301 | else |
James Hogan | 7006e2d | 2014-05-29 10:16:23 +0100 | [diff] [blame] | 302 | size = 0x4000; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 303 | |
| 304 | /* Save Linux EBASE */ |
| 305 | vcpu->arch.host_ebase = (void *)read_c0_ebase(); |
| 306 | |
| 307 | gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL); |
| 308 | |
| 309 | if (!gebase) { |
| 310 | err = -ENOMEM; |
| 311 | goto out_free_cpu; |
| 312 | } |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 313 | kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n", |
| 314 | ALIGN(size, PAGE_SIZE), gebase); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 315 | |
| 316 | /* Save new ebase */ |
| 317 | vcpu->arch.guest_ebase = gebase; |
| 318 | |
| 319 | /* Copy L1 Guest Exception handler to correct offset */ |
| 320 | |
| 321 | /* TLB Refill, EXL = 0 */ |
| 322 | memcpy(gebase, mips32_exception, |
| 323 | mips32_exceptionEnd - mips32_exception); |
| 324 | |
| 325 | /* General Exception Entry point */ |
| 326 | memcpy(gebase + 0x180, mips32_exception, |
| 327 | mips32_exceptionEnd - mips32_exception); |
| 328 | |
| 329 | /* For vectored interrupts poke the exception code @ all offsets 0-7 */ |
| 330 | for (i = 0; i < 8; i++) { |
| 331 | kvm_debug("L1 Vectored handler @ %p\n", |
| 332 | gebase + 0x200 + (i * VECTORSPACING)); |
| 333 | memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception, |
| 334 | mips32_exceptionEnd - mips32_exception); |
| 335 | } |
| 336 | |
| 337 | /* General handler, relocate to unmapped space for sanity's sake */ |
| 338 | offset = 0x2000; |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 339 | kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n", |
| 340 | gebase + offset, |
| 341 | mips32_GuestExceptionEnd - mips32_GuestException); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 342 | |
| 343 | memcpy(gebase + offset, mips32_GuestException, |
| 344 | mips32_GuestExceptionEnd - mips32_GuestException); |
| 345 | |
| 346 | /* Invalidate the icache for these ranges */ |
James Hogan | facaaec | 2014-05-29 10:16:25 +0100 | [diff] [blame] | 347 | local_flush_icache_range((unsigned long)gebase, |
| 348 | (unsigned long)gebase + ALIGN(size, PAGE_SIZE)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 349 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 350 | /* |
| 351 | * Allocate comm page for guest kernel, a TLB will be reserved for |
| 352 | * mapping GVA @ 0xFFFF8000 to this page |
| 353 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 354 | vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL); |
| 355 | |
| 356 | if (!vcpu->arch.kseg0_commpage) { |
| 357 | err = -ENOMEM; |
| 358 | goto out_free_gebase; |
| 359 | } |
| 360 | |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 361 | kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 362 | kvm_mips_commpage_init(vcpu); |
| 363 | |
| 364 | /* Init */ |
| 365 | vcpu->arch.last_sched_cpu = -1; |
| 366 | |
| 367 | /* Start off the timer */ |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 368 | kvm_mips_init_count(vcpu); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 369 | |
| 370 | return vcpu; |
| 371 | |
| 372 | out_free_gebase: |
| 373 | kfree(gebase); |
| 374 | |
| 375 | out_free_cpu: |
| 376 | kfree(vcpu); |
| 377 | |
| 378 | out: |
| 379 | return ERR_PTR(err); |
| 380 | } |
| 381 | |
| 382 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 383 | { |
| 384 | hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 385 | |
| 386 | kvm_vcpu_uninit(vcpu); |
| 387 | |
| 388 | kvm_mips_dump_stats(vcpu); |
| 389 | |
James Hogan | c6c0a66 | 2014-05-29 10:16:44 +0100 | [diff] [blame] | 390 | kfree(vcpu->arch.guest_ebase); |
| 391 | kfree(vcpu->arch.kseg0_commpage); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 395 | { |
| 396 | kvm_arch_vcpu_free(vcpu); |
| 397 | } |
| 398 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 399 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 400 | struct kvm_guest_debug *dbg) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 401 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 402 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 406 | { |
| 407 | int r = 0; |
| 408 | sigset_t sigsaved; |
| 409 | |
| 410 | if (vcpu->sigset_active) |
| 411 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 412 | |
| 413 | if (vcpu->mmio_needed) { |
| 414 | if (!vcpu->mmio_is_write) |
| 415 | kvm_mips_complete_mmio_load(vcpu, run); |
| 416 | vcpu->mmio_needed = 0; |
| 417 | } |
| 418 | |
James Hogan | 044f0f0 | 2014-05-29 10:16:32 +0100 | [diff] [blame] | 419 | local_irq_disable(); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 420 | /* Check if we have any exceptions/interrupts pending */ |
| 421 | kvm_mips_deliver_interrupts(vcpu, |
| 422 | kvm_read_c0_guest_cause(vcpu->arch.cop0)); |
| 423 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 424 | kvm_guest_enter(); |
| 425 | |
| 426 | r = __kvm_mips_vcpu_run(run, vcpu); |
| 427 | |
| 428 | kvm_guest_exit(); |
| 429 | local_irq_enable(); |
| 430 | |
| 431 | if (vcpu->sigset_active) |
| 432 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 433 | |
| 434 | return r; |
| 435 | } |
| 436 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 437 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 438 | struct kvm_mips_interrupt *irq) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 439 | { |
| 440 | int intr = (int)irq->irq; |
| 441 | struct kvm_vcpu *dvcpu = NULL; |
| 442 | |
| 443 | if (intr == 3 || intr == -3 || intr == 4 || intr == -4) |
| 444 | kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu, |
| 445 | (int)intr); |
| 446 | |
| 447 | if (irq->cpu == -1) |
| 448 | dvcpu = vcpu; |
| 449 | else |
| 450 | dvcpu = vcpu->kvm->vcpus[irq->cpu]; |
| 451 | |
| 452 | if (intr == 2 || intr == 3 || intr == 4) { |
| 453 | kvm_mips_callbacks->queue_io_int(dvcpu, irq); |
| 454 | |
| 455 | } else if (intr == -2 || intr == -3 || intr == -4) { |
| 456 | kvm_mips_callbacks->dequeue_io_int(dvcpu, irq); |
| 457 | } else { |
| 458 | kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__, |
| 459 | irq->cpu, irq->irq); |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | |
| 463 | dvcpu->arch.wait = 0; |
| 464 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 465 | if (waitqueue_active(&dvcpu->wq)) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 466 | wake_up_interruptible(&dvcpu->wq); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 471 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 472 | struct kvm_mp_state *mp_state) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 473 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 474 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 477 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 478 | struct kvm_mp_state *mp_state) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 479 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 480 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 481 | } |
| 482 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 483 | static u64 kvm_mips_get_one_regs[] = { |
| 484 | KVM_REG_MIPS_R0, |
| 485 | KVM_REG_MIPS_R1, |
| 486 | KVM_REG_MIPS_R2, |
| 487 | KVM_REG_MIPS_R3, |
| 488 | KVM_REG_MIPS_R4, |
| 489 | KVM_REG_MIPS_R5, |
| 490 | KVM_REG_MIPS_R6, |
| 491 | KVM_REG_MIPS_R7, |
| 492 | KVM_REG_MIPS_R8, |
| 493 | KVM_REG_MIPS_R9, |
| 494 | KVM_REG_MIPS_R10, |
| 495 | KVM_REG_MIPS_R11, |
| 496 | KVM_REG_MIPS_R12, |
| 497 | KVM_REG_MIPS_R13, |
| 498 | KVM_REG_MIPS_R14, |
| 499 | KVM_REG_MIPS_R15, |
| 500 | KVM_REG_MIPS_R16, |
| 501 | KVM_REG_MIPS_R17, |
| 502 | KVM_REG_MIPS_R18, |
| 503 | KVM_REG_MIPS_R19, |
| 504 | KVM_REG_MIPS_R20, |
| 505 | KVM_REG_MIPS_R21, |
| 506 | KVM_REG_MIPS_R22, |
| 507 | KVM_REG_MIPS_R23, |
| 508 | KVM_REG_MIPS_R24, |
| 509 | KVM_REG_MIPS_R25, |
| 510 | KVM_REG_MIPS_R26, |
| 511 | KVM_REG_MIPS_R27, |
| 512 | KVM_REG_MIPS_R28, |
| 513 | KVM_REG_MIPS_R29, |
| 514 | KVM_REG_MIPS_R30, |
| 515 | KVM_REG_MIPS_R31, |
| 516 | |
| 517 | KVM_REG_MIPS_HI, |
| 518 | KVM_REG_MIPS_LO, |
| 519 | KVM_REG_MIPS_PC, |
| 520 | |
| 521 | KVM_REG_MIPS_CP0_INDEX, |
| 522 | KVM_REG_MIPS_CP0_CONTEXT, |
James Hogan | 7767b7d | 2014-05-29 10:16:30 +0100 | [diff] [blame] | 523 | KVM_REG_MIPS_CP0_USERLOCAL, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 524 | KVM_REG_MIPS_CP0_PAGEMASK, |
| 525 | KVM_REG_MIPS_CP0_WIRED, |
James Hogan | 16fd5c1 | 2014-05-29 10:16:31 +0100 | [diff] [blame] | 526 | KVM_REG_MIPS_CP0_HWRENA, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 527 | KVM_REG_MIPS_CP0_BADVADDR, |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 528 | KVM_REG_MIPS_CP0_COUNT, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 529 | KVM_REG_MIPS_CP0_ENTRYHI, |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 530 | KVM_REG_MIPS_CP0_COMPARE, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 531 | KVM_REG_MIPS_CP0_STATUS, |
| 532 | KVM_REG_MIPS_CP0_CAUSE, |
James Hogan | fb6df0c | 2014-05-29 10:16:27 +0100 | [diff] [blame] | 533 | KVM_REG_MIPS_CP0_EPC, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 534 | KVM_REG_MIPS_CP0_CONFIG, |
| 535 | KVM_REG_MIPS_CP0_CONFIG1, |
| 536 | KVM_REG_MIPS_CP0_CONFIG2, |
| 537 | KVM_REG_MIPS_CP0_CONFIG3, |
| 538 | KVM_REG_MIPS_CP0_CONFIG7, |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 539 | KVM_REG_MIPS_CP0_ERROREPC, |
| 540 | |
| 541 | KVM_REG_MIPS_COUNT_CTL, |
| 542 | KVM_REG_MIPS_COUNT_RESUME, |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 543 | KVM_REG_MIPS_COUNT_HZ, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | static int kvm_mips_get_reg(struct kvm_vcpu *vcpu, |
| 547 | const struct kvm_one_reg *reg) |
| 548 | { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 549 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 550 | int ret; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 551 | s64 v; |
| 552 | |
| 553 | switch (reg->id) { |
| 554 | case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31: |
| 555 | v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0]; |
| 556 | break; |
| 557 | case KVM_REG_MIPS_HI: |
| 558 | v = (long)vcpu->arch.hi; |
| 559 | break; |
| 560 | case KVM_REG_MIPS_LO: |
| 561 | v = (long)vcpu->arch.lo; |
| 562 | break; |
| 563 | case KVM_REG_MIPS_PC: |
| 564 | v = (long)vcpu->arch.pc; |
| 565 | break; |
| 566 | |
| 567 | case KVM_REG_MIPS_CP0_INDEX: |
| 568 | v = (long)kvm_read_c0_guest_index(cop0); |
| 569 | break; |
| 570 | case KVM_REG_MIPS_CP0_CONTEXT: |
| 571 | v = (long)kvm_read_c0_guest_context(cop0); |
| 572 | break; |
James Hogan | 7767b7d | 2014-05-29 10:16:30 +0100 | [diff] [blame] | 573 | case KVM_REG_MIPS_CP0_USERLOCAL: |
| 574 | v = (long)kvm_read_c0_guest_userlocal(cop0); |
| 575 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 576 | case KVM_REG_MIPS_CP0_PAGEMASK: |
| 577 | v = (long)kvm_read_c0_guest_pagemask(cop0); |
| 578 | break; |
| 579 | case KVM_REG_MIPS_CP0_WIRED: |
| 580 | v = (long)kvm_read_c0_guest_wired(cop0); |
| 581 | break; |
James Hogan | 16fd5c1 | 2014-05-29 10:16:31 +0100 | [diff] [blame] | 582 | case KVM_REG_MIPS_CP0_HWRENA: |
| 583 | v = (long)kvm_read_c0_guest_hwrena(cop0); |
| 584 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 585 | case KVM_REG_MIPS_CP0_BADVADDR: |
| 586 | v = (long)kvm_read_c0_guest_badvaddr(cop0); |
| 587 | break; |
| 588 | case KVM_REG_MIPS_CP0_ENTRYHI: |
| 589 | v = (long)kvm_read_c0_guest_entryhi(cop0); |
| 590 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 591 | case KVM_REG_MIPS_CP0_COMPARE: |
| 592 | v = (long)kvm_read_c0_guest_compare(cop0); |
| 593 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 594 | case KVM_REG_MIPS_CP0_STATUS: |
| 595 | v = (long)kvm_read_c0_guest_status(cop0); |
| 596 | break; |
| 597 | case KVM_REG_MIPS_CP0_CAUSE: |
| 598 | v = (long)kvm_read_c0_guest_cause(cop0); |
| 599 | break; |
James Hogan | fb6df0c | 2014-05-29 10:16:27 +0100 | [diff] [blame] | 600 | case KVM_REG_MIPS_CP0_EPC: |
| 601 | v = (long)kvm_read_c0_guest_epc(cop0); |
| 602 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 603 | case KVM_REG_MIPS_CP0_ERROREPC: |
| 604 | v = (long)kvm_read_c0_guest_errorepc(cop0); |
| 605 | break; |
| 606 | case KVM_REG_MIPS_CP0_CONFIG: |
| 607 | v = (long)kvm_read_c0_guest_config(cop0); |
| 608 | break; |
| 609 | case KVM_REG_MIPS_CP0_CONFIG1: |
| 610 | v = (long)kvm_read_c0_guest_config1(cop0); |
| 611 | break; |
| 612 | case KVM_REG_MIPS_CP0_CONFIG2: |
| 613 | v = (long)kvm_read_c0_guest_config2(cop0); |
| 614 | break; |
| 615 | case KVM_REG_MIPS_CP0_CONFIG3: |
| 616 | v = (long)kvm_read_c0_guest_config3(cop0); |
| 617 | break; |
| 618 | case KVM_REG_MIPS_CP0_CONFIG7: |
| 619 | v = (long)kvm_read_c0_guest_config7(cop0); |
| 620 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 621 | /* registers to be handled specially */ |
| 622 | case KVM_REG_MIPS_CP0_COUNT: |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 623 | case KVM_REG_MIPS_COUNT_CTL: |
| 624 | case KVM_REG_MIPS_COUNT_RESUME: |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 625 | case KVM_REG_MIPS_COUNT_HZ: |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 626 | ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v); |
| 627 | if (ret) |
| 628 | return ret; |
| 629 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 630 | default: |
| 631 | return -EINVAL; |
| 632 | } |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 633 | if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { |
| 634 | u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 635 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 636 | return put_user(v, uaddr64); |
| 637 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { |
| 638 | u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; |
| 639 | u32 v32 = (u32)v; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 640 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 641 | return put_user(v32, uaddr32); |
| 642 | } else { |
| 643 | return -EINVAL; |
| 644 | } |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static int kvm_mips_set_reg(struct kvm_vcpu *vcpu, |
| 648 | const struct kvm_one_reg *reg) |
| 649 | { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 650 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 651 | u64 v; |
| 652 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 653 | if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { |
| 654 | u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; |
| 655 | |
| 656 | if (get_user(v, uaddr64) != 0) |
| 657 | return -EFAULT; |
| 658 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { |
| 659 | u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; |
| 660 | s32 v32; |
| 661 | |
| 662 | if (get_user(v32, uaddr32) != 0) |
| 663 | return -EFAULT; |
| 664 | v = (s64)v32; |
| 665 | } else { |
| 666 | return -EINVAL; |
| 667 | } |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 668 | |
| 669 | switch (reg->id) { |
| 670 | case KVM_REG_MIPS_R0: |
| 671 | /* Silently ignore requests to set $0 */ |
| 672 | break; |
| 673 | case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31: |
| 674 | vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v; |
| 675 | break; |
| 676 | case KVM_REG_MIPS_HI: |
| 677 | vcpu->arch.hi = v; |
| 678 | break; |
| 679 | case KVM_REG_MIPS_LO: |
| 680 | vcpu->arch.lo = v; |
| 681 | break; |
| 682 | case KVM_REG_MIPS_PC: |
| 683 | vcpu->arch.pc = v; |
| 684 | break; |
| 685 | |
| 686 | case KVM_REG_MIPS_CP0_INDEX: |
| 687 | kvm_write_c0_guest_index(cop0, v); |
| 688 | break; |
| 689 | case KVM_REG_MIPS_CP0_CONTEXT: |
| 690 | kvm_write_c0_guest_context(cop0, v); |
| 691 | break; |
James Hogan | 7767b7d | 2014-05-29 10:16:30 +0100 | [diff] [blame] | 692 | case KVM_REG_MIPS_CP0_USERLOCAL: |
| 693 | kvm_write_c0_guest_userlocal(cop0, v); |
| 694 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 695 | case KVM_REG_MIPS_CP0_PAGEMASK: |
| 696 | kvm_write_c0_guest_pagemask(cop0, v); |
| 697 | break; |
| 698 | case KVM_REG_MIPS_CP0_WIRED: |
| 699 | kvm_write_c0_guest_wired(cop0, v); |
| 700 | break; |
James Hogan | 16fd5c1 | 2014-05-29 10:16:31 +0100 | [diff] [blame] | 701 | case KVM_REG_MIPS_CP0_HWRENA: |
| 702 | kvm_write_c0_guest_hwrena(cop0, v); |
| 703 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 704 | case KVM_REG_MIPS_CP0_BADVADDR: |
| 705 | kvm_write_c0_guest_badvaddr(cop0, v); |
| 706 | break; |
| 707 | case KVM_REG_MIPS_CP0_ENTRYHI: |
| 708 | kvm_write_c0_guest_entryhi(cop0, v); |
| 709 | break; |
| 710 | case KVM_REG_MIPS_CP0_STATUS: |
| 711 | kvm_write_c0_guest_status(cop0, v); |
| 712 | break; |
James Hogan | fb6df0c | 2014-05-29 10:16:27 +0100 | [diff] [blame] | 713 | case KVM_REG_MIPS_CP0_EPC: |
| 714 | kvm_write_c0_guest_epc(cop0, v); |
| 715 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 716 | case KVM_REG_MIPS_CP0_ERROREPC: |
| 717 | kvm_write_c0_guest_errorepc(cop0, v); |
| 718 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 719 | /* registers to be handled specially */ |
| 720 | case KVM_REG_MIPS_CP0_COUNT: |
| 721 | case KVM_REG_MIPS_CP0_COMPARE: |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 722 | case KVM_REG_MIPS_CP0_CAUSE: |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 723 | case KVM_REG_MIPS_COUNT_CTL: |
| 724 | case KVM_REG_MIPS_COUNT_RESUME: |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 725 | case KVM_REG_MIPS_COUNT_HZ: |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 726 | return kvm_mips_callbacks->set_one_reg(vcpu, reg, v); |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 727 | default: |
| 728 | return -EINVAL; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 733 | long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, |
| 734 | unsigned long arg) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 735 | { |
| 736 | struct kvm_vcpu *vcpu = filp->private_data; |
| 737 | void __user *argp = (void __user *)arg; |
| 738 | long r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 739 | |
| 740 | switch (ioctl) { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 741 | case KVM_SET_ONE_REG: |
| 742 | case KVM_GET_ONE_REG: { |
| 743 | struct kvm_one_reg reg; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 744 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 745 | if (copy_from_user(®, argp, sizeof(reg))) |
| 746 | return -EFAULT; |
| 747 | if (ioctl == KVM_SET_ONE_REG) |
| 748 | return kvm_mips_set_reg(vcpu, ®); |
| 749 | else |
| 750 | return kvm_mips_get_reg(vcpu, ®); |
| 751 | } |
| 752 | case KVM_GET_REG_LIST: { |
| 753 | struct kvm_reg_list __user *user_list = argp; |
| 754 | u64 __user *reg_dest; |
| 755 | struct kvm_reg_list reg_list; |
| 756 | unsigned n; |
| 757 | |
| 758 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
| 759 | return -EFAULT; |
| 760 | n = reg_list.n; |
| 761 | reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs); |
| 762 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
| 763 | return -EFAULT; |
| 764 | if (n < reg_list.n) |
| 765 | return -E2BIG; |
| 766 | reg_dest = user_list->reg; |
| 767 | if (copy_to_user(reg_dest, kvm_mips_get_one_regs, |
| 768 | sizeof(kvm_mips_get_one_regs))) |
| 769 | return -EFAULT; |
| 770 | return 0; |
| 771 | } |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 772 | case KVM_NMI: |
| 773 | /* Treat the NMI as a CPU reset */ |
| 774 | r = kvm_mips_reset_vcpu(vcpu); |
| 775 | break; |
| 776 | case KVM_INTERRUPT: |
| 777 | { |
| 778 | struct kvm_mips_interrupt irq; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 779 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 780 | r = -EFAULT; |
| 781 | if (copy_from_user(&irq, argp, sizeof(irq))) |
| 782 | goto out; |
| 783 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 784 | kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__, |
| 785 | irq.irq); |
| 786 | |
| 787 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 788 | break; |
| 789 | } |
| 790 | default: |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 791 | r = -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | out: |
| 795 | return r; |
| 796 | } |
| 797 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 798 | /* Get (and clear) the dirty memory log for a memory slot. */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 799 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
| 800 | { |
| 801 | struct kvm_memory_slot *memslot; |
| 802 | unsigned long ga, ga_end; |
| 803 | int is_dirty = 0; |
| 804 | int r; |
| 805 | unsigned long n; |
| 806 | |
| 807 | mutex_lock(&kvm->slots_lock); |
| 808 | |
| 809 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 810 | if (r) |
| 811 | goto out; |
| 812 | |
| 813 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 814 | if (is_dirty) { |
| 815 | memslot = &kvm->memslots->memslots[log->slot]; |
| 816 | |
| 817 | ga = memslot->base_gfn << PAGE_SHIFT; |
| 818 | ga_end = ga + (memslot->npages << PAGE_SHIFT); |
| 819 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame^] | 820 | kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga, |
| 821 | ga_end); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 822 | |
| 823 | n = kvm_dirty_bitmap_bytes(memslot); |
| 824 | memset(memslot->dirty_bitmap, 0, n); |
| 825 | } |
| 826 | |
| 827 | r = 0; |
| 828 | out: |
| 829 | mutex_unlock(&kvm->slots_lock); |
| 830 | return r; |
| 831 | |
| 832 | } |
| 833 | |
| 834 | long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) |
| 835 | { |
| 836 | long r; |
| 837 | |
| 838 | switch (ioctl) { |
| 839 | default: |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 840 | r = -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | return r; |
| 844 | } |
| 845 | |
| 846 | int kvm_arch_init(void *opaque) |
| 847 | { |
| 848 | int ret; |
| 849 | |
| 850 | if (kvm_mips_callbacks) { |
| 851 | kvm_err("kvm: module already exists\n"); |
| 852 | return -EEXIST; |
| 853 | } |
| 854 | |
| 855 | ret = kvm_mips_emulation_init(&kvm_mips_callbacks); |
| 856 | |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | void kvm_arch_exit(void) |
| 861 | { |
| 862 | kvm_mips_callbacks = NULL; |
| 863 | } |
| 864 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 865 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 866 | struct kvm_sregs *sregs) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 867 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 868 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 871 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 872 | struct kvm_sregs *sregs) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 873 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 874 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 878 | { |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 883 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 884 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 888 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 889 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 893 | { |
| 894 | return VM_FAULT_SIGBUS; |
| 895 | } |
| 896 | |
| 897 | int kvm_dev_ioctl_check_extension(long ext) |
| 898 | { |
| 899 | int r; |
| 900 | |
| 901 | switch (ext) { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 902 | case KVM_CAP_ONE_REG: |
| 903 | r = 1; |
| 904 | break; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 905 | case KVM_CAP_COALESCED_MMIO: |
| 906 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 907 | break; |
| 908 | default: |
| 909 | r = 0; |
| 910 | break; |
| 911 | } |
| 912 | return r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 916 | { |
| 917 | return kvm_mips_pending_timer(vcpu); |
| 918 | } |
| 919 | |
| 920 | int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu) |
| 921 | { |
| 922 | int i; |
| 923 | struct mips_coproc *cop0; |
| 924 | |
| 925 | if (!vcpu) |
| 926 | return -1; |
| 927 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame^] | 928 | kvm_debug("VCPU Register Dump:\n"); |
| 929 | kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc); |
| 930 | kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 931 | |
| 932 | for (i = 0; i < 32; i += 4) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame^] | 933 | kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i, |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 934 | vcpu->arch.gprs[i], |
| 935 | vcpu->arch.gprs[i + 1], |
| 936 | vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]); |
| 937 | } |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame^] | 938 | kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi); |
| 939 | kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 940 | |
| 941 | cop0 = vcpu->arch.cop0; |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame^] | 942 | kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n", |
| 943 | kvm_read_c0_guest_status(cop0), |
| 944 | kvm_read_c0_guest_cause(cop0)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 945 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame^] | 946 | kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 952 | { |
| 953 | int i; |
| 954 | |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 955 | for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++) |
David Daney | bf32ebf | 2013-05-23 09:49:07 -0700 | [diff] [blame] | 956 | vcpu->arch.gprs[i] = regs->gpr[i]; |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 957 | vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 958 | vcpu->arch.hi = regs->hi; |
| 959 | vcpu->arch.lo = regs->lo; |
| 960 | vcpu->arch.pc = regs->pc; |
| 961 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 962 | return 0; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 966 | { |
| 967 | int i; |
| 968 | |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 969 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++) |
David Daney | bf32ebf | 2013-05-23 09:49:07 -0700 | [diff] [blame] | 970 | regs->gpr[i] = vcpu->arch.gprs[i]; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 971 | |
| 972 | regs->hi = vcpu->arch.hi; |
| 973 | regs->lo = vcpu->arch.lo; |
| 974 | regs->pc = vcpu->arch.pc; |
| 975 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 976 | return 0; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 977 | } |
| 978 | |
James Hogan | 0fae34f | 2014-05-29 10:16:39 +0100 | [diff] [blame] | 979 | static void kvm_mips_comparecount_func(unsigned long data) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 980 | { |
| 981 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 982 | |
| 983 | kvm_mips_callbacks->queue_timer_int(vcpu); |
| 984 | |
| 985 | vcpu->arch.wait = 0; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 986 | if (waitqueue_active(&vcpu->wq)) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 987 | wake_up_interruptible(&vcpu->wq); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 990 | /* low level hrtimer wake routine */ |
James Hogan | 0fae34f | 2014-05-29 10:16:39 +0100 | [diff] [blame] | 991 | static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 992 | { |
| 993 | struct kvm_vcpu *vcpu; |
| 994 | |
| 995 | vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer); |
| 996 | kvm_mips_comparecount_func((unsigned long) vcpu); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 997 | return kvm_mips_count_timeout(vcpu); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 1001 | { |
| 1002 | kvm_mips_callbacks->vcpu_init(vcpu); |
| 1003 | hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC, |
| 1004 | HRTIMER_MODE_REL); |
| 1005 | vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 1010 | { |
| 1011 | return; |
| 1012 | } |
| 1013 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1014 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 1015 | struct kvm_translation *tr) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1016 | { |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /* Initial guest state */ |
| 1021 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1022 | { |
| 1023 | return kvm_mips_callbacks->vcpu_setup(vcpu); |
| 1024 | } |
| 1025 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1026 | static void kvm_mips_set_c0_status(void) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1027 | { |
| 1028 | uint32_t status = read_c0_status(); |
| 1029 | |
| 1030 | if (cpu_has_fpu) |
| 1031 | status |= (ST0_CU1); |
| 1032 | |
| 1033 | if (cpu_has_dsp) |
| 1034 | status |= (ST0_MX); |
| 1035 | |
| 1036 | write_c0_status(status); |
| 1037 | ehb(); |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) |
| 1042 | */ |
| 1043 | int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 1044 | { |
| 1045 | uint32_t cause = vcpu->arch.host_cp0_cause; |
| 1046 | uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; |
| 1047 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 1048 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 1049 | enum emulation_result er = EMULATE_DONE; |
| 1050 | int ret = RESUME_GUEST; |
| 1051 | |
| 1052 | /* Set a default exit reason */ |
| 1053 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1054 | run->ready_for_interrupt_injection = 1; |
| 1055 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1056 | /* |
| 1057 | * Set the appropriate status bits based on host CPU features, |
| 1058 | * before we hit the scheduler |
| 1059 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1060 | kvm_mips_set_c0_status(); |
| 1061 | |
| 1062 | local_irq_enable(); |
| 1063 | |
| 1064 | kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n", |
| 1065 | cause, opc, run, vcpu); |
| 1066 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1067 | /* |
| 1068 | * Do a privilege check, if in UM most of these exit conditions end up |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1069 | * causing an exception to be delivered to the Guest Kernel |
| 1070 | */ |
| 1071 | er = kvm_mips_check_privilege(cause, opc, run, vcpu); |
| 1072 | if (er == EMULATE_PRIV_FAIL) { |
| 1073 | goto skip_emul; |
| 1074 | } else if (er == EMULATE_FAIL) { |
| 1075 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1076 | ret = RESUME_HOST; |
| 1077 | goto skip_emul; |
| 1078 | } |
| 1079 | |
| 1080 | switch (exccode) { |
| 1081 | case T_INT: |
| 1082 | kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc); |
| 1083 | |
| 1084 | ++vcpu->stat.int_exits; |
| 1085 | trace_kvm_exit(vcpu, INT_EXITS); |
| 1086 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1087 | if (need_resched()) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1088 | cond_resched(); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1089 | |
| 1090 | ret = RESUME_GUEST; |
| 1091 | break; |
| 1092 | |
| 1093 | case T_COP_UNUSABLE: |
| 1094 | kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc); |
| 1095 | |
| 1096 | ++vcpu->stat.cop_unusable_exits; |
| 1097 | trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS); |
| 1098 | ret = kvm_mips_callbacks->handle_cop_unusable(vcpu); |
| 1099 | /* XXXKYMA: Might need to return to user space */ |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1100 | if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1101 | ret = RESUME_HOST; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1102 | break; |
| 1103 | |
| 1104 | case T_TLB_MOD: |
| 1105 | ++vcpu->stat.tlbmod_exits; |
| 1106 | trace_kvm_exit(vcpu, TLBMOD_EXITS); |
| 1107 | ret = kvm_mips_callbacks->handle_tlb_mod(vcpu); |
| 1108 | break; |
| 1109 | |
| 1110 | case T_TLB_ST_MISS: |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1111 | kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n", |
| 1112 | cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc, |
| 1113 | badvaddr); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1114 | |
| 1115 | ++vcpu->stat.tlbmiss_st_exits; |
| 1116 | trace_kvm_exit(vcpu, TLBMISS_ST_EXITS); |
| 1117 | ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu); |
| 1118 | break; |
| 1119 | |
| 1120 | case T_TLB_LD_MISS: |
| 1121 | kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n", |
| 1122 | cause, opc, badvaddr); |
| 1123 | |
| 1124 | ++vcpu->stat.tlbmiss_ld_exits; |
| 1125 | trace_kvm_exit(vcpu, TLBMISS_LD_EXITS); |
| 1126 | ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu); |
| 1127 | break; |
| 1128 | |
| 1129 | case T_ADDR_ERR_ST: |
| 1130 | ++vcpu->stat.addrerr_st_exits; |
| 1131 | trace_kvm_exit(vcpu, ADDRERR_ST_EXITS); |
| 1132 | ret = kvm_mips_callbacks->handle_addr_err_st(vcpu); |
| 1133 | break; |
| 1134 | |
| 1135 | case T_ADDR_ERR_LD: |
| 1136 | ++vcpu->stat.addrerr_ld_exits; |
| 1137 | trace_kvm_exit(vcpu, ADDRERR_LD_EXITS); |
| 1138 | ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu); |
| 1139 | break; |
| 1140 | |
| 1141 | case T_SYSCALL: |
| 1142 | ++vcpu->stat.syscall_exits; |
| 1143 | trace_kvm_exit(vcpu, SYSCALL_EXITS); |
| 1144 | ret = kvm_mips_callbacks->handle_syscall(vcpu); |
| 1145 | break; |
| 1146 | |
| 1147 | case T_RES_INST: |
| 1148 | ++vcpu->stat.resvd_inst_exits; |
| 1149 | trace_kvm_exit(vcpu, RESVD_INST_EXITS); |
| 1150 | ret = kvm_mips_callbacks->handle_res_inst(vcpu); |
| 1151 | break; |
| 1152 | |
| 1153 | case T_BREAK: |
| 1154 | ++vcpu->stat.break_inst_exits; |
| 1155 | trace_kvm_exit(vcpu, BREAK_INST_EXITS); |
| 1156 | ret = kvm_mips_callbacks->handle_break(vcpu); |
| 1157 | break; |
| 1158 | |
| 1159 | default: |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1160 | kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n", |
| 1161 | exccode, opc, kvm_get_inst(opc, vcpu), badvaddr, |
| 1162 | kvm_read_c0_guest_status(vcpu->arch.cop0)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1163 | kvm_arch_vcpu_dump_regs(vcpu); |
| 1164 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1165 | ret = RESUME_HOST; |
| 1166 | break; |
| 1167 | |
| 1168 | } |
| 1169 | |
| 1170 | skip_emul: |
| 1171 | local_irq_disable(); |
| 1172 | |
| 1173 | if (er == EMULATE_DONE && !(ret & RESUME_HOST)) |
| 1174 | kvm_mips_deliver_interrupts(vcpu, cause); |
| 1175 | |
| 1176 | if (!(ret & RESUME_HOST)) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1177 | /* Only check for signals if not already exiting to userspace */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1178 | if (signal_pending(current)) { |
| 1179 | run->exit_reason = KVM_EXIT_INTR; |
| 1180 | ret = (-EINTR << 2) | RESUME_HOST; |
| 1181 | ++vcpu->stat.signal_exits; |
| 1182 | trace_kvm_exit(vcpu, SIGNAL_EXITS); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | return ret; |
| 1187 | } |
| 1188 | |
| 1189 | int __init kvm_mips_init(void) |
| 1190 | { |
| 1191 | int ret; |
| 1192 | |
| 1193 | ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 1194 | |
| 1195 | if (ret) |
| 1196 | return ret; |
| 1197 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1198 | /* |
| 1199 | * On MIPS, kernel modules are executed from "mapped space", which |
| 1200 | * requires TLBs. The TLB handling code is statically linked with |
| 1201 | * the rest of the kernel (kvm_tlb.c) to avoid the possibility of |
| 1202 | * double faulting. The issue is that the TLB code references |
| 1203 | * routines that are part of the the KVM module, which are only |
| 1204 | * available once the module is loaded. |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1205 | */ |
| 1206 | kvm_mips_gfn_to_pfn = gfn_to_pfn; |
| 1207 | kvm_mips_release_pfn_clean = kvm_release_pfn_clean; |
| 1208 | kvm_mips_is_error_pfn = is_error_pfn; |
| 1209 | |
| 1210 | pr_info("KVM/MIPS Initialized\n"); |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | void __exit kvm_mips_exit(void) |
| 1215 | { |
| 1216 | kvm_exit(); |
| 1217 | |
| 1218 | kvm_mips_gfn_to_pfn = NULL; |
| 1219 | kvm_mips_release_pfn_clean = NULL; |
| 1220 | kvm_mips_is_error_pfn = NULL; |
| 1221 | |
| 1222 | pr_info("KVM/MIPS unloaded\n"); |
| 1223 | } |
| 1224 | |
| 1225 | module_init(kvm_mips_init); |
| 1226 | module_exit(kvm_mips_exit); |
| 1227 | |
| 1228 | EXPORT_TRACEPOINT_SYMBOL(kvm_exit); |