kernel-ark/include/asm-x86_64
Rusty Lynch 73649dab0f [PATCH] x86_64 specific function return probes
The following patch adds the x86_64 architecture specific implementation
for function return probes.

Function return probes is a mechanism built on top of kprobes that allows
a caller to register a handler to be called when a given function exits.
For example, to instrument the return path of sys_mkdir:

static int sys_mkdir_exit(struct kretprobe_instance *i, struct pt_regs *regs)
{
	printk("sys_mkdir exited\n");
	return 0;
}
static struct kretprobe return_probe = {
	.handler = sys_mkdir_exit,
};

<inside setup function>

return_probe.kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("sys_mkdir");
if (register_kretprobe(&return_probe)) {
	printk(KERN_DEBUG "Unable to register return probe!\n");
	/* do error path */
}

<inside cleanup function>
unregister_kretprobe(&return_probe);

The way this works is that:

* At system initialization time, kernel/kprobes.c installs a kprobe
  on a function called kretprobe_trampoline() that is implemented in
  the arch/x86_64/kernel/kprobes.c  (More on this later)

* When a return probe is registered using register_kretprobe(),
  kernel/kprobes.c will install a kprobe on the first instruction of the
  targeted function with the pre handler set to arch_prepare_kretprobe()
  which is implemented in arch/x86_64/kernel/kprobes.c.

* arch_prepare_kretprobe() will prepare a kretprobe instance that stores:
  - nodes for hanging this instance in an empty or free list
  - a pointer to the return probe
  - the original return address
  - a pointer to the stack address

  With all this stowed away, arch_prepare_kretprobe() then sets the return
  address for the targeted function to a special trampoline function called
  kretprobe_trampoline() implemented in arch/x86_64/kernel/kprobes.c

* The kprobe completes as normal, with control passing back to the target
  function that executes as normal, and eventually returns to our trampoline
  function.

* Since a kprobe was installed on kretprobe_trampoline() during system
  initialization, control passes back to kprobes via the architecture
  specific function trampoline_probe_handler() which will lookup the
  instance in an hlist maintained by kernel/kprobes.c, and then call
  the handler function.

* When trampoline_probe_handler() is done, the kprobes infrastructure
  single steps the original instruction (in this case just a top), and
  then calls trampoline_post_handler().  trampoline_post_handler() then
  looks up the instance again, puts the instance back on the free list,
  and then makes a long jump back to the original return instruction.

So to recap, to instrument the exit path of a function this implementation
will cause four interruptions:

  - A breakpoint at the very beginning of the function allowing us to
    switch out the return address
  - A single step interruption to execute the original instruction that
    we replaced with the break instruction (normal kprobe flow)
  - A breakpoint in the trampoline function where our instrumented function
    returned to
  - A single step interruption to execute the original instruction that
    we replaced with the break instruction (normal kprobe flow)

Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-06-23 09:45:21 -07:00
..
8253pit.h
a.out.h [PATCH] x86_64: TASK_SIZE fixes for compatibility mode processes 2005-06-21 18:46:12 -07:00
acpi.h
agp.h [PATCH] AGP fix for Xen VMM 2005-06-07 12:35:43 -07:00
apic.h
apicdef.h [PATCH] x86_64: Increase number of IO-APICs 2005-05-17 07:59:13 -07:00
atomic.h
bitops.h [PATCH] add x86-64 specific support for sparsemem 2005-06-23 09:45:07 -07:00
boot.h
bootsetup.h
bug.h [PATCH] x86_64: CONFIG_BUG=n fixes 2005-05-25 15:31:28 -07:00
bugs.h
byteorder.h
cache.h
cacheflush.h
calling.h
checksum.h
compat.h
cpu.h
cpufeature.h
cputime.h
current.h
debugreg.h
delay.h
desc.h
div64.h
dma-mapping.h
dma.h
dwarf2.h
e820.h
elf.h
errno.h
fcntl.h
fixmap.h
floppy.h [PATCH] make some things static 2005-05-05 16:36:47 -07:00
fpu32.h
genapic.h
hardirq.h
hdreg.h
hpet.h
hw_irq.h
i387.h
ia32_unistd.h
ia32.h
ide.h
io_apic.h [PATCH] x86_64: Remove unique APIC/IO-APIC ID check 2005-05-17 07:59:14 -07:00
io.h [PATCH] reorganize x86-64 NUMA and DISCONTIGMEM config options 2005-06-23 09:45:06 -07:00
ioctl.h
ioctls.h
ipcbuf.h
ipi.h
irq.h
kdebug.h
kmap_types.h
kprobes.h [PATCH] x86_64 specific function return probes 2005-06-23 09:45:21 -07:00
ldt.h
linkage.h
local.h
mach_apic.h
mc146818rtc.h
mce.h
mman.h
mmsegment.h
mmu_context.h
mmu.h
mmx.h
mmzone.h [PATCH] reorganize x86-64 NUMA and DISCONTIGMEM config options 2005-06-23 09:45:06 -07:00
module.h
mpspec.h
msgbuf.h
msi.h
msr.h [PATCH] eliminate duplicate rdpmc definition 2005-06-23 09:45:13 -07:00
mtrr.h
namei.h
nmi.h [PATCH] x86_64: Collected NMI watchdog fixes. 2005-05-17 07:59:16 -07:00
node.h
numa.h
numnodes.h
page.h [PATCH] reorganize x86-64 NUMA and DISCONTIGMEM config options 2005-06-23 09:45:06 -07:00
param.h [PATCH] i386: Selectable Frequency of the Timer Interrupt 2005-06-23 09:45:10 -07:00
parport.h
pci-direct.h
pci.h
pda.h
percpu.h
pgalloc.h
pgtable.h [PATCH] Hugepage consolidation 2005-06-21 18:46:15 -07:00
poll.h
posix_types.h
prctl.h
processor.h [PATCH] xen: x86_64: Add macro for debugreg 2005-06-23 09:45:14 -07:00
proto.h [PATCH] x86_64: Add pmtimer support 2005-05-17 07:59:15 -07:00
ptrace.h [PATCH] xen: x86: Rename usermode macro 2005-06-23 09:45:14 -07:00
resource.h
rtc.h
rwlock.h
rwsem.h
scatterlist.h
seccomp.h
sections.h
segment.h
semaphore.h
sembuf.h
serial.h
setup.h
shmbuf.h
shmparam.h
sigcontext32.h
sigcontext.h
siginfo.h
signal.h [PATCH] asm/signal.h unification 2005-05-04 07:33:15 -07:00
smp.h [PATCH] smp_processor_id() cleanup 2005-06-21 18:46:13 -07:00
socket.h
sockios.h
sparsemem.h [PATCH] add x86-64 specific support for sparsemem 2005-06-23 09:45:07 -07:00
spinlock.h
stat.h
statfs.h
string.h
suspend.h
swiotlb.h
system.h
termbits.h
termios.h
thread_info.h [PATCH] streamline preempt_count type across archs 2005-06-23 09:45:19 -07:00
timex.h [PATCH] Platform SMIs and their interferance with tsc based delay calibration 2005-06-23 09:45:08 -07:00
tlb.h
tlbflush.h
topology.h [PATCH] x86/x86_64: pcibus_to_node 2005-06-23 09:45:08 -07:00
types.h
uaccess.h
ucontext.h
unaligned.h
unistd.h
user32.h
user.h
vga.h
vsyscall32.h
vsyscall.h [PATCH] x86_64: Add pmtimer support 2005-05-17 07:59:15 -07:00
xor.h