1 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
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.
32 * Copyright (c) 2003-2005 McAfee, Inc.
33 * All rights reserved.
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
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include "opt_compat.h"
66 #include "opt_sysvipc.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/limits.h>
73 #include <sys/sysctl.h>
76 #include <sys/malloc.h>
78 #include <sys/module.h>
79 #include <sys/mutex.h>
80 #include <sys/resourcevar.h>
82 #include <sys/syscall.h>
83 #include <sys/syscallsubr.h>
84 #include <sys/sysent.h>
85 #include <sys/sysproto.h>
88 #include <security/mac/mac_framework.h>
91 #include <vm/vm_param.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_pager.h>
98 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
100 static int shmget_allocate_segment(struct thread *td,
101 struct shmget_args *uap, int mode);
102 static int shmget_existing(struct thread *td, struct shmget_args *uap,
103 int mode, int segnum);
105 #define SHMSEG_FREE 0x0200
106 #define SHMSEG_REMOVED 0x0400
107 #define SHMSEG_ALLOCATED 0x0800
108 #define SHMSEG_WANTED 0x1000
110 static int shm_last_free, shm_nused, shmalloced;
111 vm_size_t shm_committed;
112 static struct shmid_kernel *shmsegs;
114 struct shmmap_state {
119 static void shm_deallocate_segment(struct shmid_kernel *);
120 static int shm_find_segment_by_key(key_t);
121 static struct shmid_kernel *shm_find_segment_by_shmid(int);
122 static struct shmid_kernel *shm_find_segment_by_shmidx(int);
123 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
124 static void shmrealloc(void);
125 static int shminit(void);
126 static int sysvshm_modload(struct module *, int, void *);
127 static int shmunload(void);
128 static void shmexit_myhook(struct vmspace *vm);
129 static void shmfork_myhook(struct proc *p1, struct proc *p2);
130 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
136 #define SHMMAXPGS 8192 /* Note: sysv shared memory is swap backed. */
139 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
151 #define SHMALL (SHMMAXPGS)
154 struct shminfo shminfo = {
162 static int shm_use_phys;
163 static int shm_allow_removed;
165 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
166 "Maximum shared memory segment size");
167 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
168 "Minimum shared memory segment size");
169 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
170 "Number of shared memory identifiers");
171 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
172 "Number of segments per process");
173 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
174 "Maximum number of pages available for shared memory");
175 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
176 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
177 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
178 &shm_allow_removed, 0,
179 "Enable/Disable attachment to attached segments marked for removal");
180 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
181 NULL, 0, sysctl_shmsegs, "",
182 "Current number of shared memory segments allocated");
185 shm_find_segment_by_key(key)
190 for (i = 0; i < shmalloced; i++)
191 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
192 shmsegs[i].u.shm_perm.key == key)
197 static struct shmid_kernel *
198 shm_find_segment_by_shmid(int shmid)
201 struct shmid_kernel *shmseg;
203 segnum = IPCID_TO_IX(shmid);
204 if (segnum < 0 || segnum >= shmalloced)
206 shmseg = &shmsegs[segnum];
207 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
208 (!shm_allow_removed &&
209 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
210 shmseg->u.shm_perm.seq != IPCID_TO_SEQ(shmid))
215 static struct shmid_kernel *
216 shm_find_segment_by_shmidx(int segnum)
218 struct shmid_kernel *shmseg;
220 if (segnum < 0 || segnum >= shmalloced)
222 shmseg = &shmsegs[segnum];
223 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
224 (!shm_allow_removed &&
225 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0))
231 shm_deallocate_segment(shmseg)
232 struct shmid_kernel *shmseg;
238 vm_object_deallocate(shmseg->object);
239 shmseg->object = NULL;
240 size = round_page(shmseg->u.shm_segsz);
241 shm_committed -= btoc(size);
243 shmseg->u.shm_perm.mode = SHMSEG_FREE;
245 mac_sysvshm_cleanup(shmseg);
250 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
252 struct shmid_kernel *shmseg;
258 segnum = IPCID_TO_IX(shmmap_s->shmid);
259 shmseg = &shmsegs[segnum];
260 size = round_page(shmseg->u.shm_segsz);
261 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
262 if (result != KERN_SUCCESS)
264 shmmap_s->shmid = -1;
265 shmseg->u.shm_dtime = time_second;
266 if ((--shmseg->u.shm_nattch <= 0) &&
267 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
268 shm_deallocate_segment(shmseg);
269 shm_last_free = segnum;
274 #ifndef _SYS_SYSPROTO_H_
282 struct shmdt_args *uap;
284 struct proc *p = td->td_proc;
285 struct shmmap_state *shmmap_s;
287 struct shmid_kernel *shmsegptr;
292 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
295 shmmap_s = p->p_vmspace->vm_shm;
296 if (shmmap_s == NULL) {
300 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
301 if (shmmap_s->shmid != -1 &&
302 shmmap_s->va == (vm_offset_t)uap->shmaddr) {
306 if (i == shminfo.shmseg) {
311 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
312 error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
316 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
322 #ifndef _SYS_SYSPROTO_H_
330 kern_shmat(td, shmid, shmaddr, shmflg)
336 struct proc *p = td->td_proc;
338 struct shmid_kernel *shmseg;
339 struct shmmap_state *shmmap_s = NULL;
340 vm_offset_t attach_va;
346 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
349 shmmap_s = p->p_vmspace->vm_shm;
350 if (shmmap_s == NULL) {
351 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
353 for (i = 0; i < shminfo.shmseg; i++)
354 shmmap_s[i].shmid = -1;
355 p->p_vmspace->vm_shm = shmmap_s;
357 shmseg = shm_find_segment_by_shmid(shmid);
358 if (shmseg == NULL) {
362 error = ipcperm(td, &shmseg->u.shm_perm,
363 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
367 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
371 for (i = 0; i < shminfo.shmseg; i++) {
372 if (shmmap_s->shmid == -1)
376 if (i >= shminfo.shmseg) {
380 size = round_page(shmseg->u.shm_segsz);
382 if ((shmflg & SHM_RDONLY) == 0)
383 prot |= VM_PROT_WRITE;
384 flags = MAP_ANON | MAP_SHARED;
387 if (shmflg & SHM_RND) {
388 attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
389 } else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
390 attach_va = (vm_offset_t)shmaddr;
397 * This is just a hint to vm_map_find() about where to
401 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
402 lim_max(p, RLIMIT_DATA));
406 vm_object_reference(shmseg->object);
407 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object,
408 0, &attach_va, size, (flags & MAP_FIXED) ? VMFS_NO_SPACE :
409 VMFS_ANY_SPACE, prot, prot, 0);
410 if (rv != KERN_SUCCESS) {
411 vm_object_deallocate(shmseg->object);
415 vm_map_inherit(&p->p_vmspace->vm_map,
416 attach_va, attach_va + size, VM_INHERIT_SHARE);
418 shmmap_s->va = attach_va;
419 shmmap_s->shmid = shmid;
420 shmseg->u.shm_lpid = p->p_pid;
421 shmseg->u.shm_atime = time_second;
422 shmseg->u.shm_nattch++;
423 td->td_retval[0] = attach_va;
432 struct shmat_args *uap;
434 return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
438 kern_shmctl(td, shmid, cmd, buf, bufsz)
446 struct shmid_kernel *shmseg;
448 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
454 * It is possible that kern_shmctl is being called from the Linux ABI
455 * layer, in which case, we will need to implement IPC_INFO. It should
456 * be noted that other shmctl calls will be funneled through here for
457 * Linix binaries as well.
459 * NB: The Linux ABI layer will convert this data to structure(s) more
460 * consistent with the Linux ABI.
463 memcpy(buf, &shminfo, sizeof(shminfo));
465 *bufsz = sizeof(shminfo);
466 td->td_retval[0] = shmalloced;
469 struct shm_info shm_info;
470 shm_info.used_ids = shm_nused;
471 shm_info.shm_rss = 0; /*XXX where to get from ? */
472 shm_info.shm_tot = 0; /*XXX where to get from ? */
473 shm_info.shm_swp = 0; /*XXX where to get from ? */
474 shm_info.swap_attempts = 0; /*XXX where to get from ? */
475 shm_info.swap_successes = 0; /*XXX where to get from ? */
476 memcpy(buf, &shm_info, sizeof(shm_info));
478 *bufsz = sizeof(shm_info);
479 td->td_retval[0] = shmalloced;
484 shmseg = shm_find_segment_by_shmidx(shmid);
486 shmseg = shm_find_segment_by_shmid(shmid);
487 if (shmseg == NULL) {
492 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
499 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
502 memcpy(buf, &shmseg->u, sizeof(struct shmid_ds));
504 *bufsz = sizeof(struct shmid_ds);
506 td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->u.shm_perm);
509 struct shmid_ds *shmid;
511 shmid = (struct shmid_ds *)buf;
512 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
515 shmseg->u.shm_perm.uid = shmid->shm_perm.uid;
516 shmseg->u.shm_perm.gid = shmid->shm_perm.gid;
517 shmseg->u.shm_perm.mode =
518 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
519 (shmid->shm_perm.mode & ACCESSPERMS);
520 shmseg->u.shm_ctime = time_second;
524 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
527 shmseg->u.shm_perm.key = IPC_PRIVATE;
528 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
529 if (shmseg->u.shm_nattch <= 0) {
530 shm_deallocate_segment(shmseg);
531 shm_last_free = IPCID_TO_IX(shmid);
547 #ifndef _SYS_SYSPROTO_H_
551 struct shmid_ds *buf;
557 struct shmctl_args *uap;
564 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
565 * Linux binaries. If we see the call come through the FreeBSD ABI,
566 * return an error back to the user since we do not to support this.
568 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
569 uap->cmd == SHM_STAT)
572 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
573 if (uap->cmd == IPC_SET) {
574 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
578 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
582 /* Cases in which we need to copyout */
585 error = copyout(&buf, uap->buf, bufsz);
591 /* Invalidate the return value */
592 td->td_retval[0] = -1;
599 shmget_existing(td, uap, mode, segnum)
601 struct shmget_args *uap;
605 struct shmid_kernel *shmseg;
608 shmseg = &shmsegs[segnum];
609 if (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) {
611 * This segment is in the process of being allocated. Wait
612 * until it's done, and look the key up again (in case the
613 * allocation failed or it was freed).
615 shmseg->u.shm_perm.mode |= SHMSEG_WANTED;
616 error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
621 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
624 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
628 if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
630 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
635 shmget_allocate_segment(td, uap, mode)
637 struct shmget_args *uap;
640 int i, segnum, shmid;
642 struct ucred *cred = td->td_ucred;
643 struct shmid_kernel *shmseg;
644 vm_object_t shm_object;
648 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
650 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
652 size = round_page(uap->size);
653 if (shm_committed + btoc(size) > shminfo.shmall)
655 if (shm_last_free < 0) {
656 shmrealloc(); /* Maybe expand the shmsegs[] array. */
657 for (i = 0; i < shmalloced; i++)
658 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
664 segnum = shm_last_free;
667 shmseg = &shmsegs[segnum];
669 * In case we sleep in malloc(), mark the segment present but deleted
670 * so that noone else tries to create the same key.
672 shmseg->u.shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
673 shmseg->u.shm_perm.key = uap->key;
674 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
675 shmid = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
678 * We make sure that we have allocated a pager before we need
681 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
682 0, size, VM_PROT_DEFAULT, 0, cred);
683 if (shm_object == NULL)
685 VM_OBJECT_LOCK(shm_object);
686 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
687 vm_object_set_flag(shm_object, OBJ_NOSPLIT);
688 VM_OBJECT_UNLOCK(shm_object);
690 shmseg->object = shm_object;
691 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
692 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
693 shmseg->u.shm_perm.mode = (shmseg->u.shm_perm.mode & SHMSEG_WANTED) |
694 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
695 shmseg->u.shm_segsz = uap->size;
696 shmseg->u.shm_cpid = td->td_proc->p_pid;
697 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
698 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
700 mac_sysvshm_create(cred, shmseg);
702 shmseg->u.shm_ctime = time_second;
703 shm_committed += btoc(size);
705 if (shmseg->u.shm_perm.mode & SHMSEG_WANTED) {
707 * Somebody else wanted this key while we were asleep. Wake
710 shmseg->u.shm_perm.mode &= ~SHMSEG_WANTED;
713 td->td_retval[0] = shmid;
717 #ifndef _SYS_SYSPROTO_H_
727 struct shmget_args *uap;
732 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
735 mode = uap->shmflg & ACCESSPERMS;
736 if (uap->key != IPC_PRIVATE) {
738 segnum = shm_find_segment_by_key(uap->key);
740 error = shmget_existing(td, uap, mode, segnum);
745 if ((uap->shmflg & IPC_CREAT) == 0) {
750 error = shmget_allocate_segment(td, uap, mode);
757 shmfork_myhook(p1, p2)
758 struct proc *p1, *p2;
760 struct shmmap_state *shmmap_s;
765 size = shminfo.shmseg * sizeof(struct shmmap_state);
766 shmmap_s = malloc(size, M_SHM, M_WAITOK);
767 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
768 p2->p_vmspace->vm_shm = shmmap_s;
769 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
770 if (shmmap_s->shmid != -1)
771 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
776 shmexit_myhook(struct vmspace *vm)
778 struct shmmap_state *base, *shm;
781 if ((base = vm->vm_shm) != NULL) {
784 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
785 if (shm->shmid != -1)
786 shm_delete_mapping(vm, shm);
797 struct shmid_kernel *newsegs;
799 if (shmalloced >= shminfo.shmmni)
802 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
805 for (i = 0; i < shmalloced; i++)
806 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
807 for (; i < shminfo.shmmni; i++) {
808 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
809 shmsegs[i].u.shm_perm.seq = 0;
811 mac_sysvshm_init(&shmsegs[i]);
814 free(shmsegs, M_SHM);
816 shmalloced = shminfo.shmmni;
819 static struct syscall_helper_data shm_syscalls[] = {
820 SYSCALL_INIT_HELPER(shmat),
821 SYSCALL_INIT_HELPER(shmctl),
822 SYSCALL_INIT_HELPER(shmdt),
823 SYSCALL_INIT_HELPER(shmget),
824 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
825 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
826 SYSCALL_INIT_HELPER(freebsd7_shmctl),
828 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
829 SYSCALL_INIT_HELPER(shmsys),
834 #ifdef COMPAT_FREEBSD32
835 #include <compat/freebsd32/freebsd32.h>
836 #include <compat/freebsd32/freebsd32_ipc.h>
837 #include <compat/freebsd32/freebsd32_proto.h>
838 #include <compat/freebsd32/freebsd32_signal.h>
839 #include <compat/freebsd32/freebsd32_syscall.h>
840 #include <compat/freebsd32/freebsd32_util.h>
842 static struct syscall_helper_data shm32_syscalls[] = {
843 SYSCALL32_INIT_HELPER(shmat),
844 SYSCALL32_INIT_HELPER(shmdt),
845 SYSCALL32_INIT_HELPER(shmget),
846 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
847 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
848 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
849 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
850 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
861 TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
862 for (i = PAGE_SIZE; i > 0; i--) {
863 shminfo.shmmax = shminfo.shmall * i;
864 if (shminfo.shmmax >= shminfo.shmall)
867 TUNABLE_ULONG_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
868 TUNABLE_ULONG_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
869 TUNABLE_ULONG_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
870 TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
872 shmalloced = shminfo.shmmni;
873 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
875 panic("cannot allocate initial memory for sysvshm");
876 for (i = 0; i < shmalloced; i++) {
877 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
878 shmsegs[i].u.shm_perm.seq = 0;
880 mac_sysvshm_init(&shmsegs[i]);
886 shmexit_hook = &shmexit_myhook;
887 shmfork_hook = &shmfork_myhook;
889 error = syscall_helper_register(shm_syscalls);
892 #ifdef COMPAT_FREEBSD32
893 error = syscall32_helper_register(shm32_syscalls);
910 #ifdef COMPAT_FREEBSD32
911 syscall32_helper_unregister(shm32_syscalls);
913 syscall_helper_unregister(shm_syscalls);
916 for (i = 0; i < shmalloced; i++)
917 mac_sysvshm_destroy(&shmsegs[i]);
919 free(shmsegs, M_SHM);
926 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
929 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
932 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
934 struct ipc_perm_old shm_perm; /* operation perms */
935 int shm_segsz; /* size of segment (bytes) */
936 u_short shm_cpid; /* pid, creator */
937 u_short shm_lpid; /* pid, last operation */
938 short shm_nattch; /* no. of current attaches */
939 time_t shm_atime; /* last attach time */
940 time_t shm_dtime; /* last detach time */
941 time_t shm_ctime; /* last change time */
942 void *shm_handle; /* internal handle for shm segment */
945 struct oshmctl_args {
948 struct oshmid_ds *ubuf;
952 oshmctl(struct thread *td, struct oshmctl_args *uap)
956 struct shmid_kernel *shmseg;
957 struct oshmid_ds outbuf;
959 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
962 shmseg = shm_find_segment_by_shmid(uap->shmid);
963 if (shmseg == NULL) {
969 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
973 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
977 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
978 outbuf.shm_segsz = shmseg->u.shm_segsz;
979 outbuf.shm_cpid = shmseg->u.shm_cpid;
980 outbuf.shm_lpid = shmseg->u.shm_lpid;
981 outbuf.shm_nattch = shmseg->u.shm_nattch;
982 outbuf.shm_atime = shmseg->u.shm_atime;
983 outbuf.shm_dtime = shmseg->u.shm_dtime;
984 outbuf.shm_ctime = shmseg->u.shm_ctime;
985 outbuf.shm_handle = shmseg->object;
986 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
991 error = freebsd7_shmctl(td, (struct freebsd7_shmctl_args *)uap);
1002 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1003 static sy_call_t *shmcalls[] = {
1004 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
1005 (sy_call_t *)shmdt, (sy_call_t *)shmget,
1006 (sy_call_t *)freebsd7_shmctl
1012 /* XXX actually varargs. */
1013 struct shmsys_args /* {
1022 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1024 if (uap->which < 0 ||
1025 uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
1028 error = (*shmcalls[uap->which])(td, &uap->a2);
1033 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1035 #ifdef COMPAT_FREEBSD32
1038 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1041 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1042 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1043 switch (uap->which) {
1044 case 0: { /* shmat */
1045 struct shmat_args ap;
1048 ap.shmaddr = PTRIN(uap->a3);
1049 ap.shmflg = uap->a4;
1050 return (sysent[SYS_shmat].sy_call(td, &ap));
1052 case 2: { /* shmdt */
1053 struct shmdt_args ap;
1055 ap.shmaddr = PTRIN(uap->a2);
1056 return (sysent[SYS_shmdt].sy_call(td, &ap));
1058 case 3: { /* shmget */
1059 struct shmget_args ap;
1063 ap.shmflg = uap->a4;
1064 return (sysent[SYS_shmget].sy_call(td, &ap));
1066 case 4: { /* shmctl */
1067 struct freebsd7_freebsd32_shmctl_args ap;
1071 ap.buf = PTRIN(uap->a4);
1072 return (freebsd7_freebsd32_shmctl(td, &ap));
1074 case 1: /* oshmctl */
1079 return (nosys(td, NULL));
1083 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1084 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1086 freebsd7_freebsd32_shmctl(struct thread *td,
1087 struct freebsd7_freebsd32_shmctl_args *uap)
1091 struct shmid_ds shmid_ds;
1092 struct shm_info shm_info;
1093 struct shminfo shminfo;
1096 struct shmid_ds32_old shmid_ds32;
1097 struct shm_info32 shm_info32;
1098 struct shminfo32 shminfo32;
1102 if (uap->cmd == IPC_SET) {
1103 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1104 sizeof(u32.shmid_ds32))))
1106 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1107 &u.shmid_ds.shm_perm);
1108 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1109 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1110 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1111 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1112 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1113 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1114 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1117 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1121 /* Cases in which we need to copyout */
1124 CP(u.shminfo, u32.shminfo32, shmmax);
1125 CP(u.shminfo, u32.shminfo32, shmmin);
1126 CP(u.shminfo, u32.shminfo32, shmmni);
1127 CP(u.shminfo, u32.shminfo32, shmseg);
1128 CP(u.shminfo, u32.shminfo32, shmall);
1129 error = copyout(&u32.shminfo32, uap->buf,
1130 sizeof(u32.shminfo32));
1133 CP(u.shm_info, u32.shm_info32, used_ids);
1134 CP(u.shm_info, u32.shm_info32, shm_rss);
1135 CP(u.shm_info, u32.shm_info32, shm_tot);
1136 CP(u.shm_info, u32.shm_info32, shm_swp);
1137 CP(u.shm_info, u32.shm_info32, swap_attempts);
1138 CP(u.shm_info, u32.shm_info32, swap_successes);
1139 error = copyout(&u32.shm_info32, uap->buf,
1140 sizeof(u32.shm_info32));
1144 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1145 &u32.shmid_ds32.shm_perm);
1146 if (u.shmid_ds.shm_segsz > INT32_MAX)
1147 u32.shmid_ds32.shm_segsz = INT32_MAX;
1149 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1150 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1151 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1152 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1153 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1154 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1155 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1156 u32.shmid_ds32.shm_internal = 0;
1157 error = copyout(&u32.shmid_ds32, uap->buf,
1158 sizeof(u32.shmid_ds32));
1164 /* Invalidate the return value */
1165 td->td_retval[0] = -1;
1172 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1176 struct shmid_ds shmid_ds;
1177 struct shm_info shm_info;
1178 struct shminfo shminfo;
1181 struct shmid_ds32 shmid_ds32;
1182 struct shm_info32 shm_info32;
1183 struct shminfo32 shminfo32;
1187 if (uap->cmd == IPC_SET) {
1188 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1189 sizeof(u32.shmid_ds32))))
1191 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1192 &u.shmid_ds.shm_perm);
1193 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1194 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1195 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1196 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1197 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1198 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1199 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1202 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1206 /* Cases in which we need to copyout */
1209 CP(u.shminfo, u32.shminfo32, shmmax);
1210 CP(u.shminfo, u32.shminfo32, shmmin);
1211 CP(u.shminfo, u32.shminfo32, shmmni);
1212 CP(u.shminfo, u32.shminfo32, shmseg);
1213 CP(u.shminfo, u32.shminfo32, shmall);
1214 error = copyout(&u32.shminfo32, uap->buf,
1215 sizeof(u32.shminfo32));
1218 CP(u.shm_info, u32.shm_info32, used_ids);
1219 CP(u.shm_info, u32.shm_info32, shm_rss);
1220 CP(u.shm_info, u32.shm_info32, shm_tot);
1221 CP(u.shm_info, u32.shm_info32, shm_swp);
1222 CP(u.shm_info, u32.shm_info32, swap_attempts);
1223 CP(u.shm_info, u32.shm_info32, swap_successes);
1224 error = copyout(&u32.shm_info32, uap->buf,
1225 sizeof(u32.shm_info32));
1229 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1230 &u32.shmid_ds32.shm_perm);
1231 if (u.shmid_ds.shm_segsz > INT32_MAX)
1232 u32.shmid_ds32.shm_segsz = INT32_MAX;
1234 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1235 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1236 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1237 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1238 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1239 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1240 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1241 error = copyout(&u32.shmid_ds32, uap->buf,
1242 sizeof(u32.shmid_ds32));
1248 /* Invalidate the return value */
1249 td->td_retval[0] = -1;
1255 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1256 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1259 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1262 #ifndef _SYS_SYSPROTO_H_
1263 struct freebsd7_shmctl_args {
1266 struct shmid_ds_old *buf;
1270 freebsd7_shmctl(td, uap)
1272 struct freebsd7_shmctl_args *uap;
1275 struct shmid_ds_old old;
1276 struct shmid_ds buf;
1280 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1281 * Linux binaries. If we see the call come through the FreeBSD ABI,
1282 * return an error back to the user since we do not to support this.
1284 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1285 uap->cmd == SHM_STAT)
1288 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1289 if (uap->cmd == IPC_SET) {
1290 if ((error = copyin(uap->buf, &old, sizeof(old))))
1292 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1293 CP(old, buf, shm_segsz);
1294 CP(old, buf, shm_lpid);
1295 CP(old, buf, shm_cpid);
1296 CP(old, buf, shm_nattch);
1297 CP(old, buf, shm_atime);
1298 CP(old, buf, shm_dtime);
1299 CP(old, buf, shm_ctime);
1302 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1306 /* Cases in which we need to copyout */
1309 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1310 if (buf.shm_segsz > INT_MAX)
1311 old.shm_segsz = INT_MAX;
1313 CP(buf, old, shm_segsz);
1314 CP(buf, old, shm_lpid);
1315 CP(buf, old, shm_cpid);
1316 if (buf.shm_nattch > SHRT_MAX)
1317 old.shm_nattch = SHRT_MAX;
1319 CP(buf, old, shm_nattch);
1320 CP(buf, old, shm_atime);
1321 CP(buf, old, shm_dtime);
1322 CP(buf, old, shm_ctime);
1323 old.shm_internal = NULL;
1324 error = copyout(&old, uap->buf, sizeof(old));
1330 /* Invalidate the return value */
1331 td->td_retval[0] = -1;
1336 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1340 sysvshm_modload(struct module *module, int cmd, void *arg)
1351 error = shmunload();
1362 static moduledata_t sysvshm_mod = {
1368 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1369 MODULE_VERSION(sysvshm, 1);