Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.38.10.1
      1  1.38.10.1   thorpej /*	$NetBSD: sysv_shm.c,v 1.38.10.1 1997/10/14 10:26:16 thorpej Exp $	*/
      2       1.22       cgd 
      3       1.11   hpeyerl /*
      4       1.17   mycroft  * Copyright (c) 1994 Adam Glass and Charles Hannum.  All rights reserved.
      5       1.11   hpeyerl  *
      6       1.11   hpeyerl  * Redistribution and use in source and binary forms, with or without
      7       1.11   hpeyerl  * modification, are permitted provided that the following conditions
      8       1.11   hpeyerl  * are met:
      9       1.11   hpeyerl  * 1. Redistributions of source code must retain the above copyright
     10       1.11   hpeyerl  *    notice, this list of conditions and the following disclaimer.
     11       1.17   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.17   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13       1.17   mycroft  *    documentation and/or other materials provided with the distribution.
     14       1.17   mycroft  * 3. All advertising materials mentioning features or use of this software
     15       1.17   mycroft  *    must display the following acknowledgement:
     16       1.17   mycroft  *	This product includes software developed by Adam Glass and Charles
     17       1.17   mycroft  *	Hannum.
     18       1.17   mycroft  * 4. The names of the authors may not be used to endorse or promote products
     19       1.11   hpeyerl  *    derived from this software without specific prior written permission.
     20       1.11   hpeyerl  *
     21       1.17   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     22       1.17   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.17   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.17   mycroft  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.17   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26       1.17   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.17   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.17   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.17   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30       1.17   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.11   hpeyerl  */
     32       1.11   hpeyerl 
     33       1.11   hpeyerl #include <sys/types.h>
     34       1.11   hpeyerl #include <sys/param.h>
     35       1.11   hpeyerl #include <sys/kernel.h>
     36       1.11   hpeyerl #include <sys/shm.h>
     37       1.11   hpeyerl #include <sys/proc.h>
     38       1.11   hpeyerl #include <sys/uio.h>
     39       1.11   hpeyerl #include <sys/time.h>
     40       1.11   hpeyerl #include <sys/malloc.h>
     41       1.11   hpeyerl #include <sys/mman.h>
     42       1.11   hpeyerl #include <sys/systm.h>
     43       1.12   mycroft #include <sys/stat.h>
     44       1.11   hpeyerl 
     45       1.26       cgd #include <sys/mount.h>
     46       1.26       cgd #include <sys/syscallargs.h>
     47       1.26       cgd 
     48       1.11   hpeyerl #include <vm/vm.h>
     49       1.11   hpeyerl #include <vm/vm_map.h>
     50       1.11   hpeyerl #include <vm/vm_map.h>
     51       1.11   hpeyerl #include <vm/vm_kern.h>
     52       1.35  christos 
     53       1.35  christos struct shmid_ds *shm_find_segment_by_shmid __P((int));
     54       1.35  christos void shmexit __P((struct proc *));
     55       1.35  christos 
     56       1.11   hpeyerl /*
     57       1.11   hpeyerl  * Provides the following externally accessible functions:
     58       1.11   hpeyerl  *
     59       1.11   hpeyerl  * shminit(void);		           initialization
     60       1.11   hpeyerl  * shmexit(struct proc *)                  cleanup
     61       1.34   mycroft  * shmfork(struct proc *, struct proc *)   fork handling
     62       1.11   hpeyerl  * shmsys(arg1, arg2, arg3, arg4);         shm{at,ctl,dt,get}(arg2, arg3, arg4)
     63       1.11   hpeyerl  *
     64       1.11   hpeyerl  * Structures:
     65       1.11   hpeyerl  * shmsegs (an array of 'struct shmid_ds')
     66       1.11   hpeyerl  * per proc array of 'struct shmmap_state'
     67       1.11   hpeyerl  */
     68       1.12   mycroft 
     69       1.16   mycroft #define	SHMSEG_FREE     	0x0200
     70       1.16   mycroft #define	SHMSEG_REMOVED  	0x0400
     71       1.16   mycroft #define	SHMSEG_ALLOCATED	0x0800
     72       1.16   mycroft #define	SHMSEG_WANTED		0x1000
     73       1.11   hpeyerl 
     74       1.11   hpeyerl int shm_last_free, shm_nused, shm_committed;
     75       1.11   hpeyerl 
     76       1.11   hpeyerl struct shm_handle {
     77  1.38.10.1   thorpej 	vm_object_t shm_object;
     78       1.11   hpeyerl };
     79       1.11   hpeyerl 
     80       1.11   hpeyerl struct shmmap_state {
     81       1.11   hpeyerl 	vm_offset_t va;
     82       1.11   hpeyerl 	int shmid;
     83       1.11   hpeyerl };
     84       1.11   hpeyerl 
     85       1.35  christos static int shm_find_segment_by_key __P((key_t));
     86       1.12   mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
     87       1.12   mycroft static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
     88       1.35  christos static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
     89       1.35  christos 				int, int, register_t *));
     90       1.35  christos static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
     91       1.35  christos 					int, register_t *));
     92       1.11   hpeyerl 
     93       1.12   mycroft static int
     94       1.12   mycroft shm_find_segment_by_key(key)
     95       1.11   hpeyerl 	key_t key;
     96       1.11   hpeyerl {
     97       1.11   hpeyerl 	int i;
     98       1.11   hpeyerl 
     99       1.12   mycroft 	for (i = 0; i < shminfo.shmmni; i++)
    100       1.12   mycroft 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
    101       1.12   mycroft 		    shmsegs[i].shm_perm.key == key)
    102       1.12   mycroft 			return i;
    103       1.11   hpeyerl 	return -1;
    104       1.11   hpeyerl }
    105       1.11   hpeyerl 
    106       1.28  christos struct shmid_ds *
    107       1.15   mycroft shm_find_segment_by_shmid(shmid)
    108       1.11   hpeyerl 	int shmid;
    109       1.11   hpeyerl {
    110       1.11   hpeyerl 	int segnum;
    111       1.11   hpeyerl 	struct shmid_ds *shmseg;
    112       1.11   hpeyerl 
    113       1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    114       1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    115       1.11   hpeyerl 		return NULL;
    116       1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    117       1.12   mycroft 	if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
    118       1.12   mycroft 	    != SHMSEG_ALLOCATED ||
    119       1.12   mycroft 	    shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
    120       1.11   hpeyerl 		return NULL;
    121       1.11   hpeyerl 	return shmseg;
    122       1.11   hpeyerl }
    123       1.11   hpeyerl 
    124       1.12   mycroft static void
    125       1.12   mycroft shm_deallocate_segment(shmseg)
    126       1.12   mycroft 	struct shmid_ds *shmseg;
    127       1.12   mycroft {
    128       1.12   mycroft 	struct shm_handle *shm_handle;
    129       1.14   mycroft 	size_t size;
    130       1.12   mycroft 
    131       1.12   mycroft 	shm_handle = shmseg->shm_internal;
    132       1.14   mycroft 	size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
    133  1.38.10.1   thorpej 	vm_object_deallocate(shm_handle->shm_object);
    134       1.12   mycroft 	free((caddr_t)shm_handle, M_SHM);
    135       1.12   mycroft 	shmseg->shm_internal = NULL;
    136       1.14   mycroft 	shm_committed -= btoc(size);
    137       1.12   mycroft 	shmseg->shm_perm.mode = SHMSEG_FREE;
    138       1.25   mycroft 	shm_nused--;
    139       1.12   mycroft }
    140       1.12   mycroft 
    141       1.12   mycroft static int
    142       1.12   mycroft shm_delete_mapping(p, shmmap_s)
    143       1.11   hpeyerl 	struct proc *p;
    144       1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    145       1.11   hpeyerl {
    146       1.12   mycroft 	struct shmid_ds *shmseg;
    147       1.12   mycroft 	int segnum, result;
    148       1.14   mycroft 	size_t size;
    149       1.11   hpeyerl 
    150       1.12   mycroft 	segnum = IPCID_TO_IX(shmmap_s->shmid);
    151       1.12   mycroft 	shmseg = &shmsegs[segnum];
    152       1.14   mycroft 	size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
    153  1.38.10.1   thorpej 	result = vm_map_remove(&p->p_vmspace->vm_map,
    154  1.38.10.1   thorpej 			       shmmap_s->va, shmmap_s->va + size);
    155       1.11   hpeyerl 	if (result != KERN_SUCCESS)
    156       1.11   hpeyerl 		return EINVAL;
    157       1.12   mycroft 	shmmap_s->shmid = -1;
    158       1.11   hpeyerl 	shmseg->shm_dtime = time.tv_sec;
    159       1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    160       1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    161       1.12   mycroft 		shm_deallocate_segment(shmseg);
    162       1.12   mycroft 		shm_last_free = segnum;
    163       1.11   hpeyerl 	}
    164       1.11   hpeyerl 	return 0;
    165       1.11   hpeyerl }
    166       1.11   hpeyerl 
    167       1.12   mycroft int
    168       1.33   mycroft sys_shmdt(p, v, retval)
    169       1.11   hpeyerl 	struct proc *p;
    170       1.32   thorpej 	void *v;
    171       1.32   thorpej 	register_t *retval;
    172       1.32   thorpej {
    173       1.33   mycroft 	struct sys_shmdt_args /* {
    174       1.26       cgd 		syscallarg(void *) shmaddr;
    175       1.32   thorpej 	} */ *uap = v;
    176       1.12   mycroft 	struct shmmap_state *shmmap_s;
    177       1.11   hpeyerl 	int i;
    178       1.11   hpeyerl 
    179       1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    180       1.38  christos 	if (shmmap_s == NULL)
    181       1.38  christos 		return EINVAL;
    182       1.38  christos 
    183       1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    184       1.12   mycroft 		if (shmmap_s->shmid != -1 &&
    185       1.26       cgd 		    shmmap_s->va == (vm_offset_t)SCARG(uap, shmaddr))
    186       1.12   mycroft 			break;
    187       1.11   hpeyerl 	if (i == shminfo.shmseg)
    188       1.11   hpeyerl 		return EINVAL;
    189       1.12   mycroft 	return shm_delete_mapping(p, shmmap_s);
    190       1.11   hpeyerl }
    191       1.11   hpeyerl 
    192       1.12   mycroft int
    193       1.33   mycroft sys_shmat(p, v, retval)
    194       1.11   hpeyerl 	struct proc *p;
    195       1.32   thorpej 	void *v;
    196       1.32   thorpej 	register_t *retval;
    197       1.32   thorpej {
    198       1.33   mycroft 	struct sys_shmat_args /* {
    199       1.26       cgd 		syscallarg(int) shmid;
    200       1.26       cgd 		syscallarg(void *) shmaddr;
    201       1.35  christos 		syscallarg(int) shmflg;
    202       1.32   thorpej 	} */ *uap = v;
    203       1.12   mycroft 	int error, i, flags;
    204       1.12   mycroft 	struct ucred *cred = p->p_ucred;
    205       1.11   hpeyerl 	struct shmid_ds *shmseg;
    206       1.11   hpeyerl 	struct shmmap_state *shmmap_s = NULL;
    207  1.38.10.1   thorpej 	struct shm_handle *shm_handle;
    208       1.11   hpeyerl 	vm_offset_t attach_va;
    209       1.11   hpeyerl 	vm_prot_t prot;
    210       1.11   hpeyerl 	vm_size_t size;
    211  1.38.10.1   thorpej 	int rv;
    212       1.11   hpeyerl 
    213       1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    214       1.12   mycroft 	if (shmmap_s == NULL) {
    215       1.12   mycroft 		size = shminfo.shmseg * sizeof(struct shmmap_state);
    216       1.12   mycroft 		shmmap_s = malloc(size, M_SHM, M_WAITOK);
    217       1.18       cgd 		for (i = 0; i < shminfo.shmseg; i++)
    218       1.18       cgd 			shmmap_s[i].shmid = -1;
    219       1.12   mycroft 		p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    220       1.12   mycroft 	}
    221       1.26       cgd 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    222       1.11   hpeyerl 	if (shmseg == NULL)
    223       1.11   hpeyerl 		return EINVAL;
    224       1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    225       1.35  christos 		    (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    226       1.35  christos 	if (error)
    227       1.12   mycroft 		return error;
    228       1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++) {
    229       1.12   mycroft 		if (shmmap_s->shmid == -1)
    230       1.12   mycroft 			break;
    231       1.12   mycroft 		shmmap_s++;
    232       1.11   hpeyerl 	}
    233       1.12   mycroft 	if (i >= shminfo.shmseg)
    234       1.12   mycroft 		return EMFILE;
    235       1.14   mycroft 	size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
    236       1.12   mycroft 	prot = VM_PROT_READ;
    237       1.26       cgd 	if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
    238       1.12   mycroft 		prot |= VM_PROT_WRITE;
    239       1.12   mycroft 	flags = MAP_ANON | MAP_SHARED;
    240       1.26       cgd 	if (SCARG(uap, shmaddr)) {
    241       1.12   mycroft 		flags |= MAP_FIXED;
    242       1.26       cgd 		if (SCARG(uap, shmflg) & SHM_RND)
    243       1.26       cgd 			attach_va =
    244       1.26       cgd 			    (vm_offset_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    245       1.26       cgd 		else if (((vm_offset_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    246       1.26       cgd 			attach_va = (vm_offset_t)SCARG(uap, shmaddr);
    247       1.11   hpeyerl 		else
    248       1.14   mycroft 			return EINVAL;
    249       1.12   mycroft 	} else {
    250       1.20   mycroft 		/* This is just a hint to vm_mmap() about where to put it. */
    251       1.31       cgd 		attach_va =
    252       1.31       cgd 		    round_page(p->p_vmspace->vm_taddr + MAXTSIZ + MAXDSIZ);
    253       1.11   hpeyerl 	}
    254  1.38.10.1   thorpej 	shm_handle = shmseg->shm_internal;
    255  1.38.10.1   thorpej 	vm_object_reference(shm_handle->shm_object);
    256  1.38.10.1   thorpej 	rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
    257  1.38.10.1   thorpej 		0, &attach_va, size, (flags & MAP_FIXED)?0:1);
    258  1.38.10.1   thorpej 	if (rv != KERN_SUCCESS) {
    259  1.38.10.1   thorpej 		return ENOMEM;
    260  1.38.10.1   thorpej 	}
    261  1.38.10.1   thorpej 	vm_map_protect(&p->p_vmspace->vm_map, attach_va, attach_va + size,
    262  1.38.10.1   thorpej 		       prot, 0);
    263  1.38.10.1   thorpej 	vm_map_inherit(&p->p_vmspace->vm_map,
    264  1.38.10.1   thorpej 		attach_va, attach_va + size, VM_INHERIT_SHARE);
    265  1.38.10.1   thorpej 
    266       1.12   mycroft 	shmmap_s->va = attach_va;
    267       1.26       cgd 	shmmap_s->shmid = SCARG(uap, shmid);
    268       1.11   hpeyerl 	shmseg->shm_lpid = p->p_pid;
    269       1.11   hpeyerl 	shmseg->shm_atime = time.tv_sec;
    270       1.11   hpeyerl 	shmseg->shm_nattch++;
    271       1.12   mycroft 	*retval = attach_va;
    272       1.11   hpeyerl 	return 0;
    273       1.11   hpeyerl }
    274       1.11   hpeyerl 
    275       1.12   mycroft int
    276       1.33   mycroft sys_shmctl(p, v, retval)
    277       1.11   hpeyerl 	struct proc *p;
    278       1.32   thorpej 	void *v;
    279       1.32   thorpej 	register_t *retval;
    280       1.32   thorpej {
    281       1.33   mycroft 	struct sys_shmctl_args /* {
    282       1.26       cgd 		syscallarg(int) shmid;
    283       1.26       cgd 		syscallarg(int) cmd;
    284       1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    285       1.32   thorpej 	} */ *uap = v;
    286       1.35  christos 	int error;
    287       1.12   mycroft 	struct ucred *cred = p->p_ucred;
    288       1.11   hpeyerl 	struct shmid_ds inbuf;
    289       1.11   hpeyerl 	struct shmid_ds *shmseg;
    290       1.11   hpeyerl 
    291       1.26       cgd 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    292       1.11   hpeyerl 	if (shmseg == NULL)
    293       1.11   hpeyerl 		return EINVAL;
    294       1.26       cgd 	switch (SCARG(uap, cmd)) {
    295       1.11   hpeyerl 	case IPC_STAT:
    296       1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    297       1.11   hpeyerl 			return error;
    298       1.35  christos 		error = copyout((caddr_t)shmseg, SCARG(uap, buf),
    299       1.35  christos 				sizeof(inbuf));
    300       1.35  christos 		if (error)
    301       1.11   hpeyerl 			return error;
    302       1.11   hpeyerl 		break;
    303       1.11   hpeyerl 	case IPC_SET:
    304       1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    305       1.13   mycroft 			return error;
    306       1.35  christos 		error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
    307       1.35  christos 			       sizeof(inbuf));
    308       1.35  christos 		if (error)
    309       1.11   hpeyerl 			return error;
    310       1.11   hpeyerl 		shmseg->shm_perm.uid = inbuf.shm_perm.uid;
    311       1.11   hpeyerl 		shmseg->shm_perm.gid = inbuf.shm_perm.gid;
    312       1.12   mycroft 		shmseg->shm_perm.mode =
    313       1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    314       1.12   mycroft 		    (inbuf.shm_perm.mode & ACCESSPERMS);
    315       1.12   mycroft 		shmseg->shm_ctime = time.tv_sec;
    316       1.11   hpeyerl 		break;
    317       1.11   hpeyerl 	case IPC_RMID:
    318       1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    319       1.13   mycroft 			return error;
    320       1.12   mycroft 		shmseg->shm_perm.key = IPC_PRIVATE;
    321       1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    322       1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    323       1.12   mycroft 			shm_deallocate_segment(shmseg);
    324       1.26       cgd 			shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
    325       1.11   hpeyerl 		}
    326       1.11   hpeyerl 		break;
    327       1.11   hpeyerl 	case SHM_LOCK:
    328       1.11   hpeyerl 	case SHM_UNLOCK:
    329       1.11   hpeyerl 	default:
    330       1.11   hpeyerl 		return EINVAL;
    331       1.11   hpeyerl 	}
    332       1.11   hpeyerl 	return 0;
    333       1.11   hpeyerl }
    334       1.11   hpeyerl 
    335       1.12   mycroft static int
    336       1.12   mycroft shmget_existing(p, uap, mode, segnum, retval)
    337       1.11   hpeyerl 	struct proc *p;
    338       1.33   mycroft 	struct sys_shmget_args /* {
    339       1.26       cgd 		syscallarg(key_t) key;
    340       1.26       cgd 		syscallarg(int) size;
    341       1.35  christos 		syscallarg(int) shmflg;
    342       1.26       cgd 	} */ *uap;
    343       1.11   hpeyerl 	int mode;
    344       1.11   hpeyerl 	int segnum;
    345       1.26       cgd 	register_t *retval;
    346       1.11   hpeyerl {
    347       1.12   mycroft 	struct shmid_ds *shmseg;
    348       1.12   mycroft 	struct ucred *cred = p->p_ucred;
    349       1.11   hpeyerl 	int error;
    350       1.11   hpeyerl 
    351       1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    352       1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    353       1.16   mycroft 		/*
    354       1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    355       1.16   mycroft 		 * until it's done, and look the key up again (in case the
    356       1.16   mycroft 		 * allocation failed or it was freed).
    357       1.16   mycroft 		 */
    358       1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    359       1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    360       1.35  christos 		if (error)
    361       1.16   mycroft 			return error;
    362       1.16   mycroft 		return EAGAIN;
    363       1.16   mycroft 	}
    364       1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    365       1.11   hpeyerl 		return error;
    366       1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    367       1.11   hpeyerl 		return EINVAL;
    368       1.37  christos 	if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
    369       1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    370       1.12   mycroft 		return EEXIST;
    371       1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    372       1.11   hpeyerl 	return 0;
    373       1.11   hpeyerl }
    374       1.11   hpeyerl 
    375       1.14   mycroft static int
    376       1.14   mycroft shmget_allocate_segment(p, uap, mode, retval)
    377       1.14   mycroft 	struct proc *p;
    378       1.33   mycroft 	struct sys_shmget_args /* {
    379       1.26       cgd 		syscallarg(key_t) key;
    380       1.26       cgd 		syscallarg(int) size;
    381       1.35  christos 		syscallarg(int) shmflg;
    382       1.26       cgd 	} */ *uap;
    383       1.14   mycroft 	int mode;
    384       1.26       cgd 	register_t *retval;
    385       1.14   mycroft {
    386  1.38.10.1   thorpej 	int i, segnum, shmid, size;
    387       1.14   mycroft 	struct ucred *cred = p->p_ucred;
    388       1.14   mycroft 	struct shmid_ds *shmseg;
    389       1.14   mycroft 	struct shm_handle *shm_handle;
    390  1.38.10.1   thorpej 	vm_pager_t pager;
    391  1.38.10.1   thorpej 	int error = 0;
    392       1.14   mycroft 
    393       1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    394       1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    395       1.14   mycroft 		return EINVAL;
    396       1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    397       1.14   mycroft 		return ENOSPC;
    398       1.26       cgd 	size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
    399       1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    400       1.14   mycroft 		return ENOMEM;
    401       1.14   mycroft 	if (shm_last_free < 0) {
    402       1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    403       1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    404       1.14   mycroft 				break;
    405       1.14   mycroft 		if (i == shminfo.shmmni)
    406       1.14   mycroft 			panic("shmseg free count inconsistent");
    407       1.14   mycroft 		segnum = i;
    408       1.14   mycroft 	} else  {
    409       1.14   mycroft 		segnum = shm_last_free;
    410       1.14   mycroft 		shm_last_free = -1;
    411       1.14   mycroft 	}
    412       1.14   mycroft 	shmseg = &shmsegs[segnum];
    413       1.14   mycroft 	/*
    414       1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    415       1.14   mycroft 	 * so that noone else tries to create the same key.
    416       1.14   mycroft 	 */
    417       1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    418       1.26       cgd 	shmseg->shm_perm.key = SCARG(uap, key);
    419       1.14   mycroft 	shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
    420       1.14   mycroft 	shm_handle = (struct shm_handle *)
    421       1.14   mycroft 	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
    422       1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    423  1.38.10.1   thorpej 
    424  1.38.10.1   thorpej 	shm_handle->shm_object = vm_object_allocate(size);
    425  1.38.10.1   thorpej 	if (shm_handle->shm_object == NULL) {
    426  1.38.10.1   thorpej 		/* XXX cannot happen */
    427  1.38.10.1   thorpej 		error = ENOMEM;
    428  1.38.10.1   thorpej 		goto out;
    429  1.38.10.1   thorpej 	}
    430  1.38.10.1   thorpej 	/*
    431  1.38.10.1   thorpej 	 * We make sure that we have allocated a pager before we need
    432  1.38.10.1   thorpej 	 * to.
    433  1.38.10.1   thorpej 	 */
    434  1.38.10.1   thorpej 	pager = vm_pager_allocate(PG_DFLT, 0, size, VM_PROT_DEFAULT, 0);
    435  1.38.10.1   thorpej 	if (pager == NULL) {
    436  1.38.10.1   thorpej 		error = ENOMEM;
    437  1.38.10.1   thorpej 		goto out;
    438       1.14   mycroft 	}
    439  1.38.10.1   thorpej 	vm_object_setpager(shm_handle->shm_object, pager, 0, 0);
    440       1.14   mycroft 	shmseg->shm_internal = shm_handle;
    441       1.14   mycroft 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
    442       1.14   mycroft 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
    443       1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    444       1.16   mycroft 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
    445       1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    446       1.14   mycroft 	shmseg->shm_cpid = p->p_pid;
    447       1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    448       1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    449       1.14   mycroft 	shmseg->shm_ctime = time.tv_sec;
    450       1.14   mycroft 	shm_committed += btoc(size);
    451       1.14   mycroft 	shm_nused++;
    452  1.38.10.1   thorpej 
    453  1.38.10.1   thorpej out:
    454  1.38.10.1   thorpej 	if (error) {
    455  1.38.10.1   thorpej 		if (shm_handle->shm_object != NULL)
    456  1.38.10.1   thorpej 			vm_object_deallocate(shm_handle->shm_object);
    457  1.38.10.1   thorpej 		free(shm_handle, M_SHM);
    458  1.38.10.1   thorpej 		shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED)
    459  1.38.10.1   thorpej 		    | SHMSEG_FREE;
    460  1.38.10.1   thorpej 	} else
    461  1.38.10.1   thorpej 		*retval = shmid;
    462       1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    463       1.16   mycroft 		/*
    464       1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    465       1.16   mycroft 		 * them up now.
    466       1.16   mycroft 		 */
    467       1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    468       1.16   mycroft 		wakeup((caddr_t)shmseg);
    469       1.16   mycroft 	}
    470  1.38.10.1   thorpej 	return error;
    471       1.14   mycroft }
    472       1.14   mycroft 
    473       1.12   mycroft int
    474       1.33   mycroft sys_shmget(p, v, retval)
    475       1.11   hpeyerl 	struct proc *p;
    476       1.32   thorpej 	void *v;
    477       1.32   thorpej 	register_t *retval;
    478       1.32   thorpej {
    479       1.33   mycroft 	struct sys_shmget_args /* {
    480       1.26       cgd 		syscallarg(key_t) key;
    481       1.26       cgd 		syscallarg(int) size;
    482       1.35  christos 		syscallarg(int) shmflg;
    483       1.32   thorpej 	} */ *uap = v;
    484       1.11   hpeyerl 	int segnum, mode, error;
    485       1.11   hpeyerl 
    486       1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    487       1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    488       1.16   mycroft 	again:
    489       1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    490       1.16   mycroft 		if (segnum >= 0) {
    491       1.16   mycroft 			error = shmget_existing(p, uap, mode, segnum, retval);
    492       1.16   mycroft 			if (error == EAGAIN)
    493       1.16   mycroft 				goto again;
    494       1.16   mycroft 			return error;
    495       1.16   mycroft 		}
    496       1.26       cgd 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    497       1.14   mycroft 			return ENOENT;
    498       1.11   hpeyerl 	}
    499       1.14   mycroft 	return shmget_allocate_segment(p, uap, mode, retval);
    500       1.11   hpeyerl }
    501       1.11   hpeyerl 
    502       1.12   mycroft void
    503       1.34   mycroft shmfork(p1, p2)
    504       1.12   mycroft 	struct proc *p1, *p2;
    505       1.11   hpeyerl {
    506       1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    507       1.12   mycroft 	size_t size;
    508       1.11   hpeyerl 	int i;
    509       1.11   hpeyerl 
    510       1.38  christos 	if (p1->p_vmspace->vm_shm == NULL) {
    511       1.38  christos 		p2->p_vmspace->vm_shm = NULL;
    512       1.38  christos 		return;
    513       1.38  christos 	}
    514       1.38  christos 
    515       1.12   mycroft 	size = shminfo.shmseg * sizeof(struct shmmap_state);
    516       1.12   mycroft 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
    517       1.12   mycroft 	bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
    518       1.12   mycroft 	p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    519       1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    520       1.12   mycroft 		if (shmmap_s->shmid != -1)
    521       1.11   hpeyerl 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
    522       1.11   hpeyerl }
    523       1.11   hpeyerl 
    524       1.12   mycroft void
    525       1.12   mycroft shmexit(p)
    526       1.11   hpeyerl 	struct proc *p;
    527       1.11   hpeyerl {
    528       1.12   mycroft 	struct shmmap_state *shmmap_s;
    529       1.11   hpeyerl 	int i;
    530       1.11   hpeyerl 
    531       1.11   hpeyerl 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    532       1.38  christos 	if (shmmap_s == NULL)
    533       1.38  christos 		return;
    534       1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    535       1.12   mycroft 		if (shmmap_s->shmid != -1)
    536       1.12   mycroft 			shm_delete_mapping(p, shmmap_s);
    537       1.12   mycroft 	free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
    538       1.11   hpeyerl 	p->p_vmspace->vm_shm = NULL;
    539       1.11   hpeyerl }
    540       1.11   hpeyerl 
    541       1.12   mycroft void
    542       1.12   mycroft shminit()
    543       1.11   hpeyerl {
    544       1.11   hpeyerl 	int i;
    545       1.24   deraadt 
    546       1.24   deraadt 	shminfo.shmmax *= NBPG;
    547       1.11   hpeyerl 
    548       1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    549       1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    550       1.11   hpeyerl 		shmsegs[i].shm_perm.seq = 0;
    551       1.11   hpeyerl 	}
    552       1.11   hpeyerl 	shm_last_free = 0;
    553       1.11   hpeyerl 	shm_nused = 0;
    554       1.11   hpeyerl 	shm_committed = 0;
    555       1.11   hpeyerl }
    556