1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #include <linux/sched.h> 3 #include <linux/sched/task.h> 4 #include <linux/sched/task_stack.h> 5 #include <linux/interrupt.h> 6 #include <asm/sections.h> 7 #include <asm/ptrace.h> 8 #include <asm/bitops.h> 9 #include <asm/stacktrace.h> 10 #include <asm/unwind.h> 11 12 unsigned long unwind_get_return_address(struct unwind_state *state) 13 { 14 if (unwind_done(state)) 15 return 0; 16 return __kernel_text_address(state->ip) ? state->ip : 0; 17 } 18 EXPORT_SYMBOL_GPL(unwind_get_return_address); 19 20 static bool outside_of_stack(struct unwind_state *state, unsigned long sp) 21 { 22 return (sp <= state->sp) || 23 (sp > state->stack_info.end - sizeof(struct stack_frame)); 24 } 25 26 static bool update_stack_info(struct unwind_state *state, unsigned long sp) 27 { 28 struct stack_info *info = &state->stack_info; 29 unsigned long *mask = &state->stack_mask; 30 31 /* New stack pointer leaves the current stack */ 32 if (get_stack_info(sp, state->task, info, mask) != 0 || 33 !on_stack(info, sp, sizeof(struct stack_frame))) 34 /* 'sp' does not point to a valid stack */ 35 return false; 36 return true; 37 } 38 39 bool unwind_next_frame(struct unwind_state *state) 40 { 41 struct stack_info *info = &state->stack_info; 42 struct stack_frame *sf; 43 struct pt_regs *regs; 44 unsigned long sp, ip; 45 bool reliable; 46 47 regs = state->regs; 48 if (unlikely(regs)) { 49 sp = state->sp; 50 sf = (struct stack_frame *) sp; 51 ip = READ_ONCE_NOCHECK(sf->gprs[8]); 52 reliable = false; 53 regs = NULL; 54 } else { 55 sf = (struct stack_frame *) state->sp; 56 sp = READ_ONCE_NOCHECK(sf->back_chain); 57 if (likely(sp)) { 58 /* Non-zero back-chain points to the previous frame */ 59 if (unlikely(outside_of_stack(state, sp))) { 60 if (!update_stack_info(state, sp)) 61 goto out_err; 62 } 63 sf = (struct stack_frame *) sp; 64 ip = READ_ONCE_NOCHECK(sf->gprs[8]); 65 reliable = true; 66 } else { 67 /* No back-chain, look for a pt_regs structure */ 68 sp = state->sp + STACK_FRAME_OVERHEAD; 69 if (!on_stack(info, sp, sizeof(struct pt_regs))) 70 goto out_err; 71 regs = (struct pt_regs *) sp; 72 if (READ_ONCE_NOCHECK(regs->psw.mask) & PSW_MASK_PSTATE) 73 goto out_stop; 74 ip = READ_ONCE_NOCHECK(regs->psw.addr); 75 sp = READ_ONCE_NOCHECK(regs->gprs[15]); 76 if (unlikely(outside_of_stack(state, sp))) { 77 if (!update_stack_info(state, sp)) 78 goto out_err; 79 } 80 reliable = true; 81 } 82 } 83 84 ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *) sp); 85 86 /* Update unwind state */ 87 state->sp = sp; 88 state->ip = ip; 89 state->regs = regs; 90 state->reliable = reliable; 91 return true; 92 93 out_err: 94 state->error = true; 95 out_stop: 96 state->stack_info.type = STACK_TYPE_UNKNOWN; 97 return false; 98 } 99 EXPORT_SYMBOL_GPL(unwind_next_frame); 100 101 void __unwind_start(struct unwind_state *state, struct task_struct *task, 102 struct pt_regs *regs, unsigned long sp) 103 { 104 struct stack_info *info = &state->stack_info; 105 unsigned long *mask = &state->stack_mask; 106 bool reliable; 107 struct stack_frame *sf; 108 unsigned long ip; 109 110 memset(state, 0, sizeof(*state)); 111 state->task = task; 112 state->regs = regs; 113 114 /* Don't even attempt to start from user mode regs: */ 115 if (regs && user_mode(regs)) { 116 info->type = STACK_TYPE_UNKNOWN; 117 return; 118 } 119 120 /* Get current stack pointer and initialize stack info */ 121 if (get_stack_info(sp, task, info, mask) != 0 || 122 !on_stack(info, sp, sizeof(struct stack_frame))) { 123 /* Something is wrong with the stack pointer */ 124 info->type = STACK_TYPE_UNKNOWN; 125 state->error = true; 126 return; 127 } 128 129 /* Get the instruction pointer from pt_regs or the stack frame */ 130 if (regs) { 131 ip = READ_ONCE_NOCHECK(regs->psw.addr); 132 reliable = true; 133 } else { 134 sf = (struct stack_frame *) sp; 135 ip = READ_ONCE_NOCHECK(sf->gprs[8]); 136 reliable = false; 137 } 138 139 ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, NULL); 140 141 /* Update unwind state */ 142 state->sp = sp; 143 state->ip = ip; 144 state->reliable = reliable; 145 } 146 EXPORT_SYMBOL_GPL(__unwind_start); 147