emulate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. * Copyright 2011 Freescale Semiconductor, Inc.
  17. *
  18. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  19. */
  20. #include <linux/jiffies.h>
  21. #include <linux/hrtimer.h>
  22. #include <linux/types.h>
  23. #include <linux/string.h>
  24. #include <linux/kvm_host.h>
  25. #include <linux/clockchips.h>
  26. #include <asm/reg.h>
  27. #include <asm/time.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/kvm_ppc.h>
  30. #include <asm/disassemble.h>
  31. #include <asm/ppc-opcode.h>
  32. #include "timing.h"
  33. #include "trace.h"
  34. void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
  35. {
  36. unsigned long dec_nsec;
  37. unsigned long long dec_time;
  38. pr_debug("mtDEC: %x\n", vcpu->arch.dec);
  39. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  40. #ifdef CONFIG_PPC_BOOK3S
  41. /* mtdec lowers the interrupt line when positive. */
  42. kvmppc_core_dequeue_dec(vcpu);
  43. /* POWER4+ triggers a dec interrupt if the value is < 0 */
  44. if (vcpu->arch.dec & 0x80000000) {
  45. kvmppc_core_queue_dec(vcpu);
  46. return;
  47. }
  48. #endif
  49. #ifdef CONFIG_BOOKE
  50. /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
  51. if (vcpu->arch.dec == 0)
  52. return;
  53. #endif
  54. /*
  55. * The decrementer ticks at the same rate as the timebase, so
  56. * that's how we convert the guest DEC value to the number of
  57. * host ticks.
  58. */
  59. dec_time = vcpu->arch.dec;
  60. /*
  61. * Guest timebase ticks at the same frequency as host decrementer.
  62. * So use the host decrementer calculations for decrementer emulation.
  63. */
  64. dec_time = dec_time << decrementer_clockevent.shift;
  65. do_div(dec_time, decrementer_clockevent.mult);
  66. dec_nsec = do_div(dec_time, NSEC_PER_SEC);
  67. hrtimer_start(&vcpu->arch.dec_timer,
  68. ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
  69. vcpu->arch.dec_jiffies = get_tb();
  70. }
  71. u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
  72. {
  73. u64 jd = tb - vcpu->arch.dec_jiffies;
  74. #ifdef CONFIG_BOOKE
  75. if (vcpu->arch.dec < jd)
  76. return 0;
  77. #endif
  78. return vcpu->arch.dec - jd;
  79. }
  80. static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  81. {
  82. enum emulation_result emulated = EMULATE_DONE;
  83. ulong spr_val = kvmppc_get_gpr(vcpu, rs);
  84. switch (sprn) {
  85. case SPRN_SRR0:
  86. kvmppc_set_srr0(vcpu, spr_val);
  87. break;
  88. case SPRN_SRR1:
  89. kvmppc_set_srr1(vcpu, spr_val);
  90. break;
  91. /* XXX We need to context-switch the timebase for
  92. * watchdog and FIT. */
  93. case SPRN_TBWL: break;
  94. case SPRN_TBWU: break;
  95. case SPRN_DEC:
  96. vcpu->arch.dec = spr_val;
  97. kvmppc_emulate_dec(vcpu);
  98. break;
  99. case SPRN_SPRG0:
  100. kvmppc_set_sprg0(vcpu, spr_val);
  101. break;
  102. case SPRN_SPRG1:
  103. kvmppc_set_sprg1(vcpu, spr_val);
  104. break;
  105. case SPRN_SPRG2:
  106. kvmppc_set_sprg2(vcpu, spr_val);
  107. break;
  108. case SPRN_SPRG3:
  109. kvmppc_set_sprg3(vcpu, spr_val);
  110. break;
  111. /* PIR can legally be written, but we ignore it */
  112. case SPRN_PIR: break;
  113. default:
  114. emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
  115. spr_val);
  116. if (emulated == EMULATE_FAIL)
  117. printk(KERN_INFO "mtspr: unknown spr "
  118. "0x%x\n", sprn);
  119. break;
  120. }
  121. kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
  122. return emulated;
  123. }
  124. static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  125. {
  126. enum emulation_result emulated = EMULATE_DONE;
  127. ulong spr_val = 0;
  128. switch (sprn) {
  129. case SPRN_SRR0:
  130. spr_val = kvmppc_get_srr0(vcpu);
  131. break;
  132. case SPRN_SRR1:
  133. spr_val = kvmppc_get_srr1(vcpu);
  134. break;
  135. case SPRN_PVR:
  136. spr_val = vcpu->arch.pvr;
  137. break;
  138. case SPRN_PIR:
  139. spr_val = vcpu->vcpu_id;
  140. break;
  141. /* Note: mftb and TBRL/TBWL are user-accessible, so
  142. * the guest can always access the real TB anyways.
  143. * In fact, we probably will never see these traps. */
  144. case SPRN_TBWL:
  145. spr_val = get_tb() >> 32;
  146. break;
  147. case SPRN_TBWU:
  148. spr_val = get_tb();
  149. break;
  150. case SPRN_SPRG0:
  151. spr_val = kvmppc_get_sprg0(vcpu);
  152. break;
  153. case SPRN_SPRG1:
  154. spr_val = kvmppc_get_sprg1(vcpu);
  155. break;
  156. case SPRN_SPRG2:
  157. spr_val = kvmppc_get_sprg2(vcpu);
  158. break;
  159. case SPRN_SPRG3:
  160. spr_val = kvmppc_get_sprg3(vcpu);
  161. break;
  162. /* Note: SPRG4-7 are user-readable, so we don't get
  163. * a trap. */
  164. case SPRN_DEC:
  165. spr_val = kvmppc_get_dec(vcpu, get_tb());
  166. break;
  167. default:
  168. emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
  169. &spr_val);
  170. if (unlikely(emulated == EMULATE_FAIL)) {
  171. printk(KERN_INFO "mfspr: unknown spr "
  172. "0x%x\n", sprn);
  173. }
  174. break;
  175. }
  176. if (emulated == EMULATE_DONE)
  177. kvmppc_set_gpr(vcpu, rt, spr_val);
  178. kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
  179. return emulated;
  180. }
  181. /* XXX to do:
  182. * lhax
  183. * lhaux
  184. * lswx
  185. * lswi
  186. * stswx
  187. * stswi
  188. * lha
  189. * lhau
  190. * lmw
  191. * stmw
  192. *
  193. */
  194. /* XXX Should probably auto-generate instruction decoding for a particular core
  195. * from opcode tables in the future. */
  196. int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
  197. {
  198. u32 inst = kvmppc_get_last_inst(vcpu);
  199. int ra = get_ra(inst);
  200. int rs = get_rs(inst);
  201. int rt = get_rt(inst);
  202. int sprn = get_sprn(inst);
  203. enum emulation_result emulated = EMULATE_DONE;
  204. int advance = 1;
  205. /* this default type might be overwritten by subcategories */
  206. kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
  207. pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
  208. switch (get_op(inst)) {
  209. case OP_TRAP:
  210. #ifdef CONFIG_PPC_BOOK3S
  211. case OP_TRAP_64:
  212. kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
  213. #else
  214. kvmppc_core_queue_program(vcpu,
  215. vcpu->arch.shared->esr | ESR_PTR);
  216. #endif
  217. advance = 0;
  218. break;
  219. case 31:
  220. switch (get_xop(inst)) {
  221. case OP_31_XOP_TRAP:
  222. #ifdef CONFIG_64BIT
  223. case OP_31_XOP_TRAP_64:
  224. #endif
  225. #ifdef CONFIG_PPC_BOOK3S
  226. kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
  227. #else
  228. kvmppc_core_queue_program(vcpu,
  229. vcpu->arch.shared->esr | ESR_PTR);
  230. #endif
  231. advance = 0;
  232. break;
  233. case OP_31_XOP_LWZX:
  234. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  235. break;
  236. case OP_31_XOP_LBZX:
  237. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  238. break;
  239. case OP_31_XOP_LBZUX:
  240. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  241. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  242. break;
  243. case OP_31_XOP_STWX:
  244. emulated = kvmppc_handle_store(run, vcpu,
  245. kvmppc_get_gpr(vcpu, rs),
  246. 4, 1);
  247. break;
  248. case OP_31_XOP_STBX:
  249. emulated = kvmppc_handle_store(run, vcpu,
  250. kvmppc_get_gpr(vcpu, rs),
  251. 1, 1);
  252. break;
  253. case OP_31_XOP_STBUX:
  254. emulated = kvmppc_handle_store(run, vcpu,
  255. kvmppc_get_gpr(vcpu, rs),
  256. 1, 1);
  257. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  258. break;
  259. case OP_31_XOP_LHAX:
  260. emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
  261. break;
  262. case OP_31_XOP_LHZX:
  263. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  264. break;
  265. case OP_31_XOP_LHZUX:
  266. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  267. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  268. break;
  269. case OP_31_XOP_MFSPR:
  270. emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
  271. break;
  272. case OP_31_XOP_STHX:
  273. emulated = kvmppc_handle_store(run, vcpu,
  274. kvmppc_get_gpr(vcpu, rs),
  275. 2, 1);
  276. break;
  277. case OP_31_XOP_STHUX:
  278. emulated = kvmppc_handle_store(run, vcpu,
  279. kvmppc_get_gpr(vcpu, rs),
  280. 2, 1);
  281. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  282. break;
  283. case OP_31_XOP_MTSPR:
  284. emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
  285. break;
  286. case OP_31_XOP_DCBST:
  287. case OP_31_XOP_DCBF:
  288. case OP_31_XOP_DCBI:
  289. /* Do nothing. The guest is performing dcbi because
  290. * hardware DMA is not snooped by the dcache, but
  291. * emulated DMA either goes through the dcache as
  292. * normal writes, or the host kernel has handled dcache
  293. * coherence. */
  294. break;
  295. case OP_31_XOP_LWBRX:
  296. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
  297. break;
  298. case OP_31_XOP_TLBSYNC:
  299. break;
  300. case OP_31_XOP_STWBRX:
  301. emulated = kvmppc_handle_store(run, vcpu,
  302. kvmppc_get_gpr(vcpu, rs),
  303. 4, 0);
  304. break;
  305. case OP_31_XOP_LHBRX:
  306. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
  307. break;
  308. case OP_31_XOP_STHBRX:
  309. emulated = kvmppc_handle_store(run, vcpu,
  310. kvmppc_get_gpr(vcpu, rs),
  311. 2, 0);
  312. break;
  313. default:
  314. /* Attempt core-specific emulation below. */
  315. emulated = EMULATE_FAIL;
  316. }
  317. break;
  318. case OP_LWZ:
  319. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  320. break;
  321. /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */
  322. case OP_LD:
  323. rt = get_rt(inst);
  324. emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1);
  325. break;
  326. case OP_LWZU:
  327. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  328. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  329. break;
  330. case OP_LBZ:
  331. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  332. break;
  333. case OP_LBZU:
  334. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  335. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  336. break;
  337. case OP_STW:
  338. emulated = kvmppc_handle_store(run, vcpu,
  339. kvmppc_get_gpr(vcpu, rs),
  340. 4, 1);
  341. break;
  342. /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */
  343. case OP_STD:
  344. rs = get_rs(inst);
  345. emulated = kvmppc_handle_store(run, vcpu,
  346. kvmppc_get_gpr(vcpu, rs),
  347. 8, 1);
  348. break;
  349. case OP_STWU:
  350. emulated = kvmppc_handle_store(run, vcpu,
  351. kvmppc_get_gpr(vcpu, rs),
  352. 4, 1);
  353. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  354. break;
  355. case OP_STB:
  356. emulated = kvmppc_handle_store(run, vcpu,
  357. kvmppc_get_gpr(vcpu, rs),
  358. 1, 1);
  359. break;
  360. case OP_STBU:
  361. emulated = kvmppc_handle_store(run, vcpu,
  362. kvmppc_get_gpr(vcpu, rs),
  363. 1, 1);
  364. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  365. break;
  366. case OP_LHZ:
  367. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  368. break;
  369. case OP_LHZU:
  370. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  371. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  372. break;
  373. case OP_LHA:
  374. emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
  375. break;
  376. case OP_LHAU:
  377. emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
  378. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  379. break;
  380. case OP_STH:
  381. emulated = kvmppc_handle_store(run, vcpu,
  382. kvmppc_get_gpr(vcpu, rs),
  383. 2, 1);
  384. break;
  385. case OP_STHU:
  386. emulated = kvmppc_handle_store(run, vcpu,
  387. kvmppc_get_gpr(vcpu, rs),
  388. 2, 1);
  389. kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
  390. break;
  391. default:
  392. emulated = EMULATE_FAIL;
  393. }
  394. if (emulated == EMULATE_FAIL) {
  395. emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
  396. &advance);
  397. if (emulated == EMULATE_AGAIN) {
  398. advance = 0;
  399. } else if (emulated == EMULATE_FAIL) {
  400. advance = 0;
  401. printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
  402. "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
  403. kvmppc_core_queue_program(vcpu, 0);
  404. }
  405. }
  406. trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
  407. /* Advance past emulated instruction. */
  408. if (advance)
  409. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
  410. return emulated;
  411. }
  412. EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);