e500_tlb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. #ifdef CONFIG_SMP
  86. return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
  87. #else
  88. return mas2 & MAS2_ATTRIB_MASK;
  89. #endif
  90. }
  91. /*
  92. * writing shadow tlb entry to host TLB
  93. */
  94. static inline void __write_host_tlbe(struct tlbe *stlbe)
  95. {
  96. mtspr(SPRN_MAS1, stlbe->mas1);
  97. mtspr(SPRN_MAS2, stlbe->mas2);
  98. mtspr(SPRN_MAS3, stlbe->mas3);
  99. mtspr(SPRN_MAS7, stlbe->mas7);
  100. __asm__ __volatile__ ("tlbwe\n" : : );
  101. }
  102. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  103. int tlbsel, int esel)
  104. {
  105. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  106. local_irq_disable();
  107. if (tlbsel == 0) {
  108. __write_host_tlbe(stlbe);
  109. } else {
  110. unsigned register mas0;
  111. mas0 = mfspr(SPRN_MAS0);
  112. mtspr(SPRN_MAS0, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(esel)));
  113. __write_host_tlbe(stlbe);
  114. mtspr(SPRN_MAS0, mas0);
  115. }
  116. local_irq_enable();
  117. }
  118. void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
  119. {
  120. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  121. int i;
  122. unsigned register mas0;
  123. /* Load all valid TLB1 entries to reduce guest tlb miss fault */
  124. local_irq_disable();
  125. mas0 = mfspr(SPRN_MAS0);
  126. for (i = 0; i < tlb1_max_shadow_size(); i++) {
  127. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
  128. if (get_tlb_v(stlbe)) {
  129. mtspr(SPRN_MAS0, MAS0_TLBSEL(1)
  130. | MAS0_ESEL(to_htlb1_esel(i)));
  131. __write_host_tlbe(stlbe);
  132. }
  133. }
  134. mtspr(SPRN_MAS0, mas0);
  135. local_irq_enable();
  136. }
  137. void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
  138. {
  139. _tlbil_all();
  140. }
  141. /* Search the guest TLB for a matching entry. */
  142. static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
  143. gva_t eaddr, int tlbsel, unsigned int pid, int as)
  144. {
  145. int i;
  146. /* XXX Replace loop with fancy data structures. */
  147. for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
  148. struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
  149. unsigned int tid;
  150. if (eaddr < get_tlb_eaddr(tlbe))
  151. continue;
  152. if (eaddr > get_tlb_end(tlbe))
  153. continue;
  154. tid = get_tlb_tid(tlbe);
  155. if (tid && (tid != pid))
  156. continue;
  157. if (!get_tlb_v(tlbe))
  158. continue;
  159. if (get_tlb_ts(tlbe) != as && as != -1)
  160. continue;
  161. return i;
  162. }
  163. return -1;
  164. }
  165. static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
  166. int tlbsel, int esel)
  167. {
  168. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  169. struct page *page = vcpu_e500->shadow_pages[tlbsel][esel];
  170. if (page) {
  171. vcpu_e500->shadow_pages[tlbsel][esel] = NULL;
  172. if (get_tlb_v(stlbe)) {
  173. if (tlbe_is_writable(stlbe))
  174. kvm_release_page_dirty(page);
  175. else
  176. kvm_release_page_clean(page);
  177. }
  178. }
  179. }
  180. static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
  181. int tlbsel, int esel)
  182. {
  183. struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
  184. kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
  185. stlbe->mas1 = 0;
  186. KVMTRACE_5D(STLB_INVAL, &vcpu_e500->vcpu, index_of(tlbsel, esel),
  187. stlbe->mas1, stlbe->mas2, stlbe->mas3, stlbe->mas7,
  188. handler);
  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 >> 8) & 0xf;
  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(BOOKE_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. KVMTRACE_5D(STLB_WRITE, &vcpu_e500->vcpu, index_of(tlbsel, esel),
  265. stlbe->mas1, stlbe->mas2, stlbe->mas3, stlbe->mas7,
  266. handler);
  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) ? vcpu->arch.gpr[ra] : 0) + vcpu->arch.gpr[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 = vcpu->arch.gpr[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. KVMTRACE_5D(GTLB_WRITE, vcpu, vcpu_e500->mas0,
  436. gtlbe->mas1, gtlbe->mas2, gtlbe->mas3, gtlbe->mas7,
  437. handler);
  438. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  439. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  440. switch (tlbsel) {
  441. case 0:
  442. /* TLB0 */
  443. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  444. gtlbe->mas1 |= MAS1_TSIZE(BOOKE_PAGESZ_4K);
  445. stlbsel = 0;
  446. sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
  447. break;
  448. case 1:
  449. /* TLB1 */
  450. eaddr = get_tlb_eaddr(gtlbe);
  451. raddr = get_tlb_raddr(gtlbe);
  452. /* Create a 4KB mapping on the host.
  453. * If the guest wanted a large page,
  454. * only the first 4KB is mapped here and the rest
  455. * are mapped on the fly. */
  456. stlbsel = 1;
  457. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  458. raddr >> PAGE_SHIFT, gtlbe);
  459. break;
  460. default:
  461. BUG();
  462. }
  463. write_host_tlbe(vcpu_e500, stlbsel, sesel);
  464. }
  465. return EMULATE_DONE;
  466. }
  467. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  468. {
  469. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  470. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  471. }
  472. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  473. {
  474. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  475. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  476. }
  477. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  478. {
  479. unsigned int as = !!(vcpu->arch.msr & MSR_IS);
  480. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  481. }
  482. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  483. {
  484. unsigned int as = !!(vcpu->arch.msr & MSR_DS);
  485. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  486. }
  487. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  488. gva_t eaddr)
  489. {
  490. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  491. struct tlbe *gtlbe =
  492. &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
  493. u64 pgmask = get_tlb_bytes(gtlbe) - 1;
  494. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  495. }
  496. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  497. {
  498. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  499. int tlbsel, i;
  500. for (tlbsel = 0; tlbsel < 2; tlbsel++)
  501. for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
  502. kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
  503. /* discard all guest mapping */
  504. _tlbil_all();
  505. }
  506. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  507. unsigned int index)
  508. {
  509. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  510. int tlbsel = tlbsel_of(index);
  511. int esel = esel_of(index);
  512. int stlbsel, sesel;
  513. switch (tlbsel) {
  514. case 0:
  515. stlbsel = 0;
  516. sesel = esel;
  517. break;
  518. case 1: {
  519. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  520. struct tlbe *gtlbe
  521. = &vcpu_e500->guest_tlb[tlbsel][esel];
  522. stlbsel = 1;
  523. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
  524. break;
  525. }
  526. default:
  527. BUG();
  528. break;
  529. }
  530. write_host_tlbe(vcpu_e500, stlbsel, sesel);
  531. }
  532. int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  533. gva_t eaddr, unsigned int pid, int as)
  534. {
  535. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  536. int esel, tlbsel;
  537. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  538. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  539. if (esel >= 0)
  540. return index_of(tlbsel, esel);
  541. }
  542. return -1;
  543. }
  544. void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  545. {
  546. struct tlbe *tlbe;
  547. /* Insert large initial mapping for guest. */
  548. tlbe = &vcpu_e500->guest_tlb[1][0];
  549. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_256M);
  550. tlbe->mas2 = 0;
  551. tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
  552. tlbe->mas7 = 0;
  553. /* 4K map for serial output. Used by kernel wrapper. */
  554. tlbe = &vcpu_e500->guest_tlb[1][1];
  555. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_4K);
  556. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  557. tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  558. tlbe->mas7 = 0;
  559. }
  560. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  561. {
  562. tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
  563. vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
  564. vcpu_e500->guest_tlb[0] =
  565. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  566. if (vcpu_e500->guest_tlb[0] == NULL)
  567. goto err_out;
  568. vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
  569. vcpu_e500->shadow_tlb[0] =
  570. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  571. if (vcpu_e500->shadow_tlb[0] == NULL)
  572. goto err_out_guest0;
  573. vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
  574. vcpu_e500->guest_tlb[1] =
  575. kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
  576. if (vcpu_e500->guest_tlb[1] == NULL)
  577. goto err_out_shadow0;
  578. vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
  579. vcpu_e500->shadow_tlb[1] =
  580. kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
  581. if (vcpu_e500->shadow_tlb[1] == NULL)
  582. goto err_out_guest1;
  583. vcpu_e500->shadow_pages[0] = (struct page **)
  584. kzalloc(sizeof(struct page *) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
  585. if (vcpu_e500->shadow_pages[0] == NULL)
  586. goto err_out_shadow1;
  587. vcpu_e500->shadow_pages[1] = (struct page **)
  588. kzalloc(sizeof(struct page *) * tlb1_entry_num, GFP_KERNEL);
  589. if (vcpu_e500->shadow_pages[1] == NULL)
  590. goto err_out_page0;
  591. return 0;
  592. err_out_page0:
  593. kfree(vcpu_e500->shadow_pages[0]);
  594. err_out_shadow1:
  595. kfree(vcpu_e500->shadow_tlb[1]);
  596. err_out_guest1:
  597. kfree(vcpu_e500->guest_tlb[1]);
  598. err_out_shadow0:
  599. kfree(vcpu_e500->shadow_tlb[0]);
  600. err_out_guest0:
  601. kfree(vcpu_e500->guest_tlb[0]);
  602. err_out:
  603. return -1;
  604. }
  605. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  606. {
  607. kfree(vcpu_e500->shadow_pages[1]);
  608. kfree(vcpu_e500->shadow_pages[0]);
  609. kfree(vcpu_e500->shadow_tlb[1]);
  610. kfree(vcpu_e500->guest_tlb[1]);
  611. kfree(vcpu_e500->shadow_tlb[0]);
  612. kfree(vcpu_e500->guest_tlb[0]);
  613. }