sysv_shm.c revision 57884
1/* $FreeBSD: head/sys/kern/sysv_shm.c 57884 2000-03-10 09:11:24Z alc $ */
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 "opt_compat.h"
35#include "opt_rlimit.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/sysproto.h>
40#include <sys/kernel.h>
41#include <sys/shm.h>
42#include <sys/proc.h>
43#include <sys/malloc.h>
44#include <sys/mman.h>
45#include <sys/stat.h>
46#include <sys/sysent.h>
47
48#include <vm/vm.h>
49#include <vm/vm_param.h>
50#include <sys/lock.h>
51#include <vm/pmap.h>
52#include <vm/vm_object.h>
53#include <vm/vm_map.h>
54#include <vm/vm_page.h>
55#include <vm/vm_pager.h>
56
57#ifndef _SYS_SYSPROTO_H_
58struct shmat_args;
59extern int shmat __P((struct proc *p, struct shmat_args *uap));
60struct shmctl_args;
61extern int shmctl __P((struct proc *p, struct shmctl_args *uap));
62struct shmdt_args;
63extern int shmdt __P((struct proc *p, struct shmdt_args *uap));
64struct shmget_args;
65extern int shmget __P((struct proc *p, struct shmget_args *uap));
66#endif
67
68static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
69
70static void shminit __P((void *));
71SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL)
72
73struct oshmctl_args;
74static int oshmctl __P((struct proc *p, struct oshmctl_args *uap));
75static int shmget_allocate_segment __P((struct proc *p, struct shmget_args *uap, int mode));
76static int shmget_existing __P((struct proc *p, struct shmget_args *uap, int mode, int segnum));
77
78/* XXX casting to (sy_call_t *) is bogus, as usual. */
79static sy_call_t *shmcalls[] = {
80	(sy_call_t *)shmat, (sy_call_t *)oshmctl,
81	(sy_call_t *)shmdt, (sy_call_t *)shmget,
82	(sy_call_t *)shmctl
83};
84
85#define	SHMSEG_FREE     	0x0200
86#define	SHMSEG_REMOVED  	0x0400
87#define	SHMSEG_ALLOCATED	0x0800
88#define	SHMSEG_WANTED		0x1000
89
90static int shm_last_free, shm_nused, shm_committed;
91struct shmid_ds	*shmsegs;
92
93struct shm_handle {
94	/* vm_offset_t kva; */
95	vm_object_t shm_object;
96};
97
98struct shmmap_state {
99	vm_offset_t va;
100	int shmid;
101};
102
103static void shm_deallocate_segment __P((struct shmid_ds *));
104static int shm_find_segment_by_key __P((key_t));
105static struct shmid_ds *shm_find_segment_by_shmid __P((int));
106static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
107
108static int
109shm_find_segment_by_key(key)
110	key_t key;
111{
112	int i;
113
114	for (i = 0; i < shminfo.shmmni; i++)
115		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
116		    shmsegs[i].shm_perm.key == key)
117			return i;
118	return -1;
119}
120
121static struct shmid_ds *
122shm_find_segment_by_shmid(shmid)
123	int shmid;
124{
125	int segnum;
126	struct shmid_ds *shmseg;
127
128	segnum = IPCID_TO_IX(shmid);
129	if (segnum < 0 || segnum >= shminfo.shmmni)
130		return NULL;
131	shmseg = &shmsegs[segnum];
132	if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
133	    != SHMSEG_ALLOCATED ||
134	    shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
135		return NULL;
136	return shmseg;
137}
138
139static void
140shm_deallocate_segment(shmseg)
141	struct shmid_ds *shmseg;
142{
143	struct shm_handle *shm_handle;
144	size_t size;
145
146	shm_handle = shmseg->shm_internal;
147	vm_object_deallocate(shm_handle->shm_object);
148	free((caddr_t)shm_handle, M_SHM);
149	shmseg->shm_internal = NULL;
150	size = round_page(shmseg->shm_segsz);
151	shm_committed -= btoc(size);
152	shm_nused--;
153	shmseg->shm_perm.mode = SHMSEG_FREE;
154}
155
156static int
157shm_delete_mapping(p, shmmap_s)
158	struct proc *p;
159	struct shmmap_state *shmmap_s;
160{
161	struct shmid_ds *shmseg;
162	int segnum, result;
163	size_t size;
164
165	segnum = IPCID_TO_IX(shmmap_s->shmid);
166	shmseg = &shmsegs[segnum];
167	size = round_page(shmseg->shm_segsz);
168	result = vm_map_remove(&p->p_vmspace->vm_map, shmmap_s->va, shmmap_s->va + size);
169	if (result != KERN_SUCCESS)
170		return EINVAL;
171	shmmap_s->shmid = -1;
172	shmseg->shm_dtime = time_second;
173	if ((--shmseg->shm_nattch <= 0) &&
174	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
175		shm_deallocate_segment(shmseg);
176		shm_last_free = segnum;
177	}
178	return 0;
179}
180
181#ifndef _SYS_SYSPROTO_H_
182struct shmdt_args {
183	void *shmaddr;
184};
185#endif
186
187int
188shmdt(p, uap)
189	struct proc *p;
190	struct shmdt_args *uap;
191{
192	struct shmmap_state *shmmap_s;
193	int i;
194
195	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
196 	if (shmmap_s == NULL)
197 	    return EINVAL;
198	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
199		if (shmmap_s->shmid != -1 &&
200		    shmmap_s->va == (vm_offset_t)uap->shmaddr)
201			break;
202	if (i == shminfo.shmseg)
203		return EINVAL;
204	return shm_delete_mapping(p, shmmap_s);
205}
206
207#ifndef _SYS_SYSPROTO_H_
208struct shmat_args {
209	int shmid;
210	void *shmaddr;
211	int shmflg;
212};
213#endif
214
215int
216shmat(p, uap)
217	struct proc *p;
218	struct shmat_args *uap;
219{
220	int error, i, flags;
221	struct shmid_ds *shmseg;
222	struct shmmap_state *shmmap_s = NULL;
223	struct shm_handle *shm_handle;
224	vm_offset_t attach_va;
225	vm_prot_t prot;
226	vm_size_t size;
227	int rv;
228
229	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
230	if (shmmap_s == NULL) {
231		size = shminfo.shmseg * sizeof(struct shmmap_state);
232		shmmap_s = malloc(size, M_SHM, M_WAITOK);
233		for (i = 0; i < shminfo.shmseg; i++)
234			shmmap_s[i].shmid = -1;
235		p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
236	}
237	shmseg = shm_find_segment_by_shmid(uap->shmid);
238	if (shmseg == NULL)
239		return EINVAL;
240	error = ipcperm(p, &shmseg->shm_perm,
241	    (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
242	if (error)
243		return error;
244	for (i = 0; i < shminfo.shmseg; i++) {
245		if (shmmap_s->shmid == -1)
246			break;
247		shmmap_s++;
248	}
249	if (i >= shminfo.shmseg)
250		return EMFILE;
251	size = round_page(shmseg->shm_segsz);
252#ifdef VM_PROT_READ_IS_EXEC
253	prot = VM_PROT_READ | VM_PROT_EXECUTE;
254#else
255	prot = VM_PROT_READ;
256#endif
257	if ((uap->shmflg & SHM_RDONLY) == 0)
258		prot |= VM_PROT_WRITE;
259	flags = MAP_ANON | MAP_SHARED;
260	if (uap->shmaddr) {
261		flags |= MAP_FIXED;
262		if (uap->shmflg & SHM_RND)
263			attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
264		else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
265			attach_va = (vm_offset_t)uap->shmaddr;
266		else
267			return EINVAL;
268	} else {
269		/* This is just a hint to vm_map_find() about where to put it. */
270		attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + MAXTSIZ + MAXDSIZ);
271	}
272
273	shm_handle = shmseg->shm_internal;
274	vm_object_reference(shm_handle->shm_object);
275	rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
276		0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
277	if (rv != KERN_SUCCESS) {
278		return ENOMEM;
279	}
280	vm_map_inherit(&p->p_vmspace->vm_map,
281		attach_va, attach_va + size, VM_INHERIT_SHARE);
282
283	shmmap_s->va = attach_va;
284	shmmap_s->shmid = uap->shmid;
285	shmseg->shm_lpid = p->p_pid;
286	shmseg->shm_atime = time_second;
287	shmseg->shm_nattch++;
288	p->p_retval[0] = attach_va;
289	return 0;
290}
291
292struct oshmid_ds {
293	struct	ipc_perm shm_perm;	/* operation perms */
294	int	shm_segsz;		/* size of segment (bytes) */
295	ushort	shm_cpid;		/* pid, creator */
296	ushort	shm_lpid;		/* pid, last operation */
297	short	shm_nattch;		/* no. of current attaches */
298	time_t	shm_atime;		/* last attach time */
299	time_t	shm_dtime;		/* last detach time */
300	time_t	shm_ctime;		/* last change time */
301	void	*shm_handle;		/* internal handle for shm segment */
302};
303
304struct oshmctl_args {
305	int shmid;
306	int cmd;
307	struct oshmid_ds *ubuf;
308};
309
310static int
311oshmctl(p, uap)
312	struct proc *p;
313	struct oshmctl_args *uap;
314{
315#ifdef COMPAT_43
316	int error;
317	struct shmid_ds *shmseg;
318	struct oshmid_ds outbuf;
319
320	shmseg = shm_find_segment_by_shmid(uap->shmid);
321	if (shmseg == NULL)
322		return EINVAL;
323	switch (uap->cmd) {
324	case IPC_STAT:
325		error = ipcperm(p, &shmseg->shm_perm, IPC_R);
326		if (error)
327			return error;
328		outbuf.shm_perm = shmseg->shm_perm;
329		outbuf.shm_segsz = shmseg->shm_segsz;
330		outbuf.shm_cpid = shmseg->shm_cpid;
331		outbuf.shm_lpid = shmseg->shm_lpid;
332		outbuf.shm_nattch = shmseg->shm_nattch;
333		outbuf.shm_atime = shmseg->shm_atime;
334		outbuf.shm_dtime = shmseg->shm_dtime;
335		outbuf.shm_ctime = shmseg->shm_ctime;
336		outbuf.shm_handle = shmseg->shm_internal;
337		error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
338		if (error)
339			return error;
340		break;
341	default:
342		/* XXX casting to (sy_call_t *) is bogus, as usual. */
343		return ((sy_call_t *)shmctl)(p, uap);
344	}
345	return 0;
346#else
347	return EINVAL;
348#endif
349}
350
351#ifndef _SYS_SYSPROTO_H_
352struct shmctl_args {
353	int shmid;
354	int cmd;
355	struct shmid_ds *buf;
356};
357#endif
358
359int
360shmctl(p, uap)
361	struct proc *p;
362	struct shmctl_args *uap;
363{
364	int error;
365	struct shmid_ds inbuf;
366	struct shmid_ds *shmseg;
367
368	shmseg = shm_find_segment_by_shmid(uap->shmid);
369	if (shmseg == NULL)
370		return EINVAL;
371	switch (uap->cmd) {
372	case IPC_STAT:
373		error = ipcperm(p, &shmseg->shm_perm, IPC_R);
374		if (error)
375			return error;
376		error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
377		if (error)
378			return error;
379		break;
380	case IPC_SET:
381		error = ipcperm(p, &shmseg->shm_perm, IPC_M);
382		if (error)
383			return error;
384		error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
385		if (error)
386			return error;
387		shmseg->shm_perm.uid = inbuf.shm_perm.uid;
388		shmseg->shm_perm.gid = inbuf.shm_perm.gid;
389		shmseg->shm_perm.mode =
390		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
391		    (inbuf.shm_perm.mode & ACCESSPERMS);
392		shmseg->shm_ctime = time_second;
393		break;
394	case IPC_RMID:
395		error = ipcperm(p, &shmseg->shm_perm, IPC_M);
396		if (error)
397			return error;
398		shmseg->shm_perm.key = IPC_PRIVATE;
399		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
400		if (shmseg->shm_nattch <= 0) {
401			shm_deallocate_segment(shmseg);
402			shm_last_free = IPCID_TO_IX(uap->shmid);
403		}
404		break;
405#if 0
406	case SHM_LOCK:
407	case SHM_UNLOCK:
408#endif
409	default:
410		return EINVAL;
411	}
412	return 0;
413}
414
415#ifndef _SYS_SYSPROTO_H_
416struct shmget_args {
417	key_t key;
418	size_t size;
419	int shmflg;
420};
421#endif
422
423static int
424shmget_existing(p, uap, mode, segnum)
425	struct proc *p;
426	struct shmget_args *uap;
427	int mode;
428	int segnum;
429{
430	struct shmid_ds *shmseg;
431	int error;
432
433	shmseg = &shmsegs[segnum];
434	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
435		/*
436		 * This segment is in the process of being allocated.  Wait
437		 * until it's done, and look the key up again (in case the
438		 * allocation failed or it was freed).
439		 */
440		shmseg->shm_perm.mode |= SHMSEG_WANTED;
441		error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
442		if (error)
443			return error;
444		return EAGAIN;
445	}
446	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
447		return EEXIST;
448	error = ipcperm(p, &shmseg->shm_perm, mode);
449	if (error)
450		return error;
451	if (uap->size && uap->size > shmseg->shm_segsz)
452		return EINVAL;
453	p->p_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
454	return 0;
455}
456
457static int
458shmget_allocate_segment(p, uap, mode)
459	struct proc *p;
460	struct shmget_args *uap;
461	int mode;
462{
463	int i, segnum, shmid, size;
464	struct ucred *cred = p->p_ucred;
465	struct shmid_ds *shmseg;
466	struct shm_handle *shm_handle;
467
468	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
469		return EINVAL;
470	if (shm_nused >= shminfo.shmmni) /* any shmids left? */
471		return ENOSPC;
472	size = round_page(uap->size);
473	if (shm_committed + btoc(size) > shminfo.shmall)
474		return ENOMEM;
475	if (shm_last_free < 0) {
476		for (i = 0; i < shminfo.shmmni; i++)
477			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
478				break;
479		if (i == shminfo.shmmni)
480			panic("shmseg free count inconsistent");
481		segnum = i;
482	} else  {
483		segnum = shm_last_free;
484		shm_last_free = -1;
485	}
486	shmseg = &shmsegs[segnum];
487	/*
488	 * In case we sleep in malloc(), mark the segment present but deleted
489	 * so that noone else tries to create the same key.
490	 */
491	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
492	shmseg->shm_perm.key = uap->key;
493	shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
494	shm_handle = (struct shm_handle *)
495	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
496	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
497
498	/*
499	 * We make sure that we have allocated a pager before we need
500	 * to.
501	 */
502	shm_handle->shm_object =
503		vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
504	vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
505	vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
506
507	shmseg->shm_internal = shm_handle;
508	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
509	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
510	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
511	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
512	shmseg->shm_segsz = uap->size;
513	shmseg->shm_cpid = p->p_pid;
514	shmseg->shm_lpid = shmseg->shm_nattch = 0;
515	shmseg->shm_atime = shmseg->shm_dtime = 0;
516	shmseg->shm_ctime = time_second;
517	shm_committed += btoc(size);
518	shm_nused++;
519	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
520		/*
521		 * Somebody else wanted this key while we were asleep.  Wake
522		 * them up now.
523		 */
524		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
525		wakeup((caddr_t)shmseg);
526	}
527	p->p_retval[0] = shmid;
528	return 0;
529}
530
531int
532shmget(p, uap)
533	struct proc *p;
534	struct shmget_args *uap;
535{
536	int segnum, mode, error;
537
538	mode = uap->shmflg & ACCESSPERMS;
539	if (uap->key != IPC_PRIVATE) {
540	again:
541		segnum = shm_find_segment_by_key(uap->key);
542		if (segnum >= 0) {
543			error = shmget_existing(p, uap, mode, segnum);
544			if (error == EAGAIN)
545				goto again;
546			return error;
547		}
548		if ((uap->shmflg & IPC_CREAT) == 0)
549			return ENOENT;
550	}
551	return shmget_allocate_segment(p, uap, mode);
552}
553
554int
555shmsys(p, uap)
556	struct proc *p;
557	/* XXX actually varargs. */
558	struct shmsys_args /* {
559		u_int	which;
560		int	a2;
561		int	a3;
562		int	a4;
563	} */ *uap;
564{
565
566	if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
567		return EINVAL;
568	return ((*shmcalls[uap->which])(p, &uap->a2));
569}
570
571void
572shmfork(p1, p2)
573	struct proc *p1, *p2;
574{
575	struct shmmap_state *shmmap_s;
576	size_t size;
577	int i;
578
579	size = shminfo.shmseg * sizeof(struct shmmap_state);
580	shmmap_s = malloc(size, M_SHM, M_WAITOK);
581	bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
582	p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
583	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
584		if (shmmap_s->shmid != -1)
585			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
586}
587
588void
589shmexit(p)
590	struct proc *p;
591{
592	struct shmmap_state *shmmap_s;
593	int i;
594
595	shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
596	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
597		if (shmmap_s->shmid != -1)
598			shm_delete_mapping(p, shmmap_s);
599	free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
600	p->p_vmspace->vm_shm = NULL;
601}
602
603void
604shminit(dummy)
605	void *dummy;
606{
607	int i;
608	for (i = 0; i < shminfo.shmmni; i++) {
609		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
610		shmsegs[i].shm_perm.seq = 0;
611	}
612	shm_last_free = 0;
613	shm_nused = 0;
614	shm_committed = 0;
615}
616