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