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