1/*
2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
3 *
4 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
6 */
7
8#include <asm/head.h>
9
10#include <linux/string.h>
11#include <linux/types.h>
12#include <linux/sched.h>
13#include <linux/ptrace.h>
14#include <linux/mman.h>
15#include <linux/signal.h>
16#include <linux/mm.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/perf_event.h>
20#include <linux/interrupt.h>
21#include <linux/kprobes.h>
22#include <linux/kdebug.h>
23#include <linux/percpu.h>
24#include <linux/context_tracking.h>
25
26#include <asm/page.h>
27#include <asm/pgtable.h>
28#include <asm/openprom.h>
29#include <asm/oplib.h>
30#include <asm/uaccess.h>
31#include <asm/asi.h>
32#include <asm/lsu.h>
33#include <asm/sections.h>
34#include <asm/mmu_context.h>
35#include <asm/setup.h>
36
37int show_unhandled_signals = 1;
38
39static inline __kprobes int notify_page_fault(struct pt_regs *regs)
40{
41	int ret = 0;
42
43	/* kprobe_running() needs smp_processor_id() */
44	if (kprobes_built_in() && !user_mode(regs)) {
45		preempt_disable();
46		if (kprobe_running() && kprobe_fault_handler(regs, 0))
47			ret = 1;
48		preempt_enable();
49	}
50	return ret;
51}
52
53static void __kprobes unhandled_fault(unsigned long address,
54				      struct task_struct *tsk,
55				      struct pt_regs *regs)
56{
57	if ((unsigned long) address < PAGE_SIZE) {
58		printk(KERN_ALERT "Unable to handle kernel NULL "
59		       "pointer dereference\n");
60	} else {
61		printk(KERN_ALERT "Unable to handle kernel paging request "
62		       "at virtual address %016lx\n", (unsigned long)address);
63	}
64	printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
65	       (tsk->mm ?
66		CTX_HWBITS(tsk->mm->context) :
67		CTX_HWBITS(tsk->active_mm->context)));
68	printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
69	       (tsk->mm ? (unsigned long) tsk->mm->pgd :
70		          (unsigned long) tsk->active_mm->pgd));
71	die_if_kernel("Oops", regs);
72}
73
74static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
75{
76	printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
77	       regs->tpc);
78	printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
79	printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
80	printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
81	dump_stack();
82	unhandled_fault(regs->tpc, current, regs);
83}
84
85/*
86 * We now make sure that mmap_sem is held in all paths that call
87 * this. Additionally, to prevent kswapd from ripping ptes from
88 * under us, raise interrupts around the time that we look at the
89 * pte, kswapd will have to wait to get his smp ipi response from
90 * us. vmtruncate likewise. This saves us having to get pte lock.
91 */
92static unsigned int get_user_insn(unsigned long tpc)
93{
94	pgd_t *pgdp = pgd_offset(current->mm, tpc);
95	pud_t *pudp;
96	pmd_t *pmdp;
97	pte_t *ptep, pte;
98	unsigned long pa;
99	u32 insn = 0;
100
101	if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp)))
102		goto out;
103	pudp = pud_offset(pgdp, tpc);
104	if (pud_none(*pudp) || unlikely(pud_bad(*pudp)))
105		goto out;
106
107	/* This disables preemption for us as well. */
108	local_irq_disable();
109
110	pmdp = pmd_offset(pudp, tpc);
111	if (pmd_none(*pmdp) || unlikely(pmd_bad(*pmdp)))
112		goto out_irq_enable;
113
114#ifdef CONFIG_TRANSPARENT_HUGEPAGE
115	if (pmd_trans_huge(*pmdp)) {
116		if (pmd_trans_splitting(*pmdp))
117			goto out_irq_enable;
118
119		pa  = pmd_pfn(*pmdp) << PAGE_SHIFT;
120		pa += tpc & ~HPAGE_MASK;
121
122		/* Use phys bypass so we don't pollute dtlb/dcache. */
123		__asm__ __volatile__("lduwa [%1] %2, %0"
124				     : "=r" (insn)
125				     : "r" (pa), "i" (ASI_PHYS_USE_EC));
126	} else
127#endif
128	{
129		ptep = pte_offset_map(pmdp, tpc);
130		pte = *ptep;
131		if (pte_present(pte)) {
132			pa  = (pte_pfn(pte) << PAGE_SHIFT);
133			pa += (tpc & ~PAGE_MASK);
134
135			/* Use phys bypass so we don't pollute dtlb/dcache. */
136			__asm__ __volatile__("lduwa [%1] %2, %0"
137					     : "=r" (insn)
138					     : "r" (pa), "i" (ASI_PHYS_USE_EC));
139		}
140		pte_unmap(ptep);
141	}
142out_irq_enable:
143	local_irq_enable();
144out:
145	return insn;
146}
147
148static inline void
149show_signal_msg(struct pt_regs *regs, int sig, int code,
150		unsigned long address, struct task_struct *tsk)
151{
152	if (!unhandled_signal(tsk, sig))
153		return;
154
155	if (!printk_ratelimit())
156		return;
157
158	printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
159	       task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
160	       tsk->comm, task_pid_nr(tsk), address,
161	       (void *)regs->tpc, (void *)regs->u_regs[UREG_I7],
162	       (void *)regs->u_regs[UREG_FP], code);
163
164	print_vma_addr(KERN_CONT " in ", regs->tpc);
165
166	printk(KERN_CONT "\n");
167}
168
169static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
170			     unsigned long fault_addr, unsigned int insn,
171			     int fault_code)
172{
173	unsigned long addr;
174	siginfo_t info;
175
176	info.si_code = code;
177	info.si_signo = sig;
178	info.si_errno = 0;
179	if (fault_code & FAULT_CODE_ITLB) {
180		addr = regs->tpc;
181	} else {
182		/* If we were able to probe the faulting instruction, use it
183		 * to compute a precise fault address.  Otherwise use the fault
184		 * time provided address which may only have page granularity.
185		 */
186		if (insn)
187			addr = compute_effective_address(regs, insn, 0);
188		else
189			addr = fault_addr;
190	}
191	info.si_addr = (void __user *) addr;
192	info.si_trapno = 0;
193
194	if (unlikely(show_unhandled_signals))
195		show_signal_msg(regs, sig, code, addr, current);
196
197	force_sig_info(sig, &info, current);
198}
199
200static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
201{
202	if (!insn) {
203		if (!regs->tpc || (regs->tpc & 0x3))
204			return 0;
205		if (regs->tstate & TSTATE_PRIV) {
206			insn = *(unsigned int *) regs->tpc;
207		} else {
208			insn = get_user_insn(regs->tpc);
209		}
210	}
211	return insn;
212}
213
214static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
215				      int fault_code, unsigned int insn,
216				      unsigned long address)
217{
218	unsigned char asi = ASI_P;
219
220	if ((!insn) && (regs->tstate & TSTATE_PRIV))
221		goto cannot_handle;
222
223	/* If user insn could be read (thus insn is zero), that
224	 * is fine.  We will just gun down the process with a signal
225	 * in that case.
226	 */
227
228	if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
229	    (insn & 0xc0800000) == 0xc0800000) {
230		if (insn & 0x2000)
231			asi = (regs->tstate >> 24);
232		else
233			asi = (insn >> 5);
234		if ((asi & 0xf2) == 0x82) {
235			if (insn & 0x1000000) {
236				handle_ldf_stq(insn, regs);
237			} else {
238				/* This was a non-faulting load. Just clear the
239				 * destination register(s) and continue with the next
240				 * instruction. -jj
241				 */
242				handle_ld_nf(insn, regs);
243			}
244			return;
245		}
246	}
247
248	/* Is this in ex_table? */
249	if (regs->tstate & TSTATE_PRIV) {
250		const struct exception_table_entry *entry;
251
252		entry = search_exception_tables(regs->tpc);
253		if (entry) {
254			regs->tpc = entry->fixup;
255			regs->tnpc = regs->tpc + 4;
256			return;
257		}
258	} else {
259		/* The si_code was set to make clear whether
260		 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
261		 */
262		do_fault_siginfo(si_code, SIGSEGV, regs, address, insn, fault_code);
263		return;
264	}
265
266cannot_handle:
267	unhandled_fault (address, current, regs);
268}
269
270static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
271{
272	static int times;
273
274	if (times++ < 10)
275		printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
276		       "64-bit TPC [%lx]\n",
277		       current->comm, current->pid,
278		       regs->tpc);
279	show_regs(regs);
280}
281
282asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
283{
284	enum ctx_state prev_state = exception_enter();
285	struct mm_struct *mm = current->mm;
286	struct vm_area_struct *vma;
287	unsigned int insn = 0;
288	int si_code, fault_code, fault;
289	unsigned long address, mm_rss;
290	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
291
292	fault_code = get_thread_fault_code();
293
294	if (notify_page_fault(regs))
295		goto exit_exception;
296
297	si_code = SEGV_MAPERR;
298	address = current_thread_info()->fault_address;
299
300	if ((fault_code & FAULT_CODE_ITLB) &&
301	    (fault_code & FAULT_CODE_DTLB))
302		BUG();
303
304	if (test_thread_flag(TIF_32BIT)) {
305		if (!(regs->tstate & TSTATE_PRIV)) {
306			if (unlikely((regs->tpc >> 32) != 0)) {
307				bogus_32bit_fault_tpc(regs);
308				goto intr_or_no_mm;
309			}
310		}
311		if (unlikely((address >> 32) != 0))
312			goto intr_or_no_mm;
313	}
314
315	if (regs->tstate & TSTATE_PRIV) {
316		unsigned long tpc = regs->tpc;
317
318		/* Sanity check the PC. */
319		if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
320		    (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
321			/* Valid, no problems... */
322		} else {
323			bad_kernel_pc(regs, address);
324			goto exit_exception;
325		}
326	} else
327		flags |= FAULT_FLAG_USER;
328
329	/*
330	 * If we're in an interrupt or have no user
331	 * context, we must not take the fault..
332	 */
333	if (in_atomic() || !mm)
334		goto intr_or_no_mm;
335
336	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
337
338	if (!down_read_trylock(&mm->mmap_sem)) {
339		if ((regs->tstate & TSTATE_PRIV) &&
340		    !search_exception_tables(regs->tpc)) {
341			insn = get_fault_insn(regs, insn);
342			goto handle_kernel_fault;
343		}
344
345retry:
346		down_read(&mm->mmap_sem);
347	}
348
349	if (fault_code & FAULT_CODE_BAD_RA)
350		goto do_sigbus;
351
352	vma = find_vma(mm, address);
353	if (!vma)
354		goto bad_area;
355
356	/* Pure DTLB misses do not tell us whether the fault causing
357	 * load/store/atomic was a write or not, it only says that there
358	 * was no match.  So in such a case we (carefully) read the
359	 * instruction to try and figure this out.  It's an optimization
360	 * so it's ok if we can't do this.
361	 *
362	 * Special hack, window spill/fill knows the exact fault type.
363	 */
364	if (((fault_code &
365	      (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
366	    (vma->vm_flags & VM_WRITE) != 0) {
367		insn = get_fault_insn(regs, 0);
368		if (!insn)
369			goto continue_fault;
370		/* All loads, stores and atomics have bits 30 and 31 both set
371		 * in the instruction.  Bit 21 is set in all stores, but we
372		 * have to avoid prefetches which also have bit 21 set.
373		 */
374		if ((insn & 0xc0200000) == 0xc0200000 &&
375		    (insn & 0x01780000) != 0x01680000) {
376			/* Don't bother updating thread struct value,
377			 * because update_mmu_cache only cares which tlb
378			 * the access came from.
379			 */
380			fault_code |= FAULT_CODE_WRITE;
381		}
382	}
383continue_fault:
384
385	if (vma->vm_start <= address)
386		goto good_area;
387	if (!(vma->vm_flags & VM_GROWSDOWN))
388		goto bad_area;
389	if (!(fault_code & FAULT_CODE_WRITE)) {
390		/* Non-faulting loads shouldn't expand stack. */
391		insn = get_fault_insn(regs, insn);
392		if ((insn & 0xc0800000) == 0xc0800000) {
393			unsigned char asi;
394
395			if (insn & 0x2000)
396				asi = (regs->tstate >> 24);
397			else
398				asi = (insn >> 5);
399			if ((asi & 0xf2) == 0x82)
400				goto bad_area;
401		}
402	}
403	if (expand_stack(vma, address))
404		goto bad_area;
405	/*
406	 * Ok, we have a good vm_area for this memory access, so
407	 * we can handle it..
408	 */
409good_area:
410	si_code = SEGV_ACCERR;
411
412	/* If we took a ITLB miss on a non-executable page, catch
413	 * that here.
414	 */
415	if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
416		BUG_ON(address != regs->tpc);
417		BUG_ON(regs->tstate & TSTATE_PRIV);
418		goto bad_area;
419	}
420
421	if (fault_code & FAULT_CODE_WRITE) {
422		if (!(vma->vm_flags & VM_WRITE))
423			goto bad_area;
424
425		/* Spitfire has an icache which does not snoop
426		 * processor stores.  Later processors do...
427		 */
428		if (tlb_type == spitfire &&
429		    (vma->vm_flags & VM_EXEC) != 0 &&
430		    vma->vm_file != NULL)
431			set_thread_fault_code(fault_code |
432					      FAULT_CODE_BLKCOMMIT);
433
434		flags |= FAULT_FLAG_WRITE;
435	} else {
436		/* Allow reads even for write-only mappings */
437		if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
438			goto bad_area;
439	}
440
441	fault = handle_mm_fault(mm, vma, address, flags);
442
443	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
444		goto exit_exception;
445
446	if (unlikely(fault & VM_FAULT_ERROR)) {
447		if (fault & VM_FAULT_OOM)
448			goto out_of_memory;
449		else if (fault & VM_FAULT_SIGSEGV)
450			goto bad_area;
451		else if (fault & VM_FAULT_SIGBUS)
452			goto do_sigbus;
453		BUG();
454	}
455
456	if (flags & FAULT_FLAG_ALLOW_RETRY) {
457		if (fault & VM_FAULT_MAJOR) {
458			current->maj_flt++;
459			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
460				      1, regs, address);
461		} else {
462			current->min_flt++;
463			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
464				      1, regs, address);
465		}
466		if (fault & VM_FAULT_RETRY) {
467			flags &= ~FAULT_FLAG_ALLOW_RETRY;
468			flags |= FAULT_FLAG_TRIED;
469
470			/* No need to up_read(&mm->mmap_sem) as we would
471			 * have already released it in __lock_page_or_retry
472			 * in mm/filemap.c.
473			 */
474
475			goto retry;
476		}
477	}
478	up_read(&mm->mmap_sem);
479
480	mm_rss = get_mm_rss(mm);
481#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
482	mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
483#endif
484	if (unlikely(mm_rss >
485		     mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
486		tsb_grow(mm, MM_TSB_BASE, mm_rss);
487#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
488	mm_rss = mm->context.huge_pte_count;
489	if (unlikely(mm_rss >
490		     mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit)) {
491		if (mm->context.tsb_block[MM_TSB_HUGE].tsb)
492			tsb_grow(mm, MM_TSB_HUGE, mm_rss);
493		else
494			hugetlb_setup(regs);
495
496	}
497#endif
498exit_exception:
499	exception_exit(prev_state);
500	return;
501
502	/*
503	 * Something tried to access memory that isn't in our memory map..
504	 * Fix it, but check if it's kernel or user first..
505	 */
506bad_area:
507	insn = get_fault_insn(regs, insn);
508	up_read(&mm->mmap_sem);
509
510handle_kernel_fault:
511	do_kernel_fault(regs, si_code, fault_code, insn, address);
512	goto exit_exception;
513
514/*
515 * We ran out of memory, or some other thing happened to us that made
516 * us unable to handle the page fault gracefully.
517 */
518out_of_memory:
519	insn = get_fault_insn(regs, insn);
520	up_read(&mm->mmap_sem);
521	if (!(regs->tstate & TSTATE_PRIV)) {
522		pagefault_out_of_memory();
523		goto exit_exception;
524	}
525	goto handle_kernel_fault;
526
527intr_or_no_mm:
528	insn = get_fault_insn(regs, 0);
529	goto handle_kernel_fault;
530
531do_sigbus:
532	insn = get_fault_insn(regs, insn);
533	up_read(&mm->mmap_sem);
534
535	/*
536	 * Send a sigbus, regardless of whether we were in kernel
537	 * or user mode.
538	 */
539	do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, address, insn, fault_code);
540
541	/* Kernel mode? Handle exceptions or die */
542	if (regs->tstate & TSTATE_PRIV)
543		goto handle_kernel_fault;
544}
545