Home | History | Annotate | Line # | Download | only in kern
sysv_shm.c revision 1.35
      1  1.35  christos /*	$NetBSD: sysv_shm.c,v 1.35 1996/02/04 02:17:10 christos 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.11   hpeyerl 
     53  1.35  christos #include <kern/kern_extern.h>
     54  1.35  christos 
     55  1.35  christos struct shmid_ds *shm_find_segment_by_shmid __P((int));
     56  1.35  christos void shmexit __P((struct proc *));
     57  1.35  christos 
     58  1.11   hpeyerl /*
     59  1.11   hpeyerl  * Provides the following externally accessible functions:
     60  1.11   hpeyerl  *
     61  1.11   hpeyerl  * shminit(void);		           initialization
     62  1.11   hpeyerl  * shmexit(struct proc *)                  cleanup
     63  1.34   mycroft  * shmfork(struct proc *, struct proc *)   fork handling
     64  1.11   hpeyerl  * shmsys(arg1, arg2, arg3, arg4);         shm{at,ctl,dt,get}(arg2, arg3, arg4)
     65  1.11   hpeyerl  *
     66  1.11   hpeyerl  * Structures:
     67  1.11   hpeyerl  * shmsegs (an array of 'struct shmid_ds')
     68  1.11   hpeyerl  * per proc array of 'struct shmmap_state'
     69  1.11   hpeyerl  */
     70  1.12   mycroft 
     71  1.16   mycroft #define	SHMSEG_FREE     	0x0200
     72  1.16   mycroft #define	SHMSEG_REMOVED  	0x0400
     73  1.16   mycroft #define	SHMSEG_ALLOCATED	0x0800
     74  1.16   mycroft #define	SHMSEG_WANTED		0x1000
     75  1.11   hpeyerl 
     76  1.11   hpeyerl vm_map_t sysvshm_map;
     77  1.11   hpeyerl int shm_last_free, shm_nused, shm_committed;
     78  1.11   hpeyerl 
     79  1.11   hpeyerl struct shm_handle {
     80  1.11   hpeyerl 	vm_offset_t kva;
     81  1.11   hpeyerl };
     82  1.11   hpeyerl 
     83  1.11   hpeyerl struct shmmap_state {
     84  1.11   hpeyerl 	vm_offset_t va;
     85  1.11   hpeyerl 	int shmid;
     86  1.11   hpeyerl };
     87  1.11   hpeyerl 
     88  1.35  christos static int shm_find_segment_by_key __P((key_t));
     89  1.12   mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
     90  1.12   mycroft static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
     91  1.35  christos static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
     92  1.35  christos 				int, int, register_t *));
     93  1.35  christos static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
     94  1.35  christos 					int, register_t *));
     95  1.11   hpeyerl 
     96  1.12   mycroft static int
     97  1.12   mycroft shm_find_segment_by_key(key)
     98  1.11   hpeyerl 	key_t key;
     99  1.11   hpeyerl {
    100  1.11   hpeyerl 	int i;
    101  1.11   hpeyerl 
    102  1.12   mycroft 	for (i = 0; i < shminfo.shmmni; i++)
    103  1.12   mycroft 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
    104  1.12   mycroft 		    shmsegs[i].shm_perm.key == key)
    105  1.12   mycroft 			return i;
    106  1.11   hpeyerl 	return -1;
    107  1.11   hpeyerl }
    108  1.11   hpeyerl 
    109  1.28  christos struct shmid_ds *
    110  1.15   mycroft shm_find_segment_by_shmid(shmid)
    111  1.11   hpeyerl 	int shmid;
    112  1.11   hpeyerl {
    113  1.11   hpeyerl 	int segnum;
    114  1.11   hpeyerl 	struct shmid_ds *shmseg;
    115  1.11   hpeyerl 
    116  1.11   hpeyerl 	segnum = IPCID_TO_IX(shmid);
    117  1.12   mycroft 	if (segnum < 0 || segnum >= shminfo.shmmni)
    118  1.11   hpeyerl 		return NULL;
    119  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    120  1.12   mycroft 	if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
    121  1.12   mycroft 	    != SHMSEG_ALLOCATED ||
    122  1.12   mycroft 	    shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
    123  1.11   hpeyerl 		return NULL;
    124  1.11   hpeyerl 	return shmseg;
    125  1.11   hpeyerl }
    126  1.11   hpeyerl 
    127  1.12   mycroft static void
    128  1.12   mycroft shm_deallocate_segment(shmseg)
    129  1.12   mycroft 	struct shmid_ds *shmseg;
    130  1.12   mycroft {
    131  1.12   mycroft 	struct shm_handle *shm_handle;
    132  1.14   mycroft 	size_t size;
    133  1.12   mycroft 
    134  1.12   mycroft 	shm_handle = shmseg->shm_internal;
    135  1.14   mycroft 	size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
    136  1.14   mycroft 	vm_deallocate(sysvshm_map, shm_handle->kva, size);
    137  1.12   mycroft 	free((caddr_t)shm_handle, M_SHM);
    138  1.12   mycroft 	shmseg->shm_internal = NULL;
    139  1.14   mycroft 	shm_committed -= btoc(size);
    140  1.12   mycroft 	shmseg->shm_perm.mode = SHMSEG_FREE;
    141  1.25   mycroft 	shm_nused--;
    142  1.12   mycroft }
    143  1.12   mycroft 
    144  1.12   mycroft static int
    145  1.12   mycroft shm_delete_mapping(p, shmmap_s)
    146  1.11   hpeyerl 	struct proc *p;
    147  1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    148  1.11   hpeyerl {
    149  1.12   mycroft 	struct shmid_ds *shmseg;
    150  1.12   mycroft 	int segnum, result;
    151  1.14   mycroft 	size_t size;
    152  1.11   hpeyerl 
    153  1.12   mycroft 	segnum = IPCID_TO_IX(shmmap_s->shmid);
    154  1.12   mycroft 	shmseg = &shmsegs[segnum];
    155  1.14   mycroft 	size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
    156  1.14   mycroft 	result = vm_deallocate(&p->p_vmspace->vm_map, shmmap_s->va, size);
    157  1.11   hpeyerl 	if (result != KERN_SUCCESS)
    158  1.11   hpeyerl 		return EINVAL;
    159  1.12   mycroft 	shmmap_s->shmid = -1;
    160  1.11   hpeyerl 	shmseg->shm_dtime = time.tv_sec;
    161  1.12   mycroft 	if ((--shmseg->shm_nattch <= 0) &&
    162  1.11   hpeyerl 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
    163  1.12   mycroft 		shm_deallocate_segment(shmseg);
    164  1.12   mycroft 		shm_last_free = segnum;
    165  1.11   hpeyerl 	}
    166  1.11   hpeyerl 	return 0;
    167  1.11   hpeyerl }
    168  1.11   hpeyerl 
    169  1.12   mycroft int
    170  1.33   mycroft sys_shmdt(p, v, retval)
    171  1.11   hpeyerl 	struct proc *p;
    172  1.32   thorpej 	void *v;
    173  1.32   thorpej 	register_t *retval;
    174  1.32   thorpej {
    175  1.33   mycroft 	struct sys_shmdt_args /* {
    176  1.26       cgd 		syscallarg(void *) shmaddr;
    177  1.32   thorpej 	} */ *uap = v;
    178  1.12   mycroft 	struct shmmap_state *shmmap_s;
    179  1.11   hpeyerl 	int i;
    180  1.11   hpeyerl 
    181  1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    182  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    183  1.12   mycroft 		if (shmmap_s->shmid != -1 &&
    184  1.26       cgd 		    shmmap_s->va == (vm_offset_t)SCARG(uap, shmaddr))
    185  1.12   mycroft 			break;
    186  1.11   hpeyerl 	if (i == shminfo.shmseg)
    187  1.11   hpeyerl 		return EINVAL;
    188  1.12   mycroft 	return shm_delete_mapping(p, shmmap_s);
    189  1.11   hpeyerl }
    190  1.11   hpeyerl 
    191  1.12   mycroft int
    192  1.33   mycroft sys_shmat(p, v, retval)
    193  1.11   hpeyerl 	struct proc *p;
    194  1.32   thorpej 	void *v;
    195  1.32   thorpej 	register_t *retval;
    196  1.32   thorpej {
    197  1.33   mycroft 	struct sys_shmat_args /* {
    198  1.26       cgd 		syscallarg(int) shmid;
    199  1.26       cgd 		syscallarg(void *) shmaddr;
    200  1.35  christos 		syscallarg(int) shmflg;
    201  1.32   thorpej 	} */ *uap = v;
    202  1.12   mycroft 	int error, i, flags;
    203  1.12   mycroft 	struct ucred *cred = p->p_ucred;
    204  1.11   hpeyerl 	struct shmid_ds *shmseg;
    205  1.11   hpeyerl 	struct shmmap_state *shmmap_s = NULL;
    206  1.11   hpeyerl 	vm_offset_t attach_va;
    207  1.11   hpeyerl 	vm_prot_t prot;
    208  1.11   hpeyerl 	vm_size_t size;
    209  1.11   hpeyerl 
    210  1.12   mycroft 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    211  1.12   mycroft 	if (shmmap_s == NULL) {
    212  1.12   mycroft 		size = shminfo.shmseg * sizeof(struct shmmap_state);
    213  1.12   mycroft 		shmmap_s = malloc(size, M_SHM, M_WAITOK);
    214  1.18       cgd 		for (i = 0; i < shminfo.shmseg; i++)
    215  1.18       cgd 			shmmap_s[i].shmid = -1;
    216  1.12   mycroft 		p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    217  1.12   mycroft 	}
    218  1.26       cgd 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    219  1.11   hpeyerl 	if (shmseg == NULL)
    220  1.11   hpeyerl 		return EINVAL;
    221  1.35  christos 	error = ipcperm(cred, &shmseg->shm_perm,
    222  1.35  christos 		    (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
    223  1.35  christos 	if (error)
    224  1.12   mycroft 		return error;
    225  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++) {
    226  1.12   mycroft 		if (shmmap_s->shmid == -1)
    227  1.12   mycroft 			break;
    228  1.12   mycroft 		shmmap_s++;
    229  1.11   hpeyerl 	}
    230  1.12   mycroft 	if (i >= shminfo.shmseg)
    231  1.12   mycroft 		return EMFILE;
    232  1.14   mycroft 	size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
    233  1.12   mycroft 	prot = VM_PROT_READ;
    234  1.26       cgd 	if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
    235  1.12   mycroft 		prot |= VM_PROT_WRITE;
    236  1.12   mycroft 	flags = MAP_ANON | MAP_SHARED;
    237  1.26       cgd 	if (SCARG(uap, shmaddr)) {
    238  1.12   mycroft 		flags |= MAP_FIXED;
    239  1.26       cgd 		if (SCARG(uap, shmflg) & SHM_RND)
    240  1.26       cgd 			attach_va =
    241  1.26       cgd 			    (vm_offset_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
    242  1.26       cgd 		else if (((vm_offset_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
    243  1.26       cgd 			attach_va = (vm_offset_t)SCARG(uap, shmaddr);
    244  1.11   hpeyerl 		else
    245  1.14   mycroft 			return EINVAL;
    246  1.12   mycroft 	} else {
    247  1.20   mycroft 		/* This is just a hint to vm_mmap() about where to put it. */
    248  1.31       cgd 		attach_va =
    249  1.31       cgd 		    round_page(p->p_vmspace->vm_taddr + MAXTSIZ + MAXDSIZ);
    250  1.11   hpeyerl 	}
    251  1.12   mycroft 	error = vm_mmap(&p->p_vmspace->vm_map, &attach_va, size, prot,
    252  1.26       cgd 	    VM_PROT_DEFAULT, flags, (caddr_t)(long)SCARG(uap, shmid), 0);
    253  1.11   hpeyerl 	if (error)
    254  1.11   hpeyerl 		return error;
    255  1.12   mycroft 	shmmap_s->va = attach_va;
    256  1.26       cgd 	shmmap_s->shmid = SCARG(uap, shmid);
    257  1.11   hpeyerl 	shmseg->shm_lpid = p->p_pid;
    258  1.11   hpeyerl 	shmseg->shm_atime = time.tv_sec;
    259  1.11   hpeyerl 	shmseg->shm_nattch++;
    260  1.12   mycroft 	*retval = attach_va;
    261  1.11   hpeyerl 	return 0;
    262  1.11   hpeyerl }
    263  1.11   hpeyerl 
    264  1.12   mycroft int
    265  1.33   mycroft sys_shmctl(p, v, retval)
    266  1.11   hpeyerl 	struct proc *p;
    267  1.32   thorpej 	void *v;
    268  1.32   thorpej 	register_t *retval;
    269  1.32   thorpej {
    270  1.33   mycroft 	struct sys_shmctl_args /* {
    271  1.26       cgd 		syscallarg(int) shmid;
    272  1.26       cgd 		syscallarg(int) cmd;
    273  1.26       cgd 		syscallarg(struct shmid_ds *) buf;
    274  1.32   thorpej 	} */ *uap = v;
    275  1.35  christos 	int error;
    276  1.12   mycroft 	struct ucred *cred = p->p_ucred;
    277  1.11   hpeyerl 	struct shmid_ds inbuf;
    278  1.11   hpeyerl 	struct shmid_ds *shmseg;
    279  1.11   hpeyerl 
    280  1.26       cgd 	shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
    281  1.11   hpeyerl 	if (shmseg == NULL)
    282  1.11   hpeyerl 		return EINVAL;
    283  1.26       cgd 	switch (SCARG(uap, cmd)) {
    284  1.11   hpeyerl 	case IPC_STAT:
    285  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
    286  1.11   hpeyerl 			return error;
    287  1.35  christos 		error = copyout((caddr_t)shmseg, SCARG(uap, buf),
    288  1.35  christos 				sizeof(inbuf));
    289  1.35  christos 		if (error)
    290  1.11   hpeyerl 			return error;
    291  1.11   hpeyerl 		break;
    292  1.11   hpeyerl 	case IPC_SET:
    293  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    294  1.13   mycroft 			return error;
    295  1.35  christos 		error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
    296  1.35  christos 			       sizeof(inbuf));
    297  1.35  christos 		if (error)
    298  1.11   hpeyerl 			return error;
    299  1.11   hpeyerl 		shmseg->shm_perm.uid = inbuf.shm_perm.uid;
    300  1.11   hpeyerl 		shmseg->shm_perm.gid = inbuf.shm_perm.gid;
    301  1.12   mycroft 		shmseg->shm_perm.mode =
    302  1.12   mycroft 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
    303  1.12   mycroft 		    (inbuf.shm_perm.mode & ACCESSPERMS);
    304  1.12   mycroft 		shmseg->shm_ctime = time.tv_sec;
    305  1.11   hpeyerl 		break;
    306  1.11   hpeyerl 	case IPC_RMID:
    307  1.35  christos 		if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
    308  1.13   mycroft 			return error;
    309  1.12   mycroft 		shmseg->shm_perm.key = IPC_PRIVATE;
    310  1.12   mycroft 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
    311  1.12   mycroft 		if (shmseg->shm_nattch <= 0) {
    312  1.12   mycroft 			shm_deallocate_segment(shmseg);
    313  1.26       cgd 			shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
    314  1.11   hpeyerl 		}
    315  1.11   hpeyerl 		break;
    316  1.11   hpeyerl 	case SHM_LOCK:
    317  1.11   hpeyerl 	case SHM_UNLOCK:
    318  1.11   hpeyerl 	default:
    319  1.11   hpeyerl 		return EINVAL;
    320  1.11   hpeyerl 	}
    321  1.11   hpeyerl 	return 0;
    322  1.11   hpeyerl }
    323  1.11   hpeyerl 
    324  1.12   mycroft static int
    325  1.12   mycroft shmget_existing(p, uap, mode, segnum, retval)
    326  1.11   hpeyerl 	struct proc *p;
    327  1.33   mycroft 	struct sys_shmget_args /* {
    328  1.26       cgd 		syscallarg(key_t) key;
    329  1.26       cgd 		syscallarg(int) size;
    330  1.35  christos 		syscallarg(int) shmflg;
    331  1.26       cgd 	} */ *uap;
    332  1.11   hpeyerl 	int mode;
    333  1.11   hpeyerl 	int segnum;
    334  1.26       cgd 	register_t *retval;
    335  1.11   hpeyerl {
    336  1.12   mycroft 	struct shmid_ds *shmseg;
    337  1.12   mycroft 	struct ucred *cred = p->p_ucred;
    338  1.11   hpeyerl 	int error;
    339  1.11   hpeyerl 
    340  1.11   hpeyerl 	shmseg = &shmsegs[segnum];
    341  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
    342  1.16   mycroft 		/*
    343  1.16   mycroft 		 * This segment is in the process of being allocated.  Wait
    344  1.16   mycroft 		 * until it's done, and look the key up again (in case the
    345  1.16   mycroft 		 * allocation failed or it was freed).
    346  1.16   mycroft 		 */
    347  1.16   mycroft 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
    348  1.35  christos 		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
    349  1.35  christos 		if (error)
    350  1.16   mycroft 			return error;
    351  1.16   mycroft 		return EAGAIN;
    352  1.16   mycroft 	}
    353  1.35  christos 	if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
    354  1.11   hpeyerl 		return error;
    355  1.26       cgd 	if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
    356  1.11   hpeyerl 		return EINVAL;
    357  1.26       cgd 	if (SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL) ==
    358  1.26       cgd 	    (IPC_CREAT | IPC_EXCL))
    359  1.12   mycroft 		return EEXIST;
    360  1.11   hpeyerl 	*retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    361  1.11   hpeyerl 	return 0;
    362  1.11   hpeyerl }
    363  1.11   hpeyerl 
    364  1.14   mycroft static int
    365  1.14   mycroft shmget_allocate_segment(p, uap, mode, retval)
    366  1.14   mycroft 	struct proc *p;
    367  1.33   mycroft 	struct sys_shmget_args /* {
    368  1.26       cgd 		syscallarg(key_t) key;
    369  1.26       cgd 		syscallarg(int) size;
    370  1.35  christos 		syscallarg(int) shmflg;
    371  1.26       cgd 	} */ *uap;
    372  1.14   mycroft 	int mode;
    373  1.26       cgd 	register_t *retval;
    374  1.14   mycroft {
    375  1.14   mycroft 	int i, segnum, result, shmid, size;
    376  1.14   mycroft 	struct ucred *cred = p->p_ucred;
    377  1.14   mycroft 	struct shmid_ds *shmseg;
    378  1.14   mycroft 	struct shm_handle *shm_handle;
    379  1.14   mycroft 
    380  1.26       cgd 	if (SCARG(uap, size) < shminfo.shmmin ||
    381  1.26       cgd 	    SCARG(uap, size) > shminfo.shmmax)
    382  1.14   mycroft 		return EINVAL;
    383  1.14   mycroft 	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
    384  1.14   mycroft 		return ENOSPC;
    385  1.26       cgd 	size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
    386  1.14   mycroft 	if (shm_committed + btoc(size) > shminfo.shmall)
    387  1.14   mycroft 		return ENOMEM;
    388  1.14   mycroft 	if (shm_last_free < 0) {
    389  1.14   mycroft 		for (i = 0; i < shminfo.shmmni; i++)
    390  1.14   mycroft 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
    391  1.14   mycroft 				break;
    392  1.14   mycroft 		if (i == shminfo.shmmni)
    393  1.14   mycroft 			panic("shmseg free count inconsistent");
    394  1.14   mycroft 		segnum = i;
    395  1.14   mycroft 	} else  {
    396  1.14   mycroft 		segnum = shm_last_free;
    397  1.14   mycroft 		shm_last_free = -1;
    398  1.14   mycroft 	}
    399  1.14   mycroft 	shmseg = &shmsegs[segnum];
    400  1.14   mycroft 	/*
    401  1.14   mycroft 	 * In case we sleep in malloc(), mark the segment present but deleted
    402  1.14   mycroft 	 * so that noone else tries to create the same key.
    403  1.14   mycroft 	 */
    404  1.14   mycroft 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
    405  1.26       cgd 	shmseg->shm_perm.key = SCARG(uap, key);
    406  1.14   mycroft 	shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
    407  1.14   mycroft 	shm_handle = (struct shm_handle *)
    408  1.14   mycroft 	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
    409  1.14   mycroft 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
    410  1.14   mycroft 	result = vm_mmap(sysvshm_map, &shm_handle->kva, size, VM_PROT_ALL,
    411  1.26       cgd 	    VM_PROT_DEFAULT, MAP_ANON, (caddr_t)(long)shmid, 0);
    412  1.14   mycroft 	if (result != KERN_SUCCESS) {
    413  1.14   mycroft 		shmseg->shm_perm.mode = SHMSEG_FREE;
    414  1.15   mycroft 		shm_last_free = segnum;
    415  1.14   mycroft 		free((caddr_t)shm_handle, M_SHM);
    416  1.16   mycroft 		/* Just in case. */
    417  1.16   mycroft 		wakeup((caddr_t)shmseg);
    418  1.14   mycroft 		return ENOMEM;
    419  1.14   mycroft 	}
    420  1.14   mycroft 	shmseg->shm_internal = shm_handle;
    421  1.14   mycroft 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
    422  1.14   mycroft 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
    423  1.16   mycroft 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
    424  1.16   mycroft 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
    425  1.26       cgd 	shmseg->shm_segsz = SCARG(uap, size);
    426  1.14   mycroft 	shmseg->shm_cpid = p->p_pid;
    427  1.14   mycroft 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
    428  1.14   mycroft 	shmseg->shm_atime = shmseg->shm_dtime = 0;
    429  1.14   mycroft 	shmseg->shm_ctime = time.tv_sec;
    430  1.14   mycroft 	shm_committed += btoc(size);
    431  1.14   mycroft 	shm_nused++;
    432  1.16   mycroft 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
    433  1.16   mycroft 		/*
    434  1.16   mycroft 		 * Somebody else wanted this key while we were asleep.  Wake
    435  1.16   mycroft 		 * them up now.
    436  1.16   mycroft 		 */
    437  1.16   mycroft 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
    438  1.16   mycroft 		wakeup((caddr_t)shmseg);
    439  1.16   mycroft 	}
    440  1.14   mycroft 	*retval = shmid;
    441  1.14   mycroft 	return 0;
    442  1.14   mycroft }
    443  1.14   mycroft 
    444  1.12   mycroft int
    445  1.33   mycroft sys_shmget(p, v, retval)
    446  1.11   hpeyerl 	struct proc *p;
    447  1.32   thorpej 	void *v;
    448  1.32   thorpej 	register_t *retval;
    449  1.32   thorpej {
    450  1.33   mycroft 	struct sys_shmget_args /* {
    451  1.26       cgd 		syscallarg(key_t) key;
    452  1.26       cgd 		syscallarg(int) size;
    453  1.35  christos 		syscallarg(int) shmflg;
    454  1.32   thorpej 	} */ *uap = v;
    455  1.11   hpeyerl 	int segnum, mode, error;
    456  1.11   hpeyerl 
    457  1.26       cgd 	mode = SCARG(uap, shmflg) & ACCESSPERMS;
    458  1.26       cgd 	if (SCARG(uap, key) != IPC_PRIVATE) {
    459  1.16   mycroft 	again:
    460  1.26       cgd 		segnum = shm_find_segment_by_key(SCARG(uap, key));
    461  1.16   mycroft 		if (segnum >= 0) {
    462  1.16   mycroft 			error = shmget_existing(p, uap, mode, segnum, retval);
    463  1.16   mycroft 			if (error == EAGAIN)
    464  1.16   mycroft 				goto again;
    465  1.16   mycroft 			return error;
    466  1.16   mycroft 		}
    467  1.26       cgd 		if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
    468  1.14   mycroft 			return ENOENT;
    469  1.11   hpeyerl 	}
    470  1.14   mycroft 	return shmget_allocate_segment(p, uap, mode, retval);
    471  1.11   hpeyerl }
    472  1.11   hpeyerl 
    473  1.12   mycroft void
    474  1.34   mycroft shmfork(p1, p2)
    475  1.12   mycroft 	struct proc *p1, *p2;
    476  1.11   hpeyerl {
    477  1.11   hpeyerl 	struct shmmap_state *shmmap_s;
    478  1.12   mycroft 	size_t size;
    479  1.11   hpeyerl 	int i;
    480  1.11   hpeyerl 
    481  1.12   mycroft 	size = shminfo.shmseg * sizeof(struct shmmap_state);
    482  1.12   mycroft 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
    483  1.12   mycroft 	bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
    484  1.12   mycroft 	p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
    485  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    486  1.12   mycroft 		if (shmmap_s->shmid != -1)
    487  1.11   hpeyerl 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
    488  1.11   hpeyerl }
    489  1.11   hpeyerl 
    490  1.12   mycroft void
    491  1.12   mycroft shmexit(p)
    492  1.11   hpeyerl 	struct proc *p;
    493  1.11   hpeyerl {
    494  1.12   mycroft 	struct shmmap_state *shmmap_s;
    495  1.11   hpeyerl 	int i;
    496  1.11   hpeyerl 
    497  1.11   hpeyerl 	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
    498  1.12   mycroft 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
    499  1.12   mycroft 		if (shmmap_s->shmid != -1)
    500  1.12   mycroft 			shm_delete_mapping(p, shmmap_s);
    501  1.12   mycroft 	free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
    502  1.11   hpeyerl 	p->p_vmspace->vm_shm = NULL;
    503  1.11   hpeyerl }
    504  1.11   hpeyerl 
    505  1.12   mycroft void
    506  1.12   mycroft shminit()
    507  1.11   hpeyerl {
    508  1.11   hpeyerl 	int i;
    509  1.11   hpeyerl 	vm_offset_t garbage1, garbage2;
    510  1.24   deraadt 
    511  1.24   deraadt 	shminfo.shmmax *= NBPG;
    512  1.11   hpeyerl 
    513  1.11   hpeyerl 	/* actually this *should* be pageable.  SHM_{LOCK,UNLOCK} */
    514  1.11   hpeyerl 	sysvshm_map = kmem_suballoc(kernel_map, &garbage1, &garbage2,
    515  1.21   mycroft 				    shminfo.shmall * NBPG, TRUE);
    516  1.11   hpeyerl 	for (i = 0; i < shminfo.shmmni; i++) {
    517  1.11   hpeyerl 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
    518  1.11   hpeyerl 		shmsegs[i].shm_perm.seq = 0;
    519  1.11   hpeyerl 	}
    520  1.11   hpeyerl 	shm_last_free = 0;
    521  1.11   hpeyerl 	shm_nused = 0;
    522  1.11   hpeyerl 	shm_committed = 0;
    523  1.11   hpeyerl }
    524