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