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