Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* $Id: fault.c,v 1.59 2002/02/09 19:49:31 davem Exp $ |
2 | * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc. | |
3 | * | |
4 | * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu) | |
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/smp_lock.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/interrupt.h> | |
21 | ||
22 | #include <asm/page.h> | |
23 | #include <asm/pgtable.h> | |
24 | #include <asm/openprom.h> | |
25 | #include <asm/oplib.h> | |
26 | #include <asm/uaccess.h> | |
27 | #include <asm/asi.h> | |
28 | #include <asm/lsu.h> | |
29 | #include <asm/sections.h> | |
30 | #include <asm/kdebug.h> | |
31 | ||
32 | #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0])) | |
33 | ||
34 | extern struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS]; | |
35 | ||
1da177e4 LT |
36 | /* |
37 | * To debug kernel to catch accesses to certain virtual/physical addresses. | |
38 | * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints. | |
39 | * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses. | |
40 | * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be | |
41 | * watched. This is only useful on a single cpu machine for now. After the watchpoint | |
42 | * is detected, the process causing it will be killed, thus preventing an infinite loop. | |
43 | */ | |
44 | void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode) | |
45 | { | |
46 | unsigned long lsubits; | |
47 | ||
48 | __asm__ __volatile__("ldxa [%%g0] %1, %0" | |
49 | : "=r" (lsubits) | |
50 | : "i" (ASI_LSU_CONTROL)); | |
51 | lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM | | |
52 | LSU_CONTROL_PR | LSU_CONTROL_VR | | |
53 | LSU_CONTROL_PW | LSU_CONTROL_VW); | |
54 | ||
55 | __asm__ __volatile__("stxa %0, [%1] %2\n\t" | |
56 | "membar #Sync" | |
57 | : /* no outputs */ | |
58 | : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT), | |
59 | "i" (ASI_DMMU)); | |
60 | ||
61 | lsubits |= ((unsigned long)mask << (mode ? 25 : 33)); | |
62 | if (flags & VM_READ) | |
63 | lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR); | |
64 | if (flags & VM_WRITE) | |
65 | lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW); | |
66 | __asm__ __volatile__("stxa %0, [%%g0] %1\n\t" | |
67 | "membar #Sync" | |
68 | : /* no outputs */ | |
69 | : "r" (lsubits), "i" (ASI_LSU_CONTROL) | |
70 | : "memory"); | |
71 | } | |
72 | ||
73 | /* Nice, simple, prom library does all the sweating for us. ;) */ | |
74 | unsigned long __init prom_probe_memory (void) | |
75 | { | |
76 | register struct linux_mlist_p1275 *mlist; | |
77 | register unsigned long bytes, base_paddr, tally; | |
78 | register int i; | |
79 | ||
80 | i = 0; | |
81 | mlist = *prom_meminfo()->p1275_available; | |
82 | bytes = tally = mlist->num_bytes; | |
83 | base_paddr = mlist->start_adr; | |
84 | ||
85 | sp_banks[0].base_addr = base_paddr; | |
86 | sp_banks[0].num_bytes = bytes; | |
87 | ||
88 | while (mlist->theres_more != (void *) 0) { | |
89 | i++; | |
90 | mlist = mlist->theres_more; | |
91 | bytes = mlist->num_bytes; | |
92 | tally += bytes; | |
93 | if (i >= SPARC_PHYS_BANKS-1) { | |
94 | printk ("The machine has more banks than " | |
95 | "this kernel can support\n" | |
96 | "Increase the SPARC_PHYS_BANKS " | |
97 | "setting (currently %d)\n", | |
98 | SPARC_PHYS_BANKS); | |
99 | i = SPARC_PHYS_BANKS-1; | |
100 | break; | |
101 | } | |
102 | ||
103 | sp_banks[i].base_addr = mlist->start_adr; | |
104 | sp_banks[i].num_bytes = mlist->num_bytes; | |
105 | } | |
106 | ||
107 | i++; | |
108 | sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL; | |
109 | sp_banks[i].num_bytes = 0; | |
110 | ||
111 | /* Now mask all bank sizes on a page boundary, it is all we can | |
112 | * use anyways. | |
113 | */ | |
114 | for (i = 0; sp_banks[i].num_bytes != 0; i++) | |
115 | sp_banks[i].num_bytes &= PAGE_MASK; | |
116 | ||
117 | return tally; | |
118 | } | |
119 | ||
120 | static void unhandled_fault(unsigned long address, struct task_struct *tsk, | |
121 | struct pt_regs *regs) | |
122 | { | |
123 | if ((unsigned long) address < PAGE_SIZE) { | |
124 | printk(KERN_ALERT "Unable to handle kernel NULL " | |
125 | "pointer dereference\n"); | |
126 | } else { | |
127 | printk(KERN_ALERT "Unable to handle kernel paging request " | |
128 | "at virtual address %016lx\n", (unsigned long)address); | |
129 | } | |
130 | printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n", | |
131 | (tsk->mm ? | |
132 | CTX_HWBITS(tsk->mm->context) : | |
133 | CTX_HWBITS(tsk->active_mm->context))); | |
134 | printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n", | |
135 | (tsk->mm ? (unsigned long) tsk->mm->pgd : | |
136 | (unsigned long) tsk->active_mm->pgd)); | |
137 | if (notify_die(DIE_GPF, "general protection fault", regs, | |
138 | 0, 0, SIGSEGV) == NOTIFY_STOP) | |
139 | return; | |
140 | die_if_kernel("Oops", regs); | |
141 | } | |
142 | ||
143 | static void bad_kernel_pc(struct pt_regs *regs) | |
144 | { | |
145 | unsigned long *ksp; | |
146 | ||
147 | printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n", | |
148 | regs->tpc); | |
149 | __asm__("mov %%sp, %0" : "=r" (ksp)); | |
150 | show_stack(current, ksp); | |
151 | unhandled_fault(regs->tpc, current, regs); | |
152 | } | |
153 | ||
154 | /* | |
155 | * We now make sure that mmap_sem is held in all paths that call | |
156 | * this. Additionally, to prevent kswapd from ripping ptes from | |
157 | * under us, raise interrupts around the time that we look at the | |
158 | * pte, kswapd will have to wait to get his smp ipi response from | |
159 | * us. This saves us having to get page_table_lock. | |
160 | */ | |
161 | static unsigned int get_user_insn(unsigned long tpc) | |
162 | { | |
163 | pgd_t *pgdp = pgd_offset(current->mm, tpc); | |
164 | pud_t *pudp; | |
165 | pmd_t *pmdp; | |
166 | pte_t *ptep, pte; | |
167 | unsigned long pa; | |
168 | u32 insn = 0; | |
169 | unsigned long pstate; | |
170 | ||
171 | if (pgd_none(*pgdp)) | |
172 | goto outret; | |
173 | pudp = pud_offset(pgdp, tpc); | |
174 | if (pud_none(*pudp)) | |
175 | goto outret; | |
176 | pmdp = pmd_offset(pudp, tpc); | |
177 | if (pmd_none(*pmdp)) | |
178 | goto outret; | |
179 | ||
180 | /* This disables preemption for us as well. */ | |
181 | __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate)); | |
182 | __asm__ __volatile__("wrpr %0, %1, %%pstate" | |
183 | : : "r" (pstate), "i" (PSTATE_IE)); | |
184 | ptep = pte_offset_map(pmdp, tpc); | |
185 | pte = *ptep; | |
186 | if (!pte_present(pte)) | |
187 | goto out; | |
188 | ||
189 | pa = (pte_val(pte) & _PAGE_PADDR); | |
190 | pa += (tpc & ~PAGE_MASK); | |
191 | ||
192 | /* Use phys bypass so we don't pollute dtlb/dcache. */ | |
193 | __asm__ __volatile__("lduwa [%1] %2, %0" | |
194 | : "=r" (insn) | |
195 | : "r" (pa), "i" (ASI_PHYS_USE_EC)); | |
196 | ||
197 | out: | |
198 | pte_unmap(ptep); | |
199 | __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate)); | |
200 | outret: | |
201 | return insn; | |
202 | } | |
203 | ||
204 | extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int); | |
205 | ||
206 | static void do_fault_siginfo(int code, int sig, struct pt_regs *regs, | |
207 | unsigned int insn, int fault_code) | |
208 | { | |
209 | siginfo_t info; | |
210 | ||
211 | info.si_code = code; | |
212 | info.si_signo = sig; | |
213 | info.si_errno = 0; | |
214 | if (fault_code & FAULT_CODE_ITLB) | |
215 | info.si_addr = (void __user *) regs->tpc; | |
216 | else | |
217 | info.si_addr = (void __user *) | |
218 | compute_effective_address(regs, insn, 0); | |
219 | info.si_trapno = 0; | |
220 | force_sig_info(sig, &info, current); | |
221 | } | |
222 | ||
223 | extern int handle_ldf_stq(u32, struct pt_regs *); | |
224 | extern int handle_ld_nf(u32, struct pt_regs *); | |
225 | ||
226 | static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn) | |
227 | { | |
228 | if (!insn) { | |
229 | if (!regs->tpc || (regs->tpc & 0x3)) | |
230 | return 0; | |
231 | if (regs->tstate & TSTATE_PRIV) { | |
232 | insn = *(unsigned int *) regs->tpc; | |
233 | } else { | |
234 | insn = get_user_insn(regs->tpc); | |
235 | } | |
236 | } | |
237 | return insn; | |
238 | } | |
239 | ||
240 | static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code, | |
241 | unsigned int insn, unsigned long address) | |
242 | { | |
243 | unsigned long g2; | |
244 | unsigned char asi = ASI_P; | |
245 | ||
246 | if ((!insn) && (regs->tstate & TSTATE_PRIV)) | |
247 | goto cannot_handle; | |
248 | ||
249 | /* If user insn could be read (thus insn is zero), that | |
250 | * is fine. We will just gun down the process with a signal | |
251 | * in that case. | |
252 | */ | |
253 | ||
254 | if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) && | |
255 | (insn & 0xc0800000) == 0xc0800000) { | |
256 | if (insn & 0x2000) | |
257 | asi = (regs->tstate >> 24); | |
258 | else | |
259 | asi = (insn >> 5); | |
260 | if ((asi & 0xf2) == 0x82) { | |
261 | if (insn & 0x1000000) { | |
262 | handle_ldf_stq(insn, regs); | |
263 | } else { | |
264 | /* This was a non-faulting load. Just clear the | |
265 | * destination register(s) and continue with the next | |
266 | * instruction. -jj | |
267 | */ | |
268 | handle_ld_nf(insn, regs); | |
269 | } | |
270 | return; | |
271 | } | |
272 | } | |
273 | ||
274 | g2 = regs->u_regs[UREG_G2]; | |
275 | ||
276 | /* Is this in ex_table? */ | |
277 | if (regs->tstate & TSTATE_PRIV) { | |
278 | unsigned long fixup; | |
279 | ||
280 | if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) { | |
281 | if (insn & 0x2000) | |
282 | asi = (regs->tstate >> 24); | |
283 | else | |
284 | asi = (insn >> 5); | |
285 | } | |
286 | ||
287 | /* Look in asi.h: All _S asis have LS bit set */ | |
288 | if ((asi & 0x1) && | |
289 | (fixup = search_extables_range(regs->tpc, &g2))) { | |
290 | regs->tpc = fixup; | |
291 | regs->tnpc = regs->tpc + 4; | |
292 | regs->u_regs[UREG_G2] = g2; | |
293 | return; | |
294 | } | |
295 | } else { | |
296 | /* The si_code was set to make clear whether | |
297 | * this was a SEGV_MAPERR or SEGV_ACCERR fault. | |
298 | */ | |
299 | do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code); | |
300 | return; | |
301 | } | |
302 | ||
303 | cannot_handle: | |
304 | unhandled_fault (address, current, regs); | |
305 | } | |
306 | ||
307 | asmlinkage void do_sparc64_fault(struct pt_regs *regs) | |
308 | { | |
309 | struct mm_struct *mm = current->mm; | |
310 | struct vm_area_struct *vma; | |
311 | unsigned int insn = 0; | |
312 | int si_code, fault_code; | |
313 | unsigned long address; | |
314 | ||
315 | fault_code = get_thread_fault_code(); | |
316 | ||
317 | if (notify_die(DIE_PAGE_FAULT, "page_fault", regs, | |
318 | fault_code, 0, SIGSEGV) == NOTIFY_STOP) | |
319 | return; | |
320 | ||
321 | si_code = SEGV_MAPERR; | |
322 | address = current_thread_info()->fault_address; | |
323 | ||
324 | if ((fault_code & FAULT_CODE_ITLB) && | |
325 | (fault_code & FAULT_CODE_DTLB)) | |
326 | BUG(); | |
327 | ||
328 | if (regs->tstate & TSTATE_PRIV) { | |
329 | unsigned long tpc = regs->tpc; | |
330 | ||
331 | /* Sanity check the PC. */ | |
332 | if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) || | |
333 | (tpc >= MODULES_VADDR && tpc < MODULES_END)) { | |
334 | /* Valid, no problems... */ | |
335 | } else { | |
336 | bad_kernel_pc(regs); | |
337 | return; | |
338 | } | |
339 | } | |
340 | ||
341 | /* | |
342 | * If we're in an interrupt or have no user | |
343 | * context, we must not take the fault.. | |
344 | */ | |
345 | if (in_atomic() || !mm) | |
346 | goto intr_or_no_mm; | |
347 | ||
348 | if (test_thread_flag(TIF_32BIT)) { | |
349 | if (!(regs->tstate & TSTATE_PRIV)) | |
350 | regs->tpc &= 0xffffffff; | |
351 | address &= 0xffffffff; | |
352 | } | |
353 | ||
354 | if (!down_read_trylock(&mm->mmap_sem)) { | |
355 | if ((regs->tstate & TSTATE_PRIV) && | |
356 | !search_exception_tables(regs->tpc)) { | |
357 | insn = get_fault_insn(regs, insn); | |
358 | goto handle_kernel_fault; | |
359 | } | |
360 | down_read(&mm->mmap_sem); | |
361 | } | |
362 | ||
363 | vma = find_vma(mm, address); | |
364 | if (!vma) | |
365 | goto bad_area; | |
366 | ||
367 | /* Pure DTLB misses do not tell us whether the fault causing | |
368 | * load/store/atomic was a write or not, it only says that there | |
369 | * was no match. So in such a case we (carefully) read the | |
370 | * instruction to try and figure this out. It's an optimization | |
371 | * so it's ok if we can't do this. | |
372 | * | |
373 | * Special hack, window spill/fill knows the exact fault type. | |
374 | */ | |
375 | if (((fault_code & | |
376 | (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) && | |
377 | (vma->vm_flags & VM_WRITE) != 0) { | |
378 | insn = get_fault_insn(regs, 0); | |
379 | if (!insn) | |
380 | goto continue_fault; | |
381 | if ((insn & 0xc0200000) == 0xc0200000 && | |
382 | (insn & 0x1780000) != 0x1680000) { | |
383 | /* Don't bother updating thread struct value, | |
384 | * because update_mmu_cache only cares which tlb | |
385 | * the access came from. | |
386 | */ | |
387 | fault_code |= FAULT_CODE_WRITE; | |
388 | } | |
389 | } | |
390 | continue_fault: | |
391 | ||
392 | if (vma->vm_start <= address) | |
393 | goto good_area; | |
394 | if (!(vma->vm_flags & VM_GROWSDOWN)) | |
395 | goto bad_area; | |
396 | if (!(fault_code & FAULT_CODE_WRITE)) { | |
397 | /* Non-faulting loads shouldn't expand stack. */ | |
398 | insn = get_fault_insn(regs, insn); | |
399 | if ((insn & 0xc0800000) == 0xc0800000) { | |
400 | unsigned char asi; | |
401 | ||
402 | if (insn & 0x2000) | |
403 | asi = (regs->tstate >> 24); | |
404 | else | |
405 | asi = (insn >> 5); | |
406 | if ((asi & 0xf2) == 0x82) | |
407 | goto bad_area; | |
408 | } | |
409 | } | |
410 | if (expand_stack(vma, address)) | |
411 | goto bad_area; | |
412 | /* | |
413 | * Ok, we have a good vm_area for this memory access, so | |
414 | * we can handle it.. | |
415 | */ | |
416 | good_area: | |
417 | si_code = SEGV_ACCERR; | |
418 | ||
419 | /* If we took a ITLB miss on a non-executable page, catch | |
420 | * that here. | |
421 | */ | |
422 | if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) { | |
423 | BUG_ON(address != regs->tpc); | |
424 | BUG_ON(regs->tstate & TSTATE_PRIV); | |
425 | goto bad_area; | |
426 | } | |
427 | ||
428 | if (fault_code & FAULT_CODE_WRITE) { | |
429 | if (!(vma->vm_flags & VM_WRITE)) | |
430 | goto bad_area; | |
431 | ||
432 | /* Spitfire has an icache which does not snoop | |
433 | * processor stores. Later processors do... | |
434 | */ | |
435 | if (tlb_type == spitfire && | |
436 | (vma->vm_flags & VM_EXEC) != 0 && | |
437 | vma->vm_file != NULL) | |
438 | set_thread_fault_code(fault_code | | |
439 | FAULT_CODE_BLKCOMMIT); | |
440 | } else { | |
441 | /* Allow reads even for write-only mappings */ | |
442 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) | |
443 | goto bad_area; | |
444 | } | |
445 | ||
446 | switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) { | |
447 | case VM_FAULT_MINOR: | |
448 | current->min_flt++; | |
449 | break; | |
450 | case VM_FAULT_MAJOR: | |
451 | current->maj_flt++; | |
452 | break; | |
453 | case VM_FAULT_SIGBUS: | |
454 | goto do_sigbus; | |
455 | case VM_FAULT_OOM: | |
456 | goto out_of_memory; | |
457 | default: | |
458 | BUG(); | |
459 | } | |
460 | ||
461 | up_read(&mm->mmap_sem); | |
462 | goto fault_done; | |
463 | ||
464 | /* | |
465 | * Something tried to access memory that isn't in our memory map.. | |
466 | * Fix it, but check if it's kernel or user first.. | |
467 | */ | |
468 | bad_area: | |
469 | insn = get_fault_insn(regs, insn); | |
470 | up_read(&mm->mmap_sem); | |
471 | ||
472 | handle_kernel_fault: | |
473 | do_kernel_fault(regs, si_code, fault_code, insn, address); | |
474 | ||
475 | goto fault_done; | |
476 | ||
477 | /* | |
478 | * We ran out of memory, or some other thing happened to us that made | |
479 | * us unable to handle the page fault gracefully. | |
480 | */ | |
481 | out_of_memory: | |
482 | insn = get_fault_insn(regs, insn); | |
483 | up_read(&mm->mmap_sem); | |
484 | printk("VM: killing process %s\n", current->comm); | |
485 | if (!(regs->tstate & TSTATE_PRIV)) | |
486 | do_exit(SIGKILL); | |
487 | goto handle_kernel_fault; | |
488 | ||
489 | intr_or_no_mm: | |
490 | insn = get_fault_insn(regs, 0); | |
491 | goto handle_kernel_fault; | |
492 | ||
493 | do_sigbus: | |
494 | insn = get_fault_insn(regs, insn); | |
495 | up_read(&mm->mmap_sem); | |
496 | ||
497 | /* | |
498 | * Send a sigbus, regardless of whether we were in kernel | |
499 | * or user mode. | |
500 | */ | |
501 | do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code); | |
502 | ||
503 | /* Kernel mode? Handle exceptions or die */ | |
504 | if (regs->tstate & TSTATE_PRIV) | |
505 | goto handle_kernel_fault; | |
506 | ||
507 | fault_done: | |
508 | /* These values are no longer needed, clear them. */ | |
509 | set_thread_fault_code(0); | |
510 | current_thread_info()->fault_address = 0; | |
511 | } |