Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.109
      1  1.109     rmind /*	$NetBSD: sysv_shm.c,v 1.109 2008/05/29 21:38:18 rmind Exp $	*/
      2   1.52   thorpej 
      3   1.52   thorpej /*-
      4   1.96        ad  * Copyright (c) 1999, 2007 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.102        ad  * NASA Ames Research Center, and by Mindaugas Rasiukevicius.
     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  *
     20   1.52   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.52   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.52   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.52   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.52   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.52   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.52   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.52   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.52   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.52   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.52   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.52   thorpej  */
     32   1.22       cgd 
     33   1.11   hpeyerl /*
     34   1.48   mycroft  * Copyright (c) 1994 Adam Glass and Charles M. Hannum.  All rights reserved.
     35   1.11   hpeyerl  *
     36   1.11   hpeyerl  * Redistribution and use in source and binary forms, with or without
     37   1.11   hpeyerl  * modification, are permitted provided that the following conditions
     38   1.11   hpeyerl  * are met:
     39   1.11   hpeyerl  * 1. Redistributions of source code must retain the above copyright
     40   1.11   hpeyerl  *    notice, this list of conditions and the following disclaimer.
     41   1.17   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.17   mycroft  *    notice, this list of conditions and the following disclaimer in the
     43   1.17   mycroft  *    documentation and/or other materials provided with the distribution.
     44   1.17   mycroft  * 3. All advertising materials mentioning features or use of this software
     45   1.17   mycroft  *    must display the following acknowledgement:
     46   1.48   mycroft  *	This product includes software developed by Adam Glass and Charles M.
     47   1.17   mycroft  *	Hannum.
     48   1.17   mycroft  * 4. The names of the authors may not be used to endorse or promote products
     49   1.11   hpeyerl  *    derived from this software without specific prior written permission.
     50   1.11   hpeyerl  *
     51   1.17   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     52   1.17   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53   1.17   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54   1.17   mycroft  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     55   1.17   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56   1.17   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57   1.17   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58   1.17   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59   1.17   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60   1.17   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61   1.11   hpeyerl  */
     62   1.62     lukem 
     63   1.62     lukem #include <sys/cdefs.h>
     64  1.109     rmind __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.109 2008/05/29 21:38:18 rmind Exp $");
     65   1.43       mrg 
     66   1.50      tron #define SYSVSHM
     67   1.11   hpeyerl 
     68   1.11   hpeyerl #include <sys/param.h>
     69   1.11   hpeyerl #include <sys/kernel.h>
     70  1.102        ad #include <sys/kmem.h>
     71   1.11   hpeyerl #include <sys/shm.h>
     72   1.96        ad #include <sys/mutex.h>
     73   1.11   hpeyerl #include <sys/mman.h>
     74   1.12   mycroft #include <sys/stat.h>
     75   1.56    simonb #include <sys/sysctl.h>
     76   1.56    simonb #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     77   1.56    simonb #include <sys/syscallargs.h>
     78   1.69  drochner #include <sys/queue.h>
     79   1.69  drochner #include <sys/pool.h>
     80   1.87      elad #include <sys/kauth.h>
     81   1.35  christos 
     82   1.60   thorpej #include <uvm/uvm_extern.h>
     83   1.75  christos #include <uvm/uvm_object.h>
     84   1.60   thorpej 
     85   1.85  christos int shm_nused;
     86   1.55    simonb struct	shmid_ds *shmsegs;
     87   1.11   hpeyerl 
     88   1.69  drochner struct shmmap_entry {
     89   1.69  drochner 	SLIST_ENTRY(shmmap_entry) next;
     90   1.47       eeh 	vaddr_t va;
     91   1.11   hpeyerl 	int shmid;
     92   1.11   hpeyerl };
     93   1.11   hpeyerl 
     94  1.102        ad static kmutex_t		shm_lock;
     95  1.102        ad static kcondvar_t *	shm_cv;
     96  1.102        ad static struct pool	shmmap_entry_pool;
     97  1.106     rmind static int		shm_last_free, shm_use_phys;
     98  1.106     rmind static size_t		shm_committed;
     99  1.102        ad 
    100  1.102        ad static kcondvar_t	shm_realloc_cv;
    101  1.102        ad static bool		shm_realloc_state;
    102  1.102        ad static u_int		shm_realloc_disable;
    103   1.69  drochner 
    104   1.69  drochner struct shmmap_state {
    105   1.69  drochner 	unsigned int nitems;
    106   1.69  drochner 	unsigned int nrefs;
    107   1.69  drochner 	SLIST_HEAD(, shmmap_entry) entries;
    108   1.69  drochner };
    109   1.69  drochner 
    110  1.102        ad #ifdef SHMDEBUG
    111  1.102        ad #define SHMPRINTF(a) printf a
    112  1.102        ad #else
    113  1.102        ad #define SHMPRINTF(a)
    114  1.102        ad #endif
    115  1.102        ad 
    116   1.92  christos static int shmrealloc(int);
    117   1.11   hpeyerl 
    118  1.102        ad /*
    119  1.102        ad  * Find the shared memory segment by the identifier.
    120  1.102        ad  *  => must be called with shm_lock held;
    121  1.102        ad  */
    122   1.86   thorpej static struct shmid_ds *
    123   1.86   thorpej shm_find_segment_by_shmid(int shmid)
    124   1.11   hpeyerl {
    125   1.11   hpeyerl 	int segnum;
    126   1.11   hpeyerl 	struct shmid_ds *shmseg;
    127   1.11   hpeyerl 
    128  1.102        ad 	KASSERT(mutex_owned(&shm_lock));
    129  1.102        ad 
    130   1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    131   1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    132   1.11   hpeyerl 		return NULL;
    133   1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    134   1.64      fvdl 	if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
    135   1.64      fvdl 		return NULL;
    136  1.102        ad 	if ((shmseg->shm_perm.mode &
    137  1.102        ad 	    (SHMSEG_REMOVED|SHMSEG_RMLINGER)) == SHMSEG_REMOVED)
    138   1.64      fvdl 		return NULL;
    139   1.64      fvdl 	if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
    140   1.11   hpeyerl 		return NULL;
    141  1.102        ad 
    142   1.11   hpeyerl 	return shmseg;
    143   1.11   hpeyerl }
    144   1.11   hpeyerl 
    145  1.102        ad /*
    146  1.102        ad  * Free memory segment.
    147  1.102        ad  *  => must be called with shm_lock held;
    148  1.102        ad  */
    149   1.12   mycroft static void
    150  1.102        ad shm_free_segment(int segnum)
    151   1.12   mycroft {
    152  1.102        ad 	struct shmid_ds *shmseg;
    153  1.102        ad 	size_t size;
    154  1.102        ad 	bool wanted;
    155  1.102        ad 
    156  1.102        ad 	KASSERT(mutex_owned(&shm_lock));
    157   1.12   mycroft 
    158  1.102        ad 	shmseg = &shmsegs[segnum];
    159  1.102        ad 	SHMPRINTF(("shm freeing key 0x%lx seq 0x%x\n",
    160  1.102        ad 	    shmseg->shm_perm._key, shmseg->shm_perm._seq));
    161  1.102        ad 
    162  1.102        ad 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    163  1.102        ad 	wanted = (shmseg->shm_perm.mode & SHMSEG_WANTED);
    164   1.85  christos 
    165   1.52   thorpej 	shmseg->_shm_internal = NULL;
    166   1.14   mycroft 	shm_committed -= btoc(size);
    167  1.102        ad 	shm_nused--;
    168   1.12   mycroft 	shmseg->shm_perm.mode = SHMSEG_FREE;
    169  1.102        ad 	shm_last_free = segnum;
    170  1.102        ad 	if (wanted == true)
    171  1.102        ad 		cv_broadcast(&shm_cv[segnum]);
    172   1.12   mycroft }
    173   1.12   mycroft 
    174  1.102        ad /*
    175  1.102        ad  * Delete entry from the shm map.
    176  1.102        ad  *  => must be called with shm_lock held;
    177  1.102        ad  */
    178  1.102        ad static struct uvm_object *
    179  1.102        ad shm_delete_mapping(struct shmmap_state *shmmap_s,
    180   1.86   thorpej     struct shmmap_entry *shmmap_se)
    181   1.11   hpeyerl {
    182  1.102        ad 	struct uvm_object *uobj = NULL;
    183   1.12   mycroft 	struct shmid_ds *shmseg;
    184   1.61       chs 	int segnum;
    185  1.102        ad 
    186  1.102        ad 	KASSERT(mutex_owned(&shm_lock));
    187   1.76  junyoung 
    188   1.69  drochner 	segnum = IPCID_TO_IX(shmmap_se->shmid);
    189   1.12   mycroft 	shmseg = &shmsegs[segnum];
    190   1.69  drochner 	SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
    191   1.69  drochner 	shmmap_s->nitems--;
    192   1.88    kardel 	shmseg->shm_dtime = time_second;
    193   1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    194   1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    195  1.102        ad 		uobj = shmseg->_shm_internal;
    196  1.102        ad 		shm_free_segment(segnum);
    197   1.11   hpeyerl 	}
    198  1.102        ad 
    199  1.102        ad 	return uobj;
    200   1.11   hpeyerl }
    201   1.11   hpeyerl 
    202   1.69  drochner /*
    203  1.102        ad  * Get a non-shared shm map for that vmspace.  Note, that memory
    204  1.102        ad  * allocation might be performed with lock held.
    205   1.69  drochner  */
    206   1.69  drochner static struct shmmap_state *
    207   1.69  drochner shmmap_getprivate(struct proc *p)
    208   1.69  drochner {
    209   1.69  drochner 	struct shmmap_state *oshmmap_s, *shmmap_s;
    210   1.69  drochner 	struct shmmap_entry *oshmmap_se, *shmmap_se;
    211   1.69  drochner 
    212  1.102        ad 	KASSERT(mutex_owned(&shm_lock));
    213  1.102        ad 
    214  1.102        ad 	/* 1. A shm map with refcnt = 1, used by ourselves, thus return */
    215   1.69  drochner 	oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    216   1.69  drochner 	if (oshmmap_s && oshmmap_s->nrefs == 1)
    217  1.102        ad 		return oshmmap_s;
    218   1.69  drochner 
    219  1.102        ad 	/* 2. No shm map preset - create a fresh one */
    220  1.102        ad 	shmmap_s = kmem_zalloc(sizeof(struct shmmap_state), KM_SLEEP);
    221   1.69  drochner 	shmmap_s->nrefs = 1;
    222   1.69  drochner 	SLIST_INIT(&shmmap_s->entries);
    223   1.98  christos 	p->p_vmspace->vm_shm = (void *)shmmap_s;
    224   1.69  drochner 
    225  1.102        ad 	if (oshmmap_s == NULL)
    226  1.102        ad 		return shmmap_s;
    227  1.102        ad 
    228  1.102        ad 	SHMPRINTF(("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
    229  1.102        ad 	    p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs));
    230   1.69  drochner 
    231  1.102        ad 	/* 3. A shared shm map, copy to a fresh one and adjust refcounts */
    232   1.69  drochner 	SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
    233   1.69  drochner 		shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
    234   1.69  drochner 		shmmap_se->va = oshmmap_se->va;
    235   1.69  drochner 		shmmap_se->shmid = oshmmap_se->shmid;
    236   1.69  drochner 		SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
    237   1.69  drochner 	}
    238   1.69  drochner 	shmmap_s->nitems = oshmmap_s->nitems;
    239   1.69  drochner 	oshmmap_s->nrefs--;
    240  1.102        ad 
    241  1.102        ad 	return shmmap_s;
    242   1.69  drochner }
    243   1.69  drochner 
    244  1.102        ad /*
    245  1.102        ad  * Lock/unlock the memory.
    246  1.102        ad  *  => must be called with shm_lock held;
    247  1.102        ad  *  => called from one place, thus, inline;
    248  1.102        ad  */
    249  1.102        ad static inline int
    250  1.102        ad shm_memlock(struct lwp *l, struct shmid_ds *shmseg, int shmid, int cmd)
    251   1.70  drochner {
    252  1.102        ad 	struct proc *p = l->l_proc;
    253   1.70  drochner 	struct shmmap_entry *shmmap_se;
    254  1.102        ad 	struct shmmap_state *shmmap_s;
    255  1.102        ad 	size_t size;
    256  1.102        ad 	int error;
    257  1.102        ad 
    258  1.102        ad 	KASSERT(mutex_owned(&shm_lock));
    259  1.102        ad 	shmmap_s = shmmap_getprivate(p);
    260  1.102        ad 
    261  1.102        ad 	/* Find our shared memory address by shmid */
    262  1.102        ad 	SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
    263  1.102        ad 		if (shmmap_se->shmid != shmid)
    264  1.102        ad 			continue;
    265  1.102        ad 
    266  1.102        ad 		size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    267  1.102        ad 
    268  1.102        ad 		if (cmd == SHM_LOCK &&
    269  1.102        ad 		    (shmseg->shm_perm.mode & SHMSEG_WIRED) == 0) {
    270  1.102        ad 			/* Wire the object and map, then tag it */
    271  1.109     rmind 			error = uobj_wirepages(shmseg->_shm_internal, 0, size);
    272  1.102        ad 			if (error)
    273  1.102        ad 				return EIO;
    274  1.102        ad 			error = uvm_map_pageable(&p->p_vmspace->vm_map,
    275  1.102        ad 			    shmmap_se->va, shmmap_se->va + size, false, 0);
    276  1.102        ad 			if (error) {
    277  1.109     rmind 				uobj_unwirepages(shmseg->_shm_internal, 0, size);
    278  1.102        ad 				if (error == EFAULT)
    279  1.102        ad 					error = ENOMEM;
    280  1.102        ad 				return error;
    281  1.102        ad 			}
    282  1.102        ad 			shmseg->shm_perm.mode |= SHMSEG_WIRED;
    283   1.70  drochner 
    284  1.102        ad 		} else if (cmd == SHM_UNLOCK &&
    285  1.102        ad 		    (shmseg->shm_perm.mode & SHMSEG_WIRED) != 0) {
    286  1.102        ad 			/* Unwire the object and map, then untag it */
    287  1.109     rmind 			uobj_unwirepages(shmseg->_shm_internal, 0, size);
    288  1.102        ad 			error = uvm_map_pageable(&p->p_vmspace->vm_map,
    289  1.102        ad 			    shmmap_se->va, shmmap_se->va + size, true, 0);
    290  1.102        ad 			if (error)
    291  1.102        ad 				return EIO;
    292  1.102        ad 			shmseg->shm_perm.mode &= ~SHMSEG_WIRED;
    293  1.102        ad 		}
    294   1.70  drochner 	}
    295  1.102        ad 
    296   1.70  drochner 	return 0;
    297   1.70  drochner }
    298   1.70  drochner 
    299  1.102        ad /*
    300  1.102        ad  * Unmap shared memory.
    301  1.102        ad  */
    302   1.12   mycroft int
    303  1.101       dsl sys_shmdt(struct lwp *l, const struct sys_shmdt_args *uap, register_t *retval)
    304   1.32   thorpej {
    305  1.101       dsl 	/* {
    306   1.44    kleink 		syscallarg(const void *) shmaddr;
    307  1.101       dsl 	} */
    308   1.65   thorpej 	struct proc *p = l->l_proc;
    309  1.102        ad 	struct shmmap_state *shmmap_s1, *shmmap_s;
    310   1.69  drochner 	struct shmmap_entry *shmmap_se;
    311  1.102        ad 	struct uvm_object *uobj;
    312  1.102        ad 	struct shmid_ds *shmseg;
    313  1.102        ad 	size_t size;
    314   1.11   hpeyerl 
    315  1.102        ad 	mutex_enter(&shm_lock);
    316  1.102        ad 	/* In case of reallocation, we will wait for completion */
    317  1.102        ad 	while (__predict_false(shm_realloc_state))
    318  1.102        ad 		cv_wait(&shm_realloc_cv, &shm_lock);
    319  1.102        ad 
    320  1.102        ad 	shmmap_s1 = (struct shmmap_state *)p->p_vmspace->vm_shm;
    321  1.102        ad 	if (shmmap_s1 == NULL) {
    322  1.102        ad 		mutex_exit(&shm_lock);
    323   1.38  christos 		return EINVAL;
    324  1.102        ad 	}
    325   1.38  christos 
    326  1.102        ad 	/* Find the map entry */
    327  1.102        ad 	SLIST_FOREACH(shmmap_se, &shmmap_s1->entries, next)
    328  1.102        ad 		if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr))
    329  1.102        ad 			break;
    330  1.102        ad 	if (shmmap_se == NULL) {
    331  1.102        ad 		mutex_exit(&shm_lock);
    332   1.70  drochner 		return EINVAL;
    333  1.102        ad 	}
    334   1.70  drochner 
    335  1.102        ad 	shmmap_s = shmmap_getprivate(p);
    336  1.102        ad 	if (shmmap_s != shmmap_s1) {
    337  1.102        ad 		/* Map has been copied, lookup entry in new map */
    338  1.102        ad 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    339  1.102        ad 			if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr))
    340  1.102        ad 				break;
    341  1.102        ad 		if (shmmap_se == NULL) {
    342  1.102        ad 			mutex_exit(&shm_lock);
    343  1.102        ad 			return EINVAL;
    344  1.102        ad 		}
    345   1.70  drochner 	}
    346  1.102        ad 
    347  1.102        ad 	SHMPRINTF(("shmdt: vm %p: remove %d @%lx\n",
    348  1.102        ad 	    p->p_vmspace, shmmap_se->shmid, shmmap_se->va));
    349  1.102        ad 
    350  1.102        ad 	/* Delete the entry from shm map */
    351  1.102        ad 	uobj = shm_delete_mapping(shmmap_s, shmmap_se);
    352  1.102        ad 	shmseg = &shmsegs[IPCID_TO_IX(shmmap_se->shmid)];
    353  1.102        ad 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    354  1.102        ad 	mutex_exit(&shm_lock);
    355  1.102        ad 
    356  1.102        ad 	uvm_deallocate(&p->p_vmspace->vm_map, shmmap_se->va, size);
    357  1.102        ad 	if (uobj != NULL)
    358  1.102        ad 		uao_detach(uobj);
    359  1.102        ad 	pool_put(&shmmap_entry_pool, shmmap_se);
    360  1.102        ad 
    361   1.70  drochner 	return 0;
    362   1.11   hpeyerl }
    363   1.11   hpeyerl 
    364  1.102        ad /*
    365  1.102        ad  * Map shared memory.
    366  1.102        ad  */
    367   1.12   mycroft int
    368  1.101       dsl sys_shmat(struct lwp *l, const struct sys_shmat_args *uap, register_t *retval)
    369   1.32   thorpej {
    370  1.101       dsl 	/* {
    371   1.26       cgd 		syscallarg(int) shmid;
    372   1.44    kleink 		syscallarg(const void *) shmaddr;
    373   1.35  christos 		syscallarg(int) shmflg;
    374  1.101       dsl 	} */
    375   1.94     rmind 	int error, flags = 0;
    376   1.65   thorpej 	struct proc *p = l->l_proc;
    377   1.89        ad 	kauth_cred_t cred = l->l_cred;
    378   1.11   hpeyerl 	struct shmid_ds *shmseg;
    379   1.69  drochner 	struct shmmap_state *shmmap_s;
    380  1.102        ad 	struct shmmap_entry *shmmap_se;
    381   1.74  christos 	struct uvm_object *uobj;
    382  1.102        ad 	struct vmspace *vm;
    383   1.47       eeh 	vaddr_t attach_va;
    384   1.11   hpeyerl 	vm_prot_t prot;
    385   1.47       eeh 	vsize_t size;
    386  1.102        ad 
    387  1.102        ad 	/* Allocate a new map entry and set it */
    388  1.102        ad 	shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
    389  1.102        ad 
    390  1.102        ad 	mutex_enter(&shm_lock);
    391  1.102        ad 	/* In case of reallocation, we will wait for completion */
    392  1.102        ad 	while (__predict_false(shm_realloc_state))
    393  1.102        ad 		cv_wait(&shm_realloc_cv, &shm_lock);
    394   1.11   hpeyerl 
    395   1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    396  1.102        ad 	if (shmseg == NULL) {
    397  1.102        ad 		error = EINVAL;
    398  1.102        ad 		goto err;
    399  1.102        ad 	}
    400   1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    401  1.102        ad 	    (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    402   1.35  christos 	if (error)
    403  1.102        ad 		goto err;
    404   1.69  drochner 
    405  1.102        ad 	vm = p->p_vmspace;
    406  1.102        ad 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    407  1.102        ad 	if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg) {
    408  1.102        ad 		error = EMFILE;
    409  1.102        ad 		goto err;
    410  1.102        ad 	}
    411   1.69  drochner 
    412   1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    413   1.12   mycroft 	prot = VM_PROT_READ;
    414   1.78  jdolecek 	if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
    415   1.12   mycroft 		prot |= VM_PROT_WRITE;
    416   1.78  jdolecek 	if (SCARG(uap, shmaddr)) {
    417   1.94     rmind 		flags |= UVM_FLAG_FIXED;
    418   1.78  jdolecek 		if (SCARG(uap, shmflg) & SHM_RND)
    419   1.26       cgd 			attach_va =
    420   1.78  jdolecek 			    (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    421   1.78  jdolecek 		else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    422   1.78  jdolecek 			attach_va = (vaddr_t)SCARG(uap, shmaddr);
    423  1.102        ad 		else {
    424  1.102        ad 			error = EINVAL;
    425  1.102        ad 			goto err;
    426  1.102        ad 		}
    427   1.12   mycroft 	} else {
    428  1.108     rmind 		/* This is just a hint to uvm_map() about where to put it. */
    429   1.83      fvdl 		attach_va = p->p_emul->e_vm_default_addr(p,
    430  1.102        ad 		    (vaddr_t)vm->vm_daddr, size);
    431   1.11   hpeyerl 	}
    432  1.102        ad 
    433  1.102        ad 	/*
    434  1.102        ad 	 * Create a map entry, add it to the list and increase the counters.
    435  1.102        ad 	 * The lock will be dropped before the mapping, disable reallocation.
    436  1.102        ad 	 */
    437  1.102        ad 	shmmap_s = shmmap_getprivate(p);
    438  1.102        ad 	SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
    439  1.102        ad 	shmmap_s->nitems++;
    440  1.102        ad 	shmseg->shm_lpid = p->p_pid;
    441  1.102        ad 	shmseg->shm_nattch++;
    442  1.102        ad 	shm_realloc_disable++;
    443  1.102        ad 	mutex_exit(&shm_lock);
    444  1.102        ad 
    445  1.102        ad 	/*
    446  1.102        ad 	 * Add a reference to the memory object, map it to the
    447  1.102        ad 	 * address space, and lock the memory, if needed.
    448  1.102        ad 	 */
    449   1.80  jdolecek 	uobj = shmseg->_shm_internal;
    450  1.102        ad 	uao_reference(uobj);
    451  1.102        ad 	error = uvm_map(&vm->vm_map, &attach_va, size, uobj, 0, 0,
    452   1.94     rmind 	    UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
    453   1.92  christos 	if (error)
    454  1.102        ad 		goto err_detach;
    455   1.92  christos 	if (shm_use_phys || (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    456  1.102        ad 		error = uvm_map_pageable(&vm->vm_map, attach_va,
    457   1.97   thorpej 		    attach_va + size, false, 0);
    458   1.92  christos 		if (error) {
    459   1.92  christos 			if (error == EFAULT)
    460   1.92  christos 				error = ENOMEM;
    461  1.102        ad 			uvm_deallocate(&vm->vm_map, attach_va, size);
    462  1.102        ad 			goto err_detach;
    463   1.92  christos 		}
    464   1.42       mrg 	}
    465   1.92  christos 
    466  1.102        ad 	/* Set the new address, and update the time */
    467  1.102        ad 	mutex_enter(&shm_lock);
    468   1.69  drochner 	shmmap_se->va = attach_va;
    469   1.78  jdolecek 	shmmap_se->shmid = SCARG(uap, shmid);
    470   1.88    kardel 	shmseg->shm_atime = time_second;
    471  1.102        ad 	shm_realloc_disable--;
    472  1.102        ad 	retval[0] = attach_va;
    473  1.102        ad 	SHMPRINTF(("shmat: vm %p: add %d @%lx\n",
    474  1.102        ad 	    p->p_vmspace, shmmap_se->shmid, attach_va));
    475  1.102        ad err:
    476  1.102        ad 	cv_broadcast(&shm_realloc_cv);
    477  1.102        ad 	mutex_exit(&shm_lock);
    478  1.102        ad 	if (error && shmmap_se)
    479  1.102        ad 		pool_put(&shmmap_entry_pool, shmmap_se);
    480  1.102        ad 	return error;
    481   1.78  jdolecek 
    482  1.102        ad err_detach:
    483  1.102        ad 	uao_detach(uobj);
    484  1.102        ad 	mutex_enter(&shm_lock);
    485  1.102        ad 	uobj = shm_delete_mapping(shmmap_s, shmmap_se);
    486  1.102        ad 	shm_realloc_disable--;
    487  1.102        ad 	cv_broadcast(&shm_realloc_cv);
    488  1.102        ad 	mutex_exit(&shm_lock);
    489  1.102        ad 	if (uobj != NULL)
    490  1.102        ad 		uao_detach(uobj);
    491  1.102        ad 	pool_put(&shmmap_entry_pool, shmmap_se);
    492   1.92  christos 	return error;
    493   1.11   hpeyerl }
    494   1.11   hpeyerl 
    495  1.102        ad /*
    496  1.102        ad  * Shared memory control operations.
    497  1.102        ad  */
    498   1.12   mycroft int
    499  1.101       dsl sys___shmctl13(struct lwp *l, const struct sys___shmctl13_args *uap, register_t *retval)
    500   1.32   thorpej {
    501  1.101       dsl 	/* {
    502   1.26       cgd 		syscallarg(int) shmid;
    503   1.26       cgd 		syscallarg(int) cmd;
    504   1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    505  1.101       dsl 	} */
    506   1.52   thorpej 	struct shmid_ds shmbuf;
    507   1.52   thorpej 	int cmd, error;
    508   1.52   thorpej 
    509   1.52   thorpej 	cmd = SCARG(uap, cmd);
    510   1.52   thorpej 	if (cmd == IPC_SET) {
    511   1.52   thorpej 		error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
    512   1.52   thorpej 		if (error)
    513  1.102        ad 			return error;
    514   1.52   thorpej 	}
    515   1.52   thorpej 
    516   1.89        ad 	error = shmctl1(l, SCARG(uap, shmid), cmd,
    517   1.52   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
    518   1.52   thorpej 
    519   1.52   thorpej 	if (error == 0 && cmd == IPC_STAT)
    520   1.52   thorpej 		error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
    521   1.52   thorpej 
    522  1.102        ad 	return error;
    523   1.52   thorpej }
    524   1.52   thorpej 
    525   1.52   thorpej int
    526   1.89        ad shmctl1(struct lwp *l, int shmid, int cmd, struct shmid_ds *shmbuf)
    527   1.52   thorpej {
    528  1.102        ad 	struct uvm_object *uobj = NULL;
    529   1.89        ad 	kauth_cred_t cred = l->l_cred;
    530   1.11   hpeyerl 	struct shmid_ds *shmseg;
    531   1.52   thorpej 	int error = 0;
    532  1.102        ad 
    533  1.102        ad 	mutex_enter(&shm_lock);
    534  1.102        ad 	/* In case of reallocation, we will wait for completion */
    535  1.102        ad 	while (__predict_false(shm_realloc_state))
    536  1.102        ad 		cv_wait(&shm_realloc_cv, &shm_lock);
    537   1.11   hpeyerl 
    538   1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(shmid);
    539  1.102        ad 	if (shmseg == NULL) {
    540  1.102        ad 		mutex_exit(&shm_lock);
    541   1.11   hpeyerl 		return EINVAL;
    542  1.102        ad 	}
    543   1.92  christos 
    544   1.52   thorpej 	switch (cmd) {
    545   1.11   hpeyerl 	case IPC_STAT:
    546   1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    547  1.102        ad 			break;
    548   1.52   thorpej 		memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
    549   1.11   hpeyerl 		break;
    550   1.11   hpeyerl 	case IPC_SET:
    551   1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    552  1.102        ad 			break;
    553   1.52   thorpej 		shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
    554   1.52   thorpej 		shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
    555   1.12   mycroft 		shmseg->shm_perm.mode =
    556   1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    557   1.52   thorpej 		    (shmbuf->shm_perm.mode & ACCESSPERMS);
    558   1.88    kardel 		shmseg->shm_ctime = time_second;
    559   1.11   hpeyerl 		break;
    560   1.11   hpeyerl 	case IPC_RMID:
    561   1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    562  1.102        ad 			break;
    563   1.52   thorpej 		shmseg->shm_perm._key = IPC_PRIVATE;
    564   1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    565   1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    566  1.102        ad 			uobj = shmseg->_shm_internal;
    567  1.102        ad 			shm_free_segment(IPCID_TO_IX(shmid));
    568   1.11   hpeyerl 		}
    569   1.11   hpeyerl 		break;
    570   1.11   hpeyerl 	case SHM_LOCK:
    571   1.11   hpeyerl 	case SHM_UNLOCK:
    572   1.92  christos 		if ((error = kauth_authorize_generic(cred,
    573   1.92  christos 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
    574  1.102        ad 			break;
    575  1.102        ad 		error = shm_memlock(l, shmseg, shmid, cmd);
    576   1.92  christos 		break;
    577   1.11   hpeyerl 	default:
    578  1.102        ad 		error = EINVAL;
    579   1.11   hpeyerl 	}
    580  1.102        ad 
    581  1.102        ad 	mutex_exit(&shm_lock);
    582  1.102        ad 	if (uobj != NULL)
    583  1.102        ad 		uao_detach(uobj);
    584  1.102        ad 	return error;
    585   1.11   hpeyerl }
    586   1.11   hpeyerl 
    587  1.102        ad /*
    588  1.102        ad  * Try to take an already existing segment.
    589  1.102        ad  *  => must be called with shm_lock held;
    590  1.102        ad  *  => called from one place, thus, inline;
    591  1.102        ad  */
    592  1.102        ad static inline int
    593  1.101       dsl shmget_existing(struct lwp *l, const struct sys_shmget_args *uap, int mode,
    594  1.102        ad     register_t *retval)
    595   1.11   hpeyerl {
    596   1.12   mycroft 	struct shmid_ds *shmseg;
    597   1.89        ad 	kauth_cred_t cred = l->l_cred;
    598  1.102        ad 	int segnum, error;
    599  1.102        ad again:
    600  1.102        ad 	KASSERT(mutex_owned(&shm_lock));
    601  1.102        ad 
    602  1.102        ad 	/* Find segment by key */
    603  1.102        ad 	for (segnum = 0; segnum < shminfo.shmmni; segnum++)
    604  1.102        ad 		if ((shmsegs[segnum].shm_perm.mode & SHMSEG_ALLOCATED) &&
    605  1.102        ad 		    shmsegs[segnum].shm_perm._key == SCARG(uap, key))
    606  1.102        ad 			break;
    607  1.102        ad 	if (segnum == shminfo.shmmni) {
    608  1.102        ad 		/* Not found */
    609  1.102        ad 		return -1;
    610  1.102        ad 	}
    611   1.11   hpeyerl 
    612   1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    613   1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    614   1.16   mycroft 		/*
    615   1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    616   1.16   mycroft 		 * until it's done, and look the key up again (in case the
    617   1.16   mycroft 		 * allocation failed or it was freed).
    618   1.16   mycroft 		 */
    619   1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    620  1.102        ad 		error = cv_wait_sig(&shm_cv[segnum], &shm_lock);
    621   1.35  christos 		if (error)
    622   1.16   mycroft 			return error;
    623  1.102        ad 		goto again;
    624   1.16   mycroft 	}
    625  1.102        ad 
    626  1.102        ad 	/* Check the permission, segment size and appropriate flag */
    627  1.102        ad 	error = ipcperm(cred, &shmseg->shm_perm, mode);
    628  1.102        ad 	if (error)
    629   1.11   hpeyerl 		return error;
    630   1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    631   1.11   hpeyerl 		return EINVAL;
    632   1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    633   1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    634   1.12   mycroft 		return EEXIST;
    635  1.102        ad 
    636   1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    637   1.11   hpeyerl 	return 0;
    638   1.11   hpeyerl }
    639   1.11   hpeyerl 
    640  1.102        ad int
    641  1.102        ad sys_shmget(struct lwp *l, const struct sys_shmget_args *uap, register_t *retval)
    642   1.14   mycroft {
    643  1.102        ad 	/* {
    644  1.102        ad 		syscallarg(key_t) key;
    645  1.104     rmind 		syscallarg(size_t) size;
    646  1.102        ad 		syscallarg(int) shmflg;
    647  1.102        ad 	} */
    648  1.102        ad 	struct shmid_ds *shmseg;
    649   1.89        ad 	kauth_cred_t cred = l->l_cred;
    650  1.102        ad 	key_t key = SCARG(uap, key);
    651  1.104     rmind 	size_t size;
    652  1.104     rmind 	int error, mode, segnum;
    653  1.102        ad 	bool lockmem;
    654  1.102        ad 
    655  1.102        ad 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    656  1.102        ad 	if (SCARG(uap, shmflg) & _SHM_RMLINGER)
    657  1.102        ad 		mode |= SHMSEG_RMLINGER;
    658  1.102        ad 
    659  1.102        ad 	SHMPRINTF(("shmget: key 0x%lx size 0x%x shmflg 0x%x mode 0x%x\n",
    660  1.102        ad 	    SCARG(uap, key), SCARG(uap, size), SCARG(uap, shmflg), mode));
    661  1.102        ad 
    662  1.102        ad 	mutex_enter(&shm_lock);
    663  1.102        ad 	/* In case of reallocation, we will wait for completion */
    664  1.102        ad 	while (__predict_false(shm_realloc_state))
    665  1.102        ad 		cv_wait(&shm_realloc_cv, &shm_lock);
    666  1.102        ad 
    667  1.102        ad 	if (key != IPC_PRIVATE) {
    668  1.102        ad 		error = shmget_existing(l, uap, mode, retval);
    669  1.102        ad 		if (error != -1) {
    670  1.102        ad 			mutex_exit(&shm_lock);
    671  1.102        ad 			return error;
    672  1.102        ad 		}
    673  1.102        ad 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0) {
    674  1.102        ad 			mutex_exit(&shm_lock);
    675  1.102        ad 			return ENOENT;
    676  1.102        ad 		}
    677  1.102        ad 	}
    678  1.102        ad 	error = 0;
    679   1.76  junyoung 
    680  1.102        ad 	/*
    681  1.102        ad 	 * Check the for the limits.
    682  1.102        ad 	 */
    683  1.102        ad 	size = SCARG(uap, size);
    684  1.102        ad 	if (size < shminfo.shmmin || size > shminfo.shmmax) {
    685  1.102        ad 		mutex_exit(&shm_lock);
    686   1.14   mycroft 		return EINVAL;
    687  1.102        ad 	}
    688  1.102        ad 	if (shm_nused >= shminfo.shmmni) {
    689  1.102        ad 		mutex_exit(&shm_lock);
    690   1.14   mycroft 		return ENOSPC;
    691  1.102        ad 	}
    692  1.102        ad 	size = (size + PGOFSET) & ~PGOFSET;
    693  1.102        ad 	if (shm_committed + btoc(size) > shminfo.shmall) {
    694  1.102        ad 		mutex_exit(&shm_lock);
    695   1.14   mycroft 		return ENOMEM;
    696  1.102        ad 	}
    697  1.102        ad 
    698  1.102        ad 	/* Find the first available segment */
    699   1.14   mycroft 	if (shm_last_free < 0) {
    700  1.102        ad 		for (segnum = 0; segnum < shminfo.shmmni; segnum++)
    701  1.102        ad 			if (shmsegs[segnum].shm_perm.mode & SHMSEG_FREE)
    702   1.14   mycroft 				break;
    703  1.102        ad 		KASSERT(segnum < shminfo.shmmni);
    704  1.102        ad 	} else {
    705   1.14   mycroft 		segnum = shm_last_free;
    706   1.14   mycroft 		shm_last_free = -1;
    707   1.14   mycroft 	}
    708  1.102        ad 
    709  1.102        ad 	/*
    710  1.102        ad 	 * Initialize the segment.
    711  1.102        ad 	 * We will drop the lock while allocating the memory, thus mark the
    712  1.102        ad 	 * segment present, but removed, that no other thread could take it.
    713  1.102        ad 	 * Also, disable reallocation, while lock is dropped.
    714  1.102        ad 	 */
    715   1.14   mycroft 	shmseg = &shmsegs[segnum];
    716  1.102        ad 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    717  1.102        ad 	shm_committed += btoc(size);
    718  1.102        ad 	shm_nused++;
    719  1.102        ad 	lockmem = shm_use_phys;
    720  1.102        ad 	shm_realloc_disable++;
    721  1.102        ad 	mutex_exit(&shm_lock);
    722  1.102        ad 
    723  1.102        ad 	/* Allocate the memory object and lock it if needed */
    724  1.102        ad 	shmseg->_shm_internal = uao_create(size, 0);
    725  1.102        ad 	if (lockmem) {
    726  1.102        ad 		/* Wire the pages and tag it */
    727  1.109     rmind 		error = uobj_wirepages(shmseg->_shm_internal, 0, size);
    728  1.102        ad 		if (error) {
    729  1.108     rmind 			uao_detach(shmseg->_shm_internal);
    730  1.102        ad 			mutex_enter(&shm_lock);
    731  1.102        ad 			shm_free_segment(segnum);
    732  1.102        ad 			shm_realloc_disable--;
    733  1.102        ad 			mutex_exit(&shm_lock);
    734  1.102        ad 			return error;
    735  1.102        ad 		}
    736  1.102        ad 	}
    737  1.102        ad 
    738   1.14   mycroft 	/*
    739  1.102        ad 	 * Please note, while segment is marked, there are no need to hold the
    740  1.102        ad 	 * lock, while setting it (except shm_perm.mode).
    741   1.14   mycroft 	 */
    742   1.52   thorpej 	shmseg->shm_perm._key = SCARG(uap, key);
    743   1.52   thorpej 	shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
    744  1.102        ad 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    745   1.42       mrg 
    746   1.87      elad 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = kauth_cred_geteuid(cred);
    747   1.87      elad 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = kauth_cred_getegid(cred);
    748   1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    749   1.89        ad 	shmseg->shm_cpid = l->l_proc->p_pid;
    750   1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    751   1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    752   1.88    kardel 	shmseg->shm_ctime = time_second;
    753   1.40  drochner 
    754  1.102        ad 	/*
    755  1.102        ad 	 * Segment is initialized.
    756  1.102        ad 	 * Enter the lock, mark as allocated, and notify waiters (if any).
    757  1.102        ad 	 * Also, unmark the state of reallocation.
    758  1.102        ad 	 */
    759  1.102        ad 	mutex_enter(&shm_lock);
    760  1.102        ad 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    761  1.102        ad 	    (mode & (ACCESSPERMS | SHMSEG_RMLINGER)) |
    762  1.102        ad 	    SHMSEG_ALLOCATED | (lockmem ? SHMSEG_WIRED : 0);
    763   1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    764   1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    765  1.102        ad 		cv_broadcast(&shm_cv[segnum]);
    766   1.92  christos 	}
    767  1.102        ad 	shm_realloc_disable--;
    768  1.102        ad 	cv_broadcast(&shm_realloc_cv);
    769  1.102        ad 	mutex_exit(&shm_lock);
    770   1.92  christos 
    771   1.40  drochner 	return error;
    772   1.14   mycroft }
    773   1.14   mycroft 
    774   1.12   mycroft void
    775   1.86   thorpej shmfork(struct vmspace *vm1, struct vmspace *vm2)
    776   1.11   hpeyerl {
    777   1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    778   1.69  drochner 	struct shmmap_entry *shmmap_se;
    779   1.69  drochner 
    780  1.102        ad 	SHMPRINTF(("shmfork %p->%p\n", vm1, vm2));
    781  1.102        ad 	mutex_enter(&shm_lock);
    782   1.69  drochner 	vm2->vm_shm = vm1->vm_shm;
    783  1.102        ad 	if (vm1->vm_shm) {
    784  1.102        ad 		shmmap_s = (struct shmmap_state *)vm1->vm_shm;
    785  1.102        ad 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    786  1.102        ad 			shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
    787  1.102        ad 		shmmap_s->nrefs++;
    788  1.102        ad 	}
    789  1.102        ad 	mutex_exit(&shm_lock);
    790   1.11   hpeyerl }
    791   1.11   hpeyerl 
    792   1.12   mycroft void
    793   1.86   thorpej shmexit(struct vmspace *vm)
    794   1.11   hpeyerl {
    795   1.12   mycroft 	struct shmmap_state *shmmap_s;
    796   1.69  drochner 	struct shmmap_entry *shmmap_se;
    797  1.102        ad 	struct uvm_object **uobj;
    798  1.102        ad 	size_t *size;
    799  1.102        ad 	u_int i, n;
    800   1.11   hpeyerl 
    801  1.102        ad 	SLIST_HEAD(, shmmap_entry) tmp_entries;
    802  1.102        ad 
    803  1.102        ad 	mutex_enter(&shm_lock);
    804   1.41   thorpej 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    805  1.102        ad 	if (shmmap_s == NULL) {
    806  1.102        ad 		mutex_exit(&shm_lock);
    807   1.38  christos 		return;
    808  1.102        ad 	}
    809   1.69  drochner 
    810   1.41   thorpej 	vm->vm_shm = NULL;
    811   1.69  drochner 
    812   1.69  drochner 	if (--shmmap_s->nrefs > 0) {
    813  1.102        ad 		SHMPRINTF(("shmexit: vm %p drop ref (%d entries), refs = %d\n",
    814  1.102        ad 		    vm, shmmap_s->nitems, shmmap_s->nrefs));
    815   1.69  drochner 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    816   1.69  drochner 			shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
    817  1.102        ad 		mutex_exit(&shm_lock);
    818  1.102        ad 		return;
    819  1.102        ad 	}
    820  1.102        ad 
    821  1.102        ad 	KASSERT(shmmap_s->nrefs == 0);
    822  1.102        ad 	n = shmmap_s->nitems;
    823  1.102        ad 	SHMPRINTF(("shmexit: vm %p cleanup (%d entries)\n", vm, n));
    824  1.102        ad 	mutex_exit(&shm_lock);
    825  1.102        ad 	if (n == 0) {
    826  1.102        ad 		kmem_free(shmmap_s, sizeof(struct shmmap_state));
    827   1.69  drochner 		return;
    828   1.69  drochner 	}
    829   1.69  drochner 
    830  1.102        ad 	/* Allocate the arrays */
    831  1.102        ad 	SLIST_INIT(&tmp_entries);
    832  1.102        ad 	uobj = kmem_zalloc(n * sizeof(void *), KM_SLEEP);
    833  1.102        ad 	size = kmem_zalloc(n * sizeof(size_t), KM_SLEEP);
    834  1.102        ad 
    835  1.102        ad 	/* Delete the entry from shm map */
    836  1.102        ad 	i = 0;
    837  1.102        ad 	mutex_enter(&shm_lock);
    838   1.69  drochner 	while (!SLIST_EMPTY(&shmmap_s->entries)) {
    839  1.102        ad 		struct shmid_ds *shmseg;
    840  1.102        ad 
    841   1.69  drochner 		shmmap_se = SLIST_FIRST(&shmmap_s->entries);
    842  1.102        ad 		shmseg = &shmsegs[IPCID_TO_IX(shmmap_se->shmid)];
    843  1.102        ad 		size[i] = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    844  1.102        ad 		uobj[i] = shm_delete_mapping(shmmap_s, shmmap_se);
    845  1.102        ad 		SLIST_INSERT_HEAD(&tmp_entries, shmmap_se, next);
    846  1.102        ad 		i++;
    847   1.69  drochner 	}
    848  1.102        ad 	mutex_exit(&shm_lock);
    849  1.102        ad 
    850  1.102        ad 	/* Unmap all segments, free the entries */
    851  1.102        ad 	i = 0;
    852  1.102        ad 	while (!SLIST_EMPTY(&tmp_entries)) {
    853  1.102        ad 		KASSERT(i < n);
    854  1.102        ad 		shmmap_se = SLIST_FIRST(&tmp_entries);
    855  1.102        ad 		SLIST_REMOVE(&tmp_entries, shmmap_se, shmmap_entry, next);
    856  1.102        ad 		uvm_deallocate(&vm->vm_map, shmmap_se->va, size[i]);
    857  1.102        ad 		if (uobj[i] != NULL)
    858  1.102        ad 			uao_detach(uobj[i]);
    859  1.102        ad 		pool_put(&shmmap_entry_pool, shmmap_se);
    860  1.102        ad 		i++;
    861  1.102        ad 	}
    862  1.102        ad 
    863  1.102        ad 	kmem_free(uobj, n * sizeof(void *));
    864  1.102        ad 	kmem_free(size, n * sizeof(size_t));
    865  1.102        ad 	kmem_free(shmmap_s, sizeof(struct shmmap_state));
    866   1.11   hpeyerl }
    867   1.11   hpeyerl 
    868   1.92  christos static int
    869   1.92  christos shmrealloc(int newshmni)
    870   1.92  christos {
    871   1.92  christos 	vaddr_t v;
    872  1.102        ad 	struct shmid_ds *oldshmsegs, *newshmsegs;
    873  1.102        ad 	kcondvar_t *newshm_cv;
    874  1.104     rmind 	size_t sz;
    875  1.104     rmind 	int i, lsegid;
    876   1.92  christos 
    877   1.92  christos 	if (newshmni < 1)
    878   1.92  christos 		return EINVAL;
    879   1.92  christos 
    880   1.92  christos 	/* Allocate new memory area */
    881  1.102        ad 	sz = ALIGN(newshmni * sizeof(struct shmid_ds)) +
    882  1.104     rmind 	    ALIGN(newshmni * sizeof(kcondvar_t));
    883  1.102        ad 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
    884  1.102        ad 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    885   1.92  christos 	if (v == 0)
    886   1.92  christos 		return ENOMEM;
    887   1.92  christos 
    888  1.102        ad 	mutex_enter(&shm_lock);
    889  1.102        ad 	while (shm_realloc_state || shm_realloc_disable)
    890  1.102        ad 		cv_wait(&shm_realloc_cv, &shm_lock);
    891  1.102        ad 
    892  1.102        ad 	/*
    893  1.102        ad 	 * Get the number of last segment.  Fail we are trying to
    894  1.102        ad 	 * reallocate less memory than we use.
    895  1.104     rmind 	 */
    896  1.102        ad 	lsegid = 0;
    897  1.102        ad 	for (i = 0; i < shminfo.shmmni; i++)
    898  1.102        ad 		if ((shmsegs[i].shm_perm.mode & SHMSEG_FREE) == 0)
    899  1.102        ad 			lsegid = i;
    900  1.102        ad 	if (lsegid >= newshmni) {
    901  1.102        ad 		mutex_exit(&shm_lock);
    902  1.102        ad 		uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
    903  1.102        ad 		return EBUSY;
    904  1.102        ad 	}
    905  1.102        ad 	shm_realloc_state = true;
    906  1.102        ad 
    907   1.92  christos 	newshmsegs = (void *)v;
    908  1.102        ad 	newshm_cv = (void *)(ALIGN(newshmsegs) +
    909  1.105     njoly 	    newshmni * sizeof(struct shmid_ds));
    910   1.92  christos 
    911   1.92  christos 	/* Copy all memory to the new area */
    912   1.92  christos 	for (i = 0; i < shm_nused; i++)
    913   1.92  christos 		(void)memcpy(&newshmsegs[i], &shmsegs[i],
    914   1.92  christos 		    sizeof(newshmsegs[0]));
    915   1.92  christos 
    916   1.92  christos 	/* Mark as free all new segments, if there is any */
    917   1.92  christos 	for (; i < newshmni; i++) {
    918  1.102        ad 		cv_init(&newshm_cv[i], "shmwait");
    919   1.92  christos 		newshmsegs[i].shm_perm.mode = SHMSEG_FREE;
    920   1.92  christos 		newshmsegs[i].shm_perm._seq = 0;
    921   1.92  christos 	}
    922   1.92  christos 
    923  1.102        ad 	oldshmsegs = shmsegs;
    924  1.102        ad 	sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) +
    925  1.102        ad 	    ALIGN(shminfo.shmmni * sizeof(kcondvar_t));
    926  1.102        ad 
    927  1.102        ad 	shminfo.shmmni = newshmni;
    928   1.92  christos 	shmsegs = newshmsegs;
    929  1.102        ad 	shm_cv = newshm_cv;
    930  1.102        ad 
    931  1.102        ad 	/* Reallocation completed - notify all waiters, if any */
    932  1.102        ad 	shm_realloc_state = false;
    933  1.102        ad 	cv_broadcast(&shm_realloc_cv);
    934  1.102        ad 	mutex_exit(&shm_lock);
    935   1.92  christos 
    936  1.102        ad 	uvm_km_free(kernel_map, (vaddr_t)oldshmsegs, sz, UVM_KMF_WIRED);
    937   1.92  christos 	return 0;
    938   1.92  christos }
    939   1.92  christos 
    940   1.12   mycroft void
    941   1.86   thorpej shminit(void)
    942   1.11   hpeyerl {
    943   1.71  jdolecek 	vaddr_t v;
    944  1.104     rmind 	size_t sz;
    945  1.104     rmind 	int i;
    946   1.71  jdolecek 
    947   1.96        ad 	mutex_init(&shm_lock, MUTEX_DEFAULT, IPL_NONE);
    948  1.102        ad 	pool_init(&shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
    949  1.102        ad 	    "shmmp", &pool_allocator_nointr, IPL_NONE);
    950  1.102        ad 	cv_init(&shm_realloc_cv, "shmrealc");
    951  1.102        ad 
    952  1.102        ad 	/* Allocate the wired memory for our structures */
    953  1.102        ad 	sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) +
    954  1.102        ad 	    ALIGN(shminfo.shmmni * sizeof(kcondvar_t));
    955  1.102        ad 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
    956  1.102        ad 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    957   1.84      yamt 	if (v == 0)
    958   1.71  jdolecek 		panic("sysv_shm: cannot allocate memory");
    959   1.71  jdolecek 	shmsegs = (void *)v;
    960  1.102        ad 	shm_cv = (void *)(ALIGN(shmsegs) +
    961  1.105     njoly 	    shminfo.shmmni * sizeof(struct shmid_ds));
    962   1.24   deraadt 
    963   1.60   thorpej 	shminfo.shmmax *= PAGE_SIZE;
    964   1.11   hpeyerl 
    965   1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    966  1.102        ad 		cv_init(&shm_cv[i], "shmwait");
    967   1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    968   1.52   thorpej 		shmsegs[i].shm_perm._seq = 0;
    969   1.11   hpeyerl 	}
    970   1.11   hpeyerl 	shm_last_free = 0;
    971   1.11   hpeyerl 	shm_nused = 0;
    972   1.11   hpeyerl 	shm_committed = 0;
    973  1.102        ad 	shm_realloc_disable = 0;
    974  1.102        ad 	shm_realloc_state = false;
    975   1.11   hpeyerl }
    976   1.92  christos 
    977   1.92  christos static int
    978   1.92  christos sysctl_ipc_shmmni(SYSCTLFN_ARGS)
    979   1.92  christos {
    980   1.92  christos 	int newsize, error;
    981   1.92  christos 	struct sysctlnode node;
    982   1.92  christos 	node = *rnode;
    983   1.92  christos 	node.sysctl_data = &newsize;
    984   1.92  christos 
    985   1.92  christos 	newsize = shminfo.shmmni;
    986   1.92  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    987   1.92  christos 	if (error || newp == NULL)
    988   1.92  christos 		return error;
    989   1.92  christos 
    990  1.103        ad 	sysctl_unlock();
    991  1.103        ad 	error = shmrealloc(newsize);
    992  1.103        ad 	sysctl_relock();
    993  1.103        ad 	return error;
    994   1.92  christos }
    995   1.92  christos 
    996   1.92  christos static int
    997   1.92  christos sysctl_ipc_shmmaxpgs(SYSCTLFN_ARGS)
    998   1.92  christos {
    999   1.92  christos 	int newsize, error;
   1000   1.92  christos 	struct sysctlnode node;
   1001   1.92  christos 	node = *rnode;
   1002   1.92  christos 	node.sysctl_data = &newsize;
   1003  1.102        ad 
   1004   1.92  christos 	newsize = shminfo.shmall;
   1005   1.92  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1006   1.92  christos 	if (error || newp == NULL)
   1007   1.92  christos 		return error;
   1008   1.92  christos 
   1009   1.92  christos 	if (newsize < 1)
   1010   1.92  christos 		return EINVAL;
   1011   1.92  christos 
   1012   1.92  christos 	shminfo.shmall = newsize;
   1013   1.92  christos 	shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
   1014   1.92  christos 
   1015   1.92  christos 	return 0;
   1016   1.92  christos }
   1017   1.92  christos 
   1018   1.92  christos SYSCTL_SETUP(sysctl_ipc_shm_setup, "sysctl kern.ipc subtree setup")
   1019   1.92  christos {
   1020  1.102        ad 
   1021   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1022   1.92  christos 		CTLFLAG_PERMANENT,
   1023   1.92  christos 		CTLTYPE_NODE, "kern", NULL,
   1024   1.92  christos 		NULL, 0, NULL, 0,
   1025   1.92  christos 		CTL_KERN, CTL_EOL);
   1026   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1027   1.92  christos 		CTLFLAG_PERMANENT,
   1028   1.92  christos 		CTLTYPE_NODE, "ipc",
   1029   1.92  christos 		SYSCTL_DESCR("SysV IPC options"),
   1030   1.92  christos 		NULL, 0, NULL, 0,
   1031   1.92  christos 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
   1032   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1033   1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
   1034   1.92  christos 		CTLTYPE_INT, "shmmax",
   1035   1.92  christos 		SYSCTL_DESCR("Max shared memory segment size in bytes"),
   1036   1.92  christos 		NULL, 0, &shminfo.shmmax, 0,
   1037   1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAX, CTL_EOL);
   1038   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1039   1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1040   1.92  christos 		CTLTYPE_INT, "shmmni",
   1041   1.92  christos 		SYSCTL_DESCR("Max number of shared memory identifiers"),
   1042   1.92  christos 		sysctl_ipc_shmmni, 0, &shminfo.shmmni, 0,
   1043   1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMNI, CTL_EOL);
   1044   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1045   1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1046   1.92  christos 		CTLTYPE_INT, "shmseg",
   1047   1.92  christos 		SYSCTL_DESCR("Max shared memory segments per process"),
   1048   1.92  christos 		NULL, 0, &shminfo.shmseg, 0,
   1049   1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMSEG, CTL_EOL);
   1050   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1051   1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1052   1.92  christos 		CTLTYPE_INT, "shmmaxpgs",
   1053   1.92  christos 		SYSCTL_DESCR("Max amount of shared memory in pages"),
   1054   1.92  christos 		sysctl_ipc_shmmaxpgs, 0, &shminfo.shmall, 0,
   1055   1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAXPGS, CTL_EOL);
   1056   1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
   1057   1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1058   1.92  christos 		CTLTYPE_INT, "shm_use_phys",
   1059   1.92  christos 		SYSCTL_DESCR("Enable/disable locking of shared memory in "
   1060   1.92  christos 		    "physical memory"), NULL, 0, &shm_use_phys, 0,
   1061   1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMUSEPHYS, CTL_EOL);
   1062   1.92  christos }
   1063