emulate.c 20 KB

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