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