unwind.c revision 278578
1/*	$NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2000, 2001 Ben Harris
5 * Copyright (c) 1996 Scott K. Stevens
6 *
7 * Mach Operating System
8 * Copyright (c) 1991,1990 Carnegie Mellon University
9 * All Rights Reserved.
10 *
11 * Permission to use, copy, modify and distribute this software and its
12 * documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation.
16 *
17 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
19 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 *
21 * Carnegie Mellon requests users of this software to return to
22 *
23 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
24 *  School of Computer Science
25 *  Carnegie Mellon University
26 *  Pittsburgh PA 15213-3890
27 *
28 * any improvements or extensions that they make and grant Carnegie the
29 * rights to redistribute these changes.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/arm/arm/db_trace.c 278578 2015-02-11 10:40:49Z andrew $");
34#include <sys/param.h>
35#include <sys/systm.h>
36
37
38#include <sys/proc.h>
39#include <sys/kdb.h>
40#include <sys/stack.h>
41#include <machine/armreg.h>
42#include <machine/asm.h>
43#include <machine/cpufunc.h>
44#include <machine/db_machdep.h>
45#include <machine/pcb.h>
46#include <machine/stack.h>
47#include <machine/vmparam.h>
48#include <ddb/ddb.h>
49#include <ddb/db_access.h>
50#include <ddb/db_sym.h>
51#include <ddb/db_output.h>
52
53/*
54 * Definitions for the instruction interpreter.
55 *
56 * The ARM EABI specifies how to perform the frame unwinding in the
57 * Exception Handling ABI for the ARM Architecture document. To perform
58 * the unwind we need to know the initial frame pointer, stack pointer,
59 * link register and program counter. We then find the entry within the
60 * index table that points to the function the program counter is within.
61 * This gives us either a list of three instructions to process, a 31-bit
62 * relative offset to a table of instructions, or a value telling us
63 * we can't unwind any further.
64 *
65 * When we have the instructions to process we need to decode them
66 * following table 4 in section 9.3. This describes a collection of bit
67 * patterns to encode that steps to take to update the stack pointer and
68 * link register to the correct values at the start of the function.
69 */
70
71/* A special case when we are unable to unwind past this function */
72#define	EXIDX_CANTUNWIND	1
73
74/* The register names */
75#define	FP	11
76#define	SP	13
77#define	LR	14
78#define	PC	15
79
80/*
81 * These are set in the linker script. Their addresses will be
82 * either the start or end of the exception table or index.
83 */
84extern int extab_start, extab_end, exidx_start, exidx_end;
85
86/*
87 * Entry types.
88 * These are the only entry types that have been seen in the kernel.
89 */
90#define	ENTRY_MASK	0xff000000
91#define	ENTRY_ARM_SU16	0x80000000
92#define	ENTRY_ARM_LU16	0x81000000
93
94/* Instruction masks. */
95#define	INSN_VSP_MASK		0xc0
96#define	INSN_VSP_SIZE_MASK	0x3f
97#define	INSN_STD_MASK		0xf0
98#define	INSN_STD_DATA_MASK	0x0f
99#define	INSN_POP_TYPE_MASK	0x08
100#define	INSN_POP_COUNT_MASK	0x07
101#define	INSN_VSP_LARGE_INC_MASK	0xff
102
103/* Instruction definitions */
104#define	INSN_VSP_INC		0x00
105#define	INSN_VSP_DEC		0x40
106#define	INSN_POP_MASKED		0x80
107#define	INSN_VSP_REG		0x90
108#define	INSN_POP_COUNT		0xa0
109#define	INSN_FINISH		0xb0
110#define	INSN_POP_REGS		0xb1
111#define	INSN_VSP_LARGE_INC	0xb2
112
113/* An item in the exception index table */
114struct unwind_idx {
115	uint32_t offset;
116	uint32_t insn;
117};
118
119/* The state of the unwind process */
120struct unwind_state {
121	uint32_t registers[16];
122	uint32_t start_pc;
123	uint32_t *insn;
124	u_int entries;
125	u_int byte;
126	uint16_t update_mask;
127};
128
129/* Expand a 31-bit signed value to a 32-bit signed value */
130static __inline int32_t
131db_expand_prel31(uint32_t prel31)
132{
133
134	return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
135}
136
137/*
138 * Perform a binary search of the index table to find the function
139 * with the largest address that doesn't exceed addr.
140 */
141static struct unwind_idx *
142db_find_index(uint32_t addr)
143{
144	unsigned int min, mid, max;
145	struct unwind_idx *start;
146	struct unwind_idx *item;
147	int32_t prel31_addr;
148	uint32_t func_addr;
149
150	start = (struct unwind_idx *)&exidx_start;
151
152	min = 0;
153	max = (&exidx_end - &exidx_start) / 2;
154
155	while (min != max) {
156		mid = min + (max - min + 1) / 2;
157
158		item = &start[mid];
159
160	 	prel31_addr = db_expand_prel31(item->offset);
161		func_addr = (uint32_t)&item->offset + prel31_addr;
162
163		if (func_addr <= addr) {
164			min = mid;
165		} else {
166			max = mid - 1;
167		}
168	}
169
170	return &start[min];
171}
172
173/* Reads the next byte from the instruction list */
174static uint8_t
175db_unwind_exec_read_byte(struct unwind_state *state)
176{
177	uint8_t insn;
178
179	/* Read the unwind instruction */
180	insn = (*state->insn) >> (state->byte * 8);
181
182	/* Update the location of the next instruction */
183	if (state->byte == 0) {
184		state->byte = 3;
185		state->insn++;
186		state->entries--;
187	} else
188		state->byte--;
189
190	return insn;
191}
192
193/* Executes the next instruction on the list */
194static int
195db_unwind_exec_insn(struct unwind_state *state)
196{
197	unsigned int insn;
198	uint32_t *vsp = (uint32_t *)state->registers[SP];
199	int update_vsp = 0;
200
201	/* This should never happen */
202	if (state->entries == 0)
203		return 1;
204
205	/* Read the next instruction */
206	insn = db_unwind_exec_read_byte(state);
207
208	if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
209		state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
210
211	} else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
212		state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
213
214	} else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
215		unsigned int mask, reg;
216
217		/* Load the mask */
218		mask = db_unwind_exec_read_byte(state);
219		mask |= (insn & INSN_STD_DATA_MASK) << 8;
220
221		/* We have a refuse to unwind instruction */
222		if (mask == 0)
223			return 1;
224
225		/* Update SP */
226		update_vsp = 1;
227
228		/* Load the registers */
229		for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
230			if (mask & 1) {
231				state->registers[reg] = *vsp++;
232				state->update_mask |= 1 << reg;
233
234				/* If we have updated SP kep its value */
235				if (reg == SP)
236					update_vsp = 0;
237			}
238		}
239
240	} else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
241	    ((insn & INSN_STD_DATA_MASK) != 13) &&
242	    ((insn & INSN_STD_DATA_MASK) != 15)) {
243		/* sp = register */
244		state->registers[SP] =
245		    state->registers[insn & INSN_STD_DATA_MASK];
246
247	} else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
248		unsigned int count, reg;
249
250		/* Read how many registers to load */
251		count = insn & INSN_POP_COUNT_MASK;
252
253		/* Update sp */
254		update_vsp = 1;
255
256		/* Pop the registers */
257		for (reg = 4; reg <= 4 + count; reg++) {
258			state->registers[reg] = *vsp++;
259			state->update_mask |= 1 << reg;
260		}
261
262		/* Check if we are in the pop r14 version */
263		if ((insn & INSN_POP_TYPE_MASK) != 0) {
264			state->registers[14] = *vsp++;
265		}
266
267	} else if (insn == INSN_FINISH) {
268		/* Stop processing */
269		state->entries = 0;
270
271	} else if (insn == INSN_POP_REGS) {
272		unsigned int mask, reg;
273
274		mask = db_unwind_exec_read_byte(state);
275		if (mask == 0 || (mask & 0xf0) != 0)
276			return 1;
277
278		/* Update SP */
279		update_vsp = 1;
280
281		/* Load the registers */
282		for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
283			if (mask & 1) {
284				state->registers[reg] = *vsp++;
285				state->update_mask |= 1 << reg;
286			}
287		}
288
289	} else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
290		unsigned int uleb128;
291
292		/* Read the increment value */
293		uleb128 = db_unwind_exec_read_byte(state);
294
295		state->registers[SP] += 0x204 + (uleb128 << 2);
296
297	} else {
298		/* We hit a new instruction that needs to be implemented */
299		db_printf("Unhandled instruction %.2x\n", insn);
300		return 1;
301	}
302
303	if (update_vsp) {
304		state->registers[SP] = (uint32_t)vsp;
305	}
306
307#if 0
308	db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
309	    state->registers[FP], state->registers[SP], state->registers[LR],
310	    state->registers[PC]);
311#endif
312
313	return 0;
314}
315
316/* Performs the unwind of a function */
317static int
318db_unwind_tab(struct unwind_state *state)
319{
320	uint32_t entry;
321
322	/* Set PC to a known value */
323	state->registers[PC] = 0;
324
325	/* Read the personality */
326	entry = *state->insn & ENTRY_MASK;
327
328	if (entry == ENTRY_ARM_SU16) {
329		state->byte = 2;
330		state->entries = 1;
331	} else if (entry == ENTRY_ARM_LU16) {
332		state->byte = 1;
333		state->entries = ((*state->insn >> 16) & 0xFF) + 1;
334	} else {
335		db_printf("Unknown entry: %x\n", entry);
336		return 1;
337	}
338
339	while (state->entries > 0) {
340		if (db_unwind_exec_insn(state) != 0)
341			return 1;
342	}
343
344	/*
345	 * The program counter was not updated, load it from the link register.
346	 */
347	if (state->registers[PC] == 0) {
348		state->registers[PC] = state->registers[LR];
349
350		/*
351		 * If the program counter changed, flag it in the update mask.
352		 */
353		if (state->start_pc != state->registers[PC])
354			state->update_mask |= 1 << PC;
355	}
356
357	return 0;
358}
359
360static void
361db_stack_trace_cmd(struct unwind_state *state)
362{
363	struct unwind_idx *index;
364	const char *name;
365	db_expr_t value;
366	db_expr_t offset;
367	c_db_sym_t sym;
368	u_int reg, i;
369	char *sep;
370	uint16_t upd_mask;
371	bool finished;
372
373	finished = false;
374	while (!finished) {
375		/* Reset the mask of updated registers */
376		state->update_mask = 0;
377
378		/* The pc value is correct and will be overwritten, save it */
379		state->start_pc = state->registers[PC];
380
381		/* Find the item to run */
382		index = db_find_index(state->start_pc);
383
384		if (index->insn != EXIDX_CANTUNWIND) {
385			if (index->insn & (1U << 31)) {
386				/* The data is within the instruction */
387				state->insn = &index->insn;
388			} else {
389				/* A prel31 offset to the unwind table */
390				state->insn = (uint32_t *)
391				    ((uintptr_t)&index->insn +
392				     db_expand_prel31(index->insn));
393			}
394			/* Run the unwind function */
395			finished = db_unwind_tab(state);
396		}
397
398		/* Print the frame details */
399		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
400		if (sym == C_DB_SYM_NULL) {
401			value = 0;
402			name = "(null)";
403		} else
404			db_symbol_values(sym, &name, &value);
405		db_printf("%s() at ", name);
406		db_printsym(state->start_pc, DB_STGY_PROC);
407		db_printf("\n");
408		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
409		    state->registers[LR]);
410		db_printsym(state->registers[LR], DB_STGY_PROC);
411		db_printf(")\n");
412		db_printf("\t sp = 0x%08x  fp = 0x%08x",
413		    state->registers[SP], state->registers[FP]);
414
415		/* Don't print the registers we have already printed */
416		upd_mask = state->update_mask &
417		    ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
418		sep = "\n\t";
419		for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
420			if ((upd_mask & 1) != 0) {
421				db_printf("%s%sr%d = 0x%08x", sep,
422				    (reg < 10) ? " " : "", reg,
423				    state->registers[reg]);
424				i++;
425				if (i == 2) {
426					sep = "\n\t";
427					i = 0;
428				} else
429					sep = " ";
430
431			}
432		}
433		db_printf("\n");
434
435		/*
436		 * Stop if directed to do so, or if we've unwound back to the
437		 * kernel entry point, or if the unwind function didn't change
438		 * anything (to avoid getting stuck in this loop forever).
439		 * If the latter happens, it's an indication that the unwind
440		 * information is incorrect somehow for the function named in
441		 * the last frame printed before you see the unwind failure
442		 * message (maybe it needs a STOP_UNWINDING).
443		 */
444		if (index->insn == EXIDX_CANTUNWIND) {
445			finished = true;
446		} else if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
447			db_printf("Unable to unwind into user mode\n");
448			finished = true;
449		} else if (state->update_mask == 0) {
450			db_printf("Unwind failure (no registers changed)\n");
451			finished = true;
452		}
453	}
454}
455
456/* XXX stubs */
457void
458db_md_list_watchpoints()
459{
460}
461
462int
463db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
464{
465	return (0);
466}
467
468int
469db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
470{
471	return (0);
472}
473
474int
475db_trace_thread(struct thread *thr, int count)
476{
477	struct unwind_state state;
478	struct pcb *ctx;
479
480	if (thr != curthread) {
481		ctx = kdb_thr_ctx(thr);
482
483		state.registers[FP] = ctx->pcb_regs.sf_r11;
484		state.registers[SP] = ctx->pcb_regs.sf_sp;
485		state.registers[LR] = ctx->pcb_regs.sf_lr;
486		state.registers[PC] = ctx->pcb_regs.sf_pc;
487
488		db_stack_trace_cmd(&state);
489	} else
490		db_trace_self();
491	return (0);
492}
493
494void
495db_trace_self(void)
496{
497	struct unwind_state state;
498	uint32_t sp;
499
500	/* Read the stack pointer */
501	__asm __volatile("mov %0, sp" : "=&r" (sp));
502
503	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
504	state.registers[SP] = sp;
505	state.registers[LR] = (uint32_t)__builtin_return_address(0);
506	state.registers[PC] = (uint32_t)db_trace_self;
507
508	db_stack_trace_cmd(&state);
509}
510