emulate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #include <linux/jiffies.h>
  20. #include <linux/timer.h>
  21. #include <linux/types.h>
  22. #include <linux/string.h>
  23. #include <linux/kvm_host.h>
  24. #include <asm/dcr.h>
  25. #include <asm/dcr-regs.h>
  26. #include <asm/time.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/kvm_ppc.h>
  29. #include "44x_tlb.h"
  30. /* Instruction decoding */
  31. static inline unsigned int get_op(u32 inst)
  32. {
  33. return inst >> 26;
  34. }
  35. static inline unsigned int get_xop(u32 inst)
  36. {
  37. return (inst >> 1) & 0x3ff;
  38. }
  39. static inline unsigned int get_sprn(u32 inst)
  40. {
  41. return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0);
  42. }
  43. static inline unsigned int get_dcrn(u32 inst)
  44. {
  45. return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0);
  46. }
  47. static inline unsigned int get_rt(u32 inst)
  48. {
  49. return (inst >> 21) & 0x1f;
  50. }
  51. static inline unsigned int get_rs(u32 inst)
  52. {
  53. return (inst >> 21) & 0x1f;
  54. }
  55. static inline unsigned int get_ra(u32 inst)
  56. {
  57. return (inst >> 16) & 0x1f;
  58. }
  59. static inline unsigned int get_rb(u32 inst)
  60. {
  61. return (inst >> 11) & 0x1f;
  62. }
  63. static inline unsigned int get_rc(u32 inst)
  64. {
  65. return inst & 0x1;
  66. }
  67. static inline unsigned int get_ws(u32 inst)
  68. {
  69. return (inst >> 11) & 0x1f;
  70. }
  71. static inline unsigned int get_d(u32 inst)
  72. {
  73. return inst & 0xffff;
  74. }
  75. static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
  76. const struct tlbe *tlbe)
  77. {
  78. gpa_t gpa;
  79. if (!get_tlb_v(tlbe))
  80. return 0;
  81. /* Does it match current guest AS? */
  82. /* XXX what about IS != DS? */
  83. if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
  84. return 0;
  85. gpa = get_tlb_raddr(tlbe);
  86. if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
  87. /* Mapping is not for RAM. */
  88. return 0;
  89. return 1;
  90. }
  91. static int kvmppc_emul_tlbwe(struct kvm_vcpu *vcpu, u32 inst)
  92. {
  93. u64 eaddr;
  94. u64 raddr;
  95. u64 asid;
  96. u32 flags;
  97. struct tlbe *tlbe;
  98. unsigned int ra;
  99. unsigned int rs;
  100. unsigned int ws;
  101. unsigned int index;
  102. ra = get_ra(inst);
  103. rs = get_rs(inst);
  104. ws = get_ws(inst);
  105. index = vcpu->arch.gpr[ra];
  106. if (index > PPC44x_TLB_SIZE) {
  107. printk("%s: index %d\n", __func__, index);
  108. kvmppc_dump_vcpu(vcpu);
  109. return EMULATE_FAIL;
  110. }
  111. tlbe = &vcpu->arch.guest_tlb[index];
  112. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  113. if (tlbe->word0 & PPC44x_TLB_VALID) {
  114. eaddr = get_tlb_eaddr(tlbe);
  115. asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
  116. kvmppc_mmu_invalidate(vcpu, eaddr, get_tlb_end(tlbe), asid);
  117. }
  118. switch (ws) {
  119. case PPC44x_TLB_PAGEID:
  120. tlbe->tid = vcpu->arch.mmucr & 0xff;
  121. tlbe->word0 = vcpu->arch.gpr[rs];
  122. break;
  123. case PPC44x_TLB_XLAT:
  124. tlbe->word1 = vcpu->arch.gpr[rs];
  125. break;
  126. case PPC44x_TLB_ATTRIB:
  127. tlbe->word2 = vcpu->arch.gpr[rs];
  128. break;
  129. default:
  130. return EMULATE_FAIL;
  131. }
  132. if (tlbe_is_host_safe(vcpu, tlbe)) {
  133. eaddr = get_tlb_eaddr(tlbe);
  134. raddr = get_tlb_raddr(tlbe);
  135. asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
  136. flags = tlbe->word2 & 0xffff;
  137. /* Create a 4KB mapping on the host. If the guest wanted a
  138. * large page, only the first 4KB is mapped here and the rest
  139. * are mapped on the fly. */
  140. kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags);
  141. }
  142. return EMULATE_DONE;
  143. }
  144. static void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
  145. {
  146. if (vcpu->arch.tcr & TCR_DIE) {
  147. /* The decrementer ticks at the same rate as the timebase, so
  148. * that's how we convert the guest DEC value to the number of
  149. * host ticks. */
  150. unsigned long nr_jiffies;
  151. nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
  152. mod_timer(&vcpu->arch.dec_timer,
  153. get_jiffies_64() + nr_jiffies);
  154. } else {
  155. del_timer(&vcpu->arch.dec_timer);
  156. }
  157. }
  158. static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
  159. {
  160. vcpu->arch.pc = vcpu->arch.srr0;
  161. kvmppc_set_msr(vcpu, vcpu->arch.srr1);
  162. }
  163. /* XXX to do:
  164. * lhax
  165. * lhaux
  166. * lswx
  167. * lswi
  168. * stswx
  169. * stswi
  170. * lha
  171. * lhau
  172. * lmw
  173. * stmw
  174. *
  175. * XXX is_bigendian should depend on MMU mapping or MSR[LE]
  176. */
  177. int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
  178. {
  179. u32 inst = vcpu->arch.last_inst;
  180. u32 ea;
  181. int ra;
  182. int rb;
  183. int rc;
  184. int rs;
  185. int rt;
  186. int sprn;
  187. int dcrn;
  188. enum emulation_result emulated = EMULATE_DONE;
  189. int advance = 1;
  190. switch (get_op(inst)) {
  191. case 3: /* trap */
  192. printk("trap!\n");
  193. kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
  194. advance = 0;
  195. break;
  196. case 19:
  197. switch (get_xop(inst)) {
  198. case 50: /* rfi */
  199. kvmppc_emul_rfi(vcpu);
  200. advance = 0;
  201. break;
  202. default:
  203. emulated = EMULATE_FAIL;
  204. break;
  205. }
  206. break;
  207. case 31:
  208. switch (get_xop(inst)) {
  209. case 23: /* lwzx */
  210. rt = get_rt(inst);
  211. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  212. break;
  213. case 83: /* mfmsr */
  214. rt = get_rt(inst);
  215. vcpu->arch.gpr[rt] = vcpu->arch.msr;
  216. break;
  217. case 87: /* lbzx */
  218. rt = get_rt(inst);
  219. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  220. break;
  221. case 131: /* wrtee */
  222. rs = get_rs(inst);
  223. vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
  224. | (vcpu->arch.gpr[rs] & MSR_EE);
  225. break;
  226. case 146: /* mtmsr */
  227. rs = get_rs(inst);
  228. kvmppc_set_msr(vcpu, vcpu->arch.gpr[rs]);
  229. break;
  230. case 151: /* stwx */
  231. rs = get_rs(inst);
  232. emulated = kvmppc_handle_store(run, vcpu,
  233. vcpu->arch.gpr[rs],
  234. 4, 1);
  235. break;
  236. case 163: /* wrteei */
  237. vcpu->arch.msr = (vcpu->arch.msr & ~MSR_EE)
  238. | (inst & MSR_EE);
  239. break;
  240. case 215: /* stbx */
  241. rs = get_rs(inst);
  242. emulated = kvmppc_handle_store(run, vcpu,
  243. vcpu->arch.gpr[rs],
  244. 1, 1);
  245. break;
  246. case 247: /* stbux */
  247. rs = get_rs(inst);
  248. ra = get_ra(inst);
  249. rb = get_rb(inst);
  250. ea = vcpu->arch.gpr[rb];
  251. if (ra)
  252. ea += vcpu->arch.gpr[ra];
  253. emulated = kvmppc_handle_store(run, vcpu,
  254. vcpu->arch.gpr[rs],
  255. 1, 1);
  256. vcpu->arch.gpr[rs] = ea;
  257. break;
  258. case 279: /* lhzx */
  259. rt = get_rt(inst);
  260. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  261. break;
  262. case 311: /* lhzux */
  263. rt = get_rt(inst);
  264. ra = get_ra(inst);
  265. rb = get_rb(inst);
  266. ea = vcpu->arch.gpr[rb];
  267. if (ra)
  268. ea += vcpu->arch.gpr[ra];
  269. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  270. vcpu->arch.gpr[ra] = ea;
  271. break;
  272. case 323: /* mfdcr */
  273. dcrn = get_dcrn(inst);
  274. rt = get_rt(inst);
  275. /* The guest may access CPR0 registers to determine the timebase
  276. * frequency, and it must know the real host frequency because it
  277. * can directly access the timebase registers.
  278. *
  279. * It would be possible to emulate those accesses in userspace,
  280. * but userspace can really only figure out the end frequency.
  281. * We could decompose that into the factors that compute it, but
  282. * that's tricky math, and it's easier to just report the real
  283. * CPR0 values.
  284. */
  285. switch (dcrn) {
  286. case DCRN_CPR0_CONFIG_ADDR:
  287. vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr;
  288. break;
  289. case DCRN_CPR0_CONFIG_DATA:
  290. local_irq_disable();
  291. mtdcr(DCRN_CPR0_CONFIG_ADDR,
  292. vcpu->arch.cpr0_cfgaddr);
  293. vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CONFIG_DATA);
  294. local_irq_enable();
  295. break;
  296. default:
  297. run->dcr.dcrn = dcrn;
  298. run->dcr.data = 0;
  299. run->dcr.is_write = 0;
  300. vcpu->arch.io_gpr = rt;
  301. vcpu->arch.dcr_needed = 1;
  302. emulated = EMULATE_DO_DCR;
  303. }
  304. break;
  305. case 339: /* mfspr */
  306. sprn = get_sprn(inst);
  307. rt = get_rt(inst);
  308. switch (sprn) {
  309. case SPRN_SRR0:
  310. vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
  311. case SPRN_SRR1:
  312. vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
  313. case SPRN_MMUCR:
  314. vcpu->arch.gpr[rt] = vcpu->arch.mmucr; break;
  315. case SPRN_PID:
  316. vcpu->arch.gpr[rt] = vcpu->arch.pid; break;
  317. case SPRN_IVPR:
  318. vcpu->arch.gpr[rt] = vcpu->arch.ivpr; break;
  319. case SPRN_CCR0:
  320. vcpu->arch.gpr[rt] = vcpu->arch.ccr0; break;
  321. case SPRN_CCR1:
  322. vcpu->arch.gpr[rt] = vcpu->arch.ccr1; break;
  323. case SPRN_PVR:
  324. vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
  325. case SPRN_DEAR:
  326. vcpu->arch.gpr[rt] = vcpu->arch.dear; break;
  327. case SPRN_ESR:
  328. vcpu->arch.gpr[rt] = vcpu->arch.esr; break;
  329. case SPRN_DBCR0:
  330. vcpu->arch.gpr[rt] = vcpu->arch.dbcr0; break;
  331. case SPRN_DBCR1:
  332. vcpu->arch.gpr[rt] = vcpu->arch.dbcr1; break;
  333. /* Note: mftb and TBRL/TBWL are user-accessible, so
  334. * the guest can always access the real TB anyways.
  335. * In fact, we probably will never see these traps. */
  336. case SPRN_TBWL:
  337. vcpu->arch.gpr[rt] = mftbl(); break;
  338. case SPRN_TBWU:
  339. vcpu->arch.gpr[rt] = mftbu(); break;
  340. case SPRN_SPRG0:
  341. vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
  342. case SPRN_SPRG1:
  343. vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
  344. case SPRN_SPRG2:
  345. vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
  346. case SPRN_SPRG3:
  347. vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
  348. /* Note: SPRG4-7 are user-readable, so we don't get
  349. * a trap. */
  350. case SPRN_IVOR0:
  351. vcpu->arch.gpr[rt] = vcpu->arch.ivor[0]; break;
  352. case SPRN_IVOR1:
  353. vcpu->arch.gpr[rt] = vcpu->arch.ivor[1]; break;
  354. case SPRN_IVOR2:
  355. vcpu->arch.gpr[rt] = vcpu->arch.ivor[2]; break;
  356. case SPRN_IVOR3:
  357. vcpu->arch.gpr[rt] = vcpu->arch.ivor[3]; break;
  358. case SPRN_IVOR4:
  359. vcpu->arch.gpr[rt] = vcpu->arch.ivor[4]; break;
  360. case SPRN_IVOR5:
  361. vcpu->arch.gpr[rt] = vcpu->arch.ivor[5]; break;
  362. case SPRN_IVOR6:
  363. vcpu->arch.gpr[rt] = vcpu->arch.ivor[6]; break;
  364. case SPRN_IVOR7:
  365. vcpu->arch.gpr[rt] = vcpu->arch.ivor[7]; break;
  366. case SPRN_IVOR8:
  367. vcpu->arch.gpr[rt] = vcpu->arch.ivor[8]; break;
  368. case SPRN_IVOR9:
  369. vcpu->arch.gpr[rt] = vcpu->arch.ivor[9]; break;
  370. case SPRN_IVOR10:
  371. vcpu->arch.gpr[rt] = vcpu->arch.ivor[10]; break;
  372. case SPRN_IVOR11:
  373. vcpu->arch.gpr[rt] = vcpu->arch.ivor[11]; break;
  374. case SPRN_IVOR12:
  375. vcpu->arch.gpr[rt] = vcpu->arch.ivor[12]; break;
  376. case SPRN_IVOR13:
  377. vcpu->arch.gpr[rt] = vcpu->arch.ivor[13]; break;
  378. case SPRN_IVOR14:
  379. vcpu->arch.gpr[rt] = vcpu->arch.ivor[14]; break;
  380. case SPRN_IVOR15:
  381. vcpu->arch.gpr[rt] = vcpu->arch.ivor[15]; break;
  382. default:
  383. printk("mfspr: unknown spr %x\n", sprn);
  384. vcpu->arch.gpr[rt] = 0;
  385. break;
  386. }
  387. break;
  388. case 407: /* sthx */
  389. rs = get_rs(inst);
  390. ra = get_ra(inst);
  391. rb = get_rb(inst);
  392. emulated = kvmppc_handle_store(run, vcpu,
  393. vcpu->arch.gpr[rs],
  394. 2, 1);
  395. break;
  396. case 439: /* sthux */
  397. rs = get_rs(inst);
  398. ra = get_ra(inst);
  399. rb = get_rb(inst);
  400. ea = vcpu->arch.gpr[rb];
  401. if (ra)
  402. ea += vcpu->arch.gpr[ra];
  403. emulated = kvmppc_handle_store(run, vcpu,
  404. vcpu->arch.gpr[rs],
  405. 2, 1);
  406. vcpu->arch.gpr[ra] = ea;
  407. break;
  408. case 451: /* mtdcr */
  409. dcrn = get_dcrn(inst);
  410. rs = get_rs(inst);
  411. /* emulate some access in kernel */
  412. switch (dcrn) {
  413. case DCRN_CPR0_CONFIG_ADDR:
  414. vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs];
  415. break;
  416. default:
  417. run->dcr.dcrn = dcrn;
  418. run->dcr.data = vcpu->arch.gpr[rs];
  419. run->dcr.is_write = 1;
  420. vcpu->arch.dcr_needed = 1;
  421. emulated = EMULATE_DO_DCR;
  422. }
  423. break;
  424. case 467: /* mtspr */
  425. sprn = get_sprn(inst);
  426. rs = get_rs(inst);
  427. switch (sprn) {
  428. case SPRN_SRR0:
  429. vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
  430. case SPRN_SRR1:
  431. vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
  432. case SPRN_MMUCR:
  433. vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break;
  434. case SPRN_PID:
  435. vcpu->arch.pid = vcpu->arch.gpr[rs]; break;
  436. case SPRN_CCR0:
  437. vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break;
  438. case SPRN_CCR1:
  439. vcpu->arch.ccr1 = vcpu->arch.gpr[rs]; break;
  440. case SPRN_DEAR:
  441. vcpu->arch.dear = vcpu->arch.gpr[rs]; break;
  442. case SPRN_ESR:
  443. vcpu->arch.esr = vcpu->arch.gpr[rs]; break;
  444. case SPRN_DBCR0:
  445. vcpu->arch.dbcr0 = vcpu->arch.gpr[rs]; break;
  446. case SPRN_DBCR1:
  447. vcpu->arch.dbcr1 = vcpu->arch.gpr[rs]; break;
  448. /* XXX We need to context-switch the timebase for
  449. * watchdog and FIT. */
  450. case SPRN_TBWL: break;
  451. case SPRN_TBWU: break;
  452. case SPRN_DEC:
  453. vcpu->arch.dec = vcpu->arch.gpr[rs];
  454. kvmppc_emulate_dec(vcpu);
  455. break;
  456. case SPRN_TSR:
  457. vcpu->arch.tsr &= ~vcpu->arch.gpr[rs]; break;
  458. case SPRN_TCR:
  459. vcpu->arch.tcr = vcpu->arch.gpr[rs];
  460. kvmppc_emulate_dec(vcpu);
  461. break;
  462. case SPRN_SPRG0:
  463. vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
  464. case SPRN_SPRG1:
  465. vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
  466. case SPRN_SPRG2:
  467. vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
  468. case SPRN_SPRG3:
  469. vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
  470. /* Note: SPRG4-7 are user-readable. These values are
  471. * loaded into the real SPRGs when resuming the
  472. * guest. */
  473. case SPRN_SPRG4:
  474. vcpu->arch.sprg4 = vcpu->arch.gpr[rs]; break;
  475. case SPRN_SPRG5:
  476. vcpu->arch.sprg5 = vcpu->arch.gpr[rs]; break;
  477. case SPRN_SPRG6:
  478. vcpu->arch.sprg6 = vcpu->arch.gpr[rs]; break;
  479. case SPRN_SPRG7:
  480. vcpu->arch.sprg7 = vcpu->arch.gpr[rs]; break;
  481. case SPRN_IVPR:
  482. vcpu->arch.ivpr = vcpu->arch.gpr[rs]; break;
  483. case SPRN_IVOR0:
  484. vcpu->arch.ivor[0] = vcpu->arch.gpr[rs]; break;
  485. case SPRN_IVOR1:
  486. vcpu->arch.ivor[1] = vcpu->arch.gpr[rs]; break;
  487. case SPRN_IVOR2:
  488. vcpu->arch.ivor[2] = vcpu->arch.gpr[rs]; break;
  489. case SPRN_IVOR3:
  490. vcpu->arch.ivor[3] = vcpu->arch.gpr[rs]; break;
  491. case SPRN_IVOR4:
  492. vcpu->arch.ivor[4] = vcpu->arch.gpr[rs]; break;
  493. case SPRN_IVOR5:
  494. vcpu->arch.ivor[5] = vcpu->arch.gpr[rs]; break;
  495. case SPRN_IVOR6:
  496. vcpu->arch.ivor[6] = vcpu->arch.gpr[rs]; break;
  497. case SPRN_IVOR7:
  498. vcpu->arch.ivor[7] = vcpu->arch.gpr[rs]; break;
  499. case SPRN_IVOR8:
  500. vcpu->arch.ivor[8] = vcpu->arch.gpr[rs]; break;
  501. case SPRN_IVOR9:
  502. vcpu->arch.ivor[9] = vcpu->arch.gpr[rs]; break;
  503. case SPRN_IVOR10:
  504. vcpu->arch.ivor[10] = vcpu->arch.gpr[rs]; break;
  505. case SPRN_IVOR11:
  506. vcpu->arch.ivor[11] = vcpu->arch.gpr[rs]; break;
  507. case SPRN_IVOR12:
  508. vcpu->arch.ivor[12] = vcpu->arch.gpr[rs]; break;
  509. case SPRN_IVOR13:
  510. vcpu->arch.ivor[13] = vcpu->arch.gpr[rs]; break;
  511. case SPRN_IVOR14:
  512. vcpu->arch.ivor[14] = vcpu->arch.gpr[rs]; break;
  513. case SPRN_IVOR15:
  514. vcpu->arch.ivor[15] = vcpu->arch.gpr[rs]; break;
  515. default:
  516. printk("mtspr: unknown spr %x\n", sprn);
  517. emulated = EMULATE_FAIL;
  518. break;
  519. }
  520. break;
  521. case 470: /* dcbi */
  522. /* Do nothing. The guest is performing dcbi because
  523. * hardware DMA is not snooped by the dcache, but
  524. * emulated DMA either goes through the dcache as
  525. * normal writes, or the host kernel has handled dcache
  526. * coherence. */
  527. break;
  528. case 534: /* lwbrx */
  529. rt = get_rt(inst);
  530. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
  531. break;
  532. case 566: /* tlbsync */
  533. break;
  534. case 662: /* stwbrx */
  535. rs = get_rs(inst);
  536. ra = get_ra(inst);
  537. rb = get_rb(inst);
  538. emulated = kvmppc_handle_store(run, vcpu,
  539. vcpu->arch.gpr[rs],
  540. 4, 0);
  541. break;
  542. case 978: /* tlbwe */
  543. emulated = kvmppc_emul_tlbwe(vcpu, inst);
  544. break;
  545. case 914: { /* tlbsx */
  546. int index;
  547. unsigned int as = get_mmucr_sts(vcpu);
  548. unsigned int pid = get_mmucr_stid(vcpu);
  549. rt = get_rt(inst);
  550. ra = get_ra(inst);
  551. rb = get_rb(inst);
  552. rc = get_rc(inst);
  553. ea = vcpu->arch.gpr[rb];
  554. if (ra)
  555. ea += vcpu->arch.gpr[ra];
  556. index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
  557. if (rc) {
  558. if (index < 0)
  559. vcpu->arch.cr &= ~0x20000000;
  560. else
  561. vcpu->arch.cr |= 0x20000000;
  562. }
  563. vcpu->arch.gpr[rt] = index;
  564. }
  565. break;
  566. case 790: /* lhbrx */
  567. rt = get_rt(inst);
  568. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
  569. break;
  570. case 918: /* sthbrx */
  571. rs = get_rs(inst);
  572. ra = get_ra(inst);
  573. rb = get_rb(inst);
  574. emulated = kvmppc_handle_store(run, vcpu,
  575. vcpu->arch.gpr[rs],
  576. 2, 0);
  577. break;
  578. case 966: /* iccci */
  579. break;
  580. default:
  581. printk("unknown: op %d xop %d\n", get_op(inst),
  582. get_xop(inst));
  583. emulated = EMULATE_FAIL;
  584. break;
  585. }
  586. break;
  587. case 32: /* lwz */
  588. rt = get_rt(inst);
  589. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  590. break;
  591. case 33: /* lwzu */
  592. ra = get_ra(inst);
  593. rt = get_rt(inst);
  594. emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
  595. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  596. break;
  597. case 34: /* lbz */
  598. rt = get_rt(inst);
  599. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  600. break;
  601. case 35: /* lbzu */
  602. ra = get_ra(inst);
  603. rt = get_rt(inst);
  604. emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
  605. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  606. break;
  607. case 36: /* stw */
  608. rs = get_rs(inst);
  609. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  610. 4, 1);
  611. break;
  612. case 37: /* stwu */
  613. ra = get_ra(inst);
  614. rs = get_rs(inst);
  615. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  616. 4, 1);
  617. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  618. break;
  619. case 38: /* stb */
  620. rs = get_rs(inst);
  621. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  622. 1, 1);
  623. break;
  624. case 39: /* stbu */
  625. ra = get_ra(inst);
  626. rs = get_rs(inst);
  627. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  628. 1, 1);
  629. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  630. break;
  631. case 40: /* lhz */
  632. rt = get_rt(inst);
  633. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  634. break;
  635. case 41: /* lhzu */
  636. ra = get_ra(inst);
  637. rt = get_rt(inst);
  638. emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
  639. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  640. break;
  641. case 44: /* sth */
  642. rs = get_rs(inst);
  643. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  644. 2, 1);
  645. break;
  646. case 45: /* sthu */
  647. ra = get_ra(inst);
  648. rs = get_rs(inst);
  649. emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
  650. 2, 1);
  651. vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
  652. break;
  653. default:
  654. printk("unknown op %d\n", get_op(inst));
  655. emulated = EMULATE_FAIL;
  656. break;
  657. }
  658. if (advance)
  659. vcpu->arch.pc += 4; /* Advance past emulated instruction. */
  660. return emulated;
  661. }