blob: 1ace47b62592adabd69c568d1f842ffa33f057e2 [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
Will Drewryb7456532012-04-12 16:47:56 -050016#include <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
Mike Frysingere7b8e672010-01-26 04:40:03 -050023extern const unsigned long sys_call_table[];
24
Roland McGrath18c1e2c2009-09-22 19:57:51 -070025/*
26 * Only the low 32 bits of orig_ax are meaningful, so we return int.
27 * This importantly ignores the high bits on 64-bit, so comparisons
28 * sign-extend the low 32 bits.
29 */
30static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
Roland McGrath68bd0f42008-04-18 17:08:44 -070031{
H. Peter Anvinfca460f2012-02-19 07:56:26 -080032 return regs->orig_ax & __SYSCALL_MASK;
Roland McGrath68bd0f42008-04-18 17:08:44 -070033}
34
35static inline void syscall_rollback(struct task_struct *task,
36 struct pt_regs *regs)
37{
H. Peter Anvinfca460f2012-02-19 07:56:26 -080038 regs->ax = regs->orig_ax & __SYSCALL_MASK;
Roland McGrath68bd0f42008-04-18 17:08:44 -070039}
40
41static inline long syscall_get_error(struct task_struct *task,
42 struct pt_regs *regs)
43{
44 unsigned long error = regs->ax;
45#ifdef CONFIG_IA32_EMULATION
46 /*
47 * TS_COMPAT is set for 32-bit syscall entries and then
48 * remains set until we return to user mode.
49 */
50 if (task_thread_info(task)->status & TS_COMPAT)
51 /*
52 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
53 * and will match correctly in comparisons.
54 */
55 error = (long) (int) error;
56#endif
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020057 return IS_ERR_VALUE(error) ? error : 0;
Roland McGrath68bd0f42008-04-18 17:08:44 -070058}
59
60static inline long syscall_get_return_value(struct task_struct *task,
61 struct pt_regs *regs)
62{
63 return regs->ax;
64}
65
66static inline void syscall_set_return_value(struct task_struct *task,
67 struct pt_regs *regs,
68 int error, long val)
69{
70 regs->ax = (long) error ?: val;
71}
72
73#ifdef CONFIG_X86_32
74
75static inline void syscall_get_arguments(struct task_struct *task,
76 struct pt_regs *regs,
77 unsigned int i, unsigned int n,
78 unsigned long *args)
79{
80 BUG_ON(i + n > 6);
81 memcpy(args, &regs->bx + i, n * sizeof(args[0]));
82}
83
84static inline void syscall_set_arguments(struct task_struct *task,
85 struct pt_regs *regs,
86 unsigned int i, unsigned int n,
87 const unsigned long *args)
88{
89 BUG_ON(i + n > 6);
90 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
91}
92
Will Drewryb7456532012-04-12 16:47:56 -050093static inline int syscall_get_arch(struct task_struct *task,
94 struct pt_regs *regs)
95{
96 return AUDIT_ARCH_I386;
97}
98
Roland McGrath68bd0f42008-04-18 17:08:44 -070099#else /* CONFIG_X86_64 */
100
101static inline void syscall_get_arguments(struct task_struct *task,
102 struct pt_regs *regs,
103 unsigned int i, unsigned int n,
104 unsigned long *args)
105{
106# ifdef CONFIG_IA32_EMULATION
107 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700108 switch (i) {
109 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700110 if (!n--) break;
111 *args++ = regs->bx;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700112 case 1:
113 if (!n--) break;
114 *args++ = regs->cx;
115 case 2:
116 if (!n--) break;
117 *args++ = regs->dx;
118 case 3:
119 if (!n--) break;
120 *args++ = regs->si;
121 case 4:
122 if (!n--) break;
123 *args++ = regs->di;
124 case 5:
125 if (!n--) break;
126 *args++ = regs->bp;
127 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700128 if (!n--) break;
129 default:
130 BUG();
131 break;
132 }
133 else
134# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700135 switch (i) {
136 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700137 if (!n--) break;
138 *args++ = regs->di;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700139 case 1:
140 if (!n--) break;
141 *args++ = regs->si;
142 case 2:
143 if (!n--) break;
144 *args++ = regs->dx;
145 case 3:
146 if (!n--) break;
147 *args++ = regs->r10;
148 case 4:
149 if (!n--) break;
150 *args++ = regs->r8;
151 case 5:
152 if (!n--) break;
153 *args++ = regs->r9;
154 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700155 if (!n--) break;
156 default:
157 BUG();
158 break;
159 }
160}
161
162static inline void syscall_set_arguments(struct task_struct *task,
163 struct pt_regs *regs,
164 unsigned int i, unsigned int n,
165 const unsigned long *args)
166{
167# ifdef CONFIG_IA32_EMULATION
168 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700169 switch (i) {
170 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700171 if (!n--) break;
172 regs->bx = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700173 case 1:
174 if (!n--) break;
175 regs->cx = *args++;
176 case 2:
177 if (!n--) break;
178 regs->dx = *args++;
179 case 3:
180 if (!n--) break;
181 regs->si = *args++;
182 case 4:
183 if (!n--) break;
184 regs->di = *args++;
185 case 5:
186 if (!n--) break;
187 regs->bp = *args++;
188 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700189 if (!n--) break;
190 default:
191 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700192 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700193 }
194 else
195# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700196 switch (i) {
197 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700198 if (!n--) break;
199 regs->di = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700200 case 1:
201 if (!n--) break;
202 regs->si = *args++;
203 case 2:
204 if (!n--) break;
205 regs->dx = *args++;
206 case 3:
207 if (!n--) break;
208 regs->r10 = *args++;
209 case 4:
210 if (!n--) break;
211 regs->r8 = *args++;
212 case 5:
213 if (!n--) break;
214 regs->r9 = *args++;
215 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700216 if (!n--) break;
217 default:
218 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700219 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700220 }
221}
222
Will Drewryb7456532012-04-12 16:47:56 -0500223static inline int syscall_get_arch(struct task_struct *task,
224 struct pt_regs *regs)
225{
226#ifdef CONFIG_IA32_EMULATION
227 /*
228 * TS_COMPAT is set for 32-bit syscall entry and then
229 * remains set until we return to user mode.
230 *
231 * TIF_IA32 tasks should always have TS_COMPAT set at
232 * system call time.
233 *
234 * x32 tasks should be considered AUDIT_ARCH_X86_64.
235 */
236 if (task_thread_info(task)->status & TS_COMPAT)
237 return AUDIT_ARCH_I386;
238#endif
239 /* Both x32 and x86_64 are considered "64-bit". */
240 return AUDIT_ARCH_X86_64;
241}
Roland McGrath68bd0f42008-04-18 17:08:44 -0700242#endif /* CONFIG_X86_32 */
243
H. Peter Anvin5e1b0072008-10-23 00:20:33 -0700244#endif /* _ASM_X86_SYSCALL_H */