Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.54.4.1
      1  1.54.4.1     rmind /*	$NetBSD: linux_ipc.c,v 1.54.4.1 2011/05/31 03:04:30 rmind 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.54.4.1     rmind __KERNEL_RCSID(0, "$NetBSD: linux_ipc.c,v 1.54.4.1 2011/05/31 03:04:30 rmind 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.54.4.1     rmind  *	linux_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.53     njoly 	lcmd |= LINUX_IPC_64;
    213      1.49     njoly #endif
    214      1.21   thorpej 
    215      1.53     njoly 	switch (lcmd & ~LINUX_IPC_64) {
    216      1.21   thorpej 	case LINUX_IPC_SET:
    217      1.53     njoly 		if (lcmd & LINUX_IPC_64) {
    218      1.53     njoly 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf64,
    219      1.53     njoly 		    	    sizeof(lsembuf64));
    220      1.53     njoly 			linux_to_bsd_semid64_ds(&lsembuf64, &sembuf);
    221      1.53     njoly 		} else {
    222      1.53     njoly 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
    223      1.53     njoly 		    	   sizeof(lsembuf));
    224      1.53     njoly 			linux_to_bsd_semid_ds(&lsembuf, &sembuf);
    225      1.53     njoly 		}
    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.54     joerg 	memset(bmp, 0, sizeof(*bmp));
    310       1.6      fvdl 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    311      1.21   thorpej 	bmp->_msg_first = lmp->l_msg_first;
    312      1.21   thorpej 	bmp->_msg_last = lmp->l_msg_last;
    313      1.21   thorpej 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    314       1.6      fvdl 	bmp->msg_qnum = lmp->l_msg_qnum;
    315       1.6      fvdl 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    316       1.6      fvdl 	bmp->msg_lspid = lmp->l_msg_lspid;
    317       1.6      fvdl 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    318       1.6      fvdl 	bmp->msg_stime = lmp->l_msg_stime;
    319       1.6      fvdl 	bmp->msg_rtime = lmp->l_msg_rtime;
    320       1.6      fvdl 	bmp->msg_ctime = lmp->l_msg_ctime;
    321       1.6      fvdl }
    322       1.6      fvdl 
    323      1.14       erh void
    324      1.51     njoly linux_to_bsd_msqid64_ds(struct linux_msqid64_ds *lmp, struct msqid_ds *bmp)
    325      1.51     njoly {
    326      1.54     joerg 
    327      1.54     joerg 	memset(bmp, 0, sizeof(*bmp));
    328      1.51     njoly 	linux_to_bsd_ipc64_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    329      1.51     njoly 	bmp->msg_stime = lmp->l_msg_stime;
    330      1.51     njoly 	bmp->msg_rtime = lmp->l_msg_rtime;
    331      1.51     njoly 	bmp->msg_ctime = lmp->l_msg_ctime;
    332      1.51     njoly 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    333      1.51     njoly 	bmp->msg_qnum = lmp->l_msg_qnum;
    334      1.51     njoly 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    335      1.51     njoly 	bmp->msg_lspid = lmp->l_msg_lspid;
    336      1.51     njoly 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    337      1.51     njoly }
    338      1.51     njoly 
    339      1.51     njoly void
    340      1.41       dsl bsd_to_linux_msqid_ds(struct msqid_ds *bmp, struct linux_msqid_ds *lmp)
    341       1.6      fvdl {
    342       1.8   mycroft 
    343      1.54     joerg 	memset(lmp, 0, sizeof(*lmp));
    344       1.6      fvdl 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    345      1.21   thorpej 	lmp->l_msg_first = bmp->_msg_first;
    346      1.21   thorpej 	lmp->l_msg_last = bmp->_msg_last;
    347      1.21   thorpej 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    348       1.6      fvdl 	lmp->l_msg_qnum = bmp->msg_qnum;
    349       1.6      fvdl 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    350       1.6      fvdl 	lmp->l_msg_lspid = bmp->msg_lspid;
    351       1.6      fvdl 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    352       1.6      fvdl 	lmp->l_msg_stime = bmp->msg_stime;
    353       1.6      fvdl 	lmp->l_msg_rtime = bmp->msg_rtime;
    354       1.6      fvdl 	lmp->l_msg_ctime = bmp->msg_ctime;
    355       1.6      fvdl }
    356       1.6      fvdl 
    357      1.51     njoly void
    358      1.51     njoly bsd_to_linux_msqid64_ds(struct msqid_ds *bmp, struct linux_msqid64_ds *lmp)
    359      1.51     njoly {
    360      1.54     joerg 
    361      1.54     joerg 	memset(lmp, 0, sizeof(*lmp));
    362      1.51     njoly 	bsd_to_linux_ipc64_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    363      1.51     njoly 	lmp->l_msg_stime = bmp->msg_stime;
    364      1.51     njoly 	lmp->l_msg_rtime = bmp->msg_rtime;
    365      1.51     njoly 	lmp->l_msg_ctime = bmp->msg_ctime;
    366      1.51     njoly 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    367      1.51     njoly 	lmp->l_msg_qnum = bmp->msg_qnum;
    368      1.51     njoly 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    369      1.51     njoly 	lmp->l_msg_lspid = bmp->msg_lspid;
    370      1.51     njoly 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    371      1.51     njoly }
    372      1.51     njoly 
    373      1.14       erh int
    374      1.42       dsl linux_sys_msgctl(struct lwp *l, const struct linux_sys_msgctl_args *uap, register_t *retval)
    375       1.1      fvdl {
    376      1.42       dsl 	/* {
    377      1.14       erh 		syscallarg(int) msqid;
    378      1.14       erh 		syscallarg(int) cmd;
    379      1.14       erh 		syscallarg(struct linux_msqid_ds *) buf;
    380      1.42       dsl 	} */
    381      1.51     njoly 	struct msqid_ds bm, *bmp = NULL;
    382       1.6      fvdl 	struct linux_msqid_ds lm;
    383      1.51     njoly 	struct linux_msqid64_ds lm64;
    384      1.51     njoly 	int cmd, lcmd, error;
    385      1.51     njoly 
    386      1.51     njoly 	lcmd = SCARG(uap, cmd);
    387      1.51     njoly #ifdef LINUX_IPC_FORCE64
    388      1.53     njoly 	lcmd |= LINUX_IPC_64;
    389      1.51     njoly #endif
    390       1.6      fvdl 
    391      1.53     njoly 	switch (lcmd & ~LINUX_IPC_64) {
    392      1.13   mycroft 	case LINUX_IPC_STAT:
    393      1.51     njoly 		cmd = IPC_STAT;
    394      1.51     njoly 		bmp = &bm;
    395      1.51     njoly 		break;
    396       1.6      fvdl 	case LINUX_IPC_SET:
    397      1.53     njoly 		if (lcmd & LINUX_IPC_64) {
    398      1.53     njoly 			error = copyin(SCARG(uap, buf), &lm64, sizeof lm64);
    399      1.53     njoly 			linux_to_bsd_msqid64_ds(&lm64, &bm);
    400      1.53     njoly 		} else {
    401      1.53     njoly 			error = copyin(SCARG(uap, buf), &lm, sizeof lm);
    402      1.53     njoly 			linux_to_bsd_msqid_ds(&lm, &bm);
    403      1.53     njoly 		}
    404      1.53     njoly 		if (error)
    405      1.51     njoly 			return error;
    406      1.51     njoly 		cmd = IPC_SET;
    407      1.51     njoly 		bmp = &bm;
    408      1.51     njoly 		break;
    409      1.13   mycroft 	case LINUX_IPC_RMID:
    410      1.51     njoly 		cmd = IPC_RMID;
    411      1.13   mycroft 		break;
    412      1.13   mycroft 	default:
    413      1.13   mycroft 		return EINVAL;
    414       1.6      fvdl 	}
    415      1.51     njoly 
    416      1.51     njoly 	if ((error = msgctl1(l, SCARG(uap, msqid), cmd, bmp)))
    417      1.51     njoly 		return error;
    418      1.51     njoly 
    419      1.51     njoly 	switch (lcmd) {
    420      1.51     njoly 	case LINUX_IPC_STAT:
    421      1.51     njoly 		bsd_to_linux_msqid_ds(&bm, &lm);
    422      1.51     njoly 		error = copyout(&lm, SCARG(uap, buf), sizeof lm);
    423      1.51     njoly 		break;
    424      1.51     njoly 	case LINUX_IPC_STAT|LINUX_IPC_64:
    425      1.51     njoly 		bsd_to_linux_msqid64_ds(&bm, &lm64);
    426      1.51     njoly 		error = copyout(&lm64, SCARG(uap, buf), sizeof lm64);
    427      1.51     njoly 		break;
    428      1.51     njoly 	default:
    429      1.51     njoly 		break;
    430      1.51     njoly 	}
    431      1.51     njoly 
    432      1.51     njoly 	return error;
    433       1.1      fvdl }
    434       1.1      fvdl #endif /* SYSVMSG */
    435       1.1      fvdl 
    436       1.1      fvdl #ifdef SYSVSHM
    437       1.1      fvdl /*
    438      1.30  jdolecek  * shmget(2). Just make sure the Linux-compatible shmat() semantics
    439      1.30  jdolecek  * is enabled for the segment, so that shmat() succeeds even when
    440      1.30  jdolecek  * the segment would be removed.
    441      1.30  jdolecek  */
    442      1.30  jdolecek int
    443      1.42       dsl linux_sys_shmget(struct lwp *l, const struct linux_sys_shmget_args *uap, register_t *retval)
    444      1.30  jdolecek {
    445      1.42       dsl 	/* {
    446      1.30  jdolecek 		syscallarg(key_t) key;
    447      1.30  jdolecek 		syscallarg(size_t) size;
    448      1.30  jdolecek 		syscallarg(int) shmflg;
    449      1.42       dsl 	} */
    450      1.42       dsl 	struct sys_shmget_args bsd_ua;
    451      1.30  jdolecek 
    452      1.42       dsl 	SCARG(&bsd_ua, key) = SCARG(uap, key);
    453      1.42       dsl 	SCARG(&bsd_ua, size) = SCARG(uap, size);
    454      1.42       dsl 	SCARG(&bsd_ua, shmflg) = SCARG(uap, shmflg) | _SHM_RMLINGER;
    455      1.42       dsl 
    456      1.42       dsl 	return sys_shmget(l, &bsd_ua, retval);
    457      1.30  jdolecek }
    458      1.30  jdolecek 
    459      1.30  jdolecek /*
    460       1.1      fvdl  * shmat(2). Very straightforward, except that Linux passes a pointer
    461       1.1      fvdl  * in which the return value is to be passed. This is subsequently
    462       1.1      fvdl  * handled by libc, apparently.
    463       1.1      fvdl  */
    464      1.36      manu #ifndef __amd64__
    465      1.14       erh int
    466      1.42       dsl linux_sys_shmat(struct lwp *l, const struct linux_sys_shmat_args *uap, register_t *retval)
    467       1.1      fvdl {
    468      1.42       dsl 	/* {
    469      1.14       erh 		syscallarg(int) shmid;
    470      1.14       erh 		syscallarg(void *) shmaddr;
    471      1.14       erh 		syscallarg(int) shmflg;
    472      1.14       erh 		syscallarg(u_long *) raddr;
    473      1.42       dsl 	} */
    474       1.1      fvdl 	int error;
    475       1.1      fvdl 
    476      1.42       dsl 	if ((error = sys_shmat(l, (const void *)uap, retval)))
    477       1.1      fvdl 		return error;
    478       1.1      fvdl 
    479      1.42       dsl 	if ((error = copyout(&retval[0], SCARG(uap, raddr), sizeof retval[0])))
    480       1.1      fvdl 		return error;
    481      1.31     perry 
    482       1.1      fvdl 	retval[0] = 0;
    483       1.1      fvdl 	return 0;
    484       1.1      fvdl }
    485      1.36      manu #endif /* __amd64__ */
    486       1.1      fvdl 
    487       1.1      fvdl /*
    488       1.1      fvdl  * Convert between Linux and NetBSD shmid_ds structures.
    489       1.1      fvdl  * The order of the fields is once again the difference, and
    490       1.1      fvdl  * we also need a place to store the internal data pointer
    491       1.1      fvdl  * in, which is unfortunately stored in this structure.
    492       1.1      fvdl  *
    493       1.1      fvdl  * We abuse a Linux internal field for that.
    494       1.1      fvdl  */
    495      1.14       erh void
    496      1.41       dsl linux_to_bsd_shmid_ds(struct linux_shmid_ds *lsp, struct shmid_ds *bsp)
    497       1.1      fvdl {
    498       1.8   mycroft 
    499       1.1      fvdl 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    500       1.1      fvdl 	bsp->shm_segsz = lsp->l_shm_segsz;
    501       1.1      fvdl 	bsp->shm_lpid = lsp->l_shm_lpid;
    502       1.1      fvdl 	bsp->shm_cpid = lsp->l_shm_cpid;
    503       1.1      fvdl 	bsp->shm_nattch = lsp->l_shm_nattch;
    504       1.1      fvdl 	bsp->shm_atime = lsp->l_shm_atime;
    505       1.1      fvdl 	bsp->shm_dtime = lsp->l_shm_dtime;
    506       1.1      fvdl 	bsp->shm_ctime = lsp->l_shm_ctime;
    507      1.21   thorpej 	bsp->_shm_internal = lsp->l_private2;	/* XXX Oh well. */
    508       1.1      fvdl }
    509       1.1      fvdl 
    510      1.14       erh void
    511      1.41       dsl linux_to_bsd_shmid64_ds(struct linux_shmid64_ds *lsp, struct shmid_ds *bsp)
    512      1.32  christos {
    513      1.32  christos 
    514      1.32  christos 	linux_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    515      1.32  christos 	bsp->shm_segsz = lsp->l_shm_segsz;
    516      1.32  christos 	bsp->shm_lpid = lsp->l_shm_lpid;
    517      1.32  christos 	bsp->shm_cpid = lsp->l_shm_cpid;
    518      1.32  christos 	bsp->shm_nattch = lsp->l_shm_nattch;
    519      1.32  christos 	bsp->shm_atime = lsp->l_shm_atime;
    520      1.32  christos 	bsp->shm_dtime = lsp->l_shm_dtime;
    521      1.32  christos 	bsp->shm_ctime = lsp->l_shm_ctime;
    522      1.32  christos 	bsp->_shm_internal = (void*)lsp->l___unused5;	/* XXX Oh well. */
    523      1.32  christos }
    524      1.32  christos 
    525      1.32  christos void
    526      1.41       dsl bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct linux_shmid_ds *lsp)
    527       1.1      fvdl {
    528       1.8   mycroft 
    529       1.1      fvdl 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    530       1.1      fvdl 	lsp->l_shm_segsz = bsp->shm_segsz;
    531       1.1      fvdl 	lsp->l_shm_lpid = bsp->shm_lpid;
    532       1.1      fvdl 	lsp->l_shm_cpid = bsp->shm_cpid;
    533       1.1      fvdl 	lsp->l_shm_nattch = bsp->shm_nattch;
    534       1.1      fvdl 	lsp->l_shm_atime = bsp->shm_atime;
    535       1.1      fvdl 	lsp->l_shm_dtime = bsp->shm_dtime;
    536       1.1      fvdl 	lsp->l_shm_ctime = bsp->shm_ctime;
    537      1.21   thorpej 	lsp->l_private2 = bsp->_shm_internal;	/* XXX */
    538       1.1      fvdl }
    539       1.1      fvdl 
    540      1.32  christos void
    541      1.41       dsl bsd_to_linux_shmid64_ds(struct shmid_ds *bsp, struct linux_shmid64_ds *lsp)
    542      1.32  christos {
    543      1.32  christos 	bsd_to_linux_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    544      1.32  christos 	lsp->l_shm_segsz = bsp->shm_segsz;
    545      1.32  christos 	lsp->l_shm_lpid = bsp->shm_lpid;
    546      1.32  christos 	lsp->l_shm_cpid = bsp->shm_cpid;
    547      1.32  christos 	lsp->l_shm_nattch = bsp->shm_nattch;
    548      1.32  christos 	lsp->l_shm_atime = bsp->shm_atime;
    549      1.32  christos 	lsp->l_shm_dtime = bsp->shm_dtime;
    550      1.32  christos 	lsp->l_shm_ctime = bsp->shm_ctime;
    551      1.32  christos 	lsp->l___unused5 = (u_long)bsp->_shm_internal;	/* XXX */
    552      1.32  christos }
    553      1.32  christos 
    554       1.1      fvdl /*
    555      1.46     njoly  * shmctl.
    556       1.1      fvdl  *
    557       1.1      fvdl  * The usual structure conversion and massaging is done.
    558       1.1      fvdl  */
    559      1.14       erh int
    560      1.42       dsl linux_sys_shmctl(struct lwp *l, const struct linux_sys_shmctl_args *uap, register_t *retval)
    561       1.1      fvdl {
    562      1.42       dsl 	/* {
    563      1.14       erh 		syscallarg(int) shmid;
    564      1.14       erh 		syscallarg(int) cmd;
    565      1.14       erh 		syscallarg(struct linux_shmid_ds *) buf;
    566      1.42       dsl 	} */
    567      1.40       dsl 	struct shmid_ds bs;
    568      1.13   mycroft 	struct linux_shmid_ds ls;
    569      1.32  christos 	struct linux_shmid64_ds ls64;
    570      1.32  christos 	struct linux_shminfo64 lsi64;
    571      1.32  christos 	struct linux_shm_info lsi;
    572      1.43     njoly 	int error, i, cmd, shmid;
    573       1.1      fvdl 
    574      1.43     njoly 	shmid = SCARG(uap, shmid);
    575      1.32  christos 	cmd = SCARG(uap, cmd);
    576      1.47     njoly #ifdef LINUX_IPC_FORCE64
    577      1.52     njoly 	cmd |= LINUX_IPC_64;
    578      1.44     njoly #endif
    579      1.43     njoly 
    580      1.52     njoly 	switch (cmd & ~LINUX_IPC_64) {
    581      1.52     njoly 	case LINUX_SHM_STAT:
    582      1.52     njoly 		shmid = IXSEQ_TO_IPCID(shmid, shmsegs[shmid].shm_perm);
    583      1.52     njoly 		retval[0] = shmid;
    584      1.52     njoly 		/*FALLTHROUGH*/
    585      1.52     njoly 
    586       1.1      fvdl 	case LINUX_IPC_STAT:
    587      1.43     njoly 		error = shmctl1(l, shmid, IPC_STAT, &bs);
    588      1.40       dsl 		if (error != 0)
    589       1.1      fvdl 			return error;
    590      1.52     njoly 		if (cmd & LINUX_IPC_64) {
    591      1.52     njoly 			bsd_to_linux_shmid64_ds(&bs, &ls64);
    592      1.52     njoly 			error = copyout(&ls64, SCARG(uap, buf), sizeof ls64);
    593      1.52     njoly 		} else {
    594      1.52     njoly 			bsd_to_linux_shmid_ds(&bs, &ls);
    595      1.52     njoly 			error = copyout(&ls, SCARG(uap, buf), sizeof ls);
    596      1.52     njoly 		}
    597      1.52     njoly 		return error;
    598      1.32  christos 
    599      1.52     njoly 	case LINUX_IPC_SET:
    600      1.52     njoly 		if (cmd & LINUX_IPC_64) {
    601      1.52     njoly 			error = copyin(SCARG(uap, buf), &ls64, sizeof ls64);
    602      1.52     njoly 			linux_to_bsd_shmid64_ds(&ls64, &bs);
    603      1.52     njoly 		} else {
    604      1.52     njoly 			error = copyin(SCARG(uap, buf), &ls, sizeof ls);
    605      1.52     njoly 			linux_to_bsd_shmid_ds(&ls, &bs);
    606      1.43     njoly 		}
    607      1.40       dsl 		if (error != 0)
    608      1.32  christos 			return error;
    609      1.48     njoly 		return shmctl1(l, shmid, IPC_SET, &bs);
    610      1.48     njoly 
    611       1.1      fvdl 	case LINUX_IPC_RMID:
    612      1.43     njoly 		return shmctl1(l, shmid, IPC_RMID, NULL);
    613      1.32  christos 
    614       1.1      fvdl 	case LINUX_SHM_LOCK:
    615      1.43     njoly 		return shmctl1(l, shmid, SHM_LOCK, NULL);
    616      1.32  christos 
    617       1.1      fvdl 	case LINUX_SHM_UNLOCK:
    618      1.43     njoly 		return shmctl1(l, shmid, SHM_UNLOCK, NULL);
    619      1.32  christos 
    620       1.1      fvdl 	case LINUX_IPC_INFO:
    621      1.32  christos 		memset(&lsi64, 0, sizeof lsi64);
    622      1.32  christos 		lsi64.l_shmmax = shminfo.shmmax;
    623      1.32  christos 		lsi64.l_shmmin = shminfo.shmmin;
    624      1.32  christos 		lsi64.l_shmmni = shminfo.shmmni;
    625      1.32  christos 		lsi64.l_shmseg = shminfo.shmseg;
    626      1.32  christos 		lsi64.l_shmall = shminfo.shmall;
    627      1.43     njoly 		for (i = shminfo.shmmni - 1; i > 0; i--)
    628      1.43     njoly 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    629      1.43     njoly 				break;
    630      1.43     njoly 		retval[0] = i;
    631      1.32  christos 		return copyout(&lsi64, SCARG(uap, buf), sizeof lsi64);
    632      1.32  christos 
    633       1.1      fvdl 	case LINUX_SHM_INFO:
    634      1.32  christos 		(void)memset(&lsi, 0, sizeof lsi);
    635      1.32  christos 		lsi.l_used_ids = shm_nused;
    636      1.32  christos 		for (i = 0; i < shminfo.shmmni; i++)
    637      1.32  christos 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    638      1.43     njoly 				lsi.l_shm_tot +=
    639      1.43     njoly 				    round_page(shmsegs[i].shm_segsz) /
    640      1.43     njoly 				    uvmexp.pagesize;
    641      1.32  christos 		lsi.l_shm_rss = 0;
    642      1.32  christos 		lsi.l_shm_swp = 0;
    643      1.32  christos 		lsi.l_swap_attempts = 0;
    644      1.32  christos 		lsi.l_swap_successes = 0;
    645      1.43     njoly 		for (i = shminfo.shmmni - 1; i > 0; i--)
    646      1.43     njoly 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    647      1.43     njoly 				break;
    648      1.43     njoly 		retval[0] = i;
    649      1.32  christos 		return copyout(&lsi, SCARG(uap, buf), sizeof lsi);
    650      1.32  christos 
    651       1.1      fvdl 	default:
    652      1.32  christos #ifdef DEBUG
    653      1.32  christos 		printf("linux_sys_shmctl cmd %d\n", SCARG(uap, cmd));
    654      1.32  christos #endif
    655       1.1      fvdl 		return EINVAL;
    656       1.1      fvdl 	}
    657       1.1      fvdl }
    658       1.1      fvdl #endif /* SYSVSHM */
    659