1 /*
2  *  linux/arch/arm/mm/fault-armv.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Modifications for ARM processor (c) 1995-2001 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/config.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/ptrace.h>
19 #include <linux/mman.h>
20 #include <linux/mm.h>
21 #include <linux/interrupt.h>
22 #include <linux/proc_fs.h>
23 #include <linux/bitops.h>
24 #include <linux/init.h>
25 
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgalloc.h>
29 #include <asm/pgtable.h>
30 
31 extern void show_pte(struct mm_struct *mm, unsigned long addr);
32 extern int do_page_fault(unsigned long addr, int error_code,
33 			 struct pt_regs *regs);
34 extern int do_translation_fault(unsigned long addr, int error_code,
35 				struct pt_regs *regs);
36 extern void do_bad_area(struct task_struct *tsk, struct mm_struct *mm,
37 			unsigned long addr, int error_code,
38 			struct pt_regs *regs);
39 
40 #ifdef CONFIG_ALIGNMENT_TRAP
41 extern int do_alignment(unsigned long addr, int error_code, struct pt_regs *regs);
42 #else
43 #define do_alignment do_bad
44 #endif
45 
46 
47 /*
48  * Some section permission faults need to be handled gracefully.
49  * They can happen due to a __{get,put}_user during an oops.
50  */
51 static int
do_sect_fault(unsigned long addr,int error_code,struct pt_regs * regs)52 do_sect_fault(unsigned long addr, int error_code, struct pt_regs *regs)
53 {
54 	struct task_struct *tsk = current;
55 	do_bad_area(tsk, tsk->active_mm, addr, error_code, regs);
56 	return 0;
57 }
58 
59 /*
60  * Hook for things that need to trap external faults.  Note that
61  * we don't guarantee that this will be the final version of the
62  * interface.
63  */
64 int (*external_fault)(unsigned long addr, struct pt_regs *regs);
65 
66 static int
do_external_fault(unsigned long addr,int error_code,struct pt_regs * regs)67 do_external_fault(unsigned long addr, int error_code, struct pt_regs *regs)
68 {
69 	if (external_fault)
70 		return external_fault(addr, regs);
71 	return 1;
72 }
73 
74 /*
75  * This abort handler always returns "fault".
76  */
77 static int
do_bad(unsigned long addr,int error_code,struct pt_regs * regs)78 do_bad(unsigned long addr, int error_code, struct pt_regs *regs)
79 {
80 	return 1;
81 }
82 
83 static const struct fsr_info {
84 	int	(*fn)(unsigned long addr, int error_code, struct pt_regs *regs);
85 	int	sig;
86 	const char *name;
87 } fsr_info[] = {
88 	{ do_bad,		SIGSEGV, "vector exception"		   },
89 	{ do_alignment,		SIGILL,	 "alignment exception"		   },
90 	{ do_bad,		SIGKILL, "terminal exception"		   },
91 	{ do_alignment,		SIGILL,	 "alignment exception"		   },
92 	{ do_external_fault,	SIGBUS,	 "external abort on linefetch"	   },
93 	{ do_translation_fault,	SIGSEGV, "section translation fault"	   },
94 	{ do_external_fault,	SIGBUS,	 "external abort on linefetch"	   },
95 	{ do_page_fault,	SIGSEGV, "page translation fault"	   },
96 	{ do_external_fault,	SIGBUS,	 "external abort on non-linefetch" },
97 	{ do_bad,		SIGSEGV, "section domain fault"		   },
98 	{ do_external_fault,	SIGBUS,	 "external abort on non-linefetch" },
99 	{ do_bad,		SIGSEGV, "page domain fault"		   },
100 	{ do_bad,		SIGBUS,	 "external abort on translation"   },
101 	{ do_sect_fault,	SIGSEGV, "section permission fault"	   },
102 	{ do_bad,		SIGBUS,	 "external abort on translation"   },
103 	{ do_page_fault,	SIGSEGV, "page permission fault"	   }
104 };
105 
106 /*
107  * Dispatch a data abort to the relevant handler.
108  */
109 asmlinkage void
do_DataAbort(unsigned long addr,int error_code,struct pt_regs * regs,int fsr)110 do_DataAbort(unsigned long addr, int error_code, struct pt_regs *regs, int fsr)
111 {
112 	const struct fsr_info *inf = fsr_info + (fsr & 15);
113 
114 	if (!inf->fn(addr, error_code, regs))
115 		return;
116 
117 	printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
118 		inf->name, fsr, addr);
119 	force_sig(inf->sig, current);
120 	show_pte(current->mm, addr);
121 	die_if_kernel("Oops", regs, 0);
122 }
123 
124 asmlinkage void
do_PrefetchAbort(unsigned long addr,struct pt_regs * regs)125 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
126 {
127 	do_translation_fault(addr, 0, regs);
128 }
129 
130 /*
131  * We take the easy way out of this problem - we make the
132  * PTE uncacheable.  However, we leave the write buffer on.
133  */
adjust_pte(struct vm_area_struct * vma,unsigned long address)134 static void adjust_pte(struct vm_area_struct *vma, unsigned long address)
135 {
136 	pgd_t *pgd;
137 	pmd_t *pmd;
138 	pte_t *pte, entry;
139 
140 	pgd = pgd_offset(vma->vm_mm, address);
141 	if (pgd_none(*pgd))
142 		return;
143 	if (pgd_bad(*pgd))
144 		goto bad_pgd;
145 
146 	pmd = pmd_offset(pgd, address);
147 	if (pmd_none(*pmd))
148 		return;
149 	if (pmd_bad(*pmd))
150 		goto bad_pmd;
151 
152 	pte = pte_offset(pmd, address);
153 	entry = *pte;
154 
155 	/*
156 	 * If this page isn't present, or is already setup to
157 	 * fault (ie, is old), we can safely ignore any issues.
158 	 */
159 	if (pte_present(entry) && pte_val(entry) & L_PTE_CACHEABLE) {
160 		flush_cache_page(vma, address);
161 		pte_val(entry) &= ~L_PTE_CACHEABLE;
162 		set_pte(pte, entry);
163 		flush_tlb_page(vma, address);
164 	}
165 	return;
166 
167 bad_pgd:
168 	pgd_ERROR(*pgd);
169 	pgd_clear(pgd);
170 	return;
171 
172 bad_pmd:
173 	pmd_ERROR(*pmd);
174 	pmd_clear(pmd);
175 	return;
176 }
177 
178 static void
make_coherent(struct vm_area_struct * vma,unsigned long addr,struct page * page)179 make_coherent(struct vm_area_struct *vma, unsigned long addr, struct page *page)
180 {
181 	struct vm_area_struct *mpnt;
182 	struct mm_struct *mm = vma->vm_mm;
183 	unsigned long pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
184 	int aliases = 0;
185 
186 	/*
187 	 * If we have any shared mappings that are in the same mm
188 	 * space, then we need to handle them specially to maintain
189 	 * cache coherency.
190 	 */
191 	for (mpnt = page->mapping->i_mmap_shared; mpnt;
192 	     mpnt = mpnt->vm_next_share) {
193 		unsigned long off;
194 
195 		/*
196 		 * If this VMA is not in our MM, we can ignore it.
197 		 * Note that we intentionally don't mask out the VMA
198 		 * that we are fixing up.
199 		 */
200 		if (mpnt->vm_mm != mm || mpnt == vma)
201 			continue;
202 
203 		/*
204 		 * If the page isn't in this VMA, we can also ignore it.
205 		 */
206 		if (pgoff < mpnt->vm_pgoff)
207 			continue;
208 
209 		off = pgoff - mpnt->vm_pgoff;
210 		if (off >= (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT)
211 			continue;
212 
213 		/*
214 		 * Ok, it is within mpnt.  Fix it up.
215 		 */
216 		adjust_pte(mpnt, mpnt->vm_start + (off << PAGE_SHIFT));
217 		aliases ++;
218 	}
219 	if (aliases)
220 		adjust_pte(vma, addr);
221 }
222 
223 /*
224  * Take care of architecture specific things when placing a new PTE into
225  * a page table, or changing an existing PTE.  Basically, there are two
226  * things that we need to take care of:
227  *
228  *  1. If PG_dcache_dirty is set for the page, we need to ensure
229  *     that any cache entries for the kernels virtual memory
230  *     range are written back to the page.
231  *  2. If we have multiple shared mappings of the same space in
232  *     an object, we need to deal with the cache aliasing issues.
233  *
234  * Note that the page_table_lock will be held.
235  */
update_mmu_cache(struct vm_area_struct * vma,unsigned long addr,pte_t pte)236 void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
237 {
238 	unsigned long pfn = pte_pfn(pte);
239 	struct page *page;
240 
241 	if (!pfn_valid(pfn))
242 		return;
243 	page = pfn_to_page(pfn);
244 	if (page->mapping) {
245 		if (test_and_clear_bit(PG_dcache_dirty, &page->flags)) {
246 			unsigned long kvirt = (unsigned long)page_address(page);
247 			cpu_cache_clean_invalidate_range(kvirt, kvirt + PAGE_SIZE, 0);
248 		}
249 
250 		make_coherent(vma, addr, page);
251 	}
252 }
253