e500_mmu_host.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008-2013 Freescale Semiconductor, Inc. All rights reserved.
  4. *
  5. * Author: Yu Liu, yu.liu@freescale.com
  6. * Scott Wood, scottwood@freescale.com
  7. * Ashish Kalra, ashish.kalra@freescale.com
  8. * Varun Sethi, varun.sethi@freescale.com
  9. * Alexander Graf, agraf@suse.de
  10. *
  11. * Description:
  12. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  13. * by Hollis Blanchard <hollisb@us.ibm.com>.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/highmem.h>
  22. #include <linux/log2.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/sched/mm.h>
  25. #include <linux/rwsem.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/hugetlb.h>
  28. #include <asm/kvm_ppc.h>
  29. #include <asm/pte-walk.h>
  30. #include "e500.h"
  31. #include "timing.h"
  32. #include "e500_mmu_host.h"
  33. #include "trace_booke.h"
  34. #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
  35. static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
  36. static inline unsigned int tlb1_max_shadow_size(void)
  37. {
  38. /* reserve one entry for magic page */
  39. return host_tlb_params[1].entries - tlbcam_index - 1;
  40. }
  41. static inline u32 e500_shadow_mas3_attrib(u32 mas3, bool writable, int usermode)
  42. {
  43. /* Mask off reserved bits. */
  44. mas3 &= MAS3_ATTRIB_MASK;
  45. if (!writable)
  46. mas3 &= ~(MAS3_UW|MAS3_SW);
  47. #ifndef CONFIG_KVM_BOOKE_HV
  48. if (!usermode) {
  49. /* Guest is in supervisor mode,
  50. * so we need to translate guest
  51. * supervisor permissions into user permissions. */
  52. mas3 &= ~E500_TLB_USER_PERM_MASK;
  53. mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
  54. }
  55. mas3 |= E500_TLB_SUPER_PERM_MASK;
  56. #endif
  57. return mas3;
  58. }
  59. /*
  60. * writing shadow tlb entry to host TLB
  61. */
  62. static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
  63. uint32_t mas0,
  64. uint32_t lpid)
  65. {
  66. unsigned long flags;
  67. local_irq_save(flags);
  68. mtspr(SPRN_MAS0, mas0);
  69. mtspr(SPRN_MAS1, stlbe->mas1);
  70. mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
  71. mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
  72. mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
  73. #ifdef CONFIG_KVM_BOOKE_HV
  74. mtspr(SPRN_MAS8, MAS8_TGS | get_thread_specific_lpid(lpid));
  75. #endif
  76. asm volatile("isync; tlbwe" : : : "memory");
  77. #ifdef CONFIG_KVM_BOOKE_HV
  78. /* Must clear mas8 for other host tlbwe's */
  79. mtspr(SPRN_MAS8, 0);
  80. isync();
  81. #endif
  82. local_irq_restore(flags);
  83. trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
  84. stlbe->mas2, stlbe->mas7_3);
  85. }
  86. /*
  87. * Acquire a mas0 with victim hint, as if we just took a TLB miss.
  88. *
  89. * We don't care about the address we're searching for, other than that it's
  90. * in the right set and is not present in the TLB. Using a zero PID and a
  91. * userspace address means we don't have to set and then restore MAS5, or
  92. * calculate a proper MAS6 value.
  93. */
  94. static u32 get_host_mas0(unsigned long eaddr)
  95. {
  96. unsigned long flags;
  97. u32 mas0;
  98. u32 mas4;
  99. local_irq_save(flags);
  100. mtspr(SPRN_MAS6, 0);
  101. mas4 = mfspr(SPRN_MAS4);
  102. mtspr(SPRN_MAS4, mas4 & ~MAS4_TLBSEL_MASK);
  103. asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
  104. mas0 = mfspr(SPRN_MAS0);
  105. mtspr(SPRN_MAS4, mas4);
  106. local_irq_restore(flags);
  107. return mas0;
  108. }
  109. /* sesel is for tlb1 only */
  110. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  111. int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
  112. {
  113. u32 mas0;
  114. if (tlbsel == 0) {
  115. mas0 = get_host_mas0(stlbe->mas2);
  116. __write_host_tlbe(stlbe, mas0, vcpu_e500->vcpu.kvm->arch.lpid);
  117. } else {
  118. __write_host_tlbe(stlbe,
  119. MAS0_TLBSEL(1) |
  120. MAS0_ESEL(to_htlb1_esel(sesel)),
  121. vcpu_e500->vcpu.kvm->arch.lpid);
  122. }
  123. }
  124. /* sesel is for tlb1 only */
  125. static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  126. struct kvm_book3e_206_tlb_entry *gtlbe,
  127. struct kvm_book3e_206_tlb_entry *stlbe,
  128. int stlbsel, int sesel)
  129. {
  130. int stid;
  131. preempt_disable();
  132. stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
  133. stlbe->mas1 |= MAS1_TID(stid);
  134. write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
  135. preempt_enable();
  136. }
  137. #ifdef CONFIG_KVM_E500V2
  138. /* XXX should be a hook in the gva2hpa translation */
  139. void kvmppc_map_magic(struct kvm_vcpu *vcpu)
  140. {
  141. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  142. struct kvm_book3e_206_tlb_entry magic;
  143. ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
  144. unsigned int stid;
  145. kvm_pfn_t pfn;
  146. pfn = (kvm_pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
  147. get_page(pfn_to_page(pfn));
  148. preempt_disable();
  149. stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
  150. magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
  151. MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  152. magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
  153. magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  154. MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
  155. magic.mas8 = 0;
  156. __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index), 0);
  157. preempt_enable();
  158. }
  159. #endif
  160. void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
  161. int esel)
  162. {
  163. struct kvm_book3e_206_tlb_entry *gtlbe =
  164. get_entry(vcpu_e500, tlbsel, esel);
  165. struct tlbe_priv *tlbe = &vcpu_e500->gtlb_priv[tlbsel][esel];
  166. /* Don't bother with unmapped entries */
  167. if (!(tlbe->flags & E500_TLB_VALID)) {
  168. WARN(tlbe->flags & (E500_TLB_BITMAP | E500_TLB_TLB0),
  169. "%s: flags %x\n", __func__, tlbe->flags);
  170. WARN_ON(tlbsel == 1 && vcpu_e500->g2h_tlb1_map[esel]);
  171. }
  172. if (tlbsel == 1 && tlbe->flags & E500_TLB_BITMAP) {
  173. u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
  174. int hw_tlb_indx;
  175. unsigned long flags;
  176. local_irq_save(flags);
  177. while (tmp) {
  178. hw_tlb_indx = __ilog2_u64(tmp & -tmp);
  179. mtspr(SPRN_MAS0,
  180. MAS0_TLBSEL(1) |
  181. MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
  182. mtspr(SPRN_MAS1, 0);
  183. asm volatile("tlbwe");
  184. vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
  185. tmp &= tmp - 1;
  186. }
  187. mb();
  188. vcpu_e500->g2h_tlb1_map[esel] = 0;
  189. tlbe->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
  190. local_irq_restore(flags);
  191. }
  192. if (tlbsel == 1 && tlbe->flags & E500_TLB_TLB0) {
  193. /*
  194. * TLB1 entry is backed by 4k pages. This should happen
  195. * rarely and is not worth optimizing. Invalidate everything.
  196. */
  197. kvmppc_e500_tlbil_all(vcpu_e500);
  198. tlbe->flags &= ~(E500_TLB_TLB0 | E500_TLB_VALID);
  199. }
  200. /*
  201. * If TLB entry is still valid then it's a TLB0 entry, and thus
  202. * backed by at most one host tlbe per shadow pid
  203. */
  204. if (tlbe->flags & E500_TLB_VALID)
  205. kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
  206. /* Mark the TLB as not backed by the host anymore */
  207. tlbe->flags = 0;
  208. }
  209. static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
  210. {
  211. return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
  212. }
  213. static inline void kvmppc_e500_tlbe_setup(struct tlbe_priv *tlbe,
  214. struct kvm_book3e_206_tlb_entry *gtlbe,
  215. kvm_pfn_t pfn, unsigned int wimg,
  216. bool writable)
  217. {
  218. tlbe->pfn = pfn;
  219. tlbe->flags = E500_TLB_VALID;
  220. if (writable)
  221. tlbe->flags |= E500_TLB_WRITABLE;
  222. /* Use guest supplied MAS2_G and MAS2_E */
  223. tlbe->flags |= (gtlbe->mas2 & MAS2_ATTRIB_MASK) | wimg;
  224. }
  225. static inline void kvmppc_e500_tlbe_release(struct tlbe_priv *tlbe)
  226. {
  227. if (tlbe->flags & E500_TLB_VALID) {
  228. /* FIXME: don't log bogus pfn for TLB1 */
  229. trace_kvm_booke206_ref_release(tlbe->pfn, tlbe->flags);
  230. tlbe->flags = 0;
  231. }
  232. }
  233. static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
  234. {
  235. if (vcpu_e500->g2h_tlb1_map)
  236. memset(vcpu_e500->g2h_tlb1_map, 0,
  237. sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
  238. if (vcpu_e500->h2g_tlb1_rmap)
  239. memset(vcpu_e500->h2g_tlb1_rmap, 0,
  240. sizeof(unsigned int) * host_tlb_params[1].entries);
  241. }
  242. static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
  243. {
  244. int tlbsel;
  245. int i;
  246. for (tlbsel = 0; tlbsel <= 1; tlbsel++) {
  247. for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++)
  248. kvmppc_e500_tlbe_release(&vcpu_e500->gtlb_priv[tlbsel][i]);
  249. }
  250. }
  251. void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
  252. {
  253. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  254. kvmppc_e500_tlbil_all(vcpu_e500);
  255. clear_tlb_privs(vcpu_e500);
  256. clear_tlb1_bitmap(vcpu_e500);
  257. }
  258. /* TID must be supplied by the caller */
  259. static void kvmppc_e500_setup_stlbe(
  260. struct kvm_vcpu *vcpu,
  261. struct kvm_book3e_206_tlb_entry *gtlbe,
  262. int tsize, struct tlbe_priv *tlbe, u64 gvaddr,
  263. struct kvm_book3e_206_tlb_entry *stlbe)
  264. {
  265. kvm_pfn_t pfn = tlbe->pfn;
  266. u32 pr = vcpu->arch.shared->msr & MSR_PR;
  267. bool writable = !!(tlbe->flags & E500_TLB_WRITABLE);
  268. BUG_ON(!(tlbe->flags & E500_TLB_VALID));
  269. /* Force IPROT=0 for all guest mappings. */
  270. stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
  271. stlbe->mas2 = (gvaddr & MAS2_EPN) | (tlbe->flags & E500_TLB_MAS2_ATTR);
  272. stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  273. e500_shadow_mas3_attrib(gtlbe->mas7_3, writable, pr);
  274. }
  275. static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  276. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  277. int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
  278. struct tlbe_priv *tlbe)
  279. {
  280. struct kvm_memory_slot *slot;
  281. unsigned int psize;
  282. unsigned long pfn;
  283. struct page *page = NULL;
  284. unsigned long hva;
  285. int tsize = BOOK3E_PAGESZ_4K;
  286. int ret = 0;
  287. unsigned long mmu_seq;
  288. struct kvm *kvm = vcpu_e500->vcpu.kvm;
  289. pte_t *ptep;
  290. unsigned int wimg = 0;
  291. pgd_t *pgdir;
  292. unsigned long flags;
  293. bool writable = false;
  294. /* used to check for invalidations in progress */
  295. mmu_seq = kvm->mmu_invalidate_seq;
  296. smp_rmb();
  297. /*
  298. * Translate guest physical to true physical, acquiring
  299. * a page reference if it is normal, non-reserved memory.
  300. *
  301. * gfn_to_memslot() must succeed because otherwise we wouldn't
  302. * have gotten this far. Eventually we should just pass the slot
  303. * pointer through from the first lookup.
  304. */
  305. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  306. hva = gfn_to_hva_memslot(slot, gfn);
  307. pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, &writable, &page);
  308. if (is_error_noslot_pfn(pfn)) {
  309. if (printk_ratelimit())
  310. pr_err("%s: real page not found for gfn %lx\n",
  311. __func__, (long)gfn);
  312. return -EINVAL;
  313. }
  314. spin_lock(&kvm->mmu_lock);
  315. if (mmu_invalidate_retry(kvm, mmu_seq)) {
  316. ret = -EAGAIN;
  317. goto out;
  318. }
  319. pgdir = vcpu_e500->vcpu.arch.pgdir;
  320. /*
  321. * We are just looking at the wimg bits, so we don't
  322. * care much about the trans splitting bit.
  323. * We are holding kvm->mmu_lock so a notifier invalidate
  324. * can't run hence pfn won't change.
  325. */
  326. local_irq_save(flags);
  327. ptep = find_linux_pte(pgdir, hva, NULL, &psize);
  328. if (ptep) {
  329. pte_t pte = READ_ONCE(*ptep);
  330. if (pte_present(pte)) {
  331. wimg = (pte_val(pte) >> PTE_WIMGE_SHIFT) &
  332. MAS2_WIMGE_MASK;
  333. } else {
  334. local_irq_restore(flags);
  335. pr_err_ratelimited("%s: pte not present: gfn %lx,pfn %lx\n",
  336. __func__, (long)gfn, pfn);
  337. ret = -EINVAL;
  338. goto out;
  339. }
  340. }
  341. local_irq_restore(flags);
  342. if (psize && tlbsel == 1) {
  343. unsigned long psize_pages, tsize_pages;
  344. unsigned long start, end;
  345. unsigned long slot_start, slot_end;
  346. psize_pages = 1UL << (psize - PAGE_SHIFT);
  347. start = pfn & ~(psize_pages - 1);
  348. end = start + psize_pages;
  349. slot_start = pfn - (gfn - slot->base_gfn);
  350. slot_end = slot_start + slot->npages;
  351. if (start < slot_start)
  352. start = slot_start;
  353. if (end > slot_end)
  354. end = slot_end;
  355. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  356. MAS1_TSIZE_SHIFT;
  357. /*
  358. * Any page size that doesn't satisfy the host mapping
  359. * will fail the start and end tests.
  360. */
  361. tsize = min(psize - PAGE_SHIFT + BOOK3E_PAGESZ_4K, tsize);
  362. /*
  363. * e500 doesn't implement the lowest tsize bit,
  364. * or 1K pages.
  365. */
  366. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  367. /*
  368. * Now find the largest tsize (up to what the guest
  369. * requested) that will cover gfn, stay within the
  370. * range, and for which gfn and pfn are mutually
  371. * aligned.
  372. */
  373. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  374. unsigned long gfn_start, gfn_end;
  375. tsize_pages = 1UL << (tsize - 2);
  376. gfn_start = gfn & ~(tsize_pages - 1);
  377. gfn_end = gfn_start + tsize_pages;
  378. if (gfn_start + pfn - gfn < start)
  379. continue;
  380. if (gfn_end + pfn - gfn > end)
  381. continue;
  382. if ((gfn & (tsize_pages - 1)) !=
  383. (pfn & (tsize_pages - 1)))
  384. continue;
  385. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  386. pfn &= ~(tsize_pages - 1);
  387. break;
  388. }
  389. }
  390. kvmppc_e500_tlbe_setup(tlbe, gtlbe, pfn, wimg, writable);
  391. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  392. tlbe, gvaddr, stlbe);
  393. writable = tlbe_is_writable(stlbe);
  394. /* Clear i-cache for new pages */
  395. kvmppc_mmu_flush_icache(pfn);
  396. out:
  397. kvm_release_faultin_page(kvm, page, !!ret, writable);
  398. spin_unlock(&kvm->mmu_lock);
  399. return ret;
  400. }
  401. /* XXX only map the one-one case, for now use TLB0 */
  402. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, int esel,
  403. struct kvm_book3e_206_tlb_entry *stlbe)
  404. {
  405. struct kvm_book3e_206_tlb_entry *gtlbe;
  406. struct tlbe_priv *tlbe;
  407. int stlbsel = 0;
  408. int sesel = 0;
  409. int r;
  410. gtlbe = get_entry(vcpu_e500, 0, esel);
  411. tlbe = &vcpu_e500->gtlb_priv[0][esel];
  412. r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  413. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  414. gtlbe, 0, stlbe, tlbe);
  415. if (r)
  416. return r;
  417. write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
  418. return 0;
  419. }
  420. static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
  421. struct tlbe_priv *tlbe,
  422. int esel)
  423. {
  424. unsigned int sesel = vcpu_e500->host_tlb1_nv++;
  425. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  426. vcpu_e500->host_tlb1_nv = 0;
  427. if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
  428. unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
  429. vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
  430. }
  431. vcpu_e500->gtlb_priv[1][esel].flags |= E500_TLB_BITMAP;
  432. vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
  433. vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
  434. WARN_ON(!(tlbe->flags & E500_TLB_VALID));
  435. return sesel;
  436. }
  437. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  438. * the shadow TLB. */
  439. /* For both one-one and one-to-many */
  440. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  441. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  442. struct kvm_book3e_206_tlb_entry *stlbe, int esel)
  443. {
  444. struct tlbe_priv *tlbe = &vcpu_e500->gtlb_priv[1][esel];
  445. int sesel;
  446. int r;
  447. r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
  448. tlbe);
  449. if (r)
  450. return r;
  451. /* Use TLB0 when we can only map a page with 4k */
  452. if (get_tlb_tsize(stlbe) == BOOK3E_PAGESZ_4K) {
  453. vcpu_e500->gtlb_priv[1][esel].flags |= E500_TLB_TLB0;
  454. write_stlbe(vcpu_e500, gtlbe, stlbe, 0, 0);
  455. return 0;
  456. }
  457. /* Otherwise map into TLB1 */
  458. sesel = kvmppc_e500_tlb1_map_tlb1(vcpu_e500, tlbe, esel);
  459. write_stlbe(vcpu_e500, gtlbe, stlbe, 1, sesel);
  460. return 0;
  461. }
  462. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  463. unsigned int index)
  464. {
  465. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  466. struct tlbe_priv *priv;
  467. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  468. int tlbsel = tlbsel_of(index);
  469. int esel = esel_of(index);
  470. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  471. switch (tlbsel) {
  472. case 0:
  473. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  474. /* Triggers after clear_tlb_privs or on initial mapping */
  475. if (!(priv->flags & E500_TLB_VALID)) {
  476. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  477. } else {
  478. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  479. priv, eaddr, &stlbe);
  480. write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
  481. }
  482. break;
  483. case 1: {
  484. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  485. kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
  486. esel);
  487. break;
  488. }
  489. default:
  490. BUG();
  491. break;
  492. }
  493. }
  494. #ifdef CONFIG_KVM_BOOKE_HV
  495. int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
  496. enum instruction_fetch_type type, unsigned long *instr)
  497. {
  498. gva_t geaddr;
  499. hpa_t addr;
  500. hfn_t pfn;
  501. hva_t eaddr;
  502. u32 mas1, mas2, mas3;
  503. u64 mas7_mas3;
  504. struct page *page;
  505. unsigned int addr_space, psize_shift;
  506. bool pr;
  507. unsigned long flags;
  508. /* Search TLB for guest pc to get the real address */
  509. geaddr = kvmppc_get_pc(vcpu);
  510. addr_space = (vcpu->arch.shared->msr & MSR_IS) >> MSR_IR_LG;
  511. local_irq_save(flags);
  512. mtspr(SPRN_MAS6, (vcpu->arch.pid << MAS6_SPID_SHIFT) | addr_space);
  513. mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(vcpu));
  514. asm volatile("tlbsx 0, %[geaddr]\n" : :
  515. [geaddr] "r" (geaddr));
  516. mtspr(SPRN_MAS5, 0);
  517. mtspr(SPRN_MAS8, 0);
  518. mas1 = mfspr(SPRN_MAS1);
  519. mas2 = mfspr(SPRN_MAS2);
  520. mas3 = mfspr(SPRN_MAS3);
  521. #ifdef CONFIG_64BIT
  522. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  523. #else
  524. mas7_mas3 = ((u64)mfspr(SPRN_MAS7) << 32) | mas3;
  525. #endif
  526. local_irq_restore(flags);
  527. /*
  528. * If the TLB entry for guest pc was evicted, return to the guest.
  529. * There are high chances to find a valid TLB entry next time.
  530. */
  531. if (!(mas1 & MAS1_VALID))
  532. return EMULATE_AGAIN;
  533. /*
  534. * Another thread may rewrite the TLB entry in parallel, don't
  535. * execute from the address if the execute permission is not set
  536. */
  537. pr = vcpu->arch.shared->msr & MSR_PR;
  538. if (unlikely((pr && !(mas3 & MAS3_UX)) ||
  539. (!pr && !(mas3 & MAS3_SX)))) {
  540. pr_err_ratelimited(
  541. "%s: Instruction emulation from guest address %08lx without execute permission\n",
  542. __func__, geaddr);
  543. return EMULATE_AGAIN;
  544. }
  545. /*
  546. * The real address will be mapped by a cacheable, memory coherent,
  547. * write-back page. Check for mismatches when LRAT is used.
  548. */
  549. if (has_feature(vcpu, VCPU_FTR_MMU_V2) &&
  550. unlikely((mas2 & MAS2_I) || (mas2 & MAS2_W) || !(mas2 & MAS2_M))) {
  551. pr_err_ratelimited(
  552. "%s: Instruction emulation from guest address %08lx mismatches storage attributes\n",
  553. __func__, geaddr);
  554. return EMULATE_AGAIN;
  555. }
  556. /* Get pfn */
  557. psize_shift = MAS1_GET_TSIZE(mas1) + 10;
  558. addr = (mas7_mas3 & (~0ULL << psize_shift)) |
  559. (geaddr & ((1ULL << psize_shift) - 1ULL));
  560. pfn = addr >> PAGE_SHIFT;
  561. /* Guard against emulation from devices area */
  562. if (unlikely(!page_is_ram(pfn))) {
  563. pr_err_ratelimited("%s: Instruction emulation from non-RAM host address %08llx is not supported\n",
  564. __func__, addr);
  565. return EMULATE_AGAIN;
  566. }
  567. /* Map a page and get guest's instruction */
  568. page = pfn_to_page(pfn);
  569. eaddr = (unsigned long)kmap_atomic(page);
  570. *instr = *(u32 *)(eaddr | (unsigned long)(addr & ~PAGE_MASK));
  571. kunmap_atomic((u32 *)eaddr);
  572. return EMULATE_DONE;
  573. }
  574. #else
  575. int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
  576. enum instruction_fetch_type type, unsigned long *instr)
  577. {
  578. return EMULATE_AGAIN;
  579. }
  580. #endif
  581. /************* MMU Notifiers *************/
  582. static bool kvm_e500_mmu_unmap_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  583. {
  584. /*
  585. * Flush all shadow tlb entries everywhere. This is slow, but
  586. * we are 100% sure that we catch the to be unmapped page
  587. */
  588. return true;
  589. }
  590. bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
  591. {
  592. return kvm_e500_mmu_unmap_gfn(kvm, range);
  593. }
  594. bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  595. {
  596. /* XXX could be more clever ;) */
  597. return false;
  598. }
  599. bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  600. {
  601. /* XXX could be more clever ;) */
  602. return false;
  603. }
  604. /*****************************************/
  605. int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  606. {
  607. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  608. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  609. /*
  610. * This should never happen on real e500 hardware, but is
  611. * architecturally possible -- e.g. in some weird nested
  612. * virtualization case.
  613. */
  614. if (host_tlb_params[0].entries == 0 ||
  615. host_tlb_params[1].entries == 0) {
  616. pr_err("%s: need to know host tlb size\n", __func__);
  617. return -ENODEV;
  618. }
  619. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  620. TLBnCFG_ASSOC_SHIFT;
  621. host_tlb_params[1].ways = host_tlb_params[1].entries;
  622. if (!is_power_of_2(host_tlb_params[0].entries) ||
  623. !is_power_of_2(host_tlb_params[0].ways) ||
  624. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  625. host_tlb_params[0].ways == 0) {
  626. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  627. __func__, host_tlb_params[0].entries,
  628. host_tlb_params[0].ways);
  629. return -ENODEV;
  630. }
  631. host_tlb_params[0].sets =
  632. host_tlb_params[0].entries / host_tlb_params[0].ways;
  633. host_tlb_params[1].sets = 1;
  634. vcpu_e500->h2g_tlb1_rmap = kcalloc(host_tlb_params[1].entries,
  635. sizeof(*vcpu_e500->h2g_tlb1_rmap),
  636. GFP_KERNEL);
  637. if (!vcpu_e500->h2g_tlb1_rmap)
  638. return -EINVAL;
  639. return 0;
  640. }
  641. void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  642. {
  643. kfree(vcpu_e500->h2g_tlb1_rmap);
  644. }