# readahead __________________________________________________
#
# asmlinkage ssize_t
# ssize_t sys32_readahead(int fd, unsigned off_lo, unsigned off_hi, size_t count)
# COMPAT_SYSCALL_DEFINE4(x86_readahead, int, fd, unsigned int, off_lo,
#                        unsigned int, off_hi, size_t, count)
#

@define _SYSCALL_COMPAT_READAHEAD_NAME
%(
	name = "readahead"
%)

@define _SYSCALL_COMPAT_READAHEAD_ARGSTR
%(
	argstr = sprintf("%d, %d, %u", fd, offset, count)
%)

@define _SYSCALL_COMPAT_READAHEAD_REGARGS
%(
	fd = int_arg(1)
	offset = ((uint_arg(3) << 32) | uint_arg(2))
	count = ulong_arg(4)
%)

@define _SYSCALL_COMPAT_READAHEAD_REGARGS_STORE
%(
        if (@probewrite(fd))
          set_int_arg(1, fd)

	if (@probewrite(offset)) {
	  set_uint_arg(2, offset & 0xffffffff)
	  set_uint_arg(3, offset >> 32)
	}

	if (@probewrite(count))
	  set_ulong_arg(4, count)
%)

probe syscall.compat_readahead = dw_syscall.compat_readahead !,
                                 nd_syscall.compat_readahead ? {}
probe syscall.compat_readahead.return = dw_syscall.compat_readahead.return !,
                                        nd_syscall.compat_readahead.return ? {}

# dw_compat_readahead _____________________________________________________

probe dw_syscall.compat_readahead = kernel.function("sys32_readahead") ?
{
	@_SYSCALL_COMPAT_READAHEAD_NAME
	fd = __int32($fd)
	offset = (($off_hi << 32) | $off_lo)
	count = __uint32($count)
	@_SYSCALL_COMPAT_READAHEAD_ARGSTR
}
probe dw_syscall.compat_readahead.return =
	kernel.function("sys32_readahead").return ?
{
	@_SYSCALL_COMPAT_READAHEAD_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_readahead _____________________________________________________

probe nd_syscall.compat_readahead = nd1_syscall.compat_readahead!, nd2_syscall.compat_readahead!, tp_syscall.compat_readahead
  { }

probe nd1_syscall.compat_readahead = kprobe.function("sys32_readahead") ?,
	kprobe.function("compat_sys_x86_readahead") ?
{
	@_SYSCALL_COMPAT_READAHEAD_NAME
	asmlinkage()
	@_SYSCALL_COMPAT_READAHEAD_REGARGS
	@_SYSCALL_COMPAT_READAHEAD_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_readahead =
	kprobe.function(@arch_syscall_prefix "compat_sys_x86_readahead") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_COMPAT_READAHEAD_NAME
	@_SYSCALL_COMPAT_READAHEAD_REGARGS
	@_SYSCALL_COMPAT_READAHEAD_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_READAHEAD_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_readahead = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_ia32_readahead"))
	@_SYSCALL_COMPAT_READAHEAD_NAME
	@_SYSCALL_COMPAT_READAHEAD_REGARGS
	@_SYSCALL_COMPAT_READAHEAD_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_READAHEAD_REGARGS_STORE %)
}

probe nd_syscall.compat_readahead.return = nd1_syscall.compat_readahead.return!, nd2_syscall.compat_readahead.return!, tp_syscall.compat_readahead.return
  { }
  
probe nd1_syscall.compat_readahead.return =
	kprobe.function("sys32_readahead").return ?,
	kprobe.function("compat_sys_x86_readahead").return ?
{
	@_SYSCALL_COMPAT_READAHEAD_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.compat_readahead.return =
	kprobe.function(@arch_syscall_prefix "compat_sys_x86_readahead").return ?
{
	@_SYSCALL_COMPAT_READAHEAD_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_readahead.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_ia32_readahead"))
	@_SYSCALL_COMPAT_READAHEAD_NAME
	@SYSC_RETVALSTR($ret)
}
