xref: /openbmc/linux/arch/sparc/mm/fault_64.c (revision 759496ba6407c6994d6a5ce3a5e74937d7816208)
127137e52SSam Ravnborg /*
227137e52SSam Ravnborg  * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
327137e52SSam Ravnborg  *
427137e52SSam Ravnborg  * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
527137e52SSam Ravnborg  * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
627137e52SSam Ravnborg  */
727137e52SSam Ravnborg 
827137e52SSam Ravnborg #include <asm/head.h>
927137e52SSam Ravnborg 
1027137e52SSam Ravnborg #include <linux/string.h>
1127137e52SSam Ravnborg #include <linux/types.h>
1227137e52SSam Ravnborg #include <linux/sched.h>
1327137e52SSam Ravnborg #include <linux/ptrace.h>
1427137e52SSam Ravnborg #include <linux/mman.h>
1527137e52SSam Ravnborg #include <linux/signal.h>
1627137e52SSam Ravnborg #include <linux/mm.h>
1727137e52SSam Ravnborg #include <linux/module.h>
1827137e52SSam Ravnborg #include <linux/init.h>
19a084b667SDavid S. Miller #include <linux/perf_event.h>
2027137e52SSam Ravnborg #include <linux/interrupt.h>
2127137e52SSam Ravnborg #include <linux/kprobes.h>
2227137e52SSam Ravnborg #include <linux/kdebug.h>
23eeabac73SDavid S. Miller #include <linux/percpu.h>
2427137e52SSam Ravnborg 
2527137e52SSam Ravnborg #include <asm/page.h>
2627137e52SSam Ravnborg #include <asm/pgtable.h>
2727137e52SSam Ravnborg #include <asm/openprom.h>
2827137e52SSam Ravnborg #include <asm/oplib.h>
2927137e52SSam Ravnborg #include <asm/uaccess.h>
3027137e52SSam Ravnborg #include <asm/asi.h>
3127137e52SSam Ravnborg #include <asm/lsu.h>
3227137e52SSam Ravnborg #include <asm/sections.h>
3327137e52SSam Ravnborg #include <asm/mmu_context.h>
3427137e52SSam Ravnborg 
354b177647SDavid S. Miller int show_unhandled_signals = 1;
364b177647SDavid S. Miller 
374ed5d5e4SDavid S. Miller static inline __kprobes int notify_page_fault(struct pt_regs *regs)
3827137e52SSam Ravnborg {
3927137e52SSam Ravnborg 	int ret = 0;
4027137e52SSam Ravnborg 
4127137e52SSam Ravnborg 	/* kprobe_running() needs smp_processor_id() */
42135d0821SDavid S. Miller 	if (kprobes_built_in() && !user_mode(regs)) {
4327137e52SSam Ravnborg 		preempt_disable();
4427137e52SSam Ravnborg 		if (kprobe_running() && kprobe_fault_handler(regs, 0))
4527137e52SSam Ravnborg 			ret = 1;
4627137e52SSam Ravnborg 		preempt_enable();
4727137e52SSam Ravnborg 	}
4827137e52SSam Ravnborg 	return ret;
4927137e52SSam Ravnborg }
5027137e52SSam Ravnborg 
5127137e52SSam Ravnborg static void __kprobes unhandled_fault(unsigned long address,
5227137e52SSam Ravnborg 				      struct task_struct *tsk,
5327137e52SSam Ravnborg 				      struct pt_regs *regs)
5427137e52SSam Ravnborg {
5527137e52SSam Ravnborg 	if ((unsigned long) address < PAGE_SIZE) {
5627137e52SSam Ravnborg 		printk(KERN_ALERT "Unable to handle kernel NULL "
5727137e52SSam Ravnborg 		       "pointer dereference\n");
5827137e52SSam Ravnborg 	} else {
5927137e52SSam Ravnborg 		printk(KERN_ALERT "Unable to handle kernel paging request "
6027137e52SSam Ravnborg 		       "at virtual address %016lx\n", (unsigned long)address);
6127137e52SSam Ravnborg 	}
6227137e52SSam Ravnborg 	printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
6327137e52SSam Ravnborg 	       (tsk->mm ?
6427137e52SSam Ravnborg 		CTX_HWBITS(tsk->mm->context) :
6527137e52SSam Ravnborg 		CTX_HWBITS(tsk->active_mm->context)));
6627137e52SSam Ravnborg 	printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
6727137e52SSam Ravnborg 	       (tsk->mm ? (unsigned long) tsk->mm->pgd :
6827137e52SSam Ravnborg 		          (unsigned long) tsk->active_mm->pgd));
6927137e52SSam Ravnborg 	die_if_kernel("Oops", regs);
7027137e52SSam Ravnborg }
7127137e52SSam Ravnborg 
724ed5d5e4SDavid S. Miller static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
7327137e52SSam Ravnborg {
7427137e52SSam Ravnborg 	printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
7527137e52SSam Ravnborg 	       regs->tpc);
7627137e52SSam Ravnborg 	printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
7727137e52SSam Ravnborg 	printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
7827137e52SSam Ravnborg 	printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
7927137e52SSam Ravnborg 	dump_stack();
8027137e52SSam Ravnborg 	unhandled_fault(regs->tpc, current, regs);
8127137e52SSam Ravnborg }
8227137e52SSam Ravnborg 
8327137e52SSam Ravnborg /*
8427137e52SSam Ravnborg  * We now make sure that mmap_sem is held in all paths that call
8527137e52SSam Ravnborg  * this. Additionally, to prevent kswapd from ripping ptes from
8627137e52SSam Ravnborg  * under us, raise interrupts around the time that we look at the
8727137e52SSam Ravnborg  * pte, kswapd will have to wait to get his smp ipi response from
8827137e52SSam Ravnborg  * us. vmtruncate likewise. This saves us having to get pte lock.
8927137e52SSam Ravnborg  */
9027137e52SSam Ravnborg static unsigned int get_user_insn(unsigned long tpc)
9127137e52SSam Ravnborg {
9227137e52SSam Ravnborg 	pgd_t *pgdp = pgd_offset(current->mm, tpc);
9327137e52SSam Ravnborg 	pud_t *pudp;
9427137e52SSam Ravnborg 	pmd_t *pmdp;
9527137e52SSam Ravnborg 	pte_t *ptep, pte;
9627137e52SSam Ravnborg 	unsigned long pa;
9727137e52SSam Ravnborg 	u32 insn = 0;
9827137e52SSam Ravnborg 	unsigned long pstate;
9927137e52SSam Ravnborg 
10027137e52SSam Ravnborg 	if (pgd_none(*pgdp))
10127137e52SSam Ravnborg 		goto outret;
10227137e52SSam Ravnborg 	pudp = pud_offset(pgdp, tpc);
10327137e52SSam Ravnborg 	if (pud_none(*pudp))
10427137e52SSam Ravnborg 		goto outret;
10527137e52SSam Ravnborg 	pmdp = pmd_offset(pudp, tpc);
10627137e52SSam Ravnborg 	if (pmd_none(*pmdp))
10727137e52SSam Ravnborg 		goto outret;
10827137e52SSam Ravnborg 
10927137e52SSam Ravnborg 	/* This disables preemption for us as well. */
11027137e52SSam Ravnborg 	__asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
11127137e52SSam Ravnborg 	__asm__ __volatile__("wrpr %0, %1, %%pstate"
11227137e52SSam Ravnborg 				: : "r" (pstate), "i" (PSTATE_IE));
11327137e52SSam Ravnborg 	ptep = pte_offset_map(pmdp, tpc);
11427137e52SSam Ravnborg 	pte = *ptep;
11527137e52SSam Ravnborg 	if (!pte_present(pte))
11627137e52SSam Ravnborg 		goto out;
11727137e52SSam Ravnborg 
11827137e52SSam Ravnborg 	pa  = (pte_pfn(pte) << PAGE_SHIFT);
11927137e52SSam Ravnborg 	pa += (tpc & ~PAGE_MASK);
12027137e52SSam Ravnborg 
12127137e52SSam Ravnborg 	/* Use phys bypass so we don't pollute dtlb/dcache. */
12227137e52SSam Ravnborg 	__asm__ __volatile__("lduwa [%1] %2, %0"
12327137e52SSam Ravnborg 			     : "=r" (insn)
12427137e52SSam Ravnborg 			     : "r" (pa), "i" (ASI_PHYS_USE_EC));
12527137e52SSam Ravnborg 
12627137e52SSam Ravnborg out:
12727137e52SSam Ravnborg 	pte_unmap(ptep);
12827137e52SSam Ravnborg 	__asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
12927137e52SSam Ravnborg outret:
13027137e52SSam Ravnborg 	return insn;
13127137e52SSam Ravnborg }
13227137e52SSam Ravnborg 
1334b177647SDavid S. Miller static inline void
1344b177647SDavid S. Miller show_signal_msg(struct pt_regs *regs, int sig, int code,
1354b177647SDavid S. Miller 		unsigned long address, struct task_struct *tsk)
1364b177647SDavid S. Miller {
1374b177647SDavid S. Miller 	if (!unhandled_signal(tsk, sig))
1384b177647SDavid S. Miller 		return;
1394b177647SDavid S. Miller 
1404b177647SDavid S. Miller 	if (!printk_ratelimit())
1414b177647SDavid S. Miller 		return;
1424b177647SDavid S. Miller 
1434b177647SDavid S. Miller 	printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
1444b177647SDavid S. Miller 	       task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
1454b177647SDavid S. Miller 	       tsk->comm, task_pid_nr(tsk), address,
1464b177647SDavid S. Miller 	       (void *)regs->tpc, (void *)regs->u_regs[UREG_I7],
1474b177647SDavid S. Miller 	       (void *)regs->u_regs[UREG_FP], code);
1484b177647SDavid S. Miller 
1494b177647SDavid S. Miller 	print_vma_addr(KERN_CONT " in ", regs->tpc);
1504b177647SDavid S. Miller 
1514b177647SDavid S. Miller 	printk(KERN_CONT "\n");
1524b177647SDavid S. Miller }
1534b177647SDavid S. Miller 
15427137e52SSam Ravnborg static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
15527137e52SSam Ravnborg 			     unsigned int insn, int fault_code)
15627137e52SSam Ravnborg {
1574b177647SDavid S. Miller 	unsigned long addr;
15827137e52SSam Ravnborg 	siginfo_t info;
15927137e52SSam Ravnborg 
16027137e52SSam Ravnborg 	info.si_code = code;
16127137e52SSam Ravnborg 	info.si_signo = sig;
16227137e52SSam Ravnborg 	info.si_errno = 0;
16327137e52SSam Ravnborg 	if (fault_code & FAULT_CODE_ITLB)
1644b177647SDavid S. Miller 		addr = regs->tpc;
16527137e52SSam Ravnborg 	else
1664b177647SDavid S. Miller 		addr = compute_effective_address(regs, insn, 0);
1674b177647SDavid S. Miller 	info.si_addr = (void __user *) addr;
16827137e52SSam Ravnborg 	info.si_trapno = 0;
1694b177647SDavid S. Miller 
1704b177647SDavid S. Miller 	if (unlikely(show_unhandled_signals))
1714b177647SDavid S. Miller 		show_signal_msg(regs, sig, code, addr, current);
1724b177647SDavid S. Miller 
17327137e52SSam Ravnborg 	force_sig_info(sig, &info, current);
17427137e52SSam Ravnborg }
17527137e52SSam Ravnborg 
17627137e52SSam Ravnborg extern int handle_ldf_stq(u32, struct pt_regs *);
17727137e52SSam Ravnborg extern int handle_ld_nf(u32, struct pt_regs *);
17827137e52SSam Ravnborg 
17927137e52SSam Ravnborg static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
18027137e52SSam Ravnborg {
18127137e52SSam Ravnborg 	if (!insn) {
18227137e52SSam Ravnborg 		if (!regs->tpc || (regs->tpc & 0x3))
18327137e52SSam Ravnborg 			return 0;
18427137e52SSam Ravnborg 		if (regs->tstate & TSTATE_PRIV) {
18527137e52SSam Ravnborg 			insn = *(unsigned int *) regs->tpc;
18627137e52SSam Ravnborg 		} else {
18727137e52SSam Ravnborg 			insn = get_user_insn(regs->tpc);
18827137e52SSam Ravnborg 		}
18927137e52SSam Ravnborg 	}
19027137e52SSam Ravnborg 	return insn;
19127137e52SSam Ravnborg }
19227137e52SSam Ravnborg 
1934ed5d5e4SDavid S. Miller static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
1944ed5d5e4SDavid S. Miller 				      int fault_code, unsigned int insn,
1954ed5d5e4SDavid S. Miller 				      unsigned long address)
19627137e52SSam Ravnborg {
19727137e52SSam Ravnborg 	unsigned char asi = ASI_P;
19827137e52SSam Ravnborg 
19927137e52SSam Ravnborg 	if ((!insn) && (regs->tstate & TSTATE_PRIV))
20027137e52SSam Ravnborg 		goto cannot_handle;
20127137e52SSam Ravnborg 
20227137e52SSam Ravnborg 	/* If user insn could be read (thus insn is zero), that
20327137e52SSam Ravnborg 	 * is fine.  We will just gun down the process with a signal
20427137e52SSam Ravnborg 	 * in that case.
20527137e52SSam Ravnborg 	 */
20627137e52SSam Ravnborg 
20727137e52SSam Ravnborg 	if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
20827137e52SSam Ravnborg 	    (insn & 0xc0800000) == 0xc0800000) {
20927137e52SSam Ravnborg 		if (insn & 0x2000)
21027137e52SSam Ravnborg 			asi = (regs->tstate >> 24);
21127137e52SSam Ravnborg 		else
21227137e52SSam Ravnborg 			asi = (insn >> 5);
21327137e52SSam Ravnborg 		if ((asi & 0xf2) == 0x82) {
21427137e52SSam Ravnborg 			if (insn & 0x1000000) {
21527137e52SSam Ravnborg 				handle_ldf_stq(insn, regs);
21627137e52SSam Ravnborg 			} else {
21727137e52SSam Ravnborg 				/* This was a non-faulting load. Just clear the
21827137e52SSam Ravnborg 				 * destination register(s) and continue with the next
21927137e52SSam Ravnborg 				 * instruction. -jj
22027137e52SSam Ravnborg 				 */
22127137e52SSam Ravnborg 				handle_ld_nf(insn, regs);
22227137e52SSam Ravnborg 			}
22327137e52SSam Ravnborg 			return;
22427137e52SSam Ravnborg 		}
22527137e52SSam Ravnborg 	}
22627137e52SSam Ravnborg 
22727137e52SSam Ravnborg 	/* Is this in ex_table? */
22827137e52SSam Ravnborg 	if (regs->tstate & TSTATE_PRIV) {
22927137e52SSam Ravnborg 		const struct exception_table_entry *entry;
23027137e52SSam Ravnborg 
23127137e52SSam Ravnborg 		entry = search_exception_tables(regs->tpc);
23227137e52SSam Ravnborg 		if (entry) {
23327137e52SSam Ravnborg 			regs->tpc = entry->fixup;
23427137e52SSam Ravnborg 			regs->tnpc = regs->tpc + 4;
23527137e52SSam Ravnborg 			return;
23627137e52SSam Ravnborg 		}
23727137e52SSam Ravnborg 	} else {
23827137e52SSam Ravnborg 		/* The si_code was set to make clear whether
23927137e52SSam Ravnborg 		 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
24027137e52SSam Ravnborg 		 */
24127137e52SSam Ravnborg 		do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
24227137e52SSam Ravnborg 		return;
24327137e52SSam Ravnborg 	}
24427137e52SSam Ravnborg 
24527137e52SSam Ravnborg cannot_handle:
24627137e52SSam Ravnborg 	unhandled_fault (address, current, regs);
24727137e52SSam Ravnborg }
24827137e52SSam Ravnborg 
2494ed5d5e4SDavid S. Miller static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
2509b026058SDavid S. Miller {
2519b026058SDavid S. Miller 	static int times;
2529b026058SDavid S. Miller 
2539b026058SDavid S. Miller 	if (times++ < 10)
2549b026058SDavid S. Miller 		printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
2559b026058SDavid S. Miller 		       "64-bit TPC [%lx]\n",
2569b026058SDavid S. Miller 		       current->comm, current->pid,
2579b026058SDavid S. Miller 		       regs->tpc);
2589b026058SDavid S. Miller 	show_regs(regs);
2599b026058SDavid S. Miller }
2609b026058SDavid S. Miller 
2614ed5d5e4SDavid S. Miller static void noinline __kprobes bogus_32bit_fault_address(struct pt_regs *regs,
2629b026058SDavid S. Miller 							 unsigned long addr)
2639b026058SDavid S. Miller {
2649b026058SDavid S. Miller 	static int times;
2659b026058SDavid S. Miller 
2669b026058SDavid S. Miller 	if (times++ < 10)
2679b026058SDavid S. Miller 		printk(KERN_ERR "FAULT[%s:%d]: 32-bit process "
2689b026058SDavid S. Miller 		       "reports 64-bit fault address [%lx]\n",
2699b026058SDavid S. Miller 		       current->comm, current->pid, addr);
2709b026058SDavid S. Miller 	show_regs(regs);
2719b026058SDavid S. Miller }
2729b026058SDavid S. Miller 
27327137e52SSam Ravnborg asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
27427137e52SSam Ravnborg {
27527137e52SSam Ravnborg 	struct mm_struct *mm = current->mm;
27627137e52SSam Ravnborg 	struct vm_area_struct *vma;
27727137e52SSam Ravnborg 	unsigned int insn = 0;
27827137e52SSam Ravnborg 	int si_code, fault_code, fault;
27927137e52SSam Ravnborg 	unsigned long address, mm_rss;
2807358e510SKautuk Consul 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
28127137e52SSam Ravnborg 
28227137e52SSam Ravnborg 	fault_code = get_thread_fault_code();
28327137e52SSam Ravnborg 
28427137e52SSam Ravnborg 	if (notify_page_fault(regs))
28527137e52SSam Ravnborg 		return;
28627137e52SSam Ravnborg 
28727137e52SSam Ravnborg 	si_code = SEGV_MAPERR;
28827137e52SSam Ravnborg 	address = current_thread_info()->fault_address;
28927137e52SSam Ravnborg 
29027137e52SSam Ravnborg 	if ((fault_code & FAULT_CODE_ITLB) &&
29127137e52SSam Ravnborg 	    (fault_code & FAULT_CODE_DTLB))
29227137e52SSam Ravnborg 		BUG();
29327137e52SSam Ravnborg 
294eeabac73SDavid S. Miller 	if (test_thread_flag(TIF_32BIT)) {
2959b026058SDavid S. Miller 		if (!(regs->tstate & TSTATE_PRIV)) {
2969b026058SDavid S. Miller 			if (unlikely((regs->tpc >> 32) != 0)) {
2979b026058SDavid S. Miller 				bogus_32bit_fault_tpc(regs);
2989b026058SDavid S. Miller 				goto intr_or_no_mm;
2999b026058SDavid S. Miller 			}
3009b026058SDavid S. Miller 		}
3019b026058SDavid S. Miller 		if (unlikely((address >> 32) != 0)) {
3029b026058SDavid S. Miller 			bogus_32bit_fault_address(regs, address);
3039b026058SDavid S. Miller 			goto intr_or_no_mm;
3049b026058SDavid S. Miller 		}
305eeabac73SDavid S. Miller 	}
306eeabac73SDavid S. Miller 
30727137e52SSam Ravnborg 	if (regs->tstate & TSTATE_PRIV) {
3089b026058SDavid S. Miller 		unsigned long tpc = regs->tpc;
30927137e52SSam Ravnborg 
31027137e52SSam Ravnborg 		/* Sanity check the PC. */
31127137e52SSam Ravnborg 		if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
31227137e52SSam Ravnborg 		    (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
31327137e52SSam Ravnborg 			/* Valid, no problems... */
31427137e52SSam Ravnborg 		} else {
31527137e52SSam Ravnborg 			bad_kernel_pc(regs, address);
31627137e52SSam Ravnborg 			return;
31727137e52SSam Ravnborg 		}
318*759496baSJohannes Weiner 	} else
319*759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
32027137e52SSam Ravnborg 
32127137e52SSam Ravnborg 	/*
32227137e52SSam Ravnborg 	 * If we're in an interrupt or have no user
32327137e52SSam Ravnborg 	 * context, we must not take the fault..
32427137e52SSam Ravnborg 	 */
32527137e52SSam Ravnborg 	if (in_atomic() || !mm)
32627137e52SSam Ravnborg 		goto intr_or_no_mm;
32727137e52SSam Ravnborg 
328a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
329a084b667SDavid S. Miller 
33027137e52SSam Ravnborg 	if (!down_read_trylock(&mm->mmap_sem)) {
33127137e52SSam Ravnborg 		if ((regs->tstate & TSTATE_PRIV) &&
33227137e52SSam Ravnborg 		    !search_exception_tables(regs->tpc)) {
33327137e52SSam Ravnborg 			insn = get_fault_insn(regs, insn);
33427137e52SSam Ravnborg 			goto handle_kernel_fault;
33527137e52SSam Ravnborg 		}
3367358e510SKautuk Consul 
3377358e510SKautuk Consul retry:
33827137e52SSam Ravnborg 		down_read(&mm->mmap_sem);
33927137e52SSam Ravnborg 	}
34027137e52SSam Ravnborg 
34127137e52SSam Ravnborg 	vma = find_vma(mm, address);
34227137e52SSam Ravnborg 	if (!vma)
34327137e52SSam Ravnborg 		goto bad_area;
34427137e52SSam Ravnborg 
34527137e52SSam Ravnborg 	/* Pure DTLB misses do not tell us whether the fault causing
34627137e52SSam Ravnborg 	 * load/store/atomic was a write or not, it only says that there
34727137e52SSam Ravnborg 	 * was no match.  So in such a case we (carefully) read the
34827137e52SSam Ravnborg 	 * instruction to try and figure this out.  It's an optimization
34927137e52SSam Ravnborg 	 * so it's ok if we can't do this.
35027137e52SSam Ravnborg 	 *
35127137e52SSam Ravnborg 	 * Special hack, window spill/fill knows the exact fault type.
35227137e52SSam Ravnborg 	 */
35327137e52SSam Ravnborg 	if (((fault_code &
35427137e52SSam Ravnborg 	      (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
35527137e52SSam Ravnborg 	    (vma->vm_flags & VM_WRITE) != 0) {
35627137e52SSam Ravnborg 		insn = get_fault_insn(regs, 0);
35727137e52SSam Ravnborg 		if (!insn)
35827137e52SSam Ravnborg 			goto continue_fault;
35927137e52SSam Ravnborg 		/* All loads, stores and atomics have bits 30 and 31 both set
36027137e52SSam Ravnborg 		 * in the instruction.  Bit 21 is set in all stores, but we
36127137e52SSam Ravnborg 		 * have to avoid prefetches which also have bit 21 set.
36227137e52SSam Ravnborg 		 */
36327137e52SSam Ravnborg 		if ((insn & 0xc0200000) == 0xc0200000 &&
36427137e52SSam Ravnborg 		    (insn & 0x01780000) != 0x01680000) {
36527137e52SSam Ravnborg 			/* Don't bother updating thread struct value,
36627137e52SSam Ravnborg 			 * because update_mmu_cache only cares which tlb
36727137e52SSam Ravnborg 			 * the access came from.
36827137e52SSam Ravnborg 			 */
36927137e52SSam Ravnborg 			fault_code |= FAULT_CODE_WRITE;
37027137e52SSam Ravnborg 		}
37127137e52SSam Ravnborg 	}
37227137e52SSam Ravnborg continue_fault:
37327137e52SSam Ravnborg 
37427137e52SSam Ravnborg 	if (vma->vm_start <= address)
37527137e52SSam Ravnborg 		goto good_area;
37627137e52SSam Ravnborg 	if (!(vma->vm_flags & VM_GROWSDOWN))
37727137e52SSam Ravnborg 		goto bad_area;
37827137e52SSam Ravnborg 	if (!(fault_code & FAULT_CODE_WRITE)) {
37927137e52SSam Ravnborg 		/* Non-faulting loads shouldn't expand stack. */
38027137e52SSam Ravnborg 		insn = get_fault_insn(regs, insn);
38127137e52SSam Ravnborg 		if ((insn & 0xc0800000) == 0xc0800000) {
38227137e52SSam Ravnborg 			unsigned char asi;
38327137e52SSam Ravnborg 
38427137e52SSam Ravnborg 			if (insn & 0x2000)
38527137e52SSam Ravnborg 				asi = (regs->tstate >> 24);
38627137e52SSam Ravnborg 			else
38727137e52SSam Ravnborg 				asi = (insn >> 5);
38827137e52SSam Ravnborg 			if ((asi & 0xf2) == 0x82)
38927137e52SSam Ravnborg 				goto bad_area;
39027137e52SSam Ravnborg 		}
39127137e52SSam Ravnborg 	}
39227137e52SSam Ravnborg 	if (expand_stack(vma, address))
39327137e52SSam Ravnborg 		goto bad_area;
39427137e52SSam Ravnborg 	/*
39527137e52SSam Ravnborg 	 * Ok, we have a good vm_area for this memory access, so
39627137e52SSam Ravnborg 	 * we can handle it..
39727137e52SSam Ravnborg 	 */
39827137e52SSam Ravnborg good_area:
39927137e52SSam Ravnborg 	si_code = SEGV_ACCERR;
40027137e52SSam Ravnborg 
40127137e52SSam Ravnborg 	/* If we took a ITLB miss on a non-executable page, catch
40227137e52SSam Ravnborg 	 * that here.
40327137e52SSam Ravnborg 	 */
40427137e52SSam Ravnborg 	if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
40527137e52SSam Ravnborg 		BUG_ON(address != regs->tpc);
40627137e52SSam Ravnborg 		BUG_ON(regs->tstate & TSTATE_PRIV);
40727137e52SSam Ravnborg 		goto bad_area;
40827137e52SSam Ravnborg 	}
40927137e52SSam Ravnborg 
41027137e52SSam Ravnborg 	if (fault_code & FAULT_CODE_WRITE) {
41127137e52SSam Ravnborg 		if (!(vma->vm_flags & VM_WRITE))
41227137e52SSam Ravnborg 			goto bad_area;
41327137e52SSam Ravnborg 
41427137e52SSam Ravnborg 		/* Spitfire has an icache which does not snoop
41527137e52SSam Ravnborg 		 * processor stores.  Later processors do...
41627137e52SSam Ravnborg 		 */
41727137e52SSam Ravnborg 		if (tlb_type == spitfire &&
41827137e52SSam Ravnborg 		    (vma->vm_flags & VM_EXEC) != 0 &&
41927137e52SSam Ravnborg 		    vma->vm_file != NULL)
42027137e52SSam Ravnborg 			set_thread_fault_code(fault_code |
42127137e52SSam Ravnborg 					      FAULT_CODE_BLKCOMMIT);
422*759496baSJohannes Weiner 
423*759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
42427137e52SSam Ravnborg 	} else {
42527137e52SSam Ravnborg 		/* Allow reads even for write-only mappings */
42627137e52SSam Ravnborg 		if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
42727137e52SSam Ravnborg 			goto bad_area;
42827137e52SSam Ravnborg 	}
42927137e52SSam Ravnborg 
4307358e510SKautuk Consul 	fault = handle_mm_fault(mm, vma, address, flags);
4317358e510SKautuk Consul 
4327358e510SKautuk Consul 	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
4337358e510SKautuk Consul 		return;
4347358e510SKautuk Consul 
43527137e52SSam Ravnborg 	if (unlikely(fault & VM_FAULT_ERROR)) {
43627137e52SSam Ravnborg 		if (fault & VM_FAULT_OOM)
43727137e52SSam Ravnborg 			goto out_of_memory;
43827137e52SSam Ravnborg 		else if (fault & VM_FAULT_SIGBUS)
43927137e52SSam Ravnborg 			goto do_sigbus;
44027137e52SSam Ravnborg 		BUG();
44127137e52SSam Ravnborg 	}
4427358e510SKautuk Consul 
4437358e510SKautuk Consul 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
444a084b667SDavid S. Miller 		if (fault & VM_FAULT_MAJOR) {
44527137e52SSam Ravnborg 			current->maj_flt++;
4467358e510SKautuk Consul 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
4477358e510SKautuk Consul 				      1, regs, address);
448a084b667SDavid S. Miller 		} else {
44927137e52SSam Ravnborg 			current->min_flt++;
4507358e510SKautuk Consul 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
4517358e510SKautuk Consul 				      1, regs, address);
4527358e510SKautuk Consul 		}
4537358e510SKautuk Consul 		if (fault & VM_FAULT_RETRY) {
4547358e510SKautuk Consul 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
45545cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
4567358e510SKautuk Consul 
4577358e510SKautuk Consul 			/* No need to up_read(&mm->mmap_sem) as we would
4587358e510SKautuk Consul 			 * have already released it in __lock_page_or_retry
4597358e510SKautuk Consul 			 * in mm/filemap.c.
4607358e510SKautuk Consul 			 */
4617358e510SKautuk Consul 
4627358e510SKautuk Consul 			goto retry;
4637358e510SKautuk Consul 		}
464a084b667SDavid S. Miller 	}
46527137e52SSam Ravnborg 	up_read(&mm->mmap_sem);
46627137e52SSam Ravnborg 
46727137e52SSam Ravnborg 	mm_rss = get_mm_rss(mm);
4689e695d2eSDavid Miller #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
46927137e52SSam Ravnborg 	mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
47027137e52SSam Ravnborg #endif
47127137e52SSam Ravnborg 	if (unlikely(mm_rss >
47227137e52SSam Ravnborg 		     mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
47327137e52SSam Ravnborg 		tsb_grow(mm, MM_TSB_BASE, mm_rss);
4749e695d2eSDavid Miller #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
47527137e52SSam Ravnborg 	mm_rss = mm->context.huge_pte_count;
47627137e52SSam Ravnborg 	if (unlikely(mm_rss >
4770fbebed6SDavid S. Miller 		     mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit)) {
4780fbebed6SDavid S. Miller 		if (mm->context.tsb_block[MM_TSB_HUGE].tsb)
47927137e52SSam Ravnborg 			tsb_grow(mm, MM_TSB_HUGE, mm_rss);
4800fbebed6SDavid S. Miller 		else
4810fbebed6SDavid S. Miller 			hugetlb_setup(regs);
4820fbebed6SDavid S. Miller 
4830fbebed6SDavid S. Miller 	}
48427137e52SSam Ravnborg #endif
48527137e52SSam Ravnborg 	return;
48627137e52SSam Ravnborg 
48727137e52SSam Ravnborg 	/*
48827137e52SSam Ravnborg 	 * Something tried to access memory that isn't in our memory map..
48927137e52SSam Ravnborg 	 * Fix it, but check if it's kernel or user first..
49027137e52SSam Ravnborg 	 */
49127137e52SSam Ravnborg bad_area:
49227137e52SSam Ravnborg 	insn = get_fault_insn(regs, insn);
49327137e52SSam Ravnborg 	up_read(&mm->mmap_sem);
49427137e52SSam Ravnborg 
49527137e52SSam Ravnborg handle_kernel_fault:
49627137e52SSam Ravnborg 	do_kernel_fault(regs, si_code, fault_code, insn, address);
49727137e52SSam Ravnborg 	return;
49827137e52SSam Ravnborg 
49927137e52SSam Ravnborg /*
50027137e52SSam Ravnborg  * We ran out of memory, or some other thing happened to us that made
50127137e52SSam Ravnborg  * us unable to handle the page fault gracefully.
50227137e52SSam Ravnborg  */
50327137e52SSam Ravnborg out_of_memory:
50427137e52SSam Ravnborg 	insn = get_fault_insn(regs, insn);
50527137e52SSam Ravnborg 	up_read(&mm->mmap_sem);
506a923c28fSDavid S. Miller 	if (!(regs->tstate & TSTATE_PRIV)) {
507a923c28fSDavid S. Miller 		pagefault_out_of_memory();
508a923c28fSDavid S. Miller 		return;
509a923c28fSDavid S. Miller 	}
51027137e52SSam Ravnborg 	goto handle_kernel_fault;
51127137e52SSam Ravnborg 
51227137e52SSam Ravnborg intr_or_no_mm:
51327137e52SSam Ravnborg 	insn = get_fault_insn(regs, 0);
51427137e52SSam Ravnborg 	goto handle_kernel_fault;
51527137e52SSam Ravnborg 
51627137e52SSam Ravnborg do_sigbus:
51727137e52SSam Ravnborg 	insn = get_fault_insn(regs, insn);
51827137e52SSam Ravnborg 	up_read(&mm->mmap_sem);
51927137e52SSam Ravnborg 
52027137e52SSam Ravnborg 	/*
52127137e52SSam Ravnborg 	 * Send a sigbus, regardless of whether we were in kernel
52227137e52SSam Ravnborg 	 * or user mode.
52327137e52SSam Ravnborg 	 */
52427137e52SSam Ravnborg 	do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
52527137e52SSam Ravnborg 
52627137e52SSam Ravnborg 	/* Kernel mode? Handle exceptions or die */
52727137e52SSam Ravnborg 	if (regs->tstate & TSTATE_PRIV)
52827137e52SSam Ravnborg 		goto handle_kernel_fault;
52927137e52SSam Ravnborg }
530