e500_tlb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. *
  6. * Description:
  7. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  8. * by Hollis Blanchard <hollisb@us.ibm.com>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/kvm.h>
  18. #include <linux/kvm_host.h>
  19. #include <linux/highmem.h>
  20. #include <asm/kvm_ppc.h>
  21. #include <asm/kvm_e500.h>
  22. #include "../mm/mmu_decl.h"
  23. #include "e500_tlb.h"
  24. #include "trace.h"
  25. #include "timing.h"
  26. #define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
  27. static unsigned int tlb1_entry_num;
  28. void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
  29. {
  30. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  31. struct tlbe *tlbe;
  32. int i, tlbsel;
  33. printk("| %8s | %8s | %8s | %8s | %8s |\n",
  34. "nr", "mas1", "mas2", "mas3", "mas7");
  35. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  36. printk("Guest TLB%d:\n", tlbsel);
  37. for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
  38. tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
  39. if (tlbe->mas1 & MAS1_VALID)
  40. printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
  41. tlbsel, i, tlbe->mas1, tlbe->mas2,
  42. tlbe->mas3, tlbe->mas7);
  43. }
  44. }
  45. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  46. printk("Shadow TLB%d:\n", tlbsel);
  47. for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) {
  48. tlbe = &vcpu_e500->shadow_tlb[tlbsel][i];
  49. if (tlbe->mas1 & MAS1_VALID)
  50. printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
  51. tlbsel, i, tlbe->mas1, tlbe->mas2,
  52. tlbe->mas3, tlbe->mas7);
  53. }
  54. }
  55. }
  56. static inline unsigned int tlb0_get_next_victim(
  57. struct kvmppc_vcpu_e500 *vcpu_e500)
  58. {
  59. unsigned int victim;
  60. victim = vcpu_e500->guest_tlb_nv[0]++;
  61. if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
  62. vcpu_e500->guest_tlb_nv[0] = 0;
  63. return victim;
  64. }
  65. static inline unsigned int tlb1_max_shadow_size(void)
  66. {
  67. return tlb1_entry_num - tlbcam_index;
  68. }
  69. static inline int tlbe_is_writable(struct tlbe *tlbe)
  70. {
  71. return tlbe->mas3 & (MAS3_SW|MAS3_UW);
  72. }
  73. static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
  74. {
  75. /* Mask off reserved bits. */
  76. mas3 &= MAS3_ATTRIB_MASK;
  77. if (!usermode) {
  78. /* Guest is in supervisor mode,
  79. * so we need to translate guest
  80. * supervisor permissions into user permissions. */
  81. mas3 &= ~E500_TLB_USER_PERM_MASK;
  82. mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
  83. }
  84. return mas3 | E500_TLB_SUPER_PERM_MASK;
  85. }
  86. static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
  87. {
  88. #ifdef CONFIG_SMP
  89. return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
  90. #else
  91. return mas2 & MAS2_ATTRIB_MASK;
  92. #endif
  93. }
  94. /*
  95. * writing shadow tlb entry to host TLB
  96. */
  97. static inline void __write_host_tlbe(struct tlbe *stlbe)
  98. {
  99. mtspr(SPRN_MAS1, stlbe->mas1);
  100. mtspr(SPRN_MAS2, stlbe->mas2);
  101. mtspr(SPRN_MAS3, stlbe->mas3);
  102. mtspr(SPRN_MAS7, stlbe->mas7);
  103. __asm__ __volatile__ ("tlbwe\n" : : );
  104. }
  105. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  106. int tlbsel, int esel)
  107. {
  108. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  109. local_irq_disable();
  110. if (tlbsel == 0) {
  111. __write_host_tlbe(stlbe);
  112. } else {
  113. unsigned register mas0;
  114. mas0 = mfspr(SPRN_MAS0);
  115. mtspr(SPRN_MAS0, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(esel)));
  116. __write_host_tlbe(stlbe);
  117. mtspr(SPRN_MAS0, mas0);
  118. }
  119. local_irq_enable();
  120. }
  121. void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
  122. {
  123. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  124. int i;
  125. unsigned register mas0;
  126. /* Load all valid TLB1 entries to reduce guest tlb miss fault */
  127. local_irq_disable();
  128. mas0 = mfspr(SPRN_MAS0);
  129. for (i = 0; i < tlb1_max_shadow_size(); i++) {
  130. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
  131. if (get_tlb_v(stlbe)) {
  132. mtspr(SPRN_MAS0, MAS0_TLBSEL(1)
  133. | MAS0_ESEL(to_htlb1_esel(i)));
  134. __write_host_tlbe(stlbe);
  135. }
  136. }
  137. mtspr(SPRN_MAS0, mas0);
  138. local_irq_enable();
  139. }
  140. void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
  141. {
  142. _tlbil_all();
  143. }
  144. /* Search the guest TLB for a matching entry. */
  145. static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
  146. gva_t eaddr, int tlbsel, unsigned int pid, int as)
  147. {
  148. int i;
  149. /* XXX Replace loop with fancy data structures. */
  150. for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
  151. struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
  152. unsigned int tid;
  153. if (eaddr < get_tlb_eaddr(tlbe))
  154. continue;
  155. if (eaddr > get_tlb_end(tlbe))
  156. continue;
  157. tid = get_tlb_tid(tlbe);
  158. if (tid && (tid != pid))
  159. continue;
  160. if (!get_tlb_v(tlbe))
  161. continue;
  162. if (get_tlb_ts(tlbe) != as && as != -1)
  163. continue;
  164. return i;
  165. }
  166. return -1;
  167. }
  168. static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
  169. int tlbsel, int esel)
  170. {
  171. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  172. struct page *page = vcpu_e500->shadow_pages[tlbsel][esel];
  173. if (page) {
  174. vcpu_e500->shadow_pages[tlbsel][esel] = NULL;
  175. if (get_tlb_v(stlbe)) {
  176. if (tlbe_is_writable(stlbe))
  177. kvm_release_page_dirty(page);
  178. else
  179. kvm_release_page_clean(page);
  180. }
  181. }
  182. }
  183. static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
  184. int tlbsel, int esel)
  185. {
  186. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  187. kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
  188. stlbe->mas1 = 0;
  189. trace_kvm_stlb_inval(index_of(tlbsel, esel));
  190. }
  191. static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
  192. gva_t eaddr, gva_t eend, u32 tid)
  193. {
  194. unsigned int pid = tid & 0xff;
  195. unsigned int i;
  196. /* XXX Replace loop with fancy data structures. */
  197. for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) {
  198. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
  199. unsigned int tid;
  200. if (!get_tlb_v(stlbe))
  201. continue;
  202. if (eend < get_tlb_eaddr(stlbe))
  203. continue;
  204. if (eaddr > get_tlb_end(stlbe))
  205. continue;
  206. tid = get_tlb_tid(stlbe);
  207. if (tid && (tid != pid))
  208. continue;
  209. kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
  210. write_host_tlbe(vcpu_e500, 1, i);
  211. }
  212. }
  213. static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
  214. unsigned int eaddr, int as)
  215. {
  216. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  217. unsigned int victim, pidsel, tsized;
  218. int tlbsel;
  219. /* since we only have two TLBs, only lower bit is used. */
  220. tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
  221. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  222. pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
  223. tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
  224. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  225. | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  226. vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
  227. | MAS1_TID(vcpu_e500->pid[pidsel])
  228. | MAS1_TSIZE(tsized);
  229. vcpu_e500->mas2 = (eaddr & MAS2_EPN)
  230. | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
  231. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  232. vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
  233. | (get_cur_pid(vcpu) << 16)
  234. | (as ? MAS6_SAS : 0);
  235. vcpu_e500->mas7 = 0;
  236. }
  237. static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  238. u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel)
  239. {
  240. struct page *new_page;
  241. struct tlbe *stlbe;
  242. hpa_t hpaddr;
  243. stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  244. /* Get reference to new page. */
  245. new_page = gfn_to_page(vcpu_e500->vcpu.kvm, gfn);
  246. if (is_error_page(new_page)) {
  247. printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n",
  248. (long)gfn);
  249. kvm_release_page_clean(new_page);
  250. return;
  251. }
  252. hpaddr = page_to_phys(new_page);
  253. /* Drop reference to old page. */
  254. kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
  255. vcpu_e500->shadow_pages[tlbsel][esel] = new_page;
  256. /* Force TS=1 IPROT=0 TSIZE=4KB for all guest mappings. */
  257. stlbe->mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K)
  258. | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
  259. stlbe->mas2 = (gvaddr & MAS2_EPN)
  260. | e500_shadow_mas2_attrib(gtlbe->mas2,
  261. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  262. stlbe->mas3 = (hpaddr & MAS3_RPN)
  263. | e500_shadow_mas3_attrib(gtlbe->mas3,
  264. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  265. stlbe->mas7 = (hpaddr >> 32) & MAS7_RPN;
  266. trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
  267. stlbe->mas3, stlbe->mas7);
  268. }
  269. /* XXX only map the one-one case, for now use TLB0 */
  270. static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  271. int tlbsel, int esel)
  272. {
  273. struct tlbe *gtlbe;
  274. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  275. kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  276. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  277. gtlbe, tlbsel, esel);
  278. return esel;
  279. }
  280. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  281. * the shadow TLB. */
  282. /* XXX for both one-one and one-to-many , for now use TLB1 */
  283. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  284. u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe)
  285. {
  286. unsigned int victim;
  287. victim = vcpu_e500->guest_tlb_nv[1]++;
  288. if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size()))
  289. vcpu_e500->guest_tlb_nv[1] = 0;
  290. kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim);
  291. return victim;
  292. }
  293. /* Invalidate all guest kernel mappings when enter usermode,
  294. * so that when they fault back in they will get the
  295. * proper permission bits. */
  296. void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
  297. {
  298. if (usermode) {
  299. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  300. int i;
  301. /* XXX Replace loop with fancy data structures. */
  302. for (i = 0; i < tlb1_max_shadow_size(); i++)
  303. kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
  304. _tlbil_all();
  305. }
  306. }
  307. static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
  308. int tlbsel, int esel)
  309. {
  310. struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  311. if (unlikely(get_tlb_iprot(gtlbe)))
  312. return -1;
  313. if (tlbsel == 1) {
  314. kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe),
  315. get_tlb_end(gtlbe),
  316. get_tlb_tid(gtlbe));
  317. } else {
  318. kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
  319. }
  320. gtlbe->mas1 = 0;
  321. return 0;
  322. }
  323. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  324. {
  325. int esel;
  326. if (value & MMUCSR0_TLB0FI)
  327. for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++)
  328. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  329. if (value & MMUCSR0_TLB1FI)
  330. for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++)
  331. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  332. _tlbil_all();
  333. return EMULATE_DONE;
  334. }
  335. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
  336. {
  337. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  338. unsigned int ia;
  339. int esel, tlbsel;
  340. gva_t ea;
  341. ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
  342. ia = (ea >> 2) & 0x1;
  343. /* since we only have two TLBs, only lower bit is used. */
  344. tlbsel = (ea >> 3) & 0x1;
  345. if (ia) {
  346. /* invalidate all entries */
  347. for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
  348. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  349. } else {
  350. ea &= 0xfffff000;
  351. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  352. get_cur_pid(vcpu), -1);
  353. if (esel >= 0)
  354. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  355. }
  356. _tlbil_all();
  357. return EMULATE_DONE;
  358. }
  359. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  360. {
  361. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  362. int tlbsel, esel;
  363. struct tlbe *gtlbe;
  364. tlbsel = get_tlb_tlbsel(vcpu_e500);
  365. esel = get_tlb_esel(vcpu_e500, tlbsel);
  366. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  367. vcpu_e500->mas0 &= ~MAS0_NV(~0);
  368. vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  369. vcpu_e500->mas1 = gtlbe->mas1;
  370. vcpu_e500->mas2 = gtlbe->mas2;
  371. vcpu_e500->mas3 = gtlbe->mas3;
  372. vcpu_e500->mas7 = gtlbe->mas7;
  373. return EMULATE_DONE;
  374. }
  375. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
  376. {
  377. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  378. int as = !!get_cur_sas(vcpu_e500);
  379. unsigned int pid = get_cur_spid(vcpu_e500);
  380. int esel, tlbsel;
  381. struct tlbe *gtlbe = NULL;
  382. gva_t ea;
  383. ea = kvmppc_get_gpr(vcpu, rb);
  384. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  385. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  386. if (esel >= 0) {
  387. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  388. break;
  389. }
  390. }
  391. if (gtlbe) {
  392. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  393. | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  394. vcpu_e500->mas1 = gtlbe->mas1;
  395. vcpu_e500->mas2 = gtlbe->mas2;
  396. vcpu_e500->mas3 = gtlbe->mas3;
  397. vcpu_e500->mas7 = gtlbe->mas7;
  398. } else {
  399. int victim;
  400. /* since we only have two TLBs, only lower bit is used. */
  401. tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
  402. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  403. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  404. | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  405. vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
  406. | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  407. | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
  408. vcpu_e500->mas2 &= MAS2_EPN;
  409. vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
  410. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  411. vcpu_e500->mas7 = 0;
  412. }
  413. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  414. return EMULATE_DONE;
  415. }
  416. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  417. {
  418. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  419. u64 eaddr;
  420. u64 raddr;
  421. u32 tid;
  422. struct tlbe *gtlbe;
  423. int tlbsel, esel, stlbsel, sesel;
  424. tlbsel = get_tlb_tlbsel(vcpu_e500);
  425. esel = get_tlb_esel(vcpu_e500, tlbsel);
  426. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  427. if (get_tlb_v(gtlbe) && tlbsel == 1) {
  428. eaddr = get_tlb_eaddr(gtlbe);
  429. tid = get_tlb_tid(gtlbe);
  430. kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
  431. get_tlb_end(gtlbe), tid);
  432. }
  433. gtlbe->mas1 = vcpu_e500->mas1;
  434. gtlbe->mas2 = vcpu_e500->mas2;
  435. gtlbe->mas3 = vcpu_e500->mas3;
  436. gtlbe->mas7 = vcpu_e500->mas7;
  437. trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
  438. gtlbe->mas3, gtlbe->mas7);
  439. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  440. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  441. switch (tlbsel) {
  442. case 0:
  443. /* TLB0 */
  444. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  445. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  446. stlbsel = 0;
  447. sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
  448. break;
  449. case 1:
  450. /* TLB1 */
  451. eaddr = get_tlb_eaddr(gtlbe);
  452. raddr = get_tlb_raddr(gtlbe);
  453. /* Create a 4KB mapping on the host.
  454. * If the guest wanted a large page,
  455. * only the first 4KB is mapped here and the rest
  456. * are mapped on the fly. */
  457. stlbsel = 1;
  458. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  459. raddr >> PAGE_SHIFT, gtlbe);
  460. break;
  461. default:
  462. BUG();
  463. }
  464. write_host_tlbe(vcpu_e500, stlbsel, sesel);
  465. }
  466. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  467. return EMULATE_DONE;
  468. }
  469. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  470. {
  471. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  472. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  473. }
  474. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  475. {
  476. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  477. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  478. }
  479. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  480. {
  481. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  482. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  483. }
  484. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  485. {
  486. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  487. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  488. }
  489. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  490. gva_t eaddr)
  491. {
  492. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  493. struct tlbe *gtlbe =
  494. &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
  495. u64 pgmask = get_tlb_bytes(gtlbe) - 1;
  496. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  497. }
  498. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  499. {
  500. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  501. int tlbsel, i;
  502. for (tlbsel = 0; tlbsel < 2; tlbsel++)
  503. for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
  504. kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
  505. /* discard all guest mapping */
  506. _tlbil_all();
  507. }
  508. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  509. unsigned int index)
  510. {
  511. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  512. int tlbsel = tlbsel_of(index);
  513. int esel = esel_of(index);
  514. int stlbsel, sesel;
  515. switch (tlbsel) {
  516. case 0:
  517. stlbsel = 0;
  518. sesel = esel;
  519. break;
  520. case 1: {
  521. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  522. struct tlbe *gtlbe
  523. = &vcpu_e500->guest_tlb[tlbsel][esel];
  524. stlbsel = 1;
  525. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
  526. break;
  527. }
  528. default:
  529. BUG();
  530. break;
  531. }
  532. write_host_tlbe(vcpu_e500, stlbsel, sesel);
  533. }
  534. int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  535. gva_t eaddr, unsigned int pid, int as)
  536. {
  537. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  538. int esel, tlbsel;
  539. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  540. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  541. if (esel >= 0)
  542. return index_of(tlbsel, esel);
  543. }
  544. return -1;
  545. }
  546. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  547. {
  548. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  549. vcpu_e500->pid[0] = vcpu->arch.shadow_pid =
  550. vcpu->arch.pid = pid;
  551. }
  552. void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  553. {
  554. struct tlbe *tlbe;
  555. /* Insert large initial mapping for guest. */
  556. tlbe = &vcpu_e500->guest_tlb[1][0];
  557. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
  558. tlbe->mas2 = 0;
  559. tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
  560. tlbe->mas7 = 0;
  561. /* 4K map for serial output. Used by kernel wrapper. */
  562. tlbe = &vcpu_e500->guest_tlb[1][1];
  563. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  564. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  565. tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  566. tlbe->mas7 = 0;
  567. }
  568. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  569. {
  570. tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
  571. vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
  572. vcpu_e500->guest_tlb[0] =
  573. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  574. if (vcpu_e500->guest_tlb[0] == NULL)
  575. goto err_out;
  576. vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
  577. vcpu_e500->shadow_tlb[0] =
  578. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  579. if (vcpu_e500->shadow_tlb[0] == NULL)
  580. goto err_out_guest0;
  581. vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
  582. vcpu_e500->guest_tlb[1] =
  583. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  584. if (vcpu_e500->guest_tlb[1] == NULL)
  585. goto err_out_shadow0;
  586. vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
  587. vcpu_e500->shadow_tlb[1] =
  588. kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
  589. if (vcpu_e500->shadow_tlb[1] == NULL)
  590. goto err_out_guest1;
  591. vcpu_e500->shadow_pages[0] = (struct page **)
  592. kzalloc(sizeof(struct page *) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  593. if (vcpu_e500->shadow_pages[0] == NULL)
  594. goto err_out_shadow1;
  595. vcpu_e500->shadow_pages[1] = (struct page **)
  596. kzalloc(sizeof(struct page *) * tlb1_entry_num, GFP_KERNEL);
  597. if (vcpu_e500->shadow_pages[1] == NULL)
  598. goto err_out_page0;
  599. /* Init TLB configuration register */
  600. vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
  601. vcpu_e500->tlb0cfg |= vcpu_e500->guest_tlb_size[0];
  602. vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
  603. vcpu_e500->tlb1cfg |= vcpu_e500->guest_tlb_size[1];
  604. return 0;
  605. err_out_page0:
  606. kfree(vcpu_e500->shadow_pages[0]);
  607. err_out_shadow1:
  608. kfree(vcpu_e500->shadow_tlb[1]);
  609. err_out_guest1:
  610. kfree(vcpu_e500->guest_tlb[1]);
  611. err_out_shadow0:
  612. kfree(vcpu_e500->shadow_tlb[0]);
  613. err_out_guest0:
  614. kfree(vcpu_e500->guest_tlb[0]);
  615. err_out:
  616. return -1;
  617. }
  618. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  619. {
  620. kfree(vcpu_e500->shadow_pages[1]);
  621. kfree(vcpu_e500->shadow_pages[0]);
  622. kfree(vcpu_e500->shadow_tlb[1]);
  623. kfree(vcpu_e500->guest_tlb[1]);
  624. kfree(vcpu_e500->shadow_tlb[0]);
  625. kfree(vcpu_e500->guest_tlb[0]);
  626. }