e500_mmu_host.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. {
  65. unsigned long flags;
  66. local_irq_save(flags);
  67. mtspr(SPRN_MAS0, mas0);
  68. mtspr(SPRN_MAS1, stlbe->mas1);
  69. mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
  70. mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
  71. mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
  72. #ifdef CONFIG_KVM_BOOKE_HV
  73. mtspr(SPRN_MAS8, stlbe->mas8);
  74. #endif
  75. asm volatile("isync; tlbwe" : : : "memory");
  76. #ifdef CONFIG_KVM_BOOKE_HV
  77. /* Must clear mas8 for other host tlbwe's */
  78. mtspr(SPRN_MAS8, 0);
  79. isync();
  80. #endif
  81. local_irq_restore(flags);
  82. trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
  83. stlbe->mas2, stlbe->mas7_3);
  84. }
  85. /*
  86. * Acquire a mas0 with victim hint, as if we just took a TLB miss.
  87. *
  88. * We don't care about the address we're searching for, other than that it's
  89. * in the right set and is not present in the TLB. Using a zero PID and a
  90. * userspace address means we don't have to set and then restore MAS5, or
  91. * calculate a proper MAS6 value.
  92. */
  93. static u32 get_host_mas0(unsigned long eaddr)
  94. {
  95. unsigned long flags;
  96. u32 mas0;
  97. u32 mas4;
  98. local_irq_save(flags);
  99. mtspr(SPRN_MAS6, 0);
  100. mas4 = mfspr(SPRN_MAS4);
  101. mtspr(SPRN_MAS4, mas4 & ~MAS4_TLBSEL_MASK);
  102. asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
  103. mas0 = mfspr(SPRN_MAS0);
  104. mtspr(SPRN_MAS4, mas4);
  105. local_irq_restore(flags);
  106. return mas0;
  107. }
  108. /* sesel is for tlb1 only */
  109. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  110. int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
  111. {
  112. u32 mas0;
  113. if (tlbsel == 0) {
  114. mas0 = get_host_mas0(stlbe->mas2);
  115. __write_host_tlbe(stlbe, mas0);
  116. } else {
  117. __write_host_tlbe(stlbe,
  118. MAS0_TLBSEL(1) |
  119. MAS0_ESEL(to_htlb1_esel(sesel)));
  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. pfn_t pfn;
  144. pfn = (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));
  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. 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. 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. #ifdef CONFIG_KVM_BOOKE_HV
  276. stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
  277. #endif
  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. /* 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 = 1 << (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 = 1 << (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. ptep = lookup_linux_ptep(pgdir, hva, &tsize_pages);
  401. if (pte_present(*ptep))
  402. wimg = (*ptep >> PTE_WIMGE_SHIFT) & MAS2_WIMGE_MASK;
  403. else {
  404. if (printk_ratelimit())
  405. pr_err("%s: pte not present: gfn %lx, pfn %lx\n",
  406. __func__, (long)gfn, pfn);
  407. ret = -EINVAL;
  408. goto out;
  409. }
  410. kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg);
  411. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  412. ref, gvaddr, stlbe);
  413. /* Clear i-cache for new pages */
  414. kvmppc_mmu_flush_icache(pfn);
  415. out:
  416. spin_unlock(&kvm->mmu_lock);
  417. /* Drop refcount on page, so that mmu notifiers can clear it */
  418. kvm_release_pfn_clean(pfn);
  419. return ret;
  420. }
  421. /* XXX only map the one-one case, for now use TLB0 */
  422. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, int esel,
  423. struct kvm_book3e_206_tlb_entry *stlbe)
  424. {
  425. struct kvm_book3e_206_tlb_entry *gtlbe;
  426. struct tlbe_ref *ref;
  427. int stlbsel = 0;
  428. int sesel = 0;
  429. int r;
  430. gtlbe = get_entry(vcpu_e500, 0, esel);
  431. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  432. r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  433. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  434. gtlbe, 0, stlbe, ref);
  435. if (r)
  436. return r;
  437. write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
  438. return 0;
  439. }
  440. static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
  441. struct tlbe_ref *ref,
  442. int esel)
  443. {
  444. unsigned int sesel = vcpu_e500->host_tlb1_nv++;
  445. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  446. vcpu_e500->host_tlb1_nv = 0;
  447. if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
  448. unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
  449. vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
  450. }
  451. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
  452. vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
  453. vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
  454. WARN_ON(!(ref->flags & E500_TLB_VALID));
  455. return sesel;
  456. }
  457. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  458. * the shadow TLB. */
  459. /* For both one-one and one-to-many */
  460. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  461. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  462. struct kvm_book3e_206_tlb_entry *stlbe, int esel)
  463. {
  464. struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[1][esel].ref;
  465. int sesel;
  466. int r;
  467. r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
  468. ref);
  469. if (r)
  470. return r;
  471. /* Use TLB0 when we can only map a page with 4k */
  472. if (get_tlb_tsize(stlbe) == BOOK3E_PAGESZ_4K) {
  473. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_TLB0;
  474. write_stlbe(vcpu_e500, gtlbe, stlbe, 0, 0);
  475. return 0;
  476. }
  477. /* Otherwise map into TLB1 */
  478. sesel = kvmppc_e500_tlb1_map_tlb1(vcpu_e500, ref, esel);
  479. write_stlbe(vcpu_e500, gtlbe, stlbe, 1, sesel);
  480. return 0;
  481. }
  482. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  483. unsigned int index)
  484. {
  485. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  486. struct tlbe_priv *priv;
  487. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  488. int tlbsel = tlbsel_of(index);
  489. int esel = esel_of(index);
  490. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  491. switch (tlbsel) {
  492. case 0:
  493. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  494. /* Triggers after clear_tlb_privs or on initial mapping */
  495. if (!(priv->ref.flags & E500_TLB_VALID)) {
  496. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  497. } else {
  498. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  499. &priv->ref, eaddr, &stlbe);
  500. write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
  501. }
  502. break;
  503. case 1: {
  504. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  505. kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
  506. esel);
  507. break;
  508. }
  509. default:
  510. BUG();
  511. break;
  512. }
  513. }
  514. #ifdef CONFIG_KVM_BOOKE_HV
  515. int kvmppc_load_last_inst(struct kvm_vcpu *vcpu, enum instruction_type type,
  516. u32 *instr)
  517. {
  518. gva_t geaddr;
  519. hpa_t addr;
  520. hfn_t pfn;
  521. hva_t eaddr;
  522. u32 mas1, mas2, mas3;
  523. u64 mas7_mas3;
  524. struct page *page;
  525. unsigned int addr_space, psize_shift;
  526. bool pr;
  527. unsigned long flags;
  528. /* Search TLB for guest pc to get the real address */
  529. geaddr = kvmppc_get_pc(vcpu);
  530. addr_space = (vcpu->arch.shared->msr & MSR_IS) >> MSR_IR_LG;
  531. local_irq_save(flags);
  532. mtspr(SPRN_MAS6, (vcpu->arch.pid << MAS6_SPID_SHIFT) | addr_space);
  533. mtspr(SPRN_MAS5, MAS5_SGS | vcpu->kvm->arch.lpid);
  534. asm volatile("tlbsx 0, %[geaddr]\n" : :
  535. [geaddr] "r" (geaddr));
  536. mtspr(SPRN_MAS5, 0);
  537. mtspr(SPRN_MAS8, 0);
  538. mas1 = mfspr(SPRN_MAS1);
  539. mas2 = mfspr(SPRN_MAS2);
  540. mas3 = mfspr(SPRN_MAS3);
  541. #ifdef CONFIG_64BIT
  542. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  543. #else
  544. mas7_mas3 = ((u64)mfspr(SPRN_MAS7) << 32) | mas3;
  545. #endif
  546. local_irq_restore(flags);
  547. /*
  548. * If the TLB entry for guest pc was evicted, return to the guest.
  549. * There are high chances to find a valid TLB entry next time.
  550. */
  551. if (!(mas1 & MAS1_VALID))
  552. return EMULATE_AGAIN;
  553. /*
  554. * Another thread may rewrite the TLB entry in parallel, don't
  555. * execute from the address if the execute permission is not set
  556. */
  557. pr = vcpu->arch.shared->msr & MSR_PR;
  558. if (unlikely((pr && !(mas3 & MAS3_UX)) ||
  559. (!pr && !(mas3 & MAS3_SX)))) {
  560. pr_err_ratelimited(
  561. "%s: Instuction emulation from guest addres %08lx without execute permission\n",
  562. __func__, geaddr);
  563. return EMULATE_AGAIN;
  564. }
  565. /*
  566. * The real address will be mapped by a cacheable, memory coherent,
  567. * write-back page. Check for mismatches when LRAT is used.
  568. */
  569. if (has_feature(vcpu, VCPU_FTR_MMU_V2) &&
  570. unlikely((mas2 & MAS2_I) || (mas2 & MAS2_W) || !(mas2 & MAS2_M))) {
  571. pr_err_ratelimited(
  572. "%s: Instuction emulation from guest addres %08lx mismatches storage attributes\n",
  573. __func__, geaddr);
  574. return EMULATE_AGAIN;
  575. }
  576. /* Get pfn */
  577. psize_shift = MAS1_GET_TSIZE(mas1) + 10;
  578. addr = (mas7_mas3 & (~0ULL << psize_shift)) |
  579. (geaddr & ((1ULL << psize_shift) - 1ULL));
  580. pfn = addr >> PAGE_SHIFT;
  581. /* Guard against emulation from devices area */
  582. if (unlikely(!page_is_ram(pfn))) {
  583. pr_err_ratelimited("%s: Instruction emulation from non-RAM host addres %08llx is not supported\n",
  584. __func__, addr);
  585. return EMULATE_AGAIN;
  586. }
  587. /* Map a page and get guest's instruction */
  588. page = pfn_to_page(pfn);
  589. eaddr = (unsigned long)kmap_atomic(page);
  590. *instr = *(u32 *)(eaddr | (unsigned long)(addr & ~PAGE_MASK));
  591. kunmap_atomic((u32 *)eaddr);
  592. return EMULATE_DONE;
  593. }
  594. #else
  595. int kvmppc_load_last_inst(struct kvm_vcpu *vcpu, enum instruction_type type,
  596. u32 *instr)
  597. {
  598. return EMULATE_AGAIN;
  599. }
  600. #endif
  601. /************* MMU Notifiers *************/
  602. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  603. {
  604. trace_kvm_unmap_hva(hva);
  605. /*
  606. * Flush all shadow tlb entries everywhere. This is slow, but
  607. * we are 100% sure that we catch the to be unmapped page
  608. */
  609. kvm_flush_remote_tlbs(kvm);
  610. return 0;
  611. }
  612. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  613. {
  614. /* kvm_unmap_hva flushes everything anyways */
  615. kvm_unmap_hva(kvm, start);
  616. return 0;
  617. }
  618. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  619. {
  620. /* XXX could be more clever ;) */
  621. return 0;
  622. }
  623. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  624. {
  625. /* XXX could be more clever ;) */
  626. return 0;
  627. }
  628. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  629. {
  630. /* The page will get remapped properly on its next fault */
  631. kvm_unmap_hva(kvm, hva);
  632. }
  633. /*****************************************/
  634. int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  635. {
  636. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  637. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  638. /*
  639. * This should never happen on real e500 hardware, but is
  640. * architecturally possible -- e.g. in some weird nested
  641. * virtualization case.
  642. */
  643. if (host_tlb_params[0].entries == 0 ||
  644. host_tlb_params[1].entries == 0) {
  645. pr_err("%s: need to know host tlb size\n", __func__);
  646. return -ENODEV;
  647. }
  648. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  649. TLBnCFG_ASSOC_SHIFT;
  650. host_tlb_params[1].ways = host_tlb_params[1].entries;
  651. if (!is_power_of_2(host_tlb_params[0].entries) ||
  652. !is_power_of_2(host_tlb_params[0].ways) ||
  653. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  654. host_tlb_params[0].ways == 0) {
  655. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  656. __func__, host_tlb_params[0].entries,
  657. host_tlb_params[0].ways);
  658. return -ENODEV;
  659. }
  660. host_tlb_params[0].sets =
  661. host_tlb_params[0].entries / host_tlb_params[0].ways;
  662. host_tlb_params[1].sets = 1;
  663. vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
  664. host_tlb_params[1].entries,
  665. GFP_KERNEL);
  666. if (!vcpu_e500->h2g_tlb1_rmap)
  667. return -EINVAL;
  668. return 0;
  669. }
  670. void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  671. {
  672. kfree(vcpu_e500->h2g_tlb1_rmap);
  673. }