blob: 1785cd82253caec869abbb7f5d7804b42effad04 [file] [log] [blame]
Heiko Carstens5bdc9b42006-07-03 00:24:41 -07001/*
Heiko Carstens5bdc9b42006-07-03 00:24:41 -07002 * Stack trace management functions
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2006
Heiko Carstens5bdc9b42006-07-03 00:24:41 -07005 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
6 */
7
8#include <linux/sched.h>
9#include <linux/stacktrace.h>
10#include <linux/kallsyms.h>
Heiko Carstens8de2ce82008-07-17 17:16:45 +020011#include <linux/module.h>
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070012
Heiko Carstens4d284ca2007-02-05 21:18:53 +010013static unsigned long save_context_stack(struct stack_trace *trace,
Heiko Carstens4d284ca2007-02-05 21:18:53 +010014 unsigned long sp,
15 unsigned long low,
Heiko Carstensa3afe702008-02-05 16:50:45 +010016 unsigned long high,
17 int savesched)
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070018{
19 struct stack_frame *sf;
20 struct pt_regs *regs;
21 unsigned long addr;
22
23 while(1) {
24 sp &= PSW_ADDR_INSN;
25 if (sp < low || sp > high)
26 return sp;
27 sf = (struct stack_frame *)sp;
28 while(1) {
29 addr = sf->gprs[8] & PSW_ADDR_INSN;
Heiko Carstense90a2852007-07-17 13:36:07 +020030 if (!trace->skip)
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070031 trace->entries[trace->nr_entries++] = addr;
32 else
Heiko Carstense90a2852007-07-17 13:36:07 +020033 trace->skip--;
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070034 if (trace->nr_entries >= trace->max_entries)
35 return sp;
36 low = sp;
37 sp = sf->back_chain & PSW_ADDR_INSN;
38 if (!sp)
39 break;
40 if (sp <= low || sp > high - sizeof(*sf))
41 return sp;
42 sf = (struct stack_frame *)sp;
43 }
44 /* Zero backchain detected, check for interrupt frame. */
45 sp = (unsigned long)(sf + 1);
46 if (sp <= low || sp > high - sizeof(*regs))
47 return sp;
48 regs = (struct pt_regs *)sp;
49 addr = regs->psw.addr & PSW_ADDR_INSN;
Heiko Carstensa3afe702008-02-05 16:50:45 +010050 if (savesched || !in_sched_functions(addr)) {
51 if (!trace->skip)
52 trace->entries[trace->nr_entries++] = addr;
53 else
54 trace->skip--;
55 }
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070056 if (trace->nr_entries >= trace->max_entries)
57 return sp;
58 low = sp;
59 sp = regs->gprs[15];
60 }
61}
62
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070063void save_stack_trace(struct stack_trace *trace)
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070064{
65 register unsigned long sp asm ("15");
Christian Borntraeger75e9de12006-10-11 15:31:52 +020066 unsigned long orig_sp, new_sp;
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070067
Christian Borntraeger75e9de12006-10-11 15:31:52 +020068 orig_sp = sp & PSW_ADDR_INSN;
Heiko Carstense90a2852007-07-17 13:36:07 +020069 new_sp = save_context_stack(trace, orig_sp,
70 S390_lowcore.panic_stack - PAGE_SIZE,
Heiko Carstensa3afe702008-02-05 16:50:45 +010071 S390_lowcore.panic_stack, 1);
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070072 if (new_sp != orig_sp)
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070073 return;
Heiko Carstense90a2852007-07-17 13:36:07 +020074 new_sp = save_context_stack(trace, new_sp,
75 S390_lowcore.async_stack - ASYNC_SIZE,
Heiko Carstensa3afe702008-02-05 16:50:45 +010076 S390_lowcore.async_stack, 1);
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070077 if (new_sp != orig_sp)
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070078 return;
Heiko Carstense90a2852007-07-17 13:36:07 +020079 save_context_stack(trace, new_sp,
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070080 S390_lowcore.thread_info,
Heiko Carstensa3afe702008-02-05 16:50:45 +010081 S390_lowcore.thread_info + THREAD_SIZE, 1);
82}
Ingo Molnar7b4c9502008-07-03 09:17:55 +020083EXPORT_SYMBOL_GPL(save_stack_trace);
Heiko Carstensa3afe702008-02-05 16:50:45 +010084
85void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
86{
87 unsigned long sp, low, high;
88
89 sp = tsk->thread.ksp & PSW_ADDR_INSN;
90 low = (unsigned long) task_stack_page(tsk);
91 high = (unsigned long) task_pt_regs(tsk);
92 save_context_stack(trace, sp, low, high, 0);
93 if (trace->nr_entries < trace->max_entries)
94 trace->entries[trace->nr_entries++] = ULONG_MAX;
Heiko Carstens5bdc9b42006-07-03 00:24:41 -070095}
Ingo Molnar7b4c9502008-07-03 09:17:55 +020096EXPORT_SYMBOL_GPL(save_stack_trace_tsk);