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