e500_tlb.c 18 KB

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