···44 * Copyright IBM Corp. 200955 *66 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,77- *77+ * Martin Schwidefsky <schwidefsky@de.ibm.com>88 */991010#include <linux/hardirq.h>···1212#include <linux/ftrace.h>1313#include <linux/kernel.h>1414#include <linux/types.h>1515+#include <linux/kprobes.h>1516#include <trace/syscall.h>1617#include <asm/asm-offsets.h>1818+1919+#ifdef CONFIG_64BIT2020+#define MCOUNT_OFFSET_RET 122121+#else2222+#define MCOUNT_OFFSET_RET 222323+#endif17241825#ifdef CONFIG_DYNAMIC_FTRACE19262027void ftrace_disable_code(void);2121-void ftrace_disable_return(void);2222-void ftrace_call_code(void);2323-void ftrace_nop_code(void);2424-2525-#define FTRACE_INSN_SIZE 42828+void ftrace_enable_insn(void);26292730#ifdef CONFIG_64BIT2828-3131+/*3232+ * The 64-bit mcount code looks like this:3333+ * stg %r14,8(%r15) # offset 03434+ * > larl %r1,<&counter> # offset 63535+ * > brasl %r14,_mcount # offset 123636+ * lg %r14,8(%r15) # offset 183737+ * Total length is 24 bytes. The middle two instructions of the mcount3838+ * block get overwritten by ftrace_make_nop / ftrace_make_call.3939+ * The 64-bit enabled ftrace code block looks like this:4040+ * stg %r14,8(%r15) # offset 04141+ * > lg %r1,__LC_FTRACE_FUNC # offset 64242+ * > lgr %r0,%r0 # offset 124343+ * > basr %r14,%r1 # offset 164444+ * lg %r14,8(%15) # offset 184545+ * The return points of the mcount/ftrace function have the same offset 18.4646+ * The 64-bit disable ftrace code block looks like this:4747+ * stg %r14,8(%r15) # offset 04848+ * > jg .+18 # offset 64949+ * > lgr %r0,%r0 # offset 125050+ * > basr %r14,%r1 # offset 165151+ * lg %r14,8(%15) # offset 185252+ * The jg instruction branches to offset 24 to skip as many instructions5353+ * as possible.5454+ */2955asm(3056 " .align 4\n"3157 "ftrace_disable_code:\n"3232- " j 0f\n"3333- " .word 0x0024\n"3434- " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n"3535- " basr %r14,%r1\n"3636- "ftrace_disable_return:\n"3737- " lg %r14,8(15)\n"5858+ " jg 0f\n"3859 " lgr %r0,%r0\n"3939- "0:\n");4040-4141-asm(6060+ " basr %r14,%r1\n"6161+ "0:\n"4262 " .align 4\n"4343- "ftrace_nop_code:\n"4444- " j .+"__stringify(MCOUNT_INSN_SIZE)"\n");6363+ "ftrace_enable_insn:\n"6464+ " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n");45654646-asm(4747- " .align 4\n"4848- "ftrace_call_code:\n"4949- " stg %r14,8(%r15)\n");6666+#define FTRACE_INSN_SIZE 650675168#else /* CONFIG_64BIT */5252-6969+/*7070+ * The 31-bit mcount code looks like this:7171+ * st %r14,4(%r15) # offset 07272+ * > bras %r1,0f # offset 47373+ * > .long _mcount # offset 87474+ * > .long <&counter> # offset 127575+ * > 0: l %r14,0(%r1) # offset 167676+ * > l %r1,4(%r1) # offset 207777+ * basr %r14,%r14 # offset 247878+ * l %r14,4(%r15) # offset 267979+ * Total length is 30 bytes. The twenty bytes starting from offset 48080+ * to offset 24 get overwritten by ftrace_make_nop / ftrace_make_call.8181+ * The 31-bit enabled ftrace code block looks like this:8282+ * st %r14,4(%r15) # offset 08383+ * > l %r14,__LC_FTRACE_FUNC # offset 48484+ * > j 0f # offset 88585+ * > .fill 12,1,0x07 # offset 128686+ * 0: basr %r14,%r14 # offset 248787+ * l %r14,4(%r14) # offset 268888+ * The return points of the mcount/ftrace function have the same offset 26.8989+ * The 31-bit disabled ftrace code block looks like this:9090+ * st %r14,4(%r15) # offset 09191+ * > j .+26 # offset 49292+ * > j 0f # offset 89393+ * > .fill 12,1,0x07 # offset 129494+ * 0: basr %r14,%r14 # offset 249595+ * l %r14,4(%r14) # offset 269696+ * The j instruction branches to offset 30 to skip as many instructions9797+ * as possible.9898+ */5399asm(54100 " .align 4\n"55101 "ftrace_disable_code:\n"102102+ " j 1f\n"56103 " j 0f\n"5757- " l %r1,"__stringify(__LC_FTRACE_FUNC)"\n"5858- " basr %r14,%r1\n"5959- "ftrace_disable_return:\n"6060- " l %r14,4(%r15)\n"6161- " j 0f\n"6262- " bcr 0,%r7\n"6363- " bcr 0,%r7\n"6464- " bcr 0,%r7\n"6565- " bcr 0,%r7\n"6666- " bcr 0,%r7\n"6767- " bcr 0,%r7\n"6868- "0:\n");6969-7070-asm(104104+ " .fill 12,1,0x07\n"105105+ "0: basr %r14,%r14\n"106106+ "1:\n"71107 " .align 4\n"7272- "ftrace_nop_code:\n"7373- " j .+"__stringify(MCOUNT_INSN_SIZE)"\n");108108+ "ftrace_enable_insn:\n"109109+ " l %r14,"__stringify(__LC_FTRACE_FUNC)"\n");741107575-asm(7676- " .align 4\n"7777- "ftrace_call_code:\n"7878- " st %r14,4(%r15)\n");111111+#define FTRACE_INSN_SIZE 47911280113#endif /* CONFIG_64BIT */811148282-static int ftrace_modify_code(unsigned long ip,8383- void *old_code, int old_size,8484- void *new_code, int new_size)8585-{8686- unsigned char replaced[MCOUNT_INSN_SIZE];8787-8888- /*8989- * Note: Due to modules code can disappear and change.9090- * We need to protect against faulting as well as code9191- * changing. We do this by using the probe_kernel_*9292- * functions.9393- * This however is just a simple sanity check.9494- */9595- if (probe_kernel_read(replaced, (void *)ip, old_size))9696- return -EFAULT;9797- if (memcmp(replaced, old_code, old_size) != 0)9898- return -EINVAL;9999- if (probe_kernel_write((void *)ip, new_code, new_size))100100- return -EPERM;101101- return 0;102102-}103103-104104-static int ftrace_make_initial_nop(struct module *mod, struct dyn_ftrace *rec,105105- unsigned long addr)106106-{107107- return ftrace_modify_code(rec->ip,108108- ftrace_call_code, FTRACE_INSN_SIZE,109109- ftrace_disable_code, MCOUNT_INSN_SIZE);110110-}111115112116int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,113117 unsigned long addr)114118{115115- if (addr == MCOUNT_ADDR)116116- return ftrace_make_initial_nop(mod, rec, addr);117117- return ftrace_modify_code(rec->ip,118118- ftrace_call_code, FTRACE_INSN_SIZE,119119- ftrace_nop_code, FTRACE_INSN_SIZE);119119+ if (probe_kernel_write((void *) rec->ip, ftrace_disable_code,120120+ MCOUNT_INSN_SIZE))121121+ return -EPERM;122122+ return 0;120123}121124122125int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)123126{124124- return ftrace_modify_code(rec->ip,125125- ftrace_nop_code, FTRACE_INSN_SIZE,126126- ftrace_call_code, FTRACE_INSN_SIZE);127127+ if (probe_kernel_write((void *) rec->ip, ftrace_enable_insn,128128+ FTRACE_INSN_SIZE))129129+ return -EPERM;130130+ return 0;127131}128132129133int ftrace_update_ftrace_func(ftrace_func_t func)130134{131131- ftrace_dyn_func = (unsigned long)func;132135 return 0;133136}134137135138int __init ftrace_dyn_arch_init(void *data)136139{137137- *(unsigned long *)data = 0;140140+ *(unsigned long *) data = 0;138141 return 0;139142}140143141144#endif /* CONFIG_DYNAMIC_FTRACE */142145143146#ifdef CONFIG_FUNCTION_GRAPH_TRACER144144-#ifdef CONFIG_DYNAMIC_FTRACE145145-/*146146- * Patch the kernel code at ftrace_graph_caller location:147147- * The instruction there is branch relative on condition. The condition mask148148- * is either all ones (always branch aka disable ftrace_graph_caller) or all149149- * zeroes (nop aka enable ftrace_graph_caller).150150- * Instruction format for brc is a7m4xxxx where m is the condition mask.151151- */152152-int ftrace_enable_ftrace_graph_caller(void)153153-{154154- unsigned short opcode = 0xa704;155155-156156- return probe_kernel_write(ftrace_graph_caller, &opcode, sizeof(opcode));157157-}158158-159159-int ftrace_disable_ftrace_graph_caller(void)160160-{161161- unsigned short opcode = 0xa7f4;162162-163163- return probe_kernel_write(ftrace_graph_caller, &opcode, sizeof(opcode));164164-}165165-166166-static inline unsigned long ftrace_mcount_call_adjust(unsigned long addr)167167-{168168- return addr - (ftrace_disable_return - ftrace_disable_code);169169-}170170-171171-#else /* CONFIG_DYNAMIC_FTRACE */172172-173173-static inline unsigned long ftrace_mcount_call_adjust(unsigned long addr)174174-{175175- return addr - MCOUNT_OFFSET_RET;176176-}177177-178178-#endif /* CONFIG_DYNAMIC_FTRACE */179179-180147/*181148 * Hook the return address and push it in the stack of return addresses182149 * in current thread info.183150 */184184-unsigned long prepare_ftrace_return(unsigned long ip, unsigned long parent)151151+unsigned long __kprobes prepare_ftrace_return(unsigned long parent,152152+ unsigned long ip)185153{186154 struct ftrace_graph_ent trace;187155···157189 goto out;158190 if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)159191 goto out;160160- trace.func = ftrace_mcount_call_adjust(ip) & PSW_ADDR_INSN;192192+ trace.func = (ip & PSW_ADDR_INSN) - MCOUNT_OFFSET_RET;161193 /* Only trace if the calling function expects to. */162194 if (!ftrace_graph_entry(&trace)) {163195 current->curr_ret_stack--;164196 goto out;165197 }166166- parent = (unsigned long)return_to_handler;198198+ parent = (unsigned long) return_to_handler;167199out:168200 return parent;169201}202202+203203+#ifdef CONFIG_DYNAMIC_FTRACE204204+/*205205+ * Patch the kernel code at ftrace_graph_caller location. The instruction206206+ * there is branch relative and save to prepare_ftrace_return. To disable207207+ * the call to prepare_ftrace_return we patch the bras offset to point208208+ * directly after the instructions. To enable the call we calculate209209+ * the original offset to prepare_ftrace_return and put it back.210210+ */211211+int ftrace_enable_ftrace_graph_caller(void)212212+{213213+ unsigned short offset;214214+215215+ offset = ((void *) prepare_ftrace_return -216216+ (void *) ftrace_graph_caller) / 2;217217+ return probe_kernel_write(ftrace_graph_caller + 2,218218+ &offset, sizeof(offset));219219+}220220+221221+int ftrace_disable_ftrace_graph_caller(void)222222+{223223+ static unsigned short offset = 0x0002;224224+225225+ return probe_kernel_write(ftrace_graph_caller + 2,226226+ &offset, sizeof(offset));227227+}228228+229229+#endif /* CONFIG_DYNAMIC_FTRACE */170230#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+7-23
arch/s390/kernel/mcount.S
···1818#ifdef CONFIG_DYNAMIC_FTRACE1919 br %r1420202121- .data2222- .globl ftrace_dyn_func2323-ftrace_dyn_func:2424- .long ftrace_stub2525- .previous2626-2721 .globl ftrace_caller2822ftrace_caller:2923#endif3024 stm %r2,%r5,16(%r15)3125 bras %r1,2f3232-#ifdef CONFIG_DYNAMIC_FTRACE3333-0: .long ftrace_dyn_func3434-#else35260: .long ftrace_trace_function3636-#endif37271: .long function_trace_stop38282: l %r2,1b-0b(%r1)3929 icm %r2,0xf,0(%r2)···3949 l %r14,0(%r14)4050 basr %r14,%r144151#ifdef CONFIG_FUNCTION_GRAPH_TRACER4242-#ifdef CONFIG_DYNAMIC_FTRACE5252+ l %r2,100(%r15)5353+ l %r3,152(%r15)4354 .globl ftrace_graph_caller4455ftrace_graph_caller:4545- # This unconditional branch gets runtime patched. Change only if4646- # you know what you are doing. See ftrace_enable_graph_caller().4747- j 1f4848-#endif4949- bras %r1,0f5050- .long prepare_ftrace_return5151-0: l %r2,152(%r15)5252- l %r4,0(%r1)5353- l %r3,100(%r15)5454- basr %r14,%r45555- st %r2,100(%r15)5656-1:5656+# The bras instruction gets runtime patched to call prepare_ftrace_return.5757+# See ftrace_enable_ftrace_graph_caller. The patched instruction is:5858+# bras %r14,prepare_ftrace_return5959+ bras %r14,0f6060+0: st %r2,100(%r15)5761#endif5862 ahi %r15,965963 l %r14,56(%r15)
+7-20
arch/s390/kernel/mcount64.S
···1818#ifdef CONFIG_DYNAMIC_FTRACE1919 br %r1420202121- .data2222- .globl ftrace_dyn_func2323-ftrace_dyn_func:2424- .quad ftrace_stub2525- .previous2626-2721 .globl ftrace_caller2822ftrace_caller:2923#endif···3137 stg %r1,__SF_BACKCHAIN(%r15)3238 lgr %r2,%r143339 lg %r3,168(%r15)3434-#ifdef CONFIG_DYNAMIC_FTRACE3535- larl %r14,ftrace_dyn_func3636-#else3740 larl %r14,ftrace_trace_function3838-#endif3941 lg %r14,0(%r14)4042 basr %r14,%r144143#ifdef CONFIG_FUNCTION_GRAPH_TRACER4242-#ifdef CONFIG_DYNAMIC_FTRACE4444+ lg %r2,168(%r15)4545+ lg %r3,272(%r15)4346 .globl ftrace_graph_caller4447ftrace_graph_caller:4545- # This unconditional branch gets runtime patched. Change only if4646- # you know what you are doing. See ftrace_enable_graph_caller().4747- j 0f4848-#endif4949- lg %r2,272(%r15)5050- lg %r3,168(%r15)5151- brasl %r14,prepare_ftrace_return5252- stg %r2,168(%r15)5353-0:4848+# The bras instruction gets runtime patched to call prepare_ftrace_return.4949+# See ftrace_enable_ftrace_graph_caller. The patched instruction is:5050+# bras %r14,prepare_ftrace_return5151+ bras %r14,0f5252+0: stg %r2,168(%r15)5453#endif5554 aghi %r15,1605655 lmg %r2,%r5,32(%r15)