xref: /OK3568_Linux_fs/kernel/arch/nds32/include/asm/syscall.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun // Copyright (C) 2008-2009 Red Hat, Inc.  All rights reserved.
3*4882a593Smuzhiyun // Copyright (C) 2005-2017 Andes Technology Corporation
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #ifndef _ASM_NDS32_SYSCALL_H
6*4882a593Smuzhiyun #define _ASM_NDS32_SYSCALL_H	1
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <uapi/linux/audit.h>
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun struct task_struct;
11*4882a593Smuzhiyun struct pt_regs;
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /**
14*4882a593Smuzhiyun  * syscall_get_nr - find what system call a task is executing
15*4882a593Smuzhiyun  * @task:	task of interest, must be blocked
16*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * If @task is executing a system call or is at system call
19*4882a593Smuzhiyun  * tracing about to attempt one, returns the system call number.
20*4882a593Smuzhiyun  * If @task is not executing a system call, i.e. it's blocked
21*4882a593Smuzhiyun  * inside the kernel for a fault or signal, returns -1.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Note this returns int even on 64-bit machines.  Only 32 bits of
24*4882a593Smuzhiyun  * system call number can be meaningful.  If the actual arch value
25*4882a593Smuzhiyun  * is 64 bits, this truncates to 32 bits so 0xffffffff means -1.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * It's only valid to call this when @task is known to be blocked.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun static inline int
syscall_get_nr(struct task_struct * task,struct pt_regs * regs)30*4882a593Smuzhiyun syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	return regs->syscallno;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun  * syscall_rollback - roll back registers after an aborted system call
37*4882a593Smuzhiyun  * @task:	task of interest, must be in system call exit tracing
38*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * It's only valid to call this when @task is stopped for system
41*4882a593Smuzhiyun  * call exit tracing (due to TIF_SYSCALL_TRACE or TIF_SYSCALL_AUDIT),
42*4882a593Smuzhiyun  * after tracehook_report_syscall_entry() returned nonzero to prevent
43*4882a593Smuzhiyun  * the system call from taking place.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * This rolls back the register state in @regs so it's as if the
46*4882a593Smuzhiyun  * system call instruction was a no-op.  The registers containing
47*4882a593Smuzhiyun  * the system call number and arguments are as they were before the
48*4882a593Smuzhiyun  * system call instruction.  This may not be the same as what the
49*4882a593Smuzhiyun  * register state looked like at system call entry tracing.
50*4882a593Smuzhiyun  */
51*4882a593Smuzhiyun static inline void
syscall_rollback(struct task_struct * task,struct pt_regs * regs)52*4882a593Smuzhiyun syscall_rollback(struct task_struct *task, struct pt_regs *regs)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	regs->uregs[0] = regs->orig_r0;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /**
58*4882a593Smuzhiyun  * syscall_get_error - check result of traced system call
59*4882a593Smuzhiyun  * @task:	task of interest, must be blocked
60*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Returns 0 if the system call succeeded, or -ERRORCODE if it failed.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * It's only valid to call this when @task is stopped for tracing on exit
65*4882a593Smuzhiyun  * from a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun static inline long
syscall_get_error(struct task_struct * task,struct pt_regs * regs)68*4882a593Smuzhiyun syscall_get_error(struct task_struct *task, struct pt_regs *regs)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	unsigned long error = regs->uregs[0];
71*4882a593Smuzhiyun 	return IS_ERR_VALUE(error) ? error : 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  * syscall_get_return_value - get the return value of a traced system call
76*4882a593Smuzhiyun  * @task:	task of interest, must be blocked
77*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Returns the return value of the successful system call.
80*4882a593Smuzhiyun  * This value is meaningless if syscall_get_error() returned nonzero.
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  * It's only valid to call this when @task is stopped for tracing on exit
83*4882a593Smuzhiyun  * from a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun static inline long
syscall_get_return_value(struct task_struct * task,struct pt_regs * regs)86*4882a593Smuzhiyun syscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	return regs->uregs[0];
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun  * syscall_set_return_value - change the return value of a traced system call
93*4882a593Smuzhiyun  * @task:	task of interest, must be blocked
94*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
95*4882a593Smuzhiyun  * @error:	negative error code, or zero to indicate success
96*4882a593Smuzhiyun  * @val:	user return value if @error is zero
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * This changes the results of the system call that user mode will see.
99*4882a593Smuzhiyun  * If @error is zero, the user sees a successful system call with a
100*4882a593Smuzhiyun  * return value of @val.  If @error is nonzero, it's a negated errno
101*4882a593Smuzhiyun  * code; the user sees a failed system call with this errno code.
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * It's only valid to call this when @task is stopped for tracing on exit
104*4882a593Smuzhiyun  * from a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun static inline void
syscall_set_return_value(struct task_struct * task,struct pt_regs * regs,int error,long val)107*4882a593Smuzhiyun syscall_set_return_value(struct task_struct *task, struct pt_regs *regs,
108*4882a593Smuzhiyun 			 int error, long val)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	regs->uregs[0] = (long)error ? error : val;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun  * syscall_get_arguments - extract system call parameter values
115*4882a593Smuzhiyun  * @task:	task of interest, must be blocked
116*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
117*4882a593Smuzhiyun  * @args:	array filled with argument values
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * Fetches 6 arguments to the system call (from 0 through 5). The first
120*4882a593Smuzhiyun  * argument is stored in @args[0], and so on.
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * It's only valid to call this when @task is stopped for tracing on
123*4882a593Smuzhiyun  * entry to a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun #define SYSCALL_MAX_ARGS 6
126*4882a593Smuzhiyun static inline void
syscall_get_arguments(struct task_struct * task,struct pt_regs * regs,unsigned long * args)127*4882a593Smuzhiyun syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
128*4882a593Smuzhiyun 		      unsigned long *args)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	args[0] = regs->orig_r0;
131*4882a593Smuzhiyun 	args++;
132*4882a593Smuzhiyun 	memcpy(args, &regs->uregs[0] + 1, 5 * sizeof(args[0]));
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /**
136*4882a593Smuzhiyun  * syscall_set_arguments - change system call parameter value
137*4882a593Smuzhiyun  * @task:	task of interest, must be in system call entry tracing
138*4882a593Smuzhiyun  * @regs:	task_pt_regs() of @task
139*4882a593Smuzhiyun  * @args:	array of argument values to store
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * Changes 6 arguments to the system call. The first argument gets value
142*4882a593Smuzhiyun  * @args[0], and so on.
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * It's only valid to call this when @task is stopped for tracing on
145*4882a593Smuzhiyun  * entry to a system call, due to %TIF_SYSCALL_TRACE or %TIF_SYSCALL_AUDIT.
146*4882a593Smuzhiyun  */
147*4882a593Smuzhiyun static inline void
syscall_set_arguments(struct task_struct * task,struct pt_regs * regs,const unsigned long * args)148*4882a593Smuzhiyun syscall_set_arguments(struct task_struct *task, struct pt_regs *regs,
149*4882a593Smuzhiyun 		      const unsigned long *args)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	regs->orig_r0 = args[0];
152*4882a593Smuzhiyun 	args++;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	memcpy(&regs->uregs[0] + 1, args, 5 * sizeof(args[0]));
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun static inline int
syscall_get_arch(struct task_struct * task)158*4882a593Smuzhiyun syscall_get_arch(struct task_struct *task)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
161*4882a593Smuzhiyun 		? AUDIT_ARCH_NDS32BE : AUDIT_ARCH_NDS32;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun #endif /* _ASM_NDS32_SYSCALL_H */
165