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/slab.h>
14 #include <linux/kallsyms.h>
16 #include <asm/uaccess.h>
17 #include <asm/assembly.h>
19 #include <asm/unwind.h>
23 #define dbg(x...) printk(x)
28 extern struct unwind_table_entry __start___unwind[];
29 extern struct unwind_table_entry __stop___unwind[];
31 static spinlock_t unwind_lock;
33 * the kernel unwind block is not dynamically allocated so that
34 * we can call unwind_init as early in the bootup process as
35 * possible (before the slab allocator is initialized)
37 static struct unwind_table kernel_unwind_table __read_mostly;
38 static LIST_HEAD(unwind_tables);
40 static inline const struct unwind_table_entry *
41 find_unwind_entry_in_table(const struct unwind_table *table, unsigned long addr)
43 const struct unwind_table_entry *e = NULL;
44 unsigned long lo, hi, mid;
47 hi = table->length - 1;
50 mid = (hi - lo) / 2 + lo;
51 e = &table->table[mid];
52 if (addr < e->region_start)
54 else if (addr > e->region_end)
63 static const struct unwind_table_entry *
64 find_unwind_entry(unsigned long addr)
66 struct unwind_table *table;
67 const struct unwind_table_entry *e = NULL;
69 if (addr >= kernel_unwind_table.start &&
70 addr <= kernel_unwind_table.end)
71 e = find_unwind_entry_in_table(&kernel_unwind_table, addr);
73 list_for_each_entry(table, &unwind_tables, list) {
74 if (addr >= table->start &&
76 e = find_unwind_entry_in_table(table, addr);
85 unwind_table_init(struct unwind_table *table, const char *name,
86 unsigned long base_addr, unsigned long gp,
87 void *table_start, void *table_end)
89 struct unwind_table_entry *start = table_start;
90 struct unwind_table_entry *end =
91 (struct unwind_table_entry *)table_end - 1;
94 table->base_addr = base_addr;
96 table->start = base_addr + start->region_start;
97 table->end = base_addr + end->region_end;
98 table->table = (struct unwind_table_entry *)table_start;
99 table->length = end - start + 1;
100 INIT_LIST_HEAD(&table->list);
102 for (; start <= end; start++) {
104 start->region_end > (start+1)->region_start) {
105 printk("WARNING: Out of order unwind entry! %p and %p\n", start, start+1);
108 start->region_start += base_addr;
109 start->region_end += base_addr;
114 unwind_table_sort(struct unwind_table_entry *start,
115 struct unwind_table_entry *finish)
117 struct unwind_table_entry el, *p, *q;
119 for (p = start + 1; p < finish; ++p) {
120 if (p[0].region_start < p[-1].region_start) {
126 } while (q > start &&
127 el.region_start < q[-1].region_start);
133 struct unwind_table *
134 unwind_table_add(const char *name, unsigned long base_addr,
136 void *start, void *end)
138 struct unwind_table *table;
140 struct unwind_table_entry *s = (struct unwind_table_entry *)start;
141 struct unwind_table_entry *e = (struct unwind_table_entry *)end;
143 unwind_table_sort(s, e);
145 table = kmalloc(sizeof(struct unwind_table), GFP_USER);
148 unwind_table_init(table, name, base_addr, gp, start, end);
149 spin_lock_irqsave(&unwind_lock, flags);
150 list_add_tail(&table->list, &unwind_tables);
151 spin_unlock_irqrestore(&unwind_lock, flags);
156 void unwind_table_remove(struct unwind_table *table)
160 spin_lock_irqsave(&unwind_lock, flags);
161 list_del(&table->list);
162 spin_unlock_irqrestore(&unwind_lock, flags);
167 /* Called from setup_arch to import the kernel unwind info */
168 static int unwind_init(void)
171 register unsigned long gp __asm__ ("r27");
173 start = (long)&__start___unwind[0];
174 stop = (long)&__stop___unwind[0];
176 spin_lock_init(&unwind_lock);
178 printk("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
180 (stop - start) / sizeof(struct unwind_table_entry));
182 unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
184 &__start___unwind[0], &__stop___unwind[0]);
188 for (i = 0; i < 10; i++)
190 printk("region 0x%x-0x%x\n",
191 __start___unwind[i].region_start,
192 __start___unwind[i].region_end);
199 static void unwind_frame_regs(struct unwind_frame_info *info)
201 const struct unwind_table_entry *e;
205 int looking_for_rp, rpoffset = 0;
207 e = find_unwind_entry(info->ip);
210 extern char _stext[], _etext[];
212 dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info->ip);
214 #ifdef CONFIG_KALLSYMS
215 /* Handle some frequent special cases.... */
217 char symname[KSYM_NAME_LEN+1];
219 unsigned long symsize, offset;
221 kallsyms_lookup(info->ip, &symsize, &offset,
224 dbg("info->ip = 0x%lx, name = %s\n", info->ip, symname);
226 if (strcmp(symname, "_switch_to_ret") == 0) {
227 info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
228 info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
229 dbg("_switch_to_ret @ %lx - setting "
230 "prev_sp=%lx prev_ip=%lx\n",
231 info->ip, info->prev_sp,
234 } else if (strcmp(symname, "ret_from_kernel_thread") == 0 ||
235 strcmp(symname, "syscall_exit") == 0) {
236 info->prev_ip = info->prev_sp = 0;
242 /* Since we are doing the unwinding blind, we don't know if
243 we are adjusting the stack correctly or extracting the rp
244 correctly. The rp is checked to see if it belongs to the
245 kernel text section, if not we assume we don't have a
246 correct stack frame and we continue to unwind the stack.
247 This is not quite correct, and will fail for loadable
253 info->prev_sp = sp - 64;
255 if (get_user(tmp, (unsigned long *)(info->prev_sp - RP_OFFSET)))
259 } while (info->prev_ip < (unsigned long)_stext ||
260 info->prev_ip > (unsigned long)_etext);
264 dbg("analyzing func @ %lx with no unwind info, setting "
265 "prev_sp=%lx prev_ip=%lx\n", info->ip,
266 info->prev_sp, info->prev_ip);
268 dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
269 "Save_RP = %d, Millicode = %d size = %u\n",
270 e->region_start, e->region_end, e->Save_SP, e->Save_RP,
271 e->Millicode, e->Total_frame_size);
273 looking_for_rp = e->Save_RP;
275 for (npc = e->region_start;
276 (frame_size < (e->Total_frame_size << 3) ||
281 insn = *(unsigned int *)npc;
283 if ((insn & 0xffffc000) == 0x37de0000 ||
284 (insn & 0xffe00000) == 0x6fc00000) {
285 /* ldo X(sp), sp, or stwm X,D(sp) */
286 frame_size += (insn & 0x1 ? -1 << 13 : 0) |
287 ((insn & 0x3fff) >> 1);
288 dbg("analyzing func @ %lx, insn=%08x @ "
289 "%lx, frame_size = %ld\n", info->ip,
290 insn, npc, frame_size);
291 } else if ((insn & 0xffe00008) == 0x73c00008) {
293 frame_size += (insn & 0x1 ? -1 << 13 : 0) |
294 (((insn >> 4) & 0x3ff) << 3);
295 dbg("analyzing func @ %lx, insn=%08x @ "
296 "%lx, frame_size = %ld\n", info->ip,
297 insn, npc, frame_size);
298 } else if (insn == 0x6bc23fd9) {
302 dbg("analyzing func @ %lx, insn=stw rp,"
303 "-20(sp) @ %lx\n", info->ip, npc);
304 } else if (insn == 0x0fc212c1) {
305 /* std rp,-16(sr0,sp) */
308 dbg("analyzing func @ %lx, insn=std rp,"
309 "-16(sp) @ %lx\n", info->ip, npc);
313 info->prev_sp = info->sp - frame_size;
315 info->rp = info->r31;
317 info->rp = *(unsigned long *)(info->prev_sp - rpoffset);
318 info->prev_ip = info->rp;
321 dbg("analyzing func @ %lx, setting prev_sp=%lx "
322 "prev_ip=%lx npc=%lx\n", info->ip, info->prev_sp,
327 void unwind_frame_init(struct unwind_frame_info *info, struct task_struct *t,
328 struct pt_regs *regs)
330 memset(info, 0, sizeof(struct unwind_frame_info));
332 info->sp = regs->gr[30];
333 info->ip = regs->iaoq[0];
334 info->rp = regs->gr[2];
335 info->r31 = regs->gr[31];
337 dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
338 t ? (int)t->pid : -1, info->sp, info->ip);
341 void unwind_frame_init_from_blocked_task(struct unwind_frame_info *info, struct task_struct *t)
343 struct pt_regs *r = &t->thread.regs;
346 r2 = kmalloc(sizeof(struct pt_regs), GFP_KERNEL);
351 r2->iaoq[0] = r->kpc;
352 unwind_frame_init(info, t, r2);
356 void unwind_frame_init_running(struct unwind_frame_info *info, struct pt_regs *regs)
358 unwind_frame_init(info, current, regs);
361 int unwind_once(struct unwind_frame_info *next_frame)
363 unwind_frame_regs(next_frame);
365 if (next_frame->prev_sp == 0 ||
366 next_frame->prev_ip == 0)
369 next_frame->sp = next_frame->prev_sp;
370 next_frame->ip = next_frame->prev_ip;
371 next_frame->prev_sp = 0;
372 next_frame->prev_ip = 0;
374 dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
375 next_frame->t ? (int)next_frame->t->pid : -1,
376 next_frame->sp, next_frame->ip);
381 int unwind_to_user(struct unwind_frame_info *info)
386 ret = unwind_once(info);
387 } while (!ret && !(info->ip & 3));
392 module_init(unwind_init);