xref: /linux/arch/sh/mm/fault.c (revision c79c3c34f75d72a066e292b10aa50fc758c97c89)
1 /*
2  * Page fault handler for SH with an MMU.
3  *
4  *  Copyright (C) 1999  Niibe Yutaka
5  *  Copyright (C) 2003 - 2012  Paul Mundt
6  *
7  *  Based on linux/arch/i386/mm/fault.c:
8  *   Copyright (C) 1995  Linus Torvalds
9  *
10  * This file is subject to the terms and conditions of the GNU General Public
11  * License.  See the file "COPYING" in the main directory of this archive
12  * for more details.
13  */
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/sched/signal.h>
17 #include <linux/hardirq.h>
18 #include <linux/kprobes.h>
19 #include <linux/perf_event.h>
20 #include <linux/kdebug.h>
21 #include <linux/uaccess.h>
22 #include <asm/io_trapped.h>
23 #include <asm/mmu_context.h>
24 #include <asm/tlbflush.h>
25 #include <asm/traps.h>
26 
27 static void
28 force_sig_info_fault(int si_signo, int si_code, unsigned long address)
29 {
30 	force_sig_fault(si_signo, si_code, (void __user *)address);
31 }
32 
33 /*
34  * This is useful to dump out the page tables associated with
35  * 'addr' in mm 'mm'.
36  */
37 static void show_pte(struct mm_struct *mm, unsigned long addr)
38 {
39 	pgd_t *pgd;
40 
41 	if (mm) {
42 		pgd = mm->pgd;
43 	} else {
44 		pgd = get_TTB();
45 
46 		if (unlikely(!pgd))
47 			pgd = swapper_pg_dir;
48 	}
49 
50 	pr_alert("pgd = %p\n", pgd);
51 	pgd += pgd_index(addr);
52 	pr_alert("[%08lx] *pgd=%0*llx", addr, (u32)(sizeof(*pgd) * 2),
53 		 (u64)pgd_val(*pgd));
54 
55 	do {
56 		p4d_t *p4d;
57 		pud_t *pud;
58 		pmd_t *pmd;
59 		pte_t *pte;
60 
61 		if (pgd_none(*pgd))
62 			break;
63 
64 		if (pgd_bad(*pgd)) {
65 			pr_cont("(bad)");
66 			break;
67 		}
68 
69 		p4d = p4d_offset(pgd, addr);
70 		if (PTRS_PER_P4D != 1)
71 			pr_cont(", *p4d=%0*Lx", (u32)(sizeof(*p4d) * 2),
72 			        (u64)p4d_val(*p4d));
73 
74 		if (p4d_none(*p4d))
75 			break;
76 
77 		if (p4d_bad(*p4d)) {
78 			pr_cont("(bad)");
79 			break;
80 		}
81 
82 		pud = pud_offset(p4d, addr);
83 		if (PTRS_PER_PUD != 1)
84 			pr_cont(", *pud=%0*llx", (u32)(sizeof(*pud) * 2),
85 				(u64)pud_val(*pud));
86 
87 		if (pud_none(*pud))
88 			break;
89 
90 		if (pud_bad(*pud)) {
91 			pr_cont("(bad)");
92 			break;
93 		}
94 
95 		pmd = pmd_offset(pud, addr);
96 		if (PTRS_PER_PMD != 1)
97 			pr_cont(", *pmd=%0*llx", (u32)(sizeof(*pmd) * 2),
98 				(u64)pmd_val(*pmd));
99 
100 		if (pmd_none(*pmd))
101 			break;
102 
103 		if (pmd_bad(*pmd)) {
104 			pr_cont("(bad)");
105 			break;
106 		}
107 
108 		/* We must not map this if we have highmem enabled */
109 		if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
110 			break;
111 
112 		pte = pte_offset_kernel(pmd, addr);
113 		pr_cont(", *pte=%0*llx", (u32)(sizeof(*pte) * 2),
114 			(u64)pte_val(*pte));
115 	} while (0);
116 
117 	pr_cont("\n");
118 }
119 
120 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
121 {
122 	unsigned index = pgd_index(address);
123 	pgd_t *pgd_k;
124 	p4d_t *p4d, *p4d_k;
125 	pud_t *pud, *pud_k;
126 	pmd_t *pmd, *pmd_k;
127 
128 	pgd += index;
129 	pgd_k = init_mm.pgd + index;
130 
131 	if (!pgd_present(*pgd_k))
132 		return NULL;
133 
134 	p4d = p4d_offset(pgd, address);
135 	p4d_k = p4d_offset(pgd_k, address);
136 	if (!p4d_present(*p4d_k))
137 		return NULL;
138 
139 	pud = pud_offset(p4d, address);
140 	pud_k = pud_offset(p4d_k, address);
141 	if (!pud_present(*pud_k))
142 		return NULL;
143 
144 	if (!pud_present(*pud))
145 	    set_pud(pud, *pud_k);
146 
147 	pmd = pmd_offset(pud, address);
148 	pmd_k = pmd_offset(pud_k, address);
149 	if (!pmd_present(*pmd_k))
150 		return NULL;
151 
152 	if (!pmd_present(*pmd))
153 		set_pmd(pmd, *pmd_k);
154 	else {
155 		/*
156 		 * The page tables are fully synchronised so there must
157 		 * be another reason for the fault. Return NULL here to
158 		 * signal that we have not taken care of the fault.
159 		 */
160 		BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
161 		return NULL;
162 	}
163 
164 	return pmd_k;
165 }
166 
167 #ifdef CONFIG_SH_STORE_QUEUES
168 #define __FAULT_ADDR_LIMIT	P3_ADDR_MAX
169 #else
170 #define __FAULT_ADDR_LIMIT	VMALLOC_END
171 #endif
172 
173 /*
174  * Handle a fault on the vmalloc or module mapping area
175  */
176 static noinline int vmalloc_fault(unsigned long address)
177 {
178 	pgd_t *pgd_k;
179 	pmd_t *pmd_k;
180 	pte_t *pte_k;
181 
182 	/* Make sure we are in vmalloc/module/P3 area: */
183 	if (!(address >= VMALLOC_START && address < __FAULT_ADDR_LIMIT))
184 		return -1;
185 
186 	/*
187 	 * Synchronize this task's top level page-table
188 	 * with the 'reference' page table.
189 	 *
190 	 * Do _not_ use "current" here. We might be inside
191 	 * an interrupt in the middle of a task switch..
192 	 */
193 	pgd_k = get_TTB();
194 	pmd_k = vmalloc_sync_one(pgd_k, address);
195 	if (!pmd_k)
196 		return -1;
197 
198 	pte_k = pte_offset_kernel(pmd_k, address);
199 	if (!pte_present(*pte_k))
200 		return -1;
201 
202 	return 0;
203 }
204 
205 static void
206 show_fault_oops(struct pt_regs *regs, unsigned long address)
207 {
208 	if (!oops_may_print())
209 		return;
210 
211 	pr_alert("BUG: unable to handle kernel %s at %08lx\n",
212 		 address < PAGE_SIZE ? "NULL pointer dereference"
213 				     : "paging request",
214 		 address);
215 	pr_alert("PC:");
216 	printk_address(regs->pc, 1);
217 
218 	show_pte(NULL, address);
219 }
220 
221 static noinline void
222 no_context(struct pt_regs *regs, unsigned long error_code,
223 	   unsigned long address)
224 {
225 	/* Are we prepared to handle this kernel fault?  */
226 	if (fixup_exception(regs))
227 		return;
228 
229 	if (handle_trapped_io(regs, address))
230 		return;
231 
232 	/*
233 	 * Oops. The kernel tried to access some bad page. We'll have to
234 	 * terminate things with extreme prejudice.
235 	 */
236 	bust_spinlocks(1);
237 
238 	show_fault_oops(regs, address);
239 
240 	die("Oops", regs, error_code);
241 	bust_spinlocks(0);
242 	do_exit(SIGKILL);
243 }
244 
245 static void
246 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
247 		       unsigned long address, int si_code)
248 {
249 	/* User mode accesses just cause a SIGSEGV */
250 	if (user_mode(regs)) {
251 		/*
252 		 * It's possible to have interrupts off here:
253 		 */
254 		local_irq_enable();
255 
256 		force_sig_info_fault(SIGSEGV, si_code, address);
257 
258 		return;
259 	}
260 
261 	no_context(regs, error_code, address);
262 }
263 
264 static noinline void
265 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
266 		     unsigned long address)
267 {
268 	__bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
269 }
270 
271 static void
272 __bad_area(struct pt_regs *regs, unsigned long error_code,
273 	   unsigned long address, int si_code)
274 {
275 	struct mm_struct *mm = current->mm;
276 
277 	/*
278 	 * Something tried to access memory that isn't in our memory map..
279 	 * Fix it, but check if it's kernel or user first..
280 	 */
281 	mmap_read_unlock(mm);
282 
283 	__bad_area_nosemaphore(regs, error_code, address, si_code);
284 }
285 
286 static noinline void
287 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
288 {
289 	__bad_area(regs, error_code, address, SEGV_MAPERR);
290 }
291 
292 static noinline void
293 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
294 		      unsigned long address)
295 {
296 	__bad_area(regs, error_code, address, SEGV_ACCERR);
297 }
298 
299 static void
300 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
301 {
302 	struct task_struct *tsk = current;
303 	struct mm_struct *mm = tsk->mm;
304 
305 	mmap_read_unlock(mm);
306 
307 	/* Kernel mode? Handle exceptions or die: */
308 	if (!user_mode(regs))
309 		no_context(regs, error_code, address);
310 
311 	force_sig_info_fault(SIGBUS, BUS_ADRERR, address);
312 }
313 
314 static noinline int
315 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
316 	       unsigned long address, vm_fault_t fault)
317 {
318 	/*
319 	 * Pagefault was interrupted by SIGKILL. We have no reason to
320 	 * continue pagefault.
321 	 */
322 	if (fault_signal_pending(fault, regs)) {
323 		if (!user_mode(regs))
324 			no_context(regs, error_code, address);
325 		return 1;
326 	}
327 
328 	/* Release mmap_lock first if necessary */
329 	if (!(fault & VM_FAULT_RETRY))
330 		mmap_read_unlock(current->mm);
331 
332 	if (!(fault & VM_FAULT_ERROR))
333 		return 0;
334 
335 	if (fault & VM_FAULT_OOM) {
336 		/* Kernel mode? Handle exceptions or die: */
337 		if (!user_mode(regs)) {
338 			no_context(regs, error_code, address);
339 			return 1;
340 		}
341 
342 		/*
343 		 * We ran out of memory, call the OOM killer, and return the
344 		 * userspace (which will retry the fault, or kill us if we got
345 		 * oom-killed):
346 		 */
347 		pagefault_out_of_memory();
348 	} else {
349 		if (fault & VM_FAULT_SIGBUS)
350 			do_sigbus(regs, error_code, address);
351 		else if (fault & VM_FAULT_SIGSEGV)
352 			bad_area(regs, error_code, address);
353 		else
354 			BUG();
355 	}
356 
357 	return 1;
358 }
359 
360 static inline int access_error(int error_code, struct vm_area_struct *vma)
361 {
362 	if (error_code & FAULT_CODE_WRITE) {
363 		/* write, present and write, not present: */
364 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
365 			return 1;
366 		return 0;
367 	}
368 
369 	/* ITLB miss on NX page */
370 	if (unlikely((error_code & FAULT_CODE_ITLB) &&
371 		     !(vma->vm_flags & VM_EXEC)))
372 		return 1;
373 
374 	/* read, not present: */
375 	if (unlikely(!vma_is_accessible(vma)))
376 		return 1;
377 
378 	return 0;
379 }
380 
381 static int fault_in_kernel_space(unsigned long address)
382 {
383 	return address >= TASK_SIZE;
384 }
385 
386 /*
387  * This routine handles page faults.  It determines the address,
388  * and the problem, and then passes it off to one of the appropriate
389  * routines.
390  */
391 asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
392 					unsigned long error_code,
393 					unsigned long address)
394 {
395 	unsigned long vec;
396 	struct task_struct *tsk;
397 	struct mm_struct *mm;
398 	struct vm_area_struct * vma;
399 	vm_fault_t fault;
400 	unsigned int flags = FAULT_FLAG_DEFAULT;
401 
402 	tsk = current;
403 	mm = tsk->mm;
404 	vec = lookup_exception_vector();
405 
406 	/*
407 	 * We fault-in kernel-space virtual memory on-demand. The
408 	 * 'reference' page table is init_mm.pgd.
409 	 *
410 	 * NOTE! We MUST NOT take any locks for this case. We may
411 	 * be in an interrupt or a critical region, and should
412 	 * only copy the information from the master page table,
413 	 * nothing more.
414 	 */
415 	if (unlikely(fault_in_kernel_space(address))) {
416 		if (vmalloc_fault(address) >= 0)
417 			return;
418 		if (kprobe_page_fault(regs, vec))
419 			return;
420 
421 		bad_area_nosemaphore(regs, error_code, address);
422 		return;
423 	}
424 
425 	if (unlikely(kprobe_page_fault(regs, vec)))
426 		return;
427 
428 	/* Only enable interrupts if they were on before the fault */
429 	if ((regs->sr & SR_IMASK) != SR_IMASK)
430 		local_irq_enable();
431 
432 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
433 
434 	/*
435 	 * If we're in an interrupt, have no user context or are running
436 	 * with pagefaults disabled then we must not take the fault:
437 	 */
438 	if (unlikely(faulthandler_disabled() || !mm)) {
439 		bad_area_nosemaphore(regs, error_code, address);
440 		return;
441 	}
442 
443 retry:
444 	mmap_read_lock(mm);
445 
446 	vma = find_vma(mm, address);
447 	if (unlikely(!vma)) {
448 		bad_area(regs, error_code, address);
449 		return;
450 	}
451 	if (likely(vma->vm_start <= address))
452 		goto good_area;
453 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
454 		bad_area(regs, error_code, address);
455 		return;
456 	}
457 	if (unlikely(expand_stack(vma, address))) {
458 		bad_area(regs, error_code, address);
459 		return;
460 	}
461 
462 	/*
463 	 * Ok, we have a good vm_area for this memory access, so
464 	 * we can handle it..
465 	 */
466 good_area:
467 	if (unlikely(access_error(error_code, vma))) {
468 		bad_area_access_error(regs, error_code, address);
469 		return;
470 	}
471 
472 	set_thread_fault_code(error_code);
473 
474 	if (user_mode(regs))
475 		flags |= FAULT_FLAG_USER;
476 	if (error_code & FAULT_CODE_WRITE)
477 		flags |= FAULT_FLAG_WRITE;
478 
479 	/*
480 	 * If for any reason at all we couldn't handle the fault,
481 	 * make sure we exit gracefully rather than endlessly redo
482 	 * the fault.
483 	 */
484 	fault = handle_mm_fault(vma, address, flags, regs);
485 
486 	if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
487 		if (mm_fault_error(regs, error_code, address, fault))
488 			return;
489 
490 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
491 		if (fault & VM_FAULT_RETRY) {
492 			flags |= FAULT_FLAG_TRIED;
493 
494 			/*
495 			 * No need to mmap_read_unlock(mm) as we would
496 			 * have already released it in __lock_page_or_retry
497 			 * in mm/filemap.c.
498 			 */
499 			goto retry;
500 		}
501 	}
502 
503 	mmap_read_unlock(mm);
504 }
505