44x_emulate.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2008
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #include <asm/kvm_ppc.h>
  20. #include <asm/dcr.h>
  21. #include <asm/dcr-regs.h>
  22. #include <asm/disassemble.h>
  23. #include <asm/kvm_44x.h>
  24. #include "timing.h"
  25. #include "booke.h"
  26. #include "44x_tlb.h"
  27. #define XOP_MFDCR 323
  28. #define XOP_MTDCR 451
  29. #define XOP_TLBSX 914
  30. #define XOP_ICCCI 966
  31. #define XOP_TLBWE 978
  32. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  33. unsigned int inst, int *advance)
  34. {
  35. int emulated = EMULATE_DONE;
  36. int dcrn = get_dcrn(inst);
  37. int ra = get_ra(inst);
  38. int rb = get_rb(inst);
  39. int rc = get_rc(inst);
  40. int rs = get_rs(inst);
  41. int rt = get_rt(inst);
  42. int ws = get_ws(inst);
  43. switch (get_op(inst)) {
  44. case 31:
  45. switch (get_xop(inst)) {
  46. case XOP_MFDCR:
  47. /* The guest may access CPR0 registers to determine the timebase
  48. * frequency, and it must know the real host frequency because it
  49. * can directly access the timebase registers.
  50. *
  51. * It would be possible to emulate those accesses in userspace,
  52. * but userspace can really only figure out the end frequency.
  53. * We could decompose that into the factors that compute it, but
  54. * that's tricky math, and it's easier to just report the real
  55. * CPR0 values.
  56. */
  57. switch (dcrn) {
  58. case DCRN_CPR0_CONFIG_ADDR:
  59. kvmppc_set_gpr(vcpu, rt, vcpu->arch.cpr0_cfgaddr);
  60. break;
  61. case DCRN_CPR0_CONFIG_DATA:
  62. local_irq_disable();
  63. mtdcr(DCRN_CPR0_CONFIG_ADDR,
  64. vcpu->arch.cpr0_cfgaddr);
  65. kvmppc_set_gpr(vcpu, rt,
  66. mfdcr(DCRN_CPR0_CONFIG_DATA));
  67. local_irq_enable();
  68. break;
  69. default:
  70. run->dcr.dcrn = dcrn;
  71. run->dcr.data = 0;
  72. run->dcr.is_write = 0;
  73. vcpu->arch.io_gpr = rt;
  74. vcpu->arch.dcr_needed = 1;
  75. kvmppc_account_exit(vcpu, DCR_EXITS);
  76. emulated = EMULATE_DO_DCR;
  77. }
  78. break;
  79. case XOP_MTDCR:
  80. /* emulate some access in kernel */
  81. switch (dcrn) {
  82. case DCRN_CPR0_CONFIG_ADDR:
  83. vcpu->arch.cpr0_cfgaddr = kvmppc_get_gpr(vcpu, rs);
  84. break;
  85. default:
  86. run->dcr.dcrn = dcrn;
  87. run->dcr.data = kvmppc_get_gpr(vcpu, rs);
  88. run->dcr.is_write = 1;
  89. vcpu->arch.dcr_needed = 1;
  90. kvmppc_account_exit(vcpu, DCR_EXITS);
  91. emulated = EMULATE_DO_DCR;
  92. }
  93. break;
  94. case XOP_TLBWE:
  95. emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
  96. break;
  97. case XOP_TLBSX:
  98. emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
  99. break;
  100. case XOP_ICCCI:
  101. break;
  102. default:
  103. emulated = EMULATE_FAIL;
  104. }
  105. break;
  106. default:
  107. emulated = EMULATE_FAIL;
  108. }
  109. if (emulated == EMULATE_FAIL)
  110. emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
  111. return emulated;
  112. }
  113. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  114. {
  115. int emulated = EMULATE_DONE;
  116. switch (sprn) {
  117. case SPRN_PID:
  118. kvmppc_set_pid(vcpu, spr_val); break;
  119. case SPRN_MMUCR:
  120. vcpu->arch.mmucr = spr_val; break;
  121. case SPRN_CCR0:
  122. vcpu->arch.ccr0 = spr_val; break;
  123. case SPRN_CCR1:
  124. vcpu->arch.ccr1 = spr_val; break;
  125. default:
  126. emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, spr_val);
  127. }
  128. return emulated;
  129. }
  130. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  131. {
  132. int emulated = EMULATE_DONE;
  133. switch (sprn) {
  134. case SPRN_PID:
  135. *spr_val = vcpu->arch.pid; break;
  136. case SPRN_MMUCR:
  137. *spr_val = vcpu->arch.mmucr; break;
  138. case SPRN_CCR0:
  139. *spr_val = vcpu->arch.ccr0; break;
  140. case SPRN_CCR1:
  141. *spr_val = vcpu->arch.ccr1; break;
  142. default:
  143. emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, spr_val);
  144. }
  145. return emulated;
  146. }