unwind.c revision 248366
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 248366 2013-03-16 04:06: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#ifdef __ARM_EABI__
54/*
55 * Definitions for the instruction interpreter.
56 *
57 * The ARM EABI specifies how to perform the frame unwinding in the
58 * Exception Handling ABI for the ARM Architecture document. To perform
59 * the unwind we need to know the initial frame pointer, stack pointer,
60 * link register and program counter. We then find the entry within the
61 * index table that points to the function the program counter is within.
62 * This gives us either a list of three instructions to process, a 31-bit
63 * relative offset to a table of instructions, or a value telling us
64 * we can't unwind any further.
65 *
66 * When we have the instructions to process we need to decode them
67 * following table 4 in section 9.3. This describes a collection of bit
68 * patterns to encode that steps to take to update the stack pointer and
69 * link register to the correct values at the start of the function.
70 */
71
72/* A special case when we are unable to unwind past this function */
73#define	EXIDX_CANTUNWIND	1
74
75/* The register names */
76#define	FP	11
77#define	SP	13
78#define	LR	14
79#define	PC	15
80
81/*
82 * These are set in the linker script. Their addresses will be
83 * either the start or end of the exception table or index.
84 */
85extern int extab_start, extab_end, exidx_start, exidx_end;
86
87/*
88 * Entry types.
89 * These are the only entry types that have been seen in the kernel.
90 */
91#define	ENTRY_MASK	0xff000000
92#define	ENTRY_ARM_SU16	0x80000000
93#define	ENTRY_ARM_LU16	0x81000000
94
95/* Instruction masks. */
96#define	INSN_VSP_MASK		0xc0
97#define	INSN_VSP_SIZE_MASK	0x3f
98#define	INSN_STD_MASK		0xf0
99#define	INSN_STD_DATA_MASK	0x0f
100#define	INSN_POP_TYPE_MASK	0x08
101#define	INSN_POP_COUNT_MASK	0x07
102#define	INSN_VSP_LARGE_INC_MASK	0xff
103
104/* Instruction definitions */
105#define	INSN_VSP_INC		0x00
106#define	INSN_VSP_DEC		0x40
107#define	INSN_POP_MASKED		0x80
108#define	INSN_VSP_REG		0x90
109#define	INSN_POP_COUNT		0xa0
110#define	INSN_FINISH		0xb0
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_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
272		unsigned int uleb128;
273
274		/* Read the increment value */
275		uleb128 = db_unwind_exec_read_byte(state);
276
277		state->registers[SP] += 0x204 + (uleb128 << 2);
278
279	} else {
280		/* We hit a new instruction that needs to be implemented */
281		db_printf("Unhandled instruction %.2x\n", insn);
282		return 1;
283	}
284
285	if (update_vsp) {
286		state->registers[SP] = (uint32_t)vsp;
287	}
288
289#if 0
290	db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
291	    state->registers[FP], state->registers[SP], state->registers[LR],
292	    state->registers[PC]);
293#endif
294
295	return 0;
296}
297
298/* Performs the unwind of a function */
299static int
300db_unwind_tab(struct unwind_state *state)
301{
302	uint32_t entry;
303
304	/* Set PC to a known value */
305	state->registers[PC] = 0;
306
307	/* Read the personality */
308	entry = *state->insn & ENTRY_MASK;
309
310	if (entry == ENTRY_ARM_SU16) {
311		state->byte = 2;
312		state->entries = 1;
313	} else if (entry == ENTRY_ARM_LU16) {
314		state->byte = 1;
315		state->entries = ((*state->insn >> 16) & 0xFF) + 1;
316	} else {
317		db_printf("Unknown entry: %x\n", entry);
318		return 1;
319	}
320
321	while (state->entries > 0) {
322		if (db_unwind_exec_insn(state) != 0)
323			return 1;
324	}
325
326	/*
327	 * The program counter was not updated, load it from the link register.
328	 */
329	if (state->registers[PC] == 0)
330		state->registers[PC] = state->registers[LR];
331
332	return 0;
333}
334
335static void
336db_stack_trace_cmd(struct unwind_state *state)
337{
338	struct unwind_idx *index;
339	const char *name;
340	db_expr_t value;
341	db_expr_t offset;
342	c_db_sym_t sym;
343	u_int reg, i;
344	char *sep;
345
346	while (1) {
347		/* Reset the mask of updated registers */
348		state->update_mask = 0;
349
350		/* The pc value is correct and will be overwritten, save it */
351		state->start_pc = state->registers[PC];
352
353		/* Find the item to run */
354		index = db_find_index(state->start_pc);
355
356		if (index->insn == EXIDX_CANTUNWIND) {
357			db_printf("Unable to unwind\n");
358			break;
359		} else if (index->insn & (1 << 31)) {
360			/* The data is within the instruction */
361			state->insn = &index->insn;
362		} else {
363			/* We have a prel31 offset to the unwind table */
364			uint32_t prel31_tbl = db_expand_prel31(index->insn);
365
366			state->insn = (uint32_t *)((uintptr_t)&index->insn +
367			    prel31_tbl);
368		}
369
370		/* Run the unwind function */
371		if (db_unwind_tab(state) != 0)
372			break;
373
374		/* This is not a kernel address, stop processing */
375		if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS)
376			break;
377
378		/* Print the frame details */
379		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
380		if (sym == C_DB_SYM_NULL) {
381			value = 0;
382			name = "(null)";
383		} else
384			db_symbol_values(sym, &name, &value);
385		db_printf("%s() at ", name);
386		db_printsym(state->start_pc, DB_STGY_PROC);
387		db_printf("\n");
388		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
389		    state->registers[LR]);
390		db_printsym(state->registers[LR], DB_STGY_PROC);
391		db_printf(")\n");
392		db_printf("\t sp = 0x%08x  fp = 0x%08x",
393		    state->registers[SP], state->registers[FP]);
394
395		/* Don't print the registers we have already printed */
396		state->update_mask &= ~((1 << SP) | (1 << FP) | (1 << LR) |
397		    (1 << PC));
398		sep = "\n\t";
399		for (i = 0, reg = 0; state->update_mask != 0;
400		    state->update_mask >>= 1, reg++) {
401			if ((state->update_mask & 1) != 0) {
402				db_printf("%s%sr%d = 0x%08x", sep,
403				    (reg < 10) ? " " : "", reg,
404				    state->registers[reg]);
405				i++;
406				if (i == 2) {
407					sep = "\n\t";
408					i = 0;
409				} else
410					sep = " ";
411
412			}
413		}
414		db_printf("\n");
415	}
416}
417#endif
418
419/*
420 * APCS stack frames are awkward beasts, so I don't think even trying to use
421 * a structure to represent them is a good idea.
422 *
423 * Here's the diagram from the APCS.  Increasing address is _up_ the page.
424 *
425 *          save code pointer       [fp]        <- fp points to here
426 *          return link value       [fp, #-4]
427 *          return sp value         [fp, #-8]
428 *          return fp value         [fp, #-12]
429 *          [saved v7 value]
430 *          [saved v6 value]
431 *          [saved v5 value]
432 *          [saved v4 value]
433 *          [saved v3 value]
434 *          [saved v2 value]
435 *          [saved v1 value]
436 *          [saved a4 value]
437 *          [saved a3 value]
438 *          [saved a2 value]
439 *          [saved a1 value]
440 *
441 * The save code pointer points twelve bytes beyond the start of the
442 * code sequence (usually a single STM) that created the stack frame.
443 * We have to disassemble it if we want to know which of the optional
444 * fields are actually present.
445 */
446
447#ifndef __ARM_EABI__	/* The frame format is differend in AAPCS */
448static void
449db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
450{
451	u_int32_t	*frame, *lastframe;
452	c_db_sym_t sym;
453	const char *name;
454	db_expr_t value;
455	db_expr_t offset;
456	int	scp_offset;
457
458	frame = (u_int32_t *)addr;
459	lastframe = NULL;
460	scp_offset = -(get_pc_str_offset() >> 2);
461
462	while (count-- && frame != NULL && !db_pager_quit) {
463		db_addr_t	scp;
464		u_int32_t	savecode;
465		int		r;
466		u_int32_t	*rp;
467		const char	*sep;
468
469		/*
470		 * In theory, the SCP isn't guaranteed to be in the function
471		 * that generated the stack frame.  We hope for the best.
472		 */
473		scp = frame[FR_SCP];
474
475		sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
476		if (sym == C_DB_SYM_NULL) {
477			value = 0;
478			name = "(null)";
479		} else
480			db_symbol_values(sym, &name, &value);
481		db_printf("%s() at ", name);
482		db_printsym(scp, DB_STGY_PROC);
483		db_printf("\n");
484#ifdef __PROG26
485		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
486		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
487		db_printf(")\n");
488#else
489		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
490		db_printsym(frame[FR_RLV], DB_STGY_PROC);
491		db_printf(")\n");
492#endif
493		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
494
495		savecode = ((u_int32_t *)scp)[scp_offset];
496		if ((savecode & 0x0e100000) == 0x08000000) {
497			/* Looks like an STM */
498			rp = frame - 4;
499			sep = "\n\t";
500			for (r = 10; r >= 0; r--) {
501				if (savecode & (1 << r)) {
502					db_printf("%sr%d=0x%08x",
503					    sep, r, *rp--);
504					sep = (frame - rp) % 4 == 2 ?
505					    "\n\t" : " ";
506				}
507			}
508		}
509
510		db_printf("\n");
511
512		/*
513		 * Switch to next frame up
514		 */
515		if (frame[FR_RFP] == 0)
516			break; /* Top of stack */
517
518		lastframe = frame;
519		frame = (u_int32_t *)(frame[FR_RFP]);
520
521		if (INKERNEL((int)frame)) {
522			/* staying in kernel */
523			if (frame <= lastframe) {
524				db_printf("Bad frame pointer: %p\n", frame);
525				break;
526			}
527		} else if (INKERNEL((int)lastframe)) {
528			/* switch from user to kernel */
529			if (kernel_only)
530				break;	/* kernel stack only */
531		} else {
532			/* in user */
533			if (frame <= lastframe) {
534				db_printf("Bad user frame pointer: %p\n",
535					  frame);
536				break;
537			}
538		}
539	}
540}
541#endif
542
543/* XXX stubs */
544void
545db_md_list_watchpoints()
546{
547}
548
549int
550db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
551{
552	return (0);
553}
554
555int
556db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
557{
558	return (0);
559}
560
561int
562db_trace_thread(struct thread *thr, int count)
563{
564#ifdef __ARM_EABI__
565	struct unwind_state state;
566#endif
567	struct pcb *ctx;
568
569	if (thr != curthread) {
570		ctx = kdb_thr_ctx(thr);
571
572#ifdef __ARM_EABI__
573		state.registers[FP] = ctx->un_32.pcb32_r11;
574		state.registers[SP] = ctx->un_32.pcb32_sp;
575		state.registers[LR] = ctx->un_32.pcb32_lr;
576		state.registers[PC] = ctx->un_32.pcb32_pc;
577
578		db_stack_trace_cmd(&state);
579#else
580		db_stack_trace_cmd(ctx->un_32.pcb32_r11, -1, TRUE);
581#endif
582	} else
583		db_trace_self();
584	return (0);
585}
586
587void
588db_trace_self(void)
589{
590#ifdef __ARM_EABI__
591	struct unwind_state state;
592	uint32_t sp;
593
594	/* Read the stack pointer */
595	__asm __volatile("mov %0, sp" : "=&r" (sp));
596
597	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
598	state.registers[SP] = sp;
599	state.registers[LR] = (uint32_t)__builtin_return_address(0);
600	state.registers[PC] = (uint32_t)db_trace_self;
601
602	db_stack_trace_cmd(&state);
603#else
604	db_addr_t addr;
605
606	addr = (db_addr_t)__builtin_frame_address(0);
607	db_stack_trace_cmd(addr, -1, FALSE);
608#endif
609}
610