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