~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/xtensa/kernel/stacktrace.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Kernel and userspace stack tracing.
  3  *
  4  * This file is subject to the terms and conditions of the GNU General Public
  5  * License.  See the file "COPYING" in the main directory of this archive
  6  * for more details.
  7  *
  8  * Copyright (C) 2001 - 2013 Tensilica Inc.
  9  * Copyright (C) 2015 Cadence Design Systems Inc.
 10  */
 11 #include <linux/export.h>
 12 #include <linux/sched.h>
 13 #include <linux/stacktrace.h>
 14 
 15 #include <asm/ftrace.h>
 16 #include <asm/sections.h>
 17 #include <asm/stacktrace.h>
 18 #include <asm/traps.h>
 19 #include <linux/uaccess.h>
 20 
 21 #if IS_ENABLED(CONFIG_PERF_EVENTS)
 22 
 23 /* Address of common_exception_return, used to check the
 24  * transition from kernel to user space.
 25  */
 26 extern int common_exception_return;
 27 
 28 void xtensa_backtrace_user(struct pt_regs *regs, unsigned int depth,
 29                            int (*ufn)(struct stackframe *frame, void *data),
 30                            void *data)
 31 {
 32         unsigned long windowstart = regs->windowstart;
 33         unsigned long windowbase = regs->windowbase;
 34         unsigned long a0 = regs->areg[0];
 35         unsigned long a1 = regs->areg[1];
 36         unsigned long pc = regs->pc;
 37         struct stackframe frame;
 38         int index;
 39 
 40         if (!depth--)
 41                 return;
 42 
 43         frame.pc = pc;
 44         frame.sp = a1;
 45 
 46         if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
 47                 return;
 48 
 49         if (IS_ENABLED(CONFIG_USER_ABI_CALL0_ONLY) ||
 50             (IS_ENABLED(CONFIG_USER_ABI_CALL0_PROBE) &&
 51              !(regs->ps & PS_WOE_MASK)))
 52                 return;
 53 
 54         /* Two steps:
 55          *
 56          * 1. Look through the register window for the
 57          * previous PCs in the call trace.
 58          *
 59          * 2. Look on the stack.
 60          */
 61 
 62         /* Step 1.  */
 63         /* Rotate WINDOWSTART to move the bit corresponding to
 64          * the current window to the bit #0.
 65          */
 66         windowstart = (windowstart << WSBITS | windowstart) >> windowbase;
 67 
 68         /* Look for bits that are set, they correspond to
 69          * valid windows.
 70          */
 71         for (index = WSBITS - 1; (index > 0) && depth; depth--, index--)
 72                 if (windowstart & (1 << index)) {
 73                         /* Get the PC from a0 and a1. */
 74                         pc = MAKE_PC_FROM_RA(a0, pc);
 75                         /* Read a0 and a1 from the
 76                          * corresponding position in AREGs.
 77                          */
 78                         a0 = regs->areg[index * 4];
 79                         a1 = regs->areg[index * 4 + 1];
 80 
 81                         frame.pc = pc;
 82                         frame.sp = a1;
 83 
 84                         if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
 85                                 return;
 86                 }
 87 
 88         /* Step 2. */
 89         /* We are done with the register window, we need to
 90          * look through the stack.
 91          */
 92         if (!depth)
 93                 return;
 94 
 95         /* Start from the a1 register. */
 96         /* a1 = regs->areg[1]; */
 97         while (a0 != 0 && depth--) {
 98                 pc = MAKE_PC_FROM_RA(a0, pc);
 99 
100                 /* Check if the region is OK to access. */
101                 if (!access_ok(&SPILL_SLOT(a1, 0), 8))
102                         return;
103                 /* Copy a1, a0 from user space stack frame. */
104                 if (__get_user(a0, &SPILL_SLOT(a1, 0)) ||
105                     __get_user(a1, &SPILL_SLOT(a1, 1)))
106                         return;
107 
108                 frame.pc = pc;
109                 frame.sp = a1;
110 
111                 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
112                         return;
113         }
114 }
115 EXPORT_SYMBOL(xtensa_backtrace_user);
116 
117 void xtensa_backtrace_kernel(struct pt_regs *regs, unsigned int depth,
118                              int (*kfn)(struct stackframe *frame, void *data),
119                              int (*ufn)(struct stackframe *frame, void *data),
120                              void *data)
121 {
122         unsigned long pc = regs->depc > VALID_DOUBLE_EXCEPTION_ADDRESS ?
123                 regs->depc : regs->pc;
124         unsigned long sp_start, sp_end;
125         unsigned long a0 = regs->areg[0];
126         unsigned long a1 = regs->areg[1];
127 
128         sp_start = a1 & ~(THREAD_SIZE - 1);
129         sp_end = sp_start + THREAD_SIZE;
130 
131         /* Spill the register window to the stack first. */
132         spill_registers();
133 
134         /* Read the stack frames one by one and create the PC
135          * from the a0 and a1 registers saved there.
136          */
137         while (a1 > sp_start && a1 < sp_end && depth--) {
138                 struct stackframe frame;
139 
140                 frame.pc = pc;
141                 frame.sp = a1;
142 
143                 if (kernel_text_address(pc) && kfn(&frame, data))
144                         return;
145 
146                 if (pc == (unsigned long)&common_exception_return) {
147                         regs = (struct pt_regs *)a1;
148                         if (user_mode(regs)) {
149                                 if (ufn == NULL)
150                                         return;
151                                 xtensa_backtrace_user(regs, depth, ufn, data);
152                                 return;
153                         }
154                         a0 = regs->areg[0];
155                         a1 = regs->areg[1];
156                         continue;
157                 }
158 
159                 sp_start = a1;
160 
161                 pc = MAKE_PC_FROM_RA(a0, pc);
162                 a0 = SPILL_SLOT(a1, 0);
163                 a1 = SPILL_SLOT(a1, 1);
164         }
165 }
166 EXPORT_SYMBOL(xtensa_backtrace_kernel);
167 
168 #endif
169 
170 void walk_stackframe(unsigned long *sp,
171                 int (*fn)(struct stackframe *frame, void *data),
172                 void *data)
173 {
174         unsigned long a0, a1;
175         unsigned long sp_end;
176 
177         a1 = (unsigned long)sp;
178         sp_end = ALIGN(a1, THREAD_SIZE);
179 
180         spill_registers();
181 
182         while (a1 < sp_end) {
183                 struct stackframe frame;
184 
185                 sp = (unsigned long *)a1;
186 
187                 a0 = SPILL_SLOT(a1, 0);
188                 a1 = SPILL_SLOT(a1, 1);
189 
190                 if (a1 <= (unsigned long)sp)
191                         break;
192 
193                 frame.pc = MAKE_PC_FROM_RA(a0, _text);
194                 frame.sp = a1;
195 
196                 if (fn(&frame, data))
197                         return;
198         }
199 }
200 
201 #ifdef CONFIG_STACKTRACE
202 
203 struct stack_trace_data {
204         struct stack_trace *trace;
205         unsigned skip;
206 };
207 
208 static int stack_trace_cb(struct stackframe *frame, void *data)
209 {
210         struct stack_trace_data *trace_data = data;
211         struct stack_trace *trace = trace_data->trace;
212 
213         if (trace_data->skip) {
214                 --trace_data->skip;
215                 return 0;
216         }
217         if (!kernel_text_address(frame->pc))
218                 return 0;
219 
220         trace->entries[trace->nr_entries++] = frame->pc;
221         return trace->nr_entries >= trace->max_entries;
222 }
223 
224 void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
225 {
226         struct stack_trace_data trace_data = {
227                 .trace = trace,
228                 .skip = trace->skip,
229         };
230         walk_stackframe(stack_pointer(task), stack_trace_cb, &trace_data);
231 }
232 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
233 
234 void save_stack_trace(struct stack_trace *trace)
235 {
236         save_stack_trace_tsk(current, trace);
237 }
238 EXPORT_SYMBOL_GPL(save_stack_trace);
239 
240 #endif
241 
242 struct return_addr_data {
243         unsigned long addr;
244         unsigned skip;
245 };
246 
247 static int return_address_cb(struct stackframe *frame, void *data)
248 {
249         struct return_addr_data *r = data;
250 
251         if (r->skip) {
252                 --r->skip;
253                 return 0;
254         }
255         if (!kernel_text_address(frame->pc))
256                 return 0;
257         r->addr = frame->pc;
258         return 1;
259 }
260 
261 /*
262  * level == 0 is for the return address from the caller of this function,
263  * not from this function itself.
264  */
265 unsigned long return_address(unsigned level)
266 {
267         struct return_addr_data r = {
268                 .skip = level,
269         };
270         walk_stackframe(stack_pointer(NULL), return_address_cb, &r);
271         return r.addr;
272 }
273 EXPORT_SYMBOL(return_address);
274 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php