Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.82.10.1
      1  1.82.10.1      tron /*	$NetBSD: sysv_shm.c,v 1.82.10.1 2005/09/18 20:09:50 tron 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.82.10.1      tron __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.82.10.1 2005/09/18 20:09:50 tron 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.11   hpeyerl #include <sys/malloc.h>
     79       1.11   hpeyerl #include <sys/mman.h>
     80       1.12   mycroft #include <sys/stat.h>
     81       1.56    simonb #include <sys/sysctl.h>
     82       1.56    simonb #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     83       1.65   thorpej #include <sys/sa.h>
     84       1.56    simonb #include <sys/syscallargs.h>
     85       1.69  drochner #include <sys/queue.h>
     86       1.69  drochner #include <sys/pool.h>
     87       1.35  christos 
     88       1.60   thorpej #include <uvm/uvm_extern.h>
     89       1.75  christos #include <uvm/uvm_object.h>
     90       1.60   thorpej 
     91       1.78  jdolecek struct shmid_ds *shm_find_segment_by_shmid(int);
     92       1.67   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.16   mycroft #define	SHMSEG_FREE     	0x0200
    108       1.16   mycroft #define	SHMSEG_REMOVED  	0x0400
    109       1.16   mycroft #define	SHMSEG_ALLOCATED	0x0800
    110       1.16   mycroft #define	SHMSEG_WANTED		0x1000
    111       1.79  jdolecek #define	SHMSEG_RMLINGER		0x2000
    112       1.11   hpeyerl 
    113       1.71  jdolecek static int	shm_last_free, shm_nused, shm_committed;
    114       1.55    simonb struct	shmid_ds *shmsegs;
    115       1.11   hpeyerl 
    116       1.69  drochner struct shmmap_entry {
    117       1.69  drochner 	SLIST_ENTRY(shmmap_entry) next;
    118       1.47       eeh 	vaddr_t va;
    119       1.11   hpeyerl 	int shmid;
    120       1.11   hpeyerl };
    121       1.11   hpeyerl 
    122       1.77    simonb static POOL_INIT(shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
    123       1.82  jdolecek     "shmmp", &pool_allocator_nointr);
    124       1.69  drochner 
    125       1.69  drochner struct shmmap_state {
    126       1.69  drochner 	unsigned int nitems;
    127       1.69  drochner 	unsigned int nrefs;
    128       1.69  drochner 	SLIST_HEAD(, shmmap_entry) entries;
    129       1.69  drochner };
    130       1.69  drochner 
    131       1.76  junyoung static int shm_find_segment_by_key(key_t);
    132       1.76  junyoung static void shm_deallocate_segment(struct shmid_ds *);
    133       1.76  junyoung static void shm_delete_mapping(struct vmspace *, struct shmmap_state *,
    134       1.76  junyoung 			       struct shmmap_entry *);
    135       1.76  junyoung static int shmget_existing(struct proc *, struct sys_shmget_args *,
    136       1.76  junyoung 			   int, int, register_t *);
    137       1.76  junyoung static int shmget_allocate_segment(struct proc *, struct sys_shmget_args *,
    138       1.76  junyoung 				   int, register_t *);
    139       1.76  junyoung static struct shmmap_state *shmmap_getprivate(struct proc *);
    140       1.76  junyoung static struct shmmap_entry *shm_find_mapping(struct shmmap_state *, vaddr_t);
    141       1.11   hpeyerl 
    142       1.12   mycroft static int
    143       1.12   mycroft shm_find_segment_by_key(key)
    144       1.11   hpeyerl 	key_t key;
    145       1.11   hpeyerl {
    146       1.11   hpeyerl 	int i;
    147       1.11   hpeyerl 
    148       1.12   mycroft 	for (i = 0; i < shminfo.shmmni; i++)
    149       1.12   mycroft 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
    150       1.52   thorpej 		    shmsegs[i].shm_perm._key == key)
    151       1.12   mycroft 			return i;
    152       1.11   hpeyerl 	return -1;
    153       1.11   hpeyerl }
    154       1.11   hpeyerl 
    155       1.28  christos struct shmid_ds *
    156       1.78  jdolecek shm_find_segment_by_shmid(shmid)
    157       1.11   hpeyerl 	int shmid;
    158       1.11   hpeyerl {
    159       1.11   hpeyerl 	int segnum;
    160       1.11   hpeyerl 	struct shmid_ds *shmseg;
    161       1.11   hpeyerl 
    162       1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    163       1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    164       1.11   hpeyerl 		return NULL;
    165       1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    166       1.64      fvdl 	if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
    167       1.64      fvdl 		return NULL;
    168       1.79  jdolecek 	if ((shmseg->shm_perm.mode & (SHMSEG_REMOVED|SHMSEG_RMLINGER)) == SHMSEG_REMOVED)
    169       1.64      fvdl 		return NULL;
    170       1.64      fvdl 	if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
    171       1.11   hpeyerl 		return NULL;
    172       1.11   hpeyerl 	return shmseg;
    173       1.11   hpeyerl }
    174       1.11   hpeyerl 
    175       1.12   mycroft static void
    176       1.12   mycroft shm_deallocate_segment(shmseg)
    177       1.12   mycroft 	struct shmid_ds *shmseg;
    178       1.12   mycroft {
    179       1.80  jdolecek 	struct uvm_object *uobj = shmseg->_shm_internal;
    180       1.74  christos 	size_t size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    181       1.12   mycroft 
    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.69  drochner shm_delete_mapping(vm, shmmap_s, shmmap_se)
    191       1.41   thorpej 	struct vmspace *vm;
    192       1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    193       1.69  drochner 	struct shmmap_entry *shmmap_se;
    194       1.11   hpeyerl {
    195       1.12   mycroft 	struct shmid_ds *shmseg;
    196       1.61       chs 	int segnum;
    197       1.14   mycroft 	size_t size;
    198       1.76  junyoung 
    199       1.69  drochner 	segnum = IPCID_TO_IX(shmmap_se->shmid);
    200       1.72  jdolecek #ifdef DEBUG
    201       1.72  jdolecek 	if (segnum < 0 || segnum >= shminfo.shmmni)
    202       1.73    simonb 		panic("shm_delete_mapping: vmspace %p state %p entry %p - "
    203       1.73    simonb 		    "entry segment ID bad (%d)",
    204       1.73    simonb 		    vm, shmmap_s, shmmap_se, segnum);
    205       1.72  jdolecek #endif
    206       1.12   mycroft 	shmseg = &shmsegs[segnum];
    207       1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    208       1.69  drochner 	uvm_deallocate(&vm->vm_map, shmmap_se->va, size);
    209       1.69  drochner 	SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
    210       1.69  drochner 	shmmap_s->nitems--;
    211       1.69  drochner 	pool_put(&shmmap_entry_pool, shmmap_se);
    212       1.11   hpeyerl 	shmseg->shm_dtime = time.tv_sec;
    213       1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    214       1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    215       1.12   mycroft 		shm_deallocate_segment(shmseg);
    216       1.12   mycroft 		shm_last_free = segnum;
    217       1.11   hpeyerl 	}
    218       1.11   hpeyerl }
    219       1.11   hpeyerl 
    220       1.69  drochner /*
    221       1.69  drochner  * Get a non-shared shm map for that vmspace.
    222       1.69  drochner  * 3 cases:
    223       1.69  drochner  *   - no shm map present: create a fresh one
    224       1.69  drochner  *   - a shm map with refcount=1, just used by ourselves: fine
    225       1.69  drochner  *   - a shared shm map: copy to a fresh one and adjust refcounts
    226       1.69  drochner  */
    227       1.69  drochner static struct shmmap_state *
    228       1.69  drochner shmmap_getprivate(struct proc *p)
    229       1.69  drochner {
    230       1.69  drochner 	struct shmmap_state *oshmmap_s, *shmmap_s;
    231       1.69  drochner 	struct shmmap_entry *oshmmap_se, *shmmap_se;
    232       1.69  drochner 
    233       1.69  drochner 	oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    234       1.69  drochner 	if (oshmmap_s && oshmmap_s->nrefs == 1)
    235       1.69  drochner 		return (oshmmap_s);
    236       1.69  drochner 
    237       1.69  drochner 	shmmap_s = malloc(sizeof(struct shmmap_state), M_SHM, M_WAITOK);
    238       1.69  drochner 	memset(shmmap_s, 0, sizeof(struct shmmap_state));
    239       1.69  drochner 	shmmap_s->nrefs = 1;
    240       1.69  drochner 	SLIST_INIT(&shmmap_s->entries);
    241       1.69  drochner 	p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    242       1.69  drochner 
    243       1.69  drochner 	if (!oshmmap_s)
    244       1.69  drochner 		return (shmmap_s);
    245       1.69  drochner 
    246       1.69  drochner #ifdef SHMDEBUG
    247       1.70  drochner 	printf("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
    248       1.69  drochner 	       p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs);
    249       1.69  drochner #endif
    250       1.69  drochner 	SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
    251       1.69  drochner 		shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
    252       1.69  drochner 		shmmap_se->va = oshmmap_se->va;
    253       1.69  drochner 		shmmap_se->shmid = oshmmap_se->shmid;
    254       1.69  drochner 		SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
    255       1.69  drochner 	}
    256       1.69  drochner 	shmmap_s->nitems = oshmmap_s->nitems;
    257       1.69  drochner 	oshmmap_s->nrefs--;
    258       1.69  drochner 	return (shmmap_s);
    259       1.69  drochner }
    260       1.69  drochner 
    261       1.70  drochner static struct shmmap_entry *
    262       1.70  drochner shm_find_mapping(map, va)
    263       1.70  drochner 	struct shmmap_state *map;
    264       1.70  drochner 	vaddr_t va;
    265       1.70  drochner {
    266       1.70  drochner 	struct shmmap_entry *shmmap_se;
    267       1.70  drochner 
    268       1.70  drochner 	SLIST_FOREACH(shmmap_se, &map->entries, next) {
    269       1.70  drochner 		if (shmmap_se->va == va)
    270       1.70  drochner 			return shmmap_se;
    271       1.70  drochner 	}
    272       1.70  drochner 	return 0;
    273       1.70  drochner }
    274       1.70  drochner 
    275       1.12   mycroft int
    276       1.65   thorpej sys_shmdt(l, v, retval)
    277       1.65   thorpej 	struct lwp *l;
    278       1.32   thorpej 	void *v;
    279       1.32   thorpej 	register_t *retval;
    280       1.32   thorpej {
    281       1.33   mycroft 	struct sys_shmdt_args /* {
    282       1.44    kleink 		syscallarg(const void *) shmaddr;
    283       1.32   thorpej 	} */ *uap = v;
    284       1.65   thorpej 	struct proc *p = l->l_proc;
    285       1.70  drochner 	struct shmmap_state *shmmap_s, *shmmap_s1;
    286       1.69  drochner 	struct shmmap_entry *shmmap_se;
    287       1.11   hpeyerl 
    288       1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    289       1.38  christos 	if (shmmap_s == NULL)
    290       1.38  christos 		return EINVAL;
    291       1.38  christos 
    292       1.70  drochner 	shmmap_se = shm_find_mapping(shmmap_s, (vaddr_t)SCARG(uap, shmaddr));
    293       1.70  drochner 	if (!shmmap_se)
    294       1.70  drochner 		return EINVAL;
    295       1.70  drochner 
    296       1.70  drochner 	shmmap_s1 = shmmap_getprivate(p);
    297       1.70  drochner 	if (shmmap_s1 != shmmap_s) {
    298       1.70  drochner 		/* map has been copied, lookup entry in new map */
    299       1.70  drochner 		shmmap_se = shm_find_mapping(shmmap_s1,
    300       1.70  drochner 					     (vaddr_t)SCARG(uap, shmaddr));
    301       1.70  drochner 		KASSERT(shmmap_se != NULL);
    302       1.70  drochner 	}
    303       1.69  drochner #ifdef SHMDEBUG
    304       1.70  drochner 	printf("shmdt: vm %p: remove %d @%lx\n",
    305       1.70  drochner 	       p->p_vmspace, shmmap_se->shmid, shmmap_se->va);
    306       1.69  drochner #endif
    307       1.70  drochner 	shm_delete_mapping(p->p_vmspace, shmmap_s1, shmmap_se);
    308       1.70  drochner 	return 0;
    309       1.11   hpeyerl }
    310       1.11   hpeyerl 
    311       1.12   mycroft int
    312       1.65   thorpej sys_shmat(l, v, retval)
    313       1.65   thorpej 	struct lwp *l;
    314       1.32   thorpej 	void *v;
    315       1.32   thorpej 	register_t *retval;
    316       1.32   thorpej {
    317       1.33   mycroft 	struct sys_shmat_args /* {
    318       1.26       cgd 		syscallarg(int) shmid;
    319       1.44    kleink 		syscallarg(const void *) shmaddr;
    320       1.35  christos 		syscallarg(int) shmflg;
    321       1.32   thorpej 	} */ *uap = v;
    322       1.78  jdolecek 	int error, flags;
    323       1.65   thorpej 	struct proc *p = l->l_proc;
    324       1.12   mycroft 	struct ucred *cred = p->p_ucred;
    325       1.11   hpeyerl 	struct shmid_ds *shmseg;
    326       1.69  drochner 	struct shmmap_state *shmmap_s;
    327       1.74  christos 	struct uvm_object *uobj;
    328       1.47       eeh 	vaddr_t attach_va;
    329       1.11   hpeyerl 	vm_prot_t prot;
    330       1.47       eeh 	vsize_t size;
    331       1.69  drochner 	struct shmmap_entry *shmmap_se;
    332       1.11   hpeyerl 
    333       1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    334       1.11   hpeyerl 	if (shmseg == NULL)
    335       1.11   hpeyerl 		return EINVAL;
    336       1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    337       1.78  jdolecek 		    (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    338       1.35  christos 	if (error)
    339       1.12   mycroft 		return error;
    340       1.69  drochner 
    341       1.69  drochner 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    342       1.69  drochner 	if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg)
    343       1.12   mycroft 		return EMFILE;
    344       1.69  drochner 
    345       1.53     ragge 	size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
    346       1.12   mycroft 	prot = VM_PROT_READ;
    347       1.78  jdolecek 	if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
    348       1.12   mycroft 		prot |= VM_PROT_WRITE;
    349       1.12   mycroft 	flags = MAP_ANON | MAP_SHARED;
    350       1.78  jdolecek 	if (SCARG(uap, shmaddr)) {
    351       1.12   mycroft 		flags |= MAP_FIXED;
    352       1.78  jdolecek 		if (SCARG(uap, shmflg) & SHM_RND)
    353       1.26       cgd 			attach_va =
    354       1.78  jdolecek 			    (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    355       1.78  jdolecek 		else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    356       1.78  jdolecek 			attach_va = (vaddr_t)SCARG(uap, shmaddr);
    357       1.11   hpeyerl 		else
    358       1.14   mycroft 			return EINVAL;
    359       1.12   mycroft 	} else {
    360       1.66    atatat 		/* This is just a hint to uvm_mmap() about where to put it. */
    361  1.82.10.1      tron 		attach_va = p->p_emul->e_vm_default_addr(p,
    362  1.82.10.1      tron 		    (vaddr_t)p->p_vmspace->vm_daddr, size);
    363       1.11   hpeyerl 	}
    364       1.80  jdolecek 	uobj = shmseg->_shm_internal;
    365       1.74  christos 	(*uobj->pgops->pgo_reference)(uobj);
    366       1.61       chs 	error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
    367       1.74  christos 	    uobj, 0, 0,
    368       1.61       chs 	    UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
    369       1.61       chs 	if (error) {
    370       1.74  christos 		(*uobj->pgops->pgo_detach)(uobj);
    371       1.61       chs 		return error;
    372       1.42       mrg 	}
    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.69  drochner 	printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, 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.11   hpeyerl 	shmseg->shm_atime = time.tv_sec;
    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.11   hpeyerl }
    389       1.11   hpeyerl 
    390       1.12   mycroft int
    391       1.65   thorpej sys___shmctl13(l, v, retval)
    392       1.65   thorpej 	struct lwp *l;
    393       1.32   thorpej 	void *v;
    394       1.32   thorpej 	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.65   thorpej 	struct proc *p = l->l_proc;
    402       1.52   thorpej 	struct shmid_ds shmbuf;
    403       1.52   thorpej 	int cmd, error;
    404       1.52   thorpej 
    405       1.52   thorpej 	cmd = SCARG(uap, cmd);
    406       1.52   thorpej 
    407       1.52   thorpej 	if (cmd == IPC_SET) {
    408       1.52   thorpej 		error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
    409       1.52   thorpej 		if (error)
    410       1.52   thorpej 			return (error);
    411       1.52   thorpej 	}
    412       1.52   thorpej 
    413       1.52   thorpej 	error = shmctl1(p, SCARG(uap, shmid), cmd,
    414       1.52   thorpej 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
    415       1.52   thorpej 
    416       1.52   thorpej 	if (error == 0 && cmd == IPC_STAT)
    417       1.52   thorpej 		error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
    418       1.52   thorpej 
    419       1.52   thorpej 	return (error);
    420       1.52   thorpej }
    421       1.52   thorpej 
    422       1.52   thorpej int
    423       1.52   thorpej shmctl1(p, shmid, cmd, shmbuf)
    424       1.52   thorpej 	struct proc *p;
    425       1.52   thorpej 	int shmid;
    426       1.52   thorpej 	int cmd;
    427       1.52   thorpej 	struct shmid_ds *shmbuf;
    428       1.52   thorpej {
    429       1.12   mycroft 	struct ucred *cred = p->p_ucred;
    430       1.11   hpeyerl 	struct shmid_ds *shmseg;
    431       1.52   thorpej 	int error = 0;
    432       1.11   hpeyerl 
    433       1.78  jdolecek 	shmseg = shm_find_segment_by_shmid(shmid);
    434       1.11   hpeyerl 	if (shmseg == NULL)
    435       1.11   hpeyerl 		return EINVAL;
    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.12   mycroft 		shmseg->shm_ctime = time.tv_sec;
    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.11   hpeyerl 	default:
    465       1.11   hpeyerl 		return EINVAL;
    466       1.11   hpeyerl 	}
    467       1.11   hpeyerl 	return 0;
    468       1.11   hpeyerl }
    469       1.11   hpeyerl 
    470       1.12   mycroft static int
    471       1.12   mycroft shmget_existing(p, uap, mode, segnum, retval)
    472       1.11   hpeyerl 	struct proc *p;
    473       1.33   mycroft 	struct sys_shmget_args /* {
    474       1.26       cgd 		syscallarg(key_t) key;
    475       1.44    kleink 		syscallarg(size_t) size;
    476       1.35  christos 		syscallarg(int) shmflg;
    477       1.26       cgd 	} */ *uap;
    478       1.11   hpeyerl 	int mode;
    479       1.11   hpeyerl 	int segnum;
    480       1.26       cgd 	register_t *retval;
    481       1.11   hpeyerl {
    482       1.12   mycroft 	struct shmid_ds *shmseg;
    483       1.12   mycroft 	struct ucred *cred = p->p_ucred;
    484       1.11   hpeyerl 	int error;
    485       1.11   hpeyerl 
    486       1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    487       1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    488       1.16   mycroft 		/*
    489       1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    490       1.16   mycroft 		 * until it's done, and look the key up again (in case the
    491       1.16   mycroft 		 * allocation failed or it was freed).
    492       1.16   mycroft 		 */
    493       1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    494       1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    495       1.35  christos 		if (error)
    496       1.16   mycroft 			return error;
    497       1.16   mycroft 		return EAGAIN;
    498       1.16   mycroft 	}
    499       1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    500       1.11   hpeyerl 		return error;
    501       1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    502       1.11   hpeyerl 		return EINVAL;
    503       1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    504       1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    505       1.12   mycroft 		return EEXIST;
    506       1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    507       1.11   hpeyerl 	return 0;
    508       1.11   hpeyerl }
    509       1.11   hpeyerl 
    510       1.14   mycroft static int
    511       1.14   mycroft shmget_allocate_segment(p, uap, mode, retval)
    512       1.14   mycroft 	struct proc *p;
    513       1.33   mycroft 	struct sys_shmget_args /* {
    514       1.26       cgd 		syscallarg(key_t) key;
    515       1.44    kleink 		syscallarg(size_t) size;
    516       1.35  christos 		syscallarg(int) shmflg;
    517       1.26       cgd 	} */ *uap;
    518       1.14   mycroft 	int mode;
    519       1.26       cgd 	register_t *retval;
    520       1.14   mycroft {
    521       1.39  drochner 	int i, segnum, shmid, size;
    522       1.14   mycroft 	struct ucred *cred = p->p_ucred;
    523       1.14   mycroft 	struct shmid_ds *shmseg;
    524       1.40  drochner 	int error = 0;
    525       1.76  junyoung 
    526       1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    527       1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    528       1.14   mycroft 		return EINVAL;
    529       1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    530       1.14   mycroft 		return ENOSPC;
    531       1.53     ragge 	size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
    532       1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    533       1.14   mycroft 		return ENOMEM;
    534       1.14   mycroft 	if (shm_last_free < 0) {
    535       1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    536       1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    537       1.14   mycroft 				break;
    538       1.14   mycroft 		if (i == shminfo.shmmni)
    539       1.14   mycroft 			panic("shmseg free count inconsistent");
    540       1.14   mycroft 		segnum = i;
    541       1.14   mycroft 	} else  {
    542       1.14   mycroft 		segnum = shm_last_free;
    543       1.14   mycroft 		shm_last_free = -1;
    544       1.14   mycroft 	}
    545       1.14   mycroft 	shmseg = &shmsegs[segnum];
    546       1.14   mycroft 	/*
    547       1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    548       1.14   mycroft 	 * so that noone else tries to create the same key.
    549       1.14   mycroft 	 */
    550       1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    551       1.52   thorpej 	shmseg->shm_perm._key = SCARG(uap, key);
    552       1.52   thorpej 	shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
    553       1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    554       1.42       mrg 
    555       1.80  jdolecek 	shmseg->_shm_internal = uao_create(size, 0);
    556       1.42       mrg 
    557       1.14   mycroft 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
    558       1.14   mycroft 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
    559       1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    560       1.79  jdolecek 	    (mode & (ACCESSPERMS|SHMSEG_RMLINGER)) | SHMSEG_ALLOCATED;
    561       1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    562       1.14   mycroft 	shmseg->shm_cpid = p->p_pid;
    563       1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    564       1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    565       1.14   mycroft 	shmseg->shm_ctime = time.tv_sec;
    566       1.14   mycroft 	shm_committed += btoc(size);
    567       1.14   mycroft 	shm_nused++;
    568       1.40  drochner 
    569       1.51       mrg 	*retval = shmid;
    570       1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    571       1.16   mycroft 		/*
    572       1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    573       1.16   mycroft 		 * them up now.
    574       1.16   mycroft 		 */
    575       1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    576       1.16   mycroft 		wakeup((caddr_t)shmseg);
    577       1.16   mycroft 	}
    578       1.40  drochner 	return error;
    579       1.14   mycroft }
    580       1.14   mycroft 
    581       1.12   mycroft int
    582       1.65   thorpej sys_shmget(l, v, retval)
    583       1.65   thorpej 	struct lwp *l;
    584       1.32   thorpej 	void *v;
    585       1.32   thorpej 	register_t *retval;
    586       1.32   thorpej {
    587       1.33   mycroft 	struct sys_shmget_args /* {
    588       1.26       cgd 		syscallarg(key_t) key;
    589       1.26       cgd 		syscallarg(int) size;
    590       1.35  christos 		syscallarg(int) shmflg;
    591       1.32   thorpej 	} */ *uap = v;
    592       1.65   thorpej 	struct proc *p = l->l_proc;
    593       1.11   hpeyerl 	int segnum, mode, error;
    594       1.11   hpeyerl 
    595       1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    596       1.79  jdolecek 	if (SCARG(uap, shmflg) & _SHM_RMLINGER)
    597       1.79  jdolecek 		mode |= SHMSEG_RMLINGER;
    598       1.79  jdolecek 
    599       1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    600       1.16   mycroft 	again:
    601       1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    602       1.16   mycroft 		if (segnum >= 0) {
    603       1.16   mycroft 			error = shmget_existing(p, uap, mode, segnum, retval);
    604       1.16   mycroft 			if (error == EAGAIN)
    605       1.16   mycroft 				goto again;
    606       1.16   mycroft 			return error;
    607       1.16   mycroft 		}
    608       1.76  junyoung 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    609       1.14   mycroft 			return ENOENT;
    610       1.11   hpeyerl 	}
    611       1.14   mycroft 	return shmget_allocate_segment(p, uap, mode, retval);
    612       1.11   hpeyerl }
    613       1.11   hpeyerl 
    614       1.12   mycroft void
    615       1.41   thorpej shmfork(vm1, vm2)
    616       1.41   thorpej 	struct vmspace *vm1, *vm2;
    617       1.11   hpeyerl {
    618       1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    619       1.69  drochner 	struct shmmap_entry *shmmap_se;
    620       1.69  drochner 
    621       1.69  drochner 	vm2->vm_shm = vm1->vm_shm;
    622       1.11   hpeyerl 
    623       1.69  drochner 	if (vm1->vm_shm == NULL)
    624       1.38  christos 		return;
    625       1.41   thorpej 
    626       1.69  drochner #ifdef SHMDEBUG
    627       1.69  drochner 	printf("shmfork %p->%p\n", vm1, vm2);
    628       1.69  drochner #endif
    629       1.69  drochner 
    630       1.69  drochner 	shmmap_s = (struct shmmap_state *)vm1->vm_shm;
    631       1.69  drochner 
    632       1.69  drochner 	SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    633       1.69  drochner 		shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
    634       1.69  drochner 	shmmap_s->nrefs++;
    635       1.11   hpeyerl }
    636       1.11   hpeyerl 
    637       1.12   mycroft void
    638       1.41   thorpej shmexit(vm)
    639       1.41   thorpej 	struct vmspace *vm;
    640       1.11   hpeyerl {
    641       1.12   mycroft 	struct shmmap_state *shmmap_s;
    642       1.69  drochner 	struct shmmap_entry *shmmap_se;
    643       1.11   hpeyerl 
    644       1.41   thorpej 	shmmap_s = (struct shmmap_state *)vm->vm_shm;
    645       1.38  christos 	if (shmmap_s == NULL)
    646       1.38  christos 		return;
    647       1.69  drochner 
    648       1.41   thorpej 	vm->vm_shm = NULL;
    649       1.69  drochner 
    650       1.69  drochner 	if (--shmmap_s->nrefs > 0) {
    651       1.69  drochner #ifdef SHMDEBUG
    652       1.70  drochner 		printf("shmexit: vm %p drop ref (%d entries), now used by %d\n",
    653       1.69  drochner 		       vm, shmmap_s->nitems, shmmap_s->nrefs);
    654       1.69  drochner #endif
    655       1.69  drochner 		SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
    656       1.69  drochner 			shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
    657       1.69  drochner 		return;
    658       1.69  drochner 	}
    659       1.69  drochner 
    660       1.69  drochner #ifdef SHMDEBUG
    661       1.69  drochner 	printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems);
    662       1.69  drochner #endif
    663       1.69  drochner 	while (!SLIST_EMPTY(&shmmap_s->entries)) {
    664       1.69  drochner 		shmmap_se = SLIST_FIRST(&shmmap_s->entries);
    665       1.69  drochner 		shm_delete_mapping(vm, shmmap_s, shmmap_se);
    666       1.69  drochner 	}
    667       1.69  drochner 	KASSERT(shmmap_s->nitems == 0);
    668       1.69  drochner 	free(shmmap_s, M_SHM);
    669       1.11   hpeyerl }
    670       1.11   hpeyerl 
    671       1.12   mycroft void
    672       1.12   mycroft shminit()
    673       1.11   hpeyerl {
    674       1.71  jdolecek 	int i, sz;
    675       1.71  jdolecek 	vaddr_t v;
    676       1.71  jdolecek 
    677       1.71  jdolecek 	/* Allocate pageable memory for our structures */
    678       1.71  jdolecek 	sz = shminfo.shmmni * sizeof(struct shmid_ds);
    679       1.71  jdolecek 	if ((v = uvm_km_alloc(kernel_map, round_page(sz))) == 0)
    680       1.71  jdolecek 		panic("sysv_shm: cannot allocate memory");
    681       1.71  jdolecek 	shmsegs = (void *)v;
    682       1.24   deraadt 
    683       1.60   thorpej 	shminfo.shmmax *= PAGE_SIZE;
    684       1.11   hpeyerl 
    685       1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    686       1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    687       1.52   thorpej 		shmsegs[i].shm_perm._seq = 0;
    688       1.11   hpeyerl 	}
    689       1.11   hpeyerl 	shm_last_free = 0;
    690       1.11   hpeyerl 	shm_nused = 0;
    691       1.11   hpeyerl 	shm_committed = 0;
    692       1.11   hpeyerl }
    693