Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 1 | /* |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 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: Deliver/Emulate exceptions to the guest kernel |
| 7 | * |
| 8 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 9 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 10 | */ |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/err.h> |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 14 | #include <linux/kvm_host.h> |
James Hogan | 1581ff3 | 2016-11-16 23:48:56 +0000 | [diff] [blame^] | 15 | #include <linux/vmalloc.h> |
| 16 | #include <asm/mmu_context.h> |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 17 | |
Deng-Cheng Zhu | d7d5b05 | 2014-06-26 12:11:38 -0700 | [diff] [blame] | 18 | #include "interrupt.h" |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 19 | |
| 20 | static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva) |
| 21 | { |
| 22 | gpa_t gpa; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 23 | gva_t kseg = KSEGX(gva); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 24 | |
| 25 | if ((kseg == CKSEG0) || (kseg == CKSEG1)) |
| 26 | gpa = CPHYSADDR(gva); |
| 27 | else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 28 | kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 29 | kvm_mips_dump_host_tlbs(); |
| 30 | gpa = KVM_INVALID_ADDR; |
| 31 | } |
| 32 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 33 | kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 34 | |
| 35 | return gpa; |
| 36 | } |
| 37 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 38 | static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu) |
| 39 | { |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 40 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 41 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 42 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 43 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 44 | enum emulation_result er = EMULATE_DONE; |
| 45 | int ret = RESUME_GUEST; |
| 46 | |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 47 | if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) { |
| 48 | /* FPU Unusable */ |
| 49 | if (!kvm_mips_guest_has_fpu(&vcpu->arch) || |
| 50 | (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) { |
| 51 | /* |
| 52 | * Unusable/no FPU in guest: |
| 53 | * deliver guest COP1 Unusable Exception |
| 54 | */ |
| 55 | er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu); |
| 56 | } else { |
| 57 | /* Restore FPU state */ |
| 58 | kvm_own_fpu(vcpu); |
| 59 | er = EMULATE_DONE; |
| 60 | } |
| 61 | } else { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 62 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 63 | } |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 64 | |
| 65 | switch (er) { |
| 66 | case EMULATE_DONE: |
| 67 | ret = RESUME_GUEST; |
| 68 | break; |
| 69 | |
| 70 | case EMULATE_FAIL: |
| 71 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 72 | ret = RESUME_HOST; |
| 73 | break; |
| 74 | |
| 75 | case EMULATE_WAIT: |
| 76 | run->exit_reason = KVM_EXIT_INTR; |
| 77 | ret = RESUME_HOST; |
| 78 | break; |
| 79 | |
| 80 | default: |
| 81 | BUG(); |
| 82 | } |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu) |
| 87 | { |
| 88 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 89 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 90 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 91 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 92 | enum emulation_result er = EMULATE_DONE; |
| 93 | int ret = RESUME_GUEST; |
| 94 | |
| 95 | if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0 |
| 96 | || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) { |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 97 | kvm_debug("USER/KSEG23 ADDR TLB MOD fault: cause %#x, PC: %p, BadVaddr: %#lx\n", |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 98 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 99 | er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu); |
| 100 | |
| 101 | if (er == EMULATE_DONE) |
| 102 | ret = RESUME_GUEST; |
| 103 | else { |
| 104 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 105 | ret = RESUME_HOST; |
| 106 | } |
| 107 | } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 108 | /* |
| 109 | * XXXKYMA: The guest kernel does not expect to get this fault |
| 110 | * when we are not using HIGHMEM. Need to address this in a |
| 111 | * HIGHMEM kernel |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 112 | */ |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 113 | kvm_err("TLB MOD fault not handled, cause %#x, PC: %p, BadVaddr: %#lx\n", |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 114 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 115 | kvm_mips_dump_host_tlbs(); |
| 116 | kvm_arch_vcpu_dump_regs(vcpu); |
| 117 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 118 | ret = RESUME_HOST; |
| 119 | } else { |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 120 | kvm_err("Illegal TLB Mod fault address , cause %#x, PC: %p, BadVaddr: %#lx\n", |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 121 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 122 | kvm_mips_dump_host_tlbs(); |
| 123 | kvm_arch_vcpu_dump_regs(vcpu); |
| 124 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 125 | ret = RESUME_HOST; |
| 126 | } |
| 127 | return ret; |
| 128 | } |
| 129 | |
James Hogan | 3b08aec | 2016-06-09 14:19:20 +0100 | [diff] [blame] | 130 | static int kvm_trap_emul_handle_tlb_miss(struct kvm_vcpu *vcpu, bool store) |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 131 | { |
| 132 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 133 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 134 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 135 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 136 | enum emulation_result er = EMULATE_DONE; |
| 137 | int ret = RESUME_GUEST; |
| 138 | |
| 139 | if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) |
| 140 | && KVM_GUEST_KERNEL_MODE(vcpu)) { |
| 141 | if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) { |
| 142 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 143 | ret = RESUME_HOST; |
| 144 | } |
| 145 | } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0 |
| 146 | || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) { |
James Hogan | 3b08aec | 2016-06-09 14:19:20 +0100 | [diff] [blame] | 147 | kvm_debug("USER ADDR TLB %s fault: cause %#x, PC: %p, BadVaddr: %#lx\n", |
| 148 | store ? "ST" : "LD", cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 149 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 150 | /* |
| 151 | * User Address (UA) fault, this could happen if |
| 152 | * (1) TLB entry not present/valid in both Guest and shadow host |
| 153 | * TLBs, in this case we pass on the fault to the guest |
| 154 | * kernel and let it handle it. |
| 155 | * (2) TLB entry is present in the Guest TLB but not in the |
| 156 | * shadow, in this case we inject the TLB from the Guest TLB |
| 157 | * into the shadow host TLB |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 158 | */ |
| 159 | |
| 160 | er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu); |
| 161 | if (er == EMULATE_DONE) |
| 162 | ret = RESUME_GUEST; |
| 163 | else { |
| 164 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 165 | ret = RESUME_HOST; |
| 166 | } |
| 167 | } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) { |
James Hogan | 3b08aec | 2016-06-09 14:19:20 +0100 | [diff] [blame] | 168 | /* |
| 169 | * All KSEG0 faults are handled by KVM, as the guest kernel does |
| 170 | * not expect to ever get them |
| 171 | */ |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 172 | if (kvm_mips_handle_kseg0_tlb_fault |
| 173 | (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) { |
| 174 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 175 | ret = RESUME_HOST; |
| 176 | } |
James Hogan | d588847 | 2016-08-19 15:09:47 +0100 | [diff] [blame] | 177 | } else if (KVM_GUEST_KERNEL_MODE(vcpu) |
| 178 | && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) { |
| 179 | /* |
| 180 | * With EVA we may get a TLB exception instead of an address |
| 181 | * error when the guest performs MMIO to KSeg1 addresses. |
| 182 | */ |
| 183 | kvm_debug("Emulate %s MMIO space\n", |
| 184 | store ? "Store to" : "Load from"); |
| 185 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
| 186 | if (er == EMULATE_FAIL) { |
| 187 | kvm_err("Emulate %s MMIO space failed\n", |
| 188 | store ? "Store to" : "Load from"); |
| 189 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 190 | ret = RESUME_HOST; |
| 191 | } else { |
| 192 | run->exit_reason = KVM_EXIT_MMIO; |
| 193 | ret = RESUME_HOST; |
| 194 | } |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 195 | } else { |
James Hogan | 3b08aec | 2016-06-09 14:19:20 +0100 | [diff] [blame] | 196 | kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n", |
| 197 | store ? "ST" : "LD", cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 198 | kvm_mips_dump_host_tlbs(); |
| 199 | kvm_arch_vcpu_dump_regs(vcpu); |
| 200 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 201 | ret = RESUME_HOST; |
| 202 | } |
| 203 | return ret; |
| 204 | } |
| 205 | |
James Hogan | 3b08aec | 2016-06-09 14:19:20 +0100 | [diff] [blame] | 206 | static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu) |
| 207 | { |
| 208 | return kvm_trap_emul_handle_tlb_miss(vcpu, true); |
| 209 | } |
| 210 | |
| 211 | static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu) |
| 212 | { |
| 213 | return kvm_trap_emul_handle_tlb_miss(vcpu, false); |
| 214 | } |
| 215 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 216 | static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu) |
| 217 | { |
| 218 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 219 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 220 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 221 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 222 | enum emulation_result er = EMULATE_DONE; |
| 223 | int ret = RESUME_GUEST; |
| 224 | |
| 225 | if (KVM_GUEST_KERNEL_MODE(vcpu) |
| 226 | && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 227 | kvm_debug("Emulate Store to MMIO space\n"); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 228 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
| 229 | if (er == EMULATE_FAIL) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 230 | kvm_err("Emulate Store to MMIO space failed\n"); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 231 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 232 | ret = RESUME_HOST; |
| 233 | } else { |
| 234 | run->exit_reason = KVM_EXIT_MMIO; |
| 235 | ret = RESUME_HOST; |
| 236 | } |
| 237 | } else { |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 238 | kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n", |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 239 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 240 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 241 | ret = RESUME_HOST; |
| 242 | } |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu) |
| 247 | { |
| 248 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 249 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 250 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 251 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 252 | enum emulation_result er = EMULATE_DONE; |
| 253 | int ret = RESUME_GUEST; |
| 254 | |
| 255 | if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 256 | kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 257 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
| 258 | if (er == EMULATE_FAIL) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 259 | kvm_err("Emulate Load from MMIO space failed\n"); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 260 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 261 | ret = RESUME_HOST; |
| 262 | } else { |
| 263 | run->exit_reason = KVM_EXIT_MMIO; |
| 264 | ret = RESUME_HOST; |
| 265 | } |
| 266 | } else { |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 267 | kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n", |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 268 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 269 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 270 | ret = RESUME_HOST; |
| 271 | er = EMULATE_FAIL; |
| 272 | } |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu) |
| 277 | { |
| 278 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 279 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 280 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 281 | enum emulation_result er = EMULATE_DONE; |
| 282 | int ret = RESUME_GUEST; |
| 283 | |
| 284 | er = kvm_mips_emulate_syscall(cause, opc, run, vcpu); |
| 285 | if (er == EMULATE_DONE) |
| 286 | ret = RESUME_GUEST; |
| 287 | else { |
| 288 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 289 | ret = RESUME_HOST; |
| 290 | } |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu) |
| 295 | { |
| 296 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 297 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 298 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 299 | enum emulation_result er = EMULATE_DONE; |
| 300 | int ret = RESUME_GUEST; |
| 301 | |
| 302 | er = kvm_mips_handle_ri(cause, opc, run, vcpu); |
| 303 | if (er == EMULATE_DONE) |
| 304 | ret = RESUME_GUEST; |
| 305 | else { |
| 306 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 307 | ret = RESUME_HOST; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu) |
| 313 | { |
| 314 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 315 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 316 | u32 cause = vcpu->arch.host_cp0_cause; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 317 | enum emulation_result er = EMULATE_DONE; |
| 318 | int ret = RESUME_GUEST; |
| 319 | |
| 320 | er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu); |
| 321 | if (er == EMULATE_DONE) |
| 322 | ret = RESUME_GUEST; |
| 323 | else { |
| 324 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 325 | ret = RESUME_HOST; |
| 326 | } |
| 327 | return ret; |
| 328 | } |
| 329 | |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 330 | static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu) |
| 331 | { |
| 332 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 333 | u32 __user *opc = (u32 __user *)vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 334 | u32 cause = vcpu->arch.host_cp0_cause; |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 335 | enum emulation_result er = EMULATE_DONE; |
| 336 | int ret = RESUME_GUEST; |
| 337 | |
| 338 | er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu); |
| 339 | if (er == EMULATE_DONE) { |
| 340 | ret = RESUME_GUEST; |
| 341 | } else { |
| 342 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 343 | ret = RESUME_HOST; |
| 344 | } |
| 345 | return ret; |
| 346 | } |
| 347 | |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 348 | static int kvm_trap_emul_handle_msa_fpe(struct kvm_vcpu *vcpu) |
| 349 | { |
| 350 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 351 | u32 __user *opc = (u32 __user *)vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 352 | u32 cause = vcpu->arch.host_cp0_cause; |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 353 | enum emulation_result er = EMULATE_DONE; |
| 354 | int ret = RESUME_GUEST; |
| 355 | |
| 356 | er = kvm_mips_emulate_msafpe_exc(cause, opc, run, vcpu); |
| 357 | if (er == EMULATE_DONE) { |
| 358 | ret = RESUME_GUEST; |
| 359 | } else { |
| 360 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 361 | ret = RESUME_HOST; |
| 362 | } |
| 363 | return ret; |
| 364 | } |
| 365 | |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 366 | static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu) |
| 367 | { |
| 368 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 369 | u32 __user *opc = (u32 __user *)vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 370 | u32 cause = vcpu->arch.host_cp0_cause; |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 371 | enum emulation_result er = EMULATE_DONE; |
| 372 | int ret = RESUME_GUEST; |
| 373 | |
| 374 | er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu); |
| 375 | if (er == EMULATE_DONE) { |
| 376 | ret = RESUME_GUEST; |
| 377 | } else { |
| 378 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 379 | ret = RESUME_HOST; |
| 380 | } |
| 381 | return ret; |
| 382 | } |
| 383 | |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 384 | /** |
| 385 | * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root. |
| 386 | * @vcpu: Virtual CPU context. |
| 387 | * |
| 388 | * Handle when the guest attempts to use MSA when it is disabled. |
| 389 | */ |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 390 | static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu) |
| 391 | { |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 392 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 393 | struct kvm_run *run = vcpu->run; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 394 | u32 __user *opc = (u32 __user *) vcpu->arch.pc; |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 395 | u32 cause = vcpu->arch.host_cp0_cause; |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 396 | enum emulation_result er = EMULATE_DONE; |
| 397 | int ret = RESUME_GUEST; |
| 398 | |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 399 | if (!kvm_mips_guest_has_msa(&vcpu->arch) || |
| 400 | (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) { |
| 401 | /* |
| 402 | * No MSA in guest, or FPU enabled and not in FR=1 mode, |
| 403 | * guest reserved instruction exception |
| 404 | */ |
| 405 | er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu); |
| 406 | } else if (!(kvm_read_c0_guest_config5(cop0) & MIPS_CONF5_MSAEN)) { |
| 407 | /* MSA disabled by guest, guest MSA disabled exception */ |
| 408 | er = kvm_mips_emulate_msadis_exc(cause, opc, run, vcpu); |
| 409 | } else { |
| 410 | /* Restore MSA/FPU state */ |
| 411 | kvm_own_msa(vcpu); |
| 412 | er = EMULATE_DONE; |
| 413 | } |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 414 | |
| 415 | switch (er) { |
| 416 | case EMULATE_DONE: |
| 417 | ret = RESUME_GUEST; |
| 418 | break; |
| 419 | |
| 420 | case EMULATE_FAIL: |
| 421 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 422 | ret = RESUME_HOST; |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | BUG(); |
| 427 | } |
| 428 | return ret; |
| 429 | } |
| 430 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 431 | static int kvm_trap_emul_vm_init(struct kvm *kvm) |
| 432 | { |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu) |
| 437 | { |
James Hogan | 0510870 | 2016-06-15 19:29:56 +0100 | [diff] [blame] | 438 | vcpu->arch.kscratch_enabled = 0xfc; |
| 439 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu) |
| 444 | { |
| 445 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | e342925 | 2016-06-15 19:30:00 +0100 | [diff] [blame] | 446 | u32 config, config1; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 447 | int vcpu_id = vcpu->vcpu_id; |
| 448 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 449 | /* |
| 450 | * Arch specific stuff, set up config registers properly so that the |
James Hogan | 8426097 | 2016-07-04 19:35:15 +0100 | [diff] [blame] | 451 | * guest will come up as expected |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 452 | */ |
James Hogan | 8426097 | 2016-07-04 19:35:15 +0100 | [diff] [blame] | 453 | #ifndef CONFIG_CPU_MIPSR6 |
| 454 | /* r2-r5, simulate a MIPS 24kc */ |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 455 | kvm_write_c0_guest_prid(cop0, 0x00019300); |
James Hogan | 8426097 | 2016-07-04 19:35:15 +0100 | [diff] [blame] | 456 | #else |
| 457 | /* r6+, simulate a generic QEMU machine */ |
| 458 | kvm_write_c0_guest_prid(cop0, 0x00010000); |
| 459 | #endif |
James Hogan | e342925 | 2016-06-15 19:30:00 +0100 | [diff] [blame] | 460 | /* |
| 461 | * Have config1, Cacheable, noncoherent, write-back, write allocate. |
| 462 | * Endianness, arch revision & virtually tagged icache should match |
| 463 | * host. |
| 464 | */ |
| 465 | config = read_c0_config() & MIPS_CONF_AR; |
James Hogan | 4e10b76 | 2016-06-15 19:30:01 +0100 | [diff] [blame] | 466 | config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB; |
James Hogan | e342925 | 2016-06-15 19:30:00 +0100 | [diff] [blame] | 467 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 468 | config |= CONF_BE; |
| 469 | #endif |
| 470 | if (cpu_has_vtag_icache) |
| 471 | config |= MIPS_CONF_VI; |
| 472 | kvm_write_c0_guest_config(cop0, config); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 473 | |
| 474 | /* Read the cache characteristics from the host Config1 Register */ |
| 475 | config1 = (read_c0_config1() & ~0x7f); |
| 476 | |
| 477 | /* Set up MMU size */ |
| 478 | config1 &= ~(0x3f << 25); |
| 479 | config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25); |
| 480 | |
| 481 | /* We unset some bits that we aren't emulating */ |
James Hogan | 4e10b76 | 2016-06-15 19:30:01 +0100 | [diff] [blame] | 482 | config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC | |
| 483 | MIPS_CONF1_WR | MIPS_CONF1_CA); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 484 | kvm_write_c0_guest_config1(cop0, config1); |
| 485 | |
James Hogan | 2211ee8 | 2015-03-04 15:56:47 +0000 | [diff] [blame] | 486 | /* Have config3, no tertiary/secondary caches implemented */ |
| 487 | kvm_write_c0_guest_config2(cop0, MIPS_CONF_M); |
| 488 | /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */ |
| 489 | |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 490 | /* Have config4, UserLocal */ |
| 491 | kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI); |
| 492 | |
| 493 | /* Have config5 */ |
| 494 | kvm_write_c0_guest_config4(cop0, MIPS_CONF_M); |
| 495 | |
| 496 | /* No config6 */ |
| 497 | kvm_write_c0_guest_config5(cop0, 0); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 498 | |
| 499 | /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */ |
| 500 | kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10)); |
| 501 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 502 | /* |
Adam Buchbinder | 92a76f6 | 2016-02-25 00:44:58 -0800 | [diff] [blame] | 503 | * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5) |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 504 | */ |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 505 | kvm_write_c0_guest_intctl(cop0, 0xFC000000); |
| 506 | |
| 507 | /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */ |
James Hogan | 37af2f3 | 2016-05-11 13:50:49 +0100 | [diff] [blame] | 508 | kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 | |
| 509 | (vcpu_id & MIPS_EBASE_CPUNUM)); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 514 | static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu) |
| 515 | { |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu, |
| 520 | u64 __user *indices) |
| 521 | { |
| 522 | return 0; |
| 523 | } |
| 524 | |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 525 | static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu, |
| 526 | const struct kvm_one_reg *reg, |
| 527 | s64 *v) |
| 528 | { |
| 529 | switch (reg->id) { |
| 530 | case KVM_REG_MIPS_CP0_COUNT: |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 531 | *v = kvm_mips_read_count(vcpu); |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 532 | break; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 533 | case KVM_REG_MIPS_COUNT_CTL: |
| 534 | *v = vcpu->arch.count_ctl; |
| 535 | break; |
| 536 | case KVM_REG_MIPS_COUNT_RESUME: |
| 537 | *v = ktime_to_ns(vcpu->arch.count_resume); |
| 538 | break; |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 539 | case KVM_REG_MIPS_COUNT_HZ: |
| 540 | *v = vcpu->arch.count_hz; |
| 541 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 542 | default: |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu, |
| 549 | const struct kvm_one_reg *reg, |
| 550 | s64 v) |
| 551 | { |
| 552 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 553 | int ret = 0; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 554 | unsigned int cur, change; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 555 | |
| 556 | switch (reg->id) { |
| 557 | case KVM_REG_MIPS_CP0_COUNT: |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 558 | kvm_mips_write_count(vcpu, v); |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 559 | break; |
| 560 | case KVM_REG_MIPS_CP0_COMPARE: |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 561 | kvm_mips_write_compare(vcpu, v, false); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 562 | break; |
| 563 | case KVM_REG_MIPS_CP0_CAUSE: |
| 564 | /* |
| 565 | * If the timer is stopped or started (DC bit) it must look |
| 566 | * atomic with changes to the interrupt pending bits (TI, IRQ5). |
| 567 | * A timer interrupt should not happen in between. |
| 568 | */ |
| 569 | if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) { |
| 570 | if (v & CAUSEF_DC) { |
| 571 | /* disable timer first */ |
| 572 | kvm_mips_count_disable_cause(vcpu); |
| 573 | kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v); |
| 574 | } else { |
| 575 | /* enable timer last */ |
| 576 | kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v); |
| 577 | kvm_mips_count_enable_cause(vcpu); |
| 578 | } |
| 579 | } else { |
| 580 | kvm_write_c0_guest_cause(cop0, v); |
| 581 | } |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 582 | break; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 583 | case KVM_REG_MIPS_CP0_CONFIG: |
| 584 | /* read-only for now */ |
| 585 | break; |
| 586 | case KVM_REG_MIPS_CP0_CONFIG1: |
| 587 | cur = kvm_read_c0_guest_config1(cop0); |
| 588 | change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu); |
| 589 | if (change) { |
| 590 | v = cur ^ change; |
| 591 | kvm_write_c0_guest_config1(cop0, v); |
| 592 | } |
| 593 | break; |
| 594 | case KVM_REG_MIPS_CP0_CONFIG2: |
| 595 | /* read-only for now */ |
| 596 | break; |
| 597 | case KVM_REG_MIPS_CP0_CONFIG3: |
| 598 | cur = kvm_read_c0_guest_config3(cop0); |
| 599 | change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu); |
| 600 | if (change) { |
| 601 | v = cur ^ change; |
| 602 | kvm_write_c0_guest_config3(cop0, v); |
| 603 | } |
| 604 | break; |
| 605 | case KVM_REG_MIPS_CP0_CONFIG4: |
| 606 | cur = kvm_read_c0_guest_config4(cop0); |
| 607 | change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu); |
| 608 | if (change) { |
| 609 | v = cur ^ change; |
| 610 | kvm_write_c0_guest_config4(cop0, v); |
| 611 | } |
| 612 | break; |
| 613 | case KVM_REG_MIPS_CP0_CONFIG5: |
| 614 | cur = kvm_read_c0_guest_config5(cop0); |
| 615 | change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu); |
| 616 | if (change) { |
| 617 | v = cur ^ change; |
| 618 | kvm_write_c0_guest_config5(cop0, v); |
| 619 | } |
| 620 | break; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 621 | case KVM_REG_MIPS_COUNT_CTL: |
| 622 | ret = kvm_mips_set_count_ctl(vcpu, v); |
| 623 | break; |
| 624 | case KVM_REG_MIPS_COUNT_RESUME: |
| 625 | ret = kvm_mips_set_count_resume(vcpu, v); |
| 626 | break; |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 627 | case KVM_REG_MIPS_COUNT_HZ: |
| 628 | ret = kvm_mips_set_count_hz(vcpu, v); |
| 629 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 630 | default: |
| 631 | return -EINVAL; |
| 632 | } |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 633 | return ret; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 634 | } |
| 635 | |
James Hogan | a60b843 | 2016-11-12 00:00:13 +0000 | [diff] [blame] | 636 | static int kvm_trap_emul_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 637 | { |
James Hogan | 1581ff3 | 2016-11-16 23:48:56 +0000 | [diff] [blame^] | 638 | unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]); |
| 639 | |
| 640 | /* Allocate new kernel and user ASIDs if needed */ |
| 641 | |
| 642 | if ((vcpu->arch.guest_kernel_asid[cpu] ^ asid_cache(cpu)) & |
| 643 | asid_version_mask(cpu)) { |
| 644 | kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu); |
| 645 | vcpu->arch.guest_kernel_asid[cpu] = |
| 646 | vcpu->arch.guest_kernel_mm.context.asid[cpu]; |
| 647 | |
| 648 | kvm_debug("[%d]: cpu_context: %#lx\n", cpu, |
| 649 | cpu_context(cpu, current->mm)); |
| 650 | kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n", |
| 651 | cpu, vcpu->arch.guest_kernel_asid[cpu]); |
| 652 | } |
| 653 | |
| 654 | if ((vcpu->arch.guest_user_asid[cpu] ^ asid_cache(cpu)) & |
| 655 | asid_version_mask(cpu)) { |
| 656 | kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu); |
| 657 | vcpu->arch.guest_user_asid[cpu] = |
| 658 | vcpu->arch.guest_user_mm.context.asid[cpu]; |
| 659 | |
| 660 | kvm_debug("[%d]: cpu_context: %#lx\n", cpu, |
| 661 | cpu_context(cpu, current->mm)); |
| 662 | kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu, |
| 663 | vcpu->arch.guest_user_asid[cpu]); |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Were we in guest context? If so then the pre-empted ASID is |
| 668 | * no longer valid, we need to set it to what it should be based |
| 669 | * on the mode of the Guest (Kernel/User) |
| 670 | */ |
| 671 | if (current->flags & PF_VCPU) { |
| 672 | if (KVM_GUEST_KERNEL_MODE(vcpu)) |
| 673 | write_c0_entryhi(vcpu->arch.guest_kernel_asid[cpu] & |
| 674 | asid_mask); |
| 675 | else |
| 676 | write_c0_entryhi(vcpu->arch.guest_user_asid[cpu] & |
| 677 | asid_mask); |
| 678 | ehb(); |
| 679 | } |
| 680 | |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
James Hogan | a60b843 | 2016-11-12 00:00:13 +0000 | [diff] [blame] | 684 | static int kvm_trap_emul_vcpu_put(struct kvm_vcpu *vcpu, int cpu) |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 685 | { |
James Hogan | a60b843 | 2016-11-12 00:00:13 +0000 | [diff] [blame] | 686 | kvm_lose_fpu(vcpu); |
| 687 | |
James Hogan | 1581ff3 | 2016-11-16 23:48:56 +0000 | [diff] [blame^] | 688 | if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) & |
| 689 | asid_version_mask(cpu))) { |
| 690 | kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__, |
| 691 | cpu_context(cpu, current->mm)); |
| 692 | drop_mmu_context(current->mm, cpu); |
| 693 | } |
| 694 | write_c0_entryhi(cpu_asid(cpu, current->mm)); |
| 695 | ehb(); |
| 696 | |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 700 | static struct kvm_mips_callbacks kvm_trap_emul_callbacks = { |
| 701 | /* exit handlers */ |
| 702 | .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable, |
| 703 | .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod, |
| 704 | .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss, |
| 705 | .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss, |
| 706 | .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st, |
| 707 | .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld, |
| 708 | .handle_syscall = kvm_trap_emul_handle_syscall, |
| 709 | .handle_res_inst = kvm_trap_emul_handle_res_inst, |
| 710 | .handle_break = kvm_trap_emul_handle_break, |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 711 | .handle_trap = kvm_trap_emul_handle_trap, |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 712 | .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe, |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 713 | .handle_fpe = kvm_trap_emul_handle_fpe, |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 714 | .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled, |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 715 | |
| 716 | .vm_init = kvm_trap_emul_vm_init, |
| 717 | .vcpu_init = kvm_trap_emul_vcpu_init, |
| 718 | .vcpu_setup = kvm_trap_emul_vcpu_setup, |
| 719 | .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb, |
| 720 | .queue_timer_int = kvm_mips_queue_timer_int_cb, |
| 721 | .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb, |
| 722 | .queue_io_int = kvm_mips_queue_io_int_cb, |
| 723 | .dequeue_io_int = kvm_mips_dequeue_io_int_cb, |
| 724 | .irq_deliver = kvm_mips_irq_deliver_cb, |
| 725 | .irq_clear = kvm_mips_irq_clear_cb, |
James Hogan | f5c43bd | 2016-06-15 19:29:49 +0100 | [diff] [blame] | 726 | .num_regs = kvm_trap_emul_num_regs, |
| 727 | .copy_reg_indices = kvm_trap_emul_copy_reg_indices, |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 728 | .get_one_reg = kvm_trap_emul_get_one_reg, |
| 729 | .set_one_reg = kvm_trap_emul_set_one_reg, |
James Hogan | a60b843 | 2016-11-12 00:00:13 +0000 | [diff] [blame] | 730 | .vcpu_load = kvm_trap_emul_vcpu_load, |
| 731 | .vcpu_put = kvm_trap_emul_vcpu_put, |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks) |
| 735 | { |
| 736 | *install_callbacks = &kvm_trap_emul_callbacks; |
| 737 | return 0; |
| 738 | } |