Blame view

arch/arm/kernel/unwind.c 12.4 KB
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  /*
   * arch/arm/kernel/unwind.c
   *
   * Copyright (C) 2008 ARM Limited
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   *
   *
   * Stack unwinding support for ARM
   *
   * An ARM EABI version of gcc is required to generate the unwind
   * tables. For information about the structure of the unwind tables,
   * see "Exception Handling ABI for the ARM Architecture" at:
   *
   * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
   */
830703c76   Alexander Shishkin   ARM: 6140/1: sile...
28
  #ifndef __CHECKER__
6603a4fd5   Claudio Scordino   ARM: 5776/1: Chec...
29
30
31
32
33
34
35
36
  #if !defined (__ARM_EABI__)
  #warning Your compiler does not have EABI support.
  #warning    ARM unwind is known to compile only with EABI compilers.
  #warning    Change compiler or disable ARM_UNWIND option.
  #elif (__GNUC__ == 4 && __GNUC_MINOR__ <= 2)
  #warning Your compiler is too buggy; it is known to not compile ARM unwind support.
  #warning    Change compiler or disable ARM_UNWIND option.
  #endif
830703c76   Alexander Shishkin   ARM: 6140/1: sile...
37
  #endif /* __CHECKER__ */
6603a4fd5   Claudio Scordino   ARM: 5776/1: Chec...
38

bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
39
40
  #include <linux/kernel.h>
  #include <linux/init.h>
ecea4ab6d   Paul Gortmaker   arm: convert core...
41
  #include <linux/export.h>
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/list.h>
  
  #include <asm/stacktrace.h>
  #include <asm/traps.h>
  #include <asm/unwind.h>
  
  /* Dummy functions to avoid linker complaints */
  void __aeabi_unwind_cpp_pr0(void)
  {
  };
  EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
  
  void __aeabi_unwind_cpp_pr1(void)
  {
  };
  EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
  
  void __aeabi_unwind_cpp_pr2(void)
  {
  };
  EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
  
  struct unwind_ctrl_block {
  	unsigned long vrs[16];		/* virtual register set */
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
69
  	const unsigned long *insn;	/* pointer to the current instructions word */
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
70
71
72
73
74
  	int entries;			/* number of entries left to interpret */
  	int byte;			/* current byte number in the instructions word */
  };
  
  enum regs {
b86040a59   Catalin Marinas   Thumb-2: Implemen...
75
76
77
  #ifdef CONFIG_THUMB2_KERNEL
  	FP = 7,
  #else
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
78
  	FP = 11,
b86040a59   Catalin Marinas   Thumb-2: Implemen...
79
  #endif
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
80
81
82
83
  	SP = 13,
  	LR = 14,
  	PC = 15
  };
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
84
85
86
  extern const struct unwind_idx __start_unwind_idx[];
  static const struct unwind_idx *__origin_unwind_idx;
  extern const struct unwind_idx __stop_unwind_idx[];
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
87
88
89
90
91
92
93
94
95
96
97
98
99
  
  static DEFINE_SPINLOCK(unwind_lock);
  static LIST_HEAD(unwind_tables);
  
  /* Convert a prel31 symbol to an absolute address */
  #define prel31_to_addr(ptr)				\
  ({							\
  	/* sign-extend to 32 bits */			\
  	long offset = (((long)*(ptr)) << 1) >> 1;	\
  	(unsigned long)(ptr) + offset;			\
  })
  
  /*
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
100
   * Binary search in the unwind index. The entries are
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
101
   * guaranteed to be sorted in ascending order by the linker.
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
102
103
104
105
   *
   * start = first entry
   * origin = first entry with positive offset (or stop if there is no such entry)
   * stop - 1 = last entry
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
106
   */
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
107
108
109
110
  static const struct unwind_idx *search_index(unsigned long addr,
  				       const struct unwind_idx *start,
  				       const struct unwind_idx *origin,
  				       const struct unwind_idx *stop)
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
111
  {
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  	unsigned long addr_prel31;
  
  	pr_debug("%s(%08lx, %p, %p, %p)
  ",
  			__func__, addr, start, origin, stop);
  
  	/*
  	 * only search in the section with the matching sign. This way the
  	 * prel31 numbers can be compared as unsigned longs.
  	 */
  	if (addr < (unsigned long)start)
  		/* negative offsets: [start; origin) */
  		stop = origin;
  	else
  		/* positive offsets: [origin; stop) */
  		start = origin;
  
  	/* prel31 for address relavive to start */
  	addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
131

de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	while (start < stop - 1) {
  		const struct unwind_idx *mid = start + ((stop - start) >> 1);
  
  		/*
  		 * As addr_prel31 is relative to start an offset is needed to
  		 * make it relative to mid.
  		 */
  		if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
  				mid->addr_offset)
  			stop = mid;
  		else {
  			/* keep addr_prel31 relative to start */
  			addr_prel31 -= ((unsigned long)mid -
  					(unsigned long)start);
  			start = mid;
  		}
  	}
  
  	if (likely(start->addr_offset <= addr_prel31))
  		return start;
  	else {
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
153
154
155
  		pr_warning("unwind: Unknown symbol address %08lx
  ", addr);
  		return NULL;
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
156
157
  	}
  }
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
158

