tlb.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * arch/xtensa/mm/tlb.c
  3. *
  4. * Logic that manipulates the Xtensa MMU. Derived from MIPS.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2003 Tensilica Inc.
  11. *
  12. * Joe Taylor
  13. * Chris Zankel <chris@zankel.net>
  14. * Marc Gauthier
  15. */
  16. #include <linux/mm.h>
  17. #include <asm/processor.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/tlb.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/cacheflush.h>
  22. static inline void __flush_itlb_all (void)
  23. {
  24. int w, i;
  25. for (w = 0; w < ITLB_ARF_WAYS; w++) {
  26. for (i = 0; i < (1 << XCHAL_ITLB_ARF_ENTRIES_LOG2); i++) {
  27. int e = w + (i << PAGE_SHIFT);
  28. invalidate_itlb_entry_no_isync(e);
  29. }
  30. }
  31. asm volatile ("isync\n");
  32. }
  33. static inline void __flush_dtlb_all (void)
  34. {
  35. int w, i;
  36. for (w = 0; w < DTLB_ARF_WAYS; w++) {
  37. for (i = 0; i < (1 << XCHAL_DTLB_ARF_ENTRIES_LOG2); i++) {
  38. int e = w + (i << PAGE_SHIFT);
  39. invalidate_dtlb_entry_no_isync(e);
  40. }
  41. }
  42. asm volatile ("isync\n");
  43. }
  44. void local_flush_tlb_all(void)
  45. {
  46. __flush_itlb_all();
  47. __flush_dtlb_all();
  48. }
  49. /* If mm is current, we simply assign the current task a new ASID, thus,
  50. * invalidating all previous tlb entries. If mm is someone else's user mapping,
  51. * wie invalidate the context, thus, when that user mapping is swapped in,
  52. * a new context will be assigned to it.
  53. */
  54. void local_flush_tlb_mm(struct mm_struct *mm)
  55. {
  56. int cpu = smp_processor_id();
  57. if (mm == current->active_mm) {
  58. unsigned long flags;
  59. local_irq_save(flags);
  60. mm->context.asid[cpu] = NO_CONTEXT;
  61. activate_context(mm, cpu);
  62. local_irq_restore(flags);
  63. } else {
  64. mm->context.asid[cpu] = NO_CONTEXT;
  65. mm->context.cpu = -1;
  66. }
  67. }
  68. #define _ITLB_ENTRIES (ITLB_ARF_WAYS << XCHAL_ITLB_ARF_ENTRIES_LOG2)
  69. #define _DTLB_ENTRIES (DTLB_ARF_WAYS << XCHAL_DTLB_ARF_ENTRIES_LOG2)
  70. #if _ITLB_ENTRIES > _DTLB_ENTRIES
  71. # define _TLB_ENTRIES _ITLB_ENTRIES
  72. #else
  73. # define _TLB_ENTRIES _DTLB_ENTRIES
  74. #endif
  75. void local_flush_tlb_range(struct vm_area_struct *vma,
  76. unsigned long start, unsigned long end)
  77. {
  78. int cpu = smp_processor_id();
  79. struct mm_struct *mm = vma->vm_mm;
  80. unsigned long flags;
  81. if (mm->context.asid[cpu] == NO_CONTEXT)
  82. return;
  83. pr_debug("[tlbrange<%02lx,%08lx,%08lx>]\n",
  84. (unsigned long)mm->context.asid[cpu], start, end);
  85. local_irq_save(flags);
  86. if (end-start + (PAGE_SIZE-1) <= _TLB_ENTRIES << PAGE_SHIFT) {
  87. int oldpid = get_rasid_register();
  88. set_rasid_register(ASID_INSERT(mm->context.asid[cpu]));
  89. start &= PAGE_MASK;
  90. if (vma->vm_flags & VM_EXEC)
  91. while(start < end) {
  92. invalidate_itlb_mapping(start);
  93. invalidate_dtlb_mapping(start);
  94. start += PAGE_SIZE;
  95. }
  96. else
  97. while(start < end) {
  98. invalidate_dtlb_mapping(start);
  99. start += PAGE_SIZE;
  100. }
  101. set_rasid_register(oldpid);
  102. } else {
  103. local_flush_tlb_mm(mm);
  104. }
  105. local_irq_restore(flags);
  106. }
  107. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  108. {
  109. int cpu = smp_processor_id();
  110. struct mm_struct* mm = vma->vm_mm;
  111. unsigned long flags;
  112. int oldpid;
  113. if (mm->context.asid[cpu] == NO_CONTEXT)
  114. return;
  115. local_irq_save(flags);
  116. oldpid = get_rasid_register();
  117. set_rasid_register(ASID_INSERT(mm->context.asid[cpu]));
  118. if (vma->vm_flags & VM_EXEC)
  119. invalidate_itlb_mapping(page);
  120. invalidate_dtlb_mapping(page);
  121. set_rasid_register(oldpid);
  122. local_irq_restore(flags);
  123. }
  124. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  125. {
  126. if (end > start && start >= TASK_SIZE && end <= PAGE_OFFSET &&
  127. end - start < _TLB_ENTRIES << PAGE_SHIFT) {
  128. start &= PAGE_MASK;
  129. while (start < end) {
  130. invalidate_itlb_mapping(start);
  131. invalidate_dtlb_mapping(start);
  132. start += PAGE_SIZE;
  133. }
  134. } else {
  135. local_flush_tlb_all();
  136. }
  137. }
  138. void update_mmu_tlb_range(struct vm_area_struct *vma,
  139. unsigned long address, pte_t *ptep, unsigned int nr)
  140. {
  141. local_flush_tlb_range(vma, address, address + PAGE_SIZE * nr);
  142. }
  143. #ifdef CONFIG_DEBUG_TLB_SANITY
  144. static unsigned get_pte_for_vaddr(unsigned vaddr)
  145. {
  146. struct task_struct *task = get_current();
  147. struct mm_struct *mm = task->mm;
  148. pgd_t *pgd;
  149. p4d_t *p4d;
  150. pud_t *pud;
  151. pmd_t *pmd;
  152. pte_t *pte;
  153. unsigned int pteval;
  154. if (!mm)
  155. mm = task->active_mm;
  156. pgd = pgd_offset(mm, vaddr);
  157. if (pgd_none_or_clear_bad(pgd))
  158. return 0;
  159. p4d = p4d_offset(pgd, vaddr);
  160. if (p4d_none_or_clear_bad(p4d))
  161. return 0;
  162. pud = pud_offset(p4d, vaddr);
  163. if (pud_none_or_clear_bad(pud))
  164. return 0;
  165. pmd = pmd_offset(pud, vaddr);
  166. if (pmd_none_or_clear_bad(pmd))
  167. return 0;
  168. pte = pte_offset_map(pmd, vaddr);
  169. if (!pte)
  170. return 0;
  171. pteval = pte_val(*pte);
  172. pte_unmap(pte);
  173. return pteval;
  174. }
  175. enum {
  176. TLB_SUSPICIOUS = 1,
  177. TLB_INSANE = 2,
  178. };
  179. static void tlb_insane(void)
  180. {
  181. BUG_ON(1);
  182. }
  183. static void tlb_suspicious(void)
  184. {
  185. WARN_ON(1);
  186. }
  187. /*
  188. * Check that TLB entries with kernel ASID (1) have kernel VMA (>= TASK_SIZE),
  189. * and TLB entries with user ASID (>=4) have VMA < TASK_SIZE.
  190. *
  191. * Check that valid TLB entries either have the same PA as the PTE, or PTE is
  192. * marked as non-present. Non-present PTE and the page with non-zero refcount
  193. * and zero mapcount is normal for batched TLB flush operation. Zero refcount
  194. * means that the page was freed prematurely. Non-zero mapcount is unusual,
  195. * but does not necessary means an error, thus marked as suspicious.
  196. */
  197. static int check_tlb_entry(unsigned w, unsigned e, bool dtlb)
  198. {
  199. unsigned tlbidx = w | (e << PAGE_SHIFT);
  200. unsigned r0 = dtlb ?
  201. read_dtlb_virtual(tlbidx) : read_itlb_virtual(tlbidx);
  202. unsigned r1 = dtlb ?
  203. read_dtlb_translation(tlbidx) : read_itlb_translation(tlbidx);
  204. unsigned vpn = (r0 & PAGE_MASK) | (e << PAGE_SHIFT);
  205. unsigned pte = get_pte_for_vaddr(vpn);
  206. unsigned mm_asid = (get_rasid_register() >> 8) & ASID_MASK;
  207. unsigned tlb_asid = r0 & ASID_MASK;
  208. bool kernel = tlb_asid == 1;
  209. int rc = 0;
  210. if (tlb_asid > 0 && ((vpn < TASK_SIZE) == kernel)) {
  211. pr_err("%cTLB: way: %u, entry: %u, VPN %08x in %s PTE\n",
  212. dtlb ? 'D' : 'I', w, e, vpn,
  213. kernel ? "kernel" : "user");
  214. rc |= TLB_INSANE;
  215. }
  216. if (tlb_asid == mm_asid) {
  217. if ((pte ^ r1) & PAGE_MASK) {
  218. pr_err("%cTLB: way: %u, entry: %u, mapping: %08x->%08x, PTE: %08x\n",
  219. dtlb ? 'D' : 'I', w, e, r0, r1, pte);
  220. if (pte == 0 || !pte_present(__pte(pte))) {
  221. struct page *p = pfn_to_page(r1 >> PAGE_SHIFT);
  222. struct folio *f = page_folio(p);
  223. pr_err("folio refcount: %d, mapcount: %d\n",
  224. folio_ref_count(f), folio_mapcount(f));
  225. if (!folio_ref_count(f))
  226. rc |= TLB_INSANE;
  227. else if (folio_mapped(f))
  228. rc |= TLB_SUSPICIOUS;
  229. } else {
  230. rc |= TLB_INSANE;
  231. }
  232. }
  233. }
  234. return rc;
  235. }
  236. void check_tlb_sanity(void)
  237. {
  238. unsigned long flags;
  239. unsigned w, e;
  240. int bug = 0;
  241. local_irq_save(flags);
  242. for (w = 0; w < DTLB_ARF_WAYS; ++w)
  243. for (e = 0; e < (1 << XCHAL_DTLB_ARF_ENTRIES_LOG2); ++e)
  244. bug |= check_tlb_entry(w, e, true);
  245. for (w = 0; w < ITLB_ARF_WAYS; ++w)
  246. for (e = 0; e < (1 << XCHAL_ITLB_ARF_ENTRIES_LOG2); ++e)
  247. bug |= check_tlb_entry(w, e, false);
  248. if (bug & TLB_INSANE)
  249. tlb_insane();
  250. if (bug & TLB_SUSPICIOUS)
  251. tlb_suspicious();
  252. local_irq_restore(flags);
  253. }
  254. #endif /* CONFIG_DEBUG_TLB_SANITY */