1 /* $NetBSD: sysv_shm.c,v 1.86 2005/12/07 06:14:13 thorpej 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.86 2005/12/07 06:14:13 thorpej 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 #include <sys/queue.h> 86 #include <sys/pool.h> 87 88 #include <uvm/uvm_extern.h> 89 #include <uvm/uvm_object.h> 90 91 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments"); 92 93 /* 94 * Provides the following externally accessible functions: 95 * 96 * shminit(void); initialization 97 * shmexit(struct vmspace *) cleanup 98 * shmfork(struct vmspace *, struct vmspace *) fork handling 99 * 100 * Structures: 101 * shmsegs (an array of 'struct shmid_ds') 102 * per proc array of 'struct shmmap_state' 103 */ 104 105 int shm_nused; 106 struct shmid_ds *shmsegs; 107 108 struct shmmap_entry { 109 SLIST_ENTRY(shmmap_entry) next; 110 vaddr_t va; 111 int shmid; 112 }; 113 114 static int shm_last_free, shm_committed; 115 116 static POOL_INIT(shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0, 117 "shmmp", &pool_allocator_nointr); 118 119 struct shmmap_state { 120 unsigned int nitems; 121 unsigned int nrefs; 122 SLIST_HEAD(, shmmap_entry) entries; 123 }; 124 125 static int shm_find_segment_by_key(key_t); 126 static void shm_deallocate_segment(struct shmid_ds *); 127 static void shm_delete_mapping(struct vmspace *, struct shmmap_state *, 128 struct shmmap_entry *); 129 static int shmget_existing(struct proc *, struct sys_shmget_args *, 130 int, int, register_t *); 131 static int shmget_allocate_segment(struct proc *, struct sys_shmget_args *, 132 int, register_t *); 133 static struct shmmap_state *shmmap_getprivate(struct proc *); 134 static struct shmmap_entry *shm_find_mapping(struct shmmap_state *, vaddr_t); 135 136 static int 137 shm_find_segment_by_key(key_t key) 138 { 139 int i; 140 141 for (i = 0; i < shminfo.shmmni; i++) 142 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) && 143 shmsegs[i].shm_perm._key == key) 144 return i; 145 return -1; 146 } 147 148 static struct shmid_ds * 149 shm_find_segment_by_shmid(int shmid) 150 { 151 int segnum; 152 struct shmid_ds *shmseg; 153 154 segnum = IPCID_TO_IX(shmid); 155 if (segnum < 0 || segnum >= shminfo.shmmni) 156 return NULL; 157 shmseg = &shmsegs[segnum]; 158 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0) 159 return NULL; 160 if ((shmseg->shm_perm.mode & (SHMSEG_REMOVED|SHMSEG_RMLINGER)) == SHMSEG_REMOVED) 161 return NULL; 162 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid)) 163 return NULL; 164 return shmseg; 165 } 166 167 static void 168 shm_deallocate_segment(struct shmid_ds *shmseg) 169 { 170 struct uvm_object *uobj = shmseg->_shm_internal; 171 size_t size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 172 173 #ifdef SHMDEBUG 174 printf("shm freeing key 0x%lx seq 0x%x\n", 175 shmseg->shm_perm._key, shmseg->shm_perm._seq); 176 #endif 177 178 (*uobj->pgops->pgo_detach)(uobj); 179 shmseg->_shm_internal = NULL; 180 shm_committed -= btoc(size); 181 shmseg->shm_perm.mode = SHMSEG_FREE; 182 shm_nused--; 183 } 184 185 static void 186 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s, 187 struct shmmap_entry *shmmap_se) 188 { 189 struct shmid_ds *shmseg; 190 int segnum; 191 size_t size; 192 193 segnum = IPCID_TO_IX(shmmap_se->shmid); 194 #ifdef DEBUG 195 if (segnum < 0 || segnum >= shminfo.shmmni) 196 panic("shm_delete_mapping: vmspace %p state %p entry %p - " 197 "entry segment ID bad (%d)", 198 vm, shmmap_s, shmmap_se, segnum); 199 #endif 200 shmseg = &shmsegs[segnum]; 201 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 202 uvm_deallocate(&vm->vm_map, shmmap_se->va, size); 203 SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next); 204 shmmap_s->nitems--; 205 pool_put(&shmmap_entry_pool, shmmap_se); 206 shmseg->shm_dtime = time.tv_sec; 207 if ((--shmseg->shm_nattch <= 0) && 208 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) { 209 shm_deallocate_segment(shmseg); 210 shm_last_free = segnum; 211 } 212 } 213 214 /* 215 * Get a non-shared shm map for that vmspace. 216 * 3 cases: 217 * - no shm map present: create a fresh one 218 * - a shm map with refcount=1, just used by ourselves: fine 219 * - a shared shm map: copy to a fresh one and adjust refcounts 220 */ 221 static struct shmmap_state * 222 shmmap_getprivate(struct proc *p) 223 { 224 struct shmmap_state *oshmmap_s, *shmmap_s; 225 struct shmmap_entry *oshmmap_se, *shmmap_se; 226 227 oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 228 if (oshmmap_s && oshmmap_s->nrefs == 1) 229 return (oshmmap_s); 230 231 shmmap_s = malloc(sizeof(struct shmmap_state), M_SHM, M_WAITOK); 232 memset(shmmap_s, 0, sizeof(struct shmmap_state)); 233 shmmap_s->nrefs = 1; 234 SLIST_INIT(&shmmap_s->entries); 235 p->p_vmspace->vm_shm = (caddr_t)shmmap_s; 236 237 if (!oshmmap_s) 238 return (shmmap_s); 239 240 #ifdef SHMDEBUG 241 printf("shmmap_getprivate: vm %p split (%d entries), was used by %d\n", 242 p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs); 243 #endif 244 SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) { 245 shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK); 246 shmmap_se->va = oshmmap_se->va; 247 shmmap_se->shmid = oshmmap_se->shmid; 248 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next); 249 } 250 shmmap_s->nitems = oshmmap_s->nitems; 251 oshmmap_s->nrefs--; 252 return (shmmap_s); 253 } 254 255 static struct shmmap_entry * 256 shm_find_mapping(struct shmmap_state *map, vaddr_t va) 257 { 258 struct shmmap_entry *shmmap_se; 259 260 SLIST_FOREACH(shmmap_se, &map->entries, next) { 261 if (shmmap_se->va == va) 262 return shmmap_se; 263 } 264 return 0; 265 } 266 267 int 268 sys_shmdt(struct lwp *l, void *v, register_t *retval) 269 { 270 struct sys_shmdt_args /* { 271 syscallarg(const void *) shmaddr; 272 } */ *uap = v; 273 struct proc *p = l->l_proc; 274 struct shmmap_state *shmmap_s, *shmmap_s1; 275 struct shmmap_entry *shmmap_se; 276 277 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 278 if (shmmap_s == NULL) 279 return EINVAL; 280 281 shmmap_se = shm_find_mapping(shmmap_s, (vaddr_t)SCARG(uap, shmaddr)); 282 if (!shmmap_se) 283 return EINVAL; 284 285 shmmap_s1 = shmmap_getprivate(p); 286 if (shmmap_s1 != shmmap_s) { 287 /* map has been copied, lookup entry in new map */ 288 shmmap_se = shm_find_mapping(shmmap_s1, 289 (vaddr_t)SCARG(uap, shmaddr)); 290 KASSERT(shmmap_se != NULL); 291 } 292 #ifdef SHMDEBUG 293 printf("shmdt: vm %p: remove %d @%lx\n", 294 p->p_vmspace, shmmap_se->shmid, shmmap_se->va); 295 #endif 296 shm_delete_mapping(p->p_vmspace, shmmap_s1, shmmap_se); 297 return 0; 298 } 299 300 int 301 sys_shmat(struct lwp *l, void *v, register_t *retval) 302 { 303 struct sys_shmat_args /* { 304 syscallarg(int) shmid; 305 syscallarg(const void *) shmaddr; 306 syscallarg(int) shmflg; 307 } */ *uap = v; 308 int error, flags; 309 struct proc *p = l->l_proc; 310 struct ucred *cred = p->p_ucred; 311 struct shmid_ds *shmseg; 312 struct shmmap_state *shmmap_s; 313 struct uvm_object *uobj; 314 vaddr_t attach_va; 315 vm_prot_t prot; 316 vsize_t size; 317 struct shmmap_entry *shmmap_se; 318 319 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid)); 320 if (shmseg == NULL) 321 return EINVAL; 322 error = ipcperm(cred, &shmseg->shm_perm, 323 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W); 324 if (error) 325 return error; 326 327 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 328 if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg) 329 return EMFILE; 330 331 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 332 prot = VM_PROT_READ; 333 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0) 334 prot |= VM_PROT_WRITE; 335 flags = MAP_ANON | MAP_SHARED; 336 if (SCARG(uap, shmaddr)) { 337 flags |= MAP_FIXED; 338 if (SCARG(uap, shmflg) & SHM_RND) 339 attach_va = 340 (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1); 341 else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0) 342 attach_va = (vaddr_t)SCARG(uap, shmaddr); 343 else 344 return EINVAL; 345 } else { 346 /* This is just a hint to uvm_mmap() about where to put it. */ 347 attach_va = p->p_emul->e_vm_default_addr(p, 348 (vaddr_t)p->p_vmspace->vm_daddr, size); 349 } 350 uobj = shmseg->_shm_internal; 351 (*uobj->pgops->pgo_reference)(uobj); 352 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size, 353 uobj, 0, 0, 354 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0)); 355 if (error) { 356 (*uobj->pgops->pgo_detach)(uobj); 357 return error; 358 } 359 shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK); 360 shmmap_se->va = attach_va; 361 shmmap_se->shmid = SCARG(uap, shmid); 362 shmmap_s = shmmap_getprivate(p); 363 #ifdef SHMDEBUG 364 printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, shmmap_se->shmid, attach_va); 365 #endif 366 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next); 367 shmmap_s->nitems++; 368 shmseg->shm_lpid = p->p_pid; 369 shmseg->shm_atime = time.tv_sec; 370 shmseg->shm_nattch++; 371 372 retval[0] = attach_va; 373 return 0; 374 } 375 376 int 377 sys___shmctl13(struct lwp *l, void *v, register_t *retval) 378 { 379 struct sys___shmctl13_args /* { 380 syscallarg(int) shmid; 381 syscallarg(int) cmd; 382 syscallarg(struct shmid_ds *) buf; 383 } */ *uap = v; 384 struct proc *p = l->l_proc; 385 struct shmid_ds shmbuf; 386 int cmd, error; 387 388 cmd = SCARG(uap, cmd); 389 390 if (cmd == IPC_SET) { 391 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf)); 392 if (error) 393 return (error); 394 } 395 396 error = shmctl1(p, SCARG(uap, shmid), cmd, 397 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL); 398 399 if (error == 0 && cmd == IPC_STAT) 400 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf)); 401 402 return (error); 403 } 404 405 int 406 shmctl1(struct proc *p, int shmid, int cmd, struct shmid_ds *shmbuf) 407 { 408 struct ucred *cred = p->p_ucred; 409 struct shmid_ds *shmseg; 410 int error = 0; 411 412 shmseg = shm_find_segment_by_shmid(shmid); 413 if (shmseg == NULL) 414 return EINVAL; 415 switch (cmd) { 416 case IPC_STAT: 417 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0) 418 return error; 419 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds)); 420 break; 421 case IPC_SET: 422 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0) 423 return error; 424 shmseg->shm_perm.uid = shmbuf->shm_perm.uid; 425 shmseg->shm_perm.gid = shmbuf->shm_perm.gid; 426 shmseg->shm_perm.mode = 427 (shmseg->shm_perm.mode & ~ACCESSPERMS) | 428 (shmbuf->shm_perm.mode & ACCESSPERMS); 429 shmseg->shm_ctime = time.tv_sec; 430 break; 431 case IPC_RMID: 432 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0) 433 return error; 434 shmseg->shm_perm._key = IPC_PRIVATE; 435 shmseg->shm_perm.mode |= SHMSEG_REMOVED; 436 if (shmseg->shm_nattch <= 0) { 437 shm_deallocate_segment(shmseg); 438 shm_last_free = IPCID_TO_IX(shmid); 439 } 440 break; 441 case SHM_LOCK: 442 case SHM_UNLOCK: 443 default: 444 return EINVAL; 445 } 446 return 0; 447 } 448 449 static int 450 shmget_existing(struct proc *p, struct sys_shmget_args *uap, int mode, 451 int segnum, register_t *retval) 452 { 453 struct shmid_ds *shmseg; 454 struct ucred *cred = p->p_ucred; 455 int error; 456 457 shmseg = &shmsegs[segnum]; 458 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) { 459 /* 460 * This segment is in the process of being allocated. Wait 461 * until it's done, and look the key up again (in case the 462 * allocation failed or it was freed). 463 */ 464 shmseg->shm_perm.mode |= SHMSEG_WANTED; 465 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0); 466 if (error) 467 return error; 468 return EAGAIN; 469 } 470 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0) 471 return error; 472 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz) 473 return EINVAL; 474 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) == 475 (IPC_CREAT | IPC_EXCL)) 476 return EEXIST; 477 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 478 return 0; 479 } 480 481 static int 482 shmget_allocate_segment(struct proc *p, struct sys_shmget_args *uap, int mode, 483 register_t *retval) 484 { 485 int i, segnum, shmid, size; 486 struct ucred *cred = p->p_ucred; 487 struct shmid_ds *shmseg; 488 int error = 0; 489 490 if (SCARG(uap, size) < shminfo.shmmin || 491 SCARG(uap, size) > shminfo.shmmax) 492 return EINVAL; 493 if (shm_nused >= shminfo.shmmni) /* any shmids left? */ 494 return ENOSPC; 495 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET; 496 if (shm_committed + btoc(size) > shminfo.shmall) 497 return ENOMEM; 498 if (shm_last_free < 0) { 499 for (i = 0; i < shminfo.shmmni; i++) 500 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE) 501 break; 502 if (i == shminfo.shmmni) 503 panic("shmseg free count inconsistent"); 504 segnum = i; 505 } else { 506 segnum = shm_last_free; 507 shm_last_free = -1; 508 } 509 shmseg = &shmsegs[segnum]; 510 /* 511 * In case we sleep in malloc(), mark the segment present but deleted 512 * so that noone else tries to create the same key. 513 */ 514 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; 515 shmseg->shm_perm._key = SCARG(uap, key); 516 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff; 517 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 518 519 shmseg->_shm_internal = uao_create(size, 0); 520 521 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid; 522 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid; 523 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) | 524 (mode & (ACCESSPERMS|SHMSEG_RMLINGER)) | SHMSEG_ALLOCATED; 525 shmseg->shm_segsz = SCARG(uap, size); 526 shmseg->shm_cpid = p->p_pid; 527 shmseg->shm_lpid = shmseg->shm_nattch = 0; 528 shmseg->shm_atime = shmseg->shm_dtime = 0; 529 shmseg->shm_ctime = time.tv_sec; 530 shm_committed += btoc(size); 531 shm_nused++; 532 533 *retval = shmid; 534 if (shmseg->shm_perm.mode & SHMSEG_WANTED) { 535 /* 536 * Somebody else wanted this key while we were asleep. Wake 537 * them up now. 538 */ 539 shmseg->shm_perm.mode &= ~SHMSEG_WANTED; 540 wakeup((caddr_t)shmseg); 541 } 542 return error; 543 } 544 545 int 546 sys_shmget(struct lwp *l, void *v, register_t *retval) 547 { 548 struct sys_shmget_args /* { 549 syscallarg(key_t) key; 550 syscallarg(int) size; 551 syscallarg(int) shmflg; 552 } */ *uap = v; 553 struct proc *p = l->l_proc; 554 int segnum, mode, error; 555 556 mode = SCARG(uap, shmflg) & ACCESSPERMS; 557 if (SCARG(uap, shmflg) & _SHM_RMLINGER) 558 mode |= SHMSEG_RMLINGER; 559 560 #ifdef SHMDEBUG 561 printf("shmget: key 0x%lx size 0x%x shmflg 0x%x mode 0x%x\n", 562 SCARG(uap, key), SCARG(uap, size), SCARG(uap, shmflg), mode); 563 #endif 564 565 if (SCARG(uap, key) != IPC_PRIVATE) { 566 again: 567 segnum = shm_find_segment_by_key(SCARG(uap, key)); 568 if (segnum >= 0) { 569 error = shmget_existing(p, uap, mode, segnum, retval); 570 if (error == EAGAIN) 571 goto again; 572 return error; 573 } 574 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0) 575 return ENOENT; 576 } 577 return shmget_allocate_segment(p, uap, mode, retval); 578 } 579 580 void 581 shmfork(struct vmspace *vm1, struct vmspace *vm2) 582 { 583 struct shmmap_state *shmmap_s; 584 struct shmmap_entry *shmmap_se; 585 586 vm2->vm_shm = vm1->vm_shm; 587 588 if (vm1->vm_shm == NULL) 589 return; 590 591 #ifdef SHMDEBUG 592 printf("shmfork %p->%p\n", vm1, vm2); 593 #endif 594 595 shmmap_s = (struct shmmap_state *)vm1->vm_shm; 596 597 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) 598 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++; 599 shmmap_s->nrefs++; 600 } 601 602 void 603 shmexit(struct vmspace *vm) 604 { 605 struct shmmap_state *shmmap_s; 606 struct shmmap_entry *shmmap_se; 607 608 shmmap_s = (struct shmmap_state *)vm->vm_shm; 609 if (shmmap_s == NULL) 610 return; 611 612 vm->vm_shm = NULL; 613 614 if (--shmmap_s->nrefs > 0) { 615 #ifdef SHMDEBUG 616 printf("shmexit: vm %p drop ref (%d entries), now used by %d\n", 617 vm, shmmap_s->nitems, shmmap_s->nrefs); 618 #endif 619 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) 620 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--; 621 return; 622 } 623 624 #ifdef SHMDEBUG 625 printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems); 626 #endif 627 while (!SLIST_EMPTY(&shmmap_s->entries)) { 628 shmmap_se = SLIST_FIRST(&shmmap_s->entries); 629 shm_delete_mapping(vm, shmmap_s, shmmap_se); 630 } 631 KASSERT(shmmap_s->nitems == 0); 632 free(shmmap_s, M_SHM); 633 } 634 635 void 636 shminit(void) 637 { 638 int i, sz; 639 vaddr_t v; 640 641 /* Allocate pageable memory for our structures */ 642 sz = shminfo.shmmni * sizeof(struct shmid_ds); 643 v = uvm_km_alloc(kernel_map, round_page(sz), 0, UVM_KMF_WIRED); 644 if (v == 0) 645 panic("sysv_shm: cannot allocate memory"); 646 shmsegs = (void *)v; 647 648 shminfo.shmmax *= PAGE_SIZE; 649 650 for (i = 0; i < shminfo.shmmni; i++) { 651 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 652 shmsegs[i].shm_perm._seq = 0; 653 } 654 shm_last_free = 0; 655 shm_nused = 0; 656 shm_committed = 0; 657 } 658