44x_emulate.c 4.4 KB

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