xref: /freebsd/sys/compat/linux/linux_ipc.c (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/limits.h>
31 #include <sys/msg.h>
32 #include <sys/proc.h>
33 #include <sys/sem.h>
34 #include <sys/shm.h>
35 #include <sys/stat.h>
36 #include <sys/syscallsubr.h>
37 #include <sys/sysent.h>
38 #include <sys/sysproto.h>
39 
40 #ifdef COMPAT_LINUX32
41 #include <machine/../linux32/linux.h>
42 #include <machine/../linux32/linux32_proto.h>
43 #else
44 #include <machine/../linux/linux.h>
45 #include <machine/../linux/linux_proto.h>
46 #endif
47 #include <compat/linux/linux_ipc.h>
48 #include <compat/linux/linux_ipc64.h>
49 #include <compat/linux/linux_time.h>
50 #include <compat/linux/linux_util.h>
51 
52 /*
53  * old, pre 2.4 kernel
54  */
55 struct l_ipc_perm {
56 	l_key_t		key;
57 	l_uid16_t	uid;
58 	l_gid16_t	gid;
59 	l_uid16_t	cuid;
60 	l_gid16_t	cgid;
61 	l_ushort	mode;
62 	l_ushort	seq;
63 };
64 
65 struct l_seminfo {
66 	l_int semmap;
67 	l_int semmni;
68 	l_int semmns;
69 	l_int semmnu;
70 	l_int semmsl;
71 	l_int semopm;
72 	l_int semume;
73 	l_int semusz;
74 	l_int semvmx;
75 	l_int semaem;
76 };
77 
78 struct l_shminfo {
79 	l_int shmmax;
80 	l_int shmmin;
81 	l_int shmmni;
82 	l_int shmseg;
83 	l_int shmall;
84 };
85 
86 struct l_shm_info {
87 	l_int used_ids;
88 	l_ulong shm_tot;  /* total allocated shm */
89 	l_ulong shm_rss;  /* total resident shm */
90 	l_ulong shm_swp;  /* total swapped shm */
91 	l_ulong swap_attempts;
92 	l_ulong swap_successes;
93 };
94 
95 struct l_msginfo {
96 	l_int msgpool;
97 	l_int msgmap;
98 	l_int msgmax;
99 	l_int msgmnb;
100 	l_int msgmni;
101 	l_int msgssz;
102 	l_int msgtql;
103 	l_ushort msgseg;
104 };
105 
106 static void
107 bsd_to_linux_shminfo( struct shminfo *bpp, struct l_shminfo64 *lpp)
108 {
109 
110 	lpp->shmmax = bpp->shmmax;
111 	lpp->shmmin = bpp->shmmin;
112 	lpp->shmmni = bpp->shmmni;
113 	lpp->shmseg = bpp->shmseg;
114 	lpp->shmall = bpp->shmall;
115 }
116 
117 static void
118 bsd_to_linux_shm_info( struct shm_info *bpp, struct l_shm_info *lpp)
119 {
120 
121 	lpp->used_ids = bpp->used_ids;
122 	lpp->shm_tot = bpp->shm_tot;
123 	lpp->shm_rss = bpp->shm_rss;
124 	lpp->shm_swp = bpp->shm_swp;
125 	lpp->swap_attempts = bpp->swap_attempts;
126 	lpp->swap_successes = bpp->swap_successes;
127 }
128 
129 static void
130 linux_to_bsd_ipc_perm(struct l_ipc64_perm *lpp, struct ipc_perm *bpp)
131 {
132 
133 	bpp->key = lpp->key;
134 	bpp->uid = lpp->uid;
135 	bpp->gid = lpp->gid;
136 	bpp->cuid = lpp->cuid;
137 	bpp->cgid = lpp->cgid;
138 	bpp->mode = lpp->mode;
139 	bpp->seq = lpp->seq;
140 }
141 
142 static void
143 bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct l_ipc64_perm *lpp)
144 {
145 
146 	lpp->key = bpp->key;
147 	lpp->uid = bpp->uid;
148 	lpp->gid = bpp->gid;
149 	lpp->cuid = bpp->cuid;
150 	lpp->cgid = bpp->cgid;
151 	lpp->mode = bpp->mode & (S_IRWXU|S_IRWXG|S_IRWXO);
152 	lpp->seq = bpp->seq;
153 }
154 
155 struct l_msqid_ds {
156 	struct l_ipc_perm	msg_perm;
157 	l_uintptr_t		msg_first;	/* first message on queue,unused */
158 	l_uintptr_t		msg_last;	/* last message in queue,unused */
159 	l_time_t		msg_stime;	/* last msgsnd time */
160 	l_time_t		msg_rtime;	/* last msgrcv time */
161 	l_time_t		msg_ctime;	/* last change time */
162 	l_ulong			msg_lcbytes;	/* Reuse junk fields for 32 bit */
163 	l_ulong			msg_lqbytes;	/* ditto */
164 	l_ushort		msg_cbytes;	/* current number of bytes on queue */
165 	l_ushort		msg_qnum;	/* number of messages in queue */
166 	l_ushort		msg_qbytes;	/* max number of bytes on queue */
167 	l_pid_t			msg_lspid;	/* pid of last msgsnd */
168 	l_pid_t			msg_lrpid;	/* last receive pid */
169 };
170 
171 struct l_semid_ds {
172 	struct l_ipc_perm	sem_perm;
173 	l_time_t		sem_otime;
174 	l_time_t		sem_ctime;
175 	l_uintptr_t		sem_base;
176 	l_uintptr_t		sem_pending;
177 	l_uintptr_t		sem_pending_last;
178 	l_uintptr_t		undo;
179 	l_ushort		sem_nsems;
180 };
181 
182 struct l_shmid_ds {
183 	struct l_ipc_perm	shm_perm;
184 	l_int			shm_segsz;
185 	l_time_t		shm_atime;
186 	l_time_t		shm_dtime;
187 	l_time_t		shm_ctime;
188 	l_ushort		shm_cpid;
189 	l_ushort		shm_lpid;
190 	l_short			shm_nattch;
191 	l_ushort		private1;
192 	l_uintptr_t		private2;
193 	l_uintptr_t		private3;
194 };
195 
196 static void
197 linux_to_bsd_semid_ds(struct l_semid64_ds *lsp, struct semid_ds *bsp)
198 {
199 
200 	linux_to_bsd_ipc_perm(&lsp->sem_perm, &bsp->sem_perm);
201 	bsp->sem_otime = lsp->sem_otime;
202 	bsp->sem_ctime = lsp->sem_ctime;
203 	bsp->sem_nsems = lsp->sem_nsems;
204 }
205 
206 static void
207 bsd_to_linux_semid_ds(struct semid_ds *bsp, struct l_semid64_ds *lsp)
208 {
209 
210 	bsd_to_linux_ipc_perm(&bsp->sem_perm, &lsp->sem_perm);
211 	lsp->sem_otime = bsp->sem_otime;
212 	lsp->sem_ctime = bsp->sem_ctime;
213 	lsp->sem_nsems = bsp->sem_nsems;
214 }
215 
216 static void
217 linux_to_bsd_shmid_ds(struct l_shmid64_ds *lsp, struct shmid_ds *bsp)
218 {
219 
220 	linux_to_bsd_ipc_perm(&lsp->shm_perm, &bsp->shm_perm);
221 	bsp->shm_segsz = lsp->shm_segsz;
222 	bsp->shm_lpid = lsp->shm_lpid;
223 	bsp->shm_cpid = lsp->shm_cpid;
224 	bsp->shm_nattch = lsp->shm_nattch;
225 	bsp->shm_atime = lsp->shm_atime;
226 	bsp->shm_dtime = lsp->shm_dtime;
227 	bsp->shm_ctime = lsp->shm_ctime;
228 }
229 
230 static void
231 bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct l_shmid64_ds *lsp)
232 {
233 
234 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->shm_perm);
235 	lsp->shm_segsz = bsp->shm_segsz;
236 	lsp->shm_lpid = bsp->shm_lpid;
237 	lsp->shm_cpid = bsp->shm_cpid;
238 	lsp->shm_nattch = bsp->shm_nattch;
239 	lsp->shm_atime = bsp->shm_atime;
240 	lsp->shm_dtime = bsp->shm_dtime;
241 	lsp->shm_ctime = bsp->shm_ctime;
242 }
243 
244 static void
245 linux_to_bsd_msqid_ds(struct l_msqid64_ds *lsp, struct msqid_ds *bsp)
246 {
247 
248 	linux_to_bsd_ipc_perm(&lsp->msg_perm, &bsp->msg_perm);
249 	bsp->msg_cbytes = lsp->msg_cbytes;
250 	bsp->msg_qnum = lsp->msg_qnum;
251 	bsp->msg_qbytes = lsp->msg_qbytes;
252 	bsp->msg_lspid = lsp->msg_lspid;
253 	bsp->msg_lrpid = lsp->msg_lrpid;
254 	bsp->msg_stime = lsp->msg_stime;
255 	bsp->msg_rtime = lsp->msg_rtime;
256 	bsp->msg_ctime = lsp->msg_ctime;
257 }
258 
259 static void
260 bsd_to_linux_msqid_ds(struct msqid_ds *bsp, struct l_msqid64_ds *lsp)
261 {
262 
263 	bsd_to_linux_ipc_perm(&bsp->msg_perm, &lsp->msg_perm);
264 	lsp->msg_cbytes = bsp->msg_cbytes;
265 	lsp->msg_qnum = bsp->msg_qnum;
266 	lsp->msg_qbytes = bsp->msg_qbytes;
267 	lsp->msg_lspid = bsp->msg_lspid;
268 	lsp->msg_lrpid = bsp->msg_lrpid;
269 	lsp->msg_stime = bsp->msg_stime;
270 	lsp->msg_rtime = bsp->msg_rtime;
271 	lsp->msg_ctime = bsp->msg_ctime;
272 }
273 
274 static int
275 linux_ipc64_perm_to_ipc_perm(struct l_ipc64_perm *in, struct l_ipc_perm *out)
276 {
277 
278 	out->key = in->key;
279 	out->uid = in->uid;
280 	out->gid = in->gid;
281 	out->cuid = in->cuid;
282 	out->cgid = in->cgid;
283 	out->mode = in->mode;
284 	out->seq = in->seq;
285 
286 	/* Linux does not check overflow */
287 	if (out->uid != in->uid || out->gid != in->gid ||
288 	    out->cuid != in->cuid || out->cgid != in->cgid ||
289 	    out->mode != in->mode)
290 		return (EOVERFLOW);
291 	else
292 		return (0);
293 }
294 
295 static int
296 linux_msqid_pullup(l_int ver, struct l_msqid64_ds *linux_msqid64, caddr_t uaddr)
297 {
298 	struct l_msqid_ds linux_msqid;
299 	int error;
300 
301 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
302 		return (copyin(uaddr, linux_msqid64, sizeof(*linux_msqid64)));
303 
304 	error = copyin(uaddr, &linux_msqid, sizeof(linux_msqid));
305 	if (error != 0)
306 		return (error);
307 
308 	bzero(linux_msqid64, sizeof(*linux_msqid64));
309 	linux_msqid64->msg_perm.uid = linux_msqid.msg_perm.uid;
310 	linux_msqid64->msg_perm.gid = linux_msqid.msg_perm.gid;
311 	linux_msqid64->msg_perm.mode = linux_msqid.msg_perm.mode;
312 	if (linux_msqid.msg_qbytes == 0)
313 		linux_msqid64->msg_qbytes = linux_msqid.msg_lqbytes;
314 	else
315 		linux_msqid64->msg_qbytes = linux_msqid.msg_qbytes;
316 	return (0);
317 }
318 
319 static int
320 linux_msqid_pushdown(l_int ver, struct l_msqid64_ds *linux_msqid64, caddr_t uaddr)
321 {
322 	struct l_msqid_ds linux_msqid;
323 	int error;
324 
325 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
326 		return (copyout(linux_msqid64, uaddr, sizeof(*linux_msqid64)));
327 
328 	bzero(&linux_msqid, sizeof(linux_msqid));
329 	error = linux_ipc64_perm_to_ipc_perm(&linux_msqid64->msg_perm,
330 	    &linux_msqid.msg_perm);
331 	if (error != 0)
332 		return (error);
333 
334 	linux_msqid.msg_stime = linux_msqid64->msg_stime;
335 	linux_msqid.msg_rtime = linux_msqid64->msg_rtime;
336 	linux_msqid.msg_ctime = linux_msqid64->msg_ctime;
337 
338 	if (linux_msqid64->msg_cbytes > USHRT_MAX)
339 		linux_msqid.msg_cbytes = USHRT_MAX;
340 	else
341 		linux_msqid.msg_cbytes = linux_msqid64->msg_cbytes;
342 	linux_msqid.msg_lcbytes = linux_msqid64->msg_cbytes;
343 	if (linux_msqid64->msg_qnum > USHRT_MAX)
344 		linux_msqid.msg_qnum = USHRT_MAX;
345 	else
346 		linux_msqid.msg_qnum = linux_msqid64->msg_qnum;
347 	if (linux_msqid64->msg_qbytes > USHRT_MAX)
348 		linux_msqid.msg_qbytes = USHRT_MAX;
349 	else
350 		linux_msqid.msg_qbytes = linux_msqid64->msg_qbytes;
351 	linux_msqid.msg_lqbytes = linux_msqid64->msg_qbytes;
352 	linux_msqid.msg_lspid = linux_msqid64->msg_lspid;
353 	linux_msqid.msg_lrpid = linux_msqid64->msg_lrpid;
354 
355 	/* Linux does not check overflow */
356 	if (linux_msqid.msg_stime != linux_msqid64->msg_stime ||
357 	    linux_msqid.msg_rtime != linux_msqid64->msg_rtime ||
358 	    linux_msqid.msg_ctime != linux_msqid64->msg_ctime)
359 		return (EOVERFLOW);
360 	return (copyout(&linux_msqid, uaddr, sizeof(linux_msqid)));
361 }
362 
363 static int
364 linux_semid_pullup(l_int ver, struct l_semid64_ds *linux_semid64, caddr_t uaddr)
365 {
366 	struct l_semid_ds linux_semid;
367 	int error;
368 
369 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
370 		return (copyin(uaddr, linux_semid64, sizeof(*linux_semid64)));
371 	error = copyin(uaddr, &linux_semid, sizeof(linux_semid));
372 	if (error != 0)
373 		return (error);
374 
375 	bzero(linux_semid64, sizeof(*linux_semid64));
376 	linux_semid64->sem_perm.uid = linux_semid.sem_perm.uid;
377 	linux_semid64->sem_perm.gid = linux_semid.sem_perm.gid;
378 	linux_semid64->sem_perm.mode = linux_semid.sem_perm.mode;
379 	return (0);
380 }
381 
382 static int
383 linux_semid_pushdown(l_int ver, struct l_semid64_ds *linux_semid64, caddr_t uaddr)
384 {
385 	struct l_semid_ds linux_semid;
386 	int error;
387 
388 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
389 		return (copyout(linux_semid64, uaddr, sizeof(*linux_semid64)));
390 
391 	bzero(&linux_semid, sizeof(linux_semid));
392 		error = linux_ipc64_perm_to_ipc_perm(&linux_semid64->sem_perm,
393 	    &linux_semid.sem_perm);
394 	if (error != 0)
395 		return (error);
396 
397 	linux_semid.sem_otime = linux_semid64->sem_otime;
398 	linux_semid.sem_ctime = linux_semid64->sem_ctime;
399 	linux_semid.sem_nsems = linux_semid64->sem_nsems;
400 
401 	/* Linux does not check overflow */
402 	if (linux_semid.sem_otime != linux_semid64->sem_otime ||
403 	    linux_semid.sem_ctime != linux_semid64->sem_ctime ||
404 	    linux_semid.sem_nsems != linux_semid64->sem_nsems)
405 		return (EOVERFLOW);
406 	return (copyout(&linux_semid, uaddr, sizeof(linux_semid)));
407 }
408 
409 static int
410 linux_shmid_pullup(l_int ver, struct l_shmid64_ds *linux_shmid64, caddr_t uaddr)
411 {
412 	struct l_shmid_ds linux_shmid;
413 	int error;
414 
415 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
416 		return (copyin(uaddr, linux_shmid64, sizeof(*linux_shmid64)));
417 
418 	error = copyin(uaddr, &linux_shmid, sizeof(linux_shmid));
419 	if (error != 0)
420 		return (error);
421 
422 	bzero(linux_shmid64, sizeof(*linux_shmid64));
423 	linux_shmid64->shm_perm.uid = linux_shmid.shm_perm.uid;
424 	linux_shmid64->shm_perm.gid = linux_shmid.shm_perm.gid;
425 	linux_shmid64->shm_perm.mode = linux_shmid.shm_perm.mode;
426 	return (0);
427 }
428 
429 static int
430 linux_shmid_pushdown(l_int ver, struct l_shmid64_ds *linux_shmid64, caddr_t uaddr)
431 {
432 	struct l_shmid_ds linux_shmid;
433 	int error;
434 
435 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
436 		return (copyout(linux_shmid64, uaddr, sizeof(*linux_shmid64)));
437 
438 	bzero(&linux_shmid, sizeof(linux_shmid));
439 	error = linux_ipc64_perm_to_ipc_perm(&linux_shmid64->shm_perm,
440 	    &linux_shmid.shm_perm);
441 	if (error != 0)
442 		return (error);
443 
444 	linux_shmid.shm_segsz = linux_shmid64->shm_segsz;
445 	linux_shmid.shm_atime = linux_shmid64->shm_atime;
446 	linux_shmid.shm_dtime = linux_shmid64->shm_dtime;
447 	linux_shmid.shm_ctime = linux_shmid64->shm_ctime;
448 	linux_shmid.shm_cpid = linux_shmid64->shm_cpid;
449 	linux_shmid.shm_lpid = linux_shmid64->shm_lpid;
450 	linux_shmid.shm_nattch = linux_shmid64->shm_nattch;
451 
452 	/* Linux does not check overflow */
453 	if (linux_shmid.shm_segsz != linux_shmid64->shm_segsz ||
454 	    linux_shmid.shm_atime != linux_shmid64->shm_atime ||
455 	    linux_shmid.shm_dtime != linux_shmid64->shm_dtime ||
456 	    linux_shmid.shm_ctime != linux_shmid64->shm_ctime ||
457 	    linux_shmid.shm_cpid != linux_shmid64->shm_cpid ||
458 	    linux_shmid.shm_lpid != linux_shmid64->shm_lpid ||
459 	    linux_shmid.shm_nattch != linux_shmid64->shm_nattch)
460 		return (EOVERFLOW);
461 	return (copyout(&linux_shmid, uaddr, sizeof(linux_shmid)));
462 }
463 
464 static int
465 linux_shminfo_pushdown(l_int ver, struct l_shminfo64 *linux_shminfo64,
466     caddr_t uaddr)
467 {
468 	struct l_shminfo linux_shminfo;
469 
470 	if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64))
471 		return (copyout(linux_shminfo64, uaddr,
472 		    sizeof(*linux_shminfo64)));
473 
474 	bzero(&linux_shminfo, sizeof(linux_shminfo));
475 	linux_shminfo.shmmax = linux_shminfo64->shmmax;
476 	linux_shminfo.shmmin = linux_shminfo64->shmmin;
477 	linux_shminfo.shmmni = linux_shminfo64->shmmni;
478 	linux_shminfo.shmseg = linux_shminfo64->shmseg;
479 	linux_shminfo.shmall = linux_shminfo64->shmall;
480 	return (copyout(&linux_shminfo, uaddr, sizeof(linux_shminfo)));
481 }
482 
483 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
484 int
485 linux_semtimedop_time64(struct thread *td, struct linux_semtimedop_time64_args *args)
486 {
487 	struct timespec ts, *tsa;
488 	int error;
489 
490 	if (args->timeout) {
491 		error = linux_get_timespec64(&ts, args->timeout);
492 		if (error != 0)
493 			return (error);
494 		tsa = &ts;
495 	} else
496 		tsa = NULL;
497 
498 	return (kern_semop(td, args->semid, PTRIN(args->tsops),
499 	    args->nsops, tsa));
500 }
501 #endif /* __i386__) || (__amd64__ && COMPAT_LINUX32) */
502 
503 int
504 linux_semtimedop(struct thread *td, struct linux_semtimedop_args *args)
505 {
506 	struct timespec ts, *tsa;
507 	int error;
508 
509 	if (args->timeout) {
510 		error = linux_get_timespec(&ts, args->timeout);
511 		if (error != 0)
512 			return (error);
513 		tsa = &ts;
514 	} else
515 		tsa = NULL;
516 
517 	return (kern_semop(td, args->semid, PTRIN(args->tsops),
518 	    args->nsops, tsa));
519 }
520 
521 int
522 linux_semget(struct thread *td, struct linux_semget_args *args)
523 {
524 	struct semget_args bsd_args = {
525 		.key = args->key,
526 		.nsems = args->nsems,
527 		.semflg = args->semflg
528 	};
529 
530 	if (args->nsems < 0)
531 		return (EINVAL);
532 	return (sys_semget(td, &bsd_args));
533 }
534 
535 int
536 linux_semctl(struct thread *td, struct linux_semctl_args *args)
537 {
538 	struct l_semid64_ds linux_semid64;
539 	struct l_seminfo linux_seminfo;
540 	struct semid_ds semid;
541 	union semun semun;
542 	register_t rval;
543 	int cmd, error;
544 
545 	memset(&linux_seminfo, 0, sizeof(linux_seminfo));
546 	memset(&linux_semid64, 0, sizeof(linux_semid64));
547 
548 	switch (args->cmd & ~LINUX_IPC_64) {
549 	case LINUX_IPC_RMID:
550 		cmd = IPC_RMID;
551 		break;
552 	case LINUX_GETNCNT:
553 		cmd = GETNCNT;
554 		break;
555 	case LINUX_GETPID:
556 		cmd = GETPID;
557 		break;
558 	case LINUX_GETVAL:
559 		cmd = GETVAL;
560 		break;
561 	case LINUX_GETZCNT:
562 		cmd = GETZCNT;
563 		break;
564 	case LINUX_SETVAL:
565 		cmd = SETVAL;
566 		semun.val = args->arg.val;
567 		break;
568 	case LINUX_IPC_SET:
569 		cmd = IPC_SET;
570 		error = linux_semid_pullup(args->cmd & LINUX_IPC_64,
571 		    &linux_semid64, PTRIN(args->arg.buf));
572 		if (error != 0)
573 			return (error);
574 		linux_to_bsd_semid_ds(&linux_semid64, &semid);
575 		semun.buf = &semid;
576 		return (kern_semctl(td, args->semid, args->semnum, cmd, &semun,
577 		    td->td_retval));
578 	case LINUX_IPC_STAT:
579 		cmd = IPC_STAT;
580 		semun.buf = &semid;
581 		error = kern_semctl(td, args->semid, args->semnum, cmd, &semun,
582 		    &rval);
583 		if (error != 0)
584 			return (error);
585 		bsd_to_linux_semid_ds(&semid, &linux_semid64);
586 		return (linux_semid_pushdown(args->cmd & LINUX_IPC_64,
587 		    &linux_semid64, PTRIN(args->arg.buf)));
588 	case LINUX_SEM_STAT:
589 		cmd = SEM_STAT;
590 		semun.buf = &semid;
591 		error = kern_semctl(td, args->semid, args->semnum, cmd, &semun,
592 		    &rval);
593 		if (error != 0)
594 			return (error);
595 		bsd_to_linux_semid_ds(&semid, &linux_semid64);
596 		error = linux_semid_pushdown(args->cmd & LINUX_IPC_64,
597 		    &linux_semid64, PTRIN(args->arg.buf));
598 		if (error == 0)
599 			td->td_retval[0] = rval;
600 		return (error);
601 	case LINUX_IPC_INFO:
602 	case LINUX_SEM_INFO:
603 		bcopy(&seminfo, &linux_seminfo.semmni, sizeof(linux_seminfo) -
604 		    sizeof(linux_seminfo.semmap) );
605 		/*
606 		 * Linux does not use the semmap field but populates it with
607 		 * the defined value from SEMMAP, which really is redefined to
608 		 * SEMMNS, which they define as SEMMNI * SEMMSL.  Try to
609 		 * simulate this returning our dynamic semmns value.
610 		 */
611 		linux_seminfo.semmap = linux_seminfo.semmns;
612 /* XXX BSD equivalent?
613 #define used_semids 10
614 #define used_sems 10
615 		linux_seminfo.semusz = used_semids;
616 		linux_seminfo.semaem = used_sems;
617 */
618 		error = copyout(&linux_seminfo,
619 		    PTRIN(args->arg.buf), sizeof(linux_seminfo));
620 		if (error != 0)
621 			return (error);
622 		/*
623 		 * TODO: Linux return the last assigned id, not the semmni.
624 		 */
625 		td->td_retval[0] = seminfo.semmni;
626 		return (0);
627 	case LINUX_GETALL:
628 		cmd = GETALL;
629 		semun.array = PTRIN(args->arg.array);
630 		break;
631 	case LINUX_SETALL:
632 		cmd = SETALL;
633 		semun.array = PTRIN(args->arg.array);
634 		break;
635 	default:
636 		linux_msg(td, "ipc type %d is not implemented",
637 		  args->cmd & ~LINUX_IPC_64);
638 		return (EINVAL);
639 	}
640 	return (kern_semctl(td, args->semid, args->semnum, cmd, &semun,
641 	    td->td_retval));
642 }
643 
644 int
645 linux_msgsnd(struct thread *td, struct linux_msgsnd_args *args)
646 {
647 	const void *msgp;
648 	long mtype;
649 	l_long lmtype;
650 	int error;
651 
652 	if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax)
653 		return (EINVAL);
654 	msgp = PTRIN(args->msgp);
655 	if ((error = copyin(msgp, &lmtype, sizeof(lmtype))) != 0)
656 		return (error);
657 	mtype = (long)lmtype;
658 	return (kern_msgsnd(td, args->msqid,
659 	    (const char *)msgp + sizeof(lmtype),
660 	    args->msgsz, args->msgflg, mtype));
661 }
662 
663 int
664 linux_msgrcv(struct thread *td, struct linux_msgrcv_args *args)
665 {
666 	void *msgp;
667 	long mtype;
668 	l_long lmtype;
669 	int error;
670 
671 	if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax)
672 		return (EINVAL);
673 	msgp = PTRIN(args->msgp);
674 	if ((error = kern_msgrcv(td, args->msqid,
675 	    (char *)msgp + sizeof(lmtype), args->msgsz,
676 	    args->msgtyp, args->msgflg, &mtype)) != 0)
677 		return (error);
678 	lmtype = (l_long)mtype;
679 	return (copyout(&lmtype, msgp, sizeof(lmtype)));
680 }
681 
682 int
683 linux_msgget(struct thread *td, struct linux_msgget_args *args)
684 {
685 	struct msgget_args bsd_args = {
686 		.key = args->key,
687 		.msgflg = args->msgflg
688 	};
689 
690 	return (sys_msgget(td, &bsd_args));
691 }
692 
693 int
694 linux_msgctl(struct thread *td, struct linux_msgctl_args *args)
695 {
696 	int error, bsd_cmd;
697 	struct l_msqid64_ds linux_msqid64;
698 	struct msqid_ds bsd_msqid;
699 
700 	memset(&linux_msqid64, 0, sizeof(linux_msqid64));
701 
702 	bsd_cmd = args->cmd & ~LINUX_IPC_64;
703 	switch (bsd_cmd) {
704 	case LINUX_IPC_INFO:
705 	case LINUX_MSG_INFO: {
706 		struct l_msginfo linux_msginfo;
707 
708 		memset(&linux_msginfo, 0, sizeof(linux_msginfo));
709 		/*
710 		 * XXX MSG_INFO uses the same data structure but returns different
711 		 * dynamic counters in msgpool, msgmap, and msgtql fields.
712 		 */
713 		linux_msginfo.msgpool = (long)msginfo.msgmni *
714 		    (long)msginfo.msgmnb / 1024L;	/* XXX MSG_INFO. */
715 		linux_msginfo.msgmap = msginfo.msgmnb;	/* XXX MSG_INFO. */
716 		linux_msginfo.msgmax = msginfo.msgmax;
717 		linux_msginfo.msgmnb = msginfo.msgmnb;
718 		linux_msginfo.msgmni = msginfo.msgmni;
719 		linux_msginfo.msgssz = msginfo.msgssz;
720 		linux_msginfo.msgtql = msginfo.msgtql;	/* XXX MSG_INFO. */
721 		linux_msginfo.msgseg = msginfo.msgseg;
722 		error = copyout(&linux_msginfo, PTRIN(args->buf),
723 		    sizeof(linux_msginfo));
724 		if (error == 0)
725 		    td->td_retval[0] = msginfo.msgmni;	/* XXX */
726 
727 		return (error);
728 	}
729 
730 	/*
731 	 * TODO: implement this
732 	 * case LINUX_MSG_STAT:
733 	 */
734 	case LINUX_IPC_STAT:
735 		/* NOTHING */
736 		break;
737 
738 	case LINUX_IPC_SET:
739 		error = linux_msqid_pullup(args->cmd & LINUX_IPC_64,
740 		    &linux_msqid64, PTRIN(args->buf));
741 		if (error != 0)
742 			return (error);
743 		linux_to_bsd_msqid_ds(&linux_msqid64, &bsd_msqid);
744 		break;
745 
746 	case LINUX_IPC_RMID:
747 		/* NOTHING */
748 		break;
749 
750 	default:
751 		return (EINVAL);
752 		break;
753 	}
754 
755 	error = kern_msgctl(td, args->msqid, bsd_cmd, &bsd_msqid);
756 	if (error != 0) {
757 		if (bsd_cmd == LINUX_IPC_RMID && error == EACCES)
758 			return (EPERM);
759 		if (bsd_cmd != LINUX_IPC_RMID || error != EINVAL)
760 			return (error);
761 	}
762 
763 	if (bsd_cmd == LINUX_IPC_STAT) {
764 		bsd_to_linux_msqid_ds(&bsd_msqid, &linux_msqid64);
765 		return (linux_msqid_pushdown(args->cmd & LINUX_IPC_64,
766 		    &linux_msqid64, PTRIN(args->buf)));
767 	}
768 
769 	return (0);
770 }
771 
772 int
773 linux_shmat(struct thread *td, struct linux_shmat_args *args)
774 {
775 	struct shmat_args bsd_args = {
776 		.shmid = args->shmid,
777 		.shmaddr = PTRIN(args->shmaddr),
778 		.shmflg = args->shmflg
779 	};
780 
781 	return (sys_shmat(td, &bsd_args));
782 }
783 
784 int
785 linux_shmdt(struct thread *td, struct linux_shmdt_args *args)
786 {
787 	struct shmdt_args bsd_args = {
788 		.shmaddr = PTRIN(args->shmaddr)
789 	};
790 
791 	return (sys_shmdt(td, &bsd_args));
792 }
793 
794 int
795 linux_shmget(struct thread *td, struct linux_shmget_args *args)
796 {
797 	struct shmget_args bsd_args = {
798 		.key = args->key,
799 		.size = args->size,
800 		.shmflg = args->shmflg
801 	};
802 
803 	return (sys_shmget(td, &bsd_args));
804 }
805 
806 int
807 linux_shmctl(struct thread *td, struct linux_shmctl_args *args)
808 {
809 	struct l_shmid64_ds linux_shmid64;
810 	struct l_shminfo64 linux_shminfo64;
811 	struct l_shm_info linux_shm_info;
812 	struct shmid_ds bsd_shmid;
813 	int error;
814 
815 	memset(&linux_shm_info, 0, sizeof(linux_shm_info));
816 	memset(&linux_shmid64, 0, sizeof(linux_shmid64));
817 	memset(&linux_shminfo64, 0, sizeof(linux_shminfo64));
818 
819 	switch (args->cmd & ~LINUX_IPC_64) {
820 	case LINUX_IPC_INFO: {
821 		struct shminfo bsd_shminfo;
822 
823 		/* Perform shmctl wanting removed segments lookup */
824 		error = kern_shmctl(td, args->shmid, IPC_INFO,
825 		    (void *)&bsd_shminfo, NULL);
826 		if (error != 0)
827 			return (error);
828 
829 		bsd_to_linux_shminfo(&bsd_shminfo, &linux_shminfo64);
830 
831 		return (linux_shminfo_pushdown(args->cmd & LINUX_IPC_64,
832 		    &linux_shminfo64, PTRIN(args->buf)));
833 	}
834 
835 	case LINUX_SHM_INFO: {
836 		struct shm_info bsd_shm_info;
837 
838 		/* Perform shmctl wanting removed segments lookup */
839 		error = kern_shmctl(td, args->shmid, SHM_INFO,
840 		    (void *)&bsd_shm_info, NULL);
841 		if (error != 0)
842 			return (error);
843 
844 		bsd_to_linux_shm_info(&bsd_shm_info, &linux_shm_info);
845 
846 		return (copyout(&linux_shm_info, PTRIN(args->buf),
847 		    sizeof(struct l_shm_info)));
848 	}
849 
850 	case LINUX_IPC_STAT:
851 		/* Perform shmctl wanting removed segments lookup */
852 		error = kern_shmctl(td, args->shmid, IPC_STAT,
853 		    (void *)&bsd_shmid, NULL);
854 		if (error != 0)
855 			return (error);
856 
857 		bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid64);
858 
859 		return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64,
860 		    &linux_shmid64, PTRIN(args->buf)));
861 
862 	case LINUX_SHM_STAT:
863 		/* Perform shmctl wanting removed segments lookup */
864 		error = kern_shmctl(td, args->shmid, IPC_STAT,
865 		    (void *)&bsd_shmid, NULL);
866 		if (error != 0)
867 			return (error);
868 
869 		bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid64);
870 
871 		return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64,
872 		    &linux_shmid64, PTRIN(args->buf)));
873 
874 	case LINUX_IPC_SET:
875 		error = linux_shmid_pullup(args->cmd & LINUX_IPC_64,
876 		    &linux_shmid64, PTRIN(args->buf));
877 		if (error != 0)
878 			return (error);
879 
880 		linux_to_bsd_shmid_ds(&linux_shmid64, &bsd_shmid);
881 
882 		/* Perform shmctl wanting removed segments lookup */
883 		return (kern_shmctl(td, args->shmid, IPC_SET,
884 		    (void *)&bsd_shmid, NULL));
885 
886 	case LINUX_IPC_RMID: {
887 		void *buf;
888 
889 		if (args->buf == 0)
890 			buf = NULL;
891 		else {
892 			error = linux_shmid_pullup(args->cmd & LINUX_IPC_64,
893 			    &linux_shmid64, PTRIN(args->buf));
894 			if (error != 0)
895 				return (error);
896 			linux_to_bsd_shmid_ds(&linux_shmid64, &bsd_shmid);
897 			buf = (void *)&bsd_shmid;
898 		}
899 		return (kern_shmctl(td, args->shmid, IPC_RMID, buf, NULL));
900 	}
901 
902 	case LINUX_SHM_LOCK:
903 		/* FALLTHROUGH */
904 	case LINUX_SHM_UNLOCK:
905 		/* FALLTHROUGH */
906 	default:
907 		linux_msg(td, "ipc type %d not implemented",
908 		    args->cmd & ~LINUX_IPC_64);
909 		return (EINVAL);
910 	}
911 }
912 
913 MODULE_DEPEND(linux, sysvmsg, 1, 1, 1);
914 MODULE_DEPEND(linux, sysvsem, 1, 1, 1);
915 MODULE_DEPEND(linux, sysvshm, 1, 1, 1);
916