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