xref: /openbmc/linux/arch/s390/kernel/unwind_bc.c (revision bf018ee6)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/sched.h>
3 #include <linux/sched/task.h>
4 #include <linux/sched/task_stack.h>
5 #include <linux/interrupt.h>
6 #include <asm/sections.h>
7 #include <asm/ptrace.h>
8 #include <asm/bitops.h>
9 #include <asm/stacktrace.h>
10 #include <asm/unwind.h>
11 
12 unsigned long unwind_get_return_address(struct unwind_state *state)
13 {
14 	if (unwind_done(state))
15 		return 0;
16 	return __kernel_text_address(state->ip) ? state->ip : 0;
17 }
18 EXPORT_SYMBOL_GPL(unwind_get_return_address);
19 
20 static bool outside_of_stack(struct unwind_state *state, unsigned long sp)
21 {
22 	return (sp <= state->sp) ||
23 		(sp > state->stack_info.end - sizeof(struct stack_frame));
24 }
25 
26 static bool update_stack_info(struct unwind_state *state, unsigned long sp)
27 {
28 	struct stack_info *info = &state->stack_info;
29 	unsigned long *mask = &state->stack_mask;
30 
31 	/* New stack pointer leaves the current stack */
32 	if (get_stack_info(sp, state->task, info, mask) != 0 ||
33 	    !on_stack(info, sp, sizeof(struct stack_frame)))
34 		/* 'sp' does not point to a valid stack */
35 		return false;
36 	return true;
37 }
38 
39 static inline bool is_task_pt_regs(struct unwind_state *state,
40 				   struct pt_regs *regs)
41 {
42 	return task_pt_regs(state->task) == regs;
43 }
44 
45 bool unwind_next_frame(struct unwind_state *state)
46 {
47 	struct stack_info *info = &state->stack_info;
48 	struct stack_frame *sf;
49 	struct pt_regs *regs;
50 	unsigned long sp, ip;
51 	bool reliable;
52 
53 	regs = state->regs;
54 	if (unlikely(regs)) {
55 		sp = state->sp;
56 		sf = (struct stack_frame *) sp;
57 		ip = READ_ONCE_NOCHECK(sf->gprs[8]);
58 		reliable = false;
59 		regs = NULL;
60 		if (!__kernel_text_address(ip)) {
61 			/* skip bogus %r14 */
62 			state->regs = NULL;
63 			return unwind_next_frame(state);
64 		}
65 	} else {
66 		sf = (struct stack_frame *) state->sp;
67 		sp = READ_ONCE_NOCHECK(sf->back_chain);
68 		if (likely(sp)) {
69 			/* Non-zero back-chain points to the previous frame */
70 			if (unlikely(outside_of_stack(state, sp))) {
71 				if (!update_stack_info(state, sp))
72 					goto out_err;
73 			}
74 			sf = (struct stack_frame *) sp;
75 			ip = READ_ONCE_NOCHECK(sf->gprs[8]);
76 			reliable = true;
77 		} else {
78 			/* No back-chain, look for a pt_regs structure */
79 			sp = state->sp + STACK_FRAME_OVERHEAD;
80 			if (!on_stack(info, sp, sizeof(struct pt_regs)))
81 				goto out_err;
82 			regs = (struct pt_regs *) sp;
83 			if (is_task_pt_regs(state, regs))
84 				goto out_stop;
85 			ip = READ_ONCE_NOCHECK(regs->psw.addr);
86 			sp = READ_ONCE_NOCHECK(regs->gprs[15]);
87 			if (unlikely(outside_of_stack(state, sp))) {
88 				if (!update_stack_info(state, sp))
89 					goto out_err;
90 			}
91 			reliable = true;
92 		}
93 	}
94 
95 	ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *) sp);
96 
97 	/* Update unwind state */
98 	state->sp = sp;
99 	state->ip = ip;
100 	state->regs = regs;
101 	state->reliable = reliable;
102 	return true;
103 
104 out_err:
105 	state->error = true;
106 out_stop:
107 	state->stack_info.type = STACK_TYPE_UNKNOWN;
108 	return false;
109 }
110 EXPORT_SYMBOL_GPL(unwind_next_frame);
111 
112 void __unwind_start(struct unwind_state *state, struct task_struct *task,
113 		    struct pt_regs *regs, unsigned long first_frame)
114 {
115 	struct stack_info *info = &state->stack_info;
116 	struct stack_frame *sf;
117 	unsigned long ip, sp;
118 
119 	memset(state, 0, sizeof(*state));
120 	state->task = task;
121 	state->regs = regs;
122 
123 	/* Don't even attempt to start from user mode regs: */
124 	if (regs && user_mode(regs)) {
125 		info->type = STACK_TYPE_UNKNOWN;
126 		return;
127 	}
128 
129 	/* Get the instruction pointer from pt_regs or the stack frame */
130 	if (regs) {
131 		ip = regs->psw.addr;
132 		sp = regs->gprs[15];
133 	} else if (task == current) {
134 		sp = current_frame_address();
135 	} else {
136 		sp = task->thread.ksp;
137 	}
138 
139 	/* Get current stack pointer and initialize stack info */
140 	if (!update_stack_info(state, sp)) {
141 		/* Something is wrong with the stack pointer */
142 		info->type = STACK_TYPE_UNKNOWN;
143 		state->error = true;
144 		return;
145 	}
146 
147 	if (!regs) {
148 		/* Stack frame is within valid stack */
149 		sf = (struct stack_frame *)sp;
150 		ip = READ_ONCE_NOCHECK(sf->gprs[8]);
151 	}
152 
153 	ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, NULL);
154 
155 	/* Update unwind state */
156 	state->sp = sp;
157 	state->ip = ip;
158 	state->reliable = true;
159 
160 	if (!first_frame)
161 		return;
162 	/* Skip through the call chain to the specified starting frame */
163 	while (!unwind_done(state)) {
164 		if (on_stack(&state->stack_info, first_frame, sizeof(struct stack_frame))) {
165 			if (state->sp >= first_frame)
166 				break;
167 		}
168 		unwind_next_frame(state);
169 	}
170 }
171 EXPORT_SYMBOL_GPL(__unwind_start);
172