xref: /freebsd/sys/arm/arm/db_trace.c (revision 8ec81e392654c03ebba7792e47bf3744d623cf54)
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$");
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  */
85 extern 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 */
114 struct unwind_idx {
115 	uint32_t offset;
116 	uint32_t insn;
117 };
118 
119 /* The state of the unwind process */
120 struct 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 /* We need to provide these but never use them */
130 void __aeabi_unwind_cpp_pr0(void);
131 void __aeabi_unwind_cpp_pr1(void);
132 void __aeabi_unwind_cpp_pr2(void);
133 
134 void
135 __aeabi_unwind_cpp_pr0(void)
136 {
137 	panic("__aeabi_unwind_cpp_pr0");
138 }
139 
140 void
141 __aeabi_unwind_cpp_pr1(void)
142 {
143 	panic("__aeabi_unwind_cpp_pr1");
144 }
145 
146 void
147 __aeabi_unwind_cpp_pr2(void)
148 {
149 	panic("__aeabi_unwind_cpp_pr2");
150 }
151 
152 /* Expand a 31-bit signed value to a 32-bit signed value */
153 static __inline int32_t
154 db_expand_prel31(uint32_t prel31)
155 {
156 
157 	return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
158 }
159 
160 /*
161  * Perform a binary search of the index table to find the function
162  * with the largest address that doesn't exceed addr.
163  */
164 static struct unwind_idx *
165 db_find_index(uint32_t addr)
166 {
167 	unsigned int min, mid, max;
168 	struct unwind_idx *start;
169 	struct unwind_idx *item;
170 	int32_t prel31_addr;
171 	uint32_t func_addr;
172 
173 	start = (struct unwind_idx *)&exidx_start;
174 
175 	min = 0;
176 	max = (&exidx_end - &exidx_start) / 2;
177 
178 	while (min != max) {
179 		mid = min + (max - min + 1) / 2;
180 
181 		item = &start[mid];
182 
183 	 	prel31_addr = db_expand_prel31(item->offset);
184 		func_addr = (uint32_t)&item->offset + prel31_addr;
185 
186 		if (func_addr <= addr) {
187 			min = mid;
188 		} else {
189 			max = mid - 1;
190 		}
191 	}
192 
193 	return &start[min];
194 }
195 
196 /* Reads the next byte from the instruction list */
197 static uint8_t
198 db_unwind_exec_read_byte(struct unwind_state *state)
199 {
200 	uint8_t insn;
201 
202 	/* Read the unwind instruction */
203 	insn = (*state->insn) >> (state->byte * 8);
204 
205 	/* Update the location of the next instruction */
206 	if (state->byte == 0) {
207 		state->byte = 3;
208 		state->insn++;
209 		state->entries--;
210 	} else
211 		state->byte--;
212 
213 	return insn;
214 }
215 
216 /* Executes the next instruction on the list */
217 static int
218 db_unwind_exec_insn(struct unwind_state *state)
219 {
220 	unsigned int insn;
221 	uint32_t *vsp = (uint32_t *)state->registers[SP];
222 	int update_vsp = 0;
223 
224 	/* This should never happen */
225 	if (state->entries == 0)
226 		return 1;
227 
228 	/* Read the next instruction */
229 	insn = db_unwind_exec_read_byte(state);
230 
231 	if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
232 		state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
233 
234 	} else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
235 		state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
236 
237 	} else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
238 		unsigned int mask, reg;
239 
240 		/* Load the mask */
241 		mask = db_unwind_exec_read_byte(state);
242 		mask |= (insn & INSN_STD_DATA_MASK) << 8;
243 
244 		/* We have a refuse to unwind instruction */
245 		if (mask == 0)
246 			return 1;
247 
248 		/* Update SP */
249 		update_vsp = 1;
250 
251 		/* Load the registers */
252 		for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
253 			if (mask & 1) {
254 				state->registers[reg] = *vsp++;
255 				state->update_mask |= 1 << reg;
256 
257 				/* If we have updated SP kep its value */
258 				if (reg == SP)
259 					update_vsp = 0;
260 			}
261 		}
262 
263 	} else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
264 	    ((insn & INSN_STD_DATA_MASK) != 13) &&
265 	    ((insn & INSN_STD_DATA_MASK) != 15)) {
266 		/* sp = register */
267 		state->registers[SP] =
268 		    state->registers[insn & INSN_STD_DATA_MASK];
269 
270 	} else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
271 		unsigned int count, reg;
272 
273 		/* Read how many registers to load */
274 		count = insn & INSN_POP_COUNT_MASK;
275 
276 		/* Update sp */
277 		update_vsp = 1;
278 
279 		/* Pop the registers */
280 		for (reg = 4; reg <= 4 + count; reg++) {
281 			state->registers[reg] = *vsp++;
282 			state->update_mask |= 1 << reg;
283 		}
284 
285 		/* Check if we are in the pop r14 version */
286 		if ((insn & INSN_POP_TYPE_MASK) != 0) {
287 			state->registers[14] = *vsp++;
288 		}
289 
290 	} else if (insn == INSN_FINISH) {
291 		/* Stop processing */
292 		state->entries = 0;
293 
294 	} else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
295 		unsigned int uleb128;
296 
297 		/* Read the increment value */
298 		uleb128 = db_unwind_exec_read_byte(state);
299 
300 		state->registers[SP] += 0x204 + (uleb128 << 2);
301 
302 	} else {
303 		/* We hit a new instruction that needs to be implemented */
304 		db_printf("Unhandled instruction %.2x\n", insn);
305 		return 1;
306 	}
307 
308 	if (update_vsp) {
309 		state->registers[SP] = (uint32_t)vsp;
310 	}
311 
312 #if 0
313 	db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
314 	    state->registers[FP], state->registers[SP], state->registers[LR],
315 	    state->registers[PC]);
316 #endif
317 
318 	return 0;
319 }
320 
321 /* Performs the unwind of a function */
322 static int
323 db_unwind_tab(struct unwind_state *state)
324 {
325 	uint32_t entry;
326 
327 	/* Set PC to a known value */
328 	state->registers[PC] = 0;
329 
330 	/* Read the personality */
331 	entry = *state->insn & ENTRY_MASK;
332 
333 	if (entry == ENTRY_ARM_SU16) {
334 		state->byte = 2;
335 		state->entries = 1;
336 	} else if (entry == ENTRY_ARM_LU16) {
337 		state->byte = 1;
338 		state->entries = ((*state->insn >> 16) & 0xFF) + 1;
339 	} else {
340 		db_printf("Unknown entry: %x\n", entry);
341 		return 1;
342 	}
343 
344 	while (state->entries > 0) {
345 		if (db_unwind_exec_insn(state) != 0)
346 			return 1;
347 	}
348 
349 	/*
350 	 * The program counter was not updated, load it from the link register.
351 	 */
352 	if (state->registers[PC] == 0)
353 		state->registers[PC] = state->registers[LR];
354 
355 	return 0;
356 }
357 
358 static void
359 db_stack_trace_cmd(struct unwind_state *state)
360 {
361 	struct unwind_idx *index;
362 	const char *name;
363 	db_expr_t value;
364 	db_expr_t offset;
365 	c_db_sym_t sym;
366 	u_int reg, i;
367 	char *sep;
368 
369 	while (1) {
370 		/* Reset the mask of updated registers */
371 		state->update_mask = 0;
372 
373 		/* The pc value is correct and will be overwritten, save it */
374 		state->start_pc = state->registers[PC];
375 
376 		/* Find the item to run */
377 		index = db_find_index(state->start_pc);
378 
379 		if (index->insn == EXIDX_CANTUNWIND) {
380 			printf("Unable to unwind\n");
381 			break;
382 		} else if (index->insn & (1 << 31)) {
383 			/* The data is within the instruction */
384 			state->insn = &index->insn;
385 		} else {
386 			/* We have a prel31 offset to the unwind table */
387 			uint32_t prel31_tbl = db_expand_prel31(index->insn);
388 
389 			state->insn = (uint32_t *)((uintptr_t)&index->insn +
390 			    prel31_tbl);
391 		}
392 
393 		/* Run the unwind function */
394 		if (db_unwind_tab(state) != 0)
395 			break;
396 
397 		/* This is not a kernel address, stop processing */
398 		if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS)
399 			break;
400 
401 		/* Print the frame details */
402 		sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
403 		if (sym == C_DB_SYM_NULL) {
404 			value = 0;
405 			name = "(null)";
406 		} else
407 			db_symbol_values(sym, &name, &value);
408 		db_printf("%s() at ", name);
409 		db_printsym(state->start_pc, DB_STGY_PROC);
410 		db_printf("\n");
411 		db_printf("\t pc = 0x%08x  lr = 0x%08x (", state->start_pc,
412 		    state->registers[LR]);
413 		db_printsym(state->registers[LR], DB_STGY_PROC);
414 		db_printf(")\n");
415 		db_printf("\t sp = 0x%08x  fp = 0x%08x",
416 		    state->registers[SP], state->registers[FP]);
417 
418 		/* Don't print the registers we have already printed */
419 		state->update_mask &= ~((1 << SP) | (1 << FP) | (1 << LR) |
420 		    (1 << PC));
421 		sep = "\n\t";
422 		for (i = 0, reg = 0; state->update_mask != 0;
423 		    state->update_mask >>= 1, reg++) {
424 			if ((state->update_mask & 1) != 0) {
425 				db_printf("%s%sr%d = 0x%08x", sep,
426 				    (reg < 10) ? " " : "", reg,
427 				    state->registers[reg]);
428 				i++;
429 				if (i == 2) {
430 					sep = "\n\t";
431 					i = 0;
432 				} else
433 					sep = " ";
434 
435 			}
436 		}
437 		db_printf("\n");
438 	}
439 }
440 #endif
441 
442 /*
443  * APCS stack frames are awkward beasts, so I don't think even trying to use
444  * a structure to represent them is a good idea.
445  *
446  * Here's the diagram from the APCS.  Increasing address is _up_ the page.
447  *
448  *          save code pointer       [fp]        <- fp points to here
449  *          return link value       [fp, #-4]
450  *          return sp value         [fp, #-8]
451  *          return fp value         [fp, #-12]
452  *          [saved v7 value]
453  *          [saved v6 value]
454  *          [saved v5 value]
455  *          [saved v4 value]
456  *          [saved v3 value]
457  *          [saved v2 value]
458  *          [saved v1 value]
459  *          [saved a4 value]
460  *          [saved a3 value]
461  *          [saved a2 value]
462  *          [saved a1 value]
463  *
464  * The save code pointer points twelve bytes beyond the start of the
465  * code sequence (usually a single STM) that created the stack frame.
466  * We have to disassemble it if we want to know which of the optional
467  * fields are actually present.
468  */
469 
470 #ifndef __ARM_EABI__	/* The frame format is differend in AAPCS */
471 static void
472 db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
473 {
474 	u_int32_t	*frame, *lastframe;
475 	c_db_sym_t sym;
476 	const char *name;
477 	db_expr_t value;
478 	db_expr_t offset;
479 	int	scp_offset;
480 
481 	frame = (u_int32_t *)addr;
482 	lastframe = NULL;
483 	scp_offset = -(get_pc_str_offset() >> 2);
484 
485 	while (count-- && frame != NULL && !db_pager_quit) {
486 		db_addr_t	scp;
487 		u_int32_t	savecode;
488 		int		r;
489 		u_int32_t	*rp;
490 		const char	*sep;
491 
492 		/*
493 		 * In theory, the SCP isn't guaranteed to be in the function
494 		 * that generated the stack frame.  We hope for the best.
495 		 */
496 		scp = frame[FR_SCP];
497 
498 		sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
499 		if (sym == C_DB_SYM_NULL) {
500 			value = 0;
501 			name = "(null)";
502 		} else
503 			db_symbol_values(sym, &name, &value);
504 		db_printf("%s() at ", name);
505 		db_printsym(scp, DB_STGY_PROC);
506 		db_printf("\n");
507 #ifdef __PROG26
508 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
509 		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
510 		db_printf(")\n");
511 #else
512 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
513 		db_printsym(frame[FR_RLV], DB_STGY_PROC);
514 		db_printf(")\n");
515 #endif
516 		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
517 
518 		savecode = ((u_int32_t *)scp)[scp_offset];
519 		if ((savecode & 0x0e100000) == 0x08000000) {
520 			/* Looks like an STM */
521 			rp = frame - 4;
522 			sep = "\n\t";
523 			for (r = 10; r >= 0; r--) {
524 				if (savecode & (1 << r)) {
525 					db_printf("%sr%d=0x%08x",
526 					    sep, r, *rp--);
527 					sep = (frame - rp) % 4 == 2 ?
528 					    "\n\t" : " ";
529 				}
530 			}
531 		}
532 
533 		db_printf("\n");
534 
535 		/*
536 		 * Switch to next frame up
537 		 */
538 		if (frame[FR_RFP] == 0)
539 			break; /* Top of stack */
540 
541 		lastframe = frame;
542 		frame = (u_int32_t *)(frame[FR_RFP]);
543 
544 		if (INKERNEL((int)frame)) {
545 			/* staying in kernel */
546 			if (frame <= lastframe) {
547 				db_printf("Bad frame pointer: %p\n", frame);
548 				break;
549 			}
550 		} else if (INKERNEL((int)lastframe)) {
551 			/* switch from user to kernel */
552 			if (kernel_only)
553 				break;	/* kernel stack only */
554 		} else {
555 			/* in user */
556 			if (frame <= lastframe) {
557 				db_printf("Bad user frame pointer: %p\n",
558 					  frame);
559 				break;
560 			}
561 		}
562 	}
563 }
564 #endif
565 
566 /* XXX stubs */
567 void
568 db_md_list_watchpoints()
569 {
570 }
571 
572 int
573 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
574 {
575 	return (0);
576 }
577 
578 int
579 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
580 {
581 	return (0);
582 }
583 
584 int
585 db_trace_thread(struct thread *thr, int count)
586 {
587 #ifdef __ARM_EABI__
588 	struct unwind_state state;
589 #endif
590 	struct pcb *ctx;
591 
592 	if (thr != curthread) {
593 		ctx = kdb_thr_ctx(thr);
594 
595 #ifdef __ARM_EABI__
596 		state.registers[FP] = ctx->un_32.pcb32_r11;
597 		state.registers[SP] = ctx->un_32.pcb32_sp;
598 		state.registers[LR] = ctx->un_32.pcb32_lr;
599 		state.registers[PC] = ctx->un_32.pcb32_pc;
600 
601 		db_stack_trace_cmd(&state);
602 #else
603 		db_stack_trace_cmd(ctx->un_32.pcb32_r11, -1, TRUE);
604 #endif
605 	} else
606 		db_trace_self();
607 	return (0);
608 }
609 
610 void
611 db_trace_self(void)
612 {
613 #ifdef __ARM_EABI__
614 	struct unwind_state state;
615 	register uint32_t sp __asm__ ("sp");
616 
617 	state.registers[FP] = (uint32_t)__builtin_frame_address(0);
618 	state.registers[SP] = (uint32_t)sp;
619 	state.registers[LR] = (uint32_t)__builtin_return_address(0);
620 	state.registers[PC] = (uint32_t)db_trace_self;
621 
622 	db_stack_trace_cmd(&state);
623 #else
624 	db_addr_t addr;
625 
626 	addr = (db_addr_t)__builtin_frame_address(0);
627 	db_stack_trace_cmd(addr, -1, FALSE);
628 #endif
629 }
630