Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.51.12.1
      1  1.51.12.1       jym /*	$NetBSD: linux_ipc.c,v 1.51.12.1 2009/05/13 17:18:57 jym Exp $	*/
      2       1.14       erh 
      3       1.14       erh /*-
      4       1.16      fvdl  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5       1.14       erh  * All rights reserved.
      6       1.14       erh  *
      7       1.14       erh  * This code is derived from software contributed to The NetBSD Foundation
      8       1.16      fvdl  * by Frank van der Linden and Eric Haszlakiewicz.
      9       1.14       erh  *
     10       1.14       erh  * Redistribution and use in source and binary forms, with or without
     11       1.14       erh  * modification, are permitted provided that the following conditions
     12       1.14       erh  * are met:
     13       1.14       erh  * 1. Redistributions of source code must retain the above copyright
     14       1.14       erh  *    notice, this list of conditions and the following disclaimer.
     15       1.14       erh  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.14       erh  *    notice, this list of conditions and the following disclaimer in the
     17       1.14       erh  *    documentation and/or other materials provided with the distribution.
     18       1.14       erh  *
     19       1.14       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.14       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.14       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.14       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.14       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.14       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.14       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.14       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.14       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.14       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.14       erh  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1      fvdl  */
     31       1.24     lukem 
     32       1.24     lukem #include <sys/cdefs.h>
     33  1.51.12.1       jym __KERNEL_RCSID(0, "$NetBSD: linux_ipc.c,v 1.51.12.1 2009/05/13 17:18:57 jym Exp $");
     34       1.19       erh 
     35       1.23       mrg #if defined(_KERNEL_OPT)
     36       1.19       erh #include "opt_sysv.h"
     37       1.22  jdolecek #endif
     38        1.1      fvdl 
     39        1.1      fvdl #include <sys/param.h>
     40        1.1      fvdl #include <sys/shm.h>
     41        1.6      fvdl #include <sys/sem.h>
     42        1.1      fvdl #include <sys/msg.h>
     43        1.1      fvdl #include <sys/proc.h>
     44        1.1      fvdl #include <sys/systm.h>
     45       1.43     njoly #include <sys/vnode.h>
     46        1.1      fvdl 
     47        1.1      fvdl #include <sys/mount.h>
     48        1.1      fvdl #include <sys/syscallargs.h>
     49        1.1      fvdl 
     50       1.15  christos #include <compat/linux/common/linux_types.h>
     51       1.15  christos #include <compat/linux/common/linux_signal.h>
     52       1.15  christos #include <compat/linux/common/linux_util.h>
     53       1.15  christos #include <compat/linux/common/linux_ipc.h>
     54       1.15  christos #include <compat/linux/common/linux_msg.h>
     55       1.15  christos #include <compat/linux/common/linux_shm.h>
     56       1.15  christos #include <compat/linux/common/linux_sem.h>
     57        1.1      fvdl 
     58       1.33      manu #include <compat/linux/linux_syscallargs.h>
     59       1.33      manu #include <compat/linux/linux_syscall.h>
     60       1.33      manu 
     61       1.34    dogcow #include <compat/linux/common/linux_ipccall.h>
     62       1.44     njoly #include <compat/linux/common/linux_machdep.h>
     63       1.34    dogcow 
     64        1.1      fvdl /*
     65       1.14       erh  * Note: Not all linux architechtures have explicit versions
     66       1.14       erh  *	of the SYSV* syscalls.  On the ones that don't
     67       1.14       erh  *	we pretend that they are defined anyway.  *_args and
     68       1.14       erh  *	prototypes are defined in individual headers;
     69       1.14       erh  *	syscalls.master lists those syscalls as NOARGS.
     70        1.1      fvdl  *
     71       1.14       erh  *	The functions in multiarch are the ones that just need
     72       1.14       erh  *	the arguments shuffled around and then use the
     73       1.14       erh  *	normal NetBSD syscall.
     74       1.14       erh  *
     75       1.14       erh  * Function in multiarch:
     76       1.14       erh  *	linux_sys_ipc		: linux_ipccall.c
     77       1.14       erh  *	liunx_semop		: linux_ipccall.c
     78       1.14       erh  *	linux_semget		: linux_ipccall.c
     79       1.14       erh  *	linux_msgsnd		: linux_ipccall.c
     80       1.14       erh  *	linux_msgrcv		: linux_ipccall.c
     81       1.14       erh  *	linux_msgget		: linux_ipccall.c
     82       1.14       erh  *	linux_shmdt		: linux_ipccall.c
     83       1.14       erh  *	linux_shmget		: linux_ipccall.c
     84        1.1      fvdl  */
     85        1.1      fvdl 
     86       1.11      fvdl #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
     87        1.1      fvdl /*
     88        1.1      fvdl  * Convert between Linux and NetBSD ipc_perm structures. Only the
     89        1.1      fvdl  * order of the fields is different.
     90        1.1      fvdl  */
     91       1.14       erh void
     92       1.41       dsl linux_to_bsd_ipc_perm(struct linux_ipc_perm *lpp, struct ipc_perm *bpp)
     93        1.1      fvdl {
     94        1.8   mycroft 
     95       1.21   thorpej 	bpp->_key = lpp->l_key;
     96        1.1      fvdl 	bpp->uid = lpp->l_uid;
     97        1.1      fvdl 	bpp->gid = lpp->l_gid;
     98        1.1      fvdl 	bpp->cuid = lpp->l_cuid;
     99        1.1      fvdl 	bpp->cgid = lpp->l_cgid;
    100        1.1      fvdl 	bpp->mode = lpp->l_mode;
    101       1.21   thorpej 	bpp->_seq = lpp->l_seq;
    102        1.1      fvdl }
    103        1.1      fvdl 
    104       1.14       erh void
    105       1.41       dsl linux_to_bsd_ipc64_perm(struct linux_ipc64_perm *lpp, struct ipc_perm *bpp)
    106       1.32  christos {
    107       1.32  christos 	bpp->_key = lpp->l_key;
    108       1.32  christos 	bpp->uid = lpp->l_uid;
    109       1.32  christos 	bpp->gid = lpp->l_gid;
    110       1.32  christos 	bpp->cuid = lpp->l_cuid;
    111       1.32  christos 	bpp->cgid = lpp->l_cgid;
    112       1.32  christos 	bpp->mode = lpp->l_mode;
    113       1.32  christos 	bpp->_seq = lpp->l_seq;
    114       1.32  christos }
    115       1.32  christos 
    116       1.32  christos void
    117       1.41       dsl bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct linux_ipc_perm *lpp)
    118        1.1      fvdl {
    119        1.8   mycroft 
    120       1.21   thorpej 	lpp->l_key = bpp->_key;
    121        1.1      fvdl 	lpp->l_uid = bpp->uid;
    122        1.1      fvdl 	lpp->l_gid = bpp->gid;
    123        1.1      fvdl 	lpp->l_cuid = bpp->cuid;
    124        1.1      fvdl 	lpp->l_cgid = bpp->cgid;
    125        1.1      fvdl 	lpp->l_mode = bpp->mode;
    126       1.21   thorpej 	lpp->l_seq = bpp->_seq;
    127        1.1      fvdl }
    128       1.32  christos 
    129       1.32  christos void
    130       1.41       dsl bsd_to_linux_ipc64_perm(struct ipc_perm *bpp, struct linux_ipc64_perm *lpp)
    131       1.32  christos {
    132       1.32  christos 	lpp->l_key = bpp->_key;
    133       1.32  christos 	lpp->l_uid = bpp->uid;
    134       1.32  christos 	lpp->l_gid = bpp->gid;
    135       1.32  christos 	lpp->l_cuid = bpp->cuid;
    136       1.32  christos 	lpp->l_cgid = bpp->cgid;
    137       1.32  christos 	lpp->l_mode = bpp->mode;
    138       1.32  christos 	lpp->l_seq = bpp->_seq;
    139       1.32  christos }
    140       1.32  christos 
    141       1.11      fvdl #endif
    142        1.1      fvdl 
    143        1.1      fvdl #ifdef SYSVSEM
    144        1.1      fvdl /*
    145        1.6      fvdl  * Semaphore operations. Most constants and structures are the same on
    146        1.6      fvdl  * both systems. Only semctl() needs some extra work.
    147        1.1      fvdl  */
    148        1.6      fvdl 
    149        1.6      fvdl /*
    150        1.6      fvdl  * Convert between Linux and NetBSD semid_ds structures.
    151        1.6      fvdl  */
    152       1.14       erh void
    153       1.41       dsl bsd_to_linux_semid_ds(struct semid_ds *bs, struct linux_semid_ds *ls)
    154        1.6      fvdl {
    155        1.6      fvdl 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    156        1.6      fvdl 	ls->l_sem_otime = bs->sem_otime;
    157        1.6      fvdl 	ls->l_sem_ctime = bs->sem_ctime;
    158        1.6      fvdl 	ls->l_sem_nsems = bs->sem_nsems;
    159       1.21   thorpej 	ls->l_sem_base = bs->_sem_base;
    160        1.6      fvdl }
    161        1.6      fvdl 
    162       1.14       erh void
    163       1.49     njoly bsd_to_linux_semid64_ds(struct semid_ds *bs, struct linux_semid64_ds *ls)
    164       1.49     njoly {
    165       1.49     njoly 	bsd_to_linux_ipc64_perm(&bs->sem_perm, &ls->l_sem_perm);
    166       1.49     njoly 	ls->l_sem_otime = bs->sem_otime;
    167       1.49     njoly 	ls->l_sem_ctime = bs->sem_ctime;
    168       1.49     njoly 	ls->l_sem_nsems = bs->sem_nsems;
    169       1.49     njoly }
    170       1.49     njoly 
    171       1.49     njoly void
    172       1.41       dsl linux_to_bsd_semid_ds(struct linux_semid_ds *ls, struct semid_ds *bs)
    173        1.6      fvdl {
    174        1.6      fvdl 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    175        1.6      fvdl 	bs->sem_otime = ls->l_sem_otime;
    176        1.6      fvdl 	bs->sem_ctime = ls->l_sem_ctime;
    177        1.6      fvdl 	bs->sem_nsems = ls->l_sem_nsems;
    178       1.21   thorpej 	bs->_sem_base = ls->l_sem_base;
    179        1.6      fvdl }
    180        1.6      fvdl 
    181       1.49     njoly void
    182       1.49     njoly linux_to_bsd_semid64_ds(struct linux_semid64_ds *ls, struct semid_ds *bs)
    183       1.49     njoly {
    184       1.49     njoly 	linux_to_bsd_ipc64_perm(&ls->l_sem_perm, &bs->sem_perm);
    185       1.49     njoly 	bs->sem_otime = ls->l_sem_otime;
    186       1.49     njoly 	bs->sem_ctime = ls->l_sem_ctime;
    187       1.49     njoly 	bs->sem_nsems = ls->l_sem_nsems;
    188       1.49     njoly }
    189       1.49     njoly 
    190        1.6      fvdl /*
    191       1.21   thorpej  * Most of this can be handled by directly passing the arguments on; we
    192       1.21   thorpej  * just need to frob the `cmd' and convert the semid_ds and semun.
    193        1.6      fvdl  */
    194        1.1      fvdl int
    195       1.42       dsl linux_sys_semctl(struct lwp *l, const struct linux_sys_semctl_args *uap, register_t *retval)
    196        1.1      fvdl {
    197       1.42       dsl 	/* {
    198       1.14       erh 		syscallarg(int) semid;
    199       1.14       erh 		syscallarg(int) semnum;
    200       1.14       erh 		syscallarg(int) cmd;
    201       1.14       erh 		syscallarg(union linux_semun) arg;
    202       1.42       dsl 	} */
    203       1.21   thorpej 	struct semid_ds sembuf;
    204       1.21   thorpej 	struct linux_semid_ds lsembuf;
    205       1.49     njoly 	struct linux_semid64_ds lsembuf64;
    206       1.21   thorpej 	union __semun semun;
    207       1.49     njoly 	int cmd, lcmd, error;
    208       1.21   thorpej 	void *pass_arg = NULL;
    209       1.21   thorpej 
    210       1.49     njoly 	lcmd = SCARG(uap, cmd);
    211       1.49     njoly #ifdef LINUX_IPC_FORCE64
    212  1.51.12.1       jym 	lcmd |= LINUX_IPC_64;
    213       1.49     njoly #endif
    214       1.21   thorpej 
    215  1.51.12.1       jym 	switch (lcmd & ~LINUX_IPC_64) {
    216       1.21   thorpej 	case LINUX_IPC_SET:
    217  1.51.12.1       jym 		if (lcmd & LINUX_IPC_64) {
    218  1.51.12.1       jym 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf64,
    219  1.51.12.1       jym 		    	    sizeof(lsembuf64));
    220  1.51.12.1       jym 			linux_to_bsd_semid64_ds(&lsembuf64, &sembuf);
    221  1.51.12.1       jym 		} else {
    222  1.51.12.1       jym 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
    223  1.51.12.1       jym 		    	   sizeof(lsembuf));
    224  1.51.12.1       jym 			linux_to_bsd_semid_ds(&lsembuf, &sembuf);
    225  1.51.12.1       jym 		}
    226       1.49     njoly 		if (error)
    227       1.49     njoly 			return (error);
    228       1.21   thorpej 		pass_arg = &sembuf;
    229       1.21   thorpej 		cmd = IPC_SET;
    230       1.21   thorpej 		break;
    231        1.6      fvdl 
    232       1.13   mycroft 	case LINUX_IPC_STAT:
    233       1.21   thorpej 		pass_arg = &sembuf;
    234       1.21   thorpej 		cmd = IPC_STAT;
    235       1.14       erh 		break;
    236       1.21   thorpej 
    237       1.13   mycroft 	case LINUX_IPC_RMID:
    238       1.21   thorpej 		cmd = IPC_RMID;
    239       1.13   mycroft 		break;
    240       1.21   thorpej 
    241        1.6      fvdl 	case LINUX_GETVAL:
    242       1.21   thorpej 		cmd = GETVAL;
    243        1.6      fvdl 		break;
    244       1.21   thorpej 
    245        1.6      fvdl 	case LINUX_GETPID:
    246       1.21   thorpej 		cmd = GETPID;
    247        1.6      fvdl 		break;
    248       1.21   thorpej 
    249        1.6      fvdl 	case LINUX_GETNCNT:
    250       1.21   thorpej 		cmd = GETNCNT;
    251        1.6      fvdl 		break;
    252       1.21   thorpej 
    253        1.6      fvdl 	case LINUX_GETZCNT:
    254       1.21   thorpej 		cmd = GETZCNT;
    255        1.6      fvdl 		break;
    256       1.21   thorpej 
    257       1.21   thorpej 	case LINUX_GETALL:
    258       1.21   thorpej 		pass_arg = &semun;
    259       1.21   thorpej 		semun.array = SCARG(uap, arg).l_array;
    260       1.21   thorpej 		cmd = GETALL;
    261       1.21   thorpej 		break;
    262       1.21   thorpej 
    263        1.6      fvdl 	case LINUX_SETVAL:
    264       1.21   thorpej 		pass_arg = &semun;
    265       1.21   thorpej 		semun.val = SCARG(uap, arg).l_val;
    266       1.21   thorpej 		cmd = SETVAL;
    267       1.20      tron 		break;
    268       1.21   thorpej 
    269       1.20      tron 	case LINUX_SETALL:
    270       1.21   thorpej 		pass_arg = &semun;
    271       1.21   thorpej 		semun.array = SCARG(uap, arg).l_array;
    272       1.21   thorpej 		cmd = SETALL;
    273        1.6      fvdl 		break;
    274       1.21   thorpej 
    275        1.6      fvdl 	default:
    276       1.21   thorpej 		return (EINVAL);
    277       1.21   thorpej 	}
    278       1.21   thorpej 
    279       1.35        ad 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    280       1.21   thorpej 	    pass_arg, retval);
    281       1.49     njoly 	if (error)
    282       1.49     njoly 		return error;
    283       1.21   thorpej 
    284       1.49     njoly 	switch (lcmd) {
    285       1.49     njoly 	case LINUX_IPC_STAT:
    286       1.21   thorpej 		bsd_to_linux_semid_ds(&sembuf, &lsembuf);
    287       1.21   thorpej 		error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
    288       1.21   thorpej 		    sizeof(lsembuf));
    289       1.49     njoly 		break;
    290       1.49     njoly 	case LINUX_IPC_STAT | LINUX_IPC_64:
    291       1.49     njoly 		bsd_to_linux_semid64_ds(&sembuf, &lsembuf64);
    292       1.49     njoly 		error = copyout(&lsembuf64, SCARG(uap, arg).l_buf,
    293       1.49     njoly 		    sizeof(lsembuf64));
    294       1.49     njoly 		break;
    295       1.49     njoly 	default:
    296       1.49     njoly 		break;
    297        1.6      fvdl 	}
    298       1.21   thorpej 
    299       1.21   thorpej 	return (error);
    300        1.1      fvdl }
    301        1.1      fvdl #endif /* SYSVSEM */
    302        1.1      fvdl 
    303        1.1      fvdl #ifdef SYSVMSG
    304        1.6      fvdl 
    305       1.14       erh void
    306       1.41       dsl linux_to_bsd_msqid_ds(struct linux_msqid_ds *lmp, struct msqid_ds *bmp)
    307        1.6      fvdl {
    308        1.8   mycroft 
    309        1.6      fvdl 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    310       1.21   thorpej 	bmp->_msg_first = lmp->l_msg_first;
    311       1.21   thorpej 	bmp->_msg_last = lmp->l_msg_last;
    312       1.21   thorpej 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    313        1.6      fvdl 	bmp->msg_qnum = lmp->l_msg_qnum;
    314        1.6      fvdl 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    315        1.6      fvdl 	bmp->msg_lspid = lmp->l_msg_lspid;
    316        1.6      fvdl 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    317        1.6      fvdl 	bmp->msg_stime = lmp->l_msg_stime;
    318        1.6      fvdl 	bmp->msg_rtime = lmp->l_msg_rtime;
    319        1.6      fvdl 	bmp->msg_ctime = lmp->l_msg_ctime;
    320        1.6      fvdl }
    321        1.6      fvdl 
    322       1.14       erh void
    323       1.51     njoly linux_to_bsd_msqid64_ds(struct linux_msqid64_ds *lmp, struct msqid_ds *bmp)
    324       1.51     njoly {
    325       1.51     njoly 	linux_to_bsd_ipc64_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    326       1.51     njoly 	bmp->msg_stime = lmp->l_msg_stime;
    327       1.51     njoly 	bmp->msg_rtime = lmp->l_msg_rtime;
    328       1.51     njoly 	bmp->msg_ctime = lmp->l_msg_ctime;
    329       1.51     njoly 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    330       1.51     njoly 	bmp->msg_qnum = lmp->l_msg_qnum;
    331       1.51     njoly 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    332       1.51     njoly 	bmp->msg_lspid = lmp->l_msg_lspid;
    333       1.51     njoly 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    334       1.51     njoly }
    335       1.51     njoly 
    336       1.51     njoly void
    337       1.41       dsl bsd_to_linux_msqid_ds(struct msqid_ds *bmp, struct linux_msqid_ds *lmp)
    338        1.6      fvdl {
    339        1.8   mycroft 
    340        1.6      fvdl 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    341       1.21   thorpej 	lmp->l_msg_first = bmp->_msg_first;
    342       1.21   thorpej 	lmp->l_msg_last = bmp->_msg_last;
    343       1.21   thorpej 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    344        1.6      fvdl 	lmp->l_msg_qnum = bmp->msg_qnum;
    345        1.6      fvdl 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    346        1.6      fvdl 	lmp->l_msg_lspid = bmp->msg_lspid;
    347        1.6      fvdl 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    348        1.6      fvdl 	lmp->l_msg_stime = bmp->msg_stime;
    349        1.6      fvdl 	lmp->l_msg_rtime = bmp->msg_rtime;
    350        1.6      fvdl 	lmp->l_msg_ctime = bmp->msg_ctime;
    351        1.6      fvdl }
    352        1.6      fvdl 
    353       1.51     njoly void
    354       1.51     njoly bsd_to_linux_msqid64_ds(struct msqid_ds *bmp, struct linux_msqid64_ds *lmp)
    355       1.51     njoly {
    356       1.51     njoly 	bsd_to_linux_ipc64_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    357       1.51     njoly 	lmp->l_msg_stime = bmp->msg_stime;
    358       1.51     njoly 	lmp->l_msg_rtime = bmp->msg_rtime;
    359       1.51     njoly 	lmp->l_msg_ctime = bmp->msg_ctime;
    360       1.51     njoly 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    361       1.51     njoly 	lmp->l_msg_qnum = bmp->msg_qnum;
    362       1.51     njoly 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    363       1.51     njoly 	lmp->l_msg_lspid = bmp->msg_lspid;
    364       1.51     njoly 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    365       1.51     njoly }
    366       1.51     njoly 
    367       1.14       erh int
    368       1.42       dsl linux_sys_msgctl(struct lwp *l, const struct linux_sys_msgctl_args *uap, register_t *retval)
    369        1.1      fvdl {
    370       1.42       dsl 	/* {
    371       1.14       erh 		syscallarg(int) msqid;
    372       1.14       erh 		syscallarg(int) cmd;
    373       1.14       erh 		syscallarg(struct linux_msqid_ds *) buf;
    374       1.42       dsl 	} */
    375       1.51     njoly 	struct msqid_ds bm, *bmp = NULL;
    376        1.6      fvdl 	struct linux_msqid_ds lm;
    377       1.51     njoly 	struct linux_msqid64_ds lm64;
    378       1.51     njoly 	int cmd, lcmd, error;
    379       1.51     njoly 
    380       1.51     njoly 	lcmd = SCARG(uap, cmd);
    381       1.51     njoly #ifdef LINUX_IPC_FORCE64
    382  1.51.12.1       jym 	lcmd |= LINUX_IPC_64;
    383       1.51     njoly #endif
    384        1.6      fvdl 
    385  1.51.12.1       jym 	switch (lcmd & ~LINUX_IPC_64) {
    386       1.13   mycroft 	case LINUX_IPC_STAT:
    387       1.51     njoly 		cmd = IPC_STAT;
    388       1.51     njoly 		bmp = &bm;
    389       1.51     njoly 		break;
    390        1.6      fvdl 	case LINUX_IPC_SET:
    391  1.51.12.1       jym 		if (lcmd & LINUX_IPC_64) {
    392  1.51.12.1       jym 			error = copyin(SCARG(uap, buf), &lm64, sizeof lm64);
    393  1.51.12.1       jym 			linux_to_bsd_msqid64_ds(&lm64, &bm);
    394  1.51.12.1       jym 		} else {
    395  1.51.12.1       jym 			error = copyin(SCARG(uap, buf), &lm, sizeof lm);
    396  1.51.12.1       jym 			linux_to_bsd_msqid_ds(&lm, &bm);
    397  1.51.12.1       jym 		}
    398  1.51.12.1       jym 		if (error)
    399       1.51     njoly 			return error;
    400       1.51     njoly 		cmd = IPC_SET;
    401       1.51     njoly 		bmp = &bm;
    402       1.51     njoly 		break;
    403       1.13   mycroft 	case LINUX_IPC_RMID:
    404       1.51     njoly 		cmd = IPC_RMID;
    405       1.13   mycroft 		break;
    406       1.13   mycroft 	default:
    407       1.13   mycroft 		return EINVAL;
    408        1.6      fvdl 	}
    409       1.51     njoly 
    410       1.51     njoly 	if ((error = msgctl1(l, SCARG(uap, msqid), cmd, bmp)))
    411       1.51     njoly 		return error;
    412       1.51     njoly 
    413       1.51     njoly 	switch (lcmd) {
    414       1.51     njoly 	case LINUX_IPC_STAT:
    415       1.51     njoly 		bsd_to_linux_msqid_ds(&bm, &lm);
    416       1.51     njoly 		error = copyout(&lm, SCARG(uap, buf), sizeof lm);
    417       1.51     njoly 		break;
    418       1.51     njoly 	case LINUX_IPC_STAT|LINUX_IPC_64:
    419       1.51     njoly 		bsd_to_linux_msqid64_ds(&bm, &lm64);
    420       1.51     njoly 		error = copyout(&lm64, SCARG(uap, buf), sizeof lm64);
    421       1.51     njoly 		break;
    422       1.51     njoly 	default:
    423       1.51     njoly 		break;
    424       1.51     njoly 	}
    425       1.51     njoly 
    426       1.51     njoly 	return error;
    427        1.1      fvdl }
    428        1.1      fvdl #endif /* SYSVMSG */
    429        1.1      fvdl 
    430        1.1      fvdl #ifdef SYSVSHM
    431        1.1      fvdl /*
    432       1.30  jdolecek  * shmget(2). Just make sure the Linux-compatible shmat() semantics
    433       1.30  jdolecek  * is enabled for the segment, so that shmat() succeeds even when
    434       1.30  jdolecek  * the segment would be removed.
    435       1.30  jdolecek  */
    436       1.30  jdolecek int
    437       1.42       dsl linux_sys_shmget(struct lwp *l, const struct linux_sys_shmget_args *uap, register_t *retval)
    438       1.30  jdolecek {
    439       1.42       dsl 	/* {
    440       1.30  jdolecek 		syscallarg(key_t) key;
    441       1.30  jdolecek 		syscallarg(size_t) size;
    442       1.30  jdolecek 		syscallarg(int) shmflg;
    443       1.42       dsl 	} */
    444       1.42       dsl 	struct sys_shmget_args bsd_ua;
    445       1.30  jdolecek 
    446       1.42       dsl 	SCARG(&bsd_ua, key) = SCARG(uap, key);
    447       1.42       dsl 	SCARG(&bsd_ua, size) = SCARG(uap, size);
    448       1.42       dsl 	SCARG(&bsd_ua, shmflg) = SCARG(uap, shmflg) | _SHM_RMLINGER;
    449       1.42       dsl 
    450       1.42       dsl 	return sys_shmget(l, &bsd_ua, retval);
    451       1.30  jdolecek }
    452       1.30  jdolecek 
    453       1.30  jdolecek /*
    454        1.1      fvdl  * shmat(2). Very straightforward, except that Linux passes a pointer
    455        1.1      fvdl  * in which the return value is to be passed. This is subsequently
    456        1.1      fvdl  * handled by libc, apparently.
    457        1.1      fvdl  */
    458       1.36      manu #ifndef __amd64__
    459       1.14       erh int
    460       1.42       dsl linux_sys_shmat(struct lwp *l, const struct linux_sys_shmat_args *uap, register_t *retval)
    461        1.1      fvdl {
    462       1.42       dsl 	/* {
    463       1.14       erh 		syscallarg(int) shmid;
    464       1.14       erh 		syscallarg(void *) shmaddr;
    465       1.14       erh 		syscallarg(int) shmflg;
    466       1.14       erh 		syscallarg(u_long *) raddr;
    467       1.42       dsl 	} */
    468        1.1      fvdl 	int error;
    469        1.1      fvdl 
    470       1.42       dsl 	if ((error = sys_shmat(l, (const void *)uap, retval)))
    471        1.1      fvdl 		return error;
    472        1.1      fvdl 
    473       1.42       dsl 	if ((error = copyout(&retval[0], SCARG(uap, raddr), sizeof retval[0])))
    474        1.1      fvdl 		return error;
    475       1.31     perry 
    476        1.1      fvdl 	retval[0] = 0;
    477        1.1      fvdl 	return 0;
    478        1.1      fvdl }
    479       1.36      manu #endif /* __amd64__ */
    480        1.1      fvdl 
    481        1.1      fvdl /*
    482        1.1      fvdl  * Convert between Linux and NetBSD shmid_ds structures.
    483        1.1      fvdl  * The order of the fields is once again the difference, and
    484        1.1      fvdl  * we also need a place to store the internal data pointer
    485        1.1      fvdl  * in, which is unfortunately stored in this structure.
    486        1.1      fvdl  *
    487        1.1      fvdl  * We abuse a Linux internal field for that.
    488        1.1      fvdl  */
    489       1.14       erh void
    490       1.41       dsl linux_to_bsd_shmid_ds(struct linux_shmid_ds *lsp, struct shmid_ds *bsp)
    491        1.1      fvdl {
    492        1.8   mycroft 
    493        1.1      fvdl 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    494        1.1      fvdl 	bsp->shm_segsz = lsp->l_shm_segsz;
    495        1.1      fvdl 	bsp->shm_lpid = lsp->l_shm_lpid;
    496        1.1      fvdl 	bsp->shm_cpid = lsp->l_shm_cpid;
    497        1.1      fvdl 	bsp->shm_nattch = lsp->l_shm_nattch;
    498        1.1      fvdl 	bsp->shm_atime = lsp->l_shm_atime;
    499        1.1      fvdl 	bsp->shm_dtime = lsp->l_shm_dtime;
    500        1.1      fvdl 	bsp->shm_ctime = lsp->l_shm_ctime;
    501       1.21   thorpej 	bsp->_shm_internal = lsp->l_private2;	/* XXX Oh well. */
    502        1.1      fvdl }
    503        1.1      fvdl 
    504       1.14       erh void
    505       1.41       dsl linux_to_bsd_shmid64_ds(struct linux_shmid64_ds *lsp, struct shmid_ds *bsp)
    506       1.32  christos {
    507       1.32  christos 
    508       1.32  christos 	linux_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    509       1.32  christos 	bsp->shm_segsz = lsp->l_shm_segsz;
    510       1.32  christos 	bsp->shm_lpid = lsp->l_shm_lpid;
    511       1.32  christos 	bsp->shm_cpid = lsp->l_shm_cpid;
    512       1.32  christos 	bsp->shm_nattch = lsp->l_shm_nattch;
    513       1.32  christos 	bsp->shm_atime = lsp->l_shm_atime;
    514       1.32  christos 	bsp->shm_dtime = lsp->l_shm_dtime;
    515       1.32  christos 	bsp->shm_ctime = lsp->l_shm_ctime;
    516       1.32  christos 	bsp->_shm_internal = (void*)lsp->l___unused5;	/* XXX Oh well. */
    517       1.32  christos }
    518       1.32  christos 
    519       1.32  christos void
    520       1.41       dsl bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct linux_shmid_ds *lsp)
    521        1.1      fvdl {
    522        1.8   mycroft 
    523        1.1      fvdl 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    524        1.1      fvdl 	lsp->l_shm_segsz = bsp->shm_segsz;
    525        1.1      fvdl 	lsp->l_shm_lpid = bsp->shm_lpid;
    526        1.1      fvdl 	lsp->l_shm_cpid = bsp->shm_cpid;
    527        1.1      fvdl 	lsp->l_shm_nattch = bsp->shm_nattch;
    528        1.1      fvdl 	lsp->l_shm_atime = bsp->shm_atime;
    529        1.1      fvdl 	lsp->l_shm_dtime = bsp->shm_dtime;
    530        1.1      fvdl 	lsp->l_shm_ctime = bsp->shm_ctime;
    531       1.21   thorpej 	lsp->l_private2 = bsp->_shm_internal;	/* XXX */
    532        1.1      fvdl }
    533        1.1      fvdl 
    534       1.32  christos void
    535       1.41       dsl bsd_to_linux_shmid64_ds(struct shmid_ds *bsp, struct linux_shmid64_ds *lsp)
    536       1.32  christos {
    537       1.32  christos 	bsd_to_linux_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    538       1.32  christos 	lsp->l_shm_segsz = bsp->shm_segsz;
    539       1.32  christos 	lsp->l_shm_lpid = bsp->shm_lpid;
    540       1.32  christos 	lsp->l_shm_cpid = bsp->shm_cpid;
    541       1.32  christos 	lsp->l_shm_nattch = bsp->shm_nattch;
    542       1.32  christos 	lsp->l_shm_atime = bsp->shm_atime;
    543       1.32  christos 	lsp->l_shm_dtime = bsp->shm_dtime;
    544       1.32  christos 	lsp->l_shm_ctime = bsp->shm_ctime;
    545       1.32  christos 	lsp->l___unused5 = (u_long)bsp->_shm_internal;	/* XXX */
    546       1.32  christos }
    547       1.32  christos 
    548        1.1      fvdl /*
    549       1.46     njoly  * shmctl.
    550        1.1      fvdl  *
    551        1.1      fvdl  * The usual structure conversion and massaging is done.
    552        1.1      fvdl  */
    553       1.14       erh int
    554       1.42       dsl linux_sys_shmctl(struct lwp *l, const struct linux_sys_shmctl_args *uap, register_t *retval)
    555        1.1      fvdl {
    556       1.42       dsl 	/* {
    557       1.14       erh 		syscallarg(int) shmid;
    558       1.14       erh 		syscallarg(int) cmd;
    559       1.14       erh 		syscallarg(struct linux_shmid_ds *) buf;
    560       1.42       dsl 	} */
    561       1.40       dsl 	struct shmid_ds bs;
    562       1.13   mycroft 	struct linux_shmid_ds ls;
    563       1.32  christos 	struct linux_shmid64_ds ls64;
    564       1.32  christos 	struct linux_shminfo64 lsi64;
    565       1.32  christos 	struct linux_shm_info lsi;
    566       1.43     njoly 	int error, i, cmd, shmid;
    567        1.1      fvdl 
    568       1.43     njoly 	shmid = SCARG(uap, shmid);
    569       1.32  christos 	cmd = SCARG(uap, cmd);
    570       1.47     njoly #ifdef LINUX_IPC_FORCE64
    571  1.51.12.1       jym 	cmd |= LINUX_IPC_64;
    572       1.44     njoly #endif
    573       1.43     njoly 
    574  1.51.12.1       jym 	switch (cmd & ~LINUX_IPC_64) {
    575       1.32  christos 	case LINUX_SHM_STAT:
    576  1.51.12.1       jym 		shmid = IXSEQ_TO_IPCID(shmid, shmsegs[shmid].shm_perm);
    577  1.51.12.1       jym 		retval[0] = shmid;
    578  1.51.12.1       jym 		/*FALLTHROUGH*/
    579       1.32  christos 
    580  1.51.12.1       jym 	case LINUX_IPC_STAT:
    581       1.43     njoly 		error = shmctl1(l, shmid, IPC_STAT, &bs);
    582       1.40       dsl 		if (error != 0)
    583       1.32  christos 			return error;
    584  1.51.12.1       jym 		if (cmd & LINUX_IPC_64) {
    585  1.51.12.1       jym 			bsd_to_linux_shmid64_ds(&bs, &ls64);
    586  1.51.12.1       jym 			error = copyout(&ls64, SCARG(uap, buf), sizeof ls64);
    587  1.51.12.1       jym 		} else {
    588  1.51.12.1       jym 			bsd_to_linux_shmid_ds(&bs, &ls);
    589  1.51.12.1       jym 			error = copyout(&ls, SCARG(uap, buf), sizeof ls);
    590  1.51.12.1       jym 		}
    591  1.51.12.1       jym 		return error;
    592       1.32  christos 
    593        1.1      fvdl 	case LINUX_IPC_SET:
    594  1.51.12.1       jym 		if (cmd & LINUX_IPC_64) {
    595  1.51.12.1       jym 			error = copyin(SCARG(uap, buf), &ls64, sizeof ls64);
    596  1.51.12.1       jym 			linux_to_bsd_shmid64_ds(&ls64, &bs);
    597  1.51.12.1       jym 		} else {
    598  1.51.12.1       jym 			error = copyin(SCARG(uap, buf), &ls, sizeof ls);
    599  1.51.12.1       jym 			linux_to_bsd_shmid_ds(&ls, &bs);
    600  1.51.12.1       jym 		}
    601  1.51.12.1       jym 		if (error != 0)
    602       1.48     njoly 			return error;
    603       1.48     njoly 		return shmctl1(l, shmid, IPC_SET, &bs);
    604       1.48     njoly 
    605        1.1      fvdl 	case LINUX_IPC_RMID:
    606       1.43     njoly 		return shmctl1(l, shmid, IPC_RMID, NULL);
    607       1.32  christos 
    608        1.1      fvdl 	case LINUX_SHM_LOCK:
    609       1.43     njoly 		return shmctl1(l, shmid, SHM_LOCK, NULL);
    610       1.32  christos 
    611        1.1      fvdl 	case LINUX_SHM_UNLOCK:
    612       1.43     njoly 		return shmctl1(l, shmid, SHM_UNLOCK, NULL);
    613       1.32  christos 
    614        1.1      fvdl 	case LINUX_IPC_INFO:
    615       1.32  christos 		memset(&lsi64, 0, sizeof lsi64);
    616       1.32  christos 		lsi64.l_shmmax = shminfo.shmmax;
    617       1.32  christos 		lsi64.l_shmmin = shminfo.shmmin;
    618       1.32  christos 		lsi64.l_shmmni = shminfo.shmmni;
    619       1.32  christos 		lsi64.l_shmseg = shminfo.shmseg;
    620       1.32  christos 		lsi64.l_shmall = shminfo.shmall;
    621       1.43     njoly 		for (i = shminfo.shmmni - 1; i > 0; i--)
    622       1.43     njoly 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    623       1.43     njoly 				break;
    624       1.43     njoly 		retval[0] = i;
    625       1.32  christos 		return copyout(&lsi64, SCARG(uap, buf), sizeof lsi64);
    626       1.32  christos 
    627        1.1      fvdl 	case LINUX_SHM_INFO:
    628       1.32  christos 		(void)memset(&lsi, 0, sizeof lsi);
    629       1.32  christos 		lsi.l_used_ids = shm_nused;
    630       1.32  christos 		for (i = 0; i < shminfo.shmmni; i++)
    631       1.32  christos 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    632       1.43     njoly 				lsi.l_shm_tot +=
    633       1.43     njoly 				    round_page(shmsegs[i].shm_segsz) /
    634       1.43     njoly 				    uvmexp.pagesize;
    635       1.32  christos 		lsi.l_shm_rss = 0;
    636       1.32  christos 		lsi.l_shm_swp = 0;
    637       1.32  christos 		lsi.l_swap_attempts = 0;
    638       1.32  christos 		lsi.l_swap_successes = 0;
    639       1.43     njoly 		for (i = shminfo.shmmni - 1; i > 0; i--)
    640       1.43     njoly 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    641       1.43     njoly 				break;
    642       1.43     njoly 		retval[0] = i;
    643       1.32  christos 		return copyout(&lsi, SCARG(uap, buf), sizeof lsi);
    644       1.32  christos 
    645        1.1      fvdl 	default:
    646       1.32  christos #ifdef DEBUG
    647       1.32  christos 		printf("linux_sys_shmctl cmd %d\n", SCARG(uap, cmd));
    648       1.32  christos #endif
    649        1.1      fvdl 		return EINVAL;
    650        1.1      fvdl 	}
    651        1.1      fvdl }
    652        1.1      fvdl #endif /* SYSVSHM */
    653