1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Generic interfaces for unwinding user space
4 */
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
7 #include <linux/sched/task_stack.h>
8 #include <linux/unwind_user.h>
9 #include <linux/uaccess.h>
10
11 static const struct unwind_user_frame fp_frame = {
12 ARCH_INIT_USER_FP_FRAME
13 };
14
15 #define for_each_user_frame(state) \
16 for (unwind_user_start(state); !(state)->done; unwind_user_next(state))
17
unwind_user_next_fp(struct unwind_user_state * state)18 static int unwind_user_next_fp(struct unwind_user_state *state)
19 {
20 const struct unwind_user_frame *frame = &fp_frame;
21 unsigned long cfa, fp, ra;
22 unsigned int shift;
23
24 if (frame->use_fp) {
25 if (state->fp < state->sp)
26 return -EINVAL;
27 cfa = state->fp;
28 } else {
29 cfa = state->sp;
30 }
31
32 /* Get the Canonical Frame Address (CFA) */
33 cfa += frame->cfa_off;
34
35 /* stack going in wrong direction? */
36 if (cfa <= state->sp)
37 return -EINVAL;
38
39 /* Make sure that the address is word aligned */
40 shift = sizeof(long) == 4 ? 2 : 3;
41 if (cfa & ((1 << shift) - 1))
42 return -EINVAL;
43
44 /* Find the Return Address (RA) */
45 if (get_user(ra, (unsigned long *)(cfa + frame->ra_off)))
46 return -EINVAL;
47
48 if (frame->fp_off && get_user(fp, (unsigned long __user *)(cfa + frame->fp_off)))
49 return -EINVAL;
50
51 state->ip = ra;
52 state->sp = cfa;
53 if (frame->fp_off)
54 state->fp = fp;
55 return 0;
56 }
57
unwind_user_next(struct unwind_user_state * state)58 static int unwind_user_next(struct unwind_user_state *state)
59 {
60 unsigned long iter_mask = state->available_types;
61 unsigned int bit;
62
63 if (state->done)
64 return -EINVAL;
65
66 for_each_set_bit(bit, &iter_mask, NR_UNWIND_USER_TYPE_BITS) {
67 enum unwind_user_type type = BIT(bit);
68
69 state->current_type = type;
70 switch (type) {
71 case UNWIND_USER_TYPE_FP:
72 if (!unwind_user_next_fp(state))
73 return 0;
74 continue;
75 default:
76 WARN_ONCE(1, "Undefined unwind bit %d", bit);
77 break;
78 }
79 break;
80 }
81
82 /* No successful unwind method. */
83 state->current_type = UNWIND_USER_TYPE_NONE;
84 state->done = true;
85 return -EINVAL;
86 }
87
unwind_user_start(struct unwind_user_state * state)88 static int unwind_user_start(struct unwind_user_state *state)
89 {
90 struct pt_regs *regs = task_pt_regs(current);
91
92 memset(state, 0, sizeof(*state));
93
94 if ((current->flags & PF_KTHREAD) || !user_mode(regs)) {
95 state->done = true;
96 return -EINVAL;
97 }
98
99 if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP))
100 state->available_types |= UNWIND_USER_TYPE_FP;
101
102 state->ip = instruction_pointer(regs);
103 state->sp = user_stack_pointer(regs);
104 state->fp = frame_pointer(regs);
105
106 return 0;
107 }
108
unwind_user(struct unwind_stacktrace * trace,unsigned int max_entries)109 int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries)
110 {
111 struct unwind_user_state state;
112
113 trace->nr = 0;
114
115 if (!max_entries)
116 return -EINVAL;
117
118 if (current->flags & PF_KTHREAD)
119 return 0;
120
121 for_each_user_frame(&state) {
122 trace->entries[trace->nr++] = state.ip;
123 if (trace->nr >= max_entries)
124 break;
125 }
126
127 return 0;
128 }
129