Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
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      * GCC can occasionally decide to realign the stack pointer and change
0108      * the offset of the stack frame in the prologue of a function called
0109      * by head/entry code.  Examples:
0110      *
0111      * <start_secondary>:
0112      *      push   %edi
0113      *      lea    0x8(%esp),%edi
0114      *      and    $0xfffffff8,%esp
0115      *      pushl  -0x4(%edi)
0116      *      push   %ebp
0117      *      mov    %esp,%ebp
0118      *
0119      * <x86_64_start_kernel>:
0120      *      lea    0x8(%rsp),%r10
0121      *      and    $0xfffffffffffffff0,%rsp
0122      *      pushq  -0x8(%r10)
0123      *      push   %rbp
0124      *      mov    %rsp,%rbp
0125      *
0126      * After aligning the stack, it pushes a duplicate copy of the return
0127      * address before pushing the frame pointer.
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      * When unwinding from an ftrace handler of a function called by entry
0139      * code, the stack layout of the last frame is:
0140      *
0141      *   bp
0142      *   parent ret addr
0143      *   bp
0144      *   function ret addr
0145      *   parent ret addr
0146      *   pt_regs
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  * This determines if the frame pointer actually contains an encoded pointer to
0162  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
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     /* Is the next frame pointer an encoded pointer to pt_regs? */
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      * If the next bp isn't on the current stack, switch to the next one.
0213      *
0214      * We may have to traverse multiple stacks to deal with the possibility
0215      * that info->next_sp could point to an empty stack and the next bp
0216      * could be on a subsequent stack.
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     /* Make sure it only unwinds up and doesn't overlap the prev frame: */
0224     if (state->orig_sp && state->stack_info.type == prev_type &&
0225         frame < prev_frame_end)
0226         return false;
0227 
0228     /* Move state to the next frame: */
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     /* Save the return address: */
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     /* Save the original stack pointer for unwind_dump(): */
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     /* Have we reached the end? */
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          * kthreads (other than the boot CPU's idle thread) have some
0270          * partial regs at the end of their stack which were placed
0271          * there by copy_thread().  But the regs don't have any
0272          * useful information, so we can skip them.
0273          *
0274          * This user_mode() check is slightly broader than a PF_KTHREAD
0275          * check because it also catches the awkward situation where a
0276          * newly forked kthread transitions into a user task by calling
0277          * kernel_execve(), which eventually clears PF_KTHREAD.
0278          */
0279         if (!user_mode(regs))
0280             goto the_end;
0281 
0282         /*
0283          * We're almost at the end, but not quite: there's still the
0284          * syscall regs frame.  Entry code doesn't encode the regs
0285          * pointer for syscalls, so we have to set it manually.
0286          */
0287         state->regs = regs;
0288         state->bp = NULL;
0289         state->ip = 0;
0290         return true;
0291     }
0292 
0293     /* Get the next frame pointer: */
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     /* Move to the next frame if it's safe: */
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      * When unwinding a non-current task, the task might actually be
0314      * running on another CPU, in which case it could be modifying its
0315      * stack while we're reading it.  This is generally not a problem and
0316      * can be ignored as long as the caller understands that unwinding
0317      * another task will not always succeed.
0318      */
0319     if (state->task != current)
0320         goto the_end;
0321 
0322     /*
0323      * Don't warn if the unwinder got lost due to an interrupt in entry
0324      * code or in the C handler before the first frame pointer got set up:
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      * There are some known frame pointer issues on 32-bit.  Disable
0335      * unwinder warnings on 32-bit until it gets objtool support.
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     /* Don't even attempt to start from user mode regs: */
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      * If we crash with IP==0, the last successfully executed instruction
0381      * was probably an indirect function call with a NULL function pointer.
0382      * That means that SP points into the middle of an incomplete frame:
0383      * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
0384      * would have written a frame pointer if we hadn't crashed.
0385      * Pretend that the frame is complete and that BP points to it, but save
0386      * the real BP so that we can use it when looking for the next frame.
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     /* Initialize stack info and make sure the frame data is accessible: */
0394     get_stack_info(bp, state->task, &state->stack_info,
0395                &state->stack_mask);
0396     update_stack_state(state, bp);
0397 
0398     /*
0399      * The caller can provide the address of the first frame directly
0400      * (first_frame) or indirectly (regs->sp) to indicate which stack frame
0401      * to start unwinding at.  Skip ahead until we reach it.
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);