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