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