1 /* $NetBSD: linux_ipc.c,v 1.28 2003/01/18 08:02:53 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 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_ipc.c,v 1.28 2003/01/18 08:02:53 thorpej 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 #include <sys/mount.h> 54 #include <sys/sa.h> 55 #include <sys/syscallargs.h> 56 57 #include <compat/linux/common/linux_types.h> 58 #include <compat/linux/common/linux_signal.h> 59 #include <compat/linux/common/linux_util.h> 60 61 #include <compat/linux/linux_syscallargs.h> 62 #include <compat/linux/linux_syscall.h> 63 64 #include <compat/linux/common/linux_ipc.h> 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 #include <compat/linux/common/linux_ipccall.h> 69 70 /* 71 * Note: Not all linux architechtures have explicit versions 72 * of the SYSV* syscalls. On the ones that don't 73 * we pretend that they are defined anyway. *_args and 74 * prototypes are defined in individual headers; 75 * syscalls.master lists those syscalls as NOARGS. 76 * 77 * The functions in multiarch are the ones that just need 78 * the arguments shuffled around and then use the 79 * normal NetBSD syscall. 80 * 81 * Function in multiarch: 82 * linux_sys_ipc : linux_ipccall.c 83 * liunx_semop : linux_ipccall.c 84 * linux_semget : linux_ipccall.c 85 * linux_msgsnd : linux_ipccall.c 86 * linux_msgrcv : linux_ipccall.c 87 * linux_msgget : linux_ipccall.c 88 * linux_shmdt : linux_ipccall.c 89 * linux_shmget : linux_ipccall.c 90 */ 91 92 #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG) 93 /* 94 * Convert between Linux and NetBSD ipc_perm structures. Only the 95 * order of the fields is different. 96 */ 97 void 98 linux_to_bsd_ipc_perm(lpp, bpp) 99 struct linux_ipc_perm *lpp; 100 struct ipc_perm *bpp; 101 { 102 103 bpp->_key = lpp->l_key; 104 bpp->uid = lpp->l_uid; 105 bpp->gid = lpp->l_gid; 106 bpp->cuid = lpp->l_cuid; 107 bpp->cgid = lpp->l_cgid; 108 bpp->mode = lpp->l_mode; 109 bpp->_seq = lpp->l_seq; 110 } 111 112 void 113 bsd_to_linux_ipc_perm(bpp, lpp) 114 struct ipc_perm *bpp; 115 struct linux_ipc_perm *lpp; 116 { 117 118 lpp->l_key = bpp->_key; 119 lpp->l_uid = bpp->uid; 120 lpp->l_gid = bpp->gid; 121 lpp->l_cuid = bpp->cuid; 122 lpp->l_cgid = bpp->cgid; 123 lpp->l_mode = bpp->mode; 124 lpp->l_seq = bpp->_seq; 125 } 126 #endif 127 128 #ifdef SYSVSEM 129 /* 130 * Semaphore operations. Most constants and structures are the same on 131 * both systems. Only semctl() needs some extra work. 132 */ 133 134 /* 135 * Convert between Linux and NetBSD semid_ds structures. 136 */ 137 void 138 bsd_to_linux_semid_ds(bs, ls) 139 struct semid_ds *bs; 140 struct linux_semid_ds *ls; 141 { 142 143 bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm); 144 ls->l_sem_otime = bs->sem_otime; 145 ls->l_sem_ctime = bs->sem_ctime; 146 ls->l_sem_nsems = bs->sem_nsems; 147 ls->l_sem_base = bs->_sem_base; 148 } 149 150 void 151 linux_to_bsd_semid_ds(ls, bs) 152 struct linux_semid_ds *ls; 153 struct semid_ds *bs; 154 { 155 156 linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm); 157 bs->sem_otime = ls->l_sem_otime; 158 bs->sem_ctime = ls->l_sem_ctime; 159 bs->sem_nsems = ls->l_sem_nsems; 160 bs->_sem_base = ls->l_sem_base; 161 } 162 163 /* 164 * Most of this can be handled by directly passing the arguments on; we 165 * just need to frob the `cmd' and convert the semid_ds and semun. 166 */ 167 int 168 linux_sys_semctl(l, v, retval) 169 struct lwp *l; 170 void *v; 171 register_t *retval; 172 { 173 struct linux_sys_semctl_args /* { 174 syscallarg(int) semid; 175 syscallarg(int) semnum; 176 syscallarg(int) cmd; 177 syscallarg(union linux_semun) arg; 178 } */ *uap = v; 179 struct proc *p = l->l_proc; 180 struct semid_ds sembuf; 181 struct linux_semid_ds lsembuf; 182 union __semun semun; 183 int cmd, error; 184 void *pass_arg = NULL; 185 186 cmd = SCARG(uap, cmd); 187 188 switch (cmd) { 189 case LINUX_IPC_SET: 190 pass_arg = &sembuf; 191 cmd = IPC_SET; 192 break; 193 194 case LINUX_IPC_STAT: 195 pass_arg = &sembuf; 196 cmd = IPC_STAT; 197 break; 198 199 case LINUX_IPC_RMID: 200 cmd = IPC_RMID; 201 break; 202 203 case LINUX_GETVAL: 204 cmd = GETVAL; 205 break; 206 207 case LINUX_GETPID: 208 cmd = GETPID; 209 break; 210 211 case LINUX_GETNCNT: 212 cmd = GETNCNT; 213 break; 214 215 case LINUX_GETZCNT: 216 cmd = GETZCNT; 217 break; 218 219 case LINUX_GETALL: 220 pass_arg = &semun; 221 semun.array = SCARG(uap, arg).l_array; 222 cmd = GETALL; 223 break; 224 225 case LINUX_SETVAL: 226 pass_arg = &semun; 227 semun.val = SCARG(uap, arg).l_val; 228 cmd = SETVAL; 229 break; 230 231 case LINUX_SETALL: 232 pass_arg = &semun; 233 semun.array = SCARG(uap, arg).l_array; 234 cmd = SETALL; 235 break; 236 237 default: 238 return (EINVAL); 239 } 240 241 if (cmd == IPC_SET) { 242 error = copyin(SCARG(uap, arg).l_buf, &lsembuf, 243 sizeof(lsembuf)); 244 if (error) 245 return (error); 246 linux_to_bsd_semid_ds(&lsembuf, &sembuf); 247 } 248 249 error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd, 250 pass_arg, retval); 251 252 if (error == 0 && cmd == IPC_STAT) { 253 bsd_to_linux_semid_ds(&sembuf, &lsembuf); 254 error = copyout(&lsembuf, SCARG(uap, arg).l_buf, 255 sizeof(lsembuf)); 256 } 257 258 return (error); 259 } 260 #endif /* SYSVSEM */ 261 262 #ifdef SYSVMSG 263 264 void 265 linux_to_bsd_msqid_ds(lmp, bmp) 266 struct linux_msqid_ds *lmp; 267 struct msqid_ds *bmp; 268 { 269 270 linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm); 271 bmp->_msg_first = lmp->l_msg_first; 272 bmp->_msg_last = lmp->l_msg_last; 273 bmp->_msg_cbytes = lmp->l_msg_cbytes; 274 bmp->msg_qnum = lmp->l_msg_qnum; 275 bmp->msg_qbytes = lmp->l_msg_qbytes; 276 bmp->msg_lspid = lmp->l_msg_lspid; 277 bmp->msg_lrpid = lmp->l_msg_lrpid; 278 bmp->msg_stime = lmp->l_msg_stime; 279 bmp->msg_rtime = lmp->l_msg_rtime; 280 bmp->msg_ctime = lmp->l_msg_ctime; 281 } 282 283 void 284 bsd_to_linux_msqid_ds(bmp, lmp) 285 struct msqid_ds *bmp; 286 struct linux_msqid_ds *lmp; 287 { 288 289 bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm); 290 lmp->l_msg_first = bmp->_msg_first; 291 lmp->l_msg_last = bmp->_msg_last; 292 lmp->l_msg_cbytes = bmp->_msg_cbytes; 293 lmp->l_msg_qnum = bmp->msg_qnum; 294 lmp->l_msg_qbytes = bmp->msg_qbytes; 295 lmp->l_msg_lspid = bmp->msg_lspid; 296 lmp->l_msg_lrpid = bmp->msg_lrpid; 297 lmp->l_msg_stime = bmp->msg_stime; 298 lmp->l_msg_rtime = bmp->msg_rtime; 299 lmp->l_msg_ctime = bmp->msg_ctime; 300 } 301 302 int 303 linux_sys_msgctl(l, v, retval) 304 struct lwp *l; 305 void *v; 306 register_t *retval; 307 { 308 struct linux_sys_msgctl_args /* { 309 syscallarg(int) msqid; 310 syscallarg(int) cmd; 311 syscallarg(struct linux_msqid_ds *) buf; 312 } */ *uap = v; 313 struct proc *p = l->l_proc; 314 caddr_t sg; 315 struct sys___msgctl13_args nua; 316 struct msqid_ds *bmp, bm; 317 struct linux_msqid_ds lm; 318 int error; 319 320 SCARG(&nua, msqid) = SCARG(uap, msqid); 321 switch (SCARG(uap, cmd)) { 322 case LINUX_IPC_STAT: 323 sg = stackgap_init(p, 0); 324 bmp = stackgap_alloc(p, &sg, sizeof (struct msqid_ds)); 325 SCARG(&nua, cmd) = IPC_STAT; 326 SCARG(&nua, buf) = bmp; 327 if ((error = sys___msgctl13(l, &nua, retval))) 328 return error; 329 if ((error = copyin(bmp, &bm, sizeof bm))) 330 return error; 331 bsd_to_linux_msqid_ds(&bm, &lm); 332 return copyout(&lm, SCARG(uap, buf), sizeof lm); 333 case LINUX_IPC_SET: 334 if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm))) 335 return error; 336 linux_to_bsd_msqid_ds(&lm, &bm); 337 sg = stackgap_init(p, 0); 338 bmp = stackgap_alloc(p, &sg, sizeof bm); 339 if ((error = copyout(&bm, bmp, sizeof bm))) 340 return error; 341 SCARG(&nua, cmd) = IPC_SET; 342 SCARG(&nua, buf) = bmp; 343 break; 344 case LINUX_IPC_RMID: 345 SCARG(&nua, cmd) = IPC_RMID; 346 SCARG(&nua, buf) = NULL; 347 break; 348 default: 349 return EINVAL; 350 } 351 return sys___msgctl13(l, &nua, retval); 352 } 353 #endif /* SYSVMSG */ 354 355 #ifdef SYSVSHM 356 /* 357 * shmat(2). Very straightforward, except that Linux passes a pointer 358 * in which the return value is to be passed. This is subsequently 359 * handled by libc, apparently. 360 */ 361 int 362 linux_sys_shmat(l, v, retval) 363 struct lwp *l; 364 void *v; 365 register_t *retval; 366 { 367 struct linux_sys_shmat_args /* { 368 syscallarg(int) shmid; 369 syscallarg(void *) shmaddr; 370 syscallarg(int) shmflg; 371 syscallarg(u_long *) raddr; 372 } */ *uap = v; 373 struct proc *p = l->l_proc; 374 int error; 375 vaddr_t attach_va; 376 u_long raddr; 377 378 if ((error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr), 379 SCARG(uap, shmflg), &attach_va, 1))) 380 return error; 381 382 raddr = (u_long)attach_va; 383 384 if ((error = copyout(&raddr, (caddr_t) SCARG(uap, raddr), 385 sizeof retval[0]))) 386 return error; 387 388 retval[0] = 0; 389 return 0; 390 } 391 392 /* 393 * Convert between Linux and NetBSD shmid_ds structures. 394 * The order of the fields is once again the difference, and 395 * we also need a place to store the internal data pointer 396 * in, which is unfortunately stored in this structure. 397 * 398 * We abuse a Linux internal field for that. 399 */ 400 void 401 linux_to_bsd_shmid_ds(lsp, bsp) 402 struct linux_shmid_ds *lsp; 403 struct shmid_ds *bsp; 404 { 405 406 linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm); 407 bsp->shm_segsz = lsp->l_shm_segsz; 408 bsp->shm_lpid = lsp->l_shm_lpid; 409 bsp->shm_cpid = lsp->l_shm_cpid; 410 bsp->shm_nattch = lsp->l_shm_nattch; 411 bsp->shm_atime = lsp->l_shm_atime; 412 bsp->shm_dtime = lsp->l_shm_dtime; 413 bsp->shm_ctime = lsp->l_shm_ctime; 414 bsp->_shm_internal = lsp->l_private2; /* XXX Oh well. */ 415 } 416 417 void 418 bsd_to_linux_shmid_ds(bsp, lsp) 419 struct shmid_ds *bsp; 420 struct linux_shmid_ds *lsp; 421 { 422 423 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm); 424 lsp->l_shm_segsz = bsp->shm_segsz; 425 lsp->l_shm_lpid = bsp->shm_lpid; 426 lsp->l_shm_cpid = bsp->shm_cpid; 427 lsp->l_shm_nattch = bsp->shm_nattch; 428 lsp->l_shm_atime = bsp->shm_atime; 429 lsp->l_shm_dtime = bsp->shm_dtime; 430 lsp->l_shm_ctime = bsp->shm_ctime; 431 lsp->l_private2 = bsp->_shm_internal; /* XXX */ 432 } 433 434 /* 435 * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT 436 * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented 437 * by NetBSD itself. 438 * 439 * The usual structure conversion and massaging is done. 440 */ 441 int 442 linux_sys_shmctl(l, v, retval) 443 struct lwp *l; 444 void *v; 445 register_t *retval; 446 { 447 struct linux_sys_shmctl_args /* { 448 syscallarg(int) shmid; 449 syscallarg(int) cmd; 450 syscallarg(struct linux_shmid_ds *) buf; 451 } */ *uap = v; 452 struct proc *p = l->l_proc; 453 caddr_t sg; 454 struct sys___shmctl13_args nua; 455 struct shmid_ds *bsp, bs; 456 struct linux_shmid_ds ls; 457 int error; 458 459 SCARG(&nua, shmid) = SCARG(uap, shmid); 460 switch (SCARG(uap, cmd)) { 461 case LINUX_IPC_STAT: 462 sg = stackgap_init(p, 0); 463 bsp = stackgap_alloc(p, &sg, sizeof(struct shmid_ds)); 464 SCARG(&nua, cmd) = IPC_STAT; 465 SCARG(&nua, buf) = bsp; 466 if ((error = sys___shmctl13(l, &nua, retval))) 467 return error; 468 if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs))) 469 return error; 470 bsd_to_linux_shmid_ds(&bs, &ls); 471 return copyout(&ls, SCARG(uap, buf), sizeof ls); 472 case LINUX_IPC_SET: 473 if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls))) 474 return error; 475 linux_to_bsd_shmid_ds(&ls, &bs); 476 sg = stackgap_init(p, 0); 477 bsp = stackgap_alloc(p, &sg, sizeof bs); 478 if ((error = copyout(&bs, bsp, sizeof bs))) 479 return error; 480 SCARG(&nua, cmd) = IPC_SET; 481 SCARG(&nua, buf) = bsp; 482 break; 483 case LINUX_IPC_RMID: 484 SCARG(&nua, cmd) = IPC_RMID; 485 SCARG(&nua, buf) = NULL; 486 break; 487 case LINUX_SHM_LOCK: 488 SCARG(&nua, cmd) = SHM_LOCK; 489 SCARG(&nua, buf) = NULL; 490 break; 491 case LINUX_SHM_UNLOCK: 492 SCARG(&nua, cmd) = SHM_UNLOCK; 493 SCARG(&nua, buf) = NULL; 494 break; 495 case LINUX_IPC_INFO: 496 case LINUX_SHM_STAT: 497 case LINUX_SHM_INFO: 498 default: 499 return EINVAL; 500 } 501 return sys___shmctl13(l, &nua, retval); 502 } 503 #endif /* SYSVSHM */ 504