# pkey_free _______________________________________________________
# long sys_pkey_free(int pkey)

@define _SYSCALL_PKEY_FREE_NAME
%(
	name = "pkey_free"
%)

@define _SYSCALL_PKEY_FREE_ARGSTR
%(
	argstr = sprintf("%d", pkey)
%)

@define _SYSCALL_PKEY_FREE_REGARGS
%(
	pkey = int_arg(1)
%)

@define _SYSCALL_PKEY_FREE_REGARGS_STORE
%(
        if (@probewrite(pkey))
          set_int_arg(1, pkey)
%)

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

# dw_pkey_free _____________________________________________________

probe dw_syscall.pkey_free = kernel.function("sys_pkey_free").call
{
	@_SYSCALL_PKEY_FREE_NAME
	pkey = __int32($pkey)
	@_SYSCALL_PKEY_FREE_ARGSTR
}
probe dw_syscall.pkey_free.return = kernel.function("sys_pkey_free").return
{
	@_SYSCALL_PKEY_FREE_NAME
	@SYSC_RETVALSTR($return)
}

# nd_pkey_free _____________________________________________________

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

probe nd1_syscall.pkey_free = kprobe.function("sys_pkey_free") ?
{
	@_SYSCALL_PKEY_FREE_NAME
	asmlinkage()
	@_SYSCALL_PKEY_FREE_REGARGS
	@_SYSCALL_PKEY_FREE_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.pkey_free = kprobe.function(@arch_syscall_prefix "sys_pkey_free") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_PKEY_FREE_NAME
	@_SYSCALL_PKEY_FREE_REGARGS
	@_SYSCALL_PKEY_FREE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PKEY_FREE_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.pkey_free = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_pkey_free"), @const("__NR_compat_pkey_free"))
	@_SYSCALL_PKEY_FREE_NAME
	@_SYSCALL_PKEY_FREE_REGARGS
	@_SYSCALL_PKEY_FREE_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_PKEY_FREE_REGARGS_STORE %)
}

probe nd_syscall.pkey_free.return = nd1_syscall.pkey_free.return!, nd2_syscall.pkey_free.return!, tp_syscall.pkey_free.return
  { }
  
probe nd1_syscall.pkey_free.return = kprobe.function("sys_pkey_free").return ?
{
	@_SYSCALL_PKEY_FREE_NAME
	@SYSC_RETVALSTR(returnval())
}

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