linux/arch/tile/kernel/ftrace.c
<<
>>
Prefs
   1/*
   2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
   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, version 2.
   7 *
   8 *   This program is distributed in the hope that it will be useful, but
   9 *   WITHOUT ANY WARRANTY; without even the implied warranty of
  10 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11 *   NON INFRINGEMENT.  See the GNU General Public License for
  12 *   more details.
  13 *
  14 * TILE-Gx specific ftrace support
  15 */
  16
  17#include <linux/ftrace.h>
  18#include <linux/uaccess.h>
  19
  20#include <asm/cacheflush.h>
  21#include <asm/ftrace.h>
  22#include <asm/sections.h>
  23#include <asm/insn.h>
  24
  25#include <arch/opcode.h>
  26
  27#ifdef CONFIG_DYNAMIC_FTRACE
  28
  29static int machine_stopped __read_mostly;
  30
  31int ftrace_arch_code_modify_prepare(void)
  32{
  33        machine_stopped = 1;
  34        return 0;
  35}
  36
  37int ftrace_arch_code_modify_post_process(void)
  38{
  39        flush_icache_range(0, CHIP_L1I_CACHE_SIZE());
  40        machine_stopped = 0;
  41        return 0;
  42}
  43
  44/*
  45 * Put { move r10, lr; jal ftrace_caller } in a bundle, this lets dynamic
  46 * tracer just add one cycle overhead to every kernel function when disabled.
  47 */
  48static unsigned long ftrace_gen_branch(unsigned long pc, unsigned long addr,
  49                                       bool link)
  50{
  51        tilegx_bundle_bits opcode_x0, opcode_x1;
  52        long pcrel_by_instr = (addr - pc) >> TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES;
  53
  54        if (link) {
  55                /* opcode: jal addr */
  56                opcode_x1 =
  57                        create_Opcode_X1(JUMP_OPCODE_X1) |
  58                        create_JumpOpcodeExtension_X1(JAL_JUMP_OPCODE_X1) |
  59                        create_JumpOff_X1(pcrel_by_instr);
  60        } else {
  61                /* opcode: j addr */
  62                opcode_x1 =
  63                        create_Opcode_X1(JUMP_OPCODE_X1) |
  64                        create_JumpOpcodeExtension_X1(J_JUMP_OPCODE_X1) |
  65                        create_JumpOff_X1(pcrel_by_instr);
  66        }
  67
  68        /*
  69         * Also put { move r10, lr; jal ftrace_stub } in a bundle, which
  70         * is used to replace the instruction in address ftrace_call.
  71         */
  72        if (addr == FTRACE_ADDR || addr == (unsigned long)ftrace_stub) {
  73                /* opcode: or r10, lr, zero */
  74                opcode_x0 =
  75                        create_Dest_X0(10) |
  76                        create_SrcA_X0(TREG_LR) |
  77                        create_SrcB_X0(TREG_ZERO) |
  78                        create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0) |
  79                        create_Opcode_X0(RRR_0_OPCODE_X0);
  80        } else {
  81                /* opcode: fnop */
  82                opcode_x0 =
  83                        create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0) |
  84                        create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0) |
  85                        create_Opcode_X0(RRR_0_OPCODE_X0);
  86        }
  87
  88        return opcode_x1 | opcode_x0;
  89}
  90
  91static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  92{
  93        return NOP();
  94}
  95
  96static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  97{
  98        return ftrace_gen_branch(pc, addr, true);
  99}
 100
 101static int ftrace_modify_code(unsigned long pc, unsigned long old,
 102                              unsigned long new)
 103{
 104        unsigned long pc_wr;
 105
 106        /* Check if the address is in kernel text space and module space. */
 107        if (!kernel_text_address(pc))
 108                return -EINVAL;
 109
 110        /* Operate on writable kernel text mapping. */
 111        pc_wr = ktext_writable_addr(pc);
 112
 113        if (probe_kernel_write((void *)pc_wr, &new, MCOUNT_INSN_SIZE))
 114                return -EPERM;
 115
 116        smp_wmb();
 117
 118        if (!machine_stopped && num_online_cpus() > 1)
 119                flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
 120
 121        return 0;
 122}
 123
 124int ftrace_update_ftrace_func(ftrace_func_t func)
 125{
 126        unsigned long pc, old;
 127        unsigned long new;
 128        int ret;
 129
 130        pc = (unsigned long)&ftrace_call;
 131        memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
 132        new = ftrace_call_replace(pc, (unsigned long)func);
 133
 134        ret = ftrace_modify_code(pc, old, new);
 135
 136        return ret;
 137}
 138
 139int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 140{
 141        unsigned long new, old;
 142        unsigned long ip = rec->ip;
 143
 144        old = ftrace_nop_replace(rec);
 145        new = ftrace_call_replace(ip, addr);
 146
 147        return ftrace_modify_code(rec->ip, old, new);
 148}
 149
 150int ftrace_make_nop(struct module *mod,
 151                    struct dyn_ftrace *rec, unsigned long addr)
 152{
 153        unsigned long ip = rec->ip;
 154        unsigned long old;
 155        unsigned long new;
 156        int ret;
 157
 158        old = ftrace_call_replace(ip, addr);
 159        new = ftrace_nop_replace(rec);
 160        ret = ftrace_modify_code(ip, old, new);
 161
 162        return ret;
 163}
 164
 165int __init ftrace_dyn_arch_init(void)
 166{
 167        return 0;
 168}
 169#endif /* CONFIG_DYNAMIC_FTRACE */
 170
 171#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 172void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
 173                           unsigned long frame_pointer)
 174{
 175        unsigned long return_hooker = (unsigned long) &return_to_handler;
 176        struct ftrace_graph_ent trace;
 177        unsigned long old;
 178        int err;
 179
 180        if (unlikely(atomic_read(&current->tracing_graph_pause)))
 181                return;
 182
 183        old = *parent;
 184        *parent = return_hooker;
 185
 186        err = ftrace_push_return_trace(old, self_addr, &trace.depth,
 187                                       frame_pointer, NULL);
 188        if (err == -EBUSY) {
 189                *parent = old;
 190                return;
 191        }
 192
 193        trace.func = self_addr;
 194
 195        /* Only trace if the calling function expects to */
 196        if (!ftrace_graph_entry(&trace)) {
 197                current->curr_ret_stack--;
 198                *parent = old;
 199        }
 200}
 201
 202#ifdef CONFIG_DYNAMIC_FTRACE
 203extern unsigned long ftrace_graph_call;
 204
 205static int __ftrace_modify_caller(unsigned long *callsite,
 206                                  void (*func) (void), bool enable)
 207{
 208        unsigned long caller_fn = (unsigned long) func;
 209        unsigned long pc = (unsigned long) callsite;
 210        unsigned long branch = ftrace_gen_branch(pc, caller_fn, false);
 211        unsigned long nop = NOP();
 212        unsigned long old = enable ? nop : branch;
 213        unsigned long new = enable ? branch : nop;
 214
 215        return ftrace_modify_code(pc, old, new);
 216}
 217
 218static int ftrace_modify_graph_caller(bool enable)
 219{
 220        int ret;
 221
 222        ret = __ftrace_modify_caller(&ftrace_graph_call,
 223                                     ftrace_graph_caller,
 224                                     enable);
 225
 226        return ret;
 227}
 228
 229int ftrace_enable_ftrace_graph_caller(void)
 230{
 231        return ftrace_modify_graph_caller(true);
 232}
 233
 234int ftrace_disable_ftrace_graph_caller(void)
 235{
 236        return ftrace_modify_graph_caller(false);
 237}
 238#endif /* CONFIG_DYNAMIC_FTRACE */
 239#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
 240