18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci// Copyright (C) 2008-2009 Red Hat, Inc.  All rights reserved.
38c2ecf20Sopenharmony_ci// Copyright (C) 2005-2017 Andes Technology Corporation
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifndef _ASM_NDS32_SYSCALL_H
68c2ecf20Sopenharmony_ci#define _ASM_NDS32_SYSCALL_H	1
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <uapi/linux/audit.h>
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_cistruct task_struct;
118c2ecf20Sopenharmony_cistruct pt_regs;
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/**
148c2ecf20Sopenharmony_ci * syscall_get_nr - find what system call a task is executing
158c2ecf20Sopenharmony_ci * @task:	task of interest, must be blocked
168c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * If @task is executing a system call or is at system call
198c2ecf20Sopenharmony_ci * tracing about to attempt one, returns the system call number.
208c2ecf20Sopenharmony_ci * If @task is not executing a system call, i.e. it's blocked
218c2ecf20Sopenharmony_ci * inside the kernel for a fault or signal, returns -1.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Note this returns int even on 64-bit machines.  Only 32 bits of
248c2ecf20Sopenharmony_ci * system call number can be meaningful.  If the actual arch value
258c2ecf20Sopenharmony_ci * is 64 bits, this truncates to 32 bits so 0xffffffff means -1.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * It's only valid to call this when @task is known to be blocked.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_cistatic inline int
308c2ecf20Sopenharmony_cisyscall_get_nr(struct task_struct *task, struct pt_regs *regs)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	return regs->syscallno;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/**
368c2ecf20Sopenharmony_ci * syscall_rollback - roll back registers after an aborted system call
378c2ecf20Sopenharmony_ci * @task:	task of interest, must be in system call exit tracing
388c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * It's only valid to call this when @task is stopped for system
418c2ecf20Sopenharmony_ci * call exit tracing (due to TIF_SYSCALL_TRACE or TIF_SYSCALL_AUDIT),
428c2ecf20Sopenharmony_ci * after tracehook_report_syscall_entry() returned nonzero to prevent
438c2ecf20Sopenharmony_ci * the system call from taking place.
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * This rolls back the register state in @regs so it's as if the
468c2ecf20Sopenharmony_ci * system call instruction was a no-op.  The registers containing
478c2ecf20Sopenharmony_ci * the system call number and arguments are as they were before the
488c2ecf20Sopenharmony_ci * system call instruction.  This may not be the same as what the
498c2ecf20Sopenharmony_ci * register state looked like at system call entry tracing.
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_cistatic inline void
528c2ecf20Sopenharmony_cisyscall_rollback(struct task_struct *task, struct pt_regs *regs)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	regs->uregs[0] = regs->orig_r0;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/**
588c2ecf20Sopenharmony_ci * syscall_get_error - check result of traced system call
598c2ecf20Sopenharmony_ci * @task:	task of interest, must be blocked
608c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * Returns 0 if the system call succeeded, or -ERRORCODE if it failed.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * It's only valid to call this when @task is stopped for tracing on exit
658c2ecf20Sopenharmony_ci * from a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_cistatic inline long
688c2ecf20Sopenharmony_cisyscall_get_error(struct task_struct *task, struct pt_regs *regs)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	unsigned long error = regs->uregs[0];
718c2ecf20Sopenharmony_ci	return IS_ERR_VALUE(error) ? error : 0;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * syscall_get_return_value - get the return value of a traced system call
768c2ecf20Sopenharmony_ci * @task:	task of interest, must be blocked
778c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * Returns the return value of the successful system call.
808c2ecf20Sopenharmony_ci * This value is meaningless if syscall_get_error() returned nonzero.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * It's only valid to call this when @task is stopped for tracing on exit
838c2ecf20Sopenharmony_ci * from a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
848c2ecf20Sopenharmony_ci */
858c2ecf20Sopenharmony_cistatic inline long
868c2ecf20Sopenharmony_cisyscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return regs->uregs[0];
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/**
928c2ecf20Sopenharmony_ci * syscall_set_return_value - change the return value of a traced system call
938c2ecf20Sopenharmony_ci * @task:	task of interest, must be blocked
948c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
958c2ecf20Sopenharmony_ci * @error:	negative error code, or zero to indicate success
968c2ecf20Sopenharmony_ci * @val:	user return value if @error is zero
978c2ecf20Sopenharmony_ci *
988c2ecf20Sopenharmony_ci * This changes the results of the system call that user mode will see.
998c2ecf20Sopenharmony_ci * If @error is zero, the user sees a successful system call with a
1008c2ecf20Sopenharmony_ci * return value of @val.  If @error is nonzero, it's a negated errno
1018c2ecf20Sopenharmony_ci * code; the user sees a failed system call with this errno code.
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * It's only valid to call this when @task is stopped for tracing on exit
1048c2ecf20Sopenharmony_ci * from a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_cistatic inline void
1078c2ecf20Sopenharmony_cisyscall_set_return_value(struct task_struct *task, struct pt_regs *regs,
1088c2ecf20Sopenharmony_ci			 int error, long val)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	regs->uregs[0] = (long)error ? error : val;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/**
1148c2ecf20Sopenharmony_ci * syscall_get_arguments - extract system call parameter values
1158c2ecf20Sopenharmony_ci * @task:	task of interest, must be blocked
1168c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
1178c2ecf20Sopenharmony_ci * @args:	array filled with argument values
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * Fetches 6 arguments to the system call (from 0 through 5). The first
1208c2ecf20Sopenharmony_ci * argument is stored in @args[0], and so on.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * It's only valid to call this when @task is stopped for tracing on
1238c2ecf20Sopenharmony_ci * entry to a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_ci#define SYSCALL_MAX_ARGS 6
1268c2ecf20Sopenharmony_cistatic inline void
1278c2ecf20Sopenharmony_cisyscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
1288c2ecf20Sopenharmony_ci		      unsigned long *args)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	args[0] = regs->orig_r0;
1318c2ecf20Sopenharmony_ci	args++;
1328c2ecf20Sopenharmony_ci	memcpy(args, &regs->uregs[0] + 1, 5 * sizeof(args[0]));
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/**
1368c2ecf20Sopenharmony_ci * syscall_set_arguments - change system call parameter value
1378c2ecf20Sopenharmony_ci * @task:	task of interest, must be in system call entry tracing
1388c2ecf20Sopenharmony_ci * @regs:	task_pt_regs() of @task
1398c2ecf20Sopenharmony_ci * @args:	array of argument values to store
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * Changes 6 arguments to the system call. The first argument gets value
1428c2ecf20Sopenharmony_ci * @args[0], and so on.
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * It's only valid to call this when @task is stopped for tracing on
1458c2ecf20Sopenharmony_ci * entry to a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistatic inline void
1488c2ecf20Sopenharmony_cisyscall_set_arguments(struct task_struct *task, struct pt_regs *regs,
1498c2ecf20Sopenharmony_ci		      const unsigned long *args)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	regs->orig_r0 = args[0];
1528c2ecf20Sopenharmony_ci	args++;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	memcpy(&regs->uregs[0] + 1, args, 5 * sizeof(args[0]));
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic inline int
1588c2ecf20Sopenharmony_cisyscall_get_arch(struct task_struct *task)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	return IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
1618c2ecf20Sopenharmony_ci		? AUDIT_ARCH_NDS32BE : AUDIT_ARCH_NDS32;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#endif /* _ASM_NDS32_SYSCALL_H */
165