booke_emulate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. 2008
  16. * Copyright 2011 Freescale Semiconductor, Inc.
  17. *
  18. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <asm/disassemble.h>
  22. #include "booke.h"
  23. #define OP_19_XOP_RFI 50
  24. #define OP_19_XOP_RFCI 51
  25. #define OP_19_XOP_RFDI 39
  26. #define OP_31_XOP_MFMSR 83
  27. #define OP_31_XOP_WRTEE 131
  28. #define OP_31_XOP_MTMSR 146
  29. #define OP_31_XOP_WRTEEI 163
  30. static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
  31. {
  32. vcpu->arch.pc = vcpu->arch.shared->srr0;
  33. kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
  34. }
  35. static void kvmppc_emul_rfdi(struct kvm_vcpu *vcpu)
  36. {
  37. vcpu->arch.pc = vcpu->arch.dsrr0;
  38. kvmppc_set_msr(vcpu, vcpu->arch.dsrr1);
  39. }
  40. static void kvmppc_emul_rfci(struct kvm_vcpu *vcpu)
  41. {
  42. vcpu->arch.pc = vcpu->arch.csrr0;
  43. kvmppc_set_msr(vcpu, vcpu->arch.csrr1);
  44. }
  45. int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  46. unsigned int inst, int *advance)
  47. {
  48. int emulated = EMULATE_DONE;
  49. int rs = get_rs(inst);
  50. int rt = get_rt(inst);
  51. switch (get_op(inst)) {
  52. case 19:
  53. switch (get_xop(inst)) {
  54. case OP_19_XOP_RFI:
  55. kvmppc_emul_rfi(vcpu);
  56. kvmppc_set_exit_type(vcpu, EMULATED_RFI_EXITS);
  57. *advance = 0;
  58. break;
  59. case OP_19_XOP_RFCI:
  60. kvmppc_emul_rfci(vcpu);
  61. kvmppc_set_exit_type(vcpu, EMULATED_RFCI_EXITS);
  62. *advance = 0;
  63. break;
  64. case OP_19_XOP_RFDI:
  65. kvmppc_emul_rfdi(vcpu);
  66. kvmppc_set_exit_type(vcpu, EMULATED_RFDI_EXITS);
  67. *advance = 0;
  68. break;
  69. default:
  70. emulated = EMULATE_FAIL;
  71. break;
  72. }
  73. break;
  74. case 31:
  75. switch (get_xop(inst)) {
  76. case OP_31_XOP_MFMSR:
  77. kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
  78. kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
  79. break;
  80. case OP_31_XOP_MTMSR:
  81. kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
  82. kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
  83. break;
  84. case OP_31_XOP_WRTEE:
  85. vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
  86. | (kvmppc_get_gpr(vcpu, rs) & MSR_EE);
  87. kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
  88. break;
  89. case OP_31_XOP_WRTEEI:
  90. vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
  91. | (inst & MSR_EE);
  92. kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
  93. break;
  94. default:
  95. emulated = EMULATE_FAIL;
  96. }
  97. break;
  98. default:
  99. emulated = EMULATE_FAIL;
  100. }
  101. return emulated;
  102. }
  103. /*
  104. * NOTE: some of these registers are not emulated on BOOKE_HV (GS-mode).
  105. * Their backing store is in real registers, and these functions
  106. * will return the wrong result if called for them in another context
  107. * (such as debugging).
  108. */
  109. int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  110. {
  111. int emulated = EMULATE_DONE;
  112. bool debug_inst = false;
  113. switch (sprn) {
  114. case SPRN_DEAR:
  115. vcpu->arch.shared->dar = spr_val;
  116. break;
  117. case SPRN_ESR:
  118. vcpu->arch.shared->esr = spr_val;
  119. break;
  120. case SPRN_CSRR0:
  121. vcpu->arch.csrr0 = spr_val;
  122. break;
  123. case SPRN_CSRR1:
  124. vcpu->arch.csrr1 = spr_val;
  125. break;
  126. case SPRN_DSRR0:
  127. vcpu->arch.dsrr0 = spr_val;
  128. break;
  129. case SPRN_DSRR1:
  130. vcpu->arch.dsrr1 = spr_val;
  131. break;
  132. case SPRN_IAC1:
  133. /*
  134. * If userspace is debugging guest then guest
  135. * can not access debug registers.
  136. */
  137. if (vcpu->guest_debug)
  138. break;
  139. debug_inst = true;
  140. vcpu->arch.dbg_reg.iac1 = spr_val;
  141. break;
  142. case SPRN_IAC2:
  143. /*
  144. * If userspace is debugging guest then guest
  145. * can not access debug registers.
  146. */
  147. if (vcpu->guest_debug)
  148. break;
  149. debug_inst = true;
  150. vcpu->arch.dbg_reg.iac2 = spr_val;
  151. break;
  152. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  153. case SPRN_IAC3:
  154. /*
  155. * If userspace is debugging guest then guest
  156. * can not access debug registers.
  157. */
  158. if (vcpu->guest_debug)
  159. break;
  160. debug_inst = true;
  161. vcpu->arch.dbg_reg.iac3 = spr_val;
  162. break;
  163. case SPRN_IAC4:
  164. /*
  165. * If userspace is debugging guest then guest
  166. * can not access debug registers.
  167. */
  168. if (vcpu->guest_debug)
  169. break;
  170. debug_inst = true;
  171. vcpu->arch.dbg_reg.iac4 = spr_val;
  172. break;
  173. #endif
  174. case SPRN_DAC1:
  175. /*
  176. * If userspace is debugging guest then guest
  177. * can not access debug registers.
  178. */
  179. if (vcpu->guest_debug)
  180. break;
  181. debug_inst = true;
  182. vcpu->arch.dbg_reg.dac1 = spr_val;
  183. break;
  184. case SPRN_DAC2:
  185. /*
  186. * If userspace is debugging guest then guest
  187. * can not access debug registers.
  188. */
  189. if (vcpu->guest_debug)
  190. break;
  191. debug_inst = true;
  192. vcpu->arch.dbg_reg.dac2 = spr_val;
  193. break;
  194. case SPRN_DBCR0:
  195. /*
  196. * If userspace is debugging guest then guest
  197. * can not access debug registers.
  198. */
  199. if (vcpu->guest_debug)
  200. break;
  201. debug_inst = true;
  202. spr_val &= (DBCR0_IDM | DBCR0_IC | DBCR0_BT | DBCR0_TIE |
  203. DBCR0_IAC1 | DBCR0_IAC2 | DBCR0_IAC3 | DBCR0_IAC4 |
  204. DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W);
  205. vcpu->arch.dbg_reg.dbcr0 = spr_val;
  206. break;
  207. case SPRN_DBCR1:
  208. /*
  209. * If userspace is debugging guest then guest
  210. * can not access debug registers.
  211. */
  212. if (vcpu->guest_debug)
  213. break;
  214. debug_inst = true;
  215. vcpu->arch.dbg_reg.dbcr1 = spr_val;
  216. break;
  217. case SPRN_DBCR2:
  218. /*
  219. * If userspace is debugging guest then guest
  220. * can not access debug registers.
  221. */
  222. if (vcpu->guest_debug)
  223. break;
  224. debug_inst = true;
  225. vcpu->arch.dbg_reg.dbcr2 = spr_val;
  226. break;
  227. case SPRN_DBSR:
  228. /*
  229. * If userspace is debugging guest then guest
  230. * can not access debug registers.
  231. */
  232. if (vcpu->guest_debug)
  233. break;
  234. vcpu->arch.dbsr &= ~spr_val;
  235. if (!(vcpu->arch.dbsr & ~DBSR_IDE))
  236. kvmppc_core_dequeue_debug(vcpu);
  237. break;
  238. case SPRN_TSR:
  239. kvmppc_clr_tsr_bits(vcpu, spr_val);
  240. break;
  241. case SPRN_TCR:
  242. /*
  243. * WRC is a 2-bit field that is supposed to preserve its
  244. * value once written to non-zero.
  245. */
  246. if (vcpu->arch.tcr & TCR_WRC_MASK) {
  247. spr_val &= ~TCR_WRC_MASK;
  248. spr_val |= vcpu->arch.tcr & TCR_WRC_MASK;
  249. }
  250. kvmppc_set_tcr(vcpu, spr_val);
  251. break;
  252. case SPRN_DECAR:
  253. vcpu->arch.decar = spr_val;
  254. break;
  255. /*
  256. * Note: SPRG4-7 are user-readable.
  257. * These values are loaded into the real SPRGs when resuming the
  258. * guest (PR-mode only).
  259. */
  260. case SPRN_SPRG4:
  261. kvmppc_set_sprg4(vcpu, spr_val);
  262. break;
  263. case SPRN_SPRG5:
  264. kvmppc_set_sprg5(vcpu, spr_val);
  265. break;
  266. case SPRN_SPRG6:
  267. kvmppc_set_sprg6(vcpu, spr_val);
  268. break;
  269. case SPRN_SPRG7:
  270. kvmppc_set_sprg7(vcpu, spr_val);
  271. break;
  272. case SPRN_IVPR:
  273. vcpu->arch.ivpr = spr_val;
  274. #ifdef CONFIG_KVM_BOOKE_HV
  275. mtspr(SPRN_GIVPR, spr_val);
  276. #endif
  277. break;
  278. case SPRN_IVOR0:
  279. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = spr_val;
  280. break;
  281. case SPRN_IVOR1:
  282. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = spr_val;
  283. break;
  284. case SPRN_IVOR2:
  285. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = spr_val;
  286. #ifdef CONFIG_KVM_BOOKE_HV
  287. mtspr(SPRN_GIVOR2, spr_val);
  288. #endif
  289. break;
  290. case SPRN_IVOR3:
  291. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = spr_val;
  292. break;
  293. case SPRN_IVOR4:
  294. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = spr_val;
  295. break;
  296. case SPRN_IVOR5:
  297. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = spr_val;
  298. break;
  299. case SPRN_IVOR6:
  300. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = spr_val;
  301. break;
  302. case SPRN_IVOR7:
  303. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = spr_val;
  304. break;
  305. case SPRN_IVOR8:
  306. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = spr_val;
  307. #ifdef CONFIG_KVM_BOOKE_HV
  308. mtspr(SPRN_GIVOR8, spr_val);
  309. #endif
  310. break;
  311. case SPRN_IVOR9:
  312. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = spr_val;
  313. break;
  314. case SPRN_IVOR10:
  315. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = spr_val;
  316. break;
  317. case SPRN_IVOR11:
  318. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = spr_val;
  319. break;
  320. case SPRN_IVOR12:
  321. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = spr_val;
  322. break;
  323. case SPRN_IVOR13:
  324. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = spr_val;
  325. break;
  326. case SPRN_IVOR14:
  327. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = spr_val;
  328. break;
  329. case SPRN_IVOR15:
  330. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = spr_val;
  331. break;
  332. case SPRN_MCSR:
  333. vcpu->arch.mcsr &= ~spr_val;
  334. break;
  335. #if defined(CONFIG_64BIT)
  336. case SPRN_EPCR:
  337. kvmppc_set_epcr(vcpu, spr_val);
  338. #ifdef CONFIG_KVM_BOOKE_HV
  339. mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
  340. #endif
  341. break;
  342. #endif
  343. default:
  344. emulated = EMULATE_FAIL;
  345. }
  346. if (debug_inst) {
  347. current->thread.debug = vcpu->arch.dbg_reg;
  348. switch_booke_debug_regs(&vcpu->arch.dbg_reg);
  349. }
  350. return emulated;
  351. }
  352. int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  353. {
  354. int emulated = EMULATE_DONE;
  355. switch (sprn) {
  356. case SPRN_IVPR:
  357. *spr_val = vcpu->arch.ivpr;
  358. break;
  359. case SPRN_DEAR:
  360. *spr_val = vcpu->arch.shared->dar;
  361. break;
  362. case SPRN_ESR:
  363. *spr_val = vcpu->arch.shared->esr;
  364. break;
  365. case SPRN_EPR:
  366. *spr_val = vcpu->arch.epr;
  367. break;
  368. case SPRN_CSRR0:
  369. *spr_val = vcpu->arch.csrr0;
  370. break;
  371. case SPRN_CSRR1:
  372. *spr_val = vcpu->arch.csrr1;
  373. break;
  374. case SPRN_DSRR0:
  375. *spr_val = vcpu->arch.dsrr0;
  376. break;
  377. case SPRN_DSRR1:
  378. *spr_val = vcpu->arch.dsrr1;
  379. break;
  380. case SPRN_IAC1:
  381. *spr_val = vcpu->arch.dbg_reg.iac1;
  382. break;
  383. case SPRN_IAC2:
  384. *spr_val = vcpu->arch.dbg_reg.iac2;
  385. break;
  386. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  387. case SPRN_IAC3:
  388. *spr_val = vcpu->arch.dbg_reg.iac3;
  389. break;
  390. case SPRN_IAC4:
  391. *spr_val = vcpu->arch.dbg_reg.iac4;
  392. break;
  393. #endif
  394. case SPRN_DAC1:
  395. *spr_val = vcpu->arch.dbg_reg.dac1;
  396. break;
  397. case SPRN_DAC2:
  398. *spr_val = vcpu->arch.dbg_reg.dac2;
  399. break;
  400. case SPRN_DBCR0:
  401. *spr_val = vcpu->arch.dbg_reg.dbcr0;
  402. if (vcpu->guest_debug)
  403. *spr_val = *spr_val | DBCR0_EDM;
  404. break;
  405. case SPRN_DBCR1:
  406. *spr_val = vcpu->arch.dbg_reg.dbcr1;
  407. break;
  408. case SPRN_DBCR2:
  409. *spr_val = vcpu->arch.dbg_reg.dbcr2;
  410. break;
  411. case SPRN_DBSR:
  412. *spr_val = vcpu->arch.dbsr;
  413. break;
  414. case SPRN_TSR:
  415. *spr_val = vcpu->arch.tsr;
  416. break;
  417. case SPRN_TCR:
  418. *spr_val = vcpu->arch.tcr;
  419. break;
  420. case SPRN_IVOR0:
  421. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  422. break;
  423. case SPRN_IVOR1:
  424. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  425. break;
  426. case SPRN_IVOR2:
  427. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  428. break;
  429. case SPRN_IVOR3:
  430. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  431. break;
  432. case SPRN_IVOR4:
  433. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  434. break;
  435. case SPRN_IVOR5:
  436. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  437. break;
  438. case SPRN_IVOR6:
  439. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  440. break;
  441. case SPRN_IVOR7:
  442. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  443. break;
  444. case SPRN_IVOR8:
  445. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  446. break;
  447. case SPRN_IVOR9:
  448. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  449. break;
  450. case SPRN_IVOR10:
  451. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  452. break;
  453. case SPRN_IVOR11:
  454. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  455. break;
  456. case SPRN_IVOR12:
  457. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  458. break;
  459. case SPRN_IVOR13:
  460. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  461. break;
  462. case SPRN_IVOR14:
  463. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  464. break;
  465. case SPRN_IVOR15:
  466. *spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  467. break;
  468. case SPRN_MCSR:
  469. *spr_val = vcpu->arch.mcsr;
  470. break;
  471. #if defined(CONFIG_64BIT)
  472. case SPRN_EPCR:
  473. *spr_val = vcpu->arch.epcr;
  474. break;
  475. #endif
  476. default:
  477. emulated = EMULATE_FAIL;
  478. }
  479. return emulated;
  480. }