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