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