tlb.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef _ASM_IA64_TLB_H
  2. #define _ASM_IA64_TLB_H
  3. /*
  4. * Based on <asm-generic/tlb.h>.
  5. *
  6. * Copyright (C) 2002-2003 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. */
  9. /*
  10. * Removing a translation from a page table (including TLB-shootdown) is a four-step
  11. * procedure:
  12. *
  13. * (1) Flush (virtual) caches --- ensures virtual memory is coherent with kernel memory
  14. * (this is a no-op on ia64).
  15. * (2) Clear the relevant portions of the page-table
  16. * (3) Flush the TLBs --- ensures that stale content is gone from CPU TLBs
  17. * (4) Release the pages that were freed up in step (2).
  18. *
  19. * Note that the ordering of these steps is crucial to avoid races on MP machines.
  20. *
  21. * The Linux kernel defines several platform-specific hooks for TLB-shootdown. When
  22. * unmapping a portion of the virtual address space, these hooks are called according to
  23. * the following template:
  24. *
  25. * tlb <- tlb_gather_mmu(mm, full_mm_flush); // start unmap for address space MM
  26. * {
  27. * for each vma that needs a shootdown do {
  28. * tlb_start_vma(tlb, vma);
  29. * for each page-table-entry PTE that needs to be removed do {
  30. * tlb_remove_tlb_entry(tlb, pte, address);
  31. * if (pte refers to a normal page) {
  32. * tlb_remove_page(tlb, page);
  33. * }
  34. * }
  35. * tlb_end_vma(tlb, vma);
  36. * }
  37. * }
  38. * tlb_finish_mmu(tlb, start, end); // finish unmap for address space MM
  39. */
  40. #include <linux/mm.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/swap.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/processor.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/machvec.h>
  47. #ifdef CONFIG_SMP
  48. # define FREE_PTE_NR 2048
  49. # define tlb_fast_mode(tlb) ((tlb)->nr == ~0U)
  50. #else
  51. # define FREE_PTE_NR 0
  52. # define tlb_fast_mode(tlb) (1)
  53. #endif
  54. struct mmu_gather {
  55. struct mm_struct *mm;
  56. unsigned int nr; /* == ~0U => fast mode */
  57. unsigned char fullmm; /* non-zero means full mm flush */
  58. unsigned char need_flush; /* really unmapped some PTEs? */
  59. unsigned long start_addr;
  60. unsigned long end_addr;
  61. struct page *pages[FREE_PTE_NR];
  62. };
  63. /* Users of the generic TLB shootdown code must declare this storage space. */
  64. DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
  65. /*
  66. * Flush the TLB for address range START to END and, if not in fast mode, release the
  67. * freed pages that where gathered up to this point.
  68. */
  69. static inline void
  70. ia64_tlb_flush_mmu (struct mmu_gather *tlb, unsigned long start, unsigned long end)
  71. {
  72. unsigned int nr;
  73. if (!tlb->need_flush)
  74. return;
  75. tlb->need_flush = 0;
  76. if (tlb->fullmm) {
  77. /*
  78. * Tearing down the entire address space. This happens both as a result
  79. * of exit() and execve(). The latter case necessitates the call to
  80. * flush_tlb_mm() here.
  81. */
  82. flush_tlb_mm(tlb->mm);
  83. } else if (unlikely (end - start >= 1024*1024*1024*1024UL
  84. || REGION_NUMBER(start) != REGION_NUMBER(end - 1)))
  85. {
  86. /*
  87. * If we flush more than a tera-byte or across regions, we're probably
  88. * better off just flushing the entire TLB(s). This should be very rare
  89. * and is not worth optimizing for.
  90. */
  91. flush_tlb_all();
  92. } else {
  93. /*
  94. * XXX fix me: flush_tlb_range() should take an mm pointer instead of a
  95. * vma pointer.
  96. */
  97. struct vm_area_struct vma;
  98. vma.vm_mm = tlb->mm;
  99. /* flush the address range from the tlb: */
  100. flush_tlb_range(&vma, start, end);
  101. /* now flush the virt. page-table area mapping the address range: */
  102. flush_tlb_range(&vma, ia64_thash(start), ia64_thash(end));
  103. }
  104. /* lastly, release the freed pages */
  105. nr = tlb->nr;
  106. if (!tlb_fast_mode(tlb)) {
  107. unsigned long i;
  108. tlb->nr = 0;
  109. tlb->start_addr = ~0UL;
  110. for (i = 0; i < nr; ++i)
  111. free_page_and_swap_cache(tlb->pages[i]);
  112. }
  113. }
  114. /*
  115. * Return a pointer to an initialized struct mmu_gather.
  116. */
  117. static inline struct mmu_gather *
  118. tlb_gather_mmu (struct mm_struct *mm, unsigned int full_mm_flush)
  119. {
  120. struct mmu_gather *tlb = &get_cpu_var(mmu_gathers);
  121. tlb->mm = mm;
  122. /*
  123. * Use fast mode if only 1 CPU is online.
  124. *
  125. * It would be tempting to turn on fast-mode for full_mm_flush as well. But this
  126. * doesn't work because of speculative accesses and software prefetching: the page
  127. * table of "mm" may (and usually is) the currently active page table and even
  128. * though the kernel won't do any user-space accesses during the TLB shoot down, a
  129. * compiler might use speculation or lfetch.fault on what happens to be a valid
  130. * user-space address. This in turn could trigger a TLB miss fault (or a VHPT
  131. * walk) and re-insert a TLB entry we just removed. Slow mode avoids such
  132. * problems. (We could make fast-mode work by switching the current task to a
  133. * different "mm" during the shootdown.) --davidm 08/02/2002
  134. */
  135. tlb->nr = (num_online_cpus() == 1) ? ~0U : 0;
  136. tlb->fullmm = full_mm_flush;
  137. tlb->start_addr = ~0UL;
  138. return tlb;
  139. }
  140. /*
  141. * Called at the end of the shootdown operation to free up any resources that were
  142. * collected.
  143. */
  144. static inline void
  145. tlb_finish_mmu (struct mmu_gather *tlb, unsigned long start, unsigned long end)
  146. {
  147. /*
  148. * Note: tlb->nr may be 0 at this point, so we can't rely on tlb->start_addr and
  149. * tlb->end_addr.
  150. */
  151. ia64_tlb_flush_mmu(tlb, start, end);
  152. /* keep the page table cache within bounds */
  153. check_pgt_cache();
  154. put_cpu_var(mmu_gathers);
  155. }
  156. /*
  157. * Logically, this routine frees PAGE. On MP machines, the actual freeing of the page
  158. * must be delayed until after the TLB has been flushed (see comments at the beginning of
  159. * this file).
  160. */
  161. static inline void
  162. tlb_remove_page (struct mmu_gather *tlb, struct page *page)
  163. {
  164. tlb->need_flush = 1;
  165. if (tlb_fast_mode(tlb)) {
  166. free_page_and_swap_cache(page);
  167. return;
  168. }
  169. tlb->pages[tlb->nr++] = page;
  170. if (tlb->nr >= FREE_PTE_NR)
  171. ia64_tlb_flush_mmu(tlb, tlb->start_addr, tlb->end_addr);
  172. }
  173. /*
  174. * Remove TLB entry for PTE mapped at virtual address ADDRESS. This is called for any
  175. * PTE, not just those pointing to (normal) physical memory.
  176. */
  177. static inline void
  178. __tlb_remove_tlb_entry (struct mmu_gather *tlb, pte_t *ptep, unsigned long address)
  179. {
  180. if (tlb->start_addr == ~0UL)
  181. tlb->start_addr = address;
  182. tlb->end_addr = address + PAGE_SIZE;
  183. }
  184. #define tlb_migrate_finish(mm) platform_tlb_migrate_finish(mm)
  185. #define tlb_start_vma(tlb, vma) do { } while (0)
  186. #define tlb_end_vma(tlb, vma) do { } while (0)
  187. #define tlb_remove_tlb_entry(tlb, ptep, addr) \
  188. do { \
  189. tlb->need_flush = 1; \
  190. __tlb_remove_tlb_entry(tlb, ptep, addr); \
  191. } while (0)
  192. #define pte_free_tlb(tlb, ptep) \
  193. do { \
  194. tlb->need_flush = 1; \
  195. __pte_free_tlb(tlb, ptep); \
  196. } while (0)
  197. #define pmd_free_tlb(tlb, ptep) \
  198. do { \
  199. tlb->need_flush = 1; \
  200. __pmd_free_tlb(tlb, ptep); \
  201. } while (0)
  202. #define pud_free_tlb(tlb, pudp) \
  203. do { \
  204. tlb->need_flush = 1; \
  205. __pud_free_tlb(tlb, pudp); \
  206. } while (0)
  207. #endif /* _ASM_IA64_TLB_H */