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