Michal Simek | 7d241ff | 2009-12-10 14:15:44 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Ftrace support for Microblaze. |
| 3 | * |
| 4 | * Copyright (C) 2009 Michal Simek <monstr@monstr.eu> |
| 5 | * Copyright (C) 2009 PetaLogix |
| 6 | * |
| 7 | * Based on MIPS and PowerPC ftrace code |
| 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <asm/cacheflush.h> |
| 15 | #include <linux/ftrace.h> |
| 16 | |
| 17 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 18 | /* save value to addr - it is save to do it in asm */ |
| 19 | static int ftrace_modify_code(unsigned long addr, unsigned int value) |
| 20 | { |
| 21 | int faulted = 0; |
| 22 | |
| 23 | __asm__ __volatile__(" 1: swi %2, %1, 0; \ |
| 24 | addik %0, r0, 0; \ |
| 25 | 2: \ |
| 26 | .section .fixup, \"ax\"; \ |
| 27 | 3: brid 2b; \ |
| 28 | addik %0, r0, 1; \ |
| 29 | .previous; \ |
| 30 | .section __ex_table,\"a\"; \ |
| 31 | .word 1b,3b; \ |
| 32 | .previous;" \ |
| 33 | : "=r" (faulted) |
| 34 | : "r" (addr), "r" (value) |
| 35 | ); |
| 36 | |
| 37 | if (unlikely(faulted)) |
| 38 | return -EFAULT; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | #define MICROBLAZE_NOP 0x80000000 |
| 44 | #define MICROBLAZE_BRI 0xb800000C |
| 45 | |
| 46 | static unsigned int recorded; /* if save was or not */ |
| 47 | static unsigned int imm; /* saving whole imm instruction */ |
| 48 | |
| 49 | /* There are two approaches howto solve ftrace_make nop function - look below */ |
| 50 | #undef USE_FTRACE_NOP |
| 51 | |
| 52 | #ifdef USE_FTRACE_NOP |
| 53 | static unsigned int bralid; /* saving whole bralid instruction */ |
| 54 | #endif |
| 55 | |
| 56 | int ftrace_make_nop(struct module *mod, |
| 57 | struct dyn_ftrace *rec, unsigned long addr) |
| 58 | { |
| 59 | /* we have this part of code which we are working with |
| 60 | * b000c000 imm -16384 |
| 61 | * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount> |
| 62 | * 80000000 or r0, r0, r0 |
| 63 | * |
| 64 | * The first solution (!USE_FTRACE_NOP-could be called branch solution) |
| 65 | * b000c000 bri 12 (0xC - jump to any other instruction) |
| 66 | * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount> |
| 67 | * 80000000 or r0, r0, r0 |
| 68 | * any other instruction |
| 69 | * |
| 70 | * The second solution (USE_FTRACE_NOP) - no jump just nops |
| 71 | * 80000000 or r0, r0, r0 |
| 72 | * 80000000 or r0, r0, r0 |
| 73 | * 80000000 or r0, r0, r0 |
| 74 | */ |
| 75 | int ret = 0; |
| 76 | |
| 77 | if (recorded == 0) { |
| 78 | recorded = 1; |
| 79 | imm = *(unsigned int *)rec->ip; |
| 80 | pr_debug("%s: imm:0x%x\n", __func__, imm); |
| 81 | #ifdef USE_FTRACE_NOP |
| 82 | bralid = *(unsigned int *)(rec->ip + 4); |
| 83 | pr_debug("%s: bralid 0x%x\n", __func__, bralid); |
| 84 | #endif /* USE_FTRACE_NOP */ |
| 85 | } |
| 86 | |
| 87 | #ifdef USE_FTRACE_NOP |
| 88 | ret = ftrace_modify_code(rec->ip, MICROBLAZE_NOP); |
| 89 | ret += ftrace_modify_code(rec->ip + 4, MICROBLAZE_NOP); |
| 90 | #else /* USE_FTRACE_NOP */ |
| 91 | ret = ftrace_modify_code(rec->ip, MICROBLAZE_BRI); |
| 92 | #endif /* USE_FTRACE_NOP */ |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static int ret_addr; /* initialized as 0 by default */ |
| 97 | |
| 98 | /* I believe that first is called ftrace_make_nop before this function */ |
| 99 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 100 | { |
| 101 | int ret; |
| 102 | ret_addr = addr; /* saving where the barrier jump is */ |
| 103 | pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n", |
| 104 | __func__, (unsigned int)addr, (unsigned int)rec->ip, imm); |
| 105 | ret = ftrace_modify_code(rec->ip, imm); |
| 106 | #ifdef USE_FTRACE_NOP |
| 107 | pr_debug("%s: bralid:0x%x\n", __func__, bralid); |
| 108 | ret += ftrace_modify_code(rec->ip + 4, bralid); |
| 109 | #endif /* USE_FTRACE_NOP */ |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | int __init ftrace_dyn_arch_init(void *data) |
| 114 | { |
| 115 | /* The return code is retured via data */ |
| 116 | *(unsigned long *)data = 0; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 122 | { |
| 123 | unsigned long ip = (unsigned long)(&ftrace_call); |
| 124 | unsigned int upper = (unsigned int)func; |
| 125 | unsigned int lower = (unsigned int)func; |
| 126 | int ret = 0; |
| 127 | |
| 128 | /* create proper saving to ftrace_call poll */ |
| 129 | upper = 0xb0000000 + (upper >> 16); /* imm func_upper */ |
| 130 | lower = 0x32800000 + (lower & 0xFFFF); /* addik r20, r0, func_lower */ |
| 131 | |
| 132 | pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n", |
| 133 | __func__, (unsigned int)func, (unsigned int)ip, upper, lower); |
| 134 | |
| 135 | /* save upper and lower code */ |
| 136 | ret = ftrace_modify_code(ip, upper); |
| 137 | ret += ftrace_modify_code(ip + 4, lower); |
| 138 | |
| 139 | /* We just need to remove the rtsd r15, 8 by NOP */ |
| 140 | BUG_ON(!ret_addr); |
| 141 | if (ret_addr) |
| 142 | ret += ftrace_modify_code(ret_addr, MICROBLAZE_NOP); |
| 143 | else |
| 144 | ret = 1; /* fault */ |
| 145 | |
| 146 | /* All changes are done - lets do caches consistent */ |
| 147 | flush_icache(); |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | #endif /* CONFIG_DYNAMIC_FTRACE */ |