0001
0002 #include <linux/sched.h>
0003 #include <linux/sched/task.h>
0004 #include <linux/sched/task_stack.h>
0005 #include <linux/interrupt.h>
0006 #include <asm/sections.h>
0007 #include <asm/ptrace.h>
0008 #include <asm/bitops.h>
0009 #include <asm/stacktrace.h>
0010 #include <asm/unwind.h>
0011
0012 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
0013
0014 unsigned long unwind_get_return_address(struct unwind_state *state)
0015 {
0016 if (unwind_done(state))
0017 return 0;
0018
0019 return __kernel_text_address(state->ip) ? state->ip : 0;
0020 }
0021 EXPORT_SYMBOL_GPL(unwind_get_return_address);
0022
0023 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
0024 {
0025 if (unwind_done(state))
0026 return NULL;
0027
0028 return state->regs ? &state->regs->ip : state->bp + 1;
0029 }
0030
0031 static void unwind_dump(struct unwind_state *state)
0032 {
0033 static bool dumped_before = false;
0034 bool prev_zero, zero = false;
0035 unsigned long word, *sp;
0036 struct stack_info stack_info = {0};
0037 unsigned long visit_mask = 0;
0038
0039 if (dumped_before)
0040 return;
0041
0042 dumped_before = true;
0043
0044 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
0045 state->stack_info.type, state->stack_info.next_sp,
0046 state->stack_mask, state->graph_idx);
0047
0048 for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
0049 sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
0050 if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
0051 break;
0052
0053 for (; sp < stack_info.end; sp++) {
0054
0055 word = READ_ONCE_NOCHECK(*sp);
0056
0057 prev_zero = zero;
0058 zero = word == 0;
0059
0060 if (zero) {
0061 if (!prev_zero)
0062 printk_deferred("%p: %0*x ...\n",
0063 sp, BITS_PER_LONG/4, 0);
0064 continue;
0065 }
0066
0067 printk_deferred("%p: %0*lx (%pB)\n",
0068 sp, BITS_PER_LONG/4, word, (void *)word);
0069 }
0070 }
0071 }
0072
0073 static bool in_entry_code(unsigned long ip)
0074 {
0075 char *addr = (char *)ip;
0076
0077 return addr >= __entry_text_start && addr < __entry_text_end;
0078 }
0079
0080 static inline unsigned long *last_frame(struct unwind_state *state)
0081 {
0082 return (unsigned long *)task_pt_regs(state->task) - 2;
0083 }
0084
0085 static bool is_last_frame(struct unwind_state *state)
0086 {
0087 return state->bp == last_frame(state);
0088 }
0089
0090 #ifdef CONFIG_X86_32
0091 #define GCC_REALIGN_WORDS 3
0092 #else
0093 #define GCC_REALIGN_WORDS 1
0094 #endif
0095
0096 static inline unsigned long *last_aligned_frame(struct unwind_state *state)
0097 {
0098 return last_frame(state) - GCC_REALIGN_WORDS;
0099 }
0100
0101 static bool is_last_aligned_frame(struct unwind_state *state)
0102 {
0103 unsigned long *last_bp = last_frame(state);
0104 unsigned long *aligned_bp = last_aligned_frame(state);
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129 return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
0130 }
0131
0132 static bool is_last_ftrace_frame(struct unwind_state *state)
0133 {
0134 unsigned long *last_bp = last_frame(state);
0135 unsigned long *last_ftrace_bp = last_bp - 3;
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 return (state->bp == last_ftrace_bp &&
0150 *state->bp == *(state->bp + 2) &&
0151 *(state->bp + 1) == *(state->bp + 4));
0152 }
0153
0154 static bool is_last_task_frame(struct unwind_state *state)
0155 {
0156 return is_last_frame(state) || is_last_aligned_frame(state) ||
0157 is_last_ftrace_frame(state);
0158 }
0159
0160
0161
0162
0163
0164 #ifdef CONFIG_X86_64
0165 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
0166 {
0167 unsigned long regs = (unsigned long)bp;
0168
0169 if (!(regs & 0x1))
0170 return NULL;
0171
0172 return (struct pt_regs *)(regs & ~0x1);
0173 }
0174 #else
0175 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
0176 {
0177 unsigned long regs = (unsigned long)bp;
0178
0179 if (regs & 0x80000000)
0180 return NULL;
0181
0182 return (struct pt_regs *)(regs | 0x80000000);
0183 }
0184 #endif
0185
0186 static bool update_stack_state(struct unwind_state *state,
0187 unsigned long *next_bp)
0188 {
0189 struct stack_info *info = &state->stack_info;
0190 enum stack_type prev_type = info->type;
0191 struct pt_regs *regs;
0192 unsigned long *frame, *prev_frame_end, *addr_p, addr;
0193 size_t len;
0194
0195 if (state->regs)
0196 prev_frame_end = (void *)state->regs + sizeof(*state->regs);
0197 else
0198 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
0199
0200
0201 regs = decode_frame_pointer(next_bp);
0202 if (regs) {
0203 frame = (unsigned long *)regs;
0204 len = sizeof(*regs);
0205 state->got_irq = true;
0206 } else {
0207 frame = next_bp;
0208 len = FRAME_HEADER_SIZE;
0209 }
0210
0211
0212
0213
0214
0215
0216
0217
0218 while (!on_stack(info, frame, len))
0219 if (get_stack_info(info->next_sp, state->task, info,
0220 &state->stack_mask))
0221 return false;
0222
0223
0224 if (state->orig_sp && state->stack_info.type == prev_type &&
0225 frame < prev_frame_end)
0226 return false;
0227
0228
0229 if (regs) {
0230 state->regs = regs;
0231 state->bp = NULL;
0232 } else {
0233 state->bp = next_bp;
0234 state->regs = NULL;
0235 }
0236
0237
0238 if (state->regs && user_mode(state->regs))
0239 state->ip = 0;
0240 else {
0241 addr_p = unwind_get_return_address_ptr(state);
0242 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
0243 state->ip = unwind_recover_ret_addr(state, addr, addr_p);
0244 }
0245
0246
0247 if (!state->orig_sp)
0248 state->orig_sp = frame;
0249
0250 return true;
0251 }
0252
0253 bool unwind_next_frame(struct unwind_state *state)
0254 {
0255 struct pt_regs *regs;
0256 unsigned long *next_bp;
0257
0258 if (unwind_done(state))
0259 return false;
0260
0261
0262 if (state->regs && user_mode(state->regs))
0263 goto the_end;
0264
0265 if (is_last_task_frame(state)) {
0266 regs = task_pt_regs(state->task);
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 if (!user_mode(regs))
0280 goto the_end;
0281
0282
0283
0284
0285
0286
0287 state->regs = regs;
0288 state->bp = NULL;
0289 state->ip = 0;
0290 return true;
0291 }
0292
0293
0294 if (state->next_bp) {
0295 next_bp = state->next_bp;
0296 state->next_bp = NULL;
0297 } else if (state->regs) {
0298 next_bp = (unsigned long *)state->regs->bp;
0299 } else {
0300 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
0301 }
0302
0303
0304 if (!update_stack_state(state, next_bp))
0305 goto bad_address;
0306
0307 return true;
0308
0309 bad_address:
0310 state->error = true;
0311
0312
0313
0314
0315
0316
0317
0318
0319 if (state->task != current)
0320 goto the_end;
0321
0322
0323
0324
0325
0326 if (state->got_irq && in_entry_code(state->ip))
0327 goto the_end;
0328 if (state->regs &&
0329 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
0330 state->regs->sp < (unsigned long)task_pt_regs(state->task))
0331 goto the_end;
0332
0333
0334
0335
0336
0337 if (IS_ENABLED(CONFIG_X86_32))
0338 goto the_end;
0339
0340 if (state->task != current)
0341 goto the_end;
0342
0343 if (state->regs) {
0344 printk_deferred_once(KERN_WARNING
0345 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
0346 state->regs, state->task->comm,
0347 state->task->pid, next_bp);
0348 unwind_dump(state);
0349 } else {
0350 printk_deferred_once(KERN_WARNING
0351 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
0352 state->bp, state->task->comm,
0353 state->task->pid, next_bp);
0354 unwind_dump(state);
0355 }
0356 the_end:
0357 state->stack_info.type = STACK_TYPE_UNKNOWN;
0358 return false;
0359 }
0360 EXPORT_SYMBOL_GPL(unwind_next_frame);
0361
0362 void __unwind_start(struct unwind_state *state, struct task_struct *task,
0363 struct pt_regs *regs, unsigned long *first_frame)
0364 {
0365 unsigned long *bp;
0366
0367 memset(state, 0, sizeof(*state));
0368 state->task = task;
0369 state->got_irq = (regs);
0370
0371
0372 if (regs && user_mode(regs)) {
0373 state->stack_info.type = STACK_TYPE_UNKNOWN;
0374 return;
0375 }
0376
0377 bp = get_frame_pointer(task, regs);
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
0389 state->next_bp = bp;
0390 bp = ((unsigned long *)regs->sp) - 1;
0391 }
0392
0393
0394 get_stack_info(bp, state->task, &state->stack_info,
0395 &state->stack_mask);
0396 update_stack_state(state, bp);
0397
0398
0399
0400
0401
0402
0403 while (!unwind_done(state) &&
0404 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
0405 (state->next_bp == NULL && state->bp < first_frame)))
0406 unwind_next_frame(state);
0407 }
0408 EXPORT_SYMBOL_GPL(__unwind_start);