e500_mmu_host.c 21 KB

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