# fadvise64 _______________________________________________________
#
# COMPAT_SYSCALL_DEFINE5(x86_fadvise64, int, fd, unsigned int, 
# 			 offset_lo, unsigned int, offset_hi,
# 			 size_t, len, int, advice)
# asmlinkage long sys32_fadvise64(int fd, unsigned offset_lo,
#				  unsigned offset_hi,
#				  size_t len, int advice)
#

@define _SYSCALL_COMPAT_FADVISE64_NAME
%(
	name = "fadvise64"
%)

@define _SYSCALL_COMPAT_FADVISE64_ARGSTR
%(
	argstr = sprintf("%d, %d, %d, %s", fd, offset, len, advice_str)
%)

@define _SYSCALL_COMPAT_FADVISE64_REGARGS
%(
	fd = int_arg(1)
	offset = ((uint_arg(3) << 32) | uint_arg(2))
	len = int_arg(4)
	advice = int_arg(5)
	advice_str = _fadvice_advice_str(advice)
%)
@define _SYSCALL_COMPAT_FADVISE64_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(len))
	  set_int_arg(4, len)

	if (@probewrite(advice))
	  set_int_arg(5, advice)
%)


@define _SYSCALL_COMPAT_FADVISE64_64_REGARGS
%(
	fd = int_arg(1)
	offset = ((uint_arg(3) << 32) | uint_arg(2))
	len = ((uint_arg(5) << 32) | uint_arg(4))
	advice = int_arg(6)
	advice_str = _fadvice_advice_str(advice)
%)

@define _SYSCALL_COMPAT_FADVISE64_64_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(len)) {
	  set_uint_arg(4, len & 0xffffffff)
	  set_uint_arg(5, len >> 32)
	}

	if (@probewrite(advice))
	  set_int_arg(6, advice)
%)


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

# dw_compat_fadvise64 _____________________________________________________

probe dw_syscall.compat_fadvise64 = kernel.function("sys32_fadvise64").call ?,
	kernel.function("compat_sys_x86_fadvise64").call ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	fd = __int32($fd)
	offset = (($offset_hi << 32) | $offset_lo)
	len = __int32($len)
	advice = __int32($advice)
	advice_str = _fadvice_advice_str(advice)
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
}
probe dw_syscall.compat_fadvise64.return =
	kernel.function("sys32_fadvise64").return ?,
	kernel.function("compat_sys_x86_fadvise64").return ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_fadvise64 _____________________________________________________

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

probe nd1_syscall.compat_fadvise64 = kprobe.function("sys32_fadvise64") ?,
	kprobe.function("compat_sys_x86_fadvise64") ?
{
	asmlinkage()
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_fadvise64 = 
	kprobe.function(@arch_syscall_prefix "compat_sys_x86_fadvise64") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_FADVISE64_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_fadvise64 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_ia32_fadvise64"))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_FADVISE64_REGARGS_STORE %)
}

probe nd_syscall.compat_fadvise64.return = nd1_syscall.compat_fadvise64.return!, nd2_syscall.compat_fadvise64.return!, tp_syscall.compat_fadvise64.return
  { }
  
probe nd1_syscall.compat_fadvise64.return =
	kprobe.function("sys32_fadvise64").return ?,
	kprobe.function("compat_sys_x86_fadvise64").return ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR(returnval())
}

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

# fadvise64_64 __________________________________________________
#
# COMPAT_SYSCALL_DEFINE6(x86_fadvise64_64, int, fd, __u32, offset_low,
#		       __u32, offset_high, __u32, len_low, __u32, 
#		       len_high, int, advice) 
# long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
#			__u32 len_low, __u32 len_high, int advice)

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

# dw_compat_fadvise64_64 _____________________________________________________

probe dw_syscall.compat_fadvise64_64 = kernel.function("sys32_fadvise64_64").call ?,
	kernel.function("compat_sys_x86_fadvise64_64").call ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	fd = __int32($fd)
	offset = (($offset_high << 32) | $offset_low)
	len = (($len_high << 32) | $len_low)
	advice = __int32($advice)
	advice_str = _fadvice_advice_str(advice)
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
}
probe dw_syscall.compat_fadvise64_64.return =
	kernel.function("sys32_fadvise64_64").return ?,
	kernel.function("compat_sys_x86_fadvise64_64").return ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_fadvise64_64 _____________________________________________________

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

probe nd1_syscall.compat_fadvise64_64 = kprobe.function("sys32_fadvise64_64") ?,
	kprobe.function("compat_sys_x86_fadvise64_64") ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_fadvise64_64 = 
	kprobe.function(@arch_syscall_prefix "compat_sys_x86_fadvise64_64") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_FADVISE64_64_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_fadvise64_64 = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_ia32_fadvise64_64"))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_COMPAT_FADVISE64_64_REGARGS_STORE %)
}

probe nd_syscall.compat_fadvise64_64.return = nd1_syscall.compat_fadvise64_64.return!, nd2_syscall.compat_fadvise64_64.return!, tp_syscall.compat_fadvise64_64.return
  { }
  
probe nd1_syscall.compat_fadvise64_64.return =
	kprobe.function("sys32_fadvise64_64").return ?,
	kprobe.function("compat_sys_x86_fadvise64_64").return ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR(returnval())
}

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