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