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