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