Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2005 Brian Rogan <bcr6@cornell.edu>, IBM |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | **/ |
| 9 | |
Deepa Dinamani | 0d55303 | 2018-03-13 21:03:25 -0700 | [diff] [blame] | 10 | #include <linux/compat_time.h> |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 11 | #include <linux/oprofile.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <asm/processor.h> |
Jiang Lu | 0de3b56b | 2014-11-21 11:23:19 +0800 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
Al Viro | 62034f0 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 15 | #include <asm/compat.h> |
Anton Blanchard | 1217d34 | 2014-08-20 08:55:19 +1000 | [diff] [blame] | 16 | #include <asm/oprofile_impl.h> |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 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 | |
| 29 | static unsigned int user_getsp32(unsigned int sp, int is_first) |
| 30 | { |
| 31 | unsigned int stack_frame[2]; |
Al Viro | 62034f0 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 32 | void __user *p = compat_ptr(sp); |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 33 | |
Al Viro | 62034f0 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 34 | if (!access_ok(VERIFY_READ, p, sizeof(stack_frame))) |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 35 | return 0; |
| 36 | |
| 37 | /* |
| 38 | * The most likely reason for this is that we returned -EFAULT, |
| 39 | * which means that we've done all that we can do from |
| 40 | * interrupt context. |
| 41 | */ |
Al Viro | 62034f0 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 42 | if (__copy_from_user_inatomic(stack_frame, p, sizeof(stack_frame))) |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 43 | return 0; |
| 44 | |
| 45 | if (!is_first) |
| 46 | oprofile_add_trace(STACK_LR32(stack_frame)); |
| 47 | |
| 48 | /* |
| 49 | * We do not enforce increasing stack addresses here because |
| 50 | * we may transition to a different stack, eg a signal handler. |
| 51 | */ |
| 52 | return STACK_SP(stack_frame); |
| 53 | } |
| 54 | |
| 55 | #ifdef CONFIG_PPC64 |
| 56 | static unsigned long user_getsp64(unsigned long sp, int is_first) |
| 57 | { |
| 58 | unsigned long stack_frame[3]; |
| 59 | |
Al Viro | 62034f0 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 60 | if (!access_ok(VERIFY_READ, (void __user *)sp, sizeof(stack_frame))) |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 61 | return 0; |
| 62 | |
Al Viro | 62034f0 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 63 | if (__copy_from_user_inatomic(stack_frame, (void __user *)sp, |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 64 | sizeof(stack_frame))) |
| 65 | return 0; |
| 66 | |
| 67 | if (!is_first) |
| 68 | oprofile_add_trace(STACK_LR64(stack_frame)); |
| 69 | |
| 70 | return STACK_SP(stack_frame); |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | static unsigned long kernel_getsp(unsigned long sp, int is_first) |
| 75 | { |
| 76 | unsigned long *stack_frame = (unsigned long *)sp; |
| 77 | |
| 78 | if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD)) |
| 79 | return 0; |
| 80 | |
| 81 | if (!is_first) |
| 82 | oprofile_add_trace(STACK_LR(stack_frame)); |
| 83 | |
| 84 | /* |
| 85 | * We do not enforce increasing stack addresses here because |
| 86 | * we might be transitioning from an interrupt stack to a kernel |
| 87 | * stack. validate_sp() is designed to understand this, so just |
| 88 | * use it. |
| 89 | */ |
| 90 | return STACK_SP(stack_frame); |
| 91 | } |
| 92 | |
| 93 | void op_powerpc_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 94 | { |
| 95 | unsigned long sp = regs->gpr[1]; |
| 96 | int first_frame = 1; |
| 97 | |
| 98 | /* We ditch the top stackframe so need to loop through an extra time */ |
| 99 | depth += 1; |
| 100 | |
| 101 | if (!user_mode(regs)) { |
| 102 | while (depth--) { |
| 103 | sp = kernel_getsp(sp, first_frame); |
| 104 | if (!sp) |
| 105 | break; |
| 106 | first_frame = 0; |
| 107 | } |
| 108 | } else { |
Jiang Lu | 0de3b56b | 2014-11-21 11:23:19 +0800 | [diff] [blame] | 109 | pagefault_disable(); |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 110 | #ifdef CONFIG_PPC64 |
Denis Kirjanov | cab175f | 2010-08-27 03:49:11 +0000 | [diff] [blame] | 111 | if (!is_32bit_task()) { |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 112 | while (depth--) { |
| 113 | sp = user_getsp64(sp, first_frame); |
| 114 | if (!sp) |
| 115 | break; |
| 116 | first_frame = 0; |
| 117 | } |
Jiang Lu | 0de3b56b | 2014-11-21 11:23:19 +0800 | [diff] [blame] | 118 | pagefault_enable(); |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | while (depth--) { |
| 124 | sp = user_getsp32(sp, first_frame); |
| 125 | if (!sp) |
| 126 | break; |
| 127 | first_frame = 0; |
| 128 | } |
Jiang Lu | 0de3b56b | 2014-11-21 11:23:19 +0800 | [diff] [blame] | 129 | pagefault_enable(); |
Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 130 | } |
| 131 | } |