linux/arch/powerpc/oprofile/backtrace.c
<<
>>
Prefs
   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
  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         * The most likely reason for this is that we returned -EFAULT,
  36         * which means that we've done all that we can do from
  37         * interrupt context.
  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         * We do not enforce increasing stack addresses here because
  47         * we may transition to a different stack, eg a signal handler.
  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         * We do not enforce increasing stack addresses here because
  79         * we might be transitioning from an interrupt stack to a kernel
  80         * stack. validate_sp() is designed to understand this, so just
  81         * use it.
  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        /* We ditch the top stackframe so need to loop through an extra time */
  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