]>
Commit | Line | Data |
---|---|---|
867e359b CM |
1 | /* |
2 | * Copyright 2010 Tilera Corporation. All Rights Reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation, version 2. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
11 | * NON INFRINGEMENT. See the GNU General Public License for | |
12 | * more details. | |
13 | */ | |
14 | ||
15 | #include <linux/sched.h> | |
16 | #include <linux/kernel.h> | |
17 | #include <linux/kprobes.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/pfn.h> | |
20 | #include <linux/kallsyms.h> | |
21 | #include <linux/stacktrace.h> | |
22 | #include <linux/uaccess.h> | |
23 | #include <linux/mmzone.h> | |
24 | #include <asm/backtrace.h> | |
25 | #include <asm/page.h> | |
26 | #include <asm/tlbflush.h> | |
27 | #include <asm/ucontext.h> | |
28 | #include <asm/sigframe.h> | |
29 | #include <asm/stack.h> | |
30 | #include <arch/abi.h> | |
31 | #include <arch/interrupts.h> | |
32 | ||
33 | ||
34 | /* Is address on the specified kernel stack? */ | |
35 | static int in_kernel_stack(struct KBacktraceIterator *kbt, VirtualAddress sp) | |
36 | { | |
37 | ulong kstack_base = (ulong) kbt->task->stack; | |
38 | if (kstack_base == 0) /* corrupt task pointer; just follow stack... */ | |
39 | return sp >= PAGE_OFFSET && sp < (unsigned long)high_memory; | |
40 | return sp >= kstack_base && sp < kstack_base + THREAD_SIZE; | |
41 | } | |
42 | ||
43 | /* Is address in the specified kernel code? */ | |
44 | static int in_kernel_text(VirtualAddress address) | |
45 | { | |
46 | return (address >= MEM_SV_INTRPT && | |
47 | address < MEM_SV_INTRPT + HPAGE_SIZE); | |
48 | } | |
49 | ||
50 | /* Is address valid for reading? */ | |
51 | static int valid_address(struct KBacktraceIterator *kbt, VirtualAddress address) | |
52 | { | |
53 | HV_PTE *l1_pgtable = kbt->pgtable; | |
54 | HV_PTE *l2_pgtable; | |
55 | unsigned long pfn; | |
56 | HV_PTE pte; | |
57 | struct page *page; | |
58 | ||
0707ad30 CM |
59 | if (l1_pgtable == NULL) |
60 | return 0; /* can't read user space in other tasks */ | |
61 | ||
867e359b CM |
62 | pte = l1_pgtable[HV_L1_INDEX(address)]; |
63 | if (!hv_pte_get_present(pte)) | |
64 | return 0; | |
65 | pfn = hv_pte_get_pfn(pte); | |
66 | if (pte_huge(pte)) { | |
67 | if (!pfn_valid(pfn)) { | |
0707ad30 | 68 | pr_err("huge page has bad pfn %#lx\n", pfn); |
867e359b CM |
69 | return 0; |
70 | } | |
71 | return hv_pte_get_present(pte) && hv_pte_get_readable(pte); | |
72 | } | |
73 | ||
74 | page = pfn_to_page(pfn); | |
75 | if (PageHighMem(page)) { | |
0707ad30 | 76 | pr_err("L2 page table not in LOWMEM (%#llx)\n", |
867e359b CM |
77 | HV_PFN_TO_CPA(pfn)); |
78 | return 0; | |
79 | } | |
80 | l2_pgtable = (HV_PTE *)pfn_to_kaddr(pfn); | |
81 | pte = l2_pgtable[HV_L2_INDEX(address)]; | |
82 | return hv_pte_get_present(pte) && hv_pte_get_readable(pte); | |
83 | } | |
84 | ||
85 | /* Callback for backtracer; basically a glorified memcpy */ | |
86 | static bool read_memory_func(void *result, VirtualAddress address, | |
87 | unsigned int size, void *vkbt) | |
88 | { | |
89 | int retval; | |
90 | struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt; | |
91 | if (in_kernel_text(address)) { | |
92 | /* OK to read kernel code. */ | |
93 | } else if (address >= PAGE_OFFSET) { | |
94 | /* We only tolerate kernel-space reads of this task's stack */ | |
95 | if (!in_kernel_stack(kbt, address)) | |
96 | return 0; | |
867e359b CM |
97 | } else if (!valid_address(kbt, address)) { |
98 | return 0; /* invalid user-space address */ | |
99 | } | |
100 | pagefault_disable(); | |
0707ad30 CM |
101 | retval = __copy_from_user_inatomic(result, |
102 | (void __user __force *)address, | |
867e359b CM |
103 | size); |
104 | pagefault_enable(); | |
105 | return (retval == 0); | |
106 | } | |
107 | ||
108 | /* Return a pt_regs pointer for a valid fault handler frame */ | |
109 | static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt) | |
110 | { | |
867e359b CM |
111 | const char *fault = NULL; /* happy compiler */ |
112 | char fault_buf[64]; | |
113 | VirtualAddress sp = kbt->it.sp; | |
114 | struct pt_regs *p; | |
115 | ||
116 | if (!in_kernel_stack(kbt, sp)) | |
117 | return NULL; | |
118 | if (!in_kernel_stack(kbt, sp + C_ABI_SAVE_AREA_SIZE + PTREGS_SIZE-1)) | |
119 | return NULL; | |
120 | p = (struct pt_regs *)(sp + C_ABI_SAVE_AREA_SIZE); | |
121 | if (p->faultnum == INT_SWINT_1 || p->faultnum == INT_SWINT_1_SIGRETURN) | |
122 | fault = "syscall"; | |
123 | else { | |
124 | if (kbt->verbose) { /* else we aren't going to use it */ | |
125 | snprintf(fault_buf, sizeof(fault_buf), | |
126 | "interrupt %ld", p->faultnum); | |
127 | fault = fault_buf; | |
128 | } | |
129 | } | |
130 | if (EX1_PL(p->ex1) == KERNEL_PL && | |
131 | in_kernel_text(p->pc) && | |
132 | in_kernel_stack(kbt, p->sp) && | |
133 | p->sp >= sp) { | |
134 | if (kbt->verbose) | |
0707ad30 | 135 | pr_err(" <%s while in kernel mode>\n", fault); |
867e359b CM |
136 | } else if (EX1_PL(p->ex1) == USER_PL && |
137 | p->pc < PAGE_OFFSET && | |
138 | p->sp < PAGE_OFFSET) { | |
139 | if (kbt->verbose) | |
0707ad30 | 140 | pr_err(" <%s while in user mode>\n", fault); |
867e359b | 141 | } else if (kbt->verbose) { |
0707ad30 | 142 | pr_err(" (odd fault: pc %#lx, sp %#lx, ex1 %#lx?)\n", |
867e359b CM |
143 | p->pc, p->sp, p->ex1); |
144 | p = NULL; | |
145 | } | |
146 | if (!kbt->profile || (INT_MASK(p->faultnum) & QUEUED_INTERRUPTS) == 0) | |
147 | return p; | |
867e359b CM |
148 | return NULL; |
149 | } | |
150 | ||
151 | /* Is the pc pointing to a sigreturn trampoline? */ | |
152 | static int is_sigreturn(VirtualAddress pc) | |
153 | { | |
154 | return (pc == VDSO_BASE); | |
155 | } | |
156 | ||
157 | /* Return a pt_regs pointer for a valid signal handler frame */ | |
158 | static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt) | |
159 | { | |
160 | BacktraceIterator *b = &kbt->it; | |
161 | ||
162 | if (b->pc == VDSO_BASE) { | |
163 | struct rt_sigframe *frame; | |
164 | unsigned long sigframe_top = | |
165 | b->sp + sizeof(struct rt_sigframe) - 1; | |
166 | if (!valid_address(kbt, b->sp) || | |
167 | !valid_address(kbt, sigframe_top)) { | |
168 | if (kbt->verbose) | |
0707ad30 | 169 | pr_err(" (odd signal: sp %#lx?)\n", |
867e359b CM |
170 | (unsigned long)(b->sp)); |
171 | return NULL; | |
172 | } | |
173 | frame = (struct rt_sigframe *)b->sp; | |
174 | if (kbt->verbose) { | |
0707ad30 | 175 | pr_err(" <received signal %d>\n", |
867e359b CM |
176 | frame->info.si_signo); |
177 | } | |
74fca9da | 178 | return (struct pt_regs *)&frame->uc.uc_mcontext; |
867e359b CM |
179 | } |
180 | return NULL; | |
181 | } | |
182 | ||
0707ad30 | 183 | static int KBacktraceIterator_is_sigreturn(struct KBacktraceIterator *kbt) |
867e359b CM |
184 | { |
185 | return is_sigreturn(kbt->it.pc); | |
186 | } | |
187 | ||
188 | static int KBacktraceIterator_restart(struct KBacktraceIterator *kbt) | |
189 | { | |
190 | struct pt_regs *p; | |
191 | ||
192 | p = valid_fault_handler(kbt); | |
193 | if (p == NULL) | |
194 | p = valid_sigframe(kbt); | |
195 | if (p == NULL) | |
196 | return 0; | |
197 | backtrace_init(&kbt->it, read_memory_func, kbt, | |
198 | p->pc, p->lr, p->sp, p->regs[52]); | |
199 | kbt->new_context = 1; | |
200 | return 1; | |
201 | } | |
202 | ||
203 | /* Find a frame that isn't a sigreturn, if there is one. */ | |
204 | static int KBacktraceIterator_next_item_inclusive( | |
205 | struct KBacktraceIterator *kbt) | |
206 | { | |
207 | for (;;) { | |
208 | do { | |
209 | if (!KBacktraceIterator_is_sigreturn(kbt)) | |
210 | return 1; | |
211 | } while (backtrace_next(&kbt->it)); | |
212 | ||
213 | if (!KBacktraceIterator_restart(kbt)) | |
214 | return 0; | |
215 | } | |
216 | } | |
217 | ||
218 | /* | |
219 | * If the current sp is on a page different than what we recorded | |
220 | * as the top-of-kernel-stack last time we context switched, we have | |
221 | * probably blown the stack, and nothing is going to work out well. | |
222 | * If we can at least get out a warning, that may help the debug, | |
223 | * though we probably won't be able to backtrace into the code that | |
224 | * actually did the recursive damage. | |
225 | */ | |
226 | static void validate_stack(struct pt_regs *regs) | |
227 | { | |
228 | int cpu = smp_processor_id(); | |
229 | unsigned long ksp0 = get_current_ksp0(); | |
230 | unsigned long ksp0_base = ksp0 - THREAD_SIZE; | |
231 | unsigned long sp = stack_pointer; | |
232 | ||
233 | if (EX1_PL(regs->ex1) == KERNEL_PL && regs->sp >= ksp0) { | |
0707ad30 | 234 | pr_err("WARNING: cpu %d: kernel stack page %#lx underrun!\n" |
867e359b CM |
235 | " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n", |
236 | cpu, ksp0_base, sp, regs->sp, regs->pc, regs->lr); | |
237 | } | |
238 | ||
239 | else if (sp < ksp0_base + sizeof(struct thread_info)) { | |
0707ad30 | 240 | pr_err("WARNING: cpu %d: kernel stack page %#lx overrun!\n" |
867e359b CM |
241 | " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n", |
242 | cpu, ksp0_base, sp, regs->sp, regs->pc, regs->lr); | |
243 | } | |
244 | } | |
245 | ||
246 | void KBacktraceIterator_init(struct KBacktraceIterator *kbt, | |
247 | struct task_struct *t, struct pt_regs *regs) | |
248 | { | |
249 | VirtualAddress pc, lr, sp, r52; | |
250 | int is_current; | |
251 | ||
252 | /* | |
253 | * Set up callback information. We grab the kernel stack base | |
254 | * so we will allow reads of that address range, and if we're | |
255 | * asking about the current process we grab the page table | |
256 | * so we can check user accesses before trying to read them. | |
257 | * We flush the TLB to avoid any weird skew issues. | |
258 | */ | |
259 | is_current = (t == NULL); | |
260 | kbt->is_current = is_current; | |
261 | if (is_current) | |
262 | t = validate_current(); | |
263 | kbt->task = t; | |
264 | kbt->pgtable = NULL; | |
265 | kbt->verbose = 0; /* override in caller if desired */ | |
266 | kbt->profile = 0; /* override in caller if desired */ | |
267 | kbt->end = 0; | |
268 | kbt->new_context = 0; | |
269 | if (is_current) { | |
270 | HV_PhysAddr pgdir_pa = hv_inquire_context().page_table; | |
271 | if (pgdir_pa == (unsigned long)swapper_pg_dir - PAGE_OFFSET) { | |
272 | /* | |
273 | * Not just an optimization: this also allows | |
274 | * this to work at all before va/pa mappings | |
275 | * are set up. | |
276 | */ | |
277 | kbt->pgtable = swapper_pg_dir; | |
278 | } else { | |
279 | struct page *page = pfn_to_page(PFN_DOWN(pgdir_pa)); | |
280 | if (!PageHighMem(page)) | |
281 | kbt->pgtable = __va(pgdir_pa); | |
282 | else | |
0707ad30 | 283 | pr_err("page table not in LOWMEM" |
867e359b CM |
284 | " (%#llx)\n", pgdir_pa); |
285 | } | |
286 | local_flush_tlb_all(); | |
287 | validate_stack(regs); | |
288 | } | |
289 | ||
290 | if (regs == NULL) { | |
867e359b CM |
291 | if (is_current || t->state == TASK_RUNNING) { |
292 | /* Can't do this; we need registers */ | |
293 | kbt->end = 1; | |
294 | return; | |
295 | } | |
0707ad30 | 296 | pc = get_switch_to_pc(); |
867e359b CM |
297 | lr = t->thread.pc; |
298 | sp = t->thread.ksp; | |
299 | r52 = 0; | |
300 | } else { | |
301 | pc = regs->pc; | |
302 | lr = regs->lr; | |
303 | sp = regs->sp; | |
304 | r52 = regs->regs[52]; | |
305 | } | |
306 | ||
307 | backtrace_init(&kbt->it, read_memory_func, kbt, pc, lr, sp, r52); | |
308 | kbt->end = !KBacktraceIterator_next_item_inclusive(kbt); | |
309 | } | |
310 | EXPORT_SYMBOL(KBacktraceIterator_init); | |
311 | ||
312 | int KBacktraceIterator_end(struct KBacktraceIterator *kbt) | |
313 | { | |
314 | return kbt->end; | |
315 | } | |
316 | EXPORT_SYMBOL(KBacktraceIterator_end); | |
317 | ||
318 | void KBacktraceIterator_next(struct KBacktraceIterator *kbt) | |
319 | { | |
320 | kbt->new_context = 0; | |
321 | if (!backtrace_next(&kbt->it) && | |
322 | !KBacktraceIterator_restart(kbt)) { | |
323 | kbt->end = 1; | |
324 | return; | |
325 | } | |
326 | ||
327 | kbt->end = !KBacktraceIterator_next_item_inclusive(kbt); | |
328 | } | |
329 | EXPORT_SYMBOL(KBacktraceIterator_next); | |
330 | ||
331 | /* | |
332 | * This method wraps the backtracer's more generic support. | |
333 | * It is only invoked from the architecture-specific code; show_stack() | |
334 | * and dump_stack() (in entry.S) are architecture-independent entry points. | |
335 | */ | |
336 | void tile_show_stack(struct KBacktraceIterator *kbt, int headers) | |
337 | { | |
338 | int i; | |
339 | ||
340 | if (headers) { | |
341 | /* | |
342 | * Add a blank line since if we are called from panic(), | |
343 | * then bust_spinlocks() spit out a space in front of us | |
344 | * and it will mess up our KERN_ERR. | |
345 | */ | |
0707ad30 CM |
346 | pr_err("\n"); |
347 | pr_err("Starting stack dump of tid %d, pid %d (%s)" | |
867e359b CM |
348 | " on cpu %d at cycle %lld\n", |
349 | kbt->task->pid, kbt->task->tgid, kbt->task->comm, | |
350 | smp_processor_id(), get_cycles()); | |
351 | } | |
867e359b CM |
352 | kbt->verbose = 1; |
353 | i = 0; | |
354 | for (; !KBacktraceIterator_end(kbt); KBacktraceIterator_next(kbt)) { | |
355 | char *modname; | |
356 | const char *name; | |
357 | unsigned long address = kbt->it.pc; | |
358 | unsigned long offset, size; | |
359 | char namebuf[KSYM_NAME_LEN+100]; | |
360 | ||
361 | if (address >= PAGE_OFFSET) | |
362 | name = kallsyms_lookup(address, &size, &offset, | |
363 | &modname, namebuf); | |
364 | else | |
365 | name = NULL; | |
366 | ||
367 | if (!name) | |
368 | namebuf[0] = '\0'; | |
369 | else { | |
370 | size_t namelen = strlen(namebuf); | |
371 | size_t remaining = (sizeof(namebuf) - 1) - namelen; | |
372 | char *p = namebuf + namelen; | |
373 | int rc = snprintf(p, remaining, "+%#lx/%#lx ", | |
374 | offset, size); | |
375 | if (modname && rc < remaining) | |
376 | snprintf(p + rc, remaining - rc, | |
377 | "[%s] ", modname); | |
378 | namebuf[sizeof(namebuf)-1] = '\0'; | |
379 | } | |
380 | ||
0707ad30 | 381 | pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n", |
867e359b CM |
382 | i++, address, namebuf, (unsigned long)(kbt->it.sp)); |
383 | ||
384 | if (i >= 100) { | |
0707ad30 | 385 | pr_err("Stack dump truncated" |
867e359b CM |
386 | " (%d frames)\n", i); |
387 | break; | |
388 | } | |
389 | } | |
390 | if (headers) | |
0707ad30 | 391 | pr_err("Stack dump complete\n"); |
867e359b CM |
392 | } |
393 | EXPORT_SYMBOL(tile_show_stack); | |
394 | ||
395 | ||
396 | /* This is called from show_regs() and _dump_stack() */ | |
397 | void dump_stack_regs(struct pt_regs *regs) | |
398 | { | |
399 | struct KBacktraceIterator kbt; | |
400 | KBacktraceIterator_init(&kbt, NULL, regs); | |
401 | tile_show_stack(&kbt, 1); | |
402 | } | |
403 | EXPORT_SYMBOL(dump_stack_regs); | |
404 | ||
405 | static struct pt_regs *regs_to_pt_regs(struct pt_regs *regs, | |
406 | ulong pc, ulong lr, ulong sp, ulong r52) | |
407 | { | |
408 | memset(regs, 0, sizeof(struct pt_regs)); | |
409 | regs->pc = pc; | |
410 | regs->lr = lr; | |
411 | regs->sp = sp; | |
412 | regs->regs[52] = r52; | |
413 | return regs; | |
414 | } | |
415 | ||
416 | /* This is called from dump_stack() and just converts to pt_regs */ | |
417 | void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52) | |
418 | { | |
419 | struct pt_regs regs; | |
420 | dump_stack_regs(regs_to_pt_regs(®s, pc, lr, sp, r52)); | |
421 | } | |
422 | ||
423 | /* This is called from KBacktraceIterator_init_current() */ | |
424 | void _KBacktraceIterator_init_current(struct KBacktraceIterator *kbt, ulong pc, | |
425 | ulong lr, ulong sp, ulong r52) | |
426 | { | |
427 | struct pt_regs regs; | |
428 | KBacktraceIterator_init(kbt, NULL, | |
429 | regs_to_pt_regs(®s, pc, lr, sp, r52)); | |
430 | } | |
431 | ||
432 | /* This is called only from kernel/sched.c, with esp == NULL */ | |
433 | void show_stack(struct task_struct *task, unsigned long *esp) | |
434 | { | |
435 | struct KBacktraceIterator kbt; | |
436 | if (task == NULL || task == current) | |
437 | KBacktraceIterator_init_current(&kbt); | |
438 | else | |
439 | KBacktraceIterator_init(&kbt, task, NULL); | |
440 | tile_show_stack(&kbt, 0); | |
441 | } | |
442 | ||
443 | #ifdef CONFIG_STACKTRACE | |
444 | ||
445 | /* Support generic Linux stack API too */ | |
446 | ||
447 | void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace) | |
448 | { | |
449 | struct KBacktraceIterator kbt; | |
450 | int skip = trace->skip; | |
451 | int i = 0; | |
452 | ||
453 | if (task == NULL || task == current) | |
454 | KBacktraceIterator_init_current(&kbt); | |
455 | else | |
456 | KBacktraceIterator_init(&kbt, task, NULL); | |
457 | for (; !KBacktraceIterator_end(&kbt); KBacktraceIterator_next(&kbt)) { | |
458 | if (skip) { | |
459 | --skip; | |
460 | continue; | |
461 | } | |
462 | if (i >= trace->max_entries || kbt.it.pc < PAGE_OFFSET) | |
463 | break; | |
464 | trace->entries[i++] = kbt.it.pc; | |
465 | } | |
466 | trace->nr_entries = i; | |
467 | } | |
468 | EXPORT_SYMBOL(save_stack_trace_tsk); | |
469 | ||
470 | void save_stack_trace(struct stack_trace *trace) | |
471 | { | |
472 | save_stack_trace_tsk(NULL, trace); | |
473 | } | |
474 | ||
475 | #endif | |
476 | ||
477 | /* In entry.S */ | |
478 | EXPORT_SYMBOL(KBacktraceIterator_init_current); |