xref: /linux-6.15/arch/s390/kernel/stacktrace.c (revision cd581092)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Stack trace management functions
4  *
5  *  Copyright IBM Corp. 2006
6  */
7 
8 #include <linux/perf_event.h>
9 #include <linux/stacktrace.h>
10 #include <linux/uaccess.h>
11 #include <linux/compat.h>
12 #include <asm/stacktrace.h>
13 #include <asm/unwind.h>
14 #include <asm/kprobes.h>
15 #include <asm/ptrace.h>
16 
17 void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
18 		     struct task_struct *task, struct pt_regs *regs)
19 {
20 	struct unwind_state state;
21 	unsigned long addr;
22 
23 	unwind_for_each_frame(&state, task, regs, 0) {
24 		addr = unwind_get_return_address(&state);
25 		if (!addr || !consume_entry(cookie, addr))
26 			break;
27 	}
28 }
29 
30 int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
31 			     void *cookie, struct task_struct *task)
32 {
33 	struct unwind_state state;
34 	unsigned long addr;
35 
36 	unwind_for_each_frame(&state, task, NULL, 0) {
37 		if (state.stack_info.type != STACK_TYPE_TASK)
38 			return -EINVAL;
39 
40 		if (state.regs)
41 			return -EINVAL;
42 
43 		addr = unwind_get_return_address(&state);
44 		if (!addr)
45 			return -EINVAL;
46 
47 #ifdef CONFIG_RETHOOK
48 		/*
49 		 * Mark stacktraces with krethook functions on them
50 		 * as unreliable.
51 		 */
52 		if (state.ip == (unsigned long)arch_rethook_trampoline)
53 			return -EINVAL;
54 #endif
55 
56 		if (!consume_entry(cookie, addr))
57 			return -EINVAL;
58 	}
59 
60 	/* Check for stack corruption */
61 	if (unwind_error(&state))
62 		return -EINVAL;
63 	return 0;
64 }
65 
66 static inline bool store_ip(stack_trace_consume_fn consume_entry, void *cookie,
67 			    struct perf_callchain_entry_ctx *entry, bool perf,
68 			    unsigned long ip)
69 {
70 #ifdef CONFIG_PERF_EVENTS
71 	if (perf) {
72 		if (perf_callchain_store(entry, ip))
73 			return false;
74 		return true;
75 	}
76 #endif
77 	return consume_entry(cookie, ip);
78 }
79 
80 static inline bool ip_invalid(unsigned long ip)
81 {
82 	/*
83 	 * Perform some basic checks if an instruction address taken
84 	 * from unreliable source is invalid.
85 	 */
86 	if (ip & 1)
87 		return true;
88 	if (ip < mmap_min_addr)
89 		return true;
90 	if (ip >= current->mm->context.asce_limit)
91 		return true;
92 	return false;
93 }
94 
95 void arch_stack_walk_user_common(stack_trace_consume_fn consume_entry, void *cookie,
96 				 struct perf_callchain_entry_ctx *entry,
97 				 const struct pt_regs *regs, bool perf)
98 {
99 	struct stack_frame_user __user *sf;
100 	unsigned long ip, sp;
101 	bool first = true;
102 
103 	if (is_compat_task())
104 		return;
105 	if (!current->mm)
106 		return;
107 	ip = instruction_pointer(regs);
108 	if (!store_ip(consume_entry, cookie, entry, perf, ip))
109 		return;
110 	sf = (void __user *)user_stack_pointer(regs);
111 	pagefault_disable();
112 	while (1) {
113 		if (__get_user(sp, &sf->back_chain))
114 			break;
115 		/* Sanity check: ABI requires SP to be 8 byte aligned. */
116 		if (!sp || sp & 0x7)
117 			break;
118 		sf = (void __user *)sp;
119 		if (__get_user(ip, &sf->gprs[8]))
120 			break;
121 		if (ip_invalid(ip)) {
122 			/*
123 			 * If the instruction address is invalid, and this
124 			 * is the first stack frame, assume r14 has not
125 			 * been written to the stack yet. Otherwise exit.
126 			 */
127 			if (!first)
128 				break;
129 			ip = regs->gprs[14];
130 			if (ip_invalid(ip))
131 				break;
132 		}
133 		if (!store_ip(consume_entry, cookie, entry, perf, ip))
134 			return;
135 		first = false;
136 	}
137 	pagefault_enable();
138 }
139 
140 void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
141 			  const struct pt_regs *regs)
142 {
143 	arch_stack_walk_user_common(consume_entry, cookie, NULL, regs, false);
144 }
145 
146 unsigned long return_address(unsigned int n)
147 {
148 	struct unwind_state state;
149 	unsigned long addr;
150 
151 	/* Increment to skip current stack entry */
152 	n++;
153 
154 	unwind_for_each_frame(&state, NULL, NULL, 0) {
155 		addr = unwind_get_return_address(&state);
156 		if (!addr)
157 			break;
158 		if (!n--)
159 			return addr;
160 	}
161 	return 0;
162 }
163 EXPORT_SYMBOL_GPL(return_address);
164