44x_emulate.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "booke.h"
  24. #include "44x_tlb.h"
  25. #define OP_RFI 19
  26. #define XOP_RFI 50
  27. #define XOP_MFMSR 83
  28. #define XOP_WRTEE 131
  29. #define XOP_MTMSR 146
  30. #define XOP_WRTEEI 163
  31. #define XOP_MFDCR 323
  32. #define XOP_MTDCR 451
  33. #define XOP_TLBSX 914
  34. #define XOP_ICCCI 966
  35. #define XOP_TLBWE 978
  36. static inline void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
  37. {
  38. if (vcpu->arch.pid != new_pid) {
  39. vcpu->arch.pid = new_pid;
  40. vcpu->arch.swap_pid = 1;
  41. }
  42. }
  43. static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
  44. {
  45. vcpu->arch.pc = vcpu->arch.srr0;
  46. kvmppc_set_msr(vcpu, vcpu->arch.srr1);
  47. }
  48. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  49. unsigned int inst, int *advance)
  50. {
  51. int emulated = EMULATE_DONE;
  52. int dcrn;
  53. int ra;
  54. int rb;
  55. int rc;
  56. int rs;
  57. int rt;
  58. int ws;
  59. switch (get_op(inst)) {
  60. case OP_RFI:
  61. switch (get_xop(inst)) {
  62. case XOP_RFI:
  63. kvmppc_emul_rfi(vcpu);
  64. *advance = 0;
  65. break;
  66. default:
  67. emulated = EMULATE_FAIL;
  68. break;
  69. }
  70. break;
  71. case 31:
  72. switch (get_xop(inst)) {
  73. case XOP_MFMSR:
  74. rt = get_rt(inst);
  75. vcpu->arch.gpr[rt] = vcpu->arch.msr;
  76. break;
  77. case XOP_MTMSR:
  78. rs = get_rs(inst);
  79. kvmppc_set_msr(vcpu, vcpu->arch.gpr[rs]);
  80. break;
  81. case XOP_WRTEE:
  82. rs = get_rs(inst);
  83. vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
  84. | (vcpu->arch.gpr[rs] & MSR_EE);
  85. break;
  86. case XOP_WRTEEI:
  87. vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
  88. | (inst & MSR_EE);
  89. break;
  90. case XOP_MFDCR:
  91. dcrn = get_dcrn(inst);
  92. rt = get_rt(inst);
  93. /* The guest may access CPR0 registers to determine the timebase
  94. * frequency, and it must know the real host frequency because it
  95. * can directly access the timebase registers.
  96. *
  97. * It would be possible to emulate those accesses in userspace,
  98. * but userspace can really only figure out the end frequency.
  99. * We could decompose that into the factors that compute it, but
  100. * that's tricky math, and it's easier to just report the real
  101. * CPR0 values.
  102. */
  103. switch (dcrn) {
  104. case DCRN_CPR0_CONFIG_ADDR:
  105. vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr;
  106. break;
  107. case DCRN_CPR0_CONFIG_DATA:
  108. local_irq_disable();
  109. mtdcr(DCRN_CPR0_CONFIG_ADDR,
  110. vcpu->arch.cpr0_cfgaddr);
  111. vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CONFIG_DATA);
  112. local_irq_enable();
  113. break;
  114. default:
  115. run->dcr.dcrn = dcrn;
  116. run->dcr.data = 0;
  117. run->dcr.is_write = 0;
  118. vcpu->arch.io_gpr = rt;
  119. vcpu->arch.dcr_needed = 1;
  120. emulated = EMULATE_DO_DCR;
  121. }
  122. break;
  123. case XOP_MTDCR:
  124. dcrn = get_dcrn(inst);
  125. rs = get_rs(inst);
  126. /* emulate some access in kernel */
  127. switch (dcrn) {
  128. case DCRN_CPR0_CONFIG_ADDR:
  129. vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs];
  130. break;
  131. default:
  132. run->dcr.dcrn = dcrn;
  133. run->dcr.data = vcpu->arch.gpr[rs];
  134. run->dcr.is_write = 1;
  135. vcpu->arch.dcr_needed = 1;
  136. emulated = EMULATE_DO_DCR;
  137. }
  138. break;
  139. case XOP_TLBWE:
  140. ra = get_ra(inst);
  141. rs = get_rs(inst);
  142. ws = get_ws(inst);
  143. emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
  144. break;
  145. case XOP_TLBSX:
  146. rt = get_rt(inst);
  147. ra = get_ra(inst);
  148. rb = get_rb(inst);
  149. rc = get_rc(inst);
  150. emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
  151. break;
  152. case XOP_ICCCI:
  153. break;
  154. default:
  155. emulated = EMULATE_FAIL;
  156. }
  157. break;
  158. default:
  159. emulated = EMULATE_FAIL;
  160. }
  161. return emulated;
  162. }
  163. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  164. {
  165. switch (sprn) {
  166. case SPRN_MMUCR:
  167. vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break;
  168. case SPRN_PID:
  169. kvmppc_set_pid(vcpu, vcpu->arch.gpr[rs]); break;
  170. case SPRN_CCR0:
  171. vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break;
  172. case SPRN_CCR1:
  173. vcpu->arch.ccr1 = vcpu->arch.gpr[rs]; break;
  174. case SPRN_DEAR:
  175. vcpu->arch.dear = vcpu->arch.gpr[rs]; break;
  176. case SPRN_ESR:
  177. vcpu->arch.esr = vcpu->arch.gpr[rs]; break;
  178. case SPRN_DBCR0:
  179. vcpu->arch.dbcr0 = vcpu->arch.gpr[rs]; break;
  180. case SPRN_DBCR1:
  181. vcpu->arch.dbcr1 = vcpu->arch.gpr[rs]; break;
  182. case SPRN_TSR:
  183. vcpu->arch.tsr &= ~vcpu->arch.gpr[rs]; break;
  184. case SPRN_TCR:
  185. vcpu->arch.tcr = vcpu->arch.gpr[rs];
  186. kvmppc_emulate_dec(vcpu);
  187. break;
  188. /* Note: SPRG4-7 are user-readable. These values are
  189. * loaded into the real SPRGs when resuming the
  190. * guest. */
  191. case SPRN_SPRG4:
  192. vcpu->arch.sprg4 = vcpu->arch.gpr[rs]; break;
  193. case SPRN_SPRG5:
  194. vcpu->arch.sprg5 = vcpu->arch.gpr[rs]; break;
  195. case SPRN_SPRG6:
  196. vcpu->arch.sprg6 = vcpu->arch.gpr[rs]; break;
  197. case SPRN_SPRG7:
  198. vcpu->arch.sprg7 = vcpu->arch.gpr[rs]; break;
  199. case SPRN_IVPR:
  200. vcpu->arch.ivpr = vcpu->arch.gpr[rs]; break;
  201. case SPRN_IVOR0:
  202. vcpu->arch.ivor[0] = vcpu->arch.gpr[rs]; break;
  203. case SPRN_IVOR1:
  204. vcpu->arch.ivor[1] = vcpu->arch.gpr[rs]; break;
  205. case SPRN_IVOR2:
  206. vcpu->arch.ivor[2] = vcpu->arch.gpr[rs]; break;
  207. case SPRN_IVOR3:
  208. vcpu->arch.ivor[3] = vcpu->arch.gpr[rs]; break;
  209. case SPRN_IVOR4:
  210. vcpu->arch.ivor[4] = vcpu->arch.gpr[rs]; break;
  211. case SPRN_IVOR5:
  212. vcpu->arch.ivor[5] = vcpu->arch.gpr[rs]; break;
  213. case SPRN_IVOR6:
  214. vcpu->arch.ivor[6] = vcpu->arch.gpr[rs]; break;
  215. case SPRN_IVOR7:
  216. vcpu->arch.ivor[7] = vcpu->arch.gpr[rs]; break;
  217. case SPRN_IVOR8:
  218. vcpu->arch.ivor[8] = vcpu->arch.gpr[rs]; break;
  219. case SPRN_IVOR9:
  220. vcpu->arch.ivor[9] = vcpu->arch.gpr[rs]; break;
  221. case SPRN_IVOR10:
  222. vcpu->arch.ivor[10] = vcpu->arch.gpr[rs]; break;
  223. case SPRN_IVOR11:
  224. vcpu->arch.ivor[11] = vcpu->arch.gpr[rs]; break;
  225. case SPRN_IVOR12:
  226. vcpu->arch.ivor[12] = vcpu->arch.gpr[rs]; break;
  227. case SPRN_IVOR13:
  228. vcpu->arch.ivor[13] = vcpu->arch.gpr[rs]; break;
  229. case SPRN_IVOR14:
  230. vcpu->arch.ivor[14] = vcpu->arch.gpr[rs]; break;
  231. case SPRN_IVOR15:
  232. vcpu->arch.ivor[15] = vcpu->arch.gpr[rs]; break;
  233. default:
  234. return EMULATE_FAIL;
  235. }
  236. return EMULATE_DONE;
  237. }
  238. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  239. {
  240. switch (sprn) {
  241. /* 440 */
  242. case SPRN_MMUCR:
  243. vcpu->arch.gpr[rt] = vcpu->arch.mmucr; break;
  244. case SPRN_CCR0:
  245. vcpu->arch.gpr[rt] = vcpu->arch.ccr0; break;
  246. case SPRN_CCR1:
  247. vcpu->arch.gpr[rt] = vcpu->arch.ccr1; break;
  248. /* Book E */
  249. case SPRN_PID:
  250. vcpu->arch.gpr[rt] = vcpu->arch.pid; break;
  251. case SPRN_IVPR:
  252. vcpu->arch.gpr[rt] = vcpu->arch.ivpr; break;
  253. case SPRN_DEAR:
  254. vcpu->arch.gpr[rt] = vcpu->arch.dear; break;
  255. case SPRN_ESR:
  256. vcpu->arch.gpr[rt] = vcpu->arch.esr; break;
  257. case SPRN_DBCR0:
  258. vcpu->arch.gpr[rt] = vcpu->arch.dbcr0; break;
  259. case SPRN_DBCR1:
  260. vcpu->arch.gpr[rt] = vcpu->arch.dbcr1; break;
  261. case SPRN_IVOR0:
  262. vcpu->arch.gpr[rt] = vcpu->arch.ivor[0]; break;
  263. case SPRN_IVOR1:
  264. vcpu->arch.gpr[rt] = vcpu->arch.ivor[1]; break;
  265. case SPRN_IVOR2:
  266. vcpu->arch.gpr[rt] = vcpu->arch.ivor[2]; break;
  267. case SPRN_IVOR3:
  268. vcpu->arch.gpr[rt] = vcpu->arch.ivor[3]; break;
  269. case SPRN_IVOR4:
  270. vcpu->arch.gpr[rt] = vcpu->arch.ivor[4]; break;
  271. case SPRN_IVOR5:
  272. vcpu->arch.gpr[rt] = vcpu->arch.ivor[5]; break;
  273. case SPRN_IVOR6:
  274. vcpu->arch.gpr[rt] = vcpu->arch.ivor[6]; break;
  275. case SPRN_IVOR7:
  276. vcpu->arch.gpr[rt] = vcpu->arch.ivor[7]; break;
  277. case SPRN_IVOR8:
  278. vcpu->arch.gpr[rt] = vcpu->arch.ivor[8]; break;
  279. case SPRN_IVOR9:
  280. vcpu->arch.gpr[rt] = vcpu->arch.ivor[9]; break;
  281. case SPRN_IVOR10:
  282. vcpu->arch.gpr[rt] = vcpu->arch.ivor[10]; break;
  283. case SPRN_IVOR11:
  284. vcpu->arch.gpr[rt] = vcpu->arch.ivor[11]; break;
  285. case SPRN_IVOR12:
  286. vcpu->arch.gpr[rt] = vcpu->arch.ivor[12]; break;
  287. case SPRN_IVOR13:
  288. vcpu->arch.gpr[rt] = vcpu->arch.ivor[13]; break;
  289. case SPRN_IVOR14:
  290. vcpu->arch.gpr[rt] = vcpu->arch.ivor[14]; break;
  291. case SPRN_IVOR15:
  292. vcpu->arch.gpr[rt] = vcpu->arch.ivor[15]; break;
  293. default:
  294. return EMULATE_FAIL;
  295. }
  296. return EMULATE_DONE;
  297. }