1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun * Copyright (C) 2005 Brian Rogan <bcr6@cornell.edu>, IBM
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun **/
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/time.h>
8*4882a593Smuzhiyun #include <linux/oprofile.h>
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <asm/processor.h>
11*4882a593Smuzhiyun #include <linux/uaccess.h>
12*4882a593Smuzhiyun #include <linux/compat.h>
13*4882a593Smuzhiyun #include <asm/oprofile_impl.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define STACK_SP(STACK) *(STACK)
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define STACK_LR64(STACK) *((unsigned long *)(STACK) + 2)
18*4882a593Smuzhiyun #define STACK_LR32(STACK) *((unsigned int *)(STACK) + 1)
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #ifdef CONFIG_PPC64
21*4882a593Smuzhiyun #define STACK_LR(STACK) STACK_LR64(STACK)
22*4882a593Smuzhiyun #else
23*4882a593Smuzhiyun #define STACK_LR(STACK) STACK_LR32(STACK)
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun
user_getsp32(unsigned int sp,int is_first)26*4882a593Smuzhiyun static unsigned int user_getsp32(unsigned int sp, int is_first)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun unsigned int stack_frame[2];
29*4882a593Smuzhiyun void __user *p = compat_ptr(sp);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * The most likely reason for this is that we returned -EFAULT,
33*4882a593Smuzhiyun * which means that we've done all that we can do from
34*4882a593Smuzhiyun * interrupt context.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun if (copy_from_user_nofault(stack_frame, (void __user *)p,
37*4882a593Smuzhiyun sizeof(stack_frame)))
38*4882a593Smuzhiyun return 0;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun if (!is_first)
41*4882a593Smuzhiyun oprofile_add_trace(STACK_LR32(stack_frame));
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun * We do not enforce increasing stack addresses here because
45*4882a593Smuzhiyun * we may transition to a different stack, eg a signal handler.
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun return STACK_SP(stack_frame);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #ifdef CONFIG_PPC64
user_getsp64(unsigned long sp,int is_first)51*4882a593Smuzhiyun static unsigned long user_getsp64(unsigned long sp, int is_first)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun unsigned long stack_frame[3];
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (copy_from_user_nofault(stack_frame, (void __user *)sp,
56*4882a593Smuzhiyun sizeof(stack_frame)))
57*4882a593Smuzhiyun return 0;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (!is_first)
60*4882a593Smuzhiyun oprofile_add_trace(STACK_LR64(stack_frame));
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun return STACK_SP(stack_frame);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun
kernel_getsp(unsigned long sp,int is_first)66*4882a593Smuzhiyun static unsigned long kernel_getsp(unsigned long sp, int is_first)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun unsigned long *stack_frame = (unsigned long *)sp;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
71*4882a593Smuzhiyun return 0;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (!is_first)
74*4882a593Smuzhiyun oprofile_add_trace(STACK_LR(stack_frame));
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * We do not enforce increasing stack addresses here because
78*4882a593Smuzhiyun * we might be transitioning from an interrupt stack to a kernel
79*4882a593Smuzhiyun * stack. validate_sp() is designed to understand this, so just
80*4882a593Smuzhiyun * use it.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun return STACK_SP(stack_frame);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
op_powerpc_backtrace(struct pt_regs * const regs,unsigned int depth)85*4882a593Smuzhiyun void op_powerpc_backtrace(struct pt_regs * const regs, unsigned int depth)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun unsigned long sp = regs->gpr[1];
88*4882a593Smuzhiyun int first_frame = 1;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* We ditch the top stackframe so need to loop through an extra time */
91*4882a593Smuzhiyun depth += 1;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if (!user_mode(regs)) {
94*4882a593Smuzhiyun while (depth--) {
95*4882a593Smuzhiyun sp = kernel_getsp(sp, first_frame);
96*4882a593Smuzhiyun if (!sp)
97*4882a593Smuzhiyun break;
98*4882a593Smuzhiyun first_frame = 0;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun } else {
101*4882a593Smuzhiyun #ifdef CONFIG_PPC64
102*4882a593Smuzhiyun if (!is_32bit_task()) {
103*4882a593Smuzhiyun while (depth--) {
104*4882a593Smuzhiyun sp = user_getsp64(sp, first_frame);
105*4882a593Smuzhiyun if (!sp)
106*4882a593Smuzhiyun break;
107*4882a593Smuzhiyun first_frame = 0;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun return;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun #endif
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun while (depth--) {
114*4882a593Smuzhiyun sp = user_getsp32(sp, first_frame);
115*4882a593Smuzhiyun if (!sp)
116*4882a593Smuzhiyun break;
117*4882a593Smuzhiyun first_frame = 0;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun }
121