e500_tlb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  319. {
  320. int esel;
  321. if (value & MMUCSR0_TLB0FI)
  322. for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++)
  323. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  324. if (value & MMUCSR0_TLB1FI)
  325. for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++)
  326. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  327. _tlbil_all();
  328. return EMULATE_DONE;
  329. }
  330. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
  331. {
  332. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  333. unsigned int ia;
  334. int esel, tlbsel;
  335. gva_t ea;
  336. ea = ((ra) ? vcpu->arch.gpr[ra] : 0) + vcpu->arch.gpr[rb];
  337. ia = (ea >> 2) & 0x1;
  338. /* since we only have two TLBs, only lower bit is used. */
  339. tlbsel = (ea >> 3) & 0x1;
  340. if (ia) {
  341. /* invalidate all entries */
  342. for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
  343. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  344. } else {
  345. ea &= 0xfffff000;
  346. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  347. get_cur_pid(vcpu), -1);
  348. if (esel >= 0)
  349. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  350. }
  351. _tlbil_all();
  352. return EMULATE_DONE;
  353. }
  354. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  355. {
  356. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  357. int tlbsel, esel;
  358. struct tlbe *gtlbe;
  359. tlbsel = get_tlb_tlbsel(vcpu_e500);
  360. esel = get_tlb_esel(vcpu_e500, tlbsel);
  361. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  362. vcpu_e500->mas0 &= ~MAS0_NV(~0);
  363. vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  364. vcpu_e500->mas1 = gtlbe->mas1;
  365. vcpu_e500->mas2 = gtlbe->mas2;
  366. vcpu_e500->mas3 = gtlbe->mas3;
  367. vcpu_e500->mas7 = gtlbe->mas7;
  368. return EMULATE_DONE;
  369. }
  370. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
  371. {
  372. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  373. int as = !!get_cur_sas(vcpu_e500);
  374. unsigned int pid = get_cur_spid(vcpu_e500);
  375. int esel, tlbsel;
  376. struct tlbe *gtlbe = NULL;
  377. gva_t ea;
  378. ea = vcpu->arch.gpr[rb];
  379. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  380. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  381. if (esel >= 0) {
  382. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  383. break;
  384. }
  385. }
  386. if (gtlbe) {
  387. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  388. | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  389. vcpu_e500->mas1 = gtlbe->mas1;
  390. vcpu_e500->mas2 = gtlbe->mas2;
  391. vcpu_e500->mas3 = gtlbe->mas3;
  392. vcpu_e500->mas7 = gtlbe->mas7;
  393. } else {
  394. int victim;
  395. /* since we only have two TLBs, only lower bit is used. */
  396. tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
  397. victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
  398. vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  399. | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
  400. vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
  401. | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  402. | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
  403. vcpu_e500->mas2 &= MAS2_EPN;
  404. vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
  405. vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  406. vcpu_e500->mas7 = 0;
  407. }
  408. return EMULATE_DONE;
  409. }
  410. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  411. {
  412. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  413. u64 eaddr;
  414. u64 raddr;
  415. u32 tid;
  416. struct tlbe *gtlbe;
  417. int tlbsel, esel, stlbsel, sesel;
  418. tlbsel = get_tlb_tlbsel(vcpu_e500);
  419. esel = get_tlb_esel(vcpu_e500, tlbsel);
  420. gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
  421. if (get_tlb_v(gtlbe) && tlbsel == 1) {
  422. eaddr = get_tlb_eaddr(gtlbe);
  423. tid = get_tlb_tid(gtlbe);
  424. kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
  425. get_tlb_end(gtlbe), tid);
  426. }
  427. gtlbe->mas1 = vcpu_e500->mas1;
  428. gtlbe->mas2 = vcpu_e500->mas2;
  429. gtlbe->mas3 = vcpu_e500->mas3;
  430. gtlbe->mas7 = vcpu_e500->mas7;
  431. KVMTRACE_5D(GTLB_WRITE, vcpu, vcpu_e500->mas0,
  432. gtlbe->mas1, gtlbe->mas2, gtlbe->mas3, gtlbe->mas7,
  433. handler);
  434. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  435. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  436. switch (tlbsel) {
  437. case 0:
  438. /* TLB0 */
  439. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  440. gtlbe->mas1 |= MAS1_TSIZE(BOOKE_PAGESZ_4K);
  441. stlbsel = 0;
  442. sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
  443. break;
  444. case 1:
  445. /* TLB1 */
  446. eaddr = get_tlb_eaddr(gtlbe);
  447. raddr = get_tlb_raddr(gtlbe);
  448. /* Create a 4KB mapping on the host.
  449. * If the guest wanted a large page,
  450. * only the first 4KB is mapped here and the rest
  451. * are mapped on the fly. */
  452. stlbsel = 1;
  453. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  454. raddr >> PAGE_SHIFT, gtlbe);
  455. break;
  456. default:
  457. BUG();
  458. }
  459. write_host_tlbe(vcpu_e500, stlbsel, sesel);
  460. }
  461. return EMULATE_DONE;
  462. }
  463. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  464. {
  465. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  466. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  467. }
  468. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  469. {
  470. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  471. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  472. }
  473. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  474. {
  475. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  476. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  477. }
  478. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  479. {
  480. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  481. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  482. }
  483. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  484. gva_t eaddr)
  485. {
  486. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  487. struct tlbe *gtlbe =
  488. &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
  489. u64 pgmask = get_tlb_bytes(gtlbe) - 1;
  490. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  491. }
  492. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  493. {
  494. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  495. int tlbsel, i;
  496. for (tlbsel = 0; tlbsel < 2; tlbsel++)
  497. for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
  498. kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
  499. /* discard all guest mapping */
  500. _tlbil_all();
  501. }
  502. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  503. unsigned int index)
  504. {
  505. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  506. int tlbsel = tlbsel_of(index);
  507. int esel = esel_of(index);
  508. int stlbsel, sesel;
  509. switch (tlbsel) {
  510. case 0:
  511. stlbsel = 0;
  512. sesel = esel;
  513. break;
  514. case 1: {
  515. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  516. struct tlbe *gtlbe
  517. = &vcpu_e500->guest_tlb[tlbsel][esel];
  518. stlbsel = 1;
  519. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
  520. break;
  521. }
  522. default:
  523. BUG();
  524. break;
  525. }
  526. write_host_tlbe(vcpu_e500, stlbsel, sesel);
  527. }
  528. int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  529. gva_t eaddr, unsigned int pid, int as)
  530. {
  531. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  532. int esel, tlbsel;
  533. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  534. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  535. if (esel >= 0)
  536. return index_of(tlbsel, esel);
  537. }
  538. return -1;
  539. }
  540. void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  541. {
  542. struct tlbe *tlbe;
  543. /* Insert large initial mapping for guest. */
  544. tlbe = &vcpu_e500->guest_tlb[1][0];
  545. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_256M);
  546. tlbe->mas2 = 0;
  547. tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
  548. tlbe->mas7 = 0;
  549. /* 4K map for serial output. Used by kernel wrapper. */
  550. tlbe = &vcpu_e500->guest_tlb[1][1];
  551. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_4K);
  552. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  553. tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  554. tlbe->mas7 = 0;
  555. }
  556. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  557. {
  558. tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
  559. vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
  560. vcpu_e500->guest_tlb[0] =
  561. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  562. if (vcpu_e500->guest_tlb[0] == NULL)
  563. goto err_out;
  564. vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
  565. vcpu_e500->shadow_tlb[0] =
  566. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  567. if (vcpu_e500->shadow_tlb[0] == NULL)
  568. goto err_out_guest0;
  569. vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
  570. vcpu_e500->guest_tlb[1] =
  571. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  572. if (vcpu_e500->guest_tlb[1] == NULL)
  573. goto err_out_shadow0;
  574. vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
  575. vcpu_e500->shadow_tlb[1] =
  576. kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
  577. if (vcpu_e500->shadow_tlb[1] == NULL)
  578. goto err_out_guest1;
  579. vcpu_e500->shadow_pages[0] = (struct page **)
  580. kzalloc(sizeof(struct page *) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  581. if (vcpu_e500->shadow_pages[0] == NULL)
  582. goto err_out_shadow1;
  583. vcpu_e500->shadow_pages[1] = (struct page **)
  584. kzalloc(sizeof(struct page *) * tlb1_entry_num, GFP_KERNEL);
  585. if (vcpu_e500->shadow_pages[1] == NULL)
  586. goto err_out_page0;
  587. return 0;
  588. err_out_page0:
  589. kfree(vcpu_e500->shadow_pages[0]);
  590. err_out_shadow1:
  591. kfree(vcpu_e500->shadow_tlb[1]);
  592. err_out_guest1:
  593. kfree(vcpu_e500->guest_tlb[1]);
  594. err_out_shadow0:
  595. kfree(vcpu_e500->shadow_tlb[0]);
  596. err_out_guest0:
  597. kfree(vcpu_e500->guest_tlb[0]);
  598. err_out:
  599. return -1;
  600. }
  601. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  602. {
  603. kfree(vcpu_e500->shadow_pages[1]);
  604. kfree(vcpu_e500->shadow_pages[0]);
  605. kfree(vcpu_e500->shadow_tlb[1]);
  606. kfree(vcpu_e500->guest_tlb[1]);
  607. kfree(vcpu_e500->shadow_tlb[0]);
  608. kfree(vcpu_e500->guest_tlb[0]);
  609. }