Marc Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 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 | |
AKASHI Takahiro | 875cbf3 | 2014-07-04 08:28:30 +0100 | [diff] [blame] | 19 | #include <uapi/linux/audit.h> |
| 20 | #include <linux/compat.h> |
Marc Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | |
Mark Rutland | 27d83e6 | 2018-07-11 14:56:42 +0100 | [diff] [blame] | 23 | typedef long (*syscall_fn_t)(unsigned long, unsigned long, |
| 24 | unsigned long, unsigned long, |
| 25 | unsigned long, unsigned long); |
| 26 | |
| 27 | extern const syscall_fn_t sys_call_table[]; |
Marc Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 28 | |
Mark Rutland | 3b71427 | 2018-07-11 14:56:45 +0100 | [diff] [blame^] | 29 | #ifdef CONFIG_COMPAT |
| 30 | extern const syscall_fn_t compat_sys_call_table[]; |
| 31 | #endif |
| 32 | |
Marc Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 33 | static inline int syscall_get_nr(struct task_struct *task, |
| 34 | struct pt_regs *regs) |
| 35 | { |
| 36 | return regs->syscallno; |
| 37 | } |
| 38 | |
| 39 | static inline void syscall_rollback(struct task_struct *task, |
| 40 | struct pt_regs *regs) |
| 41 | { |
| 42 | regs->regs[0] = regs->orig_x0; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static 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 | |
| 53 | static inline long syscall_get_return_value(struct task_struct *task, |
| 54 | struct pt_regs *regs) |
| 55 | { |
| 56 | return regs->regs[0]; |
| 57 | } |
| 58 | |
| 59 | static 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 | |
| 68 | static 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 Takahiro | 7b22c03 | 2013-10-03 06:47:44 +0100 | [diff] [blame] | 73 | if (n == 0) |
| 74 | return; |
| 75 | |
Marc Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 76 | 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, ®s->regs[i], n * sizeof(args[0])); |
| 92 | } |
| 93 | |
| 94 | static 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 Takahiro | 7b22c03 | 2013-10-03 06:47:44 +0100 | [diff] [blame] | 99 | if (n == 0) |
| 100 | return; |
| 101 | |
Marc Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 102 | 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(®s->regs[i], args, n * sizeof(args[0])); |
| 116 | } |
| 117 | |
AKASHI Takahiro | 875cbf3 | 2014-07-04 08:28:30 +0100 | [diff] [blame] | 118 | /* |
| 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 | */ |
| 122 | static 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 Zyngier | f27bb13 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 130 | #endif /* __ASM_SYSCALL_H */ |