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 = READ_ONCE_NOCHECK(regs->gprs[15]); 50 if (unlikely(outside_of_stack(state, sp))) { 51 if (!update_stack_info(state, sp)) 52 goto out_err; 53 } 54 sf = (struct stack_frame *) sp; 55 ip = READ_ONCE_NOCHECK(sf->gprs[8]); 56 reliable = false; 57 regs = NULL; 58 } else { 59 sf = (struct stack_frame *) state->sp; 60 sp = READ_ONCE_NOCHECK(sf->back_chain); 61 if (likely(sp)) { 62 /* Non-zero back-chain points to the previous frame */ 63 if (unlikely(outside_of_stack(state, sp))) { 64 if (!update_stack_info(state, sp)) 65 goto out_err; 66 } 67 sf = (struct stack_frame *) sp; 68 ip = READ_ONCE_NOCHECK(sf->gprs[8]); 69 reliable = true; 70 } else { 71 /* No back-chain, look for a pt_regs structure */ 72 sp = state->sp + STACK_FRAME_OVERHEAD; 73 if (!on_stack(info, sp, sizeof(struct pt_regs))) 74 goto out_stop; 75 regs = (struct pt_regs *) sp; 76 if (READ_ONCE_NOCHECK(regs->psw.mask) & PSW_MASK_PSTATE) 77 goto out_stop; 78 ip = READ_ONCE_NOCHECK(regs->psw.addr); 79 reliable = true; 80 } 81 } 82 83 ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *) sp); 84 85 /* Update unwind state */ 86 state->sp = sp; 87 state->ip = ip; 88 state->regs = regs; 89 state->reliable = reliable; 90 return true; 91 92 out_err: 93 state->error = true; 94 out_stop: 95 state->stack_info.type = STACK_TYPE_UNKNOWN; 96 return false; 97 } 98 EXPORT_SYMBOL_GPL(unwind_next_frame); 99 100 void __unwind_start(struct unwind_state *state, struct task_struct *task, 101 struct pt_regs *regs, unsigned long sp) 102 { 103 struct stack_info *info = &state->stack_info; 104 unsigned long *mask = &state->stack_mask; 105 struct stack_frame *sf; 106 unsigned long ip; 107 bool reliable; 108 109 memset(state, 0, sizeof(*state)); 110 state->task = task; 111 state->regs = regs; 112 113 /* Don't even attempt to start from user mode regs: */ 114 if (regs && user_mode(regs)) { 115 info->type = STACK_TYPE_UNKNOWN; 116 return; 117 } 118 119 /* Get current stack pointer and initialize stack info */ 120 if (get_stack_info(sp, task, info, mask) != 0 || 121 !on_stack(info, sp, sizeof(struct stack_frame))) { 122 /* Something is wrong with the stack pointer */ 123 info->type = STACK_TYPE_UNKNOWN; 124 state->error = true; 125 return; 126 } 127 128 /* Get the instruction pointer from pt_regs or the stack frame */ 129 if (regs) { 130 ip = READ_ONCE_NOCHECK(regs->psw.addr); 131 reliable = true; 132 } else { 133 sf = (struct stack_frame *) sp; 134 ip = READ_ONCE_NOCHECK(sf->gprs[8]); 135 reliable = false; 136 } 137 138 ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, NULL); 139 140 /* Update unwind state */ 141 state->sp = sp; 142 state->ip = ip; 143 state->reliable = reliable; 144 } 145 EXPORT_SYMBOL_GPL(__unwind_start); 146