Merge tag 'audit-pr-20190507' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoor...
[linux-2.6-microblaze.git] / arch / x86 / include / asm / syscall.h
1 /*
2  * Access to user system call parameters and results
3  *
4  * Copyright (C) 2008-2009 Red Hat, Inc.  All rights reserved.
5  *
6  * This copyrighted material is made available to anyone wishing to use,
7  * modify, copy, or redistribute it subject to the terms and conditions
8  * of the GNU General Public License v.2.
9  *
10  * See asm-generic/syscall.h for descriptions of what we must do here.
11  */
12
13 #ifndef _ASM_X86_SYSCALL_H
14 #define _ASM_X86_SYSCALL_H
15
16 #include <uapi/linux/audit.h>
17 #include <linux/sched.h>
18 #include <linux/err.h>
19 #include <asm/asm-offsets.h>    /* For NR_syscalls */
20 #include <asm/thread_info.h>    /* for TS_COMPAT */
21 #include <asm/unistd.h>
22
23 #ifdef CONFIG_X86_64
24 typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *);
25 #else
26 typedef asmlinkage long (*sys_call_ptr_t)(unsigned long, unsigned long,
27                                           unsigned long, unsigned long,
28                                           unsigned long, unsigned long);
29 #endif /* CONFIG_X86_64 */
30 extern const sys_call_ptr_t sys_call_table[];
31
32 #if defined(CONFIG_X86_32)
33 #define ia32_sys_call_table sys_call_table
34 #define __NR_syscall_compat_max __NR_syscall_max
35 #define IA32_NR_syscalls NR_syscalls
36 #endif
37
38 #if defined(CONFIG_IA32_EMULATION)
39 extern const sys_call_ptr_t ia32_sys_call_table[];
40 #endif
41
42 /*
43  * Only the low 32 bits of orig_ax are meaningful, so we return int.
44  * This importantly ignores the high bits on 64-bit, so comparisons
45  * sign-extend the low 32 bits.
46  */
47 static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
48 {
49         return regs->orig_ax;
50 }
51
52 static inline void syscall_rollback(struct task_struct *task,
53                                     struct pt_regs *regs)
54 {
55         regs->ax = regs->orig_ax;
56 }
57
58 static inline long syscall_get_error(struct task_struct *task,
59                                      struct pt_regs *regs)
60 {
61         unsigned long error = regs->ax;
62 #ifdef CONFIG_IA32_EMULATION
63         /*
64          * TS_COMPAT is set for 32-bit syscall entries and then
65          * remains set until we return to user mode.
66          */
67         if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
68                 /*
69                  * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
70                  * and will match correctly in comparisons.
71                  */
72                 error = (long) (int) error;
73 #endif
74         return IS_ERR_VALUE(error) ? error : 0;
75 }
76
77 static inline long syscall_get_return_value(struct task_struct *task,
78                                             struct pt_regs *regs)
79 {
80         return regs->ax;
81 }
82
83 static inline void syscall_set_return_value(struct task_struct *task,
84                                             struct pt_regs *regs,
85                                             int error, long val)
86 {
87         regs->ax = (long) error ?: val;
88 }
89
90 #ifdef CONFIG_X86_32
91
92 static inline void syscall_get_arguments(struct task_struct *task,
93                                          struct pt_regs *regs,
94                                          unsigned long *args)
95 {
96         memcpy(args, &regs->bx, 6 * sizeof(args[0]));
97 }
98
99 static inline void syscall_set_arguments(struct task_struct *task,
100                                          struct pt_regs *regs,
101                                          unsigned int i, unsigned int n,
102                                          const unsigned long *args)
103 {
104         BUG_ON(i + n > 6);
105         memcpy(&regs->bx + i, args, n * sizeof(args[0]));
106 }
107
108 static inline int syscall_get_arch(struct task_struct *task)
109 {
110         return AUDIT_ARCH_I386;
111 }
112
113 #else    /* CONFIG_X86_64 */
114
115 static inline void syscall_get_arguments(struct task_struct *task,
116                                          struct pt_regs *regs,
117                                          unsigned long *args)
118 {
119 # ifdef CONFIG_IA32_EMULATION
120         if (task->thread_info.status & TS_COMPAT) {
121                 *args++ = regs->bx;
122                 *args++ = regs->cx;
123                 *args++ = regs->dx;
124                 *args++ = regs->si;
125                 *args++ = regs->di;
126                 *args   = regs->bp;
127         } else
128 # endif
129         {
130                 *args++ = regs->di;
131                 *args++ = regs->si;
132                 *args++ = regs->dx;
133                 *args++ = regs->r10;
134                 *args++ = regs->r8;
135                 *args   = regs->r9;
136         }
137 }
138
139 static inline void syscall_set_arguments(struct task_struct *task,
140                                          struct pt_regs *regs,
141                                          const unsigned long *args)
142 {
143 # ifdef CONFIG_IA32_EMULATION
144         if (task->thread_info.status & TS_COMPAT) {
145                 regs->bx = *args++;
146                 regs->cx = *args++;
147                 regs->dx = *args++;
148                 regs->si = *args++;
149                 regs->di = *args++;
150                 regs->bp = *args;
151         } else
152 # endif
153         {
154                 regs->di = *args++;
155                 regs->si = *args++;
156                 regs->dx = *args++;
157                 regs->r10 = *args++;
158                 regs->r8 = *args++;
159                 regs->r9 = *args;
160         }
161 }
162
163 static inline int syscall_get_arch(struct task_struct *task)
164 {
165         /* x32 tasks should be considered AUDIT_ARCH_X86_64. */
166         return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
167                 task->thread_info.status & TS_COMPAT)
168                 ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
169 }
170 #endif  /* CONFIG_X86_32 */
171
172 #endif  /* _ASM_X86_SYSCALL_H */