Wu Zhangjin | 538f195 | 2009-11-20 20:34:32 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Code for replacing ftrace calls with jumps. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | * Copyright (C) 2009 DSLab, Lanzhou University, China |
| 6 | * Author: Wu Zhangjin <wuzj@lemote.com> |
| 7 | * |
| 8 | * Thanks goes to Steven Rostedt for writing the original x86 version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/uaccess.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/ftrace.h> |
| 14 | |
| 15 | #include <asm/cacheflush.h> |
| 16 | |
| 17 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 18 | |
| 19 | #define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */ |
| 20 | #define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */ |
| 21 | #define jump_insn_encode(op_code, addr) \ |
| 22 | ((unsigned int)((op_code) | (((addr) >> 2) & ADDR_MASK))) |
| 23 | |
| 24 | static unsigned int ftrace_nop = 0x00000000; |
| 25 | |
| 26 | static int ftrace_modify_code(unsigned long ip, unsigned int new_code) |
| 27 | { |
| 28 | *(unsigned int *)ip = new_code; |
| 29 | |
| 30 | flush_icache_range(ip, ip + 8); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int lui_v1; |
| 36 | static int jal_mcount; |
| 37 | |
| 38 | int ftrace_make_nop(struct module *mod, |
| 39 | struct dyn_ftrace *rec, unsigned long addr) |
| 40 | { |
| 41 | unsigned int new; |
| 42 | unsigned long ip = rec->ip; |
| 43 | |
| 44 | /* We have compiled module with -mlong-calls, but compiled the kernel |
| 45 | * without it, we need to cope with them respectively. */ |
| 46 | if (ip & 0x40000000) { |
| 47 | /* record it for ftrace_make_call */ |
| 48 | if (lui_v1 == 0) |
| 49 | lui_v1 = *(unsigned int *)ip; |
| 50 | |
| 51 | /* lui v1, hi_16bit_of_mcount --> b 1f (0x10000004) |
| 52 | * addiu v1, v1, low_16bit_of_mcount |
| 53 | * move at, ra |
| 54 | * jalr v1 |
| 55 | * nop |
| 56 | * 1f: (ip + 12) |
| 57 | */ |
| 58 | new = 0x10000004; |
| 59 | } else { |
| 60 | /* record/calculate it for ftrace_make_call */ |
| 61 | if (jal_mcount == 0) { |
| 62 | /* We can record it directly like this: |
| 63 | * jal_mcount = *(unsigned int *)ip; |
| 64 | * Herein, jump over the first two nop instructions */ |
| 65 | jal_mcount = jump_insn_encode(JAL, (MCOUNT_ADDR + 8)); |
| 66 | } |
| 67 | |
| 68 | /* move at, ra |
| 69 | * jalr v1 --> nop |
| 70 | */ |
| 71 | new = ftrace_nop; |
| 72 | } |
| 73 | return ftrace_modify_code(ip, new); |
| 74 | } |
| 75 | |
| 76 | static int modified; /* initialized as 0 by default */ |
| 77 | |
| 78 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 79 | { |
| 80 | unsigned int new; |
| 81 | unsigned long ip = rec->ip; |
| 82 | |
| 83 | /* We just need to remove the "b ftrace_stub" at the fist time! */ |
| 84 | if (modified == 0) { |
| 85 | modified = 1; |
| 86 | ftrace_modify_code(addr, ftrace_nop); |
| 87 | } |
| 88 | /* ip, module: 0xc0000000, kernel: 0x80000000 */ |
| 89 | new = (ip & 0x40000000) ? lui_v1 : jal_mcount; |
| 90 | |
| 91 | return ftrace_modify_code(ip, new); |
| 92 | } |
| 93 | |
| 94 | #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call)) |
| 95 | |
| 96 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 97 | { |
| 98 | unsigned int new; |
| 99 | |
| 100 | new = jump_insn_encode(JAL, (unsigned long)func); |
| 101 | |
| 102 | return ftrace_modify_code(FTRACE_CALL_IP, new); |
| 103 | } |
| 104 | |
| 105 | int __init ftrace_dyn_arch_init(void *data) |
| 106 | { |
| 107 | /* The return code is retured via data */ |
| 108 | *(unsigned long *)data = 0; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | #endif /* CONFIG_DYNAMIC_FTRACE */ |