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