Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.94
      1  1.94     rmind /*	$NetBSD: sysv_shm.c,v 1.94 2007/02/07 18:45:36 rmind 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.94     rmind __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.94 2007/02/07 18:45:36 rmind 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.93        ad struct lock	shm_lock;
    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.94     rmind 	int error, flags = 0;
    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.78  jdolecek 	if (SCARG(uap, shmaddr)) {
    340  1.94     rmind 		flags |= UVM_FLAG_FIXED;
    341  1.78  jdolecek 		if (SCARG(uap, shmflg) & SHM_RND)
    342  1.26       cgd 			attach_va =
    343  1.78  jdolecek 			    (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    344  1.78  jdolecek 		else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    345  1.78  jdolecek 			attach_va = (vaddr_t)SCARG(uap, shmaddr);
    346  1.11   hpeyerl 		else
    347  1.14   mycroft 			return EINVAL;
    348  1.12   mycroft 	} else {
    349  1.66    atatat 		/* This is just a hint to uvm_mmap() about where to put it. */
    350  1.83      fvdl 		attach_va = p->p_emul->e_vm_default_addr(p,
    351  1.83      fvdl 		    (vaddr_t)p->p_vmspace->vm_daddr, size);
    352  1.11   hpeyerl 	}
    353  1.80  jdolecek 	uobj = shmseg->_shm_internal;
    354  1.74  christos 	(*uobj->pgops->pgo_reference)(uobj);
    355  1.61       chs 	error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
    356  1.74  christos 	    uobj, 0, 0,
    357  1.94     rmind 	    UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
    358  1.92  christos 	if (error)
    359  1.92  christos 		goto out;
    360  1.92  christos 	/* Lock the memory */
    361  1.92  christos 	if (shm_use_phys || (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    362  1.92  christos 		/* Wire the map */
    363  1.92  christos 		error = uvm_map_pageable(&p->p_vmspace->vm_map, attach_va,
    364  1.92  christos 		    attach_va + size, FALSE, 0);
    365  1.92  christos 		if (error) {
    366  1.92  christos 			if (error == EFAULT)
    367  1.92  christos 				error = ENOMEM;
    368  1.92  christos 			goto out;
    369  1.92  christos 		}
    370  1.42       mrg 	}
    371  1.92  christos 
    372  1.69  drochner 	shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
    373  1.69  drochner 	shmmap_se->va = attach_va;
    374  1.78  jdolecek 	shmmap_se->shmid = SCARG(uap, shmid);
    375  1.69  drochner 	shmmap_s = shmmap_getprivate(p);
    376  1.69  drochner #ifdef SHMDEBUG
    377  1.85  christos 	printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, shmmap_se->shmid, attach_va);
    378  1.69  drochner #endif
    379  1.69  drochner 	SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
    380  1.69  drochner 	shmmap_s->nitems++;
    381  1.11   hpeyerl 	shmseg->shm_lpid = p->p_pid;
    382  1.88    kardel 	shmseg->shm_atime = time_second;
    383  1.11   hpeyerl 	shmseg->shm_nattch++;
    384  1.78  jdolecek 
    385  1.78  jdolecek 	retval[0] = attach_va;
    386  1.11   hpeyerl 	return 0;
    387  1.92  christos out:
    388  1.92  christos 	(*uobj->pgops->pgo_detach)(uobj);
    389  1.92  christos 	return error;
    390  1.11   hpeyerl }
    391  1.11   hpeyerl 
    392  1.12   mycroft int
    393  1.91      yamt sys___shmctl13(struct lwp *l, void *v, register_t *retval)
    394  1.32   thorpej {
    395  1.52   thorpej 	struct sys___shmctl13_args /* {
    396  1.26       cgd 		syscallarg(int) shmid;
    397  1.26       cgd 		syscallarg(int) cmd;
    398  1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    399  1.76  junyoung 	} */ *uap = v;
    400  1.52   thorpej 	struct shmid_ds shmbuf;
    401  1.52   thorpej 	int cmd, error;
    402  1.52   thorpej 
    403  1.52   thorpej 	cmd = SCARG(uap, cmd);
    404  1.52   thorpej 
    405  1.52   thorpej 	if (cmd == IPC_SET) {
    406  1.52   thorpej 		error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
    407  1.52   thorpej 		if (error)
    408  1.52   thorpej 			return (error);
    409  1.52   thorpej 	}
    410  1.52   thorpej 
    411  1.89        ad 	error = shmctl1(l, SCARG(uap, shmid), cmd,
    412  1.52   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
    413  1.52   thorpej 
    414  1.52   thorpej 	if (error == 0 && cmd == IPC_STAT)
    415  1.52   thorpej 		error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
    416  1.52   thorpej 
    417  1.52   thorpej 	return (error);
    418  1.52   thorpej }
    419  1.52   thorpej 
    420  1.52   thorpej int
    421  1.89        ad shmctl1(struct lwp *l, int shmid, int cmd, struct shmid_ds *shmbuf)
    422  1.52   thorpej {
    423  1.89        ad 	kauth_cred_t cred = l->l_cred;
    424  1.92  christos  	struct proc *p = l->l_proc;
    425  1.11   hpeyerl 	struct shmid_ds *shmseg;
    426  1.92  christos 	struct shmmap_entry *shmmap_se;
    427  1.92  christos 	struct shmmap_state *shmmap_s;
    428  1.52   thorpej 	int error = 0;
    429  1.92  christos 	size_t size;
    430  1.11   hpeyerl 
    431  1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(shmid);
    432  1.11   hpeyerl 	if (shmseg == NULL)
    433  1.11   hpeyerl 		return EINVAL;
    434  1.92  christos 
    435  1.52   thorpej 	switch (cmd) {
    436  1.11   hpeyerl 	case IPC_STAT:
    437  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    438  1.11   hpeyerl 			return error;
    439  1.52   thorpej 		memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
    440  1.11   hpeyerl 		break;
    441  1.11   hpeyerl 	case IPC_SET:
    442  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    443  1.13   mycroft 			return error;
    444  1.52   thorpej 		shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
    445  1.52   thorpej 		shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
    446  1.12   mycroft 		shmseg->shm_perm.mode =
    447  1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    448  1.52   thorpej 		    (shmbuf->shm_perm.mode & ACCESSPERMS);
    449  1.88    kardel 		shmseg->shm_ctime = time_second;
    450  1.11   hpeyerl 		break;
    451  1.11   hpeyerl 	case IPC_RMID:
    452  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    453  1.13   mycroft 			return error;
    454  1.52   thorpej 		shmseg->shm_perm._key = IPC_PRIVATE;
    455  1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    456  1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    457  1.12   mycroft 			shm_deallocate_segment(shmseg);
    458  1.52   thorpej 			shm_last_free = IPCID_TO_IX(shmid);
    459  1.11   hpeyerl 		}
    460  1.11   hpeyerl 		break;
    461  1.11   hpeyerl 	case SHM_LOCK:
    462  1.11   hpeyerl 	case SHM_UNLOCK:
    463  1.92  christos 		if ((error = kauth_authorize_generic(cred,
    464  1.92  christos 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
    465  1.92  christos 			return error;
    466  1.92  christos 		shmmap_s = shmmap_getprivate(p);
    467  1.92  christos 		/* Find our shared memory address by shmid */
    468  1.92  christos 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
    469  1.92  christos 			if (shmmap_se->shmid != shmid)
    470  1.92  christos 				continue;
    471  1.92  christos 
    472  1.92  christos 			size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    473  1.92  christos 
    474  1.92  christos 			if (cmd == SHM_LOCK &&
    475  1.92  christos 			    !(shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    476  1.92  christos 				/* Wire the entire object */
    477  1.92  christos 				error = uobj_wirepages(shmseg->_shm_internal, 0,
    478  1.92  christos 					round_page(shmseg->shm_segsz));
    479  1.92  christos 				if (error)
    480  1.92  christos 					return EIO;
    481  1.92  christos 				/* Wire the map */
    482  1.92  christos 				error = uvm_map_pageable(&p->p_vmspace->vm_map,
    483  1.92  christos 				    shmmap_se->va, shmmap_se->va + size, FALSE,
    484  1.92  christos 				    0);
    485  1.92  christos 				if (error) {
    486  1.92  christos 					uobj_unwirepages(shmseg->_shm_internal,
    487  1.92  christos 					    0, round_page(shmseg->shm_segsz));
    488  1.92  christos 					if (error == EFAULT)
    489  1.92  christos 						error = ENOMEM;
    490  1.92  christos 					return error;
    491  1.92  christos 				}
    492  1.92  christos 				/* Tag as wired */
    493  1.92  christos 				shmseg->shm_perm.mode |= SHMSEG_WIRED;
    494  1.92  christos 
    495  1.92  christos 			} else if (cmd == SHM_UNLOCK &&
    496  1.92  christos 			    (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
    497  1.92  christos 				/* Unwire the object */
    498  1.92  christos 				uobj_unwirepages(shmseg->_shm_internal, 0,
    499  1.92  christos 				    round_page(shmseg->shm_segsz));
    500  1.92  christos 				error = uvm_map_pageable(&p->p_vmspace->vm_map,
    501  1.92  christos 				    shmmap_se->va, shmmap_se->va + size, TRUE,
    502  1.92  christos 				    0);
    503  1.92  christos 				if (error) {
    504  1.92  christos 					/*
    505  1.92  christos 					 * In fact, uvm_map_pageable could fail
    506  1.92  christos 					 * only if arguments are invalid,
    507  1.92  christos 					 * otherwise it should allways return 0.
    508  1.92  christos 					 */
    509  1.92  christos 					return EIO;
    510  1.92  christos 				}
    511  1.92  christos 				/* Tag as unwired */
    512  1.92  christos 				shmseg->shm_perm.mode &= ~SHMSEG_WIRED;
    513  1.92  christos 			}
    514  1.92  christos 		}
    515  1.92  christos 		break;
    516  1.11   hpeyerl 	default:
    517  1.11   hpeyerl 		return EINVAL;
    518  1.11   hpeyerl 	}
    519  1.11   hpeyerl 	return 0;
    520  1.11   hpeyerl }
    521  1.11   hpeyerl 
    522  1.12   mycroft static int
    523  1.89        ad shmget_existing(struct lwp *l, struct sys_shmget_args *uap, int mode,
    524  1.86   thorpej     int segnum, register_t *retval)
    525  1.11   hpeyerl {
    526  1.12   mycroft 	struct shmid_ds *shmseg;
    527  1.89        ad 	kauth_cred_t cred = l->l_cred;
    528  1.11   hpeyerl 	int error;
    529  1.11   hpeyerl 
    530  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    531  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    532  1.16   mycroft 		/*
    533  1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    534  1.16   mycroft 		 * until it's done, and look the key up again (in case the
    535  1.16   mycroft 		 * allocation failed or it was freed).
    536  1.16   mycroft 		 */
    537  1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    538  1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    539  1.35  christos 		if (error)
    540  1.16   mycroft 			return error;
    541  1.16   mycroft 		return EAGAIN;
    542  1.16   mycroft 	}
    543  1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    544  1.11   hpeyerl 		return error;
    545  1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    546  1.11   hpeyerl 		return EINVAL;
    547  1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    548  1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    549  1.12   mycroft 		return EEXIST;
    550  1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    551  1.11   hpeyerl 	return 0;
    552  1.11   hpeyerl }
    553  1.11   hpeyerl 
    554  1.14   mycroft static int
    555  1.89        ad shmget_allocate_segment(struct lwp *l, struct sys_shmget_args *uap, int mode,
    556  1.86   thorpej     register_t *retval)
    557  1.14   mycroft {
    558  1.39  drochner 	int i, segnum, shmid, size;
    559  1.89        ad 	kauth_cred_t cred = l->l_cred;
    560  1.14   mycroft 	struct shmid_ds *shmseg;
    561  1.40  drochner 	int error = 0;
    562  1.76  junyoung 
    563  1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    564  1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    565  1.14   mycroft 		return EINVAL;
    566  1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    567  1.14   mycroft 		return ENOSPC;
    568  1.53     ragge 	size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
    569  1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    570  1.14   mycroft 		return ENOMEM;
    571  1.14   mycroft 	if (shm_last_free < 0) {
    572  1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    573  1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    574  1.14   mycroft 				break;
    575  1.14   mycroft 		if (i == shminfo.shmmni)
    576  1.14   mycroft 			panic("shmseg free count inconsistent");
    577  1.14   mycroft 		segnum = i;
    578  1.14   mycroft 	} else  {
    579  1.14   mycroft 		segnum = shm_last_free;
    580  1.14   mycroft 		shm_last_free = -1;
    581  1.14   mycroft 	}
    582  1.14   mycroft 	shmseg = &shmsegs[segnum];
    583  1.14   mycroft 	/*
    584  1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    585  1.14   mycroft 	 * so that noone else tries to create the same key.
    586  1.14   mycroft 	 */
    587  1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    588  1.52   thorpej 	shmseg->shm_perm._key = SCARG(uap, key);
    589  1.52   thorpej 	shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
    590  1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    591  1.42       mrg 
    592  1.80  jdolecek 	shmseg->_shm_internal = uao_create(size, 0);
    593  1.42       mrg 
    594  1.87      elad 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = kauth_cred_geteuid(cred);
    595  1.87      elad 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = kauth_cred_getegid(cred);
    596  1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    597  1.79  jdolecek 	    (mode & (ACCESSPERMS|SHMSEG_RMLINGER)) | SHMSEG_ALLOCATED;
    598  1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    599  1.89        ad 	shmseg->shm_cpid = l->l_proc->p_pid;
    600  1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    601  1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    602  1.88    kardel 	shmseg->shm_ctime = time_second;
    603  1.14   mycroft 	shm_committed += btoc(size);
    604  1.14   mycroft 	shm_nused++;
    605  1.40  drochner 
    606  1.51       mrg 	*retval = shmid;
    607  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    608  1.16   mycroft 		/*
    609  1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    610  1.16   mycroft 		 * them up now.
    611  1.16   mycroft 		 */
    612  1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    613  1.16   mycroft 		wakeup((caddr_t)shmseg);
    614  1.16   mycroft 	}
    615  1.92  christos 
    616  1.92  christos 	/* Lock the memory */
    617  1.92  christos 	if (shm_use_phys) {
    618  1.92  christos 		/* Wire the entire object */
    619  1.92  christos 		error = uobj_wirepages(shmseg->_shm_internal, 0,
    620  1.92  christos 		    round_page(shmseg->shm_segsz));
    621  1.92  christos 		if (error) {
    622  1.92  christos 			shm_deallocate_segment(shmseg);
    623  1.92  christos 		} else {
    624  1.92  christos 			/* Tag as wired */
    625  1.92  christos 			shmseg->shm_perm.mode |= SHMSEG_WIRED;
    626  1.92  christos 		}
    627  1.92  christos 	}
    628  1.92  christos 
    629  1.40  drochner 	return error;
    630  1.14   mycroft }
    631  1.14   mycroft 
    632  1.12   mycroft int
    633  1.86   thorpej sys_shmget(struct lwp *l, void *v, register_t *retval)
    634  1.32   thorpej {
    635  1.33   mycroft 	struct sys_shmget_args /* {
    636  1.26       cgd 		syscallarg(key_t) key;
    637  1.26       cgd 		syscallarg(int) size;
    638  1.35  christos 		syscallarg(int) shmflg;
    639  1.32   thorpej 	} */ *uap = v;
    640  1.11   hpeyerl 	int segnum, mode, error;
    641  1.11   hpeyerl 
    642  1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    643  1.79  jdolecek 	if (SCARG(uap, shmflg) & _SHM_RMLINGER)
    644  1.79  jdolecek 		mode |= SHMSEG_RMLINGER;
    645  1.79  jdolecek 
    646  1.85  christos #ifdef SHMDEBUG
    647  1.85  christos 	printf("shmget: key 0x%lx size 0x%x shmflg 0x%x mode 0x%x\n",
    648  1.85  christos         	SCARG(uap, key), SCARG(uap, size), SCARG(uap, shmflg), mode);
    649  1.85  christos #endif
    650  1.85  christos 
    651  1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    652  1.85  christos again:
    653  1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    654  1.16   mycroft 		if (segnum >= 0) {
    655  1.89        ad 			error = shmget_existing(l, uap, mode, segnum, retval);
    656  1.16   mycroft 			if (error == EAGAIN)
    657  1.16   mycroft 				goto again;
    658  1.16   mycroft 			return error;
    659  1.16   mycroft 		}
    660  1.76  junyoung 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    661  1.14   mycroft 			return ENOENT;
    662  1.11   hpeyerl 	}
    663  1.89        ad 	return shmget_allocate_segment(l, uap, mode, retval);
    664  1.11   hpeyerl }
    665  1.11   hpeyerl 
    666  1.12   mycroft void
    667  1.86   thorpej shmfork(struct vmspace *vm1, struct vmspace *vm2)
    668  1.11   hpeyerl {
    669  1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    670  1.69  drochner 	struct shmmap_entry *shmmap_se;
    671  1.69  drochner 
    672  1.69  drochner 	vm2->vm_shm = vm1->vm_shm;
    673  1.11   hpeyerl 
    674  1.69  drochner 	if (vm1->vm_shm == NULL)
    675  1.38  christos 		return;
    676  1.41   thorpej 
    677  1.69  drochner #ifdef SHMDEBUG
    678  1.69  drochner 	printf("shmfork %p->%p\n", vm1, vm2);
    679  1.69  drochner #endif
    680  1.69  drochner 
    681  1.69  drochner 	shmmap_s = (struct shmmap_state *)vm1->vm_shm;
    682  1.69  drochner 
    683  1.69  drochner 	SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    684  1.69  drochner 		shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
    685  1.69  drochner 	shmmap_s->nrefs++;
    686  1.11   hpeyerl }
    687  1.11   hpeyerl 
    688  1.12   mycroft void
    689  1.86   thorpej shmexit(struct vmspace *vm)
    690  1.11   hpeyerl {
    691  1.12   mycroft 	struct shmmap_state *shmmap_s;
    692  1.69  drochner 	struct shmmap_entry *shmmap_se;
    693  1.11   hpeyerl 
    694  1.41   thorpej 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    695  1.38  christos 	if (shmmap_s == NULL)
    696  1.38  christos 		return;
    697  1.69  drochner 
    698  1.41   thorpej 	vm->vm_shm = NULL;
    699  1.69  drochner 
    700  1.69  drochner 	if (--shmmap_s->nrefs > 0) {
    701  1.69  drochner #ifdef SHMDEBUG
    702  1.70  drochner 		printf("shmexit: vm %p drop ref (%d entries), now used by %d\n",
    703  1.69  drochner 		       vm, shmmap_s->nitems, shmmap_s->nrefs);
    704  1.69  drochner #endif
    705  1.69  drochner 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    706  1.69  drochner 			shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
    707  1.69  drochner 		return;
    708  1.69  drochner 	}
    709  1.69  drochner 
    710  1.69  drochner #ifdef SHMDEBUG
    711  1.69  drochner 	printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems);
    712  1.69  drochner #endif
    713  1.69  drochner 	while (!SLIST_EMPTY(&shmmap_s->entries)) {
    714  1.69  drochner 		shmmap_se = SLIST_FIRST(&shmmap_s->entries);
    715  1.69  drochner 		shm_delete_mapping(vm, shmmap_s, shmmap_se);
    716  1.69  drochner 	}
    717  1.69  drochner 	KASSERT(shmmap_s->nitems == 0);
    718  1.69  drochner 	free(shmmap_s, M_SHM);
    719  1.11   hpeyerl }
    720  1.11   hpeyerl 
    721  1.92  christos static int
    722  1.92  christos shmrealloc(int newshmni)
    723  1.92  christos {
    724  1.92  christos 	int i, sz;
    725  1.92  christos 	vaddr_t v;
    726  1.92  christos 	struct shmid_ds *newshmsegs;
    727  1.92  christos 
    728  1.92  christos 	/* XXX: Would be good to have a upper limit */
    729  1.92  christos 	if (newshmni < 1)
    730  1.92  christos 		return EINVAL;
    731  1.92  christos 
    732  1.94     rmind 	/* We can't reallocate less memory than we use */
    733  1.92  christos 	if (shm_nused > newshmni)
    734  1.92  christos 		return EPERM;
    735  1.92  christos 
    736  1.92  christos 	/* Allocate new memory area */
    737  1.92  christos 	sz = newshmni * sizeof(struct shmid_ds);
    738  1.92  christos 	v = uvm_km_alloc(kernel_map, round_page(sz), 0, UVM_KMF_WIRED);
    739  1.92  christos 	if (v == 0)
    740  1.92  christos 		return ENOMEM;
    741  1.92  christos 
    742  1.92  christos 	newshmsegs = (void *)v;
    743  1.92  christos 
    744  1.92  christos 	/* Copy all memory to the new area */
    745  1.92  christos 	for (i = 0; i < shm_nused; i++)
    746  1.92  christos 		(void)memcpy(&newshmsegs[i], &shmsegs[i],
    747  1.92  christos 		    sizeof(newshmsegs[0]));
    748  1.92  christos 
    749  1.92  christos 	/* Mark as free all new segments, if there is any */
    750  1.92  christos 	for (; i < newshmni; i++) {
    751  1.92  christos 		newshmsegs[i].shm_perm.mode = SHMSEG_FREE;
    752  1.92  christos 		newshmsegs[i].shm_perm._seq = 0;
    753  1.92  christos 	}
    754  1.92  christos 
    755  1.92  christos 	sz = shminfo.shmmni * sizeof(struct shmid_ds);
    756  1.92  christos 	uvm_km_free(kernel_map, (vaddr_t)shmsegs, sz, UVM_KMF_WIRED);
    757  1.92  christos 	shmsegs = newshmsegs;
    758  1.92  christos 
    759  1.92  christos 	return 0;
    760  1.92  christos }
    761  1.92  christos 
    762  1.12   mycroft void
    763  1.86   thorpej shminit(void)
    764  1.11   hpeyerl {
    765  1.71  jdolecek 	int i, sz;
    766  1.71  jdolecek 	vaddr_t v;
    767  1.71  jdolecek 
    768  1.93        ad 	lockinit(&shm_lock, PWAIT, "shmlk", 0, 0);
    769  1.92  christos 
    770  1.71  jdolecek 	/* Allocate pageable memory for our structures */
    771  1.71  jdolecek 	sz = shminfo.shmmni * sizeof(struct shmid_ds);
    772  1.84      yamt 	v = uvm_km_alloc(kernel_map, round_page(sz), 0, UVM_KMF_WIRED);
    773  1.84      yamt 	if (v == 0)
    774  1.71  jdolecek 		panic("sysv_shm: cannot allocate memory");
    775  1.71  jdolecek 	shmsegs = (void *)v;
    776  1.24   deraadt 
    777  1.60   thorpej 	shminfo.shmmax *= PAGE_SIZE;
    778  1.11   hpeyerl 
    779  1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    780  1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    781  1.52   thorpej 		shmsegs[i].shm_perm._seq = 0;
    782  1.11   hpeyerl 	}
    783  1.11   hpeyerl 	shm_last_free = 0;
    784  1.11   hpeyerl 	shm_nused = 0;
    785  1.11   hpeyerl 	shm_committed = 0;
    786  1.11   hpeyerl }
    787  1.92  christos 
    788  1.92  christos static int
    789  1.92  christos sysctl_ipc_shmmni(SYSCTLFN_ARGS)
    790  1.92  christos {
    791  1.92  christos 	int newsize, error;
    792  1.92  christos 	struct sysctlnode node;
    793  1.92  christos 	node = *rnode;
    794  1.92  christos 	node.sysctl_data = &newsize;
    795  1.92  christos 
    796  1.92  christos 	newsize = shminfo.shmmni;
    797  1.92  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    798  1.92  christos 	if (error || newp == NULL)
    799  1.92  christos 		return error;
    800  1.92  christos 
    801  1.93        ad 	lockmgr(&shm_lock, LK_EXCLUSIVE, NULL);
    802  1.92  christos 	error = shmrealloc(newsize);
    803  1.92  christos 	if (error == 0)
    804  1.92  christos 		shminfo.shmmni = newsize;
    805  1.93        ad 	lockmgr(&shm_lock, LK_RELEASE, NULL);
    806  1.92  christos 
    807  1.92  christos 	return error;
    808  1.92  christos }
    809  1.92  christos 
    810  1.92  christos static int
    811  1.92  christos sysctl_ipc_shmmaxpgs(SYSCTLFN_ARGS)
    812  1.92  christos {
    813  1.92  christos 	int newsize, error;
    814  1.92  christos 	struct sysctlnode node;
    815  1.92  christos 	node = *rnode;
    816  1.92  christos 	node.sysctl_data = &newsize;
    817  1.92  christos 	newsize = shminfo.shmall;
    818  1.92  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    819  1.92  christos 	if (error || newp == NULL)
    820  1.92  christos 		return error;
    821  1.92  christos 
    822  1.92  christos 	/* XXX: Would be good to have a upper limit */
    823  1.92  christos 	if (newsize < 1)
    824  1.92  christos 		return EINVAL;
    825  1.92  christos 
    826  1.92  christos 	shminfo.shmall = newsize;
    827  1.92  christos 	shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
    828  1.92  christos 
    829  1.92  christos 	return 0;
    830  1.92  christos }
    831  1.92  christos 
    832  1.92  christos SYSCTL_SETUP(sysctl_ipc_shm_setup, "sysctl kern.ipc subtree setup")
    833  1.92  christos {
    834  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    835  1.92  christos 		CTLFLAG_PERMANENT,
    836  1.92  christos 		CTLTYPE_NODE, "kern", NULL,
    837  1.92  christos 		NULL, 0, NULL, 0,
    838  1.92  christos 		CTL_KERN, CTL_EOL);
    839  1.92  christos 
    840  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    841  1.92  christos 		CTLFLAG_PERMANENT,
    842  1.92  christos 		CTLTYPE_NODE, "ipc",
    843  1.92  christos 		SYSCTL_DESCR("SysV IPC options"),
    844  1.92  christos 		NULL, 0, NULL, 0,
    845  1.92  christos 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    846  1.92  christos 
    847  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    848  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
    849  1.92  christos 		CTLTYPE_INT, "shmmax",
    850  1.92  christos 		SYSCTL_DESCR("Max shared memory segment size in bytes"),
    851  1.92  christos 		NULL, 0, &shminfo.shmmax, 0,
    852  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAX, CTL_EOL);
    853  1.92  christos 
    854  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    855  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    856  1.92  christos 		CTLTYPE_INT, "shmmni",
    857  1.92  christos 		SYSCTL_DESCR("Max number of shared memory identifiers"),
    858  1.92  christos 		sysctl_ipc_shmmni, 0, &shminfo.shmmni, 0,
    859  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMNI, CTL_EOL);
    860  1.92  christos 
    861  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    862  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    863  1.92  christos 		CTLTYPE_INT, "shmseg",
    864  1.92  christos 		SYSCTL_DESCR("Max shared memory segments per process"),
    865  1.92  christos 		NULL, 0, &shminfo.shmseg, 0,
    866  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMSEG, CTL_EOL);
    867  1.92  christos 
    868  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    869  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    870  1.92  christos 		CTLTYPE_INT, "shmmaxpgs",
    871  1.92  christos 		SYSCTL_DESCR("Max amount of shared memory in pages"),
    872  1.92  christos 		sysctl_ipc_shmmaxpgs, 0, &shminfo.shmall, 0,
    873  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAXPGS, CTL_EOL);
    874  1.92  christos 
    875  1.92  christos 	sysctl_createv(clog, 0, NULL, NULL,
    876  1.92  christos 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    877  1.92  christos 		CTLTYPE_INT, "shm_use_phys",
    878  1.92  christos 		SYSCTL_DESCR("Enable/disable locking of shared memory in "
    879  1.92  christos 		    "physical memory"), NULL, 0, &shm_use_phys, 0,
    880  1.92  christos 		CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMUSEPHYS, CTL_EOL);
    881  1.92  christos }
    882