Home | History | Annotate | Line # | Download | only in common
linux_ipccall.c revision 1.17
      1 /*	$NetBSD: linux_ipccall.c,v 1.17 1998/11/22 15:00:47 drochner Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "opt_sysv.h"
     40 
     41 #include <sys/types.h>
     42 #include <sys/param.h>
     43 #include <sys/shm.h>
     44 #include <sys/sem.h>
     45 #include <sys/msg.h>
     46 #include <sys/proc.h>
     47 #include <sys/systm.h>
     48 
     49 /* real syscalls */
     50 #include <sys/mount.h>
     51 #include <sys/syscallargs.h>
     52 
     53 
     54 /* sys_ipc + args prototype */
     55 #include <compat/linux/common/linux_types.h>
     56 #include <compat/linux/common/linux_signal.h>
     57 
     58 #include <compat/linux/linux_syscallargs.h>
     59 #include <compat/linux/linux_syscall.h>
     60 
     61 /* general ipc defines */
     62 #include <compat/linux/common/linux_ipc.h>
     63 
     64 /* prototypes for real/normal linux-emul syscalls */
     65 #include <compat/linux/common/linux_msg.h>
     66 #include <compat/linux/common/linux_shm.h>
     67 #include <compat/linux/common/linux_sem.h>
     68 
     69 /* prototypes for sys_ipc stuff */
     70 #include <compat/linux/common/linux_ipccall.h>
     71 
     72 
     73 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     74 /* Not used on: alpha */
     75 
     76 /*
     77  * Stuff to deal with the SysV ipc/shm/semaphore interface in Linux.
     78  * The main difference is, that Linux handles it all via one
     79  * system call, which has the usual maximum amount of 5 arguments.
     80  * This results in a kludge for calls that take 6 of them.
     81  *
     82  * The SYSV??? options have to be enabled to get the appropriate
     83  * functions to work.
     84  */
     85 
     86 int
     87 linux_sys_ipc(p, v, retval)
     88 	struct proc *p;
     89 	void *v;
     90 	register_t *retval;
     91 {
     92 	struct linux_sys_ipc_args /* {
     93 		syscallarg(int) what;
     94 		syscallarg(int) a1;
     95 		syscallarg(int) a2;
     96 		syscallarg(int) a3;
     97 		syscallarg(caddr_t) ptr;
     98 	} */ *uap = v;
     99 
    100 	switch (SCARG(uap, what)) {
    101 #ifdef SYSVSEM
    102 	case LINUX_SYS_semop:
    103 		return linux_semop(p, uap, retval);
    104 	case LINUX_SYS_semget:
    105 		return linux_semget(p, uap, retval);
    106 	case LINUX_SYS_semctl: {
    107 		struct linux_sys_semctl_args bsa;
    108 		union linux_semun arg;
    109 		int error;
    110 
    111 		SCARG(&bsa, semid) = SCARG(uap, a1);
    112 		SCARG(&bsa, semnum) = SCARG(uap, a2);
    113 		SCARG(&bsa, cmd) = SCARG(uap, a3);
    114 		/* Convert from (union linux_semun *) to (union linux_semun) */
    115 		if ((error = copyin(SCARG(uap, ptr), &arg, sizeof arg)))
    116 			return error;
    117 		SCARG(&bsa, arg) = arg;
    118 
    119 		return linux_sys_semctl(p, &bsa, retval);
    120 	    }
    121 #endif
    122 #ifdef SYSVMSG
    123 	case LINUX_SYS_msgsnd:
    124 		return linux_msgsnd(p, uap, retval);
    125 	case LINUX_SYS_msgrcv:
    126 		return linux_msgrcv(p, uap, retval);
    127 	case LINUX_SYS_msgget:
    128 		return linux_msgget(p, uap, retval);
    129 	case LINUX_SYS_msgctl: {
    130 		struct linux_sys_msgctl_args bsa;
    131 
    132 		SCARG(&bsa, msqid) = SCARG(uap, a1);
    133 		SCARG(&bsa, cmd) = SCARG(uap, a2);
    134 		SCARG(&bsa, buf) = (struct linux_msqid_ds *)SCARG(uap, ptr);
    135 
    136 		return linux_sys_msgctl(p, &bsa, retval);
    137 	    }
    138 #endif
    139 #ifdef SYSVSHM
    140 	case LINUX_SYS_shmat: {
    141 		struct linux_sys_shmat_args bsa;
    142 
    143 		SCARG(&bsa, shmid) = SCARG(uap, a1);
    144 		SCARG(&bsa, shmaddr) = (void *)SCARG(uap, ptr);
    145 		SCARG(&bsa, shmflg) = SCARG(uap, a2);
    146 		/* XXX passing pointer inside int here */
    147 		SCARG(&bsa, raddr) = (u_long *)SCARG(uap, a3);
    148 
    149 		return linux_sys_shmat(p, &bsa, retval);
    150 	    }
    151 	case LINUX_SYS_shmdt:
    152 		return linux_shmdt(p, uap, retval);
    153 	case LINUX_SYS_shmget:
    154 		return linux_shmget(p, uap, retval);
    155 	case LINUX_SYS_shmctl: {
    156 		struct linux_sys_shmctl_args bsa;
    157 
    158 		SCARG(&bsa, shmid) = SCARG(uap, a1);
    159 		SCARG(&bsa, cmd) = SCARG(uap, a2);
    160 		SCARG(&bsa, buf) = (struct linux_shmid_ds *)SCARG(uap, ptr);
    161 
    162 		return linux_sys_shmctl(p, &bsa, retval);
    163 	    }
    164 #endif
    165 	default:
    166 		return ENOSYS;
    167 	}
    168 }
    169 
    170 #ifdef SYSVSEM
    171 inline int
    172 linux_semop(p, uap, retval)
    173 	struct proc *p;
    174 	struct linux_sys_ipc_args /* {
    175 		syscallarg(int) what;
    176 		syscallarg(int) a1;
    177 		syscallarg(int) a2;
    178 		syscallarg(int) a3;
    179 		syscallarg(caddr_t) ptr;
    180 	} */ *uap;
    181 	register_t *retval;
    182 {
    183 	struct sys_semop_args bsa;
    184 
    185 	SCARG(&bsa, semid) = SCARG(uap, a1);
    186 	SCARG(&bsa, sops) = (struct sembuf *)SCARG(uap, ptr);
    187 	SCARG(&bsa, nsops) = SCARG(uap, a2);
    188 
    189 	return sys_semop(p, &bsa, retval);
    190 }
    191 
    192 inline int
    193 linux_semget(p, uap, retval)
    194 	struct proc *p;
    195 	struct linux_sys_ipc_args /* {
    196 		syscallarg(int) what;
    197 		syscallarg(int) a1;
    198 		syscallarg(int) a2;
    199 		syscallarg(int) a3;
    200 		syscallarg(caddr_t) ptr;
    201 	} */ *uap;
    202 	register_t *retval;
    203 {
    204 	struct sys_semget_args bsa;
    205 
    206 	SCARG(&bsa, key) = (key_t)SCARG(uap, a1);
    207 	SCARG(&bsa, nsems) = SCARG(uap, a2);
    208 	SCARG(&bsa, semflg) = SCARG(uap, a3);
    209 
    210 	return sys_semget(p, &bsa, retval);
    211 }
    212 
    213 #endif /* SYSVSEM */
    214 
    215 #ifdef SYSVMSG
    216 
    217 inline int
    218 linux_msgsnd(p, uap, retval)
    219 	struct proc *p;
    220 	struct linux_sys_ipc_args /* {
    221 		syscallarg(int) what;
    222 		syscallarg(int) a1;
    223 		syscallarg(int) a2;
    224 		syscallarg(int) a3;
    225 		syscallarg(caddr_t) ptr;
    226 	} */ *uap;
    227 	register_t *retval;
    228 {
    229 	struct sys_msgsnd_args bma;
    230 
    231 	SCARG(&bma, msqid) = SCARG(uap, a1);
    232 	SCARG(&bma, msgp) = SCARG(uap, ptr);
    233 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    234 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    235 
    236 	return sys_msgsnd(p, &bma, retval);
    237 }
    238 
    239 inline int
    240 linux_msgrcv(p, uap, retval)
    241 	struct proc *p;
    242 	struct linux_sys_ipc_args /* {
    243 		syscallarg(int) what;
    244 		syscallarg(int) a1;
    245 		syscallarg(int) a2;
    246 		syscallarg(int) a3;
    247 		syscallarg(caddr_t) ptr;
    248 	} */ *uap;
    249 	register_t *retval;
    250 {
    251 	struct sys_msgrcv_args bma;
    252 	struct linux_msgrcv_msgarg kluge;
    253 	int error;
    254 
    255 	if ((error = copyin(SCARG(uap, ptr), &kluge, sizeof kluge)))
    256 		return error;
    257 
    258 	SCARG(&bma, msqid) = SCARG(uap, a1);
    259 	SCARG(&bma, msgp) = kluge.msg;
    260 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    261 	SCARG(&bma, msgtyp) = kluge.type;
    262 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    263 
    264 	return sys_msgrcv(p, &bma, retval);
    265 }
    266 
    267 inline int
    268 linux_msgget(p, uap, retval)
    269 	struct proc *p;
    270 	struct linux_sys_ipc_args /* {
    271 		syscallarg(int) what;
    272 		syscallarg(int) a1;
    273 		syscallarg(int) a2;
    274 		syscallarg(int) a3;
    275 		syscallarg(caddr_t) ptr;
    276 	} */ *uap;
    277 	register_t *retval;
    278 {
    279 	struct sys_msgget_args bma;
    280 
    281 	SCARG(&bma, key) = (key_t)SCARG(uap, a1);
    282 	SCARG(&bma, msgflg) = SCARG(uap, a2);
    283 
    284 	return sys_msgget(p, &bma, retval);
    285 }
    286 
    287 #endif /* SYSVMSG */
    288 
    289 #ifdef SYSVSHM
    290 /*
    291  * shmdt(): this could have been mapped directly, if it wasn't for
    292  * the extra indirection by the linux_ipc system call.
    293  */
    294 inline int
    295 linux_shmdt(p, uap, retval)
    296 	struct proc *p;
    297 	struct linux_sys_ipc_args /* {
    298 		syscallarg(int) what;
    299 		syscallarg(int) a1;
    300 		syscallarg(int) a2;
    301 		syscallarg(int) a3;
    302 		syscallarg(caddr_t) ptr;
    303 	} */ *uap;
    304 	register_t *retval;
    305 {
    306 	struct sys_shmdt_args bsa;
    307 
    308 	SCARG(&bsa, shmaddr) = SCARG(uap, ptr);
    309 
    310 	return sys_shmdt(p, &bsa, retval);
    311 }
    312 
    313 /*
    314  * Same story as shmdt.
    315  */
    316 inline int
    317 linux_shmget(p, uap, retval)
    318 	struct proc *p;
    319 	struct linux_sys_ipc_args /* {
    320 		syscallarg(int) what;
    321 		syscallarg(int) a1;
    322 		syscallarg(int) a2;
    323 		syscallarg(int) a3;
    324 		syscallarg(caddr_t) ptr;
    325 	} */ *uap;
    326 	register_t *retval;
    327 {
    328 	struct sys_shmget_args bsa;
    329 
    330 	SCARG(&bsa, key) = SCARG(uap, a1);
    331 	SCARG(&bsa, size) = SCARG(uap, a2);
    332 	SCARG(&bsa, shmflg) = SCARG(uap, a3);
    333 
    334 	return sys_shmget(p, &bsa, retval);
    335 }
    336 
    337 #endif /* SYSVSHM */
    338