1 /* $Id: fault.c,v 1.122 2001/11/17 07:19:26 davem Exp $
2  * fault.c:  Page fault handlers for the Sparc.
3  *
4  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
6  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7  */
8 
9 #include <asm/head.h>
10 
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/threads.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
22 
23 #include <asm/system.h>
24 #include <asm/segment.h>
25 #include <asm/page.h>
26 #include <asm/pgtable.h>
27 #include <asm/memreg.h>
28 #include <asm/openprom.h>
29 #include <asm/oplib.h>
30 #include <asm/smp.h>
31 #include <asm/traps.h>
32 #include <asm/kdebug.h>
33 #include <asm/uaccess.h>
34 
35 #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
36 
37 extern struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
38 extern int prom_node_root;
39 
40 /* At boot time we determine these two values necessary for setting
41  * up the segment maps and page table entries (pte's).
42  */
43 
44 int num_segmaps, num_contexts;
45 int invalid_segment;
46 
47 /* various Virtual Address Cache parameters we find at boot time... */
48 
49 int vac_size, vac_linesize, vac_do_hw_vac_flushes;
50 int vac_entries_per_context, vac_entries_per_segment;
51 int vac_entries_per_page;
52 
53 /* Nice, simple, prom library does all the sweating for us. ;) */
prom_probe_memory(void)54 int prom_probe_memory (void)
55 {
56 	register struct linux_mlist_v0 *mlist;
57 	register unsigned long bytes, base_paddr, tally;
58 	register int i;
59 
60 	i = 0;
61 	mlist= *prom_meminfo()->v0_available;
62 	bytes = tally = mlist->num_bytes;
63 	base_paddr = (unsigned long) mlist->start_adr;
64 
65 	sp_banks[0].base_addr = base_paddr;
66 	sp_banks[0].num_bytes = bytes;
67 
68 	while (mlist->theres_more != (void *) 0){
69 		i++;
70 		mlist = mlist->theres_more;
71 		bytes = mlist->num_bytes;
72 		tally += bytes;
73 		if (i >= SPARC_PHYS_BANKS-1) {
74 			printk ("The machine has more banks than "
75 				"this kernel can support\n"
76 				"Increase the SPARC_PHYS_BANKS "
77 				"setting (currently %d)\n",
78 				SPARC_PHYS_BANKS);
79 			i = SPARC_PHYS_BANKS-1;
80 			break;
81 		}
82 
83 		sp_banks[i].base_addr = (unsigned long) mlist->start_adr;
84 		sp_banks[i].num_bytes = mlist->num_bytes;
85 	}
86 
87 	i++;
88 	sp_banks[i].base_addr = 0xdeadbeef;
89 	sp_banks[i].num_bytes = 0;
90 
91 	/* Now mask all bank sizes on a page boundary, it is all we can
92 	 * use anyways.
93 	 */
94 	for(i=0; sp_banks[i].num_bytes != 0; i++)
95 		sp_banks[i].num_bytes &= PAGE_MASK;
96 
97 	return tally;
98 }
99 
100 /* Traverse the memory lists in the prom to see how much physical we
101  * have.
102  */
103 unsigned long
probe_memory(void)104 probe_memory(void)
105 {
106 	int total;
107 
108 	total = prom_probe_memory();
109 
110 	/* Oh man, much nicer, keep the dirt in promlib. */
111 	return total;
112 }
113 
114 extern void sun4c_complete_all_stores(void);
115 
116 /* Whee, a level 15 NMI interrupt memory error.  Let's have fun... */
sparc_lvl15_nmi(struct pt_regs * regs,unsigned long serr,unsigned long svaddr,unsigned long aerr,unsigned long avaddr)117 asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
118 				unsigned long svaddr, unsigned long aerr,
119 				unsigned long avaddr)
120 {
121 	sun4c_complete_all_stores();
122 	printk("FAULT: NMI received\n");
123 	printk("SREGS: Synchronous Error %08lx\n", serr);
124 	printk("       Synchronous Vaddr %08lx\n", svaddr);
125 	printk("      Asynchronous Error %08lx\n", aerr);
126 	printk("      Asynchronous Vaddr %08lx\n", avaddr);
127 	if (sun4c_memerr_reg)
128 		printk("     Memory Parity Error %08lx\n", *sun4c_memerr_reg);
129 	printk("REGISTER DUMP:\n");
130 	show_regs(regs);
131 	prom_halt();
132 }
133 
134 static void unhandled_fault(unsigned long, struct task_struct *,
135 		struct pt_regs *) __attribute__ ((noreturn));
136 
unhandled_fault(unsigned long address,struct task_struct * tsk,struct pt_regs * regs)137 static void unhandled_fault(unsigned long address, struct task_struct *tsk,
138                      struct pt_regs *regs)
139 {
140 	if((unsigned long) address < PAGE_SIZE) {
141 		printk(KERN_ALERT "Unable to handle kernel NULL "
142 		       "pointer dereference\n");
143 	} else {
144 		printk(KERN_ALERT "Unable to handle kernel paging request "
145 		       "at virtual address %08lx\n", address);
146 	}
147 	printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
148 		(tsk->mm ? tsk->mm->context : tsk->active_mm->context));
149 	printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
150 		(tsk->mm ? (unsigned long) tsk->mm->pgd :
151 		 	(unsigned long) tsk->active_mm->pgd));
152 	die_if_kernel("Oops", regs);
153 }
154 
lookup_fault(unsigned long pc,unsigned long ret_pc,unsigned long address)155 asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
156 			    unsigned long address)
157 {
158 	struct pt_regs regs;
159 	unsigned long g2;
160 	unsigned int insn;
161 	int i;
162 
163 	i = search_exception_table(ret_pc, &g2);
164 	switch (i) {
165 	case 3:
166 		/* load & store will be handled by fixup */
167 		return 3;
168 
169 	case 1:
170 		/* store will be handled by fixup, load will bump out */
171 		/* for _to_ macros */
172 		insn = *((unsigned int *) pc);
173 		if ((insn >> 21) & 1)
174 			return 1;
175 		break;
176 
177 	case 2:
178 		/* load will be handled by fixup, store will bump out */
179 		/* for _from_ macros */
180 		insn = *((unsigned int *) pc);
181 		if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
182 			return 2;
183 		break;
184 
185 	default:
186 		break;
187 	};
188 
189 	memset(&regs, 0, sizeof (regs));
190 	regs.pc = pc;
191 	regs.npc = pc + 4;
192 	__asm__ __volatile__(
193 		"rd %%psr, %0\n\t"
194 		"nop\n\t"
195 		"nop\n\t"
196 		"nop\n" : "=r" (regs.psr));
197 	unhandled_fault(address, current, &regs);
198 
199 	/* Not reached */
200 	return 0;
201 }
202 
203 extern unsigned long safe_compute_effective_address(struct pt_regs *,
204 						    unsigned int);
205 
compute_si_addr(struct pt_regs * regs,int text_fault)206 static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
207 {
208 	unsigned int insn;
209 
210 	if (text_fault)
211 		return regs->pc;
212 
213 	if (regs->psr & PSR_PS) {
214 		insn = *(unsigned int *) regs->pc;
215 	} else {
216 		__get_user(insn, (unsigned int *) regs->pc);
217 	}
218 
219 	return safe_compute_effective_address(regs, insn);
220 }
221 
do_sparc_fault(struct pt_regs * regs,int text_fault,int write,unsigned long address)222 asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
223 			       unsigned long address)
224 {
225 	struct vm_area_struct *vma;
226 	struct task_struct *tsk = current;
227 	struct mm_struct *mm = tsk->mm;
228 	unsigned int fixup;
229 	unsigned long g2;
230 	siginfo_t info;
231 	int from_user = !(regs->psr & PSR_PS);
232 
233 	if(text_fault)
234 		address = regs->pc;
235 
236 	/*
237 	 * We fault-in kernel-space virtual memory on-demand. The
238 	 * 'reference' page table is init_mm.pgd.
239 	 *
240 	 * NOTE! We MUST NOT take any locks for this case. We may
241 	 * be in an interrupt or a critical region, and should
242 	 * only copy the information from the master page table,
243 	 * nothing more.
244 	 */
245 	if (!ARCH_SUN4C_SUN4 && address >= TASK_SIZE)
246 		goto vmalloc_fault;
247 
248 	info.si_code = SEGV_MAPERR;
249 
250 	/*
251 	 * If we're in an interrupt or have no user
252 	 * context, we must not take the fault..
253 	 */
254         if (in_interrupt() || !mm)
255                 goto no_context;
256 
257 	down_read(&mm->mmap_sem);
258 
259 	/*
260 	 * The kernel referencing a bad kernel pointer can lock up
261 	 * a sun4c machine completely, so we must attempt recovery.
262 	 */
263 	if(!from_user && address >= PAGE_OFFSET)
264 		goto bad_area;
265 
266 	vma = find_vma(mm, address);
267 	if(!vma)
268 		goto bad_area;
269 	if(vma->vm_start <= address)
270 		goto good_area;
271 	if(!(vma->vm_flags & VM_GROWSDOWN))
272 		goto bad_area;
273 	if(expand_stack(vma, address))
274 		goto bad_area;
275 	/*
276 	 * Ok, we have a good vm_area for this memory access, so
277 	 * we can handle it..
278 	 */
279 good_area:
280 	info.si_code = SEGV_ACCERR;
281 	if(write) {
282 		if(!(vma->vm_flags & VM_WRITE))
283 			goto bad_area;
284 	} else {
285 		/* Allow reads even for write-only mappings */
286 		if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
287 			goto bad_area;
288 	}
289 
290 	/*
291 	 * If for any reason at all we couldn't handle the fault,
292 	 * make sure we exit gracefully rather than endlessly redo
293 	 * the fault.
294 	 */
295 	switch (handle_mm_fault(mm, vma, address, write)) {
296 	case 1:
297 		current->min_flt++;
298 		break;
299 	case 2:
300 		current->maj_flt++;
301 		break;
302 	case 0:
303 		goto do_sigbus;
304 	default:
305 		goto out_of_memory;
306 	}
307 	up_read(&mm->mmap_sem);
308 	return;
309 
310 	/*
311 	 * Something tried to access memory that isn't in our memory map..
312 	 * Fix it, but check if it's kernel or user first..
313 	 */
314 bad_area:
315 	up_read(&mm->mmap_sem);
316 
317 bad_area_nosemaphore:
318 	/* User mode accesses just cause a SIGSEGV */
319 	if(from_user) {
320 #if 0
321 		printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
322 		       tsk->comm, tsk->pid, address, regs->pc);
323 #endif
324 		info.si_signo = SIGSEGV;
325 		info.si_errno = 0;
326 		/* info.si_code set above to make clear whether
327 		   this was a SEGV_MAPERR or SEGV_ACCERR fault.  */
328 		info.si_addr = (void *) compute_si_addr(regs, text_fault);
329 		info.si_trapno = 0;
330 		force_sig_info (SIGSEGV, &info, tsk);
331 		return;
332 	}
333 
334 	/* Is this in ex_table? */
335 no_context:
336 	g2 = regs->u_regs[UREG_G2];
337 	if (!from_user && (fixup = search_exception_table (regs->pc, &g2))) {
338 		if (fixup > 10) { /* Values below are reserved for other things */
339 			extern const unsigned __memset_start[];
340 			extern const unsigned __memset_end[];
341 			extern const unsigned __csum_partial_copy_start[];
342 			extern const unsigned __csum_partial_copy_end[];
343 
344 #ifdef DEBUG_EXCEPTIONS
345 			printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
346 			printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
347 				regs->pc, fixup, g2);
348 #endif
349 			if ((regs->pc >= (unsigned long)__memset_start &&
350 			     regs->pc < (unsigned long)__memset_end) ||
351 			    (regs->pc >= (unsigned long)__csum_partial_copy_start &&
352 			     regs->pc < (unsigned long)__csum_partial_copy_end)) {
353 			        regs->u_regs[UREG_I4] = address;
354 				regs->u_regs[UREG_I5] = regs->pc;
355 			}
356 			regs->u_regs[UREG_G2] = g2;
357 			regs->pc = fixup;
358 			regs->npc = regs->pc + 4;
359 			return;
360 		}
361 	}
362 
363 	unhandled_fault (address, tsk, regs);
364 	do_exit(SIGKILL);
365 
366 /*
367  * We ran out of memory, or some other thing happened to us that made
368  * us unable to handle the page fault gracefully.
369  */
370 out_of_memory:
371 	up_read(&mm->mmap_sem);
372 	printk("VM: killing process %s\n", tsk->comm);
373 	if (from_user)
374 		do_exit(SIGKILL);
375 	goto no_context;
376 
377 do_sigbus:
378 	up_read(&mm->mmap_sem);
379 	info.si_signo = SIGBUS;
380 	info.si_errno = 0;
381 	info.si_code = BUS_ADRERR;
382 	info.si_addr = (void *) compute_si_addr(regs, text_fault);
383 	info.si_trapno = 0;
384 	force_sig_info (SIGBUS, &info, tsk);
385 	if (!from_user)
386 		goto no_context;
387 
388 vmalloc_fault:
389 	{
390 		/*
391 		 * Synchronize this task's top level page-table
392 		 * with the 'reference' page table.
393 		 */
394 		int offset = pgd_index(address);
395 		pgd_t *pgd, *pgd_k;
396 		pmd_t *pmd, *pmd_k;
397 
398 		pgd = tsk->active_mm->pgd + offset;
399 		pgd_k = init_mm.pgd + offset;
400 
401 		if (!pgd_present(*pgd)) {
402 			if (!pgd_present(*pgd_k))
403 				goto bad_area_nosemaphore;
404 			pgd_val(*pgd) = pgd_val(*pgd_k);
405 			return;
406 		}
407 
408 		pmd = pmd_offset(pgd, address);
409 		pmd_k = pmd_offset(pgd_k, address);
410 
411 		if (pmd_present(*pmd) || !pmd_present(*pmd_k))
412 			goto bad_area_nosemaphore;
413 		pmd_val(*pmd) = pmd_val(*pmd_k);
414 		return;
415 	}
416 }
417 
do_sun4c_fault(struct pt_regs * regs,int text_fault,int write,unsigned long address)418 asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
419 			       unsigned long address)
420 {
421 	extern void sun4c_update_mmu_cache(struct vm_area_struct *,
422 					   unsigned long,pte_t);
423 	extern pte_t *sun4c_pte_offset(pmd_t *,unsigned long);
424 	struct task_struct *tsk = current;
425 	struct mm_struct *mm = tsk->mm;
426 	pgd_t *pgdp;
427 	pte_t *ptep;
428 
429 	if (text_fault) {
430 		address = regs->pc;
431 	} else if (!write &&
432 		   !(regs->psr & PSR_PS)) {
433 		unsigned int insn, *ip;
434 
435 		ip = (unsigned int *)regs->pc;
436 		if (! get_user(insn, ip)) {
437 			if ((insn & 0xc1680000) == 0xc0680000)
438 				write = 1;
439 		}
440 	}
441 
442 	pgdp = pgd_offset(mm, address);
443 	ptep = sun4c_pte_offset((pmd_t *) pgdp, address);
444 
445 	if (pgd_val(*pgdp)) {
446 	    if (write) {
447 		if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
448 				   == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
449 			unsigned long flags;
450 
451 			*ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
452 				      _SUN4C_PAGE_MODIFIED |
453 				      _SUN4C_PAGE_VALID |
454 				      _SUN4C_PAGE_DIRTY);
455 
456 			save_and_cli(flags);
457 			if (sun4c_get_segmap(address) != invalid_segment) {
458 				sun4c_put_pte(address, pte_val(*ptep));
459 				restore_flags(flags);
460 				return;
461 			}
462 			restore_flags(flags);
463 		}
464 	    } else {
465 		if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
466 				   == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
467 			unsigned long flags;
468 
469 			*ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
470 				      _SUN4C_PAGE_VALID);
471 
472 			save_and_cli(flags);
473 			if (sun4c_get_segmap(address) != invalid_segment) {
474 				sun4c_put_pte(address, pte_val(*ptep));
475 				restore_flags(flags);
476 				return;
477 			}
478 			restore_flags(flags);
479 		}
480 	    }
481 	}
482 
483 	/* This conditional is 'interesting'. */
484 	if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
485 	    && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
486 		/* Note: It is safe to not grab the MMAP semaphore here because
487 		 *       we know that update_mmu_cache() will not sleep for
488 		 *       any reason (at least not in the current implementation)
489 		 *       and therefore there is no danger of another thread getting
490 		 *       on the CPU and doing a shrink_mmap() on this vma.
491 		 */
492 		sun4c_update_mmu_cache (find_vma(current->mm, address), address,
493 					*ptep);
494 	else
495 		do_sparc_fault(regs, text_fault, write, address);
496 }
497 
498 /* This always deals with user addresses. */
force_user_fault(unsigned long address,int write)499 inline void force_user_fault(unsigned long address, int write)
500 {
501 	struct vm_area_struct *vma;
502 	struct task_struct *tsk = current;
503 	struct mm_struct *mm = tsk->mm;
504 	siginfo_t info;
505 
506 	info.si_code = SEGV_MAPERR;
507 
508 #if 0
509 	printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
510 	       tsk->pid, write, address);
511 #endif
512 	down_read(&mm->mmap_sem);
513 	vma = find_vma(mm, address);
514 	if(!vma)
515 		goto bad_area;
516 	if(vma->vm_start <= address)
517 		goto good_area;
518 	if(!(vma->vm_flags & VM_GROWSDOWN))
519 		goto bad_area;
520 	if(expand_stack(vma, address))
521 		goto bad_area;
522 good_area:
523 	info.si_code = SEGV_ACCERR;
524 	if(write) {
525 		if(!(vma->vm_flags & VM_WRITE))
526 			goto bad_area;
527 	} else {
528 		if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
529 			goto bad_area;
530 	}
531 	if (!handle_mm_fault(mm, vma, address, write))
532 		goto do_sigbus;
533 	up_read(&mm->mmap_sem);
534 	return;
535 bad_area:
536 	up_read(&mm->mmap_sem);
537 #if 0
538 	printk("Window whee %s [%d]: segfaults at %08lx\n",
539 	       tsk->comm, tsk->pid, address);
540 #endif
541 	info.si_signo = SIGSEGV;
542 	info.si_errno = 0;
543 	/* info.si_code set above to make clear whether
544 	   this was a SEGV_MAPERR or SEGV_ACCERR fault.  */
545 	info.si_addr = (void *) address;
546 	info.si_trapno = 0;
547 	force_sig_info (SIGSEGV, &info, tsk);
548 	return;
549 
550 do_sigbus:
551 	up_read(&mm->mmap_sem);
552 	info.si_signo = SIGBUS;
553 	info.si_errno = 0;
554 	info.si_code = BUS_ADRERR;
555 	info.si_addr = (void *) address;
556 	info.si_trapno = 0;
557 	force_sig_info (SIGBUS, &info, tsk);
558 }
559 
window_overflow_fault(void)560 void window_overflow_fault(void)
561 {
562 	unsigned long sp;
563 
564 	sp = current->thread.rwbuf_stkptrs[0];
565 	if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
566 		force_user_fault(sp + 0x38, 1);
567 	force_user_fault(sp, 1);
568 }
569 
window_underflow_fault(unsigned long sp)570 void window_underflow_fault(unsigned long sp)
571 {
572 	if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
573 		force_user_fault(sp + 0x38, 0);
574 	force_user_fault(sp, 0);
575 }
576 
window_ret_fault(struct pt_regs * regs)577 void window_ret_fault(struct pt_regs *regs)
578 {
579 	unsigned long sp;
580 
581 	sp = regs->u_regs[UREG_FP];
582 	if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
583 		force_user_fault(sp + 0x38, 0);
584 	force_user_fault(sp, 0);
585 }
586