linux_ipccall.c revision 1.31.2.1 1 /* $NetBSD: linux_ipccall.c,v 1.31.2.1 2008/05/10 23:48:56 wrstuden Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_ipccall.c,v 1.31.2.1 2008/05/10 23:48:56 wrstuden Exp $");
34
35 #if defined(_KERNEL_OPT)
36 #include "opt_sysv.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/shm.h>
41 #include <sys/sem.h>
42 #include <sys/msg.h>
43 #include <sys/proc.h>
44 #include <sys/systm.h>
45
46 /* real syscalls */
47 #include <sys/mount.h>
48 #include <sys/sa.h>
49 #include <sys/syscallargs.h>
50
51 /* sys_ipc + args prototype */
52 #include <compat/linux/common/linux_types.h>
53 #include <compat/linux/common/linux_signal.h>
54
55 #include <compat/linux/linux_syscallargs.h>
56 #include <compat/linux/linux_syscall.h>
57
58 /* general ipc defines */
59 #include <compat/linux/common/linux_ipc.h>
60
61 /* prototypes for real/normal linux-emul syscalls */
62 #include <compat/linux/common/linux_msg.h>
63 #include <compat/linux/common/linux_shm.h>
64 #include <compat/linux/common/linux_sem.h>
65
66 /* prototypes for sys_ipc stuff */
67 #include <compat/linux/common/linux_ipccall.h>
68
69 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
70 /* Not used on: alpha */
71
72 /*
73 * Stuff to deal with the SysV ipc/shm/semaphore interface in Linux.
74 * The main difference is, that Linux handles it all via one
75 * system call, which has the usual maximum amount of 5 arguments.
76 * This results in a kludge for calls that take 6 of them.
77 *
78 * The SYSV??? options have to be enabled to get the appropriate
79 * functions to work.
80 */
81
82 int
83 linux_sys_ipc(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
84 {
85 /* {
86 syscallarg(int) what;
87 syscallarg(int) a1;
88 syscallarg(int) a2;
89 syscallarg(int) a3;
90 syscallarg(void *) ptr;
91 } */
92
93 switch (SCARG(uap, what)) {
94 #ifdef SYSVSEM
95 case LINUX_SYS_semop:
96 return linux_semop(l, uap, retval);
97 case LINUX_SYS_semget:
98 return linux_semget(l, uap, retval);
99 case LINUX_SYS_semctl: {
100 struct linux_sys_semctl_args bsa;
101 union linux_semun arg;
102 int error;
103
104 SCARG(&bsa, semid) = SCARG(uap, a1);
105 SCARG(&bsa, semnum) = SCARG(uap, a2);
106 SCARG(&bsa, cmd) = SCARG(uap, a3);
107 /* Convert from (union linux_semun *) to (union linux_semun) */
108 if ((error = copyin(SCARG(uap, ptr), &arg, sizeof arg)))
109 return error;
110 SCARG(&bsa, arg) = arg;
111
112 return linux_sys_semctl(l, &bsa, retval);
113 }
114 #endif
115 #ifdef SYSVMSG
116 case LINUX_SYS_msgsnd:
117 return linux_msgsnd(l, uap, retval);
118 case LINUX_SYS_msgrcv:
119 return linux_msgrcv(l, uap, retval);
120 case LINUX_SYS_msgget:
121 return linux_msgget(l, uap, retval);
122 case LINUX_SYS_msgctl: {
123 struct linux_sys_msgctl_args bsa;
124
125 SCARG(&bsa, msqid) = SCARG(uap, a1);
126 SCARG(&bsa, cmd) = SCARG(uap, a2);
127 SCARG(&bsa, buf) = (struct linux_msqid_ds *)SCARG(uap, ptr);
128
129 return linux_sys_msgctl(l, &bsa, retval);
130 }
131 #endif
132 #ifdef SYSVSHM
133 case LINUX_SYS_shmat: {
134 struct linux_sys_shmat_args bsa;
135
136 SCARG(&bsa, shmid) = SCARG(uap, a1);
137 SCARG(&bsa, shmaddr) = (void *)SCARG(uap, ptr);
138 SCARG(&bsa, shmflg) = SCARG(uap, a2);
139 /* XXX passing pointer inside int here */
140 SCARG(&bsa, raddr) = (u_long *)SCARG(uap, a3);
141
142 return linux_sys_shmat(l, &bsa, retval);
143 }
144 case LINUX_SYS_shmdt:
145 return linux_shmdt(l, uap, retval);
146 case LINUX_SYS_shmget:
147 return linux_shmget(l, uap, retval);
148 case LINUX_SYS_shmctl: {
149 struct linux_sys_shmctl_args bsa;
150
151 SCARG(&bsa, shmid) = SCARG(uap, a1);
152 SCARG(&bsa, cmd) = SCARG(uap, a2);
153 SCARG(&bsa, buf) = (struct linux_shmid_ds *)SCARG(uap, ptr);
154
155 return linux_sys_shmctl(l, &bsa, retval);
156 }
157 #endif
158 default:
159 return ENOSYS;
160 }
161 }
162
163 #ifdef SYSVSEM
164 inline int
165 linux_semop(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
166 {
167 /* {
168 syscallarg(int) what;
169 syscallarg(int) a1;
170 syscallarg(int) a2;
171 syscallarg(int) a3;
172 syscallarg(void *) ptr;
173 } */
174 struct sys_semop_args bsa;
175
176 SCARG(&bsa, semid) = SCARG(uap, a1);
177 SCARG(&bsa, sops) = (struct sembuf *)SCARG(uap, ptr);
178 SCARG(&bsa, nsops) = SCARG(uap, a2);
179
180 return sys_semop(l, &bsa, retval);
181 }
182
183 inline int
184 linux_semget(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
185 {
186 /* {
187 syscallarg(int) what;
188 syscallarg(int) a1;
189 syscallarg(int) a2;
190 syscallarg(int) a3;
191 syscallarg(void *) ptr;
192 } */
193 struct sys_semget_args bsa;
194
195 SCARG(&bsa, key) = (key_t)SCARG(uap, a1);
196 SCARG(&bsa, nsems) = SCARG(uap, a2);
197 SCARG(&bsa, semflg) = SCARG(uap, a3);
198
199 return sys_semget(l, &bsa, retval);
200 }
201
202 #endif /* SYSVSEM */
203
204 #ifdef SYSVMSG
205
206 inline int
207 linux_msgsnd(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
208 {
209 struct sys_msgsnd_args bma;
210
211 SCARG(&bma, msqid) = SCARG(uap, a1);
212 SCARG(&bma, msgp) = SCARG(uap, ptr);
213 SCARG(&bma, msgsz) = SCARG(uap, a2);
214 SCARG(&bma, msgflg) = SCARG(uap, a3);
215
216 return sys_msgsnd(l, &bma, retval);
217 }
218
219 inline int
220 linux_msgrcv(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
221 {
222 struct sys_msgrcv_args bma;
223 struct linux_msgrcv_msgarg kluge;
224 int error;
225
226 if ((error = copyin(SCARG(uap, ptr), &kluge, sizeof kluge)))
227 return error;
228
229 SCARG(&bma, msqid) = SCARG(uap, a1);
230 SCARG(&bma, msgp) = kluge.msg;
231 SCARG(&bma, msgsz) = SCARG(uap, a2);
232 SCARG(&bma, msgtyp) = kluge.type;
233 SCARG(&bma, msgflg) = SCARG(uap, a3);
234
235 return sys_msgrcv(l, &bma, retval);
236 }
237
238 inline int
239 linux_msgget(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
240 {
241 struct sys_msgget_args bma;
242
243 SCARG(&bma, key) = (key_t)SCARG(uap, a1);
244 SCARG(&bma, msgflg) = SCARG(uap, a2);
245
246 return sys_msgget(l, &bma, retval);
247 }
248
249 #endif /* SYSVMSG */
250
251 #ifdef SYSVSHM
252 /*
253 * shmdt(): this could have been mapped directly, if it wasn't for
254 * the extra indirection by the linux_ipc system call.
255 */
256 inline int
257 linux_shmdt(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
258 {
259 struct sys_shmdt_args bsa;
260
261 SCARG(&bsa, shmaddr) = SCARG(uap, ptr);
262
263 return sys_shmdt(l, &bsa, retval);
264 }
265
266 /*
267 * Same story as shmdt.
268 */
269 inline int
270 linux_shmget(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval)
271 {
272 struct linux_sys_shmget_args bsa;
273
274 SCARG(&bsa, key) = SCARG(uap, a1);
275 SCARG(&bsa, size) = SCARG(uap, a2);
276 SCARG(&bsa, shmflg) = SCARG(uap, a3);
277
278 return linux_sys_shmget(l, &bsa, retval);
279 }
280
281 #endif /* SYSVSHM */
282