blob: 84be7f02d0c2157029cb2868231b67c7603bbf42 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02002/*
3 * Dynamic function tracer architecture backend.
4 *
Heiko Carstens3d1e2202014-09-03 13:26:23 +02005 * Copyright IBM Corp. 2009,2014
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02006 *
7 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +01008 * Martin Schwidefsky <schwidefsky@de.ibm.com>
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +02009 */
10
Heiko Carstensc9331462014-10-15 12:17:38 +020011#include <linux/moduleloader.h>
Heiko Carstens88dbd202009-06-12 10:26:46 +020012#include <linux/hardirq.h>
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020013#include <linux/uaccess.h>
14#include <linux/ftrace.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010017#include <linux/kprobes.h>
Heiko Carstens9bf12262009-06-12 10:26:47 +020018#include <trace/syscall.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010019#include <asm/asm-offsets.h>
Heiko Carstensc9331462014-10-15 12:17:38 +020020#include <asm/cacheflush.h>
Laura Abbotte6c7c632017-05-08 15:58:08 -070021#include <asm/set_memory.h>
Heiko Carstens63df41d62013-09-06 19:10:48 +020022#include "entry.h"
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020023
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010024/*
Heiko Carstens53255c92014-10-07 15:45:10 +020025 * The mcount code looks like this:
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010026 * stg %r14,8(%r15) # offset 0
Heiko Carstens3d1e2202014-09-03 13:26:23 +020027 * larl %r1,<&counter> # offset 6
28 * brasl %r14,_mcount # offset 12
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010029 * lg %r14,8(%r15) # offset 18
Heiko Carstensc9331462014-10-15 12:17:38 +020030 * Total length is 24 bytes. Only the first instruction will be patched
31 * by ftrace_make_call / ftrace_make_nop.
Heiko Carstens53255c92014-10-07 15:45:10 +020032 * The enabled ftrace code block looks like this:
Heiko Carstensc9331462014-10-15 12:17:38 +020033 * > brasl %r0,ftrace_caller # offset 0
34 * larl %r1,<&counter> # offset 6
35 * brasl %r14,_mcount # offset 12
36 * lg %r14,8(%r15) # offset 18
Heiko Carstens3d1e2202014-09-03 13:26:23 +020037 * The ftrace function gets called with a non-standard C function call ABI
38 * where r0 contains the return address. It is also expected that the called
39 * function only clobbers r0 and r1, but restores r2-r15.
Heiko Carstensc9331462014-10-15 12:17:38 +020040 * For module code we can't directly jump to ftrace caller, but need a
41 * trampoline (ftrace_plt), which clobbers also r1.
Heiko Carstens3d1e2202014-09-03 13:26:23 +020042 * The return point of the ftrace function has offset 24, so execution
43 * continues behind the mcount block.
Heiko Carstensc9331462014-10-15 12:17:38 +020044 * The disabled ftrace code block looks like this:
45 * > jg .+24 # offset 0
46 * larl %r1,<&counter> # offset 6
47 * brasl %r14,_mcount # offset 12
48 * lg %r14,8(%r15) # offset 18
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010049 * The jg instruction branches to offset 24 to skip as many instructions
50 * as possible.
Heiko Carstense6d60b32015-01-09 13:08:28 +010051 * In case we use gcc's hotpatch feature the original and also the disabled
52 * function prologue contains only a single six byte instruction and looks
53 * like this:
54 * > brcl 0,0 # offset 0
55 * To enable ftrace the code gets patched like above and afterwards looks
56 * like this:
57 * > brasl %r0,ftrace_caller # offset 0
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +010058 */
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020059
Heiko Carstensc9331462014-10-15 12:17:38 +020060unsigned long ftrace_plt;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +020061
Heiko Carstens20e76ee2015-03-13 10:31:42 +010062static inline void ftrace_generate_orig_insn(struct ftrace_insn *insn)
63{
Vasily Gorbikd983c892018-08-06 15:17:47 +020064#if defined(CC_USING_HOTPATCH) || defined(CC_USING_NOP_MCOUNT)
Heiko Carstens20e76ee2015-03-13 10:31:42 +010065 /* brcl 0,0 */
66 insn->opc = 0xc004;
67 insn->disp = 0;
68#else
69 /* stg r14,8(r15) */
70 insn->opc = 0xe3e0;
71 insn->disp = 0xf0080024;
72#endif
73}
74
75static inline int is_kprobe_on_ftrace(struct ftrace_insn *insn)
76{
77#ifdef CONFIG_KPROBES
78 if (insn->opc == BREAKPOINT_INSTRUCTION)
79 return 1;
80#endif
81 return 0;
82}
83
84static inline void ftrace_generate_kprobe_nop_insn(struct ftrace_insn *insn)
85{
86#ifdef CONFIG_KPROBES
87 insn->opc = BREAKPOINT_INSTRUCTION;
88 insn->disp = KPROBE_ON_FTRACE_NOP;
89#endif
90}
91
92static inline void ftrace_generate_kprobe_call_insn(struct ftrace_insn *insn)
93{
94#ifdef CONFIG_KPROBES
95 insn->opc = BREAKPOINT_INSTRUCTION;
96 insn->disp = KPROBE_ON_FTRACE_CALL;
97#endif
98}
99
Heiko Carstens10dec7d2014-08-15 13:01:46 +0200100int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
101 unsigned long addr)
102{
103 return 0;
104}
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200105
106int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
107 unsigned long addr)
108{
Heiko Carstens58498ee2014-12-09 10:18:49 +0100109 struct ftrace_insn orig, new, old;
Heiko Carstensc9331462014-10-15 12:17:38 +0200110
Heiko Carstens58498ee2014-12-09 10:18:49 +0100111 if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
Heiko Carstensc9331462014-10-15 12:17:38 +0200112 return -EFAULT;
Heiko Carstens58498ee2014-12-09 10:18:49 +0100113 if (addr == MCOUNT_ADDR) {
Heiko Carstense6d60b32015-01-09 13:08:28 +0100114 /* Initial code replacement */
Heiko Carstens20e76ee2015-03-13 10:31:42 +0100115 ftrace_generate_orig_insn(&orig);
Heiko Carstens58498ee2014-12-09 10:18:49 +0100116 ftrace_generate_nop_insn(&new);
Heiko Carstens20e76ee2015-03-13 10:31:42 +0100117 } else if (is_kprobe_on_ftrace(&old)) {
Heiko Carstens58498ee2014-12-09 10:18:49 +0100118 /*
119 * If we find a breakpoint instruction, a kprobe has been
120 * placed at the beginning of the function. We write the
121 * constant KPROBE_ON_FTRACE_NOP into the remaining four
122 * bytes of the original instruction so that the kprobes
123 * handler can execute a nop, if it reaches this breakpoint.
124 */
Heiko Carstens20e76ee2015-03-13 10:31:42 +0100125 ftrace_generate_kprobe_call_insn(&orig);
126 ftrace_generate_kprobe_nop_insn(&new);
Heiko Carstens58498ee2014-12-09 10:18:49 +0100127 } else {
128 /* Replace ftrace call with a nop. */
129 ftrace_generate_call_insn(&orig, rec->ip);
130 ftrace_generate_nop_insn(&new);
Heiko Carstens3d1e2202014-09-03 13:26:23 +0200131 }
Heiko Carstens58498ee2014-12-09 10:18:49 +0100132 /* Verify that the to be replaced code matches what we expect. */
133 if (memcmp(&orig, &old, sizeof(old)))
134 return -EINVAL;
Heiko Carstens8a5d8472015-03-13 12:55:56 +0100135 s390_kernel_write((void *) rec->ip, &new, sizeof(new));
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100136 return 0;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200137}
138
139int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
140{
Heiko Carstens58498ee2014-12-09 10:18:49 +0100141 struct ftrace_insn orig, new, old;
Heiko Carstensc9331462014-10-15 12:17:38 +0200142
Heiko Carstens58498ee2014-12-09 10:18:49 +0100143 if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
Heiko Carstensc9331462014-10-15 12:17:38 +0200144 return -EFAULT;
Heiko Carstens20e76ee2015-03-13 10:31:42 +0100145 if (is_kprobe_on_ftrace(&old)) {
Heiko Carstens58498ee2014-12-09 10:18:49 +0100146 /*
147 * If we find a breakpoint instruction, a kprobe has been
148 * placed at the beginning of the function. We write the
149 * constant KPROBE_ON_FTRACE_CALL into the remaining four
150 * bytes of the original instruction so that the kprobes
151 * handler can execute a brasl if it reaches this breakpoint.
152 */
Heiko Carstens20e76ee2015-03-13 10:31:42 +0100153 ftrace_generate_kprobe_nop_insn(&orig);
154 ftrace_generate_kprobe_call_insn(&new);
Heiko Carstens58498ee2014-12-09 10:18:49 +0100155 } else {
156 /* Replace nop with an ftrace call. */
157 ftrace_generate_nop_insn(&orig);
158 ftrace_generate_call_insn(&new, rec->ip);
Heiko Carstensc9331462014-10-15 12:17:38 +0200159 }
Heiko Carstens58498ee2014-12-09 10:18:49 +0100160 /* Verify that the to be replaced code matches what we expect. */
161 if (memcmp(&orig, &old, sizeof(old)))
162 return -EINVAL;
Heiko Carstens8a5d8472015-03-13 12:55:56 +0100163 s390_kernel_write((void *) rec->ip, &new, sizeof(new));
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100164 return 0;
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200165}
166
167int ftrace_update_ftrace_func(ftrace_func_t func)
168{
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200169 return 0;
170}
171
Jiri Slaby3a36cb12014-02-24 19:59:59 +0100172int __init ftrace_dyn_arch_init(void)
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200173{
Heiko Carstensdfd9f7a2009-06-12 10:26:44 +0200174 return 0;
175}
Heiko Carstens88dbd202009-06-12 10:26:46 +0200176
Heiko Carstens085b6ba2017-05-02 12:38:57 +0200177#ifdef CONFIG_MODULES
178
Heiko Carstensc9331462014-10-15 12:17:38 +0200179static int __init ftrace_plt_init(void)
180{
181 unsigned int *ip;
182
183 ftrace_plt = (unsigned long) module_alloc(PAGE_SIZE);
184 if (!ftrace_plt)
185 panic("cannot allocate ftrace plt\n");
186 ip = (unsigned int *) ftrace_plt;
187 ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
188 ip[1] = 0x100a0004;
189 ip[2] = 0x07f10000;
190 ip[3] = FTRACE_ADDR >> 32;
191 ip[4] = FTRACE_ADDR & 0xffffffff;
192 set_memory_ro(ftrace_plt, 1);
193 return 0;
194}
195device_initcall(ftrace_plt_init);
196
Heiko Carstens085b6ba2017-05-02 12:38:57 +0200197#endif /* CONFIG_MODULES */
198
Heiko Carstens88dbd202009-06-12 10:26:46 +0200199#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Heiko Carstens88dbd202009-06-12 10:26:46 +0200200/*
201 * Hook the return address and push it in the stack of return addresses
202 * in current thread info.
203 */
Heiko Carstens7a5388d2014-10-22 12:42:38 +0200204unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
Heiko Carstens88dbd202009-06-12 10:26:46 +0200205{
206 struct ftrace_graph_ent trace;
207
Heiko Carstens6ed15ea2014-10-27 15:49:06 +0100208 if (unlikely(ftrace_graph_is_dead()))
209 goto out;
Heiko Carstens88dbd202009-06-12 10:26:46 +0200210 if (unlikely(atomic_read(&current->tracing_graph_pause)))
211 goto out;
Heiko Carstens9cb1cce2016-01-18 13:12:19 +0100212 ip -= MCOUNT_INSN_SIZE;
Heiko Carstens05e0baa2013-10-11 08:55:57 +0200213 trace.func = ip;
214 trace.depth = current->curr_ret_stack + 1;
215 /* Only trace if the calling function expects to. */
216 if (!ftrace_graph_entry(&trace))
217 goto out;
Josh Poimboeuf9a7c3482016-08-19 06:52:57 -0500218 if (ftrace_push_return_trace(parent, ip, &trace.depth, 0,
219 NULL) == -EBUSY)
Heiko Carstens88dbd202009-06-12 10:26:46 +0200220 goto out;
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100221 parent = (unsigned long) return_to_handler;
Heiko Carstens88dbd202009-06-12 10:26:46 +0200222out:
223 return parent;
224}
Heiko Carstens7a5388d2014-10-22 12:42:38 +0200225NOKPROBE_SYMBOL(prepare_ftrace_return);
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100226
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100227/*
228 * Patch the kernel code at ftrace_graph_caller location. The instruction
Heiko Carstens0cccdda2014-10-08 10:03:08 +0200229 * there is branch relative on condition. To enable the ftrace graph code
230 * block, we simply patch the mask field of the instruction to zero and
231 * turn the instruction into a nop.
232 * To disable the ftrace graph code the mask field will be patched to
233 * all ones, which turns the instruction into an unconditional branch.
Martin Schwidefsky4cc9bed2011-01-05 12:48:11 +0100234 */
Heiko Carstens2481a872014-08-15 12:33:46 +0200235int ftrace_enable_ftrace_graph_caller(void)
236{
Heiko Carstens0cccdda2014-10-08 10:03:08 +0200237 u8 op = 0x04; /* set mask field to zero */
Heiko Carstens2481a872014-08-15 12:33:46 +0200238
Heiko Carstens8a5d8472015-03-13 12:55:56 +0100239 s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
240 return 0;
Heiko Carstens2481a872014-08-15 12:33:46 +0200241}
242
243int ftrace_disable_ftrace_graph_caller(void)
244{
Heiko Carstens0cccdda2014-10-08 10:03:08 +0200245 u8 op = 0xf4; /* set mask field to all ones */
Heiko Carstens2481a872014-08-15 12:33:46 +0200246
Heiko Carstens8a5d8472015-03-13 12:55:56 +0100247 s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
248 return 0;
Heiko Carstens2481a872014-08-15 12:33:46 +0200249}
250
Heiko Carstens88dbd202009-06-12 10:26:46 +0200251#endif /* CONFIG_FUNCTION_GRAPH_TRACER */