flush.c 11 KB

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