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/racct.h>
81 #include <sys/resourcevar.h>
82 #include <sys/rwlock.h>
84 #include <sys/syscall.h>
85 #include <sys/syscallsubr.h>
86 #include <sys/sysent.h>
87 #include <sys/sysproto.h>
90 #include <security/mac/mac_framework.h>
93 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pager.h>
100 FEATURE(sysv_shm, "System V shared memory segments support");
102 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
104 static int shmget_allocate_segment(struct thread *td,
105 struct shmget_args *uap, int mode);
106 static int shmget_existing(struct thread *td, struct shmget_args *uap,
107 int mode, int segnum);
109 #define SHMSEG_FREE 0x0200
110 #define SHMSEG_REMOVED 0x0400
111 #define SHMSEG_ALLOCATED 0x0800
113 static int shm_last_free, shm_nused, shmalloced;
114 vm_size_t shm_committed;
115 static struct shmid_kernel *shmsegs;
117 struct shmmap_state {
122 static void shm_deallocate_segment(struct shmid_kernel *);
123 static int shm_find_segment_by_key(key_t);
124 static struct shmid_kernel *shm_find_segment(int, bool);
125 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
126 static void shmrealloc(void);
127 static int shminit(void);
128 static int sysvshm_modload(struct module *, int, void *);
129 static int shmunload(void);
130 static void shmexit_myhook(struct vmspace *vm);
131 static void shmfork_myhook(struct proc *p1, struct proc *p2);
132 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
138 #define SHMMAXPGS 131072 /* Note: sysv shared memory is swap backed. */
141 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
153 #define SHMALL (SHMMAXPGS)
156 struct shminfo shminfo = {
164 static int shm_use_phys;
165 static int shm_allow_removed;
167 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
168 "Maximum shared memory segment size");
169 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
170 "Minimum shared memory segment size");
171 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
172 "Number of shared memory identifiers");
173 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
174 "Number of segments per process");
175 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
176 "Maximum number of pages available for shared memory");
177 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
178 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
179 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
180 &shm_allow_removed, 0,
181 "Enable/Disable attachment to attached segments marked for removal");
182 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
183 CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
184 "Current number of shared memory segments allocated");
186 static struct sx sysvshmsx;
187 #define SYSVSHM_LOCK() sx_xlock(&sysvshmsx)
188 #define SYSVSHM_UNLOCK() sx_xunlock(&sysvshmsx)
189 #define SYSVSHM_ASSERT_LOCKED() sx_assert(&sysvshmsx, SA_XLOCKED)
192 shm_find_segment_by_key(key_t key)
196 for (i = 0; i < shmalloced; i++)
197 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
198 shmsegs[i].u.shm_perm.key == key)
204 * Finds segment either by shmid if is_shmid is true, or by segnum if
207 static struct shmid_kernel *
208 shm_find_segment(int arg, bool is_shmid)
210 struct shmid_kernel *shmseg;
213 segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
214 if (segnum < 0 || segnum >= shmalloced)
216 shmseg = &shmsegs[segnum];
217 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
218 (!shm_allow_removed &&
219 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
220 (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)))
226 shm_deallocate_segment(struct shmid_kernel *shmseg)
230 SYSVSHM_ASSERT_LOCKED();
232 vm_object_deallocate(shmseg->object);
233 shmseg->object = NULL;
234 size = round_page(shmseg->u.shm_segsz);
235 shm_committed -= btoc(size);
237 shmseg->u.shm_perm.mode = SHMSEG_FREE;
239 mac_sysvshm_cleanup(shmseg);
241 racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
242 racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
243 crfree(shmseg->cred);
248 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
250 struct shmid_kernel *shmseg;
254 SYSVSHM_ASSERT_LOCKED();
255 segnum = IPCID_TO_IX(shmmap_s->shmid);
256 KASSERT(segnum >= 0 && segnum < shmalloced,
257 ("segnum %d shmalloced %d", segnum, shmalloced));
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;
275 kern_shmdt_locked(struct thread *td, const void *shmaddr)
277 struct proc *p = td->td_proc;
278 struct shmmap_state *shmmap_s;
280 struct shmid_kernel *shmsegptr;
284 SYSVSHM_ASSERT_LOCKED();
285 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
287 shmmap_s = p->p_vmspace->vm_shm;
288 if (shmmap_s == NULL)
290 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
291 if (shmmap_s->shmid != -1 &&
292 shmmap_s->va == (vm_offset_t)shmaddr) {
296 if (i == shminfo.shmseg)
299 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
300 error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
304 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
308 #ifndef _SYS_SYSPROTO_H_
314 sys_shmdt(struct thread *td, struct shmdt_args *uap)
319 error = kern_shmdt_locked(td, uap->shmaddr);
325 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
328 struct proc *p = td->td_proc;
329 struct shmid_kernel *shmseg;
330 struct shmmap_state *shmmap_s;
331 vm_offset_t attach_va;
336 SYSVSHM_ASSERT_LOCKED();
337 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
339 shmmap_s = p->p_vmspace->vm_shm;
340 if (shmmap_s == NULL) {
341 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
343 for (i = 0; i < shminfo.shmseg; i++)
344 shmmap_s[i].shmid = -1;
345 KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
346 p->p_vmspace->vm_shm = shmmap_s;
348 shmseg = shm_find_segment(shmid, true);
351 error = ipcperm(td, &shmseg->u.shm_perm,
352 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
356 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
360 for (i = 0; i < shminfo.shmseg; i++) {
361 if (shmmap_s->shmid == -1)
365 if (i >= shminfo.shmseg)
367 size = round_page(shmseg->u.shm_segsz);
369 if ((shmflg & SHM_RDONLY) == 0)
370 prot |= VM_PROT_WRITE;
371 if (shmaddr != NULL) {
372 if ((shmflg & SHM_RND) != 0)
373 attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
374 else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
375 attach_va = (vm_offset_t)shmaddr;
380 * This is just a hint to vm_map_find() about where to
384 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
385 lim_max(p, RLIMIT_DATA));
389 vm_object_reference(shmseg->object);
390 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object,
391 0, &attach_va, size, 0, shmaddr != NULL ? VMFS_NO_SPACE :
392 VMFS_OPTIMAL_SPACE, prot, prot, MAP_INHERIT_SHARE);
393 if (rv != KERN_SUCCESS) {
394 vm_object_deallocate(shmseg->object);
398 shmmap_s->va = attach_va;
399 shmmap_s->shmid = shmid;
400 shmseg->u.shm_lpid = p->p_pid;
401 shmseg->u.shm_atime = time_second;
402 shmseg->u.shm_nattch++;
403 td->td_retval[0] = attach_va;
408 kern_shmat(struct thread *td, int shmid, const void *shmaddr, int shmflg)
413 error = kern_shmat_locked(td, shmid, shmaddr, shmflg);
418 #ifndef _SYS_SYSPROTO_H_
426 sys_shmat(struct thread *td, struct shmat_args *uap)
429 return (kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg));
433 kern_shmctl_locked(struct thread *td, int shmid, int cmd, void *buf,
436 struct shmid_kernel *shmseg;
437 struct shmid_ds *shmidp;
438 struct shm_info shm_info;
441 SYSVSHM_ASSERT_LOCKED();
443 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
449 * It is possible that kern_shmctl is being called from the Linux ABI
450 * layer, in which case, we will need to implement IPC_INFO. It should
451 * be noted that other shmctl calls will be funneled through here for
452 * Linix binaries as well.
454 * NB: The Linux ABI layer will convert this data to structure(s) more
455 * consistent with the Linux ABI.
458 memcpy(buf, &shminfo, sizeof(shminfo));
460 *bufsz = sizeof(shminfo);
461 td->td_retval[0] = shmalloced;
464 shm_info.used_ids = shm_nused;
465 shm_info.shm_rss = 0; /*XXX where to get from ? */
466 shm_info.shm_tot = 0; /*XXX where to get from ? */
467 shm_info.shm_swp = 0; /*XXX where to get from ? */
468 shm_info.swap_attempts = 0; /*XXX where to get from ? */
469 shm_info.swap_successes = 0; /*XXX where to get from ? */
470 memcpy(buf, &shm_info, sizeof(shm_info));
472 *bufsz = sizeof(shm_info);
473 td->td_retval[0] = shmalloced;
477 shmseg = shm_find_segment(shmid, cmd != SHM_STAT);
481 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
488 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
491 memcpy(buf, &shmseg->u, sizeof(struct shmid_ds));
493 *bufsz = sizeof(struct shmid_ds);
494 if (cmd == SHM_STAT) {
495 td->td_retval[0] = IXSEQ_TO_IPCID(shmid,
500 shmidp = (struct shmid_ds *)buf;
501 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
504 shmseg->u.shm_perm.uid = shmidp->shm_perm.uid;
505 shmseg->u.shm_perm.gid = shmidp->shm_perm.gid;
506 shmseg->u.shm_perm.mode =
507 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
508 (shmidp->shm_perm.mode & ACCESSPERMS);
509 shmseg->u.shm_ctime = time_second;
512 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
515 shmseg->u.shm_perm.key = IPC_PRIVATE;
516 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
517 if (shmseg->u.shm_nattch <= 0) {
518 shm_deallocate_segment(shmseg);
519 shm_last_free = IPCID_TO_IX(shmid);
534 kern_shmctl(struct thread *td, int shmid, int cmd, void *buf, size_t *bufsz)
539 error = kern_shmctl_locked(td, shmid, cmd, buf, bufsz);
545 #ifndef _SYS_SYSPROTO_H_
549 struct shmid_ds *buf;
553 sys_shmctl(struct thread *td, struct shmctl_args *uap)
560 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
561 * Linux binaries. If we see the call come through the FreeBSD ABI,
562 * return an error back to the user since we do not to support this.
564 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
565 uap->cmd == SHM_STAT)
568 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
569 if (uap->cmd == IPC_SET) {
570 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
574 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
578 /* Cases in which we need to copyout */
581 error = copyout(&buf, uap->buf, bufsz);
587 /* Invalidate the return value */
588 td->td_retval[0] = -1;
595 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
598 struct shmid_kernel *shmseg;
603 SYSVSHM_ASSERT_LOCKED();
604 KASSERT(segnum >= 0 && segnum < shmalloced,
605 ("segnum %d shmalloced %d", segnum, shmalloced));
606 shmseg = &shmsegs[segnum];
607 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
610 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
614 if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
616 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
621 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
623 struct ucred *cred = td->td_ucred;
624 struct shmid_kernel *shmseg;
625 vm_object_t shm_object;
629 SYSVSHM_ASSERT_LOCKED();
631 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
633 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
635 size = round_page(uap->size);
636 if (shm_committed + btoc(size) > shminfo.shmall)
638 if (shm_last_free < 0) {
639 shmrealloc(); /* Maybe expand the shmsegs[] array. */
640 for (i = 0; i < shmalloced; i++)
641 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
647 segnum = shm_last_free;
650 KASSERT(segnum >= 0 && segnum < shmalloced,
651 ("segnum %d shmalloced %d", segnum, shmalloced));
652 shmseg = &shmsegs[segnum];
655 PROC_LOCK(td->td_proc);
656 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
657 PROC_UNLOCK(td->td_proc);
660 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
661 racct_sub(td->td_proc, RACCT_NSHM, 1);
662 PROC_UNLOCK(td->td_proc);
665 PROC_UNLOCK(td->td_proc);
670 * We make sure that we have allocated a pager before we need
673 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
674 0, size, VM_PROT_DEFAULT, 0, cred);
675 if (shm_object == NULL) {
678 PROC_LOCK(td->td_proc);
679 racct_sub(td->td_proc, RACCT_NSHM, 1);
680 racct_sub(td->td_proc, RACCT_SHMSIZE, size);
681 PROC_UNLOCK(td->td_proc);
686 shm_object->pg_color = 0;
687 VM_OBJECT_WLOCK(shm_object);
688 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
689 vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
690 VM_OBJECT_WUNLOCK(shm_object);
692 shmseg->object = shm_object;
693 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
694 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
695 shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
696 shmseg->u.shm_perm.key = uap->key;
697 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
698 shmseg->cred = crhold(cred);
699 shmseg->u.shm_segsz = uap->size;
700 shmseg->u.shm_cpid = td->td_proc->p_pid;
701 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
702 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
704 mac_sysvshm_create(cred, shmseg);
706 shmseg->u.shm_ctime = time_second;
707 shm_committed += btoc(size);
709 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
714 #ifndef _SYS_SYSPROTO_H_
722 sys_shmget(struct thread *td, struct shmget_args *uap)
727 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
729 mode = uap->shmflg & ACCESSPERMS;
731 if (uap->key == IPC_PRIVATE) {
732 error = shmget_allocate_segment(td, uap, mode);
734 segnum = shm_find_segment_by_key(uap->key);
736 error = shmget_existing(td, uap, mode, segnum);
737 else if ((uap->shmflg & IPC_CREAT) == 0)
740 error = shmget_allocate_segment(td, uap, mode);
747 shmfork_myhook(struct proc *p1, struct proc *p2)
749 struct shmmap_state *shmmap_s;
754 size = shminfo.shmseg * sizeof(struct shmmap_state);
755 shmmap_s = malloc(size, M_SHM, M_WAITOK);
756 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
757 p2->p_vmspace->vm_shm = shmmap_s;
758 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
759 if (shmmap_s->shmid != -1) {
760 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
761 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
762 ("segnum %d shmalloced %d",
763 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
764 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
771 shmexit_myhook(struct vmspace *vm)
773 struct shmmap_state *base, *shm;
780 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
781 if (shm->shmid != -1)
782 shm_delete_mapping(vm, shm);
792 struct shmid_kernel *newsegs;
795 SYSVSHM_ASSERT_LOCKED();
797 if (shmalloced >= shminfo.shmmni)
800 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
801 for (i = 0; i < shmalloced; i++)
802 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
803 for (; i < shminfo.shmmni; i++) {
804 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
805 shmsegs[i].u.shm_perm.seq = 0;
807 mac_sysvshm_init(&shmsegs[i]);
810 free(shmsegs, M_SHM);
812 shmalloced = shminfo.shmmni;
815 static struct syscall_helper_data shm_syscalls[] = {
816 SYSCALL_INIT_HELPER(shmat),
817 SYSCALL_INIT_HELPER(shmctl),
818 SYSCALL_INIT_HELPER(shmdt),
819 SYSCALL_INIT_HELPER(shmget),
820 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
821 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
822 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
824 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
825 SYSCALL_INIT_HELPER(shmsys),
830 #ifdef COMPAT_FREEBSD32
831 #include <compat/freebsd32/freebsd32.h>
832 #include <compat/freebsd32/freebsd32_ipc.h>
833 #include <compat/freebsd32/freebsd32_proto.h>
834 #include <compat/freebsd32/freebsd32_signal.h>
835 #include <compat/freebsd32/freebsd32_syscall.h>
836 #include <compat/freebsd32/freebsd32_util.h>
838 static struct syscall_helper_data shm32_syscalls[] = {
839 SYSCALL32_INIT_HELPER_COMPAT(shmat),
840 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
841 SYSCALL32_INIT_HELPER_COMPAT(shmget),
842 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
843 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
844 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
845 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
846 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
858 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
859 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
861 TUNABLE_ULONG_FETCH("kern.ipc.shmall", &shminfo.shmall);
862 if (!TUNABLE_ULONG_FETCH("kern.ipc.shmmax", &shminfo.shmmax)) {
863 /* Initialize shmmax dealing with possible overflow. */
864 for (i = PAGE_SIZE; i > 0; i--) {
865 shminfo.shmmax = shminfo.shmall * i;
866 if (shminfo.shmmax >= shminfo.shmall)
870 TUNABLE_ULONG_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
871 TUNABLE_ULONG_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
872 TUNABLE_ULONG_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
873 TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
875 shmalloced = shminfo.shmmni;
876 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
877 for (i = 0; i < shmalloced; i++) {
878 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
879 shmsegs[i].u.shm_perm.seq = 0;
881 mac_sysvshm_init(&shmsegs[i]);
887 sx_init(&sysvshmsx, "sysvshmsx");
888 shmexit_hook = &shmexit_myhook;
889 shmfork_hook = &shmfork_myhook;
891 error = syscall_helper_register(shm_syscalls);
894 #ifdef COMPAT_FREEBSD32
895 error = syscall32_helper_register(shm32_syscalls);
910 #ifdef COMPAT_FREEBSD32
911 syscall32_helper_unregister(shm32_syscalls);
913 syscall_helper_unregister(shm_syscalls);
915 for (i = 0; i < shmalloced; i++) {
917 mac_sysvshm_destroy(&shmsegs[i]);
920 * Objects might be still mapped into the processes
921 * address spaces. Actual free would happen on the
922 * last mapping destruction.
924 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
925 vm_object_deallocate(shmsegs[i].object);
927 free(shmsegs, M_SHM);
930 sx_destroy(&sysvshmsx);
935 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
940 error = SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0]));
945 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
947 struct ipc_perm_old shm_perm; /* operation perms */
948 int shm_segsz; /* size of segment (bytes) */
949 u_short shm_cpid; /* pid, creator */
950 u_short shm_lpid; /* pid, last operation */
951 short shm_nattch; /* no. of current attaches */
952 time_t shm_atime; /* last attach time */
953 time_t shm_dtime; /* last detach time */
954 time_t shm_ctime; /* last change time */
955 void *shm_handle; /* internal handle for shm segment */
958 struct oshmctl_args {
961 struct oshmid_ds *ubuf;
965 oshmctl(struct thread *td, struct oshmctl_args *uap)
969 struct shmid_kernel *shmseg;
970 struct oshmid_ds outbuf;
972 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
974 if (uap->cmd != IPC_STAT) {
975 return (freebsd7_shmctl(td,
976 (struct freebsd7_shmctl_args *)uap));
979 shmseg = shm_find_segment(uap->shmid, true);
980 if (shmseg == NULL) {
984 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
990 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
996 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
997 outbuf.shm_segsz = shmseg->u.shm_segsz;
998 outbuf.shm_cpid = shmseg->u.shm_cpid;
999 outbuf.shm_lpid = shmseg->u.shm_lpid;
1000 outbuf.shm_nattch = shmseg->u.shm_nattch;
1001 outbuf.shm_atime = shmseg->u.shm_atime;
1002 outbuf.shm_dtime = shmseg->u.shm_dtime;
1003 outbuf.shm_ctime = shmseg->u.shm_ctime;
1004 outbuf.shm_handle = shmseg->object;
1006 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
1013 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1014 static sy_call_t *shmcalls[] = {
1015 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1016 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1017 (sy_call_t *)freebsd7_shmctl
1020 #ifndef _SYS_SYSPROTO_H_
1021 /* XXX actually varargs. */
1022 struct shmsys_args {
1030 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1034 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
1036 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1038 error = (*shmcalls[uap->which])(td, &uap->a2);
1042 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1044 #ifdef COMPAT_FREEBSD32
1047 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1050 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1051 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1052 switch (uap->which) {
1053 case 0: { /* shmat */
1054 struct shmat_args ap;
1057 ap.shmaddr = PTRIN(uap->a3);
1058 ap.shmflg = uap->a4;
1059 return (sysent[SYS_shmat].sy_call(td, &ap));
1061 case 2: { /* shmdt */
1062 struct shmdt_args ap;
1064 ap.shmaddr = PTRIN(uap->a2);
1065 return (sysent[SYS_shmdt].sy_call(td, &ap));
1067 case 3: { /* shmget */
1068 struct shmget_args ap;
1072 ap.shmflg = uap->a4;
1073 return (sysent[SYS_shmget].sy_call(td, &ap));
1075 case 4: { /* shmctl */
1076 struct freebsd7_freebsd32_shmctl_args ap;
1080 ap.buf = PTRIN(uap->a4);
1081 return (freebsd7_freebsd32_shmctl(td, &ap));
1083 case 1: /* oshmctl */
1088 return (nosys(td, NULL));
1092 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1093 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1095 freebsd7_freebsd32_shmctl(struct thread *td,
1096 struct freebsd7_freebsd32_shmctl_args *uap)
1100 struct shmid_ds shmid_ds;
1101 struct shm_info shm_info;
1102 struct shminfo shminfo;
1105 struct shmid_ds32_old shmid_ds32;
1106 struct shm_info32 shm_info32;
1107 struct shminfo32 shminfo32;
1111 if (uap->cmd == IPC_SET) {
1112 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1113 sizeof(u32.shmid_ds32))))
1115 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1116 &u.shmid_ds.shm_perm);
1117 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1118 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1119 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1120 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1121 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1122 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1123 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1126 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1130 /* Cases in which we need to copyout */
1133 CP(u.shminfo, u32.shminfo32, shmmax);
1134 CP(u.shminfo, u32.shminfo32, shmmin);
1135 CP(u.shminfo, u32.shminfo32, shmmni);
1136 CP(u.shminfo, u32.shminfo32, shmseg);
1137 CP(u.shminfo, u32.shminfo32, shmall);
1138 error = copyout(&u32.shminfo32, uap->buf,
1139 sizeof(u32.shminfo32));
1142 CP(u.shm_info, u32.shm_info32, used_ids);
1143 CP(u.shm_info, u32.shm_info32, shm_rss);
1144 CP(u.shm_info, u32.shm_info32, shm_tot);
1145 CP(u.shm_info, u32.shm_info32, shm_swp);
1146 CP(u.shm_info, u32.shm_info32, swap_attempts);
1147 CP(u.shm_info, u32.shm_info32, swap_successes);
1148 error = copyout(&u32.shm_info32, uap->buf,
1149 sizeof(u32.shm_info32));
1153 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1154 &u32.shmid_ds32.shm_perm);
1155 if (u.shmid_ds.shm_segsz > INT32_MAX)
1156 u32.shmid_ds32.shm_segsz = INT32_MAX;
1158 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1159 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1160 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1161 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1162 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1163 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1164 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1165 u32.shmid_ds32.shm_internal = 0;
1166 error = copyout(&u32.shmid_ds32, uap->buf,
1167 sizeof(u32.shmid_ds32));
1173 /* Invalidate the return value */
1174 td->td_retval[0] = -1;
1181 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1185 struct shmid_ds shmid_ds;
1186 struct shm_info shm_info;
1187 struct shminfo shminfo;
1190 struct shmid_ds32 shmid_ds32;
1191 struct shm_info32 shm_info32;
1192 struct shminfo32 shminfo32;
1196 if (uap->cmd == IPC_SET) {
1197 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1198 sizeof(u32.shmid_ds32))))
1200 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1201 &u.shmid_ds.shm_perm);
1202 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1203 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1204 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1205 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1206 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1207 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1208 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1211 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1215 /* Cases in which we need to copyout */
1218 CP(u.shminfo, u32.shminfo32, shmmax);
1219 CP(u.shminfo, u32.shminfo32, shmmin);
1220 CP(u.shminfo, u32.shminfo32, shmmni);
1221 CP(u.shminfo, u32.shminfo32, shmseg);
1222 CP(u.shminfo, u32.shminfo32, shmall);
1223 error = copyout(&u32.shminfo32, uap->buf,
1224 sizeof(u32.shminfo32));
1227 CP(u.shm_info, u32.shm_info32, used_ids);
1228 CP(u.shm_info, u32.shm_info32, shm_rss);
1229 CP(u.shm_info, u32.shm_info32, shm_tot);
1230 CP(u.shm_info, u32.shm_info32, shm_swp);
1231 CP(u.shm_info, u32.shm_info32, swap_attempts);
1232 CP(u.shm_info, u32.shm_info32, swap_successes);
1233 error = copyout(&u32.shm_info32, uap->buf,
1234 sizeof(u32.shm_info32));
1238 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1239 &u32.shmid_ds32.shm_perm);
1240 if (u.shmid_ds.shm_segsz > INT32_MAX)
1241 u32.shmid_ds32.shm_segsz = INT32_MAX;
1243 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1244 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1245 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1246 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1247 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1248 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1249 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1250 error = copyout(&u32.shmid_ds32, uap->buf,
1251 sizeof(u32.shmid_ds32));
1257 /* Invalidate the return value */
1258 td->td_retval[0] = -1;
1264 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1265 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1268 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1271 #ifndef _SYS_SYSPROTO_H_
1272 struct freebsd7_shmctl_args {
1275 struct shmid_ds_old *buf;
1279 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1282 struct shmid_ds_old old;
1283 struct shmid_ds buf;
1287 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1288 * Linux binaries. If we see the call come through the FreeBSD ABI,
1289 * return an error back to the user since we do not to support this.
1291 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1292 uap->cmd == SHM_STAT)
1295 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1296 if (uap->cmd == IPC_SET) {
1297 if ((error = copyin(uap->buf, &old, sizeof(old))))
1299 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1300 CP(old, buf, shm_segsz);
1301 CP(old, buf, shm_lpid);
1302 CP(old, buf, shm_cpid);
1303 CP(old, buf, shm_nattch);
1304 CP(old, buf, shm_atime);
1305 CP(old, buf, shm_dtime);
1306 CP(old, buf, shm_ctime);
1309 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1313 /* Cases in which we need to copyout */
1316 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1317 if (buf.shm_segsz > INT_MAX)
1318 old.shm_segsz = INT_MAX;
1320 CP(buf, old, shm_segsz);
1321 CP(buf, old, shm_lpid);
1322 CP(buf, old, shm_cpid);
1323 if (buf.shm_nattch > SHRT_MAX)
1324 old.shm_nattch = SHRT_MAX;
1326 CP(buf, old, shm_nattch);
1327 CP(buf, old, shm_atime);
1328 CP(buf, old, shm_dtime);
1329 CP(buf, old, shm_ctime);
1330 old.shm_internal = NULL;
1331 error = copyout(&old, uap->buf, sizeof(old));
1337 /* Invalidate the return value */
1338 td->td_retval[0] = -1;
1343 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1347 sysvshm_modload(struct module *module, int cmd, void *arg)
1358 error = shmunload();
1369 static moduledata_t sysvshm_mod = {
1375 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1376 MODULE_VERSION(sysvshm, 1);