emulate.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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: %lx\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. #endif
  44. #ifdef CONFIG_BOOKE
  45. /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
  46. if (vcpu->arch.dec == 0)
  47. return;
  48. #endif
  49. /*
  50. * The decrementer ticks at the same rate as the timebase, so
  51. * that's how we convert the guest DEC value to the number of
  52. * host ticks.
  53. */
  54. dec_time = vcpu->arch.dec;
  55. /*
  56. * Guest timebase ticks at the same frequency as host timebase.
  57. * So use the host timebase calculations for decrementer emulation.
  58. */
  59. dec_time = tb_to_ns(dec_time);
  60. dec_nsec = do_div(dec_time, NSEC_PER_SEC);
  61. hrtimer_start(&vcpu->arch.dec_timer,
  62. ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
  63. vcpu->arch.dec_jiffies = get_tb();
  64. }
  65. u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
  66. {
  67. u64 jd = tb - vcpu->arch.dec_jiffies;
  68. #ifdef CONFIG_BOOKE
  69. if (vcpu->arch.dec < jd)
  70. return 0;
  71. #endif
  72. return vcpu->arch.dec - jd;
  73. }
  74. static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  75. {
  76. enum emulation_result emulated = EMULATE_DONE;
  77. ulong spr_val = kvmppc_get_gpr(vcpu, rs);
  78. switch (sprn) {
  79. case SPRN_SRR0:
  80. kvmppc_set_srr0(vcpu, spr_val);
  81. break;
  82. case SPRN_SRR1:
  83. kvmppc_set_srr1(vcpu, spr_val);
  84. break;
  85. /* XXX We need to context-switch the timebase for
  86. * watchdog and FIT. */
  87. case SPRN_TBWL: break;
  88. case SPRN_TBWU: break;
  89. case SPRN_DEC:
  90. vcpu->arch.dec = (u32) spr_val;
  91. kvmppc_emulate_dec(vcpu);
  92. break;
  93. case SPRN_SPRG0:
  94. kvmppc_set_sprg0(vcpu, spr_val);
  95. break;
  96. case SPRN_SPRG1:
  97. kvmppc_set_sprg1(vcpu, spr_val);
  98. break;
  99. case SPRN_SPRG2:
  100. kvmppc_set_sprg2(vcpu, spr_val);
  101. break;
  102. case SPRN_SPRG3:
  103. kvmppc_set_sprg3(vcpu, spr_val);
  104. break;
  105. /* PIR can legally be written, but we ignore it */
  106. case SPRN_PIR: break;
  107. default:
  108. emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
  109. spr_val);
  110. if (emulated == EMULATE_FAIL)
  111. printk(KERN_INFO "mtspr: unknown spr "
  112. "0x%x\n", sprn);
  113. break;
  114. }
  115. kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
  116. return emulated;
  117. }
  118. static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  119. {
  120. enum emulation_result emulated = EMULATE_DONE;
  121. ulong spr_val = 0;
  122. switch (sprn) {
  123. case SPRN_SRR0:
  124. spr_val = kvmppc_get_srr0(vcpu);
  125. break;
  126. case SPRN_SRR1:
  127. spr_val = kvmppc_get_srr1(vcpu);
  128. break;
  129. case SPRN_PVR:
  130. spr_val = vcpu->arch.pvr;
  131. break;
  132. case SPRN_PIR:
  133. spr_val = vcpu->vcpu_id;
  134. break;
  135. /* Note: mftb and TBRL/TBWL are user-accessible, so
  136. * the guest can always access the real TB anyways.
  137. * In fact, we probably will never see these traps. */
  138. case SPRN_TBWL:
  139. spr_val = get_tb() >> 32;
  140. break;
  141. case SPRN_TBWU:
  142. spr_val = get_tb();
  143. break;
  144. case SPRN_SPRG0:
  145. spr_val = kvmppc_get_sprg0(vcpu);
  146. break;
  147. case SPRN_SPRG1:
  148. spr_val = kvmppc_get_sprg1(vcpu);
  149. break;
  150. case SPRN_SPRG2:
  151. spr_val = kvmppc_get_sprg2(vcpu);
  152. break;
  153. case SPRN_SPRG3:
  154. spr_val = kvmppc_get_sprg3(vcpu);
  155. break;
  156. /* Note: SPRG4-7 are user-readable, so we don't get
  157. * a trap. */
  158. case SPRN_DEC:
  159. spr_val = kvmppc_get_dec(vcpu, get_tb());
  160. break;
  161. default:
  162. emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
  163. &spr_val);
  164. if (unlikely(emulated == EMULATE_FAIL)) {
  165. printk(KERN_INFO "mfspr: unknown spr "
  166. "0x%x\n", sprn);
  167. }
  168. break;
  169. }
  170. if (emulated == EMULATE_DONE)
  171. kvmppc_set_gpr(vcpu, rt, spr_val);
  172. kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
  173. return emulated;
  174. }
  175. /* XXX Should probably auto-generate instruction decoding for a particular core
  176. * from opcode tables in the future. */
  177. int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
  178. {
  179. u32 inst;
  180. int rs, rt, sprn;
  181. enum emulation_result emulated;
  182. int advance = 1;
  183. /* this default type might be overwritten by subcategories */
  184. kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
  185. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
  186. if (emulated != EMULATE_DONE)
  187. return emulated;
  188. pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
  189. rs = get_rs(inst);
  190. rt = get_rt(inst);
  191. sprn = get_sprn(inst);
  192. switch (get_op(inst)) {
  193. case OP_TRAP:
  194. #ifdef CONFIG_PPC_BOOK3S
  195. case OP_TRAP_64:
  196. kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
  197. #else
  198. kvmppc_core_queue_program(vcpu,
  199. vcpu->arch.shared->esr | ESR_PTR);
  200. #endif
  201. advance = 0;
  202. break;
  203. case 31:
  204. switch (get_xop(inst)) {
  205. case OP_31_XOP_TRAP:
  206. #ifdef CONFIG_64BIT
  207. case OP_31_XOP_TRAP_64:
  208. #endif
  209. #ifdef CONFIG_PPC_BOOK3S
  210. kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
  211. #else
  212. kvmppc_core_queue_program(vcpu,
  213. vcpu->arch.shared->esr | ESR_PTR);
  214. #endif
  215. advance = 0;
  216. break;
  217. case OP_31_XOP_MFSPR:
  218. emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
  219. if (emulated == EMULATE_AGAIN) {
  220. emulated = EMULATE_DONE;
  221. advance = 0;
  222. }
  223. break;
  224. case OP_31_XOP_MTSPR:
  225. emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
  226. if (emulated == EMULATE_AGAIN) {
  227. emulated = EMULATE_DONE;
  228. advance = 0;
  229. }
  230. break;
  231. case OP_31_XOP_TLBSYNC:
  232. break;
  233. default:
  234. /* Attempt core-specific emulation below. */
  235. emulated = EMULATE_FAIL;
  236. }
  237. break;
  238. case 0:
  239. /*
  240. * Instruction with primary opcode 0. Based on PowerISA
  241. * these are illegal instructions.
  242. */
  243. if (inst == KVMPPC_INST_SW_BREAKPOINT) {
  244. run->exit_reason = KVM_EXIT_DEBUG;
  245. run->debug.arch.address = kvmppc_get_pc(vcpu);
  246. emulated = EMULATE_EXIT_USER;
  247. advance = 0;
  248. } else
  249. emulated = EMULATE_FAIL;
  250. break;
  251. default:
  252. emulated = EMULATE_FAIL;
  253. }
  254. if (emulated == EMULATE_FAIL) {
  255. emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
  256. &advance);
  257. if (emulated == EMULATE_AGAIN) {
  258. advance = 0;
  259. } else if (emulated == EMULATE_FAIL) {
  260. advance = 0;
  261. printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
  262. "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
  263. }
  264. }
  265. trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
  266. /* Advance past emulated instruction. */
  267. if (advance)
  268. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
  269. return emulated;
  270. }
  271. EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);