e500_tlb.c 19 KB

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