blob: aea284b413122f14ccbf757fd5b114d6b12bb094 [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
Andi Kleen1599e8f2013-08-05 15:02:35 -070023typedef void (*sys_call_ptr_t)(void);
24extern const sys_call_ptr_t sys_call_table[];
Mike Frysingere7b8e672010-01-26 04:40:03 -050025
Roland McGrath18c1e2c2009-09-22 19:57:51 -070026/*
27 * Only the low 32 bits of orig_ax are meaningful, so we return int.
28 * This importantly ignores the high bits on 64-bit, so comparisons
29 * sign-extend the low 32 bits.
30 */
31static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
Roland McGrath68bd0f42008-04-18 17:08:44 -070032{
Paul Moore8b4b9f22013-02-15 12:21:43 -050033 return regs->orig_ax;
Roland McGrath68bd0f42008-04-18 17:08:44 -070034}
35
36static inline void syscall_rollback(struct task_struct *task,
37 struct pt_regs *regs)
38{
Paul Moore8b4b9f22013-02-15 12:21:43 -050039 regs->ax = regs->orig_ax;
Roland McGrath68bd0f42008-04-18 17:08:44 -070040}
41
42static inline long syscall_get_error(struct task_struct *task,
43 struct pt_regs *regs)
44{
45 unsigned long error = regs->ax;
46#ifdef CONFIG_IA32_EMULATION
47 /*
48 * TS_COMPAT is set for 32-bit syscall entries and then
49 * remains set until we return to user mode.
50 */
51 if (task_thread_info(task)->status & TS_COMPAT)
52 /*
53 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
54 * and will match correctly in comparisons.
55 */
56 error = (long) (int) error;
57#endif
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020058 return IS_ERR_VALUE(error) ? error : 0;
Roland McGrath68bd0f42008-04-18 17:08:44 -070059}
60
61static inline long syscall_get_return_value(struct task_struct *task,
62 struct pt_regs *regs)
63{
64 return regs->ax;
65}
66
67static inline void syscall_set_return_value(struct task_struct *task,
68 struct pt_regs *regs,
69 int error, long val)
70{
71 regs->ax = (long) error ?: val;
72}
73
74#ifdef CONFIG_X86_32
75
76static inline void syscall_get_arguments(struct task_struct *task,
77 struct pt_regs *regs,
78 unsigned int i, unsigned int n,
79 unsigned long *args)
80{
81 BUG_ON(i + n > 6);
82 memcpy(args, &regs->bx + i, n * sizeof(args[0]));
83}
84
85static inline void syscall_set_arguments(struct task_struct *task,
86 struct pt_regs *regs,
87 unsigned int i, unsigned int n,
88 const unsigned long *args)
89{
90 BUG_ON(i + n > 6);
91 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
92}
93
Will Drewryb7456532012-04-12 16:47:56 -050094static inline int syscall_get_arch(struct task_struct *task,
95 struct pt_regs *regs)
96{
97 return AUDIT_ARCH_I386;
98}
99
Roland McGrath68bd0f42008-04-18 17:08:44 -0700100#else /* CONFIG_X86_64 */
101
102static inline void syscall_get_arguments(struct task_struct *task,
103 struct pt_regs *regs,
104 unsigned int i, unsigned int n,
105 unsigned long *args)
106{
107# ifdef CONFIG_IA32_EMULATION
108 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700109 switch (i) {
110 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700111 if (!n--) break;
112 *args++ = regs->bx;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700113 case 1:
114 if (!n--) break;
115 *args++ = regs->cx;
116 case 2:
117 if (!n--) break;
118 *args++ = regs->dx;
119 case 3:
120 if (!n--) break;
121 *args++ = regs->si;
122 case 4:
123 if (!n--) break;
124 *args++ = regs->di;
125 case 5:
126 if (!n--) break;
127 *args++ = regs->bp;
128 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700129 if (!n--) break;
130 default:
131 BUG();
132 break;
133 }
134 else
135# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700136 switch (i) {
137 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700138 if (!n--) break;
139 *args++ = regs->di;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700140 case 1:
141 if (!n--) break;
142 *args++ = regs->si;
143 case 2:
144 if (!n--) break;
145 *args++ = regs->dx;
146 case 3:
147 if (!n--) break;
148 *args++ = regs->r10;
149 case 4:
150 if (!n--) break;
151 *args++ = regs->r8;
152 case 5:
153 if (!n--) break;
154 *args++ = regs->r9;
155 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700156 if (!n--) break;
157 default:
158 BUG();
159 break;
160 }
161}
162
163static inline void syscall_set_arguments(struct task_struct *task,
164 struct pt_regs *regs,
165 unsigned int i, unsigned int n,
166 const unsigned long *args)
167{
168# ifdef CONFIG_IA32_EMULATION
169 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700170 switch (i) {
171 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700172 if (!n--) break;
173 regs->bx = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700174 case 1:
175 if (!n--) break;
176 regs->cx = *args++;
177 case 2:
178 if (!n--) break;
179 regs->dx = *args++;
180 case 3:
181 if (!n--) break;
182 regs->si = *args++;
183 case 4:
184 if (!n--) break;
185 regs->di = *args++;
186 case 5:
187 if (!n--) break;
188 regs->bp = *args++;
189 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700190 if (!n--) break;
191 default:
192 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700193 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700194 }
195 else
196# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700197 switch (i) {
198 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700199 if (!n--) break;
200 regs->di = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700201 case 1:
202 if (!n--) break;
203 regs->si = *args++;
204 case 2:
205 if (!n--) break;
206 regs->dx = *args++;
207 case 3:
208 if (!n--) break;
209 regs->r10 = *args++;
210 case 4:
211 if (!n--) break;
212 regs->r8 = *args++;
213 case 5:
214 if (!n--) break;
215 regs->r9 = *args++;
216 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700217 if (!n--) break;
218 default:
219 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700220 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700221 }
222}
223
Will Drewryb7456532012-04-12 16:47:56 -0500224static inline int syscall_get_arch(struct task_struct *task,
225 struct pt_regs *regs)
226{
227#ifdef CONFIG_IA32_EMULATION
228 /*
229 * TS_COMPAT is set for 32-bit syscall entry and then
230 * remains set until we return to user mode.
231 *
232 * TIF_IA32 tasks should always have TS_COMPAT set at
233 * system call time.
234 *
235 * x32 tasks should be considered AUDIT_ARCH_X86_64.
236 */
237 if (task_thread_info(task)->status & TS_COMPAT)
238 return AUDIT_ARCH_I386;
239#endif
240 /* Both x32 and x86_64 are considered "64-bit". */
241 return AUDIT_ARCH_X86_64;
242}
Roland McGrath68bd0f42008-04-18 17:08:44 -0700243#endif /* CONFIG_X86_32 */
244
H. Peter Anvin5e1b0072008-10-23 00:20:33 -0700245#endif /* _ASM_X86_SYSCALL_H */