xref: /openbmc/linux/arch/alpha/lib/stacktrace.c (revision a72b9869)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 
4 typedef unsigned int instr;
5 
6 #define MAJOR_OP	0xfc000000
7 #define LDA_OP		0x20000000
8 #define STQ_OP		0xb4000000
9 #define BR_OP		0xc0000000
10 
11 #define STK_ALLOC_1	0x23de8000 /* lda $30,-X($30) */
12 #define STK_ALLOC_1M	0xffff8000
13 #define STK_ALLOC_2	0x43c0153e /* subq $30,X,$30 */
14 #define STK_ALLOC_2M	0xffe01fff
15 
16 #define MEM_REG		0x03e00000
17 #define MEM_BASE	0x001f0000
18 #define MEM_OFF		0x0000ffff
19 #define MEM_OFF_SIGN	0x00008000
20 #define	BASE_SP		0x001e0000
21 
22 #define STK_ALLOC_MATCH(INSTR)			\
23   (((INSTR) & STK_ALLOC_1M) == STK_ALLOC_1	\
24    || ((INSTR) & STK_ALLOC_2M) == STK_ALLOC_2)
25 #define STK_PUSH_MATCH(INSTR) \
26   (((INSTR) & (MAJOR_OP | MEM_BASE | MEM_OFF_SIGN)) == (STQ_OP | BASE_SP))
27 #define MEM_OP_OFFSET(INSTR) \
28   (((long)((INSTR) & MEM_OFF) << 48) >> 48)
29 #define MEM_OP_REG(INSTR) \
30   (((INSTR) & MEM_REG) >> 22)
31 
32 /* Branches, jumps, PAL calls, and illegal opcodes end a basic block. */
33 #define BB_END(INSTR)						\
34   (((instr)(INSTR) >= BR_OP) | ((instr)(INSTR) < LDA_OP) |	\
35    ((((instr)(INSTR) ^ 0x60000000) < 0x20000000) &		\
36     (((instr)(INSTR) & 0x0c000000) != 0)))
37 
38 #define IS_KERNEL_TEXT(PC) ((unsigned long)(PC) > START_ADDR)
39 
40 static char reg_name[][4] = {
41 	"v0 ", "t0 ", "t1 ", "t2 ", "t3 ", "t4 ", "t5 ", "t6 ", "t7 ",
42 	"s0 ", "s1 ", "s2 ", "s3 ", "s4 ", "s5 ", "s6 ", "a0 ", "a1 ",
43 	"a2 ", "a3 ", "a4 ", "a5 ", "t8 ", "t9 ", "t10", "t11", "ra ",
44 	"pv ", "at ", "gp ", "sp ", "0"
45 };
46 
47 
48 static instr *
49 display_stored_regs(instr * pro_pc, unsigned char * sp)
50 {
51 	instr * ret_pc = 0;
52 	int reg;
53 	unsigned long value;
54 
55 	printk("Prologue [<%p>], Frame %p:\n", pro_pc, sp);
56 	while (!BB_END(*pro_pc))
57 		if (STK_PUSH_MATCH(*pro_pc)) {
58 			reg = (*pro_pc & MEM_REG) >> 21;
59 			value = *(unsigned long *)(sp + (*pro_pc & MEM_OFF));
60 			if (reg == 26)
61 				ret_pc = (instr *)value;
62 			printk("\t\t%s / 0x%016lx\n", reg_name[reg], value);
63 		}
64 	return ret_pc;
65 }
66 
67 static instr *
68 seek_prologue(instr * pc)
69 {
70 	while (!STK_ALLOC_MATCH(*pc))
71 		--pc;
72 	while (!BB_END(*(pc - 1)))
73 		--pc;
74 	return pc;
75 }
76 
77 static long
78 stack_increment(instr * prologue_pc)
79 {
80 	while (!STK_ALLOC_MATCH(*prologue_pc))
81 		++prologue_pc;
82 
83 	/* Count the bytes allocated. */
84 	if ((*prologue_pc & STK_ALLOC_1M) == STK_ALLOC_1M)
85 		return -(((long)(*prologue_pc) << 48) >> 48);
86 	else
87 		return (*prologue_pc >> 13) & 0xff;
88 }
89 
90 void
91 stacktrace(void)
92 {
93 	instr * ret_pc;
94 	instr * prologue = (instr *)stacktrace;
95 	unsigned char *sp = (unsigned char *)current_stack_pointer;
96 
97 	printk("\tstack trace:\n");
98 	do {
99 		ret_pc = display_stored_regs(prologue, sp);
100 		sp += stack_increment(prologue);
101 		prologue = seek_prologue(ret_pc);
102 	} while (IS_KERNEL_TEXT(ret_pc));
103 }
104