^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/unwind.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define FRAME_HEADER_SIZE (sizeof(long) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) unsigned long unwind_get_return_address(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return __kernel_text_address(state->ip) ? state->ip : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) EXPORT_SYMBOL_GPL(unwind_get_return_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return state->regs ? &state->regs->ip : state->bp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void unwind_dump(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static bool dumped_before = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) bool prev_zero, zero = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned long word, *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct stack_info stack_info = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long visit_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (dumped_before)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) dumped_before = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) state->stack_info.type, state->stack_info.next_sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) state->stack_mask, state->graph_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (; sp < stack_info.end; sp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) word = READ_ONCE_NOCHECK(*sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) prev_zero = zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) zero = word == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!prev_zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) printk_deferred("%p: %0*x ...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) sp, BITS_PER_LONG/4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) printk_deferred("%p: %0*lx (%pB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) sp, BITS_PER_LONG/4, word, (void *)word);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static bool in_entry_code(unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) char *addr = (char *)ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return addr >= __entry_text_start && addr < __entry_text_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static inline unsigned long *last_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return (unsigned long *)task_pt_regs(state->task) - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static bool is_last_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return state->bp == last_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define GCC_REALIGN_WORDS 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define GCC_REALIGN_WORDS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static inline unsigned long *last_aligned_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return last_frame(state) - GCC_REALIGN_WORDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static bool is_last_aligned_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned long *last_bp = last_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned long *aligned_bp = last_aligned_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * GCC can occasionally decide to realign the stack pointer and change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * the offset of the stack frame in the prologue of a function called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * by head/entry code. Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * <start_secondary>:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * push %edi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * lea 0x8(%esp),%edi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * and $0xfffffff8,%esp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * pushl -0x4(%edi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * push %ebp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * mov %esp,%ebp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * <x86_64_start_kernel>:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * lea 0x8(%rsp),%r10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * and $0xfffffffffffffff0,%rsp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * pushq -0x8(%r10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * push %rbp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * mov %rsp,%rbp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * After aligning the stack, it pushes a duplicate copy of the return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * address before pushing the frame pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static bool is_last_ftrace_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) unsigned long *last_bp = last_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned long *last_ftrace_bp = last_bp - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * When unwinding from an ftrace handler of a function called by entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * code, the stack layout of the last frame is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * bp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * parent ret addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * bp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * function ret addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * parent ret addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * pt_regs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * -----------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return (state->bp == last_ftrace_bp &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *state->bp == *(state->bp + 2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *(state->bp + 1) == *(state->bp + 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static bool is_last_task_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return is_last_frame(state) || is_last_aligned_frame(state) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) is_last_ftrace_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * This determines if the frame pointer actually contains an encoded pointer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * pt_regs on the stack. See ENCODE_FRAME_POINTER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct pt_regs *decode_frame_pointer(unsigned long *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) unsigned long regs = (unsigned long)bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!(regs & 0x1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return (struct pt_regs *)(regs & ~0x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static struct pt_regs *decode_frame_pointer(unsigned long *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned long regs = (unsigned long)bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (regs & 0x80000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return (struct pt_regs *)(regs | 0x80000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static bool update_stack_state(struct unwind_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned long *next_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct stack_info *info = &state->stack_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) enum stack_type prev_type = info->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) unsigned long *frame, *prev_frame_end, *addr_p, addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (state->regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) prev_frame_end = (void *)state->regs + sizeof(*state->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* Is the next frame pointer an encoded pointer to pt_regs? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) regs = decode_frame_pointer(next_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) frame = (unsigned long *)regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) len = sizeof(*regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) state->got_irq = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) frame = next_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) len = FRAME_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * If the next bp isn't on the current stack, switch to the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * We may have to traverse multiple stacks to deal with the possibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * that info->next_sp could point to an empty stack and the next bp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * could be on a subsequent stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) while (!on_stack(info, frame, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (get_stack_info(info->next_sp, state->task, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) &state->stack_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* Make sure it only unwinds up and doesn't overlap the prev frame: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (state->orig_sp && state->stack_info.type == prev_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) frame < prev_frame_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* Move state to the next frame: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) state->regs = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) state->bp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) state->bp = next_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) state->regs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* Save the return address: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (state->regs && user_mode(state->regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) state->ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) addr_p = unwind_get_return_address_ptr(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) addr, addr_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* Save the original stack pointer for unwind_dump(): */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (!state->orig_sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) state->orig_sp = frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) bool unwind_next_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) unsigned long *next_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Have we reached the end? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (state->regs && user_mode(state->regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (is_last_task_frame(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) regs = task_pt_regs(state->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * kthreads (other than the boot CPU's idle thread) have some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * partial regs at the end of their stack which were placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * there by copy_thread(). But the regs don't have any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * useful information, so we can skip them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * This user_mode() check is slightly broader than a PF_KTHREAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * check because it also catches the awkward situation where a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * newly forked kthread transitions into a user task by calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * kernel_execve(), which eventually clears PF_KTHREAD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!user_mode(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * We're almost at the end, but not quite: there's still the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * syscall regs frame. Entry code doesn't encode the regs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * pointer for syscalls, so we have to set it manually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) state->regs = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) state->bp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) state->ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Get the next frame pointer: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (state->next_bp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) next_bp = state->next_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) state->next_bp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) } else if (state->regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) next_bp = (unsigned long *)state->regs->bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* Move to the next frame if it's safe: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!update_stack_state(state, next_bp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto bad_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) bad_address:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) state->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * When unwinding a non-current task, the task might actually be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * running on another CPU, in which case it could be modifying its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * stack while we're reading it. This is generally not a problem and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * can be ignored as long as the caller understands that unwinding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * another task will not always succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (state->task != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Don't warn if the unwinder got lost due to an interrupt in entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * code or in the C handler before the first frame pointer got set up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (state->got_irq && in_entry_code(state->ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (state->regs &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) state->regs->sp < (unsigned long)task_pt_regs(state->task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * There are some known frame pointer issues on 32-bit. Disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * unwinder warnings on 32-bit until it gets objtool support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (IS_ENABLED(CONFIG_X86_32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (state->task != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) goto the_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (state->regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) printk_deferred_once(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) state->regs, state->task->comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) state->task->pid, next_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) unwind_dump(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) printk_deferred_once(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) state->bp, state->task->comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) state->task->pid, next_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) unwind_dump(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) the_end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) state->stack_info.type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) EXPORT_SYMBOL_GPL(unwind_next_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) void __unwind_start(struct unwind_state *state, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct pt_regs *regs, unsigned long *first_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) unsigned long *bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) memset(state, 0, sizeof(*state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) state->task = task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) state->got_irq = (regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /* Don't even attempt to start from user mode regs: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (regs && user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) state->stack_info.type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) bp = get_frame_pointer(task, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * If we crash with IP==0, the last successfully executed instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * was probably an indirect function call with a NULL function pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * That means that SP points into the middle of an incomplete frame:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * would have written a frame pointer if we hadn't crashed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Pretend that the frame is complete and that BP points to it, but save
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * the real BP so that we can use it when looking for the next frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) state->next_bp = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) bp = ((unsigned long *)regs->sp) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* Initialize stack info and make sure the frame data is accessible: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) get_stack_info(bp, state->task, &state->stack_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) &state->stack_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) update_stack_state(state, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * The caller can provide the address of the first frame directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * (first_frame) or indirectly (regs->sp) to indicate which stack frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * to start unwinding at. Skip ahead until we reach it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) while (!unwind_done(state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) (state->next_bp == NULL && state->bp < first_frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) unwind_next_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) EXPORT_SYMBOL_GPL(__unwind_start);