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