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