1 /* $NetBSD: sysv_shm.c,v 1.68 2003/02/20 22:16:07 atatat Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1994 Adam Glass and Charles M. Hannum. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by Adam Glass and Charles M. 54 * Hannum. 55 * 4. The names of the authors may not be used to endorse or promote products 56 * derived from this software without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 61 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68 */ 69 70 #include <sys/cdefs.h> 71 __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.68 2003/02/20 22:16:07 atatat Exp $"); 72 73 #define SYSVSHM 74 75 #include <sys/param.h> 76 #include <sys/kernel.h> 77 #include <sys/shm.h> 78 #include <sys/malloc.h> 79 #include <sys/mman.h> 80 #include <sys/stat.h> 81 #include <sys/sysctl.h> 82 #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */ 83 #include <sys/sa.h> 84 #include <sys/syscallargs.h> 85 86 #include <uvm/uvm_extern.h> 87 88 struct shmid_ds *shm_find_segment_by_shmid __P((int, int)); 89 90 MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments"); 91 92 /* 93 * Provides the following externally accessible functions: 94 * 95 * shminit(void); initialization 96 * shmexit(struct vmspace *) cleanup 97 * shmfork(struct vmspace *, struct vmspace *) fork handling 98 * 99 * Structures: 100 * shmsegs (an array of 'struct shmid_ds') 101 * per proc array of 'struct shmmap_state' 102 */ 103 104 #define SHMSEG_FREE 0x0200 105 #define SHMSEG_REMOVED 0x0400 106 #define SHMSEG_ALLOCATED 0x0800 107 #define SHMSEG_WANTED 0x1000 108 109 int shm_last_free, shm_nused, shm_committed; 110 struct shmid_ds *shmsegs; 111 112 struct shm_handle { 113 struct uvm_object *shm_object; 114 }; 115 116 struct shmmap_state { 117 vaddr_t va; 118 int shmid; 119 }; 120 121 static int shm_find_segment_by_key __P((key_t)); 122 static void shm_deallocate_segment __P((struct shmid_ds *)); 123 static void shm_delete_mapping __P((struct vmspace *, struct shmmap_state *)); 124 static int shmget_existing __P((struct proc *, struct sys_shmget_args *, 125 int, int, register_t *)); 126 static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *, 127 int, register_t *)); 128 129 static int 130 shm_find_segment_by_key(key) 131 key_t key; 132 { 133 int i; 134 135 for (i = 0; i < shminfo.shmmni; i++) 136 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) && 137 shmsegs[i].shm_perm._key == key) 138 return i; 139 return -1; 140 } 141 142 struct shmid_ds * 143 shm_find_segment_by_shmid(shmid, findremoved) 144 int shmid; 145 int findremoved; 146 { 147 int segnum; 148 struct shmid_ds *shmseg; 149 150 segnum = IPCID_TO_IX(shmid); 151 if (segnum < 0 || segnum >= shminfo.shmmni) 152 return NULL; 153 shmseg = &shmsegs[segnum]; 154 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0) 155 return NULL; 156 if (!findremoved && ((shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0)) 157 return NULL; 158 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid)) 159 return NULL; 160 return shmseg; 161 } 162 163 static void 164 shm_deallocate_segment(shmseg) 165 struct shmid_ds *shmseg; 166 { 167 struct shm_handle *shm_handle; 168 size_t size; 169 170 shm_handle = shmseg->_shm_internal; 171 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 172 uao_detach(shm_handle->shm_object); 173 free((caddr_t)shm_handle, M_SHM); 174 shmseg->_shm_internal = NULL; 175 shm_committed -= btoc(size); 176 shmseg->shm_perm.mode = SHMSEG_FREE; 177 shm_nused--; 178 } 179 180 static void 181 shm_delete_mapping(vm, shmmap_s) 182 struct vmspace *vm; 183 struct shmmap_state *shmmap_s; 184 { 185 struct shmid_ds *shmseg; 186 int segnum; 187 size_t size; 188 189 segnum = IPCID_TO_IX(shmmap_s->shmid); 190 shmseg = &shmsegs[segnum]; 191 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 192 uvm_deallocate(&vm->vm_map, shmmap_s->va, size); 193 shmmap_s->shmid = -1; 194 shmseg->shm_dtime = time.tv_sec; 195 if ((--shmseg->shm_nattch <= 0) && 196 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) { 197 shm_deallocate_segment(shmseg); 198 shm_last_free = segnum; 199 } 200 } 201 202 int 203 sys_shmdt(l, v, retval) 204 struct lwp *l; 205 void *v; 206 register_t *retval; 207 { 208 struct sys_shmdt_args /* { 209 syscallarg(const void *) shmaddr; 210 } */ *uap = v; 211 struct proc *p = l->l_proc; 212 struct shmmap_state *shmmap_s; 213 int i; 214 215 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 216 if (shmmap_s == NULL) 217 return EINVAL; 218 219 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 220 if (shmmap_s->shmid != -1 && 221 shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr)) 222 break; 223 if (i == shminfo.shmseg) 224 return EINVAL; 225 shm_delete_mapping(p->p_vmspace, shmmap_s); 226 return 0; 227 } 228 229 int 230 sys_shmat(l, v, retval) 231 struct lwp *l; 232 void *v; 233 register_t *retval; 234 { 235 struct sys_shmat_args /* { 236 syscallarg(int) shmid; 237 syscallarg(const void *) shmaddr; 238 syscallarg(int) shmflg; 239 } */ *uap = v; 240 struct proc *p = l->l_proc; 241 vaddr_t attach_va; 242 int error; 243 244 error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr), 245 SCARG(uap, shmflg), &attach_va, 0); 246 if (error != 0) 247 return error; 248 retval[0] = attach_va; 249 return 0; 250 } 251 252 int 253 shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved) 254 struct proc *p; 255 int shmid; 256 const void *shmaddr; 257 int shmflg; 258 vaddr_t *attachp; 259 int findremoved; 260 { 261 int error, i, flags; 262 struct ucred *cred = p->p_ucred; 263 struct shmid_ds *shmseg; 264 struct shmmap_state *shmmap_s = NULL; 265 struct shm_handle *shm_handle; 266 vaddr_t attach_va; 267 vm_prot_t prot; 268 vsize_t size; 269 270 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 271 if (shmmap_s == NULL) { 272 size = shminfo.shmseg * sizeof(struct shmmap_state); 273 shmmap_s = malloc(size, M_SHM, M_WAITOK); 274 for (i = 0; i < shminfo.shmseg; i++) 275 shmmap_s[i].shmid = -1; 276 p->p_vmspace->vm_shm = (caddr_t)shmmap_s; 277 } 278 shmseg = shm_find_segment_by_shmid(shmid, findremoved); 279 if (shmseg == NULL) 280 return EINVAL; 281 error = ipcperm(cred, &shmseg->shm_perm, 282 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W); 283 if (error) 284 return error; 285 for (i = 0; i < shminfo.shmseg; i++) { 286 if (shmmap_s->shmid == -1) 287 break; 288 shmmap_s++; 289 } 290 if (i >= shminfo.shmseg) 291 return EMFILE; 292 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 293 prot = VM_PROT_READ; 294 if ((shmflg & SHM_RDONLY) == 0) 295 prot |= VM_PROT_WRITE; 296 flags = MAP_ANON | MAP_SHARED; 297 if (shmaddr) { 298 flags |= MAP_FIXED; 299 if (shmflg & SHM_RND) 300 attach_va = 301 (vaddr_t)shmaddr & ~(SHMLBA-1); 302 else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0) 303 attach_va = (vaddr_t)shmaddr; 304 else 305 return EINVAL; 306 } else { 307 /* This is just a hint to uvm_mmap() about where to put it. */ 308 attach_va = VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size); 309 } 310 shm_handle = shmseg->_shm_internal; 311 uao_reference(shm_handle->shm_object); 312 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size, 313 shm_handle->shm_object, 0, 0, 314 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0)); 315 if (error) { 316 return error; 317 } 318 shmmap_s->va = attach_va; 319 shmmap_s->shmid = shmid; 320 shmseg->shm_lpid = p->p_pid; 321 shmseg->shm_atime = time.tv_sec; 322 shmseg->shm_nattch++; 323 *attachp = attach_va; 324 return 0; 325 } 326 327 int 328 sys___shmctl13(l, v, retval) 329 struct lwp *l; 330 void *v; 331 register_t *retval; 332 { 333 struct sys___shmctl13_args /* { 334 syscallarg(int) shmid; 335 syscallarg(int) cmd; 336 syscallarg(struct shmid_ds *) buf; 337 } */ *uap = v; 338 struct proc *p = l->l_proc; 339 struct shmid_ds shmbuf; 340 int cmd, error; 341 342 cmd = SCARG(uap, cmd); 343 344 if (cmd == IPC_SET) { 345 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf)); 346 if (error) 347 return (error); 348 } 349 350 error = shmctl1(p, SCARG(uap, shmid), cmd, 351 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL); 352 353 if (error == 0 && cmd == IPC_STAT) 354 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf)); 355 356 return (error); 357 } 358 359 int 360 shmctl1(p, shmid, cmd, shmbuf) 361 struct proc *p; 362 int shmid; 363 int cmd; 364 struct shmid_ds *shmbuf; 365 { 366 struct ucred *cred = p->p_ucred; 367 struct shmid_ds *shmseg; 368 int error = 0; 369 370 shmseg = shm_find_segment_by_shmid(shmid, 0); 371 if (shmseg == NULL) 372 return EINVAL; 373 switch (cmd) { 374 case IPC_STAT: 375 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0) 376 return error; 377 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds)); 378 break; 379 case IPC_SET: 380 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0) 381 return error; 382 shmseg->shm_perm.uid = shmbuf->shm_perm.uid; 383 shmseg->shm_perm.gid = shmbuf->shm_perm.gid; 384 shmseg->shm_perm.mode = 385 (shmseg->shm_perm.mode & ~ACCESSPERMS) | 386 (shmbuf->shm_perm.mode & ACCESSPERMS); 387 shmseg->shm_ctime = time.tv_sec; 388 break; 389 case IPC_RMID: 390 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0) 391 return error; 392 shmseg->shm_perm._key = IPC_PRIVATE; 393 shmseg->shm_perm.mode |= SHMSEG_REMOVED; 394 if (shmseg->shm_nattch <= 0) { 395 shm_deallocate_segment(shmseg); 396 shm_last_free = IPCID_TO_IX(shmid); 397 } 398 break; 399 case SHM_LOCK: 400 case SHM_UNLOCK: 401 default: 402 return EINVAL; 403 } 404 return 0; 405 } 406 407 static int 408 shmget_existing(p, uap, mode, segnum, retval) 409 struct proc *p; 410 struct sys_shmget_args /* { 411 syscallarg(key_t) key; 412 syscallarg(size_t) size; 413 syscallarg(int) shmflg; 414 } */ *uap; 415 int mode; 416 int segnum; 417 register_t *retval; 418 { 419 struct shmid_ds *shmseg; 420 struct ucred *cred = p->p_ucred; 421 int error; 422 423 shmseg = &shmsegs[segnum]; 424 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) { 425 /* 426 * This segment is in the process of being allocated. Wait 427 * until it's done, and look the key up again (in case the 428 * allocation failed or it was freed). 429 */ 430 shmseg->shm_perm.mode |= SHMSEG_WANTED; 431 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0); 432 if (error) 433 return error; 434 return EAGAIN; 435 } 436 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0) 437 return error; 438 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz) 439 return EINVAL; 440 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) == 441 (IPC_CREAT | IPC_EXCL)) 442 return EEXIST; 443 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 444 return 0; 445 } 446 447 static int 448 shmget_allocate_segment(p, uap, mode, retval) 449 struct proc *p; 450 struct sys_shmget_args /* { 451 syscallarg(key_t) key; 452 syscallarg(size_t) size; 453 syscallarg(int) shmflg; 454 } */ *uap; 455 int mode; 456 register_t *retval; 457 { 458 int i, segnum, shmid, size; 459 struct ucred *cred = p->p_ucred; 460 struct shmid_ds *shmseg; 461 struct shm_handle *shm_handle; 462 int error = 0; 463 464 if (SCARG(uap, size) < shminfo.shmmin || 465 SCARG(uap, size) > shminfo.shmmax) 466 return EINVAL; 467 if (shm_nused >= shminfo.shmmni) /* any shmids left? */ 468 return ENOSPC; 469 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET; 470 if (shm_committed + btoc(size) > shminfo.shmall) 471 return ENOMEM; 472 if (shm_last_free < 0) { 473 for (i = 0; i < shminfo.shmmni; i++) 474 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE) 475 break; 476 if (i == shminfo.shmmni) 477 panic("shmseg free count inconsistent"); 478 segnum = i; 479 } else { 480 segnum = shm_last_free; 481 shm_last_free = -1; 482 } 483 shmseg = &shmsegs[segnum]; 484 /* 485 * In case we sleep in malloc(), mark the segment present but deleted 486 * so that noone else tries to create the same key. 487 */ 488 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; 489 shmseg->shm_perm._key = SCARG(uap, key); 490 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff; 491 shm_handle = (struct shm_handle *) 492 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK); 493 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 494 495 shm_handle->shm_object = uao_create(size, 0); 496 497 shmseg->_shm_internal = shm_handle; 498 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid; 499 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid; 500 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) | 501 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED; 502 shmseg->shm_segsz = SCARG(uap, size); 503 shmseg->shm_cpid = p->p_pid; 504 shmseg->shm_lpid = shmseg->shm_nattch = 0; 505 shmseg->shm_atime = shmseg->shm_dtime = 0; 506 shmseg->shm_ctime = time.tv_sec; 507 shm_committed += btoc(size); 508 shm_nused++; 509 510 *retval = shmid; 511 if (shmseg->shm_perm.mode & SHMSEG_WANTED) { 512 /* 513 * Somebody else wanted this key while we were asleep. Wake 514 * them up now. 515 */ 516 shmseg->shm_perm.mode &= ~SHMSEG_WANTED; 517 wakeup((caddr_t)shmseg); 518 } 519 return error; 520 } 521 522 int 523 sys_shmget(l, v, retval) 524 struct lwp *l; 525 void *v; 526 register_t *retval; 527 { 528 struct sys_shmget_args /* { 529 syscallarg(key_t) key; 530 syscallarg(int) size; 531 syscallarg(int) shmflg; 532 } */ *uap = v; 533 struct proc *p = l->l_proc; 534 int segnum, mode, error; 535 536 mode = SCARG(uap, shmflg) & ACCESSPERMS; 537 if (SCARG(uap, key) != IPC_PRIVATE) { 538 again: 539 segnum = shm_find_segment_by_key(SCARG(uap, key)); 540 if (segnum >= 0) { 541 error = shmget_existing(p, uap, mode, segnum, retval); 542 if (error == EAGAIN) 543 goto again; 544 return error; 545 } 546 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0) 547 return ENOENT; 548 } 549 return shmget_allocate_segment(p, uap, mode, retval); 550 } 551 552 void 553 shmfork(vm1, vm2) 554 struct vmspace *vm1, *vm2; 555 { 556 struct shmmap_state *shmmap_s; 557 size_t size; 558 int i; 559 560 if (vm1->vm_shm == NULL) { 561 vm2->vm_shm = NULL; 562 return; 563 } 564 565 size = shminfo.shmseg * sizeof(struct shmmap_state); 566 shmmap_s = malloc(size, M_SHM, M_WAITOK); 567 memcpy(shmmap_s, vm1->vm_shm, size); 568 vm2->vm_shm = (caddr_t)shmmap_s; 569 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 570 if (shmmap_s->shmid != -1) 571 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++; 572 } 573 574 void 575 shmexit(vm) 576 struct vmspace *vm; 577 { 578 struct shmmap_state *shmmap_s; 579 int i; 580 581 shmmap_s = (struct shmmap_state *)vm->vm_shm; 582 if (shmmap_s == NULL) 583 return; 584 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 585 if (shmmap_s->shmid != -1) 586 shm_delete_mapping(vm, shmmap_s); 587 free(vm->vm_shm, M_SHM); 588 vm->vm_shm = NULL; 589 } 590 591 void 592 shminit() 593 { 594 int i; 595 596 shminfo.shmmax *= PAGE_SIZE; 597 598 for (i = 0; i < shminfo.shmmni; i++) { 599 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 600 shmsegs[i].shm_perm._seq = 0; 601 } 602 shm_last_free = 0; 603 shm_nused = 0; 604 shm_committed = 0; 605 } 606