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