Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.56.4.1
      1  1.56.4.1    martin /*	$NetBSD: linux_ipc.c,v 1.56.4.1 2019/09/13 06:25:25 martin 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.56.4.1    martin __KERNEL_RCSID(0, "$NetBSD: linux_ipc.c,v 1.56.4.1 2019/09/13 06:25:25 martin 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.55     alnsn  *	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.56       mrg 	memset(lpp, 0, sizeof *lpp);
    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.56       mrg 
    134      1.56       mrg 	memset(lpp, 0, sizeof *lpp);
    135      1.32  christos 	lpp->l_key = bpp->_key;
    136      1.32  christos 	lpp->l_uid = bpp->uid;
    137      1.32  christos 	lpp->l_gid = bpp->gid;
    138      1.32  christos 	lpp->l_cuid = bpp->cuid;
    139      1.32  christos 	lpp->l_cgid = bpp->cgid;
    140      1.32  christos 	lpp->l_mode = bpp->mode;
    141      1.32  christos 	lpp->l_seq = bpp->_seq;
    142      1.32  christos }
    143      1.32  christos 
    144      1.11      fvdl #endif
    145       1.1      fvdl 
    146       1.1      fvdl #ifdef SYSVSEM
    147       1.1      fvdl /*
    148       1.6      fvdl  * Semaphore operations. Most constants and structures are the same on
    149       1.6      fvdl  * both systems. Only semctl() needs some extra work.
    150       1.1      fvdl  */
    151       1.6      fvdl 
    152       1.6      fvdl /*
    153       1.6      fvdl  * Convert between Linux and NetBSD semid_ds structures.
    154       1.6      fvdl  */
    155      1.14       erh void
    156      1.41       dsl bsd_to_linux_semid_ds(struct semid_ds *bs, struct linux_semid_ds *ls)
    157       1.6      fvdl {
    158      1.56       mrg 
    159      1.56       mrg 	memset(ls, 0, sizeof *ls);
    160       1.6      fvdl 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    161       1.6      fvdl 	ls->l_sem_otime = bs->sem_otime;
    162       1.6      fvdl 	ls->l_sem_ctime = bs->sem_ctime;
    163       1.6      fvdl 	ls->l_sem_nsems = bs->sem_nsems;
    164       1.6      fvdl }
    165       1.6      fvdl 
    166      1.14       erh void
    167      1.49     njoly bsd_to_linux_semid64_ds(struct semid_ds *bs, struct linux_semid64_ds *ls)
    168      1.49     njoly {
    169      1.56       mrg 
    170      1.56       mrg 	memset(ls, 0, sizeof *ls);
    171      1.49     njoly 	bsd_to_linux_ipc64_perm(&bs->sem_perm, &ls->l_sem_perm);
    172      1.49     njoly 	ls->l_sem_otime = bs->sem_otime;
    173      1.49     njoly 	ls->l_sem_ctime = bs->sem_ctime;
    174      1.49     njoly 	ls->l_sem_nsems = bs->sem_nsems;
    175      1.49     njoly }
    176      1.49     njoly 
    177      1.49     njoly void
    178      1.41       dsl linux_to_bsd_semid_ds(struct linux_semid_ds *ls, struct semid_ds *bs)
    179       1.6      fvdl {
    180      1.56       mrg 
    181       1.6      fvdl 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    182       1.6      fvdl 	bs->sem_otime = ls->l_sem_otime;
    183       1.6      fvdl 	bs->sem_ctime = ls->l_sem_ctime;
    184       1.6      fvdl 	bs->sem_nsems = ls->l_sem_nsems;
    185       1.6      fvdl }
    186       1.6      fvdl 
    187      1.49     njoly void
    188      1.49     njoly linux_to_bsd_semid64_ds(struct linux_semid64_ds *ls, struct semid_ds *bs)
    189      1.49     njoly {
    190      1.56       mrg 
    191      1.49     njoly 	linux_to_bsd_ipc64_perm(&ls->l_sem_perm, &bs->sem_perm);
    192      1.49     njoly 	bs->sem_otime = ls->l_sem_otime;
    193      1.49     njoly 	bs->sem_ctime = ls->l_sem_ctime;
    194      1.49     njoly 	bs->sem_nsems = ls->l_sem_nsems;
    195      1.49     njoly }
    196      1.49     njoly 
    197       1.6      fvdl /*
    198      1.21   thorpej  * Most of this can be handled by directly passing the arguments on; we
    199      1.21   thorpej  * just need to frob the `cmd' and convert the semid_ds and semun.
    200       1.6      fvdl  */
    201       1.1      fvdl int
    202      1.42       dsl linux_sys_semctl(struct lwp *l, const struct linux_sys_semctl_args *uap, register_t *retval)
    203       1.1      fvdl {
    204      1.42       dsl 	/* {
    205      1.14       erh 		syscallarg(int) semid;
    206      1.14       erh 		syscallarg(int) semnum;
    207      1.14       erh 		syscallarg(int) cmd;
    208      1.14       erh 		syscallarg(union linux_semun) arg;
    209      1.42       dsl 	} */
    210      1.21   thorpej 	struct semid_ds sembuf;
    211      1.21   thorpej 	struct linux_semid_ds lsembuf;
    212      1.49     njoly 	struct linux_semid64_ds lsembuf64;
    213      1.21   thorpej 	union __semun semun;
    214      1.49     njoly 	int cmd, lcmd, error;
    215      1.21   thorpej 	void *pass_arg = NULL;
    216      1.21   thorpej 
    217      1.49     njoly 	lcmd = SCARG(uap, cmd);
    218      1.49     njoly #ifdef LINUX_IPC_FORCE64
    219      1.53     njoly 	lcmd |= LINUX_IPC_64;
    220      1.49     njoly #endif
    221      1.21   thorpej 
    222      1.53     njoly 	switch (lcmd & ~LINUX_IPC_64) {
    223      1.21   thorpej 	case LINUX_IPC_SET:
    224      1.53     njoly 		if (lcmd & LINUX_IPC_64) {
    225      1.53     njoly 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf64,
    226      1.53     njoly 		    	    sizeof(lsembuf64));
    227      1.53     njoly 			linux_to_bsd_semid64_ds(&lsembuf64, &sembuf);
    228      1.53     njoly 		} else {
    229      1.53     njoly 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
    230      1.53     njoly 		    	   sizeof(lsembuf));
    231      1.53     njoly 			linux_to_bsd_semid_ds(&lsembuf, &sembuf);
    232      1.53     njoly 		}
    233      1.49     njoly 		if (error)
    234      1.49     njoly 			return (error);
    235      1.21   thorpej 		pass_arg = &sembuf;
    236      1.21   thorpej 		cmd = IPC_SET;
    237      1.21   thorpej 		break;
    238       1.6      fvdl 
    239      1.13   mycroft 	case LINUX_IPC_STAT:
    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.54     joerg 	memset(bmp, 0, sizeof(*bmp));
    317       1.6      fvdl 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    318      1.21   thorpej 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    319       1.6      fvdl 	bmp->msg_qnum = lmp->l_msg_qnum;
    320       1.6      fvdl 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    321       1.6      fvdl 	bmp->msg_lspid = lmp->l_msg_lspid;
    322       1.6      fvdl 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    323       1.6      fvdl 	bmp->msg_stime = lmp->l_msg_stime;
    324       1.6      fvdl 	bmp->msg_rtime = lmp->l_msg_rtime;
    325       1.6      fvdl 	bmp->msg_ctime = lmp->l_msg_ctime;
    326       1.6      fvdl }
    327       1.6      fvdl 
    328      1.14       erh void
    329      1.51     njoly linux_to_bsd_msqid64_ds(struct linux_msqid64_ds *lmp, struct msqid_ds *bmp)
    330      1.51     njoly {
    331      1.54     joerg 
    332      1.54     joerg 	memset(bmp, 0, sizeof(*bmp));
    333      1.51     njoly 	linux_to_bsd_ipc64_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    334      1.56       mrg 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    335      1.51     njoly 	bmp->msg_stime = lmp->l_msg_stime;
    336      1.51     njoly 	bmp->msg_rtime = lmp->l_msg_rtime;
    337      1.51     njoly 	bmp->msg_ctime = lmp->l_msg_ctime;
    338      1.51     njoly 	bmp->msg_qnum = lmp->l_msg_qnum;
    339      1.51     njoly 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    340      1.51     njoly 	bmp->msg_lspid = lmp->l_msg_lspid;
    341      1.51     njoly 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    342      1.51     njoly }
    343      1.51     njoly 
    344      1.51     njoly void
    345      1.41       dsl bsd_to_linux_msqid_ds(struct msqid_ds *bmp, struct linux_msqid_ds *lmp)
    346       1.6      fvdl {
    347       1.8   mycroft 
    348      1.54     joerg 	memset(lmp, 0, sizeof(*lmp));
    349       1.6      fvdl 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    350      1.21   thorpej 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    351       1.6      fvdl 	lmp->l_msg_qnum = bmp->msg_qnum;
    352       1.6      fvdl 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    353       1.6      fvdl 	lmp->l_msg_lspid = bmp->msg_lspid;
    354       1.6      fvdl 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    355       1.6      fvdl 	lmp->l_msg_stime = bmp->msg_stime;
    356       1.6      fvdl 	lmp->l_msg_rtime = bmp->msg_rtime;
    357       1.6      fvdl 	lmp->l_msg_ctime = bmp->msg_ctime;
    358       1.6      fvdl }
    359       1.6      fvdl 
    360      1.51     njoly void
    361      1.51     njoly bsd_to_linux_msqid64_ds(struct msqid_ds *bmp, struct linux_msqid64_ds *lmp)
    362      1.51     njoly {
    363      1.54     joerg 
    364      1.54     joerg 	memset(lmp, 0, sizeof(*lmp));
    365      1.51     njoly 	bsd_to_linux_ipc64_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    366      1.56       mrg 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    367      1.51     njoly 	lmp->l_msg_stime = bmp->msg_stime;
    368      1.51     njoly 	lmp->l_msg_rtime = bmp->msg_rtime;
    369      1.51     njoly 	lmp->l_msg_ctime = bmp->msg_ctime;
    370      1.51     njoly 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    371      1.51     njoly 	lmp->l_msg_qnum = bmp->msg_qnum;
    372      1.51     njoly 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    373      1.51     njoly 	lmp->l_msg_lspid = bmp->msg_lspid;
    374      1.51     njoly 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    375      1.51     njoly }
    376      1.51     njoly 
    377      1.14       erh int
    378      1.42       dsl linux_sys_msgctl(struct lwp *l, const struct linux_sys_msgctl_args *uap, register_t *retval)
    379       1.1      fvdl {
    380      1.42       dsl 	/* {
    381      1.14       erh 		syscallarg(int) msqid;
    382      1.14       erh 		syscallarg(int) cmd;
    383      1.14       erh 		syscallarg(struct linux_msqid_ds *) buf;
    384      1.42       dsl 	} */
    385      1.51     njoly 	struct msqid_ds bm, *bmp = NULL;
    386       1.6      fvdl 	struct linux_msqid_ds lm;
    387      1.51     njoly 	struct linux_msqid64_ds lm64;
    388      1.51     njoly 	int cmd, lcmd, error;
    389      1.51     njoly 
    390      1.51     njoly 	lcmd = SCARG(uap, cmd);
    391      1.51     njoly #ifdef LINUX_IPC_FORCE64
    392      1.53     njoly 	lcmd |= LINUX_IPC_64;
    393      1.51     njoly #endif
    394       1.6      fvdl 
    395      1.53     njoly 	switch (lcmd & ~LINUX_IPC_64) {
    396      1.13   mycroft 	case LINUX_IPC_STAT:
    397      1.51     njoly 		cmd = IPC_STAT;
    398      1.51     njoly 		bmp = &bm;
    399      1.51     njoly 		break;
    400       1.6      fvdl 	case LINUX_IPC_SET:
    401      1.53     njoly 		if (lcmd & LINUX_IPC_64) {
    402      1.53     njoly 			error = copyin(SCARG(uap, buf), &lm64, sizeof lm64);
    403      1.53     njoly 			linux_to_bsd_msqid64_ds(&lm64, &bm);
    404      1.53     njoly 		} else {
    405      1.53     njoly 			error = copyin(SCARG(uap, buf), &lm, sizeof lm);
    406      1.53     njoly 			linux_to_bsd_msqid_ds(&lm, &bm);
    407      1.53     njoly 		}
    408      1.53     njoly 		if (error)
    409      1.51     njoly 			return error;
    410      1.51     njoly 		cmd = IPC_SET;
    411      1.51     njoly 		bmp = &bm;
    412      1.51     njoly 		break;
    413      1.13   mycroft 	case LINUX_IPC_RMID:
    414      1.51     njoly 		cmd = IPC_RMID;
    415      1.13   mycroft 		break;
    416      1.13   mycroft 	default:
    417      1.13   mycroft 		return EINVAL;
    418       1.6      fvdl 	}
    419      1.51     njoly 
    420      1.51     njoly 	if ((error = msgctl1(l, SCARG(uap, msqid), cmd, bmp)))
    421      1.51     njoly 		return error;
    422      1.51     njoly 
    423      1.51     njoly 	switch (lcmd) {
    424      1.51     njoly 	case LINUX_IPC_STAT:
    425      1.51     njoly 		bsd_to_linux_msqid_ds(&bm, &lm);
    426      1.51     njoly 		error = copyout(&lm, SCARG(uap, buf), sizeof lm);
    427      1.51     njoly 		break;
    428      1.51     njoly 	case LINUX_IPC_STAT|LINUX_IPC_64:
    429      1.51     njoly 		bsd_to_linux_msqid64_ds(&bm, &lm64);
    430      1.51     njoly 		error = copyout(&lm64, SCARG(uap, buf), sizeof lm64);
    431      1.51     njoly 		break;
    432      1.51     njoly 	default:
    433      1.51     njoly 		break;
    434      1.51     njoly 	}
    435      1.51     njoly 
    436      1.51     njoly 	return error;
    437       1.1      fvdl }
    438       1.1      fvdl #endif /* SYSVMSG */
    439       1.1      fvdl 
    440       1.1      fvdl #ifdef SYSVSHM
    441       1.1      fvdl /*
    442      1.30  jdolecek  * shmget(2). Just make sure the Linux-compatible shmat() semantics
    443      1.30  jdolecek  * is enabled for the segment, so that shmat() succeeds even when
    444      1.30  jdolecek  * the segment would be removed.
    445      1.30  jdolecek  */
    446      1.30  jdolecek int
    447      1.42       dsl linux_sys_shmget(struct lwp *l, const struct linux_sys_shmget_args *uap, register_t *retval)
    448      1.30  jdolecek {
    449      1.42       dsl 	/* {
    450      1.30  jdolecek 		syscallarg(key_t) key;
    451      1.30  jdolecek 		syscallarg(size_t) size;
    452      1.30  jdolecek 		syscallarg(int) shmflg;
    453      1.42       dsl 	} */
    454      1.42       dsl 	struct sys_shmget_args bsd_ua;
    455      1.30  jdolecek 
    456      1.42       dsl 	SCARG(&bsd_ua, key) = SCARG(uap, key);
    457      1.42       dsl 	SCARG(&bsd_ua, size) = SCARG(uap, size);
    458      1.42       dsl 	SCARG(&bsd_ua, shmflg) = SCARG(uap, shmflg) | _SHM_RMLINGER;
    459      1.42       dsl 
    460      1.42       dsl 	return sys_shmget(l, &bsd_ua, retval);
    461      1.30  jdolecek }
    462      1.30  jdolecek 
    463      1.30  jdolecek /*
    464       1.1      fvdl  * shmat(2). Very straightforward, except that Linux passes a pointer
    465       1.1      fvdl  * in which the return value is to be passed. This is subsequently
    466       1.1      fvdl  * handled by libc, apparently.
    467       1.1      fvdl  */
    468      1.36      manu #ifndef __amd64__
    469      1.14       erh int
    470      1.42       dsl linux_sys_shmat(struct lwp *l, const struct linux_sys_shmat_args *uap, register_t *retval)
    471       1.1      fvdl {
    472      1.42       dsl 	/* {
    473      1.14       erh 		syscallarg(int) shmid;
    474      1.14       erh 		syscallarg(void *) shmaddr;
    475      1.14       erh 		syscallarg(int) shmflg;
    476      1.14       erh 		syscallarg(u_long *) raddr;
    477      1.42       dsl 	} */
    478       1.1      fvdl 	int error;
    479       1.1      fvdl 
    480      1.42       dsl 	if ((error = sys_shmat(l, (const void *)uap, retval)))
    481       1.1      fvdl 		return error;
    482       1.1      fvdl 
    483      1.42       dsl 	if ((error = copyout(&retval[0], SCARG(uap, raddr), sizeof retval[0])))
    484       1.1      fvdl 		return error;
    485      1.31     perry 
    486       1.1      fvdl 	retval[0] = 0;
    487       1.1      fvdl 	return 0;
    488       1.1      fvdl }
    489      1.36      manu #endif /* __amd64__ */
    490       1.1      fvdl 
    491       1.1      fvdl /*
    492       1.1      fvdl  * Convert between Linux and NetBSD shmid_ds structures.
    493       1.1      fvdl  * The order of the fields is once again the difference, and
    494       1.1      fvdl  * we also need a place to store the internal data pointer
    495       1.1      fvdl  * in, which is unfortunately stored in this structure.
    496       1.1      fvdl  *
    497       1.1      fvdl  * We abuse a Linux internal field for that.
    498       1.1      fvdl  */
    499      1.14       erh void
    500      1.41       dsl linux_to_bsd_shmid_ds(struct linux_shmid_ds *lsp, struct shmid_ds *bsp)
    501       1.1      fvdl {
    502       1.8   mycroft 
    503       1.1      fvdl 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    504       1.1      fvdl 	bsp->shm_segsz = lsp->l_shm_segsz;
    505       1.1      fvdl 	bsp->shm_lpid = lsp->l_shm_lpid;
    506       1.1      fvdl 	bsp->shm_cpid = lsp->l_shm_cpid;
    507       1.1      fvdl 	bsp->shm_nattch = lsp->l_shm_nattch;
    508       1.1      fvdl 	bsp->shm_atime = lsp->l_shm_atime;
    509       1.1      fvdl 	bsp->shm_dtime = lsp->l_shm_dtime;
    510       1.1      fvdl 	bsp->shm_ctime = lsp->l_shm_ctime;
    511       1.1      fvdl }
    512       1.1      fvdl 
    513      1.14       erh void
    514      1.41       dsl linux_to_bsd_shmid64_ds(struct linux_shmid64_ds *lsp, struct shmid_ds *bsp)
    515      1.32  christos {
    516      1.32  christos 
    517      1.32  christos 	linux_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    518      1.32  christos 	bsp->shm_segsz = lsp->l_shm_segsz;
    519      1.32  christos 	bsp->shm_lpid = lsp->l_shm_lpid;
    520      1.32  christos 	bsp->shm_cpid = lsp->l_shm_cpid;
    521      1.32  christos 	bsp->shm_nattch = lsp->l_shm_nattch;
    522      1.32  christos 	bsp->shm_atime = lsp->l_shm_atime;
    523      1.32  christos 	bsp->shm_dtime = lsp->l_shm_dtime;
    524      1.32  christos 	bsp->shm_ctime = lsp->l_shm_ctime;
    525      1.32  christos }
    526      1.32  christos 
    527      1.32  christos void
    528      1.41       dsl bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct linux_shmid_ds *lsp)
    529       1.1      fvdl {
    530       1.8   mycroft 
    531      1.56       mrg 	memset(lsp, 0, sizeof *lsp);
    532       1.1      fvdl 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    533       1.1      fvdl 	lsp->l_shm_segsz = bsp->shm_segsz;
    534       1.1      fvdl 	lsp->l_shm_lpid = bsp->shm_lpid;
    535       1.1      fvdl 	lsp->l_shm_cpid = bsp->shm_cpid;
    536       1.1      fvdl 	lsp->l_shm_nattch = bsp->shm_nattch;
    537       1.1      fvdl 	lsp->l_shm_atime = bsp->shm_atime;
    538       1.1      fvdl 	lsp->l_shm_dtime = bsp->shm_dtime;
    539       1.1      fvdl 	lsp->l_shm_ctime = bsp->shm_ctime;
    540       1.1      fvdl }
    541       1.1      fvdl 
    542      1.32  christos void
    543      1.41       dsl bsd_to_linux_shmid64_ds(struct shmid_ds *bsp, struct linux_shmid64_ds *lsp)
    544      1.32  christos {
    545      1.56       mrg 
    546      1.56       mrg 	memset(lsp, 0, sizeof *lsp);
    547      1.32  christos 	bsd_to_linux_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    548      1.32  christos 	lsp->l_shm_segsz = bsp->shm_segsz;
    549      1.32  christos 	lsp->l_shm_lpid = bsp->shm_lpid;
    550      1.32  christos 	lsp->l_shm_cpid = bsp->shm_cpid;
    551      1.32  christos 	lsp->l_shm_nattch = bsp->shm_nattch;
    552      1.32  christos 	lsp->l_shm_atime = bsp->shm_atime;
    553      1.32  christos 	lsp->l_shm_dtime = bsp->shm_dtime;
    554      1.32  christos 	lsp->l_shm_ctime = bsp->shm_ctime;
    555      1.32  christos }
    556      1.32  christos 
    557       1.1      fvdl /*
    558      1.46     njoly  * shmctl.
    559       1.1      fvdl  *
    560       1.1      fvdl  * The usual structure conversion and massaging is done.
    561       1.1      fvdl  */
    562      1.14       erh int
    563      1.42       dsl linux_sys_shmctl(struct lwp *l, const struct linux_sys_shmctl_args *uap, register_t *retval)
    564       1.1      fvdl {
    565      1.42       dsl 	/* {
    566      1.14       erh 		syscallarg(int) shmid;
    567      1.14       erh 		syscallarg(int) cmd;
    568      1.14       erh 		syscallarg(struct linux_shmid_ds *) buf;
    569      1.42       dsl 	} */
    570      1.40       dsl 	struct shmid_ds bs;
    571  1.56.4.1    martin 	struct ipc_perm perm;
    572      1.13   mycroft 	struct linux_shmid_ds ls;
    573      1.32  christos 	struct linux_shmid64_ds ls64;
    574      1.32  christos 	struct linux_shminfo64 lsi64;
    575      1.32  christos 	struct linux_shm_info lsi;
    576      1.43     njoly 	int error, i, cmd, shmid;
    577       1.1      fvdl 
    578      1.43     njoly 	shmid = SCARG(uap, shmid);
    579      1.32  christos 	cmd = SCARG(uap, cmd);
    580      1.47     njoly #ifdef LINUX_IPC_FORCE64
    581      1.52     njoly 	cmd |= LINUX_IPC_64;
    582      1.44     njoly #endif
    583      1.43     njoly 
    584      1.52     njoly 	switch (cmd & ~LINUX_IPC_64) {
    585      1.52     njoly 	case LINUX_SHM_STAT:
    586  1.56.4.1    martin 		error = shm_find_segment_perm_by_index(shmid, &perm);
    587  1.56.4.1    martin 		if (error)
    588  1.56.4.1    martin 			return error;
    589  1.56.4.1    martin 		shmid = IXSEQ_TO_IPCID(shmid, perm);
    590      1.52     njoly 		retval[0] = shmid;
    591      1.52     njoly 		/*FALLTHROUGH*/
    592      1.52     njoly 
    593       1.1      fvdl 	case LINUX_IPC_STAT:
    594      1.43     njoly 		error = shmctl1(l, shmid, IPC_STAT, &bs);
    595      1.40       dsl 		if (error != 0)
    596       1.1      fvdl 			return error;
    597      1.52     njoly 		if (cmd & LINUX_IPC_64) {
    598      1.52     njoly 			bsd_to_linux_shmid64_ds(&bs, &ls64);
    599      1.52     njoly 			error = copyout(&ls64, SCARG(uap, buf), sizeof ls64);
    600      1.52     njoly 		} else {
    601      1.52     njoly 			bsd_to_linux_shmid_ds(&bs, &ls);
    602      1.52     njoly 			error = copyout(&ls, SCARG(uap, buf), sizeof ls);
    603      1.52     njoly 		}
    604      1.52     njoly 		return error;
    605      1.32  christos 
    606      1.52     njoly 	case LINUX_IPC_SET:
    607      1.52     njoly 		if (cmd & LINUX_IPC_64) {
    608      1.52     njoly 			error = copyin(SCARG(uap, buf), &ls64, sizeof ls64);
    609      1.52     njoly 			linux_to_bsd_shmid64_ds(&ls64, &bs);
    610      1.52     njoly 		} else {
    611      1.52     njoly 			error = copyin(SCARG(uap, buf), &ls, sizeof ls);
    612      1.52     njoly 			linux_to_bsd_shmid_ds(&ls, &bs);
    613      1.43     njoly 		}
    614      1.40       dsl 		if (error != 0)
    615      1.32  christos 			return error;
    616      1.48     njoly 		return shmctl1(l, shmid, IPC_SET, &bs);
    617      1.48     njoly 
    618       1.1      fvdl 	case LINUX_IPC_RMID:
    619      1.43     njoly 		return shmctl1(l, shmid, IPC_RMID, NULL);
    620      1.32  christos 
    621       1.1      fvdl 	case LINUX_SHM_LOCK:
    622      1.43     njoly 		return shmctl1(l, shmid, SHM_LOCK, NULL);
    623      1.32  christos 
    624       1.1      fvdl 	case LINUX_SHM_UNLOCK:
    625      1.43     njoly 		return shmctl1(l, shmid, SHM_UNLOCK, NULL);
    626      1.32  christos 
    627       1.1      fvdl 	case LINUX_IPC_INFO:
    628      1.32  christos 		memset(&lsi64, 0, sizeof lsi64);
    629      1.32  christos 		lsi64.l_shmmax = shminfo.shmmax;
    630      1.32  christos 		lsi64.l_shmmin = shminfo.shmmin;
    631      1.32  christos 		lsi64.l_shmmni = shminfo.shmmni;
    632      1.32  christos 		lsi64.l_shmseg = shminfo.shmseg;
    633      1.32  christos 		lsi64.l_shmall = shminfo.shmall;
    634      1.43     njoly 		for (i = shminfo.shmmni - 1; i > 0; i--)
    635      1.43     njoly 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    636      1.43     njoly 				break;
    637      1.43     njoly 		retval[0] = i;
    638      1.32  christos 		return copyout(&lsi64, SCARG(uap, buf), sizeof lsi64);
    639      1.32  christos 
    640       1.1      fvdl 	case LINUX_SHM_INFO:
    641      1.32  christos 		(void)memset(&lsi, 0, sizeof lsi);
    642      1.32  christos 		lsi.l_used_ids = shm_nused;
    643      1.32  christos 		for (i = 0; i < shminfo.shmmni; i++)
    644      1.32  christos 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    645      1.43     njoly 				lsi.l_shm_tot +=
    646      1.43     njoly 				    round_page(shmsegs[i].shm_segsz) /
    647      1.43     njoly 				    uvmexp.pagesize;
    648      1.32  christos 		lsi.l_shm_rss = 0;
    649      1.32  christos 		lsi.l_shm_swp = 0;
    650      1.32  christos 		lsi.l_swap_attempts = 0;
    651      1.32  christos 		lsi.l_swap_successes = 0;
    652      1.43     njoly 		for (i = shminfo.shmmni - 1; i > 0; i--)
    653      1.43     njoly 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    654      1.43     njoly 				break;
    655      1.43     njoly 		retval[0] = i;
    656      1.32  christos 		return copyout(&lsi, SCARG(uap, buf), sizeof lsi);
    657      1.32  christos 
    658       1.1      fvdl 	default:
    659      1.32  christos #ifdef DEBUG
    660      1.32  christos 		printf("linux_sys_shmctl cmd %d\n", SCARG(uap, cmd));
    661      1.32  christos #endif
    662       1.1      fvdl 		return EINVAL;
    663       1.1      fvdl 	}
    664       1.1      fvdl }
    665       1.1      fvdl #endif /* SYSVSHM */
    666