1 /* $NetBSD: sysv_shm.c,v 1.63 2001/11/15 09:48:20 lukem 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.63 2001/11/15 09:48:20 lukem 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/syscallargs.h> 84 85 #include <uvm/uvm_extern.h> 86 87 struct shmid_ds *shm_find_segment_by_shmid __P((int)); 88 89 /* 90 * Provides the following externally accessible functions: 91 * 92 * shminit(void); initialization 93 * shmexit(struct vmspace *) cleanup 94 * shmfork(struct vmspace *, struct vmspace *) fork handling 95 * 96 * Structures: 97 * shmsegs (an array of 'struct shmid_ds') 98 * per proc array of 'struct shmmap_state' 99 */ 100 101 #define SHMSEG_FREE 0x0200 102 #define SHMSEG_REMOVED 0x0400 103 #define SHMSEG_ALLOCATED 0x0800 104 #define SHMSEG_WANTED 0x1000 105 106 int shm_last_free, shm_nused, shm_committed; 107 struct shmid_ds *shmsegs; 108 109 struct shm_handle { 110 struct uvm_object *shm_object; 111 }; 112 113 struct shmmap_state { 114 vaddr_t va; 115 int shmid; 116 }; 117 118 static int shm_find_segment_by_key __P((key_t)); 119 static void shm_deallocate_segment __P((struct shmid_ds *)); 120 static void shm_delete_mapping __P((struct vmspace *, struct shmmap_state *)); 121 static int shmget_existing __P((struct proc *, struct sys_shmget_args *, 122 int, int, register_t *)); 123 static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *, 124 int, register_t *)); 125 126 static int 127 shm_find_segment_by_key(key) 128 key_t key; 129 { 130 int i; 131 132 for (i = 0; i < shminfo.shmmni; i++) 133 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) && 134 shmsegs[i].shm_perm._key == key) 135 return i; 136 return -1; 137 } 138 139 struct shmid_ds * 140 shm_find_segment_by_shmid(shmid) 141 int shmid; 142 { 143 int segnum; 144 struct shmid_ds *shmseg; 145 146 segnum = IPCID_TO_IX(shmid); 147 if (segnum < 0 || segnum >= shminfo.shmmni) 148 return NULL; 149 shmseg = &shmsegs[segnum]; 150 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED)) 151 != SHMSEG_ALLOCATED || 152 shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid)) 153 return NULL; 154 return shmseg; 155 } 156 157 static void 158 shm_deallocate_segment(shmseg) 159 struct shmid_ds *shmseg; 160 { 161 struct shm_handle *shm_handle; 162 size_t size; 163 164 shm_handle = shmseg->_shm_internal; 165 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 166 uao_detach(shm_handle->shm_object); 167 free((caddr_t)shm_handle, M_SHM); 168 shmseg->_shm_internal = NULL; 169 shm_committed -= btoc(size); 170 shmseg->shm_perm.mode = SHMSEG_FREE; 171 shm_nused--; 172 } 173 174 static void 175 shm_delete_mapping(vm, shmmap_s) 176 struct vmspace *vm; 177 struct shmmap_state *shmmap_s; 178 { 179 struct shmid_ds *shmseg; 180 int segnum; 181 size_t size; 182 183 segnum = IPCID_TO_IX(shmmap_s->shmid); 184 shmseg = &shmsegs[segnum]; 185 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 186 uvm_deallocate(&vm->vm_map, shmmap_s->va, size); 187 shmmap_s->shmid = -1; 188 shmseg->shm_dtime = time.tv_sec; 189 if ((--shmseg->shm_nattch <= 0) && 190 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) { 191 shm_deallocate_segment(shmseg); 192 shm_last_free = segnum; 193 } 194 } 195 196 int 197 sys_shmdt(p, v, retval) 198 struct proc *p; 199 void *v; 200 register_t *retval; 201 { 202 struct sys_shmdt_args /* { 203 syscallarg(const void *) shmaddr; 204 } */ *uap = v; 205 struct shmmap_state *shmmap_s; 206 int i; 207 208 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 209 if (shmmap_s == NULL) 210 return EINVAL; 211 212 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 213 if (shmmap_s->shmid != -1 && 214 shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr)) 215 break; 216 if (i == shminfo.shmseg) 217 return EINVAL; 218 shm_delete_mapping(p->p_vmspace, shmmap_s); 219 return 0; 220 } 221 222 int 223 sys_shmat(p, v, retval) 224 struct proc *p; 225 void *v; 226 register_t *retval; 227 { 228 struct sys_shmat_args /* { 229 syscallarg(int) shmid; 230 syscallarg(const void *) shmaddr; 231 syscallarg(int) shmflg; 232 } */ *uap = v; 233 int error, i, flags; 234 struct ucred *cred = p->p_ucred; 235 struct shmid_ds *shmseg; 236 struct shmmap_state *shmmap_s = NULL; 237 struct shm_handle *shm_handle; 238 vaddr_t attach_va; 239 vm_prot_t prot; 240 vsize_t size; 241 242 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 243 if (shmmap_s == NULL) { 244 size = shminfo.shmseg * sizeof(struct shmmap_state); 245 shmmap_s = malloc(size, M_SHM, M_WAITOK); 246 for (i = 0; i < shminfo.shmseg; i++) 247 shmmap_s[i].shmid = -1; 248 p->p_vmspace->vm_shm = (caddr_t)shmmap_s; 249 } 250 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid)); 251 if (shmseg == NULL) 252 return EINVAL; 253 error = ipcperm(cred, &shmseg->shm_perm, 254 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W); 255 if (error) 256 return error; 257 for (i = 0; i < shminfo.shmseg; i++) { 258 if (shmmap_s->shmid == -1) 259 break; 260 shmmap_s++; 261 } 262 if (i >= shminfo.shmseg) 263 return EMFILE; 264 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET; 265 prot = VM_PROT_READ; 266 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0) 267 prot |= VM_PROT_WRITE; 268 flags = MAP_ANON | MAP_SHARED; 269 if (SCARG(uap, shmaddr)) { 270 flags |= MAP_FIXED; 271 if (SCARG(uap, shmflg) & SHM_RND) 272 attach_va = 273 (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1); 274 else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0) 275 attach_va = (vaddr_t)SCARG(uap, shmaddr); 276 else 277 return EINVAL; 278 } else { 279 /* This is just a hint to vm_mmap() about where to put it. */ 280 attach_va = 281 round_page((vaddr_t)p->p_vmspace->vm_taddr + 282 MAXTSIZ + MAXDSIZ); 283 } 284 shm_handle = shmseg->_shm_internal; 285 uao_reference(shm_handle->shm_object); 286 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size, 287 shm_handle->shm_object, 0, 0, 288 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0)); 289 if (error) { 290 return error; 291 } 292 shmmap_s->va = attach_va; 293 shmmap_s->shmid = SCARG(uap, shmid); 294 shmseg->shm_lpid = p->p_pid; 295 shmseg->shm_atime = time.tv_sec; 296 shmseg->shm_nattch++; 297 *retval = attach_va; 298 return 0; 299 } 300 301 int 302 sys___shmctl13(p, v, retval) 303 struct proc *p; 304 void *v; 305 register_t *retval; 306 { 307 struct sys___shmctl13_args /* { 308 syscallarg(int) shmid; 309 syscallarg(int) cmd; 310 syscallarg(struct shmid_ds *) buf; 311 } */ *uap = v; 312 struct shmid_ds shmbuf; 313 int cmd, error; 314 315 cmd = SCARG(uap, cmd); 316 317 if (cmd == IPC_SET) { 318 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf)); 319 if (error) 320 return (error); 321 } 322 323 error = shmctl1(p, SCARG(uap, shmid), cmd, 324 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL); 325 326 if (error == 0 && cmd == IPC_STAT) 327 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf)); 328 329 return (error); 330 } 331 332 int 333 shmctl1(p, shmid, cmd, shmbuf) 334 struct proc *p; 335 int shmid; 336 int cmd; 337 struct shmid_ds *shmbuf; 338 { 339 struct ucred *cred = p->p_ucred; 340 struct shmid_ds *shmseg; 341 int error = 0; 342 343 shmseg = shm_find_segment_by_shmid(shmid); 344 if (shmseg == NULL) 345 return EINVAL; 346 switch (cmd) { 347 case IPC_STAT: 348 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0) 349 return error; 350 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds)); 351 break; 352 case IPC_SET: 353 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0) 354 return error; 355 shmseg->shm_perm.uid = shmbuf->shm_perm.uid; 356 shmseg->shm_perm.gid = shmbuf->shm_perm.gid; 357 shmseg->shm_perm.mode = 358 (shmseg->shm_perm.mode & ~ACCESSPERMS) | 359 (shmbuf->shm_perm.mode & ACCESSPERMS); 360 shmseg->shm_ctime = time.tv_sec; 361 break; 362 case IPC_RMID: 363 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0) 364 return error; 365 shmseg->shm_perm._key = IPC_PRIVATE; 366 shmseg->shm_perm.mode |= SHMSEG_REMOVED; 367 if (shmseg->shm_nattch <= 0) { 368 shm_deallocate_segment(shmseg); 369 shm_last_free = IPCID_TO_IX(shmid); 370 } 371 break; 372 case SHM_LOCK: 373 case SHM_UNLOCK: 374 default: 375 return EINVAL; 376 } 377 return 0; 378 } 379 380 static int 381 shmget_existing(p, uap, mode, segnum, retval) 382 struct proc *p; 383 struct sys_shmget_args /* { 384 syscallarg(key_t) key; 385 syscallarg(size_t) size; 386 syscallarg(int) shmflg; 387 } */ *uap; 388 int mode; 389 int segnum; 390 register_t *retval; 391 { 392 struct shmid_ds *shmseg; 393 struct ucred *cred = p->p_ucred; 394 int error; 395 396 shmseg = &shmsegs[segnum]; 397 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) { 398 /* 399 * This segment is in the process of being allocated. Wait 400 * until it's done, and look the key up again (in case the 401 * allocation failed or it was freed). 402 */ 403 shmseg->shm_perm.mode |= SHMSEG_WANTED; 404 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0); 405 if (error) 406 return error; 407 return EAGAIN; 408 } 409 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0) 410 return error; 411 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz) 412 return EINVAL; 413 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) == 414 (IPC_CREAT | IPC_EXCL)) 415 return EEXIST; 416 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 417 return 0; 418 } 419 420 static int 421 shmget_allocate_segment(p, uap, mode, retval) 422 struct proc *p; 423 struct sys_shmget_args /* { 424 syscallarg(key_t) key; 425 syscallarg(size_t) size; 426 syscallarg(int) shmflg; 427 } */ *uap; 428 int mode; 429 register_t *retval; 430 { 431 int i, segnum, shmid, size; 432 struct ucred *cred = p->p_ucred; 433 struct shmid_ds *shmseg; 434 struct shm_handle *shm_handle; 435 int error = 0; 436 437 if (SCARG(uap, size) < shminfo.shmmin || 438 SCARG(uap, size) > shminfo.shmmax) 439 return EINVAL; 440 if (shm_nused >= shminfo.shmmni) /* any shmids left? */ 441 return ENOSPC; 442 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET; 443 if (shm_committed + btoc(size) > shminfo.shmall) 444 return ENOMEM; 445 if (shm_last_free < 0) { 446 for (i = 0; i < shminfo.shmmni; i++) 447 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE) 448 break; 449 if (i == shminfo.shmmni) 450 panic("shmseg free count inconsistent"); 451 segnum = i; 452 } else { 453 segnum = shm_last_free; 454 shm_last_free = -1; 455 } 456 shmseg = &shmsegs[segnum]; 457 /* 458 * In case we sleep in malloc(), mark the segment present but deleted 459 * so that noone else tries to create the same key. 460 */ 461 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; 462 shmseg->shm_perm._key = SCARG(uap, key); 463 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff; 464 shm_handle = (struct shm_handle *) 465 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK); 466 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 467 468 shm_handle->shm_object = uao_create(size, 0); 469 470 shmseg->_shm_internal = shm_handle; 471 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid; 472 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid; 473 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) | 474 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED; 475 shmseg->shm_segsz = SCARG(uap, size); 476 shmseg->shm_cpid = p->p_pid; 477 shmseg->shm_lpid = shmseg->shm_nattch = 0; 478 shmseg->shm_atime = shmseg->shm_dtime = 0; 479 shmseg->shm_ctime = time.tv_sec; 480 shm_committed += btoc(size); 481 shm_nused++; 482 483 *retval = shmid; 484 if (shmseg->shm_perm.mode & SHMSEG_WANTED) { 485 /* 486 * Somebody else wanted this key while we were asleep. Wake 487 * them up now. 488 */ 489 shmseg->shm_perm.mode &= ~SHMSEG_WANTED; 490 wakeup((caddr_t)shmseg); 491 } 492 return error; 493 } 494 495 int 496 sys_shmget(p, v, retval) 497 struct proc *p; 498 void *v; 499 register_t *retval; 500 { 501 struct sys_shmget_args /* { 502 syscallarg(key_t) key; 503 syscallarg(int) size; 504 syscallarg(int) shmflg; 505 } */ *uap = v; 506 int segnum, mode, error; 507 508 mode = SCARG(uap, shmflg) & ACCESSPERMS; 509 if (SCARG(uap, key) != IPC_PRIVATE) { 510 again: 511 segnum = shm_find_segment_by_key(SCARG(uap, key)); 512 if (segnum >= 0) { 513 error = shmget_existing(p, uap, mode, segnum, retval); 514 if (error == EAGAIN) 515 goto again; 516 return error; 517 } 518 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0) 519 return ENOENT; 520 } 521 return shmget_allocate_segment(p, uap, mode, retval); 522 } 523 524 void 525 shmfork(vm1, vm2) 526 struct vmspace *vm1, *vm2; 527 { 528 struct shmmap_state *shmmap_s; 529 size_t size; 530 int i; 531 532 if (vm1->vm_shm == NULL) { 533 vm2->vm_shm = NULL; 534 return; 535 } 536 537 size = shminfo.shmseg * sizeof(struct shmmap_state); 538 shmmap_s = malloc(size, M_SHM, M_WAITOK); 539 memcpy(shmmap_s, vm1->vm_shm, size); 540 vm2->vm_shm = (caddr_t)shmmap_s; 541 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 542 if (shmmap_s->shmid != -1) 543 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++; 544 } 545 546 void 547 shmexit(vm) 548 struct vmspace *vm; 549 { 550 struct shmmap_state *shmmap_s; 551 int i; 552 553 shmmap_s = (struct shmmap_state *)vm->vm_shm; 554 if (shmmap_s == NULL) 555 return; 556 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) 557 if (shmmap_s->shmid != -1) 558 shm_delete_mapping(vm, shmmap_s); 559 free(vm->vm_shm, M_SHM); 560 vm->vm_shm = NULL; 561 } 562 563 void 564 shminit() 565 { 566 int i; 567 568 shminfo.shmmax *= PAGE_SIZE; 569 570 for (i = 0; i < shminfo.shmmni; i++) { 571 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 572 shmsegs[i].shm_perm._seq = 0; 573 } 574 shm_last_free = 0; 575 shm_nused = 0; 576 shm_committed = 0; 577 } 578