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