18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/arch/arm/mm/fault-armv.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1995 Linus Torvalds 68c2ecf20Sopenharmony_ci * Modifications for ARM processor (c) 1995-2002 Russell King 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/sched.h> 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/mm.h> 118c2ecf20Sopenharmony_ci#include <linux/bitops.h> 128c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 158c2ecf20Sopenharmony_ci#include <linux/gfp.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <asm/bugs.h> 188c2ecf20Sopenharmony_ci#include <asm/cacheflush.h> 198c2ecf20Sopenharmony_ci#include <asm/cachetype.h> 208c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "mm.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic pteval_t shared_pte_mask = L_PTE_MT_BUFFERABLE; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#if __LINUX_ARM_ARCH__ < 6 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * We take the easy way out of this problem - we make the 298c2ecf20Sopenharmony_ci * PTE uncacheable. However, we leave the write buffer on. 308c2ecf20Sopenharmony_ci * 318c2ecf20Sopenharmony_ci * Note that the pte lock held when calling update_mmu_cache must also 328c2ecf20Sopenharmony_ci * guard the pte (somewhere else in the same mm) that we modify here. 338c2ecf20Sopenharmony_ci * Therefore those configurations which might call adjust_pte (those 348c2ecf20Sopenharmony_ci * without CONFIG_CPU_CACHE_VIPT) cannot support split page_table_lock. 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_cistatic int do_adjust_pte(struct vm_area_struct *vma, unsigned long address, 378c2ecf20Sopenharmony_ci unsigned long pfn, pte_t *ptep) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci pte_t entry = *ptep; 408c2ecf20Sopenharmony_ci int ret; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci /* 438c2ecf20Sopenharmony_ci * If this page is present, it's actually being shared. 448c2ecf20Sopenharmony_ci */ 458c2ecf20Sopenharmony_ci ret = pte_present(entry); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci /* 488c2ecf20Sopenharmony_ci * If this page isn't present, or is already setup to 498c2ecf20Sopenharmony_ci * fault (ie, is old), we can safely ignore any issues. 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_ci if (ret && (pte_val(entry) & L_PTE_MT_MASK) != shared_pte_mask) { 528c2ecf20Sopenharmony_ci flush_cache_page(vma, address, pfn); 538c2ecf20Sopenharmony_ci outer_flush_range((pfn << PAGE_SHIFT), 548c2ecf20Sopenharmony_ci (pfn << PAGE_SHIFT) + PAGE_SIZE); 558c2ecf20Sopenharmony_ci pte_val(entry) &= ~L_PTE_MT_MASK; 568c2ecf20Sopenharmony_ci pte_val(entry) |= shared_pte_mask; 578c2ecf20Sopenharmony_ci set_pte_at(vma->vm_mm, address, ptep, entry); 588c2ecf20Sopenharmony_ci flush_tlb_page(vma, address); 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci return ret; 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#if USE_SPLIT_PTE_PTLOCKS 658c2ecf20Sopenharmony_ci/* 668c2ecf20Sopenharmony_ci * If we are using split PTE locks, then we need to take the page 678c2ecf20Sopenharmony_ci * lock here. Otherwise we are using shared mm->page_table_lock 688c2ecf20Sopenharmony_ci * which is already locked, thus cannot take it. 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_cistatic inline void do_pte_lock(spinlock_t *ptl) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci /* 738c2ecf20Sopenharmony_ci * Use nested version here to indicate that we are already 748c2ecf20Sopenharmony_ci * holding one similar spinlock. 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_ci spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic inline void do_pte_unlock(spinlock_t *ptl) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci spin_unlock(ptl); 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci#else /* !USE_SPLIT_PTE_PTLOCKS */ 848c2ecf20Sopenharmony_cistatic inline void do_pte_lock(spinlock_t *ptl) {} 858c2ecf20Sopenharmony_cistatic inline void do_pte_unlock(spinlock_t *ptl) {} 868c2ecf20Sopenharmony_ci#endif /* USE_SPLIT_PTE_PTLOCKS */ 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic int adjust_pte(struct vm_area_struct *vma, unsigned long address, 898c2ecf20Sopenharmony_ci unsigned long pfn) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci spinlock_t *ptl; 928c2ecf20Sopenharmony_ci pgd_t *pgd; 938c2ecf20Sopenharmony_ci p4d_t *p4d; 948c2ecf20Sopenharmony_ci pud_t *pud; 958c2ecf20Sopenharmony_ci pmd_t *pmd; 968c2ecf20Sopenharmony_ci pte_t *pte; 978c2ecf20Sopenharmony_ci int ret; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci pgd = pgd_offset(vma->vm_mm, address); 1008c2ecf20Sopenharmony_ci if (pgd_none_or_clear_bad(pgd)) 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci p4d = p4d_offset(pgd, address); 1048c2ecf20Sopenharmony_ci if (p4d_none_or_clear_bad(p4d)) 1058c2ecf20Sopenharmony_ci return 0; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci pud = pud_offset(p4d, address); 1088c2ecf20Sopenharmony_ci if (pud_none_or_clear_bad(pud)) 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci pmd = pmd_offset(pud, address); 1128c2ecf20Sopenharmony_ci if (pmd_none_or_clear_bad(pmd)) 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* 1168c2ecf20Sopenharmony_ci * This is called while another page table is mapped, so we 1178c2ecf20Sopenharmony_ci * must use the nested version. This also means we need to 1188c2ecf20Sopenharmony_ci * open-code the spin-locking. 1198c2ecf20Sopenharmony_ci */ 1208c2ecf20Sopenharmony_ci ptl = pte_lockptr(vma->vm_mm, pmd); 1218c2ecf20Sopenharmony_ci pte = pte_offset_map(pmd, address); 1228c2ecf20Sopenharmony_ci do_pte_lock(ptl); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci ret = do_adjust_pte(vma, address, pfn, pte); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci do_pte_unlock(ptl); 1278c2ecf20Sopenharmony_ci pte_unmap(pte); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return ret; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic void 1338c2ecf20Sopenharmony_cimake_coherent(struct address_space *mapping, struct vm_area_struct *vma, 1348c2ecf20Sopenharmony_ci unsigned long addr, pte_t *ptep, unsigned long pfn) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 1378c2ecf20Sopenharmony_ci struct vm_area_struct *mpnt; 1388c2ecf20Sopenharmony_ci unsigned long offset; 1398c2ecf20Sopenharmony_ci pgoff_t pgoff; 1408c2ecf20Sopenharmony_ci int aliases = 0; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* 1458c2ecf20Sopenharmony_ci * If we have any shared mappings that are in the same mm 1468c2ecf20Sopenharmony_ci * space, then we need to handle them specially to maintain 1478c2ecf20Sopenharmony_ci * cache coherency. 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_ci flush_dcache_mmap_lock(mapping); 1508c2ecf20Sopenharmony_ci vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) { 1518c2ecf20Sopenharmony_ci /* 1528c2ecf20Sopenharmony_ci * If this VMA is not in our MM, we can ignore it. 1538c2ecf20Sopenharmony_ci * Note that we intentionally mask out the VMA 1548c2ecf20Sopenharmony_ci * that we are fixing up. 1558c2ecf20Sopenharmony_ci */ 1568c2ecf20Sopenharmony_ci if (mpnt->vm_mm != mm || mpnt == vma) 1578c2ecf20Sopenharmony_ci continue; 1588c2ecf20Sopenharmony_ci if (!(mpnt->vm_flags & VM_MAYSHARE)) 1598c2ecf20Sopenharmony_ci continue; 1608c2ecf20Sopenharmony_ci offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT; 1618c2ecf20Sopenharmony_ci aliases += adjust_pte(mpnt, mpnt->vm_start + offset, pfn); 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci flush_dcache_mmap_unlock(mapping); 1648c2ecf20Sopenharmony_ci if (aliases) 1658c2ecf20Sopenharmony_ci do_adjust_pte(vma, addr, pfn, ptep); 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci/* 1698c2ecf20Sopenharmony_ci * Take care of architecture specific things when placing a new PTE into 1708c2ecf20Sopenharmony_ci * a page table, or changing an existing PTE. Basically, there are two 1718c2ecf20Sopenharmony_ci * things that we need to take care of: 1728c2ecf20Sopenharmony_ci * 1738c2ecf20Sopenharmony_ci * 1. If PG_dcache_clean is not set for the page, we need to ensure 1748c2ecf20Sopenharmony_ci * that any cache entries for the kernels virtual memory 1758c2ecf20Sopenharmony_ci * range are written back to the page. 1768c2ecf20Sopenharmony_ci * 2. If we have multiple shared mappings of the same space in 1778c2ecf20Sopenharmony_ci * an object, we need to deal with the cache aliasing issues. 1788c2ecf20Sopenharmony_ci * 1798c2ecf20Sopenharmony_ci * Note that the pte lock will be held. 1808c2ecf20Sopenharmony_ci */ 1818c2ecf20Sopenharmony_civoid update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, 1828c2ecf20Sopenharmony_ci pte_t *ptep) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci unsigned long pfn = pte_pfn(*ptep); 1858c2ecf20Sopenharmony_ci struct address_space *mapping; 1868c2ecf20Sopenharmony_ci struct page *page; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci if (!pfn_valid(pfn)) 1898c2ecf20Sopenharmony_ci return; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci /* 1928c2ecf20Sopenharmony_ci * The zero page is never written to, so never has any dirty 1938c2ecf20Sopenharmony_ci * cache lines, and therefore never needs to be flushed. 1948c2ecf20Sopenharmony_ci */ 1958c2ecf20Sopenharmony_ci page = pfn_to_page(pfn); 1968c2ecf20Sopenharmony_ci if (page == ZERO_PAGE(0)) 1978c2ecf20Sopenharmony_ci return; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci mapping = page_mapping_file(page); 2008c2ecf20Sopenharmony_ci if (!test_and_set_bit(PG_dcache_clean, &page->flags)) 2018c2ecf20Sopenharmony_ci __flush_dcache_page(mapping, page); 2028c2ecf20Sopenharmony_ci if (mapping) { 2038c2ecf20Sopenharmony_ci if (cache_is_vivt()) 2048c2ecf20Sopenharmony_ci make_coherent(mapping, vma, addr, ptep, pfn); 2058c2ecf20Sopenharmony_ci else if (vma->vm_flags & VM_EXEC) 2068c2ecf20Sopenharmony_ci __flush_icache_all(); 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci#endif /* __LINUX_ARM_ARCH__ < 6 */ 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci/* 2128c2ecf20Sopenharmony_ci * Check whether the write buffer has physical address aliasing 2138c2ecf20Sopenharmony_ci * issues. If it has, we need to avoid them for the case where 2148c2ecf20Sopenharmony_ci * we have several shared mappings of the same object in user 2158c2ecf20Sopenharmony_ci * space. 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_cistatic int __init check_writebuffer(unsigned long *p1, unsigned long *p2) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci register unsigned long zero = 0, one = 1, val; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci local_irq_disable(); 2228c2ecf20Sopenharmony_ci mb(); 2238c2ecf20Sopenharmony_ci *p1 = one; 2248c2ecf20Sopenharmony_ci mb(); 2258c2ecf20Sopenharmony_ci *p2 = zero; 2268c2ecf20Sopenharmony_ci mb(); 2278c2ecf20Sopenharmony_ci val = *p1; 2288c2ecf20Sopenharmony_ci mb(); 2298c2ecf20Sopenharmony_ci local_irq_enable(); 2308c2ecf20Sopenharmony_ci return val != zero; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_civoid __init check_writebuffer_bugs(void) 2348c2ecf20Sopenharmony_ci{ 2358c2ecf20Sopenharmony_ci struct page *page; 2368c2ecf20Sopenharmony_ci const char *reason; 2378c2ecf20Sopenharmony_ci unsigned long v = 1; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci pr_info("CPU: Testing write buffer coherency: "); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci page = alloc_page(GFP_KERNEL); 2428c2ecf20Sopenharmony_ci if (page) { 2438c2ecf20Sopenharmony_ci unsigned long *p1, *p2; 2448c2ecf20Sopenharmony_ci pgprot_t prot = __pgprot_modify(PAGE_KERNEL, 2458c2ecf20Sopenharmony_ci L_PTE_MT_MASK, L_PTE_MT_BUFFERABLE); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci p1 = vmap(&page, 1, VM_IOREMAP, prot); 2488c2ecf20Sopenharmony_ci p2 = vmap(&page, 1, VM_IOREMAP, prot); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci if (p1 && p2) { 2518c2ecf20Sopenharmony_ci v = check_writebuffer(p1, p2); 2528c2ecf20Sopenharmony_ci reason = "enabling work-around"; 2538c2ecf20Sopenharmony_ci } else { 2548c2ecf20Sopenharmony_ci reason = "unable to map memory\n"; 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci vunmap(p1); 2588c2ecf20Sopenharmony_ci vunmap(p2); 2598c2ecf20Sopenharmony_ci put_page(page); 2608c2ecf20Sopenharmony_ci } else { 2618c2ecf20Sopenharmony_ci reason = "unable to grab page\n"; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci if (v) { 2658c2ecf20Sopenharmony_ci pr_cont("failed, %s\n", reason); 2668c2ecf20Sopenharmony_ci shared_pte_mask = L_PTE_MT_UNCACHED; 2678c2ecf20Sopenharmony_ci } else { 2688c2ecf20Sopenharmony_ci pr_cont("ok\n"); 2698c2ecf20Sopenharmony_ci } 2708c2ecf20Sopenharmony_ci} 271