diff options
author | Abhishek Sagar <sagar.abhishek@gmail.com> | 2008-05-31 04:53:50 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2008-06-02 05:32:20 -0400 |
commit | 014c257cce65e9d1cd2d28ec1c89a37c536b151d (patch) | |
tree | c7a28fbf6f295538964539d5b8c552ea786f7072 /arch/arm/kernel/ftrace.c | |
parent | b1829d2705daa7cb72eb1e08bdc8b7e9fad34266 (diff) |
ftrace: core support for ARM
Core ftrace support for the ARM architecture, which includes support
for dynamic function tracing.
Signed-off-by: Abhishek Sagar <sagar.abhishek@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'arch/arm/kernel/ftrace.c')
-rw-r--r-- | arch/arm/kernel/ftrace.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c new file mode 100644 index 000000000000..f4cb4cc3fa0c --- /dev/null +++ b/arch/arm/kernel/ftrace.c | |||
@@ -0,0 +1,128 @@ | |||
1 | /* | ||
2 | * Dynamic function tracing support. | ||
3 | * | ||
4 | * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com> | ||
5 | * | ||
6 | * For licencing details, see COPYING. | ||
7 | * | ||
8 | * Defines low-level handling of mcount calls when the kernel | ||
9 | * is compiled with the -pg flag. When using dynamic ftrace, the | ||
10 | * mcount call-sites get patched lazily with NOP till they are | ||
11 | * enabled. All code mutation routines here take effect atomically. | ||
12 | */ | ||
13 | |||
14 | #include <linux/ftrace.h> | ||
15 | #include <asm/cacheflush.h> | ||
16 | |||
17 | #define INSN_SIZE 4 | ||
18 | #define PC_OFFSET 8 | ||
19 | #define BL_OPCODE 0xeb000000 | ||
20 | #define BL_OFFSET_MASK 0x00ffffff | ||
21 | |||
22 | static unsigned long bl_insn; | ||
23 | static const unsigned long NOP = 0xe1a00000; /* mov r0, r0 */ | ||
24 | |||
25 | /* return true if mcount call site is already patched/no-op'ed */ | ||
26 | int ftrace_ip_converted(unsigned long pc) | ||
27 | { | ||
28 | unsigned long save; | ||
29 | |||
30 | pc -= INSN_SIZE; | ||
31 | save = *(unsigned long *)pc; | ||
32 | return save == NOP; | ||
33 | } | ||
34 | |||
35 | unsigned char *ftrace_nop_replace(void) | ||
36 | { | ||
37 | return (char *)&NOP; | ||
38 | } | ||
39 | |||
40 | /* construct a branch (BL) instruction to addr */ | ||
41 | unsigned char *ftrace_call_replace(unsigned long pc, unsigned long addr) | ||
42 | { | ||
43 | long offset; | ||
44 | |||
45 | offset = (long)addr - (long)(pc - INSN_SIZE + PC_OFFSET); | ||
46 | if (unlikely(offset < -33554432 || offset > 33554428)) { | ||
47 | /* Can't generate branches that far (from ARM ARM). Ftrace | ||
48 | * doesn't generate branches outside of core kernel text. | ||
49 | */ | ||
50 | WARN_ON_ONCE(1); | ||
51 | return NULL; | ||
52 | } | ||
53 | offset = (offset >> 2) & BL_OFFSET_MASK; | ||
54 | bl_insn = BL_OPCODE | offset; | ||
55 | return (unsigned char *)&bl_insn; | ||
56 | } | ||
57 | |||
58 | int ftrace_modify_code(unsigned long pc, unsigned char *old_code, | ||
59 | unsigned char *new_code) | ||
60 | { | ||
61 | unsigned long err = 0, replaced = 0, old, new; | ||
62 | |||
63 | old = *(unsigned long *)old_code; | ||
64 | new = *(unsigned long *)new_code; | ||
65 | pc -= INSN_SIZE; | ||
66 | |||
67 | __asm__ __volatile__ ( | ||
68 | "1: ldr %1, [%2] \n" | ||
69 | " cmp %1, %4 \n" | ||
70 | "2: streq %3, [%2] \n" | ||
71 | " cmpne %1, %3 \n" | ||
72 | " movne %0, #2 \n" | ||
73 | "3:\n" | ||
74 | |||
75 | ".section .fixup, \"ax\"\n" | ||
76 | "4: mov %0, #1 \n" | ||
77 | " b 3b \n" | ||
78 | ".previous\n" | ||
79 | |||
80 | ".section __ex_table, \"a\"\n" | ||
81 | " .long 1b, 4b \n" | ||
82 | " .long 2b, 4b \n" | ||
83 | ".previous\n" | ||
84 | |||
85 | : "=r"(err), "=r"(replaced) | ||
86 | : "r"(pc), "r"(new), "r"(old), "0"(err), "1"(replaced) | ||
87 | : "memory"); | ||
88 | |||
89 | if (!err && (replaced == old)) | ||
90 | flush_icache_range(pc, pc + INSN_SIZE); | ||
91 | |||
92 | return err; | ||
93 | } | ||
94 | |||
95 | int ftrace_update_ftrace_func(ftrace_func_t func) | ||
96 | { | ||
97 | int ret; | ||
98 | unsigned long pc, old; | ||
99 | unsigned char *new; | ||
100 | |||
101 | pc = (unsigned long)&ftrace_call; | ||
102 | pc += INSN_SIZE; | ||
103 | memcpy(&old, &ftrace_call, INSN_SIZE); | ||
104 | new = ftrace_call_replace(pc, (unsigned long)func); | ||
105 | ret = ftrace_modify_code(pc, (unsigned char *)&old, new); | ||
106 | return ret; | ||
107 | } | ||
108 | |||
109 | int ftrace_mcount_set(unsigned long *data) | ||
110 | { | ||
111 | unsigned long pc, old; | ||
112 | unsigned long *addr = data; | ||
113 | unsigned char *new; | ||
114 | |||
115 | pc = (unsigned long)&mcount_call; | ||
116 | pc += INSN_SIZE; | ||
117 | memcpy(&old, &mcount_call, INSN_SIZE); | ||
118 | new = ftrace_call_replace(pc, *addr); | ||
119 | *addr = ftrace_modify_code(pc, (unsigned char *)&old, new); | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | /* run from kstop_machine */ | ||
124 | int __init ftrace_dyn_arch_init(void *data) | ||
125 | { | ||
126 | ftrace_mcount_set(data); | ||
127 | return 0; | ||
128 | } | ||