Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.13
      1  1.13   mycroft /*	$NetBSD: linux_ipc.c,v 1.13 1998/01/22 16:33:57 mycroft Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*
      4   1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
      5   1.1      fvdl  * All rights reserved.
      6   1.1      fvdl  *
      7   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      8   1.1      fvdl  * modification, are permitted provided that the following conditions
      9   1.1      fvdl  * are met:
     10   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     11   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     12   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     15   1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     16   1.1      fvdl  *    must display the following acknowledgement:
     17   1.1      fvdl  *      This product includes software developed for the NetBSD Project
     18   1.1      fvdl  *      by Frank van der Linden
     19   1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     20   1.1      fvdl  *    derived from this software without specific prior written permission
     21   1.1      fvdl  *
     22   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1      fvdl  */
     33   1.1      fvdl 
     34   1.1      fvdl #include <sys/types.h>
     35   1.1      fvdl #include <sys/param.h>
     36   1.1      fvdl #include <sys/kernel.h>
     37   1.1      fvdl #include <sys/shm.h>
     38   1.6      fvdl #include <sys/sem.h>
     39   1.1      fvdl #include <sys/msg.h>
     40   1.1      fvdl #include <sys/proc.h>
     41   1.1      fvdl #include <sys/uio.h>
     42   1.1      fvdl #include <sys/time.h>
     43   1.1      fvdl #include <sys/malloc.h>
     44   1.1      fvdl #include <sys/mman.h>
     45   1.1      fvdl #include <sys/systm.h>
     46   1.1      fvdl #include <sys/stat.h>
     47   1.1      fvdl 
     48   1.1      fvdl #include <sys/mount.h>
     49   1.1      fvdl #include <sys/syscallargs.h>
     50   1.1      fvdl 
     51   1.1      fvdl #include <compat/linux/linux_types.h>
     52   1.5   mycroft #include <compat/linux/linux_signal.h>
     53   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     54   1.1      fvdl #include <compat/linux/linux_util.h>
     55   1.1      fvdl #include <compat/linux/linux_ipc.h>
     56   1.1      fvdl #include <compat/linux/linux_msg.h>
     57   1.1      fvdl #include <compat/linux/linux_shm.h>
     58   1.6      fvdl #include <compat/linux/linux_sem.h>
     59   1.1      fvdl #include <compat/linux/linux_ipccall.h>
     60   1.1      fvdl 
     61   1.1      fvdl /*
     62   1.1      fvdl  * Stuff to deal with the SysV ipc/shm/semaphore interface in Linux.
     63   1.1      fvdl  * The main difference is, that Linux handles it all via one
     64   1.1      fvdl  * system call, which has the usual maximum amount of 5 arguments.
     65   1.1      fvdl  * This results in a kludge for calls that take 6 of them.
     66   1.1      fvdl  *
     67   1.1      fvdl  * The SYSVXXXX options have to be enabled to get the appropriate
     68   1.1      fvdl  * functions to work.
     69   1.1      fvdl  */
     70   1.1      fvdl 
     71   1.1      fvdl #ifdef SYSVSEM
     72   1.8   mycroft static int linux_semop __P((struct proc *, struct linux_sys_ipc_args *,
     73   1.1      fvdl 				register_t *));
     74   1.8   mycroft static int linux_semget __P((struct proc *, struct linux_sys_ipc_args *,
     75   1.1      fvdl 				register_t *));
     76   1.8   mycroft static int linux_semctl __P((struct proc *, struct linux_sys_ipc_args *,
     77   1.1      fvdl 				register_t *));
     78  1.10  christos static void bsd_to_linux_semid_ds __P((struct semid_ds *,
     79  1.10  christos 				       struct linux_semid_ds *));
     80  1.10  christos static void linux_to_bsd_semid_ds __P((struct linux_semid_ds *,
     81  1.10  christos 				       struct semid_ds *));
     82   1.1      fvdl #endif
     83   1.1      fvdl 
     84   1.1      fvdl #ifdef SYSVMSG
     85   1.8   mycroft static int linux_msgsnd __P((struct proc *, struct linux_sys_ipc_args *,
     86   1.1      fvdl 				register_t *));
     87   1.8   mycroft static int linux_msgrcv __P((struct proc *, struct linux_sys_ipc_args *,
     88   1.1      fvdl 				register_t *));
     89  1.10  christos static int linux_msgget __P((struct proc *, struct linux_sys_ipc_args *,
     90   1.1      fvdl 				register_t *));
     91   1.8   mycroft static int linux_msgctl __P((struct proc *, struct linux_sys_ipc_args *,
     92   1.1      fvdl 				register_t *));
     93  1.10  christos static void linux_to_bsd_msqid_ds __P((struct linux_msqid_ds *,
     94  1.10  christos 				       struct msqid_ds *));
     95  1.10  christos static void bsd_to_linux_msqid_ds __P((struct msqid_ds *,
     96  1.10  christos 				       struct linux_msqid_ds *));
     97   1.1      fvdl #endif
     98   1.1      fvdl 
     99   1.1      fvdl #ifdef SYSVSHM
    100   1.8   mycroft static int linux_shmat __P((struct proc *, struct linux_sys_ipc_args *,
    101   1.1      fvdl 				register_t *));
    102   1.8   mycroft static int linux_shmdt __P((struct proc *, struct linux_sys_ipc_args *,
    103   1.1      fvdl 				register_t *));
    104   1.8   mycroft static int linux_shmget __P((struct proc *, struct linux_sys_ipc_args *,
    105   1.1      fvdl 				register_t *));
    106   1.8   mycroft static int linux_shmctl __P((struct proc *, struct linux_sys_ipc_args *,
    107   1.1      fvdl 				register_t *));
    108  1.10  christos static void linux_to_bsd_shmid_ds __P((struct linux_shmid_ds *,
    109  1.10  christos 				       struct shmid_ds *));
    110  1.10  christos static void bsd_to_linux_shmid_ds __P((struct shmid_ds *,
    111  1.10  christos 				       struct linux_shmid_ds *));
    112   1.1      fvdl #endif
    113   1.1      fvdl 
    114   1.1      fvdl 
    115  1.11      fvdl #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
    116  1.10  christos static void linux_to_bsd_ipc_perm __P((struct linux_ipc_perm *,
    117  1.10  christos 				       struct ipc_perm *));
    118  1.10  christos static void bsd_to_linux_ipc_perm __P((struct ipc_perm *,
    119  1.10  christos 				       struct linux_ipc_perm *));
    120  1.11      fvdl #endif
    121  1.10  christos 
    122   1.1      fvdl int
    123   1.8   mycroft linux_sys_ipc(p, v, retval)
    124   1.1      fvdl 	struct proc *p;
    125   1.7   thorpej 	void *v;
    126   1.7   thorpej 	register_t *retval;
    127   1.7   thorpej {
    128   1.8   mycroft 	struct linux_sys_ipc_args /* {
    129   1.1      fvdl 		syscallarg(int) what;
    130   1.1      fvdl 		syscallarg(int) a1;
    131   1.1      fvdl 		syscallarg(int) a2;
    132   1.1      fvdl 		syscallarg(int) a3;
    133   1.1      fvdl 		syscallarg(caddr_t) ptr;
    134   1.7   thorpej 	} */ *uap = v;
    135   1.1      fvdl 
    136   1.1      fvdl 	switch (SCARG(uap, what)) {
    137   1.1      fvdl #ifdef SYSVSEM
    138   1.6      fvdl 	case LINUX_SYS_semop:
    139   1.6      fvdl 		return linux_semop(p, uap, retval);
    140   1.6      fvdl 	case LINUX_SYS_semget:
    141   1.6      fvdl 		return linux_semget(p, uap, retval);
    142   1.6      fvdl 	case LINUX_SYS_semctl:
    143   1.6      fvdl 		return linux_semctl(p, uap, retval);
    144   1.1      fvdl #endif
    145   1.1      fvdl #ifdef SYSVMSG
    146   1.6      fvdl 	case LINUX_SYS_msgsnd:
    147   1.6      fvdl 		return linux_msgsnd(p, uap, retval);
    148   1.6      fvdl 	case LINUX_SYS_msgrcv:
    149   1.6      fvdl 		return linux_msgrcv(p, uap, retval);
    150   1.6      fvdl 	case LINUX_SYS_msgget:
    151   1.6      fvdl 		return linux_msgget(p, uap, retval);
    152   1.6      fvdl 	case LINUX_SYS_msgctl:
    153   1.6      fvdl 		return linux_msgctl(p, uap, retval);
    154   1.1      fvdl #endif
    155   1.1      fvdl #ifdef SYSVSHM
    156   1.6      fvdl 	case LINUX_SYS_shmat:
    157   1.6      fvdl 		return linux_shmat(p, uap, retval);
    158   1.6      fvdl 	case LINUX_SYS_shmdt:
    159   1.6      fvdl 		return linux_shmdt(p, uap, retval);
    160   1.6      fvdl 	case LINUX_SYS_shmget:
    161   1.6      fvdl 		return linux_shmget(p, uap, retval);
    162   1.6      fvdl 	case LINUX_SYS_shmctl:
    163   1.6      fvdl 		return linux_shmctl(p, uap, retval);
    164   1.1      fvdl #endif
    165   1.6      fvdl 	default:
    166   1.6      fvdl 		return ENOSYS;
    167   1.1      fvdl 	}
    168   1.1      fvdl }
    169   1.1      fvdl 
    170  1.11      fvdl #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
    171   1.1      fvdl /*
    172   1.1      fvdl  * Convert between Linux and NetBSD ipc_perm structures. Only the
    173   1.1      fvdl  * order of the fields is different.
    174   1.1      fvdl  */
    175   1.1      fvdl static void
    176   1.1      fvdl linux_to_bsd_ipc_perm(lpp, bpp)
    177   1.1      fvdl 	struct linux_ipc_perm *lpp;
    178   1.1      fvdl 	struct ipc_perm *bpp;
    179   1.1      fvdl {
    180   1.8   mycroft 
    181   1.1      fvdl 	bpp->key = lpp->l_key;
    182   1.1      fvdl 	bpp->uid = lpp->l_uid;
    183   1.1      fvdl 	bpp->gid = lpp->l_gid;
    184   1.1      fvdl 	bpp->cuid = lpp->l_cuid;
    185   1.1      fvdl 	bpp->cgid = lpp->l_cgid;
    186   1.1      fvdl 	bpp->mode = lpp->l_mode;
    187   1.1      fvdl 	bpp->seq = lpp->l_seq;
    188   1.1      fvdl }
    189   1.1      fvdl 
    190   1.1      fvdl static void
    191   1.1      fvdl bsd_to_linux_ipc_perm(bpp, lpp)
    192   1.1      fvdl 	struct ipc_perm *bpp;
    193   1.1      fvdl 	struct linux_ipc_perm *lpp;
    194   1.1      fvdl {
    195   1.8   mycroft 
    196   1.1      fvdl 	lpp->l_key = bpp->key;
    197   1.1      fvdl 	lpp->l_uid = bpp->uid;
    198   1.1      fvdl 	lpp->l_gid = bpp->gid;
    199   1.1      fvdl 	lpp->l_cuid = bpp->cuid;
    200   1.1      fvdl 	lpp->l_cgid = bpp->cgid;
    201   1.1      fvdl 	lpp->l_mode = bpp->mode;
    202   1.1      fvdl 	lpp->l_seq = bpp->seq;
    203   1.1      fvdl }
    204  1.11      fvdl #endif
    205   1.1      fvdl 
    206   1.1      fvdl #ifdef SYSVSEM
    207   1.1      fvdl /*
    208   1.6      fvdl  * Semaphore operations. Most constants and structures are the same on
    209   1.6      fvdl  * both systems. Only semctl() needs some extra work.
    210   1.1      fvdl  */
    211   1.6      fvdl 
    212   1.6      fvdl /*
    213   1.6      fvdl  * Convert between Linux and NetBSD semid_ds structures.
    214   1.6      fvdl  */
    215   1.6      fvdl static void
    216   1.6      fvdl bsd_to_linux_semid_ds(bs, ls)
    217   1.6      fvdl 	struct semid_ds *bs;
    218   1.6      fvdl 	struct linux_semid_ds *ls;
    219   1.6      fvdl {
    220   1.8   mycroft 
    221   1.6      fvdl 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    222   1.6      fvdl 	ls->l_sem_otime = bs->sem_otime;
    223   1.6      fvdl 	ls->l_sem_ctime = bs->sem_ctime;
    224   1.6      fvdl 	ls->l_sem_nsems = bs->sem_nsems;
    225   1.8   mycroft 	ls->l_sem_base = bs->sem_base;
    226   1.6      fvdl }
    227   1.6      fvdl 
    228   1.6      fvdl static void
    229   1.6      fvdl linux_to_bsd_semid_ds(ls, bs)
    230   1.6      fvdl 	struct linux_semid_ds *ls;
    231   1.6      fvdl 	struct semid_ds *bs;
    232   1.6      fvdl {
    233   1.8   mycroft 
    234   1.6      fvdl 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    235   1.6      fvdl 	bs->sem_otime = ls->l_sem_otime;
    236   1.6      fvdl 	bs->sem_ctime = ls->l_sem_ctime;
    237   1.6      fvdl 	bs->sem_nsems = ls->l_sem_nsems;
    238   1.8   mycroft 	bs->sem_base = ls->l_sem_base;
    239   1.6      fvdl }
    240   1.6      fvdl 
    241   1.1      fvdl int
    242   1.1      fvdl linux_semop(p, uap, retval)
    243   1.1      fvdl 	struct proc *p;
    244   1.8   mycroft 	struct linux_sys_ipc_args /* {
    245   1.1      fvdl 		syscallarg(int) what;
    246   1.1      fvdl 		syscallarg(int) a1;
    247   1.1      fvdl 		syscallarg(int) a2;
    248   1.1      fvdl 		syscallarg(int) a3;
    249   1.1      fvdl 		syscallarg(caddr_t) ptr;
    250   1.1      fvdl 	} */ *uap;
    251   1.1      fvdl 	register_t *retval;
    252   1.1      fvdl {
    253   1.8   mycroft 	struct sys_semop_args bsa;
    254   1.6      fvdl 
    255   1.6      fvdl 	SCARG(&bsa, semid) = SCARG(uap, a1);
    256   1.6      fvdl 	SCARG(&bsa, sops) = (struct sembuf *)SCARG(uap, ptr);
    257   1.6      fvdl 	SCARG(&bsa, nsops) = SCARG(uap, a2);
    258   1.6      fvdl 
    259   1.8   mycroft 	return sys_semop(p, &bsa, retval);
    260   1.1      fvdl }
    261   1.1      fvdl 
    262   1.1      fvdl int
    263   1.1      fvdl linux_semget(p, uap, retval)
    264   1.1      fvdl 	struct proc *p;
    265   1.8   mycroft 	struct linux_sys_ipc_args /* {
    266   1.1      fvdl 		syscallarg(int) what;
    267   1.1      fvdl 		syscallarg(int) a1;
    268   1.1      fvdl 		syscallarg(int) a2;
    269   1.1      fvdl 		syscallarg(int) a3;
    270   1.1      fvdl 		syscallarg(caddr_t) ptr;
    271   1.1      fvdl 	} */ *uap;
    272   1.1      fvdl 	register_t *retval;
    273   1.1      fvdl {
    274   1.8   mycroft 	struct sys_semget_args bsa;
    275   1.6      fvdl 
    276   1.6      fvdl 	SCARG(&bsa, key) = (key_t)SCARG(uap, a1);
    277   1.6      fvdl 	SCARG(&bsa, nsems) = SCARG(uap, a2);
    278   1.6      fvdl 	SCARG(&bsa, semflg) = SCARG(uap, a3);
    279   1.6      fvdl 
    280   1.8   mycroft 	return sys_semget(p, &bsa, retval);
    281   1.1      fvdl }
    282   1.1      fvdl 
    283   1.6      fvdl /*
    284   1.6      fvdl  * Most of this can be handled by directly passing the arguments on,
    285   1.6      fvdl  * buf IPC_* require a lot of copy{in,out} because of the extra indirection
    286   1.6      fvdl  * (we are passed a pointer to a union cointaining a pointer to a semid_ds
    287   1.6      fvdl  * structure.
    288   1.6      fvdl  */
    289   1.1      fvdl int
    290   1.1      fvdl linux_semctl(p, uap, retval)
    291   1.1      fvdl 	struct proc *p;
    292   1.8   mycroft 	struct linux_sys_ipc_args /* {
    293   1.1      fvdl 		syscallarg(int) what;
    294   1.1      fvdl 		syscallarg(int) a1;
    295   1.1      fvdl 		syscallarg(int) a2;
    296   1.1      fvdl 		syscallarg(int) a3;
    297   1.1      fvdl 		syscallarg(caddr_t) ptr;
    298   1.1      fvdl 	} */ *uap;
    299   1.1      fvdl 	register_t *retval;
    300   1.1      fvdl {
    301  1.13   mycroft 	caddr_t sg;
    302  1.13   mycroft 	struct sys___semctl_args nua;
    303  1.13   mycroft 	struct semid_ds *bmp, bm;
    304   1.6      fvdl 	struct linux_semid_ds lm;
    305  1.13   mycroft 	union semun *bup;
    306  1.13   mycroft 	union linux_semun lu;
    307  1.13   mycroft 	int error;
    308   1.6      fvdl 
    309  1.13   mycroft 	SCARG(&nua, semid) = SCARG(uap, a1);
    310  1.13   mycroft 	SCARG(&nua, semnum) = SCARG(uap, a2);
    311  1.13   mycroft 	SCARG(&nua, arg) = (union semun *)SCARG(uap, ptr);
    312  1.13   mycroft 	switch (SCARG(uap, a3)) {
    313  1.13   mycroft 	case LINUX_IPC_STAT:
    314  1.13   mycroft 		sg = stackgap_init(p->p_emul);
    315  1.13   mycroft 		bup = stackgap_alloc(&sg, sizeof (union semun));
    316  1.13   mycroft 		bmp = stackgap_alloc(&sg, sizeof (struct semid_ds));
    317  1.13   mycroft 		if ((error = copyout(&bmp, bup, sizeof bmp)))
    318  1.13   mycroft 			return error;
    319  1.13   mycroft 		SCARG(&nua, cmd) = IPC_STAT;
    320  1.13   mycroft 		SCARG(&nua, arg) = bup;
    321  1.13   mycroft 		if ((error = sys___semctl(p, &nua, retval)))
    322  1.13   mycroft 			return error;
    323  1.13   mycroft 		if ((error = copyin(bmp, &bm, sizeof bm)))
    324  1.13   mycroft 			return error;
    325  1.13   mycroft 		bsd_to_linux_semid_ds(&bm, &lm);
    326  1.13   mycroft 		if ((error = copyin(SCARG(uap, ptr), &lu, sizeof lu)))
    327  1.13   mycroft 			return error;
    328  1.13   mycroft 		return copyout(&lm, lu.l_buf, sizeof lm);
    329  1.13   mycroft 	case LINUX_IPC_SET:
    330  1.13   mycroft 		if ((error = copyin(SCARG(uap, ptr), &lu, sizeof lu)))
    331  1.13   mycroft 			return error;
    332  1.13   mycroft 		if ((error = copyin(lu.l_buf, &lm, sizeof lm)))
    333  1.13   mycroft 			return error;
    334  1.13   mycroft 		linux_to_bsd_semid_ds(&lm, &bm);
    335  1.13   mycroft 		sg = stackgap_init(p->p_emul);
    336  1.13   mycroft 		bup = stackgap_alloc(&sg, sizeof (union semun));
    337  1.13   mycroft 		bmp = stackgap_alloc(&sg, sizeof (struct semid_ds));
    338  1.13   mycroft 		if ((error = copyout(&bm, bmp, sizeof bm)))
    339  1.13   mycroft 			return error;
    340  1.13   mycroft 		if ((error = copyout(&bmp, bup, sizeof bmp)))
    341  1.13   mycroft 			return error;
    342  1.13   mycroft 		SCARG(&nua, cmd) = IPC_SET;
    343  1.13   mycroft 		SCARG(&nua, arg) = bup;
    344  1.13   mycroft 		return sys___semctl(p, &nua, retval);
    345  1.13   mycroft 	case LINUX_IPC_RMID:
    346  1.13   mycroft 		SCARG(&nua, cmd) = IPC_RMID;
    347  1.13   mycroft 		break;
    348   1.6      fvdl 	case LINUX_GETVAL:
    349  1.13   mycroft 		SCARG(&nua, cmd) = GETVAL;
    350   1.6      fvdl 		break;
    351   1.6      fvdl 	case LINUX_GETPID:
    352  1.13   mycroft 		SCARG(&nua, cmd) = GETPID;
    353   1.6      fvdl 		break;
    354   1.6      fvdl 	case LINUX_GETNCNT:
    355  1.13   mycroft 		SCARG(&nua, cmd) = GETNCNT;
    356   1.6      fvdl 		break;
    357   1.6      fvdl 	case LINUX_GETZCNT:
    358  1.13   mycroft 		SCARG(&nua, cmd) = GETZCNT;
    359   1.6      fvdl 		break;
    360   1.6      fvdl 	case LINUX_SETVAL:
    361  1.13   mycroft 		SCARG(&nua, cmd) = SETVAL;
    362   1.6      fvdl 		break;
    363   1.6      fvdl 	default:
    364   1.6      fvdl 		return EINVAL;
    365   1.6      fvdl 	}
    366  1.13   mycroft 	return sys___semctl(p, &nua, retval);
    367   1.1      fvdl }
    368   1.1      fvdl #endif /* SYSVSEM */
    369   1.1      fvdl 
    370   1.1      fvdl #ifdef SYSVMSG
    371   1.6      fvdl 
    372   1.6      fvdl static void
    373   1.6      fvdl linux_to_bsd_msqid_ds(lmp, bmp)
    374   1.6      fvdl 	struct linux_msqid_ds *lmp;
    375   1.6      fvdl 	struct msqid_ds *bmp;
    376   1.6      fvdl {
    377   1.8   mycroft 
    378   1.6      fvdl 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    379   1.6      fvdl 	bmp->msg_first = lmp->l_msg_first;
    380   1.6      fvdl 	bmp->msg_last = lmp->l_msg_last;
    381   1.6      fvdl 	bmp->msg_cbytes = lmp->l_msg_cbytes;
    382   1.6      fvdl 	bmp->msg_qnum = lmp->l_msg_qnum;
    383   1.6      fvdl 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    384   1.6      fvdl 	bmp->msg_lspid = lmp->l_msg_lspid;
    385   1.6      fvdl 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    386   1.6      fvdl 	bmp->msg_stime = lmp->l_msg_stime;
    387   1.6      fvdl 	bmp->msg_rtime = lmp->l_msg_rtime;
    388   1.6      fvdl 	bmp->msg_ctime = lmp->l_msg_ctime;
    389   1.6      fvdl }
    390   1.6      fvdl 
    391   1.6      fvdl static void
    392   1.6      fvdl bsd_to_linux_msqid_ds(bmp, lmp)
    393   1.6      fvdl 	struct msqid_ds *bmp;
    394   1.6      fvdl 	struct linux_msqid_ds *lmp;
    395   1.6      fvdl {
    396   1.8   mycroft 
    397   1.6      fvdl 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    398   1.6      fvdl 	lmp->l_msg_first = bmp->msg_first;
    399   1.6      fvdl 	lmp->l_msg_last = bmp->msg_last;
    400   1.6      fvdl 	lmp->l_msg_cbytes = bmp->msg_cbytes;
    401   1.6      fvdl 	lmp->l_msg_qnum = bmp->msg_qnum;
    402   1.6      fvdl 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    403   1.6      fvdl 	lmp->l_msg_lspid = bmp->msg_lspid;
    404   1.6      fvdl 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    405   1.6      fvdl 	lmp->l_msg_stime = bmp->msg_stime;
    406   1.6      fvdl 	lmp->l_msg_rtime = bmp->msg_rtime;
    407   1.6      fvdl 	lmp->l_msg_ctime = bmp->msg_ctime;
    408   1.6      fvdl }
    409   1.6      fvdl 
    410  1.10  christos static int
    411   1.1      fvdl linux_msgsnd(p, uap, retval)
    412   1.1      fvdl 	struct proc *p;
    413   1.8   mycroft 	struct linux_sys_ipc_args /* {
    414   1.1      fvdl 		syscallarg(int) what;
    415   1.1      fvdl 		syscallarg(int) a1;
    416   1.1      fvdl 		syscallarg(int) a2;
    417   1.1      fvdl 		syscallarg(int) a3;
    418   1.1      fvdl 		syscallarg(caddr_t) ptr;
    419   1.1      fvdl 	} */ *uap;
    420   1.1      fvdl 	register_t *retval;
    421   1.1      fvdl {
    422   1.8   mycroft 	struct sys_msgsnd_args bma;
    423   1.6      fvdl 
    424   1.6      fvdl 	SCARG(&bma, msqid) = SCARG(uap, a1);
    425   1.6      fvdl 	SCARG(&bma, msgp) = SCARG(uap, ptr);
    426   1.6      fvdl 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    427   1.6      fvdl 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    428   1.6      fvdl 
    429   1.8   mycroft 	return sys_msgsnd(p, &bma, retval);
    430   1.1      fvdl }
    431   1.1      fvdl 
    432  1.10  christos static int
    433   1.1      fvdl linux_msgrcv(p, uap, retval)
    434   1.1      fvdl 	struct proc *p;
    435   1.8   mycroft 	struct linux_sys_ipc_args /* {
    436   1.1      fvdl 		syscallarg(int) what;
    437   1.1      fvdl 		syscallarg(int) a1;
    438   1.1      fvdl 		syscallarg(int) a2;
    439   1.1      fvdl 		syscallarg(int) a3;
    440   1.1      fvdl 		syscallarg(caddr_t) ptr;
    441   1.1      fvdl 	} */ *uap;
    442   1.1      fvdl 	register_t *retval;
    443   1.1      fvdl {
    444   1.8   mycroft 	struct sys_msgrcv_args bma;
    445   1.6      fvdl 	struct linux_msgrcv_msgarg kluge;
    446   1.6      fvdl 	int error;
    447   1.6      fvdl 
    448   1.6      fvdl 	if ((error = copyin(SCARG(uap, ptr), &kluge, sizeof kluge)))
    449   1.6      fvdl 		return error;
    450   1.6      fvdl 
    451   1.6      fvdl 	SCARG(&bma, msqid) = SCARG(uap, a1);
    452   1.6      fvdl 	SCARG(&bma, msgp) = kluge.msg;
    453   1.6      fvdl 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    454   1.6      fvdl 	SCARG(&bma, msgtyp) = kluge.type;
    455   1.6      fvdl 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    456   1.8   mycroft 
    457   1.8   mycroft 	return sys_msgrcv(p, &bma, retval);
    458   1.1      fvdl }
    459   1.1      fvdl 
    460  1.10  christos static int
    461   1.1      fvdl linux_msgget(p, uap, retval)
    462   1.1      fvdl 	struct proc *p;
    463   1.8   mycroft 	struct linux_sys_ipc_args /* {
    464   1.1      fvdl 		syscallarg(int) what;
    465   1.1      fvdl 		syscallarg(int) a1;
    466   1.1      fvdl 		syscallarg(int) a2;
    467   1.1      fvdl 		syscallarg(int) a3;
    468   1.1      fvdl 		syscallarg(caddr_t) ptr;
    469   1.1      fvdl 	} */ *uap;
    470   1.1      fvdl 	register_t *retval;
    471   1.1      fvdl {
    472   1.8   mycroft 	struct sys_msgget_args bma;
    473   1.6      fvdl 
    474   1.6      fvdl 	SCARG(&bma, key) = (key_t)SCARG(uap, a1);
    475   1.6      fvdl 	SCARG(&bma, msgflg) = SCARG(uap, a2);
    476   1.8   mycroft 
    477   1.8   mycroft 	return sys_msgget(p, &bma, retval);
    478   1.1      fvdl }
    479   1.1      fvdl 
    480  1.10  christos static int
    481   1.1      fvdl linux_msgctl(p, uap, retval)
    482   1.1      fvdl 	struct proc *p;
    483   1.8   mycroft 	struct linux_sys_ipc_args /* {
    484   1.1      fvdl 		syscallarg(int) what;
    485   1.1      fvdl 		syscallarg(int) a1;
    486   1.1      fvdl 		syscallarg(int) a2;
    487   1.1      fvdl 		syscallarg(int) a3;
    488   1.1      fvdl 		syscallarg(caddr_t) ptr;
    489   1.1      fvdl 	} */ *uap;
    490   1.1      fvdl 	register_t *retval;
    491   1.1      fvdl {
    492  1.13   mycroft 	caddr_t sg;
    493  1.13   mycroft 	struct sys_msgctl_args nua;
    494  1.13   mycroft 	struct msqid_ds *bmp, bm;
    495   1.6      fvdl 	struct linux_msqid_ds lm;
    496   1.6      fvdl 	int error;
    497   1.6      fvdl 
    498  1.13   mycroft 	SCARG(&nua, msqid) = SCARG(uap, a1);
    499   1.6      fvdl 	switch (SCARG(uap, a2)) {
    500  1.13   mycroft 	case LINUX_IPC_STAT:
    501  1.13   mycroft 		sg = stackgap_init(p->p_emul);
    502  1.13   mycroft 		bmp = stackgap_alloc(&sg, sizeof (struct msqid_ds));
    503  1.13   mycroft 		SCARG(&nua, cmd) = IPC_STAT;
    504  1.13   mycroft 		SCARG(&nua, buf) = bmp;
    505  1.13   mycroft 		if ((error = sys_msgctl(p, &nua, retval)))
    506  1.13   mycroft 			return error;
    507  1.13   mycroft 		if ((error = copyin(bmp, &bm, sizeof bm)))
    508  1.13   mycroft 			return error;
    509  1.13   mycroft 		bsd_to_linux_msqid_ds(&bm, &lm);
    510  1.13   mycroft 		return copyout(&lm, SCARG(uap, ptr), sizeof lm);
    511   1.6      fvdl 	case LINUX_IPC_SET:
    512  1.13   mycroft 		if ((error = copyin(SCARG(uap, ptr), &lm, sizeof lm)))
    513   1.6      fvdl 			return error;
    514   1.6      fvdl 		linux_to_bsd_msqid_ds(&lm, &bm);
    515   1.6      fvdl 		sg = stackgap_init(p->p_emul);
    516  1.13   mycroft 		bmp = stackgap_alloc(&sg, sizeof bm);
    517  1.13   mycroft 		if ((error = copyout(&bm, bmp, sizeof bm)))
    518   1.6      fvdl 			return error;
    519  1.13   mycroft 		SCARG(&nua, cmd) = IPC_SET;
    520  1.13   mycroft 		SCARG(&nua, buf) = bmp;
    521  1.13   mycroft 		return sys_msgctl(p, &nua, retval);
    522  1.13   mycroft 	case LINUX_IPC_RMID:
    523  1.13   mycroft 		SCARG(&nua, cmd) = IPC_RMID;
    524  1.13   mycroft 		SCARG(&nua, buf) = NULL;
    525  1.13   mycroft 		break;
    526  1.13   mycroft 	default:
    527  1.13   mycroft 		return EINVAL;
    528   1.6      fvdl 	}
    529  1.13   mycroft 	return sys_msgctl(p, &nua, retval);
    530   1.1      fvdl }
    531   1.1      fvdl #endif /* SYSVMSG */
    532   1.1      fvdl 
    533   1.1      fvdl #ifdef SYSVSHM
    534   1.1      fvdl /*
    535   1.1      fvdl  * shmat(2). Very straightforward, except that Linux passes a pointer
    536   1.1      fvdl  * in which the return value is to be passed. This is subsequently
    537   1.1      fvdl  * handled by libc, apparently.
    538   1.1      fvdl  */
    539  1.10  christos static int
    540   1.1      fvdl linux_shmat(p, uap, retval)
    541   1.1      fvdl 	struct proc *p;
    542   1.8   mycroft 	struct linux_sys_ipc_args /* {
    543   1.1      fvdl 		syscallarg(int) what;
    544   1.1      fvdl 		syscallarg(int) a1;
    545   1.1      fvdl 		syscallarg(int) a2;
    546   1.1      fvdl 		syscallarg(int) a3;
    547   1.1      fvdl 		syscallarg(caddr_t) ptr;
    548   1.1      fvdl 	} */ *uap;
    549   1.1      fvdl 	register_t *retval;
    550   1.1      fvdl {
    551   1.8   mycroft 	struct sys_shmat_args bsa;
    552   1.1      fvdl 	int error;
    553   1.1      fvdl 
    554   1.1      fvdl 	SCARG(&bsa, shmid) = SCARG(uap, a1);
    555   1.1      fvdl 	SCARG(&bsa, shmaddr) = SCARG(uap, ptr);
    556   1.1      fvdl 	SCARG(&bsa, shmflg) = SCARG(uap, a2);
    557   1.1      fvdl 
    558   1.8   mycroft 	if ((error = sys_shmat(p, &bsa, retval)))
    559   1.1      fvdl 		return error;
    560   1.1      fvdl 
    561   1.1      fvdl 	if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, a3),
    562   1.1      fvdl 	     sizeof retval[0])))
    563   1.1      fvdl 		return error;
    564   1.1      fvdl 
    565   1.1      fvdl 	retval[0] = 0;
    566   1.1      fvdl 	return 0;
    567   1.1      fvdl }
    568   1.1      fvdl 
    569   1.1      fvdl /*
    570   1.1      fvdl  * shmdt(): this could have been mapped directly, if it wasn't for
    571   1.1      fvdl  * the extra indirection by the linux_ipc system call.
    572   1.1      fvdl  */
    573  1.10  christos static int
    574   1.1      fvdl linux_shmdt(p, uap, retval)
    575   1.1      fvdl 	struct proc *p;
    576   1.8   mycroft 	struct linux_sys_ipc_args /* {
    577   1.1      fvdl 		syscallarg(int) what;
    578   1.1      fvdl 		syscallarg(int) a1;
    579   1.1      fvdl 		syscallarg(int) a2;
    580   1.1      fvdl 		syscallarg(int) a3;
    581   1.1      fvdl 		syscallarg(caddr_t) ptr;
    582   1.1      fvdl 	} */ *uap;
    583   1.1      fvdl 	register_t *retval;
    584   1.1      fvdl {
    585   1.8   mycroft 	struct sys_shmdt_args bsa;
    586   1.1      fvdl 
    587   1.1      fvdl 	SCARG(&bsa, shmaddr) = SCARG(uap, ptr);
    588   1.8   mycroft 
    589   1.8   mycroft 	return sys_shmdt(p, &bsa, retval);
    590   1.1      fvdl }
    591   1.1      fvdl 
    592   1.1      fvdl /*
    593   1.1      fvdl  * Same story as shmdt.
    594   1.1      fvdl  */
    595  1.10  christos static int
    596   1.1      fvdl linux_shmget(p, uap, retval)
    597   1.1      fvdl 	struct proc *p;
    598   1.8   mycroft 	struct linux_sys_ipc_args /* {
    599   1.1      fvdl 		syscallarg(int) what;
    600   1.1      fvdl 		syscallarg(int) a1;
    601   1.1      fvdl 		syscallarg(int) a2;
    602   1.1      fvdl 		syscallarg(int) a3;
    603   1.1      fvdl 		syscallarg(caddr_t) ptr;
    604   1.1      fvdl 	} */ *uap;
    605   1.1      fvdl 	register_t *retval;
    606   1.1      fvdl {
    607   1.8   mycroft 	struct sys_shmget_args bsa;
    608   1.1      fvdl 
    609   1.1      fvdl 	SCARG(&bsa, key) = SCARG(uap, a1);
    610   1.1      fvdl 	SCARG(&bsa, size) = SCARG(uap, a2);
    611   1.1      fvdl 	SCARG(&bsa, shmflg) = SCARG(uap, a3);
    612   1.8   mycroft 
    613   1.8   mycroft 	return sys_shmget(p, &bsa, retval);
    614   1.1      fvdl }
    615   1.1      fvdl 
    616   1.1      fvdl /*
    617   1.1      fvdl  * Convert between Linux and NetBSD shmid_ds structures.
    618   1.1      fvdl  * The order of the fields is once again the difference, and
    619   1.1      fvdl  * we also need a place to store the internal data pointer
    620   1.1      fvdl  * in, which is unfortunately stored in this structure.
    621   1.1      fvdl  *
    622   1.1      fvdl  * We abuse a Linux internal field for that.
    623   1.1      fvdl  */
    624   1.1      fvdl static void
    625   1.1      fvdl linux_to_bsd_shmid_ds(lsp, bsp)
    626   1.1      fvdl 	struct linux_shmid_ds *lsp;
    627   1.1      fvdl 	struct shmid_ds *bsp;
    628   1.1      fvdl {
    629   1.8   mycroft 
    630   1.1      fvdl 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    631   1.1      fvdl 	bsp->shm_segsz = lsp->l_shm_segsz;
    632   1.1      fvdl 	bsp->shm_lpid = lsp->l_shm_lpid;
    633   1.1      fvdl 	bsp->shm_cpid = lsp->l_shm_cpid;
    634   1.1      fvdl 	bsp->shm_nattch = lsp->l_shm_nattch;
    635   1.1      fvdl 	bsp->shm_atime = lsp->l_shm_atime;
    636   1.1      fvdl 	bsp->shm_dtime = lsp->l_shm_dtime;
    637   1.1      fvdl 	bsp->shm_ctime = lsp->l_shm_ctime;
    638   1.1      fvdl 	bsp->shm_internal = lsp->l_private2;	/* XXX Oh well. */
    639   1.1      fvdl }
    640   1.1      fvdl 
    641   1.1      fvdl static void
    642   1.1      fvdl bsd_to_linux_shmid_ds(bsp, lsp)
    643   1.1      fvdl 	struct shmid_ds *bsp;
    644   1.1      fvdl 	struct linux_shmid_ds *lsp;
    645   1.1      fvdl {
    646   1.8   mycroft 
    647   1.1      fvdl 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    648   1.1      fvdl 	lsp->l_shm_segsz = bsp->shm_segsz;
    649   1.1      fvdl 	lsp->l_shm_lpid = bsp->shm_lpid;
    650   1.1      fvdl 	lsp->l_shm_cpid = bsp->shm_cpid;
    651   1.1      fvdl 	lsp->l_shm_nattch = bsp->shm_nattch;
    652   1.1      fvdl 	lsp->l_shm_atime = bsp->shm_atime;
    653   1.1      fvdl 	lsp->l_shm_dtime = bsp->shm_dtime;
    654   1.1      fvdl 	lsp->l_shm_ctime = bsp->shm_ctime;
    655   1.1      fvdl 	lsp->l_private2 = bsp->shm_internal;	/* XXX */
    656   1.1      fvdl }
    657   1.1      fvdl 
    658   1.1      fvdl /*
    659   1.1      fvdl  * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
    660   1.1      fvdl  * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
    661   1.1      fvdl  * by NetBSD itself.
    662   1.1      fvdl  *
    663   1.1      fvdl  * The usual structure conversion and massaging is done.
    664   1.1      fvdl  */
    665  1.10  christos static int
    666   1.1      fvdl linux_shmctl(p, uap, retval)
    667   1.1      fvdl 	struct proc *p;
    668   1.8   mycroft 	struct linux_sys_ipc_args /* {
    669   1.1      fvdl 		syscallarg(int) what;
    670   1.1      fvdl 		syscallarg(int) a1;
    671   1.1      fvdl 		syscallarg(int) a2;
    672   1.1      fvdl 		syscallarg(int) a3;
    673   1.1      fvdl 		syscallarg(caddr_t) ptr;
    674   1.1      fvdl 	} */ *uap;
    675   1.1      fvdl 	register_t *retval;
    676   1.1      fvdl {
    677   1.1      fvdl 	caddr_t sg;
    678  1.13   mycroft 	struct sys_shmctl_args nua;
    679   1.1      fvdl 	struct shmid_ds *bsp, bs;
    680  1.13   mycroft 	struct linux_shmid_ds ls;
    681  1.13   mycroft 	int error;
    682   1.1      fvdl 
    683  1.13   mycroft 	SCARG(&nua, shmid) = SCARG(uap, a1);
    684   1.1      fvdl 	switch (SCARG(uap, a2)) {
    685   1.1      fvdl 	case LINUX_IPC_STAT:
    686   1.4  christos 		sg = stackgap_init(p->p_emul);
    687   1.1      fvdl 		bsp = stackgap_alloc(&sg, sizeof (struct shmid_ds));
    688  1.13   mycroft 		SCARG(&nua, cmd) = IPC_STAT;
    689  1.13   mycroft 		SCARG(&nua, buf) = bsp;
    690  1.13   mycroft 		if ((error = sys_shmctl(p, &nua, retval)))
    691   1.1      fvdl 			return error;
    692  1.13   mycroft 		if ((error = copyin(bsp, &bs, sizeof bs)))
    693   1.1      fvdl 			return error;
    694  1.13   mycroft 		bsd_to_linux_shmid_ds(&bs, &ls);
    695  1.13   mycroft 		return copyout(&ls, SCARG(uap, ptr), sizeof ls);
    696   1.1      fvdl 	case LINUX_IPC_SET:
    697  1.13   mycroft 		if ((error = copyin(SCARG(uap, ptr), &ls, sizeof ls)))
    698   1.1      fvdl 			return error;
    699  1.13   mycroft 		linux_to_bsd_shmid_ds(&ls, &bs);
    700   1.4  christos 		sg = stackgap_init(p->p_emul);
    701  1.13   mycroft 		bsp = stackgap_alloc(&sg, sizeof bs);
    702  1.13   mycroft 		if ((error = copyout(&bs, bsp, sizeof bs)))
    703   1.1      fvdl 			return error;
    704  1.13   mycroft 		SCARG(&nua, cmd) = IPC_SET;
    705  1.13   mycroft 		SCARG(&nua, buf) = bsp;
    706  1.13   mycroft 		return sys_shmctl(p, &nua, retval);
    707   1.1      fvdl 	case LINUX_IPC_RMID:
    708  1.13   mycroft 		SCARG(&nua, cmd) = IPC_RMID;
    709  1.13   mycroft 		SCARG(&nua, buf) = NULL;
    710  1.13   mycroft 		break;
    711   1.1      fvdl 	case LINUX_SHM_LOCK:
    712  1.13   mycroft 		SCARG(&nua, cmd) = SHM_LOCK;
    713  1.13   mycroft 		SCARG(&nua, buf) = NULL;
    714  1.13   mycroft 		break;
    715   1.1      fvdl 	case LINUX_SHM_UNLOCK:
    716  1.13   mycroft 		SCARG(&nua, cmd) = SHM_UNLOCK;
    717  1.13   mycroft 		SCARG(&nua, buf) = NULL;
    718  1.13   mycroft 		break;
    719   1.1      fvdl 	case LINUX_IPC_INFO:
    720   1.1      fvdl 	case LINUX_SHM_STAT:
    721   1.1      fvdl 	case LINUX_SHM_INFO:
    722   1.1      fvdl 	default:
    723   1.1      fvdl 		return EINVAL;
    724   1.1      fvdl 	}
    725  1.13   mycroft 	return sys_shmctl(p, &nua, retval);
    726   1.1      fvdl }
    727   1.1      fvdl #endif /* SYSVSHM */
    728