xref: /openbmc/linux/arch/xtensa/mm/fault.c (revision dc6a81c3)
1 // TODO VM_EXEC flag work-around, cache aliasing
2 /*
3  * arch/xtensa/mm/fault.c
4  *
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License.  See the file "COPYING" in the main directory of this archive
7  * for more details.
8  *
9  * Copyright (C) 2001 - 2010 Tensilica Inc.
10  *
11  * Chris Zankel <chris@zankel.net>
12  * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
13  */
14 
15 #include <linux/mm.h>
16 #include <linux/extable.h>
17 #include <linux/hardirq.h>
18 #include <linux/perf_event.h>
19 #include <linux/uaccess.h>
20 #include <asm/mmu_context.h>
21 #include <asm/cacheflush.h>
22 #include <asm/hardirq.h>
23 #include <asm/pgalloc.h>
24 
25 DEFINE_PER_CPU(unsigned long, asid_cache) = ASID_USER_FIRST;
26 void bad_page_fault(struct pt_regs*, unsigned long, int);
27 
28 /*
29  * This routine handles page faults.  It determines the address,
30  * and the problem, and then passes it off to one of the appropriate
31  * routines.
32  *
33  * Note: does not handle Miss and MultiHit.
34  */
35 
36 void do_page_fault(struct pt_regs *regs)
37 {
38 	struct vm_area_struct * vma;
39 	struct mm_struct *mm = current->mm;
40 	unsigned int exccause = regs->exccause;
41 	unsigned int address = regs->excvaddr;
42 	int code;
43 
44 	int is_write, is_exec;
45 	vm_fault_t fault;
46 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
47 
48 	code = SEGV_MAPERR;
49 
50 	/* We fault-in kernel-space virtual memory on-demand. The
51 	 * 'reference' page table is init_mm.pgd.
52 	 */
53 	if (address >= TASK_SIZE && !user_mode(regs))
54 		goto vmalloc_fault;
55 
56 	/* If we're in an interrupt or have no user
57 	 * context, we must not take the fault..
58 	 */
59 	if (faulthandler_disabled() || !mm) {
60 		bad_page_fault(regs, address, SIGSEGV);
61 		return;
62 	}
63 
64 	is_write = (exccause == EXCCAUSE_STORE_CACHE_ATTRIBUTE) ? 1 : 0;
65 	is_exec =  (exccause == EXCCAUSE_ITLB_PRIVILEGE ||
66 		    exccause == EXCCAUSE_ITLB_MISS ||
67 		    exccause == EXCCAUSE_FETCH_CACHE_ATTRIBUTE) ? 1 : 0;
68 
69 	pr_debug("[%s:%d:%08x:%d:%08lx:%s%s]\n",
70 		 current->comm, current->pid,
71 		 address, exccause, regs->pc,
72 		 is_write ? "w" : "", is_exec ? "x" : "");
73 
74 	if (user_mode(regs))
75 		flags |= FAULT_FLAG_USER;
76 retry:
77 	down_read(&mm->mmap_sem);
78 	vma = find_vma(mm, address);
79 
80 	if (!vma)
81 		goto bad_area;
82 	if (vma->vm_start <= address)
83 		goto good_area;
84 	if (!(vma->vm_flags & VM_GROWSDOWN))
85 		goto bad_area;
86 	if (expand_stack(vma, address))
87 		goto bad_area;
88 
89 	/* Ok, we have a good vm_area for this memory access, so
90 	 * we can handle it..
91 	 */
92 
93 good_area:
94 	code = SEGV_ACCERR;
95 
96 	if (is_write) {
97 		if (!(vma->vm_flags & VM_WRITE))
98 			goto bad_area;
99 		flags |= FAULT_FLAG_WRITE;
100 	} else if (is_exec) {
101 		if (!(vma->vm_flags & VM_EXEC))
102 			goto bad_area;
103 	} else	/* Allow read even from write-only pages. */
104 		if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
105 			goto bad_area;
106 
107 	/* If for any reason at all we couldn't handle the fault,
108 	 * make sure we exit gracefully rather than endlessly redo
109 	 * the fault.
110 	 */
111 	fault = handle_mm_fault(vma, address, flags);
112 
113 	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
114 		return;
115 
116 	if (unlikely(fault & VM_FAULT_ERROR)) {
117 		if (fault & VM_FAULT_OOM)
118 			goto out_of_memory;
119 		else if (fault & VM_FAULT_SIGSEGV)
120 			goto bad_area;
121 		else if (fault & VM_FAULT_SIGBUS)
122 			goto do_sigbus;
123 		BUG();
124 	}
125 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
126 		if (fault & VM_FAULT_MAJOR)
127 			current->maj_flt++;
128 		else
129 			current->min_flt++;
130 		if (fault & VM_FAULT_RETRY) {
131 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
132 			flags |= FAULT_FLAG_TRIED;
133 
134 			 /* No need to up_read(&mm->mmap_sem) as we would
135 			 * have already released it in __lock_page_or_retry
136 			 * in mm/filemap.c.
137 			 */
138 
139 			goto retry;
140 		}
141 	}
142 
143 	up_read(&mm->mmap_sem);
144 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
145 	if (flags & VM_FAULT_MAJOR)
146 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
147 	else
148 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
149 
150 	return;
151 
152 	/* Something tried to access memory that isn't in our memory map..
153 	 * Fix it, but check if it's kernel or user first..
154 	 */
155 bad_area:
156 	up_read(&mm->mmap_sem);
157 	if (user_mode(regs)) {
158 		current->thread.bad_vaddr = address;
159 		current->thread.error_code = is_write;
160 		force_sig_fault(SIGSEGV, code, (void *) address);
161 		return;
162 	}
163 	bad_page_fault(regs, address, SIGSEGV);
164 	return;
165 
166 
167 	/* We ran out of memory, or some other thing happened to us that made
168 	 * us unable to handle the page fault gracefully.
169 	 */
170 out_of_memory:
171 	up_read(&mm->mmap_sem);
172 	if (!user_mode(regs))
173 		bad_page_fault(regs, address, SIGKILL);
174 	else
175 		pagefault_out_of_memory();
176 	return;
177 
178 do_sigbus:
179 	up_read(&mm->mmap_sem);
180 
181 	/* Send a sigbus, regardless of whether we were in kernel
182 	 * or user mode.
183 	 */
184 	current->thread.bad_vaddr = address;
185 	force_sig_fault(SIGBUS, BUS_ADRERR, (void *) address);
186 
187 	/* Kernel mode? Handle exceptions or die */
188 	if (!user_mode(regs))
189 		bad_page_fault(regs, address, SIGBUS);
190 	return;
191 
192 vmalloc_fault:
193 	{
194 		/* Synchronize this task's top level page-table
195 		 * with the 'reference' page table.
196 		 */
197 		struct mm_struct *act_mm = current->active_mm;
198 		int index = pgd_index(address);
199 		pgd_t *pgd, *pgd_k;
200 		p4d_t *p4d, *p4d_k;
201 		pud_t *pud, *pud_k;
202 		pmd_t *pmd, *pmd_k;
203 		pte_t *pte_k;
204 
205 		if (act_mm == NULL)
206 			goto bad_page_fault;
207 
208 		pgd = act_mm->pgd + index;
209 		pgd_k = init_mm.pgd + index;
210 
211 		if (!pgd_present(*pgd_k))
212 			goto bad_page_fault;
213 
214 		pgd_val(*pgd) = pgd_val(*pgd_k);
215 
216 		p4d = p4d_offset(pgd, address);
217 		p4d_k = p4d_offset(pgd_k, address);
218 		if (!p4d_present(*p4d) || !p4d_present(*p4d_k))
219 			goto bad_page_fault;
220 
221 		pud = pud_offset(p4d, address);
222 		pud_k = pud_offset(p4d_k, address);
223 		if (!pud_present(*pud) || !pud_present(*pud_k))
224 			goto bad_page_fault;
225 
226 		pmd = pmd_offset(pud, address);
227 		pmd_k = pmd_offset(pud_k, address);
228 		if (!pmd_present(*pmd) || !pmd_present(*pmd_k))
229 			goto bad_page_fault;
230 
231 		pmd_val(*pmd) = pmd_val(*pmd_k);
232 		pte_k = pte_offset_kernel(pmd_k, address);
233 
234 		if (!pte_present(*pte_k))
235 			goto bad_page_fault;
236 		return;
237 	}
238 bad_page_fault:
239 	bad_page_fault(regs, address, SIGKILL);
240 	return;
241 }
242 
243 
244 void
245 bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
246 {
247 	extern void die(const char*, struct pt_regs*, long);
248 	const struct exception_table_entry *entry;
249 
250 	/* Are we prepared to handle this kernel fault?  */
251 	if ((entry = search_exception_tables(regs->pc)) != NULL) {
252 		pr_debug("%s: Exception at pc=%#010lx (%lx)\n",
253 			 current->comm, regs->pc, entry->fixup);
254 		current->thread.bad_uaddr = address;
255 		regs->pc = entry->fixup;
256 		return;
257 	}
258 
259 	/* Oops. The kernel tried to access some bad page. We'll have to
260 	 * terminate things with extreme prejudice.
261 	 */
262 	pr_alert("Unable to handle kernel paging request at virtual "
263 		 "address %08lx\n pc = %08lx, ra = %08lx\n",
264 		 address, regs->pc, regs->areg[0]);
265 	die("Oops", regs, sig);
266 	do_exit(sig);
267 }
268