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