# fadvise64 _______________________________________________________
#
# The following kernel commit:
#
#   commit 5383d2c8b3ee61a762043818d7c07bbc0049b031
#   Author: Heiko Carstens <heiko.carstens@de.ibm.com>
#   Date:   Wed Feb 26 14:40:43 2014 +0100
#
#       s390/compat: convert to COMPAT_SYSCALL_DEFINEx part 7
#
# changed sys32_fadvise64 from:
#
# asmlinkage long
# sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
#
# to:
#
# COMPAT_SYSCALL_DEFINE5(s390_fadvise64, int, fd, u32, high, u32, low,
#			 compat_size_t, len, int, advise)

@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)
%( kernel_v < "3.15" %?
 	# We have to use raw access here since the long long value is
	# stored in 1 register, not 2 - which longlong_arg() expects.
	offset = u_register("r3")
	len = int_arg(3)
	advice = int_arg(4)
%:
        offset = ((uint_arg(2) << 32) | uint_arg(3))
	len = int_arg(4)
	advice = int_arg(5)
%)
	advice_str = _fadvice_advice_str(advice)
%)

@define _SYSCALL_COMPAT_FADVISE64_64_REGARGS
%(
	__args = &@uint_cast(pointer_arg(1))
	fd = user_int(&(__args)[0])
	offset = user_int64(&(__args)[2])
	len = user_uint64(&(__args)[4])
	advice = user_int(&(__args)[6])
	advice_str = _fadvice_advice_str(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_s390_fadvise64").call ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	fd = __int32($fd)
	if (@defined($offset)) {
		offset = $offset
	}
	else {
	        offset = (($high << 32) | __uint32($low))
	}
	len = __int32($len)
	advice = __int32($advise)
	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_s390_fadvise64").return ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_fadvise64 _____________________________________________________

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

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

/* 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_fadvise64"))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
}

probe nd_syscall.compat_fadvise64.return = nd1_syscall.compat_fadvise64.return!, tp_syscall.compat_fadvise64.return
  { }
  
probe nd1_syscall.compat_fadvise64.return =
	kprobe.function("sys32_fadvise64").return ?,
	kprobe.function("compat_sys_s390_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_fadvise64"))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR($ret)
}

# fadvise64_64 __________________________________________________
#
# COMPAT_SYSCALL_DEFINE1(s390_fadvise64_64,
#			 struct fadvise64_64_args __user *, args)
# struct fadvise64_64_args {
#	int fd;
#	long long offset;
#	long long len;
#	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_s390_fadvise64_64").call ?
{
	@_SYSCALL_COMPAT_FADVISE64_NAME
	fd = user_int(&@cast(@__pointer($args), "fadvise64_64_args")->fd)
	offset = user_int64(&@cast(@__pointer($args), "fadvise64_64_args")->offset)
	len = user_int64(&@cast(@__pointer($args), "fadvise64_64_args")->len)
	advice = user_int(&@cast(@__pointer($args), "fadvise64_64_args")->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_s390_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!, tp_syscall.compat_fadvise64_64
  { }

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

/* 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_fadvise64_64"))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@_SYSCALL_COMPAT_FADVISE64_64_REGARGS
	@_SYSCALL_COMPAT_FADVISE64_ARGSTR
}

probe nd_syscall.compat_fadvise64_64.return = nd1_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_s390_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_fadvise64_64"))
	@_SYSCALL_COMPAT_FADVISE64_NAME
	@SYSC_RETVALSTR($ret)
}
