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>
16 #include <asm/uaccess.h>
18 #include <asm/unwind.h>
22 #define dbg(x...) printk(x)
27 extern struct unwind_table_entry __start___unwind
[];
28 extern struct unwind_table_entry __stop___unwind
[];
30 static spinlock_t unwind_lock
;
32 * the kernel unwind block is not dynamically allocated so that
33 * we can call unwind_init as early in the bootup process as
34 * possible (before the slab allocator is initialized)
36 static struct unwind_table kernel_unwind_table
;
37 static struct unwind_table
*unwind_tables
, *unwind_tables_end
;
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
= 0;
44 unsigned long lo
, hi
, mid
;
46 for (lo
= 0, hi
= table
->length
; lo
< hi
; )
49 e
= &table
->table
[mid
];
50 if (addr
< e
->region_start
)
52 else if (addr
> e
->region_end
)
61 static inline const struct unwind_table_entry
*
62 find_unwind_entry(unsigned long addr
)
64 struct unwind_table
*table
= unwind_tables
;
65 const struct unwind_table_entry
*e
= NULL
;
67 if (addr
>= kernel_unwind_table
.start
&&
68 addr
<= kernel_unwind_table
.end
)
69 e
= find_unwind_entry_in_table(&kernel_unwind_table
, addr
);
71 for (; table
; table
= table
->next
)
73 if (addr
>= table
->start
&&
75 e
= find_unwind_entry_in_table(table
, addr
);
84 unwind_table_init(struct unwind_table
*table
, const char *name
,
85 unsigned long base_addr
, unsigned long gp
,
86 void *table_start
, void *table_end
)
88 struct unwind_table_entry
*start
= table_start
;
89 struct unwind_table_entry
*end
=
90 (struct unwind_table_entry
*)table_end
- 1;
93 table
->base_addr
= base_addr
;
95 table
->start
= base_addr
+ start
->region_start
;
96 table
->end
= base_addr
+ end
->region_end
;
97 table
->table
= (struct unwind_table_entry
*)table_start
;
98 table
->length
= end
- start
+ 1;
101 for (; start
<= end
; start
++) {
102 start
->region_start
+= base_addr
;
103 start
->region_end
+= base_addr
;
108 unwind_table_add(const char *name
, unsigned long base_addr
,
110 void *start
, void *end
)
112 struct unwind_table
*table
;
115 table
= kmalloc(sizeof(struct unwind_table
), GFP_USER
);
118 unwind_table_init(table
, name
, base_addr
, gp
, start
, end
);
119 spin_lock_irqsave(&unwind_lock
, flags
);
122 unwind_tables_end
->next
= table
;
123 unwind_tables_end
= table
;
127 unwind_tables
= unwind_tables_end
= table
;
129 spin_unlock_irqrestore(&unwind_lock
, flags
);
134 /* Called from setup_arch to import the kernel unwind info */
135 static int unwind_init(void)
138 register unsigned long gp
__asm__ ("r27");
140 start
= (long)&__start___unwind
[0];
141 stop
= (long)&__stop___unwind
[0];
143 printk("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
145 (stop
- start
) / sizeof(struct unwind_table_entry
));
147 unwind_table_init(&kernel_unwind_table
, "kernel", KERNEL_START
,
149 &__start___unwind
[0], &__stop___unwind
[0]);
153 for (i
= 0; i
< 10; i
++)
155 printk("region 0x%x-0x%x\n",
156 __start___unwind
[i
].region_start
,
157 __start___unwind
[i
].region_end
);
164 static void unwind_frame_regs(struct unwind_frame_info
*info
)
166 const struct unwind_table_entry
*e
;
170 int looking_for_rp
, rpoffset
= 0;
172 e
= find_unwind_entry(info
->ip
);
175 extern char _stext
[], _etext
[];
177 dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info
->ip
);
179 /* Since we are doing the unwinding blind, we don't know if
180 we are adjusting the stack correctly or extracting the rp
181 correctly. The rp is checked to see if it belongs to the
182 kernel text section, if not we assume we don't have a
183 correct stack frame and we continue to unwind the stack.
184 This is not quite correct, and will fail for loadable
188 info
->prev_sp
= sp
- 64;
190 /* FIXME: what happens if we unwind too far so that
191 sp no longer falls in a mapped kernel page? */
193 info
->prev_ip
= *(unsigned long *)(info
->prev_sp
- 20);
195 info
->prev_ip
= *(unsigned long *)(info
->prev_sp
- 16);
199 } while (info
->prev_ip
< (unsigned long)_stext
||
200 info
->prev_ip
> (unsigned long)_etext
);
202 dbg("analyzing func @ %lx with no unwind info, setting prev_sp=%lx prev_ip=%lx\n", info
->ip
, info
->prev_sp
, info
->prev_ip
);
205 dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, Save_RP = %d size = %u\n",
206 e
->region_start
, e
->region_end
, e
->Save_SP
, e
->Save_RP
, e
->Total_frame_size
);
208 looking_for_rp
= e
->Save_RP
;
210 for (npc
= e
->region_start
;
211 (frame_size
< (e
->Total_frame_size
<< 3) || looking_for_rp
) &&
215 insn
= *(unsigned int *)npc
;
217 if ((insn
& 0xffffc000) == 0x37de0000 ||
218 (insn
& 0xffe00000) == 0x6fc00000) {
219 /* ldo X(sp), sp, or stwm X,D(sp) */
220 frame_size
+= (insn
& 0x1 ? -1 << 13 : 0) |
221 ((insn
& 0x3fff) >> 1);
222 dbg("analyzing func @ %lx, insn=%08x @ %lx, frame_size = %ld\n", info
->ip
, insn
, npc
, frame_size
);
223 } else if ((insn
& 0xffe00008) == 0x7ec00008) {
225 frame_size
+= (insn
& 0x1 ? -1 << 13 : 0) |
226 (((insn
>> 4) & 0x3ff) << 3);
227 dbg("analyzing func @ %lx, insn=%08x @ %lx, frame_size = %ld\n", info
->ip
, insn
, npc
, frame_size
);
228 } else if (insn
== 0x6bc23fd9) {
232 dbg("analyzing func @ %lx, insn=stw rp,-20(sp) @ %lx\n", info
->ip
, npc
);
233 } else if (insn
== 0x0fc212c1) {
234 /* std rp,-16(sr0,sp) */
237 dbg("analyzing func @ %lx, insn=std rp,-16(sp) @ %lx\n", info
->ip
, npc
);
241 info
->prev_sp
= info
->sp
- frame_size
;
243 info
->rp
= *(unsigned long *)(info
->prev_sp
- rpoffset
);
244 info
->prev_ip
= info
->rp
;
247 dbg("analyzing func @ %lx, setting prev_sp=%lx prev_ip=%lx\n", info
->ip
, info
->prev_sp
, info
->prev_ip
);
251 void unwind_frame_init(struct unwind_frame_info
*info
, struct task_struct
*t
,
252 unsigned long sp
, unsigned long ip
, unsigned long rp
)
254 memset(info
, 0, sizeof(struct unwind_frame_info
));
260 dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n", t
? (int)t
->pid
: 0, info
->sp
, info
->ip
);
263 void unwind_frame_init_from_blocked_task(struct unwind_frame_info
*info
, struct task_struct
*t
)
265 struct pt_regs
*regs
= &t
->thread
.regs
;
266 unwind_frame_init(info
, t
, regs
->ksp
, regs
->kpc
, 0);
269 void unwind_frame_init_running(struct unwind_frame_info
*info
, struct pt_regs
*regs
)
271 unwind_frame_init(info
, current
, regs
->gr
[30], regs
->iaoq
[0],
275 int unwind_once(struct unwind_frame_info
*next_frame
)
277 unwind_frame_regs(next_frame
);
279 if (next_frame
->prev_sp
== 0 ||
280 next_frame
->prev_ip
== 0)
283 next_frame
->sp
= next_frame
->prev_sp
;
284 next_frame
->ip
= next_frame
->prev_ip
;
285 next_frame
->prev_sp
= 0;
286 next_frame
->prev_ip
= 0;
288 dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n", (int)next_frame
->t
->pid
, next_frame
->sp
, next_frame
->ip
);
293 int unwind_to_user(struct unwind_frame_info
*info
)
298 ret
= unwind_once(info
);
299 } while (!ret
&& !(info
->ip
& 3));
304 module_init(unwind_init
);