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