xref: /openbmc/linux/arch/sparc/kernel/ftrace.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a88b5ba8SSam Ravnborg #include <linux/spinlock.h>
3a88b5ba8SSam Ravnborg #include <linux/hardirq.h>
4a88b5ba8SSam Ravnborg #include <linux/ftrace.h>
5a88b5ba8SSam Ravnborg #include <linux/percpu.h>
6a88b5ba8SSam Ravnborg #include <linux/init.h>
7a88b5ba8SSam Ravnborg #include <linux/list.h>
8c658ad1bSDavid S. Miller #include <trace/syscall.h>
9a88b5ba8SSam Ravnborg 
10a88b5ba8SSam Ravnborg #include <asm/ftrace.h>
11a88b5ba8SSam Ravnborg 
129be12f9bSDavid S. Miller #ifdef CONFIG_DYNAMIC_FTRACE
13a88b5ba8SSam Ravnborg static const u32 ftrace_nop = 0x01000000;
14a88b5ba8SSam Ravnborg 
ftrace_call_replace(unsigned long ip,unsigned long addr)159be12f9bSDavid S. Miller static u32 ftrace_call_replace(unsigned long ip, unsigned long addr)
16a88b5ba8SSam Ravnborg {
17d96478d5SDavid S. Miller 	u32 call;
18a88b5ba8SSam Ravnborg 	s32 off;
19a88b5ba8SSam Ravnborg 
20a88b5ba8SSam Ravnborg 	off = ((s32)addr - (s32)ip);
21a88b5ba8SSam Ravnborg 	call = 0x40000000 | ((u32)off >> 2);
22a88b5ba8SSam Ravnborg 
239be12f9bSDavid S. Miller 	return call;
24a88b5ba8SSam Ravnborg }
25a88b5ba8SSam Ravnborg 
ftrace_modify_code(unsigned long ip,u32 old,u32 new)269be12f9bSDavid S. Miller static int ftrace_modify_code(unsigned long ip, u32 old, u32 new)
27a88b5ba8SSam Ravnborg {
28a88b5ba8SSam Ravnborg 	u32 replaced;
29a88b5ba8SSam Ravnborg 	int faulted;
30a88b5ba8SSam Ravnborg 
31a88b5ba8SSam Ravnborg 	__asm__ __volatile__(
32a88b5ba8SSam Ravnborg 	"1:	cas	[%[ip]], %[old], %[new]\n"
33a88b5ba8SSam Ravnborg 	"	flush	%[ip]\n"
34a88b5ba8SSam Ravnborg 	"	mov	0, %[faulted]\n"
35a88b5ba8SSam Ravnborg 	"2:\n"
36a88b5ba8SSam Ravnborg 	"	.section .fixup,#alloc,#execinstr\n"
37a88b5ba8SSam Ravnborg 	"	.align	4\n"
38a88b5ba8SSam Ravnborg 	"3:	sethi	%%hi(2b), %[faulted]\n"
39a88b5ba8SSam Ravnborg 	"	jmpl	%[faulted] + %%lo(2b), %%g0\n"
40a88b5ba8SSam Ravnborg 	"	 mov	1, %[faulted]\n"
41a88b5ba8SSam Ravnborg 	"	.previous\n"
42a88b5ba8SSam Ravnborg 	"	.section __ex_table,\"a\"\n"
43a88b5ba8SSam Ravnborg 	"	.align	4\n"
44a88b5ba8SSam Ravnborg 	"	.word	1b, 3b\n"
45a88b5ba8SSam Ravnborg 	"	.previous\n"
46a88b5ba8SSam Ravnborg 	: "=r" (replaced), [faulted] "=r" (faulted)
47a88b5ba8SSam Ravnborg 	: [new] "0" (new), [old] "r" (old), [ip] "r" (ip)
48a88b5ba8SSam Ravnborg 	: "memory");
49a88b5ba8SSam Ravnborg 
50a88b5ba8SSam Ravnborg 	if (replaced != old && replaced != new)
51a88b5ba8SSam Ravnborg 		faulted = 2;
52a88b5ba8SSam Ravnborg 
53a88b5ba8SSam Ravnborg 	return faulted;
54a88b5ba8SSam Ravnborg }
55a88b5ba8SSam Ravnborg 
ftrace_make_nop(struct module * mod,struct dyn_ftrace * rec,unsigned long addr)569be12f9bSDavid S. Miller int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
579be12f9bSDavid S. Miller {
589be12f9bSDavid S. Miller 	unsigned long ip = rec->ip;
599be12f9bSDavid S. Miller 	u32 old, new;
609be12f9bSDavid S. Miller 
619be12f9bSDavid S. Miller 	old = ftrace_call_replace(ip, addr);
629be12f9bSDavid S. Miller 	new = ftrace_nop;
639be12f9bSDavid S. Miller 	return ftrace_modify_code(ip, old, new);
649be12f9bSDavid S. Miller }
659be12f9bSDavid S. Miller 
ftrace_make_call(struct dyn_ftrace * rec,unsigned long addr)669be12f9bSDavid S. Miller int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
679be12f9bSDavid S. Miller {
689be12f9bSDavid S. Miller 	unsigned long ip = rec->ip;
699be12f9bSDavid S. Miller 	u32 old, new;
709be12f9bSDavid S. Miller 
719be12f9bSDavid S. Miller 	old = ftrace_nop;
729be12f9bSDavid S. Miller 	new = ftrace_call_replace(ip, addr);
739be12f9bSDavid S. Miller 	return ftrace_modify_code(ip, old, new);
749be12f9bSDavid S. Miller }
759be12f9bSDavid S. Miller 
ftrace_update_ftrace_func(ftrace_func_t func)76a88b5ba8SSam Ravnborg int ftrace_update_ftrace_func(ftrace_func_t func)
77a88b5ba8SSam Ravnborg {
78a88b5ba8SSam Ravnborg 	unsigned long ip = (unsigned long)(&ftrace_call);
799be12f9bSDavid S. Miller 	u32 old, new;
80a88b5ba8SSam Ravnborg 
819be12f9bSDavid S. Miller 	old = *(u32 *) &ftrace_call;
82a88b5ba8SSam Ravnborg 	new = ftrace_call_replace(ip, (unsigned long)func);
83a88b5ba8SSam Ravnborg 	return ftrace_modify_code(ip, old, new);
84a88b5ba8SSam Ravnborg }
859be12f9bSDavid S. Miller #endif
869960e9e8SDavid S. Miller 
879960e9e8SDavid S. Miller #ifdef CONFIG_FUNCTION_GRAPH_TRACER
889960e9e8SDavid S. Miller 
899960e9e8SDavid S. Miller #ifdef CONFIG_DYNAMIC_FTRACE
909960e9e8SDavid S. Miller extern void ftrace_graph_call(void);
919960e9e8SDavid S. Miller 
ftrace_enable_ftrace_graph_caller(void)929960e9e8SDavid S. Miller int ftrace_enable_ftrace_graph_caller(void)
939960e9e8SDavid S. Miller {
949960e9e8SDavid S. Miller 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
959960e9e8SDavid S. Miller 	u32 old, new;
969960e9e8SDavid S. Miller 
979960e9e8SDavid S. Miller 	old = *(u32 *) &ftrace_graph_call;
989960e9e8SDavid S. Miller 	new = ftrace_call_replace(ip, (unsigned long) &ftrace_graph_caller);
999960e9e8SDavid S. Miller 	return ftrace_modify_code(ip, old, new);
1009960e9e8SDavid S. Miller }
1019960e9e8SDavid S. Miller 
ftrace_disable_ftrace_graph_caller(void)1029960e9e8SDavid S. Miller int ftrace_disable_ftrace_graph_caller(void)
1039960e9e8SDavid S. Miller {
1049960e9e8SDavid S. Miller 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
1059960e9e8SDavid S. Miller 	u32 old, new;
1069960e9e8SDavid S. Miller 
1079960e9e8SDavid S. Miller 	old = *(u32 *) &ftrace_graph_call;
1089960e9e8SDavid S. Miller 	new = ftrace_call_replace(ip, (unsigned long) &ftrace_stub);
1099960e9e8SDavid S. Miller 
1109960e9e8SDavid S. Miller 	return ftrace_modify_code(ip, old, new);
1119960e9e8SDavid S. Miller }
1129960e9e8SDavid S. Miller 
1139960e9e8SDavid S. Miller #endif /* !CONFIG_DYNAMIC_FTRACE */
1149960e9e8SDavid S. Miller 
1159960e9e8SDavid S. Miller /*
1169960e9e8SDavid S. Miller  * Hook the return address and push it in the stack of return addrs
1179960e9e8SDavid S. Miller  * in current thread info.
1189960e9e8SDavid S. Miller  */
prepare_ftrace_return(unsigned long parent,unsigned long self_addr,unsigned long frame_pointer)1199960e9e8SDavid S. Miller unsigned long prepare_ftrace_return(unsigned long parent,
1209960e9e8SDavid S. Miller 				    unsigned long self_addr,
1219960e9e8SDavid S. Miller 				    unsigned long frame_pointer)
1229960e9e8SDavid S. Miller {
1239960e9e8SDavid S. Miller 	unsigned long return_hooker = (unsigned long) &return_to_handler;
1249960e9e8SDavid S. Miller 
1259960e9e8SDavid S. Miller 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
1269960e9e8SDavid S. Miller 		return parent + 8UL;
1279960e9e8SDavid S. Miller 
128*9c4bf5e0SSteven Rostedt (VMware) 	if (function_graph_enter(parent, self_addr, frame_pointer, NULL))
1299960e9e8SDavid S. Miller 		return parent + 8UL;
1309960e9e8SDavid S. Miller 
1319960e9e8SDavid S. Miller 	return return_hooker;
1329960e9e8SDavid S. Miller }
1339960e9e8SDavid S. Miller #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
134