GNU Linux-libre 4.19.268-gnu1
[releases.git] / arch / arm64 / include / asm / syscall.h
1 /*
2  * Copyright (C) 2012 ARM Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #ifndef __ASM_SYSCALL_H
17 #define __ASM_SYSCALL_H
18
19 #include <uapi/linux/audit.h>
20 #include <linux/compat.h>
21 #include <linux/err.h>
22
23 typedef long (*syscall_fn_t)(const struct pt_regs *regs);
24
25 extern const syscall_fn_t sys_call_table[];
26
27 #ifdef CONFIG_COMPAT
28 extern const syscall_fn_t compat_sys_call_table[];
29 #endif
30
31 static inline int syscall_get_nr(struct task_struct *task,
32                                  struct pt_regs *regs)
33 {
34         return regs->syscallno;
35 }
36
37 static inline void syscall_rollback(struct task_struct *task,
38                                     struct pt_regs *regs)
39 {
40         regs->regs[0] = regs->orig_x0;
41 }
42
43
44 static inline long syscall_get_error(struct task_struct *task,
45                                      struct pt_regs *regs)
46 {
47         unsigned long error = regs->regs[0];
48
49         if (is_compat_thread(task_thread_info(task)))
50                 error = sign_extend64(error, 31);
51
52         return IS_ERR_VALUE(error) ? error : 0;
53 }
54
55 static inline long syscall_get_return_value(struct task_struct *task,
56                                             struct pt_regs *regs)
57 {
58         return regs->regs[0];
59 }
60
61 static inline void syscall_set_return_value(struct task_struct *task,
62                                             struct pt_regs *regs,
63                                             int error, long val)
64 {
65         if (error)
66                 val = error;
67
68         if (is_compat_thread(task_thread_info(task)))
69                 val = lower_32_bits(val);
70
71         regs->regs[0] = val;
72 }
73
74 #define SYSCALL_MAX_ARGS 6
75
76 static inline void syscall_get_arguments(struct task_struct *task,
77                                          struct pt_regs *regs,
78                                          unsigned int i, unsigned int n,
79                                          unsigned long *args)
80 {
81         if (n == 0)
82                 return;
83
84         if (i + n > SYSCALL_MAX_ARGS) {
85                 unsigned long *args_bad = args + SYSCALL_MAX_ARGS - i;
86                 unsigned int n_bad = n + i - SYSCALL_MAX_ARGS;
87                 pr_warning("%s called with max args %d, handling only %d\n",
88                            __func__, i + n, SYSCALL_MAX_ARGS);
89                 memset(args_bad, 0, n_bad * sizeof(args[0]));
90         }
91
92         if (i == 0) {
93                 args[0] = regs->orig_x0;
94                 args++;
95                 i++;
96                 n--;
97         }
98
99         memcpy(args, &regs->regs[i], n * sizeof(args[0]));
100 }
101
102 static inline void syscall_set_arguments(struct task_struct *task,
103                                          struct pt_regs *regs,
104                                          unsigned int i, unsigned int n,
105                                          const unsigned long *args)
106 {
107         if (n == 0)
108                 return;
109
110         if (i + n > SYSCALL_MAX_ARGS) {
111                 pr_warning("%s called with max args %d, handling only %d\n",
112                            __func__, i + n, SYSCALL_MAX_ARGS);
113                 n = SYSCALL_MAX_ARGS - i;
114         }
115
116         if (i == 0) {
117                 regs->orig_x0 = args[0];
118                 args++;
119                 i++;
120                 n--;
121         }
122
123         memcpy(&regs->regs[i], args, n * sizeof(args[0]));
124 }
125
126 /*
127  * We don't care about endianness (__AUDIT_ARCH_LE bit) here because
128  * AArch64 has the same system calls both on little- and big- endian.
129  */
130 static inline int syscall_get_arch(void)
131 {
132         if (is_compat_task())
133                 return AUDIT_ARCH_ARM;
134
135         return AUDIT_ARCH_AARCH64;
136 }
137
138 #endif  /* __ASM_SYSCALL_H */