# mlock ______________________________________________________
# long sys_mlock(unsigned long start, size_t len)
#

@define _SYSCALL_MLOCK_NAME
%(
	name = "mlock"
%)

@define _SYSCALL_MLOCK_ARGSTR
%(
	argstr = sprintf("%p, %u", addr, len)
%)

@define _SYSCALL_MLOCK_REGARGS
%(
	addr = ulong_arg(1)
	len = ulong_arg(2)
%)

@define _SYSCALL_MLOCK_REGARGS_STORE
%(
        if (@probewrite(addr))
          set_ulong_arg(1, addr)

	if (@probewrite(len))
	 set_ulong_arg(2, len)
%)

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

# dw_mlock _____________________________________________________

probe dw_syscall.mlock = kernel.function("sys_mlock").call ?
{
	@_SYSCALL_MLOCK_NAME
	addr = __ulong($start)
	len = __ulong($len)
	@_SYSCALL_MLOCK_ARGSTR
}
probe dw_syscall.mlock.return = kernel.function("sys_mlock").return ?
{
	@_SYSCALL_MLOCK_NAME
	@SYSC_RETVALSTR($return)
}

# nd_mlock _____________________________________________________

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

probe nd1_syscall.mlock = kprobe.function("sys_mlock") ?
{
	@_SYSCALL_MLOCK_NAME
	asmlinkage()
	@_SYSCALL_MLOCK_REGARGS
	@_SYSCALL_MLOCK_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.mlock = kprobe.function(@arch_syscall_prefix "sys_mlock") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_MLOCK_NAME
	@_SYSCALL_MLOCK_REGARGS
	@_SYSCALL_MLOCK_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_MLOCK_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mlock = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_mlock"), @const("__NR_compat_mlock"))
	@_SYSCALL_MLOCK_NAME
	@_SYSCALL_MLOCK_REGARGS
	@_SYSCALL_MLOCK_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_MLOCK_REGARGS_STORE %)
}

probe nd_syscall.mlock.return = nd1_syscall.mlock.return!, nd2_syscall.mlock.return!, tp_syscall.mlock.return
  { }
  
probe nd1_syscall.mlock.return = kprobe.function("sys_mlock").return ?
{
	@_SYSCALL_MLOCK_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.mlock.return = kprobe.function(@arch_syscall_prefix "sys_mlock").return ?
{
	@_SYSCALL_MLOCK_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.mlock.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_mlock"), @const("__NR_compat_mlock"))
	@_SYSCALL_MLOCK_NAME
	@SYSC_RETVALSTR($ret)
}
