flush.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mm/flush.c
  4. *
  5. * Copyright (C) 1995-2002 Russell King
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/highmem.h>
  11. #include <asm/cacheflush.h>
  12. #include <asm/cachetype.h>
  13. #include <asm/highmem.h>
  14. #include <asm/smp_plat.h>
  15. #include <asm/tlbflush.h>
  16. #include <linux/hugetlb.h>
  17. #include "mm.h"
  18. #ifdef CONFIG_ARM_HEAVY_MB
  19. void (*soc_mb)(void);
  20. void arm_heavy_mb(void)
  21. {
  22. #ifdef CONFIG_OUTER_CACHE_SYNC
  23. if (outer_cache.sync)
  24. outer_cache.sync();
  25. #endif
  26. if (soc_mb)
  27. soc_mb();
  28. }
  29. EXPORT_SYMBOL(arm_heavy_mb);
  30. #endif
  31. #ifdef CONFIG_CPU_CACHE_VIPT
  32. static void flush_pfn_alias(unsigned long pfn, unsigned long vaddr)
  33. {
  34. unsigned long to = FLUSH_ALIAS_START + (CACHE_COLOUR(vaddr) << PAGE_SHIFT);
  35. const int zero = 0;
  36. set_top_pte(to, pfn_pte(pfn, PAGE_KERNEL));
  37. asm( "mcrr p15, 0, %1, %0, c14\n"
  38. " mcr p15, 0, %2, c7, c10, 4"
  39. :
  40. : "r" (to), "r" (to + PAGE_SIZE - 1), "r" (zero)
  41. : "cc");
  42. }
  43. static void flush_icache_alias(unsigned long pfn, unsigned long vaddr, unsigned long len)
  44. {
  45. unsigned long va = FLUSH_ALIAS_START + (CACHE_COLOUR(vaddr) << PAGE_SHIFT);
  46. unsigned long offset = vaddr & (PAGE_SIZE - 1);
  47. unsigned long to;
  48. set_top_pte(va, pfn_pte(pfn, PAGE_KERNEL));
  49. to = va + offset;
  50. flush_icache_range(to, to + len);
  51. }
  52. void flush_cache_mm(struct mm_struct *mm)
  53. {
  54. if (cache_is_vivt()) {
  55. vivt_flush_cache_mm(mm);
  56. return;
  57. }
  58. if (cache_is_vipt_aliasing()) {
  59. asm( "mcr p15, 0, %0, c7, c14, 0\n"
  60. " mcr p15, 0, %0, c7, c10, 4"
  61. :
  62. : "r" (0)
  63. : "cc");
  64. }
  65. }
  66. void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  67. {
  68. if (cache_is_vivt()) {
  69. vivt_flush_cache_range(vma, start, end);
  70. return;
  71. }
  72. if (cache_is_vipt_aliasing()) {
  73. asm( "mcr p15, 0, %0, c7, c14, 0\n"
  74. " mcr p15, 0, %0, c7, c10, 4"
  75. :
  76. : "r" (0)
  77. : "cc");
  78. }
  79. if (vma->vm_flags & VM_EXEC)
  80. __flush_icache_all();
  81. }
  82. void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  83. {
  84. if (cache_is_vivt()) {
  85. vivt_flush_cache_page(vma, user_addr, pfn);
  86. return;
  87. }
  88. if (cache_is_vipt_aliasing()) {
  89. flush_pfn_alias(pfn, user_addr);
  90. __flush_icache_all();
  91. }
  92. if (vma->vm_flags & VM_EXEC && icache_is_vivt_asid_tagged())
  93. __flush_icache_all();
  94. }
  95. #else
  96. #define flush_pfn_alias(pfn,vaddr) do { } while (0)
  97. #define flush_icache_alias(pfn,vaddr,len) do { } while (0)
  98. #endif
  99. #define FLAG_PA_IS_EXEC 1
  100. #define FLAG_PA_CORE_IN_MM 2
  101. static void flush_ptrace_access_other(void *args)
  102. {
  103. __flush_icache_all();
  104. }
  105. static inline
  106. void __flush_ptrace_access(struct page *page, unsigned long uaddr, void *kaddr,
  107. unsigned long len, unsigned int flags)
  108. {
  109. if (cache_is_vivt()) {
  110. if (flags & FLAG_PA_CORE_IN_MM) {
  111. unsigned long addr = (unsigned long)kaddr;
  112. __cpuc_coherent_kern_range(addr, addr + len);
  113. }
  114. return;
  115. }
  116. if (cache_is_vipt_aliasing()) {
  117. flush_pfn_alias(page_to_pfn(page), uaddr);
  118. __flush_icache_all();
  119. return;
  120. }
  121. /* VIPT non-aliasing D-cache */
  122. if (flags & FLAG_PA_IS_EXEC) {
  123. unsigned long addr = (unsigned long)kaddr;
  124. if (icache_is_vipt_aliasing())
  125. flush_icache_alias(page_to_pfn(page), uaddr, len);
  126. else
  127. __cpuc_coherent_kern_range(addr, addr + len);
  128. if (cache_ops_need_broadcast())
  129. smp_call_function(flush_ptrace_access_other,
  130. NULL, 1);
  131. }
  132. }
  133. static
  134. void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
  135. unsigned long uaddr, void *kaddr, unsigned long len)
  136. {
  137. unsigned int flags = 0;
  138. if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm)))
  139. flags |= FLAG_PA_CORE_IN_MM;
  140. if (vma->vm_flags & VM_EXEC)
  141. flags |= FLAG_PA_IS_EXEC;
  142. __flush_ptrace_access(page, uaddr, kaddr, len, flags);
  143. }
  144. void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
  145. void *kaddr, unsigned long len)
  146. {
  147. unsigned int flags = FLAG_PA_CORE_IN_MM|FLAG_PA_IS_EXEC;
  148. __flush_ptrace_access(page, uaddr, kaddr, len, flags);
  149. }
  150. /*
  151. * Copy user data from/to a page which is mapped into a different
  152. * processes address space. Really, we want to allow our "user
  153. * space" model to handle this.
  154. *
  155. * Note that this code needs to run on the current CPU.
  156. */
  157. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  158. unsigned long uaddr, void *dst, const void *src,
  159. unsigned long len)
  160. {
  161. #ifdef CONFIG_SMP
  162. preempt_disable();
  163. #endif
  164. memcpy(dst, src, len);
  165. flush_ptrace_access(vma, page, uaddr, dst, len);
  166. #ifdef CONFIG_SMP
  167. preempt_enable();
  168. #endif
  169. }
  170. void __flush_dcache_page(struct address_space *mapping, struct page *page)
  171. {
  172. /*
  173. * Writeback any data associated with the kernel mapping of this
  174. * page. This ensures that data in the physical page is mutually
  175. * coherent with the kernels mapping.
  176. */
  177. if (!PageHighMem(page)) {
  178. __cpuc_flush_dcache_area(page_address(page), page_size(page));
  179. } else {
  180. unsigned long i;
  181. if (cache_is_vipt_nonaliasing()) {
  182. for (i = 0; i < compound_nr(page); i++) {
  183. void *addr = kmap_atomic(page + i);
  184. __cpuc_flush_dcache_area(addr, PAGE_SIZE);
  185. kunmap_atomic(addr);
  186. }
  187. } else {
  188. for (i = 0; i < compound_nr(page); i++) {
  189. void *addr = kmap_high_get(page + i);
  190. if (addr) {
  191. __cpuc_flush_dcache_area(addr, PAGE_SIZE);
  192. kunmap_high(page + i);
  193. }
  194. }
  195. }
  196. }
  197. /*
  198. * If this is a page cache page, and we have an aliasing VIPT cache,
  199. * we only need to do one flush - which would be at the relevant
  200. * userspace colour, which is congruent with page->index.
  201. */
  202. if (mapping && cache_is_vipt_aliasing())
  203. flush_pfn_alias(page_to_pfn(page),
  204. page->index << PAGE_SHIFT);
  205. }
  206. static void __flush_dcache_aliases(struct address_space *mapping, struct page *page)
  207. {
  208. struct mm_struct *mm = current->active_mm;
  209. struct vm_area_struct *mpnt;
  210. pgoff_t pgoff;
  211. /*
  212. * There are possible user space mappings of this page:
  213. * - VIVT cache: we need to also write back and invalidate all user
  214. * data in the current VM view associated with this page.
  215. * - aliasing VIPT: we only need to find one mapping of this page.
  216. */
  217. pgoff = page->index;
  218. flush_dcache_mmap_lock(mapping);
  219. vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {
  220. unsigned long offset;
  221. /*
  222. * If this VMA is not in our MM, we can ignore it.
  223. */
  224. if (mpnt->vm_mm != mm)
  225. continue;
  226. if (!(mpnt->vm_flags & VM_MAYSHARE))
  227. continue;
  228. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  229. flush_cache_page(mpnt, mpnt->vm_start + offset, page_to_pfn(page));
  230. }
  231. flush_dcache_mmap_unlock(mapping);
  232. }
  233. #if __LINUX_ARM_ARCH__ >= 6
  234. void __sync_icache_dcache(pte_t pteval)
  235. {
  236. unsigned long pfn;
  237. struct page *page;
  238. struct address_space *mapping;
  239. if (cache_is_vipt_nonaliasing() && !pte_exec(pteval))
  240. /* only flush non-aliasing VIPT caches for exec mappings */
  241. return;
  242. pfn = pte_pfn(pteval);
  243. if (!pfn_valid(pfn))
  244. return;
  245. page = pfn_to_page(pfn);
  246. if (cache_is_vipt_aliasing())
  247. mapping = page_mapping_file(page);
  248. else
  249. mapping = NULL;
  250. if (!test_and_set_bit(PG_dcache_clean, &page->flags))
  251. __flush_dcache_page(mapping, page);
  252. if (pte_exec(pteval))
  253. __flush_icache_all();
  254. }
  255. #endif
  256. /*
  257. * Ensure cache coherency between kernel mapping and userspace mapping
  258. * of this page.
  259. *
  260. * We have three cases to consider:
  261. * - VIPT non-aliasing cache: fully coherent so nothing required.
  262. * - VIVT: fully aliasing, so we need to handle every alias in our
  263. * current VM view.
  264. * - VIPT aliasing: need to handle one alias in our current VM view.
  265. *
  266. * If we need to handle aliasing:
  267. * If the page only exists in the page cache and there are no user
  268. * space mappings, we can be lazy and remember that we may have dirty
  269. * kernel cache lines for later. Otherwise, we assume we have
  270. * aliasing mappings.
  271. *
  272. * Note that we disable the lazy flush for SMP configurations where
  273. * the cache maintenance operations are not automatically broadcasted.
  274. */
  275. void flush_dcache_page(struct page *page)
  276. {
  277. struct address_space *mapping;
  278. /*
  279. * The zero page is never written to, so never has any dirty
  280. * cache lines, and therefore never needs to be flushed.
  281. */
  282. if (page == ZERO_PAGE(0))
  283. return;
  284. if (!cache_ops_need_broadcast() && cache_is_vipt_nonaliasing()) {
  285. if (test_bit(PG_dcache_clean, &page->flags))
  286. clear_bit(PG_dcache_clean, &page->flags);
  287. return;
  288. }
  289. mapping = page_mapping_file(page);
  290. if (!cache_ops_need_broadcast() &&
  291. mapping && !page_mapcount(page))
  292. clear_bit(PG_dcache_clean, &page->flags);
  293. else {
  294. __flush_dcache_page(mapping, page);
  295. if (mapping && cache_is_vivt())
  296. __flush_dcache_aliases(mapping, page);
  297. else if (mapping)
  298. __flush_icache_all();
  299. set_bit(PG_dcache_clean, &page->flags);
  300. }
  301. }
  302. EXPORT_SYMBOL(flush_dcache_page);
  303. /*
  304. * Flush an anonymous page so that users of get_user_pages()
  305. * can safely access the data. The expected sequence is:
  306. *
  307. * get_user_pages()
  308. * -> flush_anon_page
  309. * memcpy() to/from page
  310. * if written to page, flush_dcache_page()
  311. */
  312. void __flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  313. {
  314. unsigned long pfn;
  315. /* VIPT non-aliasing caches need do nothing */
  316. if (cache_is_vipt_nonaliasing())
  317. return;
  318. /*
  319. * Write back and invalidate userspace mapping.
  320. */
  321. pfn = page_to_pfn(page);
  322. if (cache_is_vivt()) {
  323. flush_cache_page(vma, vmaddr, pfn);
  324. } else {
  325. /*
  326. * For aliasing VIPT, we can flush an alias of the
  327. * userspace address only.
  328. */
  329. flush_pfn_alias(pfn, vmaddr);
  330. __flush_icache_all();
  331. }
  332. /*
  333. * Invalidate kernel mapping. No data should be contained
  334. * in this mapping of the page. FIXME: this is overkill
  335. * since we actually ask for a write-back and invalidate.
  336. */
  337. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  338. }