2 * Kernel unwinding support
4 * (c) 2002-2004 Randolph Chung <tausq@debian.org>
6 * Derived partially from the IA64 implementation. The PA-RISC
7 * Runtime Architecture Document is also a useful reference to
8 * understand what is happening here
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/kallsyms.h>
17 #include <asm/uaccess.h>
18 #include <asm/assembly.h>
20 #include <asm/unwind.h>
24 #define dbg(x...) printk(x)
29 extern struct unwind_table_entry __start___unwind[];
30 extern struct unwind_table_entry __stop___unwind[];
32 static spinlock_t unwind_lock;
34 * the kernel unwind block is not dynamically allocated so that
35 * we can call unwind_init as early in the bootup process as
36 * possible (before the slab allocator is initialized)
38 static struct unwind_table kernel_unwind_table __read_mostly;
39 static LIST_HEAD(unwind_tables);
41 static inline const struct unwind_table_entry *
42 find_unwind_entry_in_table(const struct unwind_table *table, unsigned long addr)
44 const struct unwind_table_entry *e = NULL;
45 unsigned long lo, hi, mid;
48 hi = table->length - 1;
51 mid = (hi - lo) / 2 + lo;
52 e = &table->table[mid];
53 if (addr < e->region_start)
55 else if (addr > e->region_end)
64 static const struct unwind_table_entry *
65 find_unwind_entry(unsigned long addr)
67 struct unwind_table *table;
68 const struct unwind_table_entry *e = NULL;
70 if (addr >= kernel_unwind_table.start &&
71 addr <= kernel_unwind_table.end)
72 e = find_unwind_entry_in_table(&kernel_unwind_table, addr);
74 list_for_each_entry(table, &unwind_tables, list) {
75 if (addr >= table->start &&
77 e = find_unwind_entry_in_table(table, addr);
86 unwind_table_init(struct unwind_table *table, const char *name,
87 unsigned long base_addr, unsigned long gp,
88 void *table_start, void *table_end)
90 struct unwind_table_entry *start = table_start;
91 struct unwind_table_entry *end =
92 (struct unwind_table_entry *)table_end - 1;
95 table->base_addr = base_addr;
97 table->start = base_addr + start->region_start;
98 table->end = base_addr + end->region_end;
99 table->table = (struct unwind_table_entry *)table_start;
100 table->length = end - start + 1;
101 INIT_LIST_HEAD(&table->list);
103 for (; start <= end; start++) {
105 start->region_end > (start+1)->region_start) {
106 printk("WARNING: Out of order unwind entry! %p and %p\n", start, start+1);
109 start->region_start += base_addr;
110 start->region_end += base_addr;
115 unwind_table_sort(struct unwind_table_entry *start,
116 struct unwind_table_entry *finish)
118 struct unwind_table_entry el, *p, *q;
120 for (p = start + 1; p < finish; ++p) {
121 if (p[0].region_start < p[-1].region_start) {
127 } while (q > start &&
128 el.region_start < q[-1].region_start);
134 struct unwind_table *
135 unwind_table_add(const char *name, unsigned long base_addr,
137 void *start, void *end)
139 struct unwind_table *table;
141 struct unwind_table_entry *s = (struct unwind_table_entry *)start;
142 struct unwind_table_entry *e = (struct unwind_table_entry *)end;
144 unwind_table_sort(s, e);
146 table = kmalloc(sizeof(struct unwind_table), GFP_USER);
149 unwind_table_init(table, name, base_addr, gp, start, end);
150 spin_lock_irqsave(&unwind_lock, flags);
151 list_add_tail(&table->list, &unwind_tables);
152 spin_unlock_irqrestore(&unwind_lock, flags);
157 void unwind_table_remove(struct unwind_table *table)
161 spin_lock_irqsave(&unwind_lock, flags);
162 list_del(&table->list);
163 spin_unlock_irqrestore(&unwind_lock, flags);
168 /* Called from setup_arch to import the kernel unwind info */
169 static int unwind_init(void)
172 register unsigned long gp __asm__ ("r27");
174 start = (long)&__start___unwind[0];
175 stop = (long)&__stop___unwind[0];
177 spin_lock_init(&unwind_lock);
179 printk("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
181 (stop - start) / sizeof(struct unwind_table_entry));
183 unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
185 &__start___unwind[0], &__stop___unwind[0]);
189 for (i = 0; i < 10; i++)
191 printk("region 0x%x-0x%x\n",
192 __start___unwind[i].region_start,
193 __start___unwind[i].region_end);
200 static void unwind_frame_regs(struct unwind_frame_info *info)
202 const struct unwind_table_entry *e;
206 int looking_for_rp, rpoffset = 0;
208 e = find_unwind_entry(info->ip);
211 extern char _stext[], _etext[];
213 dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info->ip);
215 #ifdef CONFIG_KALLSYMS
216 /* Handle some frequent special cases.... */
218 char symname[KSYM_NAME_LEN+1];
220 kallsyms_lookup(info->ip, NULL, NULL, NULL, symname);
222 dbg("info->ip = 0x%lx, name = %s\n", info->ip, symname);
224 if (strcmp(symname, "_switch_to_ret") == 0) {
225 info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
226 info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
227 dbg("_switch_to_ret @ %lx - setting "
228 "prev_sp=%lx prev_ip=%lx\n",
229 info->ip, info->prev_sp,
232 } else if (strcmp(symname, "ret_from_kernel_thread") == 0 ||
233 strcmp(symname, "syscall_exit") == 0) {
234 info->prev_ip = info->prev_sp = 0;
240 /* Since we are doing the unwinding blind, we don't know if
241 we are adjusting the stack correctly or extracting the rp
242 correctly. The rp is checked to see if it belongs to the
243 kernel text section, if not we assume we don't have a
244 correct stack frame and we continue to unwind the stack.
245 This is not quite correct, and will fail for loadable
251 info->prev_sp = sp - 64;
253 if (get_user(tmp, (unsigned long *)(info->prev_sp - RP_OFFSET)))
257 } while (info->prev_ip < (unsigned long)_stext ||
258 info->prev_ip > (unsigned long)_etext);
262 dbg("analyzing func @ %lx with no unwind info, setting "
263 "prev_sp=%lx prev_ip=%lx\n", info->ip,
264 info->prev_sp, info->prev_ip);
266 dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
267 "Save_RP = %d, Millicode = %d size = %u\n",
268 e->region_start, e->region_end, e->Save_SP, e->Save_RP,
269 e->Millicode, e->Total_frame_size);
271 looking_for_rp = e->Save_RP;
273 for (npc = e->region_start;
274 (frame_size < (e->Total_frame_size << 3) ||
279 insn = *(unsigned int *)npc;
281 if ((insn & 0xffffc000) == 0x37de0000 ||
282 (insn & 0xffe00000) == 0x6fc00000) {
283 /* ldo X(sp), sp, or stwm X,D(sp) */
284 frame_size += (insn & 0x1 ? -1 << 13 : 0) |
285 ((insn & 0x3fff) >> 1);
286 dbg("analyzing func @ %lx, insn=%08x @ "
287 "%lx, frame_size = %ld\n", info->ip,
288 insn, npc, frame_size);
289 } else if ((insn & 0xffe00008) == 0x73c00008) {
291 frame_size += (insn & 0x1 ? -1 << 13 : 0) |
292 (((insn >> 4) & 0x3ff) << 3);
293 dbg("analyzing func @ %lx, insn=%08x @ "
294 "%lx, frame_size = %ld\n", info->ip,
295 insn, npc, frame_size);
296 } else if (insn == 0x6bc23fd9) {
300 dbg("analyzing func @ %lx, insn=stw rp,"
301 "-20(sp) @ %lx\n", info->ip, npc);
302 } else if (insn == 0x0fc212c1) {
303 /* std rp,-16(sr0,sp) */
306 dbg("analyzing func @ %lx, insn=std rp,"
307 "-16(sp) @ %lx\n", info->ip, npc);
311 info->prev_sp = info->sp - frame_size;
313 info->rp = info->r31;
315 info->rp = *(unsigned long *)(info->prev_sp - rpoffset);
316 info->prev_ip = info->rp;
319 dbg("analyzing func @ %lx, setting prev_sp=%lx "
320 "prev_ip=%lx npc=%lx\n", info->ip, info->prev_sp,
325 void unwind_frame_init(struct unwind_frame_info *info, struct task_struct *t,
326 struct pt_regs *regs)
328 memset(info, 0, sizeof(struct unwind_frame_info));
330 info->sp = regs->gr[30];
331 info->ip = regs->iaoq[0];
332 info->rp = regs->gr[2];
333 info->r31 = regs->gr[31];
335 dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
336 t ? (int)t->pid : -1, info->sp, info->ip);
339 void unwind_frame_init_from_blocked_task(struct unwind_frame_info *info, struct task_struct *t)
341 struct pt_regs *r = &t->thread.regs;
344 r2 = kmalloc(sizeof(struct pt_regs), GFP_KERNEL);
349 r2->iaoq[0] = r->kpc;
350 unwind_frame_init(info, t, r2);
354 void unwind_frame_init_running(struct unwind_frame_info *info, struct pt_regs *regs)
356 unwind_frame_init(info, current, regs);
359 int unwind_once(struct unwind_frame_info *next_frame)
361 unwind_frame_regs(next_frame);
363 if (next_frame->prev_sp == 0 ||
364 next_frame->prev_ip == 0)
367 next_frame->sp = next_frame->prev_sp;
368 next_frame->ip = next_frame->prev_ip;
369 next_frame->prev_sp = 0;
370 next_frame->prev_ip = 0;
372 dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
373 next_frame->t ? (int)next_frame->t->pid : -1,
374 next_frame->sp, next_frame->ip);
379 int unwind_to_user(struct unwind_frame_info *info)
384 ret = unwind_once(info);
385 } while (!ret && !(info->ip & 3));
390 module_init(unwind_init);