e500_mmu_host.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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, [email protected]
  6. * Scott Wood, [email protected]
  7. * Ashish Kalra, [email protected]
  8. * Varun Sethi, [email protected]
  9. * Alexander Graf, [email protected]
  10. *
  11. * Description:
  12. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  13. * by Hollis Blanchard <[email protected]>.
  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 void 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. /* Mark the page accessed */
  220. kvm_set_pfn_accessed(pfn);
  221. if (tlbe_is_writable(gtlbe))
  222. kvm_set_pfn_dirty(pfn);
  223. }
  224. static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
  225. {
  226. if (ref->flags & E500_TLB_VALID) {
  227. /* FIXME: don't log bogus pfn for TLB1 */
  228. trace_kvm_booke206_ref_release(ref->pfn, ref->flags);
  229. ref->flags = 0;
  230. }
  231. }
  232. static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
  233. {
  234. if (vcpu_e500->g2h_tlb1_map)
  235. memset(vcpu_e500->g2h_tlb1_map, 0,
  236. sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
  237. if (vcpu_e500->h2g_tlb1_rmap)
  238. memset(vcpu_e500->h2g_tlb1_rmap, 0,
  239. sizeof(unsigned int) * host_tlb_params[1].entries);
  240. }
  241. static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
  242. {
  243. int tlbsel;
  244. int i;
  245. for (tlbsel = 0; tlbsel <= 1; tlbsel++) {
  246. for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
  247. struct tlbe_ref *ref =
  248. &vcpu_e500->gtlb_priv[tlbsel][i].ref;
  249. kvmppc_e500_ref_release(ref);
  250. }
  251. }
  252. }
  253. void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
  254. {
  255. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  256. kvmppc_e500_tlbil_all(vcpu_e500);
  257. clear_tlb_privs(vcpu_e500);
  258. clear_tlb1_bitmap(vcpu_e500);
  259. }
  260. /* TID must be supplied by the caller */
  261. static void kvmppc_e500_setup_stlbe(
  262. struct kvm_vcpu *vcpu,
  263. struct kvm_book3e_206_tlb_entry *gtlbe,
  264. int tsize, struct tlbe_ref *ref, u64 gvaddr,
  265. struct kvm_book3e_206_tlb_entry *stlbe)
  266. {
  267. kvm_pfn_t pfn = ref->pfn;
  268. u32 pr = vcpu->arch.shared->msr & MSR_PR;
  269. BUG_ON(!(ref->flags & E500_TLB_VALID));
  270. /* Force IPROT=0 for all guest mappings. */
  271. stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
  272. stlbe->mas2 = (gvaddr & MAS2_EPN) | (ref->flags & E500_TLB_MAS2_ATTR);
  273. stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  274. e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
  275. }
  276. static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  277. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  278. int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
  279. struct tlbe_ref *ref)
  280. {
  281. struct kvm_memory_slot *slot;
  282. unsigned long pfn = 0; /* silence GCC warning */
  283. unsigned long hva;
  284. int pfnmap = 0;
  285. int tsize = BOOK3E_PAGESZ_4K;
  286. int ret = 0;
  287. unsigned long mmu_seq;
  288. struct kvm *kvm = vcpu_e500->vcpu.kvm;
  289. unsigned long tsize_pages = 0;
  290. pte_t *ptep;
  291. unsigned int wimg = 0;
  292. pgd_t *pgdir;
  293. unsigned long flags;
  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. if (tlbsel == 1) {
  308. struct vm_area_struct *vma;
  309. mmap_read_lock(kvm->mm);
  310. vma = find_vma(kvm->mm, hva);
  311. if (vma && hva >= vma->vm_start &&
  312. (vma->vm_flags & VM_PFNMAP)) {
  313. /*
  314. * This VMA is a physically contiguous region (e.g.
  315. * /dev/mem) that bypasses normal Linux page
  316. * management. Find the overlap between the
  317. * vma and the memslot.
  318. */
  319. unsigned long start, end;
  320. unsigned long slot_start, slot_end;
  321. pfnmap = 1;
  322. start = vma->vm_pgoff;
  323. end = start +
  324. vma_pages(vma);
  325. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  326. slot_start = pfn - (gfn - slot->base_gfn);
  327. slot_end = slot_start + slot->npages;
  328. if (start < slot_start)
  329. start = slot_start;
  330. if (end > slot_end)
  331. end = slot_end;
  332. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  333. MAS1_TSIZE_SHIFT;
  334. /*
  335. * e500 doesn't implement the lowest tsize bit,
  336. * or 1K pages.
  337. */
  338. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  339. /*
  340. * Now find the largest tsize (up to what the guest
  341. * requested) that will cover gfn, stay within the
  342. * range, and for which gfn and pfn are mutually
  343. * aligned.
  344. */
  345. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  346. unsigned long gfn_start, gfn_end;
  347. tsize_pages = 1UL << (tsize - 2);
  348. gfn_start = gfn & ~(tsize_pages - 1);
  349. gfn_end = gfn_start + tsize_pages;
  350. if (gfn_start + pfn - gfn < start)
  351. continue;
  352. if (gfn_end + pfn - gfn > end)
  353. continue;
  354. if ((gfn & (tsize_pages - 1)) !=
  355. (pfn & (tsize_pages - 1)))
  356. continue;
  357. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  358. pfn &= ~(tsize_pages - 1);
  359. break;
  360. }
  361. } else if (vma && hva >= vma->vm_start &&
  362. is_vm_hugetlb_page(vma)) {
  363. unsigned long psize = vma_kernel_pagesize(vma);
  364. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  365. MAS1_TSIZE_SHIFT;
  366. /*
  367. * Take the largest page size that satisfies both host
  368. * and guest mapping
  369. */
  370. tsize = min(__ilog2(psize) - 10, tsize);
  371. /*
  372. * e500 doesn't implement the lowest tsize bit,
  373. * or 1K pages.
  374. */
  375. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  376. }
  377. mmap_read_unlock(kvm->mm);
  378. }
  379. if (likely(!pfnmap)) {
  380. tsize_pages = 1UL << (tsize + 10 - PAGE_SHIFT);
  381. pfn = gfn_to_pfn_memslot(slot, gfn);
  382. if (is_error_noslot_pfn(pfn)) {
  383. if (printk_ratelimit())
  384. pr_err("%s: real page not found for gfn %lx\n",
  385. __func__, (long)gfn);
  386. return -EINVAL;
  387. }
  388. /* Align guest and physical address to page map boundaries */
  389. pfn &= ~(tsize_pages - 1);
  390. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  391. }
  392. spin_lock(&kvm->mmu_lock);
  393. if (mmu_invalidate_retry(kvm, mmu_seq)) {
  394. ret = -EAGAIN;
  395. goto out;
  396. }
  397. pgdir = vcpu_e500->vcpu.arch.pgdir;
  398. /*
  399. * We are just looking at the wimg bits, so we don't
  400. * care much about the trans splitting bit.
  401. * We are holding kvm->mmu_lock so a notifier invalidate
  402. * can't run hence pfn won't change.
  403. */
  404. local_irq_save(flags);
  405. ptep = find_linux_pte(pgdir, hva, NULL, NULL);
  406. if (ptep) {
  407. pte_t pte = READ_ONCE(*ptep);
  408. if (pte_present(pte)) {
  409. wimg = (pte_val(pte) >> PTE_WIMGE_SHIFT) &
  410. MAS2_WIMGE_MASK;
  411. local_irq_restore(flags);
  412. } else {
  413. local_irq_restore(flags);
  414. pr_err_ratelimited("%s: pte not present: gfn %lx,pfn %lx\n",
  415. __func__, (long)gfn, pfn);
  416. ret = -EINVAL;
  417. goto out;
  418. }
  419. }
  420. kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg);
  421. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  422. ref, gvaddr, stlbe);
  423. /* Clear i-cache for new pages */
  424. kvmppc_mmu_flush_icache(pfn);
  425. out:
  426. spin_unlock(&kvm->mmu_lock);
  427. /* Drop refcount on page, so that mmu notifiers can clear it */
  428. kvm_release_pfn_clean(pfn);
  429. return ret;
  430. }
  431. /* XXX only map the one-one case, for now use TLB0 */
  432. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, int esel,
  433. struct kvm_book3e_206_tlb_entry *stlbe)
  434. {
  435. struct kvm_book3e_206_tlb_entry *gtlbe;
  436. struct tlbe_ref *ref;
  437. int stlbsel = 0;
  438. int sesel = 0;
  439. int r;
  440. gtlbe = get_entry(vcpu_e500, 0, esel);
  441. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  442. r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  443. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  444. gtlbe, 0, stlbe, ref);
  445. if (r)
  446. return r;
  447. write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
  448. return 0;
  449. }
  450. static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
  451. struct tlbe_ref *ref,
  452. int esel)
  453. {
  454. unsigned int sesel = vcpu_e500->host_tlb1_nv++;
  455. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  456. vcpu_e500->host_tlb1_nv = 0;
  457. if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
  458. unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
  459. vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
  460. }
  461. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
  462. vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
  463. vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
  464. WARN_ON(!(ref->flags & E500_TLB_VALID));
  465. return sesel;
  466. }
  467. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  468. * the shadow TLB. */
  469. /* For both one-one and one-to-many */
  470. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  471. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  472. struct kvm_book3e_206_tlb_entry *stlbe, int esel)
  473. {
  474. struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[1][esel].ref;
  475. int sesel;
  476. int r;
  477. r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
  478. ref);
  479. if (r)
  480. return r;
  481. /* Use TLB0 when we can only map a page with 4k */
  482. if (get_tlb_tsize(stlbe) == BOOK3E_PAGESZ_4K) {
  483. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_TLB0;
  484. write_stlbe(vcpu_e500, gtlbe, stlbe, 0, 0);
  485. return 0;
  486. }
  487. /* Otherwise map into TLB1 */
  488. sesel = kvmppc_e500_tlb1_map_tlb1(vcpu_e500, ref, esel);
  489. write_stlbe(vcpu_e500, gtlbe, stlbe, 1, sesel);
  490. return 0;
  491. }
  492. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  493. unsigned int index)
  494. {
  495. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  496. struct tlbe_priv *priv;
  497. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  498. int tlbsel = tlbsel_of(index);
  499. int esel = esel_of(index);
  500. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  501. switch (tlbsel) {
  502. case 0:
  503. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  504. /* Triggers after clear_tlb_privs or on initial mapping */
  505. if (!(priv->ref.flags & E500_TLB_VALID)) {
  506. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  507. } else {
  508. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  509. &priv->ref, eaddr, &stlbe);
  510. write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
  511. }
  512. break;
  513. case 1: {
  514. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  515. kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
  516. esel);
  517. break;
  518. }
  519. default:
  520. BUG();
  521. break;
  522. }
  523. }
  524. #ifdef CONFIG_KVM_BOOKE_HV
  525. int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
  526. enum instruction_fetch_type type, u32 *instr)
  527. {
  528. gva_t geaddr;
  529. hpa_t addr;
  530. hfn_t pfn;
  531. hva_t eaddr;
  532. u32 mas1, mas2, mas3;
  533. u64 mas7_mas3;
  534. struct page *page;
  535. unsigned int addr_space, psize_shift;
  536. bool pr;
  537. unsigned long flags;
  538. /* Search TLB for guest pc to get the real address */
  539. geaddr = kvmppc_get_pc(vcpu);
  540. addr_space = (vcpu->arch.shared->msr & MSR_IS) >> MSR_IR_LG;
  541. local_irq_save(flags);
  542. mtspr(SPRN_MAS6, (vcpu->arch.pid << MAS6_SPID_SHIFT) | addr_space);
  543. mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(vcpu));
  544. asm volatile("tlbsx 0, %[geaddr]\n" : :
  545. [geaddr] "r" (geaddr));
  546. mtspr(SPRN_MAS5, 0);
  547. mtspr(SPRN_MAS8, 0);
  548. mas1 = mfspr(SPRN_MAS1);
  549. mas2 = mfspr(SPRN_MAS2);
  550. mas3 = mfspr(SPRN_MAS3);
  551. #ifdef CONFIG_64BIT
  552. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  553. #else
  554. mas7_mas3 = ((u64)mfspr(SPRN_MAS7) << 32) | mas3;
  555. #endif
  556. local_irq_restore(flags);
  557. /*
  558. * If the TLB entry for guest pc was evicted, return to the guest.
  559. * There are high chances to find a valid TLB entry next time.
  560. */
  561. if (!(mas1 & MAS1_VALID))
  562. return EMULATE_AGAIN;
  563. /*
  564. * Another thread may rewrite the TLB entry in parallel, don't
  565. * execute from the address if the execute permission is not set
  566. */
  567. pr = vcpu->arch.shared->msr & MSR_PR;
  568. if (unlikely((pr && !(mas3 & MAS3_UX)) ||
  569. (!pr && !(mas3 & MAS3_SX)))) {
  570. pr_err_ratelimited(
  571. "%s: Instruction emulation from guest address %08lx without execute permission\n",
  572. __func__, geaddr);
  573. return EMULATE_AGAIN;
  574. }
  575. /*
  576. * The real address will be mapped by a cacheable, memory coherent,
  577. * write-back page. Check for mismatches when LRAT is used.
  578. */
  579. if (has_feature(vcpu, VCPU_FTR_MMU_V2) &&
  580. unlikely((mas2 & MAS2_I) || (mas2 & MAS2_W) || !(mas2 & MAS2_M))) {
  581. pr_err_ratelimited(
  582. "%s: Instruction emulation from guest address %08lx mismatches storage attributes\n",
  583. __func__, geaddr);
  584. return EMULATE_AGAIN;
  585. }
  586. /* Get pfn */
  587. psize_shift = MAS1_GET_TSIZE(mas1) + 10;
  588. addr = (mas7_mas3 & (~0ULL << psize_shift)) |
  589. (geaddr & ((1ULL << psize_shift) - 1ULL));
  590. pfn = addr >> PAGE_SHIFT;
  591. /* Guard against emulation from devices area */
  592. if (unlikely(!page_is_ram(pfn))) {
  593. pr_err_ratelimited("%s: Instruction emulation from non-RAM host address %08llx is not supported\n",
  594. __func__, addr);
  595. return EMULATE_AGAIN;
  596. }
  597. /* Map a page and get guest's instruction */
  598. page = pfn_to_page(pfn);
  599. eaddr = (unsigned long)kmap_atomic(page);
  600. *instr = *(u32 *)(eaddr | (unsigned long)(addr & ~PAGE_MASK));
  601. kunmap_atomic((u32 *)eaddr);
  602. return EMULATE_DONE;
  603. }
  604. #else
  605. int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
  606. enum instruction_fetch_type type, u32 *instr)
  607. {
  608. return EMULATE_AGAIN;
  609. }
  610. #endif
  611. /************* MMU Notifiers *************/
  612. static bool kvm_e500_mmu_unmap_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  613. {
  614. /*
  615. * Flush all shadow tlb entries everywhere. This is slow, but
  616. * we are 100% sure that we catch the to be unmapped page
  617. */
  618. return true;
  619. }
  620. bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
  621. {
  622. return kvm_e500_mmu_unmap_gfn(kvm, range);
  623. }
  624. bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  625. {
  626. /* XXX could be more clever ;) */
  627. return false;
  628. }
  629. bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  630. {
  631. /* XXX could be more clever ;) */
  632. return false;
  633. }
  634. bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  635. {
  636. /* The page will get remapped properly on its next fault */
  637. return kvm_e500_mmu_unmap_gfn(kvm, range);
  638. }
  639. /*****************************************/
  640. int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  641. {
  642. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  643. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  644. /*
  645. * This should never happen on real e500 hardware, but is
  646. * architecturally possible -- e.g. in some weird nested
  647. * virtualization case.
  648. */
  649. if (host_tlb_params[0].entries == 0 ||
  650. host_tlb_params[1].entries == 0) {
  651. pr_err("%s: need to know host tlb size\n", __func__);
  652. return -ENODEV;
  653. }
  654. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  655. TLBnCFG_ASSOC_SHIFT;
  656. host_tlb_params[1].ways = host_tlb_params[1].entries;
  657. if (!is_power_of_2(host_tlb_params[0].entries) ||
  658. !is_power_of_2(host_tlb_params[0].ways) ||
  659. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  660. host_tlb_params[0].ways == 0) {
  661. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  662. __func__, host_tlb_params[0].entries,
  663. host_tlb_params[0].ways);
  664. return -ENODEV;
  665. }
  666. host_tlb_params[0].sets =
  667. host_tlb_params[0].entries / host_tlb_params[0].ways;
  668. host_tlb_params[1].sets = 1;
  669. vcpu_e500->h2g_tlb1_rmap = kcalloc(host_tlb_params[1].entries,
  670. sizeof(*vcpu_e500->h2g_tlb1_rmap),
  671. GFP_KERNEL);
  672. if (!vcpu_e500->h2g_tlb1_rmap)
  673. return -EINVAL;
  674. return 0;
  675. }
  676. void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  677. {
  678. kfree(vcpu_e500->h2g_tlb1_rmap);
  679. }