Skip to content

Commit 7cc45e6

Browse files
author
Steven Rostedt
committed
powerpc/ppc32: ftrace, dynamic ftrace to handle modules
Impact: add ability to trace modules on 32 bit PowerPC This patch performs the necessary trampoline calls to handle modules with dynamic ftrace on 32 bit PowerPC. Signed-off-by: Steven Rostedt <srostedt@redhat.com>
1 parent f48cb8b commit 7cc45e6

3 files changed

Lines changed: 109 additions & 7 deletions

File tree

‎arch/powerpc/include/asm/module.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ struct mod_arch_specific {
3939
unsigned long tramp;
4040
#endif
4141

42-
#else
42+
#else /* powerpc64 */
4343
/* Indices of PLT sections within module. */
4444
unsigned int core_plt_section;
4545
unsigned int init_plt_section;
46+
#ifdef CONFIG_DYNAMIC_FTRACE
47+
unsigned long tramp;
4648
#endif
49+
#endif /* powerpc64 */
4750

4851
/* List of BUG addresses, source line numbers and filenames */
4952
struct list_head bug_list;

‎arch/powerpc/kernel/ftrace.c‎

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,63 @@ static int
274274
__ftrace_make_nop(struct module *mod,
275275
struct dyn_ftrace *rec, unsigned long addr)
276276
{
277-
/* Ignore modules for PPC32 (for now) */
277+
unsigned char replaced[MCOUNT_INSN_SIZE];
278+
unsigned int *op = (unsigned *)&replaced;
279+
unsigned char jmp[8];
280+
unsigned int *ptr = (unsigned int *)&jmp;
281+
unsigned long ip = rec->ip;
282+
unsigned long tramp;
283+
int offset;
284+
285+
if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
286+
return -EFAULT;
287+
288+
/* Make sure that that this is still a 24bit jump */
289+
if (!is_bl_op(*op)) {
290+
printk(KERN_ERR "Not expected bl: opcode is %x\n", *op);
291+
return -EINVAL;
292+
}
293+
294+
/* lets find where the pointer goes */
295+
tramp = find_bl_target(ip, *op);
296+
297+
/*
298+
* On PPC32 the trampoline looks like:
299+
* lis r11,sym@ha
300+
* addi r11,r11,sym@l
301+
* mtctr r11
302+
* bctr
303+
*/
304+
305+
DEBUGP("ip:%lx jumps to %lx", ip, tramp);
306+
307+
/* Find where the trampoline jumps to */
308+
if (probe_kernel_read(jmp, (void *)tramp, 8)) {
309+
printk(KERN_ERR "Failed to read %lx\n", tramp);
310+
return -EFAULT;
311+
}
312+
313+
DEBUGP(" %08x %08x ", ptr[0], ptr[1]);
314+
315+
tramp = (ptr[1] & 0xffff) |
316+
((ptr[0] & 0xffff) << 16);
317+
if (tramp & 0x8000)
318+
tramp -= 0x10000;
319+
320+
DEBUGP(" %x ", tramp);
321+
322+
if (tramp != addr) {
323+
printk(KERN_ERR
324+
"Trampoline location %08lx does not match addr\n",
325+
tramp);
326+
return -EINVAL;
327+
}
328+
329+
op[0] = PPC_NOP_INSTR;
330+
331+
if (probe_kernel_write((void *)ip, replaced, MCOUNT_INSN_SIZE))
332+
return -EPERM;
333+
278334
return 0;
279335
}
280336
#endif /* PPC64 */
@@ -297,7 +353,6 @@ int ftrace_make_nop(struct module *mod,
297353
return ftrace_modify_code(ip, old, new);
298354
}
299355

300-
#ifdef CONFIG_PPC64
301356
/*
302357
* Out of range jumps are called from modules.
303358
* We should either already have a pointer to the module
@@ -320,7 +375,6 @@ int ftrace_make_nop(struct module *mod,
320375
/* nothing to do if mod == rec->arch.mod */
321376
} else
322377
mod = rec->arch.mod;
323-
#endif /* CONFIG_PPC64 */
324378

325379
return __ftrace_make_nop(mod, rec, addr);
326380

@@ -380,7 +434,44 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
380434
static int
381435
__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
382436
{
383-
/* PPC32 ignores modules for now */
437+
unsigned char replaced[MCOUNT_INSN_SIZE];
438+
unsigned int *op = (unsigned *)&replaced;
439+
unsigned long ip = rec->ip;
440+
unsigned long offset;
441+
442+
/* read where this goes */
443+
if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
444+
return -EFAULT;
445+
446+
/* It should be pointing to a nop */
447+
if (op[0] != PPC_NOP_INSTR) {
448+
printk(KERN_ERR "Expected NOP but have %x\n", op[0]);
449+
return -EINVAL;
450+
}
451+
452+
/* If we never set up a trampoline to ftrace_caller, then bail */
453+
if (!rec->arch.mod->arch.tramp) {
454+
printk(KERN_ERR "No ftrace trampoline\n");
455+
return -EINVAL;
456+
}
457+
458+
/* now calculate a jump to the ftrace caller trampoline */
459+
offset = rec->arch.mod->arch.tramp - ip;
460+
461+
if (test_offset(offset)) {
462+
printk(KERN_ERR "REL24 %li out of range!\n",
463+
(long int)offset);
464+
return -EINVAL;
465+
}
466+
467+
/* Set to "bl addr" */
468+
op[0] = branch_offset(offset);
469+
470+
DEBUGP("write to %lx\n", rec->ip);
471+
472+
if (probe_kernel_write((void *)ip, replaced, MCOUNT_INSN_SIZE))
473+
return -EPERM;
474+
384475
return 0;
385476
}
386477
#endif /* CONFIG_PPC64 */
@@ -402,7 +493,6 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
402493
return ftrace_modify_code(ip, old, new);
403494
}
404495

405-
#ifdef CONFIG_PPC64
406496
/*
407497
* Out of range jumps are called from modules.
408498
* Being that we are converting from nop, it had better
@@ -412,7 +502,6 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
412502
printk(KERN_ERR "No module loaded\n");
413503
return -EINVAL;
414504
}
415-
#endif
416505

417506
return __ftrace_make_call(rec, addr);
418507
}

‎arch/powerpc/kernel/module_32.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/fs.h>
2323
#include <linux/string.h>
2424
#include <linux/kernel.h>
25+
#include <linux/ftrace.h>
2526
#include <linux/cache.h>
2627
#include <linux/bug.h>
2728
#include <linux/sort.h>
@@ -53,6 +54,9 @@ static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
5354
r_addend = rela[i].r_addend;
5455
}
5556

57+
#ifdef CONFIG_DYNAMIC_FTRACE
58+
_count_relocs++; /* add one for ftrace_caller */
59+
#endif
5660
return _count_relocs;
5761
}
5862

@@ -306,5 +310,11 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
306310
return -ENOEXEC;
307311
}
308312
}
313+
#ifdef CONFIG_DYNAMIC_FTRACE
314+
module->arch.tramp =
315+
do_plt_call(module->module_core,
316+
(unsigned long)ftrace_caller,
317+
sechdrs, module);
318+
#endif
309319
return 0;
310320
}

0 commit comments

Comments
 (0)