blob: 999b7cd2e78c7ffe21526fd2b9715b3486216907 [file] [log] [blame]
Roland McGrath68bd0f42008-04-18 17:08:44 -07001/*
2 * Access to user system call parameters and results
3 *
Roland McGrath18c1e2c2009-09-22 19:57:51 -07004 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
Roland McGrath68bd0f42008-04-18 17:08:44 -07005 *
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
H. Peter Anvin5e1b0072008-10-23 00:20:33 -070013#ifndef _ASM_X86_SYSCALL_H
14#define _ASM_X86_SYSCALL_H
Roland McGrath68bd0f42008-04-18 17:08:44 -070015
Eric Paris579ec9e2014-03-11 12:55:42 -040016#include <uapi/linux/audit.h>
Roland McGrath68bd0f42008-04-18 17:08:44 -070017#include <linux/sched.h>
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020018#include <linux/err.h>
H. Peter Anvin72142fd2012-01-07 14:10:18 -080019#include <asm/asm-offsets.h> /* For NR_syscalls */
Will Drewryb7456532012-04-12 16:47:56 -050020#include <asm/thread_info.h> /* for TS_COMPAT */
H. Peter Anvinfca460f2012-02-19 07:56:26 -080021#include <asm/unistd.h>
Roland McGrath68bd0f42008-04-18 17:08:44 -070022
Andy Lutomirskieb974c62015-10-05 17:48:07 -070023typedef asmlinkage long (*sys_call_ptr_t)(unsigned long, unsigned long,
24 unsigned long, unsigned long,
25 unsigned long, unsigned long);
Andi Kleen1599e8f2013-08-05 15:02:35 -070026extern const sys_call_ptr_t sys_call_table[];
Mike Frysingere7b8e672010-01-26 04:40:03 -050027
Andy Lutomirski034042c2015-10-05 17:48:06 -070028#if defined(CONFIG_X86_32)
29#define ia32_sys_call_table sys_call_table
30#define __NR_syscall_compat_max __NR_syscall_max
31#define IA32_NR_syscalls NR_syscalls
32#endif
33
34#if defined(CONFIG_IA32_EMULATION)
35extern const sys_call_ptr_t ia32_sys_call_table[];
36#endif
37
Roland McGrath18c1e2c2009-09-22 19:57:51 -070038/*
39 * Only the low 32 bits of orig_ax are meaningful, so we return int.
40 * This importantly ignores the high bits on 64-bit, so comparisons
41 * sign-extend the low 32 bits.
42 */
43static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
Roland McGrath68bd0f42008-04-18 17:08:44 -070044{
Paul Moore8b4b9f22013-02-15 12:21:43 -050045 return regs->orig_ax;
Roland McGrath68bd0f42008-04-18 17:08:44 -070046}
47
48static inline void syscall_rollback(struct task_struct *task,
49 struct pt_regs *regs)
50{
Paul Moore8b4b9f22013-02-15 12:21:43 -050051 regs->ax = regs->orig_ax;
Roland McGrath68bd0f42008-04-18 17:08:44 -070052}
53
54static inline long syscall_get_error(struct task_struct *task,
55 struct pt_regs *regs)
56{
57 unsigned long error = regs->ax;
58#ifdef CONFIG_IA32_EMULATION
59 /*
60 * TS_COMPAT is set for 32-bit syscall entries and then
61 * remains set until we return to user mode.
62 */
63 if (task_thread_info(task)->status & TS_COMPAT)
64 /*
65 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
66 * and will match correctly in comparisons.
67 */
68 error = (long) (int) error;
69#endif
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020070 return IS_ERR_VALUE(error) ? error : 0;
Roland McGrath68bd0f42008-04-18 17:08:44 -070071}
72
73static inline long syscall_get_return_value(struct task_struct *task,
74 struct pt_regs *regs)
75{
76 return regs->ax;
77}
78
79static inline void syscall_set_return_value(struct task_struct *task,
80 struct pt_regs *regs,
81 int error, long val)
82{
83 regs->ax = (long) error ?: val;
84}
85
86#ifdef CONFIG_X86_32
87
88static inline void syscall_get_arguments(struct task_struct *task,
89 struct pt_regs *regs,
90 unsigned int i, unsigned int n,
91 unsigned long *args)
92{
93 BUG_ON(i + n > 6);
94 memcpy(args, &regs->bx + i, n * sizeof(args[0]));
95}
96
97static inline void syscall_set_arguments(struct task_struct *task,
98 struct pt_regs *regs,
99 unsigned int i, unsigned int n,
100 const unsigned long *args)
101{
102 BUG_ON(i + n > 6);
103 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
104}
105
Eric Paris5e937a92014-03-11 12:48:43 -0400106static inline int syscall_get_arch(void)
Will Drewryb7456532012-04-12 16:47:56 -0500107{
108 return AUDIT_ARCH_I386;
109}
110
Roland McGrath68bd0f42008-04-18 17:08:44 -0700111#else /* CONFIG_X86_64 */
112
113static inline void syscall_get_arguments(struct task_struct *task,
114 struct pt_regs *regs,
115 unsigned int i, unsigned int n,
116 unsigned long *args)
117{
118# ifdef CONFIG_IA32_EMULATION
119 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700120 switch (i) {
121 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700122 if (!n--) break;
123 *args++ = regs->bx;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700124 case 1:
125 if (!n--) break;
126 *args++ = regs->cx;
127 case 2:
128 if (!n--) break;
129 *args++ = regs->dx;
130 case 3:
131 if (!n--) break;
132 *args++ = regs->si;
133 case 4:
134 if (!n--) break;
135 *args++ = regs->di;
136 case 5:
137 if (!n--) break;
138 *args++ = regs->bp;
139 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700140 if (!n--) break;
141 default:
142 BUG();
143 break;
144 }
145 else
146# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700147 switch (i) {
148 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700149 if (!n--) break;
150 *args++ = regs->di;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700151 case 1:
152 if (!n--) break;
153 *args++ = regs->si;
154 case 2:
155 if (!n--) break;
156 *args++ = regs->dx;
157 case 3:
158 if (!n--) break;
159 *args++ = regs->r10;
160 case 4:
161 if (!n--) break;
162 *args++ = regs->r8;
163 case 5:
164 if (!n--) break;
165 *args++ = regs->r9;
166 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700167 if (!n--) break;
168 default:
169 BUG();
170 break;
171 }
172}
173
174static inline void syscall_set_arguments(struct task_struct *task,
175 struct pt_regs *regs,
176 unsigned int i, unsigned int n,
177 const unsigned long *args)
178{
179# ifdef CONFIG_IA32_EMULATION
180 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700181 switch (i) {
182 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700183 if (!n--) break;
184 regs->bx = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700185 case 1:
186 if (!n--) break;
187 regs->cx = *args++;
188 case 2:
189 if (!n--) break;
190 regs->dx = *args++;
191 case 3:
192 if (!n--) break;
193 regs->si = *args++;
194 case 4:
195 if (!n--) break;
196 regs->di = *args++;
197 case 5:
198 if (!n--) break;
199 regs->bp = *args++;
200 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700201 if (!n--) break;
202 default:
203 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700204 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700205 }
206 else
207# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700208 switch (i) {
209 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700210 if (!n--) break;
211 regs->di = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700212 case 1:
213 if (!n--) break;
214 regs->si = *args++;
215 case 2:
216 if (!n--) break;
217 regs->dx = *args++;
218 case 3:
219 if (!n--) break;
220 regs->r10 = *args++;
221 case 4:
222 if (!n--) break;
223 regs->r8 = *args++;
224 case 5:
225 if (!n--) break;
226 regs->r9 = *args++;
227 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700228 if (!n--) break;
229 default:
230 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700231 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700232 }
233}
234
Eric Paris5e937a92014-03-11 12:48:43 -0400235static inline int syscall_get_arch(void)
Will Drewryb7456532012-04-12 16:47:56 -0500236{
237#ifdef CONFIG_IA32_EMULATION
238 /*
239 * TS_COMPAT is set for 32-bit syscall entry and then
240 * remains set until we return to user mode.
241 *
242 * TIF_IA32 tasks should always have TS_COMPAT set at
243 * system call time.
244 *
245 * x32 tasks should be considered AUDIT_ARCH_X86_64.
246 */
Eric Paris5e937a92014-03-11 12:48:43 -0400247 if (task_thread_info(current)->status & TS_COMPAT)
Will Drewryb7456532012-04-12 16:47:56 -0500248 return AUDIT_ARCH_I386;
249#endif
250 /* Both x32 and x86_64 are considered "64-bit". */
251 return AUDIT_ARCH_X86_64;
252}
Roland McGrath68bd0f42008-04-18 17:08:44 -0700253#endif /* CONFIG_X86_32 */
254
H. Peter Anvin5e1b0072008-10-23 00:20:33 -0700255#endif /* _ASM_X86_SYSCALL_H */