blob: 386b78686c4d9e9d096e9dbf215541003eec0837 [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
16#include <linux/sched.h>
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020017#include <linux/err.h>
H. Peter Anvin72142fd2012-01-07 14:10:18 -080018#include <asm/asm-offsets.h> /* For NR_syscalls */
H. Peter Anvinfca460f2012-02-19 07:56:26 -080019#include <asm/unistd.h>
Roland McGrath68bd0f42008-04-18 17:08:44 -070020
Mike Frysingere7b8e672010-01-26 04:40:03 -050021extern const unsigned long sys_call_table[];
22
Roland McGrath18c1e2c2009-09-22 19:57:51 -070023/*
24 * Only the low 32 bits of orig_ax are meaningful, so we return int.
25 * This importantly ignores the high bits on 64-bit, so comparisons
26 * sign-extend the low 32 bits.
27 */
28static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
Roland McGrath68bd0f42008-04-18 17:08:44 -070029{
H. Peter Anvinfca460f2012-02-19 07:56:26 -080030 return regs->orig_ax & __SYSCALL_MASK;
Roland McGrath68bd0f42008-04-18 17:08:44 -070031}
32
33static inline void syscall_rollback(struct task_struct *task,
34 struct pt_regs *regs)
35{
H. Peter Anvinfca460f2012-02-19 07:56:26 -080036 regs->ax = regs->orig_ax & __SYSCALL_MASK;
Roland McGrath68bd0f42008-04-18 17:08:44 -070037}
38
39static inline long syscall_get_error(struct task_struct *task,
40 struct pt_regs *regs)
41{
42 unsigned long error = regs->ax;
43#ifdef CONFIG_IA32_EMULATION
44 /*
45 * TS_COMPAT is set for 32-bit syscall entries and then
46 * remains set until we return to user mode.
47 */
48 if (task_thread_info(task)->status & TS_COMPAT)
49 /*
50 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
51 * and will match correctly in comparisons.
52 */
53 error = (long) (int) error;
54#endif
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020055 return IS_ERR_VALUE(error) ? error : 0;
Roland McGrath68bd0f42008-04-18 17:08:44 -070056}
57
58static inline long syscall_get_return_value(struct task_struct *task,
59 struct pt_regs *regs)
60{
61 return regs->ax;
62}
63
64static inline void syscall_set_return_value(struct task_struct *task,
65 struct pt_regs *regs,
66 int error, long val)
67{
68 regs->ax = (long) error ?: val;
69}
70
71#ifdef CONFIG_X86_32
72
73static inline void syscall_get_arguments(struct task_struct *task,
74 struct pt_regs *regs,
75 unsigned int i, unsigned int n,
76 unsigned long *args)
77{
78 BUG_ON(i + n > 6);
79 memcpy(args, &regs->bx + i, n * sizeof(args[0]));
80}
81
82static inline void syscall_set_arguments(struct task_struct *task,
83 struct pt_regs *regs,
84 unsigned int i, unsigned int n,
85 const unsigned long *args)
86{
87 BUG_ON(i + n > 6);
88 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
89}
90
91#else /* CONFIG_X86_64 */
92
93static inline void syscall_get_arguments(struct task_struct *task,
94 struct pt_regs *regs,
95 unsigned int i, unsigned int n,
96 unsigned long *args)
97{
98# ifdef CONFIG_IA32_EMULATION
99 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700100 switch (i) {
101 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700102 if (!n--) break;
103 *args++ = regs->bx;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700104 case 1:
105 if (!n--) break;
106 *args++ = regs->cx;
107 case 2:
108 if (!n--) break;
109 *args++ = regs->dx;
110 case 3:
111 if (!n--) break;
112 *args++ = regs->si;
113 case 4:
114 if (!n--) break;
115 *args++ = regs->di;
116 case 5:
117 if (!n--) break;
118 *args++ = regs->bp;
119 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700120 if (!n--) break;
121 default:
122 BUG();
123 break;
124 }
125 else
126# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700127 switch (i) {
128 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700129 if (!n--) break;
130 *args++ = regs->di;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700131 case 1:
132 if (!n--) break;
133 *args++ = regs->si;
134 case 2:
135 if (!n--) break;
136 *args++ = regs->dx;
137 case 3:
138 if (!n--) break;
139 *args++ = regs->r10;
140 case 4:
141 if (!n--) break;
142 *args++ = regs->r8;
143 case 5:
144 if (!n--) break;
145 *args++ = regs->r9;
146 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700147 if (!n--) break;
148 default:
149 BUG();
150 break;
151 }
152}
153
154static inline void syscall_set_arguments(struct task_struct *task,
155 struct pt_regs *regs,
156 unsigned int i, unsigned int n,
157 const unsigned long *args)
158{
159# ifdef CONFIG_IA32_EMULATION
160 if (task_thread_info(task)->status & TS_COMPAT)
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700161 switch (i) {
162 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700163 if (!n--) break;
164 regs->bx = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700165 case 1:
166 if (!n--) break;
167 regs->cx = *args++;
168 case 2:
169 if (!n--) break;
170 regs->dx = *args++;
171 case 3:
172 if (!n--) break;
173 regs->si = *args++;
174 case 4:
175 if (!n--) break;
176 regs->di = *args++;
177 case 5:
178 if (!n--) break;
179 regs->bp = *args++;
180 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700181 if (!n--) break;
182 default:
183 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700184 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700185 }
186 else
187# endif
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700188 switch (i) {
189 case 0:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700190 if (!n--) break;
191 regs->di = *args++;
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700192 case 1:
193 if (!n--) break;
194 regs->si = *args++;
195 case 2:
196 if (!n--) break;
197 regs->dx = *args++;
198 case 3:
199 if (!n--) break;
200 regs->r10 = *args++;
201 case 4:
202 if (!n--) break;
203 regs->r8 = *args++;
204 case 5:
205 if (!n--) break;
206 regs->r9 = *args++;
207 case 6:
Roland McGrath68bd0f42008-04-18 17:08:44 -0700208 if (!n--) break;
209 default:
210 BUG();
Linus Torvaldsc3c98972008-10-23 12:38:39 -0700211 break;
Roland McGrath68bd0f42008-04-18 17:08:44 -0700212 }
213}
214
215#endif /* CONFIG_X86_32 */
216
H. Peter Anvin5e1b0072008-10-23 00:20:33 -0700217#endif /* _ASM_X86_SYSCALL_H */