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