Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.60.2.5
      1  1.60.2.5   nathanw /*	$NetBSD: sysv_shm.c,v 1.60.2.5 2002/04/17 00:06:19 nathanw Exp $	*/
      2      1.52   thorpej 
      3      1.52   thorpej /*-
      4      1.52   thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5      1.52   thorpej  * All rights reserved.
      6      1.52   thorpej  *
      7      1.52   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.52   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.52   thorpej  * NASA Ames Research Center.
     10      1.52   thorpej  *
     11      1.52   thorpej  * Redistribution and use in source and binary forms, with or without
     12      1.52   thorpej  * modification, are permitted provided that the following conditions
     13      1.52   thorpej  * are met:
     14      1.52   thorpej  * 1. Redistributions of source code must retain the above copyright
     15      1.52   thorpej  *    notice, this list of conditions and the following disclaimer.
     16      1.52   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.52   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18      1.52   thorpej  *    documentation and/or other materials provided with the distribution.
     19      1.52   thorpej  * 3. All advertising materials mentioning features or use of this software
     20      1.52   thorpej  *    must display the following acknowledgement:
     21      1.52   thorpej  *	This product includes software developed by the NetBSD
     22      1.52   thorpej  *	Foundation, Inc. and its contributors.
     23      1.52   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.52   thorpej  *    contributors may be used to endorse or promote products derived
     25      1.52   thorpej  *    from this software without specific prior written permission.
     26      1.52   thorpej  *
     27      1.52   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.52   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.52   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.52   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.52   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.52   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.52   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.52   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.52   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.52   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.52   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38      1.52   thorpej  */
     39      1.22       cgd 
     40      1.11   hpeyerl /*
     41      1.48   mycroft  * Copyright (c) 1994 Adam Glass and Charles M. Hannum.  All rights reserved.
     42      1.11   hpeyerl  *
     43      1.11   hpeyerl  * Redistribution and use in source and binary forms, with or without
     44      1.11   hpeyerl  * modification, are permitted provided that the following conditions
     45      1.11   hpeyerl  * are met:
     46      1.11   hpeyerl  * 1. Redistributions of source code must retain the above copyright
     47      1.11   hpeyerl  *    notice, this list of conditions and the following disclaimer.
     48      1.17   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     49      1.17   mycroft  *    notice, this list of conditions and the following disclaimer in the
     50      1.17   mycroft  *    documentation and/or other materials provided with the distribution.
     51      1.17   mycroft  * 3. All advertising materials mentioning features or use of this software
     52      1.17   mycroft  *    must display the following acknowledgement:
     53      1.48   mycroft  *	This product includes software developed by Adam Glass and Charles M.
     54      1.17   mycroft  *	Hannum.
     55      1.17   mycroft  * 4. The names of the authors may not be used to endorse or promote products
     56      1.11   hpeyerl  *    derived from this software without specific prior written permission.
     57      1.11   hpeyerl  *
     58      1.17   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     59      1.17   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60      1.17   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61      1.17   mycroft  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     62      1.17   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63      1.17   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64      1.17   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65      1.17   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66      1.17   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67      1.17   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68      1.11   hpeyerl  */
     69  1.60.2.3   nathanw 
     70  1.60.2.3   nathanw #include <sys/cdefs.h>
     71  1.60.2.5   nathanw __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.60.2.5 2002/04/17 00:06:19 nathanw Exp $");
     72      1.43       mrg 
     73      1.50      tron #define SYSVSHM
     74      1.11   hpeyerl 
     75      1.11   hpeyerl #include <sys/param.h>
     76      1.11   hpeyerl #include <sys/kernel.h>
     77      1.11   hpeyerl #include <sys/shm.h>
     78      1.11   hpeyerl #include <sys/malloc.h>
     79      1.11   hpeyerl #include <sys/mman.h>
     80      1.12   mycroft #include <sys/stat.h>
     81      1.56    simonb #include <sys/sysctl.h>
     82      1.56    simonb #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     83      1.56    simonb #include <sys/syscallargs.h>
     84      1.35  christos 
     85      1.60   thorpej #include <uvm/uvm_extern.h>
     86      1.60   thorpej 
     87  1.60.2.5   nathanw struct shmid_ds *shm_find_segment_by_shmid __P((int, int));
     88      1.35  christos 
     89      1.11   hpeyerl /*
     90      1.11   hpeyerl  * Provides the following externally accessible functions:
     91      1.11   hpeyerl  *
     92      1.41   thorpej  * shminit(void);		                 initialization
     93      1.41   thorpej  * shmexit(struct vmspace *)                     cleanup
     94      1.41   thorpej  * shmfork(struct vmspace *, struct vmspace *)   fork handling
     95      1.11   hpeyerl  *
     96      1.11   hpeyerl  * Structures:
     97      1.11   hpeyerl  * shmsegs (an array of 'struct shmid_ds')
     98      1.11   hpeyerl  * per proc array of 'struct shmmap_state'
     99      1.11   hpeyerl  */
    100      1.12   mycroft 
    101      1.16   mycroft #define	SHMSEG_FREE     	0x0200
    102      1.16   mycroft #define	SHMSEG_REMOVED  	0x0400
    103      1.16   mycroft #define	SHMSEG_ALLOCATED	0x0800
    104      1.16   mycroft #define	SHMSEG_WANTED		0x1000
    105      1.11   hpeyerl 
    106      1.55    simonb int	shm_last_free, shm_nused, shm_committed;
    107      1.55    simonb struct	shmid_ds *shmsegs;
    108      1.11   hpeyerl 
    109      1.11   hpeyerl struct shm_handle {
    110      1.42       mrg 	struct uvm_object *shm_object;
    111      1.11   hpeyerl };
    112      1.11   hpeyerl 
    113      1.11   hpeyerl struct shmmap_state {
    114      1.47       eeh 	vaddr_t va;
    115      1.11   hpeyerl 	int shmid;
    116      1.11   hpeyerl };
    117      1.11   hpeyerl 
    118      1.35  christos static int shm_find_segment_by_key __P((key_t));
    119      1.12   mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
    120  1.60.2.2   nathanw static void shm_delete_mapping __P((struct vmspace *, struct shmmap_state *));
    121      1.35  christos static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
    122      1.35  christos 				int, int, register_t *));
    123      1.35  christos static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
    124      1.35  christos 					int, register_t *));
    125      1.11   hpeyerl 
    126      1.12   mycroft static int
    127      1.12   mycroft shm_find_segment_by_key(key)
    128      1.11   hpeyerl 	key_t key;
    129      1.11   hpeyerl {
    130      1.11   hpeyerl 	int i;
    131      1.11   hpeyerl 
    132      1.12   mycroft 	for (i = 0; i < shminfo.shmmni; i++)
    133      1.12   mycroft 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
    134      1.52   thorpej 		    shmsegs[i].shm_perm._key == key)
    135      1.12   mycroft 			return i;
    136      1.11   hpeyerl 	return -1;
    137      1.11   hpeyerl }
    138      1.11   hpeyerl 
    139      1.28  christos struct shmid_ds *
    140  1.60.2.5   nathanw shm_find_segment_by_shmid(shmid, findremoved)
    141      1.11   hpeyerl 	int shmid;
    142  1.60.2.5   nathanw 	int findremoved;
    143      1.11   hpeyerl {
    144      1.11   hpeyerl 	int segnum;
    145      1.11   hpeyerl 	struct shmid_ds *shmseg;
    146      1.11   hpeyerl 
    147      1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    148      1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    149      1.11   hpeyerl 		return NULL;
    150      1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    151  1.60.2.5   nathanw 	if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
    152  1.60.2.5   nathanw 		return NULL;
    153  1.60.2.5   nathanw 	if (!findremoved && ((shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0))
    154  1.60.2.5   nathanw 		return NULL;
    155  1.60.2.5   nathanw 	if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
    156      1.11   hpeyerl 		return NULL;
    157      1.11   hpeyerl 	return shmseg;
    158      1.11   hpeyerl }
    159      1.11   hpeyerl 
    160      1.12   mycroft static void
    161      1.12   mycroft shm_deallocate_segment(shmseg)
    162      1.12   mycroft 	struct shmid_ds *shmseg;
    163      1.12   mycroft {
    164      1.12   mycroft 	struct shm_handle *shm_handle;
    165      1.14   mycroft 	size_t size;
    166      1.12   mycroft 
    167      1.52   thorpej 	shm_handle = shmseg->_shm_internal;
    168      1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    169      1.42       mrg 	uao_detach(shm_handle->shm_object);
    170      1.12   mycroft 	free((caddr_t)shm_handle, M_SHM);
    171      1.52   thorpej 	shmseg->_shm_internal = NULL;
    172      1.14   mycroft 	shm_committed -= btoc(size);
    173      1.12   mycroft 	shmseg->shm_perm.mode = SHMSEG_FREE;
    174      1.25   mycroft 	shm_nused--;
    175      1.12   mycroft }
    176      1.12   mycroft 
    177  1.60.2.2   nathanw static void
    178      1.41   thorpej shm_delete_mapping(vm, shmmap_s)
    179      1.41   thorpej 	struct vmspace *vm;
    180      1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    181      1.11   hpeyerl {
    182      1.12   mycroft 	struct shmid_ds *shmseg;
    183  1.60.2.2   nathanw 	int segnum;
    184      1.14   mycroft 	size_t size;
    185      1.11   hpeyerl 
    186      1.12   mycroft 	segnum = IPCID_TO_IX(shmmap_s->shmid);
    187      1.12   mycroft 	shmseg = &shmsegs[segnum];
    188      1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    189  1.60.2.2   nathanw 	uvm_deallocate(&vm->vm_map, shmmap_s->va, size);
    190      1.12   mycroft 	shmmap_s->shmid = -1;
    191      1.11   hpeyerl 	shmseg->shm_dtime = time.tv_sec;
    192      1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    193      1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    194      1.12   mycroft 		shm_deallocate_segment(shmseg);
    195      1.12   mycroft 		shm_last_free = segnum;
    196      1.11   hpeyerl 	}
    197      1.11   hpeyerl }
    198      1.11   hpeyerl 
    199      1.12   mycroft int
    200  1.60.2.1   nathanw sys_shmdt(l, v, retval)
    201  1.60.2.1   nathanw 	struct lwp *l;
    202      1.32   thorpej 	void *v;
    203      1.32   thorpej 	register_t *retval;
    204      1.32   thorpej {
    205      1.33   mycroft 	struct sys_shmdt_args /* {
    206      1.44    kleink 		syscallarg(const void *) shmaddr;
    207      1.32   thorpej 	} */ *uap = v;
    208  1.60.2.1   nathanw 	struct proc *p = l->l_proc;
    209      1.12   mycroft 	struct shmmap_state *shmmap_s;
    210      1.11   hpeyerl 	int i;
    211      1.11   hpeyerl 
    212      1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    213      1.38  christos 	if (shmmap_s == NULL)
    214      1.38  christos 		return EINVAL;
    215      1.38  christos 
    216      1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    217      1.12   mycroft 		if (shmmap_s->shmid != -1 &&
    218      1.47       eeh 		    shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr))
    219      1.12   mycroft 			break;
    220      1.11   hpeyerl 	if (i == shminfo.shmseg)
    221      1.11   hpeyerl 		return EINVAL;
    222  1.60.2.2   nathanw 	shm_delete_mapping(p->p_vmspace, shmmap_s);
    223  1.60.2.2   nathanw 	return 0;
    224      1.11   hpeyerl }
    225      1.11   hpeyerl 
    226      1.12   mycroft int
    227  1.60.2.1   nathanw sys_shmat(l, v, retval)
    228  1.60.2.1   nathanw 	struct lwp *l;
    229      1.32   thorpej 	void *v;
    230      1.32   thorpej 	register_t *retval;
    231      1.32   thorpej {
    232      1.33   mycroft 	struct sys_shmat_args /* {
    233      1.26       cgd 		syscallarg(int) shmid;
    234      1.44    kleink 		syscallarg(const void *) shmaddr;
    235      1.35  christos 		syscallarg(int) shmflg;
    236      1.32   thorpej 	} */ *uap = v;
    237  1.60.2.1   nathanw 	struct proc *p = l->l_proc;
    238  1.60.2.5   nathanw 	vaddr_t attach_va;
    239  1.60.2.5   nathanw 	int error;
    240  1.60.2.5   nathanw 
    241  1.60.2.5   nathanw 	error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr),
    242  1.60.2.5   nathanw 	    SCARG(uap, shmflg), &attach_va, 0);
    243  1.60.2.5   nathanw 	if (error != 0)
    244  1.60.2.5   nathanw 		return error;
    245  1.60.2.5   nathanw 	retval[0] = attach_va;
    246  1.60.2.5   nathanw 	return 0;
    247  1.60.2.5   nathanw }
    248  1.60.2.5   nathanw 
    249  1.60.2.5   nathanw int
    250  1.60.2.5   nathanw shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved)
    251  1.60.2.5   nathanw 	struct proc *p;
    252  1.60.2.5   nathanw 	int shmid;
    253  1.60.2.5   nathanw 	const void *shmaddr;
    254  1.60.2.5   nathanw 	int shmflg;
    255  1.60.2.5   nathanw 	vaddr_t *attachp;
    256  1.60.2.5   nathanw 	int findremoved;
    257  1.60.2.5   nathanw {
    258      1.12   mycroft 	int error, i, flags;
    259      1.12   mycroft 	struct ucred *cred = p->p_ucred;
    260      1.11   hpeyerl 	struct shmid_ds *shmseg;
    261      1.11   hpeyerl 	struct shmmap_state *shmmap_s = NULL;
    262      1.39  drochner 	struct shm_handle *shm_handle;
    263      1.47       eeh 	vaddr_t attach_va;
    264      1.11   hpeyerl 	vm_prot_t prot;
    265      1.47       eeh 	vsize_t size;
    266      1.11   hpeyerl 
    267      1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    268      1.12   mycroft 	if (shmmap_s == NULL) {
    269      1.12   mycroft 		size = shminfo.shmseg * sizeof(struct shmmap_state);
    270      1.12   mycroft 		shmmap_s = malloc(size, M_SHM, M_WAITOK);
    271      1.18       cgd 		for (i = 0; i < shminfo.shmseg; i++)
    272      1.18       cgd 			shmmap_s[i].shmid = -1;
    273      1.12   mycroft 		p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    274      1.12   mycroft 	}
    275  1.60.2.5   nathanw 	shmseg = shm_find_segment_by_shmid(shmid, findremoved);
    276      1.11   hpeyerl 	if (shmseg == NULL)
    277      1.11   hpeyerl 		return EINVAL;
    278      1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    279  1.60.2.5   nathanw 		    (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    280      1.35  christos 	if (error)
    281      1.12   mycroft 		return error;
    282      1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++) {
    283      1.12   mycroft 		if (shmmap_s->shmid == -1)
    284      1.12   mycroft 			break;
    285      1.12   mycroft 		shmmap_s++;
    286      1.11   hpeyerl 	}
    287      1.12   mycroft 	if (i >= shminfo.shmseg)
    288      1.12   mycroft 		return EMFILE;
    289      1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    290      1.12   mycroft 	prot = VM_PROT_READ;
    291  1.60.2.5   nathanw 	if ((shmflg & SHM_RDONLY) == 0)
    292      1.12   mycroft 		prot |= VM_PROT_WRITE;
    293      1.12   mycroft 	flags = MAP_ANON | MAP_SHARED;
    294  1.60.2.5   nathanw 	if (shmaddr) {
    295      1.12   mycroft 		flags |= MAP_FIXED;
    296  1.60.2.5   nathanw 		if (shmflg & SHM_RND)
    297      1.26       cgd 			attach_va =
    298  1.60.2.5   nathanw 			    (vaddr_t)shmaddr & ~(SHMLBA-1);
    299  1.60.2.5   nathanw 		else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0)
    300  1.60.2.5   nathanw 			attach_va = (vaddr_t)shmaddr;
    301      1.11   hpeyerl 		else
    302      1.14   mycroft 			return EINVAL;
    303      1.12   mycroft 	} else {
    304      1.20   mycroft 		/* This is just a hint to vm_mmap() about where to put it. */
    305      1.31       cgd 		attach_va =
    306      1.54    kleink 		    round_page((vaddr_t)p->p_vmspace->vm_taddr +
    307      1.54    kleink 			MAXTSIZ + MAXDSIZ);
    308      1.11   hpeyerl 	}
    309      1.52   thorpej 	shm_handle = shmseg->_shm_internal;
    310      1.42       mrg 	uao_reference(shm_handle->shm_object);
    311  1.60.2.2   nathanw 	error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
    312  1.60.2.2   nathanw 	    shm_handle->shm_object, 0, 0,
    313  1.60.2.2   nathanw 	    UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
    314  1.60.2.2   nathanw 	if (error) {
    315  1.60.2.2   nathanw 		return error;
    316      1.42       mrg 	}
    317      1.12   mycroft 	shmmap_s->va = attach_va;
    318  1.60.2.5   nathanw 	shmmap_s->shmid = shmid;
    319      1.11   hpeyerl 	shmseg->shm_lpid = p->p_pid;
    320      1.11   hpeyerl 	shmseg->shm_atime = time.tv_sec;
    321      1.11   hpeyerl 	shmseg->shm_nattch++;
    322  1.60.2.5   nathanw 	*attachp = attach_va;
    323      1.11   hpeyerl 	return 0;
    324      1.11   hpeyerl }
    325      1.11   hpeyerl 
    326      1.12   mycroft int
    327  1.60.2.1   nathanw sys___shmctl13(l, v, retval)
    328  1.60.2.1   nathanw 	struct lwp *l;
    329      1.32   thorpej 	void *v;
    330      1.32   thorpej 	register_t *retval;
    331      1.32   thorpej {
    332      1.52   thorpej 	struct sys___shmctl13_args /* {
    333      1.26       cgd 		syscallarg(int) shmid;
    334      1.26       cgd 		syscallarg(int) cmd;
    335      1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    336      1.52   thorpej 	} */ *uap = v;
    337  1.60.2.1   nathanw 	struct proc *p = l->l_proc;
    338      1.52   thorpej 	struct shmid_ds shmbuf;
    339      1.52   thorpej 	int cmd, error;
    340      1.52   thorpej 
    341      1.52   thorpej 	cmd = SCARG(uap, cmd);
    342      1.52   thorpej 
    343      1.52   thorpej 	if (cmd == IPC_SET) {
    344      1.52   thorpej 		error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
    345      1.52   thorpej 		if (error)
    346      1.52   thorpej 			return (error);
    347      1.52   thorpej 	}
    348      1.52   thorpej 
    349      1.52   thorpej 	error = shmctl1(p, SCARG(uap, shmid), cmd,
    350      1.52   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
    351      1.52   thorpej 
    352      1.52   thorpej 	if (error == 0 && cmd == IPC_STAT)
    353      1.52   thorpej 		error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
    354      1.52   thorpej 
    355      1.52   thorpej 	return (error);
    356      1.52   thorpej }
    357      1.52   thorpej 
    358      1.52   thorpej int
    359      1.52   thorpej shmctl1(p, shmid, cmd, shmbuf)
    360      1.52   thorpej 	struct proc *p;
    361      1.52   thorpej 	int shmid;
    362      1.52   thorpej 	int cmd;
    363      1.52   thorpej 	struct shmid_ds *shmbuf;
    364      1.52   thorpej {
    365      1.12   mycroft 	struct ucred *cred = p->p_ucred;
    366      1.11   hpeyerl 	struct shmid_ds *shmseg;
    367      1.52   thorpej 	int error = 0;
    368      1.11   hpeyerl 
    369  1.60.2.5   nathanw 	shmseg = shm_find_segment_by_shmid(shmid, 0);
    370      1.11   hpeyerl 	if (shmseg == NULL)
    371      1.11   hpeyerl 		return EINVAL;
    372      1.52   thorpej 	switch (cmd) {
    373      1.11   hpeyerl 	case IPC_STAT:
    374      1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    375      1.11   hpeyerl 			return error;
    376      1.52   thorpej 		memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
    377      1.11   hpeyerl 		break;
    378      1.11   hpeyerl 	case IPC_SET:
    379      1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    380      1.13   mycroft 			return error;
    381      1.52   thorpej 		shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
    382      1.52   thorpej 		shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
    383      1.12   mycroft 		shmseg->shm_perm.mode =
    384      1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    385      1.52   thorpej 		    (shmbuf->shm_perm.mode & ACCESSPERMS);
    386      1.12   mycroft 		shmseg->shm_ctime = time.tv_sec;
    387      1.11   hpeyerl 		break;
    388      1.11   hpeyerl 	case IPC_RMID:
    389      1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    390      1.13   mycroft 			return error;
    391      1.52   thorpej 		shmseg->shm_perm._key = IPC_PRIVATE;
    392      1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    393      1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    394      1.12   mycroft 			shm_deallocate_segment(shmseg);
    395      1.52   thorpej 			shm_last_free = IPCID_TO_IX(shmid);
    396      1.11   hpeyerl 		}
    397      1.11   hpeyerl 		break;
    398      1.11   hpeyerl 	case SHM_LOCK:
    399      1.11   hpeyerl 	case SHM_UNLOCK:
    400      1.11   hpeyerl 	default:
    401      1.11   hpeyerl 		return EINVAL;
    402      1.11   hpeyerl 	}
    403      1.11   hpeyerl 	return 0;
    404      1.11   hpeyerl }
    405      1.11   hpeyerl 
    406      1.12   mycroft static int
    407      1.12   mycroft shmget_existing(p, uap, mode, segnum, retval)
    408      1.11   hpeyerl 	struct proc *p;
    409      1.33   mycroft 	struct sys_shmget_args /* {
    410      1.26       cgd 		syscallarg(key_t) key;
    411      1.44    kleink 		syscallarg(size_t) size;
    412      1.35  christos 		syscallarg(int) shmflg;
    413      1.26       cgd 	} */ *uap;
    414      1.11   hpeyerl 	int mode;
    415      1.11   hpeyerl 	int segnum;
    416      1.26       cgd 	register_t *retval;
    417      1.11   hpeyerl {
    418      1.12   mycroft 	struct shmid_ds *shmseg;
    419      1.12   mycroft 	struct ucred *cred = p->p_ucred;
    420      1.11   hpeyerl 	int error;
    421      1.11   hpeyerl 
    422      1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    423      1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    424      1.16   mycroft 		/*
    425      1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    426      1.16   mycroft 		 * until it's done, and look the key up again (in case the
    427      1.16   mycroft 		 * allocation failed or it was freed).
    428      1.16   mycroft 		 */
    429      1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    430      1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    431      1.35  christos 		if (error)
    432      1.16   mycroft 			return error;
    433      1.16   mycroft 		return EAGAIN;
    434      1.16   mycroft 	}
    435      1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    436      1.11   hpeyerl 		return error;
    437      1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    438      1.11   hpeyerl 		return EINVAL;
    439      1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    440      1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    441      1.12   mycroft 		return EEXIST;
    442      1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    443      1.11   hpeyerl 	return 0;
    444      1.11   hpeyerl }
    445      1.11   hpeyerl 
    446      1.14   mycroft static int
    447      1.14   mycroft shmget_allocate_segment(p, uap, mode, retval)
    448      1.14   mycroft 	struct proc *p;
    449      1.33   mycroft 	struct sys_shmget_args /* {
    450      1.26       cgd 		syscallarg(key_t) key;
    451      1.44    kleink 		syscallarg(size_t) size;
    452      1.35  christos 		syscallarg(int) shmflg;
    453      1.26       cgd 	} */ *uap;
    454      1.14   mycroft 	int mode;
    455      1.26       cgd 	register_t *retval;
    456      1.14   mycroft {
    457      1.39  drochner 	int i, segnum, shmid, size;
    458      1.14   mycroft 	struct ucred *cred = p->p_ucred;
    459      1.14   mycroft 	struct shmid_ds *shmseg;
    460      1.14   mycroft 	struct shm_handle *shm_handle;
    461      1.40  drochner 	int error = 0;
    462      1.14   mycroft 
    463      1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    464      1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    465      1.14   mycroft 		return EINVAL;
    466      1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    467      1.14   mycroft 		return ENOSPC;
    468      1.53     ragge 	size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
    469      1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    470      1.14   mycroft 		return ENOMEM;
    471      1.14   mycroft 	if (shm_last_free < 0) {
    472      1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    473      1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    474      1.14   mycroft 				break;
    475      1.14   mycroft 		if (i == shminfo.shmmni)
    476      1.14   mycroft 			panic("shmseg free count inconsistent");
    477      1.14   mycroft 		segnum = i;
    478      1.14   mycroft 	} else  {
    479      1.14   mycroft 		segnum = shm_last_free;
    480      1.14   mycroft 		shm_last_free = -1;
    481      1.14   mycroft 	}
    482      1.14   mycroft 	shmseg = &shmsegs[segnum];
    483      1.14   mycroft 	/*
    484      1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    485      1.14   mycroft 	 * so that noone else tries to create the same key.
    486      1.14   mycroft 	 */
    487      1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    488      1.52   thorpej 	shmseg->shm_perm._key = SCARG(uap, key);
    489      1.52   thorpej 	shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
    490      1.14   mycroft 	shm_handle = (struct shm_handle *)
    491      1.14   mycroft 	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
    492      1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    493      1.42       mrg 
    494      1.42       mrg 	shm_handle->shm_object = uao_create(size, 0);
    495      1.42       mrg 
    496      1.52   thorpej 	shmseg->_shm_internal = shm_handle;
    497      1.14   mycroft 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
    498      1.14   mycroft 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
    499      1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    500      1.16   mycroft 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
    501      1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    502      1.14   mycroft 	shmseg->shm_cpid = p->p_pid;
    503      1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    504      1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    505      1.14   mycroft 	shmseg->shm_ctime = time.tv_sec;
    506      1.14   mycroft 	shm_committed += btoc(size);
    507      1.14   mycroft 	shm_nused++;
    508      1.40  drochner 
    509      1.51       mrg 	*retval = shmid;
    510      1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    511      1.16   mycroft 		/*
    512      1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    513      1.16   mycroft 		 * them up now.
    514      1.16   mycroft 		 */
    515      1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    516      1.16   mycroft 		wakeup((caddr_t)shmseg);
    517      1.16   mycroft 	}
    518      1.40  drochner 	return error;
    519      1.14   mycroft }
    520      1.14   mycroft 
    521      1.12   mycroft int
    522  1.60.2.1   nathanw sys_shmget(l, v, retval)
    523  1.60.2.1   nathanw 	struct lwp *l;
    524      1.32   thorpej 	void *v;
    525      1.32   thorpej 	register_t *retval;
    526      1.32   thorpej {
    527      1.33   mycroft 	struct sys_shmget_args /* {
    528      1.26       cgd 		syscallarg(key_t) key;
    529      1.26       cgd 		syscallarg(int) size;
    530      1.35  christos 		syscallarg(int) shmflg;
    531      1.32   thorpej 	} */ *uap = v;
    532  1.60.2.1   nathanw 	struct proc *p = l->l_proc;
    533      1.11   hpeyerl 	int segnum, mode, error;
    534      1.11   hpeyerl 
    535      1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    536      1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    537      1.16   mycroft 	again:
    538      1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    539      1.16   mycroft 		if (segnum >= 0) {
    540      1.16   mycroft 			error = shmget_existing(p, uap, mode, segnum, retval);
    541      1.16   mycroft 			if (error == EAGAIN)
    542      1.16   mycroft 				goto again;
    543      1.16   mycroft 			return error;
    544      1.16   mycroft 		}
    545      1.26       cgd 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    546      1.14   mycroft 			return ENOENT;
    547      1.11   hpeyerl 	}
    548      1.14   mycroft 	return shmget_allocate_segment(p, uap, mode, retval);
    549      1.11   hpeyerl }
    550      1.11   hpeyerl 
    551      1.12   mycroft void
    552      1.41   thorpej shmfork(vm1, vm2)
    553      1.41   thorpej 	struct vmspace *vm1, *vm2;
    554      1.11   hpeyerl {
    555      1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    556      1.12   mycroft 	size_t size;
    557      1.11   hpeyerl 	int i;
    558      1.11   hpeyerl 
    559      1.41   thorpej 	if (vm1->vm_shm == NULL) {
    560      1.41   thorpej 		vm2->vm_shm = NULL;
    561      1.38  christos 		return;
    562      1.38  christos 	}
    563      1.41   thorpej 
    564      1.12   mycroft 	size = shminfo.shmseg * sizeof(struct shmmap_state);
    565      1.12   mycroft 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
    566      1.46     perry 	memcpy(shmmap_s, vm1->vm_shm, size);
    567      1.41   thorpej 	vm2->vm_shm = (caddr_t)shmmap_s;
    568      1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    569      1.12   mycroft 		if (shmmap_s->shmid != -1)
    570      1.11   hpeyerl 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
    571      1.11   hpeyerl }
    572      1.11   hpeyerl 
    573      1.12   mycroft void
    574      1.41   thorpej shmexit(vm)
    575      1.41   thorpej 	struct vmspace *vm;
    576      1.11   hpeyerl {
    577      1.12   mycroft 	struct shmmap_state *shmmap_s;
    578      1.11   hpeyerl 	int i;
    579      1.11   hpeyerl 
    580      1.41   thorpej 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    581      1.38  christos 	if (shmmap_s == NULL)
    582      1.38  christos 		return;
    583      1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    584      1.12   mycroft 		if (shmmap_s->shmid != -1)
    585      1.41   thorpej 			shm_delete_mapping(vm, shmmap_s);
    586      1.41   thorpej 	free(vm->vm_shm, M_SHM);
    587      1.41   thorpej 	vm->vm_shm = NULL;
    588      1.11   hpeyerl }
    589      1.11   hpeyerl 
    590      1.12   mycroft void
    591      1.12   mycroft shminit()
    592      1.11   hpeyerl {
    593      1.11   hpeyerl 	int i;
    594      1.24   deraadt 
    595      1.60   thorpej 	shminfo.shmmax *= PAGE_SIZE;
    596      1.11   hpeyerl 
    597      1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    598      1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    599      1.52   thorpej 		shmsegs[i].shm_perm._seq = 0;
    600      1.11   hpeyerl 	}
    601      1.11   hpeyerl 	shm_last_free = 0;
    602      1.11   hpeyerl 	shm_nused = 0;
    603      1.11   hpeyerl 	shm_committed = 0;
    604      1.11   hpeyerl }
    605