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/config.h>
12 #include <linux/kernel.h>
13 #include <linux/init.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;
39 static struct unwind_table *unwind_tables, *unwind_tables_end;
42 static inline const struct unwind_table_entry *
43 find_unwind_entry_in_table(const struct unwind_table *table, unsigned long addr)
45 const struct unwind_table_entry *e = NULL;
46 unsigned long lo, hi, mid;
49 hi = table->length - 1;
52 mid = (hi - lo) / 2 + lo;
53 e = &table->table[mid];
54 if (addr < e->region_start)
56 else if (addr > e->region_end)
65 static const struct unwind_table_entry *
66 find_unwind_entry(unsigned long addr)
68 struct unwind_table *table = unwind_tables;
69 const struct unwind_table_entry *e = NULL;
71 if (addr >= kernel_unwind_table.start &&
72 addr <= kernel_unwind_table.end)
73 e = find_unwind_entry_in_table(&kernel_unwind_table, addr);
75 for (; table; table = table->next) {
76 if (addr >= table->start &&
78 e = find_unwind_entry_in_table(table, addr);
87 unwind_table_init(struct unwind_table *table, const char *name,
88 unsigned long base_addr, unsigned long gp,
89 void *table_start, void *table_end)
91 struct unwind_table_entry *start = table_start;
92 struct unwind_table_entry *end =
93 (struct unwind_table_entry *)table_end - 1;
96 table->base_addr = base_addr;
98 table->start = base_addr + start->region_start;
99 table->end = base_addr + end->region_end;
100 table->table = (struct unwind_table_entry *)table_start;
101 table->length = end - start + 1;
104 for (; start <= end; start++) {
106 start->region_end > (start+1)->region_start) {
107 printk("WARNING: Out of order unwind entry! %p and %p\n", start, start+1);
110 start->region_start += base_addr;
111 start->region_end += base_addr;
116 unwind_table_add(const char *name, unsigned long base_addr,
118 void *start, void *end)
120 struct unwind_table *table;
123 table = kmalloc(sizeof(struct unwind_table), GFP_USER);
126 unwind_table_init(table, name, base_addr, gp, start, end);
127 spin_lock_irqsave(&unwind_lock, flags);
130 unwind_tables_end->next = table;
131 unwind_tables_end = table;
135 unwind_tables = unwind_tables_end = table;
137 spin_unlock_irqrestore(&unwind_lock, flags);
142 /* Called from setup_arch to import the kernel unwind info */
143 static int unwind_init(void)
146 register unsigned long gp __asm__ ("r27");
148 start = (long)&__start___unwind[0];
149 stop = (long)&__stop___unwind[0];
151 printk("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
153 (stop - start) / sizeof(struct unwind_table_entry));
155 unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
157 &__start___unwind[0], &__stop___unwind[0]);
161 for (i = 0; i < 10; i++)
163 printk("region 0x%x-0x%x\n",
164 __start___unwind[i].region_start,
165 __start___unwind[i].region_end);
172 static void unwind_frame_regs(struct unwind_frame_info *info)
174 const struct unwind_table_entry *e;
178 int looking_for_rp, rpoffset = 0;
180 e = find_unwind_entry(info->ip);
183 extern char _stext[], _etext[];
185 dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info->ip);
187 #ifdef CONFIG_KALLSYMS
188 /* Handle some frequent special cases.... */
190 char symname[KSYM_NAME_LEN+1];
192 unsigned long symsize, offset;
194 kallsyms_lookup(info->ip, &symsize, &offset,
197 dbg("info->ip = 0x%lx, name = %s\n", info->ip, symname);
199 if (strcmp(symname, "_switch_to_ret") == 0) {
200 info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
201 info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
202 dbg("_switch_to_ret @ %lx - setting "
203 "prev_sp=%lx prev_ip=%lx\n",
204 info->ip, info->prev_sp,
207 } else if (strcmp(symname, "ret_from_kernel_thread") == 0 ||
208 strcmp(symname, "syscall_exit") == 0) {
209 info->prev_ip = info->prev_sp = 0;
215 /* Since we are doing the unwinding blind, we don't know if
216 we are adjusting the stack correctly or extracting the rp
217 correctly. The rp is checked to see if it belongs to the
218 kernel text section, if not we assume we don't have a
219 correct stack frame and we continue to unwind the stack.
220 This is not quite correct, and will fail for loadable
226 info->prev_sp = sp - 64;
228 if (get_user(tmp, (unsigned long *)(info->prev_sp - RP_OFFSET)))
232 } while (info->prev_ip < (unsigned long)_stext ||
233 info->prev_ip > (unsigned long)_etext);
237 dbg("analyzing func @ %lx with no unwind info, setting "
238 "prev_sp=%lx prev_ip=%lx\n", info->ip,
239 info->prev_sp, info->prev_ip);
241 dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
242 "Save_RP = %d size = %u\n", e->region_start,
243 e->region_end, e->Save_SP, e->Save_RP,
244 e->Total_frame_size);
246 looking_for_rp = e->Save_RP;
248 for (npc = e->region_start;
249 (frame_size < (e->Total_frame_size << 3) ||
254 insn = *(unsigned int *)npc;
256 if ((insn & 0xffffc000) == 0x37de0000 ||
257 (insn & 0xffe00000) == 0x6fc00000) {
258 /* ldo X(sp), sp, or stwm X,D(sp) */
259 frame_size += (insn & 0x1 ? -1 << 13 : 0) |
260 ((insn & 0x3fff) >> 1);
261 dbg("analyzing func @ %lx, insn=%08x @ "
262 "%lx, frame_size = %ld\n", info->ip,
263 insn, npc, frame_size);
264 } else if ((insn & 0xffe00008) == 0x73c00008) {
266 frame_size += (insn & 0x1 ? -1 << 13 : 0) |
267 (((insn >> 4) & 0x3ff) << 3);
268 dbg("analyzing func @ %lx, insn=%08x @ "
269 "%lx, frame_size = %ld\n", info->ip,
270 insn, npc, frame_size);
271 } else if (insn == 0x6bc23fd9) {
275 dbg("analyzing func @ %lx, insn=stw rp,"
276 "-20(sp) @ %lx\n", info->ip, npc);
277 } else if (insn == 0x0fc212c1) {
278 /* std rp,-16(sr0,sp) */
281 dbg("analyzing func @ %lx, insn=std rp,"
282 "-16(sp) @ %lx\n", info->ip, npc);
286 info->prev_sp = info->sp - frame_size;
288 info->rp = *(unsigned long *)(info->prev_sp - rpoffset);
289 info->prev_ip = info->rp;
292 dbg("analyzing func @ %lx, setting prev_sp=%lx "
293 "prev_ip=%lx npc=%lx\n", info->ip, info->prev_sp,
298 void unwind_frame_init(struct unwind_frame_info *info, struct task_struct *t,
299 unsigned long sp, unsigned long ip, unsigned long rp)
301 memset(info, 0, sizeof(struct unwind_frame_info));
307 dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
308 t ? (int)t->pid : -1, info->sp, info->ip);
311 void unwind_frame_init_from_blocked_task(struct unwind_frame_info *info, struct task_struct *t)
313 struct pt_regs *regs = &t->thread.regs;
314 unwind_frame_init(info, t, regs->ksp, regs->kpc, 0);
317 void unwind_frame_init_running(struct unwind_frame_info *info, struct pt_regs *regs)
319 unwind_frame_init(info, current, regs->gr[30], regs->iaoq[0],
323 int unwind_once(struct unwind_frame_info *next_frame)
325 unwind_frame_regs(next_frame);
327 if (next_frame->prev_sp == 0 ||
328 next_frame->prev_ip == 0)
331 next_frame->sp = next_frame->prev_sp;
332 next_frame->ip = next_frame->prev_ip;
333 next_frame->prev_sp = 0;
334 next_frame->prev_ip = 0;
336 dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
337 next_frame->t ? (int)next_frame->t->pid : -1,
338 next_frame->sp, next_frame->ip);
343 int unwind_to_user(struct unwind_frame_info *info)
348 ret = unwind_once(info);
349 } while (!ret && !(info->ip & 3));
354 module_init(unwind_init);