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 | |
James Hogan | 0510870 | 2016-06-15 19:29:56 +0100 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/err.h> |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 15 | #include <linux/kdebug.h> |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 16 | #include <linux/module.h> |
James Hogan | d852b5f | 2016-10-19 00:24:27 +0100 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/bootmem.h> |
James Hogan | f798217 | 2015-02-04 17:06:37 +0000 | [diff] [blame] | 21 | #include <asm/fpu.h> |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 22 | #include <asm/page.h> |
| 23 | #include <asm/cacheflush.h> |
| 24 | #include <asm/mmu_context.h> |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 25 | #include <asm/pgalloc.h> |
James Hogan | c4c6f2c | 2015-02-04 10:52:03 +0000 | [diff] [blame] | 26 | #include <asm/pgtable.h> |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 27 | |
| 28 | #include <linux/kvm_host.h> |
| 29 | |
Deng-Cheng Zhu | d7d5b05 | 2014-06-26 12:11:38 -0700 | [diff] [blame] | 30 | #include "interrupt.h" |
| 31 | #include "commpage.h" |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 32 | |
| 33 | #define CREATE_TRACE_POINTS |
| 34 | #include "trace.h" |
| 35 | |
| 36 | #ifndef VECTORSPACING |
| 37 | #define VECTORSPACING 0x100 /* for EI/VI mode */ |
| 38 | #endif |
| 39 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 40 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 41 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 42 | { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU }, |
| 43 | { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU }, |
| 44 | { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU }, |
| 45 | { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU }, |
| 46 | { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU }, |
| 47 | { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU }, |
| 48 | { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU }, |
| 49 | { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU }, |
| 50 | { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU }, |
| 51 | { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU }, |
| 52 | { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU }, |
| 53 | { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU }, |
| 54 | { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU }, |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 55 | { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU }, |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 56 | { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU }, |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 57 | { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU }, |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 58 | { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU }, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 59 | { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU }, |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 60 | { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU }, |
Paolo Bonzini | 62bea5b | 2015-09-15 18:27:57 +0200 | [diff] [blame] | 61 | { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU }, |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 62 | { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU }, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 63 | { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU }, |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 64 | {NULL} |
| 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 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 96 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 97 | { |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 98 | /* Allocate page table to map GPA -> RPA */ |
| 99 | kvm->arch.gpa_mm.pgd = kvm_pgd_alloc(); |
| 100 | if (!kvm->arch.gpa_mm.pgd) |
| 101 | return -ENOMEM; |
| 102 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 106 | bool kvm_arch_has_vcpu_debugfs(void) |
| 107 | { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 116 | void kvm_mips_free_vcpus(struct kvm *kvm) |
| 117 | { |
| 118 | unsigned int i; |
| 119 | struct kvm_vcpu *vcpu; |
| 120 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 121 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 122 | kvm_arch_vcpu_free(vcpu); |
| 123 | } |
| 124 | |
| 125 | mutex_lock(&kvm->lock); |
| 126 | |
| 127 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 128 | kvm->vcpus[i] = NULL; |
| 129 | |
| 130 | atomic_set(&kvm->online_vcpus, 0); |
| 131 | |
| 132 | mutex_unlock(&kvm->lock); |
| 133 | } |
| 134 | |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 135 | static void kvm_mips_free_gpa_pt(struct kvm *kvm) |
| 136 | { |
| 137 | /* It should always be safe to remove after flushing the whole range */ |
| 138 | WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0)); |
| 139 | pgd_free(NULL, kvm->arch.gpa_mm.pgd); |
| 140 | } |
| 141 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 142 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 143 | { |
| 144 | kvm_mips_free_vcpus(kvm); |
James Hogan | 06c158c | 2015-05-01 13:50:18 +0100 | [diff] [blame] | 145 | kvm_mips_free_gpa_pt(kvm); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 148 | long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, |
| 149 | unsigned long arg) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 150 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 151 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 154 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 155 | unsigned long npages) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
James Hogan | b620911 | 2016-10-25 00:01:37 +0100 | [diff] [blame] | 160 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 161 | { |
| 162 | /* Flush whole GPA */ |
| 163 | kvm_mips_flush_gpa_pt(kvm, 0, ~0); |
| 164 | |
| 165 | /* Let implementation do the rest */ |
| 166 | kvm_mips_callbacks->flush_shadow_all(kvm); |
| 167 | } |
| 168 | |
| 169 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 170 | struct kvm_memory_slot *slot) |
| 171 | { |
| 172 | /* |
| 173 | * The slot has been made invalid (ready for moving or deletion), so we |
| 174 | * need to ensure that it can no longer be accessed by any guest VCPUs. |
| 175 | */ |
| 176 | |
| 177 | spin_lock(&kvm->mmu_lock); |
| 178 | /* Flush slot from GPA */ |
| 179 | kvm_mips_flush_gpa_pt(kvm, slot->base_gfn, |
| 180 | slot->base_gfn + slot->npages - 1); |
| 181 | /* Let implementation do the rest */ |
| 182 | kvm_mips_callbacks->flush_shadow_memslot(kvm, slot); |
| 183 | spin_unlock(&kvm->mmu_lock); |
| 184 | } |
| 185 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 186 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 187 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 188 | const struct kvm_userspace_memory_region *mem, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 189 | enum kvm_mr_change change) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 190 | { |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 195 | const struct kvm_userspace_memory_region *mem, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 196 | const struct kvm_memory_slot *old, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 197 | const struct kvm_memory_slot *new, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 198 | enum kvm_mr_change change) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 199 | { |
James Hogan | a1ac9e1 | 2016-12-06 14:56:20 +0000 | [diff] [blame] | 200 | int needs_flush; |
| 201 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 202 | kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n", |
| 203 | __func__, kvm, mem->slot, mem->guest_phys_addr, |
| 204 | mem->memory_size, mem->userspace_addr); |
James Hogan | a1ac9e1 | 2016-12-06 14:56:20 +0000 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * If dirty page logging is enabled, write protect all pages in the slot |
| 208 | * ready for dirty logging. |
| 209 | * |
| 210 | * There is no need to do this in any of the following cases: |
| 211 | * CREATE: No dirty mappings will already exist. |
| 212 | * MOVE/DELETE: The old mappings will already have been cleaned up by |
| 213 | * kvm_arch_flush_shadow_memslot() |
| 214 | */ |
| 215 | if (change == KVM_MR_FLAGS_ONLY && |
| 216 | (!(old->flags & KVM_MEM_LOG_DIRTY_PAGES) && |
| 217 | new->flags & KVM_MEM_LOG_DIRTY_PAGES)) { |
| 218 | spin_lock(&kvm->mmu_lock); |
| 219 | /* Write protect GPA page table entries */ |
| 220 | needs_flush = kvm_mips_mkclean_gpa_pt(kvm, new->base_gfn, |
| 221 | new->base_gfn + new->npages - 1); |
| 222 | /* Let implementation do the rest */ |
| 223 | if (needs_flush) |
| 224 | kvm_mips_callbacks->flush_shadow_memslot(kvm, new); |
| 225 | spin_unlock(&kvm->mmu_lock); |
| 226 | } |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 227 | } |
| 228 | |
James Hogan | d7b8f89 | 2016-06-23 17:34:40 +0100 | [diff] [blame] | 229 | static inline void dump_handler(const char *symbol, void *start, void *end) |
| 230 | { |
| 231 | u32 *p; |
| 232 | |
| 233 | pr_debug("LEAF(%s)\n", symbol); |
| 234 | |
| 235 | pr_debug("\t.set push\n"); |
| 236 | pr_debug("\t.set noreorder\n"); |
| 237 | |
| 238 | for (p = start; p < (u32 *)end; ++p) |
| 239 | pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p); |
| 240 | |
| 241 | pr_debug("\t.set\tpop\n"); |
| 242 | |
| 243 | pr_debug("\tEND(%s)\n", symbol); |
| 244 | } |
| 245 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 246 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 247 | { |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 248 | int err, size; |
James Hogan | a7cfa7a | 2016-09-10 23:56:46 +0100 | [diff] [blame] | 249 | void *gebase, *p, *handler, *refill_start, *refill_end; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 250 | int i; |
| 251 | |
| 252 | struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL); |
| 253 | |
| 254 | if (!vcpu) { |
| 255 | err = -ENOMEM; |
| 256 | goto out; |
| 257 | } |
| 258 | |
| 259 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 260 | |
| 261 | if (err) |
| 262 | goto out_free_cpu; |
| 263 | |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 264 | 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] | 265 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 266 | /* |
| 267 | * Allocate space for host mode exception handlers that handle |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 268 | * guest mode exits |
| 269 | */ |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 270 | if (cpu_has_veic || cpu_has_vint) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 271 | size = 0x200 + VECTORSPACING * 64; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 272 | else |
James Hogan | 7006e2d | 2014-05-29 10:16:23 +0100 | [diff] [blame] | 273 | size = 0x4000; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 274 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 275 | gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL); |
| 276 | |
| 277 | if (!gebase) { |
| 278 | err = -ENOMEM; |
James Hogan | 585bb8f | 2015-11-11 14:21:20 +0000 | [diff] [blame] | 279 | goto out_uninit_cpu; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 280 | } |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 281 | kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n", |
| 282 | ALIGN(size, PAGE_SIZE), gebase); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 283 | |
James Hogan | 2a06dab | 2016-07-08 11:53:26 +0100 | [diff] [blame] | 284 | /* |
| 285 | * Check new ebase actually fits in CP0_EBase. The lack of a write gate |
| 286 | * limits us to the low 512MB of physical address space. If the memory |
| 287 | * we allocate is out of range, just give up now. |
| 288 | */ |
| 289 | if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) { |
| 290 | kvm_err("CP0_EBase.WG required for guest exception base %pK\n", |
| 291 | gebase); |
| 292 | err = -ENOMEM; |
| 293 | goto out_free_gebase; |
| 294 | } |
| 295 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 296 | /* Save new ebase */ |
| 297 | vcpu->arch.guest_ebase = gebase; |
| 298 | |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 299 | /* Build guest exception vectors dynamically in unmapped memory */ |
James Hogan | 1f9ca62 | 2016-06-23 17:34:46 +0100 | [diff] [blame] | 300 | handler = gebase + 0x2000; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 301 | |
James Hogan | a7cfa7a | 2016-09-10 23:56:46 +0100 | [diff] [blame] | 302 | /* TLB refill */ |
| 303 | refill_start = gebase; |
| 304 | refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 305 | |
| 306 | /* General Exception Entry point */ |
James Hogan | 1f9ca62 | 2016-06-23 17:34:46 +0100 | [diff] [blame] | 307 | kvm_mips_build_exception(gebase + 0x180, handler); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 308 | |
| 309 | /* For vectored interrupts poke the exception code @ all offsets 0-7 */ |
| 310 | for (i = 0; i < 8; i++) { |
| 311 | kvm_debug("L1 Vectored handler @ %p\n", |
| 312 | gebase + 0x200 + (i * VECTORSPACING)); |
James Hogan | 1f9ca62 | 2016-06-23 17:34:46 +0100 | [diff] [blame] | 313 | kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING, |
| 314 | handler); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 315 | } |
| 316 | |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 317 | /* General exit handler */ |
James Hogan | 1f9ca62 | 2016-06-23 17:34:46 +0100 | [diff] [blame] | 318 | p = handler; |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 319 | p = kvm_mips_build_exit(p); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 320 | |
James Hogan | 90e9311 | 2016-06-23 17:34:39 +0100 | [diff] [blame] | 321 | /* Guest entry routine */ |
| 322 | vcpu->arch.vcpu_run = p; |
| 323 | p = kvm_mips_build_vcpu_run(p); |
James Hogan | 797179b | 2016-06-09 10:50:43 +0100 | [diff] [blame] | 324 | |
James Hogan | d7b8f89 | 2016-06-23 17:34:40 +0100 | [diff] [blame] | 325 | /* Dump the generated code */ |
| 326 | pr_debug("#include <asm/asm.h>\n"); |
| 327 | pr_debug("#include <asm/regdef.h>\n"); |
| 328 | pr_debug("\n"); |
| 329 | dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p); |
James Hogan | a7cfa7a | 2016-09-10 23:56:46 +0100 | [diff] [blame] | 330 | dump_handler("kvm_tlb_refill", refill_start, refill_end); |
James Hogan | d7b8f89 | 2016-06-23 17:34:40 +0100 | [diff] [blame] | 331 | dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200); |
| 332 | dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run); |
| 333 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 334 | /* Invalidate the icache for these ranges */ |
James Hogan | 32eb12a | 2017-01-03 17:43:01 +0000 | [diff] [blame] | 335 | flush_icache_range((unsigned long)gebase, |
| 336 | (unsigned long)gebase + ALIGN(size, PAGE_SIZE)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 337 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 338 | /* |
| 339 | * Allocate comm page for guest kernel, a TLB will be reserved for |
| 340 | * mapping GVA @ 0xFFFF8000 to this page |
| 341 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 342 | vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL); |
| 343 | |
| 344 | if (!vcpu->arch.kseg0_commpage) { |
| 345 | err = -ENOMEM; |
| 346 | goto out_free_gebase; |
| 347 | } |
| 348 | |
James Hogan | 6e95bfd | 2014-05-29 10:16:43 +0100 | [diff] [blame] | 349 | kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 350 | kvm_mips_commpage_init(vcpu); |
| 351 | |
| 352 | /* Init */ |
| 353 | vcpu->arch.last_sched_cpu = -1; |
| 354 | |
| 355 | /* Start off the timer */ |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 356 | kvm_mips_init_count(vcpu); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 357 | |
| 358 | return vcpu; |
| 359 | |
| 360 | out_free_gebase: |
| 361 | kfree(gebase); |
| 362 | |
James Hogan | 585bb8f | 2015-11-11 14:21:20 +0000 | [diff] [blame] | 363 | out_uninit_cpu: |
| 364 | kvm_vcpu_uninit(vcpu); |
| 365 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 366 | out_free_cpu: |
| 367 | kfree(vcpu); |
| 368 | |
| 369 | out: |
| 370 | return ERR_PTR(err); |
| 371 | } |
| 372 | |
| 373 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 374 | { |
| 375 | hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 376 | |
| 377 | kvm_vcpu_uninit(vcpu); |
| 378 | |
| 379 | kvm_mips_dump_stats(vcpu); |
| 380 | |
James Hogan | aba85929 | 2016-12-16 15:57:00 +0000 | [diff] [blame] | 381 | kvm_mmu_free_memory_caches(vcpu); |
James Hogan | c6c0a66 | 2014-05-29 10:16:44 +0100 | [diff] [blame] | 382 | kfree(vcpu->arch.guest_ebase); |
| 383 | kfree(vcpu->arch.kseg0_commpage); |
Deng-Cheng Zhu | 8c9eb04 | 2014-06-24 10:31:08 -0700 | [diff] [blame] | 384 | kfree(vcpu); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 388 | { |
| 389 | kvm_arch_vcpu_free(vcpu); |
| 390 | } |
| 391 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 392 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 393 | struct kvm_guest_debug *dbg) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 394 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 395 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 399 | { |
| 400 | int r = 0; |
| 401 | sigset_t sigsaved; |
| 402 | |
| 403 | if (vcpu->sigset_active) |
| 404 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 405 | |
| 406 | if (vcpu->mmio_needed) { |
| 407 | if (!vcpu->mmio_is_write) |
| 408 | kvm_mips_complete_mmio_load(vcpu, run); |
| 409 | vcpu->mmio_needed = 0; |
| 410 | } |
| 411 | |
James Hogan | f798217 | 2015-02-04 17:06:37 +0000 | [diff] [blame] | 412 | lose_fpu(1); |
| 413 | |
James Hogan | 044f0f0 | 2014-05-29 10:16:32 +0100 | [diff] [blame] | 414 | local_irq_disable(); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 415 | guest_enter_irqoff(); |
James Hogan | 9325860 | 2016-06-14 09:40:14 +0100 | [diff] [blame] | 416 | trace_kvm_enter(vcpu); |
James Hogan | 25b08c7 | 2016-09-16 00:06:43 +0100 | [diff] [blame] | 417 | |
James Hogan | 4841e0d | 2016-11-28 22:45:04 +0000 | [diff] [blame] | 418 | /* |
| 419 | * Make sure the read of VCPU requests in vcpu_run() callback is not |
| 420 | * reordered ahead of the write to vcpu->mode, or we could miss a TLB |
| 421 | * flush request while the requester sees the VCPU as outside of guest |
| 422 | * mode and not needing an IPI. |
| 423 | */ |
| 424 | smp_store_mb(vcpu->mode, IN_GUEST_MODE); |
| 425 | |
James Hogan | a2c046e | 2016-11-18 13:14:37 +0000 | [diff] [blame] | 426 | r = kvm_mips_callbacks->vcpu_run(run, vcpu); |
James Hogan | 25b08c7 | 2016-09-16 00:06:43 +0100 | [diff] [blame] | 427 | |
James Hogan | 9325860 | 2016-06-14 09:40:14 +0100 | [diff] [blame] | 428 | trace_kvm_out(vcpu); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 429 | guest_exit_irqoff(); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 430 | local_irq_enable(); |
| 431 | |
| 432 | if (vcpu->sigset_active) |
| 433 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 434 | |
| 435 | return r; |
| 436 | } |
| 437 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 438 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 439 | struct kvm_mips_interrupt *irq) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 440 | { |
| 441 | int intr = (int)irq->irq; |
| 442 | struct kvm_vcpu *dvcpu = NULL; |
| 443 | |
| 444 | if (intr == 3 || intr == -3 || intr == 4 || intr == -4) |
| 445 | kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu, |
| 446 | (int)intr); |
| 447 | |
| 448 | if (irq->cpu == -1) |
| 449 | dvcpu = vcpu; |
| 450 | else |
| 451 | dvcpu = vcpu->kvm->vcpus[irq->cpu]; |
| 452 | |
| 453 | if (intr == 2 || intr == 3 || intr == 4) { |
| 454 | kvm_mips_callbacks->queue_io_int(dvcpu, irq); |
| 455 | |
| 456 | } else if (intr == -2 || intr == -3 || intr == -4) { |
| 457 | kvm_mips_callbacks->dequeue_io_int(dvcpu, irq); |
| 458 | } else { |
| 459 | kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__, |
| 460 | irq->cpu, irq->irq); |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | |
| 464 | dvcpu->arch.wait = 0; |
| 465 | |
Marcelo Tosatti | 8577370 | 2016-02-19 09:46:39 +0100 | [diff] [blame] | 466 | if (swait_active(&dvcpu->wq)) |
| 467 | swake_up(&dvcpu->wq); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 472 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 473 | struct kvm_mp_state *mp_state) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 474 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 475 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 478 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 479 | struct kvm_mp_state *mp_state) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 480 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 481 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 482 | } |
| 483 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 484 | static u64 kvm_mips_get_one_regs[] = { |
| 485 | KVM_REG_MIPS_R0, |
| 486 | KVM_REG_MIPS_R1, |
| 487 | KVM_REG_MIPS_R2, |
| 488 | KVM_REG_MIPS_R3, |
| 489 | KVM_REG_MIPS_R4, |
| 490 | KVM_REG_MIPS_R5, |
| 491 | KVM_REG_MIPS_R6, |
| 492 | KVM_REG_MIPS_R7, |
| 493 | KVM_REG_MIPS_R8, |
| 494 | KVM_REG_MIPS_R9, |
| 495 | KVM_REG_MIPS_R10, |
| 496 | KVM_REG_MIPS_R11, |
| 497 | KVM_REG_MIPS_R12, |
| 498 | KVM_REG_MIPS_R13, |
| 499 | KVM_REG_MIPS_R14, |
| 500 | KVM_REG_MIPS_R15, |
| 501 | KVM_REG_MIPS_R16, |
| 502 | KVM_REG_MIPS_R17, |
| 503 | KVM_REG_MIPS_R18, |
| 504 | KVM_REG_MIPS_R19, |
| 505 | KVM_REG_MIPS_R20, |
| 506 | KVM_REG_MIPS_R21, |
| 507 | KVM_REG_MIPS_R22, |
| 508 | KVM_REG_MIPS_R23, |
| 509 | KVM_REG_MIPS_R24, |
| 510 | KVM_REG_MIPS_R25, |
| 511 | KVM_REG_MIPS_R26, |
| 512 | KVM_REG_MIPS_R27, |
| 513 | KVM_REG_MIPS_R28, |
| 514 | KVM_REG_MIPS_R29, |
| 515 | KVM_REG_MIPS_R30, |
| 516 | KVM_REG_MIPS_R31, |
| 517 | |
James Hogan | 70e92c7e | 2016-07-04 19:35:11 +0100 | [diff] [blame] | 518 | #ifndef CONFIG_CPU_MIPSR6 |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 519 | KVM_REG_MIPS_HI, |
| 520 | KVM_REG_MIPS_LO, |
James Hogan | 70e92c7e | 2016-07-04 19:35:11 +0100 | [diff] [blame] | 521 | #endif |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 522 | KVM_REG_MIPS_PC, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 523 | }; |
| 524 | |
James Hogan | e577593 | 2016-06-15 19:29:51 +0100 | [diff] [blame] | 525 | static u64 kvm_mips_get_one_regs_fpu[] = { |
| 526 | KVM_REG_MIPS_FCR_IR, |
| 527 | KVM_REG_MIPS_FCR_CSR, |
| 528 | }; |
| 529 | |
| 530 | static u64 kvm_mips_get_one_regs_msa[] = { |
| 531 | KVM_REG_MIPS_MSA_IR, |
| 532 | KVM_REG_MIPS_MSA_CSR, |
| 533 | }; |
| 534 | |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 535 | static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu) |
| 536 | { |
| 537 | unsigned long ret; |
| 538 | |
| 539 | ret = ARRAY_SIZE(kvm_mips_get_one_regs); |
James Hogan | e577593 | 2016-06-15 19:29:51 +0100 | [diff] [blame] | 540 | if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) { |
| 541 | ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48; |
| 542 | /* odd doubles */ |
| 543 | if (boot_cpu_data.fpu_id & MIPS_FPIR_F64) |
| 544 | ret += 16; |
| 545 | } |
| 546 | if (kvm_mips_guest_can_have_msa(&vcpu->arch)) |
| 547 | ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32; |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 548 | ret += kvm_mips_callbacks->num_regs(vcpu); |
| 549 | |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices) |
| 554 | { |
James Hogan | e577593 | 2016-06-15 19:29:51 +0100 | [diff] [blame] | 555 | u64 index; |
| 556 | unsigned int i; |
| 557 | |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 558 | if (copy_to_user(indices, kvm_mips_get_one_regs, |
| 559 | sizeof(kvm_mips_get_one_regs))) |
| 560 | return -EFAULT; |
| 561 | indices += ARRAY_SIZE(kvm_mips_get_one_regs); |
| 562 | |
James Hogan | e577593 | 2016-06-15 19:29:51 +0100 | [diff] [blame] | 563 | if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) { |
| 564 | if (copy_to_user(indices, kvm_mips_get_one_regs_fpu, |
| 565 | sizeof(kvm_mips_get_one_regs_fpu))) |
| 566 | return -EFAULT; |
| 567 | indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu); |
| 568 | |
| 569 | for (i = 0; i < 32; ++i) { |
| 570 | index = KVM_REG_MIPS_FPR_32(i); |
| 571 | if (copy_to_user(indices, &index, sizeof(index))) |
| 572 | return -EFAULT; |
| 573 | ++indices; |
| 574 | |
| 575 | /* skip odd doubles if no F64 */ |
| 576 | if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64)) |
| 577 | continue; |
| 578 | |
| 579 | index = KVM_REG_MIPS_FPR_64(i); |
| 580 | if (copy_to_user(indices, &index, sizeof(index))) |
| 581 | return -EFAULT; |
| 582 | ++indices; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (kvm_mips_guest_can_have_msa(&vcpu->arch)) { |
| 587 | if (copy_to_user(indices, kvm_mips_get_one_regs_msa, |
| 588 | sizeof(kvm_mips_get_one_regs_msa))) |
| 589 | return -EFAULT; |
| 590 | indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa); |
| 591 | |
| 592 | for (i = 0; i < 32; ++i) { |
| 593 | index = KVM_REG_MIPS_VEC_128(i); |
| 594 | if (copy_to_user(indices, &index, sizeof(index))) |
| 595 | return -EFAULT; |
| 596 | ++indices; |
| 597 | } |
| 598 | } |
| 599 | |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 600 | return kvm_mips_callbacks->copy_reg_indices(vcpu, indices); |
| 601 | } |
| 602 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 603 | static int kvm_mips_get_reg(struct kvm_vcpu *vcpu, |
| 604 | const struct kvm_one_reg *reg) |
| 605 | { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 606 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 607 | struct mips_fpu_struct *fpu = &vcpu->arch.fpu; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 608 | int ret; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 609 | s64 v; |
James Hogan | ab86bd6 | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 610 | s64 vs[2]; |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 611 | unsigned int idx; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 612 | |
| 613 | switch (reg->id) { |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 614 | /* General purpose registers */ |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 615 | case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31: |
| 616 | v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0]; |
| 617 | break; |
James Hogan | 70e92c7e | 2016-07-04 19:35:11 +0100 | [diff] [blame] | 618 | #ifndef CONFIG_CPU_MIPSR6 |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 619 | case KVM_REG_MIPS_HI: |
| 620 | v = (long)vcpu->arch.hi; |
| 621 | break; |
| 622 | case KVM_REG_MIPS_LO: |
| 623 | v = (long)vcpu->arch.lo; |
| 624 | break; |
James Hogan | 70e92c7e | 2016-07-04 19:35:11 +0100 | [diff] [blame] | 625 | #endif |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 626 | case KVM_REG_MIPS_PC: |
| 627 | v = (long)vcpu->arch.pc; |
| 628 | break; |
| 629 | |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 630 | /* Floating point registers */ |
| 631 | case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31): |
| 632 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 633 | return -EINVAL; |
| 634 | idx = reg->id - KVM_REG_MIPS_FPR_32(0); |
| 635 | /* Odd singles in top of even double when FR=0 */ |
| 636 | if (kvm_read_c0_guest_status(cop0) & ST0_FR) |
| 637 | v = get_fpr32(&fpu->fpr[idx], 0); |
| 638 | else |
| 639 | v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1); |
| 640 | break; |
| 641 | case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31): |
| 642 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 643 | return -EINVAL; |
| 644 | idx = reg->id - KVM_REG_MIPS_FPR_64(0); |
| 645 | /* Can't access odd doubles in FR=0 mode */ |
| 646 | if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR)) |
| 647 | return -EINVAL; |
| 648 | v = get_fpr64(&fpu->fpr[idx], 0); |
| 649 | break; |
| 650 | case KVM_REG_MIPS_FCR_IR: |
| 651 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 652 | return -EINVAL; |
| 653 | v = boot_cpu_data.fpu_id; |
| 654 | break; |
| 655 | case KVM_REG_MIPS_FCR_CSR: |
| 656 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 657 | return -EINVAL; |
| 658 | v = fpu->fcr31; |
| 659 | break; |
| 660 | |
James Hogan | ab86bd6 | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 661 | /* MIPS SIMD Architecture (MSA) registers */ |
| 662 | case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31): |
| 663 | if (!kvm_mips_guest_has_msa(&vcpu->arch)) |
| 664 | return -EINVAL; |
| 665 | /* Can't access MSA registers in FR=0 mode */ |
| 666 | if (!(kvm_read_c0_guest_status(cop0) & ST0_FR)) |
| 667 | return -EINVAL; |
| 668 | idx = reg->id - KVM_REG_MIPS_VEC_128(0); |
| 669 | #ifdef CONFIG_CPU_LITTLE_ENDIAN |
| 670 | /* least significant byte first */ |
| 671 | vs[0] = get_fpr64(&fpu->fpr[idx], 0); |
| 672 | vs[1] = get_fpr64(&fpu->fpr[idx], 1); |
| 673 | #else |
| 674 | /* most significant byte first */ |
| 675 | vs[0] = get_fpr64(&fpu->fpr[idx], 1); |
| 676 | vs[1] = get_fpr64(&fpu->fpr[idx], 0); |
| 677 | #endif |
| 678 | break; |
| 679 | case KVM_REG_MIPS_MSA_IR: |
| 680 | if (!kvm_mips_guest_has_msa(&vcpu->arch)) |
| 681 | return -EINVAL; |
| 682 | v = boot_cpu_data.msa_id; |
| 683 | break; |
| 684 | case KVM_REG_MIPS_MSA_CSR: |
| 685 | if (!kvm_mips_guest_has_msa(&vcpu->arch)) |
| 686 | return -EINVAL; |
| 687 | v = fpu->msacsr; |
| 688 | break; |
| 689 | |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 690 | /* registers to be handled specially */ |
James Hogan | cc68d22 | 2016-06-15 19:29:48 +0100 | [diff] [blame] | 691 | default: |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 692 | ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 696 | } |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 697 | if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { |
| 698 | u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 699 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 700 | return put_user(v, uaddr64); |
| 701 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { |
| 702 | u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; |
| 703 | u32 v32 = (u32)v; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 704 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 705 | return put_user(v32, uaddr32); |
James Hogan | ab86bd6 | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 706 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) { |
| 707 | void __user *uaddr = (void __user *)(long)reg->addr; |
| 708 | |
Michael S. Tsirkin | 0178fd7 | 2016-02-28 17:35:59 +0200 | [diff] [blame] | 709 | return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0; |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 710 | } else { |
| 711 | return -EINVAL; |
| 712 | } |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static int kvm_mips_set_reg(struct kvm_vcpu *vcpu, |
| 716 | const struct kvm_one_reg *reg) |
| 717 | { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 718 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 719 | struct mips_fpu_struct *fpu = &vcpu->arch.fpu; |
| 720 | s64 v; |
James Hogan | ab86bd6 | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 721 | s64 vs[2]; |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 722 | unsigned int idx; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 723 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 724 | if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { |
| 725 | u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; |
| 726 | |
| 727 | if (get_user(v, uaddr64) != 0) |
| 728 | return -EFAULT; |
| 729 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { |
| 730 | u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; |
| 731 | s32 v32; |
| 732 | |
| 733 | if (get_user(v32, uaddr32) != 0) |
| 734 | return -EFAULT; |
| 735 | v = (s64)v32; |
James Hogan | ab86bd6 | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 736 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) { |
| 737 | void __user *uaddr = (void __user *)(long)reg->addr; |
| 738 | |
Michael S. Tsirkin | 0178fd7 | 2016-02-28 17:35:59 +0200 | [diff] [blame] | 739 | return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0; |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 740 | } else { |
| 741 | return -EINVAL; |
| 742 | } |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 743 | |
| 744 | switch (reg->id) { |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 745 | /* General purpose registers */ |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 746 | case KVM_REG_MIPS_R0: |
| 747 | /* Silently ignore requests to set $0 */ |
| 748 | break; |
| 749 | case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31: |
| 750 | vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v; |
| 751 | break; |
James Hogan | 70e92c7e | 2016-07-04 19:35:11 +0100 | [diff] [blame] | 752 | #ifndef CONFIG_CPU_MIPSR6 |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 753 | case KVM_REG_MIPS_HI: |
| 754 | vcpu->arch.hi = v; |
| 755 | break; |
| 756 | case KVM_REG_MIPS_LO: |
| 757 | vcpu->arch.lo = v; |
| 758 | break; |
James Hogan | 70e92c7e | 2016-07-04 19:35:11 +0100 | [diff] [blame] | 759 | #endif |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 760 | case KVM_REG_MIPS_PC: |
| 761 | vcpu->arch.pc = v; |
| 762 | break; |
| 763 | |
James Hogan | 379245c | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 764 | /* Floating point registers */ |
| 765 | case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31): |
| 766 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 767 | return -EINVAL; |
| 768 | idx = reg->id - KVM_REG_MIPS_FPR_32(0); |
| 769 | /* Odd singles in top of even double when FR=0 */ |
| 770 | if (kvm_read_c0_guest_status(cop0) & ST0_FR) |
| 771 | set_fpr32(&fpu->fpr[idx], 0, v); |
| 772 | else |
| 773 | set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v); |
| 774 | break; |
| 775 | case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31): |
| 776 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 777 | return -EINVAL; |
| 778 | idx = reg->id - KVM_REG_MIPS_FPR_64(0); |
| 779 | /* Can't access odd doubles in FR=0 mode */ |
| 780 | if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR)) |
| 781 | return -EINVAL; |
| 782 | set_fpr64(&fpu->fpr[idx], 0, v); |
| 783 | break; |
| 784 | case KVM_REG_MIPS_FCR_IR: |
| 785 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 786 | return -EINVAL; |
| 787 | /* Read-only */ |
| 788 | break; |
| 789 | case KVM_REG_MIPS_FCR_CSR: |
| 790 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 791 | return -EINVAL; |
| 792 | fpu->fcr31 = v; |
| 793 | break; |
| 794 | |
James Hogan | ab86bd6 | 2014-12-02 15:48:24 +0000 | [diff] [blame] | 795 | /* MIPS SIMD Architecture (MSA) registers */ |
| 796 | case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31): |
| 797 | if (!kvm_mips_guest_has_msa(&vcpu->arch)) |
| 798 | return -EINVAL; |
| 799 | idx = reg->id - KVM_REG_MIPS_VEC_128(0); |
| 800 | #ifdef CONFIG_CPU_LITTLE_ENDIAN |
| 801 | /* least significant byte first */ |
| 802 | set_fpr64(&fpu->fpr[idx], 0, vs[0]); |
| 803 | set_fpr64(&fpu->fpr[idx], 1, vs[1]); |
| 804 | #else |
| 805 | /* most significant byte first */ |
| 806 | set_fpr64(&fpu->fpr[idx], 1, vs[0]); |
| 807 | set_fpr64(&fpu->fpr[idx], 0, vs[1]); |
| 808 | #endif |
| 809 | break; |
| 810 | case KVM_REG_MIPS_MSA_IR: |
| 811 | if (!kvm_mips_guest_has_msa(&vcpu->arch)) |
| 812 | return -EINVAL; |
| 813 | /* Read-only */ |
| 814 | break; |
| 815 | case KVM_REG_MIPS_MSA_CSR: |
| 816 | if (!kvm_mips_guest_has_msa(&vcpu->arch)) |
| 817 | return -EINVAL; |
| 818 | fpu->msacsr = v; |
| 819 | break; |
| 820 | |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 821 | /* registers to be handled specially */ |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 822 | default: |
James Hogan | cc68d22 | 2016-06-15 19:29:48 +0100 | [diff] [blame] | 823 | return kvm_mips_callbacks->set_one_reg(vcpu, reg, v); |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 828 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 829 | struct kvm_enable_cap *cap) |
| 830 | { |
| 831 | int r = 0; |
| 832 | |
| 833 | if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap)) |
| 834 | return -EINVAL; |
| 835 | if (cap->flags) |
| 836 | return -EINVAL; |
| 837 | if (cap->args[0]) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | switch (cap->cap) { |
| 841 | case KVM_CAP_MIPS_FPU: |
| 842 | vcpu->arch.fpu_enabled = true; |
| 843 | break; |
James Hogan | d952bd0 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 844 | case KVM_CAP_MIPS_MSA: |
| 845 | vcpu->arch.msa_enabled = true; |
| 846 | break; |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 847 | default: |
| 848 | r = -EINVAL; |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | return r; |
| 853 | } |
| 854 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 855 | long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, |
| 856 | unsigned long arg) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 857 | { |
| 858 | struct kvm_vcpu *vcpu = filp->private_data; |
| 859 | void __user *argp = (void __user *)arg; |
| 860 | long r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 861 | |
| 862 | switch (ioctl) { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 863 | case KVM_SET_ONE_REG: |
| 864 | case KVM_GET_ONE_REG: { |
| 865 | struct kvm_one_reg reg; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 866 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 867 | if (copy_from_user(®, argp, sizeof(reg))) |
| 868 | return -EFAULT; |
| 869 | if (ioctl == KVM_SET_ONE_REG) |
| 870 | return kvm_mips_set_reg(vcpu, ®); |
| 871 | else |
| 872 | return kvm_mips_get_reg(vcpu, ®); |
| 873 | } |
| 874 | case KVM_GET_REG_LIST: { |
| 875 | struct kvm_reg_list __user *user_list = argp; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 876 | struct kvm_reg_list reg_list; |
| 877 | unsigned n; |
| 878 | |
| 879 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
| 880 | return -EFAULT; |
| 881 | n = reg_list.n; |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 882 | reg_list.n = kvm_mips_num_regs(vcpu); |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 883 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
| 884 | return -EFAULT; |
| 885 | if (n < reg_list.n) |
| 886 | return -E2BIG; |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 887 | return kvm_mips_copy_reg_indices(vcpu, user_list->reg); |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 888 | } |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 889 | case KVM_INTERRUPT: |
| 890 | { |
| 891 | struct kvm_mips_interrupt irq; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 892 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 893 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Markus Elfring | 5a6da5f | 2017-01-19 11:10:26 +0100 | [diff] [blame] | 894 | return -EFAULT; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 895 | kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__, |
| 896 | irq.irq); |
| 897 | |
| 898 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 899 | break; |
| 900 | } |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 901 | case KVM_ENABLE_CAP: { |
| 902 | struct kvm_enable_cap cap; |
| 903 | |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 904 | if (copy_from_user(&cap, argp, sizeof(cap))) |
Markus Elfring | 5a6da5f | 2017-01-19 11:10:26 +0100 | [diff] [blame] | 905 | return -EFAULT; |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 906 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 907 | break; |
| 908 | } |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 909 | default: |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 910 | r = -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 911 | } |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 912 | return r; |
| 913 | } |
| 914 | |
James Hogan | e88643b | 2016-12-06 14:50:52 +0000 | [diff] [blame] | 915 | /** |
| 916 | * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot |
| 917 | * @kvm: kvm instance |
| 918 | * @log: slot id and address to which we copy the log |
| 919 | * |
| 920 | * Steps 1-4 below provide general overview of dirty page logging. See |
| 921 | * kvm_get_dirty_log_protect() function description for additional details. |
| 922 | * |
| 923 | * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we |
| 924 | * always flush the TLB (step 4) even if previous step failed and the dirty |
| 925 | * bitmap may be corrupt. Regardless of previous outcome the KVM logging API |
| 926 | * does not preclude user space subsequent dirty log read. Flushing TLB ensures |
| 927 | * writes will be marked dirty for next log read. |
| 928 | * |
| 929 | * 1. Take a snapshot of the bit and clear it if needed. |
| 930 | * 2. Write protect the corresponding page. |
| 931 | * 3. Copy the snapshot to the userspace. |
| 932 | * 4. Flush TLB's if needed. |
| 933 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 934 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
| 935 | { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 936 | struct kvm_memslots *slots; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 937 | struct kvm_memory_slot *memslot; |
James Hogan | e88643b | 2016-12-06 14:50:52 +0000 | [diff] [blame] | 938 | bool is_dirty = false; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 939 | int r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 940 | |
| 941 | mutex_lock(&kvm->slots_lock); |
| 942 | |
James Hogan | e88643b | 2016-12-06 14:50:52 +0000 | [diff] [blame] | 943 | r = kvm_get_dirty_log_protect(kvm, log, &is_dirty); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 944 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 945 | if (is_dirty) { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 946 | slots = kvm_memslots(kvm); |
| 947 | memslot = id_to_memslot(slots, log->slot); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 948 | |
James Hogan | e88643b | 2016-12-06 14:50:52 +0000 | [diff] [blame] | 949 | /* Let implementation handle TLB/GVA invalidation */ |
| 950 | kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 953 | mutex_unlock(&kvm->slots_lock); |
| 954 | return r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) |
| 958 | { |
| 959 | long r; |
| 960 | |
| 961 | switch (ioctl) { |
| 962 | default: |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 963 | r = -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | return r; |
| 967 | } |
| 968 | |
| 969 | int kvm_arch_init(void *opaque) |
| 970 | { |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 971 | if (kvm_mips_callbacks) { |
| 972 | kvm_err("kvm: module already exists\n"); |
| 973 | return -EEXIST; |
| 974 | } |
| 975 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 976 | return kvm_mips_emulation_init(&kvm_mips_callbacks); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void kvm_arch_exit(void) |
| 980 | { |
| 981 | kvm_mips_callbacks = NULL; |
| 982 | } |
| 983 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 984 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 985 | struct kvm_sregs *sregs) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 986 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 987 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 990 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 991 | struct kvm_sregs *sregs) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 992 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 993 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Dominik Dingel | 31928aa | 2014-12-04 15:47:07 +0100 | [diff] [blame] | 996 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 997 | { |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1001 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 1002 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1006 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 1007 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 1011 | { |
| 1012 | return VM_FAULT_SIGBUS; |
| 1013 | } |
| 1014 | |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 1015 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1016 | { |
| 1017 | int r; |
| 1018 | |
| 1019 | switch (ext) { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 1020 | case KVM_CAP_ONE_REG: |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 1021 | case KVM_CAP_ENABLE_CAP: |
James Hogan | 230c572 | 2015-05-08 17:11:49 +0100 | [diff] [blame] | 1022 | case KVM_CAP_READONLY_MEM: |
James Hogan | 411740f | 2016-12-13 16:32:39 +0000 | [diff] [blame] | 1023 | case KVM_CAP_SYNC_MMU: |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 1024 | r = 1; |
| 1025 | break; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1026 | case KVM_CAP_COALESCED_MMIO: |
| 1027 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 1028 | break; |
James Hogan | 12ed1fa | 2016-12-13 22:39:39 +0000 | [diff] [blame] | 1029 | case KVM_CAP_NR_VCPUS: |
| 1030 | r = num_online_cpus(); |
| 1031 | break; |
| 1032 | case KVM_CAP_MAX_VCPUS: |
| 1033 | r = KVM_MAX_VCPUS; |
| 1034 | break; |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 1035 | case KVM_CAP_MIPS_FPU: |
James Hogan | 556f2a5 | 2016-04-22 10:38:48 +0100 | [diff] [blame] | 1036 | /* We don't handle systems with inconsistent cpu_has_fpu */ |
| 1037 | r = !!raw_cpu_has_fpu; |
James Hogan | 5fafd874 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 1038 | break; |
James Hogan | d952bd0 | 2014-12-08 23:07:56 +0000 | [diff] [blame] | 1039 | case KVM_CAP_MIPS_MSA: |
| 1040 | /* |
| 1041 | * We don't support MSA vector partitioning yet: |
| 1042 | * 1) It would require explicit support which can't be tested |
| 1043 | * yet due to lack of support in current hardware. |
| 1044 | * 2) It extends the state that would need to be saved/restored |
| 1045 | * by e.g. QEMU for migration. |
| 1046 | * |
| 1047 | * When vector partitioning hardware becomes available, support |
| 1048 | * could be added by requiring a flag when enabling |
| 1049 | * KVM_CAP_MIPS_MSA capability to indicate that userland knows |
| 1050 | * to save/restore the appropriate extra state. |
| 1051 | */ |
| 1052 | r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF); |
| 1053 | break; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1054 | default: |
| 1055 | r = 0; |
| 1056 | break; |
| 1057 | } |
| 1058 | return r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 1062 | { |
| 1063 | return kvm_mips_pending_timer(vcpu); |
| 1064 | } |
| 1065 | |
| 1066 | int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu) |
| 1067 | { |
| 1068 | int i; |
| 1069 | struct mips_coproc *cop0; |
| 1070 | |
| 1071 | if (!vcpu) |
| 1072 | return -1; |
| 1073 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1074 | kvm_debug("VCPU Register Dump:\n"); |
| 1075 | kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc); |
| 1076 | kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1077 | |
| 1078 | for (i = 0; i < 32; i += 4) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1079 | kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i, |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1080 | vcpu->arch.gprs[i], |
| 1081 | vcpu->arch.gprs[i + 1], |
| 1082 | vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]); |
| 1083 | } |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1084 | kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi); |
| 1085 | kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1086 | |
| 1087 | cop0 = vcpu->arch.cop0; |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1088 | kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n", |
| 1089 | kvm_read_c0_guest_status(cop0), |
| 1090 | kvm_read_c0_guest_cause(cop0)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1091 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1092 | kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1098 | { |
| 1099 | int i; |
| 1100 | |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 1101 | for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++) |
David Daney | bf32ebf | 2013-05-23 09:49:07 -0700 | [diff] [blame] | 1102 | vcpu->arch.gprs[i] = regs->gpr[i]; |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 1103 | vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1104 | vcpu->arch.hi = regs->hi; |
| 1105 | vcpu->arch.lo = regs->lo; |
| 1106 | vcpu->arch.pc = regs->pc; |
| 1107 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 1108 | return 0; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1112 | { |
| 1113 | int i; |
| 1114 | |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 1115 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++) |
David Daney | bf32ebf | 2013-05-23 09:49:07 -0700 | [diff] [blame] | 1116 | regs->gpr[i] = vcpu->arch.gprs[i]; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1117 | |
| 1118 | regs->hi = vcpu->arch.hi; |
| 1119 | regs->lo = vcpu->arch.lo; |
| 1120 | regs->pc = vcpu->arch.pc; |
| 1121 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 1122 | return 0; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
James Hogan | 0fae34f | 2014-05-29 10:16:39 +0100 | [diff] [blame] | 1125 | static void kvm_mips_comparecount_func(unsigned long data) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1126 | { |
| 1127 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 1128 | |
| 1129 | kvm_mips_callbacks->queue_timer_int(vcpu); |
| 1130 | |
| 1131 | vcpu->arch.wait = 0; |
Marcelo Tosatti | 8577370 | 2016-02-19 09:46:39 +0100 | [diff] [blame] | 1132 | if (swait_active(&vcpu->wq)) |
| 1133 | swake_up(&vcpu->wq); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1136 | /* low level hrtimer wake routine */ |
James Hogan | 0fae34f | 2014-05-29 10:16:39 +0100 | [diff] [blame] | 1137 | static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1138 | { |
| 1139 | struct kvm_vcpu *vcpu; |
| 1140 | |
| 1141 | vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer); |
| 1142 | kvm_mips_comparecount_func((unsigned long) vcpu); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 1143 | return kvm_mips_count_timeout(vcpu); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 1147 | { |
James Hogan | f7f1427 | 2016-09-08 22:57:03 +0100 | [diff] [blame] | 1148 | int err; |
| 1149 | |
| 1150 | err = kvm_mips_callbacks->vcpu_init(vcpu); |
| 1151 | if (err) |
| 1152 | return err; |
| 1153 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1154 | hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC, |
| 1155 | HRTIMER_MODE_REL); |
| 1156 | vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1157 | return 0; |
| 1158 | } |
| 1159 | |
James Hogan | 630766b | 2016-09-08 23:00:24 +0100 | [diff] [blame] | 1160 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 1161 | { |
| 1162 | kvm_mips_callbacks->vcpu_uninit(vcpu); |
| 1163 | } |
| 1164 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1165 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 1166 | struct kvm_translation *tr) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1167 | { |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | /* Initial guest state */ |
| 1172 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1173 | { |
| 1174 | return kvm_mips_callbacks->vcpu_setup(vcpu); |
| 1175 | } |
| 1176 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1177 | static void kvm_mips_set_c0_status(void) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1178 | { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1179 | u32 status = read_c0_status(); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1180 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1181 | if (cpu_has_dsp) |
| 1182 | status |= (ST0_MX); |
| 1183 | |
| 1184 | write_c0_status(status); |
| 1185 | ehb(); |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) |
| 1190 | */ |
| 1191 | int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 1192 | { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1193 | u32 cause = vcpu->arch.host_cp0_cause; |
| 1194 | u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; |
| 1195 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1196 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 1197 | enum emulation_result er = EMULATE_DONE; |
James Hogan | 122e51d | 2016-11-28 17:23:14 +0000 | [diff] [blame] | 1198 | u32 inst; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1199 | int ret = RESUME_GUEST; |
| 1200 | |
James Hogan | 4841e0d | 2016-11-28 22:45:04 +0000 | [diff] [blame] | 1201 | vcpu->mode = OUTSIDE_GUEST_MODE; |
| 1202 | |
James Hogan | c4c6f2c | 2015-02-04 10:52:03 +0000 | [diff] [blame] | 1203 | /* re-enable HTW before enabling interrupts */ |
| 1204 | htw_start(); |
| 1205 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1206 | /* Set a default exit reason */ |
| 1207 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1208 | run->ready_for_interrupt_injection = 1; |
| 1209 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1210 | /* |
| 1211 | * Set the appropriate status bits based on host CPU features, |
| 1212 | * before we hit the scheduler |
| 1213 | */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1214 | kvm_mips_set_c0_status(); |
| 1215 | |
| 1216 | local_irq_enable(); |
| 1217 | |
| 1218 | kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n", |
| 1219 | cause, opc, run, vcpu); |
James Hogan | 1e09e86 | 2016-06-14 09:40:12 +0100 | [diff] [blame] | 1220 | trace_kvm_exit(vcpu, exccode); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1221 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1222 | /* |
| 1223 | * 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] | 1224 | * causing an exception to be delivered to the Guest Kernel |
| 1225 | */ |
| 1226 | er = kvm_mips_check_privilege(cause, opc, run, vcpu); |
| 1227 | if (er == EMULATE_PRIV_FAIL) { |
| 1228 | goto skip_emul; |
| 1229 | } else if (er == EMULATE_FAIL) { |
| 1230 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1231 | ret = RESUME_HOST; |
| 1232 | goto skip_emul; |
| 1233 | } |
| 1234 | |
| 1235 | switch (exccode) { |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1236 | case EXCCODE_INT: |
| 1237 | kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1238 | |
| 1239 | ++vcpu->stat.int_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1240 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1241 | if (need_resched()) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1242 | cond_resched(); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1243 | |
| 1244 | ret = RESUME_GUEST; |
| 1245 | break; |
| 1246 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1247 | case EXCCODE_CPU: |
| 1248 | kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1249 | |
| 1250 | ++vcpu->stat.cop_unusable_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1251 | ret = kvm_mips_callbacks->handle_cop_unusable(vcpu); |
| 1252 | /* XXXKYMA: Might need to return to user space */ |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1253 | if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1254 | ret = RESUME_HOST; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1255 | break; |
| 1256 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1257 | case EXCCODE_MOD: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1258 | ++vcpu->stat.tlbmod_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1259 | ret = kvm_mips_callbacks->handle_tlb_mod(vcpu); |
| 1260 | break; |
| 1261 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1262 | case EXCCODE_TLBS: |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1263 | kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n", |
| 1264 | cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc, |
| 1265 | badvaddr); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1266 | |
| 1267 | ++vcpu->stat.tlbmiss_st_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1268 | ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu); |
| 1269 | break; |
| 1270 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1271 | case EXCCODE_TLBL: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1272 | kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n", |
| 1273 | cause, opc, badvaddr); |
| 1274 | |
| 1275 | ++vcpu->stat.tlbmiss_ld_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1276 | ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu); |
| 1277 | break; |
| 1278 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1279 | case EXCCODE_ADES: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1280 | ++vcpu->stat.addrerr_st_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1281 | ret = kvm_mips_callbacks->handle_addr_err_st(vcpu); |
| 1282 | break; |
| 1283 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1284 | case EXCCODE_ADEL: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1285 | ++vcpu->stat.addrerr_ld_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1286 | ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu); |
| 1287 | break; |
| 1288 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1289 | case EXCCODE_SYS: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1290 | ++vcpu->stat.syscall_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1291 | ret = kvm_mips_callbacks->handle_syscall(vcpu); |
| 1292 | break; |
| 1293 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1294 | case EXCCODE_RI: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1295 | ++vcpu->stat.resvd_inst_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1296 | ret = kvm_mips_callbacks->handle_res_inst(vcpu); |
| 1297 | break; |
| 1298 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1299 | case EXCCODE_BP: |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1300 | ++vcpu->stat.break_inst_exits; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1301 | ret = kvm_mips_callbacks->handle_break(vcpu); |
| 1302 | break; |
| 1303 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1304 | case EXCCODE_TR: |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 1305 | ++vcpu->stat.trap_inst_exits; |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 1306 | ret = kvm_mips_callbacks->handle_trap(vcpu); |
| 1307 | break; |
| 1308 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1309 | case EXCCODE_MSAFPE: |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 1310 | ++vcpu->stat.msa_fpe_exits; |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 1311 | ret = kvm_mips_callbacks->handle_msa_fpe(vcpu); |
| 1312 | break; |
| 1313 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1314 | case EXCCODE_FPE: |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 1315 | ++vcpu->stat.fpe_exits; |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 1316 | ret = kvm_mips_callbacks->handle_fpe(vcpu); |
| 1317 | break; |
| 1318 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1319 | case EXCCODE_MSADIS: |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 1320 | ++vcpu->stat.msa_disabled_exits; |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 1321 | ret = kvm_mips_callbacks->handle_msa_disabled(vcpu); |
| 1322 | break; |
| 1323 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1324 | default: |
James Hogan | 122e51d | 2016-11-28 17:23:14 +0000 | [diff] [blame] | 1325 | if (cause & CAUSEF_BD) |
| 1326 | opc += 1; |
| 1327 | inst = 0; |
James Hogan | 6a97c77 | 2015-04-23 16:54:35 +0100 | [diff] [blame] | 1328 | kvm_get_badinstr(opc, vcpu, &inst); |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1329 | kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n", |
James Hogan | 122e51d | 2016-11-28 17:23:14 +0000 | [diff] [blame] | 1330 | exccode, opc, inst, badvaddr, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1331 | kvm_read_c0_guest_status(vcpu->arch.cop0)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1332 | kvm_arch_vcpu_dump_regs(vcpu); |
| 1333 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1334 | ret = RESUME_HOST; |
| 1335 | break; |
| 1336 | |
| 1337 | } |
| 1338 | |
| 1339 | skip_emul: |
| 1340 | local_irq_disable(); |
| 1341 | |
| 1342 | if (er == EMULATE_DONE && !(ret & RESUME_HOST)) |
| 1343 | kvm_mips_deliver_interrupts(vcpu, cause); |
| 1344 | |
| 1345 | if (!(ret & RESUME_HOST)) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1346 | /* Only check for signals if not already exiting to userspace */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1347 | if (signal_pending(current)) { |
| 1348 | run->exit_reason = KVM_EXIT_INTR; |
| 1349 | ret = (-EINTR << 2) | RESUME_HOST; |
| 1350 | ++vcpu->stat.signal_exits; |
James Hogan | 1e09e86 | 2016-06-14 09:40:12 +0100 | [diff] [blame] | 1351 | trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1355 | if (ret == RESUME_GUEST) { |
James Hogan | 9325860 | 2016-06-14 09:40:14 +0100 | [diff] [blame] | 1356 | trace_kvm_reenter(vcpu); |
| 1357 | |
James Hogan | 4841e0d | 2016-11-28 22:45:04 +0000 | [diff] [blame] | 1358 | /* |
| 1359 | * Make sure the read of VCPU requests in vcpu_reenter() |
| 1360 | * callback is not reordered ahead of the write to vcpu->mode, |
| 1361 | * or we could miss a TLB flush request while the requester sees |
| 1362 | * the VCPU as outside of guest mode and not needing an IPI. |
| 1363 | */ |
| 1364 | smp_store_mb(vcpu->mode, IN_GUEST_MODE); |
| 1365 | |
James Hogan | a2c046e | 2016-11-18 13:14:37 +0000 | [diff] [blame] | 1366 | kvm_mips_callbacks->vcpu_reenter(run, vcpu); |
James Hogan | 25b08c7 | 2016-09-16 00:06:43 +0100 | [diff] [blame] | 1367 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1368 | /* |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1369 | * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context |
| 1370 | * is live), restore FCR31 / MSACSR. |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1371 | * |
| 1372 | * This should be before returning to the guest exception |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1373 | * vector, as it may well cause an [MSA] FP exception if there |
| 1374 | * are pending exception bits unmasked. (see |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1375 | * kvm_mips_csr_die_notifier() for how that is handled). |
| 1376 | */ |
| 1377 | if (kvm_mips_guest_has_fpu(&vcpu->arch) && |
| 1378 | read_c0_status() & ST0_CU1) |
| 1379 | __kvm_restore_fcsr(&vcpu->arch); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (kvm_mips_guest_has_msa(&vcpu->arch) && |
| 1382 | read_c0_config5() & MIPS_CONF5_MSAEN) |
| 1383 | __kvm_restore_msacsr(&vcpu->arch); |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
James Hogan | c4c6f2c | 2015-02-04 10:52:03 +0000 | [diff] [blame] | 1386 | /* Disable HTW before returning to guest or host */ |
| 1387 | htw_stop(); |
| 1388 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1389 | return ret; |
| 1390 | } |
| 1391 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1392 | /* Enable FPU for guest and restore context */ |
| 1393 | void kvm_own_fpu(struct kvm_vcpu *vcpu) |
| 1394 | { |
| 1395 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 1396 | unsigned int sr, cfg5; |
| 1397 | |
| 1398 | preempt_disable(); |
| 1399 | |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1400 | sr = kvm_read_c0_guest_status(cop0); |
| 1401 | |
| 1402 | /* |
| 1403 | * If MSA state is already live, it is undefined how it interacts with |
| 1404 | * FR=0 FPU state, and we don't want to hit reserved instruction |
| 1405 | * exceptions trying to save the MSA state later when CU=1 && FR=1, so |
| 1406 | * play it safe and save it first. |
| 1407 | * |
| 1408 | * In theory we shouldn't ever hit this case since kvm_lose_fpu() should |
| 1409 | * get called when guest CU1 is set, however we can't trust the guest |
| 1410 | * not to clobber the status register directly via the commpage. |
| 1411 | */ |
| 1412 | if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) && |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1413 | vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1414 | kvm_lose_fpu(vcpu); |
| 1415 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1416 | /* |
| 1417 | * Enable FPU for guest |
| 1418 | * We set FR and FRE according to guest context |
| 1419 | */ |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1420 | change_c0_status(ST0_CU1 | ST0_FR, sr); |
| 1421 | if (cpu_has_fre) { |
| 1422 | cfg5 = kvm_read_c0_guest_config5(cop0); |
| 1423 | change_c0_config5(MIPS_CONF5_FRE, cfg5); |
| 1424 | } |
| 1425 | enable_fpu_hazard(); |
| 1426 | |
| 1427 | /* If guest FPU state not active, restore it now */ |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1428 | if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) { |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1429 | __kvm_restore_fpu(&vcpu->arch); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1430 | vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU; |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1431 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU); |
| 1432 | } else { |
| 1433 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU); |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | preempt_enable(); |
| 1437 | } |
| 1438 | |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1439 | #ifdef CONFIG_CPU_HAS_MSA |
| 1440 | /* Enable MSA for guest and restore context */ |
| 1441 | void kvm_own_msa(struct kvm_vcpu *vcpu) |
| 1442 | { |
| 1443 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 1444 | unsigned int sr, cfg5; |
| 1445 | |
| 1446 | preempt_disable(); |
| 1447 | |
| 1448 | /* |
| 1449 | * Enable FPU if enabled in guest, since we're restoring FPU context |
| 1450 | * anyway. We set FR and FRE according to guest context. |
| 1451 | */ |
| 1452 | if (kvm_mips_guest_has_fpu(&vcpu->arch)) { |
| 1453 | sr = kvm_read_c0_guest_status(cop0); |
| 1454 | |
| 1455 | /* |
| 1456 | * If FR=0 FPU state is already live, it is undefined how it |
| 1457 | * interacts with MSA state, so play it safe and save it first. |
| 1458 | */ |
| 1459 | if (!(sr & ST0_FR) && |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1460 | (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | |
| 1461 | KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU) |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1462 | kvm_lose_fpu(vcpu); |
| 1463 | |
| 1464 | change_c0_status(ST0_CU1 | ST0_FR, sr); |
| 1465 | if (sr & ST0_CU1 && cpu_has_fre) { |
| 1466 | cfg5 = kvm_read_c0_guest_config5(cop0); |
| 1467 | change_c0_config5(MIPS_CONF5_FRE, cfg5); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | /* Enable MSA for guest */ |
| 1472 | set_c0_config5(MIPS_CONF5_MSAEN); |
| 1473 | enable_fpu_hazard(); |
| 1474 | |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1475 | switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) { |
| 1476 | case KVM_MIPS_AUX_FPU: |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1477 | /* |
| 1478 | * Guest FPU state already loaded, only restore upper MSA state |
| 1479 | */ |
| 1480 | __kvm_restore_msa_upper(&vcpu->arch); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1481 | vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA; |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1482 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1483 | break; |
| 1484 | case 0: |
| 1485 | /* Neither FPU or MSA already active, restore full MSA state */ |
| 1486 | __kvm_restore_msa(&vcpu->arch); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1487 | vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA; |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1488 | if (kvm_mips_guest_has_fpu(&vcpu->arch)) |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1489 | vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU; |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1490 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, |
| 1491 | KVM_TRACE_AUX_FPU_MSA); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1492 | break; |
| 1493 | default: |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1494 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | preempt_enable(); |
| 1499 | } |
| 1500 | #endif |
| 1501 | |
| 1502 | /* Drop FPU & MSA without saving it */ |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1503 | void kvm_drop_fpu(struct kvm_vcpu *vcpu) |
| 1504 | { |
| 1505 | preempt_disable(); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1506 | if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) { |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1507 | disable_msa(); |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1508 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1509 | vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA; |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1510 | } |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1511 | if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) { |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1512 | clear_c0_status(ST0_CU1 | ST0_FR); |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1513 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1514 | vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU; |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1515 | } |
| 1516 | preempt_enable(); |
| 1517 | } |
| 1518 | |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1519 | /* Save and disable FPU & MSA */ |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1520 | void kvm_lose_fpu(struct kvm_vcpu *vcpu) |
| 1521 | { |
| 1522 | /* |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1523 | * FPU & MSA get disabled in root context (hardware) when it is disabled |
| 1524 | * in guest context (software), but the register state in the hardware |
| 1525 | * may still be in use. This is why we explicitly re-enable the hardware |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1526 | * before saving. |
| 1527 | */ |
| 1528 | |
| 1529 | preempt_disable(); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1530 | if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) { |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1531 | set_c0_config5(MIPS_CONF5_MSAEN); |
| 1532 | enable_fpu_hazard(); |
| 1533 | |
| 1534 | __kvm_save_msa(&vcpu->arch); |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1535 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA); |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1536 | |
| 1537 | /* Disable MSA & FPU */ |
| 1538 | disable_msa(); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1539 | if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) { |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1540 | clear_c0_status(ST0_CU1 | ST0_FR); |
James Hogan | 4ac3342 | 2016-04-22 10:38:49 +0100 | [diff] [blame] | 1541 | disable_fpu_hazard(); |
| 1542 | } |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1543 | vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA); |
| 1544 | } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) { |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1545 | set_c0_status(ST0_CU1); |
| 1546 | enable_fpu_hazard(); |
| 1547 | |
| 1548 | __kvm_save_fpu(&vcpu->arch); |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1549 | vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU; |
James Hogan | 04ebebf | 2016-06-14 09:40:11 +0100 | [diff] [blame] | 1550 | trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU); |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* Disable FPU */ |
| 1553 | clear_c0_status(ST0_CU1 | ST0_FR); |
James Hogan | 4ac3342 | 2016-04-22 10:38:49 +0100 | [diff] [blame] | 1554 | disable_fpu_hazard(); |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1555 | } |
| 1556 | preempt_enable(); |
| 1557 | } |
| 1558 | |
| 1559 | /* |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1560 | * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are |
| 1561 | * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP |
| 1562 | * exception if cause bits are set in the value being written. |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1563 | */ |
| 1564 | static int kvm_mips_csr_die_notify(struct notifier_block *self, |
| 1565 | unsigned long cmd, void *ptr) |
| 1566 | { |
| 1567 | struct die_args *args = (struct die_args *)ptr; |
| 1568 | struct pt_regs *regs = args->regs; |
| 1569 | unsigned long pc; |
| 1570 | |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1571 | /* Only interested in FPE and MSAFPE */ |
| 1572 | if (cmd != DIE_FP && cmd != DIE_MSAFP) |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1573 | return NOTIFY_DONE; |
| 1574 | |
| 1575 | /* Return immediately if guest context isn't active */ |
| 1576 | if (!(current->flags & PF_VCPU)) |
| 1577 | return NOTIFY_DONE; |
| 1578 | |
| 1579 | /* Should never get here from user mode */ |
| 1580 | BUG_ON(user_mode(regs)); |
| 1581 | |
| 1582 | pc = instruction_pointer(regs); |
| 1583 | switch (cmd) { |
| 1584 | case DIE_FP: |
| 1585 | /* match 2nd instruction in __kvm_restore_fcsr */ |
| 1586 | if (pc != (unsigned long)&__kvm_restore_fcsr + 4) |
| 1587 | return NOTIFY_DONE; |
| 1588 | break; |
James Hogan | 539cb89fb | 2015-03-05 11:43:36 +0000 | [diff] [blame] | 1589 | case DIE_MSAFP: |
| 1590 | /* match 2nd/3rd instruction in __kvm_restore_msacsr */ |
| 1591 | if (!cpu_has_msa || |
| 1592 | pc < (unsigned long)&__kvm_restore_msacsr + 4 || |
| 1593 | pc > (unsigned long)&__kvm_restore_msacsr + 8) |
| 1594 | return NOTIFY_DONE; |
| 1595 | break; |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* Move PC forward a little and continue executing */ |
| 1599 | instruction_pointer(regs) += 4; |
| 1600 | |
| 1601 | return NOTIFY_STOP; |
| 1602 | } |
| 1603 | |
| 1604 | static struct notifier_block kvm_mips_csr_die_notifier = { |
| 1605 | .notifier_call = kvm_mips_csr_die_notify, |
| 1606 | }; |
| 1607 | |
James Hogan | 2db9d23 | 2015-12-16 23:49:32 +0000 | [diff] [blame] | 1608 | static int __init kvm_mips_init(void) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1609 | { |
| 1610 | int ret; |
| 1611 | |
James Hogan | 1e5217f5 | 2016-06-23 17:34:45 +0100 | [diff] [blame] | 1612 | ret = kvm_mips_entry_setup(); |
| 1613 | if (ret) |
| 1614 | return ret; |
| 1615 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1616 | ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 1617 | |
| 1618 | if (ret) |
| 1619 | return ret; |
| 1620 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1621 | register_die_notifier(&kvm_mips_csr_die_notifier); |
| 1622 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1623 | return 0; |
| 1624 | } |
| 1625 | |
James Hogan | 2db9d23 | 2015-12-16 23:49:32 +0000 | [diff] [blame] | 1626 | static void __exit kvm_mips_exit(void) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1627 | { |
| 1628 | kvm_exit(); |
| 1629 | |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 1630 | unregister_die_notifier(&kvm_mips_csr_die_notifier); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | module_init(kvm_mips_init); |
| 1634 | module_exit(kvm_mips_exit); |
| 1635 | |
| 1636 | EXPORT_TRACEPOINT_SYMBOL(kvm_exit); |