# kexec_file_load ____________________________________________
# The kexec_file_load() system call first appeared in Linux 3.17
# SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
#		  unsigned long, cmdline_len, const char __user *, cmdline_ptr,
#		  unsigned long, flags)

@define _SYSCALL_KEXEC_FILE_LOAD_NAME
%(
	name = "kexec_file_load"
%)

@define _SYSCALL_KEXEC_FILE_LOAD_ARGSTR
%(
	argstr = sprintf("%d, %d, %u, %s, %s", kernel_fd, initrd_fd, cmdline_len,
	                 cmdline, flags_str)
%)

@define _SYSCALL_KEXEC_FILE_LOAD_REGARGS
%(
	kernel_fd = int_arg(1)
	initrd_fd = int_arg(2)
	cmdline_len = ulong_arg(3)
	cmdline = user_string_quoted(pointer_arg(4))
	flags = ulong_arg(5)
	flags_str = _kexec_file_load_flags_str(flags)
%)

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

# dw_kexec_file_load _____________________________________________________

probe dw_syscall.kexec_file_load = kernel.function("sys_kexec_file_load").call ?
{
	@_SYSCALL_KEXEC_FILE_LOAD_NAME
	kernel_fd = __int32($kernel_fd)
	initrd_fd = __int32($initrd_fd)
	cmdline_len = @__compat_ulong($cmdline_len)
	cmdline = user_string_quoted($cmdline_ptr)
	flags = @__compat_ulong($flags)
	flags_str = _kexec_file_load_flags_str(flags)
	@_SYSCALL_KEXEC_FILE_LOAD_ARGSTR
}
probe dw_syscall.kexec_file_load.return =
	kernel.function("sys_kexec_file_load").return ?
{
	@_SYSCALL_KEXEC_FILE_LOAD_NAME
	@SYSC_RETVALSTR($return)
}

# nd_kexec_file_load _____________________________________________________

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

probe nd1_syscall.kexec_file_load = kprobe.function("sys_kexec_file_load") ?
{
	@_SYSCALL_KEXEC_FILE_LOAD_NAME
	@_SYSCALL_KEXEC_FILE_LOAD_REGARGS
	@_SYSCALL_KEXEC_FILE_LOAD_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.kexec_file_load = kprobe.function(@arch_syscall_prefix "sys_kexec_file_load") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_KEXEC_FILE_LOAD_NAME
	@_SYSCALL_KEXEC_FILE_LOAD_REGARGS
	@_SYSCALL_KEXEC_FILE_LOAD_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.kexec_file_load = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_kexec_file_load"), @const("__NR_compat_kexec_file_load"))
	@_SYSCALL_KEXEC_FILE_LOAD_NAME
	@_SYSCALL_KEXEC_FILE_LOAD_REGARGS
	@_SYSCALL_KEXEC_FILE_LOAD_ARGSTR
}

probe nd_syscall.kexec_file_load.return = nd1_syscall.kexec_file_load.return!, nd2_syscall.kexec_file_load.return!, tp_syscall.kexec_file_load.return
  { }
  
probe nd1_syscall.kexec_file_load.return =
	kprobe.function("sys_kexec_file_load").return ?
{
	@_SYSCALL_KEXEC_FILE_LOAD_NAME
	@SYSC_RETVALSTR(returnval())
}

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