emulate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. 2007
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #include <linux/jiffies.h>
  20. #include <linux/timer.h>
  21. #include <linux/types.h>
  22. #include <linux/string.h>
  23. #include <linux/kvm_host.h>
  24. #include <asm/reg.h>
  25. #include <asm/time.h>
  26. #include <asm/byteorder.h>
  27. #include <asm/kvm_ppc.h>
  28. #include <asm/disassemble.h>
  29. #include "timing.h"
  30. void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
  31. {
  32. if (vcpu->arch.tcr & TCR_DIE) {
  33. /* The decrementer ticks at the same rate as the timebase, so
  34. * that's how we convert the guest DEC value to the number of
  35. * host ticks. */
  36. unsigned long nr_jiffies;
  37. nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
  38. mod_timer(&vcpu->arch.dec_timer,
  39. get_jiffies_64() + nr_jiffies);
  40. } else {
  41. del_timer(&vcpu->arch.dec_timer);
  42. }
  43. }
  44. /* XXX to do:
  45. * lhax
  46. * lhaux
  47. * lswx
  48. * lswi
  49. * stswx
  50. * stswi
  51. * lha
  52. * lhau
  53. * lmw
  54. * stmw
  55. *
  56. * XXX is_bigendian should depend on MMU mapping or MSR[LE]
  57. */
  58. /* XXX Should probably auto-generate instruction decoding for a particular core
  59. * from opcode tables in the future. */
  60. int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
  61. {
  62. u32 inst = vcpu->arch.last_inst;
  63. u32 ea;
  64. int ra;
  65. int rb;
  66. int rs;
  67. int rt;
  68. int sprn;
  69. enum emulation_result emulated = EMULATE_DONE;
  70. int advance = 1;
  71. /* this default type might be overwritten by subcategories */
  72. kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
  73. switch (get_op(inst)) {
  74. case 3: /* trap */
  75. vcpu->arch.esr |= ESR_PTR;
  76. kvmppc_core_queue_program(vcpu);
  77. advance = 0;
  78. break;
  79. case 31:
  80. switch (get_xop(inst)) {
  81. case 23: /* lwzx */
  82. rt = get_rt(inst);
  83. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  84. break;
  85. case 87: /* lbzx */
  86. rt = get_rt(inst);
  87. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  88. break;
  89. case 151: /* stwx */
  90. rs = get_rs(inst);
  91. emulated = kvmppc_handle_store(run, vcpu,
  92. vcpu->arch.gpr[rs],
  93. 4, 1);
  94. break;
  95. case 215: /* stbx */
  96. rs = get_rs(inst);
  97. emulated = kvmppc_handle_store(run, vcpu,
  98. vcpu->arch.gpr[rs],
  99. 1, 1);
  100. break;
  101. case 247: /* stbux */
  102. rs = get_rs(inst);
  103. ra = get_ra(inst);
  104. rb = get_rb(inst);
  105. ea = vcpu->arch.gpr[rb];
  106. if (ra)
  107. ea += vcpu->arch.gpr[ra];
  108. emulated = kvmppc_handle_store(run, vcpu,
  109. vcpu->arch.gpr[rs],
  110. 1, 1);
  111. vcpu->arch.gpr[rs] = ea;
  112. break;
  113. case 279: /* lhzx */
  114. rt = get_rt(inst);
  115. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  116. break;
  117. case 311: /* lhzux */
  118. rt = get_rt(inst);
  119. ra = get_ra(inst);
  120. rb = get_rb(inst);
  121. ea = vcpu->arch.gpr[rb];
  122. if (ra)
  123. ea += vcpu->arch.gpr[ra];
  124. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  125. vcpu->arch.gpr[ra] = ea;
  126. break;
  127. case 339: /* mfspr */
  128. sprn = get_sprn(inst);
  129. rt = get_rt(inst);
  130. switch (sprn) {
  131. case SPRN_SRR0:
  132. vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
  133. case SPRN_SRR1:
  134. vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
  135. case SPRN_PVR:
  136. vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
  137. /* Note: mftb and TBRL/TBWL are user-accessible, so
  138. * the guest can always access the real TB anyways.
  139. * In fact, we probably will never see these traps. */
  140. case SPRN_TBWL:
  141. vcpu->arch.gpr[rt] = mftbl(); break;
  142. case SPRN_TBWU:
  143. vcpu->arch.gpr[rt] = mftbu(); break;
  144. case SPRN_SPRG0:
  145. vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
  146. case SPRN_SPRG1:
  147. vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
  148. case SPRN_SPRG2:
  149. vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
  150. case SPRN_SPRG3:
  151. vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
  152. /* Note: SPRG4-7 are user-readable, so we don't get
  153. * a trap. */
  154. default:
  155. emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
  156. if (emulated == EMULATE_FAIL) {
  157. printk("mfspr: unknown spr %x\n", sprn);
  158. vcpu->arch.gpr[rt] = 0;
  159. }
  160. break;
  161. }
  162. break;
  163. case 407: /* sthx */
  164. rs = get_rs(inst);
  165. ra = get_ra(inst);
  166. rb = get_rb(inst);
  167. emulated = kvmppc_handle_store(run, vcpu,
  168. vcpu->arch.gpr[rs],
  169. 2, 1);
  170. break;
  171. case 439: /* sthux */
  172. rs = get_rs(inst);
  173. ra = get_ra(inst);
  174. rb = get_rb(inst);
  175. ea = vcpu->arch.gpr[rb];
  176. if (ra)
  177. ea += vcpu->arch.gpr[ra];
  178. emulated = kvmppc_handle_store(run, vcpu,
  179. vcpu->arch.gpr[rs],
  180. 2, 1);
  181. vcpu->arch.gpr[ra] = ea;
  182. break;
  183. case 467: /* mtspr */
  184. sprn = get_sprn(inst);
  185. rs = get_rs(inst);
  186. switch (sprn) {
  187. case SPRN_SRR0:
  188. vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
  189. case SPRN_SRR1:
  190. vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
  191. /* XXX We need to context-switch the timebase for
  192. * watchdog and FIT. */
  193. case SPRN_TBWL: break;
  194. case SPRN_TBWU: break;
  195. case SPRN_DEC:
  196. vcpu->arch.dec = vcpu->arch.gpr[rs];
  197. kvmppc_emulate_dec(vcpu);
  198. break;
  199. case SPRN_SPRG0:
  200. vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
  201. case SPRN_SPRG1:
  202. vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
  203. case SPRN_SPRG2:
  204. vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
  205. case SPRN_SPRG3:
  206. vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
  207. default:
  208. emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
  209. if (emulated == EMULATE_FAIL)
  210. printk("mtspr: unknown spr %x\n", sprn);
  211. break;
  212. }
  213. break;
  214. case 470: /* dcbi */
  215. /* Do nothing. The guest is performing dcbi because
  216. * hardware DMA is not snooped by the dcache, but
  217. * emulated DMA either goes through the dcache as
  218. * normal writes, or the host kernel has handled dcache
  219. * coherence. */
  220. break;
  221. case 534: /* lwbrx */
  222. rt = get_rt(inst);
  223. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
  224. break;
  225. case 566: /* tlbsync */
  226. break;
  227. case 662: /* stwbrx */
  228. rs = get_rs(inst);
  229. ra = get_ra(inst);
  230. rb = get_rb(inst);
  231. emulated = kvmppc_handle_store(run, vcpu,
  232. vcpu->arch.gpr[rs],
  233. 4, 0);
  234. break;
  235. case 790: /* lhbrx */
  236. rt = get_rt(inst);
  237. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
  238. break;
  239. case 918: /* sthbrx */
  240. rs = get_rs(inst);
  241. ra = get_ra(inst);
  242. rb = get_rb(inst);
  243. emulated = kvmppc_handle_store(run, vcpu,
  244. vcpu->arch.gpr[rs],
  245. 2, 0);
  246. break;
  247. default:
  248. /* Attempt core-specific emulation below. */
  249. emulated = EMULATE_FAIL;
  250. }
  251. break;
  252. case 32: /* lwz */
  253. rt = get_rt(inst);
  254. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  255. break;
  256. case 33: /* lwzu */
  257. ra = get_ra(inst);
  258. rt = get_rt(inst);
  259. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  260. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  261. break;
  262. case 34: /* lbz */
  263. rt = get_rt(inst);
  264. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  265. break;
  266. case 35: /* lbzu */
  267. ra = get_ra(inst);
  268. rt = get_rt(inst);
  269. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  270. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  271. break;
  272. case 36: /* stw */
  273. rs = get_rs(inst);
  274. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  275. 4, 1);
  276. break;
  277. case 37: /* stwu */
  278. ra = get_ra(inst);
  279. rs = get_rs(inst);
  280. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  281. 4, 1);
  282. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  283. break;
  284. case 38: /* stb */
  285. rs = get_rs(inst);
  286. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  287. 1, 1);
  288. break;
  289. case 39: /* stbu */
  290. ra = get_ra(inst);
  291. rs = get_rs(inst);
  292. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  293. 1, 1);
  294. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  295. break;
  296. case 40: /* lhz */
  297. rt = get_rt(inst);
  298. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  299. break;
  300. case 41: /* lhzu */
  301. ra = get_ra(inst);
  302. rt = get_rt(inst);
  303. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  304. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  305. break;
  306. case 44: /* sth */
  307. rs = get_rs(inst);
  308. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  309. 2, 1);
  310. break;
  311. case 45: /* sthu */
  312. ra = get_ra(inst);
  313. rs = get_rs(inst);
  314. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  315. 2, 1);
  316. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  317. break;
  318. default:
  319. emulated = EMULATE_FAIL;
  320. }
  321. if (emulated == EMULATE_FAIL) {
  322. emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
  323. if (emulated == EMULATE_FAIL) {
  324. advance = 0;
  325. printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
  326. "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
  327. }
  328. }
  329. KVMTRACE_3D(PPC_INSTR, vcpu, inst, (int)vcpu->arch.pc, emulated, entryexit);
  330. if (advance)
  331. vcpu->arch.pc += 4; /* Advance past emulated instruction. */
  332. return emulated;
  333. }