book3s_64_emulate.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 SUSE Linux Products GmbH 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #include <asm/kvm_ppc.h>
  20. #include <asm/disassemble.h>
  21. #include <asm/kvm_book3s.h>
  22. #include <asm/reg.h>
  23. #define OP_19_XOP_RFID 18
  24. #define OP_19_XOP_RFI 50
  25. #define OP_31_XOP_MFMSR 83
  26. #define OP_31_XOP_MTMSR 146
  27. #define OP_31_XOP_MTMSRD 178
  28. #define OP_31_XOP_MTSRIN 242
  29. #define OP_31_XOP_TLBIEL 274
  30. #define OP_31_XOP_TLBIE 306
  31. #define OP_31_XOP_SLBMTE 402
  32. #define OP_31_XOP_SLBIE 434
  33. #define OP_31_XOP_SLBIA 498
  34. #define OP_31_XOP_MFSRIN 659
  35. #define OP_31_XOP_SLBMFEV 851
  36. #define OP_31_XOP_EIOIO 854
  37. #define OP_31_XOP_SLBMFEE 915
  38. /* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
  39. #define OP_31_XOP_DCBZ 1010
  40. #define SPRN_GQR0 912
  41. #define SPRN_GQR1 913
  42. #define SPRN_GQR2 914
  43. #define SPRN_GQR3 915
  44. #define SPRN_GQR4 916
  45. #define SPRN_GQR5 917
  46. #define SPRN_GQR6 918
  47. #define SPRN_GQR7 919
  48. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  49. unsigned int inst, int *advance)
  50. {
  51. int emulated = EMULATE_DONE;
  52. switch (get_op(inst)) {
  53. case 19:
  54. switch (get_xop(inst)) {
  55. case OP_19_XOP_RFID:
  56. case OP_19_XOP_RFI:
  57. vcpu->arch.pc = vcpu->arch.srr0;
  58. kvmppc_set_msr(vcpu, vcpu->arch.srr1);
  59. *advance = 0;
  60. break;
  61. default:
  62. emulated = EMULATE_FAIL;
  63. break;
  64. }
  65. break;
  66. case 31:
  67. switch (get_xop(inst)) {
  68. case OP_31_XOP_MFMSR:
  69. kvmppc_set_gpr(vcpu, get_rt(inst), vcpu->arch.msr);
  70. break;
  71. case OP_31_XOP_MTMSRD:
  72. {
  73. ulong rs = kvmppc_get_gpr(vcpu, get_rs(inst));
  74. if (inst & 0x10000) {
  75. vcpu->arch.msr &= ~(MSR_RI | MSR_EE);
  76. vcpu->arch.msr |= rs & (MSR_RI | MSR_EE);
  77. } else
  78. kvmppc_set_msr(vcpu, rs);
  79. break;
  80. }
  81. case OP_31_XOP_MTMSR:
  82. kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, get_rs(inst)));
  83. break;
  84. case OP_31_XOP_MFSRIN:
  85. {
  86. int srnum;
  87. srnum = (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf;
  88. if (vcpu->arch.mmu.mfsrin) {
  89. u32 sr;
  90. sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
  91. kvmppc_set_gpr(vcpu, get_rt(inst), sr);
  92. }
  93. break;
  94. }
  95. case OP_31_XOP_MTSRIN:
  96. vcpu->arch.mmu.mtsrin(vcpu,
  97. (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
  98. kvmppc_get_gpr(vcpu, get_rs(inst)));
  99. break;
  100. case OP_31_XOP_TLBIE:
  101. case OP_31_XOP_TLBIEL:
  102. {
  103. bool large = (inst & 0x00200000) ? true : false;
  104. ulong addr = kvmppc_get_gpr(vcpu, get_rb(inst));
  105. vcpu->arch.mmu.tlbie(vcpu, addr, large);
  106. break;
  107. }
  108. case OP_31_XOP_EIOIO:
  109. break;
  110. case OP_31_XOP_SLBMTE:
  111. if (!vcpu->arch.mmu.slbmte)
  112. return EMULATE_FAIL;
  113. vcpu->arch.mmu.slbmte(vcpu,
  114. kvmppc_get_gpr(vcpu, get_rs(inst)),
  115. kvmppc_get_gpr(vcpu, get_rb(inst)));
  116. break;
  117. case OP_31_XOP_SLBIE:
  118. if (!vcpu->arch.mmu.slbie)
  119. return EMULATE_FAIL;
  120. vcpu->arch.mmu.slbie(vcpu,
  121. kvmppc_get_gpr(vcpu, get_rb(inst)));
  122. break;
  123. case OP_31_XOP_SLBIA:
  124. if (!vcpu->arch.mmu.slbia)
  125. return EMULATE_FAIL;
  126. vcpu->arch.mmu.slbia(vcpu);
  127. break;
  128. case OP_31_XOP_SLBMFEE:
  129. if (!vcpu->arch.mmu.slbmfee) {
  130. emulated = EMULATE_FAIL;
  131. } else {
  132. ulong t, rb;
  133. rb = kvmppc_get_gpr(vcpu, get_rb(inst));
  134. t = vcpu->arch.mmu.slbmfee(vcpu, rb);
  135. kvmppc_set_gpr(vcpu, get_rt(inst), t);
  136. }
  137. break;
  138. case OP_31_XOP_SLBMFEV:
  139. if (!vcpu->arch.mmu.slbmfev) {
  140. emulated = EMULATE_FAIL;
  141. } else {
  142. ulong t, rb;
  143. rb = kvmppc_get_gpr(vcpu, get_rb(inst));
  144. t = vcpu->arch.mmu.slbmfev(vcpu, rb);
  145. kvmppc_set_gpr(vcpu, get_rt(inst), t);
  146. }
  147. break;
  148. case OP_31_XOP_DCBZ:
  149. {
  150. ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
  151. ulong ra = 0;
  152. ulong addr;
  153. u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  154. if (get_ra(inst))
  155. ra = kvmppc_get_gpr(vcpu, get_ra(inst));
  156. addr = (ra + rb) & ~31ULL;
  157. if (!(vcpu->arch.msr & MSR_SF))
  158. addr &= 0xffffffff;
  159. if (kvmppc_st(vcpu, addr, 32, zeros)) {
  160. vcpu->arch.dear = addr;
  161. vcpu->arch.fault_dear = addr;
  162. to_book3s(vcpu)->dsisr = DSISR_PROTFAULT |
  163. DSISR_ISSTORE;
  164. kvmppc_book3s_queue_irqprio(vcpu,
  165. BOOK3S_INTERRUPT_DATA_STORAGE);
  166. kvmppc_mmu_pte_flush(vcpu, addr, ~0xFFFULL);
  167. }
  168. break;
  169. }
  170. default:
  171. emulated = EMULATE_FAIL;
  172. }
  173. break;
  174. default:
  175. emulated = EMULATE_FAIL;
  176. }
  177. return emulated;
  178. }
  179. void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
  180. u32 val)
  181. {
  182. if (upper) {
  183. /* Upper BAT */
  184. u32 bl = (val >> 2) & 0x7ff;
  185. bat->bepi_mask = (~bl << 17);
  186. bat->bepi = val & 0xfffe0000;
  187. bat->vs = (val & 2) ? 1 : 0;
  188. bat->vp = (val & 1) ? 1 : 0;
  189. bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
  190. } else {
  191. /* Lower BAT */
  192. bat->brpn = val & 0xfffe0000;
  193. bat->wimg = (val >> 3) & 0xf;
  194. bat->pp = val & 3;
  195. bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
  196. }
  197. }
  198. static void kvmppc_write_bat(struct kvm_vcpu *vcpu, int sprn, u32 val)
  199. {
  200. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  201. struct kvmppc_bat *bat;
  202. switch (sprn) {
  203. case SPRN_IBAT0U ... SPRN_IBAT3L:
  204. bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
  205. break;
  206. case SPRN_IBAT4U ... SPRN_IBAT7L:
  207. bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT4U) / 2];
  208. break;
  209. case SPRN_DBAT0U ... SPRN_DBAT3L:
  210. bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
  211. break;
  212. case SPRN_DBAT4U ... SPRN_DBAT7L:
  213. bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT4U) / 2];
  214. break;
  215. default:
  216. BUG();
  217. }
  218. kvmppc_set_bat(vcpu, bat, !(sprn % 2), val);
  219. }
  220. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  221. {
  222. int emulated = EMULATE_DONE;
  223. ulong spr_val = kvmppc_get_gpr(vcpu, rs);
  224. switch (sprn) {
  225. case SPRN_SDR1:
  226. to_book3s(vcpu)->sdr1 = spr_val;
  227. break;
  228. case SPRN_DSISR:
  229. to_book3s(vcpu)->dsisr = spr_val;
  230. break;
  231. case SPRN_DAR:
  232. vcpu->arch.dear = spr_val;
  233. break;
  234. case SPRN_HIOR:
  235. to_book3s(vcpu)->hior = spr_val;
  236. break;
  237. case SPRN_IBAT0U ... SPRN_IBAT3L:
  238. case SPRN_IBAT4U ... SPRN_IBAT7L:
  239. case SPRN_DBAT0U ... SPRN_DBAT3L:
  240. case SPRN_DBAT4U ... SPRN_DBAT7L:
  241. kvmppc_write_bat(vcpu, sprn, (u32)spr_val);
  242. /* BAT writes happen so rarely that we're ok to flush
  243. * everything here */
  244. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  245. break;
  246. case SPRN_HID0:
  247. to_book3s(vcpu)->hid[0] = spr_val;
  248. break;
  249. case SPRN_HID1:
  250. to_book3s(vcpu)->hid[1] = spr_val;
  251. break;
  252. case SPRN_HID2:
  253. to_book3s(vcpu)->hid[2] = spr_val;
  254. break;
  255. case SPRN_HID2_GEKKO:
  256. to_book3s(vcpu)->hid[2] = spr_val;
  257. /* HID2.PSE controls paired single on gekko */
  258. switch (vcpu->arch.pvr) {
  259. case 0x00080200: /* lonestar 2.0 */
  260. case 0x00088202: /* lonestar 2.2 */
  261. case 0x70000100: /* gekko 1.0 */
  262. case 0x00080100: /* gekko 2.0 */
  263. case 0x00083203: /* gekko 2.3a */
  264. case 0x00083213: /* gekko 2.3b */
  265. case 0x00083204: /* gekko 2.4 */
  266. case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
  267. if (spr_val & (1 << 29)) { /* HID2.PSE */
  268. vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
  269. kvmppc_giveup_ext(vcpu, MSR_FP);
  270. } else {
  271. vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
  272. }
  273. break;
  274. }
  275. break;
  276. case SPRN_HID4:
  277. case SPRN_HID4_GEKKO:
  278. to_book3s(vcpu)->hid[4] = spr_val;
  279. break;
  280. case SPRN_HID5:
  281. to_book3s(vcpu)->hid[5] = spr_val;
  282. /* guest HID5 set can change is_dcbz32 */
  283. if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
  284. (mfmsr() & MSR_HV))
  285. vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
  286. break;
  287. case SPRN_GQR0:
  288. case SPRN_GQR1:
  289. case SPRN_GQR2:
  290. case SPRN_GQR3:
  291. case SPRN_GQR4:
  292. case SPRN_GQR5:
  293. case SPRN_GQR6:
  294. case SPRN_GQR7:
  295. to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
  296. break;
  297. case SPRN_ICTC:
  298. case SPRN_THRM1:
  299. case SPRN_THRM2:
  300. case SPRN_THRM3:
  301. case SPRN_CTRLF:
  302. case SPRN_CTRLT:
  303. case SPRN_L2CR:
  304. case SPRN_MMCR0_GEKKO:
  305. case SPRN_MMCR1_GEKKO:
  306. case SPRN_PMC1_GEKKO:
  307. case SPRN_PMC2_GEKKO:
  308. case SPRN_PMC3_GEKKO:
  309. case SPRN_PMC4_GEKKO:
  310. case SPRN_WPAR_GEKKO:
  311. break;
  312. default:
  313. printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
  314. #ifndef DEBUG_SPR
  315. emulated = EMULATE_FAIL;
  316. #endif
  317. break;
  318. }
  319. return emulated;
  320. }
  321. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  322. {
  323. int emulated = EMULATE_DONE;
  324. switch (sprn) {
  325. case SPRN_SDR1:
  326. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->sdr1);
  327. break;
  328. case SPRN_DSISR:
  329. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->dsisr);
  330. break;
  331. case SPRN_DAR:
  332. kvmppc_set_gpr(vcpu, rt, vcpu->arch.dear);
  333. break;
  334. case SPRN_HIOR:
  335. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hior);
  336. break;
  337. case SPRN_HID0:
  338. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[0]);
  339. break;
  340. case SPRN_HID1:
  341. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[1]);
  342. break;
  343. case SPRN_HID2:
  344. case SPRN_HID2_GEKKO:
  345. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[2]);
  346. break;
  347. case SPRN_HID4:
  348. case SPRN_HID4_GEKKO:
  349. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[4]);
  350. break;
  351. case SPRN_HID5:
  352. kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[5]);
  353. break;
  354. case SPRN_GQR0:
  355. case SPRN_GQR1:
  356. case SPRN_GQR2:
  357. case SPRN_GQR3:
  358. case SPRN_GQR4:
  359. case SPRN_GQR5:
  360. case SPRN_GQR6:
  361. case SPRN_GQR7:
  362. kvmppc_set_gpr(vcpu, rt,
  363. to_book3s(vcpu)->gqr[sprn - SPRN_GQR0]);
  364. break;
  365. case SPRN_THRM1:
  366. case SPRN_THRM2:
  367. case SPRN_THRM3:
  368. case SPRN_CTRLF:
  369. case SPRN_CTRLT:
  370. case SPRN_L2CR:
  371. case SPRN_MMCR0_GEKKO:
  372. case SPRN_MMCR1_GEKKO:
  373. case SPRN_PMC1_GEKKO:
  374. case SPRN_PMC2_GEKKO:
  375. case SPRN_PMC3_GEKKO:
  376. case SPRN_PMC4_GEKKO:
  377. case SPRN_WPAR_GEKKO:
  378. kvmppc_set_gpr(vcpu, rt, 0);
  379. break;
  380. default:
  381. printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
  382. #ifndef DEBUG_SPR
  383. emulated = EMULATE_FAIL;
  384. #endif
  385. break;
  386. }
  387. return emulated;
  388. }