[SPARC64]: Fix and re-enable dynamic TSB sizing.
[deliverable/linux.git] / arch / sparc64 / mm / fault.c
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 #include <linux/kprobes.h>
22
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/openprom.h>
26 #include <asm/oplib.h>
27 #include <asm/uaccess.h>
28 #include <asm/asi.h>
29 #include <asm/lsu.h>
30 #include <asm/sections.h>
31 #include <asm/kdebug.h>
32 #include <asm/mmu_context.h>
33
34 /*
35 * To debug kernel to catch accesses to certain virtual/physical addresses.
36 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
37 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
38 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
39 * watched. This is only useful on a single cpu machine for now. After the watchpoint
40 * is detected, the process causing it will be killed, thus preventing an infinite loop.
41 */
42 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
43 {
44 unsigned long lsubits;
45
46 __asm__ __volatile__("ldxa [%%g0] %1, %0"
47 : "=r" (lsubits)
48 : "i" (ASI_LSU_CONTROL));
49 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
50 LSU_CONTROL_PR | LSU_CONTROL_VR |
51 LSU_CONTROL_PW | LSU_CONTROL_VW);
52
53 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
54 "membar #Sync"
55 : /* no outputs */
56 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
57 "i" (ASI_DMMU));
58
59 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
60 if (flags & VM_READ)
61 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
62 if (flags & VM_WRITE)
63 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
64 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
65 "membar #Sync"
66 : /* no outputs */
67 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
68 : "memory");
69 }
70
71 static void __kprobes unhandled_fault(unsigned long address,
72 struct task_struct *tsk,
73 struct pt_regs *regs)
74 {
75 if ((unsigned long) address < PAGE_SIZE) {
76 printk(KERN_ALERT "Unable to handle kernel NULL "
77 "pointer dereference\n");
78 } else {
79 printk(KERN_ALERT "Unable to handle kernel paging request "
80 "at virtual address %016lx\n", (unsigned long)address);
81 }
82 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
83 (tsk->mm ?
84 CTX_HWBITS(tsk->mm->context) :
85 CTX_HWBITS(tsk->active_mm->context)));
86 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
87 (tsk->mm ? (unsigned long) tsk->mm->pgd :
88 (unsigned long) tsk->active_mm->pgd));
89 if (notify_die(DIE_GPF, "general protection fault", regs,
90 0, 0, SIGSEGV) == NOTIFY_STOP)
91 return;
92 die_if_kernel("Oops", regs);
93 }
94
95 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
96 {
97 unsigned long *ksp;
98
99 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
100 regs->tpc);
101 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
102 __asm__("mov %%sp, %0" : "=r" (ksp));
103 show_stack(current, ksp);
104 unhandled_fault(regs->tpc, current, regs);
105 }
106
107 /*
108 * We now make sure that mmap_sem is held in all paths that call
109 * this. Additionally, to prevent kswapd from ripping ptes from
110 * under us, raise interrupts around the time that we look at the
111 * pte, kswapd will have to wait to get his smp ipi response from
112 * us. vmtruncate likewise. This saves us having to get pte lock.
113 */
114 static unsigned int get_user_insn(unsigned long tpc)
115 {
116 pgd_t *pgdp = pgd_offset(current->mm, tpc);
117 pud_t *pudp;
118 pmd_t *pmdp;
119 pte_t *ptep, pte;
120 unsigned long pa;
121 u32 insn = 0;
122 unsigned long pstate;
123
124 if (pgd_none(*pgdp))
125 goto outret;
126 pudp = pud_offset(pgdp, tpc);
127 if (pud_none(*pudp))
128 goto outret;
129 pmdp = pmd_offset(pudp, tpc);
130 if (pmd_none(*pmdp))
131 goto outret;
132
133 /* This disables preemption for us as well. */
134 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
135 __asm__ __volatile__("wrpr %0, %1, %%pstate"
136 : : "r" (pstate), "i" (PSTATE_IE));
137 ptep = pte_offset_map(pmdp, tpc);
138 pte = *ptep;
139 if (!pte_present(pte))
140 goto out;
141
142 pa = (pte_pfn(pte) << PAGE_SHIFT);
143 pa += (tpc & ~PAGE_MASK);
144
145 /* Use phys bypass so we don't pollute dtlb/dcache. */
146 __asm__ __volatile__("lduwa [%1] %2, %0"
147 : "=r" (insn)
148 : "r" (pa), "i" (ASI_PHYS_USE_EC));
149
150 out:
151 pte_unmap(ptep);
152 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
153 outret:
154 return insn;
155 }
156
157 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
158
159 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
160 unsigned int insn, int fault_code)
161 {
162 siginfo_t info;
163
164 info.si_code = code;
165 info.si_signo = sig;
166 info.si_errno = 0;
167 if (fault_code & FAULT_CODE_ITLB)
168 info.si_addr = (void __user *) regs->tpc;
169 else
170 info.si_addr = (void __user *)
171 compute_effective_address(regs, insn, 0);
172 info.si_trapno = 0;
173 force_sig_info(sig, &info, current);
174 }
175
176 extern int handle_ldf_stq(u32, struct pt_regs *);
177 extern int handle_ld_nf(u32, struct pt_regs *);
178
179 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
180 {
181 if (!insn) {
182 if (!regs->tpc || (regs->tpc & 0x3))
183 return 0;
184 if (regs->tstate & TSTATE_PRIV) {
185 insn = *(unsigned int *) regs->tpc;
186 } else {
187 insn = get_user_insn(regs->tpc);
188 }
189 }
190 return insn;
191 }
192
193 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
194 unsigned int insn, unsigned long address)
195 {
196 unsigned char asi = ASI_P;
197
198 if ((!insn) && (regs->tstate & TSTATE_PRIV))
199 goto cannot_handle;
200
201 /* If user insn could be read (thus insn is zero), that
202 * is fine. We will just gun down the process with a signal
203 * in that case.
204 */
205
206 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
207 (insn & 0xc0800000) == 0xc0800000) {
208 if (insn & 0x2000)
209 asi = (regs->tstate >> 24);
210 else
211 asi = (insn >> 5);
212 if ((asi & 0xf2) == 0x82) {
213 if (insn & 0x1000000) {
214 handle_ldf_stq(insn, regs);
215 } else {
216 /* This was a non-faulting load. Just clear the
217 * destination register(s) and continue with the next
218 * instruction. -jj
219 */
220 handle_ld_nf(insn, regs);
221 }
222 return;
223 }
224 }
225
226 /* Is this in ex_table? */
227 if (regs->tstate & TSTATE_PRIV) {
228 const struct exception_table_entry *entry;
229
230 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
231 if (insn & 0x2000)
232 asi = (regs->tstate >> 24);
233 else
234 asi = (insn >> 5);
235 }
236
237 /* Look in asi.h: All _S asis have LS bit set */
238 if ((asi & 0x1) &&
239 (entry = search_exception_tables(regs->tpc))) {
240 regs->tpc = entry->fixup;
241 regs->tnpc = regs->tpc + 4;
242 return;
243 }
244 } else {
245 /* The si_code was set to make clear whether
246 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
247 */
248 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
249 return;
250 }
251
252 cannot_handle:
253 unhandled_fault (address, current, regs);
254 }
255
256 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
257 {
258 struct mm_struct *mm = current->mm;
259 struct vm_area_struct *vma;
260 unsigned int insn = 0;
261 int si_code, fault_code;
262 unsigned long address, mm_rss;
263
264 fault_code = get_thread_fault_code();
265
266 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
267 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
268 return;
269
270 si_code = SEGV_MAPERR;
271 address = current_thread_info()->fault_address;
272
273 if ((fault_code & FAULT_CODE_ITLB) &&
274 (fault_code & FAULT_CODE_DTLB))
275 BUG();
276
277 if (regs->tstate & TSTATE_PRIV) {
278 unsigned long tpc = regs->tpc;
279
280 /* Sanity check the PC. */
281 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
282 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
283 /* Valid, no problems... */
284 } else {
285 bad_kernel_pc(regs, address);
286 return;
287 }
288 }
289
290 /*
291 * If we're in an interrupt or have no user
292 * context, we must not take the fault..
293 */
294 if (in_atomic() || !mm)
295 goto intr_or_no_mm;
296
297 if (test_thread_flag(TIF_32BIT)) {
298 if (!(regs->tstate & TSTATE_PRIV))
299 regs->tpc &= 0xffffffff;
300 address &= 0xffffffff;
301 }
302
303 if (!down_read_trylock(&mm->mmap_sem)) {
304 if ((regs->tstate & TSTATE_PRIV) &&
305 !search_exception_tables(regs->tpc)) {
306 insn = get_fault_insn(regs, insn);
307 goto handle_kernel_fault;
308 }
309 down_read(&mm->mmap_sem);
310 }
311
312 vma = find_vma(mm, address);
313 if (!vma)
314 goto bad_area;
315
316 /* Pure DTLB misses do not tell us whether the fault causing
317 * load/store/atomic was a write or not, it only says that there
318 * was no match. So in such a case we (carefully) read the
319 * instruction to try and figure this out. It's an optimization
320 * so it's ok if we can't do this.
321 *
322 * Special hack, window spill/fill knows the exact fault type.
323 */
324 if (((fault_code &
325 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
326 (vma->vm_flags & VM_WRITE) != 0) {
327 insn = get_fault_insn(regs, 0);
328 if (!insn)
329 goto continue_fault;
330 if ((insn & 0xc0200000) == 0xc0200000 &&
331 (insn & 0x1780000) != 0x1680000) {
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 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
396 case VM_FAULT_MINOR:
397 current->min_flt++;
398 break;
399 case VM_FAULT_MAJOR:
400 current->maj_flt++;
401 break;
402 case VM_FAULT_SIGBUS:
403 goto do_sigbus;
404 case VM_FAULT_OOM:
405 goto out_of_memory;
406 default:
407 BUG();
408 }
409
410 up_read(&mm->mmap_sem);
411
412 mm_rss = get_mm_rss(mm);
413 if (unlikely(mm_rss >= mm->context.tsb_rss_limit))
414 tsb_grow(mm, mm_rss);
415
416 return;
417
418 /*
419 * Something tried to access memory that isn't in our memory map..
420 * Fix it, but check if it's kernel or user first..
421 */
422 bad_area:
423 insn = get_fault_insn(regs, insn);
424 up_read(&mm->mmap_sem);
425
426 handle_kernel_fault:
427 do_kernel_fault(regs, si_code, fault_code, insn, address);
428 return;
429
430 /*
431 * We ran out of memory, or some other thing happened to us that made
432 * us unable to handle the page fault gracefully.
433 */
434 out_of_memory:
435 insn = get_fault_insn(regs, insn);
436 up_read(&mm->mmap_sem);
437 printk("VM: killing process %s\n", current->comm);
438 if (!(regs->tstate & TSTATE_PRIV))
439 do_exit(SIGKILL);
440 goto handle_kernel_fault;
441
442 intr_or_no_mm:
443 insn = get_fault_insn(regs, 0);
444 goto handle_kernel_fault;
445
446 do_sigbus:
447 insn = get_fault_insn(regs, insn);
448 up_read(&mm->mmap_sem);
449
450 /*
451 * Send a sigbus, regardless of whether we were in kernel
452 * or user mode.
453 */
454 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
455
456 /* Kernel mode? Handle exceptions or die */
457 if (regs->tstate & TSTATE_PRIV)
458 goto handle_kernel_fault;
459 }
This page took 0.042463 seconds and 5 git commands to generate.