# vmsplice ___________________________________________________
#
# long sys_vmsplice(int fd, const struct iovec __user *iov,
#	    unsigned long nr_segs, unsigned int flags)
# long compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
#	    unsigned int nr_segs, unsigned int flags)
#

@define _SYSCALL_VMSPLICE_NAME
%(
	name = "vmsplice"
%)

@define _SYSCALL_VMSPLICE_ARGSTR
%(
	argstr = sprintf("%d, %p, %u, %s", fd, iov, nr_segs, flags_str)
%)

@define _SYSCALL_VMSPLICE_REGARGS
%(
	fd = int_arg(1)
	iov = pointer_arg(2)
	nr_segs = ulong_arg(3)
	flags = uint_arg(4)
	flags_str = _stp_splice_flags_str(flags)
%)

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

	if (@probewrite(iov))
	  set_pointer_arg(2, iov)

	if (@probewrite(nr_segs))
	  set_ulong_arg(3, nr_segs)

	if (@probewrite(flags))
	  set_uint_arg(4, flags)
%)

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

# dw_vmsplice _____________________________________________________

probe dw_syscall.vmsplice = kernel.function("sys_vmsplice").call ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_VMSPLICE_NAME
	fd = __int32($fd)
	iov = $iov
	nr_segs = __ulong($nr_segs)
	flags = $flags
	flags_str = _stp_splice_flags_str(flags)
	@_SYSCALL_VMSPLICE_ARGSTR
}
probe dw_syscall.vmsplice.return = kernel.function("sys_vmsplice").return ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_vmsplice _____________________________________________________

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

probe nd1_syscall.vmsplice = kprobe.function("sys_vmsplice") ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_VMSPLICE_NAME
	asmlinkage()
	@_SYSCALL_VMSPLICE_REGARGS
	@_SYSCALL_VMSPLICE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.vmsplice = kprobe.function(@arch_syscall_prefix "sys_vmsplice") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_VMSPLICE_NAME
	@_SYSCALL_VMSPLICE_REGARGS
	@_SYSCALL_VMSPLICE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_VMSPLICE_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.vmsplice = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_vmsplice"))
	@_SYSCALL_VMSPLICE_NAME
	@_SYSCALL_VMSPLICE_REGARGS
	@_SYSCALL_VMSPLICE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_VMSPLICE_REGARGS_STORE %)
}

probe nd_syscall.vmsplice.return = nd1_syscall.vmsplice.return!, nd2_syscall.vmsplice.return!, tp_syscall.vmsplice.return
  { }

probe nd1_syscall.vmsplice.return = kprobe.function("sys_vmsplice").return ?
{
	@__syscall_gate_compat_simple
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.vmsplice.return = kprobe.function(@arch_syscall_prefix "sys_vmsplice").return ?
{
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.vmsplice.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_gate(@const("__NR_vmsplice"))
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR($ret)
}

# compat_vmsplice ____________________________________________
#
# long sys_vmsplice(int fd, const struct iovec __user *iov,
#	    unsigned long nr_segs, unsigned int flags)
# long compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
#	    unsigned int nr_segs, unsigned int flags)
#

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

# dw_compat_vmsplice _____________________________________________________

probe dw_syscall.compat_vmsplice = kernel.function("compat_sys_vmsplice").call ?
{
	@_SYSCALL_VMSPLICE_NAME
	fd = __int32($fd)
	iov = @__pointer($iov32)
	nr_segs = __uint32($nr_segs)
	flags = $flags
	flags_str = _stp_splice_flags_str(flags)
	@_SYSCALL_VMSPLICE_ARGSTR
}
probe dw_syscall.compat_vmsplice.return = kernel.function("compat_sys_vmsplice").return ?
{
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_compat_vmsplice _____________________________________________________

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

probe nd1_syscall.compat_vmsplice = kprobe.function("compat_sys_vmsplice") ?
{
	@_SYSCALL_VMSPLICE_NAME
	asmlinkage()
	@_SYSCALL_VMSPLICE_REGARGS
	@_SYSCALL_VMSPLICE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.compat_vmsplice =
	kprobe.function(@arch_syscall_prefix "compat_sys_vmsplice") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_VMSPLICE_NAME
	@_SYSCALL_VMSPLICE_REGARGS
	@_SYSCALL_VMSPLICE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_VMSPLICE_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_vmsplice = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_vmsplice"))
	@_SYSCALL_VMSPLICE_NAME
	@_SYSCALL_VMSPLICE_REGARGS
	@_SYSCALL_VMSPLICE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_VMSPLICE_REGARGS_STORE %)
}

probe nd_syscall.compat_vmsplice.return = nd1_syscall.compat_vmsplice.return!, nd2_syscall.compat_vmsplice.return!, tp_syscall.compat_vmsplice.return
  { }

probe nd1_syscall.compat_vmsplice.return = kprobe.function("compat_sys_vmsplice").return ?
{
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.compat_vmsplice.return =
	kprobe.function(@arch_syscall_prefix "compat_sys_vmsplice").return ?
{
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.compat_vmsplice.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__compat_syscall_gate(@const("__NR_compat_vmsplice"))
	@_SYSCALL_VMSPLICE_NAME
	@SYSC_RETVALSTR($ret)
}
