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