e500_tlb.c 19 KB

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