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