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