emulate.c 7.4 KB

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