e500_mmu_host.c 21 KB

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