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