^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <trace/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static const u32 ftrace_nop = 0x01000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static u32 ftrace_call_replace(unsigned long ip, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) u32 call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) s32 off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) off = ((s32)addr - (s32)ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) call = 0x40000000 | ((u32)off >> 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int ftrace_modify_code(unsigned long ip, u32 old, u32 new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 replaced;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __asm__ __volatile__(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) "1: cas [%[ip]], %[old], %[new]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) " flush %[ip]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) " mov 0, %[faulted]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) "2:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) " .section .fixup,#alloc,#execinstr\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) " .align 4\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) "3: sethi %%hi(2b), %[faulted]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) " jmpl %[faulted] + %%lo(2b), %%g0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) " mov 1, %[faulted]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) " .previous\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) " .section __ex_table,\"a\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) " .align 4\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) " .word 1b, 3b\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) " .previous\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) : "=r" (replaced), [faulted] "=r" (faulted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) : [new] "0" (new), [old] "r" (old), [ip] "r" (ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) : "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (replaced != old && replaced != new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) faulted = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long ip = rec->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u32 old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) old = ftrace_call_replace(ip, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) new = ftrace_nop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return ftrace_modify_code(ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned long ip = rec->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u32 old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) old = ftrace_nop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) new = ftrace_call_replace(ip, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return ftrace_modify_code(ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int ftrace_update_ftrace_func(ftrace_func_t func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned long ip = (unsigned long)(&ftrace_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u32 old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) old = *(u32 *) &ftrace_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) new = ftrace_call_replace(ip, (unsigned long)func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return ftrace_modify_code(ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int __init ftrace_dyn_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #ifdef CONFIG_FUNCTION_GRAPH_TRACER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) extern void ftrace_graph_call(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int ftrace_enable_ftrace_graph_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned long ip = (unsigned long)(&ftrace_graph_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) u32 old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) old = *(u32 *) &ftrace_graph_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) new = ftrace_call_replace(ip, (unsigned long) &ftrace_graph_caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return ftrace_modify_code(ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int ftrace_disable_ftrace_graph_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned long ip = (unsigned long)(&ftrace_graph_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u32 old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) old = *(u32 *) &ftrace_graph_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) new = ftrace_call_replace(ip, (unsigned long) &ftrace_stub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ftrace_modify_code(ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #endif /* !CONFIG_DYNAMIC_FTRACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * Hook the return address and push it in the stack of return addrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * in current thread info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned long prepare_ftrace_return(unsigned long parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) unsigned long self_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned long frame_pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) unsigned long return_hooker = (unsigned long) &return_to_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (unlikely(atomic_read(¤t->tracing_graph_pause)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return parent + 8UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (function_graph_enter(parent, self_addr, frame_pointer, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return parent + 8UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return return_hooker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #endif /* CONFIG_FUNCTION_GRAPH_TRACER */