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