blob: fcb009d3edde42ee534bda94ac68f44e17bc710e [file] [log] [blame]
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02001/*
2 * Dynamic function tracer architecture backend.
3 *
Heiko Carstens3d1e2202014-09-03 13:26:23 +02004 * Copyright IBM Corp. 2009,2014
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02005 *
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 Carstens3d1e2202014-09-03 13:26:23 +020020void mcount_replace_code(void);
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020021void ftrace_disable_code(void);
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010022void ftrace_enable_insn(void);
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020023
24#ifdef CONFIG_64BIT
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010025/*
26 * The 64-bit mcount code looks like this:
27 * stg %r14,8(%r15) # offset 0
Heiko Carstens3d1e2202014-09-03 13:26:23 +020028 * larl %r1,<&counter> # offset 6
29 * brasl %r14,_mcount # offset 12
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010030 * lg %r14,8(%r15) # offset 18
Heiko Carstens3d1e2202014-09-03 13:26:23 +020031 * Total length is 24 bytes. The complete mcount block initially gets replaced
32 * by ftrace_make_nop. Subsequent calls to ftrace_make_call / ftrace_make_nop
33 * only patch the jg/lg instruction within the block.
34 * Note: we do not patch the first instruction to an unconditional branch,
35 * since that would break kprobes/jprobes. It is easier to leave the larl
36 * instruction in and only modify the second instruction.
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010037 * The 64-bit enabled ftrace code block looks like this:
Heiko Carstens3d1e2202014-09-03 13:26:23 +020038 * larl %r0,.+24 # offset 0
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010039 * > lg %r1,__LC_FTRACE_FUNC # offset 6
Heiko Carstens3d1e2202014-09-03 13:26:23 +020040 * br %r1 # offset 12
41 * brcl 0,0 # offset 14
42 * brc 0,0 # offset 20
43 * The ftrace function gets called with a non-standard C function call ABI
44 * where r0 contains the return address. It is also expected that the called
45 * function only clobbers r0 and r1, but restores r2-r15.
46 * The return point of the ftrace function has offset 24, so execution
47 * continues behind the mcount block.
48 * larl %r0,.+24 # offset 0
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010049 * > jg .+18 # offset 6
Heiko Carstens3d1e2202014-09-03 13:26:23 +020050 * br %r1 # offset 12
51 * brcl 0,0 # offset 14
52 * brc 0,0 # offset 20
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010053 * The jg instruction branches to offset 24 to skip as many instructions
54 * as possible.
55 */
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020056asm(
57 " .align 4\n"
Heiko Carstens3d1e2202014-09-03 13:26:23 +020058 "mcount_replace_code:\n"
59 " larl %r0,0f\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020060 "ftrace_disable_code:\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010061 " jg 0f\n"
Heiko Carstens3d1e2202014-09-03 13:26:23 +020062 " br %r1\n"
63 " brcl 0,0\n"
64 " brc 0,0\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010065 "0:\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020066 " .align 4\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010067 "ftrace_enable_insn:\n"
68 " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n");
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020069
Heiko Carstens3d1e2202014-09-03 13:26:23 +020070#define MCOUNT_BLOCK_SIZE 24
71#define MCOUNT_INSN_OFFSET 6
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010072#define FTRACE_INSN_SIZE 6
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020073
74#else /* CONFIG_64BIT */
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010075/*
76 * The 31-bit mcount code looks like this:
77 * st %r14,4(%r15) # offset 0
78 * > bras %r1,0f # offset 4
79 * > .long _mcount # offset 8
80 * > .long <&counter> # offset 12
81 * > 0: l %r14,0(%r1) # offset 16
82 * > l %r1,4(%r1) # offset 20
83 * basr %r14,%r14 # offset 24
84 * l %r14,4(%r15) # offset 26
85 * Total length is 30 bytes. The twenty bytes starting from offset 4
86 * to offset 24 get overwritten by ftrace_make_nop / ftrace_make_call.
87 * The 31-bit enabled ftrace code block looks like this:
88 * st %r14,4(%r15) # offset 0
89 * > l %r14,__LC_FTRACE_FUNC # offset 4
90 * > j 0f # offset 8
91 * > .fill 12,1,0x07 # offset 12
92 * 0: basr %r14,%r14 # offset 24
93 * l %r14,4(%r14) # offset 26
94 * The return points of the mcount/ftrace function have the same offset 26.
95 * The 31-bit disabled ftrace code block looks like this:
96 * st %r14,4(%r15) # offset 0
97 * > j .+26 # offset 4
98 * > j 0f # offset 8
99 * > .fill 12,1,0x07 # offset 12
100 * 0: basr %r14,%r14 # offset 24
101 * l %r14,4(%r14) # offset 26
102 * The j instruction branches to offset 30 to skip as many instructions
103 * as possible.
104 */
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200105asm(
106 " .align 4\n"
107 "ftrace_disable_code:\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100108 " j 1f\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200109 " j 0f\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100110 " .fill 12,1,0x07\n"
111 "0: basr %r14,%r14\n"
112 "1:\n"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200113 " .align 4\n"
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100114 "ftrace_enable_insn:\n"
115 " l %r14,"__stringify(__LC_FTRACE_FUNC)"\n");
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200116
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100117#define FTRACE_INSN_SIZE 4
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200118
119#endif /* CONFIG_64BIT */
120
Heiko Carstens10dec7d2014-08-15 13:01:46 +0200121#ifdef CONFIG_64BIT
122int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
123 unsigned long addr)
124{
125 return 0;
126}
127#endif
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200128
129int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
130 unsigned long addr)
131{
Heiko Carstens3d1e2202014-09-03 13:26:23 +0200132#ifdef CONFIG_64BIT
133 /* Initial replacement of the whole mcount block */
134 if (addr == MCOUNT_ADDR) {
135 if (probe_kernel_write((void *) rec->ip - MCOUNT_INSN_OFFSET,
136 mcount_replace_code,
137 MCOUNT_BLOCK_SIZE))
138 return -EPERM;
139 return 0;
140 }
141#endif
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100142 if (probe_kernel_write((void *) rec->ip, ftrace_disable_code,
143 MCOUNT_INSN_SIZE))
144 return -EPERM;
145 return 0;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200146}
147
148int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
149{
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100150 if (probe_kernel_write((void *) rec->ip, ftrace_enable_insn,
151 FTRACE_INSN_SIZE))
152 return -EPERM;
153 return 0;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200154}
155
156int ftrace_update_ftrace_func(ftrace_func_t func)
157{
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200158 return 0;
159}
160
Jiri Slaby3a36cb12014-02-24 19:59:59 +0100161int __init ftrace_dyn_arch_init(void)
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200162{
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200163 return 0;
164}
Heiko Carstens88dbd202009-06-12 10:26:46 +0200165
Heiko Carstens88dbd202009-06-12 10:26:46 +0200166#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Heiko Carstens88dbd202009-06-12 10:26:46 +0200167/*
168 * Hook the return address and push it in the stack of return addresses
169 * in current thread info.
170 */
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100171unsigned long __kprobes prepare_ftrace_return(unsigned long parent,
172 unsigned long ip)
Heiko Carstens88dbd202009-06-12 10:26:46 +0200173{
174 struct ftrace_graph_ent trace;
175
Heiko Carstens88dbd202009-06-12 10:26:46 +0200176 if (unlikely(atomic_read(&current->tracing_graph_pause)))
177 goto out;
Heiko Carstensaca91202013-05-13 14:48:52 +0200178 ip = (ip & PSW_ADDR_INSN) - MCOUNT_INSN_SIZE;
Heiko Carstens05e0baa2013-10-11 08:55:57 +0200179 trace.func = ip;
180 trace.depth = current->curr_ret_stack + 1;
181 /* Only trace if the calling function expects to. */
182 if (!ftrace_graph_entry(&trace))
183 goto out;
Steven Rostedt71e308a2009-06-18 12:45:08 -0400184 if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)
Heiko Carstens88dbd202009-06-12 10:26:46 +0200185 goto out;
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100186 parent = (unsigned long) return_to_handler;
Heiko Carstens88dbd202009-06-12 10:26:46 +0200187out:
188 return parent;
189}
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100190
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100191/*
192 * Patch the kernel code at ftrace_graph_caller location. The instruction
193 * there is branch relative and save to prepare_ftrace_return. To disable
194 * the call to prepare_ftrace_return we patch the bras offset to point
195 * directly after the instructions. To enable the call we calculate
196 * the original offset to prepare_ftrace_return and put it back.
197 */
Heiko Carstens2481a872014-08-15 12:33:46 +0200198
199#ifdef CONFIG_64BIT
200
201int ftrace_enable_ftrace_graph_caller(void)
202{
203 static unsigned short offset = 0x0002;
204
205 return probe_kernel_write((void *) ftrace_graph_caller + 2,
206 &offset, sizeof(offset));
207}
208
209int ftrace_disable_ftrace_graph_caller(void)
210{
211 unsigned short offset;
212
213 offset = ((void *) &ftrace_graph_caller_end -
214 (void *) ftrace_graph_caller) / 2;
215 return probe_kernel_write((void *) ftrace_graph_caller + 2,
216 &offset, sizeof(offset));
217}
218
219#else /* CONFIG_64BIT */
220
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100221int ftrace_enable_ftrace_graph_caller(void)
222{
223 unsigned short offset;
224
225 offset = ((void *) prepare_ftrace_return -
226 (void *) ftrace_graph_caller) / 2;
Heiko Carstens5eb8ae52013-09-06 19:16:14 +0200227 return probe_kernel_write((void *) ftrace_graph_caller + 2,
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100228 &offset, sizeof(offset));
229}
230
231int ftrace_disable_ftrace_graph_caller(void)
232{
233 static unsigned short offset = 0x0002;
234
Heiko Carstens5eb8ae52013-09-06 19:16:14 +0200235 return probe_kernel_write((void *) ftrace_graph_caller + 2,
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100236 &offset, sizeof(offset));
237}
238
Heiko Carstens2481a872014-08-15 12:33:46 +0200239#endif /* CONFIG_64BIT */
Heiko Carstens88dbd202009-06-12 10:26:46 +0200240#endif /* CONFIG_FUNCTION_GRAPH_TRACER */