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