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