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