blob: b83d0e6980a3e15a0f8b6ebea654bebc71184421 [file] [log] [blame]
Marc Zyngierf27bb132012-03-05 11:49:33 +00001/*
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
AKASHI Takahiro875cbf32014-07-04 08:28:30 +010019#include <uapi/linux/audit.h>
20#include <linux/compat.h>
Marc Zyngierf27bb132012-03-05 11:49:33 +000021#include <linux/err.h>
22
Mark Rutland27d83e62018-07-11 14:56:42 +010023typedef long (*syscall_fn_t)(unsigned long, unsigned long,
24 unsigned long, unsigned long,
25 unsigned long, unsigned long);
26
27extern const syscall_fn_t sys_call_table[];
Marc Zyngierf27bb132012-03-05 11:49:33 +000028
Mark Rutland3b714272018-07-11 14:56:45 +010029#ifdef CONFIG_COMPAT
30extern const syscall_fn_t compat_sys_call_table[];
31#endif
32
Marc Zyngierf27bb132012-03-05 11:49:33 +000033static inline int syscall_get_nr(struct task_struct *task,
34 struct pt_regs *regs)
35{
36 return regs->syscallno;
37}
38
39static inline void syscall_rollback(struct task_struct *task,
40 struct pt_regs *regs)
41{
42 regs->regs[0] = regs->orig_x0;
43}
44
45
46static inline long syscall_get_error(struct task_struct *task,
47 struct pt_regs *regs)
48{
49 unsigned long error = regs->regs[0];
50 return IS_ERR_VALUE(error) ? error : 0;
51}
52
53static inline long syscall_get_return_value(struct task_struct *task,
54 struct pt_regs *regs)
55{
56 return regs->regs[0];
57}
58
59static inline void syscall_set_return_value(struct task_struct *task,
60 struct pt_regs *regs,
61 int error, long val)
62{
63 regs->regs[0] = (long) error ? error : val;
64}
65
66#define SYSCALL_MAX_ARGS 6
67
68static inline void syscall_get_arguments(struct task_struct *task,
69 struct pt_regs *regs,
70 unsigned int i, unsigned int n,
71 unsigned long *args)
72{
AKASHI Takahiro7b22c032013-10-03 06:47:44 +010073 if (n == 0)
74 return;
75
Marc Zyngierf27bb132012-03-05 11:49:33 +000076 if (i + n > SYSCALL_MAX_ARGS) {
77 unsigned long *args_bad = args + SYSCALL_MAX_ARGS - i;
78 unsigned int n_bad = n + i - SYSCALL_MAX_ARGS;
79 pr_warning("%s called with max args %d, handling only %d\n",
80 __func__, i + n, SYSCALL_MAX_ARGS);
81 memset(args_bad, 0, n_bad * sizeof(args[0]));
82 }
83
84 if (i == 0) {
85 args[0] = regs->orig_x0;
86 args++;
87 i++;
88 n--;
89 }
90
91 memcpy(args, &regs->regs[i], n * sizeof(args[0]));
92}
93
94static inline void syscall_set_arguments(struct task_struct *task,
95 struct pt_regs *regs,
96 unsigned int i, unsigned int n,
97 const unsigned long *args)
98{
AKASHI Takahiro7b22c032013-10-03 06:47:44 +010099 if (n == 0)
100 return;
101
Marc Zyngierf27bb132012-03-05 11:49:33 +0000102 if (i + n > SYSCALL_MAX_ARGS) {
103 pr_warning("%s called with max args %d, handling only %d\n",
104 __func__, i + n, SYSCALL_MAX_ARGS);
105 n = SYSCALL_MAX_ARGS - i;
106 }
107
108 if (i == 0) {
109 regs->orig_x0 = args[0];
110 args++;
111 i++;
112 n--;
113 }
114
115 memcpy(&regs->regs[i], args, n * sizeof(args[0]));
116}
117
AKASHI Takahiro875cbf32014-07-04 08:28:30 +0100118/*
119 * We don't care about endianness (__AUDIT_ARCH_LE bit) here because
120 * AArch64 has the same system calls both on little- and big- endian.
121 */
122static inline int syscall_get_arch(void)
123{
124 if (is_compat_task())
125 return AUDIT_ARCH_ARM;
126
127 return AUDIT_ARCH_AARCH64;
128}
129
Marc Zyngierf27bb132012-03-05 11:49:33 +0000130#endif /* __ASM_SYSCALL_H */