Home | History | Annotate | Line # | Download | only in common
linux_ipccall.c revision 1.7
      1  1.7   thorpej /*	$NetBSD: linux_ipccall.c,v 1.7 1995/09/19 22:37:31 thorpej 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.1      fvdl static int linux_semop __P((struct proc *, struct linux_ipc_args *,
     73  1.1      fvdl 				register_t *));
     74  1.1      fvdl static int linux_semget __P((struct proc *, struct linux_ipc_args *,
     75  1.1      fvdl 				register_t *));
     76  1.1      fvdl static int linux_semctl __P((struct proc *, struct linux_ipc_args *,
     77  1.1      fvdl 				register_t *));
     78  1.1      fvdl #endif
     79  1.1      fvdl 
     80  1.1      fvdl #ifdef SYSVMSG
     81  1.1      fvdl static int linux_msgsnd __P((struct proc *, struct linux_ipc_args *,
     82  1.1      fvdl 				register_t *));
     83  1.1      fvdl static int linux_msgrcv __P((struct proc *, struct linux_ipc_args *,
     84  1.1      fvdl 				register_t *));
     85  1.1      fvdl static int linux_msgop __P((struct proc *, struct linux_ipc_args *,
     86  1.1      fvdl 				register_t *));
     87  1.1      fvdl static int linux_msgctl __P((struct proc *, struct linux_ipc_args *,
     88  1.1      fvdl 				register_t *));
     89  1.1      fvdl #endif
     90  1.1      fvdl 
     91  1.1      fvdl #ifdef SYSVSHM
     92  1.1      fvdl static int linux_shmat __P((struct proc *, struct linux_ipc_args *,
     93  1.1      fvdl 				register_t *));
     94  1.1      fvdl static int linux_shmdt __P((struct proc *, struct linux_ipc_args *,
     95  1.1      fvdl 				register_t *));
     96  1.1      fvdl static int linux_shmget __P((struct proc *, struct linux_ipc_args *,
     97  1.1      fvdl 				register_t *));
     98  1.1      fvdl static int linux_shmctl __P((struct proc *, struct linux_ipc_args *,
     99  1.1      fvdl 				register_t *));
    100  1.1      fvdl #endif
    101  1.1      fvdl 
    102  1.1      fvdl 
    103  1.1      fvdl int
    104  1.7   thorpej linux_ipc(p, v, retval)
    105  1.1      fvdl 	struct proc *p;
    106  1.7   thorpej 	void *v;
    107  1.7   thorpej 	register_t *retval;
    108  1.7   thorpej {
    109  1.1      fvdl 	struct linux_ipc_args /* {
    110  1.1      fvdl 		syscallarg(int) what;
    111  1.1      fvdl 		syscallarg(int) a1;
    112  1.1      fvdl 		syscallarg(int) a2;
    113  1.1      fvdl 		syscallarg(int) a3;
    114  1.1      fvdl 		syscallarg(caddr_t) ptr;
    115  1.7   thorpej 	} */ *uap = v;
    116  1.1      fvdl 	int what, error;
    117  1.1      fvdl 
    118  1.1      fvdl 	switch (SCARG(uap, what)) {
    119  1.1      fvdl #ifdef SYSVSEM
    120  1.6      fvdl 	case LINUX_SYS_semop:
    121  1.6      fvdl 		return linux_semop(p, uap, retval);
    122  1.6      fvdl 	case LINUX_SYS_semget:
    123  1.6      fvdl 		return linux_semget(p, uap, retval);
    124  1.6      fvdl 	case LINUX_SYS_semctl:
    125  1.6      fvdl 		return linux_semctl(p, uap, retval);
    126  1.1      fvdl #endif
    127  1.1      fvdl #ifdef SYSVMSG
    128  1.6      fvdl 	case LINUX_SYS_msgsnd:
    129  1.6      fvdl 		return linux_msgsnd(p, uap, retval);
    130  1.6      fvdl 	case LINUX_SYS_msgrcv:
    131  1.6      fvdl 		return linux_msgrcv(p, uap, retval);
    132  1.6      fvdl 	case LINUX_SYS_msgget:
    133  1.6      fvdl 		return linux_msgget(p, uap, retval);
    134  1.6      fvdl 	case LINUX_SYS_msgctl:
    135  1.6      fvdl 		return linux_msgctl(p, uap, retval);
    136  1.1      fvdl #endif
    137  1.1      fvdl #ifdef SYSVSHM
    138  1.6      fvdl 	case LINUX_SYS_shmat:
    139  1.6      fvdl 		return linux_shmat(p, uap, retval);
    140  1.6      fvdl 	case LINUX_SYS_shmdt:
    141  1.6      fvdl 		return linux_shmdt(p, uap, retval);
    142  1.6      fvdl 	case LINUX_SYS_shmget:
    143  1.6      fvdl 		return linux_shmget(p, uap, retval);
    144  1.6      fvdl 	case LINUX_SYS_shmctl:
    145  1.6      fvdl 		return linux_shmctl(p, uap, retval);
    146  1.1      fvdl #endif
    147  1.6      fvdl 	default:
    148  1.6      fvdl 		return ENOSYS;
    149  1.1      fvdl 	}
    150  1.1      fvdl }
    151  1.1      fvdl 
    152  1.1      fvdl /*
    153  1.1      fvdl  * Convert between Linux and NetBSD ipc_perm structures. Only the
    154  1.1      fvdl  * order of the fields is different.
    155  1.1      fvdl  */
    156  1.1      fvdl static void
    157  1.1      fvdl linux_to_bsd_ipc_perm(lpp, bpp)
    158  1.1      fvdl 	struct linux_ipc_perm *lpp;
    159  1.1      fvdl 	struct ipc_perm *bpp;
    160  1.1      fvdl {
    161  1.1      fvdl 	bpp->key = lpp->l_key;
    162  1.1      fvdl 	bpp->uid = lpp->l_uid;
    163  1.1      fvdl 	bpp->gid = lpp->l_gid;
    164  1.1      fvdl 	bpp->cuid = lpp->l_cuid;
    165  1.1      fvdl 	bpp->cgid = lpp->l_cgid;
    166  1.1      fvdl 	bpp->mode = lpp->l_mode;
    167  1.1      fvdl 	bpp->seq = lpp->l_seq;
    168  1.1      fvdl }
    169  1.1      fvdl 
    170  1.1      fvdl 
    171  1.1      fvdl static void
    172  1.1      fvdl bsd_to_linux_ipc_perm(bpp, lpp)
    173  1.1      fvdl 	struct ipc_perm *bpp;
    174  1.1      fvdl 	struct linux_ipc_perm *lpp;
    175  1.1      fvdl {
    176  1.1      fvdl 	lpp->l_key = bpp->key;
    177  1.1      fvdl 	lpp->l_uid = bpp->uid;
    178  1.1      fvdl 	lpp->l_gid = bpp->gid;
    179  1.1      fvdl 	lpp->l_cuid = bpp->cuid;
    180  1.1      fvdl 	lpp->l_cgid = bpp->cgid;
    181  1.1      fvdl 	lpp->l_mode = bpp->mode;
    182  1.1      fvdl 	lpp->l_seq = bpp->seq;
    183  1.1      fvdl }
    184  1.1      fvdl 
    185  1.1      fvdl #ifdef SYSVSEM
    186  1.1      fvdl /*
    187  1.6      fvdl  * Semaphore operations. Most constants and structures are the same on
    188  1.6      fvdl  * both systems. Only semctl() needs some extra work.
    189  1.1      fvdl  */
    190  1.6      fvdl 
    191  1.6      fvdl /*
    192  1.6      fvdl  * Convert between Linux and NetBSD semid_ds structures.
    193  1.6      fvdl  */
    194  1.6      fvdl static void
    195  1.6      fvdl bsd_to_linux_semid_ds(bs, ls)
    196  1.6      fvdl 	struct semid_ds *bs;
    197  1.6      fvdl 	struct linux_semid_ds *ls;
    198  1.6      fvdl {
    199  1.6      fvdl 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    200  1.6      fvdl 	ls->l_sem_otime = bs->sem_otime;
    201  1.6      fvdl 	ls->l_sem_ctime = bs->sem_ctime;
    202  1.6      fvdl 	ls->l_sem_nsems = bs->sem_nsems;
    203  1.6      fvdl 	ls->l_sem_base  = bs->sem_base;
    204  1.6      fvdl }
    205  1.6      fvdl 
    206  1.6      fvdl static void
    207  1.6      fvdl linux_to_bsd_semid_ds(ls, bs)
    208  1.6      fvdl 	struct linux_semid_ds *ls;
    209  1.6      fvdl 	struct semid_ds *bs;
    210  1.6      fvdl {
    211  1.6      fvdl 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    212  1.6      fvdl 	bs->sem_otime = ls->l_sem_otime;
    213  1.6      fvdl 	bs->sem_ctime = ls->l_sem_ctime;
    214  1.6      fvdl 	bs->sem_nsems = ls->l_sem_nsems;
    215  1.6      fvdl 	bs->sem_base =  ls->l_sem_base;
    216  1.6      fvdl }
    217  1.6      fvdl 
    218  1.1      fvdl int
    219  1.1      fvdl linux_semop(p, uap, retval)
    220  1.1      fvdl 	struct proc *p;
    221  1.1      fvdl 	struct linux_ipc_args /* {
    222  1.1      fvdl 		syscallarg(int) what;
    223  1.1      fvdl 		syscallarg(int) a1;
    224  1.1      fvdl 		syscallarg(int) a2;
    225  1.1      fvdl 		syscallarg(int) a3;
    226  1.1      fvdl 		syscallarg(caddr_t) ptr;
    227  1.1      fvdl 	} */ *uap;
    228  1.1      fvdl 	register_t *retval;
    229  1.1      fvdl {
    230  1.6      fvdl 	struct semop_args bsa;
    231  1.6      fvdl 
    232  1.6      fvdl 	SCARG(&bsa, semid) = SCARG(uap, a1);
    233  1.6      fvdl 	SCARG(&bsa, sops) = (struct sembuf *)SCARG(uap, ptr);
    234  1.6      fvdl 	SCARG(&bsa, nsops) = SCARG(uap, a2);
    235  1.6      fvdl 
    236  1.6      fvdl 	return semop(p, &bsa, retval);
    237  1.1      fvdl }
    238  1.1      fvdl 
    239  1.1      fvdl int
    240  1.1      fvdl linux_semget(p, uap, retval)
    241  1.1      fvdl 	struct proc *p;
    242  1.1      fvdl 	struct linux_ipc_args /* {
    243  1.1      fvdl 		syscallarg(int) what;
    244  1.1      fvdl 		syscallarg(int) a1;
    245  1.1      fvdl 		syscallarg(int) a2;
    246  1.1      fvdl 		syscallarg(int) a3;
    247  1.1      fvdl 		syscallarg(caddr_t) ptr;
    248  1.1      fvdl 	} */ *uap;
    249  1.1      fvdl 	register_t *retval;
    250  1.1      fvdl {
    251  1.6      fvdl 	struct semget_args bsa;
    252  1.6      fvdl 
    253  1.6      fvdl 	SCARG(&bsa, key) = (key_t)SCARG(uap, a1);
    254  1.6      fvdl 	SCARG(&bsa, nsems) = SCARG(uap, a2);
    255  1.6      fvdl 	SCARG(&bsa, semflg) = SCARG(uap, a3);
    256  1.6      fvdl 
    257  1.6      fvdl 	return semget(p, &bsa, retval);
    258  1.1      fvdl }
    259  1.1      fvdl 
    260  1.6      fvdl /*
    261  1.6      fvdl  * Most of this can be handled by directly passing the arguments on,
    262  1.6      fvdl  * buf IPC_* require a lot of copy{in,out} because of the extra indirection
    263  1.6      fvdl  * (we are passed a pointer to a union cointaining a pointer to a semid_ds
    264  1.6      fvdl  * structure.
    265  1.6      fvdl  */
    266  1.1      fvdl int
    267  1.1      fvdl linux_semctl(p, uap, retval)
    268  1.1      fvdl 	struct proc *p;
    269  1.1      fvdl 	struct linux_ipc_args /* {
    270  1.1      fvdl 		syscallarg(int) what;
    271  1.1      fvdl 		syscallarg(int) a1;
    272  1.1      fvdl 		syscallarg(int) a2;
    273  1.1      fvdl 		syscallarg(int) a3;
    274  1.1      fvdl 		syscallarg(caddr_t) ptr;
    275  1.1      fvdl 	} */ *uap;
    276  1.1      fvdl 	register_t *retval;
    277  1.1      fvdl {
    278  1.6      fvdl 	caddr_t sg, unptr, dsp, ldsp;
    279  1.6      fvdl 	int error, cmd;
    280  1.6      fvdl 	struct __semctl_args bsa;
    281  1.6      fvdl 	struct linux_semid_ds lm;
    282  1.6      fvdl 	struct semid_ds bm;
    283  1.6      fvdl 
    284  1.6      fvdl 	SCARG(&bsa, semid) = SCARG(uap, a1);
    285  1.6      fvdl 	SCARG(&bsa, semnum) = SCARG(uap, a2);
    286  1.6      fvdl 	SCARG(&bsa, cmd) = SCARG(uap, a3);
    287  1.6      fvdl 	SCARG(&bsa, arg) = (union semun *)SCARG(uap, ptr);
    288  1.6      fvdl 	switch(SCARG(uap, a3)) {
    289  1.6      fvdl 	case LINUX_GETVAL:
    290  1.6      fvdl 		cmd = GETVAL;
    291  1.6      fvdl 		break;
    292  1.6      fvdl 	case LINUX_GETPID:
    293  1.6      fvdl 		cmd = GETPID;
    294  1.6      fvdl 		break;
    295  1.6      fvdl 	case LINUX_GETNCNT:
    296  1.6      fvdl 		cmd = GETNCNT;
    297  1.6      fvdl 		break;
    298  1.6      fvdl 	case LINUX_GETZCNT:
    299  1.6      fvdl 		cmd = GETZCNT;
    300  1.6      fvdl 		break;
    301  1.6      fvdl 	case LINUX_SETVAL:
    302  1.6      fvdl 		cmd = SETVAL;
    303  1.6      fvdl 		break;
    304  1.6      fvdl 	case LINUX_IPC_RMID:
    305  1.6      fvdl 		cmd = IPC_RMID;
    306  1.6      fvdl 		break;
    307  1.6      fvdl 	case LINUX_IPC_SET:
    308  1.6      fvdl 		if ((error = copyin(SCARG(uap, ptr), &ldsp, sizeof ldsp)))
    309  1.6      fvdl 			return error;
    310  1.6      fvdl 		if ((error = copyin(ldsp, (caddr_t)&lm, sizeof lm)))
    311  1.6      fvdl 			return error;
    312  1.6      fvdl 		linux_to_bsd_semid_ds(&lm, &bm);
    313  1.6      fvdl 		sg = stackgap_init(p->p_emul);
    314  1.6      fvdl 		unptr = stackgap_alloc(&sg, sizeof (union semun));
    315  1.6      fvdl 		dsp = stackgap_alloc(&sg, sizeof (struct semid_ds));
    316  1.6      fvdl 		if ((error = copyout((caddr_t)&bm, dsp, sizeof bm)))
    317  1.6      fvdl 			return error;
    318  1.6      fvdl 		if ((error = copyout((caddr_t)&dsp, unptr, sizeof dsp)))
    319  1.6      fvdl 			return error;
    320  1.6      fvdl 		SCARG(&bsa, arg) = (union semun *)unptr;
    321  1.6      fvdl 		return __semctl(p, &bsa, retval);
    322  1.6      fvdl 	case LINUX_IPC_STAT:
    323  1.6      fvdl 		sg = stackgap_init(p->p_emul);
    324  1.6      fvdl 		unptr = stackgap_alloc(&sg, sizeof (union semun *));
    325  1.6      fvdl 		dsp = stackgap_alloc(&sg, sizeof (struct semid_ds));
    326  1.6      fvdl 		if ((error = copyout((caddr_t)&dsp, unptr, sizeof dsp)))
    327  1.6      fvdl 			return error;
    328  1.6      fvdl 		SCARG(&bsa, arg) = (union semun *)unptr;
    329  1.6      fvdl 		if ((error = __semctl(p, &bsa, retval)))
    330  1.6      fvdl 			return error;
    331  1.6      fvdl 		if ((error = copyin(dsp, (caddr_t)&bm, sizeof bm)))
    332  1.6      fvdl 			return error;
    333  1.6      fvdl 		bsd_to_linux_semid_ds(&bm, &lm);
    334  1.6      fvdl 		if ((error = copyin(SCARG(uap, ptr), &ldsp, sizeof ldsp)))
    335  1.6      fvdl 			return error;
    336  1.6      fvdl 		return copyout((caddr_t)&lm, ldsp, sizeof lm);
    337  1.6      fvdl 	default:
    338  1.6      fvdl 		return EINVAL;
    339  1.6      fvdl 	}
    340  1.6      fvdl 	SCARG(&bsa, cmd) = cmd;
    341  1.6      fvdl 	return __semctl(p, &bsa, retval);
    342  1.1      fvdl }
    343  1.1      fvdl #endif /* SYSVSEM */
    344  1.1      fvdl 
    345  1.1      fvdl #ifdef SYSVMSG
    346  1.6      fvdl 
    347  1.6      fvdl static void
    348  1.6      fvdl linux_to_bsd_msqid_ds(lmp, bmp)
    349  1.6      fvdl 	struct linux_msqid_ds *lmp;
    350  1.6      fvdl 	struct msqid_ds *bmp;
    351  1.6      fvdl {
    352  1.6      fvdl 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    353  1.6      fvdl 	bmp->msg_first = lmp->l_msg_first;
    354  1.6      fvdl 	bmp->msg_last = lmp->l_msg_last;
    355  1.6      fvdl 	bmp->msg_cbytes = lmp->l_msg_cbytes;
    356  1.6      fvdl 	bmp->msg_qnum = lmp->l_msg_qnum;
    357  1.6      fvdl 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    358  1.6      fvdl 	bmp->msg_lspid = lmp->l_msg_lspid;
    359  1.6      fvdl 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    360  1.6      fvdl 	bmp->msg_stime = lmp->l_msg_stime;
    361  1.6      fvdl 	bmp->msg_rtime = lmp->l_msg_rtime;
    362  1.6      fvdl 	bmp->msg_ctime = lmp->l_msg_ctime;
    363  1.6      fvdl }
    364  1.6      fvdl 
    365  1.6      fvdl static void
    366  1.6      fvdl bsd_to_linux_msqid_ds(bmp, lmp)
    367  1.6      fvdl 	struct msqid_ds *bmp;
    368  1.6      fvdl 	struct linux_msqid_ds *lmp;
    369  1.6      fvdl {
    370  1.6      fvdl 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    371  1.6      fvdl 	lmp->l_msg_first = bmp->msg_first;
    372  1.6      fvdl 	lmp->l_msg_last = bmp->msg_last;
    373  1.6      fvdl 	lmp->l_msg_cbytes = bmp->msg_cbytes;
    374  1.6      fvdl 	lmp->l_msg_qnum = bmp->msg_qnum;
    375  1.6      fvdl 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    376  1.6      fvdl 	lmp->l_msg_lspid = bmp->msg_lspid;
    377  1.6      fvdl 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    378  1.6      fvdl 	lmp->l_msg_stime = bmp->msg_stime;
    379  1.6      fvdl 	lmp->l_msg_rtime = bmp->msg_rtime;
    380  1.6      fvdl 	lmp->l_msg_ctime = bmp->msg_ctime;
    381  1.6      fvdl }
    382  1.6      fvdl 
    383  1.1      fvdl int
    384  1.1      fvdl linux_msgsnd(p, uap, retval)
    385  1.1      fvdl 	struct proc *p;
    386  1.1      fvdl 	struct linux_ipc_args /* {
    387  1.1      fvdl 		syscallarg(int) what;
    388  1.1      fvdl 		syscallarg(int) a1;
    389  1.1      fvdl 		syscallarg(int) a2;
    390  1.1      fvdl 		syscallarg(int) a3;
    391  1.1      fvdl 		syscallarg(caddr_t) ptr;
    392  1.1      fvdl 	} */ *uap;
    393  1.1      fvdl 	register_t *retval;
    394  1.1      fvdl {
    395  1.6      fvdl 	struct msgsnd_args bma;
    396  1.6      fvdl 
    397  1.6      fvdl 	SCARG(&bma, msqid) = SCARG(uap, a1);
    398  1.6      fvdl 	SCARG(&bma, msgp) = SCARG(uap, ptr);
    399  1.6      fvdl 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    400  1.6      fvdl 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    401  1.6      fvdl 
    402  1.6      fvdl 	return msgsnd(p, &bma, retval);
    403  1.1      fvdl }
    404  1.1      fvdl 
    405  1.1      fvdl int
    406  1.1      fvdl linux_msgrcv(p, uap, retval)
    407  1.1      fvdl 	struct proc *p;
    408  1.1      fvdl 	struct linux_ipc_args /* {
    409  1.1      fvdl 		syscallarg(int) what;
    410  1.1      fvdl 		syscallarg(int) a1;
    411  1.1      fvdl 		syscallarg(int) a2;
    412  1.1      fvdl 		syscallarg(int) a3;
    413  1.1      fvdl 		syscallarg(caddr_t) ptr;
    414  1.1      fvdl 	} */ *uap;
    415  1.1      fvdl 	register_t *retval;
    416  1.1      fvdl {
    417  1.6      fvdl 	struct msgrcv_args bma;
    418  1.6      fvdl 	struct linux_msgrcv_msgarg kluge;
    419  1.6      fvdl 	int error;
    420  1.6      fvdl 
    421  1.6      fvdl 	if ((error = copyin(SCARG(uap, ptr), &kluge, sizeof kluge)))
    422  1.6      fvdl 		return error;
    423  1.6      fvdl 
    424  1.6      fvdl 	SCARG(&bma, msqid) = SCARG(uap, a1);
    425  1.6      fvdl 	SCARG(&bma, msgp) = kluge.msg;
    426  1.6      fvdl 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    427  1.6      fvdl 	SCARG(&bma, msgtyp) = kluge.type;
    428  1.6      fvdl 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    429  1.6      fvdl 	return msgrcv(p, &bma, retval);
    430  1.1      fvdl }
    431  1.1      fvdl 
    432  1.1      fvdl int
    433  1.1      fvdl linux_msgget(p, uap, retval)
    434  1.1      fvdl 	struct proc *p;
    435  1.1      fvdl 	struct linux_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.6      fvdl 	struct msgget_args bma;
    445  1.6      fvdl 
    446  1.6      fvdl 	SCARG(&bma, key) = (key_t)SCARG(uap, a1);
    447  1.6      fvdl 	SCARG(&bma, msgflg) = SCARG(uap, a2);
    448  1.6      fvdl 	return msgget(p, &bma, retval);
    449  1.1      fvdl }
    450  1.1      fvdl 
    451  1.1      fvdl int
    452  1.1      fvdl linux_msgctl(p, uap, retval)
    453  1.1      fvdl 	struct proc *p;
    454  1.1      fvdl 	struct linux_ipc_args /* {
    455  1.1      fvdl 		syscallarg(int) what;
    456  1.1      fvdl 		syscallarg(int) a1;
    457  1.1      fvdl 		syscallarg(int) a2;
    458  1.1      fvdl 		syscallarg(int) a3;
    459  1.1      fvdl 		syscallarg(caddr_t) ptr;
    460  1.1      fvdl 	} */ *uap;
    461  1.1      fvdl 	register_t *retval;
    462  1.1      fvdl {
    463  1.6      fvdl 	struct msgctl_args bma;
    464  1.6      fvdl 	caddr_t umsgptr, sg;
    465  1.6      fvdl 	struct linux_msqid_ds lm;
    466  1.6      fvdl 	struct msqid_ds bm;
    467  1.6      fvdl 	int error;
    468  1.6      fvdl 
    469  1.6      fvdl 	SCARG(&bma, msqid) = SCARG(uap, a1);
    470  1.6      fvdl 	SCARG(&bma, cmd) = SCARG(uap, a2);
    471  1.6      fvdl 	switch (SCARG(uap, a2)) {
    472  1.6      fvdl 	case LINUX_IPC_RMID:
    473  1.6      fvdl 		return msgctl(p, &bma, retval);
    474  1.6      fvdl 	case LINUX_IPC_SET:
    475  1.6      fvdl 		if ((error = copyin(SCARG(uap, ptr), (caddr_t)&lm, sizeof lm)))
    476  1.6      fvdl 			return error;
    477  1.6      fvdl 		linux_to_bsd_msqid_ds(&lm, &bm);
    478  1.6      fvdl 		sg = stackgap_init(p->p_emul);
    479  1.6      fvdl 		umsgptr = stackgap_alloc(&sg, sizeof bm);
    480  1.6      fvdl 		if ((error = copyout((caddr_t)&bm, umsgptr, sizeof bm)))
    481  1.6      fvdl 			return error;
    482  1.6      fvdl 		SCARG(&bma, buf) = (struct msqid_ds *)umsgptr;
    483  1.6      fvdl 		return msgctl(p, &bma, retval);
    484  1.6      fvdl 	case LINUX_IPC_STAT:
    485  1.6      fvdl 		sg = stackgap_init(p->p_emul);
    486  1.6      fvdl 		umsgptr = stackgap_alloc(&sg, sizeof (struct msqid_ds));
    487  1.6      fvdl 		SCARG(&bma, buf) = (struct msqid_ds *)umsgptr;
    488  1.6      fvdl 		if ((error = msgctl(p, &bma, retval)))
    489  1.6      fvdl 			return error;
    490  1.6      fvdl 		if ((error = copyin(umsgptr, (caddr_t)&bm, sizeof bm)))
    491  1.6      fvdl 			return error;
    492  1.6      fvdl 		bsd_to_linux_msqid_ds(&bm, &lm);
    493  1.6      fvdl 		return copyout((caddr_t)&lm, SCARG(uap, ptr), sizeof lm);
    494  1.6      fvdl 	}
    495  1.6      fvdl 	return EINVAL;
    496  1.1      fvdl }
    497  1.1      fvdl #endif /* SYSVMSG */
    498  1.1      fvdl 
    499  1.1      fvdl #ifdef SYSVSHM
    500  1.1      fvdl /*
    501  1.1      fvdl  * shmat(2). Very straightforward, except that Linux passes a pointer
    502  1.1      fvdl  * in which the return value is to be passed. This is subsequently
    503  1.1      fvdl  * handled by libc, apparently.
    504  1.1      fvdl  */
    505  1.1      fvdl int
    506  1.1      fvdl linux_shmat(p, uap, retval)
    507  1.1      fvdl 	struct proc *p;
    508  1.1      fvdl 	struct linux_ipc_args /* {
    509  1.1      fvdl 		syscallarg(int) what;
    510  1.1      fvdl 		syscallarg(int) a1;
    511  1.1      fvdl 		syscallarg(int) a2;
    512  1.1      fvdl 		syscallarg(int) a3;
    513  1.1      fvdl 		syscallarg(caddr_t) ptr;
    514  1.1      fvdl 	} */ *uap;
    515  1.1      fvdl 	register_t *retval;
    516  1.1      fvdl {
    517  1.1      fvdl 	struct shmat_args bsa;
    518  1.1      fvdl 	int error;
    519  1.1      fvdl 
    520  1.1      fvdl 	SCARG(&bsa, shmid) = SCARG(uap, a1);
    521  1.1      fvdl 	SCARG(&bsa, shmaddr) = SCARG(uap, ptr);
    522  1.1      fvdl 	SCARG(&bsa, shmflg) = SCARG(uap, a2);
    523  1.1      fvdl 
    524  1.1      fvdl 	if ((error = shmat(p, &bsa, retval)))
    525  1.1      fvdl 		return error;
    526  1.1      fvdl 
    527  1.1      fvdl 	if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, a3),
    528  1.1      fvdl 	     sizeof retval[0])))
    529  1.1      fvdl 		return error;
    530  1.1      fvdl 
    531  1.1      fvdl 	retval[0] = 0;
    532  1.1      fvdl 
    533  1.1      fvdl 	return 0;
    534  1.1      fvdl }
    535  1.1      fvdl 
    536  1.1      fvdl /*
    537  1.1      fvdl  * shmdt(): this could have been mapped directly, if it wasn't for
    538  1.1      fvdl  * the extra indirection by the linux_ipc system call.
    539  1.1      fvdl  */
    540  1.1      fvdl int
    541  1.1      fvdl linux_shmdt(p, uap, retval)
    542  1.1      fvdl 	struct proc *p;
    543  1.1      fvdl 	struct linux_ipc_args /* {
    544  1.1      fvdl 		syscallarg(int) what;
    545  1.1      fvdl 		syscallarg(int) a1;
    546  1.1      fvdl 		syscallarg(int) a2;
    547  1.1      fvdl 		syscallarg(int) a3;
    548  1.1      fvdl 		syscallarg(caddr_t) ptr;
    549  1.1      fvdl 	} */ *uap;
    550  1.1      fvdl 	register_t *retval;
    551  1.1      fvdl {
    552  1.1      fvdl 	struct shmdt_args bsa;
    553  1.1      fvdl 
    554  1.1      fvdl 	SCARG(&bsa, shmaddr) = SCARG(uap, ptr);
    555  1.1      fvdl 	return shmdt(p, &bsa, retval);
    556  1.1      fvdl }
    557  1.1      fvdl 
    558  1.1      fvdl /*
    559  1.1      fvdl  * Same story as shmdt.
    560  1.1      fvdl  */
    561  1.1      fvdl int
    562  1.1      fvdl linux_shmget(p, uap, retval)
    563  1.1      fvdl 	struct proc *p;
    564  1.1      fvdl 	struct linux_ipc_args /* {
    565  1.1      fvdl 		syscallarg(int) what;
    566  1.1      fvdl 		syscallarg(int) a1;
    567  1.1      fvdl 		syscallarg(int) a2;
    568  1.1      fvdl 		syscallarg(int) a3;
    569  1.1      fvdl 		syscallarg(caddr_t) ptr;
    570  1.1      fvdl 	} */ *uap;
    571  1.1      fvdl 	register_t *retval;
    572  1.1      fvdl {
    573  1.1      fvdl 	struct shmget_args bsa;
    574  1.1      fvdl 
    575  1.1      fvdl 	SCARG(&bsa, key) = SCARG(uap, a1);
    576  1.1      fvdl 	SCARG(&bsa, size) = SCARG(uap, a2);
    577  1.1      fvdl 	SCARG(&bsa, shmflg) = SCARG(uap, a3);
    578  1.1      fvdl 	return shmget(p, &bsa, retval);
    579  1.1      fvdl }
    580  1.1      fvdl 
    581  1.1      fvdl /*
    582  1.1      fvdl  * Convert between Linux and NetBSD shmid_ds structures.
    583  1.1      fvdl  * The order of the fields is once again the difference, and
    584  1.1      fvdl  * we also need a place to store the internal data pointer
    585  1.1      fvdl  * in, which is unfortunately stored in this structure.
    586  1.1      fvdl  *
    587  1.1      fvdl  * We abuse a Linux internal field for that.
    588  1.1      fvdl  */
    589  1.1      fvdl static void
    590  1.1      fvdl linux_to_bsd_shmid_ds(lsp, bsp)
    591  1.1      fvdl 	struct linux_shmid_ds *lsp;
    592  1.1      fvdl 	struct shmid_ds *bsp;
    593  1.1      fvdl {
    594  1.1      fvdl 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    595  1.1      fvdl 	bsp->shm_segsz = lsp->l_shm_segsz;
    596  1.1      fvdl 	bsp->shm_lpid = lsp->l_shm_lpid;
    597  1.1      fvdl 	bsp->shm_cpid = lsp->l_shm_cpid;
    598  1.1      fvdl 	bsp->shm_nattch = lsp->l_shm_nattch;
    599  1.1      fvdl 	bsp->shm_atime = lsp->l_shm_atime;
    600  1.1      fvdl 	bsp->shm_dtime = lsp->l_shm_dtime;
    601  1.1      fvdl 	bsp->shm_ctime = lsp->l_shm_ctime;
    602  1.1      fvdl 	bsp->shm_internal = lsp->l_private2;	/* XXX Oh well. */
    603  1.1      fvdl }
    604  1.1      fvdl 
    605  1.1      fvdl static void
    606  1.1      fvdl bsd_to_linux_shmid_ds(bsp, lsp)
    607  1.1      fvdl 	struct shmid_ds *bsp;
    608  1.1      fvdl 	struct linux_shmid_ds *lsp;
    609  1.1      fvdl {
    610  1.1      fvdl 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    611  1.1      fvdl 	lsp->l_shm_segsz = bsp->shm_segsz;
    612  1.1      fvdl 	lsp->l_shm_lpid = bsp->shm_lpid;
    613  1.1      fvdl 	lsp->l_shm_cpid = bsp->shm_cpid;
    614  1.1      fvdl 	lsp->l_shm_nattch = bsp->shm_nattch;
    615  1.1      fvdl 	lsp->l_shm_atime = bsp->shm_atime;
    616  1.1      fvdl 	lsp->l_shm_dtime = bsp->shm_dtime;
    617  1.1      fvdl 	lsp->l_shm_ctime = bsp->shm_ctime;
    618  1.1      fvdl 	lsp->l_private2 = bsp->shm_internal;	/* XXX */
    619  1.1      fvdl }
    620  1.1      fvdl 
    621  1.1      fvdl /*
    622  1.1      fvdl  * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
    623  1.1      fvdl  * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
    624  1.1      fvdl  * by NetBSD itself.
    625  1.1      fvdl  *
    626  1.1      fvdl  * The usual structure conversion and massaging is done.
    627  1.1      fvdl  */
    628  1.1      fvdl int
    629  1.1      fvdl linux_shmctl(p, uap, retval)
    630  1.1      fvdl 	struct proc *p;
    631  1.1      fvdl 	struct linux_ipc_args /* {
    632  1.1      fvdl 		syscallarg(int) what;
    633  1.1      fvdl 		syscallarg(int) a1;
    634  1.1      fvdl 		syscallarg(int) a2;
    635  1.1      fvdl 		syscallarg(int) a3;
    636  1.1      fvdl 		syscallarg(caddr_t) ptr;
    637  1.1      fvdl 	} */ *uap;
    638  1.1      fvdl 	register_t *retval;
    639  1.1      fvdl {
    640  1.1      fvdl 	int error;
    641  1.1      fvdl 	caddr_t sg;
    642  1.1      fvdl 	struct shmctl_args bsa;
    643  1.1      fvdl 	struct shmid_ds *bsp, bs;
    644  1.1      fvdl 	struct linux_shmid_ds lseg;
    645  1.1      fvdl 
    646  1.1      fvdl 	switch (SCARG(uap, a2)) {
    647  1.1      fvdl 	case LINUX_IPC_STAT:
    648  1.4  christos 		sg = stackgap_init(p->p_emul);
    649  1.1      fvdl 		bsp = stackgap_alloc(&sg, sizeof (struct shmid_ds));
    650  1.1      fvdl 		SCARG(&bsa, shmid) = SCARG(uap, a1);
    651  1.1      fvdl 		SCARG(&bsa, cmd) = IPC_STAT;
    652  1.1      fvdl 		SCARG(&bsa, buf) = bsp;
    653  1.1      fvdl 		if ((error = shmctl(p, &bsa, retval)))
    654  1.1      fvdl 			return error;
    655  1.1      fvdl 		if ((error = copyin((caddr_t) &bs, (caddr_t) bsp, sizeof bs)))
    656  1.1      fvdl 			return error;
    657  1.1      fvdl 		bsd_to_linux_shmid_ds(&bs, &lseg);
    658  1.1      fvdl 		return copyout((caddr_t) &lseg, SCARG(uap, ptr), sizeof lseg);
    659  1.1      fvdl 	case LINUX_IPC_SET:
    660  1.1      fvdl 		if ((error = copyin(SCARG(uap, ptr), (caddr_t) &lseg,
    661  1.1      fvdl 		     sizeof lseg)))
    662  1.1      fvdl 			return error;
    663  1.1      fvdl 		linux_to_bsd_shmid_ds(&lseg, &bs);
    664  1.4  christos 		sg = stackgap_init(p->p_emul);
    665  1.1      fvdl 		bsp = stackgap_alloc(&sg, sizeof (struct shmid_ds));
    666  1.1      fvdl 		if ((error = copyout((caddr_t) &bs, (caddr_t) bsp, sizeof bs)))
    667  1.1      fvdl 			return error;
    668  1.1      fvdl 		SCARG(&bsa, shmid) = SCARG(uap, a1);
    669  1.1      fvdl 		SCARG(&bsa, cmd) = IPC_SET;
    670  1.1      fvdl 		SCARG(&bsa, buf) = bsp;
    671  1.1      fvdl 		return shmctl(p, &bsa, retval);
    672  1.1      fvdl 	case LINUX_IPC_RMID:
    673  1.1      fvdl 	case LINUX_SHM_LOCK:
    674  1.1      fvdl 	case LINUX_SHM_UNLOCK:
    675  1.1      fvdl 		SCARG(&bsa, shmid) = SCARG(uap, a1);
    676  1.1      fvdl 		switch (SCARG(uap, a2)) {
    677  1.1      fvdl 		case LINUX_IPC_RMID:
    678  1.1      fvdl 			SCARG(&bsa, cmd) = IPC_RMID;
    679  1.1      fvdl 			break;
    680  1.1      fvdl 		case LINUX_SHM_LOCK:
    681  1.1      fvdl 			SCARG(&bsa, cmd) = SHM_LOCK;
    682  1.1      fvdl 			break;
    683  1.1      fvdl 		case LINUX_SHM_UNLOCK:
    684  1.1      fvdl 			SCARG(&bsa, cmd) = SHM_UNLOCK;
    685  1.1      fvdl 			break;
    686  1.1      fvdl 		}
    687  1.2      fvdl 		if ((error = copyin(SCARG(uap, ptr), (caddr_t) &lseg,
    688  1.2      fvdl 		     sizeof lseg)))
    689  1.2      fvdl 			return error;
    690  1.2      fvdl 		linux_to_bsd_shmid_ds(&lseg, &bs);
    691  1.4  christos 		sg = stackgap_init(p->p_emul);
    692  1.2      fvdl 		bsp = stackgap_alloc(&sg, sizeof (struct shmid_ds));
    693  1.2      fvdl 		if ((error = copyout((caddr_t) &bs, (caddr_t) bsp, sizeof bs)))
    694  1.2      fvdl 			return error;
    695  1.2      fvdl 		SCARG(&bsa, buf) = bsp;
    696  1.1      fvdl 		return shmctl(p, &bsa, retval);
    697  1.1      fvdl 	case LINUX_IPC_INFO:
    698  1.1      fvdl 	case LINUX_SHM_STAT:
    699  1.1      fvdl 	case LINUX_SHM_INFO:
    700  1.1      fvdl 	default:
    701  1.1      fvdl 		return EINVAL;
    702  1.1      fvdl 	}
    703  1.1      fvdl }
    704  1.1      fvdl #endif /* SYSVSHM */
    705