ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / mm / fault-armv.c
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/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/bitops.h>
15 #include <linux/vmalloc.h>
16 #include <linux/init.h>
17
18 #include <asm/cacheflush.h>
19 #include <asm/pgtable.h>
20 #include <asm/tlbflush.h>
21
22 static unsigned long shared_pte_mask = L_PTE_CACHEABLE;
23
24 /*
25  * We take the easy way out of this problem - we make the
26  * PTE uncacheable.  However, we leave the write buffer on.
27  */
28 static int adjust_pte(struct vm_area_struct *vma, unsigned long address)
29 {
30         pgd_t *pgd;
31         pmd_t *pmd;
32         pte_t *pte, entry;
33         int ret = 0;
34
35         pgd = pgd_offset(vma->vm_mm, address);
36         if (pgd_none(*pgd))
37                 goto no_pgd;
38         if (pgd_bad(*pgd))
39                 goto bad_pgd;
40
41         pmd = pmd_offset(pgd, address);
42         if (pmd_none(*pmd))
43                 goto no_pmd;
44         if (pmd_bad(*pmd))
45                 goto bad_pmd;
46
47         pte = pte_offset_map(pmd, address);
48         entry = *pte;
49
50         /*
51          * If this page isn't present, or is already setup to
52          * fault (ie, is old), we can safely ignore any issues.
53          */
54         if (pte_present(entry) && pte_val(entry) & shared_pte_mask) {
55                 flush_cache_page(vma, address);
56                 pte_val(entry) &= ~shared_pte_mask;
57                 set_pte(pte, entry);
58                 flush_tlb_page(vma, address);
59                 ret = 1;
60         }
61         pte_unmap(pte);
62         return ret;
63
64 bad_pgd:
65         pgd_ERROR(*pgd);
66         pgd_clear(pgd);
67 no_pgd:
68         return 0;
69
70 bad_pmd:
71         pmd_ERROR(*pmd);
72         pmd_clear(pmd);
73 no_pmd:
74         return 0;
75 }
76
77 void __flush_dcache_page(struct page *page)
78 {
79         struct address_space *mapping = page_mapping(page);
80         struct mm_struct *mm = current->active_mm;
81         struct list_head *l;
82
83         __cpuc_flush_dcache_page(page_address(page));
84
85         if (!mapping)
86                 return;
87
88         /*
89          * With a VIVT cache, we need to also write back
90          * and invalidate any user data.
91          */
92         list_for_each(l, &mapping->i_mmap_shared) {
93                 struct vm_area_struct *mpnt;
94                 unsigned long off;
95
96                 mpnt = list_entry(l, struct vm_area_struct, shared);
97
98                 /*
99                  * If this VMA is not in our MM, we can ignore it.
100                  */
101                 if (mpnt->vm_mm != mm)
102                         continue;
103
104                 if (page->index < mpnt->vm_pgoff)
105                         continue;
106
107                 off = page->index - mpnt->vm_pgoff;
108                 if (off >= (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT)
109                         continue;
110
111                 flush_cache_page(mpnt, mpnt->vm_start + (off << PAGE_SHIFT));
112         }
113 }
114
115 static void
116 make_coherent(struct vm_area_struct *vma, unsigned long addr, struct page *page, int dirty)
117 {
118         struct address_space *mapping = page_mapping(page);
119         struct list_head *l;
120         struct mm_struct *mm = vma->vm_mm;
121         unsigned long pgoff;
122         int aliases = 0;
123
124         if (!mapping)
125                 return;
126
127         pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
128
129         /*
130          * If we have any shared mappings that are in the same mm
131          * space, then we need to handle them specially to maintain
132          * cache coherency.
133          */
134         list_for_each(l, &mapping->i_mmap_shared) {
135                 struct vm_area_struct *mpnt;
136                 unsigned long off;
137
138                 mpnt = list_entry(l, struct vm_area_struct, shared);
139
140                 /*
141                  * If this VMA is not in our MM, we can ignore it.
142                  * Note that we intentionally mask out the VMA
143                  * that we are fixing up.
144                  */
145                 if (mpnt->vm_mm != mm || mpnt == vma)
146                         continue;
147
148                 /*
149                  * If the page isn't in this VMA, we can also ignore it.
150                  */
151                 if (pgoff < mpnt->vm_pgoff)
152                         continue;
153
154                 off = pgoff - mpnt->vm_pgoff;
155                 if (off >= (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT)
156                         continue;
157
158                 off = mpnt->vm_start + (off << PAGE_SHIFT);
159
160                 /*
161                  * Ok, it is within mpnt.  Fix it up.
162                  */
163                 aliases += adjust_pte(mpnt, off);
164         }
165         if (aliases)
166                 adjust_pte(vma, addr);
167         else
168                 flush_cache_page(vma, addr);
169 }
170
171 /*
172  * Take care of architecture specific things when placing a new PTE into
173  * a page table, or changing an existing PTE.  Basically, there are two
174  * things that we need to take care of:
175  *
176  *  1. If PG_dcache_dirty is set for the page, we need to ensure
177  *     that any cache entries for the kernels virtual memory
178  *     range are written back to the page.
179  *  2. If we have multiple shared mappings of the same space in
180  *     an object, we need to deal with the cache aliasing issues.
181  *
182  * Note that the page_table_lock will be held.
183  */
184 void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
185 {
186         unsigned long pfn = pte_pfn(pte);
187         struct page *page;
188
189         if (!pfn_valid(pfn))
190                 return;
191         page = pfn_to_page(pfn);
192         if (page_mapping(page)) {
193                 int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
194
195                 if (dirty)
196                         __cpuc_flush_dcache_page(page_address(page));
197
198                 make_coherent(vma, addr, page, dirty);
199         }
200 }
201
202 /*
203  * Check whether the write buffer has physical address aliasing
204  * issues.  If it has, we need to avoid them for the case where
205  * we have several shared mappings of the same object in user
206  * space.
207  */
208 static int __init check_writebuffer(unsigned long *p1, unsigned long *p2)
209 {
210         register unsigned long zero = 0, one = 1, val;
211
212         local_irq_disable();
213         mb();
214         *p1 = one;
215         mb();
216         *p2 = zero;
217         mb();
218         val = *p1;
219         mb();
220         local_irq_enable();
221         return val != zero;
222 }
223
224 void __init check_writebuffer_bugs(void)
225 {
226         struct page *page;
227         const char *reason;
228         unsigned long v = 1;
229
230         printk(KERN_INFO "CPU: Testing write buffer coherency: ");
231
232         page = alloc_page(GFP_KERNEL);
233         if (page) {
234                 unsigned long *p1, *p2;
235                 pgprot_t prot = __pgprot(L_PTE_PRESENT|L_PTE_YOUNG|
236                                          L_PTE_DIRTY|L_PTE_WRITE|
237                                          L_PTE_BUFFERABLE);
238
239                 p1 = vmap(&page, 1, VM_IOREMAP, prot);
240                 p2 = vmap(&page, 1, VM_IOREMAP, prot);
241
242                 if (p1 && p2) {
243                         v = check_writebuffer(p1, p2);
244                         reason = "enabling work-around";
245                 } else {
246                         reason = "unable to map memory\n";
247                 }
248
249                 vunmap(p1);
250                 vunmap(p2);
251                 put_page(page);
252         } else {
253                 reason = "unable to grab page\n";
254         }
255
256         if (v) {
257                 printk("failed, %s\n", reason);
258                 shared_pte_mask |= L_PTE_BUFFERABLE;
259         } else {
260                 printk("ok\n");
261         }
262 }