# userfaultfd ________________________________________________
# SYSCALL_DEFINE2(userfaultfd, int, flags)

@define _SYSCALL_USERFAULTFD_NAME
%(
	name = "userfaultfd"
%)

@define _SYSCALL_USERFAULTFD_ARGSTR
%(
	argstr = sprintf("%s", flags_str)
%)

@define _SYSCALL_USERFAULTFD_REGARGS
%(
	flags = int_arg(1)
	flags_str = _userfaultfd_flags_str(flags)
%)

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

# dw_userfaultfd _____________________________________________________

probe dw_syscall.userfaultfd = kernel.function("sys_userfaultfd").call ?
{
	@_SYSCALL_USERFAULTFD_NAME
	flags = __int32($flags)
	flags_str = _userfaultfd_flags_str(flags)
	@_SYSCALL_USERFAULTFD_ARGSTR
}
probe dw_syscall.userfaultfd.return = kernel.function("sys_userfaultfd").return ?
{
	@_SYSCALL_USERFAULTFD_NAME
	@SYSC_RETVALSTR($return)
}

# nd_userfaultfd _____________________________________________________

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

probe nd1_syscall.userfaultfd = kprobe.function("sys_userfaultfd").call ?
{
	@_SYSCALL_USERFAULTFD_NAME
	asmlinkage()
	@_SYSCALL_USERFAULTFD_REGARGS
	@_SYSCALL_USERFAULTFD_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.userfaultfd =
	kprobe.function(@arch_syscall_prefix "sys_userfaultfd") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_USERFAULTFD_NAME
	@_SYSCALL_USERFAULTFD_REGARGS
	@_SYSCALL_USERFAULTFD_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.userfaultfd = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_userfaultfd"),
		               @const("__NR_compat_userfaultfd"))
	@_SYSCALL_USERFAULTFD_NAME
	@_SYSCALL_USERFAULTFD_REGARGS
	@_SYSCALL_USERFAULTFD_ARGSTR
}

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

probe nd1_syscall.userfaultfd.return =
	kprobe.function("sys_userfaultfd").return ?
{
	@_SYSCALL_USERFAULTFD_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.userfaultfd.return =
	kprobe.function(@arch_syscall_prefix "sys_userfaultfd").return ?
{
	@_SYSCALL_USERFAULTFD_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.userfaultfd.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_userfaultfd"),
			       @const("__NR_compat_userfaultfd"))
	@_SYSCALL_USERFAULTFD_NAME
	@SYSC_RETVALSTR($ret)
}
