tlb.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains the routines for TLB flushing.
  4. * On machines where the MMU uses a hash table to store virtual to
  5. * physical translations, these routines flush entries from the
  6. * hash table also.
  7. * -- paulus
  8. *
  9. * Derived from arch/ppc/mm/init.c:
  10. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  11. *
  12. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  13. * and Cort Dougan (PReP) ([email protected])
  14. * Copyright (C) 1996 Paul Mackerras
  15. *
  16. * Derived from "arch/i386/mm/init.c"
  17. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/highmem.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/export.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/tlb.h>
  27. #include <mm/mmu_decl.h>
  28. /*
  29. * TLB flushing:
  30. *
  31. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  32. * - flush_tlb_page(vma, vmaddr) flushes one page
  33. * - flush_tlb_range(vma, start, end) flushes a range of pages
  34. * - flush_tlb_kernel_range(start, end) flushes kernel pages
  35. *
  36. * since the hardware hash table functions as an extension of the
  37. * tlb as far as the linux tables are concerned, flush it too.
  38. * -- Cort
  39. */
  40. /*
  41. * For each address in the range, find the pte for the address
  42. * and check _PAGE_HASHPTE bit; if it is set, find and destroy
  43. * the corresponding HPTE.
  44. */
  45. void hash__flush_range(struct mm_struct *mm, unsigned long start, unsigned long end)
  46. {
  47. pmd_t *pmd;
  48. unsigned long pmd_end;
  49. int count;
  50. unsigned int ctx = mm->context.id;
  51. start &= PAGE_MASK;
  52. if (start >= end)
  53. return;
  54. end = (end - 1) | ~PAGE_MASK;
  55. pmd = pmd_off(mm, start);
  56. for (;;) {
  57. pmd_end = ((start + PGDIR_SIZE) & PGDIR_MASK) - 1;
  58. if (pmd_end > end)
  59. pmd_end = end;
  60. if (!pmd_none(*pmd)) {
  61. count = ((pmd_end - start) >> PAGE_SHIFT) + 1;
  62. flush_hash_pages(ctx, start, pmd_val(*pmd), count);
  63. }
  64. if (pmd_end == end)
  65. break;
  66. start = pmd_end + 1;
  67. ++pmd;
  68. }
  69. }
  70. EXPORT_SYMBOL(hash__flush_range);
  71. /*
  72. * Flush all the (user) entries for the address space described by mm.
  73. */
  74. void hash__flush_tlb_mm(struct mm_struct *mm)
  75. {
  76. struct vm_area_struct *mp;
  77. VMA_ITERATOR(vmi, mm, 0);
  78. /*
  79. * It is safe to iterate the vmas when called from dup_mmap,
  80. * holding mmap_lock. It would also be safe from unmap_region
  81. * or exit_mmap, but not from vmtruncate on SMP - but it seems
  82. * dup_mmap is the only SMP case which gets here.
  83. */
  84. for_each_vma(vmi, mp)
  85. hash__flush_range(mp->vm_mm, mp->vm_start, mp->vm_end);
  86. }
  87. EXPORT_SYMBOL(hash__flush_tlb_mm);
  88. void hash__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  89. {
  90. struct mm_struct *mm;
  91. pmd_t *pmd;
  92. mm = (vmaddr < TASK_SIZE)? vma->vm_mm: &init_mm;
  93. pmd = pmd_off(mm, vmaddr);
  94. if (!pmd_none(*pmd))
  95. flush_hash_pages(mm->context.id, vmaddr, pmd_val(*pmd), 1);
  96. }
  97. EXPORT_SYMBOL(hash__flush_tlb_page);