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>
16 #include <linux/sort.h>
18 #include <asm/uaccess.h>
19 #include <asm/assembly.h>
20 #include <asm/asm-offsets.h>
21 #include <asm/ptrace.h>
23 #include <asm/unwind.h>
27 #define dbg(x...) printk(x)
32 #define KERNEL_START (KERNEL_BINARY_TEXT_START)
34 extern struct unwind_table_entry __start___unwind
[];
35 extern struct unwind_table_entry __stop___unwind
[];
37 static spinlock_t unwind_lock
;
39 * the kernel unwind block is not dynamically allocated so that
40 * we can call unwind_init as early in the bootup process as
41 * possible (before the slab allocator is initialized)
43 static struct unwind_table kernel_unwind_table __read_mostly
;
44 static LIST_HEAD(unwind_tables
);
46 static inline const struct unwind_table_entry
*
47 find_unwind_entry_in_table(const struct unwind_table
*table
, unsigned long addr
)
49 const struct unwind_table_entry
*e
= NULL
;
50 unsigned long lo
, hi
, mid
;
53 hi
= table
->length
- 1;
56 mid
= (hi
- lo
) / 2 + lo
;
57 e
= &table
->table
[mid
];
58 if (addr
< e
->region_start
)
60 else if (addr
> e
->region_end
)
69 static const struct unwind_table_entry
*
70 find_unwind_entry(unsigned long addr
)
72 struct unwind_table
*table
;
73 const struct unwind_table_entry
*e
= NULL
;
75 if (addr
>= kernel_unwind_table
.start
&&
76 addr
<= kernel_unwind_table
.end
)
77 e
= find_unwind_entry_in_table(&kernel_unwind_table
, addr
);
79 list_for_each_entry(table
, &unwind_tables
, list
) {
80 if (addr
>= table
->start
&&
82 e
= find_unwind_entry_in_table(table
, addr
);
91 unwind_table_init(struct unwind_table
*table
, const char *name
,
92 unsigned long base_addr
, unsigned long gp
,
93 void *table_start
, void *table_end
)
95 struct unwind_table_entry
*start
= table_start
;
96 struct unwind_table_entry
*end
=
97 (struct unwind_table_entry
*)table_end
- 1;
100 table
->base_addr
= base_addr
;
102 table
->start
= base_addr
+ start
->region_start
;
103 table
->end
= base_addr
+ end
->region_end
;
104 table
->table
= (struct unwind_table_entry
*)table_start
;
105 table
->length
= end
- start
+ 1;
106 INIT_LIST_HEAD(&table
->list
);
108 for (; start
<= end
; start
++) {
110 start
->region_end
> (start
+1)->region_start
) {
111 printk("WARNING: Out of order unwind entry! %p and %p\n", start
, start
+1);
114 start
->region_start
+= base_addr
;
115 start
->region_end
+= base_addr
;
119 static int cmp_unwind_table_entry(const void *a
, const void *b
)
121 return ((const struct unwind_table_entry
*)a
)->region_start
122 - ((const struct unwind_table_entry
*)b
)->region_start
;
126 unwind_table_sort(struct unwind_table_entry
*start
,
127 struct unwind_table_entry
*finish
)
129 sort(start
, finish
- start
, sizeof(struct unwind_table_entry
),
130 cmp_unwind_table_entry
, NULL
);
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 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]);
189 #define get_func_addr(fptr) fptr[2]
191 #define get_func_addr(fptr) fptr[0]
194 static int unwind_special(struct unwind_frame_info
*info
, unsigned long pc
, int frame_size
)
196 extern void handle_interruption(int, struct pt_regs
*);
197 static unsigned long *hi
= (unsigned long *)&handle_interruption
;
199 if (pc
== get_func_addr(hi
)) {
200 struct pt_regs
*regs
= (struct pt_regs
*)(info
->sp
- frame_size
- PT_SZ_ALGN
);
201 dbg("Unwinding through handle_interruption()\n");
202 info
->prev_sp
= regs
->gr
[30];
203 info
->prev_ip
= regs
->iaoq
[0];
211 static void unwind_frame_regs(struct unwind_frame_info
*info
)
213 const struct unwind_table_entry
*e
;
217 int looking_for_rp
, rpoffset
= 0;
219 e
= find_unwind_entry(info
->ip
);
222 extern char _stext
[], _etext
[];
224 dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info
->ip
);
226 #ifdef CONFIG_KALLSYMS
227 /* Handle some frequent special cases.... */
229 char symname
[KSYM_NAME_LEN
];
232 kallsyms_lookup(info
->ip
, NULL
, NULL
, &modname
,
235 dbg("info->ip = 0x%lx, name = %s\n", info
->ip
, symname
);
237 if (strcmp(symname
, "_switch_to_ret") == 0) {
238 info
->prev_sp
= info
->sp
- CALLEE_SAVE_FRAME_SIZE
;
239 info
->prev_ip
= *(unsigned long *)(info
->prev_sp
- RP_OFFSET
);
240 dbg("_switch_to_ret @ %lx - setting "
241 "prev_sp=%lx prev_ip=%lx\n",
242 info
->ip
, info
->prev_sp
,
245 } else if (strcmp(symname
, "ret_from_kernel_thread") == 0 ||
246 strcmp(symname
, "syscall_exit") == 0) {
247 info
->prev_ip
= info
->prev_sp
= 0;
253 /* Since we are doing the unwinding blind, we don't know if
254 we are adjusting the stack correctly or extracting the rp
255 correctly. The rp is checked to see if it belongs to the
256 kernel text section, if not we assume we don't have a
257 correct stack frame and we continue to unwind the stack.
258 This is not quite correct, and will fail for loadable
264 info
->prev_sp
= sp
- 64;
266 if (get_user(tmp
, (unsigned long *)(info
->prev_sp
- RP_OFFSET
)))
270 } while (info
->prev_ip
< (unsigned long)_stext
||
271 info
->prev_ip
> (unsigned long)_etext
);
275 dbg("analyzing func @ %lx with no unwind info, setting "
276 "prev_sp=%lx prev_ip=%lx\n", info
->ip
,
277 info
->prev_sp
, info
->prev_ip
);
279 dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
280 "Save_RP = %d, Millicode = %d size = %u\n",
281 e
->region_start
, e
->region_end
, e
->Save_SP
, e
->Save_RP
,
282 e
->Millicode
, e
->Total_frame_size
);
284 looking_for_rp
= e
->Save_RP
;
286 for (npc
= e
->region_start
;
287 (frame_size
< (e
->Total_frame_size
<< 3) ||
292 insn
= *(unsigned int *)npc
;
294 if ((insn
& 0xffffc000) == 0x37de0000 ||
295 (insn
& 0xffe00000) == 0x6fc00000) {
296 /* ldo X(sp), sp, or stwm X,D(sp) */
297 frame_size
+= (insn
& 0x1 ? -1 << 13 : 0) |
298 ((insn
& 0x3fff) >> 1);
299 dbg("analyzing func @ %lx, insn=%08x @ "
300 "%lx, frame_size = %ld\n", info
->ip
,
301 insn
, npc
, frame_size
);
302 } else if ((insn
& 0xffe00008) == 0x73c00008) {
304 frame_size
+= (insn
& 0x1 ? -1 << 13 : 0) |
305 (((insn
>> 4) & 0x3ff) << 3);
306 dbg("analyzing func @ %lx, insn=%08x @ "
307 "%lx, frame_size = %ld\n", info
->ip
,
308 insn
, npc
, frame_size
);
309 } else if (insn
== 0x6bc23fd9) {
313 dbg("analyzing func @ %lx, insn=stw rp,"
314 "-20(sp) @ %lx\n", info
->ip
, npc
);
315 } else if (insn
== 0x0fc212c1) {
316 /* std rp,-16(sr0,sp) */
319 dbg("analyzing func @ %lx, insn=std rp,"
320 "-16(sp) @ %lx\n", info
->ip
, npc
);
324 if (!unwind_special(info
, e
->region_start
, frame_size
)) {
325 info
->prev_sp
= info
->sp
- frame_size
;
327 info
->rp
= info
->r31
;
329 info
->rp
= *(unsigned long *)(info
->prev_sp
- rpoffset
);
330 info
->prev_ip
= info
->rp
;
334 dbg("analyzing func @ %lx, setting prev_sp=%lx "
335 "prev_ip=%lx npc=%lx\n", info
->ip
, info
->prev_sp
,
340 void unwind_frame_init(struct unwind_frame_info
*info
, struct task_struct
*t
,
341 struct pt_regs
*regs
)
343 memset(info
, 0, sizeof(struct unwind_frame_info
));
345 info
->sp
= regs
->gr
[30];
346 info
->ip
= regs
->iaoq
[0];
347 info
->rp
= regs
->gr
[2];
348 info
->r31
= regs
->gr
[31];
350 dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
351 t
? (int)t
->pid
: -1, info
->sp
, info
->ip
);
354 void unwind_frame_init_from_blocked_task(struct unwind_frame_info
*info
, struct task_struct
*t
)
356 struct pt_regs
*r
= &t
->thread
.regs
;
359 r2
= kmalloc(sizeof(struct pt_regs
), GFP_ATOMIC
);
364 r2
->iaoq
[0] = r
->kpc
;
365 unwind_frame_init(info
, t
, r2
);
369 void unwind_frame_init_running(struct unwind_frame_info
*info
, struct pt_regs
*regs
)
371 unwind_frame_init(info
, current
, regs
);
374 int unwind_once(struct unwind_frame_info
*next_frame
)
376 unwind_frame_regs(next_frame
);
378 if (next_frame
->prev_sp
== 0 ||
379 next_frame
->prev_ip
== 0)
382 next_frame
->sp
= next_frame
->prev_sp
;
383 next_frame
->ip
= next_frame
->prev_ip
;
384 next_frame
->prev_sp
= 0;
385 next_frame
->prev_ip
= 0;
387 dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
388 next_frame
->t
? (int)next_frame
->t
->pid
: -1,
389 next_frame
->sp
, next_frame
->ip
);
394 int unwind_to_user(struct unwind_frame_info
*info
)
399 ret
= unwind_once(info
);
400 } while (!ret
&& !(info
->ip
& 3));
405 unsigned long return_address(unsigned int level
)
407 struct unwind_frame_info info
;
411 /* initialize unwind info */
412 asm volatile ("copy %%r30, %0" : "=r"(sp
));
413 memset(&r
, 0, sizeof(struct pt_regs
));
414 r
.iaoq
[0] = (unsigned long) current_text_addr();
415 r
.gr
[2] = (unsigned long) __builtin_return_address(0);
417 unwind_frame_init(&info
, current
, &r
);
422 if (unwind_once(&info
) < 0 || info
.ip
== 0)
424 if (!__kernel_text_address(info
.ip
)) {
427 } while (info
.ip
&& level
--);