1
2
3
4
5
6
7
8
9
10#include <linux/compat_time.h>
11#include <linux/oprofile.h>
12#include <linux/sched.h>
13#include <asm/processor.h>
14#include <linux/uaccess.h>
15#include <linux/compat.h>
16#include <asm/oprofile_impl.h>
17
18#define STACK_SP(STACK) *(STACK)
19
20#define STACK_LR64(STACK) *((unsigned long *)(STACK) + 2)
21#define STACK_LR32(STACK) *((unsigned int *)(STACK) + 1)
22
23#ifdef CONFIG_PPC64
24#define STACK_LR(STACK) STACK_LR64(STACK)
25#else
26#define STACK_LR(STACK) STACK_LR32(STACK)
27#endif
28
29static unsigned int user_getsp32(unsigned int sp, int is_first)
30{
31 unsigned int stack_frame[2];
32 void __user *p = compat_ptr(sp);
33
34
35
36
37
38
39 if (probe_user_read(stack_frame, (void __user *)p, sizeof(stack_frame)))
40 return 0;
41
42 if (!is_first)
43 oprofile_add_trace(STACK_LR32(stack_frame));
44
45
46
47
48
49 return STACK_SP(stack_frame);
50}
51
52#ifdef CONFIG_PPC64
53static unsigned long user_getsp64(unsigned long sp, int is_first)
54{
55 unsigned long stack_frame[3];
56
57 if (probe_user_read(stack_frame, (void __user *)sp, sizeof(stack_frame)))
58 return 0;
59
60 if (!is_first)
61 oprofile_add_trace(STACK_LR64(stack_frame));
62
63 return STACK_SP(stack_frame);
64}
65#endif
66
67static unsigned long kernel_getsp(unsigned long sp, int is_first)
68{
69 unsigned long *stack_frame = (unsigned long *)sp;
70
71 if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
72 return 0;
73
74 if (!is_first)
75 oprofile_add_trace(STACK_LR(stack_frame));
76
77
78
79
80
81
82
83 return STACK_SP(stack_frame);
84}
85
86void op_powerpc_backtrace(struct pt_regs * const regs, unsigned int depth)
87{
88 unsigned long sp = regs->gpr[1];
89 int first_frame = 1;
90
91
92 depth += 1;
93
94 if (!user_mode(regs)) {
95 while (depth--) {
96 sp = kernel_getsp(sp, first_frame);
97 if (!sp)
98 break;
99 first_frame = 0;
100 }
101 } else {
102#ifdef CONFIG_PPC64
103 if (!is_32bit_task()) {
104 while (depth--) {
105 sp = user_getsp64(sp, first_frame);
106 if (!sp)
107 break;
108 first_frame = 0;
109 }
110 return;
111 }
112#endif
113
114 while (depth--) {
115 sp = user_getsp32(sp, first_frame);
116 if (!sp)
117 break;
118 first_frame = 0;
119 }
120 }
121}
122