44x_emulate.c 4.6 KB

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