2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
26 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by Adam Glass and Charles
40 * 4. The names of the authors may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 #include <sys/appleapiopts.h>
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/shm_internal.h>
61 #include <sys/proc_internal.h>
62 #include <sys/kauth.h>
63 #include <sys/malloc.h>
66 #include <sys/sysctl.h>
68 #include <sys/sysent.h>
69 #include <sys/sysproto.h>
71 #include <bsm/audit_kernel.h>
73 #include <mach/mach_types.h>
74 #include <mach/vm_inherit.h>
75 #include <mach/vm_map.h>
77 #include <mach/mach_vm.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_shared_memory_server.h>
81 #include <vm/vm_protos.h>
83 #include <kern/locks.h>
85 static void shminit(void *);
87 SYSINIT(sysv_shm
, SI_SUB_SYSV_SHM
, SI_ORDER_FIRST
, shminit
, NULL
)
90 static lck_grp_t
*sysv_shm_subsys_lck_grp
;
91 static lck_grp_attr_t
*sysv_shm_subsys_lck_grp_attr
;
92 static lck_attr_t
*sysv_shm_subsys_lck_attr
;
93 static lck_mtx_t sysv_shm_subsys_mutex
;
95 #define SYSV_SHM_SUBSYS_LOCK() lck_mtx_lock(&sysv_shm_subsys_mutex)
96 #define SYSV_SHM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_shm_subsys_mutex)
98 static int oshmctl(void *p
, void *uap
, void *retval
);
99 static int shmget_allocate_segment(struct proc
*p
, struct shmget_args
*uap
, int mode
, int * retval
);
100 static int shmget_existing(struct shmget_args
*uap
, int mode
, int segnum
, int * retval
);
101 static void shmid_ds_64to32(struct user_shmid_ds
*in
, struct shmid_ds
*out
);
102 static void shmid_ds_32to64(struct shmid_ds
*in
, struct user_shmid_ds
*out
);
104 /* XXX casting to (sy_call_t *) is bogus, as usual. */
105 static sy_call_t
*shmcalls
[] = {
106 (sy_call_t
*)shmat
, (sy_call_t
*)oshmctl
,
107 (sy_call_t
*)shmdt
, (sy_call_t
*)shmget
,
111 #define SHMSEG_FREE 0x0200
112 #define SHMSEG_REMOVED 0x0400
113 #define SHMSEG_ALLOCATED 0x0800
114 #define SHMSEG_WANTED 0x1000
116 static int shm_last_free
, shm_nused
, shm_committed
;
117 struct user_shmid_ds
*shmsegs
; /* 64 bit version */
118 static int shm_inited
= 0;
121 void * shm_object
; /* vm_offset_t kva; */
124 struct shmmap_state
{
125 mach_vm_address_t va
; /* user address */
126 int shmid
; /* segment id */
129 static void shm_deallocate_segment(struct user_shmid_ds
*);
130 static int shm_find_segment_by_key(key_t
);
131 static struct user_shmid_ds
*shm_find_segment_by_shmid(int);
132 static int shm_delete_mapping(struct proc
*, struct shmmap_state
*, int);
134 #ifdef __APPLE_API_PRIVATE
135 struct shminfo shminfo
= {
136 -1, /* SHMMAX 4096 *1024 */
140 -1 /* SHMALL = 1024 */
142 #endif /* __APPLE_API_PRIVATE */
144 void sysv_shm_lock_init(void);
146 static __inline__
time_t
155 * This conversion is safe, since if we are converting for a 32 bit process,
156 * then it's value of (struct shmid_ds)->shm_segsz will never exceed 4G.
158 * NOTE: Source and target may *NOT* overlap! (target is smaller)
161 shmid_ds_64to32(struct user_shmid_ds
*in
, struct shmid_ds
*out
)
163 out
->shm_perm
= in
->shm_perm
;
164 out
->shm_segsz
= (size_t)in
->shm_segsz
;
165 out
->shm_lpid
= in
->shm_lpid
;
166 out
->shm_cpid
= in
->shm_cpid
;
167 out
->shm_nattch
= in
->shm_nattch
;
168 out
->shm_atime
= in
->shm_atime
;
169 out
->shm_dtime
= in
->shm_dtime
;
170 out
->shm_ctime
= in
->shm_ctime
;
171 out
->shm_internal
= CAST_DOWN(void *,in
->shm_internal
);
175 * NOTE: Source and target may are permitted to overlap! (source is smaller);
176 * this works because we copy fields in order from the end of the struct to
180 shmid_ds_32to64(struct shmid_ds
*in
, struct user_shmid_ds
*out
)
182 out
->shm_internal
= CAST_USER_ADDR_T(in
->shm_internal
);
183 out
->shm_ctime
= in
->shm_ctime
;
184 out
->shm_dtime
= in
->shm_dtime
;
185 out
->shm_atime
= in
->shm_atime
;
186 out
->shm_nattch
= in
->shm_nattch
;
187 out
->shm_cpid
= in
->shm_cpid
;
188 out
->shm_lpid
= in
->shm_lpid
;
189 out
->shm_segsz
= (user_size_t
)in
->shm_segsz
;
190 out
->shm_perm
= in
->shm_perm
;
195 shm_find_segment_by_key(key_t key
)
199 for (i
= 0; i
< shminfo
.shmmni
; i
++)
200 if ((shmsegs
[i
].shm_perm
.mode
& SHMSEG_ALLOCATED
) &&
201 shmsegs
[i
].shm_perm
.key
== key
)
206 static struct user_shmid_ds
*
207 shm_find_segment_by_shmid(int shmid
)
210 struct user_shmid_ds
*shmseg
;
212 segnum
= IPCID_TO_IX(shmid
);
213 if (segnum
< 0 || segnum
>= shminfo
.shmmni
)
215 shmseg
= &shmsegs
[segnum
];
216 if ((shmseg
->shm_perm
.mode
& (SHMSEG_ALLOCATED
| SHMSEG_REMOVED
))
217 != SHMSEG_ALLOCATED
||
218 shmseg
->shm_perm
.seq
!= IPCID_TO_SEQ(shmid
))
224 shm_deallocate_segment(struct user_shmid_ds
*shmseg
)
226 struct shm_handle
*shm_handle
;
229 shm_handle
= CAST_DOWN(void *,shmseg
->shm_internal
); /* tunnel */
230 size
= mach_vm_round_page(shmseg
->shm_segsz
);
231 mach_memory_entry_port_release(shm_handle
->shm_object
);
232 shm_handle
->shm_object
= NULL
;
233 FREE((caddr_t
)shm_handle
, M_SHM
);
234 shmseg
->shm_internal
= USER_ADDR_NULL
; /* tunnel */
235 shm_committed
-= btoc(size
);
237 shmseg
->shm_perm
.mode
= SHMSEG_FREE
;
241 shm_delete_mapping(__unused
struct proc
*p
, struct shmmap_state
*shmmap_s
,
244 struct user_shmid_ds
*shmseg
;
248 segnum
= IPCID_TO_IX(shmmap_s
->shmid
);
249 shmseg
= &shmsegs
[segnum
];
250 size
= mach_vm_round_page(shmseg
->shm_segsz
); /* XXX done for us? */
252 result
= mach_vm_deallocate(current_map(), shmmap_s
->va
, size
);
253 if (result
!= KERN_SUCCESS
)
256 shmmap_s
->shmid
= -1;
257 shmseg
->shm_dtime
= sysv_shmtime();
258 if ((--shmseg
->shm_nattch
<= 0) &&
259 (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
)) {
260 shm_deallocate_segment(shmseg
);
261 shm_last_free
= segnum
;
267 shmdt(struct proc
*p
, struct shmdt_args
*uap
, register_t
*retval
)
269 struct shmmap_state
*shmmap_s
;
273 // LP64todo - fix this
274 AUDIT_ARG(svipc_addr
, CAST_DOWN(void *,uap
->shmaddr
));
276 SYSV_SHM_SUBSYS_LOCK();
282 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
283 if (shmmap_s
== NULL
) {
288 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
289 if (shmmap_s
->shmid
!= -1 &&
290 shmmap_s
->va
== (mach_vm_offset_t
)uap
->shmaddr
)
292 if (i
== shminfo
.shmseg
) {
296 i
= shm_delete_mapping(p
, shmmap_s
, 1);
302 SYSV_SHM_SUBSYS_UNLOCK();
307 shmat(struct proc
*p
, struct shmat_args
*uap
, register_t
*retval
)
310 struct user_shmid_ds
*shmseg
;
311 struct shmmap_state
*shmmap_s
= NULL
;
312 struct shm_handle
*shm_handle
;
313 mach_vm_address_t attach_va
; /* attach address in/out */
314 mach_vm_size_t map_size
; /* size of map entry */
320 AUDIT_ARG(svipc_id
, uap
->shmid
);
321 // LP64todo - fix this
322 AUDIT_ARG(svipc_addr
, CAST_DOWN(void *,uap
->shmaddr
));
324 SYSV_SHM_SUBSYS_LOCK();
331 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
333 if (shmmap_s
== NULL
) {
334 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
335 MALLOC(shmmap_s
, struct shmmap_state
*, size
, M_SHM
, M_WAITOK
);
336 if (shmmap_s
== NULL
) {
340 for (i
= 0; i
< shminfo
.shmseg
; i
++)
341 shmmap_s
[i
].shmid
= -1;
342 p
->vm_shm
= (caddr_t
)shmmap_s
;
344 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
345 if (shmseg
== NULL
) {
350 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
351 error
= ipcperm(kauth_cred_get(), &shmseg
->shm_perm
,
352 (uap
->shmflg
& SHM_RDONLY
) ? IPC_R
: IPC_R
|IPC_W
);
358 for (i
= 0; i
< shminfo
.shmseg
; i
++) {
359 if (shmmap_s
->shmid
== -1)
363 if (i
>= shminfo
.shmseg
) {
368 map_size
= mach_vm_round_page(shmseg
->shm_segsz
);
370 if ((uap
->shmflg
& SHM_RDONLY
) == 0)
371 prot
|= VM_PROT_WRITE
;
372 flags
= MAP_ANON
| MAP_SHARED
;
376 attach_va
= (mach_vm_address_t
)uap
->shmaddr
;
377 if (uap
->shmflg
& SHM_RND
)
378 attach_va
&= ~(SHMLBA
-1);
379 else if ((attach_va
& (SHMLBA
-1)) != 0) {
384 shm_handle
= CAST_DOWN(void *, shmseg
->shm_internal
); /* tunnel */
386 rv
= mach_vm_map(current_map(), /* process map */
387 &attach_va
, /* attach address */
388 map_size
, /* segment size */
389 (mach_vm_offset_t
)0, /* alignment mask */
390 (flags
& MAP_FIXED
)? VM_FLAGS_FIXED
: VM_FLAGS_ANYWHERE
,
391 shm_handle
->shm_object
,
397 if (rv
!= KERN_SUCCESS
)
400 rv
= mach_vm_inherit(current_map(), attach_va
, map_size
, VM_INHERIT_SHARE
);
401 if (rv
!= KERN_SUCCESS
) {
402 (void)mach_vm_deallocate(current_map(), attach_va
, map_size
);
406 shmmap_s
->va
= attach_va
;
407 shmmap_s
->shmid
= uap
->shmid
;
408 shmseg
->shm_lpid
= p
->p_pid
;
409 shmseg
->shm_atime
= sysv_shmtime();
410 shmseg
->shm_nattch
++;
411 *retval
= attach_va
; /* XXX return -1 on error */
416 case KERN_INVALID_ADDRESS
:
419 case KERN_PROTECTION_FAILURE
:
425 SYSV_SHM_SUBSYS_UNLOCK();
430 oshmctl(__unused
void *p
, __unused
void *uap
, __unused
void *retval
)
436 shmctl(__unused
struct proc
*p
, struct shmctl_args
*uap
, register_t
*retval
)
439 kauth_cred_t cred
= kauth_cred_get();
440 struct user_shmid_ds inbuf
;
441 struct user_shmid_ds
*shmseg
;
442 size_t shmid_ds_sz
= sizeof(struct user_shmid_ds
);
446 AUDIT_ARG(svipc_cmd
, uap
->cmd
);
447 AUDIT_ARG(svipc_id
, uap
->shmid
);
449 SYSV_SHM_SUBSYS_LOCK();
456 if (!IS_64BIT_PROCESS(p
))
457 shmid_ds_sz
= sizeof(struct shmid_ds
);
459 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
460 if (shmseg
== NULL
) {
465 /* XXAUDIT: This is the perms BEFORE any change by this call. This
466 * may not be what is desired.
468 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
472 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_R
);
478 if (IS_64BIT_PROCESS(p
)) {
479 error
= copyout(shmseg
, uap
->buf
, sizeof(struct user_shmid_ds
));
481 struct shmid_ds shmid_ds32
;
482 shmid_ds_64to32(shmseg
, &shmid_ds32
);
483 error
= copyout(&shmid_ds32
, uap
->buf
, sizeof(struct shmid_ds
));
491 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
496 if (IS_64BIT_PROCESS(p
)) {
497 error
= copyin(uap
->buf
, &inbuf
, sizeof(struct user_shmid_ds
));
499 error
= copyin(uap
->buf
, &inbuf
, sizeof(struct shmid_ds
));
500 /* convert in place; ugly, but safe */
501 shmid_ds_32to64((struct shmid_ds
*)&inbuf
, &inbuf
);
507 shmseg
->shm_perm
.uid
= inbuf
.shm_perm
.uid
;
508 shmseg
->shm_perm
.gid
= inbuf
.shm_perm
.gid
;
509 shmseg
->shm_perm
.mode
=
510 (shmseg
->shm_perm
.mode
& ~ACCESSPERMS
) |
511 (inbuf
.shm_perm
.mode
& ACCESSPERMS
);
512 shmseg
->shm_ctime
= sysv_shmtime();
515 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
520 shmseg
->shm_perm
.key
= IPC_PRIVATE
;
521 shmseg
->shm_perm
.mode
|= SHMSEG_REMOVED
;
522 if (shmseg
->shm_nattch
<= 0) {
523 shm_deallocate_segment(shmseg
);
524 shm_last_free
= IPCID_TO_IX(uap
->shmid
);
538 SYSV_SHM_SUBSYS_UNLOCK();
543 shmget_existing(struct shmget_args
*uap
, int mode
, int segnum
, int *retval
)
545 struct user_shmid_ds
*shmseg
;
548 shmseg
= &shmsegs
[segnum
];
549 if (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
) {
551 * This segment is in the process of being allocated. Wait
552 * until it's done, and look the key up again (in case the
553 * allocation failed or it was freed).
555 shmseg
->shm_perm
.mode
|= SHMSEG_WANTED
;
556 error
= tsleep((caddr_t
)shmseg
, PLOCK
| PCATCH
, "shmget", 0);
561 error
= ipcperm(kauth_cred_get(), &shmseg
->shm_perm
, mode
);
564 if (uap
->size
&& uap
->size
> shmseg
->shm_segsz
)
566 if ((uap
->shmflg
& (IPC_CREAT
| IPC_EXCL
)) == (IPC_CREAT
| IPC_EXCL
))
568 *retval
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
573 shmget_allocate_segment(struct proc
*p
, struct shmget_args
*uap
, int mode
,
576 int i
, segnum
, shmid
, size
;
577 kauth_cred_t cred
= kauth_cred_get();
578 struct user_shmid_ds
*shmseg
;
579 struct shm_handle
*shm_handle
;
581 vm_offset_t user_addr
;
584 if (uap
->size
< (user_size_t
)shminfo
.shmmin
||
585 uap
->size
> (user_size_t
)shminfo
.shmmax
)
587 if (shm_nused
>= shminfo
.shmmni
) /* any shmids left? */
589 size
= mach_vm_round_page(uap
->size
);
590 if (shm_committed
+ btoc(size
) > shminfo
.shmall
)
592 if (shm_last_free
< 0) {
593 for (i
= 0; i
< shminfo
.shmmni
; i
++)
594 if (shmsegs
[i
].shm_perm
.mode
& SHMSEG_FREE
)
596 if (i
== shminfo
.shmmni
)
597 panic("shmseg free count inconsistent");
600 segnum
= shm_last_free
;
603 shmseg
= &shmsegs
[segnum
];
605 * In case we sleep in malloc(), mark the segment present but deleted
606 * so that noone else tries to create the same key.
608 kret
= vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
609 if (kret
!= KERN_SUCCESS
)
612 kret
= mach_make_memory_entry (current_map(), &size
, user_addr
,
613 VM_PROT_DEFAULT
, (mem_entry_name_port_t
*)&mem_object
, 0);
615 if (kret
!= KERN_SUCCESS
)
618 vm_deallocate(current_map(), user_addr
, size
);
620 shmseg
->shm_perm
.mode
= SHMSEG_ALLOCATED
| SHMSEG_REMOVED
;
621 shmseg
->shm_perm
.key
= uap
->key
;
622 shmseg
->shm_perm
.seq
= (shmseg
->shm_perm
.seq
+ 1) & 0x7fff;
623 MALLOC(shm_handle
, struct shm_handle
*, sizeof(struct shm_handle
), M_SHM
, M_WAITOK
);
624 if (shm_handle
== NULL
) {
625 kret
= KERN_NO_SPACE
;
626 mach_memory_entry_port_release(mem_object
);
630 shm_handle
->shm_object
= mem_object
;
631 shmid
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
633 shmseg
->shm_internal
= CAST_USER_ADDR_T(shm_handle
); /* tunnel */
634 shmseg
->shm_perm
.cuid
= shmseg
->shm_perm
.uid
= kauth_cred_getuid(cred
);
635 shmseg
->shm_perm
.cgid
= shmseg
->shm_perm
.gid
= cred
->cr_gid
;
636 shmseg
->shm_perm
.mode
= (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) |
637 (mode
& ACCESSPERMS
) | SHMSEG_ALLOCATED
;
638 shmseg
->shm_segsz
= uap
->size
;
639 shmseg
->shm_cpid
= p
->p_pid
;
640 shmseg
->shm_lpid
= shmseg
->shm_nattch
= 0;
641 shmseg
->shm_atime
= shmseg
->shm_dtime
= 0;
642 shmseg
->shm_ctime
= sysv_shmtime();
643 shm_committed
+= btoc(size
);
645 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
646 if (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) {
648 * Somebody else wanted this key while we were asleep. Wake
651 shmseg
->shm_perm
.mode
&= ~SHMSEG_WANTED
;
652 wakeup((caddr_t
)shmseg
);
655 AUDIT_ARG(svipc_id
, shmid
);
659 case KERN_INVALID_ADDRESS
:
662 case KERN_PROTECTION_FAILURE
:
671 shmget(struct proc
*p
, struct shmget_args
*uap
, register_t
*retval
)
673 int segnum
, mode
, error
;
676 /* Auditing is actually done in shmget_allocate_segment() */
678 SYSV_SHM_SUBSYS_LOCK();
685 mode
= uap
->shmflg
& ACCESSPERMS
;
686 if (uap
->key
!= IPC_PRIVATE
) {
688 segnum
= shm_find_segment_by_key(uap
->key
);
690 error
= shmget_existing(uap
, mode
, segnum
, retval
);
696 if ((uap
->shmflg
& IPC_CREAT
) == 0) {
701 shmget_ret
= shmget_allocate_segment(p
, uap
, mode
, retval
);
703 SYSV_SHM_SUBSYS_UNLOCK();
709 /* XXX actually varargs. */
711 shmsys(struct proc
*p
, struct shmsys_args
*uap
, register_t
*retval
)
714 /* The routine that we are dispatching already does this */
716 if (uap
->which
>= sizeof(shmcalls
)/sizeof(shmcalls
[0]))
718 return ((*shmcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
722 * Return 0 on success, 1 on failure.
725 shmfork(struct proc
*p1
, struct proc
*p2
)
727 struct shmmap_state
*shmmap_s
;
732 SYSV_SHM_SUBSYS_LOCK();
739 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
740 MALLOC(shmmap_s
, struct shmmap_state
*, size
, M_SHM
, M_WAITOK
);
741 if (shmmap_s
!= NULL
) {
742 bcopy((caddr_t
)p1
->vm_shm
, (caddr_t
)shmmap_s
, size
);
743 p2
->vm_shm
= (caddr_t
)shmmap_s
;
744 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
745 if (shmmap_s
->shmid
!= -1)
746 shmsegs
[IPCID_TO_IX(shmmap_s
->shmid
)].shm_nattch
++;
751 shmfork_ret
= 1; /* failed to copy to child - ENOMEM */
753 SYSV_SHM_SUBSYS_UNLOCK();
758 shmexit(struct proc
*p
)
760 struct shmmap_state
*shmmap_s
;
763 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
765 SYSV_SHM_SUBSYS_LOCK();
766 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
767 if (shmmap_s
->shmid
!= -1)
768 shm_delete_mapping(p
, shmmap_s
, 1);
769 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
771 SYSV_SHM_SUBSYS_UNLOCK();
775 * shmexec() is like shmexit(), only it doesn't delete the mappings,
776 * since the old address space has already been destroyed and the new
777 * one instantiated. Instead, it just does the housekeeping work we
778 * need to do to keep the System V shared memory subsystem sane.
780 __private_extern__
void
781 shmexec(struct proc
*p
)
783 struct shmmap_state
*shmmap_s
;
786 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
787 SYSV_SHM_SUBSYS_LOCK();
788 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
789 if (shmmap_s
->shmid
!= -1)
790 shm_delete_mapping(p
, shmmap_s
, 0);
791 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
793 SYSV_SHM_SUBSYS_UNLOCK();
797 shminit(__unused
void *dummy
)
804 * we store internally 64 bit, since if we didn't, we would
805 * be unable to represent a segment size in excess of 32 bits
806 * with the (struct shmid_ds)->shm_segsz field; also, POSIX
807 * dictates this filed be a size_t, which is 64 bits when
808 * running 64 bit binaries.
810 s
= sizeof(struct user_shmid_ds
) * shminfo
.shmmni
;
812 MALLOC(shmsegs
, struct user_shmid_ds
*, s
, M_SHM
, M_WAITOK
);
813 if (shmsegs
== NULL
) {
814 /* XXX fail safely: leave shared memory uninited */
817 for (i
= 0; i
< shminfo
.shmmni
; i
++) {
818 shmsegs
[i
].shm_perm
.mode
= SHMSEG_FREE
;
819 shmsegs
[i
].shm_perm
.seq
= 0;
827 /* Initialize the mutex governing access to the SysV shm subsystem */
828 __private_extern__
void
829 sysv_shm_lock_init( void )
832 sysv_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
833 lck_grp_attr_setstat(sysv_shm_subsys_lck_grp_attr
);
835 sysv_shm_subsys_lck_grp
= lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_shm_subsys_lck_grp_attr
);
837 sysv_shm_subsys_lck_attr
= lck_attr_alloc_init();
838 /* lck_attr_setdebug(sysv_shm_subsys_lck_attr); */
839 lck_mtx_init(&sysv_shm_subsys_mutex
, sysv_shm_subsys_lck_grp
, sysv_shm_subsys_lck_attr
);
842 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
843 struct sysctl_req *req) */
845 sysctl_shminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
846 __unused
int arg2
, struct sysctl_req
*req
)
849 int sysctl_shminfo_ret
= 0;
851 error
= SYSCTL_OUT(req
, arg1
, sizeof(user_ssize_t
));
852 if (error
|| req
->newptr
== USER_ADDR_NULL
)
855 SYSV_SHM_SUBSYS_LOCK();
856 /* Set the values only if shared memory is not initialised */
858 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(user_ssize_t
)))
860 sysctl_shminfo_ret
= error
;
861 goto sysctl_shminfo_out
;
864 if (arg1
== &shminfo
.shmmax
) {
865 if (shminfo
.shmmax
& PAGE_MASK_64
) {
866 shminfo
.shmmax
= (user_ssize_t
)-1;
867 sysctl_shminfo_ret
= EINVAL
;
868 goto sysctl_shminfo_out
;
872 /* Initialize only when all values are set */
873 if ((shminfo
.shmmax
!= (user_ssize_t
)-1) &&
874 (shminfo
.shmmin
!= (user_ssize_t
)-1) &&
875 (shminfo
.shmmni
!= (user_ssize_t
)-1) &&
876 (shminfo
.shmseg
!= (user_ssize_t
)-1) &&
877 (shminfo
.shmall
!= (user_ssize_t
)-1)) {
881 sysctl_shminfo_ret
= 0;
883 SYSV_SHM_SUBSYS_UNLOCK();
884 return sysctl_shminfo_ret
;
888 IPCS_shm_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
889 __unused
int arg2
, struct sysctl_req
*req
)
894 struct IPCS_command u32
;
895 struct user_IPCS_command u64
;
897 struct shmid_ds shmid_ds32
; /* post conversion, 32 bit version */
899 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
900 size_t shmid_ds_sz
= sizeof(struct user_shmid_ds
);
901 struct proc
*p
= current_proc();
903 int ipcs__shminfo_ret
= 0;
905 SYSV_SHM_SUBSYS_LOCK();
909 goto ipcs_shm_sysctl_out
;
912 if (!IS_64BIT_PROCESS(p
)) {
913 ipcs_sz
= sizeof(struct IPCS_command
);
914 shmid_ds_sz
= sizeof(struct shmid_ds
);
917 /* Copy in the command structure */
918 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
919 goto ipcs_shm_sysctl_out
;
922 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
923 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
925 /* Let us version this interface... */
926 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
928 goto ipcs_shm_sysctl_out
;
931 switch(ipcs
.u64
.ipcs_op
) {
932 case IPCS_SHM_CONF
: /* Obtain global configuration data */
933 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct shminfo
)) {
934 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
941 error
= copyout(&shminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
944 case IPCS_SHM_ITER
: /* Iterate over existing segments */
945 cursor
= ipcs
.u64
.ipcs_cursor
;
946 if (cursor
< 0 || cursor
>= shminfo
.shmmni
) {
950 if (ipcs
.u64
.ipcs_datalen
!= (int)shmid_ds_sz
) {
954 for( ; cursor
< shminfo
.shmmni
; cursor
++) {
955 if (shmsegs
[cursor
].shm_perm
.mode
& SHMSEG_ALLOCATED
)
959 if (cursor
== shminfo
.shmmni
) {
964 shmid_dsp
= &shmsegs
[cursor
]; /* default: 64 bit */
967 * If necessary, convert the 64 bit kernel segment
968 * descriptor to a 32 bit user one.
970 if (!IS_64BIT_PROCESS(p
)) {
971 shmid_ds_64to32(shmid_dsp
, &shmid_ds32
);
972 shmid_dsp
= &shmid_ds32
;
974 error
= copyout(shmid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
977 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
979 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
980 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
981 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
990 SYSV_SHM_SUBSYS_UNLOCK();
994 SYSCTL_NODE(_kern
, KERN_SYSV
, sysv
, CTLFLAG_RW
, 0, "SYSV");
996 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMAX
, shmmax
, CTLTYPE_QUAD
| CTLFLAG_RW
,
997 &shminfo
.shmmax
, 0, &sysctl_shminfo
,"Q","shmmax");
999 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMIN
, shmmin
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1000 &shminfo
.shmmin
, 0, &sysctl_shminfo
,"Q","shmmin");
1002 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMNI
, shmmni
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1003 &shminfo
.shmmni
, 0, &sysctl_shminfo
,"Q","shmmni");
1005 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMSEG
, shmseg
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1006 &shminfo
.shmseg
, 0, &sysctl_shminfo
,"Q","shmseg");
1008 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMALL
, shmall
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1009 &shminfo
.shmall
, 0, &sysctl_shminfo
,"Q","shmall");
1011 SYSCTL_NODE(_kern_sysv
, OID_AUTO
, ipcs
, CTLFLAG_RW
, 0, "SYSVIPCS");
1013 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, shm
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1014 0, 0, IPCS_shm_sysctl
,
1015 "S,IPCS_shm_command",
1016 "ipcs shm command interface");