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