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