Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mm/fault-armv.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | * Modifications for ARM processor (c) 1995-2002 Russell King |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | |
| 20 | #include <asm/cacheflush.h> |
| 21 | #include <asm/pgtable.h> |
| 22 | #include <asm/tlbflush.h> |
| 23 | |
Russell King | bb30f36 | 2008-09-06 20:04:59 +0100 | [diff] [blame^] | 24 | static unsigned long shared_pte_mask = L_PTE_MT_BUFFERABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * We take the easy way out of this problem - we make the |
| 28 | * PTE uncacheable. However, we leave the write buffer on. |
Hugh Dickins | 69b0475 | 2005-10-29 18:16:36 -0700 | [diff] [blame] | 29 | * |
| 30 | * Note that the pte lock held when calling update_mmu_cache must also |
| 31 | * guard the pte (somewhere else in the same mm) that we modify here. |
| 32 | * Therefore those configurations which might call adjust_pte (those |
| 33 | * without CONFIG_CPU_CACHE_VIPT) cannot support split page_table_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | static int adjust_pte(struct vm_area_struct *vma, unsigned long address) |
| 36 | { |
| 37 | pgd_t *pgd; |
| 38 | pmd_t *pmd; |
| 39 | pte_t *pte, entry; |
Russell King | 53cdb27 | 2008-07-27 10:35:54 +0100 | [diff] [blame] | 40 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | pgd = pgd_offset(vma->vm_mm, address); |
| 43 | if (pgd_none(*pgd)) |
| 44 | goto no_pgd; |
| 45 | if (pgd_bad(*pgd)) |
| 46 | goto bad_pgd; |
| 47 | |
| 48 | pmd = pmd_offset(pgd, address); |
| 49 | if (pmd_none(*pmd)) |
| 50 | goto no_pmd; |
| 51 | if (pmd_bad(*pmd)) |
| 52 | goto bad_pmd; |
| 53 | |
| 54 | pte = pte_offset_map(pmd, address); |
| 55 | entry = *pte; |
| 56 | |
| 57 | /* |
Russell King | 53cdb27 | 2008-07-27 10:35:54 +0100 | [diff] [blame] | 58 | * If this page is present, it's actually being shared. |
| 59 | */ |
| 60 | ret = pte_present(entry); |
| 61 | |
| 62 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * If this page isn't present, or is already setup to |
| 64 | * fault (ie, is old), we can safely ignore any issues. |
| 65 | */ |
Russell King | bb30f36 | 2008-09-06 20:04:59 +0100 | [diff] [blame^] | 66 | if (ret && (pte_val(entry) & L_PTE_MT_MASK) != shared_pte_mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | flush_cache_page(vma, address, pte_pfn(entry)); |
Russell King | bb30f36 | 2008-09-06 20:04:59 +0100 | [diff] [blame^] | 68 | pte_val(entry) &= ~L_PTE_MT_MASK; |
| 69 | pte_val(entry) |= shared_pte_mask; |
Russell King | ad1ae2f | 2006-12-13 14:34:43 +0000 | [diff] [blame] | 70 | set_pte_at(vma->vm_mm, address, pte, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | flush_tlb_page(vma, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | pte_unmap(pte); |
| 74 | return ret; |
| 75 | |
| 76 | bad_pgd: |
| 77 | pgd_ERROR(*pgd); |
| 78 | pgd_clear(pgd); |
| 79 | no_pgd: |
| 80 | return 0; |
| 81 | |
| 82 | bad_pmd: |
| 83 | pmd_ERROR(*pmd); |
| 84 | pmd_clear(pmd); |
| 85 | no_pmd: |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 90 | make_coherent(struct address_space *mapping, struct vm_area_struct *vma, unsigned long addr, unsigned long pfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | struct mm_struct *mm = vma->vm_mm; |
| 93 | struct vm_area_struct *mpnt; |
| 94 | struct prio_tree_iter iter; |
| 95 | unsigned long offset; |
| 96 | pgoff_t pgoff; |
| 97 | int aliases = 0; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT); |
| 100 | |
| 101 | /* |
| 102 | * If we have any shared mappings that are in the same mm |
| 103 | * space, then we need to handle them specially to maintain |
| 104 | * cache coherency. |
| 105 | */ |
| 106 | flush_dcache_mmap_lock(mapping); |
| 107 | vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) { |
| 108 | /* |
| 109 | * If this VMA is not in our MM, we can ignore it. |
| 110 | * Note that we intentionally mask out the VMA |
| 111 | * that we are fixing up. |
| 112 | */ |
| 113 | if (mpnt->vm_mm != mm || mpnt == vma) |
| 114 | continue; |
| 115 | if (!(mpnt->vm_flags & VM_MAYSHARE)) |
| 116 | continue; |
| 117 | offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT; |
| 118 | aliases += adjust_pte(mpnt, mpnt->vm_start + offset); |
| 119 | } |
| 120 | flush_dcache_mmap_unlock(mapping); |
| 121 | if (aliases) |
| 122 | adjust_pte(vma, addr); |
| 123 | else |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 124 | flush_cache_page(vma, addr, pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Take care of architecture specific things when placing a new PTE into |
| 129 | * a page table, or changing an existing PTE. Basically, there are two |
| 130 | * things that we need to take care of: |
| 131 | * |
| 132 | * 1. If PG_dcache_dirty is set for the page, we need to ensure |
| 133 | * that any cache entries for the kernels virtual memory |
| 134 | * range are written back to the page. |
| 135 | * 2. If we have multiple shared mappings of the same space in |
| 136 | * an object, we need to deal with the cache aliasing issues. |
| 137 | * |
Hugh Dickins | 69b0475 | 2005-10-29 18:16:36 -0700 | [diff] [blame] | 138 | * Note that the pte lock will be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | */ |
| 140 | void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte) |
| 141 | { |
| 142 | unsigned long pfn = pte_pfn(pte); |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 143 | struct address_space *mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | struct page *page; |
| 145 | |
| 146 | if (!pfn_valid(pfn)) |
| 147 | return; |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | page = pfn_to_page(pfn); |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 150 | mapping = page_mapping(page); |
| 151 | if (mapping) { |
Catalin Marinas | 826cbda | 2008-06-13 10:28:36 +0100 | [diff] [blame] | 152 | #ifndef CONFIG_SMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags); |
| 154 | |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 155 | if (dirty) |
| 156 | __flush_dcache_page(mapping, page); |
Catalin Marinas | 826cbda | 2008-06-13 10:28:36 +0100 | [diff] [blame] | 157 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | if (cache_is_vivt()) |
Russell King | 8830f04 | 2005-06-20 09:51:03 +0100 | [diff] [blame] | 160 | make_coherent(mapping, vma, addr, pfn); |
Catalin Marinas | 826cbda | 2008-06-13 10:28:36 +0100 | [diff] [blame] | 161 | else if (vma->vm_flags & VM_EXEC) |
| 162 | __flush_icache_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Check whether the write buffer has physical address aliasing |
| 168 | * issues. If it has, we need to avoid them for the case where |
| 169 | * we have several shared mappings of the same object in user |
| 170 | * space. |
| 171 | */ |
| 172 | static int __init check_writebuffer(unsigned long *p1, unsigned long *p2) |
| 173 | { |
| 174 | register unsigned long zero = 0, one = 1, val; |
| 175 | |
| 176 | local_irq_disable(); |
| 177 | mb(); |
| 178 | *p1 = one; |
| 179 | mb(); |
| 180 | *p2 = zero; |
| 181 | mb(); |
| 182 | val = *p1; |
| 183 | mb(); |
| 184 | local_irq_enable(); |
| 185 | return val != zero; |
| 186 | } |
| 187 | |
| 188 | void __init check_writebuffer_bugs(void) |
| 189 | { |
| 190 | struct page *page; |
| 191 | const char *reason; |
| 192 | unsigned long v = 1; |
| 193 | |
| 194 | printk(KERN_INFO "CPU: Testing write buffer coherency: "); |
| 195 | |
| 196 | page = alloc_page(GFP_KERNEL); |
| 197 | if (page) { |
| 198 | unsigned long *p1, *p2; |
| 199 | pgprot_t prot = __pgprot(L_PTE_PRESENT|L_PTE_YOUNG| |
| 200 | L_PTE_DIRTY|L_PTE_WRITE| |
Russell King | bb30f36 | 2008-09-06 20:04:59 +0100 | [diff] [blame^] | 201 | L_PTE_MT_BUFFERABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | p1 = vmap(&page, 1, VM_IOREMAP, prot); |
| 204 | p2 = vmap(&page, 1, VM_IOREMAP, prot); |
| 205 | |
| 206 | if (p1 && p2) { |
| 207 | v = check_writebuffer(p1, p2); |
| 208 | reason = "enabling work-around"; |
| 209 | } else { |
| 210 | reason = "unable to map memory\n"; |
| 211 | } |
| 212 | |
| 213 | vunmap(p1); |
| 214 | vunmap(p2); |
| 215 | put_page(page); |
| 216 | } else { |
| 217 | reason = "unable to grab page\n"; |
| 218 | } |
| 219 | |
| 220 | if (v) { |
| 221 | printk("failed, %s\n", reason); |
Russell King | bb30f36 | 2008-09-06 20:04:59 +0100 | [diff] [blame^] | 222 | shared_pte_mask = L_PTE_MT_UNCACHED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } else { |
| 224 | printk("ok\n"); |
| 225 | } |
| 226 | } |