44x_emulate.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  37. int ra;
  38. int rb;
  39. int rc;
  40. int rs;
  41. int rt;
  42. int ws;
  43. switch (get_op(inst)) {
  44. case 31:
  45. switch (get_xop(inst)) {
  46. case XOP_MFDCR:
  47. dcrn = get_dcrn(inst);
  48. rt = get_rt(inst);
  49. /* The guest may access CPR0 registers to determine the timebase
  50. * frequency, and it must know the real host frequency because it
  51. * can directly access the timebase registers.
  52. *
  53. * It would be possible to emulate those accesses in userspace,
  54. * but userspace can really only figure out the end frequency.
  55. * We could decompose that into the factors that compute it, but
  56. * that's tricky math, and it's easier to just report the real
  57. * CPR0 values.
  58. */
  59. switch (dcrn) {
  60. case DCRN_CPR0_CONFIG_ADDR:
  61. kvmppc_set_gpr(vcpu, rt, vcpu->arch.cpr0_cfgaddr);
  62. break;
  63. case DCRN_CPR0_CONFIG_DATA:
  64. local_irq_disable();
  65. mtdcr(DCRN_CPR0_CONFIG_ADDR,
  66. vcpu->arch.cpr0_cfgaddr);
  67. kvmppc_set_gpr(vcpu, rt,
  68. mfdcr(DCRN_CPR0_CONFIG_DATA));
  69. local_irq_enable();
  70. break;
  71. default:
  72. run->dcr.dcrn = dcrn;
  73. run->dcr.data = 0;
  74. run->dcr.is_write = 0;
  75. vcpu->arch.dcr_is_write = 0;
  76. vcpu->arch.io_gpr = rt;
  77. vcpu->arch.dcr_needed = 1;
  78. kvmppc_account_exit(vcpu, DCR_EXITS);
  79. emulated = EMULATE_DO_DCR;
  80. }
  81. break;
  82. case XOP_MTDCR:
  83. dcrn = get_dcrn(inst);
  84. rs = get_rs(inst);
  85. /* emulate some access in kernel */
  86. switch (dcrn) {
  87. case DCRN_CPR0_CONFIG_ADDR:
  88. vcpu->arch.cpr0_cfgaddr = kvmppc_get_gpr(vcpu, rs);
  89. break;
  90. default:
  91. run->dcr.dcrn = dcrn;
  92. run->dcr.data = kvmppc_get_gpr(vcpu, rs);
  93. run->dcr.is_write = 1;
  94. vcpu->arch.dcr_is_write = 1;
  95. vcpu->arch.dcr_needed = 1;
  96. kvmppc_account_exit(vcpu, DCR_EXITS);
  97. emulated = EMULATE_DO_DCR;
  98. }
  99. break;
  100. case XOP_TLBWE:
  101. ra = get_ra(inst);
  102. rs = get_rs(inst);
  103. ws = get_ws(inst);
  104. emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
  105. break;
  106. case XOP_TLBSX:
  107. rt = get_rt(inst);
  108. ra = get_ra(inst);
  109. rb = get_rb(inst);
  110. rc = get_rc(inst);
  111. emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
  112. break;
  113. case XOP_ICCCI:
  114. break;
  115. default:
  116. emulated = EMULATE_FAIL;
  117. }
  118. break;
  119. default:
  120. emulated = EMULATE_FAIL;
  121. }
  122. if (emulated == EMULATE_FAIL)
  123. emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
  124. return emulated;
  125. }
  126. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  127. {
  128. int emulated = EMULATE_DONE;
  129. switch (sprn) {
  130. case SPRN_PID:
  131. kvmppc_set_pid(vcpu, kvmppc_get_gpr(vcpu, rs)); break;
  132. case SPRN_MMUCR:
  133. vcpu->arch.mmucr = kvmppc_get_gpr(vcpu, rs); break;
  134. case SPRN_CCR0:
  135. vcpu->arch.ccr0 = kvmppc_get_gpr(vcpu, rs); break;
  136. case SPRN_CCR1:
  137. vcpu->arch.ccr1 = kvmppc_get_gpr(vcpu, rs); break;
  138. default:
  139. emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, rs);
  140. }
  141. return emulated;
  142. }
  143. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  144. {
  145. int emulated = EMULATE_DONE;
  146. switch (sprn) {
  147. case SPRN_PID:
  148. kvmppc_set_gpr(vcpu, rt, vcpu->arch.pid); break;
  149. case SPRN_MMUCR:
  150. kvmppc_set_gpr(vcpu, rt, vcpu->arch.mmucr); break;
  151. case SPRN_CCR0:
  152. kvmppc_set_gpr(vcpu, rt, vcpu->arch.ccr0); break;
  153. case SPRN_CCR1:
  154. kvmppc_set_gpr(vcpu, rt, vcpu->arch.ccr1); break;
  155. default:
  156. emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, rt);
  157. }
  158. return emulated;
  159. }