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