flush.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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_pages(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn, unsigned int nr)
  83. {
  84. if (cache_is_vivt()) {
  85. vivt_flush_cache_pages(vma, user_addr, pfn, nr);
  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_folio(struct address_space *mapping, struct folio *folio)
  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 (!folio_test_highmem(folio)) {
  178. __cpuc_flush_dcache_area(folio_address(folio),
  179. folio_size(folio));
  180. } else {
  181. unsigned long i;
  182. if (cache_is_vipt_nonaliasing()) {
  183. for (i = 0; i < folio_nr_pages(folio); i++) {
  184. void *addr = kmap_local_folio(folio,
  185. i * PAGE_SIZE);
  186. __cpuc_flush_dcache_area(addr, PAGE_SIZE);
  187. kunmap_local(addr);
  188. }
  189. } else {
  190. for (i = 0; i < folio_nr_pages(folio); i++) {
  191. void *addr = kmap_high_get(folio_page(folio, i));
  192. if (addr) {
  193. __cpuc_flush_dcache_area(addr, PAGE_SIZE);
  194. kunmap_high(folio_page(folio, i));
  195. }
  196. }
  197. }
  198. }
  199. /*
  200. * If this is a page cache page, and we have an aliasing VIPT cache,
  201. * we only need to do one flush - which would be at the relevant
  202. * userspace colour, which is congruent with page->index.
  203. */
  204. if (mapping && cache_is_vipt_aliasing())
  205. flush_pfn_alias(folio_pfn(folio), folio_pos(folio));
  206. }
  207. static void __flush_dcache_aliases(struct address_space *mapping, struct folio *folio)
  208. {
  209. struct mm_struct *mm = current->active_mm;
  210. struct vm_area_struct *vma;
  211. pgoff_t pgoff, pgoff_end;
  212. /*
  213. * There are possible user space mappings of this page:
  214. * - VIVT cache: we need to also write back and invalidate all user
  215. * data in the current VM view associated with this page.
  216. * - aliasing VIPT: we only need to find one mapping of this page.
  217. */
  218. pgoff = folio->index;
  219. pgoff_end = pgoff + folio_nr_pages(folio) - 1;
  220. flush_dcache_mmap_lock(mapping);
  221. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff_end) {
  222. unsigned long start, offset, pfn;
  223. unsigned int nr;
  224. /*
  225. * If this VMA is not in our MM, we can ignore it.
  226. */
  227. if (vma->vm_mm != mm)
  228. continue;
  229. if (!(vma->vm_flags & VM_MAYSHARE))
  230. continue;
  231. start = vma->vm_start;
  232. pfn = folio_pfn(folio);
  233. nr = folio_nr_pages(folio);
  234. offset = pgoff - vma->vm_pgoff;
  235. if (offset > -nr) {
  236. pfn -= offset;
  237. nr += offset;
  238. } else {
  239. start += offset * PAGE_SIZE;
  240. }
  241. if (start + nr * PAGE_SIZE > vma->vm_end)
  242. nr = (vma->vm_end - start) / PAGE_SIZE;
  243. flush_cache_pages(vma, start, pfn, nr);
  244. }
  245. flush_dcache_mmap_unlock(mapping);
  246. }
  247. #if __LINUX_ARM_ARCH__ >= 6
  248. void __sync_icache_dcache(pte_t pteval)
  249. {
  250. unsigned long pfn;
  251. struct folio *folio;
  252. struct address_space *mapping;
  253. if (cache_is_vipt_nonaliasing() && !pte_exec(pteval))
  254. /* only flush non-aliasing VIPT caches for exec mappings */
  255. return;
  256. pfn = pte_pfn(pteval);
  257. if (!pfn_valid(pfn))
  258. return;
  259. folio = page_folio(pfn_to_page(pfn));
  260. if (folio_test_reserved(folio))
  261. return;
  262. if (cache_is_vipt_aliasing())
  263. mapping = folio_flush_mapping(folio);
  264. else
  265. mapping = NULL;
  266. if (!test_and_set_bit(PG_dcache_clean, &folio->flags))
  267. __flush_dcache_folio(mapping, folio);
  268. if (pte_exec(pteval))
  269. __flush_icache_all();
  270. }
  271. #endif
  272. /*
  273. * Ensure cache coherency between kernel mapping and userspace mapping
  274. * of this page.
  275. *
  276. * We have three cases to consider:
  277. * - VIPT non-aliasing cache: fully coherent so nothing required.
  278. * - VIVT: fully aliasing, so we need to handle every alias in our
  279. * current VM view.
  280. * - VIPT aliasing: need to handle one alias in our current VM view.
  281. *
  282. * If we need to handle aliasing:
  283. * If the page only exists in the page cache and there are no user
  284. * space mappings, we can be lazy and remember that we may have dirty
  285. * kernel cache lines for later. Otherwise, we assume we have
  286. * aliasing mappings.
  287. *
  288. * Note that we disable the lazy flush for SMP configurations where
  289. * the cache maintenance operations are not automatically broadcasted.
  290. */
  291. void flush_dcache_folio(struct folio *folio)
  292. {
  293. struct address_space *mapping;
  294. /*
  295. * The zero page is never written to, so never has any dirty
  296. * cache lines, and therefore never needs to be flushed.
  297. */
  298. if (is_zero_pfn(folio_pfn(folio)))
  299. return;
  300. if (!cache_ops_need_broadcast() && cache_is_vipt_nonaliasing()) {
  301. if (test_bit(PG_dcache_clean, &folio->flags))
  302. clear_bit(PG_dcache_clean, &folio->flags);
  303. return;
  304. }
  305. mapping = folio_flush_mapping(folio);
  306. if (!cache_ops_need_broadcast() &&
  307. mapping && !folio_mapped(folio))
  308. clear_bit(PG_dcache_clean, &folio->flags);
  309. else {
  310. __flush_dcache_folio(mapping, folio);
  311. if (mapping && cache_is_vivt())
  312. __flush_dcache_aliases(mapping, folio);
  313. else if (mapping)
  314. __flush_icache_all();
  315. set_bit(PG_dcache_clean, &folio->flags);
  316. }
  317. }
  318. EXPORT_SYMBOL(flush_dcache_folio);
  319. void flush_dcache_page(struct page *page)
  320. {
  321. flush_dcache_folio(page_folio(page));
  322. }
  323. EXPORT_SYMBOL(flush_dcache_page);
  324. /*
  325. * Flush an anonymous page so that users of get_user_pages()
  326. * can safely access the data. The expected sequence is:
  327. *
  328. * get_user_pages()
  329. * -> flush_anon_page
  330. * memcpy() to/from page
  331. * if written to page, flush_dcache_page()
  332. */
  333. void __flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr);
  334. void __flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  335. {
  336. unsigned long pfn;
  337. /* VIPT non-aliasing caches need do nothing */
  338. if (cache_is_vipt_nonaliasing())
  339. return;
  340. /*
  341. * Write back and invalidate userspace mapping.
  342. */
  343. pfn = page_to_pfn(page);
  344. if (cache_is_vivt()) {
  345. flush_cache_page(vma, vmaddr, pfn);
  346. } else {
  347. /*
  348. * For aliasing VIPT, we can flush an alias of the
  349. * userspace address only.
  350. */
  351. flush_pfn_alias(pfn, vmaddr);
  352. __flush_icache_all();
  353. }
  354. /*
  355. * Invalidate kernel mapping. No data should be contained
  356. * in this mapping of the page. FIXME: this is overkill
  357. * since we actually ask for a write-back and invalidate.
  358. */
  359. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  360. }