1 /* $NetBSD: linux_ipccall.c,v 1.22 2001/11/15 09:48:01 lukem 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 <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: linux_ipccall.c,v 1.22 2001/11/15 09:48:01 lukem Exp $"); 41 42 #if defined(_KERNEL_OPT) 43 #include "opt_sysv.h" 44 #endif 45 46 #include <sys/param.h> 47 #include <sys/shm.h> 48 #include <sys/sem.h> 49 #include <sys/msg.h> 50 #include <sys/proc.h> 51 #include <sys/systm.h> 52 53 /* real syscalls */ 54 #include <sys/mount.h> 55 #include <sys/syscallargs.h> 56 57 /* sys_ipc + args prototype */ 58 #include <compat/linux/common/linux_types.h> 59 #include <compat/linux/common/linux_signal.h> 60 61 #include <compat/linux/linux_syscallargs.h> 62 #include <compat/linux/linux_syscall.h> 63 64 /* general ipc defines */ 65 #include <compat/linux/common/linux_ipc.h> 66 67 /* prototypes for real/normal linux-emul syscalls */ 68 #include <compat/linux/common/linux_msg.h> 69 #include <compat/linux/common/linux_shm.h> 70 #include <compat/linux/common/linux_sem.h> 71 72 /* prototypes for sys_ipc stuff */ 73 #include <compat/linux/common/linux_ipccall.h> 74 75 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */ 76 /* Not used on: alpha */ 77 78 /* 79 * Stuff to deal with the SysV ipc/shm/semaphore interface in Linux. 80 * The main difference is, that Linux handles it all via one 81 * system call, which has the usual maximum amount of 5 arguments. 82 * This results in a kludge for calls that take 6 of them. 83 * 84 * The SYSV??? options have to be enabled to get the appropriate 85 * functions to work. 86 */ 87 88 int 89 linux_sys_ipc(p, v, retval) 90 struct proc *p; 91 void *v; 92 register_t *retval; 93 { 94 struct linux_sys_ipc_args /* { 95 syscallarg(int) what; 96 syscallarg(int) a1; 97 syscallarg(int) a2; 98 syscallarg(int) a3; 99 syscallarg(caddr_t) ptr; 100 } */ *uap = v; 101 102 switch (SCARG(uap, what)) { 103 #ifdef SYSVSEM 104 case LINUX_SYS_semop: 105 return linux_semop(p, uap, retval); 106 case LINUX_SYS_semget: 107 return linux_semget(p, uap, retval); 108 case LINUX_SYS_semctl: { 109 struct linux_sys_semctl_args bsa; 110 union linux_semun arg; 111 int error; 112 113 SCARG(&bsa, semid) = SCARG(uap, a1); 114 SCARG(&bsa, semnum) = SCARG(uap, a2); 115 SCARG(&bsa, cmd) = SCARG(uap, a3); 116 /* Convert from (union linux_semun *) to (union linux_semun) */ 117 if ((error = copyin(SCARG(uap, ptr), &arg, sizeof arg))) 118 return error; 119 SCARG(&bsa, arg) = arg; 120 121 return linux_sys_semctl(p, &bsa, retval); 122 } 123 #endif 124 #ifdef SYSVMSG 125 case LINUX_SYS_msgsnd: 126 return linux_msgsnd(p, uap, retval); 127 case LINUX_SYS_msgrcv: 128 return linux_msgrcv(p, uap, retval); 129 case LINUX_SYS_msgget: 130 return linux_msgget(p, uap, retval); 131 case LINUX_SYS_msgctl: { 132 struct linux_sys_msgctl_args bsa; 133 134 SCARG(&bsa, msqid) = SCARG(uap, a1); 135 SCARG(&bsa, cmd) = SCARG(uap, a2); 136 SCARG(&bsa, buf) = (struct linux_msqid_ds *)SCARG(uap, ptr); 137 138 return linux_sys_msgctl(p, &bsa, retval); 139 } 140 #endif 141 #ifdef SYSVSHM 142 case LINUX_SYS_shmat: { 143 struct linux_sys_shmat_args bsa; 144 145 SCARG(&bsa, shmid) = SCARG(uap, a1); 146 SCARG(&bsa, shmaddr) = (void *)SCARG(uap, ptr); 147 SCARG(&bsa, shmflg) = SCARG(uap, a2); 148 /* XXX passing pointer inside int here */ 149 SCARG(&bsa, raddr) = (u_long *)SCARG(uap, a3); 150 151 return linux_sys_shmat(p, &bsa, retval); 152 } 153 case LINUX_SYS_shmdt: 154 return linux_shmdt(p, uap, retval); 155 case LINUX_SYS_shmget: 156 return linux_shmget(p, uap, retval); 157 case LINUX_SYS_shmctl: { 158 struct linux_sys_shmctl_args bsa; 159 160 SCARG(&bsa, shmid) = SCARG(uap, a1); 161 SCARG(&bsa, cmd) = SCARG(uap, a2); 162 SCARG(&bsa, buf) = (struct linux_shmid_ds *)SCARG(uap, ptr); 163 164 return linux_sys_shmctl(p, &bsa, retval); 165 } 166 #endif 167 default: 168 return ENOSYS; 169 } 170 } 171 172 #ifdef SYSVSEM 173 inline int 174 linux_semop(p, uap, retval) 175 struct proc *p; 176 struct linux_sys_ipc_args /* { 177 syscallarg(int) what; 178 syscallarg(int) a1; 179 syscallarg(int) a2; 180 syscallarg(int) a3; 181 syscallarg(caddr_t) ptr; 182 } */ *uap; 183 register_t *retval; 184 { 185 struct sys_semop_args bsa; 186 187 SCARG(&bsa, semid) = SCARG(uap, a1); 188 SCARG(&bsa, sops) = (struct sembuf *)SCARG(uap, ptr); 189 SCARG(&bsa, nsops) = SCARG(uap, a2); 190 191 return sys_semop(p, &bsa, retval); 192 } 193 194 inline int 195 linux_semget(p, uap, retval) 196 struct proc *p; 197 struct linux_sys_ipc_args /* { 198 syscallarg(int) what; 199 syscallarg(int) a1; 200 syscallarg(int) a2; 201 syscallarg(int) a3; 202 syscallarg(caddr_t) ptr; 203 } */ *uap; 204 register_t *retval; 205 { 206 struct sys_semget_args bsa; 207 208 SCARG(&bsa, key) = (key_t)SCARG(uap, a1); 209 SCARG(&bsa, nsems) = SCARG(uap, a2); 210 SCARG(&bsa, semflg) = SCARG(uap, a3); 211 212 return sys_semget(p, &bsa, retval); 213 } 214 215 #endif /* SYSVSEM */ 216 217 #ifdef SYSVMSG 218 219 inline int 220 linux_msgsnd(p, uap, retval) 221 struct proc *p; 222 struct linux_sys_ipc_args /* { 223 syscallarg(int) what; 224 syscallarg(int) a1; 225 syscallarg(int) a2; 226 syscallarg(int) a3; 227 syscallarg(caddr_t) ptr; 228 } */ *uap; 229 register_t *retval; 230 { 231 struct sys_msgsnd_args bma; 232 233 SCARG(&bma, msqid) = SCARG(uap, a1); 234 SCARG(&bma, msgp) = SCARG(uap, ptr); 235 SCARG(&bma, msgsz) = SCARG(uap, a2); 236 SCARG(&bma, msgflg) = SCARG(uap, a3); 237 238 return sys_msgsnd(p, &bma, retval); 239 } 240 241 inline int 242 linux_msgrcv(p, uap, retval) 243 struct proc *p; 244 struct linux_sys_ipc_args /* { 245 syscallarg(int) what; 246 syscallarg(int) a1; 247 syscallarg(int) a2; 248 syscallarg(int) a3; 249 syscallarg(caddr_t) ptr; 250 } */ *uap; 251 register_t *retval; 252 { 253 struct sys_msgrcv_args bma; 254 struct linux_msgrcv_msgarg kluge; 255 int error; 256 257 if ((error = copyin(SCARG(uap, ptr), &kluge, sizeof kluge))) 258 return error; 259 260 SCARG(&bma, msqid) = SCARG(uap, a1); 261 SCARG(&bma, msgp) = kluge.msg; 262 SCARG(&bma, msgsz) = SCARG(uap, a2); 263 SCARG(&bma, msgtyp) = kluge.type; 264 SCARG(&bma, msgflg) = SCARG(uap, a3); 265 266 return sys_msgrcv(p, &bma, retval); 267 } 268 269 inline int 270 linux_msgget(p, uap, retval) 271 struct proc *p; 272 struct linux_sys_ipc_args /* { 273 syscallarg(int) what; 274 syscallarg(int) a1; 275 syscallarg(int) a2; 276 syscallarg(int) a3; 277 syscallarg(caddr_t) ptr; 278 } */ *uap; 279 register_t *retval; 280 { 281 struct sys_msgget_args bma; 282 283 SCARG(&bma, key) = (key_t)SCARG(uap, a1); 284 SCARG(&bma, msgflg) = SCARG(uap, a2); 285 286 return sys_msgget(p, &bma, retval); 287 } 288 289 #endif /* SYSVMSG */ 290 291 #ifdef SYSVSHM 292 /* 293 * shmdt(): this could have been mapped directly, if it wasn't for 294 * the extra indirection by the linux_ipc system call. 295 */ 296 inline int 297 linux_shmdt(p, uap, retval) 298 struct proc *p; 299 struct linux_sys_ipc_args /* { 300 syscallarg(int) what; 301 syscallarg(int) a1; 302 syscallarg(int) a2; 303 syscallarg(int) a3; 304 syscallarg(caddr_t) ptr; 305 } */ *uap; 306 register_t *retval; 307 { 308 struct sys_shmdt_args bsa; 309 310 SCARG(&bsa, shmaddr) = SCARG(uap, ptr); 311 312 return sys_shmdt(p, &bsa, retval); 313 } 314 315 /* 316 * Same story as shmdt. 317 */ 318 inline int 319 linux_shmget(p, uap, retval) 320 struct proc *p; 321 struct linux_sys_ipc_args /* { 322 syscallarg(int) what; 323 syscallarg(int) a1; 324 syscallarg(int) a2; 325 syscallarg(int) a3; 326 syscallarg(caddr_t) ptr; 327 } */ *uap; 328 register_t *retval; 329 { 330 struct sys_shmget_args bsa; 331 332 SCARG(&bsa, key) = SCARG(uap, a1); 333 SCARG(&bsa, size) = SCARG(uap, a2); 334 SCARG(&bsa, shmflg) = SCARG(uap, a3); 335 336 return sys_shmget(p, &bsa, retval); 337 } 338 339 #endif /* SYSVSHM */ 340