1 #include <linux/sched.h> 2 #include <asm/ptrace.h> 3 #include <asm/bitops.h> 4 #include <asm/stacktrace.h> 5 #include <asm/unwind.h> 6 7 #define FRAME_HEADER_SIZE (sizeof(long) * 2) 8 9 unsigned long unwind_get_return_address(struct unwind_state *state) 10 { 11 unsigned long addr; 12 unsigned long *addr_p = unwind_get_return_address_ptr(state); 13 14 if (unwind_done(state)) 15 return 0; 16 17 if (state->regs && user_mode(state->regs)) 18 return 0; 19 20 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p, 21 addr_p); 22 23 if (!__kernel_text_address(addr)) { 24 printk_deferred_once(KERN_WARNING 25 "WARNING: unrecognized kernel stack return address %p at %p in %s:%d\n", 26 (void *)addr, addr_p, state->task->comm, 27 state->task->pid); 28 return 0; 29 } 30 31 return addr; 32 } 33 EXPORT_SYMBOL_GPL(unwind_get_return_address); 34 35 static size_t regs_size(struct pt_regs *regs) 36 { 37 /* x86_32 regs from kernel mode are two words shorter: */ 38 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs)) 39 return sizeof(*regs) - 2*sizeof(long); 40 41 return sizeof(*regs); 42 } 43 44 static bool is_last_task_frame(struct unwind_state *state) 45 { 46 unsigned long bp = (unsigned long)state->bp; 47 unsigned long regs = (unsigned long)task_pt_regs(state->task); 48 49 return bp == regs - FRAME_HEADER_SIZE; 50 } 51 52 /* 53 * This determines if the frame pointer actually contains an encoded pointer to 54 * pt_regs on the stack. See ENCODE_FRAME_POINTER. 55 */ 56 static struct pt_regs *decode_frame_pointer(unsigned long *bp) 57 { 58 unsigned long regs = (unsigned long)bp; 59 60 if (!(regs & 0x1)) 61 return NULL; 62 63 return (struct pt_regs *)(regs & ~0x1); 64 } 65 66 static bool update_stack_state(struct unwind_state *state, void *addr, 67 size_t len) 68 { 69 struct stack_info *info = &state->stack_info; 70 71 /* 72 * If addr isn't on the current stack, switch to the next one. 73 * 74 * We may have to traverse multiple stacks to deal with the possibility 75 * that 'info->next_sp' could point to an empty stack and 'addr' could 76 * be on a subsequent stack. 77 */ 78 while (!on_stack(info, addr, len)) 79 if (get_stack_info(info->next_sp, state->task, info, 80 &state->stack_mask)) 81 return false; 82 83 return true; 84 } 85 86 bool unwind_next_frame(struct unwind_state *state) 87 { 88 struct pt_regs *regs; 89 unsigned long *next_bp, *next_frame; 90 size_t next_len; 91 enum stack_type prev_type = state->stack_info.type; 92 93 if (unwind_done(state)) 94 return false; 95 96 /* have we reached the end? */ 97 if (state->regs && user_mode(state->regs)) 98 goto the_end; 99 100 if (is_last_task_frame(state)) { 101 regs = task_pt_regs(state->task); 102 103 /* 104 * kthreads (other than the boot CPU's idle thread) have some 105 * partial regs at the end of their stack which were placed 106 * there by copy_thread_tls(). But the regs don't have any 107 * useful information, so we can skip them. 108 * 109 * This user_mode() check is slightly broader than a PF_KTHREAD 110 * check because it also catches the awkward situation where a 111 * newly forked kthread transitions into a user task by calling 112 * do_execve(), which eventually clears PF_KTHREAD. 113 */ 114 if (!user_mode(regs)) 115 goto the_end; 116 117 /* 118 * We're almost at the end, but not quite: there's still the 119 * syscall regs frame. Entry code doesn't encode the regs 120 * pointer for syscalls, so we have to set it manually. 121 */ 122 state->regs = regs; 123 state->bp = NULL; 124 return true; 125 } 126 127 /* get the next frame pointer */ 128 if (state->regs) 129 next_bp = (unsigned long *)state->regs->bp; 130 else 131 next_bp = (unsigned long *)*state->bp; 132 133 /* is the next frame pointer an encoded pointer to pt_regs? */ 134 regs = decode_frame_pointer(next_bp); 135 if (regs) { 136 next_frame = (unsigned long *)regs; 137 next_len = sizeof(*regs); 138 } else { 139 next_frame = next_bp; 140 next_len = FRAME_HEADER_SIZE; 141 } 142 143 /* make sure the next frame's data is accessible */ 144 if (!update_stack_state(state, next_frame, next_len)) { 145 /* 146 * Don't warn on bad regs->bp. An interrupt in entry code 147 * might cause a false positive warning. 148 */ 149 if (state->regs) 150 goto the_end; 151 152 goto bad_address; 153 } 154 155 /* Make sure it only unwinds up and doesn't overlap the last frame: */ 156 if (state->stack_info.type == prev_type) { 157 if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs)) 158 goto bad_address; 159 160 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE) 161 goto bad_address; 162 } 163 164 /* move to the next frame */ 165 if (regs) { 166 state->regs = regs; 167 state->bp = NULL; 168 } else { 169 state->bp = next_bp; 170 state->regs = NULL; 171 } 172 173 return true; 174 175 bad_address: 176 if (state->regs) { 177 printk_deferred_once(KERN_WARNING 178 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", 179 state->regs, state->task->comm, 180 state->task->pid, next_frame); 181 } else { 182 printk_deferred_once(KERN_WARNING 183 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 184 state->bp, state->task->comm, 185 state->task->pid, next_frame); 186 } 187 the_end: 188 state->stack_info.type = STACK_TYPE_UNKNOWN; 189 return false; 190 } 191 EXPORT_SYMBOL_GPL(unwind_next_frame); 192 193 void __unwind_start(struct unwind_state *state, struct task_struct *task, 194 struct pt_regs *regs, unsigned long *first_frame) 195 { 196 unsigned long *bp, *frame; 197 size_t len; 198 199 memset(state, 0, sizeof(*state)); 200 state->task = task; 201 202 /* don't even attempt to start from user mode regs */ 203 if (regs && user_mode(regs)) { 204 state->stack_info.type = STACK_TYPE_UNKNOWN; 205 return; 206 } 207 208 /* set up the starting stack frame */ 209 bp = get_frame_pointer(task, regs); 210 regs = decode_frame_pointer(bp); 211 if (regs) { 212 state->regs = regs; 213 frame = (unsigned long *)regs; 214 len = sizeof(*regs); 215 } else { 216 state->bp = bp; 217 frame = bp; 218 len = FRAME_HEADER_SIZE; 219 } 220 221 /* initialize stack info and make sure the frame data is accessible */ 222 get_stack_info(frame, state->task, &state->stack_info, 223 &state->stack_mask); 224 update_stack_state(state, frame, len); 225 226 /* 227 * The caller can provide the address of the first frame directly 228 * (first_frame) or indirectly (regs->sp) to indicate which stack frame 229 * to start unwinding at. Skip ahead until we reach it. 230 */ 231 while (!unwind_done(state) && 232 (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 233 state->bp < first_frame)) 234 unwind_next_frame(state); 235 } 236 EXPORT_SYMBOL_GPL(__unwind_start); 237