Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -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: Instruction/Exception emulation |
| 7 | * |
| 8 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 9 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 10 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/err.h> |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 14 | #include <linux/ktime.h> |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 15 | #include <linux/kvm_host.h> |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/bootmem.h> |
| 19 | #include <linux/random.h> |
| 20 | #include <asm/page.h> |
| 21 | #include <asm/cacheflush.h> |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 22 | #include <asm/cacheops.h> |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 23 | #include <asm/cpu-info.h> |
| 24 | #include <asm/mmu_context.h> |
| 25 | #include <asm/tlbflush.h> |
| 26 | #include <asm/inst.h> |
| 27 | |
| 28 | #undef CONFIG_MIPS_MT |
| 29 | #include <asm/r4kcache.h> |
| 30 | #define CONFIG_MIPS_MT |
| 31 | |
Deng-Cheng Zhu | d7d5b05 | 2014-06-26 12:11:38 -0700 | [diff] [blame] | 32 | #include "interrupt.h" |
| 33 | #include "commpage.h" |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 34 | |
| 35 | #include "trace.h" |
| 36 | |
| 37 | /* |
| 38 | * Compute the return address and do emulate branch simulation, if required. |
| 39 | * This function should be called only in branch delay slot active. |
| 40 | */ |
| 41 | unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu, |
| 42 | unsigned long instpc) |
| 43 | { |
| 44 | unsigned int dspcontrol; |
| 45 | union mips_instruction insn; |
| 46 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 47 | long epc = instpc; |
| 48 | long nextpc = KVM_INVALID_INST; |
| 49 | |
| 50 | if (epc & 3) |
| 51 | goto unaligned; |
| 52 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 53 | /* Read the instruction */ |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 54 | insn.word = kvm_get_inst((u32 *) epc, vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 55 | |
| 56 | if (insn.word == KVM_INVALID_INST) |
| 57 | return KVM_INVALID_INST; |
| 58 | |
| 59 | switch (insn.i_format.opcode) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 60 | /* jr and jalr are in r_format format. */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 61 | case spec_op: |
| 62 | switch (insn.r_format.func) { |
| 63 | case jalr_op: |
| 64 | arch->gprs[insn.r_format.rd] = epc + 8; |
| 65 | /* Fall through */ |
| 66 | case jr_op: |
| 67 | nextpc = arch->gprs[insn.r_format.rs]; |
| 68 | break; |
| 69 | } |
| 70 | break; |
| 71 | |
| 72 | /* |
| 73 | * This group contains: |
| 74 | * bltz_op, bgez_op, bltzl_op, bgezl_op, |
| 75 | * bltzal_op, bgezal_op, bltzall_op, bgezall_op. |
| 76 | */ |
| 77 | case bcond_op: |
| 78 | switch (insn.i_format.rt) { |
| 79 | case bltz_op: |
| 80 | case bltzl_op: |
| 81 | if ((long)arch->gprs[insn.i_format.rs] < 0) |
| 82 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 83 | else |
| 84 | epc += 8; |
| 85 | nextpc = epc; |
| 86 | break; |
| 87 | |
| 88 | case bgez_op: |
| 89 | case bgezl_op: |
| 90 | if ((long)arch->gprs[insn.i_format.rs] >= 0) |
| 91 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 92 | else |
| 93 | epc += 8; |
| 94 | nextpc = epc; |
| 95 | break; |
| 96 | |
| 97 | case bltzal_op: |
| 98 | case bltzall_op: |
| 99 | arch->gprs[31] = epc + 8; |
| 100 | if ((long)arch->gprs[insn.i_format.rs] < 0) |
| 101 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 102 | else |
| 103 | epc += 8; |
| 104 | nextpc = epc; |
| 105 | break; |
| 106 | |
| 107 | case bgezal_op: |
| 108 | case bgezall_op: |
| 109 | arch->gprs[31] = epc + 8; |
| 110 | if ((long)arch->gprs[insn.i_format.rs] >= 0) |
| 111 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 112 | else |
| 113 | epc += 8; |
| 114 | nextpc = epc; |
| 115 | break; |
| 116 | case bposge32_op: |
| 117 | if (!cpu_has_dsp) |
| 118 | goto sigill; |
| 119 | |
| 120 | dspcontrol = rddsp(0x01); |
| 121 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 122 | if (dspcontrol >= 32) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 123 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 124 | else |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 125 | epc += 8; |
| 126 | nextpc = epc; |
| 127 | break; |
| 128 | } |
| 129 | break; |
| 130 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 131 | /* These are unconditional and in j_format. */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 132 | case jal_op: |
| 133 | arch->gprs[31] = instpc + 8; |
| 134 | case j_op: |
| 135 | epc += 4; |
| 136 | epc >>= 28; |
| 137 | epc <<= 28; |
| 138 | epc |= (insn.j_format.target << 2); |
| 139 | nextpc = epc; |
| 140 | break; |
| 141 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 142 | /* These are conditional and in i_format. */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 143 | case beq_op: |
| 144 | case beql_op: |
| 145 | if (arch->gprs[insn.i_format.rs] == |
| 146 | arch->gprs[insn.i_format.rt]) |
| 147 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 148 | else |
| 149 | epc += 8; |
| 150 | nextpc = epc; |
| 151 | break; |
| 152 | |
| 153 | case bne_op: |
| 154 | case bnel_op: |
| 155 | if (arch->gprs[insn.i_format.rs] != |
| 156 | arch->gprs[insn.i_format.rt]) |
| 157 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 158 | else |
| 159 | epc += 8; |
| 160 | nextpc = epc; |
| 161 | break; |
| 162 | |
James Hogan | 2e0badf | 2016-07-04 19:35:12 +0100 | [diff] [blame] | 163 | case blez_op: /* POP06 */ |
| 164 | #ifndef CONFIG_CPU_MIPSR6 |
| 165 | case blezl_op: /* removed in R6 */ |
| 166 | #endif |
| 167 | if (insn.i_format.rt != 0) |
| 168 | goto compact_branch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 169 | if ((long)arch->gprs[insn.i_format.rs] <= 0) |
| 170 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 171 | else |
| 172 | epc += 8; |
| 173 | nextpc = epc; |
| 174 | break; |
| 175 | |
James Hogan | 2e0badf | 2016-07-04 19:35:12 +0100 | [diff] [blame] | 176 | case bgtz_op: /* POP07 */ |
| 177 | #ifndef CONFIG_CPU_MIPSR6 |
| 178 | case bgtzl_op: /* removed in R6 */ |
| 179 | #endif |
| 180 | if (insn.i_format.rt != 0) |
| 181 | goto compact_branch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 182 | if ((long)arch->gprs[insn.i_format.rs] > 0) |
| 183 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 184 | else |
| 185 | epc += 8; |
| 186 | nextpc = epc; |
| 187 | break; |
| 188 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 189 | /* And now the FPA/cp1 branch instructions. */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 190 | case cop1_op: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 191 | kvm_err("%s: unsupported cop1_op\n", __func__); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 192 | break; |
James Hogan | 2e0badf | 2016-07-04 19:35:12 +0100 | [diff] [blame] | 193 | |
| 194 | #ifdef CONFIG_CPU_MIPSR6 |
| 195 | /* R6 added the following compact branches with forbidden slots */ |
| 196 | case blezl_op: /* POP26 */ |
| 197 | case bgtzl_op: /* POP27 */ |
| 198 | /* only rt == 0 isn't compact branch */ |
| 199 | if (insn.i_format.rt != 0) |
| 200 | goto compact_branch; |
| 201 | break; |
| 202 | case pop10_op: |
| 203 | case pop30_op: |
| 204 | /* only rs == rt == 0 is reserved, rest are compact branches */ |
| 205 | if (insn.i_format.rs != 0 || insn.i_format.rt != 0) |
| 206 | goto compact_branch; |
| 207 | break; |
| 208 | case pop66_op: |
| 209 | case pop76_op: |
| 210 | /* only rs == 0 isn't compact branch */ |
| 211 | if (insn.i_format.rs != 0) |
| 212 | goto compact_branch; |
| 213 | break; |
| 214 | compact_branch: |
| 215 | /* |
| 216 | * If we've hit an exception on the forbidden slot, then |
| 217 | * the branch must not have been taken. |
| 218 | */ |
| 219 | epc += 8; |
| 220 | nextpc = epc; |
| 221 | break; |
| 222 | #else |
| 223 | compact_branch: |
| 224 | /* Compact branches not supported before R6 */ |
| 225 | break; |
| 226 | #endif |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return nextpc; |
| 230 | |
| 231 | unaligned: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 232 | kvm_err("%s: unaligned epc\n", __func__); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 233 | return nextpc; |
| 234 | |
| 235 | sigill: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 236 | kvm_err("%s: DSP branch but not DSP ASE\n", __func__); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 237 | return nextpc; |
| 238 | } |
| 239 | |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 240 | enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 241 | { |
| 242 | unsigned long branch_pc; |
| 243 | enum emulation_result er = EMULATE_DONE; |
| 244 | |
| 245 | if (cause & CAUSEF_BD) { |
| 246 | branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc); |
| 247 | if (branch_pc == KVM_INVALID_INST) { |
| 248 | er = EMULATE_FAIL; |
| 249 | } else { |
| 250 | vcpu->arch.pc = branch_pc; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 251 | kvm_debug("BD update_pc(): New PC: %#lx\n", |
| 252 | vcpu->arch.pc); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 253 | } |
| 254 | } else |
| 255 | vcpu->arch.pc += 4; |
| 256 | |
| 257 | kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc); |
| 258 | |
| 259 | return er; |
| 260 | } |
| 261 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 262 | /** |
| 263 | * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled. |
| 264 | * @vcpu: Virtual CPU. |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 265 | * |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 266 | * Returns: 1 if the CP0_Count timer is disabled by either the guest |
| 267 | * CP0_Cause.DC bit or the count_ctl.DC bit. |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 268 | * 0 otherwise (in which case CP0_Count timer is running). |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 269 | */ |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 270 | static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 271 | { |
| 272 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 273 | |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 274 | return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) || |
| 275 | (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 276 | } |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 277 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 278 | /** |
| 279 | * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count. |
| 280 | * |
| 281 | * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias. |
| 282 | * |
| 283 | * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running). |
| 284 | */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 285 | static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 286 | { |
| 287 | s64 now_ns, periods; |
| 288 | u64 delta; |
| 289 | |
| 290 | now_ns = ktime_to_ns(now); |
| 291 | delta = now_ns + vcpu->arch.count_dyn_bias; |
| 292 | |
| 293 | if (delta >= vcpu->arch.count_period) { |
| 294 | /* If delta is out of safe range the bias needs adjusting */ |
| 295 | periods = div64_s64(now_ns, vcpu->arch.count_period); |
| 296 | vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period; |
| 297 | /* Recalculate delta with new bias */ |
| 298 | delta = now_ns + vcpu->arch.count_dyn_bias; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 299 | } |
| 300 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 301 | /* |
| 302 | * We've ensured that: |
| 303 | * delta < count_period |
| 304 | * |
| 305 | * Therefore the intermediate delta*count_hz will never overflow since |
| 306 | * at the boundary condition: |
| 307 | * delta = count_period |
| 308 | * delta = NSEC_PER_SEC * 2^32 / count_hz |
| 309 | * delta * count_hz = NSEC_PER_SEC * 2^32 |
| 310 | */ |
| 311 | return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC); |
| 312 | } |
| 313 | |
| 314 | /** |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 315 | * kvm_mips_count_time() - Get effective current time. |
| 316 | * @vcpu: Virtual CPU. |
| 317 | * |
| 318 | * Get effective monotonic ktime. This is usually a straightforward ktime_get(), |
| 319 | * except when the master disable bit is set in count_ctl, in which case it is |
| 320 | * count_resume, i.e. the time that the count was disabled. |
| 321 | * |
| 322 | * Returns: Effective monotonic ktime for CP0_Count. |
| 323 | */ |
| 324 | static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu) |
| 325 | { |
| 326 | if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)) |
| 327 | return vcpu->arch.count_resume; |
| 328 | |
| 329 | return ktime_get(); |
| 330 | } |
| 331 | |
| 332 | /** |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 333 | * kvm_mips_read_count_running() - Read the current count value as if running. |
| 334 | * @vcpu: Virtual CPU. |
| 335 | * @now: Kernel time to read CP0_Count at. |
| 336 | * |
| 337 | * Returns the current guest CP0_Count register at time @now and handles if the |
| 338 | * timer interrupt is pending and hasn't been handled yet. |
| 339 | * |
| 340 | * Returns: The current value of the guest CP0_Count register. |
| 341 | */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 342 | static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 343 | { |
James Hogan | 4355c44 | 2016-04-22 10:38:45 +0100 | [diff] [blame] | 344 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 345 | ktime_t expires, threshold; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 346 | u32 count, compare; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 347 | int running; |
| 348 | |
James Hogan | 4355c44 | 2016-04-22 10:38:45 +0100 | [diff] [blame] | 349 | /* Calculate the biased and scaled guest CP0_Count */ |
| 350 | count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now); |
| 351 | compare = kvm_read_c0_guest_compare(cop0); |
| 352 | |
| 353 | /* |
| 354 | * Find whether CP0_Count has reached the closest timer interrupt. If |
| 355 | * not, we shouldn't inject it. |
| 356 | */ |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 357 | if ((s32)(count - compare) < 0) |
James Hogan | 4355c44 | 2016-04-22 10:38:45 +0100 | [diff] [blame] | 358 | return count; |
| 359 | |
| 360 | /* |
| 361 | * The CP0_Count we're going to return has already reached the closest |
| 362 | * timer interrupt. Quickly check if it really is a new interrupt by |
| 363 | * looking at whether the interval until the hrtimer expiry time is |
| 364 | * less than 1/4 of the timer period. |
| 365 | */ |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 366 | expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer); |
James Hogan | 4355c44 | 2016-04-22 10:38:45 +0100 | [diff] [blame] | 367 | threshold = ktime_add_ns(now, vcpu->arch.count_period / 4); |
| 368 | if (ktime_before(expires, threshold)) { |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 369 | /* |
| 370 | * Cancel it while we handle it so there's no chance of |
| 371 | * interference with the timeout handler. |
| 372 | */ |
| 373 | running = hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 374 | |
| 375 | /* Nothing should be waiting on the timeout */ |
| 376 | kvm_mips_callbacks->queue_timer_int(vcpu); |
| 377 | |
| 378 | /* |
| 379 | * Restart the timer if it was running based on the expiry time |
| 380 | * we read, so that we don't push it back 2 periods. |
| 381 | */ |
| 382 | if (running) { |
| 383 | expires = ktime_add_ns(expires, |
| 384 | vcpu->arch.count_period); |
| 385 | hrtimer_start(&vcpu->arch.comparecount_timer, expires, |
| 386 | HRTIMER_MODE_ABS); |
| 387 | } |
| 388 | } |
| 389 | |
James Hogan | 4355c44 | 2016-04-22 10:38:45 +0100 | [diff] [blame] | 390 | return count; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * kvm_mips_read_count() - Read the current count value. |
| 395 | * @vcpu: Virtual CPU. |
| 396 | * |
| 397 | * Read the current guest CP0_Count value, taking into account whether the timer |
| 398 | * is stopped. |
| 399 | * |
| 400 | * Returns: The current guest CP0_Count value. |
| 401 | */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 402 | u32 kvm_mips_read_count(struct kvm_vcpu *vcpu) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 403 | { |
| 404 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 405 | |
| 406 | /* If count disabled just read static copy of count */ |
| 407 | if (kvm_mips_count_disabled(vcpu)) |
| 408 | return kvm_read_c0_guest_count(cop0); |
| 409 | |
| 410 | return kvm_mips_read_count_running(vcpu, ktime_get()); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer. |
| 415 | * @vcpu: Virtual CPU. |
| 416 | * @count: Output pointer for CP0_Count value at point of freeze. |
| 417 | * |
| 418 | * Freeze the hrtimer safely and return both the ktime and the CP0_Count value |
| 419 | * at the point it was frozen. It is guaranteed that any pending interrupts at |
| 420 | * the point it was frozen are handled, and none after that point. |
| 421 | * |
| 422 | * This is useful where the time/CP0_Count is needed in the calculation of the |
| 423 | * new parameters. |
| 424 | * |
| 425 | * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running). |
| 426 | * |
| 427 | * Returns: The ktime at the point of freeze. |
| 428 | */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 429 | static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 430 | { |
| 431 | ktime_t now; |
| 432 | |
| 433 | /* stop hrtimer before finding time */ |
| 434 | hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 435 | now = ktime_get(); |
| 436 | |
| 437 | /* find count at this point and handle pending hrtimer */ |
| 438 | *count = kvm_mips_read_count_running(vcpu, now); |
| 439 | |
| 440 | return now; |
| 441 | } |
| 442 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 443 | /** |
| 444 | * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry. |
| 445 | * @vcpu: Virtual CPU. |
| 446 | * @now: ktime at point of resume. |
| 447 | * @count: CP0_Count at point of resume. |
| 448 | * |
| 449 | * Resumes the timer and updates the timer expiry based on @now and @count. |
| 450 | * This can be used in conjunction with kvm_mips_freeze_timer() when timer |
| 451 | * parameters need to be changed. |
| 452 | * |
| 453 | * It is guaranteed that a timer interrupt immediately after resume will be |
| 454 | * handled, but not if CP_Compare is exactly at @count. That case is already |
| 455 | * handled by kvm_mips_freeze_timer(). |
| 456 | * |
| 457 | * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running). |
| 458 | */ |
| 459 | static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 460 | ktime_t now, u32 count) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 461 | { |
| 462 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 463 | u32 compare; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 464 | u64 delta; |
| 465 | ktime_t expire; |
| 466 | |
| 467 | /* Calculate timeout (wrap 0 to 2^32) */ |
| 468 | compare = kvm_read_c0_guest_compare(cop0); |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 469 | delta = (u64)(u32)(compare - count - 1) + 1; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 470 | delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz); |
| 471 | expire = ktime_add_ns(now, delta); |
| 472 | |
| 473 | /* Update hrtimer to use new timeout */ |
| 474 | hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 475 | hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS); |
| 476 | } |
| 477 | |
| 478 | /** |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 479 | * kvm_mips_write_count() - Modify the count and update timer. |
| 480 | * @vcpu: Virtual CPU. |
| 481 | * @count: Guest CP0_Count value to set. |
| 482 | * |
| 483 | * Sets the CP0_Count value and updates the timer accordingly. |
| 484 | */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 485 | void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 486 | { |
| 487 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 488 | ktime_t now; |
| 489 | |
| 490 | /* Calculate bias */ |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 491 | now = kvm_mips_count_time(vcpu); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 492 | vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now); |
| 493 | |
| 494 | if (kvm_mips_count_disabled(vcpu)) |
| 495 | /* The timer's disabled, adjust the static count */ |
| 496 | kvm_write_c0_guest_count(cop0, count); |
| 497 | else |
| 498 | /* Update timeout */ |
| 499 | kvm_mips_resume_hrtimer(vcpu, now, count); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * kvm_mips_init_count() - Initialise timer. |
| 504 | * @vcpu: Virtual CPU. |
| 505 | * |
| 506 | * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set |
| 507 | * it going if it's enabled. |
| 508 | */ |
| 509 | void kvm_mips_init_count(struct kvm_vcpu *vcpu) |
| 510 | { |
| 511 | /* 100 MHz */ |
| 512 | vcpu->arch.count_hz = 100*1000*1000; |
| 513 | vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, |
| 514 | vcpu->arch.count_hz); |
| 515 | vcpu->arch.count_dyn_bias = 0; |
| 516 | |
| 517 | /* Starting at 0 */ |
| 518 | kvm_mips_write_count(vcpu, 0); |
| 519 | } |
| 520 | |
| 521 | /** |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 522 | * kvm_mips_set_count_hz() - Update the frequency of the timer. |
| 523 | * @vcpu: Virtual CPU. |
| 524 | * @count_hz: Frequency of CP0_Count timer in Hz. |
| 525 | * |
| 526 | * Change the frequency of the CP0_Count timer. This is done atomically so that |
| 527 | * CP0_Count is continuous and no timer interrupt is lost. |
| 528 | * |
| 529 | * Returns: -EINVAL if @count_hz is out of range. |
| 530 | * 0 on success. |
| 531 | */ |
| 532 | int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz) |
| 533 | { |
| 534 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 535 | int dc; |
| 536 | ktime_t now; |
| 537 | u32 count; |
| 538 | |
| 539 | /* ensure the frequency is in a sensible range... */ |
| 540 | if (count_hz <= 0 || count_hz > NSEC_PER_SEC) |
| 541 | return -EINVAL; |
| 542 | /* ... and has actually changed */ |
| 543 | if (vcpu->arch.count_hz == count_hz) |
| 544 | return 0; |
| 545 | |
| 546 | /* Safely freeze timer so we can keep it continuous */ |
| 547 | dc = kvm_mips_count_disabled(vcpu); |
| 548 | if (dc) { |
| 549 | now = kvm_mips_count_time(vcpu); |
| 550 | count = kvm_read_c0_guest_count(cop0); |
| 551 | } else { |
| 552 | now = kvm_mips_freeze_hrtimer(vcpu, &count); |
| 553 | } |
| 554 | |
| 555 | /* Update the frequency */ |
| 556 | vcpu->arch.count_hz = count_hz; |
| 557 | vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz); |
| 558 | vcpu->arch.count_dyn_bias = 0; |
| 559 | |
| 560 | /* Calculate adjusted bias so dynamic count is unchanged */ |
| 561 | vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now); |
| 562 | |
| 563 | /* Update and resume hrtimer */ |
| 564 | if (!dc) |
| 565 | kvm_mips_resume_hrtimer(vcpu, now, count); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | /** |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 570 | * kvm_mips_write_compare() - Modify compare and update timer. |
| 571 | * @vcpu: Virtual CPU. |
| 572 | * @compare: New CP0_Compare value. |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 573 | * @ack: Whether to acknowledge timer interrupt. |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 574 | * |
| 575 | * Update CP0_Compare to a new value and update the timeout. |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 576 | * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure |
| 577 | * any pending timer interrupt is preserved. |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 578 | */ |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 579 | void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack) |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 580 | { |
| 581 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 582 | int dc; |
| 583 | u32 old_compare = kvm_read_c0_guest_compare(cop0); |
| 584 | ktime_t now; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 585 | u32 count; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 586 | |
| 587 | /* if unchanged, must just be an ack */ |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 588 | if (old_compare == compare) { |
| 589 | if (!ack) |
| 590 | return; |
| 591 | kvm_mips_callbacks->dequeue_timer_int(vcpu); |
| 592 | kvm_write_c0_guest_compare(cop0, compare); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 593 | return; |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 594 | } |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 595 | |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 596 | /* freeze_hrtimer() takes care of timer interrupts <= count */ |
| 597 | dc = kvm_mips_count_disabled(vcpu); |
| 598 | if (!dc) |
| 599 | now = kvm_mips_freeze_hrtimer(vcpu, &count); |
| 600 | |
| 601 | if (ack) |
| 602 | kvm_mips_callbacks->dequeue_timer_int(vcpu); |
| 603 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 604 | kvm_write_c0_guest_compare(cop0, compare); |
| 605 | |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 606 | /* resume_hrtimer() takes care of timer interrupts > count */ |
| 607 | if (!dc) |
| 608 | kvm_mips_resume_hrtimer(vcpu, now, count); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
| 612 | * kvm_mips_count_disable() - Disable count. |
| 613 | * @vcpu: Virtual CPU. |
| 614 | * |
| 615 | * Disable the CP0_Count timer. A timer interrupt on or before the final stop |
| 616 | * time will be handled but not after. |
| 617 | * |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 618 | * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or |
| 619 | * count_ctl.DC has been set (count disabled). |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 620 | * |
| 621 | * Returns: The time that the timer was stopped. |
| 622 | */ |
| 623 | static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu) |
| 624 | { |
| 625 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 626 | u32 count; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 627 | ktime_t now; |
| 628 | |
| 629 | /* Stop hrtimer */ |
| 630 | hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 631 | |
| 632 | /* Set the static count from the dynamic count, handling pending TI */ |
| 633 | now = ktime_get(); |
| 634 | count = kvm_mips_read_count_running(vcpu, now); |
| 635 | kvm_write_c0_guest_count(cop0, count); |
| 636 | |
| 637 | return now; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC. |
| 642 | * @vcpu: Virtual CPU. |
| 643 | * |
| 644 | * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 645 | * before the final stop time will be handled if the timer isn't disabled by |
| 646 | * count_ctl.DC, but not after. |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 647 | * |
| 648 | * Assumes CP0_Cause.DC is clear (count enabled). |
| 649 | */ |
| 650 | void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu) |
| 651 | { |
| 652 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 653 | |
| 654 | kvm_set_c0_guest_cause(cop0, CAUSEF_DC); |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 655 | if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)) |
| 656 | kvm_mips_count_disable(vcpu); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /** |
| 660 | * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC. |
| 661 | * @vcpu: Virtual CPU. |
| 662 | * |
| 663 | * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 664 | * the start time will be handled if the timer isn't disabled by count_ctl.DC, |
| 665 | * potentially before even returning, so the caller should be careful with |
| 666 | * ordering of CP0_Cause modifications so as not to lose it. |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 667 | * |
| 668 | * Assumes CP0_Cause.DC is set (count disabled). |
| 669 | */ |
| 670 | void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu) |
| 671 | { |
| 672 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 673 | u32 count; |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 674 | |
| 675 | kvm_clear_c0_guest_cause(cop0, CAUSEF_DC); |
| 676 | |
| 677 | /* |
| 678 | * Set the dynamic count to match the static count. |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 679 | * This starts the hrtimer if count_ctl.DC allows it. |
| 680 | * Otherwise it conveniently updates the biases. |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 681 | */ |
| 682 | count = kvm_read_c0_guest_count(cop0); |
| 683 | kvm_mips_write_count(vcpu, count); |
| 684 | } |
| 685 | |
| 686 | /** |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 687 | * kvm_mips_set_count_ctl() - Update the count control KVM register. |
| 688 | * @vcpu: Virtual CPU. |
| 689 | * @count_ctl: Count control register new value. |
| 690 | * |
| 691 | * Set the count control KVM register. The timer is updated accordingly. |
| 692 | * |
| 693 | * Returns: -EINVAL if reserved bits are set. |
| 694 | * 0 on success. |
| 695 | */ |
| 696 | int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl) |
| 697 | { |
| 698 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 699 | s64 changed = count_ctl ^ vcpu->arch.count_ctl; |
| 700 | s64 delta; |
| 701 | ktime_t expire, now; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 702 | u32 count, compare; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 703 | |
| 704 | /* Only allow defined bits to be changed */ |
| 705 | if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC)) |
| 706 | return -EINVAL; |
| 707 | |
| 708 | /* Apply new value */ |
| 709 | vcpu->arch.count_ctl = count_ctl; |
| 710 | |
| 711 | /* Master CP0_Count disable */ |
| 712 | if (changed & KVM_REG_MIPS_COUNT_CTL_DC) { |
| 713 | /* Is CP0_Cause.DC already disabling CP0_Count? */ |
| 714 | if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) { |
| 715 | if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) |
| 716 | /* Just record the current time */ |
| 717 | vcpu->arch.count_resume = ktime_get(); |
| 718 | } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) { |
| 719 | /* disable timer and record current time */ |
| 720 | vcpu->arch.count_resume = kvm_mips_count_disable(vcpu); |
| 721 | } else { |
| 722 | /* |
| 723 | * Calculate timeout relative to static count at resume |
| 724 | * time (wrap 0 to 2^32). |
| 725 | */ |
| 726 | count = kvm_read_c0_guest_count(cop0); |
| 727 | compare = kvm_read_c0_guest_compare(cop0); |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 728 | delta = (u64)(u32)(compare - count - 1) + 1; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 729 | delta = div_u64(delta * NSEC_PER_SEC, |
| 730 | vcpu->arch.count_hz); |
| 731 | expire = ktime_add_ns(vcpu->arch.count_resume, delta); |
| 732 | |
| 733 | /* Handle pending interrupt */ |
| 734 | now = ktime_get(); |
| 735 | if (ktime_compare(now, expire) >= 0) |
| 736 | /* Nothing should be waiting on the timeout */ |
| 737 | kvm_mips_callbacks->queue_timer_int(vcpu); |
| 738 | |
| 739 | /* Resume hrtimer without changing bias */ |
| 740 | count = kvm_mips_read_count_running(vcpu, now); |
| 741 | kvm_mips_resume_hrtimer(vcpu, now, count); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * kvm_mips_set_count_resume() - Update the count resume KVM register. |
| 750 | * @vcpu: Virtual CPU. |
| 751 | * @count_resume: Count resume register new value. |
| 752 | * |
| 753 | * Set the count resume KVM register. |
| 754 | * |
| 755 | * Returns: -EINVAL if out of valid range (0..now). |
| 756 | * 0 on success. |
| 757 | */ |
| 758 | int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume) |
| 759 | { |
| 760 | /* |
| 761 | * It doesn't make sense for the resume time to be in the future, as it |
| 762 | * would be possible for the next interrupt to be more than a full |
| 763 | * period in the future. |
| 764 | */ |
| 765 | if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get())) |
| 766 | return -EINVAL; |
| 767 | |
| 768 | vcpu->arch.count_resume = ns_to_ktime(count_resume); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /** |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 773 | * kvm_mips_count_timeout() - Push timer forward on timeout. |
| 774 | * @vcpu: Virtual CPU. |
| 775 | * |
| 776 | * Handle an hrtimer event by push the hrtimer forward a period. |
| 777 | * |
| 778 | * Returns: The hrtimer_restart value to return to the hrtimer subsystem. |
| 779 | */ |
| 780 | enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu) |
| 781 | { |
| 782 | /* Add the Count period to the current expiry time */ |
| 783 | hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer, |
| 784 | vcpu->arch.count_period); |
| 785 | return HRTIMER_RESTART; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu) |
| 789 | { |
| 790 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 791 | enum emulation_result er = EMULATE_DONE; |
| 792 | |
James Hogan | ede5f3e | 2016-10-25 16:11:11 +0100 | [diff] [blame] | 793 | if (kvm_read_c0_guest_status(cop0) & ST0_ERL) { |
| 794 | kvm_clear_c0_guest_status(cop0, ST0_ERL); |
| 795 | vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0); |
| 796 | } else if (kvm_read_c0_guest_status(cop0) & ST0_EXL) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 797 | kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc, |
| 798 | kvm_read_c0_guest_epc(cop0)); |
| 799 | kvm_clear_c0_guest_status(cop0, ST0_EXL); |
| 800 | vcpu->arch.pc = kvm_read_c0_guest_epc(cop0); |
| 801 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 802 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 803 | kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n", |
| 804 | vcpu->arch.pc); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 805 | er = EMULATE_FAIL; |
| 806 | } |
| 807 | |
| 808 | return er; |
| 809 | } |
| 810 | |
| 811 | enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu) |
| 812 | { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 813 | kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc, |
| 814 | vcpu->arch.pending_exceptions); |
| 815 | |
| 816 | ++vcpu->stat.wait_exits; |
James Hogan | 1e09e86 | 2016-06-14 09:40:12 +0100 | [diff] [blame] | 817 | trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 818 | if (!vcpu->arch.pending_exceptions) { |
| 819 | vcpu->arch.wait = 1; |
| 820 | kvm_vcpu_block(vcpu); |
| 821 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 822 | /* |
| 823 | * We we are runnable, then definitely go off to user space to |
| 824 | * check if any I/O interrupts are pending. |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 825 | */ |
| 826 | if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) { |
| 827 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
| 828 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
| 829 | } |
| 830 | } |
| 831 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 832 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 835 | /* |
| 836 | * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that |
| 837 | * we can catch this, if things ever change |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 838 | */ |
| 839 | enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu) |
| 840 | { |
| 841 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 842 | unsigned long pc = vcpu->arch.pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 843 | |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 844 | kvm_err("[%#lx] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0)); |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 845 | return EMULATE_FAIL; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 846 | } |
| 847 | |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 848 | /** |
| 849 | * kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map. |
| 850 | * @vcpu: VCPU with changed mappings. |
| 851 | * @tlb: TLB entry being removed. |
| 852 | * |
| 853 | * This is called to indicate a single change in guest MMU mappings, so that we |
| 854 | * can arrange TLB flushes on this and other CPUs. |
| 855 | */ |
| 856 | static void kvm_mips_invalidate_guest_tlb(struct kvm_vcpu *vcpu, |
| 857 | struct kvm_mips_tlb *tlb) |
| 858 | { |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 859 | struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm; |
| 860 | struct mm_struct *user_mm = &vcpu->arch.guest_user_mm; |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 861 | int cpu, i; |
| 862 | bool user; |
| 863 | |
| 864 | /* No need to flush for entries which are already invalid */ |
| 865 | if (!((tlb->tlb_lo[0] | tlb->tlb_lo[1]) & ENTRYLO_V)) |
| 866 | return; |
| 867 | /* User address space doesn't need flushing for KSeg2/3 changes */ |
| 868 | user = tlb->tlb_hi < KVM_GUEST_KSEG0; |
| 869 | |
| 870 | preempt_disable(); |
| 871 | |
| 872 | /* |
| 873 | * Probe the shadow host TLB for the entry being overwritten, if one |
| 874 | * matches, invalidate it |
| 875 | */ |
James Hogan | 57e3869 | 2016-10-08 00:15:52 +0100 | [diff] [blame^] | 876 | kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi, user, true); |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 877 | |
| 878 | /* Invalidate the whole ASID on other CPUs */ |
| 879 | cpu = smp_processor_id(); |
| 880 | for_each_possible_cpu(i) { |
| 881 | if (i == cpu) |
| 882 | continue; |
| 883 | if (user) |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 884 | cpu_context(i, user_mm) = 0; |
| 885 | cpu_context(i, kern_mm) = 0; |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | preempt_enable(); |
| 889 | } |
| 890 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 891 | /* Write Guest TLB Entry @ Index */ |
| 892 | enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu) |
| 893 | { |
| 894 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 895 | int index = kvm_read_c0_guest_index(cop0); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 896 | struct kvm_mips_tlb *tlb = NULL; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 897 | unsigned long pc = vcpu->arch.pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 898 | |
| 899 | if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 900 | kvm_debug("%s: illegal index: %d\n", __func__, index); |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 901 | kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n", |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 902 | pc, index, kvm_read_c0_guest_entryhi(cop0), |
| 903 | kvm_read_c0_guest_entrylo0(cop0), |
| 904 | kvm_read_c0_guest_entrylo1(cop0), |
| 905 | kvm_read_c0_guest_pagemask(cop0)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 906 | index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE; |
| 907 | } |
| 908 | |
| 909 | tlb = &vcpu->arch.guest_tlb[index]; |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 910 | |
| 911 | kvm_mips_invalidate_guest_tlb(vcpu, tlb); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 912 | |
| 913 | tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0); |
| 914 | tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0); |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 915 | tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0); |
| 916 | tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 917 | |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 918 | kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n", |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 919 | pc, index, kvm_read_c0_guest_entryhi(cop0), |
| 920 | kvm_read_c0_guest_entrylo0(cop0), |
| 921 | kvm_read_c0_guest_entrylo1(cop0), |
| 922 | kvm_read_c0_guest_pagemask(cop0)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 923 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 924 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* Write Guest TLB Entry @ Random Index */ |
| 928 | enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu) |
| 929 | { |
| 930 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 931 | struct kvm_mips_tlb *tlb = NULL; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 932 | unsigned long pc = vcpu->arch.pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 933 | int index; |
| 934 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 935 | get_random_bytes(&index, sizeof(index)); |
| 936 | index &= (KVM_MIPS_GUEST_TLB_SIZE - 1); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 937 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 938 | tlb = &vcpu->arch.guest_tlb[index]; |
| 939 | |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 940 | kvm_mips_invalidate_guest_tlb(vcpu, tlb); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 941 | |
| 942 | tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0); |
| 943 | tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0); |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 944 | tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0); |
| 945 | tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 946 | |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 947 | kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n", |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 948 | pc, index, kvm_read_c0_guest_entryhi(cop0), |
| 949 | kvm_read_c0_guest_entrylo0(cop0), |
| 950 | kvm_read_c0_guest_entrylo1(cop0)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 951 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 952 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu) |
| 956 | { |
| 957 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 958 | long entryhi = kvm_read_c0_guest_entryhi(cop0); |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 959 | unsigned long pc = vcpu->arch.pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 960 | int index = -1; |
| 961 | |
| 962 | index = kvm_mips_guest_tlb_lookup(vcpu, entryhi); |
| 963 | |
| 964 | kvm_write_c0_guest_index(cop0, index); |
| 965 | |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 966 | kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi, |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 967 | index); |
| 968 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 969 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 970 | } |
| 971 | |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 972 | /** |
| 973 | * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1 |
| 974 | * @vcpu: Virtual CPU. |
| 975 | * |
| 976 | * Finds the mask of bits which are writable in the guest's Config1 CP0 |
| 977 | * register, by userland (currently read-only to the guest). |
| 978 | */ |
| 979 | unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu) |
| 980 | { |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 981 | unsigned int mask = 0; |
| 982 | |
| 983 | /* Permit FPU to be present if FPU is supported */ |
| 984 | if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) |
| 985 | mask |= MIPS_CONF1_FP; |
| 986 | |
| 987 | return mask; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /** |
| 991 | * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3 |
| 992 | * @vcpu: Virtual CPU. |
| 993 | * |
| 994 | * Finds the mask of bits which are writable in the guest's Config3 CP0 |
| 995 | * register, by userland (currently read-only to the guest). |
| 996 | */ |
| 997 | unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu) |
| 998 | { |
James Hogan | cef061d0 | 2016-06-15 19:29:54 +0100 | [diff] [blame] | 999 | /* Config4 and ULRI are optional */ |
| 1000 | unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI; |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1001 | |
| 1002 | /* Permit MSA to be present if MSA is supported */ |
| 1003 | if (kvm_mips_guest_can_have_msa(&vcpu->arch)) |
| 1004 | mask |= MIPS_CONF3_MSA; |
| 1005 | |
| 1006 | return mask; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4 |
| 1011 | * @vcpu: Virtual CPU. |
| 1012 | * |
| 1013 | * Finds the mask of bits which are writable in the guest's Config4 CP0 |
| 1014 | * register, by userland (currently read-only to the guest). |
| 1015 | */ |
| 1016 | unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu) |
| 1017 | { |
| 1018 | /* Config5 is optional */ |
James Hogan | 0510870 | 2016-06-15 19:29:56 +0100 | [diff] [blame] | 1019 | unsigned int mask = MIPS_CONF_M; |
| 1020 | |
| 1021 | /* KScrExist */ |
| 1022 | mask |= (unsigned int)vcpu->arch.kscratch_enabled << 16; |
| 1023 | |
| 1024 | return mask; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5 |
| 1029 | * @vcpu: Virtual CPU. |
| 1030 | * |
| 1031 | * Finds the mask of bits which are writable in the guest's Config5 CP0 |
| 1032 | * register, by the guest itself. |
| 1033 | */ |
| 1034 | unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu) |
| 1035 | { |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1036 | unsigned int mask = 0; |
| 1037 | |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1038 | /* Permit MSAEn changes if MSA supported and enabled */ |
| 1039 | if (kvm_mips_guest_has_msa(&vcpu->arch)) |
| 1040 | mask |= MIPS_CONF5_MSAEN; |
| 1041 | |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1042 | /* |
| 1043 | * Permit guest FPU mode changes if FPU is enabled and the relevant |
| 1044 | * feature exists according to FIR register. |
| 1045 | */ |
| 1046 | if (kvm_mips_guest_has_fpu(&vcpu->arch)) { |
| 1047 | if (cpu_has_fre) |
| 1048 | mask |= MIPS_CONF5_FRE; |
| 1049 | /* We don't support UFR or UFE */ |
| 1050 | } |
| 1051 | |
| 1052 | return mask; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1055 | enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst, |
| 1056 | u32 *opc, u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 1057 | struct kvm_run *run, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1058 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1059 | { |
| 1060 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 1061 | struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1062 | enum emulation_result er = EMULATE_DONE; |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1063 | u32 rt, rd, sel; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1064 | unsigned long curr_pc; |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 1065 | int cpu, i; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * Update PC and hold onto current PC in case there is |
| 1069 | * an error and we want to rollback the PC |
| 1070 | */ |
| 1071 | curr_pc = vcpu->arch.pc; |
| 1072 | er = update_pc(vcpu, cause); |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1073 | if (er == EMULATE_FAIL) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1074 | return er; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1075 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1076 | if (inst.co_format.co) { |
| 1077 | switch (inst.co_format.func) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1078 | case tlbr_op: /* Read indexed TLB entry */ |
| 1079 | er = kvm_mips_emul_tlbr(vcpu); |
| 1080 | break; |
| 1081 | case tlbwi_op: /* Write indexed */ |
| 1082 | er = kvm_mips_emul_tlbwi(vcpu); |
| 1083 | break; |
| 1084 | case tlbwr_op: /* Write random */ |
| 1085 | er = kvm_mips_emul_tlbwr(vcpu); |
| 1086 | break; |
| 1087 | case tlbp_op: /* TLB Probe */ |
| 1088 | er = kvm_mips_emul_tlbp(vcpu); |
| 1089 | break; |
| 1090 | case rfe_op: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1091 | kvm_err("!!!COP0_RFE!!!\n"); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1092 | break; |
| 1093 | case eret_op: |
| 1094 | er = kvm_mips_emul_eret(vcpu); |
| 1095 | goto dont_update_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1096 | case wait_op: |
| 1097 | er = kvm_mips_emul_wait(vcpu); |
| 1098 | break; |
| 1099 | } |
| 1100 | } else { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1101 | rt = inst.c0r_format.rt; |
| 1102 | rd = inst.c0r_format.rd; |
| 1103 | sel = inst.c0r_format.sel; |
| 1104 | |
| 1105 | switch (inst.c0r_format.rs) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1106 | case mfc_op: |
| 1107 | #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS |
| 1108 | cop0->stat[rd][sel]++; |
| 1109 | #endif |
| 1110 | /* Get reg */ |
| 1111 | if ((rd == MIPS_CP0_COUNT) && (sel == 0)) { |
James Hogan | 172e02d | 2016-07-08 11:53:28 +0100 | [diff] [blame] | 1112 | vcpu->arch.gprs[rt] = |
| 1113 | (s32)kvm_mips_read_count(vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1114 | } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) { |
| 1115 | vcpu->arch.gprs[rt] = 0x0; |
| 1116 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
| 1117 | kvm_mips_trans_mfc0(inst, opc, vcpu); |
| 1118 | #endif |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1119 | } else { |
James Hogan | 172e02d | 2016-07-08 11:53:28 +0100 | [diff] [blame] | 1120 | vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel]; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1121 | |
| 1122 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
| 1123 | kvm_mips_trans_mfc0(inst, opc, vcpu); |
| 1124 | #endif |
| 1125 | } |
| 1126 | |
James Hogan | 6398da1 | 2016-06-14 09:40:15 +0100 | [diff] [blame] | 1127 | trace_kvm_hwr(vcpu, KVM_TRACE_MFC0, |
| 1128 | KVM_TRACE_COP0(rd, sel), |
| 1129 | vcpu->arch.gprs[rt]); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1130 | break; |
| 1131 | |
| 1132 | case dmfc_op: |
| 1133 | vcpu->arch.gprs[rt] = cop0->reg[rd][sel]; |
James Hogan | 6398da1 | 2016-06-14 09:40:15 +0100 | [diff] [blame] | 1134 | |
| 1135 | trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0, |
| 1136 | KVM_TRACE_COP0(rd, sel), |
| 1137 | vcpu->arch.gprs[rt]); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1138 | break; |
| 1139 | |
| 1140 | case mtc_op: |
| 1141 | #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS |
| 1142 | cop0->stat[rd][sel]++; |
| 1143 | #endif |
James Hogan | 6398da1 | 2016-06-14 09:40:15 +0100 | [diff] [blame] | 1144 | trace_kvm_hwr(vcpu, KVM_TRACE_MTC0, |
| 1145 | KVM_TRACE_COP0(rd, sel), |
| 1146 | vcpu->arch.gprs[rt]); |
| 1147 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1148 | if ((rd == MIPS_CP0_TLB_INDEX) |
| 1149 | && (vcpu->arch.gprs[rt] >= |
| 1150 | KVM_MIPS_GUEST_TLB_SIZE)) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1151 | kvm_err("Invalid TLB Index: %ld", |
| 1152 | vcpu->arch.gprs[rt]); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1153 | er = EMULATE_FAIL; |
| 1154 | break; |
| 1155 | } |
| 1156 | #define C0_EBASE_CORE_MASK 0xff |
| 1157 | if ((rd == MIPS_CP0_PRID) && (sel == 1)) { |
| 1158 | /* Preserve CORE number */ |
| 1159 | kvm_change_c0_guest_ebase(cop0, |
| 1160 | ~(C0_EBASE_CORE_MASK), |
| 1161 | vcpu->arch.gprs[rt]); |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1162 | kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n", |
| 1163 | kvm_read_c0_guest_ebase(cop0)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1164 | } else if (rd == MIPS_CP0_TLB_HI && sel == 0) { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1165 | u32 nasid = |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 1166 | vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID; |
James Hogan | bf18db4 | 2016-09-16 13:14:09 +0100 | [diff] [blame] | 1167 | if (((kvm_read_c0_guest_entryhi(cop0) & |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 1168 | KVM_ENTRYHI_ASID) != nasid)) { |
James Hogan | 9887d1c | 2016-06-14 09:40:13 +0100 | [diff] [blame] | 1169 | trace_kvm_asid_change(vcpu, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1170 | kvm_read_c0_guest_entryhi(cop0) |
James Hogan | 9887d1c | 2016-06-14 09:40:13 +0100 | [diff] [blame] | 1171 | & KVM_ENTRYHI_ASID, |
| 1172 | nasid); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1173 | |
James Hogan | 25b08c7 | 2016-09-16 00:06:43 +0100 | [diff] [blame] | 1174 | /* |
| 1175 | * Regenerate/invalidate kernel MMU |
| 1176 | * context. |
| 1177 | * The user MMU context will be |
| 1178 | * regenerated lazily on re-entry to |
| 1179 | * guest user if the guest ASID actually |
| 1180 | * changes. |
| 1181 | */ |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 1182 | preempt_disable(); |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 1183 | cpu = smp_processor_id(); |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 1184 | kvm_get_new_mmu_context(kern_mm, |
James Hogan | 25b08c7 | 2016-09-16 00:06:43 +0100 | [diff] [blame] | 1185 | cpu, vcpu); |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 1186 | for_each_possible_cpu(i) |
James Hogan | 25b08c7 | 2016-09-16 00:06:43 +0100 | [diff] [blame] | 1187 | if (i != cpu) |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 1188 | cpu_context(i, kern_mm) = 0; |
James Hogan | 91e4f1b | 2016-09-15 17:20:06 +0100 | [diff] [blame] | 1189 | preempt_enable(); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1190 | } |
| 1191 | kvm_write_c0_guest_entryhi(cop0, |
| 1192 | vcpu->arch.gprs[rt]); |
| 1193 | } |
| 1194 | /* Are we writing to COUNT */ |
| 1195 | else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) { |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 1196 | kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1197 | goto done; |
| 1198 | } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1199 | /* If we are writing to COMPARE */ |
| 1200 | /* Clear pending timer interrupt, if any */ |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 1201 | kvm_mips_write_compare(vcpu, |
James Hogan | b45bacd | 2016-04-22 10:38:46 +0100 | [diff] [blame] | 1202 | vcpu->arch.gprs[rt], |
| 1203 | true); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1204 | } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) { |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1205 | unsigned int old_val, val, change; |
| 1206 | |
| 1207 | old_val = kvm_read_c0_guest_status(cop0); |
| 1208 | val = vcpu->arch.gprs[rt]; |
| 1209 | change = val ^ old_val; |
| 1210 | |
| 1211 | /* Make sure that the NMI bit is never set */ |
| 1212 | val &= ~ST0_NMI; |
| 1213 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1214 | /* |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1215 | * Don't allow CU1 or FR to be set unless FPU |
| 1216 | * capability enabled and exists in guest |
| 1217 | * configuration. |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1218 | */ |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1219 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 1220 | val &= ~(ST0_CU1 | ST0_FR); |
| 1221 | |
| 1222 | /* |
| 1223 | * Also don't allow FR to be set if host doesn't |
| 1224 | * support it. |
| 1225 | */ |
| 1226 | if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64)) |
| 1227 | val &= ~ST0_FR; |
| 1228 | |
| 1229 | |
| 1230 | /* Handle changes in FPU mode */ |
| 1231 | preempt_disable(); |
| 1232 | |
| 1233 | /* |
| 1234 | * FPU and Vector register state is made |
| 1235 | * UNPREDICTABLE by a change of FR, so don't |
| 1236 | * even bother saving it. |
| 1237 | */ |
| 1238 | if (change & ST0_FR) |
| 1239 | kvm_drop_fpu(vcpu); |
| 1240 | |
| 1241 | /* |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1242 | * If MSA state is already live, it is undefined |
| 1243 | * how it interacts with FR=0 FPU state, and we |
| 1244 | * don't want to hit reserved instruction |
| 1245 | * exceptions trying to save the MSA state later |
| 1246 | * when CU=1 && FR=1, so play it safe and save |
| 1247 | * it first. |
| 1248 | */ |
| 1249 | if (change & ST0_CU1 && !(val & ST0_FR) && |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1250 | vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1251 | kvm_lose_fpu(vcpu); |
| 1252 | |
| 1253 | /* |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1254 | * Propagate CU1 (FPU enable) changes |
| 1255 | * immediately if the FPU context is already |
| 1256 | * loaded. When disabling we leave the context |
| 1257 | * loaded so it can be quickly enabled again in |
| 1258 | * the near future. |
| 1259 | */ |
| 1260 | if (change & ST0_CU1 && |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1261 | vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1262 | change_c0_status(ST0_CU1, val); |
| 1263 | |
| 1264 | preempt_enable(); |
| 1265 | |
| 1266 | kvm_write_c0_guest_status(cop0, val); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1267 | |
| 1268 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1269 | /* |
| 1270 | * If FPU present, we need CU1/FR bits to take |
| 1271 | * effect fairly soon. |
| 1272 | */ |
| 1273 | if (!kvm_mips_guest_has_fpu(&vcpu->arch)) |
| 1274 | kvm_mips_trans_mtc0(inst, opc, vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1275 | #endif |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1276 | } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) { |
| 1277 | unsigned int old_val, val, change, wrmask; |
| 1278 | |
| 1279 | old_val = kvm_read_c0_guest_config5(cop0); |
| 1280 | val = vcpu->arch.gprs[rt]; |
| 1281 | |
| 1282 | /* Only a few bits are writable in Config5 */ |
| 1283 | wrmask = kvm_mips_config5_wrmask(vcpu); |
| 1284 | change = (val ^ old_val) & wrmask; |
| 1285 | val = old_val ^ change; |
| 1286 | |
| 1287 | |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1288 | /* Handle changes in FPU/MSA modes */ |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1289 | preempt_disable(); |
| 1290 | |
| 1291 | /* |
| 1292 | * Propagate FRE changes immediately if the FPU |
| 1293 | * context is already loaded. |
| 1294 | */ |
| 1295 | if (change & MIPS_CONF5_FRE && |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1296 | vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1297 | change_c0_config5(MIPS_CONF5_FRE, val); |
| 1298 | |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1299 | /* |
| 1300 | * Propagate MSAEn changes immediately if the |
| 1301 | * MSA context is already loaded. When disabling |
| 1302 | * we leave the context loaded so it can be |
| 1303 | * quickly enabled again in the near future. |
| 1304 | */ |
| 1305 | if (change & MIPS_CONF5_MSAEN && |
James Hogan | f943176 | 2016-06-14 09:40:10 +0100 | [diff] [blame] | 1306 | vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) |
James Hogan | 2b6009d | 2015-02-06 23:01:00 +0000 | [diff] [blame] | 1307 | change_c0_config5(MIPS_CONF5_MSAEN, |
| 1308 | val); |
| 1309 | |
James Hogan | 6cdc65e | 2015-02-03 13:59:38 +0000 | [diff] [blame] | 1310 | preempt_enable(); |
| 1311 | |
| 1312 | kvm_write_c0_guest_config5(cop0, val); |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 1313 | } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1314 | u32 old_cause, new_cause; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1315 | |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 1316 | old_cause = kvm_read_c0_guest_cause(cop0); |
| 1317 | new_cause = vcpu->arch.gprs[rt]; |
| 1318 | /* Update R/W bits */ |
| 1319 | kvm_change_c0_guest_cause(cop0, 0x08800300, |
| 1320 | new_cause); |
| 1321 | /* DC bit enabling/disabling timer? */ |
| 1322 | if ((old_cause ^ new_cause) & CAUSEF_DC) { |
| 1323 | if (new_cause & CAUSEF_DC) |
| 1324 | kvm_mips_count_disable_cause(vcpu); |
| 1325 | else |
| 1326 | kvm_mips_count_enable_cause(vcpu); |
| 1327 | } |
James Hogan | cef061d0 | 2016-06-15 19:29:54 +0100 | [diff] [blame] | 1328 | } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) { |
| 1329 | u32 mask = MIPS_HWRENA_CPUNUM | |
| 1330 | MIPS_HWRENA_SYNCISTEP | |
| 1331 | MIPS_HWRENA_CC | |
| 1332 | MIPS_HWRENA_CCRES; |
| 1333 | |
| 1334 | if (kvm_read_c0_guest_config3(cop0) & |
| 1335 | MIPS_CONF3_ULRI) |
| 1336 | mask |= MIPS_HWRENA_ULR; |
| 1337 | cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1338 | } else { |
| 1339 | cop0->reg[rd][sel] = vcpu->arch.gprs[rt]; |
| 1340 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
| 1341 | kvm_mips_trans_mtc0(inst, opc, vcpu); |
| 1342 | #endif |
| 1343 | } |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1344 | break; |
| 1345 | |
| 1346 | case dmtc_op: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1347 | kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n", |
| 1348 | vcpu->arch.pc, rt, rd, sel); |
James Hogan | 6398da1 | 2016-06-14 09:40:15 +0100 | [diff] [blame] | 1349 | trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0, |
| 1350 | KVM_TRACE_COP0(rd, sel), |
| 1351 | vcpu->arch.gprs[rt]); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1352 | er = EMULATE_FAIL; |
| 1353 | break; |
| 1354 | |
James Hogan | b2c5963 | 2015-12-16 23:49:38 +0000 | [diff] [blame] | 1355 | case mfmc0_op: |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1356 | #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS |
| 1357 | cop0->stat[MIPS_CP0_STATUS][0]++; |
| 1358 | #endif |
James Hogan | caa1faa | 2015-12-16 23:49:26 +0000 | [diff] [blame] | 1359 | if (rt != 0) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1360 | vcpu->arch.gprs[rt] = |
| 1361 | kvm_read_c0_guest_status(cop0); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1362 | /* EI */ |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1363 | if (inst.mfmc0_format.sc) { |
James Hogan | b2c5963 | 2015-12-16 23:49:38 +0000 | [diff] [blame] | 1364 | kvm_debug("[%#lx] mfmc0_op: EI\n", |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1365 | vcpu->arch.pc); |
| 1366 | kvm_set_c0_guest_status(cop0, ST0_IE); |
| 1367 | } else { |
James Hogan | b2c5963 | 2015-12-16 23:49:38 +0000 | [diff] [blame] | 1368 | kvm_debug("[%#lx] mfmc0_op: DI\n", |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1369 | vcpu->arch.pc); |
| 1370 | kvm_clear_c0_guest_status(cop0, ST0_IE); |
| 1371 | } |
| 1372 | |
| 1373 | break; |
| 1374 | |
| 1375 | case wrpgpr_op: |
| 1376 | { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1377 | u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf; |
| 1378 | u32 pss = |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1379 | (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1380 | /* |
| 1381 | * We don't support any shadow register sets, so |
| 1382 | * SRSCtl[PSS] == SRSCtl[CSS] = 0 |
| 1383 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1384 | if (css || pss) { |
| 1385 | er = EMULATE_FAIL; |
| 1386 | break; |
| 1387 | } |
| 1388 | kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd, |
| 1389 | vcpu->arch.gprs[rt]); |
| 1390 | vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt]; |
| 1391 | } |
| 1392 | break; |
| 1393 | default: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1394 | kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n", |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1395 | vcpu->arch.pc, inst.c0r_format.rs); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1396 | er = EMULATE_FAIL; |
| 1397 | break; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | done: |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1402 | /* Rollback PC only if emulation was unsuccessful */ |
| 1403 | if (er == EMULATE_FAIL) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1404 | vcpu->arch.pc = curr_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1405 | |
| 1406 | dont_update_pc: |
| 1407 | /* |
| 1408 | * This is for special instructions whose emulation |
| 1409 | * updates the PC, so do not overwrite the PC under |
| 1410 | * any circumstances |
| 1411 | */ |
| 1412 | |
| 1413 | return er; |
| 1414 | } |
| 1415 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1416 | enum emulation_result kvm_mips_emulate_store(union mips_instruction inst, |
| 1417 | u32 cause, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1418 | struct kvm_run *run, |
| 1419 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1420 | { |
| 1421 | enum emulation_result er = EMULATE_DO_MMIO; |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1422 | u32 rt; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1423 | u32 bytes; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1424 | void *data = run->mmio.data; |
| 1425 | unsigned long curr_pc; |
| 1426 | |
| 1427 | /* |
| 1428 | * Update PC and hold onto current PC in case there is |
| 1429 | * an error and we want to rollback the PC |
| 1430 | */ |
| 1431 | curr_pc = vcpu->arch.pc; |
| 1432 | er = update_pc(vcpu, cause); |
| 1433 | if (er == EMULATE_FAIL) |
| 1434 | return er; |
| 1435 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1436 | rt = inst.i_format.rt; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1437 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1438 | switch (inst.i_format.opcode) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1439 | case sb_op: |
| 1440 | bytes = 1; |
| 1441 | if (bytes > sizeof(run->mmio.data)) { |
| 1442 | kvm_err("%s: bad MMIO length: %d\n", __func__, |
| 1443 | run->mmio.len); |
| 1444 | } |
| 1445 | run->mmio.phys_addr = |
| 1446 | kvm_mips_callbacks->gva_to_gpa(vcpu->arch. |
| 1447 | host_cp0_badvaddr); |
| 1448 | if (run->mmio.phys_addr == KVM_INVALID_ADDR) { |
| 1449 | er = EMULATE_FAIL; |
| 1450 | break; |
| 1451 | } |
| 1452 | run->mmio.len = bytes; |
| 1453 | run->mmio.is_write = 1; |
| 1454 | vcpu->mmio_needed = 1; |
| 1455 | vcpu->mmio_is_write = 1; |
| 1456 | *(u8 *) data = vcpu->arch.gprs[rt]; |
| 1457 | kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n", |
| 1458 | vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt], |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1459 | *(u8 *) data); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1460 | |
| 1461 | break; |
| 1462 | |
| 1463 | case sw_op: |
| 1464 | bytes = 4; |
| 1465 | if (bytes > sizeof(run->mmio.data)) { |
| 1466 | kvm_err("%s: bad MMIO length: %d\n", __func__, |
| 1467 | run->mmio.len); |
| 1468 | } |
| 1469 | run->mmio.phys_addr = |
| 1470 | kvm_mips_callbacks->gva_to_gpa(vcpu->arch. |
| 1471 | host_cp0_badvaddr); |
| 1472 | if (run->mmio.phys_addr == KVM_INVALID_ADDR) { |
| 1473 | er = EMULATE_FAIL; |
| 1474 | break; |
| 1475 | } |
| 1476 | |
| 1477 | run->mmio.len = bytes; |
| 1478 | run->mmio.is_write = 1; |
| 1479 | vcpu->mmio_needed = 1; |
| 1480 | vcpu->mmio_is_write = 1; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1481 | *(u32 *) data = vcpu->arch.gprs[rt]; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1482 | |
| 1483 | kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n", |
| 1484 | vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr, |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1485 | vcpu->arch.gprs[rt], *(u32 *) data); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1486 | break; |
| 1487 | |
| 1488 | case sh_op: |
| 1489 | bytes = 2; |
| 1490 | if (bytes > sizeof(run->mmio.data)) { |
| 1491 | kvm_err("%s: bad MMIO length: %d\n", __func__, |
| 1492 | run->mmio.len); |
| 1493 | } |
| 1494 | run->mmio.phys_addr = |
| 1495 | kvm_mips_callbacks->gva_to_gpa(vcpu->arch. |
| 1496 | host_cp0_badvaddr); |
| 1497 | if (run->mmio.phys_addr == KVM_INVALID_ADDR) { |
| 1498 | er = EMULATE_FAIL; |
| 1499 | break; |
| 1500 | } |
| 1501 | |
| 1502 | run->mmio.len = bytes; |
| 1503 | run->mmio.is_write = 1; |
| 1504 | vcpu->mmio_needed = 1; |
| 1505 | vcpu->mmio_is_write = 1; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1506 | *(u16 *) data = vcpu->arch.gprs[rt]; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1507 | |
| 1508 | kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n", |
| 1509 | vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr, |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1510 | vcpu->arch.gprs[rt], *(u32 *) data); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1511 | break; |
| 1512 | |
| 1513 | default: |
James Hogan | d86c1eb | 2016-06-14 09:40:17 +0100 | [diff] [blame] | 1514 | kvm_err("Store not yet supported (inst=0x%08x)\n", |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1515 | inst.word); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1516 | er = EMULATE_FAIL; |
| 1517 | break; |
| 1518 | } |
| 1519 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1520 | /* Rollback PC if emulation was unsuccessful */ |
| 1521 | if (er == EMULATE_FAIL) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1522 | vcpu->arch.pc = curr_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1523 | |
| 1524 | return er; |
| 1525 | } |
| 1526 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1527 | enum emulation_result kvm_mips_emulate_load(union mips_instruction inst, |
| 1528 | u32 cause, struct kvm_run *run, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1529 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1530 | { |
| 1531 | enum emulation_result er = EMULATE_DO_MMIO; |
James Hogan | e1e575f6 | 2016-10-25 16:11:12 +0100 | [diff] [blame] | 1532 | unsigned long curr_pc; |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1533 | u32 op, rt; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1534 | u32 bytes; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1535 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1536 | rt = inst.i_format.rt; |
| 1537 | op = inst.i_format.opcode; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1538 | |
James Hogan | e1e575f6 | 2016-10-25 16:11:12 +0100 | [diff] [blame] | 1539 | /* |
| 1540 | * Find the resume PC now while we have safe and easy access to the |
| 1541 | * prior branch instruction, and save it for |
| 1542 | * kvm_mips_complete_mmio_load() to restore later. |
| 1543 | */ |
| 1544 | curr_pc = vcpu->arch.pc; |
| 1545 | er = update_pc(vcpu, cause); |
| 1546 | if (er == EMULATE_FAIL) |
| 1547 | return er; |
| 1548 | vcpu->arch.io_pc = vcpu->arch.pc; |
| 1549 | vcpu->arch.pc = curr_pc; |
| 1550 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1551 | vcpu->arch.io_gpr = rt; |
| 1552 | |
| 1553 | switch (op) { |
| 1554 | case lw_op: |
| 1555 | bytes = 4; |
| 1556 | if (bytes > sizeof(run->mmio.data)) { |
| 1557 | kvm_err("%s: bad MMIO length: %d\n", __func__, |
| 1558 | run->mmio.len); |
| 1559 | er = EMULATE_FAIL; |
| 1560 | break; |
| 1561 | } |
| 1562 | run->mmio.phys_addr = |
| 1563 | kvm_mips_callbacks->gva_to_gpa(vcpu->arch. |
| 1564 | host_cp0_badvaddr); |
| 1565 | if (run->mmio.phys_addr == KVM_INVALID_ADDR) { |
| 1566 | er = EMULATE_FAIL; |
| 1567 | break; |
| 1568 | } |
| 1569 | |
| 1570 | run->mmio.len = bytes; |
| 1571 | run->mmio.is_write = 0; |
| 1572 | vcpu->mmio_needed = 1; |
| 1573 | vcpu->mmio_is_write = 0; |
| 1574 | break; |
| 1575 | |
| 1576 | case lh_op: |
| 1577 | case lhu_op: |
| 1578 | bytes = 2; |
| 1579 | if (bytes > sizeof(run->mmio.data)) { |
| 1580 | kvm_err("%s: bad MMIO length: %d\n", __func__, |
| 1581 | run->mmio.len); |
| 1582 | er = EMULATE_FAIL; |
| 1583 | break; |
| 1584 | } |
| 1585 | run->mmio.phys_addr = |
| 1586 | kvm_mips_callbacks->gva_to_gpa(vcpu->arch. |
| 1587 | host_cp0_badvaddr); |
| 1588 | if (run->mmio.phys_addr == KVM_INVALID_ADDR) { |
| 1589 | er = EMULATE_FAIL; |
| 1590 | break; |
| 1591 | } |
| 1592 | |
| 1593 | run->mmio.len = bytes; |
| 1594 | run->mmio.is_write = 0; |
| 1595 | vcpu->mmio_needed = 1; |
| 1596 | vcpu->mmio_is_write = 0; |
| 1597 | |
| 1598 | if (op == lh_op) |
| 1599 | vcpu->mmio_needed = 2; |
| 1600 | else |
| 1601 | vcpu->mmio_needed = 1; |
| 1602 | |
| 1603 | break; |
| 1604 | |
| 1605 | case lbu_op: |
| 1606 | case lb_op: |
| 1607 | bytes = 1; |
| 1608 | if (bytes > sizeof(run->mmio.data)) { |
| 1609 | kvm_err("%s: bad MMIO length: %d\n", __func__, |
| 1610 | run->mmio.len); |
| 1611 | er = EMULATE_FAIL; |
| 1612 | break; |
| 1613 | } |
| 1614 | run->mmio.phys_addr = |
| 1615 | kvm_mips_callbacks->gva_to_gpa(vcpu->arch. |
| 1616 | host_cp0_badvaddr); |
| 1617 | if (run->mmio.phys_addr == KVM_INVALID_ADDR) { |
| 1618 | er = EMULATE_FAIL; |
| 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | run->mmio.len = bytes; |
| 1623 | run->mmio.is_write = 0; |
| 1624 | vcpu->mmio_is_write = 0; |
| 1625 | |
| 1626 | if (op == lb_op) |
| 1627 | vcpu->mmio_needed = 2; |
| 1628 | else |
| 1629 | vcpu->mmio_needed = 1; |
| 1630 | |
| 1631 | break; |
| 1632 | |
| 1633 | default: |
James Hogan | d86c1eb | 2016-06-14 09:40:17 +0100 | [diff] [blame] | 1634 | kvm_err("Load not yet supported (inst=0x%08x)\n", |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1635 | inst.word); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1636 | er = EMULATE_FAIL; |
| 1637 | break; |
| 1638 | } |
| 1639 | |
| 1640 | return er; |
| 1641 | } |
| 1642 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1643 | enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst, |
| 1644 | u32 *opc, u32 cause, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1645 | struct kvm_run *run, |
| 1646 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1647 | { |
| 1648 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1649 | enum emulation_result er = EMULATE_DONE; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 1650 | u32 cache, op_inst, op, base; |
| 1651 | s16 offset; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1652 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 1653 | unsigned long va; |
| 1654 | unsigned long curr_pc; |
| 1655 | |
| 1656 | /* |
| 1657 | * Update PC and hold onto current PC in case there is |
| 1658 | * an error and we want to rollback the PC |
| 1659 | */ |
| 1660 | curr_pc = vcpu->arch.pc; |
| 1661 | er = update_pc(vcpu, cause); |
| 1662 | if (er == EMULATE_FAIL) |
| 1663 | return er; |
| 1664 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1665 | base = inst.i_format.rs; |
| 1666 | op_inst = inst.i_format.rt; |
James Hogan | 5cc4aaf | 2016-07-04 19:35:13 +0100 | [diff] [blame] | 1667 | if (cpu_has_mips_r6) |
| 1668 | offset = inst.spec3_format.simmediate; |
| 1669 | else |
| 1670 | offset = inst.i_format.simmediate; |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 1671 | cache = op_inst & CacheOp_Cache; |
| 1672 | op = op_inst & CacheOp_Op; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1673 | |
| 1674 | va = arch->gprs[base] + offset; |
| 1675 | |
| 1676 | kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n", |
| 1677 | cache, op, base, arch->gprs[base], offset); |
| 1678 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1679 | /* |
| 1680 | * Treat INDEX_INV as a nop, basically issued by Linux on startup to |
| 1681 | * invalidate the caches entirely by stepping through all the |
| 1682 | * ways/indexes |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1683 | */ |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 1684 | if (op == Index_Writeback_Inv) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1685 | kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n", |
| 1686 | vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base, |
| 1687 | arch->gprs[base], offset); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1688 | |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 1689 | if (cache == Cache_D) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1690 | r4k_blast_dcache(); |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 1691 | else if (cache == Cache_I) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1692 | r4k_blast_icache(); |
| 1693 | else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1694 | kvm_err("%s: unsupported CACHE INDEX operation\n", |
| 1695 | __func__); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1696 | return EMULATE_FAIL; |
| 1697 | } |
| 1698 | |
| 1699 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
| 1700 | kvm_mips_trans_cache_index(inst, opc, vcpu); |
| 1701 | #endif |
| 1702 | goto done; |
| 1703 | } |
| 1704 | |
| 1705 | preempt_disable(); |
| 1706 | if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) { |
James Hogan | 9b731bc | 2016-08-11 11:58:15 +0100 | [diff] [blame] | 1707 | if (kvm_mips_host_tlb_lookup(vcpu, va) < 0 && |
| 1708 | kvm_mips_handle_kseg0_tlb_fault(va, vcpu)) { |
| 1709 | kvm_err("%s: handling mapped kseg0 tlb fault for %lx, vcpu: %p, ASID: %#lx\n", |
| 1710 | __func__, va, vcpu, read_c0_entryhi()); |
| 1711 | er = EMULATE_FAIL; |
| 1712 | preempt_enable(); |
| 1713 | goto done; |
| 1714 | } |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1715 | } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) || |
| 1716 | KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) { |
| 1717 | int index; |
| 1718 | |
| 1719 | /* If an entry already exists then skip */ |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1720 | if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1721 | goto skip_fault; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1722 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1723 | /* |
| 1724 | * If address not in the guest TLB, then give the guest a fault, |
| 1725 | * the resulting handler will do the right thing |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1726 | */ |
| 1727 | index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) | |
David Daney | 48c4ac9 | 2013-05-13 13:56:44 -0700 | [diff] [blame] | 1728 | (kvm_read_c0_guest_entryhi |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 1729 | (cop0) & KVM_ENTRYHI_ASID)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1730 | |
| 1731 | if (index < 0) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1732 | vcpu->arch.host_cp0_badvaddr = va; |
James Hogan | 6df82a7 | 2016-06-09 10:50:46 +0100 | [diff] [blame] | 1733 | vcpu->arch.pc = curr_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1734 | er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run, |
| 1735 | vcpu); |
| 1736 | preempt_enable(); |
| 1737 | goto dont_update_pc; |
| 1738 | } else { |
| 1739 | struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index]; |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1740 | /* |
| 1741 | * Check if the entry is valid, if not then setup a TLB |
| 1742 | * invalid exception to the guest |
| 1743 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1744 | if (!TLB_IS_VALID(*tlb, va)) { |
James Hogan | 6df82a7 | 2016-06-09 10:50:46 +0100 | [diff] [blame] | 1745 | vcpu->arch.host_cp0_badvaddr = va; |
| 1746 | vcpu->arch.pc = curr_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1747 | er = kvm_mips_emulate_tlbinv_ld(cause, NULL, |
| 1748 | run, vcpu); |
| 1749 | preempt_enable(); |
| 1750 | goto dont_update_pc; |
James Hogan | 9b731bc | 2016-08-11 11:58:15 +0100 | [diff] [blame] | 1751 | } |
| 1752 | /* |
| 1753 | * We fault an entry from the guest tlb to the |
| 1754 | * shadow host TLB |
| 1755 | */ |
| 1756 | if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb)) { |
| 1757 | kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n", |
| 1758 | __func__, va, index, vcpu, |
| 1759 | read_c0_entryhi()); |
| 1760 | er = EMULATE_FAIL; |
| 1761 | preempt_enable(); |
| 1762 | goto done; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1766 | kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n", |
| 1767 | cache, op, base, arch->gprs[base], offset); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1768 | er = EMULATE_FAIL; |
| 1769 | preempt_enable(); |
James Hogan | cc81e94 | 2016-06-09 10:50:45 +0100 | [diff] [blame] | 1770 | goto done; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1771 | |
| 1772 | } |
| 1773 | |
| 1774 | skip_fault: |
| 1775 | /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */ |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 1776 | if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1777 | flush_dcache_line(va); |
| 1778 | |
| 1779 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1780 | /* |
| 1781 | * Replace the CACHE instruction, with a SYNCI, not the same, |
| 1782 | * but avoids a trap |
| 1783 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1784 | kvm_mips_trans_cache_va(inst, opc, vcpu); |
| 1785 | #endif |
James Hogan | f4956f6 | 2015-12-16 23:49:37 +0000 | [diff] [blame] | 1786 | } else if (op_inst == Hit_Invalidate_I) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1787 | flush_dcache_line(va); |
| 1788 | flush_icache_line(va); |
| 1789 | |
| 1790 | #ifdef CONFIG_KVM_MIPS_DYN_TRANS |
| 1791 | /* Replace the CACHE instruction, with a SYNCI */ |
| 1792 | kvm_mips_trans_cache_va(inst, opc, vcpu); |
| 1793 | #endif |
| 1794 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1795 | kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n", |
| 1796 | cache, op, base, arch->gprs[base], offset); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1797 | er = EMULATE_FAIL; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | preempt_enable(); |
James Hogan | cc81e94 | 2016-06-09 10:50:45 +0100 | [diff] [blame] | 1801 | done: |
| 1802 | /* Rollback PC only if emulation was unsuccessful */ |
| 1803 | if (er == EMULATE_FAIL) |
| 1804 | vcpu->arch.pc = curr_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1805 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1806 | dont_update_pc: |
James Hogan | cc81e94 | 2016-06-09 10:50:45 +0100 | [diff] [blame] | 1807 | /* |
| 1808 | * This is for exceptions whose emulation updates the PC, so do not |
| 1809 | * overwrite the PC under any circumstances |
| 1810 | */ |
| 1811 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1812 | return er; |
| 1813 | } |
| 1814 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 1815 | enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1816 | struct kvm_run *run, |
| 1817 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1818 | { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1819 | union mips_instruction inst; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1820 | enum emulation_result er = EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1821 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1822 | /* Fetch the instruction. */ |
| 1823 | if (cause & CAUSEF_BD) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1824 | opc += 1; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1825 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1826 | inst.word = kvm_get_inst(opc, vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1827 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1828 | switch (inst.r_format.opcode) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1829 | case cop0_op: |
| 1830 | er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu); |
| 1831 | break; |
| 1832 | case sb_op: |
| 1833 | case sh_op: |
| 1834 | case sw_op: |
| 1835 | er = kvm_mips_emulate_store(inst, cause, run, vcpu); |
| 1836 | break; |
| 1837 | case lb_op: |
| 1838 | case lbu_op: |
| 1839 | case lhu_op: |
| 1840 | case lh_op: |
| 1841 | case lw_op: |
| 1842 | er = kvm_mips_emulate_load(inst, cause, run, vcpu); |
| 1843 | break; |
| 1844 | |
James Hogan | 5cc4aaf | 2016-07-04 19:35:13 +0100 | [diff] [blame] | 1845 | #ifndef CONFIG_CPU_MIPSR6 |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1846 | case cache_op: |
| 1847 | ++vcpu->stat.cache_exits; |
James Hogan | 1e09e86 | 2016-06-14 09:40:12 +0100 | [diff] [blame] | 1848 | trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1849 | er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu); |
| 1850 | break; |
James Hogan | 5cc4aaf | 2016-07-04 19:35:13 +0100 | [diff] [blame] | 1851 | #else |
| 1852 | case spec3_op: |
| 1853 | switch (inst.spec3_format.func) { |
| 1854 | case cache6_op: |
| 1855 | ++vcpu->stat.cache_exits; |
| 1856 | trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE); |
| 1857 | er = kvm_mips_emulate_cache(inst, opc, cause, run, |
| 1858 | vcpu); |
| 1859 | break; |
| 1860 | default: |
| 1861 | goto unknown; |
| 1862 | }; |
| 1863 | break; |
| 1864 | unknown: |
| 1865 | #endif |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1866 | |
| 1867 | default: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1868 | kvm_err("Instruction emulation not supported (%p/%#x)\n", opc, |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 1869 | inst.word); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1870 | kvm_arch_vcpu_dump_regs(vcpu); |
| 1871 | er = EMULATE_FAIL; |
| 1872 | break; |
| 1873 | } |
| 1874 | |
| 1875 | return er; |
| 1876 | } |
| 1877 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 1878 | enum emulation_result kvm_mips_emulate_syscall(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 1879 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1880 | struct kvm_run *run, |
| 1881 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1882 | { |
| 1883 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 1884 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 1885 | enum emulation_result er = EMULATE_DONE; |
| 1886 | |
| 1887 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 1888 | /* save old pc */ |
| 1889 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 1890 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 1891 | |
| 1892 | if (cause & CAUSEF_BD) |
| 1893 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 1894 | else |
| 1895 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 1896 | |
| 1897 | kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc); |
| 1898 | |
| 1899 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1900 | (EXCCODE_SYS << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1901 | |
| 1902 | /* Set PC to the exception entry point */ |
| 1903 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 1904 | |
| 1905 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 1906 | kvm_err("Trying to deliver SYSCALL when EXL is already set\n"); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1907 | er = EMULATE_FAIL; |
| 1908 | } |
| 1909 | |
| 1910 | return er; |
| 1911 | } |
| 1912 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 1913 | enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 1914 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1915 | struct kvm_run *run, |
| 1916 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1917 | { |
| 1918 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 1919 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1920 | unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 1921 | (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1922 | |
| 1923 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 1924 | /* save old pc */ |
| 1925 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 1926 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 1927 | |
| 1928 | if (cause & CAUSEF_BD) |
| 1929 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 1930 | else |
| 1931 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 1932 | |
| 1933 | kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n", |
| 1934 | arch->pc); |
| 1935 | |
| 1936 | /* set pc to the exception entry point */ |
| 1937 | arch->pc = KVM_GUEST_KSEG0 + 0x0; |
| 1938 | |
| 1939 | } else { |
| 1940 | kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n", |
| 1941 | arch->pc); |
| 1942 | |
| 1943 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 1944 | } |
| 1945 | |
| 1946 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1947 | (EXCCODE_TLBL << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1948 | |
| 1949 | /* setup badvaddr, context and entryhi registers for the guest */ |
| 1950 | kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr); |
| 1951 | /* XXXKYMA: is the context register used by linux??? */ |
| 1952 | kvm_write_c0_guest_entryhi(cop0, entryhi); |
| 1953 | /* Blow away the shadow host TLBs */ |
| 1954 | kvm_mips_flush_host_tlb(1); |
| 1955 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 1956 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1957 | } |
| 1958 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 1959 | enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 1960 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 1961 | struct kvm_run *run, |
| 1962 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1963 | { |
| 1964 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 1965 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1966 | unsigned long entryhi = |
| 1967 | (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 1968 | (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1969 | |
| 1970 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 1971 | /* save old pc */ |
| 1972 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 1973 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 1974 | |
| 1975 | if (cause & CAUSEF_BD) |
| 1976 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 1977 | else |
| 1978 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 1979 | |
| 1980 | kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n", |
| 1981 | arch->pc); |
| 1982 | |
| 1983 | /* set pc to the exception entry point */ |
| 1984 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 1985 | |
| 1986 | } else { |
| 1987 | kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n", |
| 1988 | arch->pc); |
| 1989 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 1990 | } |
| 1991 | |
| 1992 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 1993 | (EXCCODE_TLBL << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 1994 | |
| 1995 | /* setup badvaddr, context and entryhi registers for the guest */ |
| 1996 | kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr); |
| 1997 | /* XXXKYMA: is the context register used by linux??? */ |
| 1998 | kvm_write_c0_guest_entryhi(cop0, entryhi); |
| 1999 | /* Blow away the shadow host TLBs */ |
| 2000 | kvm_mips_flush_host_tlb(1); |
| 2001 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 2002 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2005 | enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2006 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2007 | struct kvm_run *run, |
| 2008 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2009 | { |
| 2010 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2011 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2012 | unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 2013 | (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2014 | |
| 2015 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2016 | /* save old pc */ |
| 2017 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2018 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2019 | |
| 2020 | if (cause & CAUSEF_BD) |
| 2021 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2022 | else |
| 2023 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2024 | |
| 2025 | kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n", |
| 2026 | arch->pc); |
| 2027 | |
| 2028 | /* Set PC to the exception entry point */ |
| 2029 | arch->pc = KVM_GUEST_KSEG0 + 0x0; |
| 2030 | } else { |
| 2031 | kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n", |
| 2032 | arch->pc); |
| 2033 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2034 | } |
| 2035 | |
| 2036 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2037 | (EXCCODE_TLBS << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2038 | |
| 2039 | /* setup badvaddr, context and entryhi registers for the guest */ |
| 2040 | kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr); |
| 2041 | /* XXXKYMA: is the context register used by linux??? */ |
| 2042 | kvm_write_c0_guest_entryhi(cop0, entryhi); |
| 2043 | /* Blow away the shadow host TLBs */ |
| 2044 | kvm_mips_flush_host_tlb(1); |
| 2045 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 2046 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2047 | } |
| 2048 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2049 | enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2050 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2051 | struct kvm_run *run, |
| 2052 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2053 | { |
| 2054 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2055 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2056 | unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 2057 | (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2058 | |
| 2059 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2060 | /* save old pc */ |
| 2061 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2062 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2063 | |
| 2064 | if (cause & CAUSEF_BD) |
| 2065 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2066 | else |
| 2067 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2068 | |
| 2069 | kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n", |
| 2070 | arch->pc); |
| 2071 | |
| 2072 | /* Set PC to the exception entry point */ |
| 2073 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2074 | } else { |
| 2075 | kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n", |
| 2076 | arch->pc); |
| 2077 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2078 | } |
| 2079 | |
| 2080 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2081 | (EXCCODE_TLBS << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2082 | |
| 2083 | /* setup badvaddr, context and entryhi registers for the guest */ |
| 2084 | kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr); |
| 2085 | /* XXXKYMA: is the context register used by linux??? */ |
| 2086 | kvm_write_c0_guest_entryhi(cop0, entryhi); |
| 2087 | /* Blow away the shadow host TLBs */ |
| 2088 | kvm_mips_flush_host_tlb(1); |
| 2089 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 2090 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /* TLBMOD: store into address matching TLB with Dirty bit off */ |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2094 | enum emulation_result kvm_mips_handle_tlbmod(u32 cause, u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2095 | struct kvm_run *run, |
| 2096 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2097 | { |
| 2098 | enum emulation_result er = EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2099 | #ifdef DEBUG |
James Hogan | 3d65483 | 2014-05-29 10:16:41 +0100 | [diff] [blame] | 2100 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2101 | unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 2102 | (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID); |
James Hogan | 57e3869 | 2016-10-08 00:15:52 +0100 | [diff] [blame^] | 2103 | bool kernel = KVM_GUEST_KERNEL_MODE(vcpu); |
James Hogan | 3d65483 | 2014-05-29 10:16:41 +0100 | [diff] [blame] | 2104 | int index; |
| 2105 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2106 | /* If address not in the guest TLB, then we are in trouble */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2107 | index = kvm_mips_guest_tlb_lookup(vcpu, entryhi); |
| 2108 | if (index < 0) { |
| 2109 | /* XXXKYMA Invalidate and retry */ |
James Hogan | 57e3869 | 2016-10-08 00:15:52 +0100 | [diff] [blame^] | 2110 | kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr, |
| 2111 | !kernel, kernel); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2112 | kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n", |
| 2113 | __func__, entryhi); |
| 2114 | kvm_mips_dump_guest_tlbs(vcpu); |
| 2115 | kvm_mips_dump_host_tlbs(); |
| 2116 | return EMULATE_FAIL; |
| 2117 | } |
| 2118 | #endif |
| 2119 | |
| 2120 | er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu); |
| 2121 | return er; |
| 2122 | } |
| 2123 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2124 | enum emulation_result kvm_mips_emulate_tlbmod(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2125 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2126 | struct kvm_run *run, |
| 2127 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2128 | { |
| 2129 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2130 | unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 2131 | (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2132 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2133 | |
| 2134 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2135 | /* save old pc */ |
| 2136 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2137 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2138 | |
| 2139 | if (cause & CAUSEF_BD) |
| 2140 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2141 | else |
| 2142 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2143 | |
| 2144 | kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n", |
| 2145 | arch->pc); |
| 2146 | |
| 2147 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2148 | } else { |
| 2149 | kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n", |
| 2150 | arch->pc); |
| 2151 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2152 | } |
| 2153 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2154 | kvm_change_c0_guest_cause(cop0, (0xff), |
| 2155 | (EXCCODE_MOD << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2156 | |
| 2157 | /* setup badvaddr, context and entryhi registers for the guest */ |
| 2158 | kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr); |
| 2159 | /* XXXKYMA: is the context register used by linux??? */ |
| 2160 | kvm_write_c0_guest_entryhi(cop0, entryhi); |
| 2161 | /* Blow away the shadow host TLBs */ |
| 2162 | kvm_mips_flush_host_tlb(1); |
| 2163 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 2164 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2165 | } |
| 2166 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2167 | enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2168 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2169 | struct kvm_run *run, |
| 2170 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2171 | { |
| 2172 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2173 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2174 | |
| 2175 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2176 | /* save old pc */ |
| 2177 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2178 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2179 | |
| 2180 | if (cause & CAUSEF_BD) |
| 2181 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2182 | else |
| 2183 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2184 | |
| 2185 | } |
| 2186 | |
| 2187 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2188 | |
| 2189 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2190 | (EXCCODE_CPU << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2191 | kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE)); |
| 2192 | |
Deng-Cheng Zhu | d98403a | 2014-06-26 12:11:36 -0700 | [diff] [blame] | 2193 | return EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2194 | } |
| 2195 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2196 | enum emulation_result kvm_mips_emulate_ri_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2197 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2198 | struct kvm_run *run, |
| 2199 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2200 | { |
| 2201 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2202 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2203 | enum emulation_result er = EMULATE_DONE; |
| 2204 | |
| 2205 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2206 | /* save old pc */ |
| 2207 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2208 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2209 | |
| 2210 | if (cause & CAUSEF_BD) |
| 2211 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2212 | else |
| 2213 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2214 | |
| 2215 | kvm_debug("Delivering RI @ pc %#lx\n", arch->pc); |
| 2216 | |
| 2217 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2218 | (EXCCODE_RI << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2219 | |
| 2220 | /* Set PC to the exception entry point */ |
| 2221 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2222 | |
| 2223 | } else { |
| 2224 | kvm_err("Trying to deliver RI when EXL is already set\n"); |
| 2225 | er = EMULATE_FAIL; |
| 2226 | } |
| 2227 | |
| 2228 | return er; |
| 2229 | } |
| 2230 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2231 | enum emulation_result kvm_mips_emulate_bp_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2232 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2233 | struct kvm_run *run, |
| 2234 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2235 | { |
| 2236 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2237 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2238 | enum emulation_result er = EMULATE_DONE; |
| 2239 | |
| 2240 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2241 | /* save old pc */ |
| 2242 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2243 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2244 | |
| 2245 | if (cause & CAUSEF_BD) |
| 2246 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2247 | else |
| 2248 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2249 | |
| 2250 | kvm_debug("Delivering BP @ pc %#lx\n", arch->pc); |
| 2251 | |
| 2252 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2253 | (EXCCODE_BP << CAUSEB_EXCCODE)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2254 | |
| 2255 | /* Set PC to the exception entry point */ |
| 2256 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2257 | |
| 2258 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2259 | kvm_err("Trying to deliver BP when EXL is already set\n"); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2260 | er = EMULATE_FAIL; |
| 2261 | } |
| 2262 | |
| 2263 | return er; |
| 2264 | } |
| 2265 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2266 | enum emulation_result kvm_mips_emulate_trap_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2267 | u32 *opc, |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 2268 | struct kvm_run *run, |
| 2269 | struct kvm_vcpu *vcpu) |
| 2270 | { |
| 2271 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2272 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2273 | enum emulation_result er = EMULATE_DONE; |
| 2274 | |
| 2275 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2276 | /* save old pc */ |
| 2277 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2278 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2279 | |
| 2280 | if (cause & CAUSEF_BD) |
| 2281 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2282 | else |
| 2283 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2284 | |
| 2285 | kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc); |
| 2286 | |
| 2287 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2288 | (EXCCODE_TR << CAUSEB_EXCCODE)); |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 2289 | |
| 2290 | /* Set PC to the exception entry point */ |
| 2291 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2292 | |
| 2293 | } else { |
| 2294 | kvm_err("Trying to deliver TRAP when EXL is already set\n"); |
| 2295 | er = EMULATE_FAIL; |
| 2296 | } |
| 2297 | |
| 2298 | return er; |
| 2299 | } |
| 2300 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2301 | enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2302 | u32 *opc, |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 2303 | struct kvm_run *run, |
| 2304 | struct kvm_vcpu *vcpu) |
| 2305 | { |
| 2306 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2307 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2308 | enum emulation_result er = EMULATE_DONE; |
| 2309 | |
| 2310 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2311 | /* save old pc */ |
| 2312 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2313 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2314 | |
| 2315 | if (cause & CAUSEF_BD) |
| 2316 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2317 | else |
| 2318 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2319 | |
| 2320 | kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc); |
| 2321 | |
| 2322 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2323 | (EXCCODE_MSAFPE << CAUSEB_EXCCODE)); |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 2324 | |
| 2325 | /* Set PC to the exception entry point */ |
| 2326 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2327 | |
| 2328 | } else { |
| 2329 | kvm_err("Trying to deliver MSAFPE when EXL is already set\n"); |
| 2330 | er = EMULATE_FAIL; |
| 2331 | } |
| 2332 | |
| 2333 | return er; |
| 2334 | } |
| 2335 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2336 | enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2337 | u32 *opc, |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 2338 | struct kvm_run *run, |
| 2339 | struct kvm_vcpu *vcpu) |
| 2340 | { |
| 2341 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2342 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2343 | enum emulation_result er = EMULATE_DONE; |
| 2344 | |
| 2345 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2346 | /* save old pc */ |
| 2347 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2348 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2349 | |
| 2350 | if (cause & CAUSEF_BD) |
| 2351 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2352 | else |
| 2353 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2354 | |
| 2355 | kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc); |
| 2356 | |
| 2357 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2358 | (EXCCODE_FPE << CAUSEB_EXCCODE)); |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 2359 | |
| 2360 | /* Set PC to the exception entry point */ |
| 2361 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2362 | |
| 2363 | } else { |
| 2364 | kvm_err("Trying to deliver FPE when EXL is already set\n"); |
| 2365 | er = EMULATE_FAIL; |
| 2366 | } |
| 2367 | |
| 2368 | return er; |
| 2369 | } |
| 2370 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2371 | enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2372 | u32 *opc, |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 2373 | struct kvm_run *run, |
| 2374 | struct kvm_vcpu *vcpu) |
| 2375 | { |
| 2376 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2377 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2378 | enum emulation_result er = EMULATE_DONE; |
| 2379 | |
| 2380 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2381 | /* save old pc */ |
| 2382 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2383 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2384 | |
| 2385 | if (cause & CAUSEF_BD) |
| 2386 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2387 | else |
| 2388 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2389 | |
| 2390 | kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc); |
| 2391 | |
| 2392 | kvm_change_c0_guest_cause(cop0, (0xff), |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2393 | (EXCCODE_MSADIS << CAUSEB_EXCCODE)); |
James Hogan | c2537ed | 2015-02-06 10:56:27 +0000 | [diff] [blame] | 2394 | |
| 2395 | /* Set PC to the exception entry point */ |
| 2396 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2397 | |
| 2398 | } else { |
| 2399 | kvm_err("Trying to deliver MSADIS when EXL is already set\n"); |
| 2400 | er = EMULATE_FAIL; |
| 2401 | } |
| 2402 | |
| 2403 | return er; |
| 2404 | } |
| 2405 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2406 | enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2407 | struct kvm_run *run, |
| 2408 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2409 | { |
| 2410 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2411 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2412 | enum emulation_result er = EMULATE_DONE; |
| 2413 | unsigned long curr_pc; |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 2414 | union mips_instruction inst; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2415 | |
| 2416 | /* |
| 2417 | * Update PC and hold onto current PC in case there is |
| 2418 | * an error and we want to rollback the PC |
| 2419 | */ |
| 2420 | curr_pc = vcpu->arch.pc; |
| 2421 | er = update_pc(vcpu, cause); |
| 2422 | if (er == EMULATE_FAIL) |
| 2423 | return er; |
| 2424 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2425 | /* Fetch the instruction. */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2426 | if (cause & CAUSEF_BD) |
| 2427 | opc += 1; |
| 2428 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 2429 | inst.word = kvm_get_inst(opc, vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2430 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 2431 | if (inst.word == KVM_INVALID_INST) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2432 | kvm_err("%s: Cannot get inst @ %p\n", __func__, opc); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2433 | return EMULATE_FAIL; |
| 2434 | } |
| 2435 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 2436 | if (inst.r_format.opcode == spec3_op && |
James Hogan | 8eeab81 | 2016-07-04 19:35:14 +0100 | [diff] [blame] | 2437 | inst.r_format.func == rdhwr_op && |
| 2438 | inst.r_format.rs == 0 && |
| 2439 | (inst.r_format.re >> 3) == 0) { |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2440 | int usermode = !KVM_GUEST_KERNEL_MODE(vcpu); |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 2441 | int rd = inst.r_format.rd; |
| 2442 | int rt = inst.r_format.rt; |
| 2443 | int sel = inst.r_format.re & 0x7; |
James Hogan | 6398da1 | 2016-06-14 09:40:15 +0100 | [diff] [blame] | 2444 | |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2445 | /* If usermode, check RDHWR rd is allowed by guest HWREna */ |
| 2446 | if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) { |
| 2447 | kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n", |
| 2448 | rd, opc); |
| 2449 | goto emulate_ri; |
| 2450 | } |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2451 | switch (rd) { |
James Hogan | aff565a | 2016-06-15 19:29:52 +0100 | [diff] [blame] | 2452 | case MIPS_HWR_CPUNUM: /* CPU number */ |
James Hogan | cf1fb0f | 2016-06-15 19:29:55 +0100 | [diff] [blame] | 2453 | arch->gprs[rt] = vcpu->vcpu_id; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2454 | break; |
James Hogan | aff565a | 2016-06-15 19:29:52 +0100 | [diff] [blame] | 2455 | case MIPS_HWR_SYNCISTEP: /* SYNCI length */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2456 | arch->gprs[rt] = min(current_cpu_data.dcache.linesz, |
| 2457 | current_cpu_data.icache.linesz); |
| 2458 | break; |
James Hogan | aff565a | 2016-06-15 19:29:52 +0100 | [diff] [blame] | 2459 | case MIPS_HWR_CC: /* Read count register */ |
James Hogan | 172e02d | 2016-07-08 11:53:28 +0100 | [diff] [blame] | 2460 | arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2461 | break; |
James Hogan | aff565a | 2016-06-15 19:29:52 +0100 | [diff] [blame] | 2462 | case MIPS_HWR_CCRES: /* Count register resolution */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2463 | switch (current_cpu_data.cputype) { |
| 2464 | case CPU_20KC: |
| 2465 | case CPU_25KF: |
| 2466 | arch->gprs[rt] = 1; |
| 2467 | break; |
| 2468 | default: |
| 2469 | arch->gprs[rt] = 2; |
| 2470 | } |
| 2471 | break; |
James Hogan | aff565a | 2016-06-15 19:29:52 +0100 | [diff] [blame] | 2472 | case MIPS_HWR_ULR: /* Read UserLocal register */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2473 | arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2474 | break; |
| 2475 | |
| 2476 | default: |
James Hogan | 1550567 | 2014-03-14 13:06:07 +0000 | [diff] [blame] | 2477 | kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc); |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2478 | goto emulate_ri; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2479 | } |
James Hogan | 6398da1 | 2016-06-14 09:40:15 +0100 | [diff] [blame] | 2480 | |
| 2481 | trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel), |
| 2482 | vcpu->arch.gprs[rt]); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2483 | } else { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 2484 | kvm_debug("Emulate RI not supported @ %p: %#x\n", |
| 2485 | opc, inst.word); |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2486 | goto emulate_ri; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2489 | return EMULATE_DONE; |
| 2490 | |
| 2491 | emulate_ri: |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2492 | /* |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2493 | * Rollback PC (if in branch delay slot then the PC already points to |
| 2494 | * branch target), and pass the RI exception to the guest OS. |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2495 | */ |
James Hogan | 26f4f3b | 2014-03-14 13:06:09 +0000 | [diff] [blame] | 2496 | vcpu->arch.pc = curr_pc; |
| 2497 | return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2500 | enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu, |
| 2501 | struct kvm_run *run) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2502 | { |
| 2503 | unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr]; |
| 2504 | enum emulation_result er = EMULATE_DONE; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2505 | |
| 2506 | if (run->mmio.len > sizeof(*gpr)) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2507 | kvm_err("Bad MMIO length: %d", run->mmio.len); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2508 | er = EMULATE_FAIL; |
| 2509 | goto done; |
| 2510 | } |
| 2511 | |
James Hogan | e1e575f6 | 2016-10-25 16:11:12 +0100 | [diff] [blame] | 2512 | /* Restore saved resume PC */ |
| 2513 | vcpu->arch.pc = vcpu->arch.io_pc; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2514 | |
| 2515 | switch (run->mmio.len) { |
| 2516 | case 4: |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2517 | *gpr = *(s32 *) run->mmio.data; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2518 | break; |
| 2519 | |
| 2520 | case 2: |
| 2521 | if (vcpu->mmio_needed == 2) |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2522 | *gpr = *(s16 *) run->mmio.data; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2523 | else |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2524 | *gpr = *(u16 *)run->mmio.data; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2525 | |
| 2526 | break; |
| 2527 | case 1: |
| 2528 | if (vcpu->mmio_needed == 2) |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2529 | *gpr = *(s8 *) run->mmio.data; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2530 | else |
| 2531 | *gpr = *(u8 *) run->mmio.data; |
| 2532 | break; |
| 2533 | } |
| 2534 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2535 | done: |
| 2536 | return er; |
| 2537 | } |
| 2538 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2539 | static enum emulation_result kvm_mips_emulate_exc(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2540 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2541 | struct kvm_run *run, |
| 2542 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2543 | { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2544 | u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2545 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 2546 | struct kvm_vcpu_arch *arch = &vcpu->arch; |
| 2547 | enum emulation_result er = EMULATE_DONE; |
| 2548 | |
| 2549 | if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) { |
| 2550 | /* save old pc */ |
| 2551 | kvm_write_c0_guest_epc(cop0, arch->pc); |
| 2552 | kvm_set_c0_guest_status(cop0, ST0_EXL); |
| 2553 | |
| 2554 | if (cause & CAUSEF_BD) |
| 2555 | kvm_set_c0_guest_cause(cop0, CAUSEF_BD); |
| 2556 | else |
| 2557 | kvm_clear_c0_guest_cause(cop0, CAUSEF_BD); |
| 2558 | |
| 2559 | kvm_change_c0_guest_cause(cop0, (0xff), |
| 2560 | (exccode << CAUSEB_EXCCODE)); |
| 2561 | |
| 2562 | /* Set PC to the exception entry point */ |
| 2563 | arch->pc = KVM_GUEST_KSEG0 + 0x180; |
| 2564 | kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr); |
| 2565 | |
| 2566 | kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n", |
| 2567 | exccode, kvm_read_c0_guest_epc(cop0), |
| 2568 | kvm_read_c0_guest_badvaddr(cop0)); |
| 2569 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2570 | kvm_err("Trying to deliver EXC when EXL is already set\n"); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2571 | er = EMULATE_FAIL; |
| 2572 | } |
| 2573 | |
| 2574 | return er; |
| 2575 | } |
| 2576 | |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2577 | enum emulation_result kvm_mips_check_privilege(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2578 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2579 | struct kvm_run *run, |
| 2580 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2581 | { |
| 2582 | enum emulation_result er = EMULATE_DONE; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2583 | u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2584 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 2585 | |
| 2586 | int usermode = !KVM_GUEST_KERNEL_MODE(vcpu); |
| 2587 | |
| 2588 | if (usermode) { |
| 2589 | switch (exccode) { |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2590 | case EXCCODE_INT: |
| 2591 | case EXCCODE_SYS: |
| 2592 | case EXCCODE_BP: |
| 2593 | case EXCCODE_RI: |
| 2594 | case EXCCODE_TR: |
| 2595 | case EXCCODE_MSAFPE: |
| 2596 | case EXCCODE_FPE: |
| 2597 | case EXCCODE_MSADIS: |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2598 | break; |
| 2599 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2600 | case EXCCODE_CPU: |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2601 | if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0) |
| 2602 | er = EMULATE_PRIV_FAIL; |
| 2603 | break; |
| 2604 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2605 | case EXCCODE_MOD: |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2606 | break; |
| 2607 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2608 | case EXCCODE_TLBL: |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2609 | /* |
| 2610 | * We we are accessing Guest kernel space, then send an |
| 2611 | * address error exception to the guest |
| 2612 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2613 | if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2614 | kvm_debug("%s: LD MISS @ %#lx\n", __func__, |
| 2615 | badvaddr); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2616 | cause &= ~0xff; |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2617 | cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2618 | er = EMULATE_PRIV_FAIL; |
| 2619 | } |
| 2620 | break; |
| 2621 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2622 | case EXCCODE_TLBS: |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2623 | /* |
| 2624 | * We we are accessing Guest kernel space, then send an |
| 2625 | * address error exception to the guest |
| 2626 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2627 | if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2628 | kvm_debug("%s: ST MISS @ %#lx\n", __func__, |
| 2629 | badvaddr); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2630 | cause &= ~0xff; |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2631 | cause |= (EXCCODE_ADES << CAUSEB_EXCCODE); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2632 | er = EMULATE_PRIV_FAIL; |
| 2633 | } |
| 2634 | break; |
| 2635 | |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2636 | case EXCCODE_ADES: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2637 | kvm_debug("%s: address error ST @ %#lx\n", __func__, |
| 2638 | badvaddr); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2639 | if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) { |
| 2640 | cause &= ~0xff; |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2641 | cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2642 | } |
| 2643 | er = EMULATE_PRIV_FAIL; |
| 2644 | break; |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2645 | case EXCCODE_ADEL: |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2646 | kvm_debug("%s: address error LD @ %#lx\n", __func__, |
| 2647 | badvaddr); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2648 | if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) { |
| 2649 | cause &= ~0xff; |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2650 | cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2651 | } |
| 2652 | er = EMULATE_PRIV_FAIL; |
| 2653 | break; |
| 2654 | default: |
| 2655 | er = EMULATE_PRIV_FAIL; |
| 2656 | break; |
| 2657 | } |
| 2658 | } |
| 2659 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2660 | if (er == EMULATE_PRIV_FAIL) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2661 | kvm_mips_emulate_exc(cause, opc, run, vcpu); |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2662 | |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2663 | return er; |
| 2664 | } |
| 2665 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2666 | /* |
| 2667 | * User Address (UA) fault, this could happen if |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2668 | * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this |
| 2669 | * case we pass on the fault to the guest kernel and let it handle it. |
| 2670 | * (2) TLB entry is present in the Guest TLB but not in the shadow, in this |
| 2671 | * case we inject the TLB from the Guest TLB into the shadow host TLB |
| 2672 | */ |
James Hogan | 31cf749 | 2016-06-09 14:19:09 +0100 | [diff] [blame] | 2673 | enum emulation_result kvm_mips_handle_tlbmiss(u32 cause, |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 2674 | u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2675 | struct kvm_run *run, |
| 2676 | struct kvm_vcpu *vcpu) |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2677 | { |
| 2678 | enum emulation_result er = EMULATE_DONE; |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 2679 | u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2680 | unsigned long va = vcpu->arch.host_cp0_badvaddr; |
| 2681 | int index; |
| 2682 | |
James Hogan | e4e94c0 | 2016-06-09 14:19:05 +0100 | [diff] [blame] | 2683 | kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n", |
| 2684 | vcpu->arch.host_cp0_badvaddr); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2685 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2686 | /* |
| 2687 | * KVM would not have got the exception if this entry was valid in the |
| 2688 | * shadow host TLB. Check the Guest TLB, if the entry is not there then |
| 2689 | * send the guest an exception. The guest exc handler should then inject |
| 2690 | * an entry into the guest TLB. |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2691 | */ |
| 2692 | index = kvm_mips_guest_tlb_lookup(vcpu, |
James Hogan | caa1faa | 2015-12-16 23:49:26 +0000 | [diff] [blame] | 2693 | (va & VPN2_MASK) | |
Paul Burton | ca64c2b | 2016-05-06 14:36:20 +0100 | [diff] [blame] | 2694 | (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) & |
| 2695 | KVM_ENTRYHI_ASID)); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2696 | if (index < 0) { |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2697 | if (exccode == EXCCODE_TLBL) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2698 | er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu); |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2699 | } else if (exccode == EXCCODE_TLBS) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2700 | er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu); |
| 2701 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2702 | kvm_err("%s: invalid exc code: %d\n", __func__, |
| 2703 | exccode); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2704 | er = EMULATE_FAIL; |
| 2705 | } |
| 2706 | } else { |
| 2707 | struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index]; |
| 2708 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2709 | /* |
| 2710 | * Check if the entry is valid, if not then setup a TLB invalid |
| 2711 | * exception to the guest |
| 2712 | */ |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2713 | if (!TLB_IS_VALID(*tlb, va)) { |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2714 | if (exccode == EXCCODE_TLBL) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2715 | er = kvm_mips_emulate_tlbinv_ld(cause, opc, run, |
| 2716 | vcpu); |
James Hogan | 16d100db | 2015-12-16 23:49:33 +0000 | [diff] [blame] | 2717 | } else if (exccode == EXCCODE_TLBS) { |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2718 | er = kvm_mips_emulate_tlbinv_st(cause, opc, run, |
| 2719 | vcpu); |
| 2720 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 2721 | kvm_err("%s: invalid exc code: %d\n", __func__, |
| 2722 | exccode); |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2723 | er = EMULATE_FAIL; |
| 2724 | } |
| 2725 | } else { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2726 | kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n", |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 2727 | tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]); |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2728 | /* |
| 2729 | * OK we have a Guest TLB entry, now inject it into the |
| 2730 | * shadow host TLB |
| 2731 | */ |
James Hogan | 9b731bc | 2016-08-11 11:58:15 +0100 | [diff] [blame] | 2732 | if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb)) { |
| 2733 | kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n", |
| 2734 | __func__, va, index, vcpu, |
| 2735 | read_c0_entryhi()); |
| 2736 | er = EMULATE_FAIL; |
| 2737 | } |
Sanjay Lal | e685c68 | 2012-11-21 18:34:04 -0800 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | return er; |
| 2742 | } |