Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.96
      1  1.96        ad /*	$NetBSD: sysv_shm.c,v 1.96 2007/02/15 15:40:52 ad 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.52   thorpej  * NASA Ames Research Center.
     10  1.52   thorpej  *
     11  1.52   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.52   thorpej  * modification, are permitted provided that the following conditions
     13  1.52   thorpej  * are met:
     14  1.52   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.52   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.52   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.52   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.52   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.52   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.52   thorpej  *    must display the following acknowledgement:
     21  1.52   thorpej  *	This product includes software developed by the NetBSD
     22  1.52   thorpej  *	Foundation, Inc. and its contributors.
     23  1.52   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.52   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.52   thorpej  *    from this software without specific prior written permission.
     26  1.52   thorpej  *
     27  1.52   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.52   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.52   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.52   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.52   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.52   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.52   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.52   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.52   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.52   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.52   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.52   thorpej  */
     39  1.22       cgd 
     40  1.11   hpeyerl /*
     41  1.48   mycroft  * Copyright (c) 1994 Adam Glass and Charles M. Hannum.  All rights reserved.
     42  1.11   hpeyerl  *
     43  1.11   hpeyerl  * Redistribution and use in source and binary forms, with or without
     44  1.11   hpeyerl  * modification, are permitted provided that the following conditions
     45  1.11   hpeyerl  * are met:
     46  1.11   hpeyerl  * 1. Redistributions of source code must retain the above copyright
     47  1.11   hpeyerl  *    notice, this list of conditions and the following disclaimer.
     48  1.17   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.17   mycroft  *    notice, this list of conditions and the following disclaimer in the
     50  1.17   mycroft  *    documentation and/or other materials provided with the distribution.
     51  1.17   mycroft  * 3. All advertising materials mentioning features or use of this software
     52  1.17   mycroft  *    must display the following acknowledgement:
     53  1.48   mycroft  *	This product includes software developed by Adam Glass and Charles M.
     54  1.17   mycroft  *	Hannum.
     55  1.17   mycroft  * 4. The names of the authors may not be used to endorse or promote products
     56  1.11   hpeyerl  *    derived from this software without specific prior written permission.
     57  1.11   hpeyerl  *
     58  1.17   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     59  1.17   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  1.17   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  1.17   mycroft  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  1.17   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  1.17   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  1.17   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  1.17   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  1.17   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  1.17   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  1.11   hpeyerl  */
     69  1.62     lukem 
     70  1.62     lukem #include <sys/cdefs.h>
     71  1.96        ad __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.96 2007/02/15 15:40:52 ad Exp $");
     72  1.43       mrg 
     73  1.50      tron #define SYSVSHM
     74  1.11   hpeyerl 
     75  1.11   hpeyerl #include <sys/param.h>
     76  1.11   hpeyerl #include <sys/kernel.h>
     77  1.11   hpeyerl #include <sys/shm.h>
     78  1.96        ad #include <sys/mutex.h>
     79  1.11   hpeyerl #include <sys/malloc.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.87      elad #include <sys/kauth.h>
     88  1.35  christos 
     89  1.60   thorpej #include <uvm/uvm_extern.h>
     90  1.75  christos #include <uvm/uvm_object.h>
     91  1.60   thorpej 
     92  1.71  jdolecek static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
     93  1.35  christos 
     94  1.11   hpeyerl /*
     95  1.11   hpeyerl  * Provides the following externally accessible functions:
     96  1.11   hpeyerl  *
     97  1.41   thorpej  * shminit(void);		                 initialization
     98  1.41   thorpej  * shmexit(struct vmspace *)                     cleanup
     99  1.41   thorpej  * shmfork(struct vmspace *, struct vmspace *)   fork handling
    100  1.11   hpeyerl  *
    101  1.11   hpeyerl  * Structures:
    102  1.11   hpeyerl  * shmsegs (an array of 'struct shmid_ds')
    103  1.11   hpeyerl  * per proc array of 'struct shmmap_state'
    104  1.11   hpeyerl  */
    105  1.12   mycroft 
    106  1.85  christos int shm_nused;
    107  1.55    simonb struct	shmid_ds *shmsegs;
    108  1.11   hpeyerl 
    109  1.69  drochner struct shmmap_entry {
    110  1.69  drochner 	SLIST_ENTRY(shmmap_entry) next;
    111  1.47       eeh 	vaddr_t va;
    112  1.11   hpeyerl 	int shmid;
    113  1.11   hpeyerl };
    114  1.11   hpeyerl 
    115  1.96        ad static kmutex_t	shm_lock;
    116  1.92  christos static int	shm_last_free, shm_committed, shm_use_phys;
    117  1.85  christos 
    118  1.77    simonb static POOL_INIT(shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
    119  1.82  jdolecek     "shmmp", &pool_allocator_nointr);
    120  1.69  drochner 
    121  1.69  drochner struct shmmap_state {
    122  1.69  drochner 	unsigned int nitems;
    123  1.69  drochner 	unsigned int nrefs;
    124  1.69  drochner 	SLIST_HEAD(, shmmap_entry) entries;
    125  1.69  drochner };
    126  1.69  drochner 
    127  1.76  junyoung static int shm_find_segment_by_key(key_t);
    128  1.76  junyoung static void shm_deallocate_segment(struct shmid_ds *);
    129  1.76  junyoung static void shm_delete_mapping(struct vmspace *, struct shmmap_state *,
    130  1.76  junyoung 			       struct shmmap_entry *);
    131  1.89        ad static int shmget_existing(struct lwp *, struct sys_shmget_args *,
    132  1.76  junyoung 			   int, int, register_t *);
    133  1.89        ad static int shmget_allocate_segment(struct lwp *, struct sys_shmget_args *,
    134  1.76  junyoung 				   int, register_t *);
    135  1.76  junyoung static struct shmmap_state *shmmap_getprivate(struct proc *);
    136  1.76  junyoung static struct shmmap_entry *shm_find_mapping(struct shmmap_state *, vaddr_t);
    137  1.92  christos static int shmrealloc(int);
    138  1.11   hpeyerl 
    139  1.12   mycroft static int
    140  1.86   thorpej shm_find_segment_by_key(key_t key)
    141  1.11   hpeyerl {
    142  1.11   hpeyerl 	int i;
    143  1.11   hpeyerl 
    144  1.12   mycroft 	for (i = 0; i < shminfo.shmmni; i++)
    145  1.12   mycroft 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
    146  1.52   thorpej 		    shmsegs[i].shm_perm._key == key)
    147  1.12   mycroft 			return i;
    148  1.11   hpeyerl 	return -1;
    149  1.11   hpeyerl }
    150  1.11   hpeyerl 
    151  1.86   thorpej static struct shmid_ds *
    152  1.86   thorpej shm_find_segment_by_shmid(int shmid)
    153  1.11   hpeyerl {
    154  1.11   hpeyerl 	int segnum;
    155  1.11   hpeyerl 	struct shmid_ds *shmseg;
    156  1.11   hpeyerl 
    157  1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    158  1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    159  1.11   hpeyerl 		return NULL;
    160  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    161  1.64      fvdl 	if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
    162  1.64      fvdl 		return NULL;
    163  1.79  jdolecek 	if ((shmseg->shm_perm.mode & (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.11   hpeyerl 	return shmseg;
    168  1.11   hpeyerl }
    169  1.11   hpeyerl 
    170  1.12   mycroft static void
    171  1.86   thorpej shm_deallocate_segment(struct shmid_ds *shmseg)
    172  1.12   mycroft {
    173  1.80  jdolecek 	struct uvm_object *uobj = shmseg->_shm_internal;
    174  1.74  christos 	size_t size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    175  1.12   mycroft 
    176  1.85  christos #ifdef SHMDEBUG
    177  1.85  christos 	printf("shm freeing key 0x%lx seq 0x%x\n",
    178  1.85  christos 	       shmseg->shm_perm._key, shmseg->shm_perm._seq);
    179  1.85  christos #endif
    180  1.85  christos 
    181  1.74  christos 	(*uobj->pgops->pgo_detach)(uobj);
    182  1.52   thorpej 	shmseg->_shm_internal = NULL;
    183  1.14   mycroft 	shm_committed -= btoc(size);
    184  1.12   mycroft 	shmseg->shm_perm.mode = SHMSEG_FREE;
    185  1.25   mycroft 	shm_nused--;
    186  1.12   mycroft }
    187  1.12   mycroft 
    188  1.61       chs static void
    189  1.86   thorpej shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s,
    190  1.86   thorpej     struct shmmap_entry *shmmap_se)
    191  1.11   hpeyerl {
    192  1.12   mycroft 	struct shmid_ds *shmseg;
    193  1.61       chs 	int segnum;
    194  1.14   mycroft 	size_t size;
    195  1.76  junyoung 
    196  1.69  drochner 	segnum = IPCID_TO_IX(shmmap_se->shmid);
    197  1.72  jdolecek #ifdef DEBUG
    198  1.72  jdolecek 	if (segnum < 0 || segnum >= shminfo.shmmni)
    199  1.73    simonb 		panic("shm_delete_mapping: vmspace %p state %p entry %p - "
    200  1.73    simonb 		    "entry segment ID bad (%d)",
    201  1.73    simonb 		    vm, shmmap_s, shmmap_se, segnum);
    202  1.72  jdolecek #endif
    203  1.12   mycroft 	shmseg = &shmsegs[segnum];
    204  1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    205  1.69  drochner 	uvm_deallocate(&vm->vm_map, shmmap_se->va, size);
    206  1.69  drochner 	SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
    207  1.69  drochner 	shmmap_s->nitems--;
    208  1.69  drochner 	pool_put(&shmmap_entry_pool, shmmap_se);
    209  1.88    kardel 	shmseg->shm_dtime = time_second;
    210  1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    211  1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    212  1.12   mycroft 		shm_deallocate_segment(shmseg);
    213  1.12   mycroft 		shm_last_free = segnum;
    214  1.11   hpeyerl 	}
    215  1.11   hpeyerl }
    216  1.11   hpeyerl 
    217  1.69  drochner /*
    218  1.69  drochner  * Get a non-shared shm map for that vmspace.
    219  1.69  drochner  * 3 cases:
    220  1.69  drochner  *   - no shm map present: create a fresh one
    221  1.69  drochner  *   - a shm map with refcount=1, just used by ourselves: fine
    222  1.69  drochner  *   - a shared shm map: copy to a fresh one and adjust refcounts
    223  1.69  drochner  */
    224  1.69  drochner static struct shmmap_state *
    225  1.69  drochner shmmap_getprivate(struct proc *p)
    226  1.69  drochner {
    227  1.69  drochner 	struct shmmap_state *oshmmap_s, *shmmap_s;
    228  1.69  drochner 	struct shmmap_entry *oshmmap_se, *shmmap_se;
    229  1.69  drochner 
    230  1.69  drochner 	oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    231  1.69  drochner 	if (oshmmap_s && oshmmap_s->nrefs == 1)
    232  1.69  drochner 		return (oshmmap_s);
    233  1.69  drochner 
    234  1.69  drochner 	shmmap_s = malloc(sizeof(struct shmmap_state), M_SHM, M_WAITOK);
    235  1.69  drochner 	memset(shmmap_s, 0, sizeof(struct shmmap_state));
    236  1.69  drochner 	shmmap_s->nrefs = 1;
    237  1.69  drochner 	SLIST_INIT(&shmmap_s->entries);
    238  1.69  drochner 	p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    239  1.69  drochner 
    240  1.69  drochner 	if (!oshmmap_s)
    241  1.69  drochner 		return (shmmap_s);
    242  1.69  drochner 
    243  1.69  drochner #ifdef SHMDEBUG
    244  1.70  drochner 	printf("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
    245  1.69  drochner 	       p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs);
    246  1.69  drochner #endif
    247  1.69  drochner 	SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
    248  1.69  drochner 		shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
    249  1.69  drochner 		shmmap_se->va = oshmmap_se->va;
    250  1.69  drochner 		shmmap_se->shmid = oshmmap_se->shmid;
    251  1.69  drochner 		SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
    252  1.69  drochner 	}
    253  1.69  drochner 	shmmap_s->nitems = oshmmap_s->nitems;
    254  1.69  drochner 	oshmmap_s->nrefs--;
    255  1.69  drochner 	return (shmmap_s);
    256  1.69  drochner }
    257  1.69  drochner 
    258  1.70  drochner static struct shmmap_entry *
    259  1.86   thorpej shm_find_mapping(struct shmmap_state *map, vaddr_t va)
    260  1.70  drochner {
    261  1.70  drochner 	struct shmmap_entry *shmmap_se;
    262  1.70  drochner 
    263  1.70  drochner 	SLIST_FOREACH(shmmap_se, &map->entries, next) {
    264  1.70  drochner 		if (shmmap_se->va == va)
    265  1.70  drochner 			return shmmap_se;
    266  1.70  drochner 	}
    267  1.70  drochner 	return 0;
    268  1.70  drochner }
    269  1.70  drochner 
    270  1.12   mycroft int
    271  1.91      yamt sys_shmdt(struct lwp *l, void *v, register_t *retval)
    272  1.32   thorpej {
    273  1.33   mycroft 	struct sys_shmdt_args /* {
    274  1.44    kleink 		syscallarg(const void *) shmaddr;
    275  1.32   thorpej 	} */ *uap = v;
    276  1.65   thorpej 	struct proc *p = l->l_proc;
    277  1.70  drochner 	struct shmmap_state *shmmap_s, *shmmap_s1;
    278  1.69  drochner 	struct shmmap_entry *shmmap_se;
    279  1.11   hpeyerl 
    280  1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    281  1.38  christos 	if (shmmap_s == NULL)
    282  1.38  christos 		return EINVAL;
    283  1.38  christos 
    284  1.70  drochner 	shmmap_se = shm_find_mapping(shmmap_s, (vaddr_t)SCARG(uap, shmaddr));
    285  1.70  drochner 	if (!shmmap_se)
    286  1.70  drochner 		return EINVAL;
    287  1.70  drochner 
    288  1.70  drochner 	shmmap_s1 = shmmap_getprivate(p);
    289  1.70  drochner 	if (shmmap_s1 != shmmap_s) {
    290  1.70  drochner 		/* map has been copied, lookup entry in new map */
    291  1.70  drochner 		shmmap_se = shm_find_mapping(shmmap_s1,
    292  1.70  drochner 					     (vaddr_t)SCARG(uap, shmaddr));
    293  1.70  drochner 		KASSERT(shmmap_se != NULL);
    294  1.70  drochner 	}
    295  1.69  drochner #ifdef SHMDEBUG
    296  1.70  drochner 	printf("shmdt: vm %p: remove %d @%lx\n",
    297  1.70  drochner 	       p->p_vmspace, shmmap_se->shmid, shmmap_se->va);
    298  1.69  drochner #endif
    299  1.70  drochner 	shm_delete_mapping(p->p_vmspace, shmmap_s1, shmmap_se);
    300  1.70  drochner 	return 0;
    301  1.11   hpeyerl }
    302  1.11   hpeyerl 
    303  1.12   mycroft int
    304  1.86   thorpej sys_shmat(struct lwp *l, void *v, register_t *retval)
    305  1.32   thorpej {
    306  1.33   mycroft 	struct sys_shmat_args /* {
    307  1.26       cgd 		syscallarg(int) shmid;
    308  1.44    kleink 		syscallarg(const void *) shmaddr;
    309  1.35  christos 		syscallarg(int) shmflg;
    310  1.32   thorpej 	} */ *uap = v;
    311  1.94     rmind 	int error, flags = 0;
    312  1.65   thorpej 	struct proc *p = l->l_proc;
    313  1.89        ad 	kauth_cred_t cred = l->l_cred;
    314  1.11   hpeyerl 	struct shmid_ds *shmseg;
    315  1.69  drochner 	struct shmmap_state *shmmap_s;
    316  1.74  christos 	struct uvm_object *uobj;
    317  1.47       eeh 	vaddr_t attach_va;
    318  1.11   hpeyerl 	vm_prot_t prot;
    319  1.47       eeh 	vsize_t size;
    320  1.69  drochner 	struct shmmap_entry *shmmap_se;
    321  1.11   hpeyerl 
    322  1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    323  1.11   hpeyerl 	if (shmseg == NULL)
    324  1.11   hpeyerl 		return EINVAL;
    325  1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    326  1.78  jdolecek 		    (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    327  1.35  christos 	if (error)
    328  1.12   mycroft 		return error;
    329  1.69  drochner 
    330  1.69  drochner 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    331  1.69  drochner 	if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg)
    332  1.12   mycroft 		return EMFILE;
    333  1.69  drochner 
    334  1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    335  1.12   mycroft 	prot = VM_PROT_READ;
    336  1.78  jdolecek 	if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
    337  1.12   mycroft 		prot |= VM_PROT_WRITE;
    338  1.78  jdolecek 	if (SCARG(uap, shmaddr)) {
    339  1.94     rmind 		flags |= UVM_FLAG_FIXED;
    340  1.78  jdolecek 		if (SCARG(uap, shmflg) & SHM_RND)
    341  1.26       cgd 			attach_va =
    342  1.78  jdolecek 			    (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    343  1.78  jdolecek 		else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    344  1.78  jdolecek 			attach_va = (vaddr_t)SCARG(uap, shmaddr);
    345  1.11   hpeyerl 		else
    346  1.14   mycroft 			return EINVAL;
    347  1.12   mycroft 	} else {
    348  1.66    atatat 		/* This is just a hint to uvm_mmap() about where to put it. */
    349  1.83      fvdl 		attach_va = p->p_emul->e_vm_default_addr(p,
    350  1.83      fvdl 		    (vaddr_t)p->p_vmspace->vm_daddr, size);
    351  1.11   hpeyerl 	}
    352  1.80  jdolecek 	uobj = shmseg->_shm_internal;
    353  1.74  christos 	(*uobj->pgops->pgo_reference)(uobj);
    354  1.61       chs 	error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
    355  1.74  christos 	    uobj, 0, 0,
    356  1.94     rmind 	    UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
    357  1.92  christos 	if (error)
    358  1.92  christos 		goto out;
    359  1.92  christos 	/* Lock the memory */
    360  1.92  christos 	if (shm_use_phys || (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    361  1.92  christos 		/* Wire the map */
    362  1.92  christos 		error = uvm_map_pageable(&p->p_vmspace->vm_map, attach_va,
    363  1.92  christos 		    attach_va + size, FALSE, 0);
    364  1.92  christos 		if (error) {
    365  1.92  christos 			if (error == EFAULT)
    366  1.92  christos 				error = ENOMEM;
    367  1.92  christos 			goto out;
    368  1.92  christos 		}
    369  1.42       mrg 	}
    370  1.92  christos 
    371  1.69  drochner 	shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
    372  1.69  drochner 	shmmap_se->va = attach_va;
    373  1.78  jdolecek 	shmmap_se->shmid = SCARG(uap, shmid);
    374  1.69  drochner 	shmmap_s = shmmap_getprivate(p);
    375  1.69  drochner #ifdef SHMDEBUG
    376  1.85  christos 	printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, shmmap_se->shmid, attach_va);
    377  1.69  drochner #endif
    378  1.69  drochner 	SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
    379  1.69  drochner 	shmmap_s->nitems++;
    380  1.11   hpeyerl 	shmseg->shm_lpid = p->p_pid;
    381  1.88    kardel 	shmseg->shm_atime = time_second;
    382  1.11   hpeyerl 	shmseg->shm_nattch++;
    383  1.78  jdolecek 
    384  1.78  jdolecek 	retval[0] = attach_va;
    385  1.11   hpeyerl 	return 0;
    386  1.92  christos out:
    387  1.92  christos 	(*uobj->pgops->pgo_detach)(uobj);
    388  1.92  christos 	return error;
    389  1.11   hpeyerl }
    390  1.11   hpeyerl 
    391  1.12   mycroft int
    392  1.91      yamt sys___shmctl13(struct lwp *l, void *v, register_t *retval)
    393  1.32   thorpej {
    394  1.52   thorpej 	struct sys___shmctl13_args /* {
    395  1.26       cgd 		syscallarg(int) shmid;
    396  1.26       cgd 		syscallarg(int) cmd;
    397  1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    398  1.76  junyoung 	} */ *uap = v;
    399  1.52   thorpej 	struct shmid_ds shmbuf;
    400  1.52   thorpej 	int cmd, error;
    401  1.52   thorpej 
    402  1.52   thorpej 	cmd = SCARG(uap, cmd);
    403  1.52   thorpej 
    404  1.52   thorpej 	if (cmd == IPC_SET) {
    405  1.52   thorpej 		error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
    406  1.52   thorpej 		if (error)
    407  1.52   thorpej 			return (error);
    408  1.52   thorpej 	}
    409  1.52   thorpej 
    410  1.89        ad 	error = shmctl1(l, SCARG(uap, shmid), cmd,
    411  1.52   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
    412  1.52   thorpej 
    413  1.52   thorpej 	if (error == 0 && cmd == IPC_STAT)
    414  1.52   thorpej 		error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
    415  1.52   thorpej 
    416  1.52   thorpej 	return (error);
    417  1.52   thorpej }
    418  1.52   thorpej 
    419  1.52   thorpej int
    420  1.89        ad shmctl1(struct lwp *l, int shmid, int cmd, struct shmid_ds *shmbuf)
    421  1.52   thorpej {
    422  1.89        ad 	kauth_cred_t cred = l->l_cred;
    423  1.92  christos  	struct proc *p = l->l_proc;
    424  1.11   hpeyerl 	struct shmid_ds *shmseg;
    425  1.92  christos 	struct shmmap_entry *shmmap_se;
    426  1.92  christos 	struct shmmap_state *shmmap_s;
    427  1.52   thorpej 	int error = 0;
    428  1.92  christos 	size_t size;
    429  1.11   hpeyerl 
    430  1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(shmid);
    431  1.11   hpeyerl 	if (shmseg == NULL)
    432  1.11   hpeyerl 		return EINVAL;
    433  1.92  christos 
    434  1.52   thorpej 	switch (cmd) {
    435  1.11   hpeyerl 	case IPC_STAT:
    436  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    437  1.11   hpeyerl 			return error;
    438  1.52   thorpej 		memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
    439  1.11   hpeyerl 		break;
    440  1.11   hpeyerl 	case IPC_SET:
    441  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    442  1.13   mycroft 			return error;
    443  1.52   thorpej 		shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
    444  1.52   thorpej 		shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
    445  1.12   mycroft 		shmseg->shm_perm.mode =
    446  1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    447  1.52   thorpej 		    (shmbuf->shm_perm.mode & ACCESSPERMS);
    448  1.88    kardel 		shmseg->shm_ctime = time_second;
    449  1.11   hpeyerl 		break;
    450  1.11   hpeyerl 	case IPC_RMID:
    451  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    452  1.13   mycroft 			return error;
    453  1.52   thorpej 		shmseg->shm_perm._key = IPC_PRIVATE;
    454  1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    455  1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    456  1.12   mycroft 			shm_deallocate_segment(shmseg);
    457  1.52   thorpej 			shm_last_free = IPCID_TO_IX(shmid);
    458  1.11   hpeyerl 		}
    459  1.11   hpeyerl 		break;
    460  1.11   hpeyerl 	case SHM_LOCK:
    461  1.11   hpeyerl 	case SHM_UNLOCK:
    462  1.92  christos 		if ((error = kauth_authorize_generic(cred,
    463  1.92  christos 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
    464  1.92  christos 			return error;
    465  1.92  christos 		shmmap_s = shmmap_getprivate(p);
    466  1.92  christos 		/* Find our shared memory address by shmid */
    467  1.92  christos 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
    468  1.92  christos 			if (shmmap_se->shmid != shmid)
    469  1.92  christos 				continue;
    470  1.92  christos 
    471  1.92  christos 			size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    472  1.92  christos 
    473  1.92  christos 			if (cmd == SHM_LOCK &&
    474  1.92  christos 			    !(shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    475  1.92  christos 				/* Wire the entire object */
    476  1.92  christos 				error = uobj_wirepages(shmseg->_shm_internal, 0,
    477  1.92  christos 					round_page(shmseg->shm_segsz));
    478  1.92  christos 				if (error)
    479  1.92  christos 					return EIO;
    480  1.92  christos 				/* Wire the map */
    481  1.92  christos 				error = uvm_map_pageable(&p->p_vmspace->vm_map,
    482  1.92  christos 				    shmmap_se->va, shmmap_se->va + size, FALSE,
    483  1.92  christos 				    0);
    484  1.92  christos 				if (error) {
    485  1.92  christos 					uobj_unwirepages(shmseg->_shm_internal,
    486  1.92  christos 					    0, round_page(shmseg->shm_segsz));
    487  1.92  christos 					if (error == EFAULT)
    488  1.92  christos 						error = ENOMEM;
    489  1.92  christos 					return error;
    490  1.92  christos 				}
    491  1.92  christos 				/* Tag as wired */
    492  1.92  christos 				shmseg->shm_perm.mode |= SHMSEG_WIRED;
    493  1.92  christos 
    494  1.92  christos 			} else if (cmd == SHM_UNLOCK &&
    495  1.92  christos 			    (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    496  1.92  christos 				/* Unwire the object */
    497  1.92  christos 				uobj_unwirepages(shmseg->_shm_internal, 0,
    498  1.92  christos 				    round_page(shmseg->shm_segsz));
    499  1.92  christos 				error = uvm_map_pageable(&p->p_vmspace->vm_map,
    500  1.92  christos 				    shmmap_se->va, shmmap_se->va + size, TRUE,
    501  1.92  christos 				    0);
    502  1.92  christos 				if (error) {
    503  1.92  christos 					/*
    504  1.92  christos 					 * In fact, uvm_map_pageable could fail
    505  1.92  christos 					 * only if arguments are invalid,
    506  1.92  christos 					 * otherwise it should allways return 0.
    507  1.92  christos 					 */
    508  1.92  christos 					return EIO;
    509  1.92  christos 				}
    510  1.92  christos 				/* Tag as unwired */
    511  1.92  christos 				shmseg->shm_perm.mode &= ~SHMSEG_WIRED;
    512  1.92  christos 			}
    513  1.92  christos 		}
    514  1.92  christos 		break;
    515  1.11   hpeyerl 	default:
    516  1.11   hpeyerl 		return EINVAL;
    517  1.11   hpeyerl 	}
    518  1.11   hpeyerl 	return 0;
    519  1.11   hpeyerl }
    520  1.11   hpeyerl 
    521  1.12   mycroft static int
    522  1.89        ad shmget_existing(struct lwp *l, struct sys_shmget_args *uap, int mode,
    523  1.86   thorpej     int segnum, register_t *retval)
    524  1.11   hpeyerl {
    525  1.12   mycroft 	struct shmid_ds *shmseg;
    526  1.89        ad 	kauth_cred_t cred = l->l_cred;
    527  1.11   hpeyerl 	int error;
    528  1.11   hpeyerl 
    529  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    530  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    531  1.16   mycroft 		/*
    532  1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    533  1.16   mycroft 		 * until it's done, and look the key up again (in case the
    534  1.16   mycroft 		 * allocation failed or it was freed).
    535  1.16   mycroft 		 */
    536  1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    537  1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    538  1.35  christos 		if (error)
    539  1.16   mycroft 			return error;
    540  1.16   mycroft 		return EAGAIN;
    541  1.16   mycroft 	}
    542  1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    543  1.11   hpeyerl 		return error;
    544  1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    545  1.11   hpeyerl 		return EINVAL;
    546  1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    547  1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    548  1.12   mycroft 		return EEXIST;
    549  1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    550  1.11   hpeyerl 	return 0;
    551  1.11   hpeyerl }
    552  1.11   hpeyerl 
    553  1.14   mycroft static int
    554  1.89        ad shmget_allocate_segment(struct lwp *l, struct sys_shmget_args *uap, int mode,
    555  1.86   thorpej     register_t *retval)
    556  1.14   mycroft {
    557  1.39  drochner 	int i, segnum, shmid, size;
    558  1.89        ad 	kauth_cred_t cred = l->l_cred;
    559  1.14   mycroft 	struct shmid_ds *shmseg;
    560  1.40  drochner 	int error = 0;
    561  1.76  junyoung 
    562  1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    563  1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    564  1.14   mycroft 		return EINVAL;
    565  1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    566  1.14   mycroft 		return ENOSPC;
    567  1.53     ragge 	size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
    568  1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    569  1.14   mycroft 		return ENOMEM;
    570  1.14   mycroft 	if (shm_last_free < 0) {
    571  1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    572  1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    573  1.14   mycroft 				break;
    574  1.14   mycroft 		if (i == shminfo.shmmni)
    575  1.14   mycroft 			panic("shmseg free count inconsistent");
    576  1.14   mycroft 		segnum = i;
    577  1.14   mycroft 	} else  {
    578  1.14   mycroft 		segnum = shm_last_free;
    579  1.14   mycroft 		shm_last_free = -1;
    580  1.14   mycroft 	}
    581  1.14   mycroft 	shmseg = &shmsegs[segnum];
    582  1.14   mycroft 	/*
    583  1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    584  1.14   mycroft 	 * so that noone else tries to create the same key.
    585  1.14   mycroft 	 */
    586  1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    587  1.52   thorpej 	shmseg->shm_perm._key = SCARG(uap, key);
    588  1.52   thorpej 	shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
    589  1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    590  1.42       mrg 
    591  1.80  jdolecek 	shmseg->_shm_internal = uao_create(size, 0);
    592  1.42       mrg 
    593  1.87      elad 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = kauth_cred_geteuid(cred);
    594  1.87      elad 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = kauth_cred_getegid(cred);
    595  1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    596  1.79  jdolecek 	    (mode & (ACCESSPERMS|SHMSEG_RMLINGER)) | SHMSEG_ALLOCATED;
    597  1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    598  1.89        ad 	shmseg->shm_cpid = l->l_proc->p_pid;
    599  1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    600  1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    601  1.88    kardel 	shmseg->shm_ctime = time_second;
    602  1.14   mycroft 	shm_committed += btoc(size);
    603  1.14   mycroft 	shm_nused++;
    604  1.40  drochner 
    605  1.51       mrg 	*retval = shmid;
    606  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    607  1.16   mycroft 		/*
    608  1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    609  1.16   mycroft 		 * them up now.
    610  1.16   mycroft 		 */
    611  1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    612  1.16   mycroft 		wakeup((caddr_t)shmseg);
    613  1.16   mycroft 	}
    614  1.92  christos 
    615  1.92  christos 	/* Lock the memory */
    616  1.92  christos 	if (shm_use_phys) {
    617  1.92  christos 		/* Wire the entire object */
    618  1.92  christos 		error = uobj_wirepages(shmseg->_shm_internal, 0,
    619  1.92  christos 		    round_page(shmseg->shm_segsz));
    620  1.92  christos 		if (error) {
    621  1.92  christos 			shm_deallocate_segment(shmseg);
    622  1.92  christos 		} else {
    623  1.92  christos 			/* Tag as wired */
    624  1.92  christos 			shmseg->shm_perm.mode |= SHMSEG_WIRED;
    625  1.92  christos 		}
    626  1.92  christos 	}
    627  1.92  christos 
    628  1.40  drochner 	return error;
    629  1.14   mycroft }
    630  1.14   mycroft 
    631  1.12   mycroft int
    632  1.86   thorpej sys_shmget(struct lwp *l, void *v, register_t *retval)
    633  1.32   thorpej {
    634  1.33   mycroft 	struct sys_shmget_args /* {
    635  1.26       cgd 		syscallarg(key_t) key;
    636  1.26       cgd 		syscallarg(int) size;
    637  1.35  christos 		syscallarg(int) shmflg;
    638  1.32   thorpej 	} */ *uap = v;
    639  1.11   hpeyerl 	int segnum, mode, error;
    640  1.11   hpeyerl 
    641  1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    642  1.79  jdolecek 	if (SCARG(uap, shmflg) & _SHM_RMLINGER)
    643  1.79  jdolecek 		mode |= SHMSEG_RMLINGER;
    644  1.79  jdolecek 
    645  1.85  christos #ifdef SHMDEBUG
    646  1.85  christos 	printf("shmget: key 0x%lx size 0x%x shmflg 0x%x mode 0x%x\n",
    647  1.85  christos         	SCARG(uap, key), SCARG(uap, size), SCARG(uap, shmflg), mode);
    648  1.85  christos #endif
    649  1.85  christos 
    650  1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    651  1.85  christos again:
    652  1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    653  1.16   mycroft 		if (segnum >= 0) {
    654  1.89        ad 			error = shmget_existing(l, uap, mode, segnum, retval);
    655  1.16   mycroft 			if (error == EAGAIN)
    656  1.16   mycroft 				goto again;
    657  1.16   mycroft 			return error;
    658  1.16   mycroft 		}
    659  1.76  junyoung 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    660  1.14   mycroft 			return ENOENT;
    661  1.11   hpeyerl 	}
    662  1.89        ad 	return shmget_allocate_segment(l, uap, mode, retval);
    663  1.11   hpeyerl }
    664  1.11   hpeyerl 
    665  1.12   mycroft void
    666  1.86   thorpej shmfork(struct vmspace *vm1, struct vmspace *vm2)
    667  1.11   hpeyerl {
    668  1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    669  1.69  drochner 	struct shmmap_entry *shmmap_se;
    670  1.69  drochner 
    671  1.69  drochner 	vm2->vm_shm = vm1->vm_shm;
    672  1.11   hpeyerl 
    673  1.69  drochner 	if (vm1->vm_shm == NULL)
    674  1.38  christos 		return;
    675  1.41   thorpej 
    676  1.69  drochner #ifdef SHMDEBUG
    677  1.69  drochner 	printf("shmfork %p->%p\n", vm1, vm2);
    678  1.69  drochner #endif
    679  1.69  drochner 
    680  1.69  drochner 	shmmap_s = (struct shmmap_state *)vm1->vm_shm;
    681  1.69  drochner 
    682  1.69  drochner 	SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    683  1.69  drochner 		shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
    684  1.69  drochner 	shmmap_s->nrefs++;
    685  1.11   hpeyerl }
    686  1.11   hpeyerl 
    687  1.12   mycroft void
    688  1.86   thorpej shmexit(struct vmspace *vm)
    689  1.11   hpeyerl {
    690  1.12   mycroft 	struct shmmap_state *shmmap_s;
    691  1.69  drochner 	struct shmmap_entry *shmmap_se;
    692  1.11   hpeyerl 
    693  1.41   thorpej 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    694  1.38  christos 	if (shmmap_s == NULL)
    695  1.38  christos 		return;
    696  1.69  drochner 
    697  1.41   thorpej 	vm->vm_shm = NULL;
    698  1.69  drochner 
    699  1.69  drochner 	if (--shmmap_s->nrefs > 0) {
    700  1.69  drochner #ifdef SHMDEBUG
    701  1.70  drochner 		printf("shmexit: vm %p drop ref (%d entries), now used by %d\n",
    702  1.69  drochner 		       vm, shmmap_s->nitems, shmmap_s->nrefs);
    703  1.69  drochner #endif
    704  1.69  drochner 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    705  1.69  drochner 			shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
    706  1.69  drochner 		return;
    707  1.69  drochner 	}
    708  1.69  drochner 
    709  1.69  drochner #ifdef SHMDEBUG
    710  1.69  drochner 	printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems);
    711  1.69  drochner #endif
    712  1.69  drochner 	while (!SLIST_EMPTY(&shmmap_s->entries)) {
    713  1.69  drochner 		shmmap_se = SLIST_FIRST(&shmmap_s->entries);
    714  1.69  drochner 		shm_delete_mapping(vm, shmmap_s, shmmap_se);
    715  1.69  drochner 	}
    716  1.69  drochner 	KASSERT(shmmap_s->nitems == 0);
    717  1.69  drochner 	free(shmmap_s, M_SHM);
    718  1.11   hpeyerl }
    719  1.11   hpeyerl 
    720  1.92  christos static int
    721  1.92  christos shmrealloc(int newshmni)
    722  1.92  christos {
    723  1.92  christos 	int i, sz;
    724  1.92  christos 	vaddr_t v;
    725  1.92  christos 	struct shmid_ds *newshmsegs;
    726  1.92  christos 
    727  1.92  christos 	/* XXX: Would be good to have a upper limit */
    728  1.92  christos 	if (newshmni < 1)
    729  1.92  christos 		return EINVAL;
    730  1.92  christos 
    731  1.94     rmind 	/* We can't reallocate less memory than we use */
    732  1.92  christos 	if (shm_nused > newshmni)
    733  1.92  christos 		return EPERM;
    734  1.92  christos 
    735  1.92  christos 	/* Allocate new memory area */
    736  1.92  christos 	sz = newshmni * sizeof(struct shmid_ds);
    737  1.92  christos 	v = uvm_km_alloc(kernel_map, round_page(sz), 0, UVM_KMF_WIRED);
    738  1.92  christos 	if (v == 0)
    739  1.92  christos 		return ENOMEM;
    740  1.92  christos 
    741  1.92  christos 	newshmsegs = (void *)v;
    742  1.92  christos 
    743  1.92  christos 	/* Copy all memory to the new area */
    744  1.92  christos 	for (i = 0; i < shm_nused; i++)
    745  1.92  christos 		(void)memcpy(&newshmsegs[i], &shmsegs[i],
    746  1.92  christos 		    sizeof(newshmsegs[0]));
    747  1.92  christos 
    748  1.92  christos 	/* Mark as free all new segments, if there is any */
    749  1.92  christos 	for (; i < newshmni; i++) {
    750  1.92  christos 		newshmsegs[i].shm_perm.mode = SHMSEG_FREE;
    751  1.92  christos 		newshmsegs[i].shm_perm._seq = 0;
    752  1.92  christos 	}
    753  1.92  christos 
    754  1.92  christos 	sz = shminfo.shmmni * sizeof(struct shmid_ds);
    755  1.92  christos 	uvm_km_free(kernel_map, (vaddr_t)shmsegs, sz, UVM_KMF_WIRED);
    756  1.92  christos 	shmsegs = newshmsegs;
    757  1.92  christos 
    758  1.92  christos 	return 0;
    759  1.92  christos }
    760  1.92  christos 
    761  1.12   mycroft void
    762  1.86   thorpej shminit(void)
    763  1.11   hpeyerl {
    764  1.71  jdolecek 	int i, sz;
    765  1.71  jdolecek 	vaddr_t v;
    766  1.71  jdolecek 
    767  1.96        ad 	mutex_init(&shm_lock, MUTEX_DEFAULT, IPL_NONE);
    768  1.92  christos 
    769  1.71  jdolecek 	/* Allocate pageable memory for our structures */
    770  1.71  jdolecek 	sz = shminfo.shmmni * sizeof(struct shmid_ds);
    771  1.84      yamt 	v = uvm_km_alloc(kernel_map, round_page(sz), 0, UVM_KMF_WIRED);
    772  1.84      yamt 	if (v == 0)
    773  1.71  jdolecek 		panic("sysv_shm: cannot allocate memory");
    774  1.71  jdolecek 	shmsegs = (void *)v;
    775  1.24   deraadt 
    776  1.60   thorpej 	shminfo.shmmax *= PAGE_SIZE;
    777  1.11   hpeyerl 
    778  1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    779  1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    780  1.52   thorpej 		shmsegs[i].shm_perm._seq = 0;
    781  1.11   hpeyerl 	}
    782  1.11   hpeyerl 	shm_last_free = 0;
    783  1.11   hpeyerl 	shm_nused = 0;
    784  1.11   hpeyerl 	shm_committed = 0;
    785  1.11   hpeyerl }
    786  1.92  christos 
    787  1.92  christos static int
    788  1.92  christos sysctl_ipc_shmmni(SYSCTLFN_ARGS)
    789  1.92  christos {
    790  1.92  christos 	int newsize, error;
    791  1.92  christos 	struct sysctlnode node;
    792  1.92  christos 	node = *rnode;
    793  1.92  christos 	node.sysctl_data = &newsize;
    794  1.92  christos 
    795  1.92  christos 	newsize = shminfo.shmmni;
    796  1.92  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    797  1.92  christos 	if (error || newp == NULL)
    798  1.92  christos 		return error;
    799  1.92  christos 
    800  1.96        ad 	mutex_enter(&shm_lock);
    801  1.92  christos 	error = shmrealloc(newsize);
    802  1.92  christos 	if (error == 0)
    803  1.92  christos 		shminfo.shmmni = newsize;
    804  1.96        ad 	mutex_exit(&shm_lock);
    805  1.92  christos 
    806  1.92  christos 	return error;
    807  1.92  christos }
    808  1.92  christos 
    809  1.92  christos static int
    810  1.92  christos sysctl_ipc_shmmaxpgs(SYSCTLFN_ARGS)
    811  1.92  christos {
    812  1.92  christos 	int newsize, error;
    813  1.92  christos 	struct sysctlnode node;
    814  1.92  christos 	node = *rnode;
    815  1.92  christos 	node.sysctl_data = &newsize;
    816  1.92  christos 	newsize = shminfo.shmall;
    817  1.92  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    818  1.92  christos 	if (error || newp == NULL)
    819  1.92  christos 		return error;
    820  1.92  christos 
    821  1.92  christos 	/* XXX: Would be good to have a upper limit */
    822  1.92  christos 	if (newsize < 1)
    823  1.92  christos 		return EINVAL;
    824  1.92  christos 
    825  1.92  christos 	shminfo.shmall = newsize;
    826  1.92  christos 	shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
    827  1.92  christos 
    828  1.92  christos 	return 0;
    829  1.92  christos }
    830  1.92  christos 
    831  1.92  christos SYSCTL_SETUP(sysctl_ipc_shm_setup, "sysctl kern.ipc subtree setup")
    832  1.92  christos {
    833  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    834  1.92  christos 		CTLFLAG_PERMANENT,
    835  1.92  christos 		CTLTYPE_NODE, "kern", NULL,
    836  1.92  christos 		NULL, 0, NULL, 0,
    837  1.92  christos 		CTL_KERN, CTL_EOL);
    838  1.92  christos 
    839  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    840  1.92  christos 		CTLFLAG_PERMANENT,
    841  1.92  christos 		CTLTYPE_NODE, "ipc",
    842  1.92  christos 		SYSCTL_DESCR("SysV IPC options"),
    843  1.92  christos 		NULL, 0, NULL, 0,
    844  1.92  christos 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    845  1.92  christos 
    846  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    847  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
    848  1.92  christos 		CTLTYPE_INT, "shmmax",
    849  1.92  christos 		SYSCTL_DESCR("Max shared memory segment size in bytes"),
    850  1.92  christos 		NULL, 0, &shminfo.shmmax, 0,
    851  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAX, CTL_EOL);
    852  1.92  christos 
    853  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    854  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    855  1.92  christos 		CTLTYPE_INT, "shmmni",
    856  1.92  christos 		SYSCTL_DESCR("Max number of shared memory identifiers"),
    857  1.92  christos 		sysctl_ipc_shmmni, 0, &shminfo.shmmni, 0,
    858  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMNI, CTL_EOL);
    859  1.92  christos 
    860  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    861  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    862  1.92  christos 		CTLTYPE_INT, "shmseg",
    863  1.92  christos 		SYSCTL_DESCR("Max shared memory segments per process"),
    864  1.92  christos 		NULL, 0, &shminfo.shmseg, 0,
    865  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMSEG, CTL_EOL);
    866  1.92  christos 
    867  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    868  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    869  1.92  christos 		CTLTYPE_INT, "shmmaxpgs",
    870  1.92  christos 		SYSCTL_DESCR("Max amount of shared memory in pages"),
    871  1.92  christos 		sysctl_ipc_shmmaxpgs, 0, &shminfo.shmall, 0,
    872  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAXPGS, CTL_EOL);
    873  1.92  christos 
    874  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    875  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    876  1.92  christos 		CTLTYPE_INT, "shm_use_phys",
    877  1.92  christos 		SYSCTL_DESCR("Enable/disable locking of shared memory in "
    878  1.92  christos 		    "physical memory"), NULL, 0, &shm_use_phys, 0,
    879  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMUSEPHYS, CTL_EOL);
    880  1.92  christos }
    881