sysv_shm.c revision 194832
1/*	$NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $	*/
2/*-
3 * Copyright (c) 1994 Adam Glass and Charles Hannum.  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. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Adam Glass and Charles
16 *	Hannum.
17 * 4. The names of the authors may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*-
32 * Copyright (c) 2003-2005 McAfee, Inc.
33 * All rights reserved.
34 *
35 * This software was developed for the FreeBSD Project in part by McAfee
36 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
37 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
38 * program.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 *    notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 *    notice, this list of conditions and the following disclaimer in the
47 *    documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 */
61
62#include <sys/cdefs.h>
63__FBSDID("$FreeBSD: head/sys/kern/sysv_shm.c 194832 2009-06-24 13:35:38Z jhb $");
64
65#include "opt_compat.h"
66#include "opt_sysvipc.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/kernel.h>
71#include <sys/lock.h>
72#include <sys/sysctl.h>
73#include <sys/shm.h>
74#include <sys/proc.h>
75#include <sys/malloc.h>
76#include <sys/mman.h>
77#include <sys/module.h>
78#include <sys/mutex.h>
79#include <sys/resourcevar.h>
80#include <sys/stat.h>
81#include <sys/syscall.h>
82#include <sys/syscallsubr.h>
83#include <sys/sysent.h>
84#include <sys/sysproto.h>
85#include <sys/jail.h>
86
87#include <security/mac/mac_framework.h>
88
89#include <vm/vm.h>
90#include <vm/vm_param.h>
91#include <vm/pmap.h>
92#include <vm/vm_object.h>
93#include <vm/vm_map.h>
94#include <vm/vm_page.h>
95#include <vm/vm_pager.h>
96
97static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
98
99#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
100struct oshmctl_args;
101static int oshmctl(struct thread *td, struct oshmctl_args *uap);
102#endif
103
104static int shmget_allocate_segment(struct thread *td,
105    struct shmget_args *uap, int mode);
106static int shmget_existing(struct thread *td, struct shmget_args *uap,
107    int mode, int segnum);
108
109#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
110/* XXX casting to (sy_call_t *) is bogus, as usual. */
111static sy_call_t *shmcalls[] = {
112	(sy_call_t *)shmat, (sy_call_t *)oshmctl,
113	(sy_call_t *)shmdt, (sy_call_t *)shmget,
114	(sy_call_t *)shmctl
115};
116#endif
117
118#define	SHMSEG_FREE     	0x0200
119#define	SHMSEG_REMOVED  	0x0400
120#define	SHMSEG_ALLOCATED	0x0800
121#define	SHMSEG_WANTED		0x1000
122
123static int shm_last_free, shm_nused, shmalloced;
124vm_size_t shm_committed;
125static struct shmid_kernel	*shmsegs;
126
127struct shmmap_state {
128	vm_offset_t va;
129	int shmid;
130};
131
132static void shm_deallocate_segment(struct shmid_kernel *);
133static int shm_find_segment_by_key(key_t);
134static struct shmid_kernel *shm_find_segment_by_shmid(int);
135static struct shmid_kernel *shm_find_segment_by_shmidx(int);
136static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
137static void shmrealloc(void);
138static void shminit(void);
139static int sysvshm_modload(struct module *, int, void *);
140static int shmunload(void);
141static void shmexit_myhook(struct vmspace *vm);
142static void shmfork_myhook(struct proc *p1, struct proc *p2);
143static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
144
145/*
146 * Tuneable values.
147 */
148#ifndef SHMMAXPGS
149#define	SHMMAXPGS	8192	/* Note: sysv shared memory is swap backed. */
150#endif
151#ifndef SHMMAX
152#define	SHMMAX	(SHMMAXPGS*PAGE_SIZE)
153#endif
154#ifndef SHMMIN
155#define	SHMMIN	1
156#endif
157#ifndef SHMMNI
158#define	SHMMNI	192
159#endif
160#ifndef SHMSEG
161#define	SHMSEG	128
162#endif
163#ifndef SHMALL
164#define	SHMALL	(SHMMAXPGS)
165#endif
166
167struct	shminfo shminfo = {
168	SHMMAX,
169	SHMMIN,
170	SHMMNI,
171	SHMSEG,
172	SHMALL
173};
174
175static int shm_use_phys;
176static int shm_allow_removed;
177
178SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
179    "Maximum shared memory segment size");
180SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
181    "Minimum shared memory segment size");
182SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
183    "Number of shared memory identifiers");
184SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
185    "Number of segments per process");
186SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
187    "Maximum number of pages available for shared memory");
188SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
189    &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
190SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
191    &shm_allow_removed, 0,
192    "Enable/Disable attachment to attached segments marked for removal");
193SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
194    NULL, 0, sysctl_shmsegs, "",
195    "Current number of shared memory segments allocated");
196
197static int
198shm_find_segment_by_key(key)
199	key_t key;
200{
201	int i;
202
203	for (i = 0; i < shmalloced; i++)
204		if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
205		    shmsegs[i].u.shm_perm.key == key)
206			return (i);
207	return (-1);
208}
209
210static struct shmid_kernel *
211shm_find_segment_by_shmid(int shmid)
212{
213	int segnum;
214	struct shmid_kernel *shmseg;
215
216	segnum = IPCID_TO_IX(shmid);
217	if (segnum < 0 || segnum >= shmalloced)
218		return (NULL);
219	shmseg = &shmsegs[segnum];
220	if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
221	    (!shm_allow_removed &&
222	     (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
223	    shmseg->u.shm_perm.seq != IPCID_TO_SEQ(shmid))
224		return (NULL);
225	return (shmseg);
226}
227
228static struct shmid_kernel *
229shm_find_segment_by_shmidx(int segnum)
230{
231	struct shmid_kernel *shmseg;
232
233	if (segnum < 0 || segnum >= shmalloced)
234		return (NULL);
235	shmseg = &shmsegs[segnum];
236	if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
237	    (!shm_allow_removed &&
238	     (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0))
239		return (NULL);
240	return (shmseg);
241}
242
243static void
244shm_deallocate_segment(shmseg)
245	struct shmid_kernel *shmseg;
246{
247	vm_size_t size;
248
249	GIANT_REQUIRED;
250
251	vm_object_deallocate(shmseg->u.shm_internal);
252	shmseg->u.shm_internal = NULL;
253	size = round_page(shmseg->shm_bsegsz);
254	shm_committed -= btoc(size);
255	shm_nused--;
256	shmseg->u.shm_perm.mode = SHMSEG_FREE;
257#ifdef MAC
258	mac_sysvshm_cleanup(shmseg);
259#endif
260}
261
262static int
263shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
264{
265	struct shmid_kernel *shmseg;
266	int segnum, result;
267	vm_size_t size;
268
269	GIANT_REQUIRED;
270
271	segnum = IPCID_TO_IX(shmmap_s->shmid);
272	shmseg = &shmsegs[segnum];
273	size = round_page(shmseg->shm_bsegsz);
274	result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
275	if (result != KERN_SUCCESS)
276		return (EINVAL);
277	shmmap_s->shmid = -1;
278	shmseg->u.shm_dtime = time_second;
279	if ((--shmseg->u.shm_nattch <= 0) &&
280	    (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
281		shm_deallocate_segment(shmseg);
282		shm_last_free = segnum;
283	}
284	return (0);
285}
286
287#ifndef _SYS_SYSPROTO_H_
288struct shmdt_args {
289	const void *shmaddr;
290};
291#endif
292int
293shmdt(td, uap)
294	struct thread *td;
295	struct shmdt_args *uap;
296{
297	struct proc *p = td->td_proc;
298	struct shmmap_state *shmmap_s;
299#ifdef MAC
300	struct shmid_kernel *shmsegptr;
301#endif
302	int i;
303	int error = 0;
304
305	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
306		return (ENOSYS);
307	mtx_lock(&Giant);
308	shmmap_s = p->p_vmspace->vm_shm;
309 	if (shmmap_s == NULL) {
310		error = EINVAL;
311		goto done2;
312	}
313	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
314		if (shmmap_s->shmid != -1 &&
315		    shmmap_s->va == (vm_offset_t)uap->shmaddr) {
316			break;
317		}
318	}
319	if (i == shminfo.shmseg) {
320		error = EINVAL;
321		goto done2;
322	}
323#ifdef MAC
324	shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
325	error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
326	if (error != 0)
327		goto done2;
328#endif
329	error = shm_delete_mapping(p->p_vmspace, shmmap_s);
330done2:
331	mtx_unlock(&Giant);
332	return (error);
333}
334
335#ifndef _SYS_SYSPROTO_H_
336struct shmat_args {
337	int shmid;
338	const void *shmaddr;
339	int shmflg;
340};
341#endif
342int
343kern_shmat(td, shmid, shmaddr, shmflg)
344	struct thread *td;
345	int shmid;
346	const void *shmaddr;
347	int shmflg;
348{
349	struct proc *p = td->td_proc;
350	int i, flags;
351	struct shmid_kernel *shmseg;
352	struct shmmap_state *shmmap_s = NULL;
353	vm_offset_t attach_va;
354	vm_prot_t prot;
355	vm_size_t size;
356	int rv;
357	int error = 0;
358
359	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
360		return (ENOSYS);
361	mtx_lock(&Giant);
362	shmmap_s = p->p_vmspace->vm_shm;
363	if (shmmap_s == NULL) {
364		shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
365		    M_SHM, M_WAITOK);
366		for (i = 0; i < shminfo.shmseg; i++)
367			shmmap_s[i].shmid = -1;
368		p->p_vmspace->vm_shm = shmmap_s;
369	}
370	shmseg = shm_find_segment_by_shmid(shmid);
371	if (shmseg == NULL) {
372		error = EINVAL;
373		goto done2;
374	}
375	error = ipcperm(td, &shmseg->u.shm_perm,
376	    (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
377	if (error)
378		goto done2;
379#ifdef MAC
380	error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
381	if (error != 0)
382		goto done2;
383#endif
384	for (i = 0; i < shminfo.shmseg; i++) {
385		if (shmmap_s->shmid == -1)
386			break;
387		shmmap_s++;
388	}
389	if (i >= shminfo.shmseg) {
390		error = EMFILE;
391		goto done2;
392	}
393	size = round_page(shmseg->shm_bsegsz);
394	prot = VM_PROT_READ;
395	if ((shmflg & SHM_RDONLY) == 0)
396		prot |= VM_PROT_WRITE;
397	flags = MAP_ANON | MAP_SHARED;
398	if (shmaddr) {
399		flags |= MAP_FIXED;
400		if (shmflg & SHM_RND) {
401			attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
402		} else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
403			attach_va = (vm_offset_t)shmaddr;
404		} else {
405			error = EINVAL;
406			goto done2;
407		}
408	} else {
409		/*
410		 * This is just a hint to vm_map_find() about where to
411		 * put it.
412		 */
413		PROC_LOCK(p);
414		attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
415		    lim_max(p, RLIMIT_DATA));
416		PROC_UNLOCK(p);
417	}
418
419	vm_object_reference(shmseg->u.shm_internal);
420	rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->u.shm_internal,
421	    0, &attach_va, size, (flags & MAP_FIXED) ? VMFS_NO_SPACE :
422	    VMFS_ANY_SPACE, prot, prot, 0);
423	if (rv != KERN_SUCCESS) {
424		vm_object_deallocate(shmseg->u.shm_internal);
425		error = ENOMEM;
426		goto done2;
427	}
428	vm_map_inherit(&p->p_vmspace->vm_map,
429		attach_va, attach_va + size, VM_INHERIT_SHARE);
430
431	shmmap_s->va = attach_va;
432	shmmap_s->shmid = shmid;
433	shmseg->u.shm_lpid = p->p_pid;
434	shmseg->u.shm_atime = time_second;
435	shmseg->u.shm_nattch++;
436	td->td_retval[0] = attach_va;
437done2:
438	mtx_unlock(&Giant);
439	return (error);
440}
441
442int
443shmat(td, uap)
444	struct thread *td;
445	struct shmat_args *uap;
446{
447	return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
448}
449
450#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
451struct oshmid_ds {
452	struct	ipc_perm shm_perm;	/* operation perms */
453	int	shm_segsz;		/* size of segment (bytes) */
454	u_short	shm_cpid;		/* pid, creator */
455	u_short	shm_lpid;		/* pid, last operation */
456	short	shm_nattch;		/* no. of current attaches */
457	time_t	shm_atime;		/* last attach time */
458	time_t	shm_dtime;		/* last detach time */
459	time_t	shm_ctime;		/* last change time */
460	void	*shm_handle;		/* internal handle for shm segment */
461};
462
463struct oshmctl_args {
464	int shmid;
465	int cmd;
466	struct oshmid_ds *ubuf;
467};
468static int
469oshmctl(td, uap)
470	struct thread *td;
471	struct oshmctl_args *uap;
472{
473#ifdef COMPAT_43
474	int error = 0;
475	struct shmid_kernel *shmseg;
476	struct oshmid_ds outbuf;
477
478	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
479		return (ENOSYS);
480	mtx_lock(&Giant);
481	shmseg = shm_find_segment_by_shmid(uap->shmid);
482	if (shmseg == NULL) {
483		error = EINVAL;
484		goto done2;
485	}
486	switch (uap->cmd) {
487	case IPC_STAT:
488		error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
489		if (error)
490			goto done2;
491#ifdef MAC
492		error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
493		if (error != 0)
494			goto done2;
495#endif
496		outbuf.shm_perm = shmseg->u.shm_perm;
497		outbuf.shm_segsz = shmseg->u.shm_segsz;
498		outbuf.shm_cpid = shmseg->u.shm_cpid;
499		outbuf.shm_lpid = shmseg->u.shm_lpid;
500		outbuf.shm_nattch = shmseg->u.shm_nattch;
501		outbuf.shm_atime = shmseg->u.shm_atime;
502		outbuf.shm_dtime = shmseg->u.shm_dtime;
503		outbuf.shm_ctime = shmseg->u.shm_ctime;
504		outbuf.shm_handle = shmseg->u.shm_internal;
505		error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
506		if (error)
507			goto done2;
508		break;
509	default:
510		error = shmctl(td, (struct shmctl_args *)uap);
511		break;
512	}
513done2:
514	mtx_unlock(&Giant);
515	return (error);
516#else
517	return (EINVAL);
518#endif
519}
520#endif
521
522int
523kern_shmctl(td, shmid, cmd, buf, bufsz)
524	struct thread *td;
525	int shmid;
526	int cmd;
527	void *buf;
528	size_t *bufsz;
529{
530	int error = 0;
531	struct shmid_kernel *shmseg;
532
533	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
534		return (ENOSYS);
535
536	mtx_lock(&Giant);
537	switch (cmd) {
538	/*
539	 * It is possible that kern_shmctl is being called from the Linux ABI
540	 * layer, in which case, we will need to implement IPC_INFO.  It should
541	 * be noted that other shmctl calls will be funneled through here for
542	 * Linix binaries as well.
543	 *
544	 * NB: The Linux ABI layer will convert this data to structure(s) more
545	 * consistent with the Linux ABI.
546	 */
547	case IPC_INFO:
548		memcpy(buf, &shminfo, sizeof(shminfo));
549		if (bufsz)
550			*bufsz = sizeof(shminfo);
551		td->td_retval[0] = shmalloced;
552		goto done2;
553	case SHM_INFO: {
554		struct shm_info shm_info;
555		shm_info.used_ids = shm_nused;
556		shm_info.shm_rss = 0;	/*XXX where to get from ? */
557		shm_info.shm_tot = 0;	/*XXX where to get from ? */
558		shm_info.shm_swp = 0;	/*XXX where to get from ? */
559		shm_info.swap_attempts = 0;	/*XXX where to get from ? */
560		shm_info.swap_successes = 0;	/*XXX where to get from ? */
561		memcpy(buf, &shm_info, sizeof(shm_info));
562		if (bufsz)
563			*bufsz = sizeof(shm_info);
564		td->td_retval[0] = shmalloced;
565		goto done2;
566	}
567	}
568	if (cmd == SHM_STAT)
569		shmseg = shm_find_segment_by_shmidx(shmid);
570	else
571		shmseg = shm_find_segment_by_shmid(shmid);
572	if (shmseg == NULL) {
573		error = EINVAL;
574		goto done2;
575	}
576#ifdef MAC
577	error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
578	if (error != 0)
579		goto done2;
580#endif
581	switch (cmd) {
582	case SHM_STAT:
583	case IPC_STAT:
584		error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
585		if (error)
586			goto done2;
587		memcpy(buf, &shmseg->u, sizeof(struct shmid_ds));
588		if (bufsz)
589			*bufsz = sizeof(struct shmid_ds);
590		if (cmd == SHM_STAT)
591			td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->u.shm_perm);
592		break;
593	case IPC_SET: {
594		struct shmid_ds *shmid;
595
596		shmid = (struct shmid_ds *)buf;
597		error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
598		if (error)
599			goto done2;
600		shmseg->u.shm_perm.uid = shmid->shm_perm.uid;
601		shmseg->u.shm_perm.gid = shmid->shm_perm.gid;
602		shmseg->u.shm_perm.mode =
603		    (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
604		    (shmid->shm_perm.mode & ACCESSPERMS);
605		shmseg->u.shm_ctime = time_second;
606		break;
607	}
608	case IPC_RMID:
609		error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
610		if (error)
611			goto done2;
612		shmseg->u.shm_perm.key = IPC_PRIVATE;
613		shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
614		if (shmseg->u.shm_nattch <= 0) {
615			shm_deallocate_segment(shmseg);
616			shm_last_free = IPCID_TO_IX(shmid);
617		}
618		break;
619#if 0
620	case SHM_LOCK:
621	case SHM_UNLOCK:
622#endif
623	default:
624		error = EINVAL;
625		break;
626	}
627done2:
628	mtx_unlock(&Giant);
629	return (error);
630}
631
632#ifndef _SYS_SYSPROTO_H_
633struct shmctl_args {
634	int shmid;
635	int cmd;
636	struct shmid_ds *buf;
637};
638#endif
639int
640shmctl(td, uap)
641	struct thread *td;
642	struct shmctl_args *uap;
643{
644	int error = 0;
645	struct shmid_ds buf;
646	size_t bufsz;
647
648	/*
649	 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
650	 * Linux binaries.  If we see the call come through the FreeBSD ABI,
651	 * return an error back to the user since we do not to support this.
652	 */
653	if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
654	    uap->cmd == SHM_STAT)
655		return (EINVAL);
656
657	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
658	if (uap->cmd == IPC_SET) {
659		if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
660			goto done;
661	}
662
663	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
664	if (error)
665		goto done;
666
667	/* Cases in which we need to copyout */
668	switch (uap->cmd) {
669	case IPC_STAT:
670		error = copyout(&buf, uap->buf, bufsz);
671		break;
672	}
673
674done:
675	if (error) {
676		/* Invalidate the return value */
677		td->td_retval[0] = -1;
678	}
679	return (error);
680}
681
682
683static int
684shmget_existing(td, uap, mode, segnum)
685	struct thread *td;
686	struct shmget_args *uap;
687	int mode;
688	int segnum;
689{
690	struct shmid_kernel *shmseg;
691	int error;
692
693	shmseg = &shmsegs[segnum];
694	if (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) {
695		/*
696		 * This segment is in the process of being allocated.  Wait
697		 * until it's done, and look the key up again (in case the
698		 * allocation failed or it was freed).
699		 */
700		shmseg->u.shm_perm.mode |= SHMSEG_WANTED;
701		error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
702		if (error)
703			return (error);
704		return (EAGAIN);
705	}
706	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
707		return (EEXIST);
708#ifdef MAC
709	error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
710	if (error != 0)
711		return (error);
712#endif
713	if (uap->size != 0 && uap->size > shmseg->shm_bsegsz)
714		return (EINVAL);
715	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
716	return (0);
717}
718
719static int
720shmget_allocate_segment(td, uap, mode)
721	struct thread *td;
722	struct shmget_args *uap;
723	int mode;
724{
725	int i, segnum, shmid;
726	size_t size;
727	struct ucred *cred = td->td_ucred;
728	struct shmid_kernel *shmseg;
729	vm_object_t shm_object;
730
731	GIANT_REQUIRED;
732
733	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
734		return (EINVAL);
735	if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
736		return (ENOSPC);
737	size = round_page(uap->size);
738	if (shm_committed + btoc(size) > shminfo.shmall)
739		return (ENOMEM);
740	if (shm_last_free < 0) {
741		shmrealloc();	/* Maybe expand the shmsegs[] array. */
742		for (i = 0; i < shmalloced; i++)
743			if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
744				break;
745		if (i == shmalloced)
746			return (ENOSPC);
747		segnum = i;
748	} else  {
749		segnum = shm_last_free;
750		shm_last_free = -1;
751	}
752	shmseg = &shmsegs[segnum];
753	/*
754	 * In case we sleep in malloc(), mark the segment present but deleted
755	 * so that noone else tries to create the same key.
756	 */
757	shmseg->u.shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
758	shmseg->u.shm_perm.key = uap->key;
759	shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
760	shmid = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
761
762	/*
763	 * We make sure that we have allocated a pager before we need
764	 * to.
765	 */
766	shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
767	    0, size, VM_PROT_DEFAULT, 0, cred);
768	if (shm_object == NULL)
769		return (ENOMEM);
770	VM_OBJECT_LOCK(shm_object);
771	vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
772	vm_object_set_flag(shm_object, OBJ_NOSPLIT);
773	VM_OBJECT_UNLOCK(shm_object);
774
775	shmseg->u.shm_internal = shm_object;
776	shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
777	shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
778	shmseg->u.shm_perm.mode = (shmseg->u.shm_perm.mode & SHMSEG_WANTED) |
779	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
780	shmseg->u.shm_segsz = uap->size;
781	shmseg->shm_bsegsz = uap->size;
782	shmseg->u.shm_cpid = td->td_proc->p_pid;
783	shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
784	shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
785#ifdef MAC
786	mac_sysvshm_create(cred, shmseg);
787#endif
788	shmseg->u.shm_ctime = time_second;
789	shm_committed += btoc(size);
790	shm_nused++;
791	if (shmseg->u.shm_perm.mode & SHMSEG_WANTED) {
792		/*
793		 * Somebody else wanted this key while we were asleep.  Wake
794		 * them up now.
795		 */
796		shmseg->u.shm_perm.mode &= ~SHMSEG_WANTED;
797		wakeup(shmseg);
798	}
799	td->td_retval[0] = shmid;
800	return (0);
801}
802
803#ifndef _SYS_SYSPROTO_H_
804struct shmget_args {
805	key_t key;
806	size_t size;
807	int shmflg;
808};
809#endif
810int
811shmget(td, uap)
812	struct thread *td;
813	struct shmget_args *uap;
814{
815	int segnum, mode;
816	int error;
817
818	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
819		return (ENOSYS);
820	mtx_lock(&Giant);
821	mode = uap->shmflg & ACCESSPERMS;
822	if (uap->key != IPC_PRIVATE) {
823	again:
824		segnum = shm_find_segment_by_key(uap->key);
825		if (segnum >= 0) {
826			error = shmget_existing(td, uap, mode, segnum);
827			if (error == EAGAIN)
828				goto again;
829			goto done2;
830		}
831		if ((uap->shmflg & IPC_CREAT) == 0) {
832			error = ENOENT;
833			goto done2;
834		}
835	}
836	error = shmget_allocate_segment(td, uap, mode);
837done2:
838	mtx_unlock(&Giant);
839	return (error);
840}
841
842int
843shmsys(td, uap)
844	struct thread *td;
845	/* XXX actually varargs. */
846	struct shmsys_args /* {
847		int	which;
848		int	a2;
849		int	a3;
850		int	a4;
851	} */ *uap;
852{
853#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
854	int error;
855
856	if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
857		return (ENOSYS);
858	if (uap->which < 0 ||
859	    uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
860		return (EINVAL);
861	mtx_lock(&Giant);
862	error = (*shmcalls[uap->which])(td, &uap->a2);
863	mtx_unlock(&Giant);
864	return (error);
865#else
866	return (nosys(td, NULL));
867#endif
868}
869
870static void
871shmfork_myhook(p1, p2)
872	struct proc *p1, *p2;
873{
874	struct shmmap_state *shmmap_s;
875	size_t size;
876	int i;
877
878	mtx_lock(&Giant);
879	size = shminfo.shmseg * sizeof(struct shmmap_state);
880	shmmap_s = malloc(size, M_SHM, M_WAITOK);
881	bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
882	p2->p_vmspace->vm_shm = shmmap_s;
883	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
884		if (shmmap_s->shmid != -1)
885			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
886	mtx_unlock(&Giant);
887}
888
889static void
890shmexit_myhook(struct vmspace *vm)
891{
892	struct shmmap_state *base, *shm;
893	int i;
894
895	if ((base = vm->vm_shm) != NULL) {
896		vm->vm_shm = NULL;
897		mtx_lock(&Giant);
898		for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
899			if (shm->shmid != -1)
900				shm_delete_mapping(vm, shm);
901		}
902		mtx_unlock(&Giant);
903		free(base, M_SHM);
904	}
905}
906
907static void
908shmrealloc(void)
909{
910	int i;
911	struct shmid_kernel *newsegs;
912
913	if (shmalloced >= shminfo.shmmni)
914		return;
915
916	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
917	if (newsegs == NULL)
918		return;
919	for (i = 0; i < shmalloced; i++)
920		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
921	for (; i < shminfo.shmmni; i++) {
922		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
923		shmsegs[i].u.shm_perm.seq = 0;
924#ifdef MAC
925		mac_sysvshm_init(&shmsegs[i]);
926#endif
927	}
928	free(shmsegs, M_SHM);
929	shmsegs = newsegs;
930	shmalloced = shminfo.shmmni;
931}
932
933static void
934shminit()
935{
936	int i;
937
938	TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
939	for (i = PAGE_SIZE; i > 0; i--) {
940		shminfo.shmmax = shminfo.shmall * i;
941		if (shminfo.shmmax >= shminfo.shmall)
942			break;
943	}
944	TUNABLE_ULONG_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
945	TUNABLE_ULONG_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
946	TUNABLE_ULONG_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
947	TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
948
949	shmalloced = shminfo.shmmni;
950	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
951	if (shmsegs == NULL)
952		panic("cannot allocate initial memory for sysvshm");
953	for (i = 0; i < shmalloced; i++) {
954		shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
955		shmsegs[i].u.shm_perm.seq = 0;
956#ifdef MAC
957		mac_sysvshm_init(&shmsegs[i]);
958#endif
959	}
960	shm_last_free = 0;
961	shm_nused = 0;
962	shm_committed = 0;
963	shmexit_hook = &shmexit_myhook;
964	shmfork_hook = &shmfork_myhook;
965}
966
967static int
968shmunload()
969{
970#ifdef MAC
971	int i;
972#endif
973
974	if (shm_nused > 0)
975		return (EBUSY);
976
977#ifdef MAC
978	for (i = 0; i < shmalloced; i++)
979		mac_sysvshm_destroy(&shmsegs[i]);
980#endif
981	free(shmsegs, M_SHM);
982	shmexit_hook = NULL;
983	shmfork_hook = NULL;
984	return (0);
985}
986
987static int
988sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
989{
990
991	return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
992}
993
994static int
995sysvshm_modload(struct module *module, int cmd, void *arg)
996{
997	int error = 0;
998
999	switch (cmd) {
1000	case MOD_LOAD:
1001		shminit();
1002		break;
1003	case MOD_UNLOAD:
1004		error = shmunload();
1005		break;
1006	case MOD_SHUTDOWN:
1007		break;
1008	default:
1009		error = EINVAL;
1010		break;
1011	}
1012	return (error);
1013}
1014
1015static moduledata_t sysvshm_mod = {
1016	"sysvshm",
1017	&sysvshm_modload,
1018	NULL
1019};
1020
1021SYSCALL_MODULE_HELPER(shmsys);
1022SYSCALL_MODULE_HELPER(shmat);
1023SYSCALL_MODULE_HELPER(shmctl);
1024SYSCALL_MODULE_HELPER(shmdt);
1025SYSCALL_MODULE_HELPER(shmget);
1026
1027DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1028MODULE_VERSION(sysvshm, 1);
1029