linux/arch/mips
Wu Zhangjin 538f19526e MIPS: Tracing: Add dynamic function tracer support
With dynamic function tracer, by default, _mcount is defined as an
"empty" function, it returns directly without any more action . When
enabling it in user-space, it will jump to a real tracing
function(ftrace_caller), and do the real job for us.

Differ from the static function tracer, dynamic function tracer provides
two functions ftrace_make_call()/ftrace_make_nop() to enable/disable the
tracing of some indicated kernel functions(set_ftrace_filter).

In the -v4 version, the implementation of this support is basically the same as
X86 version does: _mcount is implemented as an empty function and ftrace_caller
is implemented as a real tracing function respectively.

But in this version, to support module tracing with the help of
-mlong-calls in arch/mips/Makefile:

MODFLAGS += -mlong-calls.

The stuff becomes a little more complex. We need to cope with two
different type of calling to _mcount.

For the kernel part, the calling to _mcount(result of "objdump -hdr
vmlinux"). is like this:

	108:   03e0082d        move    at,ra
	10c:   0c000000        jal     0 <fpcsr_pending>
                        10c: R_MIPS_26  _mcount
                        10c: R_MIPS_NONE        *ABS*
                        10c: R_MIPS_NONE        *ABS*
	110:   00020021        nop

For the module with -mlong-calls, it looks like this:

	c:	3c030000 	lui	v1,0x0
			c: R_MIPS_HI16	_mcount
			c: R_MIPS_NONE	*ABS*
			c: R_MIPS_NONE	*ABS*
	10:	64630000 	daddiu	v1,v1,0
			10: R_MIPS_LO16	_mcount
			10: R_MIPS_NONE	*ABS*
			10: R_MIPS_NONE	*ABS*
	14:	03e0082d 	move	at,ra
	18:	0060f809 	jalr	v1

In the kernel version, there is only one "_mcount" string for every
kernel function, so, we just need to match this one in mcount_regex of
scripts/recordmcount.pl, but in the module version, we need to choose
one of the two to match. Herein, I choose the first one with
"R_MIPS_HI16 _mcount".

and In the kernel verion, without module tracing support, we just need
to replace "jal _mcount" by "jal ftrace_caller" to do real tracing, and
filter the tracing of some kernel functions via replacing it by a nop
instruction.

but as we have described before, the instruction "jal ftrace_caller" only left
32bit length for the address of ftrace_caller, it will fail when calling from
the module space. so, herein, we must replace something else.

the basic idea is loading the address of ftrace_caller to v1 via changing these
two instructions:

	lui	v1,0x0
	addiu	v1,v1,0

If we want to enable the tracing, we need to replace the above instructions to:

	lui	v1, HI_16BIT_ftrace_caller
	addiu	v1, v1, LOW_16BIT_ftrace_caller

If we want to stop the tracing of the indicated kernel functions, we
just need to replace the "jalr v1" to a nop instruction. but we need to
replace two instructions and encode the above two instructions
oursevles.

Is there a simpler solution? Yes! Here it is, in this version, we put _mcount
and ftrace_caller together, which means the address of _mcount and
ftrace_caller is the same:

_mcount:
ftrace_caller:
	j	ftrace_stub
	 nop

	...(do real tracing here)...

ftrace_stub:
	jr	ra
	 move	ra, at

By default, the kernel functions call _mcount, and then jump to ftrace_stub and
return. and when we want to do real tracing, we just need to remove that "j
ftrace_stub", and it will run through the two "nop" instructions and then do
the real tracing job.

what about filtering job? we just need to do this:

	 lui v1, hi_16bit_of_mcount        <--> b 1f (0x10000004)
	 addiu v1, v1, low_16bit_of_mcount
	 move at, ra
	 jalr v1
	 nop
	 				     1f: (rec->ip + 12)

In linux-mips64, there will be some local symbols, whose name are
prefixed by $L, which need to be filtered. thanks goes to Steven for
writing the mips64-specific function_regex.

In a conclusion, with RISC, things becomes easier with such a "stupid"
trick, RISC is something like K.I.S.S, and also, there are lots of
"simple" tricks in the whole ftrace support, thanks goes to Steven and
the other folks for providing such a wonderful tracing framework!

Signed-off-by: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Nicholas Mc Guire <der.herr@hofr.at>
Cc: zhangfx@lemote.com
Cc: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-mips@linux-mips.org
Patchwork: http://patchwork.linux-mips.org/patch/675/
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
2009-12-17 01:57:23 +00:00
..
alchemy MIPS: Alchemy: Fix hang with high-frequency edge interrupts 2009-11-02 12:00:08 +01:00
ar7 MIPS: AR7: register watchdog device only if enabled in hw configuration 2009-11-02 12:00:03 +01:00
basler/excite MIPS: Excite: Get rid of BKL. 2009-09-30 21:47:01 +02:00
bcm47xx MIPS: Replace all usages of CL_SIZE by COMMAND_LINE_SIZE 2009-12-17 01:56:56 +00:00
bcm63xx MIPS: bcm63xx: Set the correct BCM3302 CPU name 2009-11-02 12:00:07 +01:00
boot MIPS: Remove addinitrd and CONFIG_PROBE_INITRD_HEADER 2009-12-17 01:57:07 +00:00
cavium-octeon MIPS: Octeon: Add platform devices MGMT Ethernet ports. 2009-12-17 01:56:59 +00:00
cobalt Update Yoichi Yuasa's e-mail address 2009-07-03 15:45:29 +01:00
configs MIPS: PowerTV: Base files for Cisco PowerTV platform 2009-12-17 01:57:17 +00:00
dec Merge branch 'timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip 2009-09-18 09:15:24 -07:00
emma MIPS: Remove useless zero initializations. 2009-09-17 20:07:51 +02:00
fw MIPS: IP22: Remove an unused function 2009-12-17 01:56:56 +00:00
gt64120/wrppmc Update Yoichi Yuasa's e-mail address 2009-07-03 15:45:29 +01:00
include/asm MIPS: Tracing: Add dynamic function tracer support 2009-12-17 01:57:23 +00:00
jazz MIPS: Add IRQF_TIMER flag for timer interrupts 2009-11-02 12:00:02 +01:00
kernel MIPS: Tracing: Add dynamic function tracer support 2009-12-17 01:57:23 +00:00
lasat MIPS: Replace all usages of CL_SIZE by COMMAND_LINE_SIZE 2009-12-17 01:56:56 +00:00
lib MIPS: Fix __ndelay build error and add 'ull' suffix for 32-bit kernel 2009-06-17 11:06:24 +01:00
loongson MIPS: Loongson 2F: Add CPU frequency scaling support 2009-12-17 01:57:20 +00:00
math-emu MIPS: Collect FPU emulator statistics per-CPU. 2009-12-17 01:57:08 +00:00
mipssim MIPS: MIPSsim: Remove unused code 2009-12-17 01:57:01 +00:00
mm MIPS: Sibyte: Use hweight8 instead of counting bits 2009-12-17 01:57:16 +00:00
mti-malta MIPS: Replace all usages of CL_SIZE by COMMAND_LINE_SIZE 2009-12-17 01:56:56 +00:00
nxp MIPS: Fixup last users of irq_chip->typename 2009-12-17 01:57:21 +00:00
oprofile MIPS: oprofile: Only do performance counter handling for counter interrupts 2009-12-17 01:57:10 +00:00
pci MIPS: Lemote 2F: Add PCI support 2009-12-17 01:57:12 +00:00
pmc-sierra cpumask: arch_send_call_function_ipi_mask: mips 2009-09-24 09:34:45 +09:30
power MIPS: Use PAGE_SIZE in assembly instead of _PAGE_SIZE. 2009-09-17 20:07:48 +02:00
powertv MIPS: PowerTV: Base files for Cisco PowerTV platform 2009-12-17 01:57:17 +00:00
rb532 MIPS: Replace all usages of CL_SIZE by COMMAND_LINE_SIZE 2009-12-17 01:56:56 +00:00
sgi-ip22 MIPS: IP22, IP28: Build with -Werror 2009-09-17 20:07:44 +02:00
sgi-ip27 MIPS: Add IRQF_TIMER flag for timer interrupts 2009-11-02 12:00:02 +01:00
sgi-ip32 MIPS: IP32: Remove unnecessary if not even harmful volatile keywords. 2009-05-22 13:52:06 +01:00
sibyte MIPS: Replace all usages of CL_SIZE by COMMAND_LINE_SIZE 2009-12-17 01:56:56 +00:00
sni MIPS: Fixup last users of irq_chip->typename 2009-12-17 01:57:21 +00:00
txx9 MIPS: Replace all usages of CL_SIZE by COMMAND_LINE_SIZE 2009-12-17 01:56:56 +00:00
vr41xx genirq: Convert irq_desc.lock to raw_spinlock 2009-12-14 23:55:33 +01:00
Kconfig MIPS: Tracing: Add dynamic function tracer support 2009-12-17 01:57:23 +00:00
Kconfig.debug MIPS: Kconfig: Fix the arch-specific header path 2008-12-12 18:12:23 +00:00
Makefile MIPS: Tracing: Add static function tracer support for MIPS 2009-12-17 01:57:21 +00:00