blob: de55efa5b64e255914c1c9e9c95629eb8680f768 [file] [log] [blame]
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02001/*
2 * Dynamic function tracer architecture backend.
3 *
4 * Copyright IBM Corp. 2009
5 *
6 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +01007 * Martin Schwidefsky <schwidefsky@de.ibm.com>
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02008 */
9
Heiko Carstens88dbd202009-06-12 10:26:46 +020010#include <linux/hardirq.h>
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020011#include <linux/uaccess.h>
12#include <linux/ftrace.h>
13#include <linux/kernel.h>
14#include <linux/types.h>
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010015#include <linux/kprobes.h>
Heiko Carstens9bf12262009-06-12 10:26:47 +020016#include <trace/syscall.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010017#include <asm/asm-offsets.h>
Heiko Carstens63df41d62013-09-06 19:10:48 +020018#include "entry.h"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020019
Heiko Carstens88dbd202009-06-12 10:26:46 +020020#ifdef CONFIG_DYNAMIC_FTRACE
21
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020022void ftrace_disable_code(void);
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010023void ftrace_enable_insn(void);
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020024
25#ifdef CONFIG_64BIT
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010026/*
27 * The 64-bit mcount code looks like this:
28 * stg %r14,8(%r15) # offset 0
29 * > larl %r1,<&counter> # offset 6
30 * > brasl %r14,_mcount # offset 12
31 * lg %r14,8(%r15) # offset 18
32 * Total length is 24 bytes. The middle two instructions of the mcount
33 * block get overwritten by ftrace_make_nop / ftrace_make_call.
34 * The 64-bit enabled ftrace code block looks like this:
35 * stg %r14,8(%r15) # offset 0
36 * > lg %r1,__LC_FTRACE_FUNC # offset 6
37 * > lgr %r0,%r0 # offset 12
38 * > basr %r14,%r1 # offset 16
39 * lg %r14,8(%15) # offset 18
40 * The return points of the mcount/ftrace function have the same offset 18.
41 * The 64-bit disable ftrace code block looks like this:
42 * stg %r14,8(%r15) # offset 0
43 * > jg .+18 # offset 6
44 * > lgr %r0,%r0 # offset 12
45 * > basr %r14,%r1 # offset 16
46 * lg %r14,8(%15) # offset 18
47 * The jg instruction branches to offset 24 to skip as many instructions
48 * as possible.
49 */
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020050asm(
51 " .align 4\n"
52 "ftrace_disable_code:\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010053 " jg 0f\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020054 " lgr %r0,%r0\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010055 " basr %r14,%r1\n"
56 "0:\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020057 " .align 4\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010058 "ftrace_enable_insn:\n"
59 " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n");
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020060
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010061#define FTRACE_INSN_SIZE 6
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020062
63#else /* CONFIG_64BIT */
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010064/*
65 * The 31-bit mcount code looks like this:
66 * st %r14,4(%r15) # offset 0
67 * > bras %r1,0f # offset 4
68 * > .long _mcount # offset 8
69 * > .long <&counter> # offset 12
70 * > 0: l %r14,0(%r1) # offset 16
71 * > l %r1,4(%r1) # offset 20
72 * basr %r14,%r14 # offset 24
73 * l %r14,4(%r15) # offset 26
74 * Total length is 30 bytes. The twenty bytes starting from offset 4
75 * to offset 24 get overwritten by ftrace_make_nop / ftrace_make_call.
76 * The 31-bit enabled ftrace code block looks like this:
77 * st %r14,4(%r15) # offset 0
78 * > l %r14,__LC_FTRACE_FUNC # offset 4
79 * > j 0f # offset 8
80 * > .fill 12,1,0x07 # offset 12
81 * 0: basr %r14,%r14 # offset 24
82 * l %r14,4(%r14) # offset 26
83 * The return points of the mcount/ftrace function have the same offset 26.
84 * The 31-bit disabled ftrace code block looks like this:
85 * st %r14,4(%r15) # offset 0
86 * > j .+26 # offset 4
87 * > j 0f # offset 8
88 * > .fill 12,1,0x07 # offset 12
89 * 0: basr %r14,%r14 # offset 24
90 * l %r14,4(%r14) # offset 26
91 * The j instruction branches to offset 30 to skip as many instructions
92 * as possible.
93 */
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020094asm(
95 " .align 4\n"
96 "ftrace_disable_code:\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010097 " j 1f\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020098 " j 0f\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010099 " .fill 12,1,0x07\n"
100 "0: basr %r14,%r14\n"
101 "1:\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200102 " .align 4\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100103 "ftrace_enable_insn:\n"
104 " l %r14,"__stringify(__LC_FTRACE_FUNC)"\n");
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200105
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100106#define FTRACE_INSN_SIZE 4
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200107
108#endif /* CONFIG_64BIT */
109
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200110
111int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
112 unsigned long addr)
113{
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100114 if (probe_kernel_write((void *) rec->ip, ftrace_disable_code,
115 MCOUNT_INSN_SIZE))
116 return -EPERM;
117 return 0;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200118}
119
120int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
121{
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100122 if (probe_kernel_write((void *) rec->ip, ftrace_enable_insn,
123 FTRACE_INSN_SIZE))
124 return -EPERM;
125 return 0;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200126}
127
128int ftrace_update_ftrace_func(ftrace_func_t func)
129{
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200130 return 0;
131}
132
Jiri Slaby3a36cb12014-02-24 19:59:59 +0100133int __init ftrace_dyn_arch_init(void)
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200134{
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200135 return 0;
136}
Heiko Carstens88dbd202009-06-12 10:26:46 +0200137
138#endif /* CONFIG_DYNAMIC_FTRACE */
139
140#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Heiko Carstens88dbd202009-06-12 10:26:46 +0200141/*
142 * Hook the return address and push it in the stack of return addresses
143 * in current thread info.
144 */
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100145unsigned long __kprobes prepare_ftrace_return(unsigned long parent,
146 unsigned long ip)
Heiko Carstens88dbd202009-06-12 10:26:46 +0200147{
148 struct ftrace_graph_ent trace;
149
Heiko Carstens88dbd202009-06-12 10:26:46 +0200150 if (unlikely(atomic_read(&current->tracing_graph_pause)))
151 goto out;
Heiko Carstensaca91202013-05-13 14:48:52 +0200152 ip = (ip & PSW_ADDR_INSN) - MCOUNT_INSN_SIZE;
Heiko Carstens05e0baa2013-10-11 08:55:57 +0200153 trace.func = ip;
154 trace.depth = current->curr_ret_stack + 1;
155 /* Only trace if the calling function expects to. */
156 if (!ftrace_graph_entry(&trace))
157 goto out;
Steven Rostedt71e308a2009-06-18 12:45:08 -0400158 if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)
Heiko Carstens88dbd202009-06-12 10:26:46 +0200159 goto out;
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100160 parent = (unsigned long) return_to_handler;
Heiko Carstens88dbd202009-06-12 10:26:46 +0200161out:
162 return parent;
163}
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100164
165#ifdef CONFIG_DYNAMIC_FTRACE
166/*
167 * Patch the kernel code at ftrace_graph_caller location. The instruction
168 * there is branch relative and save to prepare_ftrace_return. To disable
169 * the call to prepare_ftrace_return we patch the bras offset to point
170 * directly after the instructions. To enable the call we calculate
171 * the original offset to prepare_ftrace_return and put it back.
172 */
Heiko Carstens2481a872014-08-15 12:33:46 +0200173
174#ifdef CONFIG_64BIT
175
176int ftrace_enable_ftrace_graph_caller(void)
177{
178 static unsigned short offset = 0x0002;
179
180 return probe_kernel_write((void *) ftrace_graph_caller + 2,
181 &offset, sizeof(offset));
182}
183
184int ftrace_disable_ftrace_graph_caller(void)
185{
186 unsigned short offset;
187
188 offset = ((void *) &ftrace_graph_caller_end -
189 (void *) ftrace_graph_caller) / 2;
190 return probe_kernel_write((void *) ftrace_graph_caller + 2,
191 &offset, sizeof(offset));
192}
193
194#else /* CONFIG_64BIT */
195
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100196int ftrace_enable_ftrace_graph_caller(void)
197{
198 unsigned short offset;
199
200 offset = ((void *) prepare_ftrace_return -
201 (void *) ftrace_graph_caller) / 2;
Heiko Carstens5eb8ae52013-09-06 19:16:14 +0200202 return probe_kernel_write((void *) ftrace_graph_caller + 2,
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100203 &offset, sizeof(offset));
204}
205
206int ftrace_disable_ftrace_graph_caller(void)
207{
208 static unsigned short offset = 0x0002;
209
Heiko Carstens5eb8ae52013-09-06 19:16:14 +0200210 return probe_kernel_write((void *) ftrace_graph_caller + 2,
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100211 &offset, sizeof(offset));
212}
213
Heiko Carstens2481a872014-08-15 12:33:46 +0200214#endif /* CONFIG_64BIT */
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100215#endif /* CONFIG_DYNAMIC_FTRACE */
Heiko Carstens88dbd202009-06-12 10:26:46 +0200216#endif /* CONFIG_FUNCTION_GRAPH_TRACER */