de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
159
160
161
162
163
  static const struct unwind_idx *unwind_find_origin(
  		const struct unwind_idx *start, const struct unwind_idx *stop)
  {
  	pr_debug("%s(%p, %p)
  ", __func__, start, stop);
ddf5a25c5   Uwe Kleine-König   ARM: unwinder: fi...
164
  	while (start < stop) {
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
165
  		const struct unwind_idx *mid = start + ((stop - start) >> 1);
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
166

de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
167
168
  		if (mid->addr_offset >= 0x40000000)
  			/* negative offset */
ddf5a25c5   Uwe Kleine-König   ARM: unwinder: fi...
169
  			start = mid + 1;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
170
  		else
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
171
172
  			/* positive offset */
  			stop = mid;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
173
  	}
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
174
175
176
  	pr_debug("%s -> %p
  ", __func__, stop);
  	return stop;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
177
  }
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
178
  static const struct unwind_idx *unwind_find_idx(unsigned long addr)
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
179
  {
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
180
  	const struct unwind_idx *idx = NULL;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
181
182
183
184
  	unsigned long flags;
  
  	pr_debug("%s(%08lx)
  ", __func__, addr);
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
185
186
187
188
189
  	if (core_kernel_text(addr)) {
  		if (unlikely(!__origin_unwind_idx))
  			__origin_unwind_idx =
  				unwind_find_origin(__start_unwind_idx,
  						__stop_unwind_idx);
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
190
191
  		/* main unwind table */
  		idx = search_index(addr, __start_unwind_idx,
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
192
193
194
  				   __origin_unwind_idx,
  				   __stop_unwind_idx);
  	} else {
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
195
196
197
198
199
200
201
202
  		/* module unwind tables */
  		struct unwind_table *table;
  
  		spin_lock_irqsave(&unwind_lock, flags);
  		list_for_each_entry(table, &unwind_tables, list) {
  			if (addr >= table->begin_addr &&
  			    addr < table->end_addr) {
  				idx = search_index(addr, table->start,
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
203
204
  						   table->origin,
  						   table->stop);
5333a3de3   Phil Carmody   ARM: 6341/1: unwi...
205
206
  				/* Move-to-front to exploit common traces */
  				list_move(&table->list, &unwind_tables);
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  				break;
  			}
  		}
  		spin_unlock_irqrestore(&unwind_lock, flags);
  	}
  
  	pr_debug("%s: idx = %p
  ", __func__, idx);
  	return idx;
  }
  
  static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
  {
  	unsigned long ret;
  
  	if (ctrl->entries <= 0) {
  		pr_warning("unwind: Corrupt unwind table
  ");
  		return 0;
  	}
  
  	ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
  
  	if (ctrl->byte == 0) {
  		ctrl->insn++;
  		ctrl->entries--;
  		ctrl->byte = 3;
  	} else
  		ctrl->byte--;
  
  	return ret;
  }
  
  /*
   * Execute the current unwind instruction.
   */
  static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
  {
  	unsigned long insn = unwind_get_byte(ctrl);
  
  	pr_debug("%s: insn = %08lx
  ", __func__, insn);
  
  	if ((insn & 0xc0) == 0x00)
  		ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
  	else if ((insn & 0xc0) == 0x40)
  		ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
  	else if ((insn & 0xf0) == 0x80) {
  		unsigned long mask;
  		unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  		int load_sp, reg = 4;
  
  		insn = (insn << 8) | unwind_get_byte(ctrl);
  		mask = insn & 0x0fff;
  		if (mask == 0) {
  			pr_warning("unwind: 'Refuse to unwind' instruction %04lx
  ",
  				   insn);
  			return -URC_FAILURE;
  		}
  
  		/* pop R4-R15 according to mask */
  		load_sp = mask & (1 << (13 - 4));
  		while (mask) {
  			if (mask & 1)
  				ctrl->vrs[reg] = *vsp++;
  			mask >>= 1;
  			reg++;
  		}
  		if (!load_sp)
  			ctrl->vrs[SP] = (unsigned long)vsp;
  	} else if ((insn & 0xf0) == 0x90 &&
  		   (insn & 0x0d) != 0x0d)
  		ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
  	else if ((insn & 0xf0) == 0xa0) {
  		unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  		int reg;
  
  		/* pop R4-R[4+bbb] */
  		for (reg = 4; reg <= 4 + (insn & 7); reg++)
  			ctrl->vrs[reg] = *vsp++;
  		if (insn & 0x80)
  			ctrl->vrs[14] = *vsp++;
  		ctrl->vrs[SP] = (unsigned long)vsp;
  	} else if (insn == 0xb0) {
c894ed695   Catalin Marinas   [ARM] 5558/1: Add...
292
293
  		if (ctrl->vrs[PC] == 0)
  			ctrl->vrs[PC] = ctrl->vrs[LR];
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  		/* no further processing */
  		ctrl->entries = 0;
  	} else if (insn == 0xb1) {
  		unsigned long mask = unwind_get_byte(ctrl);
  		unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  		int reg = 0;
  
  		if (mask == 0 || mask & 0xf0) {
  			pr_warning("unwind: Spare encoding %04lx
  ",
  			       (insn << 8) | mask);
  			return -URC_FAILURE;
  		}
  
  		/* pop R0-R3 according to mask */
  		while (mask) {
  			if (mask & 1)
  				ctrl->vrs[reg] = *vsp++;
  			mask >>= 1;
  			reg++;
  		}
  		ctrl->vrs[SP] = (unsigned long)vsp;
  	} else if (insn == 0xb2) {
  		unsigned long uleb128 = unwind_get_byte(ctrl);
  
  		ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
  	} else {
  		pr_warning("unwind: Unhandled instruction %02lx
  ", insn);
  		return -URC_FAILURE;
  	}
  
  	pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx
  ", __func__,
  		 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
  
  	return URC_OK;
  }
  
  /*
   * Unwind a single frame starting with *sp for the symbol at *pc. It
   * updates the *pc and *sp with the new values.
   */
  int unwind_frame(struct stackframe *frame)
  {
  	unsigned long high, low;
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
340
  	const struct unwind_idx *idx;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
341
342
343
344
  	struct unwind_ctrl_block ctrl;
  
  	/* only go to a higher address on the stack */
  	low = frame->sp;
d33aadbf8   Will Deacon   ARM: 6468/1: back...
345
  	high = ALIGN(low, THREAD_SIZE);
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  
  	pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)
  ", __func__,
  		 frame->pc, frame->lr, frame->sp);
  
  	if (!kernel_text_address(frame->pc))
  		return -URC_FAILURE;
  
  	idx = unwind_find_idx(frame->pc);
  	if (!idx) {
  		pr_warning("unwind: Index not found %08lx
  ", frame->pc);
  		return -URC_FAILURE;
  	}
  
  	ctrl.vrs[FP] = frame->fp;
  	ctrl.vrs[SP] = frame->sp;
  	ctrl.vrs[LR] = frame->lr;
  	ctrl.vrs[PC] = 0;
  
  	if (idx->insn == 1)
  		/* can't unwind */
  		return -URC_FAILURE;
  	else if ((idx->insn & 0x80000000) == 0)
  		/* prel31 to the unwind table */
  		ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
  	else if ((idx->insn & 0xff000000) == 0x80000000)
  		/* only personality routine 0 supported in the index */
  		ctrl.insn = &idx->insn;
  	else {
  		pr_warning("unwind: Unsupported personality routine %08lx in the index at %p
  ",
  			   idx->insn, idx);
  		return -URC_FAILURE;
  	}
  
  	/* check the personality routine */
  	if ((*ctrl.insn & 0xff000000) == 0x80000000) {
  		ctrl.byte = 2;
  		ctrl.entries = 1;
  	} else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
  		ctrl.byte = 1;
  		ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
  	} else {
  		pr_warning("unwind: Unsupported personality routine %08lx at %p
  ",
  			   *ctrl.insn, ctrl.insn);
  		return -URC_FAILURE;
  	}
  
  	while (ctrl.entries > 0) {
c894ed695   Catalin Marinas   [ARM] 5558/1: Add...
397
  		int urc = unwind_exec_insn(&ctrl);
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
398
399
  		if (urc < 0)
  			return urc;
c894ed695   Catalin Marinas   [ARM] 5558/1: Add...
400
401
  		if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high)
  			return -URC_FAILURE;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
402
403
404
405
  	}
  
  	if (ctrl.vrs[PC] == 0)
  		ctrl.vrs[PC] = ctrl.vrs[LR];
c894ed695   Catalin Marinas   [ARM] 5558/1: Add...
406
407
408
  	/* check for infinite loop */
  	if (frame->pc == ctrl.vrs[PC])
  		return -URC_FAILURE;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
409
410
411
412
413
414
415
416
417
418
419
  	frame->fp = ctrl.vrs[FP];
  	frame->sp = ctrl.vrs[SP];
  	frame->lr = ctrl.vrs[LR];
  	frame->pc = ctrl.vrs[PC];
  
  	return URC_OK;
  }
  
  void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  {
  	struct stackframe frame;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
420
421
422
423
424
425
426
427
428
429
430
431
  	register unsigned long current_sp asm ("sp");
  
  	pr_debug("%s(regs = %p tsk = %p)
  ", __func__, regs, tsk);
  
  	if (!tsk)
  		tsk = current;
  
  	if (regs) {
  		frame.fp = regs->ARM_fp;
  		frame.sp = regs->ARM_sp;
  		frame.lr = regs->ARM_lr;
51d47999b   Laurent Pinchart   ARM: 5977/1: arm:...
432
433
434
  		/* PC might be corrupted, use LR in that case. */
  		frame.pc = kernel_text_address(regs->ARM_pc)
  			 ? regs->ARM_pc : regs->ARM_lr;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  	} else if (tsk == current) {
  		frame.fp = (unsigned long)__builtin_frame_address(0);
  		frame.sp = current_sp;
  		frame.lr = (unsigned long)__builtin_return_address(0);
  		frame.pc = (unsigned long)unwind_backtrace;
  	} else {
  		/* task blocked in __switch_to */
  		frame.fp = thread_saved_fp(tsk);
  		frame.sp = thread_saved_sp(tsk);
  		/*
  		 * The function calling __switch_to cannot be a leaf function
  		 * so LR is recovered from the stack.
  		 */
  		frame.lr = 0;
  		frame.pc = thread_saved_pc(tsk);
  	}
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
  	while (1) {
  		int urc;
  		unsigned long where = frame.pc;
  
  		urc = unwind_frame(&frame);
  		if (urc < 0)
  			break;
  		dump_backtrace_entry(where, frame.pc, frame.sp - 4);
  	}
  }
  
  struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  				      unsigned long text_addr,
  				      unsigned long text_size)
  {
  	unsigned long flags;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
467
468
469
470
471
472
473
474
  	struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
  
  	pr_debug("%s(%08lx, %08lx, %08lx, %08lx)
  ", __func__, start, size,
  		 text_addr, text_size);
  
  	if (!tab)
  		return tab;
de66a9790   Uwe Kleine-König   ARM: 7187/1: fix ...
475
476
477
  	tab->start = (const struct unwind_idx *)start;
  	tab->stop = (const struct unwind_idx *)(start + size);
  	tab->origin = unwind_find_origin(tab->start, tab->stop);
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
478
479
  	tab->begin_addr = text_addr;
  	tab->end_addr = text_addr + text_size;
bff595c15   Catalin Marinas   [ARM] 5383/2: unw...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
  	spin_lock_irqsave(&unwind_lock, flags);
  	list_add_tail(&tab->list, &unwind_tables);
  	spin_unlock_irqrestore(&unwind_lock, flags);
  
  	return tab;
  }
  
  void unwind_table_del(struct unwind_table *tab)
  {
  	unsigned long flags;
  
  	if (!tab)
  		return;
  
  	spin_lock_irqsave(&unwind_lock, flags);
  	list_del(&tab->list);
  	spin_unlock_irqrestore(&unwind_lock, flags);
  
  	kfree(tab);
  }