xref: /openbmc/linux/arch/sh/mm/fault.c (revision c1e8d7c6a7a682e1405e3e242d32fc377fd196ff)
128080329SPaul Mundt /*
228080329SPaul Mundt  * Page fault handler for SH with an MMU.
328080329SPaul Mundt  *
428080329SPaul Mundt  *  Copyright (C) 1999  Niibe Yutaka
528080329SPaul Mundt  *  Copyright (C) 2003 - 2012  Paul Mundt
628080329SPaul Mundt  *
728080329SPaul Mundt  *  Based on linux/arch/i386/mm/fault.c:
828080329SPaul Mundt  *   Copyright (C) 1995  Linus Torvalds
928080329SPaul Mundt  *
1028080329SPaul Mundt  * This file is subject to the terms and conditions of the GNU General Public
1128080329SPaul Mundt  * License.  See the file "COPYING" in the main directory of this archive
1228080329SPaul Mundt  * for more details.
1328080329SPaul Mundt  */
1428080329SPaul Mundt #include <linux/kernel.h>
1528080329SPaul Mundt #include <linux/mm.h>
163f07c014SIngo Molnar #include <linux/sched/signal.h>
1728080329SPaul Mundt #include <linux/hardirq.h>
1828080329SPaul Mundt #include <linux/kprobes.h>
1928080329SPaul Mundt #include <linux/perf_event.h>
2028080329SPaul Mundt #include <linux/kdebug.h>
2170ffdb93SDavid Hildenbrand #include <linux/uaccess.h>
2228080329SPaul Mundt #include <asm/io_trapped.h>
2328080329SPaul Mundt #include <asm/mmu_context.h>
2428080329SPaul Mundt #include <asm/tlbflush.h>
2528080329SPaul Mundt #include <asm/traps.h>
2628080329SPaul Mundt 
2728080329SPaul Mundt static void
28e1656829SEric W. Biederman force_sig_info_fault(int si_signo, int si_code, unsigned long address)
2928080329SPaul Mundt {
302e1661d2SEric W. Biederman 	force_sig_fault(si_signo, si_code, (void __user *)address);
3128080329SPaul Mundt }
3228080329SPaul Mundt 
3328080329SPaul Mundt /*
3428080329SPaul Mundt  * This is useful to dump out the page tables associated with
3528080329SPaul Mundt  * 'addr' in mm 'mm'.
3628080329SPaul Mundt  */
3728080329SPaul Mundt static void show_pte(struct mm_struct *mm, unsigned long addr)
3828080329SPaul Mundt {
3928080329SPaul Mundt 	pgd_t *pgd;
4028080329SPaul Mundt 
4190eed7d8SPaul Mundt 	if (mm) {
4228080329SPaul Mundt 		pgd = mm->pgd;
4390eed7d8SPaul Mundt 	} else {
4428080329SPaul Mundt 		pgd = get_TTB();
4528080329SPaul Mundt 
4690eed7d8SPaul Mundt 		if (unlikely(!pgd))
4790eed7d8SPaul Mundt 			pgd = swapper_pg_dir;
4890eed7d8SPaul Mundt 	}
4990eed7d8SPaul Mundt 
50eaabf98bSGeert Uytterhoeven 	pr_alert("pgd = %p\n", pgd);
5128080329SPaul Mundt 	pgd += pgd_index(addr);
52eaabf98bSGeert Uytterhoeven 	pr_alert("[%08lx] *pgd=%0*llx", addr, (u32)(sizeof(*pgd) * 2),
53eaabf98bSGeert Uytterhoeven 		 (u64)pgd_val(*pgd));
5428080329SPaul Mundt 
5528080329SPaul Mundt 	do {
56874e2cc1SMike Rapoport 		p4d_t *p4d;
5728080329SPaul Mundt 		pud_t *pud;
5828080329SPaul Mundt 		pmd_t *pmd;
5928080329SPaul Mundt 		pte_t *pte;
6028080329SPaul Mundt 
6128080329SPaul Mundt 		if (pgd_none(*pgd))
6228080329SPaul Mundt 			break;
6328080329SPaul Mundt 
6428080329SPaul Mundt 		if (pgd_bad(*pgd)) {
65eaabf98bSGeert Uytterhoeven 			pr_cont("(bad)");
6628080329SPaul Mundt 			break;
6728080329SPaul Mundt 		}
6828080329SPaul Mundt 
69874e2cc1SMike Rapoport 		p4d = p4d_offset(pgd, addr);
70874e2cc1SMike Rapoport 		if (PTRS_PER_P4D != 1)
71874e2cc1SMike Rapoport 			pr_cont(", *p4d=%0*Lx", (u32)(sizeof(*p4d) * 2),
72874e2cc1SMike Rapoport 			        (u64)p4d_val(*p4d));
73874e2cc1SMike Rapoport 
74874e2cc1SMike Rapoport 		if (p4d_none(*p4d))
75874e2cc1SMike Rapoport 			break;
76874e2cc1SMike Rapoport 
77874e2cc1SMike Rapoport 		if (p4d_bad(*p4d)) {
78874e2cc1SMike Rapoport 			pr_cont("(bad)");
79874e2cc1SMike Rapoport 			break;
80874e2cc1SMike Rapoport 		}
81874e2cc1SMike Rapoport 
82874e2cc1SMike Rapoport 		pud = pud_offset(p4d, addr);
8328080329SPaul Mundt 		if (PTRS_PER_PUD != 1)
84eaabf98bSGeert Uytterhoeven 			pr_cont(", *pud=%0*llx", (u32)(sizeof(*pud) * 2),
8528080329SPaul Mundt 				(u64)pud_val(*pud));
8628080329SPaul Mundt 
8728080329SPaul Mundt 		if (pud_none(*pud))
8828080329SPaul Mundt 			break;
8928080329SPaul Mundt 
9028080329SPaul Mundt 		if (pud_bad(*pud)) {
91eaabf98bSGeert Uytterhoeven 			pr_cont("(bad)");
9228080329SPaul Mundt 			break;
9328080329SPaul Mundt 		}
9428080329SPaul Mundt 
9528080329SPaul Mundt 		pmd = pmd_offset(pud, addr);
9628080329SPaul Mundt 		if (PTRS_PER_PMD != 1)
97eaabf98bSGeert Uytterhoeven 			pr_cont(", *pmd=%0*llx", (u32)(sizeof(*pmd) * 2),
9828080329SPaul Mundt 				(u64)pmd_val(*pmd));
9928080329SPaul Mundt 
10028080329SPaul Mundt 		if (pmd_none(*pmd))
10128080329SPaul Mundt 			break;
10228080329SPaul Mundt 
10328080329SPaul Mundt 		if (pmd_bad(*pmd)) {
104eaabf98bSGeert Uytterhoeven 			pr_cont("(bad)");
10528080329SPaul Mundt 			break;
10628080329SPaul Mundt 		}
10728080329SPaul Mundt 
10828080329SPaul Mundt 		/* We must not map this if we have highmem enabled */
10928080329SPaul Mundt 		if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
11028080329SPaul Mundt 			break;
11128080329SPaul Mundt 
11228080329SPaul Mundt 		pte = pte_offset_kernel(pmd, addr);
113eaabf98bSGeert Uytterhoeven 		pr_cont(", *pte=%0*llx", (u32)(sizeof(*pte) * 2),
11428080329SPaul Mundt 			(u64)pte_val(*pte));
11528080329SPaul Mundt 	} while (0);
11628080329SPaul Mundt 
117eaabf98bSGeert Uytterhoeven 	pr_cont("\n");
11828080329SPaul Mundt }
11928080329SPaul Mundt 
12028080329SPaul Mundt static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
12128080329SPaul Mundt {
12228080329SPaul Mundt 	unsigned index = pgd_index(address);
12328080329SPaul Mundt 	pgd_t *pgd_k;
124874e2cc1SMike Rapoport 	p4d_t *p4d, *p4d_k;
12528080329SPaul Mundt 	pud_t *pud, *pud_k;
12628080329SPaul Mundt 	pmd_t *pmd, *pmd_k;
12728080329SPaul Mundt 
12828080329SPaul Mundt 	pgd += index;
12928080329SPaul Mundt 	pgd_k = init_mm.pgd + index;
13028080329SPaul Mundt 
13128080329SPaul Mundt 	if (!pgd_present(*pgd_k))
13228080329SPaul Mundt 		return NULL;
13328080329SPaul Mundt 
134874e2cc1SMike Rapoport 	p4d = p4d_offset(pgd, address);
135874e2cc1SMike Rapoport 	p4d_k = p4d_offset(pgd_k, address);
136874e2cc1SMike Rapoport 	if (!p4d_present(*p4d_k))
137874e2cc1SMike Rapoport 		return NULL;
138874e2cc1SMike Rapoport 
139874e2cc1SMike Rapoport 	pud = pud_offset(p4d, address);
140874e2cc1SMike Rapoport 	pud_k = pud_offset(p4d_k, address);
14128080329SPaul Mundt 	if (!pud_present(*pud_k))
14228080329SPaul Mundt 		return NULL;
14328080329SPaul Mundt 
14428080329SPaul Mundt 	if (!pud_present(*pud))
14528080329SPaul Mundt 	    set_pud(pud, *pud_k);
14628080329SPaul Mundt 
14728080329SPaul Mundt 	pmd = pmd_offset(pud, address);
14828080329SPaul Mundt 	pmd_k = pmd_offset(pud_k, address);
14928080329SPaul Mundt 	if (!pmd_present(*pmd_k))
15028080329SPaul Mundt 		return NULL;
15128080329SPaul Mundt 
15228080329SPaul Mundt 	if (!pmd_present(*pmd))
15328080329SPaul Mundt 		set_pmd(pmd, *pmd_k);
15428080329SPaul Mundt 	else {
15528080329SPaul Mundt 		/*
15628080329SPaul Mundt 		 * The page tables are fully synchronised so there must
15728080329SPaul Mundt 		 * be another reason for the fault. Return NULL here to
15828080329SPaul Mundt 		 * signal that we have not taken care of the fault.
15928080329SPaul Mundt 		 */
16028080329SPaul Mundt 		BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
16128080329SPaul Mundt 		return NULL;
16228080329SPaul Mundt 	}
16328080329SPaul Mundt 
16428080329SPaul Mundt 	return pmd_k;
16528080329SPaul Mundt }
16628080329SPaul Mundt 
167d8fd35fcSPaul Mundt #ifdef CONFIG_SH_STORE_QUEUES
168d8fd35fcSPaul Mundt #define __FAULT_ADDR_LIMIT	P3_ADDR_MAX
169d8fd35fcSPaul Mundt #else
170d8fd35fcSPaul Mundt #define __FAULT_ADDR_LIMIT	VMALLOC_END
171d8fd35fcSPaul Mundt #endif
172d8fd35fcSPaul Mundt 
17328080329SPaul Mundt /*
17428080329SPaul Mundt  * Handle a fault on the vmalloc or module mapping area
17528080329SPaul Mundt  */
17628080329SPaul Mundt static noinline int vmalloc_fault(unsigned long address)
17728080329SPaul Mundt {
17828080329SPaul Mundt 	pgd_t *pgd_k;
17928080329SPaul Mundt 	pmd_t *pmd_k;
18028080329SPaul Mundt 	pte_t *pte_k;
18128080329SPaul Mundt 
182c3e0af98SPaul Mundt 	/* Make sure we are in vmalloc/module/P3 area: */
183d8fd35fcSPaul Mundt 	if (!(address >= VMALLOC_START && address < __FAULT_ADDR_LIMIT))
18428080329SPaul Mundt 		return -1;
18528080329SPaul Mundt 
18628080329SPaul Mundt 	/*
18728080329SPaul Mundt 	 * Synchronize this task's top level page-table
18828080329SPaul Mundt 	 * with the 'reference' page table.
18928080329SPaul Mundt 	 *
19028080329SPaul Mundt 	 * Do _not_ use "current" here. We might be inside
19128080329SPaul Mundt 	 * an interrupt in the middle of a task switch..
19228080329SPaul Mundt 	 */
19328080329SPaul Mundt 	pgd_k = get_TTB();
19428080329SPaul Mundt 	pmd_k = vmalloc_sync_one(pgd_k, address);
19528080329SPaul Mundt 	if (!pmd_k)
19628080329SPaul Mundt 		return -1;
19728080329SPaul Mundt 
19828080329SPaul Mundt 	pte_k = pte_offset_kernel(pmd_k, address);
19928080329SPaul Mundt 	if (!pte_present(*pte_k))
20028080329SPaul Mundt 		return -1;
20128080329SPaul Mundt 
20228080329SPaul Mundt 	return 0;
20328080329SPaul Mundt }
20428080329SPaul Mundt 
20528080329SPaul Mundt static void
20628080329SPaul Mundt show_fault_oops(struct pt_regs *regs, unsigned long address)
20728080329SPaul Mundt {
20828080329SPaul Mundt 	if (!oops_may_print())
20928080329SPaul Mundt 		return;
21028080329SPaul Mundt 
21128080329SPaul Mundt 	printk(KERN_ALERT "PC:");
212eaabf98bSGeert Uytterhoeven 	pr_alert("BUG: unable to handle kernel %s at %08lx\n",
213eaabf98bSGeert Uytterhoeven 		 address < PAGE_SIZE ? "NULL pointer dereference"
214eaabf98bSGeert Uytterhoeven 				     : "paging request",
215eaabf98bSGeert Uytterhoeven 		 address);
216eaabf98bSGeert Uytterhoeven 	pr_alert("PC:");
2172deebe4dSDmitry Safonov 	printk_address(regs->pc, 1, KERN_ALERT);
21828080329SPaul Mundt 
21928080329SPaul Mundt 	show_pte(NULL, address);
22028080329SPaul Mundt }
22128080329SPaul Mundt 
22228080329SPaul Mundt static noinline void
22328080329SPaul Mundt no_context(struct pt_regs *regs, unsigned long error_code,
22428080329SPaul Mundt 	   unsigned long address)
22528080329SPaul Mundt {
22628080329SPaul Mundt 	/* Are we prepared to handle this kernel fault?  */
22728080329SPaul Mundt 	if (fixup_exception(regs))
22828080329SPaul Mundt 		return;
22928080329SPaul Mundt 
23028080329SPaul Mundt 	if (handle_trapped_io(regs, address))
23128080329SPaul Mundt 		return;
23228080329SPaul Mundt 
23328080329SPaul Mundt 	/*
23428080329SPaul Mundt 	 * Oops. The kernel tried to access some bad page. We'll have to
23528080329SPaul Mundt 	 * terminate things with extreme prejudice.
23628080329SPaul Mundt 	 */
23728080329SPaul Mundt 	bust_spinlocks(1);
23828080329SPaul Mundt 
23928080329SPaul Mundt 	show_fault_oops(regs, address);
24028080329SPaul Mundt 
24128080329SPaul Mundt 	die("Oops", regs, error_code);
24228080329SPaul Mundt 	bust_spinlocks(0);
24328080329SPaul Mundt 	do_exit(SIGKILL);
24428080329SPaul Mundt }
24528080329SPaul Mundt 
24628080329SPaul Mundt static void
24728080329SPaul Mundt __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
24828080329SPaul Mundt 		       unsigned long address, int si_code)
24928080329SPaul Mundt {
25028080329SPaul Mundt 	/* User mode accesses just cause a SIGSEGV */
25128080329SPaul Mundt 	if (user_mode(regs)) {
25228080329SPaul Mundt 		/*
25328080329SPaul Mundt 		 * It's possible to have interrupts off here:
25428080329SPaul Mundt 		 */
25528080329SPaul Mundt 		local_irq_enable();
25628080329SPaul Mundt 
257e1656829SEric W. Biederman 		force_sig_info_fault(SIGSEGV, si_code, address);
25828080329SPaul Mundt 
25928080329SPaul Mundt 		return;
26028080329SPaul Mundt 	}
26128080329SPaul Mundt 
26228080329SPaul Mundt 	no_context(regs, error_code, address);
26328080329SPaul Mundt }
26428080329SPaul Mundt 
26528080329SPaul Mundt static noinline void
26628080329SPaul Mundt bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
26728080329SPaul Mundt 		     unsigned long address)
26828080329SPaul Mundt {
26928080329SPaul Mundt 	__bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
27028080329SPaul Mundt }
27128080329SPaul Mundt 
27228080329SPaul Mundt static void
27328080329SPaul Mundt __bad_area(struct pt_regs *regs, unsigned long error_code,
27428080329SPaul Mundt 	   unsigned long address, int si_code)
27528080329SPaul Mundt {
27628080329SPaul Mundt 	struct mm_struct *mm = current->mm;
27728080329SPaul Mundt 
27828080329SPaul Mundt 	/*
27928080329SPaul Mundt 	 * Something tried to access memory that isn't in our memory map..
28028080329SPaul Mundt 	 * Fix it, but check if it's kernel or user first..
28128080329SPaul Mundt 	 */
282d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
28328080329SPaul Mundt 
28428080329SPaul Mundt 	__bad_area_nosemaphore(regs, error_code, address, si_code);
28528080329SPaul Mundt }
28628080329SPaul Mundt 
28728080329SPaul Mundt static noinline void
28828080329SPaul Mundt bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
28928080329SPaul Mundt {
29028080329SPaul Mundt 	__bad_area(regs, error_code, address, SEGV_MAPERR);
29128080329SPaul Mundt }
29228080329SPaul Mundt 
29328080329SPaul Mundt static noinline void
29428080329SPaul Mundt bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
29528080329SPaul Mundt 		      unsigned long address)
29628080329SPaul Mundt {
29728080329SPaul Mundt 	__bad_area(regs, error_code, address, SEGV_ACCERR);
29828080329SPaul Mundt }
29928080329SPaul Mundt 
30028080329SPaul Mundt static void
30128080329SPaul Mundt do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
30228080329SPaul Mundt {
30328080329SPaul Mundt 	struct task_struct *tsk = current;
30428080329SPaul Mundt 	struct mm_struct *mm = tsk->mm;
30528080329SPaul Mundt 
306d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
30728080329SPaul Mundt 
30828080329SPaul Mundt 	/* Kernel mode? Handle exceptions or die: */
30928080329SPaul Mundt 	if (!user_mode(regs))
31028080329SPaul Mundt 		no_context(regs, error_code, address);
31128080329SPaul Mundt 
312e1656829SEric W. Biederman 	force_sig_info_fault(SIGBUS, BUS_ADRERR, address);
31328080329SPaul Mundt }
31428080329SPaul Mundt 
31528080329SPaul Mundt static noinline int
31628080329SPaul Mundt mm_fault_error(struct pt_regs *regs, unsigned long error_code,
31750a7ca3cSSouptick Joarder 	       unsigned long address, vm_fault_t fault)
31828080329SPaul Mundt {
31928080329SPaul Mundt 	/*
32028080329SPaul Mundt 	 * Pagefault was interrupted by SIGKILL. We have no reason to
32128080329SPaul Mundt 	 * continue pagefault.
32228080329SPaul Mundt 	 */
323fb027adaSPeter Xu 	if (fault_signal_pending(fault, regs)) {
32428080329SPaul Mundt 		if (!user_mode(regs))
32528080329SPaul Mundt 			no_context(regs, error_code, address);
32628080329SPaul Mundt 		return 1;
32728080329SPaul Mundt 	}
32828080329SPaul Mundt 
329*c1e8d7c6SMichel Lespinasse 	/* Release mmap_lock first if necessary */
330fb027adaSPeter Xu 	if (!(fault & VM_FAULT_RETRY))
331d8ed45c5SMichel Lespinasse 		mmap_read_unlock(current->mm);
332fb027adaSPeter Xu 
33328080329SPaul Mundt 	if (!(fault & VM_FAULT_ERROR))
33428080329SPaul Mundt 		return 0;
33528080329SPaul Mundt 
33628080329SPaul Mundt 	if (fault & VM_FAULT_OOM) {
33728080329SPaul Mundt 		/* Kernel mode? Handle exceptions or die: */
33828080329SPaul Mundt 		if (!user_mode(regs)) {
33928080329SPaul Mundt 			no_context(regs, error_code, address);
34028080329SPaul Mundt 			return 1;
34128080329SPaul Mundt 		}
34228080329SPaul Mundt 
343c2d23f91SDavid Rientjes 		/*
344c2d23f91SDavid Rientjes 		 * We ran out of memory, call the OOM killer, and return the
345c2d23f91SDavid Rientjes 		 * userspace (which will retry the fault, or kill us if we got
346c2d23f91SDavid Rientjes 		 * oom-killed):
347c2d23f91SDavid Rientjes 		 */
348c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
34928080329SPaul Mundt 	} else {
35028080329SPaul Mundt 		if (fault & VM_FAULT_SIGBUS)
35128080329SPaul Mundt 			do_sigbus(regs, error_code, address);
35233692f27SLinus Torvalds 		else if (fault & VM_FAULT_SIGSEGV)
35333692f27SLinus Torvalds 			bad_area(regs, error_code, address);
35428080329SPaul Mundt 		else
35528080329SPaul Mundt 			BUG();
35628080329SPaul Mundt 	}
35728080329SPaul Mundt 
35828080329SPaul Mundt 	return 1;
35928080329SPaul Mundt }
36028080329SPaul Mundt 
36128080329SPaul Mundt static inline int access_error(int error_code, struct vm_area_struct *vma)
36228080329SPaul Mundt {
36328080329SPaul Mundt 	if (error_code & FAULT_CODE_WRITE) {
36428080329SPaul Mundt 		/* write, present and write, not present: */
36528080329SPaul Mundt 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
36628080329SPaul Mundt 			return 1;
36728080329SPaul Mundt 		return 0;
36828080329SPaul Mundt 	}
36928080329SPaul Mundt 
37028080329SPaul Mundt 	/* ITLB miss on NX page */
37128080329SPaul Mundt 	if (unlikely((error_code & FAULT_CODE_ITLB) &&
37228080329SPaul Mundt 		     !(vma->vm_flags & VM_EXEC)))
37328080329SPaul Mundt 		return 1;
37428080329SPaul Mundt 
37528080329SPaul Mundt 	/* read, not present: */
3763122e80eSAnshuman Khandual 	if (unlikely(!vma_is_accessible(vma)))
37728080329SPaul Mundt 		return 1;
37828080329SPaul Mundt 
37928080329SPaul Mundt 	return 0;
38028080329SPaul Mundt }
38128080329SPaul Mundt 
38228080329SPaul Mundt static int fault_in_kernel_space(unsigned long address)
38328080329SPaul Mundt {
38428080329SPaul Mundt 	return address >= TASK_SIZE;
38528080329SPaul Mundt }
38628080329SPaul Mundt 
38728080329SPaul Mundt /*
38828080329SPaul Mundt  * This routine handles page faults.  It determines the address,
38928080329SPaul Mundt  * and the problem, and then passes it off to one of the appropriate
39028080329SPaul Mundt  * routines.
39128080329SPaul Mundt  */
39228080329SPaul Mundt asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
39328080329SPaul Mundt 					unsigned long error_code,
39428080329SPaul Mundt 					unsigned long address)
39528080329SPaul Mundt {
39628080329SPaul Mundt 	unsigned long vec;
39728080329SPaul Mundt 	struct task_struct *tsk;
39828080329SPaul Mundt 	struct mm_struct *mm;
39928080329SPaul Mundt 	struct vm_area_struct * vma;
40050a7ca3cSSouptick Joarder 	vm_fault_t fault;
401dde16072SPeter Xu 	unsigned int flags = FAULT_FLAG_DEFAULT;
40228080329SPaul Mundt 
40328080329SPaul Mundt 	tsk = current;
40428080329SPaul Mundt 	mm = tsk->mm;
40528080329SPaul Mundt 	vec = lookup_exception_vector();
40628080329SPaul Mundt 
40728080329SPaul Mundt 	/*
40828080329SPaul Mundt 	 * We fault-in kernel-space virtual memory on-demand. The
40928080329SPaul Mundt 	 * 'reference' page table is init_mm.pgd.
41028080329SPaul Mundt 	 *
41128080329SPaul Mundt 	 * NOTE! We MUST NOT take any locks for this case. We may
41228080329SPaul Mundt 	 * be in an interrupt or a critical region, and should
41328080329SPaul Mundt 	 * only copy the information from the master page table,
41428080329SPaul Mundt 	 * nothing more.
41528080329SPaul Mundt 	 */
41628080329SPaul Mundt 	if (unlikely(fault_in_kernel_space(address))) {
41728080329SPaul Mundt 		if (vmalloc_fault(address) >= 0)
41828080329SPaul Mundt 			return;
419b98cca44SAnshuman Khandual 		if (kprobe_page_fault(regs, vec))
42028080329SPaul Mundt 			return;
42128080329SPaul Mundt 
42228080329SPaul Mundt 		bad_area_nosemaphore(regs, error_code, address);
42328080329SPaul Mundt 		return;
42428080329SPaul Mundt 	}
42528080329SPaul Mundt 
426b98cca44SAnshuman Khandual 	if (unlikely(kprobe_page_fault(regs, vec)))
42728080329SPaul Mundt 		return;
42828080329SPaul Mundt 
42928080329SPaul Mundt 	/* Only enable interrupts if they were on before the fault */
43028080329SPaul Mundt 	if ((regs->sr & SR_IMASK) != SR_IMASK)
43128080329SPaul Mundt 		local_irq_enable();
43228080329SPaul Mundt 
43328080329SPaul Mundt 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
43428080329SPaul Mundt 
43528080329SPaul Mundt 	/*
43628080329SPaul Mundt 	 * If we're in an interrupt, have no user context or are running
43770ffdb93SDavid Hildenbrand 	 * with pagefaults disabled then we must not take the fault:
43828080329SPaul Mundt 	 */
43970ffdb93SDavid Hildenbrand 	if (unlikely(faulthandler_disabled() || !mm)) {
44028080329SPaul Mundt 		bad_area_nosemaphore(regs, error_code, address);
44128080329SPaul Mundt 		return;
44228080329SPaul Mundt 	}
44328080329SPaul Mundt 
44428080329SPaul Mundt retry:
445d8ed45c5SMichel Lespinasse 	mmap_read_lock(mm);
44628080329SPaul Mundt 
44728080329SPaul Mundt 	vma = find_vma(mm, address);
44828080329SPaul Mundt 	if (unlikely(!vma)) {
44928080329SPaul Mundt 		bad_area(regs, error_code, address);
45028080329SPaul Mundt 		return;
45128080329SPaul Mundt 	}
45228080329SPaul Mundt 	if (likely(vma->vm_start <= address))
45328080329SPaul Mundt 		goto good_area;
45428080329SPaul Mundt 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
45528080329SPaul Mundt 		bad_area(regs, error_code, address);
45628080329SPaul Mundt 		return;
45728080329SPaul Mundt 	}
45828080329SPaul Mundt 	if (unlikely(expand_stack(vma, address))) {
45928080329SPaul Mundt 		bad_area(regs, error_code, address);
46028080329SPaul Mundt 		return;
46128080329SPaul Mundt 	}
46228080329SPaul Mundt 
46328080329SPaul Mundt 	/*
46428080329SPaul Mundt 	 * Ok, we have a good vm_area for this memory access, so
46528080329SPaul Mundt 	 * we can handle it..
46628080329SPaul Mundt 	 */
46728080329SPaul Mundt good_area:
46828080329SPaul Mundt 	if (unlikely(access_error(error_code, vma))) {
46928080329SPaul Mundt 		bad_area_access_error(regs, error_code, address);
47028080329SPaul Mundt 		return;
47128080329SPaul Mundt 	}
47228080329SPaul Mundt 
47328080329SPaul Mundt 	set_thread_fault_code(error_code);
47428080329SPaul Mundt 
475759496baSJohannes Weiner 	if (user_mode(regs))
476759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
477759496baSJohannes Weiner 	if (error_code & FAULT_CODE_WRITE)
478759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
479759496baSJohannes Weiner 
48028080329SPaul Mundt 	/*
48128080329SPaul Mundt 	 * If for any reason at all we couldn't handle the fault,
48228080329SPaul Mundt 	 * make sure we exit gracefully rather than endlessly redo
48328080329SPaul Mundt 	 * the fault.
48428080329SPaul Mundt 	 */
485dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
48628080329SPaul Mundt 
48728080329SPaul Mundt 	if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
48828080329SPaul Mundt 		if (mm_fault_error(regs, error_code, address, fault))
48928080329SPaul Mundt 			return;
49028080329SPaul Mundt 
49128080329SPaul Mundt 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
49228080329SPaul Mundt 		if (fault & VM_FAULT_MAJOR) {
49328080329SPaul Mundt 			tsk->maj_flt++;
49428080329SPaul Mundt 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
49528080329SPaul Mundt 				      regs, address);
49628080329SPaul Mundt 		} else {
49728080329SPaul Mundt 			tsk->min_flt++;
49828080329SPaul Mundt 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
49928080329SPaul Mundt 				      regs, address);
50028080329SPaul Mundt 		}
50128080329SPaul Mundt 		if (fault & VM_FAULT_RETRY) {
50245cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
50328080329SPaul Mundt 
50428080329SPaul Mundt 			/*
5053e4e28c5SMichel Lespinasse 			 * No need to mmap_read_unlock(mm) as we would
50628080329SPaul Mundt 			 * have already released it in __lock_page_or_retry
50728080329SPaul Mundt 			 * in mm/filemap.c.
50828080329SPaul Mundt 			 */
50928080329SPaul Mundt 			goto retry;
51028080329SPaul Mundt 		}
51128080329SPaul Mundt 	}
51228080329SPaul Mundt 
513d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
51428080329SPaul Mundt }
515