emulate.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Should probably auto-generate instruction decoding for a particular core
  182. * from opcode tables in the future. */
  183. int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
  184. {
  185. u32 inst;
  186. int rs, rt, sprn;
  187. enum emulation_result emulated;
  188. int advance = 1;
  189. /* this default type might be overwritten by subcategories */
  190. kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
  191. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
  192. if (emulated != EMULATE_DONE)
  193. return emulated;
  194. pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
  195. rs = get_rs(inst);
  196. rt = get_rt(inst);
  197. sprn = get_sprn(inst);
  198. switch (get_op(inst)) {
  199. case OP_TRAP:
  200. #ifdef CONFIG_PPC_BOOK3S
  201. case OP_TRAP_64:
  202. kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
  203. #else
  204. kvmppc_core_queue_program(vcpu,
  205. vcpu->arch.shared->esr | ESR_PTR);
  206. #endif
  207. advance = 0;
  208. break;
  209. case 31:
  210. switch (get_xop(inst)) {
  211. case OP_31_XOP_TRAP:
  212. #ifdef CONFIG_64BIT
  213. case OP_31_XOP_TRAP_64:
  214. #endif
  215. #ifdef CONFIG_PPC_BOOK3S
  216. kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
  217. #else
  218. kvmppc_core_queue_program(vcpu,
  219. vcpu->arch.shared->esr | ESR_PTR);
  220. #endif
  221. advance = 0;
  222. break;
  223. case OP_31_XOP_MFSPR:
  224. emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
  225. break;
  226. case OP_31_XOP_MTSPR:
  227. emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
  228. break;
  229. case OP_31_XOP_TLBSYNC:
  230. break;
  231. default:
  232. /* Attempt core-specific emulation below. */
  233. emulated = EMULATE_FAIL;
  234. }
  235. break;
  236. case 0:
  237. /*
  238. * Instruction with primary opcode 0. Based on PowerISA
  239. * these are illegal instructions.
  240. */
  241. if (inst == KVMPPC_INST_SW_BREAKPOINT) {
  242. run->exit_reason = KVM_EXIT_DEBUG;
  243. run->debug.arch.address = kvmppc_get_pc(vcpu);
  244. emulated = EMULATE_EXIT_USER;
  245. advance = 0;
  246. } else
  247. emulated = EMULATE_FAIL;
  248. break;
  249. default:
  250. emulated = EMULATE_FAIL;
  251. }
  252. if (emulated == EMULATE_FAIL) {
  253. emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
  254. &advance);
  255. if (emulated == EMULATE_AGAIN) {
  256. advance = 0;
  257. } else if (emulated == EMULATE_FAIL) {
  258. advance = 0;
  259. printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
  260. "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
  261. }
  262. }
  263. trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
  264. /* Advance past emulated instruction. */
  265. if (advance)
  266. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
  267. return emulated;
  268. }
  269. EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);