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