Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 1 | /* |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * KVM/MIPS: Deliver/Emulate exceptions to the guest kernel |
| 7 | * |
| 8 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 9 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 10 | */ |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | |
| 17 | #include <linux/kvm_host.h> |
| 18 | |
Deng-Cheng Zhu | d7d5b05 | 2014-06-26 12:11:38 -0700 | [diff] [blame] | 19 | #include "opcode.h" |
| 20 | #include "interrupt.h" |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 21 | |
| 22 | static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva) |
| 23 | { |
| 24 | gpa_t gpa; |
| 25 | uint32_t kseg = KSEGX(gva); |
| 26 | |
| 27 | if ((kseg == CKSEG0) || (kseg == CKSEG1)) |
| 28 | gpa = CPHYSADDR(gva); |
| 29 | else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 30 | kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 31 | kvm_mips_dump_host_tlbs(); |
| 32 | gpa = KVM_INVALID_ADDR; |
| 33 | } |
| 34 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 35 | kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 36 | |
| 37 | return gpa; |
| 38 | } |
| 39 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 40 | static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu) |
| 41 | { |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame^] | 42 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 43 | struct kvm_run *run = vcpu->run; |
| 44 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 45 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 46 | enum emulation_result er = EMULATE_DONE; |
| 47 | int ret = RESUME_GUEST; |
| 48 | |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame^] | 49 | if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) { |
| 50 | /* FPU Unusable */ |
| 51 | if (!kvm_mips_guest_has_fpu(&vcpu->arch) || |
| 52 | (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) { |
| 53 | /* |
| 54 | * Unusable/no FPU in guest: |
| 55 | * deliver guest COP1 Unusable Exception |
| 56 | */ |
| 57 | er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu); |
| 58 | } else { |
| 59 | /* Restore FPU state */ |
| 60 | kvm_own_fpu(vcpu); |
| 61 | er = EMULATE_DONE; |
| 62 | } |
| 63 | } else { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 64 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame^] | 65 | } |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 66 | |
| 67 | switch (er) { |
| 68 | case EMULATE_DONE: |
| 69 | ret = RESUME_GUEST; |
| 70 | break; |
| 71 | |
| 72 | case EMULATE_FAIL: |
| 73 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 74 | ret = RESUME_HOST; |
| 75 | break; |
| 76 | |
| 77 | case EMULATE_WAIT: |
| 78 | run->exit_reason = KVM_EXIT_INTR; |
| 79 | ret = RESUME_HOST; |
| 80 | break; |
| 81 | |
| 82 | default: |
| 83 | BUG(); |
| 84 | } |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu) |
| 89 | { |
| 90 | struct kvm_run *run = vcpu->run; |
| 91 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 92 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 93 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 94 | enum emulation_result er = EMULATE_DONE; |
| 95 | int ret = RESUME_GUEST; |
| 96 | |
| 97 | if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0 |
| 98 | || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 99 | kvm_debug("USER/KSEG23 ADDR TLB MOD fault: cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 100 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 101 | er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu); |
| 102 | |
| 103 | if (er == EMULATE_DONE) |
| 104 | ret = RESUME_GUEST; |
| 105 | else { |
| 106 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 107 | ret = RESUME_HOST; |
| 108 | } |
| 109 | } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 110 | /* |
| 111 | * XXXKYMA: The guest kernel does not expect to get this fault |
| 112 | * when we are not using HIGHMEM. Need to address this in a |
| 113 | * HIGHMEM kernel |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 114 | */ |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 115 | kvm_err("TLB MOD fault not handled, cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 116 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 117 | kvm_mips_dump_host_tlbs(); |
| 118 | kvm_arch_vcpu_dump_regs(vcpu); |
| 119 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 120 | ret = RESUME_HOST; |
| 121 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 122 | kvm_err("Illegal TLB Mod fault address , cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 123 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 124 | kvm_mips_dump_host_tlbs(); |
| 125 | kvm_arch_vcpu_dump_regs(vcpu); |
| 126 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 127 | ret = RESUME_HOST; |
| 128 | } |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu) |
| 133 | { |
| 134 | struct kvm_run *run = vcpu->run; |
| 135 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 136 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 137 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 138 | enum emulation_result er = EMULATE_DONE; |
| 139 | int ret = RESUME_GUEST; |
| 140 | |
| 141 | if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) |
| 142 | && KVM_GUEST_KERNEL_MODE(vcpu)) { |
| 143 | if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) { |
| 144 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 145 | ret = RESUME_HOST; |
| 146 | } |
| 147 | } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0 |
| 148 | || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 149 | kvm_debug("USER ADDR TLB LD fault: cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 150 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 151 | er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu); |
| 152 | if (er == EMULATE_DONE) |
| 153 | ret = RESUME_GUEST; |
| 154 | else { |
| 155 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 156 | ret = RESUME_HOST; |
| 157 | } |
| 158 | } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 159 | /* |
| 160 | * All KSEG0 faults are handled by KVM, as the guest kernel does |
| 161 | * not expect to ever get them |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 162 | */ |
| 163 | if (kvm_mips_handle_kseg0_tlb_fault |
| 164 | (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) { |
| 165 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 166 | ret = RESUME_HOST; |
| 167 | } |
| 168 | } else { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 169 | kvm_err("Illegal TLB LD fault address , cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 170 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 171 | kvm_mips_dump_host_tlbs(); |
| 172 | kvm_arch_vcpu_dump_regs(vcpu); |
| 173 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 174 | ret = RESUME_HOST; |
| 175 | } |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu) |
| 180 | { |
| 181 | struct kvm_run *run = vcpu->run; |
| 182 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 183 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 184 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 185 | enum emulation_result er = EMULATE_DONE; |
| 186 | int ret = RESUME_GUEST; |
| 187 | |
| 188 | if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) |
| 189 | && KVM_GUEST_KERNEL_MODE(vcpu)) { |
| 190 | if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) { |
| 191 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 192 | ret = RESUME_HOST; |
| 193 | } |
| 194 | } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0 |
| 195 | || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 196 | kvm_debug("USER ADDR TLB ST fault: PC: %#lx, BadVaddr: %#lx\n", |
| 197 | vcpu->arch.pc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 198 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 199 | /* |
| 200 | * User Address (UA) fault, this could happen if |
| 201 | * (1) TLB entry not present/valid in both Guest and shadow host |
| 202 | * TLBs, in this case we pass on the fault to the guest |
| 203 | * kernel and let it handle it. |
| 204 | * (2) TLB entry is present in the Guest TLB but not in the |
| 205 | * shadow, in this case we inject the TLB from the Guest TLB |
| 206 | * into the shadow host TLB |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 207 | */ |
| 208 | |
| 209 | er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu); |
| 210 | if (er == EMULATE_DONE) |
| 211 | ret = RESUME_GUEST; |
| 212 | else { |
| 213 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 214 | ret = RESUME_HOST; |
| 215 | } |
| 216 | } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) { |
| 217 | if (kvm_mips_handle_kseg0_tlb_fault |
| 218 | (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) { |
| 219 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 220 | ret = RESUME_HOST; |
| 221 | } |
| 222 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 223 | kvm_err("Illegal TLB ST fault address , cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 224 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 225 | kvm_mips_dump_host_tlbs(); |
| 226 | kvm_arch_vcpu_dump_regs(vcpu); |
| 227 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 228 | ret = RESUME_HOST; |
| 229 | } |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu) |
| 234 | { |
| 235 | struct kvm_run *run = vcpu->run; |
| 236 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 237 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 238 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 239 | enum emulation_result er = EMULATE_DONE; |
| 240 | int ret = RESUME_GUEST; |
| 241 | |
| 242 | if (KVM_GUEST_KERNEL_MODE(vcpu) |
| 243 | && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 244 | kvm_debug("Emulate Store to MMIO space\n"); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 245 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
| 246 | if (er == EMULATE_FAIL) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 247 | kvm_err("Emulate Store to MMIO space failed\n"); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 248 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 249 | ret = RESUME_HOST; |
| 250 | } else { |
| 251 | run->exit_reason = KVM_EXIT_MMIO; |
| 252 | ret = RESUME_HOST; |
| 253 | } |
| 254 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 255 | kvm_err("Address Error (STORE): cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 256 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 257 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 258 | ret = RESUME_HOST; |
| 259 | } |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu) |
| 264 | { |
| 265 | struct kvm_run *run = vcpu->run; |
| 266 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 267 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 268 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 269 | enum emulation_result er = EMULATE_DONE; |
| 270 | int ret = RESUME_GUEST; |
| 271 | |
| 272 | if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) { |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 273 | kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 274 | er = kvm_mips_emulate_inst(cause, opc, run, vcpu); |
| 275 | if (er == EMULATE_FAIL) { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 276 | kvm_err("Emulate Load from MMIO space failed\n"); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 277 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 278 | ret = RESUME_HOST; |
| 279 | } else { |
| 280 | run->exit_reason = KVM_EXIT_MMIO; |
| 281 | ret = RESUME_HOST; |
| 282 | } |
| 283 | } else { |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 284 | kvm_err("Address Error (LOAD): cause %#lx, PC: %p, BadVaddr: %#lx\n", |
| 285 | cause, opc, badvaddr); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 286 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 287 | ret = RESUME_HOST; |
| 288 | er = EMULATE_FAIL; |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu) |
| 294 | { |
| 295 | struct kvm_run *run = vcpu->run; |
| 296 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 297 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 298 | enum emulation_result er = EMULATE_DONE; |
| 299 | int ret = RESUME_GUEST; |
| 300 | |
| 301 | er = kvm_mips_emulate_syscall(cause, opc, run, vcpu); |
| 302 | if (er == EMULATE_DONE) |
| 303 | ret = RESUME_GUEST; |
| 304 | else { |
| 305 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 306 | ret = RESUME_HOST; |
| 307 | } |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu) |
| 312 | { |
| 313 | struct kvm_run *run = vcpu->run; |
| 314 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 315 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 316 | enum emulation_result er = EMULATE_DONE; |
| 317 | int ret = RESUME_GUEST; |
| 318 | |
| 319 | er = kvm_mips_handle_ri(cause, opc, run, vcpu); |
| 320 | if (er == EMULATE_DONE) |
| 321 | ret = RESUME_GUEST; |
| 322 | else { |
| 323 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 324 | ret = RESUME_HOST; |
| 325 | } |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu) |
| 330 | { |
| 331 | struct kvm_run *run = vcpu->run; |
| 332 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 333 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 334 | enum emulation_result er = EMULATE_DONE; |
| 335 | int ret = RESUME_GUEST; |
| 336 | |
| 337 | er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu); |
| 338 | if (er == EMULATE_DONE) |
| 339 | ret = RESUME_GUEST; |
| 340 | else { |
| 341 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 342 | ret = RESUME_HOST; |
| 343 | } |
| 344 | return ret; |
| 345 | } |
| 346 | |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 347 | static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu) |
| 348 | { |
| 349 | struct kvm_run *run = vcpu->run; |
| 350 | uint32_t __user *opc = (uint32_t __user *)vcpu->arch.pc; |
| 351 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 352 | enum emulation_result er = EMULATE_DONE; |
| 353 | int ret = RESUME_GUEST; |
| 354 | |
| 355 | er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu); |
| 356 | if (er == EMULATE_DONE) { |
| 357 | ret = RESUME_GUEST; |
| 358 | } else { |
| 359 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 360 | ret = RESUME_HOST; |
| 361 | } |
| 362 | return ret; |
| 363 | } |
| 364 | |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame^] | 365 | static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu) |
| 366 | { |
| 367 | struct kvm_run *run = vcpu->run; |
| 368 | uint32_t __user *opc = (uint32_t __user *)vcpu->arch.pc; |
| 369 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 370 | enum emulation_result er = EMULATE_DONE; |
| 371 | int ret = RESUME_GUEST; |
| 372 | |
| 373 | er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu); |
| 374 | if (er == EMULATE_DONE) { |
| 375 | ret = RESUME_GUEST; |
| 376 | } else { |
| 377 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 378 | ret = RESUME_HOST; |
| 379 | } |
| 380 | return ret; |
| 381 | } |
| 382 | |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 383 | static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu) |
| 384 | { |
| 385 | struct kvm_run *run = vcpu->run; |
| 386 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 387 | unsigned long cause = vcpu->arch.host_cp0_cause; |
| 388 | enum emulation_result er = EMULATE_DONE; |
| 389 | int ret = RESUME_GUEST; |
| 390 | |
| 391 | /* No MSA supported in guest, guest reserved instruction exception */ |
| 392 | er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu); |
| 393 | |
| 394 | switch (er) { |
| 395 | case EMULATE_DONE: |
| 396 | ret = RESUME_GUEST; |
| 397 | break; |
| 398 | |
| 399 | case EMULATE_FAIL: |
| 400 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 401 | ret = RESUME_HOST; |
| 402 | break; |
| 403 | |
| 404 | default: |
| 405 | BUG(); |
| 406 | } |
| 407 | return ret; |
| 408 | } |
| 409 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 410 | static int kvm_trap_emul_vm_init(struct kvm *kvm) |
| 411 | { |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu) |
| 416 | { |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu) |
| 421 | { |
| 422 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 423 | uint32_t config1; |
| 424 | int vcpu_id = vcpu->vcpu_id; |
| 425 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 426 | /* |
| 427 | * Arch specific stuff, set up config registers properly so that the |
| 428 | * guest will come up as expected, for now we simulate a MIPS 24kc |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 429 | */ |
| 430 | kvm_write_c0_guest_prid(cop0, 0x00019300); |
James Hogan | 2211ee8 | 2015-03-04 15:56:47 +0000 | [diff] [blame] | 431 | /* Have config1, Cacheable, noncoherent, write-back, write allocate */ |
| 432 | kvm_write_c0_guest_config(cop0, MIPS_CONF_M | (0x3 << CP0C0_K0) | |
| 433 | (0x1 << CP0C0_AR) | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 434 | (MMU_TYPE_R4000 << CP0C0_MT)); |
| 435 | |
| 436 | /* Read the cache characteristics from the host Config1 Register */ |
| 437 | config1 = (read_c0_config1() & ~0x7f); |
| 438 | |
| 439 | /* Set up MMU size */ |
| 440 | config1 &= ~(0x3f << 25); |
| 441 | config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25); |
| 442 | |
| 443 | /* We unset some bits that we aren't emulating */ |
| 444 | config1 &= |
| 445 | ~((1 << CP0C1_C2) | (1 << CP0C1_MD) | (1 << CP0C1_PC) | |
| 446 | (1 << CP0C1_WR) | (1 << CP0C1_CA)); |
| 447 | kvm_write_c0_guest_config1(cop0, config1); |
| 448 | |
James Hogan | 2211ee8 | 2015-03-04 15:56:47 +0000 | [diff] [blame] | 449 | /* Have config3, no tertiary/secondary caches implemented */ |
| 450 | kvm_write_c0_guest_config2(cop0, MIPS_CONF_M); |
| 451 | /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */ |
| 452 | |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 453 | /* Have config4, UserLocal */ |
| 454 | kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI); |
| 455 | |
| 456 | /* Have config5 */ |
| 457 | kvm_write_c0_guest_config4(cop0, MIPS_CONF_M); |
| 458 | |
| 459 | /* No config6 */ |
| 460 | kvm_write_c0_guest_config5(cop0, 0); |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 461 | |
| 462 | /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */ |
| 463 | kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10)); |
| 464 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 465 | /* |
| 466 | * Setup IntCtl defaults, compatibilty mode for timer interrupts (HW5) |
| 467 | */ |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 468 | kvm_write_c0_guest_intctl(cop0, 0xFC000000); |
| 469 | |
| 470 | /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */ |
| 471 | kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 | (vcpu_id & 0xFF)); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 476 | static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu, |
| 477 | const struct kvm_one_reg *reg, |
| 478 | s64 *v) |
| 479 | { |
| 480 | switch (reg->id) { |
| 481 | case KVM_REG_MIPS_CP0_COUNT: |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 482 | *v = kvm_mips_read_count(vcpu); |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 483 | break; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 484 | case KVM_REG_MIPS_COUNT_CTL: |
| 485 | *v = vcpu->arch.count_ctl; |
| 486 | break; |
| 487 | case KVM_REG_MIPS_COUNT_RESUME: |
| 488 | *v = ktime_to_ns(vcpu->arch.count_resume); |
| 489 | break; |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 490 | case KVM_REG_MIPS_COUNT_HZ: |
| 491 | *v = vcpu->arch.count_hz; |
| 492 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 493 | default: |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu, |
| 500 | const struct kvm_one_reg *reg, |
| 501 | s64 v) |
| 502 | { |
| 503 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 504 | int ret = 0; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 505 | unsigned int cur, change; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 506 | |
| 507 | switch (reg->id) { |
| 508 | case KVM_REG_MIPS_CP0_COUNT: |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 509 | kvm_mips_write_count(vcpu, v); |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 510 | break; |
| 511 | case KVM_REG_MIPS_CP0_COMPARE: |
James Hogan | e30492b | 2014-05-29 10:16:35 +0100 | [diff] [blame] | 512 | kvm_mips_write_compare(vcpu, v); |
| 513 | break; |
| 514 | case KVM_REG_MIPS_CP0_CAUSE: |
| 515 | /* |
| 516 | * If the timer is stopped or started (DC bit) it must look |
| 517 | * atomic with changes to the interrupt pending bits (TI, IRQ5). |
| 518 | * A timer interrupt should not happen in between. |
| 519 | */ |
| 520 | if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) { |
| 521 | if (v & CAUSEF_DC) { |
| 522 | /* disable timer first */ |
| 523 | kvm_mips_count_disable_cause(vcpu); |
| 524 | kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v); |
| 525 | } else { |
| 526 | /* enable timer last */ |
| 527 | kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v); |
| 528 | kvm_mips_count_enable_cause(vcpu); |
| 529 | } |
| 530 | } else { |
| 531 | kvm_write_c0_guest_cause(cop0, v); |
| 532 | } |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 533 | break; |
James Hogan | c771607 | 2014-06-26 15:11:29 +0100 | [diff] [blame] | 534 | case KVM_REG_MIPS_CP0_CONFIG: |
| 535 | /* read-only for now */ |
| 536 | break; |
| 537 | case KVM_REG_MIPS_CP0_CONFIG1: |
| 538 | cur = kvm_read_c0_guest_config1(cop0); |
| 539 | change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu); |
| 540 | if (change) { |
| 541 | v = cur ^ change; |
| 542 | kvm_write_c0_guest_config1(cop0, v); |
| 543 | } |
| 544 | break; |
| 545 | case KVM_REG_MIPS_CP0_CONFIG2: |
| 546 | /* read-only for now */ |
| 547 | break; |
| 548 | case KVM_REG_MIPS_CP0_CONFIG3: |
| 549 | cur = kvm_read_c0_guest_config3(cop0); |
| 550 | change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu); |
| 551 | if (change) { |
| 552 | v = cur ^ change; |
| 553 | kvm_write_c0_guest_config3(cop0, v); |
| 554 | } |
| 555 | break; |
| 556 | case KVM_REG_MIPS_CP0_CONFIG4: |
| 557 | cur = kvm_read_c0_guest_config4(cop0); |
| 558 | change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu); |
| 559 | if (change) { |
| 560 | v = cur ^ change; |
| 561 | kvm_write_c0_guest_config4(cop0, v); |
| 562 | } |
| 563 | break; |
| 564 | case KVM_REG_MIPS_CP0_CONFIG5: |
| 565 | cur = kvm_read_c0_guest_config5(cop0); |
| 566 | change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu); |
| 567 | if (change) { |
| 568 | v = cur ^ change; |
| 569 | kvm_write_c0_guest_config5(cop0, v); |
| 570 | } |
| 571 | break; |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 572 | case KVM_REG_MIPS_COUNT_CTL: |
| 573 | ret = kvm_mips_set_count_ctl(vcpu, v); |
| 574 | break; |
| 575 | case KVM_REG_MIPS_COUNT_RESUME: |
| 576 | ret = kvm_mips_set_count_resume(vcpu, v); |
| 577 | break; |
James Hogan | f74a8e2 | 2014-05-29 10:16:38 +0100 | [diff] [blame] | 578 | case KVM_REG_MIPS_COUNT_HZ: |
| 579 | ret = kvm_mips_set_count_hz(vcpu, v); |
| 580 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 581 | default: |
| 582 | return -EINVAL; |
| 583 | } |
James Hogan | f823934 | 2014-05-29 10:16:37 +0100 | [diff] [blame] | 584 | return ret; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 585 | } |
| 586 | |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 587 | static int kvm_trap_emul_vcpu_get_regs(struct kvm_vcpu *vcpu) |
| 588 | { |
James Hogan | 98e91b8 | 2014-11-18 14:09:12 +0000 | [diff] [blame] | 589 | kvm_lose_fpu(vcpu); |
| 590 | |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int kvm_trap_emul_vcpu_set_regs(struct kvm_vcpu *vcpu) |
| 595 | { |
| 596 | return 0; |
| 597 | } |
| 598 | |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 599 | static struct kvm_mips_callbacks kvm_trap_emul_callbacks = { |
| 600 | /* exit handlers */ |
| 601 | .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable, |
| 602 | .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod, |
| 603 | .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss, |
| 604 | .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss, |
| 605 | .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st, |
| 606 | .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld, |
| 607 | .handle_syscall = kvm_trap_emul_handle_syscall, |
| 608 | .handle_res_inst = kvm_trap_emul_handle_res_inst, |
| 609 | .handle_break = kvm_trap_emul_handle_break, |
James Hogan | 0a56042 | 2015-02-06 16:03:57 +0000 | [diff] [blame] | 610 | .handle_trap = kvm_trap_emul_handle_trap, |
James Hogan | 1c0cd66 | 2015-02-06 10:56:27 +0000 | [diff] [blame^] | 611 | .handle_fpe = kvm_trap_emul_handle_fpe, |
James Hogan | 98119ad | 2015-02-06 11:11:56 +0000 | [diff] [blame] | 612 | .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled, |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 613 | |
| 614 | .vm_init = kvm_trap_emul_vm_init, |
| 615 | .vcpu_init = kvm_trap_emul_vcpu_init, |
| 616 | .vcpu_setup = kvm_trap_emul_vcpu_setup, |
| 617 | .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb, |
| 618 | .queue_timer_int = kvm_mips_queue_timer_int_cb, |
| 619 | .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb, |
| 620 | .queue_io_int = kvm_mips_queue_io_int_cb, |
| 621 | .dequeue_io_int = kvm_mips_dequeue_io_int_cb, |
| 622 | .irq_deliver = kvm_mips_irq_deliver_cb, |
| 623 | .irq_clear = kvm_mips_irq_clear_cb, |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 624 | .get_one_reg = kvm_trap_emul_get_one_reg, |
| 625 | .set_one_reg = kvm_trap_emul_set_one_reg, |
James Hogan | b86ecb3 | 2015-02-09 16:35:20 +0000 | [diff] [blame] | 626 | .vcpu_get_regs = kvm_trap_emul_vcpu_get_regs, |
| 627 | .vcpu_set_regs = kvm_trap_emul_vcpu_set_regs, |
Sanjay Lal | f5c236d | 2012-11-21 18:34:09 -0800 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks) |
| 631 | { |
| 632 | *install_callbacks = &kvm_trap_emul_callbacks; |
| 633 | return 0; |
| 634 | } |